Skip to contentSkip to conversation

clk: realtek: Add RTD1625 clock support

PATCH Superseded Yu-Chun Lin 4 participants 22 messages Nov 12 – Nov 19

Subsystems (3)

v16 of 17 Superseded Latest is v17 →

lore
Share & tools
lore

Yu-Chun Lin

author maintainer
#0 lore raw

v16 cover clk: realtek: Add RTD1625 clock support

Hello,

This patch series adds the clock controller support for Realtek's RTD1625 SoC platform.

Because the reset controllers share the same register space with the clock controllers on this platform, we utilize the Auxiliary Bus framework to decouple them. The clock controllers act as the primary devices, registering the reset controllers as auxiliary devices.

The series includes:
1. Device Tree Bindings:
- Add bindings for the Realtek RTD1625 Clock & Reset Controllers.

2. Clock Subsystem Infrastructure:
- Introduce a common probe, and add support for basic clocks including PLLs, gate clocks, mux clocks, and MMC-tuned PLLs.

3. Clock Platform Drivers:
- Add the clock controller drivers for RTD1625-CRT and RTD1625-ISO.
- These drivers provide the clock sources and instantiate the corresponding auxiliary reset devices.

Best regards,
Yu-Chun Lin
---
Changes in v16:
General:
- Split out the reset controller support into a separate series.

Patch 1 (dt-binding):
- Remove Krzysztof's reviewed-by tag.
- Add 'clock-name' property with a predefined enum list.
- Update 'clocks' property constraints.
- Move the reset ID header to the reset controller series.

Patch 3 (PLL):
- Remove Brian's reviewed-by tag.
- Introduce 'expected_parent_rate' validation in '.determine_rate()' and '.set_rate()' to safely enforce the fixed input rate requirement.
- In determined_rate, replacing the floor/ceil algorithm with a closest-match search within [min_rate, max_rate] boundaries.
- Improve comments for spinlock.
- Add '.is_enabled' callback to 'rtk_clk_pll_ro_ops'

Patch 4 (GATE):
- Remove Brian's reviewed-by tag.
- Replace 'CLK_HW_INIT' with 'CLK_HW_INIT_HW' and 'CLK_HW_INIT_FW_NAME' macros to avoid relying on unstable internal CCF global names.
- Drop all unused NO_PARENT macro and 'rtk_clk_regmap_gate_ro_ops'.

Patch 5 (MUX):
- Remove Brian's reviewed-by tag.
- Replace 'CLK_HW_INIT_PARENTS' with 'CLK_HW_INIT_PARENTS_HW' to use '.parent_hws'

Patch 6 (MMC PLL):
- Remove Brian's reviewed-by tag.
- Move the MMC PLL structs and ops declarations out of clk-pll.h into a new clk-regmap-pll-mmc.h.
- Add a separate 'struct rtk_clk_pll_mmc_phase' with its own to_clk_pll_mmc_phase().
- Merge prepare/unprepare/is_prepared into enable/disable/is_enabled, as
suggested: there is no sleeping operation.
- Introduce the 'rtk_clk_pll_mmc_div_cfg' table for the SSCPLL analog tuning.
- Introduce the 'rtk_clk_pll_mmc_rate' table.
- determine_rate() selects the closest entry within [min_rate, max_rate]
- set_rate() programs that entry only
- recalc_rate() matches all five raw register fields against the table. However, only on a miss does it fall back to 'clk_pll_mmc_calc_rate()', which implements the formula.

Patch 7 & 8 (CRT & ISO clock driver):
- Remove Brian's reviewed-by tag.
- Replace 'const char *' parent arrays with 'struct clk_hw *' arrays.
- Switch all gate, mux, and fixed-factor clock initializations to use '_HW' or '_FW_NAME' macros.
- Use 'CLK_HW_INIT_FW_NAME' for regmap_pll initialization when referencing external clocks, and add 'expected_parent_rate' to ensure a safe input rate.
- Refactor eMMC PLL initialization (decoupling phases and appending rate tables).

Patch 9 (DTS):
- Add 'clock-names = "osc27m"' to the 'cc' and 'iso_s_cc' clock controller nodes.
- Explicitly wire up internal parent clocks from the 'cc' controller to the 'ic' clock controller, along with their corresponding 'clock-names'.

v15: https://lore.kernel.org/lkml/[email protected]/
v14: https://lore.kernel.org/lkml/[email protected]/
v13: https://lore.kernel.org/lkml/[email protected]/
v12: https://lore.kernel.org/lkml/[email protected]/
v11: https://lore.kernel.org/lkml/[email protected]/
v10: https://lore.kernel.org/lkml/[email protected]/
v9: https://lore.kernel.org/lkml/[email protected]/
v8: https://lore.kernel.org/lkml/[email protected]/
v7: https://lore.kernel.org/lkml/[email protected]/
v6: https://lore.kernel.org/lkml/[email protected]/
v5: https://lore.kernel.org/lkml/[email protected]/
v4: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

Cheng-Yu Lee (7):
clk: realtek: Introduce common probe()
clk: realtek: Add support for phase locked loops (PLLs)
clk: realtek: Add support for gate clock
clk: realtek: Add support for mux clock
clk: realtek: Add support for MMC-tuned PLL clocks
clk: realtek: Add RTD1625-CRT clock controller driver
clk: realtek: Add RTD1625-ISO clock controller driver

Yu-Chun Lin (2):
dt-bindings: clock: Add Realtek RTD1625 Clock & Reset Controller
arm64: dts: realtek: Add clock support for RTD1625

 .../bindings/clock/realtek,rtd1625-clk.yaml   |  95 ++
 MAINTAINERS                                   |   7 +
 arch/arm64/boot/dts/realtek/kent.dtsi         |  41 +
 drivers/clk/Kconfig                           |   1 +
 drivers/clk/Makefile                          |   1 +
 drivers/clk/realtek/Kconfig                   |  46 +
 drivers/clk/realtek/Makefile                  |  12 +
 drivers/clk/realtek/clk-regmap-gate.c         |  59 ++
 drivers/clk/realtek/clk-regmap-gate.h         |  54 ++
 drivers/clk/realtek/clk-regmap-mux.c          |  40 +
 drivers/clk/realtek/clk-regmap-mux.h          |  42 +
 drivers/clk/realtek/clk-regmap-pll-mmc.c      | 457 +++++++++
 drivers/clk/realtek/clk-regmap-pll-mmc.h      |  66 ++
 drivers/clk/realtek/clk-regmap-pll.c          | 219 +++++
 drivers/clk/realtek/clk-regmap-pll.h          |  53 ++
 drivers/clk/realtek/clk-rtd1625-crt.c         | 878 ++++++++++++++++++
 drivers/clk/realtek/clk-rtd1625-iso.c         | 140 +++
 drivers/clk/realtek/clk-rtk-common.c          |  84 ++
 drivers/clk/realtek/clk-rtk-common.h          |  37 +
 drivers/clk/realtek/freq_table.c              |  63 ++
 drivers/clk/realtek/freq_table.h              |  24 +
 .../dt-bindings/clock/realtek,rtd1625-clk.h   | 166 ++++
 22 files changed, 2585 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml
 create mode 100644 drivers/clk/realtek/Kconfig
 create mode 100644 drivers/clk/realtek/Makefile
 create mode 100644 drivers/clk/realtek/clk-regmap-gate.c
 create mode 100644 drivers/clk/realtek/clk-regmap-gate.h
 create mode 100644 drivers/clk/realtek/clk-regmap-mux.c
 create mode 100644 drivers/clk/realtek/clk-regmap-mux.h
 create mode 100644 drivers/clk/realtek/clk-regmap-pll-mmc.c
 create mode 100644 drivers/clk/realtek/clk-regmap-pll-mmc.h
 create mode 100644 drivers/clk/realtek/clk-regmap-pll.c
 create mode 100644 drivers/clk/realtek/clk-regmap-pll.h
 create mode 100644 drivers/clk/realtek/clk-rtd1625-crt.c
 create mode 100644 drivers/clk/realtek/clk-rtd1625-iso.c
 create mode 100644 drivers/clk/realtek/clk-rtk-common.c
 create mode 100644 drivers/clk/realtek/clk-rtk-common.h
 create mode 100644 drivers/clk/realtek/freq_table.c
 create mode 100644 drivers/clk/realtek/freq_table.h
 create mode 100644 include/dt-bindings/clock/realtek,rtd1625-clk.h
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v16 1/9 dt-bindings: clock: Add Realtek RTD1625 Clock & Reset Controller

Add DT binding schema for Realtek RTD1625 clock and reset controller

---
Changes in v16:
- Remove Krzysztof's reviewed-by tag.
- Add 'clock-name' property with a predefined enum list.
- Update 'clocks' property constraints.
- Move the reset ID header to the reset controller series.

---
 .../bindings/clock/realtek,rtd1625-clk.yaml   |  95 ++++++++++
 MAINTAINERS                                   |   6 +
 .../dt-bindings/clock/realtek,rtd1625-clk.h   | 166 ++++++++++++++++++
 3 files changed, 267 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml
 create mode 100644 include/dt-bindings/clock/realtek,rtd1625-clk.h
Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml new file +95 -0
new file mode 100644
index 000000000000..31213e401111
@@ -0,0 +1,95 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/realtek,rtd1625-clk.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Realtek RTD1625 Clock & Reset Controller
+
+maintainers:
+  - Cheng-Yu Lee <[email protected]>
+  - Yu-Chun Lin <[email protected]>
+
+description:
+  The Realtek RTD1625 Clock Controller manages and distributes clock
+  signals to various controllers and implements a Reset Controller for the
+  SoC peripherals.
+
+properties:
+  compatible:
+    enum:
+      - realtek,rtd1625-crt-clk
+      - realtek,rtd1625-iso-clk
+      - realtek,rtd1625-iso-s-clk
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    minItems: 1
+    maxItems: 5
+
+  clock-names:
+    minItems: 1
+    maxItems: 5
+
+  "#clock-cells":
+    const: 1
+
+  "#reset-cells":
+    const: 1
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - "#clock-cells"
+  - "#reset-cells"
+
+allOf:
+  - if:
+      properties:
+        compatible:
+          const: realtek,rtd1625-iso-clk
+
+    then:
+      properties:
+        clocks:
+          items:
+            - description: External 27 MHz oscillator
+            - description: CRT SYS clock input
+            - description: CRT SYSH clock input
+            - description: CRT DDSA divide-by-four clock input
+            - description: CRT miscellaneous gate clock input
+
+        clock-names:
+          items:
+            - const: osc27m
+            - const: clk_sys
+            - const: clk_sysh
+            - const: clk_ddsa_div4
+            - const: clk_en_misc
+
+    else:
+      properties:
+        clocks:
+          items:
+            - description: External 27 MHz oscillator
+
+        clock-names:
+          items:
+            - const: osc27m
+
+additionalProperties: false
+
+examples:
+  - |
+    clock-controller@98000000 {
+      compatible = "realtek,rtd1625-crt-clk";
+      reg = <0x98000000 0x1000>;
+      clocks = <&osc27m>;
+      clock-names = "osc27m";
+      #clock-cells = <1>;
+      #reset-cells = <1>;
+    };
MAINTAINERS +6 -0
index 93abbb315065..c51f9fc157c8 100644
@@ -23537,6 +23537,12 @@ S:	Maintained
 F:	include/sound/rt*.h
 F:	sound/soc/codecs/rt*
 
+REALTEK CLOCK DRIVERS
+M:	Cheng-Yu Lee <[email protected]>
+M:	Yu-Chun Lin <[email protected]>
+S:	Supported
+F:	include/dt-bindings/clock/realtek*
+
 REALTEK OTTO WATCHDOG
 M:	Sander Vanheule <[email protected]>
 L:	[email protected]
include/dt-bindings/clock/realtek,rtd1625-clk.h new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v16 1/9] dt-bindings: clock: Add Realtek RTD1625 Clock & Reset Controller

Quoted 7 lines · Yu-Chun Lin
Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml new file +7 -0
+    then:
+      properties:
+        clocks:
+          items:
+            - description: External 27 MHz oscillator
+            - description: CRT SYS clock input
+            - description: CRT SYSH clock input

Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml line 72

Quoted 10 lines
Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml new file +10 -0
+            - description: CRT DDSA divide-by-four clock input
+            - description: CRT miscellaneous gate clock input
+
+        clock-names:
+          items:
+            - const: osc27m
+            - const: clk_sys
+            - const: clk_sysh
+            - const: clk_ddsa_div4
+            - const: clk_en_misc

Put the list in the top level with minItems: 1, then here you only set
minItems: 5 or maxItems: 1.

Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml line 82

Quoted 10 lines
Documentation/devicetree/bindings/clock/realtek,rtd1625-clk.yaml new file +10 -0
+
+    else:
+      properties:
+        clocks:
+          items:
+            - description: External 27 MHz oscillator
+
+        clock-names:
+          items:
+            - const: osc27m

Best regards,
Krzysztof

author maintainer
#3 lore raw

PATCH v16 2/9 clk: realtek: Introduce common probe()

In reply to Yu-Chun Lin

From: Cheng-Yu Lee <[email protected]>

Add rtk_clk_probe() to set up the shared regmap, register clock hardware, and add the clock provider.

