max1118.c 6.9 KB

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