aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorPeter Meerwald <pmeerw@pmeerw.net>2013-08-08 12:39:00 -0400
committerJonathan Cameron <jic23@kernel.org>2013-08-17 10:50:53 -0400
commite5a639421821c7cd11832fd7fbe6376bfb304880 (patch)
tree7ae4df3e33bb99c0c3ad74a148b1b6dc6f1ff888 /drivers/iio
parent9c6cd3b39048c8bbb83c5cd936f4dffc847321c6 (diff)
iio: Add tmp006 IR temperature sensor
the TI TMP006 is a non-contact temperature sensor with I2C interface; it measures the surface temperature of a distance object using a thermopile to absorb IR energy emitted from the object the sensor has two channels: IR sensor voltage (16-bit) and reference temperature of the chip (14-bit); datasheet is here: http://www.ti.com/lit/ds/symlink/tmp006.pdf v2 (thanks to Grygorii Strashko, Lars-Peter Clausen, Jonathan Cameron for review comments): * power down device on driver remove * use sign_extend32() * style cleanup * add comments what channel raw LSBs mean * spelling of thermopile Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Cc: Grygorii Strashko <grygorii.strashko@ti.com> Cc: Lars-Peter Clausen <lars@metafoo.de> Cc: Jonathan Cameron <jic23@kernel.org> Cc: LM Sensors <lm-sensors@lm-sensors.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/Kconfig1
-rw-r--r--drivers/iio/Makefile1
-rw-r--r--drivers/iio/temperature/Kconfig16
-rw-r--r--drivers/iio/temperature/Makefile5
-rw-r--r--drivers/iio/temperature/tmp006.c291
5 files changed, 314 insertions, 0 deletions
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index 524380c9ae6c..cbea3271c1b1 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -73,5 +73,6 @@ if IIO_TRIGGER
73 source "drivers/iio/trigger/Kconfig" 73 source "drivers/iio/trigger/Kconfig"
74endif #IIO_TRIGGER 74endif #IIO_TRIGGER
75source "drivers/iio/pressure/Kconfig" 75source "drivers/iio/pressure/Kconfig"
76source "drivers/iio/temperature/Kconfig"
76 77
77endif # IIO 78endif # IIO
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index 7a3866c2d2a1..a0547f59e88f 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -21,5 +21,6 @@ obj-y += frequency/
21obj-y += imu/ 21obj-y += imu/
22obj-y += light/ 22obj-y += light/
23obj-y += magnetometer/ 23obj-y += magnetometer/
24obj-y += temperature/
24obj-y += trigger/ 25obj-y += trigger/
25obj-y += pressure/ 26obj-y += pressure/
diff --git a/drivers/iio/temperature/Kconfig b/drivers/iio/temperature/Kconfig
new file mode 100644
index 000000000000..372f8fb3085f
--- /dev/null
+++ b/drivers/iio/temperature/Kconfig
@@ -0,0 +1,16 @@
1#
2# Temperature sensor drivers
3#
4menu "Temperature sensors"
5
6config TMP006
7 tristate "TMP006 infrared thermopile sensor"
8 depends on I2C
9 help
10 If you say yes here you get support for the Texas Instruments
11 TMP006 infrared thermopile sensor.
12
13 This driver can also be built as a module. If so, the module will
14 be called tmp006.
15
16endmenu
diff --git a/drivers/iio/temperature/Makefile b/drivers/iio/temperature/Makefile
new file mode 100644
index 000000000000..24d7b602db3e
--- /dev/null
+++ b/drivers/iio/temperature/Makefile
@@ -0,0 +1,5 @@
1#
2# Makefile for industrial I/O temperature drivers
3#
4
5obj-$(CONFIG_TMP006) += tmp006.o
diff --git a/drivers/iio/temperature/tmp006.c b/drivers/iio/temperature/tmp006.c
new file mode 100644
index 000000000000..64ccde3f1f7a
--- /dev/null
+++ b/drivers/iio/temperature/tmp006.c
@@ -0,0 +1,291 @@
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 0x7000
40
41#define TMP006_CONFIG_CR_MASK 0x0e00
42#define TMP006_CONFIG_CR_SHIFT 9
43
44#define MANUFACTURER_MAGIC 0x5449
45#define DEVICE_MAGIC 0x0067
46
47struct tmp006_data {
48 struct i2c_client *client;
49 u16 config;
50};
51
52static 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
73static int tmp006_read_raw(struct iio_dev *indio_dev,
74 struct iio_chan_spec const *channel, int *val,
75 int *val2, long mask)
76{
77 struct tmp006_data *data = iio_priv(indio_dev);
78 s32 ret;
79
80 switch (mask) {
81 case IIO_CHAN_INFO_RAW:
82 if (channel->type == IIO_VOLTAGE) {
83 /* LSB is 156.25 nV */
84 ret = tmp006_read_measurement(data, TMP006_VOBJECT);
85 if (ret < 0)
86 return ret;
87 *val = sign_extend32(ret, 15);
88 } else if (channel->type == IIO_TEMP) {
89 /* LSB is 0.03125 degrees Celsius */
90 ret = tmp006_read_measurement(data, TMP006_TAMBIENT);
91 if (ret < 0)
92 return ret;
93 *val = sign_extend32(ret, 15) >> TMP006_TAMBIENT_SHIFT;
94 } else {
95 break;
96 }
97 return IIO_VAL_INT;
98 case IIO_CHAN_INFO_SCALE:
99 if (channel->type == IIO_VOLTAGE) {
100 *val = 0;
101 *val2 = 156250;
102 } else if (channel->type == IIO_TEMP) {
103 *val = 31;
104 *val2 = 250000;
105 } else {
106 break;
107 }
108 return IIO_VAL_INT_PLUS_MICRO;
109 default:
110 break;
111 }
112
113 return -EINVAL;
114}
115
116static const char * const tmp006_freqs[] = { "4", "2", "1", "0.5", "0.25" };
117
118static ssize_t tmp006_show_freq(struct device *dev,
119 struct device_attribute *attr, char *buf)
120{
121 struct tmp006_data *data = iio_priv(dev_to_iio_dev(dev));
122 int cr = (data->config & TMP006_CONFIG_CR_MASK)
123 >> TMP006_CONFIG_CR_SHIFT;
124 return sprintf(buf, "%s\n", tmp006_freqs[cr]);
125}
126
127static ssize_t tmp006_store_freq(struct device *dev,
128 struct device_attribute *attr,
129 const char *buf, size_t len)
130{
131 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
132 struct tmp006_data *data = iio_priv(indio_dev);
133 int i;
134 bool found = false;
135
136 for (i = 0; i < ARRAY_SIZE(tmp006_freqs); i++)
137 if (sysfs_streq(buf, tmp006_freqs[i])) {
138 found = true;
139 break;
140 }
141 if (!found)
142 return -EINVAL;
143
144 data->config &= ~TMP006_CONFIG_CR_MASK;
145 data->config |= i << TMP006_CONFIG_CR_SHIFT;
146
147 return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
148 data->config);
149}
150
151static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
152 tmp006_show_freq, tmp006_store_freq);
153
154static IIO_CONST_ATTR(sampling_frequency_available, "4 2 1 0.5 0.25");
155
156static struct attribute *tmp006_attributes[] = {
157 &iio_dev_attr_sampling_frequency.dev_attr.attr,
158 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
159 NULL
160};
161
162static const struct attribute_group tmp006_attribute_group = {
163 .attrs = tmp006_attributes,
164};
165
166static const struct iio_chan_spec tmp006_channels[] = {
167 {
168 .type = IIO_VOLTAGE,
169 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
170 BIT(IIO_CHAN_INFO_SCALE),
171 },
172 {
173 .type = IIO_TEMP,
174 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
175 BIT(IIO_CHAN_INFO_SCALE),
176 }
177};
178
179static const struct iio_info tmp006_info = {
180 .read_raw = tmp006_read_raw,
181 .attrs = &tmp006_attribute_group,
182 .driver_module = THIS_MODULE,
183};
184
185static bool tmp006_check_identification(struct i2c_client *client)
186{
187 int mid, did;
188
189 mid = i2c_smbus_read_word_swapped(client, TMP006_MANUFACTURER_ID);
190 if (mid < 0)
191 return false;
192
193 did = i2c_smbus_read_word_swapped(client, TMP006_DEVICE_ID);
194 if (did < 0)
195 return false;
196
197 return mid == MANUFACTURER_MAGIC && did == DEVICE_MAGIC;
198}
199
200static int tmp006_probe(struct i2c_client *client,
201 const struct i2c_device_id *id)
202{
203 struct iio_dev *indio_dev;
204 struct tmp006_data *data;
205 int ret;
206
207 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
208 return -ENODEV;
209
210 if (!tmp006_check_identification(client)) {
211 dev_err(&client->dev, "no TMP006 sensor\n");
212 return -ENODEV;
213 }
214
215 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
216 if (!indio_dev)
217 return -ENOMEM;
218
219 data = iio_priv(indio_dev);
220 i2c_set_clientdata(client, indio_dev);
221 data->client = client;
222
223 indio_dev->dev.parent = &client->dev;
224 indio_dev->name = dev_name(&client->dev);
225 indio_dev->modes = INDIO_DIRECT_MODE;
226 indio_dev->info = &tmp006_info;
227
228 indio_dev->channels = tmp006_channels;
229 indio_dev->num_channels = ARRAY_SIZE(tmp006_channels);
230
231 ret = i2c_smbus_read_word_swapped(data->client, TMP006_CONFIG);
232 if (ret < 0)
233 return ret;
234 data->config = ret;
235
236 return iio_device_register(indio_dev);
237}
238
239static int tmp006_powerdown(struct tmp006_data *data)
240{
241 return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
242 data->config & ~TMP006_CONFIG_MOD_MASK);
243}
244
245static int tmp006_remove(struct i2c_client *client)
246{
247 struct iio_dev *indio_dev = i2c_get_clientdata(client);
248
249 iio_device_unregister(indio_dev);
250 tmp006_powerdown(iio_priv(indio_dev));
251
252 return 0;
253}
254
255#ifdef CONFIG_PM_SLEEP
256static int tmp006_suspend(struct device *dev)
257{
258 return tmp006_powerdown(iio_priv(dev_to_iio_dev(dev)));
259}
260
261static int tmp006_resume(struct device *dev)
262{
263 struct tmp006_data *data = iio_priv(dev_to_iio_dev(dev));
264 return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
265 data->config | TMP006_CONFIG_MOD_MASK);
266}
267#endif
268
269static SIMPLE_DEV_PM_OPS(tmp006_pm_ops, tmp006_suspend, tmp006_resume);
270
271static const struct i2c_device_id tmp006_id[] = {
272 { "tmp006", 0 },
273 { }
274};
275MODULE_DEVICE_TABLE(i2c, tmp006_id);
276
277static struct i2c_driver tmp006_driver = {
278 .driver = {
279 .name = "tmp006",
280 .pm = &tmp006_pm_ops,
281 .owner = THIS_MODULE,
282 },
283 .probe = tmp006_probe,
284 .remove = tmp006_remove,
285 .id_table = tmp006_id,
286};
287module_i2c_driver(tmp006_driver);
288
289MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
290MODULE_DESCRIPTION("TI TMP006 IR thermopile sensor driver");
291MODULE_LICENSE("GPL");