Additionally, if the "#reset-cells" property is present in the device tree, it creates and registers an auxiliary device using the provided aux_name. This allows the dedicated reset driver to bind to this device, enabling both clock and reset drivers to share the same regmap.

---
 MAINTAINERS                          |  1 +
 drivers/clk/Kconfig                  |  1 +
 drivers/clk/Makefile                 |  1 +
 drivers/clk/realtek/Kconfig          | 29 ++++++++++
 drivers/clk/realtek/Makefile         |  4 ++
 drivers/clk/realtek/clk-rtk-common.c | 84 ++++++++++++++++++++++++++++
 drivers/clk/realtek/clk-rtk-common.h | 37 ++++++++++++
 7 files changed, 157 insertions(+)
 create mode 100644 drivers/clk/realtek/Kconfig
 create mode 100644 drivers/clk/realtek/Makefile
 create mode 100644 drivers/clk/realtek/clk-rtk-common.c
 create mode 100644 drivers/clk/realtek/clk-rtk-common.h
MAINTAINERS +1 -0
index c51f9fc157c8..54873a33038b 100644
@@ -23541,6 +23541,7 @@ REALTEK CLOCK DRIVERS
 M:	Cheng-Yu Lee <[email protected]>
 M:	Yu-Chun Lin <[email protected]>
 S:	Supported
+F:	drivers/clk/realtek/*
 F:	include/dt-bindings/clock/realtek*
 
 REALTEK OTTO WATCHDOG
drivers/clk/Kconfig +1 -0
index 37a8e35d4178..eb9256459251 100644
@@ -532,6 +532,7 @@ source "drivers/clk/nuvoton/Kconfig"
 source "drivers/clk/pistachio/Kconfig"
 source "drivers/clk/qcom/Kconfig"
 source "drivers/clk/ralink/Kconfig"
+source "drivers/clk/realtek/Kconfig"
 source "drivers/clk/renesas/Kconfig"
 source "drivers/clk/rockchip/Kconfig"
 source "drivers/clk/samsung/Kconfig"
drivers/clk/Makefile +1 -0
index 52d138d4ffc8..3930bf20667b 100644
@@ -156,6 +156,7 @@ obj-$(CONFIG_COMMON_CLK_PISTACHIO)	+= pistachio/
 obj-$(CONFIG_COMMON_CLK_PXA)		+= pxa/
 obj-$(CONFIG_COMMON_CLK_QCOM)		+= qcom/
 obj-y					+= ralink/
+obj-$(CONFIG_COMMON_CLK_REALTEK)	+= realtek/
 obj-y					+= renesas/
 obj-$(CONFIG_COMMON_CLK_ROCKCHIP)	+= rockchip/
 obj-$(CONFIG_COMMON_CLK_SAMSUNG)	+= samsung/
drivers/clk/realtek/Kconfig new file +29 -0
new file mode 100644
index 000000000000..e8deccf93f94
@@ -0,0 +1,29 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config COMMON_CLK_REALTEK
+	tristate "Clock driver for Realtek SoCs"
+	depends on ARCH_REALTEK || COMPILE_TEST
+	default ARCH_REALTEK
+	help
+	  Enable the common clock framework infrastructure for Realtek
+	  system-on-chip platforms.
+
+	  This provides the base support required by individual Realtek
+	  clock controller drivers to expose clocks to peripheral devices.
+
+	  If you have a Realtek-based platform, say Y.
+
+if COMMON_CLK_REALTEK
+
+config RTK_CLK_COMMON
+	tristate "Realtek Clock Common"
+	select AUXILIARY_BUS
+	select REGMAP_MMIO
+	help
+	  Common helper code shared by Realtek clock controller drivers.
+
+	  This provides utility functions and data structures used by
+	  multiple Realtek clock implementations. It also provides the
+	  infrastructure to spawn reset controllers via the auxiliary bus
+	  where required.
+
+endif
drivers/clk/realtek/Makefile new file +4 -0
new file mode 100644
index 000000000000..13000ed4ba11
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_RTK_CLK_COMMON) += clk-rtk.o
+
+clk-rtk-y += clk-rtk-common.o
drivers/clk/realtek/clk-rtk-common.c new file +84 -0

84 lines not on this page · raw message

drivers/clk/realtek/clk-rtk-common.h new file +37 -0
new file mode 100644
index 000000000000..de3f23120b53
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2016-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#ifndef __CLK_REALTEK_COMMON_H
+#define __CLK_REALTEK_COMMON_H
+
+#include <linux/clk-provider.h>
+
+#define __rtk_clk_regmap_hw(_p) ((_p)->hw)
+
+struct device;
+struct platform_device;
+struct regmap;
+
+struct rtk_clk_regmap {
+	struct clk_hw hw;
+	struct regmap *regmap;
+};
+
+struct rtk_clk_desc {
+	struct clk_hw_onecell_data *clk_data;
+	struct rtk_clk_regmap **clks;
+	size_t num_clks;
+	char *aux_name;
+};
+
+static inline struct rtk_clk_regmap *to_rtk_clk_regmap(struct clk_hw *hw)
+{
+	return container_of(hw, struct rtk_clk_regmap, hw);
+}
+
+int rtk_clk_probe(struct platform_device *pdev, const struct rtk_clk_desc *desc);
+
+#endif /* __CLK_REALTEK_COMMON_H */
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v16 2/9] clk: realtek: Introduce common probe()

Quoted 85 lines

Add rtk_clk_probe() to set up the shared regmap, register clock hardware, and add the clock provider.

Additionally, if the "#reset-cells" property is present in the device tree, it creates and registers an auxiliary device using the provided aux_name. This allows the dedicated reset driver to bind to this device, enabling both clock and reset drivers to share the same regmap.

---
 MAINTAINERS                          |  1 +
 drivers/clk/Kconfig                  |  1 +
 drivers/clk/Makefile                 |  1 +
 drivers/clk/realtek/Kconfig          | 29 ++++++++++
 drivers/clk/realtek/Makefile         |  4 ++
 drivers/clk/realtek/clk-rtk-common.c | 84 ++++++++++++++++++++++++++++
 drivers/clk/realtek/clk-rtk-common.h | 37 ++++++++++++
 7 files changed, 157 insertions(+)
 create mode 100644 drivers/clk/realtek/Kconfig
 create mode 100644 drivers/clk/realtek/Makefile
 create mode 100644 drivers/clk/realtek/clk-rtk-common.c
 create mode 100644 drivers/clk/realtek/clk-rtk-common.h
MAINTAINERS +1 -0
index c51f9fc157c8..54873a33038b 100644
@@ -23541,6 +23541,7 @@ REALTEK CLOCK DRIVERS
 M:	Cheng-Yu Lee <[email protected]>
 M:	Yu-Chun Lin <[email protected]>
 S:	Supported
+F:	drivers/clk/realtek/*
 F:	include/dt-bindings/clock/realtek*
 
 REALTEK OTTO WATCHDOG
drivers/clk/Kconfig +1 -0
index 37a8e35d4178..eb9256459251 100644
@@ -532,6 +532,7 @@ source "drivers/clk/nuvoton/Kconfig"
 source "drivers/clk/pistachio/Kconfig"
 source "drivers/clk/qcom/Kconfig"
 source "drivers/clk/ralink/Kconfig"
+source "drivers/clk/realtek/Kconfig"
 source "drivers/clk/renesas/Kconfig"
 source "drivers/clk/rockchip/Kconfig"
 source "drivers/clk/samsung/Kconfig"
drivers/clk/Makefile +1 -0
index 52d138d4ffc8..3930bf20667b 100644
@@ -156,6 +156,7 @@ obj-$(CONFIG_COMMON_CLK_PISTACHIO)	+= pistachio/
 obj-$(CONFIG_COMMON_CLK_PXA)		+= pxa/
 obj-$(CONFIG_COMMON_CLK_QCOM)		+= qcom/
 obj-y					+= ralink/
+obj-$(CONFIG_COMMON_CLK_REALTEK)	+= realtek/
 obj-y					+= renesas/
 obj-$(CONFIG_COMMON_CLK_ROCKCHIP)	+= rockchip/
 obj-$(CONFIG_COMMON_CLK_SAMSUNG)	+= samsung/
drivers/clk/realtek/Kconfig new file +17 -0
new file mode 100644
index 000000000000..e8deccf93f94
@@ -0,0 +1,29 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config COMMON_CLK_REALTEK
+	tristate "Clock driver for Realtek SoCs"
+	depends on ARCH_REALTEK || COMPILE_TEST
+	default ARCH_REALTEK
+	help
+	  Enable the common clock framework infrastructure for Realtek
+	  system-on-chip platforms.
+
+	  This provides the base support required by individual Realtek
+	  clock controller drivers to expose clocks to peripheral devices.
+
+	  If you have a Realtek-based platform, say Y.
+
+if COMMON_CLK_REALTEK
+
+config RTK_CLK_COMMON

drivers/clk/realtek/Kconfig line 27

Quoted 10 lines
drivers/clk/realtek/Kconfig new file +10 -0
+	tristate "Realtek Clock Common"
+	select AUXILIARY_BUS
+	select REGMAP_MMIO
+	help
+	  Common helper code shared by Realtek clock controller drivers.
+
+	  This provides utility functions and data structures used by
+	  multiple Realtek clock implementations. It also provides the
+	  infrastructure to spawn reset controllers via the auxiliary bus
+	  where required.

Drop the prompt text because this isn't meant to be user selectable.

Quoted 125 lines
drivers/clk/realtek/Kconfig new file +2 -0
+
+endif
drivers/clk/realtek/Makefile new file +4 -0
new file mode 100644
index 000000000000..13000ed4ba11
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_RTK_CLK_COMMON) += clk-rtk.o
+
+clk-rtk-y += clk-rtk-common.o
drivers/clk/realtek/clk-rtk-common.c new file +84 -0
new file mode 100644
index 000000000000..7df6d04bb95a
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2019-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/device.h>
+#include <linux/export.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include "clk-rtk-common.h"
+
+static int rtk_reset_controller_register(struct device *dev, const char *aux_name,
+					 struct regmap *map)
+{
+	struct auxiliary_device *adev;
+
+	if (!of_property_present(dev->of_node, "#reset-cells"))
+		return 0;
+
+	if (!aux_name) {
+		dev_err(dev, "DTS requires reset controller, but aux_name is missing\n");
+		return -EINVAL;
+	}
+
+	adev = devm_auxiliary_device_create(dev, aux_name, (void *)map);
+	if (!adev)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static const struct regmap_config rtk_clk_regmap_config = {
+	.reg_bits = 32,
+	.reg_stride = 4,
+	.val_bits = 32,
+};
+
+int rtk_clk_probe(struct platform_device *pdev, const struct rtk_clk_desc *desc)
+{
+	struct device *dev = &pdev->dev;
+	struct regmap *regmap;
+	void __iomem *base;
+	struct clk_hw *hw;
+	int i, ret;
+
+	base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	regmap = devm_regmap_init_mmio(dev, base, &rtk_clk_regmap_config);
+	if (IS_ERR(regmap))
+		return PTR_ERR(regmap);
+
+	for (i = 0; i < desc->num_clks; i++)
+		desc->clks[i]->regmap = regmap;
+
+	for (i = 0; i < desc->clk_data->num; i++) {
+		hw = desc->clk_data->hws[i];
+		if (!hw)
+			continue;
+
+		ret = devm_clk_hw_register(dev, hw);
+		if (ret)
+			return dev_err_probe(dev, ret, "failed to register hw of clk%d\n", i);
+	}
+
+	ret = rtk_reset_controller_register(dev, desc->aux_name, regmap);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to register reset controller\n");
+
+	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
+					  desc->clk_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add clock provider\n");
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(rtk_clk_probe, "CLK_REALTEK");
+
+MODULE_DESCRIPTION("Realtek clock infrastructure");
+MODULE_LICENSE("GPL");
drivers/clk/realtek/clk-rtk-common.h new file +17 -0
new file mode 100644
index 000000000000..de3f23120b53
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2016-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#ifndef __CLK_REALTEK_COMMON_H
+#define __CLK_REALTEK_COMMON_H
+
+#include <linux/clk-provider.h>
+
+#define __rtk_clk_regmap_hw(_p) ((_p)->hw)
+
+struct device;
+struct platform_device;
+struct regmap;
+

drivers/clk/realtek/clk-rtk-common.h line 27

Quoted 10 lines
drivers/clk/realtek/clk-rtk-common.h new file +10 -0
+struct rtk_clk_regmap {
+	struct clk_hw hw;
+	struct regmap *regmap;
+};
+
+struct rtk_clk_desc {
+	struct clk_hw_onecell_data *clk_data;
+	struct rtk_clk_regmap **clks;
+	size_t num_clks;
+	char *aux_name;

Can this be const?

Brian

Signature · 1 line
Brian Masney <[email protected]>
author maintainer
#5 lore raw

PATCH v16 3/9 clk: realtek: Add support for phase locked loops (PLLs)

In reply to Yu-Chun Lin

From: Cheng-Yu Lee <[email protected]>

Provide a full set of PLL operations for programmable PLLs and a read-only variant for fixed or hardware-managed PLLs.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Introduce 'expected_parent_rate' validation in '.determine_rate()' and '.set_rate()' to safely enforce the fixed input rate requirement.
- In determined_rate, replacing the floor/ceil algorithm with a closest-match search within [min_rate, max_rate] boundaries.
- Improve comments for spinlock.
- Add '.is_enabled' callback to 'rtk_clk_pll_ro_ops'

---
 drivers/clk/realtek/Makefile         |   2 +
 drivers/clk/realtek/clk-regmap-pll.c | 219 +++++++++++++++++++++++++++
 drivers/clk/realtek/clk-regmap-pll.h |  53 +++++++
 drivers/clk/realtek/freq_table.c     |  62 ++++++++
 drivers/clk/realtek/freq_table.h     |  24 +++
 5 files changed, 360 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-regmap-pll.c
 create mode 100644 drivers/clk/realtek/clk-regmap-pll.h
 create mode 100644 drivers/clk/realtek/freq_table.c
 create mode 100644 drivers/clk/realtek/freq_table.h
drivers/clk/realtek/Makefile +2 -0
index 13000ed4ba11..6fbe6e07b760 100644
@@ -2,3 +2,5 @@
 obj-$(CONFIG_RTK_CLK_COMMON) += clk-rtk.o
 
 clk-rtk-y += clk-rtk-common.o
+clk-rtk-y += clk-regmap-pll.o
+clk-rtk-y += freq_table.o
drivers/clk/realtek/clk-regmap-pll.c new file +219 -0

219 lines not on this page · raw message

drivers/clk/realtek/clk-regmap-pll.h new file +53 -0

53 lines not on this page · raw message

drivers/clk/realtek/freq_table.c new file +62 -0

62 lines not on this page · raw message

drivers/clk/realtek/freq_table.h new file +24 -0
new file mode 100644
index 000000000000..801f4882a891
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __CLK_REALTEK_FREQ_TABLE_H
+#define __CLK_REALTEK_FREQ_TABLE_H
+
+struct rtk_freq_table {
+	u32 val;
+	unsigned long rate;
+};
+
+#define FREQ_TABLE_END    \
+	{                 \
+		.rate = 0 \
+	}
+
+const struct rtk_freq_table *ftbl_find_closest_by_rate(const struct rtk_freq_table *ftbl,
+						       unsigned long rate,
+						       unsigned long min_rate,
+						       unsigned long max_rate);
+const struct rtk_freq_table *ftbl_find_by_rate(const struct rtk_freq_table *ftbl,
+					       unsigned long rate);
+const struct rtk_freq_table *
+ftbl_find_by_val_with_mask(const struct rtk_freq_table *ftbl, u32 mask, u32 value);
+
+#endif /* __CLK_REALTEK_FREQ_TABLE_H */
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v16 3/9] clk: realtek: Add support for phase locked loops (PLLs)

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] clk: realtek: missing linux/limits.h include in freq_table.c breaks build --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] clk: realtek: missing linux/limits.h include in freq_table.c breaks build
--

