blob: acf41ba3638dd7ba7950c6a2cdd34dd7db5ee471 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/**
2 * dwc3-of-simple.c - OF glue layer for simple integrations
3 *
4 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Author: Felipe Balbi <balbi@ti.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 of
10 * the License as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
18 * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC
19 * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com>
20 */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/platform_device.h>
26#include <linux/dma-mapping.h>
27#include <linux/clk.h>
28#include <linux/of.h>
29#include <linux/of_platform.h>
30#include <linux/pm_runtime.h>
31
32struct dwc3_of_simple {
33 struct device *dev;
34 struct clk **clks;
35 int num_clocks;
36};
37
38static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
39{
40 struct device *dev = simple->dev;
41 struct device_node *np = dev->of_node;
42 int i;
43
44 simple->num_clocks = count;
45
46 if (!count)
47 return 0;
48
49 simple->clks = devm_kcalloc(dev, simple->num_clocks,
50 sizeof(struct clk *), GFP_KERNEL);
51 if (!simple->clks)
52 return -ENOMEM;
53
54 for (i = 0; i < simple->num_clocks; i++) {
55 struct clk *clk;
56 int ret;
57
58 clk = of_clk_get(np, i);
59 if (IS_ERR(clk)) {
60 while (--i >= 0) {
61 clk_disable_unprepare(simple->clks[i]);
62 clk_put(simple->clks[i]);
63 }
64 return PTR_ERR(clk);
65 }
66
67 ret = clk_prepare_enable(clk);
68 if (ret < 0) {
69 while (--i >= 0) {
70 clk_disable_unprepare(simple->clks[i]);
71 clk_put(simple->clks[i]);
72 }
73 clk_put(clk);
74
75 return ret;
76 }
77
78 simple->clks[i] = clk;
79 }
80
81 return 0;
82}
83
84static int dwc3_of_simple_probe(struct platform_device *pdev)
85{
86 struct dwc3_of_simple *simple;
87 struct device *dev = &pdev->dev;
88 struct device_node *np = dev->of_node;
89
90 int ret;
91 int i;
92
93 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
94 if (!simple)
95 return -ENOMEM;
96
97 platform_set_drvdata(pdev, simple);
98 simple->dev = dev;
99
100 ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
101 "clocks", "#clock-cells"));
102 if (ret)
103 return ret;
104
105 ret = of_platform_populate(np, NULL, NULL, dev);
106 if (ret) {
107 for (i = 0; i < simple->num_clocks; i++) {
108 clk_disable_unprepare(simple->clks[i]);
109 clk_put(simple->clks[i]);
110 }
111
112 return ret;
113 }
114
115 pm_runtime_set_active(dev);
116 pm_runtime_enable(dev);
117 pm_runtime_get_sync(dev);
118
119 return 0;
120}
121
122static int dwc3_of_simple_remove(struct platform_device *pdev)
123{
124 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
125 struct device *dev = &pdev->dev;
126 int i;
127
128 for (i = 0; i < simple->num_clocks; i++) {
129 clk_disable_unprepare(simple->clks[i]);
130 clk_put(simple->clks[i]);
131 }
132
133 of_platform_depopulate(dev);
134
135 pm_runtime_disable(dev);
136 pm_runtime_put_noidle(dev);
137 pm_runtime_set_suspended(dev);
138
139 return 0;
140}
141
142#ifdef CONFIG_PM
143static int dwc3_of_simple_runtime_suspend(struct device *dev)
144{
145 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
146 int i;
147
148 for (i = 0; i < simple->num_clocks; i++)
149 clk_disable(simple->clks[i]);
150
151 return 0;
152}
153
154static int dwc3_of_simple_runtime_resume(struct device *dev)
155{
156 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
157 int ret;
158 int i;
159
160 for (i = 0; i < simple->num_clocks; i++) {
161 ret = clk_enable(simple->clks[i]);
162 if (ret < 0) {
163 while (--i >= 0)
164 clk_disable(simple->clks[i]);
165 return ret;
166 }
167 }
168
169 return 0;
170}
171#endif
172
173static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
174 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
175 dwc3_of_simple_runtime_resume, NULL)
176};
177
178static const struct of_device_id of_dwc3_simple_match[] = {
179 { .compatible = "qcom,dwc3" },
180 { .compatible = "rockchip,rk3399-dwc3" },
181 { .compatible = "xlnx,zynqmp-dwc3" },
182 { .compatible = "cavium,octeon-7130-usb-uctl" },
183 { .compatible = "sprd,sc9860-dwc3" },
184 { /* Sentinel */ }
185};
186MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
187
188static struct platform_driver dwc3_of_simple_driver = {
189 .probe = dwc3_of_simple_probe,
190 .remove = dwc3_of_simple_remove,
191 .driver = {
192 .name = "dwc3-of-simple",
193 .of_match_table = of_dwc3_simple_match,
194 },
195};
196
197module_platform_driver(dwc3_of_simple_driver);
198MODULE_LICENSE("GPL v2");
199MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
200MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");