logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

usb_mux.c (13104B)


  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. #include "usb_mux.h"
  19. #include <stdbool.h>
  20. #include "i2c_master.h"
  21. #include "wait.h"
  22. #define REG_PF1_CTL 0xBF800C04
  23. #define REG_PIO64_OEN 0xBF800908
  24. #define REG_PIO64_OUT 0xBF800928
  25. #define REG_VID 0xBF803000
  26. #define REG_PRT_SWAP 0xBF8030FA
  27. #define REG_USB3_HUB_VID 0xBFD2E548
  28. #define REG_RUNTIME_FLAGS2 0xBFD23408
  29. #define REG_I2S_FEAT_SEL 0xBFD23412
  30. struct USB7206 {
  31. uint8_t addr;
  32. };
  33. struct USB7206 usb_hub = {.addr = 0x2D};
  34. // Perform USB7206 register access.
  35. // Returns zero on success or a negative number on error.
  36. i2c_status_t usb7206_register_access(struct USB7206* self) {
  37. uint8_t register_access[3] = {
  38. 0x99,
  39. 0x37,
  40. 0x00,
  41. };
  42. return i2c_transmit(self->addr << 1, register_access, sizeof(register_access), I2C_TIMEOUT);
  43. }
  44. // Read data from USB7206 register region.
  45. // Returns number of bytes read on success or a negative number on error.
  46. i2c_status_t usb7206_read_reg(struct USB7206* self, uint32_t addr, uint8_t* data, int length) {
  47. i2c_status_t status;
  48. uint8_t register_read[9] = {
  49. 0x00, // Buffer address MSB: always 0
  50. 0x00, // Buffer address LSB: always 0
  51. 0x06, // Number of bytes to write to command block buffer area
  52. 0x01, // Direction: 0 = write, 1 = read
  53. (uint8_t)length, // Number of bytes to read from register
  54. (uint8_t)(addr >> 24), // Register address byte 3
  55. (uint8_t)(addr >> 16), // Register address byte 2
  56. (uint8_t)(addr >> 8), // Register address byte 1
  57. (uint8_t)(addr >> 0), // Register address byte 0
  58. };
  59. status = i2c_transmit(self->addr << 1, register_read, sizeof(register_read), I2C_TIMEOUT);
  60. if (status < 0) {
  61. return status;
  62. }
  63. status = usb7206_register_access(self);
  64. if (status < 0) {
  65. return status;
  66. }
  67. uint16_t read = 0x0006; // Buffer address 6 to skip header
  68. uint8_t data_with_buffer_length[length];
  69. status = i2c_read_register16((self->addr << 1), read, data_with_buffer_length, length, I2C_TIMEOUT);
  70. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  71. data[i] = data_with_buffer_length[i+1];
  72. }
  73. return (status < 0) ? status : length;
  74. }
  75. // Read 32-bit value from USB7206 register region.
  76. // Returns number of bytes read on success or a negative number on error.
  77. i2c_status_t usb7206_read_reg_32(struct USB7206* self, uint32_t addr, uint32_t* data) {
  78. i2c_status_t status;
  79. // First byte is available length
  80. uint8_t bytes[4] = {0, 0, 0, 0};
  81. status = usb7206_read_reg(self, addr, bytes, sizeof(bytes));
  82. if (status < 0) {
  83. return status;
  84. }
  85. // Convert from little endian
  86. *data = (((uint32_t)bytes[0]) << 0) | (((uint32_t)bytes[1]) << 8) | (((uint32_t)bytes[2]) << 16) | (((uint32_t)bytes[3]) << 24);
  87. return status;
  88. }
  89. // Write data to USB7206 register region.
  90. // Returns number of bytes written on success or a negative number on error.
  91. i2c_status_t usb7206_write_reg(struct USB7206* self, uint32_t addr, uint8_t* data, int length) {
  92. i2c_status_t status;
  93. uint8_t register_write[9] = {
  94. 0x00, // Buffer address MSB: always 0
  95. 0x00, // Buffer address LSB: always 0
  96. ((uint8_t)length) + 6, // Number of bytes to write to command block buffer area
  97. 0x00, // Direction: 0 = write, 1 = read
  98. (uint8_t)length, // Number of bytes to write to register
  99. (uint8_t)(addr >> 24), // Register address byte 3
  100. (uint8_t)(addr >> 16), // Register address byte 2
  101. (uint8_t)(addr >> 8), // Register address byte 1
  102. (uint8_t)(addr >> 0), // Register address byte 0
  103. };
  104. uint8_t send_buffer_length = sizeof(register_write) + length;
  105. uint8_t send_buffer[send_buffer_length];
  106. uint8_t j = 0;
  107. for (uint16_t i = 0; i < sizeof(register_write); i++) {
  108. send_buffer[j++] = register_write[i];
  109. }
  110. for (uint16_t i = 0; i < length; i++) {
  111. send_buffer[j++] = data[i];
  112. }
  113. status = i2c_transmit((self->addr << 1), send_buffer, send_buffer_length, I2C_TIMEOUT);
  114. status = usb7206_register_access(self);
  115. return (status < 0) ? status : length;
  116. }
  117. // Write 8-bit value to USB7206 register region.
  118. // Returns number of bytes written on success or a negative number on error.
  119. 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)); }
  120. // Write 32-bit value to USB7206 register region.
  121. // Returns number of bytes written on success or a negative number on error.
  122. i2c_status_t usb7206_write_reg_32(struct USB7206* self, uint32_t addr, uint32_t data) {
  123. // Convert to little endian
  124. uint8_t bytes[4] = {
  125. (uint8_t)(data >> 0),
  126. (uint8_t)(data >> 8),
  127. (uint8_t)(data >> 16),
  128. (uint8_t)(data >> 24),
  129. };
  130. return usb7206_write_reg(self, addr, bytes, sizeof(bytes));
  131. }
  132. // Initialize USB7206.
  133. // Returns zero on success or a negative number on error.
  134. int usb7206_init(struct USB7206* self) {
  135. i2c_status_t status;
  136. uint32_t data;
  137. // DM and DP are swapped on ports 2 and 3
  138. status = usb7206_write_reg_8(self, REG_PRT_SWAP, 0x0C);
  139. if (status < 0) {
  140. return status;
  141. }
  142. // Disable audio
  143. status = usb7206_write_reg_8(self, REG_I2S_FEAT_SEL, 0);
  144. if (status < 0) {
  145. return status;
  146. }
  147. // Set HFC_DISABLE
  148. data = 0;
  149. status = usb7206_read_reg_32(self, REG_RUNTIME_FLAGS2, &data);
  150. if (status < 0) {
  151. return status;
  152. }
  153. data |= 1;
  154. status = usb7206_write_reg_32(self, REG_RUNTIME_FLAGS2, data);
  155. if (status < 0) {
  156. return status;
  157. }
  158. // Set Vendor ID and Product ID of USB 2 hub
  159. status = usb7206_write_reg_32(self, REG_VID, 0x00033384);
  160. if (status < 0) {
  161. return status;
  162. }
  163. // Set Vendor ID and Product ID of USB 3 hub
  164. status = usb7206_write_reg_32(self, REG_USB3_HUB_VID, 0x00043384);
  165. if (status < 0) {
  166. return status;
  167. }
  168. return 0;
  169. }
  170. // Attach USB7206.
  171. // Returns bytes written on success or a negative number on error.
  172. i2c_status_t usb7206_attach(struct USB7206* self) {
  173. uint8_t data[3] = {
  174. 0xAA,
  175. 0x56,
  176. 0x00,
  177. };
  178. return i2c_transmit(self->addr << 1, data, sizeof(data), I2C_TIMEOUT);
  179. }
  180. struct USB7206_GPIO {
  181. struct USB7206* usb7206;
  182. uint32_t pf;
  183. };
  184. struct USB7206_GPIO usb_gpio_sink = {.usb7206 = &usb_hub, .pf = 29}; // UP_SEL = PF29 = GPIO93
  185. struct USB7206_GPIO usb_gpio_source_left = {.usb7206 = &usb_hub, .pf = 10}; // CL_SEL = PF10 = GPIO74
  186. struct USB7206_GPIO usb_gpio_source_right = {.usb7206 = &usb_hub, .pf = 25}; // CR_SEL = PF25 = GPIO88
  187. // Set USB7206 GPIO to specified value.
  188. // Returns zero on success or negative number on error.
  189. i2c_status_t usb7206_gpio_set(struct USB7206_GPIO* self, bool value) {
  190. i2c_status_t status;
  191. uint32_t data;
  192. data = 0;
  193. status = usb7206_read_reg_32(self->usb7206, REG_PIO64_OUT, &data);
  194. if (status < 0) {
  195. return status;
  196. }
  197. if (value) {
  198. data |= (((uint32_t)1) << self->pf);
  199. } else {
  200. data &= ~(((uint32_t)1) << self->pf);
  201. }
  202. status = usb7206_write_reg_32(self->usb7206, REG_PIO64_OUT, data);
  203. if (status < 0) {
  204. return status;
  205. }
  206. return 0;
  207. }
  208. // Initialize USB7206 GPIO.
  209. // Returns zero on success or a negative number on error.
  210. i2c_status_t usb7206_gpio_init(struct USB7206_GPIO* self) {
  211. i2c_status_t status;
  212. uint32_t data;
  213. // Set programmable function to GPIO
  214. status = usb7206_write_reg_8(self->usb7206, REG_PF1_CTL + (self->pf - 1), 0);
  215. if (status < 0) {
  216. return status;
  217. }
  218. // Set GPIO to false by default
  219. usb7206_gpio_set(self, false);
  220. // Set GPIO to output
  221. data = 0;
  222. status = usb7206_read_reg_32(self->usb7206, REG_PIO64_OEN, &data);
  223. if (status < 0) {
  224. return status;
  225. }
  226. data |= (((uint32_t)1) << self->pf);
  227. status = usb7206_write_reg_32(self->usb7206, REG_PIO64_OEN, data);
  228. if (status < 0) {
  229. return status;
  230. }
  231. return 0;
  232. }
  233. struct PTN5110 {
  234. uint8_t addr;
  235. uint8_t cc;
  236. struct USB7206_GPIO* gpio;
  237. };
  238. struct PTN5110 usb_sink = {.addr = 0x51, .gpio = &usb_gpio_sink};
  239. struct PTN5110 usb_source_left = {.addr = 0x52, .gpio = &usb_gpio_source_left};
  240. struct PTN5110 usb_source_right = {.addr = 0x50, .gpio = &usb_gpio_source_right};
  241. // Initialize PTN5110.
  242. // Returns zero on success or a negative number on error.
  243. i2c_status_t ptn5110_init(struct PTN5110* self) {
  244. // Set last cc to invalid value, to force update
  245. self->cc = 0xFF;
  246. // Initialize GPIO
  247. return usb7206_gpio_init(self->gpio);
  248. }
  249. // Read PTN5110 CC_STATUS.
  250. // Returns zero on success or a negative number on error.
  251. i2c_status_t ptn5110_get_cc_status(struct PTN5110* self, uint8_t* cc) { return i2c_read_register(self->addr << 1, 0x1D, cc, 1, I2C_TIMEOUT); }
  252. // Set PTN5110 SSMUX orientation.
  253. // Returns zero on success or a negative number on error.
  254. i2c_status_t ptn5110_set_ssmux(struct PTN5110* self, bool orientation) { return usb7206_gpio_set(self->gpio, orientation); }
  255. // Write PTN5110 COMMAND.
  256. // Returns zero on success or negative number on error.
  257. i2c_status_t ptn5110_command(struct PTN5110* self, uint8_t command) { return i2c_write_register(self->addr << 1, 0x23, &command, 1, I2C_TIMEOUT); }
  258. // Set orientation of PTN5110 operating as a sink, call this once.
  259. // Returns zero on success or a negative number on error.
  260. i2c_status_t ptn5110_sink_set_orientation(struct PTN5110* self) {
  261. i2c_status_t status;
  262. uint8_t cc;
  263. status = ptn5110_get_cc_status(self, &cc);
  264. if (status < 0) {
  265. return status;
  266. }
  267. if ((cc & 0x03) == 0) {
  268. status = ptn5110_set_ssmux(self, false);
  269. if (status < 0) {
  270. return status;
  271. }
  272. } else {
  273. status = ptn5110_set_ssmux(self, true);
  274. if (status < 0) {
  275. return status;
  276. }
  277. }
  278. return 0;
  279. }
  280. // Update PTN5110 operating as a source, call this repeatedly.
  281. // Returns zero on success or a negative number on error.
  282. i2c_status_t ptn5110_source_update(struct PTN5110* self) {
  283. i2c_status_t status;
  284. uint8_t cc;
  285. status = ptn5110_get_cc_status(self, &cc);
  286. if (status < 0) {
  287. return status;
  288. }
  289. if (cc != self->cc) {
  290. // WARNING: Setting this here will disable retries
  291. self->cc = cc;
  292. bool connected = false;
  293. bool orientation = false;
  294. if ((cc & 0x03) == 2) {
  295. connected = true;
  296. orientation = true;
  297. } else if (((cc >> 2) & 0x03) == 2) {
  298. connected = true;
  299. orientation = false;
  300. }
  301. if (connected) {
  302. // Set SS mux orientation
  303. status = ptn5110_set_ssmux(self, orientation);
  304. if (status < 0) {
  305. return status;
  306. }
  307. // Enable source Vbus command
  308. status = ptn5110_command(self, 0b01110111);
  309. if (status < 0) {
  310. return status;
  311. }
  312. } else {
  313. // Disable source Vbus command
  314. status = ptn5110_command(self, 0b01100110);
  315. if (status < 0) {
  316. return status;
  317. }
  318. }
  319. }
  320. return 0;
  321. }
  322. void usb_mux_event(void) {
  323. // Run this on every 1000th matrix scan
  324. static int cycle = 0;
  325. if (cycle >= 1000) {
  326. cycle = 0;
  327. ptn5110_source_update(&usb_source_left);
  328. ptn5110_source_update(&usb_source_right);
  329. } else {
  330. cycle += 1;
  331. }
  332. }
  333. void usb_mux_init(void) {
  334. // Run I2C bus at 100 kHz
  335. i2c_init();
  336. // Set up hub
  337. usb7206_init(&usb_hub);
  338. // Set up sink
  339. ptn5110_init(&usb_sink);
  340. ptn5110_sink_set_orientation(&usb_sink);
  341. // Set up sources
  342. ptn5110_init(&usb_source_left);
  343. ptn5110_init(&usb_source_right);
  344. // Attach hub
  345. usb7206_attach(&usb_hub);
  346. // Ensure orientation is correct after attaching hub
  347. // TODO: Find reason why GPIO for sink orientation is reset
  348. for (int i = 0; i < 100; i++) {
  349. ptn5110_sink_set_orientation(&usb_sink);
  350. wait_ms(10);
  351. }
  352. }