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