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_uv
;
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).
46 uint8_t data
[ALIGN(4, sizeof(s64
)) + sizeof(s64
)] ____cacheline_aligned
;
49 static int ad7266_wakeup(struct ad7266_state
*st
)
51 /* Any read with >= 2 bytes will wake the device */
52 return spi_read(st
->spi
, st
->data
, 2);
55 static int ad7266_powerdown(struct ad7266_state
*st
)
57 /* Any read with < 2 bytes will powerdown the device */
58 return spi_read(st
->spi
, st
->data
, 1);
61 static int ad7266_preenable(struct iio_dev
*indio_dev
)
63 struct ad7266_state
*st
= iio_priv(indio_dev
);
66 ret
= ad7266_wakeup(st
);
70 ret
= iio_sw_buffer_preenable(indio_dev
);
77 static int ad7266_postdisable(struct iio_dev
*indio_dev
)
79 struct ad7266_state
*st
= iio_priv(indio_dev
);
80 return ad7266_powerdown(st
);
83 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops
= {
84 .preenable
= &ad7266_preenable
,
85 .postenable
= &iio_triggered_buffer_postenable
,
86 .predisable
= &iio_triggered_buffer_predisable
,
87 .postdisable
= &ad7266_postdisable
,
90 static irqreturn_t
ad7266_trigger_handler(int irq
, void *p
)
92 struct iio_poll_func
*pf
= p
;
93 struct iio_dev
*indio_dev
= pf
->indio_dev
;
94 struct ad7266_state
*st
= iio_priv(indio_dev
);
97 ret
= spi_read(st
->spi
, st
->data
, 4);
99 if (indio_dev
->scan_timestamp
)
100 ((s64
*)st
->data
)[1] = pf
->timestamp
;
101 iio_push_to_buffers(indio_dev
, (u8
*)st
->data
);
104 iio_trigger_notify_done(indio_dev
->trig
);
109 static void ad7266_select_input(struct ad7266_state
*st
, unsigned int nr
)
117 case AD7266_MODE_SINGLE_ENDED
:
120 case AD7266_MODE_PSEUDO_DIFF
:
123 case AD7266_MODE_DIFF
:
128 for (i
= 0; i
< 3; ++i
)
129 gpio_set_value(st
->gpios
[i
].gpio
, (bool)(nr
& BIT(i
)));
132 static int ad7266_update_scan_mode(struct iio_dev
*indio_dev
,
133 const unsigned long *scan_mask
)
135 struct ad7266_state
*st
= iio_priv(indio_dev
);
136 unsigned int nr
= find_first_bit(scan_mask
, indio_dev
->masklength
);
138 ad7266_select_input(st
, nr
);
143 static int ad7266_read_single(struct ad7266_state
*st
, int *val
,
144 unsigned int address
)
148 ad7266_select_input(st
, address
);
150 ret
= spi_sync(st
->spi
, &st
->single_msg
);
151 *val
= be16_to_cpu(st
->data
[address
% 2]);
156 static int ad7266_read_raw(struct iio_dev
*indio_dev
,
157 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long m
)
159 struct ad7266_state
*st
= iio_priv(indio_dev
);
160 unsigned long scale_uv
;
164 case IIO_CHAN_INFO_RAW
:
165 if (iio_buffer_enabled(indio_dev
))
168 ret
= ad7266_read_single(st
, val
, chan
->address
);
172 *val
= (*val
>> 2) & 0xfff;
173 if (chan
->scan_type
.sign
== 's')
174 *val
= sign_extend32(*val
, 11);
177 case IIO_CHAN_INFO_SCALE
:
178 scale_uv
= (st
->vref_uv
* 100);
179 if (st
->mode
== AD7266_MODE_DIFF
)
181 if (st
->range
== AD7266_RANGE_2VREF
)
184 scale_uv
>>= chan
->scan_type
.realbits
;
185 *val
= scale_uv
/ 100000;
186 *val2
= (scale_uv
% 100000) * 10;
187 return IIO_VAL_INT_PLUS_MICRO
;
188 case IIO_CHAN_INFO_OFFSET
:
189 if (st
->range
== AD7266_RANGE_2VREF
&&
190 st
->mode
!= AD7266_MODE_DIFF
)
199 #define AD7266_CHAN(_chan, _sign) { \
200 .type = IIO_VOLTAGE, \
202 .channel = (_chan), \
203 .address = (_chan), \
204 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
205 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
206 | BIT(IIO_CHAN_INFO_OFFSET), \
207 .scan_index = (_chan), \
213 .endianness = IIO_BE, \
217 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS(_name, _sign) \
218 const struct iio_chan_spec ad7266_channels_##_name[] = { \
219 AD7266_CHAN(0, (_sign)), \
220 AD7266_CHAN(1, (_sign)), \
221 AD7266_CHAN(2, (_sign)), \
222 AD7266_CHAN(3, (_sign)), \
223 AD7266_CHAN(4, (_sign)), \
224 AD7266_CHAN(5, (_sign)), \
225 AD7266_CHAN(6, (_sign)), \
226 AD7266_CHAN(7, (_sign)), \
227 AD7266_CHAN(8, (_sign)), \
228 AD7266_CHAN(9, (_sign)), \
229 AD7266_CHAN(10, (_sign)), \
230 AD7266_CHAN(11, (_sign)), \
231 IIO_CHAN_SOFT_TIMESTAMP(13), \
234 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(_name, _sign) \
235 const struct iio_chan_spec ad7266_channels_##_name##_fixed[] = { \
236 AD7266_CHAN(0, (_sign)), \
237 AD7266_CHAN(1, (_sign)), \
238 IIO_CHAN_SOFT_TIMESTAMP(2), \
241 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS(u
, 'u');
242 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS(s
, 's');
243 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(u
, 'u');
244 static AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(s
, 's');
246 #define AD7266_CHAN_DIFF(_chan, _sign) { \
247 .type = IIO_VOLTAGE, \
249 .channel = (_chan) * 2, \
250 .channel2 = (_chan) * 2 + 1, \
251 .address = (_chan), \
252 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
253 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
254 | BIT(IIO_CHAN_INFO_OFFSET), \
255 .scan_index = (_chan), \
261 .endianness = IIO_BE, \
266 #define AD7266_DECLARE_DIFF_CHANNELS(_name, _sign) \
267 const struct iio_chan_spec ad7266_channels_diff_##_name[] = { \
268 AD7266_CHAN_DIFF(0, (_sign)), \
269 AD7266_CHAN_DIFF(1, (_sign)), \
270 AD7266_CHAN_DIFF(2, (_sign)), \
271 AD7266_CHAN_DIFF(3, (_sign)), \
272 AD7266_CHAN_DIFF(4, (_sign)), \
273 AD7266_CHAN_DIFF(5, (_sign)), \
274 IIO_CHAN_SOFT_TIMESTAMP(6), \
277 static AD7266_DECLARE_DIFF_CHANNELS(s
, 's');
278 static AD7266_DECLARE_DIFF_CHANNELS(u
, 'u');
280 #define AD7266_DECLARE_DIFF_CHANNELS_FIXED(_name, _sign) \
281 const struct iio_chan_spec ad7266_channels_diff_fixed_##_name[] = { \
282 AD7266_CHAN_DIFF(0, (_sign)), \
283 AD7266_CHAN_DIFF(1, (_sign)), \
284 IIO_CHAN_SOFT_TIMESTAMP(2), \
287 static AD7266_DECLARE_DIFF_CHANNELS_FIXED(s
, 's');
288 static AD7266_DECLARE_DIFF_CHANNELS_FIXED(u
, 'u');
290 static const struct iio_info ad7266_info
= {
291 .read_raw
= &ad7266_read_raw
,
292 .update_scan_mode
= &ad7266_update_scan_mode
,
293 .driver_module
= THIS_MODULE
,
296 static unsigned long ad7266_available_scan_masks
[] = {
306 static unsigned long ad7266_available_scan_masks_diff
[] = {
313 static unsigned long ad7266_available_scan_masks_fixed
[] = {
318 struct ad7266_chan_info
{
319 const struct iio_chan_spec
*channels
;
320 unsigned int num_channels
;
321 unsigned long *scan_masks
;
324 #define AD7266_CHAN_INFO_INDEX(_differential, _signed, _fixed) \
325 (((_differential) << 2) | ((_signed) << 1) | ((_fixed) << 0))
327 static const struct ad7266_chan_info ad7266_chan_infos
[] = {
328 [AD7266_CHAN_INFO_INDEX(0, 0, 0)] = {
329 .channels
= ad7266_channels_u
,
330 .num_channels
= ARRAY_SIZE(ad7266_channels_u
),
331 .scan_masks
= ad7266_available_scan_masks
,
333 [AD7266_CHAN_INFO_INDEX(0, 0, 1)] = {
334 .channels
= ad7266_channels_u_fixed
,
335 .num_channels
= ARRAY_SIZE(ad7266_channels_u_fixed
),
336 .scan_masks
= ad7266_available_scan_masks_fixed
,
338 [AD7266_CHAN_INFO_INDEX(0, 1, 0)] = {
339 .channels
= ad7266_channels_s
,
340 .num_channels
= ARRAY_SIZE(ad7266_channels_s
),
341 .scan_masks
= ad7266_available_scan_masks
,
343 [AD7266_CHAN_INFO_INDEX(0, 1, 1)] = {
344 .channels
= ad7266_channels_s_fixed
,
345 .num_channels
= ARRAY_SIZE(ad7266_channels_s_fixed
),
346 .scan_masks
= ad7266_available_scan_masks_fixed
,
348 [AD7266_CHAN_INFO_INDEX(1, 0, 0)] = {
349 .channels
= ad7266_channels_diff_u
,
350 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_u
),
351 .scan_masks
= ad7266_available_scan_masks_diff
,
353 [AD7266_CHAN_INFO_INDEX(1, 0, 1)] = {
354 .channels
= ad7266_channels_diff_fixed_u
,
355 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_fixed_u
),
356 .scan_masks
= ad7266_available_scan_masks_fixed
,
358 [AD7266_CHAN_INFO_INDEX(1, 1, 0)] = {
359 .channels
= ad7266_channels_diff_s
,
360 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_s
),
361 .scan_masks
= ad7266_available_scan_masks_diff
,
363 [AD7266_CHAN_INFO_INDEX(1, 1, 1)] = {
364 .channels
= ad7266_channels_diff_fixed_s
,
365 .num_channels
= ARRAY_SIZE(ad7266_channels_diff_fixed_s
),
366 .scan_masks
= ad7266_available_scan_masks_fixed
,
370 static void ad7266_init_channels(struct iio_dev
*indio_dev
)
372 struct ad7266_state
*st
= iio_priv(indio_dev
);
373 bool is_differential
, is_signed
;
374 const struct ad7266_chan_info
*chan_info
;
377 is_differential
= st
->mode
!= AD7266_MODE_SINGLE_ENDED
;
378 is_signed
= (st
->range
== AD7266_RANGE_2VREF
) |
379 (st
->mode
== AD7266_MODE_DIFF
);
381 i
= AD7266_CHAN_INFO_INDEX(is_differential
, is_signed
, st
->fixed_addr
);
382 chan_info
= &ad7266_chan_infos
[i
];
384 indio_dev
->channels
= chan_info
->channels
;
385 indio_dev
->num_channels
= chan_info
->num_channels
;
386 indio_dev
->available_scan_masks
= chan_info
->scan_masks
;
387 indio_dev
->masklength
= chan_info
->num_channels
- 1;
390 static const char * const ad7266_gpio_labels
[] = {
394 static int ad7266_probe(struct spi_device
*spi
)
396 struct ad7266_platform_data
*pdata
= spi
->dev
.platform_data
;
397 struct iio_dev
*indio_dev
;
398 struct ad7266_state
*st
;
402 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
403 if (indio_dev
== NULL
)
406 st
= iio_priv(indio_dev
);
408 st
->reg
= devm_regulator_get(&spi
->dev
, "vref");
409 if (!IS_ERR_OR_NULL(st
->reg
)) {
410 ret
= regulator_enable(st
->reg
);
414 ret
= regulator_get_voltage(st
->reg
);
416 goto error_disable_reg
;
420 /* Use internal reference */
421 st
->vref_uv
= 2500000;
425 st
->fixed_addr
= pdata
->fixed_addr
;
426 st
->mode
= pdata
->mode
;
427 st
->range
= pdata
->range
;
429 if (!st
->fixed_addr
) {
430 for (i
= 0; i
< ARRAY_SIZE(st
->gpios
); ++i
) {
431 st
->gpios
[i
].gpio
= pdata
->addr_gpios
[i
];
432 st
->gpios
[i
].flags
= GPIOF_OUT_INIT_LOW
;
433 st
->gpios
[i
].label
= ad7266_gpio_labels
[i
];
435 ret
= gpio_request_array(st
->gpios
,
436 ARRAY_SIZE(st
->gpios
));
438 goto error_disable_reg
;
441 st
->fixed_addr
= true;
442 st
->range
= AD7266_RANGE_VREF
;
443 st
->mode
= AD7266_MODE_DIFF
;
446 spi_set_drvdata(spi
, indio_dev
);
449 indio_dev
->dev
.parent
= &spi
->dev
;
450 indio_dev
->name
= spi_get_device_id(spi
)->name
;
451 indio_dev
->modes
= INDIO_DIRECT_MODE
;
452 indio_dev
->info
= &ad7266_info
;
454 ad7266_init_channels(indio_dev
);
457 st
->single_xfer
[0].rx_buf
= &st
->data
;
458 st
->single_xfer
[0].len
= 2;
459 st
->single_xfer
[0].cs_change
= 1;
461 st
->single_xfer
[1].rx_buf
= &st
->data
;
462 st
->single_xfer
[1].len
= 4;
463 st
->single_xfer
[1].cs_change
= 1;
465 st
->single_xfer
[2].tx_buf
= &st
->data
;
466 st
->single_xfer
[2].len
= 1;
468 spi_message_init(&st
->single_msg
);
469 spi_message_add_tail(&st
->single_xfer
[0], &st
->single_msg
);
470 spi_message_add_tail(&st
->single_xfer
[1], &st
->single_msg
);
471 spi_message_add_tail(&st
->single_xfer
[2], &st
->single_msg
);
473 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
474 &ad7266_trigger_handler
, &iio_triggered_buffer_setup_ops
);
476 goto error_free_gpios
;
478 ret
= iio_device_register(indio_dev
);
480 goto error_buffer_cleanup
;
484 error_buffer_cleanup
:
485 iio_triggered_buffer_cleanup(indio_dev
);
488 gpio_free_array(st
->gpios
, ARRAY_SIZE(st
->gpios
));
490 if (!IS_ERR_OR_NULL(st
->reg
))
491 regulator_disable(st
->reg
);
496 static int ad7266_remove(struct spi_device
*spi
)
498 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
499 struct ad7266_state
*st
= iio_priv(indio_dev
);
501 iio_device_unregister(indio_dev
);
502 iio_triggered_buffer_cleanup(indio_dev
);
504 gpio_free_array(st
->gpios
, ARRAY_SIZE(st
->gpios
));
505 if (!IS_ERR_OR_NULL(st
->reg
))
506 regulator_disable(st
->reg
);
511 static const struct spi_device_id ad7266_id
[] = {
516 MODULE_DEVICE_TABLE(spi
, ad7266_id
);
518 static struct spi_driver ad7266_driver
= {
521 .owner
= THIS_MODULE
,
523 .probe
= ad7266_probe
,
524 .remove
= ad7266_remove
,
525 .id_table
= ad7266_id
,
527 module_spi_driver(ad7266_driver
);
529 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
530 MODULE_DESCRIPTION("Analog Devices AD7266/65 ADC");
531 MODULE_LICENSE("GPL v2");