adcxx.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * adcxx.c
  4. *
  5. * The adcxx4s is an AD converter family from National Semiconductor (NS).
  6. *
  7. * Copyright (c) 2008 Marc Pignat <[email protected]>
  8. *
  9. * The adcxx4s communicates with a host processor via an SPI/Microwire Bus
  10. * interface. This driver supports the whole family of devices with name
  11. * ADC<bb><c>S<sss>, where
  12. * * bb is the resolution in number of bits (8, 10, 12)
  13. * * c is the number of channels (1, 2, 4, 8)
  14. * * sss is the maximum conversion speed (021 for 200 kSPS, 051 for 500 kSPS
  15. * and 101 for 1 MSPS)
  16. *
  17. * Complete datasheets are available at National's website here:
  18. * http://www.national.com/ds/DC/ADC<bb><c>S<sss>.pdf
  19. *
  20. * Handling of 8, 10 and 12 bits converters are the same, the
  21. * unavailable bits are 0 :)
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/device.h>
  28. #include <linux/err.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/mutex.h>
  33. #include <linux/mod_devicetable.h>
  34. #include <linux/spi/spi.h>
  35. #define DRVNAME "adcxx"
  36. struct adcxx {
  37. struct device *hwmon_dev;
  38. struct mutex lock;
  39. u32 channels;
  40. u32 reference; /* in millivolts */
  41. };
  42. /* sysfs hook function */
  43. static ssize_t adcxx_show(struct device *dev,
  44. struct device_attribute *devattr, char *buf)
  45. {
  46. struct spi_device *spi = to_spi_device(dev);
  47. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  48. struct adcxx *adc = spi_get_drvdata(spi);
  49. u8 tx_buf[2];
  50. u8 rx_buf[2];
  51. int status;
  52. u32 value;
  53. if (mutex_lock_interruptible(&adc->lock))
  54. return -ERESTARTSYS;
  55. if (adc->channels == 1) {
  56. status = spi_read(spi, rx_buf, sizeof(rx_buf));
  57. } else {
  58. tx_buf[0] = attr->index << 3; /* other bits are don't care */
  59. status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf),
  60. rx_buf, sizeof(rx_buf));
  61. }
  62. if (status < 0) {
  63. dev_warn(dev, "SPI synch. transfer failed with status %d\n",
  64. status);
  65. goto out;
  66. }
  67. value = (rx_buf[0] << 8) + rx_buf[1];
  68. dev_dbg(dev, "raw value = 0x%x\n", value);
  69. value = value * adc->reference >> 12;
  70. status = sprintf(buf, "%d\n", value);
  71. out:
  72. mutex_unlock(&adc->lock);
  73. return status;
  74. }
  75. static ssize_t adcxx_min_show(struct device *dev,
  76. struct device_attribute *devattr, char *buf)
  77. {
  78. /* The minimum reference is 0 for this chip family */
  79. return sprintf(buf, "0\n");
  80. }
  81. static ssize_t adcxx_max_show(struct device *dev,
  82. struct device_attribute *devattr, char *buf)
  83. {
  84. struct spi_device *spi = to_spi_device(dev);
  85. struct adcxx *adc = spi_get_drvdata(spi);
  86. u32 reference;
  87. if (mutex_lock_interruptible(&adc->lock))
  88. return -ERESTARTSYS;
  89. reference = adc->reference;
  90. mutex_unlock(&adc->lock);
  91. return sprintf(buf, "%d\n", reference);
  92. }
  93. static ssize_t adcxx_max_store(struct device *dev,
  94. struct device_attribute *devattr,
  95. const char *buf, size_t count)
  96. {
  97. struct spi_device *spi = to_spi_device(dev);
  98. struct adcxx *adc = spi_get_drvdata(spi);
  99. unsigned long value;
  100. if (kstrtoul(buf, 10, &value))
  101. return -EINVAL;
  102. if (mutex_lock_interruptible(&adc->lock))
  103. return -ERESTARTSYS;
  104. adc->reference = value;
  105. mutex_unlock(&adc->lock);
  106. return count;
  107. }
  108. static ssize_t adcxx_name_show(struct device *dev,
  109. struct device_attribute *devattr, char *buf)
  110. {
  111. return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
  112. }
  113. static struct sensor_device_attribute ad_input[] = {
  114. SENSOR_ATTR_RO(name, adcxx_name, 0),
  115. SENSOR_ATTR_RO(in_min, adcxx_min, 0),
  116. SENSOR_ATTR_RW(in_max, adcxx_max, 0),
  117. SENSOR_ATTR_RO(in0_input, adcxx, 0),
  118. SENSOR_ATTR_RO(in1_input, adcxx, 1),
  119. SENSOR_ATTR_RO(in2_input, adcxx, 2),
  120. SENSOR_ATTR_RO(in3_input, adcxx, 3),
  121. SENSOR_ATTR_RO(in4_input, adcxx, 4),
  122. SENSOR_ATTR_RO(in5_input, adcxx, 5),
  123. SENSOR_ATTR_RO(in6_input, adcxx, 6),
  124. SENSOR_ATTR_RO(in7_input, adcxx, 7),
  125. };
  126. /*----------------------------------------------------------------------*/
  127. static int adcxx_probe(struct spi_device *spi)
  128. {
  129. int channels = spi_get_device_id(spi)->driver_data;
  130. struct adcxx *adc;
  131. int status;
  132. int i;
  133. adc = devm_kzalloc(&spi->dev, sizeof(*adc), GFP_KERNEL);
  134. if (!adc)
  135. return -ENOMEM;
  136. /* set a default value for the reference */
  137. adc->reference = 3300;
  138. adc->channels = channels;
  139. mutex_init(&adc->lock);
  140. mutex_lock(&adc->lock);
  141. spi_set_drvdata(spi, adc);
  142. for (i = 0; i < 3 + adc->channels; i++) {
  143. status = device_create_file(&spi->dev, &ad_input[i].dev_attr);
  144. if (status) {
  145. dev_err(&spi->dev, "device_create_file failed.\n");
  146. goto out_err;
  147. }
  148. }
  149. adc->hwmon_dev = hwmon_device_register(&spi->dev);
  150. if (IS_ERR(adc->hwmon_dev)) {
  151. dev_err(&spi->dev, "hwmon_device_register failed.\n");
  152. status = PTR_ERR(adc->hwmon_dev);
  153. goto out_err;
  154. }
  155. mutex_unlock(&adc->lock);
  156. return 0;
  157. out_err:
  158. for (i--; i >= 0; i--)
  159. device_remove_file(&spi->dev, &ad_input[i].dev_attr);
  160. mutex_unlock(&adc->lock);
  161. return status;
  162. }
  163. static void adcxx_remove(struct spi_device *spi)
  164. {
  165. struct adcxx *adc = spi_get_drvdata(spi);
  166. int i;
  167. mutex_lock(&adc->lock);
  168. hwmon_device_unregister(adc->hwmon_dev);
  169. for (i = 0; i < 3 + adc->channels; i++)
  170. device_remove_file(&spi->dev, &ad_input[i].dev_attr);
  171. mutex_unlock(&adc->lock);
  172. }
  173. static const struct spi_device_id adcxx_ids[] = {
  174. { "adcxx1s", 1 },
  175. { "adcxx2s", 2 },
  176. { "adcxx4s", 4 },
  177. { "adcxx8s", 8 },
  178. { },
  179. };
  180. MODULE_DEVICE_TABLE(spi, adcxx_ids);
  181. static struct spi_driver adcxx_driver = {
  182. .driver = {
  183. .name = "adcxx",
  184. },
  185. .id_table = adcxx_ids,
  186. .probe = adcxx_probe,
  187. .remove = adcxx_remove,
  188. };
  189. module_spi_driver(adcxx_driver);
  190. MODULE_AUTHOR("Marc Pignat");
  191. MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver");
  192. MODULE_LICENSE("GPL");