commit 05422d9707f4d9d5ef01c91a7d3ca363d63c729c
Author: Cheng-Yu Lee <[email protected]>

clk: realtek: Add support for phase locked loops (PLLs)

This commit adds regmap-based PLL clock operations and frequency table lookup helpers for Realtek SoCs.

Quoted 10 lines
drivers/clk/realtek/freq_table.c new file +4 -0
new file mode 100644
index 0000000000000..0f977a3fc9021
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/types.h>
+#include "freq_table.h"

drivers/clk/realtek/freq_table.c line 14

Quoted 10 lines
drivers/clk/realtek/freq_table.c new file +10 -0
+
+#define IS_FREQ_TABLE_END(_f) ((_f)->rate == 0)
+
+const struct rtk_freq_table *ftbl_find_closest_by_rate(const struct rtk_freq_table *ftbl,
+						       unsigned long rate,
+						       unsigned long min_rate,
+						       unsigned long max_rate)
+{
+	const struct rtk_freq_table *best = NULL;
+	unsigned long best_delta = ULONG_MAX;

[Severity: High]
Does freq_table.c need to include <linux/limits.h> for ULONG_MAX?

Neither <linux/types.h> nor freq_table.h pulls in <linux/limits.h>, so building with CONFIG_RTK_CLK_COMMON enabled fails at this commit with an undeclared identifier error for ULONG_MAX. The include is added later in the series by commit "clk: realtek: Add support for MMC-tuned PLL clocks", leaving the intermediate commits unbuildable during bisection.

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

Re: [PATCH v16 3/9] clk: realtek: Add support for phase locked loops (PLLs)

In reply to Yu-Chun Lin
Quoted 151 lines

Provide a full set of PLL operations for programmable PLLs and a read-only variant for fixed or hardware-managed PLLs.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Introduce 'expected_parent_rate' validation in '.determine_rate()' and '.set_rate()' to safely enforce the fixed input rate requirement.
- In determined_rate, replacing the floor/ceil algorithm with a closest-match search within [min_rate, max_rate] boundaries.
- Improve comments for spinlock.
- Add '.is_enabled' callback to 'rtk_clk_pll_ro_ops'

---
 drivers/clk/realtek/Makefile         |   2 +
 drivers/clk/realtek/clk-regmap-pll.c | 219 +++++++++++++++++++++++++++
 drivers/clk/realtek/clk-regmap-pll.h |  53 +++++++
 drivers/clk/realtek/freq_table.c     |  62 ++++++++
 drivers/clk/realtek/freq_table.h     |  24 +++
 5 files changed, 360 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-regmap-pll.c
 create mode 100644 drivers/clk/realtek/clk-regmap-pll.h
 create mode 100644 drivers/clk/realtek/freq_table.c
 create mode 100644 drivers/clk/realtek/freq_table.h
drivers/clk/realtek/Makefile +2 -0
index 13000ed4ba11..6fbe6e07b760 100644
@@ -2,3 +2,5 @@
 obj-$(CONFIG_RTK_CLK_COMMON) += clk-rtk.o
 
 clk-rtk-y += clk-rtk-common.o
+clk-rtk-y += clk-regmap-pll.o
+clk-rtk-y += freq_table.o
drivers/clk/realtek/clk-regmap-pll.c new file +108 -0
new file mode 100644
index 000000000000..e43e78a36647
@@ -0,0 +1,219 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#include <linux/export.h>
+#include <linux/regmap.h>
+#include <linux/spinlock.h>
+#include "clk-regmap-pll.h"
+
+#define TIMEOUT 500
+
+static inline struct rtk_clk_regmap_pll *to_rtk_clk_regmap_pll(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap *clkr = to_rtk_clk_regmap(hw);
+
+	return container_of(clkr, struct rtk_clk_regmap_pll, clkr);
+}
+
+static int wait_freq_ready(struct rtk_clk_regmap_pll *clkp)
+{
+	u32 pollval;
+
+	/*
+	 * Optional register: return success if not provided.
+	 */
+	if (!clkp->freq_ready_reg)
+		return 0;
+
+	return regmap_read_poll_timeout_atomic(clkp->clkr.regmap, clkp->freq_ready_reg, pollval,
+		(pollval & clkp->freq_ready_mask) == clkp->freq_ready_val, 1, TIMEOUT);
+}
+
+static bool is_power_on(struct rtk_clk_regmap_pll *clkp)
+{
+	u32 val;
+
+	/*
+	 * If the PLL does not provide a power register, it is considered
+	 * to be always powered.
+	 */
+	if (!clkp->power_reg)
+		return true;
+
+	if (regmap_read(clkp->clkr.regmap, clkp->power_reg, &val))
+		return false;
+
+	return (val & clkp->power_mask) == clkp->power_val_on;
+}
+
+static void rtk_clk_regmap_pll_disable(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_pll *clkp = to_rtk_clk_regmap_pll(hw);
+	unsigned long flags;
+
+	if (!clkp->seq_power_off)
+		return;
+
+	spin_lock_irqsave(&clkp->lock, flags);
+
+	regmap_multi_reg_write(clkp->clkr.regmap, clkp->seq_power_off,
+			       clkp->num_seq_power_off);
+
+	spin_unlock_irqrestore(&clkp->lock, flags);
+}
+
+static int rtk_clk_regmap_pll_is_enabled(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_pll *clkp = to_rtk_clk_regmap_pll(hw);
+	unsigned long flags;
+	int ret;
+
+	spin_lock_irqsave(&clkp->lock, flags);
+
+	ret = is_power_on(clkp);
+
+	spin_unlock_irqrestore(&clkp->lock, flags);
+
+	return ret;
+}
+
+static int rtk_clk_regmap_pll_determine_rate(struct clk_hw *hw,
+					     struct clk_rate_request *req)
+{
+	struct rtk_clk_regmap_pll *clkp = to_rtk_clk_regmap_pll(hw);
+	const struct rtk_freq_table *ftblv = NULL;
+
+	if (clkp->expected_parent_rate &&
+	    req->best_parent_rate != clkp->expected_parent_rate)
+		return -EINVAL;
+
+	ftblv = ftbl_find_closest_by_rate(clkp->freq_tbl, req->rate,
+					  req->min_rate, req->max_rate);
+	if (!ftblv)
+		return -EINVAL;
+
+	req->rate = ftblv->rate;
+
+	return 0;
+}
+
+static unsigned long rtk_clk_regmap_pll_recalc_rate(struct clk_hw *hw,
+						    unsigned long parent_rate)
+{
+	struct rtk_clk_regmap_pll *clkp = to_rtk_clk_regmap_pll(hw);
+	const struct rtk_freq_table *fv;
+	unsigned long flags;

drivers/clk/realtek/clk-regmap-pll.c line 118

Quoted 10 lines
drivers/clk/realtek/clk-regmap-pll.c new file +10 -0
+	u32 freq_val;
+
+	spin_lock_irqsave(&clkp->lock, flags);
+
+	if (regmap_read(clkp->clkr.regmap, clkp->freq_reg, &freq_val)) {
+		spin_unlock_irqrestore(&clkp->lock, flags);
+		return 0;
+	}
+
+	freq_val &= clkp->freq_mask;

ftbl_find_by_val_with_mask() performs the mask operation as well, so this is redundant. Just pick one way.

Quoted 67 lines
drivers/clk/realtek/clk-regmap-pll.c new file +67 -0
+
+	fv = ftbl_find_by_val_with_mask(clkp->freq_tbl, clkp->freq_mask,
+					freq_val);
+
+	spin_unlock_irqrestore(&clkp->lock, flags);
+
+	return fv ? fv->rate : 0;
+}
+
+static int rtk_clk_regmap_pll_set_rate(struct clk_hw *hw, unsigned long rate,
+				       unsigned long parent_rate)
+{
+	struct rtk_clk_regmap_pll *clkp = to_rtk_clk_regmap_pll(hw);
+	const struct rtk_freq_table *fv;
+	unsigned long flags;
+	int ret;
+
+	if (clkp->expected_parent_rate && parent_rate != clkp->expected_parent_rate)
+		return -EINVAL;
+
+	fv = ftbl_find_by_rate(clkp->freq_tbl, rate);
+	if (!fv || fv->rate != rate)
+		return -EINVAL;
+
+	spin_lock_irqsave(&clkp->lock, flags);
+
+	if (clkp->seq_pre_set_freq) {
+		ret = regmap_multi_reg_write(clkp->clkr.regmap, clkp->seq_pre_set_freq,
+					     clkp->num_seq_pre_set_freq);
+		if (ret)
+			goto unlock;
+	}
+
+	ret = regmap_update_bits(clkp->clkr.regmap, clkp->freq_reg,
+				 clkp->freq_mask, fv->val);
+	if (ret)
+		goto unlock;
+
+	if (clkp->seq_post_set_freq) {
+		ret = regmap_multi_reg_write(clkp->clkr.regmap, clkp->seq_post_set_freq,
+					     clkp->num_seq_post_set_freq);
+		if (ret)
+			goto unlock;
+	}
+
+	if (is_power_on(clkp)) {
+		ret = wait_freq_ready(clkp);
+		if (ret)
+			goto unlock;
+	}
+
+unlock:
+	spin_unlock_irqrestore(&clkp->lock, flags);
+
+	return ret;
+}
+
+static int rtk_clk_regmap_pll_enable(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_pll *clkp = to_rtk_clk_regmap_pll(hw);
+	unsigned long flags;
+	int ret = 0;
+
+	if (!clkp->seq_power_on)
+		return ret;
+
+	spin_lock_irqsave(&clkp->lock, flags);

drivers/clk/realtek/clk-regmap-pll.c line 195

Quoted 10 lines
drivers/clk/realtek/clk-regmap-pll.c new file +10 -0
+
+	if (is_power_on(clkp))
+		goto unlock;
+
+	ret = regmap_multi_reg_write(clkp->clkr.regmap, clkp->seq_power_on,
+				     clkp->num_seq_power_on);
+	if (ret)
+		goto unlock;
+
+	ret = wait_freq_ready(clkp);

This can wait up to 500 us with interrupts disabled. Should this move to a prepare lock instead with a mutex?

drivers/clk/realtek/clk-regmap-pll.c line 173

Quoted 7 lines
drivers/clk/realtek/clk-regmap-pll.c new file +7 -0
+	if (ret)
+		goto unlock;
+
+unlock:
+	spin_unlock_irqrestore(&clkp->lock, flags);
+
+	return ret;

+}

Quoted 85 lines
drivers/clk/realtek/clk-regmap-pll.c new file +16 -0
+
+const struct clk_ops rtk_clk_pll_ops = {
+	.enable         = rtk_clk_regmap_pll_enable,
+	.disable        = rtk_clk_regmap_pll_disable,
+	.is_enabled     = rtk_clk_regmap_pll_is_enabled,
+	.recalc_rate    = rtk_clk_regmap_pll_recalc_rate,
+	.determine_rate = rtk_clk_regmap_pll_determine_rate,
+	.set_rate       = rtk_clk_regmap_pll_set_rate,
+};
+EXPORT_SYMBOL_NS_GPL(rtk_clk_pll_ops, "CLK_REALTEK");
+
+const struct clk_ops rtk_clk_pll_ro_ops = {
+	.is_enabled     = rtk_clk_regmap_pll_is_enabled,
+	.recalc_rate = rtk_clk_regmap_pll_recalc_rate,
+};
+EXPORT_SYMBOL_NS_GPL(rtk_clk_pll_ro_ops, "CLK_REALTEK");
drivers/clk/realtek/clk-regmap-pll.h new file +53 -0
new file mode 100644
index 000000000000..f6fac6b4ac90
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2017-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#ifndef __CLK_REALTEK_CLK_PLL_H
+#define __CLK_REALTEK_CLK_PLL_H
+
+#include <linux/spinlock.h>
+#include "clk-rtk-common.h"
+#include "freq_table.h"
+
+struct reg_sequence;
+
+struct rtk_clk_regmap_pll {
+	struct rtk_clk_regmap clkr;
+	const struct reg_sequence *seq_power_on;
+	u32 num_seq_power_on;
+	const struct reg_sequence *seq_power_off;
+	u32 num_seq_power_off;
+	const struct reg_sequence *seq_pre_set_freq;
+	u32 num_seq_pre_set_freq;
+	const struct reg_sequence *seq_post_set_freq;
+	u32 num_seq_post_set_freq;
+	const struct rtk_freq_table *freq_tbl;
+	u32 freq_reg;
+	u32 freq_mask;
+	u32 freq_ready_mask;
+	u32 freq_ready_reg;
+	u32 freq_ready_val;
+	u32 power_reg;
+	u32 power_mask;
+	u32 power_val_on;
+
+	/* Expected input rate for the preset frequency table; zero disables check. */
+	unsigned long expected_parent_rate;
+
+	/*
+	 * Serialize the multi-step register sequences in .set_rate() against
+	 * .enable()/.disable(). CCF protects them with different locks
+	 * (prepare_lock vs. enable_lock), and regmap only guarantees atomicity of
+	 * individual register accesses.
+	 */
+	spinlock_t lock;
+};
+
+#define __rtk_clk_regmap_pll_hw(_ptr)  __rtk_clk_regmap_hw(&(_ptr)->clkr)
+
+extern const struct clk_ops rtk_clk_pll_ops;
+extern const struct clk_ops rtk_clk_pll_ro_ops;
+
+#endif /* __CLK_REALTEK_CLK_PLL_H */
drivers/clk/realtek/freq_table.c new file +4 -0
new file mode 100644
index 000000000000..0f977a3fc902
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/types.h>
+#include "freq_table.h"

drivers/clk/realtek/freq_table.c line 14

Quoted 10 lines
drivers/clk/realtek/freq_table.c new file +10 -0
+
+#define IS_FREQ_TABLE_END(_f) ((_f)->rate == 0)
+
+const struct rtk_freq_table *ftbl_find_closest_by_rate(const struct rtk_freq_table *ftbl,
+						       unsigned long rate,
+						       unsigned long min_rate,
+						       unsigned long max_rate)
+{
+	const struct rtk_freq_table *best = NULL;
+	unsigned long best_delta = ULONG_MAX;

From sashiko: #include <linux/limits.h> for ULONG_MAX.

There's some other sashiko feedback on these files and I'm not going to duplicate that.

Brian

Signature · 1 line
Brian Masney <[email protected]>
author maintainer
#8 lore raw

PATCH v16 4/9 clk: realtek: Add support for gate clock

In reply to Yu-Chun Lin

From: Cheng-Yu Lee <[email protected]>

Introduce clk_regmap_gate_ops supporting enable, disable, and is_enabled for standard regmap gate clocks. To accommodate hardware designs, the enable and disable operations handle a 'write_en' flag, which asserts an adjacent write-enable bit (bit_idx + 1) simultaneously when modifying the gate bit.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Replace 'CLK_HW_INIT' with 'CLK_HW_INIT_HW' and 'CLK_HW_INIT_FW_NAME' macros to avoid relying on unstable internal CCF global names.
- Drop all unused NO_PARENT macro and 'rtk_clk_regmap_gate_ro_ops'.

---
 drivers/clk/realtek/Makefile          |  1 +
 drivers/clk/realtek/clk-regmap-gate.c | 59 +++++++++++++++++++++++++++
 drivers/clk/realtek/clk-regmap-gate.h | 54 ++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-regmap-gate.c
 create mode 100644 drivers/clk/realtek/clk-regmap-gate.h
drivers/clk/realtek/Makefile +1 -0
index 6fbe6e07b760..9397d677f52d 100644
@@ -2,5 +2,6 @@
 obj-$(CONFIG_RTK_CLK_COMMON) += clk-rtk.o
 
 clk-rtk-y += clk-rtk-common.o
+clk-rtk-y += clk-regmap-gate.o
 clk-rtk-y += clk-regmap-pll.o
 clk-rtk-y += freq_table.o
drivers/clk/realtek/clk-regmap-gate.c new file +59 -0

59 lines not on this page · raw message

drivers/clk/realtek/clk-regmap-gate.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH v16 4/9] clk: realtek: Add support for gate clock

Quoted 79 lines

Introduce clk_regmap_gate_ops supporting enable, disable, and is_enabled for standard regmap gate clocks. To accommodate hardware designs, the enable and disable operations handle a 'write_en' flag, which asserts an adjacent write-enable bit (bit_idx + 1) simultaneously when modifying the gate bit.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Replace 'CLK_HW_INIT' with 'CLK_HW_INIT_HW' and 'CLK_HW_INIT_FW_NAME' macros to avoid relying on unstable internal CCF global names.
- Drop all unused NO_PARENT macro and 'rtk_clk_regmap_gate_ro_ops'.

---
 drivers/clk/realtek/Makefile          |  1 +
 drivers/clk/realtek/clk-regmap-gate.c | 59 +++++++++++++++++++++++++++
 drivers/clk/realtek/clk-regmap-gate.h | 54 ++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-regmap-gate.c
 create mode 100644 drivers/clk/realtek/clk-regmap-gate.h
drivers/clk/realtek/Makefile +1 -0
index 6fbe6e07b760..9397d677f52d 100644
@@ -2,5 +2,6 @@
 obj-$(CONFIG_RTK_CLK_COMMON) += clk-rtk.o
 
 clk-rtk-y += clk-rtk-common.o
+clk-rtk-y += clk-regmap-gate.o
 clk-rtk-y += clk-regmap-pll.o
 clk-rtk-y += freq_table.o
drivers/clk/realtek/clk-regmap-gate.c new file +39 -0
new file mode 100644
index 000000000000..12539e11647c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2017-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#include <linux/bits.h>
+#include <linux/clk-provider.h>
+#include <linux/export.h>
+#include <linux/regmap.h>
+#include "clk-regmap-gate.h"
+
+static int rtk_clk_regmap_gate_update(struct clk_hw *hw, bool enable)
+{
+	struct rtk_clk_regmap_gate *clkg = to_rtk_clk_regmap_gate(hw);
+	unsigned int mask = BIT(clkg->bit_idx);
+	unsigned int val = enable ? mask : 0;
+
+	/*
+	 * Hardware specific design: to modify the gate bit, the adjacent
+	 * write-enable bit (bit_idx + 1) must be asserted simultaneously.
+	 */
+	if (clkg->write_en) {
+		mask |= BIT(clkg->bit_idx + 1);
+		val |= BIT(clkg->bit_idx + 1);
+	}
+
+	return regmap_update_bits(clkg->clkr.regmap, clkg->gate_ofs, mask, val);
+}
+
+static int rtk_clk_regmap_gate_enable(struct clk_hw *hw)
+{
+	return rtk_clk_regmap_gate_update(hw, true);
+}
+
+static void rtk_clk_regmap_gate_disable(struct clk_hw *hw)
+{
+	rtk_clk_regmap_gate_update(hw, false);
+}

drivers/clk/realtek/clk-regmap-gate.c line 49

Quoted 10 lines
drivers/clk/realtek/clk-regmap-gate.c new file +10 -0
+
+static int rtk_clk_regmap_gate_is_enabled(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_gate *clkg = to_rtk_clk_regmap_gate(hw);
+	int ret;
+	u32 val;
+
+	ret = regmap_read(clkg->clkr.regmap, clkg->gate_ofs, &val);
+	if (ret < 0)
+		return ret;

This becomes a bool in clk_core_is_enabled(). Either return 0 or 1.

Brian

Signature · 1 line
Brian Masney <[email protected]>
author maintainer
#10 lore raw

PATCH v16 5/9 clk: realtek: Add support for mux clock

In reply to Yu-Chun Lin

From: Cheng-Yu Lee <[email protected]>

Add a simple regmap-based clk_ops implementation for Realtek mux clocks.

The implementation supports parent selection and rate determination through regmap-backed register access.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Replace 'CLK_HW_INIT_PARENTS' with 'CLK_HW_INIT_PARENTS_HW' to use '.parent_hws'

---
 drivers/clk/realtek/Makefile         |  1 +
 drivers/clk/realtek/clk-regmap-mux.c | 40 ++++++++++++++++++++++++++
 drivers/clk/realtek/clk-regmap-mux.h | 42 ++++++++++++++++++++++++++++
 3 files changed, 83 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-regmap-mux.c
 create mode 100644 drivers/clk/realtek/clk-regmap-mux.h
drivers/clk/realtek/Makefile +1 -0
index 9397d677f52d..4be39e2dd5b4 100644
@@ -3,5 +3,6 @@ obj-$(CONFIG_RTK_CLK_COMMON) += clk-rtk.o
 
 clk-rtk-y += clk-rtk-common.o
 clk-rtk-y += clk-regmap-gate.o
+clk-rtk-y += clk-regmap-mux.o
 clk-rtk-y += clk-regmap-pll.o
 clk-rtk-y += freq_table.o
drivers/clk/realtek/clk-regmap-mux.c new file +40 -0
new file mode 100644
index 000000000000..ba86f314bb5e
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2017-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#include <linux/clk-provider.h>
+#include <linux/export.h>
+#include <linux/regmap.h>
+#include "clk-regmap-mux.h"
+
+static u8 rtk_clk_regmap_mux_get_parent(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_mux *clkm = to_rtk_clk_regmap_mux(hw);
+	u32 val;
+	int ret;
+
+	ret = regmap_read(clkm->clkr.regmap, clkm->mux_ofs, &val);
+	if (ret)
+		return 0xff;
+
+	val = (val >> clkm->shift) & clkm->mask;
+
+	return val;
+}
+
+static int rtk_clk_regmap_mux_set_parent(struct clk_hw *hw, u8 index)
+{
+	struct rtk_clk_regmap_mux *clkm = to_rtk_clk_regmap_mux(hw);
+
+	return regmap_update_bits(clkm->clkr.regmap, clkm->mux_ofs,
+				  clkm->mask << clkm->shift, (u32)index << clkm->shift);
+}
+
+const struct clk_ops rtk_clk_regmap_mux_ops = {
+	.set_parent = rtk_clk_regmap_mux_set_parent,
+	.get_parent = rtk_clk_regmap_mux_get_parent,
+	.determine_rate = __clk_mux_determine_rate,
+};
+EXPORT_SYMBOL_NS_GPL(rtk_clk_regmap_mux_ops, "CLK_REALTEK");
drivers/clk/realtek/clk-regmap-mux.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#11 lore raw

PATCH v16 6/9 clk: realtek: Add support for MMC-tuned PLL clocks

In reply to Yu-Chun Lin

From: Cheng-Yu Lee <[email protected]>

Add clk_pll_mmc_ops for enable/disable, rate control, and status operations on MMC PLL clocks.

Also add clk_pll_mmc_phase_ops to support phase get/set operations.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Move the MMC PLL structs and ops declarations out of clk-pll.h into a new clk-regmap-pll-mmc.h.
- Add a separate 'struct rtk_clk_pll_mmc_phase' with its own to_clk_pll_mmc_phase().
- Merge prepare/unprepare/is_prepared into enable/disable/is_enabled, as
suggested: there is no sleeping operation.
- Introduce the 'rtk_clk_pll_mmc_div_cfg' table for the SSCPLL analog tuning.
- Introduce the 'rtk_clk_pll_mmc_rate' table.
- determine_rate() selects the closest entry within [min_rate, max_rate]
- set_rate() programs that entry only
- recalc_rate() matches all five raw register fields against the table. However, only on a miss does it fall back to 'clk_pll_mmc_calc_rate()', which implements the formula.

---
 drivers/clk/realtek/Kconfig              |   3 +
 drivers/clk/realtek/Makefile             |   2 +
 drivers/clk/realtek/clk-regmap-pll-mmc.c | 457 +++++++++++++++++++++++
 drivers/clk/realtek/clk-regmap-pll-mmc.h |  66 ++++
 drivers/clk/realtek/freq_table.c         |   1 +
 5 files changed, 529 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-regmap-pll-mmc.c
 create mode 100644 drivers/clk/realtek/clk-regmap-pll-mmc.h
drivers/clk/realtek/Kconfig +3 -0
index e8deccf93f94..00ef9c5ba969 100644
@@ -26,4 +26,7 @@ config RTK_CLK_COMMON
 	  infrastructure to spawn reset controllers via the auxiliary bus
 	  where required.
 
+config RTK_CLK_PLL_MMC
+	bool
+
 endif
drivers/clk/realtek/Makefile +2 -0
index 4be39e2dd5b4..5fefe9bf901f 100644
@@ -6,3 +6,5 @@ clk-rtk-y += clk-regmap-gate.o
 clk-rtk-y += clk-regmap-mux.o
 clk-rtk-y += clk-regmap-pll.o
 clk-rtk-y += freq_table.o
+
+clk-rtk-$(CONFIG_RTK_CLK_PLL_MMC) += clk-regmap-pll-mmc.o
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +457 -0

457 lines not on this page · raw message

drivers/clk/realtek/clk-regmap-pll-mmc.h new file +66 -0

66 lines not on this page · raw message

drivers/clk/realtek/freq_table.c +1 -0
index 0f977a3fc902..d0e015131e57 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <linux/limits.h>
 #include <linux/types.h>
 #include "freq_table.h"
 
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH v16 6/9] clk: realtek: Add support for MMC-tuned PLL clocks

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] clk: realtek: misplaced linux/limits.h include in freq_table.c breaks bisect --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] clk: realtek: misplaced linux/limits.h include in freq_table.c breaks bisect
--

