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