1 /*
2  * Copyright (c) 2022 T-Mobile USA, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_TSL2540_TSL2540_H_
8 #define ZEPHYR_DRIVERS_SENSOR_TSL2540_TSL2540_H_
9 
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/drivers/sensor.h>
13 #include <zephyr/drivers/sensor/tsl2540.h>
14 
15 #define TSL2540_REG_ATIME    0x81
16 #define TSL2540_REG_WTIME    0x83
17 #define TSL2540_REG_AILT_LOW 0x84
18 #define TSL2540_REG_AILT_HI  0x85
19 #define TSL2540_REG_AIHT_LOW 0x86
20 #define TSL2540_REG_AIHT_HI  0x87
21 #define TSL2540_REG_PERS     0x8c
22 #define TSL2540_REG_CFG_0    0x8d
23 #define TSL2540_REG_CFG_1    0x90
24 #define TSL2540_REG_REVID    0x91
25 #define TSL2540_REG_ID	     0x92
26 #define TSL2540_REG_STATUS   0x93
27 #define TSL2540_REG_VIS_LOW  0x94
28 #define TSL2540_REG_VIS_HI   0x95
29 #define TSL2540_REG_IR_LOW   0x96
30 #define TSL2540_REG_IR_HI    0x97
31 #define TSL2540_REG_REVID2   0x9E
32 #define TSL2540_REG_CFG_2    0x9f
33 
34 #define TSL2540_AGAIN_S1_2 0.5
35 #define TSL2540_AGAIN_S1   1
36 #define TSL2540_AGAIN_S4   4
37 #define TSL2540_AGAIN_S16  16
38 #define TSL2540_AGAIN_S64  67
39 #define TSL2540_AGAIN_S128 140
40 
41 #define TSL2540_CFG1_G1_2 0x00
42 #define TSL2540_CFG1_G1	  0x00
43 #define TSL2540_CFG1_G4	  0x01
44 #define TSL2540_CFG1_G16  0x02
45 #define TSL2540_CFG1_G64  0x03
46 #define TSL2540_CFG1_G128 0x03
47 
48 #define TSL2540_CFG2_G1_2 0x00
49 #define TSL2540_CFG2_G1	  0x04
50 #define TSL2540_CFG2_G4	  0x04
51 #define TSL2540_CFG2_G16  0x04
52 #define TSL2540_CFG2_G64  0x04
53 #define TSL2540_CFG2_G128 0x14
54 
55 /* ENABLE(0x80: 0x00): Reserved:7:4 | WEN:3 | Reserved:2 | AEN:1 | PON:0 */
56 #define TSL2540_ENABLE_ADDR 0x80
57 #define TSL2540_ENABLE_MASK (BIT(3) | BIT(1) | BIT(0))
58 #define TSL2540_ENABLE_CONF (BIT(3) | BIT(1) | BIT(0))
59 #define TSL2540_ENABLE_AEN_PON (BIT(1) | BIT(0))
60 #define TSL2540_ENABLE_DISABLE (0)
61 
62 /* CRG3(0xAB: 0x0C):  INT_READ_CLEAR:7 | Reserved:6:5 | SAI:4 | Reserved:3:0 */
63 #define TSL2540_CFG3_ADDR 0xAB
64 #define TSL2540_CFG3_MASK (BIT(7) | BIT(4))
65 #define TSL2540_CFG3_CONF (BIT(7) | BIT(4))
66 #define TSL2540_CFG3_DFLT (0)
67 
68 /* INTENAB(0xDD: 0x00): ASIEN:7 | Reserved:6:5 | AIEN:4 | Reserved:3:0 */
69 #define TSL2540_INTENAB_ADDR 0xDD
70 #define TSL2540_INTENAB_MASK (BIT(7) | BIT(4))
71 #define TSL2540_INTENAB_CONF (BIT(4))
72 
73 #define TSL2540_INT_EN_AEN 0x90
74 
75 struct tsl2540_config {
76 	const struct i2c_dt_spec i2c_spec;
77 #ifdef CONFIG_TSL2540_TRIGGER
78 	const struct gpio_dt_spec int_gpio;
79 #endif
80 	const uint32_t glass_attenuation;
81 	const uint32_t glass_ir_attenuation;
82 };
83 
84 struct tsl2540_data {
85 	const struct device *i2c;
86 	struct k_sem sem;
87 #ifdef CONFIG_TSL2540_TRIGGER
88 	const struct device *dev;
89 	struct gpio_callback gpio_cb;
90 	const struct sensor_trigger *als_trigger;
91 	sensor_trigger_handler_t als_handler;
92 #endif
93 #ifdef CONFIG_TSL2540_TRIGGER_OWN_THREAD
94 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_TSL2540_THREAD_STACK_SIZE);
95 	struct k_thread thread;
96 	struct k_sem trig_sem;
97 #endif
98 #ifdef CONFIG_TSL2540_TRIGGER_GLOBAL_THREAD
99 	struct k_work work;
100 #endif
101 	uint8_t enable_mode;
102 	uint16_t count_vis;
103 	uint16_t count_ir;
104 	uint8_t integration_time;
105 	double again;
106 };
107 
108 #ifdef CONFIG_TSL2540_TRIGGER
109 int tsl2540_trigger_init(const struct device *dev);
110 
111 int tsl2540_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
112 			sensor_trigger_handler_t handler);
113 #endif
114 
115 #endif
116