s3c-hwmon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #define DEF_ADC_ATTR(x) \
  96. static SENSOR_DEVICE_ATTR(adc##x##_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, x)
  97. DEF_ADC_ATTR(0);
  98. DEF_ADC_ATTR(1);
  99. DEF_ADC_ATTR(2);
  100. DEF_ADC_ATTR(3);
  101. DEF_ADC_ATTR(4);
  102. DEF_ADC_ATTR(5);
  103. DEF_ADC_ATTR(6);
  104. DEF_ADC_ATTR(7);
  105. static struct attribute *s3c_hwmon_attrs[9] = {
  106. &sensor_dev_attr_adc0_raw.dev_attr.attr,
  107. &sensor_dev_attr_adc1_raw.dev_attr.attr,
  108. &sensor_dev_attr_adc2_raw.dev_attr.attr,
  109. &sensor_dev_attr_adc3_raw.dev_attr.attr,
  110. &sensor_dev_attr_adc4_raw.dev_attr.attr,
  111. &sensor_dev_attr_adc5_raw.dev_attr.attr,
  112. &sensor_dev_attr_adc6_raw.dev_attr.attr,
  113. &sensor_dev_attr_adc7_raw.dev_attr.attr,
  114. NULL,
  115. };
  116. static struct attribute_group s3c_hwmon_attrgroup = {
  117. .attrs = s3c_hwmon_attrs,
  118. };
  119. static inline int s3c_hwmon_add_raw(struct device *dev)
  120. {
  121. return sysfs_create_group(&dev->kobj, &s3c_hwmon_attrgroup);
  122. }
  123. static inline void s3c_hwmon_remove_raw(struct device *dev)
  124. {
  125. sysfs_remove_group(&dev->kobj, &s3c_hwmon_attrgroup);
  126. }
  127. #else
  128. static inline int s3c_hwmon_add_raw(struct device *dev) { return 0; }
  129. static inline void s3c_hwmon_remove_raw(struct device *dev) { }
  130. #endif /* CONFIG_SENSORS_S3C_RAW */
  131. /**
  132. * s3c_hwmon_ch_show - show value of a given channel
  133. * @dev: The device that the attribute belongs to.
  134. * @attr: The attribute being read.
  135. * @buf: The result buffer.
  136. *
  137. * Read a value from the ADC and scale it before returning it to the
  138. * caller. The scale factor is gained from the channel configuration
  139. * passed via the platform data when the device was registered.
  140. */
  141. static ssize_t s3c_hwmon_ch_show(struct device *dev,
  142. struct device_attribute *attr,
  143. char *buf)
  144. {
  145. struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
  146. struct s3c_hwmon *hwmon = platform_get_drvdata(to_platform_device(dev));
  147. struct s3c_hwmon_pdata *pdata = dev->platform_data;
  148. struct s3c_hwmon_chcfg *cfg;
  149. int ret;
  150. cfg = pdata->in[sen_attr->index];
  151. ret = s3c_hwmon_read_ch(dev, hwmon, sen_attr->index);
  152. if (ret < 0)
  153. return ret;
  154. ret *= cfg->mult;
  155. ret = DIV_ROUND_CLOSEST(ret, cfg->div);
  156. return snprintf(buf, PAGE_SIZE, "%d\n", ret);
  157. }
  158. /**
  159. * s3c_hwmon_label_show - show label name of the given channel.
  160. * @dev: The device that the attribute belongs to.
  161. * @attr: The attribute being read.
  162. * @buf: The result buffer.
  163. *
  164. * Return the label name of a given channel
  165. */
  166. static ssize_t s3c_hwmon_label_show(struct device *dev,
  167. struct device_attribute *attr,
  168. char *buf)
  169. {
  170. struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
  171. struct s3c_hwmon_pdata *pdata = dev->platform_data;
  172. struct s3c_hwmon_chcfg *cfg;
  173. cfg = pdata->in[sen_attr->index];
  174. return snprintf(buf, PAGE_SIZE, "%s\n", cfg->name);
  175. }
  176. /**
  177. * s3c_hwmon_create_attr - create hwmon attribute for given channel.
  178. * @dev: The device to create the attribute on.
  179. * @cfg: The channel configuration passed from the platform data.
  180. * @channel: The ADC channel number to process.
  181. *
  182. * Create the scaled attribute for use with hwmon from the specified
  183. * platform data in @pdata. The sysfs entry is handled by the routine
  184. * s3c_hwmon_ch_show().
  185. *
  186. * The attribute name is taken from the configuration data if present
  187. * otherwise the name is taken by concatenating in_ with the channel
  188. * number.
  189. */
  190. static int s3c_hwmon_create_attr(struct device *dev,
  191. struct s3c_hwmon_chcfg *cfg,
  192. struct s3c_hwmon_attr *attrs,
  193. int channel)
  194. {
  195. struct sensor_device_attribute *attr;
  196. int ret;
  197. snprintf(attrs->in_name, sizeof(attrs->in_name), "in%d_input", channel);
  198. attr = &attrs->in;
  199. attr->index = channel;
  200. sysfs_attr_init(&attr->dev_attr.attr);
  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. sysfs_attr_init(&attr->dev_attr.attr);
  216. attr->dev_attr.attr.name = attrs->label_name;
  217. attr->dev_attr.attr.mode = S_IRUGO;
  218. attr->dev_attr.show = s3c_hwmon_label_show;
  219. ret = device_create_file(dev, &attr->dev_attr);
  220. if (ret < 0) {
  221. device_remove_file(dev, &attrs->in.dev_attr);
  222. dev_err(dev, "failed to create label attribute\n");
  223. }
  224. }
  225. return ret;
  226. }
  227. static void s3c_hwmon_remove_attr(struct device *dev,
  228. struct s3c_hwmon_attr *attrs)
  229. {
  230. device_remove_file(dev, &attrs->in.dev_attr);
  231. device_remove_file(dev, &attrs->label.dev_attr);
  232. }
  233. /**
  234. * s3c_hwmon_probe - device probe entry.
  235. * @dev: The device being probed.
  236. */
  237. static int s3c_hwmon_probe(struct platform_device *dev)
  238. {
  239. struct s3c_hwmon_pdata *pdata = dev->dev.platform_data;
  240. struct s3c_hwmon *hwmon;
  241. int ret = 0;
  242. int i;
  243. if (!pdata) {
  244. dev_err(&dev->dev, "no platform data supplied\n");
  245. return -EINVAL;
  246. }
  247. hwmon = devm_kzalloc(&dev->dev, sizeof(struct s3c_hwmon), GFP_KERNEL);
  248. if (hwmon == NULL) {
  249. dev_err(&dev->dev, "no memory\n");
  250. return -ENOMEM;
  251. }
  252. platform_set_drvdata(dev, hwmon);
  253. mutex_init(&hwmon->lock);
  254. /* Register with the core ADC driver. */
  255. hwmon->client = s3c_adc_register(dev, NULL, NULL, 0);
  256. if (IS_ERR(hwmon->client)) {
  257. dev_err(&dev->dev, "cannot register adc\n");
  258. return PTR_ERR(hwmon->client);
  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. return ret;
  302. }
  303. static int s3c_hwmon_remove(struct platform_device *dev)
  304. {
  305. struct s3c_hwmon *hwmon = platform_get_drvdata(dev);
  306. int i;
  307. s3c_hwmon_remove_raw(&dev->dev);
  308. for (i = 0; i < ARRAY_SIZE(hwmon->attrs); i++)
  309. s3c_hwmon_remove_attr(&dev->dev, &hwmon->attrs[i]);
  310. hwmon_device_unregister(hwmon->hwmon_dev);
  311. s3c_adc_release(hwmon->client);
  312. return 0;
  313. }
  314. static struct platform_driver s3c_hwmon_driver = {
  315. .driver = {
  316. .name = "s3c-hwmon",
  317. .owner = THIS_MODULE,
  318. },
  319. .probe = s3c_hwmon_probe,
  320. .remove = s3c_hwmon_remove,
  321. };
  322. module_platform_driver(s3c_hwmon_driver);
  323. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  324. MODULE_DESCRIPTION("S3C ADC HWMon driver");
  325. MODULE_LICENSE("GPL v2");
  326. MODULE_ALIAS("platform:s3c-hwmon");