WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / temperature / tmp007.c
blobad2b35c6554853dc41017d9c237f40562f1a36d6
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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)
11 * Note:
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>
21 #include <linux/pm.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
63 struct tmp007_data {
64 struct i2c_client *client;
65 struct mutex lock;
66 u16 config;
67 u16 status_mask;
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)
75 s32 ret;
76 int tries = 50;
78 while (tries-- > 0) {
79 ret = i2c_smbus_read_word_swapped(data->client,
80 TMP007_STATUS);
81 if (ret < 0)
82 return ret;
83 if ((ret & TMP007_STATUS_CONV_READY) &&
84 !(ret & TMP007_STATUS_DATA_VALID))
85 break;
86 msleep(100);
89 if (tries < 0)
90 return -EIO;
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);
106 s32 ret;
107 int conv_rate;
109 switch (mask) {
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);
114 if (ret < 0)
115 return ret;
116 break;
117 case IIO_MOD_TEMP_OBJECT:
118 ret = tmp007_read_temperature(data, TMP007_TOBJECT);
119 if (ret < 0)
120 return ret;
121 break;
122 default:
123 return -EINVAL;
126 *val = sign_extend32(ret, 15) >> TMP007_TEMP_SHIFT;
128 return IIO_VAL_INT;
129 case IIO_CHAN_INFO_SCALE:
130 *val = 31;
131 *val2 = 250000;
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;
141 default:
142 return -EINVAL;
146 static int tmp007_write_raw(struct iio_dev *indio_dev,
147 struct iio_chan_spec const *channel, int val,
148 int val2, long mask)
150 struct tmp007_data *data = iio_priv(indio_dev);
151 int i;
152 u16 tmp;
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,
162 TMP007_CONFIG,
163 data->config = tmp);
168 return -EINVAL;
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);
175 int ret;
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)))
180 return IRQ_NONE;
182 if (ret & TMP007_STATUS_OHF)
183 iio_push_event(indio_dev,
184 IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
185 IIO_MOD_TEMP_OBJECT,
186 IIO_EV_TYPE_THRESH,
187 IIO_EV_DIR_RISING),
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,
193 IIO_MOD_TEMP_OBJECT,
194 IIO_EV_TYPE_THRESH,
195 IIO_EV_DIR_FALLING),
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,
202 IIO_EV_TYPE_THRESH,
203 IIO_EV_DIR_RISING),
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,
210 IIO_EV_TYPE_THRESH,
211 IIO_EV_DIR_FALLING),
212 iio_get_time_ns(indio_dev));
214 return IRQ_HANDLED;
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;
223 int ret;
225 switch (chan->channel2) {
226 case IIO_MOD_TEMP_AMBIENT:
227 if (dir == IIO_EV_DIR_RISING)
228 status_mask = TMP007_STATUS_LHF;
229 else
230 status_mask = TMP007_STATUS_LLF;
231 break;
232 case IIO_MOD_TEMP_OBJECT:
233 if (dir == IIO_EV_DIR_RISING)
234 status_mask = TMP007_STATUS_OHF;
235 else
236 status_mask = TMP007_STATUS_OLF;
237 break;
238 default:
239 return -EINVAL;
242 mutex_lock(&data->lock);
243 ret = i2c_smbus_read_word_swapped(data->client, TMP007_STATUS_MASK);
244 mutex_unlock(&data->lock);
245 if (ret < 0)
246 return ret;
248 if (state)
249 ret |= status_mask;
250 else
251 ret &= ~status_mask;
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);
262 unsigned int mask;
264 switch (chan->channel2) {
265 case IIO_MOD_TEMP_AMBIENT:
266 if (dir == IIO_EV_DIR_RISING)
267 mask = TMP007_STATUS_LHF;
268 else
269 mask = TMP007_STATUS_LLF;
270 break;
271 case IIO_MOD_TEMP_OBJECT:
272 if (dir == IIO_EV_DIR_RISING)
273 mask = TMP007_STATUS_OHF;
274 else
275 mask = TMP007_STATUS_OLF;
276 break;
277 default:
278 return -EINVAL;
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,
287 int *val, int *val2)
289 struct tmp007_data *data = iio_priv(indio_dev);
290 int ret;
291 u8 reg;
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;
297 else
298 reg = TMP007_TDIE_LOW_LIMIT;
299 break;
300 case IIO_MOD_TEMP_OBJECT:
301 if (dir == IIO_EV_DIR_RISING)
302 reg = TMP007_TOBJ_HIGH_LIMIT;
303 else
304 reg = TMP007_TOBJ_LOW_LIMIT;
305 break;
306 default:
307 return -EINVAL;
310 ret = i2c_smbus_read_word_swapped(data->client, reg);
311 if (ret < 0)
312 return ret;
314 /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
315 *val = sign_extend32(ret, 15) >> 7;
317 return IIO_VAL_INT;
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,
323 int val, int val2)
325 struct tmp007_data *data = iio_priv(indio_dev);
326 u8 reg;
328 switch (chan->channel2) {
329 case IIO_MOD_TEMP_AMBIENT:
330 if (dir == IIO_EV_DIR_RISING)
331 reg = TMP007_TDIE_HIGH_LIMIT;
332 else
333 reg = TMP007_TDIE_LOW_LIMIT;
334 break;
335 case IIO_MOD_TEMP_OBJECT:
336 if (dir == IIO_EV_DIR_RISING)
337 reg = TMP007_TOBJ_HIGH_LIMIT;
338 else
339 reg = TMP007_TOBJ_LOW_LIMIT;
340 break;
341 default:
342 return -EINVAL;
345 /* Full scale threshold value is +/- 256 degree Celsius */
346 if (val < -256 || val > 255)
347 return -EINVAL;
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,
357 NULL
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[] = {
396 .type = IIO_TEMP,
397 .modified = 1,
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),
406 .type = IIO_TEMP,
407 .modified = 1,
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)
429 int manf_id, dev_id;
431 manf_id = i2c_smbus_read_word_swapped(client, TMP007_MANUFACTURER_ID);
432 if (manf_id < 0)
433 return false;
435 dev_id = i2c_smbus_read_word_swapped(client, TMP007_DEVICE_ID);
436 if (dev_id < 0)
437 return false;
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;
447 int ret;
449 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
450 return -EOPNOTSUPP;
452 if (!tmp007_identify(client)) {
453 dev_err(&client->dev, "TMP007 not found\n");
454 return -ENODEV;
457 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
458 if (!indio_dev)
459 return -ENOMEM;
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:
475 * 1. Conversion ON
476 * 2. ALERT enable
477 * 3. Transient correction enable
480 ret = i2c_smbus_read_word_swapped(data->client, TMP007_CONFIG);
481 if (ret < 0)
482 return ret;
484 data->config = ret;
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,
488 data->config);
489 if (ret < 0)
490 return ret;
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);
504 if (ret < 0)
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);
512 if (ret < 0)
513 goto error_powerdown;
515 if (client->irq) {
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);
520 if (ret) {
521 dev_err(&client->dev, "irq request error %d\n", -ret);
522 goto error_powerdown;
526 return iio_device_register(indio_dev);
528 error_powerdown:
529 tmp007_powerdown(data);
531 return ret;
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);
542 return 0;
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);
562 #endif
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", },
568 { },
570 MODULE_DEVICE_TABLE(of, tmp007_of_match);
572 static const struct i2c_device_id tmp007_id[] = {
573 { "tmp007", 0 },
576 MODULE_DEVICE_TABLE(i2c, tmp007_id);
578 static struct i2c_driver tmp007_driver = {
579 .driver = {
580 .name = "tmp007",
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");