summaryrefslogtreecommitdiff
path: root/kernel-pinebook-pro.patch
blob: d92d5ab799e415819e2f2b71bb1eda31c5fc6836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
From 8149051c34bc3d4c55adc56d04ffb7f7a04c2fd9 Mon Sep 17 00:00:00 2001
From: Dan Johansen <strit@manjaro.org>
Date: Sun, 2 Jan 2022 16:45:28 +0100
Subject: [PATCH 1/2] Add megis extcon changes to fusb302

Signed-off-by: Dan Johansen <strit@manjaro.org>
---
 drivers/phy/rockchip/phy-rockchip-typec.c |  5 +++
 drivers/usb/typec/Kconfig                 |  7 ++++
 drivers/usb/typec/Makefile                |  1 +
 drivers/usb/typec/tcpm/fusb302.c          | 47 ++++++++++++++++-------
 drivers/usb/typec/tcpm/fusb302_reg.h      | 16 ++++----
 5 files changed, 53 insertions(+), 23 deletions(-)

diff --git a/drivers/phy/rockchip/phy-rockchip-typec.c b/drivers/phy/rockchip/phy-rockchip-typec.c
index d2bbdc96a167..fa10ee9a5794 100644
--- a/drivers/phy/rockchip/phy-rockchip-typec.c
+++ b/drivers/phy/rockchip/phy-rockchip-typec.c
@@ -350,6 +350,7 @@ struct usb3phy_reg {
  * struct rockchip_usb3phy_port_cfg - usb3-phy port configuration.
  * @reg: the base address for usb3-phy config.
  * @typec_conn_dir: the register of type-c connector direction.
+ * @typec_conn_dir_sel: the register of type-c connector direction source.
  * @usb3tousb2_en: the register of type-c force usb2 to usb2 enable.
  * @external_psm: the register of type-c phy external psm clock.
  * @pipe_status: the register of type-c phy pipe status.
@@ -360,6 +361,7 @@ struct usb3phy_reg {
 struct rockchip_usb3phy_port_cfg {
 	unsigned int reg;
 	struct usb3phy_reg typec_conn_dir;
+	struct usb3phy_reg typec_conn_dir_sel;
 	struct usb3phy_reg usb3tousb2_en;
 	struct usb3phy_reg external_psm;
 	struct usb3phy_reg pipe_status;
@@ -434,6 +436,7 @@ static const struct rockchip_usb3phy_port_cfg rk3399_usb3phy_port_cfgs[] = {
 	{
 		.reg = 0xff7c0000,
 		.typec_conn_dir	= { 0xe580, 0, 16 },
+		.typec_conn_dir_sel	= { 0xe580, 8, 16+8 },
 		.usb3tousb2_en	= { 0xe580, 3, 19 },
 		.external_psm	= { 0xe588, 14, 30 },
 		.pipe_status	= { 0xe5c0, 0, 0 },
@@ -444,6 +447,7 @@ static const struct rockchip_usb3phy_port_cfg rk3399_usb3phy_port_cfgs[] = {
 	{
 		.reg = 0xff800000,
 		.typec_conn_dir	= { 0xe58c, 0, 16 },
+		.typec_conn_dir_sel	= { 0xe58c, 8, 16+8 },
 		.usb3tousb2_en	= { 0xe58c, 3, 19 },
 		.external_psm	= { 0xe594, 14, 30 },
 		.pipe_status	= { 0xe5c0, 16, 16 },
@@ -739,6 +743,7 @@ static int tcphy_phy_init(struct rockchip_typec_phy *tcphy, u8 mode)
 
 	reset_control_deassert(tcphy->tcphy_rst);
 
+	property_enable(tcphy, &cfg->typec_conn_dir_sel, 0);
 	property_enable(tcphy, &cfg->typec_conn_dir, tcphy->flip);
 	tcphy_dp_aux_set_flip(tcphy);
 
diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig
index ab480f38523a..01ecc5e590f1 100644
--- a/drivers/usb/typec/Kconfig
+++ b/drivers/usb/typec/Kconfig
@@ -88,6 +88,13 @@ config TYPEC_QCOM_PMIC
 	  If you choose to build this driver as a dynamically linked module, the
 	  module will be called wusb3801.ko.
 
+config TYPEC_EXTCON
+	tristate "Type-C switch/mux -> extcon interface bridge driver"
+	depends on USB_ROLE_SWITCH
+	help
+	  Say Y or M here if your system needs bridging between typec class
+	  and extcon interfaces.
+
 source "drivers/usb/typec/mux/Kconfig"
 
 source "drivers/usb/typec/altmodes/Kconfig"
diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
index a0adb8947a30..d9d829386b73 100644
--- a/drivers/usb/typec/Makefile
+++ b/drivers/usb/typec/Makefile
@@ -8,4 +8,5 @@ obj-$(CONFIG_TYPEC_TPS6598X)	+= tipd/
 obj-$(CONFIG_TYPEC_STUSB160X) 	+= stusb160x.o
 obj-$(CONFIG_TYPEC_RT1719)	+= rt1719.o
 obj-$(CONFIG_TYPEC_WUSB3801)	+= wusb3801.o
+obj-$(CONFIG_TYPEC_EXTCON)	+= typec-extcon.o
 obj-$(CONFIG_TYPEC)		+= mux/
diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index 72f9001b0792..cb26793f90f8 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -440,6 +440,16 @@ static int tcpm_get_current_limit(struct tcpc_dev *dev)
 	int current_limit = 0;
 	unsigned long timeout;
 
+	/*
+	 * To avoid cycles in OF dependencies, we get extcon when necessary
+	 * outside of probe function.
+	 */
+	if (of_property_read_bool(chip->dev->of_node, "extcon") && !chip->extcon) {
+		chip->extcon = extcon_get_edev_by_phandle(chip->dev, 0);
+		if (IS_ERR(chip->extcon))
+			chip->extcon = NULL;
+	}
+
 	if (!chip->extcon)
 		return 0;
 
@@ -498,6 +508,7 @@ static int fusb302_set_toggling(struct fusb302_chip *chip,
 				enum toggling_mode mode)
 {
 	int ret = 0;
+	u8 reg;
 
 	/* first disable toggling */
 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
@@ -556,6 +567,12 @@ static int fusb302_set_toggling(struct fusb302_chip *chip,
 	} else {
 		/* Datasheet says vconn MUST be off when toggling */
 		WARN(chip->vconn_on, "Vconn is on during toggle start");
+
+		/* clear interrupts */
+                ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &reg);
+		if (ret < 0)
+			return ret;
+
 		/* unmask TOGDONE interrupt */
 		ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
 					     FUSB_REG_MASKA_TOGDONE);
@@ -635,6 +652,14 @@ static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
 		goto done;
 	}
 
+	/* adjust current for SRC */
+	ret = fusb302_set_src_current(chip, cc_src_current[cc]);
+	if (ret < 0) {
+		fusb302_log(chip, "cannot set src current %s, ret=%d",
+			    typec_cc_status_name[cc], ret);
+		goto done;
+	}
+
 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
 				     switches0_mask, switches0_data);
 	if (ret < 0) {
@@ -645,14 +670,6 @@ static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
 	chip->cc1 = TYPEC_CC_OPEN;
 	chip->cc2 = TYPEC_CC_OPEN;
 
-	/* adjust current for SRC */
-	ret = fusb302_set_src_current(chip, cc_src_current[cc]);
-	if (ret < 0) {
-		fusb302_log(chip, "cannot set src current %s, ret=%d",
-			    typec_cc_status_name[cc], ret);
-		goto done;
-	}
-
 	/* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
 	switch (cc) {
 	case TYPEC_CC_RP_DEF:
@@ -1528,14 +1545,16 @@ static void fusb302_irq_work(struct work_struct *work)
 		    "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
 		    interrupt, interrupta, interruptb, status0);
 
-	if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
-		vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
+	vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
+	if (interrupt & FUSB_REG_INTERRUPT_VBUSOK)
 		fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
 			    vbus_present ? "On" : "Off");
-		if (vbus_present != chip->vbus_present) {
-			chip->vbus_present = vbus_present;
-			tcpm_vbus_change(chip->tcpm_port);
-		}
+	if (vbus_present != chip->vbus_present) {
+		chip->vbus_present = vbus_present;
+		if (!(interrupt & FUSB_REG_INTERRUPT_VBUSOK))
+		fusb302_log(chip, "IRQ: VBUS changed without interrupt, vbus=%s",
+			    vbus_present ? "On" : "Off");
+		tcpm_vbus_change(chip->tcpm_port);
 	}
 
 	if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
diff --git a/drivers/usb/typec/tcpm/fusb302_reg.h b/drivers/usb/typec/tcpm/fusb302_reg.h
index edc0e4b0f1e6..f37d226c5027 100644
--- a/drivers/usb/typec/tcpm/fusb302_reg.h
+++ b/drivers/usb/typec/tcpm/fusb302_reg.h
@@ -27,14 +27,13 @@
 #define FUSB_REG_SWITCHES1_TXCC2_EN		BIT(1)
 #define FUSB_REG_SWITCHES1_TXCC1_EN		BIT(0)
 #define FUSB_REG_MEASURE			0x04
-#define FUSB_REG_MEASURE_MDAC5			BIT(7)
-#define FUSB_REG_MEASURE_MDAC4			BIT(6)
-#define FUSB_REG_MEASURE_MDAC3			BIT(5)
-#define FUSB_REG_MEASURE_MDAC2			BIT(4)
-#define FUSB_REG_MEASURE_MDAC1			BIT(3)
-#define FUSB_REG_MEASURE_MDAC0			BIT(2)
-#define FUSB_REG_MEASURE_VBUS			BIT(1)
-#define FUSB_REG_MEASURE_XXXX5			BIT(0)
+#define FUSB_REG_MEASURE_VBUS			BIT(6)
+#define FUSB_REG_MEASURE_MDAC5			BIT(5)
+#define FUSB_REG_MEASURE_MDAC4			BIT(4)
+#define FUSB_REG_MEASURE_MDAC3			BIT(3)
+#define FUSB_REG_MEASURE_MDAC2			BIT(2)
+#define FUSB_REG_MEASURE_MDAC1			BIT(1)
+#define FUSB_REG_MEASURE_MDAC0			BIT(0)
 #define FUSB_REG_CONTROL0			0x06
 #define FUSB_REG_CONTROL0_TX_FLUSH		BIT(6)
 #define FUSB_REG_CONTROL0_INT_MASK		BIT(5)
@@ -105,7 +104,6 @@
 #define FUSB_REG_STATUS0A_RX_SOFT_RESET		BIT(1)
 #define FUSB_REG_STATUS0A_RX_HARD_RESET		BIT(0)
 #define FUSB_REG_STATUS1A			0x3D
-#define FUSB_REG_STATUS1A_TOGSS			BIT(3)
 #define FUSB_REG_STATUS1A_TOGSS_RUNNING		0x0
 #define FUSB_REG_STATUS1A_TOGSS_SRC1		0x1
 #define FUSB_REG_STATUS1A_TOGSS_SRC2		0x2
-- 
2.34.1

From 6af2e6a2d59bd755234e5e15a47dfa669788143c Mon Sep 17 00:00:00 2001
From: Dan Johansen <strit@manjaro.org>
Date: Sun, 2 Jan 2022 16:47:40 +0100
Subject: [PATCH 2/2] usb: typec: Add megis typex to extcon bridge driver

Signed-off-by: Dan Johansen <strit@manjaro.org>
---
 drivers/usb/typec/typec-extcon.c | 337 +++++++++++++++++++++++++++++++
 1 file changed, 337 insertions(+)
 create mode 100644 drivers/usb/typec/typec-extcon.c

diff --git a/drivers/usb/typec/typec-extcon.c b/drivers/usb/typec/typec-extcon.c
new file mode 100644
index 000000000000..143ff2486f2f
--- /dev/null
+++ b/drivers/usb/typec/typec-extcon.c
@@ -0,0 +1,337 @@
+/*
+ * typec -> extcon bridge
+ * Copyright (c) 2021 Ondřej Jirman <megi@xff.cz>
+ *
+ * This driver bridges standard type-c interfaces to drivers that
+ * expect extcon interface.
+ */
+
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/power_supply.h>
+#include <linux/platform_device.h>
+#include <linux/usb/pd.h>
+#include <linux/usb/role.h>
+#include <linux/usb/typec.h>
+#include <linux/usb/typec_dp.h>
+#include <linux/usb/typec_mux.h>
+#include <linux/extcon-provider.h>
+
+struct typec_extcon {
+        struct device *dev;
+
+	/* consumers */
+	struct usb_role_switch *role_sw;
+        struct typec_switch_dev *sw;
+        struct typec_mux_dev *mux;
+
+	/* providers */
+	struct extcon_dev *extcon;
+	struct notifier_block extcon_nb;
+
+	/* cached state from typec controller */
+	enum usb_role role;
+	enum typec_orientation orientation;
+	struct typec_altmode alt;
+	unsigned long mode;
+	bool has_alt;
+	struct mutex lock;
+};
+
+static const unsigned int typec_extcon_cable[] = {
+	EXTCON_DISP_DP,
+
+	EXTCON_USB,
+	EXTCON_USB_HOST,
+
+	EXTCON_CHG_USB_SDP,
+	EXTCON_CHG_USB_CDP,
+	EXTCON_CHG_USB_DCP,
+	EXTCON_CHG_USB_ACA,
+
+	EXTCON_NONE,
+};
+
+static void typec_extcon_set_cable(struct typec_extcon *tce, int id, bool on,
+				   union extcon_property_value prop_ss,
+				   union extcon_property_value prop_or)
+{
+	union extcon_property_value cur_ss, cur_or;
+	bool prop_diff = false;
+	int ret;
+
+	ret = extcon_get_property(tce->extcon, id,
+				  EXTCON_PROP_USB_SS, &cur_ss);
+	if (ret || cur_ss.intval != prop_ss.intval)
+		prop_diff = true;
+
+	ret = extcon_get_property(tce->extcon, id,
+				  EXTCON_PROP_USB_TYPEC_POLARITY, &cur_or);
+	if (ret || cur_or.intval != prop_or.intval)
+		prop_diff = true;
+
+	if (!on && extcon_get_state(tce->extcon, id)) {
+		extcon_set_state_sync(tce->extcon, id, false);
+	} else if (on && (!extcon_get_state(tce->extcon, id) || prop_diff)) {
+		extcon_set_state(tce->extcon, id, true);
+		extcon_set_property(tce->extcon, id,
+				    EXTCON_PROP_USB_SS, prop_ss);
+		extcon_set_property(tce->extcon, id,
+				    EXTCON_PROP_USB_TYPEC_POLARITY, prop_or);
+		extcon_sync(tce->extcon, id);
+	}
+}
+
+static int typec_extcon_sync_extcon(struct typec_extcon *tce)
+{
+	union extcon_property_value prop_ss, prop_or;
+	bool has_dp = false;
+
+        mutex_lock(&tce->lock);
+
+	/* connector is disconnected */
+	if (tce->orientation == TYPEC_ORIENTATION_NONE) {
+		typec_extcon_set_cable(tce, EXTCON_USB, false, prop_ss, prop_or);
+		typec_extcon_set_cable(tce, EXTCON_USB_HOST, false, prop_ss, prop_or);
+		typec_extcon_set_cable(tce, EXTCON_DISP_DP, false, prop_ss, prop_or);
+
+		extcon_set_state_sync(tce->extcon, EXTCON_CHG_USB_SDP, false);
+		extcon_set_state_sync(tce->extcon, EXTCON_CHG_USB_DCP, false);
+		extcon_set_state_sync(tce->extcon, EXTCON_CHG_USB_CDP, false);
+		extcon_set_state_sync(tce->extcon, EXTCON_CHG_USB_ACA, false);
+
+                goto out_unlock;
+	}
+
+	prop_or.intval = tce->orientation == TYPEC_ORIENTATION_NORMAL ? 0 : 1;
+	prop_ss.intval = 0;
+
+	if (tce->has_alt && tce->alt.svid == USB_TYPEC_DP_SID) {
+		switch (tce->mode) {
+		case TYPEC_STATE_SAFE:
+			break;
+		case TYPEC_DP_STATE_C:
+		case TYPEC_DP_STATE_E:
+			has_dp = true;
+			break;
+		case TYPEC_DP_STATE_D:
+			has_dp = true;
+			fallthrough;
+		case TYPEC_STATE_USB:
+			prop_ss.intval = 1;
+			break;
+		default:
+			dev_err(tce->dev, "unhandled mux mode=%lu\n", tce->mode);
+			break;
+		}
+	}
+
+	typec_extcon_set_cable(tce, EXTCON_USB,
+			tce->role == USB_ROLE_DEVICE, prop_ss, prop_or);
+	typec_extcon_set_cable(tce, EXTCON_USB_HOST,
+			tce->role == USB_ROLE_HOST, prop_ss, prop_or);
+
+	typec_extcon_set_cable(tce, EXTCON_DISP_DP, has_dp, prop_ss, prop_or);
+
+out_unlock:
+	mutex_unlock(&tce->lock);
+	return 0;
+}
+
+static int typec_extcon_sw_set(struct typec_switch_dev *sw,
+			       enum typec_orientation orientation)
+{
+        struct typec_extcon *tce = typec_switch_get_drvdata(sw);
+
+	dev_dbg(tce->dev, "SW SET: orientation=%d\n", orientation);
+
+        mutex_lock(&tce->lock);
+	tce->orientation = orientation;
+        mutex_unlock(&tce->lock);
+
+	typec_extcon_sync_extcon(tce);
+
+        return 0;
+}
+
+static int typec_extcon_mux_set(struct typec_mux_dev *mux,
+				struct typec_mux_state *state)
+{
+        struct typec_extcon *tce = typec_mux_get_drvdata(mux);
+	struct typec_altmode *alt = state->alt;
+
+	dev_dbg(tce->dev, "MUX SET: state->mode=%lu\n", state->mode);
+	if (alt)
+		dev_dbg(tce->dev, "      ...alt: svid=%04hx mode=%d vdo=%08x active=%u\n",
+			alt->svid, alt->mode, alt->vdo, alt->active);
+
+        mutex_lock(&tce->lock);
+	tce->mode = state->mode;
+	tce->has_alt = alt != NULL;
+        if (alt)
+		tce->alt = *alt;
+	mutex_unlock(&tce->lock);
+
+	typec_extcon_sync_extcon(tce);
+
+        return 0;
+}
+
+static int typec_extcon_usb_set_role(struct usb_role_switch *sw,
+				     enum usb_role role)
+{
+        struct typec_extcon *tce = usb_role_switch_get_drvdata(sw);
+
+	dev_dbg(tce->dev, "ROLE SET: role=%d\n", role);
+
+        mutex_lock(&tce->lock);
+	tce->role = role;
+	mutex_unlock(&tce->lock);
+
+	typec_extcon_sync_extcon(tce);
+
+        return 0;
+}
+
+static int typec_extcon_notifier(struct notifier_block *nb,
+					 unsigned long action, void *data)
+{
+	struct typec_extcon *tce = container_of(nb, struct typec_extcon, extcon_nb);
+
+	bool sdp = extcon_get_state(tce->extcon, EXTCON_CHG_USB_SDP);
+	bool cdp = extcon_get_state(tce->extcon, EXTCON_CHG_USB_CDP);
+	bool dcp = extcon_get_state(tce->extcon, EXTCON_CHG_USB_DCP);
+	bool usb = extcon_get_state(tce->extcon, EXTCON_USB);
+	bool usb_host = extcon_get_state(tce->extcon, EXTCON_USB_HOST);
+	bool dp = extcon_get_state(tce->extcon, EXTCON_DISP_DP);
+
+	dev_info(tce->dev, "extcon changed sdp=%d cdp=%d dcp=%d usb=%d usb_host=%d dp=%d\n",
+		 sdp, cdp, dcp, usb, usb_host, dp);
+
+	return NOTIFY_OK;
+}
+
+static int typec_extcon_probe(struct platform_device *pdev)
+{
+        struct typec_switch_desc sw_desc = { };
+        struct typec_mux_desc mux_desc = { };
+        struct usb_role_switch_desc role_desc = { };
+        struct device *dev = &pdev->dev;
+        struct typec_extcon *tce;
+        int ret = 0;
+
+        tce = devm_kzalloc(dev, sizeof(*tce), GFP_KERNEL);
+        if (!tce)
+                return -ENOMEM;
+
+        tce->dev = &pdev->dev;
+	mutex_init(&tce->lock);
+	tce->mode = TYPEC_STATE_SAFE;
+
+	sw_desc.drvdata = tce;
+	sw_desc.fwnode = dev->fwnode;
+	sw_desc.set = typec_extcon_sw_set;
+
+	tce->sw = typec_switch_register(dev, &sw_desc);
+	if (IS_ERR(tce->sw))
+		return dev_err_probe(dev, PTR_ERR(tce->sw),
+				     "Error registering typec switch\n");
+
+	mux_desc.drvdata = tce;
+	mux_desc.fwnode = dev->fwnode;
+	mux_desc.set = typec_extcon_mux_set;
+
+	tce->mux = typec_mux_register(dev, &mux_desc);
+	if (IS_ERR(tce->mux)) {
+		ret = dev_err_probe(dev, PTR_ERR(tce->mux),
+				    "Error registering typec mux\n");
+		goto err_sw;
+	}
+
+	role_desc.driver_data = tce;
+	role_desc.fwnode = dev->fwnode;
+	role_desc.name = fwnode_get_name(dev->fwnode);
+	role_desc.set = typec_extcon_usb_set_role;
+
+	tce->role_sw = usb_role_switch_register(dev, &role_desc);
+	if (IS_ERR(tce->role_sw)) {
+		ret = dev_err_probe(dev, PTR_ERR(tce->role_sw),
+				    "Error registering USB role switch\n");
+		goto err_mux;
+	}
+
+	tce->extcon = devm_extcon_dev_allocate(dev, typec_extcon_cable);
+	if (IS_ERR(tce->extcon)) {
+		ret = PTR_ERR(tce->extcon);
+		goto err_role;
+	}
+
+	ret = devm_extcon_dev_register(dev, tce->extcon);
+	if (ret) {
+		ret = dev_err_probe(dev, ret, "failed to register extcon device\n");
+		goto err_role;
+	}
+
+	extcon_set_property_capability(tce->extcon, EXTCON_USB,
+				       EXTCON_PROP_USB_SS);
+	extcon_set_property_capability(tce->extcon, EXTCON_USB,
+				       EXTCON_PROP_USB_TYPEC_POLARITY);
+	extcon_set_property_capability(tce->extcon, EXTCON_USB_HOST,
+				       EXTCON_PROP_USB_SS);
+	extcon_set_property_capability(tce->extcon, EXTCON_USB_HOST,
+				       EXTCON_PROP_USB_TYPEC_POLARITY);
+	extcon_set_property_capability(tce->extcon, EXTCON_DISP_DP,
+				       EXTCON_PROP_USB_SS);
+	extcon_set_property_capability(tce->extcon, EXTCON_DISP_DP,
+				       EXTCON_PROP_USB_TYPEC_POLARITY);
+
+	tce->extcon_nb.notifier_call = typec_extcon_notifier;
+	ret = devm_extcon_register_notifier_all(dev, tce->extcon, &tce->extcon_nb);
+	if (ret) {
+		dev_err_probe(dev, ret, "Failed to register extcon notifier\n");
+		goto err_role;
+	}
+
+	return 0;
+
+err_role:
+	usb_role_switch_unregister(tce->role_sw);
+err_mux:
+	typec_mux_unregister(tce->mux);
+err_sw:
+	typec_switch_unregister(tce->sw);
+	return ret;
+}
+
+static int typec_extcon_remove(struct platform_device *pdev)
+{
+        struct typec_extcon *tce = platform_get_drvdata(pdev);
+
+	usb_role_switch_unregister(tce->role_sw);
+	typec_mux_unregister(tce->mux);
+	typec_switch_unregister(tce->sw);
+
+        return 0;
+}
+
+static struct of_device_id typec_extcon_of_match_table[] = {
+        { .compatible = "linux,typec-extcon-bridge" },
+        { },
+};
+MODULE_DEVICE_TABLE(of, typec_extcon_of_match_table);
+
+static struct platform_driver typec_extcon_driver = {
+        .driver = {
+                .name = "typec-extcon",
+                .of_match_table = typec_extcon_of_match_table,
+        },
+        .probe = typec_extcon_probe,
+        .remove = typec_extcon_remove,
+};
+
+module_platform_driver(typec_extcon_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Ondrej Jirman <megous@megous.com>");
+MODULE_DESCRIPTION("typec -> extcon bridge driver");
-- 
2.34.1

From 4c839ce95766910235ff558b2959589c9068917c Mon Sep 17 00:00:00 2001
From: Dan Johansen <strit@manjaro.org>
Date: Sun, 2 Jan 2022 19:15:39 +0100
Subject: [PATCH] arm64: dts: rockchip: add typec extcon hack

Signed-off-by: Dan Johansen <strit@manjaro.org>
---
 .../boot/dts/rockchip/rk3399-pinebook-pro.dts | 31 +++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/boot/dts/rockchip/rk3399-pinebook-pro.dts b/arch/arm64/boot/dts/rockchip/rk3399-pinebook-pro.dts
index c2f021a1a18f..fc33e111bbee 100644
--- a/arch/arm64/boot/dts/rockchip/rk3399-pinebook-pro.dts
+++ b/arch/arm64/boot/dts/rockchip/rk3399-pinebook-pro.dts
@@ -384,6 +384,20 @@ mains_charger: dc-charger {
 		pinctrl-names = "default";
 		pinctrl-0 = <&dc_det_pin>;
 	};
+	
+    typec_extcon_bridge: typec-extcon {
+		compatible = "linux,typec-extcon-bridge";
+		usb-role-switch;
+		orientation-switch;
+		mode-switch;
+		svid = /bits/ 16 <0xff01>;
+	};
+};
+
+&cdn_dp {
+	status = "okay";
+	extcon = <&typec_extcon_bridge>;
+	phys = <&tcphy0_dp>;
 };
 
 &cpu_b0 {
@@ -705,6 +719,8 @@ fusb0: fusb30x@22 {
 		pinctrl-names = "default";
 		pinctrl-0 = <&fusb0_int_pin>;
 		vbus-supply = <&vbus_typec>;
+		extcon = <&typec_extcon_bridge>;
+		usb-role-switch = <&typec_extcon_bridge>;
 
 		connector {
 			compatible = "usb-c-connector";
@@ -713,10 +729,20 @@ connector {
 			op-sink-microwatt = <1000000>;
 			power-role = "dual";
 			sink-pdos =
-				<PDO_FIXED(5000, 2500, PDO_FIXED_USB_COMM)>;
+				<PDO_FIXED(5000, 2500, PDO_FIXED_USB_COMM | PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP)>;
 			source-pdos =
-				<PDO_FIXED(5000, 1400, PDO_FIXED_USB_COMM)>;
+				<PDO_FIXED(5000, 1400, PDO_FIXED_USB_COMM | PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP)>;
 			try-power-role = "sink";
+			
+			mode-switch = <&typec_extcon_bridge>;
+			orientation-switch = <&typec_extcon_bridge>;
+
+			altmodes {
+				dp {
+					svid = <0xff01>;
+					vdo = <0x0c0006>;
+				};
+			};
 
 			ports {
 				#address-cells = <1>;
@@ -984,6 +1010,7 @@ spiflash: flash@0 {
 };
 
 &tcphy0 {
+	extcon = <&typec_extcon_bridge>;
 	status = "okay";
 };
 
-- 
2.34.1