max197.c 8.5 KB

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