s3c-hwmon.c 10 KB

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