1 /*
2  * Copyright (c) 2017, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <drivers/sensor.h>
8 #include <drivers/i2c.h>
9 #include <drivers/gpio.h>
10 
11 #define FXAS21002_REG_STATUS		0x00
12 #define FXAS21002_REG_OUTXMSB		0x01
13 #define FXAS21002_REG_INT_SOURCE	0x0b
14 #define FXAS21002_REG_WHOAMI		0x0c
15 #define FXAS21002_REG_CTRLREG0		0x0d
16 #define FXAS21002_REG_CTRLREG1		0x13
17 #define FXAS21002_REG_CTRLREG2		0x14
18 #define FXAS21002_REG_CTRLREG3		0x15
19 
20 #define FXAS21002_INT_SOURCE_DRDY_MASK	(1 << 0)
21 
22 #define FXAS21002_CTRLREG0_FS_MASK	(3 << 0)
23 
24 #define FXAS21002_CTRLREG1_DR_SHIFT	2
25 
26 #define FXAS21002_CTRLREG1_POWER_MASK	(3 << 0)
27 #define FXAS21002_CTRLREG1_DR_MASK	(7 << FXAS21002_CTRLREG1_DR_SHIFT)
28 #define FXAS21002_CTRLREG1_RST_MASK	(1 << 6)
29 
30 #define FXAS21002_CTRLREG2_CFG_EN_MASK		(1 << 2)
31 #define FXAS21002_CTRLREG2_CFG_DRDY_MASK	(1 << 3)
32 
33 #define FXAS21002_MAX_NUM_CHANNELS	3
34 
35 #define FXAS21002_BYTES_PER_CHANNEL	2
36 
37 #define FXAS21002_MAX_NUM_BYTES		(FXAS21002_BYTES_PER_CHANNEL * \
38 					 FXAS21002_MAX_NUM_CHANNELS)
39 
40 enum fxas21002_power {
41 	FXAS21002_POWER_STANDBY		= 0,
42 	FXAS21002_POWER_READY           = 1,
43 	FXAS21002_POWER_ACTIVE          = 3,
44 };
45 
46 enum fxas21002_range {
47 	FXAS21002_RANGE_2000DPS		= 0,
48 	FXAS21002_RANGE_1000DPS,
49 	FXAS21002_RANGE_500DPS,
50 	FXAS21002_RANGE_250DPS,
51 };
52 
53 enum fxas21002_channel {
54 	FXAS21002_CHANNEL_GYRO_X	= 0,
55 	FXAS21002_CHANNEL_GYRO_Y,
56 	FXAS21002_CHANNEL_GYRO_Z,
57 };
58 
59 struct fxas21002_config {
60 	char *i2c_name;
61 #ifdef CONFIG_FXAS21002_TRIGGER
62 	char *gpio_name;
63 	uint8_t gpio_pin;
64 	gpio_dt_flags_t gpio_flags;
65 #endif
66 	uint8_t i2c_address;
67 	uint8_t whoami;
68 	enum fxas21002_range range;
69 	uint8_t dr;
70 };
71 
72 struct fxas21002_data {
73 	const struct device *i2c;
74 	struct k_sem sem;
75 #ifdef CONFIG_FXAS21002_TRIGGER
76 	const struct device *dev;
77 	const struct device *gpio;
78 	uint8_t gpio_pin;
79 	struct gpio_callback gpio_cb;
80 	sensor_trigger_handler_t drdy_handler;
81 #endif
82 #ifdef CONFIG_FXAS21002_TRIGGER_OWN_THREAD
83 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_FXAS21002_THREAD_STACK_SIZE);
84 	struct k_thread thread;
85 	struct k_sem trig_sem;
86 #endif
87 #ifdef CONFIG_FXAS21002_TRIGGER_GLOBAL_THREAD
88 	struct k_work work;
89 #endif
90 	int16_t raw[FXAS21002_MAX_NUM_CHANNELS];
91 };
92 
93 int fxas21002_get_power(const struct device *dev, enum fxas21002_power *power);
94 int fxas21002_set_power(const struct device *dev, enum fxas21002_power power);
95 
96 uint32_t fxas21002_get_transition_time(enum fxas21002_power start,
97 				       enum fxas21002_power end,
98 				       uint8_t dr);
99 
100 #if CONFIG_FXAS21002_TRIGGER
101 int fxas21002_trigger_init(const struct device *dev);
102 int fxas21002_trigger_set(const struct device *dev,
103 			  const struct sensor_trigger *trig,
104 			  sensor_trigger_handler_t handler);
105 #endif
106