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