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