1 /*
2  * Copyright (c) 2018 Analog Devices Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_ADT7420_ADT7420_H_
8 #define ZEPHYR_DRIVERS_SENSOR_ADT7420_ADT7420_H_
9 
10 #include <zephyr/types.h>
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/gpio.h>
13 
14 /* ADT7420 registers */
15 #define ADT7420_REG_TEMP_MSB		0x00 /* Temperature value MSB */
16 #define ADT7420_REG_TEMP_LSB		0x01 /* Temperature value LSB */
17 #define ADT7420_REG_STATUS		0x02 /* Status */
18 #define ADT7420_REG_CONFIG		0x03 /* Configuration */
19 #define ADT7420_REG_T_HIGH_MSB		0x04 /* Temperature HIGH setpoint MSB */
20 #define ADT7420_REG_T_HIGH_LSB		0x05 /* Temperature HIGH setpoint LSB */
21 #define ADT7420_REG_T_LOW_MSB		0x06 /* Temperature LOW setpoint MSB */
22 #define ADT7420_REG_T_LOW_LSB		0x07 /* Temperature LOW setpoint LSB */
23 #define ADT7420_REG_T_CRIT_MSB		0x08 /* Temperature CRIT setpoint MSB */
24 #define ADT7420_REG_T_CRIT_LSB		0x09 /* Temperature CRIT setpoint LSB */
25 #define ADT7420_REG_HIST		0x0A /* Temperature HYST setpoint */
26 #define ADT7420_REG_ID			0x0B /* ID */
27 #define ADT7420_REG_RESET		0x2F /* Software reset */
28 
29 /* ADT7420_REG_STATUS definition */
30 #define ADT7420_STATUS_T_LOW		BIT(4)
31 #define ADT7420_STATUS_T_HIGH		BIT(5)
32 #define ADT7420_STATUS_T_CRIT		BIT(6)
33 #define ADT7420_STATUS_RDY		BIT(7)
34 
35 /* ADT7420_REG_CONFIG definition */
36 #define ADT7420_CONFIG_FAULT_QUEUE(x)	((x) & 0x3)
37 #define ADT7420_CONFIG_CT_POL		BIT(2)
38 #define ADT7420_CONFIG_INT_POL		BIT(3)
39 #define ADT7420_CONFIG_INT_CT_MODE	BIT(4)
40 #define ADT7420_CONFIG_OP_MODE(x)	(((x) & 0x3) << 5)
41 #define ADT7420_CONFIG_RESOLUTION	BIT(7)
42 
43 /* ADT7420_CONFIG_FAULT_QUEUE(x) options */
44 #define ADT7420_FAULT_QUEUE_1_FAULT	0
45 #define ADT7420_FAULT_QUEUE_2_FAULTS	1
46 #define ADT7420_FAULT_QUEUE_3_FAULTS	2
47 #define ADT7420_FAULT_QUEUE_4_FAULTS	3
48 
49 /* ADT7420_CONFIG_OP_MODE(x) options */
50 #define ADT7420_OP_MODE_CONT_CONV	0
51 #define ADT7420_OP_MODE_ONE_SHOT	1
52 #define ADT7420_OP_MODE_1_SPS		2
53 #define ADT7420_OP_MODE_SHUTDOWN	3
54 
55 /* ADT7420 default ID */
56 #define ADT7420_DEFAULT_ID		0xCB
57 
58 /* scale in micro degrees Celsius */
59 #define ADT7420_TEMP_SCALE		15625
60 
61 struct adt7420_data {
62 	int16_t sample;
63 #ifdef CONFIG_ADT7420_TRIGGER
64 	struct gpio_callback gpio_cb;
65 
66 	sensor_trigger_handler_t th_handler;
67 	const struct sensor_trigger *th_trigger;
68 
69 	const struct device *dev;
70 
71 #if defined(CONFIG_ADT7420_TRIGGER_OWN_THREAD)
72 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_ADT7420_THREAD_STACK_SIZE);
73 	struct k_sem gpio_sem;
74 	struct k_thread thread;
75 #elif defined(CONFIG_ADT7420_TRIGGER_GLOBAL_THREAD)
76 	struct k_work work;
77 #endif
78 #endif /* CONFIG_ADT7420_TRIGGER */
79 
80 };
81 
82 struct adt7420_dev_config {
83 	struct i2c_dt_spec i2c;
84 #ifdef CONFIG_ADT7420_TRIGGER
85 	struct gpio_dt_spec int_gpio;
86 #endif
87 };
88 
89 #ifdef CONFIG_ADT7420_TRIGGER
90 int adt7420_trigger_set(const struct device *dev,
91 			const struct sensor_trigger *trig,
92 			sensor_trigger_handler_t handler);
93 
94 int adt7420_init_interrupt(const struct device *dev);
95 #endif /* CONFIG_ADT7420_TRIGGER */
96 
97 
98 #endif /* ZEPHYR_DRIVERS_SENSOR_ADT7420_ADT7420_H_ */
99