Lines Matching 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()
131 static int ad5592r_gpio_init(struct ad5592r_state *st) in ad5592r_gpio_init() argument
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()
154 static void ad5592r_gpio_cleanup(struct ad5592r_state *st) in ad5592r_gpio_cleanup() argument
156 if (st->gpio_map) in ad5592r_gpio_cleanup()
157 gpiochip_remove(&st->gpiochip); in ad5592r_gpio_cleanup()
160 static int ad5592r_reset(struct ad5592r_state *st) in ad5592r_reset() argument
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()
183 static int ad5592r_get_vref(struct ad5592r_state *st) in ad5592r_get_vref() argument
187 if (st->reg) { in ad5592r_get_vref()
188 ret = regulator_get_voltage(st->reg); in ad5592r_get_vref()
198 static int ad5592r_set_channel_modes(struct ad5592r_state *st) in ad5592r_set_channel_modes() argument
200 const struct ad5592r_rw_ops *ops = st->ops; in ad5592r_set_channel_modes()
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()
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()
288 mutex_unlock(&st->lock); in ad5592r_set_channel_modes()
292 static int ad5592r_reset_channel_modes(struct ad5592r_state *st) in ad5592r_reset_channel_modes() argument
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()
299 return ad5592r_set_channel_modes(st); in ad5592r_reset_channel_modes()
305 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_write_raw() local
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()
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()
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()
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()
379 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_read_raw() local
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()
393 dev_err(st->dev, "Error while reading channel %u\n", 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()
412 *val = ad5592r_get_vref(st); in ad5592r_read_raw()
421 mutex_lock(&st->lock); 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()
438 ret = ad5592r_get_vref(st); 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()
447 mutex_unlock(&st->lock); in ad5592r_read_raw()
480 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_show_scale_available() local
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()
513 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_alloc_channels() local
515 num_channels = st->num_channels; in ad5592r_alloc_channels()
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()
532 st->channel_offstate[reg] = tmp; in ad5592r_alloc_channels()
535 channels = devm_kcalloc(st->dev, in ad5592r_alloc_channels()
542 switch (st->channel_modes[i]) { in ad5592r_alloc_channels()
582 static void ad5592r_init_scales(struct ad5592r_state *st, int vref_mV) in ad5592r_init_scales() argument
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()
596 struct ad5592r_state *st; in ad5592r_probe() local
599 iio_dev = devm_iio_device_alloc(dev, sizeof(*st)); in ad5592r_probe()
603 st = iio_priv(iio_dev); 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()
625 mutex_init(&st->lock); in ad5592r_probe()
627 ad5592r_init_scales(st, ad5592r_get_vref(st)); in ad5592r_probe()
629 ret = ad5592r_reset(st); 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()
642 ret = ad5592r_set_channel_modes(st); in ad5592r_probe()
650 ret = ad5592r_gpio_init(st); in ad5592r_probe()
660 ad5592r_reset_channel_modes(st); in ad5592r_probe()
663 if (st->reg) in ad5592r_probe()
664 regulator_disable(st->reg); in ad5592r_probe()
673 struct ad5592r_state *st = iio_priv(iio_dev); in ad5592r_remove() local
676 ad5592r_reset_channel_modes(st); in ad5592r_remove()
677 ad5592r_gpio_cleanup(st); in ad5592r_remove()
679 if (st->reg) in ad5592r_remove()
680 regulator_disable(st->reg); in ad5592r_remove()