usb: dwc3: support runtime power management for rockchip platform
[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                 pm_runtime_get_sync(dwc->dev);
95
96                 spin_lock_irqsave(&dwc->lock, flags);
97                 dwc->connected = true;
98                 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
99                 spin_unlock_irqrestore(&dwc->lock, flags);
100
101                 dev_info(rockchip->dev, "USB peripheral connected\n");
102         } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
103                 if (dwc->connected)
104                         return;
105
106                 /*
107                  * If dr_mode is device only, never to
108                  * set the mode to the host mode.
109                  */
110                 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_PERIPHERAL))
111                         return;
112
113                 reset_control_assert(rockchip->otg_rst);
114
115                 ret = phy_power_on(dwc->usb2_generic_phy);
116                 if (ret < 0)
117                         return;
118
119                 ret = phy_power_on(dwc->usb3_generic_phy);
120                 if (ret < 0)
121                         return;
122
123                 reset_control_deassert(rockchip->otg_rst);
124
125                 pm_runtime_get_sync(dwc->dev);
126
127                 hcd = dev_get_drvdata(&dwc->xhci->dev);
128
129                 spin_lock_irqsave(&dwc->lock, flags);
130                 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
131                 dwc->connected = true;
132                 spin_unlock_irqrestore(&dwc->lock, flags);
133
134                 if (hcd->state == HC_STATE_HALT) {
135                         usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
136                         usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
137                 }
138
139                 dev_info(rockchip->dev, "USB HOST connected\n");
140         } else {
141                 if (!dwc->connected)
142                         return;
143
144                 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
145
146                 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
147                     DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
148                         hcd = dev_get_drvdata(&dwc->xhci->dev);
149
150                         if (hcd->state != HC_STATE_HALT) {
151                                 usb_remove_hcd(hcd->shared_hcd);
152                                 usb_remove_hcd(hcd);
153                         }
154
155                         phy_power_off(dwc->usb2_generic_phy);
156                         phy_power_off(dwc->usb3_generic_phy);
157
158                 }
159
160                 spin_lock_irqsave(&dwc->lock, flags);
161                 dwc->connected = false;
162                 spin_unlock_irqrestore(&dwc->lock, flags);
163
164                 pm_runtime_put_sync(dwc->dev);
165
166                 dev_info(rockchip->dev, "USB unconnected\n");
167         }
168 }
169
170 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
171 {
172         int                     ret;
173         struct device           *dev = rockchip->dev;
174         struct extcon_dev       *edev;
175
176         if (device_property_read_bool(dev, "extcon")) {
177                 edev = extcon_get_edev_by_phandle(dev, 0);
178                 if (IS_ERR(edev)) {
179                         if (PTR_ERR(edev) != -EPROBE_DEFER)
180                                 dev_err(dev, "couldn't get extcon device\n");
181                         return PTR_ERR(edev);
182                 }
183
184                 INIT_WORK(&rockchip->otg_work,
185                           dwc3_rockchip_otg_extcon_evt_work);
186
187                 rockchip->device_nb.notifier_call =
188                                 dwc3_rockchip_device_notifier;
189                 ret = extcon_register_notifier(edev, EXTCON_USB,
190                                                &rockchip->device_nb);
191                 if (ret < 0) {
192                         dev_err(dev, "failed to register notifier for USB\n");
193                         return ret;
194                 }
195
196                 rockchip->host_nb.notifier_call =
197                                 dwc3_rockchip_host_notifier;
198                 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
199                                                &rockchip->host_nb);
200                 if (ret < 0) {
201                         dev_err(dev, "failed to register notifier for USB HOST\n");
202                         extcon_unregister_notifier(edev, EXTCON_USB,
203                                                    &rockchip->device_nb);
204                         return ret;
205                 }
206
207                 rockchip->edev = edev;
208         }
209
210         return 0;
211 }
212
213 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
214 {
215         if (!rockchip->edev)
216                 return;
217
218         extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
219                                    &rockchip->device_nb);
220         extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
221                                    &rockchip->host_nb);
222 }
223
224 static int dwc3_rockchip_probe(struct platform_device *pdev)
225 {
226         struct dwc3_rockchip    *rockchip;
227         struct device           *dev = &pdev->dev;
228         struct device_node      *np = dev->of_node, *child;
229         struct platform_device  *child_pdev;
230
231         unsigned int            count;
232         int                     ret;
233         int                     i;
234
235         rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
236
237         if (!rockchip)
238                 return -ENOMEM;
239
240         count = of_clk_get_parent_count(np);
241         if (!count)
242                 return -ENOENT;
243
244         rockchip->num_clocks = count;
245
246         rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
247                                       sizeof(struct clk *), GFP_KERNEL);
248         if (!rockchip->clks)
249                 return -ENOMEM;
250
251         platform_set_drvdata(pdev, rockchip);
252
253         rockchip->dev = dev;
254         rockchip->edev = NULL;
255
256         for (i = 0; i < rockchip->num_clocks; i++) {
257                 struct clk      *clk;
258
259                 clk = of_clk_get(np, i);
260                 if (IS_ERR(clk)) {
261                         while (--i >= 0)
262                                 clk_put(rockchip->clks[i]);
263                         return PTR_ERR(clk);
264                 }
265
266                 ret = clk_prepare_enable(clk);
267                 if (ret < 0) {
268                         while (--i >= 0) {
269                                 clk_disable_unprepare(rockchip->clks[i]);
270                                 clk_put(rockchip->clks[i]);
271                         }
272                         clk_put(clk);
273
274                         return ret;
275                 }
276
277                 rockchip->clks[i] = clk;
278         }
279
280         pm_runtime_set_active(dev);
281         pm_runtime_enable(dev);
282         ret = pm_runtime_get_sync(dev);
283         if (ret < 0) {
284                 dev_err(dev, "get_sync failed with err %d\n", ret);
285                 goto err1;
286         }
287
288         rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
289         if (IS_ERR(rockchip->otg_rst)) {
290                 dev_err(dev, "could not get reset controller\n");
291                 ret = PTR_ERR(rockchip->otg_rst);
292                 goto err1;
293         }
294
295         ret = dwc3_rockchip_extcon_register(rockchip);
296         if (ret < 0)
297                 goto err1;
298
299         child = of_get_child_by_name(np, "dwc3");
300         if (!child) {
301                 dev_err(dev, "failed to find dwc3 core node\n");
302                 ret = -ENODEV;
303                 goto err2;
304         }
305
306         /* Allocate and initialize the core */
307         ret = of_platform_populate(np, NULL, NULL, dev);
308         if (ret) {
309                 dev_err(dev, "failed to create dwc3 core\n");
310                 goto err2;
311         }
312
313         child_pdev = of_find_device_by_node(child);
314         if (!child_pdev) {
315                 dev_err(dev, "failed to find dwc3 core device\n");
316                 ret = -ENODEV;
317                 goto err3;
318         }
319
320         rockchip->dwc = platform_get_drvdata(child_pdev);
321
322         if (rockchip->edev) {
323                 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
324                                                  DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
325                 pm_runtime_allow(&child_pdev->dev);
326
327                 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
328                     rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
329                         struct usb_hcd *hcd =
330                                 dev_get_drvdata(&rockchip->dwc->xhci->dev);
331
332                         if (hcd->state != HC_STATE_HALT) {
333                                 usb_remove_hcd(hcd->shared_hcd);
334                                 usb_remove_hcd(hcd);
335                         }
336                 }
337
338                 pm_runtime_put_sync(dev);
339         }
340
341         return ret;
342
343 err3:
344         of_platform_depopulate(dev);
345
346 err2:
347         dwc3_rockchip_extcon_unregister(rockchip);
348
349 err1:
350         pm_runtime_put_sync(dev);
351         pm_runtime_disable(dev);
352
353         for (i = 0; i < rockchip->num_clocks; i++) {
354                 clk_unprepare(rockchip->clks[i]);
355                 clk_put(rockchip->clks[i]);
356         }
357
358         return ret;
359 }
360
361 static int dwc3_rockchip_remove(struct platform_device *pdev)
362 {
363         struct dwc3_rockchip    *rockchip = platform_get_drvdata(pdev);
364         struct device           *dev = &pdev->dev;
365         int                     i;
366
367         dwc3_rockchip_extcon_unregister(rockchip);
368
369         of_platform_depopulate(dev);
370
371         pm_runtime_put_sync(dev);
372         pm_runtime_disable(dev);
373
374         for (i = 0; i < rockchip->num_clocks; i++) {
375                 clk_unprepare(rockchip->clks[i]);
376                 clk_put(rockchip->clks[i]);
377         }
378
379         return 0;
380 }
381
382 #ifdef CONFIG_PM
383 static int dwc3_rockchip_runtime_suspend(struct device *dev)
384 {
385         struct dwc3_rockchip    *rockchip = dev_get_drvdata(dev);
386         int                     i;
387
388         for (i = 0; i < rockchip->num_clocks; i++)
389                 clk_disable(rockchip->clks[i]);
390
391         return 0;
392 }
393
394 static int dwc3_rockchip_runtime_resume(struct device *dev)
395 {
396         struct dwc3_rockchip    *rockchip = dev_get_drvdata(dev);
397         int                     i;
398
399         for (i = 0; i < rockchip->num_clocks; i++)
400                 clk_enable(rockchip->clks[i]);
401
402         return 0;
403 }
404
405 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
406         SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
407                            dwc3_rockchip_runtime_resume, NULL)
408 };
409
410 #define DEV_PM_OPS      (&dwc3_rockchip_dev_pm_ops)
411 #else
412 #define DEV_PM_OPS      NULL
413 #endif /* CONFIG_PM */
414
415 static const struct of_device_id rockchip_dwc3_match[] = {
416         { .compatible = "rockchip,rk3399-dwc3" },
417         { /* Sentinel */ }
418 };
419
420 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
421
422 static struct platform_driver dwc3_rockchip_driver = {
423         .probe          = dwc3_rockchip_probe,
424         .remove         = dwc3_rockchip_remove,
425         .driver         = {
426                 .name   = "rockchip-dwc3",
427                 .of_match_table = rockchip_dwc3_match,
428                 .pm     = DEV_PM_OPS,
429         },
430 };
431
432 module_platform_driver(dwc3_rockchip_driver);
433
434 MODULE_ALIAS("platform:rockchip-dwc3");
435 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
436 MODULE_LICENSE("GPL v2");
437 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");