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: pressure: bmp280: Generalize read_*() functions

Add the coefficients for the IIO standard units and the IIO value
inside the chip_info structure.

Move the calculations for the IIO unit compatibility from inside the
read_{temp,press,humid}() functions and move them to the general
read_raw() function.

In this way, all the data for the calculation of the value are
located in the chip_info structure of the respective sensor.

Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
Acked-by: Adam Rizkalla <ajarizzo@gmail.com>
Link: https://patch.msgid.link/20240628171726.124852-2-vassilisamir@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Vasileios Amoiridis and committed by
Jonathan Cameron
74353ceb 7c7d9170

+108 -73
+98 -70
drivers/iio/pressure/bmp280-core.c
··· 445 445 return (u32)p; 446 446 } 447 447 448 - static int bmp280_read_temp(struct bmp280_data *data, 449 - int *val, int *val2) 448 + static int bmp280_read_temp(struct bmp280_data *data, s32 *comp_temp) 450 449 { 451 - s32 comp_temp; 452 450 u32 adc_temp; 453 451 int ret; 454 452 ··· 454 456 if (ret) 455 457 return ret; 456 458 457 - comp_temp = bmp280_compensate_temp(data, adc_temp); 459 + *comp_temp = bmp280_compensate_temp(data, adc_temp); 458 460 459 - *val = comp_temp * 10; 460 - return IIO_VAL_INT; 461 + return 0; 461 462 } 462 463 463 - static int bmp280_read_press(struct bmp280_data *data, 464 - int *val, int *val2) 464 + static int bmp280_read_press(struct bmp280_data *data, u32 *comp_press) 465 465 { 466 - u32 comp_press, adc_press, t_fine; 466 + u32 adc_press; 467 + s32 t_fine; 467 468 int ret; 468 469 469 470 ret = bmp280_get_t_fine(data, &t_fine); ··· 473 476 if (ret) 474 477 return ret; 475 478 476 - comp_press = bmp280_compensate_press(data, adc_press, t_fine); 479 + *comp_press = bmp280_compensate_press(data, adc_press, t_fine); 477 480 478 - *val = comp_press; 479 - *val2 = 256000; 480 - 481 - return IIO_VAL_FRACTIONAL; 481 + return 0; 482 482 } 483 483 484 - static int bme280_read_humid(struct bmp280_data *data, int *val, int *val2) 484 + static int bme280_read_humid(struct bmp280_data *data, u32 *comp_humidity) 485 485 { 486 - u32 comp_humidity; 487 486 u16 adc_humidity; 488 487 s32 t_fine; 489 488 int ret; ··· 492 499 if (ret) 493 500 return ret; 494 501 495 - comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine); 502 + *comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine); 496 503 497 - *val = comp_humidity * 1000 / 1024; 498 - 499 - return IIO_VAL_INT; 504 + return 0; 500 505 } 501 506 502 507 static int bmp280_read_raw_impl(struct iio_dev *indio_dev, ··· 502 511 int *val, int *val2, long mask) 503 512 { 504 513 struct bmp280_data *data = iio_priv(indio_dev); 514 + int chan_value; 515 + int ret; 505 516 506 517 guard(mutex)(&data->lock); 507 518 ··· 511 518 case IIO_CHAN_INFO_PROCESSED: 512 519 switch (chan->type) { 513 520 case IIO_HUMIDITYRELATIVE: 514 - return data->chip_info->read_humid(data, val, val2); 521 + ret = data->chip_info->read_humid(data, &chan_value); 522 + if (ret) 523 + return ret; 524 + 525 + *val = data->chip_info->humid_coeffs[0] * chan_value; 526 + *val2 = data->chip_info->humid_coeffs[1]; 527 + return data->chip_info->humid_coeffs_type; 515 528 case IIO_PRESSURE: 516 - return data->chip_info->read_press(data, val, val2); 529 + ret = data->chip_info->read_press(data, &chan_value); 530 + if (ret) 531 + return ret; 532 + 533 + *val = data->chip_info->press_coeffs[0] * chan_value; 534 + *val2 = data->chip_info->press_coeffs[1]; 535 + return data->chip_info->press_coeffs_type; 517 536 case IIO_TEMP: 518 - return data->chip_info->read_temp(data, val, val2); 537 + ret = data->chip_info->read_temp(data, &chan_value); 538 + if (ret) 539 + return ret; 540 + 541 + *val = data->chip_info->temp_coeffs[0] * chan_value; 542 + *val2 = data->chip_info->temp_coeffs[1]; 543 + return data->chip_info->temp_coeffs_type; 519 544 default: 520 545 return -EINVAL; 521 546 } ··· 833 822 834 823 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; 835 824 static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID }; 825 + static const int bmp280_temp_coeffs[] = { 10, 1 }; 826 + static const int bmp280_press_coeffs[] = { 1, 256000 }; 836 827 837 828 const struct bmp280_chip_info bmp280_chip_info = { 838 829 .id_reg = BMP280_REG_ID, ··· 863 850 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 864 851 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1, 865 852 853 + .temp_coeffs = bmp280_temp_coeffs, 854 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 855 + .press_coeffs = bmp280_press_coeffs, 856 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 857 + 866 858 .chip_config = bmp280_chip_config, 867 859 .read_temp = bmp280_read_temp, 868 860 .read_press = bmp280_read_press, ··· 895 877 } 896 878 897 879 static const u8 bme280_chip_ids[] = { BME280_CHIP_ID }; 880 + static const int bme280_humid_coeffs[] = { 1000, 1024 }; 898 881 899 882 const struct bmp280_chip_info bme280_chip_info = { 900 883 .id_reg = BMP280_REG_ID, ··· 917 898 .oversampling_humid_avail = bmp280_oversampling_avail, 918 899 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), 919 900 .oversampling_humid_default = BME280_OSRS_HUMIDITY_16X - 1, 901 + 902 + .temp_coeffs = bmp280_temp_coeffs, 903 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 904 + .press_coeffs = bmp280_press_coeffs, 905 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 906 + .humid_coeffs = bme280_humid_coeffs, 907 + .humid_coeffs_type = IIO_VAL_FRACTIONAL, 920 908 921 909 .chip_config = bme280_chip_config, 922 910 .read_temp = bmp280_read_temp, ··· 1117 1091 return comp_press; 1118 1092 } 1119 1093 1120 - static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2) 1094 + static int bmp380_read_temp(struct bmp280_data *data, s32 *comp_temp) 1121 1095 { 1122 - s32 comp_temp; 1123 1096 u32 adc_temp; 1124 1097 int ret; 1125 1098 ··· 1126 1101 if (ret) 1127 1102 return ret; 1128 1103 1129 - comp_temp = bmp380_compensate_temp(data, adc_temp); 1104 + *comp_temp = bmp380_compensate_temp(data, adc_temp); 1130 1105 1131 - *val = comp_temp * 10; 1132 - return IIO_VAL_INT; 1106 + return 0; 1133 1107 } 1134 1108 1135 - static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2) 1109 + static int bmp380_read_press(struct bmp280_data *data, u32 *comp_press) 1136 1110 { 1137 - u32 adc_press, comp_press, t_fine; 1111 + u32 adc_press, t_fine; 1138 1112 int ret; 1139 1113 1140 1114 ret = bmp380_get_t_fine(data, &t_fine); ··· 1144 1120 if (ret) 1145 1121 return ret; 1146 1122 1147 - comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1123 + *comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1148 1124 1149 - *val = comp_press; 1150 - *val2 = 100000; 1151 - 1152 - return IIO_VAL_FRACTIONAL; 1125 + return 0; 1153 1126 } 1154 1127 1155 1128 static int bmp380_read_calib(struct bmp280_data *data) ··· 1317 1296 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; 1318 1297 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128}; 1319 1298 static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID }; 1299 + static const int bmp380_temp_coeffs[] = { 10, 1 }; 1300 + static const int bmp380_press_coeffs[] = { 1, 100000 }; 1320 1301 1321 1302 const struct bmp280_chip_info bmp380_chip_info = { 1322 1303 .id_reg = BMP380_REG_ID, ··· 1345 1322 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail, 1346 1323 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 1347 1324 .iir_filter_coeff_default = 2, 1325 + 1326 + .temp_coeffs = bmp380_temp_coeffs, 1327 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 1328 + .press_coeffs = bmp380_press_coeffs, 1329 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 1348 1330 1349 1331 .chip_config = bmp380_chip_config, 1350 1332 .read_temp = bmp380_read_temp, ··· 1471 1443 * for what is expected on IIO ABI. 1472 1444 */ 1473 1445 1474 - static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2) 1446 + static int bmp580_read_temp(struct bmp280_data *data, s32 *raw_temp) 1475 1447 { 1476 - s32 raw_temp; 1448 + s32 value_temp; 1477 1449 int ret; 1478 1450 1479 1451 ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf, ··· 1483 1455 return ret; 1484 1456 } 1485 1457 1486 - raw_temp = get_unaligned_le24(data->buf); 1487 - if (raw_temp == BMP580_TEMP_SKIPPED) { 1458 + value_temp = get_unaligned_le24(data->buf); 1459 + if (value_temp == BMP580_TEMP_SKIPPED) { 1488 1460 dev_err(data->dev, "reading temperature skipped\n"); 1489 1461 return -EIO; 1490 1462 } 1463 + *raw_temp = sign_extend32(value_temp, 23); 1491 1464 1492 - /* 1493 - * Temperature is returned in Celsius degrees in fractional 1494 - * form down 2^16. We rescale by x1000 to return millidegrees 1495 - * Celsius to respect IIO ABI. 1496 - */ 1497 - raw_temp = sign_extend32(raw_temp, 23); 1498 - *val = ((s64)raw_temp * 1000) / (1 << 16); 1499 - return IIO_VAL_INT; 1465 + return 0; 1500 1466 } 1501 1467 1502 - static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2) 1468 + static int bmp580_read_press(struct bmp280_data *data, u32 *raw_press) 1503 1469 { 1504 - u32 raw_press; 1470 + u32 value_press; 1505 1471 int ret; 1506 1472 1507 1473 ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf, ··· 1505 1483 return ret; 1506 1484 } 1507 1485 1508 - raw_press = get_unaligned_le24(data->buf); 1509 - if (raw_press == BMP580_PRESS_SKIPPED) { 1486 + value_press = get_unaligned_le24(data->buf); 1487 + if (value_press == BMP580_PRESS_SKIPPED) { 1510 1488 dev_err(data->dev, "reading pressure skipped\n"); 1511 1489 return -EIO; 1512 1490 } 1513 - /* 1514 - * Pressure is returned in Pascals in fractional form down 2^16. 1515 - * We rescale /1000 to convert to kilopascal to respect IIO ABI. 1516 - */ 1517 - *val = raw_press; 1518 - *val2 = 64000; /* 2^6 * 1000 */ 1519 - return IIO_VAL_FRACTIONAL; 1491 + *raw_press = value_press; 1492 + 1493 + return 0; 1520 1494 } 1521 1495 1522 1496 static const int bmp580_odr_table[][2] = { ··· 1848 1830 1849 1831 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 1850 1832 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT }; 1833 + /* Instead of { 1000, 16 } we do this, to avoid overflow issues */ 1834 + static const int bmp580_temp_coeffs[] = { 125, 13 }; 1835 + static const int bmp580_press_coeffs[] = { 1, 64000}; 1851 1836 1852 1837 const struct bmp280_chip_info bmp580_chip_info = { 1853 1838 .id_reg = BMP580_REG_CHIP_ID, ··· 1876 1855 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail, 1877 1856 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 1878 1857 .iir_filter_coeff_default = 2, 1858 + 1859 + .temp_coeffs = bmp580_temp_coeffs, 1860 + .temp_coeffs_type = IIO_VAL_FRACTIONAL_LOG2, 1861 + .press_coeffs = bmp580_press_coeffs, 1862 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 1879 1863 1880 1864 .chip_config = bmp580_chip_config, 1881 1865 .read_temp = bmp580_read_temp, ··· 2037 2011 return (bmp180_calc_t_fine(data, adc_temp) + 8) / 16; 2038 2012 } 2039 2013 2040 - static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2) 2014 + static int bmp180_read_temp(struct bmp280_data *data, s32 *comp_temp) 2041 2015 { 2042 - s32 comp_temp; 2043 2016 u32 adc_temp; 2044 2017 int ret; 2045 2018 ··· 2046 2021 if (ret) 2047 2022 return ret; 2048 2023 2049 - comp_temp = bmp180_compensate_temp(data, adc_temp); 2024 + *comp_temp = bmp180_compensate_temp(data, adc_temp); 2050 2025 2051 - *val = comp_temp * 100; 2052 - return IIO_VAL_INT; 2026 + return 0; 2053 2027 } 2054 2028 2055 2029 static int bmp180_read_press_adc(struct bmp280_data *data, u32 *adc_press) ··· 2111 2087 return p + ((x1 + x2 + 3791) >> 4); 2112 2088 } 2113 2089 2114 - static int bmp180_read_press(struct bmp280_data *data, int *val, int *val2) 2090 + static int bmp180_read_press(struct bmp280_data *data, u32 *comp_press) 2115 2091 { 2116 - u32 comp_press, adc_press; 2092 + u32 adc_press; 2117 2093 s32 t_fine; 2118 2094 int ret; 2119 2095 ··· 2125 2101 if (ret) 2126 2102 return ret; 2127 2103 2128 - comp_press = bmp180_compensate_press(data, adc_press, t_fine); 2104 + *comp_press = bmp180_compensate_press(data, adc_press, t_fine); 2129 2105 2130 - *val = comp_press; 2131 - *val2 = 1000; 2132 - 2133 - return IIO_VAL_FRACTIONAL; 2106 + return 0; 2134 2107 } 2135 2108 2136 2109 static int bmp180_chip_config(struct bmp280_data *data) ··· 2138 2117 static const int bmp180_oversampling_temp_avail[] = { 1 }; 2139 2118 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; 2140 2119 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID }; 2120 + static const int bmp180_temp_coeffs[] = { 100, 1 }; 2121 + static const int bmp180_press_coeffs[] = { 1, 1000 }; 2141 2122 2142 2123 const struct bmp280_chip_info bmp180_chip_info = { 2143 2124 .id_reg = BMP280_REG_ID, ··· 2159 2136 .num_oversampling_press_avail = 2160 2137 ARRAY_SIZE(bmp180_oversampling_press_avail), 2161 2138 .oversampling_press_default = BMP180_MEAS_PRESS_8X, 2139 + 2140 + .temp_coeffs = bmp180_temp_coeffs, 2141 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 2142 + .press_coeffs = bmp180_press_coeffs, 2143 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 2162 2144 2163 2145 .chip_config = bmp180_chip_config, 2164 2146 .read_temp = bmp180_read_temp,
+10 -3
drivers/iio/pressure/bmp280.h
··· 446 446 int num_sampling_freq_avail; 447 447 int sampling_freq_default; 448 448 449 + const int *temp_coeffs; 450 + const int temp_coeffs_type; 451 + const int *press_coeffs; 452 + const int press_coeffs_type; 453 + const int *humid_coeffs; 454 + const int humid_coeffs_type; 455 + 449 456 int (*chip_config)(struct bmp280_data *data); 450 - int (*read_temp)(struct bmp280_data *data, int *val, int *val2); 451 - int (*read_press)(struct bmp280_data *data, int *val, int *val2); 452 - int (*read_humid)(struct bmp280_data *data, int *val, int *val2); 457 + int (*read_temp)(struct bmp280_data *data, s32 *adc_temp); 458 + int (*read_press)(struct bmp280_data *data, u32 *adc_press); 459 + int (*read_humid)(struct bmp280_data *data, u32 *adc_humidity); 453 460 int (*read_calib)(struct bmp280_data *data); 454 461 int (*preinit)(struct bmp280_data *data); 455 462 };