max1241.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MAX1241 low-power, 12-bit serial ADC
  4. *
  5. * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1240-MAX1241.pdf
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/iio/iio.h>
  10. #include <linux/module.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/spi/spi.h>
  13. #define MAX1241_VAL_MASK GENMASK(11, 0)
  14. #define MAX1241_SHUTDOWN_DELAY_USEC 4
  15. enum max1241_id {
  16. max1241,
  17. };
  18. struct max1241 {
  19. struct spi_device *spi;
  20. struct mutex lock;
  21. struct regulator *vdd;
  22. struct regulator *vref;
  23. struct gpio_desc *shutdown;
  24. __be16 data __aligned(IIO_DMA_MINALIGN);
  25. };
  26. static const struct iio_chan_spec max1241_channels[] = {
  27. {
  28. .type = IIO_VOLTAGE,
  29. .indexed = 1,
  30. .channel = 0,
  31. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  32. BIT(IIO_CHAN_INFO_SCALE),
  33. },
  34. };
  35. static int max1241_read(struct max1241 *adc)
  36. {
  37. struct spi_transfer xfers[] = {
  38. /*
  39. * Begin conversion by bringing /CS low for at least
  40. * tconv us.
  41. */
  42. {
  43. .len = 0,
  44. .delay.value = 8,
  45. .delay.unit = SPI_DELAY_UNIT_USECS,
  46. },
  47. /*
  48. * Then read two bytes of data in our RX buffer.
  49. */
  50. {
  51. .rx_buf = &adc->data,
  52. .len = 2,
  53. },
  54. };
  55. return spi_sync_transfer(adc->spi, xfers, ARRAY_SIZE(xfers));
  56. }
  57. static int max1241_read_raw(struct iio_dev *indio_dev,
  58. struct iio_chan_spec const *chan,
  59. int *val, int *val2, long mask)
  60. {
  61. int ret, vref_uV;
  62. struct max1241 *adc = iio_priv(indio_dev);
  63. switch (mask) {
  64. case IIO_CHAN_INFO_RAW:
  65. mutex_lock(&adc->lock);
  66. if (adc->shutdown) {
  67. gpiod_set_value(adc->shutdown, 0);
  68. udelay(MAX1241_SHUTDOWN_DELAY_USEC);
  69. ret = max1241_read(adc);
  70. gpiod_set_value(adc->shutdown, 1);
  71. } else
  72. ret = max1241_read(adc);
  73. if (ret) {
  74. mutex_unlock(&adc->lock);
  75. return ret;
  76. }
  77. *val = (be16_to_cpu(adc->data) >> 3) & MAX1241_VAL_MASK;
  78. mutex_unlock(&adc->lock);
  79. return IIO_VAL_INT;
  80. case IIO_CHAN_INFO_SCALE:
  81. vref_uV = regulator_get_voltage(adc->vref);
  82. if (vref_uV < 0)
  83. return vref_uV;
  84. *val = vref_uV / 1000;
  85. *val2 = 12;
  86. return IIO_VAL_FRACTIONAL_LOG2;
  87. default:
  88. return -EINVAL;
  89. }
  90. }
  91. static const struct iio_info max1241_info = {
  92. .read_raw = max1241_read_raw,
  93. };
  94. static void max1241_disable_vdd_action(void *data)
  95. {
  96. struct max1241 *adc = data;
  97. struct device *dev = &adc->spi->dev;
  98. int err;
  99. err = regulator_disable(adc->vdd);
  100. if (err)
  101. dev_err(dev, "could not disable vdd regulator.\n");
  102. }
  103. static void max1241_disable_vref_action(void *data)
  104. {
  105. struct max1241 *adc = data;
  106. struct device *dev = &adc->spi->dev;
  107. int err;
  108. err = regulator_disable(adc->vref);
  109. if (err)
  110. dev_err(dev, "could not disable vref regulator.\n");
  111. }
  112. static int max1241_probe(struct spi_device *spi)
  113. {
  114. struct device *dev = &spi->dev;
  115. struct iio_dev *indio_dev;
  116. struct max1241 *adc;
  117. int ret;
  118. indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
  119. if (!indio_dev)
  120. return -ENOMEM;
  121. adc = iio_priv(indio_dev);
  122. adc->spi = spi;
  123. mutex_init(&adc->lock);
  124. adc->vdd = devm_regulator_get(dev, "vdd");
  125. if (IS_ERR(adc->vdd))
  126. return dev_err_probe(dev, PTR_ERR(adc->vdd),
  127. "failed to get vdd regulator\n");
  128. ret = regulator_enable(adc->vdd);
  129. if (ret)
  130. return ret;
  131. ret = devm_add_action_or_reset(dev, max1241_disable_vdd_action, adc);
  132. if (ret) {
  133. dev_err(dev, "could not set up vdd regulator cleanup action\n");
  134. return ret;
  135. }
  136. adc->vref = devm_regulator_get(dev, "vref");
  137. if (IS_ERR(adc->vref))
  138. return dev_err_probe(dev, PTR_ERR(adc->vref),
  139. "failed to get vref regulator\n");
  140. ret = regulator_enable(adc->vref);
  141. if (ret)
  142. return ret;
  143. ret = devm_add_action_or_reset(dev, max1241_disable_vref_action, adc);
  144. if (ret) {
  145. dev_err(dev, "could not set up vref regulator cleanup action\n");
  146. return ret;
  147. }
  148. adc->shutdown = devm_gpiod_get_optional(dev, "shutdown",
  149. GPIOD_OUT_HIGH);
  150. if (IS_ERR(adc->shutdown))
  151. return dev_err_probe(dev, PTR_ERR(adc->shutdown),
  152. "cannot get shutdown gpio\n");
  153. if (adc->shutdown)
  154. dev_dbg(dev, "shutdown pin passed, low-power mode enabled");
  155. else
  156. dev_dbg(dev, "no shutdown pin passed, low-power mode disabled");
  157. indio_dev->name = spi_get_device_id(spi)->name;
  158. indio_dev->info = &max1241_info;
  159. indio_dev->modes = INDIO_DIRECT_MODE;
  160. indio_dev->channels = max1241_channels;
  161. indio_dev->num_channels = ARRAY_SIZE(max1241_channels);
  162. return devm_iio_device_register(dev, indio_dev);
  163. }
  164. static const struct spi_device_id max1241_id[] = {
  165. { "max1241", max1241 },
  166. {}
  167. };
  168. static const struct of_device_id max1241_dt_ids[] = {
  169. { .compatible = "maxim,max1241" },
  170. {}
  171. };
  172. MODULE_DEVICE_TABLE(of, max1241_dt_ids);
  173. static struct spi_driver max1241_spi_driver = {
  174. .driver = {
  175. .name = "max1241",
  176. .of_match_table = max1241_dt_ids,
  177. },
  178. .probe = max1241_probe,
  179. .id_table = max1241_id,
  180. };
  181. module_spi_driver(max1241_spi_driver);
  182. MODULE_AUTHOR("Alexandru Lazar <[email protected]>");
  183. MODULE_DESCRIPTION("MAX1241 ADC driver");
  184. MODULE_LICENSE("GPL v2");