1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Digital temperature sensor with integrated Non-volatile memory 4 * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com> 5 * 6 * Driver for the Texas Instruments TMP117 Temperature Sensor 7 * (7-bit I2C slave address (0x48 - 0x4B), changeable via ADD pins) 8 * 9 * Note: This driver assumes that the sensor has been calibrated beforehand. 10 */ 11 12 #include <linux/err.h> 13 #include <linux/i2c.h> 14 #include <linux/module.h> 15 #include <linux/bitops.h> 16 #include <linux/types.h> 17 #include <linux/kernel.h> 18 #include <linux/limits.h> 19 #include <linux/property.h> 20 21 #include <linux/iio/iio.h> 22 23 #define TMP117_REG_TEMP 0x0 24 #define TMP117_REG_CFGR 0x1 25 #define TMP117_REG_HIGH_LIM 0x2 26 #define TMP117_REG_LOW_LIM 0x3 27 #define TMP117_REG_EEPROM_UL 0x4 28 #define TMP117_REG_EEPROM1 0x5 29 #define TMP117_REG_EEPROM2 0x6 30 #define TMP117_REG_TEMP_OFFSET 0x7 31 #define TMP117_REG_EEPROM3 0x8 32 #define TMP117_REG_DEVICE_ID 0xF 33 34 #define TMP117_RESOLUTION_10UC 78125 35 #define MICRODEGREE_PER_10MILLIDEGREE 10000 36 37 #define TMP116_DEVICE_ID 0x1116 38 #define TMP117_DEVICE_ID 0x0117 39 40 struct tmp117_data { 41 struct i2c_client *client; 42 s16 calibbias; 43 }; 44 45 struct tmp11x_info { 46 const char *name; 47 struct iio_chan_spec const *channels; 48 int num_channels; 49 }; 50 51 static int tmp117_read_raw(struct iio_dev *indio_dev, 52 struct iio_chan_spec const *channel, int *val, 53 int *val2, long mask) 54 { 55 struct tmp117_data *data = iio_priv(indio_dev); 56 s32 ret; 57 58 switch (mask) { 59 case IIO_CHAN_INFO_RAW: 60 ret = i2c_smbus_read_word_swapped(data->client, 61 TMP117_REG_TEMP); 62 if (ret < 0) 63 return ret; 64 *val = sign_extend32(ret, 15); 65 return IIO_VAL_INT; 66 67 case IIO_CHAN_INFO_CALIBBIAS: 68 ret = i2c_smbus_read_word_swapped(data->client, 69 TMP117_REG_TEMP_OFFSET); 70 if (ret < 0) 71 return ret; 72 *val = sign_extend32(ret, 15); 73 return IIO_VAL_INT; 74 75 case IIO_CHAN_INFO_SCALE: 76 /* 77 * Conversion from 10s of uC to mC 78 * as IIO reports temperature in mC 79 */ 80 *val = TMP117_RESOLUTION_10UC / MICRODEGREE_PER_10MILLIDEGREE; 81 *val2 = (TMP117_RESOLUTION_10UC % 82 MICRODEGREE_PER_10MILLIDEGREE) * 100; 83 84 return IIO_VAL_INT_PLUS_MICRO; 85 86 default: 87 return -EINVAL; 88 } 89 } 90 91 static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec 92 const *channel, int val, int val2, long mask) 93 { 94 struct tmp117_data *data = iio_priv(indio_dev); 95 s16 off; 96 97 switch (mask) { 98 case IIO_CHAN_INFO_CALIBBIAS: 99 off = clamp_t(int, val, S16_MIN, S16_MAX); 100 if (off == data->calibbias) 101 return 0; 102 data->calibbias = off; 103 return i2c_smbus_write_word_swapped(data->client, 104 TMP117_REG_TEMP_OFFSET, off); 105 106 default: 107 return -EINVAL; 108 } 109 } 110 111 static const struct iio_chan_spec tmp117_channels[] = { 112 { 113 .type = IIO_TEMP, 114 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 115 BIT(IIO_CHAN_INFO_CALIBBIAS) | 116 BIT(IIO_CHAN_INFO_SCALE), 117 }, 118 }; 119 120 static const struct iio_chan_spec tmp116_channels[] = { 121 { 122 .type = IIO_TEMP, 123 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 124 BIT(IIO_CHAN_INFO_SCALE), 125 }, 126 }; 127 128 static const struct tmp11x_info tmp116_channels_info = { 129 .name = "tmp116", 130 .channels = tmp116_channels, 131 .num_channels = ARRAY_SIZE(tmp116_channels) 132 }; 133 134 static const struct tmp11x_info tmp117_channels_info = { 135 .name = "tmp117", 136 .channels = tmp117_channels, 137 .num_channels = ARRAY_SIZE(tmp117_channels) 138 }; 139 140 static const struct iio_info tmp117_info = { 141 .read_raw = tmp117_read_raw, 142 .write_raw = tmp117_write_raw, 143 }; 144 145 static int tmp117_probe(struct i2c_client *client) 146 { 147 const struct tmp11x_info *match_data; 148 struct tmp117_data *data; 149 struct iio_dev *indio_dev; 150 int dev_id; 151 152 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 153 return -EOPNOTSUPP; 154 155 dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID); 156 if (dev_id < 0) 157 return dev_id; 158 159 switch (dev_id) { 160 case TMP116_DEVICE_ID: 161 match_data = &tmp116_channels_info; 162 break; 163 case TMP117_DEVICE_ID: 164 match_data = &tmp117_channels_info; 165 break; 166 default: 167 dev_info(&client->dev, 168 "Unknown device id (0x%x), use fallback compatible\n", 169 dev_id); 170 match_data = i2c_get_match_data(client); 171 } 172 173 if (!match_data) 174 return dev_err_probe(&client->dev, -ENODEV, 175 "Failed to identify unsupported device\n"); 176 177 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 178 if (!indio_dev) 179 return -ENOMEM; 180 181 data = iio_priv(indio_dev); 182 data->client = client; 183 data->calibbias = 0; 184 185 indio_dev->modes = INDIO_DIRECT_MODE; 186 indio_dev->info = &tmp117_info; 187 indio_dev->channels = match_data->channels; 188 indio_dev->num_channels = match_data->num_channels; 189 indio_dev->name = match_data->name; 190 191 192 return devm_iio_device_register(&client->dev, indio_dev); 193 } 194 195 static const struct of_device_id tmp117_of_match[] = { 196 { .compatible = "ti,tmp116", .data = &tmp116_channels_info }, 197 { .compatible = "ti,tmp117", .data = &tmp117_channels_info }, 198 { } 199 }; 200 MODULE_DEVICE_TABLE(of, tmp117_of_match); 201 202 static const struct i2c_device_id tmp117_id[] = { 203 { "tmp116", (kernel_ulong_t)&tmp116_channels_info }, 204 { "tmp117", (kernel_ulong_t)&tmp117_channels_info }, 205 { } 206 }; 207 MODULE_DEVICE_TABLE(i2c, tmp117_id); 208 209 static struct i2c_driver tmp117_driver = { 210 .driver = { 211 .name = "tmp117", 212 .of_match_table = tmp117_of_match, 213 }, 214 .probe = tmp117_probe, 215 .id_table = tmp117_id, 216 }; 217 module_i2c_driver(tmp117_driver); 218 219 MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>"); 220 MODULE_DESCRIPTION("TI TMP117 Temperature sensor driver"); 221 MODULE_LICENSE("GPL"); 222