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