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>
19 #include <linux/bitops.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define TMP006_VOBJECT 0x00
25 #define TMP006_TAMBIENT 0x01
26 #define TMP006_CONFIG 0x02
27 #define TMP006_MANUFACTURER_ID 0xfe
28 #define TMP006_DEVICE_ID 0xff
30 #define TMP006_TAMBIENT_SHIFT 2
32 #define TMP006_CONFIG_RESET BIT(15)
33 #define TMP006_CONFIG_DRDY_EN BIT(8)
34 #define TMP006_CONFIG_DRDY BIT(7)
36 #define TMP006_CONFIG_MOD_MASK GENMASK(14, 12)
38 #define TMP006_CONFIG_CR_MASK GENMASK(11, 9)
39 #define TMP006_CONFIG_CR_SHIFT 9
41 #define TMP006_MANUFACTURER_MAGIC 0x5449
42 #define TMP006_DEVICE_MAGIC 0x0067
45 struct i2c_client
*client
;
49 static int tmp006_read_measurement(struct tmp006_data
*data
, u8 reg
)
55 ret
= i2c_smbus_read_word_swapped(data
->client
,
59 if (ret
& TMP006_CONFIG_DRDY
)
67 return i2c_smbus_read_word_swapped(data
->client
, reg
);
70 static const int tmp006_freqs
[5][2] = { {4, 0}, {2, 0}, {1, 0},
71 {0, 500000}, {0, 250000} };
73 static int tmp006_read_raw(struct iio_dev
*indio_dev
,
74 struct iio_chan_spec
const *channel
, int *val
,
77 struct tmp006_data
*data
= iio_priv(indio_dev
);
82 case IIO_CHAN_INFO_RAW
:
83 if (channel
->type
== IIO_VOLTAGE
) {
84 /* LSB is 156.25 nV */
85 ret
= tmp006_read_measurement(data
, TMP006_VOBJECT
);
88 *val
= sign_extend32(ret
, 15);
89 } else if (channel
->type
== IIO_TEMP
) {
90 /* LSB is 0.03125 degrees Celsius */
91 ret
= tmp006_read_measurement(data
, TMP006_TAMBIENT
);
94 *val
= sign_extend32(ret
, 15) >> TMP006_TAMBIENT_SHIFT
;
99 case IIO_CHAN_INFO_SCALE
:
100 if (channel
->type
== IIO_VOLTAGE
) {
103 } else if (channel
->type
== IIO_TEMP
) {
109 return IIO_VAL_INT_PLUS_MICRO
;
110 case IIO_CHAN_INFO_SAMP_FREQ
:
111 cr
= (data
->config
& TMP006_CONFIG_CR_MASK
)
112 >> TMP006_CONFIG_CR_SHIFT
;
113 *val
= tmp006_freqs
[cr
][0];
114 *val2
= tmp006_freqs
[cr
][1];
115 return IIO_VAL_INT_PLUS_MICRO
;
123 static int tmp006_write_raw(struct iio_dev
*indio_dev
,
124 struct iio_chan_spec
const *chan
,
129 struct tmp006_data
*data
= iio_priv(indio_dev
);
132 if (mask
!= IIO_CHAN_INFO_SAMP_FREQ
)
135 for (i
= 0; i
< ARRAY_SIZE(tmp006_freqs
); i
++)
136 if ((val
== tmp006_freqs
[i
][0]) &&
137 (val2
== tmp006_freqs
[i
][1])) {
138 data
->config
&= ~TMP006_CONFIG_CR_MASK
;
139 data
->config
|= i
<< TMP006_CONFIG_CR_SHIFT
;
141 return i2c_smbus_write_word_swapped(data
->client
,
149 static IIO_CONST_ATTR(sampling_frequency_available
, "4 2 1 0.5 0.25");
151 static struct attribute
*tmp006_attributes
[] = {
152 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
156 static const struct attribute_group tmp006_attribute_group
= {
157 .attrs
= tmp006_attributes
,
160 static const struct iio_chan_spec tmp006_channels
[] = {
163 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
164 BIT(IIO_CHAN_INFO_SCALE
),
165 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
169 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
170 BIT(IIO_CHAN_INFO_SCALE
),
171 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
175 static const struct iio_info tmp006_info
= {
176 .read_raw
= tmp006_read_raw
,
177 .write_raw
= tmp006_write_raw
,
178 .attrs
= &tmp006_attribute_group
,
181 static bool tmp006_check_identification(struct i2c_client
*client
)
185 mid
= i2c_smbus_read_word_swapped(client
, TMP006_MANUFACTURER_ID
);
189 did
= i2c_smbus_read_word_swapped(client
, TMP006_DEVICE_ID
);
193 return mid
== TMP006_MANUFACTURER_MAGIC
&& did
== TMP006_DEVICE_MAGIC
;
196 static int tmp006_probe(struct i2c_client
*client
,
197 const struct i2c_device_id
*id
)
199 struct iio_dev
*indio_dev
;
200 struct tmp006_data
*data
;
203 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
206 if (!tmp006_check_identification(client
)) {
207 dev_err(&client
->dev
, "no TMP006 sensor\n");
211 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
215 data
= iio_priv(indio_dev
);
216 i2c_set_clientdata(client
, indio_dev
);
217 data
->client
= client
;
219 indio_dev
->name
= dev_name(&client
->dev
);
220 indio_dev
->modes
= INDIO_DIRECT_MODE
;
221 indio_dev
->info
= &tmp006_info
;
223 indio_dev
->channels
= tmp006_channels
;
224 indio_dev
->num_channels
= ARRAY_SIZE(tmp006_channels
);
226 ret
= i2c_smbus_read_word_swapped(data
->client
, TMP006_CONFIG
);
231 return iio_device_register(indio_dev
);
234 static int tmp006_powerdown(struct tmp006_data
*data
)
236 return i2c_smbus_write_word_swapped(data
->client
, TMP006_CONFIG
,
237 data
->config
& ~TMP006_CONFIG_MOD_MASK
);
240 static int tmp006_remove(struct i2c_client
*client
)
242 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
244 iio_device_unregister(indio_dev
);
245 tmp006_powerdown(iio_priv(indio_dev
));
250 #ifdef CONFIG_PM_SLEEP
251 static int tmp006_suspend(struct device
*dev
)
253 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
254 return tmp006_powerdown(iio_priv(indio_dev
));
257 static int tmp006_resume(struct device
*dev
)
259 struct tmp006_data
*data
= iio_priv(i2c_get_clientdata(
260 to_i2c_client(dev
)));
261 return i2c_smbus_write_word_swapped(data
->client
, TMP006_CONFIG
,
262 data
->config
| TMP006_CONFIG_MOD_MASK
);
266 static SIMPLE_DEV_PM_OPS(tmp006_pm_ops
, tmp006_suspend
, tmp006_resume
);
268 static const struct i2c_device_id tmp006_id
[] = {
272 MODULE_DEVICE_TABLE(i2c
, tmp006_id
);
274 static struct i2c_driver tmp006_driver
= {
277 .pm
= &tmp006_pm_ops
,
279 .probe
= tmp006_probe
,
280 .remove
= tmp006_remove
,
281 .id_table
= tmp006_id
,
283 module_i2c_driver(tmp006_driver
);
285 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
286 MODULE_DESCRIPTION("TI TMP006 IR thermopile sensor driver");
287 MODULE_LICENSE("GPL");