usb: support different type of usb charger
[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 = rk_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_battery_charger_detect_cb(usb_battery_charger_detect(1));
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         schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
425
426         return IRQ_HANDLED;
427 }
428
429 /************* register usb detection irqs **************/
430 static int otg_irq_detect_init(struct platform_device *pdev)
431 {
432         int ret = 0;
433         int irq = 0;
434
435         if (control_usb->usb_irq_wakeup) {
436                 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
437                                "usb_detect");
438                 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
439         }
440
441         /*register otg_bvalid irq */
442         irq = platform_get_irq_byname(pdev, "otg_bvalid");
443         if ((irq > 0) && control_usb->usb_irq_wakeup) {
444                 ret = request_irq(irq, bvalid_irq_handler,
445                                   0, "otg_bvalid", NULL);
446                 if (ret < 0) {
447                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
448                 } else {
449                         /* enable bvalid irq  */
450                         writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
451                                RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
452                 }
453         }
454         return ret;
455 }
456
457 /********** end of rk3126 usb detections **********/
458 static int rk_usb_control_probe(struct platform_device *pdev)
459 {
460         int gpio, err;
461         struct device_node *np = pdev->dev.of_node;
462         int ret = 0;
463
464         control_usb =
465             devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
466         if (!control_usb) {
467                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
468                 ret = -ENOMEM;
469                 goto out;
470         }
471
472         control_usb->chip_id = RK3126_USB_CTLR;
473         control_usb->remote_wakeup = of_property_read_bool(np,
474                                                            "rockchip,remote_wakeup");
475         control_usb->usb_irq_wakeup = of_property_read_bool(np,
476                                                             "rockchip,usb_irq_wakeup");
477
478         INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
479                           usb_battery_charger_detect_work);
480
481         control_usb->host_gpios =
482             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
483         if (!control_usb->host_gpios) {
484                 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
485                 ret = -ENOMEM;
486                 goto out;
487         }
488
489         gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
490         control_usb->host_gpios->gpio = gpio;
491
492         if (!gpio_is_valid(gpio)) {
493                 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
494         } else {
495                 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
496                 if (err) {
497                         dev_err(&pdev->dev,
498                                 "failed to request GPIO%d for host_drv\n",
499                                 gpio);
500                         ret = err;
501                         goto out;
502                 }
503                 gpio_direction_output(control_usb->host_gpios->gpio, 1);
504         }
505
506         control_usb->otg_gpios =
507             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
508         if (!control_usb->otg_gpios) {
509                 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
510                 ret = -ENOMEM;
511                 goto out;
512         }
513
514         gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
515         control_usb->otg_gpios->gpio = gpio;
516
517         if (!gpio_is_valid(gpio)) {
518                 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
519         } else {
520                 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
521                 if (err) {
522                         dev_err(&pdev->dev,
523                                 "failed to request GPIO%d for otg_drv\n", gpio);
524                         ret = err;
525                         goto out;
526                 }
527                 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
528         }
529
530 out:
531         return ret;
532 }
533
534 static int rk_usb_control_remove(struct platform_device *pdev)
535 {
536         return 0;
537 }
538
539 static struct platform_driver rk_usb_control_driver = {
540         .probe = rk_usb_control_probe,
541         .remove = rk_usb_control_remove,
542         .driver = {
543                    .name = "rk3126-usb-control",
544                    .owner = THIS_MODULE,
545                    .of_match_table = of_match_ptr(rk_usb_control_id_table),
546                    },
547 };
548
549 #ifdef CONFIG_OF
550
551 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
552         {
553          .compatible = "rockchip,rk3126-dwc-control-usb",
554          },
555         {},
556 };
557
558 #endif
559 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
560 {
561         struct clk *hclk_usb_peri;
562         int ret = 0;
563
564         if (!control_usb) {
565                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
566                 ret = -ENOMEM;
567                 goto err1;
568         }
569
570         hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
571         if (IS_ERR(hclk_usb_peri)) {
572                 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
573                 ret = -EINVAL;
574                 goto err1;
575         }
576
577         control_usb->hclk_usb_peri = hclk_usb_peri;
578         clk_prepare_enable(hclk_usb_peri);
579
580 #ifdef CONFIG_USB20_OTG
581         if (usb20otg_get_status(USB_STATUS_BVABLID))
582                 schedule_delayed_work(&control_usb->usb_charger_det_work,
583                                       HZ / 10);
584 #endif
585
586         ret = otg_irq_detect_init(pdev);
587         if (ret < 0)
588                 goto err2;
589
590         return 0;
591
592 err2:
593         clk_disable_unprepare(hclk_usb_peri);
594 err1:
595         return ret;
596 }
597
598 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
599 {
600         clk_disable_unprepare(control_usb->hclk_usb_peri);
601         return 0;
602 }
603
604 static struct platform_driver dwc_otg_control_usb_driver = {
605         .probe = dwc_otg_control_usb_probe,
606         .remove = dwc_otg_control_usb_remove,
607         .driver = {
608                    .name = "rk3126-dwc-control-usb",
609                    .owner = THIS_MODULE,
610                    .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
611                    },
612 };
613
614 static int __init dwc_otg_control_usb_init(void)
615 {
616         int retval = 0;
617
618         retval |= platform_driver_register(&rk_usb_control_driver);
619         retval |= platform_driver_register(&dwc_otg_control_usb_driver);
620         return retval;
621 }
622
623 subsys_initcall(dwc_otg_control_usb_init);
624
625 static void __exit dwc_otg_control_usb_exit(void)
626 {
627         platform_driver_unregister(&rk_usb_control_driver);
628         platform_driver_unregister(&dwc_otg_control_usb_driver);
629 }
630
631 module_exit(dwc_otg_control_usb_exit);
632 MODULE_ALIAS("platform: dwc_control_usb");
633 MODULE_AUTHOR("RockChip Inc.");
634 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
635 MODULE_LICENSE("GPL v2");
636