1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
4 * Copyright (c) 2012 Bosch Sensortec GmbH
5 * Copyright (c) 2012 Unixphere AB
6 * Copyright (c) 2014 Intel Corporation
7 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
8 *
9 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
10 *
11 * Datasheet:
12 * https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
13 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf
14 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
15 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf
16 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp390-ds002.pdf
17 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp581-ds004.pdf
18 *
19 * Notice:
20 * The link to the bmp180 datasheet points to an outdated version missing these changes:
21 * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26
22 * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4
23 * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26
24 */
25
26 #define pr_fmt(fmt) "bmp280: " fmt
27
28 #include <linux/bitops.h>
29 #include <linux/bitfield.h>
30 #include <linux/cleanup.h>
31 #include <linux/completion.h>
32 #include <linux/delay.h>
33 #include <linux/device.h>
34 #include <linux/gpio/consumer.h>
35 #include <linux/interrupt.h>
36 #include <linux/irq.h> /* For irq_get_irq_data() */
37 #include <linux/module.h>
38 #include <linux/nvmem-provider.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/random.h>
41 #include <linux/regmap.h>
42 #include <linux/regulator/consumer.h>
43
44 #include <linux/iio/buffer.h>
45 #include <linux/iio/iio.h>
46 #include <linux/iio/trigger_consumer.h>
47 #include <linux/iio/triggered_buffer.h>
48
49 #include <linux/unaligned.h>
50
51 #include "bmp280.h"
52
53 /*
54 * These enums are used for indexing into the array of calibration
55 * coefficients for BMP180.
56 */
57 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
58
59 enum bmp380_odr {
60 BMP380_ODR_200HZ,
61 BMP380_ODR_100HZ,
62 BMP380_ODR_50HZ,
63 BMP380_ODR_25HZ,
64 BMP380_ODR_12_5HZ,
65 BMP380_ODR_6_25HZ,
66 BMP380_ODR_3_125HZ,
67 BMP380_ODR_1_5625HZ,
68 BMP380_ODR_0_78HZ,
69 BMP380_ODR_0_39HZ,
70 BMP380_ODR_0_2HZ,
71 BMP380_ODR_0_1HZ,
72 BMP380_ODR_0_05HZ,
73 BMP380_ODR_0_02HZ,
74 BMP380_ODR_0_01HZ,
75 BMP380_ODR_0_006HZ,
76 BMP380_ODR_0_003HZ,
77 BMP380_ODR_0_0015HZ,
78 };
79
80 enum bmp580_odr {
81 BMP580_ODR_240HZ,
82 BMP580_ODR_218HZ,
83 BMP580_ODR_199HZ,
84 BMP580_ODR_179HZ,
85 BMP580_ODR_160HZ,
86 BMP580_ODR_149HZ,
87 BMP580_ODR_140HZ,
88 BMP580_ODR_129HZ,
89 BMP580_ODR_120HZ,
90 BMP580_ODR_110HZ,
91 BMP580_ODR_100HZ,
92 BMP580_ODR_89HZ,
93 BMP580_ODR_80HZ,
94 BMP580_ODR_70HZ,
95 BMP580_ODR_60HZ,
96 BMP580_ODR_50HZ,
97 BMP580_ODR_45HZ,
98 BMP580_ODR_40HZ,
99 BMP580_ODR_35HZ,
100 BMP580_ODR_30HZ,
101 BMP580_ODR_25HZ,
102 BMP580_ODR_20HZ,
103 BMP580_ODR_15HZ,
104 BMP580_ODR_10HZ,
105 BMP580_ODR_5HZ,
106 BMP580_ODR_4HZ,
107 BMP580_ODR_3HZ,
108 BMP580_ODR_2HZ,
109 BMP580_ODR_1HZ,
110 BMP580_ODR_0_5HZ,
111 BMP580_ODR_0_25HZ,
112 BMP580_ODR_0_125HZ,
113 };
114
115 /*
116 * These enums are used for indexing into the array of compensation
117 * parameters for BMP280.
118 */
119 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 };
120
121 enum {
122 /* Temperature calib indexes */
123 BMP380_T1 = 0,
124 BMP380_T2 = 2,
125 BMP380_T3 = 4,
126 /* Pressure calib indexes */
127 BMP380_P1 = 5,
128 BMP380_P2 = 7,
129 BMP380_P3 = 9,
130 BMP380_P4 = 10,
131 BMP380_P5 = 11,
132 BMP380_P6 = 13,
133 BMP380_P7 = 15,
134 BMP380_P8 = 16,
135 BMP380_P9 = 17,
136 BMP380_P10 = 19,
137 BMP380_P11 = 20,
138 };
139
140 enum bmp280_scan {
141 BMP280_PRESS,
142 BMP280_TEMP,
143 BME280_HUMID,
144 };
145
146 static const struct iio_chan_spec bmp280_channels[] = {
147 {
148 .type = IIO_PRESSURE,
149 /* PROCESSED maintained for ABI backwards compatibility */
150 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
151 BIT(IIO_CHAN_INFO_RAW) |
152 BIT(IIO_CHAN_INFO_SCALE) |
153 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
154 .scan_index = 0,
155 .scan_type = {
156 .sign = 'u',
157 .realbits = 32,
158 .storagebits = 32,
159 .endianness = IIO_CPU,
160 },
161 },
162 {
163 .type = IIO_TEMP,
164 /* PROCESSED maintained for ABI backwards compatibility */
165 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
166 BIT(IIO_CHAN_INFO_RAW) |
167 BIT(IIO_CHAN_INFO_SCALE) |
168 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
169 .scan_index = 1,
170 .scan_type = {
171 .sign = 's',
172 .realbits = 32,
173 .storagebits = 32,
174 .endianness = IIO_CPU,
175 },
176 },
177 IIO_CHAN_SOFT_TIMESTAMP(2),
178 };
179
180 static const struct iio_chan_spec bme280_channels[] = {
181 {
182 .type = IIO_PRESSURE,
183 /* PROCESSED maintained for ABI backwards compatibility */
184 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
185 BIT(IIO_CHAN_INFO_RAW) |
186 BIT(IIO_CHAN_INFO_SCALE) |
187 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
188 .scan_index = 0,
189 .scan_type = {
190 .sign = 'u',
191 .realbits = 32,
192 .storagebits = 32,
193 .endianness = IIO_CPU,
194 },
195 },
196 {
197 .type = IIO_TEMP,
198 /* PROCESSED maintained for ABI backwards compatibility */
199 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
200 BIT(IIO_CHAN_INFO_RAW) |
201 BIT(IIO_CHAN_INFO_SCALE) |
202 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
203 .scan_index = 1,
204 .scan_type = {
205 .sign = 's',
206 .realbits = 32,
207 .storagebits = 32,
208 .endianness = IIO_CPU,
209 },
210 },
211 {
212 .type = IIO_HUMIDITYRELATIVE,
213 /* PROCESSED maintained for ABI backwards compatibility */
214 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
215 BIT(IIO_CHAN_INFO_RAW) |
216 BIT(IIO_CHAN_INFO_SCALE) |
217 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
218 .scan_index = 2,
219 .scan_type = {
220 .sign = 'u',
221 .realbits = 32,
222 .storagebits = 32,
223 .endianness = IIO_CPU,
224 },
225 },
226 IIO_CHAN_SOFT_TIMESTAMP(3),
227 };
228
229 static const struct iio_chan_spec bmp380_channels[] = {
230 {
231 .type = IIO_PRESSURE,
232 /* PROCESSED maintained for ABI backwards compatibility */
233 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
234 BIT(IIO_CHAN_INFO_RAW) |
235 BIT(IIO_CHAN_INFO_SCALE) |
236 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
237 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
238 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
239 .scan_index = 0,
240 .scan_type = {
241 .sign = 'u',
242 .realbits = 32,
243 .storagebits = 32,
244 .endianness = IIO_CPU,
245 },
246 },
247 {
248 .type = IIO_TEMP,
249 /* PROCESSED maintained for ABI backwards compatibility */
250 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
251 BIT(IIO_CHAN_INFO_RAW) |
252 BIT(IIO_CHAN_INFO_SCALE) |
253 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
254 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
255 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
256 .scan_index = 1,
257 .scan_type = {
258 .sign = 's',
259 .realbits = 32,
260 .storagebits = 32,
261 .endianness = IIO_CPU,
262 },
263 },
264 IIO_CHAN_SOFT_TIMESTAMP(2),
265 };
266
267 static const struct iio_chan_spec bmp580_channels[] = {
268 {
269 .type = IIO_PRESSURE,
270 /* PROCESSED maintained for ABI backwards compatibility */
271 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
272 BIT(IIO_CHAN_INFO_RAW) |
273 BIT(IIO_CHAN_INFO_SCALE) |
274 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
275 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
276 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
277 .scan_index = 0,
278 .scan_type = {
279 .sign = 'u',
280 .realbits = 24,
281 .storagebits = 32,
282 .endianness = IIO_LE,
283 },
284 },
285 {
286 .type = IIO_TEMP,
287 /* PROCESSED maintained for ABI backwards compatibility */
288 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
289 BIT(IIO_CHAN_INFO_RAW) |
290 BIT(IIO_CHAN_INFO_SCALE) |
291 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
292 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
293 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
294 .scan_index = 1,
295 .scan_type = {
296 .sign = 's',
297 .realbits = 24,
298 .storagebits = 32,
299 .endianness = IIO_LE,
300 },
301 },
302 IIO_CHAN_SOFT_TIMESTAMP(2),
303 };
304
bmp280_read_calib(struct bmp280_data * data)305 static int bmp280_read_calib(struct bmp280_data *data)
306 {
307 struct bmp280_calib *calib = &data->calib.bmp280;
308 int ret;
309
310 /* Read temperature and pressure calibration values. */
311 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
312 data->bmp280_cal_buf,
313 sizeof(data->bmp280_cal_buf));
314 if (ret) {
315 dev_err(data->dev,
316 "failed to read calibration parameters\n");
317 return ret;
318 }
319
320 /* Toss calibration data into the entropy pool */
321 add_device_randomness(data->bmp280_cal_buf,
322 sizeof(data->bmp280_cal_buf));
323
324 /* Parse temperature calibration values. */
325 calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]);
326 calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]);
327 calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]);
328
329 /* Parse pressure calibration values. */
330 calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]);
331 calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]);
332 calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]);
333 calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]);
334 calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]);
335 calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]);
336 calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]);
337 calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]);
338 calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]);
339
340 return 0;
341 }
342
bme280_read_calib(struct bmp280_data * data)343 static int bme280_read_calib(struct bmp280_data *data)
344 {
345 struct bmp280_calib *calib = &data->calib.bmp280;
346 struct device *dev = data->dev;
347 unsigned int tmp;
348 int ret;
349
350 /* Load shared calibration params with bmp280 first */
351 ret = bmp280_read_calib(data);
352 if (ret)
353 return ret;
354
355 /*
356 * Read humidity calibration values.
357 * Due to some odd register addressing we cannot just
358 * do a big bulk read. Instead, we have to read each Hx
359 * value separately and sometimes do some bit shifting...
360 * Humidity data is only available on BME280.
361 */
362
363 ret = regmap_read(data->regmap, BME280_REG_COMP_H1, &tmp);
364 if (ret) {
365 dev_err(dev, "failed to read H1 comp value\n");
366 return ret;
367 }
368 calib->H1 = tmp;
369
370 ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H2,
371 &data->le16, sizeof(data->le16));
372 if (ret) {
373 dev_err(dev, "failed to read H2 comp value\n");
374 return ret;
375 }
376 calib->H2 = sign_extend32(le16_to_cpu(data->le16), 15);
377
378 ret = regmap_read(data->regmap, BME280_REG_COMP_H3, &tmp);
379 if (ret) {
380 dev_err(dev, "failed to read H3 comp value\n");
381 return ret;
382 }
383 calib->H3 = tmp;
384
385 ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H4,
386 &data->be16, sizeof(data->be16));
387 if (ret) {
388 dev_err(dev, "failed to read H4 comp value\n");
389 return ret;
390 }
391 calib->H4 = sign_extend32(((be16_to_cpu(data->be16) >> 4) & 0xff0) |
392 (be16_to_cpu(data->be16) & 0xf), 11);
393
394 ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H5,
395 &data->le16, sizeof(data->le16));
396 if (ret) {
397 dev_err(dev, "failed to read H5 comp value\n");
398 return ret;
399 }
400 calib->H5 = sign_extend32(FIELD_GET(BME280_COMP_H5_MASK, le16_to_cpu(data->le16)), 11);
401
402 ret = regmap_read(data->regmap, BME280_REG_COMP_H6, &tmp);
403 if (ret) {
404 dev_err(dev, "failed to read H6 comp value\n");
405 return ret;
406 }
407 calib->H6 = sign_extend32(tmp, 7);
408
409 return 0;
410 }
411
bme280_read_humid_adc(struct bmp280_data * data,u16 * adc_humidity)412 static int bme280_read_humid_adc(struct bmp280_data *data, u16 *adc_humidity)
413 {
414 u16 value_humidity;
415 int ret;
416
417 ret = regmap_bulk_read(data->regmap, BME280_REG_HUMIDITY_MSB,
418 &data->be16, BME280_NUM_HUMIDITY_BYTES);
419 if (ret) {
420 dev_err(data->dev, "failed to read humidity\n");
421 return ret;
422 }
423
424 value_humidity = be16_to_cpu(data->be16);
425 if (value_humidity == BMP280_HUMIDITY_SKIPPED) {
426 dev_err(data->dev, "reading humidity skipped\n");
427 return -EIO;
428 }
429 *adc_humidity = value_humidity;
430
431 return 0;
432 }
433
434 /*
435 * Returns humidity in percent, resolution is 0.01 percent. Output value of
436 * "47445" represents 47445/1024 = 46.333 %RH.
437 *
438 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
439 */
bme280_compensate_humidity(struct bmp280_data * data,u16 adc_humidity,s32 t_fine)440 static u32 bme280_compensate_humidity(struct bmp280_data *data,
441 u16 adc_humidity, s32 t_fine)
442 {
443 struct bmp280_calib *calib = &data->calib.bmp280;
444 s32 var;
445
446 var = t_fine - (s32)76800;
447 var = (((((s32)adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
448 + (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
449 * (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
450 + (s32)2097152) * calib->H2 + 8192) >> 14);
451 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
452
453 var = clamp_val(var, 0, 419430400);
454
455 return var >> 12;
456 }
457
bmp280_read_temp_adc(struct bmp280_data * data,u32 * adc_temp)458 static int bmp280_read_temp_adc(struct bmp280_data *data, u32 *adc_temp)
459 {
460 u32 value_temp;
461 int ret;
462
463 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
464 data->buf, BMP280_NUM_TEMP_BYTES);
465 if (ret) {
466 dev_err(data->dev, "failed to read temperature\n");
467 return ret;
468 }
469
470 value_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
471 if (value_temp == BMP280_TEMP_SKIPPED) {
472 dev_err(data->dev, "reading temperature skipped\n");
473 return -EIO;
474 }
475 *adc_temp = value_temp;
476
477 return 0;
478 }
479
480 /*
481 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
482 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
483 * value.
484 *
485 * Taken from datasheet, Section 3.11.3, "Compensation formula".
486 */
bmp280_calc_t_fine(struct bmp280_data * data,u32 adc_temp)487 static s32 bmp280_calc_t_fine(struct bmp280_data *data, u32 adc_temp)
488 {
489 struct bmp280_calib *calib = &data->calib.bmp280;
490 s32 var1, var2;
491
492 var1 = (((((s32)adc_temp) >> 3) - ((s32)calib->T1 << 1)) *
493 ((s32)calib->T2)) >> 11;
494 var2 = (((((((s32)adc_temp) >> 4) - ((s32)calib->T1)) *
495 ((((s32)adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
496 ((s32)calib->T3))) >> 14;
497 return var1 + var2; /* t_fine = var1 + var2 */
498 }
499
bmp280_get_t_fine(struct bmp280_data * data,s32 * t_fine)500 static int bmp280_get_t_fine(struct bmp280_data *data, s32 *t_fine)
501 {
502 u32 adc_temp;
503 int ret;
504
505 ret = bmp280_read_temp_adc(data, &adc_temp);
506 if (ret)
507 return ret;
508
509 *t_fine = bmp280_calc_t_fine(data, adc_temp);
510
511 return 0;
512 }
513
bmp280_compensate_temp(struct bmp280_data * data,u32 adc_temp)514 static s32 bmp280_compensate_temp(struct bmp280_data *data, u32 adc_temp)
515 {
516 return (bmp280_calc_t_fine(data, adc_temp) * 5 + 128) / 256;
517 }
518
bmp280_read_press_adc(struct bmp280_data * data,u32 * adc_press)519 static int bmp280_read_press_adc(struct bmp280_data *data, u32 *adc_press)
520 {
521 u32 value_press;
522 int ret;
523
524 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
525 data->buf, BMP280_NUM_PRESS_BYTES);
526 if (ret) {
527 dev_err(data->dev, "failed to read pressure\n");
528 return ret;
529 }
530
531 value_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
532 if (value_press == BMP280_PRESS_SKIPPED) {
533 dev_err(data->dev, "reading pressure skipped\n");
534 return -EIO;
535 }
536 *adc_press = value_press;
537
538 return 0;
539 }
540
541 /*
542 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
543 * integer bits and 8 fractional bits). Output value of "24674867"
544 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
545 *
546 * Taken from datasheet, Section 3.11.3, "Compensation formula".
547 */
bmp280_compensate_press(struct bmp280_data * data,u32 adc_press,s32 t_fine)548 static u32 bmp280_compensate_press(struct bmp280_data *data,
549 u32 adc_press, s32 t_fine)
550 {
551 struct bmp280_calib *calib = &data->calib.bmp280;
552 s64 var1, var2, p;
553
554 var1 = ((s64)t_fine) - 128000;
555 var2 = var1 * var1 * (s64)calib->P6;
556 var2 += (var1 * (s64)calib->P5) << 17;
557 var2 += ((s64)calib->P4) << 35;
558 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
559 ((var1 * (s64)calib->P2) << 12);
560 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
561
562 if (var1 == 0)
563 return 0;
564
565 p = ((((s64)1048576 - (s32)adc_press) << 31) - var2) * 3125;
566 p = div64_s64(p, var1);
567 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
568 var2 = ((s64)(calib->P8) * p) >> 19;
569 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
570
571 return (u32)p;
572 }
573
bmp280_read_temp(struct bmp280_data * data,s32 * comp_temp)574 static int bmp280_read_temp(struct bmp280_data *data, s32 *comp_temp)
575 {
576 u32 adc_temp;
577 int ret;
578
579 ret = bmp280_read_temp_adc(data, &adc_temp);
580 if (ret)
581 return ret;
582
583 *comp_temp = bmp280_compensate_temp(data, adc_temp);
584
585 return 0;
586 }
587
bmp280_read_press(struct bmp280_data * data,u32 * comp_press)588 static int bmp280_read_press(struct bmp280_data *data, u32 *comp_press)
589 {
590 u32 adc_press;
591 s32 t_fine;
592 int ret;
593
594 ret = bmp280_get_t_fine(data, &t_fine);
595 if (ret)
596 return ret;
597
598 ret = bmp280_read_press_adc(data, &adc_press);
599 if (ret)
600 return ret;
601
602 *comp_press = bmp280_compensate_press(data, adc_press, t_fine);
603
604 return 0;
605 }
606
bme280_read_humid(struct bmp280_data * data,u32 * comp_humidity)607 static int bme280_read_humid(struct bmp280_data *data, u32 *comp_humidity)
608 {
609 u16 adc_humidity;
610 s32 t_fine;
611 int ret;
612
613 ret = bmp280_get_t_fine(data, &t_fine);
614 if (ret)
615 return ret;
616
617 ret = bme280_read_humid_adc(data, &adc_humidity);
618 if (ret)
619 return ret;
620
621 *comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine);
622
623 return 0;
624 }
625
bmp280_read_raw_impl(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)626 static int bmp280_read_raw_impl(struct iio_dev *indio_dev,
627 struct iio_chan_spec const *chan,
628 int *val, int *val2, long mask)
629 {
630 struct bmp280_data *data = iio_priv(indio_dev);
631 int chan_value;
632 int ret;
633
634 guard(mutex)(&data->lock);
635
636 switch (mask) {
637 case IIO_CHAN_INFO_PROCESSED:
638 switch (chan->type) {
639 case IIO_HUMIDITYRELATIVE:
640 ret = data->chip_info->read_humid(data, &chan_value);
641 if (ret)
642 return ret;
643
644 *val = data->chip_info->humid_coeffs[0] * chan_value;
645 *val2 = data->chip_info->humid_coeffs[1];
646 return data->chip_info->humid_coeffs_type;
647 case IIO_PRESSURE:
648 ret = data->chip_info->read_press(data, &chan_value);
649 if (ret)
650 return ret;
651
652 *val = data->chip_info->press_coeffs[0] * chan_value;
653 *val2 = data->chip_info->press_coeffs[1];
654 return data->chip_info->press_coeffs_type;
655 case IIO_TEMP:
656 ret = data->chip_info->read_temp(data, &chan_value);
657 if (ret)
658 return ret;
659
660 *val = data->chip_info->temp_coeffs[0] * chan_value;
661 *val2 = data->chip_info->temp_coeffs[1];
662 return data->chip_info->temp_coeffs_type;
663 default:
664 return -EINVAL;
665 }
666 case IIO_CHAN_INFO_RAW:
667 switch (chan->type) {
668 case IIO_HUMIDITYRELATIVE:
669 ret = data->chip_info->read_humid(data, &chan_value);
670 if (ret)
671 return ret;
672
673 *val = chan_value;
674 return IIO_VAL_INT;
675 case IIO_PRESSURE:
676 ret = data->chip_info->read_press(data, &chan_value);
677 if (ret)
678 return ret;
679
680 *val = chan_value;
681 return IIO_VAL_INT;
682 case IIO_TEMP:
683 ret = data->chip_info->read_temp(data, &chan_value);
684 if (ret)
685 return ret;
686
687 *val = chan_value;
688 return IIO_VAL_INT;
689 default:
690 return -EINVAL;
691 }
692 case IIO_CHAN_INFO_SCALE:
693 switch (chan->type) {
694 case IIO_HUMIDITYRELATIVE:
695 *val = data->chip_info->humid_coeffs[0];
696 *val2 = data->chip_info->humid_coeffs[1];
697 return data->chip_info->humid_coeffs_type;
698 case IIO_PRESSURE:
699 *val = data->chip_info->press_coeffs[0];
700 *val2 = data->chip_info->press_coeffs[1];
701 return data->chip_info->press_coeffs_type;
702 case IIO_TEMP:
703 *val = data->chip_info->temp_coeffs[0];
704 *val2 = data->chip_info->temp_coeffs[1];
705 return data->chip_info->temp_coeffs_type;
706 default:
707 return -EINVAL;
708 }
709 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
710 switch (chan->type) {
711 case IIO_HUMIDITYRELATIVE:
712 *val = 1 << data->oversampling_humid;
713 return IIO_VAL_INT;
714 case IIO_PRESSURE:
715 *val = 1 << data->oversampling_press;
716 return IIO_VAL_INT;
717 case IIO_TEMP:
718 *val = 1 << data->oversampling_temp;
719 return IIO_VAL_INT;
720 default:
721 return -EINVAL;
722 }
723 case IIO_CHAN_INFO_SAMP_FREQ:
724 if (!data->chip_info->sampling_freq_avail)
725 return -EINVAL;
726
727 *val = data->chip_info->sampling_freq_avail[data->sampling_freq][0];
728 *val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1];
729 return IIO_VAL_INT_PLUS_MICRO;
730 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
731 if (!data->chip_info->iir_filter_coeffs_avail)
732 return -EINVAL;
733
734 *val = (1 << data->iir_filter_coeff) - 1;
735 return IIO_VAL_INT;
736 default:
737 return -EINVAL;
738 }
739 }
740
bmp280_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)741 static int bmp280_read_raw(struct iio_dev *indio_dev,
742 struct iio_chan_spec const *chan,
743 int *val, int *val2, long mask)
744 {
745 struct bmp280_data *data = iio_priv(indio_dev);
746 int ret;
747
748 pm_runtime_get_sync(data->dev);
749 ret = bmp280_read_raw_impl(indio_dev, chan, val, val2, mask);
750 pm_runtime_mark_last_busy(data->dev);
751 pm_runtime_put_autosuspend(data->dev);
752
753 return ret;
754 }
755
bme280_write_oversampling_ratio_humid(struct bmp280_data * data,int val)756 static int bme280_write_oversampling_ratio_humid(struct bmp280_data *data,
757 int val)
758 {
759 const int *avail = data->chip_info->oversampling_humid_avail;
760 const int n = data->chip_info->num_oversampling_humid_avail;
761 int ret, prev;
762 int i;
763
764 for (i = 0; i < n; i++) {
765 if (avail[i] == val) {
766 prev = data->oversampling_humid;
767 data->oversampling_humid = ilog2(val);
768
769 ret = data->chip_info->chip_config(data);
770 if (ret) {
771 data->oversampling_humid = prev;
772 data->chip_info->chip_config(data);
773 return ret;
774 }
775 return 0;
776 }
777 }
778 return -EINVAL;
779 }
780
bmp280_write_oversampling_ratio_temp(struct bmp280_data * data,int val)781 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
782 int val)
783 {
784 const int *avail = data->chip_info->oversampling_temp_avail;
785 const int n = data->chip_info->num_oversampling_temp_avail;
786 int ret, prev;
787 int i;
788
789 for (i = 0; i < n; i++) {
790 if (avail[i] == val) {
791 prev = data->oversampling_temp;
792 data->oversampling_temp = ilog2(val);
793
794 ret = data->chip_info->chip_config(data);
795 if (ret) {
796 data->oversampling_temp = prev;
797 data->chip_info->chip_config(data);
798 return ret;
799 }
800 return 0;
801 }
802 }
803 return -EINVAL;
804 }
805
bmp280_write_oversampling_ratio_press(struct bmp280_data * data,int val)806 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
807 int val)
808 {
809 const int *avail = data->chip_info->oversampling_press_avail;
810 const int n = data->chip_info->num_oversampling_press_avail;
811 int ret, prev;
812 int i;
813
814 for (i = 0; i < n; i++) {
815 if (avail[i] == val) {
816 prev = data->oversampling_press;
817 data->oversampling_press = ilog2(val);
818
819 ret = data->chip_info->chip_config(data);
820 if (ret) {
821 data->oversampling_press = prev;
822 data->chip_info->chip_config(data);
823 return ret;
824 }
825 return 0;
826 }
827 }
828 return -EINVAL;
829 }
830
bmp280_write_sampling_frequency(struct bmp280_data * data,int val,int val2)831 static int bmp280_write_sampling_frequency(struct bmp280_data *data,
832 int val, int val2)
833 {
834 const int (*avail)[2] = data->chip_info->sampling_freq_avail;
835 const int n = data->chip_info->num_sampling_freq_avail;
836 int ret, prev;
837 int i;
838
839 for (i = 0; i < n; i++) {
840 if (avail[i][0] == val && avail[i][1] == val2) {
841 prev = data->sampling_freq;
842 data->sampling_freq = i;
843
844 ret = data->chip_info->chip_config(data);
845 if (ret) {
846 data->sampling_freq = prev;
847 data->chip_info->chip_config(data);
848 return ret;
849 }
850 return 0;
851 }
852 }
853 return -EINVAL;
854 }
855
bmp280_write_iir_filter_coeffs(struct bmp280_data * data,int val)856 static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val)
857 {
858 const int *avail = data->chip_info->iir_filter_coeffs_avail;
859 const int n = data->chip_info->num_iir_filter_coeffs_avail;
860 int ret, prev;
861 int i;
862
863 for (i = 0; i < n; i++) {
864 if (avail[i] - 1 == val) {
865 prev = data->iir_filter_coeff;
866 data->iir_filter_coeff = i;
867
868 ret = data->chip_info->chip_config(data);
869 if (ret) {
870 data->iir_filter_coeff = prev;
871 data->chip_info->chip_config(data);
872 return ret;
873
874 }
875 return 0;
876 }
877 }
878 return -EINVAL;
879 }
880
bmp280_write_raw_impl(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)881 static int bmp280_write_raw_impl(struct iio_dev *indio_dev,
882 struct iio_chan_spec const *chan,
883 int val, int val2, long mask)
884 {
885 struct bmp280_data *data = iio_priv(indio_dev);
886
887 guard(mutex)(&data->lock);
888
889 /*
890 * Helper functions to update sensor running configuration.
891 * If an error happens applying new settings, will try restore
892 * previous parameters to ensure the sensor is left in a known
893 * working configuration.
894 */
895 switch (mask) {
896 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
897 switch (chan->type) {
898 case IIO_HUMIDITYRELATIVE:
899 return bme280_write_oversampling_ratio_humid(data, val);
900 case IIO_PRESSURE:
901 return bmp280_write_oversampling_ratio_press(data, val);
902 case IIO_TEMP:
903 return bmp280_write_oversampling_ratio_temp(data, val);
904 default:
905 return -EINVAL;
906 }
907 case IIO_CHAN_INFO_SAMP_FREQ:
908 return bmp280_write_sampling_frequency(data, val, val2);
909 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
910 return bmp280_write_iir_filter_coeffs(data, val);
911 default:
912 return -EINVAL;
913 }
914 }
915
bmp280_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)916 static int bmp280_write_raw(struct iio_dev *indio_dev,
917 struct iio_chan_spec const *chan,
918 int val, int val2, long mask)
919 {
920 struct bmp280_data *data = iio_priv(indio_dev);
921 int ret;
922
923 pm_runtime_get_sync(data->dev);
924 ret = bmp280_write_raw_impl(indio_dev, chan, val, val2, mask);
925 pm_runtime_mark_last_busy(data->dev);
926 pm_runtime_put_autosuspend(data->dev);
927
928 return ret;
929 }
930
bmp280_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)931 static int bmp280_read_avail(struct iio_dev *indio_dev,
932 struct iio_chan_spec const *chan,
933 const int **vals, int *type, int *length,
934 long mask)
935 {
936 struct bmp280_data *data = iio_priv(indio_dev);
937
938 switch (mask) {
939 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
940 switch (chan->type) {
941 case IIO_PRESSURE:
942 *vals = data->chip_info->oversampling_press_avail;
943 *length = data->chip_info->num_oversampling_press_avail;
944 break;
945 case IIO_TEMP:
946 *vals = data->chip_info->oversampling_temp_avail;
947 *length = data->chip_info->num_oversampling_temp_avail;
948 break;
949 default:
950 return -EINVAL;
951 }
952 *type = IIO_VAL_INT;
953 return IIO_AVAIL_LIST;
954 case IIO_CHAN_INFO_SAMP_FREQ:
955 *vals = (const int *)data->chip_info->sampling_freq_avail;
956 *type = IIO_VAL_INT_PLUS_MICRO;
957 /* Values are stored in a 2D matrix */
958 *length = data->chip_info->num_sampling_freq_avail;
959 return IIO_AVAIL_LIST;
960 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
961 *vals = data->chip_info->iir_filter_coeffs_avail;
962 *type = IIO_VAL_INT;
963 *length = data->chip_info->num_iir_filter_coeffs_avail;
964 return IIO_AVAIL_LIST;
965 default:
966 return -EINVAL;
967 }
968 }
969
970 static const struct iio_info bmp280_info = {
971 .read_raw = &bmp280_read_raw,
972 .read_avail = &bmp280_read_avail,
973 .write_raw = &bmp280_write_raw,
974 };
975
976 static const unsigned long bmp280_avail_scan_masks[] = {
977 BIT(BMP280_TEMP) | BIT(BMP280_PRESS),
978 0
979 };
980
981 static const unsigned long bme280_avail_scan_masks[] = {
982 BIT(BME280_HUMID) | BIT(BMP280_TEMP) | BIT(BMP280_PRESS),
983 0
984 };
985
bmp280_chip_config(struct bmp280_data * data)986 static int bmp280_chip_config(struct bmp280_data *data)
987 {
988 u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) |
989 FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1);
990 int ret;
991
992 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
993 BMP280_OSRS_TEMP_MASK |
994 BMP280_OSRS_PRESS_MASK |
995 BMP280_MODE_MASK,
996 osrs | BMP280_MODE_NORMAL);
997 if (ret) {
998 dev_err(data->dev, "failed to write ctrl_meas register\n");
999 return ret;
1000 }
1001
1002 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
1003 BMP280_FILTER_MASK,
1004 BMP280_FILTER_4X);
1005 if (ret) {
1006 dev_err(data->dev, "failed to write config register\n");
1007 return ret;
1008 }
1009
1010 return ret;
1011 }
1012
bmp280_trigger_handler(int irq,void * p)1013 static irqreturn_t bmp280_trigger_handler(int irq, void *p)
1014 {
1015 struct iio_poll_func *pf = p;
1016 struct iio_dev *indio_dev = pf->indio_dev;
1017 struct bmp280_data *data = iio_priv(indio_dev);
1018 s32 adc_temp, adc_press, t_fine;
1019 int ret;
1020
1021 guard(mutex)(&data->lock);
1022
1023 /* Burst read data registers */
1024 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
1025 data->buf, BMP280_BURST_READ_BYTES);
1026 if (ret) {
1027 dev_err(data->dev, "failed to burst read sensor data\n");
1028 goto out;
1029 }
1030
1031 /* Temperature calculations */
1032 adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3]));
1033 if (adc_temp == BMP280_TEMP_SKIPPED) {
1034 dev_err(data->dev, "reading temperature skipped\n");
1035 goto out;
1036 }
1037
1038 data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp);
1039
1040 /* Pressure calculations */
1041 adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
1042 if (adc_press == BMP280_PRESS_SKIPPED) {
1043 dev_err(data->dev, "reading pressure skipped\n");
1044 goto out;
1045 }
1046
1047 t_fine = bmp280_calc_t_fine(data, adc_temp);
1048
1049 data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine);
1050
1051 iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
1052 iio_get_time_ns(indio_dev));
1053
1054 out:
1055 iio_trigger_notify_done(indio_dev->trig);
1056
1057 return IRQ_HANDLED;
1058 }
1059
1060 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
1061 static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID };
1062 static const int bmp280_temp_coeffs[] = { 10, 1 };
1063 static const int bmp280_press_coeffs[] = { 1, 256000 };
1064
1065 const struct bmp280_chip_info bmp280_chip_info = {
1066 .id_reg = BMP280_REG_ID,
1067 .chip_id = bmp280_chip_ids,
1068 .num_chip_id = ARRAY_SIZE(bmp280_chip_ids),
1069 .regmap_config = &bmp280_regmap_config,
1070 .start_up_time = 2000,
1071 .channels = bmp280_channels,
1072 .num_channels = ARRAY_SIZE(bmp280_channels),
1073 .avail_scan_masks = bmp280_avail_scan_masks,
1074
1075 .oversampling_temp_avail = bmp280_oversampling_avail,
1076 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1077 /*
1078 * Oversampling config values on BMx280 have one additional setting
1079 * that other generations of the family don't:
1080 * The value 0 means the measurement is bypassed instead of
1081 * oversampling set to x1.
1082 *
1083 * To account for this difference, and preserve the same common
1084 * config logic, this is handled later on chip_config callback
1085 * incrementing one unit the oversampling setting.
1086 */
1087 .oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
1088
1089 .oversampling_press_avail = bmp280_oversampling_avail,
1090 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1091 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
1092
1093 .temp_coeffs = bmp280_temp_coeffs,
1094 .temp_coeffs_type = IIO_VAL_FRACTIONAL,
1095 .press_coeffs = bmp280_press_coeffs,
1096 .press_coeffs_type = IIO_VAL_FRACTIONAL,
1097
1098 .chip_config = bmp280_chip_config,
1099 .read_temp = bmp280_read_temp,
1100 .read_press = bmp280_read_press,
1101 .read_calib = bmp280_read_calib,
1102
1103 .trigger_handler = bmp280_trigger_handler,
1104 };
1105 EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280);
1106
bme280_chip_config(struct bmp280_data * data)1107 static int bme280_chip_config(struct bmp280_data *data)
1108 {
1109 u8 osrs = FIELD_PREP(BME280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1);
1110 int ret;
1111
1112 /*
1113 * Oversampling of humidity must be set before oversampling of
1114 * temperature/pressure is set to become effective.
1115 */
1116 ret = regmap_update_bits(data->regmap, BME280_REG_CTRL_HUMIDITY,
1117 BME280_OSRS_HUMIDITY_MASK, osrs);
1118 if (ret) {
1119 dev_err(data->dev, "failed to set humidity oversampling");
1120 return ret;
1121 }
1122
1123 return bmp280_chip_config(data);
1124 }
1125
bme280_trigger_handler(int irq,void * p)1126 static irqreturn_t bme280_trigger_handler(int irq, void *p)
1127 {
1128 struct iio_poll_func *pf = p;
1129 struct iio_dev *indio_dev = pf->indio_dev;
1130 struct bmp280_data *data = iio_priv(indio_dev);
1131 s32 adc_temp, adc_press, adc_humidity, t_fine;
1132 int ret;
1133
1134 guard(mutex)(&data->lock);
1135
1136 /* Burst read data registers */
1137 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
1138 data->buf, BME280_BURST_READ_BYTES);
1139 if (ret) {
1140 dev_err(data->dev, "failed to burst read sensor data\n");
1141 goto out;
1142 }
1143
1144 /* Temperature calculations */
1145 adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3]));
1146 if (adc_temp == BMP280_TEMP_SKIPPED) {
1147 dev_err(data->dev, "reading temperature skipped\n");
1148 goto out;
1149 }
1150
1151 data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp);
1152
1153 /* Pressure calculations */
1154 adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
1155 if (adc_press == BMP280_PRESS_SKIPPED) {
1156 dev_err(data->dev, "reading pressure skipped\n");
1157 goto out;
1158 }
1159
1160 t_fine = bmp280_calc_t_fine(data, adc_temp);
1161
1162 data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine);
1163
1164 /* Humidity calculations */
1165 adc_humidity = get_unaligned_be16(&data->buf[6]);
1166
1167 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
1168 dev_err(data->dev, "reading humidity skipped\n");
1169 goto out;
1170 }
1171 data->sensor_data[2] = bme280_compensate_humidity(data, adc_humidity, t_fine);
1172
1173 iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
1174 iio_get_time_ns(indio_dev));
1175
1176 out:
1177 iio_trigger_notify_done(indio_dev->trig);
1178
1179 return IRQ_HANDLED;
1180 }
1181
1182 static const u8 bme280_chip_ids[] = { BME280_CHIP_ID };
1183 static const int bme280_humid_coeffs[] = { 1000, 1024 };
1184
1185 const struct bmp280_chip_info bme280_chip_info = {
1186 .id_reg = BMP280_REG_ID,
1187 .chip_id = bme280_chip_ids,
1188 .num_chip_id = ARRAY_SIZE(bme280_chip_ids),
1189 .regmap_config = &bme280_regmap_config,
1190 .start_up_time = 2000,
1191 .channels = bme280_channels,
1192 .num_channels = ARRAY_SIZE(bme280_channels),
1193 .avail_scan_masks = bme280_avail_scan_masks,
1194
1195 .oversampling_temp_avail = bmp280_oversampling_avail,
1196 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1197 .oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
1198
1199 .oversampling_press_avail = bmp280_oversampling_avail,
1200 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1201 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
1202
1203 .oversampling_humid_avail = bmp280_oversampling_avail,
1204 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1205 .oversampling_humid_default = BME280_OSRS_HUMIDITY_16X - 1,
1206
1207 .temp_coeffs = bmp280_temp_coeffs,
1208 .temp_coeffs_type = IIO_VAL_FRACTIONAL,
1209 .press_coeffs = bmp280_press_coeffs,
1210 .press_coeffs_type = IIO_VAL_FRACTIONAL,
1211 .humid_coeffs = bme280_humid_coeffs,
1212 .humid_coeffs_type = IIO_VAL_FRACTIONAL,
1213
1214 .chip_config = bme280_chip_config,
1215 .read_temp = bmp280_read_temp,
1216 .read_press = bmp280_read_press,
1217 .read_humid = bme280_read_humid,
1218 .read_calib = bme280_read_calib,
1219
1220 .trigger_handler = bme280_trigger_handler,
1221 };
1222 EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280);
1223
1224 /*
1225 * Helper function to send a command to BMP3XX sensors.
1226 *
1227 * Sensor processes commands written to the CMD register and signals
1228 * execution result through "cmd_rdy" and "cmd_error" flags available on
1229 * STATUS and ERROR registers.
1230 */
bmp380_cmd(struct bmp280_data * data,u8 cmd)1231 static int bmp380_cmd(struct bmp280_data *data, u8 cmd)
1232 {
1233 unsigned int reg;
1234 int ret;
1235
1236 /* Check if device is ready to process a command */
1237 ret = regmap_read(data->regmap, BMP380_REG_STATUS, ®);
1238 if (ret) {
1239 dev_err(data->dev, "failed to read error register\n");
1240 return ret;
1241 }
1242 if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) {
1243 dev_err(data->dev, "device is not ready to accept commands\n");
1244 return -EBUSY;
1245 }
1246
1247 /* Send command to process */
1248 ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd);
1249 if (ret) {
1250 dev_err(data->dev, "failed to send command to device\n");
1251 return ret;
1252 }
1253 /* Wait for 2ms for command to be processed */
1254 usleep_range(data->start_up_time, data->start_up_time + 100);
1255 /* Check for command processing error */
1256 ret = regmap_read(data->regmap, BMP380_REG_ERROR, ®);
1257 if (ret) {
1258 dev_err(data->dev, "error reading ERROR reg\n");
1259 return ret;
1260 }
1261 if (reg & BMP380_ERR_CMD_MASK) {
1262 dev_err(data->dev, "error processing command 0x%X\n", cmd);
1263 return -EINVAL;
1264 }
1265
1266 return 0;
1267 }
1268
bmp380_read_temp_adc(struct bmp280_data * data,u32 * adc_temp)1269 static int bmp380_read_temp_adc(struct bmp280_data *data, u32 *adc_temp)
1270 {
1271 u32 value_temp;
1272 int ret;
1273
1274 ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB,
1275 data->buf, BMP280_NUM_TEMP_BYTES);
1276 if (ret) {
1277 dev_err(data->dev, "failed to read temperature\n");
1278 return ret;
1279 }
1280
1281 value_temp = get_unaligned_le24(data->buf);
1282 if (value_temp == BMP380_TEMP_SKIPPED) {
1283 dev_err(data->dev, "reading temperature skipped\n");
1284 return -EIO;
1285 }
1286 *adc_temp = value_temp;
1287
1288 return 0;
1289 }
1290
1291 /*
1292 * Returns temperature in Celsius degrees, resolution is 0.01º C. Output value
1293 * of "5123" equals 51.2º C. t_fine carries fine temperature as global value.
1294 *
1295 * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo
1296 * https://github.com/BoschSensortec/BMP3-Sensor-API.
1297 */
bmp380_calc_t_fine(struct bmp280_data * data,u32 adc_temp)1298 static s32 bmp380_calc_t_fine(struct bmp280_data *data, u32 adc_temp)
1299 {
1300 s64 var1, var2, var3, var4, var5, var6;
1301 struct bmp380_calib *calib = &data->calib.bmp380;
1302
1303 var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8);
1304 var2 = var1 * ((s64) calib->T2);
1305 var3 = var1 * var1;
1306 var4 = var3 * ((s64) calib->T3);
1307 var5 = (var2 << 18) + var4;
1308 var6 = var5 >> 32;
1309 return (s32)var6; /* t_fine = var6 */
1310 }
1311
bmp380_get_t_fine(struct bmp280_data * data,s32 * t_fine)1312 static int bmp380_get_t_fine(struct bmp280_data *data, s32 *t_fine)
1313 {
1314 s32 adc_temp;
1315 int ret;
1316
1317 ret = bmp380_read_temp_adc(data, &adc_temp);
1318 if (ret)
1319 return ret;
1320
1321 *t_fine = bmp380_calc_t_fine(data, adc_temp);
1322
1323 return 0;
1324 }
1325
bmp380_compensate_temp(struct bmp280_data * data,u32 adc_temp)1326 static int bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp)
1327 {
1328 s64 comp_temp;
1329 s32 var6;
1330
1331 var6 = bmp380_calc_t_fine(data, adc_temp);
1332 comp_temp = (var6 * 25) >> 14;
1333
1334 comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP);
1335 return (s32) comp_temp;
1336 }
1337
bmp380_read_press_adc(struct bmp280_data * data,u32 * adc_press)1338 static int bmp380_read_press_adc(struct bmp280_data *data, u32 *adc_press)
1339 {
1340 u32 value_press;
1341 int ret;
1342
1343 ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1344 data->buf, BMP280_NUM_PRESS_BYTES);
1345 if (ret) {
1346 dev_err(data->dev, "failed to read pressure\n");
1347 return ret;
1348 }
1349
1350 value_press = get_unaligned_le24(data->buf);
1351 if (value_press == BMP380_PRESS_SKIPPED) {
1352 dev_err(data->dev, "reading pressure skipped\n");
1353 return -EIO;
1354 }
1355 *adc_press = value_press;
1356
1357 return 0;
1358 }
1359
1360 /*
1361 * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal.
1362 * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa.
1363 *
1364 * Taken from datasheet, Section 9.3. "Pressure compensation" and repository
1365 * https://github.com/BoschSensortec/BMP3-Sensor-API.
1366 */
bmp380_compensate_press(struct bmp280_data * data,u32 adc_press,s32 t_fine)1367 static u32 bmp380_compensate_press(struct bmp280_data *data,
1368 u32 adc_press, s32 t_fine)
1369 {
1370 s64 var1, var2, var3, var4, var5, var6, offset, sensitivity;
1371 struct bmp380_calib *calib = &data->calib.bmp380;
1372 u32 comp_press;
1373
1374 var1 = (s64)t_fine * (s64)t_fine;
1375 var2 = var1 >> 6;
1376 var3 = (var2 * ((s64)t_fine)) >> 8;
1377 var4 = ((s64)calib->P8 * var3) >> 5;
1378 var5 = ((s64)calib->P7 * var1) << 4;
1379 var6 = ((s64)calib->P6 * (s64)t_fine) << 22;
1380 offset = ((s64)calib->P5 << 47) + var4 + var5 + var6;
1381 var2 = ((s64)calib->P4 * var3) >> 5;
1382 var4 = ((s64)calib->P3 * var1) << 2;
1383 var5 = ((s64)calib->P2 - ((s64)1 << 14)) *
1384 ((s64)t_fine << 21);
1385 sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) +
1386 var2 + var4 + var5;
1387 var1 = (sensitivity >> 24) * (s64)adc_press;
1388 var2 = (s64)calib->P10 * (s64)t_fine;
1389 var3 = var2 + ((s64)calib->P9 << 16);
1390 var4 = (var3 * (s64)adc_press) >> 13;
1391
1392 /*
1393 * Dividing by 10 followed by multiplying by 10 to avoid
1394 * possible overflow caused by (uncomp_data->pressure * partial_data4).
1395 */
1396 var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9;
1397 var5 *= 10;
1398 var6 = (s64)adc_press * (s64)adc_press;
1399 var2 = ((s64)calib->P11 * var6) >> 16;
1400 var3 = (var2 * (s64)adc_press) >> 7;
1401 var4 = (offset >> 2) + var1 + var5 + var3;
1402 comp_press = ((u64)var4 * 25) >> 40;
1403
1404 comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES);
1405 return comp_press;
1406 }
1407
bmp380_read_temp(struct bmp280_data * data,s32 * comp_temp)1408 static int bmp380_read_temp(struct bmp280_data *data, s32 *comp_temp)
1409 {
1410 u32 adc_temp;
1411 int ret;
1412
1413 ret = bmp380_read_temp_adc(data, &adc_temp);
1414 if (ret)
1415 return ret;
1416
1417 *comp_temp = bmp380_compensate_temp(data, adc_temp);
1418
1419 return 0;
1420 }
1421
bmp380_read_press(struct bmp280_data * data,u32 * comp_press)1422 static int bmp380_read_press(struct bmp280_data *data, u32 *comp_press)
1423 {
1424 u32 adc_press, t_fine;
1425 int ret;
1426
1427 ret = bmp380_get_t_fine(data, &t_fine);
1428 if (ret)
1429 return ret;
1430
1431 ret = bmp380_read_press_adc(data, &adc_press);
1432 if (ret)
1433 return ret;
1434
1435 *comp_press = bmp380_compensate_press(data, adc_press, t_fine);
1436
1437 return 0;
1438 }
1439
bmp380_read_calib(struct bmp280_data * data)1440 static int bmp380_read_calib(struct bmp280_data *data)
1441 {
1442 struct bmp380_calib *calib = &data->calib.bmp380;
1443 int ret;
1444
1445 /* Read temperature and pressure calibration data */
1446 ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START,
1447 data->bmp380_cal_buf,
1448 sizeof(data->bmp380_cal_buf));
1449 if (ret) {
1450 dev_err(data->dev,
1451 "failed to read calibration parameters\n");
1452 return ret;
1453 }
1454
1455 /* Toss the temperature calibration data into the entropy pool */
1456 add_device_randomness(data->bmp380_cal_buf,
1457 sizeof(data->bmp380_cal_buf));
1458
1459 /* Parse calibration values */
1460 calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]);
1461 calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]);
1462 calib->T3 = data->bmp380_cal_buf[BMP380_T3];
1463 calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]);
1464 calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]);
1465 calib->P3 = data->bmp380_cal_buf[BMP380_P3];
1466 calib->P4 = data->bmp380_cal_buf[BMP380_P4];
1467 calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]);
1468 calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]);
1469 calib->P7 = data->bmp380_cal_buf[BMP380_P7];
1470 calib->P8 = data->bmp380_cal_buf[BMP380_P8];
1471 calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]);
1472 calib->P10 = data->bmp380_cal_buf[BMP380_P10];
1473 calib->P11 = data->bmp380_cal_buf[BMP380_P11];
1474
1475 return 0;
1476 }
1477
1478 static const int bmp380_odr_table[][2] = {
1479 [BMP380_ODR_200HZ] = {200, 0},
1480 [BMP380_ODR_100HZ] = {100, 0},
1481 [BMP380_ODR_50HZ] = {50, 0},
1482 [BMP380_ODR_25HZ] = {25, 0},
1483 [BMP380_ODR_12_5HZ] = {12, 500000},
1484 [BMP380_ODR_6_25HZ] = {6, 250000},
1485 [BMP380_ODR_3_125HZ] = {3, 125000},
1486 [BMP380_ODR_1_5625HZ] = {1, 562500},
1487 [BMP380_ODR_0_78HZ] = {0, 781250},
1488 [BMP380_ODR_0_39HZ] = {0, 390625},
1489 [BMP380_ODR_0_2HZ] = {0, 195313},
1490 [BMP380_ODR_0_1HZ] = {0, 97656},
1491 [BMP380_ODR_0_05HZ] = {0, 48828},
1492 [BMP380_ODR_0_02HZ] = {0, 24414},
1493 [BMP380_ODR_0_01HZ] = {0, 12207},
1494 [BMP380_ODR_0_006HZ] = {0, 6104},
1495 [BMP380_ODR_0_003HZ] = {0, 3052},
1496 [BMP380_ODR_0_0015HZ] = {0, 1526},
1497 };
1498
bmp380_preinit(struct bmp280_data * data)1499 static int bmp380_preinit(struct bmp280_data *data)
1500 {
1501 /* BMP3xx requires soft-reset as part of initialization */
1502 return bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
1503 }
1504
bmp380_chip_config(struct bmp280_data * data)1505 static int bmp380_chip_config(struct bmp280_data *data)
1506 {
1507 bool change = false, aux;
1508 unsigned int tmp;
1509 u8 osrs;
1510 int ret;
1511
1512 /* Configure power control register */
1513 ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1514 BMP380_CTRL_SENSORS_MASK,
1515 BMP380_CTRL_SENSORS_PRESS_EN |
1516 BMP380_CTRL_SENSORS_TEMP_EN);
1517 if (ret) {
1518 dev_err(data->dev,
1519 "failed to write operation control register\n");
1520 return ret;
1521 }
1522
1523 /* Configure oversampling */
1524 osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) |
1525 FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press);
1526
1527 ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR,
1528 BMP380_OSRS_TEMP_MASK |
1529 BMP380_OSRS_PRESS_MASK,
1530 osrs, &aux);
1531 if (ret) {
1532 dev_err(data->dev, "failed to write oversampling register\n");
1533 return ret;
1534 }
1535 change = change || aux;
1536
1537 /* Configure output data rate */
1538 ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR,
1539 BMP380_ODRS_MASK, data->sampling_freq,
1540 &aux);
1541 if (ret) {
1542 dev_err(data->dev, "failed to write ODR selection register\n");
1543 return ret;
1544 }
1545 change = change || aux;
1546
1547 /* Set filter data */
1548 ret = regmap_update_bits_check(data->regmap, BMP380_REG_CONFIG, BMP380_FILTER_MASK,
1549 FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff),
1550 &aux);
1551 if (ret) {
1552 dev_err(data->dev, "failed to write config register\n");
1553 return ret;
1554 }
1555 change = change || aux;
1556
1557 if (change) {
1558 /*
1559 * The configurations errors are detected on the fly during a
1560 * measurement cycle. If the sampling frequency is too low, it's
1561 * faster to reset the measurement loop than wait until the next
1562 * measurement is due.
1563 *
1564 * Resets sensor measurement loop toggling between sleep and
1565 * normal operating modes.
1566 */
1567 ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1568 BMP380_MODE_MASK,
1569 FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_SLEEP));
1570 if (ret) {
1571 dev_err(data->dev, "failed to set sleep mode\n");
1572 return ret;
1573 }
1574 usleep_range(2000, 2500);
1575 ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1576 BMP380_MODE_MASK,
1577 FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL));
1578 if (ret) {
1579 dev_err(data->dev, "failed to set normal mode\n");
1580 return ret;
1581 }
1582 /*
1583 * Waits for measurement before checking configuration error
1584 * flag. Selected longest measurement time, calculated from
1585 * formula in datasheet section 3.9.2 with an offset of ~+15%
1586 * as it seen as well in table 3.9.1.
1587 */
1588 msleep(150);
1589
1590 /* Check config error flag */
1591 ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp);
1592 if (ret) {
1593 dev_err(data->dev, "failed to read error register\n");
1594 return ret;
1595 }
1596 if (tmp & BMP380_ERR_CONF_MASK) {
1597 dev_warn(data->dev,
1598 "sensor flagged configuration as incompatible\n");
1599 return -EINVAL;
1600 }
1601 }
1602
1603 return 0;
1604 }
1605
bmp380_trigger_handler(int irq,void * p)1606 static irqreturn_t bmp380_trigger_handler(int irq, void *p)
1607 {
1608 struct iio_poll_func *pf = p;
1609 struct iio_dev *indio_dev = pf->indio_dev;
1610 struct bmp280_data *data = iio_priv(indio_dev);
1611 s32 adc_temp, adc_press, t_fine;
1612 int ret;
1613
1614 guard(mutex)(&data->lock);
1615
1616 /* Burst read data registers */
1617 ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1618 data->buf, BMP280_BURST_READ_BYTES);
1619 if (ret) {
1620 dev_err(data->dev, "failed to burst read sensor data\n");
1621 goto out;
1622 }
1623
1624 /* Temperature calculations */
1625 adc_temp = get_unaligned_le24(&data->buf[3]);
1626 if (adc_temp == BMP380_TEMP_SKIPPED) {
1627 dev_err(data->dev, "reading temperature skipped\n");
1628 goto out;
1629 }
1630
1631 data->sensor_data[1] = bmp380_compensate_temp(data, adc_temp);
1632
1633 /* Pressure calculations */
1634 adc_press = get_unaligned_le24(&data->buf[0]);
1635 if (adc_press == BMP380_PRESS_SKIPPED) {
1636 dev_err(data->dev, "reading pressure skipped\n");
1637 goto out;
1638 }
1639
1640 t_fine = bmp380_calc_t_fine(data, adc_temp);
1641
1642 data->sensor_data[0] = bmp380_compensate_press(data, adc_press, t_fine);
1643
1644 iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
1645 iio_get_time_ns(indio_dev));
1646
1647 out:
1648 iio_trigger_notify_done(indio_dev->trig);
1649
1650 return IRQ_HANDLED;
1651 }
1652
1653 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 };
1654 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128};
1655 static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID };
1656 static const int bmp380_temp_coeffs[] = { 10, 1 };
1657 static const int bmp380_press_coeffs[] = { 1, 100000 };
1658
1659 const struct bmp280_chip_info bmp380_chip_info = {
1660 .id_reg = BMP380_REG_ID,
1661 .chip_id = bmp380_chip_ids,
1662 .num_chip_id = ARRAY_SIZE(bmp380_chip_ids),
1663 .regmap_config = &bmp380_regmap_config,
1664 .spi_read_extra_byte = true,
1665 .start_up_time = 2000,
1666 .channels = bmp380_channels,
1667 .num_channels = ARRAY_SIZE(bmp380_channels),
1668 .avail_scan_masks = bmp280_avail_scan_masks,
1669
1670 .oversampling_temp_avail = bmp380_oversampling_avail,
1671 .num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1672 .oversampling_temp_default = ilog2(1),
1673
1674 .oversampling_press_avail = bmp380_oversampling_avail,
1675 .num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1676 .oversampling_press_default = ilog2(4),
1677
1678 .sampling_freq_avail = bmp380_odr_table,
1679 .num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2,
1680 .sampling_freq_default = BMP380_ODR_50HZ,
1681
1682 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1683 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1684 .iir_filter_coeff_default = 2,
1685
1686 .temp_coeffs = bmp380_temp_coeffs,
1687 .temp_coeffs_type = IIO_VAL_FRACTIONAL,
1688 .press_coeffs = bmp380_press_coeffs,
1689 .press_coeffs_type = IIO_VAL_FRACTIONAL,
1690
1691 .chip_config = bmp380_chip_config,
1692 .read_temp = bmp380_read_temp,
1693 .read_press = bmp380_read_press,
1694 .read_calib = bmp380_read_calib,
1695 .preinit = bmp380_preinit,
1696
1697 .trigger_handler = bmp380_trigger_handler,
1698 };
1699 EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280);
1700
bmp580_soft_reset(struct bmp280_data * data)1701 static int bmp580_soft_reset(struct bmp280_data *data)
1702 {
1703 unsigned int reg;
1704 int ret;
1705
1706 ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET);
1707 if (ret) {
1708 dev_err(data->dev, "failed to send reset command to device\n");
1709 return ret;
1710 }
1711 usleep_range(2000, 2500);
1712
1713 /* Dummy read of chip_id */
1714 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®);
1715 if (ret) {
1716 dev_err(data->dev, "failed to reestablish comms after reset\n");
1717 return ret;
1718 }
1719
1720 ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, ®);
1721 if (ret) {
1722 dev_err(data->dev, "error reading interrupt status register\n");
1723 return ret;
1724 }
1725 if (!(reg & BMP580_INT_STATUS_POR_MASK)) {
1726 dev_err(data->dev, "error resetting sensor\n");
1727 return -EINVAL;
1728 }
1729
1730 return 0;
1731 }
1732
1733 /**
1734 * bmp580_nvm_operation() - Helper function to commit NVM memory operations
1735 * @data: sensor data struct
1736 * @is_write: flag to signal write operation
1737 */
bmp580_nvm_operation(struct bmp280_data * data,bool is_write)1738 static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write)
1739 {
1740 unsigned long timeout, poll;
1741 unsigned int reg;
1742 int ret;
1743
1744 /* Check NVM ready flag */
1745 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®);
1746 if (ret) {
1747 dev_err(data->dev, "failed to check nvm status\n");
1748 return ret;
1749 }
1750 if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) {
1751 dev_err(data->dev, "sensor's nvm is not ready\n");
1752 return -EIO;
1753 }
1754
1755 /* Start NVM operation sequence */
1756 ret = regmap_write(data->regmap, BMP580_REG_CMD,
1757 BMP580_CMD_NVM_OP_SEQ_0);
1758 if (ret) {
1759 dev_err(data->dev,
1760 "failed to send nvm operation's first sequence\n");
1761 return ret;
1762 }
1763 if (is_write) {
1764 /* Send NVM write sequence */
1765 ret = regmap_write(data->regmap, BMP580_REG_CMD,
1766 BMP580_CMD_NVM_WRITE_SEQ_1);
1767 if (ret) {
1768 dev_err(data->dev,
1769 "failed to send nvm write sequence\n");
1770 return ret;
1771 }
1772 /* Datasheet says on 4.8.1.2 it takes approximately 10ms */
1773 poll = 2000;
1774 timeout = 12000;
1775 } else {
1776 /* Send NVM read sequence */
1777 ret = regmap_write(data->regmap, BMP580_REG_CMD,
1778 BMP580_CMD_NVM_READ_SEQ_1);
1779 if (ret) {
1780 dev_err(data->dev,
1781 "failed to send nvm read sequence\n");
1782 return ret;
1783 }
1784 /* Datasheet says on 4.8.1.1 it takes approximately 200us */
1785 poll = 50;
1786 timeout = 400;
1787 }
1788
1789 /* Wait until NVM is ready again */
1790 ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg,
1791 (reg & BMP580_STATUS_NVM_RDY_MASK),
1792 poll, timeout);
1793 if (ret) {
1794 dev_err(data->dev, "error checking nvm operation status\n");
1795 return ret;
1796 }
1797
1798 /* Check NVM error flags */
1799 if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) {
1800 dev_err(data->dev, "error processing nvm operation\n");
1801 return -EIO;
1802 }
1803
1804 return 0;
1805 }
1806
1807 /*
1808 * Contrary to previous sensors families, compensation algorithm is builtin.
1809 * We are only required to read the register raw data and adapt the ranges
1810 * for what is expected on IIO ABI.
1811 */
1812
bmp580_read_temp(struct bmp280_data * data,s32 * raw_temp)1813 static int bmp580_read_temp(struct bmp280_data *data, s32 *raw_temp)
1814 {
1815 s32 value_temp;
1816 int ret;
1817
1818 ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB,
1819 data->buf, BMP280_NUM_TEMP_BYTES);
1820 if (ret) {
1821 dev_err(data->dev, "failed to read temperature\n");
1822 return ret;
1823 }
1824
1825 value_temp = get_unaligned_le24(data->buf);
1826 if (value_temp == BMP580_TEMP_SKIPPED) {
1827 dev_err(data->dev, "reading temperature skipped\n");
1828 return -EIO;
1829 }
1830 *raw_temp = sign_extend32(value_temp, 23);
1831
1832 return 0;
1833 }
1834
bmp580_read_press(struct bmp280_data * data,u32 * raw_press)1835 static int bmp580_read_press(struct bmp280_data *data, u32 *raw_press)
1836 {
1837 u32 value_press;
1838 int ret;
1839
1840 ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB,
1841 data->buf, BMP280_NUM_PRESS_BYTES);
1842 if (ret) {
1843 dev_err(data->dev, "failed to read pressure\n");
1844 return ret;
1845 }
1846
1847 value_press = get_unaligned_le24(data->buf);
1848 if (value_press == BMP580_PRESS_SKIPPED) {
1849 dev_err(data->dev, "reading pressure skipped\n");
1850 return -EIO;
1851 }
1852 *raw_press = value_press;
1853
1854 return 0;
1855 }
1856
1857 static const int bmp580_odr_table[][2] = {
1858 [BMP580_ODR_240HZ] = {240, 0},
1859 [BMP580_ODR_218HZ] = {218, 0},
1860 [BMP580_ODR_199HZ] = {199, 0},
1861 [BMP580_ODR_179HZ] = {179, 0},
1862 [BMP580_ODR_160HZ] = {160, 0},
1863 [BMP580_ODR_149HZ] = {149, 0},
1864 [BMP580_ODR_140HZ] = {140, 0},
1865 [BMP580_ODR_129HZ] = {129, 0},
1866 [BMP580_ODR_120HZ] = {120, 0},
1867 [BMP580_ODR_110HZ] = {110, 0},
1868 [BMP580_ODR_100HZ] = {100, 0},
1869 [BMP580_ODR_89HZ] = {89, 0},
1870 [BMP580_ODR_80HZ] = {80, 0},
1871 [BMP580_ODR_70HZ] = {70, 0},
1872 [BMP580_ODR_60HZ] = {60, 0},
1873 [BMP580_ODR_50HZ] = {50, 0},
1874 [BMP580_ODR_45HZ] = {45, 0},
1875 [BMP580_ODR_40HZ] = {40, 0},
1876 [BMP580_ODR_35HZ] = {35, 0},
1877 [BMP580_ODR_30HZ] = {30, 0},
1878 [BMP580_ODR_25HZ] = {25, 0},
1879 [BMP580_ODR_20HZ] = {20, 0},
1880 [BMP580_ODR_15HZ] = {15, 0},
1881 [BMP580_ODR_10HZ] = {10, 0},
1882 [BMP580_ODR_5HZ] = {5, 0},
1883 [BMP580_ODR_4HZ] = {4, 0},
1884 [BMP580_ODR_3HZ] = {3, 0},
1885 [BMP580_ODR_2HZ] = {2, 0},
1886 [BMP580_ODR_1HZ] = {1, 0},
1887 [BMP580_ODR_0_5HZ] = {0, 500000},
1888 [BMP580_ODR_0_25HZ] = {0, 250000},
1889 [BMP580_ODR_0_125HZ] = {0, 125000},
1890 };
1891
1892 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
1893
bmp580_nvmem_read_impl(void * priv,unsigned int offset,void * val,size_t bytes)1894 static int bmp580_nvmem_read_impl(void *priv, unsigned int offset, void *val,
1895 size_t bytes)
1896 {
1897 struct bmp280_data *data = priv;
1898 u16 *dst = val;
1899 int ret, addr;
1900
1901 guard(mutex)(&data->lock);
1902
1903 /* Set sensor in standby mode */
1904 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1905 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1906 BMP580_ODR_DEEPSLEEP_DIS |
1907 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1908 if (ret) {
1909 dev_err(data->dev, "failed to change sensor to standby mode\n");
1910 goto exit;
1911 }
1912 /* Wait standby transition time */
1913 usleep_range(2500, 3000);
1914
1915 while (bytes >= sizeof(*dst)) {
1916 addr = bmp580_nvmem_addrs[offset / sizeof(*dst)];
1917
1918 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1919 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1920 if (ret) {
1921 dev_err(data->dev, "error writing nvm address\n");
1922 goto exit;
1923 }
1924
1925 ret = bmp580_nvm_operation(data, false);
1926 if (ret)
1927 goto exit;
1928
1929 ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB,
1930 &data->le16, sizeof(data->le16));
1931 if (ret) {
1932 dev_err(data->dev, "error reading nvm data regs\n");
1933 goto exit;
1934 }
1935
1936 *dst++ = le16_to_cpu(data->le16);
1937 bytes -= sizeof(*dst);
1938 offset += sizeof(*dst);
1939 }
1940 exit:
1941 /* Restore chip config */
1942 data->chip_info->chip_config(data);
1943 return ret;
1944 }
1945
bmp580_nvmem_read(void * priv,unsigned int offset,void * val,size_t bytes)1946 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
1947 size_t bytes)
1948 {
1949 struct bmp280_data *data = priv;
1950 int ret;
1951
1952 pm_runtime_get_sync(data->dev);
1953 ret = bmp580_nvmem_read_impl(priv, offset, val, bytes);
1954 pm_runtime_mark_last_busy(data->dev);
1955 pm_runtime_put_autosuspend(data->dev);
1956
1957 return ret;
1958 }
1959
bmp580_nvmem_write_impl(void * priv,unsigned int offset,void * val,size_t bytes)1960 static int bmp580_nvmem_write_impl(void *priv, unsigned int offset, void *val,
1961 size_t bytes)
1962 {
1963 struct bmp280_data *data = priv;
1964 u16 *buf = val;
1965 int ret, addr;
1966
1967 guard(mutex)(&data->lock);
1968
1969 /* Set sensor in standby mode */
1970 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1971 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1972 BMP580_ODR_DEEPSLEEP_DIS |
1973 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1974 if (ret) {
1975 dev_err(data->dev, "failed to change sensor to standby mode\n");
1976 goto exit;
1977 }
1978 /* Wait standby transition time */
1979 usleep_range(2500, 3000);
1980
1981 while (bytes >= sizeof(*buf)) {
1982 addr = bmp580_nvmem_addrs[offset / sizeof(*buf)];
1983
1984 ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1985 BMP580_NVM_PROG_EN |
1986 FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1987 if (ret) {
1988 dev_err(data->dev, "error writing nvm address\n");
1989 goto exit;
1990 }
1991 data->le16 = cpu_to_le16(*buf++);
1992
1993 ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB,
1994 &data->le16, sizeof(data->le16));
1995 if (ret) {
1996 dev_err(data->dev, "error writing LSB NVM data regs\n");
1997 goto exit;
1998 }
1999
2000 ret = bmp580_nvm_operation(data, true);
2001 if (ret)
2002 goto exit;
2003
2004 /* Disable programming mode bit */
2005 ret = regmap_clear_bits(data->regmap, BMP580_REG_NVM_ADDR,
2006 BMP580_NVM_PROG_EN);
2007 if (ret) {
2008 dev_err(data->dev, "error resetting nvm write\n");
2009 goto exit;
2010 }
2011
2012 bytes -= sizeof(*buf);
2013 offset += sizeof(*buf);
2014 }
2015 exit:
2016 /* Restore chip config */
2017 data->chip_info->chip_config(data);
2018 return ret;
2019 }
2020
bmp580_nvmem_write(void * priv,unsigned int offset,void * val,size_t bytes)2021 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
2022 size_t bytes)
2023 {
2024 struct bmp280_data *data = priv;
2025 int ret;
2026
2027 pm_runtime_get_sync(data->dev);
2028 ret = bmp580_nvmem_write_impl(priv, offset, val, bytes);
2029 pm_runtime_mark_last_busy(data->dev);
2030 pm_runtime_put_autosuspend(data->dev);
2031
2032 return ret;
2033 }
2034
bmp580_preinit(struct bmp280_data * data)2035 static int bmp580_preinit(struct bmp280_data *data)
2036 {
2037 struct nvmem_config config = {
2038 .dev = data->dev,
2039 .priv = data,
2040 .name = "bmp580_nvmem",
2041 .word_size = sizeof(u16),
2042 .stride = sizeof(u16),
2043 .size = 3 * sizeof(u16),
2044 .reg_read = bmp580_nvmem_read,
2045 .reg_write = bmp580_nvmem_write,
2046 };
2047 unsigned int reg;
2048 int ret;
2049
2050 /* Issue soft-reset command */
2051 ret = bmp580_soft_reset(data);
2052 if (ret)
2053 return ret;
2054
2055 /* Post powerup sequence */
2056 ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, ®);
2057 if (ret) {
2058 dev_err(data->dev, "failed to establish comms with the chip\n");
2059 return ret;
2060 }
2061
2062 /* Print warn message if we don't know the chip id */
2063 if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT)
2064 dev_warn(data->dev, "unexpected chip_id\n");
2065
2066 ret = regmap_read(data->regmap, BMP580_REG_STATUS, ®);
2067 if (ret) {
2068 dev_err(data->dev, "failed to read nvm status\n");
2069 return ret;
2070 }
2071
2072 /* Check nvm status */
2073 if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) {
2074 dev_err(data->dev, "nvm error on powerup sequence\n");
2075 return -EIO;
2076 }
2077
2078 /* Register nvmem device */
2079 return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
2080 }
2081
bmp580_chip_config(struct bmp280_data * data)2082 static int bmp580_chip_config(struct bmp280_data *data)
2083 {
2084 bool change = false, aux;
2085 unsigned int tmp;
2086 u8 reg_val;
2087 int ret;
2088
2089 /* Sets sensor in standby mode */
2090 ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
2091 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
2092 BMP580_ODR_DEEPSLEEP_DIS |
2093 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
2094 if (ret) {
2095 dev_err(data->dev, "failed to change sensor to standby mode\n");
2096 return ret;
2097 }
2098 /* From datasheet's table 4: electrical characteristics */
2099 usleep_range(2500, 3000);
2100
2101 /* Set default DSP mode settings */
2102 reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) |
2103 BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN;
2104
2105 ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG,
2106 BMP580_DSP_COMP_MASK |
2107 BMP580_DSP_SHDW_IIR_TEMP_EN |
2108 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val);
2109 if (ret) {
2110 dev_err(data->dev, "failed to change DSP mode settings\n");
2111 return ret;
2112 }
2113
2114 /* Configure oversampling */
2115 reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) |
2116 FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) |
2117 BMP580_OSR_PRESS_EN;
2118
2119 ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG,
2120 BMP580_OSR_TEMP_MASK |
2121 BMP580_OSR_PRESS_MASK |
2122 BMP580_OSR_PRESS_EN,
2123 reg_val, &aux);
2124 if (ret) {
2125 dev_err(data->dev, "failed to write oversampling register\n");
2126 return ret;
2127 }
2128 change = change || aux;
2129
2130 /* Configure output data rate */
2131 ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK,
2132 FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq),
2133 &aux);
2134 if (ret) {
2135 dev_err(data->dev, "failed to write ODR configuration register\n");
2136 return ret;
2137 }
2138 change = change || aux;
2139
2140 /* Set filter data */
2141 reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) |
2142 FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff);
2143
2144 ret = regmap_update_bits_check(data->regmap, BMP580_REG_DSP_IIR,
2145 BMP580_DSP_IIR_PRESS_MASK |
2146 BMP580_DSP_IIR_TEMP_MASK,
2147 reg_val, &aux);
2148 if (ret) {
2149 dev_err(data->dev, "failed to write config register\n");
2150 return ret;
2151 }
2152 change = change || aux;
2153
2154 /* Restore sensor to normal operation mode */
2155 ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG,
2156 BMP580_MODE_MASK,
2157 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_NORMAL));
2158 if (ret) {
2159 dev_err(data->dev, "failed to set normal mode\n");
2160 return ret;
2161 }
2162 /* From datasheet's table 4: electrical characteristics */
2163 usleep_range(3000, 3500);
2164
2165 if (change) {
2166 /*
2167 * Check if ODR and OSR settings are valid or we are
2168 * operating in a degraded mode.
2169 */
2170 ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp);
2171 if (ret) {
2172 dev_err(data->dev,
2173 "error reading effective OSR register\n");
2174 return ret;
2175 }
2176 if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) {
2177 dev_warn(data->dev, "OSR and ODR incompatible settings detected\n");
2178 /* Set current OSR settings from data on effective OSR */
2179 data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp);
2180 data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp);
2181 return -EINVAL;
2182 }
2183 }
2184
2185 return 0;
2186 }
2187
bmp580_trigger_handler(int irq,void * p)2188 static irqreturn_t bmp580_trigger_handler(int irq, void *p)
2189 {
2190 struct iio_poll_func *pf = p;
2191 struct iio_dev *indio_dev = pf->indio_dev;
2192 struct bmp280_data *data = iio_priv(indio_dev);
2193 int ret;
2194
2195 guard(mutex)(&data->lock);
2196
2197 /* Burst read data registers */
2198 ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB,
2199 data->buf, BMP280_BURST_READ_BYTES);
2200 if (ret) {
2201 dev_err(data->dev, "failed to burst read sensor data\n");
2202 goto out;
2203 }
2204
2205 /* Temperature calculations */
2206 memcpy(&data->sensor_data[1], &data->buf[0], 3);
2207
2208 /* Pressure calculations */
2209 memcpy(&data->sensor_data[0], &data->buf[3], 3);
2210
2211 iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
2212 iio_get_time_ns(indio_dev));
2213
2214 out:
2215 iio_trigger_notify_done(indio_dev->trig);
2216
2217 return IRQ_HANDLED;
2218 }
2219
2220 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
2221 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT };
2222 /* Instead of { 1000, 16 } we do this, to avoid overflow issues */
2223 static const int bmp580_temp_coeffs[] = { 125, 13 };
2224 static const int bmp580_press_coeffs[] = { 1, 64000};
2225
2226 const struct bmp280_chip_info bmp580_chip_info = {
2227 .id_reg = BMP580_REG_CHIP_ID,
2228 .chip_id = bmp580_chip_ids,
2229 .num_chip_id = ARRAY_SIZE(bmp580_chip_ids),
2230 .regmap_config = &bmp580_regmap_config,
2231 .start_up_time = 2000,
2232 .channels = bmp580_channels,
2233 .num_channels = ARRAY_SIZE(bmp580_channels),
2234 .avail_scan_masks = bmp280_avail_scan_masks,
2235
2236 .oversampling_temp_avail = bmp580_oversampling_avail,
2237 .num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail),
2238 .oversampling_temp_default = ilog2(1),
2239
2240 .oversampling_press_avail = bmp580_oversampling_avail,
2241 .num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail),
2242 .oversampling_press_default = ilog2(4),
2243
2244 .sampling_freq_avail = bmp580_odr_table,
2245 .num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2,
2246 .sampling_freq_default = BMP580_ODR_50HZ,
2247
2248 .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
2249 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
2250 .iir_filter_coeff_default = 2,
2251
2252 .temp_coeffs = bmp580_temp_coeffs,
2253 .temp_coeffs_type = IIO_VAL_FRACTIONAL_LOG2,
2254 .press_coeffs = bmp580_press_coeffs,
2255 .press_coeffs_type = IIO_VAL_FRACTIONAL,
2256
2257 .chip_config = bmp580_chip_config,
2258 .read_temp = bmp580_read_temp,
2259 .read_press = bmp580_read_press,
2260 .preinit = bmp580_preinit,
2261
2262 .trigger_handler = bmp580_trigger_handler,
2263 };
2264 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280);
2265
bmp180_wait_for_eoc(struct bmp280_data * data,u8 ctrl_meas)2266 static int bmp180_wait_for_eoc(struct bmp280_data *data, u8 ctrl_meas)
2267 {
2268 static const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
2269 unsigned int delay_us;
2270 unsigned int ctrl;
2271 int ret;
2272
2273 if (data->use_eoc)
2274 reinit_completion(&data->done);
2275
2276 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
2277 if (ret) {
2278 dev_err(data->dev, "failed to write crtl_meas register\n");
2279 return ret;
2280 }
2281
2282 if (data->use_eoc) {
2283 /*
2284 * If we have a completion interrupt, use it, wait up to
2285 * 100ms. The longest conversion time listed is 76.5 ms for
2286 * advanced resolution mode.
2287 */
2288 ret = wait_for_completion_timeout(&data->done,
2289 1 + msecs_to_jiffies(100));
2290 if (!ret)
2291 dev_err(data->dev, "timeout waiting for completion\n");
2292 } else {
2293 if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
2294 delay_us = 4500;
2295 else
2296 delay_us =
2297 conversion_time_max[data->oversampling_press];
2298
2299 usleep_range(delay_us, delay_us + 1000);
2300 }
2301
2302 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
2303 if (ret) {
2304 dev_err(data->dev, "failed to read ctrl_meas register\n");
2305 return ret;
2306 }
2307
2308 /* The value of this bit reset to "0" after conversion is complete */
2309 if (ctrl & BMP180_MEAS_SCO) {
2310 dev_err(data->dev, "conversion didn't complete\n");
2311 return -EIO;
2312 }
2313
2314 return 0;
2315 }
2316
bmp180_read_temp_adc(struct bmp280_data * data,u32 * adc_temp)2317 static int bmp180_read_temp_adc(struct bmp280_data *data, u32 *adc_temp)
2318 {
2319 int ret;
2320
2321 ret = bmp180_wait_for_eoc(data,
2322 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
2323 BMP180_MEAS_SCO);
2324 if (ret)
2325 return ret;
2326
2327 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
2328 &data->be16, sizeof(data->be16));
2329 if (ret) {
2330 dev_err(data->dev, "failed to read temperature\n");
2331 return ret;
2332 }
2333
2334 *adc_temp = be16_to_cpu(data->be16);
2335
2336 return 0;
2337 }
2338
bmp180_read_calib(struct bmp280_data * data)2339 static int bmp180_read_calib(struct bmp280_data *data)
2340 {
2341 struct bmp180_calib *calib = &data->calib.bmp180;
2342 int ret;
2343 int i;
2344
2345 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
2346 data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
2347 if (ret) {
2348 dev_err(data->dev, "failed to read calibration parameters\n");
2349 return ret;
2350 }
2351
2352 /* None of the words has the value 0 or 0xFFFF */
2353 for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) {
2354 if (data->bmp180_cal_buf[i] == cpu_to_be16(0) ||
2355 data->bmp180_cal_buf[i] == cpu_to_be16(0xffff))
2356 return -EIO;
2357 }
2358
2359 /* Toss the calibration data into the entropy pool */
2360 add_device_randomness(data->bmp180_cal_buf,
2361 sizeof(data->bmp180_cal_buf));
2362
2363 calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]);
2364 calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]);
2365 calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]);
2366 calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]);
2367 calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]);
2368 calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]);
2369 calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]);
2370 calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]);
2371 calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]);
2372 calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]);
2373 calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]);
2374
2375 return 0;
2376 }
2377
2378 /*
2379 * Returns temperature in DegC, resolution is 0.1 DegC.
2380 * t_fine carries fine temperature as global value.
2381 *
2382 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
2383 */
2384
bmp180_calc_t_fine(struct bmp280_data * data,u32 adc_temp)2385 static s32 bmp180_calc_t_fine(struct bmp280_data *data, u32 adc_temp)
2386 {
2387 struct bmp180_calib *calib = &data->calib.bmp180;
2388 s32 x1, x2;
2389
2390 x1 = ((((s32)adc_temp) - calib->AC6) * calib->AC5) >> 15;
2391 x2 = (calib->MC << 11) / (x1 + calib->MD);
2392 return x1 + x2; /* t_fine = x1 + x2; */
2393 }
2394
bmp180_get_t_fine(struct bmp280_data * data,s32 * t_fine)2395 static int bmp180_get_t_fine(struct bmp280_data *data, s32 *t_fine)
2396 {
2397 s32 adc_temp;
2398 int ret;
2399
2400 ret = bmp180_read_temp_adc(data, &adc_temp);
2401 if (ret)
2402 return ret;
2403
2404 *t_fine = bmp180_calc_t_fine(data, adc_temp);
2405
2406 return 0;
2407 }
2408
bmp180_compensate_temp(struct bmp280_data * data,u32 adc_temp)2409 static s32 bmp180_compensate_temp(struct bmp280_data *data, u32 adc_temp)
2410 {
2411 return (bmp180_calc_t_fine(data, adc_temp) + 8) / 16;
2412 }
2413
bmp180_read_temp(struct bmp280_data * data,s32 * comp_temp)2414 static int bmp180_read_temp(struct bmp280_data *data, s32 *comp_temp)
2415 {
2416 u32 adc_temp;
2417 int ret;
2418
2419 ret = bmp180_read_temp_adc(data, &adc_temp);
2420 if (ret)
2421 return ret;
2422
2423 *comp_temp = bmp180_compensate_temp(data, adc_temp);
2424
2425 return 0;
2426 }
2427
bmp180_read_press_adc(struct bmp280_data * data,u32 * adc_press)2428 static int bmp180_read_press_adc(struct bmp280_data *data, u32 *adc_press)
2429 {
2430 u8 oss = data->oversampling_press;
2431 int ret;
2432
2433 ret = bmp180_wait_for_eoc(data,
2434 FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
2435 FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
2436 BMP180_MEAS_SCO);
2437 if (ret)
2438 return ret;
2439
2440 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
2441 data->buf, BMP280_NUM_PRESS_BYTES);
2442 if (ret) {
2443 dev_err(data->dev, "failed to read pressure\n");
2444 return ret;
2445 }
2446
2447 *adc_press = get_unaligned_be24(data->buf) >> (8 - oss);
2448
2449 return 0;
2450 }
2451
2452 /*
2453 * Returns pressure in Pa, resolution is 1 Pa.
2454 *
2455 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
2456 */
bmp180_compensate_press(struct bmp280_data * data,u32 adc_press,s32 t_fine)2457 static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press,
2458 s32 t_fine)
2459 {
2460 struct bmp180_calib *calib = &data->calib.bmp180;
2461 s32 oss = data->oversampling_press;
2462 s32 x1, x2, x3, p;
2463 s32 b3, b6;
2464 u32 b4, b7;
2465
2466 b6 = t_fine - 4000;
2467 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
2468 x2 = calib->AC2 * b6 >> 11;
2469 x3 = x1 + x2;
2470 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
2471 x1 = calib->AC3 * b6 >> 13;
2472 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
2473 x3 = (x1 + x2 + 2) >> 2;
2474 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
2475 b7 = (adc_press - b3) * (50000 >> oss);
2476 if (b7 < 0x80000000)
2477 p = (b7 * 2) / b4;
2478 else
2479 p = (b7 / b4) * 2;
2480
2481 x1 = (p >> 8) * (p >> 8);
2482 x1 = (x1 * 3038) >> 16;
2483 x2 = (-7357 * p) >> 16;
2484
2485 return p + ((x1 + x2 + 3791) >> 4);
2486 }
2487
bmp180_read_press(struct bmp280_data * data,u32 * comp_press)2488 static int bmp180_read_press(struct bmp280_data *data, u32 *comp_press)
2489 {
2490 u32 adc_press;
2491 s32 t_fine;
2492 int ret;
2493
2494 ret = bmp180_get_t_fine(data, &t_fine);
2495 if (ret)
2496 return ret;
2497
2498 ret = bmp180_read_press_adc(data, &adc_press);
2499 if (ret)
2500 return ret;
2501
2502 *comp_press = bmp180_compensate_press(data, adc_press, t_fine);
2503
2504 return 0;
2505 }
2506
bmp180_chip_config(struct bmp280_data * data)2507 static int bmp180_chip_config(struct bmp280_data *data)
2508 {
2509 return 0;
2510 }
2511
bmp180_trigger_handler(int irq,void * p)2512 static irqreturn_t bmp180_trigger_handler(int irq, void *p)
2513 {
2514 struct iio_poll_func *pf = p;
2515 struct iio_dev *indio_dev = pf->indio_dev;
2516 struct bmp280_data *data = iio_priv(indio_dev);
2517 int ret, chan_value;
2518
2519 guard(mutex)(&data->lock);
2520
2521 ret = bmp180_read_temp(data, &chan_value);
2522 if (ret)
2523 goto out;
2524
2525 data->sensor_data[1] = chan_value;
2526
2527 ret = bmp180_read_press(data, &chan_value);
2528 if (ret)
2529 goto out;
2530
2531 data->sensor_data[0] = chan_value;
2532
2533 iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
2534 iio_get_time_ns(indio_dev));
2535
2536 out:
2537 iio_trigger_notify_done(indio_dev->trig);
2538
2539 return IRQ_HANDLED;
2540 }
2541
2542 static const int bmp180_oversampling_temp_avail[] = { 1 };
2543 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
2544 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID };
2545 static const int bmp180_temp_coeffs[] = { 100, 1 };
2546 static const int bmp180_press_coeffs[] = { 1, 1000 };
2547
2548 const struct bmp280_chip_info bmp180_chip_info = {
2549 .id_reg = BMP280_REG_ID,
2550 .chip_id = bmp180_chip_ids,
2551 .num_chip_id = ARRAY_SIZE(bmp180_chip_ids),
2552 .regmap_config = &bmp180_regmap_config,
2553 .start_up_time = 2000,
2554 .channels = bmp280_channels,
2555 .num_channels = ARRAY_SIZE(bmp280_channels),
2556 .avail_scan_masks = bmp280_avail_scan_masks,
2557
2558 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
2559 .num_oversampling_temp_avail =
2560 ARRAY_SIZE(bmp180_oversampling_temp_avail),
2561 .oversampling_temp_default = 0,
2562
2563 .oversampling_press_avail = bmp180_oversampling_press_avail,
2564 .num_oversampling_press_avail =
2565 ARRAY_SIZE(bmp180_oversampling_press_avail),
2566 .oversampling_press_default = BMP180_MEAS_PRESS_8X,
2567
2568 .temp_coeffs = bmp180_temp_coeffs,
2569 .temp_coeffs_type = IIO_VAL_FRACTIONAL,
2570 .press_coeffs = bmp180_press_coeffs,
2571 .press_coeffs_type = IIO_VAL_FRACTIONAL,
2572
2573 .chip_config = bmp180_chip_config,
2574 .read_temp = bmp180_read_temp,
2575 .read_press = bmp180_read_press,
2576 .read_calib = bmp180_read_calib,
2577
2578 .trigger_handler = bmp180_trigger_handler,
2579 };
2580 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280);
2581
bmp085_eoc_irq(int irq,void * d)2582 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
2583 {
2584 struct bmp280_data *data = d;
2585
2586 complete(&data->done);
2587
2588 return IRQ_HANDLED;
2589 }
2590
bmp085_fetch_eoc_irq(struct device * dev,const char * name,int irq,struct bmp280_data * data)2591 static int bmp085_fetch_eoc_irq(struct device *dev,
2592 const char *name,
2593 int irq,
2594 struct bmp280_data *data)
2595 {
2596 unsigned long irq_trig;
2597 int ret;
2598
2599 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
2600 if (irq_trig != IRQF_TRIGGER_RISING) {
2601 dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
2602 irq_trig = IRQF_TRIGGER_RISING;
2603 }
2604
2605 init_completion(&data->done);
2606
2607 ret = devm_request_threaded_irq(dev,
2608 irq,
2609 bmp085_eoc_irq,
2610 NULL,
2611 irq_trig,
2612 name,
2613 data);
2614 if (ret) {
2615 /* Bail out without IRQ but keep the driver in place */
2616 dev_err(dev, "unable to request DRDY IRQ\n");
2617 return 0;
2618 }
2619
2620 data->use_eoc = true;
2621 return 0;
2622 }
2623
bmp280_buffer_preenable(struct iio_dev * indio_dev)2624 static int bmp280_buffer_preenable(struct iio_dev *indio_dev)
2625 {
2626 struct bmp280_data *data = iio_priv(indio_dev);
2627
2628 pm_runtime_get_sync(data->dev);
2629
2630 return 0;
2631 }
2632
bmp280_buffer_postdisable(struct iio_dev * indio_dev)2633 static int bmp280_buffer_postdisable(struct iio_dev *indio_dev)
2634 {
2635 struct bmp280_data *data = iio_priv(indio_dev);
2636
2637 pm_runtime_mark_last_busy(data->dev);
2638 pm_runtime_put_autosuspend(data->dev);
2639
2640 return 0;
2641 }
2642
2643 static const struct iio_buffer_setup_ops bmp280_buffer_setup_ops = {
2644 .preenable = bmp280_buffer_preenable,
2645 .postdisable = bmp280_buffer_postdisable,
2646 };
2647
bmp280_pm_disable(void * data)2648 static void bmp280_pm_disable(void *data)
2649 {
2650 struct device *dev = data;
2651
2652 pm_runtime_get_sync(dev);
2653 pm_runtime_put_noidle(dev);
2654 pm_runtime_disable(dev);
2655 }
2656
bmp280_regulators_disable(void * data)2657 static void bmp280_regulators_disable(void *data)
2658 {
2659 struct regulator_bulk_data *supplies = data;
2660
2661 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
2662 }
2663
bmp280_common_probe(struct device * dev,struct regmap * regmap,const struct bmp280_chip_info * chip_info,const char * name,int irq)2664 int bmp280_common_probe(struct device *dev,
2665 struct regmap *regmap,
2666 const struct bmp280_chip_info *chip_info,
2667 const char *name,
2668 int irq)
2669 {
2670 struct iio_dev *indio_dev;
2671 struct bmp280_data *data;
2672 struct gpio_desc *gpiod;
2673 unsigned int chip_id;
2674 unsigned int i;
2675 int ret;
2676
2677 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
2678 if (!indio_dev)
2679 return -ENOMEM;
2680
2681 data = iio_priv(indio_dev);
2682 mutex_init(&data->lock);
2683 data->dev = dev;
2684
2685 indio_dev->name = name;
2686 indio_dev->info = &bmp280_info;
2687 indio_dev->modes = INDIO_DIRECT_MODE;
2688
2689 data->chip_info = chip_info;
2690
2691 /* Apply initial values from chip info structure */
2692 indio_dev->channels = chip_info->channels;
2693 indio_dev->num_channels = chip_info->num_channels;
2694 indio_dev->available_scan_masks = chip_info->avail_scan_masks;
2695 data->oversampling_press = chip_info->oversampling_press_default;
2696 data->oversampling_humid = chip_info->oversampling_humid_default;
2697 data->oversampling_temp = chip_info->oversampling_temp_default;
2698 data->iir_filter_coeff = chip_info->iir_filter_coeff_default;
2699 data->sampling_freq = chip_info->sampling_freq_default;
2700 data->start_up_time = chip_info->start_up_time;
2701
2702 /* Bring up regulators */
2703 regulator_bulk_set_supply_names(data->supplies,
2704 bmp280_supply_names,
2705 BMP280_NUM_SUPPLIES);
2706
2707 ret = devm_regulator_bulk_get(dev,
2708 BMP280_NUM_SUPPLIES, data->supplies);
2709 if (ret) {
2710 dev_err(dev, "failed to get regulators\n");
2711 return ret;
2712 }
2713
2714 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2715 if (ret) {
2716 dev_err(dev, "failed to enable regulators\n");
2717 return ret;
2718 }
2719
2720 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
2721 data->supplies);
2722 if (ret)
2723 return ret;
2724
2725 /* Wait to make sure we started up properly */
2726 usleep_range(data->start_up_time, data->start_up_time + 100);
2727
2728 /* Bring chip out of reset if there is an assigned GPIO line */
2729 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
2730 /* Deassert the signal */
2731 if (gpiod) {
2732 dev_info(dev, "release reset\n");
2733 gpiod_set_value(gpiod, 0);
2734 }
2735
2736 data->regmap = regmap;
2737
2738 ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
2739 if (ret) {
2740 dev_err(data->dev, "failed to read chip id\n");
2741 return ret;
2742 }
2743
2744 for (i = 0; i < data->chip_info->num_chip_id; i++) {
2745 if (chip_id == data->chip_info->chip_id[i]) {
2746 dev_info(dev, "0x%x is a known chip id for %s\n", chip_id, name);
2747 break;
2748 }
2749 }
2750
2751 if (i == data->chip_info->num_chip_id)
2752 dev_warn(dev, "bad chip id: 0x%x is not a known chip id\n", chip_id);
2753
2754 if (data->chip_info->preinit) {
2755 ret = data->chip_info->preinit(data);
2756 if (ret)
2757 return dev_err_probe(data->dev, ret,
2758 "error running preinit tasks\n");
2759 }
2760
2761 ret = data->chip_info->chip_config(data);
2762 if (ret)
2763 return ret;
2764
2765 dev_set_drvdata(dev, indio_dev);
2766
2767 /*
2768 * Some chips have calibration parameters "programmed into the devices'
2769 * non-volatile memory during production". Let's read them out at probe
2770 * time once. They will not change.
2771 */
2772
2773 if (data->chip_info->read_calib) {
2774 ret = data->chip_info->read_calib(data);
2775 if (ret)
2776 return dev_err_probe(data->dev, ret,
2777 "failed to read calibration coefficients\n");
2778 }
2779
2780 ret = devm_iio_triggered_buffer_setup(data->dev, indio_dev,
2781 iio_pollfunc_store_time,
2782 data->chip_info->trigger_handler,
2783 &bmp280_buffer_setup_ops);
2784 if (ret)
2785 return dev_err_probe(data->dev, ret,
2786 "iio triggered buffer setup failed\n");
2787
2788 /*
2789 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
2790 * however as it happens, the BMP085 shares the chip ID of BMP180
2791 * so we look for an IRQ if we have that.
2792 */
2793 if (irq > 0 && (chip_id == BMP180_CHIP_ID)) {
2794 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
2795 if (ret)
2796 return ret;
2797 }
2798
2799 /* Enable runtime PM */
2800 pm_runtime_get_noresume(dev);
2801 pm_runtime_set_active(dev);
2802 pm_runtime_enable(dev);
2803 /*
2804 * Set autosuspend to two orders of magnitude larger than the
2805 * start-up time.
2806 */
2807 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
2808 pm_runtime_use_autosuspend(dev);
2809 pm_runtime_put(dev);
2810
2811 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
2812 if (ret)
2813 return ret;
2814
2815 return devm_iio_device_register(dev, indio_dev);
2816 }
2817 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
2818
bmp280_runtime_suspend(struct device * dev)2819 static int bmp280_runtime_suspend(struct device *dev)
2820 {
2821 struct iio_dev *indio_dev = dev_get_drvdata(dev);
2822 struct bmp280_data *data = iio_priv(indio_dev);
2823
2824 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
2825 }
2826
bmp280_runtime_resume(struct device * dev)2827 static int bmp280_runtime_resume(struct device *dev)
2828 {
2829 struct iio_dev *indio_dev = dev_get_drvdata(dev);
2830 struct bmp280_data *data = iio_priv(indio_dev);
2831 int ret;
2832
2833 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2834 if (ret)
2835 return ret;
2836
2837 usleep_range(data->start_up_time, data->start_up_time + 100);
2838 return data->chip_info->chip_config(data);
2839 }
2840
2841 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
2842 bmp280_runtime_resume, NULL);
2843
2844 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
2845 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
2846 MODULE_LICENSE("GPL v2");
2847