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

iio: adc: ad4695: add offload-based oversampling support

Add support for the ad4695's oversampling feature when SPI offload is
available. This allows the ad4695 to set oversampling ratios on a
per-channel basis, raising the effective-number-of-bits from 16
(OSR == 1) to 17 (4), 18 (16), or 19 (64) for a given sample (i.e. one
full cycle through the auto-sequencer). The logic for reading and
writing sampling frequency for a given channel is also adjusted based on
the current oversampling ratio.

The non-offload case isn't supported as there isn't a good way to
trigger the CNV pin in this mode. Support could be added in the future
if a use-case arises.

Signed-off-by: Trevor Gamblin <tgamblin@baylibre.com>
Reviewed-by: Nuno Sa <nuno.sa@analog.com>
Tested-by: David Lechner <dlechner@baylibre.com>
Link: https://patch.msgid.link/20250109-ad4695-oversampling-v2-1-a46ac487082c@baylibre.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Trevor Gamblin and committed by
Jonathan Cameron
67d63185 192b669b

+302 -29
+302 -29
drivers/iio/adc/ad4695.c
··· 79 79 #define AD4695_REG_CONFIG_IN_MODE BIT(6) 80 80 #define AD4695_REG_CONFIG_IN_PAIR GENMASK(5, 4) 81 81 #define AD4695_REG_CONFIG_IN_AINHIGHZ_EN BIT(3) 82 + #define AD4695_REG_CONFIG_IN_OSR_SET GENMASK(1, 0) 82 83 #define AD4695_REG_UPPER_IN(n) (0x0040 | (2 * (n))) 83 84 #define AD4695_REG_LOWER_IN(n) (0x0060 | (2 * (n))) 84 85 #define AD4695_REG_HYST_IN(n) (0x0080 | (2 * (n))) ··· 128 127 bool bipolar; 129 128 enum ad4695_in_pair pin_pairing; 130 129 unsigned int common_mode_mv; 130 + unsigned int oversampling_ratio; 131 131 }; 132 132 133 133 struct ad4695_state { ··· 308 306 .val_format_endian_default = REGMAP_ENDIAN_BIG, 309 307 }; 310 308 309 + enum { 310 + AD4695_SCAN_TYPE_OSR_1, 311 + AD4695_SCAN_TYPE_OSR_4, 312 + AD4695_SCAN_TYPE_OSR_16, 313 + AD4695_SCAN_TYPE_OSR_64, 314 + }; 315 + 316 + static const struct iio_scan_type ad4695_scan_type_offload_u[] = { 317 + [AD4695_SCAN_TYPE_OSR_1] = { 318 + .sign = 'u', 319 + .realbits = 16, 320 + .shift = 3, 321 + .storagebits = 32, 322 + }, 323 + [AD4695_SCAN_TYPE_OSR_4] = { 324 + .sign = 'u', 325 + .realbits = 17, 326 + .shift = 2, 327 + .storagebits = 32, 328 + }, 329 + [AD4695_SCAN_TYPE_OSR_16] = { 330 + .sign = 'u', 331 + .realbits = 18, 332 + .shift = 1, 333 + .storagebits = 32, 334 + }, 335 + [AD4695_SCAN_TYPE_OSR_64] = { 336 + .sign = 'u', 337 + .realbits = 19, 338 + .storagebits = 32, 339 + }, 340 + }; 341 + 342 + static const struct iio_scan_type ad4695_scan_type_offload_s[] = { 343 + [AD4695_SCAN_TYPE_OSR_1] = { 344 + .sign = 's', 345 + .realbits = 16, 346 + .shift = 3, 347 + .storagebits = 32, 348 + }, 349 + [AD4695_SCAN_TYPE_OSR_4] = { 350 + .sign = 's', 351 + .realbits = 17, 352 + .shift = 2, 353 + .storagebits = 32, 354 + }, 355 + [AD4695_SCAN_TYPE_OSR_16] = { 356 + .sign = 's', 357 + .realbits = 18, 358 + .shift = 1, 359 + .storagebits = 32, 360 + }, 361 + [AD4695_SCAN_TYPE_OSR_64] = { 362 + .sign = 's', 363 + .realbits = 19, 364 + .storagebits = 32, 365 + }, 366 + }; 367 + 311 368 static const struct iio_chan_spec ad4695_channel_template = { 312 369 .type = IIO_VOLTAGE, 313 370 .indexed = 1, ··· 402 341 403 342 static const char * const ad4695_power_supplies[] = { 404 343 "avdd", "vio" 344 + }; 345 + 346 + static const int ad4695_oversampling_ratios[] = { 347 + 1, 4, 16, 64, 405 348 }; 406 349 407 350 static const struct ad4695_chip_info ad4695_chip_info = { ··· 582 517 return regmap_update_bits(st->regmap, AD4695_REG_REF_CTRL, 583 518 AD4695_REG_REF_CTRL_VREF_SET, 584 519 FIELD_PREP(AD4695_REG_REF_CTRL_VREF_SET, val)); 520 + } 521 + 522 + /** 523 + * ad4695_osr_to_regval - convert ratio to OSR register value 524 + * @ratio: ratio to check 525 + * 526 + * Check if ratio is present in the list of available ratios and return 527 + * the corresponding value that needs to be written to the register to 528 + * select that ratio. 529 + * 530 + * Returns: register value (0 to 3) or -EINVAL if there is not an exact 531 + * match 532 + */ 533 + static int ad4695_osr_to_regval(int ratio) 534 + { 535 + int i; 536 + 537 + for (i = 0; i < ARRAY_SIZE(ad4695_oversampling_ratios); i++) { 538 + if (ratio == ad4695_oversampling_ratios[i]) 539 + return i; 540 + } 541 + 542 + return -EINVAL; 585 543 } 586 544 587 545 static int ad4695_write_chn_cfg(struct ad4695_state *st, ··· 1034 946 int *val, int *val2, long mask) 1035 947 { 1036 948 struct ad4695_state *st = iio_priv(indio_dev); 949 + const struct iio_scan_type *scan_type; 1037 950 struct ad4695_channel_config *cfg = &st->channels_cfg[chan->scan_index]; 1038 - u8 realbits = chan->scan_type.realbits; 951 + unsigned int osr = st->channels_cfg[chan->scan_index].oversampling_ratio; 1039 952 unsigned int reg_val; 1040 953 int ret, tmp; 954 + u8 realbits; 955 + 956 + scan_type = iio_get_current_scan_type(indio_dev, chan); 957 + if (IS_ERR(scan_type)) 958 + return PTR_ERR(scan_type); 959 + 960 + realbits = scan_type->realbits; 1041 961 1042 962 switch (mask) { 1043 963 case IIO_CHAN_INFO_RAW: ··· 1054 958 if (ret) 1055 959 return ret; 1056 960 1057 - if (chan->scan_type.sign == 's') 961 + if (scan_type->sign == 's') 1058 962 *val = sign_extend32(st->raw_data, realbits - 1); 1059 963 else 1060 964 *val = st->raw_data; ··· 1066 970 switch (chan->type) { 1067 971 case IIO_VOLTAGE: 1068 972 *val = st->vref_mv; 1069 - *val2 = chan->scan_type.realbits; 973 + *val2 = realbits; 1070 974 return IIO_VAL_FRACTIONAL_LOG2; 1071 975 case IIO_TEMP: 1072 976 /* T_scale (°C) = raw * V_REF (mV) / (-1.8 mV/°C * 2^16) */ ··· 1127 1031 1128 1032 tmp = sign_extend32(reg_val, 15); 1129 1033 1130 - *val = tmp / 4; 1131 - *val2 = abs(tmp) % 4 * MICRO / 4; 1034 + switch (cfg->oversampling_ratio) { 1035 + case 1: 1036 + *val = tmp / 4; 1037 + *val2 = abs(tmp) % 4 * MICRO / 4; 1038 + break; 1039 + case 4: 1040 + *val = tmp / 2; 1041 + *val2 = abs(tmp) % 2 * MICRO / 2; 1042 + break; 1043 + case 16: 1044 + *val = tmp; 1045 + *val2 = 0; 1046 + break; 1047 + case 64: 1048 + *val = tmp * 2; 1049 + *val2 = 0; 1050 + break; 1051 + default: 1052 + return -EINVAL; 1053 + } 1132 1054 1133 1055 if (tmp < 0 && *val2) { 1134 1056 *val *= -1; ··· 1159 1045 default: 1160 1046 return -EINVAL; 1161 1047 } 1048 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 1049 + switch (chan->type) { 1050 + case IIO_VOLTAGE: 1051 + *val = st->channels_cfg[chan->scan_index].oversampling_ratio; 1052 + return IIO_VAL_INT; 1053 + default: 1054 + return -EINVAL; 1055 + } 1162 1056 case IIO_CHAN_INFO_SAMP_FREQ: { 1163 1057 struct pwm_state state; 1164 1058 ··· 1174 1052 if (ret) 1175 1053 return ret; 1176 1054 1177 - *val = DIV_ROUND_UP_ULL(NSEC_PER_SEC, state.period); 1055 + /* 1056 + * The effective sampling frequency for a channel is the input 1057 + * frequency divided by the channel's OSR value. 1058 + */ 1059 + *val = DIV_ROUND_UP_ULL(NSEC_PER_SEC, state.period * osr); 1178 1060 1179 1061 return IIO_VAL_INT; 1180 1062 } ··· 1199 1073 } 1200 1074 } 1201 1075 1076 + static int ad4695_set_osr_val(struct ad4695_state *st, 1077 + struct iio_chan_spec const *chan, 1078 + int val) 1079 + { 1080 + int osr = ad4695_osr_to_regval(val); 1081 + 1082 + if (osr < 0) 1083 + return osr; 1084 + 1085 + switch (chan->type) { 1086 + case IIO_VOLTAGE: 1087 + st->channels_cfg[chan->scan_index].oversampling_ratio = val; 1088 + return regmap_update_bits(st->regmap, 1089 + AD4695_REG_CONFIG_IN(chan->scan_index), 1090 + AD4695_REG_CONFIG_IN_OSR_SET, 1091 + FIELD_PREP(AD4695_REG_CONFIG_IN_OSR_SET, osr)); 1092 + default: 1093 + return -EINVAL; 1094 + } 1095 + } 1096 + 1097 + static unsigned int ad4695_get_calibbias(int val, int val2, int osr) 1098 + { 1099 + int val_calc, scale; 1100 + 1101 + switch (osr) { 1102 + case 4: 1103 + scale = 4; 1104 + break; 1105 + case 16: 1106 + scale = 2; 1107 + break; 1108 + case 64: 1109 + scale = 1; 1110 + break; 1111 + default: 1112 + scale = 8; 1113 + break; 1114 + } 1115 + 1116 + val = clamp_t(int, val, S32_MIN / 8, S32_MAX / 8); 1117 + 1118 + /* val2 range is (-MICRO, MICRO) if val == 0, otherwise [0, MICRO) */ 1119 + if (val < 0) 1120 + val_calc = val * scale - val2 * scale / MICRO; 1121 + else if (val2 < 0) 1122 + /* if val2 < 0 then val == 0 */ 1123 + val_calc = val2 * scale / (int)MICRO; 1124 + else 1125 + val_calc = val * scale + val2 * scale / MICRO; 1126 + 1127 + val_calc /= 2; 1128 + 1129 + return clamp_t(int, val_calc, S16_MIN, S16_MAX); 1130 + } 1131 + 1202 1132 static int ad4695_write_raw(struct iio_dev *indio_dev, 1203 1133 struct iio_chan_spec const *chan, 1204 1134 int val, int val2, long mask) 1205 1135 { 1206 1136 struct ad4695_state *st = iio_priv(indio_dev); 1207 1137 unsigned int reg_val; 1138 + unsigned int osr = st->channels_cfg[chan->scan_index].oversampling_ratio; 1208 1139 1209 1140 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 1210 1141 switch (mask) { ··· 1286 1103 case IIO_CHAN_INFO_CALIBBIAS: 1287 1104 switch (chan->type) { 1288 1105 case IIO_VOLTAGE: 1289 - if (val2 >= 0 && val > S16_MAX / 4) 1290 - reg_val = S16_MAX; 1291 - else if ((val2 < 0 ? -val : val) < S16_MIN / 4) 1292 - reg_val = S16_MIN; 1293 - else if (val2 < 0) 1294 - reg_val = clamp_t(int, 1295 - -(val * 4 + -val2 * 4 / MICRO), 1296 - S16_MIN, S16_MAX); 1297 - else if (val < 0) 1298 - reg_val = clamp_t(int, 1299 - val * 4 - val2 * 4 / MICRO, 1300 - S16_MIN, S16_MAX); 1301 - else 1302 - reg_val = clamp_t(int, 1303 - val * 4 + val2 * 4 / MICRO, 1304 - S16_MIN, S16_MAX); 1305 - 1106 + reg_val = ad4695_get_calibbias(val, val2, osr); 1306 1107 return regmap_write(st->regmap16, 1307 1108 AD4695_REG_OFFSET_IN(chan->scan_index), 1308 1109 reg_val); ··· 1295 1128 } 1296 1129 case IIO_CHAN_INFO_SAMP_FREQ: { 1297 1130 struct pwm_state state; 1131 + /* 1132 + * Limit the maximum acceptable sample rate according to 1133 + * the channel's oversampling ratio. 1134 + */ 1135 + u64 max_osr_rate = DIV_ROUND_UP_ULL(st->chip_info->max_sample_rate, 1136 + osr); 1298 1137 1299 - if (val <= 0 || val > st->chip_info->max_sample_rate) 1138 + if (val <= 0 || val > max_osr_rate) 1300 1139 return -EINVAL; 1301 1140 1302 1141 guard(mutex)(&st->cnv_pwm_lock); 1303 1142 pwm_get_state(st->cnv_pwm, &state); 1304 - state.period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, val); 1143 + /* 1144 + * The required sample frequency for a given OSR is the 1145 + * input frequency multiplied by it. 1146 + */ 1147 + state.period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, val * osr); 1305 1148 return pwm_apply_might_sleep(st->cnv_pwm, &state); 1306 1149 } 1150 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 1151 + return ad4695_set_osr_val(st, chan, val); 1307 1152 default: 1308 1153 return -EINVAL; 1309 1154 } ··· 1328 1149 const int **vals, int *type, int *length, 1329 1150 long mask) 1330 1151 { 1152 + int ret; 1331 1153 static const int ad4695_calibscale_available[6] = { 1332 1154 /* Range of 0 (inclusive) to 2 (exclusive) */ 1333 1155 0, 15, 1, 15, U16_MAX, 15 1334 1156 }; 1335 - static const int ad4695_calibbias_available[6] = { 1157 + static const int ad4695_calibbias_available[4][6] = { 1336 1158 /* 1337 1159 * Datasheet says FSR/8 which translates to signed/4. The step 1338 - * depends on oversampling ratio which is always 1 for now. 1160 + * depends on oversampling ratio, so we need four different 1161 + * ranges to select from. 1339 1162 */ 1340 - S16_MIN / 4, 0, 0, MICRO / 4, S16_MAX / 4, S16_MAX % 4 * MICRO / 4 1163 + { 1164 + S16_MIN / 4, 0, 1165 + 0, MICRO / 4, 1166 + S16_MAX / 4, S16_MAX % 4 * MICRO / 4 1167 + }, 1168 + { 1169 + S16_MIN / 2, 0, 1170 + 0, MICRO / 2, 1171 + S16_MAX / 2, S16_MAX % 2 * MICRO / 2, 1172 + }, 1173 + { 1174 + S16_MIN, 0, 1175 + 1, 0, 1176 + S16_MAX, 0, 1177 + }, 1178 + { 1179 + S16_MIN * 2, 0, 1180 + 2, 0, 1181 + S16_MAX * 2, 0, 1182 + }, 1341 1183 }; 1342 1184 struct ad4695_state *st = iio_priv(indio_dev); 1185 + unsigned int osr = st->channels_cfg[chan->scan_index].oversampling_ratio; 1343 1186 1344 1187 switch (mask) { 1345 1188 case IIO_CHAN_INFO_CALIBSCALE: ··· 1376 1175 case IIO_CHAN_INFO_CALIBBIAS: 1377 1176 switch (chan->type) { 1378 1177 case IIO_VOLTAGE: 1379 - *vals = ad4695_calibbias_available; 1178 + ret = ad4695_osr_to_regval(osr); 1179 + if (ret < 0) 1180 + return ret; 1181 + /* 1182 + * Select the appropriate calibbias array based on the 1183 + * OSR value in the register. 1184 + */ 1185 + *vals = ad4695_calibbias_available[ret]; 1380 1186 *type = IIO_VAL_INT_PLUS_MICRO; 1381 1187 return IIO_AVAIL_RANGE; 1382 1188 default: 1383 1189 return -EINVAL; 1384 1190 } 1385 1191 case IIO_CHAN_INFO_SAMP_FREQ: 1192 + /* Max sample rate for the channel depends on OSR */ 1193 + st->sample_freq_range[2] = 1194 + DIV_ROUND_UP_ULL(st->chip_info->max_sample_rate, osr); 1386 1195 *vals = st->sample_freq_range; 1387 1196 *type = IIO_VAL_INT; 1388 1197 return IIO_AVAIL_RANGE; 1198 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 1199 + switch (chan->type) { 1200 + case IIO_VOLTAGE: 1201 + *vals = ad4695_oversampling_ratios; 1202 + *length = ARRAY_SIZE(ad4695_oversampling_ratios); 1203 + *type = IIO_VAL_INT; 1204 + return IIO_AVAIL_LIST; 1205 + default: 1206 + return -EINVAL; 1207 + } 1389 1208 default: 1390 1209 return -EINVAL; 1391 1210 } ··· 1439 1218 return -EINVAL; 1440 1219 } 1441 1220 1221 + static int ad4695_get_current_scan_type(const struct iio_dev *indio_dev, 1222 + const struct iio_chan_spec *chan) 1223 + { 1224 + struct ad4695_state *st = iio_priv(indio_dev); 1225 + unsigned int osr = st->channels_cfg[chan->scan_index].oversampling_ratio; 1226 + 1227 + switch (osr) { 1228 + case 1: 1229 + return AD4695_SCAN_TYPE_OSR_1; 1230 + case 4: 1231 + return AD4695_SCAN_TYPE_OSR_4; 1232 + case 16: 1233 + return AD4695_SCAN_TYPE_OSR_16; 1234 + case 64: 1235 + return AD4695_SCAN_TYPE_OSR_64; 1236 + default: 1237 + return -EINVAL; 1238 + } 1239 + } 1240 + 1442 1241 static const struct iio_info ad4695_info = { 1443 1242 .read_raw = &ad4695_read_raw, 1444 1243 .write_raw_get_fmt = &ad4695_write_raw_get_fmt, 1445 1244 .write_raw = &ad4695_write_raw, 1245 + .read_avail = &ad4695_read_avail, 1246 + .debugfs_reg_access = &ad4695_debugfs_reg_access, 1247 + }; 1248 + 1249 + static const struct iio_info ad4695_offload_info = { 1250 + .read_raw = &ad4695_read_raw, 1251 + .write_raw_get_fmt = &ad4695_write_raw_get_fmt, 1252 + .write_raw = &ad4695_write_raw, 1253 + .get_current_scan_type = &ad4695_get_current_scan_type, 1446 1254 .read_avail = &ad4695_read_avail, 1447 1255 .debugfs_reg_access = &ad4695_debugfs_reg_access, 1448 1256 }; ··· 1490 1240 1491 1241 chan_cfg->highz_en = true; 1492 1242 chan_cfg->channel = i; 1243 + 1244 + /* This is the default OSR after reset */ 1245 + chan_cfg->oversampling_ratio = 1; 1493 1246 1494 1247 *iio_chan = ad4695_channel_template; 1495 1248 iio_chan->channel = i; ··· 1662 1409 struct dma_chan *rx_dma; 1663 1410 int ret, i; 1664 1411 1412 + indio_dev->info = &ad4695_offload_info; 1665 1413 indio_dev->num_channels = st->chip_info->num_voltage_inputs + 1; 1666 1414 indio_dev->setup_ops = &ad4695_offload_buffer_setup_ops; 1667 1415 ··· 1713 1459 1714 1460 for (i = 0; i < indio_dev->num_channels; i++) { 1715 1461 struct iio_chan_spec *chan = &st->iio_chan[i]; 1462 + struct ad4695_channel_config *cfg = &st->channels_cfg[i]; 1716 1463 1717 1464 /* 1718 1465 * NB: When using offload support, all channels need to have the ··· 1729 1474 /* add sample frequency for PWM CNV trigger */ 1730 1475 chan->info_mask_separate |= BIT(IIO_CHAN_INFO_SAMP_FREQ); 1731 1476 chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_SAMP_FREQ); 1477 + 1478 + /* Add the oversampling properties only for voltage channels */ 1479 + if (chan->type != IIO_VOLTAGE) 1480 + continue; 1481 + 1482 + chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO); 1483 + chan->info_mask_separate_available |= 1484 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO); 1485 + chan->has_ext_scan_type = 1; 1486 + if (cfg->bipolar) { 1487 + chan->ext_scan_type = ad4695_scan_type_offload_s; 1488 + chan->num_ext_scan_type = 1489 + ARRAY_SIZE(ad4695_scan_type_offload_s); 1490 + } else { 1491 + chan->ext_scan_type = ad4695_scan_type_offload_u; 1492 + chan->num_ext_scan_type = 1493 + ARRAY_SIZE(ad4695_scan_type_offload_u); 1494 + } 1732 1495 } 1733 1496 1734 1497 return devm_iio_dmaengine_buffer_setup_with_handle(dev, indio_dev,