1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Bosch BME680 - Temperature, Pressure, Humidity & Gas Sensor 4 * 5 * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH 6 * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com> 7 * 8 * Datasheet: 9 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME680-DS001-00.pdf 10 */ 11 #include <linux/acpi.h> 12 #include <linux/bitfield.h> 13 #include <linux/device.h> 14 #include <linux/module.h> 15 #include <linux/log2.h> 16 #include <linux/regmap.h> 17 #include <linux/iio/iio.h> 18 #include <linux/iio/sysfs.h> 19 20 #include "bme680.h" 21 22 struct bme680_calib { 23 u16 par_t1; 24 s16 par_t2; 25 s8 par_t3; 26 u16 par_p1; 27 s16 par_p2; 28 s8 par_p3; 29 s16 par_p4; 30 s16 par_p5; 31 s8 par_p6; 32 s8 par_p7; 33 s16 par_p8; 34 s16 par_p9; 35 u8 par_p10; 36 u16 par_h1; 37 u16 par_h2; 38 s8 par_h3; 39 s8 par_h4; 40 s8 par_h5; 41 s8 par_h6; 42 s8 par_h7; 43 s8 par_gh1; 44 s16 par_gh2; 45 s8 par_gh3; 46 u8 res_heat_range; 47 s8 res_heat_val; 48 s8 range_sw_err; 49 }; 50 51 struct bme680_data { 52 struct regmap *regmap; 53 struct bme680_calib bme680; 54 u8 oversampling_temp; 55 u8 oversampling_press; 56 u8 oversampling_humid; 57 u16 heater_dur; 58 u16 heater_temp; 59 /* 60 * Carryover value from temperature conversion, used in pressure 61 * and humidity compensation calculations. 62 */ 63 s32 t_fine; 64 }; 65 66 static const struct regmap_range bme680_volatile_ranges[] = { 67 regmap_reg_range(BME680_REG_MEAS_STAT_0, BME680_REG_GAS_R_LSB), 68 regmap_reg_range(BME680_REG_STATUS, BME680_REG_STATUS), 69 regmap_reg_range(BME680_T2_LSB_REG, BME680_GH3_REG), 70 }; 71 72 static const struct regmap_access_table bme680_volatile_table = { 73 .yes_ranges = bme680_volatile_ranges, 74 .n_yes_ranges = ARRAY_SIZE(bme680_volatile_ranges), 75 }; 76 77 const struct regmap_config bme680_regmap_config = { 78 .reg_bits = 8, 79 .val_bits = 8, 80 .max_register = 0xef, 81 .volatile_table = &bme680_volatile_table, 82 .cache_type = REGCACHE_RBTREE, 83 }; 84 EXPORT_SYMBOL(bme680_regmap_config); 85 86 static const struct iio_chan_spec bme680_channels[] = { 87 { 88 .type = IIO_TEMP, 89 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 90 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 91 }, 92 { 93 .type = IIO_PRESSURE, 94 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 95 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 96 }, 97 { 98 .type = IIO_HUMIDITYRELATIVE, 99 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 100 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 101 }, 102 { 103 .type = IIO_RESISTANCE, 104 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 105 }, 106 }; 107 108 static int bme680_read_calib(struct bme680_data *data, 109 struct bme680_calib *calib) 110 { 111 struct device *dev = regmap_get_device(data->regmap); 112 unsigned int tmp, tmp_msb, tmp_lsb; 113 int ret; 114 __le16 buf; 115 116 /* Temperature related coefficients */ 117 ret = regmap_bulk_read(data->regmap, BME680_T1_LSB_REG, (u8 *) &buf, 2); 118 if (ret < 0) { 119 dev_err(dev, "failed to read BME680_T1_LSB_REG\n"); 120 return ret; 121 } 122 calib->par_t1 = le16_to_cpu(buf); 123 124 ret = regmap_bulk_read(data->regmap, BME680_T2_LSB_REG, (u8 *) &buf, 2); 125 if (ret < 0) { 126 dev_err(dev, "failed to read BME680_T2_LSB_REG\n"); 127 return ret; 128 } 129 calib->par_t2 = le16_to_cpu(buf); 130 131 ret = regmap_read(data->regmap, BME680_T3_REG, &tmp); 132 if (ret < 0) { 133 dev_err(dev, "failed to read BME680_T3_REG\n"); 134 return ret; 135 } 136 calib->par_t3 = tmp; 137 138 /* Pressure related coefficients */ 139 ret = regmap_bulk_read(data->regmap, BME680_P1_LSB_REG, (u8 *) &buf, 2); 140 if (ret < 0) { 141 dev_err(dev, "failed to read BME680_P1_LSB_REG\n"); 142 return ret; 143 } 144 calib->par_p1 = le16_to_cpu(buf); 145 146 ret = regmap_bulk_read(data->regmap, BME680_P2_LSB_REG, (u8 *) &buf, 2); 147 if (ret < 0) { 148 dev_err(dev, "failed to read BME680_P2_LSB_REG\n"); 149 return ret; 150 } 151 calib->par_p2 = le16_to_cpu(buf); 152 153 ret = regmap_read(data->regmap, BME680_P3_REG, &tmp); 154 if (ret < 0) { 155 dev_err(dev, "failed to read BME680_P3_REG\n"); 156 return ret; 157 } 158 calib->par_p3 = tmp; 159 160 ret = regmap_bulk_read(data->regmap, BME680_P4_LSB_REG, (u8 *) &buf, 2); 161 if (ret < 0) { 162 dev_err(dev, "failed to read BME680_P4_LSB_REG\n"); 163 return ret; 164 } 165 calib->par_p4 = le16_to_cpu(buf); 166 167 ret = regmap_bulk_read(data->regmap, BME680_P5_LSB_REG, (u8 *) &buf, 2); 168 if (ret < 0) { 169 dev_err(dev, "failed to read BME680_P5_LSB_REG\n"); 170 return ret; 171 } 172 calib->par_p5 = le16_to_cpu(buf); 173 174 ret = regmap_read(data->regmap, BME680_P6_REG, &tmp); 175 if (ret < 0) { 176 dev_err(dev, "failed to read BME680_P6_REG\n"); 177 return ret; 178 } 179 calib->par_p6 = tmp; 180 181 ret = regmap_read(data->regmap, BME680_P7_REG, &tmp); 182 if (ret < 0) { 183 dev_err(dev, "failed to read BME680_P7_REG\n"); 184 return ret; 185 } 186 calib->par_p7 = tmp; 187 188 ret = regmap_bulk_read(data->regmap, BME680_P8_LSB_REG, (u8 *) &buf, 2); 189 if (ret < 0) { 190 dev_err(dev, "failed to read BME680_P8_LSB_REG\n"); 191 return ret; 192 } 193 calib->par_p8 = le16_to_cpu(buf); 194 195 ret = regmap_bulk_read(data->regmap, BME680_P9_LSB_REG, (u8 *) &buf, 2); 196 if (ret < 0) { 197 dev_err(dev, "failed to read BME680_P9_LSB_REG\n"); 198 return ret; 199 } 200 calib->par_p9 = le16_to_cpu(buf); 201 202 ret = regmap_read(data->regmap, BME680_P10_REG, &tmp); 203 if (ret < 0) { 204 dev_err(dev, "failed to read BME680_P10_REG\n"); 205 return ret; 206 } 207 calib->par_p10 = tmp; 208 209 /* Humidity related coefficients */ 210 ret = regmap_read(data->regmap, BME680_H1_MSB_REG, &tmp_msb); 211 if (ret < 0) { 212 dev_err(dev, "failed to read BME680_H1_MSB_REG\n"); 213 return ret; 214 } 215 ret = regmap_read(data->regmap, BME680_H1_LSB_REG, &tmp_lsb); 216 if (ret < 0) { 217 dev_err(dev, "failed to read BME680_H1_LSB_REG\n"); 218 return ret; 219 } 220 calib->par_h1 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) | 221 (tmp_lsb & BME680_BIT_H1_DATA_MASK); 222 223 ret = regmap_read(data->regmap, BME680_H2_MSB_REG, &tmp_msb); 224 if (ret < 0) { 225 dev_err(dev, "failed to read BME680_H2_MSB_REG\n"); 226 return ret; 227 } 228 ret = regmap_read(data->regmap, BME680_H2_LSB_REG, &tmp_lsb); 229 if (ret < 0) { 230 dev_err(dev, "failed to read BME680_H2_LSB_REG\n"); 231 return ret; 232 } 233 calib->par_h2 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) | 234 (tmp_lsb >> BME680_HUM_REG_SHIFT_VAL); 235 236 ret = regmap_read(data->regmap, BME680_H3_REG, &tmp); 237 if (ret < 0) { 238 dev_err(dev, "failed to read BME680_H3_REG\n"); 239 return ret; 240 } 241 calib->par_h3 = tmp; 242 243 ret = regmap_read(data->regmap, BME680_H4_REG, &tmp); 244 if (ret < 0) { 245 dev_err(dev, "failed to read BME680_H4_REG\n"); 246 return ret; 247 } 248 calib->par_h4 = tmp; 249 250 ret = regmap_read(data->regmap, BME680_H5_REG, &tmp); 251 if (ret < 0) { 252 dev_err(dev, "failed to read BME680_H5_REG\n"); 253 return ret; 254 } 255 calib->par_h5 = tmp; 256 257 ret = regmap_read(data->regmap, BME680_H6_REG, &tmp); 258 if (ret < 0) { 259 dev_err(dev, "failed to read BME680_H6_REG\n"); 260 return ret; 261 } 262 calib->par_h6 = tmp; 263 264 ret = regmap_read(data->regmap, BME680_H7_REG, &tmp); 265 if (ret < 0) { 266 dev_err(dev, "failed to read BME680_H7_REG\n"); 267 return ret; 268 } 269 calib->par_h7 = tmp; 270 271 /* Gas heater related coefficients */ 272 ret = regmap_read(data->regmap, BME680_GH1_REG, &tmp); 273 if (ret < 0) { 274 dev_err(dev, "failed to read BME680_GH1_REG\n"); 275 return ret; 276 } 277 calib->par_gh1 = tmp; 278 279 ret = regmap_bulk_read(data->regmap, BME680_GH2_LSB_REG, (u8 *) &buf, 280 2); 281 if (ret < 0) { 282 dev_err(dev, "failed to read BME680_GH2_LSB_REG\n"); 283 return ret; 284 } 285 calib->par_gh2 = le16_to_cpu(buf); 286 287 ret = regmap_read(data->regmap, BME680_GH3_REG, &tmp); 288 if (ret < 0) { 289 dev_err(dev, "failed to read BME680_GH3_REG\n"); 290 return ret; 291 } 292 calib->par_gh3 = tmp; 293 294 /* Other coefficients */ 295 ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_RANGE, &tmp); 296 if (ret < 0) { 297 dev_err(dev, "failed to read resistance heat range\n"); 298 return ret; 299 } 300 calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK, tmp); 301 302 ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_VAL, &tmp); 303 if (ret < 0) { 304 dev_err(dev, "failed to read resistance heat value\n"); 305 return ret; 306 } 307 calib->res_heat_val = tmp; 308 309 ret = regmap_read(data->regmap, BME680_REG_RANGE_SW_ERR, &tmp); 310 if (ret < 0) { 311 dev_err(dev, "failed to read range software error\n"); 312 return ret; 313 } 314 calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK, tmp); 315 316 return 0; 317 } 318 319 /* 320 * Taken from Bosch BME680 API: 321 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L876 322 * 323 * Returns temperature measurement in DegC, resolutions is 0.01 DegC. Therefore, 324 * output value of "3233" represents 32.33 DegC. 325 */ 326 static s16 bme680_compensate_temp(struct bme680_data *data, 327 s32 adc_temp) 328 { 329 struct bme680_calib *calib = &data->bme680; 330 s64 var1, var2, var3; 331 s16 calc_temp; 332 333 /* If the calibration is invalid, attempt to reload it */ 334 if (!calib->par_t2) 335 bme680_read_calib(data, calib); 336 337 var1 = (adc_temp >> 3) - (calib->par_t1 << 1); 338 var2 = (var1 * calib->par_t2) >> 11; 339 var3 = ((var1 >> 1) * (var1 >> 1)) >> 12; 340 var3 = (var3 * (calib->par_t3 << 4)) >> 14; 341 data->t_fine = var2 + var3; 342 calc_temp = (data->t_fine * 5 + 128) >> 8; 343 344 return calc_temp; 345 } 346 347 /* 348 * Taken from Bosch BME680 API: 349 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L896 350 * 351 * Returns pressure measurement in Pa. Output value of "97356" represents 352 * 97356 Pa = 973.56 hPa. 353 */ 354 static u32 bme680_compensate_press(struct bme680_data *data, 355 u32 adc_press) 356 { 357 struct bme680_calib *calib = &data->bme680; 358 s32 var1, var2, var3, press_comp; 359 360 var1 = (data->t_fine >> 1) - 64000; 361 var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * calib->par_p6) >> 2; 362 var2 = var2 + (var1 * calib->par_p5 << 1); 363 var2 = (var2 >> 2) + (calib->par_p4 << 16); 364 var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) * 365 (calib->par_p3 << 5)) >> 3) + 366 ((calib->par_p2 * var1) >> 1); 367 var1 = var1 >> 18; 368 var1 = ((32768 + var1) * calib->par_p1) >> 15; 369 press_comp = 1048576 - adc_press; 370 press_comp = ((press_comp - (var2 >> 12)) * 3125); 371 372 if (press_comp >= BME680_MAX_OVERFLOW_VAL) 373 press_comp = ((press_comp / (u32)var1) << 1); 374 else 375 press_comp = ((press_comp << 1) / (u32)var1); 376 377 var1 = (calib->par_p9 * (((press_comp >> 3) * 378 (press_comp >> 3)) >> 13)) >> 12; 379 var2 = ((press_comp >> 2) * calib->par_p8) >> 13; 380 var3 = ((press_comp >> 8) * (press_comp >> 8) * 381 (press_comp >> 8) * calib->par_p10) >> 17; 382 383 press_comp += (var1 + var2 + var3 + (calib->par_p7 << 7)) >> 4; 384 385 return press_comp; 386 } 387 388 /* 389 * Taken from Bosch BME680 API: 390 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L937 391 * 392 * Returns humidity measurement in percent, resolution is 0.001 percent. Output 393 * value of "43215" represents 43.215 %rH. 394 */ 395 static u32 bme680_compensate_humid(struct bme680_data *data, 396 u16 adc_humid) 397 { 398 struct bme680_calib *calib = &data->bme680; 399 s32 var1, var2, var3, var4, var5, var6, temp_scaled, calc_hum; 400 401 temp_scaled = (data->t_fine * 5 + 128) >> 8; 402 var1 = (adc_humid - ((s32) ((s32) calib->par_h1 * 16))) - 403 (((temp_scaled * (s32) calib->par_h3) / 100) >> 1); 404 var2 = ((s32) calib->par_h2 * 405 (((temp_scaled * calib->par_h4) / 100) + 406 (((temp_scaled * ((temp_scaled * calib->par_h5) / 100)) 407 >> 6) / 100) + (1 << 14))) >> 10; 408 var3 = var1 * var2; 409 var4 = calib->par_h6 << 7; 410 var4 = (var4 + ((temp_scaled * calib->par_h7) / 100)) >> 4; 411 var5 = ((var3 >> 14) * (var3 >> 14)) >> 10; 412 var6 = (var4 * var5) >> 1; 413 calc_hum = (((var3 + var6) >> 10) * 1000) >> 12; 414 415 calc_hum = clamp(calc_hum, 0, 100000); /* clamp between 0-100 %rH */ 416 417 return calc_hum; 418 } 419 420 /* 421 * Taken from Bosch BME680 API: 422 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L973 423 * 424 * Returns gas measurement in Ohm. Output value of "82986" represent 82986 ohms. 425 */ 426 static u32 bme680_compensate_gas(struct bme680_data *data, u16 gas_res_adc, 427 u8 gas_range) 428 { 429 struct bme680_calib *calib = &data->bme680; 430 s64 var1; 431 u64 var2; 432 s64 var3; 433 u32 calc_gas_res; 434 435 /* Look up table for the possible gas range values */ 436 const u32 lookupTable[16] = {2147483647u, 2147483647u, 437 2147483647u, 2147483647u, 2147483647u, 438 2126008810u, 2147483647u, 2130303777u, 439 2147483647u, 2147483647u, 2143188679u, 440 2136746228u, 2147483647u, 2126008810u, 441 2147483647u, 2147483647u}; 442 443 var1 = ((1340 + (5 * (s64) calib->range_sw_err)) * 444 ((s64) lookupTable[gas_range])) >> 16; 445 var2 = ((gas_res_adc << 15) - 16777216) + var1; 446 var3 = ((125000 << (15 - gas_range)) * var1) >> 9; 447 var3 += (var2 >> 1); 448 calc_gas_res = div64_s64(var3, (s64) var2); 449 450 return calc_gas_res; 451 } 452 453 /* 454 * Taken from Bosch BME680 API: 455 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1002 456 */ 457 static u8 bme680_calc_heater_res(struct bme680_data *data, u16 temp) 458 { 459 struct bme680_calib *calib = &data->bme680; 460 s32 var1, var2, var3, var4, var5, heatr_res_x100; 461 u8 heatr_res; 462 463 if (temp > 400) /* Cap temperature */ 464 temp = 400; 465 466 var1 = (((s32) BME680_AMB_TEMP * calib->par_gh3) / 1000) * 256; 467 var2 = (calib->par_gh1 + 784) * (((((calib->par_gh2 + 154009) * 468 temp * 5) / 100) 469 + 3276800) / 10); 470 var3 = var1 + (var2 / 2); 471 var4 = (var3 / (calib->res_heat_range + 4)); 472 var5 = 131 * calib->res_heat_val + 65536; 473 heatr_res_x100 = ((var4 / var5) - 250) * 34; 474 heatr_res = (heatr_res_x100 + 50) / 100; 475 476 return heatr_res; 477 } 478 479 /* 480 * Taken from Bosch BME680 API: 481 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1188 482 */ 483 static u8 bme680_calc_heater_dur(u16 dur) 484 { 485 u8 durval, factor = 0; 486 487 if (dur >= 0xfc0) { 488 durval = 0xff; /* Max duration */ 489 } else { 490 while (dur > 0x3F) { 491 dur = dur / 4; 492 factor += 1; 493 } 494 durval = dur + (factor * 64); 495 } 496 497 return durval; 498 } 499 500 static int bme680_set_mode(struct bme680_data *data, bool mode) 501 { 502 struct device *dev = regmap_get_device(data->regmap); 503 int ret; 504 505 if (mode) { 506 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS, 507 BME680_MODE_MASK, BME680_MODE_FORCED); 508 if (ret < 0) 509 dev_err(dev, "failed to set forced mode\n"); 510 511 } else { 512 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS, 513 BME680_MODE_MASK, BME680_MODE_SLEEP); 514 if (ret < 0) 515 dev_err(dev, "failed to set sleep mode\n"); 516 517 } 518 519 return ret; 520 } 521 522 static u8 bme680_oversampling_to_reg(u8 val) 523 { 524 return ilog2(val) + 1; 525 } 526 527 static int bme680_chip_config(struct bme680_data *data) 528 { 529 struct device *dev = regmap_get_device(data->regmap); 530 int ret; 531 u8 osrs; 532 533 osrs = FIELD_PREP( 534 BME680_OSRS_HUMIDITY_MASK, 535 bme680_oversampling_to_reg(data->oversampling_humid)); 536 /* 537 * Highly recommended to set oversampling of humidity before 538 * temperature/pressure oversampling. 539 */ 540 ret = regmap_update_bits(data->regmap, BME680_REG_CTRL_HUMIDITY, 541 BME680_OSRS_HUMIDITY_MASK, osrs); 542 if (ret < 0) { 543 dev_err(dev, "failed to write ctrl_hum register\n"); 544 return ret; 545 } 546 547 /* IIR filter settings */ 548 ret = regmap_update_bits(data->regmap, BME680_REG_CONFIG, 549 BME680_FILTER_MASK, 550 BME680_FILTER_COEFF_VAL); 551 if (ret < 0) { 552 dev_err(dev, "failed to write config register\n"); 553 return ret; 554 } 555 556 osrs = FIELD_PREP(BME680_OSRS_TEMP_MASK, 557 bme680_oversampling_to_reg(data->oversampling_temp)) | 558 FIELD_PREP(BME680_OSRS_PRESS_MASK, 559 bme680_oversampling_to_reg(data->oversampling_press)); 560 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS, 561 BME680_OSRS_TEMP_MASK | BME680_OSRS_PRESS_MASK, 562 osrs); 563 if (ret < 0) 564 dev_err(dev, "failed to write ctrl_meas register\n"); 565 566 return ret; 567 } 568 569 static int bme680_gas_config(struct bme680_data *data) 570 { 571 struct device *dev = regmap_get_device(data->regmap); 572 int ret; 573 u8 heatr_res, heatr_dur; 574 575 heatr_res = bme680_calc_heater_res(data, data->heater_temp); 576 577 /* set target heater temperature */ 578 ret = regmap_write(data->regmap, BME680_REG_RES_HEAT_0, heatr_res); 579 if (ret < 0) { 580 dev_err(dev, "failed to write res_heat_0 register\n"); 581 return ret; 582 } 583 584 heatr_dur = bme680_calc_heater_dur(data->heater_dur); 585 586 /* set target heating duration */ 587 ret = regmap_write(data->regmap, BME680_REG_GAS_WAIT_0, heatr_dur); 588 if (ret < 0) { 589 dev_err(dev, "failed to write gas_wait_0 register\n"); 590 return ret; 591 } 592 593 /* Enable the gas sensor and select heater profile set-point 0 */ 594 ret = regmap_update_bits(data->regmap, BME680_REG_CTRL_GAS_1, 595 BME680_RUN_GAS_MASK | BME680_NB_CONV_MASK, 596 FIELD_PREP(BME680_RUN_GAS_MASK, 1) | 597 FIELD_PREP(BME680_NB_CONV_MASK, 0)); 598 if (ret < 0) 599 dev_err(dev, "failed to write ctrl_gas_1 register\n"); 600 601 return ret; 602 } 603 604 static int bme680_read_temp(struct bme680_data *data, int *val) 605 { 606 struct device *dev = regmap_get_device(data->regmap); 607 int ret; 608 __be32 tmp = 0; 609 s32 adc_temp; 610 s16 comp_temp; 611 612 /* set forced mode to trigger measurement */ 613 ret = bme680_set_mode(data, true); 614 if (ret < 0) 615 return ret; 616 617 ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB, 618 (u8 *) &tmp, 3); 619 if (ret < 0) { 620 dev_err(dev, "failed to read temperature\n"); 621 return ret; 622 } 623 624 adc_temp = be32_to_cpu(tmp) >> 12; 625 if (adc_temp == BME680_MEAS_SKIPPED) { 626 /* reading was skipped */ 627 dev_err(dev, "reading temperature skipped\n"); 628 return -EINVAL; 629 } 630 comp_temp = bme680_compensate_temp(data, adc_temp); 631 /* 632 * val might be NULL if we're called by the read_press/read_humid 633 * routine which is callled to get t_fine value used in 634 * compensate_press/compensate_humid to get compensated 635 * pressure/humidity readings. 636 */ 637 if (val) { 638 *val = comp_temp * 10; /* Centidegrees to millidegrees */ 639 return IIO_VAL_INT; 640 } 641 642 return ret; 643 } 644 645 static int bme680_read_press(struct bme680_data *data, 646 int *val, int *val2) 647 { 648 struct device *dev = regmap_get_device(data->regmap); 649 int ret; 650 __be32 tmp = 0; 651 s32 adc_press; 652 653 /* Read and compensate temperature to get a reading of t_fine */ 654 ret = bme680_read_temp(data, NULL); 655 if (ret < 0) 656 return ret; 657 658 ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB, 659 (u8 *) &tmp, 3); 660 if (ret < 0) { 661 dev_err(dev, "failed to read pressure\n"); 662 return ret; 663 } 664 665 adc_press = be32_to_cpu(tmp) >> 12; 666 if (adc_press == BME680_MEAS_SKIPPED) { 667 /* reading was skipped */ 668 dev_err(dev, "reading pressure skipped\n"); 669 return -EINVAL; 670 } 671 672 *val = bme680_compensate_press(data, adc_press); 673 *val2 = 100; 674 return IIO_VAL_FRACTIONAL; 675 } 676 677 static int bme680_read_humid(struct bme680_data *data, 678 int *val, int *val2) 679 { 680 struct device *dev = regmap_get_device(data->regmap); 681 int ret; 682 __be16 tmp = 0; 683 s32 adc_humidity; 684 u32 comp_humidity; 685 686 /* Read and compensate temperature to get a reading of t_fine */ 687 ret = bme680_read_temp(data, NULL); 688 if (ret < 0) 689 return ret; 690 691 ret = regmap_bulk_read(data->regmap, BM6880_REG_HUMIDITY_MSB, 692 (u8 *) &tmp, 2); 693 if (ret < 0) { 694 dev_err(dev, "failed to read humidity\n"); 695 return ret; 696 } 697 698 adc_humidity = be16_to_cpu(tmp); 699 if (adc_humidity == BME680_MEAS_SKIPPED) { 700 /* reading was skipped */ 701 dev_err(dev, "reading humidity skipped\n"); 702 return -EINVAL; 703 } 704 comp_humidity = bme680_compensate_humid(data, adc_humidity); 705 706 *val = comp_humidity; 707 *val2 = 1000; 708 return IIO_VAL_FRACTIONAL; 709 } 710 711 static int bme680_read_gas(struct bme680_data *data, 712 int *val) 713 { 714 struct device *dev = regmap_get_device(data->regmap); 715 int ret; 716 __be16 tmp = 0; 717 unsigned int check; 718 u16 adc_gas_res; 719 u8 gas_range; 720 721 /* Set heater settings */ 722 ret = bme680_gas_config(data); 723 if (ret < 0) { 724 dev_err(dev, "failed to set gas config\n"); 725 return ret; 726 } 727 728 /* set forced mode to trigger measurement */ 729 ret = bme680_set_mode(data, true); 730 if (ret < 0) 731 return ret; 732 733 ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check); 734 if (check & BME680_GAS_MEAS_BIT) { 735 dev_err(dev, "gas measurement incomplete\n"); 736 return -EBUSY; 737 } 738 739 ret = regmap_read(data->regmap, BME680_REG_GAS_R_LSB, &check); 740 if (ret < 0) { 741 dev_err(dev, "failed to read gas_r_lsb register\n"); 742 return ret; 743 } 744 745 /* 746 * occurs if either the gas heating duration was insuffient 747 * to reach the target heater temperature or the target 748 * heater temperature was too high for the heater sink to 749 * reach. 750 */ 751 if ((check & BME680_GAS_STAB_BIT) == 0) { 752 dev_err(dev, "heater failed to reach the target temperature\n"); 753 return -EINVAL; 754 } 755 756 ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB, 757 (u8 *) &tmp, 2); 758 if (ret < 0) { 759 dev_err(dev, "failed to read gas resistance\n"); 760 return ret; 761 } 762 763 gas_range = check & BME680_GAS_RANGE_MASK; 764 adc_gas_res = be16_to_cpu(tmp) >> BME680_ADC_GAS_RES_SHIFT; 765 766 *val = bme680_compensate_gas(data, adc_gas_res, gas_range); 767 return IIO_VAL_INT; 768 } 769 770 static int bme680_read_raw(struct iio_dev *indio_dev, 771 struct iio_chan_spec const *chan, 772 int *val, int *val2, long mask) 773 { 774 struct bme680_data *data = iio_priv(indio_dev); 775 776 switch (mask) { 777 case IIO_CHAN_INFO_PROCESSED: 778 switch (chan->type) { 779 case IIO_TEMP: 780 return bme680_read_temp(data, val); 781 case IIO_PRESSURE: 782 return bme680_read_press(data, val, val2); 783 case IIO_HUMIDITYRELATIVE: 784 return bme680_read_humid(data, val, val2); 785 case IIO_RESISTANCE: 786 return bme680_read_gas(data, val); 787 default: 788 return -EINVAL; 789 } 790 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 791 switch (chan->type) { 792 case IIO_TEMP: 793 *val = data->oversampling_temp; 794 return IIO_VAL_INT; 795 case IIO_PRESSURE: 796 *val = data->oversampling_press; 797 return IIO_VAL_INT; 798 case IIO_HUMIDITYRELATIVE: 799 *val = data->oversampling_humid; 800 return IIO_VAL_INT; 801 default: 802 return -EINVAL; 803 } 804 default: 805 return -EINVAL; 806 } 807 } 808 809 static bool bme680_is_valid_oversampling(int rate) 810 { 811 return (rate > 0 && rate <= 16 && is_power_of_2(rate)); 812 } 813 814 static int bme680_write_raw(struct iio_dev *indio_dev, 815 struct iio_chan_spec const *chan, 816 int val, int val2, long mask) 817 { 818 struct bme680_data *data = iio_priv(indio_dev); 819 820 if (val2 != 0) 821 return -EINVAL; 822 823 switch (mask) { 824 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 825 { 826 if (!bme680_is_valid_oversampling(val)) 827 return -EINVAL; 828 829 switch (chan->type) { 830 case IIO_TEMP: 831 data->oversampling_temp = val; 832 break; 833 case IIO_PRESSURE: 834 data->oversampling_press = val; 835 break; 836 case IIO_HUMIDITYRELATIVE: 837 data->oversampling_humid = val; 838 break; 839 default: 840 return -EINVAL; 841 } 842 843 return bme680_chip_config(data); 844 } 845 default: 846 return -EINVAL; 847 } 848 } 849 850 static const char bme680_oversampling_ratio_show[] = "1 2 4 8 16"; 851 852 static IIO_CONST_ATTR(oversampling_ratio_available, 853 bme680_oversampling_ratio_show); 854 855 static struct attribute *bme680_attributes[] = { 856 &iio_const_attr_oversampling_ratio_available.dev_attr.attr, 857 NULL, 858 }; 859 860 static const struct attribute_group bme680_attribute_group = { 861 .attrs = bme680_attributes, 862 }; 863 864 static const struct iio_info bme680_info = { 865 .read_raw = &bme680_read_raw, 866 .write_raw = &bme680_write_raw, 867 .attrs = &bme680_attribute_group, 868 }; 869 870 static const char *bme680_match_acpi_device(struct device *dev) 871 { 872 const struct acpi_device_id *id; 873 874 id = acpi_match_device(dev->driver->acpi_match_table, dev); 875 if (!id) 876 return NULL; 877 878 return dev_name(dev); 879 } 880 881 int bme680_core_probe(struct device *dev, struct regmap *regmap, 882 const char *name) 883 { 884 struct iio_dev *indio_dev; 885 struct bme680_data *data; 886 unsigned int val; 887 int ret; 888 889 ret = regmap_write(regmap, BME680_REG_SOFT_RESET, 890 BME680_CMD_SOFTRESET); 891 if (ret < 0) { 892 dev_err(dev, "Failed to reset chip\n"); 893 return ret; 894 } 895 896 ret = regmap_read(regmap, BME680_REG_CHIP_ID, &val); 897 if (ret < 0) { 898 dev_err(dev, "Error reading chip ID\n"); 899 return ret; 900 } 901 902 if (val != BME680_CHIP_ID_VAL) { 903 dev_err(dev, "Wrong chip ID, got %x expected %x\n", 904 val, BME680_CHIP_ID_VAL); 905 return -ENODEV; 906 } 907 908 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 909 if (!indio_dev) 910 return -ENOMEM; 911 912 if (!name && ACPI_HANDLE(dev)) 913 name = bme680_match_acpi_device(dev); 914 915 data = iio_priv(indio_dev); 916 dev_set_drvdata(dev, indio_dev); 917 data->regmap = regmap; 918 indio_dev->dev.parent = dev; 919 indio_dev->name = name; 920 indio_dev->channels = bme680_channels; 921 indio_dev->num_channels = ARRAY_SIZE(bme680_channels); 922 indio_dev->info = &bme680_info; 923 indio_dev->modes = INDIO_DIRECT_MODE; 924 925 /* default values for the sensor */ 926 data->oversampling_humid = 2; /* 2X oversampling rate */ 927 data->oversampling_press = 4; /* 4X oversampling rate */ 928 data->oversampling_temp = 8; /* 8X oversampling rate */ 929 data->heater_temp = 320; /* degree Celsius */ 930 data->heater_dur = 150; /* milliseconds */ 931 932 ret = bme680_chip_config(data); 933 if (ret < 0) { 934 dev_err(dev, "failed to set chip_config data\n"); 935 return ret; 936 } 937 938 ret = bme680_gas_config(data); 939 if (ret < 0) { 940 dev_err(dev, "failed to set gas config data\n"); 941 return ret; 942 } 943 944 ret = bme680_read_calib(data, &data->bme680); 945 if (ret < 0) { 946 dev_err(dev, 947 "failed to read calibration coefficients at probe\n"); 948 return ret; 949 } 950 951 return devm_iio_device_register(dev, indio_dev); 952 } 953 EXPORT_SYMBOL_GPL(bme680_core_probe); 954 955 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>"); 956 MODULE_DESCRIPTION("Bosch BME680 Driver"); 957 MODULE_LICENSE("GPL v2"); 958