1 /*
2  * Copyright (c) 2020 Antmicro <www.antmicro.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_ADX345_ADX345_H_
8 #define ZEPHYR_DRIVERS_SENSOR_ADX345_ADX345_H_
9 
10 #include <zephyr/drivers/sensor.h>
11 #include <zephyr/types.h>
12 #include <zephyr/device.h>
13 #include <zephyr/drivers/gpio.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/sys/util.h>
16 
17 #ifdef CONFIG_ADXL345_STREAM
18 #include <zephyr/rtio/rtio.h>
19 #endif /* CONFIG_ADXL345_STREAM */
20 
21 #define DT_DRV_COMPAT adi_adxl345
22 
23 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
24 #include <zephyr/drivers/i2c.h>
25 #endif
26 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
27 #include <zephyr/drivers/spi.h>
28 #endif
29 #include <zephyr/sys/util.h>
30 
31 /* ADXL345 communication commands */
32 #define ADXL345_WRITE_CMD          0x00
33 #define ADXL345_READ_CMD           0x80
34 #define ADXL345_MULTIBYTE_FLAG     0x40
35 
36 #define ADXL345_REG_READ(x)	((x & 0xFF) | ADXL345_READ_CMD)
37 
38 #define SAMPLE_SIZE 6
39 #define SAMPLE_MASK 0x3F
40 #define SAMPLE_NUM  0x1F
41 
42 /* Registers */
43 #define ADXL345_DEVICE_ID_REG           0x00
44 #define ADXL345_RATE_REG                0x2c
45 #define ADXL345_POWER_CTL_REG           0x2d
46 #define ADXL345_DATA_FORMAT_REG         0x31
47 #define ADXL345_DATA_FORMAT_FULL_RES    0x08
48 #define ADXL345_X_AXIS_DATA_0_REG       0x32
49 #define ADXL345_FIFO_CTL_REG            0x38
50 #define ADXL345_FIFO_STATUS_REG         0x39
51 
52 #define ADXL345_PART_ID            0xe5
53 
54 #define ADXL345_RANGE_2G           0x0
55 #define ADXL345_RANGE_4G           0x1
56 #define ADXL345_RANGE_8G           0x2
57 #define ADXL345_RANGE_16G          0x3
58 #define ADXL345_RATE_25HZ          0x8
59 #define ADXL345_ENABLE_MEASURE_BIT (1 << 3)
60 #define ADXL345_FIFO_STREAM_MODE   (1 << 7)
61 #define ADXL345_FIFO_COUNT_MASK    0x3f
62 #define ADXL345_COMPLEMENT_MASK(x) GENMASK(15, (x))
63 #define ADXL345_COMPLEMENT         0xfc00
64 
65 #define ADXL345_MAX_FIFO_SIZE      32
66 
67 #define ADXL345_INT_ENABLE 0x2Eu
68 #define ADXL345_INT_MAP    0x2Fu
69 #define ADXL345_INT_SOURCE 0x30u
70 
71 /* ADXL345_STATUS_1 */
72 #define ADXL345_STATUS_DOUBLE_TAP(x) (((x) >> 5) & 0x1)
73 #define ADXL345_STATUS_SINGLE_TAP(x) (((x) >> 6) & 0x1)
74 #define ADXL345_STATUS_DATA_RDY(x)   (((x) >> 7) & 0x1)
75 
76 /* ADXL345_INT_MAP */
77 #define ADXL345_INT_MAP_OVERRUN_MSK        BIT(0)
78 #define ADXL345_INT_MAP_OVERRUN_MODE(x)    (((x) & 0x1) << 0)
79 #define ADXL345_INT_MAP_WATERMARK_MSK      BIT(1)
80 #define ADXL345_INT_MAP_WATERMARK_MODE(x)  (((x) & 0x1) << 1)
81 #define ADXL345_INT_MAP_FREE_FALL_MSK      BIT(2)
82 #define ADXL345_INT_MAP_FREE_FALL_MODE(x)  (((x) & 0x1) << 2)
83 #define ADXL345_INT_MAP_INACT_MSK          BIT(3)
84 #define ADXL345_INT_MAP_INACT_MODE(x)      (((x) & 0x1) << 3)
85 #define ADXL345_INT_MAP_ACT_MSK            BIT(4)
86 #define ADXL345_INT_MAP_ACT_MODE(x)        (((x) & 0x1) << 4)
87 #define ADXL345_INT_MAP_DOUBLE_TAP_MSK     BIT(5)
88 #define ADXL345_INT_MAP_DOUBLE_TAP_MODE(x) (((x) & 0x1) << 5)
89 #define ADXL345_INT_MAP_SINGLE_TAP_MSK     BIT(6)
90 #define ADXL345_INT_MAP_SINGLE_TAP_MODE(x) (((x) & 0x1) << 6)
91 #define ADXL345_INT_MAP_DATA_RDY_MSK       BIT(7)
92 #define ADXL345_INT_MAP_DATA_RDY_MODE(x)   (((x) & 0x1) << 7)
93 
94 /* POWER_CTL */
95 #define ADXL345_POWER_CTL_WAKEUP_4HZ         BIT(0)
96 #define ADXL345_POWER_CTL_WAKEUP_4HZ_MODE(x) (((x) & 0x1) << 0)
97 #define ADXL345_POWER_CTL_WAKEUP_2HZ         BIT(1)
98 #define ADXL345_POWER_CTL_WAKEUP_2HZ_MODE(x) (((x) & 0x1) << 1)
99 #define ADXL345_POWER_CTL_SLEEP              BIT(2)
100 #define ADXL345_POWER_CTL_SLEEP_MODE(x)      (((x) & 0x1) << 2)
101 #define ADXL345_POWER_CTL_MEASURE_MSK        GENMASK(3, 3)
102 #define ADXL345_POWER_CTL_MEASURE_MODE(x)    (((x) & 0x1) << 3)
103 #define ADXL345_POWER_CTL_STANDBY_MODE(x)    (((x) & 0x0) << 3)
104 
105 /* ADXL345_FIFO_CTL */
106 #define ADXL345_FIFO_CTL_MODE_MSK        GENMASK(7, 6)
107 #define ADXL345_FIFO_CTL_MODE_MODE(x)    (((x) & 0x3) << 6)
108 #define ADXL345_FIFO_CTL_TRIGGER_MSK     BIT(5)
109 #define ADXL345_FIFO_CTL_TRIGGER_MODE(x) (((x) & 0x1) << 5)
110 #define ADXL345_FIFO_CTL_SAMPLES_MSK     BIT(0)
111 #define ADXL345_FIFO_CTL_SAMPLES_MODE(x) ((x) & 0x1F)
112 
113 #define ADXL345_ODR_MSK     GENMASK(3, 0)
114 #define ADXL345_ODR_MODE(x) ((x) & 0xF)
115 
116 #define ADXL345_BUS_I2C 0
117 #define ADXL345_BUS_SPI 1
118 
119 enum adxl345_odr {
120 	ADXL345_ODR_12HZ = 0x7,
121 	ADXL345_ODR_25HZ,
122 	ADXL345_ODR_50HZ,
123 	ADXL345_ODR_100HZ,
124 	ADXL345_ODR_200HZ,
125 	ADXL345_ODR_400HZ
126 };
127 
128 enum adxl345_fifo_trigger {
129 	ADXL345_INT1,
130 	ADXL345_INT2
131 };
132 
133 enum adxl345_fifo_mode {
134 	ADXL345_FIFO_BYPASSED,
135 	ADXL345_FIFO_OLD_SAVED,
136 	ADXL345_FIFO_STREAMED,
137 	ADXL345_FIFO_TRIGGERED
138 };
139 
140 struct adxl345_fifo_config {
141 	enum adxl345_fifo_mode fifo_mode;
142 	enum adxl345_fifo_trigger fifo_trigger;
143 	uint16_t fifo_samples;
144 };
145 
146 enum adxl345_op_mode {
147 	ADXL345_STANDBY,
148 	ADXL345_MEASURE
149 };
150 
151 struct adxl345_dev_data {
152 	unsigned int sample_number;
153 	int16_t bufx[ADXL345_MAX_FIFO_SIZE];
154 	int16_t bufy[ADXL345_MAX_FIFO_SIZE];
155 	int16_t bufz[ADXL345_MAX_FIFO_SIZE];
156 	struct adxl345_fifo_config fifo_config;
157 	uint8_t is_full_res;
158 	uint8_t selected_range;
159 	enum adxl345_odr odr;
160 #ifdef CONFIG_ADXL345_TRIGGER
161 	struct gpio_callback gpio_cb;
162 
163 	sensor_trigger_handler_t th_handler;
164 	const struct sensor_trigger *th_trigger;
165 	sensor_trigger_handler_t drdy_handler;
166 	const struct sensor_trigger *drdy_trigger;
167 	const struct device *dev;
168 
169 #if defined(CONFIG_ADXL345_TRIGGER_OWN_THREAD)
170 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_ADXL345_THREAD_STACK_SIZE);
171 	struct k_sem gpio_sem;
172 	struct k_thread thread;
173 #elif defined(CONFIG_ADXL345_TRIGGER_GLOBAL_THREAD)
174 	struct k_work work;
175 #endif
176 #endif /* CONFIG_ADXL345_TRIGGER */
177 #ifdef CONFIG_ADXL345_STREAM
178 	struct rtio_iodev_sqe *sqe;
179 	struct rtio *rtio_ctx;
180 	struct rtio_iodev *iodev;
181 	uint8_t status1;
182 	uint8_t fifo_ent[1];
183 	uint64_t timestamp;
184 	struct rtio *r_cb;
185 	uint8_t fifo_watermark_irq;
186 	uint8_t fifo_samples;
187 	uint16_t fifo_total_bytes;
188 #endif /* CONFIG_ADXL345_STREAM */
189 };
190 
191 struct adxl345_fifo_data {
192 	uint8_t is_fifo: 1;
193 	uint8_t is_full_res: 1;
194 	uint8_t selected_range: 2;
195 	uint8_t sample_set_size: 4;
196 	uint8_t int_status;
197 	uint16_t accel_odr: 4;
198 	uint16_t fifo_byte_count: 12;
199 	uint64_t timestamp;
200 } __attribute__((__packed__));
201 
202 struct adxl345_sample {
203 #ifdef CONFIG_ADXL345_STREAM
204 	uint8_t is_fifo: 1;
205 	uint8_t res: 7;
206 #endif /* CONFIG_ADXL345_STREAM */
207 	uint8_t selected_range;
208 	bool is_full_res;
209 	int16_t x;
210 	int16_t y;
211 	int16_t z;
212 };
213 
214 union adxl345_bus {
215 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
216 	struct i2c_dt_spec i2c;
217 #endif
218 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
219 	struct spi_dt_spec spi;
220 #endif
221 };
222 
223 typedef bool (*adxl345_bus_is_ready_fn)(const union adxl345_bus *bus);
224 typedef int (*adxl345_reg_access_fn)(const struct device *dev, uint8_t cmd,
225 				     uint8_t reg_addr, uint8_t *data, size_t length);
226 
227 struct adxl345_dev_config {
228 	const union adxl345_bus bus;
229 	adxl345_bus_is_ready_fn bus_is_ready;
230 	adxl345_reg_access_fn reg_access;
231 	enum adxl345_odr odr;
232 	bool op_mode;
233 	struct adxl345_fifo_config fifo_config;
234 	uint8_t bus_type;
235 #ifdef CONFIG_ADXL345_TRIGGER
236 	struct gpio_dt_spec interrupt;
237 #endif
238 };
239 
240 void adxl345_submit_stream(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
241 void adxl345_stream_irq_handler(const struct device *dev);
242 
243 #ifdef CONFIG_ADXL345_TRIGGER
244 int adxl345_get_status(const struct device *dev,
245 		       uint8_t *status, uint16_t *fifo_entries);
246 
247 int adxl345_trigger_set(const struct device *dev,
248 			const struct sensor_trigger *trig,
249 			sensor_trigger_handler_t handler);
250 
251 int adxl345_init_interrupt(const struct device *dev);
252 
253 #endif /* CONFIG_ADXL345_TRIGGER */
254 
255 int adxl345_reg_write_mask(const struct device *dev,
256 			       uint8_t reg_addr,
257 			       uint8_t mask,
258 			       uint8_t data);
259 
260 int adxl345_reg_access(const struct device *dev, uint8_t cmd, uint8_t addr,
261 				     uint8_t *data, size_t len);
262 
263 int adxl345_reg_write(const struct device *dev, uint8_t addr, uint8_t *data,
264 				    uint8_t len);
265 
266 int adxl345_reg_read(const struct device *dev, uint8_t addr, uint8_t *data,
267 				   uint8_t len);
268 
269 int adxl345_reg_write_byte(const struct device *dev, uint8_t addr, uint8_t val);
270 
271 int adxl345_reg_read_byte(const struct device *dev, uint8_t addr, uint8_t *buf);
272 
273 int adxl345_set_op_mode(const struct device *dev, enum adxl345_op_mode op_mode);
274 #ifdef CONFIG_SENSOR_ASYNC_API
275 int adxl345_read_sample(const struct device *dev, struct adxl345_sample *sample);
276 void adxl345_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
277 int adxl345_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder);
278 void adxl345_accel_convert(struct sensor_value *val, int16_t sample);
279 #endif /* CONFIG_SENSOR_ASYNC_API */
280 
281 #ifdef CONFIG_ADXL345_STREAM
282 int adxl345_configure_fifo(const struct device *dev, enum adxl345_fifo_mode mode,
283 		enum adxl345_fifo_trigger trigger, uint16_t fifo_samples);
284 size_t adxl345_get_packet_size(const struct adxl345_dev_config *cfg);
285 #endif /* CONFIG_ADXL345_STREAM */
286 #endif /* ZEPHYR_DRIVERS_SENSOR_ADX345_ADX345_H_ */
287