1 /**
2 * dwc3-rockchip-inno.c - Rockchip DWC3 Specific Glue layer with INNO PHY
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 *
5 * Authors: William Wu <william.wu@rock-chips.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/clk.h>
23 #include <linux/clk-provider.h>
24 #include <linux/debugfs.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/usb.h>
29 #include <linux/usb/hcd.h>
30 #include <linux/notifier.h>
31 #include <linux/uaccess.h>
32 #include <linux/usb/phy.h>
33 #include <linux/usb/ch9.h>
34
35 #include "core.h"
36
37 #define XHCI_TSTCTRL_MASK (0xf << 28)
38 struct dwc3_rockchip {
39 struct device *dev;
40 struct clk **clks;
41 int num_clocks;
42 struct dwc3 *dwc;
43 struct usb_phy *phy;
44 struct notifier_block u3phy_nb;
45 struct work_struct u3_work;
46 struct mutex lock;
47 };
48
u3phy_disconnect_det_notifier(struct notifier_block * nb,unsigned long event,void * p)49 static int u3phy_disconnect_det_notifier(struct notifier_block *nb,
50 unsigned long event, void *p)
51 {
52 struct dwc3_rockchip *rockchip =
53 container_of(nb, struct dwc3_rockchip, u3phy_nb);
54
55 schedule_work(&rockchip->u3_work);
56
57 return NOTIFY_DONE;
58 }
59
u3phy_disconnect_det_work(struct work_struct * work)60 static void u3phy_disconnect_det_work(struct work_struct *work)
61 {
62 struct dwc3_rockchip *rockchip =
63 container_of(work, struct dwc3_rockchip, u3_work);
64 struct usb_hcd *hcd = dev_get_drvdata(&rockchip->dwc->xhci->dev);
65 u32 count = 0;
66
67 mutex_lock(&rockchip->lock);
68
69 if (hcd->state != HC_STATE_HALT)
70 dwc3_host_exit(rockchip->dwc);
71
72 if (rockchip->phy)
73 usb_phy_shutdown(rockchip->phy);
74
75 while (hcd->state != HC_STATE_HALT) {
76 if (++count > 1000) {
77 dev_err(rockchip->dev,
78 "wait for HCD remove 1s timeout!\n");
79 break;
80 }
81 usleep_range(1000, 1100);
82 }
83
84 if (hcd->state == HC_STATE_HALT)
85 dwc3_host_init(rockchip->dwc);
86
87 if (rockchip->phy)
88 usb_phy_init(rockchip->phy);
89
90 mutex_unlock(&rockchip->lock);
91 }
92
dwc3_rockchip_probe(struct platform_device * pdev)93 static int dwc3_rockchip_probe(struct platform_device *pdev)
94 {
95 struct dwc3_rockchip *rockchip;
96 struct device *dev = &pdev->dev;
97 struct device_node *np = dev->of_node, *child;
98 struct platform_device *child_pdev;
99
100 unsigned int count;
101 int ret;
102 int i;
103
104 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
105 if (!rockchip)
106 return -ENOMEM;
107
108 child = of_get_child_by_name(np, "dwc3");
109 if (!child) {
110 dev_err(dev, "failed to find dwc3 core node\n");
111 return -ENODEV;
112 }
113
114 count = of_clk_get_parent_count(np);
115 if (!count)
116 return -ENOENT;
117
118 rockchip->num_clocks = count;
119
120 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
121 sizeof(struct clk *), GFP_KERNEL);
122 if (!rockchip->clks)
123 return -ENOMEM;
124
125 platform_set_drvdata(pdev, rockchip);
126 rockchip->dev = dev;
127
128 for (i = 0; i < rockchip->num_clocks; i++) {
129 struct clk *clk;
130
131 clk = of_clk_get(np, i);
132 if (IS_ERR(clk)) {
133 ret = PTR_ERR(clk);
134 goto err0;
135 }
136
137 ret = clk_prepare_enable(clk);
138 if (ret < 0) {
139 clk_put(clk);
140 goto err0;
141 }
142
143 rockchip->clks[i] = clk;
144 }
145
146 pm_runtime_set_active(dev);
147 pm_runtime_enable(dev);
148 ret = pm_runtime_get_sync(dev);
149 if (ret < 0) {
150 dev_err(dev, "get_sync failed with err %d\n", ret);
151 goto err1;
152 }
153
154 ret = of_platform_populate(np, NULL, NULL, dev);
155 if (ret)
156 goto err1;
157
158 child_pdev = of_find_device_by_node(child);
159 if (!child_pdev) {
160 dev_err(dev, "failed to find dwc3 core device\n");
161 ret = -ENODEV;
162 goto err2;
163 }
164
165 rockchip->dwc = platform_get_drvdata(child_pdev);
166 if (!rockchip->dwc || !rockchip->dwc->xhci) {
167 dev_dbg(dev, "failed to get drvdata dwc3\n");
168 ret = -EPROBE_DEFER;
169 goto err2;
170 }
171
172 mutex_init(&rockchip->lock);
173
174 rockchip->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
175 if (rockchip->phy) {
176 INIT_WORK(&rockchip->u3_work, u3phy_disconnect_det_work);
177 rockchip->u3phy_nb.notifier_call =
178 u3phy_disconnect_det_notifier;
179 usb_register_notifier(rockchip->phy, &rockchip->u3phy_nb);
180 }
181
182 return 0;
183
184 err2:
185 of_platform_depopulate(dev);
186 err1:
187 pm_runtime_put_sync(dev);
188 pm_runtime_disable(dev);
189 err0:
190 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
191 if (!pm_runtime_status_suspended(dev))
192 clk_disable(rockchip->clks[i]);
193 clk_unprepare(rockchip->clks[i]);
194 clk_put(rockchip->clks[i]);
195 }
196
197 return ret;
198 }
199
dwc3_rockchip_remove(struct platform_device * pdev)200 static int dwc3_rockchip_remove(struct platform_device *pdev)
201 {
202 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
203 struct device *dev = &pdev->dev;
204 int i;
205
206 of_platform_depopulate(dev);
207
208 pm_runtime_put_sync(dev);
209 pm_runtime_disable(dev);
210
211 for (i = 0; i < rockchip->num_clocks; i++) {
212 if (!pm_runtime_status_suspended(dev))
213 clk_disable(rockchip->clks[i]);
214 clk_unprepare(rockchip->clks[i]);
215 clk_put(rockchip->clks[i]);
216 }
217
218 return 0;
219 }
220
221 #ifdef CONFIG_PM
dwc3_rockchip_runtime_suspend(struct device * dev)222 static int dwc3_rockchip_runtime_suspend(struct device *dev)
223 {
224 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
225 int i;
226
227 for (i = 0; i < rockchip->num_clocks; i++)
228 clk_disable(rockchip->clks[i]);
229
230 return 0;
231 }
232
dwc3_rockchip_runtime_resume(struct device * dev)233 static int dwc3_rockchip_runtime_resume(struct device *dev)
234 {
235 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
236 int ret;
237 int i;
238
239 for (i = 0; i < rockchip->num_clocks; i++) {
240 ret = clk_enable(rockchip->clks[i]);
241 if (ret < 0) {
242 while (--i >= 0)
243 clk_disable(rockchip->clks[i]);
244 return ret;
245 }
246 }
247
248 return 0;
249 }
250
251 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
252 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
253 dwc3_rockchip_runtime_resume, NULL)
254 };
255
256 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
257 #else
258 #define DEV_PM_OPS NULL
259 #endif /* CONFIG_PM */
260
261 static const struct of_device_id rockchip_dwc3_match[] = {
262 { .compatible = "rockchip,rk3328-dwc3" },
263 { /* Sentinel */ }
264 };
265 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
266
267 static struct platform_driver dwc3_rockchip_driver = {
268 .probe = dwc3_rockchip_probe,
269 .remove = dwc3_rockchip_remove,
270 .driver = {
271 .name = "rockchip-inno-dwc3",
272 .pm = DEV_PM_OPS,
273 .of_match_table = rockchip_dwc3_match,
274 },
275 };
276
277 module_platform_driver(dwc3_rockchip_driver);
278
279 MODULE_ALIAS("platform:rockchip-inno-dwc3");
280 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
281 MODULE_LICENSE("GPL v2");
282 MODULE_DESCRIPTION("DesignWare USB3 rockchip-inno Glue Layer");
283