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