commit bb40f458c667a0d1463f9b5c0600dafb76a332e7
Author: Cheng-Yu Lee <[email protected]>

clk: realtek: Add support for MMC-tuned PLL clocks

This commit adds clock operations for Realtek MMC-tuned PLL clocks, supporting rate control, enable/disable, and phase get/set operations.

drivers/clk/realtek/freq_table.c line 5

Quoted 10 lines
drivers/clk/realtek/freq_table.c +1 -0
index 0f977a3fc9021..d0e015131e57a 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <linux/limits.h>
 #include <linux/types.h>
 #include "freq_table.h"

[Severity: Medium]
Should this #include <linux/limits.h> be folded into earlier commit "clk: realtek: Add support for phase locked loops (PLLs)" instead?

That earlier patch introduces ftbl_find_closest_by_rate() in drivers/clk/realtek/freq_table.c using ULONG_MAX without including linux/limits.h, so building at the intermediate commits in this series fails and breaks git bisect.

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

Re: [PATCH v16 6/9] clk: realtek: Add support for MMC-tuned PLL clocks

In reply to Yu-Chun Lin
Quoted 351 lines

Add clk_pll_mmc_ops for enable/disable, rate control, and status operations on MMC PLL clocks.

Also add clk_pll_mmc_phase_ops to support phase get/set operations.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Move the MMC PLL structs and ops declarations out of clk-pll.h into a new clk-regmap-pll-mmc.h.
- Add a separate 'struct rtk_clk_pll_mmc_phase' with its own to_clk_pll_mmc_phase().
- Merge prepare/unprepare/is_prepared into enable/disable/is_enabled, as
suggested: there is no sleeping operation.
- Introduce the 'rtk_clk_pll_mmc_div_cfg' table for the SSCPLL analog tuning.
- Introduce the 'rtk_clk_pll_mmc_rate' table.
- determine_rate() selects the closest entry within [min_rate, max_rate]
- set_rate() programs that entry only
- recalc_rate() matches all five raw register fields against the table. However, only on a miss does it fall back to 'clk_pll_mmc_calc_rate()', which implements the formula.

