1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas R-Car USB2.0 clock selector
4  *
5  * Copyright (C) 2017 Renesas Electronics Corp.
6  *
7  * Based on renesas-cpg-mssr.c
8  *
9  * Copyright (C) 2015 Glider bvba
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/reset.h>
23 #include <linux/slab.h>
24 
25 #define USB20_CLKSET0		0x00
26 #define CLKSET0_INTCLK_EN	BIT(11)
27 #define CLKSET0_PRIVATE		BIT(0)
28 #define CLKSET0_EXTAL_ONLY	(CLKSET0_INTCLK_EN | CLKSET0_PRIVATE)
29 
30 static const struct clk_bulk_data rcar_usb2_clocks[] = {
31 	{ .id = "ehci_ohci", },
32 	{ .id = "hs-usb-if", },
33 };
34 
35 struct usb2_clock_sel_priv {
36 	void __iomem *base;
37 	struct clk_hw hw;
38 	struct clk_bulk_data clks[ARRAY_SIZE(rcar_usb2_clocks)];
39 	struct reset_control *rsts;
40 	bool extal;
41 	bool xtal;
42 };
43 #define to_priv(_hw)	container_of(_hw, struct usb2_clock_sel_priv, hw)
44 
45 static void usb2_clock_sel_enable_extal_only(struct usb2_clock_sel_priv *priv)
46 {
47 	u16 val = readw(priv->base + USB20_CLKSET0);
48 
49 	pr_debug("%s: enter %d %d %x\n", __func__,
50 		 priv->extal, priv->xtal, val);
51 
52 	if (priv->extal && !priv->xtal && val != CLKSET0_EXTAL_ONLY)
53 		writew(CLKSET0_EXTAL_ONLY, priv->base + USB20_CLKSET0);
54 }
55 
56 static void usb2_clock_sel_disable_extal_only(struct usb2_clock_sel_priv *priv)
57 {
58 	if (priv->extal && !priv->xtal)
59 		writew(CLKSET0_PRIVATE, priv->base + USB20_CLKSET0);
60 }
61 
62 static int usb2_clock_sel_enable(struct clk_hw *hw)
63 {
64 	struct usb2_clock_sel_priv *priv = to_priv(hw);
65 	int ret;
66 
67 	ret = reset_control_deassert(priv->rsts);
68 	if (ret)
69 		return ret;
70 
71 	ret = clk_bulk_prepare_enable(ARRAY_SIZE(priv->clks), priv->clks);
72 	if (ret) {
73 		reset_control_assert(priv->rsts);
74 		return ret;
75 	}
76 
77 	usb2_clock_sel_enable_extal_only(priv);
78 
79 	return 0;
80 }
81 
82 static void usb2_clock_sel_disable(struct clk_hw *hw)
83 {
84 	struct usb2_clock_sel_priv *priv = to_priv(hw);
85 
86 	usb2_clock_sel_disable_extal_only(priv);
87 
88 	clk_bulk_disable_unprepare(ARRAY_SIZE(priv->clks), priv->clks);
89 	reset_control_assert(priv->rsts);
90 }
91 
92 /*
93  * This module seems a mux, but this driver assumes a gate because
94  * ehci/ohci platform drivers don't support clk_set_parent() for now.
95  * If this driver acts as a gate, ehci/ohci-platform drivers don't need
96  * any modification.
97  */
98 static const struct clk_ops usb2_clock_sel_clock_ops = {
99 	.enable = usb2_clock_sel_enable,
100 	.disable = usb2_clock_sel_disable,
101 };
102 
103 static const struct of_device_id rcar_usb2_clock_sel_match[] = {
104 	{ .compatible = "renesas,rcar-gen3-usb2-clock-sel" },
105 	{ }
106 };
107 
108 static int rcar_usb2_clock_sel_suspend(struct device *dev)
109 {
110 	struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
111 
112 	usb2_clock_sel_disable_extal_only(priv);
113 	pm_runtime_put(dev);
114 
115 	return 0;
116 }
117 
118 static int rcar_usb2_clock_sel_resume(struct device *dev)
119 {
120 	struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
121 
122 	pm_runtime_get_sync(dev);
123 	usb2_clock_sel_enable_extal_only(priv);
124 
125 	return 0;
126 }
127 
128 static void rcar_usb2_clock_sel_remove(struct platform_device *pdev)
129 {
130 	struct device *dev = &pdev->dev;
131 
132 	of_clk_del_provider(dev->of_node);
133 	pm_runtime_put(dev);
134 	pm_runtime_disable(dev);
135 }
136 
137 static int rcar_usb2_clock_sel_probe(struct platform_device *pdev)
138 {
139 	struct device *dev = &pdev->dev;
140 	struct device_node *np = dev->of_node;
141 	struct usb2_clock_sel_priv *priv;
142 	struct clk *clk;
143 	struct clk_init_data init = {};
144 	int ret;
145 
146 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
147 	if (!priv)
148 		return -ENOMEM;
149 
150 	priv->base = devm_platform_ioremap_resource(pdev, 0);
151 	if (IS_ERR(priv->base))
152 		return PTR_ERR(priv->base);
153 
154 	memcpy(priv->clks, rcar_usb2_clocks, sizeof(priv->clks));
155 	ret = devm_clk_bulk_get(dev, ARRAY_SIZE(priv->clks), priv->clks);
156 	if (ret < 0)
157 		return ret;
158 
159 	priv->rsts = devm_reset_control_array_get_shared(dev);
160 	if (IS_ERR(priv->rsts))
161 		return PTR_ERR(priv->rsts);
162 
163 	clk = devm_clk_get(dev, "usb_extal");
164 	if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
165 		priv->extal = !!clk_get_rate(clk);
166 		clk_disable_unprepare(clk);
167 	}
168 	clk = devm_clk_get(dev, "usb_xtal");
169 	if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
170 		priv->xtal = !!clk_get_rate(clk);
171 		clk_disable_unprepare(clk);
172 	}
173 
174 	if (!priv->extal && !priv->xtal) {
175 		dev_err(dev, "This driver needs usb_extal or usb_xtal\n");
176 		return -ENOENT;
177 	}
178 
179 	pm_runtime_enable(dev);
180 	pm_runtime_get_sync(dev);
181 	platform_set_drvdata(pdev, priv);
182 	dev_set_drvdata(dev, priv);
183 
184 	init.name = "rcar_usb2_clock_sel";
185 	init.ops = &usb2_clock_sel_clock_ops;
186 	priv->hw.init = &init;
187 
188 	ret = devm_clk_hw_register(dev, &priv->hw);
189 	if (ret)
190 		goto pm_put;
191 
192 	ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
193 	if (ret)
194 		goto pm_put;
195 
196 	return 0;
197 
198 pm_put:
199 	pm_runtime_put(dev);
200 	pm_runtime_disable(dev);
201 	return ret;
202 }
203 
204 static const struct dev_pm_ops rcar_usb2_clock_sel_pm_ops = {
205 	.suspend	= rcar_usb2_clock_sel_suspend,
206 	.resume		= rcar_usb2_clock_sel_resume,
207 };
208 
209 static struct platform_driver rcar_usb2_clock_sel_driver = {
210 	.driver		= {
211 		.name	= "rcar-usb2-clock-sel",
212 		.of_match_table = rcar_usb2_clock_sel_match,
213 		.pm	= &rcar_usb2_clock_sel_pm_ops,
214 	},
215 	.probe		= rcar_usb2_clock_sel_probe,
216 	.remove_new	= rcar_usb2_clock_sel_remove,
217 };
218 builtin_platform_driver(rcar_usb2_clock_sel_driver);
219 
220 MODULE_DESCRIPTION("Renesas R-Car USB2 clock selector Driver");
221 MODULE_LICENSE("GPL v2");
222