1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas R-Car USB2.0 clock selector
5 * Copyright (C) 2017 Renesas Electronics Corp.
7 * Based on renesas-cpg-mssr.c
9 * Copyright (C) 2015 Glider bvba
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/slab.h>
24 #define USB20_CLKSET0 0x00
25 #define CLKSET0_INTCLK_EN BIT(11)
26 #define CLKSET0_PRIVATE BIT(0)
27 #define CLKSET0_EXTAL_ONLY (CLKSET0_INTCLK_EN | CLKSET0_PRIVATE)
29 struct usb2_clock_sel_priv {
35 #define to_priv(_hw) container_of(_hw, struct usb2_clock_sel_priv, hw)
37 static void usb2_clock_sel_enable_extal_only(struct usb2_clock_sel_priv *priv)
39 u16 val = readw(priv->base + USB20_CLKSET0);
41 pr_debug("%s: enter %d %d %x\n", __func__,
42 priv->extal, priv->xtal, val);
44 if (priv->extal && !priv->xtal && val != CLKSET0_EXTAL_ONLY)
45 writew(CLKSET0_EXTAL_ONLY, priv->base + USB20_CLKSET0);
48 static void usb2_clock_sel_disable_extal_only(struct usb2_clock_sel_priv *priv)
50 if (priv->extal && !priv->xtal)
51 writew(CLKSET0_PRIVATE, priv->base + USB20_CLKSET0);
54 static int usb2_clock_sel_enable(struct clk_hw *hw)
56 usb2_clock_sel_enable_extal_only(to_priv(hw));
61 static void usb2_clock_sel_disable(struct clk_hw *hw)
63 usb2_clock_sel_disable_extal_only(to_priv(hw));
67 * This module seems a mux, but this driver assumes a gate because
68 * ehci/ohci platform drivers don't support clk_set_parent() for now.
69 * If this driver acts as a gate, ehci/ohci-platform drivers don't need
72 static const struct clk_ops usb2_clock_sel_clock_ops = {
73 .enable = usb2_clock_sel_enable,
74 .disable = usb2_clock_sel_disable,
77 static const struct of_device_id rcar_usb2_clock_sel_match[] = {
78 { .compatible = "renesas,rcar-gen3-usb2-clock-sel" },
82 static int rcar_usb2_clock_sel_suspend(struct device *dev)
84 struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
86 usb2_clock_sel_disable_extal_only(priv);
92 static int rcar_usb2_clock_sel_resume(struct device *dev)
94 struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
96 pm_runtime_get_sync(dev);
97 usb2_clock_sel_enable_extal_only(priv);
102 static int rcar_usb2_clock_sel_remove(struct platform_device *pdev)
104 struct device *dev = &pdev->dev;
105 struct usb2_clock_sel_priv *priv = platform_get_drvdata(pdev);
107 of_clk_del_provider(dev->of_node);
108 clk_hw_unregister(&priv->hw);
110 pm_runtime_disable(dev);
115 static int rcar_usb2_clock_sel_probe(struct platform_device *pdev)
117 struct device *dev = &pdev->dev;
118 struct device_node *np = dev->of_node;
119 struct usb2_clock_sel_priv *priv;
120 struct resource *res;
122 struct clk_init_data init;
124 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
128 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
129 priv->base = devm_ioremap_resource(dev, res);
130 if (IS_ERR(priv->base))
131 return PTR_ERR(priv->base);
133 pm_runtime_enable(dev);
134 pm_runtime_get_sync(dev);
136 clk = devm_clk_get(dev, "usb_extal");
137 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
138 priv->extal = !!clk_get_rate(clk);
139 clk_disable_unprepare(clk);
141 clk = devm_clk_get(dev, "usb_xtal");
142 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
143 priv->xtal = !!clk_get_rate(clk);
144 clk_disable_unprepare(clk);
147 if (!priv->extal && !priv->xtal) {
148 dev_err(dev, "This driver needs usb_extal or usb_xtal\n");
152 platform_set_drvdata(pdev, priv);
153 dev_set_drvdata(dev, priv);
155 init.name = "rcar_usb2_clock_sel";
156 init.ops = &usb2_clock_sel_clock_ops;
158 init.parent_names = NULL;
159 init.num_parents = 0;
160 priv->hw.init = &init;
162 clk = clk_register(NULL, &priv->hw);
166 return of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
169 static const struct dev_pm_ops rcar_usb2_clock_sel_pm_ops = {
170 .suspend = rcar_usb2_clock_sel_suspend,
171 .resume = rcar_usb2_clock_sel_resume,
174 static struct platform_driver rcar_usb2_clock_sel_driver = {
176 .name = "rcar-usb2-clock-sel",
177 .of_match_table = rcar_usb2_clock_sel_match,
178 .pm = &rcar_usb2_clock_sel_pm_ops,
180 .probe = rcar_usb2_clock_sel_probe,
181 .remove = rcar_usb2_clock_sel_remove,
183 builtin_platform_driver(rcar_usb2_clock_sel_driver);
185 MODULE_DESCRIPTION("Renesas R-Car USB2 clock selector Driver");
186 MODULE_LICENSE("GPL v2");