2 * AD5592R Digital <-> Analog converters driver
4 * Copyright 2014-2016 Analog Devices Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
7 * Licensed under the GPL-2.
10 #include <linux/bitops.h>
11 #include <linux/delay.h>
12 #include <linux/iio/iio.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/gpio.h>
20 #include <linux/property.h>
22 #include <dt-bindings/iio/adi,ad5592r.h>
24 #include "ad5592r-base.h"
26 static int ad5592r_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
28 struct ad5592r_state
*st
= gpiochip_get_data(chip
);
32 mutex_lock(&st
->gpio_lock
);
34 if (st
->gpio_out
& BIT(offset
))
37 ret
= st
->ops
->gpio_read(st
, &val
);
39 mutex_unlock(&st
->gpio_lock
);
44 return !!(val
& BIT(offset
));
47 static void ad5592r_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
49 struct ad5592r_state
*st
= gpiochip_get_data(chip
);
51 mutex_lock(&st
->gpio_lock
);
54 st
->gpio_val
|= BIT(offset
);
56 st
->gpio_val
&= ~BIT(offset
);
58 st
->ops
->reg_write(st
, AD5592R_REG_GPIO_SET
, st
->gpio_val
);
60 mutex_unlock(&st
->gpio_lock
);
63 static int ad5592r_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
65 struct ad5592r_state
*st
= gpiochip_get_data(chip
);
68 mutex_lock(&st
->gpio_lock
);
70 st
->gpio_out
&= ~BIT(offset
);
71 st
->gpio_in
|= BIT(offset
);
73 ret
= st
->ops
->reg_write(st
, AD5592R_REG_GPIO_OUT_EN
, st
->gpio_out
);
77 ret
= st
->ops
->reg_write(st
, AD5592R_REG_GPIO_IN_EN
, st
->gpio_in
);
80 mutex_unlock(&st
->gpio_lock
);
85 static int ad5592r_gpio_direction_output(struct gpio_chip
*chip
,
86 unsigned offset
, int value
)
88 struct ad5592r_state
*st
= gpiochip_get_data(chip
);
91 mutex_lock(&st
->gpio_lock
);
94 st
->gpio_val
|= BIT(offset
);
96 st
->gpio_val
&= ~BIT(offset
);
98 st
->gpio_in
&= ~BIT(offset
);
99 st
->gpio_out
|= BIT(offset
);
101 ret
= st
->ops
->reg_write(st
, AD5592R_REG_GPIO_SET
, st
->gpio_val
);
105 ret
= st
->ops
->reg_write(st
, AD5592R_REG_GPIO_OUT_EN
, st
->gpio_out
);
109 ret
= st
->ops
->reg_write(st
, AD5592R_REG_GPIO_IN_EN
, st
->gpio_in
);
112 mutex_unlock(&st
->gpio_lock
);
117 static int ad5592r_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
119 struct ad5592r_state
*st
= gpiochip_get_data(chip
);
121 if (!(st
->gpio_map
& BIT(offset
))) {
122 dev_err(st
->dev
, "GPIO %d is reserved by alternate function\n",
130 static int ad5592r_gpio_init(struct ad5592r_state
*st
)
135 st
->gpiochip
.label
= dev_name(st
->dev
);
136 st
->gpiochip
.base
= -1;
137 st
->gpiochip
.ngpio
= 8;
138 st
->gpiochip
.parent
= st
->dev
;
139 st
->gpiochip
.can_sleep
= true;
140 st
->gpiochip
.direction_input
= ad5592r_gpio_direction_input
;
141 st
->gpiochip
.direction_output
= ad5592r_gpio_direction_output
;
142 st
->gpiochip
.get
= ad5592r_gpio_get
;
143 st
->gpiochip
.set
= ad5592r_gpio_set
;
144 st
->gpiochip
.request
= ad5592r_gpio_request
;
145 st
->gpiochip
.owner
= THIS_MODULE
;
147 mutex_init(&st
->gpio_lock
);
149 return gpiochip_add_data(&st
->gpiochip
, st
);
152 static void ad5592r_gpio_cleanup(struct ad5592r_state
*st
)
155 gpiochip_remove(&st
->gpiochip
);
158 static int ad5592r_reset(struct ad5592r_state
*st
)
160 struct gpio_desc
*gpio
;
161 struct iio_dev
*iio_dev
= iio_priv_to_dev(st
);
163 gpio
= devm_gpiod_get_optional(st
->dev
, "reset", GPIOD_OUT_LOW
);
165 return PTR_ERR(gpio
);
169 gpiod_set_value(gpio
, 1);
171 mutex_lock(&iio_dev
->mlock
);
172 /* Writing this magic value resets the device */
173 st
->ops
->reg_write(st
, AD5592R_REG_RESET
, 0xdac);
174 mutex_unlock(&iio_dev
->mlock
);
182 static int ad5592r_get_vref(struct ad5592r_state
*st
)
187 ret
= regulator_get_voltage(st
->reg
);
197 static int ad5592r_set_channel_modes(struct ad5592r_state
*st
)
199 const struct ad5592r_rw_ops
*ops
= st
->ops
;
202 struct iio_dev
*iio_dev
= iio_priv_to_dev(st
);
203 u8 pulldown
= 0, tristate
= 0, dac
= 0, adc
= 0;
206 for (i
= 0; i
< st
->num_channels
; i
++) {
207 switch (st
->channel_modes
[i
]) {
216 case CH_MODE_DAC_AND_ADC
:
222 st
->gpio_map
|= BIT(i
);
223 st
->gpio_in
|= BIT(i
); /* Default to input */
229 switch (st
->channel_offstate
[i
]) {
230 case CH_OFFSTATE_OUT_TRISTATE
:
234 case CH_OFFSTATE_OUT_LOW
:
235 st
->gpio_out
|= BIT(i
);
238 case CH_OFFSTATE_OUT_HIGH
:
239 st
->gpio_out
|= BIT(i
);
240 st
->gpio_val
|= BIT(i
);
243 case CH_OFFSTATE_PULLDOWN
:
252 mutex_lock(&iio_dev
->mlock
);
254 /* Pull down unused pins to GND */
255 ret
= ops
->reg_write(st
, AD5592R_REG_PULLDOWN
, pulldown
);
259 ret
= ops
->reg_write(st
, AD5592R_REG_TRISTATE
, tristate
);
263 /* Configure pins that we use */
264 ret
= ops
->reg_write(st
, AD5592R_REG_DAC_EN
, dac
);
268 ret
= ops
->reg_write(st
, AD5592R_REG_ADC_EN
, adc
);
272 ret
= ops
->reg_write(st
, AD5592R_REG_GPIO_SET
, st
->gpio_val
);
276 ret
= ops
->reg_write(st
, AD5592R_REG_GPIO_OUT_EN
, st
->gpio_out
);
280 ret
= ops
->reg_write(st
, AD5592R_REG_GPIO_IN_EN
, st
->gpio_in
);
284 /* Verify that we can read back at least one register */
285 ret
= ops
->reg_read(st
, AD5592R_REG_ADC_EN
, &read_back
);
286 if (!ret
&& (read_back
& 0xff) != adc
)
290 mutex_unlock(&iio_dev
->mlock
);
294 static int ad5592r_reset_channel_modes(struct ad5592r_state
*st
)
298 for (i
= 0; i
< ARRAY_SIZE(st
->channel_modes
); i
++)
299 st
->channel_modes
[i
] = CH_MODE_UNUSED
;
301 return ad5592r_set_channel_modes(st
);
304 static int ad5592r_write_raw(struct iio_dev
*iio_dev
,
305 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
307 struct ad5592r_state
*st
= iio_priv(iio_dev
);
311 case IIO_CHAN_INFO_RAW
:
313 if (val
>= (1 << chan
->scan_type
.realbits
) || val
< 0)
319 mutex_lock(&iio_dev
->mlock
);
320 ret
= st
->ops
->write_dac(st
, chan
->channel
, val
);
322 st
->cached_dac
[chan
->channel
] = val
;
323 mutex_unlock(&iio_dev
->mlock
);
325 case IIO_CHAN_INFO_SCALE
:
326 if (chan
->type
== IIO_VOLTAGE
) {
329 if (val
== st
->scale_avail
[0][0] &&
330 val2
== st
->scale_avail
[0][1])
332 else if (val
== st
->scale_avail
[1][0] &&
333 val2
== st
->scale_avail
[1][1])
338 mutex_lock(&iio_dev
->mlock
);
340 ret
= st
->ops
->reg_read(st
, AD5592R_REG_CTRL
,
341 &st
->cached_gp_ctrl
);
343 mutex_unlock(&iio_dev
->mlock
);
349 st
->cached_gp_ctrl
|=
350 AD5592R_REG_CTRL_DAC_RANGE
;
352 st
->cached_gp_ctrl
&=
353 ~AD5592R_REG_CTRL_DAC_RANGE
;
356 st
->cached_gp_ctrl
|=
357 AD5592R_REG_CTRL_ADC_RANGE
;
359 st
->cached_gp_ctrl
&=
360 ~AD5592R_REG_CTRL_ADC_RANGE
;
363 ret
= st
->ops
->reg_write(st
, AD5592R_REG_CTRL
,
365 mutex_unlock(&iio_dev
->mlock
);
377 static int ad5592r_read_raw(struct iio_dev
*iio_dev
,
378 struct iio_chan_spec
const *chan
,
379 int *val
, int *val2
, long m
)
381 struct ad5592r_state
*st
= iio_priv(iio_dev
);
386 case IIO_CHAN_INFO_RAW
:
387 mutex_lock(&iio_dev
->mlock
);
390 ret
= st
->ops
->read_adc(st
, chan
->channel
, &read_val
);
394 if ((read_val
>> 12 & 0x7) != (chan
->channel
& 0x7)) {
395 dev_err(st
->dev
, "Error while reading channel %u\n",
401 read_val
&= GENMASK(11, 0);
404 read_val
= st
->cached_dac
[chan
->channel
];
407 dev_dbg(st
->dev
, "Channel %u read: 0x%04hX\n",
408 chan
->channel
, read_val
);
410 *val
= (int) read_val
;
413 case IIO_CHAN_INFO_SCALE
:
414 *val
= ad5592r_get_vref(st
);
416 if (chan
->type
== IIO_TEMP
) {
417 s64 tmp
= *val
* (3767897513LL / 25LL);
418 *val
= div_s64_rem(tmp
, 1000000000LL, val2
);
420 ret
= IIO_VAL_INT_PLUS_MICRO
;
424 mutex_lock(&iio_dev
->mlock
);
427 mult
= !!(st
->cached_gp_ctrl
&
428 AD5592R_REG_CTRL_DAC_RANGE
);
430 mult
= !!(st
->cached_gp_ctrl
&
431 AD5592R_REG_CTRL_ADC_RANGE
);
435 *val2
= chan
->scan_type
.realbits
;
436 ret
= IIO_VAL_FRACTIONAL_LOG2
;
439 case IIO_CHAN_INFO_OFFSET
:
440 ret
= ad5592r_get_vref(st
);
442 mutex_lock(&iio_dev
->mlock
);
444 if (st
->cached_gp_ctrl
& AD5592R_REG_CTRL_ADC_RANGE
)
445 *val
= (-34365 * 25) / ret
;
447 *val
= (-75365 * 25) / ret
;
455 mutex_unlock(&iio_dev
->mlock
);
459 static int ad5592r_write_raw_get_fmt(struct iio_dev
*indio_dev
,
460 struct iio_chan_spec
const *chan
, long mask
)
463 case IIO_CHAN_INFO_SCALE
:
464 return IIO_VAL_INT_PLUS_NANO
;
467 return IIO_VAL_INT_PLUS_MICRO
;
473 static const struct iio_info ad5592r_info
= {
474 .read_raw
= ad5592r_read_raw
,
475 .write_raw
= ad5592r_write_raw
,
476 .write_raw_get_fmt
= ad5592r_write_raw_get_fmt
,
479 static ssize_t
ad5592r_show_scale_available(struct iio_dev
*iio_dev
,
481 const struct iio_chan_spec
*chan
,
484 struct ad5592r_state
*st
= iio_priv(iio_dev
);
486 return sprintf(buf
, "%d.%09u %d.%09u\n",
487 st
->scale_avail
[0][0], st
->scale_avail
[0][1],
488 st
->scale_avail
[1][0], st
->scale_avail
[1][1]);
491 static struct iio_chan_spec_ext_info ad5592r_ext_info
[] = {
493 .name
= "scale_available",
494 .read
= ad5592r_show_scale_available
,
500 static void ad5592r_setup_channel(struct iio_dev
*iio_dev
,
501 struct iio_chan_spec
*chan
, bool output
, unsigned id
)
503 chan
->type
= IIO_VOLTAGE
;
505 chan
->output
= output
;
507 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
508 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
);
509 chan
->scan_type
.sign
= 'u';
510 chan
->scan_type
.realbits
= 12;
511 chan
->scan_type
.storagebits
= 16;
512 chan
->ext_info
= ad5592r_ext_info
;
515 static int ad5592r_alloc_channels(struct ad5592r_state
*st
)
517 unsigned i
, curr_channel
= 0,
518 num_channels
= st
->num_channels
;
519 struct iio_dev
*iio_dev
= iio_priv_to_dev(st
);
520 struct iio_chan_spec
*channels
;
521 struct fwnode_handle
*child
;
525 device_for_each_child_node(st
->dev
, child
) {
526 ret
= fwnode_property_read_u32(child
, "reg", ®
);
527 if (ret
|| reg
>= ARRAY_SIZE(st
->channel_modes
))
530 ret
= fwnode_property_read_u32(child
, "adi,mode", &tmp
);
532 st
->channel_modes
[reg
] = tmp
;
534 fwnode_property_read_u32(child
, "adi,off-state", &tmp
);
536 st
->channel_offstate
[reg
] = tmp
;
539 channels
= devm_kcalloc(st
->dev
,
540 1 + 2 * num_channels
, sizeof(*channels
),
545 for (i
= 0; i
< num_channels
; i
++) {
546 switch (st
->channel_modes
[i
]) {
548 ad5592r_setup_channel(iio_dev
, &channels
[curr_channel
],
554 ad5592r_setup_channel(iio_dev
, &channels
[curr_channel
],
559 case CH_MODE_DAC_AND_ADC
:
560 ad5592r_setup_channel(iio_dev
, &channels
[curr_channel
],
563 ad5592r_setup_channel(iio_dev
, &channels
[curr_channel
],
573 channels
[curr_channel
].type
= IIO_TEMP
;
574 channels
[curr_channel
].channel
= 8;
575 channels
[curr_channel
].info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
576 BIT(IIO_CHAN_INFO_SCALE
) |
577 BIT(IIO_CHAN_INFO_OFFSET
);
580 iio_dev
->num_channels
= curr_channel
;
581 iio_dev
->channels
= channels
;
586 static void ad5592r_init_scales(struct ad5592r_state
*st
, int vref_mV
)
588 s64 tmp
= (s64
)vref_mV
* 1000000000LL >> 12;
590 st
->scale_avail
[0][0] =
591 div_s64_rem(tmp
, 1000000000LL, &st
->scale_avail
[0][1]);
592 st
->scale_avail
[1][0] =
593 div_s64_rem(tmp
* 2, 1000000000LL, &st
->scale_avail
[1][1]);
596 int ad5592r_probe(struct device
*dev
, const char *name
,
597 const struct ad5592r_rw_ops
*ops
)
599 struct iio_dev
*iio_dev
;
600 struct ad5592r_state
*st
;
603 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
607 st
= iio_priv(iio_dev
);
610 st
->num_channels
= 8;
611 dev_set_drvdata(dev
, iio_dev
);
613 st
->reg
= devm_regulator_get_optional(dev
, "vref");
614 if (IS_ERR(st
->reg
)) {
615 if ((PTR_ERR(st
->reg
) != -ENODEV
) && dev
->of_node
)
616 return PTR_ERR(st
->reg
);
620 ret
= regulator_enable(st
->reg
);
625 iio_dev
->dev
.parent
= dev
;
626 iio_dev
->name
= name
;
627 iio_dev
->info
= &ad5592r_info
;
628 iio_dev
->modes
= INDIO_DIRECT_MODE
;
630 ad5592r_init_scales(st
, ad5592r_get_vref(st
));
632 ret
= ad5592r_reset(st
);
634 goto error_disable_reg
;
636 ret
= ops
->reg_write(st
, AD5592R_REG_PD
,
637 (st
->reg
== NULL
) ? AD5592R_REG_PD_EN_REF
: 0);
639 goto error_disable_reg
;
641 ret
= ad5592r_alloc_channels(st
);
643 goto error_disable_reg
;
645 ret
= ad5592r_set_channel_modes(st
);
647 goto error_reset_ch_modes
;
649 ret
= iio_device_register(iio_dev
);
651 goto error_reset_ch_modes
;
653 ret
= ad5592r_gpio_init(st
);
655 goto error_dev_unregister
;
659 error_dev_unregister
:
660 iio_device_unregister(iio_dev
);
662 error_reset_ch_modes
:
663 ad5592r_reset_channel_modes(st
);
667 regulator_disable(st
->reg
);
671 EXPORT_SYMBOL_GPL(ad5592r_probe
);
673 int ad5592r_remove(struct device
*dev
)
675 struct iio_dev
*iio_dev
= dev_get_drvdata(dev
);
676 struct ad5592r_state
*st
= iio_priv(iio_dev
);
678 iio_device_unregister(iio_dev
);
679 ad5592r_reset_channel_modes(st
);
680 ad5592r_gpio_cleanup(st
);
683 regulator_disable(st
->reg
);
687 EXPORT_SYMBOL_GPL(ad5592r_remove
);
689 MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
690 MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
691 MODULE_LICENSE("GPL v2");