Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iio: adc: ad7173: add openwire detection support for single conversions

Some chips of the ad7173 family supports open wire detection.

Generate a level fault event whenever an external source is disconnected
from the system input on single conversions.

Reviewed-by: Nuno Sa <nuno.sa@analog.com>
Signed-off-by: Guillaume Ranquet <granquet@baylibre.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Link: https://patch.msgid.link/20250127-ad4111_openwire-v5-2-ef2db05c384f@baylibre.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Guillaume Ranquet and committed by
Jonathan Cameron
7530ed2a 34934d79

+179
+179
drivers/iio/adc/ad7173.c
··· 35 35 #include <linux/units.h> 36 36 37 37 #include <linux/iio/buffer.h> 38 + #include <linux/iio/events.h> 38 39 #include <linux/iio/iio.h> 39 40 #include <linux/iio/trigger_consumer.h> 40 41 #include <linux/iio/triggered_buffer.h> ··· 103 102 104 103 #define AD7173_GPIO_PDSW BIT(14) 105 104 #define AD7173_GPIO_OP_EN2_3 BIT(13) 105 + #define AD4111_GPIO_GP_OW_EN BIT(12) 106 106 #define AD7173_GPIO_MUX_IO BIT(12) 107 107 #define AD7173_GPIO_SYNC_EN BIT(11) 108 108 #define AD7173_GPIO_ERR_EN BIT(10) ··· 151 149 152 150 #define AD7173_FILTER_ODR0_MASK GENMASK(5, 0) 153 151 #define AD7173_MAX_CONFIGS 8 152 + #define AD4111_OW_DET_THRSH_MV 300 154 153 155 154 #define AD7173_MODE_CAL_INT_ZERO 0x4 /* Internal Zero-Scale Calibration */ 156 155 #define AD7173_MODE_CAL_INT_FULL 0x5 /* Internal Full-Scale Calibration */ ··· 185 182 bool has_int_ref; 186 183 bool has_ref2; 187 184 bool has_internal_fs_calibration; 185 + bool has_openwire_det; 188 186 bool higher_gpio_bits; 189 187 u8 num_gpios; 190 188 }; 191 189 192 190 struct ad7173_channel_config { 191 + /* Openwire detection threshold */ 192 + unsigned int openwire_thrsh_raw; 193 + int openwire_comp_chan; 193 194 u8 cfg_slot; 194 195 bool live; 195 196 ··· 210 203 unsigned int ain; 211 204 struct ad7173_channel_config cfg; 212 205 u8 syscalib_mode; 206 + bool openwire_det_en; 213 207 }; 214 208 215 209 struct ad7173_state { ··· 400 392 } 401 393 402 394 return 0; 395 + } 396 + 397 + /* 398 + * Associative array of channel pairs for open wire detection 399 + * The array is indexed by ain and gives the associated channel pair 400 + * to perform the open wire detection with 401 + * the channel pair [0] is for non differential and pair [1] 402 + * is for differential inputs 403 + */ 404 + static int openwire_ain_to_channel_pair[][2][2] = { 405 + /* AIN Single Differential */ 406 + [0] = { { 0, 15 }, { 1, 2 } }, 407 + [1] = { { 1, 2 }, { 2, 1 } }, 408 + [2] = { { 3, 4 }, { 5, 6 } }, 409 + [3] = { { 5, 6 }, { 6, 5 } }, 410 + [4] = { { 7, 8 }, { 9, 10 } }, 411 + [5] = { { 9, 10 }, { 10, 9 } }, 412 + [6] = { { 11, 12 }, { 13, 14 } }, 413 + [7] = { { 13, 14 }, { 14, 13 } }, 414 + }; 415 + 416 + /* 417 + * Openwire detection on ad4111 works by running the same input measurement 418 + * on two different channels and compare if the difference between the two 419 + * measurements exceeds a certain value (typical 300mV) 420 + */ 421 + static int ad4111_openwire_event(struct iio_dev *indio_dev, 422 + const struct iio_chan_spec *chan) 423 + { 424 + struct ad7173_state *st = iio_priv(indio_dev); 425 + struct ad7173_channel *adchan = &st->channels[chan->address]; 426 + struct ad7173_channel_config *cfg = &adchan->cfg; 427 + int ret, val1, val2; 428 + 429 + ret = regmap_set_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, 430 + AD4111_GPIO_GP_OW_EN); 431 + if (ret) 432 + return ret; 433 + 434 + adchan->cfg.openwire_comp_chan = 435 + openwire_ain_to_channel_pair[chan->channel][chan->differential][0]; 436 + 437 + ret = ad_sigma_delta_single_conversion(indio_dev, chan, &val1); 438 + if (ret < 0) { 439 + dev_err(&indio_dev->dev, 440 + "Error running ad_sigma_delta single conversion: %d", ret); 441 + goto out; 442 + } 443 + 444 + adchan->cfg.openwire_comp_chan = 445 + openwire_ain_to_channel_pair[chan->channel][chan->differential][1]; 446 + 447 + ret = ad_sigma_delta_single_conversion(indio_dev, chan, &val2); 448 + if (ret < 0) { 449 + dev_err(&indio_dev->dev, 450 + "Error running ad_sigma_delta single conversion: %d", ret); 451 + goto out; 452 + } 453 + 454 + if (abs(val1 - val2) > cfg->openwire_thrsh_raw) 455 + iio_push_event(indio_dev, 456 + IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, chan->address, 457 + IIO_EV_TYPE_FAULT, IIO_EV_DIR_FAULT_OPENWIRE), 458 + iio_get_time_ns(indio_dev)); 459 + 460 + out: 461 + adchan->cfg.openwire_comp_chan = -1; 462 + regmap_clear_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, 463 + AD4111_GPIO_GP_OW_EN); 464 + return ret; 403 465 } 404 466 405 467 static int ad7173_mask_xlate(struct gpio_regmap *gpio, unsigned int base, ··· 669 591 FIELD_PREP(AD7173_CH_SETUP_SEL_MASK, st->channels[channel].cfg.cfg_slot) | 670 592 st->channels[channel].ain; 671 593 594 + if (st->channels[channel].cfg.openwire_comp_chan >= 0) 595 + channel = st->channels[channel].cfg.openwire_comp_chan; 596 + 672 597 return ad_sd_write_reg(&st->sd, AD7173_REG_CH(channel), 2, val); 673 598 } 674 599 ··· 720 639 721 640 static int ad7173_disable_one(struct ad_sigma_delta *sd, unsigned int chan) 722 641 { 642 + struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd); 643 + 644 + if (st->channels[chan].cfg.openwire_comp_chan >= 0) 645 + chan = st->channels[chan].cfg.openwire_comp_chan; 646 + 723 647 return ad_sd_write_reg(sd, AD7173_REG_CH(chan), 2, 0); 724 648 } 725 649 ··· 776 690 .has_current_inputs = true, 777 691 .has_int_ref = true, 778 692 .has_internal_fs_calibration = true, 693 + .has_openwire_det = true, 779 694 .clock = 2 * HZ_PER_MHZ, 780 695 .sinc5_data_rates = ad7173_sinc5_data_rates, 781 696 .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates), ··· 1087 1000 if (ret < 0) 1088 1001 return ret; 1089 1002 1003 + if (ch->openwire_det_en) { 1004 + ret = ad4111_openwire_event(indio_dev, chan); 1005 + if (ret < 0) 1006 + return ret; 1007 + } 1008 + 1090 1009 return IIO_VAL_INT; 1091 1010 case IIO_CHAN_INFO_SCALE: 1092 1011 ··· 1237 1144 return ad_sd_write_reg(&st->sd, reg, reg_size, writeval); 1238 1145 } 1239 1146 1147 + static int ad7173_write_event_config(struct iio_dev *indio_dev, 1148 + const struct iio_chan_spec *chan, 1149 + enum iio_event_type type, 1150 + enum iio_event_direction dir, 1151 + bool state) 1152 + { 1153 + struct ad7173_state *st = iio_priv(indio_dev); 1154 + struct ad7173_channel *adchan = &st->channels[chan->address]; 1155 + 1156 + switch (type) { 1157 + case IIO_EV_TYPE_FAULT: 1158 + adchan->openwire_det_en = state; 1159 + return 0; 1160 + default: 1161 + return -EINVAL; 1162 + } 1163 + } 1164 + 1165 + static int ad7173_read_event_config(struct iio_dev *indio_dev, 1166 + const struct iio_chan_spec *chan, 1167 + enum iio_event_type type, 1168 + enum iio_event_direction dir) 1169 + { 1170 + struct ad7173_state *st = iio_priv(indio_dev); 1171 + struct ad7173_channel *adchan = &st->channels[chan->address]; 1172 + 1173 + switch (type) { 1174 + case IIO_EV_TYPE_FAULT: 1175 + return adchan->openwire_det_en; 1176 + default: 1177 + return -EINVAL; 1178 + } 1179 + } 1180 + 1181 + static const struct iio_event_spec ad4111_events[] = { 1182 + { 1183 + .type = IIO_EV_TYPE_FAULT, 1184 + .dir = IIO_EV_DIR_FAULT_OPENWIRE, 1185 + .mask_separate = BIT(IIO_EV_INFO_VALUE), 1186 + .mask_shared_by_all = BIT(IIO_EV_INFO_ENABLE), 1187 + }, 1188 + }; 1189 + 1240 1190 static const struct iio_info ad7173_info = { 1241 1191 .read_raw = &ad7173_read_raw, 1242 1192 .write_raw = &ad7173_write_raw, 1243 1193 .debugfs_reg_access = &ad7173_debug_reg_access, 1244 1194 .validate_trigger = ad_sd_validate_trigger, 1245 1195 .update_scan_mode = ad7173_update_scan_mode, 1196 + .write_event_config = ad7173_write_event_config, 1197 + .read_event_config = ad7173_read_event_config, 1246 1198 }; 1247 1199 1248 1200 static const struct iio_scan_type ad4113_scan_type = { ··· 1491 1353 return 0; 1492 1354 } 1493 1355 1356 + static int ad7173_validate_openwire_ain_inputs(struct ad7173_state *st, 1357 + bool differential, 1358 + unsigned int ain0, 1359 + unsigned int ain1) 1360 + { 1361 + /* 1362 + * If the channel is configured as differential, 1363 + * the ad4111 requires specific ains to be used together 1364 + */ 1365 + if (differential) 1366 + return (ain0 % 2) ? (ain0 - 1) == ain1 : (ain0 + 1) == ain1; 1367 + 1368 + return ain1 == AD4111_VINCOM_INPUT; 1369 + } 1370 + 1371 + static unsigned int ad7173_calc_openwire_thrsh_raw(struct ad7173_state *st, 1372 + struct iio_chan_spec *chan, 1373 + struct ad7173_channel *chan_st_priv, 1374 + unsigned int thrsh_mv) { 1375 + unsigned int thrsh_raw; 1376 + 1377 + thrsh_raw = 1378 + BIT(chan->scan_type.realbits - !!(chan_st_priv->cfg.bipolar)) 1379 + * thrsh_mv 1380 + / ad7173_get_ref_voltage_milli(st, chan_st_priv->cfg.ref_sel); 1381 + if (chan->channel < st->info->num_voltage_in_div) 1382 + thrsh_raw /= AD4111_DIVIDER_RATIO; 1383 + 1384 + return thrsh_raw; 1385 + } 1386 + 1494 1387 static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev) 1495 1388 { 1496 1389 struct ad7173_channel *chans_st_arr, *chan_st_priv; ··· 1569 1400 chan_st_priv->cfg.bipolar = false; 1570 1401 chan_st_priv->cfg.input_buf = st->info->has_input_buf; 1571 1402 chan_st_priv->cfg.ref_sel = AD7173_SETUP_REF_SEL_INT_REF; 1403 + chan_st_priv->cfg.openwire_comp_chan = -1; 1572 1404 st->adc_mode |= AD7173_ADC_MODE_REF_EN; 1573 1405 if (st->info->data_reg_only_16bit) 1574 1406 chan_arr[chan_index].scan_type = ad4113_scan_type; ··· 1636 1466 chan->channel = ain[0]; 1637 1467 chan_st_priv->cfg.input_buf = st->info->has_input_buf; 1638 1468 chan_st_priv->cfg.odr = 0; 1469 + chan_st_priv->cfg.openwire_comp_chan = -1; 1639 1470 1640 1471 chan_st_priv->cfg.bipolar = fwnode_property_read_bool(child, "bipolar"); 1641 1472 if (chan_st_priv->cfg.bipolar) ··· 1651 1480 chan_st_priv->cfg.input_buf = st->info->has_input_buf; 1652 1481 chan->channel2 = ain[1]; 1653 1482 chan_st_priv->ain = AD7173_CH_ADDRESS(ain[0], ain[1]); 1483 + if (st->info->has_openwire_det && 1484 + ad7173_validate_openwire_ain_inputs(st, chan->differential, ain[0], ain[1])) { 1485 + chan->event_spec = ad4111_events; 1486 + chan->num_event_specs = ARRAY_SIZE(ad4111_events); 1487 + chan_st_priv->cfg.openwire_thrsh_raw = 1488 + ad7173_calc_openwire_thrsh_raw(st, chan, chan_st_priv, 1489 + AD4111_OW_DET_THRSH_MV); 1490 + } 1654 1491 } 1655 1492 1656 1493 if (st->info->data_reg_only_16bit)