ti-adc128s052.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. .driver_module = THIS_MODULE,
  106. };
  107. static int adc128_probe(struct spi_device *spi)
  108. {
  109. struct iio_dev *indio_dev;
  110. struct adc128 *adc;
  111. int config = spi_get_device_id(spi)->driver_data;
  112. int ret;
  113. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  114. if (!indio_dev)
  115. return -ENOMEM;
  116. adc = iio_priv(indio_dev);
  117. adc->spi = spi;
  118. spi_set_drvdata(spi, indio_dev);
  119. indio_dev->dev.parent = &spi->dev;
  120. indio_dev->dev.of_node = spi->dev.of_node;
  121. indio_dev->name = spi_get_device_id(spi)->name;
  122. indio_dev->modes = INDIO_DIRECT_MODE;
  123. indio_dev->info = &adc128_info;
  124. indio_dev->channels = adc128_config[config].channels;
  125. indio_dev->num_channels = adc128_config[config].num_channels;
  126. adc->reg = devm_regulator_get(&spi->dev, "vref");
  127. if (IS_ERR(adc->reg))
  128. return PTR_ERR(adc->reg);
  129. ret = regulator_enable(adc->reg);
  130. if (ret < 0)
  131. return ret;
  132. mutex_init(&adc->lock);
  133. ret = iio_device_register(indio_dev);
  134. return ret;
  135. }
  136. static int adc128_remove(struct spi_device *spi)
  137. {
  138. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  139. struct adc128 *adc = iio_priv(indio_dev);
  140. iio_device_unregister(indio_dev);
  141. regulator_disable(adc->reg);
  142. return 0;
  143. }
  144. static const struct of_device_id adc128_of_match[] = {
  145. { .compatible = "ti,adc128s052", },
  146. { .compatible = "ti,adc122s021", },
  147. { .compatible = "ti,adc124s021", },
  148. { /* sentinel */ },
  149. };
  150. MODULE_DEVICE_TABLE(of, adc128_of_match);
  151. static const struct spi_device_id adc128_id[] = {
  152. { "adc128s052", 0}, /* index into adc128_config */
  153. { "adc122s021", 1},
  154. { "adc124s021", 2},
  155. { }
  156. };
  157. MODULE_DEVICE_TABLE(spi, adc128_id);
  158. static struct spi_driver adc128_driver = {
  159. .driver = {
  160. .name = "adc128s052",
  161. .of_match_table = of_match_ptr(adc128_of_match),
  162. },
  163. .probe = adc128_probe,
  164. .remove = adc128_remove,
  165. .id_table = adc128_id,
  166. };
  167. module_spi_driver(adc128_driver);
  168. MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
  169. MODULE_DESCRIPTION("Texas Instruments ADC128S052");
  170. MODULE_LICENSE("GPL v2");