1 // SPDX-License-Identifier: GPL-2.0-only
3 * Digital temperature sensor with integrated Non-volatile memory
6 * Driver for the Texas Instruments TMP117 Temperature Sensor
7 * (7-bit I2C slave address (0x48 - 0x4B), changeable via ADD pins)
9 * Note: This driver assumes that the sensor has been calibrated beforehand.
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>
21 #include <linux/iio/iio.h>
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
34 #define TMP117_RESOLUTION_10UC 78125
35 #define MICRODEGREE_PER_10MILLIDEGREE 10000
37 #define TMP116_DEVICE_ID 0x1116
38 #define TMP117_DEVICE_ID 0x0117
41 struct i2c_client *client;
47 struct iio_chan_spec const *channels;
51 static int tmp117_read_raw(struct iio_dev *indio_dev,
52 struct iio_chan_spec const *channel, int *val,
55 struct tmp117_data *data = iio_priv(indio_dev);
59 case IIO_CHAN_INFO_RAW:
60 ret = i2c_smbus_read_word_swapped(data->client,
64 *val = sign_extend32(ret, 15);
67 case IIO_CHAN_INFO_CALIBBIAS:
68 ret = i2c_smbus_read_word_swapped(data->client,
69 TMP117_REG_TEMP_OFFSET);
72 *val = sign_extend32(ret, 15);
75 case IIO_CHAN_INFO_SCALE:
77 * Conversion from 10s of uC to mC
78 * as IIO reports temperature in mC
80 *val = TMP117_RESOLUTION_10UC / MICRODEGREE_PER_10MILLIDEGREE;
81 *val2 = (TMP117_RESOLUTION_10UC %
82 MICRODEGREE_PER_10MILLIDEGREE) * 100;
84 return IIO_VAL_INT_PLUS_MICRO;
91 static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec
92 const *channel, int val, int val2, long mask)
94 struct tmp117_data *data = iio_priv(indio_dev);
98 case IIO_CHAN_INFO_CALIBBIAS:
99 off = clamp_t(int, val, S16_MIN, S16_MAX);
100 if (off == data->calibbias)
102 data->calibbias = off;
103 return i2c_smbus_write_word_swapped(data->client,
104 TMP117_REG_TEMP_OFFSET, off);
111 static const struct iio_chan_spec tmp117_channels[] = {
114 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
115 BIT(IIO_CHAN_INFO_CALIBBIAS) |
116 BIT(IIO_CHAN_INFO_SCALE),
120 static const struct iio_chan_spec tmp116_channels[] = {
123 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
124 BIT(IIO_CHAN_INFO_SCALE),
128 static const struct tmp11x_info tmp116_channels_info = {
130 .channels = tmp116_channels,
131 .num_channels = ARRAY_SIZE(tmp116_channels)
134 static const struct tmp11x_info tmp117_channels_info = {
136 .channels = tmp117_channels,
137 .num_channels = ARRAY_SIZE(tmp117_channels)
140 static const struct iio_info tmp117_info = {
141 .read_raw = tmp117_read_raw,
142 .write_raw = tmp117_write_raw,
145 static int tmp117_probe(struct i2c_client *client)
147 const struct tmp11x_info *match_data;
148 struct tmp117_data *data;
149 struct iio_dev *indio_dev;
152 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
155 dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID);
160 case TMP116_DEVICE_ID:
161 match_data = &tmp116_channels_info;
163 case TMP117_DEVICE_ID:
164 match_data = &tmp117_channels_info;
167 dev_info(&client->dev,
168 "Unknown device id (0x%x), use fallback compatible\n",
170 match_data = i2c_get_match_data(client);
174 return dev_err_probe(&client->dev, -ENODEV,
175 "Failed to identify unsupported device\n");
177 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
181 data = iio_priv(indio_dev);
182 data->client = client;
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;
192 return devm_iio_device_register(&client->dev, indio_dev);
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 },
200 MODULE_DEVICE_TABLE(of, tmp117_of_match);
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 },
207 MODULE_DEVICE_TABLE(i2c, tmp117_id);
209 static struct i2c_driver tmp117_driver = {
212 .of_match_table = tmp117_of_match,
214 .probe = tmp117_probe,
215 .id_table = tmp117_id,
217 module_i2c_driver(tmp117_driver);
220 MODULE_DESCRIPTION("TI TMP117 Temperature sensor driver");
221 MODULE_LICENSE("GPL");