1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
214e8015fSLinus Walleij /*
3b26b4e91SLinus Walleij  * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
4b26b4e91SLinus Walleij  * Copyright (c) 2012 Bosch Sensortec GmbH
5b26b4e91SLinus Walleij  * Copyright (c) 2012 Unixphere AB
614e8015fSLinus Walleij  * Copyright (c) 2014 Intel Corporation
7b26b4e91SLinus Walleij  * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
814e8015fSLinus Walleij  *
914e8015fSLinus Walleij  * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
1014e8015fSLinus Walleij  *
1114e8015fSLinus Walleij  * Datasheet:
125d5129b1SAngel Iglesias  * https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
135d5129b1SAngel Iglesias  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf
145d5129b1SAngel Iglesias  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
158d329309SAngel Iglesias  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf
16597dfb2aSAngel Iglesias  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp581-ds004.pdf
175d5129b1SAngel Iglesias  *
185d5129b1SAngel Iglesias  * Notice:
195d5129b1SAngel Iglesias  * The link to the bmp180 datasheet points to an outdated version missing these changes:
205d5129b1SAngel Iglesias  * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26
215d5129b1SAngel Iglesias  * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4
225d5129b1SAngel Iglesias  * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26
2314e8015fSLinus Walleij  */
2414e8015fSLinus Walleij 
2514e8015fSLinus Walleij #define pr_fmt(fmt) "bmp280: " fmt
2614e8015fSLinus Walleij 
272405f8ccSAngel Iglesias #include <linux/bitops.h>
282405f8ccSAngel Iglesias #include <linux/bitfield.h>
2914e8015fSLinus Walleij #include <linux/device.h>
3017118843SLinus Walleij #include <linux/module.h>
31accb9d05SAngel Iglesias #include <linux/nvmem-provider.h>
3214e8015fSLinus Walleij #include <linux/regmap.h>
3314e8015fSLinus Walleij #include <linux/delay.h>
3414e8015fSLinus Walleij #include <linux/iio/iio.h>
3514e8015fSLinus Walleij #include <linux/iio/sysfs.h>
3614e8015fSLinus Walleij #include <linux/gpio/consumer.h>
3714e8015fSLinus Walleij #include <linux/regulator/consumer.h>
38aae95394SLinus Walleij #include <linux/interrupt.h>
39aae95394SLinus Walleij #include <linux/irq.h> /* For irq_get_irq_data() */
40aae95394SLinus Walleij #include <linux/completion.h>
413d838118SLinus Walleij #include <linux/pm_runtime.h>
42b33b7d5aSLinus Walleij #include <linux/random.h>
4314e8015fSLinus Walleij 
44327b5c05SAngel Iglesias #include <asm/unaligned.h>
45327b5c05SAngel Iglesias 
4614e8015fSLinus Walleij #include "bmp280.h"
4714e8015fSLinus Walleij 
48b33b7d5aSLinus Walleij /*
49b33b7d5aSLinus Walleij  * These enums are used for indexing into the array of calibration
50b33b7d5aSLinus Walleij  * coefficients for BMP180.
51b33b7d5aSLinus Walleij  */
52b33b7d5aSLinus Walleij enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
53b33b7d5aSLinus Walleij 
541372d1a1SBartosz Golaszewski 
5510b40ffbSAngel Iglesias enum bmp380_odr {
5610b40ffbSAngel Iglesias 	BMP380_ODR_200HZ,
5710b40ffbSAngel Iglesias 	BMP380_ODR_100HZ,
5810b40ffbSAngel Iglesias 	BMP380_ODR_50HZ,
5910b40ffbSAngel Iglesias 	BMP380_ODR_25HZ,
6010b40ffbSAngel Iglesias 	BMP380_ODR_12_5HZ,
6110b40ffbSAngel Iglesias 	BMP380_ODR_6_25HZ,
6210b40ffbSAngel Iglesias 	BMP380_ODR_3_125HZ,
6310b40ffbSAngel Iglesias 	BMP380_ODR_1_5625HZ,
6410b40ffbSAngel Iglesias 	BMP380_ODR_0_78HZ,
6510b40ffbSAngel Iglesias 	BMP380_ODR_0_39HZ,
6610b40ffbSAngel Iglesias 	BMP380_ODR_0_2HZ,
6710b40ffbSAngel Iglesias 	BMP380_ODR_0_1HZ,
6810b40ffbSAngel Iglesias 	BMP380_ODR_0_05HZ,
6910b40ffbSAngel Iglesias 	BMP380_ODR_0_02HZ,
7010b40ffbSAngel Iglesias 	BMP380_ODR_0_01HZ,
7110b40ffbSAngel Iglesias 	BMP380_ODR_0_006HZ,
7210b40ffbSAngel Iglesias 	BMP380_ODR_0_003HZ,
7310b40ffbSAngel Iglesias 	BMP380_ODR_0_0015HZ,
7410b40ffbSAngel Iglesias };
7510b40ffbSAngel Iglesias 
76597dfb2aSAngel Iglesias enum bmp580_odr {
77597dfb2aSAngel Iglesias 	BMP580_ODR_240HZ,
78597dfb2aSAngel Iglesias 	BMP580_ODR_218HZ,
79597dfb2aSAngel Iglesias 	BMP580_ODR_199HZ,
80597dfb2aSAngel Iglesias 	BMP580_ODR_179HZ,
81597dfb2aSAngel Iglesias 	BMP580_ODR_160HZ,
82597dfb2aSAngel Iglesias 	BMP580_ODR_149HZ,
83597dfb2aSAngel Iglesias 	BMP580_ODR_140HZ,
84597dfb2aSAngel Iglesias 	BMP580_ODR_129HZ,
85597dfb2aSAngel Iglesias 	BMP580_ODR_120HZ,
86597dfb2aSAngel Iglesias 	BMP580_ODR_110HZ,
87597dfb2aSAngel Iglesias 	BMP580_ODR_100HZ,
88597dfb2aSAngel Iglesias 	BMP580_ODR_89HZ,
89597dfb2aSAngel Iglesias 	BMP580_ODR_80HZ,
90597dfb2aSAngel Iglesias 	BMP580_ODR_70HZ,
91597dfb2aSAngel Iglesias 	BMP580_ODR_60HZ,
92597dfb2aSAngel Iglesias 	BMP580_ODR_50HZ,
93597dfb2aSAngel Iglesias 	BMP580_ODR_45HZ,
94597dfb2aSAngel Iglesias 	BMP580_ODR_40HZ,
95597dfb2aSAngel Iglesias 	BMP580_ODR_35HZ,
96597dfb2aSAngel Iglesias 	BMP580_ODR_30HZ,
97597dfb2aSAngel Iglesias 	BMP580_ODR_25HZ,
98597dfb2aSAngel Iglesias 	BMP580_ODR_20HZ,
99597dfb2aSAngel Iglesias 	BMP580_ODR_15HZ,
100597dfb2aSAngel Iglesias 	BMP580_ODR_10HZ,
101597dfb2aSAngel Iglesias 	BMP580_ODR_5HZ,
102597dfb2aSAngel Iglesias 	BMP580_ODR_4HZ,
103597dfb2aSAngel Iglesias 	BMP580_ODR_3HZ,
104597dfb2aSAngel Iglesias 	BMP580_ODR_2HZ,
105597dfb2aSAngel Iglesias 	BMP580_ODR_1HZ,
106597dfb2aSAngel Iglesias 	BMP580_ODR_0_5HZ,
107597dfb2aSAngel Iglesias 	BMP580_ODR_0_25HZ,
108597dfb2aSAngel Iglesias 	BMP580_ODR_0_125HZ,
109597dfb2aSAngel Iglesias };
110597dfb2aSAngel Iglesias 
11114e8015fSLinus Walleij /*
11214e8015fSLinus Walleij  * These enums are used for indexing into the array of compensation
11314e8015fSLinus Walleij  * parameters for BMP280.
11414e8015fSLinus Walleij  */
11583cb40beSAngel Iglesias enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 };
11614e8015fSLinus Walleij 
1178d329309SAngel Iglesias enum {
1188d329309SAngel Iglesias 	/* Temperature calib indexes */
1198d329309SAngel Iglesias 	BMP380_T1 = 0,
1208d329309SAngel Iglesias 	BMP380_T2 = 2,
1218d329309SAngel Iglesias 	BMP380_T3 = 4,
1228d329309SAngel Iglesias 	/* Pressure calib indexes */
1238d329309SAngel Iglesias 	BMP380_P1 = 5,
1248d329309SAngel Iglesias 	BMP380_P2 = 7,
1258d329309SAngel Iglesias 	BMP380_P3 = 9,
1268d329309SAngel Iglesias 	BMP380_P4 = 10,
1278d329309SAngel Iglesias 	BMP380_P5 = 11,
1288d329309SAngel Iglesias 	BMP380_P6 = 13,
1298d329309SAngel Iglesias 	BMP380_P7 = 15,
1308d329309SAngel Iglesias 	BMP380_P8 = 16,
1318d329309SAngel Iglesias 	BMP380_P9 = 17,
1328d329309SAngel Iglesias 	BMP380_P10 = 19,
1338d329309SAngel Iglesias 	BMP380_P11 = 20,
1348d329309SAngel Iglesias };
1358d329309SAngel Iglesias 
13614e8015fSLinus Walleij static const struct iio_chan_spec bmp280_channels[] = {
13714e8015fSLinus Walleij 	{
13814e8015fSLinus Walleij 		.type = IIO_PRESSURE,
13914e8015fSLinus Walleij 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
14014e8015fSLinus Walleij 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
14114e8015fSLinus Walleij 	},
14214e8015fSLinus Walleij 	{
14314e8015fSLinus Walleij 		.type = IIO_TEMP,
14414e8015fSLinus Walleij 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
14514e8015fSLinus Walleij 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
14614e8015fSLinus Walleij 	},
14714e8015fSLinus Walleij 	{
14814e8015fSLinus Walleij 		.type = IIO_HUMIDITYRELATIVE,
14914e8015fSLinus Walleij 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
15014e8015fSLinus Walleij 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
15114e8015fSLinus Walleij 	},
15214e8015fSLinus Walleij };
15314e8015fSLinus Walleij 
15410b40ffbSAngel Iglesias static const struct iio_chan_spec bmp380_channels[] = {
15510b40ffbSAngel Iglesias 	{
15610b40ffbSAngel Iglesias 		.type = IIO_PRESSURE,
15710b40ffbSAngel Iglesias 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
15810b40ffbSAngel Iglesias 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
15910b40ffbSAngel Iglesias 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
16010b40ffbSAngel Iglesias 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
16110b40ffbSAngel Iglesias 	},
16210b40ffbSAngel Iglesias 	{
16310b40ffbSAngel Iglesias 		.type = IIO_TEMP,
16410b40ffbSAngel Iglesias 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
16510b40ffbSAngel Iglesias 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
16610b40ffbSAngel Iglesias 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
16710b40ffbSAngel Iglesias 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
16810b40ffbSAngel Iglesias 	},
16910b40ffbSAngel Iglesias 	{
17010b40ffbSAngel Iglesias 		.type = IIO_HUMIDITYRELATIVE,
17110b40ffbSAngel Iglesias 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
17210b40ffbSAngel Iglesias 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
17310b40ffbSAngel Iglesias 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
17410b40ffbSAngel Iglesias 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
17510b40ffbSAngel Iglesias 	},
17610b40ffbSAngel Iglesias };
17710b40ffbSAngel Iglesias 
bmp280_read_calib(struct bmp280_data * data)178b00e805aSAngel Iglesias static int bmp280_read_calib(struct bmp280_data *data)
17914e8015fSLinus Walleij {
180b00e805aSAngel Iglesias 	struct bmp280_calib *calib = &data->calib.bmp280;
1815f0c359dSAngel Iglesias 	int ret;
1825f0c359dSAngel Iglesias 
18314e8015fSLinus Walleij 
18483cb40beSAngel Iglesias 	/* Read temperature and pressure calibration values. */
1852e419aecSStefan Tatschner 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
186327b5c05SAngel Iglesias 			       data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf));
1872e419aecSStefan Tatschner 	if (ret < 0) {
1882e419aecSStefan Tatschner 		dev_err(data->dev,
18983cb40beSAngel Iglesias 			"failed to read temperature and pressure calibration parameters\n");
1902e419aecSStefan Tatschner 		return ret;
1912e419aecSStefan Tatschner 	}
1922e419aecSStefan Tatschner 
19383cb40beSAngel Iglesias 	/* Toss the temperature and pressure calibration data into the entropy pool */
194327b5c05SAngel Iglesias 	add_device_randomness(data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf));
1956282b5c6SLinus Walleij 
19683cb40beSAngel Iglesias 	/* Parse temperature calibration values. */
197327b5c05SAngel Iglesias 	calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]);
198327b5c05SAngel Iglesias 	calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]);
199327b5c05SAngel Iglesias 	calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]);
2002e419aecSStefan Tatschner 
20183cb40beSAngel Iglesias 	/* Parse pressure calibration values. */
202327b5c05SAngel Iglesias 	calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]);
203327b5c05SAngel Iglesias 	calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]);
204327b5c05SAngel Iglesias 	calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]);
205327b5c05SAngel Iglesias 	calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]);
206327b5c05SAngel Iglesias 	calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]);
207327b5c05SAngel Iglesias 	calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]);
208327b5c05SAngel Iglesias 	calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]);
209327b5c05SAngel Iglesias 	calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]);
210327b5c05SAngel Iglesias 	calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]);
2112e419aecSStefan Tatschner 
212b00e805aSAngel Iglesias 	return 0;
213b00e805aSAngel Iglesias }
214b00e805aSAngel Iglesias 
bme280_read_calib(struct bmp280_data * data)215b00e805aSAngel Iglesias static int bme280_read_calib(struct bmp280_data *data)
216b00e805aSAngel Iglesias {
217b00e805aSAngel Iglesias 	struct bmp280_calib *calib = &data->calib.bmp280;
218b00e805aSAngel Iglesias 	struct device *dev = data->dev;
219b00e805aSAngel Iglesias 	unsigned int tmp;
220b00e805aSAngel Iglesias 	int ret;
221b00e805aSAngel Iglesias 
222b00e805aSAngel Iglesias 	/* Load shared calibration params with bmp280 first */
223b00e805aSAngel Iglesias 	ret = bmp280_read_calib(data);
224b00e805aSAngel Iglesias 	if  (ret < 0) {
225b00e805aSAngel Iglesias 		dev_err(dev, "failed to read common bmp280 calibration parameters\n");
226b00e805aSAngel Iglesias 		return ret;
227b00e805aSAngel Iglesias 	}
228b00e805aSAngel Iglesias 
2292e419aecSStefan Tatschner 	/*
2302e419aecSStefan Tatschner 	 * Read humidity calibration values.
2312e419aecSStefan Tatschner 	 * Due to some odd register addressing we cannot just
2322e419aecSStefan Tatschner 	 * do a big bulk read. Instead, we have to read each Hx
2332e419aecSStefan Tatschner 	 * value separately and sometimes do some bit shifting...
2342e419aecSStefan Tatschner 	 * Humidity data is only available on BME280.
2352e419aecSStefan Tatschner 	 */
2362e419aecSStefan Tatschner 
2372e419aecSStefan Tatschner 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
23814e8015fSLinus Walleij 	if (ret < 0) {
23914e8015fSLinus Walleij 		dev_err(dev, "failed to read H1 comp value\n");
24014e8015fSLinus Walleij 		return ret;
24114e8015fSLinus Walleij 	}
2422e419aecSStefan Tatschner 	calib->H1 = tmp;
24314e8015fSLinus Walleij 
244327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2,
245327b5c05SAngel Iglesias 			       &data->le16, sizeof(data->le16));
24614e8015fSLinus Walleij 	if (ret < 0) {
24714e8015fSLinus Walleij 		dev_err(dev, "failed to read H2 comp value\n");
24814e8015fSLinus Walleij 		return ret;
24914e8015fSLinus Walleij 	}
250327b5c05SAngel Iglesias 	calib->H2 = sign_extend32(le16_to_cpu(data->le16), 15);
25114e8015fSLinus Walleij 
2522e419aecSStefan Tatschner 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
25314e8015fSLinus Walleij 	if (ret < 0) {
25414e8015fSLinus Walleij 		dev_err(dev, "failed to read H3 comp value\n");
25514e8015fSLinus Walleij 		return ret;
25614e8015fSLinus Walleij 	}
2572e419aecSStefan Tatschner 	calib->H3 = tmp;
25814e8015fSLinus Walleij 
259327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4,
260327b5c05SAngel Iglesias 			       &data->be16, sizeof(data->be16));
26114e8015fSLinus Walleij 	if (ret < 0) {
26214e8015fSLinus Walleij 		dev_err(dev, "failed to read H4 comp value\n");
26314e8015fSLinus Walleij 		return ret;
26414e8015fSLinus Walleij 	}
265327b5c05SAngel Iglesias 	calib->H4 = sign_extend32(((be16_to_cpu(data->be16) >> 4) & 0xff0) |
266327b5c05SAngel Iglesias 				  (be16_to_cpu(data->be16) & 0xf), 11);
26714e8015fSLinus Walleij 
268327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5,
269327b5c05SAngel Iglesias 			       &data->le16, sizeof(data->le16));
27014e8015fSLinus Walleij 	if (ret < 0) {
27114e8015fSLinus Walleij 		dev_err(dev, "failed to read H5 comp value\n");
27214e8015fSLinus Walleij 		return ret;
27314e8015fSLinus Walleij 	}
274327b5c05SAngel Iglesias 	calib->H5 = sign_extend32(FIELD_GET(BMP280_COMP_H5_MASK, le16_to_cpu(data->le16)), 11);
27514e8015fSLinus Walleij 
27614e8015fSLinus Walleij 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
27714e8015fSLinus Walleij 	if (ret < 0) {
27814e8015fSLinus Walleij 		dev_err(dev, "failed to read H6 comp value\n");
27914e8015fSLinus Walleij 		return ret;
28014e8015fSLinus Walleij 	}
2812e419aecSStefan Tatschner 	calib->H6 = sign_extend32(tmp, 7);
2822e419aecSStefan Tatschner 
2832e419aecSStefan Tatschner 	return 0;
2842e419aecSStefan Tatschner }
2852e419aecSStefan Tatschner /*
2862e419aecSStefan Tatschner  * Returns humidity in percent, resolution is 0.01 percent. Output value of
2872e419aecSStefan Tatschner  * "47445" represents 47445/1024 = 46.333 %RH.
2882e419aecSStefan Tatschner  *
2892e419aecSStefan Tatschner  * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
2902e419aecSStefan Tatschner  */
bmp280_compensate_humidity(struct bmp280_data * data,s32 adc_humidity)2912e419aecSStefan Tatschner static u32 bmp280_compensate_humidity(struct bmp280_data *data,
2922e419aecSStefan Tatschner 				      s32 adc_humidity)
2932e419aecSStefan Tatschner {
2942e419aecSStefan Tatschner 	struct bmp280_calib *calib = &data->calib.bmp280;
2955f0c359dSAngel Iglesias 	s32 var;
29614e8015fSLinus Walleij 
297ed3730c4SAndreas Klinger 	var = ((s32)data->t_fine) - (s32)76800;
2982e419aecSStefan Tatschner 	var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
2992e419aecSStefan Tatschner 		+ (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
3002e419aecSStefan Tatschner 		* (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
3012e419aecSStefan Tatschner 		+ (s32)2097152) * calib->H2 + 8192) >> 14);
3022e419aecSStefan Tatschner 	var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
30314e8015fSLinus Walleij 
304dee2dabcSAndreas Klinger 	var = clamp_val(var, 0, 419430400);
305dee2dabcSAndreas Klinger 
30614e8015fSLinus Walleij 	return var >> 12;
30714e8015fSLinus Walleij };
30814e8015fSLinus Walleij 
30914e8015fSLinus Walleij /*
31014e8015fSLinus Walleij  * Returns temperature in DegC, resolution is 0.01 DegC.  Output value of
31114e8015fSLinus Walleij  * "5123" equals 51.23 DegC.  t_fine carries fine temperature as global
31214e8015fSLinus Walleij  * value.
31314e8015fSLinus Walleij  *
31414e8015fSLinus Walleij  * Taken from datasheet, Section 3.11.3, "Compensation formula".
31514e8015fSLinus Walleij  */
bmp280_compensate_temp(struct bmp280_data * data,s32 adc_temp)31614e8015fSLinus Walleij static s32 bmp280_compensate_temp(struct bmp280_data *data,
31714e8015fSLinus Walleij 				  s32 adc_temp)
31814e8015fSLinus Walleij {
3192e419aecSStefan Tatschner 	struct bmp280_calib *calib = &data->calib.bmp280;
3205f0c359dSAngel Iglesias 	s32 var1, var2;
32114e8015fSLinus Walleij 
3222e419aecSStefan Tatschner 	var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
3232e419aecSStefan Tatschner 		((s32)calib->T2)) >> 11;
3242e419aecSStefan Tatschner 	var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
3252e419aecSStefan Tatschner 		  ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
3262e419aecSStefan Tatschner 		((s32)calib->T3)) >> 14;
32714e8015fSLinus Walleij 	data->t_fine = var1 + var2;
32814e8015fSLinus Walleij 
32914e8015fSLinus Walleij 	return (data->t_fine * 5 + 128) >> 8;
33014e8015fSLinus Walleij }
33114e8015fSLinus Walleij 
33214e8015fSLinus Walleij /*
33314e8015fSLinus Walleij  * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
33414e8015fSLinus Walleij  * integer bits and 8 fractional bits).  Output value of "24674867"
33514e8015fSLinus Walleij  * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
33614e8015fSLinus Walleij  *
33714e8015fSLinus Walleij  * Taken from datasheet, Section 3.11.3, "Compensation formula".
33814e8015fSLinus Walleij  */
bmp280_compensate_press(struct bmp280_data * data,s32 adc_press)33914e8015fSLinus Walleij static u32 bmp280_compensate_press(struct bmp280_data *data,
34014e8015fSLinus Walleij 				   s32 adc_press)
34114e8015fSLinus Walleij {
3422e419aecSStefan Tatschner 	struct bmp280_calib *calib = &data->calib.bmp280;
3435f0c359dSAngel Iglesias 	s64 var1, var2, p;
34414e8015fSLinus Walleij 
34514e8015fSLinus Walleij 	var1 = ((s64)data->t_fine) - 128000;
3462e419aecSStefan Tatschner 	var2 = var1 * var1 * (s64)calib->P6;
3472e419aecSStefan Tatschner 	var2 += (var1 * (s64)calib->P5) << 17;
3482e419aecSStefan Tatschner 	var2 += ((s64)calib->P4) << 35;
3492e419aecSStefan Tatschner 	var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
3502e419aecSStefan Tatschner 		((var1 * (s64)calib->P2) << 12);
3512e419aecSStefan Tatschner 	var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
35214e8015fSLinus Walleij 
35314e8015fSLinus Walleij 	if (var1 == 0)
35414e8015fSLinus Walleij 		return 0;
35514e8015fSLinus Walleij 
35614e8015fSLinus Walleij 	p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
35714e8015fSLinus Walleij 	p = div64_s64(p, var1);
3582e419aecSStefan Tatschner 	var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
3592e419aecSStefan Tatschner 	var2 = ((s64)(calib->P8) * p) >> 19;
3602e419aecSStefan Tatschner 	p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
36114e8015fSLinus Walleij 
36214e8015fSLinus Walleij 	return (u32)p;
36314e8015fSLinus Walleij }
36414e8015fSLinus Walleij 
bmp280_read_temp(struct bmp280_data * data,int * val,int * val2)36514e8015fSLinus Walleij static int bmp280_read_temp(struct bmp280_data *data,
366597dfb2aSAngel Iglesias 			    int *val, int *val2)
36714e8015fSLinus Walleij {
36814e8015fSLinus Walleij 	s32 adc_temp, comp_temp;
3695f0c359dSAngel Iglesias 	int ret;
37014e8015fSLinus Walleij 
371327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
372327b5c05SAngel Iglesias 			       data->buf, sizeof(data->buf));
37314e8015fSLinus Walleij 	if (ret < 0) {
37414e8015fSLinus Walleij 		dev_err(data->dev, "failed to read temperature\n");
37514e8015fSLinus Walleij 		return ret;
37614e8015fSLinus Walleij 	}
37714e8015fSLinus Walleij 
378327b5c05SAngel Iglesias 	adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
379eb92b418SAndreas Klinger 	if (adc_temp == BMP280_TEMP_SKIPPED) {
380eb92b418SAndreas Klinger 		/* reading was skipped */
381eb92b418SAndreas Klinger 		dev_err(data->dev, "reading temperature skipped\n");
382eb92b418SAndreas Klinger 		return -EIO;
383eb92b418SAndreas Klinger 	}
38414e8015fSLinus Walleij 	comp_temp = bmp280_compensate_temp(data, adc_temp);
38514e8015fSLinus Walleij 
38614e8015fSLinus Walleij 	/*
38714e8015fSLinus Walleij 	 * val might be NULL if we're called by the read_press routine,
38814e8015fSLinus Walleij 	 * who only cares about the carry over t_fine value.
38914e8015fSLinus Walleij 	 */
39014e8015fSLinus Walleij 	if (val) {
39114e8015fSLinus Walleij 		*val = comp_temp * 10;
39214e8015fSLinus Walleij 		return IIO_VAL_INT;
39314e8015fSLinus Walleij 	}
39414e8015fSLinus Walleij 
39514e8015fSLinus Walleij 	return 0;
39614e8015fSLinus Walleij }
39714e8015fSLinus Walleij 
bmp280_read_press(struct bmp280_data * data,int * val,int * val2)39814e8015fSLinus Walleij static int bmp280_read_press(struct bmp280_data *data,
39914e8015fSLinus Walleij 			     int *val, int *val2)
40014e8015fSLinus Walleij {
4015f0c359dSAngel Iglesias 	u32 comp_press;
40214e8015fSLinus Walleij 	s32 adc_press;
4035f0c359dSAngel Iglesias 	int ret;
40414e8015fSLinus Walleij 
40514e8015fSLinus Walleij 	/* Read and compensate temperature so we get a reading of t_fine. */
406597dfb2aSAngel Iglesias 	ret = bmp280_read_temp(data, NULL, NULL);
40714e8015fSLinus Walleij 	if (ret < 0)
40814e8015fSLinus Walleij 		return ret;
40914e8015fSLinus Walleij 
410327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
411327b5c05SAngel Iglesias 			       data->buf, sizeof(data->buf));
41214e8015fSLinus Walleij 	if (ret < 0) {
41314e8015fSLinus Walleij 		dev_err(data->dev, "failed to read pressure\n");
41414e8015fSLinus Walleij 		return ret;
41514e8015fSLinus Walleij 	}
41614e8015fSLinus Walleij 
417327b5c05SAngel Iglesias 	adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
418eb92b418SAndreas Klinger 	if (adc_press == BMP280_PRESS_SKIPPED) {
419eb92b418SAndreas Klinger 		/* reading was skipped */
420eb92b418SAndreas Klinger 		dev_err(data->dev, "reading pressure skipped\n");
421eb92b418SAndreas Klinger 		return -EIO;
422eb92b418SAndreas Klinger 	}
42314e8015fSLinus Walleij 	comp_press = bmp280_compensate_press(data, adc_press);
42414e8015fSLinus Walleij 
42514e8015fSLinus Walleij 	*val = comp_press;
42614e8015fSLinus Walleij 	*val2 = 256000;
42714e8015fSLinus Walleij 
42814e8015fSLinus Walleij 	return IIO_VAL_FRACTIONAL;
42914e8015fSLinus Walleij }
43014e8015fSLinus Walleij 
bmp280_read_humid(struct bmp280_data * data,int * val,int * val2)43114e8015fSLinus Walleij static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
43214e8015fSLinus Walleij {
4335f0c359dSAngel Iglesias 	u32 comp_humidity;
4345f0c359dSAngel Iglesias 	s32 adc_humidity;
43514e8015fSLinus Walleij 	int ret;
43614e8015fSLinus Walleij 
43714e8015fSLinus Walleij 	/* Read and compensate temperature so we get a reading of t_fine. */
438597dfb2aSAngel Iglesias 	ret = bmp280_read_temp(data, NULL, NULL);
43914e8015fSLinus Walleij 	if (ret < 0)
44014e8015fSLinus Walleij 		return ret;
44114e8015fSLinus Walleij 
442327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
443327b5c05SAngel Iglesias 			       &data->be16, sizeof(data->be16));
44414e8015fSLinus Walleij 	if (ret < 0) {
44514e8015fSLinus Walleij 		dev_err(data->dev, "failed to read humidity\n");
44614e8015fSLinus Walleij 		return ret;
44714e8015fSLinus Walleij 	}
44814e8015fSLinus Walleij 
449327b5c05SAngel Iglesias 	adc_humidity = be16_to_cpu(data->be16);
450eb92b418SAndreas Klinger 	if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
451eb92b418SAndreas Klinger 		/* reading was skipped */
452eb92b418SAndreas Klinger 		dev_err(data->dev, "reading humidity skipped\n");
453eb92b418SAndreas Klinger 		return -EIO;
454eb92b418SAndreas Klinger 	}
45514e8015fSLinus Walleij 	comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
45614e8015fSLinus Walleij 
45713399ff2STomasz Duszynski 	*val = comp_humidity * 1000 / 1024;
45814e8015fSLinus Walleij 
45913399ff2STomasz Duszynski 	return IIO_VAL_INT;
46014e8015fSLinus Walleij }
46114e8015fSLinus Walleij 
bmp280_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)46214e8015fSLinus Walleij static int bmp280_read_raw(struct iio_dev *indio_dev,
46314e8015fSLinus Walleij 			   struct iio_chan_spec const *chan,
46414e8015fSLinus Walleij 			   int *val, int *val2, long mask)
46514e8015fSLinus Walleij {
46614e8015fSLinus Walleij 	struct bmp280_data *data = iio_priv(indio_dev);
4675f0c359dSAngel Iglesias 	int ret;
46814e8015fSLinus Walleij 
4693d838118SLinus Walleij 	pm_runtime_get_sync(data->dev);
47014e8015fSLinus Walleij 	mutex_lock(&data->lock);
47114e8015fSLinus Walleij 
47214e8015fSLinus Walleij 	switch (mask) {
47314e8015fSLinus Walleij 	case IIO_CHAN_INFO_PROCESSED:
47414e8015fSLinus Walleij 		switch (chan->type) {
47514e8015fSLinus Walleij 		case IIO_HUMIDITYRELATIVE:
47614e8015fSLinus Walleij 			ret = data->chip_info->read_humid(data, val, val2);
47714e8015fSLinus Walleij 			break;
47814e8015fSLinus Walleij 		case IIO_PRESSURE:
47914e8015fSLinus Walleij 			ret = data->chip_info->read_press(data, val, val2);
48014e8015fSLinus Walleij 			break;
48114e8015fSLinus Walleij 		case IIO_TEMP:
482597dfb2aSAngel Iglesias 			ret = data->chip_info->read_temp(data, val, val2);
48314e8015fSLinus Walleij 			break;
48414e8015fSLinus Walleij 		default:
48514e8015fSLinus Walleij 			ret = -EINVAL;
48614e8015fSLinus Walleij 			break;
48714e8015fSLinus Walleij 		}
48814e8015fSLinus Walleij 		break;
48914e8015fSLinus Walleij 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
49014e8015fSLinus Walleij 		switch (chan->type) {
49114e8015fSLinus Walleij 		case IIO_HUMIDITYRELATIVE:
49214e8015fSLinus Walleij 			*val = 1 << data->oversampling_humid;
49314e8015fSLinus Walleij 			ret = IIO_VAL_INT;
49414e8015fSLinus Walleij 			break;
49514e8015fSLinus Walleij 		case IIO_PRESSURE:
49614e8015fSLinus Walleij 			*val = 1 << data->oversampling_press;
49714e8015fSLinus Walleij 			ret = IIO_VAL_INT;
49814e8015fSLinus Walleij 			break;
49914e8015fSLinus Walleij 		case IIO_TEMP:
50014e8015fSLinus Walleij 			*val = 1 << data->oversampling_temp;
50114e8015fSLinus Walleij 			ret = IIO_VAL_INT;
50214e8015fSLinus Walleij 			break;
50314e8015fSLinus Walleij 		default:
50414e8015fSLinus Walleij 			ret = -EINVAL;
50514e8015fSLinus Walleij 			break;
50614e8015fSLinus Walleij 		}
50714e8015fSLinus Walleij 		break;
50810b40ffbSAngel Iglesias 	case IIO_CHAN_INFO_SAMP_FREQ:
50910b40ffbSAngel Iglesias 		if (!data->chip_info->sampling_freq_avail) {
51010b40ffbSAngel Iglesias 			ret = -EINVAL;
51110b40ffbSAngel Iglesias 			break;
51210b40ffbSAngel Iglesias 		}
51310b40ffbSAngel Iglesias 
51410b40ffbSAngel Iglesias 		*val = data->chip_info->sampling_freq_avail[data->sampling_freq][0];
51510b40ffbSAngel Iglesias 		*val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1];
51610b40ffbSAngel Iglesias 		ret = IIO_VAL_INT_PLUS_MICRO;
51710b40ffbSAngel Iglesias 		break;
51810b40ffbSAngel Iglesias 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
51910b40ffbSAngel Iglesias 		if (!data->chip_info->iir_filter_coeffs_avail) {
52010b40ffbSAngel Iglesias 			ret = -EINVAL;
52110b40ffbSAngel Iglesias 			break;
52210b40ffbSAngel Iglesias 		}
52310b40ffbSAngel Iglesias 
52410b40ffbSAngel Iglesias 		*val = (1 << data->iir_filter_coeff) - 1;
52510b40ffbSAngel Iglesias 		ret = IIO_VAL_INT;
52610b40ffbSAngel Iglesias 		break;
52714e8015fSLinus Walleij 	default:
52814e8015fSLinus Walleij 		ret = -EINVAL;
52914e8015fSLinus Walleij 		break;
53014e8015fSLinus Walleij 	}
53114e8015fSLinus Walleij 
53214e8015fSLinus Walleij 	mutex_unlock(&data->lock);
5333d838118SLinus Walleij 	pm_runtime_mark_last_busy(data->dev);
5343d838118SLinus Walleij 	pm_runtime_put_autosuspend(data->dev);
53514e8015fSLinus Walleij 
53614e8015fSLinus Walleij 	return ret;
53714e8015fSLinus Walleij }
53814e8015fSLinus Walleij 
bmp280_write_oversampling_ratio_humid(struct bmp280_data * data,int val)53914e8015fSLinus Walleij static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
54014e8015fSLinus Walleij 					       int val)
54114e8015fSLinus Walleij {
54214e8015fSLinus Walleij 	const int *avail = data->chip_info->oversampling_humid_avail;
54314e8015fSLinus Walleij 	const int n = data->chip_info->num_oversampling_humid_avail;
54410b40ffbSAngel Iglesias 	int ret, prev;
5455f0c359dSAngel Iglesias 	int i;
54614e8015fSLinus Walleij 
54714e8015fSLinus Walleij 	for (i = 0; i < n; i++) {
54814e8015fSLinus Walleij 		if (avail[i] == val) {
54910b40ffbSAngel Iglesias 			prev = data->oversampling_humid;
55014e8015fSLinus Walleij 			data->oversampling_humid = ilog2(val);
55114e8015fSLinus Walleij 
55210b40ffbSAngel Iglesias 			ret = data->chip_info->chip_config(data);
55310b40ffbSAngel Iglesias 			if (ret) {
55410b40ffbSAngel Iglesias 				data->oversampling_humid = prev;
55510b40ffbSAngel Iglesias 				data->chip_info->chip_config(data);
55610b40ffbSAngel Iglesias 				return ret;
55710b40ffbSAngel Iglesias 			}
55810b40ffbSAngel Iglesias 			return 0;
55914e8015fSLinus Walleij 		}
56014e8015fSLinus Walleij 	}
56114e8015fSLinus Walleij 	return -EINVAL;
56214e8015fSLinus Walleij }
56314e8015fSLinus Walleij 
bmp280_write_oversampling_ratio_temp(struct bmp280_data * data,int val)56414e8015fSLinus Walleij static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
56514e8015fSLinus Walleij 					       int val)
56614e8015fSLinus Walleij {
56714e8015fSLinus Walleij 	const int *avail = data->chip_info->oversampling_temp_avail;
56814e8015fSLinus Walleij 	const int n = data->chip_info->num_oversampling_temp_avail;
56910b40ffbSAngel Iglesias 	int ret, prev;
5705f0c359dSAngel Iglesias 	int i;
57114e8015fSLinus Walleij 
57214e8015fSLinus Walleij 	for (i = 0; i < n; i++) {
57314e8015fSLinus Walleij 		if (avail[i] == val) {
57410b40ffbSAngel Iglesias 			prev = data->oversampling_temp;
57514e8015fSLinus Walleij 			data->oversampling_temp = ilog2(val);
57614e8015fSLinus Walleij 
57710b40ffbSAngel Iglesias 			ret = data->chip_info->chip_config(data);
57810b40ffbSAngel Iglesias 			if (ret) {
57910b40ffbSAngel Iglesias 				data->oversampling_temp = prev;
58010b40ffbSAngel Iglesias 				data->chip_info->chip_config(data);
58110b40ffbSAngel Iglesias 				return ret;
58210b40ffbSAngel Iglesias 			}
58310b40ffbSAngel Iglesias 			return 0;
58414e8015fSLinus Walleij 		}
58514e8015fSLinus Walleij 	}
58614e8015fSLinus Walleij 	return -EINVAL;
58714e8015fSLinus Walleij }
58814e8015fSLinus Walleij 
bmp280_write_oversampling_ratio_press(struct bmp280_data * data,int val)58914e8015fSLinus Walleij static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
59014e8015fSLinus Walleij 					       int val)
59114e8015fSLinus Walleij {
59214e8015fSLinus Walleij 	const int *avail = data->chip_info->oversampling_press_avail;
59314e8015fSLinus Walleij 	const int n = data->chip_info->num_oversampling_press_avail;
59410b40ffbSAngel Iglesias 	int ret, prev;
5955f0c359dSAngel Iglesias 	int i;
59614e8015fSLinus Walleij 
59714e8015fSLinus Walleij 	for (i = 0; i < n; i++) {
59814e8015fSLinus Walleij 		if (avail[i] == val) {
59910b40ffbSAngel Iglesias 			prev = data->oversampling_press;
60014e8015fSLinus Walleij 			data->oversampling_press = ilog2(val);
60114e8015fSLinus Walleij 
60210b40ffbSAngel Iglesias 			ret = data->chip_info->chip_config(data);
60310b40ffbSAngel Iglesias 			if (ret) {
60410b40ffbSAngel Iglesias 				data->oversampling_press = prev;
60510b40ffbSAngel Iglesias 				data->chip_info->chip_config(data);
60610b40ffbSAngel Iglesias 				return ret;
60710b40ffbSAngel Iglesias 			}
60810b40ffbSAngel Iglesias 			return 0;
60910b40ffbSAngel Iglesias 		}
61010b40ffbSAngel Iglesias 	}
61110b40ffbSAngel Iglesias 	return -EINVAL;
61210b40ffbSAngel Iglesias }
61310b40ffbSAngel Iglesias 
bmp280_write_sampling_frequency(struct bmp280_data * data,int val,int val2)61410b40ffbSAngel Iglesias static int bmp280_write_sampling_frequency(struct bmp280_data *data,
61510b40ffbSAngel Iglesias 					   int val, int val2)
61610b40ffbSAngel Iglesias {
61710b40ffbSAngel Iglesias 	const int (*avail)[2] = data->chip_info->sampling_freq_avail;
61810b40ffbSAngel Iglesias 	const int n = data->chip_info->num_sampling_freq_avail;
61910b40ffbSAngel Iglesias 	int ret, prev;
62010b40ffbSAngel Iglesias 	int i;
62110b40ffbSAngel Iglesias 
62210b40ffbSAngel Iglesias 	for (i = 0; i < n; i++) {
62310b40ffbSAngel Iglesias 		if (avail[i][0] == val && avail[i][1] == val2) {
62410b40ffbSAngel Iglesias 			prev = data->sampling_freq;
62510b40ffbSAngel Iglesias 			data->sampling_freq = i;
62610b40ffbSAngel Iglesias 
62710b40ffbSAngel Iglesias 			ret = data->chip_info->chip_config(data);
62810b40ffbSAngel Iglesias 			if (ret) {
62910b40ffbSAngel Iglesias 				data->sampling_freq = prev;
63010b40ffbSAngel Iglesias 				data->chip_info->chip_config(data);
63110b40ffbSAngel Iglesias 				return ret;
63210b40ffbSAngel Iglesias 			}
63310b40ffbSAngel Iglesias 			return 0;
63410b40ffbSAngel Iglesias 		}
63510b40ffbSAngel Iglesias 	}
63610b40ffbSAngel Iglesias 	return -EINVAL;
63710b40ffbSAngel Iglesias }
63810b40ffbSAngel Iglesias 
bmp280_write_iir_filter_coeffs(struct bmp280_data * data,int val)63910b40ffbSAngel Iglesias static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val)
64010b40ffbSAngel Iglesias {
64110b40ffbSAngel Iglesias 	const int *avail = data->chip_info->iir_filter_coeffs_avail;
64210b40ffbSAngel Iglesias 	const int n = data->chip_info->num_iir_filter_coeffs_avail;
64310b40ffbSAngel Iglesias 	int ret, prev;
64410b40ffbSAngel Iglesias 	int i;
64510b40ffbSAngel Iglesias 
64610b40ffbSAngel Iglesias 	for (i = 0; i < n; i++) {
64710b40ffbSAngel Iglesias 		if (avail[i] - 1  == val) {
64810b40ffbSAngel Iglesias 			prev = data->iir_filter_coeff;
64910b40ffbSAngel Iglesias 			data->iir_filter_coeff = i;
65010b40ffbSAngel Iglesias 
65110b40ffbSAngel Iglesias 			ret = data->chip_info->chip_config(data);
65210b40ffbSAngel Iglesias 			if (ret) {
65310b40ffbSAngel Iglesias 				data->iir_filter_coeff = prev;
65410b40ffbSAngel Iglesias 				data->chip_info->chip_config(data);
65510b40ffbSAngel Iglesias 				return ret;
65610b40ffbSAngel Iglesias 
65710b40ffbSAngel Iglesias 			}
65810b40ffbSAngel Iglesias 			return 0;
65914e8015fSLinus Walleij 		}
66014e8015fSLinus Walleij 	}
66114e8015fSLinus Walleij 	return -EINVAL;
66214e8015fSLinus Walleij }
66314e8015fSLinus Walleij 
bmp280_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)66414e8015fSLinus Walleij static int bmp280_write_raw(struct iio_dev *indio_dev,
66514e8015fSLinus Walleij 			    struct iio_chan_spec const *chan,
66614e8015fSLinus Walleij 			    int val, int val2, long mask)
66714e8015fSLinus Walleij {
66814e8015fSLinus Walleij 	struct bmp280_data *data = iio_priv(indio_dev);
6695f0c359dSAngel Iglesias 	int ret = 0;
67014e8015fSLinus Walleij 
67110b40ffbSAngel Iglesias 	/*
67210b40ffbSAngel Iglesias 	 * Helper functions to update sensor running configuration.
67310b40ffbSAngel Iglesias 	 * If an error happens applying new settings, will try restore
67410b40ffbSAngel Iglesias 	 * previous parameters to ensure the sensor is left in a known
67510b40ffbSAngel Iglesias 	 * working configuration.
67610b40ffbSAngel Iglesias 	 */
67714e8015fSLinus Walleij 	switch (mask) {
67814e8015fSLinus Walleij 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
6793d838118SLinus Walleij 		pm_runtime_get_sync(data->dev);
68014e8015fSLinus Walleij 		mutex_lock(&data->lock);
68114e8015fSLinus Walleij 		switch (chan->type) {
68214e8015fSLinus Walleij 		case IIO_HUMIDITYRELATIVE:
68314e8015fSLinus Walleij 			ret = bmp280_write_oversampling_ratio_humid(data, val);
68414e8015fSLinus Walleij 			break;
68514e8015fSLinus Walleij 		case IIO_PRESSURE:
68614e8015fSLinus Walleij 			ret = bmp280_write_oversampling_ratio_press(data, val);
68714e8015fSLinus Walleij 			break;
68814e8015fSLinus Walleij 		case IIO_TEMP:
68914e8015fSLinus Walleij 			ret = bmp280_write_oversampling_ratio_temp(data, val);
69014e8015fSLinus Walleij 			break;
69114e8015fSLinus Walleij 		default:
69214e8015fSLinus Walleij 			ret = -EINVAL;
69314e8015fSLinus Walleij 			break;
69414e8015fSLinus Walleij 		}
69514e8015fSLinus Walleij 		mutex_unlock(&data->lock);
6963d838118SLinus Walleij 		pm_runtime_mark_last_busy(data->dev);
6973d838118SLinus Walleij 		pm_runtime_put_autosuspend(data->dev);
69814e8015fSLinus Walleij 		break;
69910b40ffbSAngel Iglesias 	case IIO_CHAN_INFO_SAMP_FREQ:
70010b40ffbSAngel Iglesias 		pm_runtime_get_sync(data->dev);
70110b40ffbSAngel Iglesias 		mutex_lock(&data->lock);
70210b40ffbSAngel Iglesias 		ret = bmp280_write_sampling_frequency(data, val, val2);
70310b40ffbSAngel Iglesias 		mutex_unlock(&data->lock);
70410b40ffbSAngel Iglesias 		pm_runtime_mark_last_busy(data->dev);
70510b40ffbSAngel Iglesias 		pm_runtime_put_autosuspend(data->dev);
70610b40ffbSAngel Iglesias 		break;
70710b40ffbSAngel Iglesias 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
70810b40ffbSAngel Iglesias 		pm_runtime_get_sync(data->dev);
70910b40ffbSAngel Iglesias 		mutex_lock(&data->lock);
71010b40ffbSAngel Iglesias 		ret = bmp280_write_iir_filter_coeffs(data, val);
71110b40ffbSAngel Iglesias 		mutex_unlock(&data->lock);
71210b40ffbSAngel Iglesias 		pm_runtime_mark_last_busy(data->dev);
71310b40ffbSAngel Iglesias 		pm_runtime_put_autosuspend(data->dev);
71410b40ffbSAngel Iglesias 		break;
71514e8015fSLinus Walleij 	default:
71614e8015fSLinus Walleij 		return -EINVAL;
71714e8015fSLinus Walleij 	}
71814e8015fSLinus Walleij 
71914e8015fSLinus Walleij 	return ret;
72014e8015fSLinus Walleij }
72114e8015fSLinus Walleij 
bmp280_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)7226085102cSAndy Shevchenko static int bmp280_read_avail(struct iio_dev *indio_dev,
7236085102cSAndy Shevchenko 			     struct iio_chan_spec const *chan,
7246085102cSAndy Shevchenko 			     const int **vals, int *type, int *length,
7256085102cSAndy Shevchenko 			     long mask)
72614e8015fSLinus Walleij {
7276085102cSAndy Shevchenko 	struct bmp280_data *data = iio_priv(indio_dev);
72814e8015fSLinus Walleij 
7296085102cSAndy Shevchenko 	switch (mask) {
7306085102cSAndy Shevchenko 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
7316085102cSAndy Shevchenko 		switch (chan->type) {
7326085102cSAndy Shevchenko 		case IIO_PRESSURE:
7336085102cSAndy Shevchenko 			*vals = data->chip_info->oversampling_press_avail;
7346085102cSAndy Shevchenko 			*length = data->chip_info->num_oversampling_press_avail;
7356085102cSAndy Shevchenko 			break;
7366085102cSAndy Shevchenko 		case IIO_TEMP:
7376085102cSAndy Shevchenko 			*vals = data->chip_info->oversampling_temp_avail;
7386085102cSAndy Shevchenko 			*length = data->chip_info->num_oversampling_temp_avail;
7396085102cSAndy Shevchenko 			break;
7406085102cSAndy Shevchenko 		default:
7416085102cSAndy Shevchenko 			return -EINVAL;
74214e8015fSLinus Walleij 		}
7436085102cSAndy Shevchenko 		*type = IIO_VAL_INT;
7446085102cSAndy Shevchenko 		return IIO_AVAIL_LIST;
74510b40ffbSAngel Iglesias 	case IIO_CHAN_INFO_SAMP_FREQ:
74610b40ffbSAngel Iglesias 		*vals = (const int *)data->chip_info->sampling_freq_avail;
74710b40ffbSAngel Iglesias 		*type = IIO_VAL_INT_PLUS_MICRO;
74810b40ffbSAngel Iglesias 		/* Values are stored in a 2D matrix */
74910b40ffbSAngel Iglesias 		*length = data->chip_info->num_sampling_freq_avail;
75010b40ffbSAngel Iglesias 		return IIO_AVAIL_LIST;
75110b40ffbSAngel Iglesias 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
75210b40ffbSAngel Iglesias 		*vals = data->chip_info->iir_filter_coeffs_avail;
75310b40ffbSAngel Iglesias 		*type = IIO_VAL_INT;
75410b40ffbSAngel Iglesias 		*length = data->chip_info->num_iir_filter_coeffs_avail;
75510b40ffbSAngel Iglesias 		return IIO_AVAIL_LIST;
7566085102cSAndy Shevchenko 	default:
7576085102cSAndy Shevchenko 		return -EINVAL;
75814e8015fSLinus Walleij 	}
75914e8015fSLinus Walleij }
76014e8015fSLinus Walleij 
76114e8015fSLinus Walleij static const struct iio_info bmp280_info = {
76214e8015fSLinus Walleij 	.read_raw = &bmp280_read_raw,
7636085102cSAndy Shevchenko 	.read_avail = &bmp280_read_avail,
76414e8015fSLinus Walleij 	.write_raw = &bmp280_write_raw,
76514e8015fSLinus Walleij };
76614e8015fSLinus Walleij 
bmp280_chip_config(struct bmp280_data * data)76714e8015fSLinus Walleij static int bmp280_chip_config(struct bmp280_data *data)
76814e8015fSLinus Walleij {
7692405f8ccSAngel Iglesias 	u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) |
7702405f8ccSAngel Iglesias 		  FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1);
7715f0c359dSAngel Iglesias 	int ret;
77214e8015fSLinus Walleij 
7734b1f0c31SColin Parker 	ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
77414e8015fSLinus Walleij 				 BMP280_OSRS_TEMP_MASK |
77514e8015fSLinus Walleij 				 BMP280_OSRS_PRESS_MASK |
77614e8015fSLinus Walleij 				 BMP280_MODE_MASK,
77714e8015fSLinus Walleij 				 osrs | BMP280_MODE_NORMAL);
77814e8015fSLinus Walleij 	if (ret < 0) {
77914e8015fSLinus Walleij 		dev_err(data->dev,
78014e8015fSLinus Walleij 			"failed to write ctrl_meas register\n");
78114e8015fSLinus Walleij 		return ret;
78214e8015fSLinus Walleij 	}
78314e8015fSLinus Walleij 
78414e8015fSLinus Walleij 	ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
78514e8015fSLinus Walleij 				 BMP280_FILTER_MASK,
78614e8015fSLinus Walleij 				 BMP280_FILTER_4X);
78714e8015fSLinus Walleij 	if (ret < 0) {
78814e8015fSLinus Walleij 		dev_err(data->dev,
78914e8015fSLinus Walleij 			"failed to write config register\n");
79014e8015fSLinus Walleij 		return ret;
79114e8015fSLinus Walleij 	}
79214e8015fSLinus Walleij 
79314e8015fSLinus Walleij 	return ret;
79414e8015fSLinus Walleij }
79514e8015fSLinus Walleij 
79614e8015fSLinus Walleij static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
79714e8015fSLinus Walleij 
7980b0b7726SAngel Iglesias const struct bmp280_chip_info bmp280_chip_info = {
799b00e805aSAngel Iglesias 	.id_reg = BMP280_REG_ID,
8000b0b7726SAngel Iglesias 	.chip_id = BMP280_CHIP_ID,
8010b0b7726SAngel Iglesias 	.regmap_config = &bmp280_regmap_config,
802b00e805aSAngel Iglesias 	.start_up_time = 2000,
80310b40ffbSAngel Iglesias 	.channels = bmp280_channels,
804b00e805aSAngel Iglesias 	.num_channels = 2,
805b00e805aSAngel Iglesias 
80614e8015fSLinus Walleij 	.oversampling_temp_avail = bmp280_oversampling_avail,
80714e8015fSLinus Walleij 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
808b00e805aSAngel Iglesias 	/*
809b00e805aSAngel Iglesias 	 * Oversampling config values on BMx280 have one additional setting
810b00e805aSAngel Iglesias 	 * that other generations of the family don't:
811b00e805aSAngel Iglesias 	 * The value 0 means the measurement is bypassed instead of
812b00e805aSAngel Iglesias 	 * oversampling set to x1.
813b00e805aSAngel Iglesias 	 *
814b00e805aSAngel Iglesias 	 * To account for this difference, and preserve the same common
815b00e805aSAngel Iglesias 	 * config logic, this is handled later on chip_config callback
816b00e805aSAngel Iglesias 	 * incrementing one unit the oversampling setting.
817b00e805aSAngel Iglesias 	 */
818b00e805aSAngel Iglesias 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
81914e8015fSLinus Walleij 
82014e8015fSLinus Walleij 	.oversampling_press_avail = bmp280_oversampling_avail,
82114e8015fSLinus Walleij 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
822b00e805aSAngel Iglesias 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
82314e8015fSLinus Walleij 
82414e8015fSLinus Walleij 	.chip_config = bmp280_chip_config,
82514e8015fSLinus Walleij 	.read_temp = bmp280_read_temp,
82614e8015fSLinus Walleij 	.read_press = bmp280_read_press,
827b00e805aSAngel Iglesias 	.read_calib = bmp280_read_calib,
82814e8015fSLinus Walleij };
8290b0b7726SAngel Iglesias EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280);
83014e8015fSLinus Walleij 
bme280_chip_config(struct bmp280_data * data)83114e8015fSLinus Walleij static int bme280_chip_config(struct bmp280_data *data)
83214e8015fSLinus Walleij {
8332405f8ccSAngel Iglesias 	u8 osrs = FIELD_PREP(BMP280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1);
8345f0c359dSAngel Iglesias 	int ret;
83514e8015fSLinus Walleij 
836eb92b418SAndreas Klinger 	/*
837eb92b418SAndreas Klinger 	 * Oversampling of humidity must be set before oversampling of
838eb92b418SAndreas Klinger 	 * temperature/pressure is set to become effective.
839eb92b418SAndreas Klinger 	 */
840eb92b418SAndreas Klinger 	ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
841eb92b418SAndreas Klinger 				  BMP280_OSRS_HUMIDITY_MASK, osrs);
842eb92b418SAndreas Klinger 
84314e8015fSLinus Walleij 	if (ret < 0)
84414e8015fSLinus Walleij 		return ret;
84514e8015fSLinus Walleij 
846eb92b418SAndreas Klinger 	return bmp280_chip_config(data);
84714e8015fSLinus Walleij }
84814e8015fSLinus Walleij 
8490b0b7726SAngel Iglesias const struct bmp280_chip_info bme280_chip_info = {
850b00e805aSAngel Iglesias 	.id_reg = BMP280_REG_ID,
8510b0b7726SAngel Iglesias 	.chip_id = BME280_CHIP_ID,
8520b0b7726SAngel Iglesias 	.regmap_config = &bmp280_regmap_config,
853b00e805aSAngel Iglesias 	.start_up_time = 2000,
85410b40ffbSAngel Iglesias 	.channels = bmp280_channels,
855b00e805aSAngel Iglesias 	.num_channels = 3,
856b00e805aSAngel Iglesias 
85714e8015fSLinus Walleij 	.oversampling_temp_avail = bmp280_oversampling_avail,
85814e8015fSLinus Walleij 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
859b00e805aSAngel Iglesias 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
86014e8015fSLinus Walleij 
86114e8015fSLinus Walleij 	.oversampling_press_avail = bmp280_oversampling_avail,
86214e8015fSLinus Walleij 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
863b00e805aSAngel Iglesias 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
86414e8015fSLinus Walleij 
86514e8015fSLinus Walleij 	.oversampling_humid_avail = bmp280_oversampling_avail,
86614e8015fSLinus Walleij 	.num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
867b00e805aSAngel Iglesias 	.oversampling_humid_default = BMP280_OSRS_HUMIDITY_16X - 1,
86814e8015fSLinus Walleij 
86914e8015fSLinus Walleij 	.chip_config = bme280_chip_config,
87014e8015fSLinus Walleij 	.read_temp = bmp280_read_temp,
87114e8015fSLinus Walleij 	.read_press = bmp280_read_press,
87214e8015fSLinus Walleij 	.read_humid = bmp280_read_humid,
873b00e805aSAngel Iglesias 	.read_calib = bme280_read_calib,
87414e8015fSLinus Walleij };
8750b0b7726SAngel Iglesias EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280);
87614e8015fSLinus Walleij 
8778d329309SAngel Iglesias /*
8788d329309SAngel Iglesias  * Helper function to send a command to BMP3XX sensors.
8798d329309SAngel Iglesias  *
8808d329309SAngel Iglesias  * Sensor processes commands written to the CMD register and signals
8818d329309SAngel Iglesias  * execution result through "cmd_rdy" and "cmd_error" flags available on
8828d329309SAngel Iglesias  * STATUS and ERROR registers.
8838d329309SAngel Iglesias  */
bmp380_cmd(struct bmp280_data * data,u8 cmd)8848d329309SAngel Iglesias static int bmp380_cmd(struct bmp280_data *data, u8 cmd)
8858d329309SAngel Iglesias {
8868d329309SAngel Iglesias 	unsigned int reg;
8878d329309SAngel Iglesias 	int ret;
8888d329309SAngel Iglesias 
8898d329309SAngel Iglesias 	/* Check if device is ready to process a command */
8908d329309SAngel Iglesias 	ret = regmap_read(data->regmap, BMP380_REG_STATUS, &reg);
8918d329309SAngel Iglesias 	if (ret) {
8928d329309SAngel Iglesias 		dev_err(data->dev, "failed to read error register\n");
8938d329309SAngel Iglesias 		return ret;
8948d329309SAngel Iglesias 	}
8958d329309SAngel Iglesias 	if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) {
8968d329309SAngel Iglesias 		dev_err(data->dev, "device is not ready to accept commands\n");
8978d329309SAngel Iglesias 		return -EBUSY;
8988d329309SAngel Iglesias 	}
8998d329309SAngel Iglesias 
9008d329309SAngel Iglesias 	/* Send command to process */
9018d329309SAngel Iglesias 	ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd);
9028d329309SAngel Iglesias 	if (ret) {
9038d329309SAngel Iglesias 		dev_err(data->dev, "failed to send command to device\n");
9048d329309SAngel Iglesias 		return ret;
9058d329309SAngel Iglesias 	}
9068d329309SAngel Iglesias 	/* Wait for 2ms for command to be processed */
9078d329309SAngel Iglesias 	usleep_range(data->start_up_time, data->start_up_time + 100);
9088d329309SAngel Iglesias 	/* Check for command processing error */
9098d329309SAngel Iglesias 	ret = regmap_read(data->regmap, BMP380_REG_ERROR, &reg);
9108d329309SAngel Iglesias 	if (ret) {
9118d329309SAngel Iglesias 		dev_err(data->dev, "error reading ERROR reg\n");
9128d329309SAngel Iglesias 		return ret;
9138d329309SAngel Iglesias 	}
9148d329309SAngel Iglesias 	if (reg & BMP380_ERR_CMD_MASK) {
9158d329309SAngel Iglesias 		dev_err(data->dev, "error processing command 0x%X\n", cmd);
9168d329309SAngel Iglesias 		return -EINVAL;
9178d329309SAngel Iglesias 	}
9188d329309SAngel Iglesias 
9198d329309SAngel Iglesias 	return 0;
9208d329309SAngel Iglesias }
9218d329309SAngel Iglesias 
9228d329309SAngel Iglesias /*
9238d329309SAngel Iglesias  * Returns temperature in Celsius dregrees, resolution is 0.01º C. Output value of
9248d329309SAngel Iglesias  * "5123" equals 51.2º C. t_fine carries fine temperature as global value.
9258d329309SAngel Iglesias  *
9268d329309SAngel Iglesias  * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo
9278d329309SAngel Iglesias  * https://github.com/BoschSensortec/BMP3-Sensor-API.
9288d329309SAngel Iglesias  */
bmp380_compensate_temp(struct bmp280_data * data,u32 adc_temp)9298d329309SAngel Iglesias static s32 bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp)
9308d329309SAngel Iglesias {
9318d329309SAngel Iglesias 	s64 var1, var2, var3, var4, var5, var6, comp_temp;
9328d329309SAngel Iglesias 	struct bmp380_calib *calib = &data->calib.bmp380;
9338d329309SAngel Iglesias 
9348d329309SAngel Iglesias 	var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8);
9358d329309SAngel Iglesias 	var2 = var1 * ((s64) calib->T2);
9368d329309SAngel Iglesias 	var3 = var1 * var1;
9378d329309SAngel Iglesias 	var4 = var3 * ((s64) calib->T3);
9388d329309SAngel Iglesias 	var5 = (var2 << 18) + var4;
9398d329309SAngel Iglesias 	var6 = var5 >> 32;
9408d329309SAngel Iglesias 	data->t_fine = (s32) var6;
9418d329309SAngel Iglesias 	comp_temp = (var6 * 25) >> 14;
9428d329309SAngel Iglesias 
9438d329309SAngel Iglesias 	comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP);
9448d329309SAngel Iglesias 	return (s32) comp_temp;
9458d329309SAngel Iglesias }
9468d329309SAngel Iglesias 
9478d329309SAngel Iglesias /*
9488d329309SAngel Iglesias  * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal.
9498d329309SAngel Iglesias  * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa.
9508d329309SAngel Iglesias  *
9518d329309SAngel Iglesias  * Taken from datasheet, Section 9.3. "Pressure compensation" and repository
9528d329309SAngel Iglesias  * https://github.com/BoschSensortec/BMP3-Sensor-API.
9538d329309SAngel Iglesias  */
bmp380_compensate_press(struct bmp280_data * data,u32 adc_press)9548d329309SAngel Iglesias static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press)
9558d329309SAngel Iglesias {
9568d329309SAngel Iglesias 	s64 var1, var2, var3, var4, var5, var6, offset, sensitivity;
9578d329309SAngel Iglesias 	struct bmp380_calib *calib = &data->calib.bmp380;
9588d329309SAngel Iglesias 	u32 comp_press;
9598d329309SAngel Iglesias 
9608d329309SAngel Iglesias 	var1 = (s64)data->t_fine * (s64)data->t_fine;
9618d329309SAngel Iglesias 	var2 = var1 >> 6;
9628d329309SAngel Iglesias 	var3 = (var2 * ((s64) data->t_fine)) >> 8;
9638d329309SAngel Iglesias 	var4 = ((s64)calib->P8 * var3) >> 5;
9648d329309SAngel Iglesias 	var5 = ((s64)calib->P7 * var1) << 4;
9658d329309SAngel Iglesias 	var6 = ((s64)calib->P6 * (s64)data->t_fine) << 22;
9668d329309SAngel Iglesias 	offset = ((s64)calib->P5 << 47) + var4 + var5 + var6;
9678d329309SAngel Iglesias 	var2 = ((s64)calib->P4 * var3) >> 5;
9688d329309SAngel Iglesias 	var4 = ((s64)calib->P3 * var1) << 2;
9698d329309SAngel Iglesias 	var5 = ((s64)calib->P2 - ((s64)1 << 14)) *
9708d329309SAngel Iglesias 	       ((s64)data->t_fine << 21);
9718d329309SAngel Iglesias 	sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) +
9728d329309SAngel Iglesias 			var2 + var4 + var5;
9738d329309SAngel Iglesias 	var1 = (sensitivity >> 24) * (s64)adc_press;
9748d329309SAngel Iglesias 	var2 = (s64)calib->P10 * (s64)data->t_fine;
9758d329309SAngel Iglesias 	var3 = var2 + ((s64)calib->P9 << 16);
9768d329309SAngel Iglesias 	var4 = (var3 * (s64)adc_press) >> 13;
9778d329309SAngel Iglesias 
9788d329309SAngel Iglesias 	/*
9798d329309SAngel Iglesias 	 * Dividing by 10 followed by multiplying by 10 to avoid
9808d329309SAngel Iglesias 	 * possible overflow caused by (uncomp_data->pressure * partial_data4).
9818d329309SAngel Iglesias 	 */
9828d329309SAngel Iglesias 	var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9;
9838d329309SAngel Iglesias 	var5 *= 10;
9848d329309SAngel Iglesias 	var6 = (s64)adc_press * (s64)adc_press;
9858d329309SAngel Iglesias 	var2 = ((s64)calib->P11 * var6) >> 16;
9868d329309SAngel Iglesias 	var3 = (var2 * (s64)adc_press) >> 7;
9878d329309SAngel Iglesias 	var4 = (offset >> 2) + var1 + var5 + var3;
9888d329309SAngel Iglesias 	comp_press = ((u64)var4 * 25) >> 40;
9898d329309SAngel Iglesias 
9908d329309SAngel Iglesias 	comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES);
9918d329309SAngel Iglesias 	return comp_press;
9928d329309SAngel Iglesias }
9938d329309SAngel Iglesias 
bmp380_read_temp(struct bmp280_data * data,int * val,int * val2)994597dfb2aSAngel Iglesias static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2)
9958d329309SAngel Iglesias {
9968d329309SAngel Iglesias 	s32 comp_temp;
9978d329309SAngel Iglesias 	u32 adc_temp;
9988d329309SAngel Iglesias 	int ret;
9998d329309SAngel Iglesias 
10008d329309SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB,
10018d329309SAngel Iglesias 			       data->buf, sizeof(data->buf));
10028d329309SAngel Iglesias 	if (ret) {
10038d329309SAngel Iglesias 		dev_err(data->dev, "failed to read temperature\n");
10048d329309SAngel Iglesias 		return ret;
10058d329309SAngel Iglesias 	}
10068d329309SAngel Iglesias 
10078d329309SAngel Iglesias 	adc_temp = get_unaligned_le24(data->buf);
10088d329309SAngel Iglesias 	if (adc_temp == BMP380_TEMP_SKIPPED) {
10098d329309SAngel Iglesias 		dev_err(data->dev, "reading temperature skipped\n");
10108d329309SAngel Iglesias 		return -EIO;
10118d329309SAngel Iglesias 	}
10128d329309SAngel Iglesias 	comp_temp = bmp380_compensate_temp(data, adc_temp);
10138d329309SAngel Iglesias 
10148d329309SAngel Iglesias 	/*
10158d329309SAngel Iglesias 	 * Val might be NULL if we're called by the read_press routine,
10168d329309SAngel Iglesias 	 * who only cares about the carry over t_fine value.
10178d329309SAngel Iglesias 	 */
10188d329309SAngel Iglesias 	if (val) {
10198d329309SAngel Iglesias 		/* IIO reports temperatures in milli Celsius */
10208d329309SAngel Iglesias 		*val = comp_temp * 10;
10218d329309SAngel Iglesias 		return IIO_VAL_INT;
10228d329309SAngel Iglesias 	}
10238d329309SAngel Iglesias 
10248d329309SAngel Iglesias 	return 0;
10258d329309SAngel Iglesias }
10268d329309SAngel Iglesias 
bmp380_read_press(struct bmp280_data * data,int * val,int * val2)10278d329309SAngel Iglesias static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2)
10288d329309SAngel Iglesias {
10298d329309SAngel Iglesias 	s32 comp_press;
10308d329309SAngel Iglesias 	u32 adc_press;
10318d329309SAngel Iglesias 	int ret;
10328d329309SAngel Iglesias 
10338d329309SAngel Iglesias 	/* Read and compensate for temperature so we get a reading of t_fine */
1034597dfb2aSAngel Iglesias 	ret = bmp380_read_temp(data, NULL, NULL);
10358d329309SAngel Iglesias 	if (ret)
10368d329309SAngel Iglesias 		return ret;
10378d329309SAngel Iglesias 
10388d329309SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
10398d329309SAngel Iglesias 			       data->buf, sizeof(data->buf));
10408d329309SAngel Iglesias 	if (ret) {
10418d329309SAngel Iglesias 		dev_err(data->dev, "failed to read pressure\n");
10428d329309SAngel Iglesias 		return ret;
10438d329309SAngel Iglesias 	}
10448d329309SAngel Iglesias 
10458d329309SAngel Iglesias 	adc_press = get_unaligned_le24(data->buf);
10468d329309SAngel Iglesias 	if (adc_press == BMP380_PRESS_SKIPPED) {
10478d329309SAngel Iglesias 		dev_err(data->dev, "reading pressure skipped\n");
10488d329309SAngel Iglesias 		return -EIO;
10498d329309SAngel Iglesias 	}
10508d329309SAngel Iglesias 	comp_press = bmp380_compensate_press(data, adc_press);
10518d329309SAngel Iglesias 
10528d329309SAngel Iglesias 	*val = comp_press;
10538d329309SAngel Iglesias 	/* Compensated pressure is in cPa (centipascals) */
10548d329309SAngel Iglesias 	*val2 = 100000;
10558d329309SAngel Iglesias 
10568d329309SAngel Iglesias 	return IIO_VAL_FRACTIONAL;
10578d329309SAngel Iglesias }
10588d329309SAngel Iglesias 
bmp380_read_calib(struct bmp280_data * data)10598d329309SAngel Iglesias static int bmp380_read_calib(struct bmp280_data *data)
10608d329309SAngel Iglesias {
10618d329309SAngel Iglesias 	struct bmp380_calib *calib = &data->calib.bmp380;
10628d329309SAngel Iglesias 	int ret;
10638d329309SAngel Iglesias 
10648d329309SAngel Iglesias 	/* Read temperature and pressure calibration data */
10658d329309SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START,
10668d329309SAngel Iglesias 			       data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf));
10678d329309SAngel Iglesias 	if (ret) {
10688d329309SAngel Iglesias 		dev_err(data->dev,
10698d329309SAngel Iglesias 			"failed to read temperature calibration parameters\n");
10708d329309SAngel Iglesias 		return ret;
10718d329309SAngel Iglesias 	}
10728d329309SAngel Iglesias 
10738d329309SAngel Iglesias 	/* Toss the temperature calibration data into the entropy pool */
10748d329309SAngel Iglesias 	add_device_randomness(data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf));
10758d329309SAngel Iglesias 
10768d329309SAngel Iglesias 	/* Parse calibration values */
10778d329309SAngel Iglesias 	calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]);
10788d329309SAngel Iglesias 	calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]);
10798d329309SAngel Iglesias 	calib->T3 = data->bmp380_cal_buf[BMP380_T3];
10808d329309SAngel Iglesias 	calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]);
10818d329309SAngel Iglesias 	calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]);
10828d329309SAngel Iglesias 	calib->P3 = data->bmp380_cal_buf[BMP380_P3];
10838d329309SAngel Iglesias 	calib->P4 = data->bmp380_cal_buf[BMP380_P4];
10848d329309SAngel Iglesias 	calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]);
10858d329309SAngel Iglesias 	calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]);
10868d329309SAngel Iglesias 	calib->P7 = data->bmp380_cal_buf[BMP380_P7];
10878d329309SAngel Iglesias 	calib->P8 = data->bmp380_cal_buf[BMP380_P8];
10888d329309SAngel Iglesias 	calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]);
10898d329309SAngel Iglesias 	calib->P10 = data->bmp380_cal_buf[BMP380_P10];
10908d329309SAngel Iglesias 	calib->P11 = data->bmp380_cal_buf[BMP380_P11];
10918d329309SAngel Iglesias 
10928d329309SAngel Iglesias 	return 0;
10938d329309SAngel Iglesias }
10948d329309SAngel Iglesias 
109510b40ffbSAngel Iglesias static const int bmp380_odr_table[][2] = {
109610b40ffbSAngel Iglesias 	[BMP380_ODR_200HZ]	= {200, 0},
109710b40ffbSAngel Iglesias 	[BMP380_ODR_100HZ]	= {100, 0},
109810b40ffbSAngel Iglesias 	[BMP380_ODR_50HZ]	= {50, 0},
109910b40ffbSAngel Iglesias 	[BMP380_ODR_25HZ]	= {25, 0},
110010b40ffbSAngel Iglesias 	[BMP380_ODR_12_5HZ]	= {12, 500000},
110110b40ffbSAngel Iglesias 	[BMP380_ODR_6_25HZ]	= {6, 250000},
110210b40ffbSAngel Iglesias 	[BMP380_ODR_3_125HZ]	= {3, 125000},
110310b40ffbSAngel Iglesias 	[BMP380_ODR_1_5625HZ]	= {1, 562500},
110410b40ffbSAngel Iglesias 	[BMP380_ODR_0_78HZ]	= {0, 781250},
110510b40ffbSAngel Iglesias 	[BMP380_ODR_0_39HZ]	= {0, 390625},
110610b40ffbSAngel Iglesias 	[BMP380_ODR_0_2HZ]	= {0, 195313},
110710b40ffbSAngel Iglesias 	[BMP380_ODR_0_1HZ]	= {0, 97656},
110810b40ffbSAngel Iglesias 	[BMP380_ODR_0_05HZ]	= {0, 48828},
110910b40ffbSAngel Iglesias 	[BMP380_ODR_0_02HZ]	= {0, 24414},
111010b40ffbSAngel Iglesias 	[BMP380_ODR_0_01HZ]	= {0, 12207},
111110b40ffbSAngel Iglesias 	[BMP380_ODR_0_006HZ]	= {0, 6104},
111210b40ffbSAngel Iglesias 	[BMP380_ODR_0_003HZ]	= {0, 3052},
111310b40ffbSAngel Iglesias 	[BMP380_ODR_0_0015HZ]	= {0, 1526},
111410b40ffbSAngel Iglesias };
111510b40ffbSAngel Iglesias 
bmp380_preinit(struct bmp280_data * data)1116c25ea00fSAngel Iglesias static int bmp380_preinit(struct bmp280_data *data)
1117c25ea00fSAngel Iglesias {
1118c25ea00fSAngel Iglesias 	/* BMP3xx requires soft-reset as part of initialization */
1119c25ea00fSAngel Iglesias 	return bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
1120c25ea00fSAngel Iglesias }
1121c25ea00fSAngel Iglesias 
bmp380_chip_config(struct bmp280_data * data)11228d329309SAngel Iglesias static int bmp380_chip_config(struct bmp280_data *data)
11238d329309SAngel Iglesias {
11248d329309SAngel Iglesias 	bool change = false, aux;
11258d329309SAngel Iglesias 	unsigned int tmp;
11268d329309SAngel Iglesias 	u8 osrs;
11278d329309SAngel Iglesias 	int ret;
11288d329309SAngel Iglesias 
11298d329309SAngel Iglesias 	/* Configure power control register */
11308d329309SAngel Iglesias 	ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL,
11318d329309SAngel Iglesias 				 BMP380_CTRL_SENSORS_MASK,
11328d329309SAngel Iglesias 				 BMP380_CTRL_SENSORS_PRESS_EN |
11338d329309SAngel Iglesias 				 BMP380_CTRL_SENSORS_TEMP_EN);
11348d329309SAngel Iglesias 	if (ret) {
11358d329309SAngel Iglesias 		dev_err(data->dev,
11368d329309SAngel Iglesias 			"failed to write operation control register\n");
11378d329309SAngel Iglesias 		return ret;
11388d329309SAngel Iglesias 	}
11398d329309SAngel Iglesias 
11408d329309SAngel Iglesias 	/* Configure oversampling */
11418d329309SAngel Iglesias 	osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) |
11428d329309SAngel Iglesias 	       FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press);
11438d329309SAngel Iglesias 
11448d329309SAngel Iglesias 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR,
11458d329309SAngel Iglesias 				       BMP380_OSRS_TEMP_MASK |
11468d329309SAngel Iglesias 				       BMP380_OSRS_PRESS_MASK,
11478d329309SAngel Iglesias 				       osrs, &aux);
11488d329309SAngel Iglesias 	if (ret) {
11498d329309SAngel Iglesias 		dev_err(data->dev, "failed to write oversampling register\n");
11508d329309SAngel Iglesias 		return ret;
11518d329309SAngel Iglesias 	}
11528d329309SAngel Iglesias 	change = change || aux;
11538d329309SAngel Iglesias 
11548d329309SAngel Iglesias 	/* Configure output data rate */
115510b40ffbSAngel Iglesias 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR,
115610b40ffbSAngel Iglesias 				       BMP380_ODRS_MASK, data->sampling_freq, &aux);
11578d329309SAngel Iglesias 	if (ret) {
11588d329309SAngel Iglesias 		dev_err(data->dev, "failed to write ODR selection register\n");
11598d329309SAngel Iglesias 		return ret;
11608d329309SAngel Iglesias 	}
116110b40ffbSAngel Iglesias 	change = change || aux;
11628d329309SAngel Iglesias 
11638d329309SAngel Iglesias 	/* Set filter data */
116410b40ffbSAngel Iglesias 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_CONFIG, BMP380_FILTER_MASK,
116510b40ffbSAngel Iglesias 				       FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff),
116610b40ffbSAngel Iglesias 				       &aux);
11678d329309SAngel Iglesias 	if (ret) {
11688d329309SAngel Iglesias 		dev_err(data->dev, "failed to write config register\n");
11698d329309SAngel Iglesias 		return ret;
11708d329309SAngel Iglesias 	}
117110b40ffbSAngel Iglesias 	change = change || aux;
11728d329309SAngel Iglesias 
11738d329309SAngel Iglesias 	if (change) {
11748d329309SAngel Iglesias 		/*
11758d329309SAngel Iglesias 		 * The configurations errors are detected on the fly during a measurement
11768d329309SAngel Iglesias 		 * cycle. If the sampling frequency is too low, it's faster to reset
11778d329309SAngel Iglesias 		 * the measurement loop than wait until the next measurement is due.
11788d329309SAngel Iglesias 		 *
11798d329309SAngel Iglesias 		 * Resets sensor measurement loop toggling between sleep and normal
11808d329309SAngel Iglesias 		 * operating modes.
11818d329309SAngel Iglesias 		 */
11828d329309SAngel Iglesias 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
11838d329309SAngel Iglesias 					BMP380_MODE_MASK,
11848d329309SAngel Iglesias 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_SLEEP));
11858d329309SAngel Iglesias 		if (ret) {
11868d329309SAngel Iglesias 			dev_err(data->dev, "failed to set sleep mode\n");
11878d329309SAngel Iglesias 			return ret;
11888d329309SAngel Iglesias 		}
11898d329309SAngel Iglesias 		usleep_range(2000, 2500);
11908d329309SAngel Iglesias 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
11918d329309SAngel Iglesias 					BMP380_MODE_MASK,
11928d329309SAngel Iglesias 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL));
11938d329309SAngel Iglesias 		if (ret) {
11948d329309SAngel Iglesias 			dev_err(data->dev, "failed to set normal mode\n");
11958d329309SAngel Iglesias 			return ret;
11968d329309SAngel Iglesias 		}
11978d329309SAngel Iglesias 		/*
11988d329309SAngel Iglesias 		 * Waits for measurement before checking configuration error flag.
11998d329309SAngel Iglesias 		 * Selected longest measure time indicated in section 3.9.1
12008d329309SAngel Iglesias 		 * in the datasheet.
12018d329309SAngel Iglesias 		 */
12028d329309SAngel Iglesias 		msleep(80);
12038d329309SAngel Iglesias 
12048d329309SAngel Iglesias 		/* Check config error flag */
12058d329309SAngel Iglesias 		ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp);
12068d329309SAngel Iglesias 		if (ret) {
12078d329309SAngel Iglesias 			dev_err(data->dev,
12088d329309SAngel Iglesias 				"failed to read error register\n");
12098d329309SAngel Iglesias 			return ret;
12108d329309SAngel Iglesias 		}
12118d329309SAngel Iglesias 		if (tmp & BMP380_ERR_CONF_MASK) {
12128d329309SAngel Iglesias 			dev_warn(data->dev,
12138d329309SAngel Iglesias 				"sensor flagged configuration as incompatible\n");
12148d329309SAngel Iglesias 			return -EINVAL;
12158d329309SAngel Iglesias 		}
12168d329309SAngel Iglesias 	}
12178d329309SAngel Iglesias 
12188d329309SAngel Iglesias 	return 0;
12198d329309SAngel Iglesias }
12208d329309SAngel Iglesias 
12218d329309SAngel Iglesias static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 };
122210b40ffbSAngel Iglesias static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128};
12238d329309SAngel Iglesias 
12240b0b7726SAngel Iglesias const struct bmp280_chip_info bmp380_chip_info = {
12258d329309SAngel Iglesias 	.id_reg = BMP380_REG_ID,
12260b0b7726SAngel Iglesias 	.chip_id = BMP380_CHIP_ID,
12270b0b7726SAngel Iglesias 	.regmap_config = &bmp380_regmap_config,
12288d329309SAngel Iglesias 	.start_up_time = 2000,
122910b40ffbSAngel Iglesias 	.channels = bmp380_channels,
12308d329309SAngel Iglesias 	.num_channels = 2,
12318d329309SAngel Iglesias 
12328d329309SAngel Iglesias 	.oversampling_temp_avail = bmp380_oversampling_avail,
12338d329309SAngel Iglesias 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail),
12348d329309SAngel Iglesias 	.oversampling_temp_default = ilog2(1),
12358d329309SAngel Iglesias 
12368d329309SAngel Iglesias 	.oversampling_press_avail = bmp380_oversampling_avail,
12378d329309SAngel Iglesias 	.num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail),
12388d329309SAngel Iglesias 	.oversampling_press_default = ilog2(4),
12398d329309SAngel Iglesias 
124010b40ffbSAngel Iglesias 	.sampling_freq_avail = bmp380_odr_table,
124110b40ffbSAngel Iglesias 	.num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2,
124210b40ffbSAngel Iglesias 	.sampling_freq_default = BMP380_ODR_50HZ,
124310b40ffbSAngel Iglesias 
124410b40ffbSAngel Iglesias 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
124510b40ffbSAngel Iglesias 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
124610b40ffbSAngel Iglesias 	.iir_filter_coeff_default = 2,
124710b40ffbSAngel Iglesias 
12488d329309SAngel Iglesias 	.chip_config = bmp380_chip_config,
12498d329309SAngel Iglesias 	.read_temp = bmp380_read_temp,
12508d329309SAngel Iglesias 	.read_press = bmp380_read_press,
12518d329309SAngel Iglesias 	.read_calib = bmp380_read_calib,
1252c25ea00fSAngel Iglesias 	.preinit = bmp380_preinit,
12538d329309SAngel Iglesias };
12540b0b7726SAngel Iglesias EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280);
12558d329309SAngel Iglesias 
bmp580_soft_reset(struct bmp280_data * data)1256597dfb2aSAngel Iglesias static int bmp580_soft_reset(struct bmp280_data *data)
1257597dfb2aSAngel Iglesias {
1258597dfb2aSAngel Iglesias 	unsigned int reg;
1259597dfb2aSAngel Iglesias 	int ret;
1260597dfb2aSAngel Iglesias 
1261597dfb2aSAngel Iglesias 	ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET);
1262597dfb2aSAngel Iglesias 	if (ret) {
1263597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to send reset command to device\n");
1264597dfb2aSAngel Iglesias 		return ret;
1265597dfb2aSAngel Iglesias 	}
1266597dfb2aSAngel Iglesias 	usleep_range(2000, 2500);
1267597dfb2aSAngel Iglesias 
1268597dfb2aSAngel Iglesias 	/* Dummy read of chip_id */
1269597dfb2aSAngel Iglesias 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
1270597dfb2aSAngel Iglesias 	if (ret) {
1271597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to reestablish comms after reset\n");
1272597dfb2aSAngel Iglesias 		return ret;
1273597dfb2aSAngel Iglesias 	}
1274597dfb2aSAngel Iglesias 
1275597dfb2aSAngel Iglesias 	ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, &reg);
1276597dfb2aSAngel Iglesias 	if (ret) {
1277597dfb2aSAngel Iglesias 		dev_err(data->dev, "error reading interrupt status register\n");
1278597dfb2aSAngel Iglesias 		return ret;
1279597dfb2aSAngel Iglesias 	}
1280597dfb2aSAngel Iglesias 	if (!(reg & BMP580_INT_STATUS_POR_MASK)) {
1281597dfb2aSAngel Iglesias 		dev_err(data->dev, "error resetting sensor\n");
1282597dfb2aSAngel Iglesias 		return -EINVAL;
1283597dfb2aSAngel Iglesias 	}
1284597dfb2aSAngel Iglesias 
1285597dfb2aSAngel Iglesias 	return 0;
1286597dfb2aSAngel Iglesias }
1287597dfb2aSAngel Iglesias 
1288accb9d05SAngel Iglesias /**
1289accb9d05SAngel Iglesias  * bmp580_nvm_operation() - Helper function to commit NVM memory operations
1290accb9d05SAngel Iglesias  * @data: sensor data struct
1291accb9d05SAngel Iglesias  * @is_write: flag to signal write operation
1292accb9d05SAngel Iglesias  */
bmp580_nvm_operation(struct bmp280_data * data,bool is_write)1293accb9d05SAngel Iglesias static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write)
1294accb9d05SAngel Iglesias {
1295accb9d05SAngel Iglesias 	unsigned long timeout, poll;
1296accb9d05SAngel Iglesias 	unsigned int reg;
1297accb9d05SAngel Iglesias 	int ret;
1298accb9d05SAngel Iglesias 
1299accb9d05SAngel Iglesias 	/* Check NVM ready flag */
1300accb9d05SAngel Iglesias 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
1301accb9d05SAngel Iglesias 	if (ret) {
1302accb9d05SAngel Iglesias 		dev_err(data->dev, "failed to check nvm status\n");
1303accb9d05SAngel Iglesias 		return ret;
1304accb9d05SAngel Iglesias 	}
1305accb9d05SAngel Iglesias 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) {
1306accb9d05SAngel Iglesias 		dev_err(data->dev, "sensor's nvm is not ready\n");
1307accb9d05SAngel Iglesias 		return -EIO;
1308accb9d05SAngel Iglesias 	}
1309accb9d05SAngel Iglesias 
1310accb9d05SAngel Iglesias 	/* Start NVM operation sequence */
1311accb9d05SAngel Iglesias 	ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_NVM_OP_SEQ_0);
1312accb9d05SAngel Iglesias 	if (ret) {
1313accb9d05SAngel Iglesias 		dev_err(data->dev, "failed to send nvm operation's first sequence\n");
1314accb9d05SAngel Iglesias 		return ret;
1315accb9d05SAngel Iglesias 	}
1316accb9d05SAngel Iglesias 	if (is_write) {
1317accb9d05SAngel Iglesias 		/* Send NVM write sequence */
1318accb9d05SAngel Iglesias 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1319accb9d05SAngel Iglesias 				   BMP580_CMD_NVM_WRITE_SEQ_1);
1320accb9d05SAngel Iglesias 		if (ret) {
1321accb9d05SAngel Iglesias 			dev_err(data->dev, "failed to send nvm write sequence\n");
1322accb9d05SAngel Iglesias 			return ret;
1323accb9d05SAngel Iglesias 		}
1324accb9d05SAngel Iglesias 		/* Datasheet says on 4.8.1.2 it takes approximately 10ms */
1325accb9d05SAngel Iglesias 		poll = 2000;
1326accb9d05SAngel Iglesias 		timeout = 12000;
1327accb9d05SAngel Iglesias 	} else {
1328accb9d05SAngel Iglesias 		/* Send NVM read sequence */
1329accb9d05SAngel Iglesias 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1330accb9d05SAngel Iglesias 				   BMP580_CMD_NVM_READ_SEQ_1);
1331accb9d05SAngel Iglesias 		if (ret) {
1332accb9d05SAngel Iglesias 			dev_err(data->dev, "failed to send nvm read sequence\n");
1333accb9d05SAngel Iglesias 			return ret;
1334accb9d05SAngel Iglesias 		}
1335accb9d05SAngel Iglesias 		/* Datasheet says on 4.8.1.1 it takes approximately 200us */
1336accb9d05SAngel Iglesias 		poll = 50;
1337accb9d05SAngel Iglesias 		timeout = 400;
1338accb9d05SAngel Iglesias 	}
1339accb9d05SAngel Iglesias 	if (ret) {
1340accb9d05SAngel Iglesias 		dev_err(data->dev, "failed to write command sequence\n");
1341accb9d05SAngel Iglesias 		return -EIO;
1342accb9d05SAngel Iglesias 	}
1343accb9d05SAngel Iglesias 
1344accb9d05SAngel Iglesias 	/* Wait until NVM is ready again */
1345accb9d05SAngel Iglesias 	ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg,
1346accb9d05SAngel Iglesias 				       (reg & BMP580_STATUS_NVM_RDY_MASK),
1347accb9d05SAngel Iglesias 				       poll, timeout);
1348accb9d05SAngel Iglesias 	if (ret) {
1349accb9d05SAngel Iglesias 		dev_err(data->dev, "error checking nvm operation status\n");
1350accb9d05SAngel Iglesias 		return ret;
1351accb9d05SAngel Iglesias 	}
1352accb9d05SAngel Iglesias 
1353accb9d05SAngel Iglesias 	/* Check NVM error flags */
1354accb9d05SAngel Iglesias 	if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) {
1355accb9d05SAngel Iglesias 		dev_err(data->dev, "error processing nvm operation\n");
1356accb9d05SAngel Iglesias 		return -EIO;
1357accb9d05SAngel Iglesias 	}
1358accb9d05SAngel Iglesias 
1359accb9d05SAngel Iglesias 	return 0;
1360accb9d05SAngel Iglesias }
1361accb9d05SAngel Iglesias 
1362597dfb2aSAngel Iglesias /*
1363597dfb2aSAngel Iglesias  * Contrary to previous sensors families, compensation algorithm is builtin.
1364597dfb2aSAngel Iglesias  * We are only required to read the register raw data and adapt the ranges
1365597dfb2aSAngel Iglesias  * for what is expected on IIO ABI.
1366597dfb2aSAngel Iglesias  */
1367597dfb2aSAngel Iglesias 
bmp580_read_temp(struct bmp280_data * data,int * val,int * val2)1368597dfb2aSAngel Iglesias static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2)
1369597dfb2aSAngel Iglesias {
1370597dfb2aSAngel Iglesias 	s32 raw_temp;
1371597dfb2aSAngel Iglesias 	int ret;
1372597dfb2aSAngel Iglesias 
1373597dfb2aSAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf,
1374597dfb2aSAngel Iglesias 			       sizeof(data->buf));
1375597dfb2aSAngel Iglesias 	if (ret) {
1376597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to read temperature\n");
1377597dfb2aSAngel Iglesias 		return ret;
1378597dfb2aSAngel Iglesias 	}
1379597dfb2aSAngel Iglesias 
1380597dfb2aSAngel Iglesias 	raw_temp = get_unaligned_le24(data->buf);
1381597dfb2aSAngel Iglesias 	if (raw_temp == BMP580_TEMP_SKIPPED) {
1382597dfb2aSAngel Iglesias 		dev_err(data->dev, "reading temperature skipped\n");
1383597dfb2aSAngel Iglesias 		return -EIO;
1384597dfb2aSAngel Iglesias 	}
1385597dfb2aSAngel Iglesias 
1386597dfb2aSAngel Iglesias 	/*
1387597dfb2aSAngel Iglesias 	 * Temperature is returned in Celsius degrees in fractional
1388597dfb2aSAngel Iglesias 	 * form down 2^16. We reescale by x1000 to return milli Celsius
1389597dfb2aSAngel Iglesias 	 * to respect IIO ABI.
1390597dfb2aSAngel Iglesias 	 */
1391597dfb2aSAngel Iglesias 	*val = raw_temp * 1000;
1392597dfb2aSAngel Iglesias 	*val2 = 16;
1393597dfb2aSAngel Iglesias 	return IIO_VAL_FRACTIONAL_LOG2;
1394597dfb2aSAngel Iglesias }
1395597dfb2aSAngel Iglesias 
bmp580_read_press(struct bmp280_data * data,int * val,int * val2)1396597dfb2aSAngel Iglesias static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2)
1397597dfb2aSAngel Iglesias {
1398597dfb2aSAngel Iglesias 	u32 raw_press;
1399597dfb2aSAngel Iglesias 	int ret;
1400597dfb2aSAngel Iglesias 
1401597dfb2aSAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf,
1402597dfb2aSAngel Iglesias 			       sizeof(data->buf));
1403597dfb2aSAngel Iglesias 	if (ret) {
1404597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to read pressure\n");
1405597dfb2aSAngel Iglesias 		return ret;
1406597dfb2aSAngel Iglesias 	}
1407597dfb2aSAngel Iglesias 
1408597dfb2aSAngel Iglesias 	raw_press = get_unaligned_le24(data->buf);
1409597dfb2aSAngel Iglesias 	if (raw_press == BMP580_PRESS_SKIPPED) {
1410597dfb2aSAngel Iglesias 		dev_err(data->dev, "reading pressure skipped\n");
1411597dfb2aSAngel Iglesias 		return -EIO;
1412597dfb2aSAngel Iglesias 	}
1413597dfb2aSAngel Iglesias 	/*
1414597dfb2aSAngel Iglesias 	 * Pressure is returned in Pascals in fractional form down 2^16.
1415597dfb2aSAngel Iglesias 	 * We reescale /1000 to convert to kilopascal to respect IIO ABI.
1416597dfb2aSAngel Iglesias 	 */
1417597dfb2aSAngel Iglesias 	*val = raw_press;
1418597dfb2aSAngel Iglesias 	*val2 = 64000; /* 2^6 * 1000 */
1419597dfb2aSAngel Iglesias 	return IIO_VAL_FRACTIONAL;
1420597dfb2aSAngel Iglesias }
1421597dfb2aSAngel Iglesias 
1422597dfb2aSAngel Iglesias static const int bmp580_odr_table[][2] = {
1423597dfb2aSAngel Iglesias 	[BMP580_ODR_240HZ] =	{240, 0},
1424597dfb2aSAngel Iglesias 	[BMP580_ODR_218HZ] =	{218, 0},
1425597dfb2aSAngel Iglesias 	[BMP580_ODR_199HZ] =	{199, 0},
1426597dfb2aSAngel Iglesias 	[BMP580_ODR_179HZ] =	{179, 0},
1427597dfb2aSAngel Iglesias 	[BMP580_ODR_160HZ] =	{160, 0},
1428597dfb2aSAngel Iglesias 	[BMP580_ODR_149HZ] =	{149, 0},
1429597dfb2aSAngel Iglesias 	[BMP580_ODR_140HZ] =	{140, 0},
1430597dfb2aSAngel Iglesias 	[BMP580_ODR_129HZ] =	{129, 0},
1431597dfb2aSAngel Iglesias 	[BMP580_ODR_120HZ] =	{120, 0},
1432597dfb2aSAngel Iglesias 	[BMP580_ODR_110HZ] =	{110, 0},
1433597dfb2aSAngel Iglesias 	[BMP580_ODR_100HZ] =	{100, 0},
1434597dfb2aSAngel Iglesias 	[BMP580_ODR_89HZ] =	{89, 0},
1435597dfb2aSAngel Iglesias 	[BMP580_ODR_80HZ] =	{80, 0},
1436597dfb2aSAngel Iglesias 	[BMP580_ODR_70HZ] =	{70, 0},
1437597dfb2aSAngel Iglesias 	[BMP580_ODR_60HZ] =	{60, 0},
1438597dfb2aSAngel Iglesias 	[BMP580_ODR_50HZ] =	{50, 0},
1439597dfb2aSAngel Iglesias 	[BMP580_ODR_45HZ] =	{45, 0},
1440597dfb2aSAngel Iglesias 	[BMP580_ODR_40HZ] =	{40, 0},
1441597dfb2aSAngel Iglesias 	[BMP580_ODR_35HZ] =	{35, 0},
1442597dfb2aSAngel Iglesias 	[BMP580_ODR_30HZ] =	{30, 0},
1443597dfb2aSAngel Iglesias 	[BMP580_ODR_25HZ] =	{25, 0},
1444597dfb2aSAngel Iglesias 	[BMP580_ODR_20HZ] =	{20, 0},
1445597dfb2aSAngel Iglesias 	[BMP580_ODR_15HZ] =	{15, 0},
1446597dfb2aSAngel Iglesias 	[BMP580_ODR_10HZ] =	{10, 0},
1447597dfb2aSAngel Iglesias 	[BMP580_ODR_5HZ] =	{5, 0},
1448597dfb2aSAngel Iglesias 	[BMP580_ODR_4HZ] =	{4, 0},
1449597dfb2aSAngel Iglesias 	[BMP580_ODR_3HZ] =	{3, 0},
1450597dfb2aSAngel Iglesias 	[BMP580_ODR_2HZ] =	{2, 0},
1451597dfb2aSAngel Iglesias 	[BMP580_ODR_1HZ] =	{1, 0},
1452597dfb2aSAngel Iglesias 	[BMP580_ODR_0_5HZ] =	{0, 500000},
1453597dfb2aSAngel Iglesias 	[BMP580_ODR_0_25HZ] =	{0, 250000},
1454597dfb2aSAngel Iglesias 	[BMP580_ODR_0_125HZ] =	{0, 125000},
1455597dfb2aSAngel Iglesias };
1456597dfb2aSAngel Iglesias 
1457accb9d05SAngel Iglesias static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
1458accb9d05SAngel Iglesias 
bmp580_nvmem_read(void * priv,unsigned int offset,void * val,size_t bytes)1459accb9d05SAngel Iglesias static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
1460accb9d05SAngel Iglesias 			     size_t bytes)
1461accb9d05SAngel Iglesias {
1462accb9d05SAngel Iglesias 	struct bmp280_data *data = priv;
1463accb9d05SAngel Iglesias 	u16 *dst = val;
1464accb9d05SAngel Iglesias 	int ret, addr;
1465accb9d05SAngel Iglesias 
1466accb9d05SAngel Iglesias 	pm_runtime_get_sync(data->dev);
1467accb9d05SAngel Iglesias 	mutex_lock(&data->lock);
1468accb9d05SAngel Iglesias 
1469accb9d05SAngel Iglesias 	/* Set sensor in standby mode */
1470accb9d05SAngel Iglesias 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1471accb9d05SAngel Iglesias 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1472accb9d05SAngel Iglesias 				 BMP580_ODR_DEEPSLEEP_DIS |
1473accb9d05SAngel Iglesias 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1474accb9d05SAngel Iglesias 	if (ret) {
1475accb9d05SAngel Iglesias 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1476accb9d05SAngel Iglesias 		goto exit;
1477accb9d05SAngel Iglesias 	}
1478accb9d05SAngel Iglesias 	/* Wait standby transition time */
1479accb9d05SAngel Iglesias 	usleep_range(2500, 3000);
1480accb9d05SAngel Iglesias 
1481accb9d05SAngel Iglesias 	while (bytes >= sizeof(*dst)) {
1482accb9d05SAngel Iglesias 		addr = bmp580_nvmem_addrs[offset / sizeof(*dst)];
1483accb9d05SAngel Iglesias 
1484accb9d05SAngel Iglesias 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1485accb9d05SAngel Iglesias 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1486accb9d05SAngel Iglesias 		if (ret) {
1487accb9d05SAngel Iglesias 			dev_err(data->dev, "error writing nvm address\n");
1488accb9d05SAngel Iglesias 			goto exit;
1489accb9d05SAngel Iglesias 		}
1490accb9d05SAngel Iglesias 
1491accb9d05SAngel Iglesias 		ret = bmp580_nvm_operation(data, false);
1492accb9d05SAngel Iglesias 		if (ret)
1493accb9d05SAngel Iglesias 			goto exit;
1494accb9d05SAngel Iglesias 
1495accb9d05SAngel Iglesias 		ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1496accb9d05SAngel Iglesias 				       sizeof(data->le16));
1497accb9d05SAngel Iglesias 		if (ret) {
1498accb9d05SAngel Iglesias 			dev_err(data->dev, "error reading nvm data regs\n");
1499accb9d05SAngel Iglesias 			goto exit;
1500accb9d05SAngel Iglesias 		}
1501accb9d05SAngel Iglesias 
1502accb9d05SAngel Iglesias 		*dst++ = le16_to_cpu(data->le16);
1503accb9d05SAngel Iglesias 		bytes -= sizeof(*dst);
1504accb9d05SAngel Iglesias 		offset += sizeof(*dst);
1505accb9d05SAngel Iglesias 	}
1506accb9d05SAngel Iglesias exit:
1507accb9d05SAngel Iglesias 	/* Restore chip config */
1508accb9d05SAngel Iglesias 	data->chip_info->chip_config(data);
1509accb9d05SAngel Iglesias 	mutex_unlock(&data->lock);
1510accb9d05SAngel Iglesias 	pm_runtime_mark_last_busy(data->dev);
1511accb9d05SAngel Iglesias 	pm_runtime_put_autosuspend(data->dev);
1512accb9d05SAngel Iglesias 	return ret;
1513accb9d05SAngel Iglesias }
1514accb9d05SAngel Iglesias 
bmp580_nvmem_write(void * priv,unsigned int offset,void * val,size_t bytes)1515accb9d05SAngel Iglesias static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
1516accb9d05SAngel Iglesias 			      size_t bytes)
1517accb9d05SAngel Iglesias {
1518accb9d05SAngel Iglesias 	struct bmp280_data *data = priv;
1519accb9d05SAngel Iglesias 	u16 *buf = val;
1520accb9d05SAngel Iglesias 	int ret, addr;
1521accb9d05SAngel Iglesias 
1522accb9d05SAngel Iglesias 	pm_runtime_get_sync(data->dev);
1523accb9d05SAngel Iglesias 	mutex_lock(&data->lock);
1524accb9d05SAngel Iglesias 
1525accb9d05SAngel Iglesias 	/* Set sensor in standby mode */
1526accb9d05SAngel Iglesias 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1527accb9d05SAngel Iglesias 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1528accb9d05SAngel Iglesias 				 BMP580_ODR_DEEPSLEEP_DIS |
1529accb9d05SAngel Iglesias 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1530accb9d05SAngel Iglesias 	if (ret) {
1531accb9d05SAngel Iglesias 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1532accb9d05SAngel Iglesias 		goto exit;
1533accb9d05SAngel Iglesias 	}
1534accb9d05SAngel Iglesias 	/* Wait standby transition time */
1535accb9d05SAngel Iglesias 	usleep_range(2500, 3000);
1536accb9d05SAngel Iglesias 
1537accb9d05SAngel Iglesias 	while (bytes >= sizeof(*buf)) {
1538accb9d05SAngel Iglesias 		addr = bmp580_nvmem_addrs[offset / sizeof(*buf)];
1539accb9d05SAngel Iglesias 
1540accb9d05SAngel Iglesias 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, BMP580_NVM_PROG_EN |
1541accb9d05SAngel Iglesias 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1542accb9d05SAngel Iglesias 		if (ret) {
1543accb9d05SAngel Iglesias 			dev_err(data->dev, "error writing nvm address\n");
1544accb9d05SAngel Iglesias 			goto exit;
1545accb9d05SAngel Iglesias 		}
1546accb9d05SAngel Iglesias 		data->le16 = cpu_to_le16(*buf++);
1547accb9d05SAngel Iglesias 
1548accb9d05SAngel Iglesias 		ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1549accb9d05SAngel Iglesias 					sizeof(data->le16));
1550accb9d05SAngel Iglesias 		if (ret) {
1551accb9d05SAngel Iglesias 			dev_err(data->dev, "error writing LSB NVM data regs\n");
1552accb9d05SAngel Iglesias 			goto exit;
1553accb9d05SAngel Iglesias 		}
1554accb9d05SAngel Iglesias 
1555accb9d05SAngel Iglesias 		ret = bmp580_nvm_operation(data, true);
1556accb9d05SAngel Iglesias 		if (ret)
1557accb9d05SAngel Iglesias 			goto exit;
1558accb9d05SAngel Iglesias 
1559accb9d05SAngel Iglesias 		/* Disable programming mode bit */
1560accb9d05SAngel Iglesias 		ret = regmap_update_bits(data->regmap, BMP580_REG_NVM_ADDR,
1561accb9d05SAngel Iglesias 					 BMP580_NVM_PROG_EN, 0);
1562accb9d05SAngel Iglesias 		if (ret) {
1563accb9d05SAngel Iglesias 			dev_err(data->dev, "error resetting nvm write\n");
1564accb9d05SAngel Iglesias 			goto exit;
1565accb9d05SAngel Iglesias 		}
1566accb9d05SAngel Iglesias 
1567accb9d05SAngel Iglesias 		bytes -= sizeof(*buf);
1568accb9d05SAngel Iglesias 		offset += sizeof(*buf);
1569accb9d05SAngel Iglesias 	}
1570accb9d05SAngel Iglesias exit:
1571accb9d05SAngel Iglesias 	/* Restore chip config */
1572accb9d05SAngel Iglesias 	data->chip_info->chip_config(data);
1573accb9d05SAngel Iglesias 	mutex_unlock(&data->lock);
1574accb9d05SAngel Iglesias 	pm_runtime_mark_last_busy(data->dev);
1575accb9d05SAngel Iglesias 	pm_runtime_put_autosuspend(data->dev);
1576accb9d05SAngel Iglesias 	return ret;
1577accb9d05SAngel Iglesias }
1578accb9d05SAngel Iglesias 
bmp580_preinit(struct bmp280_data * data)1579597dfb2aSAngel Iglesias static int bmp580_preinit(struct bmp280_data *data)
1580597dfb2aSAngel Iglesias {
1581accb9d05SAngel Iglesias 	struct nvmem_config config = {
1582accb9d05SAngel Iglesias 		.dev = data->dev,
1583accb9d05SAngel Iglesias 		.priv = data,
1584accb9d05SAngel Iglesias 		.name = "bmp580_nvmem",
1585accb9d05SAngel Iglesias 		.word_size = sizeof(u16),
1586accb9d05SAngel Iglesias 		.stride = sizeof(u16),
1587accb9d05SAngel Iglesias 		.size = 3 * sizeof(u16),
1588accb9d05SAngel Iglesias 		.reg_read = bmp580_nvmem_read,
1589accb9d05SAngel Iglesias 		.reg_write = bmp580_nvmem_write,
1590accb9d05SAngel Iglesias 	};
1591597dfb2aSAngel Iglesias 	unsigned int reg;
1592597dfb2aSAngel Iglesias 	int ret;
1593597dfb2aSAngel Iglesias 
1594597dfb2aSAngel Iglesias 	/* Issue soft-reset command */
1595597dfb2aSAngel Iglesias 	ret = bmp580_soft_reset(data);
1596597dfb2aSAngel Iglesias 	if (ret)
1597597dfb2aSAngel Iglesias 		return ret;
1598597dfb2aSAngel Iglesias 
1599597dfb2aSAngel Iglesias 	/* Post powerup sequence */
1600597dfb2aSAngel Iglesias 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
1601597dfb2aSAngel Iglesias 	if (ret)
1602597dfb2aSAngel Iglesias 		return ret;
1603597dfb2aSAngel Iglesias 
1604597dfb2aSAngel Iglesias 	/* Print warn message if we don't know the chip id */
1605597dfb2aSAngel Iglesias 	if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT)
1606597dfb2aSAngel Iglesias 		dev_warn(data->dev, "preinit: unexpected chip_id\n");
1607597dfb2aSAngel Iglesias 
1608597dfb2aSAngel Iglesias 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
1609597dfb2aSAngel Iglesias 	if (ret)
1610597dfb2aSAngel Iglesias 		return ret;
1611597dfb2aSAngel Iglesias 
1612597dfb2aSAngel Iglesias 	/* Check nvm status */
1613597dfb2aSAngel Iglesias 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) {
1614597dfb2aSAngel Iglesias 		dev_err(data->dev, "preinit: nvm error on powerup sequence\n");
1615597dfb2aSAngel Iglesias 		return -EIO;
1616597dfb2aSAngel Iglesias 	}
1617597dfb2aSAngel Iglesias 
1618accb9d05SAngel Iglesias 	/* Register nvmem device */
1619accb9d05SAngel Iglesias 	return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
1620597dfb2aSAngel Iglesias }
1621597dfb2aSAngel Iglesias 
bmp580_chip_config(struct bmp280_data * data)1622597dfb2aSAngel Iglesias static int bmp580_chip_config(struct bmp280_data *data)
1623597dfb2aSAngel Iglesias {
1624597dfb2aSAngel Iglesias 	bool change = false, aux;
1625597dfb2aSAngel Iglesias 	unsigned int tmp;
1626597dfb2aSAngel Iglesias 	u8 reg_val;
1627597dfb2aSAngel Iglesias 	int ret;
1628597dfb2aSAngel Iglesias 
1629597dfb2aSAngel Iglesias 	/* Sets sensor in standby mode */
1630597dfb2aSAngel Iglesias 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1631597dfb2aSAngel Iglesias 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1632597dfb2aSAngel Iglesias 				 BMP580_ODR_DEEPSLEEP_DIS |
1633597dfb2aSAngel Iglesias 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1634597dfb2aSAngel Iglesias 	if (ret) {
1635597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1636597dfb2aSAngel Iglesias 		return ret;
1637597dfb2aSAngel Iglesias 	}
1638597dfb2aSAngel Iglesias 	/* From datasheet's table 4: electrical characteristics */
1639597dfb2aSAngel Iglesias 	usleep_range(2500, 3000);
1640597dfb2aSAngel Iglesias 
1641597dfb2aSAngel Iglesias 	/* Set default DSP mode settings */
1642597dfb2aSAngel Iglesias 	reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) |
1643597dfb2aSAngel Iglesias 		  BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN;
1644597dfb2aSAngel Iglesias 
1645597dfb2aSAngel Iglesias 	ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG,
1646597dfb2aSAngel Iglesias 				 BMP580_DSP_COMP_MASK |
1647597dfb2aSAngel Iglesias 				 BMP580_DSP_SHDW_IIR_TEMP_EN |
1648597dfb2aSAngel Iglesias 				 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val);
1649597dfb2aSAngel Iglesias 
1650597dfb2aSAngel Iglesias 	/* Configure oversampling */
1651597dfb2aSAngel Iglesias 	reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) |
1652597dfb2aSAngel Iglesias 		  FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) |
1653597dfb2aSAngel Iglesias 		  BMP580_OSR_PRESS_EN;
1654597dfb2aSAngel Iglesias 
1655597dfb2aSAngel Iglesias 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG,
1656597dfb2aSAngel Iglesias 				       BMP580_OSR_TEMP_MASK | BMP580_OSR_PRESS_MASK |
1657597dfb2aSAngel Iglesias 				       BMP580_OSR_PRESS_EN,
1658597dfb2aSAngel Iglesias 				       reg_val, &aux);
1659597dfb2aSAngel Iglesias 	if (ret) {
1660597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to write oversampling register\n");
1661597dfb2aSAngel Iglesias 		return ret;
1662597dfb2aSAngel Iglesias 	}
1663597dfb2aSAngel Iglesias 	change = change || aux;
1664597dfb2aSAngel Iglesias 
1665597dfb2aSAngel Iglesias 	/* Configure output data rate */
1666597dfb2aSAngel Iglesias 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK,
1667597dfb2aSAngel Iglesias 				       FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq),
1668597dfb2aSAngel Iglesias 				       &aux);
1669597dfb2aSAngel Iglesias 	if (ret) {
1670597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to write ODR configuration register\n");
1671597dfb2aSAngel Iglesias 		return ret;
1672597dfb2aSAngel Iglesias 	}
1673597dfb2aSAngel Iglesias 	change = change || aux;
1674597dfb2aSAngel Iglesias 
1675597dfb2aSAngel Iglesias 	/* Set filter data */
1676597dfb2aSAngel Iglesias 	reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) |
1677597dfb2aSAngel Iglesias 		  FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff);
1678597dfb2aSAngel Iglesias 
1679597dfb2aSAngel Iglesias 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_DSP_IIR,
1680597dfb2aSAngel Iglesias 				       BMP580_DSP_IIR_PRESS_MASK |
1681597dfb2aSAngel Iglesias 				       BMP580_DSP_IIR_TEMP_MASK,
1682597dfb2aSAngel Iglesias 				       reg_val, &aux);
1683597dfb2aSAngel Iglesias 	if (ret) {
1684597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to write config register\n");
1685597dfb2aSAngel Iglesias 		return ret;
1686597dfb2aSAngel Iglesias 	}
1687597dfb2aSAngel Iglesias 	change = change || aux;
1688597dfb2aSAngel Iglesias 
1689597dfb2aSAngel Iglesias 	/* Restore sensor to normal operation mode */
1690597dfb2aSAngel Iglesias 	ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1691597dfb2aSAngel Iglesias 				BMP580_MODE_MASK,
1692597dfb2aSAngel Iglesias 				FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_NORMAL));
1693597dfb2aSAngel Iglesias 	if (ret) {
1694597dfb2aSAngel Iglesias 		dev_err(data->dev, "failed to set normal mode\n");
1695597dfb2aSAngel Iglesias 		return ret;
1696597dfb2aSAngel Iglesias 	}
1697597dfb2aSAngel Iglesias 	/* From datasheet's table 4: electrical characteristics */
1698597dfb2aSAngel Iglesias 	usleep_range(3000, 3500);
1699597dfb2aSAngel Iglesias 
1700597dfb2aSAngel Iglesias 	if (change) {
1701597dfb2aSAngel Iglesias 		/*
1702597dfb2aSAngel Iglesias 		 * Check if ODR and OSR settings are valid or we are
1703597dfb2aSAngel Iglesias 		 * operating in a degraded mode.
1704597dfb2aSAngel Iglesias 		 */
1705597dfb2aSAngel Iglesias 		ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp);
1706597dfb2aSAngel Iglesias 		if (ret) {
1707597dfb2aSAngel Iglesias 			dev_err(data->dev, "error reading effective OSR register\n");
1708597dfb2aSAngel Iglesias 			return ret;
1709597dfb2aSAngel Iglesias 		}
1710597dfb2aSAngel Iglesias 		if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) {
1711597dfb2aSAngel Iglesias 			dev_warn(data->dev, "OSR and ODR incompatible settings detected\n");
1712597dfb2aSAngel Iglesias 			/* Set current OSR settings from data on effective OSR */
1713597dfb2aSAngel Iglesias 			data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp);
1714597dfb2aSAngel Iglesias 			data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp);
1715597dfb2aSAngel Iglesias 			return -EINVAL;
1716597dfb2aSAngel Iglesias 		}
1717597dfb2aSAngel Iglesias 	}
1718597dfb2aSAngel Iglesias 
1719597dfb2aSAngel Iglesias 	return 0;
1720597dfb2aSAngel Iglesias }
1721597dfb2aSAngel Iglesias 
1722597dfb2aSAngel Iglesias static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
1723597dfb2aSAngel Iglesias 
1724597dfb2aSAngel Iglesias const struct bmp280_chip_info bmp580_chip_info = {
1725597dfb2aSAngel Iglesias 	.id_reg = BMP580_REG_CHIP_ID,
1726597dfb2aSAngel Iglesias 	.chip_id = BMP580_CHIP_ID,
1727597dfb2aSAngel Iglesias 	.regmap_config = &bmp580_regmap_config,
1728597dfb2aSAngel Iglesias 	.start_up_time = 2000,
1729597dfb2aSAngel Iglesias 	.channels = bmp380_channels,
1730597dfb2aSAngel Iglesias 	.num_channels = 2,
1731597dfb2aSAngel Iglesias 
1732597dfb2aSAngel Iglesias 	.oversampling_temp_avail = bmp580_oversampling_avail,
1733597dfb2aSAngel Iglesias 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1734597dfb2aSAngel Iglesias 	.oversampling_temp_default = ilog2(1),
1735597dfb2aSAngel Iglesias 
1736597dfb2aSAngel Iglesias 	.oversampling_press_avail = bmp580_oversampling_avail,
1737597dfb2aSAngel Iglesias 	.num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1738597dfb2aSAngel Iglesias 	.oversampling_press_default = ilog2(4),
1739597dfb2aSAngel Iglesias 
1740597dfb2aSAngel Iglesias 	.sampling_freq_avail = bmp580_odr_table,
1741597dfb2aSAngel Iglesias 	.num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2,
1742597dfb2aSAngel Iglesias 	.sampling_freq_default = BMP580_ODR_50HZ,
1743597dfb2aSAngel Iglesias 
1744597dfb2aSAngel Iglesias 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1745597dfb2aSAngel Iglesias 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1746597dfb2aSAngel Iglesias 	.iir_filter_coeff_default = 2,
1747597dfb2aSAngel Iglesias 
1748597dfb2aSAngel Iglesias 	.chip_config = bmp580_chip_config,
1749597dfb2aSAngel Iglesias 	.read_temp = bmp580_read_temp,
1750597dfb2aSAngel Iglesias 	.read_press = bmp580_read_press,
1751597dfb2aSAngel Iglesias 	.preinit = bmp580_preinit,
1752597dfb2aSAngel Iglesias };
1753597dfb2aSAngel Iglesias EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280);
1754597dfb2aSAngel Iglesias 
bmp180_measure(struct bmp280_data * data,u8 ctrl_meas)175514e8015fSLinus Walleij static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
175614e8015fSLinus Walleij {
175714e8015fSLinus Walleij 	const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
175814e8015fSLinus Walleij 	unsigned int delay_us;
175914e8015fSLinus Walleij 	unsigned int ctrl;
17605f0c359dSAngel Iglesias 	int ret;
176114e8015fSLinus Walleij 
1762aae95394SLinus Walleij 	if (data->use_eoc)
176397b31a6fSAndy Shevchenko 		reinit_completion(&data->done);
1764aae95394SLinus Walleij 
176514e8015fSLinus Walleij 	ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
176614e8015fSLinus Walleij 	if (ret)
176714e8015fSLinus Walleij 		return ret;
176814e8015fSLinus Walleij 
1769aae95394SLinus Walleij 	if (data->use_eoc) {
1770aae95394SLinus Walleij 		/*
1771aae95394SLinus Walleij 		 * If we have a completion interrupt, use it, wait up to
1772aae95394SLinus Walleij 		 * 100ms. The longest conversion time listed is 76.5 ms for
1773aae95394SLinus Walleij 		 * advanced resolution mode.
1774aae95394SLinus Walleij 		 */
1775aae95394SLinus Walleij 		ret = wait_for_completion_timeout(&data->done,
1776aae95394SLinus Walleij 						  1 + msecs_to_jiffies(100));
1777aae95394SLinus Walleij 		if (!ret)
1778aae95394SLinus Walleij 			dev_err(data->dev, "timeout waiting for completion\n");
1779aae95394SLinus Walleij 	} else {
17802405f8ccSAngel Iglesias 		if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
178114e8015fSLinus Walleij 			delay_us = 4500;
178214e8015fSLinus Walleij 		else
1783aae95394SLinus Walleij 			delay_us =
1784aae95394SLinus Walleij 				conversion_time_max[data->oversampling_press];
178514e8015fSLinus Walleij 
178614e8015fSLinus Walleij 		usleep_range(delay_us, delay_us + 1000);
1787aae95394SLinus Walleij 	}
178814e8015fSLinus Walleij 
178914e8015fSLinus Walleij 	ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
179014e8015fSLinus Walleij 	if (ret)
179114e8015fSLinus Walleij 		return ret;
179214e8015fSLinus Walleij 
179314e8015fSLinus Walleij 	/* The value of this bit reset to "0" after conversion is complete */
179414e8015fSLinus Walleij 	if (ctrl & BMP180_MEAS_SCO)
179514e8015fSLinus Walleij 		return -EIO;
179614e8015fSLinus Walleij 
179714e8015fSLinus Walleij 	return 0;
179814e8015fSLinus Walleij }
179914e8015fSLinus Walleij 
bmp180_read_adc_temp(struct bmp280_data * data,int * val)180014e8015fSLinus Walleij static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
180114e8015fSLinus Walleij {
180214e8015fSLinus Walleij 	int ret;
180314e8015fSLinus Walleij 
18042405f8ccSAngel Iglesias 	ret = bmp180_measure(data,
18052405f8ccSAngel Iglesias 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
18062405f8ccSAngel Iglesias 			     BMP180_MEAS_SCO);
180714e8015fSLinus Walleij 	if (ret)
180814e8015fSLinus Walleij 		return ret;
180914e8015fSLinus Walleij 
1810327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1811327b5c05SAngel Iglesias 			       &data->be16, sizeof(data->be16));
181214e8015fSLinus Walleij 	if (ret)
181314e8015fSLinus Walleij 		return ret;
181414e8015fSLinus Walleij 
1815327b5c05SAngel Iglesias 	*val = be16_to_cpu(data->be16);
181614e8015fSLinus Walleij 
181714e8015fSLinus Walleij 	return 0;
181814e8015fSLinus Walleij }
181914e8015fSLinus Walleij 
bmp180_read_calib(struct bmp280_data * data)1820b00e805aSAngel Iglesias static int bmp180_read_calib(struct bmp280_data *data)
182114e8015fSLinus Walleij {
1822b00e805aSAngel Iglesias 	struct bmp180_calib *calib = &data->calib.bmp180;
182314e8015fSLinus Walleij 	int ret;
182414e8015fSLinus Walleij 	int i;
182514e8015fSLinus Walleij 
1826327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
1827327b5c05SAngel Iglesias 			       data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
182814e8015fSLinus Walleij 
182914e8015fSLinus Walleij 	if (ret < 0)
183014e8015fSLinus Walleij 		return ret;
183114e8015fSLinus Walleij 
183214e8015fSLinus Walleij 	/* None of the words has the value 0 or 0xFFFF */
1833327b5c05SAngel Iglesias 	for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) {
1834327b5c05SAngel Iglesias 		if (data->bmp180_cal_buf[i] == cpu_to_be16(0) ||
1835327b5c05SAngel Iglesias 		    data->bmp180_cal_buf[i] == cpu_to_be16(0xffff))
183614e8015fSLinus Walleij 			return -EIO;
183714e8015fSLinus Walleij 	}
183814e8015fSLinus Walleij 
1839b33b7d5aSLinus Walleij 	/* Toss the calibration data into the entropy pool */
1840327b5c05SAngel Iglesias 	add_device_randomness(data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1841b33b7d5aSLinus Walleij 
1842327b5c05SAngel Iglesias 	calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]);
1843327b5c05SAngel Iglesias 	calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]);
1844327b5c05SAngel Iglesias 	calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]);
1845327b5c05SAngel Iglesias 	calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]);
1846327b5c05SAngel Iglesias 	calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]);
1847327b5c05SAngel Iglesias 	calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]);
1848327b5c05SAngel Iglesias 	calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]);
1849327b5c05SAngel Iglesias 	calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]);
1850327b5c05SAngel Iglesias 	calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]);
1851327b5c05SAngel Iglesias 	calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]);
1852327b5c05SAngel Iglesias 	calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]);
185314e8015fSLinus Walleij 
185414e8015fSLinus Walleij 	return 0;
185514e8015fSLinus Walleij }
185614e8015fSLinus Walleij 
185714e8015fSLinus Walleij /*
185814e8015fSLinus Walleij  * Returns temperature in DegC, resolution is 0.1 DegC.
185914e8015fSLinus Walleij  * t_fine carries fine temperature as global value.
186014e8015fSLinus Walleij  *
186114e8015fSLinus Walleij  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
186214e8015fSLinus Walleij  */
bmp180_compensate_temp(struct bmp280_data * data,s32 adc_temp)186314e8015fSLinus Walleij static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
186414e8015fSLinus Walleij {
18652e419aecSStefan Tatschner 	struct bmp180_calib *calib = &data->calib.bmp180;
18665f0c359dSAngel Iglesias 	s32 x1, x2;
186714e8015fSLinus Walleij 
1868b33b7d5aSLinus Walleij 	x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
1869b33b7d5aSLinus Walleij 	x2 = (calib->MC << 11) / (x1 + calib->MD);
187014e8015fSLinus Walleij 	data->t_fine = x1 + x2;
187114e8015fSLinus Walleij 
187214e8015fSLinus Walleij 	return (data->t_fine + 8) >> 4;
187314e8015fSLinus Walleij }
187414e8015fSLinus Walleij 
bmp180_read_temp(struct bmp280_data * data,int * val,int * val2)1875597dfb2aSAngel Iglesias static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2)
187614e8015fSLinus Walleij {
187714e8015fSLinus Walleij 	s32 adc_temp, comp_temp;
18785f0c359dSAngel Iglesias 	int ret;
187914e8015fSLinus Walleij 
188014e8015fSLinus Walleij 	ret = bmp180_read_adc_temp(data, &adc_temp);
188114e8015fSLinus Walleij 	if (ret)
188214e8015fSLinus Walleij 		return ret;
188314e8015fSLinus Walleij 
188414e8015fSLinus Walleij 	comp_temp = bmp180_compensate_temp(data, adc_temp);
188514e8015fSLinus Walleij 
188614e8015fSLinus Walleij 	/*
188714e8015fSLinus Walleij 	 * val might be NULL if we're called by the read_press routine,
188814e8015fSLinus Walleij 	 * who only cares about the carry over t_fine value.
188914e8015fSLinus Walleij 	 */
189014e8015fSLinus Walleij 	if (val) {
189114e8015fSLinus Walleij 		*val = comp_temp * 100;
189214e8015fSLinus Walleij 		return IIO_VAL_INT;
189314e8015fSLinus Walleij 	}
189414e8015fSLinus Walleij 
189514e8015fSLinus Walleij 	return 0;
189614e8015fSLinus Walleij }
189714e8015fSLinus Walleij 
bmp180_read_adc_press(struct bmp280_data * data,int * val)189814e8015fSLinus Walleij static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
189914e8015fSLinus Walleij {
190014e8015fSLinus Walleij 	u8 oss = data->oversampling_press;
19015f0c359dSAngel Iglesias 	int ret;
190214e8015fSLinus Walleij 
19032405f8ccSAngel Iglesias 	ret = bmp180_measure(data,
19042405f8ccSAngel Iglesias 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
19052405f8ccSAngel Iglesias 			     FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
19062405f8ccSAngel Iglesias 			     BMP180_MEAS_SCO);
190714e8015fSLinus Walleij 	if (ret)
190814e8015fSLinus Walleij 		return ret;
190914e8015fSLinus Walleij 
1910327b5c05SAngel Iglesias 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1911327b5c05SAngel Iglesias 			       data->buf, sizeof(data->buf));
191214e8015fSLinus Walleij 	if (ret)
191314e8015fSLinus Walleij 		return ret;
191414e8015fSLinus Walleij 
1915327b5c05SAngel Iglesias 	*val = get_unaligned_be24(data->buf) >> (8 - oss);
191614e8015fSLinus Walleij 
191714e8015fSLinus Walleij 	return 0;
191814e8015fSLinus Walleij }
191914e8015fSLinus Walleij 
192014e8015fSLinus Walleij /*
192114e8015fSLinus Walleij  * Returns pressure in Pa, resolution is 1 Pa.
192214e8015fSLinus Walleij  *
192314e8015fSLinus Walleij  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
192414e8015fSLinus Walleij  */
bmp180_compensate_press(struct bmp280_data * data,s32 adc_press)192514e8015fSLinus Walleij static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
192614e8015fSLinus Walleij {
19275f0c359dSAngel Iglesias 	struct bmp180_calib *calib = &data->calib.bmp180;
19285f0c359dSAngel Iglesias 	s32 oss = data->oversampling_press;
192914e8015fSLinus Walleij 	s32 x1, x2, x3, p;
193014e8015fSLinus Walleij 	s32 b3, b6;
193114e8015fSLinus Walleij 	u32 b4, b7;
193214e8015fSLinus Walleij 
193314e8015fSLinus Walleij 	b6 = data->t_fine - 4000;
1934b33b7d5aSLinus Walleij 	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
1935b33b7d5aSLinus Walleij 	x2 = calib->AC2 * b6 >> 11;
193614e8015fSLinus Walleij 	x3 = x1 + x2;
1937b33b7d5aSLinus Walleij 	b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
1938b33b7d5aSLinus Walleij 	x1 = calib->AC3 * b6 >> 13;
1939b33b7d5aSLinus Walleij 	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
194014e8015fSLinus Walleij 	x3 = (x1 + x2 + 2) >> 2;
1941b33b7d5aSLinus Walleij 	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
194214e8015fSLinus Walleij 	b7 = ((u32)adc_press - b3) * (50000 >> oss);
194314e8015fSLinus Walleij 	if (b7 < 0x80000000)
194414e8015fSLinus Walleij 		p = (b7 * 2) / b4;
194514e8015fSLinus Walleij 	else
194614e8015fSLinus Walleij 		p = (b7 / b4) * 2;
194714e8015fSLinus Walleij 
194814e8015fSLinus Walleij 	x1 = (p >> 8) * (p >> 8);
194914e8015fSLinus Walleij 	x1 = (x1 * 3038) >> 16;
195014e8015fSLinus Walleij 	x2 = (-7357 * p) >> 16;
195114e8015fSLinus Walleij 
195214e8015fSLinus Walleij 	return p + ((x1 + x2 + 3791) >> 4);
195314e8015fSLinus Walleij }
195414e8015fSLinus Walleij 
bmp180_read_press(struct bmp280_data * data,int * val,int * val2)195514e8015fSLinus Walleij static int bmp180_read_press(struct bmp280_data *data,
195614e8015fSLinus Walleij 			     int *val, int *val2)
195714e8015fSLinus Walleij {
195814e8015fSLinus Walleij 	u32 comp_press;
19595f0c359dSAngel Iglesias 	s32 adc_press;
19605f0c359dSAngel Iglesias 	int ret;
196114e8015fSLinus Walleij 
196214e8015fSLinus Walleij 	/* Read and compensate temperature so we get a reading of t_fine. */
1963597dfb2aSAngel Iglesias 	ret = bmp180_read_temp(data, NULL, NULL);
196414e8015fSLinus Walleij 	if (ret)
196514e8015fSLinus Walleij 		return ret;
196614e8015fSLinus Walleij 
196714e8015fSLinus Walleij 	ret = bmp180_read_adc_press(data, &adc_press);
196814e8015fSLinus Walleij 	if (ret)
196914e8015fSLinus Walleij 		return ret;
197014e8015fSLinus Walleij 
197114e8015fSLinus Walleij 	comp_press = bmp180_compensate_press(data, adc_press);
197214e8015fSLinus Walleij 
197314e8015fSLinus Walleij 	*val = comp_press;
197414e8015fSLinus Walleij 	*val2 = 1000;
197514e8015fSLinus Walleij 
197614e8015fSLinus Walleij 	return IIO_VAL_FRACTIONAL;
197714e8015fSLinus Walleij }
197814e8015fSLinus Walleij 
bmp180_chip_config(struct bmp280_data * data)197914e8015fSLinus Walleij static int bmp180_chip_config(struct bmp280_data *data)
198014e8015fSLinus Walleij {
198114e8015fSLinus Walleij 	return 0;
198214e8015fSLinus Walleij }
198314e8015fSLinus Walleij 
198414e8015fSLinus Walleij static const int bmp180_oversampling_temp_avail[] = { 1 };
198514e8015fSLinus Walleij static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
198614e8015fSLinus Walleij 
19870b0b7726SAngel Iglesias const struct bmp280_chip_info bmp180_chip_info = {
1988b00e805aSAngel Iglesias 	.id_reg = BMP280_REG_ID,
19890b0b7726SAngel Iglesias 	.chip_id = BMP180_CHIP_ID,
19900b0b7726SAngel Iglesias 	.regmap_config = &bmp180_regmap_config,
1991b00e805aSAngel Iglesias 	.start_up_time = 2000,
199210b40ffbSAngel Iglesias 	.channels = bmp280_channels,
1993b00e805aSAngel Iglesias 	.num_channels = 2,
1994b00e805aSAngel Iglesias 
199514e8015fSLinus Walleij 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
199614e8015fSLinus Walleij 	.num_oversampling_temp_avail =
199714e8015fSLinus Walleij 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
1998b00e805aSAngel Iglesias 	.oversampling_temp_default = 0,
199914e8015fSLinus Walleij 
200014e8015fSLinus Walleij 	.oversampling_press_avail = bmp180_oversampling_press_avail,
200114e8015fSLinus Walleij 	.num_oversampling_press_avail =
200214e8015fSLinus Walleij 		ARRAY_SIZE(bmp180_oversampling_press_avail),
2003b00e805aSAngel Iglesias 	.oversampling_press_default = BMP180_MEAS_PRESS_8X,
200414e8015fSLinus Walleij 
200514e8015fSLinus Walleij 	.chip_config = bmp180_chip_config,
200614e8015fSLinus Walleij 	.read_temp = bmp180_read_temp,
200714e8015fSLinus Walleij 	.read_press = bmp180_read_press,
2008b00e805aSAngel Iglesias 	.read_calib = bmp180_read_calib,
200914e8015fSLinus Walleij };
20100b0b7726SAngel Iglesias EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280);
201114e8015fSLinus Walleij 
bmp085_eoc_irq(int irq,void * d)2012aae95394SLinus Walleij static irqreturn_t bmp085_eoc_irq(int irq, void *d)
2013aae95394SLinus Walleij {
2014aae95394SLinus Walleij 	struct bmp280_data *data = d;
2015aae95394SLinus Walleij 
2016aae95394SLinus Walleij 	complete(&data->done);
2017aae95394SLinus Walleij 
2018aae95394SLinus Walleij 	return IRQ_HANDLED;
2019aae95394SLinus Walleij }
2020aae95394SLinus Walleij 
bmp085_fetch_eoc_irq(struct device * dev,const char * name,int irq,struct bmp280_data * data)2021aae95394SLinus Walleij static int bmp085_fetch_eoc_irq(struct device *dev,
2022aae95394SLinus Walleij 				const char *name,
2023aae95394SLinus Walleij 				int irq,
2024aae95394SLinus Walleij 				struct bmp280_data *data)
2025aae95394SLinus Walleij {
2026aae95394SLinus Walleij 	unsigned long irq_trig;
2027aae95394SLinus Walleij 	int ret;
2028aae95394SLinus Walleij 
2029aae95394SLinus Walleij 	irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
2030aae95394SLinus Walleij 	if (irq_trig != IRQF_TRIGGER_RISING) {
2031792897ceSAndy Shevchenko 		dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
2032aae95394SLinus Walleij 		irq_trig = IRQF_TRIGGER_RISING;
2033aae95394SLinus Walleij 	}
203497b31a6fSAndy Shevchenko 
203597b31a6fSAndy Shevchenko 	init_completion(&data->done);
203697b31a6fSAndy Shevchenko 
2037aae95394SLinus Walleij 	ret = devm_request_threaded_irq(dev,
2038aae95394SLinus Walleij 			irq,
2039aae95394SLinus Walleij 			bmp085_eoc_irq,
2040aae95394SLinus Walleij 			NULL,
2041aae95394SLinus Walleij 			irq_trig,
2042aae95394SLinus Walleij 			name,
2043aae95394SLinus Walleij 			data);
2044aae95394SLinus Walleij 	if (ret) {
2045aae95394SLinus Walleij 		/* Bail out without IRQ but keep the driver in place */
2046aae95394SLinus Walleij 		dev_err(dev, "unable to request DRDY IRQ\n");
2047aae95394SLinus Walleij 		return 0;
2048aae95394SLinus Walleij 	}
2049aae95394SLinus Walleij 
2050aae95394SLinus Walleij 	data->use_eoc = true;
2051aae95394SLinus Walleij 	return 0;
2052aae95394SLinus Walleij }
2053aae95394SLinus Walleij 
bmp280_pm_disable(void * data)20542f4292a8SBartosz Golaszewski static void bmp280_pm_disable(void *data)
20552f4292a8SBartosz Golaszewski {
20562f4292a8SBartosz Golaszewski 	struct device *dev = data;
20572f4292a8SBartosz Golaszewski 
20582f4292a8SBartosz Golaszewski 	pm_runtime_get_sync(dev);
20592f4292a8SBartosz Golaszewski 	pm_runtime_put_noidle(dev);
20602f4292a8SBartosz Golaszewski 	pm_runtime_disable(dev);
20612f4292a8SBartosz Golaszewski }
20622f4292a8SBartosz Golaszewski 
bmp280_regulators_disable(void * data)20632f4292a8SBartosz Golaszewski static void bmp280_regulators_disable(void *data)
20642f4292a8SBartosz Golaszewski {
20652f4292a8SBartosz Golaszewski 	struct regulator_bulk_data *supplies = data;
20662f4292a8SBartosz Golaszewski 
20672f4292a8SBartosz Golaszewski 	regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
20682f4292a8SBartosz Golaszewski }
20692f4292a8SBartosz Golaszewski 
bmp280_common_probe(struct device * dev,struct regmap * regmap,const struct bmp280_chip_info * chip_info,const char * name,int irq)207014e8015fSLinus Walleij int bmp280_common_probe(struct device *dev,
207114e8015fSLinus Walleij 			struct regmap *regmap,
20720b0b7726SAngel Iglesias 			const struct bmp280_chip_info *chip_info,
2073aae95394SLinus Walleij 			const char *name,
2074aae95394SLinus Walleij 			int irq)
207514e8015fSLinus Walleij {
207614e8015fSLinus Walleij 	struct iio_dev *indio_dev;
207714e8015fSLinus Walleij 	struct bmp280_data *data;
207814e8015fSLinus Walleij 	struct gpio_desc *gpiod;
20795f0c359dSAngel Iglesias 	unsigned int chip_id;
20805f0c359dSAngel Iglesias 	int ret;
208114e8015fSLinus Walleij 
208214e8015fSLinus Walleij 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
208314e8015fSLinus Walleij 	if (!indio_dev)
208414e8015fSLinus Walleij 		return -ENOMEM;
208514e8015fSLinus Walleij 
208614e8015fSLinus Walleij 	data = iio_priv(indio_dev);
208714e8015fSLinus Walleij 	mutex_init(&data->lock);
208814e8015fSLinus Walleij 	data->dev = dev;
208914e8015fSLinus Walleij 
209014e8015fSLinus Walleij 	indio_dev->name = name;
209114e8015fSLinus Walleij 	indio_dev->info = &bmp280_info;
209214e8015fSLinus Walleij 	indio_dev->modes = INDIO_DIRECT_MODE;
209314e8015fSLinus Walleij 
2094b00e805aSAngel Iglesias 	data->chip_info = chip_info;
2095b00e805aSAngel Iglesias 
2096b00e805aSAngel Iglesias 	/* Apply initial values from chip info structure */
209710b40ffbSAngel Iglesias 	indio_dev->channels = chip_info->channels;
2098b00e805aSAngel Iglesias 	indio_dev->num_channels = chip_info->num_channels;
2099b00e805aSAngel Iglesias 	data->oversampling_press = chip_info->oversampling_press_default;
2100b00e805aSAngel Iglesias 	data->oversampling_humid = chip_info->oversampling_humid_default;
2101b00e805aSAngel Iglesias 	data->oversampling_temp = chip_info->oversampling_temp_default;
210210b40ffbSAngel Iglesias 	data->iir_filter_coeff = chip_info->iir_filter_coeff_default;
210310b40ffbSAngel Iglesias 	data->sampling_freq = chip_info->sampling_freq_default;
2104b00e805aSAngel Iglesias 	data->start_up_time = chip_info->start_up_time;
210514e8015fSLinus Walleij 
210614e8015fSLinus Walleij 	/* Bring up regulators */
21071372d1a1SBartosz Golaszewski 	regulator_bulk_set_supply_names(data->supplies,
21081372d1a1SBartosz Golaszewski 					bmp280_supply_names,
21091372d1a1SBartosz Golaszewski 					BMP280_NUM_SUPPLIES);
21101372d1a1SBartosz Golaszewski 
21111372d1a1SBartosz Golaszewski 	ret = devm_regulator_bulk_get(dev,
21121372d1a1SBartosz Golaszewski 				      BMP280_NUM_SUPPLIES, data->supplies);
211314e8015fSLinus Walleij 	if (ret) {
21141372d1a1SBartosz Golaszewski 		dev_err(dev, "failed to get regulators\n");
211514e8015fSLinus Walleij 		return ret;
211614e8015fSLinus Walleij 	}
21171372d1a1SBartosz Golaszewski 
21181372d1a1SBartosz Golaszewski 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
211914e8015fSLinus Walleij 	if (ret) {
21201372d1a1SBartosz Golaszewski 		dev_err(dev, "failed to enable regulators\n");
21211372d1a1SBartosz Golaszewski 		return ret;
212214e8015fSLinus Walleij 	}
21231372d1a1SBartosz Golaszewski 
21242f4292a8SBartosz Golaszewski 	ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
21252f4292a8SBartosz Golaszewski 				       data->supplies);
21262f4292a8SBartosz Golaszewski 	if (ret)
21272f4292a8SBartosz Golaszewski 		return ret;
21282f4292a8SBartosz Golaszewski 
212914e8015fSLinus Walleij 	/* Wait to make sure we started up properly */
2130071cf249SAniroop Mathur 	usleep_range(data->start_up_time, data->start_up_time + 100);
213114e8015fSLinus Walleij 
213214e8015fSLinus Walleij 	/* Bring chip out of reset if there is an assigned GPIO line */
2133df6e7125SAndy Shevchenko 	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
213414e8015fSLinus Walleij 	/* Deassert the signal */
2135df6e7125SAndy Shevchenko 	if (gpiod) {
213614e8015fSLinus Walleij 		dev_info(dev, "release reset\n");
213714e8015fSLinus Walleij 		gpiod_set_value(gpiod, 0);
213814e8015fSLinus Walleij 	}
213914e8015fSLinus Walleij 
214014e8015fSLinus Walleij 	data->regmap = regmap;
2141b00e805aSAngel Iglesias 
2142b00e805aSAngel Iglesias 	ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
214314e8015fSLinus Walleij 	if (ret < 0)
21442f4292a8SBartosz Golaszewski 		return ret;
21450b0b7726SAngel Iglesias 	if (chip_id != data->chip_info->chip_id) {
214614e8015fSLinus Walleij 		dev_err(dev, "bad chip id: expected %x got %x\n",
21470b0b7726SAngel Iglesias 			data->chip_info->chip_id, chip_id);
21482f4292a8SBartosz Golaszewski 		return -EINVAL;
214914e8015fSLinus Walleij 	}
215014e8015fSLinus Walleij 
2151c25ea00fSAngel Iglesias 	if (data->chip_info->preinit) {
2152c25ea00fSAngel Iglesias 		ret = data->chip_info->preinit(data);
2153c25ea00fSAngel Iglesias 		if (ret)
2154c25ea00fSAngel Iglesias 			return dev_err_probe(data->dev, ret,
2155c25ea00fSAngel Iglesias 					     "error running preinit tasks\n");
21568d329309SAngel Iglesias 	}
21578d329309SAngel Iglesias 
215814e8015fSLinus Walleij 	ret = data->chip_info->chip_config(data);
215914e8015fSLinus Walleij 	if (ret < 0)
21602f4292a8SBartosz Golaszewski 		return ret;
216114e8015fSLinus Walleij 
216214e8015fSLinus Walleij 	dev_set_drvdata(dev, indio_dev);
216314e8015fSLinus Walleij 
2164aae95394SLinus Walleij 	/*
21652e419aecSStefan Tatschner 	 * Some chips have calibration parameters "programmed into the devices'
21662e419aecSStefan Tatschner 	 * non-volatile memory during production". Let's read them out at probe
21672e419aecSStefan Tatschner 	 * time once. They will not change.
2168b33b7d5aSLinus Walleij 	 */
2169b00e805aSAngel Iglesias 
21704d545f96SAngel Iglesias 	if (data->chip_info->read_calib) {
2171b00e805aSAngel Iglesias 		ret = data->chip_info->read_calib(data);
2172b00e805aSAngel Iglesias 		if (ret < 0)
2173b00e805aSAngel Iglesias 			return dev_err_probe(data->dev, ret,
21742e419aecSStefan Tatschner 					     "failed to read calibration coefficients\n");
21754d545f96SAngel Iglesias 	}
2176b33b7d5aSLinus Walleij 
2177b33b7d5aSLinus Walleij 	/*
2178aae95394SLinus Walleij 	 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
2179aae95394SLinus Walleij 	 * however as it happens, the BMP085 shares the chip ID of BMP180
2180aae95394SLinus Walleij 	 * so we look for an IRQ if we have that.
2181aae95394SLinus Walleij 	 */
2182*85dfb43bSPhil Elwell 	if (irq > 0 && (chip_id  == BMP180_CHIP_ID)) {
2183aae95394SLinus Walleij 		ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
2184aae95394SLinus Walleij 		if (ret)
21852f4292a8SBartosz Golaszewski 			return ret;
2186aae95394SLinus Walleij 	}
2187aae95394SLinus Walleij 
21883d838118SLinus Walleij 	/* Enable runtime PM */
21893d838118SLinus Walleij 	pm_runtime_get_noresume(dev);
21903d838118SLinus Walleij 	pm_runtime_set_active(dev);
21913d838118SLinus Walleij 	pm_runtime_enable(dev);
21923d838118SLinus Walleij 	/*
21933d838118SLinus Walleij 	 * Set autosuspend to two orders of magnitude larger than the
21943d838118SLinus Walleij 	 * start-up time.
21953d838118SLinus Walleij 	 */
2196071cf249SAniroop Mathur 	pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
21973d838118SLinus Walleij 	pm_runtime_use_autosuspend(dev);
21983d838118SLinus Walleij 	pm_runtime_put(dev);
21993d838118SLinus Walleij 
22002f4292a8SBartosz Golaszewski 	ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
220114e8015fSLinus Walleij 	if (ret)
220214e8015fSLinus Walleij 		return ret;
22032f4292a8SBartosz Golaszewski 
22042f4292a8SBartosz Golaszewski 	return devm_iio_device_register(dev, indio_dev);
220514e8015fSLinus Walleij }
22060f26b9dbSJonathan Cameron EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
220714e8015fSLinus Walleij 
bmp280_runtime_suspend(struct device * dev)22083d838118SLinus Walleij static int bmp280_runtime_suspend(struct device *dev)
22093d838118SLinus Walleij {
221031f453eaSLinus Walleij 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
221131f453eaSLinus Walleij 	struct bmp280_data *data = iio_priv(indio_dev);
22123d838118SLinus Walleij 
22131372d1a1SBartosz Golaszewski 	return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
22143d838118SLinus Walleij }
22153d838118SLinus Walleij 
bmp280_runtime_resume(struct device * dev)22163d838118SLinus Walleij static int bmp280_runtime_resume(struct device *dev)
22173d838118SLinus Walleij {
221831f453eaSLinus Walleij 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
221931f453eaSLinus Walleij 	struct bmp280_data *data = iio_priv(indio_dev);
22203d838118SLinus Walleij 	int ret;
22213d838118SLinus Walleij 
22221372d1a1SBartosz Golaszewski 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
22233d838118SLinus Walleij 	if (ret)
22243d838118SLinus Walleij 		return ret;
2225071cf249SAniroop Mathur 	usleep_range(data->start_up_time, data->start_up_time + 100);
22263d838118SLinus Walleij 	return data->chip_info->chip_config(data);
22273d838118SLinus Walleij }
22283d838118SLinus Walleij 
22295865918fSPaul Cercueil EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
22305865918fSPaul Cercueil 			  bmp280_runtime_resume, NULL);
22313d838118SLinus Walleij 
223217118843SLinus Walleij MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
223317118843SLinus Walleij MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
223417118843SLinus Walleij MODULE_LICENSE("GPL v2");
2235