1 /*
2  * Copyright (c) 2018 Philémon Jaermann
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __SENSOR_LSM303DLHC_MAGN_
8 #define __SENSOR_LSM303DLHC_MAGN_
9 
10 #include <zephyr/drivers/i2c.h>
11 
12 #define LSM303_DLHC_MAGN_X_EN_BIT	BIT(0)
13 #define LSM303DLHC_MAGN_Y_EN_BIT	BIT(1)
14 #define LSM303DLHC_MAGN_Z_EN_BIT	BIT(2)
15 #define LSM303DLHC_MAGN_EN_BITS		(LSM303_DLHC_MAGN_X_EN_BIT | \
16 					LSM303DLHC_MAGN_Y_EN_BIT | \
17 					LSM303DLHC_MAGN_Z_EN_BIT)
18 
19 #if	(CONFIG_LSM303DLHC_MAGN_ODR == 0)
20 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	134
21 #elif	(CONFIG_LSM303DLHC_MAGN_ODR == 1)
22 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	67
23 #elif	(CONFIG_LSM303DLHC_MAGN_ODR == 2)
24 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	34
25 #elif	(CONFIG_LSM303DLHC_MAGN_ODR == 3)
26 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	14
27 #elif	(CONFIG_LSM303DLHC_MAGN_ODR == 4)
28 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	7
29 #elif	(CONFIG_LSM303DLHC_MAGN_ODR == 5)
30 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	4
31 #elif	(CONFIG_LSM303DLHC_MAGN_ODR == 6)
32 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	2
33 #elif	(CONFIG_LSM303DLHC_MAGN_ODR == 7)
34 	#define LSM303DLHC_MAGN_DRDY_WAIT_TIME	1
35 #endif
36 
37 #define LSM303DLHC_MAGN_ODR_SHIFT	2
38 #define LSM303DLHC_MAGN_ODR_BITS	(CONFIG_LSM303DLHC_MAGN_ODR << \
39 					LSM303DLHC_MAGN_ODR_SHIFT)
40 
41 #if	(CONFIG_LSM303DLHC_MAGN_RANGE == 1)
42 	#define LSM303DLHC_MAGN_LSB_GAUSS_XY    1100
43 	#define LSM303DLHC_MAGN_LSB_GAUSS_Z     980
44 #elif	(CONFIG_LSM303DLHC_MAGN_RANGE == 2)
45 	#define LSM303DLHC_MAGN_LSB_GAUSS_XY    855
46 	#define LSM303DLHC_MAGN_LSB_GAUSS_Z     760
47 #elif	(CONFIG_LSM303DLHC_MAGN_RANGE == 3)
48 	#define LSM303DLHC_MAGN_LSB_GAUSS_XY    670
49 	#define LSM303DLHC_MAGN_LSB_GAUSS_Z     600
50 #elif	(CONFIG_LSM303DLHC_MAGN_RANGE == 4)
51 	#define LSM303DLHC_MAGN_LSB_GAUSS_XY    450
52 	#define LSM303DLHC_MAGN_LSB_GAUSS_Z     400
53 #elif	(CONFIG_LSM303DLHC_MAGN_RANGE == 5)
54 	#define LSM303DLHC_MAGN_LSB_GAUSS_XY    400
55 	#define LSM303DLHC_MAGN_LSB_GAUSS_Z     355
56 #elif	(CONFIG_LSM303DLHC_MAGN_RANGE == 6)
57 	#define LSM303DLHC_MAGN_LSB_GAUSS_XY    330
58 	#define LSM303DLHC_MAGN_LSB_GAUSS_Z     295
59 #elif	(CONFIG_LSM303DLHC_MAGN_RANGE == 7)
60 	#define LSM303DLHC_MAGN_LSB_GAUSS_XY    230
61 	#define LSM303DLHC_MAGN_LSB_GAUSS_Z     205
62 #endif
63 
64 #define LSM303DLHC_MAGN_FS_SHIFT	5
65 #define LSM303DLHC_MAGN_FS_BITS		(CONFIG_LSM303DLHC_MAGN_RANGE << \
66 					LSM303DLHC_MAGN_FS_SHIFT)
67 #define LSM303DLHC_MAGN_CONT_UPDATE	0x00
68 #define LSM303DLHC_MAGN_DRDY		BIT(0)
69 
70 #define LSM303DLHC_CRA_REG_M		0x00
71 #define LSM303DLHC_CRB_REG_M		0x01
72 #define LSM303DLHC_MR_REG_M		0x02
73 #define LSM303DLHC_REG_MAGN_X_LSB	0x03
74 #define LSM303DLHC_SR_REG_M		0x09
75 
76 struct lsm303dlhc_magn_data {
77 	int16_t magn_x;
78 	int16_t magn_y;
79 	int16_t magn_z;
80 };
81 
82 struct lsm303dlhc_magn_config {
83 	struct i2c_dt_spec i2c;
84 };
85 #endif /* _SENSOR_LSM303DLHC_MAGN_ */
86