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