max1118.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver
  4. *
  5. * Copyright (c) 2017 Akinobu Mita <[email protected]>
  6. *
  7. * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf
  8. *
  9. * SPI interface connections
  10. *
  11. * SPI MAXIM
  12. * Master Direction MAX1117/8/9
  13. * ------ --------- -----------
  14. * nCS --> CNVST
  15. * SCK --> SCLK
  16. * MISO <-- DOUT
  17. * ------ --------- -----------
  18. */
  19. #include <linux/module.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/triggered_buffer.h>
  25. #include <linux/iio/trigger_consumer.h>
  26. #include <linux/regulator/consumer.h>
  27. enum max1118_id {
  28. max1117,
  29. max1118,
  30. max1119,
  31. };
  32. struct max1118 {
  33. struct spi_device *spi;
  34. struct mutex lock;
  35. struct regulator *reg;
  36. /* Ensure natural alignment of buffer elements */
  37. struct {
  38. u8 channels[2];
  39. s64 ts __aligned(8);
  40. } scan;
  41. u8 data __aligned(IIO_DMA_MINALIGN);
  42. };
  43. #define MAX1118_CHANNEL(ch) \
  44. { \
  45. .type = IIO_VOLTAGE, \
  46. .indexed = 1, \
  47. .channel = (ch), \
  48. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  49. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  50. .scan_index = ch, \
  51. .scan_type = { \
  52. .sign = 'u', \
  53. .realbits = 8, \
  54. .storagebits = 8, \
  55. }, \
  56. }
  57. static const struct iio_chan_spec max1118_channels[] = {
  58. MAX1118_CHANNEL(0),
  59. MAX1118_CHANNEL(1),
  60. IIO_CHAN_SOFT_TIMESTAMP(2),
  61. };
  62. static int max1118_read(struct iio_dev *indio_dev, int channel)
  63. {
  64. struct max1118 *adc = iio_priv(indio_dev);
  65. struct spi_transfer xfers[] = {
  66. /*
  67. * To select CH1 for conversion, CNVST pin must be brought high
  68. * and low for a second time.
  69. */
  70. {
  71. .len = 0,
  72. .delay = { /* > CNVST Low Time 100 ns */
  73. .value = 1,
  74. .unit = SPI_DELAY_UNIT_USECS
  75. },
  76. .cs_change = 1,
  77. },
  78. /*
  79. * The acquisition interval begins with the falling edge of
  80. * CNVST. The total acquisition and conversion process takes
  81. * <7.5us.
  82. */
  83. {
  84. .len = 0,
  85. .delay = {
  86. .value = 8,
  87. .unit = SPI_DELAY_UNIT_USECS
  88. },
  89. },
  90. {
  91. .rx_buf = &adc->data,
  92. .len = 1,
  93. },
  94. };
  95. int ret;
  96. if (channel == 0)
  97. ret = spi_sync_transfer(adc->spi, xfers + 1, 2);
  98. else
  99. ret = spi_sync_transfer(adc->spi, xfers, 3);
  100. if (ret)
  101. return ret;
  102. return adc->data;
  103. }
  104. static int max1118_get_vref_mV(struct iio_dev *indio_dev)
  105. {
  106. struct max1118 *adc = iio_priv(indio_dev);
  107. const struct spi_device_id *id = spi_get_device_id(adc->spi);
  108. int vref_uV;
  109. switch (id->driver_data) {
  110. case max1117:
  111. return 2048;
  112. case max1119:
  113. return 4096;
  114. case max1118:
  115. vref_uV = regulator_get_voltage(adc->reg);
  116. if (vref_uV < 0)
  117. return vref_uV;
  118. return vref_uV / 1000;
  119. }
  120. return -ENODEV;
  121. }
  122. static int max1118_read_raw(struct iio_dev *indio_dev,
  123. struct iio_chan_spec const *chan,
  124. int *val, int *val2, long mask)
  125. {
  126. struct max1118 *adc = iio_priv(indio_dev);
  127. switch (mask) {
  128. case IIO_CHAN_INFO_RAW:
  129. mutex_lock(&adc->lock);
  130. *val = max1118_read(indio_dev, chan->channel);
  131. mutex_unlock(&adc->lock);
  132. if (*val < 0)
  133. return *val;
  134. return IIO_VAL_INT;
  135. case IIO_CHAN_INFO_SCALE:
  136. *val = max1118_get_vref_mV(indio_dev);
  137. if (*val < 0)
  138. return *val;
  139. *val2 = 8;
  140. return IIO_VAL_FRACTIONAL_LOG2;
  141. }
  142. return -EINVAL;
  143. }
  144. static const struct iio_info max1118_info = {
  145. .read_raw = max1118_read_raw,
  146. };
  147. static irqreturn_t max1118_trigger_handler(int irq, void *p)
  148. {
  149. struct iio_poll_func *pf = p;
  150. struct iio_dev *indio_dev = pf->indio_dev;
  151. struct max1118 *adc = iio_priv(indio_dev);
  152. int scan_index;
  153. int i = 0;
  154. mutex_lock(&adc->lock);
  155. for_each_set_bit(scan_index, indio_dev->active_scan_mask,
  156. indio_dev->masklength) {
  157. const struct iio_chan_spec *scan_chan =
  158. &indio_dev->channels[scan_index];
  159. int ret = max1118_read(indio_dev, scan_chan->channel);
  160. if (ret < 0) {
  161. dev_warn(&adc->spi->dev,
  162. "failed to get conversion data\n");
  163. goto out;
  164. }
  165. adc->scan.channels[i] = ret;
  166. i++;
  167. }
  168. iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
  169. iio_get_time_ns(indio_dev));
  170. out:
  171. mutex_unlock(&adc->lock);
  172. iio_trigger_notify_done(indio_dev->trig);
  173. return IRQ_HANDLED;
  174. }
  175. static void max1118_reg_disable(void *reg)
  176. {
  177. regulator_disable(reg);
  178. }
  179. static int max1118_probe(struct spi_device *spi)
  180. {
  181. struct iio_dev *indio_dev;
  182. struct max1118 *adc;
  183. const struct spi_device_id *id = spi_get_device_id(spi);
  184. int ret;
  185. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  186. if (!indio_dev)
  187. return -ENOMEM;
  188. adc = iio_priv(indio_dev);
  189. adc->spi = spi;
  190. mutex_init(&adc->lock);
  191. if (id->driver_data == max1118) {
  192. adc->reg = devm_regulator_get(&spi->dev, "vref");
  193. if (IS_ERR(adc->reg))
  194. return dev_err_probe(&spi->dev, PTR_ERR(adc->reg),
  195. "failed to get vref regulator\n");
  196. ret = regulator_enable(adc->reg);
  197. if (ret)
  198. return ret;
  199. ret = devm_add_action_or_reset(&spi->dev, max1118_reg_disable,
  200. adc->reg);
  201. if (ret)
  202. return ret;
  203. }
  204. indio_dev->name = spi_get_device_id(spi)->name;
  205. indio_dev->info = &max1118_info;
  206. indio_dev->modes = INDIO_DIRECT_MODE;
  207. indio_dev->channels = max1118_channels;
  208. indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
  209. /*
  210. * To reinitiate a conversion on CH0, it is necessary to allow for a
  211. * conversion to be complete and all of the data to be read out. Once
  212. * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
  213. * into AutoShutdown mode until the next conversion is initiated.
  214. */
  215. max1118_read(indio_dev, 0);
  216. ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
  217. max1118_trigger_handler, NULL);
  218. if (ret)
  219. return ret;
  220. return devm_iio_device_register(&spi->dev, indio_dev);
  221. }
  222. static const struct spi_device_id max1118_id[] = {
  223. { "max1117", max1117 },
  224. { "max1118", max1118 },
  225. { "max1119", max1119 },
  226. {}
  227. };
  228. MODULE_DEVICE_TABLE(spi, max1118_id);
  229. static const struct of_device_id max1118_dt_ids[] = {
  230. { .compatible = "maxim,max1117" },
  231. { .compatible = "maxim,max1118" },
  232. { .compatible = "maxim,max1119" },
  233. {},
  234. };
  235. MODULE_DEVICE_TABLE(of, max1118_dt_ids);
  236. static struct spi_driver max1118_spi_driver = {
  237. .driver = {
  238. .name = "max1118",
  239. .of_match_table = max1118_dt_ids,
  240. },
  241. .probe = max1118_probe,
  242. .id_table = max1118_id,
  243. };
  244. module_spi_driver(max1118_spi_driver);
  245. MODULE_AUTHOR("Akinobu Mita <[email protected]>");
  246. MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
  247. MODULE_LICENSE("GPL v2");