Skip to content

rockchip64-6.15: Fix DP alt mode on some rk3399 boards #8271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hyx0329
Copy link
Contributor

@hyx0329 hyx0329 commented Jun 4, 2025

Description

Fix Type-C DP altmode on some rk3399 boards. Currently only on rockchip64-6.15.

The old implementation is tricky, and only works in normal orientation. If the plug is flipped, there will be only USB2.0 connectivity.

With the proposed changes, the type-c ports on related rk3399 boards should work in both orientations. A few patches from Megous are picked(some of them already present in sunxi64 kernel patches). The tricky part is how to notify rk3399 cdn-dp driver about DP HPD(hot plug detection), and a workaround is written.

A caveat is that the c port has to be OTG mode, otherwise it will behave very similar to the old implementation if DP is enabled.

I'm not sure if orange pi 4 / 4 LTS support this, so there's no change to their dts yet.

Apart from the work above, the missing fan node is added to tinkerboard 2's dts.

list of changes

  • fix type-c dp altmode
    • Megous' glue driver and related improvements are imported
      • this involves enabling the driver in defconfig CONFIG_TYPEC_EXTCON=m
    • a workaround for notifying DP HPD to cdn-dp is written(not merged anywhere)
    • related device tree files/patches are updated accordingly(what about orangepi 4?)
  • add missing fan node in tinkerboard 2's dts along with some minor tweaks

How Has This Been Tested?

  • build tinkerboard-2 with edge kernel
  • test on tinkerboard 2s with a hub
    • hub alone works in both orientations
    • hub with display connected works in both orientations
    • when hub is connected, display hot plug/unplug works
    • USB 3.0 type-c drive works in both orientations
  • test with a DP-only dongle
  • test on pinebook pro
  • test on nanopc-t4
  • maybe test on orangepi 4 (need dts change)
  • maybe test on orangepi 4 LTS (need dts change)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

Copy link
Contributor

coderabbitai bot commented Jun 4, 2025

Walkthrough

The linux-rockchip64-edge kernel configuration file was updated to include the CONFIG_TYPEC_EXTCON=m option. This change enables support for the Type-C external connector (extcon) as a loadable kernel module. No other configuration options or settings were modified in this update.

Suggested labels

ready to merge

Suggested reviewers

  • paolosabatino
  • igorpecovnik

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e242c88 and ff47cda.

