Lines Matching +full:ae +full:- +full:bst

1 // SPDX-License-Identifier: GPL-2.0
3 * Bosch BME680 - Temperature, Pressure, Humidity & Gas Sensor
5 * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
9 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME680-DS001-00.pdf
163 struct device *dev = regmap_get_device(data->regmap); in bme680_read_calib()
167 ret = regmap_bulk_read(data->regmap, BME680_T2_LSB_REG, in bme680_read_calib()
168 data->bme680_cal_buf_1, in bme680_read_calib()
169 sizeof(data->bme680_cal_buf_1)); in bme680_read_calib()
175 calib->par_t2 = get_unaligned_le16(&data->bme680_cal_buf_1[T2_LSB]); in bme680_read_calib()
176 calib->par_t3 = data->bme680_cal_buf_1[T3]; in bme680_read_calib()
177 calib->par_p1 = get_unaligned_le16(&data->bme680_cal_buf_1[P1_LSB]); in bme680_read_calib()
178 calib->par_p2 = get_unaligned_le16(&data->bme680_cal_buf_1[P2_LSB]); in bme680_read_calib()
179 calib->par_p3 = data->bme680_cal_buf_1[P3]; in bme680_read_calib()
180 calib->par_p4 = get_unaligned_le16(&data->bme680_cal_buf_1[P4_LSB]); in bme680_read_calib()
181 calib->par_p5 = get_unaligned_le16(&data->bme680_cal_buf_1[P5_LSB]); in bme680_read_calib()
182 calib->par_p7 = data->bme680_cal_buf_1[P7]; in bme680_read_calib()
183 calib->par_p6 = data->bme680_cal_buf_1[P6]; in bme680_read_calib()
184 calib->par_p8 = get_unaligned_le16(&data->bme680_cal_buf_1[P8_LSB]); in bme680_read_calib()
185 calib->par_p9 = get_unaligned_le16(&data->bme680_cal_buf_1[P9_LSB]); in bme680_read_calib()
186 calib->par_p10 = data->bme680_cal_buf_1[P10]; in bme680_read_calib()
188 ret = regmap_bulk_read(data->regmap, BME680_H2_MSB_REG, in bme680_read_calib()
189 data->bme680_cal_buf_2, in bme680_read_calib()
190 sizeof(data->bme680_cal_buf_2)); in bme680_read_calib()
196 tmp_lsb = data->bme680_cal_buf_2[H1_LSB]; in bme680_read_calib()
197 tmp_msb = data->bme680_cal_buf_2[H1_LSB + 1]; in bme680_read_calib()
198 calib->par_h1 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) | in bme680_read_calib()
201 tmp_msb = data->bme680_cal_buf_2[H2_MSB]; in bme680_read_calib()
202 tmp_lsb = data->bme680_cal_buf_2[H2_MSB + 1]; in bme680_read_calib()
203 calib->par_h2 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) | in bme680_read_calib()
206 calib->par_h3 = data->bme680_cal_buf_2[H3]; in bme680_read_calib()
207 calib->par_h4 = data->bme680_cal_buf_2[H4]; in bme680_read_calib()
208 calib->par_h5 = data->bme680_cal_buf_2[H5]; in bme680_read_calib()
209 calib->par_h6 = data->bme680_cal_buf_2[H6]; in bme680_read_calib()
210 calib->par_h7 = data->bme680_cal_buf_2[H7]; in bme680_read_calib()
211 calib->par_t1 = get_unaligned_le16(&data->bme680_cal_buf_2[T1_LSB]); in bme680_read_calib()
212 calib->par_gh2 = get_unaligned_le16(&data->bme680_cal_buf_2[GH2_LSB]); in bme680_read_calib()
213 calib->par_gh1 = data->bme680_cal_buf_2[GH1]; in bme680_read_calib()
214 calib->par_gh3 = data->bme680_cal_buf_2[GH3]; in bme680_read_calib()
216 ret = regmap_bulk_read(data->regmap, BME680_REG_RES_HEAT_VAL, in bme680_read_calib()
217 data->bme680_cal_buf_3, in bme680_read_calib()
218 sizeof(data->bme680_cal_buf_3)); in bme680_read_calib()
224 calib->res_heat_val = data->bme680_cal_buf_3[RES_HEAT_VAL]; in bme680_read_calib()
226 calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK, in bme680_read_calib()
227 data->bme680_cal_buf_3[RES_HEAT_RANGE]); in bme680_read_calib()
229 calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK, in bme680_read_calib()
230 data->bme680_cal_buf_3[RANGE_SW_ERR]); in bme680_read_calib()
237 struct device *dev = regmap_get_device(data->regmap); in bme680_read_temp_adc()
241 ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB, in bme680_read_temp_adc()
242 data->buf, BME680_TEMP_NUM_BYTES); in bme680_read_temp_adc()
249 get_unaligned_be24(data->buf)); in bme680_read_temp_adc()
253 return -EINVAL; in bme680_read_temp_adc()
269 struct bme680_calib *calib = &data->bme680; in bme680_calc_t_fine()
273 if (!calib->par_t2) in bme680_calc_t_fine()
276 var1 = ((s32)adc_temp >> 3) - ((s32)calib->par_t1 << 1); in bme680_calc_t_fine()
277 var2 = (var1 * calib->par_t2) >> 11; in bme680_calc_t_fine()
279 var3 = (var3 * ((s32)calib->par_t3 << 4)) >> 14; in bme680_calc_t_fine()
305 struct device *dev = regmap_get_device(data->regmap); in bme680_read_press_adc()
309 ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB, in bme680_read_press_adc()
310 data->buf, BME680_PRESS_NUM_BYTES); in bme680_read_press_adc()
317 get_unaligned_be24(data->buf)); in bme680_read_press_adc()
321 return -EINVAL; in bme680_read_press_adc()
338 struct bme680_calib *calib = &data->bme680; in bme680_compensate_press()
341 var1 = (t_fine >> 1) - 64000; in bme680_compensate_press()
342 var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * calib->par_p6) >> 2; in bme680_compensate_press()
343 var2 = var2 + (var1 * calib->par_p5 << 1); in bme680_compensate_press()
344 var2 = (var2 >> 2) + ((s32)calib->par_p4 << 16); in bme680_compensate_press()
346 ((s32)calib->par_p3 << 5)) >> 3) + in bme680_compensate_press()
347 ((calib->par_p2 * var1) >> 1); in bme680_compensate_press()
349 var1 = ((32768 + var1) * calib->par_p1) >> 15; in bme680_compensate_press()
350 press_comp = 1048576 - adc_press; in bme680_compensate_press()
351 press_comp = ((press_comp - (var2 >> 12)) * 3125); in bme680_compensate_press()
358 var1 = (calib->par_p9 * (((press_comp >> 3) * in bme680_compensate_press()
360 var2 = ((press_comp >> 2) * calib->par_p8) >> 13; in bme680_compensate_press()
362 (press_comp >> 8) * calib->par_p10) >> 17; in bme680_compensate_press()
364 press_comp += (var1 + var2 + var3 + ((s32)calib->par_p7 << 7)) >> 4; in bme680_compensate_press()
371 struct device *dev = regmap_get_device(data->regmap); in bme680_read_humid_adc()
375 ret = regmap_bulk_read(data->regmap, BME680_REG_HUMIDITY_MSB, in bme680_read_humid_adc()
376 &data->be16, BME680_HUMID_NUM_BYTES); in bme680_read_humid_adc()
382 value_humidity = be16_to_cpu(data->be16); in bme680_read_humid_adc()
386 return -EINVAL; in bme680_read_humid_adc()
403 struct bme680_calib *calib = &data->bme680; in bme680_compensate_humid()
407 var1 = (adc_humid - (((s32)calib->par_h1 * 16))) - in bme680_compensate_humid()
408 (((temp_scaled * calib->par_h3) / 100) >> 1); in bme680_compensate_humid()
409 var2 = (calib->par_h2 * in bme680_compensate_humid()
410 (((temp_scaled * calib->par_h4) / 100) + in bme680_compensate_humid()
411 (((temp_scaled * ((temp_scaled * calib->par_h5) / 100)) in bme680_compensate_humid()
414 var4 = (s32)calib->par_h6 << 7; in bme680_compensate_humid()
415 var4 = (var4 + ((temp_scaled * calib->par_h7) / 100)) >> 4; in bme680_compensate_humid()
420 calc_hum = clamp(calc_hum, 0, 100000); /* clamp between 0-100 %rH */ in bme680_compensate_humid()
434 struct bme680_calib *calib = &data->bme680; in bme680_compensate_gas()
448 var1 = ((1340 + (5 * (s64) calib->range_sw_err)) * in bme680_compensate_gas()
450 var2 = ((gas_res_adc << 15) - 16777216) + var1; in bme680_compensate_gas()
451 var3 = ((125000 << (15 - gas_range)) * var1) >> 9; in bme680_compensate_gas()
464 struct bme680_calib *calib = &data->bme680; in bme680_calc_heater_res()
471 var1 = (((s32) BME680_AMB_TEMP * calib->par_gh3) / 1000) * 256; in bme680_calc_heater_res()
472 var2 = (calib->par_gh1 + 784) * (((((calib->par_gh2 + 154009) * in bme680_calc_heater_res()
476 var4 = (var3 / (calib->res_heat_range + 4)); in bme680_calc_heater_res()
477 var5 = 131 * calib->res_heat_val + 65536; in bme680_calc_heater_res()
478 heatr_res_x100 = ((var4 / var5) - 250) * 34; in bme680_calc_heater_res()
507 struct device *dev = regmap_get_device(data->regmap); in bme680_set_mode()
511 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS, in bme680_set_mode()
517 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS, in bme680_set_mode()
538 struct device *dev = regmap_get_device(data->regmap); in bme680_wait_for_eoc()
545 int wait_eoc_us = ((data->oversampling_temp + data->oversampling_press + in bme680_wait_for_eoc()
546 data->oversampling_humid) * 1936) + (477 * 4) + in bme680_wait_for_eoc()
547 (477 * 5) + 1000 + (data->heater_dur * 1000); in bme680_wait_for_eoc()
551 ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check); in bme680_wait_for_eoc()
556 if (data->check & BME680_MEAS_BIT) { in bme680_wait_for_eoc()
558 return -EBUSY; in bme680_wait_for_eoc()
560 if (!(data->check & BME680_NEW_DATA_BIT)) { in bme680_wait_for_eoc()
562 return -ENODATA; in bme680_wait_for_eoc()
570 struct device *dev = regmap_get_device(data->regmap); in bme680_chip_config()
576 bme680_oversampling_to_reg(data->oversampling_humid)); in bme680_chip_config()
581 ret = regmap_update_bits(data->regmap, BME680_REG_CTRL_HUMIDITY, in bme680_chip_config()
589 ret = regmap_update_bits(data->regmap, BME680_REG_CONFIG, in bme680_chip_config()
598 bme680_oversampling_to_reg(data->oversampling_temp)) | in bme680_chip_config()
600 bme680_oversampling_to_reg(data->oversampling_press)); in bme680_chip_config()
601 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS, in bme680_chip_config()
614 struct device *dev = regmap_get_device(data->regmap); in bme680_gas_config()
623 heatr_res = bme680_calc_heater_res(data, data->heater_temp); in bme680_gas_config()
626 ret = regmap_write(data->regmap, BME680_REG_RES_HEAT_0, heatr_res); in bme680_gas_config()
632 heatr_dur = bme680_calc_heater_dur(data->heater_dur); in bme680_gas_config()
635 ret = regmap_write(data->regmap, BME680_REG_GAS_WAIT_0, heatr_dur); in bme680_gas_config()
641 /* Enable the gas sensor and select heater profile set-point 0 */ in bme680_gas_config()
642 ret = regmap_update_bits(data->regmap, BME680_REG_CTRL_GAS_1, in bme680_gas_config()
712 struct device *dev = regmap_get_device(data->regmap); in bme680_read_gas()
717 ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check); in bme680_read_gas()
718 if (data->check & BME680_GAS_MEAS_BIT) { in bme680_read_gas()
720 return -EBUSY; in bme680_read_gas()
723 ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB, in bme680_read_gas()
724 &data->be16, BME680_GAS_NUM_BYTES); in bme680_read_gas()
730 gas_regs_val = be16_to_cpu(data->be16); in bme680_read_gas()
741 return -EINVAL; in bme680_read_gas()
757 guard(mutex)(&data->lock); in bme680_read_raw()
770 switch (chan->type) { in bme680_read_raw()
780 return -EINVAL; in bme680_read_raw()
783 switch (chan->type) { in bme680_read_raw()
785 *val = data->oversampling_temp; in bme680_read_raw()
788 *val = data->oversampling_press; in bme680_read_raw()
791 *val = data->oversampling_humid; in bme680_read_raw()
794 return -EINVAL; in bme680_read_raw()
797 return -EINVAL; in bme680_read_raw()
812 guard(mutex)(&data->lock); in bme680_write_raw()
815 return -EINVAL; in bme680_write_raw()
821 return -EINVAL; in bme680_write_raw()
823 switch (chan->type) { in bme680_write_raw()
825 data->oversampling_temp = val; in bme680_write_raw()
828 data->oversampling_press = val; in bme680_write_raw()
831 data->oversampling_humid = val; in bme680_write_raw()
834 return -EINVAL; in bme680_write_raw()
840 return -EINVAL; in bme680_write_raw()
873 return -ENOMEM; in bme680_core_probe()
876 mutex_init(&data->lock); in bme680_core_probe()
878 data->regmap = regmap; in bme680_core_probe()
879 indio_dev->name = name; in bme680_core_probe()
880 indio_dev->channels = bme680_channels; in bme680_core_probe()
881 indio_dev->num_channels = ARRAY_SIZE(bme680_channels); in bme680_core_probe()
882 indio_dev->info = &bme680_info; in bme680_core_probe()
883 indio_dev->modes = INDIO_DIRECT_MODE; in bme680_core_probe()
886 data->oversampling_humid = 2; /* 2X oversampling rate */ in bme680_core_probe()
887 data->oversampling_press = 4; /* 4X oversampling rate */ in bme680_core_probe()
888 data->oversampling_temp = 8; /* 8X oversampling rate */ in bme680_core_probe()
889 data->heater_temp = 320; /* degree Celsius */ in bme680_core_probe()
890 data->heater_dur = 150; /* milliseconds */ in bme680_core_probe()
899 ret = regmap_read(regmap, BME680_REG_CHIP_ID, &data->check); in bme680_core_probe()
903 if (data->check != BME680_CHIP_ID_VAL) { in bme680_core_probe()
905 data->check, BME680_CHIP_ID_VAL); in bme680_core_probe()
906 return -ENODEV; in bme680_core_probe()
909 ret = bme680_read_calib(data, &data->bme680); in bme680_core_probe()