• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
tmp117_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * val,int * val2,long mask)45 static int tmp117_read_raw(struct iio_dev *indio_dev,
46 			   struct iio_chan_spec const *channel, int *val,
47 			   int *val2, long mask)
48 {
49 	struct tmp117_data *data = iio_priv(indio_dev);
50 	s32 ret;
51 
52 	switch (mask) {
53 	case IIO_CHAN_INFO_RAW:
54 		ret = i2c_smbus_read_word_swapped(data->client,
55 						  TMP117_REG_TEMP);
56 		if (ret < 0)
57 			return ret;
58 		*val = sign_extend32(ret, 15);
59 		return IIO_VAL_INT;
60 
61 	case IIO_CHAN_INFO_CALIBBIAS:
62 		ret = i2c_smbus_read_word_swapped(data->client,
63 						  TMP117_REG_TEMP_OFFSET);
64 		if (ret < 0)
65 			return ret;
66 		*val = sign_extend32(ret, 15);
67 		return IIO_VAL_INT;
68 
69 	case IIO_CHAN_INFO_SCALE:
70 		/*
71 		 * Conversion from 10s of uC to mC
72 		 * as IIO reports temperature in mC
73 		 */
74 		*val = TMP117_RESOLUTION_10UC / MICRODEGREE_PER_10MILLIDEGREE;
75 		*val2 = (TMP117_RESOLUTION_10UC %
76 					MICRODEGREE_PER_10MILLIDEGREE) * 100;
77 
78 		return IIO_VAL_INT_PLUS_MICRO;
79 
80 	default:
81 		return -EINVAL;
82 	}
83 }
84 
tmp117_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int val,int val2,long mask)85 static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec
86 			    const *channel, int val, int val2, long mask)
87 {
88 	struct tmp117_data *data = iio_priv(indio_dev);
89 	s16 off;
90 
91 	switch (mask) {
92 	case IIO_CHAN_INFO_CALIBBIAS:
93 		off = clamp_t(int, val, S16_MIN, S16_MAX);
94 		if (off == data->calibbias)
95 			return 0;
96 		data->calibbias = off;
97 		return i2c_smbus_write_word_swapped(data->client,
98 						TMP117_REG_TEMP_OFFSET, off);
99 
100 	default:
101 		return -EINVAL;
102 	}
103 }
104 
105 static const struct iio_chan_spec tmp117_channels[] = {
106 	{
107 		.type = IIO_TEMP,
108 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
109 				      BIT(IIO_CHAN_INFO_CALIBBIAS) |
110 				      BIT(IIO_CHAN_INFO_SCALE),
111 	},
112 };
113 
114 static const struct iio_chan_spec tmp116_channels[] = {
115 	{
116 		.type = IIO_TEMP,
117 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
118 				      BIT(IIO_CHAN_INFO_SCALE),
119 	},
120 };
121 
122 static const struct iio_info tmp117_info = {
123 	.read_raw = tmp117_read_raw,
124 	.write_raw = tmp117_write_raw,
125 };
126 
tmp117_identify(struct i2c_client * client)127 static int tmp117_identify(struct i2c_client *client)
128 {
129 	const struct i2c_device_id *id;
130 	unsigned long match_data;
131 	int dev_id;
132 
133 	dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID);
134 	if (dev_id < 0)
135 		return dev_id;
136 
137 	switch (dev_id) {
138 	case TMP116_DEVICE_ID:
139 	case TMP117_DEVICE_ID:
140 		return dev_id;
141 	}
142 
143 	dev_info(&client->dev, "Unknown device id (0x%x), use fallback compatible\n",
144 		 dev_id);
145 
146 	match_data = (uintptr_t)device_get_match_data(&client->dev);
147 	if (match_data)
148 		return match_data;
149 
150 	id = i2c_client_get_device_id(client);
151 	if (id)
152 		return id->driver_data;
153 
154 	dev_err(&client->dev, "Failed to identify unsupported device\n");
155 
156 	return -ENODEV;
157 }
158 
tmp117_probe(struct i2c_client * client)159 static int tmp117_probe(struct i2c_client *client)
160 {
161 	struct tmp117_data *data;
162 	struct iio_dev *indio_dev;
163 	int ret, dev_id;
164 
165 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
166 		return -EOPNOTSUPP;
167 
168 	ret = tmp117_identify(client);
169 	if (ret < 0)
170 		return ret;
171 
172 	dev_id = ret;
173 
174 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
175 	if (!indio_dev)
176 		return -ENOMEM;
177 
178 	data = iio_priv(indio_dev);
179 	data->client = client;
180 	data->calibbias = 0;
181 
182 	indio_dev->modes = INDIO_DIRECT_MODE;
183 	indio_dev->info = &tmp117_info;
184 
185 	switch (dev_id) {
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";
190 		break;
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";
195 		break;
196 	}
197 
198 	return devm_iio_device_register(&client->dev, indio_dev);
199 }
200 
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 },
204 	{ }
205 };
206 MODULE_DEVICE_TABLE(of, tmp117_of_match);
207 
208 static const struct i2c_device_id tmp117_id[] = {
209 	{ "tmp116", TMP116_DEVICE_ID },
210 	{ "tmp117", TMP117_DEVICE_ID },
211 	{ }
212 };
213 MODULE_DEVICE_TABLE(i2c, tmp117_id);
214 
215 static struct i2c_driver tmp117_driver = {
216 	.driver = {
217 		.name	= "tmp117",
218 		.of_match_table = tmp117_of_match,
219 	},
220 	.probe		= tmp117_probe,
221 	.id_table	= tmp117_id,
222 };
223 module_i2c_driver(tmp117_driver);
224 
225 MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>");
226 MODULE_DESCRIPTION("TI TMP117 Temperature sensor driver");
227 MODULE_LICENSE("GPL");
228