usb: dwc3: add rockchip specific glue layer
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc3 / dwc3-rockchip.c
1 /**
2  * dwc3-rockchip.c - Rockchip Specific Glue layer
3  *
4  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
5  *
6  * Authors: William Wu <william.wu@rock-chips.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
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/clk.h>
24 #include <linux/clk-provider.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/extcon.h>
29 #include <linux/reset.h>
30 #include <linux/usb.h>
31 #include <linux/usb/hcd.h>
32
33 #include "core.h"
34 #include "io.h"
35
36 #define DWC3_ROCKCHIP_AUTOSUSPEND_DELAY  500 /* ms */
37
38 struct dwc3_rockchip {
39         int                     num_clocks;
40         struct device           *dev;
41         struct clk              **clks;
42         struct dwc3             *dwc;
43         struct reset_control    *otg_rst;
44         struct extcon_dev       *edev;
45         struct notifier_block   device_nb;
46         struct notifier_block   host_nb;
47         struct work_struct      otg_work;
48 };
49
50 static int dwc3_rockchip_device_notifier(struct notifier_block *nb,
51                                          unsigned long event, void *ptr)
52 {
53         struct dwc3_rockchip *rockchip =
54                 container_of(nb, struct dwc3_rockchip, device_nb);
55
56         schedule_work(&rockchip->otg_work);
57
58         return NOTIFY_DONE;
59 }
60
61 static int dwc3_rockchip_host_notifier(struct notifier_block *nb,
62                                        unsigned long event, void *ptr)
63 {
64         struct dwc3_rockchip *rockchip =
65                 container_of(nb, struct dwc3_rockchip, host_nb);
66
67         schedule_work(&rockchip->otg_work);
68
69         return NOTIFY_DONE;
70 }
71
72 static void dwc3_rockchip_otg_extcon_evt_work(struct work_struct *work)
73 {
74         struct dwc3_rockchip    *rockchip =
75                 container_of(work, struct dwc3_rockchip, otg_work);
76         struct dwc3             *dwc = rockchip->dwc;
77         struct extcon_dev       *edev = rockchip->edev;
78         struct usb_hcd          *hcd;
79         unsigned long           flags;
80         int                     ret;
81         u32                     reg;
82
83         if (extcon_get_cable_state_(edev, EXTCON_USB) > 0) {
84                 if (dwc->connected)
85                         return;
86
87                 /*
88                  * If dr_mode is host only, never to set
89                  * the mode to the peripheral mode.
90                  */
91                 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_HOST))
92                         return;
93
94                 reset_control_deassert(rockchip->otg_rst);
95
96                 pm_runtime_get_sync(dwc->dev);
97
98                 spin_lock_irqsave(&dwc->lock, flags);
99                 dwc->connected = true;
100                 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
101                 spin_unlock_irqrestore(&dwc->lock, flags);
102
103                 pm_runtime_put_sync(dwc->dev);
104
105                 dev_info(rockchip->dev, "USB peripheral connected\n");
106         } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
107                 if (dwc->connected)
108                         return;
109
110                 /*
111                  * If dr_mode is device only, never to
112                  * set the mode to the host mode.
113                  */
114                 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_PERIPHERAL))
115                         return;
116
117                 reset_control_assert(rockchip->otg_rst);
118
119                 ret = phy_power_on(dwc->usb2_generic_phy);
120                 if (ret < 0)
121                         return;
122
123                 ret = phy_power_on(dwc->usb3_generic_phy);
124                 if (ret < 0)
125                         return;
126
127                 reset_control_deassert(rockchip->otg_rst);
128
129                 dwc3_phy_setup(dwc);
130
131                 hcd = dev_get_drvdata(&dwc->xhci->dev);
132
133                 spin_lock_irqsave(&dwc->lock, flags);
134                 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
135                 dwc->connected = true;
136                 spin_unlock_irqrestore(&dwc->lock, flags);
137
138                 if (hcd->state == HC_STATE_HALT) {
139                         usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
140                         usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
141                 }
142
143                 dev_info(rockchip->dev, "USB HOST connected\n");
144         } else {
145                 if (!dwc->connected)
146                         return;
147
148                 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
149
150                 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
151                     DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
152                         hcd = dev_get_drvdata(&dwc->xhci->dev);
153
154                         if (hcd->state != HC_STATE_HALT) {
155                                 usb_remove_hcd(hcd->shared_hcd);
156                                 usb_remove_hcd(hcd);
157                         }
158
159                         phy_power_off(dwc->usb2_generic_phy);
160                         phy_power_off(dwc->usb3_generic_phy);
161
162                         reset_control_assert(rockchip->otg_rst);
163                 }
164
165                 spin_lock_irqsave(&dwc->lock, flags);
166                 dwc->connected = false;
167                 spin_unlock_irqrestore(&dwc->lock, flags);
168
169                 dev_info(rockchip->dev, "USB unconnected\n");
170         }
171 }
172
173 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
174 {
175         int                     ret;
176         struct device           *dev = rockchip->dev;
177         struct extcon_dev       *edev;
178
179         if (device_property_read_bool(dev, "extcon")) {
180                 edev = extcon_get_edev_by_phandle(dev, 0);
181                 if (IS_ERR(edev)) {
182                         if (PTR_ERR(edev) != -EPROBE_DEFER)
183                                 dev_err(dev, "couldn't get extcon device\n");
184                         return PTR_ERR(edev);
185                 }
186
187                 INIT_WORK(&rockchip->otg_work,
188                           dwc3_rockchip_otg_extcon_evt_work);
189
190                 rockchip->device_nb.notifier_call =
191                                 dwc3_rockchip_device_notifier;
192                 ret = extcon_register_notifier(edev, EXTCON_USB,
193                                                &rockchip->device_nb);
194                 if (ret < 0) {
195                         dev_err(dev, "failed to register notifier for USB\n");
196                         return ret;
197                 }
198
199                 rockchip->host_nb.notifier_call =
200                                 dwc3_rockchip_host_notifier;
201                 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
202                                                &rockchip->host_nb);
203                 if (ret < 0) {
204                         dev_err(dev, "failed to register notifier for USB HOST\n");
205                         extcon_unregister_notifier(edev, EXTCON_USB,
206                                                    &rockchip->device_nb);
207                         return ret;
208                 }
209
210                 rockchip->edev = edev;
211         }
212
213         return 0;
214 }
215
216 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
217 {
218         if (!rockchip->edev)
219                 return;
220
221         extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
222                                    &rockchip->device_nb);
223         extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
224                                    &rockchip->host_nb);
225 }
226
227 static int dwc3_rockchip_probe(struct platform_device *pdev)
228 {
229         struct dwc3_rockchip    *rockchip;
230         struct device           *dev = &pdev->dev;
231         struct device_node      *np = dev->of_node, *child;
232         struct platform_device  *child_pdev;
233
234         unsigned int            count;
235         int                     ret;
236         int                     i;
237
238         rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
239
240         if (!rockchip)
241                 return -ENOMEM;
242
243         count = of_clk_get_parent_count(np);
244         if (!count)
245                 return -ENOENT;
246
247         rockchip->num_clocks = count;
248
249         rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
250                                       sizeof(struct clk *), GFP_KERNEL);
251         if (!rockchip->clks)
252                 return -ENOMEM;
253
254         platform_set_drvdata(pdev, rockchip);
255
256         rockchip->dev = dev;
257         rockchip->edev = NULL;
258
259         pm_runtime_set_active(dev);
260         pm_runtime_enable(dev);
261         ret = pm_runtime_get_sync(dev);
262         if (ret < 0) {
263                 dev_err(dev, "get_sync failed with err %d\n", ret);
264                 goto err1;
265         }
266
267         for (i = 0; i < rockchip->num_clocks; i++) {
268                 struct clk      *clk;
269
270                 clk = of_clk_get(np, i);
271                 if (IS_ERR(clk)) {
272                         while (--i >= 0)
273                                 clk_put(rockchip->clks[i]);
274                         ret = PTR_ERR(clk);
275
276                         goto err1;
277                 }
278
279                 ret = clk_prepare_enable(clk);
280                 if (ret < 0) {
281                         while (--i >= 0) {
282                                 clk_disable_unprepare(rockchip->clks[i]);
283                                 clk_put(rockchip->clks[i]);
284                         }
285                         clk_put(clk);
286
287                         goto err1;
288                 }
289
290                 rockchip->clks[i] = clk;
291         }
292
293         rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
294         if (IS_ERR(rockchip->otg_rst)) {
295                 dev_err(dev, "could not get reset controller\n");
296                 ret = PTR_ERR(rockchip->otg_rst);
297                 goto err2;
298         }
299
300         ret = dwc3_rockchip_extcon_register(rockchip);
301         if (ret < 0)
302                 goto err2;
303
304         child = of_get_child_by_name(np, "dwc3");
305         if (!child) {
306                 dev_err(dev, "failed to find dwc3 core node\n");
307                 ret = -ENODEV;
308                 goto err3;
309         }
310
311         /* Allocate and initialize the core */
312         ret = of_platform_populate(np, NULL, NULL, dev);
313         if (ret) {
314                 dev_err(dev, "failed to create dwc3 core\n");
315                 goto err3;
316         }
317
318         child_pdev = of_find_device_by_node(child);
319         if (!child_pdev) {
320                 dev_err(dev, "failed to find dwc3 core device\n");
321                 ret = -ENODEV;
322                 goto err4;
323         }
324
325         rockchip->dwc = platform_get_drvdata(child_pdev);
326
327         if (rockchip->edev) {
328                 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
329                                                  DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
330                 pm_runtime_allow(&child_pdev->dev);
331
332                 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
333                     rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
334                         struct usb_hcd *hcd =
335                                 dev_get_drvdata(&rockchip->dwc->xhci->dev);
336
337                         if (hcd->state != HC_STATE_HALT) {
338                                 usb_remove_hcd(hcd->shared_hcd);
339                                 usb_remove_hcd(hcd);
340                         }
341                 }
342         }
343
344         return ret;
345
346 err4:
347         of_platform_depopulate(dev);
348
349 err3:
350         dwc3_rockchip_extcon_unregister(rockchip);
351
352 err2:
353         for (i = 0; i < rockchip->num_clocks; i++) {
354                 clk_disable_unprepare(rockchip->clks[i]);
355                 clk_put(rockchip->clks[i]);
356         }
357
358 err1:
359         pm_runtime_put_sync(dev);
360         pm_runtime_disable(dev);
361
362         return ret;
363 }
364
365 static int dwc3_rockchip_remove(struct platform_device *pdev)
366 {
367         struct dwc3_rockchip    *rockchip = platform_get_drvdata(pdev);
368         struct device           *dev = &pdev->dev;
369         int                     i;
370
371         for (i = 0; i < rockchip->num_clocks; i++) {
372                 clk_disable_unprepare(rockchip->clks[i]);
373                 clk_put(rockchip->clks[i]);
374         }
375
376         dwc3_rockchip_extcon_unregister(rockchip);
377
378         of_platform_depopulate(dev);
379
380         pm_runtime_put_sync(dev);
381         pm_runtime_disable(dev);
382
383         return 0;
384 }
385
386 #ifdef CONFIG_PM_SLEEP
387 static int dwc3_rockchip_suspend(struct device *dev)
388 {
389         struct dwc3_rockchip    *rockchip = dev_get_drvdata(dev);
390         int                     i;
391
392         for (i = 0; i < rockchip->num_clocks; i++)
393                 clk_disable(rockchip->clks[i]);
394
395         return 0;
396 }
397
398 static int dwc3_rockchip_resume(struct device *dev)
399 {
400         struct dwc3_rockchip    *rockchip = dev_get_drvdata(dev);
401         int                     i;
402
403         for (i = 0; i < rockchip->num_clocks; i++)
404                 clk_enable(rockchip->clks[i]);
405
406         /* runtime set active to reflect active state. */
407         pm_runtime_disable(dev);
408         pm_runtime_set_active(dev);
409         pm_runtime_enable(dev);
410
411         return 0;
412 }
413
414 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
415         SET_SYSTEM_SLEEP_PM_OPS(dwc3_rockchip_suspend, dwc3_rockchip_resume)
416 };
417
418 #define DEV_PM_OPS      (&dwc3_rockchip_dev_pm_ops)
419 #else
420 #define DEV_PM_OPS      NULL
421 #endif /* CONFIG_PM_SLEEP */
422
423 static const struct of_device_id rockchip_dwc3_match[] = {
424         { .compatible = "rockchip,rk3399-dwc3" },
425         { /* Sentinel */ }
426 };
427
428 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
429
430 static struct platform_driver dwc3_rockchip_driver = {
431         .probe          = dwc3_rockchip_probe,
432         .remove         = dwc3_rockchip_remove,
433         .driver         = {
434                 .name   = "rockchip-dwc3",
435                 .of_match_table = rockchip_dwc3_match,
436                 .pm     = DEV_PM_OPS,
437         },
438 };
439
440 module_platform_driver(dwc3_rockchip_driver);
441
442 MODULE_ALIAS("platform:rockchip-dwc3");
443 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
444 MODULE_LICENSE("GPL v2");
445 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");