1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com> 4 * Copyright (c) 2012 Bosch Sensortec GmbH 5 * Copyright (c) 2012 Unixphere AB 6 * Copyright (c) 2014 Intel Corporation 7 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org> 8 * 9 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor. 10 * 11 * Datasheet: 12 * https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf 13 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf 14 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf 15 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf 16 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp390-ds002.pdf 17 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp581-ds004.pdf 18 * 19 * Sensor API: 20 * https://github.com/boschsensortec/BME280_SensorAPI 21 * https://github.com/boschsensortec/BMP3_SensorAPI 22 * https://github.com/boschsensortec/BMP5_SensorAPI 23 * 24 * Notice: 25 * The link to the bmp180 datasheet points to an outdated version missing these changes: 26 * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26 27 * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4 28 * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26 29 */ 30 31 #define pr_fmt(fmt) "bmp280: " fmt 32 33 #include <linux/bitops.h> 34 #include <linux/bitfield.h> 35 #include <linux/cleanup.h> 36 #include <linux/completion.h> 37 #include <linux/delay.h> 38 #include <linux/device.h> 39 #include <linux/gpio/consumer.h> 40 #include <linux/interrupt.h> 41 #include <linux/irq.h> /* For irq_get_irq_data() */ 42 #include <linux/module.h> 43 #include <linux/nvmem-provider.h> 44 #include <linux/pm_runtime.h> 45 #include <linux/property.h> 46 #include <linux/random.h> 47 #include <linux/regmap.h> 48 #include <linux/regulator/consumer.h> 49 50 #include <linux/iio/buffer.h> 51 #include <linux/iio/iio.h> 52 #include <linux/iio/trigger.h> 53 #include <linux/iio/trigger_consumer.h> 54 #include <linux/iio/triggered_buffer.h> 55 56 #include <linux/unaligned.h> 57 58 #include "bmp280.h" 59 60 /* 61 * These enums are used for indexing into the array of calibration 62 * coefficients for BMP180. 63 */ 64 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD }; 65 66 enum bmp380_odr { 67 BMP380_ODR_200HZ, 68 BMP380_ODR_100HZ, 69 BMP380_ODR_50HZ, 70 BMP380_ODR_25HZ, 71 BMP380_ODR_12_5HZ, 72 BMP380_ODR_6_25HZ, 73 BMP380_ODR_3_125HZ, 74 BMP380_ODR_1_5625HZ, 75 BMP380_ODR_0_78HZ, 76 BMP380_ODR_0_39HZ, 77 BMP380_ODR_0_2HZ, 78 BMP380_ODR_0_1HZ, 79 BMP380_ODR_0_05HZ, 80 BMP380_ODR_0_02HZ, 81 BMP380_ODR_0_01HZ, 82 BMP380_ODR_0_006HZ, 83 BMP380_ODR_0_003HZ, 84 BMP380_ODR_0_0015HZ, 85 }; 86 87 enum bmp580_odr { 88 BMP580_ODR_240HZ, 89 BMP580_ODR_218HZ, 90 BMP580_ODR_199HZ, 91 BMP580_ODR_179HZ, 92 BMP580_ODR_160HZ, 93 BMP580_ODR_149HZ, 94 BMP580_ODR_140HZ, 95 BMP580_ODR_129HZ, 96 BMP580_ODR_120HZ, 97 BMP580_ODR_110HZ, 98 BMP580_ODR_100HZ, 99 BMP580_ODR_89HZ, 100 BMP580_ODR_80HZ, 101 BMP580_ODR_70HZ, 102 BMP580_ODR_60HZ, 103 BMP580_ODR_50HZ, 104 BMP580_ODR_45HZ, 105 BMP580_ODR_40HZ, 106 BMP580_ODR_35HZ, 107 BMP580_ODR_30HZ, 108 BMP580_ODR_25HZ, 109 BMP580_ODR_20HZ, 110 BMP580_ODR_15HZ, 111 BMP580_ODR_10HZ, 112 BMP580_ODR_5HZ, 113 BMP580_ODR_4HZ, 114 BMP580_ODR_3HZ, 115 BMP580_ODR_2HZ, 116 BMP580_ODR_1HZ, 117 BMP580_ODR_0_5HZ, 118 BMP580_ODR_0_25HZ, 119 BMP580_ODR_0_125HZ, 120 }; 121 122 /* 123 * These enums are used for indexing into the array of compensation 124 * parameters for BMP280. 125 */ 126 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 }; 127 128 enum { 129 /* Temperature calib indexes */ 130 BMP380_T1 = 0, 131 BMP380_T2 = 2, 132 BMP380_T3 = 4, 133 /* Pressure calib indexes */ 134 BMP380_P1 = 5, 135 BMP380_P2 = 7, 136 BMP380_P3 = 9, 137 BMP380_P4 = 10, 138 BMP380_P5 = 11, 139 BMP380_P6 = 13, 140 BMP380_P7 = 15, 141 BMP380_P8 = 16, 142 BMP380_P9 = 17, 143 BMP380_P10 = 19, 144 BMP380_P11 = 20, 145 }; 146 147 enum bmp280_scan { 148 BMP280_PRESS, 149 BMP280_TEMP, 150 BME280_HUMID, 151 }; 152 153 static const struct iio_chan_spec bmp280_channels[] = { 154 { 155 .type = IIO_PRESSURE, 156 /* PROCESSED maintained for ABI backwards compatibility */ 157 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 158 BIT(IIO_CHAN_INFO_RAW) | 159 BIT(IIO_CHAN_INFO_SCALE) | 160 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 161 .scan_index = 0, 162 .scan_type = { 163 .sign = 'u', 164 .realbits = 32, 165 .storagebits = 32, 166 .endianness = IIO_CPU, 167 }, 168 }, 169 { 170 .type = IIO_TEMP, 171 /* PROCESSED maintained for ABI backwards compatibility */ 172 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 173 BIT(IIO_CHAN_INFO_RAW) | 174 BIT(IIO_CHAN_INFO_SCALE) | 175 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 176 .scan_index = 1, 177 .scan_type = { 178 .sign = 's', 179 .realbits = 32, 180 .storagebits = 32, 181 .endianness = IIO_CPU, 182 }, 183 }, 184 IIO_CHAN_SOFT_TIMESTAMP(2), 185 }; 186 187 static const struct iio_chan_spec bme280_channels[] = { 188 { 189 .type = IIO_PRESSURE, 190 /* PROCESSED maintained for ABI backwards compatibility */ 191 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 192 BIT(IIO_CHAN_INFO_RAW) | 193 BIT(IIO_CHAN_INFO_SCALE) | 194 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 195 .scan_index = 0, 196 .scan_type = { 197 .sign = 'u', 198 .realbits = 32, 199 .storagebits = 32, 200 .endianness = IIO_CPU, 201 }, 202 }, 203 { 204 .type = IIO_TEMP, 205 /* PROCESSED maintained for ABI backwards compatibility */ 206 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 207 BIT(IIO_CHAN_INFO_RAW) | 208 BIT(IIO_CHAN_INFO_SCALE) | 209 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 210 .scan_index = 1, 211 .scan_type = { 212 .sign = 's', 213 .realbits = 32, 214 .storagebits = 32, 215 .endianness = IIO_CPU, 216 }, 217 }, 218 { 219 .type = IIO_HUMIDITYRELATIVE, 220 /* PROCESSED maintained for ABI backwards compatibility */ 221 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 222 BIT(IIO_CHAN_INFO_RAW) | 223 BIT(IIO_CHAN_INFO_SCALE) | 224 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 225 .scan_index = 2, 226 .scan_type = { 227 .sign = 'u', 228 .realbits = 32, 229 .storagebits = 32, 230 .endianness = IIO_CPU, 231 }, 232 }, 233 IIO_CHAN_SOFT_TIMESTAMP(3), 234 }; 235 236 static const struct iio_chan_spec bmp380_channels[] = { 237 { 238 .type = IIO_PRESSURE, 239 /* PROCESSED maintained for ABI backwards compatibility */ 240 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 241 BIT(IIO_CHAN_INFO_RAW) | 242 BIT(IIO_CHAN_INFO_SCALE) | 243 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 244 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 245 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 246 .scan_index = 0, 247 .scan_type = { 248 .sign = 'u', 249 .realbits = 32, 250 .storagebits = 32, 251 .endianness = IIO_CPU, 252 }, 253 }, 254 { 255 .type = IIO_TEMP, 256 /* PROCESSED maintained for ABI backwards compatibility */ 257 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 258 BIT(IIO_CHAN_INFO_RAW) | 259 BIT(IIO_CHAN_INFO_SCALE) | 260 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 261 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 262 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 263 .scan_index = 1, 264 .scan_type = { 265 .sign = 's', 266 .realbits = 32, 267 .storagebits = 32, 268 .endianness = IIO_CPU, 269 }, 270 }, 271 IIO_CHAN_SOFT_TIMESTAMP(2), 272 }; 273 274 static const struct iio_chan_spec bmp580_channels[] = { 275 { 276 .type = IIO_PRESSURE, 277 /* PROCESSED maintained for ABI backwards compatibility */ 278 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 279 BIT(IIO_CHAN_INFO_RAW) | 280 BIT(IIO_CHAN_INFO_SCALE) | 281 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 282 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 283 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 284 .scan_index = 0, 285 .scan_type = { 286 .sign = 'u', 287 .realbits = 24, 288 .storagebits = 32, 289 .endianness = IIO_LE, 290 }, 291 }, 292 { 293 .type = IIO_TEMP, 294 /* PROCESSED maintained for ABI backwards compatibility */ 295 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 296 BIT(IIO_CHAN_INFO_RAW) | 297 BIT(IIO_CHAN_INFO_SCALE) | 298 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 299 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 300 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 301 .scan_index = 1, 302 .scan_type = { 303 .sign = 's', 304 .realbits = 24, 305 .storagebits = 32, 306 .endianness = IIO_LE, 307 }, 308 }, 309 IIO_CHAN_SOFT_TIMESTAMP(2), 310 }; 311 312 static int bmp280_read_calib(struct bmp280_data *data) 313 { 314 struct bmp280_calib *calib = &data->calib.bmp280; 315 int ret; 316 317 /* Read temperature and pressure calibration values. */ 318 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START, 319 data->bmp280_cal_buf, 320 sizeof(data->bmp280_cal_buf)); 321 if (ret) { 322 dev_err(data->dev, 323 "failed to read calibration parameters\n"); 324 return ret; 325 } 326 327 /* Toss calibration data into the entropy pool */ 328 add_device_randomness(data->bmp280_cal_buf, 329 sizeof(data->bmp280_cal_buf)); 330 331 /* Parse temperature calibration values. */ 332 calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]); 333 calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]); 334 calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]); 335 336 /* Parse pressure calibration values. */ 337 calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]); 338 calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]); 339 calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]); 340 calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]); 341 calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]); 342 calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]); 343 calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]); 344 calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]); 345 calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]); 346 347 return 0; 348 } 349 350 /* 351 * These enums are used for indexing into the array of humidity parameters 352 * for BME280. Due to some weird indexing, unaligned BE/LE accesses co-exist in 353 * order to prepare the FIELD_{GET/PREP}() fields. Table 16 in Section 4.2.2 of 354 * the datasheet. 355 */ 356 enum { H2 = 0, H3 = 2, H4 = 3, H5 = 4, H6 = 6 }; 357 358 static int bme280_read_calib(struct bmp280_data *data) 359 { 360 struct bmp280_calib *calib = &data->calib.bmp280; 361 struct device *dev = data->dev; 362 s16 h4_upper, h4_lower, tmp_1, tmp_2, tmp_3; 363 unsigned int tmp; 364 int ret; 365 366 /* Load shared calibration params with bmp280 first */ 367 ret = bmp280_read_calib(data); 368 if (ret) 369 return ret; 370 371 ret = regmap_read(data->regmap, BME280_REG_COMP_H1, &tmp); 372 if (ret) { 373 dev_err(dev, "failed to read H1 comp value\n"); 374 return ret; 375 } 376 calib->H1 = tmp; 377 378 ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H2, 379 data->bme280_humid_cal_buf, 380 sizeof(data->bme280_humid_cal_buf)); 381 if (ret) { 382 dev_err(dev, "failed to read humidity calibration values\n"); 383 return ret; 384 } 385 386 calib->H2 = get_unaligned_le16(&data->bme280_humid_cal_buf[H2]); 387 calib->H3 = data->bme280_humid_cal_buf[H3]; 388 tmp_1 = get_unaligned_be16(&data->bme280_humid_cal_buf[H4]); 389 tmp_2 = FIELD_GET(BME280_COMP_H4_GET_MASK_UP, tmp_1); 390 h4_upper = FIELD_PREP(BME280_COMP_H4_PREP_MASK_UP, tmp_2); 391 h4_lower = FIELD_GET(BME280_COMP_H4_MASK_LOW, tmp_1); 392 calib->H4 = sign_extend32(h4_upper | h4_lower, 11); 393 tmp_3 = get_unaligned_le16(&data->bme280_humid_cal_buf[H5]); 394 calib->H5 = sign_extend32(FIELD_GET(BME280_COMP_H5_MASK, tmp_3), 11); 395 calib->H6 = data->bme280_humid_cal_buf[H6]; 396 397 return 0; 398 } 399 400 static int bme280_read_humid_adc(struct bmp280_data *data, u16 *adc_humidity) 401 { 402 u16 value_humidity; 403 int ret; 404 405 ret = regmap_bulk_read(data->regmap, BME280_REG_HUMIDITY_MSB, 406 &data->be16, BME280_NUM_HUMIDITY_BYTES); 407 if (ret) { 408 dev_err(data->dev, "failed to read humidity\n"); 409 return ret; 410 } 411 412 value_humidity = be16_to_cpu(data->be16); 413 if (value_humidity == BMP280_HUMIDITY_SKIPPED) { 414 dev_err(data->dev, "reading humidity skipped\n"); 415 return -EIO; 416 } 417 *adc_humidity = value_humidity; 418 419 return 0; 420 } 421 422 /* 423 * Returns humidity in percent, resolution is 0.01 percent. Output value of 424 * "47445" represents 47445/1024 = 46.333 %RH. 425 * 426 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula". 427 */ 428 static u32 bme280_compensate_humidity(struct bmp280_data *data, 429 u16 adc_humidity, s32 t_fine) 430 { 431 struct bmp280_calib *calib = &data->calib.bmp280; 432 s32 var; 433 434 var = t_fine - (s32)76800; 435 var = (((((s32)adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var)) 436 + (s32)16384) >> 15) * (((((((var * calib->H6) >> 10) 437 * (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10) 438 + (s32)2097152) * calib->H2 + 8192) >> 14); 439 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4; 440 441 var = clamp_val(var, 0, 419430400); 442 443 return var >> 12; 444 } 445 446 static int bmp280_read_temp_adc(struct bmp280_data *data, u32 *adc_temp) 447 { 448 u32 value_temp; 449 int ret; 450 451 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, 452 data->buf, BMP280_NUM_TEMP_BYTES); 453 if (ret) { 454 dev_err(data->dev, "failed to read temperature\n"); 455 return ret; 456 } 457 458 value_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf)); 459 if (value_temp == BMP280_TEMP_SKIPPED) { 460 dev_err(data->dev, "reading temperature skipped\n"); 461 return -EIO; 462 } 463 *adc_temp = value_temp; 464 465 return 0; 466 } 467 468 /* 469 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of 470 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global 471 * value. 472 * 473 * Taken from datasheet, Section 3.11.3, "Compensation formula". 474 */ 475 static s32 bmp280_calc_t_fine(struct bmp280_data *data, u32 adc_temp) 476 { 477 struct bmp280_calib *calib = &data->calib.bmp280; 478 s32 var1, var2; 479 480 var1 = (((((s32)adc_temp) >> 3) - ((s32)calib->T1 << 1)) * 481 ((s32)calib->T2)) >> 11; 482 var2 = (((((((s32)adc_temp) >> 4) - ((s32)calib->T1)) * 483 ((((s32)adc_temp >> 4) - ((s32)calib->T1))) >> 12) * 484 ((s32)calib->T3))) >> 14; 485 return var1 + var2; /* t_fine = var1 + var2 */ 486 } 487 488 static int bmp280_get_t_fine(struct bmp280_data *data, s32 *t_fine) 489 { 490 u32 adc_temp; 491 int ret; 492 493 ret = bmp280_read_temp_adc(data, &adc_temp); 494 if (ret) 495 return ret; 496 497 *t_fine = bmp280_calc_t_fine(data, adc_temp); 498 499 return 0; 500 } 501 502 static s32 bmp280_compensate_temp(struct bmp280_data *data, u32 adc_temp) 503 { 504 return (bmp280_calc_t_fine(data, adc_temp) * 5 + 128) / 256; 505 } 506 507 static int bmp280_read_press_adc(struct bmp280_data *data, u32 *adc_press) 508 { 509 u32 value_press; 510 int ret; 511 512 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 513 data->buf, BMP280_NUM_PRESS_BYTES); 514 if (ret) { 515 dev_err(data->dev, "failed to read pressure\n"); 516 return ret; 517 } 518 519 value_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf)); 520 if (value_press == BMP280_PRESS_SKIPPED) { 521 dev_err(data->dev, "reading pressure skipped\n"); 522 return -EIO; 523 } 524 *adc_press = value_press; 525 526 return 0; 527 } 528 529 /* 530 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 531 * integer bits and 8 fractional bits). Output value of "24674867" 532 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa 533 * 534 * Taken from datasheet, Section 3.11.3, "Compensation formula". 535 */ 536 static u32 bmp280_compensate_press(struct bmp280_data *data, 537 u32 adc_press, s32 t_fine) 538 { 539 struct bmp280_calib *calib = &data->calib.bmp280; 540 s64 var1, var2, p; 541 542 var1 = ((s64)t_fine) - 128000; 543 var2 = var1 * var1 * (s64)calib->P6; 544 var2 += (var1 * (s64)calib->P5) << 17; 545 var2 += ((s64)calib->P4) << 35; 546 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) + 547 ((var1 * (s64)calib->P2) << 12); 548 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33; 549 550 if (var1 == 0) 551 return 0; 552 553 p = ((((s64)1048576 - (s32)adc_press) << 31) - var2) * 3125; 554 p = div64_s64(p, var1); 555 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25; 556 var2 = ((s64)(calib->P8) * p) >> 19; 557 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4); 558 559 return (u32)p; 560 } 561 562 static int bmp280_read_temp(struct bmp280_data *data, s32 *comp_temp) 563 { 564 u32 adc_temp; 565 int ret; 566 567 ret = bmp280_read_temp_adc(data, &adc_temp); 568 if (ret) 569 return ret; 570 571 *comp_temp = bmp280_compensate_temp(data, adc_temp); 572 573 return 0; 574 } 575 576 static int bmp280_read_press(struct bmp280_data *data, u32 *comp_press) 577 { 578 u32 adc_press; 579 s32 t_fine; 580 int ret; 581 582 ret = bmp280_get_t_fine(data, &t_fine); 583 if (ret) 584 return ret; 585 586 ret = bmp280_read_press_adc(data, &adc_press); 587 if (ret) 588 return ret; 589 590 *comp_press = bmp280_compensate_press(data, adc_press, t_fine); 591 592 return 0; 593 } 594 595 static int bme280_read_humid(struct bmp280_data *data, u32 *comp_humidity) 596 { 597 u16 adc_humidity; 598 s32 t_fine; 599 int ret; 600 601 ret = bmp280_get_t_fine(data, &t_fine); 602 if (ret) 603 return ret; 604 605 ret = bme280_read_humid_adc(data, &adc_humidity); 606 if (ret) 607 return ret; 608 609 *comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine); 610 611 return 0; 612 } 613 614 static int bmp280_read_raw_impl(struct iio_dev *indio_dev, 615 struct iio_chan_spec const *chan, 616 int *val, int *val2, long mask) 617 { 618 struct bmp280_data *data = iio_priv(indio_dev); 619 int chan_value; 620 int ret; 621 622 guard(mutex)(&data->lock); 623 624 switch (mask) { 625 case IIO_CHAN_INFO_PROCESSED: 626 ret = data->chip_info->set_mode(data, BMP280_FORCED); 627 if (ret) 628 return ret; 629 630 ret = data->chip_info->wait_conv(data); 631 if (ret) 632 return ret; 633 634 switch (chan->type) { 635 case IIO_HUMIDITYRELATIVE: 636 ret = data->chip_info->read_humid(data, &chan_value); 637 if (ret) 638 return ret; 639 640 *val = data->chip_info->humid_coeffs[0] * chan_value; 641 *val2 = data->chip_info->humid_coeffs[1]; 642 return data->chip_info->humid_coeffs_type; 643 case IIO_PRESSURE: 644 ret = data->chip_info->read_press(data, &chan_value); 645 if (ret) 646 return ret; 647 648 *val = data->chip_info->press_coeffs[0] * chan_value; 649 *val2 = data->chip_info->press_coeffs[1]; 650 return data->chip_info->press_coeffs_type; 651 case IIO_TEMP: 652 ret = data->chip_info->read_temp(data, &chan_value); 653 if (ret) 654 return ret; 655 656 *val = data->chip_info->temp_coeffs[0] * chan_value; 657 *val2 = data->chip_info->temp_coeffs[1]; 658 return data->chip_info->temp_coeffs_type; 659 default: 660 return -EINVAL; 661 } 662 case IIO_CHAN_INFO_RAW: 663 ret = data->chip_info->set_mode(data, BMP280_FORCED); 664 if (ret) 665 return ret; 666 667 ret = data->chip_info->wait_conv(data); 668 if (ret) 669 return ret; 670 671 switch (chan->type) { 672 case IIO_HUMIDITYRELATIVE: 673 ret = data->chip_info->read_humid(data, &chan_value); 674 if (ret) 675 return ret; 676 677 *val = chan_value; 678 return IIO_VAL_INT; 679 case IIO_PRESSURE: 680 ret = data->chip_info->read_press(data, &chan_value); 681 if (ret) 682 return ret; 683 684 *val = chan_value; 685 return IIO_VAL_INT; 686 case IIO_TEMP: 687 ret = data->chip_info->read_temp(data, &chan_value); 688 if (ret) 689 return ret; 690 691 *val = chan_value; 692 return IIO_VAL_INT; 693 default: 694 return -EINVAL; 695 } 696 case IIO_CHAN_INFO_SCALE: 697 switch (chan->type) { 698 case IIO_HUMIDITYRELATIVE: 699 *val = data->chip_info->humid_coeffs[0]; 700 *val2 = data->chip_info->humid_coeffs[1]; 701 return data->chip_info->humid_coeffs_type; 702 case IIO_PRESSURE: 703 *val = data->chip_info->press_coeffs[0]; 704 *val2 = data->chip_info->press_coeffs[1]; 705 return data->chip_info->press_coeffs_type; 706 case IIO_TEMP: 707 *val = data->chip_info->temp_coeffs[0]; 708 *val2 = data->chip_info->temp_coeffs[1]; 709 return data->chip_info->temp_coeffs_type; 710 default: 711 return -EINVAL; 712 } 713 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 714 switch (chan->type) { 715 case IIO_HUMIDITYRELATIVE: 716 *val = 1 << data->oversampling_humid; 717 return IIO_VAL_INT; 718 case IIO_PRESSURE: 719 *val = 1 << data->oversampling_press; 720 return IIO_VAL_INT; 721 case IIO_TEMP: 722 *val = 1 << data->oversampling_temp; 723 return IIO_VAL_INT; 724 default: 725 return -EINVAL; 726 } 727 case IIO_CHAN_INFO_SAMP_FREQ: 728 if (!data->chip_info->sampling_freq_avail) 729 return -EINVAL; 730 731 *val = data->chip_info->sampling_freq_avail[data->sampling_freq][0]; 732 *val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1]; 733 return IIO_VAL_INT_PLUS_MICRO; 734 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 735 if (!data->chip_info->iir_filter_coeffs_avail) 736 return -EINVAL; 737 738 *val = (1 << data->iir_filter_coeff) - 1; 739 return IIO_VAL_INT; 740 default: 741 return -EINVAL; 742 } 743 } 744 745 static int bmp280_read_raw(struct iio_dev *indio_dev, 746 struct iio_chan_spec const *chan, 747 int *val, int *val2, long mask) 748 { 749 struct bmp280_data *data = iio_priv(indio_dev); 750 int ret; 751 752 pm_runtime_get_sync(data->dev); 753 ret = bmp280_read_raw_impl(indio_dev, chan, val, val2, mask); 754 pm_runtime_mark_last_busy(data->dev); 755 pm_runtime_put_autosuspend(data->dev); 756 757 return ret; 758 } 759 760 static int bme280_write_oversampling_ratio_humid(struct bmp280_data *data, 761 int val) 762 { 763 const int *avail = data->chip_info->oversampling_humid_avail; 764 const int n = data->chip_info->num_oversampling_humid_avail; 765 int ret, prev; 766 int i; 767 768 for (i = 0; i < n; i++) { 769 if (avail[i] == val) { 770 prev = data->oversampling_humid; 771 data->oversampling_humid = ilog2(val); 772 773 ret = data->chip_info->chip_config(data); 774 if (ret) { 775 data->oversampling_humid = prev; 776 data->chip_info->chip_config(data); 777 return ret; 778 } 779 return 0; 780 } 781 } 782 return -EINVAL; 783 } 784 785 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data, 786 int val) 787 { 788 const int *avail = data->chip_info->oversampling_temp_avail; 789 const int n = data->chip_info->num_oversampling_temp_avail; 790 int ret, prev; 791 int i; 792 793 for (i = 0; i < n; i++) { 794 if (avail[i] == val) { 795 prev = data->oversampling_temp; 796 data->oversampling_temp = ilog2(val); 797 798 ret = data->chip_info->chip_config(data); 799 if (ret) { 800 data->oversampling_temp = prev; 801 data->chip_info->chip_config(data); 802 return ret; 803 } 804 return 0; 805 } 806 } 807 return -EINVAL; 808 } 809 810 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data, 811 int val) 812 { 813 const int *avail = data->chip_info->oversampling_press_avail; 814 const int n = data->chip_info->num_oversampling_press_avail; 815 int ret, prev; 816 int i; 817 818 for (i = 0; i < n; i++) { 819 if (avail[i] == val) { 820 prev = data->oversampling_press; 821 data->oversampling_press = ilog2(val); 822 823 ret = data->chip_info->chip_config(data); 824 if (ret) { 825 data->oversampling_press = prev; 826 data->chip_info->chip_config(data); 827 return ret; 828 } 829 return 0; 830 } 831 } 832 return -EINVAL; 833 } 834 835 static int bmp280_write_sampling_frequency(struct bmp280_data *data, 836 int val, int val2) 837 { 838 const int (*avail)[2] = data->chip_info->sampling_freq_avail; 839 const int n = data->chip_info->num_sampling_freq_avail; 840 int ret, prev; 841 int i; 842 843 for (i = 0; i < n; i++) { 844 if (avail[i][0] == val && avail[i][1] == val2) { 845 prev = data->sampling_freq; 846 data->sampling_freq = i; 847 848 ret = data->chip_info->chip_config(data); 849 if (ret) { 850 data->sampling_freq = prev; 851 data->chip_info->chip_config(data); 852 return ret; 853 } 854 return 0; 855 } 856 } 857 return -EINVAL; 858 } 859 860 static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val) 861 { 862 const int *avail = data->chip_info->iir_filter_coeffs_avail; 863 const int n = data->chip_info->num_iir_filter_coeffs_avail; 864 int ret, prev; 865 int i; 866 867 for (i = 0; i < n; i++) { 868 if (avail[i] - 1 == val) { 869 prev = data->iir_filter_coeff; 870 data->iir_filter_coeff = i; 871 872 ret = data->chip_info->chip_config(data); 873 if (ret) { 874 data->iir_filter_coeff = prev; 875 data->chip_info->chip_config(data); 876 return ret; 877 878 } 879 return 0; 880 } 881 } 882 return -EINVAL; 883 } 884 885 static int bmp280_write_raw_impl(struct iio_dev *indio_dev, 886 struct iio_chan_spec const *chan, 887 int val, int val2, long mask) 888 { 889 struct bmp280_data *data = iio_priv(indio_dev); 890 891 guard(mutex)(&data->lock); 892 893 /* 894 * Helper functions to update sensor running configuration. 895 * If an error happens applying new settings, will try restore 896 * previous parameters to ensure the sensor is left in a known 897 * working configuration. 898 */ 899 switch (mask) { 900 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 901 switch (chan->type) { 902 case IIO_HUMIDITYRELATIVE: 903 return bme280_write_oversampling_ratio_humid(data, val); 904 case IIO_PRESSURE: 905 return bmp280_write_oversampling_ratio_press(data, val); 906 case IIO_TEMP: 907 return bmp280_write_oversampling_ratio_temp(data, val); 908 default: 909 return -EINVAL; 910 } 911 case IIO_CHAN_INFO_SAMP_FREQ: 912 return bmp280_write_sampling_frequency(data, val, val2); 913 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 914 return bmp280_write_iir_filter_coeffs(data, val); 915 default: 916 return -EINVAL; 917 } 918 } 919 920 static int bmp280_write_raw(struct iio_dev *indio_dev, 921 struct iio_chan_spec const *chan, 922 int val, int val2, long mask) 923 { 924 struct bmp280_data *data = iio_priv(indio_dev); 925 int ret; 926 927 pm_runtime_get_sync(data->dev); 928 ret = bmp280_write_raw_impl(indio_dev, chan, val, val2, mask); 929 pm_runtime_mark_last_busy(data->dev); 930 pm_runtime_put_autosuspend(data->dev); 931 932 return ret; 933 } 934 935 static int bmp280_read_avail(struct iio_dev *indio_dev, 936 struct iio_chan_spec const *chan, 937 const int **vals, int *type, int *length, 938 long mask) 939 { 940 struct bmp280_data *data = iio_priv(indio_dev); 941 942 switch (mask) { 943 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 944 switch (chan->type) { 945 case IIO_PRESSURE: 946 *vals = data->chip_info->oversampling_press_avail; 947 *length = data->chip_info->num_oversampling_press_avail; 948 break; 949 case IIO_TEMP: 950 *vals = data->chip_info->oversampling_temp_avail; 951 *length = data->chip_info->num_oversampling_temp_avail; 952 break; 953 default: 954 return -EINVAL; 955 } 956 *type = IIO_VAL_INT; 957 return IIO_AVAIL_LIST; 958 case IIO_CHAN_INFO_SAMP_FREQ: 959 *vals = (const int *)data->chip_info->sampling_freq_avail; 960 *type = IIO_VAL_INT_PLUS_MICRO; 961 /* Values are stored in a 2D matrix */ 962 *length = data->chip_info->num_sampling_freq_avail; 963 return IIO_AVAIL_LIST; 964 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 965 *vals = data->chip_info->iir_filter_coeffs_avail; 966 *type = IIO_VAL_INT; 967 *length = data->chip_info->num_iir_filter_coeffs_avail; 968 return IIO_AVAIL_LIST; 969 default: 970 return -EINVAL; 971 } 972 } 973 974 static const struct iio_info bmp280_info = { 975 .read_raw = &bmp280_read_raw, 976 .read_avail = &bmp280_read_avail, 977 .write_raw = &bmp280_write_raw, 978 }; 979 980 static const unsigned long bmp280_avail_scan_masks[] = { 981 BIT(BMP280_TEMP) | BIT(BMP280_PRESS), 982 0 983 }; 984 985 static const unsigned long bme280_avail_scan_masks[] = { 986 BIT(BME280_HUMID) | BIT(BMP280_TEMP) | BIT(BMP280_PRESS), 987 0 988 }; 989 990 static int bmp280_preinit(struct bmp280_data *data) 991 { 992 struct device *dev = data->dev; 993 unsigned int reg; 994 int ret; 995 996 ret = regmap_write(data->regmap, BMP280_REG_RESET, BMP280_RST_SOFT_CMD); 997 if (ret) 998 return dev_err_probe(dev, ret, "Failed to reset device.\n"); 999 1000 /* 1001 * According to the datasheet in Chapter 1: Specification, Table 2, 1002 * after resetting, the device uses the complete power-on sequence so 1003 * it needs to wait for the defined start-up time. 1004 */ 1005 fsleep(data->start_up_time); 1006 1007 ret = regmap_read(data->regmap, BMP280_REG_STATUS, ®); 1008 if (ret) 1009 return dev_err_probe(dev, ret, "Failed to read status register.\n"); 1010 1011 if (reg & BMP280_REG_STATUS_IM_UPDATE) 1012 return dev_err_probe(dev, -EIO, "Failed to copy NVM contents.\n"); 1013 1014 return 0; 1015 } 1016 1017 static const u8 bmp280_operation_mode[] = { 1018 [BMP280_SLEEP] = BMP280_MODE_SLEEP, 1019 [BMP280_FORCED] = BMP280_MODE_FORCED, 1020 [BMP280_NORMAL] = BMP280_MODE_NORMAL, 1021 }; 1022 1023 static int bmp280_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode) 1024 { 1025 int ret; 1026 1027 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS, 1028 BMP280_MODE_MASK, bmp280_operation_mode[mode]); 1029 if (ret) { 1030 dev_err(data->dev, "failed to write ctrl_meas register.\n"); 1031 return ret; 1032 } 1033 1034 data->op_mode = mode; 1035 1036 return 0; 1037 } 1038 1039 static int bmp280_wait_conv(struct bmp280_data *data) 1040 { 1041 unsigned int reg, meas_time_us; 1042 int ret; 1043 1044 /* Check if we are using a BME280 device */ 1045 if (data->oversampling_humid) 1046 meas_time_us = BMP280_PRESS_HUMID_MEAS_OFFSET + 1047 BIT(data->oversampling_humid) * BMP280_MEAS_DUR; 1048 1049 else 1050 meas_time_us = 0; 1051 1052 /* Pressure measurement time */ 1053 meas_time_us += BMP280_PRESS_HUMID_MEAS_OFFSET + 1054 BIT(data->oversampling_press) * BMP280_MEAS_DUR; 1055 1056 /* Temperature measurement time */ 1057 meas_time_us += BIT(data->oversampling_temp) * BMP280_MEAS_DUR; 1058 1059 /* Waiting time according to the BM(P/E)2 Sensor API */ 1060 fsleep(meas_time_us); 1061 1062 ret = regmap_read(data->regmap, BMP280_REG_STATUS, ®); 1063 if (ret) { 1064 dev_err(data->dev, "failed to read status register.\n"); 1065 return ret; 1066 } 1067 1068 if (reg & BMP280_REG_STATUS_MEAS_BIT) { 1069 dev_err(data->dev, "Measurement cycle didn't complete.\n"); 1070 return -EBUSY; 1071 } 1072 1073 return 0; 1074 } 1075 1076 static int bmp280_chip_config(struct bmp280_data *data) 1077 { 1078 u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) | 1079 FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1); 1080 int ret; 1081 1082 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS, 1083 BMP280_OSRS_TEMP_MASK | 1084 BMP280_OSRS_PRESS_MASK | 1085 BMP280_MODE_MASK, 1086 osrs | BMP280_MODE_SLEEP); 1087 if (ret) { 1088 dev_err(data->dev, "failed to write ctrl_meas register\n"); 1089 return ret; 1090 } 1091 1092 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG, 1093 BMP280_FILTER_MASK, 1094 BMP280_FILTER_4X); 1095 if (ret) { 1096 dev_err(data->dev, "failed to write config register\n"); 1097 return ret; 1098 } 1099 1100 return ret; 1101 } 1102 1103 static irqreturn_t bmp280_trigger_handler(int irq, void *p) 1104 { 1105 struct iio_poll_func *pf = p; 1106 struct iio_dev *indio_dev = pf->indio_dev; 1107 struct bmp280_data *data = iio_priv(indio_dev); 1108 u32 adc_temp, adc_press, comp_press; 1109 s32 t_fine, comp_temp; 1110 s32 *chans = (s32 *)data->sensor_data; 1111 int ret; 1112 1113 guard(mutex)(&data->lock); 1114 1115 /* Burst read data registers */ 1116 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 1117 data->buf, BMP280_BURST_READ_BYTES); 1118 if (ret) { 1119 dev_err(data->dev, "failed to burst read sensor data\n"); 1120 goto out; 1121 } 1122 1123 /* Temperature calculations */ 1124 adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3])); 1125 if (adc_temp == BMP280_TEMP_SKIPPED) { 1126 dev_err(data->dev, "reading temperature skipped\n"); 1127 goto out; 1128 } 1129 1130 comp_temp = bmp280_compensate_temp(data, adc_temp); 1131 1132 /* Pressure calculations */ 1133 adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0])); 1134 if (adc_press == BMP280_PRESS_SKIPPED) { 1135 dev_err(data->dev, "reading pressure skipped\n"); 1136 goto out; 1137 } 1138 1139 t_fine = bmp280_calc_t_fine(data, adc_temp); 1140 comp_press = bmp280_compensate_press(data, adc_press, t_fine); 1141 1142 chans[0] = comp_press; 1143 chans[1] = comp_temp; 1144 1145 iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data, 1146 iio_get_time_ns(indio_dev)); 1147 1148 out: 1149 iio_trigger_notify_done(indio_dev->trig); 1150 1151 return IRQ_HANDLED; 1152 } 1153 1154 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; 1155 static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID }; 1156 static const int bmp280_temp_coeffs[] = { 10, 1 }; 1157 static const int bmp280_press_coeffs[] = { 1, 256000 }; 1158 1159 const struct bmp280_chip_info bmp280_chip_info = { 1160 .id_reg = BMP280_REG_ID, 1161 .chip_id = bmp280_chip_ids, 1162 .num_chip_id = ARRAY_SIZE(bmp280_chip_ids), 1163 .regmap_config = &bmp280_regmap_config, 1164 .start_up_time = 2000, 1165 .channels = bmp280_channels, 1166 .num_channels = ARRAY_SIZE(bmp280_channels), 1167 .avail_scan_masks = bmp280_avail_scan_masks, 1168 1169 .oversampling_temp_avail = bmp280_oversampling_avail, 1170 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 1171 /* 1172 * Oversampling config values on BMx280 have one additional setting 1173 * that other generations of the family don't: 1174 * The value 0 means the measurement is bypassed instead of 1175 * oversampling set to x1. 1176 * 1177 * To account for this difference, and preserve the same common 1178 * config logic, this is handled later on chip_config callback 1179 * incrementing one unit the oversampling setting. 1180 */ 1181 .oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1, 1182 1183 .oversampling_press_avail = bmp280_oversampling_avail, 1184 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 1185 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1, 1186 1187 .temp_coeffs = bmp280_temp_coeffs, 1188 .temp_coeffs_type = IIO_VAL_FRACTIONAL, 1189 .press_coeffs = bmp280_press_coeffs, 1190 .press_coeffs_type = IIO_VAL_FRACTIONAL, 1191 1192 .chip_config = bmp280_chip_config, 1193 .read_temp = bmp280_read_temp, 1194 .read_press = bmp280_read_press, 1195 .read_calib = bmp280_read_calib, 1196 .set_mode = bmp280_set_mode, 1197 .wait_conv = bmp280_wait_conv, 1198 .preinit = bmp280_preinit, 1199 1200 .trigger_handler = bmp280_trigger_handler, 1201 }; 1202 EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280); 1203 1204 static int bme280_chip_config(struct bmp280_data *data) 1205 { 1206 u8 osrs = FIELD_PREP(BME280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1); 1207 int ret; 1208 1209 /* 1210 * Oversampling of humidity must be set before oversampling of 1211 * temperature/pressure is set to become effective. 1212 */ 1213 ret = regmap_update_bits(data->regmap, BME280_REG_CTRL_HUMIDITY, 1214 BME280_OSRS_HUMIDITY_MASK, osrs); 1215 if (ret) { 1216 dev_err(data->dev, "failed to set humidity oversampling"); 1217 return ret; 1218 } 1219 1220 return bmp280_chip_config(data); 1221 } 1222 1223 static irqreturn_t bme280_trigger_handler(int irq, void *p) 1224 { 1225 struct iio_poll_func *pf = p; 1226 struct iio_dev *indio_dev = pf->indio_dev; 1227 struct bmp280_data *data = iio_priv(indio_dev); 1228 u32 adc_temp, adc_press, adc_humidity, comp_press, comp_humidity; 1229 s32 t_fine, comp_temp; 1230 s32 *chans = (s32 *)data->sensor_data; 1231 int ret; 1232 1233 guard(mutex)(&data->lock); 1234 1235 /* Burst read data registers */ 1236 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 1237 data->buf, BME280_BURST_READ_BYTES); 1238 if (ret) { 1239 dev_err(data->dev, "failed to burst read sensor data\n"); 1240 goto out; 1241 } 1242 1243 /* Temperature calculations */ 1244 adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3])); 1245 if (adc_temp == BMP280_TEMP_SKIPPED) { 1246 dev_err(data->dev, "reading temperature skipped\n"); 1247 goto out; 1248 } 1249 1250 comp_temp = bmp280_compensate_temp(data, adc_temp); 1251 1252 /* Pressure calculations */ 1253 adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0])); 1254 if (adc_press == BMP280_PRESS_SKIPPED) { 1255 dev_err(data->dev, "reading pressure skipped\n"); 1256 goto out; 1257 } 1258 1259 t_fine = bmp280_calc_t_fine(data, adc_temp); 1260 comp_press = bmp280_compensate_press(data, adc_press, t_fine); 1261 1262 /* Humidity calculations */ 1263 adc_humidity = get_unaligned_be16(&data->buf[6]); 1264 1265 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) { 1266 dev_err(data->dev, "reading humidity skipped\n"); 1267 goto out; 1268 } 1269 1270 comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine); 1271 1272 chans[0] = comp_press; 1273 chans[1] = comp_temp; 1274 chans[2] = comp_humidity; 1275 1276 iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data, 1277 iio_get_time_ns(indio_dev)); 1278 1279 out: 1280 iio_trigger_notify_done(indio_dev->trig); 1281 1282 return IRQ_HANDLED; 1283 } 1284 1285 static int __bmp280_trigger_probe(struct iio_dev *indio_dev, 1286 const struct iio_trigger_ops *trigger_ops, 1287 int (*int_pin_config)(struct bmp280_data *data), 1288 irq_handler_t irq_thread_handler) 1289 { 1290 struct bmp280_data *data = iio_priv(indio_dev); 1291 struct device *dev = data->dev; 1292 u32 irq_type; 1293 int ret, irq; 1294 1295 irq = fwnode_irq_get(dev_fwnode(dev), 0); 1296 if (irq < 0) 1297 return dev_err_probe(dev, irq, "No interrupt found.\n"); 1298 1299 irq_type = irq_get_trigger_type(irq); 1300 switch (irq_type) { 1301 case IRQF_TRIGGER_RISING: 1302 data->trig_active_high = true; 1303 break; 1304 case IRQF_TRIGGER_FALLING: 1305 data->trig_active_high = false; 1306 break; 1307 default: 1308 return dev_err_probe(dev, -EINVAL, "Invalid interrupt type specified.\n"); 1309 } 1310 1311 data->trig_open_drain = 1312 fwnode_property_read_bool(dev_fwnode(dev), "int-open-drain"); 1313 1314 ret = int_pin_config(data); 1315 if (ret) 1316 return ret; 1317 1318 data->trig = devm_iio_trigger_alloc(data->dev, "%s-dev%d", 1319 indio_dev->name, 1320 iio_device_id(indio_dev)); 1321 if (!data->trig) 1322 return -ENOMEM; 1323 1324 data->trig->ops = trigger_ops; 1325 iio_trigger_set_drvdata(data->trig, data); 1326 1327 ret = devm_request_threaded_irq(data->dev, irq, NULL, 1328 irq_thread_handler, IRQF_ONESHOT, 1329 indio_dev->name, indio_dev); 1330 if (ret) 1331 return dev_err_probe(dev, ret, "request IRQ failed.\n"); 1332 1333 ret = devm_iio_trigger_register(data->dev, data->trig); 1334 if (ret) 1335 return dev_err_probe(dev, ret, "iio trigger register failed.\n"); 1336 1337 indio_dev->trig = iio_trigger_get(data->trig); 1338 1339 return 0; 1340 } 1341 1342 static const u8 bme280_chip_ids[] = { BME280_CHIP_ID }; 1343 static const int bme280_humid_coeffs[] = { 1000, 1024 }; 1344 1345 const struct bmp280_chip_info bme280_chip_info = { 1346 .id_reg = BMP280_REG_ID, 1347 .chip_id = bme280_chip_ids, 1348 .num_chip_id = ARRAY_SIZE(bme280_chip_ids), 1349 .regmap_config = &bme280_regmap_config, 1350 .start_up_time = 2000, 1351 .channels = bme280_channels, 1352 .num_channels = ARRAY_SIZE(bme280_channels), 1353 .avail_scan_masks = bme280_avail_scan_masks, 1354 1355 .oversampling_temp_avail = bmp280_oversampling_avail, 1356 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 1357 .oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1, 1358 1359 .oversampling_press_avail = bmp280_oversampling_avail, 1360 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 1361 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1, 1362 1363 .oversampling_humid_avail = bmp280_oversampling_avail, 1364 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), 1365 .oversampling_humid_default = BME280_OSRS_HUMIDITY_16X - 1, 1366 1367 .temp_coeffs = bmp280_temp_coeffs, 1368 .temp_coeffs_type = IIO_VAL_FRACTIONAL, 1369 .press_coeffs = bmp280_press_coeffs, 1370 .press_coeffs_type = IIO_VAL_FRACTIONAL, 1371 .humid_coeffs = bme280_humid_coeffs, 1372 .humid_coeffs_type = IIO_VAL_FRACTIONAL, 1373 1374 .chip_config = bme280_chip_config, 1375 .read_temp = bmp280_read_temp, 1376 .read_press = bmp280_read_press, 1377 .read_humid = bme280_read_humid, 1378 .read_calib = bme280_read_calib, 1379 .set_mode = bmp280_set_mode, 1380 .wait_conv = bmp280_wait_conv, 1381 .preinit = bmp280_preinit, 1382 1383 .trigger_handler = bme280_trigger_handler, 1384 }; 1385 EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280); 1386 1387 /* 1388 * Helper function to send a command to BMP3XX sensors. 1389 * 1390 * Sensor processes commands written to the CMD register and signals 1391 * execution result through "cmd_rdy" and "cmd_error" flags available on 1392 * STATUS and ERROR registers. 1393 */ 1394 static int bmp380_cmd(struct bmp280_data *data, u8 cmd) 1395 { 1396 unsigned int reg; 1397 int ret; 1398 1399 /* Check if device is ready to process a command */ 1400 ret = regmap_read(data->regmap, BMP380_REG_STATUS, ®); 1401 if (ret) { 1402 dev_err(data->dev, "failed to read error register\n"); 1403 return ret; 1404 } 1405 if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) { 1406 dev_err(data->dev, "device is not ready to accept commands\n"); 1407 return -EBUSY; 1408 } 1409 1410 /* Send command to process */ 1411 ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd); 1412 if (ret) { 1413 dev_err(data->dev, "failed to send command to device\n"); 1414 return ret; 1415 } 1416 /* Wait for 2ms for command to be processed */ 1417 usleep_range(data->start_up_time, data->start_up_time + 100); 1418 /* Check for command processing error */ 1419 ret = regmap_read(data->regmap, BMP380_REG_ERROR, ®); 1420 if (ret) { 1421 dev_err(data->dev, "error reading ERROR reg\n"); 1422 return ret; 1423 } 1424 if (reg & BMP380_ERR_CMD_MASK) { 1425 dev_err(data->dev, "error processing command 0x%X\n", cmd); 1426 return -EINVAL; 1427 } 1428 1429 return 0; 1430 } 1431 1432 static int bmp380_read_temp_adc(struct bmp280_data *data, u32 *adc_temp) 1433 { 1434 u32 value_temp; 1435 int ret; 1436 1437 ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB, 1438 data->buf, BMP280_NUM_TEMP_BYTES); 1439 if (ret) { 1440 dev_err(data->dev, "failed to read temperature\n"); 1441 return ret; 1442 } 1443 1444 value_temp = get_unaligned_le24(data->buf); 1445 if (value_temp == BMP380_TEMP_SKIPPED) { 1446 dev_err(data->dev, "reading temperature skipped\n"); 1447 return -EIO; 1448 } 1449 *adc_temp = value_temp; 1450 1451 return 0; 1452 } 1453 1454 /* 1455 * Returns temperature in Celsius degrees, resolution is 0.01º C. Output value 1456 * of "5123" equals 51.2º C. t_fine carries fine temperature as global value. 1457 * 1458 * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo 1459 * https://github.com/BoschSensortec/BMP3-Sensor-API. 1460 */ 1461 static s32 bmp380_calc_t_fine(struct bmp280_data *data, u32 adc_temp) 1462 { 1463 s64 var1, var2, var3, var4, var5, var6; 1464 struct bmp380_calib *calib = &data->calib.bmp380; 1465 1466 var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8); 1467 var2 = var1 * ((s64) calib->T2); 1468 var3 = var1 * var1; 1469 var4 = var3 * ((s64) calib->T3); 1470 var5 = (var2 << 18) + var4; 1471 var6 = var5 >> 32; 1472 return (s32)var6; /* t_fine = var6 */ 1473 } 1474 1475 static int bmp380_get_t_fine(struct bmp280_data *data, s32 *t_fine) 1476 { 1477 s32 adc_temp; 1478 int ret; 1479 1480 ret = bmp380_read_temp_adc(data, &adc_temp); 1481 if (ret) 1482 return ret; 1483 1484 *t_fine = bmp380_calc_t_fine(data, adc_temp); 1485 1486 return 0; 1487 } 1488 1489 static int bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp) 1490 { 1491 s64 comp_temp; 1492 s32 var6; 1493 1494 var6 = bmp380_calc_t_fine(data, adc_temp); 1495 comp_temp = (var6 * 25) >> 14; 1496 1497 comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP); 1498 return (s32) comp_temp; 1499 } 1500 1501 static int bmp380_read_press_adc(struct bmp280_data *data, u32 *adc_press) 1502 { 1503 u32 value_press; 1504 int ret; 1505 1506 ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, 1507 data->buf, BMP280_NUM_PRESS_BYTES); 1508 if (ret) { 1509 dev_err(data->dev, "failed to read pressure\n"); 1510 return ret; 1511 } 1512 1513 value_press = get_unaligned_le24(data->buf); 1514 if (value_press == BMP380_PRESS_SKIPPED) { 1515 dev_err(data->dev, "reading pressure skipped\n"); 1516 return -EIO; 1517 } 1518 *adc_press = value_press; 1519 1520 return 0; 1521 } 1522 1523 /* 1524 * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal. 1525 * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa. 1526 * 1527 * Taken from datasheet, Section 9.3. "Pressure compensation" and repository 1528 * https://github.com/BoschSensortec/BMP3-Sensor-API. 1529 */ 1530 static u32 bmp380_compensate_press(struct bmp280_data *data, 1531 u32 adc_press, s32 t_fine) 1532 { 1533 s64 var1, var2, var3, var4, var5, var6, offset, sensitivity; 1534 struct bmp380_calib *calib = &data->calib.bmp380; 1535 u32 comp_press; 1536 1537 var1 = (s64)t_fine * (s64)t_fine; 1538 var2 = var1 >> 6; 1539 var3 = (var2 * ((s64)t_fine)) >> 8; 1540 var4 = ((s64)calib->P8 * var3) >> 5; 1541 var5 = ((s64)calib->P7 * var1) << 4; 1542 var6 = ((s64)calib->P6 * (s64)t_fine) << 22; 1543 offset = ((s64)calib->P5 << 47) + var4 + var5 + var6; 1544 var2 = ((s64)calib->P4 * var3) >> 5; 1545 var4 = ((s64)calib->P3 * var1) << 2; 1546 var5 = ((s64)calib->P2 - ((s64)1 << 14)) * 1547 ((s64)t_fine << 21); 1548 sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) + 1549 var2 + var4 + var5; 1550 var1 = (sensitivity >> 24) * (s64)adc_press; 1551 var2 = (s64)calib->P10 * (s64)t_fine; 1552 var3 = var2 + ((s64)calib->P9 << 16); 1553 var4 = (var3 * (s64)adc_press) >> 13; 1554 1555 /* 1556 * Dividing by 10 followed by multiplying by 10 to avoid 1557 * possible overflow caused by (uncomp_data->pressure * partial_data4). 1558 */ 1559 var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9; 1560 var5 *= 10; 1561 var6 = (s64)adc_press * (s64)adc_press; 1562 var2 = ((s64)calib->P11 * var6) >> 16; 1563 var3 = (var2 * (s64)adc_press) >> 7; 1564 var4 = (offset >> 2) + var1 + var5 + var3; 1565 comp_press = ((u64)var4 * 25) >> 40; 1566 1567 comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES); 1568 return comp_press; 1569 } 1570 1571 static int bmp380_read_temp(struct bmp280_data *data, s32 *comp_temp) 1572 { 1573 u32 adc_temp; 1574 int ret; 1575 1576 ret = bmp380_read_temp_adc(data, &adc_temp); 1577 if (ret) 1578 return ret; 1579 1580 *comp_temp = bmp380_compensate_temp(data, adc_temp); 1581 1582 return 0; 1583 } 1584 1585 static int bmp380_read_press(struct bmp280_data *data, u32 *comp_press) 1586 { 1587 u32 adc_press, t_fine; 1588 int ret; 1589 1590 ret = bmp380_get_t_fine(data, &t_fine); 1591 if (ret) 1592 return ret; 1593 1594 ret = bmp380_read_press_adc(data, &adc_press); 1595 if (ret) 1596 return ret; 1597 1598 *comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1599 1600 return 0; 1601 } 1602 1603 static int bmp380_read_calib(struct bmp280_data *data) 1604 { 1605 struct bmp380_calib *calib = &data->calib.bmp380; 1606 int ret; 1607 1608 /* Read temperature and pressure calibration data */ 1609 ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START, 1610 data->bmp380_cal_buf, 1611 sizeof(data->bmp380_cal_buf)); 1612 if (ret) { 1613 dev_err(data->dev, 1614 "failed to read calibration parameters\n"); 1615 return ret; 1616 } 1617 1618 /* Toss the temperature calibration data into the entropy pool */ 1619 add_device_randomness(data->bmp380_cal_buf, 1620 sizeof(data->bmp380_cal_buf)); 1621 1622 /* Parse calibration values */ 1623 calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]); 1624 calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]); 1625 calib->T3 = data->bmp380_cal_buf[BMP380_T3]; 1626 calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]); 1627 calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]); 1628 calib->P3 = data->bmp380_cal_buf[BMP380_P3]; 1629 calib->P4 = data->bmp380_cal_buf[BMP380_P4]; 1630 calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]); 1631 calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]); 1632 calib->P7 = data->bmp380_cal_buf[BMP380_P7]; 1633 calib->P8 = data->bmp380_cal_buf[BMP380_P8]; 1634 calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]); 1635 calib->P10 = data->bmp380_cal_buf[BMP380_P10]; 1636 calib->P11 = data->bmp380_cal_buf[BMP380_P11]; 1637 1638 return 0; 1639 } 1640 1641 static const int bmp380_odr_table[][2] = { 1642 [BMP380_ODR_200HZ] = {200, 0}, 1643 [BMP380_ODR_100HZ] = {100, 0}, 1644 [BMP380_ODR_50HZ] = {50, 0}, 1645 [BMP380_ODR_25HZ] = {25, 0}, 1646 [BMP380_ODR_12_5HZ] = {12, 500000}, 1647 [BMP380_ODR_6_25HZ] = {6, 250000}, 1648 [BMP380_ODR_3_125HZ] = {3, 125000}, 1649 [BMP380_ODR_1_5625HZ] = {1, 562500}, 1650 [BMP380_ODR_0_78HZ] = {0, 781250}, 1651 [BMP380_ODR_0_39HZ] = {0, 390625}, 1652 [BMP380_ODR_0_2HZ] = {0, 195313}, 1653 [BMP380_ODR_0_1HZ] = {0, 97656}, 1654 [BMP380_ODR_0_05HZ] = {0, 48828}, 1655 [BMP380_ODR_0_02HZ] = {0, 24414}, 1656 [BMP380_ODR_0_01HZ] = {0, 12207}, 1657 [BMP380_ODR_0_006HZ] = {0, 6104}, 1658 [BMP380_ODR_0_003HZ] = {0, 3052}, 1659 [BMP380_ODR_0_0015HZ] = {0, 1526}, 1660 }; 1661 1662 static int bmp380_preinit(struct bmp280_data *data) 1663 { 1664 /* BMP3xx requires soft-reset as part of initialization */ 1665 return bmp380_cmd(data, BMP380_CMD_SOFT_RESET); 1666 } 1667 1668 static const u8 bmp380_operation_mode[] = { 1669 [BMP280_SLEEP] = BMP380_MODE_SLEEP, 1670 [BMP280_FORCED] = BMP380_MODE_FORCED, 1671 [BMP280_NORMAL] = BMP380_MODE_NORMAL, 1672 }; 1673 1674 static int bmp380_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode) 1675 { 1676 int ret; 1677 1678 ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL, 1679 BMP380_MODE_MASK, 1680 FIELD_PREP(BMP380_MODE_MASK, 1681 bmp380_operation_mode[mode])); 1682 if (ret) { 1683 dev_err(data->dev, "failed to write power control register.\n"); 1684 return ret; 1685 } 1686 1687 data->op_mode = mode; 1688 1689 return 0; 1690 } 1691 1692 static int bmp380_wait_conv(struct bmp280_data *data) 1693 { 1694 unsigned int reg; 1695 int ret, meas_time_us; 1696 1697 /* Offset measurement time */ 1698 meas_time_us = BMP380_MEAS_OFFSET; 1699 1700 /* Pressure measurement time */ 1701 meas_time_us += BMP380_PRESS_MEAS_OFFSET + 1702 BIT(data->oversampling_press) * BMP380_MEAS_DUR; 1703 1704 /* Temperature measurement time */ 1705 meas_time_us += BMP380_TEMP_MEAS_OFFSET + 1706 BIT(data->oversampling_temp) * BMP380_MEAS_DUR; 1707 1708 /* Measurement time defined in Datasheet Section 3.9.2 */ 1709 fsleep(meas_time_us); 1710 1711 ret = regmap_read(data->regmap, BMP380_REG_STATUS, ®); 1712 if (ret) { 1713 dev_err(data->dev, "failed to read status register.\n"); 1714 return ret; 1715 } 1716 1717 if (!((reg & BMP380_STATUS_DRDY_PRESS_MASK) && 1718 (reg & BMP380_STATUS_DRDY_TEMP_MASK))) { 1719 dev_err(data->dev, "Measurement cycle didn't complete.\n"); 1720 return -EBUSY; 1721 } 1722 1723 return 0; 1724 } 1725 1726 static int bmp380_chip_config(struct bmp280_data *data) 1727 { 1728 bool change = false, aux; 1729 unsigned int tmp; 1730 u8 osrs; 1731 int ret; 1732 1733 /* Configure power control register */ 1734 ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL, 1735 BMP380_CTRL_SENSORS_MASK, 1736 BMP380_CTRL_SENSORS_PRESS_EN | 1737 BMP380_CTRL_SENSORS_TEMP_EN); 1738 if (ret) { 1739 dev_err(data->dev, 1740 "failed to write operation control register\n"); 1741 return ret; 1742 } 1743 1744 /* Configure oversampling */ 1745 osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) | 1746 FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press); 1747 1748 ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR, 1749 BMP380_OSRS_TEMP_MASK | 1750 BMP380_OSRS_PRESS_MASK, 1751 osrs, &aux); 1752 if (ret) { 1753 dev_err(data->dev, "failed to write oversampling register\n"); 1754 return ret; 1755 } 1756 change = change || aux; 1757 1758 /* Configure output data rate */ 1759 ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR, 1760 BMP380_ODRS_MASK, data->sampling_freq, 1761 &aux); 1762 if (ret) { 1763 dev_err(data->dev, "failed to write ODR selection register\n"); 1764 return ret; 1765 } 1766 change = change || aux; 1767 1768 /* Set filter data */ 1769 ret = regmap_update_bits(data->regmap, BMP380_REG_CONFIG, BMP380_FILTER_MASK, 1770 FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff)); 1771 if (ret) { 1772 dev_err(data->dev, "failed to write config register\n"); 1773 return ret; 1774 } 1775 1776 if (change) { 1777 /* 1778 * The configurations errors are detected on the fly during a 1779 * measurement cycle. If the sampling frequency is too low, it's 1780 * faster to reset the measurement loop than wait until the next 1781 * measurement is due. 1782 * 1783 * Resets sensor measurement loop toggling between sleep and 1784 * normal operating modes. 1785 */ 1786 ret = bmp380_set_mode(data, BMP280_SLEEP); 1787 if (ret) { 1788 dev_err(data->dev, "failed to set sleep mode\n"); 1789 return ret; 1790 } 1791 1792 /* 1793 * According to the BMP3 Sensor API, the sensor needs 5ms 1794 * in order to go to the sleep mode. 1795 */ 1796 fsleep(5 * USEC_PER_MSEC); 1797 1798 ret = bmp380_set_mode(data, BMP280_NORMAL); 1799 if (ret) { 1800 dev_err(data->dev, "failed to set normal mode\n"); 1801 return ret; 1802 } 1803 /* 1804 * Waits for measurement before checking configuration error 1805 * flag. Selected longest measurement time, calculated from 1806 * formula in datasheet section 3.9.2 with an offset of ~+15% 1807 * as it seen as well in table 3.9.1. 1808 */ 1809 msleep(150); 1810 1811 /* Check config error flag */ 1812 ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp); 1813 if (ret) { 1814 dev_err(data->dev, "failed to read error register\n"); 1815 return ret; 1816 } 1817 if (tmp & BMP380_ERR_CONF_MASK) { 1818 dev_warn(data->dev, 1819 "sensor flagged configuration as incompatible\n"); 1820 return -EINVAL; 1821 } 1822 } 1823 1824 /* Dummy read to empty data registers. */ 1825 ret = bmp380_read_press(data, &tmp); 1826 if (ret) 1827 return ret; 1828 1829 ret = bmp380_set_mode(data, BMP280_SLEEP); 1830 if (ret) 1831 dev_err(data->dev, "failed to set sleep mode.\n"); 1832 1833 return ret; 1834 } 1835 1836 static int bmp380_data_rdy_trigger_set_state(struct iio_trigger *trig, 1837 bool state) 1838 { 1839 struct bmp280_data *data = iio_trigger_get_drvdata(trig); 1840 int ret; 1841 1842 guard(mutex)(&data->lock); 1843 1844 ret = regmap_update_bits(data->regmap, BMP380_REG_INT_CONTROL, 1845 BMP380_INT_CTRL_DRDY_EN, 1846 FIELD_PREP(BMP380_INT_CTRL_DRDY_EN, !!state)); 1847 if (ret) 1848 dev_err(data->dev, 1849 "Could not %s interrupt.\n", str_enable_disable(state)); 1850 return ret; 1851 } 1852 1853 static const struct iio_trigger_ops bmp380_trigger_ops = { 1854 .set_trigger_state = &bmp380_data_rdy_trigger_set_state, 1855 }; 1856 1857 static int bmp380_int_pin_config(struct bmp280_data *data) 1858 { 1859 int pin_drive_cfg = FIELD_PREP(BMP380_INT_CTRL_OPEN_DRAIN, 1860 data->trig_open_drain); 1861 int pin_level_cfg = FIELD_PREP(BMP380_INT_CTRL_LEVEL, 1862 data->trig_active_high); 1863 int ret, int_pin_cfg = pin_drive_cfg | pin_level_cfg; 1864 1865 ret = regmap_update_bits(data->regmap, BMP380_REG_INT_CONTROL, 1866 BMP380_INT_CTRL_SETTINGS_MASK, int_pin_cfg); 1867 if (ret) 1868 dev_err(data->dev, "Could not set interrupt settings.\n"); 1869 1870 return ret; 1871 } 1872 1873 static irqreturn_t bmp380_irq_thread_handler(int irq, void *p) 1874 { 1875 struct iio_dev *indio_dev = p; 1876 struct bmp280_data *data = iio_priv(indio_dev); 1877 unsigned int int_ctrl; 1878 int ret; 1879 1880 ret = regmap_read(data->regmap, BMP380_REG_INT_STATUS, &int_ctrl); 1881 if (ret) 1882 return IRQ_NONE; 1883 1884 if (FIELD_GET(BMP380_INT_STATUS_DRDY, int_ctrl)) 1885 iio_trigger_poll_nested(data->trig); 1886 1887 return IRQ_HANDLED; 1888 } 1889 1890 static int bmp380_trigger_probe(struct iio_dev *indio_dev) 1891 { 1892 return __bmp280_trigger_probe(indio_dev, &bmp380_trigger_ops, 1893 bmp380_int_pin_config, 1894 bmp380_irq_thread_handler); 1895 } 1896 1897 static irqreturn_t bmp380_trigger_handler(int irq, void *p) 1898 { 1899 struct iio_poll_func *pf = p; 1900 struct iio_dev *indio_dev = pf->indio_dev; 1901 struct bmp280_data *data = iio_priv(indio_dev); 1902 u32 adc_temp, adc_press, comp_press; 1903 s32 t_fine, comp_temp; 1904 s32 *chans = (s32 *)data->sensor_data; 1905 int ret; 1906 1907 guard(mutex)(&data->lock); 1908 1909 /* Burst read data registers */ 1910 ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, 1911 data->buf, BMP280_BURST_READ_BYTES); 1912 if (ret) { 1913 dev_err(data->dev, "failed to burst read sensor data\n"); 1914 goto out; 1915 } 1916 1917 /* Temperature calculations */ 1918 adc_temp = get_unaligned_le24(&data->buf[3]); 1919 if (adc_temp == BMP380_TEMP_SKIPPED) { 1920 dev_err(data->dev, "reading temperature skipped\n"); 1921 goto out; 1922 } 1923 1924 comp_temp = bmp380_compensate_temp(data, adc_temp); 1925 1926 /* Pressure calculations */ 1927 adc_press = get_unaligned_le24(&data->buf[0]); 1928 if (adc_press == BMP380_PRESS_SKIPPED) { 1929 dev_err(data->dev, "reading pressure skipped\n"); 1930 goto out; 1931 } 1932 1933 t_fine = bmp380_calc_t_fine(data, adc_temp); 1934 comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1935 1936 chans[0] = comp_press; 1937 chans[1] = comp_temp; 1938 1939 iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data, 1940 iio_get_time_ns(indio_dev)); 1941 1942 out: 1943 iio_trigger_notify_done(indio_dev->trig); 1944 1945 return IRQ_HANDLED; 1946 } 1947 1948 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; 1949 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128}; 1950 static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID }; 1951 static const int bmp380_temp_coeffs[] = { 10, 1 }; 1952 static const int bmp380_press_coeffs[] = { 1, 100000 }; 1953 1954 const struct bmp280_chip_info bmp380_chip_info = { 1955 .id_reg = BMP380_REG_ID, 1956 .chip_id = bmp380_chip_ids, 1957 .num_chip_id = ARRAY_SIZE(bmp380_chip_ids), 1958 .regmap_config = &bmp380_regmap_config, 1959 .spi_read_extra_byte = true, 1960 .start_up_time = 2000, 1961 .channels = bmp380_channels, 1962 .num_channels = ARRAY_SIZE(bmp380_channels), 1963 .avail_scan_masks = bmp280_avail_scan_masks, 1964 1965 .oversampling_temp_avail = bmp380_oversampling_avail, 1966 .num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail), 1967 .oversampling_temp_default = ilog2(1), 1968 1969 .oversampling_press_avail = bmp380_oversampling_avail, 1970 .num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail), 1971 .oversampling_press_default = ilog2(4), 1972 1973 .sampling_freq_avail = bmp380_odr_table, 1974 .num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2, 1975 .sampling_freq_default = BMP380_ODR_50HZ, 1976 1977 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail, 1978 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 1979 .iir_filter_coeff_default = 2, 1980 1981 .temp_coeffs = bmp380_temp_coeffs, 1982 .temp_coeffs_type = IIO_VAL_FRACTIONAL, 1983 .press_coeffs = bmp380_press_coeffs, 1984 .press_coeffs_type = IIO_VAL_FRACTIONAL, 1985 1986 .chip_config = bmp380_chip_config, 1987 .read_temp = bmp380_read_temp, 1988 .read_press = bmp380_read_press, 1989 .read_calib = bmp380_read_calib, 1990 .set_mode = bmp380_set_mode, 1991 .wait_conv = bmp380_wait_conv, 1992 .preinit = bmp380_preinit, 1993 1994 .trigger_probe = bmp380_trigger_probe, 1995 .trigger_handler = bmp380_trigger_handler, 1996 }; 1997 EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280); 1998 1999 static int bmp580_soft_reset(struct bmp280_data *data) 2000 { 2001 unsigned int reg; 2002 int ret; 2003 2004 ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET); 2005 if (ret) { 2006 dev_err(data->dev, "failed to send reset command to device\n"); 2007 return ret; 2008 } 2009 usleep_range(2000, 2500); 2010 2011 /* Dummy read of chip_id */ 2012 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®); 2013 if (ret) { 2014 dev_err(data->dev, "failed to reestablish comms after reset\n"); 2015 return ret; 2016 } 2017 2018 ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, ®); 2019 if (ret) { 2020 dev_err(data->dev, "error reading interrupt status register\n"); 2021 return ret; 2022 } 2023 if (!(reg & BMP580_INT_STATUS_POR_MASK)) { 2024 dev_err(data->dev, "error resetting sensor\n"); 2025 return -EINVAL; 2026 } 2027 2028 return 0; 2029 } 2030 2031 /** 2032 * bmp580_nvm_operation() - Helper function to commit NVM memory operations 2033 * @data: sensor data struct 2034 * @is_write: flag to signal write operation 2035 */ 2036 static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write) 2037 { 2038 unsigned long timeout, poll; 2039 unsigned int reg; 2040 int ret; 2041 2042 /* Check NVM ready flag */ 2043 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®); 2044 if (ret) { 2045 dev_err(data->dev, "failed to check nvm status\n"); 2046 return ret; 2047 } 2048 if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) { 2049 dev_err(data->dev, "sensor's nvm is not ready\n"); 2050 return -EIO; 2051 } 2052 2053 /* Start NVM operation sequence */ 2054 ret = regmap_write(data->regmap, BMP580_REG_CMD, 2055 BMP580_CMD_NVM_OP_SEQ_0); 2056 if (ret) { 2057 dev_err(data->dev, 2058 "failed to send nvm operation's first sequence\n"); 2059 return ret; 2060 } 2061 if (is_write) { 2062 /* Send NVM write sequence */ 2063 ret = regmap_write(data->regmap, BMP580_REG_CMD, 2064 BMP580_CMD_NVM_WRITE_SEQ_1); 2065 if (ret) { 2066 dev_err(data->dev, 2067 "failed to send nvm write sequence\n"); 2068 return ret; 2069 } 2070 /* Datasheet says on 4.8.1.2 it takes approximately 10ms */ 2071 poll = 2000; 2072 timeout = 12000; 2073 } else { 2074 /* Send NVM read sequence */ 2075 ret = regmap_write(data->regmap, BMP580_REG_CMD, 2076 BMP580_CMD_NVM_READ_SEQ_1); 2077 if (ret) { 2078 dev_err(data->dev, 2079 "failed to send nvm read sequence\n"); 2080 return ret; 2081 } 2082 /* Datasheet says on 4.8.1.1 it takes approximately 200us */ 2083 poll = 50; 2084 timeout = 400; 2085 } 2086 2087 /* Wait until NVM is ready again */ 2088 ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg, 2089 (reg & BMP580_STATUS_NVM_RDY_MASK), 2090 poll, timeout); 2091 if (ret) { 2092 dev_err(data->dev, "error checking nvm operation status\n"); 2093 return ret; 2094 } 2095 2096 /* Check NVM error flags */ 2097 if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) { 2098 dev_err(data->dev, "error processing nvm operation\n"); 2099 return -EIO; 2100 } 2101 2102 return 0; 2103 } 2104 2105 /* 2106 * Contrary to previous sensors families, compensation algorithm is builtin. 2107 * We are only required to read the register raw data and adapt the ranges 2108 * for what is expected on IIO ABI. 2109 */ 2110 2111 static int bmp580_read_temp(struct bmp280_data *data, s32 *raw_temp) 2112 { 2113 s32 value_temp; 2114 int ret; 2115 2116 ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, 2117 data->buf, BMP280_NUM_TEMP_BYTES); 2118 if (ret) { 2119 dev_err(data->dev, "failed to read temperature\n"); 2120 return ret; 2121 } 2122 2123 value_temp = get_unaligned_le24(data->buf); 2124 if (value_temp == BMP580_TEMP_SKIPPED) { 2125 dev_err(data->dev, "reading temperature skipped\n"); 2126 return -EIO; 2127 } 2128 *raw_temp = sign_extend32(value_temp, 23); 2129 2130 return 0; 2131 } 2132 2133 static int bmp580_read_press(struct bmp280_data *data, u32 *raw_press) 2134 { 2135 u32 value_press; 2136 int ret; 2137 2138 ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, 2139 data->buf, BMP280_NUM_PRESS_BYTES); 2140 if (ret) { 2141 dev_err(data->dev, "failed to read pressure\n"); 2142 return ret; 2143 } 2144 2145 value_press = get_unaligned_le24(data->buf); 2146 if (value_press == BMP580_PRESS_SKIPPED) { 2147 dev_err(data->dev, "reading pressure skipped\n"); 2148 return -EIO; 2149 } 2150 *raw_press = value_press; 2151 2152 return 0; 2153 } 2154 2155 static const int bmp580_odr_table[][2] = { 2156 [BMP580_ODR_240HZ] = {240, 0}, 2157 [BMP580_ODR_218HZ] = {218, 0}, 2158 [BMP580_ODR_199HZ] = {199, 0}, 2159 [BMP580_ODR_179HZ] = {179, 0}, 2160 [BMP580_ODR_160HZ] = {160, 0}, 2161 [BMP580_ODR_149HZ] = {149, 0}, 2162 [BMP580_ODR_140HZ] = {140, 0}, 2163 [BMP580_ODR_129HZ] = {129, 0}, 2164 [BMP580_ODR_120HZ] = {120, 0}, 2165 [BMP580_ODR_110HZ] = {110, 0}, 2166 [BMP580_ODR_100HZ] = {100, 0}, 2167 [BMP580_ODR_89HZ] = {89, 0}, 2168 [BMP580_ODR_80HZ] = {80, 0}, 2169 [BMP580_ODR_70HZ] = {70, 0}, 2170 [BMP580_ODR_60HZ] = {60, 0}, 2171 [BMP580_ODR_50HZ] = {50, 0}, 2172 [BMP580_ODR_45HZ] = {45, 0}, 2173 [BMP580_ODR_40HZ] = {40, 0}, 2174 [BMP580_ODR_35HZ] = {35, 0}, 2175 [BMP580_ODR_30HZ] = {30, 0}, 2176 [BMP580_ODR_25HZ] = {25, 0}, 2177 [BMP580_ODR_20HZ] = {20, 0}, 2178 [BMP580_ODR_15HZ] = {15, 0}, 2179 [BMP580_ODR_10HZ] = {10, 0}, 2180 [BMP580_ODR_5HZ] = {5, 0}, 2181 [BMP580_ODR_4HZ] = {4, 0}, 2182 [BMP580_ODR_3HZ] = {3, 0}, 2183 [BMP580_ODR_2HZ] = {2, 0}, 2184 [BMP580_ODR_1HZ] = {1, 0}, 2185 [BMP580_ODR_0_5HZ] = {0, 500000}, 2186 [BMP580_ODR_0_25HZ] = {0, 250000}, 2187 [BMP580_ODR_0_125HZ] = {0, 125000}, 2188 }; 2189 2190 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 }; 2191 2192 static int bmp580_nvmem_read_impl(void *priv, unsigned int offset, void *val, 2193 size_t bytes) 2194 { 2195 struct bmp280_data *data = priv; 2196 u16 *dst = val; 2197 int ret, addr; 2198 2199 guard(mutex)(&data->lock); 2200 2201 /* Set sensor in standby mode */ 2202 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, 2203 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS, 2204 BMP580_ODR_DEEPSLEEP_DIS | 2205 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP)); 2206 if (ret) { 2207 dev_err(data->dev, "failed to change sensor to standby mode\n"); 2208 goto exit; 2209 } 2210 /* Wait standby transition time */ 2211 usleep_range(2500, 3000); 2212 2213 while (bytes >= sizeof(*dst)) { 2214 addr = bmp580_nvmem_addrs[offset / sizeof(*dst)]; 2215 2216 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, 2217 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr)); 2218 if (ret) { 2219 dev_err(data->dev, "error writing nvm address\n"); 2220 goto exit; 2221 } 2222 2223 ret = bmp580_nvm_operation(data, false); 2224 if (ret) 2225 goto exit; 2226 2227 ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB, 2228 &data->le16, sizeof(data->le16)); 2229 if (ret) { 2230 dev_err(data->dev, "error reading nvm data regs\n"); 2231 goto exit; 2232 } 2233 2234 *dst++ = le16_to_cpu(data->le16); 2235 bytes -= sizeof(*dst); 2236 offset += sizeof(*dst); 2237 } 2238 exit: 2239 /* Restore chip config */ 2240 data->chip_info->chip_config(data); 2241 return ret; 2242 } 2243 2244 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val, 2245 size_t bytes) 2246 { 2247 struct bmp280_data *data = priv; 2248 int ret; 2249 2250 pm_runtime_get_sync(data->dev); 2251 ret = bmp580_nvmem_read_impl(priv, offset, val, bytes); 2252 pm_runtime_mark_last_busy(data->dev); 2253 pm_runtime_put_autosuspend(data->dev); 2254 2255 return ret; 2256 } 2257 2258 static int bmp580_nvmem_write_impl(void *priv, unsigned int offset, void *val, 2259 size_t bytes) 2260 { 2261 struct bmp280_data *data = priv; 2262 u16 *buf = val; 2263 int ret, addr; 2264 2265 guard(mutex)(&data->lock); 2266 2267 /* Set sensor in standby mode */ 2268 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, 2269 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS, 2270 BMP580_ODR_DEEPSLEEP_DIS | 2271 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP)); 2272 if (ret) { 2273 dev_err(data->dev, "failed to change sensor to standby mode\n"); 2274 goto exit; 2275 } 2276 /* Wait standby transition time */ 2277 usleep_range(2500, 3000); 2278 2279 while (bytes >= sizeof(*buf)) { 2280 addr = bmp580_nvmem_addrs[offset / sizeof(*buf)]; 2281 2282 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, 2283 BMP580_NVM_PROG_EN | 2284 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr)); 2285 if (ret) { 2286 dev_err(data->dev, "error writing nvm address\n"); 2287 goto exit; 2288 } 2289 data->le16 = cpu_to_le16(*buf++); 2290 2291 ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB, 2292 &data->le16, sizeof(data->le16)); 2293 if (ret) { 2294 dev_err(data->dev, "error writing LSB NVM data regs\n"); 2295 goto exit; 2296 } 2297 2298 ret = bmp580_nvm_operation(data, true); 2299 if (ret) 2300 goto exit; 2301 2302 /* Disable programming mode bit */ 2303 ret = regmap_clear_bits(data->regmap, BMP580_REG_NVM_ADDR, 2304 BMP580_NVM_PROG_EN); 2305 if (ret) { 2306 dev_err(data->dev, "error resetting nvm write\n"); 2307 goto exit; 2308 } 2309 2310 bytes -= sizeof(*buf); 2311 offset += sizeof(*buf); 2312 } 2313 exit: 2314 /* Restore chip config */ 2315 data->chip_info->chip_config(data); 2316 return ret; 2317 } 2318 2319 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val, 2320 size_t bytes) 2321 { 2322 struct bmp280_data *data = priv; 2323 int ret; 2324 2325 pm_runtime_get_sync(data->dev); 2326 ret = bmp580_nvmem_write_impl(priv, offset, val, bytes); 2327 pm_runtime_mark_last_busy(data->dev); 2328 pm_runtime_put_autosuspend(data->dev); 2329 2330 return ret; 2331 } 2332 2333 static int bmp580_preinit(struct bmp280_data *data) 2334 { 2335 struct nvmem_config config = { 2336 .dev = data->dev, 2337 .priv = data, 2338 .name = "bmp580_nvmem", 2339 .word_size = sizeof(u16), 2340 .stride = sizeof(u16), 2341 .size = 3 * sizeof(u16), 2342 .reg_read = bmp580_nvmem_read, 2343 .reg_write = bmp580_nvmem_write, 2344 }; 2345 unsigned int reg; 2346 int ret; 2347 2348 /* Issue soft-reset command */ 2349 ret = bmp580_soft_reset(data); 2350 if (ret) 2351 return ret; 2352 2353 /* Post powerup sequence */ 2354 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®); 2355 if (ret) { 2356 dev_err(data->dev, "failed to establish comms with the chip\n"); 2357 return ret; 2358 } 2359 2360 /* Print warn message if we don't know the chip id */ 2361 if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT) 2362 dev_warn(data->dev, "unexpected chip_id\n"); 2363 2364 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®); 2365 if (ret) { 2366 dev_err(data->dev, "failed to read nvm status\n"); 2367 return ret; 2368 } 2369 2370 /* Check nvm status */ 2371 if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) { 2372 dev_err(data->dev, "nvm error on powerup sequence\n"); 2373 return -EIO; 2374 } 2375 2376 /* Register nvmem device */ 2377 return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config)); 2378 } 2379 2380 static const u8 bmp580_operation_mode[] = { 2381 [BMP280_SLEEP] = BMP580_MODE_SLEEP, 2382 [BMP280_FORCED] = BMP580_MODE_FORCED, 2383 [BMP280_NORMAL] = BMP580_MODE_NORMAL, 2384 }; 2385 2386 static int bmp580_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode) 2387 { 2388 struct device *dev = data->dev; 2389 int ret; 2390 2391 if (mode == BMP280_FORCED) { 2392 ret = regmap_set_bits(data->regmap, BMP580_REG_DSP_CONFIG, 2393 BMP580_DSP_IIR_FORCED_FLUSH); 2394 if (ret) { 2395 dev_err(dev, "Could not flush IIR filter constants.\n"); 2396 return ret; 2397 } 2398 } 2399 2400 ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG, 2401 BMP580_MODE_MASK, 2402 FIELD_PREP(BMP580_MODE_MASK, 2403 bmp580_operation_mode[mode])); 2404 if (ret) { 2405 dev_err(dev, "failed to write power control register.\n"); 2406 return ret; 2407 } 2408 2409 data->op_mode = mode; 2410 2411 return 0; 2412 } 2413 2414 static int bmp580_wait_conv(struct bmp280_data *data) 2415 { 2416 /* 2417 * Taken from datasheet, Section 2 "Specification, Table 3 "Electrical 2418 * characteristics. 2419 */ 2420 static const int time_conv_press[] = { 2421 0, 1050, 1785, 3045, 5670, 10920, 21420, 42420, 2422 84420, 2423 }; 2424 static const int time_conv_temp[] = { 2425 0, 1050, 1105, 1575, 2205, 3465, 6090, 11340, 2426 21840, 2427 }; 2428 int meas_time_us; 2429 2430 meas_time_us = 4 * USEC_PER_MSEC + 2431 time_conv_temp[data->oversampling_temp] + 2432 time_conv_press[data->oversampling_press]; 2433 2434 /* 2435 * Measurement time mentioned in Chapter 2, Table 4 of the datasheet. 2436 * The extra 4ms is the required mode change to start of measurement 2437 * time. 2438 */ 2439 fsleep(meas_time_us); 2440 2441 return 0; 2442 } 2443 2444 static int bmp580_chip_config(struct bmp280_data *data) 2445 { 2446 bool change = false, aux; 2447 unsigned int tmp; 2448 u8 reg_val; 2449 int ret; 2450 2451 /* Sets sensor in standby mode */ 2452 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, 2453 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS, 2454 BMP580_ODR_DEEPSLEEP_DIS | 2455 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP)); 2456 if (ret) { 2457 dev_err(data->dev, "failed to change sensor to standby mode\n"); 2458 return ret; 2459 } 2460 /* From datasheet's table 4: electrical characteristics */ 2461 usleep_range(2500, 3000); 2462 2463 /* Set default DSP mode settings */ 2464 reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) | 2465 BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN; 2466 2467 ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG, 2468 BMP580_DSP_COMP_MASK | 2469 BMP580_DSP_SHDW_IIR_TEMP_EN | 2470 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val); 2471 if (ret) { 2472 dev_err(data->dev, "failed to change DSP mode settings\n"); 2473 return ret; 2474 } 2475 2476 /* Configure oversampling */ 2477 reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) | 2478 FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) | 2479 BMP580_OSR_PRESS_EN; 2480 2481 ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG, 2482 BMP580_OSR_TEMP_MASK | 2483 BMP580_OSR_PRESS_MASK | 2484 BMP580_OSR_PRESS_EN, 2485 reg_val, &aux); 2486 if (ret) { 2487 dev_err(data->dev, "failed to write oversampling register\n"); 2488 return ret; 2489 } 2490 change = change || aux; 2491 2492 /* Configure output data rate */ 2493 ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK, 2494 FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq), 2495 &aux); 2496 if (ret) { 2497 dev_err(data->dev, "failed to write ODR configuration register\n"); 2498 return ret; 2499 } 2500 change = change || aux; 2501 2502 /* Set filter data */ 2503 reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) | 2504 FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff); 2505 2506 ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_IIR, 2507 BMP580_DSP_IIR_PRESS_MASK | BMP580_DSP_IIR_TEMP_MASK, 2508 reg_val); 2509 if (ret) { 2510 dev_err(data->dev, "failed to write config register\n"); 2511 return ret; 2512 } 2513 2514 if (change) { 2515 /* 2516 * Check if ODR and OSR settings are valid or we are 2517 * operating in a degraded mode. 2518 */ 2519 ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp); 2520 if (ret) { 2521 dev_err(data->dev, 2522 "error reading effective OSR register\n"); 2523 return ret; 2524 } 2525 if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) { 2526 dev_warn(data->dev, "OSR and ODR incompatible settings detected\n"); 2527 /* Set current OSR settings from data on effective OSR */ 2528 data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp); 2529 data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp); 2530 return -EINVAL; 2531 } 2532 } 2533 2534 return 0; 2535 } 2536 2537 static int bmp580_data_rdy_trigger_set_state(struct iio_trigger *trig, 2538 bool state) 2539 { 2540 struct bmp280_data *data = iio_trigger_get_drvdata(trig); 2541 int ret; 2542 2543 guard(mutex)(&data->lock); 2544 2545 ret = regmap_update_bits(data->regmap, BMP580_REG_INT_CONFIG, 2546 BMP580_INT_CONFIG_INT_EN, 2547 FIELD_PREP(BMP580_INT_CONFIG_INT_EN, !!state)); 2548 if (ret) 2549 dev_err(data->dev, 2550 "Could not %s interrupt.\n", str_enable_disable(state)); 2551 return ret; 2552 } 2553 2554 static const struct iio_trigger_ops bmp580_trigger_ops = { 2555 .set_trigger_state = &bmp580_data_rdy_trigger_set_state, 2556 }; 2557 2558 static int bmp580_int_pin_config(struct bmp280_data *data) 2559 { 2560 int pin_drive_cfg = FIELD_PREP(BMP580_INT_CONFIG_OPEN_DRAIN, 2561 data->trig_open_drain); 2562 int pin_level_cfg = FIELD_PREP(BMP580_INT_CONFIG_LEVEL, 2563 data->trig_active_high); 2564 int ret, int_pin_cfg = pin_drive_cfg | pin_level_cfg; 2565 2566 ret = regmap_update_bits(data->regmap, BMP580_REG_INT_CONFIG, 2567 BMP580_INT_CONFIG_MASK, int_pin_cfg); 2568 if (ret) { 2569 dev_err(data->dev, "Could not set interrupt settings.\n"); 2570 return ret; 2571 } 2572 2573 ret = regmap_set_bits(data->regmap, BMP580_REG_INT_SOURCE, 2574 BMP580_INT_SOURCE_DRDY); 2575 if (ret) 2576 dev_err(data->dev, "Could not set interrupt source.\n"); 2577 2578 return ret; 2579 } 2580 2581 static irqreturn_t bmp580_irq_thread_handler(int irq, void *p) 2582 { 2583 struct iio_dev *indio_dev = p; 2584 struct bmp280_data *data = iio_priv(indio_dev); 2585 unsigned int int_ctrl; 2586 int ret; 2587 2588 ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, &int_ctrl); 2589 if (ret) 2590 return IRQ_NONE; 2591 2592 if (FIELD_GET(BMP580_INT_STATUS_DRDY_MASK, int_ctrl)) 2593 iio_trigger_poll_nested(data->trig); 2594 2595 return IRQ_HANDLED; 2596 } 2597 2598 static int bmp580_trigger_probe(struct iio_dev *indio_dev) 2599 { 2600 return __bmp280_trigger_probe(indio_dev, &bmp580_trigger_ops, 2601 bmp580_int_pin_config, 2602 bmp580_irq_thread_handler); 2603 } 2604 2605 static irqreturn_t bmp580_trigger_handler(int irq, void *p) 2606 { 2607 struct iio_poll_func *pf = p; 2608 struct iio_dev *indio_dev = pf->indio_dev; 2609 struct bmp280_data *data = iio_priv(indio_dev); 2610 int ret, offset; 2611 2612 guard(mutex)(&data->lock); 2613 2614 /* Burst read data registers */ 2615 ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, 2616 data->buf, BMP280_BURST_READ_BYTES); 2617 if (ret) { 2618 dev_err(data->dev, "failed to burst read sensor data\n"); 2619 goto out; 2620 } 2621 2622 offset = 0; 2623 2624 /* Pressure calculations */ 2625 memcpy(&data->sensor_data[offset], &data->buf[3], 3); 2626 2627 offset += sizeof(s32); 2628 2629 /* Temperature calculations */ 2630 memcpy(&data->sensor_data[offset], &data->buf[0], 3); 2631 2632 iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data, 2633 iio_get_time_ns(indio_dev)); 2634 2635 out: 2636 iio_trigger_notify_done(indio_dev->trig); 2637 2638 return IRQ_HANDLED; 2639 } 2640 2641 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 2642 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT }; 2643 /* Instead of { 1000, 16 } we do this, to avoid overflow issues */ 2644 static const int bmp580_temp_coeffs[] = { 125, 13 }; 2645 static const int bmp580_press_coeffs[] = { 1, 64000}; 2646 2647 const struct bmp280_chip_info bmp580_chip_info = { 2648 .id_reg = BMP580_REG_CHIP_ID, 2649 .chip_id = bmp580_chip_ids, 2650 .num_chip_id = ARRAY_SIZE(bmp580_chip_ids), 2651 .regmap_config = &bmp580_regmap_config, 2652 .start_up_time = 2000, 2653 .channels = bmp580_channels, 2654 .num_channels = ARRAY_SIZE(bmp580_channels), 2655 .avail_scan_masks = bmp280_avail_scan_masks, 2656 2657 .oversampling_temp_avail = bmp580_oversampling_avail, 2658 .num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail), 2659 .oversampling_temp_default = ilog2(1), 2660 2661 .oversampling_press_avail = bmp580_oversampling_avail, 2662 .num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail), 2663 .oversampling_press_default = ilog2(4), 2664 2665 .sampling_freq_avail = bmp580_odr_table, 2666 .num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2, 2667 .sampling_freq_default = BMP580_ODR_50HZ, 2668 2669 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail, 2670 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 2671 .iir_filter_coeff_default = 2, 2672 2673 .temp_coeffs = bmp580_temp_coeffs, 2674 .temp_coeffs_type = IIO_VAL_FRACTIONAL_LOG2, 2675 .press_coeffs = bmp580_press_coeffs, 2676 .press_coeffs_type = IIO_VAL_FRACTIONAL, 2677 2678 .chip_config = bmp580_chip_config, 2679 .read_temp = bmp580_read_temp, 2680 .read_press = bmp580_read_press, 2681 .set_mode = bmp580_set_mode, 2682 .wait_conv = bmp580_wait_conv, 2683 .preinit = bmp580_preinit, 2684 2685 .trigger_probe = bmp580_trigger_probe, 2686 .trigger_handler = bmp580_trigger_handler, 2687 }; 2688 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280); 2689 2690 static int bmp180_wait_for_eoc(struct bmp280_data *data, u8 ctrl_meas) 2691 { 2692 static const int conversion_time_max[] = { 4500, 7500, 13500, 25500 }; 2693 unsigned int delay_us; 2694 unsigned int ctrl; 2695 int ret; 2696 2697 if (data->use_eoc) 2698 reinit_completion(&data->done); 2699 2700 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas); 2701 if (ret) { 2702 dev_err(data->dev, "failed to write crtl_meas register\n"); 2703 return ret; 2704 } 2705 2706 if (data->use_eoc) { 2707 /* 2708 * If we have a completion interrupt, use it, wait up to 2709 * 100ms. The longest conversion time listed is 76.5 ms for 2710 * advanced resolution mode. 2711 */ 2712 ret = wait_for_completion_timeout(&data->done, 2713 1 + msecs_to_jiffies(100)); 2714 if (!ret) 2715 dev_err(data->dev, "timeout waiting for completion\n"); 2716 } else { 2717 if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP) 2718 delay_us = 4500; 2719 else 2720 delay_us = 2721 conversion_time_max[data->oversampling_press]; 2722 2723 usleep_range(delay_us, delay_us + 1000); 2724 } 2725 2726 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl); 2727 if (ret) { 2728 dev_err(data->dev, "failed to read ctrl_meas register\n"); 2729 return ret; 2730 } 2731 2732 /* The value of this bit reset to "0" after conversion is complete */ 2733 if (ctrl & BMP180_MEAS_SCO) { 2734 dev_err(data->dev, "conversion didn't complete\n"); 2735 return -EIO; 2736 } 2737 2738 return 0; 2739 } 2740 2741 static int bmp180_read_temp_adc(struct bmp280_data *data, u32 *adc_temp) 2742 { 2743 int ret; 2744 2745 ret = bmp180_wait_for_eoc(data, 2746 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) | 2747 BMP180_MEAS_SCO); 2748 if (ret) 2749 return ret; 2750 2751 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, 2752 &data->be16, sizeof(data->be16)); 2753 if (ret) { 2754 dev_err(data->dev, "failed to read temperature\n"); 2755 return ret; 2756 } 2757 2758 *adc_temp = be16_to_cpu(data->be16); 2759 2760 return 0; 2761 } 2762 2763 static int bmp180_read_calib(struct bmp280_data *data) 2764 { 2765 struct bmp180_calib *calib = &data->calib.bmp180; 2766 int ret; 2767 int i; 2768 2769 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, 2770 data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf)); 2771 if (ret) { 2772 dev_err(data->dev, "failed to read calibration parameters\n"); 2773 return ret; 2774 } 2775 2776 /* None of the words has the value 0 or 0xFFFF */ 2777 for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) { 2778 if (data->bmp180_cal_buf[i] == cpu_to_be16(0) || 2779 data->bmp180_cal_buf[i] == cpu_to_be16(0xffff)) 2780 return -EIO; 2781 } 2782 2783 /* Toss the calibration data into the entropy pool */ 2784 add_device_randomness(data->bmp180_cal_buf, 2785 sizeof(data->bmp180_cal_buf)); 2786 2787 calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]); 2788 calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]); 2789 calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]); 2790 calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]); 2791 calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]); 2792 calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]); 2793 calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]); 2794 calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]); 2795 calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]); 2796 calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]); 2797 calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]); 2798 2799 return 0; 2800 } 2801 2802 /* 2803 * Returns temperature in DegC, resolution is 0.1 DegC. 2804 * t_fine carries fine temperature as global value. 2805 * 2806 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". 2807 */ 2808 2809 static s32 bmp180_calc_t_fine(struct bmp280_data *data, u32 adc_temp) 2810 { 2811 struct bmp180_calib *calib = &data->calib.bmp180; 2812 s32 x1, x2; 2813 2814 x1 = ((((s32)adc_temp) - calib->AC6) * calib->AC5) >> 15; 2815 x2 = (calib->MC << 11) / (x1 + calib->MD); 2816 return x1 + x2; /* t_fine = x1 + x2; */ 2817 } 2818 2819 static int bmp180_get_t_fine(struct bmp280_data *data, s32 *t_fine) 2820 { 2821 s32 adc_temp; 2822 int ret; 2823 2824 ret = bmp180_read_temp_adc(data, &adc_temp); 2825 if (ret) 2826 return ret; 2827 2828 *t_fine = bmp180_calc_t_fine(data, adc_temp); 2829 2830 return 0; 2831 } 2832 2833 static s32 bmp180_compensate_temp(struct bmp280_data *data, u32 adc_temp) 2834 { 2835 return (bmp180_calc_t_fine(data, adc_temp) + 8) / 16; 2836 } 2837 2838 static int bmp180_read_temp(struct bmp280_data *data, s32 *comp_temp) 2839 { 2840 u32 adc_temp; 2841 int ret; 2842 2843 ret = bmp180_read_temp_adc(data, &adc_temp); 2844 if (ret) 2845 return ret; 2846 2847 *comp_temp = bmp180_compensate_temp(data, adc_temp); 2848 2849 return 0; 2850 } 2851 2852 static int bmp180_read_press_adc(struct bmp280_data *data, u32 *adc_press) 2853 { 2854 u8 oss = data->oversampling_press; 2855 int ret; 2856 2857 ret = bmp180_wait_for_eoc(data, 2858 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) | 2859 FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) | 2860 BMP180_MEAS_SCO); 2861 if (ret) 2862 return ret; 2863 2864 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, 2865 data->buf, BMP280_NUM_PRESS_BYTES); 2866 if (ret) { 2867 dev_err(data->dev, "failed to read pressure\n"); 2868 return ret; 2869 } 2870 2871 *adc_press = get_unaligned_be24(data->buf) >> (8 - oss); 2872 2873 return 0; 2874 } 2875 2876 /* 2877 * Returns pressure in Pa, resolution is 1 Pa. 2878 * 2879 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". 2880 */ 2881 static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press, 2882 s32 t_fine) 2883 { 2884 struct bmp180_calib *calib = &data->calib.bmp180; 2885 s32 oss = data->oversampling_press; 2886 s32 x1, x2, x3, p; 2887 s32 b3, b6; 2888 u32 b4, b7; 2889 2890 b6 = t_fine - 4000; 2891 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11; 2892 x2 = calib->AC2 * b6 >> 11; 2893 x3 = x1 + x2; 2894 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4; 2895 x1 = calib->AC3 * b6 >> 13; 2896 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16; 2897 x3 = (x1 + x2 + 2) >> 2; 2898 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15; 2899 b7 = (adc_press - b3) * (50000 >> oss); 2900 if (b7 < 0x80000000) 2901 p = (b7 * 2) / b4; 2902 else 2903 p = (b7 / b4) * 2; 2904 2905 x1 = (p >> 8) * (p >> 8); 2906 x1 = (x1 * 3038) >> 16; 2907 x2 = (-7357 * p) >> 16; 2908 2909 return p + ((x1 + x2 + 3791) >> 4); 2910 } 2911 2912 static int bmp180_read_press(struct bmp280_data *data, u32 *comp_press) 2913 { 2914 u32 adc_press; 2915 s32 t_fine; 2916 int ret; 2917 2918 ret = bmp180_get_t_fine(data, &t_fine); 2919 if (ret) 2920 return ret; 2921 2922 ret = bmp180_read_press_adc(data, &adc_press); 2923 if (ret) 2924 return ret; 2925 2926 *comp_press = bmp180_compensate_press(data, adc_press, t_fine); 2927 2928 return 0; 2929 } 2930 2931 /* Keep compatibility with newer generations of the sensor */ 2932 static int bmp180_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode) 2933 { 2934 return 0; 2935 } 2936 2937 /* Keep compatibility with newer generations of the sensor */ 2938 static int bmp180_wait_conv(struct bmp280_data *data) 2939 { 2940 return 0; 2941 } 2942 2943 /* Keep compatibility with newer generations of the sensor */ 2944 static int bmp180_chip_config(struct bmp280_data *data) 2945 { 2946 return 0; 2947 } 2948 2949 static irqreturn_t bmp180_trigger_handler(int irq, void *p) 2950 { 2951 struct iio_poll_func *pf = p; 2952 struct iio_dev *indio_dev = pf->indio_dev; 2953 struct bmp280_data *data = iio_priv(indio_dev); 2954 int ret, comp_temp, comp_press; 2955 s32 *chans = (s32 *)data->sensor_data; 2956 2957 guard(mutex)(&data->lock); 2958 2959 ret = bmp180_read_temp(data, &comp_temp); 2960 if (ret) 2961 goto out; 2962 2963 2964 ret = bmp180_read_press(data, &comp_press); 2965 if (ret) 2966 goto out; 2967 2968 chans[0] = comp_press; 2969 chans[1] = comp_temp; 2970 2971 iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data, 2972 iio_get_time_ns(indio_dev)); 2973 2974 out: 2975 iio_trigger_notify_done(indio_dev->trig); 2976 2977 return IRQ_HANDLED; 2978 } 2979 2980 static const int bmp180_oversampling_temp_avail[] = { 1 }; 2981 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; 2982 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID }; 2983 static const int bmp180_temp_coeffs[] = { 100, 1 }; 2984 static const int bmp180_press_coeffs[] = { 1, 1000 }; 2985 2986 const struct bmp280_chip_info bmp180_chip_info = { 2987 .id_reg = BMP280_REG_ID, 2988 .chip_id = bmp180_chip_ids, 2989 .num_chip_id = ARRAY_SIZE(bmp180_chip_ids), 2990 .regmap_config = &bmp180_regmap_config, 2991 .start_up_time = 2000, 2992 .channels = bmp280_channels, 2993 .num_channels = ARRAY_SIZE(bmp280_channels), 2994 .avail_scan_masks = bmp280_avail_scan_masks, 2995 2996 .oversampling_temp_avail = bmp180_oversampling_temp_avail, 2997 .num_oversampling_temp_avail = 2998 ARRAY_SIZE(bmp180_oversampling_temp_avail), 2999 .oversampling_temp_default = 0, 3000 3001 .oversampling_press_avail = bmp180_oversampling_press_avail, 3002 .num_oversampling_press_avail = 3003 ARRAY_SIZE(bmp180_oversampling_press_avail), 3004 .oversampling_press_default = BMP180_MEAS_PRESS_8X, 3005 3006 .temp_coeffs = bmp180_temp_coeffs, 3007 .temp_coeffs_type = IIO_VAL_FRACTIONAL, 3008 .press_coeffs = bmp180_press_coeffs, 3009 .press_coeffs_type = IIO_VAL_FRACTIONAL, 3010 3011 .chip_config = bmp180_chip_config, 3012 .read_temp = bmp180_read_temp, 3013 .read_press = bmp180_read_press, 3014 .read_calib = bmp180_read_calib, 3015 .set_mode = bmp180_set_mode, 3016 .wait_conv = bmp180_wait_conv, 3017 3018 .trigger_handler = bmp180_trigger_handler, 3019 }; 3020 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280); 3021 3022 static irqreturn_t bmp085_eoc_irq(int irq, void *d) 3023 { 3024 struct bmp280_data *data = d; 3025 3026 complete(&data->done); 3027 3028 return IRQ_HANDLED; 3029 } 3030 3031 static int bmp085_trigger_probe(struct iio_dev *indio_dev) 3032 { 3033 struct bmp280_data *data = iio_priv(indio_dev); 3034 struct device *dev = data->dev; 3035 unsigned long irq_trig; 3036 int ret, irq; 3037 3038 irq = fwnode_irq_get(dev_fwnode(dev), 0); 3039 if (irq < 0) 3040 return dev_err_probe(dev, irq, "No interrupt found.\n"); 3041 3042 irq_trig = irq_get_trigger_type(irq); 3043 if (irq_trig != IRQF_TRIGGER_RISING) { 3044 dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n"); 3045 irq_trig = IRQF_TRIGGER_RISING; 3046 } 3047 3048 init_completion(&data->done); 3049 3050 ret = devm_request_irq(dev, irq, bmp085_eoc_irq, irq_trig, 3051 indio_dev->name, data); 3052 if (ret) { 3053 /* Bail out without IRQ but keep the driver in place */ 3054 dev_err(dev, "unable to request DRDY IRQ\n"); 3055 return 0; 3056 } 3057 3058 data->use_eoc = true; 3059 3060 return 0; 3061 } 3062 3063 /* Identical to bmp180_chip_info + bmp085_trigger_probe */ 3064 const struct bmp280_chip_info bmp085_chip_info = { 3065 .id_reg = BMP280_REG_ID, 3066 .chip_id = bmp180_chip_ids, 3067 .num_chip_id = ARRAY_SIZE(bmp180_chip_ids), 3068 .regmap_config = &bmp180_regmap_config, 3069 .start_up_time = 2000, 3070 .channels = bmp280_channels, 3071 .num_channels = ARRAY_SIZE(bmp280_channels), 3072 .avail_scan_masks = bmp280_avail_scan_masks, 3073 3074 .oversampling_temp_avail = bmp180_oversampling_temp_avail, 3075 .num_oversampling_temp_avail = 3076 ARRAY_SIZE(bmp180_oversampling_temp_avail), 3077 .oversampling_temp_default = 0, 3078 3079 .oversampling_press_avail = bmp180_oversampling_press_avail, 3080 .num_oversampling_press_avail = 3081 ARRAY_SIZE(bmp180_oversampling_press_avail), 3082 .oversampling_press_default = BMP180_MEAS_PRESS_8X, 3083 3084 .temp_coeffs = bmp180_temp_coeffs, 3085 .temp_coeffs_type = IIO_VAL_FRACTIONAL, 3086 .press_coeffs = bmp180_press_coeffs, 3087 .press_coeffs_type = IIO_VAL_FRACTIONAL, 3088 3089 .chip_config = bmp180_chip_config, 3090 .read_temp = bmp180_read_temp, 3091 .read_press = bmp180_read_press, 3092 .read_calib = bmp180_read_calib, 3093 .set_mode = bmp180_set_mode, 3094 .wait_conv = bmp180_wait_conv, 3095 3096 .trigger_probe = bmp085_trigger_probe, 3097 .trigger_handler = bmp180_trigger_handler, 3098 }; 3099 EXPORT_SYMBOL_NS(bmp085_chip_info, IIO_BMP280); 3100 3101 static int bmp280_buffer_preenable(struct iio_dev *indio_dev) 3102 { 3103 struct bmp280_data *data = iio_priv(indio_dev); 3104 3105 pm_runtime_get_sync(data->dev); 3106 data->chip_info->set_mode(data, BMP280_NORMAL); 3107 3108 return 0; 3109 } 3110 3111 static int bmp280_buffer_postdisable(struct iio_dev *indio_dev) 3112 { 3113 struct bmp280_data *data = iio_priv(indio_dev); 3114 3115 pm_runtime_mark_last_busy(data->dev); 3116 pm_runtime_put_autosuspend(data->dev); 3117 3118 return 0; 3119 } 3120 3121 static const struct iio_buffer_setup_ops bmp280_buffer_setup_ops = { 3122 .preenable = bmp280_buffer_preenable, 3123 .postdisable = bmp280_buffer_postdisable, 3124 }; 3125 3126 static void bmp280_pm_disable(void *data) 3127 { 3128 struct device *dev = data; 3129 3130 pm_runtime_get_sync(dev); 3131 pm_runtime_put_noidle(dev); 3132 pm_runtime_disable(dev); 3133 } 3134 3135 static void bmp280_regulators_disable(void *data) 3136 { 3137 struct regulator_bulk_data *supplies = data; 3138 3139 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies); 3140 } 3141 3142 int bmp280_common_probe(struct device *dev, 3143 struct regmap *regmap, 3144 const struct bmp280_chip_info *chip_info, 3145 const char *name, 3146 int irq) 3147 { 3148 struct iio_dev *indio_dev; 3149 struct bmp280_data *data; 3150 struct gpio_desc *gpiod; 3151 unsigned int chip_id; 3152 unsigned int i; 3153 int ret; 3154 3155 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 3156 if (!indio_dev) 3157 return -ENOMEM; 3158 3159 data = iio_priv(indio_dev); 3160 mutex_init(&data->lock); 3161 data->dev = dev; 3162 3163 indio_dev->name = name; 3164 indio_dev->info = &bmp280_info; 3165 indio_dev->modes = INDIO_DIRECT_MODE; 3166 3167 data->chip_info = chip_info; 3168 3169 /* Apply initial values from chip info structure */ 3170 indio_dev->channels = chip_info->channels; 3171 indio_dev->num_channels = chip_info->num_channels; 3172 indio_dev->available_scan_masks = chip_info->avail_scan_masks; 3173 data->oversampling_press = chip_info->oversampling_press_default; 3174 data->oversampling_humid = chip_info->oversampling_humid_default; 3175 data->oversampling_temp = chip_info->oversampling_temp_default; 3176 data->iir_filter_coeff = chip_info->iir_filter_coeff_default; 3177 data->sampling_freq = chip_info->sampling_freq_default; 3178 data->start_up_time = chip_info->start_up_time; 3179 3180 /* Bring up regulators */ 3181 regulator_bulk_set_supply_names(data->supplies, 3182 bmp280_supply_names, 3183 BMP280_NUM_SUPPLIES); 3184 3185 ret = devm_regulator_bulk_get(dev, 3186 BMP280_NUM_SUPPLIES, data->supplies); 3187 if (ret) { 3188 dev_err(dev, "failed to get regulators\n"); 3189 return ret; 3190 } 3191 3192 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies); 3193 if (ret) { 3194 dev_err(dev, "failed to enable regulators\n"); 3195 return ret; 3196 } 3197 3198 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable, 3199 data->supplies); 3200 if (ret) 3201 return ret; 3202 3203 /* Wait to make sure we started up properly */ 3204 usleep_range(data->start_up_time, data->start_up_time + 100); 3205 3206 /* Bring chip out of reset if there is an assigned GPIO line */ 3207 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 3208 /* Deassert the signal */ 3209 if (gpiod) { 3210 dev_info(dev, "release reset\n"); 3211 gpiod_set_value(gpiod, 0); 3212 } 3213 3214 data->regmap = regmap; 3215 3216 ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id); 3217 if (ret) { 3218 dev_err(data->dev, "failed to read chip id\n"); 3219 return ret; 3220 } 3221 3222 for (i = 0; i < data->chip_info->num_chip_id; i++) { 3223 if (chip_id == data->chip_info->chip_id[i]) { 3224 dev_info(dev, "0x%x is a known chip id for %s\n", chip_id, name); 3225 break; 3226 } 3227 } 3228 3229 if (i == data->chip_info->num_chip_id) 3230 dev_warn(dev, "bad chip id: 0x%x is not a known chip id\n", chip_id); 3231 3232 if (data->chip_info->preinit) { 3233 ret = data->chip_info->preinit(data); 3234 if (ret) 3235 return dev_err_probe(data->dev, ret, 3236 "error running preinit tasks\n"); 3237 } 3238 3239 ret = data->chip_info->chip_config(data); 3240 if (ret) 3241 return ret; 3242 3243 dev_set_drvdata(dev, indio_dev); 3244 3245 /* 3246 * Some chips have calibration parameters "programmed into the devices' 3247 * non-volatile memory during production". Let's read them out at probe 3248 * time once. They will not change. 3249 */ 3250 3251 if (data->chip_info->read_calib) { 3252 ret = data->chip_info->read_calib(data); 3253 if (ret) 3254 return dev_err_probe(data->dev, ret, 3255 "failed to read calibration coefficients\n"); 3256 } 3257 3258 ret = devm_iio_triggered_buffer_setup(data->dev, indio_dev, 3259 iio_pollfunc_store_time, 3260 data->chip_info->trigger_handler, 3261 &bmp280_buffer_setup_ops); 3262 if (ret) 3263 return dev_err_probe(data->dev, ret, 3264 "iio triggered buffer setup failed\n"); 3265 3266 /* 3267 * Attempt to grab an optional EOC IRQ - only the BMP085 has this 3268 * however as it happens, the BMP085 shares the chip ID of BMP180 3269 * so we look for an IRQ if we have that. 3270 */ 3271 if (irq > 0) { 3272 if (data->chip_info->trigger_probe) 3273 ret = data->chip_info->trigger_probe(indio_dev); 3274 if (ret) 3275 return ret; 3276 } 3277 3278 ret = data->chip_info->set_mode(data, BMP280_SLEEP); 3279 if (ret) 3280 return dev_err_probe(dev, ret, "Failed to set sleep mode\n"); 3281 3282 /* Enable runtime PM */ 3283 pm_runtime_get_noresume(dev); 3284 pm_runtime_set_active(dev); 3285 pm_runtime_enable(dev); 3286 /* 3287 * Set autosuspend to two orders of magnitude larger than the 3288 * start-up time. 3289 */ 3290 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10); 3291 pm_runtime_use_autosuspend(dev); 3292 pm_runtime_put(dev); 3293 3294 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev); 3295 if (ret) 3296 return ret; 3297 3298 return devm_iio_device_register(dev, indio_dev); 3299 } 3300 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280); 3301 3302 static int bmp280_runtime_suspend(struct device *dev) 3303 { 3304 struct iio_dev *indio_dev = dev_get_drvdata(dev); 3305 struct bmp280_data *data = iio_priv(indio_dev); 3306 3307 data->chip_info->set_mode(data, BMP280_SLEEP); 3308 3309 fsleep(data->start_up_time); 3310 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies); 3311 } 3312 3313 static int bmp280_runtime_resume(struct device *dev) 3314 { 3315 struct iio_dev *indio_dev = dev_get_drvdata(dev); 3316 struct bmp280_data *data = iio_priv(indio_dev); 3317 int ret; 3318 3319 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies); 3320 if (ret) 3321 return ret; 3322 3323 usleep_range(data->start_up_time, data->start_up_time + 100); 3324 3325 ret = data->chip_info->chip_config(data); 3326 if (ret) 3327 return ret; 3328 3329 return data->chip_info->set_mode(data, data->op_mode); 3330 } 3331 3332 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend, 3333 bmp280_runtime_resume, NULL); 3334 3335 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 3336 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor"); 3337 MODULE_LICENSE("GPL v2"); 3338