max1241.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 ____cacheline_aligned;
  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. spi_set_drvdata(spi, indio_dev);
  125. adc->vdd = devm_regulator_get(dev, "vdd");
  126. if (IS_ERR(adc->vdd)) {
  127. dev_err(dev, "failed to get vdd regulator\n");
  128. return PTR_ERR(adc->vdd);
  129. }
  130. ret = regulator_enable(adc->vdd);
  131. if (ret)
  132. return ret;
  133. ret = devm_add_action_or_reset(dev, max1241_disable_vdd_action, adc);
  134. if (ret) {
  135. dev_err(dev, "could not set up vdd regulator cleanup action\n");
  136. return ret;
  137. }
  138. adc->vref = devm_regulator_get(dev, "vref");
  139. if (IS_ERR(adc->vref)) {
  140. dev_err(dev, "failed to get vref regulator\n");
  141. return PTR_ERR(adc->vref);
  142. }
  143. ret = regulator_enable(adc->vref);
  144. if (ret)
  145. return ret;
  146. ret = devm_add_action_or_reset(dev, max1241_disable_vref_action, adc);
  147. if (ret) {
  148. dev_err(dev, "could not set up vref regulator cleanup action\n");
  149. return ret;
  150. }
  151. adc->shutdown = devm_gpiod_get_optional(dev, "shutdown",
  152. GPIOD_OUT_HIGH);
  153. if (IS_ERR(adc->shutdown))
  154. return PTR_ERR(adc->shutdown);
  155. if (adc->shutdown)
  156. dev_dbg(dev, "shutdown pin passed, low-power mode enabled");
  157. else
  158. dev_dbg(dev, "no shutdown pin passed, low-power mode disabled");
  159. indio_dev->name = spi_get_device_id(spi)->name;
  160. indio_dev->info = &max1241_info;
  161. indio_dev->modes = INDIO_DIRECT_MODE;
  162. indio_dev->channels = max1241_channels;
  163. indio_dev->num_channels = ARRAY_SIZE(max1241_channels);
  164. return devm_iio_device_register(dev, indio_dev);
  165. }
  166. static const struct spi_device_id max1241_id[] = {
  167. { "max1241", max1241 },
  168. {}
  169. };
  170. static const struct of_device_id max1241_dt_ids[] = {
  171. { .compatible = "maxim,max1241" },
  172. {}
  173. };
  174. MODULE_DEVICE_TABLE(of, max1241_dt_ids);
  175. static struct spi_driver max1241_spi_driver = {
  176. .driver = {
  177. .name = "max1241",
  178. .of_match_table = max1241_dt_ids,
  179. },
  180. .probe = max1241_probe,
  181. .id_table = max1241_id,
  182. };
  183. module_spi_driver(max1241_spi_driver);
  184. MODULE_AUTHOR("Alexandru Lazar <alazar@startmail.com>");
  185. MODULE_DESCRIPTION("MAX1241 ADC driver");
  186. MODULE_LICENSE("GPL v2");