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