ti-adc128s052.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
  3. *
  4. * Driver for Texas Instruments' ADC128S052 and ADC122S021 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. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/module.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/regulator/consumer.h>
  18. struct adc128_configuration {
  19. const struct iio_chan_spec *channels;
  20. u8 num_channels;
  21. };
  22. struct adc128 {
  23. struct spi_device *spi;
  24. struct regulator *reg;
  25. struct mutex lock;
  26. u8 buffer[2] ____cacheline_aligned;
  27. };
  28. static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
  29. {
  30. int ret;
  31. mutex_lock(&adc->lock);
  32. adc->buffer[0] = channel << 3;
  33. adc->buffer[1] = 0;
  34. ret = spi_write(adc->spi, &adc->buffer, 2);
  35. if (ret < 0) {
  36. mutex_unlock(&adc->lock);
  37. return ret;
  38. }
  39. ret = spi_read(adc->spi, &adc->buffer, 2);
  40. mutex_unlock(&adc->lock);
  41. if (ret < 0)
  42. return ret;
  43. return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
  44. }
  45. static int adc128_read_raw(struct iio_dev *indio_dev,
  46. struct iio_chan_spec const *channel, int *val,
  47. int *val2, long mask)
  48. {
  49. struct adc128 *adc = iio_priv(indio_dev);
  50. int ret;
  51. switch (mask) {
  52. case IIO_CHAN_INFO_RAW:
  53. ret = adc128_adc_conversion(adc, channel->channel);
  54. if (ret < 0)
  55. return ret;
  56. *val = ret;
  57. return IIO_VAL_INT;
  58. case IIO_CHAN_INFO_SCALE:
  59. ret = regulator_get_voltage(adc->reg);
  60. if (ret < 0)
  61. return ret;
  62. *val = ret / 1000;
  63. *val2 = 12;
  64. return IIO_VAL_FRACTIONAL_LOG2;
  65. default:
  66. return -EINVAL;
  67. }
  68. }
  69. #define ADC128_VOLTAGE_CHANNEL(num) \
  70. { \
  71. .type = IIO_VOLTAGE, \
  72. .indexed = 1, \
  73. .channel = (num), \
  74. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  75. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  76. }
  77. static const struct iio_chan_spec adc128s052_channels[] = {
  78. ADC128_VOLTAGE_CHANNEL(0),
  79. ADC128_VOLTAGE_CHANNEL(1),
  80. ADC128_VOLTAGE_CHANNEL(2),
  81. ADC128_VOLTAGE_CHANNEL(3),
  82. ADC128_VOLTAGE_CHANNEL(4),
  83. ADC128_VOLTAGE_CHANNEL(5),
  84. ADC128_VOLTAGE_CHANNEL(6),
  85. ADC128_VOLTAGE_CHANNEL(7),
  86. };
  87. static const struct iio_chan_spec adc122s021_channels[] = {
  88. ADC128_VOLTAGE_CHANNEL(0),
  89. ADC128_VOLTAGE_CHANNEL(1),
  90. };
  91. static const struct adc128_configuration adc128_config[] = {
  92. { adc128s052_channels, ARRAY_SIZE(adc128s052_channels) },
  93. { adc122s021_channels, ARRAY_SIZE(adc122s021_channels) },
  94. };
  95. static const struct iio_info adc128_info = {
  96. .read_raw = adc128_read_raw,
  97. .driver_module = THIS_MODULE,
  98. };
  99. static int adc128_probe(struct spi_device *spi)
  100. {
  101. struct iio_dev *indio_dev;
  102. struct adc128 *adc;
  103. int config = spi_get_device_id(spi)->driver_data;
  104. int ret;
  105. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  106. if (!indio_dev)
  107. return -ENOMEM;
  108. adc = iio_priv(indio_dev);
  109. adc->spi = spi;
  110. spi_set_drvdata(spi, indio_dev);
  111. indio_dev->dev.parent = &spi->dev;
  112. indio_dev->name = spi_get_device_id(spi)->name;
  113. indio_dev->modes = INDIO_DIRECT_MODE;
  114. indio_dev->info = &adc128_info;
  115. indio_dev->channels = adc128_config[config].channels;
  116. indio_dev->num_channels = adc128_config[config].num_channels;
  117. adc->reg = devm_regulator_get(&spi->dev, "vref");
  118. if (IS_ERR(adc->reg))
  119. return PTR_ERR(adc->reg);
  120. ret = regulator_enable(adc->reg);
  121. if (ret < 0)
  122. return ret;
  123. mutex_init(&adc->lock);
  124. ret = iio_device_register(indio_dev);
  125. return ret;
  126. }
  127. static int adc128_remove(struct spi_device *spi)
  128. {
  129. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  130. struct adc128 *adc = iio_priv(indio_dev);
  131. iio_device_unregister(indio_dev);
  132. regulator_disable(adc->reg);
  133. return 0;
  134. }
  135. static const struct spi_device_id adc128_id[] = {
  136. { "adc128s052", 0}, /* index into adc128_config */
  137. { "adc122s021", 1},
  138. { }
  139. };
  140. MODULE_DEVICE_TABLE(spi, adc128_id);
  141. static struct spi_driver adc128_driver = {
  142. .driver = {
  143. .name = "adc128s052",
  144. .owner = THIS_MODULE,
  145. },
  146. .probe = adc128_probe,
  147. .remove = adc128_remove,
  148. .id_table = adc128_id,
  149. };
  150. module_spi_driver(adc128_driver);
  151. MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
  152. MODULE_DESCRIPTION("Texas Instruments ADC128S052");
  153. MODULE_LICENSE("GPL v2");