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