altos: Get MMC5983 driver working
[fw/altos] / src / drivers / ao_mmc5983.h
1 /*
2  * Copyright © 2021 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_MMC5983_H_
20 #define _AO_MMC5983_H_
21
22 #define MMC5983_READ            0x80
23
24 #define MMC5983_I2C_ADDR        0x60
25
26 #define MMC5983_CONFIG_A        0
27
28 #define  MMC5983_CONFIG_A_MA            5
29 #define  MMC5983_CONFIG_A_MA_1                  0
30 #define  MMC5983_CONFIG_A_MA_2                  1
31 #define  MMC5983_CONFIG_A_MA_4                  2
32 #define  MMC5983_CONFIG_A_MA_8                  3
33 #define  MMC5983_CONFIG_A_MA_MASK               3
34
35 #define  MMC5983_CONFIG_A_DO            2
36 #define   MMC5983_CONFIG_A_DO_0_75              0
37 #define   MMC5983_CONFIG_A_DO_1_5               1
38 #define   MMC5983_CONFIG_A_DO_3                 2
39 #define   MMC5983_CONFIG_A_DO_7_5               3
40 #define   MMC5983_CONFIG_A_DO_15                4
41 #define   MMC5983_CONFIG_A_DO_30                5
42 #define   MMC5983_CONFIG_A_DO_75                6
43 #define   MMC5983_CONFIG_A_DO_MASK              7
44
45 #define MMC5983_CONFIG_A_MS             0
46 #define  MMC5983_CONFIG_A_MS_NORMAL             0
47 #define  MMC5983_CONFIG_A_MS_POSITIVE_BIAS      1
48 #define  MMC5983_CONFIG_A_MS_NEGATIVE_BIAS      2
49 #define  MMC5983_CONFIG_A_MS_MASK               3
50
51 #define MMC5983_CONFIG_B        1
52
53 #define MMC5983_CONFIG_B_GN             5
54 #define  MMC5983_CONFIG_B_GN_0_88               0
55 #define  MMC5983_CONFIG_B_GN_1_3                1
56 #define  MMC5983_CONFIG_B_GN_1_9                2
57 #define  MMC5983_CONFIG_B_GN_2_5                3
58 #define  MMC5983_CONFIG_B_GN_4_0                4
59 #define  MMC5983_CONFIG_B_GN_4_7                5
60 #define  MMC5983_CONFIG_B_GN_5_6                6
61 #define  MMC5983_CONFIG_B_GN_8_1                7
62 #define  MMC5983_CONFIG_B_GN_MASK               7
63
64 #define MMC5983_MODE            2
65 #define  MMC5983_MODE_CONTINUOUS        0
66 #define  MMC5983_MODE_SINGLE            1
67 #define  MMC5983_MODE_IDLE              2
68
69 #define MMC5983_X_OUT_0         0
70 #define MMC5983_X_OUT_1         1
71 #define MMC5983_Y_OUT_0         2
72 #define MMC5983_Y_OUT_1         3
73 #define MMC5983_Z_OUT_0         4
74 #define MMC5983_Z_OUT_1         5
75 #define MMC5983_XYZ_OUT_2       6
76 #define MMC5983_T_OUT           7
77
78 #define MMC5983_STATUS          8
79 # define MMC5983_STATUS_OTP_READ_DONE           4
80 # define MMC5983_STATUS_MEAS_T_DONE             1
81 # define MMC5983_STATUS_MEAS_M_DONE             0
82
83 #define MMC5983_CONTROL_0       9
84 # define MMC5983_CONTROL_0_OTP_READ             6
85 # define MMC5983_CONTROL_0_AUTO_SR_EN           5
86 # define MMC5983_CONTROL_0_RESET                4
87 # define MMC5983_CONTROL_0_SET                  3
88 # define MMC5983_CONTROL_0_INT_MEAS_DONE_EN     2
89 # define MMC5983_CONTROL_0_TM_T                 1
90 # define MMC5983_CONTROL_0_TM_M                 0
91
92 #define MMC5983_CONTROL_1       0xa
93 # define MMC5983_CONTROL_1_SW_RST               7
94 # define MMC5983_CONTROL_1_YZ_INHIBIT           3
95 # define MMC5983_CONTROL_1_X_INHIBIT            2
96 # define MMC5983_CONTROL_1_BW                   0
97 # define MMC5983_CONTROL_1_BW_100                       0
98 # define MMC5983_CONTROL_1_BW_200                       1
99 # define MMC5983_CONTROL_1_BW_400                       2
100 # define MMC5983_CONTROL_1_BW_800                       3
101 #define MMC5983_CONTROL_2       0xb
102 # define MMC5983_CONTROL_2_EN_PRD_SET           7
103 # define MMC5983_CONTROL_2_PRD_SET              4
104 # define MMC5983_CONTROL_2_PRD_SET_1                    0
105 # define MMC5983_CONTROL_2_PRD_SET_25                   1
106 # define MMC5983_CONTROL_2_PRD_SET_75                   2
107 # define MMC5983_CONTROL_2_PRD_SET_100                  3
108 # define MMC5983_CONTROL_2_PRD_SET_250                  4
109 # define MMC5983_CONTROL_2_PRD_SET_500                  5
110 # define MMC5983_CONTROL_2_PRD_SET_1000                 6
111 # define MMC5983_CONTROL_2_PRD_SET_2000                 7
112 # define MMC5983_CONTROL_2_CMM_EN               3
113 # define MMC5983_CONTROL_2_CM_FREQ              0
114 # define MMC5983_CONTROL_2_CM_FREQ_OFF                  0
115 # define MMC5983_CONTROL_2_CM_FREQ_1HZ                  1
116 # define MMC5983_CONTROL_2_CM_FREQ_10HZ                 2
117 # define MMC5983_CONTROL_2_CM_FREQ_20HZ                 3
118 # define MMC5983_CONTROL_2_CM_FREQ_50HZ                 4
119 # define MMC5983_CONTROL_2_CM_FREQ_100HZ                5
120 # define MMC5983_CONTROL_2_CM_FREQ_200HZ                6
121 # define MMC5983_CONTROL_2_CM_FREQ_1000HZ               7
122
123 #define MMC5983_CONTROL_3       0xc
124 #define  MMC5983_CONTROL_3_SPI_3W               6
125 #define  MMC5983_CONTROL_3_ST_ENM               2
126 #define  MMC5983_CONTROL_3_ST_ENP               1
127
128 #define MMC5983_PRODUCT_ID      0x2f
129 #define MMC5983_PRODUCT_ID_PRODUCT_I2C  0x30
130 #define MMC5983_PRODUCT_ID_PRODUCT_SPI  0x31
131
132
133 struct ao_mmc5983_sample {
134         int16_t         x, y, z;
135 };
136
137 struct ao_mmc5983_raw {
138         uint8_t         addr;
139         uint8_t         x0;
140         uint8_t         x1;
141         uint8_t         y0;
142         uint8_t         y1;
143         uint8_t         z0;
144         uint8_t         z1;
145         uint8_t         xyz2;
146 };
147
148 extern struct ao_mmc5983_sample ao_mmc5983_current;
149
150 void
151 ao_mmc5983_init(void);
152
153 #endif /* _AO_MMC5983_H_ */