1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020 Invensense, Inc.
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/device.h>
8 #include <linux/mutex.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/regmap.h>
11 #include <linux/iio/iio.h>
12
13 #include "inv_icm42600.h"
14 #include "inv_icm42600_temp.h"
15
inv_icm42600_temp_read(struct inv_icm42600_state * st,s16 * temp)16 static int inv_icm42600_temp_read(struct inv_icm42600_state *st, s16 *temp)
17 {
18 struct device *dev = regmap_get_device(st->map);
19 __be16 *raw;
20 int ret;
21
22 pm_runtime_get_sync(dev);
23 mutex_lock(&st->lock);
24
25 ret = inv_icm42600_set_temp_conf(st, true, NULL);
26 if (ret)
27 goto exit;
28
29 raw = (__be16 *)&st->buffer[0];
30 ret = regmap_bulk_read(st->map, INV_ICM42600_REG_TEMP_DATA, raw, sizeof(*raw));
31 if (ret)
32 goto exit;
33
34 *temp = (s16)be16_to_cpup(raw);
35 /*
36 * Temperature data is invalid if both accel and gyro are off.
37 * Return -EBUSY in this case.
38 */
39 if (*temp == INV_ICM42600_DATA_INVALID)
40 ret = -EBUSY;
41
42 exit:
43 mutex_unlock(&st->lock);
44 pm_runtime_mark_last_busy(dev);
45 pm_runtime_put_autosuspend(dev);
46
47 return ret;
48 }
49
inv_icm42600_temp_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)50 int inv_icm42600_temp_read_raw(struct iio_dev *indio_dev,
51 struct iio_chan_spec const *chan,
52 int *val, int *val2, long mask)
53 {
54 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
55 s16 temp;
56 int ret;
57
58 if (chan->type != IIO_TEMP)
59 return -EINVAL;
60
61 switch (mask) {
62 case IIO_CHAN_INFO_RAW:
63 ret = iio_device_claim_direct_mode(indio_dev);
64 if (ret)
65 return ret;
66 ret = inv_icm42600_temp_read(st, &temp);
67 iio_device_release_direct_mode(indio_dev);
68 if (ret)
69 return ret;
70 *val = temp;
71 return IIO_VAL_INT;
72 /*
73 * T°C = (temp / 132.48) + 25
74 * Tm°C = 1000 * ((temp / 132.48) + 25)
75 * Tm°C = 7.548309 * temp + 25000
76 * Tm°C = (temp + 3312) * 7.548309
77 * scale: 100000 / 13248 ~= 7.548309
78 * offset: 3312
79 */
80 case IIO_CHAN_INFO_SCALE:
81 *val = 7;
82 *val2 = 548309;
83 return IIO_VAL_INT_PLUS_MICRO;
84 case IIO_CHAN_INFO_OFFSET:
85 *val = 3312;
86 return IIO_VAL_INT;
87 default:
88 return -EINVAL;
89 }
90 }
91