Lines Matching +full:gpo +full:- +full:comparator
1 // SPDX-License-Identifier: GPL-2.0
26 #include <dt-bindings/iio/addac/adi,ad74413r.h>
64 * Synchronize consecutive operations when doing a one-shot
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()
186 dev_err(st->dev, "Bad CRC %02x for %02x%02x%02x\n", in ad74413r_crc_check()
188 return -EINVAL; in ad74413r_crc_check()
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()
237 return regmap_update_bits(st->regmap, AD74413R_REG_GPO_CONFIG_X(offset), in ad74413r_set_gpo_config()
252 unsigned int val = AD74413R_DIN_DEBOUNCE_LEN - 1; in ad74413r_set_comp_debounce()
261 return regmap_update_bits(st->regmap, in ad74413r_set_comp_debounce()
273 return regmap_update_bits(st->regmap, AD74413R_REG_DIN_CONFIG_X(offset), in ad74413r_set_comp_drive_strength()
283 unsigned int real_offset = st->gpo_gpio_offsets[offset]; in ad74413r_gpio_set()
291 regmap_update_bits(st->regmap, AD74413R_REG_GPO_CONFIG_X(real_offset), in ad74413r_gpio_set()
306 for_each_set_bit(offset, mask, chip->ngpio) { in ad74413r_gpio_set_multiple()
307 unsigned int real_offset = st->gpo_gpio_offsets[offset]; in ad74413r_gpio_set_multiple()
319 regmap_update_bits(st->regmap, AD74413R_REG_GPO_PAR_DATA, in ad74413r_gpio_set_multiple()
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()
348 ret = regmap_read(st->regmap, AD74413R_REG_DIN_COMP_OUT, &val); in ad74413r_gpio_get_multiple()
352 for_each_set_bit(offset, mask, chip->ngpio) { in ad74413r_gpio_get_multiple()
353 unsigned int real_offset = st->comp_gpio_offsets[offset]; in ad74413r_gpio_get_multiple()
378 unsigned int real_offset = st->gpo_gpio_offsets[offset]; in ad74413r_gpio_set_gpo_config()
388 return -ENOTSUPP; in ad74413r_gpio_set_gpo_config()
397 unsigned int real_offset = st->comp_gpio_offsets[offset]; in ad74413r_gpio_set_comp_config()
404 return -ENOTSUPP; in ad74413r_gpio_set_comp_config()
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()
436 return regmap_multi_reg_write(st->regmap, reg_seq, 2); in ad74413r_set_channel_dac_code()
444 ret = regmap_update_bits(st->regmap, in ad74413r_set_channel_function()
451 ret = regmap_set_bits(st->regmap, in ad74413r_set_channel_function()
467 ret = regmap_write_bits(st->regmap, AD74413R_REG_ADC_CONV_CTRL, in ad74413r_set_adc_conv_seq()
485 return regmap_update_bits(st->regmap, AD74413R_REG_ADC_CONV_CTRL, in ad74413r_set_adc_channel_enable()
496 ret = regmap_read(st->regmap, AD74413R_REG_ADC_CONFIG_X(channel), val); in ad74413r_get_adc_range()
511 ret = regmap_read(st->regmap, AD74413R_REG_ADC_CONFIG_X(channel), val); in ad74413r_get_adc_rejection()
524 return regmap_update_bits(st->regmap, in ad74413r_set_adc_rejection()
548 dev_err(st->dev, "ADC rejection invalid\n"); in ad74413r_rejection_to_rate()
549 return -EINVAL; in ad74413r_rejection_to_rate()
570 dev_err(st->dev, "ADC rate invalid\n"); in ad74413r_rate_to_rejection()
571 return -EINVAL; in ad74413r_rate_to_rejection()
590 dev_err(st->dev, "ADC range invalid\n"); in ad74413r_range_to_voltage_range()
591 return -EINVAL; in ad74413r_range_to_voltage_range()
605 *val = -2500; in ad74413r_range_to_voltage_offset()
608 dev_err(st->dev, "ADC range invalid\n"); in ad74413r_range_to_voltage_offset()
609 return -EINVAL; in ad74413r_range_to_voltage_offset()
622 *val = -((int)AD74413R_ADC_RESULT_MAX); in ad74413r_range_to_voltage_offset_raw()
625 *val = -((int)AD74413R_ADC_RESULT_MAX / 2); in ad74413r_range_to_voltage_offset_raw()
628 dev_err(st->dev, "ADC range invalid\n"); in ad74413r_range_to_voltage_offset_raw()
629 return -EINVAL; in ad74413r_range_to_voltage_offset_raw()
645 *val = regulator_get_voltage(st->refin_reg); in ad74413r_get_output_current_scale()
646 *val2 = st->sense_resistor_ohms * AD74413R_DAC_CODE_MAX * 1000; in ad74413r_get_output_current_scale()
703 *val2 = AD74413R_ADC_RESULT_MAX * st->sense_resistor_ohms; in ad74413r_get_input_current_scale()
766 struct iio_dev *indio_dev = pf->indio_dev; in ad74413r_trigger_handler()
768 u8 *rx_buf = st->adc_samples_buf.rx_buf; in ad74413r_trigger_handler()
772 ret = spi_sync(st->spi, &st->adc_samples_msg); in ad74413r_trigger_handler()
776 for (i = 0; i < st->adc_active_channels; i++) in ad74413r_trigger_handler()
779 iio_push_to_buffers_with_timestamp(indio_dev, &st->adc_samples_buf, in ad74413r_trigger_handler()
783 iio_trigger_notify_done(indio_dev->trig); in ad74413r_trigger_handler()
794 iio_trigger_poll(st->trig); in ad74413r_adc_data_interrupt()
796 complete(&st->adc_data_completion); in ad74413r_adc_data_interrupt()
807 reinit_completion(&st->adc_data_completion); in _ad74413r_get_single_adc_result()
817 ret = wait_for_completion_timeout(&st->adc_data_completion, in _ad74413r_get_single_adc_result()
820 ret = -ETIMEDOUT; in _ad74413r_get_single_adc_result()
824 ret = regmap_read(st->regmap, AD74413R_REG_ADC_RESULT_X(channel), in _ad74413r_get_single_adc_result()
852 mutex_lock(&st->lock); in ad74413r_get_single_adc_result()
854 mutex_unlock(&st->lock); in ad74413r_get_single_adc_result()
864 adc_result = AD74413R_ADC_RESULT_MAX - 1; in ad74413r_adc_to_resistance_result()
867 AD74413R_ADC_RESULT_MAX - adc_result); in ad74413r_adc_to_resistance_result()
874 struct spi_transfer *xfer = st->adc_samples_xfer; in ad74413r_update_scan_mode()
875 u8 *rx_buf = st->adc_samples_buf.rx_buf; in ad74413r_update_scan_mode()
876 u8 *tx_buf = st->adc_samples_tx_buf; in ad74413r_update_scan_mode()
878 int ret = -EINVAL; in ad74413r_update_scan_mode()
880 mutex_lock(&st->lock); in ad74413r_update_scan_mode()
882 spi_message_init(&st->adc_samples_msg); in ad74413r_update_scan_mode()
883 st->adc_active_channels = 0; in ad74413r_update_scan_mode()
912 st->adc_active_channels++; in ad74413r_update_scan_mode()
914 if (xfer == st->adc_samples_xfer) in ad74413r_update_scan_mode()
915 xfer->rx_buf = NULL; in ad74413r_update_scan_mode()
917 xfer->rx_buf = rx_buf; in ad74413r_update_scan_mode()
919 xfer->tx_buf = tx_buf; in ad74413r_update_scan_mode()
920 xfer->len = AD74413R_FRAME_SIZE; in ad74413r_update_scan_mode()
921 xfer->cs_change = 1; in ad74413r_update_scan_mode()
927 spi_message_add_tail(xfer, &st->adc_samples_msg); in ad74413r_update_scan_mode()
930 if (xfer != st->adc_samples_xfer) in ad74413r_update_scan_mode()
935 xfer->rx_buf = rx_buf; in ad74413r_update_scan_mode()
936 xfer->tx_buf = NULL; in ad74413r_update_scan_mode()
937 xfer->len = AD74413R_FRAME_SIZE; in ad74413r_update_scan_mode()
938 xfer->cs_change = 0; in ad74413r_update_scan_mode()
940 spi_message_add_tail(xfer, &st->adc_samples_msg); in ad74413r_update_scan_mode()
943 mutex_unlock(&st->lock); in ad74413r_update_scan_mode()
970 switch (chan->type) { in ad74413r_read_raw()
972 if (chan->output) in ad74413r_read_raw()
977 chan->channel, val, val2); in ad74413r_read_raw()
979 if (chan->output) in ad74413r_read_raw()
984 chan->channel, val, val2); in ad74413r_read_raw()
986 return -EINVAL; in ad74413r_read_raw()
989 switch (chan->type) { in ad74413r_read_raw()
992 chan->channel, val); in ad74413r_read_raw()
995 chan->channel, val); in ad74413r_read_raw()
997 return -EINVAL; in ad74413r_read_raw()
1000 if (chan->output) in ad74413r_read_raw()
1001 return -EINVAL; in ad74413r_read_raw()
1003 return ad74413r_get_single_adc_result(indio_dev, chan->channel, in ad74413r_read_raw()
1008 ret = ad74413r_get_single_adc_result(indio_dev, chan->channel, in ad74413r_read_raw()
1018 return ad74413r_get_adc_rate(st, chan->channel, val); in ad74413r_read_raw()
1020 return -EINVAL; in ad74413r_read_raw()
1032 if (!chan->output) in ad74413r_write_raw()
1033 return -EINVAL; in ad74413r_write_raw()
1036 dev_err(st->dev, "Invalid DAC code\n"); in ad74413r_write_raw()
1037 return -EINVAL; in ad74413r_write_raw()
1040 return ad74413r_set_channel_dac_code(st, chan->channel, val); in ad74413r_write_raw()
1042 return ad74413r_set_adc_rate(st, chan->channel, val); in ad74413r_write_raw()
1044 return -EINVAL; in ad74413r_write_raw()
1057 if (st->chip_info->hart_support) { in ad74413r_read_avail()
1067 return -EINVAL; in ad74413r_read_avail()
1187 dev_err(st->dev, "Failed to read channel reg: %d\n", ret); in ad74413r_parse_channel_config()
1192 dev_err(st->dev, "Channel index %u is too large\n", index); in ad74413r_parse_channel_config()
1193 return -EINVAL; in ad74413r_parse_channel_config()
1196 config = &st->channel_configs[index]; in ad74413r_parse_channel_config()
1197 if (config->initialized) { in ad74413r_parse_channel_config()
1198 dev_err(st->dev, "Channel %u already initialized\n", index); in ad74413r_parse_channel_config()
1199 return -EINVAL; in ad74413r_parse_channel_config()
1202 config->func = CH_FUNC_HIGH_IMPEDANCE; in ad74413r_parse_channel_config()
1203 fwnode_property_read_u32(channel_node, "adi,ch-func", &config->func); in ad74413r_parse_channel_config()
1205 if (config->func < CH_FUNC_MIN || config->func > CH_FUNC_MAX) { in ad74413r_parse_channel_config()
1206 dev_err(st->dev, "Invalid channel function %u\n", config->func); in ad74413r_parse_channel_config()
1207 return -EINVAL; in ad74413r_parse_channel_config()
1210 if (!st->chip_info->hart_support && in ad74413r_parse_channel_config()
1211 (config->func == CH_FUNC_CURRENT_INPUT_EXT_POWER_HART || in ad74413r_parse_channel_config()
1212 config->func == CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART)) { in ad74413r_parse_channel_config()
1213 dev_err(st->dev, "Unsupported HART function %u\n", config->func); in ad74413r_parse_channel_config()
1214 return -EINVAL; in ad74413r_parse_channel_config()
1217 if (config->func == CH_FUNC_DIGITAL_INPUT_LOGIC || in ad74413r_parse_channel_config()
1218 config->func == CH_FUNC_DIGITAL_INPUT_LOOP_POWER) in ad74413r_parse_channel_config()
1219 st->num_comparator_gpios++; in ad74413r_parse_channel_config()
1221 config->gpo_comparator = fwnode_property_read_bool(channel_node, in ad74413r_parse_channel_config()
1222 "adi,gpo-comparator"); in ad74413r_parse_channel_config()
1224 fwnode_property_read_u32(channel_node, "drive-strength-microamp", in ad74413r_parse_channel_config()
1225 &config->drive_strength); in ad74413r_parse_channel_config()
1227 if (!config->gpo_comparator) in ad74413r_parse_channel_config()
1228 st->num_gpo_gpios++; in ad74413r_parse_channel_config()
1230 indio_dev->num_channels += ad74413r_channels_map[config->func].num_channels; in ad74413r_parse_channel_config()
1232 config->initialized = true; in ad74413r_parse_channel_config()
1243 fwnode_for_each_available_child_node(dev_fwnode(st->dev), channel_node) { in ad74413r_parse_channel_configs()
1265 channels = devm_kcalloc(st->dev, sizeof(*channels), in ad74413r_setup_channels()
1266 indio_dev->num_channels, GFP_KERNEL); in ad74413r_setup_channels()
1268 return -ENOMEM; in ad74413r_setup_channels()
1270 indio_dev->channels = channels; in ad74413r_setup_channels()
1273 config = &st->channel_configs[i]; in ad74413r_setup_channels()
1274 chans = ad74413r_channels_map[config->func].channels; in ad74413r_setup_channels()
1275 num_chans = ad74413r_channels_map[config->func].num_channels; in ad74413r_setup_channels()
1282 chan->channel = i; in ad74413r_setup_channels()
1283 if (chan->output) in ad74413r_setup_channels()
1284 chan->scan_index = -1; in ad74413r_setup_channels()
1286 chan->scan_index = i; in ad74413r_setup_channels()
1289 ret = ad74413r_set_channel_function(st, i, config->func); in ad74413r_setup_channels()
1310 config = &st->channel_configs[i]; in ad74413r_setup_gpios()
1312 if (config->gpo_comparator) { in ad74413r_setup_gpios()
1316 st->gpo_gpio_offsets[gpo_gpio_i++] = i; in ad74413r_setup_gpios()
1319 if (config->func == CH_FUNC_DIGITAL_INPUT_LOGIC || in ad74413r_setup_gpios()
1320 config->func == CH_FUNC_DIGITAL_INPUT_LOOP_POWER) { in ad74413r_setup_gpios()
1321 st->comp_gpio_offsets[comp_gpio_i++] = i; in ad74413r_setup_gpios()
1323 strength = config->drive_strength; in ad74413r_setup_gpios()
1348 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in ad74413r_probe()
1350 return -ENOMEM; in ad74413r_probe()
1354 st->spi = spi; in ad74413r_probe()
1355 st->dev = &spi->dev; in ad74413r_probe()
1356 st->chip_info = device_get_match_data(&spi->dev); in ad74413r_probe()
1357 if (!st->chip_info) { in ad74413r_probe()
1361 st->chip_info = in ad74413r_probe()
1362 (struct ad74413r_chip_info *)id->driver_data; in ad74413r_probe()
1363 if (!st->chip_info) in ad74413r_probe()
1364 return -EINVAL; in ad74413r_probe()
1367 mutex_init(&st->lock); in ad74413r_probe()
1368 init_completion(&st->adc_data_completion); in ad74413r_probe()
1370 st->regmap = devm_regmap_init(st->dev, NULL, st, in ad74413r_probe()
1372 if (IS_ERR(st->regmap)) in ad74413r_probe()
1373 return PTR_ERR(st->regmap); in ad74413r_probe()
1375 st->reset_gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW); in ad74413r_probe()
1376 if (IS_ERR(st->reset_gpio)) in ad74413r_probe()
1377 return PTR_ERR(st->reset_gpio); in ad74413r_probe()
1379 st->refin_reg = devm_regulator_get(st->dev, "refin"); in ad74413r_probe()
1380 if (IS_ERR(st->refin_reg)) in ad74413r_probe()
1381 return dev_err_probe(st->dev, PTR_ERR(st->refin_reg), in ad74413r_probe()
1384 ret = regulator_enable(st->refin_reg); in ad74413r_probe()
1388 ret = devm_add_action_or_reset(st->dev, ad74413r_regulator_disable, in ad74413r_probe()
1389 st->refin_reg); in ad74413r_probe()
1393 st->sense_resistor_ohms = 100000000; in ad74413r_probe()
1394 device_property_read_u32(st->dev, "shunt-resistor-micro-ohms", in ad74413r_probe()
1395 &st->sense_resistor_ohms); in ad74413r_probe()
1396 st->sense_resistor_ohms /= 1000000; in ad74413r_probe()
1398 st->trig = devm_iio_trigger_alloc(st->dev, "%s-dev%d", in ad74413r_probe()
1399 st->chip_info->name, iio_device_id(indio_dev)); in ad74413r_probe()
1400 if (!st->trig) in ad74413r_probe()
1401 return -ENOMEM; in ad74413r_probe()
1403 st->trig->ops = &ad74413r_trigger_ops; in ad74413r_probe()
1404 iio_trigger_set_drvdata(st->trig, st); in ad74413r_probe()
1406 ret = devm_iio_trigger_register(st->dev, st->trig); in ad74413r_probe()
1410 indio_dev->name = st->chip_info->name; in ad74413r_probe()
1411 indio_dev->modes = INDIO_DIRECT_MODE; in ad74413r_probe()
1412 indio_dev->info = &ad74413r_info; in ad74413r_probe()
1413 indio_dev->trig = iio_trigger_get(st->trig); in ad74413r_probe()
1431 if (st->num_gpo_gpios) { in ad74413r_probe()
1432 st->gpo_gpiochip.owner = THIS_MODULE; in ad74413r_probe()
1433 st->gpo_gpiochip.label = st->chip_info->name; in ad74413r_probe()
1434 st->gpo_gpiochip.base = -1; in ad74413r_probe()
1435 st->gpo_gpiochip.ngpio = st->num_gpo_gpios; in ad74413r_probe()
1436 st->gpo_gpiochip.parent = st->dev; in ad74413r_probe()
1437 st->gpo_gpiochip.can_sleep = true; in ad74413r_probe()
1438 st->gpo_gpiochip.set = ad74413r_gpio_set; in ad74413r_probe()
1439 st->gpo_gpiochip.set_multiple = ad74413r_gpio_set_multiple; in ad74413r_probe()
1440 st->gpo_gpiochip.set_config = ad74413r_gpio_set_gpo_config; in ad74413r_probe()
1441 st->gpo_gpiochip.get_direction = in ad74413r_probe()
1444 ret = devm_gpiochip_add_data(st->dev, &st->gpo_gpiochip, st); in ad74413r_probe()
1449 if (st->num_comparator_gpios) { in ad74413r_probe()
1450 st->comp_gpiochip.owner = THIS_MODULE; in ad74413r_probe()
1451 st->comp_gpiochip.label = st->chip_info->name; in ad74413r_probe()
1452 st->comp_gpiochip.base = -1; in ad74413r_probe()
1453 st->comp_gpiochip.ngpio = st->num_comparator_gpios; in ad74413r_probe()
1454 st->comp_gpiochip.parent = st->dev; in ad74413r_probe()
1455 st->comp_gpiochip.can_sleep = true; in ad74413r_probe()
1456 st->comp_gpiochip.get = ad74413r_gpio_get; in ad74413r_probe()
1457 st->comp_gpiochip.get_multiple = ad74413r_gpio_get_multiple; in ad74413r_probe()
1458 st->comp_gpiochip.set_config = ad74413r_gpio_set_comp_config; in ad74413r_probe()
1459 st->comp_gpiochip.get_direction = in ad74413r_probe()
1462 ret = devm_gpiochip_add_data(st->dev, &st->comp_gpiochip, st); in ad74413r_probe()
1471 ret = devm_request_irq(st->dev, spi->irq, ad74413r_adc_data_interrupt, in ad74413r_probe()
1472 0, st->chip_info->name, indio_dev); in ad74413r_probe()
1474 return dev_err_probe(st->dev, ret, "Failed to request irq\n"); in ad74413r_probe()
1476 ret = devm_iio_triggered_buffer_setup(st->dev, indio_dev, in ad74413r_probe()
1483 return devm_iio_device_register(st->dev, indio_dev); in ad74413r_probe()