2 * tmp007.c - Support for TI TMP007 IR thermopile sensor with integrated math engine
4 * Copyright (c) 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
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.
10 * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor
12 * (7-bit I2C slave address (0x40 - 0x47), changeable via ADR pins)
14 * Note: This driver assumes that the sensor has been calibrated beforehand
16 * TODO: ALERT irq, limit threshold events
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/delay.h>
23 #include <linux/module.h>
25 #include <linux/bitops.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
31 #define TMP007_TDIE 0x01
32 #define TMP007_CONFIG 0x02
33 #define TMP007_TOBJECT 0x03
34 #define TMP007_STATUS 0x04
35 #define TMP007_STATUS_MASK 0x05
36 #define TMP007_MANUFACTURER_ID 0x1e
37 #define TMP007_DEVICE_ID 0x1f
39 #define TMP007_CONFIG_CONV_EN BIT(12)
40 #define TMP007_CONFIG_COMP_EN BIT(5)
41 #define TMP007_CONFIG_TC_EN BIT(6)
42 #define TMP007_CONFIG_CR_MASK GENMASK(11, 9)
43 #define TMP007_CONFIG_CR_SHIFT 9
45 #define TMP007_STATUS_CONV_READY BIT(14)
46 #define TMP007_STATUS_DATA_VALID BIT(9)
48 #define TMP007_MANUFACTURER_MAGIC 0x5449
49 #define TMP007_DEVICE_MAGIC 0x0078
51 #define TMP007_TEMP_SHIFT 2
54 struct i2c_client
*client
;
58 static const int tmp007_avgs
[5][2] = { {4, 0}, {2, 0}, {1, 0},
59 {0, 500000}, {0, 250000} };
61 static int tmp007_read_temperature(struct tmp007_data
*data
, u8 reg
)
67 ret
= i2c_smbus_read_word_swapped(data
->client
,
71 if ((ret
& TMP007_STATUS_CONV_READY
) &&
72 !(ret
& TMP007_STATUS_DATA_VALID
))
80 return i2c_smbus_read_word_swapped(data
->client
, reg
);
83 static int tmp007_powerdown(struct tmp007_data
*data
)
85 return i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
86 data
->config
& ~TMP007_CONFIG_CONV_EN
);
89 static int tmp007_read_raw(struct iio_dev
*indio_dev
,
90 struct iio_chan_spec
const *channel
, int *val
,
93 struct tmp007_data
*data
= iio_priv(indio_dev
);
98 case IIO_CHAN_INFO_RAW
:
99 switch (channel
->channel2
) {
100 case IIO_MOD_TEMP_AMBIENT
: /* LSB: 0.03125 degree Celsius */
101 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_TDIE
);
105 case IIO_MOD_TEMP_OBJECT
:
106 ret
= tmp007_read_temperature(data
, TMP007_TOBJECT
);
114 *val
= sign_extend32(ret
, 15) >> TMP007_TEMP_SHIFT
;
117 case IIO_CHAN_INFO_SCALE
:
121 return IIO_VAL_INT_PLUS_MICRO
;
122 case IIO_CHAN_INFO_SAMP_FREQ
:
123 conv_rate
= (data
->config
& TMP007_CONFIG_CR_MASK
)
124 >> TMP007_CONFIG_CR_SHIFT
;
125 *val
= tmp007_avgs
[conv_rate
][0];
126 *val2
= tmp007_avgs
[conv_rate
][1];
128 return IIO_VAL_INT_PLUS_MICRO
;
134 static int tmp007_write_raw(struct iio_dev
*indio_dev
,
135 struct iio_chan_spec
const *channel
, int val
,
138 struct tmp007_data
*data
= iio_priv(indio_dev
);
142 if (mask
== IIO_CHAN_INFO_SAMP_FREQ
) {
143 for (i
= 0; i
< ARRAY_SIZE(tmp007_avgs
); i
++) {
144 if ((val
== tmp007_avgs
[i
][0]) &&
145 (val2
== tmp007_avgs
[i
][1])) {
146 tmp
= data
->config
& ~TMP007_CONFIG_CR_MASK
;
147 tmp
|= (i
<< TMP007_CONFIG_CR_SHIFT
);
149 return i2c_smbus_write_word_swapped(data
->client
,
159 static IIO_CONST_ATTR(sampling_frequency_available
, "4 2 1 0.5 0.25");
161 static struct attribute
*tmp007_attributes
[] = {
162 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
166 static const struct attribute_group tmp007_attribute_group
= {
167 .attrs
= tmp007_attributes
,
170 static const struct iio_chan_spec tmp007_channels
[] = {
174 .channel2
= IIO_MOD_TEMP_AMBIENT
,
175 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
176 BIT(IIO_CHAN_INFO_SCALE
),
177 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
182 .channel2
= IIO_MOD_TEMP_OBJECT
,
183 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
184 BIT(IIO_CHAN_INFO_SCALE
),
185 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
189 static const struct iio_info tmp007_info
= {
190 .read_raw
= tmp007_read_raw
,
191 .write_raw
= tmp007_write_raw
,
192 .attrs
= &tmp007_attribute_group
,
193 .driver_module
= THIS_MODULE
,
196 static bool tmp007_identify(struct i2c_client
*client
)
200 manf_id
= i2c_smbus_read_word_swapped(client
, TMP007_MANUFACTURER_ID
);
204 dev_id
= i2c_smbus_read_word_swapped(client
, TMP007_DEVICE_ID
);
208 return (manf_id
== TMP007_MANUFACTURER_MAGIC
&& dev_id
== TMP007_DEVICE_MAGIC
);
211 static int tmp007_probe(struct i2c_client
*client
,
212 const struct i2c_device_id
*tmp007_id
)
214 struct tmp007_data
*data
;
215 struct iio_dev
*indio_dev
;
219 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
222 if (!tmp007_identify(client
)) {
223 dev_err(&client
->dev
, "TMP007 not found\n");
227 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
231 data
= iio_priv(indio_dev
);
232 i2c_set_clientdata(client
, indio_dev
);
233 data
->client
= client
;
235 indio_dev
->dev
.parent
= &client
->dev
;
236 indio_dev
->name
= "tmp007";
237 indio_dev
->modes
= INDIO_DIRECT_MODE
;
238 indio_dev
->info
= &tmp007_info
;
240 indio_dev
->channels
= tmp007_channels
;
241 indio_dev
->num_channels
= ARRAY_SIZE(tmp007_channels
);
244 * Set Configuration register:
247 * 3. Transient correction enable
250 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_CONFIG
);
255 data
->config
|= (TMP007_CONFIG_CONV_EN
| TMP007_CONFIG_COMP_EN
| TMP007_CONFIG_TC_EN
);
257 ret
= i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
263 * Set Status Mask register:
264 * 1. Conversion ready enable
265 * 2. Data valid enable
268 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_STATUS_MASK
);
270 goto error_powerdown
;
273 status
|= (TMP007_STATUS_CONV_READY
| TMP007_STATUS_DATA_VALID
);
275 ret
= i2c_smbus_write_word_swapped(data
->client
, TMP007_STATUS_MASK
, status
);
277 goto error_powerdown
;
279 return iio_device_register(indio_dev
);
282 tmp007_powerdown(data
);
287 static int tmp007_remove(struct i2c_client
*client
)
289 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
290 struct tmp007_data
*data
= iio_priv(indio_dev
);
292 iio_device_unregister(indio_dev
);
293 tmp007_powerdown(data
);
298 #ifdef CONFIG_PM_SLEEP
299 static int tmp007_suspend(struct device
*dev
)
301 struct tmp007_data
*data
= iio_priv(i2c_get_clientdata(
302 to_i2c_client(dev
)));
304 return tmp007_powerdown(data
);
307 static int tmp007_resume(struct device
*dev
)
309 struct tmp007_data
*data
= iio_priv(i2c_get_clientdata(
310 to_i2c_client(dev
)));
312 return i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
313 data
->config
| TMP007_CONFIG_CONV_EN
);
317 static SIMPLE_DEV_PM_OPS(tmp007_pm_ops
, tmp007_suspend
, tmp007_resume
);
319 static const struct of_device_id tmp007_of_match
[] = {
320 { .compatible
= "ti,tmp007", },
323 MODULE_DEVICE_TABLE(of
, tmp007_of_match
);
325 static const struct i2c_device_id tmp007_id
[] = {
329 MODULE_DEVICE_TABLE(i2c
, tmp007_id
);
331 static struct i2c_driver tmp007_driver
= {
334 .of_match_table
= of_match_ptr(tmp007_of_match
),
335 .pm
= &tmp007_pm_ops
,
337 .probe
= tmp007_probe
,
338 .remove
= tmp007_remove
,
339 .id_table
= tmp007_id
,
341 module_i2c_driver(tmp007_driver
);
343 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
344 MODULE_DESCRIPTION("TI TMP007 IR thermopile sensor driver");
345 MODULE_LICENSE("GPL");