Skip to contentSkip to conversation

drm/bridge: trilinear: add the Sky1 DisplayPort transmitter

PATCH New Luo Liu 2 participants 9 messages Nov 25

Subsystems (4)
lore
Share & tools
lore

Luo Liu

author
#0 lore raw

v4 cover drm/bridge: trilinear: add the Sky1 DisplayPort transmitter

Add the Trilinear DisplayPort transmitter as a DRM bridge, with the CIX Sky1 platform glue behind a platform data structure.

The hardware layer, the DRM bridge and the core state machine live in drivers/gpu/drm/bridge/trilinear/, so other SoCs using the same transmitter can share them; the platform registers its own dptx_plat_data through include/drm/bridge/trilinear_dp.h. The transmitter drives up to four lanes at HBR3 and has two SST sources. MST is not implemented and stays disabled.

The USB-C transmitters and the native DP/eDP transmitter use different RCSU video-adapter register offsets and bit layouts, so the binding has one compatible for each. The Sky1 devicetree nodes are not included in this posting.

Scope: DPTX hardware, bridge and bindings only. Komeda (the Sky1 display controller that feeds these bridges) and the board DTS are separate series. Nothing in this posting wires a full display stack.

Tested on a Radxa Orion O6 board; the USB-C DisplayPort output works with the connector created by drm_bridge_connector_init().

Connector handling in this driver:

The bridge is meant to be attached with DRM_BRIDGE_ATTACH_NO_CONNECTOR and drm_bridge_connector_init() creates the connector from the bridge operations. The driver implements the bridge side only and never registers a connector of its own.

Changes in v4:
- Address the issues reported in the sashiko review of v3, most serious first:
- dptx_detect() clears DPTX_STATE_CONNECTED when HPD is deasserted, and the hotplug worker updates dp->status in the same locked section as the state change, so an early userspace detect can neither keep stale sink capabilities nor suppress the hotplug notification.
- dptx_pm_prepare() re-asserts DPTX_STATE_SUSPENDED and disables the interrupt again after cancelling the hotplug works, and dptx_detect() returns -EBUSY while suspended, so a concurrent detect or atomic enable cannot power the host back up during suspend.
- dptx_irq_off() now requires dp->lock and is called with it held from dptx_pm_prepare() and dptx_deinit(), removing the unlocked dp->irq_suspended updates.
- dptx_replay_deferred_hotplug() calls sink_present() under dp->lock and rechecks the pending state, so it cannot issue AUX transfers while dptx_host_deinit() gates the APB clock.
- dptx_bridge_get_modes(), dptx_bridge_mode_valid() and dptx_bridge_atomic_check() read the link capabilities under dp->lock, so they cannot observe a half-refreshed DPCD capability set.
- dptx_wait_phy_dp_mode() aborts on suspend, detach or HPD deassertion instead of polling for ten seconds, the hotplug worker skips detection when the PHY never entered DP mode, and it rechecks dptx_hpd_work_aborted() after detection before notifying, so no hotplug event is reported while suspended or detached.
- dptx_get_irq() rejects the 0 returned by of_irq_get() when the interrupt cannot be mapped.
- Mode validation matches what dptx_bridge_atomic_check() selects:
dptx_bridge_mode_valid() accounts for the 4:2:0/4:2:2 minimum bpp and the adjusted clock, dptx_cal_bpc() rejects YCbCr below 8 bpc, and dptx_adjust_mode() clears both sync polarity flags.
- The hardware layer writes DPTX_ENHANCED_FRAMING_ENABLE with the negotiated value and clears the cause bits that are masked in DPTX_INTERRUPT_MASK before testing them.
- dptx_hpd_notify() keeps the reported state while suspended, dp->hpd_event_work is always queued on system_freezable_wq, and the bridge power management no longer toggles output polling for the whole DRM device.

Changes in v3:
- Move the driver out of drivers/gpu/drm/cix/dptx/ into the shared drivers/gpu/drm/bridge/trilinear/ bridge, leaving only the Sky1 platform data under drivers/gpu/drm/cix/dptx/.
- Split the driver into the hardware layer, the DRM bridge, and the core state machine with the build glue.
- Switch to the connector-less bridge model: the bridge refuses an attach without DRM_BRIDGE_ATTACH_NO_CONNECTOR, and the connector is created by drm_bridge_connector_init() instead of by the driver from .attach().

Luo Liu (5):
dt-bindings: display: add CIX Sky1 DisplayPort transmitter drm/bridge: trilinear: add the DPTX hardware layer drm/bridge: trilinear: add the DPTX DRM bridge drm/bridge: trilinear: add the DPTX core and build glue drm/cix: add the Sky1 DisplayPort platform glue

 .../bindings/display/cix,sky1-dp.yaml         |  102 ++
 MAINTAINERS                                   |   12 +
 drivers/gpu/drm/Kconfig                       |    1 +
 drivers/gpu/drm/Makefile                      |    1 +
 drivers/gpu/drm/bridge/Kconfig                |    2 +
 drivers/gpu/drm/bridge/Makefile               |    1 +
 drivers/gpu/drm/bridge/trilinear/Kconfig      |    9 +
 drivers/gpu/drm/bridge/trilinear/Makefile     |    4 +
 drivers/gpu/drm/bridge/trilinear/dptx.h       |  106 ++
 drivers/gpu/drm/bridge/trilinear/dptx_core.c  | 1101 +++++++++++++
 drivers/gpu/drm/bridge/trilinear/dptx_drm.c   |  664 ++++++++
 drivers/gpu/drm/bridge/trilinear/dptx_drm.h   |   17 +
 drivers/gpu/drm/bridge/trilinear/dptx_hw.c    | 1384 +++++++++++++++++
 drivers/gpu/drm/bridge/trilinear/dptx_hw.h    |  109 ++
 drivers/gpu/drm/cix/Kconfig                   |    3 +
 drivers/gpu/drm/cix/Makefile                  |    3 +
 drivers/gpu/drm/cix/dptx/Kconfig              |   15 +
 drivers/gpu/drm/cix/dptx/Makefile             |    4 +
 drivers/gpu/drm/cix/dptx/cix_dptx.c           |  163 ++
 include/drm/bridge/trilinear_dp.h             |   68 +
 20 files changed, 3769 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/display/cix,sky1-dp.yaml
 create mode 100644 drivers/gpu/drm/bridge/trilinear/Kconfig
 create mode 100644 drivers/gpu/drm/bridge/trilinear/Makefile
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx.h
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_core.c
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_drm.c
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_drm.h
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_hw.c
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_hw.h
 create mode 100644 drivers/gpu/drm/cix/Kconfig
 create mode 100644 drivers/gpu/drm/cix/Makefile
 create mode 100644 drivers/gpu/drm/cix/dptx/Kconfig
 create mode 100644 drivers/gpu/drm/cix/dptx/Makefile
 create mode 100644 drivers/gpu/drm/cix/dptx/cix_dptx.c
 create mode 100644 include/drm/bridge/trilinear_dp.h
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v4 1/5 dt-bindings: display: add CIX Sky1 DisplayPort transmitter

Add a binding for the Trilinear DisplayPort transmitter integrated on CIX Sky1. The USB-C and the native DP/eDP transmitters use different RCSU video-adapter register offsets and bit layouts, so each has its own compatible.

The transmitter takes its video stream from the display pipeline through a single graph port and drives the link through a generic PHY.

---
 .../bindings/display/cix,sky1-dp.yaml         | 102 ++++++++++++++++++
 MAINTAINERS                                   |  12 +++
 2 files changed, 114 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/display/cix,sky1-dp.yaml
Documentation/devicetree/bindings/display/cix,sky1-dp.yaml new file +102 -0
new file mode 100644
index 000000000000..5095d5daf51a
@@ -0,0 +1,102 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/display/cix,sky1-dp.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: CIX Sky1 Trilinear DisplayPort Transmitter
+
+maintainers:
+  - Luo Liu <[email protected]>
+
+description: |
+  Trilinear DisplayPort transmitter on CIX Sky1. The transmitter takes its
+  video stream from the display pipeline through a single graph port and
+  drives the link through a generic PHY.
+
+  The USB-C transmitters and the native DP/eDP transmitter use different
+  RCSU video-adapter register offsets and bit layouts, so each has its own
+  compatible: "cix,sky1-usbdp" for the USB-C transmitters and "cix,sky1-dp"
+  for the native DP/eDP one.
+
+properties:
+  compatible:
+    enum:
+      - cix,sky1-usbdp
+      - cix,sky1-dp
+
+  reg:
+    items:
+      - description: DisplayPort transmitter registers.
+      - description: RCSU video adapter registers used for link setup.
+
+  reg-names:
+    items:
+      - const: dp
+      - const: rcsu
+
+  clocks:
+    items:
+      - description: APB clock of the controller.
+      - description: Video clock of stream 0.
+      - description: Video clock of stream 1.
+
+  clock-names:
+    items:
+      - const: apb
+      - const: vid0
+      - const: vid1
+
+  interrupts:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+    description: DisplayPort controller reset.
+
+  phys:
+    maxItems: 1
+    description: DisplayPort PHY the controller transmits through.
+
+  port:
+    $ref: /schemas/graph.yaml#/properties/port
+    description:
+      Video input from the Mali D71 display pipeline (Komeda) that drives
+      this controller. The link to the sink is through phys (USB-C combo PHY
+      or the native DP PHY), not a second OF graph port on the DPTX.
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - clocks
+  - clock-names
+  - interrupts
+  - resets
+  - phys
+  - port
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    dp0: dp@14064000 {
+        compatible = "cix,sky1-usbdp";
+        reg = <0x14064000 0x4000>,
+              <0x14050000 0x400>;
+        reg-names = "dp", "rcsu";
+        clocks = <&dpc0_apb>, <&dpc0_vid0>, <&dpc0_vid1>;
+        clock-names = "apb", "vid0", "vid1";
+        interrupts = <GIC_SPI 332 IRQ_TYPE_LEVEL_HIGH>;
+        resets = <&dpc0_rst>;
+        phys = <&usbc0_dp_phy>;
+
+        port {
+            dp0_0_in: endpoint {
+                remote-endpoint = <&dpu0_pipe0_out>;
+            };
+        };
+    };
MAINTAINERS +12 -0
index 93abbb315065..468aa1ef6eb7 100644
@@ -6522,6 +6522,18 @@ M:	Satish Kharat <[email protected]>
 S:	Supported
 F:	drivers/infiniband/hw/usnic/
 
+CIX SKY1 DISPLAYPORT TRANSMITTER DRIVER
+M:	Luo Liu <[email protected]>
+M:	Wuyi Shen <[email protected]>
+R:	CIX Linux Kernel Upstream Group <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/display/cix,sky1-dp.yaml
+F:	drivers/gpu/drm/bridge/trilinear/
+F:	drivers/gpu/drm/cix/dptx/
+F:	include/drm/bridge/trilinear_dp.h
+
 CLANG CONTEXT ANALYSIS
 M:	Marco Elver <[email protected]>
 R:	Bart Van Assche <[email protected]>
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v4 1/5] dt-bindings: display: add CIX Sky1 DisplayPort transmitter

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] dt-bindings: display: cix,sky1-dp: missing output port and aux-bus in schema - [Low] dt-bindings…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] dt-bindings: display: cix,sky1-dp: missing output port and aux-bus in schema
- [Low] dt-bindings: display: cix,sky1-dp: unused labels in DTS example --

commit f212999ac361089d7da2601c852a0ab9252b18bc
Author: Luo Liu <[email protected]>

dt-bindings: display: add CIX Sky1 DisplayPort transmitter

This commit adds a Device Tree binding schema for the Trilinear DisplayPort transmitter integrated on CIX Sky1 SoCs.

Quoted 6 lines
Documentation/devicetree/bindings/display/cix,sky1-dp.yaml new file +0 -0
new file mode 100644
index 0000000000000..5095d5daf51ac
@@ -0,0 +1,102 @@

[ ... ]

Quoted 9 lines
Documentation/devicetree/bindings/display/cix,sky1-dp.yaml new file +9 -0
+  port:
+    $ref: /schemas/graph.yaml#/properties/port
+    description:
+      Video input from the Mali D71 display pipeline (Komeda) that drives
+      this controller. The link to the sink is through phys (USB-C combo PHY
+      or the native DP PHY), not a second OF graph port on the DPTX.
+
+required:
+  - compatible

Documentation/devicetree/bindings/display/cix,sky1-dp.yaml line 80

Quoted 10 lines
Documentation/devicetree/bindings/display/cix,sky1-dp.yaml new file +10 -0
+  - reg
+  - reg-names
+  - clocks
+  - clock-names
+  - interrupts
+  - resets
+  - phys
+  - port
+
+additionalProperties: false

[Severity: Medium]
Should this schema define ports (with port@0 for input and port@1 for output) and aux-bus instead of a single unnumbered port?

The phys property only references the local SoC PHY provider and does not describe the downstream display sink or AUX channel topology. Allowing only a single input port with additionalProperties: false prevents board device trees from connecting downstream DP connectors, eDP panels under aux-bus, or USB-C display graph endpoints without a later ABI-breaking change from port to ports.

Quoted 12 lines
Documentation/devicetree/bindings/display/cix,sky1-dp.yaml new file +12 -0
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    dp0: dp@14064000 {
+        compatible = "cix,sky1-usbdp";
+        reg = <0x14064000 0x4000>,
+              <0x14050000 0x400>;
+        reg-names = "dp", "rcsu";
+        clocks = <&dpc0_apb>, <&dpc0_vid0>, <&dpc0_vid1>;
+        clock-names = "apb", "vid0", "vid1";

Documentation/devicetree/bindings/display/cix,sky1-dp.yaml line 102

Quoted 10 lines
Documentation/devicetree/bindings/display/cix,sky1-dp.yaml new file +10 -0
+        interrupts = <GIC_SPI 332 IRQ_TYPE_LEVEL_HIGH>;
+        resets = <&dpc0_rst>;
+        phys = <&usbc0_dp_phy>;
+
+        port {
+            dp0_0_in: endpoint {
+                remote-endpoint = <&dpu0_pipe0_out>;
+            };
+        };
+    };

[Severity: Low]
Can the dp0 and dp0_0_in labels be dropped from the example? Neither is referenced, which triggers unused-labels warnings when running scripts/dtc/dt-check-style --mode=strict.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v4 2/5 drm/bridge: trilinear: add the DPTX hardware layer

In reply to Luo Liu

Add the register and PHY programming for the Trilinear DisplayPort
transmitter: PHY power and reset, host and core enable, AUX transfers through the controller's AUX channel, sink capability parsing from the DPCD, and link training with rate downshifting when the sink cannot lock.

The transmitter drives up to four lanes at HBR3 and has two SST sources. The stream setup programs the main stream attributes, the pixel format and the optional VSC SDP per stream. MST is not implemented and is left disabled.

