Merge branch develop-3.10 into develop-3.10-next
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / usbdev_rk3126.c
1 #include "usbdev_rk.h"
2 #include "usbdev_grf_regs.h"
3 #include "dwc_otg_regs.h"
4 static struct dwc_otg_control_usb *control_usb;
5
6 #ifdef CONFIG_USB20_OTG
7 static void usb20otg_hw_init(void)
8 {
9         /* Turn off differential receiver in suspend mode */
10         writel(UOC_HIWORD_UPDATE(0, 1, 2),
11                    RK_GRF_VIRT + RK312X_GRF_USBPHY0_CON6);
12         /* other haredware init,include:
13          * DRV_VBUS GPIO init */
14         if (gpio_is_valid(control_usb->otg_gpios->gpio)) {
15                 if (gpio_get_value(control_usb->otg_gpios->gpio))
16                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
17         }
18 }
19
20 static void usb20otg_phy_suspend(void *pdata, int suspend)
21 {
22         struct dwc_otg_platform_data *usbpdata = pdata;
23         if (suspend) {
24                 /* enable soft control */
25                 writel(UOC_HIWORD_UPDATE(0x55, 0x7f, 0),
26                        RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
27                 usbpdata->phy_status = 1;
28         } else {
29                 /* exit suspend */
30                 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
31                        RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
32                 usbpdata->phy_status = 0;
33         }
34 }
35
36 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
37 {
38         struct dwc_otg_platform_data *usbpdata = pdata;
39         struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
40
41         rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
42         rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
43         rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
44         if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
45                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
46                 return;
47         }
48
49         switch(rst_type) {
50         case RST_POR:
51                 /* PHY reset */
52                 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
53                            RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
54                 reset_control_assert(rst_otg_p);
55                 udelay(15);
56                 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
57                            RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
58                 udelay(1500);
59                 reset_control_deassert(rst_otg_p);
60                 udelay(2);
61
62                 /* Controller reset */
63                 reset_control_assert(rst_otg_c);
64                 reset_control_assert(rst_otg_h);
65
66                 udelay(2);
67
68                 reset_control_deassert(rst_otg_c);
69                 reset_control_deassert(rst_otg_h);
70                 break;
71
72         default:
73                 break;
74         }
75 }
76
77 static void usb20otg_clock_init(void *pdata)
78 {
79         struct dwc_otg_platform_data *usbpdata = pdata;
80         struct clk *ahbclk, *phyclk;
81
82         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
83         if (IS_ERR(ahbclk)) {
84                 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
85                 return;
86         }
87
88         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
89         if (IS_ERR(phyclk)) {
90                 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
91                 return;
92         }
93
94         usbpdata->phyclk = phyclk;
95         usbpdata->ahbclk = ahbclk;
96 }
97
98 static void usb20otg_clock_enable(void *pdata, int enable)
99 {
100         struct dwc_otg_platform_data *usbpdata = pdata;
101
102         if (enable) {
103                 clk_prepare_enable(usbpdata->ahbclk);
104                 clk_prepare_enable(usbpdata->phyclk);
105         } else {
106                 clk_disable_unprepare(usbpdata->ahbclk);
107                 /*
108                    clk_disable_unprepare(usbpdata->phyclk);
109                  */
110         }
111 }
112
113 static int usb20otg_get_status(int id)
114 {
115         int ret = -1;
116         u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
117         switch (id) {
118         case USB_STATUS_BVABLID:
119                 /* bvalid in grf */
120                 ret = soc_status0 & (0x1 << 5);
121                 break;
122         case USB_STATUS_DPDM:
123                 /* dpdm in grf */
124                 ret = soc_status0 & (0x3 << 6);
125                 break;
126         case USB_STATUS_ID:
127                 /* id in grf */
128                 ret = soc_status0 & (0x1 << 8);
129                 break;
130         case USB_STATUS_UARTMODE:
131                 ret = readl(RK_GRF_VIRT + RK312X_GRF_UOC1_CON4) & (1 << 12);
132                 break;
133         case USB_CHIP_ID:
134                 ret = control_usb->chip_id;
135                 break;
136         case USB_REMOTE_WAKEUP:
137                 ret = control_usb->remote_wakeup;
138                 break;
139         case USB_IRQ_WAKEUP:
140                 ret = control_usb->usb_irq_wakeup;
141                 break;
142         default:
143                 break;
144         }
145         return ret;
146 }
147
148 #ifdef CONFIG_RK_USB_UART
149 /**
150  *  dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
151  *
152  *  Returns true if the status property of node "usb_uart" is set to "okay"
153  *  or "ok", if this property is absent it will use the default status "ok"
154  *  0 otherwise
155  */
156 static bool dwc_otg_uart_enabled(void)
157 {
158         struct device_node *np;
159
160         np = of_find_node_by_name(NULL, "usb_uart");
161         if (np && of_device_is_available(np))
162                 return true;
163
164         return false;
165 }
166
167 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
168 {
169         if ((1 == enter_usb_uart_mode) && dwc_otg_uart_enabled()) {
170                 /* bypass dm, enter uart mode */
171                 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12), RK_GRF_VIRT +
172                            RK312X_GRF_UOC1_CON4);
173
174         } else if (0 == enter_usb_uart_mode) {
175                 /* enter usb mode */
176                 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12), RK_GRF_VIRT + 
177                            RK312X_GRF_UOC1_CON4);
178         }
179 }
180 #else
181 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
182 {
183 }
184 #endif
185
186 static void usb20otg_power_enable(int enable)
187 {
188         if (0 == enable) {
189                 /* disable otg_drv power */
190                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
191                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
192         } else if (1 == enable) {
193                 /* enable otg_drv power */
194                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
195                         gpio_set_value(control_usb->otg_gpios->gpio, 1);
196         }
197 }
198
199 struct dwc_otg_platform_data usb20otg_pdata_rk3126 = {
200         .phyclk = NULL,
201         .ahbclk = NULL,
202         .busclk = NULL,
203         .phy_status = 0,
204         .hw_init = usb20otg_hw_init,
205         .phy_suspend = usb20otg_phy_suspend,
206         .soft_reset = usb20otg_soft_reset,
207         .clock_init = usb20otg_clock_init,
208         .clock_enable = usb20otg_clock_enable,
209         .get_status = usb20otg_get_status,
210         .power_enable = usb20otg_power_enable,
211         .dwc_otg_uart_mode = dwc_otg_uart_mode,
212         .bc_detect_cb = rk_battery_charger_detect_cb,
213 };
214 #endif
215
216 #if defined(CONFIG_USB20_HOST) || defined(CONFIG_USB_EHCI_RK)
217 static void usb20host_hw_init(void)
218 {
219         /* Turn off differential receiver in suspend mode */
220         writel(UOC_HIWORD_UPDATE(0, 1, 2),
221                    RK_GRF_VIRT + RK312X_GRF_USBPHY1_CON6);
222         /* Set disconnect detection trigger point to 600mv */
223         writel(UOC_HIWORD_UPDATE(1, 0xf, 11),
224                    RK_GRF_VIRT + RK312X_GRF_USBPHY1_CON7);
225         /* other haredware init,include:
226          * DRV_VBUS GPIO init */
227         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
228                 if (!gpio_get_value(control_usb->host_gpios->gpio))
229                         gpio_set_value(control_usb->host_gpios->gpio, 1);
230         }
231 }
232
233 static void usb20host_phy_suspend(void *pdata, int suspend)
234 {
235         struct dwc_otg_platform_data *usbpdata = pdata;
236
237         if (suspend) {
238                 /* enable soft control */
239                 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
240                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
241                 usbpdata->phy_status = 1;
242         } else {
243                 /* exit suspend */
244                 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
245                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
246                 usbpdata->phy_status = 0;
247         }
248 }
249
250 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
251 {
252         struct dwc_otg_platform_data *usbpdata = pdata;
253         struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
254
255         rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
256         rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
257         rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
258         if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
259                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
260                 return;
261         }
262
263         switch(rst_type) {
264         case RST_POR:
265                 /* PHY reset */
266                 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
267                            RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
268                 reset_control_assert(rst_host_p);
269                 udelay(15);
270                 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
271                            RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
272
273                 udelay(1500);
274                 reset_control_deassert(rst_host_p);
275
276                 /* Controller reset */
277                 reset_control_assert(rst_host_c);
278                 reset_control_assert(rst_host_h);
279
280                 udelay(5);
281
282                 reset_control_deassert(rst_host_c);
283                 reset_control_deassert(rst_host_h);
284                 break;
285
286         default:
287                 break;
288         }
289 }
290
291 static void usb20host_clock_init(void *pdata)
292 {
293         struct dwc_otg_platform_data *usbpdata = pdata;
294         struct clk *ahbclk, *phyclk;
295
296         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
297         if (IS_ERR(ahbclk)) {
298                 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
299                 return;
300         }
301
302         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
303         if (IS_ERR(phyclk)) {
304                 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
305                 return;
306         }
307
308         usbpdata->phyclk = phyclk;
309         usbpdata->ahbclk = ahbclk;
310 }
311
312 static void usb20host_clock_enable(void *pdata, int enable)
313 {
314         struct dwc_otg_platform_data *usbpdata = pdata;
315
316         if (enable) {
317                 clk_prepare_enable(usbpdata->ahbclk);
318                 clk_prepare_enable(usbpdata->phyclk);
319         } else {
320                 clk_disable_unprepare(usbpdata->ahbclk);
321                 clk_disable_unprepare(usbpdata->phyclk);
322         }
323 }
324
325 static int usb20host_get_status(int id)
326 {
327         int ret = -1;
328         u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
329
330         switch (id) {
331         case USB_STATUS_BVABLID:
332                 /* bvalid in grf */
333                 ret = soc_status0 & (0x1 << 10);
334                 break;
335         case USB_STATUS_DPDM:
336                 /* dpdm in grf */
337                 ret = soc_status0 & (0x3 << 11);
338                 break;
339         case USB_STATUS_ID:
340                 /* id in grf */
341                 ret = 0;
342                 break;
343         case USB_CHIP_ID:
344                 ret = control_usb->chip_id;
345                 break;
346         case USB_REMOTE_WAKEUP:
347                 ret = control_usb->remote_wakeup;
348                 break;
349         case USB_IRQ_WAKEUP:
350                 ret = control_usb->usb_irq_wakeup;
351                 break;
352         default:
353                 break;
354         }
355         return ret;
356 }
357
358 static void usb20host_power_enable(int enable)
359 {
360         if (0 == enable) {
361                 /* disable host_drv power */
362                 /* do not disable power in default */
363         } else if (1 == enable) {
364                 /* enable host_drv power */
365                 if (gpio_is_valid(control_usb->host_gpios->gpio))
366                         gpio_set_value(control_usb->host_gpios->gpio, 1);
367         }
368 }
369
370 struct dwc_otg_platform_data usb20host_pdata_rk3126 = {
371         .phyclk = NULL,
372         .ahbclk = NULL,
373         .busclk = NULL,
374         .phy_status = 0,
375         .hw_init = usb20host_hw_init,
376         .phy_suspend = usb20host_phy_suspend,
377         .soft_reset = usb20host_soft_reset,
378         .clock_init = usb20host_clock_init,
379         .clock_enable = usb20host_clock_enable,
380         .get_status = usb20host_get_status,
381         .power_enable = usb20host_power_enable,
382 };
383 #endif
384
385 #ifdef CONFIG_USB_EHCI_RK
386 static void usb20ehci_phy_suspend(void *pdata, int suspend)
387 {
388         struct rkehci_platform_data *usbpdata = pdata;
389
390         if (suspend) {
391                 /* enable soft control */
392                 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
393                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
394                 usbpdata->phy_status = 1;
395         } else {
396                 /* exit suspend */
397                 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
398                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
399                 usbpdata->phy_status = 0;
400         }
401 }
402
403 static void usb20ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
404 {
405         struct rkehci_platform_data *usbpdata = pdata;
406         struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
407
408         rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
409         rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
410         rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
411         if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
412                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
413                 return;
414         }
415
416         switch(rst_type) {
417         case RST_POR:
418                 /* PHY reset */
419                 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
420                            RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
421                 reset_control_assert(rst_host_p);
422                 udelay(15);
423                 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
424                            RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
425
426                 udelay(1500);
427                 reset_control_deassert(rst_host_p);
428
429                 /* Controller reset */
430                 reset_control_assert(rst_host_c);
431                 reset_control_assert(rst_host_h);
432
433                 udelay(5);
434
435                 reset_control_deassert(rst_host_c);
436                 reset_control_deassert(rst_host_h);
437                 break;
438
439         default:
440                 break;
441         }
442 }
443
444 static void usb20ehci_clock_init(void *pdata)
445 {
446         struct rkehci_platform_data *usbpdata = pdata;
447         struct clk *ahbclk, *phyclk;
448
449         if (soc_is_rk3126b())
450                 ahbclk = devm_clk_get(usbpdata->dev, "hclk_hoct0_3126b");
451         else
452                 ahbclk = devm_clk_get(usbpdata->dev, "hclk_hoct0_3126");
453         if (IS_ERR(ahbclk)) {
454                 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
455                 return;
456         }
457
458         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
459         if (IS_ERR(phyclk)) {
460                 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
461                 return;
462         }
463
464         usbpdata->phyclk = phyclk;
465         usbpdata->ahbclk = ahbclk;
466 }
467
468 static void usb20ehci_clock_enable(void *pdata, int enable)
469 {
470         struct rkehci_platform_data *usbpdata = pdata;
471
472         if (enable) {
473                 clk_prepare_enable(usbpdata->ahbclk);
474                 clk_prepare_enable(usbpdata->phyclk);
475         } else {
476                 clk_disable_unprepare(usbpdata->ahbclk);
477                 clk_disable_unprepare(usbpdata->phyclk);
478         }
479 }
480
481 struct rkehci_platform_data usb20ehci_pdata_rk3126 = {
482         .phyclk = NULL,
483         .ahbclk = NULL,
484         .phy_status = 0,
485         .hw_init = usb20host_hw_init,
486         .phy_suspend = usb20ehci_phy_suspend,
487         .soft_reset = usb20ehci_soft_reset,
488         .clock_init = usb20ehci_clock_init,
489         .clock_enable = usb20ehci_clock_enable,
490         .get_status = usb20host_get_status,
491 };
492 #endif
493
494 struct dwc_otg_platform_data usb20ohci_pdata_rk3126;
495
496 #ifdef CONFIG_OF
497 static const struct of_device_id rk_usb_control_id_table[] = {
498         {
499          .compatible = "rockchip,rk3126-usb-control",
500          },
501         {},
502 };
503 #endif
504 /*********************************************************************
505                         rk3126 usb detections
506 *********************************************************************/
507
508 #define WAKE_LOCK_TIMEOUT (HZ * 10)
509 static inline void do_wakeup(struct work_struct *work)
510 {
511         /* wake up the system */
512         rk_send_wakeup_key();
513 }
514
515 static void usb_battery_charger_detect_work(struct work_struct *work)
516 {
517         rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
518 }
519
520 /********** handler for bvalid irq **********/
521 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
522 {
523         /* clear irq */
524         writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
525                RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
526 #ifdef CONFIG_RK_USB_UART
527         /* usb otg dp/dm switch to usb phy */
528         dwc_otg_uart_mode(NULL, PHY_USB_MODE);
529 #endif
530
531         if (control_usb->usb_irq_wakeup) {
532                 wake_lock_timeout(&control_usb->usb_wakelock,
533                                   WAKE_LOCK_TIMEOUT);
534                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
535                                       HZ / 10);
536         }
537
538         schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
539
540         return IRQ_HANDLED;
541 }
542
543 /************* register usb detection irqs **************/
544 static int otg_irq_detect_init(struct platform_device *pdev)
545 {
546         int ret = 0;
547         int irq = 0;
548
549         if (control_usb->usb_irq_wakeup) {
550                 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
551                                "usb_detect");
552                 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
553         }
554
555         /*register otg_bvalid irq */
556         irq = platform_get_irq_byname(pdev, "otg_bvalid");
557         if ((irq > 0) && control_usb->usb_irq_wakeup) {
558                 ret = request_irq(irq, bvalid_irq_handler,
559                                   0, "otg_bvalid", NULL);
560                 if (ret < 0) {
561                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
562                 } else {
563                         /* enable bvalid irq  */
564                         writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
565                                RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
566                 }
567         }
568         return ret;
569 }
570
571 /********** end of rk3126 usb detections **********/
572 static int rk_usb_control_probe(struct platform_device *pdev)
573 {
574         int gpio, err;
575         struct device_node *np = pdev->dev.of_node;
576         int ret = 0;
577
578         control_usb =
579             devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
580         if (!control_usb) {
581                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
582                 ret = -ENOMEM;
583                 goto out;
584         }
585
586         control_usb->chip_id = RK3126_USB_CTLR;
587         control_usb->remote_wakeup = of_property_read_bool(np,
588                                                            "rockchip,remote_wakeup");
589         control_usb->usb_irq_wakeup = of_property_read_bool(np,
590                                                             "rockchip,usb_irq_wakeup");
591
592         INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
593                           usb_battery_charger_detect_work);
594
595         control_usb->host_gpios =
596             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
597         if (!control_usb->host_gpios) {
598                 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
599                 ret = -ENOMEM;
600                 goto out;
601         }
602
603         gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
604         control_usb->host_gpios->gpio = gpio;
605
606         if (!gpio_is_valid(gpio)) {
607                 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
608         } else {
609                 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
610                 if (err) {
611                         dev_err(&pdev->dev,
612                                 "failed to request GPIO%d for host_drv\n",
613                                 gpio);
614                         ret = err;
615                         goto out;
616                 }
617                 gpio_direction_output(control_usb->host_gpios->gpio, 1);
618         }
619
620         control_usb->otg_gpios =
621             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
622         if (!control_usb->otg_gpios) {
623                 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
624                 ret = -ENOMEM;
625                 goto out;
626         }
627
628         gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
629         control_usb->otg_gpios->gpio = gpio;
630
631         if (!gpio_is_valid(gpio)) {
632                 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
633         } else {
634                 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
635                 if (err) {
636                         dev_err(&pdev->dev,
637                                 "failed to request GPIO%d for otg_drv\n", gpio);
638                         ret = err;
639                         goto out;
640                 }
641                 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
642         }
643
644 out:
645         return ret;
646 }
647
648 static int rk_usb_control_remove(struct platform_device *pdev)
649 {
650         return 0;
651 }
652
653 static struct platform_driver rk_usb_control_driver = {
654         .probe = rk_usb_control_probe,
655         .remove = rk_usb_control_remove,
656         .driver = {
657                    .name = "rk3126-usb-control",
658                    .owner = THIS_MODULE,
659                    .of_match_table = of_match_ptr(rk_usb_control_id_table),
660                    },
661 };
662
663 #ifdef CONFIG_OF
664
665 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
666         {
667          .compatible = "rockchip,rk3126-dwc-control-usb",
668          },
669         {},
670 };
671
672 #endif
673 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
674 {
675         struct clk *hclk_usb_peri;
676         int ret = 0;
677
678         if (!control_usb) {
679                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
680                 ret = -ENOMEM;
681                 goto err1;
682         }
683
684         hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
685         if (IS_ERR(hclk_usb_peri)) {
686                 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
687                 ret = -EINVAL;
688                 goto err1;
689         }
690
691         control_usb->hclk_usb_peri = hclk_usb_peri;
692         clk_prepare_enable(hclk_usb_peri);
693
694 #ifdef CONFIG_USB20_OTG
695         if (usb20otg_get_status(USB_STATUS_BVABLID))
696                 schedule_delayed_work(&control_usb->usb_charger_det_work,
697                                       HZ / 10);
698 #endif
699
700         ret = otg_irq_detect_init(pdev);
701         if (ret < 0)
702                 goto err2;
703
704         return 0;
705
706 err2:
707         clk_disable_unprepare(hclk_usb_peri);
708 err1:
709         return ret;
710 }
711
712 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
713 {
714         clk_disable_unprepare(control_usb->hclk_usb_peri);
715         return 0;
716 }
717
718 static struct platform_driver dwc_otg_control_usb_driver = {
719         .probe = dwc_otg_control_usb_probe,
720         .remove = dwc_otg_control_usb_remove,
721         .driver = {
722                    .name = "rk3126-dwc-control-usb",
723                    .owner = THIS_MODULE,
724                    .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
725                    },
726 };
727
728 static int __init dwc_otg_control_usb_init(void)
729 {
730         int retval = 0;
731
732         retval |= platform_driver_register(&rk_usb_control_driver);
733         retval |= platform_driver_register(&dwc_otg_control_usb_driver);
734         return retval;
735 }
736
737 subsys_initcall(dwc_otg_control_usb_init);
738
739 static void __exit dwc_otg_control_usb_exit(void)
740 {
741         platform_driver_unregister(&rk_usb_control_driver);
742         platform_driver_unregister(&dwc_otg_control_usb_driver);
743 }
744
745 module_exit(dwc_otg_control_usb_exit);
746 MODULE_ALIAS("platform: dwc_control_usb");
747 MODULE_AUTHOR("RockChip Inc.");
748 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
749 MODULE_LICENSE("GPL v2");
750