1 // SPDX-License-Identifier: GPL-2.0
3 * AD7606 SPI ADC driver
5 * Copyright 2011 Analog Devices Inc.
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/property.h>
16 #include <linux/pwm.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/sysfs.h>
21 #include <linux/units.h>
22 #include <linux/util_macros.h>
24 #include <linux/iio/backend.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/trigger_consumer.h>
35 * Scales are computed as 5000/32768 and 10000/32768 respectively,
36 * so that when applied to the raw values they provide mV values.
37 * The scale arrays are kept as IIO_VAL_INT_PLUS_MICRO, so index
38 * X is the integer part and X + 1 is the fractional part.
40 static const unsigned int ad7606_16bit_hw_scale_avail
[2][2] = {
41 { 0, 152588 }, { 0, 305176 }
44 static const unsigned int ad7606_18bit_hw_scale_avail
[2][2] = {
45 { 0, 38147 }, { 0, 76294 }
48 static const unsigned int ad7606c_16bit_single_ended_unipolar_scale_avail
[3][2] = {
49 { 0, 76294 }, { 0, 152588 }, { 0, 190735 }
52 static const unsigned int ad7606c_16bit_single_ended_bipolar_scale_avail
[5][2] = {
53 { 0, 76294 }, { 0, 152588 }, { 0, 190735 }, { 0, 305176 }, { 0, 381470 }
56 static const unsigned int ad7606c_16bit_differential_bipolar_scale_avail
[4][2] = {
57 { 0, 152588 }, { 0, 305176 }, { 0, 381470 }, { 0, 610352 }
60 static const unsigned int ad7606c_18bit_single_ended_unipolar_scale_avail
[3][2] = {
61 { 0, 19073 }, { 0, 38147 }, { 0, 47684 }
64 static const unsigned int ad7606c_18bit_single_ended_bipolar_scale_avail
[5][2] = {
65 { 0, 19073 }, { 0, 38147 }, { 0, 47684 }, { 0, 76294 }, { 0, 95367 }
68 static const unsigned int ad7606c_18bit_differential_bipolar_scale_avail
[4][2] = {
69 { 0, 38147 }, { 0, 76294 }, { 0, 95367 }, { 0, 152588 }
72 static const unsigned int ad7606_16bit_sw_scale_avail
[3][2] = {
73 { 0, 76293 }, { 0, 152588 }, { 0, 305176 }
76 static const unsigned int ad7607_hw_scale_avail
[2][2] = {
77 { 0, 610352 }, { 1, 220703 }
80 static const unsigned int ad7609_hw_scale_avail
[2][2] = {
81 { 0, 152588 }, { 0, 305176 }
84 static const unsigned int ad7606_oversampling_avail
[7] = {
85 1, 2, 4, 8, 16, 32, 64,
88 static const unsigned int ad7616_oversampling_avail
[8] = {
89 1, 2, 4, 8, 16, 32, 64, 128,
92 static const struct iio_chan_spec ad7605_channels
[] = {
93 IIO_CHAN_SOFT_TIMESTAMP(4),
100 static const struct iio_chan_spec ad7606_channels_16bit
[] = {
101 IIO_CHAN_SOFT_TIMESTAMP(8),
102 AD7606_CHANNEL(0, 16),
103 AD7606_CHANNEL(1, 16),
104 AD7606_CHANNEL(2, 16),
105 AD7606_CHANNEL(3, 16),
106 AD7606_CHANNEL(4, 16),
107 AD7606_CHANNEL(5, 16),
108 AD7606_CHANNEL(6, 16),
109 AD7606_CHANNEL(7, 16),
112 static const struct iio_chan_spec ad7606_channels_18bit
[] = {
113 IIO_CHAN_SOFT_TIMESTAMP(8),
114 AD7606_CHANNEL(0, 18),
115 AD7606_CHANNEL(1, 18),
116 AD7606_CHANNEL(2, 18),
117 AD7606_CHANNEL(3, 18),
118 AD7606_CHANNEL(4, 18),
119 AD7606_CHANNEL(5, 18),
120 AD7606_CHANNEL(6, 18),
121 AD7606_CHANNEL(7, 18),
124 static const struct iio_chan_spec ad7607_channels
[] = {
125 IIO_CHAN_SOFT_TIMESTAMP(8),
126 AD7606_CHANNEL(0, 14),
127 AD7606_CHANNEL(1, 14),
128 AD7606_CHANNEL(2, 14),
129 AD7606_CHANNEL(3, 14),
130 AD7606_CHANNEL(4, 14),
131 AD7606_CHANNEL(5, 14),
132 AD7606_CHANNEL(6, 14),
133 AD7606_CHANNEL(7, 14),
136 static const struct iio_chan_spec ad7608_channels
[] = {
137 IIO_CHAN_SOFT_TIMESTAMP(8),
138 AD7606_CHANNEL(0, 18),
139 AD7606_CHANNEL(1, 18),
140 AD7606_CHANNEL(2, 18),
141 AD7606_CHANNEL(3, 18),
142 AD7606_CHANNEL(4, 18),
143 AD7606_CHANNEL(5, 18),
144 AD7606_CHANNEL(6, 18),
145 AD7606_CHANNEL(7, 18),
149 * The current assumption that this driver makes for AD7616, is that it's
150 * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
151 * To activate them, following pins must be pulled high:
154 * And following pins must be pulled low:
158 static const struct iio_chan_spec ad7616_channels
[] = {
159 IIO_CHAN_SOFT_TIMESTAMP(16),
160 AD7606_CHANNEL(0, 16),
161 AD7606_CHANNEL(1, 16),
162 AD7606_CHANNEL(2, 16),
163 AD7606_CHANNEL(3, 16),
164 AD7606_CHANNEL(4, 16),
165 AD7606_CHANNEL(5, 16),
166 AD7606_CHANNEL(6, 16),
167 AD7606_CHANNEL(7, 16),
168 AD7606_CHANNEL(8, 16),
169 AD7606_CHANNEL(9, 16),
170 AD7606_CHANNEL(10, 16),
171 AD7606_CHANNEL(11, 16),
172 AD7606_CHANNEL(12, 16),
173 AD7606_CHANNEL(13, 16),
174 AD7606_CHANNEL(14, 16),
175 AD7606_CHANNEL(15, 16),
178 static int ad7606c_18bit_chan_scale_setup(struct ad7606_state
*st
,
179 struct iio_chan_spec
*chan
, int ch
);
180 static int ad7606c_16bit_chan_scale_setup(struct ad7606_state
*st
,
181 struct iio_chan_spec
*chan
, int ch
);
182 static int ad7606_16bit_chan_scale_setup(struct ad7606_state
*st
,
183 struct iio_chan_spec
*chan
, int ch
);
184 static int ad7607_chan_scale_setup(struct ad7606_state
*st
,
185 struct iio_chan_spec
*chan
, int ch
);
186 static int ad7608_chan_scale_setup(struct ad7606_state
*st
,
187 struct iio_chan_spec
*chan
, int ch
);
188 static int ad7609_chan_scale_setup(struct ad7606_state
*st
,
189 struct iio_chan_spec
*chan
, int ch
);
191 const struct ad7606_chip_info ad7605_4_info
= {
192 .channels
= ad7605_channels
,
194 .num_adc_channels
= 4,
196 .scale_setup_cb
= ad7606_16bit_chan_scale_setup
,
198 EXPORT_SYMBOL_NS_GPL(ad7605_4_info
, "IIO_AD7606");
200 const struct ad7606_chip_info ad7606_8_info
= {
201 .channels
= ad7606_channels_16bit
,
203 .num_adc_channels
= 8,
205 .oversampling_avail
= ad7606_oversampling_avail
,
206 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
207 .scale_setup_cb
= ad7606_16bit_chan_scale_setup
,
209 EXPORT_SYMBOL_NS_GPL(ad7606_8_info
, "IIO_AD7606");
211 const struct ad7606_chip_info ad7606_6_info
= {
212 .channels
= ad7606_channels_16bit
,
214 .num_adc_channels
= 6,
216 .oversampling_avail
= ad7606_oversampling_avail
,
217 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
218 .scale_setup_cb
= ad7606_16bit_chan_scale_setup
,
220 EXPORT_SYMBOL_NS_GPL(ad7606_6_info
, "IIO_AD7606");
222 const struct ad7606_chip_info ad7606_4_info
= {
223 .channels
= ad7606_channels_16bit
,
225 .num_adc_channels
= 4,
227 .oversampling_avail
= ad7606_oversampling_avail
,
228 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
229 .scale_setup_cb
= ad7606_16bit_chan_scale_setup
,
231 EXPORT_SYMBOL_NS_GPL(ad7606_4_info
, "IIO_AD7606");
233 const struct ad7606_chip_info ad7606b_info
= {
234 .channels
= ad7606_channels_16bit
,
235 .max_samplerate
= 800 * KILO
,
237 .num_adc_channels
= 8,
239 .oversampling_avail
= ad7606_oversampling_avail
,
240 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
241 .scale_setup_cb
= ad7606_16bit_chan_scale_setup
,
243 EXPORT_SYMBOL_NS_GPL(ad7606b_info
, "IIO_AD7606");
245 const struct ad7606_chip_info ad7606c_16_info
= {
246 .channels
= ad7606_channels_16bit
,
248 .num_adc_channels
= 8,
250 .oversampling_avail
= ad7606_oversampling_avail
,
251 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
252 .scale_setup_cb
= ad7606c_16bit_chan_scale_setup
,
254 EXPORT_SYMBOL_NS_GPL(ad7606c_16_info
, "IIO_AD7606");
256 const struct ad7606_chip_info ad7607_info
= {
257 .channels
= ad7607_channels
,
259 .num_adc_channels
= 8,
261 .oversampling_avail
= ad7606_oversampling_avail
,
262 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
263 .scale_setup_cb
= ad7607_chan_scale_setup
,
265 EXPORT_SYMBOL_NS_GPL(ad7607_info
, "IIO_AD7606");
267 const struct ad7606_chip_info ad7608_info
= {
268 .channels
= ad7608_channels
,
270 .num_adc_channels
= 8,
272 .oversampling_avail
= ad7606_oversampling_avail
,
273 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
274 .scale_setup_cb
= ad7608_chan_scale_setup
,
276 EXPORT_SYMBOL_NS_GPL(ad7608_info
, "IIO_AD7606");
278 const struct ad7606_chip_info ad7609_info
= {
279 .channels
= ad7608_channels
,
281 .num_adc_channels
= 8,
283 .oversampling_avail
= ad7606_oversampling_avail
,
284 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
285 .scale_setup_cb
= ad7609_chan_scale_setup
,
287 EXPORT_SYMBOL_NS_GPL(ad7609_info
, "IIO_AD7606");
289 const struct ad7606_chip_info ad7606c_18_info
= {
290 .channels
= ad7606_channels_18bit
,
292 .num_adc_channels
= 8,
294 .oversampling_avail
= ad7606_oversampling_avail
,
295 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
296 .scale_setup_cb
= ad7606c_18bit_chan_scale_setup
,
298 EXPORT_SYMBOL_NS_GPL(ad7606c_18_info
, "IIO_AD7606");
300 const struct ad7606_chip_info ad7616_info
= {
301 .channels
= ad7616_channels
,
304 .num_adc_channels
= 16,
306 .oversampling_avail
= ad7616_oversampling_avail
,
307 .oversampling_num
= ARRAY_SIZE(ad7616_oversampling_avail
),
308 .os_req_reset
= true,
309 .scale_setup_cb
= ad7606_16bit_chan_scale_setup
,
311 EXPORT_SYMBOL_NS_GPL(ad7616_info
, "IIO_AD7606");
313 int ad7606_reset(struct ad7606_state
*st
)
315 if (st
->gpio_reset
) {
316 gpiod_set_value(st
->gpio_reset
, 1);
317 ndelay(100); /* t_reset >= 100ns */
318 gpiod_set_value(st
->gpio_reset
, 0);
324 EXPORT_SYMBOL_NS_GPL(ad7606_reset
, "IIO_AD7606");
326 static int ad7606_16bit_chan_scale_setup(struct ad7606_state
*st
,
327 struct iio_chan_spec
*chan
, int ch
)
329 struct ad7606_chan_scale
*cs
= &st
->chan_scales
[ch
];
331 if (!st
->sw_mode_en
) {
332 /* tied to logic low, analog input range is +/- 5V */
334 cs
->scale_avail
= ad7606_16bit_hw_scale_avail
;
335 cs
->num_scales
= ARRAY_SIZE(ad7606_16bit_hw_scale_avail
);
339 /* Scale of 0.076293 is only available in sw mode */
340 /* After reset, in software mode, ±10 V is set by default */
342 cs
->scale_avail
= ad7606_16bit_sw_scale_avail
;
343 cs
->num_scales
= ARRAY_SIZE(ad7606_16bit_sw_scale_avail
);
348 static int ad7606_get_chan_config(struct ad7606_state
*st
, int ch
,
349 bool *bipolar
, bool *differential
)
351 unsigned int num_channels
= st
->chip_info
->num_channels
- 1;
352 struct device
*dev
= st
->dev
;
356 *differential
= false;
358 device_for_each_child_node_scoped(dev
, child
) {
362 ret
= fwnode_property_read_u32(child
, "reg", ®
);
366 /* channel number (here) is from 1 to num_channels */
367 if (reg
== 0 || reg
> num_channels
) {
369 "Invalid channel number (ignoring): %d\n", reg
);
376 *bipolar
= fwnode_property_read_bool(child
, "bipolar");
378 ret
= fwnode_property_read_u32_array(child
, "diff-channels",
379 pins
, ARRAY_SIZE(pins
));
380 /* Channel is differential, if pins are the same as 'reg' */
381 if (ret
== 0 && (pins
[0] != reg
|| pins
[1] != reg
)) {
383 "Differential pins must be the same as 'reg'");
387 *differential
= (ret
== 0);
389 if (*differential
&& !*bipolar
) {
391 "'bipolar' must be added for diff channel %d\n",
402 static int ad7606c_18bit_chan_scale_setup(struct ad7606_state
*st
,
403 struct iio_chan_spec
*chan
, int ch
)
405 struct ad7606_chan_scale
*cs
= &st
->chan_scales
[ch
];
406 bool bipolar
, differential
;
409 if (!st
->sw_mode_en
) {
411 cs
->scale_avail
= ad7606_18bit_hw_scale_avail
;
412 cs
->num_scales
= ARRAY_SIZE(ad7606_18bit_hw_scale_avail
);
416 ret
= ad7606_get_chan_config(st
, ch
, &bipolar
, &differential
);
421 cs
->scale_avail
= ad7606c_18bit_differential_bipolar_scale_avail
;
423 ARRAY_SIZE(ad7606c_18bit_differential_bipolar_scale_avail
);
424 /* Bipolar differential ranges start at 8 (b1000) */
427 chan
->differential
= 1;
428 chan
->channel2
= chan
->channel
;
433 chan
->differential
= 0;
436 cs
->scale_avail
= ad7606c_18bit_single_ended_bipolar_scale_avail
;
438 ARRAY_SIZE(ad7606c_18bit_single_ended_bipolar_scale_avail
);
439 /* Bipolar single-ended ranges start at 0 (b0000) */
442 chan
->scan_type
.sign
= 's';
447 cs
->scale_avail
= ad7606c_18bit_single_ended_unipolar_scale_avail
;
449 ARRAY_SIZE(ad7606c_18bit_single_ended_unipolar_scale_avail
);
450 /* Unipolar single-ended ranges start at 5 (b0101) */
453 chan
->scan_type
.sign
= 'u';
458 static int ad7606c_16bit_chan_scale_setup(struct ad7606_state
*st
,
459 struct iio_chan_spec
*chan
, int ch
)
461 struct ad7606_chan_scale
*cs
= &st
->chan_scales
[ch
];
462 bool bipolar
, differential
;
465 if (!st
->sw_mode_en
) {
467 cs
->scale_avail
= ad7606_16bit_hw_scale_avail
;
468 cs
->num_scales
= ARRAY_SIZE(ad7606_16bit_hw_scale_avail
);
472 ret
= ad7606_get_chan_config(st
, ch
, &bipolar
, &differential
);
477 cs
->scale_avail
= ad7606c_16bit_differential_bipolar_scale_avail
;
479 ARRAY_SIZE(ad7606c_16bit_differential_bipolar_scale_avail
);
480 /* Bipolar differential ranges start at 8 (b1000) */
483 chan
->differential
= 1;
484 chan
->channel2
= chan
->channel
;
485 chan
->scan_type
.sign
= 's';
490 chan
->differential
= 0;
493 cs
->scale_avail
= ad7606c_16bit_single_ended_bipolar_scale_avail
;
495 ARRAY_SIZE(ad7606c_16bit_single_ended_bipolar_scale_avail
);
496 /* Bipolar single-ended ranges start at 0 (b0000) */
499 chan
->scan_type
.sign
= 's';
504 cs
->scale_avail
= ad7606c_16bit_single_ended_unipolar_scale_avail
;
506 ARRAY_SIZE(ad7606c_16bit_single_ended_unipolar_scale_avail
);
507 /* Unipolar single-ended ranges start at 5 (b0101) */
510 chan
->scan_type
.sign
= 'u';
515 static int ad7607_chan_scale_setup(struct ad7606_state
*st
,
516 struct iio_chan_spec
*chan
, int ch
)
518 struct ad7606_chan_scale
*cs
= &st
->chan_scales
[ch
];
521 cs
->scale_avail
= ad7607_hw_scale_avail
;
522 cs
->num_scales
= ARRAY_SIZE(ad7607_hw_scale_avail
);
526 static int ad7608_chan_scale_setup(struct ad7606_state
*st
,
527 struct iio_chan_spec
*chan
, int ch
)
529 struct ad7606_chan_scale
*cs
= &st
->chan_scales
[ch
];
532 cs
->scale_avail
= ad7606_18bit_hw_scale_avail
;
533 cs
->num_scales
= ARRAY_SIZE(ad7606_18bit_hw_scale_avail
);
537 static int ad7609_chan_scale_setup(struct ad7606_state
*st
,
538 struct iio_chan_spec
*chan
, int ch
)
540 struct ad7606_chan_scale
*cs
= &st
->chan_scales
[ch
];
543 cs
->scale_avail
= ad7609_hw_scale_avail
;
544 cs
->num_scales
= ARRAY_SIZE(ad7609_hw_scale_avail
);
548 static int ad7606_reg_access(struct iio_dev
*indio_dev
,
550 unsigned int writeval
,
551 unsigned int *readval
)
553 struct ad7606_state
*st
= iio_priv(indio_dev
);
556 guard(mutex
)(&st
->lock
);
559 ret
= st
->bops
->reg_read(st
, reg
);
565 return st
->bops
->reg_write(st
, reg
, writeval
);
569 static int ad7606_pwm_set_high(struct ad7606_state
*st
)
571 struct pwm_state cnvst_pwm_state
;
574 pwm_get_state(st
->cnvst_pwm
, &cnvst_pwm_state
);
575 cnvst_pwm_state
.enabled
= true;
576 cnvst_pwm_state
.duty_cycle
= cnvst_pwm_state
.period
;
578 ret
= pwm_apply_might_sleep(st
->cnvst_pwm
, &cnvst_pwm_state
);
583 static int ad7606_pwm_set_low(struct ad7606_state
*st
)
585 struct pwm_state cnvst_pwm_state
;
588 pwm_get_state(st
->cnvst_pwm
, &cnvst_pwm_state
);
589 cnvst_pwm_state
.enabled
= true;
590 cnvst_pwm_state
.duty_cycle
= 0;
592 ret
= pwm_apply_might_sleep(st
->cnvst_pwm
, &cnvst_pwm_state
);
597 static int ad7606_pwm_set_swing(struct ad7606_state
*st
)
599 struct pwm_state cnvst_pwm_state
;
601 pwm_get_state(st
->cnvst_pwm
, &cnvst_pwm_state
);
602 cnvst_pwm_state
.enabled
= true;
603 cnvst_pwm_state
.duty_cycle
= cnvst_pwm_state
.period
/ 2;
605 return pwm_apply_might_sleep(st
->cnvst_pwm
, &cnvst_pwm_state
);
608 static bool ad7606_pwm_is_swinging(struct ad7606_state
*st
)
610 struct pwm_state cnvst_pwm_state
;
612 pwm_get_state(st
->cnvst_pwm
, &cnvst_pwm_state
);
614 return cnvst_pwm_state
.duty_cycle
!= cnvst_pwm_state
.period
&&
615 cnvst_pwm_state
.duty_cycle
!= 0;
618 static int ad7606_set_sampling_freq(struct ad7606_state
*st
, unsigned long freq
)
620 struct pwm_state cnvst_pwm_state
;
621 bool is_swinging
= ad7606_pwm_is_swinging(st
);
627 /* Retrieve the previous state. */
628 pwm_get_state(st
->cnvst_pwm
, &cnvst_pwm_state
);
629 is_high
= cnvst_pwm_state
.duty_cycle
== cnvst_pwm_state
.period
;
631 cnvst_pwm_state
.period
= DIV_ROUND_UP_ULL(NSEC_PER_SEC
, freq
);
632 cnvst_pwm_state
.polarity
= PWM_POLARITY_NORMAL
;
634 cnvst_pwm_state
.duty_cycle
= cnvst_pwm_state
.period
;
635 else if (is_swinging
)
636 cnvst_pwm_state
.duty_cycle
= cnvst_pwm_state
.period
/ 2;
638 cnvst_pwm_state
.duty_cycle
= 0;
640 return pwm_apply_might_sleep(st
->cnvst_pwm
, &cnvst_pwm_state
);
643 static int ad7606_read_samples(struct ad7606_state
*st
)
645 unsigned int num
= st
->chip_info
->num_adc_channels
;
647 return st
->bops
->read_block(st
->dev
, num
, &st
->data
);
650 static irqreturn_t
ad7606_trigger_handler(int irq
, void *p
)
652 struct iio_poll_func
*pf
= p
;
653 struct iio_dev
*indio_dev
= pf
->indio_dev
;
654 struct ad7606_state
*st
= iio_priv(indio_dev
);
657 guard(mutex
)(&st
->lock
);
659 ret
= ad7606_read_samples(st
);
663 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->data
,
664 iio_get_time_ns(indio_dev
));
666 iio_trigger_notify_done(indio_dev
->trig
);
667 /* The rising edge of the CONVST signal starts a new conversion. */
668 gpiod_set_value(st
->gpio_convst
, 1);
673 static int ad7606_scan_direct(struct iio_dev
*indio_dev
, unsigned int ch
,
676 struct ad7606_state
*st
= iio_priv(indio_dev
);
677 unsigned int realbits
= st
->chip_info
->channels
[1].scan_type
.realbits
;
678 const struct iio_chan_spec
*chan
;
681 if (st
->gpio_convst
) {
682 gpiod_set_value(st
->gpio_convst
, 1);
684 ret
= ad7606_pwm_set_high(st
);
690 * If no backend, wait for the interruption on busy pin, otherwise just add
691 * a delay to leave time for the data to be available. For now, the latter
692 * will not happen because IIO_CHAN_INFO_RAW is not supported for the backend.
693 * TODO: Add support for reading a single value when the backend is used.
696 ret
= wait_for_completion_timeout(&st
->completion
,
697 msecs_to_jiffies(1000));
706 ret
= ad7606_read_samples(st
);
710 chan
= &indio_dev
->channels
[ch
+ 1];
711 if (chan
->scan_type
.sign
== 'u') {
713 *val
= st
->data
.buf32
[ch
];
715 *val
= st
->data
.buf16
[ch
];
718 *val
= sign_extend32(st
->data
.buf32
[ch
], realbits
- 1);
720 *val
= sign_extend32(st
->data
.buf16
[ch
], realbits
- 1);
724 if (!st
->gpio_convst
) {
725 ret
= ad7606_pwm_set_low(st
);
729 gpiod_set_value(st
->gpio_convst
, 0);
734 static int ad7606_read_raw(struct iio_dev
*indio_dev
,
735 struct iio_chan_spec
const *chan
,
741 struct ad7606_state
*st
= iio_priv(indio_dev
);
742 struct ad7606_chan_scale
*cs
;
743 struct pwm_state cnvst_pwm_state
;
746 case IIO_CHAN_INFO_RAW
:
747 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
) {
748 ret
= ad7606_scan_direct(indio_dev
, chan
->address
, val
);
754 case IIO_CHAN_INFO_SCALE
:
757 cs
= &st
->chan_scales
[ch
];
758 *val
= cs
->scale_avail
[cs
->range
][0];
759 *val2
= cs
->scale_avail
[cs
->range
][1];
760 return IIO_VAL_INT_PLUS_MICRO
;
761 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
762 *val
= st
->oversampling
;
764 case IIO_CHAN_INFO_SAMP_FREQ
:
766 * TODO: return the real frequency intead of the requested one once
767 * pwm_get_state_hw comes upstream.
769 pwm_get_state(st
->cnvst_pwm
, &cnvst_pwm_state
);
770 *val
= DIV_ROUND_CLOSEST_ULL(NSEC_PER_SEC
, cnvst_pwm_state
.period
);
776 static ssize_t
in_voltage_scale_available_show(struct device
*dev
,
777 struct device_attribute
*attr
,
780 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
781 struct ad7606_state
*st
= iio_priv(indio_dev
);
782 struct ad7606_chan_scale
*cs
= &st
->chan_scales
[0];
783 const unsigned int (*vals
)[2] = cs
->scale_avail
;
787 for (i
= 0; i
< cs
->num_scales
; i
++)
788 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%u.%06u ",
789 vals
[i
][0], vals
[i
][1]);
795 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available
, 0);
797 static int ad7606_write_scale_hw(struct iio_dev
*indio_dev
, int ch
, int val
)
799 struct ad7606_state
*st
= iio_priv(indio_dev
);
801 gpiod_set_value(st
->gpio_range
, val
);
806 static int ad7606_write_os_hw(struct iio_dev
*indio_dev
, int val
)
808 struct ad7606_state
*st
= iio_priv(indio_dev
);
809 DECLARE_BITMAP(values
, 3);
811 values
[0] = val
& GENMASK(2, 0);
813 gpiod_set_array_value(st
->gpio_os
->ndescs
, st
->gpio_os
->desc
,
814 st
->gpio_os
->info
, values
);
816 /* AD7616 requires a reset to update value */
817 if (st
->chip_info
->os_req_reset
)
823 static int ad7606_write_raw(struct iio_dev
*indio_dev
,
824 struct iio_chan_spec
const *chan
,
829 struct ad7606_state
*st
= iio_priv(indio_dev
);
830 unsigned int scale_avail_uv
[AD760X_MAX_SCALES
];
831 struct ad7606_chan_scale
*cs
;
834 guard(mutex
)(&st
->lock
);
837 case IIO_CHAN_INFO_SCALE
:
840 cs
= &st
->chan_scales
[ch
];
841 for (i
= 0; i
< cs
->num_scales
; i
++) {
842 scale_avail_uv
[i
] = cs
->scale_avail
[i
][0] * MICRO
+
843 cs
->scale_avail
[i
][1];
845 val
= (val
* MICRO
) + val2
;
846 i
= find_closest(val
, scale_avail_uv
, cs
->num_scales
);
847 ret
= st
->write_scale(indio_dev
, ch
, i
+ cs
->reg_offset
);
853 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
856 i
= find_closest(val
, st
->oversampling_avail
,
858 ret
= st
->write_os(indio_dev
, i
);
861 st
->oversampling
= st
->oversampling_avail
[i
];
864 case IIO_CHAN_INFO_SAMP_FREQ
:
865 if (val
< 0 && val2
!= 0)
867 return ad7606_set_sampling_freq(st
, val
);
873 static ssize_t
ad7606_oversampling_ratio_avail(struct device
*dev
,
874 struct device_attribute
*attr
,
877 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
878 struct ad7606_state
*st
= iio_priv(indio_dev
);
879 const unsigned int *vals
= st
->oversampling_avail
;
883 for (i
= 0; i
< st
->num_os_ratios
; i
++)
884 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%u ", vals
[i
]);
890 static IIO_DEVICE_ATTR(oversampling_ratio_available
, 0444,
891 ad7606_oversampling_ratio_avail
, NULL
, 0);
893 static struct attribute
*ad7606_attributes_os_and_range
[] = {
894 &iio_dev_attr_in_voltage_scale_available
.dev_attr
.attr
,
895 &iio_dev_attr_oversampling_ratio_available
.dev_attr
.attr
,
899 static const struct attribute_group ad7606_attribute_group_os_and_range
= {
900 .attrs
= ad7606_attributes_os_and_range
,
903 static struct attribute
*ad7606_attributes_os
[] = {
904 &iio_dev_attr_oversampling_ratio_available
.dev_attr
.attr
,
908 static const struct attribute_group ad7606_attribute_group_os
= {
909 .attrs
= ad7606_attributes_os
,
912 static struct attribute
*ad7606_attributes_range
[] = {
913 &iio_dev_attr_in_voltage_scale_available
.dev_attr
.attr
,
917 static const struct attribute_group ad7606_attribute_group_range
= {
918 .attrs
= ad7606_attributes_range
,
921 static int ad7606_request_gpios(struct ad7606_state
*st
)
923 struct device
*dev
= st
->dev
;
925 st
->gpio_convst
= devm_gpiod_get_optional(dev
, "adi,conversion-start",
928 if (IS_ERR(st
->gpio_convst
))
929 return PTR_ERR(st
->gpio_convst
);
931 st
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
932 if (IS_ERR(st
->gpio_reset
))
933 return PTR_ERR(st
->gpio_reset
);
935 st
->gpio_range
= devm_gpiod_get_optional(dev
, "adi,range",
937 if (IS_ERR(st
->gpio_range
))
938 return PTR_ERR(st
->gpio_range
);
940 st
->gpio_standby
= devm_gpiod_get_optional(dev
, "standby",
942 if (IS_ERR(st
->gpio_standby
))
943 return PTR_ERR(st
->gpio_standby
);
945 st
->gpio_frstdata
= devm_gpiod_get_optional(dev
, "adi,first-data",
947 if (IS_ERR(st
->gpio_frstdata
))
948 return PTR_ERR(st
->gpio_frstdata
);
950 if (!st
->chip_info
->oversampling_num
)
953 st
->gpio_os
= devm_gpiod_get_array_optional(dev
,
954 "adi,oversampling-ratio",
956 return PTR_ERR_OR_ZERO(st
->gpio_os
);
960 * The BUSY signal indicates when conversions are in progress, so when a rising
961 * edge of CONVST is applied, BUSY goes logic high and transitions low at the
962 * end of the entire conversion process. The falling edge of the BUSY signal
963 * triggers this interrupt.
965 static irqreturn_t
ad7606_interrupt(int irq
, void *dev_id
)
967 struct iio_dev
*indio_dev
= dev_id
;
968 struct ad7606_state
*st
= iio_priv(indio_dev
);
971 if (iio_buffer_enabled(indio_dev
)) {
972 if (st
->gpio_convst
) {
973 gpiod_set_value(st
->gpio_convst
, 0);
975 ret
= ad7606_pwm_set_low(st
);
977 dev_err(st
->dev
, "PWM set low failed");
981 iio_trigger_poll_nested(st
->trig
);
983 complete(&st
->completion
);
990 static int ad7606_validate_trigger(struct iio_dev
*indio_dev
,
991 struct iio_trigger
*trig
)
993 struct ad7606_state
*st
= iio_priv(indio_dev
);
995 if (st
->trig
!= trig
)
1001 static int ad7606_buffer_postenable(struct iio_dev
*indio_dev
)
1003 struct ad7606_state
*st
= iio_priv(indio_dev
);
1005 gpiod_set_value(st
->gpio_convst
, 1);
1010 static int ad7606_buffer_predisable(struct iio_dev
*indio_dev
)
1012 struct ad7606_state
*st
= iio_priv(indio_dev
);
1014 gpiod_set_value(st
->gpio_convst
, 0);
1019 static int ad7606_read_avail(struct iio_dev
*indio_dev
,
1020 struct iio_chan_spec
const *chan
,
1021 const int **vals
, int *type
, int *length
,
1024 struct ad7606_state
*st
= iio_priv(indio_dev
);
1025 struct ad7606_chan_scale
*cs
;
1026 unsigned int ch
= 0;
1029 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1030 *vals
= st
->oversampling_avail
;
1031 *length
= st
->num_os_ratios
;
1032 *type
= IIO_VAL_INT
;
1034 return IIO_AVAIL_LIST
;
1036 case IIO_CHAN_INFO_SCALE
:
1040 cs
= &st
->chan_scales
[ch
];
1041 *vals
= (int *)cs
->scale_avail
;
1042 *length
= cs
->num_scales
;
1043 *type
= IIO_VAL_INT_PLUS_MICRO
;
1045 return IIO_AVAIL_LIST
;
1050 static int ad7606_backend_buffer_postenable(struct iio_dev
*indio_dev
)
1052 struct ad7606_state
*st
= iio_priv(indio_dev
);
1054 return ad7606_pwm_set_swing(st
);
1057 static int ad7606_backend_buffer_predisable(struct iio_dev
*indio_dev
)
1059 struct ad7606_state
*st
= iio_priv(indio_dev
);
1061 return ad7606_pwm_set_low(st
);
1064 static int ad7606_update_scan_mode(struct iio_dev
*indio_dev
,
1065 const unsigned long *scan_mask
)
1067 struct ad7606_state
*st
= iio_priv(indio_dev
);
1070 * The update scan mode is only for iio backend compatible drivers.
1071 * If the specific update_scan_mode is not defined in the bus ops,
1072 * just do nothing and return 0.
1074 if (!st
->bops
->update_scan_mode
)
1077 return st
->bops
->update_scan_mode(indio_dev
, scan_mask
);
1080 static const struct iio_buffer_setup_ops ad7606_buffer_ops
= {
1081 .postenable
= &ad7606_buffer_postenable
,
1082 .predisable
= &ad7606_buffer_predisable
,
1085 static const struct iio_buffer_setup_ops ad7606_backend_buffer_ops
= {
1086 .postenable
= &ad7606_backend_buffer_postenable
,
1087 .predisable
= &ad7606_backend_buffer_predisable
,
1090 static const struct iio_info ad7606_info_no_os_or_range
= {
1091 .read_raw
= &ad7606_read_raw
,
1092 .validate_trigger
= &ad7606_validate_trigger
,
1093 .update_scan_mode
= &ad7606_update_scan_mode
,
1096 static const struct iio_info ad7606_info_os_and_range
= {
1097 .read_raw
= &ad7606_read_raw
,
1098 .write_raw
= &ad7606_write_raw
,
1099 .attrs
= &ad7606_attribute_group_os_and_range
,
1100 .validate_trigger
= &ad7606_validate_trigger
,
1101 .update_scan_mode
= &ad7606_update_scan_mode
,
1104 static const struct iio_info ad7606_info_sw_mode
= {
1105 .read_raw
= &ad7606_read_raw
,
1106 .write_raw
= &ad7606_write_raw
,
1107 .read_avail
= &ad7606_read_avail
,
1108 .debugfs_reg_access
= &ad7606_reg_access
,
1109 .validate_trigger
= &ad7606_validate_trigger
,
1110 .update_scan_mode
= &ad7606_update_scan_mode
,
1113 static const struct iio_info ad7606_info_os
= {
1114 .read_raw
= &ad7606_read_raw
,
1115 .write_raw
= &ad7606_write_raw
,
1116 .attrs
= &ad7606_attribute_group_os
,
1117 .validate_trigger
= &ad7606_validate_trigger
,
1118 .update_scan_mode
= &ad7606_update_scan_mode
,
1121 static const struct iio_info ad7606_info_range
= {
1122 .read_raw
= &ad7606_read_raw
,
1123 .write_raw
= &ad7606_write_raw
,
1124 .attrs
= &ad7606_attribute_group_range
,
1125 .validate_trigger
= &ad7606_validate_trigger
,
1126 .update_scan_mode
= &ad7606_update_scan_mode
,
1129 static const struct iio_trigger_ops ad7606_trigger_ops
= {
1130 .validate_device
= iio_trigger_validate_own_device
,
1133 static int ad7606_sw_mode_setup(struct iio_dev
*indio_dev
)
1135 struct ad7606_state
*st
= iio_priv(indio_dev
);
1137 st
->sw_mode_en
= st
->bops
->sw_mode_config
&&
1138 device_property_present(st
->dev
, "adi,sw-mode");
1139 if (!st
->sw_mode_en
)
1142 indio_dev
->info
= &ad7606_info_sw_mode
;
1144 return st
->bops
->sw_mode_config(indio_dev
);
1147 static int ad7606_chan_scales_setup(struct iio_dev
*indio_dev
)
1149 unsigned int num_channels
= indio_dev
->num_channels
- 1;
1150 struct ad7606_state
*st
= iio_priv(indio_dev
);
1151 struct iio_chan_spec
*chans
;
1155 /* Clone IIO channels, since some may be differential */
1156 size
= indio_dev
->num_channels
* sizeof(*indio_dev
->channels
);
1157 chans
= devm_kzalloc(st
->dev
, size
, GFP_KERNEL
);
1161 memcpy(chans
, indio_dev
->channels
, size
);
1162 indio_dev
->channels
= chans
;
1164 for (ch
= 0; ch
< num_channels
; ch
++) {
1165 ret
= st
->chip_info
->scale_setup_cb(st
, &chans
[ch
+ 1], ch
);
1173 static void ad7606_pwm_disable(void *data
)
1178 int ad7606_probe(struct device
*dev
, int irq
, void __iomem
*base_address
,
1179 const struct ad7606_chip_info
*chip_info
,
1180 const struct ad7606_bus_ops
*bops
)
1182 struct ad7606_state
*st
;
1184 struct iio_dev
*indio_dev
;
1186 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
1190 st
= iio_priv(indio_dev
);
1191 dev_set_drvdata(dev
, indio_dev
);
1194 mutex_init(&st
->lock
);
1196 st
->base_address
= base_address
;
1197 st
->oversampling
= 1;
1199 ret
= devm_regulator_get_enable(dev
, "avcc");
1201 return dev_err_probe(dev
, ret
,
1202 "Failed to enable specified AVcc supply\n");
1204 st
->chip_info
= chip_info
;
1206 if (st
->chip_info
->oversampling_num
) {
1207 st
->oversampling_avail
= st
->chip_info
->oversampling_avail
;
1208 st
->num_os_ratios
= st
->chip_info
->oversampling_num
;
1211 ret
= ad7606_request_gpios(st
);
1217 indio_dev
->info
= &ad7606_info_os_and_range
;
1219 indio_dev
->info
= &ad7606_info_os
;
1222 indio_dev
->info
= &ad7606_info_range
;
1224 indio_dev
->info
= &ad7606_info_no_os_or_range
;
1226 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1227 indio_dev
->name
= chip_info
->name
;
1228 indio_dev
->channels
= st
->chip_info
->channels
;
1229 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
1231 ret
= ad7606_reset(st
);
1233 dev_warn(st
->dev
, "failed to RESET: no RESET GPIO specified\n");
1235 /* AD7616 requires al least 15ms to reconfigure after a reset */
1236 if (st
->chip_info
->init_delay_ms
) {
1237 if (msleep_interruptible(st
->chip_info
->init_delay_ms
))
1238 return -ERESTARTSYS
;
1241 st
->write_scale
= ad7606_write_scale_hw
;
1242 st
->write_os
= ad7606_write_os_hw
;
1244 ret
= ad7606_sw_mode_setup(indio_dev
);
1248 ret
= ad7606_chan_scales_setup(indio_dev
);
1252 /* If convst pin is not defined, setup PWM. */
1253 if (!st
->gpio_convst
) {
1254 st
->cnvst_pwm
= devm_pwm_get(dev
, NULL
);
1255 if (IS_ERR(st
->cnvst_pwm
))
1256 return PTR_ERR(st
->cnvst_pwm
);
1258 /* The PWM is initialized at 1MHz to have a fast enough GPIO emulation. */
1259 ret
= ad7606_set_sampling_freq(st
, 1 * MEGA
);
1263 ret
= ad7606_pwm_set_low(st
);
1268 * PWM is not disabled when sampling stops, but instead its duty cycle is set
1269 * to 0% to be sure we have a "low" state. After we unload the driver, let's
1272 ret
= devm_add_action_or_reset(dev
, ad7606_pwm_disable
,
1278 if (st
->bops
->iio_backend_config
) {
1280 * If there is a backend, the PWM should not overpass the maximum sampling
1281 * frequency the chip supports.
1283 ret
= ad7606_set_sampling_freq(st
,
1284 chip_info
->max_samplerate
? : 2 * KILO
);
1288 ret
= st
->bops
->iio_backend_config(dev
, indio_dev
);
1292 indio_dev
->setup_ops
= &ad7606_backend_buffer_ops
;
1295 /* Reserve the PWM use only for backend (force gpio_convst definition) */
1296 if (!st
->gpio_convst
)
1297 return dev_err_probe(dev
, -EINVAL
,
1298 "No backend, connect convst to a GPIO");
1300 init_completion(&st
->completion
);
1301 st
->trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d",
1303 iio_device_id(indio_dev
));
1307 st
->trig
->ops
= &ad7606_trigger_ops
;
1308 iio_trigger_set_drvdata(st
->trig
, indio_dev
);
1309 ret
= devm_iio_trigger_register(dev
, st
->trig
);
1313 indio_dev
->trig
= iio_trigger_get(st
->trig
);
1315 ret
= devm_request_threaded_irq(dev
, irq
, NULL
, &ad7606_interrupt
,
1316 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1317 chip_info
->name
, indio_dev
);
1321 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
1322 &iio_pollfunc_store_time
,
1323 &ad7606_trigger_handler
,
1324 &ad7606_buffer_ops
);
1329 return devm_iio_device_register(dev
, indio_dev
);
1331 EXPORT_SYMBOL_NS_GPL(ad7606_probe
, "IIO_AD7606");
1333 #ifdef CONFIG_PM_SLEEP
1335 static int ad7606_suspend(struct device
*dev
)
1337 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1338 struct ad7606_state
*st
= iio_priv(indio_dev
);
1340 if (st
->gpio_standby
) {
1341 gpiod_set_value(st
->gpio_range
, 1);
1342 gpiod_set_value(st
->gpio_standby
, 1);
1348 static int ad7606_resume(struct device
*dev
)
1350 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1351 struct ad7606_state
*st
= iio_priv(indio_dev
);
1353 if (st
->gpio_standby
) {
1354 gpiod_set_value(st
->gpio_range
, st
->chan_scales
[0].range
);
1355 gpiod_set_value(st
->gpio_standby
, 1);
1362 SIMPLE_DEV_PM_OPS(ad7606_pm_ops
, ad7606_suspend
, ad7606_resume
);
1363 EXPORT_SYMBOL_NS_GPL(ad7606_pm_ops
, "IIO_AD7606");
1367 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
1368 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
1369 MODULE_LICENSE("GPL v2");