clk: Remove io.h from clk-provider.h
[linux-block.git] / drivers / clk / renesas / rcar-usb2-clock-sel.c
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/slab.h>
23
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)
28
29 struct usb2_clock_sel_priv {
30         void __iomem *base;
31         struct clk_hw hw;
32         bool extal;
33         bool xtal;
34 };
35 #define to_priv(_hw)    container_of(_hw, struct usb2_clock_sel_priv, hw)
36
37 static void usb2_clock_sel_enable_extal_only(struct usb2_clock_sel_priv *priv)
38 {
39         u16 val = readw(priv->base + USB20_CLKSET0);
40
41         pr_debug("%s: enter %d %d %x\n", __func__,
42                  priv->extal, priv->xtal, val);
43
44         if (priv->extal && !priv->xtal && val != CLKSET0_EXTAL_ONLY)
45                 writew(CLKSET0_EXTAL_ONLY, priv->base + USB20_CLKSET0);
46 }
47
48 static void usb2_clock_sel_disable_extal_only(struct usb2_clock_sel_priv *priv)
49 {
50         if (priv->extal && !priv->xtal)
51                 writew(CLKSET0_PRIVATE, priv->base + USB20_CLKSET0);
52 }
53
54 static int usb2_clock_sel_enable(struct clk_hw *hw)
55 {
56         usb2_clock_sel_enable_extal_only(to_priv(hw));
57
58         return 0;
59 }
60
61 static void usb2_clock_sel_disable(struct clk_hw *hw)
62 {
63         usb2_clock_sel_disable_extal_only(to_priv(hw));
64 }
65
66 /*
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
70  * any modification.
71  */
72 static const struct clk_ops usb2_clock_sel_clock_ops = {
73         .enable = usb2_clock_sel_enable,
74         .disable = usb2_clock_sel_disable,
75 };
76
77 static const struct of_device_id rcar_usb2_clock_sel_match[] = {
78         { .compatible = "renesas,rcar-gen3-usb2-clock-sel" },
79         { }
80 };
81
82 static int rcar_usb2_clock_sel_suspend(struct device *dev)
83 {
84         struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
85
86         usb2_clock_sel_disable_extal_only(priv);
87         pm_runtime_put(dev);
88
89         return 0;
90 }
91
92 static int rcar_usb2_clock_sel_resume(struct device *dev)
93 {
94         struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
95
96         pm_runtime_get_sync(dev);
97         usb2_clock_sel_enable_extal_only(priv);
98
99         return 0;
100 }
101
102 static int rcar_usb2_clock_sel_remove(struct platform_device *pdev)
103 {
104         struct device *dev = &pdev->dev;
105         struct usb2_clock_sel_priv *priv = platform_get_drvdata(pdev);
106
107         of_clk_del_provider(dev->of_node);
108         clk_hw_unregister(&priv->hw);
109         pm_runtime_put(dev);
110         pm_runtime_disable(dev);
111
112         return 0;
113 }
114
115 static int rcar_usb2_clock_sel_probe(struct platform_device *pdev)
116 {
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;
121         struct clk *clk;
122         struct clk_init_data init;
123
124         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
125         if (!priv)
126                 return -ENOMEM;
127
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);
132
133         pm_runtime_enable(dev);
134         pm_runtime_get_sync(dev);
135
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);
140         }
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);
145         }
146
147         if (!priv->extal && !priv->xtal) {
148                 dev_err(dev, "This driver needs usb_extal or usb_xtal\n");
149                 return -ENOENT;
150         }
151
152         platform_set_drvdata(pdev, priv);
153         dev_set_drvdata(dev, priv);
154
155         init.name = "rcar_usb2_clock_sel";
156         init.ops = &usb2_clock_sel_clock_ops;
157         init.flags = 0;
158         init.parent_names = NULL;
159         init.num_parents = 0;
160         priv->hw.init = &init;
161
162         clk = clk_register(NULL, &priv->hw);
163         if (IS_ERR(clk))
164                 return PTR_ERR(clk);
165
166         return of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
167 }
168
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,
172 };
173
174 static struct platform_driver rcar_usb2_clock_sel_driver = {
175         .driver         = {
176                 .name   = "rcar-usb2-clock-sel",
177                 .of_match_table = rcar_usb2_clock_sel_match,
178                 .pm     = &rcar_usb2_clock_sel_pm_ops,
179         },
180         .probe          = rcar_usb2_clock_sel_probe,
181         .remove         = rcar_usb2_clock_sel_remove,
182 };
183 builtin_platform_driver(rcar_usb2_clock_sel_driver);
184
185 MODULE_DESCRIPTION("Renesas R-Car USB2 clock selector Driver");
186 MODULE_LICENSE("GPL v2");