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 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 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 if (!iio_device_claim_direct(indio_dev)) 64 return -EBUSY; 65 ret = inv_icm42600_temp_read(st, &temp); 66 iio_device_release_direct(indio_dev); 67 if (ret) 68 return ret; 69 *val = temp; 70 return IIO_VAL_INT; 71 /* 72 * T°C = (temp / 132.48) + 25 73 * Tm°C = 1000 * ((temp / 132.48) + 25) 74 * Tm°C = 7.548309 * temp + 25000 75 * Tm°C = (temp + 3312) * 7.548309 76 * scale: 100000 / 13248 ~= 7.548309 77 * offset: 3312 78 */ 79 case IIO_CHAN_INFO_SCALE: 80 *val = 7; 81 *val2 = 548309; 82 return IIO_VAL_INT_PLUS_MICRO; 83 case IIO_CHAN_INFO_OFFSET: 84 *val = 3312; 85 return IIO_VAL_INT; 86 default: 87 return -EINVAL; 88 } 89 } 90