1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  *
4  * Copyright (c) 2023 Linumiz
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_MC3419_H_
8 #define ZEPHYR_DRIVERS_SENSOR_MC3419_H_
9 
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/drivers/sensor.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/sys/util.h>
15 
16 /* Registers */
17 #define MC3419_REG_INT_CTRL		0x06
18 #define MC3419_REG_OP_MODE		0x07
19 #define MC3419_REG_SAMPLE_RATE		0x08
20 #define MC3419_REG_MOTION_CTRL		0x09
21 #define MC3419_REG_XOUT_L		0x0D
22 #define MC3419_REG_YOUT_L		0x0F
23 #define MC3419_REG_ZOUT_L		0x11
24 #define MC3419_REG_STATUS		0x13
25 #define MC3419_REG_INT_STATUS		0x14
26 #define MC3419_REG_RANGE_SELECT_CTRL	0x20
27 #define MC3419_REG_SAMPLE_RATE_2	0x30
28 #define MC3419_REG_COMM_CTRL		0x31
29 #define MC3419_REG_ANY_MOTION_THRES	0x43
30 
31 #define MC3419_RANGE_MASK		GENMASK(6, 4)
32 #define MC3419_LPF_MASK                 GENMASK(3, 0)
33 #define MC3419_DATA_READY_MASK		BIT(7)
34 #define MC3419_ANY_MOTION_MASK		BIT(2)
35 #define MC3419_INT_CLEAR		0x00
36 #define MC3419_INT_ROUTE		0x10
37 
38 #define MC3419_ANY_MOTION_THRESH_MAX	0x7FFF
39 #define MC3419_SAMPLE_SIZE		3
40 #define MC3419_SAMPLE_READ_SIZE		(MC3419_SAMPLE_SIZE * (sizeof(int16_t)))
41 
42 #define SENSOR_GRAIN_VALUE             (61LL / 1000.0)
43 #define SENSOR_GRAVITY_DOUBLE          (SENSOR_G / 1000000.0)
44 #define MC3419_BASE_ODR_VAL		0x10
45 
46 #define MC3419_TRIG_DATA_READY		0
47 #define MC3419_TRIG_ANY_MOTION		1
48 #define MC3419_TRIG_SIZE		2
49 
50 enum mc3419_op_mode {
51 	MC3419_MODE_STANDBY = 0x00,
52 	MC3419_MODE_WAKE = 0x01
53 };
54 
55 struct mc3419_odr_map {
56 	int16_t freq;
57 	int16_t mfreq;
58 };
59 
60 enum mc3419_accl_range {
61 	MC3419_ACCl_RANGE_2G,
62 	MC3419_ACCl_RANGE_4G,
63 	MC3419_ACCl_RANGE_8G,
64 	MC3419_ACCl_RANGE_16G,
65 	MC3419_ACCl_RANGE_12G,
66 	MC3419_ACCL_RANGE_END
67 };
68 
69 struct mc3419_config {
70 	struct i2c_dt_spec i2c;
71 #if defined(CONFIG_MC3419_TRIGGER)
72 	struct gpio_dt_spec int_gpio;
73 	bool int_cfg;
74 #endif
75 	uint8_t lpf_fc_sel;
76 	uint8_t decimation_rate;
77 };
78 
79 struct mc3419_driver_data {
80 	double sensitivity;
81 	struct k_sem sem;
82 	int16_t samples[MC3419_SAMPLE_SIZE];
83 #if defined(CONFIG_MC3419_TRIGGER)
84 	const struct device *gpio_dev;
85 	struct gpio_callback gpio_cb;
86 	sensor_trigger_handler_t handler[MC3419_TRIG_SIZE];
87 	const struct sensor_trigger *trigger[MC3419_TRIG_SIZE];
88 #if defined(CONFIG_MC3419_TRIGGER_OWN_THREAD)
89 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_MC3419_THREAD_STACK_SIZE);
90 	struct k_thread thread;
91 	struct k_sem trig_sem;
92 #else
93 	struct k_work work;
94 #endif
95 #endif
96 };
97 
98 #if defined(CONFIG_MC3419_TRIGGER)
99 int mc3419_trigger_init(const struct device *dev);
100 int mc3419_configure_trigger(const struct device *dev,
101 			     const struct sensor_trigger *trig,
102 			     sensor_trigger_handler_t handler);
103 #endif
104 
105 #endif
106