1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2012 Bosch Sensortec GmbH
5 * Copyright (c) 2012 Unixphere AB
6 * Copyright (c) 2014 Intel Corporation
9 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
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
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
25 #define pr_fmt(fmt) "bmp280: " fmt
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>
44 #include <asm/unaligned.h>
49 * These enums are used for indexing into the array of calibration
50 * coefficients for BMP180.
52 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
112 * These enums are used for indexing into the array of compensation
113 * parameters for BMP280.
115 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 };
118 /* Temperature calib indexes */
122 /* Pressure calib indexes */
136 static const struct iio_chan_spec bmp280_channels[] = {
138 .type = IIO_PRESSURE,
139 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
140 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
144 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
145 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
148 .type = IIO_HUMIDITYRELATIVE,
149 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
150 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
154 static const struct iio_chan_spec bmp380_channels[] = {
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),
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),
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),
178 static int bmp280_read_calib(struct bmp280_data *data)
180 struct bmp280_calib *calib = &data->calib.bmp280;
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));
189 "failed to read temperature and pressure calibration parameters\n");
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));
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]);
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]);
215 static int bme280_read_calib(struct bmp280_data *data)
217 struct bmp280_calib *calib = &data->calib.bmp280;
218 struct device *dev = data->dev;
222 /* Load shared calibration params with bmp280 first */
223 ret = bmp280_read_calib(data);
225 dev_err(dev, "failed to read common bmp280 calibration parameters\n");
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.
237 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
239 dev_err(dev, "failed to read H1 comp value\n");
244 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2,
245 &data->le16, sizeof(data->le16));
247 dev_err(dev, "failed to read H2 comp value\n");
250 calib->H2 = sign_extend32(le16_to_cpu(data->le16), 15);
252 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
254 dev_err(dev, "failed to read H3 comp value\n");
259 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4,
260 &data->be16, sizeof(data->be16));
262 dev_err(dev, "failed to read H4 comp value\n");
265 calib->H4 = sign_extend32(((be16_to_cpu(data->be16) >> 4) & 0xff0) |
266 (be16_to_cpu(data->be16) & 0xf), 11);
268 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5,
269 &data->le16, sizeof(data->le16));
271 dev_err(dev, "failed to read H5 comp value\n");
274 calib->H5 = sign_extend32(FIELD_GET(BMP280_COMP_H5_MASK, le16_to_cpu(data->le16)), 11);
276 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
278 dev_err(dev, "failed to read H6 comp value\n");
281 calib->H6 = sign_extend32(tmp, 7);
286 * Returns humidity in percent, resolution is 0.01 percent. Output value of
287 * "47445" represents 47445/1024 = 46.333 %RH.
289 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
291 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
294 struct bmp280_calib *calib = &data->calib.bmp280;
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;
304 var = clamp_val(var, 0, 419430400);
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
314 * Taken from datasheet, Section 3.11.3, "Compensation formula".
316 static s32 bmp280_compensate_temp(struct bmp280_data *data,
319 struct bmp280_calib *calib = &data->calib.bmp280;
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;
329 return (data->t_fine * 5 + 128) >> 8;
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
337 * Taken from datasheet, Section 3.11.3, "Compensation formula".
339 static u32 bmp280_compensate_press(struct bmp280_data *data,
342 struct bmp280_calib *calib = &data->calib.bmp280;
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;
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);
365 static int bmp280_read_temp(struct bmp280_data *data,
368 s32 adc_temp, comp_temp;
371 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
372 data->buf, sizeof(data->buf));
374 dev_err(data->dev, "failed to read temperature\n");
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");
384 comp_temp = bmp280_compensate_temp(data, adc_temp);
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.
391 *val = comp_temp * 10;
398 static int bmp280_read_press(struct bmp280_data *data,
405 /* Read and compensate temperature so we get a reading of t_fine. */
406 ret = bmp280_read_temp(data, NULL, NULL);
410 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
411 data->buf, sizeof(data->buf));
413 dev_err(data->dev, "failed to read pressure\n");
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");
423 comp_press = bmp280_compensate_press(data, adc_press);
428 return IIO_VAL_FRACTIONAL;
431 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
437 /* Read and compensate temperature so we get a reading of t_fine. */
438 ret = bmp280_read_temp(data, NULL, NULL);
442 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
443 &data->be16, sizeof(data->be16));
445 dev_err(data->dev, "failed to read humidity\n");
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");
455 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
457 *val = comp_humidity * 1000 / 1024;
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)
466 struct bmp280_data *data = iio_priv(indio_dev);
469 pm_runtime_get_sync(data->dev);
470 mutex_lock(&data->lock);
473 case IIO_CHAN_INFO_PROCESSED:
474 switch (chan->type) {
475 case IIO_HUMIDITYRELATIVE:
476 ret = data->chip_info->read_humid(data, val, val2);
479 ret = data->chip_info->read_press(data, val, val2);
482 ret = data->chip_info->read_temp(data, val, val2);
489 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
490 switch (chan->type) {
491 case IIO_HUMIDITYRELATIVE:
492 *val = 1 << data->oversampling_humid;
496 *val = 1 << data->oversampling_press;
500 *val = 1 << data->oversampling_temp;
508 case IIO_CHAN_INFO_SAMP_FREQ:
509 if (!data->chip_info->sampling_freq_avail) {
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;
518 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
519 if (!data->chip_info->iir_filter_coeffs_avail) {
524 *val = (1 << data->iir_filter_coeff) - 1;
532 mutex_unlock(&data->lock);
533 pm_runtime_mark_last_busy(data->dev);
534 pm_runtime_put_autosuspend(data->dev);
539 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
542 const int *avail = data->chip_info->oversampling_humid_avail;
543 const int n = data->chip_info->num_oversampling_humid_avail;
547 for (i = 0; i < n; i++) {
548 if (avail[i] == val) {
549 prev = data->oversampling_humid;
550 data->oversampling_humid = ilog2(val);
552 ret = data->chip_info->chip_config(data);
554 data->oversampling_humid = prev;
555 data->chip_info->chip_config(data);
564 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
567 const int *avail = data->chip_info->oversampling_temp_avail;
568 const int n = data->chip_info->num_oversampling_temp_avail;
572 for (i = 0; i < n; i++) {
573 if (avail[i] == val) {
574 prev = data->oversampling_temp;
575 data->oversampling_temp = ilog2(val);
577 ret = data->chip_info->chip_config(data);
579 data->oversampling_temp = prev;
580 data->chip_info->chip_config(data);
589 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
592 const int *avail = data->chip_info->oversampling_press_avail;
593 const int n = data->chip_info->num_oversampling_press_avail;
597 for (i = 0; i < n; i++) {
598 if (avail[i] == val) {
599 prev = data->oversampling_press;
600 data->oversampling_press = ilog2(val);
602 ret = data->chip_info->chip_config(data);
604 data->oversampling_press = prev;
605 data->chip_info->chip_config(data);
614 static int bmp280_write_sampling_frequency(struct bmp280_data *data,
617 const int (*avail)[2] = data->chip_info->sampling_freq_avail;
618 const int n = data->chip_info->num_sampling_freq_avail;
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;
627 ret = data->chip_info->chip_config(data);
629 data->sampling_freq = prev;
630 data->chip_info->chip_config(data);
639 static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val)
641 const int *avail = data->chip_info->iir_filter_coeffs_avail;
642 const int n = data->chip_info->num_iir_filter_coeffs_avail;
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;
651 ret = data->chip_info->chip_config(data);
653 data->iir_filter_coeff = prev;
654 data->chip_info->chip_config(data);
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)
668 struct bmp280_data *data = iio_priv(indio_dev);
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.
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);
686 ret = bmp280_write_oversampling_ratio_press(data, val);
689 ret = bmp280_write_oversampling_ratio_temp(data, val);
695 mutex_unlock(&data->lock);
696 pm_runtime_mark_last_busy(data->dev);
697 pm_runtime_put_autosuspend(data->dev);
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);
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);
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,
727 struct bmp280_data *data = iio_priv(indio_dev);
730 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
731 switch (chan->type) {
733 *vals = data->chip_info->oversampling_press_avail;
734 *length = data->chip_info->num_oversampling_press_avail;
737 *vals = data->chip_info->oversampling_temp_avail;
738 *length = data->chip_info->num_oversampling_temp_avail;
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;
754 *length = data->chip_info->num_iir_filter_coeffs_avail;
755 return IIO_AVAIL_LIST;
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,
767 static int bmp280_chip_config(struct bmp280_data *data)
769 u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) |
770 FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1);
773 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
774 BMP280_OSRS_TEMP_MASK |
775 BMP280_OSRS_PRESS_MASK |
777 osrs | BMP280_MODE_NORMAL);
780 "failed to write ctrl_meas register\n");
784 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
789 "failed to write config register\n");
796 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
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,
806 .oversampling_temp_avail = bmp280_oversampling_avail,
807 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
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.
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.
818 .oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
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,
824 .chip_config = bmp280_chip_config,
825 .read_temp = bmp280_read_temp,
826 .read_press = bmp280_read_press,
827 .read_calib = bmp280_read_calib,
829 EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280);
831 static int bme280_chip_config(struct bmp280_data *data)
833 u8 osrs = FIELD_PREP(BMP280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1);
837 * Oversampling of humidity must be set before oversampling of
838 * temperature/pressure is set to become effective.
840 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
841 BMP280_OSRS_HUMIDITY_MASK, osrs);
846 return bmp280_chip_config(data);
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,
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,
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,
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,
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,
875 EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280);
878 * Helper function to send a command to BMP3XX sensors.
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.
884 static int bmp380_cmd(struct bmp280_data *data, u8 cmd)
889 /* Check if device is ready to process a command */
890 ret = regmap_read(data->regmap, BMP380_REG_STATUS, ®);
892 dev_err(data->dev, "failed to read error register\n");
895 if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) {
896 dev_err(data->dev, "device is not ready to accept commands\n");
900 /* Send command to process */
901 ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd);
903 dev_err(data->dev, "failed to send command to device\n");
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, ®);
911 dev_err(data->dev, "error reading ERROR reg\n");
914 if (reg & BMP380_ERR_CMD_MASK) {
915 dev_err(data->dev, "error processing command 0x%X\n", cmd);
923 * Returns temperature in Celsius dregrees, resolution is 0.01º C. Output value of
924 * "5123" equals 51.2º C. t_fine carries fine temperature as global value.
926 * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo
927 * https://github.com/BoschSensortec/BMP3-Sensor-API.
929 static s32 bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp)
931 s64 var1, var2, var3, var4, var5, var6, comp_temp;
932 struct bmp380_calib *calib = &data->calib.bmp380;
934 var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8);
935 var2 = var1 * ((s64) calib->T2);
937 var4 = var3 * ((s64) calib->T3);
938 var5 = (var2 << 18) + var4;
940 data->t_fine = (s32) var6;
941 comp_temp = (var6 * 25) >> 14;
943 comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP);
944 return (s32) comp_temp;
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.
951 * Taken from datasheet, Section 9.3. "Pressure compensation" and repository
952 * https://github.com/BoschSensortec/BMP3-Sensor-API.
954 static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press)
956 s64 var1, var2, var3, var4, var5, var6, offset, sensitivity;
957 struct bmp380_calib *calib = &data->calib.bmp380;
960 var1 = (s64)data->t_fine * (s64)data->t_fine;
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) +
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;
979 * Dividing by 10 followed by multiplying by 10 to avoid
980 * possible overflow caused by (uncomp_data->pressure * partial_data4).
982 var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9;
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;
990 comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES);
994 static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2)
1000 ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB,
1001 data->buf, sizeof(data->buf));
1003 dev_err(data->dev, "failed to read temperature\n");
1007 adc_temp = get_unaligned_le24(data->buf);
1008 if (adc_temp == BMP380_TEMP_SKIPPED) {
1009 dev_err(data->dev, "reading temperature skipped\n");
1012 comp_temp = bmp380_compensate_temp(data, adc_temp);
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.
1019 /* IIO reports temperatures in milli Celsius */
1020 *val = comp_temp * 10;
1027 static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2)
1033 /* Read and compensate for temperature so we get a reading of t_fine */
1034 ret = bmp380_read_temp(data, NULL, NULL);
1038 ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1039 data->buf, sizeof(data->buf));
1041 dev_err(data->dev, "failed to read pressure\n");
1045 adc_press = get_unaligned_le24(data->buf);
1046 if (adc_press == BMP380_PRESS_SKIPPED) {
1047 dev_err(data->dev, "reading pressure skipped\n");
1050 comp_press = bmp380_compensate_press(data, adc_press);
1053 /* Compensated pressure is in cPa (centipascals) */
1056 return IIO_VAL_FRACTIONAL;
1059 static int bmp380_read_calib(struct bmp280_data *data)
1061 struct bmp380_calib *calib = &data->calib.bmp380;
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));
1069 "failed to read temperature calibration parameters\n");
1073 /* Toss the temperature calibration data into the entropy pool */
1074 add_device_randomness(data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf));
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];
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},
1116 static int bmp380_preinit(struct bmp280_data *data)
1118 /* BMP3xx requires soft-reset as part of initialization */
1119 return bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
1122 static int bmp380_chip_config(struct bmp280_data *data)
1124 bool change = false, aux;
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);
1136 "failed to write operation control register\n");
1140 /* Configure oversampling */
1141 osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) |
1142 FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press);
1144 ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR,
1145 BMP380_OSRS_TEMP_MASK |
1146 BMP380_OSRS_PRESS_MASK,
1149 dev_err(data->dev, "failed to write oversampling register\n");
1152 change = change || aux;
1154 /* Configure output data rate */
1155 ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR,
1156 BMP380_ODRS_MASK, data->sampling_freq, &aux);
1158 dev_err(data->dev, "failed to write ODR selection register\n");
1161 change = change || aux;
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),
1168 dev_err(data->dev, "failed to write config register\n");
1171 change = change || aux;
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.
1179 * Resets sensor measurement loop toggling between sleep and normal
1182 ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1184 FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_SLEEP));
1186 dev_err(data->dev, "failed to set sleep mode\n");
1189 usleep_range(2000, 2500);
1190 ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1192 FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL));
1194 dev_err(data->dev, "failed to set normal mode\n");
1198 * Waits for measurement before checking configuration error flag.
1199 * Selected longest measure time indicated in section 3.9.1
1204 /* Check config error flag */
1205 ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp);
1208 "failed to read error register\n");
1211 if (tmp & BMP380_ERR_CONF_MASK) {
1213 "sensor flagged configuration as incompatible\n");
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};
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,
1232 .oversampling_temp_avail = bmp380_oversampling_avail,
1233 .num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1234 .oversampling_temp_default = ilog2(1),
1236 .oversampling_press_avail = bmp380_oversampling_avail,
1237 .num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1238 .oversampling_press_default = ilog2(4),
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,
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,
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,
1254 EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280);
1256 static int bmp580_soft_reset(struct bmp280_data *data)
1261 ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET);
1263 dev_err(data->dev, "failed to send reset command to device\n");
1266 usleep_range(2000, 2500);
1268 /* Dummy read of chip_id */
1269 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®);
1271 dev_err(data->dev, "failed to reestablish comms after reset\n");
1275 ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, ®);
1277 dev_err(data->dev, "error reading interrupt status register\n");
1280 if (!(reg & BMP580_INT_STATUS_POR_MASK)) {
1281 dev_err(data->dev, "error resetting sensor\n");
1289 * bmp580_nvm_operation() - Helper function to commit NVM memory operations
1290 * @data: sensor data struct
1291 * @is_write: flag to signal write operation
1293 static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write)
1295 unsigned long timeout, poll;
1299 /* Check NVM ready flag */
1300 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®);
1302 dev_err(data->dev, "failed to check nvm status\n");
1305 if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) {
1306 dev_err(data->dev, "sensor's nvm is not ready\n");
1310 /* Start NVM operation sequence */
1311 ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_NVM_OP_SEQ_0);
1313 dev_err(data->dev, "failed to send nvm operation's first sequence\n");
1317 /* Send NVM write sequence */
1318 ret = regmap_write(data->regmap, BMP580_REG_CMD,
1319 BMP580_CMD_NVM_WRITE_SEQ_1);
1321 dev_err(data->dev, "failed to send nvm write sequence\n");
1324 /* Datasheet says on 4.8.1.2 it takes approximately 10ms */
1328 /* Send NVM read sequence */
1329 ret = regmap_write(data->regmap, BMP580_REG_CMD,
1330 BMP580_CMD_NVM_READ_SEQ_1);
1332 dev_err(data->dev, "failed to send nvm read sequence\n");
1335 /* Datasheet says on 4.8.1.1 it takes approximately 200us */
1340 dev_err(data->dev, "failed to write command sequence\n");
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),
1349 dev_err(data->dev, "error checking nvm operation status\n");
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");
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.
1368 static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2)
1373 ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf,
1376 dev_err(data->dev, "failed to read temperature\n");
1380 raw_temp = get_unaligned_le24(data->buf);
1381 if (raw_temp == BMP580_TEMP_SKIPPED) {
1382 dev_err(data->dev, "reading temperature skipped\n");
1387 * Temperature is returned in Celsius degrees in fractional
1388 * form down 2^16. We reescale by x1000 to return milli Celsius
1389 * to respect IIO ABI.
1391 *val = raw_temp * 1000;
1393 return IIO_VAL_FRACTIONAL_LOG2;
1396 static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2)
1401 ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf,
1404 dev_err(data->dev, "failed to read pressure\n");
1408 raw_press = get_unaligned_le24(data->buf);
1409 if (raw_press == BMP580_PRESS_SKIPPED) {
1410 dev_err(data->dev, "reading pressure skipped\n");
1414 * Pressure is returned in Pascals in fractional form down 2^16.
1415 * We reescale /1000 to convert to kilopascal to respect IIO ABI.
1418 *val2 = 64000; /* 2^6 * 1000 */
1419 return IIO_VAL_FRACTIONAL;
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},
1457 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
1459 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
1462 struct bmp280_data *data = priv;
1466 pm_runtime_get_sync(data->dev);
1467 mutex_lock(&data->lock);
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));
1475 dev_err(data->dev, "failed to change sensor to standby mode\n");
1478 /* Wait standby transition time */
1479 usleep_range(2500, 3000);
1481 while (bytes >= sizeof(*dst)) {
1482 addr = bmp580_nvmem_addrs[offset / sizeof(*dst)];
1484 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1485 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1487 dev_err(data->dev, "error writing nvm address\n");
1491 ret = bmp580_nvm_operation(data, false);
1495 ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1496 sizeof(data->le16));
1498 dev_err(data->dev, "error reading nvm data regs\n");
1502 *dst++ = le16_to_cpu(data->le16);
1503 bytes -= sizeof(*dst);
1504 offset += sizeof(*dst);
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);
1515 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
1518 struct bmp280_data *data = priv;
1522 pm_runtime_get_sync(data->dev);
1523 mutex_lock(&data->lock);
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));
1531 dev_err(data->dev, "failed to change sensor to standby mode\n");
1534 /* Wait standby transition time */
1535 usleep_range(2500, 3000);
1537 while (bytes >= sizeof(*buf)) {
1538 addr = bmp580_nvmem_addrs[offset / sizeof(*buf)];
1540 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, BMP580_NVM_PROG_EN |
1541 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1543 dev_err(data->dev, "error writing nvm address\n");
1546 data->le16 = cpu_to_le16(*buf++);
1548 ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1549 sizeof(data->le16));
1551 dev_err(data->dev, "error writing LSB NVM data regs\n");
1555 ret = bmp580_nvm_operation(data, true);
1559 /* Disable programming mode bit */
1560 ret = regmap_update_bits(data->regmap, BMP580_REG_NVM_ADDR,
1561 BMP580_NVM_PROG_EN, 0);
1563 dev_err(data->dev, "error resetting nvm write\n");
1567 bytes -= sizeof(*buf);
1568 offset += sizeof(*buf);
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);
1579 static int bmp580_preinit(struct bmp280_data *data)
1581 struct nvmem_config config = {
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,
1594 /* Issue soft-reset command */
1595 ret = bmp580_soft_reset(data);
1599 /* Post powerup sequence */
1600 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®);
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");
1608 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®);
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");
1618 /* Register nvmem device */
1619 return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
1622 static int bmp580_chip_config(struct bmp280_data *data)
1624 bool change = false, aux;
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));
1635 dev_err(data->dev, "failed to change sensor to standby mode\n");
1638 /* From datasheet's table 4: electrical characteristics */
1639 usleep_range(2500, 3000);
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;
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);
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;
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,
1660 dev_err(data->dev, "failed to write oversampling register\n");
1663 change = change || aux;
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),
1670 dev_err(data->dev, "failed to write ODR configuration register\n");
1673 change = change || aux;
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);
1679 ret = regmap_update_bits_check(data->regmap, BMP580_REG_DSP_IIR,
1680 BMP580_DSP_IIR_PRESS_MASK |
1681 BMP580_DSP_IIR_TEMP_MASK,
1684 dev_err(data->dev, "failed to write config register\n");
1687 change = change || aux;
1689 /* Restore sensor to normal operation mode */
1690 ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1692 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_NORMAL));
1694 dev_err(data->dev, "failed to set normal mode\n");
1697 /* From datasheet's table 4: electrical characteristics */
1698 usleep_range(3000, 3500);
1702 * Check if ODR and OSR settings are valid or we are
1703 * operating in a degraded mode.
1705 ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp);
1707 dev_err(data->dev, "error reading effective OSR register\n");
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);
1722 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
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,
1732 .oversampling_temp_avail = bmp580_oversampling_avail,
1733 .num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1734 .oversampling_temp_default = ilog2(1),
1736 .oversampling_press_avail = bmp580_oversampling_avail,
1737 .num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1738 .oversampling_press_default = ilog2(4),
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,
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,
1748 .chip_config = bmp580_chip_config,
1749 .read_temp = bmp580_read_temp,
1750 .read_press = bmp580_read_press,
1751 .preinit = bmp580_preinit,
1753 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280);
1755 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
1757 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
1758 unsigned int delay_us;
1763 reinit_completion(&data->done);
1765 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
1769 if (data->use_eoc) {
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.
1775 ret = wait_for_completion_timeout(&data->done,
1776 1 + msecs_to_jiffies(100));
1778 dev_err(data->dev, "timeout waiting for completion\n");
1780 if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
1784 conversion_time_max[data->oversampling_press];
1786 usleep_range(delay_us, delay_us + 1000);
1789 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
1793 /* The value of this bit reset to "0" after conversion is complete */
1794 if (ctrl & BMP180_MEAS_SCO)
1800 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
1804 ret = bmp180_measure(data,
1805 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
1810 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1811 &data->be16, sizeof(data->be16));
1815 *val = be16_to_cpu(data->be16);
1820 static int bmp180_read_calib(struct bmp280_data *data)
1822 struct bmp180_calib *calib = &data->calib.bmp180;
1826 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
1827 data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
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))
1839 /* Toss the calibration data into the entropy pool */
1840 add_device_randomness(data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
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]);
1858 * Returns temperature in DegC, resolution is 0.1 DegC.
1859 * t_fine carries fine temperature as global value.
1861 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1863 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
1865 struct bmp180_calib *calib = &data->calib.bmp180;
1868 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
1869 x2 = (calib->MC << 11) / (x1 + calib->MD);
1870 data->t_fine = x1 + x2;
1872 return (data->t_fine + 8) >> 4;
1875 static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2)
1877 s32 adc_temp, comp_temp;
1880 ret = bmp180_read_adc_temp(data, &adc_temp);
1884 comp_temp = bmp180_compensate_temp(data, adc_temp);
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.
1891 *val = comp_temp * 100;
1898 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
1900 u8 oss = data->oversampling_press;
1903 ret = bmp180_measure(data,
1904 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
1905 FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
1910 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1911 data->buf, sizeof(data->buf));
1915 *val = get_unaligned_be24(data->buf) >> (8 - oss);
1921 * Returns pressure in Pa, resolution is 1 Pa.
1923 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1925 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
1927 struct bmp180_calib *calib = &data->calib.bmp180;
1928 s32 oss = data->oversampling_press;
1933 b6 = data->t_fine - 4000;
1934 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
1935 x2 = calib->AC2 * b6 >> 11;
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)
1948 x1 = (p >> 8) * (p >> 8);
1949 x1 = (x1 * 3038) >> 16;
1950 x2 = (-7357 * p) >> 16;
1952 return p + ((x1 + x2 + 3791) >> 4);
1955 static int bmp180_read_press(struct bmp280_data *data,
1956 int *val, int *val2)
1962 /* Read and compensate temperature so we get a reading of t_fine. */
1963 ret = bmp180_read_temp(data, NULL, NULL);
1967 ret = bmp180_read_adc_press(data, &adc_press);
1971 comp_press = bmp180_compensate_press(data, adc_press);
1976 return IIO_VAL_FRACTIONAL;
1979 static int bmp180_chip_config(struct bmp280_data *data)
1984 static const int bmp180_oversampling_temp_avail[] = { 1 };
1985 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
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,
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,
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,
2005 .chip_config = bmp180_chip_config,
2006 .read_temp = bmp180_read_temp,
2007 .read_press = bmp180_read_press,
2008 .read_calib = bmp180_read_calib,
2010 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280);
2012 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
2014 struct bmp280_data *data = d;
2016 complete(&data->done);
2021 static int bmp085_fetch_eoc_irq(struct device *dev,
2024 struct bmp280_data *data)
2026 unsigned long irq_trig;
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;
2035 init_completion(&data->done);
2037 ret = devm_request_threaded_irq(dev,
2045 /* Bail out without IRQ but keep the driver in place */
2046 dev_err(dev, "unable to request DRDY IRQ\n");
2050 data->use_eoc = true;
2054 static void bmp280_pm_disable(void *data)
2056 struct device *dev = data;
2058 pm_runtime_get_sync(dev);
2059 pm_runtime_put_noidle(dev);
2060 pm_runtime_disable(dev);
2063 static void bmp280_regulators_disable(void *data)
2065 struct regulator_bulk_data *supplies = data;
2067 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
2070 int bmp280_common_probe(struct device *dev,
2071 struct regmap *regmap,
2072 const struct bmp280_chip_info *chip_info,
2076 struct iio_dev *indio_dev;
2077 struct bmp280_data *data;
2078 struct gpio_desc *gpiod;
2079 unsigned int chip_id;
2082 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
2086 data = iio_priv(indio_dev);
2087 mutex_init(&data->lock);
2090 indio_dev->name = name;
2091 indio_dev->info = &bmp280_info;
2092 indio_dev->modes = INDIO_DIRECT_MODE;
2094 data->chip_info = chip_info;
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;
2106 /* Bring up regulators */
2107 regulator_bulk_set_supply_names(data->supplies,
2108 bmp280_supply_names,
2109 BMP280_NUM_SUPPLIES);
2111 ret = devm_regulator_bulk_get(dev,
2112 BMP280_NUM_SUPPLIES, data->supplies);
2114 dev_err(dev, "failed to get regulators\n");
2118 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2120 dev_err(dev, "failed to enable regulators\n");
2124 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
2129 /* Wait to make sure we started up properly */
2130 usleep_range(data->start_up_time, data->start_up_time + 100);
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 */
2136 dev_info(dev, "release reset\n");
2137 gpiod_set_value(gpiod, 0);
2140 data->regmap = regmap;
2142 ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
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);
2151 if (data->chip_info->preinit) {
2152 ret = data->chip_info->preinit(data);
2154 return dev_err_probe(data->dev, ret,
2155 "error running preinit tasks\n");
2158 ret = data->chip_info->chip_config(data);
2162 dev_set_drvdata(dev, indio_dev);
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.
2170 if (data->chip_info->read_calib) {
2171 ret = data->chip_info->read_calib(data);
2173 return dev_err_probe(data->dev, ret,
2174 "failed to read calibration coefficients\n");
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.
2182 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
2183 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
2188 /* Enable runtime PM */
2189 pm_runtime_get_noresume(dev);
2190 pm_runtime_set_active(dev);
2191 pm_runtime_enable(dev);
2193 * Set autosuspend to two orders of magnitude larger than the
2196 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
2197 pm_runtime_use_autosuspend(dev);
2198 pm_runtime_put(dev);
2200 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
2204 return devm_iio_device_register(dev, indio_dev);
2206 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
2208 static int bmp280_runtime_suspend(struct device *dev)
2210 struct iio_dev *indio_dev = dev_get_drvdata(dev);
2211 struct bmp280_data *data = iio_priv(indio_dev);
2213 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
2216 static int bmp280_runtime_resume(struct device *dev)
2218 struct iio_dev *indio_dev = dev_get_drvdata(dev);
2219 struct bmp280_data *data = iio_priv(indio_dev);
2222 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2225 usleep_range(data->start_up_time, data->start_up_time + 100);
2226 return data->chip_info->chip_config(data);
2229 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
2230 bmp280_runtime_resume, NULL);
2233 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
2234 MODULE_LICENSE("GPL v2");