xref: /openbmc/linux/drivers/iio/temperature/tmp006.c (revision ae213c44)
1 /*
2  * tmp006.c - Support for TI TMP006 IR thermopile sensor
3  *
4  * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
5  *
6  * This file is subject to the terms and conditions of version 2 of
7  * the GNU General Public License.  See the file COPYING in the main
8  * directory of this archive for more details.
9  *
10  * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor
11  *
12  * (7-bit I2C slave address 0x40, changeable via ADR pins)
13  *
14  * TODO: data ready irq
15  */
16 
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/module.h>
21 #include <linux/pm.h>
22 #include <linux/bitops.h>
23 
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 
27 #define TMP006_VOBJECT 0x00
28 #define TMP006_TAMBIENT 0x01
29 #define TMP006_CONFIG 0x02
30 #define TMP006_MANUFACTURER_ID 0xfe
31 #define TMP006_DEVICE_ID 0xff
32 
33 #define TMP006_TAMBIENT_SHIFT 2
34 
35 #define TMP006_CONFIG_RESET BIT(15)
36 #define TMP006_CONFIG_DRDY_EN BIT(8)
37 #define TMP006_CONFIG_DRDY BIT(7)
38 
39 #define TMP006_CONFIG_MOD_MASK GENMASK(14, 12)
40 
41 #define TMP006_CONFIG_CR_MASK GENMASK(11, 9)
42 #define TMP006_CONFIG_CR_SHIFT 9
43 
44 #define TMP006_MANUFACTURER_MAGIC 0x5449
45 #define TMP006_DEVICE_MAGIC 0x0067
46 
47 struct tmp006_data {
48 	struct i2c_client *client;
49 	u16 config;
50 };
51 
52 static int tmp006_read_measurement(struct tmp006_data *data, u8 reg)
53 {
54 	s32 ret;
55 	int tries = 50;
56 
57 	while (tries-- > 0) {
58 		ret = i2c_smbus_read_word_swapped(data->client,
59 			TMP006_CONFIG);
60 		if (ret < 0)
61 			return ret;
62 		if (ret & TMP006_CONFIG_DRDY)
63 			break;
64 		msleep(100);
65 	}
66 
67 	if (tries < 0)
68 		return -EIO;
69 
70 	return i2c_smbus_read_word_swapped(data->client, reg);
71 }
72 
73 static const int tmp006_freqs[5][2] = { {4, 0}, {2, 0}, {1, 0},
74 					{0, 500000}, {0, 250000} };
75 
76 static int tmp006_read_raw(struct iio_dev *indio_dev,
77 			    struct iio_chan_spec const *channel, int *val,
78 			    int *val2, long mask)
79 {
80 	struct tmp006_data *data = iio_priv(indio_dev);
81 	s32 ret;
82 	int cr;
83 
84 	switch (mask) {
85 	case IIO_CHAN_INFO_RAW:
86 		if (channel->type == IIO_VOLTAGE) {
87 			/* LSB is 156.25 nV */
88 			ret = tmp006_read_measurement(data, TMP006_VOBJECT);
89 			if (ret < 0)
90 				return ret;
91 			*val = sign_extend32(ret, 15);
92 		} else if (channel->type == IIO_TEMP) {
93 			/* LSB is 0.03125 degrees Celsius */
94 			ret = tmp006_read_measurement(data, TMP006_TAMBIENT);
95 			if (ret < 0)
96 				return ret;
97 			*val = sign_extend32(ret, 15) >> TMP006_TAMBIENT_SHIFT;
98 		} else {
99 			break;
100 		}
101 		return IIO_VAL_INT;
102 	case IIO_CHAN_INFO_SCALE:
103 		if (channel->type == IIO_VOLTAGE) {
104 			*val = 0;
105 			*val2 = 156250;
106 		} else if (channel->type == IIO_TEMP) {
107 			*val = 31;
108 			*val2 = 250000;
109 		} else {
110 			break;
111 		}
112 		return IIO_VAL_INT_PLUS_MICRO;
113 	case IIO_CHAN_INFO_SAMP_FREQ:
114 		cr = (data->config & TMP006_CONFIG_CR_MASK)
115 			>> TMP006_CONFIG_CR_SHIFT;
116 		*val = tmp006_freqs[cr][0];
117 		*val2 = tmp006_freqs[cr][1];
118 		return IIO_VAL_INT_PLUS_MICRO;
119 	default:
120 		break;
121 	}
122 
123 	return -EINVAL;
124 }
125 
126 static int tmp006_write_raw(struct iio_dev *indio_dev,
127 			    struct iio_chan_spec const *chan,
128 			    int val,
129 			    int val2,
130 			    long mask)
131 {
132 	struct tmp006_data *data = iio_priv(indio_dev);
133 	int i;
134 
135 	if (mask != IIO_CHAN_INFO_SAMP_FREQ)
136 		return -EINVAL;
137 
138 	for (i = 0; i < ARRAY_SIZE(tmp006_freqs); i++)
139 		if ((val == tmp006_freqs[i][0]) &&
140 		    (val2 == tmp006_freqs[i][1])) {
141 			data->config &= ~TMP006_CONFIG_CR_MASK;
142 			data->config |= i << TMP006_CONFIG_CR_SHIFT;
143 
144 			return i2c_smbus_write_word_swapped(data->client,
145 							    TMP006_CONFIG,
146 							    data->config);
147 
148 		}
149 	return -EINVAL;
150 }
151 
152 static IIO_CONST_ATTR(sampling_frequency_available, "4 2 1 0.5 0.25");
153 
154 static struct attribute *tmp006_attributes[] = {
155 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
156 	NULL
157 };
158 
159 static const struct attribute_group tmp006_attribute_group = {
160 	.attrs = tmp006_attributes,
161 };
162 
163 static const struct iio_chan_spec tmp006_channels[] = {
164 	{
165 		.type = IIO_VOLTAGE,
166 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
167 			BIT(IIO_CHAN_INFO_SCALE),
168 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
169 	},
170 	{
171 		.type = IIO_TEMP,
172 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
173 			BIT(IIO_CHAN_INFO_SCALE),
174 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
175 	}
176 };
177 
178 static const struct iio_info tmp006_info = {
179 	.read_raw = tmp006_read_raw,
180 	.write_raw = tmp006_write_raw,
181 	.attrs = &tmp006_attribute_group,
182 };
183 
184 static bool tmp006_check_identification(struct i2c_client *client)
185 {
186 	int mid, did;
187 
188 	mid = i2c_smbus_read_word_swapped(client, TMP006_MANUFACTURER_ID);
189 	if (mid < 0)
190 		return false;
191 
192 	did = i2c_smbus_read_word_swapped(client, TMP006_DEVICE_ID);
193 	if (did < 0)
194 		return false;
195 
196 	return mid == TMP006_MANUFACTURER_MAGIC && did == TMP006_DEVICE_MAGIC;
197 }
198 
199 static int tmp006_probe(struct i2c_client *client,
200 			 const struct i2c_device_id *id)
201 {
202 	struct iio_dev *indio_dev;
203 	struct tmp006_data *data;
204 	int ret;
205 
206 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
207 		return -EOPNOTSUPP;
208 
209 	if (!tmp006_check_identification(client)) {
210 		dev_err(&client->dev, "no TMP006 sensor\n");
211 		return -ENODEV;
212 	}
213 
214 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
215 	if (!indio_dev)
216 		return -ENOMEM;
217 
218 	data = iio_priv(indio_dev);
219 	i2c_set_clientdata(client, indio_dev);
220 	data->client = client;
221 
222 	indio_dev->dev.parent = &client->dev;
223 	indio_dev->name = dev_name(&client->dev);
224 	indio_dev->modes = INDIO_DIRECT_MODE;
225 	indio_dev->info = &tmp006_info;
226 
227 	indio_dev->channels = tmp006_channels;
228 	indio_dev->num_channels = ARRAY_SIZE(tmp006_channels);
229 
230 	ret = i2c_smbus_read_word_swapped(data->client, TMP006_CONFIG);
231 	if (ret < 0)
232 		return ret;
233 	data->config = ret;
234 
235 	return iio_device_register(indio_dev);
236 }
237 
238 static int tmp006_powerdown(struct tmp006_data *data)
239 {
240 	return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
241 		data->config & ~TMP006_CONFIG_MOD_MASK);
242 }
243 
244 static int tmp006_remove(struct i2c_client *client)
245 {
246 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
247 
248 	iio_device_unregister(indio_dev);
249 	tmp006_powerdown(iio_priv(indio_dev));
250 
251 	return 0;
252 }
253 
254 #ifdef CONFIG_PM_SLEEP
255 static int tmp006_suspend(struct device *dev)
256 {
257 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
258 	return tmp006_powerdown(iio_priv(indio_dev));
259 }
260 
261 static int tmp006_resume(struct device *dev)
262 {
263 	struct tmp006_data *data = iio_priv(i2c_get_clientdata(
264 		to_i2c_client(dev)));
265 	return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
266 		data->config | TMP006_CONFIG_MOD_MASK);
267 }
268 #endif
269 
270 static SIMPLE_DEV_PM_OPS(tmp006_pm_ops, tmp006_suspend, tmp006_resume);
271 
272 static const struct i2c_device_id tmp006_id[] = {
273 	{ "tmp006", 0 },
274 	{ }
275 };
276 MODULE_DEVICE_TABLE(i2c, tmp006_id);
277 
278 static struct i2c_driver tmp006_driver = {
279 	.driver = {
280 		.name	= "tmp006",
281 		.pm	= &tmp006_pm_ops,
282 	},
283 	.probe = tmp006_probe,
284 	.remove = tmp006_remove,
285 	.id_table = tmp006_id,
286 };
287 module_i2c_driver(tmp006_driver);
288 
289 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
290 MODULE_DESCRIPTION("TI TMP006 IR thermopile sensor driver");
291 MODULE_LICENSE("GPL");
292