ti-adc128s052.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Angelo Compagnucci <[email protected]>
  4. *
  5. * Driver for Texas Instruments' ADC128S052, ADC122S021 and ADC124S021 ADC chip.
  6. * Datasheets can be found here:
  7. * https://www.ti.com/lit/ds/symlink/adc128s052.pdf
  8. * https://www.ti.com/lit/ds/symlink/adc122s021.pdf
  9. * https://www.ti.com/lit/ds/symlink/adc124s021.pdf
  10. */
  11. #include <linux/acpi.h>
  12. #include <linux/err.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/property.h>
  18. #include <linux/regulator/consumer.h>
  19. struct adc128_configuration {
  20. const struct iio_chan_spec *channels;
  21. u8 num_channels;
  22. };
  23. struct adc128 {
  24. struct spi_device *spi;
  25. struct regulator *reg;
  26. struct mutex lock;
  27. u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
  28. };
  29. static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
  30. {
  31. int ret;
  32. mutex_lock(&adc->lock);
  33. adc->buffer[0] = channel << 3;
  34. adc->buffer[1] = 0;
  35. ret = spi_write(adc->spi, &adc->buffer, 2);
  36. if (ret < 0) {
  37. mutex_unlock(&adc->lock);
  38. return ret;
  39. }
  40. ret = spi_read(adc->spi, &adc->buffer, 2);
  41. mutex_unlock(&adc->lock);
  42. if (ret < 0)
  43. return ret;
  44. return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
  45. }
  46. static int adc128_read_raw(struct iio_dev *indio_dev,
  47. struct iio_chan_spec const *channel, int *val,
  48. int *val2, long mask)
  49. {
  50. struct adc128 *adc = iio_priv(indio_dev);
  51. int ret;
  52. switch (mask) {
  53. case IIO_CHAN_INFO_RAW:
  54. ret = adc128_adc_conversion(adc, channel->channel);
  55. if (ret < 0)
  56. return ret;
  57. *val = ret;
  58. return IIO_VAL_INT;
  59. case IIO_CHAN_INFO_SCALE:
  60. ret = regulator_get_voltage(adc->reg);
  61. if (ret < 0)
  62. return ret;
  63. *val = ret / 1000;
  64. *val2 = 12;
  65. return IIO_VAL_FRACTIONAL_LOG2;
  66. default:
  67. return -EINVAL;
  68. }
  69. }
  70. #define ADC128_VOLTAGE_CHANNEL(num) \
  71. { \
  72. .type = IIO_VOLTAGE, \
  73. .indexed = 1, \
  74. .channel = (num), \
  75. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  76. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  77. }
  78. static const struct iio_chan_spec adc128s052_channels[] = {
  79. ADC128_VOLTAGE_CHANNEL(0),
  80. ADC128_VOLTAGE_CHANNEL(1),
  81. ADC128_VOLTAGE_CHANNEL(2),
  82. ADC128_VOLTAGE_CHANNEL(3),
  83. ADC128_VOLTAGE_CHANNEL(4),
  84. ADC128_VOLTAGE_CHANNEL(5),
  85. ADC128_VOLTAGE_CHANNEL(6),
  86. ADC128_VOLTAGE_CHANNEL(7),
  87. };
  88. static const struct iio_chan_spec adc122s021_channels[] = {
  89. ADC128_VOLTAGE_CHANNEL(0),
  90. ADC128_VOLTAGE_CHANNEL(1),
  91. };
  92. static const struct iio_chan_spec adc124s021_channels[] = {
  93. ADC128_VOLTAGE_CHANNEL(0),
  94. ADC128_VOLTAGE_CHANNEL(1),
  95. ADC128_VOLTAGE_CHANNEL(2),
  96. ADC128_VOLTAGE_CHANNEL(3),
  97. };
  98. static const struct adc128_configuration adc128_config[] = {
  99. { adc128s052_channels, ARRAY_SIZE(adc128s052_channels) },
  100. { adc122s021_channels, ARRAY_SIZE(adc122s021_channels) },
  101. { adc124s021_channels, ARRAY_SIZE(adc124s021_channels) },
  102. };
  103. static const struct iio_info adc128_info = {
  104. .read_raw = adc128_read_raw,
  105. };
  106. static void adc128_disable_regulator(void *reg)
  107. {
  108. regulator_disable(reg);
  109. }
  110. static int adc128_probe(struct spi_device *spi)
  111. {
  112. struct iio_dev *indio_dev;
  113. unsigned int config;
  114. struct adc128 *adc;
  115. int ret;
  116. if (dev_fwnode(&spi->dev))
  117. config = (unsigned long) device_get_match_data(&spi->dev);
  118. else
  119. config = spi_get_device_id(spi)->driver_data;
  120. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  121. if (!indio_dev)
  122. return -ENOMEM;
  123. adc = iio_priv(indio_dev);
  124. adc->spi = spi;
  125. indio_dev->name = spi_get_device_id(spi)->name;
  126. indio_dev->modes = INDIO_DIRECT_MODE;
  127. indio_dev->info = &adc128_info;
  128. indio_dev->channels = adc128_config[config].channels;
  129. indio_dev->num_channels = adc128_config[config].num_channels;
  130. adc->reg = devm_regulator_get(&spi->dev, "vref");
  131. if (IS_ERR(adc->reg))
  132. return PTR_ERR(adc->reg);
  133. ret = regulator_enable(adc->reg);
  134. if (ret < 0)
  135. return ret;
  136. ret = devm_add_action_or_reset(&spi->dev, adc128_disable_regulator,
  137. adc->reg);
  138. if (ret)
  139. return ret;
  140. mutex_init(&adc->lock);
  141. return devm_iio_device_register(&spi->dev, indio_dev);
  142. }
  143. static const struct of_device_id adc128_of_match[] = {
  144. { .compatible = "ti,adc128s052", .data = (void*)0L, },
  145. { .compatible = "ti,adc122s021", .data = (void*)1L, },
  146. { .compatible = "ti,adc122s051", .data = (void*)1L, },
  147. { .compatible = "ti,adc122s101", .data = (void*)1L, },
  148. { .compatible = "ti,adc124s021", .data = (void*)2L, },
  149. { .compatible = "ti,adc124s051", .data = (void*)2L, },
  150. { .compatible = "ti,adc124s101", .data = (void*)2L, },
  151. { /* sentinel */ },
  152. };
  153. MODULE_DEVICE_TABLE(of, adc128_of_match);
  154. static const struct spi_device_id adc128_id[] = {
  155. { "adc128s052", 0 }, /* index into adc128_config */
  156. { "adc122s021", 1 },
  157. { "adc122s051", 1 },
  158. { "adc122s101", 1 },
  159. { "adc124s021", 2 },
  160. { "adc124s051", 2 },
  161. { "adc124s101", 2 },
  162. { }
  163. };
  164. MODULE_DEVICE_TABLE(spi, adc128_id);
  165. #ifdef CONFIG_ACPI
  166. static const struct acpi_device_id adc128_acpi_match[] = {
  167. { "AANT1280", 2 }, /* ADC124S021 compatible ACPI ID */
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(acpi, adc128_acpi_match);
  171. #endif
  172. static struct spi_driver adc128_driver = {
  173. .driver = {
  174. .name = "adc128s052",
  175. .of_match_table = adc128_of_match,
  176. .acpi_match_table = ACPI_PTR(adc128_acpi_match),
  177. },
  178. .probe = adc128_probe,
  179. .id_table = adc128_id,
  180. };
  181. module_spi_driver(adc128_driver);
  182. MODULE_AUTHOR("Angelo Compagnucci <[email protected]>");
  183. MODULE_DESCRIPTION("Texas Instruments ADC128S052");
  184. MODULE_LICENSE("GPL v2");