ti-adc128s052.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
  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] ____cacheline_aligned;
  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 int adc128_probe(struct spi_device *spi)
  107. {
  108. struct iio_dev *indio_dev;
  109. unsigned int config;
  110. struct adc128 *adc;
  111. int ret;
  112. if (dev_fwnode(&spi->dev))
  113. config = (unsigned long) device_get_match_data(&spi->dev);
  114. else
  115. config = spi_get_device_id(spi)->driver_data;
  116. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  117. if (!indio_dev)
  118. return -ENOMEM;
  119. adc = iio_priv(indio_dev);
  120. adc->spi = spi;
  121. spi_set_drvdata(spi, indio_dev);
  122. indio_dev->name = spi_get_device_id(spi)->name;
  123. indio_dev->modes = INDIO_DIRECT_MODE;
  124. indio_dev->info = &adc128_info;
  125. indio_dev->channels = adc128_config[config].channels;
  126. indio_dev->num_channels = adc128_config[config].num_channels;
  127. adc->reg = devm_regulator_get(&spi->dev, "vref");
  128. if (IS_ERR(adc->reg))
  129. return PTR_ERR(adc->reg);
  130. ret = regulator_enable(adc->reg);
  131. if (ret < 0)
  132. return ret;
  133. mutex_init(&adc->lock);
  134. ret = iio_device_register(indio_dev);
  135. if (ret)
  136. goto err_disable_regulator;
  137. return 0;
  138. err_disable_regulator:
  139. regulator_disable(adc->reg);
  140. return ret;
  141. }
  142. static int adc128_remove(struct spi_device *spi)
  143. {
  144. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  145. struct adc128 *adc = iio_priv(indio_dev);
  146. iio_device_unregister(indio_dev);
  147. regulator_disable(adc->reg);
  148. return 0;
  149. }
  150. static const struct of_device_id adc128_of_match[] = {
  151. { .compatible = "ti,adc128s052", },
  152. { .compatible = "ti,adc122s021", },
  153. { .compatible = "ti,adc122s051", },
  154. { .compatible = "ti,adc122s101", },
  155. { .compatible = "ti,adc124s021", },
  156. { .compatible = "ti,adc124s051", },
  157. { .compatible = "ti,adc124s101", },
  158. { /* sentinel */ },
  159. };
  160. MODULE_DEVICE_TABLE(of, adc128_of_match);
  161. static const struct spi_device_id adc128_id[] = {
  162. { "adc128s052", 0 }, /* index into adc128_config */
  163. { "adc122s021", 1 },
  164. { "adc122s051", 1 },
  165. { "adc122s101", 1 },
  166. { "adc124s021", 2 },
  167. { "adc124s051", 2 },
  168. { "adc124s101", 2 },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(spi, adc128_id);
  172. #ifdef CONFIG_ACPI
  173. static const struct acpi_device_id adc128_acpi_match[] = {
  174. { "AANT1280", 2 }, /* ADC124S021 compatible ACPI ID */
  175. { }
  176. };
  177. MODULE_DEVICE_TABLE(acpi, adc128_acpi_match);
  178. #endif
  179. static struct spi_driver adc128_driver = {
  180. .driver = {
  181. .name = "adc128s052",
  182. .of_match_table = adc128_of_match,
  183. .acpi_match_table = ACPI_PTR(adc128_acpi_match),
  184. },
  185. .probe = adc128_probe,
  186. .remove = adc128_remove,
  187. .id_table = adc128_id,
  188. };
  189. module_spi_driver(adc128_driver);
  190. MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
  191. MODULE_DESCRIPTION("Texas Instruments ADC128S052");
  192. MODULE_LICENSE("GPL v2");