1 // SPDX-License-Identifier: GPL-2.0
3 * StarFive JH7110 Video-Output Clock Driver
5 * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
9 #include <linux/clk-provider.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/reset.h>
15 #include <dt-bindings/clock/starfive,jh7110-crg.h>
17 #include "clk-starfive-jh7110.h"
20 #define JH7110_VOUTCLK_VOUT_SRC (JH7110_VOUTCLK_END + 0)
21 #define JH7110_VOUTCLK_VOUT_TOP_AHB (JH7110_VOUTCLK_END + 1)
22 #define JH7110_VOUTCLK_VOUT_TOP_AXI (JH7110_VOUTCLK_END + 2)
23 #define JH7110_VOUTCLK_VOUT_TOP_HDMITX0_MCLK (JH7110_VOUTCLK_END + 3)
24 #define JH7110_VOUTCLK_I2STX0_BCLK (JH7110_VOUTCLK_END + 4)
25 #define JH7110_VOUTCLK_HDMITX0_PIXELCLK (JH7110_VOUTCLK_END + 5)
26 #define JH7110_VOUTCLK_EXT_END (JH7110_VOUTCLK_END + 6)
28 static struct clk_bulk_data jh7110_vout_top_clks
[] = {
30 { .id
= "vout_top_ahb" }
33 static const struct jh71x0_clk_data jh7110_voutclk_data
[] = {
35 JH71X0__DIV(JH7110_VOUTCLK_APB
, "apb", 8, JH7110_VOUTCLK_VOUT_TOP_AHB
),
36 JH71X0__DIV(JH7110_VOUTCLK_DC8200_PIX
, "dc8200_pix", 63, JH7110_VOUTCLK_VOUT_SRC
),
37 JH71X0__DIV(JH7110_VOUTCLK_DSI_SYS
, "dsi_sys", 31, JH7110_VOUTCLK_VOUT_SRC
),
38 JH71X0__DIV(JH7110_VOUTCLK_TX_ESC
, "tx_esc", 31, JH7110_VOUTCLK_VOUT_TOP_AHB
),
40 JH71X0_GATE(JH7110_VOUTCLK_DC8200_AXI
, "dc8200_axi", 0, JH7110_VOUTCLK_VOUT_TOP_AXI
),
41 JH71X0_GATE(JH7110_VOUTCLK_DC8200_CORE
, "dc8200_core", 0, JH7110_VOUTCLK_VOUT_TOP_AXI
),
42 JH71X0_GATE(JH7110_VOUTCLK_DC8200_AHB
, "dc8200_ahb", 0, JH7110_VOUTCLK_VOUT_TOP_AHB
),
43 JH71X0_GMUX(JH7110_VOUTCLK_DC8200_PIX0
, "dc8200_pix0", 0, 2,
44 JH7110_VOUTCLK_DC8200_PIX
,
45 JH7110_VOUTCLK_HDMITX0_PIXELCLK
),
46 JH71X0_GMUX(JH7110_VOUTCLK_DC8200_PIX1
, "dc8200_pix1", 0, 2,
47 JH7110_VOUTCLK_DC8200_PIX
,
48 JH7110_VOUTCLK_HDMITX0_PIXELCLK
),
50 JH71X0_GMUX(JH7110_VOUTCLK_DOM_VOUT_TOP_LCD
, "dom_vout_top_lcd", 0, 2,
51 JH7110_VOUTCLK_DC8200_PIX0
,
52 JH7110_VOUTCLK_DC8200_PIX1
),
54 JH71X0_GATE(JH7110_VOUTCLK_DSITX_APB
, "dsiTx_apb", 0, JH7110_VOUTCLK_DSI_SYS
),
55 JH71X0_GATE(JH7110_VOUTCLK_DSITX_SYS
, "dsiTx_sys", 0, JH7110_VOUTCLK_DSI_SYS
),
56 JH71X0_GMUX(JH7110_VOUTCLK_DSITX_DPI
, "dsiTx_dpi", 0, 2,
57 JH7110_VOUTCLK_DC8200_PIX
,
58 JH7110_VOUTCLK_HDMITX0_PIXELCLK
),
59 JH71X0_GATE(JH7110_VOUTCLK_DSITX_TXESC
, "dsiTx_txesc", 0, JH7110_VOUTCLK_TX_ESC
),
61 JH71X0_GATE(JH7110_VOUTCLK_MIPITX_DPHY_TXESC
, "mipitx_dphy_txesc", 0,
62 JH7110_VOUTCLK_TX_ESC
),
64 JH71X0_GATE(JH7110_VOUTCLK_HDMI_TX_MCLK
, "hdmi_tx_mclk", 0,
65 JH7110_VOUTCLK_VOUT_TOP_HDMITX0_MCLK
),
66 JH71X0_GATE(JH7110_VOUTCLK_HDMI_TX_BCLK
, "hdmi_tx_bclk", 0,
67 JH7110_VOUTCLK_I2STX0_BCLK
),
68 JH71X0_GATE(JH7110_VOUTCLK_HDMI_TX_SYS
, "hdmi_tx_sys", 0, JH7110_VOUTCLK_APB
),
71 static int jh7110_vout_top_rst_init(struct jh71x0_clk_priv
*priv
)
73 struct reset_control
*top_rst
;
75 /* The reset should be shared and other Vout modules will use its. */
76 top_rst
= devm_reset_control_get_shared(priv
->dev
, NULL
);
78 return dev_err_probe(priv
->dev
, PTR_ERR(top_rst
), "failed to get top reset\n");
80 return reset_control_deassert(top_rst
);
83 static struct clk_hw
*jh7110_voutclk_get(struct of_phandle_args
*clkspec
, void *data
)
85 struct jh71x0_clk_priv
*priv
= data
;
86 unsigned int idx
= clkspec
->args
[0];
88 if (idx
< JH7110_VOUTCLK_END
)
89 return &priv
->reg
[idx
].hw
;
91 return ERR_PTR(-EINVAL
);
95 static int jh7110_voutcrg_suspend(struct device
*dev
)
97 struct jh7110_top_sysclk
*top
= dev_get_drvdata(dev
);
99 clk_bulk_disable_unprepare(top
->top_clks_num
, top
->top_clks
);
104 static int jh7110_voutcrg_resume(struct device
*dev
)
106 struct jh7110_top_sysclk
*top
= dev_get_drvdata(dev
);
108 return clk_bulk_prepare_enable(top
->top_clks_num
, top
->top_clks
);
111 static const struct dev_pm_ops jh7110_voutcrg_pm_ops
= {
112 RUNTIME_PM_OPS(jh7110_voutcrg_suspend
, jh7110_voutcrg_resume
, NULL
)
116 static int jh7110_voutcrg_probe(struct platform_device
*pdev
)
118 struct jh71x0_clk_priv
*priv
;
119 struct jh7110_top_sysclk
*top
;
123 priv
= devm_kzalloc(&pdev
->dev
,
124 struct_size(priv
, reg
, JH7110_VOUTCLK_END
),
129 top
= devm_kzalloc(&pdev
->dev
, sizeof(*top
), GFP_KERNEL
);
133 spin_lock_init(&priv
->rmw_lock
);
134 priv
->dev
= &pdev
->dev
;
135 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
136 if (IS_ERR(priv
->base
))
137 return PTR_ERR(priv
->base
);
139 top
->top_clks
= jh7110_vout_top_clks
;
140 top
->top_clks_num
= ARRAY_SIZE(jh7110_vout_top_clks
);
141 ret
= devm_clk_bulk_get(priv
->dev
, top
->top_clks_num
, top
->top_clks
);
143 return dev_err_probe(priv
->dev
, ret
, "failed to get top clocks\n");
144 dev_set_drvdata(priv
->dev
, top
);
146 /* enable power domain and clocks */
147 pm_runtime_enable(priv
->dev
);
148 ret
= pm_runtime_resume_and_get(priv
->dev
);
150 return dev_err_probe(priv
->dev
, ret
, "failed to turn on power\n");
152 ret
= jh7110_vout_top_rst_init(priv
);
156 for (idx
= 0; idx
< JH7110_VOUTCLK_END
; idx
++) {
157 u32 max
= jh7110_voutclk_data
[idx
].max
;
158 struct clk_parent_data parents
[4] = {};
159 struct clk_init_data init
= {
160 .name
= jh7110_voutclk_data
[idx
].name
,
161 .ops
= starfive_jh71x0_clk_ops(max
),
162 .parent_data
= parents
,
164 ((max
& JH71X0_CLK_MUX_MASK
) >> JH71X0_CLK_MUX_SHIFT
) + 1,
165 .flags
= jh7110_voutclk_data
[idx
].flags
,
167 struct jh71x0_clk
*clk
= &priv
->reg
[idx
];
169 const char *fw_name
[JH7110_VOUTCLK_EXT_END
- JH7110_VOUTCLK_END
] = {
173 "vout_top_hdmitx0_mclk",
178 for (i
= 0; i
< init
.num_parents
; i
++) {
179 unsigned int pidx
= jh7110_voutclk_data
[idx
].parents
[i
];
181 if (pidx
< JH7110_VOUTCLK_END
)
182 parents
[i
].hw
= &priv
->reg
[pidx
].hw
;
183 else if (pidx
< JH7110_VOUTCLK_EXT_END
)
184 parents
[i
].fw_name
= fw_name
[pidx
- JH7110_VOUTCLK_END
];
187 clk
->hw
.init
= &init
;
189 clk
->max_div
= max
& JH71X0_CLK_DIV_MASK
;
191 ret
= devm_clk_hw_register(&pdev
->dev
, &clk
->hw
);
196 ret
= devm_of_clk_add_hw_provider(&pdev
->dev
, jh7110_voutclk_get
, priv
);
200 ret
= jh7110_reset_controller_register(priv
, "rst-vo", 4);
207 pm_runtime_put_sync(priv
->dev
);
208 pm_runtime_disable(priv
->dev
);
212 static void jh7110_voutcrg_remove(struct platform_device
*pdev
)
214 pm_runtime_put_sync(&pdev
->dev
);
215 pm_runtime_disable(&pdev
->dev
);
218 static const struct of_device_id jh7110_voutcrg_match
[] = {
219 { .compatible
= "starfive,jh7110-voutcrg" },
222 MODULE_DEVICE_TABLE(of
, jh7110_voutcrg_match
);
224 static struct platform_driver jh7110_voutcrg_driver
= {
225 .probe
= jh7110_voutcrg_probe
,
226 .remove
= jh7110_voutcrg_remove
,
228 .name
= "clk-starfive-jh7110-vout",
229 .of_match_table
= jh7110_voutcrg_match
,
230 .pm
= pm_ptr(&jh7110_voutcrg_pm_ops
),
233 module_platform_driver(jh7110_voutcrg_driver
);
235 MODULE_AUTHOR("Xingyu Wu <xingyu.wu@starfivetech.com>");
236 MODULE_DESCRIPTION("StarFive JH7110 Video-Output clock driver");
237 MODULE_LICENSE("GPL");