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 <= 20ms for usleep_range */
61 #define DPS310_POLL_SLEEP_US(t) min(20000, (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)
264 rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
268 return BIT(val & GENMASK(2, 0));
271 static int dps310_get_temp_precision(struct dps310_data *data)
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 return BIT(val & GENMASK(2, 0));
287 /* Called with lock held */
288 static int dps310_set_pres_precision(struct dps310_data *data, int val)
293 if (val < 0 || val > 128)
296 shift_en = val >= 16 ? DPS310_PRS_SHIFT_EN : 0;
297 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
298 DPS310_PRS_SHIFT_EN, shift_en);
302 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
303 DPS310_PRS_PRC_BITS, ilog2(val));
306 /* Called with lock held */
307 static int dps310_set_temp_precision(struct dps310_data *data, int val)
312 if (val < 0 || val > 128)
315 shift_en = val >= 16 ? DPS310_TMP_SHIFT_EN : 0;
316 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
317 DPS310_TMP_SHIFT_EN, shift_en);
321 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
322 DPS310_TMP_PRC_BITS, ilog2(val));
325 /* Called with lock held */
326 static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
330 if (freq < 0 || freq > 128)
333 val = ilog2(freq) << 4;
335 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
336 DPS310_PRS_RATE_BITS, val);
339 /* Called with lock held */
340 static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
344 if (freq < 0 || freq > 128)
347 val = ilog2(freq) << 4;
349 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
350 DPS310_TMP_RATE_BITS, val);
353 static int dps310_get_pres_samp_freq(struct dps310_data *data)
358 rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
362 return BIT((val & DPS310_PRS_RATE_BITS) >> 4);
365 static int dps310_get_temp_samp_freq(struct dps310_data *data)
370 rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val);
374 return BIT((val & DPS310_TMP_RATE_BITS) >> 4);
377 static int dps310_get_pres_k(struct dps310_data *data)
379 int rc = dps310_get_pres_precision(data);
384 return scale_factors[ilog2(rc)];
387 static int dps310_get_temp_k(struct dps310_data *data)
389 int rc = dps310_get_temp_precision(data);
394 return scale_factors[ilog2(rc)];
397 static int dps310_reset_wait(struct dps310_data *data)
401 rc = regmap_write(data->regmap, DPS310_RESET, DPS310_RESET_MAGIC);
405 /* Wait for device chip access: 2.5ms in specification */
406 usleep_range(2500, 12000);
410 static int dps310_reset_reinit(struct dps310_data *data)
414 rc = dps310_reset_wait(data);
418 return dps310_startup(data);
421 static int dps310_ready_status(struct dps310_data *data, int ready_bit, int timeout)
423 int sleep = DPS310_POLL_SLEEP_US(timeout);
426 return regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready, ready & ready_bit,
430 static int dps310_ready(struct dps310_data *data, int ready_bit, int timeout)
434 rc = dps310_ready_status(data, ready_bit, timeout);
436 if (rc == -ETIMEDOUT && !data->timeout_recovery_failed) {
437 /* Reset and reinitialize the chip. */
438 if (dps310_reset_reinit(data)) {
439 data->timeout_recovery_failed = true;
441 /* Try again to get sensor ready status. */
442 if (dps310_ready_status(data, ready_bit, timeout))
443 data->timeout_recovery_failed = true;
452 data->timeout_recovery_failed = false;
456 static int dps310_read_pres_raw(struct dps310_data *data)
464 if (mutex_lock_interruptible(&data->lock))
467 rate = dps310_get_pres_samp_freq(data);
468 timeout = DPS310_POLL_TIMEOUT_US(rate);
470 /* Poll for sensor readiness; base the timeout upon the sample rate. */
471 rc = dps310_ready(data, DPS310_PRS_RDY, timeout);
475 rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
479 raw = (val[0] << 16) | (val[1] << 8) | val[2];
480 data->pressure_raw = sign_extend32(raw, 23);
483 mutex_unlock(&data->lock);
487 /* Called with lock held */
488 static int dps310_read_temp_ready(struct dps310_data *data)
494 rc = regmap_bulk_read(data->regmap, DPS310_TMP_BASE, val, sizeof(val));
498 raw = (val[0] << 16) | (val[1] << 8) | val[2];
499 data->temp_raw = sign_extend32(raw, 23);
504 static int dps310_read_temp_raw(struct dps310_data *data)
510 if (mutex_lock_interruptible(&data->lock))
513 rate = dps310_get_temp_samp_freq(data);
514 timeout = DPS310_POLL_TIMEOUT_US(rate);
516 /* Poll for sensor readiness; base the timeout upon the sample rate. */
517 rc = dps310_ready(data, DPS310_TMP_RDY, timeout);
521 rc = dps310_read_temp_ready(data);
524 mutex_unlock(&data->lock);
528 static bool dps310_is_writeable_reg(struct device *dev, unsigned int reg)
533 case DPS310_MEAS_CFG:
536 /* No documentation available on the registers below */
546 static bool dps310_is_volatile_reg(struct device *dev, unsigned int reg)
555 case DPS310_MEAS_CFG:
556 case 0x32: /* No documentation available on this register */
563 static int dps310_write_raw(struct iio_dev *iio,
564 struct iio_chan_spec const *chan, int val,
568 struct dps310_data *data = iio_priv(iio);
570 if (mutex_lock_interruptible(&data->lock))
574 case IIO_CHAN_INFO_SAMP_FREQ:
575 switch (chan->type) {
577 rc = dps310_set_pres_samp_freq(data, val);
581 rc = dps310_set_temp_samp_freq(data, val);
590 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
591 switch (chan->type) {
593 rc = dps310_set_pres_precision(data, val);
597 rc = dps310_set_temp_precision(data, val);
611 mutex_unlock(&data->lock);
615 static int dps310_calculate_pressure(struct dps310_data *data)
620 int kpi = dps310_get_pres_k(data);
621 int kti = dps310_get_temp_k(data);
641 /* Refresh temp if it's ready, otherwise just use the latest value */
642 if (mutex_trylock(&data->lock)) {
643 rc = regmap_read(data->regmap, DPS310_MEAS_CFG, &t_ready);
644 if (rc >= 0 && t_ready & DPS310_TMP_RDY)
645 dps310_read_temp_ready(data);
647 mutex_unlock(&data->lock);
650 p = (s64)data->pressure_raw;
651 t = (s64)data->temp_raw;
653 /* Section 4.9.1 of the DPS310 spec; algebra'd to avoid underflow */
654 nums[0] = (s64)data->c00;
656 nums[1] = p * (s64)data->c10;
658 nums[2] = p * p * (s64)data->c20;
660 nums[3] = p * p * p * (s64)data->c30;
661 denoms[3] = kp * kp * kp;
662 nums[4] = t * (s64)data->c01;
664 nums[5] = t * p * (s64)data->c11;
666 nums[6] = t * p * p * (s64)data->c21;
667 denoms[6] = kp * kp * kt;
669 /* Kernel lacks a div64_s64_rem function; denoms are all positive */
670 for (i = 0; i < 7; ++i) {
674 pressure -= div64_u64_rem(-nums[i], denoms[i], &irem);
677 pressure += div64_u64_rem(nums[i], denoms[i], &irem);
682 /* Increase precision and calculate the remainder sum */
683 for (i = 0; i < 7; ++i)
684 rem += div64_s64((s64)rems[i] * 1000000000LL, denoms[i]);
686 pressure += div_s64(rem, 1000000000LL);
690 return (int)min_t(s64, pressure, INT_MAX);
693 static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
699 case IIO_CHAN_INFO_SAMP_FREQ:
700 rc = dps310_get_pres_samp_freq(data);
707 case IIO_CHAN_INFO_PROCESSED:
708 rc = dps310_read_pres_raw(data);
712 rc = dps310_calculate_pressure(data);
717 *val2 = 1000; /* Convert Pa to KPa per IIO ABI */
718 return IIO_VAL_FRACTIONAL;
720 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
721 rc = dps310_get_pres_precision(data);
733 static int dps310_calculate_temp(struct dps310_data *data)
737 int kt = dps310_get_temp_k(data);
742 /* Obtain inverse-scaled offset */
743 c0 = div_s64((s64)kt * (s64)data->c0, 2);
745 /* Add the offset to the unscaled temperature */
746 t = c0 + ((s64)data->temp_raw * (s64)data->c1);
748 /* Convert to milliCelsius and scale the temperature */
749 return (int)div_s64(t * 1000LL, kt);
752 static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
758 case IIO_CHAN_INFO_SAMP_FREQ:
759 rc = dps310_get_temp_samp_freq(data);
766 case IIO_CHAN_INFO_PROCESSED:
767 rc = dps310_read_temp_raw(data);
771 rc = dps310_calculate_temp(data);
778 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
779 rc = dps310_get_temp_precision(data);
791 static int dps310_read_raw(struct iio_dev *iio,
792 struct iio_chan_spec const *chan,
793 int *val, int *val2, long mask)
795 struct dps310_data *data = iio_priv(iio);
797 switch (chan->type) {
799 return dps310_read_pressure(data, val, val2, mask);
802 return dps310_read_temp(data, val, val2, mask);
809 static void dps310_reset(void *action_data)
811 struct dps310_data *data = action_data;
813 dps310_reset_wait(data);
816 static const struct regmap_config dps310_regmap_config = {
819 .writeable_reg = dps310_is_writeable_reg,
820 .volatile_reg = dps310_is_volatile_reg,
821 .cache_type = REGCACHE_RBTREE,
822 .max_register = 0x62, /* No documentation available on this register */
825 static const struct iio_info dps310_info = {
826 .read_raw = dps310_read_raw,
827 .write_raw = dps310_write_raw,
830 static int dps310_probe(struct i2c_client *client,
831 const struct i2c_device_id *id)
833 struct dps310_data *data;
837 iio = devm_iio_device_alloc(&client->dev, sizeof(*data));
841 data = iio_priv(iio);
842 data->client = client;
843 mutex_init(&data->lock);
845 iio->name = id->name;
846 iio->channels = dps310_channels;
847 iio->num_channels = ARRAY_SIZE(dps310_channels);
848 iio->info = &dps310_info;
849 iio->modes = INDIO_DIRECT_MODE;
851 data->regmap = devm_regmap_init_i2c(client, &dps310_regmap_config);
852 if (IS_ERR(data->regmap))
853 return PTR_ERR(data->regmap);
855 /* Register to run the device reset when the device is removed */
856 rc = devm_add_action_or_reset(&client->dev, dps310_reset, data);
860 rc = dps310_startup(data);
864 rc = devm_iio_device_register(&client->dev, iio);
868 i2c_set_clientdata(client, iio);
873 static const struct i2c_device_id dps310_id[] = {
874 { DPS310_DEV_NAME, 0 },
877 MODULE_DEVICE_TABLE(i2c, dps310_id);
879 static struct i2c_driver dps310_driver = {
881 .name = DPS310_DEV_NAME,
883 .probe = dps310_probe,
884 .id_table = dps310_id,
886 module_i2c_driver(dps310_driver);
888 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
889 MODULE_DESCRIPTION("Infineon DPS310 pressure and temperature sensor");
890 MODULE_LICENSE("GPL v2");