---
 drivers/clk/realtek/Kconfig              |   3 +
 drivers/clk/realtek/Makefile             |   2 +
 drivers/clk/realtek/clk-regmap-pll-mmc.c | 457 +++++++++++++++++++++++
 drivers/clk/realtek/clk-regmap-pll-mmc.h |  66 ++++
 drivers/clk/realtek/freq_table.c         |   1 +
 5 files changed, 529 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-regmap-pll-mmc.c
 create mode 100644 drivers/clk/realtek/clk-regmap-pll-mmc.h
drivers/clk/realtek/Kconfig +3 -0
index e8deccf93f94..00ef9c5ba969 100644
@@ -26,4 +26,7 @@ config RTK_CLK_COMMON
 	  infrastructure to spawn reset controllers via the auxiliary bus
 	  where required.
 
+config RTK_CLK_PLL_MMC
+	bool
+
 endif
drivers/clk/realtek/Makefile +2 -0
index 4be39e2dd5b4..5fefe9bf901f 100644
@@ -6,3 +6,5 @@ clk-rtk-y += clk-regmap-gate.o
 clk-rtk-y += clk-regmap-mux.o
 clk-rtk-y += clk-regmap-pll.o
 clk-rtk-y += freq_table.o
+
+clk-rtk-$(CONFIG_RTK_CLK_PLL_MMC) += clk-regmap-pll-mmc.o
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +288 -0
new file mode 100644
index 000000000000..50df27ef8539
@@ -0,0 +1,457 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021-2026 Realtek Semiconductor Corporation
+ * Author: Cheng-Yu Lee <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/delay.h>
+#include <linux/export.h>
+#include <linux/math64.h>
+#include <linux/regmap.h>
+#include "clk-regmap-pll-mmc.h"
+
+#define RTK_PLL_EMMC1_OFFSET            0x0
+#define RTK_PLL_EMMC2_OFFSET            0x4
+#define RTK_PLL_EMMC3_OFFSET            0x8
+#define RTK_PLL_EMMC4_OFFSET            0xc
+#define RTK_PLL_SSC_DIG_EMMC0_OFFSET    0x0
+#define RTK_PLL_SSC_DIG_EMMC3_OFFSET    0xc
+#define RTK_PLL_SSC_DIG_EMMC4_OFFSET    0x10
+
+#define RTK_PLL_PHRT0_MASK              BIT(0)
+#define RTK_PLL_PHSEL_MASK              GENMASK(4, 0)
+#define RTK_PLL_SSCPLL_RS_MASK          GENMASK(12, 10)
+#define RTK_PLL_SSCPLL_ICP_MASK         GENMASK(9, 5)
+#define RTK_PLL_SSC_DIV_EXT_F_MASK      GENMASK(25, 13)
+#define RTK_PLL_PI_IBSELH_MASK          GENMASK(28, 27)
+#define RTK_PLL_SSC_DIV_N_MASK          GENMASK(23, 16)
+#define RTK_PLL_NCODE_SSC_EMMC_MASK     GENMASK(20, 13)
+#define RTK_PLL_FCODE_SSC_EMMC_MASK     GENMASK(12, 0)
+#define RTK_PLL_GRAN_EST_EM_MC_MASK     GENMASK(20, 0)
+#define RTK_PLL_EN_SSC_EMMC_MASK        BIT(0)
+#define RTK_PLL_SSC_DIG_EMMC0_EN_SSC    BIT(1)
+#define RTK_PLL_FLAG_INITIAL_EMMC_MASK  BIT(8)
+
+#define RTK_PLL_PHRT0_SHIFT             1
+
+#define CYCLE_DEGREES                   360
+#define PHASE_STEPS                     32
+#define PHASE_SCALE_FACTOR              1125
+
+static inline struct rtk_clk_regmap_pll_mmc *to_clk_pll_mmc(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap *clkr = to_rtk_clk_regmap(hw);
+
+	return container_of(clkr, struct rtk_clk_regmap_pll_mmc, clkr);
+}
+
+static inline struct rtk_clk_pll_mmc_phase *to_clk_pll_mmc_phase(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap *clkr = to_rtk_clk_regmap(hw);
+
+	return container_of(clkr, struct rtk_clk_pll_mmc_phase, clkr);
+}
+
+static inline int get_phrt0(struct rtk_clk_regmap_pll_mmc *clkm, u32 *val)
+{
+	u32 reg;
+	int ret;
+
+	ret = regmap_read(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC1_OFFSET, &reg);
+	if (ret)
+		return ret;
+
+	*val = (reg >> RTK_PLL_PHRT0_SHIFT) & RTK_PLL_PHRT0_MASK;
+
+	return 0;
+}
+
+static inline int set_phrt0(struct rtk_clk_regmap_pll_mmc *clkm, u32 val)
+{
+	return regmap_update_bits(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC1_OFFSET,
+				  RTK_PLL_PHRT0_MASK << RTK_PLL_PHRT0_SHIFT,
+				  val << RTK_PLL_PHRT0_SHIFT);
+}
+
+static inline int clk_pll_emmc_set_sscpll_rs(struct rtk_clk_regmap_pll_mmc *clkm, u32 val)
+{
+	return regmap_update_bits(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC2_OFFSET,
+				  RTK_PLL_SSCPLL_RS_MASK, FIELD_PREP(RTK_PLL_SSCPLL_RS_MASK, val));
+}
+
+static inline int clk_pll_emmc_set_sscpll_icp(struct rtk_clk_regmap_pll_mmc *clkm, u32 val)
+{
+	return regmap_update_bits(clkm->clkr.regmap,
+				  clkm->pll_ofs + RTK_PLL_EMMC2_OFFSET,
+				  RTK_PLL_SSCPLL_ICP_MASK,
+				  FIELD_PREP(RTK_PLL_SSCPLL_ICP_MASK, val));
+}
+
+static inline int clk_pll_emmc_set_ssc_div_ext_f(struct rtk_clk_regmap_pll_mmc *clkm, u32 val)
+{
+	return regmap_update_bits(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC2_OFFSET,
+				  RTK_PLL_SSC_DIV_EXT_F_MASK,
+				  FIELD_PREP(RTK_PLL_SSC_DIV_EXT_F_MASK, val));
+}
+
+static inline int clk_pll_emmc_set_pi_ibselh(struct rtk_clk_regmap_pll_mmc *clkm, u32 val)
+{
+	return regmap_update_bits(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC2_OFFSET,
+				  RTK_PLL_PI_IBSELH_MASK, FIELD_PREP(RTK_PLL_PI_IBSELH_MASK, val));
+}
+
+static inline int clk_pll_emmc_set_ssc_div_n(struct rtk_clk_regmap_pll_mmc *clkm, u32 val)
+{
+	return regmap_update_bits(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC3_OFFSET,
+				  RTK_PLL_SSC_DIV_N_MASK, FIELD_PREP(RTK_PLL_SSC_DIV_N_MASK, val));
+}
+
+static inline int set_pow_ctl(struct rtk_clk_regmap_pll_mmc *clkm, u32 val)
+{
+	return regmap_write(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC4_OFFSET, val);
+}
+
+static inline int get_pow_ctl(struct rtk_clk_regmap_pll_mmc *clkm, u32 *val)
+{
+	u32 raw_val;
+	int ret;
+
+	ret = regmap_read(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC4_OFFSET, &raw_val);
+	if (ret)
+		return ret;
+
+	*val = raw_val;
+
+	return 0;
+}
+
+static int rtk_clk_regmap_pll_mmc_phase_set_phase(struct clk_hw *hw, int degrees)
+{
+	struct rtk_clk_pll_mmc_phase *clkm = to_clk_pll_mmc_phase(hw);
+	int ret;
+	u32 val;
+
+	val = DIV_ROUND_CLOSEST(degrees * 100, PHASE_SCALE_FACTOR);
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->pll_mmc->pll_ofs + RTK_PLL_EMMC1_OFFSET,
+				 RTK_PLL_PHSEL_MASK << clkm->shift, val << clkm->shift);
+	if (ret)
+		return ret;
+
+	usleep_range(10, 20);
+
+	return 0;
+}
+
+static int rtk_clk_regmap_pll_mmc_phase_get_phase(struct clk_hw *hw)
+{
+	struct rtk_clk_pll_mmc_phase *clkm = to_clk_pll_mmc_phase(hw);
+	unsigned int raw_val;
+	int ret;
+	u32 val;
+
+	ret = regmap_read(clkm->clkr.regmap, clkm->pll_mmc->pll_ofs + RTK_PLL_EMMC1_OFFSET,
+			  &raw_val);
+	if (ret)
+		return ret;
+
+	val = (raw_val >> clkm->shift) & RTK_PLL_PHSEL_MASK;
+
+	return DIV_ROUND_CLOSEST(val * CYCLE_DEGREES, PHASE_STEPS);
+}
+
+const struct clk_ops rtk_clk_pll_mmc_phase_ops = {
+	.set_phase = rtk_clk_regmap_pll_mmc_phase_set_phase,
+	.get_phase = rtk_clk_regmap_pll_mmc_phase_get_phase,
+};
+EXPORT_SYMBOL_NS_GPL(rtk_clk_pll_mmc_phase_ops, "CLK_REALTEK");
+
+static int rtk_clk_regmap_pll_mmc_enable(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+	int ret;
+
+	ret = set_pow_ctl(clkm, 7);
+	if (ret)
+		return ret;
+
+	ret = set_phrt0(clkm, 1);
+	if (ret)
+		return ret;
+
+	udelay(10);
+
+	return 0;
+}
+
+static void rtk_clk_regmap_pll_mmc_disable(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+
+	set_phrt0(clkm, 0);
+	udelay(10);
+	set_pow_ctl(clkm, 0);
+}
+
+static int rtk_clk_regmap_pll_mmc_is_enabled(struct clk_hw *hw)
+{
+	struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+	u32 pow_val, phrt_val;
+	int ret;
+
+	ret = get_pow_ctl(clkm, &pow_val);
+	if (ret)
+		return 1;
+
+	ret = get_phrt0(clkm, &phrt_val);
+	if (ret)
+		return 1;
+
+	return (pow_val != 0x0) && (phrt_val == 0x1);
+}
+
+static unsigned long clk_pll_mmc_calc_rate(unsigned long parent_rate,
+					   u32 div_n, u32 div_ext_f)
+{
+	u64 rate = (u64)parent_rate * (div_n + 2);
+
+	rate += (u64)parent_rate * div_ext_f / 8192;
+
+	return div64_u64(rate, 4);
+}
+
+static unsigned long rtk_clk_regmap_pll_mmc_recalc_rate(struct clk_hw *hw,
+							unsigned long parent_rate)
+{
+	struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+	u32 emmc2, emmc3, ssc3, ssc4, val, ext_f, ncode, fcode, gran_est;
+
+	if (regmap_read(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC2_OFFSET, &emmc2))
+		return 0;
+
+	if (regmap_read(clkm->clkr.regmap, clkm->pll_ofs + RTK_PLL_EMMC3_OFFSET, &emmc3))
+		return 0;
+
+	if (regmap_read(clkm->clkr.regmap, clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC3_OFFSET, &ssc3))
+		return 0;
+
+	if (regmap_read(clkm->clkr.regmap, clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC4_OFFSET, &ssc4))
+		return 0;
+
+	val = FIELD_GET(RTK_PLL_SSC_DIV_N_MASK, emmc3);
+	ext_f = FIELD_GET(RTK_PLL_SSC_DIV_EXT_F_MASK, emmc2);
+	ncode = FIELD_GET(RTK_PLL_NCODE_SSC_EMMC_MASK, ssc3);
+	fcode = FIELD_GET(RTK_PLL_FCODE_SSC_EMMC_MASK, ssc3);
+	gran_est = FIELD_GET(RTK_PLL_GRAN_EST_EM_MC_MASK, ssc4);
+
+	for (unsigned int i = 0; clkm->rate_tbl && i < clkm->num_rate_tbl; i++) {
+		const struct rtk_clk_pll_mmc_rate *entry = &clkm->rate_tbl[i];
+
+		if (entry->ssc_div_n == val && entry->ssc_div_ext_f == ext_f &&
+		    entry->ncode == ncode && entry->fcode == fcode &&
+		    entry->gran_est == gran_est)
+			return entry->rate;
+	}
+
+	return clk_pll_mmc_calc_rate(parent_rate, val, ext_f);
+}
+
+static const struct rtk_clk_pll_mmc_rate *
+rtk_clk_pll_mmc_find_rate(const struct rtk_clk_regmap_pll_mmc *clkm, unsigned long rate)
+{
+	if (!clkm->rate_tbl || !clkm->num_rate_tbl)
+		return NULL;
+
+	for (unsigned int i = 0; i < clkm->num_rate_tbl; i++) {
+		if (clkm->rate_tbl[i].rate == rate)
+			return &clkm->rate_tbl[i];
+	}
+
+	return NULL;
+}
+
+static const struct rtk_clk_pll_mmc_rate *
+rtk_clk_pll_mmc_find_closest_rate(const struct rtk_clk_pll_mmc_rate *tbl, u32 num,
+				  unsigned long rate, unsigned long min_rate,
+				  unsigned long max_rate)
+{
+	const struct rtk_clk_pll_mmc_rate *best = NULL;
+	unsigned long best_delta = ~0UL;
+
+	for (unsigned int i = 0; i < num; i++) {
+		const struct rtk_clk_pll_mmc_rate *entry = &tbl[i];
+		unsigned long delta;
+
+		if (entry->rate < min_rate || entry->rate > max_rate)
+			continue;

drivers/clk/realtek/clk-regmap-pll-mmc.c line 298

Quoted 10 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +10 -0
+
+		delta = entry->rate >= rate ? entry->rate - rate : rate - entry->rate;
+		if (delta < best_delta) {
+			best = entry;
+			best_delta = delta;
+		}
+	}
+
+	return best;
+}

