max197.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Maxim MAX197 A/D Converter driver
  3. *
  4. * Copyright (c) 2012 Savoir-faire Linux Inc.
  5. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * For further information, see the Documentation/hwmon/max197 file.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/init.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/mutex.h>
  20. #include <linux/device.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/hwmon-sysfs.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/platform_data/max197.h>
  26. #define MAX199_LIMIT 4000 /* 4V */
  27. #define MAX197_LIMIT 10000 /* 10V */
  28. #define MAX197_NUM_CH 8 /* 8 Analog Input Channels */
  29. /* Control byte format */
  30. #define MAX197_BIP (1 << 3) /* Bipolarity */
  31. #define MAX197_RNG (1 << 4) /* Full range */
  32. #define MAX197_SCALE 12207 /* Scale coefficient for raw data */
  33. /* List of supported chips */
  34. enum max197_chips { max197, max199 };
  35. /**
  36. * struct max197_data - device instance specific data
  37. * @pdata: Platform data.
  38. * @hwmon_dev: The hwmon device.
  39. * @lock: Read/Write mutex.
  40. * @limit: Max range value (10V for MAX197, 4V for MAX199).
  41. * @scale: Need to scale.
  42. * @ctrl_bytes: Channels control byte.
  43. */
  44. struct max197_data {
  45. struct max197_platform_data *pdata;
  46. struct device *hwmon_dev;
  47. struct mutex lock;
  48. int limit;
  49. bool scale;
  50. u8 ctrl_bytes[MAX197_NUM_CH];
  51. };
  52. static inline void max197_set_unipolarity(struct max197_data *data, int channel)
  53. {
  54. data->ctrl_bytes[channel] &= ~MAX197_BIP;
  55. }
  56. static inline void max197_set_bipolarity(struct max197_data *data, int channel)
  57. {
  58. data->ctrl_bytes[channel] |= MAX197_BIP;
  59. }
  60. static inline void max197_set_half_range(struct max197_data *data, int channel)
  61. {
  62. data->ctrl_bytes[channel] &= ~MAX197_RNG;
  63. }
  64. static inline void max197_set_full_range(struct max197_data *data, int channel)
  65. {
  66. data->ctrl_bytes[channel] |= MAX197_RNG;
  67. }
  68. static inline bool max197_is_bipolar(struct max197_data *data, int channel)
  69. {
  70. return data->ctrl_bytes[channel] & MAX197_BIP;
  71. }
  72. static inline bool max197_is_full_range(struct max197_data *data, int channel)
  73. {
  74. return data->ctrl_bytes[channel] & MAX197_RNG;
  75. }
  76. /* Function called on read access on in{0,1,2,3,4,5,6,7}_{min,max} */
  77. static ssize_t max197_show_range(struct device *dev,
  78. struct device_attribute *devattr, char *buf)
  79. {
  80. struct max197_data *data = dev_get_drvdata(dev);
  81. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  82. int channel = attr->index;
  83. bool is_min = attr->nr;
  84. int range;
  85. if (mutex_lock_interruptible(&data->lock))
  86. return -ERESTARTSYS;
  87. range = max197_is_full_range(data, channel) ?
  88. data->limit : data->limit / 2;
  89. if (is_min) {
  90. if (max197_is_bipolar(data, channel))
  91. range = -range;
  92. else
  93. range = 0;
  94. }
  95. mutex_unlock(&data->lock);
  96. return sprintf(buf, "%d\n", range);
  97. }
  98. /* Function called on write access on in{0,1,2,3,4,5,6,7}_{min,max} */
  99. static ssize_t max197_store_range(struct device *dev,
  100. struct device_attribute *devattr,
  101. const char *buf, size_t count)
  102. {
  103. struct max197_data *data = dev_get_drvdata(dev);
  104. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  105. int channel = attr->index;
  106. bool is_min = attr->nr;
  107. long value;
  108. int half = data->limit / 2;
  109. int full = data->limit;
  110. if (kstrtol(buf, 10, &value))
  111. return -EINVAL;
  112. if (is_min) {
  113. if (value <= -full)
  114. value = -full;
  115. else if (value < 0)
  116. value = -half;
  117. else
  118. value = 0;
  119. } else {
  120. if (value >= full)
  121. value = full;
  122. else
  123. value = half;
  124. }
  125. if (mutex_lock_interruptible(&data->lock))
  126. return -ERESTARTSYS;
  127. if (value == 0) {
  128. /* We can deduce only the polarity */
  129. max197_set_unipolarity(data, channel);
  130. } else if (value == -half) {
  131. max197_set_bipolarity(data, channel);
  132. max197_set_half_range(data, channel);
  133. } else if (value == -full) {
  134. max197_set_bipolarity(data, channel);
  135. max197_set_full_range(data, channel);
  136. } else if (value == half) {
  137. /* We can deduce only the range */
  138. max197_set_half_range(data, channel);
  139. } else if (value == full) {
  140. /* We can deduce only the range */
  141. max197_set_full_range(data, channel);
  142. }
  143. mutex_unlock(&data->lock);
  144. return count;
  145. }
  146. /* Function called on read access on in{0,1,2,3,4,5,6,7}_input */
  147. static ssize_t max197_show_input(struct device *dev,
  148. struct device_attribute *devattr,
  149. char *buf)
  150. {
  151. struct max197_data *data = dev_get_drvdata(dev);
  152. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  153. int channel = attr->index;
  154. s32 value;
  155. int ret;
  156. if (mutex_lock_interruptible(&data->lock))
  157. return -ERESTARTSYS;
  158. ret = data->pdata->convert(data->ctrl_bytes[channel]);
  159. if (ret < 0) {
  160. dev_err(dev, "conversion failed\n");
  161. goto unlock;
  162. }
  163. value = ret;
  164. /*
  165. * Coefficient to apply on raw value.
  166. * See Table 1. Full Scale and Zero Scale in the MAX197 datasheet.
  167. */
  168. if (data->scale) {
  169. value *= MAX197_SCALE;
  170. if (max197_is_full_range(data, channel))
  171. value *= 2;
  172. value /= 10000;
  173. }
  174. ret = sprintf(buf, "%d\n", value);
  175. unlock:
  176. mutex_unlock(&data->lock);
  177. return ret;
  178. }
  179. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  180. char *buf)
  181. {
  182. struct platform_device *pdev = to_platform_device(dev);
  183. return sprintf(buf, "%s\n", pdev->name);
  184. }
  185. #define MAX197_SENSOR_DEVICE_ATTR_CH(chan) \
  186. static SENSOR_DEVICE_ATTR(in##chan##_input, S_IRUGO, \
  187. max197_show_input, NULL, chan); \
  188. static SENSOR_DEVICE_ATTR_2(in##chan##_min, S_IRUGO | S_IWUSR, \
  189. max197_show_range, \
  190. max197_store_range, \
  191. true, chan); \
  192. static SENSOR_DEVICE_ATTR_2(in##chan##_max, S_IRUGO | S_IWUSR, \
  193. max197_show_range, \
  194. max197_store_range, \
  195. false, chan)
  196. #define MAX197_SENSOR_DEV_ATTR_IN(chan) \
  197. &sensor_dev_attr_in##chan##_input.dev_attr.attr, \
  198. &sensor_dev_attr_in##chan##_max.dev_attr.attr, \
  199. &sensor_dev_attr_in##chan##_min.dev_attr.attr
  200. static DEVICE_ATTR_RO(name);
  201. MAX197_SENSOR_DEVICE_ATTR_CH(0);
  202. MAX197_SENSOR_DEVICE_ATTR_CH(1);
  203. MAX197_SENSOR_DEVICE_ATTR_CH(2);
  204. MAX197_SENSOR_DEVICE_ATTR_CH(3);
  205. MAX197_SENSOR_DEVICE_ATTR_CH(4);
  206. MAX197_SENSOR_DEVICE_ATTR_CH(5);
  207. MAX197_SENSOR_DEVICE_ATTR_CH(6);
  208. MAX197_SENSOR_DEVICE_ATTR_CH(7);
  209. static const struct attribute_group max197_sysfs_group = {
  210. .attrs = (struct attribute *[]) {
  211. &dev_attr_name.attr,
  212. MAX197_SENSOR_DEV_ATTR_IN(0),
  213. MAX197_SENSOR_DEV_ATTR_IN(1),
  214. MAX197_SENSOR_DEV_ATTR_IN(2),
  215. MAX197_SENSOR_DEV_ATTR_IN(3),
  216. MAX197_SENSOR_DEV_ATTR_IN(4),
  217. MAX197_SENSOR_DEV_ATTR_IN(5),
  218. MAX197_SENSOR_DEV_ATTR_IN(6),
  219. MAX197_SENSOR_DEV_ATTR_IN(7),
  220. NULL
  221. },
  222. };
  223. static int max197_probe(struct platform_device *pdev)
  224. {
  225. int ch, ret;
  226. struct max197_data *data;
  227. struct max197_platform_data *pdata = dev_get_platdata(&pdev->dev);
  228. enum max197_chips chip = platform_get_device_id(pdev)->driver_data;
  229. if (pdata == NULL) {
  230. dev_err(&pdev->dev, "no platform data supplied\n");
  231. return -EINVAL;
  232. }
  233. if (pdata->convert == NULL) {
  234. dev_err(&pdev->dev, "no convert function supplied\n");
  235. return -EINVAL;
  236. }
  237. data = devm_kzalloc(&pdev->dev, sizeof(struct max197_data), GFP_KERNEL);
  238. if (!data)
  239. return -ENOMEM;
  240. data->pdata = pdata;
  241. mutex_init(&data->lock);
  242. if (chip == max197) {
  243. data->limit = MAX197_LIMIT;
  244. data->scale = true;
  245. } else {
  246. data->limit = MAX199_LIMIT;
  247. data->scale = false;
  248. }
  249. for (ch = 0; ch < MAX197_NUM_CH; ch++)
  250. data->ctrl_bytes[ch] = (u8) ch;
  251. platform_set_drvdata(pdev, data);
  252. ret = sysfs_create_group(&pdev->dev.kobj, &max197_sysfs_group);
  253. if (ret) {
  254. dev_err(&pdev->dev, "sysfs create group failed\n");
  255. return ret;
  256. }
  257. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  258. if (IS_ERR(data->hwmon_dev)) {
  259. ret = PTR_ERR(data->hwmon_dev);
  260. dev_err(&pdev->dev, "hwmon device register failed\n");
  261. goto error;
  262. }
  263. return 0;
  264. error:
  265. sysfs_remove_group(&pdev->dev.kobj, &max197_sysfs_group);
  266. return ret;
  267. }
  268. static int max197_remove(struct platform_device *pdev)
  269. {
  270. struct max197_data *data = platform_get_drvdata(pdev);
  271. hwmon_device_unregister(data->hwmon_dev);
  272. sysfs_remove_group(&pdev->dev.kobj, &max197_sysfs_group);
  273. return 0;
  274. }
  275. static const struct platform_device_id max197_device_ids[] = {
  276. { "max197", max197 },
  277. { "max199", max199 },
  278. { }
  279. };
  280. MODULE_DEVICE_TABLE(platform, max197_device_ids);
  281. static struct platform_driver max197_driver = {
  282. .driver = {
  283. .name = "max197",
  284. },
  285. .probe = max197_probe,
  286. .remove = max197_remove,
  287. .id_table = max197_device_ids,
  288. };
  289. module_platform_driver(max197_driver);
  290. MODULE_LICENSE("GPL");
  291. MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
  292. MODULE_DESCRIPTION("Maxim MAX197 A/D Converter driver");