xref: /openbmc/linux/drivers/iio/temperature/tmp007.c (revision a44e4f3a)
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/of.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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
442 static int tmp007_probe(struct i2c_client *client,
443 			const struct i2c_device_id *tmp007_id)
444 {
445 	struct tmp007_data *data;
446 	struct iio_dev *indio_dev;
447 	int ret;
448 
449 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
450 		return -EOPNOTSUPP;
451 
452 	if (!tmp007_identify(client)) {
453 		dev_err(&client->dev, "TMP007 not found\n");
454 		return -ENODEV;
455 	}
456 
457 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
458 	if (!indio_dev)
459 		return -ENOMEM;
460 
461 	data = iio_priv(indio_dev);
462 	i2c_set_clientdata(client, indio_dev);
463 	data->client = client;
464 	mutex_init(&data->lock);
465 
466 	indio_dev->dev.parent = &client->dev;
467 	indio_dev->name = "tmp007";
468 	indio_dev->modes = INDIO_DIRECT_MODE;
469 	indio_dev->info = &tmp007_info;
470 
471 	indio_dev->channels = tmp007_channels;
472 	indio_dev->num_channels = ARRAY_SIZE(tmp007_channels);
473 
474 	/*
475 	 * Set Configuration register:
476 	 * 1. Conversion ON
477 	 * 2. ALERT enable
478 	 * 3. Transient correction enable
479 	 */
480 
481 	ret = i2c_smbus_read_word_swapped(data->client, TMP007_CONFIG);
482 	if (ret < 0)
483 		return ret;
484 
485 	data->config = ret;
486 	data->config |= (TMP007_CONFIG_CONV_EN | TMP007_CONFIG_ALERT_EN | TMP007_CONFIG_TC_EN);
487 
488 	ret = i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
489 					data->config);
490 	if (ret < 0)
491 		return ret;
492 
493 	/*
494 	 * Only the following flags can activate ALERT pin. Data conversion/validity flags
495 	 * flags can still be polled for getting temperature data
496 	 *
497 	 * Set Status Mask register:
498 	 * 1. Object temperature high limit enable
499 	 * 2. Object temperature low limit enable
500 	 * 3. TDIE temperature high limit enable
501 	 * 4. TDIE temperature low limit enable
502 	 */
503 
504 	ret = i2c_smbus_read_word_swapped(data->client, TMP007_STATUS_MASK);
505 	if (ret < 0)
506 		goto error_powerdown;
507 
508 	data->status_mask = ret;
509 	data->status_mask |= (TMP007_STATUS_OHF | TMP007_STATUS_OLF
510 				| TMP007_STATUS_LHF | TMP007_STATUS_LLF);
511 
512 	ret = i2c_smbus_write_word_swapped(data->client, TMP007_STATUS_MASK, data->status_mask);
513 	if (ret < 0)
514 		goto error_powerdown;
515 
516 	if (client->irq) {
517 		ret = devm_request_threaded_irq(&client->dev, client->irq,
518 				NULL, tmp007_interrupt_handler,
519 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
520 				tmp007_id->name, indio_dev);
521 		if (ret) {
522 			dev_err(&client->dev, "irq request error %d\n", -ret);
523 			goto error_powerdown;
524 		}
525 	}
526 
527 	return iio_device_register(indio_dev);
528 
529 error_powerdown:
530 	tmp007_powerdown(data);
531 
532 	return ret;
533 }
534 
535 static int tmp007_remove(struct i2c_client *client)
536 {
537 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
538 	struct tmp007_data *data = iio_priv(indio_dev);
539 
540 	iio_device_unregister(indio_dev);
541 	tmp007_powerdown(data);
542 
543 	return 0;
544 }
545 
546 #ifdef CONFIG_PM_SLEEP
547 static int tmp007_suspend(struct device *dev)
548 {
549 	struct tmp007_data *data = iio_priv(i2c_get_clientdata(
550 			to_i2c_client(dev)));
551 
552 	return tmp007_powerdown(data);
553 }
554 
555 static int tmp007_resume(struct device *dev)
556 {
557 	struct tmp007_data *data = iio_priv(i2c_get_clientdata(
558 			to_i2c_client(dev)));
559 
560 	return i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
561 			data->config | TMP007_CONFIG_CONV_EN);
562 }
563 #endif
564 
565 static SIMPLE_DEV_PM_OPS(tmp007_pm_ops, tmp007_suspend, tmp007_resume);
566 
567 static const struct of_device_id tmp007_of_match[] = {
568 	{ .compatible = "ti,tmp007", },
569 	{ },
570 };
571 MODULE_DEVICE_TABLE(of, tmp007_of_match);
572 
573 static const struct i2c_device_id tmp007_id[] = {
574 	{ "tmp007", 0 },
575 	{ }
576 };
577 MODULE_DEVICE_TABLE(i2c, tmp007_id);
578 
579 static struct i2c_driver tmp007_driver = {
580 	.driver = {
581 		.name	= "tmp007",
582 		.of_match_table = of_match_ptr(tmp007_of_match),
583 		.pm	= &tmp007_pm_ops,
584 	},
585 	.probe		= tmp007_probe,
586 	.remove		= tmp007_remove,
587 	.id_table	= tmp007_id,
588 };
589 module_i2c_driver(tmp007_driver);
590 
591 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
592 MODULE_DESCRIPTION("TI TMP007 IR thermopile sensor driver");
593 MODULE_LICENSE("GPL");
594