This duplicates the functionality in ftbl_find_closest_by_rate() from patch 3.

drivers/clk/realtek/clk-regmap-pll-mmc.c line 307

Quoted 9 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +9 -0
+
+static int rtk_clk_regmap_pll_mmc_determine_rate(struct clk_hw *hw,
+						 struct clk_rate_request *req)
+{
+	struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+	const struct rtk_clk_pll_mmc_rate *entry;
+
+	if (!clkm->rate_tbl || !clkm->num_rate_tbl ||
+	    req->min_rate > req->max_rate)

clk_core_check_boundaries() already checks the min/max rate.

Quoted 15 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +15 -0
+		return -EINVAL;
+
+	entry = rtk_clk_pll_mmc_find_closest_rate(clkm->rate_tbl,
+						  clkm->num_rate_tbl, req->rate,
+						  req->min_rate, req->max_rate);
+	if (!entry)
+		return -EINVAL;
+
+	req->rate = entry->rate;
+
+	return 0;
+}
+
+static const struct rtk_clk_pll_mmc_div_cfg clk_pll_emmc_default_div_cfg[] = {
+	{ 31, 46, 3, 3, 2 },

drivers/clk/realtek/clk-regmap-pll-mmc.c line 332

Quoted 10 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +10 -0
+	{ 20, 30, 2, 3, 1 },
+	{ 10, 19, 1, 2, 1 },
+	{  5,  9, 0, 2, 0 },
+};
+
+static const struct rtk_clk_pll_mmc_div_cfg *
+clk_pll_emmc_find_div_cfg(struct rtk_clk_regmap_pll_mmc *clkm, u32 div)
+{
+	const struct rtk_clk_pll_mmc_div_cfg *tbl = clkm->div_cfg;
+	u8 num = clkm->num_div_cfg;

This is a u32 in struct rtk_clk_regmap_pll_mmc.

Quoted 91 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +91 -0
+
+	if (!tbl) {
+		tbl = clk_pll_emmc_default_div_cfg;
+		num = ARRAY_SIZE(clk_pll_emmc_default_div_cfg);
+	}
+
+	for (unsigned int i = 0; i < num; i++) {
+		if (div >= tbl[i].div_min && div <= tbl[i].div_max)
+			return &tbl[i];
+	}
+
+	return NULL;
+}
+
+static int clk_pll_emmc_program_div_cfg(struct rtk_clk_regmap_pll_mmc *clkm, u32 div)
+{
+	const struct rtk_clk_pll_mmc_div_cfg *t = clk_pll_emmc_find_div_cfg(clkm, div);
+	int ret;
+
+	if (!t)
+		return -EINVAL;
+
+	ret = clk_pll_emmc_set_pi_ibselh(clkm, t->ibselh);
+	if (ret)
+		return ret;
+
+	ret = clk_pll_emmc_set_sscpll_rs(clkm, t->rs);
+	if (ret)
+		return ret;
+
+	return clk_pll_emmc_set_sscpll_icp(clkm, t->icp);
+}
+
+static int rtk_clk_regmap_pll_mmc_set_rate(struct clk_hw *hw, unsigned long rate,
+					   unsigned long parent_rate)
+{
+	struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+	const struct rtk_clk_pll_mmc_rate *entry = rtk_clk_pll_mmc_find_rate(clkm, rate);
+	int ret;
+
+	if (!entry)
+		return -EINVAL;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC0_OFFSET,
+				 RTK_PLL_FLAG_INITIAL_EMMC_MASK,
+				 FIELD_PREP(RTK_PLL_FLAG_INITIAL_EMMC_MASK, 0));
+	if (ret)
+		return ret;
+
+	ret = clk_pll_emmc_set_ssc_div_n(clkm, entry->ssc_div_n);
+	if (ret)
+		return ret;
+
+	ret = clk_pll_emmc_set_ssc_div_ext_f(clkm, entry->ssc_div_ext_f);
+	if (ret)
+		return ret;
+
+	ret = clk_pll_emmc_program_div_cfg(clkm, entry->ssc_div_n);
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC3_OFFSET,
+				 RTK_PLL_NCODE_SSC_EMMC_MASK,
+				 FIELD_PREP(RTK_PLL_NCODE_SSC_EMMC_MASK, entry->ncode));
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC3_OFFSET,
+				 RTK_PLL_FCODE_SSC_EMMC_MASK,
+				 FIELD_PREP(RTK_PLL_FCODE_SSC_EMMC_MASK, entry->fcode));
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC4_OFFSET,
+				 RTK_PLL_GRAN_EST_EM_MC_MASK,
+				 FIELD_PREP(RTK_PLL_GRAN_EST_EM_MC_MASK, entry->gran_est));
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC0_OFFSET,
+				 RTK_PLL_SSC_DIG_EMMC0_EN_SSC,
+				 FIELD_PREP(RTK_PLL_SSC_DIG_EMMC0_EN_SSC, 1));
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,

drivers/clk/realtek/clk-regmap-pll-mmc.c line 433

Quoted 10 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +10 -0
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC0_OFFSET,
+				 RTK_PLL_EN_SSC_EMMC_MASK,
+				 FIELD_PREP(RTK_PLL_EN_SSC_EMMC_MASK, 1));
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC0_OFFSET,
+				 RTK_PLL_EN_SSC_EMMC_MASK,
+				 FIELD_PREP(RTK_PLL_EN_SSC_EMMC_MASK, 0));

EN_SSC_EMMC is set and then immediately cleared. If this is intentional then put a comment here to be sure it's intentional.

