adcxx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/mod_devicetable.h>
  46. #include <linux/spi/spi.h>
  47. #define DRVNAME "adcxx"
  48. struct adcxx {
  49. struct device *hwmon_dev;
  50. struct mutex lock;
  51. u32 channels;
  52. u32 reference; /* in millivolts */
  53. };
  54. /* sysfs hook function */
  55. static ssize_t adcxx_read(struct device *dev,
  56. struct device_attribute *devattr, char *buf)
  57. {
  58. struct spi_device *spi = to_spi_device(dev);
  59. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  60. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  61. u8 tx_buf[2];
  62. u8 rx_buf[2];
  63. int status;
  64. u32 value;
  65. if (mutex_lock_interruptible(&adc->lock))
  66. return -ERESTARTSYS;
  67. if (adc->channels == 1) {
  68. status = spi_read(spi, rx_buf, sizeof(rx_buf));
  69. } else {
  70. tx_buf[0] = attr->index << 3; /* other bits are don't care */
  71. status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf),
  72. rx_buf, sizeof(rx_buf));
  73. }
  74. if (status < 0) {
  75. dev_warn(dev, "SPI synch. transfer failed with status %d\n",
  76. status);
  77. goto out;
  78. }
  79. value = (rx_buf[0] << 8) + rx_buf[1];
  80. dev_dbg(dev, "raw value = 0x%x\n", value);
  81. value = value * adc->reference >> 12;
  82. status = sprintf(buf, "%d\n", value);
  83. out:
  84. mutex_unlock(&adc->lock);
  85. return status;
  86. }
  87. static ssize_t adcxx_show_min(struct device *dev,
  88. struct device_attribute *devattr, char *buf)
  89. {
  90. /* The minimum reference is 0 for this chip family */
  91. return sprintf(buf, "0\n");
  92. }
  93. static ssize_t adcxx_show_max(struct device *dev,
  94. struct device_attribute *devattr, char *buf)
  95. {
  96. struct spi_device *spi = to_spi_device(dev);
  97. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  98. u32 reference;
  99. if (mutex_lock_interruptible(&adc->lock))
  100. return -ERESTARTSYS;
  101. reference = adc->reference;
  102. mutex_unlock(&adc->lock);
  103. return sprintf(buf, "%d\n", reference);
  104. }
  105. static ssize_t adcxx_set_max(struct device *dev,
  106. struct device_attribute *devattr, const char *buf, size_t count)
  107. {
  108. struct spi_device *spi = to_spi_device(dev);
  109. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  110. unsigned long value;
  111. if (strict_strtoul(buf, 10, &value))
  112. return -EINVAL;
  113. if (mutex_lock_interruptible(&adc->lock))
  114. return -ERESTARTSYS;
  115. adc->reference = value;
  116. mutex_unlock(&adc->lock);
  117. return count;
  118. }
  119. static ssize_t adcxx_show_name(struct device *dev, struct device_attribute
  120. *devattr, char *buf)
  121. {
  122. struct spi_device *spi = to_spi_device(dev);
  123. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  124. return sprintf(buf, "adcxx%ds\n", adc->channels);
  125. }
  126. static struct sensor_device_attribute ad_input[] = {
  127. SENSOR_ATTR(name, S_IRUGO, adcxx_show_name, NULL, 0),
  128. SENSOR_ATTR(in_min, S_IRUGO, adcxx_show_min, NULL, 0),
  129. SENSOR_ATTR(in_max, S_IWUSR | S_IRUGO, adcxx_show_max,
  130. adcxx_set_max, 0),
  131. SENSOR_ATTR(in0_input, S_IRUGO, adcxx_read, NULL, 0),
  132. SENSOR_ATTR(in1_input, S_IRUGO, adcxx_read, NULL, 1),
  133. SENSOR_ATTR(in2_input, S_IRUGO, adcxx_read, NULL, 2),
  134. SENSOR_ATTR(in3_input, S_IRUGO, adcxx_read, NULL, 3),
  135. SENSOR_ATTR(in4_input, S_IRUGO, adcxx_read, NULL, 4),
  136. SENSOR_ATTR(in5_input, S_IRUGO, adcxx_read, NULL, 5),
  137. SENSOR_ATTR(in6_input, S_IRUGO, adcxx_read, NULL, 6),
  138. SENSOR_ATTR(in7_input, S_IRUGO, adcxx_read, NULL, 7),
  139. };
  140. /*----------------------------------------------------------------------*/
  141. static int __devinit adcxx_probe(struct spi_device *spi)
  142. {
  143. int channels = spi_get_device_id(spi)->driver_data;
  144. struct adcxx *adc;
  145. int status;
  146. int i;
  147. adc = kzalloc(sizeof *adc, GFP_KERNEL);
  148. if (!adc)
  149. return -ENOMEM;
  150. /* set a default value for the reference */
  151. adc->reference = 3300;
  152. adc->channels = channels;
  153. mutex_init(&adc->lock);
  154. mutex_lock(&adc->lock);
  155. dev_set_drvdata(&spi->dev, adc);
  156. for (i = 0; i < 3 + adc->channels; i++) {
  157. status = device_create_file(&spi->dev, &ad_input[i].dev_attr);
  158. if (status) {
  159. dev_err(&spi->dev, "device_create_file failed.\n");
  160. goto out_err;
  161. }
  162. }
  163. adc->hwmon_dev = hwmon_device_register(&spi->dev);
  164. if (IS_ERR(adc->hwmon_dev)) {
  165. dev_err(&spi->dev, "hwmon_device_register failed.\n");
  166. status = PTR_ERR(adc->hwmon_dev);
  167. goto out_err;
  168. }
  169. mutex_unlock(&adc->lock);
  170. return 0;
  171. out_err:
  172. for (i--; i >= 0; i--)
  173. device_remove_file(&spi->dev, &ad_input[i].dev_attr);
  174. dev_set_drvdata(&spi->dev, NULL);
  175. mutex_unlock(&adc->lock);
  176. kfree(adc);
  177. return status;
  178. }
  179. static int __devexit adcxx_remove(struct spi_device *spi)
  180. {
  181. struct adcxx *adc = dev_get_drvdata(&spi->dev);
  182. int i;
  183. mutex_lock(&adc->lock);
  184. hwmon_device_unregister(adc->hwmon_dev);
  185. for (i = 0; i < 3 + adc->channels; i++)
  186. device_remove_file(&spi->dev, &ad_input[i].dev_attr);
  187. dev_set_drvdata(&spi->dev, NULL);
  188. mutex_unlock(&adc->lock);
  189. kfree(adc);
  190. return 0;
  191. }
  192. static const struct spi_device_id adcxx_ids[] = {
  193. { "adcxx1s", 1 },
  194. { "adcxx2s", 2 },
  195. { "adcxx4s", 4 },
  196. { "adcxx8s", 8 },
  197. { },
  198. };
  199. MODULE_DEVICE_TABLE(spi, adcxx_ids);
  200. static struct spi_driver adcxx_driver = {
  201. .driver = {
  202. .name = "adcxx",
  203. .owner = THIS_MODULE,
  204. },
  205. .id_table = adcxx_ids,
  206. .probe = adcxx_probe,
  207. .remove = __devexit_p(adcxx_remove),
  208. };
  209. static int __init init_adcxx(void)
  210. {
  211. return spi_register_driver(&adcxx_driver);
  212. }
  213. static void __exit exit_adcxx(void)
  214. {
  215. spi_unregister_driver(&adcxx_driver);
  216. }
  217. module_init(init_adcxx);
  218. module_exit(exit_adcxx);
  219. MODULE_AUTHOR("Marc Pignat");
  220. MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver");
  221. MODULE_LICENSE("GPL");