---
 drivers/gpu/drm/bridge/trilinear/dptx_hw.c | 1384 ++++++++++++++++++++
 drivers/gpu/drm/bridge/trilinear/dptx_hw.h |  109 ++
 2 files changed, 1493 insertions(+)
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_hw.c
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_hw.h
drivers/gpu/drm/bridge/trilinear/dptx_hw.c new file +1384 -0
new file mode 100644
index 000000000000..8bbc97a7d970
@@ -0,0 +1,1384 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * DisplayPort transmitter hardware interface
+ *
+ * Copyright (C) 2026 Trilinear Technologies
+ * Copyright 2026 Cix Technology Group Co., Ltd.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/iopoll.h>
+#include <linux/phy/phy.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+
+#include <drm/display/drm_dp_helper.h>
+
+#include "dptx_hw.h"
+
+#define DPTX_SDP_DB_MAX 32
+#define DP_MAX_TRAINING_TRIES 5
+#define DP_APB_CLK_RATE 200000000UL
+
+/* ------------------------------------------------------------------------ */
+/*  Register offsets */
+/* ------------------------------------------------------------------------ */
+/* link configuration */
+#define DPTX_LINK_BW_SET			0x000ul
+#define DPTX_LANE_COUNT_SET		0x004ul
+#define DPTX_ENHANCED_FRAMING_ENABLE	0x008ul
+#define DPTX_TRAINING_PATTERN_SET	0x00cul
+#define DPTX_DISABLE_SCRAMBLING		0x014ul
+#define DPTX_DOWNSPREAD_CONTROL		0x018ul
+
+/* core enables */
+#define DPTX_TRANSMITTER_ENABLE	0x080ul
+#define DPTX_SOFT_RESET			0x090ul
+#define DPTX_SOURCE_ENABLE		0x094ul
+
+/* misc control */
+#define DPTX_FORCE_SCRAMBLER_RESET	0x0c0ul
+
+/* AUX channel */
+#define DPTX_AUX_COMMAND			0x100ul
+#define DPTX_AUX_WRITE_FIFO		0x104ul
+#define DPTX_AUX_ADDRESS			0x108ul
+#define DPTX_AUX_CLOCK_DIVIDER	0x10cul
+#define DPTX_HPD_INPUT_STATE		0x128ul
+#define DPTX_AUX_REPLY_DATA		0x134ul
+#define DPTX_AUX_REPLY_CODE		0x138ul
+#define DPTX_INTERRUPT_CAUSE		0x140ul
+#define DPTX_INTERRUPT_MASK		0x144ul
+#define DPTX_AUX_REPLY_DATA_COUNT	0x148ul
+#define DPTX_AUX_STATUS			0x14cul
+
+/* PHY status */
+#define DPTX_PHY_STATUS			0x280ul
+
+/* MST / SST control */
+#define DPTX_MST_ENABLE			0x500ul
+#define DPTX_SST_SOURCE_SELECT		0x50cul
+
+/* main stream control, virtual source 0 (bases for per-stream helpers) */
+#define DPTX_SRC0_VIDEO_STREAM_ENABLE	0x800ul
+#define DPTX_SRC0_SECONDARY_STREAM_ENABLE	0x804ul
+#define DPTX_SRC0_SECONDARY_DATA_WINDOW	0x808ul
+#define DPTX_SRC0_DATA_CONTROL		0x810ul
+#define DPTX_SRC0_MAIN_STREAM_OVERRIDE	0x814ul
+#define DPTX_SRC0_MAIN_STREAM_HTOTAL	0x820ul
+#define DPTX_SRC0_MAIN_STREAM_VTOTAL	0x824ul
+#define DPTX_SRC0_MAIN_STREAM_POLARITY	0x828ul
+#define DPTX_SRC0_MAIN_STREAM_HSWIDTH	0x82cul
+#define DPTX_SRC0_MAIN_STREAM_VSWIDTH	0x830ul
+#define DPTX_SRC0_MAIN_STREAM_HRES	0x834ul
+#define DPTX_SRC0_MAIN_STREAM_VRES	0x838ul
+#define DPTX_SRC0_MAIN_STREAM_HSTART	0x83cul
+#define DPTX_SRC0_MAIN_STREAM_VSTART	0x840ul
+#define DPTX_SRC0_MAIN_STREAM_MISC0	0x844ul
+#define DPTX_SRC0_MAIN_STREAM_MISC1	0x848ul
+#define DPTX_SRC0_TU_CONFIG		0x850ul
+#define DPTX_SRC0_USER_PIXEL_COUNT	0x858ul
+#define DPTX_SRC0_USER_DATA_COUNT	0x85cul
+#define DPTX_SRC0_USER_SYNC_POLARITY	0x864ul
+
+/* secondary channel, source 0 */
+#define DPTX_SEC0_INFOFRAME_ENABLE	0x910ul
+#define DPTX_SEC0_INFOFRAME_RATE		0x914ul
+#define DPTX_SEC0_INFOFRAME_SELECT	0x934ul
+#define DPTX_SEC0_INFOFRAME_DATA		0x938ul
+
+/* spacing between consecutive virtual source register blocks */
+#define DPTX_SOURCE_OFFSET		0x200ul
+
+/* per-stream helpers, indexed by source/stream id */
+#define DPTX_INFOFRAME_ENABLE(stream_id) \
+	(DPTX_SEC0_INFOFRAME_ENABLE + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_INFOFRAME_SELECT(stream_id) \
+	(DPTX_SEC0_INFOFRAME_SELECT + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_INFOFRAME_RATE(stream_id) \
+	(DPTX_SEC0_INFOFRAME_RATE + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_INFOFRAME_DATA(stream_id) \
+	(DPTX_SEC0_INFOFRAME_DATA + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_VIDEO_STREAM_ENABLE(stream_id) \
+	(DPTX_SRC0_VIDEO_STREAM_ENABLE + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_SECONDARY_STREAM_ENABLE(stream_id) \
+	(DPTX_SRC0_SECONDARY_STREAM_ENABLE + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_MISC0(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_MISC0 + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_MISC1(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_MISC1 + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_OVERRIDE(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_OVERRIDE + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_HTOTAL(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_HTOTAL + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_VTOTAL(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_VTOTAL + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_POLARITY(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_POLARITY + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_HSWIDTH(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_HSWIDTH + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_VSWIDTH(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_VSWIDTH + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_HRES(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_HRES + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_VRES(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_VRES + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_HSTART(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_HSTART + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_MAIN_STREAM_VSTART(stream_id) \
+	(DPTX_SRC0_MAIN_STREAM_VSTART + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_TU_CONFIG(stream_id) \
+	(DPTX_SRC0_TU_CONFIG + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_USER_DATA_COUNT(stream_id) \
+	(DPTX_SRC0_USER_DATA_COUNT + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_SECONDARY_DATA_WINDOW(stream_id) \
+	(DPTX_SRC0_SECONDARY_DATA_WINDOW + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_DATA_CONTROL(stream_id) \
+	(DPTX_SRC0_DATA_CONTROL + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_USER_PIXEL_COUNT(stream_id) \
+	(DPTX_SRC0_USER_PIXEL_COUNT + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+#define DPTX_USER_SYNC_POLARITY(stream_id) \
+	(DPTX_SRC0_USER_SYNC_POLARITY + \
+	 DPTX_SOURCE_OFFSET * (stream_id))
+
+/* ------------------------------------------------------------------------ */
+/*  Register DPTX_MAIN_STREAM_MISC0 bits */
+/* ------------------------------------------------------------------------ */
+#define DPTX_STREAM_MISC0_COMP_FORMAT_RGB (0 << 1)
+#define DPTX_STREAM_MISC0_COMP_FORMAT_YCRCB_422 (5 << 1)
+#define DPTX_STREAM_MISC0_COMP_FORMAT_YCRCB_444 (6 << 1)
+#define DPTX_STREAM_MISC0_BPC_6 (0 << 5)
+#define DPTX_STREAM_MISC0_BPC_8 BIT(5)
+#define DPTX_STREAM_MISC0_BPC_10 (2 << 5)
+#define DPTX_STREAM_MISC0_BPC_12 (3 << 5)
+#define DPTX_STREAM_MISC0_BPC_16 (4 << 5)
+
+/* ------------------------------------------------------------------------ */
+/*  Register DPTX_MAIN_STREAM_MISC1 bits */
+/* ------------------------------------------------------------------------ */
+#define DPTX_STREAM_MISC1_Y_ONLY_EN BIT(7)
+
+/* ------------------------------------------------------------------------ */
+/*  Register DPTX_MAIN_STREAM_OVERRIDE bits */
+/* ------------------------------------------------------------------------ */
+#define DPTX_VSC_COLORIMETRY_EN BIT(6)
+#define DPTX_STREAM_OVERRIDE_ENABLE BIT(6)
+/*
+ * Pixel format in bits [5:3]. 0 is RGB. 4:2:2 and 4:4:4 use the same
+ * component-format codes as MISC0 bits [2:1]. 4:2:0 cannot be carried
+ * in MISC0, so it has its own value.
+ */
+#define DPTX_STREAM_OVERRIDE_YCbCR_422 BIT(3)
+#define DPTX_STREAM_OVERRIDE_YCbCR_444 (2 << 3)
+#define DPTX_STREAM_OVERRIDE_YCbCR_420 (4 << 3)
+#define DPTX_STREAM_OVERRIDE_BPC_6 (0)
+#define DPTX_STREAM_OVERRIDE_BPC_8 (1)
+#define DPTX_STREAM_OVERRIDE_BPC_10 (2)
+#define DPTX_STREAM_OVERRIDE_BPC_12 (3)
+#define DPTX_STREAM_OVERRIDE_BPC_16 (4)
+
+/* ------------------------------------------------------------------------ */
+/*  Register DPTX_AUX_COMMAND bits */
+/* ------------------------------------------------------------------------ */
+#define DPTX_AUX_COMMAND_CMD_SHIFT 8
+#define DPTX_AUX_COMMAND_ADDRESS_ONLY BIT(12)
+#define DPTX_AUX_COMMAND_BYTES_SHIFT 0
+#define DPTX_AUX_STATUS_REPLY_ERROR BIT(3)
+#define DPTX_AUX_STATUS_REQUEST_IN_PROGRESS BIT(2)
+#define DPTX_AUX_STATUS_REPLY_IN_PROGRESS BIT(1)
+#define DPTX_AUX_STATUS_REPLY_RECEIVED BIT(0)
+
+#define DPTX_AUX_READ BIT(0)
+
+#define DPTX_AUX_REPLY_CODE_AUX_ACK 0
+#define DPTX_AUX_REPLY_CODE_I2C_ACK 0
+
+/* ------------------------------------------------------------------------ */
+/*  Register DPTX_REPLY_DATA_COUNT bits */
+/* ------------------------------------------------------------------------ */
+#define DPTX_REPLY_DATA_COUNT_MASK 0xff
+
+#define DPTX_MSA_TRANSFER_UNIT_SIZE_TU_SIZE_DEF 64
+#define DPTX_MAIN_STREAM_POLARITY_HSYNC_SHIFT 0
+#define DPTX_MAIN_STREAM_POLARITY_VSYNC_SHIFT 1
+#define DPTX_MAIN_STREAM_POLARITY_DATAENABLE_HIGH 0xc
+
+#define DPTX_DATA_CONTROL_DEFAULT		(BIT(13) | BIT(2))
+#define DPTX_DATA_CONTROL_2PPC_4LANE_HBR3	BIT(2)
+
+#define DPTX_INTERRUPT_GP_TIMER_IRQ BIT(4)
+#define DPTX_INTERRUPT_REPLY_TIMEOUT BIT(3)
+#define DPTX_INTERRUPT_REPLY_RECEIVED BIT(2)
+#define DPTX_INTERRUPT_HPD_IRQ BIT(1)
+#define DPTX_INTERRUPT_HPD_EVENT BIT(0)
+
+#define DPTX_INTERRUPT_MASK_ALL 0x3f
+#define DPTX_INTERRUPT_CFG 0x1c
+
+static const char * const stream_clk_names[] = {
+	"vid0", "vid1",
+};
+
+static u32 dptx_hw_read(struct dp_hw *hw, int offset)
+{
+	return readl(hw->base + offset);
+}
+
+static void dptx_hw_write(struct dp_hw *hw, int offset, u32 val)
+{
+	writel(val, hw->base + offset);
+}
+
+static bool dptx_hw_hpd_state(struct dp_hw *hw)
+{
+	return dptx_hw_read(hw, DPTX_HPD_INPUT_STATE) & BIT(0);
+}
+
+static bool dptx_hw_sink_present(struct dp_hw *hw)
+{
+	u8 dpcd_rev;
+
+	return drm_dp_dpcd_readb(&hw->aux, DP_DPCD_REV, &dpcd_rev) >= 0;
+}
+
+static int dptx_hw_phy_reset(struct dp_hw *hw)
+{
+	return phy_reset(hw->phy);
+}
+
+static int dptx_hw_phy_init(struct dp_hw *hw)
+{
+	return phy_init(hw->phy);
+}
+
+static void dptx_hw_phy_exit(struct dp_hw *hw)
+{
+	phy_exit(hw->phy);
+}
+
+static int dptx_hw_phy_power(struct dp_hw *hw, bool enable)
+{
+	if (enable)
+		return phy_power_on(hw->phy);
+
+	phy_power_off(hw->phy);
+	return 0;
+}
+
+static int dptx_hw_phy_vs_emph_set(struct dp_hw *hw, u8 lane_cnt,
+				   const u8 *train_set)
+{
+	union phy_configure_opts opts = { 0 };
+	u8 train;
+	int i;
+
+	opts.dp.lanes = lane_cnt;
+	for (i = 0; i < lane_cnt; i++) {
+		train = train_set[i];
+		opts.dp.voltage[i] = (train & DP_TRAIN_VOLTAGE_SWING_MASK) >>
+				     DP_TRAIN_VOLTAGE_SWING_SHIFT;
+		opts.dp.pre[i] = (train & DP_TRAIN_PRE_EMPHASIS_MASK) >>
+				 DP_TRAIN_PRE_EMPHASIS_SHIFT;
+	}
+
+	opts.dp.set_voltages = 1;
+	return phy_configure(hw->phy, &opts);
+}
+
+static enum phy_mode dptx_hw_phy_mode(struct dp_hw *hw)
+{
+	return phy_get_mode(hw->phy);
+}
+
+static void dptx_hw_aux_clock_divider(struct dp_hw *hw)
+{
+	unsigned int rate;
+
+	rate = clk_get_rate(hw->apb_clk) / (1000 * 1000);
+	if (!rate)
+		rate = 25;
+
+	dptx_hw_write(hw, DPTX_AUX_CLOCK_DIVIDER, rate);
+}
+
+static int dptx_hw_apb_clock(struct dp_hw *hw, bool enable)
+{
+	int ret;
+
+	if (IS_ERR_OR_NULL(hw->apb_clk))
+		return 0;
+
+	if (!enable) {
+		clk_disable_unprepare(hw->apb_clk);
+		return 0;
+	}
+
+	if (!clk_get_rate(hw->apb_clk)) {
+		ret = clk_set_rate(hw->apb_clk, DP_APB_CLK_RATE);
+		if (ret) {
+			dev_err(hw->dev, "failed to set apb clock rate: %d\n",
+				ret);
+			return ret;
+		}
+	}
+
+	return clk_prepare_enable(hw->apb_clk);
+}
+
+static void dptx_hw_reset(struct dp_hw *hw, bool assert)
+{
+	if (assert)
+		reset_control_assert(hw->reset);
+	else
+		reset_control_deassert(hw->reset);
+}
+
+static void dptx_hw_host_enable(struct dp_hw *hw, bool enable)
+{
+	if (enable) {
+		dptx_hw_write(hw, DPTX_TRANSMITTER_ENABLE, 1);
+		dptx_hw_write(hw, DPTX_INTERRUPT_MASK,
+			      DPTX_INTERRUPT_CFG);
+		dptx_hw_aux_clock_divider(hw);
+		return;
+	}
+
+	dptx_hw_write(hw, DPTX_TRANSMITTER_ENABLE, 0);
+	dptx_hw_write(hw, DPTX_INTERRUPT_MASK,
+		      DPTX_INTERRUPT_MASK_ALL);
+}
+
+static void dptx_hw_core_enable(struct dp_hw *hw, int source_id,
+				bool enable)
+{
+	u32 sources = BIT(source_id);
+	u32 value;
+
+	if (enable) {
+		dptx_hw_write(hw, DPTX_SOFT_RESET, sources);
+		dptx_hw_write(hw, DPTX_SOURCE_ENABLE, sources);
+		dptx_hw_write(hw, DPTX_FORCE_SCRAMBLER_RESET, sources);
+		dptx_hw_write(hw, DPTX_MST_ENABLE, 0);
+		dptx_hw_write(hw, DPTX_SST_SOURCE_SELECT, source_id);
+		return;
+	}
+
+	dptx_hw_write(hw, DPTX_MST_ENABLE, 0);
+
+	value = dptx_hw_read(hw, DPTX_SOURCE_ENABLE);
+	dptx_hw_write(hw, DPTX_SOURCE_ENABLE, value & ~sources);
+
+	value = dptx_hw_read(hw, DPTX_SOFT_RESET);
+	dptx_hw_write(hw, DPTX_SOFT_RESET, value & ~sources);
+
+	value = dptx_hw_read(hw, DPTX_FORCE_SCRAMBLER_RESET);
+	dptx_hw_write(hw, DPTX_FORCE_SCRAMBLER_RESET, value & ~sources);
+}
+
+static void dptx_hw_link_setup(struct dp_hw *hw, u8 lane_cnt,
+			       bool enhanced, bool downspread)
+{
+	dptx_hw_write(hw, DPTX_LANE_COUNT_SET, lane_cnt);
+	dptx_hw_write(hw, DPTX_ENHANCED_FRAMING_ENABLE, enhanced ? 1 : 0);
+	dptx_hw_write(hw, DPTX_DOWNSPREAD_CONTROL, downspread);
+}
+
+static int dptx_hw_link_rate(struct dp_hw *hw, u8 bw_code, u8 lanes)
+{
+	union phy_configure_opts opts = { 0 };
+	u32 ready = (1 << lanes) - 1;
+	u32 value;
+	int ret;
+
+	dptx_hw_write(hw, DPTX_LINK_BW_SET, bw_code);
+
+	opts.dp.link_rate = drm_dp_bw_code_to_link_rate(bw_code) / 100;
+	opts.dp.set_rate = 1;
+	ret = phy_configure(hw->phy, &opts);
+	if (ret)
+		return ret;
+
+	return readl_poll_timeout(hw->base + DPTX_PHY_STATUS, value,
+				  (value & ready) == ready, 1000, 100 * 1000);
+}
+
+static int dptx_hw_link_configure(struct dp_hw *hw, u8 bw_code,
+				  u8 lane_cnt, bool enhanced,
+				  u8 num_lanes)
+{
+	bool downspread;
+	int ret;
+
+	downspread = hw->dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5;
+
+	dptx_hw_link_setup(hw, lane_cnt, enhanced, downspread);
+
+	if (enhanced)
+		lane_cnt |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
+
+	drm_dp_dpcd_writeb(&hw->aux, DP_DOWNSPREAD_CTRL,
+			   downspread ? DP_SPREAD_AMP_0_5 : 0);
+
+	ret = drm_dp_dpcd_writeb(&hw->aux, DP_LANE_COUNT_SET, lane_cnt);
+	if (ret < 0)
+		return ret;
+
+	ret = drm_dp_dpcd_writeb(&hw->aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
+				 DP_SET_ANSI_8B10B);
+	if (ret < 0)
+		return ret;
+
+	ret = drm_dp_dpcd_writeb(&hw->aux, DP_LINK_BW_SET, bw_code);
+	if (ret < 0)
+		return ret;
+
+	return dptx_hw_link_rate(hw, bw_code, num_lanes);
+}
+
+static int dptx_hw_link_set_pattern(struct dp_hw *hw, u32 pattern)
+{
+	if (pattern == DP_TRAINING_PATTERN_4) {
+		dptx_hw_write(hw, DPTX_TRAINING_PATTERN_SET, 0x4);
+		dptx_hw_write(hw, DPTX_DISABLE_SCRAMBLING, 0);
+	} else {
+		dptx_hw_write(hw, DPTX_TRAINING_PATTERN_SET, pattern);
+		pattern |= DP_LINK_SCRAMBLING_DISABLE;
+	}
+
+	return drm_dp_dpcd_writeb(&hw->aux, DP_TRAINING_PATTERN_SET, pattern);
+}
+
+static int dptx_hw_link_vs_emph(struct dp_hw *hw, u8 lane_cnt)
+{
+	int ret;
+
+	ret = drm_dp_dpcd_write(&hw->aux, DP_TRAINING_LANE0_SET, hw->train_setting,
+				lane_cnt);
+	if (ret < 0)
+		return ret;
+
+	return dptx_hw_phy_vs_emph_set(hw, lane_cnt, hw->train_setting);
+}
+
+static void dptx_hw_link_adjust_train(struct dp_hw *hw, u8 lane_cnt,
+				      const u8 *link_status)
+{
+	u8 voltage;
+	u8 preemphasis;
+	int i;
+
+	for (i = 0; i < lane_cnt; i++) {
+		voltage = drm_dp_get_adjust_request_voltage(link_status, i);
+		preemphasis =
+			drm_dp_get_adjust_request_pre_emphasis(link_status, i);
+
+		if (voltage >= DP_TRAIN_VOLTAGE_SWING_LEVEL_3)
+			voltage |= DP_TRAIN_MAX_SWING_REACHED;
+
+		if (preemphasis >= DP_TRAIN_PRE_EMPH_LEVEL_2)
+			preemphasis |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
+
+		hw->train_setting[i] = voltage | preemphasis;
+	}
+}
+
+static int dptx_hw_link_clock_recovery(struct dp_hw *hw, u8 lane_cnt)
+{
+	u8 link_status[DP_LINK_STATUS_SIZE];
+	bool train_done = false;
+	u8 prev = 0;
+	int tries = 0;
+	int i;
+	int ret;
+
+	ret = dptx_hw_link_set_pattern(hw, DP_TRAINING_PATTERN_1);
+	if (ret < 0)
+		return ret;
+
+	for (i = 0; i < 512; i++) {
+		ret = dptx_hw_link_vs_emph(hw, lane_cnt);
+		if (ret)
+			return ret;
+
+		drm_dp_link_train_clock_recovery_delay(&hw->aux, hw->dpcd);
+		ret = drm_dp_dpcd_read_link_status(&hw->aux, link_status);
+		if (ret < 0)
+			return ret;
+
+		train_done = drm_dp_clock_recovery_ok(link_status, lane_cnt);
+		if (train_done)
+			break;
+
+		if ((hw->train_setting[0] &
+		     (DP_TRAIN_VOLTAGE_SWING_MASK |
+		      DP_TRAIN_PRE_EMPHASIS_MASK)) == prev)
+			tries++;
+		else
+			tries = 0;
+
+		if (tries == DP_MAX_TRAINING_TRIES)
+			break;
+
+		prev = hw->train_setting[0] &
+		       (DP_TRAIN_VOLTAGE_SWING_MASK |
+			DP_TRAIN_PRE_EMPHASIS_MASK);
+		dptx_hw_link_adjust_train(hw, lane_cnt, link_status);
+	}
+
+	if (train_done)
+		return 0;
+
+	return -EIO;
+}
+
+static int dptx_hw_link_channel_equalization(struct dp_hw *hw,
+					     u8 lane_cnt, bool tps3,
+					     bool tps4)
+{
+	u8 link_status[DP_LINK_STATUS_SIZE];
+	bool train_done = false;
+	u32 pattern;
+	int i;
+	int ret;
+
+	if (tps4)
+		pattern = DP_TRAINING_PATTERN_4;
+	else if (tps3)
+		pattern = DP_TRAINING_PATTERN_3;
+	else
+		pattern = DP_TRAINING_PATTERN_2;
+
+	ret = dptx_hw_link_set_pattern(hw, pattern);
+	if (ret < 0)
+		return ret;
+
+	for (i = 0; i < DP_MAX_TRAINING_TRIES; i++) {
+		ret = dptx_hw_link_vs_emph(hw, lane_cnt);
+		if (ret)
+			return ret;
+
+		drm_dp_link_train_channel_eq_delay(&hw->aux, hw->dpcd);
+		ret = drm_dp_dpcd_read_link_status(&hw->aux, link_status);
+		if (ret < 0)
+			return ret;
+
+		train_done = drm_dp_channel_eq_ok(link_status, lane_cnt);
+		if (train_done)
+			break;
+
+		dptx_hw_link_adjust_train(hw, lane_cnt, link_status);
+	}
+
+	if (train_done)
+		return 0;
+
+	return -EIO;
+}
+
+static void dptx_hw_link_train_start(struct dp_hw *hw)
+{
+	dptx_hw_write(hw, DPTX_DISABLE_SCRAMBLING, 1);
+	memset(hw->train_setting, 0, sizeof(hw->train_setting));
+}
+
+static void dptx_hw_link_train_end(struct dp_hw *hw)
+{
+	drm_dp_dpcd_writeb(&hw->aux, DP_TRAINING_PATTERN_SET, 0);
+	dptx_hw_write(hw, DPTX_TRAINING_PATTERN_SET, 0);
+	dptx_hw_write(hw, DPTX_DISABLE_SCRAMBLING, 0);
+}
+
+static int dptx_hw_link_train(struct dp_hw *hw, u8 bw_code,
+			      u8 lane_cnt, u8 num_lanes)
+{
+	int ret;
+
+	ret = dptx_hw_link_configure(hw, bw_code, lane_cnt,
+				     hw->caps.enhanced_framing, num_lanes);
+	if (ret < 0)
+		return ret;
+
+	dptx_hw_link_train_start(hw);
+
+	ret = dptx_hw_link_clock_recovery(hw, lane_cnt);
+	if (!ret)
+		ret = dptx_hw_link_channel_equalization(hw, lane_cnt,
+							hw->caps.tps3_supported,
+							hw->caps.tps4_supported);
+
+	dptx_hw_link_train_end(hw);
+
+	return ret;
+}
+
+static int dptx_hw_link_status(struct dp_hw *hw, u8 lane_cnt)
+{
+	u8 status[DP_LINK_STATUS_SIZE + 2];
+	ssize_t ret;
+
+	ret = drm_dp_dpcd_read(&hw->aux, DP_SINK_COUNT, status,
+			       sizeof(status));
+	if (ret != (ssize_t)sizeof(status))
+		return ret < 0 ? ret : -EIO;
+
+	if (drm_dp_clock_recovery_ok(&status[2], lane_cnt) &&
+	    drm_dp_channel_eq_ok(&status[2], lane_cnt))
+		return 0;
+
+	return 1;
+}
+
+static int dptx_hw_aux_submit(struct dp_hw *hw, u32 cmd, u32 addr,
+			      const u8 *buf, u32 len)
+{
+	bool is_read = cmd & DPTX_AUX_READ;
+	u32 value;
+	int i;
+	int ret;
+
+	value = dptx_hw_read(hw, DPTX_AUX_STATUS);
+	if (value & (DPTX_AUX_STATUS_REQUEST_IN_PROGRESS |
+		     DPTX_AUX_STATUS_REPLY_IN_PROGRESS))
+		return -EBUSY;
+
+	dptx_hw_write(hw, DPTX_AUX_ADDRESS, addr);
+
+	if (!is_read)
+		for (i = 0; i < len; i++)
+			dptx_hw_write(hw, DPTX_AUX_WRITE_FIFO,
+				      buf[i]);
+
+	value = cmd << DPTX_AUX_COMMAND_CMD_SHIFT;
+	if (!buf || !len)
+		value |= DPTX_AUX_COMMAND_ADDRESS_ONLY;
+	else
+		value |= (len - 1) << DPTX_AUX_COMMAND_BYTES_SHIFT;
+
+	dptx_hw_write(hw, DPTX_AUX_COMMAND, value);
+
+	ret = readl_poll_timeout(hw->base + DPTX_AUX_STATUS, value,
+				 value & (DPTX_AUX_STATUS_REPLY_RECEIVED |
+					  DPTX_AUX_STATUS_REPLY_ERROR),
+				 100, 4000);
+	if (ret)
+		return -ETIMEDOUT;
+
+	/*
+	 * REPLY_ERROR is the block's reply timeout. NACK and DEFER come back
+	 * as a reply with a non-zero reply code, which the caller reads.
+	 */
+	if (value & DPTX_AUX_STATUS_REPLY_ERROR)
+		return -ETIMEDOUT;
+
+	return 0;
+}
+
+static void dptx_hw_aux_reply_data(struct dp_hw *hw, u8 *buf, u32 len)
+{
+	int i;
+
+	for (i = 0; i < len; i++)
+		buf[i] = dptx_hw_read(hw, DPTX_AUX_REPLY_DATA);
+}
+
+static int dptx_hw_aux_cmd_submit(struct dp_hw *hw, u32 cmd, u32 addr,
+				  u8 *buf, u32 bytes, u8 *reply)
+{
+	bool is_read = cmd & DPTX_AUX_READ;
+	u32 reply_code;
+	int ret;
+
+	ret = dptx_hw_aux_submit(hw, cmd, addr, buf, bytes);
+	if (ret)
+		return ret;
+
+	reply_code = dptx_hw_read(hw, DPTX_AUX_REPLY_CODE);
+	if (reply)
+		*reply = reply_code;
+
+	if (is_read &&
+	    (reply_code == DPTX_AUX_REPLY_CODE_AUX_ACK ||
+	     reply_code == DPTX_AUX_REPLY_CODE_I2C_ACK)) {
+		if ((dptx_hw_read(hw, DPTX_AUX_REPLY_DATA_COUNT) &
+		     DPTX_REPLY_DATA_COUNT_MASK) != bytes)
+			return -EIO;
+
+		dptx_hw_aux_reply_data(hw, buf, bytes);
+	}
+
+	return 0;
+}
+
+static ssize_t dptx_hw_aux_transfer(struct drm_dp_aux *aux,
+				    struct drm_dp_aux_msg *msg)
+{
+	struct dp_hw *hw = container_of(aux, struct dp_hw, aux);
+	size_t to_access = 0;
+	size_t accessed_bytes = 0;
+	u32 reply_mask;
+	u32 reply_ack;
+	ssize_t ret;
+
+	switch (msg->request) {
+	case DP_AUX_NATIVE_WRITE:
+	case DP_AUX_NATIVE_READ:
+		reply_mask = DP_AUX_NATIVE_REPLY_MASK;
+		reply_ack = DP_AUX_NATIVE_REPLY_ACK;
+		break;
+	default:
+		reply_mask = DP_AUX_NATIVE_REPLY_MASK | DP_AUX_I2C_REPLY_MASK;
+		reply_ack = DP_AUX_NATIVE_REPLY_ACK | DP_AUX_I2C_REPLY_ACK;
+		break;
+	}
+
+	do {
+		to_access = min_t(size_t, DP_AUX_MAX_PAYLOAD_BYTES,
+				  msg->size - accessed_bytes);
+		ret = dptx_hw_aux_cmd_submit(hw, msg->request,
+					     msg->address + accessed_bytes,
+					     msg->buffer + accessed_bytes,
+					     to_access, &msg->reply);
+		if (ret) {
+			if (!accessed_bytes)
+				return ret;
+			break;
+		}
+
+		if ((msg->reply & reply_mask) != reply_ack)
+			break;
+
+		accessed_bytes += to_access;
+	} while (accessed_bytes < msg->size);
+
+	return accessed_bytes;
+}
+
+static int dptx_hw_stream_clock_enable(struct dp_hw *hw, int stream_id,
+				       bool enable)
+{
+	struct clk *clk;
+	int ret;
+
+	if (stream_id < 0 || stream_id >= DP_STREAM_MAX)
+		return -EINVAL;
+
+	clk = hw->stream_clk[stream_id];
+	if (IS_ERR_OR_NULL(clk))
+		return -EINVAL;
+
+	if (enable) {
+		ret = clk_prepare_enable(clk);
+		if (ret)
+			return ret;
+	} else {
+		clk_disable_unprepare(clk);
+	}
+
+	return 0;
+}
+
+static int dptx_hw_stream_clock_rate_set(struct dp_hw *hw, int stream_id,
+					 unsigned long rate)
+{
+	struct clk *clk;
+
+	if (stream_id < 0 || stream_id >= DP_STREAM_MAX)
+		return -EINVAL;
+
+	clk = hw->stream_clk[stream_id];
+	if (IS_ERR_OR_NULL(clk) || clk_get_rate(clk) == rate)
+		return 0;
+
+	return clk_set_rate(clk, rate);
+}
+
+static int dptx_hw_dpcd_power_up(struct dp_hw *hw,
+				 unsigned int delay_ms)
+{
+	u8 value;
+	int ret;
+
+	ret = drm_dp_dpcd_readb(&hw->aux, DP_SET_POWER, &value);
+	if (ret < 0)
+		return ret;
+
+	value = (value & ~DP_SET_POWER_MASK) | DP_SET_POWER_D0;
+
+	ret = drm_dp_dpcd_writeb(&hw->aux, DP_SET_POWER, value);
+	if (ret < 0)
+		return ret;
+
+	msleep(delay_ms);
+
+	return 0;
+}
+
+static int dptx_hw_dpcd_power_down(struct dp_hw *hw, bool hpd)
+{
+	u8 value;
+	int ret;
+
+	if (!hpd)
+		return 0;
+
+	ret = drm_dp_dpcd_readb(&hw->aux, DP_SET_POWER, &value);
+	if (ret < 0)
+		return ret;
+
+	value = (value & ~DP_SET_POWER_MASK) | DP_SET_POWER_D3;
+
+	ret = drm_dp_dpcd_writeb(&hw->aux, DP_SET_POWER, value);
+	if (ret < 0)
+		return ret;
+
+	usleep_range(200, 205);
+
+	return 0;
+}
+
+static int dptx_hw_dpcd_sink_caps(struct dp_hw *hw)
+{
+	int ret;
+
+	memset(&hw->caps, 0, sizeof(hw->caps));
+
+	ret = drm_dp_read_dpcd_caps(&hw->aux, hw->dpcd);
+	if (ret)
+		return ret;
+
+	ret = drm_dp_read_downstream_info(&hw->aux, hw->dpcd,
+					  hw->downstream_ports);
+	if (ret)
+		return ret;
+
+	hw->caps.enhanced_framing = drm_dp_enhanced_frame_cap(hw->dpcd);
+	hw->caps.tps3_supported = drm_dp_tps3_supported(hw->dpcd);
+	hw->caps.tps4_supported = drm_dp_tps4_supported(hw->dpcd);
+	hw->caps.vsc_supported = drm_dp_vsc_sdp_supported(&hw->aux, hw->dpcd);
+
+	hw->caps.max_lanes = drm_dp_max_lane_count(hw->dpcd);
+	hw->caps.max_rate = drm_dp_max_link_rate(hw->dpcd);
+
+	return 0;
+}
+
+static bool dptx_hw_vsc_supported(struct dp_hw *hw)
+{
+	return hw->caps.vsc_supported;
+}
+
+static void dptx_hw_stream_misc(struct dp_hw *hw, int stream_id,
+				enum dp_pixelformat format, u8 bpc,
+				bool vsc)
+{
+	u8 override_bpc;
+	u8 override = 0;
+	u8 misc1 = 0;
+	u8 misc0;
+
+	switch (format) {
+	case DP_PIXELFORMAT_YUV444:
+		misc0 = DPTX_STREAM_MISC0_COMP_FORMAT_YCRCB_444;
+		break;
+	case DP_PIXELFORMAT_YUV422:
+		misc0 = DPTX_STREAM_MISC0_COMP_FORMAT_YCRCB_422;
+		break;
+	default:
+		misc0 = DPTX_STREAM_MISC0_COMP_FORMAT_RGB;
+		break;
+	}
+
+	switch (bpc) {
+	case 6:
+		misc0 |= DPTX_STREAM_MISC0_BPC_6;
+		override_bpc = DPTX_STREAM_OVERRIDE_BPC_6;
+		break;
+	case 10:
+		misc0 |= DPTX_STREAM_MISC0_BPC_10;
+		override_bpc = DPTX_STREAM_OVERRIDE_BPC_10;
+		break;
+	case 12:
+		misc0 |= DPTX_STREAM_MISC0_BPC_12;
+		override_bpc = DPTX_STREAM_OVERRIDE_BPC_12;
+		break;
+	case 16:
+		misc0 |= DPTX_STREAM_MISC0_BPC_16;
+		override_bpc = DPTX_STREAM_OVERRIDE_BPC_16;
+		break;
+	default:
+		misc0 |= DPTX_STREAM_MISC0_BPC_8;
+		override_bpc = DPTX_STREAM_OVERRIDE_BPC_8;
+		break;
+	}
+
+	if (!vsc && format == DP_PIXELFORMAT_Y_ONLY)
+		misc1 |= DPTX_STREAM_MISC1_Y_ONLY_EN;
+
+	if (vsc) {
+		misc1 |= DPTX_VSC_COLORIMETRY_EN;
+		override = override_bpc | DPTX_STREAM_OVERRIDE_ENABLE;
+
+		switch (format) {
+		case DP_PIXELFORMAT_YUV444:
+			override |= DPTX_STREAM_OVERRIDE_YCbCR_444;
+			break;
+		case DP_PIXELFORMAT_YUV422:
+			override |= DPTX_STREAM_OVERRIDE_YCbCR_422;
+			break;
+		case DP_PIXELFORMAT_YUV420:
+			override |= DPTX_STREAM_OVERRIDE_YCbCR_420;
+			break;
+		default:
+			break;
+		}
+	}
+
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_MISC0(stream_id), misc0);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_MISC1(stream_id), misc1);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_OVERRIDE(stream_id),
+		      override);
+}
+
+static void dptx_hw_stream_tu(struct dp_hw *hw, int stream_id,
+			      u32 video_kbytes, u32 link_rate,
+			      u8 lane_cnt)
+{
+	u32 bytes_per_tu_scaled;
+	u32 symbols_per_tu;
+	u32 frac_symbols_per_tu;
+	u32 tu;
+
+	/*
+	 * Scaled by 1000 (fixed point) so the fraction below can be split
+	 * into integer symbols + 1/16 fractional symbols without floating
+	 * point.
+	 */
+	bytes_per_tu_scaled = video_kbytes *
+			      DPTX_MSA_TRANSFER_UNIT_SIZE_TU_SIZE_DEF /
+			      (lane_cnt * link_rate / 1000);
+
+	symbols_per_tu = (bytes_per_tu_scaled / 1000) & 0xff;
+
+	/*
+	 * Fractional number of valid symbols per TU in units of 1/16th. An
+	 * accumulator is used to increase the number of data per transaction
+	 * unit by 1 every 1/N TUs.
+	 */
+	frac_symbols_per_tu = ((bytes_per_tu_scaled % 1000) * 16 / 1000) & 0x0f;
+
+	tu = DPTX_MSA_TRANSFER_UNIT_SIZE_TU_SIZE_DEF;
+	tu |= FIELD_PREP(GENMASK(23, 16), symbols_per_tu);
+	tu |= FIELD_PREP(GENMASK(27, 24), frac_symbols_per_tu);
+
+	dptx_hw_write(hw, DPTX_TU_CONFIG(stream_id), tu);
+}
+
+static void dptx_hw_stream_timing_set(struct dp_hw *hw, int stream_id,
+				      const struct drm_display_mode *mode,
+				      u8 bpp, u8 lane_cnt, u32 link_rate,
+				      u32 pixel_per_cycle)
+{
+	u32 data_control = DPTX_DATA_CONTROL_DEFAULT;
+	u32 polarity;
+	u32 user_data_count;
+
+	if (pixel_per_cycle == 2 && lane_cnt == 4 &&
+	    link_rate == drm_dp_bw_code_to_link_rate(DP_LINK_BW_8_1))
+		data_control = DPTX_DATA_CONTROL_2PPC_4LANE_HBR3;
+
+	polarity = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ?
+		    BIT(DPTX_MAIN_STREAM_POLARITY_VSYNC_SHIFT) : 0) |
+		   ((mode->flags & DRM_MODE_FLAG_PHSYNC) ?
+		    BIT(DPTX_MAIN_STREAM_POLARITY_HSYNC_SHIFT) : 0);
+
+	user_data_count = ((mode->hdisplay * bpp + 7) / 8 + lane_cnt - 1) /
+			  lane_cnt;
+
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_HTOTAL(stream_id),
+		      mode->htotal);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_VTOTAL(stream_id),
+		      mode->vtotal);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_POLARITY(stream_id),
+		      polarity);
+	dptx_hw_write(hw, DPTX_USER_SYNC_POLARITY(stream_id),
+		      DPTX_MAIN_STREAM_POLARITY_DATAENABLE_HIGH |
+		      polarity);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_HSWIDTH(stream_id),
+		      mode->hsync_end - mode->hsync_start);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_VSWIDTH(stream_id),
+		      mode->vsync_end - mode->vsync_start);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_HRES(stream_id),
+		      mode->hdisplay);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_VRES(stream_id),
+		      mode->vdisplay);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_HSTART(stream_id),
+		      mode->htotal - mode->hsync_start);
+	dptx_hw_write(hw, DPTX_MAIN_STREAM_VSTART(stream_id),
+		      mode->vtotal - mode->vsync_start);
+	dptx_hw_write(hw, DPTX_USER_DATA_COUNT(stream_id),
+		      user_data_count);
+	dptx_hw_write(hw, DPTX_SECONDARY_DATA_WINDOW(stream_id),
+		      (mode->htotal - mode->hdisplay) * (link_rate / 40) *
+		      9 / mode->clock);
+	dptx_hw_write(hw, DPTX_DATA_CONTROL(stream_id), data_control);
+	dptx_hw_write(hw, DPTX_USER_PIXEL_COUNT(stream_id),
+		      pixel_per_cycle);
+}
+
+static void dptx_hw_stream_timing(struct dp_hw *hw, int stream_id,
+				  const struct drm_display_mode *mode,
+				  u8 bpp, u8 lane_cnt, u32 link_rate,
+				  u32 pixel_per_cycle)
+{
+	dptx_hw_stream_tu(hw, stream_id, mode->clock * bpp / 8, link_rate,
+			  lane_cnt);
+	dptx_hw_stream_timing_set(hw, stream_id, mode, bpp, lane_cnt,
+				  link_rate, pixel_per_cycle);
+}
+
+static void dptx_hw_stream_enable(struct dp_hw *hw, int stream_id,
+				  bool enable)
+{
+	if (enable) {
+		dptx_hw_write(hw, DPTX_VIDEO_STREAM_ENABLE(stream_id), 1);
+		dptx_hw_write(hw, DPTX_SECONDARY_STREAM_ENABLE(stream_id), 1);
+		return;
+	}
+
+	dptx_hw_write(hw, DPTX_INFOFRAME_ENABLE(stream_id), 0);
+	dptx_hw_write(hw, DPTX_INFOFRAME_SELECT(stream_id), 0);
+	dptx_hw_write(hw, DPTX_INFOFRAME_RATE(stream_id), 0);
+	dptx_hw_write(hw, DPTX_SECONDARY_STREAM_ENABLE(stream_id), 0);
+	dptx_hw_write(hw, DPTX_VIDEO_STREAM_ENABLE(stream_id), 0);
+}
+
+static void dptx_hw_stream_infoframe_write(struct dp_hw *hw,
+					   int stream_id, u32 select,
+					   const struct dp_sdp *sdp,
+					   bool repeat)
+{
+	u32 value;
+	int i;
+
+	if (stream_id < 0 || stream_id >= DPTX_MST_SOURCE_NUMBER ||
+	    select >= DPTX_MAX_SDP)
+		return;
+
+	value = dptx_hw_read(hw, DPTX_INFOFRAME_ENABLE(stream_id));
+	dptx_hw_write(hw, DPTX_INFOFRAME_ENABLE(stream_id),
+		      value & ~BIT(select));
+	dptx_hw_write(hw, DPTX_INFOFRAME_SELECT(stream_id), select);
+
+	dptx_hw_write(hw, DPTX_INFOFRAME_DATA(stream_id),
+		      sdp->sdp_header.HB0);
+	dptx_hw_write(hw, DPTX_INFOFRAME_DATA(stream_id),
+		      sdp->sdp_header.HB1);
+	dptx_hw_write(hw, DPTX_INFOFRAME_DATA(stream_id),
+		      sdp->sdp_header.HB2);
+	dptx_hw_write(hw, DPTX_INFOFRAME_DATA(stream_id),
+		      sdp->sdp_header.HB3);
+
+	for (i = 0; i < DPTX_SDP_DB_MAX; i++)
+		dptx_hw_write(hw, DPTX_INFOFRAME_DATA(stream_id),
+			      sdp->db[i]);
+
+	value = dptx_hw_read(hw, DPTX_INFOFRAME_RATE(stream_id));
+	if (repeat)
+		value |= BIT(select);
+	else
+		value &= ~BIT(select);
+	dptx_hw_write(hw, DPTX_INFOFRAME_RATE(stream_id), value);
+
+	value = dptx_hw_read(hw, DPTX_INFOFRAME_ENABLE(stream_id));
+	dptx_hw_write(hw, DPTX_INFOFRAME_ENABLE(stream_id),
+		      value | BIT(select));
+}
+
+static bool dptx_hw_needs_vsc_sdp(enum dp_pixelformat format,
+				  u32 colorspace)
+{
+	if (format == DP_PIXELFORMAT_YUV420)
+		return true;
+
+	switch (colorspace) {
+	case DRM_MODE_COLORIMETRY_SYCC_601:
+	case DRM_MODE_COLORIMETRY_OPYCC_601:
+	case DRM_MODE_COLORIMETRY_BT2020_YCC:
+	case DRM_MODE_COLORIMETRY_BT2020_RGB:
+	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
+		return true;
+	default:
+		break;
+	}
+
+	return false;
+}
+
+static enum dp_colorimetry
+dptx_hw_vsc_colorimetry(enum dp_pixelformat format, u32 colorspace)
+{
+	switch (colorspace) {
+	case DRM_MODE_COLORIMETRY_BT709_YCC:
+		return DP_COLORIMETRY_BT709_YCC;
+	case DRM_MODE_COLORIMETRY_XVYCC_601:
+		return DP_COLORIMETRY_XVYCC_601;
+	case DRM_MODE_COLORIMETRY_XVYCC_709:
+		return DP_COLORIMETRY_XVYCC_709;
+	case DRM_MODE_COLORIMETRY_SYCC_601:
+		return DP_COLORIMETRY_SYCC_601;
+	case DRM_MODE_COLORIMETRY_OPYCC_601:
+		return DP_COLORIMETRY_OPYCC_601;
+	case DRM_MODE_COLORIMETRY_BT2020_CYCC:
+		return DP_COLORIMETRY_BT2020_CYCC;
+	case DRM_MODE_COLORIMETRY_BT2020_RGB:
+		return DP_COLORIMETRY_BT2020_RGB;
+	case DRM_MODE_COLORIMETRY_BT2020_YCC:
+		return DP_COLORIMETRY_BT2020_YCC;
+	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
+	case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
+		return DP_COLORIMETRY_DCI_P3_RGB;
+	default:
+		break;
+	}
+
+	/* Unspecified RGB is sRGB. Unspecified YCbCr is BT.709. */
+	if (format == DP_PIXELFORMAT_RGB)
+		return DP_COLORIMETRY_DEFAULT;
+
+	return DP_COLORIMETRY_BT709_YCC;
+}
+
+static int dptx_hw_stream_format_set(struct dp_hw *hw, int stream_id,
+				     enum dp_pixelformat format, u32 colorspace,
+				     u8 bpc, u8 content_type)
+{
+	struct drm_dp_vsc_sdp vsc = {};
+	struct dp_sdp sdp;
+	bool enable;
+	ssize_t len;
+
+	enable = hw->caps.vsc_supported &&
+		 dptx_hw_needs_vsc_sdp(format, colorspace);
+
+	if (enable) {
+		vsc.sdp_type = DP_SDP_VSC;
+		vsc.revision = 0x5;
+		vsc.length = 0x13;
+		vsc.pixelformat = format;
+		vsc.colorimetry = dptx_hw_vsc_colorimetry(format, colorspace);
+
+		switch (bpc) {
+		case 6:
+		case 8:
+		case 10:
+		case 12:
+		case 16:
+			vsc.bpc = bpc;
+			break;
+		default:
+			vsc.bpc = 8;
+			break;
+		}
+
+		vsc.dynamic_range = DP_DYNAMIC_RANGE_CTA;
+		vsc.content_type = content_type;
+
+		len = drm_dp_vsc_sdp_pack(&vsc, &sdp);
+		if (len < 0)
+			return len;
+	}
+
+	dptx_hw_stream_misc(hw, stream_id, format, bpc, enable);
+
+	if (enable)
+		dptx_hw_stream_infoframe_write(hw, stream_id, DPTX_VSC_SDP,
+					       &sdp, true);
+	return 0;
+}
+
+static irqreturn_t dptx_hw_irq(struct dp_hw *hw)
+{
+	u32 value;
+	u32 mask;
+
+	value = dptx_hw_read(hw, DPTX_INTERRUPT_CAUSE);
+	mask = dptx_hw_read(hw, DPTX_INTERRUPT_MASK);
+	value &= ~mask;
+	if (!value)
+		return IRQ_NONE;
+
+	if (value & DPTX_INTERRUPT_GP_TIMER_IRQ)
+		dev_dbg_ratelimited(hw->dev, "gp tmr\n");
+
+	if (value & DPTX_INTERRUPT_REPLY_TIMEOUT)
+		dev_dbg_ratelimited(hw->dev, "reply timeout\n");
+
+	if (value & DPTX_INTERRUPT_REPLY_RECEIVED)
+		dev_dbg_ratelimited(hw->dev, "reply received\n");
+
+	if (value & DPTX_INTERRUPT_HPD_EVENT)
+		hw->hpd_event_callback(hw->data);
+
+	if (value & DPTX_INTERRUPT_HPD_IRQ)
+		hw->hpd_irq_callback(hw->data);
+
+	return IRQ_HANDLED;
+}
+
+static int dptx_hw_aux_register(struct dp_hw *hw,
+				struct drm_device *drm)
+{
+	int ret;
+
+	hw->aux.name = devm_kasprintf(hw->dev, GFP_KERNEL, "%s AUX",
+				      dev_name(hw->dev));
+	if (!hw->aux.name)
+		return -ENOMEM;
+
+	hw->aux.dev = hw->dev;
+	hw->aux.drm_dev = drm;
+	hw->aux.transfer = dptx_hw_aux_transfer;
+
+	ret = drm_dp_aux_register(&hw->aux);
+	if (ret)
+		return ret;
+
+	hw->aux_registered = true;
+
+	return 0;
+}
+
+static void dptx_hw_aux_unregister(struct dp_hw *hw)
+{
+	if (!hw->aux_registered)
+		return;
+
+	drm_dp_aux_unregister(&hw->aux);
+	hw->aux.drm_dev = NULL;
+	hw->aux_registered = false;
+}
+
+static const struct dptx_hw_ops dptx_hw_ops = {
+	.hpd_state			= dptx_hw_hpd_state,
+	.sink_present			= dptx_hw_sink_present,
+
+	.phy_reset			= dptx_hw_phy_reset,
+	.phy_init			= dptx_hw_phy_init,
+	.phy_exit			= dptx_hw_phy_exit,
+	.phy_power			= dptx_hw_phy_power,
+	.phy_mode			= dptx_hw_phy_mode,
+
+	.host_enable			= dptx_hw_host_enable,
+	.core_enable			= dptx_hw_core_enable,
+
+	.apb_clock			= dptx_hw_apb_clock,
+	.reset				= dptx_hw_reset,
+
+	.link_train			= dptx_hw_link_train,
+	.link_status			= dptx_hw_link_status,
+
+	.aux_register			= dptx_hw_aux_register,
+	.aux_unregister			= dptx_hw_aux_unregister,
+
+	.dpcd_power_up			= dptx_hw_dpcd_power_up,
+	.dpcd_power_down		= dptx_hw_dpcd_power_down,
+	.dpcd_sink_caps			= dptx_hw_dpcd_sink_caps,
+	.vsc_supported			= dptx_hw_vsc_supported,
+
+	.stream_clock_enable		= dptx_hw_stream_clock_enable,
+	.stream_clock_rate_set		= dptx_hw_stream_clock_rate_set,
+	.stream_timing			= dptx_hw_stream_timing,
+	.stream_enable			= dptx_hw_stream_enable,
+	.stream_format_set		= dptx_hw_stream_format_set,
+
+	.irq				= dptx_hw_irq,
+};
+
+static int dptx_hw_parse_dt(struct dp_hw *hw)
+{
+	struct platform_device *pdev = to_platform_device(hw->dev);
+	struct device *dev = hw->dev;
+	int i;
+
+	hw->base = devm_platform_ioremap_resource_byname(pdev, "dp");
+	if (IS_ERR(hw->base))
+		return dev_err_probe(dev, PTR_ERR(hw->base),
+				     "failed to map dp registers\n");
+
+	hw->phy = devm_phy_get(dev, NULL);
+	if (IS_ERR(hw->phy))
+		return dev_err_probe(dev, PTR_ERR(hw->phy),
+				     "failed to get dp phy\n");
+
+	hw->reset = devm_reset_control_get(dev, NULL);
+	if (IS_ERR(hw->reset))
+		return dev_err_probe(dev, PTR_ERR(hw->reset),
+				     "failed to get reset\n");
+
+	hw->apb_clk = devm_clk_get(dev, "apb");
+	if (IS_ERR(hw->apb_clk))
+		return dev_err_probe(dev, PTR_ERR(hw->apb_clk),
+				     "failed to get apb clock\n");
+
+	for (i = 0; i < DPTX_MST_SOURCE_NUMBER &&
+	     i < ARRAY_SIZE(stream_clk_names); i++) {
+		hw->stream_clk[i] = devm_clk_get(dev, stream_clk_names[i]);
+		if (IS_ERR(hw->stream_clk[i]))
+			return dev_err_probe(dev, PTR_ERR(hw->stream_clk[i]),
+					     "failed to get %s\n",
+					     stream_clk_names[i]);
+	}
+
+	return 0;
+}
+
+struct dp_hw *dptx_hw_init(struct device *dev, void *data,
+			   void (*hpd_event_callback)(void *data),
+			   void (*hpd_irq_callback)(void *data))
+{
+	struct dp_hw *hw;
+	int ret;
+
+	hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
+	if (!hw)
+		return ERR_PTR(-ENOMEM);
+
+	hw->dev = dev;
+	hw->data = data;
+	hw->hpd_event_callback = hpd_event_callback;
+	hw->hpd_irq_callback = hpd_irq_callback;
+	hw->ops = &dptx_hw_ops;
+
+	ret = dptx_hw_parse_dt(hw);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return hw;
+}
drivers/gpu/drm/bridge/trilinear/dptx_hw.h new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#4 lore raw

PATCH v4 3/5 drm/bridge: trilinear: add the DPTX DRM bridge

In reply to Luo Liu

Add the DRM bridge that exposes the transmitter to KMS: connector detection, EDID mode enumeration with a downstream timing fallback, validation of a mode against the sink link capabilities, and the atomic enable and disable paths.

The bridge is meant to be attached with DRM_BRIDGE_ATTACH_NO_CONNECTOR; the connector is created by drm_bridge_connector_init() from the bridge operations, so the driver only implements the bridge side and never creates a connector of its own.

---
 drivers/gpu/drm/bridge/trilinear/dptx_drm.c | 664 ++++++++++++++++++++
 drivers/gpu/drm/bridge/trilinear/dptx_drm.h |  17 +
 2 files changed, 681 insertions(+)
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_drm.c
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_drm.h
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +664 -0
new file mode 100644
index 000000000000..099dd53eb2d4
@@ -0,0 +1,664 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * DisplayPort DRM bridge helpers
+ *
+ * Copyright 2026 Cix Technology Group Co., Ltd.
+ */
+
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/math64.h>
+
+#include <drm/drm_atomic.h>
+#include <drm/drm_atomic_state_helper.h>
+#include <drm/drm_bridge.h>
+#include <drm/drm_edid.h>
+
+#include "dptx.h"
+#include "dptx_drm.h"
+
+#define DPTX_MIN_H_BACKPORCH 12
+#define DPTX_MIN_BPP		(6 * 3)
+
+struct fmt_prio_entry {
+	int format;
+	int bpc;
+};
+
+/* SST: RGB -> YUV422 10 -> YUV422 8 -> RGB 6 -> YUV420 8 */
+static const struct fmt_prio_entry fmt_prio[] = {
+	{ DRM_OUTPUT_COLOR_FORMAT_RGB444, 0 },
+	{ DRM_OUTPUT_COLOR_FORMAT_YCBCR422, 10 },
+	{ DRM_OUTPUT_COLOR_FORMAT_YCBCR422, 8 },
+	{ DRM_OUTPUT_COLOR_FORMAT_RGB444, 6 },
+	{ DRM_OUTPUT_COLOR_FORMAT_YCBCR420, 8 },
+};
+
+static const u8 dp_common_link_bw[] = {
+	DP_LINK_BW_1_62,
+	DP_LINK_BW_2_7,
+	DP_LINK_BW_5_4,
+	DP_LINK_BW_8_1,
+};
+
+static enum drm_connector_status dptx_bridge_detect(struct drm_bridge *bridge,
+						    struct drm_connector *connector)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+
+	return dp->ops->detect_status(dp);
+}
+
+static void dptx_hpd_notify(struct dptx_dev *dp,
+			    enum drm_connector_status hpd_state)
+{
+	dp->ops->lock(dp);
+	if (READ_ONCE(dp->hpd_stopped)) {
+		dp->ops->unlock(dp);
+		return;
+	}
+
+	if (dp->state & DPTX_STATE_SUSPENDED) {
+		dp->force_hpd = hpd_state;
+		dp->ops->unlock(dp);
+		return;
+	}
+
+	if (hpd_state != dp->status) {
+		dp->force_hpd = hpd_state;
+		queue_delayed_work(system_freezable_wq, &dp->hpd_event_work, 0);
+
+		if (hpd_state == connector_status_connected)
+			schedule_delayed_work(&dp->sw_hpd_irq_work, 1);
+	}
+	dp->ops->unlock(dp);
+}
+
+static void dptx_bridge_hpd_notify(struct drm_bridge *bridge,
+				   struct drm_connector *connector,
+				   enum drm_connector_status status)
+{
+	dptx_hpd_notify(bridge_to_dp(bridge), status);
+}
+
+static int dptx_bridge_get_modes(struct drm_bridge *bridge,
+				 struct drm_connector *connector)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+	const struct drm_edid *edid;
+	struct drm_display_mode *mode;
+	int ret;
+
+	dp->ops->lock(dp);
+	if (dp->state & DPTX_STATE_SUSPENDED) {
+		dp->ops->unlock(dp);
+		return 0;
+	}
+
+	edid = drm_edid_read(connector);
+	dp->ops->unlock(dp);
+
+	if (!edid) {
+		drm_edid_connector_update(connector, NULL);
+
+		dp->ops->lock(dp);
+		mode = drm_dp_downstream_mode(connector->dev, dp->hw->dpcd,
+					      dp->hw->downstream_ports);
+		dp->ops->unlock(dp);
+		if (!mode)
+			return 0;
+
+		drm_mode_probed_add(connector, mode);
+		return 1;
+	}
+
+	drm_edid_connector_update(connector, edid);
+	ret = drm_edid_connector_add_modes(connector);
+	drm_edid_free(edid);
+
+	return ret;
+}
+
+static u32 dptx_pick_ppc(const struct drm_display_mode *mode,
+			 enum dp_pixelformat format)
+{
+	if (mode->clock > DPTX_MAX_FREQ &&
+	    mode->clock < DPTX_MAX_FREQ * 2 &&
+	    format != DP_PIXELFORMAT_YUV420)
+		return 2;
+
+	return 1;
+}
+
+static void dptx_adjust_mode(const struct drm_display_mode *mode,
+			     struct drm_display_mode *adjusted_mode)
+{
+	int diff = mode->htotal - mode->hsync_end;
+
+	if (diff < DPTX_MIN_H_BACKPORCH) {
+		adjusted_mode->htotal = mode->htotal +
+					DPTX_MIN_H_BACKPORCH - diff;
+		adjusted_mode->clock =
+			div_u64(mul_u32_u32(mode->clock, adjusted_mode->htotal),
+				mode->htotal);
+	}
+
+	adjusted_mode->flags &= ~(DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC);
+	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
+		adjusted_mode->flags |= DRM_MODE_FLAG_NVSYNC;
+	else
+		adjusted_mode->flags |= DRM_MODE_FLAG_PVSYNC;
+
+	adjusted_mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC);
+	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
+		adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
+	else
+		adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
+}
+
+static enum drm_mode_status
+dptx_bridge_mode_valid(struct drm_bridge *bridge,
+		       const struct drm_display_info *info,
+		       const struct drm_display_mode *mode)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+	struct drm_display_mode adjusted;
+	int max_lanes;
+	int max_rate;
+	u32 pixel_per_cycle;
+	int minbpp = DPTX_MIN_BPP;
+	bool vsc_supported;
+	bool can_420;
+
+	adjusted = *mode;
+	dptx_adjust_mode(mode, &adjusted);
+
+	dp->ops->lock(dp);
+	max_lanes = dp->link_max_lanes;
+	max_rate = dp->link_max_rate;
+	vsc_supported = dp->hw->ops->vsc_supported(dp->hw);
+	dp->ops->unlock(dp);
+
+	pixel_per_cycle = dptx_pick_ppc(&adjusted, DP_PIXELFORMAT_RGB);
+
+	can_420 = drm_mode_is_420(info, mode) && bridge->ycbcr_420_allowed &&
+		  vsc_supported;
+
+	if (drm_mode_is_420_only(info, mode)) {
+		if (!can_420)
+			return MODE_NO_420;
+
+		minbpp = 8 * 3 / 2;
+		pixel_per_cycle = 1;
+	} else if (can_420 && adjusted.clock <= DPTX_MAX_FREQ) {
+		minbpp = 8 * 3 / 2;
+	} else if (info->color_formats &
+		   BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR422)) {
+		minbpp = 8 * 2;
+	}
+
+	if (pixel_per_cycle == 1 && adjusted.clock > DPTX_MAX_FREQ) {
+		drm_mode_debug_printmodeline(mode);
+		return MODE_CLOCK_HIGH;
+	}
+
+	if (adjusted.clock > DPTX_MAX_RATE(max_rate, max_lanes, minbpp)) {
+		drm_mode_debug_printmodeline(mode);
+		return MODE_CLOCK_HIGH;
+	}
+
+	return MODE_OK;
+}
+
+static bool dptx_bt2020_colorspace(const struct drm_connector_state *state)
+{
+	return state->colorspace >= DRM_MODE_COLORIMETRY_BT2020_CYCC &&
+	       state->colorspace <= DRM_MODE_COLORIMETRY_BT2020_YCC;
+}
+
+static int dptx_cal_bpc(struct drm_connector_state *connector_state,
+			int format, int bpc)
+{
+	struct drm_display_info *info =
+		&connector_state->connector->display_info;
+
+	if (dptx_bt2020_colorspace(connector_state))
+		bpc = min(bpc, 10);
+
+	if (info->bpc)
+		bpc = min_t(int, bpc, info->bpc);
+
+	if (connector_state->max_requested_bpc)
+		bpc = min(bpc, connector_state->max_requested_bpc);
+
+	if (bpc < 8 && format != DRM_OUTPUT_COLOR_FORMAT_RGB444)
+		return -EINVAL;
+
+	return bpc;
+}
+
+static int dptx_cal_bpp(struct dptx_dev *dp, int bpc, int format)
+{
+	int bpp = bpc * 3;
+
+	switch (format) {
+	case DRM_OUTPUT_COLOR_FORMAT_RGB444:
+	case DRM_OUTPUT_COLOR_FORMAT_YCBCR444:
+		bpp = bpc * 3;
+		break;
+	case DRM_OUTPUT_COLOR_FORMAT_YCBCR422:
+		bpp = bpc * 2;
+		break;
+	case DRM_OUTPUT_COLOR_FORMAT_YCBCR420:
+		bpp = bpc * 3 / 2;
+		break;
+	default:
+		dev_warn(dp->dev, "invalid color format %d\n", format);
+	}
+	return bpp;
+}
+
+static int compute_available_clock_rate(struct dptx_dev *dp,
+					struct drm_connector_state *conn_state,
+					int suggest_bpc, int clock,
+					int color_format, int *rt_bpc,
+					int *rt_bpp)
+{
+	int max_lanes = dp->link_max_lanes;
+	int max_rate = dp->link_max_rate;
+	int rate;
+	int bpc;
+	int bpp;
+
+	bpc = dptx_cal_bpc(conn_state, color_format, suggest_bpc);
+	if (bpc < 0)
+		return bpc;
+	bpp = dptx_cal_bpp(dp, bpc, color_format);
+	rate = DPTX_MAX_RATE(max_rate, max_lanes, bpp);
+
+	if (clock <= rate) {
+		*rt_bpc = bpc;
+		*rt_bpp = bpp;
+
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+/*
+ * @mode still has the CEA timings and sync flags. adjusted_mode has had
+ * its polarities inverted, so it no longer matches a VIC.
+ */
+static bool dptx_format_ok(struct dptx_dev *dp,
+			   const struct drm_display_info *info,
+			   const struct drm_display_mode *mode,
+			   const struct drm_display_mode *adjusted_mode,
+			   int info_formats, int format)
+{
+	if (format == DRM_OUTPUT_COLOR_FORMAT_YCBCR420) {
+		if (!dp->hw->ops->vsc_supported(dp->hw))
+			return false;
+
+		/*
+		 * Y420VDB sets the sink-wide 420 bit for every mode. This
+		 * VIC has to be 420-only or 420-also. 420 is one pixel
+		 * per cycle.
+		 */
+		if (!drm_mode_is_420(info, mode))
+			return false;
+
+		return adjusted_mode->clock <= DPTX_MAX_FREQ;
+	}
+
+	if (drm_mode_is_420_only(info, mode))
+		return false;
+
+	return info_formats & BIT(format);
+}
+
+static int dptx_pick_format_bpc(struct dptx_dev *dp,
+				struct drm_connector_state *conn_state,
+				const struct drm_display_mode *mode,
+				struct drm_display_mode *adjusted_mode,
+				int info_formats, int suggest_bpc,
+				int *color_format, int *bpc, int *bpp)
+{
+	struct drm_display_info *info =
+		&conn_state->connector->display_info;
+	int curr_bpc;
+	int format;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(fmt_prio); i++) {
+		format = fmt_prio[i].format;
+		curr_bpc = fmt_prio[i].bpc ? fmt_prio[i].bpc : suggest_bpc;
+
+		if (!dptx_format_ok(dp, info, mode, adjusted_mode,
+				    info_formats, format))
+			continue;
+
+		/*
+		 * First entry (RGB): prefer 10bit before falling back to
+		 * suggest_bpc, and step down to 8bit before giving up on RGB.
+		 * BT.2020 also tries 10 first when suggest_bpc is lower.
+		 * The DPU always outputs RGB, so a lower RGB depth is still
+		 * preferable to a converted YUV format.
+		 */
+		if (i == 0) {
+			if (dptx_bt2020_colorspace(conn_state) &&
+			    suggest_bpc < 10 &&
+			    !compute_available_clock_rate(dp, conn_state, 10,
+							  adjusted_mode->clock,
+							  format, bpc, bpp)) {
+				if (color_format)
+					*color_format = format;
+				return 0;
+			}
+
+			if (suggest_bpc > 10 &&
+			    !compute_available_clock_rate(dp, conn_state, 10,
+							  adjusted_mode->clock,
+							  format, bpc, bpp)) {
+				if (color_format)
+					*color_format = format;
+				return 0;
+			}
+
+			if (!compute_available_clock_rate(dp, conn_state,
+							  suggest_bpc,
+							  adjusted_mode->clock,
+							  format, bpc, bpp)) {
+				if (color_format)
+					*color_format = format;
+				return 0;
+			}
+
+			if (suggest_bpc > 8 &&
+			    !compute_available_clock_rate(dp, conn_state, 8,
+							  adjusted_mode->clock,
+							  format, bpc, bpp)) {
+				if (color_format)
+					*color_format = format;
+				return 0;
+			}
+
+			continue;
+		}
+
+		if (!compute_available_clock_rate(dp, conn_state, curr_bpc,
+						  adjusted_mode->clock,
+						  format, bpc, bpp)) {
+			if (color_format)
+				*color_format = format;
+			return 0;
+		}
+	}
+
+	return -EINVAL;
+}
+
+static enum dp_pixelformat dptx_drm_format_to_dp(int color_format)
+{
+	switch (color_format) {
+	case DRM_OUTPUT_COLOR_FORMAT_YCBCR444:
+		return DP_PIXELFORMAT_YUV444;
+	case DRM_OUTPUT_COLOR_FORMAT_YCBCR422:
+		return DP_PIXELFORMAT_YUV422;
+	case DRM_OUTPUT_COLOR_FORMAT_YCBCR420:
+		return DP_PIXELFORMAT_YUV420;
+	default:
+		return DP_PIXELFORMAT_RGB;
+	}
+}
+
+static int dptx_pick_link_rate(struct dptx_dev *dp,
+			       struct drm_display_mode *adjusted_mode,
+			       int bpp)
+{
+	int max_rate = dp->link_max_rate;
+	int max_lanes = dp->link_max_lanes;
+	int link_rate = max_rate;
+	int rate;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(dp_common_link_bw); i++) {
+		rate = drm_dp_bw_code_to_link_rate(dp_common_link_bw[i]);
+		if (rate > max_rate)
+			break;
+		if (adjusted_mode->clock <= DPTX_MAX_RATE(rate, max_lanes, bpp)) {
+			link_rate = rate;
+			break;
+		}
+	}
+
+	return link_rate;
+}
+
+static int dptx_pick_config(struct dptx_dev *dp,
+			    struct drm_connector_state *conn_state,
+			    const struct drm_display_mode *mode,
+			    struct drm_display_mode *adjusted_mode,
+			    int *color_format, int *bpc, int *bpp)
+{
+	struct drm_display_info *info = &conn_state->connector->display_info;
+	u32 color_formats = info->color_formats;
+	int suggest_bpc;
+
+	color_formats |= BIT(DRM_OUTPUT_COLOR_FORMAT_RGB444);
+
+	/* bpc 0 is unspecified. Start at 8, and never below 6. */
+	suggest_bpc = info->bpc ? info->bpc : 8;
+	if (suggest_bpc < 6)
+		suggest_bpc = 6;
+
+	return dptx_pick_format_bpc(dp, conn_state, mode, adjusted_mode,
+				      color_formats, suggest_bpc,
+				      color_format, bpc, bpp);
+}
+
+static int dptx_bridge_mode_set(struct dptx_dev *dp,
+				struct drm_crtc_state *crtc_state,
+				struct drm_connector_state *conn_state)
+{
+	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
+	int color_format;
+	int link_rate;
+	int bw_code;
+	int bpc;
+	int bpp;
+	int ret;
+
+	ret = dp->ops->detect(dp);
+	if (ret)
+		return ret;
+
+	dp->ops->lock(dp);
+
+	ret = dptx_pick_config(dp, conn_state, &crtc_state->mode,
+			       adjusted_mode, &color_format, &bpc, &bpp);
+	if (ret)
+		goto out;
+
+	dp->config.format = dptx_drm_format_to_dp(color_format);
+	dp->config.colorspace = conn_state->colorspace;
+	dp->config.content_type = conn_state->content_type;
+	dp->config.bpc = bpc;
+	dp->config.bpp = bpp;
+	drm_mode_copy(&dp->adjusted_mode, adjusted_mode);
+	dp->pixel_per_cycle = dptx_pick_ppc(adjusted_mode, dp->config.format);
+
+	link_rate = dptx_pick_link_rate(dp, adjusted_mode, bpp);
+	bw_code = drm_dp_link_rate_to_bw_code(link_rate);
+
+	ret = dp->ops->mode_configure(dp, adjusted_mode->clock, bw_code,
+				      bpp, false);
+	if (ret)
+		goto out;
+
+	dev_dbg(dp->dev,
+		"%dx%d@%d fmt=%d bpc=%d bpp=%d link=%u lanes=%u ppc=%u\n",
+		adjusted_mode->hdisplay, adjusted_mode->vdisplay,
+		drm_mode_vrefresh(adjusted_mode), dp->config.format,
+		bpc, bpp, bw_code, dp->mode.lane_cnt,
+		dp->pixel_per_cycle);
+out:
+	dp->ops->unlock(dp);
+	return ret;
+}
+
+static int dptx_bridge_atomic_check(struct drm_bridge *bridge,
+				    struct drm_bridge_state *bridge_state,
+				    struct drm_crtc_state *crtc_state,
+				    struct drm_connector_state *conn_state)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+	int bpc;
+	int bpp;
+	int ret;
+	bool vsc_supported;
+
+	if (!conn_state->crtc)
+		return 0;
+
+	dptx_adjust_mode(&crtc_state->mode, &crtc_state->adjusted_mode);
+
+	dp->ops->lock(dp);
+	ret = dptx_pick_config(dp, conn_state, &crtc_state->mode,
+			       &crtc_state->adjusted_mode,
+			       NULL, &bpc, &bpp);
+	vsc_supported = dp->hw->ops->vsc_supported(dp->hw);
+	dp->ops->unlock(dp);
+	if (ret)
+		return ret;
+
+	if (vsc_supported) {
+		const struct drm_connector_state *old_conn_state;
+
+		old_conn_state =
+			drm_atomic_get_old_connector_state(bridge_state->base.state,
+							   conn_state->connector);
+		if (old_conn_state &&
+		    old_conn_state->colorspace != conn_state->colorspace)
+			crtc_state->mode_changed = true;
+	}
+
+	return 0;
+}
+
+static void
+dptx_bridge_atomic_enable(struct drm_bridge *bridge,
+			  struct drm_atomic_commit *state)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+	struct drm_connector_state *conn_state;
+	struct drm_crtc_state *crtc_state;
+	struct drm_connector *connector;
+	int ret;
+
+	connector = drm_atomic_get_new_connector_for_encoder(state,
+							     bridge->encoder);
+	if (!connector)
+		return;
+
+	conn_state = drm_atomic_get_new_connector_state(state, connector);
+	if (!conn_state || !conn_state->crtc)
+		return;
+
+	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
+	if (!crtc_state)
+		return;
+
+	ret = dp->ops->prepare(dp);
+	if (ret) {
+		if (ret != -ENODEV)
+			dev_err(dp->dev, "failed to prepare DP output: %d\n",
+				ret);
+		return;
+	}
+
+	ret = dptx_bridge_mode_set(dp, crtc_state, conn_state);
+	if (ret) {
+		dev_err(dp->dev, "failed to set DP mode: %d\n", ret);
+		goto err_unprepare;
+	}
+
+	ret = dp->ops->encoder_enable(dp, 0, true);
+	if (ret) {
+		dev_err(dp->dev, "failed to enable DP output: %d\n", ret);
+		goto err_unprepare;
+	}
+
+	return;
+
+err_unprepare:
+	dp->ops->lock(dp);
+	dp->ops->unprepare(dp);
+	dp->ops->unlock(dp);
+}
+
+static void
+dptx_bridge_atomic_disable(struct drm_bridge *bridge,
+			   struct drm_atomic_commit *state)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+
+	dp->ops->encoder_enable(dp, 0, false);
+}
+
+static int dptx_bridge_attach(struct drm_bridge *bridge,
+			      struct drm_encoder *encoder,
+			      enum drm_bridge_attach_flags flags)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+	int ret;
+
+	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
+		return -EINVAL;
+
+	WRITE_ONCE(dp->hpd_stopped, false);
+
+	ret = dp->hw->ops->aux_register(dp->hw, bridge->dev);
+	if (ret)
+		return ret;
+
+	WRITE_ONCE(dp->attached, true);
+
+	/* The bridge may have been attached long after the sink was plugged. */
+	dp->ops->replay_deferred_hotplug(dp);
+
+	return 0;
+}
+
+static void dptx_bridge_detach(struct drm_bridge *bridge)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+
+	/* Stop workers before the connector goes away. */
+	dp->ops->quiesce_hpd(dp);
+
+	WRITE_ONCE(dp->attached, false);
+
+	dp->hw->ops->aux_unregister(dp->hw);
+}
+
+static const struct drm_bridge_funcs dptx_bridge_funcs = {
+	.attach = dptx_bridge_attach,
+	.detach = dptx_bridge_detach,
+	.detect = dptx_bridge_detect,
+	.get_modes = dptx_bridge_get_modes,
+	.mode_valid = dptx_bridge_mode_valid,
+	.hpd_notify = dptx_bridge_hpd_notify,
+	.atomic_check = dptx_bridge_atomic_check,
+	.atomic_enable = dptx_bridge_atomic_enable,
+	.atomic_disable = dptx_bridge_atomic_disable,
+	.atomic_create_state = drm_atomic_helper_bridge_create_state,
+	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
+	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
+};
+
+struct dptx_dev *dptx_bridge_alloc(struct device *dev)
+{
+	return devm_drm_bridge_alloc(dev, struct dptx_dev, bridge,
+				     &dptx_bridge_funcs);
+}
drivers/gpu/drm/bridge/trilinear/dptx_drm.h new file +17 -0
new file mode 100644
index 000000000000..fc30a7ce6e99
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * DisplayPort DRM helpers
+ *
+ * Copyright 2026 Cix Technology Group Co., Ltd.
+ */
+
+#ifndef DPTX_DRM_H
+#define DPTX_DRM_H
+
+#include <linux/device.h>
+
+#include "dptx.h"
+
+struct dptx_dev *dptx_bridge_alloc(struct device *dev);
+
+#endif /* DPTX_DRM_H */
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v4 3/5] drm/bridge: trilinear: add the DPTX DRM bridge

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/bridge: trilinear: force_hpd corruption in dptx_hpd_notify() - [Low] drm/bridge: trilinear…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/bridge: trilinear: force_hpd corruption in dptx_hpd_notify()
- [Low] drm/bridge: trilinear: dptx_bridge_mode_valid() ignores info->bpc < 8 for YCbCr
--

