1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved. 4 * Copyright (c) 2022, Linaro Ltd 5 */ 6 #include <linux/auxiliary_bus.h> 7 #include <linux/bitfield.h> 8 #include <linux/cleanup.h> 9 #include <linux/module.h> 10 #include <linux/of.h> 11 #include <linux/of_device.h> 12 #include <linux/mutex.h> 13 #include <linux/property.h> 14 #include <linux/soc/qcom/pdr.h> 15 #include <drm/bridge/aux-bridge.h> 16 17 #include <linux/usb/typec_altmode.h> 18 #include <linux/usb/typec_dp.h> 19 #include <linux/usb/typec_mux.h> 20 #include <linux/usb/typec_retimer.h> 21 22 #include <linux/soc/qcom/pmic_glink.h> 23 24 #define PMIC_GLINK_MAX_PORTS 3 25 26 #define USBC_SC8180X_NOTIFY_IND 0x13 27 #define USBC_CMD_WRITE_REQ 0x15 28 #define USBC_NOTIFY_IND 0x16 29 30 #define ALTMODE_PAN_EN 0x10 31 #define ALTMODE_PAN_ACK 0x11 32 33 struct usbc_write_req { 34 struct pmic_glink_hdr hdr; 35 __le32 cmd; 36 __le32 arg; 37 __le32 reserved; 38 }; 39 40 #define NOTIFY_PAYLOAD_SIZE 16 41 struct usbc_notify { 42 struct pmic_glink_hdr hdr; 43 char payload[NOTIFY_PAYLOAD_SIZE]; 44 u32 reserved; 45 }; 46 47 struct usbc_sc8180x_notify { 48 struct pmic_glink_hdr hdr; 49 __le32 notification; 50 __le32 reserved[2]; 51 }; 52 53 enum pmic_glink_altmode_pin_assignment { 54 DPAM_HPD_OUT, 55 DPAM_HPD_A, 56 DPAM_HPD_B, 57 DPAM_HPD_C, 58 DPAM_HPD_D, 59 DPAM_HPD_E, 60 DPAM_HPD_F, 61 }; 62 63 struct pmic_glink_altmode; 64 65 #define work_to_altmode_port(w) container_of((w), struct pmic_glink_altmode_port, work) 66 67 struct pmic_glink_altmode_port { 68 struct pmic_glink_altmode *altmode; 69 unsigned int index; 70 71 struct typec_switch *typec_switch; 72 struct typec_mux *typec_mux; 73 struct typec_mux_state state; 74 struct typec_retimer *typec_retimer; 75 struct typec_retimer_state retimer_state; 76 struct typec_altmode dp_alt; 77 78 struct work_struct work; 79 80 struct auxiliary_device *bridge; 81 82 enum typec_orientation orientation; 83 u16 svid; 84 u8 dp_data; 85 u8 mode; 86 u8 hpd_state; 87 u8 hpd_irq; 88 }; 89 90 #define work_to_altmode(w) container_of((w), struct pmic_glink_altmode, enable_work) 91 92 struct pmic_glink_altmode { 93 struct device *dev; 94 95 unsigned int owner_id; 96 97 /* To synchronize WRITE_REQ acks */ 98 struct mutex lock; 99 100 struct completion pan_ack; 101 struct pmic_glink_client *client; 102 103 struct work_struct enable_work; 104 105 struct pmic_glink_altmode_port ports[PMIC_GLINK_MAX_PORTS]; 106 }; 107 108 static int pmic_glink_altmode_request(struct pmic_glink_altmode *altmode, u32 cmd, u32 arg) 109 { 110 struct usbc_write_req req = {}; 111 unsigned long left; 112 int ret; 113 114 /* 115 * The USBC_CMD_WRITE_REQ ack doesn't identify the request, so wait for 116 * one ack at a time. 117 */ 118 guard(mutex)(&altmode->lock); 119 120 req.hdr.owner = cpu_to_le32(altmode->owner_id); 121 req.hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP); 122 req.hdr.opcode = cpu_to_le32(USBC_CMD_WRITE_REQ); 123 req.cmd = cpu_to_le32(cmd); 124 req.arg = cpu_to_le32(arg); 125 126 ret = pmic_glink_send(altmode->client, &req, sizeof(req)); 127 if (ret) { 128 dev_err(altmode->dev, "failed to send altmode request: %#x (%d)\n", cmd, ret); 129 return ret; 130 } 131 132 left = wait_for_completion_timeout(&altmode->pan_ack, 5 * HZ); 133 if (!left) { 134 dev_err(altmode->dev, "timeout waiting for altmode request ack for: %#x\n", cmd); 135 return -ETIMEDOUT; 136 } 137 138 return 0; 139 } 140 141 static void pmic_glink_altmode_enable_dp(struct pmic_glink_altmode *altmode, 142 struct pmic_glink_altmode_port *port, 143 u8 mode, bool hpd_state, 144 bool hpd_irq) 145 { 146 struct typec_displayport_data dp_data = {}; 147 int ret; 148 149 dp_data.status = DP_STATUS_ENABLED; 150 if (hpd_state) 151 dp_data.status |= DP_STATUS_HPD_STATE; 152 if (hpd_irq) 153 dp_data.status |= DP_STATUS_IRQ_HPD; 154 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(mode); 155 156 port->state.alt = &port->dp_alt; 157 port->state.data = &dp_data; 158 port->state.mode = TYPEC_MODAL_STATE(mode); 159 160 ret = typec_mux_set(port->typec_mux, &port->state); 161 if (ret) 162 dev_err(altmode->dev, "failed to switch mux to DP: %d\n", ret); 163 164 port->retimer_state.alt = &port->dp_alt; 165 port->retimer_state.data = &dp_data; 166 port->retimer_state.mode = TYPEC_MODAL_STATE(mode); 167 168 ret = typec_retimer_set(port->typec_retimer, &port->retimer_state); 169 if (ret) 170 dev_err(altmode->dev, "failed to setup retimer to DP: %d\n", ret); 171 } 172 173 static void pmic_glink_altmode_enable_usb(struct pmic_glink_altmode *altmode, 174 struct pmic_glink_altmode_port *port) 175 { 176 int ret; 177 178 port->state.alt = NULL; 179 port->state.data = NULL; 180 port->state.mode = TYPEC_STATE_USB; 181 182 ret = typec_mux_set(port->typec_mux, &port->state); 183 if (ret) 184 dev_err(altmode->dev, "failed to switch mux to USB: %d\n", ret); 185 186 port->retimer_state.alt = NULL; 187 port->retimer_state.data = NULL; 188 port->retimer_state.mode = TYPEC_STATE_USB; 189 190 ret = typec_retimer_set(port->typec_retimer, &port->retimer_state); 191 if (ret) 192 dev_err(altmode->dev, "failed to setup retimer to USB: %d\n", ret); 193 } 194 195 static void pmic_glink_altmode_safe(struct pmic_glink_altmode *altmode, 196 struct pmic_glink_altmode_port *port) 197 { 198 int ret; 199 200 port->state.alt = NULL; 201 port->state.data = NULL; 202 port->state.mode = TYPEC_STATE_SAFE; 203 204 ret = typec_mux_set(port->typec_mux, &port->state); 205 if (ret) 206 dev_err(altmode->dev, "failed to switch mux to safe mode: %d\n", ret); 207 208 port->retimer_state.alt = NULL; 209 port->retimer_state.data = NULL; 210 port->retimer_state.mode = TYPEC_STATE_SAFE; 211 212 ret = typec_retimer_set(port->typec_retimer, &port->retimer_state); 213 if (ret) 214 dev_err(altmode->dev, "failed to setup retimer to USB: %d\n", ret); 215 } 216 217 static void pmic_glink_altmode_worker(struct work_struct *work) 218 { 219 struct pmic_glink_altmode_port *alt_port = work_to_altmode_port(work); 220 struct pmic_glink_altmode *altmode = alt_port->altmode; 221 222 typec_switch_set(alt_port->typec_switch, alt_port->orientation); 223 224 if (alt_port->svid == USB_TYPEC_DP_SID && alt_port->mode == 0xff) 225 pmic_glink_altmode_safe(altmode, alt_port); 226 else if (alt_port->svid == USB_TYPEC_DP_SID) 227 pmic_glink_altmode_enable_dp(altmode, alt_port, alt_port->mode, 228 alt_port->hpd_state, alt_port->hpd_irq); 229 else 230 pmic_glink_altmode_enable_usb(altmode, alt_port); 231 232 drm_aux_hpd_bridge_notify(&alt_port->bridge->dev, 233 alt_port->hpd_state ? 234 connector_status_connected : 235 connector_status_disconnected); 236 237 pmic_glink_altmode_request(altmode, ALTMODE_PAN_ACK, alt_port->index); 238 } 239 240 static enum typec_orientation pmic_glink_altmode_orientation(unsigned int orientation) 241 { 242 if (orientation == 0) 243 return TYPEC_ORIENTATION_NORMAL; 244 else if (orientation == 1) 245 return TYPEC_ORIENTATION_REVERSE; 246 else 247 return TYPEC_ORIENTATION_NONE; 248 } 249 250 #define SC8180X_PORT_MASK 0x000000ff 251 #define SC8180X_ORIENTATION_MASK 0x0000ff00 252 #define SC8180X_MUX_MASK 0x00ff0000 253 #define SC8180X_MODE_MASK 0x3f000000 254 #define SC8180X_HPD_STATE_MASK 0x40000000 255 #define SC8180X_HPD_IRQ_MASK 0x80000000 256 257 static void pmic_glink_altmode_sc8180xp_notify(struct pmic_glink_altmode *altmode, 258 const void *data, size_t len) 259 { 260 struct pmic_glink_altmode_port *alt_port; 261 const struct usbc_sc8180x_notify *msg; 262 u32 notification; 263 u8 orientation; 264 u8 hpd_state; 265 u8 hpd_irq; 266 u16 svid; 267 u8 port; 268 u8 mode; 269 u8 mux; 270 271 if (len != sizeof(*msg)) { 272 dev_warn(altmode->dev, "invalid length of USBC_NOTIFY indication: %zd\n", len); 273 return; 274 } 275 276 msg = data; 277 notification = le32_to_cpu(msg->notification); 278 port = FIELD_GET(SC8180X_PORT_MASK, notification); 279 orientation = FIELD_GET(SC8180X_ORIENTATION_MASK, notification); 280 mux = FIELD_GET(SC8180X_MUX_MASK, notification); 281 mode = FIELD_GET(SC8180X_MODE_MASK, notification); 282 hpd_state = FIELD_GET(SC8180X_HPD_STATE_MASK, notification); 283 hpd_irq = FIELD_GET(SC8180X_HPD_IRQ_MASK, notification); 284 285 svid = mux == 2 ? USB_TYPEC_DP_SID : 0; 286 287 if (port >= ARRAY_SIZE(altmode->ports) || !altmode->ports[port].altmode) { 288 dev_dbg(altmode->dev, "notification on undefined port %d\n", port); 289 return; 290 } 291 292 alt_port = &altmode->ports[port]; 293 alt_port->orientation = pmic_glink_altmode_orientation(orientation); 294 alt_port->svid = svid; 295 alt_port->mode = mode; 296 alt_port->hpd_state = hpd_state; 297 alt_port->hpd_irq = hpd_irq; 298 schedule_work(&alt_port->work); 299 } 300 301 #define SC8280XP_DPAM_MASK 0x3f 302 #define SC8280XP_HPD_STATE_MASK BIT(6) 303 #define SC8280XP_HPD_IRQ_MASK BIT(7) 304 305 static void pmic_glink_altmode_sc8280xp_notify(struct pmic_glink_altmode *altmode, 306 u16 svid, const void *data, size_t len) 307 { 308 struct pmic_glink_altmode_port *alt_port; 309 const struct usbc_notify *notify; 310 u8 orientation; 311 u8 hpd_state; 312 u8 hpd_irq; 313 u8 mode; 314 u8 port; 315 316 if (len != sizeof(*notify)) { 317 dev_warn(altmode->dev, "invalid length USBC_NOTIFY_IND: %zd\n", 318 len); 319 return; 320 } 321 322 notify = data; 323 324 port = notify->payload[0]; 325 orientation = notify->payload[1]; 326 mode = FIELD_GET(SC8280XP_DPAM_MASK, notify->payload[8]) - DPAM_HPD_A; 327 hpd_state = FIELD_GET(SC8280XP_HPD_STATE_MASK, notify->payload[8]); 328 hpd_irq = FIELD_GET(SC8280XP_HPD_IRQ_MASK, notify->payload[8]); 329 330 if (port >= ARRAY_SIZE(altmode->ports) || !altmode->ports[port].altmode) { 331 dev_dbg(altmode->dev, "notification on undefined port %d\n", port); 332 return; 333 } 334 335 alt_port = &altmode->ports[port]; 336 alt_port->orientation = pmic_glink_altmode_orientation(orientation); 337 alt_port->svid = svid; 338 alt_port->mode = mode; 339 alt_port->hpd_state = hpd_state; 340 alt_port->hpd_irq = hpd_irq; 341 schedule_work(&alt_port->work); 342 } 343 344 static void pmic_glink_altmode_callback(const void *data, size_t len, void *priv) 345 { 346 struct pmic_glink_altmode *altmode = priv; 347 const struct pmic_glink_hdr *hdr = data; 348 u16 opcode; 349 u16 svid; 350 351 opcode = le32_to_cpu(hdr->opcode) & 0xff; 352 svid = le32_to_cpu(hdr->opcode) >> 16; 353 354 switch (opcode) { 355 case USBC_CMD_WRITE_REQ: 356 complete(&altmode->pan_ack); 357 break; 358 case USBC_NOTIFY_IND: 359 pmic_glink_altmode_sc8280xp_notify(altmode, svid, data, len); 360 break; 361 case USBC_SC8180X_NOTIFY_IND: 362 pmic_glink_altmode_sc8180xp_notify(altmode, data, len); 363 break; 364 } 365 } 366 367 static void pmic_glink_altmode_put_retimer(void *data) 368 { 369 typec_retimer_put(data); 370 } 371 372 static void pmic_glink_altmode_put_mux(void *data) 373 { 374 typec_mux_put(data); 375 } 376 377 static void pmic_glink_altmode_put_switch(void *data) 378 { 379 typec_switch_put(data); 380 } 381 382 static void pmic_glink_altmode_enable_worker(struct work_struct *work) 383 { 384 struct pmic_glink_altmode *altmode = work_to_altmode(work); 385 int ret; 386 387 ret = pmic_glink_altmode_request(altmode, ALTMODE_PAN_EN, 0); 388 if (ret) 389 dev_err(altmode->dev, "failed to request altmode notifications: %d\n", ret); 390 } 391 392 static void pmic_glink_altmode_pdr_notify(void *priv, int state) 393 { 394 struct pmic_glink_altmode *altmode = priv; 395 396 if (state == SERVREG_SERVICE_STATE_UP) 397 schedule_work(&altmode->enable_work); 398 } 399 400 static const struct of_device_id pmic_glink_altmode_of_quirks[] = { 401 { .compatible = "qcom,sc8180x-pmic-glink", .data = (void *)PMIC_GLINK_OWNER_USBC }, 402 {} 403 }; 404 405 static int pmic_glink_altmode_probe(struct auxiliary_device *adev, 406 const struct auxiliary_device_id *id) 407 { 408 struct pmic_glink_altmode_port *alt_port; 409 struct pmic_glink_altmode *altmode; 410 const struct of_device_id *match; 411 struct fwnode_handle *fwnode; 412 struct device *dev = &adev->dev; 413 u32 port; 414 int ret; 415 416 altmode = devm_kzalloc(dev, sizeof(*altmode), GFP_KERNEL); 417 if (!altmode) 418 return -ENOMEM; 419 420 altmode->dev = dev; 421 422 match = of_match_device(pmic_glink_altmode_of_quirks, dev->parent); 423 if (match) 424 altmode->owner_id = (unsigned long)match->data; 425 else 426 altmode->owner_id = PMIC_GLINK_OWNER_USBC_PAN; 427 428 INIT_WORK(&altmode->enable_work, pmic_glink_altmode_enable_worker); 429 init_completion(&altmode->pan_ack); 430 mutex_init(&altmode->lock); 431 432 device_for_each_child_node(dev, fwnode) { 433 ret = fwnode_property_read_u32(fwnode, "reg", &port); 434 if (ret < 0) { 435 dev_err(dev, "missing reg property of %pOFn\n", fwnode); 436 fwnode_handle_put(fwnode); 437 return ret; 438 } 439 440 if (port >= ARRAY_SIZE(altmode->ports)) { 441 dev_warn(dev, "invalid connector number, ignoring\n"); 442 continue; 443 } 444 445 if (altmode->ports[port].altmode) { 446 dev_err(dev, "multiple connector definition for port %u\n", port); 447 fwnode_handle_put(fwnode); 448 return -EINVAL; 449 } 450 451 alt_port = &altmode->ports[port]; 452 alt_port->altmode = altmode; 453 alt_port->index = port; 454 INIT_WORK(&alt_port->work, pmic_glink_altmode_worker); 455 456 alt_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode)); 457 if (IS_ERR(alt_port->bridge)) { 458 fwnode_handle_put(fwnode); 459 return PTR_ERR(alt_port->bridge); 460 } 461 462 alt_port->dp_alt.svid = USB_TYPEC_DP_SID; 463 alt_port->dp_alt.mode = USB_TYPEC_DP_MODE; 464 alt_port->dp_alt.active = 1; 465 466 alt_port->typec_mux = fwnode_typec_mux_get(fwnode); 467 if (IS_ERR(alt_port->typec_mux)) { 468 fwnode_handle_put(fwnode); 469 return dev_err_probe(dev, PTR_ERR(alt_port->typec_mux), 470 "failed to acquire mode-switch for port: %d\n", 471 port); 472 } 473 474 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_mux, 475 alt_port->typec_mux); 476 if (ret) { 477 fwnode_handle_put(fwnode); 478 return ret; 479 } 480 481 alt_port->typec_retimer = fwnode_typec_retimer_get(fwnode); 482 if (IS_ERR(alt_port->typec_retimer)) { 483 fwnode_handle_put(fwnode); 484 return dev_err_probe(dev, PTR_ERR(alt_port->typec_retimer), 485 "failed to acquire retimer-switch for port: %d\n", 486 port); 487 } 488 489 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_retimer, 490 alt_port->typec_retimer); 491 if (ret) { 492 fwnode_handle_put(fwnode); 493 return ret; 494 } 495 496 alt_port->typec_switch = fwnode_typec_switch_get(fwnode); 497 if (IS_ERR(alt_port->typec_switch)) { 498 fwnode_handle_put(fwnode); 499 return dev_err_probe(dev, PTR_ERR(alt_port->typec_switch), 500 "failed to acquire orientation-switch for port: %d\n", 501 port); 502 } 503 504 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_switch, 505 alt_port->typec_switch); 506 if (ret) { 507 fwnode_handle_put(fwnode); 508 return ret; 509 } 510 } 511 512 for (port = 0; port < ARRAY_SIZE(altmode->ports); port++) { 513 alt_port = &altmode->ports[port]; 514 if (!alt_port->bridge) 515 continue; 516 517 ret = devm_drm_dp_hpd_bridge_add(dev, alt_port->bridge); 518 if (ret) 519 return ret; 520 } 521 522 altmode->client = devm_pmic_glink_client_alloc(dev, 523 altmode->owner_id, 524 pmic_glink_altmode_callback, 525 pmic_glink_altmode_pdr_notify, 526 altmode); 527 if (IS_ERR(altmode->client)) 528 return PTR_ERR(altmode->client); 529 530 pmic_glink_client_register(altmode->client); 531 532 return 0; 533 } 534 535 static const struct auxiliary_device_id pmic_glink_altmode_id_table[] = { 536 { .name = "pmic_glink.altmode", }, 537 {}, 538 }; 539 MODULE_DEVICE_TABLE(auxiliary, pmic_glink_altmode_id_table); 540 541 static struct auxiliary_driver pmic_glink_altmode_driver = { 542 .name = "pmic_glink_altmode", 543 .probe = pmic_glink_altmode_probe, 544 .id_table = pmic_glink_altmode_id_table, 545 }; 546 547 module_auxiliary_driver(pmic_glink_altmode_driver); 548 549 MODULE_DESCRIPTION("Qualcomm PMIC GLINK Altmode driver"); 550 MODULE_LICENSE("GPL"); 551