ti-adc128s052.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
  3. *
  4. * Driver for Texas Instruments' ADC128S052, ADC122S021 and ADC124S021 ADC chip.
  5. * Datasheets can be found here:
  6. * http://www.ti.com/lit/ds/symlink/adc128s052.pdf
  7. * http://www.ti.com/lit/ds/symlink/adc122s021.pdf
  8. * http://www.ti.com/lit/ds/symlink/adc124s021.pdf
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/module.h>
  17. #include <linux/iio/iio.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. struct adc128 *adc;
  110. int config = spi_get_device_id(spi)->driver_data;
  111. int ret;
  112. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  113. if (!indio_dev)
  114. return -ENOMEM;
  115. adc = iio_priv(indio_dev);
  116. adc->spi = spi;
  117. spi_set_drvdata(spi, indio_dev);
  118. indio_dev->dev.parent = &spi->dev;
  119. indio_dev->dev.of_node = spi->dev.of_node;
  120. indio_dev->name = spi_get_device_id(spi)->name;
  121. indio_dev->modes = INDIO_DIRECT_MODE;
  122. indio_dev->info = &adc128_info;
  123. indio_dev->channels = adc128_config[config].channels;
  124. indio_dev->num_channels = adc128_config[config].num_channels;
  125. adc->reg = devm_regulator_get(&spi->dev, "vref");
  126. if (IS_ERR(adc->reg))
  127. return PTR_ERR(adc->reg);
  128. ret = regulator_enable(adc->reg);
  129. if (ret < 0)
  130. return ret;
  131. mutex_init(&adc->lock);
  132. ret = iio_device_register(indio_dev);
  133. return ret;
  134. }
  135. static int adc128_remove(struct spi_device *spi)
  136. {
  137. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  138. struct adc128 *adc = iio_priv(indio_dev);
  139. iio_device_unregister(indio_dev);
  140. regulator_disable(adc->reg);
  141. return 0;
  142. }
  143. static const struct of_device_id adc128_of_match[] = {
  144. { .compatible = "ti,adc128s052", },
  145. { .compatible = "ti,adc122s021", },
  146. { .compatible = "ti,adc124s021", },
  147. { /* sentinel */ },
  148. };
  149. MODULE_DEVICE_TABLE(of, adc128_of_match);
  150. static const struct spi_device_id adc128_id[] = {
  151. { "adc128s052", 0}, /* index into adc128_config */
  152. { "adc122s021", 1},
  153. { "adc124s021", 2},
  154. { }
  155. };
  156. MODULE_DEVICE_TABLE(spi, adc128_id);
  157. static struct spi_driver adc128_driver = {
  158. .driver = {
  159. .name = "adc128s052",
  160. .of_match_table = of_match_ptr(adc128_of_match),
  161. },
  162. .probe = adc128_probe,
  163. .remove = adc128_remove,
  164. .id_table = adc128_id,
  165. };
  166. module_spi_driver(adc128_driver);
  167. MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
  168. MODULE_DESCRIPTION("Texas Instruments ADC128S052");
  169. MODULE_LICENSE("GPL v2");