3 #include "usbdev_grf_regs.h"
4 #include "dwc_otg_regs.h"
5 static struct dwc_otg_control_usb *control_usb;
7 int is_rk3288_usb(void)
12 return control_usb->chip_id == RK3288_USB_CTLR ? true : false;
15 #ifdef CONFIG_USB20_OTG
16 static void usb20otg_hw_init(void)
18 #ifndef CONFIG_USB20_HOST
19 /* enable soft control */
20 control_usb->grf_uoc2_base->CON2 = (0x01 << 2) | ((0x01 << 2) << 16);
22 control_usb->grf_uoc2_base->CON3 = 0x2A | (0x3F << 16);
24 /* usb phy config init
25 * usb phy enter usb mode */
26 control_usb->grf_uoc0_base->CON3 = (0x00c0 << 16);
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);
36 static void usb20otg_phy_suspend(void *pdata, int suspend)
38 struct dwc_otg_platform_data *usbpdata = pdata;
41 /* enable soft control */
42 control_usb->grf_uoc0_base->CON2 =
43 (0x01 << 2) | ((0x01 << 2) << 16);
45 control_usb->grf_uoc0_base->CON3 = 0x2A | (0x3F << 16);
46 usbpdata->phy_status = 1;
49 control_usb->grf_uoc0_base->CON2 = ((0x01 << 2) << 16);
50 usbpdata->phy_status = 0;
54 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
56 struct dwc_otg_platform_data *usbpdata = pdata;
57 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
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");
67 reset_control_assert(rst_otg_h);
68 reset_control_assert(rst_otg_p);
69 reset_control_assert(rst_otg_c);
71 reset_control_deassert(rst_otg_h);
72 reset_control_deassert(rst_otg_p);
73 reset_control_deassert(rst_otg_c);
77 static void usb20otg_clock_init(void *pdata)
79 struct dwc_otg_platform_data *usbpdata = pdata;
80 struct clk *ahbclk, *phyclk;
82 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
84 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
88 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
90 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
94 usbpdata->phyclk = phyclk;
95 usbpdata->ahbclk = ahbclk;
98 static void usb20otg_clock_enable(void *pdata, int enable)
100 struct dwc_otg_platform_data *usbpdata = pdata;
103 clk_prepare_enable(usbpdata->ahbclk);
104 clk_prepare_enable(usbpdata->phyclk);
106 clk_disable_unprepare(usbpdata->ahbclk);
107 clk_disable_unprepare(usbpdata->phyclk);
111 static int usb20otg_get_status(int id)
116 case USB_STATUS_BVABLID:
118 ret = control_usb->grf_soc_status2_rk3288->otg_bvalid;
120 case USB_STATUS_DPDM:
122 ret = control_usb->grf_soc_status2_rk3288->otg_linestate;
126 ret = control_usb->grf_soc_status2_rk3288->otg_iddig;
129 ret = control_usb->chip_id;
131 case USB_REMOTE_WAKEUP:
132 ret = control_usb->remote_wakeup;
135 ret = control_usb->usb_irq_wakeup;
144 #ifdef CONFIG_RK_USB_UART
146 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
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"
152 static bool dwc_otg_uart_enabled(void)
154 struct device_node *np;
156 np = of_find_node_by_name(NULL, "usb_uart");
157 if (np && of_device_is_available(np))
163 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
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));
169 } else if (0 == enter_usb_uart_mode) {
171 control_usb->grf_uoc0_base->CON3 = (0x00c0 << 16);
175 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
180 static void usb20otg_power_enable(int 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);
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);
193 if (!usb20otg_get_status(USB_STATUS_BVABLID))
194 rk_battery_charger_detect_cb(USB_OTG_POWER_ON);
198 struct dwc_otg_platform_data usb20otg_pdata_rk3288 = {
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,
216 #ifdef CONFIG_USB20_HOST
218 static void usb20host_hw_init(void)
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.
224 control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
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);
234 static void usb20host_phy_suspend(void *pdata, int suspend)
236 struct dwc_otg_platform_data *usbpdata = pdata;
239 /* enable soft control */
240 control_usb->grf_uoc2_base->CON2 =
241 (0x01 << 2) | ((0x01 << 2) << 16);
243 control_usb->grf_uoc2_base->CON3 = 0x2A | (0x3F << 16);
244 usbpdata->phy_status = 1;
247 control_usb->grf_uoc2_base->CON2 = ((0x01 << 2) << 16);
248 usbpdata->phy_status = 0;
252 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
254 struct dwc_otg_platform_data *usbpdata = pdata;
255 struct reset_control *rst_host1_h, *rst_host1_p, *rst_host1_c;
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");
265 reset_control_assert(rst_host1_h);
266 reset_control_assert(rst_host1_p);
267 reset_control_assert(rst_host1_c);
269 reset_control_deassert(rst_host1_h);
270 reset_control_deassert(rst_host1_p);
271 reset_control_deassert(rst_host1_c);
275 static void usb20host_clock_init(void *pdata)
277 struct dwc_otg_platform_data *usbpdata = pdata;
278 struct clk *ahbclk, *phyclk, *phyclk_480m;
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");
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");
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");
298 usbpdata->phyclk = phyclk;
299 usbpdata->ahbclk = ahbclk;
300 usbpdata->phyclk_480m = phyclk_480m;
303 static void usb20host_clock_enable(void *pdata, int enable)
305 struct dwc_otg_platform_data *usbpdata = pdata;
308 clk_prepare_enable(usbpdata->ahbclk);
309 clk_prepare_enable(usbpdata->phyclk);
311 clk_disable_unprepare(usbpdata->ahbclk);
312 clk_disable_unprepare(usbpdata->phyclk);
316 static int usb20host_get_status(int id)
321 case USB_STATUS_BVABLID:
323 ret = control_usb->grf_soc_status2_rk3288->host1_bvalid;
325 case USB_STATUS_DPDM:
327 ret = control_usb->grf_soc_status2_rk3288->host1_linestate;
331 ret = control_usb->grf_soc_status2_rk3288->host1_iddig;
334 ret = control_usb->chip_id;
336 case USB_REMOTE_WAKEUP:
337 ret = control_usb->remote_wakeup;
340 ret = control_usb->usb_irq_wakeup;
349 static void usb20host_power_enable(int 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);
362 struct dwc_otg_platform_data usb20host_pdata_rk3288 = {
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,
378 #ifdef CONFIG_USB_EHCI1_RK
379 static void rk_ehci1_hw_init(void)
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);
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
390 control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
392 /* change INCR to INCR16 or INCR8(beats less than 16)
393 * or INCR4(beats less than 8) or SINGLE(beats less than 4)
395 control_usb->grf_uoc4_base->CON0 = 0x00ff00bc;
398 static void rk_ehci1_clock_init(void *pdata)
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
404 struct rkehci_platform_data *usbpdata = pdata;
405 struct clk *ahbclk, *phyclk480m_ehci1, *phyclk12m_ehci1;
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");
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");
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");
425 usbpdata->hclk_ehci = ahbclk;
426 usbpdata->ehci_phy_480m = phyclk480m_ehci1;
427 usbpdata->ehci_phy_12m = phyclk12m_ehci1;
430 static void rk_ehci1_clock_enable(void *pdata, int enable)
432 struct rkehci_platform_data *usbpdata = pdata;
434 if (enable == usbpdata->clk_status)
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;
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;
449 static void rk_ehci1_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
451 struct rkehci_platform_data *usbpdata = pdata;
452 struct reset_control *rst_ehci1_h, *rst_ehci1_a, *rst_ehci1_p;
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");
458 reset_control_assert(rst_ehci1_h);
459 reset_control_assert(rst_ehci1_a);
460 reset_control_assert(rst_ehci1_p);
462 reset_control_deassert(rst_ehci1_h);
463 reset_control_deassert(rst_ehci1_a);
464 reset_control_deassert(rst_ehci1_p);
467 /* EHCI1 per-port reset */
468 control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (1 << 10);
470 control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (0 << 10);
474 struct rkehci_platform_data rkehci1_pdata_rk3288 = {
476 .ehci_phy_12m = NULL,
477 .ehci_phy_480m = NULL,
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,
486 #ifdef CONFIG_USB_EHCI_RK
487 static void rk_ehci_hw_init(void)
489 /* usb phy config init */
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);
498 static void rk_ehci_phy_suspend(void *pdata, int suspend)
500 struct rkehci_platform_data *usbpdata = pdata;
503 /* enable soft control */
504 control_usb->grf_uoc1_base->CON2 =
505 (0x01 << 2) | ((0x01 << 2) << 16);
507 control_usb->grf_uoc1_base->CON3 = 0x2A | (0x3F << 16);
508 usbpdata->phy_status = 1;
511 control_usb->grf_uoc1_base->CON2 = ((0x01 << 2) << 16);
512 usbpdata->phy_status = 0;
516 static void rk_ehci_clock_init(void *pdata)
518 struct rkehci_platform_data *usbpdata = pdata;
519 struct clk *ahbclk, *phyclk;
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");
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");
533 usbpdata->phyclk = phyclk;
534 usbpdata->ahbclk = ahbclk;
537 static void rk_ehci_clock_enable(void *pdata, int enable)
539 struct rkehci_platform_data *usbpdata = pdata;
541 if (enable == usbpdata->clk_status)
544 clk_prepare_enable(usbpdata->ahbclk);
545 clk_prepare_enable(usbpdata->phyclk);
546 usbpdata->clk_status = 1;
548 clk_disable_unprepare(usbpdata->ahbclk);
549 clk_disable_unprepare(usbpdata->phyclk);
550 usbpdata->clk_status = 0;
554 static void rk_ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
556 struct rkehci_platform_data *usbpdata = pdata;
557 struct reset_control *rst_host0_h, *rst_host0_p,
558 *rst_host0_c , *rst_host0;
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");
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);
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);
582 static int rk_ehci_get_status(int id)
587 case USB_STATUS_DPDM:
589 ret = control_usb->grf_soc_status2_rk3288->host0_linestate;
592 ret = control_usb->chip_id;
594 case USB_REMOTE_WAKEUP:
595 ret = control_usb->remote_wakeup;
598 ret = control_usb->usb_irq_wakeup;
607 struct rkehci_platform_data rkehci_pdata_rk3288 = {
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,
621 #ifdef CONFIG_USB_OHCI_HCD_RK
622 static void rk_ohci_hw_init(void)
624 /* usb phy config init */
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);
633 static void rk_ohci_clock_init(void *pdata)
635 struct rkehci_platform_data *usbpdata = pdata;
636 struct clk *ahbclk, *phyclk;
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");
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");
650 usbpdata->phyclk = phyclk;
651 usbpdata->ahbclk = ahbclk;
654 static void rk_ohci_clock_enable(void *pdata, int enable)
656 struct rkehci_platform_data *usbpdata = pdata;
658 if (enable == usbpdata->clk_status)
661 clk_prepare_enable(usbpdata->ahbclk);
662 clk_prepare_enable(usbpdata->phyclk);
663 usbpdata->clk_status = 1;
665 clk_disable_unprepare(usbpdata->ahbclk);
666 clk_disable_unprepare(usbpdata->phyclk);
667 usbpdata->clk_status = 0;
671 static void rk_ohci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
675 struct rkehci_platform_data rkohci_pdata_rk3288 = {
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,
686 /*********************************************************************
687 rk3288 usb detections
688 *********************************************************************/
690 #define WAKE_LOCK_TIMEOUT (HZ * 10)
691 static inline void do_wakeup(struct work_struct *work)
693 /* wake up the system */
694 rk_send_wakeup_key();
697 static void usb_battery_charger_detect_work(struct work_struct *work)
699 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
702 /********** handler for bvalid irq **********/
703 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
706 control_usb->grf_uoc0_base->CON4 = (0x0008 | (0x0008 << 16));
708 /* usb otg dp/dm switch to usb phy */
709 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
711 if (control_usb->usb_irq_wakeup) {
712 wake_lock_timeout(&control_usb->usb_wakelock,
714 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
717 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
722 /***** handler for otg id rise and fall edge *****/
723 static irqreturn_t id_irq_handler(int irq, void *dev_id)
725 unsigned int uoc_con;
728 uoc_con = control_usb->grf_uoc0_base->CON4;
731 if (uoc_con & (1 << 5)) {
732 /* clear id rise irq pandding */
733 control_usb->grf_uoc0_base->CON4 = ((1 << 5) | (1 << 21));
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));
744 if (control_usb->usb_irq_wakeup) {
745 wake_lock_timeout(&control_usb->usb_wakelock,
747 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
754 #ifdef USB_LINESTATE_IRQ
755 /***** handler for usb line status change *****/
757 static irqreturn_t line_irq_handler(int irq, void *dev_id)
761 if (control_usb->grf_uoc0_base->CON0 & 1 << 15)
762 control_usb->grf_uoc0_base->CON0 = (1 << 15 | 1 << 31);
764 if (control_usb->grf_uoc1_base->CON0 & 1 << 15)
765 control_usb->grf_uoc1_base->CON0 = (1 << 15 | 1 << 31);
767 if (control_usb->grf_uoc2_base->CON0 & 1 << 15)
768 control_usb->grf_uoc2_base->CON0 = (1 << 15 | 1 << 31);
770 if (control_usb->usb_irq_wakeup) {
771 wake_lock_timeout(&control_usb->usb_wakelock,
773 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
781 /************* register usb detection irqs **************/
782 static int otg_irq_detect_init(struct platform_device *pdev)
786 if (control_usb->usb_irq_wakeup) {
787 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
789 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
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);
798 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
800 /* enable bvalid irq */
801 control_usb->grf_uoc0_base->CON4 = 0x000c000c;
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);
810 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
812 /* enable otg_id irq */
813 control_usb->grf_uoc0_base->CON4 = 0x00f000f0;
817 /*register otg_linestate irq */
818 irq = platform_get_irq_byname(pdev, "otg_linestate");
821 request_irq(irq, line_irq_handler, 0, "otg_linestate",
824 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
827 control_usb->grf_uoc0_base->CON0 = 0xc000c000;
828 if (control_usb->usb_irq_wakeup)
829 enable_irq_wake(irq);
833 /*register host0_linestate irq */
834 irq = platform_get_irq_byname(pdev, "host0_linestate");
837 request_irq(irq, line_irq_handler, 0, "host0_linestate",
840 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
843 control_usb->grf_uoc1_base->CON0 = 0xc000c000;
844 if (control_usb->usb_irq_wakeup)
845 enable_irq_wake(irq);
849 /*register host1_linestate irq */
850 irq = platform_get_irq_byname(pdev, "host1_linestate");
853 request_irq(irq, line_irq_handler, 0, "host1_linestate",
856 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
859 control_usb->grf_uoc2_base->CON0 = 0xc000c000;
860 if (control_usb->usb_irq_wakeup)
861 enable_irq_wake(irq);
868 /********** end of rk3288 usb detections **********/
870 static int usb_grf_ioremap(struct platform_device *pdev)
873 struct resource *res;
874 void *grf_soc_status1;
875 void *grf_soc_status2;
876 void *grf_soc_status19;
877 void *grf_soc_status21;
884 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
886 grf_soc_status1 = devm_ioremap_resource(&pdev->dev, res);
887 if (IS_ERR(grf_soc_status1)) {
888 ret = PTR_ERR(grf_soc_status1);
891 control_usb->grf_soc_status1_rk3288 =
892 (pGRF_SOC_STATUS1_RK3288) grf_soc_status1;
894 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
896 grf_soc_status2 = devm_ioremap_resource(&pdev->dev, res);
897 if (IS_ERR(grf_soc_status2)) {
898 ret = PTR_ERR(grf_soc_status2);
901 control_usb->grf_soc_status2_rk3288 =
902 (pGRF_SOC_STATUS2_RK3288) grf_soc_status2;
904 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
906 grf_soc_status19 = devm_ioremap_resource(&pdev->dev, res);
907 if (IS_ERR(grf_soc_status19)) {
908 ret = PTR_ERR(grf_soc_status19);
911 control_usb->grf_soc_status19_rk3288 =
912 (pGRF_SOC_STATUS19_RK3288) grf_soc_status19;
914 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
916 grf_soc_status21 = devm_ioremap_resource(&pdev->dev, res);
917 if (IS_ERR(grf_soc_status21)) {
918 ret = PTR_ERR(grf_soc_status21);
921 control_usb->grf_soc_status21_rk3288 =
922 (pGRF_SOC_STATUS21_RK3288) grf_soc_status21;
924 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
926 grf_uoc0_base = devm_ioremap_resource(&pdev->dev, res);
927 if (IS_ERR(grf_uoc0_base)) {
928 ret = PTR_ERR(grf_uoc0_base);
931 control_usb->grf_uoc0_base = (pGRF_UOC0_REG) grf_uoc0_base;
933 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
935 grf_uoc1_base = devm_ioremap_resource(&pdev->dev, res);
936 if (IS_ERR(grf_uoc1_base)) {
937 ret = PTR_ERR(grf_uoc1_base);
940 control_usb->grf_uoc1_base = (pGRF_UOC1_REG) grf_uoc1_base;
942 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
944 grf_uoc2_base = devm_ioremap_resource(&pdev->dev, res);
945 if (IS_ERR(grf_uoc2_base)) {
946 ret = PTR_ERR(grf_uoc2_base);
949 control_usb->grf_uoc2_base = (pGRF_UOC2_REG) grf_uoc2_base;
951 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
953 grf_uoc3_base = devm_ioremap_resource(&pdev->dev, res);
954 if (IS_ERR(grf_uoc3_base)) {
955 ret = PTR_ERR(grf_uoc3_base);
958 control_usb->grf_uoc3_base = (pGRF_UOC3_REG) grf_uoc3_base;
960 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
962 grf_uoc4_base = devm_ioremap_resource(&pdev->dev, res);
963 if (IS_ERR(grf_uoc4_base)) {
964 ret = PTR_ERR(grf_uoc4_base);
967 control_usb->grf_uoc4_base = (pGRF_UOC4_REG) grf_uoc4_base;
972 static int rk_usb_control_probe(struct platform_device *pdev)
975 struct device_node *np = pdev->dev.of_node;
979 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
981 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
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");
992 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
993 usb_battery_charger_detect_work);
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");
1003 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
1004 control_usb->host_gpios->gpio = gpio;
1006 if (!gpio_is_valid(gpio)) {
1007 dev_warn(&pdev->dev, "host_drv_gpio is not specified or invalid\n");
1009 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
1012 "failed to request GPIO%d for host_drv\n",
1017 gpio_direction_output(control_usb->host_gpios->gpio, 1);
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");
1028 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
1029 control_usb->otg_gpios->gpio = gpio;
1031 if (!gpio_is_valid(gpio)) {
1032 dev_warn(&pdev->dev, "otg_drv_gpio is not specified or invalid\n");
1034 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
1037 "failed to request GPIO%d for otg_drv\n", gpio);
1041 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
1049 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
1051 .compatible = "rockchip,rk3288-dwc-control-usb",
1057 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
1061 ret = rk_usb_control_probe(pdev);
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;
1071 clk_prepare_enable(control_usb->hclk_usb_peri);
1073 ret = usb_grf_ioremap(pdev);
1075 dev_err(&pdev->dev, "Failed to ioremap usb grf\n");
1078 #ifdef CONFIG_USB20_OTG
1079 if (usb20otg_get_status(USB_STATUS_BVABLID))
1080 schedule_delayed_work(&control_usb->usb_charger_det_work,
1084 ret = otg_irq_detect_init(pdev);
1091 clk_disable_unprepare(control_usb->hclk_usb_peri);
1095 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
1097 clk_disable_unprepare(control_usb->hclk_usb_peri);
1101 static struct platform_driver dwc_otg_control_usb_driver = {
1102 .probe = dwc_otg_control_usb_probe,
1103 .remove = dwc_otg_control_usb_remove,
1105 .name = "rk3288-dwc-control-usb",
1106 .owner = THIS_MODULE,
1107 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
1111 static int __init dwc_otg_control_usb_init(void)
1115 retval = platform_driver_register(&dwc_otg_control_usb_driver);
1118 printk(KERN_ERR "%s retval=%d\n", __func__, retval);
1124 subsys_initcall(dwc_otg_control_usb_init);
1126 static void __exit dwc_otg_control_usb_exit(void)
1128 platform_driver_unregister(&dwc_otg_control_usb_driver);
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");