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