diff options
Diffstat (limited to 'keyboards/system76/launch_1/usb_mux.c')
| -rw-r--r-- | keyboards/system76/launch_1/usb_mux.c | 478 |
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 | |||
| 35 | struct USB7206 { | ||
| 36 | uint8_t addr; | ||
| 37 | }; | ||
| 38 | |||
| 39 | struct USB7206 usb_hub = {.addr = 0x2D}; | ||
| 40 | |||
| 41 | // Perform USB7206 register access. | ||
| 42 | // Returns zero on success or a negative number on error. | ||
| 43 | i2c_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. | ||
| 55 | i2c_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 | |||
| 122 | error: | ||
| 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. | ||
| 130 | i2c_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. | ||
| 149 | i2c_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 | |||
| 190 | error: | ||
| 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. | ||
| 198 | i2c_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. | ||
| 202 | i2c_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. | ||
| 216 | int 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. | ||
| 261 | i2c_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 | |||
| 271 | struct USB7206_GPIO { | ||
| 272 | struct USB7206* usb7206; | ||
| 273 | uint32_t pf; | ||
| 274 | }; | ||
| 275 | |||
| 276 | struct USB7206_GPIO usb_gpio_sink = {.usb7206 = &usb_hub, .pf = 29}; // UP_SEL = PF29 = GPIO93 | ||
| 277 | struct USB7206_GPIO usb_gpio_source_left = {.usb7206 = &usb_hub, .pf = 10}; // CL_SEL = PF10 = GPIO74 | ||
| 278 | struct 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. | ||
| 282 | i2c_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. | ||
| 307 | i2c_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 | |||
| 336 | struct PTN5110 { | ||
| 337 | uint8_t addr; | ||
| 338 | uint8_t cc; | ||
| 339 | struct USB7206_GPIO* gpio; | ||
| 340 | }; | ||
| 341 | |||
| 342 | struct PTN5110 usb_sink = {.addr = 0x51, .gpio = &usb_gpio_sink}; | ||
| 343 | struct PTN5110 usb_source_left = {.addr = 0x52, .gpio = &usb_gpio_source_left}; | ||
| 344 | struct 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. | ||
| 348 | i2c_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. | ||
| 357 | i2c_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. | ||
| 361 | i2c_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. | ||
| 365 | i2c_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. | ||
| 369 | i2c_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. | ||
| 395 | i2c_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 | |||
| 442 | void 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 | |||
| 454 | void 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 | } | ||
