ad7766.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AD7766/AD7767 SPI ADC driver
  4. *
  5. * Copyright 2016 Analog Devices Inc.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/module.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/trigger.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #include <linux/iio/triggered_buffer.h>
  21. struct ad7766_chip_info {
  22. unsigned int decimation_factor;
  23. };
  24. enum {
  25. AD7766_SUPPLY_AVDD = 0,
  26. AD7766_SUPPLY_DVDD = 1,
  27. AD7766_SUPPLY_VREF = 2,
  28. AD7766_NUM_SUPPLIES = 3
  29. };
  30. struct ad7766 {
  31. const struct ad7766_chip_info *chip_info;
  32. struct spi_device *spi;
  33. struct clk *mclk;
  34. struct gpio_desc *pd_gpio;
  35. struct regulator_bulk_data reg[AD7766_NUM_SUPPLIES];
  36. struct iio_trigger *trig;
  37. struct spi_transfer xfer;
  38. struct spi_message msg;
  39. /*
  40. * DMA (thus cache coherency maintenance) may require the
  41. * transfer buffers to live in their own cache lines.
  42. * Make the buffer large enough for one 24 bit sample and one 64 bit
  43. * aligned 64 bit timestamp.
  44. */
  45. unsigned char data[ALIGN(3, sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN);
  46. };
  47. /*
  48. * AD7766 and AD7767 variations are interface compatible, the main difference is
  49. * analog performance. Both parts will use the same ID.
  50. */
  51. enum ad7766_device_ids {
  52. ID_AD7766,
  53. ID_AD7766_1,
  54. ID_AD7766_2,
  55. };
  56. static irqreturn_t ad7766_trigger_handler(int irq, void *p)
  57. {
  58. struct iio_poll_func *pf = p;
  59. struct iio_dev *indio_dev = pf->indio_dev;
  60. struct ad7766 *ad7766 = iio_priv(indio_dev);
  61. int ret;
  62. ret = spi_sync(ad7766->spi, &ad7766->msg);
  63. if (ret < 0)
  64. goto done;
  65. iio_push_to_buffers_with_timestamp(indio_dev, ad7766->data,
  66. pf->timestamp);
  67. done:
  68. iio_trigger_notify_done(indio_dev->trig);
  69. return IRQ_HANDLED;
  70. }
  71. static int ad7766_preenable(struct iio_dev *indio_dev)
  72. {
  73. struct ad7766 *ad7766 = iio_priv(indio_dev);
  74. int ret;
  75. ret = regulator_bulk_enable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
  76. if (ret < 0) {
  77. dev_err(&ad7766->spi->dev, "Failed to enable supplies: %d\n",
  78. ret);
  79. return ret;
  80. }
  81. ret = clk_prepare_enable(ad7766->mclk);
  82. if (ret < 0) {
  83. dev_err(&ad7766->spi->dev, "Failed to enable MCLK: %d\n", ret);
  84. regulator_bulk_disable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
  85. return ret;
  86. }
  87. gpiod_set_value(ad7766->pd_gpio, 0);
  88. return 0;
  89. }
  90. static int ad7766_postdisable(struct iio_dev *indio_dev)
  91. {
  92. struct ad7766 *ad7766 = iio_priv(indio_dev);
  93. gpiod_set_value(ad7766->pd_gpio, 1);
  94. /*
  95. * The PD pin is synchronous to the clock, so give it some time to
  96. * notice the change before we disable the clock.
  97. */
  98. msleep(20);
  99. clk_disable_unprepare(ad7766->mclk);
  100. regulator_bulk_disable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
  101. return 0;
  102. }
  103. static int ad7766_read_raw(struct iio_dev *indio_dev,
  104. const struct iio_chan_spec *chan, int *val, int *val2, long info)
  105. {
  106. struct ad7766 *ad7766 = iio_priv(indio_dev);
  107. struct regulator *vref = ad7766->reg[AD7766_SUPPLY_VREF].consumer;
  108. int scale_uv;
  109. switch (info) {
  110. case IIO_CHAN_INFO_SCALE:
  111. scale_uv = regulator_get_voltage(vref);
  112. if (scale_uv < 0)
  113. return scale_uv;
  114. *val = scale_uv / 1000;
  115. *val2 = chan->scan_type.realbits;
  116. return IIO_VAL_FRACTIONAL_LOG2;
  117. case IIO_CHAN_INFO_SAMP_FREQ:
  118. *val = clk_get_rate(ad7766->mclk) /
  119. ad7766->chip_info->decimation_factor;
  120. return IIO_VAL_INT;
  121. }
  122. return -EINVAL;
  123. }
  124. static const struct iio_chan_spec ad7766_channels[] = {
  125. {
  126. .type = IIO_VOLTAGE,
  127. .indexed = 1,
  128. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  129. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  130. .scan_type = {
  131. .sign = 's',
  132. .realbits = 24,
  133. .storagebits = 32,
  134. .endianness = IIO_BE,
  135. },
  136. },
  137. IIO_CHAN_SOFT_TIMESTAMP(1),
  138. };
  139. static const struct ad7766_chip_info ad7766_chip_info[] = {
  140. [ID_AD7766] = {
  141. .decimation_factor = 8,
  142. },
  143. [ID_AD7766_1] = {
  144. .decimation_factor = 16,
  145. },
  146. [ID_AD7766_2] = {
  147. .decimation_factor = 32,
  148. },
  149. };
  150. static const struct iio_buffer_setup_ops ad7766_buffer_setup_ops = {
  151. .preenable = &ad7766_preenable,
  152. .postdisable = &ad7766_postdisable,
  153. };
  154. static const struct iio_info ad7766_info = {
  155. .read_raw = &ad7766_read_raw,
  156. };
  157. static irqreturn_t ad7766_irq(int irq, void *private)
  158. {
  159. iio_trigger_poll(private);
  160. return IRQ_HANDLED;
  161. }
  162. static int ad7766_set_trigger_state(struct iio_trigger *trig, bool enable)
  163. {
  164. struct ad7766 *ad7766 = iio_trigger_get_drvdata(trig);
  165. if (enable)
  166. enable_irq(ad7766->spi->irq);
  167. else
  168. disable_irq(ad7766->spi->irq);
  169. return 0;
  170. }
  171. static const struct iio_trigger_ops ad7766_trigger_ops = {
  172. .set_trigger_state = ad7766_set_trigger_state,
  173. .validate_device = iio_trigger_validate_own_device,
  174. };
  175. static int ad7766_probe(struct spi_device *spi)
  176. {
  177. const struct spi_device_id *id = spi_get_device_id(spi);
  178. struct iio_dev *indio_dev;
  179. struct ad7766 *ad7766;
  180. int ret;
  181. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*ad7766));
  182. if (!indio_dev)
  183. return -ENOMEM;
  184. ad7766 = iio_priv(indio_dev);
  185. ad7766->chip_info = &ad7766_chip_info[id->driver_data];
  186. ad7766->mclk = devm_clk_get(&spi->dev, "mclk");
  187. if (IS_ERR(ad7766->mclk))
  188. return PTR_ERR(ad7766->mclk);
  189. ad7766->reg[AD7766_SUPPLY_AVDD].supply = "avdd";
  190. ad7766->reg[AD7766_SUPPLY_DVDD].supply = "dvdd";
  191. ad7766->reg[AD7766_SUPPLY_VREF].supply = "vref";
  192. ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(ad7766->reg),
  193. ad7766->reg);
  194. if (ret)
  195. return ret;
  196. ad7766->pd_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
  197. GPIOD_OUT_HIGH);
  198. if (IS_ERR(ad7766->pd_gpio))
  199. return PTR_ERR(ad7766->pd_gpio);
  200. indio_dev->name = spi_get_device_id(spi)->name;
  201. indio_dev->modes = INDIO_DIRECT_MODE;
  202. indio_dev->channels = ad7766_channels;
  203. indio_dev->num_channels = ARRAY_SIZE(ad7766_channels);
  204. indio_dev->info = &ad7766_info;
  205. if (spi->irq > 0) {
  206. ad7766->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
  207. indio_dev->name,
  208. iio_device_id(indio_dev));
  209. if (!ad7766->trig)
  210. return -ENOMEM;
  211. ad7766->trig->ops = &ad7766_trigger_ops;
  212. iio_trigger_set_drvdata(ad7766->trig, ad7766);
  213. /*
  214. * The device generates interrupts as long as it is powered up.
  215. * Some platforms might not allow the option to power it down so
  216. * don't enable the interrupt to avoid extra load on the system
  217. */
  218. ret = devm_request_irq(&spi->dev, spi->irq, ad7766_irq,
  219. IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN,
  220. dev_name(&spi->dev),
  221. ad7766->trig);
  222. if (ret < 0)
  223. return ret;
  224. ret = devm_iio_trigger_register(&spi->dev, ad7766->trig);
  225. if (ret)
  226. return ret;
  227. }
  228. ad7766->spi = spi;
  229. /* First byte always 0 */
  230. ad7766->xfer.rx_buf = &ad7766->data[1];
  231. ad7766->xfer.len = 3;
  232. spi_message_init(&ad7766->msg);
  233. spi_message_add_tail(&ad7766->xfer, &ad7766->msg);
  234. ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
  235. &iio_pollfunc_store_time, &ad7766_trigger_handler,
  236. &ad7766_buffer_setup_ops);
  237. if (ret)
  238. return ret;
  239. return devm_iio_device_register(&spi->dev, indio_dev);
  240. }
  241. static const struct spi_device_id ad7766_id[] = {
  242. {"ad7766", ID_AD7766},
  243. {"ad7766-1", ID_AD7766_1},
  244. {"ad7766-2", ID_AD7766_2},
  245. {"ad7767", ID_AD7766},
  246. {"ad7767-1", ID_AD7766_1},
  247. {"ad7767-2", ID_AD7766_2},
  248. {}
  249. };
  250. MODULE_DEVICE_TABLE(spi, ad7766_id);
  251. static struct spi_driver ad7766_driver = {
  252. .driver = {
  253. .name = "ad7766",
  254. },
  255. .probe = ad7766_probe,
  256. .id_table = ad7766_id,
  257. };
  258. module_spi_driver(ad7766_driver);
  259. MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
  260. MODULE_DESCRIPTION("Analog Devices AD7766 and AD7767 ADCs driver support");
  261. MODULE_LICENSE("GPL v2");