1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2022, Linaro Ltd
6 #include <linux/auxiliary_bus.h>
7 #include <linux/bitfield.h>
8 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/mutex.h>
12 #include <linux/property.h>
13 #include <linux/soc/qcom/pdr.h>
14 #include <drm/bridge/aux-bridge.h>
16 #include <linux/usb/typec_altmode.h>
17 #include <linux/usb/typec_dp.h>
18 #include <linux/usb/typec_mux.h>
19 #include <linux/usb/typec_retimer.h>
21 #include <linux/soc/qcom/pmic_glink.h>
23 #define PMIC_GLINK_MAX_PORTS 3
25 #define USBC_SC8180X_NOTIFY_IND 0x13
26 #define USBC_CMD_WRITE_REQ 0x15
27 #define USBC_NOTIFY_IND 0x16
29 #define ALTMODE_PAN_EN 0x10
30 #define ALTMODE_PAN_ACK 0x11
32 struct usbc_write_req {
33 struct pmic_glink_hdr hdr;
39 #define NOTIFY_PAYLOAD_SIZE 16
41 struct pmic_glink_hdr hdr;
42 char payload[NOTIFY_PAYLOAD_SIZE];
46 struct usbc_sc8180x_notify {
47 struct pmic_glink_hdr hdr;
52 enum pmic_glink_altmode_pin_assignment {
62 struct pmic_glink_altmode;
64 #define work_to_altmode_port(w) container_of((w), struct pmic_glink_altmode_port, work)
66 struct pmic_glink_altmode_port {
67 struct pmic_glink_altmode *altmode;
70 struct typec_switch *typec_switch;
71 struct typec_mux *typec_mux;
72 struct typec_mux_state state;
73 struct typec_retimer *typec_retimer;
74 struct typec_retimer_state retimer_state;
75 struct typec_altmode dp_alt;
77 struct work_struct work;
79 struct auxiliary_device *bridge;
81 enum typec_orientation orientation;
89 #define work_to_altmode(w) container_of((w), struct pmic_glink_altmode, enable_work)
91 struct pmic_glink_altmode {
94 unsigned int owner_id;
96 /* To synchronize WRITE_REQ acks */
99 struct completion pan_ack;
100 struct pmic_glink_client *client;
102 struct work_struct enable_work;
104 struct pmic_glink_altmode_port ports[PMIC_GLINK_MAX_PORTS];
107 static int pmic_glink_altmode_request(struct pmic_glink_altmode *altmode, u32 cmd, u32 arg)
109 struct usbc_write_req req = {};
114 * The USBC_CMD_WRITE_REQ ack doesn't identify the request, so wait for
117 mutex_lock(&altmode->lock);
119 req.hdr.owner = cpu_to_le32(altmode->owner_id);
120 req.hdr.type = cpu_to_le32(PMIC_GLINK_REQ_RESP);
121 req.hdr.opcode = cpu_to_le32(USBC_CMD_WRITE_REQ);
122 req.cmd = cpu_to_le32(cmd);
123 req.arg = cpu_to_le32(arg);
125 ret = pmic_glink_send(altmode->client, &req, sizeof(req));
127 dev_err(altmode->dev, "failed to send altmode request: %#x (%d)\n", cmd, ret);
131 left = wait_for_completion_timeout(&altmode->pan_ack, 5 * HZ);
133 dev_err(altmode->dev, "timeout waiting for altmode request ack for: %#x\n", cmd);
138 mutex_unlock(&altmode->lock);
142 static void pmic_glink_altmode_enable_dp(struct pmic_glink_altmode *altmode,
143 struct pmic_glink_altmode_port *port,
144 u8 mode, bool hpd_state,
147 struct typec_displayport_data dp_data = {};
150 dp_data.status = DP_STATUS_ENABLED;
152 dp_data.status |= DP_STATUS_HPD_STATE;
154 dp_data.status |= DP_STATUS_IRQ_HPD;
155 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(mode);
157 port->state.alt = &port->dp_alt;
158 port->state.data = &dp_data;
159 port->state.mode = TYPEC_MODAL_STATE(mode);
161 ret = typec_mux_set(port->typec_mux, &port->state);
163 dev_err(altmode->dev, "failed to switch mux to DP: %d\n", ret);
165 port->retimer_state.alt = &port->dp_alt;
166 port->retimer_state.data = &dp_data;
167 port->retimer_state.mode = TYPEC_MODAL_STATE(mode);
169 ret = typec_retimer_set(port->typec_retimer, &port->retimer_state);
171 dev_err(altmode->dev, "failed to setup retimer to DP: %d\n", ret);
174 static void pmic_glink_altmode_enable_usb(struct pmic_glink_altmode *altmode,
175 struct pmic_glink_altmode_port *port)
179 port->state.alt = NULL;
180 port->state.data = NULL;
181 port->state.mode = TYPEC_STATE_USB;
183 ret = typec_mux_set(port->typec_mux, &port->state);
185 dev_err(altmode->dev, "failed to switch mux to USB: %d\n", ret);
187 port->retimer_state.alt = NULL;
188 port->retimer_state.data = NULL;
189 port->retimer_state.mode = TYPEC_STATE_USB;
191 ret = typec_retimer_set(port->typec_retimer, &port->retimer_state);
193 dev_err(altmode->dev, "failed to setup retimer to USB: %d\n", ret);
196 static void pmic_glink_altmode_safe(struct pmic_glink_altmode *altmode,
197 struct pmic_glink_altmode_port *port)
201 port->state.alt = NULL;
202 port->state.data = NULL;
203 port->state.mode = TYPEC_STATE_SAFE;
205 ret = typec_mux_set(port->typec_mux, &port->state);
207 dev_err(altmode->dev, "failed to switch mux to safe mode: %d\n", ret);
209 port->retimer_state.alt = NULL;
210 port->retimer_state.data = NULL;
211 port->retimer_state.mode = TYPEC_STATE_SAFE;
213 ret = typec_retimer_set(port->typec_retimer, &port->retimer_state);
215 dev_err(altmode->dev, "failed to setup retimer to USB: %d\n", ret);
218 static void pmic_glink_altmode_worker(struct work_struct *work)
220 struct pmic_glink_altmode_port *alt_port = work_to_altmode_port(work);
221 struct pmic_glink_altmode *altmode = alt_port->altmode;
223 typec_switch_set(alt_port->typec_switch, alt_port->orientation);
225 if (alt_port->svid == USB_TYPEC_DP_SID && alt_port->mode == 0xff)
226 pmic_glink_altmode_safe(altmode, alt_port);
227 else if (alt_port->svid == USB_TYPEC_DP_SID)
228 pmic_glink_altmode_enable_dp(altmode, alt_port, alt_port->mode,
229 alt_port->hpd_state, alt_port->hpd_irq);
231 pmic_glink_altmode_enable_usb(altmode, alt_port);
233 drm_aux_hpd_bridge_notify(&alt_port->bridge->dev,
234 alt_port->hpd_state ?
235 connector_status_connected :
236 connector_status_disconnected);
238 pmic_glink_altmode_request(altmode, ALTMODE_PAN_ACK, alt_port->index);
241 static enum typec_orientation pmic_glink_altmode_orientation(unsigned int orientation)
243 if (orientation == 0)
244 return TYPEC_ORIENTATION_NORMAL;
245 else if (orientation == 1)
246 return TYPEC_ORIENTATION_REVERSE;
248 return TYPEC_ORIENTATION_NONE;
251 #define SC8180X_PORT_MASK 0x000000ff
252 #define SC8180X_ORIENTATION_MASK 0x0000ff00
253 #define SC8180X_MUX_MASK 0x00ff0000
254 #define SC8180X_MODE_MASK 0x3f000000
255 #define SC8180X_HPD_STATE_MASK 0x40000000
256 #define SC8180X_HPD_IRQ_MASK 0x80000000
258 static void pmic_glink_altmode_sc8180xp_notify(struct pmic_glink_altmode *altmode,
259 const void *data, size_t len)
261 struct pmic_glink_altmode_port *alt_port;
262 const struct usbc_sc8180x_notify *msg;
272 if (len != sizeof(*msg)) {
273 dev_warn(altmode->dev, "invalid length of USBC_NOTIFY indication: %zd\n", len);
278 notification = le32_to_cpu(msg->notification);
279 port = FIELD_GET(SC8180X_PORT_MASK, notification);
280 orientation = FIELD_GET(SC8180X_ORIENTATION_MASK, notification);
281 mux = FIELD_GET(SC8180X_MUX_MASK, notification);
282 mode = FIELD_GET(SC8180X_MODE_MASK, notification);
283 hpd_state = FIELD_GET(SC8180X_HPD_STATE_MASK, notification);
284 hpd_irq = FIELD_GET(SC8180X_HPD_IRQ_MASK, notification);
286 svid = mux == 2 ? USB_TYPEC_DP_SID : 0;
288 if (port >= ARRAY_SIZE(altmode->ports) || !altmode->ports[port].altmode) {
289 dev_dbg(altmode->dev, "notification on undefined port %d\n", port);
293 alt_port = &altmode->ports[port];
294 alt_port->orientation = pmic_glink_altmode_orientation(orientation);
295 alt_port->svid = svid;
296 alt_port->mode = mode;
297 alt_port->hpd_state = hpd_state;
298 alt_port->hpd_irq = hpd_irq;
299 schedule_work(&alt_port->work);
302 #define SC8280XP_DPAM_MASK 0x3f
303 #define SC8280XP_HPD_STATE_MASK BIT(6)
304 #define SC8280XP_HPD_IRQ_MASK BIT(7)
306 static void pmic_glink_altmode_sc8280xp_notify(struct pmic_glink_altmode *altmode,
307 u16 svid, const void *data, size_t len)
309 struct pmic_glink_altmode_port *alt_port;
310 const struct usbc_notify *notify;
317 if (len != sizeof(*notify)) {
318 dev_warn(altmode->dev, "invalid length USBC_NOTIFY_IND: %zd\n",
325 port = notify->payload[0];
326 orientation = notify->payload[1];
327 mode = FIELD_GET(SC8280XP_DPAM_MASK, notify->payload[8]) - DPAM_HPD_A;
328 hpd_state = FIELD_GET(SC8280XP_HPD_STATE_MASK, notify->payload[8]);
329 hpd_irq = FIELD_GET(SC8280XP_HPD_IRQ_MASK, notify->payload[8]);
331 if (port >= ARRAY_SIZE(altmode->ports) || !altmode->ports[port].altmode) {
332 dev_dbg(altmode->dev, "notification on undefined port %d\n", port);
336 alt_port = &altmode->ports[port];
337 alt_port->orientation = pmic_glink_altmode_orientation(orientation);
338 alt_port->svid = svid;
339 alt_port->mode = mode;
340 alt_port->hpd_state = hpd_state;
341 alt_port->hpd_irq = hpd_irq;
342 schedule_work(&alt_port->work);
345 static void pmic_glink_altmode_callback(const void *data, size_t len, void *priv)
347 struct pmic_glink_altmode *altmode = priv;
348 const struct pmic_glink_hdr *hdr = data;
352 opcode = le32_to_cpu(hdr->opcode) & 0xff;
353 svid = le32_to_cpu(hdr->opcode) >> 16;
356 case USBC_CMD_WRITE_REQ:
357 complete(&altmode->pan_ack);
359 case USBC_NOTIFY_IND:
360 pmic_glink_altmode_sc8280xp_notify(altmode, svid, data, len);
362 case USBC_SC8180X_NOTIFY_IND:
363 pmic_glink_altmode_sc8180xp_notify(altmode, data, len);
368 static void pmic_glink_altmode_put_retimer(void *data)
370 typec_retimer_put(data);
373 static void pmic_glink_altmode_put_mux(void *data)
378 static void pmic_glink_altmode_put_switch(void *data)
380 typec_switch_put(data);
383 static void pmic_glink_altmode_enable_worker(struct work_struct *work)
385 struct pmic_glink_altmode *altmode = work_to_altmode(work);
388 ret = pmic_glink_altmode_request(altmode, ALTMODE_PAN_EN, 0);
390 dev_err(altmode->dev, "failed to request altmode notifications: %d\n", ret);
393 static void pmic_glink_altmode_pdr_notify(void *priv, int state)
395 struct pmic_glink_altmode *altmode = priv;
397 if (state == SERVREG_SERVICE_STATE_UP)
398 schedule_work(&altmode->enable_work);
401 static const struct of_device_id pmic_glink_altmode_of_quirks[] = {
402 { .compatible = "qcom,sc8180x-pmic-glink", .data = (void *)PMIC_GLINK_OWNER_USBC },
406 static int pmic_glink_altmode_probe(struct auxiliary_device *adev,
407 const struct auxiliary_device_id *id)
409 struct pmic_glink_altmode_port *alt_port;
410 struct pmic_glink_altmode *altmode;
411 const struct of_device_id *match;
412 struct fwnode_handle *fwnode;
413 struct device *dev = &adev->dev;
417 altmode = devm_kzalloc(dev, sizeof(*altmode), GFP_KERNEL);
423 match = of_match_device(pmic_glink_altmode_of_quirks, dev->parent);
425 altmode->owner_id = (unsigned long)match->data;
427 altmode->owner_id = PMIC_GLINK_OWNER_USBC_PAN;
429 INIT_WORK(&altmode->enable_work, pmic_glink_altmode_enable_worker);
430 init_completion(&altmode->pan_ack);
431 mutex_init(&altmode->lock);
433 device_for_each_child_node(dev, fwnode) {
434 ret = fwnode_property_read_u32(fwnode, "reg", &port);
436 dev_err(dev, "missing reg property of %pOFn\n", fwnode);
437 fwnode_handle_put(fwnode);
441 if (port >= ARRAY_SIZE(altmode->ports)) {
442 dev_warn(dev, "invalid connector number, ignoring\n");
446 if (altmode->ports[port].altmode) {
447 dev_err(dev, "multiple connector definition for port %u\n", port);
448 fwnode_handle_put(fwnode);
452 alt_port = &altmode->ports[port];
453 alt_port->altmode = altmode;
454 alt_port->index = port;
455 INIT_WORK(&alt_port->work, pmic_glink_altmode_worker);
457 alt_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode));
458 if (IS_ERR(alt_port->bridge)) {
459 fwnode_handle_put(fwnode);
460 return PTR_ERR(alt_port->bridge);
463 alt_port->dp_alt.svid = USB_TYPEC_DP_SID;
464 alt_port->dp_alt.mode = USB_TYPEC_DP_MODE;
465 alt_port->dp_alt.active = 1;
467 alt_port->typec_mux = fwnode_typec_mux_get(fwnode);
468 if (IS_ERR(alt_port->typec_mux)) {
469 fwnode_handle_put(fwnode);
470 return dev_err_probe(dev, PTR_ERR(alt_port->typec_mux),
471 "failed to acquire mode-switch for port: %d\n",
475 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_mux,
476 alt_port->typec_mux);
478 fwnode_handle_put(fwnode);
482 alt_port->typec_retimer = fwnode_typec_retimer_get(fwnode);
483 if (IS_ERR(alt_port->typec_retimer)) {
484 fwnode_handle_put(fwnode);
485 return dev_err_probe(dev, PTR_ERR(alt_port->typec_retimer),
486 "failed to acquire retimer-switch for port: %d\n",
490 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_retimer,
491 alt_port->typec_retimer);
493 fwnode_handle_put(fwnode);
497 alt_port->typec_switch = fwnode_typec_switch_get(fwnode);
498 if (IS_ERR(alt_port->typec_switch)) {
499 fwnode_handle_put(fwnode);
500 return dev_err_probe(dev, PTR_ERR(alt_port->typec_switch),
501 "failed to acquire orientation-switch for port: %d\n",
505 ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_switch,
506 alt_port->typec_switch);
508 fwnode_handle_put(fwnode);
513 for (port = 0; port < ARRAY_SIZE(altmode->ports); port++) {
514 alt_port = &altmode->ports[port];
515 if (!alt_port->bridge)
518 ret = devm_drm_dp_hpd_bridge_add(dev, alt_port->bridge);
523 altmode->client = devm_pmic_glink_register_client(dev,
525 pmic_glink_altmode_callback,
526 pmic_glink_altmode_pdr_notify,
528 return PTR_ERR_OR_ZERO(altmode->client);
531 static const struct auxiliary_device_id pmic_glink_altmode_id_table[] = {
532 { .name = "pmic_glink.altmode", },
535 MODULE_DEVICE_TABLE(auxiliary, pmic_glink_altmode_id_table);
537 static struct auxiliary_driver pmic_glink_altmode_driver = {
538 .name = "pmic_glink_altmode",
539 .probe = pmic_glink_altmode_probe,
540 .id_table = pmic_glink_altmode_id_table,
543 module_auxiliary_driver(pmic_glink_altmode_driver);
545 MODULE_DESCRIPTION("Qualcomm PMIC GLINK Altmode driver");
546 MODULE_LICENSE("GPL");