]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/usb/usb2422.h
Massdrop keyboard support (#3780)
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / usb / usb2422.h
1 /*
2 Copyright 2018 Massdrop Inc.
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,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef _USB2422_H_
19 #define _USB2422_H_
20
21 #define USB2422_ALT67_1P0
22 #define REV_USB2422        0x100
23
24 #define USB2422_ADDR       0x58 //I2C device address, one instance
25
26 #define USB2422_HUB_ACTIVE_GROUP    0   //PA
27 #define USB2422_HUB_ACTIVE_PIN      18  //18
28
29 /* -------- USB2422_VID : (USB2422L Offset: 0x00) (R/W 16) Vendor ID -------- */
30 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
31 typedef union {
32   struct {
33     uint16_t VID_LSB : 8;
34     uint16_t VID_MSB : 8;
35   } bit;                       /*!< Structure used for bit  access                  */
36   uint16_t reg;                /*!< Type      used for register access              */
37 } USB2422_VID_Type;
38 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
39
40 /* -------- USB2422_PID : (USB2422L Offset: 0x02) (R/W 16) Product ID -------- */
41 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
42 typedef union {
43   struct {
44     uint16_t PID_LSB : 8;
45     uint16_t PID_MSB : 8;
46   } bit;                       /*!< Structure used for bit  access                  */
47   uint16_t reg;                /*!< Type      used for register access              */
48 } USB2422_PID_Type;
49 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
50
51 /* -------- USB2422_DID : (USB2422L Offset: 0x04) (R/W 16) Device ID -------- */
52 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
53 typedef union {
54   struct {
55     uint16_t DID_LSB : 8;
56     uint16_t DID_MSB : 8;
57   } bit;                       /*!< Structure used for bit  access                  */
58   uint16_t reg;                /*!< Type      used for register access              */
59 } USB2422_DID_Type;
60 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
61
62 /* -------- USB2422_CFG1 : (USB2422L Offset: 0x06) (R/W 8) Configuration Data Byte 1-------- */
63 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
64 typedef union {
65   struct {
66     uint8_t PORT_PWR : 1;
67     uint8_t CURRENT_SNS : 2;
68     uint8_t EOP_DISABLE : 1;
69     uint8_t MTT_ENABLE : 1;
70     uint8_t HS_DISABLE :1;
71     uint8_t :1;
72     uint8_t SELF_BUS_PWR : 1;
73   } bit;                       /*!< Structure used for bit  access                  */
74   uint8_t reg;                 /*!< Type      used for register access              */
75 } USB2422_CFG1_Type;
76 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
77
78 /* -------- USB2422_CFG2 : (USB2422L Offset: 0x07) (R/W 8) Configuration Data Byte 2-------- */
79 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
80 typedef union {
81   struct {
82     uint8_t : 3;
83     uint8_t COMPOUND : 1;
84     uint8_t OC_TIMER :2;
85     uint8_t :1;
86     uint8_t DYNAMIC : 1;
87   } bit;                       /*!< Structure used for bit  access                  */
88   uint8_t reg;                 /*!< Type      used for register access              */
89 } USB2422_CFG2_Type;
90 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
91
92 /* -------- USB2422_CFG3 : (USB2422L Offset: 0x08) (R/W 16) Configuration Data Byte 3-------- */
93 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
94 typedef union {
95   struct {
96     uint8_t STRING_EN : 1;
97     uint8_t  :2;
98     uint8_t PRTMAP_EN :1;
99     uint8_t : 4;
100   } bit;                       /*!< Structure used for bit  access                  */
101   uint8_t reg;                 /*!< Type      used for register access              */
102 } USB2422_CFG3_Type;
103 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
104
105 /* -------- USB2422_NRD : (USB2422L Offset: 0x09) (R/W 8) Non Removable Device -------- */
106 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
107 typedef union {
108   struct {
109     uint8_t : 5;
110     uint8_t PORT2_NR :1;
111     uint8_t PORT1_NR :1;
112     uint8_t : 1;
113   } bit;                       /*!< Structure used for bit  access                  */
114   uint8_t reg;                 /*!< Type      used for register access              */
115 } USB2422_NRD_Type;
116 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
117
118 /* -------- USB2422_PDS : (USB2422L Offset: 0x0A) (R/W 8) Port Diable for Self-Powered Operation -------- */
119 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
120 typedef union {
121   struct {
122     uint8_t : 1;
123     uint8_t PORT1_DIS :1;
124     uint8_t PORT2_DIS :1;
125     uint8_t : 5;
126   } bit;                       /*!< Structure used for bit  access                  */
127   uint8_t reg;                 /*!< Type      used for register access              */
128 } USB2422_PDS_Type;
129 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
130
131 /* -------- USB2422_PDB : (USB2422L Offset: 0x0B) (R/W 8) Port Diable for Bus-Powered Operation -------- */
132 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
133 typedef union {
134   struct {
135     uint8_t : 1;
136     uint8_t PORT1_DIS :1;
137     uint8_t PORT2_DIS :1;
138     uint8_t : 5;
139   } bit;                       /*!< Structure used for bit  access                  */
140   uint8_t reg;                 /*!< Type      used for register access              */
141 } USB2422_PDB_Type;
142 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
143
144 /* -------- USB2422_MAXPS : (USB2422L Offset: 0x0C) (R/W 8) Max Power for Self-Powered Operation -------- */
145 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
146 typedef union {
147   struct {
148     uint8_t MAX_PWR_SP : 8;
149   } bit;                       /*!< Structure used for bit  access                  */
150   uint8_t reg;                 /*!< Type      used for register access              */
151 } USB2422_MAXPS_Type;
152 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
153
154 /* -------- USB2422_MAXPB : (USB2422L Offset: 0x0D) (R/W 8) Max Power for Bus-Powered Operation -------- */
155 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
156 typedef union {
157   struct {
158     uint8_t MAX_PWR_BP : 8;
159   } bit;                       /*!< Structure used for bit  access                  */
160   uint8_t reg;                 /*!< Type      used for register access              */
161 } USB2422_MAXPB_Type;
162 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
163
164 /* -------- USB2422_HCMCS : (USB2422L Offset: 0x0E) (R/W 8) Hub Controller Max Current for Self-Powered Operation -------- */
165 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
166 typedef union {
167   struct {
168     uint8_t HC_MAX_C_SP : 8;
169   } bit;                       /*!< Structure used for bit  access                  */
170   uint8_t reg;                 /*!< Type      used for register access              */
171 } USB2422_HCMCS_Type;
172 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
173
174 /* -------- USB2422_HCMCB : (USB2422L Offset: 0x0F) (R/W 8) Hub Controller Max Current for Bus-Powered Operation -------- */
175 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
176 typedef union {
177   struct {
178     uint8_t HC_MAX_C_BP : 8;
179   } bit;                       /*!< Structure used for bit  access                  */
180   uint8_t reg;                 /*!< Type      used for register access              */
181 } USB2422_HCMCB_Type;
182 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
183
184 /* -------- USB2422_PWRT : (USB2422L Offset: 0x10) (R/W 8) Power On Time -------- */
185 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
186 typedef union {
187   struct {
188     uint8_t POWER_ON_TIME : 8;
189   } bit;                       /*!< Structure used for bit  access                  */
190   uint8_t reg;                 /*!< Type      used for register access              */
191 } USB2422_PWRT_Type;
192 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
193
194 /* -------- USB2422_LANGID LSB : (USB2422L Offset: 0x11) (R/W 16) Language ID -------- */
195 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
196 typedef union {
197   struct {
198     uint8_t LANGID_LSB : 8;
199   } bit;                       /*!< Structure used for bit  access                  */
200   uint8_t reg;                 /*!< Type      used for register access              */
201 } USB2422_LANGID_LSB_Type;
202 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
203
204 /* -------- USB2422_LANGID MSB : (USB2422L Offset: 0x12) (R/W 16) Language ID -------- */
205 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
206 typedef union {
207   struct {
208     uint8_t LANGID_MSB : 8;
209   } bit;                       /*!< Structure used for bit  access                  */
210   uint8_t reg;                 /*!< Type      used for register access              */
211 } USB2422_LANGID_MSB_Type;
212 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
213
214
215 /* -------- USB2422_MFRSL : (USB2422L Offset: 0x13) (R/W 8) Manufacturer String Length -------- */
216 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
217 typedef union {
218   struct {
219     uint8_t MFR_STR_LEN : 8;
220   } bit;                       /*!< Structure used for bit  access                  */
221   uint8_t reg;                 /*!< Type      used for register access              */
222 } USB2422_MFRSL_Type;
223 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
224
225 /* -------- USB2422_PRDSL : (USB2422L Offset: 0x14) (R/W 8) Product String Length -------- */
226 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
227 typedef union {
228   struct {
229     uint8_t PRD_STR_LEN : 8;
230   } bit;                       /*!< Structure used for bit  access                  */
231   uint8_t reg;                 /*!< Type      used for register access              */
232 } USB2422_PRDSL_Type;
233 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
234
235 /* -------- USB2422_SERSL : (USB2422L Offset: 0x15) (R/W 8) Serial String Length -------- */
236 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
237 typedef union {
238   struct {
239     uint8_t SER_STR_LEN : 8;
240   } bit;                       /*!< Structure used for bit  access                  */
241   uint8_t reg;                 /*!< Type      used for register access              */
242 } USB2422_SERSL_Type;
243 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
244
245 /* -------- USB2422_MFRSTR : (USB2422L Offset: 0x16-53) (R/W 8) Maufacturer String -------- */
246 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
247 typedef uint16_t USB2422_MFRSTR_Type;
248 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
249
250 /* -------- USB2422_PRDSTR : (USB2422L Offset: 0x54-91) (R/W 8) Product String -------- */
251 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
252 typedef uint16_t USB2422_PRDSTR_Type;
253 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
254
255 /* -------- USB2422_SERSTR : (USB2422L Offset: 0x92-CF) (R/W 8) Serial String -------- */
256 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
257 typedef uint16_t USB2422_SERSTR_Type;
258 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
259
260 /* -------- USB2422_BCEN : (USB2422L Offset: 0xD0) (R/W 8) Battery Charging Enable -------- */
261 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
262 typedef union {
263   struct {
264     uint8_t : 1;
265     uint8_t PORT1_BCE :1;
266     uint8_t PORT2_BCE :1;
267     uint8_t : 5;
268   } bit;                       /*!< Structure used for bit  access                  */
269   uint8_t reg;                 /*!< Type      used for register access              */
270 } USB2422_BCEN_Type;
271 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
272
273 /* -------- USB2422_BOOSTUP : (USB2422L Offset: 0xF6) (R/W 8) Boost Upstream -------- */
274 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
275 typedef union {
276   struct {
277     uint8_t BOOST :2;
278     uint8_t : 6;
279   } bit;                       /*!< Structure used for bit  access                  */
280   uint8_t reg;                 /*!< Type      used for register access              */
281 } USB2422_BOOSTUP_Type;
282 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
283
284 /* -------- USB2422_BOOSTDOWN : (USB2422L Offset: 0xF8) (R/W 8) Boost Downstream -------- */
285 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
286 typedef union {
287   struct {
288     uint8_t BOOST1 :2;
289     uint8_t BOOST2 :2;
290     uint8_t : 4;
291   } bit;                       /*!< Structure used for bit  access                  */
292   uint8_t reg;                 /*!< Type      used for register access              */
293 } USB2422_BOOSTDOWN_Type;
294 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
295
296 /* -------- USB2422_PRTSP : (USB2422L Offset: 0xFA) (R/W 8) Port Swap -------- */
297 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
298 typedef union {
299   struct {
300     uint8_t : 1;
301     uint8_t PORT1_SP :1;
302     uint8_t PORT2_SP :1;
303     uint8_t : 5;
304   } bit;                       /*!< Structure used for bit  access                  */
305   uint8_t reg;                 /*!< Type      used for register access              */
306 } USB2422_PRTSP_Type;
307 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
308
309 /* -------- USB2422_PRTR12 : (USB2422L Offset: 0xFB) (R/W 8) Port 1/2 Remap -------- */
310 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
311 typedef union {
312   struct {
313     uint8_t PORT1_REMAP: 4;
314     uint8_t PORT2_REMAP: 4;
315   } bit;                       /*!< Structure used for bit  access                  */
316   uint8_t reg;                 /*!< Type      used for register access              */
317 } USB2422_PRTR12_Type;
318 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
319 #define USB2422_PRTR12_DISABLE 0
320 #define USB2422_PRT12_P2TOL1 1
321 #define USB2422_PRT12_P2XTOL2 2
322 #define USB2422_PRT12_P1TOL1 1
323 #define USB2422_PRT12_P1XTOL2 2
324
325 /* -------- USB2422_STCD : (USB2422L Offset: 0xFF) (R/W 8) Status Command -------- */
326 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
327 typedef union {
328   struct {
329     uint8_t USB_ATTACH: 1;
330     uint8_t RESET: 1;
331     uint8_t INTF_PWRDN: 1;
332     uint8_t : 5;
333   } bit;                       /*!< Structure used for bit  access                  */
334   uint8_t reg;                 /*!< Type      used for register access              */
335 } USB2422_STCD_Type;
336 #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */
337
338 /** \brief USB2422 device hardware registers */
339 #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__))
340 typedef struct {
341   USB2422_VID_Type          VID;        /**< \brief Offset: 0x00*/
342   USB2422_PID_Type          PID;        /**< \brief Offset: 0x02*/
343   USB2422_DID_Type          DID;        /**< \brief Offset: 0x04*/
344   USB2422_CFG1_Type         CFG1;       /**< \brief Offset: 0x06*/
345   USB2422_CFG2_Type         CFG2;       /**< \brief Offset: 0x07*/
346   USB2422_CFG3_Type         CFG3;       /**< \brief Offset: 0x08*/
347   USB2422_NRD_Type          NRD;        /**< \brief Offset: 0x09*/
348   USB2422_PDS_Type          PDS;        /**< \brief Offset: 0x0A*/
349   USB2422_PDB_Type          PDB;        /**< \brief Offset: 0x0B*/
350   USB2422_MAXPS_Type        MAXPS;      /**< \brief Offset: 0x0C*/
351   USB2422_MAXPB_Type        MAXPB;      /**< \brief Offset: 0x0D*/
352   USB2422_HCMCS_Type        HCMCS;      /**< \brief Offset: 0x0E*/
353   USB2422_HCMCB_Type        HCMCB;      /**< \brief Offset: 0x0F*/
354   USB2422_PWRT_Type         PWRT;       /**< \brief Offset: 0x10*/
355   USB2422_LANGID_LSB_Type   LANGID_LSB; /**< \brief Offset: 0x11*/
356   USB2422_LANGID_MSB_Type   LANGID_MSB; /**< \brief Offset: 0x12*/
357   USB2422_MFRSL_Type        MFRSL;      /**< \brief Offset: 0x13*/
358   USB2422_PRDSL_Type        PRDSL;      /**< \brief Offset: 0x14*/
359   USB2422_SERSL_Type        SERSL;      /**< \brief Offset: 0x15*/
360   USB2422_MFRSTR_Type       MFRSTR[31]; /**< \brief Offset: 0x16*/
361   USB2422_PRDSTR_Type       PRDSTR[31]; /**< \brief Offset: 0x54*/
362   USB2422_SERSTR_Type       SERSTR[31]; /**< \brief Offset: 0x92*/
363   USB2422_BCEN_Type         BCEN;       /**< \brief Offset: 0xD0*/
364   uint8_t                   Reserved1[0x25];
365   USB2422_BOOSTUP_Type      BOOSTUP;    /**< \brief Offset: 0xF6*/
366   uint8_t                   Reserved2[0x1];
367   USB2422_BOOSTDOWN_Type    BOOSTDOWN;  /**< \brief Offset: 0xF8*/
368   uint8_t                   Reserved3[0x1];
369   USB2422_PRTSP_Type        PRTSP;      /**< \brief Offset: 0xFA*/
370   USB2422_PRTR12_Type       PRTR12;     /**< \brief Offset: 0xFB*/
371   uint8_t                   Reserved4[0x3];
372   USB2422_STCD_Type         STCD;       /**< \brief Offset: 0xFF*/
373 } Usb2422;
374 #endif
375
376 #define PORT_DETECT_RETRY_INTERVAL      2000
377
378 #define USB_EXTRA_ADC_THRESHOLD         900
379
380 #define USB_EXTRA_STATE_DISABLED                0
381 #define USB_EXTRA_STATE_ENABLED                 1
382 #define USB_EXTRA_STATE_UNKNOWN                 2
383 #define USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG   3
384
385 #define USB_HOST_PORT_1             0
386 #define USB_HOST_PORT_2             1
387 #define USB_HOST_PORT_UNKNOWN       2
388
389 extern uint8_t usb_host_port;
390 extern uint8_t usb_extra_state;
391 extern uint8_t usb_extra_manual;
392 extern uint8_t usb_gcr_auto;
393
394 void USB2422_init(void);
395 void USB_reset(void);
396 void USB_configure(void);
397 uint16_t USB_active(void);
398 void USB_set_host_by_voltage(void);
399 uint16_t adc_get(uint8_t muxpos);
400 uint8_t USB2422_Port_Detect_Init(void);
401 void USB_HandleExtraDevice(void);
402 void USB_ExtraSetState(uint8_t state);
403
404 #endif //_USB2422_H_
405