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 <= 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))
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 bool timeout_recovery_failed;
93 };
94
95 static const struct iio_chan_spec dps310_channels[] = {
96 {
97 .type = IIO_TEMP,
98 .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
99 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
100 BIT(IIO_CHAN_INFO_PROCESSED),
101 },
102 {
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),
107 },
108 };
109
110 /* To be called after checking the COEF_RDY bit in MEAS_CFG */
dps310_get_coefs(struct dps310_data * data)111 static int dps310_get_coefs(struct dps310_data *data)
112 {
113 int rc;
114 u8 coef[18];
115 u32 c0, c1;
116 u32 c00, c10, c20, c30, c01, c11, c21;
117
118 /* Read all sensor calibration coefficients from the COEF registers. */
119 rc = regmap_bulk_read(data->regmap, DPS310_COEF_BASE, coef,
120 sizeof(coef));
121 if (rc < 0)
122 return rc;
123
124 /*
125 * Calculate temperature calibration coefficients c0 and c1. The
126 * numbers are 12-bit 2's complement numbers.
127 */
128 c0 = (coef[0] << 4) | (coef[1] >> 4);
129 data->c0 = sign_extend32(c0, 11);
130
131 c1 = ((coef[1] & GENMASK(3, 0)) << 8) | coef[2];
132 data->c1 = sign_extend32(c1, 11);
133
134 /*
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
137 * numbers.
138 */
139 c00 = (coef[3] << 12) | (coef[4] << 4) | (coef[5] >> 4);
140 data->c00 = sign_extend32(c00, 19);
141
142 c10 = ((coef[5] & GENMASK(3, 0)) << 16) | (coef[6] << 8) | coef[7];
143 data->c10 = sign_extend32(c10, 19);
144
145 c01 = (coef[8] << 8) | coef[9];
146 data->c01 = sign_extend32(c01, 15);
147
148 c11 = (coef[10] << 8) | coef[11];
149 data->c11 = sign_extend32(c11, 15);
150
151 c20 = (coef[12] << 8) | coef[13];
152 data->c20 = sign_extend32(c20, 15);
153
154 c21 = (coef[14] << 8) | coef[15];
155 data->c21 = sign_extend32(c21, 15);
156
157 c30 = (coef[16] << 8) | coef[17];
158 data->c30 = sign_extend32(c30, 15);
159
160 return 0;
161 }
162
163 /*
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.
167 */
dps310_temp_workaround(struct dps310_data * data)168 static int dps310_temp_workaround(struct dps310_data *data)
169 {
170 int rc;
171 int reg;
172
173 rc = regmap_read(data->regmap, 0x32, ®);
174 if (rc < 0)
175 return rc;
176
177 /*
178 * If bit 1 is set then the device is okay, and the workaround does not
179 * need to be applied
180 */
181 if (reg & BIT(1))
182 return 0;
183
184 rc = regmap_write(data->regmap, 0x0e, 0xA5);
185 if (rc)
186 return rc;
187
188 rc = regmap_write(data->regmap, 0x0f, 0x96);
189 if (rc)
190 return rc;
191
192 rc = regmap_write(data->regmap, 0x62, 0x02);
193 if (rc)
194 return rc;
195
196 rc = regmap_write(data->regmap, 0x0e, 0x00);
197 if (rc)
198 return rc;
199
200 return regmap_write(data->regmap, 0x0f, 0x00);
201 }
202
dps310_startup(struct dps310_data * data)203 static int dps310_startup(struct dps310_data *data)
204 {
205 int rc;
206 int ready;
207
208 /*
209 * Set up pressure sensor in single sample, one measurement per second
210 * mode
211 */
212 rc = regmap_write(data->regmap, DPS310_PRS_CFG, 0);
213 if (rc)
214 return rc;
215
216 /*
217 * Set up external (MEMS) temperature sensor in single sample, one
218 * measurement per second mode
219 */
220 rc = regmap_write(data->regmap, DPS310_TMP_CFG, DPS310_TMP_EXT);
221 if (rc)
222 return rc;
223
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);
227 if (rc)
228 return rc;
229
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);
233 if (rc)
234 return rc;
235
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);
240 if (rc)
241 return rc;
242
243 /*
244 * Calibration coefficients required for reporting temperature.
245 * They are available 40ms after the device has started
246 */
247 rc = regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready,
248 ready & DPS310_COEF_RDY, 10000, 40000);
249 if (rc)
250 return rc;
251
252 rc = dps310_get_coefs(data);
253 if (rc)
254 return rc;
255
256 return dps310_temp_workaround(data);
257 }
258
dps310_get_pres_precision(struct dps310_data * data,int * val)259 static int dps310_get_pres_precision(struct dps310_data *data, int *val)
260 {
261 int reg_val, rc;
262
263 rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
264 if (rc < 0)
265 return rc;
266
267 *val = BIT(reg_val & GENMASK(2, 0));
268
269 return 0;
270 }
271
dps310_get_temp_precision(struct dps310_data * data,int * val)272 static int dps310_get_temp_precision(struct dps310_data *data, int *val)
273 {
274 int reg_val, rc;
275
276 rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
277 if (rc < 0)
278 return rc;
279
280 /*
281 * Scale factor is bottom 4 bits of the register, but 1111 is
282 * reserved so just grab bottom three
283 */
284 *val = BIT(reg_val & GENMASK(2, 0));
285
286 return 0;
287 }
288
289 /* Called with lock held */
dps310_set_pres_precision(struct dps310_data * data,int val)290 static int dps310_set_pres_precision(struct dps310_data *data, int val)
291 {
292 int rc;
293 u8 shift_en;
294
295 if (val < 0 || val > 128)
296 return -EINVAL;
297
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);
301 if (rc)
302 return rc;
303
304 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
305 DPS310_PRS_PRC_BITS, ilog2(val));
306 }
307
308 /* Called with lock held */
dps310_set_temp_precision(struct dps310_data * data,int val)309 static int dps310_set_temp_precision(struct dps310_data *data, int val)
310 {
311 int rc;
312 u8 shift_en;
313
314 if (val < 0 || val > 128)
315 return -EINVAL;
316
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);
320 if (rc)
321 return rc;
322
323 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
324 DPS310_TMP_PRC_BITS, ilog2(val));
325 }
326
327 /* Called with lock held */
dps310_set_pres_samp_freq(struct dps310_data * data,int freq)328 static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
329 {
330 u8 val;
331
332 if (freq < 0 || freq > 128)
333 return -EINVAL;
334
335 val = ilog2(freq) << 4;
336
337 return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
338 DPS310_PRS_RATE_BITS, val);
339 }
340
341 /* Called with lock held */
dps310_set_temp_samp_freq(struct dps310_data * data,int freq)342 static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
343 {
344 u8 val;
345
346 if (freq < 0 || freq > 128)
347 return -EINVAL;
348
349 val = ilog2(freq) << 4;
350
351 return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
352 DPS310_TMP_RATE_BITS, val);
353 }
354
dps310_get_pres_samp_freq(struct dps310_data * data,int * val)355 static int dps310_get_pres_samp_freq(struct dps310_data *data, int *val)
356 {
357 int reg_val, rc;
358
359 rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
360 if (rc < 0)
361 return rc;
362
363 *val = BIT((reg_val & DPS310_PRS_RATE_BITS) >> 4);
364
365 return 0;
366 }
367
dps310_get_temp_samp_freq(struct dps310_data * data,int * val)368 static int dps310_get_temp_samp_freq(struct dps310_data *data, int *val)
369 {
370 int reg_val, rc;
371
372 rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
373 if (rc < 0)
374 return rc;
375
376 *val = BIT((reg_val & DPS310_TMP_RATE_BITS) >> 4);
377
378 return 0;
379 }
380
dps310_get_pres_k(struct dps310_data * data,int * val)381 static int dps310_get_pres_k(struct dps310_data *data, int *val)
382 {
383 int reg_val, rc;
384
385 rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
386 if (rc < 0)
387 return rc;
388
389 *val = scale_factors[reg_val & GENMASK(2, 0)];
390
391 return 0;
392 }
393
dps310_get_temp_k(struct dps310_data * data,int * val)394 static int dps310_get_temp_k(struct dps310_data *data, int *val)
395 {
396 int reg_val, rc;
397
398 rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
399 if (rc < 0)
400 return rc;
401
402 *val = scale_factors[reg_val & GENMASK(2, 0)];
403
404 return 0;
405 }
406
dps310_reset_wait(struct dps310_data * data)407 static int dps310_reset_wait(struct dps310_data *data)
408 {
409 int rc;
410
411 rc = regmap_write(data->regmap, DPS310_RESET, DPS310_RESET_MAGIC);
412 if (rc)
413 return rc;
414
415 /* Wait for device chip access: 15ms in specification */
416 usleep_range(15000, 55000);
417 return 0;
418 }
419
dps310_reset_reinit(struct dps310_data * data)420 static int dps310_reset_reinit(struct dps310_data *data)
421 {
422 int rc;
423
424 rc = dps310_reset_wait(data);
425 if (rc)
426 return rc;
427
428 return dps310_startup(data);
429 }
430
dps310_ready_status(struct dps310_data * data,int ready_bit,int timeout)431 static int dps310_ready_status(struct dps310_data *data, int ready_bit, int timeout)
432 {
433 int sleep = DPS310_POLL_SLEEP_US(timeout);
434 int ready;
435
436 return regmap_read_poll_timeout(data->regmap, DPS310_MEAS_CFG, ready, ready & ready_bit,
437 sleep, timeout);
438 }
439
dps310_ready(struct dps310_data * data,int ready_bit,int timeout)440 static int dps310_ready(struct dps310_data *data, int ready_bit, int timeout)
441 {
442 int rc;
443
444 rc = dps310_ready_status(data, ready_bit, timeout);
445 if (rc) {
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;
450 } else {
451 /* Try again to get sensor ready status. */
452 if (dps310_ready_status(data, ready_bit, timeout))
453 data->timeout_recovery_failed = true;
454 else
455 return 0;
456 }
457 }
458
459 return rc;
460 }
461
462 data->timeout_recovery_failed = false;
463 return 0;
464 }
465
dps310_read_pres_raw(struct dps310_data * data)466 static int dps310_read_pres_raw(struct dps310_data *data)
467 {
468 int rc;
469 int rate;
470 int timeout;
471 s32 raw;
472 u8 val[3];
473
474 if (mutex_lock_interruptible(&data->lock))
475 return -EINTR;
476
477 rc = dps310_get_pres_samp_freq(data, &rate);
478 if (rc)
479 goto done;
480
481 timeout = DPS310_POLL_TIMEOUT_US(rate);
482
483 /* Poll for sensor readiness; base the timeout upon the sample rate. */
484 rc = dps310_ready(data, DPS310_PRS_RDY, timeout);
485 if (rc)
486 goto done;
487
488 rc = regmap_bulk_read(data->regmap, DPS310_PRS_BASE, val, sizeof(val));
489 if (rc < 0)
490 goto done;
491
492 raw = (val[0] << 16) | (val[1] << 8) | val[2];
493 data->pressure_raw = sign_extend32(raw, 23);
494
495 done:
496 mutex_unlock(&data->lock);
497 return rc;
498 }
499
500 /* Called with lock held */
dps310_read_temp_ready(struct dps310_data * data)501 static int dps310_read_temp_ready(struct dps310_data *data)
502 {
503 int rc;
504 u8 val[3];
505 s32 raw;
506
507 rc = regmap_bulk_read(data->regmap, DPS310_TMP_BASE, val, sizeof(val));
508 if (rc < 0)
509 return rc;
510
511 raw = (val[0] << 16) | (val[1] << 8) | val[2];
512 data->temp_raw = sign_extend32(raw, 23);
513
514 return 0;
515 }
516
dps310_read_temp_raw(struct dps310_data * data)517 static int dps310_read_temp_raw(struct dps310_data *data)
518 {
519 int rc;
520 int rate;
521 int timeout;
522
523 if (mutex_lock_interruptible(&data->lock))
524 return -EINTR;
525
526 rc = dps310_get_temp_samp_freq(data, &rate);
527 if (rc)
528 goto done;
529
530 timeout = DPS310_POLL_TIMEOUT_US(rate);
531
532 /* Poll for sensor readiness; base the timeout upon the sample rate. */
533 rc = dps310_ready(data, DPS310_TMP_RDY, timeout);
534 if (rc)
535 goto done;
536
537 rc = dps310_read_temp_ready(data);
538
539 done:
540 mutex_unlock(&data->lock);
541 return rc;
542 }
543
dps310_is_writeable_reg(struct device * dev,unsigned int reg)544 static bool dps310_is_writeable_reg(struct device *dev, unsigned int reg)
545 {
546 switch (reg) {
547 case DPS310_PRS_CFG:
548 case DPS310_TMP_CFG:
549 case DPS310_MEAS_CFG:
550 case DPS310_CFG_REG:
551 case DPS310_RESET:
552 /* No documentation available on the registers below */
553 case 0x0e:
554 case 0x0f:
555 case 0x62:
556 return true;
557 default:
558 return false;
559 }
560 }
561
dps310_is_volatile_reg(struct device * dev,unsigned int reg)562 static bool dps310_is_volatile_reg(struct device *dev, unsigned int reg)
563 {
564 switch (reg) {
565 case DPS310_PRS_B0:
566 case DPS310_PRS_B1:
567 case DPS310_PRS_B2:
568 case DPS310_TMP_B0:
569 case DPS310_TMP_B1:
570 case DPS310_TMP_B2:
571 case DPS310_MEAS_CFG:
572 case 0x32: /* No documentation available on this register */
573 return true;
574 default:
575 return false;
576 }
577 }
578
dps310_write_raw(struct iio_dev * iio,struct iio_chan_spec const * chan,int val,int val2,long mask)579 static int dps310_write_raw(struct iio_dev *iio,
580 struct iio_chan_spec const *chan, int val,
581 int val2, long mask)
582 {
583 int rc;
584 struct dps310_data *data = iio_priv(iio);
585
586 if (mutex_lock_interruptible(&data->lock))
587 return -EINTR;
588
589 switch (mask) {
590 case IIO_CHAN_INFO_SAMP_FREQ:
591 switch (chan->type) {
592 case IIO_PRESSURE:
593 rc = dps310_set_pres_samp_freq(data, val);
594 break;
595
596 case IIO_TEMP:
597 rc = dps310_set_temp_samp_freq(data, val);
598 break;
599
600 default:
601 rc = -EINVAL;
602 break;
603 }
604 break;
605
606 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
607 switch (chan->type) {
608 case IIO_PRESSURE:
609 rc = dps310_set_pres_precision(data, val);
610 break;
611
612 case IIO_TEMP:
613 rc = dps310_set_temp_precision(data, val);
614 break;
615
616 default:
617 rc = -EINVAL;
618 break;
619 }
620 break;
621
622 default:
623 rc = -EINVAL;
624 break;
625 }
626
627 mutex_unlock(&data->lock);
628 return rc;
629 }
630
dps310_calculate_pressure(struct dps310_data * data,int * val)631 static int dps310_calculate_pressure(struct dps310_data *data, int *val)
632 {
633 int i;
634 int rc;
635 int t_ready;
636 int kpi;
637 int kti;
638 s64 rem = 0ULL;
639 s64 pressure = 0ULL;
640 s64 p;
641 s64 t;
642 s64 denoms[7];
643 s64 nums[7];
644 s64 rems[7];
645 s64 kp;
646 s64 kt;
647
648 rc = dps310_get_pres_k(data, &kpi);
649 if (rc)
650 return rc;
651
652 rc = dps310_get_temp_k(data, &kti);
653 if (rc)
654 return rc;
655
656 kp = (s64)kpi;
657 kt = (s64)kti;
658
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);
664
665 mutex_unlock(&data->lock);
666 }
667
668 p = (s64)data->pressure_raw;
669 t = (s64)data->temp_raw;
670
671 /* Section 4.9.1 of the DPS310 spec; algebra'd to avoid underflow */
672 nums[0] = (s64)data->c00;
673 denoms[0] = 1LL;
674 nums[1] = p * (s64)data->c10;
675 denoms[1] = kp;
676 nums[2] = p * p * (s64)data->c20;
677 denoms[2] = kp * kp;
678 nums[3] = p * p * p * (s64)data->c30;
679 denoms[3] = kp * kp * kp;
680 nums[4] = t * (s64)data->c01;
681 denoms[4] = kt;
682 nums[5] = t * p * (s64)data->c11;
683 denoms[5] = kp * kt;
684 nums[6] = t * p * p * (s64)data->c21;
685 denoms[6] = kp * kp * kt;
686
687 /* Kernel lacks a div64_s64_rem function; denoms are all positive */
688 for (i = 0; i < 7; ++i) {
689 u64 irem;
690
691 if (nums[i] < 0LL) {
692 pressure -= div64_u64_rem(-nums[i], denoms[i], &irem);
693 rems[i] = -irem;
694 } else {
695 pressure += div64_u64_rem(nums[i], denoms[i], &irem);
696 rems[i] = (s64)irem;
697 }
698 }
699
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]);
703
704 pressure += div_s64(rem, 1000000000LL);
705 if (pressure < 0LL)
706 return -ERANGE;
707
708 *val = (int)min_t(s64, pressure, INT_MAX);
709
710 return 0;
711 }
712
dps310_read_pressure(struct dps310_data * data,int * val,int * val2,long mask)713 static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
714 long mask)
715 {
716 int rc;
717
718 switch (mask) {
719 case IIO_CHAN_INFO_SAMP_FREQ:
720 rc = dps310_get_pres_samp_freq(data, val);
721 if (rc)
722 return rc;
723
724 return IIO_VAL_INT;
725
726 case IIO_CHAN_INFO_PROCESSED:
727 rc = dps310_read_pres_raw(data);
728 if (rc)
729 return rc;
730
731 rc = dps310_calculate_pressure(data, val);
732 if (rc)
733 return rc;
734
735 *val2 = 1000; /* Convert Pa to KPa per IIO ABI */
736 return IIO_VAL_FRACTIONAL;
737
738 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
739 rc = dps310_get_pres_precision(data, val);
740 if (rc)
741 return rc;
742 return IIO_VAL_INT;
743
744 default:
745 return -EINVAL;
746 }
747 }
748
dps310_calculate_temp(struct dps310_data * data,int * val)749 static int dps310_calculate_temp(struct dps310_data *data, int *val)
750 {
751 s64 c0;
752 s64 t;
753 int kt, rc;
754
755 rc = dps310_get_temp_k(data, &kt);
756 if (rc)
757 return rc;
758
759 /* Obtain inverse-scaled offset */
760 c0 = div_s64((s64)kt * (s64)data->c0, 2);
761
762 /* Add the offset to the unscaled temperature */
763 t = c0 + ((s64)data->temp_raw * (s64)data->c1);
764
765 /* Convert to milliCelsius and scale the temperature */
766 *val = (int)div_s64(t * 1000LL, kt);
767
768 return 0;
769 }
770
dps310_read_temp(struct dps310_data * data,int * val,int * val2,long mask)771 static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
772 long mask)
773 {
774 int rc;
775
776 switch (mask) {
777 case IIO_CHAN_INFO_SAMP_FREQ:
778 rc = dps310_get_temp_samp_freq(data, val);
779 if (rc)
780 return rc;
781
782 return IIO_VAL_INT;
783
784 case IIO_CHAN_INFO_PROCESSED:
785 rc = dps310_read_temp_raw(data);
786 if (rc)
787 return rc;
788
789 rc = dps310_calculate_temp(data, val);
790 if (rc)
791 return rc;
792
793 return IIO_VAL_INT;
794
795 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
796 rc = dps310_get_temp_precision(data, val);
797 if (rc)
798 return rc;
799
800 return IIO_VAL_INT;
801
802 default:
803 return -EINVAL;
804 }
805 }
806
dps310_read_raw(struct iio_dev * iio,struct iio_chan_spec const * chan,int * val,int * val2,long mask)807 static int dps310_read_raw(struct iio_dev *iio,
808 struct iio_chan_spec const *chan,
809 int *val, int *val2, long mask)
810 {
811 struct dps310_data *data = iio_priv(iio);
812
813 switch (chan->type) {
814 case IIO_PRESSURE:
815 return dps310_read_pressure(data, val, val2, mask);
816
817 case IIO_TEMP:
818 return dps310_read_temp(data, val, val2, mask);
819
820 default:
821 return -EINVAL;
822 }
823 }
824
dps310_reset(void * action_data)825 static void dps310_reset(void *action_data)
826 {
827 struct dps310_data *data = action_data;
828
829 dps310_reset_wait(data);
830 }
831
832 static const struct regmap_config dps310_regmap_config = {
833 .reg_bits = 8,
834 .val_bits = 8,
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 */
839 };
840
841 static const struct iio_info dps310_info = {
842 .read_raw = dps310_read_raw,
843 .write_raw = dps310_write_raw,
844 };
845
dps310_probe(struct i2c_client * client)846 static int dps310_probe(struct i2c_client *client)
847 {
848 const struct i2c_device_id *id = i2c_client_get_device_id(client);
849 struct dps310_data *data;
850 struct iio_dev *iio;
851 int rc;
852
853 iio = devm_iio_device_alloc(&client->dev, sizeof(*data));
854 if (!iio)
855 return -ENOMEM;
856
857 data = iio_priv(iio);
858 data->client = client;
859 mutex_init(&data->lock);
860
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;
866
867 data->regmap = devm_regmap_init_i2c(client, &dps310_regmap_config);
868 if (IS_ERR(data->regmap))
869 return PTR_ERR(data->regmap);
870
871 /* Register to run the device reset when the device is removed */
872 rc = devm_add_action_or_reset(&client->dev, dps310_reset, data);
873 if (rc)
874 return rc;
875
876 rc = dps310_startup(data);
877 if (rc)
878 return rc;
879
880 rc = devm_iio_device_register(&client->dev, iio);
881 if (rc)
882 return rc;
883
884 i2c_set_clientdata(client, iio);
885
886 return 0;
887 }
888
889 static const struct i2c_device_id dps310_id[] = {
890 { DPS310_DEV_NAME },
891 {}
892 };
893 MODULE_DEVICE_TABLE(i2c, dps310_id);
894
895 static const struct acpi_device_id dps310_acpi_match[] = {
896 { "IFX3100" },
897 {}
898 };
899 MODULE_DEVICE_TABLE(acpi, dps310_acpi_match);
900
901 static struct i2c_driver dps310_driver = {
902 .driver = {
903 .name = DPS310_DEV_NAME,
904 .acpi_match_table = dps310_acpi_match,
905 },
906 .probe = dps310_probe,
907 .id_table = dps310_id,
908 };
909 module_i2c_driver(dps310_driver);
910
911 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
912 MODULE_DESCRIPTION("Infineon DPS310 pressure and temperature sensor");
913 MODULE_LICENSE("GPL v2");
914