xref: /linux/drivers/iio/adc/ad7766.c (revision 80d443e8876602be2c130f79c4de81e12e2a700d)
1 /*
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 
25 struct ad7766_chip_info {
26 	unsigned int decimation_factor;
27 };
28 
29 enum {
30 	AD7766_SUPPLY_AVDD = 0,
31 	AD7766_SUPPLY_DVDD = 1,
32 	AD7766_SUPPLY_VREF = 2,
33 	AD7766_NUM_SUPPLIES = 3
34 };
35 
36 struct 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  */
62 enum ad7766_device_ids {
63 	ID_AD7766,
64 	ID_AD7766_1,
65 	ID_AD7766_2,
66 };
67 
68 static 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);
81 done:
82 	iio_trigger_notify_done(indio_dev->trig);
83 
84 	return IRQ_HANDLED;
85 }
86 
87 static 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 	if (ad7766->pd_gpio)
107 		gpiod_set_value(ad7766->pd_gpio, 0);
108 
109 	return 0;
110 }
111 
112 static int ad7766_postdisable(struct iio_dev *indio_dev)
113 {
114 	struct ad7766 *ad7766 = iio_priv(indio_dev);
115 
116 	if (ad7766->pd_gpio)
117 		gpiod_set_value(ad7766->pd_gpio, 1);
118 
119 	/*
120 	 * The PD pin is synchronous to the clock, so give it some time to
121 	 * notice the change before we disable the clock.
122 	 */
123 	msleep(20);
124 
125 	clk_disable_unprepare(ad7766->mclk);
126 	regulator_bulk_disable(ARRAY_SIZE(ad7766->reg), ad7766->reg);
127 
128 	return 0;
129 }
130 
131 static int ad7766_read_raw(struct iio_dev *indio_dev,
132 	const struct iio_chan_spec *chan, int *val, int *val2, long info)
133 {
134 	struct ad7766 *ad7766 = iio_priv(indio_dev);
135 	struct regulator *vref = ad7766->reg[AD7766_SUPPLY_VREF].consumer;
136 	int scale_uv;
137 
138 	switch (info) {
139 	case IIO_CHAN_INFO_SCALE:
140 		scale_uv = regulator_get_voltage(vref);
141 		if (scale_uv < 0)
142 			return scale_uv;
143 		*val = scale_uv / 1000;
144 		*val2 = chan->scan_type.realbits;
145 		return IIO_VAL_FRACTIONAL_LOG2;
146 	case IIO_CHAN_INFO_SAMP_FREQ:
147 		*val = clk_get_rate(ad7766->mclk) /
148 			ad7766->chip_info->decimation_factor;
149 		return IIO_VAL_INT;
150 	}
151 	return -EINVAL;
152 }
153 
154 static const struct iio_chan_spec ad7766_channels[] = {
155 	{
156 		.type = IIO_VOLTAGE,
157 		.indexed = 1,
158 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
159 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
160 		.scan_type = {
161 			.sign = 's',
162 			.realbits = 24,
163 			.storagebits = 32,
164 			.endianness = IIO_BE,
165 		},
166 	},
167 	IIO_CHAN_SOFT_TIMESTAMP(1),
168 };
169 
170 static const struct ad7766_chip_info ad7766_chip_info[] = {
171 	[ID_AD7766] = {
172 		.decimation_factor = 8,
173 	},
174 	[ID_AD7766_1] = {
175 		.decimation_factor = 16,
176 	},
177 	[ID_AD7766_2] = {
178 		.decimation_factor = 32,
179 	},
180 };
181 
182 static const struct iio_buffer_setup_ops ad7766_buffer_setup_ops = {
183 	.preenable = &ad7766_preenable,
184 	.postenable = &iio_triggered_buffer_postenable,
185 	.predisable = &iio_triggered_buffer_predisable,
186 	.postdisable = &ad7766_postdisable,
187 };
188 
189 static const struct iio_info ad7766_info = {
190 	.driver_module = THIS_MODULE,
191 	.read_raw = &ad7766_read_raw,
192 };
193 
194 static irqreturn_t ad7766_irq(int irq, void *private)
195 {
196 	iio_trigger_poll(private);
197 	return IRQ_HANDLED;
198 }
199 
200 static int ad7766_set_trigger_state(struct iio_trigger *trig, bool enable)
201 {
202 	struct ad7766 *ad7766 = iio_trigger_get_drvdata(trig);
203 
204 	if (enable)
205 		enable_irq(ad7766->spi->irq);
206 	else
207 		disable_irq(ad7766->spi->irq);
208 
209 	return 0;
210 }
211 
212 static const struct iio_trigger_ops ad7766_trigger_ops = {
213 	.owner = THIS_MODULE,
214 	.set_trigger_state = ad7766_set_trigger_state,
215 	.validate_device = iio_trigger_validate_own_device,
216 };
217 
218 static int ad7766_probe(struct spi_device *spi)
219 {
220 	const struct spi_device_id *id = spi_get_device_id(spi);
221 	struct iio_dev *indio_dev;
222 	struct ad7766 *ad7766;
223 	int ret;
224 
225 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*ad7766));
226 	if (!indio_dev)
227 		return -ENOMEM;
228 
229 	ad7766 = iio_priv(indio_dev);
230 	ad7766->chip_info = &ad7766_chip_info[id->driver_data];
231 
232 	ad7766->mclk = devm_clk_get(&spi->dev, "mclk");
233 	if (IS_ERR(ad7766->mclk))
234 		return PTR_ERR(ad7766->mclk);
235 
236 	ad7766->reg[AD7766_SUPPLY_AVDD].supply = "avdd";
237 	ad7766->reg[AD7766_SUPPLY_DVDD].supply = "dvdd";
238 	ad7766->reg[AD7766_SUPPLY_VREF].supply = "vref";
239 
240 	ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(ad7766->reg),
241 		ad7766->reg);
242 	if (ret)
243 		return ret;
244 
245 	ad7766->pd_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
246 		GPIOD_OUT_HIGH);
247 	if (IS_ERR(ad7766->pd_gpio))
248 		return PTR_ERR(ad7766->pd_gpio);
249 
250 	indio_dev->dev.parent = &spi->dev;
251 	indio_dev->name = spi_get_device_id(spi)->name;
252 	indio_dev->modes = INDIO_DIRECT_MODE;
253 	indio_dev->channels = ad7766_channels;
254 	indio_dev->num_channels = ARRAY_SIZE(ad7766_channels);
255 	indio_dev->info = &ad7766_info;
256 
257 	if (spi->irq > 0) {
258 		ad7766->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d",
259 			indio_dev->name, indio_dev->id);
260 		if (!ad7766->trig)
261 			return -ENOMEM;
262 
263 		ad7766->trig->ops = &ad7766_trigger_ops;
264 		ad7766->trig->dev.parent = &spi->dev;
265 		iio_trigger_set_drvdata(ad7766->trig, ad7766);
266 
267 		ret = devm_request_irq(&spi->dev, spi->irq, ad7766_irq,
268 			IRQF_TRIGGER_FALLING, dev_name(&spi->dev),
269 			ad7766->trig);
270 		if (ret < 0)
271 			return ret;
272 
273 		/*
274 		 * The device generates interrupts as long as it is powered up.
275 		 * Some platforms might not allow the option to power it down so
276 		 * disable the interrupt to avoid extra load on the system
277 		 */
278 		disable_irq(spi->irq);
279 
280 		ret = devm_iio_trigger_register(&spi->dev, ad7766->trig);
281 		if (ret)
282 			return ret;
283 	}
284 
285 	spi_set_drvdata(spi, indio_dev);
286 
287 	ad7766->spi = spi;
288 
289 	/* First byte always 0 */
290 	ad7766->xfer.rx_buf = &ad7766->data[1];
291 	ad7766->xfer.len = 3;
292 
293 	spi_message_init(&ad7766->msg);
294 	spi_message_add_tail(&ad7766->xfer, &ad7766->msg);
295 
296 	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
297 		&iio_pollfunc_store_time, &ad7766_trigger_handler,
298 		&ad7766_buffer_setup_ops);
299 	if (ret)
300 		return ret;
301 
302 	ret = devm_iio_device_register(&spi->dev, indio_dev);
303 	if (ret)
304 		return ret;
305 	return 0;
306 }
307 
308 static const struct spi_device_id ad7766_id[] = {
309 	{"ad7766", ID_AD7766},
310 	{"ad7766-1", ID_AD7766_1},
311 	{"ad7766-2", ID_AD7766_2},
312 	{"ad7767", ID_AD7766},
313 	{"ad7767-1", ID_AD7766_1},
314 	{"ad7767-2", ID_AD7766_2},
315 	{}
316 };
317 MODULE_DEVICE_TABLE(spi, ad7766_id);
318 
319 static struct spi_driver ad7766_driver = {
320 	.driver = {
321 		.name	= "ad7766",
322 	},
323 	.probe		= ad7766_probe,
324 	.id_table	= ad7766_id,
325 };
326 module_spi_driver(ad7766_driver);
327 
328 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
329 MODULE_DESCRIPTION("Analog Devices AD7766 and AD7767 ADCs driver support");
330 MODULE_LICENSE("GPL v2");
331