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