s3c-hwmon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. attr->dev_attr.attr.name = attrs->in_name;
  202. attr->dev_attr.attr.mode = S_IRUGO;
  203. attr->dev_attr.show = s3c_hwmon_ch_show;
  204. ret = device_create_file(dev, &attr->dev_attr);
  205. if (ret < 0) {
  206. dev_err(dev, "failed to create input attribute\n");
  207. return ret;
  208. }
  209. /* if this has a name, add a label */
  210. if (cfg->name) {
  211. snprintf(attrs->label_name, sizeof(attrs->label_name),
  212. "in%d_label", channel);
  213. attr = &attrs->label;
  214. attr->index = channel;
  215. attr->dev_attr.attr.name = attrs->label_name;
  216. attr->dev_attr.attr.mode = S_IRUGO;
  217. attr->dev_attr.show = s3c_hwmon_label_show;
  218. ret = device_create_file(dev, &attr->dev_attr);
  219. if (ret < 0) {
  220. device_remove_file(dev, &attrs->in.dev_attr);
  221. dev_err(dev, "failed to create label attribute\n");
  222. }
  223. }
  224. return ret;
  225. }
  226. static void s3c_hwmon_remove_attr(struct device *dev,
  227. struct s3c_hwmon_attr *attrs)
  228. {
  229. device_remove_file(dev, &attrs->in.dev_attr);
  230. device_remove_file(dev, &attrs->label.dev_attr);
  231. }
  232. /**
  233. * s3c_hwmon_probe - device probe entry.
  234. * @dev: The device being probed.
  235. */
  236. static int __devinit s3c_hwmon_probe(struct platform_device *dev)
  237. {
  238. struct s3c_hwmon_pdata *pdata = dev->dev.platform_data;
  239. struct s3c_hwmon *hwmon;
  240. int ret = 0;
  241. int i;
  242. if (!pdata) {
  243. dev_err(&dev->dev, "no platform data supplied\n");
  244. return -EINVAL;
  245. }
  246. hwmon = kzalloc(sizeof(struct s3c_hwmon), GFP_KERNEL);
  247. if (hwmon == NULL) {
  248. dev_err(&dev->dev, "no memory\n");
  249. return -ENOMEM;
  250. }
  251. platform_set_drvdata(dev, hwmon);
  252. mutex_init(&hwmon->lock);
  253. /* Register with the core ADC driver. */
  254. hwmon->client = s3c_adc_register(dev, NULL, NULL, 0);
  255. if (IS_ERR(hwmon->client)) {
  256. dev_err(&dev->dev, "cannot register adc\n");
  257. ret = PTR_ERR(hwmon->client);
  258. goto err_mem;
  259. }
  260. /* add attributes for our adc devices. */
  261. ret = s3c_hwmon_add_raw(&dev->dev);
  262. if (ret)
  263. goto err_registered;
  264. /* register with the hwmon core */
  265. hwmon->hwmon_dev = hwmon_device_register(&dev->dev);
  266. if (IS_ERR(hwmon->hwmon_dev)) {
  267. dev_err(&dev->dev, "error registering with hwmon\n");
  268. ret = PTR_ERR(hwmon->hwmon_dev);
  269. goto err_raw_attribute;
  270. }
  271. for (i = 0; i < ARRAY_SIZE(pdata->in); i++) {
  272. struct s3c_hwmon_chcfg *cfg = pdata->in[i];
  273. if (!cfg)
  274. continue;
  275. if (cfg->mult >= 0x10000)
  276. dev_warn(&dev->dev,
  277. "channel %d multiplier too large\n",
  278. i);
  279. if (cfg->div == 0) {
  280. dev_err(&dev->dev, "channel %d divider zero\n", i);
  281. continue;
  282. }
  283. ret = s3c_hwmon_create_attr(&dev->dev, pdata->in[i],
  284. &hwmon->attrs[i], i);
  285. if (ret) {
  286. dev_err(&dev->dev,
  287. "error creating channel %d\n", i);
  288. for (i--; i >= 0; i--)
  289. s3c_hwmon_remove_attr(&dev->dev,
  290. &hwmon->attrs[i]);
  291. goto err_hwmon_register;
  292. }
  293. }
  294. return 0;
  295. err_hwmon_register:
  296. hwmon_device_unregister(hwmon->hwmon_dev);
  297. err_raw_attribute:
  298. s3c_hwmon_remove_raw(&dev->dev);
  299. err_registered:
  300. s3c_adc_release(hwmon->client);
  301. err_mem:
  302. kfree(hwmon);
  303. return ret;
  304. }
  305. static int __devexit s3c_hwmon_remove(struct platform_device *dev)
  306. {
  307. struct s3c_hwmon *hwmon = platform_get_drvdata(dev);
  308. int i;
  309. s3c_hwmon_remove_raw(&dev->dev);
  310. for (i = 0; i < ARRAY_SIZE(hwmon->attrs); i++)
  311. s3c_hwmon_remove_attr(&dev->dev, &hwmon->attrs[i]);
  312. hwmon_device_unregister(hwmon->hwmon_dev);
  313. s3c_adc_release(hwmon->client);
  314. return 0;
  315. }
  316. static struct platform_driver s3c_hwmon_driver = {
  317. .driver = {
  318. .name = "s3c-hwmon",
  319. .owner = THIS_MODULE,
  320. },
  321. .probe = s3c_hwmon_probe,
  322. .remove = __devexit_p(s3c_hwmon_remove),
  323. };
  324. static int __init s3c_hwmon_init(void)
  325. {
  326. return platform_driver_register(&s3c_hwmon_driver);
  327. }
  328. static void __exit s3c_hwmon_exit(void)
  329. {
  330. platform_driver_unregister(&s3c_hwmon_driver);
  331. }
  332. module_init(s3c_hwmon_init);
  333. module_exit(s3c_hwmon_exit);
  334. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  335. MODULE_DESCRIPTION("S3C ADC HWMon driver");
  336. MODULE_LICENSE("GPL v2");
  337. MODULE_ALIAS("platform:s3c-hwmon");