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-bmp581-ds004.pdf 17 * 18 * Notice: 19 * The link to the bmp180 datasheet points to an outdated version missing these changes: 20 * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26 21 * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4 22 * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26 23 */ 24 25 #define pr_fmt(fmt) "bmp280: " fmt 26 27 #include <linux/bitops.h> 28 #include <linux/bitfield.h> 29 #include <linux/device.h> 30 #include <linux/module.h> 31 #include <linux/nvmem-provider.h> 32 #include <linux/regmap.h> 33 #include <linux/delay.h> 34 #include <linux/iio/iio.h> 35 #include <linux/iio/sysfs.h> 36 #include <linux/gpio/consumer.h> 37 #include <linux/regulator/consumer.h> 38 #include <linux/interrupt.h> 39 #include <linux/irq.h> /* For irq_get_irq_data() */ 40 #include <linux/completion.h> 41 #include <linux/pm_runtime.h> 42 #include <linux/random.h> 43 44 #include <asm/unaligned.h> 45 46 #include "bmp280.h" 47 48 /* 49 * These enums are used for indexing into the array of calibration 50 * coefficients for BMP180. 51 */ 52 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD }; 53 54 55 enum bmp380_odr { 56 BMP380_ODR_200HZ, 57 BMP380_ODR_100HZ, 58 BMP380_ODR_50HZ, 59 BMP380_ODR_25HZ, 60 BMP380_ODR_12_5HZ, 61 BMP380_ODR_6_25HZ, 62 BMP380_ODR_3_125HZ, 63 BMP380_ODR_1_5625HZ, 64 BMP380_ODR_0_78HZ, 65 BMP380_ODR_0_39HZ, 66 BMP380_ODR_0_2HZ, 67 BMP380_ODR_0_1HZ, 68 BMP380_ODR_0_05HZ, 69 BMP380_ODR_0_02HZ, 70 BMP380_ODR_0_01HZ, 71 BMP380_ODR_0_006HZ, 72 BMP380_ODR_0_003HZ, 73 BMP380_ODR_0_0015HZ, 74 }; 75 76 enum bmp580_odr { 77 BMP580_ODR_240HZ, 78 BMP580_ODR_218HZ, 79 BMP580_ODR_199HZ, 80 BMP580_ODR_179HZ, 81 BMP580_ODR_160HZ, 82 BMP580_ODR_149HZ, 83 BMP580_ODR_140HZ, 84 BMP580_ODR_129HZ, 85 BMP580_ODR_120HZ, 86 BMP580_ODR_110HZ, 87 BMP580_ODR_100HZ, 88 BMP580_ODR_89HZ, 89 BMP580_ODR_80HZ, 90 BMP580_ODR_70HZ, 91 BMP580_ODR_60HZ, 92 BMP580_ODR_50HZ, 93 BMP580_ODR_45HZ, 94 BMP580_ODR_40HZ, 95 BMP580_ODR_35HZ, 96 BMP580_ODR_30HZ, 97 BMP580_ODR_25HZ, 98 BMP580_ODR_20HZ, 99 BMP580_ODR_15HZ, 100 BMP580_ODR_10HZ, 101 BMP580_ODR_5HZ, 102 BMP580_ODR_4HZ, 103 BMP580_ODR_3HZ, 104 BMP580_ODR_2HZ, 105 BMP580_ODR_1HZ, 106 BMP580_ODR_0_5HZ, 107 BMP580_ODR_0_25HZ, 108 BMP580_ODR_0_125HZ, 109 }; 110 111 /* 112 * These enums are used for indexing into the array of compensation 113 * parameters for BMP280. 114 */ 115 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 }; 116 117 enum { 118 /* Temperature calib indexes */ 119 BMP380_T1 = 0, 120 BMP380_T2 = 2, 121 BMP380_T3 = 4, 122 /* Pressure calib indexes */ 123 BMP380_P1 = 5, 124 BMP380_P2 = 7, 125 BMP380_P3 = 9, 126 BMP380_P4 = 10, 127 BMP380_P5 = 11, 128 BMP380_P6 = 13, 129 BMP380_P7 = 15, 130 BMP380_P8 = 16, 131 BMP380_P9 = 17, 132 BMP380_P10 = 19, 133 BMP380_P11 = 20, 134 }; 135 136 static const struct iio_chan_spec bmp280_channels[] = { 137 { 138 .type = IIO_PRESSURE, 139 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 140 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 141 }, 142 { 143 .type = IIO_TEMP, 144 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 145 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 146 }, 147 { 148 .type = IIO_HUMIDITYRELATIVE, 149 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 150 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 151 }, 152 }; 153 154 static const struct iio_chan_spec bmp380_channels[] = { 155 { 156 .type = IIO_PRESSURE, 157 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 158 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 159 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 160 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 161 }, 162 { 163 .type = IIO_TEMP, 164 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 165 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 166 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 167 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 168 }, 169 { 170 .type = IIO_HUMIDITYRELATIVE, 171 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 172 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 173 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 174 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 175 }, 176 }; 177 178 static int bmp280_read_calib(struct bmp280_data *data) 179 { 180 struct bmp280_calib *calib = &data->calib.bmp280; 181 int ret; 182 183 184 /* Read temperature and pressure calibration values. */ 185 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START, 186 data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf)); 187 if (ret < 0) { 188 dev_err(data->dev, 189 "failed to read temperature and pressure calibration parameters\n"); 190 return ret; 191 } 192 193 /* Toss the temperature and pressure calibration data into the entropy pool */ 194 add_device_randomness(data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf)); 195 196 /* Parse temperature calibration values. */ 197 calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]); 198 calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]); 199 calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]); 200 201 /* Parse pressure calibration values. */ 202 calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]); 203 calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]); 204 calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]); 205 calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]); 206 calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]); 207 calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]); 208 calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]); 209 calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]); 210 calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]); 211 212 return 0; 213 } 214 215 static int bme280_read_calib(struct bmp280_data *data) 216 { 217 struct bmp280_calib *calib = &data->calib.bmp280; 218 struct device *dev = data->dev; 219 unsigned int tmp; 220 int ret; 221 222 /* Load shared calibration params with bmp280 first */ 223 ret = bmp280_read_calib(data); 224 if (ret < 0) { 225 dev_err(dev, "failed to read common bmp280 calibration parameters\n"); 226 return ret; 227 } 228 229 /* 230 * Read humidity calibration values. 231 * Due to some odd register addressing we cannot just 232 * do a big bulk read. Instead, we have to read each Hx 233 * value separately and sometimes do some bit shifting... 234 * Humidity data is only available on BME280. 235 */ 236 237 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp); 238 if (ret < 0) { 239 dev_err(dev, "failed to read H1 comp value\n"); 240 return ret; 241 } 242 calib->H1 = tmp; 243 244 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, 245 &data->le16, sizeof(data->le16)); 246 if (ret < 0) { 247 dev_err(dev, "failed to read H2 comp value\n"); 248 return ret; 249 } 250 calib->H2 = sign_extend32(le16_to_cpu(data->le16), 15); 251 252 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp); 253 if (ret < 0) { 254 dev_err(dev, "failed to read H3 comp value\n"); 255 return ret; 256 } 257 calib->H3 = tmp; 258 259 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, 260 &data->be16, sizeof(data->be16)); 261 if (ret < 0) { 262 dev_err(dev, "failed to read H4 comp value\n"); 263 return ret; 264 } 265 calib->H4 = sign_extend32(((be16_to_cpu(data->be16) >> 4) & 0xff0) | 266 (be16_to_cpu(data->be16) & 0xf), 11); 267 268 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, 269 &data->le16, sizeof(data->le16)); 270 if (ret < 0) { 271 dev_err(dev, "failed to read H5 comp value\n"); 272 return ret; 273 } 274 calib->H5 = sign_extend32(FIELD_GET(BMP280_COMP_H5_MASK, le16_to_cpu(data->le16)), 11); 275 276 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp); 277 if (ret < 0) { 278 dev_err(dev, "failed to read H6 comp value\n"); 279 return ret; 280 } 281 calib->H6 = sign_extend32(tmp, 7); 282 283 return 0; 284 } 285 /* 286 * Returns humidity in percent, resolution is 0.01 percent. Output value of 287 * "47445" represents 47445/1024 = 46.333 %RH. 288 * 289 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula". 290 */ 291 static u32 bmp280_compensate_humidity(struct bmp280_data *data, 292 s32 adc_humidity) 293 { 294 struct bmp280_calib *calib = &data->calib.bmp280; 295 s32 var; 296 297 var = ((s32)data->t_fine) - (s32)76800; 298 var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var)) 299 + (s32)16384) >> 15) * (((((((var * calib->H6) >> 10) 300 * (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10) 301 + (s32)2097152) * calib->H2 + 8192) >> 14); 302 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4; 303 304 var = clamp_val(var, 0, 419430400); 305 306 return var >> 12; 307 }; 308 309 /* 310 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of 311 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global 312 * value. 313 * 314 * Taken from datasheet, Section 3.11.3, "Compensation formula". 315 */ 316 static s32 bmp280_compensate_temp(struct bmp280_data *data, 317 s32 adc_temp) 318 { 319 struct bmp280_calib *calib = &data->calib.bmp280; 320 s32 var1, var2; 321 322 var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) * 323 ((s32)calib->T2)) >> 11; 324 var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) * 325 ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) * 326 ((s32)calib->T3)) >> 14; 327 data->t_fine = var1 + var2; 328 329 return (data->t_fine * 5 + 128) >> 8; 330 } 331 332 /* 333 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 334 * integer bits and 8 fractional bits). Output value of "24674867" 335 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa 336 * 337 * Taken from datasheet, Section 3.11.3, "Compensation formula". 338 */ 339 static u32 bmp280_compensate_press(struct bmp280_data *data, 340 s32 adc_press) 341 { 342 struct bmp280_calib *calib = &data->calib.bmp280; 343 s64 var1, var2, p; 344 345 var1 = ((s64)data->t_fine) - 128000; 346 var2 = var1 * var1 * (s64)calib->P6; 347 var2 += (var1 * (s64)calib->P5) << 17; 348 var2 += ((s64)calib->P4) << 35; 349 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) + 350 ((var1 * (s64)calib->P2) << 12); 351 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33; 352 353 if (var1 == 0) 354 return 0; 355 356 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125; 357 p = div64_s64(p, var1); 358 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25; 359 var2 = ((s64)(calib->P8) * p) >> 19; 360 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4); 361 362 return (u32)p; 363 } 364 365 static int bmp280_read_temp(struct bmp280_data *data, 366 int *val, int *val2) 367 { 368 s32 adc_temp, comp_temp; 369 int ret; 370 371 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, 372 data->buf, sizeof(data->buf)); 373 if (ret < 0) { 374 dev_err(data->dev, "failed to read temperature\n"); 375 return ret; 376 } 377 378 adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf)); 379 if (adc_temp == BMP280_TEMP_SKIPPED) { 380 /* reading was skipped */ 381 dev_err(data->dev, "reading temperature skipped\n"); 382 return -EIO; 383 } 384 comp_temp = bmp280_compensate_temp(data, adc_temp); 385 386 /* 387 * val might be NULL if we're called by the read_press routine, 388 * who only cares about the carry over t_fine value. 389 */ 390 if (val) { 391 *val = comp_temp * 10; 392 return IIO_VAL_INT; 393 } 394 395 return 0; 396 } 397 398 static int bmp280_read_press(struct bmp280_data *data, 399 int *val, int *val2) 400 { 401 u32 comp_press; 402 s32 adc_press; 403 int ret; 404 405 /* Read and compensate temperature so we get a reading of t_fine. */ 406 ret = bmp280_read_temp(data, NULL, NULL); 407 if (ret < 0) 408 return ret; 409 410 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 411 data->buf, sizeof(data->buf)); 412 if (ret < 0) { 413 dev_err(data->dev, "failed to read pressure\n"); 414 return ret; 415 } 416 417 adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf)); 418 if (adc_press == BMP280_PRESS_SKIPPED) { 419 /* reading was skipped */ 420 dev_err(data->dev, "reading pressure skipped\n"); 421 return -EIO; 422 } 423 comp_press = bmp280_compensate_press(data, adc_press); 424 425 *val = comp_press; 426 *val2 = 256000; 427 428 return IIO_VAL_FRACTIONAL; 429 } 430 431 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2) 432 { 433 u32 comp_humidity; 434 s32 adc_humidity; 435 int ret; 436 437 /* Read and compensate temperature so we get a reading of t_fine. */ 438 ret = bmp280_read_temp(data, NULL, NULL); 439 if (ret < 0) 440 return ret; 441 442 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB, 443 &data->be16, sizeof(data->be16)); 444 if (ret < 0) { 445 dev_err(data->dev, "failed to read humidity\n"); 446 return ret; 447 } 448 449 adc_humidity = be16_to_cpu(data->be16); 450 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) { 451 /* reading was skipped */ 452 dev_err(data->dev, "reading humidity skipped\n"); 453 return -EIO; 454 } 455 comp_humidity = bmp280_compensate_humidity(data, adc_humidity); 456 457 *val = comp_humidity * 1000 / 1024; 458 459 return IIO_VAL_INT; 460 } 461 462 static int bmp280_read_raw(struct iio_dev *indio_dev, 463 struct iio_chan_spec const *chan, 464 int *val, int *val2, long mask) 465 { 466 struct bmp280_data *data = iio_priv(indio_dev); 467 int ret; 468 469 pm_runtime_get_sync(data->dev); 470 mutex_lock(&data->lock); 471 472 switch (mask) { 473 case IIO_CHAN_INFO_PROCESSED: 474 switch (chan->type) { 475 case IIO_HUMIDITYRELATIVE: 476 ret = data->chip_info->read_humid(data, val, val2); 477 break; 478 case IIO_PRESSURE: 479 ret = data->chip_info->read_press(data, val, val2); 480 break; 481 case IIO_TEMP: 482 ret = data->chip_info->read_temp(data, val, val2); 483 break; 484 default: 485 ret = -EINVAL; 486 break; 487 } 488 break; 489 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 490 switch (chan->type) { 491 case IIO_HUMIDITYRELATIVE: 492 *val = 1 << data->oversampling_humid; 493 ret = IIO_VAL_INT; 494 break; 495 case IIO_PRESSURE: 496 *val = 1 << data->oversampling_press; 497 ret = IIO_VAL_INT; 498 break; 499 case IIO_TEMP: 500 *val = 1 << data->oversampling_temp; 501 ret = IIO_VAL_INT; 502 break; 503 default: 504 ret = -EINVAL; 505 break; 506 } 507 break; 508 case IIO_CHAN_INFO_SAMP_FREQ: 509 if (!data->chip_info->sampling_freq_avail) { 510 ret = -EINVAL; 511 break; 512 } 513 514 *val = data->chip_info->sampling_freq_avail[data->sampling_freq][0]; 515 *val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1]; 516 ret = IIO_VAL_INT_PLUS_MICRO; 517 break; 518 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 519 if (!data->chip_info->iir_filter_coeffs_avail) { 520 ret = -EINVAL; 521 break; 522 } 523 524 *val = (1 << data->iir_filter_coeff) - 1; 525 ret = IIO_VAL_INT; 526 break; 527 default: 528 ret = -EINVAL; 529 break; 530 } 531 532 mutex_unlock(&data->lock); 533 pm_runtime_mark_last_busy(data->dev); 534 pm_runtime_put_autosuspend(data->dev); 535 536 return ret; 537 } 538 539 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data, 540 int val) 541 { 542 const int *avail = data->chip_info->oversampling_humid_avail; 543 const int n = data->chip_info->num_oversampling_humid_avail; 544 int ret, prev; 545 int i; 546 547 for (i = 0; i < n; i++) { 548 if (avail[i] == val) { 549 prev = data->oversampling_humid; 550 data->oversampling_humid = ilog2(val); 551 552 ret = data->chip_info->chip_config(data); 553 if (ret) { 554 data->oversampling_humid = prev; 555 data->chip_info->chip_config(data); 556 return ret; 557 } 558 return 0; 559 } 560 } 561 return -EINVAL; 562 } 563 564 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data, 565 int val) 566 { 567 const int *avail = data->chip_info->oversampling_temp_avail; 568 const int n = data->chip_info->num_oversampling_temp_avail; 569 int ret, prev; 570 int i; 571 572 for (i = 0; i < n; i++) { 573 if (avail[i] == val) { 574 prev = data->oversampling_temp; 575 data->oversampling_temp = ilog2(val); 576 577 ret = data->chip_info->chip_config(data); 578 if (ret) { 579 data->oversampling_temp = prev; 580 data->chip_info->chip_config(data); 581 return ret; 582 } 583 return 0; 584 } 585 } 586 return -EINVAL; 587 } 588 589 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data, 590 int val) 591 { 592 const int *avail = data->chip_info->oversampling_press_avail; 593 const int n = data->chip_info->num_oversampling_press_avail; 594 int ret, prev; 595 int i; 596 597 for (i = 0; i < n; i++) { 598 if (avail[i] == val) { 599 prev = data->oversampling_press; 600 data->oversampling_press = ilog2(val); 601 602 ret = data->chip_info->chip_config(data); 603 if (ret) { 604 data->oversampling_press = prev; 605 data->chip_info->chip_config(data); 606 return ret; 607 } 608 return 0; 609 } 610 } 611 return -EINVAL; 612 } 613 614 static int bmp280_write_sampling_frequency(struct bmp280_data *data, 615 int val, int val2) 616 { 617 const int (*avail)[2] = data->chip_info->sampling_freq_avail; 618 const int n = data->chip_info->num_sampling_freq_avail; 619 int ret, prev; 620 int i; 621 622 for (i = 0; i < n; i++) { 623 if (avail[i][0] == val && avail[i][1] == val2) { 624 prev = data->sampling_freq; 625 data->sampling_freq = i; 626 627 ret = data->chip_info->chip_config(data); 628 if (ret) { 629 data->sampling_freq = prev; 630 data->chip_info->chip_config(data); 631 return ret; 632 } 633 return 0; 634 } 635 } 636 return -EINVAL; 637 } 638 639 static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val) 640 { 641 const int *avail = data->chip_info->iir_filter_coeffs_avail; 642 const int n = data->chip_info->num_iir_filter_coeffs_avail; 643 int ret, prev; 644 int i; 645 646 for (i = 0; i < n; i++) { 647 if (avail[i] - 1 == val) { 648 prev = data->iir_filter_coeff; 649 data->iir_filter_coeff = i; 650 651 ret = data->chip_info->chip_config(data); 652 if (ret) { 653 data->iir_filter_coeff = prev; 654 data->chip_info->chip_config(data); 655 return ret; 656 657 } 658 return 0; 659 } 660 } 661 return -EINVAL; 662 } 663 664 static int bmp280_write_raw(struct iio_dev *indio_dev, 665 struct iio_chan_spec const *chan, 666 int val, int val2, long mask) 667 { 668 struct bmp280_data *data = iio_priv(indio_dev); 669 int ret = 0; 670 671 /* 672 * Helper functions to update sensor running configuration. 673 * If an error happens applying new settings, will try restore 674 * previous parameters to ensure the sensor is left in a known 675 * working configuration. 676 */ 677 switch (mask) { 678 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 679 pm_runtime_get_sync(data->dev); 680 mutex_lock(&data->lock); 681 switch (chan->type) { 682 case IIO_HUMIDITYRELATIVE: 683 ret = bmp280_write_oversampling_ratio_humid(data, val); 684 break; 685 case IIO_PRESSURE: 686 ret = bmp280_write_oversampling_ratio_press(data, val); 687 break; 688 case IIO_TEMP: 689 ret = bmp280_write_oversampling_ratio_temp(data, val); 690 break; 691 default: 692 ret = -EINVAL; 693 break; 694 } 695 mutex_unlock(&data->lock); 696 pm_runtime_mark_last_busy(data->dev); 697 pm_runtime_put_autosuspend(data->dev); 698 break; 699 case IIO_CHAN_INFO_SAMP_FREQ: 700 pm_runtime_get_sync(data->dev); 701 mutex_lock(&data->lock); 702 ret = bmp280_write_sampling_frequency(data, val, val2); 703 mutex_unlock(&data->lock); 704 pm_runtime_mark_last_busy(data->dev); 705 pm_runtime_put_autosuspend(data->dev); 706 break; 707 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 708 pm_runtime_get_sync(data->dev); 709 mutex_lock(&data->lock); 710 ret = bmp280_write_iir_filter_coeffs(data, val); 711 mutex_unlock(&data->lock); 712 pm_runtime_mark_last_busy(data->dev); 713 pm_runtime_put_autosuspend(data->dev); 714 break; 715 default: 716 return -EINVAL; 717 } 718 719 return ret; 720 } 721 722 static int bmp280_read_avail(struct iio_dev *indio_dev, 723 struct iio_chan_spec const *chan, 724 const int **vals, int *type, int *length, 725 long mask) 726 { 727 struct bmp280_data *data = iio_priv(indio_dev); 728 729 switch (mask) { 730 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 731 switch (chan->type) { 732 case IIO_PRESSURE: 733 *vals = data->chip_info->oversampling_press_avail; 734 *length = data->chip_info->num_oversampling_press_avail; 735 break; 736 case IIO_TEMP: 737 *vals = data->chip_info->oversampling_temp_avail; 738 *length = data->chip_info->num_oversampling_temp_avail; 739 break; 740 default: 741 return -EINVAL; 742 } 743 *type = IIO_VAL_INT; 744 return IIO_AVAIL_LIST; 745 case IIO_CHAN_INFO_SAMP_FREQ: 746 *vals = (const int *)data->chip_info->sampling_freq_avail; 747 *type = IIO_VAL_INT_PLUS_MICRO; 748 /* Values are stored in a 2D matrix */ 749 *length = data->chip_info->num_sampling_freq_avail; 750 return IIO_AVAIL_LIST; 751 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 752 *vals = data->chip_info->iir_filter_coeffs_avail; 753 *type = IIO_VAL_INT; 754 *length = data->chip_info->num_iir_filter_coeffs_avail; 755 return IIO_AVAIL_LIST; 756 default: 757 return -EINVAL; 758 } 759 } 760 761 static const struct iio_info bmp280_info = { 762 .read_raw = &bmp280_read_raw, 763 .read_avail = &bmp280_read_avail, 764 .write_raw = &bmp280_write_raw, 765 }; 766 767 static int bmp280_chip_config(struct bmp280_data *data) 768 { 769 u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) | 770 FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1); 771 int ret; 772 773 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS, 774 BMP280_OSRS_TEMP_MASK | 775 BMP280_OSRS_PRESS_MASK | 776 BMP280_MODE_MASK, 777 osrs | BMP280_MODE_NORMAL); 778 if (ret < 0) { 779 dev_err(data->dev, 780 "failed to write ctrl_meas register\n"); 781 return ret; 782 } 783 784 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG, 785 BMP280_FILTER_MASK, 786 BMP280_FILTER_4X); 787 if (ret < 0) { 788 dev_err(data->dev, 789 "failed to write config register\n"); 790 return ret; 791 } 792 793 return ret; 794 } 795 796 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; 797 798 const struct bmp280_chip_info bmp280_chip_info = { 799 .id_reg = BMP280_REG_ID, 800 .chip_id = BMP280_CHIP_ID, 801 .regmap_config = &bmp280_regmap_config, 802 .start_up_time = 2000, 803 .channels = bmp280_channels, 804 .num_channels = 2, 805 806 .oversampling_temp_avail = bmp280_oversampling_avail, 807 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 808 /* 809 * Oversampling config values on BMx280 have one additional setting 810 * that other generations of the family don't: 811 * The value 0 means the measurement is bypassed instead of 812 * oversampling set to x1. 813 * 814 * To account for this difference, and preserve the same common 815 * config logic, this is handled later on chip_config callback 816 * incrementing one unit the oversampling setting. 817 */ 818 .oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1, 819 820 .oversampling_press_avail = bmp280_oversampling_avail, 821 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 822 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1, 823 824 .chip_config = bmp280_chip_config, 825 .read_temp = bmp280_read_temp, 826 .read_press = bmp280_read_press, 827 .read_calib = bmp280_read_calib, 828 }; 829 EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280); 830 831 static int bme280_chip_config(struct bmp280_data *data) 832 { 833 u8 osrs = FIELD_PREP(BMP280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1); 834 int ret; 835 836 /* 837 * Oversampling of humidity must be set before oversampling of 838 * temperature/pressure is set to become effective. 839 */ 840 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY, 841 BMP280_OSRS_HUMIDITY_MASK, osrs); 842 843 if (ret < 0) 844 return ret; 845 846 return bmp280_chip_config(data); 847 } 848 849 const struct bmp280_chip_info bme280_chip_info = { 850 .id_reg = BMP280_REG_ID, 851 .chip_id = BME280_CHIP_ID, 852 .regmap_config = &bmp280_regmap_config, 853 .start_up_time = 2000, 854 .channels = bmp280_channels, 855 .num_channels = 3, 856 857 .oversampling_temp_avail = bmp280_oversampling_avail, 858 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 859 .oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1, 860 861 .oversampling_press_avail = bmp280_oversampling_avail, 862 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 863 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1, 864 865 .oversampling_humid_avail = bmp280_oversampling_avail, 866 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), 867 .oversampling_humid_default = BMP280_OSRS_HUMIDITY_16X - 1, 868 869 .chip_config = bme280_chip_config, 870 .read_temp = bmp280_read_temp, 871 .read_press = bmp280_read_press, 872 .read_humid = bmp280_read_humid, 873 .read_calib = bme280_read_calib, 874 }; 875 EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280); 876 877 /* 878 * Helper function to send a command to BMP3XX sensors. 879 * 880 * Sensor processes commands written to the CMD register and signals 881 * execution result through "cmd_rdy" and "cmd_error" flags available on 882 * STATUS and ERROR registers. 883 */ 884 static int bmp380_cmd(struct bmp280_data *data, u8 cmd) 885 { 886 unsigned int reg; 887 int ret; 888 889 /* Check if device is ready to process a command */ 890 ret = regmap_read(data->regmap, BMP380_REG_STATUS, ®); 891 if (ret) { 892 dev_err(data->dev, "failed to read error register\n"); 893 return ret; 894 } 895 if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) { 896 dev_err(data->dev, "device is not ready to accept commands\n"); 897 return -EBUSY; 898 } 899 900 /* Send command to process */ 901 ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd); 902 if (ret) { 903 dev_err(data->dev, "failed to send command to device\n"); 904 return ret; 905 } 906 /* Wait for 2ms for command to be processed */ 907 usleep_range(data->start_up_time, data->start_up_time + 100); 908 /* Check for command processing error */ 909 ret = regmap_read(data->regmap, BMP380_REG_ERROR, ®); 910 if (ret) { 911 dev_err(data->dev, "error reading ERROR reg\n"); 912 return ret; 913 } 914 if (reg & BMP380_ERR_CMD_MASK) { 915 dev_err(data->dev, "error processing command 0x%X\n", cmd); 916 return -EINVAL; 917 } 918 919 return 0; 920 } 921 922 /* 923 * Returns temperature in Celsius degrees, resolution is 0.01º C. Output value of 924 * "5123" equals 51.2º C. t_fine carries fine temperature as global value. 925 * 926 * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo 927 * https://github.com/BoschSensortec/BMP3-Sensor-API. 928 */ 929 static s32 bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp) 930 { 931 s64 var1, var2, var3, var4, var5, var6, comp_temp; 932 struct bmp380_calib *calib = &data->calib.bmp380; 933 934 var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8); 935 var2 = var1 * ((s64) calib->T2); 936 var3 = var1 * var1; 937 var4 = var3 * ((s64) calib->T3); 938 var5 = (var2 << 18) + var4; 939 var6 = var5 >> 32; 940 data->t_fine = (s32) var6; 941 comp_temp = (var6 * 25) >> 14; 942 943 comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP); 944 return (s32) comp_temp; 945 } 946 947 /* 948 * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal. 949 * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa. 950 * 951 * Taken from datasheet, Section 9.3. "Pressure compensation" and repository 952 * https://github.com/BoschSensortec/BMP3-Sensor-API. 953 */ 954 static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press) 955 { 956 s64 var1, var2, var3, var4, var5, var6, offset, sensitivity; 957 struct bmp380_calib *calib = &data->calib.bmp380; 958 u32 comp_press; 959 960 var1 = (s64)data->t_fine * (s64)data->t_fine; 961 var2 = var1 >> 6; 962 var3 = (var2 * ((s64) data->t_fine)) >> 8; 963 var4 = ((s64)calib->P8 * var3) >> 5; 964 var5 = ((s64)calib->P7 * var1) << 4; 965 var6 = ((s64)calib->P6 * (s64)data->t_fine) << 22; 966 offset = ((s64)calib->P5 << 47) + var4 + var5 + var6; 967 var2 = ((s64)calib->P4 * var3) >> 5; 968 var4 = ((s64)calib->P3 * var1) << 2; 969 var5 = ((s64)calib->P2 - ((s64)1 << 14)) * 970 ((s64)data->t_fine << 21); 971 sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) + 972 var2 + var4 + var5; 973 var1 = (sensitivity >> 24) * (s64)adc_press; 974 var2 = (s64)calib->P10 * (s64)data->t_fine; 975 var3 = var2 + ((s64)calib->P9 << 16); 976 var4 = (var3 * (s64)adc_press) >> 13; 977 978 /* 979 * Dividing by 10 followed by multiplying by 10 to avoid 980 * possible overflow caused by (uncomp_data->pressure * partial_data4). 981 */ 982 var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9; 983 var5 *= 10; 984 var6 = (s64)adc_press * (s64)adc_press; 985 var2 = ((s64)calib->P11 * var6) >> 16; 986 var3 = (var2 * (s64)adc_press) >> 7; 987 var4 = (offset >> 2) + var1 + var5 + var3; 988 comp_press = ((u64)var4 * 25) >> 40; 989 990 comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES); 991 return comp_press; 992 } 993 994 static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2) 995 { 996 s32 comp_temp; 997 u32 adc_temp; 998 int ret; 999 1000 ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB, 1001 data->buf, sizeof(data->buf)); 1002 if (ret) { 1003 dev_err(data->dev, "failed to read temperature\n"); 1004 return ret; 1005 } 1006 1007 adc_temp = get_unaligned_le24(data->buf); 1008 if (adc_temp == BMP380_TEMP_SKIPPED) { 1009 dev_err(data->dev, "reading temperature skipped\n"); 1010 return -EIO; 1011 } 1012 comp_temp = bmp380_compensate_temp(data, adc_temp); 1013 1014 /* 1015 * Val might be NULL if we're called by the read_press routine, 1016 * who only cares about the carry over t_fine value. 1017 */ 1018 if (val) { 1019 /* IIO reports temperatures in milli Celsius */ 1020 *val = comp_temp * 10; 1021 return IIO_VAL_INT; 1022 } 1023 1024 return 0; 1025 } 1026 1027 static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2) 1028 { 1029 s32 comp_press; 1030 u32 adc_press; 1031 int ret; 1032 1033 /* Read and compensate for temperature so we get a reading of t_fine */ 1034 ret = bmp380_read_temp(data, NULL, NULL); 1035 if (ret) 1036 return ret; 1037 1038 ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, 1039 data->buf, sizeof(data->buf)); 1040 if (ret) { 1041 dev_err(data->dev, "failed to read pressure\n"); 1042 return ret; 1043 } 1044 1045 adc_press = get_unaligned_le24(data->buf); 1046 if (adc_press == BMP380_PRESS_SKIPPED) { 1047 dev_err(data->dev, "reading pressure skipped\n"); 1048 return -EIO; 1049 } 1050 comp_press = bmp380_compensate_press(data, adc_press); 1051 1052 *val = comp_press; 1053 /* Compensated pressure is in cPa (centipascals) */ 1054 *val2 = 100000; 1055 1056 return IIO_VAL_FRACTIONAL; 1057 } 1058 1059 static int bmp380_read_calib(struct bmp280_data *data) 1060 { 1061 struct bmp380_calib *calib = &data->calib.bmp380; 1062 int ret; 1063 1064 /* Read temperature and pressure calibration data */ 1065 ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START, 1066 data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf)); 1067 if (ret) { 1068 dev_err(data->dev, 1069 "failed to read temperature calibration parameters\n"); 1070 return ret; 1071 } 1072 1073 /* Toss the temperature calibration data into the entropy pool */ 1074 add_device_randomness(data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf)); 1075 1076 /* Parse calibration values */ 1077 calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]); 1078 calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]); 1079 calib->T3 = data->bmp380_cal_buf[BMP380_T3]; 1080 calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]); 1081 calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]); 1082 calib->P3 = data->bmp380_cal_buf[BMP380_P3]; 1083 calib->P4 = data->bmp380_cal_buf[BMP380_P4]; 1084 calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]); 1085 calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]); 1086 calib->P7 = data->bmp380_cal_buf[BMP380_P7]; 1087 calib->P8 = data->bmp380_cal_buf[BMP380_P8]; 1088 calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]); 1089 calib->P10 = data->bmp380_cal_buf[BMP380_P10]; 1090 calib->P11 = data->bmp380_cal_buf[BMP380_P11]; 1091 1092 return 0; 1093 } 1094 1095 static const int bmp380_odr_table[][2] = { 1096 [BMP380_ODR_200HZ] = {200, 0}, 1097 [BMP380_ODR_100HZ] = {100, 0}, 1098 [BMP380_ODR_50HZ] = {50, 0}, 1099 [BMP380_ODR_25HZ] = {25, 0}, 1100 [BMP380_ODR_12_5HZ] = {12, 500000}, 1101 [BMP380_ODR_6_25HZ] = {6, 250000}, 1102 [BMP380_ODR_3_125HZ] = {3, 125000}, 1103 [BMP380_ODR_1_5625HZ] = {1, 562500}, 1104 [BMP380_ODR_0_78HZ] = {0, 781250}, 1105 [BMP380_ODR_0_39HZ] = {0, 390625}, 1106 [BMP380_ODR_0_2HZ] = {0, 195313}, 1107 [BMP380_ODR_0_1HZ] = {0, 97656}, 1108 [BMP380_ODR_0_05HZ] = {0, 48828}, 1109 [BMP380_ODR_0_02HZ] = {0, 24414}, 1110 [BMP380_ODR_0_01HZ] = {0, 12207}, 1111 [BMP380_ODR_0_006HZ] = {0, 6104}, 1112 [BMP380_ODR_0_003HZ] = {0, 3052}, 1113 [BMP380_ODR_0_0015HZ] = {0, 1526}, 1114 }; 1115 1116 static int bmp380_preinit(struct bmp280_data *data) 1117 { 1118 /* BMP3xx requires soft-reset as part of initialization */ 1119 return bmp380_cmd(data, BMP380_CMD_SOFT_RESET); 1120 } 1121 1122 static int bmp380_chip_config(struct bmp280_data *data) 1123 { 1124 bool change = false, aux; 1125 unsigned int tmp; 1126 u8 osrs; 1127 int ret; 1128 1129 /* Configure power control register */ 1130 ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL, 1131 BMP380_CTRL_SENSORS_MASK, 1132 BMP380_CTRL_SENSORS_PRESS_EN | 1133 BMP380_CTRL_SENSORS_TEMP_EN); 1134 if (ret) { 1135 dev_err(data->dev, 1136 "failed to write operation control register\n"); 1137 return ret; 1138 } 1139 1140 /* Configure oversampling */ 1141 osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) | 1142 FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press); 1143 1144 ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR, 1145 BMP380_OSRS_TEMP_MASK | 1146 BMP380_OSRS_PRESS_MASK, 1147 osrs, &aux); 1148 if (ret) { 1149 dev_err(data->dev, "failed to write oversampling register\n"); 1150 return ret; 1151 } 1152 change = change || aux; 1153 1154 /* Configure output data rate */ 1155 ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR, 1156 BMP380_ODRS_MASK, data->sampling_freq, &aux); 1157 if (ret) { 1158 dev_err(data->dev, "failed to write ODR selection register\n"); 1159 return ret; 1160 } 1161 change = change || aux; 1162 1163 /* Set filter data */ 1164 ret = regmap_update_bits_check(data->regmap, BMP380_REG_CONFIG, BMP380_FILTER_MASK, 1165 FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff), 1166 &aux); 1167 if (ret) { 1168 dev_err(data->dev, "failed to write config register\n"); 1169 return ret; 1170 } 1171 change = change || aux; 1172 1173 if (change) { 1174 /* 1175 * The configurations errors are detected on the fly during a measurement 1176 * cycle. If the sampling frequency is too low, it's faster to reset 1177 * the measurement loop than wait until the next measurement is due. 1178 * 1179 * Resets sensor measurement loop toggling between sleep and normal 1180 * operating modes. 1181 */ 1182 ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL, 1183 BMP380_MODE_MASK, 1184 FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_SLEEP)); 1185 if (ret) { 1186 dev_err(data->dev, "failed to set sleep mode\n"); 1187 return ret; 1188 } 1189 usleep_range(2000, 2500); 1190 ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL, 1191 BMP380_MODE_MASK, 1192 FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL)); 1193 if (ret) { 1194 dev_err(data->dev, "failed to set normal mode\n"); 1195 return ret; 1196 } 1197 /* 1198 * Waits for measurement before checking configuration error flag. 1199 * Selected longest measure time indicated in section 3.9.1 1200 * in the datasheet. 1201 */ 1202 msleep(80); 1203 1204 /* Check config error flag */ 1205 ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp); 1206 if (ret) { 1207 dev_err(data->dev, 1208 "failed to read error register\n"); 1209 return ret; 1210 } 1211 if (tmp & BMP380_ERR_CONF_MASK) { 1212 dev_warn(data->dev, 1213 "sensor flagged configuration as incompatible\n"); 1214 return -EINVAL; 1215 } 1216 } 1217 1218 return 0; 1219 } 1220 1221 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; 1222 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128}; 1223 1224 const struct bmp280_chip_info bmp380_chip_info = { 1225 .id_reg = BMP380_REG_ID, 1226 .chip_id = BMP380_CHIP_ID, 1227 .regmap_config = &bmp380_regmap_config, 1228 .start_up_time = 2000, 1229 .channels = bmp380_channels, 1230 .num_channels = 2, 1231 1232 .oversampling_temp_avail = bmp380_oversampling_avail, 1233 .num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail), 1234 .oversampling_temp_default = ilog2(1), 1235 1236 .oversampling_press_avail = bmp380_oversampling_avail, 1237 .num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail), 1238 .oversampling_press_default = ilog2(4), 1239 1240 .sampling_freq_avail = bmp380_odr_table, 1241 .num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2, 1242 .sampling_freq_default = BMP380_ODR_50HZ, 1243 1244 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail, 1245 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 1246 .iir_filter_coeff_default = 2, 1247 1248 .chip_config = bmp380_chip_config, 1249 .read_temp = bmp380_read_temp, 1250 .read_press = bmp380_read_press, 1251 .read_calib = bmp380_read_calib, 1252 .preinit = bmp380_preinit, 1253 }; 1254 EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280); 1255 1256 static int bmp580_soft_reset(struct bmp280_data *data) 1257 { 1258 unsigned int reg; 1259 int ret; 1260 1261 ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET); 1262 if (ret) { 1263 dev_err(data->dev, "failed to send reset command to device\n"); 1264 return ret; 1265 } 1266 usleep_range(2000, 2500); 1267 1268 /* Dummy read of chip_id */ 1269 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®); 1270 if (ret) { 1271 dev_err(data->dev, "failed to reestablish comms after reset\n"); 1272 return ret; 1273 } 1274 1275 ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, ®); 1276 if (ret) { 1277 dev_err(data->dev, "error reading interrupt status register\n"); 1278 return ret; 1279 } 1280 if (!(reg & BMP580_INT_STATUS_POR_MASK)) { 1281 dev_err(data->dev, "error resetting sensor\n"); 1282 return -EINVAL; 1283 } 1284 1285 return 0; 1286 } 1287 1288 /** 1289 * bmp580_nvm_operation() - Helper function to commit NVM memory operations 1290 * @data: sensor data struct 1291 * @is_write: flag to signal write operation 1292 */ 1293 static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write) 1294 { 1295 unsigned long timeout, poll; 1296 unsigned int reg; 1297 int ret; 1298 1299 /* Check NVM ready flag */ 1300 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®); 1301 if (ret) { 1302 dev_err(data->dev, "failed to check nvm status\n"); 1303 return ret; 1304 } 1305 if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) { 1306 dev_err(data->dev, "sensor's nvm is not ready\n"); 1307 return -EIO; 1308 } 1309 1310 /* Start NVM operation sequence */ 1311 ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_NVM_OP_SEQ_0); 1312 if (ret) { 1313 dev_err(data->dev, "failed to send nvm operation's first sequence\n"); 1314 return ret; 1315 } 1316 if (is_write) { 1317 /* Send NVM write sequence */ 1318 ret = regmap_write(data->regmap, BMP580_REG_CMD, 1319 BMP580_CMD_NVM_WRITE_SEQ_1); 1320 if (ret) { 1321 dev_err(data->dev, "failed to send nvm write sequence\n"); 1322 return ret; 1323 } 1324 /* Datasheet says on 4.8.1.2 it takes approximately 10ms */ 1325 poll = 2000; 1326 timeout = 12000; 1327 } else { 1328 /* Send NVM read sequence */ 1329 ret = regmap_write(data->regmap, BMP580_REG_CMD, 1330 BMP580_CMD_NVM_READ_SEQ_1); 1331 if (ret) { 1332 dev_err(data->dev, "failed to send nvm read sequence\n"); 1333 return ret; 1334 } 1335 /* Datasheet says on 4.8.1.1 it takes approximately 200us */ 1336 poll = 50; 1337 timeout = 400; 1338 } 1339 if (ret) { 1340 dev_err(data->dev, "failed to write command sequence\n"); 1341 return -EIO; 1342 } 1343 1344 /* Wait until NVM is ready again */ 1345 ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg, 1346 (reg & BMP580_STATUS_NVM_RDY_MASK), 1347 poll, timeout); 1348 if (ret) { 1349 dev_err(data->dev, "error checking nvm operation status\n"); 1350 return ret; 1351 } 1352 1353 /* Check NVM error flags */ 1354 if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) { 1355 dev_err(data->dev, "error processing nvm operation\n"); 1356 return -EIO; 1357 } 1358 1359 return 0; 1360 } 1361 1362 /* 1363 * Contrary to previous sensors families, compensation algorithm is builtin. 1364 * We are only required to read the register raw data and adapt the ranges 1365 * for what is expected on IIO ABI. 1366 */ 1367 1368 static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2) 1369 { 1370 s32 raw_temp; 1371 int ret; 1372 1373 ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf, 1374 sizeof(data->buf)); 1375 if (ret) { 1376 dev_err(data->dev, "failed to read temperature\n"); 1377 return ret; 1378 } 1379 1380 raw_temp = get_unaligned_le24(data->buf); 1381 if (raw_temp == BMP580_TEMP_SKIPPED) { 1382 dev_err(data->dev, "reading temperature skipped\n"); 1383 return -EIO; 1384 } 1385 1386 /* 1387 * Temperature is returned in Celsius degrees in fractional 1388 * form down 2^16. We rescale by x1000 to return millidegrees 1389 * Celsius to respect IIO ABI. 1390 */ 1391 raw_temp = sign_extend32(raw_temp, 23); 1392 *val = ((s64)raw_temp * 1000) / (1 << 16); 1393 return IIO_VAL_INT; 1394 } 1395 1396 static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2) 1397 { 1398 u32 raw_press; 1399 int ret; 1400 1401 ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf, 1402 sizeof(data->buf)); 1403 if (ret) { 1404 dev_err(data->dev, "failed to read pressure\n"); 1405 return ret; 1406 } 1407 1408 raw_press = get_unaligned_le24(data->buf); 1409 if (raw_press == BMP580_PRESS_SKIPPED) { 1410 dev_err(data->dev, "reading pressure skipped\n"); 1411 return -EIO; 1412 } 1413 /* 1414 * Pressure is returned in Pascals in fractional form down 2^16. 1415 * We rescale /1000 to convert to kilopascal to respect IIO ABI. 1416 */ 1417 *val = raw_press; 1418 *val2 = 64000; /* 2^6 * 1000 */ 1419 return IIO_VAL_FRACTIONAL; 1420 } 1421 1422 static const int bmp580_odr_table[][2] = { 1423 [BMP580_ODR_240HZ] = {240, 0}, 1424 [BMP580_ODR_218HZ] = {218, 0}, 1425 [BMP580_ODR_199HZ] = {199, 0}, 1426 [BMP580_ODR_179HZ] = {179, 0}, 1427 [BMP580_ODR_160HZ] = {160, 0}, 1428 [BMP580_ODR_149HZ] = {149, 0}, 1429 [BMP580_ODR_140HZ] = {140, 0}, 1430 [BMP580_ODR_129HZ] = {129, 0}, 1431 [BMP580_ODR_120HZ] = {120, 0}, 1432 [BMP580_ODR_110HZ] = {110, 0}, 1433 [BMP580_ODR_100HZ] = {100, 0}, 1434 [BMP580_ODR_89HZ] = {89, 0}, 1435 [BMP580_ODR_80HZ] = {80, 0}, 1436 [BMP580_ODR_70HZ] = {70, 0}, 1437 [BMP580_ODR_60HZ] = {60, 0}, 1438 [BMP580_ODR_50HZ] = {50, 0}, 1439 [BMP580_ODR_45HZ] = {45, 0}, 1440 [BMP580_ODR_40HZ] = {40, 0}, 1441 [BMP580_ODR_35HZ] = {35, 0}, 1442 [BMP580_ODR_30HZ] = {30, 0}, 1443 [BMP580_ODR_25HZ] = {25, 0}, 1444 [BMP580_ODR_20HZ] = {20, 0}, 1445 [BMP580_ODR_15HZ] = {15, 0}, 1446 [BMP580_ODR_10HZ] = {10, 0}, 1447 [BMP580_ODR_5HZ] = {5, 0}, 1448 [BMP580_ODR_4HZ] = {4, 0}, 1449 [BMP580_ODR_3HZ] = {3, 0}, 1450 [BMP580_ODR_2HZ] = {2, 0}, 1451 [BMP580_ODR_1HZ] = {1, 0}, 1452 [BMP580_ODR_0_5HZ] = {0, 500000}, 1453 [BMP580_ODR_0_25HZ] = {0, 250000}, 1454 [BMP580_ODR_0_125HZ] = {0, 125000}, 1455 }; 1456 1457 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 }; 1458 1459 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val, 1460 size_t bytes) 1461 { 1462 struct bmp280_data *data = priv; 1463 u16 *dst = val; 1464 int ret, addr; 1465 1466 pm_runtime_get_sync(data->dev); 1467 mutex_lock(&data->lock); 1468 1469 /* Set sensor in standby mode */ 1470 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, 1471 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS, 1472 BMP580_ODR_DEEPSLEEP_DIS | 1473 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP)); 1474 if (ret) { 1475 dev_err(data->dev, "failed to change sensor to standby mode\n"); 1476 goto exit; 1477 } 1478 /* Wait standby transition time */ 1479 usleep_range(2500, 3000); 1480 1481 while (bytes >= sizeof(*dst)) { 1482 addr = bmp580_nvmem_addrs[offset / sizeof(*dst)]; 1483 1484 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, 1485 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr)); 1486 if (ret) { 1487 dev_err(data->dev, "error writing nvm address\n"); 1488 goto exit; 1489 } 1490 1491 ret = bmp580_nvm_operation(data, false); 1492 if (ret) 1493 goto exit; 1494 1495 ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16, 1496 sizeof(data->le16)); 1497 if (ret) { 1498 dev_err(data->dev, "error reading nvm data regs\n"); 1499 goto exit; 1500 } 1501 1502 *dst++ = le16_to_cpu(data->le16); 1503 bytes -= sizeof(*dst); 1504 offset += sizeof(*dst); 1505 } 1506 exit: 1507 /* Restore chip config */ 1508 data->chip_info->chip_config(data); 1509 mutex_unlock(&data->lock); 1510 pm_runtime_mark_last_busy(data->dev); 1511 pm_runtime_put_autosuspend(data->dev); 1512 return ret; 1513 } 1514 1515 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val, 1516 size_t bytes) 1517 { 1518 struct bmp280_data *data = priv; 1519 u16 *buf = val; 1520 int ret, addr; 1521 1522 pm_runtime_get_sync(data->dev); 1523 mutex_lock(&data->lock); 1524 1525 /* Set sensor in standby mode */ 1526 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, 1527 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS, 1528 BMP580_ODR_DEEPSLEEP_DIS | 1529 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP)); 1530 if (ret) { 1531 dev_err(data->dev, "failed to change sensor to standby mode\n"); 1532 goto exit; 1533 } 1534 /* Wait standby transition time */ 1535 usleep_range(2500, 3000); 1536 1537 while (bytes >= sizeof(*buf)) { 1538 addr = bmp580_nvmem_addrs[offset / sizeof(*buf)]; 1539 1540 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, BMP580_NVM_PROG_EN | 1541 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr)); 1542 if (ret) { 1543 dev_err(data->dev, "error writing nvm address\n"); 1544 goto exit; 1545 } 1546 data->le16 = cpu_to_le16(*buf++); 1547 1548 ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16, 1549 sizeof(data->le16)); 1550 if (ret) { 1551 dev_err(data->dev, "error writing LSB NVM data regs\n"); 1552 goto exit; 1553 } 1554 1555 ret = bmp580_nvm_operation(data, true); 1556 if (ret) 1557 goto exit; 1558 1559 /* Disable programming mode bit */ 1560 ret = regmap_update_bits(data->regmap, BMP580_REG_NVM_ADDR, 1561 BMP580_NVM_PROG_EN, 0); 1562 if (ret) { 1563 dev_err(data->dev, "error resetting nvm write\n"); 1564 goto exit; 1565 } 1566 1567 bytes -= sizeof(*buf); 1568 offset += sizeof(*buf); 1569 } 1570 exit: 1571 /* Restore chip config */ 1572 data->chip_info->chip_config(data); 1573 mutex_unlock(&data->lock); 1574 pm_runtime_mark_last_busy(data->dev); 1575 pm_runtime_put_autosuspend(data->dev); 1576 return ret; 1577 } 1578 1579 static int bmp580_preinit(struct bmp280_data *data) 1580 { 1581 struct nvmem_config config = { 1582 .dev = data->dev, 1583 .priv = data, 1584 .name = "bmp580_nvmem", 1585 .word_size = sizeof(u16), 1586 .stride = sizeof(u16), 1587 .size = 3 * sizeof(u16), 1588 .reg_read = bmp580_nvmem_read, 1589 .reg_write = bmp580_nvmem_write, 1590 }; 1591 unsigned int reg; 1592 int ret; 1593 1594 /* Issue soft-reset command */ 1595 ret = bmp580_soft_reset(data); 1596 if (ret) 1597 return ret; 1598 1599 /* Post powerup sequence */ 1600 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®); 1601 if (ret) 1602 return ret; 1603 1604 /* Print warn message if we don't know the chip id */ 1605 if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT) 1606 dev_warn(data->dev, "preinit: unexpected chip_id\n"); 1607 1608 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®); 1609 if (ret) 1610 return ret; 1611 1612 /* Check nvm status */ 1613 if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) { 1614 dev_err(data->dev, "preinit: nvm error on powerup sequence\n"); 1615 return -EIO; 1616 } 1617 1618 /* Register nvmem device */ 1619 return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config)); 1620 } 1621 1622 static int bmp580_chip_config(struct bmp280_data *data) 1623 { 1624 bool change = false, aux; 1625 unsigned int tmp; 1626 u8 reg_val; 1627 int ret; 1628 1629 /* Sets sensor in standby mode */ 1630 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, 1631 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS, 1632 BMP580_ODR_DEEPSLEEP_DIS | 1633 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP)); 1634 if (ret) { 1635 dev_err(data->dev, "failed to change sensor to standby mode\n"); 1636 return ret; 1637 } 1638 /* From datasheet's table 4: electrical characteristics */ 1639 usleep_range(2500, 3000); 1640 1641 /* Set default DSP mode settings */ 1642 reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) | 1643 BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN; 1644 1645 ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG, 1646 BMP580_DSP_COMP_MASK | 1647 BMP580_DSP_SHDW_IIR_TEMP_EN | 1648 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val); 1649 1650 /* Configure oversampling */ 1651 reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) | 1652 FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) | 1653 BMP580_OSR_PRESS_EN; 1654 1655 ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG, 1656 BMP580_OSR_TEMP_MASK | BMP580_OSR_PRESS_MASK | 1657 BMP580_OSR_PRESS_EN, 1658 reg_val, &aux); 1659 if (ret) { 1660 dev_err(data->dev, "failed to write oversampling register\n"); 1661 return ret; 1662 } 1663 change = change || aux; 1664 1665 /* Configure output data rate */ 1666 ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK, 1667 FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq), 1668 &aux); 1669 if (ret) { 1670 dev_err(data->dev, "failed to write ODR configuration register\n"); 1671 return ret; 1672 } 1673 change = change || aux; 1674 1675 /* Set filter data */ 1676 reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) | 1677 FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff); 1678 1679 ret = regmap_update_bits_check(data->regmap, BMP580_REG_DSP_IIR, 1680 BMP580_DSP_IIR_PRESS_MASK | 1681 BMP580_DSP_IIR_TEMP_MASK, 1682 reg_val, &aux); 1683 if (ret) { 1684 dev_err(data->dev, "failed to write config register\n"); 1685 return ret; 1686 } 1687 change = change || aux; 1688 1689 /* Restore sensor to normal operation mode */ 1690 ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG, 1691 BMP580_MODE_MASK, 1692 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_NORMAL)); 1693 if (ret) { 1694 dev_err(data->dev, "failed to set normal mode\n"); 1695 return ret; 1696 } 1697 /* From datasheet's table 4: electrical characteristics */ 1698 usleep_range(3000, 3500); 1699 1700 if (change) { 1701 /* 1702 * Check if ODR and OSR settings are valid or we are 1703 * operating in a degraded mode. 1704 */ 1705 ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp); 1706 if (ret) { 1707 dev_err(data->dev, "error reading effective OSR register\n"); 1708 return ret; 1709 } 1710 if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) { 1711 dev_warn(data->dev, "OSR and ODR incompatible settings detected\n"); 1712 /* Set current OSR settings from data on effective OSR */ 1713 data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp); 1714 data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp); 1715 return -EINVAL; 1716 } 1717 } 1718 1719 return 0; 1720 } 1721 1722 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 1723 1724 const struct bmp280_chip_info bmp580_chip_info = { 1725 .id_reg = BMP580_REG_CHIP_ID, 1726 .chip_id = BMP580_CHIP_ID, 1727 .regmap_config = &bmp580_regmap_config, 1728 .start_up_time = 2000, 1729 .channels = bmp380_channels, 1730 .num_channels = 2, 1731 1732 .oversampling_temp_avail = bmp580_oversampling_avail, 1733 .num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail), 1734 .oversampling_temp_default = ilog2(1), 1735 1736 .oversampling_press_avail = bmp580_oversampling_avail, 1737 .num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail), 1738 .oversampling_press_default = ilog2(4), 1739 1740 .sampling_freq_avail = bmp580_odr_table, 1741 .num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2, 1742 .sampling_freq_default = BMP580_ODR_50HZ, 1743 1744 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail, 1745 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 1746 .iir_filter_coeff_default = 2, 1747 1748 .chip_config = bmp580_chip_config, 1749 .read_temp = bmp580_read_temp, 1750 .read_press = bmp580_read_press, 1751 .preinit = bmp580_preinit, 1752 }; 1753 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280); 1754 1755 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas) 1756 { 1757 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 }; 1758 unsigned int delay_us; 1759 unsigned int ctrl; 1760 int ret; 1761 1762 if (data->use_eoc) 1763 reinit_completion(&data->done); 1764 1765 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas); 1766 if (ret) 1767 return ret; 1768 1769 if (data->use_eoc) { 1770 /* 1771 * If we have a completion interrupt, use it, wait up to 1772 * 100ms. The longest conversion time listed is 76.5 ms for 1773 * advanced resolution mode. 1774 */ 1775 ret = wait_for_completion_timeout(&data->done, 1776 1 + msecs_to_jiffies(100)); 1777 if (!ret) 1778 dev_err(data->dev, "timeout waiting for completion\n"); 1779 } else { 1780 if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP) 1781 delay_us = 4500; 1782 else 1783 delay_us = 1784 conversion_time_max[data->oversampling_press]; 1785 1786 usleep_range(delay_us, delay_us + 1000); 1787 } 1788 1789 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl); 1790 if (ret) 1791 return ret; 1792 1793 /* The value of this bit reset to "0" after conversion is complete */ 1794 if (ctrl & BMP180_MEAS_SCO) 1795 return -EIO; 1796 1797 return 0; 1798 } 1799 1800 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val) 1801 { 1802 int ret; 1803 1804 ret = bmp180_measure(data, 1805 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) | 1806 BMP180_MEAS_SCO); 1807 if (ret) 1808 return ret; 1809 1810 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, 1811 &data->be16, sizeof(data->be16)); 1812 if (ret) 1813 return ret; 1814 1815 *val = be16_to_cpu(data->be16); 1816 1817 return 0; 1818 } 1819 1820 static int bmp180_read_calib(struct bmp280_data *data) 1821 { 1822 struct bmp180_calib *calib = &data->calib.bmp180; 1823 int ret; 1824 int i; 1825 1826 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, 1827 data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf)); 1828 1829 if (ret < 0) 1830 return ret; 1831 1832 /* None of the words has the value 0 or 0xFFFF */ 1833 for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) { 1834 if (data->bmp180_cal_buf[i] == cpu_to_be16(0) || 1835 data->bmp180_cal_buf[i] == cpu_to_be16(0xffff)) 1836 return -EIO; 1837 } 1838 1839 /* Toss the calibration data into the entropy pool */ 1840 add_device_randomness(data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf)); 1841 1842 calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]); 1843 calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]); 1844 calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]); 1845 calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]); 1846 calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]); 1847 calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]); 1848 calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]); 1849 calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]); 1850 calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]); 1851 calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]); 1852 calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]); 1853 1854 return 0; 1855 } 1856 1857 /* 1858 * Returns temperature in DegC, resolution is 0.1 DegC. 1859 * t_fine carries fine temperature as global value. 1860 * 1861 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". 1862 */ 1863 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp) 1864 { 1865 struct bmp180_calib *calib = &data->calib.bmp180; 1866 s32 x1, x2; 1867 1868 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15; 1869 x2 = (calib->MC << 11) / (x1 + calib->MD); 1870 data->t_fine = x1 + x2; 1871 1872 return (data->t_fine + 8) >> 4; 1873 } 1874 1875 static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2) 1876 { 1877 s32 adc_temp, comp_temp; 1878 int ret; 1879 1880 ret = bmp180_read_adc_temp(data, &adc_temp); 1881 if (ret) 1882 return ret; 1883 1884 comp_temp = bmp180_compensate_temp(data, adc_temp); 1885 1886 /* 1887 * val might be NULL if we're called by the read_press routine, 1888 * who only cares about the carry over t_fine value. 1889 */ 1890 if (val) { 1891 *val = comp_temp * 100; 1892 return IIO_VAL_INT; 1893 } 1894 1895 return 0; 1896 } 1897 1898 static int bmp180_read_adc_press(struct bmp280_data *data, int *val) 1899 { 1900 u8 oss = data->oversampling_press; 1901 int ret; 1902 1903 ret = bmp180_measure(data, 1904 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) | 1905 FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) | 1906 BMP180_MEAS_SCO); 1907 if (ret) 1908 return ret; 1909 1910 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, 1911 data->buf, sizeof(data->buf)); 1912 if (ret) 1913 return ret; 1914 1915 *val = get_unaligned_be24(data->buf) >> (8 - oss); 1916 1917 return 0; 1918 } 1919 1920 /* 1921 * Returns pressure in Pa, resolution is 1 Pa. 1922 * 1923 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". 1924 */ 1925 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press) 1926 { 1927 struct bmp180_calib *calib = &data->calib.bmp180; 1928 s32 oss = data->oversampling_press; 1929 s32 x1, x2, x3, p; 1930 s32 b3, b6; 1931 u32 b4, b7; 1932 1933 b6 = data->t_fine - 4000; 1934 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11; 1935 x2 = calib->AC2 * b6 >> 11; 1936 x3 = x1 + x2; 1937 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4; 1938 x1 = calib->AC3 * b6 >> 13; 1939 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16; 1940 x3 = (x1 + x2 + 2) >> 2; 1941 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15; 1942 b7 = ((u32)adc_press - b3) * (50000 >> oss); 1943 if (b7 < 0x80000000) 1944 p = (b7 * 2) / b4; 1945 else 1946 p = (b7 / b4) * 2; 1947 1948 x1 = (p >> 8) * (p >> 8); 1949 x1 = (x1 * 3038) >> 16; 1950 x2 = (-7357 * p) >> 16; 1951 1952 return p + ((x1 + x2 + 3791) >> 4); 1953 } 1954 1955 static int bmp180_read_press(struct bmp280_data *data, 1956 int *val, int *val2) 1957 { 1958 u32 comp_press; 1959 s32 adc_press; 1960 int ret; 1961 1962 /* Read and compensate temperature so we get a reading of t_fine. */ 1963 ret = bmp180_read_temp(data, NULL, NULL); 1964 if (ret) 1965 return ret; 1966 1967 ret = bmp180_read_adc_press(data, &adc_press); 1968 if (ret) 1969 return ret; 1970 1971 comp_press = bmp180_compensate_press(data, adc_press); 1972 1973 *val = comp_press; 1974 *val2 = 1000; 1975 1976 return IIO_VAL_FRACTIONAL; 1977 } 1978 1979 static int bmp180_chip_config(struct bmp280_data *data) 1980 { 1981 return 0; 1982 } 1983 1984 static const int bmp180_oversampling_temp_avail[] = { 1 }; 1985 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; 1986 1987 const struct bmp280_chip_info bmp180_chip_info = { 1988 .id_reg = BMP280_REG_ID, 1989 .chip_id = BMP180_CHIP_ID, 1990 .regmap_config = &bmp180_regmap_config, 1991 .start_up_time = 2000, 1992 .channels = bmp280_channels, 1993 .num_channels = 2, 1994 1995 .oversampling_temp_avail = bmp180_oversampling_temp_avail, 1996 .num_oversampling_temp_avail = 1997 ARRAY_SIZE(bmp180_oversampling_temp_avail), 1998 .oversampling_temp_default = 0, 1999 2000 .oversampling_press_avail = bmp180_oversampling_press_avail, 2001 .num_oversampling_press_avail = 2002 ARRAY_SIZE(bmp180_oversampling_press_avail), 2003 .oversampling_press_default = BMP180_MEAS_PRESS_8X, 2004 2005 .chip_config = bmp180_chip_config, 2006 .read_temp = bmp180_read_temp, 2007 .read_press = bmp180_read_press, 2008 .read_calib = bmp180_read_calib, 2009 }; 2010 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280); 2011 2012 static irqreturn_t bmp085_eoc_irq(int irq, void *d) 2013 { 2014 struct bmp280_data *data = d; 2015 2016 complete(&data->done); 2017 2018 return IRQ_HANDLED; 2019 } 2020 2021 static int bmp085_fetch_eoc_irq(struct device *dev, 2022 const char *name, 2023 int irq, 2024 struct bmp280_data *data) 2025 { 2026 unsigned long irq_trig; 2027 int ret; 2028 2029 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq)); 2030 if (irq_trig != IRQF_TRIGGER_RISING) { 2031 dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n"); 2032 irq_trig = IRQF_TRIGGER_RISING; 2033 } 2034 2035 init_completion(&data->done); 2036 2037 ret = devm_request_threaded_irq(dev, 2038 irq, 2039 bmp085_eoc_irq, 2040 NULL, 2041 irq_trig, 2042 name, 2043 data); 2044 if (ret) { 2045 /* Bail out without IRQ but keep the driver in place */ 2046 dev_err(dev, "unable to request DRDY IRQ\n"); 2047 return 0; 2048 } 2049 2050 data->use_eoc = true; 2051 return 0; 2052 } 2053 2054 static void bmp280_pm_disable(void *data) 2055 { 2056 struct device *dev = data; 2057 2058 pm_runtime_get_sync(dev); 2059 pm_runtime_put_noidle(dev); 2060 pm_runtime_disable(dev); 2061 } 2062 2063 static void bmp280_regulators_disable(void *data) 2064 { 2065 struct regulator_bulk_data *supplies = data; 2066 2067 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies); 2068 } 2069 2070 int bmp280_common_probe(struct device *dev, 2071 struct regmap *regmap, 2072 const struct bmp280_chip_info *chip_info, 2073 const char *name, 2074 int irq) 2075 { 2076 struct iio_dev *indio_dev; 2077 struct bmp280_data *data; 2078 struct gpio_desc *gpiod; 2079 unsigned int chip_id; 2080 int ret; 2081 2082 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 2083 if (!indio_dev) 2084 return -ENOMEM; 2085 2086 data = iio_priv(indio_dev); 2087 mutex_init(&data->lock); 2088 data->dev = dev; 2089 2090 indio_dev->name = name; 2091 indio_dev->info = &bmp280_info; 2092 indio_dev->modes = INDIO_DIRECT_MODE; 2093 2094 data->chip_info = chip_info; 2095 2096 /* Apply initial values from chip info structure */ 2097 indio_dev->channels = chip_info->channels; 2098 indio_dev->num_channels = chip_info->num_channels; 2099 data->oversampling_press = chip_info->oversampling_press_default; 2100 data->oversampling_humid = chip_info->oversampling_humid_default; 2101 data->oversampling_temp = chip_info->oversampling_temp_default; 2102 data->iir_filter_coeff = chip_info->iir_filter_coeff_default; 2103 data->sampling_freq = chip_info->sampling_freq_default; 2104 data->start_up_time = chip_info->start_up_time; 2105 2106 /* Bring up regulators */ 2107 regulator_bulk_set_supply_names(data->supplies, 2108 bmp280_supply_names, 2109 BMP280_NUM_SUPPLIES); 2110 2111 ret = devm_regulator_bulk_get(dev, 2112 BMP280_NUM_SUPPLIES, data->supplies); 2113 if (ret) { 2114 dev_err(dev, "failed to get regulators\n"); 2115 return ret; 2116 } 2117 2118 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies); 2119 if (ret) { 2120 dev_err(dev, "failed to enable regulators\n"); 2121 return ret; 2122 } 2123 2124 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable, 2125 data->supplies); 2126 if (ret) 2127 return ret; 2128 2129 /* Wait to make sure we started up properly */ 2130 usleep_range(data->start_up_time, data->start_up_time + 100); 2131 2132 /* Bring chip out of reset if there is an assigned GPIO line */ 2133 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 2134 /* Deassert the signal */ 2135 if (gpiod) { 2136 dev_info(dev, "release reset\n"); 2137 gpiod_set_value(gpiod, 0); 2138 } 2139 2140 data->regmap = regmap; 2141 2142 ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id); 2143 if (ret < 0) 2144 return ret; 2145 if (chip_id != data->chip_info->chip_id) { 2146 dev_err(dev, "bad chip id: expected %x got %x\n", 2147 data->chip_info->chip_id, chip_id); 2148 return -EINVAL; 2149 } 2150 2151 if (data->chip_info->preinit) { 2152 ret = data->chip_info->preinit(data); 2153 if (ret) 2154 return dev_err_probe(data->dev, ret, 2155 "error running preinit tasks\n"); 2156 } 2157 2158 ret = data->chip_info->chip_config(data); 2159 if (ret < 0) 2160 return ret; 2161 2162 dev_set_drvdata(dev, indio_dev); 2163 2164 /* 2165 * Some chips have calibration parameters "programmed into the devices' 2166 * non-volatile memory during production". Let's read them out at probe 2167 * time once. They will not change. 2168 */ 2169 2170 if (data->chip_info->read_calib) { 2171 ret = data->chip_info->read_calib(data); 2172 if (ret < 0) 2173 return dev_err_probe(data->dev, ret, 2174 "failed to read calibration coefficients\n"); 2175 } 2176 2177 /* 2178 * Attempt to grab an optional EOC IRQ - only the BMP085 has this 2179 * however as it happens, the BMP085 shares the chip ID of BMP180 2180 * so we look for an IRQ if we have that. 2181 */ 2182 if (irq > 0 && (chip_id == BMP180_CHIP_ID)) { 2183 ret = bmp085_fetch_eoc_irq(dev, name, irq, data); 2184 if (ret) 2185 return ret; 2186 } 2187 2188 /* Enable runtime PM */ 2189 pm_runtime_get_noresume(dev); 2190 pm_runtime_set_active(dev); 2191 pm_runtime_enable(dev); 2192 /* 2193 * Set autosuspend to two orders of magnitude larger than the 2194 * start-up time. 2195 */ 2196 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10); 2197 pm_runtime_use_autosuspend(dev); 2198 pm_runtime_put(dev); 2199 2200 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev); 2201 if (ret) 2202 return ret; 2203 2204 return devm_iio_device_register(dev, indio_dev); 2205 } 2206 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280); 2207 2208 static int bmp280_runtime_suspend(struct device *dev) 2209 { 2210 struct iio_dev *indio_dev = dev_get_drvdata(dev); 2211 struct bmp280_data *data = iio_priv(indio_dev); 2212 2213 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies); 2214 } 2215 2216 static int bmp280_runtime_resume(struct device *dev) 2217 { 2218 struct iio_dev *indio_dev = dev_get_drvdata(dev); 2219 struct bmp280_data *data = iio_priv(indio_dev); 2220 int ret; 2221 2222 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies); 2223 if (ret) 2224 return ret; 2225 usleep_range(data->start_up_time, data->start_up_time + 100); 2226 return data->chip_info->chip_config(data); 2227 } 2228 2229 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend, 2230 bmp280_runtime_resume, NULL); 2231 2232 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 2233 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor"); 2234 MODULE_LICENSE("GPL v2"); 2235