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