1 // SPDX-License-Identifier: GPL-2.0-only
3 * tmp007.c - Support for TI TMP007 IR thermopile sensor with integrated math engine
5 * Copyright (c) 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
7 * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor
9 * (7-bit I2C slave address (0x40 - 0x47), changeable via ADR pins)
12 * 1. This driver assumes that the sensor has been calibrated beforehand
13 * 2. Limit threshold events are enabled at the start
14 * 3. Operating mode: INT
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/module.h>
22 #include <linux/bitops.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/events.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_TOBJ_HIGH_LIMIT 0x06
37 #define TMP007_TOBJ_LOW_LIMIT 0x07
38 #define TMP007_TDIE_HIGH_LIMIT 0x08
39 #define TMP007_TDIE_LOW_LIMIT 0x09
40 #define TMP007_MANUFACTURER_ID 0x1e
41 #define TMP007_DEVICE_ID 0x1f
43 #define TMP007_CONFIG_CONV_EN BIT(12)
44 #define TMP007_CONFIG_TC_EN BIT(6)
45 #define TMP007_CONFIG_CR_MASK GENMASK(11, 9)
46 #define TMP007_CONFIG_ALERT_EN BIT(8)
47 #define TMP007_CONFIG_CR_SHIFT 9
49 /* Status register flags */
50 #define TMP007_STATUS_ALERT BIT(15)
51 #define TMP007_STATUS_CONV_READY BIT(14)
52 #define TMP007_STATUS_OHF BIT(13)
53 #define TMP007_STATUS_OLF BIT(12)
54 #define TMP007_STATUS_LHF BIT(11)
55 #define TMP007_STATUS_LLF BIT(10)
56 #define TMP007_STATUS_DATA_VALID BIT(9)
58 #define TMP007_MANUFACTURER_MAGIC 0x5449
59 #define TMP007_DEVICE_MAGIC 0x0078
61 #define TMP007_TEMP_SHIFT 2
64 struct i2c_client
*client
;
70 static const int tmp007_avgs
[5][2] = { {4, 0}, {2, 0}, {1, 0},
71 {0, 500000}, {0, 250000} };
73 static int tmp007_read_temperature(struct tmp007_data
*data
, u8 reg
)
79 ret
= i2c_smbus_read_word_swapped(data
->client
,
83 if ((ret
& TMP007_STATUS_CONV_READY
) &&
84 !(ret
& TMP007_STATUS_DATA_VALID
))
92 return i2c_smbus_read_word_swapped(data
->client
, reg
);
95 static int tmp007_powerdown(struct tmp007_data
*data
)
97 return i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
98 data
->config
& ~TMP007_CONFIG_CONV_EN
);
101 static int tmp007_read_raw(struct iio_dev
*indio_dev
,
102 struct iio_chan_spec
const *channel
, int *val
,
103 int *val2
, long mask
)
105 struct tmp007_data
*data
= iio_priv(indio_dev
);
110 case IIO_CHAN_INFO_RAW
:
111 switch (channel
->channel2
) {
112 case IIO_MOD_TEMP_AMBIENT
: /* LSB: 0.03125 degree Celsius */
113 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_TDIE
);
117 case IIO_MOD_TEMP_OBJECT
:
118 ret
= tmp007_read_temperature(data
, TMP007_TOBJECT
);
126 *val
= sign_extend32(ret
, 15) >> TMP007_TEMP_SHIFT
;
129 case IIO_CHAN_INFO_SCALE
:
133 return IIO_VAL_INT_PLUS_MICRO
;
134 case IIO_CHAN_INFO_SAMP_FREQ
:
135 conv_rate
= (data
->config
& TMP007_CONFIG_CR_MASK
)
136 >> TMP007_CONFIG_CR_SHIFT
;
137 *val
= tmp007_avgs
[conv_rate
][0];
138 *val2
= tmp007_avgs
[conv_rate
][1];
140 return IIO_VAL_INT_PLUS_MICRO
;
146 static int tmp007_write_raw(struct iio_dev
*indio_dev
,
147 struct iio_chan_spec
const *channel
, int val
,
150 struct tmp007_data
*data
= iio_priv(indio_dev
);
154 if (mask
== IIO_CHAN_INFO_SAMP_FREQ
) {
155 for (i
= 0; i
< ARRAY_SIZE(tmp007_avgs
); i
++) {
156 if ((val
== tmp007_avgs
[i
][0]) &&
157 (val2
== tmp007_avgs
[i
][1])) {
158 tmp
= data
->config
& ~TMP007_CONFIG_CR_MASK
;
159 tmp
|= (i
<< TMP007_CONFIG_CR_SHIFT
);
161 return i2c_smbus_write_word_swapped(data
->client
,
171 static irqreturn_t
tmp007_interrupt_handler(int irq
, void *private)
173 struct iio_dev
*indio_dev
= private;
174 struct tmp007_data
*data
= iio_priv(indio_dev
);
177 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_STATUS
);
178 if ((ret
< 0) || !(ret
& (TMP007_STATUS_OHF
| TMP007_STATUS_OLF
|
179 TMP007_STATUS_LHF
| TMP007_STATUS_LLF
)))
182 if (ret
& TMP007_STATUS_OHF
)
183 iio_push_event(indio_dev
,
184 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
188 iio_get_time_ns(indio_dev
));
190 if (ret
& TMP007_STATUS_OLF
)
191 iio_push_event(indio_dev
,
192 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
196 iio_get_time_ns(indio_dev
));
198 if (ret
& TMP007_STATUS_LHF
)
199 iio_push_event(indio_dev
,
200 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
201 IIO_MOD_TEMP_AMBIENT
,
204 iio_get_time_ns(indio_dev
));
206 if (ret
& TMP007_STATUS_LLF
)
207 iio_push_event(indio_dev
,
208 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
209 IIO_MOD_TEMP_AMBIENT
,
212 iio_get_time_ns(indio_dev
));
217 static int tmp007_write_event_config(struct iio_dev
*indio_dev
,
218 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
219 enum iio_event_direction dir
, int state
)
221 struct tmp007_data
*data
= iio_priv(indio_dev
);
222 unsigned int status_mask
;
225 switch (chan
->channel2
) {
226 case IIO_MOD_TEMP_AMBIENT
:
227 if (dir
== IIO_EV_DIR_RISING
)
228 status_mask
= TMP007_STATUS_LHF
;
230 status_mask
= TMP007_STATUS_LLF
;
232 case IIO_MOD_TEMP_OBJECT
:
233 if (dir
== IIO_EV_DIR_RISING
)
234 status_mask
= TMP007_STATUS_OHF
;
236 status_mask
= TMP007_STATUS_OLF
;
242 mutex_lock(&data
->lock
);
243 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_STATUS_MASK
);
244 mutex_unlock(&data
->lock
);
253 return i2c_smbus_write_word_swapped(data
->client
, TMP007_STATUS_MASK
,
254 data
->status_mask
= ret
);
257 static int tmp007_read_event_config(struct iio_dev
*indio_dev
,
258 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
259 enum iio_event_direction dir
)
261 struct tmp007_data
*data
= iio_priv(indio_dev
);
264 switch (chan
->channel2
) {
265 case IIO_MOD_TEMP_AMBIENT
:
266 if (dir
== IIO_EV_DIR_RISING
)
267 mask
= TMP007_STATUS_LHF
;
269 mask
= TMP007_STATUS_LLF
;
271 case IIO_MOD_TEMP_OBJECT
:
272 if (dir
== IIO_EV_DIR_RISING
)
273 mask
= TMP007_STATUS_OHF
;
275 mask
= TMP007_STATUS_OLF
;
281 return !!(data
->status_mask
& mask
);
284 static int tmp007_read_thresh(struct iio_dev
*indio_dev
,
285 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
286 enum iio_event_direction dir
, enum iio_event_info info
,
289 struct tmp007_data
*data
= iio_priv(indio_dev
);
293 switch (chan
->channel2
) {
294 case IIO_MOD_TEMP_AMBIENT
: /* LSB: 0.5 degree Celsius */
295 if (dir
== IIO_EV_DIR_RISING
)
296 reg
= TMP007_TDIE_HIGH_LIMIT
;
298 reg
= TMP007_TDIE_LOW_LIMIT
;
300 case IIO_MOD_TEMP_OBJECT
:
301 if (dir
== IIO_EV_DIR_RISING
)
302 reg
= TMP007_TOBJ_HIGH_LIMIT
;
304 reg
= TMP007_TOBJ_LOW_LIMIT
;
310 ret
= i2c_smbus_read_word_swapped(data
->client
, reg
);
314 /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
315 *val
= sign_extend32(ret
, 15) >> 7;
320 static int tmp007_write_thresh(struct iio_dev
*indio_dev
,
321 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
322 enum iio_event_direction dir
, enum iio_event_info info
,
325 struct tmp007_data
*data
= iio_priv(indio_dev
);
328 switch (chan
->channel2
) {
329 case IIO_MOD_TEMP_AMBIENT
:
330 if (dir
== IIO_EV_DIR_RISING
)
331 reg
= TMP007_TDIE_HIGH_LIMIT
;
333 reg
= TMP007_TDIE_LOW_LIMIT
;
335 case IIO_MOD_TEMP_OBJECT
:
336 if (dir
== IIO_EV_DIR_RISING
)
337 reg
= TMP007_TOBJ_HIGH_LIMIT
;
339 reg
= TMP007_TOBJ_LOW_LIMIT
;
345 /* Full scale threshold value is +/- 256 degree Celsius */
346 if (val
< -256 || val
> 255)
349 /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
350 return i2c_smbus_write_word_swapped(data
->client
, reg
, (val
<< 7));
353 static IIO_CONST_ATTR(sampling_frequency_available
, "4 2 1 0.5 0.25");
355 static struct attribute
*tmp007_attributes
[] = {
356 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
360 static const struct attribute_group tmp007_attribute_group
= {
361 .attrs
= tmp007_attributes
,
364 static const struct iio_event_spec tmp007_obj_event
[] = {
366 .type
= IIO_EV_TYPE_THRESH
,
367 .dir
= IIO_EV_DIR_RISING
,
368 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
369 BIT(IIO_EV_INFO_ENABLE
),
372 .type
= IIO_EV_TYPE_THRESH
,
373 .dir
= IIO_EV_DIR_FALLING
,
374 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
375 BIT(IIO_EV_INFO_ENABLE
),
379 static const struct iio_event_spec tmp007_die_event
[] = {
381 .type
= IIO_EV_TYPE_THRESH
,
382 .dir
= IIO_EV_DIR_RISING
,
383 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
384 BIT(IIO_EV_INFO_ENABLE
),
387 .type
= IIO_EV_TYPE_THRESH
,
388 .dir
= IIO_EV_DIR_FALLING
,
389 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
390 BIT(IIO_EV_INFO_ENABLE
),
394 static const struct iio_chan_spec tmp007_channels
[] = {
398 .channel2
= IIO_MOD_TEMP_AMBIENT
,
399 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
400 BIT(IIO_CHAN_INFO_SCALE
),
401 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
402 .event_spec
= tmp007_die_event
,
403 .num_event_specs
= ARRAY_SIZE(tmp007_die_event
),
408 .channel2
= IIO_MOD_TEMP_OBJECT
,
409 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
410 BIT(IIO_CHAN_INFO_SCALE
),
411 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
412 .event_spec
= tmp007_obj_event
,
413 .num_event_specs
= ARRAY_SIZE(tmp007_obj_event
),
417 static const struct iio_info tmp007_info
= {
418 .read_raw
= tmp007_read_raw
,
419 .write_raw
= tmp007_write_raw
,
420 .read_event_config
= tmp007_read_event_config
,
421 .write_event_config
= tmp007_write_event_config
,
422 .read_event_value
= tmp007_read_thresh
,
423 .write_event_value
= tmp007_write_thresh
,
424 .attrs
= &tmp007_attribute_group
,
427 static bool tmp007_identify(struct i2c_client
*client
)
431 manf_id
= i2c_smbus_read_word_swapped(client
, TMP007_MANUFACTURER_ID
);
435 dev_id
= i2c_smbus_read_word_swapped(client
, TMP007_DEVICE_ID
);
439 return (manf_id
== TMP007_MANUFACTURER_MAGIC
&& dev_id
== TMP007_DEVICE_MAGIC
);
442 static int tmp007_probe(struct i2c_client
*client
,
443 const struct i2c_device_id
*tmp007_id
)
445 struct tmp007_data
*data
;
446 struct iio_dev
*indio_dev
;
449 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
452 if (!tmp007_identify(client
)) {
453 dev_err(&client
->dev
, "TMP007 not found\n");
457 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
461 data
= iio_priv(indio_dev
);
462 i2c_set_clientdata(client
, indio_dev
);
463 data
->client
= client
;
464 mutex_init(&data
->lock
);
466 indio_dev
->name
= "tmp007";
467 indio_dev
->modes
= INDIO_DIRECT_MODE
;
468 indio_dev
->info
= &tmp007_info
;
470 indio_dev
->channels
= tmp007_channels
;
471 indio_dev
->num_channels
= ARRAY_SIZE(tmp007_channels
);
474 * Set Configuration register:
477 * 3. Transient correction enable
480 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_CONFIG
);
485 data
->config
|= (TMP007_CONFIG_CONV_EN
| TMP007_CONFIG_ALERT_EN
| TMP007_CONFIG_TC_EN
);
487 ret
= i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
493 * Only the following flags can activate ALERT pin. Data conversion/validity flags
494 * flags can still be polled for getting temperature data
496 * Set Status Mask register:
497 * 1. Object temperature high limit enable
498 * 2. Object temperature low limit enable
499 * 3. TDIE temperature high limit enable
500 * 4. TDIE temperature low limit enable
503 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP007_STATUS_MASK
);
505 goto error_powerdown
;
507 data
->status_mask
= ret
;
508 data
->status_mask
|= (TMP007_STATUS_OHF
| TMP007_STATUS_OLF
509 | TMP007_STATUS_LHF
| TMP007_STATUS_LLF
);
511 ret
= i2c_smbus_write_word_swapped(data
->client
, TMP007_STATUS_MASK
, data
->status_mask
);
513 goto error_powerdown
;
516 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
517 NULL
, tmp007_interrupt_handler
,
518 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
519 tmp007_id
->name
, indio_dev
);
521 dev_err(&client
->dev
, "irq request error %d\n", -ret
);
522 goto error_powerdown
;
526 return iio_device_register(indio_dev
);
529 tmp007_powerdown(data
);
534 static int tmp007_remove(struct i2c_client
*client
)
536 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
537 struct tmp007_data
*data
= iio_priv(indio_dev
);
539 iio_device_unregister(indio_dev
);
540 tmp007_powerdown(data
);
545 #ifdef CONFIG_PM_SLEEP
546 static int tmp007_suspend(struct device
*dev
)
548 struct tmp007_data
*data
= iio_priv(i2c_get_clientdata(
549 to_i2c_client(dev
)));
551 return tmp007_powerdown(data
);
554 static int tmp007_resume(struct device
*dev
)
556 struct tmp007_data
*data
= iio_priv(i2c_get_clientdata(
557 to_i2c_client(dev
)));
559 return i2c_smbus_write_word_swapped(data
->client
, TMP007_CONFIG
,
560 data
->config
| TMP007_CONFIG_CONV_EN
);
564 static SIMPLE_DEV_PM_OPS(tmp007_pm_ops
, tmp007_suspend
, tmp007_resume
);
566 static const struct of_device_id tmp007_of_match
[] = {
567 { .compatible
= "ti,tmp007", },
570 MODULE_DEVICE_TABLE(of
, tmp007_of_match
);
572 static const struct i2c_device_id tmp007_id
[] = {
576 MODULE_DEVICE_TABLE(i2c
, tmp007_id
);
578 static struct i2c_driver tmp007_driver
= {
581 .of_match_table
= tmp007_of_match
,
582 .pm
= &tmp007_pm_ops
,
584 .probe
= tmp007_probe
,
585 .remove
= tmp007_remove
,
586 .id_table
= tmp007_id
,
588 module_i2c_driver(tmp007_driver
);
590 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
591 MODULE_DESCRIPTION("TI TMP007 IR thermopile sensor driver");
592 MODULE_LICENSE("GPL");