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