1 /* sensor_bmc150_magn.h - header file for BMC150 magnetometer sensor driver */
2 
3 /*
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8 
9 #ifndef ZEPHYR_DRIVERS_SENSOR_BMC150_MAGN_BMC150_MAGN_H_
10 #define ZEPHYR_DRIVERS_SENSOR_BMC150_MAGN_BMC150_MAGN_H_
11 
12 #include <zephyr/types.h>
13 #include <zephyr/drivers/i2c.h>
14 #include <zephyr/sys/util.h>
15 
16 #define BMC150_MAGN_REG_CHIP_ID		0x40
17 #define BMC150_MAGN_CHIP_ID_VAL		0x32
18 
19 #define BMC150_MAGN_REG_X_L		0x42
20 #define BMC150_MAGN_REG_X_M		0x43
21 #define BMC150_MAGN_REG_Y_L		0x44
22 #define BMC150_MAGN_REG_Y_M		0x45
23 #define BMC150_MAGN_SHIFT_XY_L		3
24 #define BMC150_MAGN_REG_Z_L		0x46
25 #define BMC150_MAGN_REG_Z_M		0x47
26 #define BMC150_MAGN_SHIFT_Z_L		1
27 #define BMC150_MAGN_REG_RHALL_L		0x48
28 #define BMC150_MAGN_REG_RHALL_M		0x49
29 #define BMC150_MAGN_SHIFT_RHALL_L	2
30 
31 #define BMC150_MAGN_REG_INT_STATUS	0x4A
32 
33 #define BMC150_MAGN_REG_POWER		0x4B
34 #define BMC150_MAGN_MASK_POWER_CTL	BIT(0)
35 
36 #define BMC150_MAGN_REG_OPMODE_ODR	0x4C
37 #define BMC150_MAGN_MASK_OPMODE		(BIT(2) | BIT(1))
38 #define BMC150_MAGN_SHIFT_OPMODE	1
39 #define BMC150_MAGN_MODE_NORMAL		0x00
40 #define BMC150_MAGN_MODE_FORCED		0x01
41 #define BMC150_MAGN_MODE_SLEEP		0x03
42 #define BMC150_MAGN_MASK_ODR		(BIT(5) | BIT(4) | BIT(3))
43 #define BMC150_MAGN_SHIFT_ODR		3
44 
45 #define BMC150_MAGN_REG_LOW_THRESH	0x4F
46 #define BMC150_MAGN_REG_HIGH_THRESH	0x50
47 #define BMC150_MAGN_REG_REP_XY		0x51
48 #define BMC150_MAGN_REG_REP_Z		0x52
49 #define BMC150_MAGN_REG_REP_DATAMASK	0xFF
50 
51 #define BMC150_MAGN_REG_TRIM_START	0x5D
52 #define BMC150_MAGN_REG_TRIM_END	0x71
53 
54 #define BMC150_MAGN_XY_OVERFLOW_VAL	-4096
55 #define BMC150_MAGN_Z_OVERFLOW_VAL	-16384
56 
57 #define BMC150_MAGN_REGVAL_TO_REPXY(regval)	(((regval) * 2) + 1)
58 #define BMC150_MAGN_REGVAL_TO_REPZ(regval)	((regval) + 1)
59 #define BMC150_MAGN_REPXY_TO_REGVAL(rep)	(((rep) - 1) / 2)
60 #define BMC150_MAGN_REPZ_TO_REGVAL(rep)		((rep) - 1)
61 
62 #define BMC150_MAGN_REG_INT                     0x4D
63 
64 #define BMC150_MAGN_REG_INT_DRDY                0x4E
65 #define BMC150_MAGN_MASK_DRDY_EN                BIT(7)
66 #define BMC150_MAGN_SHIFT_DRDY_EN               7
67 #define BMC150_MAGN_MASK_DRDY_INT3              BIT(6)
68 #define BMC150_MAGN_MASK_DRDY_Z_EN              BIT(5)
69 #define BMC150_MAGN_MASK_DRDY_Y_EN              BIT(4)
70 #define BMC150_MAGN_MASK_DRDY_X_EN              BIT(3)
71 #define BMC150_MAGN_MASK_DRDY_DR_POLARITY       BIT(2)
72 #define BMC150_MAGN_SHIFT_DRDY_DR_POLARITY      2
73 #define BMC150_MAGN_MASK_DRDY_LATCHING          BIT(1)
74 #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY     BIT(0)
75 
76 #if defined(CONFIG_BMC150_MAGN_SAMPLING_REP_XY) || \
77 	defined(CONFIG_BMC150_MAGN_SAMPLING_REP_Z)
78 	#define BMC150_MAGN_SET_ATTR_REP
79 #endif
80 
81 #if defined(CONFIG_BMC150_MAGN_SAMPLING_RATE_RUNTIME) || \
82 	defined(BMC150_MAGN_SET_ATTR_REP)
83 	#define BMC150_MAGN_SET_ATTR
84 #endif
85 
86 struct bmc150_magn_config {
87 	struct i2c_dt_spec i2c;
88 #if defined(CONFIG_BMC150_MAGN_TRIGGER_DRDY)
89 	struct gpio_dt_spec int_gpio;
90 #endif
91 };
92 
93 struct bmc150_magn_trim_regs {
94 	int8_t x1;
95 	int8_t y1;
96 	uint16_t reserved1;
97 	uint8_t reserved2;
98 	int16_t z4;
99 	int8_t x2;
100 	int8_t y2;
101 	uint16_t reserved3;
102 	int16_t z2;
103 	uint16_t z1;
104 	uint16_t xyz1;
105 	int16_t z3;
106 	int8_t xy2;
107 	uint8_t xy1;
108 } __packed;
109 
110 struct bmc150_magn_data {
111 	struct k_sem sem;
112 
113 #if defined(CONFIG_BMC150_MAGN_TRIGGER)
114 	K_KERNEL_STACK_MEMBER(thread_stack,
115 			      CONFIG_BMC150_MAGN_TRIGGER_THREAD_STACK);
116 	struct k_thread thread;
117 #endif
118 
119 #if defined(CONFIG_BMC150_MAGN_TRIGGER_DRDY)
120 	const struct device *gpio_drdy;
121 	const struct device *dev;
122 	struct gpio_callback gpio_cb;
123 	const struct sensor_trigger *trigger_drdy;
124 	sensor_trigger_handler_t handler_drdy;
125 #endif
126 
127 	struct bmc150_magn_trim_regs tregs;
128 	int rep_xy, rep_z, odr, max_odr;
129 	int sample_x, sample_y, sample_z;
130 };
131 
132 enum bmc150_magn_power_modes {
133 	BMC150_MAGN_POWER_MODE_SUSPEND,
134 	BMC150_MAGN_POWER_MODE_SLEEP,
135 	BMC150_MAGN_POWER_MODE_NORMAL,
136 };
137 
138 enum bmc150_magn_presets {
139 	LOW_POWER_PRESET,
140 	REGULAR_PRESET,
141 	ENHANCED_REGULAR_PRESET,
142 	HIGH_ACCURACY_PRESET
143 };
144 
145 #if defined(CONFIG_BMC150_MAGN_PRESET_LOW_POWER)
146 	#define BMC150_MAGN_DEFAULT_PRESET LOW_POWER_PRESET
147 #elif defined(CONFIG_BMC150_MAGN_PRESET_REGULAR)
148 	#define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
149 #elif defined(CONFIG_BMC150_MAGN_PRESET_ENHANCED_REGULAR)
150 	#define BMC150_MAGN_DEFAULT_PRESET ENHANCED_REGULAR_PRESET
151 #elif defined(CONFIG_BMC150_MAGN_PRESET_HIGH_ACCURACY)
152 	#define BMC150_MAGN_DEFAULT_PRESET HIGH_ACCURACY_PRESET
153 #endif
154 
155 enum bmc150_magn_axis {
156 	BMC150_MAGN_AXIS_X,
157 	BMC150_MAGN_AXIS_Y,
158 	BMC150_MAGN_AXIS_Z,
159 	BMC150_MAGN_RHALL,
160 	BMC150_MAGN_AXIS_XYZ_MAX = BMC150_MAGN_RHALL,
161 	BMC150_MAGN_AXIS_XYZR_MAX,
162 };
163 
164 #if defined(CONFIG_BMC150_MAGN_TRIGGER)
165 int bmc150_magn_trigger_set(const struct device *dev,
166 			    const struct sensor_trigger *trig,
167 			    sensor_trigger_handler_t handler);
168 
169 int bmc150_magn_init_interrupt(const struct device *dev);
170 #endif
171 
172 #endif /* ZEPHYR_DRIVERS_SENSOR_BMC150_MAGN_BMC150_MAGN_H_ */
173