max1118.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <akinobu.mita@gmail.com>
  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 ____cacheline_aligned;
  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 spi_device *spi, int channel)
  63. {
  64. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  65. struct max1118 *adc = iio_priv(indio_dev);
  66. struct spi_transfer xfers[] = {
  67. /*
  68. * To select CH1 for conversion, CNVST pin must be brought high
  69. * and low for a second time.
  70. */
  71. {
  72. .len = 0,
  73. .delay = { /* > CNVST Low Time 100 ns */
  74. .value = 1,
  75. .unit = SPI_DELAY_UNIT_USECS
  76. },
  77. .cs_change = 1,
  78. },
  79. /*
  80. * The acquisition interval begins with the falling edge of
  81. * CNVST. The total acquisition and conversion process takes
  82. * <7.5us.
  83. */
  84. {
  85. .len = 0,
  86. .delay = {
  87. .value = 8,
  88. .unit = SPI_DELAY_UNIT_USECS
  89. },
  90. },
  91. {
  92. .rx_buf = &adc->data,
  93. .len = 1,
  94. },
  95. };
  96. int ret;
  97. if (channel == 0)
  98. ret = spi_sync_transfer(spi, xfers + 1, 2);
  99. else
  100. ret = spi_sync_transfer(spi, xfers, 3);
  101. if (ret)
  102. return ret;
  103. return adc->data;
  104. }
  105. static int max1118_get_vref_mV(struct spi_device *spi)
  106. {
  107. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  108. struct max1118 *adc = iio_priv(indio_dev);
  109. const struct spi_device_id *id = spi_get_device_id(spi);
  110. int vref_uV;
  111. switch (id->driver_data) {
  112. case max1117:
  113. return 2048;
  114. case max1119:
  115. return 4096;
  116. case max1118:
  117. vref_uV = regulator_get_voltage(adc->reg);
  118. if (vref_uV < 0)
  119. return vref_uV;
  120. return vref_uV / 1000;
  121. }
  122. return -ENODEV;
  123. }
  124. static int max1118_read_raw(struct iio_dev *indio_dev,
  125. struct iio_chan_spec const *chan,
  126. int *val, int *val2, long mask)
  127. {
  128. struct max1118 *adc = iio_priv(indio_dev);
  129. switch (mask) {
  130. case IIO_CHAN_INFO_RAW:
  131. mutex_lock(&adc->lock);
  132. *val = max1118_read(adc->spi, chan->channel);
  133. mutex_unlock(&adc->lock);
  134. if (*val < 0)
  135. return *val;
  136. return IIO_VAL_INT;
  137. case IIO_CHAN_INFO_SCALE:
  138. *val = max1118_get_vref_mV(adc->spi);
  139. if (*val < 0)
  140. return *val;
  141. *val2 = 8;
  142. return IIO_VAL_FRACTIONAL_LOG2;
  143. }
  144. return -EINVAL;
  145. }
  146. static const struct iio_info max1118_info = {
  147. .read_raw = max1118_read_raw,
  148. };
  149. static irqreturn_t max1118_trigger_handler(int irq, void *p)
  150. {
  151. struct iio_poll_func *pf = p;
  152. struct iio_dev *indio_dev = pf->indio_dev;
  153. struct max1118 *adc = iio_priv(indio_dev);
  154. int scan_index;
  155. int i = 0;
  156. mutex_lock(&adc->lock);
  157. for_each_set_bit(scan_index, indio_dev->active_scan_mask,
  158. indio_dev->masklength) {
  159. const struct iio_chan_spec *scan_chan =
  160. &indio_dev->channels[scan_index];
  161. int ret = max1118_read(adc->spi, scan_chan->channel);
  162. if (ret < 0) {
  163. dev_warn(&adc->spi->dev,
  164. "failed to get conversion data\n");
  165. goto out;
  166. }
  167. adc->scan.channels[i] = ret;
  168. i++;
  169. }
  170. iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
  171. iio_get_time_ns(indio_dev));
  172. out:
  173. mutex_unlock(&adc->lock);
  174. iio_trigger_notify_done(indio_dev->trig);
  175. return IRQ_HANDLED;
  176. }
  177. static int max1118_probe(struct spi_device *spi)
  178. {
  179. struct iio_dev *indio_dev;
  180. struct max1118 *adc;
  181. const struct spi_device_id *id = spi_get_device_id(spi);
  182. int ret;
  183. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  184. if (!indio_dev)
  185. return -ENOMEM;
  186. adc = iio_priv(indio_dev);
  187. adc->spi = spi;
  188. mutex_init(&adc->lock);
  189. if (id->driver_data == max1118) {
  190. adc->reg = devm_regulator_get(&spi->dev, "vref");
  191. if (IS_ERR(adc->reg)) {
  192. dev_err(&spi->dev, "failed to get vref regulator\n");
  193. return PTR_ERR(adc->reg);
  194. }
  195. ret = regulator_enable(adc->reg);
  196. if (ret)
  197. return ret;
  198. }
  199. spi_set_drvdata(spi, indio_dev);
  200. indio_dev->name = spi_get_device_id(spi)->name;
  201. indio_dev->info = &max1118_info;
  202. indio_dev->modes = INDIO_DIRECT_MODE;
  203. indio_dev->channels = max1118_channels;
  204. indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
  205. /*
  206. * To reinitiate a conversion on CH0, it is necessary to allow for a
  207. * conversion to be complete and all of the data to be read out. Once
  208. * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
  209. * into AutoShutdown mode until the next conversion is initiated.
  210. */
  211. max1118_read(spi, 0);
  212. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  213. max1118_trigger_handler, NULL);
  214. if (ret)
  215. goto err_reg_disable;
  216. ret = iio_device_register(indio_dev);
  217. if (ret)
  218. goto err_buffer_cleanup;
  219. return 0;
  220. err_buffer_cleanup:
  221. iio_triggered_buffer_cleanup(indio_dev);
  222. err_reg_disable:
  223. if (id->driver_data == max1118)
  224. regulator_disable(adc->reg);
  225. return ret;
  226. }
  227. static int max1118_remove(struct spi_device *spi)
  228. {
  229. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  230. struct max1118 *adc = iio_priv(indio_dev);
  231. const struct spi_device_id *id = spi_get_device_id(spi);
  232. iio_device_unregister(indio_dev);
  233. iio_triggered_buffer_cleanup(indio_dev);
  234. if (id->driver_data == max1118)
  235. return regulator_disable(adc->reg);
  236. return 0;
  237. }
  238. static const struct spi_device_id max1118_id[] = {
  239. { "max1117", max1117 },
  240. { "max1118", max1118 },
  241. { "max1119", max1119 },
  242. {}
  243. };
  244. MODULE_DEVICE_TABLE(spi, max1118_id);
  245. static const struct of_device_id max1118_dt_ids[] = {
  246. { .compatible = "maxim,max1117" },
  247. { .compatible = "maxim,max1118" },
  248. { .compatible = "maxim,max1119" },
  249. {},
  250. };
  251. MODULE_DEVICE_TABLE(of, max1118_dt_ids);
  252. static struct spi_driver max1118_spi_driver = {
  253. .driver = {
  254. .name = "max1118",
  255. .of_match_table = max1118_dt_ids,
  256. },
  257. .probe = max1118_probe,
  258. .remove = max1118_remove,
  259. .id_table = max1118_id,
  260. };
  261. module_spi_driver(max1118_spi_driver);
  262. MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
  263. MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
  264. MODULE_LICENSE("GPL v2");