commit fc27a9663705ea0c07bb63dcb67db3ac74bea16f
Author: Luo Liu <[email protected]>

drm/bridge: trilinear: add the DPTX DRM bridge

This commit adds the DRM bridge callbacks and helper functions for the Trilinear DisplayPort transmitter, covering connector detection, EDID mode enumeration, mode validation, and atomic modeset operations.

Quoted 5 lines
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +0 -0
new file mode 100644
index 0000000000000..099dd53eb2d4d

[ ... ]

Quoted 29 lines
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +29 -0
+static enum drm_connector_status dptx_bridge_detect(struct drm_bridge *bridge,
+						    struct drm_connector *connector)
+{
+	struct dptx_dev *dp = bridge_to_dp(bridge);
+
+	return dp->ops->detect_status(dp);
+}
+
+static void dptx_hpd_notify(struct dptx_dev *dp,
+			    enum drm_connector_status hpd_state)
+{
+	dp->ops->lock(dp);
+	if (READ_ONCE(dp->hpd_stopped)) {
+		dp->ops->unlock(dp);
+		return;
+	}
+
+	if (dp->state & DPTX_STATE_SUSPENDED) {
+		dp->force_hpd = hpd_state;
+		dp->ops->unlock(dp);
+		return;
+	}
+
+	if (hpd_state != dp->status) {
+		dp->force_hpd = hpd_state;
+		queue_delayed_work(system_freezable_wq, &dp->hpd_event_work, 0);
+
+		if (hpd_state == connector_status_connected)
+			schedule_delayed_work(&dp->sw_hpd_irq_work, 1);

drivers/gpu/drm/bridge/trilinear/dptx_drm.c line 82

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +10 -0
+	}
+	dp->ops->unlock(dp);
+}
+
+static void dptx_bridge_hpd_notify(struct drm_bridge *bridge,
+				   struct drm_connector *connector,
+				   enum drm_connector_status status)
+{
+	dptx_hpd_notify(bridge_to_dp(bridge), status);
+}

