Lines Matching full:st

174 	struct ad74413r_state *st = context;  in ad74413r_reg_write()  local
176 ad74413r_format_reg_write(reg, val, st->reg_tx_buf); in ad74413r_reg_write()
178 return spi_write(st->spi, st->reg_tx_buf, AD74413R_FRAME_SIZE); in ad74413r_reg_write()
181 static int ad74413r_crc_check(struct ad74413r_state *st, u8 *buf) in ad74413r_crc_check() argument
186 dev_err(st->dev, "Bad CRC %02x for %02x%02x%02x\n", in ad74413r_crc_check()
196 struct ad74413r_state *st = context; in ad74413r_reg_read() local
199 .tx_buf = st->reg_tx_buf, in ad74413r_reg_read()
204 .rx_buf = st->reg_rx_buf, in ad74413r_reg_read()
211 st->reg_tx_buf); in ad74413r_reg_read()
213 ret = spi_sync_transfer(st->spi, reg_read_xfer, in ad74413r_reg_read()
218 ret = ad74413r_crc_check(st, st->reg_rx_buf); in ad74413r_reg_read()
222 *val = get_unaligned_be16(&st->reg_rx_buf[1]); in ad74413r_reg_read()
234 static int ad74413r_set_gpo_config(struct ad74413r_state *st, in ad74413r_set_gpo_config() argument
237 return regmap_update_bits(st->regmap, AD74413R_REG_GPO_CONFIG_X(offset), in ad74413r_set_gpo_config()
248 static int ad74413r_set_comp_debounce(struct ad74413r_state *st, in ad74413r_set_comp_debounce() argument
261 return regmap_update_bits(st->regmap, in ad74413r_set_comp_debounce()
267 static int ad74413r_set_comp_drive_strength(struct ad74413r_state *st, in ad74413r_set_comp_drive_strength() argument
273 return regmap_update_bits(st->regmap, AD74413R_REG_DIN_CONFIG_X(offset), in ad74413r_set_comp_drive_strength()
282 struct ad74413r_state *st = gpiochip_get_data(chip); in ad74413r_gpio_set() local
283 unsigned int real_offset = st->gpo_gpio_offsets[offset]; in ad74413r_gpio_set()
286 ret = ad74413r_set_gpo_config(st, real_offset, in ad74413r_gpio_set()
291 regmap_update_bits(st->regmap, AD74413R_REG_GPO_CONFIG_X(real_offset), in ad74413r_gpio_set()
300 struct ad74413r_state *st = gpiochip_get_data(chip); in ad74413r_gpio_set_multiple() local
307 unsigned int real_offset = st->gpo_gpio_offsets[offset]; in ad74413r_gpio_set_multiple()
309 ret = ad74413r_set_gpo_config(st, real_offset, in ad74413r_gpio_set_multiple()
319 regmap_update_bits(st->regmap, AD74413R_REG_GPO_PAR_DATA, in ad74413r_gpio_set_multiple()
325 struct ad74413r_state *st = gpiochip_get_data(chip); in ad74413r_gpio_get() local
326 unsigned int real_offset = st->comp_gpio_offsets[offset]; in ad74413r_gpio_get()
330 ret = regmap_read(st->regmap, AD74413R_REG_DIN_COMP_OUT, &status); in ad74413r_gpio_get()
343 struct ad74413r_state *st = gpiochip_get_data(chip); in ad74413r_gpio_get_multiple() local
348 ret = regmap_read(st->regmap, AD74413R_REG_DIN_COMP_OUT, &val); in ad74413r_gpio_get_multiple()
353 unsigned int real_offset = st->comp_gpio_offsets[offset]; in ad74413r_gpio_get_multiple()
377 struct ad74413r_state *st = gpiochip_get_data(chip); in ad74413r_gpio_set_gpo_config() local
378 unsigned int real_offset = st->gpo_gpio_offsets[offset]; in ad74413r_gpio_set_gpo_config()
382 return ad74413r_set_gpo_config(st, real_offset, in ad74413r_gpio_set_gpo_config()
385 return ad74413r_set_gpo_config(st, real_offset, in ad74413r_gpio_set_gpo_config()
396 struct ad74413r_state *st = gpiochip_get_data(chip); in ad74413r_gpio_set_comp_config() local
397 unsigned int real_offset = st->comp_gpio_offsets[offset]; in ad74413r_gpio_set_comp_config()
401 return ad74413r_set_comp_debounce(st, real_offset, in ad74413r_gpio_set_comp_config()
408 static int ad74413r_reset(struct ad74413r_state *st) in ad74413r_reset() argument
412 if (st->reset_gpio) { in ad74413r_reset()
413 gpiod_set_value_cansleep(st->reset_gpio, 1); in ad74413r_reset()
415 gpiod_set_value_cansleep(st->reset_gpio, 0); in ad74413r_reset()
419 ret = regmap_write(st->regmap, AD74413R_REG_CMD_KEY, in ad74413r_reset()
424 return regmap_write(st->regmap, AD74413R_REG_CMD_KEY, in ad74413r_reset()
428 static int ad74413r_set_channel_dac_code(struct ad74413r_state *st, in ad74413r_set_channel_dac_code() argument
436 return regmap_multi_reg_write(st->regmap, reg_seq, 2); in ad74413r_set_channel_dac_code()
439 static int ad74413r_set_channel_function(struct ad74413r_state *st, in ad74413r_set_channel_function() argument
444 ret = regmap_update_bits(st->regmap, in ad74413r_set_channel_function()
452 ret = ad74413r_set_channel_dac_code(st, channel, 0); in ad74413r_set_channel_function()
459 ret = regmap_update_bits(st->regmap, in ad74413r_set_channel_function()
469 ret = regmap_set_bits(st->regmap, in ad74413r_set_channel_function()
476 static int ad74413r_set_adc_conv_seq(struct ad74413r_state *st, in ad74413r_set_adc_conv_seq() argument
485 ret = regmap_write_bits(st->regmap, AD74413R_REG_ADC_CONV_CTRL, in ad74413r_set_adc_conv_seq()
499 static int ad74413r_set_adc_channel_enable(struct ad74413r_state *st, in ad74413r_set_adc_channel_enable() argument
503 return regmap_update_bits(st->regmap, AD74413R_REG_ADC_CONV_CTRL, in ad74413r_set_adc_channel_enable()
508 static int ad74413r_get_adc_range(struct ad74413r_state *st, in ad74413r_get_adc_range() argument
514 ret = regmap_read(st->regmap, AD74413R_REG_ADC_CONFIG_X(channel), val); in ad74413r_get_adc_range()
523 static int ad74413r_get_adc_rejection(struct ad74413r_state *st, in ad74413r_get_adc_rejection() argument
529 ret = regmap_read(st->regmap, AD74413R_REG_ADC_CONFIG_X(channel), val); in ad74413r_get_adc_rejection()
538 static int ad74413r_set_adc_rejection(struct ad74413r_state *st, in ad74413r_set_adc_rejection() argument
542 return regmap_update_bits(st->regmap, in ad74413r_set_adc_rejection()
549 static int ad74413r_rejection_to_rate(struct ad74413r_state *st, in ad74413r_rejection_to_rate() argument
566 dev_err(st->dev, "ADC rejection invalid\n"); in ad74413r_rejection_to_rate()
571 static int ad74413r_rate_to_rejection(struct ad74413r_state *st, in ad74413r_rate_to_rejection() argument
588 dev_err(st->dev, "ADC rate invalid\n"); in ad74413r_rate_to_rejection()
593 static int ad74413r_range_to_voltage_range(struct ad74413r_state *st, in ad74413r_range_to_voltage_range() argument
608 dev_err(st->dev, "ADC range invalid\n"); in ad74413r_range_to_voltage_range()
613 static int ad74413r_range_to_voltage_offset(struct ad74413r_state *st, in ad74413r_range_to_voltage_offset() argument
626 dev_err(st->dev, "ADC range invalid\n"); in ad74413r_range_to_voltage_offset()
631 static int ad74413r_range_to_voltage_offset_raw(struct ad74413r_state *st, in ad74413r_range_to_voltage_offset_raw() argument
646 dev_err(st->dev, "ADC range invalid\n"); in ad74413r_range_to_voltage_offset_raw()
651 static int ad74413r_get_output_voltage_scale(struct ad74413r_state *st, in ad74413r_get_output_voltage_scale() argument
660 static int ad74413r_get_output_current_scale(struct ad74413r_state *st, in ad74413r_get_output_current_scale() argument
663 *val = regulator_get_voltage(st->refin_reg); in ad74413r_get_output_current_scale()
664 *val2 = st->sense_resistor_ohms * AD74413R_DAC_CODE_MAX * 1000; in ad74413r_get_output_current_scale()
669 static int ad74413r_get_input_voltage_scale(struct ad74413r_state *st, in ad74413r_get_input_voltage_scale() argument
676 ret = ad74413r_get_adc_range(st, channel, &range); in ad74413r_get_input_voltage_scale()
680 ret = ad74413r_range_to_voltage_range(st, range, val); in ad74413r_get_input_voltage_scale()
689 static int ad74413r_get_input_voltage_offset(struct ad74413r_state *st, in ad74413r_get_input_voltage_offset() argument
695 ret = ad74413r_get_adc_range(st, channel, &range); in ad74413r_get_input_voltage_offset()
699 ret = ad74413r_range_to_voltage_offset_raw(st, range, val); in ad74413r_get_input_voltage_offset()
706 static int ad74413r_get_input_current_scale(struct ad74413r_state *st, in ad74413r_get_input_current_scale() argument
713 ret = ad74413r_get_adc_range(st, channel, &range); in ad74413r_get_input_current_scale()
717 ret = ad74413r_range_to_voltage_range(st, range, val); in ad74413r_get_input_current_scale()
721 *val2 = AD74413R_ADC_RESULT_MAX * st->sense_resistor_ohms; in ad74413r_get_input_current_scale()
726 static int ad74413r_get_input_current_offset(struct ad74413r_state *st, in ad74413r_get_input_current_offset() argument
734 ret = ad74413r_get_adc_range(st, channel, &range); in ad74413r_get_input_current_offset()
738 ret = ad74413r_range_to_voltage_range(st, range, &voltage_range); in ad74413r_get_input_current_offset()
742 ret = ad74413r_range_to_voltage_offset(st, range, &voltage_offset); in ad74413r_get_input_current_offset()
751 static int ad74413r_get_adc_rate(struct ad74413r_state *st, in ad74413r_get_adc_rate() argument
757 ret = ad74413r_get_adc_rejection(st, channel, &rejection); in ad74413r_get_adc_rate()
761 ret = ad74413r_rejection_to_rate(st, rejection, val); in ad74413r_get_adc_rate()
768 static int ad74413r_set_adc_rate(struct ad74413r_state *st, in ad74413r_set_adc_rate() argument
774 ret = ad74413r_rate_to_rejection(st, val, &rejection); in ad74413r_set_adc_rate()
778 return ad74413r_set_adc_rejection(st, channel, rejection); in ad74413r_set_adc_rate()
785 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_trigger_handler() local
786 u8 *rx_buf = st->adc_samples_buf.rx_buf; in ad74413r_trigger_handler()
790 ret = spi_sync(st->spi, &st->adc_samples_msg); in ad74413r_trigger_handler()
794 for (i = 0; i < st->adc_active_channels; i++) in ad74413r_trigger_handler()
795 ad74413r_crc_check(st, &rx_buf[i * AD74413R_FRAME_SIZE]); in ad74413r_trigger_handler()
797 iio_push_to_buffers_with_timestamp(indio_dev, &st->adc_samples_buf, in ad74413r_trigger_handler()
809 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_adc_data_interrupt() local
812 iio_trigger_poll(st->trig); in ad74413r_adc_data_interrupt()
814 complete(&st->adc_data_completion); in ad74413r_adc_data_interrupt()
819 static int _ad74413r_get_single_adc_result(struct ad74413r_state *st, in _ad74413r_get_single_adc_result() argument
825 reinit_completion(&st->adc_data_completion); in _ad74413r_get_single_adc_result()
827 ret = ad74413r_set_adc_channel_enable(st, channel, true); in _ad74413r_get_single_adc_result()
831 ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_SINGLE); in _ad74413r_get_single_adc_result()
835 ret = wait_for_completion_timeout(&st->adc_data_completion, in _ad74413r_get_single_adc_result()
842 ret = regmap_read(st->regmap, AD74413R_REG_ADC_RESULT_X(channel), in _ad74413r_get_single_adc_result()
847 ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF); in _ad74413r_get_single_adc_result()
851 ret = ad74413r_set_adc_channel_enable(st, channel, false); in _ad74413r_get_single_adc_result()
863 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_get_single_adc_result() local
870 mutex_lock(&st->lock); in ad74413r_get_single_adc_result()
871 ret = _ad74413r_get_single_adc_result(st, channel, val); in ad74413r_get_single_adc_result()
872 mutex_unlock(&st->lock); in ad74413r_get_single_adc_result()
891 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_update_scan_mode() local
892 struct spi_transfer *xfer = st->adc_samples_xfer; in ad74413r_update_scan_mode()
893 u8 *rx_buf = st->adc_samples_buf.rx_buf; in ad74413r_update_scan_mode()
894 u8 *tx_buf = st->adc_samples_tx_buf; in ad74413r_update_scan_mode()
898 mutex_lock(&st->lock); in ad74413r_update_scan_mode()
900 spi_message_init(&st->adc_samples_msg); in ad74413r_update_scan_mode()
901 st->adc_active_channels = 0; in ad74413r_update_scan_mode()
904 ret = ad74413r_set_adc_channel_enable(st, channel, false); in ad74413r_update_scan_mode()
926 ret = ad74413r_set_adc_channel_enable(st, channel, true); in ad74413r_update_scan_mode()
930 st->adc_active_channels++; in ad74413r_update_scan_mode()
932 if (xfer == st->adc_samples_xfer) in ad74413r_update_scan_mode()
945 spi_message_add_tail(xfer, &st->adc_samples_msg); in ad74413r_update_scan_mode()
948 if (xfer != st->adc_samples_xfer) in ad74413r_update_scan_mode()
958 spi_message_add_tail(xfer, &st->adc_samples_msg); in ad74413r_update_scan_mode()
961 mutex_unlock(&st->lock); in ad74413r_update_scan_mode()
968 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_buffer_postenable() local
970 return ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_CONTINUOUS); in ad74413r_buffer_postenable()
975 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_buffer_predisable() local
977 return ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF); in ad74413r_buffer_predisable()
984 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_read_raw() local
991 return ad74413r_get_output_voltage_scale(st, in ad74413r_read_raw()
994 return ad74413r_get_input_voltage_scale(st, in ad74413r_read_raw()
998 return ad74413r_get_output_current_scale(st, in ad74413r_read_raw()
1001 return ad74413r_get_input_current_scale(st, in ad74413r_read_raw()
1009 return ad74413r_get_input_voltage_offset(st, in ad74413r_read_raw()
1012 return ad74413r_get_input_current_offset(st, in ad74413r_read_raw()
1036 return ad74413r_get_adc_rate(st, chan->channel, val); in ad74413r_read_raw()
1046 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_write_raw() local
1054 dev_err(st->dev, "Invalid DAC code\n"); in ad74413r_write_raw()
1058 return ad74413r_set_channel_dac_code(st, chan->channel, val); in ad74413r_write_raw()
1060 return ad74413r_set_adc_rate(st, chan->channel, val); in ad74413r_write_raw()
1071 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_read_avail() local
1075 if (st->chip_info->hart_support) { in ad74413r_read_avail()
1198 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_parse_channel_config() local
1205 dev_err(st->dev, "Failed to read channel reg: %d\n", ret); in ad74413r_parse_channel_config()
1210 dev_err(st->dev, "Channel index %u is too large\n", index); in ad74413r_parse_channel_config()
1214 config = &st->channel_configs[index]; in ad74413r_parse_channel_config()
1216 dev_err(st->dev, "Channel %u already initialized\n", index); in ad74413r_parse_channel_config()
1224 dev_err(st->dev, "Invalid channel function %u\n", config->func); in ad74413r_parse_channel_config()
1228 if (!st->chip_info->hart_support && in ad74413r_parse_channel_config()
1231 dev_err(st->dev, "Unsupported HART function %u\n", config->func); in ad74413r_parse_channel_config()
1237 st->num_comparator_gpios++; in ad74413r_parse_channel_config()
1246 st->num_gpo_gpios++; in ad74413r_parse_channel_config()
1257 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_parse_channel_configs() local
1260 device_for_each_child_node_scoped(st->dev, channel_node) { in ad74413r_parse_channel_configs()
1271 struct ad74413r_state *st = iio_priv(indio_dev); in ad74413r_setup_channels() local
1277 channels = devm_kcalloc(st->dev, sizeof(*channels), in ad74413r_setup_channels()
1285 config = &st->channel_configs[i]; in ad74413r_setup_channels()
1301 ret = ad74413r_set_channel_function(st, i, config->func); in ad74413r_setup_channels()
1311 static int ad74413r_setup_gpios(struct ad74413r_state *st) in ad74413r_setup_gpios() argument
1322 config = &st->channel_configs[i]; in ad74413r_setup_gpios()
1328 st->gpo_gpio_offsets[gpo_gpio_i++] = i; in ad74413r_setup_gpios()
1333 st->comp_gpio_offsets[comp_gpio_i++] = i; in ad74413r_setup_gpios()
1336 ret = ad74413r_set_comp_drive_strength(st, i, strength); in ad74413r_setup_gpios()
1341 ret = ad74413r_set_gpo_config(st, i, gpo_config); in ad74413r_setup_gpios()
1356 struct ad74413r_state *st; in ad74413r_probe() local
1360 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in ad74413r_probe()
1364 st = iio_priv(indio_dev); in ad74413r_probe()
1366 st->spi = spi; in ad74413r_probe()
1367 st->dev = &spi->dev; in ad74413r_probe()
1368 st->chip_info = spi_get_device_match_data(spi); in ad74413r_probe()
1369 if (!st->chip_info) in ad74413r_probe()
1372 mutex_init(&st->lock); in ad74413r_probe()
1373 init_completion(&st->adc_data_completion); in ad74413r_probe()
1375 st->regmap = devm_regmap_init(st->dev, NULL, st, in ad74413r_probe()
1377 if (IS_ERR(st->regmap)) in ad74413r_probe()
1378 return PTR_ERR(st->regmap); in ad74413r_probe()
1380 st->reset_gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW); in ad74413r_probe()
1381 if (IS_ERR(st->reset_gpio)) in ad74413r_probe()
1382 return PTR_ERR(st->reset_gpio); in ad74413r_probe()
1384 st->refin_reg = devm_regulator_get(st->dev, "refin"); in ad74413r_probe()
1385 if (IS_ERR(st->refin_reg)) in ad74413r_probe()
1386 return dev_err_probe(st->dev, PTR_ERR(st->refin_reg), in ad74413r_probe()
1389 ret = regulator_enable(st->refin_reg); in ad74413r_probe()
1393 ret = devm_add_action_or_reset(st->dev, ad74413r_regulator_disable, in ad74413r_probe()
1394 st->refin_reg); in ad74413r_probe()
1398 st->sense_resistor_ohms = 100000000; in ad74413r_probe()
1399 device_property_read_u32(st->dev, "shunt-resistor-micro-ohms", in ad74413r_probe()
1400 &st->sense_resistor_ohms); in ad74413r_probe()
1401 st->sense_resistor_ohms /= 1000000; in ad74413r_probe()
1403 st->trig = devm_iio_trigger_alloc(st->dev, "%s-dev%d", in ad74413r_probe()
1404 st->chip_info->name, iio_device_id(indio_dev)); in ad74413r_probe()
1405 if (!st->trig) in ad74413r_probe()
1408 st->trig->ops = &ad74413r_trigger_ops; in ad74413r_probe()
1409 iio_trigger_set_drvdata(st->trig, st); in ad74413r_probe()
1411 ret = devm_iio_trigger_register(st->dev, st->trig); in ad74413r_probe()
1415 indio_dev->name = st->chip_info->name; in ad74413r_probe()
1418 indio_dev->trig = iio_trigger_get(st->trig); in ad74413r_probe()
1420 ret = ad74413r_reset(st); in ad74413r_probe()
1432 ret = ad74413r_setup_gpios(st); in ad74413r_probe()
1436 if (st->num_gpo_gpios) { in ad74413r_probe()
1437 st->gpo_gpiochip.owner = THIS_MODULE; in ad74413r_probe()
1438 st->gpo_gpiochip.label = st->chip_info->name; in ad74413r_probe()
1439 st->gpo_gpiochip.base = -1; in ad74413r_probe()
1440 st->gpo_gpiochip.ngpio = st->num_gpo_gpios; in ad74413r_probe()
1441 st->gpo_gpiochip.parent = st->dev; in ad74413r_probe()
1442 st->gpo_gpiochip.can_sleep = true; in ad74413r_probe()
1443 st->gpo_gpiochip.set = ad74413r_gpio_set; in ad74413r_probe()
1444 st->gpo_gpiochip.set_multiple = ad74413r_gpio_set_multiple; in ad74413r_probe()
1445 st->gpo_gpiochip.set_config = ad74413r_gpio_set_gpo_config; in ad74413r_probe()
1446 st->gpo_gpiochip.get_direction = in ad74413r_probe()
1449 ret = devm_gpiochip_add_data(st->dev, &st->gpo_gpiochip, st); in ad74413r_probe()
1454 if (st->num_comparator_gpios) { in ad74413r_probe()
1455 st->comp_gpiochip.owner = THIS_MODULE; in ad74413r_probe()
1456 st->comp_gpiochip.label = st->chip_info->name; in ad74413r_probe()
1457 st->comp_gpiochip.base = -1; in ad74413r_probe()
1458 st->comp_gpiochip.ngpio = st->num_comparator_gpios; in ad74413r_probe()
1459 st->comp_gpiochip.parent = st->dev; in ad74413r_probe()
1460 st->comp_gpiochip.can_sleep = true; in ad74413r_probe()
1461 st->comp_gpiochip.get = ad74413r_gpio_get; in ad74413r_probe()
1462 st->comp_gpiochip.get_multiple = ad74413r_gpio_get_multiple; in ad74413r_probe()
1463 st->comp_gpiochip.set_config = ad74413r_gpio_set_comp_config; in ad74413r_probe()
1464 st->comp_gpiochip.get_direction = in ad74413r_probe()
1467 ret = devm_gpiochip_add_data(st->dev, &st->comp_gpiochip, st); in ad74413r_probe()
1472 ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF); in ad74413r_probe()
1476 ret = devm_request_irq(st->dev, spi->irq, ad74413r_adc_data_interrupt, in ad74413r_probe()
1477 0, st->chip_info->name, indio_dev); in ad74413r_probe()
1479 return dev_err_probe(st->dev, ret, "Failed to request irq\n"); in ad74413r_probe()
1481 ret = devm_iio_triggered_buffer_setup(st->dev, indio_dev, in ad74413r_probe()
1488 return devm_iio_device_register(st->dev, indio_dev); in ad74413r_probe()