Quoted 57 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +24 -0
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(clkm->clkr.regmap,
+				 clkm->ssc_dig_ofs + RTK_PLL_SSC_DIG_EMMC0_OFFSET,
+				 RTK_PLL_FLAG_INITIAL_EMMC_MASK,
+				 FIELD_PREP(RTK_PLL_FLAG_INITIAL_EMMC_MASK, 1));
+	if (ret)
+		return ret;
+
+	usleep_range(10, 20);
+
+	return 0;
+}
+
+const struct clk_ops rtk_clk_pll_mmc_ops = {
+	.enable           = rtk_clk_regmap_pll_mmc_enable,
+	.disable          = rtk_clk_regmap_pll_mmc_disable,
+	.is_enabled       = rtk_clk_regmap_pll_mmc_is_enabled,
+	.recalc_rate      = rtk_clk_regmap_pll_mmc_recalc_rate,
+	.determine_rate   = rtk_clk_regmap_pll_mmc_determine_rate,
+	.set_rate         = rtk_clk_regmap_pll_mmc_set_rate,
+};
+EXPORT_SYMBOL_NS_GPL(rtk_clk_pll_mmc_ops, "CLK_REALTEK");
drivers/clk/realtek/clk-regmap-pll-mmc.h new file +27 -0
new file mode 100644
index 000000000000..c2e973f3df72
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 Realtek Semiconductor Corporation
+ */
+
+#ifndef __CLK_REALTEK_CLK_PLL_MMC_H
+#define __CLK_REALTEK_CLK_PLL_MMC_H
+
+#include "clk-rtk-common.h"
+
+/* SSCPLL charge-pump settings per divider range */
+struct rtk_clk_pll_mmc_div_cfg {
+	u16 div_min;
+	u16 div_max;
+	u8 ibselh;
+	u8 rs;
+	u8 icp;
+};
+
+/* Register settings for a specific eMMC PLL rate */
+struct rtk_clk_pll_mmc_rate {
+	unsigned long rate;
+	u32 ssc_div_n;
+	u32 ssc_div_ext_f;
+	u32 ncode;
+	u32 fcode;
+	u32 gran_est;

drivers/clk/realtek/clk-regmap-pll-mmc.h line 37

Quoted 10 lines
drivers/clk/realtek/clk-regmap-pll-mmc.h new file +10 -0
+};
+
+struct rtk_clk_regmap_pll_mmc {
+	struct rtk_clk_regmap clkr;
+	unsigned int pll_ofs;
+	unsigned int ssc_dig_ofs;
+
+	/*
+	 * Optional per-SoC tuning table. When NULL, the driver's default table
+	 * is used and num_div_cfg is ignored;

Replace ; at end with .

Brian

Signature · 1 line
Brian Masney <[email protected]>
author maintainer
#14 lore raw

RE: [PATCH v16 6/9] clk: realtek: Add support for MMC-tuned PLL clocks

Hi Brian,

Quoted 19 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 3 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +3 -0
+
+static const struct rtk_clk_pll_mmc_rate *
+rtk_clk_pll_mmc_find_closest_rate(const struct rtk_clk_pll_mmc_rate *tbl,

u32 num,

Quoted 1 line

+ unsigned long rate, unsigned long

min_rate,

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 12 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +12 -0
+                               unsigned long max_rate) {
+     const struct rtk_clk_pll_mmc_rate *best = NULL;
+     unsigned long best_delta = ~0UL;
+
+     for (unsigned int i = 0; i < num; i++) {
+             const struct rtk_clk_pll_mmc_rate *entry = &tbl[i];
+             unsigned long delta;
+
+             if (entry->rate < min_rate || entry->rate > max_rate)
+                     continue;
+
+             delta = entry->rate >= rate ? entry->rate - rate : rate -

entry->rate;

Quoted 11 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c line 298

Quoted 8 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +8 -0
+             if (delta < best_delta) {
+                     best = entry;
+                     best_delta = delta;
+             }
+     }
+
+     return best;
+}

This duplicates the functionality in ftbl_find_closest_by_rate() from patch 3.

I considered sharing the logic, but the two tables have different entry layouts.

Unifying them would need a macro or (void *) (both lose type checking), or embedding rtk_freq_table with container_of() (a larger rework for little gain). I prefer keeping the two type-safe functions. Is that acceptable?

Quoted 4 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 3 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +3 -0
+
+static int rtk_clk_regmap_pll_mmc_determine_rate(struct clk_hw *hw,
+                                              struct

clk_rate_request

Quoted 9 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c line 307

Quoted 6 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +6 -0
+*req) {
+     struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+     const struct rtk_clk_pll_mmc_rate *entry;
+
+     if (!clkm->rate_tbl || !clkm->num_rate_tbl ||
+         req->min_rate > req->max_rate)

clk_core_check_boundaries() already checks the min/max rate.

Ack. Will drop.

Quoted 17 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c line 315

Quoted 4 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +4 -0
+             return -EINVAL;
+
+     entry = rtk_clk_pll_mmc_find_closest_rate(clkm->rate_tbl,
+

clkm->num_rate_tbl, req->rate,

Quoted 1 line

+ req->min_rate,

req->max_rate);

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 9 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +9 -0
+     if (!entry)
+             return -EINVAL;
+
+     req->rate = entry->rate;
+
+     return 0;
+}
+
+static const struct rtk_clk_pll_mmc_div_cfg clk_pll_emmc_default_div_cfg[]

= {

Quoted 13 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c line 332

Quoted 11 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +11 -0
+     { 31, 46, 3, 3, 2 },
+     { 20, 30, 2, 3, 1 },
+     { 10, 19, 1, 2, 1 },
+     {  5,  9, 0, 2, 0 },
+};
+
+static const struct rtk_clk_pll_mmc_div_cfg *
+clk_pll_emmc_find_div_cfg(struct rtk_clk_regmap_pll_mmc *clkm, u32
+div) {
+     const struct rtk_clk_pll_mmc_div_cfg *tbl = clkm->div_cfg;
+     u8 num = clkm->num_div_cfg;

This is a u32 in struct rtk_clk_regmap_pll_mmc.

Yes, will fix.

[...]

Quoted 80 lines
Quoted 1 line

+static int rtk_clk_regmap_pll_mmc_set_rate(struct clk_hw *hw, unsigned

long rate,

Quoted 1 line

+ unsigned long parent_rate)

{

Review

Quoted 2 lines
(quoted diff) +2 -0
+     struct rtk_clk_regmap_pll_mmc *clkm = to_clk_pll_mmc(hw);
+     const struct rtk_clk_pll_mmc_rate *entry =

rtk_clk_pll_mmc_find_rate(clkm, rate);

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 7 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +7 -0
+     int ret;
+
+     if (!entry)
+             return -EINVAL;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +

RTK_PLL_SSC_DIG_EMMC0_OFFSET,

Review

Quoted 2 lines
(quoted diff) +2 -0
+                              RTK_PLL_FLAG_INITIAL_EMMC_MASK,
+

FIELD_PREP(RTK_PLL_FLAG_INITIAL_EMMC_MASK, 0));

Quoted 7 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +7 -0
+     if (ret)
+             return ret;
+
+     ret = clk_pll_emmc_set_ssc_div_n(clkm, entry->ssc_div_n);
+     if (ret)
+             return ret;
+

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 10 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +10 -0
+     ret = clk_pll_emmc_set_ssc_div_ext_f(clkm, entry->ssc_div_ext_f);
+     if (ret)
+             return ret;
+
+     ret = clk_pll_emmc_program_div_cfg(clkm, entry->ssc_div_n);
+     if (ret)
+             return ret;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +

RTK_PLL_SSC_DIG_EMMC3_OFFSET,

drivers/clk/realtek/clk-regmap-pll-mmc.c line 401

Quoted 2 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +2 -0
+                              RTK_PLL_NCODE_SSC_EMMC_MASK,
+

FIELD_PREP(RTK_PLL_NCODE_SSC_EMMC_MASK, entry->ncode));

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 5 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +5 -0
+     if (ret)
+             return ret;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +

RTK_PLL_SSC_DIG_EMMC3_OFFSET,

drivers/clk/realtek/clk-regmap-pll-mmc.c line 408

Quoted 2 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +2 -0
+                              RTK_PLL_FCODE_SSC_EMMC_MASK,
+

FIELD_PREP(RTK_PLL_FCODE_SSC_EMMC_MASK, entry->fcode));

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 5 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +5 -0
+     if (ret)
+             return ret;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +

RTK_PLL_SSC_DIG_EMMC4_OFFSET,

drivers/clk/realtek/clk-regmap-pll-mmc.c line 415

Quoted 2 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +2 -0
+                              RTK_PLL_GRAN_EST_EM_MC_MASK,
+

FIELD_PREP(RTK_PLL_GRAN_EST_EM_MC_MASK, entry->gran_est));

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 5 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +5 -0
+     if (ret)
+             return ret;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +

RTK_PLL_SSC_DIG_EMMC0_OFFSET,

drivers/clk/realtek/clk-regmap-pll-mmc.c line 422

Quoted 2 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +2 -0
+                              RTK_PLL_SSC_DIG_EMMC0_EN_SSC,
+

FIELD_PREP(RTK_PLL_SSC_DIG_EMMC0_EN_SSC, 1));

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 5 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +5 -0
+     if (ret)
+             return ret;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +

RTK_PLL_SSC_DIG_EMMC0_OFFSET,

Review

Quoted 2 lines
(quoted diff) +2 -0
+                              RTK_PLL_EN_SSC_EMMC_MASK,
+

FIELD_PREP(RTK_PLL_EN_SSC_EMMC_MASK, 1));

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 5 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +5 -0
+     if (ret)
+             return ret;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +
Quoted 9 lines

RTK_PLL_SSC_DIG_EMMC0_OFFSET,

Review

Quoted 2 lines
(quoted diff) +2 -0
+                              RTK_PLL_EN_SSC_EMMC_MASK,
+

FIELD_PREP(RTK_PLL_EN_SSC_EMMC_MASK,

Quoted 1 line

+ 0));

EN_SSC_EMMC is set and then immediately cleared. If this is intentional then put a comment here to be sure it's intentional.

Sorry for the confusion, the macro name was wrong:
RTK_PLL_EN_SSC_EMMC_MASK should be RTK_PLL_SSC_DIG_EMMC0_OC_EN.

OC_EN is a trigger bit (enables over-clock or under-clock adjustment), not the SSC enable. It needs to be set and then cleared to trigger the adjustment.

Quoted 17 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 5 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +5 -0
+     if (ret)
+             return ret;
+
+     ret = regmap_update_bits(clkm->clkr.regmap,
+                              clkm->ssc_dig_ofs +

RTK_PLL_SSC_DIG_EMMC0_OFFSET,

Review

Quoted 2 lines
(quoted diff) +2 -0
+                              RTK_PLL_FLAG_INITIAL_EMMC_MASK,
+

FIELD_PREP(RTK_PLL_FLAG_INITIAL_EMMC_MASK, 1));

Quoted 8 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +8 -0
+     if (ret)
+             return ret;
+
+     usleep_range(10, 20);
+
+     return 0;
+}
+

[...]

Quoted 9 lines

drivers/clk/realtek/clk-regmap-pll-mmc.h

Quoted 8 lines
drivers/clk/realtek/clk-regmap-pll-mmc.h new file +8 -0
+
+struct rtk_clk_regmap_pll_mmc {
+     struct rtk_clk_regmap clkr;
+     unsigned int pll_ofs;
+     unsigned int ssc_dig_ofs;
+
+     /*
+      * Optional per-SoC tuning table. When NULL, the driver's default

table

Quoted 3 lines
Quoted 1 line

+ * is used and num_div_cfg is ignored;

Replace ; at end with .

Ack.

Best Regards,
Yu-Chun

#15 lore raw

Re: [PATCH v16 6/9] clk: realtek: Add support for MMC-tuned PLL clocks

Hi Yu-Chun,

Quoted 30 lines · Yu-Chun Lin [林祐君]
Quoted 18 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static const struct rtk_clk_pll_mmc_rate *
+rtk_clk_pll_mmc_find_closest_rate(const struct rtk_clk_pll_mmc_rate *tbl,

u32 num,

Quoted 1 line

+ unsigned long rate, unsigned long

min_rate,

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 12 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +12 -0
+                               unsigned long max_rate) {
+     const struct rtk_clk_pll_mmc_rate *best = NULL;
+     unsigned long best_delta = ~0UL;
+
+     for (unsigned int i = 0; i < num; i++) {
+             const struct rtk_clk_pll_mmc_rate *entry = &tbl[i];
+             unsigned long delta;
+
+             if (entry->rate < min_rate || entry->rate > max_rate)
+                     continue;
+
+             delta = entry->rate >= rate ? entry->rate - rate : rate -

entry->rate;

Quoted 11 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c line 298

Quoted 8 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +8 -0
+             if (delta < best_delta) {
+                     best = entry;
+                     best_delta = delta;
+             }
+     }
+
+     return best;
+}

This duplicates the functionality in ftbl_find_closest_by_rate() from patch 3.

Quoted 6 lines

I considered sharing the logic, but the two tables have different entry layouts.

Unifying them would need a macro or (void *) (both lose type checking), or embedding rtk_freq_table with container_of() (a larger rework for little gain). I prefer keeping the two type-safe functions. Is that acceptable?

What about this? This is completely untested.

  #define ftbl_find_closest_by_rate(_tbl, _rate, _min, _max) ({         \
        typeof(&(_tbl)[0]) __best = NULL, __e;                          \
        unsigned long __r = (_rate), __lo = (_min), __hi = (_max);      \
        unsigned long __best_delta = ULONG_MAX;                         \
                                                                        \
        for (__e = (_tbl); __e->rate; __e++) {                          \
                unsigned long __d;                                      \
                                                                        \
                if (__e->rate < __lo || __e->rate > __hi)               \
                        continue;                                       \
                                                                        \
                __d = abs_diff(__e->rate, __r);                         \
                if (__d < __best_delta) {                               \
                        __best_delta = __d;                             \
                        __best = __e;                                   \
                }                                                       \
        }                                                               \
        __best;                                                         \ 

})

Brian

author maintainer
#16 lore raw

RE: [PATCH v16 6/9] clk: realtek: Add support for MMC-tuned PLL clocks

Hi Brian,

Quoted 78 lines

Hi Yu-Chun,

Quoted 20 lines · Yu-Chun Lin [林祐君]
Quoted 20 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+static const struct rtk_clk_pll_mmc_rate *
+rtk_clk_pll_mmc_find_closest_rate(const struct
+rtk_clk_pll_mmc_rate *tbl,

u32 num,

Quoted 1 line

+ unsigned long rate, unsigned long

min_rate,

drivers/clk/realtek/clk-regmap-pll-mmc.c

Quoted 13 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +13 -0
+                               unsigned long max_rate) {
+     const struct rtk_clk_pll_mmc_rate *best = NULL;
+     unsigned long best_delta = ~0UL;
+
+     for (unsigned int i = 0; i < num; i++) {
+             const struct rtk_clk_pll_mmc_rate *entry = &tbl[i];
+             unsigned long delta;
+
+             if (entry->rate < min_rate || entry->rate > max_rate)
+                     continue;
+
+             delta = entry->rate >= rate ? entry->rate - rate :
+ rate -

entry->rate;

Quoted 10 lines
Quoted 10 lines

drivers/clk/realtek/clk-regmap-pll-mmc.c line 298

Quoted 8 lines
drivers/clk/realtek/clk-regmap-pll-mmc.c new file +8 -0
+             if (delta < best_delta) {
+                     best = entry;
+                     best_delta = delta;
+             }
+     }
+
+     return best;
+}

This duplicates the functionality in ftbl_find_closest_by_rate() from patch

3.

Quoted 8 lines
Quoted 1 line

I considered sharing the logic, but the two tables have different entry layouts.

Unifying them would need a macro or (void *) (both lose type checking), or embedding rtk_freq_table with container_of() (a larger rework for little gain). I prefer keeping the two type-safe functions. Is that

acceptable?

What about this? This is completely untested.

  #define ftbl_find_closest_by_rate(_tbl, _rate, _min, _max) ({         \
        typeof(&(_tbl)[0]) __best = NULL, __e;
\
        unsigned long __r = (_rate), __lo = (_min), __hi = (_max);      \
        unsigned long __best_delta = ULONG_MAX;

\

\
        for (__e = (_tbl); __e->rate; __e++)
{                          \
                unsigned long __d;

\

\
                if (__e->rate < __lo || __e->rate > __hi)               \
                        continue;

\

\
                __d = abs_diff(__e->rate, __r);
\
                if (__d < __best_delta)
{                               \
                        __best_delta = __d;
\
                        __best = __e;
\
                }
\
        }
\
        __best;
Quoted 3 lines

\
})

Thanks! I'll use this approach and address your other comments on this series as well.

Best Regards,
Yu-Chun

author maintainer
#17 lore raw

PATCH v16 7/9 clk: realtek: Add RTD1625-CRT clock controller driver

In reply to Yu-Chun Lin

From: Cheng-Yu Lee <[email protected]>

Add support for the CRT (Clock, Reset, and Test) domain clock controller on the Realtek RTD1625 SoC. This driver provides essential clock sources (including PLLs), gating, and multiplexing functionalities for the platform's peripherals.

Because the reset controller shares the same register space with this CRT clock controller, this driver also acts as the parent device and registers the reset controller as an auxiliary device on the auxiliary bus.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Replace 'const char *' parent arrays with 'struct clk_hw *' arrays.
- Switch all gate, mux, and fixed-factor clock initializations to use '_HW' macros.
- Use 'CLK_HW_INIT_FW_NAME' for regmap_pll initialization when referencing external clocks, and add 'expected_parent_rate' to ensure a safe input rate.
- Refactor eMMC PLL initialization (decoupling phases and appending rate tables).

---
 drivers/clk/realtek/Kconfig           |  14 +
 drivers/clk/realtek/Makefile          |   1 +
 drivers/clk/realtek/clk-rtd1625-crt.c | 878 ++++++++++++++++++++++++++
 3 files changed, 893 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-rtd1625-crt.c
drivers/clk/realtek/Kconfig +14 -0
index 00ef9c5ba969..043a6876a388 100644
@@ -29,4 +29,18 @@ config RTK_CLK_COMMON
 config RTK_CLK_PLL_MMC
 	bool
 
+config CLK_RTD1625
+	tristate "RTD1625 Clock Controller"
+	select RTK_CLK_COMMON
+	select RTK_CLK_PLL_MMC
+	help
+	  Support for the clock controller on Realtek RTD1625 SoCs.
+
+	  This driver provides clock sources, gating, multiplexing, and
+	  (via an auxiliary device) reset control for peripherals on the
+	  RTD1625 platform.
+
+	  Say Y here if your system is based on the RTD1625 and you need
+	  its peripheral devices to function.
+
 endif
drivers/clk/realtek/Makefile +1 -0
index 5fefe9bf901f..dd4c667e9808 100644
@@ -8,3 +8,4 @@ clk-rtk-y += clk-regmap-pll.o
 clk-rtk-y += freq_table.o
 
 clk-rtk-$(CONFIG_RTK_CLK_PLL_MMC) += clk-regmap-pll-mmc.o
+obj-$(CONFIG_CLK_RTD1625) += clk-rtd1625-crt.o
drivers/clk/realtek/clk-rtd1625-crt.c new file +878 -0

878 lines not on this page · raw message

Signature · 1 line
2.43.0
#18 lore raw

Re: [PATCH v16 7/9] clk: realtek: Add RTD1625-CRT clock controller driver

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] clk: realtek: FREQ_MNO_MASK omits bit 19 of O divider for pll_vodma - [Medium] clk: realtek…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] clk: realtek: FREQ_MNO_MASK omits bit 19 of O divider for pll_vodma
- [Medium] clk: realtek: static clk_hw.init cleared without suppress_bind_attrs
--

commit 127e3b9f9c4378fa7aaceda027d1d3b761330569
Author: Cheng-Yu Lee <[email protected]>

clk: realtek: Add RTD1625-CRT clock controller driver

This commit adds the CRT domain clock controller driver for the Realtek RTD1625 SoC and registers the associated reset controller on the auxiliary bus.

Quoted 6 lines
drivers/clk/realtek/clk-rtd1625-crt.c new file +0 -0
new file mode 100644
index 0000000000000..b0cccf5c7af2d
@@ -0,0 +1,878 @@

[ ... ]

drivers/clk/realtek/clk-rtd1625-crt.c line 78

Quoted 4 lines
drivers/clk/realtek/clk-rtd1625-crt.c new file +4 -0
+/* PLL VODMA: M (11:4) / N (13:12) / O (19:17) */
+#define FREQ_MNO_MASK            0x63ff0
+#define FREQ_MNO(_r, _m, _n, _o) { .rate = (_r), .val = ((_m) << 4) | \
+				 ((_n) << 12) | ((_o) << 17), }

