adcxx.c 6.7 KB

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