1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
4 * The DPS310 is a barometric pressure and temperature sensor.
5 * Currently only reading a single temperature is supported by
8 * https://www.infineon.com/dgdl/?fileId=5546d462576f34750157750826c42242
10 * Temperature calculation:
11 * c0 * 0.5 + c1 * T_raw / kT °C
14 * - Optionally support the FIFO
17 #include <linux/i2c.h>
18 #include <linux/limits.h>
19 #include <linux/math64.h>
20 #include <linux/module.h>
21 #include <linux/regmap.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 #define DPS310_DEV_NAME "dps310"
28 #define DPS310_PRS_B0 0x00
29 #define DPS310_PRS_B1 0x01
30 #define DPS310_PRS_B2 0x02
31 #define DPS310_TMP_B0 0x03
32 #define DPS310_TMP_B1 0x04
33 #define DPS310_TMP_B2 0x05
34 #define DPS310_PRS_CFG 0x06
35 #define DPS310_PRS_RATE_BITS GENMASK(6, 4)
36 #define DPS310_PRS_PRC_BITS GENMASK(3, 0)
37 #define DPS310_TMP_CFG 0x07
38 #define DPS310_TMP_RATE_BITS GENMASK(6, 4)
39 #define DPS310_TMP_PRC_BITS GENMASK(3, 0)
40 #define DPS310_TMP_EXT BIT(7)
41 #define DPS310_MEAS_CFG 0x08
42 #define DPS310_MEAS_CTRL_BITS GENMASK(2, 0)
43 #define DPS310_PRS_EN BIT(0)
44 #define DPS310_TEMP_EN BIT(1)
45 #define DPS310_BACKGROUND BIT(2)
46 #define DPS310_PRS_RDY BIT(4)
47 #define DPS310_TMP_RDY BIT(5)
48 #define DPS310_SENSOR_RDY BIT(6)
49 #define DPS310_COEF_RDY BIT(7)
50 #define DPS310_CFG_REG 0x09
51 #define DPS310_INT_HL BIT(7)
52 #define DPS310_TMP_SHIFT_EN BIT(3)
53 #define DPS310_PRS_SHIFT_EN BIT(4)
54 #define DPS310_FIFO_EN BIT(5)
55 #define DPS310_SPI_EN BIT(6)
56 #define DPS310_RESET 0x0c
57 #define DPS310_RESET_MAGIC 0x09
58 #define DPS310_COEF_BASE 0x10
60 /* Make sure sleep time is <= 30ms for usleep_range */
61 #define DPS310_POLL_SLEEP_US(t) min(30000, (t) / 8)
62 /* Silently handle error in rate value here */
63 #define DPS310_POLL_TIMEOUT_US(rc) ((rc) <= 0 ? 1000000 : 1000000 / (rc))
65 #define DPS310_PRS_BASE DPS310_PRS_B0
66 #define DPS310_TMP_BASE DPS310_TMP_B0
69 * These values (defined in the spec) indicate how to scale the raw register
70 * values for each level of precision available.
72 static const int scale_factors
[] = {
84 struct i2c_client
*client
;
85 struct regmap
*regmap
;
86 struct mutex lock
; /* Lock for sequential HW access functions */
89 s32 c00
, c10
, c20
, c30
, c01
, c11
, c21
;
92 bool timeout_recovery_failed
;
95 static const struct iio_chan_spec dps310_channels
[] = {
98 .info_mask_separate
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
) |
99 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
100 BIT(IIO_CHAN_INFO_PROCESSED
),
103 .type
= IIO_PRESSURE
,
104 .info_mask_separate
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
) |
105 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
106 BIT(IIO_CHAN_INFO_PROCESSED
),
110 /* To be called after checking the COEF_RDY bit in MEAS_CFG */
111 static int dps310_get_coefs(struct dps310_data
*data
)
116 u32 c00
, c10
, c20
, c30
, c01
, c11
, c21
;
118 /* Read all sensor calibration coefficients from the COEF registers. */
119 rc
= regmap_bulk_read(data
->regmap
, DPS310_COEF_BASE
, coef
,
125 * Calculate temperature calibration coefficients c0 and c1. The
126 * numbers are 12-bit 2's complement numbers.
128 c0
= (coef
[0] << 4) | (coef
[1] >> 4);
129 data
->c0
= sign_extend32(c0
, 11);
131 c1
= ((coef
[1] & GENMASK(3, 0)) << 8) | coef
[2];
132 data
->c1
= sign_extend32(c1
, 11);
135 * Calculate pressure calibration coefficients. c00 and c10 are 20 bit
136 * 2's complement numbers, while the rest are 16 bit 2's complement
139 c00
= (coef
[3] << 12) | (coef
[4] << 4) | (coef
[5] >> 4);
140 data
->c00
= sign_extend32(c00
, 19);
142 c10
= ((coef
[5] & GENMASK(3, 0)) << 16) | (coef
[6] << 8) | coef
[7];
143 data
->c10
= sign_extend32(c10
, 19);
145 c01
= (coef
[8] << 8) | coef
[9];
146 data
->c01
= sign_extend32(c01
, 15);
148 c11
= (coef
[10] << 8) | coef
[11];
149 data
->c11
= sign_extend32(c11
, 15);
151 c20
= (coef
[12] << 8) | coef
[13];
152 data
->c20
= sign_extend32(c20
, 15);
154 c21
= (coef
[14] << 8) | coef
[15];
155 data
->c21
= sign_extend32(c21
, 15);
157 c30
= (coef
[16] << 8) | coef
[17];
158 data
->c30
= sign_extend32(c30
, 15);
164 * Some versions of the chip will read temperatures in the ~60C range when
165 * it's actually ~20C. This is the manufacturer recommended workaround
166 * to correct the issue. The registers used below are undocumented.
168 static int dps310_temp_workaround(struct dps310_data
*data
)
173 rc
= regmap_read(data
->regmap
, 0x32, ®
);
178 * If bit 1 is set then the device is okay, and the workaround does not
184 rc
= regmap_write(data
->regmap
, 0x0e, 0xA5);
188 rc
= regmap_write(data
->regmap
, 0x0f, 0x96);
192 rc
= regmap_write(data
->regmap
, 0x62, 0x02);
196 rc
= regmap_write(data
->regmap
, 0x0e, 0x00);
200 return regmap_write(data
->regmap
, 0x0f, 0x00);
203 static int dps310_startup(struct dps310_data
*data
)
209 * Set up pressure sensor in single sample, one measurement per second
212 rc
= regmap_write(data
->regmap
, DPS310_PRS_CFG
, 0);
217 * Set up external (MEMS) temperature sensor in single sample, one
218 * measurement per second mode
220 rc
= regmap_write(data
->regmap
, DPS310_TMP_CFG
, DPS310_TMP_EXT
);
224 /* Temp and pressure shifts are disabled when PRC <= 8 */
225 rc
= regmap_write_bits(data
->regmap
, DPS310_CFG_REG
,
226 DPS310_PRS_SHIFT_EN
| DPS310_TMP_SHIFT_EN
, 0);
230 /* MEAS_CFG doesn't update correctly unless first written with 0 */
231 rc
= regmap_write_bits(data
->regmap
, DPS310_MEAS_CFG
,
232 DPS310_MEAS_CTRL_BITS
, 0);
236 /* Turn on temperature and pressure measurement in the background */
237 rc
= regmap_write_bits(data
->regmap
, DPS310_MEAS_CFG
,
238 DPS310_MEAS_CTRL_BITS
, DPS310_PRS_EN
|
239 DPS310_TEMP_EN
| DPS310_BACKGROUND
);
244 * Calibration coefficients required for reporting temperature.
245 * They are available 40ms after the device has started
247 rc
= regmap_read_poll_timeout(data
->regmap
, DPS310_MEAS_CFG
, ready
,
248 ready
& DPS310_COEF_RDY
, 10000, 40000);
252 rc
= dps310_get_coefs(data
);
256 return dps310_temp_workaround(data
);
259 static int dps310_get_pres_precision(struct dps310_data
*data
, int *val
)
263 rc
= regmap_read(data
->regmap
, DPS310_PRS_CFG
, ®_val
);
267 *val
= BIT(reg_val
& GENMASK(2, 0));
272 static int dps310_get_temp_precision(struct dps310_data
*data
, int *val
)
276 rc
= regmap_read(data
->regmap
, DPS310_TMP_CFG
, ®_val
);
281 * Scale factor is bottom 4 bits of the register, but 1111 is
282 * reserved so just grab bottom three
284 *val
= BIT(reg_val
& GENMASK(2, 0));
289 /* Called with lock held */
290 static int dps310_set_pres_precision(struct dps310_data
*data
, int val
)
295 if (val
< 0 || val
> 128)
298 shift_en
= val
>= 16 ? DPS310_PRS_SHIFT_EN
: 0;
299 rc
= regmap_write_bits(data
->regmap
, DPS310_CFG_REG
,
300 DPS310_PRS_SHIFT_EN
, shift_en
);
304 return regmap_update_bits(data
->regmap
, DPS310_PRS_CFG
,
305 DPS310_PRS_PRC_BITS
, ilog2(val
));
308 /* Called with lock held */
309 static int dps310_set_temp_precision(struct dps310_data
*data
, int val
)
314 if (val
< 0 || val
> 128)
317 shift_en
= val
>= 16 ? DPS310_TMP_SHIFT_EN
: 0;
318 rc
= regmap_write_bits(data
->regmap
, DPS310_CFG_REG
,
319 DPS310_TMP_SHIFT_EN
, shift_en
);
323 return regmap_update_bits(data
->regmap
, DPS310_TMP_CFG
,
324 DPS310_TMP_PRC_BITS
, ilog2(val
));
327 /* Called with lock held */
328 static int dps310_set_pres_samp_freq(struct dps310_data
*data
, int freq
)
332 if (freq
< 0 || freq
> 128)
335 val
= ilog2(freq
) << 4;
337 return regmap_update_bits(data
->regmap
, DPS310_PRS_CFG
,
338 DPS310_PRS_RATE_BITS
, val
);
341 /* Called with lock held */
342 static int dps310_set_temp_samp_freq(struct dps310_data
*data
, int freq
)
346 if (freq
< 0 || freq
> 128)
349 val
= ilog2(freq
) << 4;
351 return regmap_update_bits(data
->regmap
, DPS310_TMP_CFG
,
352 DPS310_TMP_RATE_BITS
, val
);
355 static int dps310_get_pres_samp_freq(struct dps310_data
*data
, int *val
)
359 rc
= regmap_read(data
->regmap
, DPS310_PRS_CFG
, ®_val
);
363 *val
= BIT((reg_val
& DPS310_PRS_RATE_BITS
) >> 4);
368 static int dps310_get_temp_samp_freq(struct dps310_data
*data
, int *val
)
372 rc
= regmap_read(data
->regmap
, DPS310_TMP_CFG
, ®_val
);
376 *val
= BIT((reg_val
& DPS310_TMP_RATE_BITS
) >> 4);
381 static int dps310_get_pres_k(struct dps310_data
*data
, int *val
)
385 rc
= regmap_read(data
->regmap
, DPS310_PRS_CFG
, ®_val
);
389 *val
= scale_factors
[reg_val
& GENMASK(2, 0)];
394 static int dps310_get_temp_k(struct dps310_data
*data
, int *val
)
398 rc
= regmap_read(data
->regmap
, DPS310_TMP_CFG
, ®_val
);
402 *val
= scale_factors
[reg_val
& GENMASK(2, 0)];
407 static int dps310_reset_wait(struct dps310_data
*data
)
411 rc
= regmap_write(data
->regmap
, DPS310_RESET
, DPS310_RESET_MAGIC
);
415 /* Wait for device chip access: 15ms in specification */
416 usleep_range(15000, 55000);
420 static int dps310_reset_reinit(struct dps310_data
*data
)
424 rc
= dps310_reset_wait(data
);
428 return dps310_startup(data
);
431 static int dps310_ready_status(struct dps310_data
*data
, int ready_bit
, int timeout
)
433 int sleep
= DPS310_POLL_SLEEP_US(timeout
);
436 return regmap_read_poll_timeout(data
->regmap
, DPS310_MEAS_CFG
, ready
, ready
& ready_bit
,
440 static int dps310_ready(struct dps310_data
*data
, int ready_bit
, int timeout
)
444 rc
= dps310_ready_status(data
, ready_bit
, timeout
);
446 if (rc
== -ETIMEDOUT
&& !data
->timeout_recovery_failed
) {
447 /* Reset and reinitialize the chip. */
448 if (dps310_reset_reinit(data
)) {
449 data
->timeout_recovery_failed
= true;
451 /* Try again to get sensor ready status. */
452 if (dps310_ready_status(data
, ready_bit
, timeout
))
453 data
->timeout_recovery_failed
= true;
462 data
->timeout_recovery_failed
= false;
466 static int dps310_read_pres_raw(struct dps310_data
*data
)
474 if (mutex_lock_interruptible(&data
->lock
))
477 rc
= dps310_get_pres_samp_freq(data
, &rate
);
481 timeout
= DPS310_POLL_TIMEOUT_US(rate
);
483 /* Poll for sensor readiness; base the timeout upon the sample rate. */
484 rc
= dps310_ready(data
, DPS310_PRS_RDY
, timeout
);
488 rc
= regmap_bulk_read(data
->regmap
, DPS310_PRS_BASE
, val
, sizeof(val
));
492 raw
= (val
[0] << 16) | (val
[1] << 8) | val
[2];
493 data
->pressure_raw
= sign_extend32(raw
, 23);
496 mutex_unlock(&data
->lock
);
500 /* Called with lock held */
501 static int dps310_read_temp_ready(struct dps310_data
*data
)
507 rc
= regmap_bulk_read(data
->regmap
, DPS310_TMP_BASE
, val
, sizeof(val
));
511 raw
= (val
[0] << 16) | (val
[1] << 8) | val
[2];
512 data
->temp_raw
= sign_extend32(raw
, 23);
517 static int dps310_read_temp_raw(struct dps310_data
*data
)
523 if (mutex_lock_interruptible(&data
->lock
))
526 rc
= dps310_get_temp_samp_freq(data
, &rate
);
530 timeout
= DPS310_POLL_TIMEOUT_US(rate
);
532 /* Poll for sensor readiness; base the timeout upon the sample rate. */
533 rc
= dps310_ready(data
, DPS310_TMP_RDY
, timeout
);
537 rc
= dps310_read_temp_ready(data
);
540 mutex_unlock(&data
->lock
);
544 static bool dps310_is_writeable_reg(struct device
*dev
, unsigned int reg
)
549 case DPS310_MEAS_CFG
:
552 /* No documentation available on the registers below */
562 static bool dps310_is_volatile_reg(struct device
*dev
, unsigned int reg
)
571 case DPS310_MEAS_CFG
:
572 case 0x32: /* No documentation available on this register */
579 static int dps310_write_raw(struct iio_dev
*iio
,
580 struct iio_chan_spec
const *chan
, int val
,
584 struct dps310_data
*data
= iio_priv(iio
);
586 if (mutex_lock_interruptible(&data
->lock
))
590 case IIO_CHAN_INFO_SAMP_FREQ
:
591 switch (chan
->type
) {
593 rc
= dps310_set_pres_samp_freq(data
, val
);
597 rc
= dps310_set_temp_samp_freq(data
, val
);
606 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
607 switch (chan
->type
) {
609 rc
= dps310_set_pres_precision(data
, val
);
613 rc
= dps310_set_temp_precision(data
, val
);
627 mutex_unlock(&data
->lock
);
631 static int dps310_calculate_pressure(struct dps310_data
*data
, int *val
)
648 rc
= dps310_get_pres_k(data
, &kpi
);
652 rc
= dps310_get_temp_k(data
, &kti
);
659 /* Refresh temp if it's ready, otherwise just use the latest value */
660 if (mutex_trylock(&data
->lock
)) {
661 rc
= regmap_read(data
->regmap
, DPS310_MEAS_CFG
, &t_ready
);
662 if (rc
>= 0 && t_ready
& DPS310_TMP_RDY
)
663 dps310_read_temp_ready(data
);
665 mutex_unlock(&data
->lock
);
668 p
= (s64
)data
->pressure_raw
;
669 t
= (s64
)data
->temp_raw
;
671 /* Section 4.9.1 of the DPS310 spec; algebra'd to avoid underflow */
672 nums
[0] = (s64
)data
->c00
;
674 nums
[1] = p
* (s64
)data
->c10
;
676 nums
[2] = p
* p
* (s64
)data
->c20
;
678 nums
[3] = p
* p
* p
* (s64
)data
->c30
;
679 denoms
[3] = kp
* kp
* kp
;
680 nums
[4] = t
* (s64
)data
->c01
;
682 nums
[5] = t
* p
* (s64
)data
->c11
;
684 nums
[6] = t
* p
* p
* (s64
)data
->c21
;
685 denoms
[6] = kp
* kp
* kt
;
687 /* Kernel lacks a div64_s64_rem function; denoms are all positive */
688 for (i
= 0; i
< 7; ++i
) {
692 pressure
-= div64_u64_rem(-nums
[i
], denoms
[i
], &irem
);
695 pressure
+= div64_u64_rem(nums
[i
], denoms
[i
], &irem
);
700 /* Increase precision and calculate the remainder sum */
701 for (i
= 0; i
< 7; ++i
)
702 rem
+= div64_s64((s64
)rems
[i
] * 1000000000LL, denoms
[i
]);
704 pressure
+= div_s64(rem
, 1000000000LL);
708 *val
= (int)min_t(s64
, pressure
, INT_MAX
);
713 static int dps310_read_pressure(struct dps310_data
*data
, int *val
, int *val2
,
719 case IIO_CHAN_INFO_SAMP_FREQ
:
720 rc
= dps310_get_pres_samp_freq(data
, val
);
726 case IIO_CHAN_INFO_PROCESSED
:
727 rc
= dps310_read_pres_raw(data
);
731 rc
= dps310_calculate_pressure(data
, val
);
735 *val2
= 1000; /* Convert Pa to KPa per IIO ABI */
736 return IIO_VAL_FRACTIONAL
;
738 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
739 rc
= dps310_get_pres_precision(data
, val
);
749 static int dps310_calculate_temp(struct dps310_data
*data
, int *val
)
755 rc
= dps310_get_temp_k(data
, &kt
);
759 /* Obtain inverse-scaled offset */
760 c0
= div_s64((s64
)kt
* (s64
)data
->c0
, 2);
762 /* Add the offset to the unscaled temperature */
763 t
= c0
+ ((s64
)data
->temp_raw
* (s64
)data
->c1
);
765 /* Convert to milliCelsius and scale the temperature */
766 *val
= (int)div_s64(t
* 1000LL, kt
);
771 static int dps310_read_temp(struct dps310_data
*data
, int *val
, int *val2
,
777 case IIO_CHAN_INFO_SAMP_FREQ
:
778 rc
= dps310_get_temp_samp_freq(data
, val
);
784 case IIO_CHAN_INFO_PROCESSED
:
785 rc
= dps310_read_temp_raw(data
);
789 rc
= dps310_calculate_temp(data
, val
);
795 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
796 rc
= dps310_get_temp_precision(data
, val
);
807 static int dps310_read_raw(struct iio_dev
*iio
,
808 struct iio_chan_spec
const *chan
,
809 int *val
, int *val2
, long mask
)
811 struct dps310_data
*data
= iio_priv(iio
);
813 switch (chan
->type
) {
815 return dps310_read_pressure(data
, val
, val2
, mask
);
818 return dps310_read_temp(data
, val
, val2
, mask
);
825 static void dps310_reset(void *action_data
)
827 struct dps310_data
*data
= action_data
;
829 dps310_reset_wait(data
);
832 static const struct regmap_config dps310_regmap_config
= {
835 .writeable_reg
= dps310_is_writeable_reg
,
836 .volatile_reg
= dps310_is_volatile_reg
,
837 .cache_type
= REGCACHE_RBTREE
,
838 .max_register
= 0x62, /* No documentation available on this register */
841 static const struct iio_info dps310_info
= {
842 .read_raw
= dps310_read_raw
,
843 .write_raw
= dps310_write_raw
,
846 static int dps310_probe(struct i2c_client
*client
)
848 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
849 struct dps310_data
*data
;
853 iio
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
857 data
= iio_priv(iio
);
858 data
->client
= client
;
859 mutex_init(&data
->lock
);
861 iio
->name
= id
->name
;
862 iio
->channels
= dps310_channels
;
863 iio
->num_channels
= ARRAY_SIZE(dps310_channels
);
864 iio
->info
= &dps310_info
;
865 iio
->modes
= INDIO_DIRECT_MODE
;
867 data
->regmap
= devm_regmap_init_i2c(client
, &dps310_regmap_config
);
868 if (IS_ERR(data
->regmap
))
869 return PTR_ERR(data
->regmap
);
871 /* Register to run the device reset when the device is removed */
872 rc
= devm_add_action_or_reset(&client
->dev
, dps310_reset
, data
);
876 rc
= dps310_startup(data
);
880 rc
= devm_iio_device_register(&client
->dev
, iio
);
884 i2c_set_clientdata(client
, iio
);
889 static const struct i2c_device_id dps310_id
[] = {
893 MODULE_DEVICE_TABLE(i2c
, dps310_id
);
895 static const struct acpi_device_id dps310_acpi_match
[] = {
899 MODULE_DEVICE_TABLE(acpi
, dps310_acpi_match
);
901 static struct i2c_driver dps310_driver
= {
903 .name
= DPS310_DEV_NAME
,
904 .acpi_match_table
= dps310_acpi_match
,
906 .probe
= dps310_probe
,
907 .id_table
= dps310_id
,
909 module_i2c_driver(dps310_driver
);
911 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
912 MODULE_DESCRIPTION("Infineon DPS310 pressure and temperature sensor");
913 MODULE_LICENSE("GPL v2");