ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / usbdev_rk32.c
1 #ifdef CONFIG_ARM
2 #include "usbdev_rk.h"
3 #include "usbdev_grf_regs.h"
4 #include "dwc_otg_regs.h"
5 static struct dwc_otg_control_usb *control_usb;
6
7 int is_rk3288_usb(void)
8 {
9         if (!control_usb)
10                 return false;
11
12         return control_usb->chip_id == RK3288_USB_CTLR ? true : false;
13 }
14
15 #ifdef CONFIG_USB20_OTG
16 static void usb20otg_hw_init(void)
17 {
18 #ifndef CONFIG_USB20_HOST
19         /* enable soft control */
20         control_usb->grf_uoc2_base->CON2 = (0x01 << 2) | ((0x01 << 2) << 16);
21         /* enter suspend */
22         control_usb->grf_uoc2_base->CON3 = 0x2A | (0x3F << 16);
23 #endif
24         /* usb phy config init
25          * usb phy enter usb mode */
26         control_usb->grf_uoc0_base->CON3 = (0x00c0 << 16);
27
28         /* other haredware init,include:
29          * DRV_VBUS GPIO init */
30         if (gpio_is_valid(control_usb->otg_gpios->gpio)) {
31                 if (gpio_get_value(control_usb->otg_gpios->gpio))
32                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
33         }
34 }
35
36 static void usb20otg_phy_suspend(void *pdata, int suspend)
37 {
38         struct dwc_otg_platform_data *usbpdata = pdata;
39
40         if (suspend) {
41                 /* enable soft control */
42                 control_usb->grf_uoc0_base->CON2 =
43                     (0x01 << 2) | ((0x01 << 2) << 16);
44                 /* enter suspend */
45                 control_usb->grf_uoc0_base->CON3 = 0x2A | (0x3F << 16);
46                 usbpdata->phy_status = 1;
47         } else {
48                 /* exit suspend */
49                 control_usb->grf_uoc0_base->CON2 = ((0x01 << 2) << 16);
50                 usbpdata->phy_status = 0;
51         }
52 }
53
54 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
55 {
56         struct dwc_otg_platform_data *usbpdata = pdata;
57         struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
58
59         rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
60         rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
61         rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
62         if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
63                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
64                 return;
65         }
66
67         reset_control_assert(rst_otg_h);
68         reset_control_assert(rst_otg_p);
69         reset_control_assert(rst_otg_c);
70         udelay(5);
71         reset_control_deassert(rst_otg_h);
72         reset_control_deassert(rst_otg_p);
73         reset_control_deassert(rst_otg_c);
74         mdelay(2);
75 }
76
77 static void usb20otg_clock_init(void *pdata)
78 {
79         struct dwc_otg_platform_data *usbpdata = pdata;
80         struct clk *ahbclk, *phyclk;
81
82         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
83         if (IS_ERR(ahbclk)) {
84                 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
85                 return;
86         }
87
88         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
89         if (IS_ERR(phyclk)) {
90                 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
91                 return;
92         }
93
94         usbpdata->phyclk = phyclk;
95         usbpdata->ahbclk = ahbclk;
96 }
97
98 static void usb20otg_clock_enable(void *pdata, int enable)
99 {
100         struct dwc_otg_platform_data *usbpdata = pdata;
101
102         if (enable) {
103                 clk_prepare_enable(usbpdata->ahbclk);
104                 clk_prepare_enable(usbpdata->phyclk);
105         } else {
106                 clk_disable_unprepare(usbpdata->ahbclk);
107                 clk_disable_unprepare(usbpdata->phyclk);
108         }
109 }
110
111 static int usb20otg_get_status(int id)
112 {
113         int ret = -1;
114
115         switch (id) {
116         case USB_STATUS_BVABLID:
117                 /* bvalid in grf */
118                 ret = control_usb->grf_soc_status2_rk3288->otg_bvalid;
119                 break;
120         case USB_STATUS_DPDM:
121                 /* dpdm in grf */
122                 ret = control_usb->grf_soc_status2_rk3288->otg_linestate;
123                 break;
124         case USB_STATUS_ID:
125                 /* id in grf */
126                 ret = control_usb->grf_soc_status2_rk3288->otg_iddig;
127                 break;
128         case USB_CHIP_ID:
129                 ret = control_usb->chip_id;
130                 break;
131         case USB_REMOTE_WAKEUP:
132                 ret = control_usb->remote_wakeup;
133                 break;
134         case USB_IRQ_WAKEUP:
135                 ret = control_usb->usb_irq_wakeup;
136                 break;
137         default:
138                 break;
139         }
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                 control_usb->grf_uoc0_base->CON3 = (0x00c0 | (0x00c0 << 16));
168
169         } else if (0 == enter_usb_uart_mode) {
170                 /* enter usb mode */
171                 control_usb->grf_uoc0_base->CON3 = (0x00c0 << 16);
172         }
173 }
174 #else
175 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
176 {
177 }
178 #endif
179
180 static void usb20otg_power_enable(int enable)
181 {
182         if (0 == enable) {
183                 /* disable otg_drv power */
184                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
185                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
186
187                 rk_battery_charger_detect_cb(USB_OTG_POWER_OFF);
188         } else if (1 == enable) {
189                 /* enable otg_drv power */
190                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
191                         gpio_set_value(control_usb->otg_gpios->gpio, 1);
192
193                 if (!usb20otg_get_status(USB_STATUS_BVABLID))
194                         rk_battery_charger_detect_cb(USB_OTG_POWER_ON);
195         }
196 }
197
198 struct dwc_otg_platform_data usb20otg_pdata_rk3288 = {
199         .phyclk = NULL,
200         .ahbclk = NULL,
201         .busclk = NULL,
202         .phy_status = 0,
203         .hw_init = usb20otg_hw_init,
204         .phy_suspend = usb20otg_phy_suspend,
205         .soft_reset = usb20otg_soft_reset,
206         .clock_init = usb20otg_clock_init,
207         .clock_enable = usb20otg_clock_enable,
208         .get_status = usb20otg_get_status,
209         .power_enable = usb20otg_power_enable,
210         .dwc_otg_uart_mode = dwc_otg_uart_mode,
211         .bc_detect_cb = rk_battery_charger_detect_cb,
212 };
213
214 #endif
215
216 #ifdef CONFIG_USB20_HOST
217
218 static void usb20host_hw_init(void)
219 {
220         /* usb phy config init
221          * set common_on = 0, in suspend mode, host1 PLL blocks remain powered.
222          * for RK3288, ehci1 and other modules use host1 (DWC_OTG) 480M phy clk.
223          */
224         control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
225
226         /* other haredware init,include:
227          * DRV_VBUS GPIO init */
228         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
229                 if (!gpio_get_value(control_usb->host_gpios->gpio))
230                         gpio_set_value(control_usb->host_gpios->gpio, 1);
231         }
232 }
233
234 static void usb20host_phy_suspend(void *pdata, int suspend)
235 {
236         struct dwc_otg_platform_data *usbpdata = pdata;
237
238         if (suspend) {
239                 /* enable soft control */
240                 control_usb->grf_uoc2_base->CON2 =
241                     (0x01 << 2) | ((0x01 << 2) << 16);
242                 /* enter suspend */
243                 control_usb->grf_uoc2_base->CON3 = 0x2A | (0x3F << 16);
244                 usbpdata->phy_status = 1;
245         } else {
246                 /* exit suspend */
247                 control_usb->grf_uoc2_base->CON2 = ((0x01 << 2) << 16);
248                 usbpdata->phy_status = 0;
249         }
250 }
251
252 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
253 {
254         struct dwc_otg_platform_data *usbpdata = pdata;
255         struct reset_control *rst_host1_h, *rst_host1_p, *rst_host1_c;
256
257         rst_host1_h = devm_reset_control_get(usbpdata->dev, "host1_ahb");
258         rst_host1_p = devm_reset_control_get(usbpdata->dev, "host1_phy");
259         rst_host1_c = devm_reset_control_get(usbpdata->dev, "host1_controller");
260         if (IS_ERR(rst_host1_h) || IS_ERR(rst_host1_p) || IS_ERR(rst_host1_c)) {
261                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
262                 return;
263         }
264
265         reset_control_assert(rst_host1_h);
266         reset_control_assert(rst_host1_p);
267         reset_control_assert(rst_host1_c);
268         udelay(5);
269         reset_control_deassert(rst_host1_h);
270         reset_control_deassert(rst_host1_p);
271         reset_control_deassert(rst_host1_c);
272         mdelay(2);
273 }
274
275 static void usb20host_clock_init(void *pdata)
276 {
277         struct dwc_otg_platform_data *usbpdata = pdata;
278         struct clk *ahbclk, *phyclk, *phyclk_480m;
279
280         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
281         if (IS_ERR(ahbclk)) {
282                 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
283                 return;
284         }
285
286         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
287         if (IS_ERR(phyclk)) {
288                 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
289                 return;
290         }
291
292         phyclk_480m = devm_clk_get(usbpdata->dev, "usbphy_480m");
293         if (IS_ERR(phyclk_480m)) {
294                 dev_err(usbpdata->dev, "Failed to get usbphy_480m\n");
295                 return;
296         }
297
298         usbpdata->phyclk = phyclk;
299         usbpdata->ahbclk = ahbclk;
300         usbpdata->phyclk_480m = phyclk_480m;
301 }
302
303 static void usb20host_clock_enable(void *pdata, int enable)
304 {
305         struct dwc_otg_platform_data *usbpdata = pdata;
306
307         if (enable) {
308                 clk_prepare_enable(usbpdata->ahbclk);
309                 clk_prepare_enable(usbpdata->phyclk);
310         } else {
311                 clk_disable_unprepare(usbpdata->ahbclk);
312                 clk_disable_unprepare(usbpdata->phyclk);
313         }
314 }
315
316 static int usb20host_get_status(int id)
317 {
318         int ret = -1;
319
320         switch (id) {
321         case USB_STATUS_BVABLID:
322                 /* bvalid in grf */
323                 ret = control_usb->grf_soc_status2_rk3288->host1_bvalid;
324                 break;
325         case USB_STATUS_DPDM:
326                 /* dpdm in grf */
327                 ret = control_usb->grf_soc_status2_rk3288->host1_linestate;
328                 break;
329         case USB_STATUS_ID:
330                 /* id in grf */
331                 ret = control_usb->grf_soc_status2_rk3288->host1_iddig;
332                 break;
333         case USB_CHIP_ID:
334                 ret = control_usb->chip_id;
335                 break;
336         case USB_REMOTE_WAKEUP:
337                 ret = control_usb->remote_wakeup;
338                 break;
339         case USB_IRQ_WAKEUP:
340                 ret = control_usb->usb_irq_wakeup;
341                 break;
342         default:
343                 break;
344         }
345
346         return ret;
347 }
348
349 static void usb20host_power_enable(int enable)
350 {
351         if (0 == enable) {
352                 /* disable host_drv power */
353                 if (gpio_is_valid(control_usb->host_gpios->gpio))
354                         gpio_set_value(control_usb->host_gpios->gpio, 0);
355         } else if (1 == enable) {
356                 /* enable host_drv power */
357                 if (gpio_is_valid(control_usb->host_gpios->gpio))
358                         gpio_set_value(control_usb->host_gpios->gpio, 1);
359         }
360 }
361
362 struct dwc_otg_platform_data usb20host_pdata_rk3288 = {
363         .phyclk = NULL,
364         .ahbclk = NULL,
365         .busclk = NULL,
366         .phy_status = 0,
367         .hw_init = usb20host_hw_init,
368         .phy_suspend = usb20host_phy_suspend,
369         .soft_reset = usb20host_soft_reset,
370         .clock_init = usb20host_clock_init,
371         .clock_enable = usb20host_clock_enable,
372         .get_status = usb20host_get_status,
373         .power_enable = usb20host_power_enable,
374 };
375
376 #endif
377
378 #ifdef CONFIG_USB_EHCI1_RK
379 static void rk_ehci1_hw_init(void)
380 {
381         /* usb phy config init
382          * ehci1 phy config init, set ehci1phy_txsrtune */
383         control_usb->grf_uoc3_base->CON0 = ((0xf << 6) << 16) | (0xf << 6);
384
385         /* other haredware init
386          * set common_on = 0, in suspend mode,
387          * otg/host PLL blocks remain powered
388          * for RK3288, use host1 (DWC_OTG) 480M phy clk
389          */
390         control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
391
392         /* change INCR to INCR16 or INCR8(beats less than 16)
393          * or INCR4(beats less than 8) or SINGLE(beats less than 4)
394          */
395         control_usb->grf_uoc4_base->CON0 = 0x00ff00bc;
396 }
397
398 static void rk_ehci1_clock_init(void *pdata)
399 {
400         /* By default, ehci1phy_480m's parent is otg phy 480MHz clk
401          * rk3188 must use host phy 480MHz clk, because if otg bypass
402          * to uart mode, otg phy 480MHz clk will be closed automatically
403          */
404         struct rkehci_platform_data *usbpdata = pdata;
405         struct clk *ahbclk, *phyclk480m_ehci1, *phyclk12m_ehci1;
406
407         phyclk480m_ehci1 = devm_clk_get(usbpdata->dev, "ehci1phy_480m");
408         if (IS_ERR(phyclk480m_ehci1)) {
409                 dev_err(usbpdata->dev, "Failed to get ehci1phy_480m\n");
410                 return;
411         }
412
413         phyclk12m_ehci1 = devm_clk_get(usbpdata->dev, "ehci1phy_12m");
414         if (IS_ERR(phyclk12m_ehci1)) {
415                 dev_err(usbpdata->dev, "Failed to get ehci1phy_12m\n");
416                 return;
417         }
418
419         ahbclk = devm_clk_get(usbpdata->dev, "hclk_ehci1");
420         if (IS_ERR(ahbclk)) {
421                 dev_err(usbpdata->dev, "Failed to get hclk_ehci1\n");
422                 return;
423         }
424
425         usbpdata->hclk_ehci = ahbclk;
426         usbpdata->ehci_phy_480m = phyclk480m_ehci1;
427         usbpdata->ehci_phy_12m = phyclk12m_ehci1;
428 }
429
430 static void rk_ehci1_clock_enable(void *pdata, int enable)
431 {
432         struct rkehci_platform_data *usbpdata = pdata;
433
434         if (enable == usbpdata->clk_status)
435                 return;
436         if (enable) {
437                 clk_prepare_enable(usbpdata->hclk_ehci);
438                 clk_prepare_enable(usbpdata->ehci_phy_480m);
439                 clk_prepare_enable(usbpdata->ehci_phy_12m);
440                 usbpdata->clk_status = 1;
441         } else {
442                 clk_disable_unprepare(usbpdata->hclk_ehci);
443                 clk_disable_unprepare(usbpdata->ehci_phy_480m);
444                 clk_disable_unprepare(usbpdata->ehci_phy_12m);
445                 usbpdata->clk_status = 0;
446         }
447 }
448
449 static void rk_ehci1_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
450 {
451         struct rkehci_platform_data *usbpdata = pdata;
452         struct reset_control *rst_ehci1_h, *rst_ehci1_a, *rst_ehci1_p;
453
454         rst_ehci1_h = devm_reset_control_get(usbpdata->dev, "ehci1_ahb");
455         rst_ehci1_a = devm_reset_control_get(usbpdata->dev, "ehci1_aux");
456         rst_ehci1_p = devm_reset_control_get(usbpdata->dev, "ehci1_phy");
457
458         reset_control_assert(rst_ehci1_h);
459         reset_control_assert(rst_ehci1_a);
460         reset_control_assert(rst_ehci1_p);
461         udelay(5);
462         reset_control_deassert(rst_ehci1_h);
463         reset_control_deassert(rst_ehci1_a);
464         reset_control_deassert(rst_ehci1_p);
465         mdelay(2);
466
467         /* EHCI1 per-port reset */
468         control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (1 << 10);
469         udelay(2);
470         control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (0 << 10);
471         udelay(2);
472 }
473
474 struct rkehci_platform_data rkehci1_pdata_rk3288 = {
475         .hclk_ehci = NULL,
476         .ehci_phy_12m = NULL,
477         .ehci_phy_480m = NULL,
478         .clk_status = -1,
479         .hw_init = rk_ehci1_hw_init,
480         .clock_init = rk_ehci1_clock_init,
481         .clock_enable = rk_ehci1_clock_enable,
482         .soft_reset = rk_ehci1_soft_reset,
483 };
484 #endif
485
486 #ifdef CONFIG_USB_EHCI_RK
487 static void rk_ehci_hw_init(void)
488 {
489         /* usb phy config init */
490
491         /* DRV_VBUS GPIO init */
492         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
493                 if (!gpio_get_value(control_usb->host_gpios->gpio))
494                         gpio_set_value(control_usb->host_gpios->gpio, 1);
495         }
496 }
497
498 static void rk_ehci_phy_suspend(void *pdata, int suspend)
499 {
500         struct rkehci_platform_data *usbpdata = pdata;
501
502         if (suspend) {
503                 /* enable soft control */
504                 control_usb->grf_uoc1_base->CON2 =
505                     (0x01 << 2) | ((0x01 << 2) << 16);
506                 /* enter suspend */
507                 control_usb->grf_uoc1_base->CON3 = 0x2A | (0x3F << 16);
508                 usbpdata->phy_status = 1;
509         } else {
510                 /* exit suspend */
511                 control_usb->grf_uoc1_base->CON2 = ((0x01 << 2) << 16);
512                 usbpdata->phy_status = 0;
513         }
514 }
515
516 static void rk_ehci_clock_init(void *pdata)
517 {
518         struct rkehci_platform_data *usbpdata = pdata;
519         struct clk *ahbclk, *phyclk;
520
521         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb2");
522         if (IS_ERR(ahbclk)) {
523                 dev_err(usbpdata->dev, "Failed to get hclk_usb2\n");
524                 return;
525         }
526
527         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy2");
528         if (IS_ERR(phyclk)) {
529                 dev_err(usbpdata->dev, "Failed to get clk_usbphy2\n");
530                 return;
531         }
532
533         usbpdata->phyclk = phyclk;
534         usbpdata->ahbclk = ahbclk;
535 }
536
537 static void rk_ehci_clock_enable(void *pdata, int enable)
538 {
539         struct rkehci_platform_data *usbpdata = pdata;
540
541         if (enable == usbpdata->clk_status)
542                 return;
543         if (enable) {
544                 clk_prepare_enable(usbpdata->ahbclk);
545                 clk_prepare_enable(usbpdata->phyclk);
546                 usbpdata->clk_status = 1;
547         } else {
548                 clk_disable_unprepare(usbpdata->ahbclk);
549                 clk_disable_unprepare(usbpdata->phyclk);
550                 usbpdata->clk_status = 0;
551         }
552 }
553
554 static void rk_ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
555 {
556         struct rkehci_platform_data *usbpdata = pdata;
557         struct reset_control *rst_host0_h, *rst_host0_p,
558                              *rst_host0_c , *rst_host0;
559
560         rst_host0_h = devm_reset_control_get(usbpdata->dev, "ehci_ahb");
561         rst_host0_p = devm_reset_control_get(usbpdata->dev, "ehci_phy");
562         rst_host0_c = devm_reset_control_get(usbpdata->dev, "ehci_controller");
563         rst_host0 = devm_reset_control_get(usbpdata->dev, "ehci");
564         if (IS_ERR(rst_host0_h) || IS_ERR(rst_host0_p) ||
565             IS_ERR(rst_host0_c) || IS_ERR(rst_host0)) {
566                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
567                 return;
568         }
569
570         reset_control_assert(rst_host0_h);
571         reset_control_assert(rst_host0_p);
572         reset_control_assert(rst_host0_c);
573         reset_control_assert(rst_host0);
574         udelay(5);
575         reset_control_deassert(rst_host0_h);
576         reset_control_deassert(rst_host0_p);
577         reset_control_deassert(rst_host0_c);
578         reset_control_deassert(rst_host0);
579         mdelay(2);
580 }
581
582 static int rk_ehci_get_status(int id)
583 {
584         int ret = -1;
585
586         switch (id) {
587         case USB_STATUS_DPDM:
588                 /* dpdm in grf */
589                 ret = control_usb->grf_soc_status2_rk3288->host0_linestate;
590                 break;
591         case USB_CHIP_ID:
592                 ret = control_usb->chip_id;
593                 break;
594         case USB_REMOTE_WAKEUP:
595                 ret = control_usb->remote_wakeup;
596                 break;
597         case USB_IRQ_WAKEUP:
598                 ret = control_usb->usb_irq_wakeup;
599                 break;
600         default:
601                 break;
602         }
603
604         return ret;
605 }
606
607 struct rkehci_platform_data rkehci_pdata_rk3288 = {
608         .phyclk = NULL,
609         .ahbclk = NULL,
610         .clk_status = -1,
611         .phy_status = 0,
612         .hw_init = rk_ehci_hw_init,
613         .phy_suspend = rk_ehci_phy_suspend,
614         .clock_init = rk_ehci_clock_init,
615         .clock_enable = rk_ehci_clock_enable,
616         .soft_reset = rk_ehci_soft_reset,
617         .get_status = rk_ehci_get_status,
618 };
619 #endif
620
621 #ifdef CONFIG_USB_OHCI_HCD_RK
622 static void rk_ohci_hw_init(void)
623 {
624         /* usb phy config init */
625
626         /* DRV_VBUS GPIO init */
627         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
628                 if (!gpio_get_value(control_usb->host_gpios->gpio))
629                         gpio_set_value(control_usb->host_gpios->gpio, 1);
630         }
631 }
632
633 static void rk_ohci_clock_init(void *pdata)
634 {
635         struct rkehci_platform_data *usbpdata = pdata;
636         struct clk *ahbclk, *phyclk;
637
638         ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb3");
639         if (IS_ERR(ahbclk)) {
640                 dev_err(usbpdata->dev, "Failed to get hclk_usb3\n");
641                 return;
642         }
643
644         phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy3");
645         if (IS_ERR(phyclk)) {
646                 dev_err(usbpdata->dev, "Failed to get clk_usbphy3\n");
647                 return;
648         }
649
650         usbpdata->phyclk = phyclk;
651         usbpdata->ahbclk = ahbclk;
652 }
653
654 static void rk_ohci_clock_enable(void *pdata, int enable)
655 {
656         struct rkehci_platform_data *usbpdata = pdata;
657
658         if (enable == usbpdata->clk_status)
659                 return;
660         if (enable) {
661                 clk_prepare_enable(usbpdata->ahbclk);
662                 clk_prepare_enable(usbpdata->phyclk);
663                 usbpdata->clk_status = 1;
664         } else {
665                 clk_disable_unprepare(usbpdata->ahbclk);
666                 clk_disable_unprepare(usbpdata->phyclk);
667                 usbpdata->clk_status = 0;
668         }
669 }
670
671 static void rk_ohci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
672 {
673 }
674
675 struct rkehci_platform_data rkohci_pdata_rk3288 = {
676         .phyclk = NULL,
677         .ahbclk = NULL,
678         .clk_status = -1,
679         .hw_init = rk_ohci_hw_init,
680         .clock_init = rk_ohci_clock_init,
681         .clock_enable = rk_ohci_clock_enable,
682         .soft_reset = rk_ohci_soft_reset,
683 };
684 #endif
685
686 /*********************************************************************
687                         rk3288 usb detections
688 *********************************************************************/
689
690 #define WAKE_LOCK_TIMEOUT (HZ * 10)
691 static inline void do_wakeup(struct work_struct *work)
692 {
693         /* wake up the system */
694         rk_send_wakeup_key();
695 }
696
697 static void usb_battery_charger_detect_work(struct work_struct *work)
698 {
699         rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
700 }
701
702 /********** handler for bvalid irq **********/
703 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
704 {
705         /* clear irq */
706         control_usb->grf_uoc0_base->CON4 = (0x0008 | (0x0008 << 16));
707
708         /* usb otg dp/dm switch to usb phy */
709         dwc_otg_uart_mode(NULL, PHY_USB_MODE);
710
711         if (control_usb->usb_irq_wakeup) {
712                 wake_lock_timeout(&control_usb->usb_wakelock,
713                                   WAKE_LOCK_TIMEOUT);
714                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
715                                       HZ / 10);
716         }
717         schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
718
719         return IRQ_HANDLED;
720 }
721
722 /***** handler for otg id rise and fall edge *****/
723 static irqreturn_t id_irq_handler(int irq, void *dev_id)
724 {
725         unsigned int uoc_con;
726
727         /* clear irq */
728         uoc_con = control_usb->grf_uoc0_base->CON4;
729
730         /* id rise */
731         if (uoc_con & (1 << 5)) {
732                 /* clear id rise irq pandding */
733                 control_usb->grf_uoc0_base->CON4 = ((1 << 5) | (1 << 21));
734         }
735
736         /* id fall */
737         if (uoc_con & (1 << 7)) {
738                 /* usb otg dp/dm switch to usb phy */
739                 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
740                 /* clear id fall irq pandding */
741                 control_usb->grf_uoc0_base->CON4 = ((1 << 7) | (1 << 23));
742         }
743
744         if (control_usb->usb_irq_wakeup) {
745                 wake_lock_timeout(&control_usb->usb_wakelock,
746                                   WAKE_LOCK_TIMEOUT);
747                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
748                                       HZ / 10);
749         }
750
751         return IRQ_HANDLED;
752 }
753
754 #ifdef USB_LINESTATE_IRQ
755 /***** handler for usb line status change *****/
756
757 static irqreturn_t line_irq_handler(int irq, void *dev_id)
758 {
759         /* clear irq */
760
761         if (control_usb->grf_uoc0_base->CON0 & 1 << 15)
762                 control_usb->grf_uoc0_base->CON0 = (1 << 15 | 1 << 31);
763
764         if (control_usb->grf_uoc1_base->CON0 & 1 << 15)
765                 control_usb->grf_uoc1_base->CON0 = (1 << 15 | 1 << 31);
766
767         if (control_usb->grf_uoc2_base->CON0 & 1 << 15)
768                 control_usb->grf_uoc2_base->CON0 = (1 << 15 | 1 << 31);
769
770         if (control_usb->usb_irq_wakeup) {
771                 wake_lock_timeout(&control_usb->usb_wakelock,
772                                   WAKE_LOCK_TIMEOUT);
773                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
774                                       HZ / 10);
775         }
776
777         return IRQ_HANDLED;
778 }
779 #endif
780
781 /************* register usb detection irqs **************/
782 static int otg_irq_detect_init(struct platform_device *pdev)
783 {
784         int ret = 0;
785         int irq = 0;
786         if (control_usb->usb_irq_wakeup) {
787                 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
788                                "usb_detect");
789                 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
790         }
791
792         /*register otg_bvalid irq */
793         irq = platform_get_irq_byname(pdev, "otg_bvalid");
794         if ((irq > 0) && control_usb->usb_irq_wakeup) {
795                 ret = request_irq(irq, bvalid_irq_handler,
796                                   0, "otg_bvalid", NULL);
797                 if (ret < 0) {
798                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
799                 } else {
800                         /* enable bvalid irq  */
801                         control_usb->grf_uoc0_base->CON4 = 0x000c000c;
802                 }
803         }
804
805         /*register otg_id irq */
806         irq = platform_get_irq_byname(pdev, "otg_id");
807         if ((irq > 0) && control_usb->usb_irq_wakeup) {
808                 ret = request_irq(irq, id_irq_handler, 0, "otg_id", NULL);
809                 if (ret < 0) {
810                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
811                 } else {
812                         /* enable otg_id irq */
813                         control_usb->grf_uoc0_base->CON4 = 0x00f000f0;
814                 }
815         }
816 #if 0
817         /*register otg_linestate irq */
818         irq = platform_get_irq_byname(pdev, "otg_linestate");
819         if (irq > 0) {
820                 ret =
821                     request_irq(irq, line_irq_handler, 0, "otg_linestate",
822                                 NULL);
823                 if (ret < 0) {
824                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
825                         return ret;
826                 } else {
827                         control_usb->grf_uoc0_base->CON0 = 0xc000c000;
828                         if (control_usb->usb_irq_wakeup)
829                                 enable_irq_wake(irq);
830                 }
831         }
832
833         /*register host0_linestate irq */
834         irq = platform_get_irq_byname(pdev, "host0_linestate");
835         if (irq > 0) {
836                 ret =
837                     request_irq(irq, line_irq_handler, 0, "host0_linestate",
838                                 NULL);
839                 if (ret < 0) {
840                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
841                         return ret;
842                 } else {
843                         control_usb->grf_uoc1_base->CON0 = 0xc000c000;
844                         if (control_usb->usb_irq_wakeup)
845                                 enable_irq_wake(irq);
846                 }
847         }
848
849         /*register host1_linestate irq */
850         irq = platform_get_irq_byname(pdev, "host1_linestate");
851         if (irq > 0) {
852                 ret =
853                     request_irq(irq, line_irq_handler, 0, "host1_linestate",
854                                 NULL);
855                 if (ret < 0) {
856                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
857                         return ret;
858                 } else {
859                         control_usb->grf_uoc2_base->CON0 = 0xc000c000;
860                         if (control_usb->usb_irq_wakeup)
861                                 enable_irq_wake(irq);
862                 }
863         }
864 #endif
865         return ret;
866 }
867
868 /********** end of rk3288 usb detections **********/
869
870 static int usb_grf_ioremap(struct platform_device *pdev)
871 {
872         int ret = 0;
873         struct resource *res;
874         void *grf_soc_status1;
875         void *grf_soc_status2;
876         void *grf_soc_status19;
877         void *grf_soc_status21;
878         void *grf_uoc0_base;
879         void *grf_uoc1_base;
880         void *grf_uoc2_base;
881         void *grf_uoc3_base;
882         void *grf_uoc4_base;
883
884         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
885                                            "GRF_SOC_STATUS1");
886         grf_soc_status1 = devm_ioremap_resource(&pdev->dev, res);
887         if (IS_ERR(grf_soc_status1)) {
888                 ret = PTR_ERR(grf_soc_status1);
889                 return ret;
890         }
891         control_usb->grf_soc_status1_rk3288 =
892             (pGRF_SOC_STATUS1_RK3288) grf_soc_status1;
893
894         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
895                                            "GRF_SOC_STATUS2");
896         grf_soc_status2 = devm_ioremap_resource(&pdev->dev, res);
897         if (IS_ERR(grf_soc_status2)) {
898                 ret = PTR_ERR(grf_soc_status2);
899                 return ret;
900         }
901         control_usb->grf_soc_status2_rk3288 =
902             (pGRF_SOC_STATUS2_RK3288) grf_soc_status2;
903
904         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
905                                            "GRF_SOC_STATUS19");
906         grf_soc_status19 = devm_ioremap_resource(&pdev->dev, res);
907         if (IS_ERR(grf_soc_status19)) {
908                 ret = PTR_ERR(grf_soc_status19);
909                 return ret;
910         }
911         control_usb->grf_soc_status19_rk3288 =
912             (pGRF_SOC_STATUS19_RK3288) grf_soc_status19;
913
914         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
915                                            "GRF_SOC_STATUS21");
916         grf_soc_status21 = devm_ioremap_resource(&pdev->dev, res);
917         if (IS_ERR(grf_soc_status21)) {
918                 ret = PTR_ERR(grf_soc_status21);
919                 return ret;
920         }
921         control_usb->grf_soc_status21_rk3288 =
922             (pGRF_SOC_STATUS21_RK3288) grf_soc_status21;
923
924         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
925                                            "GRF_UOC0_BASE");
926         grf_uoc0_base = devm_ioremap_resource(&pdev->dev, res);
927         if (IS_ERR(grf_uoc0_base)) {
928                 ret = PTR_ERR(grf_uoc0_base);
929                 return ret;
930         }
931         control_usb->grf_uoc0_base = (pGRF_UOC0_REG) grf_uoc0_base;
932
933         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
934                                            "GRF_UOC1_BASE");
935         grf_uoc1_base = devm_ioremap_resource(&pdev->dev, res);
936         if (IS_ERR(grf_uoc1_base)) {
937                 ret = PTR_ERR(grf_uoc1_base);
938                 return ret;
939         }
940         control_usb->grf_uoc1_base = (pGRF_UOC1_REG) grf_uoc1_base;
941
942         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
943                                            "GRF_UOC2_BASE");
944         grf_uoc2_base = devm_ioremap_resource(&pdev->dev, res);
945         if (IS_ERR(grf_uoc2_base)) {
946                 ret = PTR_ERR(grf_uoc2_base);
947                 return ret;
948         }
949         control_usb->grf_uoc2_base = (pGRF_UOC2_REG) grf_uoc2_base;
950
951         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
952                                            "GRF_UOC3_BASE");
953         grf_uoc3_base = devm_ioremap_resource(&pdev->dev, res);
954         if (IS_ERR(grf_uoc3_base)) {
955                 ret = PTR_ERR(grf_uoc3_base);
956                 return ret;
957         }
958         control_usb->grf_uoc3_base = (pGRF_UOC3_REG) grf_uoc3_base;
959
960         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
961                                            "GRF_UOC4_BASE");
962         grf_uoc4_base = devm_ioremap_resource(&pdev->dev, res);
963         if (IS_ERR(grf_uoc4_base)) {
964                 ret = PTR_ERR(grf_uoc4_base);
965                 return ret;
966         }
967         control_usb->grf_uoc4_base = (pGRF_UOC4_REG) grf_uoc4_base;
968
969         return ret;
970 }
971
972 static int rk_usb_control_probe(struct platform_device *pdev)
973 {
974         int gpio, err;
975         struct device_node *np = pdev->dev.of_node;
976         int ret = 0;
977
978         control_usb =
979             devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
980         if (!control_usb) {
981                 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
982                 ret = -ENOMEM;
983                 goto out;
984         }
985
986         control_usb->chip_id = RK3288_USB_CTLR;
987         control_usb->remote_wakeup = of_property_read_bool(np,
988                                                            "rockchip,remote_wakeup");
989         control_usb->usb_irq_wakeup = of_property_read_bool(np,
990                                                             "rockchip,usb_irq_wakeup");
991
992         INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
993                           usb_battery_charger_detect_work);
994
995         control_usb->host_gpios =
996             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
997         if (!control_usb->host_gpios) {
998                 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
999                 ret = -ENOMEM;
1000                 goto out;
1001         }
1002
1003         gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
1004         control_usb->host_gpios->gpio = gpio;
1005
1006         if (!gpio_is_valid(gpio)) {
1007                 dev_warn(&pdev->dev, "host_drv_gpio is not specified or invalid\n");
1008         } else {
1009                 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
1010                 if (err) {
1011                         dev_err(&pdev->dev,
1012                                 "failed to request GPIO%d for host_drv\n",
1013                                 gpio);
1014                         ret = err;
1015                         goto out;
1016                 }
1017                 gpio_direction_output(control_usb->host_gpios->gpio, 1);
1018         }
1019
1020         control_usb->otg_gpios =
1021             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
1022         if (!control_usb->otg_gpios) {
1023                 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
1024                 ret = -ENOMEM;
1025                 goto out;
1026         }
1027
1028         gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
1029         control_usb->otg_gpios->gpio = gpio;
1030
1031         if (!gpio_is_valid(gpio)) {
1032                 dev_warn(&pdev->dev, "otg_drv_gpio is not specified or invalid\n");
1033         } else {
1034                 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
1035                 if (err) {
1036                         dev_err(&pdev->dev,
1037                                 "failed to request GPIO%d for otg_drv\n", gpio);
1038                         ret = err;
1039                         goto out;
1040                 }
1041                 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
1042         }
1043
1044 out:
1045         return ret;
1046 }
1047
1048 #ifdef CONFIG_OF
1049 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
1050         {
1051          .compatible = "rockchip,rk3288-dwc-control-usb",
1052          },
1053         {},
1054 };
1055 #endif
1056
1057 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
1058 {
1059         int ret = 0;
1060
1061         ret = rk_usb_control_probe(pdev);
1062         if (ret)
1063                 return ret;
1064
1065         control_usb->hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
1066         if (IS_ERR(control_usb->hclk_usb_peri)) {
1067                 dev_info(&pdev->dev, "no hclk_usb_peri specified\n");
1068                 control_usb->hclk_usb_peri = NULL;
1069         }
1070
1071         clk_prepare_enable(control_usb->hclk_usb_peri);
1072
1073         ret = usb_grf_ioremap(pdev);
1074         if (ret) {
1075                 dev_err(&pdev->dev, "Failed to ioremap usb grf\n");
1076                 goto err;
1077         }
1078 #ifdef CONFIG_USB20_OTG
1079         if (usb20otg_get_status(USB_STATUS_BVABLID))
1080                 schedule_delayed_work(&control_usb->usb_charger_det_work,
1081                                       HZ / 10);
1082 #endif
1083
1084         ret = otg_irq_detect_init(pdev);
1085         if (ret < 0)
1086                 goto err;
1087
1088         return 0;
1089
1090 err:
1091         clk_disable_unprepare(control_usb->hclk_usb_peri);
1092         return ret;
1093 }
1094
1095 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
1096 {
1097         clk_disable_unprepare(control_usb->hclk_usb_peri);
1098         return 0;
1099 }
1100
1101 static struct platform_driver dwc_otg_control_usb_driver = {
1102         .probe = dwc_otg_control_usb_probe,
1103         .remove = dwc_otg_control_usb_remove,
1104         .driver = {
1105                    .name = "rk3288-dwc-control-usb",
1106                    .owner = THIS_MODULE,
1107                    .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
1108                    },
1109 };
1110
1111 static int __init dwc_otg_control_usb_init(void)
1112 {
1113         int retval = 0;
1114
1115         retval = platform_driver_register(&dwc_otg_control_usb_driver);
1116
1117         if (retval < 0) {
1118                 printk(KERN_ERR "%s retval=%d\n", __func__, retval);
1119                 return retval;
1120         }
1121         return retval;
1122 }
1123
1124 subsys_initcall(dwc_otg_control_usb_init);
1125
1126 static void __exit dwc_otg_control_usb_exit(void)
1127 {
1128         platform_driver_unregister(&dwc_otg_control_usb_driver);
1129 }
1130
1131 module_exit(dwc_otg_control_usb_exit);
1132 MODULE_ALIAS("platform: dwc_control_usb");
1133 MODULE_AUTHOR("RockChip Inc.");
1134 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
1135 MODULE_LICENSE("GPL v2");
1136 #endif