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);
163 * Some versions of the chip will read temperatures in the ~60C range when
164 * it's actually ~20C. This is the manufacturer recommended workaround
165 * to correct the issue. The registers used below are undocumented.
167 static int dps310_temp_workaround(struct dps310_data *data)
172 rc = regmap_read(data->regmap, 0x32, ®);
177 * If bit 1 is set then the device is okay, and the workaround does not
183 rc = regmap_write(data->regmap, 0x0e, 0xA5);
187 rc = regmap_write(data->regmap, 0x0f, 0x96);
191 rc = regmap_write(data->regmap, 0x62, 0x02);
195 rc = regmap_write(data->regmap, 0x0e, 0x00);
199 return regmap_write(data->regmap, 0x0f, 0x00);
202 static int dps310_startup(struct dps310_data *data)
208 * Set up pressure sensor in single sample, one measurement per second
211 rc = regmap_write(data->regmap, DPS310_PRS_CFG, 0);
216 * Set up external (MEMS) temperature sensor in single sample, one
217 * measurement per second mode
219 rc = regmap_write(data->regmap, DPS310_TMP_CFG, DPS310_TMP_EXT);
223 /* Temp and pressure shifts are disabled when PRC <= 8 */
224 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
225 DPS310_PRS_SHIFT_EN | DPS310_TMP_SHIFT_EN, 0);
229 /* MEAS_CFG doesn't update correctly unless first written with 0 */
230 rc = regmap_write_bits(data->regmap, DPS310_MEAS_CFG,
231 DPS310_MEAS_CTRL_BITS, 0);
235 /* Turn on temperature and pressure measurement in the background */
236 rc = regmap_write_bits(data->regmap, DPS310_MEAS_CFG,
237 DPS310_MEAS_CTRL_BITS, DPS310_PRS_EN |
238 DPS310_TEMP_EN | DPS310_BACKGROUND);
243 * Calibration coefficients required for reporting temperature.
244 * They are available 40ms after the device has started
246 rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
247 ready & DPS310_COEF_RDY, 10000, 40000);
251 rc = dps310_get_coefs(data);
255 return dps310_temp_workaround(data);
258 static int dps310_get_pres_precision(struct dps310_data *data)
263 rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
267 return BIT(val & GENMASK(2, 0));
270 static int dps310_get_temp_precision(struct dps310_data *data)
275 rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val);
280 * Scale factor is bottom 4 bits of the register, but 1111 is
281 * reserved so just grab bottom three
283 return BIT(val & GENMASK(2, 0));
286 /* Called with lock held */
287 static int dps310_set_pres_precision(struct dps310_data *data, int val)
292 if (val < 0 || val > 128)
295 shift_en = val >= 16 ? DPS310_PRS_SHIFT_EN : 0;
296 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
297 DPS310_PRS_SHIFT_EN, shift_en);
301 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
302 DPS310_PRS_PRC_BITS, ilog2(val));
305 /* Called with lock held */
306 static int dps310_set_temp_precision(struct dps310_data *data, int val)
311 if (val < 0 || val > 128)
314 shift_en = val >= 16 ? DPS310_TMP_SHIFT_EN : 0;
315 rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
316 DPS310_TMP_SHIFT_EN, shift_en);
320 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
321 DPS310_TMP_PRC_BITS, ilog2(val));
324 /* Called with lock held */
325 static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
329 if (freq < 0 || freq > 128)
332 val = ilog2(freq) << 4;
334 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
335 DPS310_PRS_RATE_BITS, val);
338 /* Called with lock held */
339 static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
343 if (freq < 0 || freq > 128)
346 val = ilog2(freq) << 4;
348 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
349 DPS310_TMP_RATE_BITS, val);
352 static int dps310_get_pres_samp_freq(struct dps310_data *data)
357 rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
361 return BIT((val & DPS310_PRS_RATE_BITS) >> 4);
364 static int dps310_get_temp_samp_freq(struct dps310_data *data)
369 rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val);
373 return BIT((val & DPS310_TMP_RATE_BITS) >> 4);
376 static int dps310_get_pres_k(struct dps310_data *data)
378 int rc = dps310_get_pres_precision(data);
383 return scale_factors[ilog2(rc)];
386 static int dps310_get_temp_k(struct dps310_data *data)
388 int rc = dps310_get_temp_precision(data);
393 return scale_factors[ilog2(rc)];
396 static int dps310_read_pres_raw(struct dps310_data *data)
405 if (mutex_lock_interruptible(&data->lock))
408 rate = dps310_get_pres_samp_freq(data);
409 timeout = DPS310_POLL_TIMEOUT_US(rate);
411 /* Poll for sensor readiness; base the timeout upon the sample rate. */
412 rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
413 ready & DPS310_PRS_RDY,
414 DPS310_POLL_SLEEP_US(timeout), timeout);
418 rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
422 raw = (val[0] << 16) | (val[1] << 8) | val[2];
423 data->pressure_raw = sign_extend32(raw, 23);
426 mutex_unlock(&data->lock);
430 /* Called with lock held */
431 static int dps310_read_temp_ready(struct dps310_data *data)
437 rc = regmap_bulk_read(data->regmap, DPS310_TMP_BASE, val, sizeof(val));
441 raw = (val[0] << 16) | (val[1] << 8) | val[2];
442 data->temp_raw = sign_extend32(raw, 23);
447 static int dps310_read_temp_raw(struct dps310_data *data)
454 if (mutex_lock_interruptible(&data->lock))
457 rate = dps310_get_temp_samp_freq(data);
458 timeout = DPS310_POLL_TIMEOUT_US(rate);
460 /* Poll for sensor readiness; base the timeout upon the sample rate. */
461 rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
462 ready & DPS310_TMP_RDY,
463 DPS310_POLL_SLEEP_US(timeout), timeout);
467 rc = dps310_read_temp_ready(data);
470 mutex_unlock(&data->lock);
474 static bool dps310_is_writeable_reg(struct device *dev, unsigned int reg)
479 case DPS310_MEAS_CFG:
482 /* No documentation available on the registers below */
492 static bool dps310_is_volatile_reg(struct device *dev, unsigned int reg)
501 case DPS310_MEAS_CFG:
502 case 0x32: /* No documentation available on this register */
509 static int dps310_write_raw(struct iio_dev *iio,
510 struct iio_chan_spec const *chan, int val,
514 struct dps310_data *data = iio_priv(iio);
516 if (mutex_lock_interruptible(&data->lock))
520 case IIO_CHAN_INFO_SAMP_FREQ:
521 switch (chan->type) {
523 rc = dps310_set_pres_samp_freq(data, val);
527 rc = dps310_set_temp_samp_freq(data, val);
536 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
537 switch (chan->type) {
539 rc = dps310_set_pres_precision(data, val);
543 rc = dps310_set_temp_precision(data, val);
557 mutex_unlock(&data->lock);
561 static int dps310_calculate_pressure(struct dps310_data *data)
566 int kpi = dps310_get_pres_k(data);
567 int kti = dps310_get_temp_k(data);
587 /* Refresh temp if it's ready, otherwise just use the latest value */
588 if (mutex_trylock(&data->lock)) {
589 rc = regmap_read(data->regmap, DPS310_MEAS_CFG, &t_ready);
590 if (rc >= 0 && t_ready & DPS310_TMP_RDY)
591 dps310_read_temp_ready(data);
593 mutex_unlock(&data->lock);
596 p = (s64)data->pressure_raw;
597 t = (s64)data->temp_raw;
599 /* Section 4.9.1 of the DPS310 spec; algebra'd to avoid underflow */
600 nums[0] = (s64)data->c00;
602 nums[1] = p * (s64)data->c10;
604 nums[2] = p * p * (s64)data->c20;
606 nums[3] = p * p * p * (s64)data->c30;
607 denoms[3] = kp * kp * kp;
608 nums[4] = t * (s64)data->c01;
610 nums[5] = t * p * (s64)data->c11;
612 nums[6] = t * p * p * (s64)data->c21;
613 denoms[6] = kp * kp * kt;
615 /* Kernel lacks a div64_s64_rem function; denoms are all positive */
616 for (i = 0; i < 7; ++i) {
620 pressure -= div64_u64_rem(-nums[i], denoms[i], &irem);
623 pressure += div64_u64_rem(nums[i], denoms[i], &irem);
628 /* Increase precision and calculate the remainder sum */
629 for (i = 0; i < 7; ++i)
630 rem += div64_s64((s64)rems[i] * 1000000000LL, denoms[i]);
632 pressure += div_s64(rem, 1000000000LL);
636 return (int)min_t(s64, pressure, INT_MAX);
639 static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
645 case IIO_CHAN_INFO_SAMP_FREQ:
646 rc = dps310_get_pres_samp_freq(data);
653 case IIO_CHAN_INFO_PROCESSED:
654 rc = dps310_read_pres_raw(data);
658 rc = dps310_calculate_pressure(data);
663 *val2 = 1000; /* Convert Pa to KPa per IIO ABI */
664 return IIO_VAL_FRACTIONAL;
666 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
667 rc = dps310_get_pres_precision(data);
679 static int dps310_calculate_temp(struct dps310_data *data)
683 int kt = dps310_get_temp_k(data);
688 /* Obtain inverse-scaled offset */
689 c0 = div_s64((s64)kt * (s64)data->c0, 2);
691 /* Add the offset to the unscaled temperature */
692 t = c0 + ((s64)data->temp_raw * (s64)data->c1);
694 /* Convert to milliCelsius and scale the temperature */
695 return (int)div_s64(t * 1000LL, kt);
698 static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
704 case IIO_CHAN_INFO_SAMP_FREQ:
705 rc = dps310_get_temp_samp_freq(data);
712 case IIO_CHAN_INFO_PROCESSED:
713 rc = dps310_read_temp_raw(data);
717 rc = dps310_calculate_temp(data);
724 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
725 rc = dps310_get_temp_precision(data);
737 static int dps310_read_raw(struct iio_dev *iio,
738 struct iio_chan_spec const *chan,
739 int *val, int *val2, long mask)
741 struct dps310_data *data = iio_priv(iio);
743 switch (chan->type) {
745 return dps310_read_pressure(data, val, val2, mask);
748 return dps310_read_temp(data, val, val2, mask);
755 static void dps310_reset(void *action_data)
757 struct dps310_data *data = action_data;
759 regmap_write(data->regmap, DPS310_RESET, DPS310_RESET_MAGIC);
762 static const struct regmap_config dps310_regmap_config = {
765 .writeable_reg = dps310_is_writeable_reg,
766 .volatile_reg = dps310_is_volatile_reg,
767 .cache_type = REGCACHE_RBTREE,
768 .max_register = 0x62, /* No documentation available on this register */
771 static const struct iio_info dps310_info = {
772 .read_raw = dps310_read_raw,
773 .write_raw = dps310_write_raw,
776 static int dps310_probe(struct i2c_client *client,
777 const struct i2c_device_id *id)
779 struct dps310_data *data;
783 iio = devm_iio_device_alloc(&client->dev, sizeof(*data));
787 data = iio_priv(iio);
788 data->client = client;
789 mutex_init(&data->lock);
791 iio->name = id->name;
792 iio->channels = dps310_channels;
793 iio->num_channels = ARRAY_SIZE(dps310_channels);
794 iio->info = &dps310_info;
795 iio->modes = INDIO_DIRECT_MODE;
797 data->regmap = devm_regmap_init_i2c(client, &dps310_regmap_config);
798 if (IS_ERR(data->regmap))
799 return PTR_ERR(data->regmap);
801 /* Register to run the device reset when the device is removed */
802 rc = devm_add_action_or_reset(&client->dev, dps310_reset, data);
806 rc = dps310_startup(data);
810 rc = devm_iio_device_register(&client->dev, iio);
814 i2c_set_clientdata(client, iio);
819 static const struct i2c_device_id dps310_id[] = {
820 { DPS310_DEV_NAME, 0 },
823 MODULE_DEVICE_TABLE(i2c, dps310_id);
825 static struct i2c_driver dps310_driver = {
827 .name = DPS310_DEV_NAME,
829 .probe = dps310_probe,
830 .id_table = dps310_id,
832 module_i2c_driver(dps310_driver);
834 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
835 MODULE_DESCRIPTION("Infineon DPS310 pressure and temperature sensor");
836 MODULE_LICENSE("GPL v2");