[Severity: High]
Can dptx_bridge_hpd_notify() corrupt dp->force_hpd during normal connector detection?

drm_bridge_connector_detect_ctx() invokes dptx_bridge_hpd_notify() right after every dptx_bridge_detect() call:

drivers/gpu/drm/display/drm_bridge_connector.c:drm_bridge_connector_detect_ctx() {
	if (detect) {
		status = detect->funcs->detect(detect, connector);
		...
		drm_bridge_connector_hpd_notify(connector, status);
	}
}

Because dp->ops->detect_status() does not update dp->status (which starts at connector_status_unknown at boot and after suspend until hpd_event_work runs), probing a connected native DisplayPort sink enters dptx_hpd_notify() with hpd_state != dp->status and latches dp->force_hpd to connector_status_connected. Once dp->force_hpd is set, physical HPD state checks evaluate true even after the cable is unplugged, wedging the native port in a connected state.

On USB-C Alt-Mode ports, if KMS probes the connector while hpd_event_work is still waiting for the PHY to enter DP mode, dptx_bridge_detect() fails AUX reads and returns connector_status_disconnected; the follow-up dptx_bridge_hpd_notify() call then overwrites dp->force_hpd with connector_status_disconnected and aborts the PHY wait. Similarly, probing while DPTX_STATE_SUSPENDED is set causes dptx_bridge_detect() to return connector_status_unknown and dptx_hpd_notify() to overwrite dp->force_hpd with connector_status_unknown.

