2 * AD7766/AD7767 SPI ADC driver
4 * Copyright 2016 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 #include <linux/spi/spi.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
25 struct ad7766_chip_info
{
26 unsigned int decimation_factor
;
30 AD7766_SUPPLY_AVDD
= 0,
31 AD7766_SUPPLY_DVDD
= 1,
32 AD7766_SUPPLY_VREF
= 2,
33 AD7766_NUM_SUPPLIES
= 3
37 const struct ad7766_chip_info
*chip_info
;
38 struct spi_device
*spi
;
40 struct gpio_desc
*pd_gpio
;
41 struct regulator_bulk_data reg
[AD7766_NUM_SUPPLIES
];
43 struct iio_trigger
*trig
;
45 struct spi_transfer xfer
;
46 struct spi_message msg
;
49 * DMA (thus cache coherency maintenance) requires the
50 * transfer buffers to live in their own cache lines.
51 * Make the buffer large enough for one 24 bit sample and one 64 bit
52 * aligned 64 bit timestamp.
54 unsigned char data
[ALIGN(3, sizeof(s64
)) + sizeof(s64
)]
55 ____cacheline_aligned
;
59 * AD7766 and AD7767 variations are interface compatible, the main difference is
60 * analog performance. Both parts will use the same ID.
62 enum ad7766_device_ids
{
68 static irqreturn_t
ad7766_trigger_handler(int irq
, void *p
)
70 struct iio_poll_func
*pf
= p
;
71 struct iio_dev
*indio_dev
= pf
->indio_dev
;
72 struct ad7766
*ad7766
= iio_priv(indio_dev
);
75 ret
= spi_sync(ad7766
->spi
, &ad7766
->msg
);
79 iio_push_to_buffers_with_timestamp(indio_dev
, ad7766
->data
,
82 iio_trigger_notify_done(indio_dev
->trig
);
87 static int ad7766_preenable(struct iio_dev
*indio_dev
)
89 struct ad7766
*ad7766
= iio_priv(indio_dev
);
92 ret
= regulator_bulk_enable(ARRAY_SIZE(ad7766
->reg
), ad7766
->reg
);
94 dev_err(&ad7766
->spi
->dev
, "Failed to enable supplies: %d\n",
99 ret
= clk_prepare_enable(ad7766
->mclk
);
101 dev_err(&ad7766
->spi
->dev
, "Failed to enable MCLK: %d\n", ret
);
102 regulator_bulk_disable(ARRAY_SIZE(ad7766
->reg
), ad7766
->reg
);
106 gpiod_set_value(ad7766
->pd_gpio
, 0);
111 static int ad7766_postdisable(struct iio_dev
*indio_dev
)
113 struct ad7766
*ad7766
= iio_priv(indio_dev
);
115 gpiod_set_value(ad7766
->pd_gpio
, 1);
118 * The PD pin is synchronous to the clock, so give it some time to
119 * notice the change before we disable the clock.
123 clk_disable_unprepare(ad7766
->mclk
);
124 regulator_bulk_disable(ARRAY_SIZE(ad7766
->reg
), ad7766
->reg
);
129 static int ad7766_read_raw(struct iio_dev
*indio_dev
,
130 const struct iio_chan_spec
*chan
, int *val
, int *val2
, long info
)
132 struct ad7766
*ad7766
= iio_priv(indio_dev
);
133 struct regulator
*vref
= ad7766
->reg
[AD7766_SUPPLY_VREF
].consumer
;
137 case IIO_CHAN_INFO_SCALE
:
138 scale_uv
= regulator_get_voltage(vref
);
141 *val
= scale_uv
/ 1000;
142 *val2
= chan
->scan_type
.realbits
;
143 return IIO_VAL_FRACTIONAL_LOG2
;
144 case IIO_CHAN_INFO_SAMP_FREQ
:
145 *val
= clk_get_rate(ad7766
->mclk
) /
146 ad7766
->chip_info
->decimation_factor
;
152 static const struct iio_chan_spec ad7766_channels
[] = {
156 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
157 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
162 .endianness
= IIO_BE
,
165 IIO_CHAN_SOFT_TIMESTAMP(1),
168 static const struct ad7766_chip_info ad7766_chip_info
[] = {
170 .decimation_factor
= 8,
173 .decimation_factor
= 16,
176 .decimation_factor
= 32,
180 static const struct iio_buffer_setup_ops ad7766_buffer_setup_ops
= {
181 .preenable
= &ad7766_preenable
,
182 .postenable
= &iio_triggered_buffer_postenable
,
183 .predisable
= &iio_triggered_buffer_predisable
,
184 .postdisable
= &ad7766_postdisable
,
187 static const struct iio_info ad7766_info
= {
188 .read_raw
= &ad7766_read_raw
,
191 static irqreturn_t
ad7766_irq(int irq
, void *private)
193 iio_trigger_poll(private);
197 static int ad7766_set_trigger_state(struct iio_trigger
*trig
, bool enable
)
199 struct ad7766
*ad7766
= iio_trigger_get_drvdata(trig
);
202 enable_irq(ad7766
->spi
->irq
);
204 disable_irq(ad7766
->spi
->irq
);
209 static const struct iio_trigger_ops ad7766_trigger_ops
= {
210 .set_trigger_state
= ad7766_set_trigger_state
,
211 .validate_device
= iio_trigger_validate_own_device
,
214 static int ad7766_probe(struct spi_device
*spi
)
216 const struct spi_device_id
*id
= spi_get_device_id(spi
);
217 struct iio_dev
*indio_dev
;
218 struct ad7766
*ad7766
;
221 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*ad7766
));
225 ad7766
= iio_priv(indio_dev
);
226 ad7766
->chip_info
= &ad7766_chip_info
[id
->driver_data
];
228 ad7766
->mclk
= devm_clk_get(&spi
->dev
, "mclk");
229 if (IS_ERR(ad7766
->mclk
))
230 return PTR_ERR(ad7766
->mclk
);
232 ad7766
->reg
[AD7766_SUPPLY_AVDD
].supply
= "avdd";
233 ad7766
->reg
[AD7766_SUPPLY_DVDD
].supply
= "dvdd";
234 ad7766
->reg
[AD7766_SUPPLY_VREF
].supply
= "vref";
236 ret
= devm_regulator_bulk_get(&spi
->dev
, ARRAY_SIZE(ad7766
->reg
),
241 ad7766
->pd_gpio
= devm_gpiod_get_optional(&spi
->dev
, "powerdown",
243 if (IS_ERR(ad7766
->pd_gpio
))
244 return PTR_ERR(ad7766
->pd_gpio
);
246 indio_dev
->dev
.parent
= &spi
->dev
;
247 indio_dev
->name
= spi_get_device_id(spi
)->name
;
248 indio_dev
->modes
= INDIO_DIRECT_MODE
;
249 indio_dev
->channels
= ad7766_channels
;
250 indio_dev
->num_channels
= ARRAY_SIZE(ad7766_channels
);
251 indio_dev
->info
= &ad7766_info
;
254 ad7766
->trig
= devm_iio_trigger_alloc(&spi
->dev
, "%s-dev%d",
255 indio_dev
->name
, indio_dev
->id
);
259 ad7766
->trig
->ops
= &ad7766_trigger_ops
;
260 ad7766
->trig
->dev
.parent
= &spi
->dev
;
261 iio_trigger_set_drvdata(ad7766
->trig
, ad7766
);
263 ret
= devm_request_irq(&spi
->dev
, spi
->irq
, ad7766_irq
,
264 IRQF_TRIGGER_FALLING
, dev_name(&spi
->dev
),
270 * The device generates interrupts as long as it is powered up.
271 * Some platforms might not allow the option to power it down so
272 * disable the interrupt to avoid extra load on the system
274 disable_irq(spi
->irq
);
276 ret
= devm_iio_trigger_register(&spi
->dev
, ad7766
->trig
);
281 spi_set_drvdata(spi
, indio_dev
);
285 /* First byte always 0 */
286 ad7766
->xfer
.rx_buf
= &ad7766
->data
[1];
287 ad7766
->xfer
.len
= 3;
289 spi_message_init(&ad7766
->msg
);
290 spi_message_add_tail(&ad7766
->xfer
, &ad7766
->msg
);
292 ret
= devm_iio_triggered_buffer_setup(&spi
->dev
, indio_dev
,
293 &iio_pollfunc_store_time
, &ad7766_trigger_handler
,
294 &ad7766_buffer_setup_ops
);
298 ret
= devm_iio_device_register(&spi
->dev
, indio_dev
);
304 static const struct spi_device_id ad7766_id
[] = {
305 {"ad7766", ID_AD7766
},
306 {"ad7766-1", ID_AD7766_1
},
307 {"ad7766-2", ID_AD7766_2
},
308 {"ad7767", ID_AD7766
},
309 {"ad7767-1", ID_AD7766_1
},
310 {"ad7767-2", ID_AD7766_2
},
313 MODULE_DEVICE_TABLE(spi
, ad7766_id
);
315 static struct spi_driver ad7766_driver
= {
319 .probe
= ad7766_probe
,
320 .id_table
= ad7766_id
,
322 module_spi_driver(ad7766_driver
);
324 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
325 MODULE_DESCRIPTION("Analog Devices AD7766 and AD7767 ADCs driver support");
326 MODULE_LICENSE("GPL v2");