1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD7266/65 SPI ADC driver
5 * Copyright 2012 Analog Devices Inc.
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/spi/spi.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/err.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
17 #include <linux/interrupt.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
24 #include <linux/platform_data/ad7266.h>
27 struct spi_device
*spi
;
28 struct regulator
*reg
;
29 unsigned long vref_mv
;
31 struct spi_transfer single_xfer
[3];
32 struct spi_message single_msg
;
34 enum ad7266_range range
;
35 enum ad7266_mode mode
;
37 struct gpio_desc
*gpios
[3];
40 * DMA (thus cache coherency maintenance) requires the
41 * transfer buffers to live in their own cache lines.
42 * The buffer needs to be large enough to hold two samples (4 bytes) and
43 * the naturally aligned timestamp (8 bytes).
48 } data ____cacheline_aligned
;
51 static int ad7266_wakeup(struct ad7266_state
*st
)
53 /* Any read with >= 2 bytes will wake the device */
54 return spi_read(st
->spi
, &st
->data
.sample
[0], 2);
57 static int ad7266_powerdown(struct ad7266_state
*st
)
59 /* Any read with < 2 bytes will powerdown the device */
60 return spi_read(st
->spi
, &st
->data
.sample
[0], 1);
63 static int ad7266_preenable(struct iio_dev
*indio_dev
)
65 struct ad7266_state
*st
= iio_priv(indio_dev
);
66 return ad7266_wakeup(st
);
69 static int ad7266_postdisable(struct iio_dev
*indio_dev
)
71 struct ad7266_state
*st
= iio_priv(indio_dev
);
72 return ad7266_powerdown(st
);
75 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops
= {
76 .preenable
= &ad7266_preenable
,
77 .postenable
= &iio_triggered_buffer_postenable
,
78 .predisable
= &iio_triggered_buffer_predisable
,
79 .postdisable
= &ad7266_postdisable
,
82 static irqreturn_t
ad7266_trigger_handler(int irq
, void *p
)
84 struct iio_poll_func
*pf
= p
;
85 struct iio_dev
*indio_dev
= pf
->indio_dev
;
86 struct ad7266_state
*st
= iio_priv(indio_dev
);
89 ret
= spi_read(st
->spi
, st
->data
.sample
, 4);
91 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->data
,
95 iio_trigger_notify_done(indio_dev
->trig
);
100 static void ad7266_select_input(struct ad7266_state
*st
, unsigned int nr
)
108 case AD7266_MODE_SINGLE_ENDED
:
111 case AD7266_MODE_PSEUDO_DIFF
:
114 case AD7266_MODE_DIFF
:
119 for (i
= 0; i
< 3; ++i
)
120 gpiod_set_value(st
->gpios
[i
], (bool)(nr
& BIT(i
)));
123 static int ad7266_update_scan_mode(struct iio_dev
*indio_dev
,
124 const unsigned long *scan_mask
)
126 struct ad7266_state
*st
= iio_priv(indio_dev
);
127 unsigned int nr
= find_first_bit(scan_mask
, indio_dev
->masklength
);
129 ad7266_select_input(st
, nr
);
134 static int ad7266_read_single(struct ad7266_state
*st
, int *val
,
135 unsigned int address
)
139 ad7266_select_input(st
, address
);
141 ret
= spi_sync(st
->spi
, &st
->single_msg
);
142 *val
= be16_to_cpu(st
->data
.sample
[address
% 2]);
147 static int ad7266_read_raw(struct iio_dev
*indio_dev
,
148 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long m
)
150 struct ad7266_state
*st
= iio_priv(indio_dev
);
151 unsigned long scale_mv
;
155 case IIO_CHAN_INFO_RAW
:
156 ret
= iio_device_claim_direct_mode(indio_dev
);
159 ret
= ad7266_read_single(st
, val
, chan
->address
);
160 iio_device_release_direct_mode(indio_dev
);
162 *val
= (*val
>> 2) & 0xfff;
163 if (chan
->scan_type
.sign
== 's')
164 *val
= sign_extend32(*val
, 11);
167 case IIO_CHAN_INFO_SCALE
:
168 scale_mv
= st
->vref_mv
;
169 if (st
->mode
== AD7266_MODE_DIFF
)
171 if (st
->range
== AD7266_RANGE_2VREF
)
175 *val2
= chan
->scan_type
.realbits
;
176 return IIO_VAL_FRACTIONAL_LOG2
;
177 case IIO_CHAN_INFO_OFFSET
:
178 if (st
->range
== AD7266_RANGE_2VREF
&&
179 st
->mode
!= AD7266_MODE_DIFF
)
188 #define AD7266_CHAN(_chan, _sign) { \
189 .type = IIO_VOLTAGE, \
191 .channel = (_chan), \
192 .address = (_chan), \
193 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
194 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
195 | BIT(IIO_CHAN_INFO_OFFSET), \
196 .scan_index = (_chan), \
202 .endianness = IIO_BE, \
206 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS(_name, _sign) \
207 const struct iio_chan_spec ad7266_channels_##_name[] = { \
208 AD7266_CHAN(0, (_sign)), \
209 AD7266_CHAN(1, (_sign)), \
210 AD7266_CHAN(2, (_sign)), \
211 AD7266_CHAN(3, (_sign)), \
212 AD7266_CHAN(4, (_sign)), \
213 AD7266_CHAN(5, (_sign)), \
214 AD7266_CHAN(6, (_sign)), \
215 AD7266_CHAN(7, (_sign)), \
216 AD7266_CHAN(8, (_sign)), \
217 AD7266_CHAN(9, (_sign)), \
218 AD7266_CHAN(10, (_sign)), \
219 AD7266_CHAN(11, (_sign)), \
220 IIO_CHAN_SOFT_TIMESTAMP(13), \
223 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(_name, _sign) \
224 const struct iio_chan_spec ad7266_channels_##_name##_fixed[] = { \
225 AD7266_CHAN(0, (_sign)), \
226 AD7266_CHAN(1, (_sign)), \
227 IIO_CHAN_SOFT_TIMESTAMP(2), \
230 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS(u
, 'u');
231 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS(s
, 's');
232 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(u
, 'u');
233 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(s
, 's');
235 #define AD7266_CHAN_DIFF(_chan, _sign) { \
236 .type = IIO_VOLTAGE, \
238 .channel = (_chan) * 2, \
239 .channel2 = (_chan) * 2 + 1, \
240 .address = (_chan), \
241 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
242 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
243 | BIT(IIO_CHAN_INFO_OFFSET), \
244 .scan_index = (_chan), \
250 .endianness = IIO_BE, \
255 #define AD7266_DECLARE_DIFF_CHANNELS(_name, _sign) \
256 const struct iio_chan_spec ad7266_channels_diff_##_name[] = { \
257 AD7266_CHAN_DIFF(0, (_sign)), \
258 AD7266_CHAN_DIFF(1, (_sign)), \
259 AD7266_CHAN_DIFF(2, (_sign)), \
260 AD7266_CHAN_DIFF(3, (_sign)), \
261 AD7266_CHAN_DIFF(4, (_sign)), \
262 AD7266_CHAN_DIFF(5, (_sign)), \
263 IIO_CHAN_SOFT_TIMESTAMP(6), \
266 static AD7266_DECLARE_DIFF_CHANNELS(s
, 's');
267 static AD7266_DECLARE_DIFF_CHANNELS(u
, 'u');
269 #define AD7266_DECLARE_DIFF_CHANNELS_FIXED(_name, _sign) \
270 const struct iio_chan_spec ad7266_channels_diff_fixed_##_name[] = { \
271 AD7266_CHAN_DIFF(0, (_sign)), \
272 AD7266_CHAN_DIFF(1, (_sign)), \
273 IIO_CHAN_SOFT_TIMESTAMP(2), \
276 static AD7266_DECLARE_DIFF_CHANNELS_FIXED(s
, 's');
277 static AD7266_DECLARE_DIFF_CHANNELS_FIXED(u
, 'u');
279 static const struct iio_info ad7266_info
= {
280 .read_raw
= &ad7266_read_raw
,
281 .update_scan_mode
= &ad7266_update_scan_mode
,
284 static const unsigned long ad7266_available_scan_masks
[] = {
294 static const unsigned long ad7266_available_scan_masks_diff
[] = {
301 static const unsigned long ad7266_available_scan_masks_fixed
[] = {
306 struct ad7266_chan_info
{
307 const struct iio_chan_spec
*channels
;
308 unsigned int num_channels
;
309 const unsigned long *scan_masks
;
312 #define AD7266_CHAN_INFO_INDEX(_differential, _signed, _fixed) \
313 (((_differential) << 2) | ((_signed) << 1) | ((_fixed) << 0))
315 static const struct ad7266_chan_info ad7266_chan_infos
[] = {
316 [AD7266_CHAN_INFO_INDEX(0, 0, 0)] = {
317 .channels
= ad7266_channels_u
,
318 .num_channels
= ARRAY_SIZE(ad7266_channels_u
),
319 .scan_masks
= ad7266_available_scan_masks
,
321 [AD7266_CHAN_INFO_INDEX(0, 0, 1)] = {
322 .channels
= ad7266_channels_u_fixed
,
323 .num_channels
= ARRAY_SIZE(ad7266_channels_u_fixed
),
324 .scan_masks
= ad7266_available_scan_masks_fixed
,
326 [AD7266_CHAN_INFO_INDEX(0, 1, 0)] = {
327 .channels
= ad7266_channels_s
,
328 .num_channels
= ARRAY_SIZE(ad7266_channels_s
),
329 .scan_masks
= ad7266_available_scan_masks
,
331 [AD7266_CHAN_INFO_INDEX(0, 1, 1)] = {
332 .channels
= ad7266_channels_s_fixed
,
333 .num_channels
= ARRAY_SIZE(ad7266_channels_s_fixed
),
334 .scan_masks
= ad7266_available_scan_masks_fixed
,
336 [AD7266_CHAN_INFO_INDEX(1, 0, 0)] = {
337 .channels
= ad7266_channels_diff_u
,
338 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_u
),
339 .scan_masks
= ad7266_available_scan_masks_diff
,
341 [AD7266_CHAN_INFO_INDEX(1, 0, 1)] = {
342 .channels
= ad7266_channels_diff_fixed_u
,
343 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_fixed_u
),
344 .scan_masks
= ad7266_available_scan_masks_fixed
,
346 [AD7266_CHAN_INFO_INDEX(1, 1, 0)] = {
347 .channels
= ad7266_channels_diff_s
,
348 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_s
),
349 .scan_masks
= ad7266_available_scan_masks_diff
,
351 [AD7266_CHAN_INFO_INDEX(1, 1, 1)] = {
352 .channels
= ad7266_channels_diff_fixed_s
,
353 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_fixed_s
),
354 .scan_masks
= ad7266_available_scan_masks_fixed
,
358 static void ad7266_init_channels(struct iio_dev
*indio_dev
)
360 struct ad7266_state
*st
= iio_priv(indio_dev
);
361 bool is_differential
, is_signed
;
362 const struct ad7266_chan_info
*chan_info
;
365 is_differential
= st
->mode
!= AD7266_MODE_SINGLE_ENDED
;
366 is_signed
= (st
->range
== AD7266_RANGE_2VREF
) |
367 (st
->mode
== AD7266_MODE_DIFF
);
369 i
= AD7266_CHAN_INFO_INDEX(is_differential
, is_signed
, st
->fixed_addr
);
370 chan_info
= &ad7266_chan_infos
[i
];
372 indio_dev
->channels
= chan_info
->channels
;
373 indio_dev
->num_channels
= chan_info
->num_channels
;
374 indio_dev
->available_scan_masks
= chan_info
->scan_masks
;
375 indio_dev
->masklength
= chan_info
->num_channels
- 1;
378 static const char * const ad7266_gpio_labels
[] = {
382 static int ad7266_probe(struct spi_device
*spi
)
384 struct ad7266_platform_data
*pdata
= spi
->dev
.platform_data
;
385 struct iio_dev
*indio_dev
;
386 struct ad7266_state
*st
;
390 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
391 if (indio_dev
== NULL
)
394 st
= iio_priv(indio_dev
);
396 st
->reg
= devm_regulator_get_optional(&spi
->dev
, "vref");
397 if (!IS_ERR(st
->reg
)) {
398 ret
= regulator_enable(st
->reg
);
402 ret
= regulator_get_voltage(st
->reg
);
404 goto error_disable_reg
;
406 st
->vref_mv
= ret
/ 1000;
408 /* Any other error indicates that the regulator does exist */
409 if (PTR_ERR(st
->reg
) != -ENODEV
)
410 return PTR_ERR(st
->reg
);
411 /* Use internal reference */
416 st
->fixed_addr
= pdata
->fixed_addr
;
417 st
->mode
= pdata
->mode
;
418 st
->range
= pdata
->range
;
420 if (!st
->fixed_addr
) {
421 for (i
= 0; i
< ARRAY_SIZE(st
->gpios
); ++i
) {
422 st
->gpios
[i
] = devm_gpiod_get(&spi
->dev
,
423 ad7266_gpio_labels
[i
],
425 if (IS_ERR(st
->gpios
[i
])) {
426 ret
= PTR_ERR(st
->gpios
[i
]);
427 goto error_disable_reg
;
432 st
->fixed_addr
= true;
433 st
->range
= AD7266_RANGE_VREF
;
434 st
->mode
= AD7266_MODE_DIFF
;
437 spi_set_drvdata(spi
, indio_dev
);
440 indio_dev
->dev
.parent
= &spi
->dev
;
441 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
442 indio_dev
->name
= spi_get_device_id(spi
)->name
;
443 indio_dev
->modes
= INDIO_DIRECT_MODE
;
444 indio_dev
->info
= &ad7266_info
;
446 ad7266_init_channels(indio_dev
);
449 st
->single_xfer
[0].rx_buf
= &st
->data
.sample
[0];
450 st
->single_xfer
[0].len
= 2;
451 st
->single_xfer
[0].cs_change
= 1;
453 st
->single_xfer
[1].rx_buf
= st
->data
.sample
;
454 st
->single_xfer
[1].len
= 4;
455 st
->single_xfer
[1].cs_change
= 1;
457 st
->single_xfer
[2].tx_buf
= &st
->data
.sample
[0];
458 st
->single_xfer
[2].len
= 1;
460 spi_message_init(&st
->single_msg
);
461 spi_message_add_tail(&st
->single_xfer
[0], &st
->single_msg
);
462 spi_message_add_tail(&st
->single_xfer
[1], &st
->single_msg
);
463 spi_message_add_tail(&st
->single_xfer
[2], &st
->single_msg
);
465 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
466 &ad7266_trigger_handler
, &iio_triggered_buffer_setup_ops
);
468 goto error_disable_reg
;
470 ret
= iio_device_register(indio_dev
);
472 goto error_buffer_cleanup
;
476 error_buffer_cleanup
:
477 iio_triggered_buffer_cleanup(indio_dev
);
479 if (!IS_ERR(st
->reg
))
480 regulator_disable(st
->reg
);
485 static int ad7266_remove(struct spi_device
*spi
)
487 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
488 struct ad7266_state
*st
= iio_priv(indio_dev
);
490 iio_device_unregister(indio_dev
);
491 iio_triggered_buffer_cleanup(indio_dev
);
492 if (!IS_ERR(st
->reg
))
493 regulator_disable(st
->reg
);
498 static const struct spi_device_id ad7266_id
[] = {
503 MODULE_DEVICE_TABLE(spi
, ad7266_id
);
505 static struct spi_driver ad7266_driver
= {
509 .probe
= ad7266_probe
,
510 .remove
= ad7266_remove
,
511 .id_table
= ad7266_id
,
513 module_spi_driver(ad7266_driver
);
515 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
516 MODULE_DESCRIPTION("Analog Devices AD7266/65 ADC");
517 MODULE_LICENSE("GPL v2");