Also, gating the dp->force_hpd update on hpd_state != dp->status while dp->status is updated asynchronously by hpd_event_work drops rapid plug/unplug transitions that arrive before hpd_event_work finishes (or after the PHY wait times out without updating dp->status), leaving dp->force_hpd stuck at the stale state.

[ ... ]

Quoted 5 lines
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +5 -0
+static enum drm_mode_status
+dptx_bridge_mode_valid(struct drm_bridge *bridge,
+		       const struct drm_display_info *info,
+		       const struct drm_display_mode *mode)
+{

[ ... ]

Quoted 15 lines
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +15 -0
+	can_420 = drm_mode_is_420(info, mode) && bridge->ycbcr_420_allowed &&
+		  vsc_supported;
+
+	if (drm_mode_is_420_only(info, mode)) {
+		if (!can_420)
+			return MODE_NO_420;
+
+		minbpp = 8 * 3 / 2;
+		pixel_per_cycle = 1;
+	} else if (can_420 && adjusted.clock <= DPTX_MAX_FREQ) {
+		minbpp = 8 * 3 / 2;
+	} else if (info->color_formats &
+		   BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR422)) {
+		minbpp = 8 * 2;
+	}

[ ... ]

Quoted 1 line

+}

[ ... ]

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +10 -0
+static int dptx_cal_bpc(struct drm_connector_state *connector_state,
+			int format, int bpc)
+{
+	struct drm_display_info *info =
+		&connector_state->connector->display_info;
+
+	if (dptx_bt2020_colorspace(connector_state))
+		bpc = min(bpc, 10);
+
+	if (info->bpc)

drivers/gpu/drm/bridge/trilinear/dptx_drm.c line 238

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_drm.c new file +10 -0
+		bpc = min_t(int, bpc, info->bpc);
+
+	if (connector_state->max_requested_bpc)
+		bpc = min(bpc, connector_state->max_requested_bpc);
+
+	if (bpc < 8 && format != DRM_OUTPUT_COLOR_FORMAT_RGB444)
+		return -EINVAL;
+
+	return bpc;
+}

