1 // SPDX-License-Identifier: GPL-2.0
3 * * extcon-rtk-type-c.c - Realtek Extcon Type C driver
5 * Copyright (C) 2023 Realtek Semiconductor Corporation
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/syscalls.h>
19 #include <linux/suspend.h>
20 #include <linux/debugfs.h>
21 #include <linux/extcon.h>
22 #include <linux/extcon-provider.h>
23 #include <linux/sys_soc.h>
24 #include <linux/nvmem-consumer.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/usb/otg.h>
27 #include <linux/usb/typec.h>
46 int parameter_ver
; /* Parameter version */
48 struct cc_param cc1_param
;
49 struct cc_param cc2_param
;
52 bool use_defalut_parameter
;
56 void __iomem
*reg_base
;
58 struct extcon_dev
*edev
;
62 /* rd control GPIO only for rtd1295 */
63 struct gpio_desc
*rd_ctrl_gpio_desc
;
66 struct type_c_cfg
*type_c_cfg
;
73 u32 debounce
; /* 1b,1us 7f,4.7us */
77 #define CONNECT_CHANGE 1
78 #define CONNECT_NO_CHANGE 0
79 int cc_mode
; /* cc is host or device */
80 #define IN_HOST_MODE 0x10
81 #define IN_DEVICE_MODE 0x20
93 /* protect the data member */
95 struct delayed_work delayed_work
;
99 struct dentry
*debug_dir
;
101 struct typec_port
*port
;
104 /* Type C register offset */
105 #define USB_TYPEC_CTRL_CC1_0 0x0
106 #define USB_TYPEC_CTRL_CC1_1 0x4
107 #define USB_TYPEC_CTRL_CC2_0 0x8
108 #define USB_TYPEC_CTRL_CC2_1 0xC
109 #define USB_TYPEC_STS 0x10
110 #define USB_TYPEC_CTRL 0x14
111 #define USB_DBUS_PWR_CTRL 0x18
113 #define ENABLE_CC1 0x1
114 #define ENABLE_CC2 0x2
115 #define DISABLE_CC 0x0
117 /* Bit mapping USB_TYPEC_CTRL_CC1_0 and USB_TYPEC_CTRL_CC2_0 */
118 #define PLR_EN BIT(29)
119 #define CC_SWITCH_MASK (BIT(29) | BIT(28) | BIT(27))
120 #define CC_CODE_MASK (0xfffff << 7)
121 #define rp4pk_code(val) ((0x1f & (val)) << 22)
122 #define code_rp4pk(val) (((val) >> 22) & 0x1f)
123 #define rp36k_code(val) ((0x1f & (val)) << 17)
124 #define code_rp36k(val) (((val) >> 17) & 0x1f)
125 #define rp12k_code(val) ((0x1f & (val)) << 12)
126 #define code_rp12k(val) (((val) >> 12) & 0x1f)
127 #define rd_code(val) ((0x1f & (val)) << 7)
128 #define code_rd(val) (((val) >> 7) & 0x1f)
129 #define dfp_mode(val) ((0x3 & (val)) << 5)
130 #define EN_RP4P7K BIT(4)
131 #define EN_RP36K BIT(3)
132 #define EN_RP12K BIT(2)
134 #define EN_CC_DET BIT(0)
136 #define CC_MODE_UFP 0x0
137 #define CC_MODE_DFP_USB 0x1
138 #define CC_MODE_DFP_1_5 0x2
139 #define CC_MODE_DFP_3_0 0x3
143 * Realtek Kylin rtd1295
144 * Realtek Hercules rtd1395
145 * Realtek Thor rtd1619
146 * Realtek Hank rtd1319
147 * Realtek Groot rtd1312c
149 * Realtek Stark rtd1619b
150 * Realtek Parker rtd1319d
151 * Realtek Danvers rtd1315e
153 enum parameter_version
{
158 /* Bit mapping USB_TYPEC_CTRL_CC1_1 and USB_TYPEC_CTRL_CC2_1 */
159 #define V0_vref_2p6v(val) ((0xf & (val)) << 26) /* Bit 29 for groot */
160 #define V0_vref_1p23v(val) ((0xf & (val)) << 22)
161 #define V0_vref_0p8v(val) ((0xf & (val)) << 18)
162 #define V0_vref_0p66v(val) ((0xf & (val)) << 14)
163 #define V0_vref_0p4v(val) ((0x7 & (val)) << 11)
164 #define V0_vref_0p2v(val) ((0x7 & (val)) << 8)
165 #define V0_vref_1_1p6v(val) ((0xf & (val)) << 4)
166 #define V0_vref_0_1p6v(val) ((0xf & (val)) << 0)
168 #define V0_decode_2p6v(val) (((val) >> 26) & 0xf) /* Bit 29 for groot */
169 #define V0_decode_1p23v(val) (((val) >> 22) & 0xf)
170 #define V0_decode_0p8v(val) (((val) >> 18) & 0xf)
171 #define V0_decode_0p66v(val) (((val) >> 14) & 0xf)
172 #define V0_decode_0p4v(val) (((val) >> 11) & 0x7)
173 #define V0_decode_0p2v(val) (((val) >> 8) & 0x7)
174 #define V0_decode_1_1p6v(val) (((val) >> 4) & 0xf)
175 #define V0_decode_0_1p6v(val) (((val) >> 0) & 0xf)
177 /* new Bit mapping USB_TYPEC_CTRL_CC1_1 and USB_TYPEC_CTRL_CC2_1 */
178 #define V1_vref_2p6v(val) ((0xf & (val)) << 28)
179 #define V1_vref_1p23v(val) ((0xf & (val)) << 24)
180 #define V1_vref_0p8v(val) ((0xf & (val)) << 20)
181 #define V1_vref_0p66v(val) ((0xf & (val)) << 16)
182 #define V1_vref_0p4v(val) ((0xf & (val)) << 12)
183 #define V1_vref_0p2v(val) ((0xf & (val)) << 8)
184 #define V1_vref_1_1p6v(val) ((0xf & (val)) << 4)
185 #define V1_vref_0_1p6v(val) ((0xf & (val)) << 0)
187 #define V1_decode_2p6v(val) (((val) >> 28) & 0xf)
188 #define V1_decode_1p23v(val) (((val) >> 24) & 0xf)
189 #define V1_decode_0p8v(val) (((val) >> 20) & 0xf)
190 #define V1_decode_0p66v(val) (((val) >> 16) & 0xf)
191 #define V1_decode_0p4v(val) (((val) >> 12) & 0xf)
192 #define V1_decode_0p2v(val) (((val) >> 8) & 0xf)
193 #define V1_decode_1_1p6v(val) (((val) >> 4) & 0xf)
194 #define V1_decode_0_1p6v(val) (((val) >> 0) & 0xf)
196 /* Bit mapping USB_TYPEC_STS */
198 #define CC1_DET_STS (DET_STS)
199 #define CC2_DET_STS (DET_STS << 3)
200 #define DET_STS_RA 0x1
201 #define DET_STS_RD 0x3
202 #define DET_STS_RP 0x1
203 #define CC1_DET_STS_RA (DET_STS_RA)
204 #define CC1_DET_STS_RD (DET_STS_RD)
205 #define CC1_DET_STS_RP (DET_STS_RP)
206 #define CC2_DET_STS_RA (DET_STS_RA << 3)
207 #define CC2_DET_STS_RD (DET_STS_RD << 3)
208 #define CC2_DET_STS_RP (DET_STS_RP << 3)
210 /* Bit mapping USB_TYPEC_CTRL */
211 #define CC2_INT_EN BIT(11)
212 #define CC1_INT_EN BIT(10)
213 #define CC2_INT_STS BIT(9)
214 #define CC1_INT_STS BIT(8)
215 #define DEBOUNCE_TIME_MASK 0xff
216 #define DEBOUNCE_EN BIT(0)
217 #define ENABLE_TYPE_C_DETECT (CC1_INT_EN | CC2_INT_EN)
218 #define ALL_CC_INT_STS (CC1_INT_STS | CC2_INT_STS)
221 #define DETECT_TIME 50 /* ms */
223 static const unsigned int usb_type_c_cable
[] = {
229 enum usb_data_roles
{
235 static const struct soc_device_attribute rtk_soc_kylin
[] = {
236 { .family
= "Realtek Kylin", },
240 static int rtd129x_switch_type_c_plug_config(struct type_c_data
*type_c
,
243 void __iomem
*reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC1_0
;
246 #define TYPE_C_EN_SWITCH BIT(29)
247 #define TYPE_C_TXRX_SEL (BIT(28) | BIT(27))
248 #define TYPE_C_SWITCH_MASK (TYPE_C_EN_SWITCH | TYPE_C_TXRX_SEL)
249 #define TYPE_C_ENABLE_CC1 TYPE_C_EN_SWITCH
250 #define TYPE_C_ENABLE_CC2 (TYPE_C_EN_SWITCH | TYPE_C_TXRX_SEL)
251 #define TYPE_C_DISABLE_CC ~TYPE_C_SWITCH_MASK
254 val_cc
&= ~TYPE_C_SWITCH_MASK
;
256 if (cc
== DISABLE_CC
) {
257 val_cc
&= TYPE_C_DISABLE_CC
;
258 } else if (cc
== ENABLE_CC1
) {
259 val_cc
|= TYPE_C_ENABLE_CC1
;
260 } else if (cc
== ENABLE_CC2
) {
261 val_cc
|= TYPE_C_ENABLE_CC2
;
263 dev_err(type_c
->dev
, "%s: Error cc setting cc=0x%x\n", __func__
, cc
);
268 /* waiting cc stable for enable/disable */
271 dev_dbg(type_c
->dev
, "%s: cc=0x%x val_cc=0x%x usb_typec_ctrl_cc1_0=0x%x\n",
272 __func__
, cc
, val_cc
, readl(reg
));
277 static inline void switch_type_c_plug_config(struct type_c_data
*type_c
,
282 if (soc_device_match(rtk_soc_kylin
))
283 ret
= rtd129x_switch_type_c_plug_config(type_c
, dr_mode
, cc
);
286 dev_err(type_c
->dev
, "%s: Error set type c plug config\n",
290 static void switch_type_c_dr_mode(struct type_c_data
*type_c
, int dr_mode
, int cc
)
292 bool is_host
= false;
293 bool is_device
= false;
294 bool polarity
= false;
298 switch_type_c_plug_config(type_c
, dr_mode
, cc
);
299 if (cc
== ENABLE_CC2
)
303 case USB_DR_MODE_HOST
:
306 case USB_DR_MODE_PERIPHERAL
:
311 dev_dbg(type_c
->dev
, "%s dr_mode=%d ==> no host or device\n",
316 dev_dbg(type_c
->dev
, "%s is_host=%d is_device=%d vbus=%d polarity=%d\n",
317 __func__
, is_host
, is_device
, vbus
, polarity
);
319 /* for EXTCON_USB device mode */
320 extcon_set_state(type_c
->edev
, EXTCON_USB
, is_device
);
321 extcon_set_property(type_c
->edev
, EXTCON_USB
,
322 EXTCON_PROP_USB_VBUS
,
323 (union extcon_property_value
)(int)vbus
);
324 extcon_set_property(type_c
->edev
, EXTCON_USB
,
325 EXTCON_PROP_USB_TYPEC_POLARITY
,
326 (union extcon_property_value
)(int)polarity
);
327 extcon_set_property(type_c
->edev
, EXTCON_USB
,
329 (union extcon_property_value
)(int)ss
);
331 /* for EXTCON_USB_HOST host mode */
332 extcon_set_state(type_c
->edev
, EXTCON_USB_HOST
, is_host
);
333 extcon_set_property(type_c
->edev
, EXTCON_USB_HOST
,
334 EXTCON_PROP_USB_VBUS
,
335 (union extcon_property_value
)(int)vbus
);
336 extcon_set_property(type_c
->edev
, EXTCON_USB_HOST
,
337 EXTCON_PROP_USB_TYPEC_POLARITY
,
338 (union extcon_property_value
)(int)polarity
);
339 extcon_set_property(type_c
->edev
, EXTCON_USB_HOST
,
341 (union extcon_property_value
)(int)ss
);
343 /* sync EXTCON_USB and EXTCON_USB_HOST */
344 extcon_sync(type_c
->edev
, EXTCON_USB
);
345 extcon_sync(type_c
->edev
, EXTCON_USB_HOST
);
349 case USB_DR_MODE_HOST
:
350 typec_set_data_role(type_c
->port
, TYPEC_HOST
);
351 typec_set_pwr_role(type_c
->port
, TYPEC_SOURCE
);
353 case USB_DR_MODE_PERIPHERAL
:
354 typec_set_data_role(type_c
->port
, TYPEC_DEVICE
);
355 typec_set_pwr_role(type_c
->port
, TYPEC_SINK
);
358 dev_dbg(type_c
->dev
, "%s unknown dr_mode=%d\n",
365 /* connector attached/detached */
366 static int connector_attached(struct type_c_data
*type_c
, u32 cc
, int dr_mode
)
368 void __iomem
*reg
= type_c
->reg_base
+ USB_TYPEC_CTRL
;
370 cancel_delayed_work(&type_c
->delayed_work
);
372 switch_type_c_dr_mode(type_c
, dr_mode
, cc
);
374 writel(ENABLE_TYPE_C_DETECT
| readl(reg
), reg
);
379 static int connector_detached(struct type_c_data
*type_c
, u32 cc
, int dr_mode
)
381 void __iomem
*reg
= type_c
->reg_base
+ USB_TYPEC_CTRL
;
383 writel(~ENABLE_TYPE_C_DETECT
& readl(reg
), reg
);
385 switch_type_c_dr_mode(type_c
, 0, cc
);
387 schedule_delayed_work(&type_c
->delayed_work
, msecs_to_jiffies(DETECT_TIME
));
392 /* detect host device switch */
393 static int __detect_host_device(struct type_c_data
*type_c
, u32 rp_or_rd_en
)
395 struct device
*dev
= type_c
->dev
;
396 void __iomem
*reg_base
= type_c
->reg_base
;
397 u32 cc1_config
, cc2_config
, default_ctrl
;
400 default_ctrl
= readl(reg_base
+ USB_TYPEC_CTRL
) & DEBOUNCE_TIME_MASK
;
401 writel(default_ctrl
, reg_base
+ USB_TYPEC_CTRL
);
403 cc1_config
= readl(reg_base
+ USB_TYPEC_CTRL_CC1_0
);
404 cc2_config
= readl(reg_base
+ USB_TYPEC_CTRL_CC2_0
);
406 cc1_config
&= ~EN_CC_DET
;
407 cc2_config
&= ~EN_CC_DET
;
408 writel(cc1_config
, reg_base
+ USB_TYPEC_CTRL_CC1_0
);
409 writel(cc2_config
, reg_base
+ USB_TYPEC_CTRL_CC2_0
);
411 if (soc_device_match(rtk_soc_kylin
))
412 cc1_switch
= cc1_config
& CC_SWITCH_MASK
;
414 cc1_config
&= CC_CODE_MASK
;
415 cc1_config
|= rp_or_rd_en
| cc1_switch
;
416 cc2_config
&= CC_CODE_MASK
;
417 cc2_config
|= rp_or_rd_en
;
418 writel(cc2_config
, reg_base
+ USB_TYPEC_CTRL_CC2_0
);
419 writel(cc1_config
, reg_base
+ USB_TYPEC_CTRL_CC1_0
);
421 /* For kylin to disable external rd control gpio */
422 if (soc_device_match(rtk_soc_kylin
)) {
423 struct gpio_desc
*gpio
= type_c
->rd_ctrl_gpio_desc
;
425 if (gpio
&& gpiod_direction_output(gpio
, 1))
426 dev_err(dev
, "%s ERROR set rd_ctrl_gpio_desc fail\n", __func__
);
429 cc1_config
|= EN_CC_DET
;
430 cc2_config
|= EN_CC_DET
;
431 writel(cc1_config
, reg_base
+ USB_TYPEC_CTRL_CC1_0
);
432 writel(cc2_config
, reg_base
+ USB_TYPEC_CTRL_CC2_0
);
437 static int detect_device(struct type_c_data
*type_c
)
439 return __detect_host_device(type_c
, type_c
->dfp_mode_rp_en
);
442 static int detect_host(struct type_c_data
*type_c
)
444 return __detect_host_device(type_c
, type_c
->ufp_mode_rd_en
);
447 static int host_device_switch_detection(struct type_c_data
*type_c
)
449 if (type_c
->cc_mode
== IN_HOST_MODE
) {
450 type_c
->cc_mode
= IN_DEVICE_MODE
;
453 type_c
->cc_mode
= IN_HOST_MODE
;
454 detect_device(type_c
);
460 static int detect_type_c_state(struct type_c_data
*type_c
)
462 struct device
*dev
= type_c
->dev
;
463 void __iomem
*reg_base
= type_c
->reg_base
;
464 u32 int_status
, cc_status
, cc_status_check
;
467 spin_lock_irqsave(&type_c
->lock
, flags
);
469 int_status
= readl(reg_base
+ USB_TYPEC_CTRL
);
470 cc_status
= readl(reg_base
+ USB_TYPEC_STS
);
472 type_c
->connect_change
= CONNECT_NO_CHANGE
;
474 switch (type_c
->cc_mode
| type_c
->is_attach
) {
475 case IN_HOST_MODE
| IN_ATTACH
:
476 if (((cc_status
& CC1_DET_STS
) == CC1_DET_STS
) && type_c
->at_cc1
== AT_CC1
) {
477 dev_dbg(dev
, "IN host mode and cc1 device detach (cc_status=0x%x)",
479 type_c
->is_attach
= TO_DETACH
;
480 type_c
->connect_change
= CONNECT_CHANGE
;
481 } else if (((cc_status
& CC2_DET_STS
) == CC2_DET_STS
) &&
482 type_c
->at_cc1
== AT_CC2
) {
483 dev_dbg(dev
, "IN host mode and cc2 device detach (cc_status=0x%x)",
485 type_c
->is_attach
= TO_DETACH
;
486 type_c
->connect_change
= CONNECT_CHANGE
;
489 case IN_HOST_MODE
| IN_DETACH
:
490 cc_status_check
= readl(reg_base
+ USB_TYPEC_STS
);
491 if (cc_status_check
!= (CC1_DET_STS
| CC2_DET_STS
)) {
492 if (in_interrupt()) {
493 /* Add delay time to avoid capacitive effect of cable. */
496 spin_unlock_irqrestore(&type_c
->lock
, flags
);
497 /* Add delay time to avoid capacitive effect of cable. */
499 spin_lock_irqsave(&type_c
->lock
, flags
);
501 cc_status_check
= readl(reg_base
+ USB_TYPEC_STS
);
503 if (cc_status
!= cc_status_check
) {
504 dev_warn(dev
, "IN_HOST_MODE: cc_status (0x%x) != cc_status_check (0x%x)\n",
505 cc_status
, cc_status_check
);
506 cc_status
= readl(reg_base
+ USB_TYPEC_STS
);
509 if ((cc_status
& CC1_DET_STS
) == CC1_DET_STS_RD
) {
510 dev_dbg(dev
, "IN host mode and cc1 device attach (cc_status=0x%x)",
512 type_c
->is_attach
= TO_ATTACH
;
513 type_c
->at_cc1
= AT_CC1
;
514 type_c
->connect_change
= CONNECT_CHANGE
;
515 } else if ((cc_status
& CC2_DET_STS
) == CC2_DET_STS_RD
) {
516 dev_dbg(dev
, "In host mode and cc2 device attach (cc_status=0x%x)",
518 type_c
->is_attach
= TO_ATTACH
;
519 type_c
->at_cc1
= AT_CC2
;
520 type_c
->connect_change
= CONNECT_CHANGE
;
523 case IN_DEVICE_MODE
| IN_ATTACH
:
524 if ((cc_status
& CC1_DET_STS
) < CC1_DET_STS_RP
||
525 (cc_status
& CC2_DET_STS
) < CC2_DET_STS_RP
) {
526 /* Add a sw debounce to filter cc signal sent from apple pd adapter */
528 cc_status_check
= readl(reg_base
+ USB_TYPEC_STS
);
530 if (cc_status
!= cc_status_check
) {
531 dev_dbg(dev
, "IN_DEVICE_MODE: cc_status (0x%x) != cc_status_check (0x%x) maybe use a pd adapter\n",
532 cc_status
, cc_status_check
);
533 cc_status
= cc_status_check
;
537 if ((cc_status
& CC1_DET_STS
) < CC1_DET_STS_RP
&& type_c
->at_cc1
== AT_CC1
) {
538 dev_dbg(dev
, "IN device mode and cc1 host disconnect (cc_status=0x%x)",
540 type_c
->is_attach
= TO_DETACH
;
541 type_c
->connect_change
= CONNECT_CHANGE
;
542 } else if ((cc_status
& CC2_DET_STS
) < CC2_DET_STS_RP
&&
543 type_c
->at_cc1
== AT_CC2
) {
544 dev_dbg(dev
, "IN device mode and cc2 host disconnect (cc_status=0x%x)",
546 type_c
->is_attach
= TO_DETACH
;
547 type_c
->connect_change
= CONNECT_CHANGE
;
550 case IN_DEVICE_MODE
| IN_DETACH
:
551 cc_status_check
= readl(reg_base
+ USB_TYPEC_STS
);
552 if (cc_status_check
!= 0x0) {
553 if (in_interrupt()) {
554 /* Add delay time to avoid capacitive effect of cable. */
557 spin_unlock_irqrestore(&type_c
->lock
, flags
);
558 /* Add delay time to avoid capacitive effect of cable. */
560 spin_lock_irqsave(&type_c
->lock
, flags
);
562 cc_status_check
= readl(reg_base
+ USB_TYPEC_STS
);
565 if (cc_status
!= cc_status_check
) {
566 dev_warn(dev
, "IN_DEVICE_MODE: cc_status (0x%x) != cc_status_check (0x%x)\n",
567 cc_status
, cc_status_check
);
568 cc_status
= readl(reg_base
+ USB_TYPEC_STS
);
571 if ((cc_status
& CC1_DET_STS
) >= CC1_DET_STS_RP
) {
572 dev_dbg(dev
, "IN device mode and cc1 host connect (cc_status=0x%x)",
574 type_c
->at_cc1
= AT_CC1
;
575 type_c
->is_attach
= TO_ATTACH
;
576 type_c
->connect_change
= CONNECT_CHANGE
;
577 } else if ((cc_status
& CC2_DET_STS
) >= CC2_DET_STS_RP
) {
578 dev_dbg(dev
, "IN device mode and cc2 host connect (cc_status=0x%x)",
580 type_c
->at_cc1
= AT_CC2
;
581 type_c
->is_attach
= TO_ATTACH
;
582 type_c
->connect_change
= CONNECT_CHANGE
;
586 dev_err(dev
, "error host or device mode (cc_mode=%d, is_attach=%d) ",
587 type_c
->cc_mode
, type_c
->is_attach
);
590 type_c
->int_status
= int_status
;
591 type_c
->cc_status
= cc_status
;
593 spin_unlock_irqrestore(&type_c
->lock
, flags
);
597 static void host_device_switch(struct work_struct
*work
)
599 struct type_c_data
*type_c
= container_of(work
, struct type_c_data
,
601 struct device
*dev
= type_c
->dev
;
603 int connect_change
= 0;
608 spin_lock_irqsave(&type_c
->lock
, flags
);
609 if (type_c
->connect_change
)
610 connect_change
= type_c
->connect_change
;
611 spin_unlock_irqrestore(&type_c
->lock
, flags
);
614 detect_type_c_state(type_c
);
616 spin_lock_irqsave(&type_c
->lock
, flags
);
617 if (type_c
->connect_change
) {
618 connect_change
= type_c
->connect_change
;
619 cc_mode
= type_c
->cc_mode
;
620 is_attach
= type_c
->is_attach
;
621 at_cc1
= type_c
->at_cc1
;
622 type_c
->connect_change
= CONNECT_NO_CHANGE
;
624 host_device_switch_detection(type_c
);
626 schedule_delayed_work(&type_c
->delayed_work
, msecs_to_jiffies(DETECT_TIME
));
628 spin_unlock_irqrestore(&type_c
->lock
, flags
);
633 dev_dbg(dev
, "%s: usb cable connection change\n", __func__
);
634 if (cc_mode
== IN_HOST_MODE
) {
635 if (is_attach
&& at_cc1
)
636 connector_attached(type_c
, ENABLE_CC1
, USB_DR_MODE_HOST
);
637 else if (is_attach
&& !at_cc1
)
638 connector_attached(type_c
, ENABLE_CC2
, USB_DR_MODE_HOST
);
640 connector_detached(type_c
, DISABLE_CC
, USB_DR_MODE_HOST
);
641 } else if (cc_mode
== IN_DEVICE_MODE
) {
642 if (is_attach
&& at_cc1
)
643 connector_attached(type_c
, ENABLE_CC1
, USB_DR_MODE_PERIPHERAL
);
644 else if (is_attach
&& !at_cc1
)
645 connector_attached(type_c
, ENABLE_CC2
, USB_DR_MODE_PERIPHERAL
);
647 connector_detached(type_c
, DISABLE_CC
, USB_DR_MODE_PERIPHERAL
);
649 dev_err(dev
, "Error: IN unknown mode %d to %s at %s (cc_status=0x%x)\n",
650 cc_mode
, is_attach
? "attach" : "detach",
651 at_cc1
? "cc1" : "cc2", type_c
->cc_status
);
653 dev_info(dev
, "Connection change OK: IN %s mode to %s at %s (cc_status=0x%x)\n",
654 cc_mode
== IN_HOST_MODE
? "host" : "device",
655 is_attach
? "attach" : "detach",
656 at_cc1
? "cc1" : "cc2", type_c
->cc_status
);
659 static irqreturn_t
type_c_detect_irq(int irq
, void *__data
)
661 struct type_c_data
*type_c
= (struct type_c_data
*)__data
;
662 struct device
*dev
= type_c
->dev
;
663 void __iomem
*reg
= type_c
->reg_base
+ USB_TYPEC_CTRL
;
666 detect_type_c_state(type_c
);
668 spin_lock_irqsave(&type_c
->lock
, flags
);
670 if (type_c
->connect_change
) {
671 dev_dbg(dev
, "%s: IN %s mode to %s (at %s interrupt) int_status=0x%x, cc_status=0x%x",
673 type_c
->cc_mode
== IN_HOST_MODE
? "host" : "device",
674 type_c
->is_attach
? "attach" : "detach",
675 type_c
->at_cc1
? "cc1" : "cc2",
676 type_c
->int_status
, type_c
->cc_status
);
678 /* clear interrupt status */
679 writel(~ALL_CC_INT_STS
& readl(reg
), reg
);
681 cancel_delayed_work(&type_c
->delayed_work
);
682 schedule_delayed_work(&type_c
->delayed_work
, msecs_to_jiffies(0));
684 static int local_count
;
686 /* if no connect_change, we keep the status to avoid status lose */
687 if (local_count
++ > 10) {
688 /* clear interrupt status */
689 writel(~ALL_CC_INT_STS
& readl(reg
), reg
);
694 spin_unlock_irqrestore(&type_c
->lock
, flags
);
699 static int type_c_port_dr_set(struct typec_port
*port
,
700 enum typec_data_role role
)
702 struct type_c_data
*type_c
= typec_get_drvdata(port
);
706 spin_lock_irqsave(&type_c
->lock
, flags
);
707 enable_cc
= type_c
->at_cc1
? ENABLE_CC1
: ENABLE_CC2
;
708 spin_unlock_irqrestore(&type_c
->lock
, flags
);
710 if (role
== TYPEC_HOST
)
711 switch_type_c_dr_mode(type_c
, USB_DR_MODE_HOST
, enable_cc
);
712 else if (role
== TYPEC_DEVICE
)
713 switch_type_c_dr_mode(type_c
, USB_DR_MODE_PERIPHERAL
, enable_cc
);
715 switch_type_c_dr_mode(type_c
, 0, DISABLE_CC
);
720 static const struct typec_operations type_c_port_ops
= {
721 .dr_set
= type_c_port_dr_set
,
724 #ifdef CONFIG_DEBUG_FS
725 static int type_c_parameter_show(struct seq_file
*s
, void *unused
)
727 struct type_c_data
*type_c
= s
->private;
728 struct type_c_cfg
*type_c_cfg
= type_c
->type_c_cfg
;
729 struct cc_param
*cc_param
;
732 spin_lock_irqsave(&type_c
->lock
, flags
);
734 seq_printf(s
, "cc_dfp_mode %s\n",
736 switch (type_c_cfg
->cc_dfp_mode
) {
737 case CC_MODE_DFP_USB
:
738 tmp
= "CC_MODE_DFP_USB"; break;
739 case CC_MODE_DFP_1_5
:
740 tmp
= "CC_MODE_DFP_1_5"; break;
741 case CC_MODE_DFP_3_0
:
742 tmp
= "CC_MODE_DFP_3_0"; break;
747 seq_printf(s
, "dfp_mode_rp_en 0x%x\n", type_c
->dfp_mode_rp_en
);
748 seq_printf(s
, "ufp_mode_rd_en 0x%x\n", type_c
->ufp_mode_rd_en
);
749 seq_printf(s
, "cc1_code 0x%x\n", type_c
->cc1_code
);
750 seq_printf(s
, "cc2_code 0x%x\n", type_c
->cc2_code
);
751 seq_printf(s
, "cc1_vref 0x%x\n", type_c
->cc1_vref
);
752 seq_printf(s
, "cc2_vref 0x%x\n", type_c
->cc2_vref
);
753 seq_printf(s
, "debounce 0x%x\n", type_c
->debounce
);
756 cc_param
= &type_c_cfg
->cc1_param
;
757 seq_puts(s
, "cc1_param:\n");
758 seq_printf(s
, " rp_4p7k_code 0x%x\n", cc_param
->rp_4p7k_code
);
759 seq_printf(s
, " rp_36k_code 0x%x\n", cc_param
->rp_36k_code
);
760 seq_printf(s
, " rp_12k_code 0x%x\n", cc_param
->rp_12k_code
);
761 seq_printf(s
, " rd_code 0x%x\n", cc_param
->rd_code
);
762 seq_printf(s
, " vref_2p6v 0x%x\n", cc_param
->vref_2p6v
);
763 seq_printf(s
, " vref_1p23v 0x%x\n", cc_param
->vref_1p23v
);
764 seq_printf(s
, " vref_0p8v 0x%x\n", cc_param
->vref_0p8v
);
765 seq_printf(s
, " vref_0p66v 0x%x\n", cc_param
->vref_0p66v
);
766 seq_printf(s
, " vref_0p4v 0x%x\n", cc_param
->vref_0p4v
);
767 seq_printf(s
, " vref_0p2v 0x%x\n", cc_param
->vref_0p2v
);
768 seq_printf(s
, " vref_1_1p6v 0x%x\n", cc_param
->vref_1_1p6v
);
769 seq_printf(s
, " vref_0_1p6v 0x%x\n", cc_param
->vref_0_1p6v
);
771 cc_param
= &type_c_cfg
->cc2_param
;
772 seq_puts(s
, "cc2_param:\n");
773 seq_printf(s
, " rp_4p7k_code 0x%x\n", cc_param
->rp_4p7k_code
);
774 seq_printf(s
, " rp_36k_code 0x%x\n", cc_param
->rp_36k_code
);
775 seq_printf(s
, " rp_12k_code 0x%x\n", cc_param
->rp_12k_code
);
776 seq_printf(s
, " rd_code 0x%x\n", cc_param
->rd_code
);
777 seq_printf(s
, " vref_2p6v 0x%x\n", cc_param
->vref_2p6v
);
778 seq_printf(s
, " vref_1p23v 0x%x\n", cc_param
->vref_1p23v
);
779 seq_printf(s
, " vref_0p8v 0x%x\n", cc_param
->vref_0p8v
);
780 seq_printf(s
, " vref_0p66v 0x%x\n", cc_param
->vref_0p66v
);
781 seq_printf(s
, " vref_0p4v 0x%x\n", cc_param
->vref_0p4v
);
782 seq_printf(s
, " vref_0p2v 0x%x\n", cc_param
->vref_0p2v
);
783 seq_printf(s
, " vref_1_1p6v 0x%x\n", cc_param
->vref_1_1p6v
);
784 seq_printf(s
, " vref_0_1p6v 0x%x\n", cc_param
->vref_0_1p6v
);
786 spin_unlock_irqrestore(&type_c
->lock
, flags
);
791 static int type_c_parameter_open(struct inode
*inode
, struct file
*file
)
793 return single_open(file
, type_c_parameter_show
, inode
->i_private
);
796 static const struct file_operations type_c_parameter_fops
= {
797 .open
= type_c_parameter_open
,
800 .release
= single_release
,
803 static int type_c_status_show(struct seq_file
*s
, void *unused
)
805 struct type_c_data
*type_c
= s
->private;
808 spin_lock_irqsave(&type_c
->lock
, flags
);
810 seq_printf(s
, "In %s mode %s at %s (cc_status=0x%x)\n",
811 type_c
->cc_mode
== IN_HOST_MODE
? "host" : "device",
812 type_c
->is_attach
? "attach" : "detach",
813 type_c
->at_cc1
? "cc1" : "cc2", type_c
->cc_status
);
815 seq_printf(s
, "Read Register (type_c_ctrl_cc1_0=0x%x)\n",
816 readl(type_c
->reg_base
+ 0x0));
817 seq_printf(s
, "Read Register (type_c_ctrl_cc1_1=0x%x)\n",
818 readl(type_c
->reg_base
+ 0x4));
819 seq_printf(s
, "Read Register (type_c_ctrl_cc2_0=0x%x)\n",
820 readl(type_c
->reg_base
+ 0x8));
821 seq_printf(s
, "Read Register (type_c_ctrl_cc2_1=0x%x)\n",
822 readl(type_c
->reg_base
+ 0xc));
823 seq_printf(s
, "Read Register (type_c_status=0x%x)\n",
824 readl(type_c
->reg_base
+ 0x10));
825 seq_printf(s
, "Read Register (type_c_ctrl=0x%x)\n",
826 readl(type_c
->reg_base
+ 0x14));
828 spin_unlock_irqrestore(&type_c
->lock
, flags
);
833 static int type_c_status_open(struct inode
*inode
, struct file
*file
)
835 return single_open(file
, type_c_status_show
, inode
->i_private
);
838 static const struct file_operations type_c_status_fops
= {
839 .open
= type_c_status_open
,
842 .release
= single_release
,
845 static inline void create_debug_files(struct type_c_data
*type_c
)
847 type_c
->debug_dir
= debugfs_create_dir("type_c", usb_debug_root
);
849 debugfs_create_file("parameter", 0444, type_c
->debug_dir
, type_c
,
850 &type_c_parameter_fops
);
852 debugfs_create_file("status", 0444, type_c
->debug_dir
, type_c
,
853 &type_c_status_fops
);
856 static inline void remove_debug_files(struct type_c_data
*type_c
)
858 debugfs_remove_recursive(type_c
->debug_dir
);
861 static inline void create_debug_files(struct type_c_data
*type_c
) { }
862 static inline void remove_debug_files(struct type_c_data
*type_c
) { }
863 #endif /* CONFIG_DEBUG_FS */
867 static inline s8
get_value(s8 value
)
869 return (((s8
)value
& 0x8) ? (-(s8
)(0x7 & value
)) : ((s8
)(value
)));
872 static int __updated_type_c_parameter_by_efuse(struct type_c_data
*type_c
)
874 struct type_c_cfg
*type_c_cfg
= type_c
->type_c_cfg
;
875 struct cc_param
*cc_param
;
876 struct nvmem_cell
*cell
;
892 cell
= nvmem_cell_get(type_c
->dev
, "usb-cal");
894 dev_warn(type_c
->dev
, "%s failed to get usb-cal: %ld\n",
895 __func__
, PTR_ERR(cell
));
900 int value_mask
= (BIT(value_size
) - 1);
902 buf
= nvmem_cell_read(cell
, &buf_size
);
904 cc1_0p2v
= get_value((buf
[0] >> value_size
* 0) & value_mask
);
905 cc1_0p8v
= get_value((buf
[0] >> value_size
* 1) & value_mask
);
906 cc1_2p6v
= get_value((buf
[1] >> value_size
* 0) & value_mask
);
907 cc1_0p66v
= get_value((buf
[1] >> value_size
* 1) & value_mask
);
908 cc1_1p23v
= get_value((buf
[2] >> value_size
* 0) & value_mask
);
910 cc2_0p2v
= get_value((buf
[3] >> value_size
* 0) & value_mask
);
911 cc2_0p8v
= get_value((buf
[3] >> value_size
* 1) & value_mask
);
912 cc2_2p6v
= get_value((buf
[4] >> value_size
* 0) & value_mask
);
913 cc2_0p66v
= get_value((buf
[4] >> value_size
* 1) & value_mask
);
914 cc2_1p23v
= get_value((buf
[5] >> value_size
* 0) & value_mask
);
916 cc1_4p7k
= get_value((buf
[6] >> value_size
* 0) & value_mask
);
917 cc1_12k
= get_value((buf
[6] >> value_size
* 1) & value_mask
);
918 cc2_4p7k
= get_value((buf
[7] >> value_size
* 0) & value_mask
);
919 cc2_12k
= get_value((buf
[7] >> value_size
* 1) & value_mask
);
923 nvmem_cell_put(cell
);
926 dev_dbg(type_c
->dev
, "check efuse cc1_4p7k=%d cc1_12k=%d cc2_4p7k=%d cc2_12k=%d\n",
927 cc1_4p7k
, cc1_12k
, cc2_4p7k
, cc2_12k
);
928 dev_dbg(type_c
->dev
, "check efuse cc1_0p2v=%d cc1_0p8v=%d cc1_2p6v=%d cc1_0p66v=%d cc1_1p23v=%d\n",
929 cc1_0p2v
, cc1_0p8v
, cc1_2p6v
, cc1_0p66v
, cc1_1p23v
);
930 dev_dbg(type_c
->dev
, "check efuse cc2_0p2v=%d cc2_0p8v=%d cc2_2p6v=%d cc2_0p66v=%d cc2_1p23v=%d\n",
931 cc2_0p2v
, cc2_0p8v
, cc2_2p6v
, cc2_0p66v
, cc2_1p23v
);
933 cc_param
= &type_c_cfg
->cc1_param
;
934 cc_param
->rp_4p7k_code
= cc_param
->rp_4p7k_code
+ cc1_4p7k
;
935 cc_param
->rp_12k_code
= cc_param
->rp_12k_code
+ cc1_12k
;
937 cc_param
->vref_1p23v
= cc_param
->vref_1p23v
+ cc1_1p23v
;
938 cc_param
->vref_0p66v
= cc_param
->vref_0p66v
+ cc1_0p66v
;
939 cc_param
->vref_2p6v
= cc_param
->vref_2p6v
+ cc1_2p6v
;
940 cc_param
->vref_0p8v
= cc_param
->vref_0p8v
+ cc1_0p8v
;
941 cc_param
->vref_0p2v
= cc_param
->vref_0p2v
+ cc1_0p2v
;
943 cc_param
= &type_c_cfg
->cc2_param
;
944 cc_param
->rp_4p7k_code
= cc_param
->rp_4p7k_code
+ cc2_4p7k
;
945 cc_param
->rp_12k_code
= cc_param
->rp_12k_code
+ cc2_12k
;
947 cc_param
->vref_1p23v
= cc_param
->vref_1p23v
+ cc2_1p23v
;
948 cc_param
->vref_0p66v
= cc_param
->vref_0p66v
+ cc2_0p66v
;
949 cc_param
->vref_2p6v
= cc_param
->vref_2p6v
+ cc2_2p6v
;
950 cc_param
->vref_0p8v
= cc_param
->vref_0p8v
+ cc2_0p8v
;
951 cc_param
->vref_0p2v
= cc_param
->vref_0p2v
+ cc2_0p2v
;
956 static int __updated_type_c_parameter_by_efuse_v2(struct type_c_data
*type_c
)
958 struct type_c_cfg
*type_c_cfg
= type_c
->type_c_cfg
;
959 struct cc_param
*cc_param
;
960 struct nvmem_cell
*cell
;
976 cell
= nvmem_cell_get(type_c
->dev
, "usb-type-c-cal");
978 dev_warn(type_c
->dev
, "%s failed to get usb-type-c-cal: %ld\n",
979 __func__
, PTR_ERR(cell
));
984 int value_mask
= (BIT(value_size
) - 1);
986 buf
= nvmem_cell_read(cell
, &buf_size
);
989 value_mask
= (BIT(value_size
) - 1);
990 cc1_4p7k
= buf
[0] & value_mask
;
991 cc1_12k
= buf
[1] & value_mask
;
992 cc2_4p7k
= buf
[2] & value_mask
;
993 cc2_12k
= buf
[3] & value_mask
;
996 value_mask
= (BIT(value_size
) - 1);
997 cc1_0p2v
= (buf
[4] >> value_size
* 0) & value_mask
;
998 cc1_0p66v
= (buf
[4] >> value_size
* 1) & value_mask
;
999 cc1_0p8v
= (buf
[5] >> value_size
* 0) & value_mask
;
1000 cc1_1p23v
= (buf
[5] >> value_size
* 1) & value_mask
;
1001 cc1_2p6v
= (buf
[6] >> value_size
* 0) & value_mask
;
1003 cc2_0p2v
= (buf
[6] >> value_size
* 1) & value_mask
;
1004 cc2_0p66v
= (buf
[7] >> value_size
* 0) & value_mask
;
1005 cc2_0p8v
= (buf
[7] >> value_size
* 1) & value_mask
;
1006 cc2_1p23v
= (buf
[8] >> value_size
* 0) & value_mask
;
1007 cc2_2p6v
= (buf
[8] >> value_size
* 1) & value_mask
;
1011 nvmem_cell_put(cell
);
1014 dev_dbg(type_c
->dev
, "check efuse v2 cc1_4p7k=%d cc1_12k=%d cc2_4p7k=%d cc2_12k=%d\n",
1015 cc1_4p7k
, cc1_12k
, cc2_4p7k
, cc2_12k
);
1016 dev_dbg(type_c
->dev
, "check efuse v2 cc1_0p2v=%d cc1_0p8v=%d cc1_2p6v=%d cc1_0p66v=%d cc1_1p23v=%d\n",
1017 cc1_0p2v
, cc1_0p8v
, cc1_2p6v
, cc1_0p66v
, cc1_1p23v
);
1018 dev_dbg(type_c
->dev
, "check efuse v2 cc2_0p2v=%d cc2_0p8v=%d cc2_2p6v=%d cc2_0p66v=%d cc2_1p23v=%d\n",
1019 cc2_0p2v
, cc2_0p8v
, cc2_2p6v
, cc2_0p66v
, cc2_1p23v
);
1021 cc_param
= &type_c_cfg
->cc1_param
;
1023 cc_param
->rp_4p7k_code
= cc1_4p7k
;
1025 cc_param
->rp_12k_code
= cc1_12k
;
1028 cc_param
->vref_1p23v
= cc1_1p23v
;
1030 cc_param
->vref_0p66v
= cc1_0p66v
;
1032 cc_param
->vref_2p6v
= cc1_2p6v
;
1034 cc_param
->vref_0p8v
= cc1_0p8v
;
1036 cc_param
->vref_0p2v
= cc1_0p2v
;
1038 cc_param
= &type_c_cfg
->cc2_param
;
1040 cc_param
->rp_4p7k_code
= cc2_4p7k
;
1042 cc_param
->rp_12k_code
= cc2_12k
;
1045 cc_param
->vref_1p23v
= cc2_1p23v
;
1047 cc_param
->vref_0p66v
= cc2_0p66v
;
1049 cc_param
->vref_2p6v
= cc2_2p6v
;
1051 cc_param
->vref_0p8v
= cc2_0p8v
;
1053 cc_param
->vref_0p2v
= cc2_0p2v
;
1058 static void get_default_type_c_parameter(struct type_c_data
*type_c
)
1063 type_c
->dfp_mode_rp_en
= dfp_mode(CC_MODE_DFP_3_0
) | EN_RP4P7K
;
1064 type_c
->ufp_mode_rd_en
= EN_RD
;
1066 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC1_0
;
1068 type_c
->cc1_code
= CC_CODE_MASK
& val
;
1070 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC2_0
;
1072 type_c
->cc2_code
= CC_CODE_MASK
& val
;
1074 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC1_1
;
1076 type_c
->cc1_vref
= val
;
1078 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC2_1
;
1080 type_c
->cc2_vref
= val
;
1082 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL
;
1084 type_c
->debounce
= DEBOUNCE_TIME_MASK
& val
;
1087 static int setup_type_c_parameter(struct type_c_data
*type_c
)
1089 struct type_c_cfg
*type_c_cfg
= type_c
->type_c_cfg
;
1090 struct cc_param
*cc_param
;
1091 struct soc_device_attribute rtk_soc_efuse_v1
[] = {
1092 { .family
= "Realtek Phoenix",},
1093 { .family
= "Realtek Kylin",},
1094 { .family
= "Realtek Hercules",},
1095 { .family
= "Realtek Thor",},
1096 { .family
= "Realtek Hank",},
1097 { .family
= "Realtek Groot",},
1098 { .family
= "Realtek Stark",},
1099 { .family
= "Realtek Parker",},
1103 if (type_c_cfg
->use_defalut_parameter
) {
1104 get_default_type_c_parameter(type_c
);
1108 if (soc_device_match(rtk_soc_efuse_v1
))
1109 __updated_type_c_parameter_by_efuse(type_c
);
1111 __updated_type_c_parameter_by_efuse_v2(type_c
);
1114 * UFP rd vref_ufp : 1p23v, 0p66v, 0p2v
1115 * DFP_USB rp36k vref_dfp_usb: 0_1p6v, 0p2v, unused
1116 * DFP_1.5 rp12k vref_dfp_1_5: 1_1p6v, 0p4v, 0p2v
1117 * DFP_3.0 rp4p7k vref_dfp_3_0: 2p6v, 0p8v, 0p2v
1120 switch (type_c_cfg
->cc_dfp_mode
) {
1121 case CC_MODE_DFP_USB
:
1122 type_c
->dfp_mode_rp_en
= dfp_mode(CC_MODE_DFP_USB
) | EN_RP36K
;
1124 case CC_MODE_DFP_1_5
:
1125 type_c
->dfp_mode_rp_en
= dfp_mode(CC_MODE_DFP_1_5
) | EN_RP12K
;
1127 case CC_MODE_DFP_3_0
:
1128 type_c
->dfp_mode_rp_en
= dfp_mode(CC_MODE_DFP_3_0
) | EN_RP4P7K
;
1131 dev_err(type_c
->dev
, "%s: unknown cc_dfp_mode %d\n",
1132 __func__
, type_c_cfg
->cc_dfp_mode
);
1135 type_c
->ufp_mode_rd_en
= EN_RD
;
1137 cc_param
= &type_c_cfg
->cc1_param
;
1138 type_c
->cc1_code
= rp4pk_code(cc_param
->rp_4p7k_code
) |
1139 rp36k_code(cc_param
->rp_36k_code
) |
1140 rp12k_code(cc_param
->rp_12k_code
) |
1141 rd_code(cc_param
->rd_code
);
1143 if (type_c_cfg
->parameter_ver
== PARAMETER_V0
)
1144 type_c
->cc1_vref
= V0_vref_2p6v(cc_param
->vref_2p6v
) |
1145 V0_vref_1p23v(cc_param
->vref_1p23v
) |
1146 V0_vref_0p8v(cc_param
->vref_0p8v
) |
1147 V0_vref_0p66v(cc_param
->vref_0p66v
) |
1148 V0_vref_0p4v(cc_param
->vref_0p4v
) |
1149 V0_vref_0p2v(cc_param
->vref_0p2v
) |
1150 V0_vref_1_1p6v(cc_param
->vref_1_1p6v
) |
1151 V0_vref_0_1p6v(cc_param
->vref_0_1p6v
);
1152 else if (type_c_cfg
->parameter_ver
== PARAMETER_V1
)
1153 type_c
->cc1_vref
= V1_vref_2p6v(cc_param
->vref_2p6v
) |
1154 V1_vref_1p23v(cc_param
->vref_1p23v
) |
1155 V1_vref_0p8v(cc_param
->vref_0p8v
) |
1156 V1_vref_0p66v(cc_param
->vref_0p66v
) |
1157 V1_vref_0p4v(cc_param
->vref_0p4v
) |
1158 V1_vref_0p2v(cc_param
->vref_0p2v
) |
1159 V1_vref_1_1p6v(cc_param
->vref_1_1p6v
) |
1160 V1_vref_0_1p6v(cc_param
->vref_0_1p6v
);
1162 dev_err(type_c
->dev
, "%s: unknown parameter_ver %d\n",
1163 __func__
, type_c_cfg
->parameter_ver
);
1165 cc_param
= &type_c_cfg
->cc2_param
;
1166 type_c
->cc2_code
= rp4pk_code(cc_param
->rp_4p7k_code
)
1167 | rp36k_code(cc_param
->rp_36k_code
)
1168 | rp12k_code(cc_param
->rp_12k_code
)
1169 | rd_code(cc_param
->rd_code
);
1171 if (type_c_cfg
->parameter_ver
== PARAMETER_V0
)
1172 type_c
->cc2_vref
= V0_vref_2p6v(cc_param
->vref_2p6v
) |
1173 V0_vref_1p23v(cc_param
->vref_1p23v
) |
1174 V0_vref_0p8v(cc_param
->vref_0p8v
) |
1175 V0_vref_0p66v(cc_param
->vref_0p66v
) |
1176 V0_vref_0p4v(cc_param
->vref_0p4v
) |
1177 V0_vref_0p2v(cc_param
->vref_0p2v
) |
1178 V0_vref_1_1p6v(cc_param
->vref_1_1p6v
) |
1179 V0_vref_0_1p6v(cc_param
->vref_0_1p6v
);
1180 else if (type_c_cfg
->parameter_ver
== PARAMETER_V1
)
1181 type_c
->cc2_vref
= V1_vref_2p6v(cc_param
->vref_2p6v
) |
1182 V1_vref_1p23v(cc_param
->vref_1p23v
) |
1183 V1_vref_0p8v(cc_param
->vref_0p8v
) |
1184 V1_vref_0p66v(cc_param
->vref_0p66v
) |
1185 V1_vref_0p4v(cc_param
->vref_0p4v
) |
1186 V1_vref_0p2v(cc_param
->vref_0p2v
) |
1187 V1_vref_1_1p6v(cc_param
->vref_1_1p6v
) |
1188 V1_vref_0_1p6v(cc_param
->vref_0_1p6v
);
1190 dev_err(type_c
->dev
, "%s: unknown parameter_ver %d\n",
1191 __func__
, type_c_cfg
->parameter_ver
);
1193 type_c
->debounce
= (type_c_cfg
->debounce_val
<< 1) | DEBOUNCE_EN
;
1198 static int extcon_rtk_type_c_init(struct type_c_data
*type_c
)
1200 struct device
*dev
= type_c
->dev
;
1201 unsigned long flags
;
1205 spin_lock_irqsave(&type_c
->lock
, flags
);
1208 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC1_0
;
1210 val
= (~CC_CODE_MASK
& val
) | (type_c
->cc1_code
& CC_CODE_MASK
);
1213 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC2_0
;
1215 val
= (~CC_CODE_MASK
& val
) | (type_c
->cc2_code
& CC_CODE_MASK
);
1217 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC1_1
;
1218 writel(type_c
->cc1_vref
, reg
);
1220 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL_CC2_1
;
1221 writel(type_c
->cc2_vref
, reg
);
1223 reg
= type_c
->reg_base
+ USB_TYPEC_CTRL
;
1225 val
= (~DEBOUNCE_TIME_MASK
& val
) | (type_c
->debounce
& DEBOUNCE_TIME_MASK
);
1227 dev_info(dev
, "First check USB_DR_MODE_PERIPHERAL");
1228 type_c
->cc_mode
= IN_DEVICE_MODE
;
1229 type_c
->is_attach
= IN_DETACH
;
1230 type_c
->connect_change
= CONNECT_NO_CHANGE
;
1232 detect_host(type_c
);
1234 spin_unlock_irqrestore(&type_c
->lock
, flags
);
1236 schedule_delayed_work(&type_c
->delayed_work
, msecs_to_jiffies(0));
1238 if (!type_c
->port
) {
1239 struct typec_capability typec_cap
= { };
1240 struct fwnode_handle
*fwnode
;
1244 typec_cap
.revision
= USB_TYPEC_REV_1_0
;
1245 typec_cap
.prefer_role
= TYPEC_NO_PREFERRED_ROLE
;
1246 typec_cap
.driver_data
= type_c
;
1247 typec_cap
.ops
= &type_c_port_ops
;
1249 fwnode
= device_get_named_child_node(dev
, "connector");
1253 ret
= fwnode_property_read_string(fwnode
, "power-role", &buf
);
1255 dev_err(dev
, "power-role not found: %d\n", ret
);
1259 ret
= typec_find_port_power_role(buf
);
1262 typec_cap
.type
= ret
;
1264 ret
= fwnode_property_read_string(fwnode
, "data-role", &buf
);
1266 dev_err(dev
, "data-role not found: %d\n", ret
);
1270 ret
= typec_find_port_data_role(buf
);
1273 typec_cap
.data
= ret
;
1275 type_c
->port
= typec_register_port(type_c
->dev
, &typec_cap
);
1276 if (IS_ERR(type_c
->port
))
1277 return PTR_ERR(type_c
->port
);
1283 static int extcon_rtk_type_c_edev_register(struct type_c_data
*type_c
)
1285 struct device
*dev
= type_c
->dev
;
1288 type_c
->edev
= devm_extcon_dev_allocate(dev
, usb_type_c_cable
);
1289 if (IS_ERR(type_c
->edev
)) {
1290 dev_err(dev
, "failed to allocate extcon device\n");
1294 ret
= devm_extcon_dev_register(dev
, type_c
->edev
);
1296 dev_err(dev
, "failed to register extcon device\n");
1300 extcon_set_property_capability(type_c
->edev
, EXTCON_USB
,
1301 EXTCON_PROP_USB_VBUS
);
1302 extcon_set_property_capability(type_c
->edev
, EXTCON_USB
,
1303 EXTCON_PROP_USB_TYPEC_POLARITY
);
1304 extcon_set_property_capability(type_c
->edev
, EXTCON_USB
,
1305 EXTCON_PROP_USB_SS
);
1307 extcon_set_property_capability(type_c
->edev
, EXTCON_USB_HOST
,
1308 EXTCON_PROP_USB_VBUS
);
1309 extcon_set_property_capability(type_c
->edev
, EXTCON_USB_HOST
,
1310 EXTCON_PROP_USB_TYPEC_POLARITY
);
1311 extcon_set_property_capability(type_c
->edev
, EXTCON_USB_HOST
,
1312 EXTCON_PROP_USB_SS
);
1317 static int extcon_rtk_type_c_probe(struct platform_device
*pdev
)
1319 struct device
*dev
= &pdev
->dev
;
1320 struct type_c_data
*type_c
;
1321 const struct type_c_cfg
*type_c_cfg
;
1324 type_c
= devm_kzalloc(dev
, sizeof(*type_c
), GFP_KERNEL
);
1328 type_c
->reg_base
= devm_platform_ioremap_resource(pdev
, 0);
1329 if (IS_ERR(type_c
->reg_base
))
1330 return PTR_ERR(type_c
->reg_base
);
1334 type_c
->irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
1335 if (type_c
->irq
<= 0) {
1336 dev_err(&pdev
->dev
, "Type C driver with no IRQ. Check %s setup!\n",
1337 dev_name(&pdev
->dev
));
1342 ret
= devm_request_irq(dev
, type_c
->irq
, type_c_detect_irq
,
1343 IRQF_SHARED
, "type_c_detect", type_c
);
1345 spin_lock_init(&type_c
->lock
);
1347 type_c
->rd_ctrl_gpio_desc
= NULL
;
1348 if (soc_device_match(rtk_soc_kylin
)) {
1349 struct gpio_desc
*gpio
;
1351 gpio
= fwnode_gpiod_get_index(of_fwnode_handle(dev
->of_node
),
1352 "realtek,rd-ctrl-gpios",
1353 0, GPIOD_OUT_HIGH
, "rd-ctrl-gpio");
1355 dev_err(dev
, "Error rd_ctrl-gpios no found (err=%d)\n",
1356 (int)PTR_ERR(gpio
));
1358 type_c
->rd_ctrl_gpio_desc
= gpio
;
1359 dev_dbg(dev
, "%s get rd-ctrl-gpios (id=%d) OK\n",
1360 __func__
, desc_to_gpio(gpio
));
1364 type_c_cfg
= of_device_get_match_data(dev
);
1366 dev_err(dev
, "type_c config are not assigned!\n");
1371 type_c
->type_c_cfg
= devm_kzalloc(dev
, sizeof(*type_c_cfg
), GFP_KERNEL
);
1373 memcpy(type_c
->type_c_cfg
, type_c_cfg
, sizeof(*type_c_cfg
));
1375 if (setup_type_c_parameter(type_c
)) {
1376 dev_err(dev
, "ERROR: %s to setup type c parameter!!", __func__
);
1381 INIT_DELAYED_WORK(&type_c
->delayed_work
, host_device_switch
);
1383 ret
= extcon_rtk_type_c_init(type_c
);
1385 dev_err(dev
, "%s failed to init type_c\n", __func__
);
1389 platform_set_drvdata(pdev
, type_c
);
1391 ret
= extcon_rtk_type_c_edev_register(type_c
);
1393 create_debug_files(type_c
);
1398 dev_err(&pdev
->dev
, "%s: Probe fail, %d\n", __func__
, ret
);
1403 static void extcon_rtk_type_c_remove(struct platform_device
*pdev
)
1405 struct device
*dev
= &pdev
->dev
;
1406 struct type_c_data
*type_c
= dev_get_drvdata(dev
);
1408 unsigned long flags
;
1410 remove_debug_files(type_c
);
1413 typec_unregister_port(type_c
->port
);
1414 type_c
->port
= NULL
;
1417 cancel_delayed_work_sync(&type_c
->delayed_work
);
1418 flush_delayed_work(&type_c
->delayed_work
);
1419 WARN_ON_ONCE(delayed_work_pending(&type_c
->delayed_work
));
1421 spin_lock_irqsave(&type_c
->lock
, flags
);
1422 /* disable interrupt */
1423 default_ctrl
= readl(type_c
->reg_base
+ USB_TYPEC_CTRL
) &
1425 writel(default_ctrl
, type_c
->reg_base
+ USB_TYPEC_CTRL
);
1427 /* disable cc detect, rp, rd */
1428 writel(PLR_EN
, type_c
->reg_base
+ USB_TYPEC_CTRL_CC1_0
);
1429 writel(0, type_c
->reg_base
+ USB_TYPEC_CTRL_CC2_0
);
1431 spin_unlock_irqrestore(&type_c
->lock
, flags
);
1433 if (type_c
->rd_ctrl_gpio_desc
)
1434 gpiod_put(type_c
->rd_ctrl_gpio_desc
);
1435 type_c
->rd_ctrl_gpio_desc
= NULL
;
1437 free_irq(type_c
->irq
, type_c
);
1440 static const struct type_c_cfg rtd1295_type_c_cfg
= {
1441 .parameter_ver
= PARAMETER_V0
,
1442 .cc_dfp_mode
= CC_MODE_DFP_3_0
,
1443 .cc1_param
= { .rp_4p7k_code
= 0xb,
1444 .rp_36k_code
= 0x17,
1445 .rp_12k_code
= 0x10,
1456 .cc2_param
= { .rp_4p7k_code
= 0xc,
1457 .rp_36k_code
= 0x17,
1458 .rp_12k_code
= 0x12,
1469 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1470 .use_defalut_parameter
= false,
1473 static const struct type_c_cfg rtd1395_type_c_cfg
= {
1474 .parameter_ver
= PARAMETER_V0
,
1475 .cc_dfp_mode
= CC_MODE_DFP_3_0
,
1476 .cc1_param
= { .rp_4p7k_code
= 0xc,
1488 .vref_0_1p6v
= 0x7 },
1489 .cc2_param
= { .rp_4p7k_code
= 0xb,
1501 .vref_0_1p6v
= 0x7 },
1502 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1503 .use_defalut_parameter
= false,
1506 static const struct type_c_cfg rtd1619_type_c_cfg
= {
1507 .parameter_ver
= PARAMETER_V0
,
1508 .cc_dfp_mode
= CC_MODE_DFP_3_0
,
1509 .cc1_param
= { .rp_4p7k_code
= 0xc,
1521 .vref_0_1p6v
= 0x7 },
1522 .cc2_param
= { .rp_4p7k_code
= 0xc,
1534 .vref_0_1p6v
= 0x7 },
1535 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1536 .use_defalut_parameter
= false,
1539 static const struct type_c_cfg rtd1319_type_c_cfg
= {
1540 .parameter_ver
= PARAMETER_V0
,
1541 .cc_dfp_mode
= CC_MODE_DFP_1_5
,
1542 .cc1_param
= { .rp_4p7k_code
= 0x9,
1554 .vref_0_1p6v
= 0x7 },
1555 .cc2_param
= { .rp_4p7k_code
= 0x8,
1567 .vref_0_1p6v
= 0x7 },
1568 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1569 .use_defalut_parameter
= false,
1572 static const struct type_c_cfg rtd1312c_type_c_cfg
= {
1573 .parameter_ver
= PARAMETER_V0
,
1574 .cc_dfp_mode
= CC_MODE_DFP_1_5
,
1575 .cc1_param
= { .rp_4p7k_code
= 0xe,
1587 .vref_0_1p6v
= 0x7 },
1588 .cc2_param
= { .rp_4p7k_code
= 0xe,
1600 .vref_0_1p6v
= 0x7 },
1601 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1602 .use_defalut_parameter
= false,
1605 static const struct type_c_cfg rtd1619b_type_c_cfg
= {
1606 .parameter_ver
= PARAMETER_V1
,
1607 .cc_dfp_mode
= CC_MODE_DFP_1_5
,
1608 .cc1_param
= { .rp_4p7k_code
= 0xf,
1620 .vref_0_1p6v
= 0x7 },
1621 .cc2_param
= { .rp_4p7k_code
= 0xf,
1632 .vref_0_1p6v
= 0x7 },
1633 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1634 .use_defalut_parameter
= false,
1637 static const struct type_c_cfg rtd1319d_type_c_cfg
= {
1638 .parameter_ver
= PARAMETER_V1
,
1639 .cc_dfp_mode
= CC_MODE_DFP_1_5
,
1640 .cc1_param
= { .rp_4p7k_code
= 0xe,
1652 .vref_0_1p6v
= 0x7 },
1653 .cc2_param
= { .rp_4p7k_code
= 0xe,
1665 .vref_0_1p6v
= 0x7 },
1666 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1667 .use_defalut_parameter
= false,
1670 static const struct type_c_cfg rtd1315e_type_c_cfg
= {
1671 .parameter_ver
= PARAMETER_V1
,
1672 .cc_dfp_mode
= CC_MODE_DFP_1_5
,
1673 .cc1_param
= { .rp_4p7k_code
= 0xe,
1685 .vref_0_1p6v
= 0x7 },
1686 .cc2_param
= { .rp_4p7k_code
= 0xe,
1698 .vref_0_1p6v
= 0x7 },
1699 .debounce_val
= 0x7f, /* 1b,1us 7f,4.7us */
1700 .use_defalut_parameter
= false,
1703 static const struct of_device_id extcon_rtk_type_c_match
[] = {
1704 { .compatible
= "realtek,rtd1295-type-c", .data
= &rtd1295_type_c_cfg
},
1705 { .compatible
= "realtek,rtd1312c-type-c", .data
= &rtd1312c_type_c_cfg
},
1706 { .compatible
= "realtek,rtd1315e-type-c", .data
= &rtd1315e_type_c_cfg
},
1707 { .compatible
= "realtek,rtd1319-type-c", .data
= &rtd1319_type_c_cfg
},
1708 { .compatible
= "realtek,rtd1319d-type-c", .data
= &rtd1319d_type_c_cfg
},
1709 { .compatible
= "realtek,rtd1395-type-c", .data
= &rtd1395_type_c_cfg
},
1710 { .compatible
= "realtek,rtd1619-type-c", .data
= &rtd1619_type_c_cfg
},
1711 { .compatible
= "realtek,rtd1619b-type-c", .data
= &rtd1619b_type_c_cfg
},
1714 MODULE_DEVICE_TABLE(of
, extcon_rtk_type_c_match
);
1716 #ifdef CONFIG_PM_SLEEP
1717 static int extcon_rtk_type_c_prepare(struct device
*dev
)
1719 struct type_c_data
*type_c
= dev_get_drvdata(dev
);
1721 unsigned long flags
;
1723 cancel_delayed_work_sync(&type_c
->delayed_work
);
1724 flush_delayed_work(&type_c
->delayed_work
);
1725 WARN_ON_ONCE(delayed_work_pending(&type_c
->delayed_work
));
1727 spin_lock_irqsave(&type_c
->lock
, flags
);
1728 /* disable interrupt */
1729 default_ctrl
= readl(type_c
->reg_base
+ USB_TYPEC_CTRL
) &
1731 writel(default_ctrl
, type_c
->reg_base
+ USB_TYPEC_CTRL
);
1733 /* disable cc detect, rp, rd */
1734 writel(PLR_EN
, type_c
->reg_base
+ USB_TYPEC_CTRL_CC1_0
);
1735 writel(0, type_c
->reg_base
+ USB_TYPEC_CTRL_CC2_0
);
1737 spin_unlock_irqrestore(&type_c
->lock
, flags
);
1742 static void extcon_rtk_type_c_complete(struct device
*dev
)
1747 static int extcon_rtk_type_c_suspend(struct device
*dev
)
1754 static int extcon_rtk_type_c_resume(struct device
*dev
)
1756 struct type_c_data
*type_c
= dev_get_drvdata(dev
);
1759 ret
= extcon_rtk_type_c_init(type_c
);
1761 dev_err(dev
, "%s failed to init type_c\n", __func__
);
1768 static const struct dev_pm_ops extcon_rtk_type_c_pm_ops
= {
1769 SET_LATE_SYSTEM_SLEEP_PM_OPS(extcon_rtk_type_c_suspend
, extcon_rtk_type_c_resume
)
1770 .prepare
= extcon_rtk_type_c_prepare
,
1771 .complete
= extcon_rtk_type_c_complete
,
1774 #define DEV_PM_OPS (&extcon_rtk_type_c_pm_ops)
1776 #define DEV_PM_OPS NULL
1777 #endif /* CONFIG_PM_SLEEP */
1779 static struct platform_driver extcon_rtk_type_c_driver
= {
1780 .probe
= extcon_rtk_type_c_probe
,
1781 .remove_new
= extcon_rtk_type_c_remove
,
1783 .name
= "extcon-rtk-type_c",
1784 .of_match_table
= extcon_rtk_type_c_match
,
1789 module_platform_driver(extcon_rtk_type_c_driver
);
1791 MODULE_DESCRIPTION("Realtek Extcon Type C driver");
1792 MODULE_AUTHOR("Stanley Chang <stanley_chang@realtek.com>");
1793 MODULE_LICENSE("GPL");