1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * tmp007.c - Support for TI TMP007 IR thermopile sensor with integrated math engine
4 *
5 * Copyright (c) 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
6 *
7 * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor
8 *
9 * (7-bit I2C slave address (0x40 - 0x47), changeable via ADR pins)
10 *
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
15 */
16
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>
26
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/events.h>
30
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
42
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
48
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)
57
58 #define TMP007_MANUFACTURER_MAGIC 0x5449
59 #define TMP007_DEVICE_MAGIC 0x0078
60
61 #define TMP007_TEMP_SHIFT 2
62
63 struct tmp007_data {
64 struct i2c_client *client;
65 struct mutex lock;
66 u16 config;
67 u16 status_mask;
68 };
69
70 static const int tmp007_avgs[5][2] = { {4, 0}, {2, 0}, {1, 0},
71 {0, 500000}, {0, 250000} };
72
tmp007_read_temperature(struct tmp007_data * data,u8 reg)73 static int tmp007_read_temperature(struct tmp007_data *data, u8 reg)
74 {
75 s32 ret;
76 int tries = 50;
77
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);
87 }
88
89 if (tries < 0)
90 return -EIO;
91
92 return i2c_smbus_read_word_swapped(data->client, reg);
93 }
94
tmp007_powerdown(struct tmp007_data * data)95 static int tmp007_powerdown(struct tmp007_data *data)
96 {
97 return i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
98 data->config & ~TMP007_CONFIG_CONV_EN);
99 }
100
tmp007_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * val,int * val2,long mask)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)
104 {
105 struct tmp007_data *data = iio_priv(indio_dev);
106 s32 ret;
107 int conv_rate;
108
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;
124 }
125
126 *val = sign_extend32(ret, 15) >> TMP007_TEMP_SHIFT;
127
128 return IIO_VAL_INT;
129 case IIO_CHAN_INFO_SCALE:
130 *val = 31;
131 *val2 = 250000;
132
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];
139
140 return IIO_VAL_INT_PLUS_MICRO;
141 default:
142 return -EINVAL;
143 }
144 }
145
tmp007_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int val,int val2,long mask)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)
149 {
150 struct tmp007_data *data = iio_priv(indio_dev);
151 int i;
152 u16 tmp;
153
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);
160
161 return i2c_smbus_write_word_swapped(data->client,
162 TMP007_CONFIG,
163 data->config = tmp);
164 }
165 }
166 }
167
168 return -EINVAL;
169 }
170
tmp007_interrupt_handler(int irq,void * private)171 static irqreturn_t tmp007_interrupt_handler(int irq, void *private)
172 {
173 struct iio_dev *indio_dev = private;
174 struct tmp007_data *data = iio_priv(indio_dev);
175 int ret;
176
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;
181
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));
189
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));
197
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));
205
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));
213
214 return IRQ_HANDLED;
215 }
216
tmp007_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)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)
220 {
221 struct tmp007_data *data = iio_priv(indio_dev);
222 unsigned int status_mask;
223 int ret;
224
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;
240 }
241
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;
247
248 if (state)
249 ret |= status_mask;
250 else
251 ret &= ~status_mask;
252
253 return i2c_smbus_write_word_swapped(data->client, TMP007_STATUS_MASK,
254 data->status_mask = ret);
255 }
256
tmp007_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)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)
260 {
261 struct tmp007_data *data = iio_priv(indio_dev);
262 unsigned int mask;
263
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;
279 }
280
281 return !!(data->status_mask & mask);
282 }
283
tmp007_read_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)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)
288 {
289 struct tmp007_data *data = iio_priv(indio_dev);
290 int ret;
291 u8 reg;
292
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;
308 }
309
310 ret = i2c_smbus_read_word_swapped(data->client, reg);
311 if (ret < 0)
312 return ret;
313
314 /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
315 *val = sign_extend32(ret, 15) >> 7;
316
317 return IIO_VAL_INT;
318 }
319
tmp007_write_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)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)
324 {
325 struct tmp007_data *data = iio_priv(indio_dev);
326 u8 reg;
327
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;
343 }
344
345 /* Full scale threshold value is +/- 256 degree Celsius */
346 if (val < -256 || val > 255)
347 return -EINVAL;
348
349 /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
350 return i2c_smbus_write_word_swapped(data->client, reg, (val << 7));
351 }
352
353 static IIO_CONST_ATTR(sampling_frequency_available, "4 2 1 0.5 0.25");
354
355 static struct attribute *tmp007_attributes[] = {
356 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
357 NULL
358 };
359
360 static const struct attribute_group tmp007_attribute_group = {
361 .attrs = tmp007_attributes,
362 };
363
364 static const struct iio_event_spec tmp007_obj_event[] = {
365 {
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),
370 },
371 {
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),
376 },
377 };
378
379 static const struct iio_event_spec tmp007_die_event[] = {
380 {
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),
385 },
386 {
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),
391 },
392 };
393
394 static const struct iio_chan_spec tmp007_channels[] = {
395 {
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),
404 },
405 {
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),
414 }
415 };
416
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,
425 };
426
tmp007_identify(struct i2c_client * client)427 static bool tmp007_identify(struct i2c_client *client)
428 {
429 int manf_id, dev_id;
430
431 manf_id = i2c_smbus_read_word_swapped(client, TMP007_MANUFACTURER_ID);
432 if (manf_id < 0)
433 return false;
434
435 dev_id = i2c_smbus_read_word_swapped(client, TMP007_DEVICE_ID);
436 if (dev_id < 0)
437 return false;
438
439 return (manf_id == TMP007_MANUFACTURER_MAGIC && dev_id == TMP007_DEVICE_MAGIC);
440 }
441
tmp007_powerdown_action_cb(void * priv)442 static void tmp007_powerdown_action_cb(void *priv)
443 {
444 struct tmp007_data *data = priv;
445
446 tmp007_powerdown(data);
447 }
448
tmp007_probe(struct i2c_client * client)449 static int tmp007_probe(struct i2c_client *client)
450 {
451 const struct i2c_device_id *tmp007_id = i2c_client_get_device_id(client);
452 struct tmp007_data *data;
453 struct iio_dev *indio_dev;
454 int ret;
455
456 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
457 return -EOPNOTSUPP;
458
459 if (!tmp007_identify(client)) {
460 dev_err(&client->dev, "TMP007 not found\n");
461 return -ENODEV;
462 }
463
464 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
465 if (!indio_dev)
466 return -ENOMEM;
467
468 data = iio_priv(indio_dev);
469 i2c_set_clientdata(client, indio_dev);
470 data->client = client;
471 mutex_init(&data->lock);
472
473 indio_dev->name = "tmp007";
474 indio_dev->modes = INDIO_DIRECT_MODE;
475 indio_dev->info = &tmp007_info;
476
477 indio_dev->channels = tmp007_channels;
478 indio_dev->num_channels = ARRAY_SIZE(tmp007_channels);
479
480 /*
481 * Set Configuration register:
482 * 1. Conversion ON
483 * 2. ALERT enable
484 * 3. Transient correction enable
485 */
486
487 ret = i2c_smbus_read_word_swapped(data->client, TMP007_CONFIG);
488 if (ret < 0)
489 return ret;
490
491 data->config = ret;
492 data->config |= (TMP007_CONFIG_CONV_EN | TMP007_CONFIG_ALERT_EN | TMP007_CONFIG_TC_EN);
493
494 ret = i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
495 data->config);
496 if (ret < 0)
497 return ret;
498
499 ret = devm_add_action_or_reset(&client->dev, tmp007_powerdown_action_cb, data);
500 if (ret)
501 return ret;
502
503 /*
504 * Only the following flags can activate ALERT pin. Data conversion/validity flags
505 * flags can still be polled for getting temperature data
506 *
507 * Set Status Mask register:
508 * 1. Object temperature high limit enable
509 * 2. Object temperature low limit enable
510 * 3. TDIE temperature high limit enable
511 * 4. TDIE temperature low limit enable
512 */
513
514 ret = i2c_smbus_read_word_swapped(data->client, TMP007_STATUS_MASK);
515 if (ret < 0)
516 return ret;
517
518 data->status_mask = ret;
519 data->status_mask |= (TMP007_STATUS_OHF | TMP007_STATUS_OLF
520 | TMP007_STATUS_LHF | TMP007_STATUS_LLF);
521
522 ret = i2c_smbus_write_word_swapped(data->client, TMP007_STATUS_MASK, data->status_mask);
523 if (ret < 0)
524 return ret;
525
526 if (client->irq) {
527 ret = devm_request_threaded_irq(&client->dev, client->irq,
528 NULL, tmp007_interrupt_handler,
529 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
530 tmp007_id->name, indio_dev);
531 if (ret) {
532 dev_err(&client->dev, "irq request error %d\n", -ret);
533 return ret;
534 }
535 }
536
537 return devm_iio_device_register(&client->dev, indio_dev);
538 }
539
tmp007_suspend(struct device * dev)540 static int tmp007_suspend(struct device *dev)
541 {
542 struct tmp007_data *data = iio_priv(i2c_get_clientdata(
543 to_i2c_client(dev)));
544
545 return tmp007_powerdown(data);
546 }
547
tmp007_resume(struct device * dev)548 static int tmp007_resume(struct device *dev)
549 {
550 struct tmp007_data *data = iio_priv(i2c_get_clientdata(
551 to_i2c_client(dev)));
552
553 return i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
554 data->config | TMP007_CONFIG_CONV_EN);
555 }
556
557 static DEFINE_SIMPLE_DEV_PM_OPS(tmp007_pm_ops, tmp007_suspend, tmp007_resume);
558
559 static const struct of_device_id tmp007_of_match[] = {
560 { .compatible = "ti,tmp007", },
561 { },
562 };
563 MODULE_DEVICE_TABLE(of, tmp007_of_match);
564
565 static const struct i2c_device_id tmp007_id[] = {
566 { "tmp007", 0 },
567 { }
568 };
569 MODULE_DEVICE_TABLE(i2c, tmp007_id);
570
571 static struct i2c_driver tmp007_driver = {
572 .driver = {
573 .name = "tmp007",
574 .of_match_table = tmp007_of_match,
575 .pm = pm_sleep_ptr(&tmp007_pm_ops),
576 },
577 .probe = tmp007_probe,
578 .id_table = tmp007_id,
579 };
580 module_i2c_driver(tmp007_driver);
581
582 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
583 MODULE_DESCRIPTION("TI TMP007 IR thermopile sensor driver");
584 MODULE_LICENSE("GPL");
585