[Severity: Low]
Can dptx_bridge_mode_valid() accept YCbCr modes that dptx_bridge_atomic_check() will reject when 0 < info->bpc < 8?

dptx_cal_bpc() clamps bpc to info->bpc and rejects any format other than DRM_OUTPUT_COLOR_FORMAT_RGB444 when bpc < 8. However, dptx_bridge_mode_valid() allows 4:2:0-only modes and lowers minbpp to 12 (YCbCr 4:2:0) or 16 (YCbCr 4:2:2) without checking that info->bpc is 0 or at least 8, so modes that rely on YCbCr on a 6-bpc sink pass validation and then fail in dptx_bridge_atomic_check() with -EINVAL.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#6 lore raw

PATCH v4 4/5 drm/bridge: trilinear: add the DPTX core and build glue

In reply to Luo Liu

Add the probe path, system sleep handling, hotplug workers and the stream state machine that tie the hardware layer to the DRM bridge, and hook the driver into the DRM bridge Kconfig and Makefile.

Detection is driven by the controller's HPD interrupt. The USB-C transmitters do not route HPD to the controller, so a sink that was plugged in before the bridge was attached raises no event; replay that plug once the common PHY is in DP mode and the AUX channel answers. The native DP port wires HPD and is reported by the interrupt.

