USB: Modify EHCI & OHCI clk and gpio.
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / usbdev_rk30.c
1
2 #include "usbdev_rk.h"
3 #include "usbdev_grf_regs.h"
4 #include "dwc_otg_regs.h"
5
6 static struct dwc_otg_control_usb *control_usb;
7
8 #ifdef CONFIG_USB20_OTG
9
10 static void usb20otg_hw_init(void)
11 {
12 #ifndef CONFIG_USB20_HOST
13         //enable soft control
14         control_usb->grf_uoc1_base->CON2 = (0x01<<2)|((0x01<<2)<<16);
15         // enter suspend
16         control_usb->grf_uoc1_base->CON3 = 0x2A|(0x3F<<16);
17 #endif
18         /* usb phy config init
19          * usb phy enter usb mode */
20         control_usb->grf_uoc0_base->CON0 = (0x0300 << 16);
21
22         /* other haredware init,include:
23          * DRV_VBUS GPIO init */
24         if(gpio_get_value(control_usb->otg_gpios->gpio)){
25                 gpio_set_value(control_usb->otg_gpios->gpio, 0);
26         }
27 }
28
29 static void usb20otg_phy_suspend(void* pdata, int suspend)
30 {
31         struct dwc_otg_platform_data *usbpdata=pdata;
32
33         if(suspend){
34                 //enable soft control
35                 control_usb->grf_uoc0_base->CON2 = (0x01<<2)|((0x01<<2)<<16);
36                 // enter suspend
37                 control_usb->grf_uoc0_base->CON3 = 0x2A|(0x3F<<16);
38                 usbpdata->phy_status = 1;
39         }else{
40                 // exit suspend.
41                 control_usb->grf_uoc0_base->CON2 = ((0x01<<2)<<16);
42                 usbpdata->phy_status = 0;
43         }
44 }
45
46 static void usb20otg_soft_reset(void)
47 {
48 }
49
50 static void usb20otg_clock_init(void* pdata)
51 {
52         struct dwc_otg_platform_data *usbpdata=pdata;
53         struct clk* ahbclk,*phyclk;
54
55         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
56         if (IS_ERR(ahbclk)) {
57                 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
58                 return;
59         }
60
61         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
62         if (IS_ERR(phyclk)) {
63                 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
64                 return;
65         }
66
67         usbpdata->phyclk = phyclk;
68         usbpdata->ahbclk = ahbclk;
69 }
70
71 static void usb20otg_clock_enable(void* pdata, int enable)
72 {
73         struct dwc_otg_platform_data *usbpdata=pdata;
74
75         if(enable){
76                 clk_prepare_enable(usbpdata->ahbclk);
77                 clk_prepare_enable(usbpdata->phyclk);
78         }else{
79                 clk_disable_unprepare(usbpdata->ahbclk);
80                 clk_disable_unprepare(usbpdata->phyclk);
81         }
82 }
83
84 static int usb20otg_get_status(int id)
85 {
86         int ret = -1;
87
88         switch(id){
89                 case USB_STATUS_BVABLID:
90                         // bvalid in grf
91                         ret = control_usb->grf_soc_status0_rk3188->otg_bvalid;
92                         break;
93                 case USB_STATUS_DPDM:
94                         // dpdm in grf
95                         ret = control_usb->grf_soc_status0_rk3188->otg_linestate;
96                         break;
97                 case USB_STATUS_ID:
98                         // id in grf
99                         ret = control_usb->grf_soc_status0_rk3188->otg_iddig;
100                         break;
101                 case USB_CHIP_ID:
102                         ret = control_usb->chip_id;
103                         break;
104                 case USB_REMOTE_WAKEUP:
105                         ret = control_usb->remote_wakeup;
106                         break;
107                 case USB_IRQ_WAKEUP:
108                         ret = control_usb->usb_irq_wakeup;
109                         break;
110                 default:
111                         break;
112         }
113
114         return ret;
115 }
116
117 #ifdef CONFIG_RK_USB_UART
118 static void dwc_otg_uart_mode(void* pdata, int enter_usb_uart_mode)
119 {
120         if(1 == enter_usb_uart_mode){
121                 /* enter uart mode
122                  * note: can't disable otg here! If otg disable, the ID change
123                  * interrupt can't be triggered when otg cable connect without
124                  * device.At the same time, uart can't be used normally
125                  */
126                 /* bypass dm, enter uart mode */
127                 control_usb->grf_uoc0_base->CON0 = (0x0300 | (0x0300 << 16));
128         }else if(0 == enter_usb_uart_mode){
129                 /* enter usb mode */
130                 control_usb->grf_uoc0_base->CON0 = (0x0300 << 16);
131         }
132 }
133 #endif
134
135 static void usb20otg_power_enable(int enable)
136 {
137         if(0 == enable){//disable otg_drv power
138                 gpio_set_value(control_usb->otg_gpios->gpio, 0);
139         }else if(1 == enable){//enable otg_drv power
140                 gpio_set_value(control_usb->otg_gpios->gpio, 1);
141         }
142 }
143
144 struct dwc_otg_platform_data usb20otg_pdata_rk3188 = {
145         .phyclk = NULL,
146         .ahbclk = NULL,
147         .busclk = NULL,
148         .phy_status = 0,
149         .hw_init = usb20otg_hw_init,
150         .phy_suspend = usb20otg_phy_suspend,
151         .soft_reset = usb20otg_soft_reset,
152         .clock_init = usb20otg_clock_init,
153         .clock_enable = usb20otg_clock_enable,
154         .get_status = usb20otg_get_status,
155         .power_enable = usb20otg_power_enable,
156 #ifdef CONFIG_RK_USB_UART
157         .dwc_otg_uart_mode = dwc_otg_uart_mode,
158 #endif
159     .bc_detect_cb=usb20otg_battery_charger_detect_cb,
160 };
161
162 #endif
163
164 #ifdef CONFIG_USB20_HOST
165 static void usb20host_hw_init(void)
166 {
167         /* usb phy config init */
168
169         /* other haredware init,include:
170          * DRV_VBUS GPIO init */
171         if(!gpio_get_value(control_usb->host_gpios->gpio)){
172                 gpio_set_value(control_usb->host_gpios->gpio, 1);
173         }
174 }
175
176 static void usb20host_phy_suspend(void* pdata, int suspend)
177 {
178         struct dwc_otg_platform_data *usbpdata=pdata;
179
180         if(suspend){
181                 // enable soft control
182                 control_usb->grf_uoc1_base->CON2 = (0x01<<2)|((0x01<<2)<<16);
183                 // enter suspend
184                 control_usb->grf_uoc1_base->CON3 = 0x2A|(0x3F<<16); 
185                 usbpdata->phy_status = 1;
186         }else{
187                 //exit suspend.
188                 control_usb->grf_uoc1_base->CON2 = ((0x01<<2)<<16);
189                 usbpdata->phy_status = 0;
190         }
191 }
192
193 static void usb20host_soft_reset(void)
194 {
195 }
196
197 static void usb20host_clock_init(void* pdata)
198 {
199         struct dwc_otg_platform_data *usbpdata=pdata;
200         struct clk* ahbclk,*phyclk;
201
202         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
203         if (IS_ERR(ahbclk)) {
204                 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
205                 return;
206         }
207
208         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
209         if (IS_ERR(phyclk)) {
210                 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
211                 return;
212         }
213
214         usbpdata->phyclk = phyclk;
215         usbpdata->ahbclk = ahbclk;
216 }
217
218 static void usb20host_clock_enable(void* pdata, int enable)
219 {
220         struct dwc_otg_platform_data *usbpdata=pdata;
221
222         if(enable){
223                 clk_prepare_enable(usbpdata->ahbclk);
224                 clk_prepare_enable(usbpdata->phyclk);
225         }else{
226                 clk_disable_unprepare(usbpdata->ahbclk);
227                 clk_disable_unprepare(usbpdata->phyclk);
228         }
229 }
230
231 static int usb20host_get_status(int id)
232 {
233         int ret = -1;
234
235         switch(id){
236                 case USB_STATUS_BVABLID:
237                         // bvalid in grf
238                         ret = control_usb->grf_soc_status0_rk3188->uhost_bvalid;
239                         break;
240                 case USB_STATUS_DPDM:
241                         // dpdm in grf
242                         ret = control_usb->grf_soc_status0_rk3188->uhost_linestate;
243                         break;
244                 case USB_STATUS_ID:
245                         // id in grf
246                         ret = control_usb->grf_soc_status0_rk3188->uhost_iddig;
247                         break;
248                 case USB_CHIP_ID:
249                         ret = control_usb->chip_id;
250                         break;
251                 case USB_REMOTE_WAKEUP:
252                         ret = control_usb->remote_wakeup;
253                         break;
254                 case USB_IRQ_WAKEUP:
255                         ret = control_usb->usb_irq_wakeup;
256                         break;
257                 default:
258                         break;
259         }
260
261         return ret;
262 }
263
264 static void usb20host_power_enable(int enable)
265 {
266         if(0 == enable){//disable host_drv power
267                 //do not disable power in default
268         }else if(1 == enable){//enable host_drv power
269                 gpio_set_value(control_usb->host_gpios->gpio, 1);
270         }
271 }
272
273 struct dwc_otg_platform_data usb20host_pdata_rk3188 = {
274         .phyclk = NULL,
275         .ahbclk = NULL,
276         .busclk = NULL,
277         .phy_status = 0,
278         .hw_init = usb20host_hw_init,
279         .phy_suspend = usb20host_phy_suspend,
280         .soft_reset = usb20host_soft_reset,
281         .clock_init = usb20host_clock_init,
282         .clock_enable = usb20host_clock_enable,
283         .get_status = usb20host_get_status,
284         .power_enable = usb20host_power_enable,
285 };
286 #endif
287
288 #ifdef CONFIG_USB_EHCI_RKHSIC
289 static void rk_hsic_hw_init(void)
290 {
291         // usb phy config init
292         // hsic phy config init, set hsicphy_txsrtune
293         control_usb->grf_uoc2_base->CON0 = ((0xf<<6)<<16)|(0xf<<6);
294
295         /* other haredware init
296          * set common_on, in suspend mode, otg/host PLL blocks remain powered
297          * for RK3168 set control_usb->grf_uoc0_base->CON0 = (1<<16)|0;
298          * for Rk3188 set control_usb->grf_uoc1_base->CON0 = (1<<16)|0;
299          */
300         control_usb->grf_uoc1_base->CON0 = (1<<16)|0;
301
302         /* change INCR to INCR16 or INCR8(beats less than 16)
303          * or INCR4(beats less than 8) or SINGLE(beats less than 4)
304          */
305         control_usb->grf_uoc3_base->CON0 = 0x00ff00bc;
306 }
307
308 static void rk_hsic_clock_init(void* pdata)
309 {
310         /* By default, hsicphy_480m's parent is otg phy 480MHz clk
311          * rk3188 must use host phy 480MHz clk, because if otg bypass
312          * to uart mode, otg phy 480MHz clk will be closed automatically
313          */
314         struct rkehci_platform_data *usbpdata=pdata;
315         struct clk *ahbclk, *phyclk480m_hsic, *phyclk12m_hsic, *phyclk_usbphy1;
316
317         phyclk480m_hsic = devm_clk_get(usbpdata->dev, "hsicphy_480m");
318         if (IS_ERR(phyclk480m_hsic)) {
319                 dev_err(usbpdata->dev, "Failed to get hsicphy_480m\n");
320                 return;
321         }
322
323         phyclk12m_hsic = devm_clk_get(usbpdata->dev, "hsicphy_12m");
324         if (IS_ERR(phyclk12m_hsic)) {
325                 dev_err(usbpdata->dev, "Failed to get hsicphy_12m\n");
326                 return;
327         }
328
329         phyclk_usbphy1 = devm_clk_get(usbpdata->dev, "hsic_usbphy1");
330         if (IS_ERR(phyclk_usbphy1)) {
331                 dev_err(usbpdata->dev, "Failed to get hsic_usbphy1\n");
332                 return;
333         }
334
335         ahbclk = devm_clk_get(usbpdata->dev, "hclk_hsic");
336         if (IS_ERR(ahbclk)) {
337                 dev_err(usbpdata->dev, "Failed to get hclk_hsic\n");
338                 return;
339         }
340
341         clk_set_parent(phyclk480m_hsic, phyclk_usbphy1);
342         
343         usbpdata->hclk_hsic = ahbclk;
344         usbpdata->hsic_phy_480m = phyclk480m_hsic;
345         usbpdata->hsic_phy_12m = phyclk12m_hsic;
346 }
347
348 static void rk_hsic_clock_enable(void* pdata, int enable)
349 {
350         struct rkehci_platform_data *usbpdata=pdata;
351
352         if(enable == usbpdata->clk_status)
353                 return;
354         if(enable){
355                 clk_prepare_enable(usbpdata->hclk_hsic);
356                 clk_prepare_enable(usbpdata->hsic_phy_480m);
357                 clk_prepare_enable(usbpdata->hsic_phy_12m);
358                 usbpdata->clk_status = 1;
359         }else{
360                 clk_disable_unprepare(usbpdata->hclk_hsic);
361                 clk_disable_unprepare(usbpdata->hsic_phy_480m);
362                 clk_disable_unprepare(usbpdata->hsic_phy_12m);
363                 usbpdata->clk_status = 0;
364         }
365 }
366
367 static void rk_hsic_soft_reset(void)
368 {
369
370 }
371
372 struct rkehci_platform_data rkhsic_pdata_rk3188 = {
373         .hclk_hsic = NULL,
374         .hsic_phy_12m = NULL,
375         .hsic_phy_480m = NULL,
376         .clk_status = -1,
377         .hw_init = rk_hsic_hw_init,
378         .clock_init = rk_hsic_clock_init,
379         .clock_enable = rk_hsic_clock_enable,
380         .soft_reset = rk_hsic_soft_reset,
381 };
382 #endif
383
384 #define WAKE_LOCK_TIMEOUT (HZ * 10)
385
386 inline static void do_wakeup(struct work_struct *work)
387 {
388 //      rk28_send_wakeup_key();
389 }
390
391 /********** handler for bvalid irq **********/
392 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
393 {
394         /* clear irq */
395         control_usb->grf_uoc0_base->CON3 = (1 << 31) | (1 << 15);
396
397 #ifdef CONFIG_RK_USB_UART
398         /* usb otg dp/dm switch to usb phy */
399         dwc_otg_uart_mode(NULL, PHY_USB_MODE);
400 #endif
401
402         if(control_usb->usb_irq_wakeup){
403                 wake_lock_timeout(&control_usb->usb_wakelock, WAKE_LOCK_TIMEOUT);
404                 schedule_delayed_work(&control_usb->usb_det_wakeup_work, HZ/10);
405         }
406
407         return IRQ_HANDLED;
408 }
409
410 /************* register bvalid irq **************/
411 static int otg_irq_detect_init(struct platform_device *pdev)
412 {
413         int ret = 0;
414         int irq = 0;
415
416         if(control_usb->usb_irq_wakeup){
417                 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND, "usb_detect");
418                 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
419         }
420
421         irq = platform_get_irq_byname(pdev, "otg_bvalid");
422         if (irq > 0) {
423                 ret = request_irq(irq, bvalid_irq_handler, 0, "otg_bvalid", NULL);
424                 if(ret < 0){
425                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
426                         return ret;
427                 }
428
429                 /* clear & enable bvalid irq */
430                 control_usb->grf_uoc0_base->CON3 = (3 << 30) | (3 << 14);
431
432                 if(control_usb->usb_irq_wakeup){
433                         enable_irq_wake(irq);
434                 }
435         }
436
437         return ret;
438 }
439
440 static int usb_grf_ioremap(struct platform_device *pdev)
441 {
442         int ret = 0;
443         struct resource *res;
444         void *grf_soc_status0;
445         void *grf_uoc0_base;
446         void *grf_uoc1_base;
447         void *grf_uoc2_base;
448         void *grf_uoc3_base;
449
450         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
451                                                 "GRF_SOC_STATUS0");
452         grf_soc_status0 = devm_ioremap_resource(&pdev->dev, res);
453         if (IS_ERR(grf_soc_status0)){
454                 ret = PTR_ERR(grf_soc_status0);
455                 return ret;
456         }
457         control_usb->grf_soc_status0_rk3188 = (pGRF_SOC_STATUS_RK3188)grf_soc_status0;
458
459         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
460                                                 "GRF_UOC0_BASE");
461         grf_uoc0_base = devm_ioremap_resource(&pdev->dev, res);
462         if (IS_ERR(grf_uoc0_base)){
463                 ret = PTR_ERR(grf_uoc0_base);
464                 return ret;
465         }
466         control_usb->grf_uoc0_base = (pGRF_UOC0_REG)grf_uoc0_base;
467
468         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
469                                                 "GRF_UOC1_BASE");
470         grf_uoc1_base = devm_ioremap_resource(&pdev->dev, res);
471         if (IS_ERR(grf_uoc1_base)){
472                 ret = PTR_ERR(grf_uoc1_base);
473                 return ret;
474         }
475         control_usb->grf_uoc1_base = (pGRF_UOC1_REG)grf_uoc1_base;
476
477         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
478                                                 "GRF_UOC2_BASE");
479         grf_uoc2_base = devm_ioremap_resource(&pdev->dev, res);
480         if (IS_ERR(grf_uoc2_base)){
481                 ret = PTR_ERR(grf_uoc2_base);
482                 return ret;
483         }
484         control_usb->grf_uoc2_base = (pGRF_UOC2_REG)grf_uoc2_base;
485
486         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
487                                                 "GRF_UOC3_BASE");
488         grf_uoc3_base = devm_ioremap_resource(&pdev->dev, res);
489         if (IS_ERR(grf_uoc3_base)){
490                 ret = PTR_ERR(grf_uoc3_base);
491                 return ret;
492         }
493         control_usb->grf_uoc3_base = (pGRF_UOC3_REG)grf_uoc3_base;
494
495         return ret;
496 }
497
498 #ifdef CONFIG_OF
499
500 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
501         {
502                 .compatible = "rockchip,rk3188-dwc-control-usb",
503         },
504         { },
505 };
506 MODULE_DEVICE_TABLE(of, dwc_otg_control_usb_id_table);
507 #endif
508
509 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
510 {
511         int gpio, err;
512         struct device_node *np = pdev->dev.of_node;
513         struct clk* hclk_usb_peri;
514         int ret = 0;
515
516         control_usb = devm_kzalloc(&pdev->dev, sizeof(*control_usb),GFP_KERNEL);
517
518         if (!control_usb) {
519                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
520                 ret =  -ENOMEM;
521                 goto err1;
522         }
523
524         control_usb->chip_id = RK3188_USB_CTLR;
525         control_usb->remote_wakeup = of_property_read_bool(np,
526                 "rockchip,remote_wakeup");
527         control_usb->usb_irq_wakeup = of_property_read_bool(np,
528                 "rockchip,usb_irq_wakeup");
529
530         hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
531         if (IS_ERR(hclk_usb_peri)) {
532                 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
533                 ret = -EINVAL;
534                 goto err1;
535         }
536
537         control_usb->hclk_usb_peri = hclk_usb_peri;
538         clk_prepare_enable(hclk_usb_peri);
539
540         ret = usb_grf_ioremap(pdev);
541         if(ret){
542                 dev_err(&pdev->dev, "Failed to ioremap usb grf\n");
543                 goto err2;
544         }
545
546         /* init host gpio */
547         control_usb->host_gpios = devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
548         if(!control_usb->host_gpios){
549                 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
550                 ret =  -ENOMEM;
551                 goto err2;
552         }
553
554         gpio =  of_get_named_gpio(np, "gpios", 0);
555         if(!gpio_is_valid(gpio)){
556                 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
557                 ret = -EINVAL;
558                 goto err2;
559         }
560
561         control_usb->host_gpios->gpio = gpio;
562
563         err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
564         if (err) {
565                 dev_err(&pdev->dev,
566                         "failed to request GPIO%d for host_drv\n",
567                         gpio);
568                 ret = err;
569                 goto err2;
570         }
571         gpio_direction_output(control_usb->host_gpios->gpio, 1);
572
573         /* init otg gpio */
574         control_usb->otg_gpios = devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
575         if(!control_usb->otg_gpios){
576                 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
577                 ret =  -ENOMEM;
578                 goto err2;
579         }
580
581         gpio =  of_get_named_gpio(np, "gpios", 1);
582         if(!gpio_is_valid(gpio)){
583                 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
584                 ret = -EINVAL;
585                 goto err2;
586         }
587         control_usb->otg_gpios->gpio = gpio;
588         err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
589         if (err) {
590                 dev_err(&pdev->dev,
591                         "failed to request GPIO%d for otg_drv\n",
592                         gpio);
593                 ret = err;
594                 goto err2;
595         }
596         gpio_direction_output(control_usb->otg_gpios->gpio, 0);
597
598         ret = otg_irq_detect_init(pdev);
599         if (ret < 0)
600                 goto err2;
601
602         return 0;
603
604 err2:
605         clk_disable_unprepare(hclk_usb_peri);
606 err1:
607         return ret;
608 }
609
610 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
611 {
612         clk_disable_unprepare(control_usb->hclk_usb_peri);
613         return 0;
614 }
615
616 static struct platform_driver dwc_otg_control_usb_driver = {
617         .probe          = dwc_otg_control_usb_probe,
618         .remove         = dwc_otg_control_usb_remove,
619         .driver         = {
620                 .name   = "rk3188-dwc-control-usb",
621                 .owner  = THIS_MODULE,
622                 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
623         },
624 };
625
626 static int __init dwc_otg_control_usb_init(void)
627 {
628         return platform_driver_register(&dwc_otg_control_usb_driver);
629 }
630 subsys_initcall(dwc_otg_control_usb_init);
631
632 static void __exit dwc_otg_control_usb_exit(void)
633 {
634         platform_driver_unregister(&dwc_otg_control_usb_driver);
635 }
636
637 module_exit(dwc_otg_control_usb_exit);
638 MODULE_ALIAS("platform: dwc_control_usb");
639 MODULE_AUTHOR("RockChip Inc.");
640 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
641 MODULE_LICENSE("GPL v2");