⛔ Files ignored due to path filters (18)
  • patch/kernel/archive/rockchip64-6.15/board-nanopc-t4-add-typec-dp.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/board-pbp-add-dp-alt-mode.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/dt/rk3399-orangepi-4-lts.dts is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/dt/rk3399-orangepi-4.dts is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/dt/rk3399-tinker-2.dts is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-Revert-usb-typec-tcpm-unregister-existing-source-cap.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-drm-rockchip-cdn-dp-Disable-CDN-DP-on-disconnect.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-notify-typec-dp-hpd-state-through-extcon.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-phy-phy-rockchip-inno-usb2-Decrease-delay-between-po.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-phy-rockchip-inno-usb2-More-robust-charger-detection.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-phy-rockchip-naneng-Add-fallback-for-old-DTs.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-usb-dwc3-Extend-reset-quirk-support-to-include-role-.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-usb-dwc3-Track-the-power-state-of-usb3_generic_phy.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-usb-typec-altmodes-displayport-Respect-DP_CAP_RECEPT.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-usb-typec-tcpm-Fix-PD-devices-capabilities-registrat.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-usb-typec-tcpm-Unregister-altmodes-before-registerin.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-usb-typec-typec-extcon-Add-typec-extcon-bridge-drive.patch is excluded by !patch/**
  • patch/kernel/archive/rockchip64-6.15/rk3399-usbc-usb-typec-typec-extcon-Allow-to-force-reset-on-each-.patch is excluded by !patch/**
📒 Files selected for processing (1)
  • config/kernel/linux-rockchip64-edge.config (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • config/kernel/linux-rockchip64-edge.config
✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added size/large PR with 250 lines or more Needs review Seeking for review Hardware Hardware related like kernel, U-Boot, ... Patches Patches related to kernel, U-Boot, ... 08 Milestone: Third quarter release labels Jun 4, 2025
@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 4, 2025
@EvilOlaf EvilOlaf removed the Ready to merge Reviewed, tested and ready for merge label Jun 4, 2025
@paolosabatino
Copy link
Contributor

Looks interesting; perhaps I can find some time during this weekend to give a check on Opi4 LTS. I'm only dubious about what the ~20 new patches for rk3399 do, but anyway...

@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 5, 2025
@hyx0329
Copy link
Contributor Author

hyx0329 commented Jun 5, 2025

Some less relevant patches are removed. All patches are rewritten with "rewrite-kernel-patches".

Edit: for convenience, here's a prebuilt kernel package with orangepi 4/LTS dts patched. link link-dtb

@paolosabatino
Copy link
Contributor

Tested on Orange Pi 4 with the help of an external Type-C Hub.
The hub has PD and HDMI; connecting both power and a monitor the the Hub and then connecting the Hub to the board Type-C port, the board was able to drive the monitor that way. Also the embedded HDMI port of the board still works.

I noticed though that, when the Hub is connected to Type-C and HDMI is in use, USB ports of the Hub don't work.

@hyx0329
Copy link
Contributor Author

hyx0329 commented Jun 9, 2025

when the Hub is connected to Type-C and HDMI is in use, USB ports of the Hub don't work.

Please elaborate. What kind of USB device(pen drive, wireless mouse, usb 2.0 or 3.0) was tested with? Is dmesg showing anything indicating the detection of USB device when it's plugged? Here's an example dmesg log:

[ 1927.188573] usb 7-1.1.4: new full-speed USB device number 10 using xhci-hcd
[ 1927.302718] usb 7-1.1.4: New USB device found, idVendor=1a86, idProduct=55d4, bcdDevice= 4.44
[ 1927.302757] usb 7-1.1.4: New USB device strings: Mfr=0, Product=2, SerialNumber=3
[ 1927.302776] usb 7-1.1.4: Product: USB Single Serial

The glue driver will print current USB extcon status in dmesg with prefix typec-extcon, and if you see usb_host=1, usb should work. Here's an example:

[   12.633716] typec-extcon typec-extcon: extcon changed sdp=0 cdp=0 dcp=0 usb=0 usb_host=1 dp=1

When DP HPD event is synced with extcon, something like this is printed:

[   12.633663] typec_displayport port0-partner.0: dp-hpd=1 (port=1c46 alt=c05)

HDMI works at a frequency that will interfere with 2.4G radios if not shielded properly. If usb works alone and then not when HDMI is plugged, I suspect that HDMI is interfering with your wireless keyboard/mouse. If so, it's the hub's quality issue. Some devices may be more resistant to the interference though.

If DP/HDMI works, I think at least USB 3.0 should work, if equipped on the hub.

For once, my hub's USB ports stopped working, but I can't reproduce the problem.

@github-actions github-actions bot removed the Ready to merge Reviewed, tested and ready for merge label Jun 10, 2025
@hyx0329 hyx0329 force-pushed the rk3399-usbc branch 2 times, most recently from 7ee69dc to bd723d8 Compare June 10, 2025 02:42
@hyx0329
Copy link
Contributor Author

hyx0329 commented Jun 10, 2025

Thanks to @paolosabatino's report, we know at least OPi4 can support DP output. But for some reason, when the HDMI(DP output) is in use, the hub's usb ports don't work. As far as I know, even if 4-lane DP is used and USB3 fails, USB2(on rk3399's side) shouldn't fail, which is the old behavior.

I pushed the changes to OPi4/LTS's dts anyway. Maybe I miss something.

Edit: oh I found it, it's PD(Power Delivery). When external PD power supply is connected, the hub's usb ports may not recognized.

@paolosabatino
Copy link
Contributor

paolosabatino commented Jun 10, 2025

@hyx0329 thanks for the answer. For the sake of completeness, I attach here a dmesg log of the session. The various USB attach/detach messages are due to me attaching and detaching the USB device to the board USB ports. The USB ports of the Hub were completely silent.

Anyway I will try to find a way to power the board without PD and report if the USB ports work along DP/HDMI when power is not delivered by Type-C.

Something I also noticed is that, when detaching the HDMI monitor from the Hub and attaching to the board HDMI, the monitor was still enumerated as a connected display on DP-1. I don't know if it was a problem of the KDE Plasma utility or a driver/dts issue, but I then got two monitors, one attached to HDMI-A-1 and the same to DP-1. Anyway, this is a minor issue.

dmesg.log.gz

@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 11, 2025
@hyx0329
Copy link
Contributor Author

hyx0329 commented Jun 11, 2025

Loading a gadget driver(like g_serial) seems fix the PD hub problem.

Out of coincidence, I left the g_serial module loaded after some tests and connected the powered hub to my tinkerboard 2s after that. Then I found dmesg printing a lot about new USB devices. Problem solved :)

Loading one gadget driver(g_serial, g_ncm, etc.) before connecting those powered hubs will somehow fix the detection issue. If the gadget driver is loaded with a config in modules-load.d, powered hubs connected before booting will also work.

I think it's something like this(on superuser), that the downstream hub is not reset or something. But I couldn't find a way to test.

By the way, if dr_mode in usbdrd_dwc3_0 node in dts is set to host, everything will behave just like the old mplementation.

@paolosabatino
Copy link
Contributor

@hyx0329 understand, thanks for delving into this. In the meantime I'm having bad time finding a proper power connector for the Opi4LTS, because it can't be powered via GPIO headers and the power connector is an unusual 1.7mm x 4.75mm or so.

I guess that, since if dr_mode = "host" is the default on other boards, none of them will be affected by this change unless they get the new bits in the device trees. If you feel safe with this PR, I can accept; if you still want to refine we can wait a little bit more.

@igorpecovnik
Copy link
Member

and the power connector is an unusual 1.7mm x 4.75mm or so.

I can sent you some of these if you want - have lots of cables and PSUs doing nothing.

@hyx0329
Copy link
Contributor Author

hyx0329 commented Jun 12, 2025

@paolosabatino well the situation is, the new implementation/extcon driver requires changes to device trees, otherwise at least the displayport output won't work. I think I'll keep dr-mode = "host" on boards which can be powered through type-c port, including pinebook pro, orangepi 4(LTS), to keep the original behavior.

On tinkerboard 2 and nanopc t4, the type-c port doesn't actually support power input and thus hubs are tend to be used without a power supply, so I would like to keep dr-mode = "otg" on those.

A second option is to load g_zero or g_serial or else on affected boards which have otg support. g_zero works like a black hole or mirror, while g_serial provides a serial interface. I think in both cases there's no significant issue. The scale of change is beyond my original expectation though.

I won't mind if this pr is postponed or closed. I just do my best to fix problems :)

@paolosabatino
Copy link
Contributor

paolosabatino commented Jun 12, 2025

and the power connector is an unusual 1.7mm x 4.75mm or so.

I can sent you some of these if you want - have lots of cables and PSUs doing nothing.

Thanks, but I am sure I have one of them somewhere I cut from an old DELL PDA adapter. Just can't find it. Anyway, @hyx0329 has found that the issue is software based, probably some reset or some register to initialize to make the USB devices working, hence there isn't the necessity to test the external hub without PD anymore.

It would have been a real nice thing to get HDMI/DP + PD + USB via Type-C on rk3399, since the hardware seems to support the whole package, there is just that annoying workaround to load a usb gadget.

In my opinion, this PR could be merged when @hyx0329 says it is ready: if I understand this correctly, it should not change the behaviour to any board other than those which gets dr_mode="otg" in the device tree changed explicitly.

@hyx0329 hyx0329 requested a review from pyavitz as a code owner June 18, 2025 05:25
@github-actions github-actions bot removed the Ready to merge Reviewed, tested and ready for merge label Jun 18, 2025
Improvements:
- type-c DP on rk3399 works with both orientations
- type-c USB 3.0 on rk3399 works with both orientations, with minor
  issues

Caveats:
- Powered USB-C hubs may be not recognized, and can be worked around by
  loading a gadget driver, or manually toggling the mode once for each
  connection.
- Some dual-role devices(phone, tablet) may be not recognized.

Tested on tinkerboard 2s. This patch contains other minor fixes for
tinker2's device tree.

The 2 patches adding dp support for nanopc t4 and pinebook pro are also
updated.

The device trees of Orange Pi 4 / 4 LTS are also updated to match the
new implementation.
@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 18, 2025
@hyx0329
Copy link
Contributor Author

hyx0329 commented Jun 18, 2025

I think this will be a "partial" fix.

With a few more tests with different devices & configurations, 2 major flaws are found:

  • Powered USB hub may not be recognized.
  • Other dual-role USB devices(phone, tablet) may not be recognized.

I couldn't find the root cause of them. Loading a gadget driver only works around the first flaw.

If the behavior change and the workaround are acceptable, I think it's okay to merge.

I'll look into it if the workaround should be included by default for each affected boards.

Behavior change and comparison

Only on RK3399

Function Old implementation New implementation
USB 2.0 fully functional1 fully functional when downstream device has no power supply2, plus OTG
USB 3.0 only one orientation fully functional when downstream device has no power supply2, plus OTG3
DisplayPort only one orientation, only 2 lanes fully functional4
Power Delivery(Charging) working5 working5
  1. Host mode only.
  2. When connecting to a powered hub, loading a gadget driver, or manually toggling the mode through debugfs, can workaound the detection issue.
  3. It seems not working when the cable is a dumb C2C one without e-marker chip.
  4. Only tested with a hub supporting 2 lane DP, and it's always working, independent of the USB status.
  5. It looks working. The related boards are either not supporting power input, or only using a 5V input. The debugfs log shows the PD negotiation works.

The old behavior can still be preserved with some tweaks to the device tree, if it's desired.

Affected boards

As far as I know, only boards mentioned in this pr are affected. Other RK3399 boards are not affected because their type-c ports are either fully functional or not working at all, if they exist. Other non-RK3399 boards should be not affected.

To be affected, RK3399 boards will have to leave dr_mode unchanged(upstream default is "otg") and create+bind the node for the new extcon bridge in their dts.

@paolosabatino
Copy link
Contributor

Thanks @hyx0329 for the summary overview table, I have a couple of questions though.

This sentence confuses me a bit:

As far as I know, only boards mentioned in this pr are affected. Other RK3399 boards are not affected because their type-c ports are either fully functional or not working at all, if they exist. Other non-RK3399 boards should be not affected.

I thought the "old implementation" in the mainline kernel - regarding the RK3399 SoC in general - is limited and/or partially broken; First of all, the patches in this PR either fix and expand the functionalities for the RK3399 SoC, then the corrections to the device trees declare the hardware in the right way to make those functionalities available to the kernel.

About the sentence:

fully functional when downstream device has no power supply, plus OTG

do you mean that if the USB device has some kind of power supply of its own won't work anymore?

Thanks for any answer, this whole PR is definitely intersting but the risk of breakage and the patches it carries along (that will require maintenance by armbian maintainers, in the future) are my main concerns, so I would like to understand as much as I can.

@hyx0329
Copy link
Contributor Author

hyx0329 commented Jun 18, 2025

Hi @paolosabatino

This sentence confuses me a bit:

As far as I know, only boards mentioned in this pr are affected. Other RK3399 boards are not affected because their type-c ports are either fully functional or not working at all, if they exist. Other non-RK3399 boards should be not affected.

I thought the "old implementation" in the mainline kernel - regarding the RK3399 SoC in general - is limited and/or partially broken; First of all, the patches in this PR either fix and expand the functionalities for the RK3399 SoC, then the corrections to the device trees declare the hardware in the right way to make those functionalities available to the kernel.

The "old implementation" contains 2 parts: existing drivers in mainline kernel, and the patch board-pbp-add-dp-alt-mode.patch(before changed by this pr). Mainline kernel has most necessary drivers already, including:

  • cdn-dp, core for DP output
  • rk3399 type-c phy, some kind of mux
  • dwc3, usb controller
  • fusb302 chip, usb-c controller

But upstream misses the extcon bridge(shared state) connecting all those components. The patch board-pbp-add-dp-alt-mode.patch contains a tricky(broken) implementation possibly from 4.19 BSP kernel. Somehow it makes type-c DP and USB 3.0 work, at least in normal orientation.

The main reason that I created this PR is to make type-c work in both orientations, which should make it more comfortable to use.

The patches in this PR mainly bring these things:

  • (new) an extcon bridge driver
    • provides the necessary shared state
    • rk3399-usbc-usb-typec-typec-extcon-Add-typec-extcon-bridge-drive.patch
    • rk3399-usbc-usb-typec-typec-extcon-Allow-to-force-reset-on-each-.patch
  • (fix) add reset quirk for dwc3 driver to reset type-c phy(mux) on rk3399
    • rk3399 needs this to switch type-c orientation & allocate DP lanes on demand
    • rk3399-usbc-phy-rockchip-naneng-Add-fallback-for-old-DTs.patch
    • rk3399-usbc-usb-dwc3-Extend-reset-quirk-support-to-include-role-.patch
  • (workaround) send DP HPD state through extcon in dp altmode driver
    • this is to notify cdn-dp driver about the actual connection state of DP, not type-c
    • I use the term workaround because it's definitely not a "proper" implementation.
    • rk3399-usbc-notify-typec-dp-hpd-state-through-extcon.patch

As you can see, there are still a few less relevant patches. Some are prerequisites, and others are minor improvements.

And yes the device trees are then updated to make those functionalities available. The functions have to be explictly specified per capable board.

Because dwc3 is a common driver, it can affect a wide range of boards if things go wrong.

About the sentence:

fully functional when downstream device has no power supply, plus OTG

do you mean that if the USB device has some kind of power supply of its own won't work anymore?

I think I made a mistake. Now I would say smarter devices which can negotiate data role or power role through USB-C may encounter issues.

What I observed:

  • The USB part of a powered hub is not detected, although DisplayPort works.
  • Some OTG gadgets(tablet) are not detected.

I thought the reason was power, but the fact seems to be their ability to negotiate data role and power role. I just tested a simple battery powered RP2040 gadget.

Thanks for any answer, this whole PR is definitely intersting but the risk of breakage and the patches it carries along (that will require maintenance by armbian maintainers, in the future) are my main concerns, so I would like to understand as much as I can.

Understood. That is being responsible :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
08 Milestone: Third quarter release Hardware Hardware related like kernel, U-Boot, ... Needs review Seeking for review Patches Patches related to kernel, U-Boot, ... Ready to merge Reviewed, tested and ready for merge size/large PR with 250 lines or more
Development

Successfully merging this pull request may close these issues.

4 participants