s3c-hwmon.c 10 KB

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