s3c-hwmon.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* linux/drivers/hwmon/s3c-hwmon.c
  3. *
  4. * Copyright (C) 2005, 2008, 2009 Simtec Electronics
  5. * http://armlinux.simtec.co.uk/
  6. * Ben Dooks <[email protected]>
  7. *
  8. * S3C24XX/S3C64XX ADC hwmon support
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/io.h>
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/clk.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/hwmon.h>
  19. #include <linux/hwmon-sysfs.h>
  20. #include <linux/soc/samsung/s3c-adc.h>
  21. #include <linux/platform_data/hwmon-s3c.h>
  22. struct s3c_hwmon_attr {
  23. struct sensor_device_attribute in;
  24. struct sensor_device_attribute label;
  25. char in_name[12];
  26. char label_name[12];
  27. };
  28. /**
  29. * struct s3c_hwmon - ADC hwmon client information
  30. * @lock: Access lock to serialise the conversions.
  31. * @client: The client we registered with the S3C ADC core.
  32. * @hwmon_dev: The hwmon device we created.
  33. * @attr: The holders for the channel attributes.
  34. */
  35. struct s3c_hwmon {
  36. struct mutex lock;
  37. struct s3c_adc_client *client;
  38. struct device *hwmon_dev;
  39. struct s3c_hwmon_attr attrs[8];
  40. };
  41. /**
  42. * s3c_hwmon_read_ch - read a value from a given adc channel.
  43. * @dev: The device.
  44. * @hwmon: Our state.
  45. * @channel: The channel we're reading from.
  46. *
  47. * Read a value from the @channel with the proper locking and sleep until
  48. * either the read completes or we timeout awaiting the ADC core to get
  49. * back to us.
  50. */
  51. static int s3c_hwmon_read_ch(struct device *dev,
  52. struct s3c_hwmon *hwmon, int channel)
  53. {
  54. int ret;
  55. ret = mutex_lock_interruptible(&hwmon->lock);
  56. if (ret < 0)
  57. return ret;
  58. dev_dbg(dev, "reading channel %d\n", channel);
  59. ret = s3c_adc_read(hwmon->client, channel);
  60. mutex_unlock(&hwmon->lock);
  61. return ret;
  62. }
  63. #ifdef CONFIG_SENSORS_S3C_RAW
  64. /**
  65. * s3c_hwmon_show_raw - show a conversion from the raw channel number.
  66. * @dev: The device that the attribute belongs to.
  67. * @attr: The attribute being read.
  68. * @buf: The result buffer.
  69. *
  70. * This show deals with the raw attribute, registered for each possible
  71. * ADC channel. This does a conversion and returns the raw (un-scaled)
  72. * value returned from the hardware.
  73. */
  74. static ssize_t s3c_hwmon_show_raw(struct device *dev,
  75. struct device_attribute *attr, char *buf)
  76. {
  77. struct s3c_hwmon *adc = dev_get_drvdata(dev);
  78. struct sensor_device_attribute *sa = to_sensor_dev_attr(attr);
  79. int ret;
  80. ret = s3c_hwmon_read_ch(dev, adc, sa->index);
  81. return (ret < 0) ? ret : snprintf(buf, PAGE_SIZE, "%d\n", ret);
  82. }
  83. static SENSOR_DEVICE_ATTR(adc0_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 0);
  84. static SENSOR_DEVICE_ATTR(adc1_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 1);
  85. static SENSOR_DEVICE_ATTR(adc2_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 2);
  86. static SENSOR_DEVICE_ATTR(adc3_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 3);
  87. static SENSOR_DEVICE_ATTR(adc4_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 4);
  88. static SENSOR_DEVICE_ATTR(adc5_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 5);
  89. static SENSOR_DEVICE_ATTR(adc6_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 6);
  90. static SENSOR_DEVICE_ATTR(adc7_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 7);
  91. static struct attribute *s3c_hwmon_attrs[9] = {
  92. &sensor_dev_attr_adc0_raw.dev_attr.attr,
  93. &sensor_dev_attr_adc1_raw.dev_attr.attr,
  94. &sensor_dev_attr_adc2_raw.dev_attr.attr,
  95. &sensor_dev_attr_adc3_raw.dev_attr.attr,
  96. &sensor_dev_attr_adc4_raw.dev_attr.attr,
  97. &sensor_dev_attr_adc5_raw.dev_attr.attr,
  98. &sensor_dev_attr_adc6_raw.dev_attr.attr,
  99. &sensor_dev_attr_adc7_raw.dev_attr.attr,
  100. NULL,
  101. };
  102. static struct attribute_group s3c_hwmon_attrgroup = {
  103. .attrs = s3c_hwmon_attrs,
  104. };
  105. static inline int s3c_hwmon_add_raw(struct device *dev)
  106. {
  107. return sysfs_create_group(&dev->kobj, &s3c_hwmon_attrgroup);
  108. }
  109. static inline void s3c_hwmon_remove_raw(struct device *dev)
  110. {
  111. sysfs_remove_group(&dev->kobj, &s3c_hwmon_attrgroup);
  112. }
  113. #else
  114. static inline int s3c_hwmon_add_raw(struct device *dev) { return 0; }
  115. static inline void s3c_hwmon_remove_raw(struct device *dev) { }
  116. #endif /* CONFIG_SENSORS_S3C_RAW */
  117. /**
  118. * s3c_hwmon_ch_show - show value of a given channel
  119. * @dev: The device that the attribute belongs to.
  120. * @attr: The attribute being read.
  121. * @buf: The result buffer.
  122. *
  123. * Read a value from the ADC and scale it before returning it to the
  124. * caller. The scale factor is gained from the channel configuration
  125. * passed via the platform data when the device was registered.
  126. */
  127. static ssize_t s3c_hwmon_ch_show(struct device *dev,
  128. struct device_attribute *attr,
  129. char *buf)
  130. {
  131. struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
  132. struct s3c_hwmon *hwmon = dev_get_drvdata(dev);
  133. struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev);
  134. struct s3c_hwmon_chcfg *cfg;
  135. int ret;
  136. cfg = pdata->in[sen_attr->index];
  137. ret = s3c_hwmon_read_ch(dev, hwmon, sen_attr->index);
  138. if (ret < 0)
  139. return ret;
  140. ret *= cfg->mult;
  141. ret = DIV_ROUND_CLOSEST(ret, cfg->div);
  142. return sysfs_emit(buf, "%d\n", ret);
  143. }
  144. /**
  145. * s3c_hwmon_label_show - show label name of the given channel.
  146. * @dev: The device that the attribute belongs to.
  147. * @attr: The attribute being read.
  148. * @buf: The result buffer.
  149. *
  150. * Return the label name of a given channel
  151. */
  152. static ssize_t s3c_hwmon_label_show(struct device *dev,
  153. struct device_attribute *attr,
  154. char *buf)
  155. {
  156. struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
  157. struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev);
  158. struct s3c_hwmon_chcfg *cfg;
  159. cfg = pdata->in[sen_attr->index];
  160. return sysfs_emit(buf, "%s\n", cfg->name);
  161. }
  162. /**
  163. * s3c_hwmon_create_attr - create hwmon attribute for given channel.
  164. * @dev: The device to create the attribute on.
  165. * @cfg: The channel configuration passed from the platform data.
  166. * @channel: The ADC channel number to process.
  167. *
  168. * Create the scaled attribute for use with hwmon from the specified
  169. * platform data in @pdata. The sysfs entry is handled by the routine
  170. * s3c_hwmon_ch_show().
  171. *
  172. * The attribute name is taken from the configuration data if present
  173. * otherwise the name is taken by concatenating in_ with the channel
  174. * number.
  175. */
  176. static int s3c_hwmon_create_attr(struct device *dev,
  177. struct s3c_hwmon_chcfg *cfg,
  178. struct s3c_hwmon_attr *attrs,
  179. int channel)
  180. {
  181. struct sensor_device_attribute *attr;
  182. int ret;
  183. snprintf(attrs->in_name, sizeof(attrs->in_name), "in%d_input", channel);
  184. attr = &attrs->in;
  185. attr->index = channel;
  186. sysfs_attr_init(&attr->dev_attr.attr);
  187. attr->dev_attr.attr.name = attrs->in_name;
  188. attr->dev_attr.attr.mode = S_IRUGO;
  189. attr->dev_attr.show = s3c_hwmon_ch_show;
  190. ret = device_create_file(dev, &attr->dev_attr);
  191. if (ret < 0) {
  192. dev_err(dev, "failed to create input attribute\n");
  193. return ret;
  194. }
  195. /* if this has a name, add a label */
  196. if (cfg->name) {
  197. snprintf(attrs->label_name, sizeof(attrs->label_name),
  198. "in%d_label", channel);
  199. attr = &attrs->label;
  200. attr->index = channel;
  201. sysfs_attr_init(&attr->dev_attr.attr);
  202. attr->dev_attr.attr.name = attrs->label_name;
  203. attr->dev_attr.attr.mode = S_IRUGO;
  204. attr->dev_attr.show = s3c_hwmon_label_show;
  205. ret = device_create_file(dev, &attr->dev_attr);
  206. if (ret < 0) {
  207. device_remove_file(dev, &attrs->in.dev_attr);
  208. dev_err(dev, "failed to create label attribute\n");
  209. }
  210. }
  211. return ret;
  212. }
  213. static void s3c_hwmon_remove_attr(struct device *dev,
  214. struct s3c_hwmon_attr *attrs)
  215. {
  216. device_remove_file(dev, &attrs->in.dev_attr);
  217. device_remove_file(dev, &attrs->label.dev_attr);
  218. }
  219. /**
  220. * s3c_hwmon_probe - device probe entry.
  221. * @dev: The device being probed.
  222. */
  223. static int s3c_hwmon_probe(struct platform_device *dev)
  224. {
  225. struct s3c_hwmon_pdata *pdata = dev_get_platdata(&dev->dev);
  226. struct s3c_hwmon *hwmon;
  227. int ret = 0;
  228. int i;
  229. if (!pdata) {
  230. dev_err(&dev->dev, "no platform data supplied\n");
  231. return -EINVAL;
  232. }
  233. hwmon = devm_kzalloc(&dev->dev, sizeof(struct s3c_hwmon), GFP_KERNEL);
  234. if (hwmon == NULL)
  235. return -ENOMEM;
  236. platform_set_drvdata(dev, hwmon);
  237. mutex_init(&hwmon->lock);
  238. /* Register with the core ADC driver. */
  239. hwmon->client = s3c_adc_register(dev, NULL, NULL, 0);
  240. if (IS_ERR(hwmon->client)) {
  241. dev_err(&dev->dev, "cannot register adc\n");
  242. return PTR_ERR(hwmon->client);
  243. }
  244. /* add attributes for our adc devices. */
  245. ret = s3c_hwmon_add_raw(&dev->dev);
  246. if (ret)
  247. goto err_registered;
  248. /* register with the hwmon core */
  249. hwmon->hwmon_dev = hwmon_device_register(&dev->dev);
  250. if (IS_ERR(hwmon->hwmon_dev)) {
  251. dev_err(&dev->dev, "error registering with hwmon\n");
  252. ret = PTR_ERR(hwmon->hwmon_dev);
  253. goto err_raw_attribute;
  254. }
  255. for (i = 0; i < ARRAY_SIZE(pdata->in); i++) {
  256. struct s3c_hwmon_chcfg *cfg = pdata->in[i];
  257. if (!cfg)
  258. continue;
  259. if (cfg->mult >= 0x10000)
  260. dev_warn(&dev->dev,
  261. "channel %d multiplier too large\n",
  262. i);
  263. if (cfg->div == 0) {
  264. dev_err(&dev->dev, "channel %d divider zero\n", i);
  265. continue;
  266. }
  267. ret = s3c_hwmon_create_attr(&dev->dev, pdata->in[i],
  268. &hwmon->attrs[i], i);
  269. if (ret) {
  270. dev_err(&dev->dev,
  271. "error creating channel %d\n", i);
  272. for (i--; i >= 0; i--)
  273. s3c_hwmon_remove_attr(&dev->dev,
  274. &hwmon->attrs[i]);
  275. goto err_hwmon_register;
  276. }
  277. }
  278. return 0;
  279. err_hwmon_register:
  280. hwmon_device_unregister(hwmon->hwmon_dev);
  281. err_raw_attribute:
  282. s3c_hwmon_remove_raw(&dev->dev);
  283. err_registered:
  284. s3c_adc_release(hwmon->client);
  285. return ret;
  286. }
  287. static int s3c_hwmon_remove(struct platform_device *dev)
  288. {
  289. struct s3c_hwmon *hwmon = platform_get_drvdata(dev);
  290. int i;
  291. s3c_hwmon_remove_raw(&dev->dev);
  292. for (i = 0; i < ARRAY_SIZE(hwmon->attrs); i++)
  293. s3c_hwmon_remove_attr(&dev->dev, &hwmon->attrs[i]);
  294. hwmon_device_unregister(hwmon->hwmon_dev);
  295. s3c_adc_release(hwmon->client);
  296. return 0;
  297. }
  298. static struct platform_driver s3c_hwmon_driver = {
  299. .driver = {
  300. .name = "s3c-hwmon",
  301. },
  302. .probe = s3c_hwmon_probe,
  303. .remove = s3c_hwmon_remove,
  304. };
  305. module_platform_driver(s3c_hwmon_driver);
  306. MODULE_AUTHOR("Ben Dooks <[email protected]>");
  307. MODULE_DESCRIPTION("S3C ADC HWMon driver");
  308. MODULE_LICENSE("GPL v2");
  309. MODULE_ALIAS("platform:s3c-hwmon");