1 // SPDX-License-Identifier: GPL-2.0-only
3 * Digital temperature sensor with integrated Non-volatile memory
4 * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com>
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;
45 static int tmp117_read_raw(struct iio_dev *indio_dev,
46 struct iio_chan_spec const *channel, int *val,
49 struct tmp117_data *data = iio_priv(indio_dev);
53 case IIO_CHAN_INFO_RAW:
54 ret = i2c_smbus_read_word_swapped(data->client,
58 *val = sign_extend32(ret, 15);
61 case IIO_CHAN_INFO_CALIBBIAS:
62 ret = i2c_smbus_read_word_swapped(data->client,
63 TMP117_REG_TEMP_OFFSET);
66 *val = sign_extend32(ret, 15);
69 case IIO_CHAN_INFO_SCALE:
71 * Conversion from 10s of uC to mC
72 * as IIO reports temperature in mC
74 *val = TMP117_RESOLUTION_10UC / MICRODEGREE_PER_10MILLIDEGREE;
75 *val2 = (TMP117_RESOLUTION_10UC %
76 MICRODEGREE_PER_10MILLIDEGREE) * 100;
78 return IIO_VAL_INT_PLUS_MICRO;
85 static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec
86 const *channel, int val, int val2, long mask)
88 struct tmp117_data *data = iio_priv(indio_dev);
92 case IIO_CHAN_INFO_CALIBBIAS:
93 off = clamp_t(int, val, S16_MIN, S16_MAX);
94 if (off == data->calibbias)
96 data->calibbias = off;
97 return i2c_smbus_write_word_swapped(data->client,
98 TMP117_REG_TEMP_OFFSET, off);
105 static const struct iio_chan_spec tmp117_channels[] = {
108 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
109 BIT(IIO_CHAN_INFO_CALIBBIAS) |
110 BIT(IIO_CHAN_INFO_SCALE),
114 static const struct iio_chan_spec tmp116_channels[] = {
117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
118 BIT(IIO_CHAN_INFO_SCALE),
122 static const struct iio_info tmp117_info = {
123 .read_raw = tmp117_read_raw,
124 .write_raw = tmp117_write_raw,
127 static int tmp117_identify(struct i2c_client *client)
129 const struct i2c_device_id *id;
130 unsigned long match_data;
133 dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID);
138 case TMP116_DEVICE_ID:
139 case TMP117_DEVICE_ID:
143 dev_info(&client->dev, "Unknown device id (0x%x), use fallback compatible\n",
146 match_data = (uintptr_t)device_get_match_data(&client->dev);
150 id = i2c_client_get_device_id(client);
152 return id->driver_data;
154 dev_err(&client->dev, "Failed to identify unsupported device\n");
159 static int tmp117_probe(struct i2c_client *client)
161 struct tmp117_data *data;
162 struct iio_dev *indio_dev;
165 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
168 ret = tmp117_identify(client);
174 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
178 data = iio_priv(indio_dev);
179 data->client = client;
182 indio_dev->modes = INDIO_DIRECT_MODE;
183 indio_dev->info = &tmp117_info;
186 case TMP116_DEVICE_ID:
187 indio_dev->channels = tmp116_channels;
188 indio_dev->num_channels = ARRAY_SIZE(tmp116_channels);
189 indio_dev->name = "tmp116";
191 case TMP117_DEVICE_ID:
192 indio_dev->channels = tmp117_channels;
193 indio_dev->num_channels = ARRAY_SIZE(tmp117_channels);
194 indio_dev->name = "tmp117";
198 return devm_iio_device_register(&client->dev, indio_dev);
201 static const struct of_device_id tmp117_of_match[] = {
202 { .compatible = "ti,tmp116", .data = (void *)TMP116_DEVICE_ID },
203 { .compatible = "ti,tmp117", .data = (void *)TMP117_DEVICE_ID },
206 MODULE_DEVICE_TABLE(of, tmp117_of_match);
208 static const struct i2c_device_id tmp117_id[] = {
209 { "tmp116", TMP116_DEVICE_ID },
210 { "tmp117", TMP117_DEVICE_ID },
213 MODULE_DEVICE_TABLE(i2c, tmp117_id);
215 static struct i2c_driver tmp117_driver = {
218 .of_match_table = tmp117_of_match,
220 .probe_new = tmp117_probe,
221 .id_table = tmp117_id,
223 module_i2c_driver(tmp117_driver);
225 MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>");
226 MODULE_DESCRIPTION("TI TMP117 Temperature sensor driver");
227 MODULE_LICENSE("GPL");