Lines Matching +full:1 +full:st

25 	struct ad5592r_state *st = gpiochip_get_data(chip);  in ad5592r_gpio_get()  local
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()
46 struct ad5592r_state *st = gpiochip_get_data(chip); in ad5592r_gpio_set() local
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()
62 struct ad5592r_state *st = gpiochip_get_data(chip); in ad5592r_gpio_direction_input() local
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()
85 struct ad5592r_state *st = gpiochip_get_data(chip); in ad5592r_gpio_direction_output() local
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()
116 struct ad5592r_state *st = gpiochip_get_data(chip); in ad5592r_gpio_request() local
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()
127 static int ad5592r_gpio_init(struct ad5592r_state *st) in ad5592r_gpio_init() argument
129 if (!st->gpio_map) in ad5592r_gpio_init()
132 st->gpiochip.label = dev_name(st->dev); in ad5592r_gpio_init()
133 st->gpiochip.base = -1; in ad5592r_gpio_init()
134 st->gpiochip.ngpio = 8; in ad5592r_gpio_init()
135 st->gpiochip.parent = st->dev; in ad5592r_gpio_init()
136 st->gpiochip.can_sleep = true; in ad5592r_gpio_init()
137 st->gpiochip.direction_input = ad5592r_gpio_direction_input; in ad5592r_gpio_init()
138 st->gpiochip.direction_output = ad5592r_gpio_direction_output; in ad5592r_gpio_init()
139 st->gpiochip.get = ad5592r_gpio_get; in ad5592r_gpio_init()
140 st->gpiochip.set = ad5592r_gpio_set; in ad5592r_gpio_init()
141 st->gpiochip.request = ad5592r_gpio_request; in ad5592r_gpio_init()
142 st->gpiochip.owner = THIS_MODULE; in ad5592r_gpio_init()
144 mutex_init(&st->gpio_lock); in ad5592r_gpio_init()
146 return gpiochip_add_data(&st->gpiochip, st); in ad5592r_gpio_init()
149 static void ad5592r_gpio_cleanup(struct ad5592r_state *st) in ad5592r_gpio_cleanup() argument
151 if (st->gpio_map) in ad5592r_gpio_cleanup()
152 gpiochip_remove(&st->gpiochip); in ad5592r_gpio_cleanup()
155 static int ad5592r_reset(struct ad5592r_state *st) in ad5592r_reset() argument
159 gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW); in ad5592r_reset()
164 udelay(1); in ad5592r_reset()
165 gpiod_set_value(gpio, 1); in ad5592r_reset()
167 mutex_lock(&st->lock); in ad5592r_reset()
169 st->ops->reg_write(st, AD5592R_REG_RESET, 0xdac); in ad5592r_reset()
170 mutex_unlock(&st->lock); in ad5592r_reset()
178 static int ad5592r_get_vref(struct ad5592r_state *st) in ad5592r_get_vref() argument
182 if (st->reg) { in ad5592r_get_vref()
183 ret = regulator_get_voltage(st->reg); in ad5592r_get_vref()
193 static int ad5592r_set_channel_modes(struct ad5592r_state *st) in ad5592r_set_channel_modes() argument
195 const struct ad5592r_rw_ops *ops = st->ops; in ad5592r_set_channel_modes()
201 for (i = 0; i < st->num_channels; i++) { in ad5592r_set_channel_modes()
202 switch (st->channel_modes[i]) { in ad5592r_set_channel_modes()
217 st->gpio_map |= BIT(i); in ad5592r_set_channel_modes()
218 st->gpio_in |= BIT(i); /* Default to input */ in ad5592r_set_channel_modes()
223 switch (st->channel_offstate[i]) { in ad5592r_set_channel_modes()
229 st->gpio_out |= BIT(i); in ad5592r_set_channel_modes()
233 st->gpio_out |= BIT(i); in ad5592r_set_channel_modes()
234 st->gpio_val |= BIT(i); in ad5592r_set_channel_modes()
245 mutex_lock(&st->lock); in ad5592r_set_channel_modes()
248 ret = ops->reg_write(st, AD5592R_REG_PULLDOWN, pulldown); in ad5592r_set_channel_modes()
252 ret = ops->reg_write(st, AD5592R_REG_TRISTATE, tristate); in ad5592r_set_channel_modes()
257 ret = ops->reg_write(st, AD5592R_REG_DAC_EN, dac); in ad5592r_set_channel_modes()
261 ret = ops->reg_write(st, AD5592R_REG_ADC_EN, adc); in ad5592r_set_channel_modes()
265 ret = ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val); in ad5592r_set_channel_modes()
269 ret = ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out); in ad5592r_set_channel_modes()
273 ret = ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in); in ad5592r_set_channel_modes()
278 ret = ops->reg_read(st, AD5592R_REG_ADC_EN, &read_back); in ad5592r_set_channel_modes()
283 mutex_unlock(&st->lock); in ad5592r_set_channel_modes()
287 static int ad5592r_reset_channel_modes(struct ad5592r_state *st) in ad5592r_reset_channel_modes() argument
291 for (i = 0; i < ARRAY_SIZE(st->channel_modes); i++) in ad5592r_reset_channel_modes()
292 st->channel_modes[i] = CH_MODE_UNUSED; in ad5592r_reset_channel_modes()
294 return ad5592r_set_channel_modes(st); in ad5592r_reset_channel_modes()
300 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_write_raw() local
306 if (val >= (1 << chan->scan_type.realbits) || val < 0) in ad5592r_write_raw()
312 mutex_lock(&st->lock); in ad5592r_write_raw()
313 ret = st->ops->write_dac(st, chan->channel, val); in ad5592r_write_raw()
315 st->cached_dac[chan->channel] = val; in ad5592r_write_raw()
316 mutex_unlock(&st->lock); in ad5592r_write_raw()
322 if (val == st->scale_avail[0][0] && in ad5592r_write_raw()
323 val2 == st->scale_avail[0][1]) in ad5592r_write_raw()
325 else if (val == st->scale_avail[1][0] && in ad5592r_write_raw()
326 val2 == st->scale_avail[1][1]) in ad5592r_write_raw()
331 mutex_lock(&st->lock); in ad5592r_write_raw()
333 ret = st->ops->reg_read(st, AD5592R_REG_CTRL, in ad5592r_write_raw()
334 &st->cached_gp_ctrl); in ad5592r_write_raw()
336 mutex_unlock(&st->lock); in ad5592r_write_raw()
342 st->cached_gp_ctrl |= in ad5592r_write_raw()
345 st->cached_gp_ctrl &= in ad5592r_write_raw()
349 st->cached_gp_ctrl |= in ad5592r_write_raw()
352 st->cached_gp_ctrl &= in ad5592r_write_raw()
356 ret = st->ops->reg_write(st, AD5592R_REG_CTRL, in ad5592r_write_raw()
357 st->cached_gp_ctrl); in ad5592r_write_raw()
358 mutex_unlock(&st->lock); in ad5592r_write_raw()
374 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_read_raw() local
381 mutex_lock(&st->lock); in ad5592r_read_raw()
382 ret = st->ops->read_adc(st, chan->channel, &read_val); in ad5592r_read_raw()
383 mutex_unlock(&st->lock); in ad5592r_read_raw()
388 dev_err(st->dev, "Error while reading channel %u\n", in ad5592r_read_raw()
396 mutex_lock(&st->lock); in ad5592r_read_raw()
397 read_val = st->cached_dac[chan->channel]; in ad5592r_read_raw()
398 mutex_unlock(&st->lock); in ad5592r_read_raw()
401 dev_dbg(st->dev, "Channel %u read: 0x%04hX\n", in ad5592r_read_raw()
407 *val = ad5592r_get_vref(st); in ad5592r_read_raw()
416 mutex_lock(&st->lock); in ad5592r_read_raw()
419 mult = !!(st->cached_gp_ctrl & in ad5592r_read_raw()
422 mult = !!(st->cached_gp_ctrl & in ad5592r_read_raw()
425 mutex_unlock(&st->lock); in ad5592r_read_raw()
433 ret = ad5592r_get_vref(st); in ad5592r_read_raw()
435 mutex_lock(&st->lock); in ad5592r_read_raw()
437 if (st->cached_gp_ctrl & AD5592R_REG_CTRL_ADC_RANGE) in ad5592r_read_raw()
442 mutex_unlock(&st->lock); in ad5592r_read_raw()
475 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_show_scale_available() local
478 st->scale_avail[0][0], st->scale_avail[0][1], in ad5592r_show_scale_available()
479 st->scale_avail[1][0], st->scale_avail[1][1]); in ad5592r_show_scale_available()
495 chan->indexed = 1; in ad5592r_setup_channel()
508 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_alloc_channels() local
510 num_channels = st->num_channels; in ad5592r_alloc_channels()
516 device_for_each_child_node(st->dev, child) { in ad5592r_alloc_channels()
518 if (ret || reg >= ARRAY_SIZE(st->channel_modes)) in ad5592r_alloc_channels()
523 st->channel_modes[reg] = tmp; in ad5592r_alloc_channels()
527 st->channel_offstate[reg] = tmp; in ad5592r_alloc_channels()
530 channels = devm_kcalloc(st->dev, in ad5592r_alloc_channels()
531 1 + 2 * num_channels, sizeof(*channels), in ad5592r_alloc_channels()
537 switch (st->channel_modes[i]) { in ad5592r_alloc_channels()
577 static void ad5592r_init_scales(struct ad5592r_state *st, int vref_mV) in ad5592r_init_scales() argument
581 st->scale_avail[0][0] = in ad5592r_init_scales()
582 div_s64_rem(tmp, 1000000000LL, &st->scale_avail[0][1]); in ad5592r_init_scales()
583 st->scale_avail[1][0] = in ad5592r_init_scales()
584 div_s64_rem(tmp * 2, 1000000000LL, &st->scale_avail[1][1]); in ad5592r_init_scales()
591 struct ad5592r_state *st; in ad5592r_probe() local
594 iio_dev = devm_iio_device_alloc(dev, sizeof(*st)); in ad5592r_probe()
598 st = iio_priv(iio_dev); in ad5592r_probe()
599 st->dev = dev; in ad5592r_probe()
600 st->ops = ops; in ad5592r_probe()
601 st->num_channels = 8; in ad5592r_probe()
604 st->reg = devm_regulator_get_optional(dev, "vref"); in ad5592r_probe()
605 if (IS_ERR(st->reg)) { in ad5592r_probe()
606 if ((PTR_ERR(st->reg) != -ENODEV) && dev_fwnode(dev)) in ad5592r_probe()
607 return PTR_ERR(st->reg); in ad5592r_probe()
609 st->reg = NULL; in ad5592r_probe()
611 ret = regulator_enable(st->reg); in ad5592r_probe()
620 mutex_init(&st->lock); in ad5592r_probe()
622 ad5592r_init_scales(st, ad5592r_get_vref(st)); in ad5592r_probe()
624 ret = ad5592r_reset(st); in ad5592r_probe()
628 ret = ops->reg_write(st, AD5592R_REG_PD, in ad5592r_probe()
629 (st->reg == NULL) ? AD5592R_REG_PD_EN_REF : 0); in ad5592r_probe()
637 ret = ad5592r_set_channel_modes(st); in ad5592r_probe()
645 ret = ad5592r_gpio_init(st); in ad5592r_probe()
655 ad5592r_reset_channel_modes(st); in ad5592r_probe()
658 if (st->reg) in ad5592r_probe()
659 regulator_disable(st->reg); in ad5592r_probe()
668 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_remove() local
671 ad5592r_reset_channel_modes(st); in ad5592r_remove()
672 ad5592r_gpio_cleanup(st); in ad5592r_remove()
674 if (st->reg) in ad5592r_remove()
675 regulator_disable(st->reg); in ad5592r_remove()