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