1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AD7766/AD7767 SPI ADC driver
5 * Copyright 2016 Analog Devices Inc.
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/module.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/slab.h>
16 #include <linux/spi/spi.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
24 struct ad7766_chip_info {
25 unsigned int decimation_factor;
29 AD7766_SUPPLY_AVDD = 0,
30 AD7766_SUPPLY_DVDD = 1,
31 AD7766_SUPPLY_VREF = 2,
32 AD7766_NUM_SUPPLIES = 3
36 const struct ad7766_chip_info *chip_info;
37 struct spi_device *spi;
39 struct gpio_desc *pd_gpio;
40 struct regulator_bulk_data reg[AD7766_NUM_SUPPLIES];
42 struct iio_trigger *trig;
44 struct spi_transfer xfer;
45 struct spi_message msg;
48 * DMA (thus cache coherency maintenance) requires the
49 * transfer buffers to live in their own cache lines.
50 * Make the buffer large enough for one 24 bit sample and one 64 bit
51 * aligned 64 bit timestamp.
53 unsigned char data[ALIGN(3, sizeof(s64)) + sizeof(s64)]
54 ____cacheline_aligned;
58 * AD7766 and AD7767 variations are interface compatible, the main difference is
59 * analog performance. Both parts will use the same ID.
61 enum ad7766_device_ids {
67 static irqreturn_t ad7766_trigger_handler(int irq, void *p)
69 struct iio_poll_func *pf = p;
70 struct iio_dev *indio_dev = pf->indio_dev;
71 struct ad7766 *ad7766 = iio_priv(indio_dev);
74 ret = spi_sync(ad7766->spi, &ad7766->msg);
78 iio_push_to_buffers_with_timestamp(indio_dev, ad7766->data,
81 iio_trigger_notify_done(indio_dev->trig);
86 static int ad7766_preenable(struct iio_dev *indio_dev)
88 struct ad7766 *ad7766 = iio_priv(indio_dev);
91 ret = regulator_bulk_enable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
93 dev_err(&ad7766->spi->dev, "Failed to enable supplies: %d\n",
98 ret = clk_prepare_enable(ad7766->mclk);
100 dev_err(&ad7766->spi->dev, "Failed to enable MCLK: %d\n", ret);
101 regulator_bulk_disable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
105 gpiod_set_value(ad7766->pd_gpio, 0);
110 static int ad7766_postdisable(struct iio_dev *indio_dev)
112 struct ad7766 *ad7766 = iio_priv(indio_dev);
114 gpiod_set_value(ad7766->pd_gpio, 1);
117 * The PD pin is synchronous to the clock, so give it some time to
118 * notice the change before we disable the clock.
122 clk_disable_unprepare(ad7766->mclk);
123 regulator_bulk_disable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
128 static int ad7766_read_raw(struct iio_dev *indio_dev,
129 const struct iio_chan_spec *chan, int *val, int *val2, long info)
131 struct ad7766 *ad7766 = iio_priv(indio_dev);
132 struct regulator *vref = ad7766->reg[AD7766_SUPPLY_VREF].consumer;
136 case IIO_CHAN_INFO_SCALE:
137 scale_uv = regulator_get_voltage(vref);
140 *val = scale_uv / 1000;
141 *val2 = chan->scan_type.realbits;
142 return IIO_VAL_FRACTIONAL_LOG2;
143 case IIO_CHAN_INFO_SAMP_FREQ:
144 *val = clk_get_rate(ad7766->mclk) /
145 ad7766->chip_info->decimation_factor;
151 static const struct iio_chan_spec ad7766_channels[] = {
155 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
156 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
161 .endianness = IIO_BE,
164 IIO_CHAN_SOFT_TIMESTAMP(1),
167 static const struct ad7766_chip_info ad7766_chip_info[] = {
169 .decimation_factor = 8,
172 .decimation_factor = 16,
175 .decimation_factor = 32,
179 static const struct iio_buffer_setup_ops ad7766_buffer_setup_ops = {
180 .preenable = &ad7766_preenable,
181 .postenable = &iio_triggered_buffer_postenable,
182 .predisable = &iio_triggered_buffer_predisable,
183 .postdisable = &ad7766_postdisable,
186 static const struct iio_info ad7766_info = {
187 .read_raw = &ad7766_read_raw,
190 static irqreturn_t ad7766_irq(int irq, void *private)
192 iio_trigger_poll(private);
196 static int ad7766_set_trigger_state(struct iio_trigger *trig, bool enable)
198 struct ad7766 *ad7766 = iio_trigger_get_drvdata(trig);
201 enable_irq(ad7766->spi->irq);
203 disable_irq(ad7766->spi->irq);
208 static const struct iio_trigger_ops ad7766_trigger_ops = {
209 .set_trigger_state = ad7766_set_trigger_state,
210 .validate_device = iio_trigger_validate_own_device,
213 static int ad7766_probe(struct spi_device *spi)
215 const struct spi_device_id *id = spi_get_device_id(spi);
216 struct iio_dev *indio_dev;
217 struct ad7766 *ad7766;
220 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*ad7766));
224 ad7766 = iio_priv(indio_dev);
225 ad7766->chip_info = &ad7766_chip_info[id->driver_data];
227 ad7766->mclk = devm_clk_get(&spi->dev, "mclk");
228 if (IS_ERR(ad7766->mclk))
229 return PTR_ERR(ad7766->mclk);
231 ad7766->reg[AD7766_SUPPLY_AVDD].supply = "avdd";
232 ad7766->reg[AD7766_SUPPLY_DVDD].supply = "dvdd";
233 ad7766->reg[AD7766_SUPPLY_VREF].supply = "vref";
235 ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(ad7766->reg),
240 ad7766->pd_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
242 if (IS_ERR(ad7766->pd_gpio))
243 return PTR_ERR(ad7766->pd_gpio);
245 indio_dev->dev.parent = &spi->dev;
246 indio_dev->name = spi_get_device_id(spi)->name;
247 indio_dev->modes = INDIO_DIRECT_MODE;
248 indio_dev->channels = ad7766_channels;
249 indio_dev->num_channels = ARRAY_SIZE(ad7766_channels);
250 indio_dev->info = &ad7766_info;
253 ad7766->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
254 indio_dev->name, indio_dev->id);
258 ad7766->trig->ops = &ad7766_trigger_ops;
259 ad7766->trig->dev.parent = &spi->dev;
260 iio_trigger_set_drvdata(ad7766->trig, ad7766);
262 ret = devm_request_irq(&spi->dev, spi->irq, ad7766_irq,
263 IRQF_TRIGGER_FALLING, dev_name(&spi->dev),
269 * The device generates interrupts as long as it is powered up.
270 * Some platforms might not allow the option to power it down so
271 * disable the interrupt to avoid extra load on the system
273 disable_irq(spi->irq);
275 ret = devm_iio_trigger_register(&spi->dev, ad7766->trig);
280 spi_set_drvdata(spi, indio_dev);
284 /* First byte always 0 */
285 ad7766->xfer.rx_buf = &ad7766->data[1];
286 ad7766->xfer.len = 3;
288 spi_message_init(&ad7766->msg);
289 spi_message_add_tail(&ad7766->xfer, &ad7766->msg);
291 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
292 &iio_pollfunc_store_time, &ad7766_trigger_handler,
293 &ad7766_buffer_setup_ops);
297 ret = devm_iio_device_register(&spi->dev, indio_dev);
303 static const struct spi_device_id ad7766_id[] = {
304 {"ad7766", ID_AD7766},
305 {"ad7766-1", ID_AD7766_1},
306 {"ad7766-2", ID_AD7766_2},
307 {"ad7767", ID_AD7766},
308 {"ad7767-1", ID_AD7766_1},
309 {"ad7767-2", ID_AD7766_2},
312 MODULE_DEVICE_TABLE(spi, ad7766_id);
314 static struct spi_driver ad7766_driver = {
318 .probe = ad7766_probe,
319 .id_table = ad7766_id,
321 module_spi_driver(ad7766_driver);
323 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
324 MODULE_DESCRIPTION("Analog Devices AD7766 and AD7767 ADCs driver support");
325 MODULE_LICENSE("GPL v2");