blob: ce45037295d88932abd579a20d40cd28e0e4d652 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * AD7766/AD7767 SPI ADC driver
3 *
4 * Copyright 2016 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/clk.h>
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>
18
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>
24
25struct ad7766_chip_info {
26 unsigned int decimation_factor;
27};
28
29enum {
30 AD7766_SUPPLY_AVDD = 0,
31 AD7766_SUPPLY_DVDD = 1,
32 AD7766_SUPPLY_VREF = 2,
33 AD7766_NUM_SUPPLIES = 3
34};
35
36struct ad7766 {
37 const struct ad7766_chip_info *chip_info;
38 struct spi_device *spi;
39 struct clk *mclk;
40 struct gpio_desc *pd_gpio;
41 struct regulator_bulk_data reg[AD7766_NUM_SUPPLIES];
42
43 struct iio_trigger *trig;
44
45 struct spi_transfer xfer;
46 struct spi_message msg;
47
48 /*
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.
53 */
54 unsigned char data[ALIGN(3, sizeof(s64)) + sizeof(s64)]
55 ____cacheline_aligned;
56};
57
58/*
59 * AD7766 and AD7767 variations are interface compatible, the main difference is
60 * analog performance. Both parts will use the same ID.
61 */
62enum ad7766_device_ids {
63 ID_AD7766,
64 ID_AD7766_1,
65 ID_AD7766_2,
66};
67
68static irqreturn_t ad7766_trigger_handler(int irq, void *p)
69{
70 struct iio_poll_func *pf = p;
71 struct iio_dev *indio_dev = pf->indio_dev;
72 struct ad7766 *ad7766 = iio_priv(indio_dev);
73 int ret;
74
75 ret = spi_sync(ad7766->spi, &ad7766->msg);
76 if (ret < 0)
77 goto done;
78
79 iio_push_to_buffers_with_timestamp(indio_dev, ad7766->data,
80 pf->timestamp);
81done:
82 iio_trigger_notify_done(indio_dev->trig);
83
84 return IRQ_HANDLED;
85}
86
87static int ad7766_preenable(struct iio_dev *indio_dev)
88{
89 struct ad7766 *ad7766 = iio_priv(indio_dev);
90 int ret;
91
92 ret = regulator_bulk_enable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
93 if (ret < 0) {
94 dev_err(&ad7766->spi->dev, "Failed to enable supplies: %d\n",
95 ret);
96 return ret;
97 }
98
99 ret = clk_prepare_enable(ad7766->mclk);
100 if (ret < 0) {
101 dev_err(&ad7766->spi->dev, "Failed to enable MCLK: %d\n", ret);
102 regulator_bulk_disable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
103 return ret;
104 }
105
106 gpiod_set_value(ad7766->pd_gpio, 0);
107
108 return 0;
109}
110
111static int ad7766_postdisable(struct iio_dev *indio_dev)
112{
113 struct ad7766 *ad7766 = iio_priv(indio_dev);
114
115 gpiod_set_value(ad7766->pd_gpio, 1);
116
117 /*
118 * The PD pin is synchronous to the clock, so give it some time to
119 * notice the change before we disable the clock.
120 */
121 msleep(20);
122
123 clk_disable_unprepare(ad7766->mclk);
124 regulator_bulk_disable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
125
126 return 0;
127}
128
129static int ad7766_read_raw(struct iio_dev *indio_dev,
130 const struct iio_chan_spec *chan, int *val, int *val2, long info)
131{
132 struct ad7766 *ad7766 = iio_priv(indio_dev);
133 struct regulator *vref = ad7766->reg[AD7766_SUPPLY_VREF].consumer;
134 int scale_uv;
135
136 switch (info) {
137 case IIO_CHAN_INFO_SCALE:
138 scale_uv = regulator_get_voltage(vref);
139 if (scale_uv < 0)
140 return scale_uv;
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;
147 return IIO_VAL_INT;
148 }
149 return -EINVAL;
150}
151
152static const struct iio_chan_spec ad7766_channels[] = {
153 {
154 .type = IIO_VOLTAGE,
155 .indexed = 1,
156 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
157 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
158 .scan_type = {
159 .sign = 's',
160 .realbits = 24,
161 .storagebits = 32,
162 .endianness = IIO_BE,
163 },
164 },
165 IIO_CHAN_SOFT_TIMESTAMP(1),
166};
167
168static const struct ad7766_chip_info ad7766_chip_info[] = {
169 [ID_AD7766] = {
170 .decimation_factor = 8,
171 },
172 [ID_AD7766_1] = {
173 .decimation_factor = 16,
174 },
175 [ID_AD7766_2] = {
176 .decimation_factor = 32,
177 },
178};
179
180static 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,
185};
186
187static const struct iio_info ad7766_info = {
188 .driver_module = THIS_MODULE,
189 .read_raw = &ad7766_read_raw,
190};
191
192static irqreturn_t ad7766_irq(int irq, void *private)
193{
194 iio_trigger_poll(private);
195 return IRQ_HANDLED;
196}
197
198static int ad7766_set_trigger_state(struct iio_trigger *trig, bool enable)
199{
200 struct ad7766 *ad7766 = iio_trigger_get_drvdata(trig);
201
202 if (enable)
203 enable_irq(ad7766->spi->irq);
204 else
205 disable_irq(ad7766->spi->irq);
206
207 return 0;
208}
209
210static const struct iio_trigger_ops ad7766_trigger_ops = {
211 .owner = THIS_MODULE,
212 .set_trigger_state = ad7766_set_trigger_state,
213 .validate_device = iio_trigger_validate_own_device,
214};
215
216static int ad7766_probe(struct spi_device *spi)
217{
218 const struct spi_device_id *id = spi_get_device_id(spi);
219 struct iio_dev *indio_dev;
220 struct ad7766 *ad7766;
221 int ret;
222
223 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*ad7766));
224 if (!indio_dev)
225 return -ENOMEM;
226
227 ad7766 = iio_priv(indio_dev);
228 ad7766->chip_info = &ad7766_chip_info[id->driver_data];
229
230 ad7766->mclk = devm_clk_get(&spi->dev, "mclk");
231 if (IS_ERR(ad7766->mclk))
232 return PTR_ERR(ad7766->mclk);
233
234 ad7766->reg[AD7766_SUPPLY_AVDD].supply = "avdd";
235 ad7766->reg[AD7766_SUPPLY_DVDD].supply = "dvdd";
236 ad7766->reg[AD7766_SUPPLY_VREF].supply = "vref";
237
238 ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(ad7766->reg),
239 ad7766->reg);
240 if (ret)
241 return ret;
242
243 ad7766->pd_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
244 GPIOD_OUT_HIGH);
245 if (IS_ERR(ad7766->pd_gpio))
246 return PTR_ERR(ad7766->pd_gpio);
247
248 indio_dev->dev.parent = &spi->dev;
249 indio_dev->name = spi_get_device_id(spi)->name;
250 indio_dev->modes = INDIO_DIRECT_MODE;
251 indio_dev->channels = ad7766_channels;
252 indio_dev->num_channels = ARRAY_SIZE(ad7766_channels);
253 indio_dev->info = &ad7766_info;
254
255 if (spi->irq > 0) {
256 ad7766->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
257 indio_dev->name, indio_dev->id);
258 if (!ad7766->trig)
259 return -ENOMEM;
260
261 ad7766->trig->ops = &ad7766_trigger_ops;
262 ad7766->trig->dev.parent = &spi->dev;
263 iio_trigger_set_drvdata(ad7766->trig, ad7766);
264
265 ret = devm_request_irq(&spi->dev, spi->irq, ad7766_irq,
266 IRQF_TRIGGER_FALLING, dev_name(&spi->dev),
267 ad7766->trig);
268 if (ret < 0)
269 return ret;
270
271 /*
272 * The device generates interrupts as long as it is powered up.
273 * Some platforms might not allow the option to power it down so
274 * disable the interrupt to avoid extra load on the system
275 */
276 disable_irq(spi->irq);
277
278 ret = devm_iio_trigger_register(&spi->dev, ad7766->trig);
279 if (ret)
280 return ret;
281 }
282
283 spi_set_drvdata(spi, indio_dev);
284
285 ad7766->spi = spi;
286
287 /* First byte always 0 */
288 ad7766->xfer.rx_buf = &ad7766->data[1];
289 ad7766->xfer.len = 3;
290
291 spi_message_init(&ad7766->msg);
292 spi_message_add_tail(&ad7766->xfer, &ad7766->msg);
293
294 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
295 &iio_pollfunc_store_time, &ad7766_trigger_handler,
296 &ad7766_buffer_setup_ops);
297 if (ret)
298 return ret;
299
300 ret = devm_iio_device_register(&spi->dev, indio_dev);
301 if (ret)
302 return ret;
303 return 0;
304}
305
306static const struct spi_device_id ad7766_id[] = {
307 {"ad7766", ID_AD7766},
308 {"ad7766-1", ID_AD7766_1},
309 {"ad7766-2", ID_AD7766_2},
310 {"ad7767", ID_AD7766},
311 {"ad7767-1", ID_AD7766_1},
312 {"ad7767-2", ID_AD7766_2},
313 {}
314};
315MODULE_DEVICE_TABLE(spi, ad7766_id);
316
317static struct spi_driver ad7766_driver = {
318 .driver = {
319 .name = "ad7766",
320 },
321 .probe = ad7766_probe,
322 .id_table = ad7766_id,
323};
324module_spi_driver(ad7766_driver);
325
326MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
327MODULE_DESCRIPTION("Analog Devices AD7766 and AD7767 ADCs driver support");
328MODULE_LICENSE("GPL v2");