An atomic enable that races the system sleep callbacks re-initializes the host on demand; the suspend path reinstates its suspended state afterwards so the host is never powered down with its interrupt armed.

---
 drivers/gpu/drm/bridge/Kconfig               |    2 +
 drivers/gpu/drm/bridge/Makefile              |    1 +
 drivers/gpu/drm/bridge/trilinear/Kconfig     |    9 +
 drivers/gpu/drm/bridge/trilinear/Makefile    |    4 +
 drivers/gpu/drm/bridge/trilinear/dptx.h      |  106 ++
 drivers/gpu/drm/bridge/trilinear/dptx_core.c | 1101 ++++++++++++++++++
 include/drm/bridge/trilinear_dp.h            |   68 ++
 7 files changed, 1291 insertions(+)
 create mode 100644 drivers/gpu/drm/bridge/trilinear/Kconfig
 create mode 100644 drivers/gpu/drm/bridge/trilinear/Makefile
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx.h
 create mode 100644 drivers/gpu/drm/bridge/trilinear/dptx_core.c
 create mode 100644 include/drm/bridge/trilinear_dp.h
drivers/gpu/drm/bridge/Kconfig +2 -0
index c7da6b02d7b6..d328d5f84dc7 100644
@@ -510,4 +510,6 @@ source "drivers/gpu/drm/bridge/imx/Kconfig"
 
 source "drivers/gpu/drm/bridge/synopsys/Kconfig"
 
+source "drivers/gpu/drm/bridge/trilinear/Kconfig"
+
 endmenu
drivers/gpu/drm/bridge/Makefile +1 -0
index 15cc821d85b7..9fe6bec55c51 100644
@@ -53,3 +53,4 @@ obj-y += analogix/
 obj-y += cadence/
 obj-y += imx/
 obj-y += synopsys/
+obj-y += trilinear/
drivers/gpu/drm/bridge/trilinear/Kconfig new file +9 -0
new file mode 100644
index 000000000000..7e2bf1f27035
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config DRM_TRILINEAR_DPTX
+	tristate
+	depends on COMMON_CLK && DRM && OF
+	depends on GENERIC_PHY
+	select DRM_KMS_HELPER
+	select DRM_DISPLAY_HELPER
+	select DRM_DISPLAY_DP_HELPER
drivers/gpu/drm/bridge/trilinear/Makefile new file +4 -0
new file mode 100644
index 000000000000..415842b8465d
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+trilinear-dptx-y := dptx_core.o dptx_drm.o dptx_hw.o
+obj-$(CONFIG_DRM_TRILINEAR_DPTX) += trilinear-dptx.o
drivers/gpu/drm/bridge/trilinear/dptx.h new file +106 -0

106 lines not on this page · raw message

drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +1101 -0

1,101 lines not on this page · raw message

include/drm/bridge/trilinear_dp.h new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH v4 4/5] drm/bridge: trilinear: add the DPTX core and build glue

Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [High] drm/bridge: trilinear: missing status update in dptx_detect() breaks HPD - [High] drm/bridge…

Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [High] drm/bridge: trilinear: missing status update in dptx_detect() breaks HPD
- [High] drm/bridge: trilinear: suspend race with dptx_prepare() and dptx_unprepare()
- [High] drm/bridge: trilinear: UAF and destroyed mutexes on unbind in dptx_probe()
- [High] drm/bridge: trilinear: unclocked AUX MMIO access after dptx_host_deinit()
- [Medium] drm/bridge: trilinear: DPTX_MAX_LINK_RATE clamps link rate to HBR2
- [Medium] drm/bridge: trilinear: stale force_hpd stalls PHY wait and USB-C unplug
- [Medium] drm/bridge: trilinear: missing host_enable() rollback in dptx_host_reset()
- [Low] drm/bridge: trilinear: unreachable dptx_host_resume() call in dptx_detect()
--

commit 5884b10a11ec4e3f84fff1f9d68ae7a14e195f9c
Author: Luo Liu <[email protected]>

drm/bridge: trilinear: add the DPTX core and build glue

This commit adds the Trilinear DisplayPort transmitter core, including the probe and system sleep paths, hotplug workers, stream state machine, and Kconfig/Makefile integration.

Quoted 6 lines
drivers/gpu/drm/bridge/trilinear/dptx.h new file +0 -0
new file mode 100644
index 0000000000000..78ebe0f14571d
@@ -0,0 +1,106 @@

[ ... ]

drivers/gpu/drm/bridge/trilinear/dptx.h line 29

Quoted 3 lines
drivers/gpu/drm/bridge/trilinear/dptx.h new file +3 -0
+#define DPTX_MAX_FREQ 600000
+#define DPTX_MAX_LINK_RATE drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4)
+#define DP_MAX_TRAINING_LOOP 3

[Severity: Medium]
Should DPTX_MAX_LINK_RATE use DP_LINK_BW_8_1 instead of DP_LINK_BW_5_4? Both dptx_probe() and dptx_read_sink_caps() clamp dp->link_max_rate to DPTX_MAX_LINK_RATE, which caps the link at HBR2 (5.4 Gbps) and makes the DP_LINK_BW_8_1 (HBR3) handling in dptx_mode_configure(), dptx_drm.c, and dptx_hw.c unreachable.

Quoted 6 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +0 -0
new file mode 100644
index 0000000000000..b59a47396b8ee
@@ -0,0 +1,1101 @@

[ ... ]

Quoted 7 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +7 -0
+static int dptx_host_reset(struct dptx_dev *dp)
+{
+	int ret;
+
+	dp->hw->ops->reset(dp->hw, true);
+	usleep_range(10, 20);
+	dp->hw->ops->reset(dp->hw, false);

drivers/gpu/drm/bridge/trilinear/dptx_core.c line 416

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +10 -0
+
+	ret = dp->hw->ops->phy_reset(dp->hw);
+	if (ret)
+		return ret;
+
+	usleep_range(100, 200);
+	dp->hw->ops->host_enable(dp->hw, true);
+
+	return dp->hw->ops->phy_init(dp->hw);
+}

[Severity: Medium]
If dp->hw->ops->phy_init() fails, does dptx_host_reset() leave the transmitter enabled and hardware HPD interrupts unmasked? dptx_host_init() then disables apb_clk, so a subsequent HPD interrupt when the IRQ is live (for example during dptx_prepare() or dptx_host_resume()) will read MMIO registers in dptx_hw_irq() with apb_clk gated off.

Quoted 28 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +28 -0
+
+static int dptx_host_init(struct dptx_dev *dp)
+{
+	int ret;
+
+	if (dp->state & DPTX_STATE_INITIALIZED)
+		return 0;
+
+	ret = dp->hw->ops->apb_clock(dp->hw, true);
+	if (ret)
+		return ret;
+
+	ret = dptx_host_reset(dp);
+	if (ret) {
+		dp->hw->ops->apb_clock(dp->hw, false);
+		return ret;
+	}
+
+	dp->state |= DPTX_STATE_INITIALIZED;
+
+	return 0;
+}
+
+static void dptx_host_deinit(struct dptx_dev *dp)
+{
+	if (dp->state & DPTX_STATE_READY)
+		dptx_core_off(dp);
+

drivers/gpu/drm/bridge/trilinear/dptx_core.c line 454

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +10 -0
+	if (!(dp->state & DPTX_STATE_INITIALIZED))
+		return;
+
+	dp->hw->ops->host_enable(dp->hw, false);
+	dp->hw->ops->phy_exit(dp->hw);
+
+	dp->hw->ops->apb_clock(dp->hw, false);
+
+	dp->state &= ~(DPTX_STATE_INITIALIZED | DPTX_STATE_INIT_TRAIN);
+}

[Severity: High]
Can dptx_hw_aux_transfer() access DPTX_AUX_* MMIO registers after dptx_host_deinit() has gated apb_clk?

dptx_probe() exposes dp->hw->aux.ddc on the bridge and dptx_bridge_attach() registers dp->hw->aux, but dptx_hw_aux_transfer() only takes hw->aux.hw_mutex without checking whether the host is initialized:

drivers/gpu/drm/bridge/trilinear/dptx_hw.c:dptx_hw_aux_transfer() {
	struct dp_hw *hw = container_of(aux, struct dp_hw, aux);
    ...
	return dptx_hw_aux_cmd_submit(hw, msg->address, cmd, msg->buffer,
				      msg->size, &msg->reply);
}

Any AUX or I2C-over-AUX transaction that runs concurrently with or after dptx_host_deinit() during suspend or shutdown will perform unclocked APB register accesses.

[ ... ]

Quoted 4 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +4 -0
+static int dptx_detect(struct dptx_dev *dp)
+{
+	int ret;
+

drivers/gpu/drm/bridge/trilinear/dptx_core.c line 519

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +10 -0
+	dptx_lock(dp);
+
+	if (dp->state & DPTX_STATE_SUSPENDED) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	ret = dptx_host_resume(dp);
+	if (ret)
+		goto out;

[Severity: Low]
Since dptx_detect() returns -EBUSY right above when DPTX_STATE_SUSPENDED is set, and dptx_host_resume() immediately returns 0 when DPTX_STATE_SUSPENDED is not set, is this dptx_host_resume() call dead code?

Quoted 26 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +26 -0
+
+	if (!(dp->state & DPTX_STATE_INITIALIZED)) {
+		dev_warn_once(dp->dev, "detect with uninitialized host\n");
+		ret = -ENODEV;
+		goto out;
+	}
+
+	if (!dptx_get_hpd_state(dp)) {
+		dptx_handle_disconnect(dp);
+		ret = -ENODEV;
+		goto out;
+	}
+
+	ret = dptx_handle_connect(dp);
+
+out:
+	dptx_unlock(dp);
+
+	return ret;
+}
+
+static enum drm_connector_status dptx_detect_status(struct dptx_dev *dp)
+{
+	enum drm_connector_status status;
+	int ret;
+

drivers/gpu/drm/bridge/trilinear/dptx_core.c line 555

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +10 -0
+	ret = dptx_detect(dp);
+	if (ret == -EBUSY)
+		status = connector_status_unknown;
+	else if (ret)
+		status = connector_status_disconnected;
+	else
+		status = connector_status_connected;
+
+	return status;
+}

