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 enum drm_connector_status conn_status; 222 223 typec_switch_set(alt_port->typec_switch, alt_port->orientation); 224 225 if (alt_port->svid == USB_TYPEC_DP_SID) { 226 if (alt_port->mode == 0xff) { 227 pmic_glink_altmode_safe(altmode, alt_port); 228 } else { 229 pmic_glink_altmode_enable_dp(altmode, alt_port, 230 alt_port->mode, 231 alt_port->hpd_state, 232 alt_port->hpd_irq); 233 } 234 235 if (alt_port->hpd_state) 236 conn_status = connector_status_connected; 237 else 238 conn_status = connector_status_disconnected; 239 240 drm_aux_hpd_bridge_notify(&alt_port->bridge->dev, conn_status); 241 } else { 242 pmic_glink_altmode_enable_usb(altmode, alt_port); 243 } 244 245 pmic_glink_altmode_request(altmode, ALTMODE_PAN_ACK, alt_port->index); 246 } 247 248 static enum typec_orientation pmic_glink_altmode_orientation(unsigned int orientation) 249 { 250 if (orientation == 0) 251 return TYPEC_ORIENTATION_NORMAL; 252 else if (orientation == 1) 253 return TYPEC_ORIENTATION_REVERSE; 254 else 255 return TYPEC_ORIENTATION_NONE; 256 } 257 258 #define SC8180X_PORT_MASK 0x000000ff 259 #define SC8180X_ORIENTATION_MASK 0x0000ff00 260 #define SC8180X_MUX_MASK 0x00ff0000 261 #define SC8180X_MODE_MASK 0x3f000000 262 #define SC8180X_HPD_STATE_MASK 0x40000000 263 #define SC8180X_HPD_IRQ_MASK 0x80000000 264 265 static void pmic_glink_altmode_sc8180xp_notify(struct pmic_glink_altmode *altmode, 266 const void *data, size_t len) 267 { 268 struct pmic_glink_altmode_port *alt_port; 269 const struct usbc_sc8180x_notify *msg; 270 u32 notification; 271 u8 orientation; 272 u8 hpd_state; 273 u8 hpd_irq; 274 u16 svid; 275 u8 port; 276 u8 mode; 277 u8 mux; 278 279 if (len != sizeof(*msg)) { 280 dev_warn(altmode->dev, "invalid length of USBC_NOTIFY indication: %zd\n", len); 281 return; 282 } 283 284 msg = data; 285 notification = le32_to_cpu(msg->notification); 286 port = FIELD_GET(SC8180X_PORT_MASK, notification); 287 orientation = FIELD_GET(SC8180X_ORIENTATION_MASK, notification); 288 mux = FIELD_GET(SC8180X_MUX_MASK, notification); 289 mode = FIELD_GET(SC8180X_MODE_MASK, notification); 290 hpd_state = FIELD_GET(SC8180X_HPD_STATE_MASK, notification); 291 hpd_irq = FIELD_GET(SC8180X_HPD_IRQ_MASK, notification); 292 293 svid = mux == 2 ? USB_TYPEC_DP_SID : 0; 294 295 if (port >= ARRAY_SIZE(altmode->ports) || !altmode->ports[port].altmode) { 296 dev_dbg(altmode->dev, "notification on undefined port %d\n", port); 297 return; 298 } 299 300 alt_port = &altmode->ports[port]; 301 alt_port->orientation = pmic_glink_altmode_orientation(orientation); 302 alt_port->svid = svid; 303 alt_port->mode = mode; 304 alt_port->hpd_state = hpd_state; 305 alt_port->hpd_irq = hpd_irq; 306 schedule_work(&alt_port->work); 307 } 308 309 #define SC8280XP_DPAM_MASK 0x3f 310 #define SC8280XP_HPD_STATE_MASK BIT(6) 311 #define SC8280XP_HPD_IRQ_MASK BIT(7) 312 313 static void pmic_glink_altmode_sc8280xp_notify(struct pmic_glink_altmode *altmode, 314 u16 svid, const void *data, size_t len) 315 { 316 struct pmic_glink_altmode_port *alt_port; 317 const struct usbc_notify *notify; 318 u8 orientation; 319 u8 hpd_state; 320 u8 hpd_irq; 321 u8 mode; 322 u8 port; 323 324 if (len != sizeof(*notify)) { 325 dev_warn(altmode->dev, "invalid length USBC_NOTIFY_IND: %zd\n", 326 len); 327 return; 328 } 329 330 notify = data; 331 332 port = notify->payload[0]; 333 orientation = notify->payload[1]; 334 mode = FIELD_GET(SC8280XP_DPAM_MASK, notify->payload[8]) - DPAM_HPD_A; 335 hpd_state = FIELD_GET(SC8280XP_HPD_STATE_MASK, notify->payload[8]); 336 hpd_irq = FIELD_GET(SC8280XP_HPD_IRQ_MASK, notify->payload[8]); 337 338 if (port >= ARRAY_SIZE(altmode->ports) || !altmode->ports[port].altmode) { 339 dev_dbg(altmode->dev, "notification on undefined port %d\n", port); 340 return; 341 } 342 343 alt_port = &altmode->ports[port]; 344 alt_port->orientation = pmic_glink_altmode_orientation(orientation); 345 alt_port->svid = svid; 346 alt_port->mode = mode; 347 alt_port->hpd_state = hpd_state; 348 alt_port->hpd_irq = hpd_irq; 349 schedule_work(&alt_port->work); 350 } 351 352 static void pmic_glink_altmode_callback(const void *data, size_t len, void *priv) 353 { 354 struct pmic_glink_altmode *altmode = priv; 355 const struct pmic_glink_hdr *hdr = data; 356 u16 opcode; 357 u16 svid; 358 359 opcode = le32_to_cpu(hdr->opcode) & 0xff; 360 svid = le32_to_cpu(hdr->opcode) >> 16; 361 362 switch (opcode) { 363 case USBC_CMD_WRITE_REQ: 364 complete(&altmode->pan_ack); 365 break; 366 case USBC_NOTIFY_IND: 367 pmic_glink_altmode_sc8280xp_notify(altmode, svid, data, len); 368 break; 369 case USBC_SC8180X_NOTIFY_IND: 370 pmic_glink_altmode_sc8180xp_notify(altmode, data, len); 371 break; 372 } 373 } 374 375 static void pmic_glink_altmode_put_retimer(void *data) 376 { 377 typec_retimer_put(data); 378 } 379 380 static void pmic_glink_altmode_put_mux(void *data) 381 { 382 typec_mux_put(data); 383 } 384 385 static void pmic_glink_altmode_put_switch(void *data) 386 { 387 typec_switch_put(data); 388 } 389 390 static void pmic_glink_altmode_enable_worker(struct work_struct *work) 391 { 392 struct pmic_glink_altmode *altmode = work_to_altmode(work); 393 int ret; 394 395 ret = pmic_glink_altmode_request(altmode, ALTMODE_PAN_EN, 0); 396 if (ret) 397 dev_err(altmode->dev, "failed to request altmode notifications: %d\n", ret); 398 } 399 400 static void pmic_glink_altmode_pdr_notify(void *priv, int state) 401 { 402 struct pmic_glink_altmode *altmode = priv; 403 404 if (state == SERVREG_SERVICE_STATE_UP) 405 schedule_work(&altmode->enable_work); 406 } 407 408 static const struct of_device_id pmic_glink_altmode_of_quirks[] = { 409 { .compatible = "qcom,sc8180x-pmic-glink", .data = (void *)PMIC_GLINK_OWNER_USBC }, 410 {} 411 }; 412 413 static int pmic_glink_altmode_probe(struct auxiliary_device *adev, 414 const struct auxiliary_device_id *id) 415 { 416 struct pmic_glink_altmode_port *alt_port; 417 struct pmic_glink_altmode *altmode; 418 const struct of_device_id *match; 419 struct fwnode_handle *fwnode; 420 struct device *dev = &adev->dev; 421 u32 port; 422 int ret; 423 424 altmode = devm_kzalloc(dev, sizeof(*altmode), GFP_KERNEL); 425 if (!altmode) 426 return -ENOMEM; 427 428 altmode->dev = dev; 429 430 match = of_match_device(pmic_glink_altmode_of_quirks, dev->parent); 431 if (match) 432 altmode->owner_id = (unsigned long)match->data; 433 else 434 altmode->owner_id = PMIC_GLINK_OWNER_USBC_PAN; 435 436 INIT_WORK(&altmode->enable_work, pmic_glink_altmode_enable_worker); 437 init_completion(&altmode->pan_ack); 438 mutex_init(&altmode->lock); 439 440 device_for_each_child_node(dev, fwnode) { 441 ret = fwnode_property_read_u32(fwnode, "reg", &port); 442 if (ret < 0) { 443 dev_err(dev, "missing reg property of %pOFn\n", fwnode); 444 fwnode_handle_put(fwnode); 445 return ret; 446 } 447 448 if (port >= ARRAY_SIZE(altmode->ports)) { 449 dev_warn(dev, "invalid connector number, ignoring\n"); 450 continue; 451 } 452 453 if (altmode->ports[port].altmode) { 454 dev_err(dev, "multiple connector definition for port %u\n", port); 455 fwnode_handle_put(fwnode); 456 return -EINVAL; 457 } 458 459 alt_port = &altmode->ports[port]; 460 alt_port->altmode = altmode; 461 alt_port->index = port; 462 INIT_WORK(&alt_port->work, pmic_glink_altmode_worker); 463 464 alt_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode)); 465 if (IS_ERR(alt_port->bridge)) { 466 fwnode_handle_put(fwnode); 467 return PTR_ERR(alt_port->bridge); 468 } 469 470 alt_port->dp_alt.svid = USB_TYPEC_DP_SID; 471 alt_port->dp_alt.mode = USB_TYPEC_DP_MODE; 472 alt_port->dp_alt.active = 1; 473 474 alt_port->typec_mux = fwnode_typec_mux_get(fwnode); 475 if (IS_ERR(alt_port->typec_mux)) { 476 fwnode_handle_put(fwnode); 477 return dev_err_probe(dev, PTR_ERR(alt_port->typec_mux), 478 "failed to acquire mode-switch for port: %d\n", 479 port); 480 } 481 482 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_mux, 483 alt_port->typec_mux); 484 if (ret) { 485 fwnode_handle_put(fwnode); 486 return ret; 487 } 488 489 alt_port->typec_retimer = fwnode_typec_retimer_get(fwnode); 490 if (IS_ERR(alt_port->typec_retimer)) { 491 fwnode_handle_put(fwnode); 492 return dev_err_probe(dev, PTR_ERR(alt_port->typec_retimer), 493 "failed to acquire retimer-switch for port: %d\n", 494 port); 495 } 496 497 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_retimer, 498 alt_port->typec_retimer); 499 if (ret) { 500 fwnode_handle_put(fwnode); 501 return ret; 502 } 503 504 alt_port->typec_switch = fwnode_typec_switch_get(fwnode); 505 if (IS_ERR(alt_port->typec_switch)) { 506 fwnode_handle_put(fwnode); 507 return dev_err_probe(dev, PTR_ERR(alt_port->typec_switch), 508 "failed to acquire orientation-switch for port: %d\n", 509 port); 510 } 511 512 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_switch, 513 alt_port->typec_switch); 514 if (ret) { 515 fwnode_handle_put(fwnode); 516 return ret; 517 } 518 } 519 520 for (port = 0; port < ARRAY_SIZE(altmode->ports); port++) { 521 alt_port = &altmode->ports[port]; 522 if (!alt_port->bridge) 523 continue; 524 525 ret = devm_drm_dp_hpd_bridge_add(dev, alt_port->bridge); 526 if (ret) 527 return ret; 528 } 529 530 altmode->client = devm_pmic_glink_client_alloc(dev, 531 altmode->owner_id, 532 pmic_glink_altmode_callback, 533 pmic_glink_altmode_pdr_notify, 534 altmode); 535 if (IS_ERR(altmode->client)) 536 return PTR_ERR(altmode->client); 537 538 pmic_glink_client_register(altmode->client); 539 540 return 0; 541 } 542 543 static const struct auxiliary_device_id pmic_glink_altmode_id_table[] = { 544 { .name = "pmic_glink.altmode", }, 545 {}, 546 }; 547 MODULE_DEVICE_TABLE(auxiliary, pmic_glink_altmode_id_table); 548 549 static struct auxiliary_driver pmic_glink_altmode_driver = { 550 .name = "pmic_glink_altmode", 551 .probe = pmic_glink_altmode_probe, 552 .id_table = pmic_glink_altmode_id_table, 553 }; 554 555 module_auxiliary_driver(pmic_glink_altmode_driver); 556 557 MODULE_DESCRIPTION("Qualcomm PMIC GLINK Altmode driver"); 558 MODULE_LICENSE("GPL"); 559