1 // SPDX-License-Identifier: GPL-2.0-only
3 * tmp006.c - Support for TI TMP006 IR thermopile sensor
5 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
7 * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor
9 * (7-bit I2C slave address 0x40, changeable via ADR pins)
11 * TODO: data ready irq
14 #include <linux/err.h>
15 #include <linux/i2c.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
20 #include <linux/bitops.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 #define TMP006_VOBJECT 0x00
26 #define TMP006_TAMBIENT 0x01
27 #define TMP006_CONFIG 0x02
28 #define TMP006_MANUFACTURER_ID 0xfe
29 #define TMP006_DEVICE_ID 0xff
31 #define TMP006_TAMBIENT_SHIFT 2
33 #define TMP006_CONFIG_RESET BIT(15)
34 #define TMP006_CONFIG_DRDY_EN BIT(8)
35 #define TMP006_CONFIG_DRDY BIT(7)
37 #define TMP006_CONFIG_MOD_MASK GENMASK(14, 12)
39 #define TMP006_CONFIG_CR_MASK GENMASK(11, 9)
40 #define TMP006_CONFIG_CR_SHIFT 9
42 #define TMP006_MANUFACTURER_MAGIC 0x5449
43 #define TMP006_DEVICE_MAGIC 0x0067
46 struct i2c_client
*client
;
50 static int tmp006_read_measurement(struct tmp006_data
*data
, u8 reg
)
56 ret
= i2c_smbus_read_word_swapped(data
->client
,
60 if (ret
& TMP006_CONFIG_DRDY
)
68 return i2c_smbus_read_word_swapped(data
->client
, reg
);
71 static const int tmp006_freqs
[5][2] = { {4, 0}, {2, 0}, {1, 0},
72 {0, 500000}, {0, 250000} };
74 static int tmp006_read_raw(struct iio_dev
*indio_dev
,
75 struct iio_chan_spec
const *channel
, int *val
,
78 struct tmp006_data
*data
= iio_priv(indio_dev
);
83 case IIO_CHAN_INFO_RAW
:
84 if (channel
->type
== IIO_VOLTAGE
) {
85 /* LSB is 156.25 nV */
86 ret
= tmp006_read_measurement(data
, TMP006_VOBJECT
);
89 *val
= sign_extend32(ret
, 15);
90 } else if (channel
->type
== IIO_TEMP
) {
91 /* LSB is 0.03125 degrees Celsius */
92 ret
= tmp006_read_measurement(data
, TMP006_TAMBIENT
);
95 *val
= sign_extend32(ret
, 15) >> TMP006_TAMBIENT_SHIFT
;
100 case IIO_CHAN_INFO_SCALE
:
101 if (channel
->type
== IIO_VOLTAGE
) {
104 } else if (channel
->type
== IIO_TEMP
) {
110 return IIO_VAL_INT_PLUS_MICRO
;
111 case IIO_CHAN_INFO_SAMP_FREQ
:
112 cr
= (data
->config
& TMP006_CONFIG_CR_MASK
)
113 >> TMP006_CONFIG_CR_SHIFT
;
114 *val
= tmp006_freqs
[cr
][0];
115 *val2
= tmp006_freqs
[cr
][1];
116 return IIO_VAL_INT_PLUS_MICRO
;
124 static int tmp006_write_raw(struct iio_dev
*indio_dev
,
125 struct iio_chan_spec
const *chan
,
130 struct tmp006_data
*data
= iio_priv(indio_dev
);
133 if (mask
!= IIO_CHAN_INFO_SAMP_FREQ
)
136 for (i
= 0; i
< ARRAY_SIZE(tmp006_freqs
); i
++)
137 if ((val
== tmp006_freqs
[i
][0]) &&
138 (val2
== tmp006_freqs
[i
][1])) {
139 data
->config
&= ~TMP006_CONFIG_CR_MASK
;
140 data
->config
|= i
<< TMP006_CONFIG_CR_SHIFT
;
142 return i2c_smbus_write_word_swapped(data
->client
,
150 static IIO_CONST_ATTR(sampling_frequency_available
, "4 2 1 0.5 0.25");
152 static struct attribute
*tmp006_attributes
[] = {
153 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
157 static const struct attribute_group tmp006_attribute_group
= {
158 .attrs
= tmp006_attributes
,
161 static const struct iio_chan_spec tmp006_channels
[] = {
164 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
165 BIT(IIO_CHAN_INFO_SCALE
),
166 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
170 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
171 BIT(IIO_CHAN_INFO_SCALE
),
172 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
176 static const struct iio_info tmp006_info
= {
177 .read_raw
= tmp006_read_raw
,
178 .write_raw
= tmp006_write_raw
,
179 .attrs
= &tmp006_attribute_group
,
182 static bool tmp006_check_identification(struct i2c_client
*client
)
186 mid
= i2c_smbus_read_word_swapped(client
, TMP006_MANUFACTURER_ID
);
190 did
= i2c_smbus_read_word_swapped(client
, TMP006_DEVICE_ID
);
194 return mid
== TMP006_MANUFACTURER_MAGIC
&& did
== TMP006_DEVICE_MAGIC
;
197 static int tmp006_power(struct device
*dev
, bool up
)
199 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
200 struct tmp006_data
*data
= iio_priv(indio_dev
);
203 data
->config
|= TMP006_CONFIG_MOD_MASK
;
205 data
->config
&= ~TMP006_CONFIG_MOD_MASK
;
207 return i2c_smbus_write_word_swapped(data
->client
, TMP006_CONFIG
,
211 static void tmp006_powerdown_cleanup(void *dev
)
213 tmp006_power(dev
, false);
216 static int tmp006_probe(struct i2c_client
*client
)
218 struct iio_dev
*indio_dev
;
219 struct tmp006_data
*data
;
222 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
225 if (!tmp006_check_identification(client
)) {
226 dev_err(&client
->dev
, "no TMP006 sensor\n");
230 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
234 data
= iio_priv(indio_dev
);
235 i2c_set_clientdata(client
, indio_dev
);
236 data
->client
= client
;
238 indio_dev
->name
= dev_name(&client
->dev
);
239 indio_dev
->modes
= INDIO_DIRECT_MODE
;
240 indio_dev
->info
= &tmp006_info
;
242 indio_dev
->channels
= tmp006_channels
;
243 indio_dev
->num_channels
= ARRAY_SIZE(tmp006_channels
);
245 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP006_CONFIG
);
250 if ((ret
& TMP006_CONFIG_MOD_MASK
) != TMP006_CONFIG_MOD_MASK
) {
251 ret
= tmp006_power(&client
->dev
, true);
256 ret
= devm_add_action_or_reset(&client
->dev
, tmp006_powerdown_cleanup
,
261 return devm_iio_device_register(&client
->dev
, indio_dev
);
264 static int tmp006_suspend(struct device
*dev
)
266 return tmp006_power(dev
, false);
269 static int tmp006_resume(struct device
*dev
)
271 return tmp006_power(dev
, true);
274 static DEFINE_SIMPLE_DEV_PM_OPS(tmp006_pm_ops
, tmp006_suspend
, tmp006_resume
);
276 static const struct of_device_id tmp006_of_match
[] = {
277 { .compatible
= "ti,tmp006" },
280 MODULE_DEVICE_TABLE(of
, tmp006_of_match
);
282 static const struct i2c_device_id tmp006_id
[] = {
286 MODULE_DEVICE_TABLE(i2c
, tmp006_id
);
288 static struct i2c_driver tmp006_driver
= {
291 .of_match_table
= tmp006_of_match
,
292 .pm
= pm_sleep_ptr(&tmp006_pm_ops
),
294 .probe
= tmp006_probe
,
295 .id_table
= tmp006_id
,
297 module_i2c_driver(tmp006_driver
);
299 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
300 MODULE_DESCRIPTION("TI TMP006 IR thermopile sensor driver");
301 MODULE_LICENSE("GPL");