adcxx.c 6.4 KB

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