1 /*
2  * Copyright (c) 2024 Gustavo Silva
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_ENS160_ENS160_H_
8 #define ZEPHYR_DRIVERS_SENSOR_ENS160_ENS160_H_
9 
10 #include <zephyr/types.h>
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/logging/log.h>
14 #include <zephyr/drivers/sensor.h>
15 
16 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
17 #include <zephyr/drivers/i2c.h>
18 #endif
19 
20 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
21 #include <zephyr/drivers/spi.h>
22 #endif
23 
24 /* Registers */
25 #define ENS160_REG_PART_ID       0x00
26 #define ENS160_REG_OPMODE        0x10
27 #define ENS160_REG_CONFIG        0x11
28 #define ENS160_REG_COMMAND       0x12
29 #define ENS160_REG_TEMP_IN       0x13
30 #define ENS160_REG_RH_IN         0x15
31 #define ENS160_REG_DEVICE_STATUS 0x20
32 #define ENS160_REG_DATA_AQI      0x21
33 #define ENS160_REG_DATA_TVOC     0x22
34 #define ENS160_REG_DATA_ECO2     0x24
35 #define ENS160_REG_DATA_T        0x30
36 #define ENS160_REG_DATA_RH       0x32
37 #define ENS160_REG_DATA_MISR     0x38
38 #define ENS160_REG_GPR_WRITE0    0x40
39 #define ENS160_REG_GPR_WRITE1    0x41
40 #define ENS160_REG_GPR_WRITE2    0x42
41 #define ENS160_REG_GPR_WRITE3    0x43
42 #define ENS160_REG_GPR_WRITE4    0x44
43 #define ENS160_REG_GPR_WRITE5    0x45
44 #define ENS160_REG_GPR_WRITE6    0x46
45 #define ENS160_REG_GPR_WRITE7    0x47
46 #define ENS160_REG_GPR_READ0     0x48
47 #define ENS160_REG_GPR_READ1     0x49
48 #define ENS160_REG_GPR_READ2     0x4A
49 #define ENS160_REG_GPR_READ3     0x4B
50 #define ENS160_REG_GPR_READ4     0x4C
51 #define ENS160_REG_GPR_READ5     0x4D
52 #define ENS160_REG_GPR_READ6     0x4E
53 #define ENS160_REG_GPR_READ7     0x4F
54 
55 #define ENS160_PART_ID 0x160
56 
57 #define ENS160_TIMEOUT_US      (1000000U)
58 #define ENS160_BOOTING_TIME_MS (10U)
59 
60 /* Operation modes */
61 #define ENS160_OPMODE_DEEP_SLEEP 0x00
62 #define ENS160_OPMODE_IDLE       0x01
63 #define ENS160_OPMODE_STANDARD   0x02
64 #define ENS160_OPMODE_RESET      0xF0
65 
66 /* Device status */
67 #define ENS160_STATUS_STATER        BIT(6)
68 #define ENS160_STATUS_VALIDITY_FLAG GENMASK(3, 2)
69 #define ENS160_STATUS_NEWDAT        BIT(1)
70 
71 #define ENS160_STATUS_NORMAL   0x00
72 #define ENS160_STATUS_WARM_UP  0x01
73 #define ENS160_STATUS_START_UP 0x02
74 #define ENS160_STATUS_INVALID  0x03
75 
76 /* Commands */
77 #define ENS160_COMMAND_NOP        0x00
78 #define ENS160_COMMAND_GET_APPVER 0x0E
79 #define ENS160_COMMAND_CLRGPR     0xCC
80 
81 /* Config register fields */
82 #define ENS160_CONFIG_INTPOL  BIT(6)
83 #define ENS160_CONFIG_INT_CFG BIT(5)
84 #define ENS160_CONFIG_INTGPR  BIT(3)
85 #define ENS160_CONFIG_INTDAT  BIT(1)
86 #define ENS160_CONFIG_INTEN   BIT(0)
87 
88 #define ENS160_DATA_AQI_UBA GENMASK(2, 0)
89 
90 struct ens160_config {
91 	int (*bus_init)(const struct device *dev);
92 	const union {
93 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
94 		struct i2c_dt_spec i2c;
95 #endif
96 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
97 		struct spi_dt_spec spi;
98 #endif
99 	};
100 #ifdef CONFIG_ENS160_TRIGGER
101 	struct gpio_dt_spec int_gpio;
102 #endif
103 };
104 
105 struct ens160_transfer_function {
106 	int (*read_reg)(const struct device *dev, uint8_t reg, uint8_t *val);
107 	int (*read_data)(const struct device *dev, uint8_t reg, uint8_t *data, size_t len);
108 	int (*write_reg)(const struct device *dev, uint8_t reg, uint8_t val);
109 	int (*write_data)(const struct device *dev, uint8_t reg, uint8_t *data, size_t len);
110 };
111 
112 struct ens160_data {
113 	uint16_t eco2;
114 	uint16_t tvoc;
115 	uint8_t aqi;
116 	const struct ens160_transfer_function *tf;
117 #ifdef CONFIG_ENS160_TRIGGER
118 	struct gpio_callback gpio_cb;
119 	const struct device *dev;
120 
121 	sensor_trigger_handler_t data_ready_handler;
122 	const struct sensor_trigger *data_ready_trigger;
123 #if defined(CONFIG_ENS160_TRIGGER_OWN_THREAD)
124 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_ENS160_THREAD_STACK_SIZE);
125 	struct k_sem gpio_sem;
126 	struct k_thread thread;
127 #elif defined(CONFIG_ENS160_TRIGGER_GLOBAL_THREAD)
128 	struct k_work work;
129 #endif
130 #endif /* CONFIG_ENS160_TRIGGER */
131 };
132 
133 #ifdef CONFIG_ENS160_TRIGGER
134 int ens160_trigger_set(const struct device *dev, const struct sensor_trigger *trigg,
135 		       sensor_trigger_handler_t handler);
136 
137 int ens160_init_interrupt(const struct device *dev);
138 #endif
139 
140 int ens160_i2c_init(const struct device *dev);
141 int ens160_spi_init(const struct device *dev);
142 
143 #endif /* ZEPHYR_DRIVERS_SENSOR_ENS160_ENS160_H_ */
144