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