1 /*
2  * Copyright (c) 2023 Kurtis Dinelle
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_TSL2591_TSL2591_H_
8 #define ZEPHYR_DRIVERS_SENSOR_TSL2591_TSL2591_H_
9 
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/logging/log.h>
13 #include <zephyr/drivers/sensor/tsl2591.h>
14 
15 /* Device Identification */
16 #define TSL2591_DEV_ID 0x50
17 
18 /* Command: CMD:7 | TRANSACTION:6:5 | ADDR/SF:4:0 */
19 #define TSL2591_NORMAL_CMD    (BIT(7) | BIT(5))
20 #define TSL2591_SPECIAL_CMD   (BIT(7) | BIT(6) | BIT(5))
21 #define TSL2591_CLEAR_INT_CMD (TSL2591_SPECIAL_CMD | 0x7)
22 
23 /* Enable: (0x00): NPIEN:7 | SAI:6 | Reserved:5 | AIEN:4 | Reserved:3:2 | AEN:1 | PON:0 */
24 #define TSL2591_POWER_MASK (BIT(1) | BIT(0))
25 #define TSL2591_POWER_ON   (BIT(1) | BIT(0))
26 #define TSL2591_POWER_OFF  (0)
27 #define TSL2591_AEN_MASK   (BIT(1))
28 #define TSL2591_AEN_ON     (BIT(1))
29 #define TSL2591_AEN_OFF    (0)
30 #define TSL2591_AIEN_MASK  (BIT(4))
31 #define TSL2591_AIEN_ON    (BIT(4))
32 #define TSL2591_AIEN_OFF   (0)
33 
34 /* Config/Control: (0x01): SRESET:7 | Reserved:6 | AGAIN:5:4 | Reserved:3 | ATIME:2:0 */
35 #define TSL2591_SRESET     (BIT(7))
36 #define TSL2591_AGAIN_MASK (BIT(5) | BIT(4))
37 #define TSL2591_ATIME_MASK (BIT(2) | BIT(1) | BIT(0))
38 
39 /* Status: (0x13): Reserved:7:6 | NPINTR:5 | AINT:4 | Reserved:3:1 | AVALID:0 */
40 #define TSL2591_AVALID_MASK (BIT(0))
41 
42 /* Register Addresses */
43 #define TSL2591_REG_ENABLE  0x00
44 #define TSL2591_REG_CONFIG  0x01
45 #define TSL2591_REG_AILTL   0x04
46 #define TSL2591_REG_AILTH   0x05
47 #define TSL2591_REG_AIHTL   0x06
48 #define TSL2591_REG_AIHTH   0x07
49 #define TSL2591_REG_NPAILTL 0x08
50 #define TSL2591_REG_NPAILTH 0x09
51 #define TSL2591_REG_NPAIHTL 0x0A
52 #define TSL2591_REG_NPAIHTH 0x0B
53 #define TSL2591_REG_PERSIST 0x0C
54 #define TSL2591_REG_PID     0x11
55 #define TSL2591_REG_ID      0x12
56 #define TSL2591_REG_STATUS  0x13
57 #define TSL2591_REG_C0DATAL 0x14
58 #define TSL2591_REG_C0DATAH 0x15
59 #define TSL2591_REG_C1DATAL 0x16
60 #define TSL2591_REG_C1DATAH 0x17
61 
62 /* Integration Time Modes */
63 #define TSL2591_INTEGRATION_100MS 0x00
64 #define TSL2591_INTEGRATION_200MS 0x01
65 #define TSL2591_INTEGRATION_300MS 0x02
66 #define TSL2591_INTEGRATION_400MS 0x03
67 #define TSL2591_INTEGRATION_500MS 0x04
68 #define TSL2591_INTEGRATION_600MS 0x05
69 
70 /* Gain Modes */
71 #define TSL2591_GAIN_MODE_LOW  0x00
72 #define TSL2591_GAIN_MODE_MED  0x10
73 #define TSL2591_GAIN_MODE_HIGH 0x20
74 #define TSL2591_GAIN_MODE_MAX  0x30
75 
76 /* Gain Scales (Typical Values)
77  * See datasheet, used only for lux calculation.
78  */
79 #define TSL2591_GAIN_SCALE_LOW  1U
80 #define TSL2591_GAIN_SCALE_MED  25U
81 #define TSL2591_GAIN_SCALE_HIGH 400U
82 #define TSL2591_GAIN_SCALE_MAX  9200U
83 
84 /* Persistence Filters */
85 #define TSL2591_PERSIST_EVERY 0x00
86 #define TSL2591_PERSIST_1     0x01
87 #define TSL2591_PERSIST_2     0x02
88 #define TSL2591_PERSIST_3     0x03
89 #define TSL2591_PERSIST_5     0x04
90 #define TSL2591_PERSIST_10    0x05
91 #define TSL2591_PERSIST_15    0x06
92 #define TSL2591_PERSIST_20    0x07
93 #define TSL2591_PERSIST_25    0x08
94 #define TSL2591_PERSIST_30    0x09
95 #define TSL2591_PERSIST_35    0x0A
96 #define TSL2591_PERSIST_40    0x0B
97 #define TSL2591_PERSIST_45    0x0C
98 #define TSL2591_PERSIST_50    0x0D
99 #define TSL2591_PERSIST_55    0x0E
100 #define TSL2591_PERSIST_60    0x0F
101 
102 /* Device factor coefficient for lux calculations */
103 #define TSL2591_LUX_DF 408
104 
105 /* Max integration time (in ms) for single step */
106 #define TSL2591_MAX_TIME_STEP 105
107 
108 /* Max ADC Counts */
109 #define TSL2591_MAX_ADC     65535
110 #define TSL2591_MAX_ADC_100 36863
111 
112 struct tsl2591_config {
113 	const struct i2c_dt_spec i2c;
114 #ifdef CONFIG_TSL2591_TRIGGER
115 	const struct gpio_dt_spec int_gpio;
116 #endif
117 };
118 
119 struct tsl2591_data {
120 	uint16_t vis_count;
121 	uint16_t ir_count;
122 	uint16_t again;
123 	uint16_t atime;
124 	bool powered_on;
125 
126 #ifdef CONFIG_TSL2591_TRIGGER
127 	const struct device *dev;
128 	struct gpio_callback gpio_cb;
129 	sensor_trigger_handler_t th_handler;
130 	const struct sensor_trigger *th_trigger;
131 
132 #if defined(CONFIG_TSL2591_TRIGGER_OWN_THREAD)
133 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_TSL2591_THREAD_STACK_SIZE);
134 	struct k_sem trig_sem;
135 	struct k_thread thread;
136 #elif defined(CONFIG_TSL2591_TRIGGER_GLOBAL_THREAD)
137 	struct k_work work;
138 #endif
139 
140 #endif
141 };
142 
143 int tsl2591_reg_update(const struct device *dev, uint8_t reg, uint8_t mask, uint8_t val);
144 
145 #ifdef CONFIG_TSL2591_TRIGGER
146 int tsl2591_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
147 			sensor_trigger_handler_t handler);
148 int tsl2591_initialize_int(const struct device *dev);
149 #endif
150 
151 #endif /* ZEPHYR_DRIVERS_SENSOR_TSL2591_TSL2591_H_ */
152