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