Merge remote-tracking branch 'origin/develop-3.10' into develop-3.10-next
[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 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
143 {
144 #ifdef CONFIG_RK_USB_UART
145         if (1 == enter_usb_uart_mode) {
146                 /* bypass dm, enter uart mode */
147                 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12),
148                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON4);
149         } else if (0 == enter_usb_uart_mode) {
150                 /* enter usb mode */
151                 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12),
152                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON4);
153         }
154 #endif
155 }
156
157 static void usb20otg_power_enable(int enable)
158 {
159         if (0 == enable) {
160                 /* disable otg_drv power */
161                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
162                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
163         } else if (1 == enable) {
164                 /* enable otg_drv power */
165                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
166                         gpio_set_value(control_usb->otg_gpios->gpio, 1);
167         }
168 }
169
170 struct dwc_otg_platform_data usb20otg_pdata_rk3126 = {
171         .phyclk = NULL,
172         .ahbclk = NULL,
173         .busclk = NULL,
174         .phy_status = 0,
175         .hw_init = usb20otg_hw_init,
176         .phy_suspend = usb20otg_phy_suspend,
177         .soft_reset = usb20otg_soft_reset,
178         .clock_init = usb20otg_clock_init,
179         .clock_enable = usb20otg_clock_enable,
180         .get_status = usb20otg_get_status,
181         .power_enable = usb20otg_power_enable,
182         .dwc_otg_uart_mode = dwc_otg_uart_mode,
183         .bc_detect_cb = usb20otg_battery_charger_detect_cb,
184 };
185 #endif
186
187 #ifdef CONFIG_USB20_HOST
188 static void usb20host_hw_init(void)
189 {
190         /* other haredware init,include:
191          * DRV_VBUS GPIO init */
192         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
193                 if (!gpio_get_value(control_usb->host_gpios->gpio))
194                         gpio_set_value(control_usb->host_gpios->gpio, 1);
195         }
196 }
197
198 static void usb20host_phy_suspend(void *pdata, int suspend)
199 {
200         struct dwc_otg_platform_data *usbpdata = pdata;
201
202         if (suspend) {
203                 /* enable soft control */
204                 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
205                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
206                 usbpdata->phy_status = 1;
207         } else {
208                 /* exit suspend */
209                 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
210                        RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
211                 usbpdata->phy_status = 0;
212         }
213 }
214
215 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
216 {
217         struct dwc_otg_platform_data *usbpdata = pdata;
218         struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
219
220         rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
221         rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
222         rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
223         if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
224                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
225                 return;
226         }
227
228         switch(rst_type) {
229         case RST_POR:
230                 /* PHY reset */
231                 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
232                            RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
233                 reset_control_assert(rst_host_p);
234                 udelay(15);
235                 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
236                            RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
237
238                 udelay(1500);
239                 reset_control_deassert(rst_host_p);
240
241                 /* Controller reset */
242                 reset_control_assert(rst_host_c);
243                 reset_control_assert(rst_host_h);
244
245                 udelay(5);
246
247                 reset_control_deassert(rst_host_c);
248                 reset_control_deassert(rst_host_h);
249                 break;
250
251         default:
252                 break;
253         }
254 }
255
256 static void usb20host_clock_init(void *pdata)
257 {
258         struct dwc_otg_platform_data *usbpdata = pdata;
259         struct clk *ahbclk, *phyclk;
260
261         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
262         if (IS_ERR(ahbclk)) {
263                 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
264                 return;
265         }
266
267         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
268         if (IS_ERR(phyclk)) {
269                 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
270                 return;
271         }
272
273         usbpdata->phyclk = phyclk;
274         usbpdata->ahbclk = ahbclk;
275 }
276
277 static void usb20host_clock_enable(void *pdata, int enable)
278 {
279         struct dwc_otg_platform_data *usbpdata = pdata;
280
281         if (enable) {
282                 clk_prepare_enable(usbpdata->ahbclk);
283                 clk_prepare_enable(usbpdata->phyclk);
284         } else {
285                 clk_disable_unprepare(usbpdata->ahbclk);
286                 clk_disable_unprepare(usbpdata->phyclk);
287         }
288 }
289
290 static int usb20host_get_status(int id)
291 {
292         int ret = -1;
293         u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
294
295         switch (id) {
296         case USB_STATUS_BVABLID:
297                 /* bvalid in grf */
298                 ret = soc_status0 & (0x1 << 10);
299                 break;
300         case USB_STATUS_DPDM:
301                 /* dpdm in grf */
302                 ret = soc_status0 & (0x3 << 11);
303                 break;
304         case USB_STATUS_ID:
305                 /* id in grf */
306                 ret = 0;
307                 break;
308         case USB_CHIP_ID:
309                 ret = control_usb->chip_id;
310                 break;
311         case USB_REMOTE_WAKEUP:
312                 ret = control_usb->remote_wakeup;
313                 break;
314         case USB_IRQ_WAKEUP:
315                 ret = control_usb->usb_irq_wakeup;
316                 break;
317         default:
318                 break;
319         }
320         return ret;
321 }
322
323 static void usb20host_power_enable(int enable)
324 {
325         if (0 == enable) {
326                 /* disable host_drv power */
327                 /* do not disable power in default */
328         } else if (1 == enable) {
329                 /* enable host_drv power */
330                 if (gpio_is_valid(control_usb->host_gpios->gpio))
331                         gpio_set_value(control_usb->host_gpios->gpio, 1);
332         }
333 }
334
335 struct dwc_otg_platform_data usb20host_pdata_rk3126 = {
336         .phyclk = NULL,
337         .ahbclk = NULL,
338         .busclk = NULL,
339         .phy_status = 0,
340         .hw_init = usb20host_hw_init,
341         .phy_suspend = usb20host_phy_suspend,
342         .soft_reset = usb20host_soft_reset,
343         .clock_init = usb20host_clock_init,
344         .clock_enable = usb20host_clock_enable,
345         .get_status = usb20host_get_status,
346         .power_enable = usb20host_power_enable,
347 };
348 #endif
349
350 #ifdef CONFIG_OF
351 static const struct of_device_id rk_usb_control_id_table[] = {
352         {
353          .compatible = "rockchip,rk3126-usb-control",
354          },
355         {},
356 };
357 #endif
358 /*********************************************************************
359                         rk3126 usb detections
360 *********************************************************************/
361
362 #define WAKE_LOCK_TIMEOUT (HZ * 10)
363 static inline void do_wakeup(struct work_struct *work)
364 {
365         /* wake up the system */
366         rk_send_wakeup_key();
367 }
368
369 static void usb_battery_charger_detect_work(struct work_struct *work)
370 {
371         rk_usb_charger_status = usb_battery_charger_detect(0);
372 }
373
374 /********** handler for bvalid irq **********/
375 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
376 {
377         /* clear irq */
378         writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
379                RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
380 #ifdef CONFIG_RK_USB_UART
381         /* usb otg dp/dm switch to usb phy */
382         dwc_otg_uart_mode(NULL, PHY_USB_MODE);
383 #endif
384
385         if (control_usb->usb_irq_wakeup) {
386                 wake_lock_timeout(&control_usb->usb_wakelock,
387                                   WAKE_LOCK_TIMEOUT);
388                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
389                                       HZ / 10);
390         }
391
392         rk_usb_charger_status = USB_BC_TYPE_SDP;
393         schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
394
395         return IRQ_HANDLED;
396 }
397
398 /************* register usb detection irqs **************/
399 static int otg_irq_detect_init(struct platform_device *pdev)
400 {
401         int ret = 0;
402         int irq = 0;
403
404         if (control_usb->usb_irq_wakeup) {
405                 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
406                                "usb_detect");
407                 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
408         }
409
410         /*register otg_bvalid irq */
411         irq = platform_get_irq_byname(pdev, "otg_bvalid");
412         if ((irq > 0) && control_usb->usb_irq_wakeup) {
413                 ret = request_irq(irq, bvalid_irq_handler,
414                                   0, "otg_bvalid", NULL);
415                 if (ret < 0) {
416                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
417                 } else {
418                         /* enable bvalid irq  */
419                         writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
420                                RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
421                 }
422         }
423         return ret;
424 }
425
426 /********** end of rk3126 usb detections **********/
427 static int rk_usb_control_probe(struct platform_device *pdev)
428 {
429         int gpio, err;
430         struct device_node *np = pdev->dev.of_node;
431         int ret = 0;
432
433         control_usb =
434             devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
435         if (!control_usb) {
436                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
437                 ret = -ENOMEM;
438                 goto out;
439         }
440
441         control_usb->chip_id = RK3126_USB_CTLR;
442         control_usb->remote_wakeup = of_property_read_bool(np,
443                                                            "rockchip,remote_wakeup");
444         control_usb->usb_irq_wakeup = of_property_read_bool(np,
445                                                             "rockchip,usb_irq_wakeup");
446
447         INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
448                           usb_battery_charger_detect_work);
449
450         control_usb->host_gpios =
451             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
452         if (!control_usb->host_gpios) {
453                 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
454                 ret = -ENOMEM;
455                 goto out;
456         }
457
458         gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
459         control_usb->host_gpios->gpio = gpio;
460
461         if (!gpio_is_valid(gpio)) {
462                 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
463         } else {
464                 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
465                 if (err) {
466                         dev_err(&pdev->dev,
467                                 "failed to request GPIO%d for host_drv\n",
468                                 gpio);
469                         ret = err;
470                         goto out;
471                 }
472                 gpio_direction_output(control_usb->host_gpios->gpio, 1);
473         }
474
475         control_usb->otg_gpios =
476             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
477         if (!control_usb->otg_gpios) {
478                 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
479                 ret = -ENOMEM;
480                 goto out;
481         }
482
483         gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
484         control_usb->otg_gpios->gpio = gpio;
485
486         if (!gpio_is_valid(gpio)) {
487                 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
488         } else {
489                 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
490                 if (err) {
491                         dev_err(&pdev->dev,
492                                 "failed to request GPIO%d for otg_drv\n", gpio);
493                         ret = err;
494                         goto out;
495                 }
496                 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
497         }
498
499 out:
500         return ret;
501 }
502
503 static int rk_usb_control_remove(struct platform_device *pdev)
504 {
505         return 0;
506 }
507
508 static struct platform_driver rk_usb_control_driver = {
509         .probe = rk_usb_control_probe,
510         .remove = rk_usb_control_remove,
511         .driver = {
512                    .name = "rk3126-usb-control",
513                    .owner = THIS_MODULE,
514                    .of_match_table = of_match_ptr(rk_usb_control_id_table),
515                    },
516 };
517
518 #ifdef CONFIG_OF
519
520 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
521         {
522          .compatible = "rockchip,rk3126-dwc-control-usb",
523          },
524         {},
525 };
526
527 #endif
528 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
529 {
530         struct clk *hclk_usb_peri;
531         int ret = 0;
532
533         if (!control_usb) {
534                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
535                 ret = -ENOMEM;
536                 goto err1;
537         }
538
539         hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
540         if (IS_ERR(hclk_usb_peri)) {
541                 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
542                 ret = -EINVAL;
543                 goto err1;
544         }
545
546         control_usb->hclk_usb_peri = hclk_usb_peri;
547         clk_prepare_enable(hclk_usb_peri);
548
549 #ifdef CONFIG_USB20_OTG
550         if (usb20otg_get_status(USB_STATUS_BVABLID)) {
551                 rk_usb_charger_status = USB_BC_TYPE_SDP;
552                 schedule_delayed_work(&control_usb->usb_charger_det_work,
553                                       HZ / 10);
554         }
555 #endif
556
557         ret = otg_irq_detect_init(pdev);
558         if (ret < 0)
559                 goto err2;
560
561         return 0;
562
563 err2:
564         clk_disable_unprepare(hclk_usb_peri);
565 err1:
566         return ret;
567 }
568
569 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
570 {
571         clk_disable_unprepare(control_usb->hclk_usb_peri);
572         return 0;
573 }
574
575 static struct platform_driver dwc_otg_control_usb_driver = {
576         .probe = dwc_otg_control_usb_probe,
577         .remove = dwc_otg_control_usb_remove,
578         .driver = {
579                    .name = "rk3126-dwc-control-usb",
580                    .owner = THIS_MODULE,
581                    .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
582                    },
583 };
584
585 static int __init dwc_otg_control_usb_init(void)
586 {
587         int retval = 0;
588
589         retval |= platform_driver_register(&rk_usb_control_driver);
590         retval |= platform_driver_register(&dwc_otg_control_usb_driver);
591         return retval;
592 }
593
594 subsys_initcall(dwc_otg_control_usb_init);
595
596 static void __exit dwc_otg_control_usb_exit(void)
597 {
598         platform_driver_unregister(&rk_usb_control_driver);
599         platform_driver_unregister(&dwc_otg_control_usb_driver);
600 }
601
602 module_exit(dwc_otg_control_usb_exit);
603 MODULE_ALIAS("platform: dwc_control_usb");
604 MODULE_AUTHOR("RockChip Inc.");
605 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
606 MODULE_LICENSE("GPL v2");
607