Lines Matching +full:adc +full:- +full:reserved +full:- +full:channels

1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD5592R Digital <-> Analog converters driver
5 * Copyright 2014-2016 Analog Devices Inc.
19 #include <dt-bindings/iio/adi,ad5592r.h>
21 #include "ad5592r-base.h"
29 mutex_lock(&st->gpio_lock); in ad5592r_gpio_get()
31 if (st->gpio_out & BIT(offset)) in ad5592r_gpio_get()
32 val = st->gpio_val; in ad5592r_gpio_get()
34 ret = st->ops->gpio_read(st, &val); in ad5592r_gpio_get()
36 mutex_unlock(&st->gpio_lock); in ad5592r_gpio_get()
48 mutex_lock(&st->gpio_lock); in ad5592r_gpio_set()
51 st->gpio_val |= BIT(offset); in ad5592r_gpio_set()
53 st->gpio_val &= ~BIT(offset); in ad5592r_gpio_set()
55 st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val); in ad5592r_gpio_set()
57 mutex_unlock(&st->gpio_lock); in ad5592r_gpio_set()
65 mutex_lock(&st->gpio_lock); in ad5592r_gpio_direction_input()
67 st->gpio_out &= ~BIT(offset); in ad5592r_gpio_direction_input()
68 st->gpio_in |= BIT(offset); in ad5592r_gpio_direction_input()
70 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out); in ad5592r_gpio_direction_input()
74 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in); in ad5592r_gpio_direction_input()
77 mutex_unlock(&st->gpio_lock); in ad5592r_gpio_direction_input()
88 mutex_lock(&st->gpio_lock); in ad5592r_gpio_direction_output()
91 st->gpio_val |= BIT(offset); in ad5592r_gpio_direction_output()
93 st->gpio_val &= ~BIT(offset); in ad5592r_gpio_direction_output()
95 st->gpio_in &= ~BIT(offset); in ad5592r_gpio_direction_output()
96 st->gpio_out |= BIT(offset); in ad5592r_gpio_direction_output()
98 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val); in ad5592r_gpio_direction_output()
102 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out); in ad5592r_gpio_direction_output()
106 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in); in ad5592r_gpio_direction_output()
109 mutex_unlock(&st->gpio_lock); in ad5592r_gpio_direction_output()
118 if (!(st->gpio_map & BIT(offset))) { in ad5592r_gpio_request()
119 dev_err(st->dev, "GPIO %d is reserved by alternate function\n", in ad5592r_gpio_request()
121 return -ENODEV; in ad5592r_gpio_request()
133 if (!st->gpio_map) in ad5592r_gpio_init()
136 st->gpiochip.label = dev_name(st->dev); in ad5592r_gpio_init()
137 st->gpiochip.base = -1; in ad5592r_gpio_init()
138 st->gpiochip.ngpio = 8; in ad5592r_gpio_init()
139 st->gpiochip.parent = st->dev; in ad5592r_gpio_init()
140 st->gpiochip.can_sleep = true; in ad5592r_gpio_init()
141 st->gpiochip.direction_input = ad5592r_gpio_direction_input; in ad5592r_gpio_init()
142 st->gpiochip.direction_output = ad5592r_gpio_direction_output; in ad5592r_gpio_init()
143 st->gpiochip.get = ad5592r_gpio_get; in ad5592r_gpio_init()
144 st->gpiochip.set = ad5592r_gpio_set; in ad5592r_gpio_init()
145 st->gpiochip.request = ad5592r_gpio_request; in ad5592r_gpio_init()
146 st->gpiochip.owner = THIS_MODULE; in ad5592r_gpio_init()
147 st->gpiochip.names = ad5592r_gpio_names; in ad5592r_gpio_init()
149 mutex_init(&st->gpio_lock); in ad5592r_gpio_init()
151 return gpiochip_add_data(&st->gpiochip, st); in ad5592r_gpio_init()
156 if (st->gpio_map) in ad5592r_gpio_cleanup()
157 gpiochip_remove(&st->gpiochip); in ad5592r_gpio_cleanup()
164 gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW); in ad5592r_reset()
172 mutex_lock(&st->lock); in ad5592r_reset()
174 st->ops->reg_write(st, AD5592R_REG_RESET, 0xdac); in ad5592r_reset()
175 mutex_unlock(&st->lock); in ad5592r_reset()
187 if (st->reg) { in ad5592r_get_vref()
188 ret = regulator_get_voltage(st->reg); in ad5592r_get_vref()
200 const struct ad5592r_rw_ops *ops = st->ops; in ad5592r_set_channel_modes()
203 u8 pulldown = 0, tristate = 0, dac = 0, adc = 0; in ad5592r_set_channel_modes() local
206 for (i = 0; i < st->num_channels; i++) { in ad5592r_set_channel_modes()
207 switch (st->channel_modes[i]) { in ad5592r_set_channel_modes()
213 adc |= BIT(i); in ad5592r_set_channel_modes()
218 adc |= BIT(i); in ad5592r_set_channel_modes()
222 st->gpio_map |= BIT(i); in ad5592r_set_channel_modes()
223 st->gpio_in |= BIT(i); /* Default to input */ in ad5592r_set_channel_modes()
228 switch (st->channel_offstate[i]) { in ad5592r_set_channel_modes()
234 st->gpio_out |= BIT(i); in ad5592r_set_channel_modes()
238 st->gpio_out |= BIT(i); in ad5592r_set_channel_modes()
239 st->gpio_val |= BIT(i); in ad5592r_set_channel_modes()
250 mutex_lock(&st->lock); in ad5592r_set_channel_modes()
253 ret = ops->reg_write(st, AD5592R_REG_PULLDOWN, pulldown); in ad5592r_set_channel_modes()
257 ret = ops->reg_write(st, AD5592R_REG_TRISTATE, tristate); in ad5592r_set_channel_modes()
262 ret = ops->reg_write(st, AD5592R_REG_DAC_EN, dac); in ad5592r_set_channel_modes()
266 ret = ops->reg_write(st, AD5592R_REG_ADC_EN, adc); in ad5592r_set_channel_modes()
270 ret = ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val); in ad5592r_set_channel_modes()
274 ret = ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out); in ad5592r_set_channel_modes()
278 ret = ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in); in ad5592r_set_channel_modes()
283 ret = ops->reg_read(st, AD5592R_REG_ADC_EN, &read_back); in ad5592r_set_channel_modes()
284 if (!ret && (read_back & 0xff) != adc) in ad5592r_set_channel_modes()
285 ret = -EIO; in ad5592r_set_channel_modes()
288 mutex_unlock(&st->lock); in ad5592r_set_channel_modes()
296 for (i = 0; i < ARRAY_SIZE(st->channel_modes); i++) in ad5592r_reset_channel_modes()
297 st->channel_modes[i] = CH_MODE_UNUSED; in ad5592r_reset_channel_modes()
311 if (val >= (1 << chan->scan_type.realbits) || val < 0) in ad5592r_write_raw()
312 return -EINVAL; in ad5592r_write_raw()
314 if (!chan->output) in ad5592r_write_raw()
315 return -EINVAL; in ad5592r_write_raw()
317 mutex_lock(&st->lock); in ad5592r_write_raw()
318 ret = st->ops->write_dac(st, chan->channel, val); in ad5592r_write_raw()
320 st->cached_dac[chan->channel] = val; in ad5592r_write_raw()
321 mutex_unlock(&st->lock); in ad5592r_write_raw()
324 if (chan->type == IIO_VOLTAGE) { in ad5592r_write_raw()
327 if (val == st->scale_avail[0][0] && in ad5592r_write_raw()
328 val2 == st->scale_avail[0][1]) in ad5592r_write_raw()
330 else if (val == st->scale_avail[1][0] && in ad5592r_write_raw()
331 val2 == st->scale_avail[1][1]) in ad5592r_write_raw()
334 return -EINVAL; in ad5592r_write_raw()
336 mutex_lock(&st->lock); in ad5592r_write_raw()
338 ret = st->ops->reg_read(st, AD5592R_REG_CTRL, in ad5592r_write_raw()
339 &st->cached_gp_ctrl); in ad5592r_write_raw()
341 mutex_unlock(&st->lock); in ad5592r_write_raw()
345 if (chan->output) { in ad5592r_write_raw()
347 st->cached_gp_ctrl |= in ad5592r_write_raw()
350 st->cached_gp_ctrl &= in ad5592r_write_raw()
354 st->cached_gp_ctrl |= in ad5592r_write_raw()
357 st->cached_gp_ctrl &= in ad5592r_write_raw()
361 ret = st->ops->reg_write(st, AD5592R_REG_CTRL, in ad5592r_write_raw()
362 st->cached_gp_ctrl); in ad5592r_write_raw()
363 mutex_unlock(&st->lock); in ad5592r_write_raw()
369 return -EINVAL; in ad5592r_write_raw()
385 if (!chan->output) { in ad5592r_read_raw()
386 mutex_lock(&st->lock); in ad5592r_read_raw()
387 ret = st->ops->read_adc(st, chan->channel, &read_val); in ad5592r_read_raw()
388 mutex_unlock(&st->lock); in ad5592r_read_raw()
392 if ((read_val >> 12 & 0x7) != (chan->channel & 0x7)) { in ad5592r_read_raw()
393 dev_err(st->dev, "Error while reading channel %u\n", in ad5592r_read_raw()
394 chan->channel); in ad5592r_read_raw()
395 return -EIO; in ad5592r_read_raw()
401 mutex_lock(&st->lock); in ad5592r_read_raw()
402 read_val = st->cached_dac[chan->channel]; in ad5592r_read_raw()
403 mutex_unlock(&st->lock); in ad5592r_read_raw()
406 dev_dbg(st->dev, "Channel %u read: 0x%04hX\n", in ad5592r_read_raw()
407 chan->channel, read_val); in ad5592r_read_raw()
414 if (chan->type == IIO_TEMP) { in ad5592r_read_raw()
421 mutex_lock(&st->lock); in ad5592r_read_raw()
423 if (chan->output) in ad5592r_read_raw()
424 mult = !!(st->cached_gp_ctrl & in ad5592r_read_raw()
427 mult = !!(st->cached_gp_ctrl & in ad5592r_read_raw()
430 mutex_unlock(&st->lock); in ad5592r_read_raw()
434 *val2 = chan->scan_type.realbits; in ad5592r_read_raw()
440 mutex_lock(&st->lock); in ad5592r_read_raw()
442 if (st->cached_gp_ctrl & AD5592R_REG_CTRL_ADC_RANGE) in ad5592r_read_raw()
443 *val = (-34365 * 25) / ret; in ad5592r_read_raw()
445 *val = (-75365 * 25) / ret; in ad5592r_read_raw()
447 mutex_unlock(&st->lock); in ad5592r_read_raw()
451 return -EINVAL; in ad5592r_read_raw()
466 return -EINVAL; in ad5592r_write_raw_get_fmt()
483 st->scale_avail[0][0], st->scale_avail[0][1], in ad5592r_show_scale_available()
484 st->scale_avail[1][0], st->scale_avail[1][1]); in ad5592r_show_scale_available()
499 chan->type = IIO_VOLTAGE; in ad5592r_setup_channel()
500 chan->indexed = 1; in ad5592r_setup_channel()
501 chan->output = output; in ad5592r_setup_channel()
502 chan->channel = id; in ad5592r_setup_channel()
503 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); in ad5592r_setup_channel()
504 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); in ad5592r_setup_channel()
505 chan->scan_type.sign = 'u'; in ad5592r_setup_channel()
506 chan->scan_type.realbits = 12; in ad5592r_setup_channel()
507 chan->scan_type.storagebits = 16; in ad5592r_setup_channel()
508 chan->ext_info = ad5592r_ext_info; in ad5592r_setup_channel()
515 num_channels = st->num_channels; in ad5592r_alloc_channels()
516 struct iio_chan_spec *channels; in ad5592r_alloc_channels() local
521 device_for_each_child_node(st->dev, child) { in ad5592r_alloc_channels()
523 if (ret || reg >= ARRAY_SIZE(st->channel_modes)) in ad5592r_alloc_channels()
528 st->channel_modes[reg] = tmp; in ad5592r_alloc_channels()
530 ret = fwnode_property_read_u32(child, "adi,off-state", &tmp); in ad5592r_alloc_channels()
532 st->channel_offstate[reg] = tmp; in ad5592r_alloc_channels()
535 channels = devm_kcalloc(st->dev, in ad5592r_alloc_channels()
536 1 + 2 * num_channels, sizeof(*channels), in ad5592r_alloc_channels()
538 if (!channels) in ad5592r_alloc_channels()
539 return -ENOMEM; in ad5592r_alloc_channels()
542 switch (st->channel_modes[i]) { in ad5592r_alloc_channels()
544 ad5592r_setup_channel(iio_dev, &channels[curr_channel], in ad5592r_alloc_channels()
550 ad5592r_setup_channel(iio_dev, &channels[curr_channel], in ad5592r_alloc_channels()
556 ad5592r_setup_channel(iio_dev, &channels[curr_channel], in ad5592r_alloc_channels()
559 ad5592r_setup_channel(iio_dev, &channels[curr_channel], in ad5592r_alloc_channels()
569 channels[curr_channel].type = IIO_TEMP; in ad5592r_alloc_channels()
570 channels[curr_channel].channel = 8; in ad5592r_alloc_channels()
571 channels[curr_channel].info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | in ad5592r_alloc_channels()
576 iio_dev->num_channels = curr_channel; in ad5592r_alloc_channels()
577 iio_dev->channels = channels; in ad5592r_alloc_channels()
586 st->scale_avail[0][0] = in ad5592r_init_scales()
587 div_s64_rem(tmp, 1000000000LL, &st->scale_avail[0][1]); in ad5592r_init_scales()
588 st->scale_avail[1][0] = in ad5592r_init_scales()
589 div_s64_rem(tmp * 2, 1000000000LL, &st->scale_avail[1][1]); in ad5592r_init_scales()
601 return -ENOMEM; in ad5592r_probe()
604 st->dev = dev; in ad5592r_probe()
605 st->ops = ops; in ad5592r_probe()
606 st->num_channels = 8; in ad5592r_probe()
609 st->reg = devm_regulator_get_optional(dev, "vref"); in ad5592r_probe()
610 if (IS_ERR(st->reg)) { in ad5592r_probe()
611 if ((PTR_ERR(st->reg) != -ENODEV) && dev_fwnode(dev)) in ad5592r_probe()
612 return PTR_ERR(st->reg); in ad5592r_probe()
614 st->reg = NULL; in ad5592r_probe()
616 ret = regulator_enable(st->reg); in ad5592r_probe()
621 iio_dev->name = name; in ad5592r_probe()
622 iio_dev->info = &ad5592r_info; in ad5592r_probe()
623 iio_dev->modes = INDIO_DIRECT_MODE; in ad5592r_probe()
625 mutex_init(&st->lock); in ad5592r_probe()
633 ret = ops->reg_write(st, AD5592R_REG_PD, in ad5592r_probe()
634 (st->reg == NULL) ? AD5592R_REG_PD_EN_REF : 0); in ad5592r_probe()
663 if (st->reg) in ad5592r_probe()
664 regulator_disable(st->reg); in ad5592r_probe()
679 if (st->reg) in ad5592r_remove()
680 regulator_disable(st->reg); in ad5592r_remove()
685 MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");