[Severity: Medium]
Should FREQ_MNO_MASK be 0xe3ff0 instead of 0x63ff0? 0x63ff0 only covers bits 18:17 for the O divider field, so rtk_clk_regmap_pll_recalc_rate() masks off bit 19 when reading RTD1625_REG_PLL_VODMA1 for pll_vodma and can match the wrong entry in pll_vodma_tbl if O >= 4.

[ ... ]

drivers/clk/realtek/clk-rtd1625-crt.c line 871

Quoted 7 lines
drivers/clk/realtek/clk-rtd1625-crt.c new file +7 -0
+static struct platform_driver rtd1625_crt_driver = {
+	.probe  = rtd1625_crt_probe,
+	.driver = {
+		.name = "rtk-rtd1625-crt-clk",
+		.of_match_table = rtd1625_crt_match,
+	},
+};

[Severity: Medium]
Does rtd1625_crt_driver need .suppress_bind_attrs = true? During probe, rtk_clk_probe() registers the static clk_hw structures in rtd1625_crt_hw_data via devm_clk_hw_register() -> __clk_register(), which clears hw->init to NULL. Unbinding and rebinding the driver via sysfs (or re-probing after a late failure in rtk_clk_probe()) will pass those same static clk_hw structures to __clk_register() with hw->init == NULL and hit a NULL pointer dereference. Unbinding also unregisters CLK_IS_CRITICAL clocks while they remain prepared and enabled, triggering WARN_ON() in clk_unregister().

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

Re: [PATCH v16 7/9] clk: realtek: Add RTD1625-CRT clock controller driver

In reply to Yu-Chun Lin
Quoted 35 lines

Add support for the CRT (Clock, Reset, and Test) domain clock controller on the Realtek RTD1625 SoC. This driver provides essential clock sources (including PLLs), gating, and multiplexing functionalities for the platform's peripherals.

Because the reset controller shares the same register space with this CRT clock controller, this driver also acts as the parent device and registers the reset controller as an auxiliary device on the auxiliary bus.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Replace 'const char *' parent arrays with 'struct clk_hw *' arrays.
- Switch all gate, mux, and fixed-factor clock initializations to use '_HW' macros.
- Use 'CLK_HW_INIT_FW_NAME' for regmap_pll initialization when referencing external clocks, and add 'expected_parent_rate' to ensure a safe input rate.
- Refactor eMMC PLL initialization (decoupling phases and appending rate tables).
---

drivers/clk/realtek/clk-rtd1625-crt.c new file +13 -0
+static int rtd1625_crt_probe(struct platform_device *pdev)
+{
+	const struct rtk_clk_desc *desc;
+
+	desc = device_get_match_data(&pdev->dev);
+	if (!desc)
+		return -EINVAL;
+
+	return rtk_clk_probe(pdev, desc);
+}
+
+static const struct of_device_id rtd1625_crt_match[] = {
+	{ .compatible = "realtek,rtd1625-crt-clk", .data = &rtd1625_crt_desc, },

drivers/clk/realtek/clk-rtd1625-crt.c line 870

Quoted 10 lines
drivers/clk/realtek/clk-rtd1625-crt.c new file +10 -0
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, rtd1625_crt_match);
+
+static struct platform_driver rtd1625_crt_driver = {
+	.probe  = rtd1625_crt_probe,
+	.driver = {
+		.name = "rtk-rtd1625-crt-clk",
+		.of_match_table = rtd1625_crt_match,
+	},

Should .suppress_bind_attrs = true similar to what you have in patch 8?

Brian

Signature · 1 line
Brian Masney <[email protected]>
author maintainer
#20 lore raw

PATCH v16 8/9 clk: realtek: Add RTD1625-ISO clock controller driver

In reply to Yu-Chun Lin

From: Cheng-Yu Lee <[email protected]>

Add support for the ISO (Isolation) domain clock controller on the Realtek RTD1625 SoC. This controller manages clocks in the always-on power domain, ensuring essential services remain functional even when the main system power is gated.

Because the reset controller shares the same register space with this ISO clock controller, this driver also acts as the parent device and registers the reset controller as an auxiliary device on the auxiliary bus.

---
Changes in v16:
- Remove Brian's reviewed-by tag.
- Switch all gate clock initializations to use '_FW_NAME' macros.

---
 drivers/clk/realtek/Makefile          |   1 +
 drivers/clk/realtek/clk-rtd1625-iso.c | 140 ++++++++++++++++++++++++++
 2 files changed, 141 insertions(+)
 create mode 100644 drivers/clk/realtek/clk-rtd1625-iso.c
drivers/clk/realtek/Makefile +1 -0
index dd4c667e9808..04990a44c88a 100644
@@ -9,3 +9,4 @@ clk-rtk-y += freq_table.o
 
 clk-rtk-$(CONFIG_RTK_CLK_PLL_MMC) += clk-regmap-pll-mmc.o
 obj-$(CONFIG_CLK_RTD1625) += clk-rtd1625-crt.o
+obj-$(CONFIG_CLK_RTD1625) += clk-rtd1625-iso.o
drivers/clk/realtek/clk-rtd1625-iso.c new file +140 -0

140 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#21 lore raw

PATCH v16 9/9 arm64: dts: realtek: Add clock support for RTD1625

In reply to Yu-Chun Lin

Add the clock controller nodes and osc27m fixed clock for the Realtek RTD1625 SoC.

---
Changes in v16:
- Add 'clock-names = "osc27m"' to the 'cc' and 'iso_s_cc' clock controller nodes.
- Explicitly wire up internal parent clocks from the 'cc' controller to the 'ic' clock controller, along with their corresponding 'clock-names'.

---
 arch/arm64/boot/dts/realtek/kent.dtsi | 41 +++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
arch/arm64/boot/dts/realtek/kent.dtsi +41 -0
index df0d5ee3b1b9..fdb2bc2b3c79 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2024 Realtek Semiconductor Corp.
  */
 
+#include <dt-bindings/clock/realtek,rtd1625-clk.h>
 #include <dt-bindings/interrupt-controller/arm-gic.h>
 #include <dt-bindings/interrupt-controller/irq.h>
 
@@ -26,6 +27,15 @@ timer {
 			     <GIC_PPI 12 IRQ_TYPE_LEVEL_HIGH>;
 	};
 
+	clocks {
+		osc27m: osc {
+			compatible = "fixed-clock";
+			clock-frequency = <27000000>;
+			clock-output-names = "osc27m";
+			#clock-cells = <0>;
+		};
+	};
+
 	cpus {
 		#address-cells = <1>;
 		#size-cells = <0>;
@@ -141,6 +151,28 @@ rbus: bus@98000000 {
 			#address-cells = <1>;
 			#size-cells = <1>;
 
+			cc: clock-controller@0 {
+				compatible = "realtek,rtd1625-crt-clk";
+				reg = <0x0 0x900>;
+				clocks = <&osc27m>;
+				clock-names = "osc27m";
+				#clock-cells = <1>;
+				#reset-cells = <1>;
+			};
+
+			ic: clock-controller@7088 {
+				compatible = "realtek,rtd1625-iso-clk";
+				reg = <0x7088 0x8>;
+				clocks = <&osc27m>, <&cc RTD1625_CRT_CLK_SYS>,
+					 <&cc RTD1625_CRT_CLK_SYSH>,
+					 <&cc RTD1625_CRT_CLK_DDSA_DIV4>,
+					 <&cc RTD1625_CRT_CLK_EN_MISC>;
+				clock-names = "osc27m", "clk_sys", "clk_sysh", "clk_ddsa_div4",
+					      "clk_en_misc";
+				#clock-cells = <1>;
+				#reset-cells = <1>;
+			};
+
 			uart0: serial@7800 {
 				compatible = "snps,dw-apb-uart";
 				reg = <0x7800 0x100>;
@@ -205,6 +237,15 @@ isom_pinctrl: pinctrl@146200 {
 				reg = <0x146200 0x34>;
 			};
 
+			iso_s_cc: clock-controller@146310 {
+				compatible = "realtek,rtd1625-iso-s-clk";
+				reg = <0x146310 0x8>;
+				clocks = <&osc27m>;
+				clock-names = "osc27m";
+				#clock-cells = <1>;
+				#reset-cells = <1>;
+			};
+
 			ve4_pinctrl: pinctrl@14e000 {
 				compatible = "realtek,rtd1625-ve4-pinctrl";
 				reg = <0x14e000 0x84>;
Signature · 1 line
2.43.0