aboutsummaryrefslogtreecommitdiff
path: root/keyboards/system76/launch_1/usb_mux.c
diff options
context:
space:
mode:
authorjecassis <jecassis@users.noreply.github.com>2022-01-11 01:39:10 +0000
committerGitHub <noreply@github.com>2022-01-10 17:39:10 -0800
commit8920db2b57b616e162e2d3162577db6f021e7658 (patch)
tree3fc960146f72f75231b70fc0a4c71c590b61355d /keyboards/system76/launch_1/usb_mux.c
parentda1a01b811a382ac92b44558dc4f6eec9314571f (diff)
downloadqmk_firmware-8920db2b57b616e162e2d3162577db6f021e7658.tar.gz
qmk_firmware-8920db2b57b616e162e2d3162577db6f021e7658.zip
[Keyboard] Add system76/launch_1 keyboard (#15395)
* WIP: virgo keyboard * Finish layout * Enable debugging and format * Debug keypresses * Add function layer * Fix whitespace * Fix some more whitespace * Add Jeremy's map * Add left split ortho 2U board * Enabled extrakeys for volume & media control * More work on split ortho 2U... ...but still not complete * Finish default layout * Fix many issues by renaming the keyboard * Add right half as a keyboard * Update config for right side * WIP: Add split ortho 2U board * WIP: Correct rules & config * More work on split ortho * More work on split ortho 2u * Nearing completion on split ortho * Remove left and right separate keyboards. Split ortho 2U is complete and they are not needed. * Add uglydense keyboard * Rename directory for uglydense * Swap right Fn and right ctrl keys * Add jeremy's layout * Add ian layout * Add reset key, which is very useful for flashing. * Add Levi's layout * Update Levi's layout * Fix Levi's Layout * Fix Levi's layout again * Add a README with some basic information * Add keymap customization info to uglydense readme * Make the readme make a little more sense. * Make John a layout with left fn and left super swapped * Update John's layout * Add Carl's layout * Add Sean's layout * Add reset keys to all layouts * Swap LALT & LGUI on default layout * shpurk keyboard: initial commit * Add nathaniel & shpurk layouts * Update instructions to include necesarry dependencies * Add Lrrr keyboard, ruler of Omicron Persei 8 * Update README for Lrrr * Update Lrrr it uses Caterina bootloader Also B1 wasn't working for Row 6, so I changed that to F6 * Swap RCTL & RALT * Un-swap RCTL and RALT, making RCTL closer to right thumb * Add printscreen to my layout * Rename lrrr to Launch, enbiggen L-Shift to 2U * Add layout files for Launch * Rename launch to launch_1 * Add levi layout for ortho_split_2u * Update carl keymap * Add launch testboard * Implement keyboard keycode reading using raw hid * Enable dynamic keymap * Add config support to launch_1 * Implement probe command, make logical key names match configurator * Update logical key names again * Add layout generator for keyboard configurator * Add board name and version * Add board name and version to test board * Fix issues with compiling board and version commands * Rename uglydense to launch_alpha_1 and launch_1 to launch_alpha_2 * Generate layouts for other launch prototypes * Fix launch_alpha_1 logical names * Add launch_beta_1 * Fix building production hex file with atmel-dfu bootloader * Limit backlight brightness * USB mux handling * Allow repeat start * Do USB MUX init before bootmagic * Fixes for mux init * Fix register write size for programmable function control * Ensure bit shifts are correct * Improve documentation * Fix when i2c read ack condition happens * Fix extra start in i2c_set * Add ISP instructions * Add fuse information * Refactor * Add RGB matrix support * Fix RGB matrix * Update Jeremy layout * Enable audio controls * Update Jeremy layout * Ensure that n-key rollover is used * Port changes to other launch boards * Configuration values for starting HSV and speed (#7740) * Define default HSV and speed for RGB matrix. * Documentation for configuration values RGB_MATRIX_STARTUP_HUE, RGB_MATRIX_STARTUP_SAT and RGB_MATRIX_STARTUP_VAL. * Document RGB_MATRIX_STARTUP_SPD. * Preserve the ordering. * Set default RGB mode, hue, and saturation * Reduce AVR clock to 8MHz * Update launch_beta_1 with new USB ID * Update default LED mode * Set default hue * Disable RGB while suspended * Add led value and color commands * Add max value to CMD_LED_GET_VALUE * Do not save custom mode to eeprom * Add reset to bootloader command for Launch keyboard * Rename launch_beta_1 to launch_1 * Enable LTO when compiling for launch_1 * Allow setting individual LED's * Convert tabs to spaces * Unlock on RESET keypress: - Display unlock pattern - Disable LED get/set functions - Enable reset to bootloader function * Reduce brightness of rainbow backdrop in unlock pattern * Add hid commands for setting led matrix mode This changes the color setting to not change the mode, and set the hue and saturation for QMK effects. * Fix `CMD_LED_GET_MODE` * Add Levi's Launch layout * Fix layer mistake in Levi's Launch layout * Add matrix command * Define default RGB matrix speed * Add active_keys effect * Move definition of RGB modes inside ifdef testing for custom RGB modes * RGB parameters per layer * fix: Call `system76_ec_rgb_layer` after setting mode * Include layer 3 and 4 in default layout for launch_1 I added support for layer 3 and 4 to the Configurator, but it seems to load bogus values. `dynamic_keymap_reset()` has a comment saying: ``` // Reset the keymaps in EEPROM to what is in flash. // All keyboards using dynamic keymaps should define a layout // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT ``` Other keyboards seem to have default layouts that only list the first two layers while setting `DYNAMIC_KEYMAP_LAYER_COUNT` to 4, but whatever. This appears to make the Configurator behave as expected with layer 3 and 4. * Use EEPROM to store RGB parameters * Add layer 2 and 3 to other keymaps * Add LED_SAVE command * Use eeprom_update_block to improve performance * Revert "Configuration values for starting HSV and speed (#7740)" This reverts commit de1f60fd370b4769336b8a707ee12657aee46412. * Update launch_1 rules.mk for changes in Qmk * WIP keycodes matching EC behavior * Modify default layout to match design * Apply updates to jeremy layout * Improvements to RGB keycodes * system76_ec: Add mode to disable layer backlight * launch_1: Use `KC_NO` instead of `KC_TRNS` for default layout * Revert "launch_1: Use `KC_NO` instead of `KC_TRNS` for default layout" This reverts commit f71c5e7ac3cecbbb1a1f8934db1f329407fef041. * Fix building bootloader * Workaround for upstream orientation * Custom USB IDs for USB hubs, disable USB hub feature controller * Set USB mux orientation in a loop for one second * Set mux orientation 100 times with 10 ms delay * Update Jeremy's keymap * Update Levi's Launch keymap * Update flashing instructions and rewrite layout design instructions * Update README.md * Add a system76_ec command to disable input events For testing purposes. * Enable system76/launch_1 keyboard to work with QMK Firmware 0.15.3 - Migrate system76/launch_1 from 0.7.103: - Explicitly enable used RGB matrix effects - Initialize flags field of `rgb_config_t` union/struct - Account for header and source file location changes - Update AVR platform makefile with Atmel DFU bootloader option - Update ATmega32U4 bootloader to latest from Microchip - Format C sources with ClangFormat - Format Markdown text with Prettier * Remove System76 pre-release or test keyboards and keymaps * Add licensing and replace guards in headers for system76/launch_1 * Remove options impact for system76/launch_1 * Revert AVR platform changes for `atmel-dfu` bootloader * Update system76/launch_1 README * Add system76/launch_1 information JSON file * Replace `util/delay.h` timing abstractions in system76/launch_1 * Use I2C QMK abstractions in system76/launch_1 * Fully revert AVR platform changes for `atmel-dfu` bootloader * Move `layouts.sh` into `keyboards/system76` * Implement GitHub PR suggestions for system76/launch_1 * Make additional system76/launch_1 updates * Implement minor system76/launch_1 change requests * Add custom version of Bootmagic Lite and document fuse values for system76/launch_1 * Remove the RESET HID command from system76/launch_1 * Reorder `process_record_user` in system76/launch_1 * Add `post_rules.mk` to system76/launch_1 * Fix overlapping key in sytem76/launch_1
Diffstat (limited to 'keyboards/system76/launch_1/usb_mux.c')
-rw-r--r--keyboards/system76/launch_1/usb_mux.c478
1 files changed, 478 insertions, 0 deletions
diff --git a/keyboards/system76/launch_1/usb_mux.c b/keyboards/system76/launch_1/usb_mux.c
new file mode 100644
index 000000000..6cb04dcdd
--- /dev/null
+++ b/keyboards/system76/launch_1/usb_mux.c
@@ -0,0 +1,478 @@
1/*
2 * Copyright (C) 2021 System76
3 * Copyright (C) 2021 Jimmy Cassis <KernelOops@outlook.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#include "usb_mux.h"
20
21#include <stdbool.h>
22
23#include "i2c_master.h"
24#include "wait.h"
25
26#define REG_PF1_CTL 0xBF800C04
27#define REG_PIO64_OEN 0xBF800908
28#define REG_PIO64_OUT 0xBF800928
29#define REG_VID 0xBF803000
30#define REG_PRT_SWAP 0xBF8030FA
31#define REG_USB3_HUB_VID 0xBFD2E548
32#define REG_RUNTIME_FLAGS2 0xBFD23408
33#define REG_I2S_FEAT_SEL 0xBFD23412
34
35struct USB7206 {
36 uint8_t addr;
37};
38
39struct USB7206 usb_hub = {.addr = 0x2D};
40
41// Perform USB7206 register access.
42// Returns zero on success or a negative number on error.
43i2c_status_t usb7206_register_access(struct USB7206* self) {
44 uint8_t register_access[3] = {
45 0x99,
46 0x37,
47 0x00,
48 };
49
50 return i2c_transmit(self->addr << 1, register_access, sizeof(register_access), I2C_TIMEOUT);
51}
52
53// Read data from USB7206 register region.
54// Returns number of bytes read on success or a negative number on error.
55i2c_status_t usb7206_read_reg(struct USB7206* self, uint32_t addr, uint8_t* data, int length) {
56 i2c_status_t status;
57
58 uint8_t register_read[9] = {
59 0x00, // Buffer address MSB: always 0
60 0x00, // Buffer address LSB: always 0
61 0x06, // Number of bytes to write to command block buffer area
62 0x01, // Direction: 0 = write, 1 = read
63 (uint8_t)length, // Number of bytes to read from register
64 (uint8_t)(addr >> 24), // Register address byte 3
65 (uint8_t)(addr >> 16), // Register address byte 2
66 (uint8_t)(addr >> 8), // Register address byte 1
67 (uint8_t)(addr >> 0), // Register address byte 0
68 };
69
70 status = i2c_transmit(self->addr << 1, register_read, sizeof(register_read), I2C_TIMEOUT);
71 if (status < 0) {
72 return status;
73 }
74
75 status = usb7206_register_access(self);
76 if (status < 0) {
77 return status;
78 }
79
80 uint8_t read[2] = {
81 0x00, // Buffer address MSB: always 0
82 0x06, // Buffer address LSB: 6 to skip header
83 };
84
85 status = i2c_start((self->addr << 1) | I2C_WRITE, I2C_TIMEOUT);
86 if (status >= 0) {
87 for (uint16_t i = 0; i < sizeof(read); i++) {
88 status = i2c_write(read[i], I2C_TIMEOUT);
89 if (status < 0) {
90 goto error;
91 }
92 }
93 } else {
94 goto error;
95 }
96
97 status = i2c_start((self->addr << 1) | I2C_READ, I2C_TIMEOUT);
98 if (status < 0) {
99 goto error;
100 }
101
102 // Read and ignore buffer length
103 status = i2c_read_ack(I2C_TIMEOUT);
104 if (status < 0) {
105 goto error;
106 }
107
108 for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
109 status = i2c_read_ack(I2C_TIMEOUT);
110 if (status >= 0) {
111 data[i] = (uint8_t)status;
112 }
113 }
114
115 if (status >= 0) {
116 status = i2c_read_nack(I2C_TIMEOUT);
117 if (status >= 0) {
118 data[(length - 1)] = (uint8_t)status;
119 }
120 }
121
122error:
123 i2c_stop();
124
125 return (status < 0) ? status : length;
126}
127
128// Read 32-bit value from USB7206 register region.
129// Returns number of bytes read on success or a negative number on error.
130i2c_status_t usb7206_read_reg_32(struct USB7206* self, uint32_t addr, uint32_t* data) {
131 i2c_status_t status;
132
133 // First byte is available length
134 uint8_t bytes[4] = {0, 0, 0, 0};
135
136 status = usb7206_read_reg(self, addr, bytes, sizeof(bytes));
137 if (status < 0) {
138 return status;
139 }
140
141 // Convert from little endian
142 *data = (((uint32_t)bytes[0]) << 0) | (((uint32_t)bytes[1]) << 8) | (((uint32_t)bytes[2]) << 16) | (((uint32_t)bytes[3]) << 24);
143
144 return status;
145}
146
147// Write data to USB7206 register region.
148// Returns number of bytes written on success or a negative number on error.
149i2c_status_t usb7206_write_reg(struct USB7206* self, uint32_t addr, uint8_t* data, int length) {
150 i2c_status_t status;
151
152 uint8_t register_write[9] = {
153 0x00, // Buffer address MSB: always 0
154 0x00, // Buffer address LSB: always 0
155 ((uint8_t)length) + 6, // Number of bytes to write to command block buffer area
156 0x00, // Direction: 0 = write, 1 = read
157 (uint8_t)length, // Number of bytes to write to register
158 (uint8_t)(addr >> 24), // Register address byte 3
159 (uint8_t)(addr >> 16), // Register address byte 2
160 (uint8_t)(addr >> 8), // Register address byte 1
161 (uint8_t)(addr >> 0), // Register address byte 0
162 };
163
164 status = i2c_start((self->addr << 1) | I2C_WRITE, I2C_TIMEOUT);
165 if (status >= 0) {
166 for (uint16_t i = 0; i < sizeof(register_write); i++) {
167 status = i2c_write(register_write[i], I2C_TIMEOUT);
168 if (status < 0) {
169 goto error;
170 }
171 }
172
173 for (uint16_t i = 0; i < length; i++) {
174 status = i2c_write(data[i], I2C_TIMEOUT);
175 if (status < 0) {
176 goto error;
177 }
178 }
179 } else {
180 goto error;
181 }
182
183 i2c_stop();
184
185 status = usb7206_register_access(self);
186 if (status < 0) {
187 goto error;
188 }
189
190error:
191 i2c_stop();
192
193 return (status < 0) ? status : length;
194}
195
196// Write 8-bit value to USB7206 register region.
197// Returns number of bytes written on success or a negative number on error.
198i2c_status_t usb7206_write_reg_8(struct USB7206* self, uint32_t addr, uint8_t data) { return usb7206_write_reg(self, addr, &data, sizeof(data)); }
199
200// Write 32-bit value to USB7206 register region.
201// Returns number of bytes written on success or a negative number on error.
202i2c_status_t usb7206_write_reg_32(struct USB7206* self, uint32_t addr, uint32_t data) {
203 // Convert to little endian
204 uint8_t bytes[4] = {
205 (uint8_t)(data >> 0),
206 (uint8_t)(data >> 8),
207 (uint8_t)(data >> 16),
208 (uint8_t)(data >> 24),
209 };
210
211 return usb7206_write_reg(self, addr, bytes, sizeof(bytes));
212}
213
214// Initialize USB7206.
215// Returns zero on success or a negative number on error.
216int usb7206_init(struct USB7206* self) {
217 i2c_status_t status;
218 uint32_t data;
219
220 // DM and DP are swapped on ports 2 and 3
221 status = usb7206_write_reg_8(self, REG_PRT_SWAP, 0x0C);
222 if (status < 0) {
223 return status;
224 }
225
226 // Disable audio
227 status = usb7206_write_reg_8(self, REG_I2S_FEAT_SEL, 0);
228 if (status < 0) {
229 return status;
230 }
231
232 // Set HFC_DISABLE
233 data = 0;
234 status = usb7206_read_reg_32(self, REG_RUNTIME_FLAGS2, &data);
235 if (status < 0) {
236 return status;
237 }
238 data |= 1;
239 status = usb7206_write_reg_32(self, REG_RUNTIME_FLAGS2, data);
240 if (status < 0) {
241 return status;
242 }
243
244 // Set Vendor ID and Product ID of USB 2 hub
245 status = usb7206_write_reg_32(self, REG_VID, 0x00033384);
246 if (status < 0) {
247 return status;
248 }
249
250 // Set Vendor ID and Product ID of USB 3 hub
251 status = usb7206_write_reg_32(self, REG_USB3_HUB_VID, 0x00043384);
252 if (status < 0) {
253 return status;
254 }
255
256 return 0;
257}
258
259// Attach USB7206.
260// Returns bytes written on success or a negative number on error.
261i2c_status_t usb7206_attach(struct USB7206* self) {
262 uint8_t data[3] = {
263 0xAA,
264 0x56,
265 0x00,
266 };
267
268 return i2c_transmit(self->addr << 1, data, sizeof(data), I2C_TIMEOUT);
269}
270
271struct USB7206_GPIO {
272 struct USB7206* usb7206;
273 uint32_t pf;
274};
275
276struct USB7206_GPIO usb_gpio_sink = {.usb7206 = &usb_hub, .pf = 29}; // UP_SEL = PF29 = GPIO93
277struct USB7206_GPIO usb_gpio_source_left = {.usb7206 = &usb_hub, .pf = 10}; // CL_SEL = PF10 = GPIO74
278struct USB7206_GPIO usb_gpio_source_right = {.usb7206 = &usb_hub, .pf = 25}; // CR_SEL = PF25 = GPIO88
279
280// Set USB7206 GPIO to specified value.
281// Returns zero on success or negative number on error.
282i2c_status_t usb7206_gpio_set(struct USB7206_GPIO* self, bool value) {
283 i2c_status_t status;
284 uint32_t data;
285
286 data = 0;
287 status = usb7206_read_reg_32(self->usb7206, REG_PIO64_OUT, &data);
288 if (status < 0) {
289 return status;
290 }
291
292 if (value) {
293 data |= (((uint32_t)1) << self->pf);
294 } else {
295 data &= ~(((uint32_t)1) << self->pf);
296 }
297 status = usb7206_write_reg_32(self->usb7206, REG_PIO64_OUT, data);
298 if (status < 0) {
299 return status;
300 }
301
302 return 0;
303}
304
305// Initialize USB7206 GPIO.
306// Returns zero on success or a negative number on error.
307i2c_status_t usb7206_gpio_init(struct USB7206_GPIO* self) {
308 i2c_status_t status;
309 uint32_t data;
310
311 // Set programmable function to GPIO
312 status = usb7206_write_reg_8(self->usb7206, REG_PF1_CTL + (self->pf - 1), 0);
313 if (status < 0) {
314 return status;
315 }
316
317 // Set GPIO to false by default
318 usb7206_gpio_set(self, false);
319
320 // Set GPIO to output
321 data = 0;
322 status = usb7206_read_reg_32(self->usb7206, REG_PIO64_OEN, &data);
323 if (status < 0) {
324 return status;
325 }
326
327 data |= (((uint32_t)1) << self->pf);
328 status = usb7206_write_reg_32(self->usb7206, REG_PIO64_OEN, data);
329 if (status < 0) {
330 return status;
331 }
332
333 return 0;
334}
335
336struct PTN5110 {
337 uint8_t addr;
338 uint8_t cc;
339 struct USB7206_GPIO* gpio;
340};
341
342struct PTN5110 usb_sink = {.addr = 0x51, .gpio = &usb_gpio_sink};
343struct PTN5110 usb_source_left = {.addr = 0x52, .gpio = &usb_gpio_source_left};
344struct PTN5110 usb_source_right = {.addr = 0x50, .gpio = &usb_gpio_source_right};
345
346// Initialize PTN5110.
347// Returns zero on success or a negative number on error.
348i2c_status_t ptn5110_init(struct PTN5110* self) {
349 // Set last cc to invalid value, to force update
350 self->cc = 0xFF;
351 // Initialize GPIO
352 return usb7206_gpio_init(self->gpio);
353}
354
355// Read PTN5110 CC_STATUS.
356// Returns zero on success or a negative number on error.
357i2c_status_t ptn5110_get_cc_status(struct PTN5110* self, uint8_t* cc) { return i2c_readReg(self->addr << 1, 0x1D, cc, 1, I2C_TIMEOUT); }
358
359// Set PTN5110 SSMUX orientation.
360// Returns zero on success or a negative number on error.
361i2c_status_t ptn5110_set_ssmux(struct PTN5110* self, bool orientation) { return usb7206_gpio_set(self->gpio, orientation); }
362
363// Write PTN5110 COMMAND.
364// Returns zero on success or negative number on error.
365i2c_status_t ptn5110_command(struct PTN5110* self, uint8_t command) { return i2c_writeReg(self->addr << 1, 0x23, &command, 1, I2C_TIMEOUT); }
366
367// Set orientation of PTN5110 operating as a sink, call this once.
368// Returns zero on success or a negative number on error.
369i2c_status_t ptn5110_sink_set_orientation(struct PTN5110* self) {
370 i2c_status_t status;
371 uint8_t cc;
372
373 status = ptn5110_get_cc_status(self, &cc);
374 if (status < 0) {
375 return status;
376 }
377
378 if ((cc & 0x03) == 0) {
379 status = ptn5110_set_ssmux(self, false);
380 if (status < 0) {
381 return status;
382 }
383 } else {
384 status = ptn5110_set_ssmux(self, true);
385 if (status < 0) {
386 return status;
387 }
388 }
389
390 return 0;
391}
392
393// Update PTN5110 operating as a source, call this repeatedly.
394// Returns zero on success or a negative number on error.
395i2c_status_t ptn5110_source_update(struct PTN5110* self) {
396 i2c_status_t status;
397 uint8_t cc;
398
399 status = ptn5110_get_cc_status(self, &cc);
400 if (status < 0) {
401 return status;
402 }
403
404 if (cc != self->cc) {
405 // WARNING: Setting this here will disable retries
406 self->cc = cc;
407
408 bool connected = false;
409 bool orientation = false;
410 if ((cc & 0x03) == 2) {
411 connected = true;
412 orientation = true;
413 } else if (((cc >> 2) & 0x03) == 2) {
414 connected = true;
415 orientation = false;
416 }
417
418 if (connected) {
419 // Set SS mux orientation
420 status = ptn5110_set_ssmux(self, orientation);
421 if (status < 0) {
422 return status;
423 }
424
425 // Enable source Vbus command
426 status = ptn5110_command(self, 0b01110111);
427 if (status < 0) {
428 return status;
429 }
430 } else {
431 // Disable source Vbus command
432 status = ptn5110_command(self, 0b01100110);
433 if (status < 0) {
434 return status;
435 }
436 }
437 }
438
439 return 0;
440}
441
442void usb_mux_event(void) {
443 // Run this on every 1000th matrix scan
444 static int cycle = 0;
445 if (cycle >= 1000) {
446 cycle = 0;
447 ptn5110_source_update(&usb_source_left);
448 ptn5110_source_update(&usb_source_right);
449 } else {
450 cycle += 1;
451 }
452}
453
454void usb_mux_init(void) {
455 // Run I2C bus at 100 kHz
456 i2c_init();
457
458 // Set up hub
459 usb7206_init(&usb_hub);
460
461 // Set up sink
462 ptn5110_init(&usb_sink);
463 ptn5110_sink_set_orientation(&usb_sink);
464
465 // Set up sources
466 ptn5110_init(&usb_source_left);
467 ptn5110_init(&usb_source_right);
468
469 // Attach hub
470 usb7206_attach(&usb_hub);
471
472 // Ensure orientation is correct after attaching hub
473 // TODO: Find reason why GPIO for sink orientation is reset
474 for (int i = 0; i < 100; i++) {
475 ptn5110_sink_set_orientation(&usb_sink);
476 wait_ms(10);
477 }
478}