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)
15 * 1. This driver assumes that the sensor has been calibrated beforehand
16 * 2. Limit threshold events are enabled at the start
17 * 3. Operating mode: INT
21 #include <linux/err.h>
22 #include <linux/i2c.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
26 #include <linux/bitops.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
33 #include <linux/iio/events.h>
35 #define TMP007_TDIE 0x01
36 #define TMP007_CONFIG 0x02
37 #define TMP007_TOBJECT 0x03
38 #define TMP007_STATUS 0x04
39 #define TMP007_STATUS_MASK 0x05
40 #define TMP007_TOBJ_HIGH_LIMIT 0x06
41 #define TMP007_TOBJ_LOW_LIMIT 0x07
42 #define TMP007_TDIE_HIGH_LIMIT 0x08
43 #define TMP007_TDIE_LOW_LIMIT 0x09
44 #define TMP007_MANUFACTURER_ID 0x1e
45 #define TMP007_DEVICE_ID 0x1f
47 #define TMP007_CONFIG_CONV_EN BIT(12)
48 #define TMP007_CONFIG_TC_EN BIT(6)
49 #define TMP007_CONFIG_CR_MASK GENMASK(11, 9)
50 #define TMP007_CONFIG_ALERT_EN BIT(8)
51 #define TMP007_CONFIG_CR_SHIFT 9
53 /* Status register flags */
54 #define TMP007_STATUS_ALERT BIT(15)
55 #define TMP007_STATUS_CONV_READY BIT(14)
56 #define TMP007_STATUS_OHF BIT(13)
57 #define TMP007_STATUS_OLF BIT(12)
58 #define TMP007_STATUS_LHF BIT(11)
59 #define TMP007_STATUS_LLF BIT(10)
60 #define TMP007_STATUS_DATA_VALID BIT(9)
62 #define TMP007_MANUFACTURER_MAGIC 0x5449
63 #define TMP007_DEVICE_MAGIC 0x0078
65 #define TMP007_TEMP_SHIFT 2
68 struct i2c_client
*client
;
74 static const int tmp007_avgs
[5][2] = { {4, 0}, {2, 0}, {1, 0},
75 {0, 500000}, {0, 250000} };
77 static int tmp007_read_temperature(struct tmp007_data
*data
, u8 reg
)
83 ret
= i2c_smbus_read_word_swapped(data
->client
,
87 if ((ret
& TMP007_STATUS_CONV_READY
) &&
88 !(ret
& TMP007_STATUS_DATA_VALID
))
96 return i2c_smbus_read_word_swapped(data
->client
, reg
);
99 static int tmp007_powerdown(struct tmp007_data
*data
)
101 return i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
102 data
->config
& ~TMP007_CONFIG_CONV_EN
);
105 static int tmp007_read_raw(struct iio_dev
*indio_dev
,
106 struct iio_chan_spec
const *channel
, int *val
,
107 int *val2
, long mask
)
109 struct tmp007_data
*data
= iio_priv(indio_dev
);
114 case IIO_CHAN_INFO_RAW
:
115 switch (channel
->channel2
) {
116 case IIO_MOD_TEMP_AMBIENT
: /* LSB: 0.03125 degree Celsius */
117 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_TDIE
);
121 case IIO_MOD_TEMP_OBJECT
:
122 ret
= tmp007_read_temperature(data
, TMP007_TOBJECT
);
130 *val
= sign_extend32(ret
, 15) >> TMP007_TEMP_SHIFT
;
133 case IIO_CHAN_INFO_SCALE
:
137 return IIO_VAL_INT_PLUS_MICRO
;
138 case IIO_CHAN_INFO_SAMP_FREQ
:
139 conv_rate
= (data
->config
& TMP007_CONFIG_CR_MASK
)
140 >> TMP007_CONFIG_CR_SHIFT
;
141 *val
= tmp007_avgs
[conv_rate
][0];
142 *val2
= tmp007_avgs
[conv_rate
][1];
144 return IIO_VAL_INT_PLUS_MICRO
;
150 static int tmp007_write_raw(struct iio_dev
*indio_dev
,
151 struct iio_chan_spec
const *channel
, int val
,
154 struct tmp007_data
*data
= iio_priv(indio_dev
);
158 if (mask
== IIO_CHAN_INFO_SAMP_FREQ
) {
159 for (i
= 0; i
< ARRAY_SIZE(tmp007_avgs
); i
++) {
160 if ((val
== tmp007_avgs
[i
][0]) &&
161 (val2
== tmp007_avgs
[i
][1])) {
162 tmp
= data
->config
& ~TMP007_CONFIG_CR_MASK
;
163 tmp
|= (i
<< TMP007_CONFIG_CR_SHIFT
);
165 return i2c_smbus_write_word_swapped(data
->client
,
175 static irqreturn_t
tmp007_interrupt_handler(int irq
, void *private)
177 struct iio_dev
*indio_dev
= private;
178 struct tmp007_data
*data
= iio_priv(indio_dev
);
181 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_STATUS
);
182 if ((ret
< 0) || !(ret
& (TMP007_STATUS_OHF
| TMP007_STATUS_OLF
|
183 TMP007_STATUS_LHF
| TMP007_STATUS_LLF
)))
186 if (ret
& TMP007_STATUS_OHF
)
187 iio_push_event(indio_dev
,
188 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
192 iio_get_time_ns(indio_dev
));
194 if (ret
& TMP007_STATUS_OLF
)
195 iio_push_event(indio_dev
,
196 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
200 iio_get_time_ns(indio_dev
));
202 if (ret
& TMP007_STATUS_LHF
)
203 iio_push_event(indio_dev
,
204 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
205 IIO_MOD_TEMP_AMBIENT
,
208 iio_get_time_ns(indio_dev
));
210 if (ret
& TMP007_STATUS_LLF
)
211 iio_push_event(indio_dev
,
212 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
213 IIO_MOD_TEMP_AMBIENT
,
216 iio_get_time_ns(indio_dev
));
221 static int tmp007_write_event_config(struct iio_dev
*indio_dev
,
222 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
223 enum iio_event_direction dir
, int state
)
225 struct tmp007_data
*data
= iio_priv(indio_dev
);
226 unsigned int status_mask
;
229 switch (chan
->channel2
) {
230 case IIO_MOD_TEMP_AMBIENT
:
231 if (dir
== IIO_EV_DIR_RISING
)
232 status_mask
= TMP007_STATUS_LHF
;
234 status_mask
= TMP007_STATUS_LLF
;
236 case IIO_MOD_TEMP_OBJECT
:
237 if (dir
== IIO_EV_DIR_RISING
)
238 status_mask
= TMP007_STATUS_OHF
;
240 status_mask
= TMP007_STATUS_OLF
;
246 mutex_lock(&data
->lock
);
247 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_STATUS_MASK
);
248 mutex_unlock(&data
->lock
);
257 return i2c_smbus_write_word_swapped(data
->client
, TMP007_STATUS_MASK
,
258 data
->status_mask
= ret
);
261 static int tmp007_read_event_config(struct iio_dev
*indio_dev
,
262 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
263 enum iio_event_direction dir
)
265 struct tmp007_data
*data
= iio_priv(indio_dev
);
268 switch (chan
->channel2
) {
269 case IIO_MOD_TEMP_AMBIENT
:
270 if (dir
== IIO_EV_DIR_RISING
)
271 mask
= TMP007_STATUS_LHF
;
273 mask
= TMP007_STATUS_LLF
;
275 case IIO_MOD_TEMP_OBJECT
:
276 if (dir
== IIO_EV_DIR_RISING
)
277 mask
= TMP007_STATUS_OHF
;
279 mask
= TMP007_STATUS_OLF
;
285 return !!(data
->status_mask
& mask
);
288 static int tmp007_read_thresh(struct iio_dev
*indio_dev
,
289 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
290 enum iio_event_direction dir
, enum iio_event_info info
,
293 struct tmp007_data
*data
= iio_priv(indio_dev
);
297 switch (chan
->channel2
) {
298 case IIO_MOD_TEMP_AMBIENT
: /* LSB: 0.5 degree Celsius */
299 if (dir
== IIO_EV_DIR_RISING
)
300 reg
= TMP007_TDIE_HIGH_LIMIT
;
302 reg
= TMP007_TDIE_LOW_LIMIT
;
304 case IIO_MOD_TEMP_OBJECT
:
305 if (dir
== IIO_EV_DIR_RISING
)
306 reg
= TMP007_TOBJ_HIGH_LIMIT
;
308 reg
= TMP007_TOBJ_LOW_LIMIT
;
314 ret
= i2c_smbus_read_word_swapped(data
->client
, reg
);
318 /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
319 *val
= sign_extend32(ret
, 15) >> 7;
324 static int tmp007_write_thresh(struct iio_dev
*indio_dev
,
325 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
326 enum iio_event_direction dir
, enum iio_event_info info
,
329 struct tmp007_data
*data
= iio_priv(indio_dev
);
332 switch (chan
->channel2
) {
333 case IIO_MOD_TEMP_AMBIENT
:
334 if (dir
== IIO_EV_DIR_RISING
)
335 reg
= TMP007_TDIE_HIGH_LIMIT
;
337 reg
= TMP007_TDIE_LOW_LIMIT
;
339 case IIO_MOD_TEMP_OBJECT
:
340 if (dir
== IIO_EV_DIR_RISING
)
341 reg
= TMP007_TOBJ_HIGH_LIMIT
;
343 reg
= TMP007_TOBJ_LOW_LIMIT
;
349 /* Full scale threshold value is +/- 256 degree Celsius */
350 if (val
< -256 || val
> 255)
353 /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
354 return i2c_smbus_write_word_swapped(data
->client
, reg
, (val
<< 7));
357 static IIO_CONST_ATTR(sampling_frequency_available
, "4 2 1 0.5 0.25");
359 static struct attribute
*tmp007_attributes
[] = {
360 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
364 static const struct attribute_group tmp007_attribute_group
= {
365 .attrs
= tmp007_attributes
,
368 static const struct iio_event_spec tmp007_obj_event
[] = {
370 .type
= IIO_EV_TYPE_THRESH
,
371 .dir
= IIO_EV_DIR_RISING
,
372 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
373 BIT(IIO_EV_INFO_ENABLE
),
376 .type
= IIO_EV_TYPE_THRESH
,
377 .dir
= IIO_EV_DIR_FALLING
,
378 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
379 BIT(IIO_EV_INFO_ENABLE
),
383 static const struct iio_event_spec tmp007_die_event
[] = {
385 .type
= IIO_EV_TYPE_THRESH
,
386 .dir
= IIO_EV_DIR_RISING
,
387 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
388 BIT(IIO_EV_INFO_ENABLE
),
391 .type
= IIO_EV_TYPE_THRESH
,
392 .dir
= IIO_EV_DIR_FALLING
,
393 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
394 BIT(IIO_EV_INFO_ENABLE
),
398 static const struct iio_chan_spec tmp007_channels
[] = {
402 .channel2
= IIO_MOD_TEMP_AMBIENT
,
403 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
404 BIT(IIO_CHAN_INFO_SCALE
),
405 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
406 .event_spec
= tmp007_die_event
,
407 .num_event_specs
= ARRAY_SIZE(tmp007_die_event
),
412 .channel2
= IIO_MOD_TEMP_OBJECT
,
413 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
414 BIT(IIO_CHAN_INFO_SCALE
),
415 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
416 .event_spec
= tmp007_obj_event
,
417 .num_event_specs
= ARRAY_SIZE(tmp007_obj_event
),
421 static const struct iio_info tmp007_info
= {
422 .read_raw
= tmp007_read_raw
,
423 .write_raw
= tmp007_write_raw
,
424 .read_event_config
= tmp007_read_event_config
,
425 .write_event_config
= tmp007_write_event_config
,
426 .read_event_value
= tmp007_read_thresh
,
427 .write_event_value
= tmp007_write_thresh
,
428 .attrs
= &tmp007_attribute_group
,
431 static bool tmp007_identify(struct i2c_client
*client
)
435 manf_id
= i2c_smbus_read_word_swapped(client
, TMP007_MANUFACTURER_ID
);
439 dev_id
= i2c_smbus_read_word_swapped(client
, TMP007_DEVICE_ID
);
443 return (manf_id
== TMP007_MANUFACTURER_MAGIC
&& dev_id
== TMP007_DEVICE_MAGIC
);
446 static int tmp007_probe(struct i2c_client
*client
,
447 const struct i2c_device_id
*tmp007_id
)
449 struct tmp007_data
*data
;
450 struct iio_dev
*indio_dev
;
453 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
456 if (!tmp007_identify(client
)) {
457 dev_err(&client
->dev
, "TMP007 not found\n");
461 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
465 data
= iio_priv(indio_dev
);
466 i2c_set_clientdata(client
, indio_dev
);
467 data
->client
= client
;
468 mutex_init(&data
->lock
);
470 indio_dev
->dev
.parent
= &client
->dev
;
471 indio_dev
->name
= "tmp007";
472 indio_dev
->modes
= INDIO_DIRECT_MODE
;
473 indio_dev
->info
= &tmp007_info
;
475 indio_dev
->channels
= tmp007_channels
;
476 indio_dev
->num_channels
= ARRAY_SIZE(tmp007_channels
);
479 * Set Configuration register:
482 * 3. Transient correction enable
485 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_CONFIG
);
490 data
->config
|= (TMP007_CONFIG_CONV_EN
| TMP007_CONFIG_ALERT_EN
| TMP007_CONFIG_TC_EN
);
492 ret
= i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
498 * Only the following flags can activate ALERT pin. Data conversion/validity flags
499 * flags can still be polled for getting temperature data
501 * Set Status Mask register:
502 * 1. Object temperature high limit enable
503 * 2. Object temperature low limit enable
504 * 3. TDIE temperature high limit enable
505 * 4. TDIE temperature low limit enable
508 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_STATUS_MASK
);
510 goto error_powerdown
;
512 data
->status_mask
= ret
;
513 data
->status_mask
|= (TMP007_STATUS_OHF
| TMP007_STATUS_OLF
514 | TMP007_STATUS_LHF
| TMP007_STATUS_LLF
);
516 ret
= i2c_smbus_write_word_swapped(data
->client
, TMP007_STATUS_MASK
, data
->status_mask
);
518 goto error_powerdown
;
521 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
522 NULL
, tmp007_interrupt_handler
,
523 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
524 tmp007_id
->name
, indio_dev
);
526 dev_err(&client
->dev
, "irq request error %d\n", -ret
);
527 goto error_powerdown
;
531 return iio_device_register(indio_dev
);
534 tmp007_powerdown(data
);
539 static int tmp007_remove(struct i2c_client
*client
)
541 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
542 struct tmp007_data
*data
= iio_priv(indio_dev
);
544 iio_device_unregister(indio_dev
);
545 tmp007_powerdown(data
);
550 #ifdef CONFIG_PM_SLEEP
551 static int tmp007_suspend(struct device
*dev
)
553 struct tmp007_data
*data
= iio_priv(i2c_get_clientdata(
554 to_i2c_client(dev
)));
556 return tmp007_powerdown(data
);
559 static int tmp007_resume(struct device
*dev
)
561 struct tmp007_data
*data
= iio_priv(i2c_get_clientdata(
562 to_i2c_client(dev
)));
564 return i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
565 data
->config
| TMP007_CONFIG_CONV_EN
);
569 static SIMPLE_DEV_PM_OPS(tmp007_pm_ops
, tmp007_suspend
, tmp007_resume
);
571 static const struct of_device_id tmp007_of_match
[] = {
572 { .compatible
= "ti,tmp007", },
575 MODULE_DEVICE_TABLE(of
, tmp007_of_match
);
577 static const struct i2c_device_id tmp007_id
[] = {
581 MODULE_DEVICE_TABLE(i2c
, tmp007_id
);
583 static struct i2c_driver tmp007_driver
= {
586 .of_match_table
= of_match_ptr(tmp007_of_match
),
587 .pm
= &tmp007_pm_ops
,
589 .probe
= tmp007_probe
,
590 .remove
= tmp007_remove
,
591 .id_table
= tmp007_id
,
593 module_i2c_driver(tmp007_driver
);
595 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
596 MODULE_DESCRIPTION("TI TMP007 IR thermopile sensor driver");
597 MODULE_LICENSE("GPL");