1 /*
2  * Copyright (c) 2023 deveritec GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_TMAG5273_H_
8 #define ZEPHYR_DRIVERS_SENSOR_TMAG5273_H_
9 
10 #include <zephyr/sys/util.h>
11 
12 /* --- Register definitions --- */
13 #define TMAG5273_REG_DEVICE_CONFIG_1		0x00
14 #define TMAG5273_REG_DEVICE_CONFIG_2		0x01
15 #define TMAG5273_REG_SENSOR_CONFIG_1		0x02
16 #define TMAG5273_REG_SENSOR_CONFIG_2		0x03
17 #define TMAG5273_REG_X_THR_CONFIG		0x04
18 #define TMAG5273_REG_Y_THR_CONFIG		0x05
19 #define TMAG5273_REG_Z_THR_CONFIG		0x06
20 #define TMAG5273_REG_T_CONFIG			0x07
21 #define TMAG5273_REG_INT_CONFIG_1		0x08
22 #define TMAG5273_REG_MAG_GAIN_CONFIG		0x09
23 #define TMAG5273_REG_MAG_OFFSET_CONFIG_1	0x0A
24 #define TMAG5273_REG_MAG_OFFSET_CONFIG_2	0x0B
25 #define TMAG5273_REG_I2C_ADDRESS		0x0C
26 #define TMAG5273_REG_DEVICE_ID			0x0D
27 #define TMAG5273_REG_MANUFACTURER_ID_LSB	0x0E
28 #define TMAG5273_REG_MANUFACTURER_ID_MSB	0x0F
29 #define TMAG5273_REG_T_MSB_RESULT		0x10
30 #define TMAG5273_REG_T_LSB_RESULT		0x11
31 #define TMAG5273_REG_X_MSB_RESULT		0x12
32 #define TMAG5273_REG_X_LSB_RESULT		0x13
33 #define TMAG5273_REG_Y_MSB_RESULT		0x14
34 #define TMAG5273_REG_Y_LSB_RESULT		0x15
35 #define TMAG5273_REG_Z_MSB_RESULT		0x16
36 #define TMAG5273_REG_Z_LSB_RESULT		0x17
37 #define TMAG5273_REG_CONV_STATUS		0x18
38 #define TMAG5273_REG_ANGLE_MSB_RESULT		0x19
39 #define TMAG5273_REG_ANGLE_LSB_RESULT		0x1A
40 #define TMAG5273_REG_MAGNITUDE_RESULT		0x1B
41 #define TMAG5273_REG_DEVICE_STATUS		0x1C
42 
43 #define TMAG5273_REG_RESULT_BEGIN	(TMAG5273_REG_T_MSB_RESULT)
44 #define TMAG5273_REG_RESULT_END		(TMAG5273_REG_MAGNITUDE_RESULT)
45 
46 /* Register DEVICE_CONFIG_1 */
47 #define TMAG5273_CRC_EN_POS	7
48 #define TMAG5273_MAG_TEMPCO_POS	5
49 #define TMAG5273_CONV_AVG_POS	2
50 #define TMAG5273_I2C_READ_POS	0
51 
52 #define TMAG5273_CONV_AVB_MSK	GENMASK(4, 2)
53 
54 #define TMAG5273_CRC_DISABLE	(0 << TMAG5273_CRC_EN_POS)
55 #define TMAG5273_CRC_ENABLE	(1 << TMAG5273_CRC_EN_POS)
56 
57 #define TMAG5273_MAGNET_TEMP_COEFF_NONE		(0 << TMAG5273_MAG_TEMPCO_POS)
58 #define TMAG5273_MAGNET_TEMP_COEFF_NDBFE	(1 << TMAG5273_MAG_TEMPCO_POS)
59 #define TMAG5273_MAGNET_TEMP_COEFF_CERAMIC	(3 << TMAG5273_MAG_TEMPCO_POS)
60 
61 #define TMAG5273_CONV_AVG_1	(0 << TMAG5273_CONV_AVG_POS)
62 #define TMAG5273_CONV_AVG_2	(1 << TMAG5273_CONV_AVG_POS)
63 #define TMAG5273_CONV_AVG_4	(2 << TMAG5273_CONV_AVG_POS)
64 #define TMAG5273_CONV_AVG_8	(3 << TMAG5273_CONV_AVG_POS)
65 #define TMAG5273_CONV_AVG_16	(4 << TMAG5273_CONV_AVG_POS)
66 #define TMAG5273_CONV_AVG_32	(5 << TMAG5273_CONV_AVG_POS)
67 
68 #define TMAG5273_I2C_READ_MODE_STANDARD		(0 << TMAG5273_I2C_READ_POS)
69 #define TMAG5273_I2C_READ_MODE_16BIT_SENSOR	(1 << TMAG5273_I2C_READ_POS)
70 #define TMAG5273_I2C_READ_MODE_8BIT_MSB_DATA	(2 << TMAG5273_I2C_READ_POS)
71 
72 /* Register DEVICE_CONFIG_2 */
73 #define TMAG5273_THR_HYST_POS		5
74 #define TMAG5273_LP_LN_POS		4
75 #define TMAG5273_I2C_GLITCH_FILTER_POS	3
76 #define TMAG5273_TRIGGER_MODE_POS	2
77 #define TMAG5273_OPERATING_MODE_POS	0
78 
79 #define TMAG5273_OPERATING_MODE_MSK	GENMASK(1, 0)
80 
81 #define TMAG5273_THR_HYST_COMPLEMENT	(0 << TMAG5273_THR_HYST_POS)
82 #define TMAG5273_THR_HYST_LSB		(1 << TMAG5273_THR_HYST_POS)
83 
84 #define TMAG5273_LP_LOWPOWER	(0 << TMAG5273_LP_LN_POS)
85 #define TMAG5273_LP_LOWNOISE	(1 << TMAG5273_LP_LN_POS)
86 
87 #define TMAG5273_I2C_GLITCH_FILTER_ON	(0 << TMAG5273_I2C_GLITCH_FILTER_POS)
88 #define TMAG5273_I2C_GLITCH_FILTER_OFF	(1 << TMAG5273_I2C_GLITCH_FILTER_POS)
89 
90 #define TMAG5273_TRIGGER_MODE_I2C	(0 << TMAG5273_TRIGGER_MODE_POS)
91 #define TMAG5273_TRIGGER_MODE_INT	(1 << TMAG5273_TRIGGER_MODE_POS)
92 
93 #define TMAG5273_OPERATING_MODE_STANDBY		(0 << TMAG5273_OPERATING_MODE_POS)
94 #define TMAG5273_OPERATING_MODE_SLEEP		(1 << TMAG5273_OPERATING_MODE_POS)
95 #define TMAG5273_OPERATING_MODE_CONTINUOUS	(2 << TMAG5273_OPERATING_MODE_POS)
96 #define TMAG5273_OPERATING_MODE_WAKEUP_SLEEP	(3 << TMAG5273_OPERATING_MODE_POS)
97 
98 /* Register SENSOR_CONFIG_1 */
99 #define TMAG5273_MAG_CH_EN_POS	4
100 #define TMAG5273_SLEEPTIME_POS	0
101 
102 #define TMAG5273_MAG_CH_EN_NONE	(0x0 << TMAG5273_MAG_CH_EN_POS)
103 #define TMAG5273_MAG_CH_EN_X	(0x1 << TMAG5273_MAG_CH_EN_POS)
104 #define TMAG5273_MAG_CH_EN_Y	(0x2 << TMAG5273_MAG_CH_EN_POS)
105 #define TMAG5273_MAG_CH_EN_Z	(0x4 << TMAG5273_MAG_CH_EN_POS)
106 
107 #define TMAG5273_WS_SLEEPTIME_1MS	(0x0 << TMAG5273_SLEEPTIME_POS)
108 #define TMAG5273_WS_SLEEPTIME_5MS	(0x1 << TMAG5273_SLEEPTIME_POS)
109 #define TMAG5273_WS_SLEEPTIME_10MS	(0x2 << TMAG5273_SLEEPTIME_POS)
110 #define TMAG5273_WS_SLEEPTIME_15MS	(0x3 << TMAG5273_SLEEPTIME_POS)
111 #define TMAG5273_WS_SLEEPTIME_20MS	(0x4 << TMAG5273_SLEEPTIME_POS)
112 #define TMAG5273_WS_SLEEPTIME_30MS	(0x5 << TMAG5273_SLEEPTIME_POS)
113 #define TMAG5273_WS_SLEEPTIME_50MS	(0x6 << TMAG5273_SLEEPTIME_POS)
114 #define TMAG5273_WS_SLEEPTIME_100MS	(0x7 << TMAG5273_SLEEPTIME_POS)
115 #define TMAG5273_WS_SLEEPTIME_500MS	(0x8 << TMAG5273_SLEEPTIME_POS)
116 #define TMAG5273_WS_SLEEPTIME_1000MS	(0x9 << TMAG5273_SLEEPTIME_POS)
117 #define TMAG5273_WS_SLEEPTIME_2000MS	(0xA << TMAG5273_SLEEPTIME_POS)
118 #define TMAG5273_WS_SLEEPTIME_5000MS	(0xB << TMAG5273_SLEEPTIME_POS)
119 #define TMAG5273_WS_SLEEPTIME_20000MS	(0xC << TMAG5273_SLEEPTIME_POS)
120 
121 /* Register SENSOR_CONFIG_2 */
122 #define TMAG5273_INT_THRX_COUNT_POS	6
123 #define TMAG5273_INT_MAG_THR_DIR_POS	5
124 #define TMAG5273_GAIN_CORRECTION_CH_POS	4
125 #define TMAG5273_ANGLE_EN_POS		2
126 #define TMAG5273_X_Y_RANGE_POS		1
127 #define TMAG5273_Z_RANGE_POS		0
128 
129 #define TMAG5273_ANGLE_EN_MSK		GENMASK(3, 2)
130 #define TMAG5273_MEAS_RANGE_X_Y_MSK	GENMASK(1, 1)
131 #define TMAG5273_MEAS_RANGE_Z_MSK	GENMASK(0, 0)
132 #define TMAG5273_MEAS_RANGE_XYZ_MSK	(TMAG5273_MEAS_RANGE_X_Y_MSK | TMAG5273_MEAS_RANGE_Z_MSK)
133 
134 #define TMAG5273_INT_THRX_COUNT_1	(0 << TMAG5273_INT_THRX_COUNT_POS)
135 #define TMAG5273_INT_THRX_COUNT_4	(1 << TMAG5273_INT_THRX_COUNT_POS)
136 
137 #define TMAG5273_MAG_THR_DIRECTION_ABOVE	(0 << TMAG5273_INT_MAG_THR_DIR_POS)
138 #define TMAG5273_MAG_THR_DIRECTION_BELOW	(1 << TMAG5273_INT_MAG_THR_DIR_POS)
139 
140 #define TMAG5273_MAG_GAIN_CORRECTION_CH_1	(0 << TMAG5273_GAIN_CORRECTION_CH_POS)
141 #define TMAG5273_MAG_GAIN_CORRECTION_CH_2	(1 << TMAG5273_GAIN_CORRECTION_CH_POS)
142 
143 #define TMAG5273_ANGLE_EN_NONE	(0 << TMAG5273_ANGLE_EN_POS)
144 #define TMAG5273_ANGLE_EN_XY	(1 << TMAG5273_ANGLE_EN_POS)
145 #define TMAG5273_ANGLE_EN_YZ	(2 << TMAG5273_ANGLE_EN_POS)
146 #define TMAG5273_ANGLE_EN_XZ	(3 << TMAG5273_ANGLE_EN_POS)
147 
148 #define TMAG5273_X_Y_MEAS_RANGE_LOW	(0 << TMAG5273_X_Y_RANGE_POS)
149 #define TMAG5273_X_Y_MEAS_RANGE_HIGH	(1 << TMAG5273_X_Y_RANGE_POS)
150 
151 #define TMAG5273_Z_MEAS_RANGE_LOW	(0 << TMAG5273_Z_RANGE_POS)
152 #define TMAG5273_Z_MEAS_RANGE_HIGH	(1 << TMAG5273_Z_RANGE_POS)
153 
154 #define TMAG5273_XYZ_MEAS_RANGE_LOW	(TMAG5273_X_Y_MEAS_RANGE_LOW | TMAG5273_Z_MEAS_RANGE_LOW)
155 #define TMAG5273_XYZ_MEAS_RANGE_HIGH	(TMAG5273_X_Y_MEAS_RANGE_HIGH | TMAG5273_Z_MEAS_RANGE_HIGH)
156 
157 /* Register T_CONFIG */
158 #define TMAG5273_T_THR_CONFIG_POS	1
159 #define TMAG5273_T_CH_EN_POS		0
160 
161 #define TMAG5273_T_CH_EN_DISABLED	(0 << TMAG5273_T_CH_EN_POS)
162 #define TMAG5273_T_CH_EN_ENABLED	(1 << TMAG5273_T_CH_EN_POS)
163 
164 /* Register INT_CONFIG_1 */
165 #define TMAG5273_INT_RSLT_INT_POS	7
166 #define TMAG5273_INT_THRSLD_INT_POS	6
167 #define TMAG5273_INT_STATE_POS		5
168 #define TMAG5273_INT_MODE_POS		2
169 #define TMAG5273_INT_MASK_INTB_POS	0
170 
171 #define TMAG5273_INT_RSLT_INT_DISABLED	(0 << TMAG5273_INT_RSLT_INT_POS)
172 #define TMAG5273_INT_RSLT_INT_ENABLED	(1 << TMAG5273_INT_RSLT_INT_POS)
173 
174 #define TMAG5273_INT_THRSLD_INT_DISABLED	(0 << TMAG5273_INT_THRSLD_INT_POS)
175 #define TMAG5273_INT_THRSLD_INT_ENABLED		(1 << TMAG5273_INT_THRSLD_INT_POS)
176 
177 #define TMAG5273_INT_STATE_LATCHED	(0 << TMAG5273_INT_STATE_POS)
178 #define TMAG5273_INT_STATE_PULSE	(1 << TMAG5273_INT_STATE_POS)
179 
180 #define TMAG5273_INT_MODE_NONE		(0 << TMAG5273_INT_MODE_POS)
181 #define TMAG5273_INT_MODE_INT		(1 << TMAG5273_INT_MODE_POS)
182 #define TMAG5273_INT_MODE_INT_EXC_I2C	(2 << TMAG5273_INT_MODE_POS)
183 #define TMAG5273_INT_MODE_SCL		(3 << TMAG5273_INT_MODE_POS)
184 #define TMAG5273_INT_MODE_SCL_EXC_I2C	(4 << TMAG5273_INT_MODE_POS)
185 
186 #define TMAG5273_INT_MASK_INTB_PIN_ENABLED	(0 << TMAG5273_INT_MASK_INTB_POS)
187 #define TMAG5273_INT_MASK_INTB_PIN_MASKED	(1 << TMAG5273_INT_MASK_INTB_POS)
188 
189 /* Register I2C_ADDRESS */
190 #define TMAG5273_I2C_ADDRESS_POS		1
191 #define TMAG5273_I2C_ADDRESS_UPDATE_EN_POS	0
192 
193 #define TMAG5273_I2C_ADDRESS_UPDATE_DISABLE	(0 << TMAG5273_I2C_ADDRESS_UPDATE_EN_POS)
194 #define TMAG5273_I2C_ADDRESS_UPDATE_ENABLE	(1 << TMAG5273_I2C_ADDRESS_UPDATE_EN_POS)
195 
196 /* Register DEVICE_ID */
197 #define TMAG5273_VER_POS	0
198 #define TMAG5273_VER_MSK	GENMASK(1, 0)
199 
200 #define TMAG5273_VER_TMAG5273X1	(1 << TMAG5273_VER_POS)
201 #define TMAG5273_VER_TMAG5273X2	(2 << TMAG5273_VER_POS)
202 
203 /* Register CONV_STATUS */
204 #define TMAG5273_SET_COUNT_POS		5
205 #define TMAG5273_POR_POS		4
206 #define TMAG5273_DIAG_STATUS_POS	1
207 #define TMAG5273_RESULT_STATUS_POS	0
208 
209 #define TMAG5273_DIAG_STATUS_MSK	GENMASK(1, 1)
210 #define TMAG5273_RESULT_STATUS_MSK	GENMASK(0, 0)
211 
212 #define TMAG5273_POR_OCCURRED		(1 << TMAG5273_POR_POS)
213 #define TMAG5273_DIAG_FAIL		(1 << TMAG5273_DIAG_STATUS_POS)
214 #define TMAG5273_CONVERSION_COMPLETE	(1 << TMAG5273_RESULT_STATUS_POS)
215 
216 /* Register DEVICE_STATUS */
217 #define TMAG5273_INTB_RB_POS	4
218 #define TMAG5273_OSC_ER_POS	3
219 #define TMAG5273_INT_ER_POS	2
220 #define TMAG5273_OTP_CRC_ER_POS	1
221 #define TMAG5273_VCC_UV_ER_POS	0
222 
223 #define TMAG5273_INTB_RB_MSK	GENMASK(4, 4)
224 #define TMAG5273_OSC_ER_MSK	GENMASK(3, 3)
225 #define TMAG5273_INT_ER_MSK	GENMASK(2, 2)
226 #define TMAG5273_OTP_CRC_ER_MSK	GENMASK(1, 1)
227 #define TMAG5273_VCC_UV_ER_MSK	GENMASK(0, 0)
228 
229 #define TMAG5273_INTB_PIN_HIGH	(1 << TMAG5273_INTB_RB_POS)
230 #define TMAG5273_OSC_ERR	(1 << TMAG5273_OSC_ER_POS)
231 #define TMAG5273_INT_ERR	(1 << TMAG5273_INT_ER_POS)
232 #define TMAG5273_OTP_CRC_ERR	(1 << TMAG5273_OTP_CRC_ER_POS)
233 #define TMAG5273_VCC_UV_ERR	(1 << TMAG5273_VCC_UV_ER_POS)
234 
235 #define TMAG5273_RESET_DEVICE_STATUS	0xF
236 
237 /* additional values */
238 #define TMAG5273_MANUFACTURER_ID_MSB	0x54
239 #define TMAG5273_MANUFACTURER_ID_LSB	0x49
240 
241 #define TMAG5273_MEAS_RANGE_LOW_MT_VER1		40
242 #define TMAG5273_MEAS_RANGE_HIGH_MT_VER1	80
243 #define TMAG5273_MEAS_RANGE_LOW_MT_VER2		133
244 #define TMAG5273_MEAS_RANGE_HIGH_MT_VER2	266
245 
246 #define TMAG5273_TEMPERATURE_T_SENS_T0	25
247 #define TMAG5273_TEMPERATURE_T_ADC_T0	17508
248 #define TMAG5273_TEMPERATURE_T_ADC_RES	60.1
249 
250 #define TMAG5273_T_START_SLEEP_US	50
251 #define TMAG5273_T_GO_SLEEP_US		20
252 /**
253  * @brief calculate conversion time as defined in the datasheet
254  * @param avg set averaging value
255  * @param nb_channels number of captured channels
256  */
257 #define TMAG5273_T_CONVERSION_US(avg, nb_channels) (((1 << avg) * 25) * nb_channels + 25)
258 
259 /** OR this bit to any register address to trigger a conversion in standby mode */
260 #define TMAG5273_CONVERSION_START_BIT 0x80
261 
262 #endif /* ZEPHYR_DRIVERS_SENSOR_TMAG5273_H_ */
263