xref: /openbmc/linux/drivers/iio/pressure/dps310.c (revision 11c416e3)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
3 /*
4  * The DPS310 is a barometric pressure and temperature sensor.
5  * Currently only reading a single temperature is supported by
6  * this driver.
7  *
8  * https://www.infineon.com/dgdl/?fileId=5546d462576f34750157750826c42242
9  *
10  * Temperature calculation:
11  *   c0 * 0.5 + c1 * T_raw / kT °C
12  *
13  * TODO:
14  *  - Optionally support the FIFO
15  */
16 
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>
22 
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 
26 #define DPS310_DEV_NAME		"dps310"
27 
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
59 
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))
64 
65 #define DPS310_PRS_BASE		DPS310_PRS_B0
66 #define DPS310_TMP_BASE		DPS310_TMP_B0
67 
68 /*
69  * These values (defined in the spec) indicate how to scale the raw register
70  * values for each level of precision available.
71  */
72 static const int scale_factors[] = {
73 	 524288,
74 	1572864,
75 	3670016,
76 	7864320,
77 	 253952,
78 	 516096,
79 	1040384,
80 	2088960,
81 };
82 
83 struct dps310_data {
84 	struct i2c_client *client;
85 	struct regmap *regmap;
86 	struct mutex lock;	/* Lock for sequential HW access functions */
87 
88 	s32 c0, c1;
89 	s32 c00, c10, c20, c30, c01, c11, c21;
90 	s32 pressure_raw;
91 	s32 temp_raw;
92 };
93 
94 static const struct iio_chan_spec dps310_channels[] = {
95 	{
96 		.type = IIO_TEMP,
97 		.info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
98 			BIT(IIO_CHAN_INFO_SAMP_FREQ) |
99 			BIT(IIO_CHAN_INFO_PROCESSED),
100 	},
101 	{
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),
106 	},
107 };
108 
109 /* To be called after checking the COEF_RDY bit in MEAS_CFG */
110 static int dps310_get_coefs(struct dps310_data *data)
111 {
112 	int rc;
113 	u8 coef[18];
114 	u32 c0, c1;
115 	u32 c00, c10, c20, c30, c01, c11, c21;
116 
117 	/* Read all sensor calibration coefficients from the COEF registers. */
118 	rc = regmap_bulk_read(data->regmap, DPS310_COEF_BASE, coef,
119 			      sizeof(coef));
120 	if (rc < 0)
121 		return rc;
122 
123 	/*
124 	 * Calculate temperature calibration coefficients c0 and c1. The
125 	 * numbers are 12-bit 2's complement numbers.
126 	 */
127 	c0 = (coef[0] << 4) | (coef[1] >> 4);
128 	data->c0 = sign_extend32(c0, 11);
129 
130 	c1 = ((coef[1] & GENMASK(3, 0)) << 8) | coef[2];
131 	data->c1 = sign_extend32(c1, 11);
132 
133 	/*
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
136 	 * numbers.
137 	 */
138 	c00 = (coef[3] << 12) | (coef[4] << 4) | (coef[5] >> 4);
139 	data->c00 = sign_extend32(c00, 19);
140 
141 	c10 = ((coef[5] & GENMASK(3, 0)) << 16) | (coef[6] << 8) | coef[7];
142 	data->c10 = sign_extend32(c10, 19);
143 
144 	c01 = (coef[8] << 8) | coef[9];
145 	data->c01 = sign_extend32(c01, 15);
146 
147 	c11 = (coef[10] << 8) | coef[11];
148 	data->c11 = sign_extend32(c11, 15);
149 
150 	c20 = (coef[12] << 8) | coef[13];
151 	data->c20 = sign_extend32(c20, 15);
152 
153 	c21 = (coef[14] << 8) | coef[15];
154 	data->c21 = sign_extend32(c21, 15);
155 
156 	c30 = (coef[16] << 8) | coef[17];
157 	data->c30 = sign_extend32(c30, 15);
158 
159 	return 0;
160 }
161 
162 static int dps310_get_pres_precision(struct dps310_data *data)
163 {
164 	int rc;
165 	int val;
166 
167 	rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
168 	if (rc < 0)
169 		return rc;
170 
171 	return BIT(val & GENMASK(2, 0));
172 }
173 
174 static int dps310_get_temp_precision(struct dps310_data *data)
175 {
176 	int rc;
177 	int val;
178 
179 	rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val);
180 	if (rc < 0)
181 		return rc;
182 
183 	/*
184 	 * Scale factor is bottom 4 bits of the register, but 1111 is
185 	 * reserved so just grab bottom three
186 	 */
187 	return BIT(val & GENMASK(2, 0));
188 }
189 
190 /* Called with lock held */
191 static int dps310_set_pres_precision(struct dps310_data *data, int val)
192 {
193 	int rc;
194 	u8 shift_en;
195 
196 	if (val < 0 || val > 128)
197 		return -EINVAL;
198 
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);
202 	if (rc)
203 		return rc;
204 
205 	return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
206 				  DPS310_PRS_PRC_BITS, ilog2(val));
207 }
208 
209 /* Called with lock held */
210 static int dps310_set_temp_precision(struct dps310_data *data, int val)
211 {
212 	int rc;
213 	u8 shift_en;
214 
215 	if (val < 0 || val > 128)
216 		return -EINVAL;
217 
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);
221 	if (rc)
222 		return rc;
223 
224 	return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
225 				  DPS310_TMP_PRC_BITS, ilog2(val));
226 }
227 
228 /* Called with lock held */
229 static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
230 {
231 	u8 val;
232 
233 	if (freq < 0 || freq > 128)
234 		return -EINVAL;
235 
236 	val = ilog2(freq) << 4;
237 
238 	return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
239 				  DPS310_PRS_RATE_BITS, val);
240 }
241 
242 /* Called with lock held */
243 static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
244 {
245 	u8 val;
246 
247 	if (freq < 0 || freq > 128)
248 		return -EINVAL;
249 
250 	val = ilog2(freq) << 4;
251 
252 	return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
253 				  DPS310_TMP_RATE_BITS, val);
254 }
255 
256 static int dps310_get_pres_samp_freq(struct dps310_data *data)
257 {
258 	int rc;
259 	int val;
260 
261 	rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val);
262 	if (rc < 0)
263 		return rc;
264 
265 	return BIT((val & DPS310_PRS_RATE_BITS) >> 4);
266 }
267 
268 static int dps310_get_temp_samp_freq(struct dps310_data *data)
269 {
270 	int rc;
271 	int val;
272 
273 	rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val);
274 	if (rc < 0)
275 		return rc;
276 
277 	return BIT((val & DPS310_TMP_RATE_BITS) >> 4);
278 }
279 
280 static int dps310_get_pres_k(struct dps310_data *data)
281 {
282 	int rc = dps310_get_pres_precision(data);
283 
284 	if (rc < 0)
285 		return rc;
286 
287 	return scale_factors[ilog2(rc)];
288 }
289 
290 static int dps310_get_temp_k(struct dps310_data *data)
291 {
292 	int rc = dps310_get_temp_precision(data);
293 
294 	if (rc < 0)
295 		return rc;
296 
297 	return scale_factors[ilog2(rc)];
298 }
299 
300 static int dps310_read_pres_raw(struct dps310_data *data)
301 {
302 	int rc;
303 	int rate;
304 	int ready;
305 	int timeout;
306 	s32 raw;
307 	u8 val[3];
308 
309 	if (mutex_lock_interruptible(&data->lock))
310 		return -EINTR;
311 
312 	rate = dps310_get_pres_samp_freq(data);
313 	timeout = DPS310_POLL_TIMEOUT_US(rate);
314 
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);
319 	if (rc)
320 		goto done;
321 
322 	rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
323 	if (rc < 0)
324 		goto done;
325 
326 	raw = (val[0] << 16) | (val[1] << 8) | val[2];
327 	data->pressure_raw = sign_extend32(raw, 23);
328 
329 done:
330 	mutex_unlock(&data->lock);
331 	return rc;
332 }
333 
334 /* Called with lock held */
335 static int dps310_read_temp_ready(struct dps310_data *data)
336 {
337 	int rc;
338 	u8 val[3];
339 	s32 raw;
340 
341 	rc = regmap_bulk_read(data->regmap, DPS310_TMP_BASE, val, sizeof(val));
342 	if (rc < 0)
343 		return rc;
344 
345 	raw = (val[0] << 16) | (val[1] << 8) | val[2];
346 	data->temp_raw = sign_extend32(raw, 23);
347 
348 	return 0;
349 }
350 
351 static int dps310_read_temp_raw(struct dps310_data *data)
352 {
353 	int rc;
354 	int rate;
355 	int ready;
356 	int timeout;
357 
358 	if (mutex_lock_interruptible(&data->lock))
359 		return -EINTR;
360 
361 	rate = dps310_get_temp_samp_freq(data);
362 	timeout = DPS310_POLL_TIMEOUT_US(rate);
363 
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);
368 	if (rc < 0)
369 		goto done;
370 
371 	rc = dps310_read_temp_ready(data);
372 
373 done:
374 	mutex_unlock(&data->lock);
375 	return rc;
376 }
377 
378 static bool dps310_is_writeable_reg(struct device *dev, unsigned int reg)
379 {
380 	switch (reg) {
381 	case DPS310_PRS_CFG:
382 	case DPS310_TMP_CFG:
383 	case DPS310_MEAS_CFG:
384 	case DPS310_CFG_REG:
385 	case DPS310_RESET:
386 	/* No documentation available on the registers below */
387 	case 0x0e:
388 	case 0x0f:
389 	case 0x62:
390 		return true;
391 	default:
392 		return false;
393 	}
394 }
395 
396 static bool dps310_is_volatile_reg(struct device *dev, unsigned int reg)
397 {
398 	switch (reg) {
399 	case DPS310_PRS_B0:
400 	case DPS310_PRS_B1:
401 	case DPS310_PRS_B2:
402 	case DPS310_TMP_B0:
403 	case DPS310_TMP_B1:
404 	case DPS310_TMP_B2:
405 	case DPS310_MEAS_CFG:
406 	case 0x32:	/* No documentation available on this register */
407 		return true;
408 	default:
409 		return false;
410 	}
411 }
412 
413 static int dps310_write_raw(struct iio_dev *iio,
414 			    struct iio_chan_spec const *chan, int val,
415 			    int val2, long mask)
416 {
417 	int rc;
418 	struct dps310_data *data = iio_priv(iio);
419 
420 	if (mutex_lock_interruptible(&data->lock))
421 		return -EINTR;
422 
423 	switch (mask) {
424 	case IIO_CHAN_INFO_SAMP_FREQ:
425 		switch (chan->type) {
426 		case IIO_PRESSURE:
427 			rc = dps310_set_pres_samp_freq(data, val);
428 			break;
429 
430 		case IIO_TEMP:
431 			rc = dps310_set_temp_samp_freq(data, val);
432 			break;
433 
434 		default:
435 			rc = -EINVAL;
436 			break;
437 		}
438 		break;
439 
440 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
441 		switch (chan->type) {
442 		case IIO_PRESSURE:
443 			rc = dps310_set_pres_precision(data, val);
444 			break;
445 
446 		case IIO_TEMP:
447 			rc = dps310_set_temp_precision(data, val);
448 			break;
449 
450 		default:
451 			rc = -EINVAL;
452 			break;
453 		}
454 		break;
455 
456 	default:
457 		rc = -EINVAL;
458 		break;
459 	}
460 
461 	mutex_unlock(&data->lock);
462 	return rc;
463 }
464 
465 static int dps310_calculate_pressure(struct dps310_data *data)
466 {
467 	int i;
468 	int rc;
469 	int t_ready;
470 	int kpi = dps310_get_pres_k(data);
471 	int kti = dps310_get_temp_k(data);
472 	s64 rem = 0ULL;
473 	s64 pressure = 0ULL;
474 	s64 p;
475 	s64 t;
476 	s64 denoms[7];
477 	s64 nums[7];
478 	s64 rems[7];
479 	s64 kp;
480 	s64 kt;
481 
482 	if (kpi < 0)
483 		return kpi;
484 
485 	if (kti < 0)
486 		return kti;
487 
488 	kp = (s64)kpi;
489 	kt = (s64)kti;
490 
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);
496 
497 		mutex_unlock(&data->lock);
498 	}
499 
500 	p = (s64)data->pressure_raw;
501 	t = (s64)data->temp_raw;
502 
503 	/* Section 4.9.1 of the DPS310 spec; algebra'd to avoid underflow */
504 	nums[0] = (s64)data->c00;
505 	denoms[0] = 1LL;
506 	nums[1] = p * (s64)data->c10;
507 	denoms[1] = kp;
508 	nums[2] = p * p * (s64)data->c20;
509 	denoms[2] = kp * kp;
510 	nums[3] = p * p * p * (s64)data->c30;
511 	denoms[3] = kp * kp * kp;
512 	nums[4] = t * (s64)data->c01;
513 	denoms[4] = kt;
514 	nums[5] = t * p * (s64)data->c11;
515 	denoms[5] = kp * kt;
516 	nums[6] = t * p * p * (s64)data->c21;
517 	denoms[6] = kp * kp * kt;
518 
519 	/* Kernel lacks a div64_s64_rem function; denoms are all positive */
520 	for (i = 0; i < 7; ++i) {
521 		u64 irem;
522 
523 		if (nums[i] < 0LL) {
524 			pressure -= div64_u64_rem(-nums[i], denoms[i], &irem);
525 			rems[i] = -irem;
526 		} else {
527 			pressure += div64_u64_rem(nums[i], denoms[i], &irem);
528 			rems[i] = (s64)irem;
529 		}
530 	}
531 
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]);
535 
536 	pressure += div_s64(rem, 1000000000LL);
537 	if (pressure < 0LL)
538 		return -ERANGE;
539 
540 	return (int)min_t(s64, pressure, INT_MAX);
541 }
542 
543 static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
544 				long mask)
545 {
546 	int rc;
547 
548 	switch (mask) {
549 	case IIO_CHAN_INFO_SAMP_FREQ:
550 		rc = dps310_get_pres_samp_freq(data);
551 		if (rc < 0)
552 			return rc;
553 
554 		*val = rc;
555 		return IIO_VAL_INT;
556 
557 	case IIO_CHAN_INFO_PROCESSED:
558 		rc = dps310_read_pres_raw(data);
559 		if (rc)
560 			return rc;
561 
562 		rc = dps310_calculate_pressure(data);
563 		if (rc < 0)
564 			return rc;
565 
566 		*val = rc;
567 		*val2 = 1000; /* Convert Pa to KPa per IIO ABI */
568 		return IIO_VAL_FRACTIONAL;
569 
570 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
571 		rc = dps310_get_pres_precision(data);
572 		if (rc < 0)
573 			return rc;
574 
575 		*val = rc;
576 		return IIO_VAL_INT;
577 
578 	default:
579 		return -EINVAL;
580 	}
581 }
582 
583 static int dps310_calculate_temp(struct dps310_data *data)
584 {
585 	s64 c0;
586 	s64 t;
587 	int kt = dps310_get_temp_k(data);
588 
589 	if (kt < 0)
590 		return kt;
591 
592 	/* Obtain inverse-scaled offset */
593 	c0 = div_s64((s64)kt * (s64)data->c0, 2);
594 
595 	/* Add the offset to the unscaled temperature */
596 	t = c0 + ((s64)data->temp_raw * (s64)data->c1);
597 
598 	/* Convert to milliCelsius and scale the temperature */
599 	return (int)div_s64(t * 1000LL, kt);
600 }
601 
602 static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
603 			    long mask)
604 {
605 	int rc;
606 
607 	switch (mask) {
608 	case IIO_CHAN_INFO_SAMP_FREQ:
609 		rc = dps310_get_temp_samp_freq(data);
610 		if (rc < 0)
611 			return rc;
612 
613 		*val = rc;
614 		return IIO_VAL_INT;
615 
616 	case IIO_CHAN_INFO_PROCESSED:
617 		rc = dps310_read_temp_raw(data);
618 		if (rc)
619 			return rc;
620 
621 		rc = dps310_calculate_temp(data);
622 		if (rc < 0)
623 			return rc;
624 
625 		*val = rc;
626 		return IIO_VAL_INT;
627 
628 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
629 		rc = dps310_get_temp_precision(data);
630 		if (rc < 0)
631 			return rc;
632 
633 		*val = rc;
634 		return IIO_VAL_INT;
635 
636 	default:
637 		return -EINVAL;
638 	}
639 }
640 
641 static int dps310_read_raw(struct iio_dev *iio,
642 			   struct iio_chan_spec const *chan,
643 			   int *val, int *val2, long mask)
644 {
645 	struct dps310_data *data = iio_priv(iio);
646 
647 	switch (chan->type) {
648 	case IIO_PRESSURE:
649 		return dps310_read_pressure(data, val, val2, mask);
650 
651 	case IIO_TEMP:
652 		return dps310_read_temp(data, val, val2, mask);
653 
654 	default:
655 		return -EINVAL;
656 	}
657 }
658 
659 static void dps310_reset(void *action_data)
660 {
661 	struct dps310_data *data = action_data;
662 
663 	regmap_write(data->regmap, DPS310_RESET, DPS310_RESET_MAGIC);
664 }
665 
666 static const struct regmap_config dps310_regmap_config = {
667 	.reg_bits = 8,
668 	.val_bits = 8,
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 */
673 };
674 
675 static const struct iio_info dps310_info = {
676 	.read_raw = dps310_read_raw,
677 	.write_raw = dps310_write_raw,
678 };
679 
680 /*
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.
684  */
685 static int dps310_temp_workaround(struct dps310_data *data)
686 {
687 	int rc;
688 	int reg;
689 
690 	rc = regmap_read(data->regmap, 0x32, &reg);
691 	if (rc < 0)
692 		return rc;
693 
694 	/*
695 	 * If bit 1 is set then the device is okay, and the workaround does not
696 	 * need to be applied
697 	 */
698 	if (reg & BIT(1))
699 		return 0;
700 
701 	rc = regmap_write(data->regmap, 0x0e, 0xA5);
702 	if (rc < 0)
703 		return rc;
704 
705 	rc = regmap_write(data->regmap, 0x0f, 0x96);
706 	if (rc < 0)
707 		return rc;
708 
709 	rc = regmap_write(data->regmap, 0x62, 0x02);
710 	if (rc < 0)
711 		return rc;
712 
713 	rc = regmap_write(data->regmap, 0x0e, 0x00);
714 	if (rc < 0)
715 		return rc;
716 
717 	return regmap_write(data->regmap, 0x0f, 0x00);
718 }
719 
720 static int dps310_probe(struct i2c_client *client,
721 			const struct i2c_device_id *id)
722 {
723 	struct dps310_data *data;
724 	struct iio_dev *iio;
725 	int rc, ready;
726 
727 	iio = devm_iio_device_alloc(&client->dev,  sizeof(*data));
728 	if (!iio)
729 		return -ENOMEM;
730 
731 	data = iio_priv(iio);
732 	data->client = client;
733 	mutex_init(&data->lock);
734 
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;
740 
741 	data->regmap = devm_regmap_init_i2c(client, &dps310_regmap_config);
742 	if (IS_ERR(data->regmap))
743 		return PTR_ERR(data->regmap);
744 
745 	/* Register to run the device reset when the device is removed */
746 	rc = devm_add_action_or_reset(&client->dev, dps310_reset, data);
747 	if (rc)
748 		return rc;
749 
750 	/*
751 	 * Set up pressure sensor in single sample, one measurement per second
752 	 * mode
753 	 */
754 	rc = regmap_write(data->regmap, DPS310_PRS_CFG, 0);
755 
756 	/*
757 	 * Set up external (MEMS) temperature sensor in single sample, one
758 	 * measurement per second mode
759 	 */
760 	rc = regmap_write(data->regmap, DPS310_TMP_CFG, DPS310_TMP_EXT);
761 	if (rc < 0)
762 		return rc;
763 
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);
767 	if (rc < 0)
768 		return rc;
769 
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);
773 	if (rc < 0)
774 		return rc;
775 
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);
780 	if (rc < 0)
781 		return rc;
782 
783 	/*
784 	 * Calibration coefficients required for reporting temperature.
785 	 * They are available 40ms after the device has started
786 	 */
787 	rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
788 				      ready & DPS310_COEF_RDY, 10000, 40000);
789 	if (rc < 0)
790 		return rc;
791 
792 	rc = dps310_get_coefs(data);
793 	if (rc < 0)
794 		return rc;
795 
796 	rc = dps310_temp_workaround(data);
797 	if (rc < 0)
798 		return rc;
799 
800 	rc = devm_iio_device_register(&client->dev, iio);
801 	if (rc)
802 		return rc;
803 
804 	i2c_set_clientdata(client, iio);
805 
806 	return 0;
807 }
808 
809 static const struct i2c_device_id dps310_id[] = {
810 	{ DPS310_DEV_NAME, 0 },
811 	{}
812 };
813 MODULE_DEVICE_TABLE(i2c, dps310_id);
814 
815 static struct i2c_driver dps310_driver = {
816 	.driver = {
817 		.name = DPS310_DEV_NAME,
818 	},
819 	.probe = dps310_probe,
820 	.id_table = dps310_id,
821 };
822 module_i2c_driver(dps310_driver);
823 
824 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
825 MODULE_DESCRIPTION("Infineon DPS310 pressure and temperature sensor");
826 MODULE_LICENSE("GPL v2");
827