1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_BMA280_BMA280_H_
8 #define ZEPHYR_DRIVERS_SENSOR_BMA280_BMA280_H_
9 
10 #include <zephyr/device.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/types.h>
13 #include <zephyr/drivers/i2c.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/kernel.h>
16 
17 #define BMA280_REG_CHIP_ID		0x00
18 #if DT_INST_PROP(0, is_bmc150)
19 	#define BMA280_CHIP_ID		0xFA
20 #else
21 	#define BMA280_CHIP_ID		0xFB
22 #endif
23 
24 #define BMA280_REG_PMU_BW		0x10
25 #if CONFIG_BMA280_PMU_BW_1
26 	#define BMA280_PMU_BW		0x08
27 #elif CONFIG_BMA280_PMU_BW_2
28 	#define BMA280_PMU_BW		0x09
29 #elif CONFIG_BMA280_PMU_BW_3
30 	#define BMA280_PMU_BW		0x0A
31 #elif CONFIG_BMA280_PMU_BW_4
32 	#define BMA280_PMU_BW		0x0B
33 #elif CONFIG_BMA280_PMU_BW_5
34 	#define BMA280_PMU_BW		0x0C
35 #elif CONFIG_BMA280_PMU_BW_6
36 	#define BMA280_PMU_BW		0x0D
37 #elif CONFIG_BMA280_PMU_BW_7
38 	#define BMA280_PMU_BW		0x0E
39 #elif CONFIG_BMA280_PMU_BW_8
40 	#define BMA280_PMU_BW		0x0F
41 #endif
42 
43 /*
44  * BMA280_PMU_FULL_RANGE measured in milli-m/s^2 instead
45  * of m/s^2 to avoid using struct sensor_value for it
46  */
47 #define BMA280_REG_PMU_RANGE		0x0F
48 #if CONFIG_BMA280_PMU_RANGE_2G
49 	#define BMA280_PMU_RANGE	0x03
50 	#define BMA280_PMU_FULL_RANGE	(4 * SENSOR_G)
51 #elif CONFIG_BMA280_PMU_RANGE_4G
52 	#define BMA280_PMU_RANGE	0x05
53 	#define BMA280_PMU_FULL_RANGE	(8 * SENSOR_G)
54 #elif CONFIG_BMA280_PMU_RANGE_8G
55 	#define BMA280_PMU_RANGE	0x08
56 	#define BMA280_PMU_FULL_RANGE	(16 * SENSOR_G)
57 #elif CONFIG_BMA280_PMU_RANGE_16G
58 	#define BMA280_PMU_RANGE	0x0C
59 	#define BMA280_PMU_FULL_RANGE	(32 * SENSOR_G)
60 #endif
61 
62 #define BMA280_REG_TEMP			0x08
63 
64 #define BMA280_REG_INT_STATUS_0		0x09
65 #define BMA280_BIT_SLOPE_INT_STATUS	BIT(2)
66 #define BMA280_REG_INT_STATUS_1		0x0A
67 #define BMA280_BIT_DATA_INT_STATUS	BIT(7)
68 
69 #define BMA280_REG_INT_EN_0		0x16
70 #define BMA280_BIT_SLOPE_EN_X		BIT(0)
71 #define BMA280_BIT_SLOPE_EN_Y		BIT(1)
72 #define BMA280_BIT_SLOPE_EN_Z		BIT(2)
73 #define BMA280_SLOPE_EN_XYZ (BMA280_BIT_SLOPE_EN_X | \
74 		BMA280_BIT_SLOPE_EN_Y | BMA280_BIT_SLOPE_EN_X)
75 
76 #define BMA280_REG_INT_EN_1		0x17
77 #define BMA280_BIT_DATA_EN		BIT(4)
78 
79 #define BMA280_REG_INT_MAP_0		0x19
80 #define BMA280_INT_MAP_0_BIT_SLOPE	BIT(2)
81 
82 #define BMA280_REG_INT_MAP_1		0x1A
83 #define BMA280_INT_MAP_1_BIT_DATA	BIT(0)
84 
85 #define BMA280_REG_INT_RST_LATCH	0x21
86 #define BMA280_INT_MODE_LATCH		0x0F
87 #define BMA280_BIT_INT_LATCH_RESET	BIT(7)
88 
89 #define BMA280_REG_INT_5		0x27
90 #define BMA280_SLOPE_DUR_SHIFT		0
91 #define BMA280_SLOPE_DUR_MASK		(3 << BMA280_SLOPE_DUR_SHIFT)
92 
93 #define BMA280_REG_SLOPE_TH		0x28
94 
95 #define BMA280_REG_ACCEL_X_LSB		0x2
96 #define BMA280_REG_ACCEL_Y_LSB		0x4
97 #define BMA280_REG_ACCEL_Z_LSB		0x6
98 
99 #if DT_INST_PROP(0, is_bmc150)
100 	#define BMA280_ACCEL_LSB_BITS	4
101 	#define BMA280_ACCEL_LSB_SHIFT	4
102 #else
103 	#define BMA280_ACCEL_LSB_BITS	6
104 	#define BMA280_ACCEL_LSB_SHIFT	2
105 #endif
106 #define BMA280_ACCEL_LSB_MASK		\
107 		(BIT_MASK(BMA280_ACCEL_LSB_BITS) << BMA280_ACCEL_LSB_SHIFT)
108 
109 #define BMA280_REG_ACCEL_X_MSB		0x3
110 #define BMA280_REG_ACCEL_Y_MSB		0x5
111 #define BMA280_REG_ACCEL_Z_MSB		0x7
112 
113 #define BMA280_THREAD_PRIORITY		10
114 #define BMA280_THREAD_STACKSIZE_UNIT	1024
115 
116 struct bma280_data {
117 	int16_t x_sample;
118 	int16_t y_sample;
119 	int16_t z_sample;
120 	int8_t temp_sample;
121 
122 #ifdef CONFIG_BMA280_TRIGGER
123 	const struct device *dev;
124 	struct gpio_callback gpio_cb;
125 
126 	const struct sensor_trigger *data_ready_trigger;
127 	sensor_trigger_handler_t data_ready_handler;
128 
129 	const struct sensor_trigger *any_motion_trigger;
130 	sensor_trigger_handler_t any_motion_handler;
131 
132 #if defined(CONFIG_BMA280_TRIGGER_OWN_THREAD)
133 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_BMA280_THREAD_STACK_SIZE);
134 	struct k_thread thread;
135 	struct k_sem gpio_sem;
136 #elif defined(CONFIG_BMA280_TRIGGER_GLOBAL_THREAD)
137 	struct k_work work;
138 #endif
139 
140 #endif /* CONFIG_BMA280_TRIGGER */
141 };
142 
143 struct bma280_config {
144 	struct i2c_dt_spec i2c;
145 #ifdef CONFIG_BMA280_TRIGGER
146 	struct gpio_dt_spec int1_gpio;
147 #endif
148 };
149 
150 #ifdef CONFIG_BMA280_TRIGGER
151 int bma280_trigger_set(const struct device *dev,
152 		       const struct sensor_trigger *trig,
153 		       sensor_trigger_handler_t handler);
154 
155 int bma280_attr_set(const struct device *dev,
156 		    enum sensor_channel chan,
157 		    enum sensor_attribute attr,
158 		    const struct sensor_value *val);
159 
160 int bma280_init_interrupt(const struct device *dev);
161 #endif
162 
163 #endif /* ZEPHYR_DRIVERS_SENSOR_BMA280_BMA280_H_ */
164