1 /*
2  * Copyright (c) 2018 Peter Bigot Consulting, LLC
3  * Copyright (c) 2018 Linaro Ltd.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_DRIVERS_SENSOR_CCS811_CCS811_H_
9 #define ZEPHYR_DRIVERS_SENSOR_CCS811_CCS811_H_
10 
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/drivers/gpio.h>
14 #include <zephyr/sys/util.h>
15 #include <zephyr/drivers/sensor/ccs811.h>
16 
17 /* Registers */
18 #define CCS811_REG_STATUS               0x00
19 #define CCS811_REG_MEAS_MODE            0x01
20 #define CCS811_REG_ALG_RESULT_DATA      0x02
21 #define CCS811_REG_RAW_DATA             0x03
22 #define CCS811_REG_ENV_DATA             0x05
23 #define CCS811_REG_THRESHOLDS           0x10
24 #define CCS811_REG_BASELINE             0x11
25 #define CCS811_REG_HW_ID                0x20
26 #define CCS811_REG_HW_VERSION           0x21
27 #define CCS811_REG_FW_BOOT_VERSION      0x23
28 #define CCS811_REG_FW_APP_VERSION       0x24
29 #define CCS811_REG_ERROR_ID             0xE0
30 #define CCS811_REG_APP_START            0xF4
31 
32 #define CCS881_HW_ID                    0x81
33 #define CCS811_HW_VERSION_MSK           0xF0
34 
35 /* Measurement modes */
36 #define CCS811_MODE_RAW_DATA            0x40
37 #define CCS811_MODE_DATARDY             0x08
38 #define CCS811_MODE_THRESH              0x04
39 
40 #define CCS811_RAW_VOLTAGE_POS          0
41 #define CCS811_RAW_VOLTAGE_MSK          (0x3FF << CCS811_RAW_VOLTAGE_POS)
42 #define CCS811_RAW_VOLTAGE_SCALE        (1650000U / (CCS811_RAW_VOLTAGE_MSK \
43 						     >> CCS811_RAW_VOLTAGE_POS))
44 #define CCS811_RAW_CURRENT_POS          10
45 #define CCS811_RAW_CURRENT_MSK          (0x3F << CCS811_RAW_CURRENT_POS)
46 #define CCS811_RAW_CURRENT_SCALE        1
47 
48 #define CCS811_CO2_MIN_PPM              400
49 #define CCS811_CO2_MAX_PPM              32767
50 
51 struct ccs811_data {
52 #ifdef CONFIG_CCS811_TRIGGER
53 	const struct device *dev;
54 
55 	/*
56 	 * DATARDY is configured through SENSOR_CHAN_ALL.
57 	 * THRESH would be configured through SENSOR_CHAN_CO2.
58 	 */
59 	struct gpio_callback gpio_cb;
60 	sensor_trigger_handler_t handler;
61 	const struct sensor_trigger *trigger;
62 #if defined(CONFIG_CCS811_TRIGGER_OWN_THREAD)
63 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_CCS811_THREAD_STACK_SIZE);
64 	struct k_sem gpio_sem;
65 	struct k_thread thread;
66 #elif defined(CONFIG_CCS811_TRIGGER_GLOBAL_THREAD)
67 	struct k_work work;
68 #endif
69 	uint16_t co2_l2m;
70 	uint16_t co2_m2h;
71 #endif /* CONFIG_CCS811_TRIGGER */
72 	struct ccs811_result_type result;
73 	uint8_t mode;
74 	uint8_t app_fw_ver;
75 };
76 
77 struct ccs811_config {
78 	struct i2c_dt_spec i2c;
79 	struct gpio_dt_spec irq_gpio;
80 	struct gpio_dt_spec reset_gpio;
81 	struct gpio_dt_spec wake_gpio;
82 };
83 
84 #ifdef CONFIG_CCS811_TRIGGER
85 
86 int ccs811_mutate_meas_mode(const struct device *dev,
87 			    uint8_t set,
88 			    uint8_t clear);
89 
90 int ccs811_set_thresholds(const struct device *dev);
91 
92 int ccs811_attr_set(const struct device *dev,
93 		    enum sensor_channel chan,
94 		    enum sensor_attribute attr,
95 		    const struct sensor_value *val);
96 
97 int ccs811_trigger_set(const struct device *dev,
98 		       const struct sensor_trigger *trig,
99 		       sensor_trigger_handler_t handler);
100 
101 int ccs811_init_interrupt(const struct device *dev);
102 
103 #endif  /* CONFIG_CCS811_TRIGGER */
104 
105 #endif  /* _SENSOR_CCS811_ */
106