2 * AD7266/65 SPI ADC driver
4 * Copyright 2012 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spi/spi.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/module.h>
18 #include <linux/interrupt.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
25 #include <linux/platform_data/ad7266.h>
28 struct spi_device
*spi
;
29 struct regulator
*reg
;
30 unsigned long vref_mv
;
32 struct spi_transfer single_xfer
[3];
33 struct spi_message single_msg
;
35 enum ad7266_range range
;
36 enum ad7266_mode mode
;
41 * DMA (thus cache coherency maintenance) requires the
42 * transfer buffers to live in their own cache lines.
43 * The buffer needs to be large enough to hold two samples (4 bytes) and
44 * the naturally aligned timestamp (8 bytes).
49 } data ____cacheline_aligned
;
52 static int ad7266_wakeup(struct ad7266_state
*st
)
54 /* Any read with >= 2 bytes will wake the device */
55 return spi_read(st
->spi
, &st
->data
.sample
[0], 2);
58 static int ad7266_powerdown(struct ad7266_state
*st
)
60 /* Any read with < 2 bytes will powerdown the device */
61 return spi_read(st
->spi
, &st
->data
.sample
[0], 1);
64 static int ad7266_preenable(struct iio_dev
*indio_dev
)
66 struct ad7266_state
*st
= iio_priv(indio_dev
);
67 return ad7266_wakeup(st
);
70 static int ad7266_postdisable(struct iio_dev
*indio_dev
)
72 struct ad7266_state
*st
= iio_priv(indio_dev
);
73 return ad7266_powerdown(st
);
76 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops
= {
77 .preenable
= &ad7266_preenable
,
78 .postenable
= &iio_triggered_buffer_postenable
,
79 .predisable
= &iio_triggered_buffer_predisable
,
80 .postdisable
= &ad7266_postdisable
,
83 static irqreturn_t
ad7266_trigger_handler(int irq
, void *p
)
85 struct iio_poll_func
*pf
= p
;
86 struct iio_dev
*indio_dev
= pf
->indio_dev
;
87 struct ad7266_state
*st
= iio_priv(indio_dev
);
90 ret
= spi_read(st
->spi
, st
->data
.sample
, 4);
92 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->data
,
96 iio_trigger_notify_done(indio_dev
->trig
);
101 static void ad7266_select_input(struct ad7266_state
*st
, unsigned int nr
)
109 case AD7266_MODE_SINGLE_ENDED
:
112 case AD7266_MODE_PSEUDO_DIFF
:
115 case AD7266_MODE_DIFF
:
120 for (i
= 0; i
< 3; ++i
)
121 gpio_set_value(st
->gpios
[i
].gpio
, (bool)(nr
& BIT(i
)));
124 static int ad7266_update_scan_mode(struct iio_dev
*indio_dev
,
125 const unsigned long *scan_mask
)
127 struct ad7266_state
*st
= iio_priv(indio_dev
);
128 unsigned int nr
= find_first_bit(scan_mask
, indio_dev
->masklength
);
130 ad7266_select_input(st
, nr
);
135 static int ad7266_read_single(struct ad7266_state
*st
, int *val
,
136 unsigned int address
)
140 ad7266_select_input(st
, address
);
142 ret
= spi_sync(st
->spi
, &st
->single_msg
);
143 *val
= be16_to_cpu(st
->data
.sample
[address
% 2]);
148 static int ad7266_read_raw(struct iio_dev
*indio_dev
,
149 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long m
)
151 struct ad7266_state
*st
= iio_priv(indio_dev
);
152 unsigned long scale_mv
;
156 case IIO_CHAN_INFO_RAW
:
157 ret
= iio_device_claim_direct_mode(indio_dev
);
160 ret
= ad7266_read_single(st
, val
, chan
->address
);
161 iio_device_release_direct_mode(indio_dev
);
163 *val
= (*val
>> 2) & 0xfff;
164 if (chan
->scan_type
.sign
== 's')
165 *val
= sign_extend32(*val
, 11);
168 case IIO_CHAN_INFO_SCALE
:
169 scale_mv
= st
->vref_mv
;
170 if (st
->mode
== AD7266_MODE_DIFF
)
172 if (st
->range
== AD7266_RANGE_2VREF
)
176 *val2
= chan
->scan_type
.realbits
;
177 return IIO_VAL_FRACTIONAL_LOG2
;
178 case IIO_CHAN_INFO_OFFSET
:
179 if (st
->range
== AD7266_RANGE_2VREF
&&
180 st
->mode
!= AD7266_MODE_DIFF
)
189 #define AD7266_CHAN(_chan, _sign) { \
190 .type = IIO_VOLTAGE, \
192 .channel = (_chan), \
193 .address = (_chan), \
194 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
195 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
196 | BIT(IIO_CHAN_INFO_OFFSET), \
197 .scan_index = (_chan), \
203 .endianness = IIO_BE, \
207 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS(_name, _sign) \
208 const struct iio_chan_spec ad7266_channels_##_name[] = { \
209 AD7266_CHAN(0, (_sign)), \
210 AD7266_CHAN(1, (_sign)), \
211 AD7266_CHAN(2, (_sign)), \
212 AD7266_CHAN(3, (_sign)), \
213 AD7266_CHAN(4, (_sign)), \
214 AD7266_CHAN(5, (_sign)), \
215 AD7266_CHAN(6, (_sign)), \
216 AD7266_CHAN(7, (_sign)), \
217 AD7266_CHAN(8, (_sign)), \
218 AD7266_CHAN(9, (_sign)), \
219 AD7266_CHAN(10, (_sign)), \
220 AD7266_CHAN(11, (_sign)), \
221 IIO_CHAN_SOFT_TIMESTAMP(13), \
224 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(_name, _sign) \
225 const struct iio_chan_spec ad7266_channels_##_name##_fixed[] = { \
226 AD7266_CHAN(0, (_sign)), \
227 AD7266_CHAN(1, (_sign)), \
228 IIO_CHAN_SOFT_TIMESTAMP(2), \
231 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS(u
, 'u');
232 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS(s
, 's');
233 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(u
, 'u');
234 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(s
, 's');
236 #define AD7266_CHAN_DIFF(_chan, _sign) { \
237 .type = IIO_VOLTAGE, \
239 .channel = (_chan) * 2, \
240 .channel2 = (_chan) * 2 + 1, \
241 .address = (_chan), \
242 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
243 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
244 | BIT(IIO_CHAN_INFO_OFFSET), \
245 .scan_index = (_chan), \
251 .endianness = IIO_BE, \
256 #define AD7266_DECLARE_DIFF_CHANNELS(_name, _sign) \
257 const struct iio_chan_spec ad7266_channels_diff_##_name[] = { \
258 AD7266_CHAN_DIFF(0, (_sign)), \
259 AD7266_CHAN_DIFF(1, (_sign)), \
260 AD7266_CHAN_DIFF(2, (_sign)), \
261 AD7266_CHAN_DIFF(3, (_sign)), \
262 AD7266_CHAN_DIFF(4, (_sign)), \
263 AD7266_CHAN_DIFF(5, (_sign)), \
264 IIO_CHAN_SOFT_TIMESTAMP(6), \
267 static AD7266_DECLARE_DIFF_CHANNELS(s
, 's');
268 static AD7266_DECLARE_DIFF_CHANNELS(u
, 'u');
270 #define AD7266_DECLARE_DIFF_CHANNELS_FIXED(_name, _sign) \
271 const struct iio_chan_spec ad7266_channels_diff_fixed_##_name[] = { \
272 AD7266_CHAN_DIFF(0, (_sign)), \
273 AD7266_CHAN_DIFF(1, (_sign)), \
274 IIO_CHAN_SOFT_TIMESTAMP(2), \
277 static AD7266_DECLARE_DIFF_CHANNELS_FIXED(s
, 's');
278 static AD7266_DECLARE_DIFF_CHANNELS_FIXED(u
, 'u');
280 static const struct iio_info ad7266_info
= {
281 .read_raw
= &ad7266_read_raw
,
282 .update_scan_mode
= &ad7266_update_scan_mode
,
283 .driver_module
= THIS_MODULE
,
286 static const unsigned long ad7266_available_scan_masks
[] = {
296 static const unsigned long ad7266_available_scan_masks_diff
[] = {
303 static const unsigned long ad7266_available_scan_masks_fixed
[] = {
308 struct ad7266_chan_info
{
309 const struct iio_chan_spec
*channels
;
310 unsigned int num_channels
;
311 const unsigned long *scan_masks
;
314 #define AD7266_CHAN_INFO_INDEX(_differential, _signed, _fixed) \
315 (((_differential) << 2) | ((_signed) << 1) | ((_fixed) << 0))
317 static const struct ad7266_chan_info ad7266_chan_infos
[] = {
318 [AD7266_CHAN_INFO_INDEX(0, 0, 0)] = {
319 .channels
= ad7266_channels_u
,
320 .num_channels
= ARRAY_SIZE(ad7266_channels_u
),
321 .scan_masks
= ad7266_available_scan_masks
,
323 [AD7266_CHAN_INFO_INDEX(0, 0, 1)] = {
324 .channels
= ad7266_channels_u_fixed
,
325 .num_channels
= ARRAY_SIZE(ad7266_channels_u_fixed
),
326 .scan_masks
= ad7266_available_scan_masks_fixed
,
328 [AD7266_CHAN_INFO_INDEX(0, 1, 0)] = {
329 .channels
= ad7266_channels_s
,
330 .num_channels
= ARRAY_SIZE(ad7266_channels_s
),
331 .scan_masks
= ad7266_available_scan_masks
,
333 [AD7266_CHAN_INFO_INDEX(0, 1, 1)] = {
334 .channels
= ad7266_channels_s_fixed
,
335 .num_channels
= ARRAY_SIZE(ad7266_channels_s_fixed
),
336 .scan_masks
= ad7266_available_scan_masks_fixed
,
338 [AD7266_CHAN_INFO_INDEX(1, 0, 0)] = {
339 .channels
= ad7266_channels_diff_u
,
340 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_u
),
341 .scan_masks
= ad7266_available_scan_masks_diff
,
343 [AD7266_CHAN_INFO_INDEX(1, 0, 1)] = {
344 .channels
= ad7266_channels_diff_fixed_u
,
345 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_fixed_u
),
346 .scan_masks
= ad7266_available_scan_masks_fixed
,
348 [AD7266_CHAN_INFO_INDEX(1, 1, 0)] = {
349 .channels
= ad7266_channels_diff_s
,
350 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_s
),
351 .scan_masks
= ad7266_available_scan_masks_diff
,
353 [AD7266_CHAN_INFO_INDEX(1, 1, 1)] = {
354 .channels
= ad7266_channels_diff_fixed_s
,
355 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_fixed_s
),
356 .scan_masks
= ad7266_available_scan_masks_fixed
,
360 static void ad7266_init_channels(struct iio_dev
*indio_dev
)
362 struct ad7266_state
*st
= iio_priv(indio_dev
);
363 bool is_differential
, is_signed
;
364 const struct ad7266_chan_info
*chan_info
;
367 is_differential
= st
->mode
!= AD7266_MODE_SINGLE_ENDED
;
368 is_signed
= (st
->range
== AD7266_RANGE_2VREF
) |
369 (st
->mode
== AD7266_MODE_DIFF
);
371 i
= AD7266_CHAN_INFO_INDEX(is_differential
, is_signed
, st
->fixed_addr
);
372 chan_info
= &ad7266_chan_infos
[i
];
374 indio_dev
->channels
= chan_info
->channels
;
375 indio_dev
->num_channels
= chan_info
->num_channels
;
376 indio_dev
->available_scan_masks
= chan_info
->scan_masks
;
377 indio_dev
->masklength
= chan_info
->num_channels
- 1;
380 static const char * const ad7266_gpio_labels
[] = {
384 static int ad7266_probe(struct spi_device
*spi
)
386 struct ad7266_platform_data
*pdata
= spi
->dev
.platform_data
;
387 struct iio_dev
*indio_dev
;
388 struct ad7266_state
*st
;
392 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
393 if (indio_dev
== NULL
)
396 st
= iio_priv(indio_dev
);
398 st
->reg
= devm_regulator_get_optional(&spi
->dev
, "vref");
399 if (!IS_ERR(st
->reg
)) {
400 ret
= regulator_enable(st
->reg
);
404 ret
= regulator_get_voltage(st
->reg
);
406 goto error_disable_reg
;
408 st
->vref_mv
= ret
/ 1000;
410 /* Any other error indicates that the regulator does exist */
411 if (PTR_ERR(st
->reg
) != -ENODEV
)
412 return PTR_ERR(st
->reg
);
413 /* Use internal reference */
418 st
->fixed_addr
= pdata
->fixed_addr
;
419 st
->mode
= pdata
->mode
;
420 st
->range
= pdata
->range
;
422 if (!st
->fixed_addr
) {
423 for (i
= 0; i
< ARRAY_SIZE(st
->gpios
); ++i
) {
424 st
->gpios
[i
].gpio
= pdata
->addr_gpios
[i
];
425 st
->gpios
[i
].flags
= GPIOF_OUT_INIT_LOW
;
426 st
->gpios
[i
].label
= ad7266_gpio_labels
[i
];
428 ret
= gpio_request_array(st
->gpios
,
429 ARRAY_SIZE(st
->gpios
));
431 goto error_disable_reg
;
434 st
->fixed_addr
= true;
435 st
->range
= AD7266_RANGE_VREF
;
436 st
->mode
= AD7266_MODE_DIFF
;
439 spi_set_drvdata(spi
, indio_dev
);
442 indio_dev
->dev
.parent
= &spi
->dev
;
443 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
444 indio_dev
->name
= spi_get_device_id(spi
)->name
;
445 indio_dev
->modes
= INDIO_DIRECT_MODE
;
446 indio_dev
->info
= &ad7266_info
;
448 ad7266_init_channels(indio_dev
);
451 st
->single_xfer
[0].rx_buf
= &st
->data
.sample
[0];
452 st
->single_xfer
[0].len
= 2;
453 st
->single_xfer
[0].cs_change
= 1;
455 st
->single_xfer
[1].rx_buf
= st
->data
.sample
;
456 st
->single_xfer
[1].len
= 4;
457 st
->single_xfer
[1].cs_change
= 1;
459 st
->single_xfer
[2].tx_buf
= &st
->data
.sample
[0];
460 st
->single_xfer
[2].len
= 1;
462 spi_message_init(&st
->single_msg
);
463 spi_message_add_tail(&st
->single_xfer
[0], &st
->single_msg
);
464 spi_message_add_tail(&st
->single_xfer
[1], &st
->single_msg
);
465 spi_message_add_tail(&st
->single_xfer
[2], &st
->single_msg
);
467 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
468 &ad7266_trigger_handler
, &iio_triggered_buffer_setup_ops
);
470 goto error_free_gpios
;
472 ret
= iio_device_register(indio_dev
);
474 goto error_buffer_cleanup
;
478 error_buffer_cleanup
:
479 iio_triggered_buffer_cleanup(indio_dev
);
482 gpio_free_array(st
->gpios
, ARRAY_SIZE(st
->gpios
));
484 if (!IS_ERR(st
->reg
))
485 regulator_disable(st
->reg
);
490 static int ad7266_remove(struct spi_device
*spi
)
492 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
493 struct ad7266_state
*st
= iio_priv(indio_dev
);
495 iio_device_unregister(indio_dev
);
496 iio_triggered_buffer_cleanup(indio_dev
);
498 gpio_free_array(st
->gpios
, ARRAY_SIZE(st
->gpios
));
499 if (!IS_ERR(st
->reg
))
500 regulator_disable(st
->reg
);
505 static const struct spi_device_id ad7266_id
[] = {
510 MODULE_DEVICE_TABLE(spi
, ad7266_id
);
512 static struct spi_driver ad7266_driver
= {
516 .probe
= ad7266_probe
,
517 .remove
= ad7266_remove
,
518 .id_table
= ad7266_id
,
520 module_spi_driver(ad7266_driver
);
522 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
523 MODULE_DESCRIPTION("Analog Devices AD7266/65 ADC");
524 MODULE_LICENSE("GPL v2");