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