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