1 /*
2  * Copyright (c) 2025 Croxel Inc.
3  * Copyright (c) 2025 CogniPilot Foundation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_DRIVERS_SENSOR_PAA3905_H_
9 #define ZEPHYR_DRIVERS_SENSOR_PAA3905_H_
10 
11 #include <stdint.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/drivers/sensor.h>
14 #include <zephyr/rtio/rtio.h>
15 
16 struct paa3905_encoded_data {
17 	struct {
18 		uint64_t timestamp;
19 		uint8_t channels : 3;
20 		struct {
21 			bool drdy : 1;
22 			bool motion : 1;
23 		} events;
24 	} header;
25 	union {
26 		uint8_t buf[14];
27 		struct {
28 			uint8_t motion;
29 			uint8_t observation;
30 			struct {
31 				int16_t x;
32 				int16_t y;
33 			} delta;
34 			uint8_t challenging_conditions;
35 			uint8_t squal;
36 			uint8_t raw_sum;
37 			uint8_t raw_max;
38 			uint8_t raw_min;
39 			uint32_t shutter : 24;
40 		} __attribute__((__packed__));
41 	};
42 };
43 
44 struct paa3905_stream {
45 	struct gpio_callback cb;
46 	const struct device *dev;
47 	struct rtio_iodev_sqe *iodev_sqe;
48 	struct k_timer timer;
49 	struct {
50 		struct {
51 			bool drdy : 1;
52 			bool motion : 1;
53 		} enabled;
54 		struct {
55 			enum sensor_stream_data_opt drdy;
56 			enum sensor_stream_data_opt motion;
57 		} opt;
58 	} settings;
59 };
60 
61 struct paa3905_data {
62 	struct {
63 		struct rtio_iodev *iodev;
64 		struct rtio *ctx;
65 	} rtio;
66 #if defined(CONFIG_PAA3905_STREAM)
67 	struct paa3905_stream stream;
68 #endif /* CONFIG_PAA3905_STREAM */
69 };
70 
71 struct paa3905_config {
72 	struct gpio_dt_spec int_gpio;
73 	int resolution;
74 	bool led_control;
75 	uint32_t backup_timer_period;
76 };
77 
78 /** Made public in order to perform chip recovery if erratic behavior
79  * is detected.
80  */
81 int paa3905_recover(const struct device *dev);
82 
83 #endif /* ZEPHYR_DRIVERS_SENSOR_PAA3905_H_ */
84