[Severity: High]
Should dptx_detect() or dptx_detect_status() update dp->status to match the detected connector status?

When drm_bridge_connector_detect_ctx() probes the connector, it calls dptx_bridge_detect() -> dptx_detect_status() and immediately follows with drm_bridge_connector_hpd_notify(), which calls dptx_hpd_notify():

drivers/gpu/drm/bridge/trilinear/dptx_drm.c:dptx_hpd_notify() {
    ...
	if (hpd_state != dp->status) {
		dp->force_hpd = hpd_state;
		queue_delayed_work(system_freezable_wq, &dp->hpd_event_work, 0);
}

Because dp->status is still at its previous value (such as connector_status_unknown after probe or suspend), dptx_hpd_notify() sees hpd_state != dp->status and sets dp->force_hpd = hpd_state. On a native DP port with a monitor connected, that latches dp->force_hpd to connector_status_connected. From then on, dptx_get_hpd_state() always returns true even after the cable is unplugged, and dptx_handle_connect() returns 0 immediately because DPTX_STATE_CONNECTED is already set, leaving the connector permanently stuck as connected.

[ ... ]

Quoted 31 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +31 -0
+static void dptx_hpd_event_work_func(struct work_struct *work)
+{
+	struct dptx_dev *dp = container_of(work, struct dptx_dev,
+					 hpd_event_work.work);
+	enum drm_connector_status old_status;
+	enum drm_connector_status status;
+	bool hpd;
+
+	dptx_lock(dp);
+	old_status = dp->status;
+	if (dptx_hpd_work_aborted(dp)) {
+		dptx_unlock(dp);
+		return;
+	}
+
+	hpd = dptx_get_hpd_state(dp);
+	dptx_unlock(dp);
+
+	if (hpd && dp->hw->ops->phy_mode(dp->hw) != PHY_MODE_INVALID &&
+	    !dptx_wait_phy_dp_mode(dp))
+		return;
+
+	status = dptx_detect_status(dp);
+
+	dptx_lock(dp);
+	if (dptx_hpd_work_aborted(dp)) {
+		dptx_unlock(dp);
+		return;
+	}
+
+	dp->status = status;

drivers/gpu/drm/bridge/trilinear/dptx_core.c line 646

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +10 -0
+	if (status == connector_status_connected)
+		dptx_handle_connect(dp);
+	else
+		dptx_handle_disconnect(dp);
+
+	dptx_unlock(dp);
+
+	if (old_status != status && dp->bridge.dev)
+		drm_bridge_hpd_notify(&dp->bridge, status);
+}

[Severity: Medium]
Can dp->force_hpd get stuck at connector_status_connected on USB-C ports and stall system_freezable_wq in dptx_wait_phy_dp_mode()?

dp->status is not updated to connector_status_connected until after dptx_wait_phy_dp_mode() (which polls for up to 10 seconds) and dptx_detect_status() complete. If a USB-C sink is unplugged while dptx_wait_phy_dp_mode() is polling (or after it times out and returns early), dp->status is still connector_status_disconnected, so dptx_hpd_notify(dp, connector_status_disconnected) sees
hpd_state == dp->status and drops the disconnect without clearing dp->force_hpd. dptx_hpd_wait_aborted() then never sees dptx_get_hpd_state() drop to false, stalling the workqueue for the full 10-second timeout and leaving dp->force_hpd stuck at connector_status_connected.

Similarly, dptx_pm_prepare() resets dp->status to connector_status_unknown without clearing dp->force_hpd; if a USB-C sink is unplugged during system sleep, resume queues hpd_event_work with dp->force_hpd still set to connector_status_connected, stalling dptx_wait_phy_dp_mode() for 10 seconds and returning before dp->status is updated.

[ ... ]

Quoted 23 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +23 -0
+static int dptx_pm_prepare(struct dptx_dev *dp)
+{
+	dptx_lock(dp);
+	dptx_irq_off(dp);
+	dp->state |= DPTX_STATE_SUSPENDED;
+	dptx_unlock(dp);
+
+	dptx_cancel_hpd_works(dp);
+
+	dptx_lock(dp);
+	if (!(dp->state & DPTX_STATE_SUSPENDED)) {
+		dptx_irq_off(dp);
+		dp->state |= DPTX_STATE_SUSPENDED;
+	}
+
+	if (!dp->active_stream_cnt)
+		dptx_host_deinit(dp);
+	dp->status = connector_status_unknown;
+	dp->state &= ~DPTX_STATE_CONNECTED;
+	dptx_unlock(dp);
+
+	return 0;
+}

[ ... ]

Quoted 45 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +45 -0
+static int dptx_prepare(struct dptx_dev *dp)
+{
+	int ret;
+
+	dptx_lock(dp);
+
+	ret = dptx_host_resume(dp);
+	if (ret)
+		goto end;
+
+	if (dp->state & DPTX_STATE_ENABLED) {
+		ret = 0;
+		goto end;
+	}
+
+	if (!(dp->state & DPTX_STATE_INITIALIZED)) {
+		ret = dptx_host_init(dp);
+		if (ret)
+			goto end;
+	} else if (dp->state & DPTX_STATE_READY) {
+		/* core_on() finished, but the stream never became ENABLED. */
+		dptx_core_off(dp);
+	}
+
+	if (!dptx_get_hpd_state(dp)) {
+		ret = -ENODEV;
+		goto end;
+	}
+
+	ret = dptx_handle_connect(dp);
+
+end:
+	dptx_unlock(dp);
+	return ret;
+}
+
+static int dptx_unprepare(struct dptx_dev *dp)
+{
+	lockdep_assert_held(&dp->lock);
+
+	if (dp->active_stream_cnt)
+		return 0;
+
+	if (!(dp->state & (DPTX_STATE_ENABLED | DPTX_STATE_READY)))
+		return 0;

drivers/gpu/drm/bridge/trilinear/dptx_core.c line 878

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +10 -0
+
+	dptx_core_off(dp);
+
+	if (dp->state & DPTX_STATE_SUSPENDED)
+		dptx_host_deinit(dp);
+
+	dp->state &= ~DPTX_STATE_ENABLED;
+
+	return 0;
+}

[Severity: High]
Can an atomic enable racing with dptx_pm_prepare() leave the host initialized, clocks enabled, and dp->irq armed across suspend?

CPU0 (suspend) CPU1 (atomic commit)
dptx_pm_prepare()
dptx_lock()
dptx_irq_off()
dp->state |= DPTX_STATE_SUSPENDED
dptx_unlock()
dptx_cancel_hpd_works()

                                  dptx_bridge_atomic_enable()
                                    dptx_prepare()
                                      dptx_host_resume()
                                        dptx_host_init()
                                        enable_irq(dp->irq)
                                        dp->state &= ~DPTX_STATE_SUSPENDED
                                    ...
                                    dptx_encoder_enable(..., true)
                                      dp->active_stream_cnt++
  dptx_lock()
  if (!(dp->state & DPTX_STATE_SUSPENDED))
    dptx_irq_off()
    dp->state |= DPTX_STATE_SUSPENDED
  if (!dp->active_stream_cnt)
    /* skipped: active_stream_cnt == 1 */

dptx_unlock()

There are three related issues in this suspend synchronization:

First, if dptx_prepare() runs while dptx_pm_prepare() is in dptx_cancel_hpd_works(), dptx_host_resume() re-enables dp->irq and clears DPTX_STATE_SUSPENDED. An interrupt can then re-queue hpd_event_work or hpd_irq_work after dptx_cancel_hpd_works() has returned, and if dptx_encoder_enable() finishes before dptx_pm_prepare() re-acquires dp->lock, dptx_pm_prepare() sees dp->active_stream_cnt != 0 and skips dptx_host_deinit().

Second, if dptx_prepare() runs after dptx_pm_prepare() has already finished its second locked block, dptx_host_resume() initializes the host, enables dp->irq, and clears DPTX_STATE_SUSPENDED with nothing left to reinstate DPTX_STATE_SUSPENDED or call dptx_host_deinit() and dptx_irq_off().

Third, in dptx_unprepare(), the early return when neither DPTX_STATE_ENABLED nor DPTX_STATE_READY is set runs before checking DPTX_STATE_SUSPENDED. If dptx_bridge_atomic_enable() fails in dptx_bridge_mode_set() or dptx_core_on() (which clears DPTX_STATE_READY on error) and jumps to err_unprepare, dptx_unprepare() returns early without calling dptx_host_deinit().

[ ... ]

Quoted 27 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +27 -0
+static void dptx_deinit(struct dptx_dev *dp)
+{
+	if (!(dp->state & DPTX_STATE_CONFIGURED))
+		return;
+
+	dptx_lock(dp);
+	WRITE_ONCE(dp->hpd_stopped, true);
+	dptx_irq_off(dp);
+	dptx_unlock(dp);
+
+	dptx_cancel_hpd_works(dp);
+
+	dptx_lock(dp);
+	dptx_stop_streams(dp);
+	dptx_host_deinit(dp);
+	dp->state &= ~(DPTX_STATE_CONFIGURED);
+	dptx_unlock(dp);
+
+	mutex_destroy(&dp->lock);
+}
+
+static void dptx_dev_release(void *res)
+{
+	struct dptx_dev *dp = res;
+
+	dptx_deinit(dp);
+}

[ ... ]

Quoted 4 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +4 -0
+struct dptx_dev *dptx_probe(struct device *dev,
+			    const struct dptx_plat_data *soc_data,
+			    void *priv)
+{

[ ... ]

Quoted 13 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +13 -0
+	ret = dptx_init(dp);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = devm_add_action_or_reset(dev, dptx_dev_release, dp);
+	if (ret)
+		return ERR_PTR(ret);
+
+	dev_set_drvdata(dev, dp);
+
+	dp->bridge.of_node = dev->of_node;
+	dp->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_MODES |
+			 DRM_BRIDGE_OP_HPD;

drivers/gpu/drm/bridge/trilinear/dptx_core.c line 1065

Quoted 10 lines
drivers/gpu/drm/bridge/trilinear/dptx_core.c new file +10 -0
+	dp->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
+	dp->bridge.ddc = &dp->hw->aux.ddc;
+	dp->bridge.ycbcr_420_allowed = true;
+
+	ret = devm_drm_bridge_add(dev, &dp->bridge);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return dp;
+}

[Severity: High]
Can unbinding the DPTX device cause a use-after-free on dp->hw and acquire destroyed mutexes?

First, devm_add_action_or_reset(dev, dptx_dev_release, dp) is registered before devm_drm_bridge_add(dev, &dp->bridge), so devres runs drm_bridge_remove() before dptx_deinit(). drm_bridge_remove() destroys bridge->hpd_mutex while dp->irq is still enabled and dp->hpd_event_work has not been cancelled, so a concurrent dptx_hpd_event_work_func() calling drm_bridge_hpd_notify() can lock the destroyed bridge->hpd_mutex.

Second, while struct dptx_dev is refcounted via devm_drm_bridge_alloc(), dp->hw (which embeds struct drm_dp_aux aux) is allocated with devm_kzalloc() on dev. When the DPTX device is unbound while the bridge is attached to an encoder, dptx_deinit() destroys dp->lock without unregistering dp->hw->aux and devres frees dp->hw, leaving the registered AUX device and I2C adapter in freed memory. When encoder cleanup later runs dptx_bridge_detach(), it locks the destroyed dp->lock in dptx_quiesce_hpd() and dereferences the freed dp->hw in dp->hw->ops->aux_unregister(dp->hw).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#8 lore raw

PATCH v4 5/5 drm/cix: add the Sky1 DisplayPort platform glue

In reply to Luo Liu

Add the platform driver that maps the Sky1 RCSU video adapter registers and programs the pixel format and pixel-per-clock mode for the Trilinear DisplayPort transmitter.

The DP protocol, link training and the DRM bridge live in the shared Trilinear bridge driver. This glue only fills in the SoC-specific parts through dptx_plat_data, declared in include/drm/bridge/trilinear_dp.h.

---
 drivers/gpu/drm/Kconfig             |   1 +
 drivers/gpu/drm/Makefile            |   1 +
 drivers/gpu/drm/cix/Kconfig         |   3 +
 drivers/gpu/drm/cix/Makefile        |   3 +
 drivers/gpu/drm/cix/dptx/Kconfig    |  15 +++
 drivers/gpu/drm/cix/dptx/Makefile   |   4 +
 drivers/gpu/drm/cix/dptx/cix_dptx.c | 163 ++++++++++++++++++++++++++++
 7 files changed, 190 insertions(+)
 create mode 100644 drivers/gpu/drm/cix/Kconfig
 create mode 100644 drivers/gpu/drm/cix/Makefile
 create mode 100644 drivers/gpu/drm/cix/dptx/Kconfig
 create mode 100644 drivers/gpu/drm/cix/dptx/Makefile
 create mode 100644 drivers/gpu/drm/cix/dptx/cix_dptx.c
drivers/gpu/drm/Kconfig +1 -0
index 61111d50b1c6..a1808db8b9c8 100644
@@ -311,6 +311,7 @@ source "drivers/gpu/drm/aspeed/Kconfig"
 source "drivers/gpu/drm/ast/Kconfig"
 source "drivers/gpu/drm/atmel-hlcdc/Kconfig"
 source "drivers/gpu/drm/bridge/Kconfig"
+source "drivers/gpu/drm/cix/Kconfig"
 source "drivers/gpu/drm/etnaviv/Kconfig"
 source "drivers/gpu/drm/exynos/Kconfig"
 source "drivers/gpu/drm/fsl-dcu/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index 2c6bc093a923..6ed7456bb993 100644
@@ -169,6 +169,7 @@ obj-y			+= tests/
 obj-$(CONFIG_DRM_MIPI_DBI) += drm_mipi_dbi.o
 obj-$(CONFIG_DRM_MIPI_DSI) += drm_mipi_dsi.o
 obj-y			+= arm/
+obj-y			+= cix/
 obj-y			+= clients/
 obj-y			+= display/
 obj-$(CONFIG_DRM_TTM)	+= ttm/
drivers/gpu/drm/cix/Kconfig new file +3 -0
new file mode 100644
index 000000000000..6cbaa5bf9fe9
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+source "drivers/gpu/drm/cix/dptx/Kconfig"
drivers/gpu/drm/cix/Makefile new file +3 -0
new file mode 100644
index 000000000000..a93591fc49e8
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_DRM_CIX_DP) += dptx/
drivers/gpu/drm/cix/dptx/Kconfig new file +15 -0
new file mode 100644
index 000000000000..f5d3afe5699f
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config DRM_CIX_DP
+	tristate "Cix Sky1 DisplayPort platform glue"
+	depends on ARCH_CIX || COMPILE_TEST
+	depends on COMMON_CLK && DRM && OF
+	depends on GENERIC_PHY
+	select DRM_TRILINEAR_DPTX
+	help
+	  Platform glue for the Cix Sky1 DisplayPort output. It maps the
+	  SoC video adapter registers and binds the shared Trilinear DPTX
+	  bridge driver.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called cix-dptx.
drivers/gpu/drm/cix/dptx/Makefile new file +4 -0
new file mode 100644
index 000000000000..64226bbd7c49
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+cix-dptx-y := cix_dptx.o
+obj-$(CONFIG_DRM_CIX_DP) += cix-dptx.o
drivers/gpu/drm/cix/dptx/cix_dptx.c new file +163 -0

163 lines not on this page · raw message

Signature · 1 line
2.25.1