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;
94 static const struct iio_chan_spec dps310_channels[] = {
97 .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
98 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
99 BIT(IIO_CHAN_INFO_PROCESSED),
102 .type = IIO_PRESSURE,
103 .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
104 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
105 BIT(IIO_CHAN_INFO_PROCESSED),
109 /* To be called after checking the COEF_RDY bit in MEAS_CFG */
110 static int dps310_get_coefs(struct dps310_data *data)
115 u32 c00, c10, c20, c30, c01, c11, c21;
117 /* Read all sensor calibration coefficients from the COEF registers. */
118 rc = regmap_bulk_read(data->regmap, DPS310_COEF_BASE, coef,
124 * Calculate temperature calibration coefficients c0 and c1. The
125 * numbers are 12-bit 2's complement numbers.
127 c0 = (coef[0] << 4) | (coef[1] >> 4);
128 data->c0 = sign_extend32(c0, 11);
130 c1 = ((coef[1] & GENMASK(3, 0)) << 8) | coef[2];
131 data->c1 = sign_extend32(c1, 11);
134 * Calculate pressure calibration coefficients. c00 and c10 are 20 bit
135 * 2's complement numbers, while the rest are 16 bit 2's complement
138 c00 = (coef[3] << 12) | (coef[4] << 4) | (coef[5] >> 4);
139 data->c00 = sign_extend32(c00, 19);
141 c10 = ((coef[5] & GENMASK(3, 0)) << 16) | (coef[6] << 8) | coef[7];
142 data->c10 = sign_extend32(c10, 19);
144 c01 = (coef[8] << 8) | coef[9];
145 data->c01 = sign_extend32(c01, 15);
147 c11 = (coef[10] << 8) | coef[11];
148 data->c11 = sign_extend32(c11, 15);
150 c20 = (coef[12] << 8) | coef[13];
151 data->c20 = sign_extend32(c20, 15);
153 c21 = (coef[14] << 8) | coef[15];
154 data->c21 = sign_extend32(c21, 15);
156 c30 = (coef[16] << 8) | coef[17];
157 data->c30 = sign_extend32(c30, 15);
162 static int dps310_get_pres_precision(struct dps310_data *data)
167 rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
171 return BIT(val & GENMASK(2, 0));
174 static int dps310_get_temp_precision(struct dps310_data *data)
179 rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val);
184 * Scale factor is bottom 4 bits of the register, but 1111 is
185 * reserved so just grab bottom three
187 return BIT(val & GENMASK(2, 0));
190 /* Called with lock held */
191 static int dps310_set_pres_precision(struct dps310_data *data, int val)
196 if (val < 0 || val > 128)
199 shift_en = val >= 16 ? DPS310_PRS_SHIFT_EN : 0;
200 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
201 DPS310_PRS_SHIFT_EN, shift_en);
205 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
206 DPS310_PRS_PRC_BITS, ilog2(val));
209 /* Called with lock held */
210 static int dps310_set_temp_precision(struct dps310_data *data, int val)
215 if (val < 0 || val > 128)
218 shift_en = val >= 16 ? DPS310_TMP_SHIFT_EN : 0;
219 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
220 DPS310_TMP_SHIFT_EN, shift_en);
224 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
225 DPS310_TMP_PRC_BITS, ilog2(val));
228 /* Called with lock held */
229 static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
233 if (freq < 0 || freq > 128)
236 val = ilog2(freq) << 4;
238 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
239 DPS310_PRS_RATE_BITS, val);
242 /* Called with lock held */
243 static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
247 if (freq < 0 || freq > 128)
250 val = ilog2(freq) << 4;
252 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
253 DPS310_TMP_RATE_BITS, val);
256 static int dps310_get_pres_samp_freq(struct dps310_data *data)
261 rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
265 return BIT((val & DPS310_PRS_RATE_BITS) >> 4);
268 static int dps310_get_temp_samp_freq(struct dps310_data *data)
273 rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val);
277 return BIT((val & DPS310_TMP_RATE_BITS) >> 4);
280 static int dps310_get_pres_k(struct dps310_data *data)
282 int rc = dps310_get_pres_precision(data);
287 return scale_factors[ilog2(rc)];
290 static int dps310_get_temp_k(struct dps310_data *data)
292 int rc = dps310_get_temp_precision(data);
297 return scale_factors[ilog2(rc)];
300 static int dps310_read_pres_raw(struct dps310_data *data)
309 if (mutex_lock_interruptible(&data->lock))
312 rate = dps310_get_pres_samp_freq(data);
313 timeout = DPS310_POLL_TIMEOUT_US(rate);
315 /* Poll for sensor readiness; base the timeout upon the sample rate. */
316 rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
317 ready & DPS310_PRS_RDY,
318 DPS310_POLL_SLEEP_US(timeout), timeout);
322 rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
326 raw = (val[0] << 16) | (val[1] << 8) | val[2];
327 data->pressure_raw = sign_extend32(raw, 23);
330 mutex_unlock(&data->lock);
334 /* Called with lock held */
335 static int dps310_read_temp_ready(struct dps310_data *data)
341 rc = regmap_bulk_read(data->regmap, DPS310_TMP_BASE, val, sizeof(val));
345 raw = (val[0] << 16) | (val[1] << 8) | val[2];
346 data->temp_raw = sign_extend32(raw, 23);
351 static int dps310_read_temp_raw(struct dps310_data *data)
358 if (mutex_lock_interruptible(&data->lock))
361 rate = dps310_get_temp_samp_freq(data);
362 timeout = DPS310_POLL_TIMEOUT_US(rate);
364 /* Poll for sensor readiness; base the timeout upon the sample rate. */
365 rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
366 ready & DPS310_TMP_RDY,
367 DPS310_POLL_SLEEP_US(timeout), timeout);
371 rc = dps310_read_temp_ready(data);
374 mutex_unlock(&data->lock);
378 static bool dps310_is_writeable_reg(struct device *dev, unsigned int reg)
383 case DPS310_MEAS_CFG:
386 /* No documentation available on the registers below */
396 static bool dps310_is_volatile_reg(struct device *dev, unsigned int reg)
405 case DPS310_MEAS_CFG:
406 case 0x32: /* No documentation available on this register */
413 static int dps310_write_raw(struct iio_dev *iio,
414 struct iio_chan_spec const *chan, int val,
418 struct dps310_data *data = iio_priv(iio);
420 if (mutex_lock_interruptible(&data->lock))
424 case IIO_CHAN_INFO_SAMP_FREQ:
425 switch (chan->type) {
427 rc = dps310_set_pres_samp_freq(data, val);
431 rc = dps310_set_temp_samp_freq(data, val);
440 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
441 switch (chan->type) {
443 rc = dps310_set_pres_precision(data, val);
447 rc = dps310_set_temp_precision(data, val);
461 mutex_unlock(&data->lock);
465 static int dps310_calculate_pressure(struct dps310_data *data)
470 int kpi = dps310_get_pres_k(data);
471 int kti = dps310_get_temp_k(data);
491 /* Refresh temp if it's ready, otherwise just use the latest value */
492 if (mutex_trylock(&data->lock)) {
493 rc = regmap_read(data->regmap, DPS310_MEAS_CFG, &t_ready);
494 if (rc >= 0 && t_ready & DPS310_TMP_RDY)
495 dps310_read_temp_ready(data);
497 mutex_unlock(&data->lock);
500 p = (s64)data->pressure_raw;
501 t = (s64)data->temp_raw;
503 /* Section 4.9.1 of the DPS310 spec; algebra'd to avoid underflow */
504 nums[0] = (s64)data->c00;
506 nums[1] = p * (s64)data->c10;
508 nums[2] = p * p * (s64)data->c20;
510 nums[3] = p * p * p * (s64)data->c30;
511 denoms[3] = kp * kp * kp;
512 nums[4] = t * (s64)data->c01;
514 nums[5] = t * p * (s64)data->c11;
516 nums[6] = t * p * p * (s64)data->c21;
517 denoms[6] = kp * kp * kt;
519 /* Kernel lacks a div64_s64_rem function; denoms are all positive */
520 for (i = 0; i < 7; ++i) {
524 pressure -= div64_u64_rem(-nums[i], denoms[i], &irem);
527 pressure += div64_u64_rem(nums[i], denoms[i], &irem);
532 /* Increase precision and calculate the remainder sum */
533 for (i = 0; i < 7; ++i)
534 rem += div64_s64((s64)rems[i] * 1000000000LL, denoms[i]);
536 pressure += div_s64(rem, 1000000000LL);
540 return (int)min_t(s64, pressure, INT_MAX);
543 static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
549 case IIO_CHAN_INFO_SAMP_FREQ:
550 rc = dps310_get_pres_samp_freq(data);
557 case IIO_CHAN_INFO_PROCESSED:
558 rc = dps310_read_pres_raw(data);
562 rc = dps310_calculate_pressure(data);
567 *val2 = 1000; /* Convert Pa to KPa per IIO ABI */
568 return IIO_VAL_FRACTIONAL;
570 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
571 rc = dps310_get_pres_precision(data);
583 static int dps310_calculate_temp(struct dps310_data *data)
587 int kt = dps310_get_temp_k(data);
592 /* Obtain inverse-scaled offset */
593 c0 = div_s64((s64)kt * (s64)data->c0, 2);
595 /* Add the offset to the unscaled temperature */
596 t = c0 + ((s64)data->temp_raw * (s64)data->c1);
598 /* Convert to milliCelsius and scale the temperature */
599 return (int)div_s64(t * 1000LL, kt);
602 static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
608 case IIO_CHAN_INFO_SAMP_FREQ:
609 rc = dps310_get_temp_samp_freq(data);
616 case IIO_CHAN_INFO_PROCESSED:
617 rc = dps310_read_temp_raw(data);
621 rc = dps310_calculate_temp(data);
628 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
629 rc = dps310_get_temp_precision(data);
641 static int dps310_read_raw(struct iio_dev *iio,
642 struct iio_chan_spec const *chan,
643 int *val, int *val2, long mask)
645 struct dps310_data *data = iio_priv(iio);
647 switch (chan->type) {
649 return dps310_read_pressure(data, val, val2, mask);
652 return dps310_read_temp(data, val, val2, mask);
659 static void dps310_reset(void *action_data)
661 struct dps310_data *data = action_data;
663 regmap_write(data->regmap, DPS310_RESET, DPS310_RESET_MAGIC);
666 static const struct regmap_config dps310_regmap_config = {
669 .writeable_reg = dps310_is_writeable_reg,
670 .volatile_reg = dps310_is_volatile_reg,
671 .cache_type = REGCACHE_RBTREE,
672 .max_register = 0x62, /* No documentation available on this register */
675 static const struct iio_info dps310_info = {
676 .read_raw = dps310_read_raw,
677 .write_raw = dps310_write_raw,
681 * Some verions of chip will read temperatures in the ~60C range when
682 * its actually ~20C. This is the manufacturer recommended workaround
683 * to correct the issue. The registers used below are undocumented.
685 static int dps310_temp_workaround(struct dps310_data *data)
690 rc = regmap_read(data->regmap, 0x32, ®);
695 * If bit 1 is set then the device is okay, and the workaround does not
701 rc = regmap_write(data->regmap, 0x0e, 0xA5);
705 rc = regmap_write(data->regmap, 0x0f, 0x96);
709 rc = regmap_write(data->regmap, 0x62, 0x02);
713 rc = regmap_write(data->regmap, 0x0e, 0x00);
717 return regmap_write(data->regmap, 0x0f, 0x00);
720 static int dps310_probe(struct i2c_client *client,
721 const struct i2c_device_id *id)
723 struct dps310_data *data;
727 iio = devm_iio_device_alloc(&client->dev, sizeof(*data));
731 data = iio_priv(iio);
732 data->client = client;
733 mutex_init(&data->lock);
735 iio->name = id->name;
736 iio->channels = dps310_channels;
737 iio->num_channels = ARRAY_SIZE(dps310_channels);
738 iio->info = &dps310_info;
739 iio->modes = INDIO_DIRECT_MODE;
741 data->regmap = devm_regmap_init_i2c(client, &dps310_regmap_config);
742 if (IS_ERR(data->regmap))
743 return PTR_ERR(data->regmap);
745 /* Register to run the device reset when the device is removed */
746 rc = devm_add_action_or_reset(&client->dev, dps310_reset, data);
751 * Set up pressure sensor in single sample, one measurement per second
754 rc = regmap_write(data->regmap, DPS310_PRS_CFG, 0);
757 * Set up external (MEMS) temperature sensor in single sample, one
758 * measurement per second mode
760 rc = regmap_write(data->regmap, DPS310_TMP_CFG, DPS310_TMP_EXT);
764 /* Temp and pressure shifts are disabled when PRC <= 8 */
765 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
766 DPS310_PRS_SHIFT_EN | DPS310_TMP_SHIFT_EN, 0);
770 /* MEAS_CFG doesn't update correctly unless first written with 0 */
771 rc = regmap_write_bits(data->regmap, DPS310_MEAS_CFG,
772 DPS310_MEAS_CTRL_BITS, 0);
776 /* Turn on temperature and pressure measurement in the background */
777 rc = regmap_write_bits(data->regmap, DPS310_MEAS_CFG,
778 DPS310_MEAS_CTRL_BITS, DPS310_PRS_EN |
779 DPS310_TEMP_EN | DPS310_BACKGROUND);
784 * Calibration coefficients required for reporting temperature.
785 * They are available 40ms after the device has started
787 rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
788 ready & DPS310_COEF_RDY, 10000, 40000);
792 rc = dps310_get_coefs(data);
796 rc = dps310_temp_workaround(data);
800 rc = devm_iio_device_register(&client->dev, iio);
804 i2c_set_clientdata(client, iio);
809 static const struct i2c_device_id dps310_id[] = {
810 { DPS310_DEV_NAME, 0 },
813 MODULE_DEVICE_TABLE(i2c, dps310_id);
815 static struct i2c_driver dps310_driver = {
817 .name = DPS310_DEV_NAME,
819 .probe = dps310_probe,
820 .id_table = dps310_id,
822 module_i2c_driver(dps310_driver);
824 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
825 MODULE_DESCRIPTION("Infineon DPS310 pressure and temperature sensor");
826 MODULE_LICENSE("GPL v2");