adcxx.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * adcxx.c
  3. *
  4. * The adcxx4s is an AD converter family from National Semiconductor (NS).
  5. *
  6. * Copyright (c) 2008 Marc Pignat <marc.pignat@hevs.ch>
  7. *
  8. * The adcxx4s communicates with a host processor via an SPI/Microwire Bus
  9. * interface. This driver supports the whole family of devices with name
  10. * ADC<bb><c>S<sss>, where
  11. * * bb is the resolution in number of bits (8, 10, 12)
  12. * * c is the number of channels (1, 2, 4, 8)
  13. * * sss is the maximum conversion speed (021 for 200 kSPS, 051 for 500 kSPS
  14. * and 101 for 1 MSPS)
  15. *
  16. * Complete datasheets are available at National's website here:
  17. * http://www.national.com/ds/DC/ADC<bb><c>S<sss>.pdf
  18. *
  19. * Handling of 8, 10 and 12 bits converters are the same, the
  20. * unavailable bits are 0 :)
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. #include <linux/init.h>
  37. #include <linux/module.h>
  38. #include <linux/kernel.h>
  39. #include <linux/device.h>
  40. #include <linux/err.h>
  41. #include <linux/sysfs.h>
  42. #include <linux/hwmon.h>
  43. #include <linux/hwmon-sysfs.h>
  44. #include <linux/mutex.h>
  45. #include <linux/spi/spi.h>
  46. #define DRVNAME "adcxx"
  47. struct adcxx {
  48. struct device *hwmon_dev;
  49. struct mutex lock;
  50. u32 channels;
  51. u32 reference; /* in millivolts */
  52. };
  53. /* sysfs hook function */
  54. static ssize_t adcxx_read(struct device *dev,
  55. struct device_attribute *devattr, char *buf)
  56. {
  57. struct spi_device *spi = to_spi_device(dev);
  58. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  59. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  60. u8 tx_buf[2] = { attr->index << 3 }; /* other bits are don't care */
  61. u8 rx_buf[2];
  62. int status;
  63. int value;
  64. if (mutex_lock_interruptible(&adc->lock))
  65. return -ERESTARTSYS;
  66. status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf),
  67. rx_buf, sizeof(rx_buf));
  68. if (status < 0) {
  69. dev_warn(dev, "spi_write_then_read failed with status %d\n",
  70. status);
  71. goto out;
  72. }
  73. value = (rx_buf[0] << 8) + rx_buf[1];
  74. dev_dbg(dev, "raw value = 0x%x\n", value);
  75. value = value * adc->reference >> 12;
  76. status = sprintf(buf, "%d\n", value);
  77. out:
  78. mutex_unlock(&adc->lock);
  79. return status;
  80. }
  81. static ssize_t adcxx_show_min(struct device *dev,
  82. struct device_attribute *devattr, char *buf)
  83. {
  84. /* The minimum reference is 0 for this chip family */
  85. return sprintf(buf, "0\n");
  86. }
  87. static ssize_t adcxx_show_max(struct device *dev,
  88. struct device_attribute *devattr, char *buf)
  89. {
  90. struct spi_device *spi = to_spi_device(dev);
  91. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  92. u32 reference;
  93. if (mutex_lock_interruptible(&adc->lock))
  94. return -ERESTARTSYS;
  95. reference = adc->reference;
  96. mutex_unlock(&adc->lock);
  97. return sprintf(buf, "%d\n", reference);
  98. }
  99. static ssize_t adcxx_set_max(struct device *dev,
  100. struct device_attribute *devattr, const char *buf, size_t count)
  101. {
  102. struct spi_device *spi = to_spi_device(dev);
  103. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  104. unsigned long value;
  105. if (strict_strtoul(buf, 10, &value))
  106. return -EINVAL;
  107. if (mutex_lock_interruptible(&adc->lock))
  108. return -ERESTARTSYS;
  109. adc->reference = value;
  110. mutex_unlock(&adc->lock);
  111. return count;
  112. }
  113. static ssize_t adcxx_show_name(struct device *dev, struct device_attribute
  114. *devattr, char *buf)
  115. {
  116. struct spi_device *spi = to_spi_device(dev);
  117. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  118. return sprintf(buf, "adcxx%ds\n", adc->channels);
  119. }
  120. static struct sensor_device_attribute ad_input[] = {
  121. SENSOR_ATTR(name, S_IRUGO, adcxx_show_name, NULL, 0),
  122. SENSOR_ATTR(in_min, S_IRUGO, adcxx_show_min, NULL, 0),
  123. SENSOR_ATTR(in_max, S_IWUSR | S_IRUGO, adcxx_show_max,
  124. adcxx_set_max, 0),
  125. SENSOR_ATTR(in0_input, S_IRUGO, adcxx_read, NULL, 0),
  126. SENSOR_ATTR(in1_input, S_IRUGO, adcxx_read, NULL, 1),
  127. SENSOR_ATTR(in2_input, S_IRUGO, adcxx_read, NULL, 2),
  128. SENSOR_ATTR(in3_input, S_IRUGO, adcxx_read, NULL, 3),
  129. SENSOR_ATTR(in4_input, S_IRUGO, adcxx_read, NULL, 4),
  130. SENSOR_ATTR(in5_input, S_IRUGO, adcxx_read, NULL, 5),
  131. SENSOR_ATTR(in6_input, S_IRUGO, adcxx_read, NULL, 6),
  132. SENSOR_ATTR(in7_input, S_IRUGO, adcxx_read, NULL, 7),
  133. };
  134. /*----------------------------------------------------------------------*/
  135. static int __devinit adcxx_probe(struct spi_device *spi, int channels)
  136. {
  137. struct adcxx *adc;
  138. int status;
  139. int i;
  140. adc = kzalloc(sizeof *adc, GFP_KERNEL);
  141. if (!adc)
  142. return -ENOMEM;
  143. /* set a default value for the reference */
  144. adc->reference = 3300;
  145. adc->channels = channels;
  146. mutex_init(&adc->lock);
  147. mutex_lock(&adc->lock);
  148. dev_set_drvdata(&spi->dev, adc);
  149. for (i = 0; i < 3 + adc->channels; i++) {
  150. status = device_create_file(&spi->dev, &ad_input[i].dev_attr);
  151. if (status) {
  152. dev_err(&spi->dev, "device_create_file failed.\n");
  153. goto out_err;
  154. }
  155. }
  156. adc->hwmon_dev = hwmon_device_register(&spi->dev);
  157. if (IS_ERR(adc->hwmon_dev)) {
  158. dev_err(&spi->dev, "hwmon_device_register failed.\n");
  159. status = PTR_ERR(adc->hwmon_dev);
  160. goto out_err;
  161. }
  162. mutex_unlock(&adc->lock);
  163. return 0;
  164. out_err:
  165. for (i--; i >= 0; i--)
  166. device_remove_file(&spi->dev, &ad_input[i].dev_attr);
  167. dev_set_drvdata(&spi->dev, NULL);
  168. mutex_unlock(&adc->lock);
  169. kfree(adc);
  170. return status;
  171. }
  172. static int __devinit adcxx1s_probe(struct spi_device *spi)
  173. {
  174. return adcxx_probe(spi, 1);
  175. }
  176. static int __devinit adcxx2s_probe(struct spi_device *spi)
  177. {
  178. return adcxx_probe(spi, 2);
  179. }
  180. static int __devinit adcxx4s_probe(struct spi_device *spi)
  181. {
  182. return adcxx_probe(spi, 4);
  183. }
  184. static int __devinit adcxx8s_probe(struct spi_device *spi)
  185. {
  186. return adcxx_probe(spi, 8);
  187. }
  188. static int __devexit adcxx_remove(struct spi_device *spi)
  189. {
  190. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  191. int i;
  192. mutex_lock(&adc->lock);
  193. hwmon_device_unregister(adc->hwmon_dev);
  194. for (i = 0; i < 3 + adc->channels; i++)
  195. device_remove_file(&spi->dev, &ad_input[i].dev_attr);
  196. dev_set_drvdata(&spi->dev, NULL);
  197. mutex_unlock(&adc->lock);
  198. kfree(adc);
  199. return 0;
  200. }
  201. static struct spi_driver adcxx1s_driver = {
  202. .driver = {
  203. .name = "adcxx1s",
  204. .owner = THIS_MODULE,
  205. },
  206. .probe = adcxx1s_probe,
  207. .remove = __devexit_p(adcxx_remove),
  208. };
  209. static struct spi_driver adcxx2s_driver = {
  210. .driver = {
  211. .name = "adcxx2s",
  212. .owner = THIS_MODULE,
  213. },
  214. .probe = adcxx2s_probe,
  215. .remove = __devexit_p(adcxx_remove),
  216. };
  217. static struct spi_driver adcxx4s_driver = {
  218. .driver = {
  219. .name = "adcxx4s",
  220. .owner = THIS_MODULE,
  221. },
  222. .probe = adcxx4s_probe,
  223. .remove = __devexit_p(adcxx_remove),
  224. };
  225. static struct spi_driver adcxx8s_driver = {
  226. .driver = {
  227. .name = "adcxx8s",
  228. .owner = THIS_MODULE,
  229. },
  230. .probe = adcxx8s_probe,
  231. .remove = __devexit_p(adcxx_remove),
  232. };
  233. static int __init init_adcxx(void)
  234. {
  235. int status;
  236. status = spi_register_driver(&adcxx1s_driver);
  237. if (status)
  238. goto reg_1_failed;
  239. status = spi_register_driver(&adcxx2s_driver);
  240. if (status)
  241. goto reg_2_failed;
  242. status = spi_register_driver(&adcxx4s_driver);
  243. if (status)
  244. goto reg_4_failed;
  245. status = spi_register_driver(&adcxx8s_driver);
  246. if (status)
  247. goto reg_8_failed;
  248. return status;
  249. reg_8_failed:
  250. spi_unregister_driver(&adcxx4s_driver);
  251. reg_4_failed:
  252. spi_unregister_driver(&adcxx2s_driver);
  253. reg_2_failed:
  254. spi_unregister_driver(&adcxx1s_driver);
  255. reg_1_failed:
  256. return status;
  257. }
  258. static void __exit exit_adcxx(void)
  259. {
  260. spi_unregister_driver(&adcxx1s_driver);
  261. spi_unregister_driver(&adcxx2s_driver);
  262. spi_unregister_driver(&adcxx4s_driver);
  263. spi_unregister_driver(&adcxx8s_driver);
  264. }
  265. module_init(init_adcxx);
  266. module_exit(exit_adcxx);
  267. MODULE_AUTHOR("Marc Pignat");
  268. MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver");
  269. MODULE_LICENSE("GPL");
  270. MODULE_ALIAS("adcxx1s");
  271. MODULE_ALIAS("adcxx2s");
  272. MODULE_ALIAS("adcxx4s");
  273. MODULE_ALIAS("adcxx8s");