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 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);
195 struct dwc_otg_platform_data usb20otg_pdata_rk3288 = {
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,
213 #ifdef CONFIG_USB20_HOST
215 static void usb20host_hw_init(void)
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.
221 control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
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);
231 static void usb20host_phy_suspend(void *pdata, int suspend)
233 struct dwc_otg_platform_data *usbpdata = pdata;
236 /* enable soft control */
237 control_usb->grf_uoc2_base->CON2 =
238 (0x01 << 2) | ((0x01 << 2) << 16);
240 control_usb->grf_uoc2_base->CON3 = 0x2A | (0x3F << 16);
241 usbpdata->phy_status = 1;
244 control_usb->grf_uoc2_base->CON2 = ((0x01 << 2) << 16);
245 usbpdata->phy_status = 0;
249 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
251 struct dwc_otg_platform_data *usbpdata = pdata;
252 struct reset_control *rst_host1_h, *rst_host1_p, *rst_host1_c;
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");
262 reset_control_assert(rst_host1_h);
263 reset_control_assert(rst_host1_p);
264 reset_control_assert(rst_host1_c);
266 reset_control_deassert(rst_host1_h);
267 reset_control_deassert(rst_host1_p);
268 reset_control_deassert(rst_host1_c);
272 static void usb20host_clock_init(void *pdata)
274 struct dwc_otg_platform_data *usbpdata = pdata;
275 struct clk *ahbclk, *phyclk, *phyclk_480m;
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");
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");
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");
295 usbpdata->phyclk = phyclk;
296 usbpdata->ahbclk = ahbclk;
297 usbpdata->phyclk_480m = phyclk_480m;
300 static void usb20host_clock_enable(void *pdata, int enable)
302 struct dwc_otg_platform_data *usbpdata = pdata;
305 clk_prepare_enable(usbpdata->ahbclk);
306 clk_prepare_enable(usbpdata->phyclk);
308 clk_disable_unprepare(usbpdata->ahbclk);
309 clk_disable_unprepare(usbpdata->phyclk);
313 static int usb20host_get_status(int id)
318 case USB_STATUS_BVABLID:
320 ret = control_usb->grf_soc_status2_rk3288->host1_bvalid;
322 case USB_STATUS_DPDM:
324 ret = control_usb->grf_soc_status2_rk3288->host1_linestate;
328 ret = control_usb->grf_soc_status2_rk3288->host1_iddig;
331 ret = control_usb->chip_id;
333 case USB_REMOTE_WAKEUP:
334 ret = control_usb->remote_wakeup;
337 ret = control_usb->usb_irq_wakeup;
346 static void usb20host_power_enable(int 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);
359 struct dwc_otg_platform_data usb20host_pdata_rk3288 = {
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,
375 #ifdef CONFIG_USB_EHCI1_RK
376 static void rk_ehci1_hw_init(void)
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);
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
387 control_usb->grf_uoc2_base->CON0 = (1 << 16) | 0;
389 /* change INCR to INCR16 or INCR8(beats less than 16)
390 * or INCR4(beats less than 8) or SINGLE(beats less than 4)
392 control_usb->grf_uoc4_base->CON0 = 0x00ff00bc;
395 static void rk_ehci1_clock_init(void *pdata)
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
401 struct rkehci_platform_data *usbpdata = pdata;
402 struct clk *ahbclk, *phyclk480m_ehci1, *phyclk12m_ehci1;
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");
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");
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");
422 usbpdata->hclk_ehci = ahbclk;
423 usbpdata->ehci_phy_480m = phyclk480m_ehci1;
424 usbpdata->ehci_phy_12m = phyclk12m_ehci1;
427 static void rk_ehci1_clock_enable(void *pdata, int enable)
429 struct rkehci_platform_data *usbpdata = pdata;
431 if (enable == usbpdata->clk_status)
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;
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;
446 static void rk_ehci1_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
448 struct rkehci_platform_data *usbpdata = pdata;
449 struct reset_control *rst_ehci1_h, *rst_ehci1_a, *rst_ehci1_p;
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");
455 reset_control_assert(rst_ehci1_h);
456 reset_control_assert(rst_ehci1_a);
457 reset_control_assert(rst_ehci1_p);
459 reset_control_deassert(rst_ehci1_h);
460 reset_control_deassert(rst_ehci1_a);
461 reset_control_deassert(rst_ehci1_p);
464 /* EHCI1 per-port reset */
465 control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (1 << 10);
467 control_usb->grf_uoc3_base->CON0 = ((1 << 10) << 16) | (0 << 10);
471 struct rkehci_platform_data rkehci1_pdata_rk3288 = {
473 .ehci_phy_12m = NULL,
474 .ehci_phy_480m = NULL,
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,
483 #ifdef CONFIG_USB_EHCI_RK
484 static void rk_ehci_hw_init(void)
486 /* usb phy config init */
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);
495 static void rk_ehci_phy_suspend(void *pdata, int suspend)
497 struct rkehci_platform_data *usbpdata = pdata;
500 /* enable soft control */
501 control_usb->grf_uoc1_base->CON2 =
502 (0x01 << 2) | ((0x01 << 2) << 16);
504 control_usb->grf_uoc1_base->CON3 = 0x2A | (0x3F << 16);
505 usbpdata->phy_status = 1;
508 control_usb->grf_uoc1_base->CON2 = ((0x01 << 2) << 16);
509 usbpdata->phy_status = 0;
513 static void rk_ehci_clock_init(void *pdata)
515 struct rkehci_platform_data *usbpdata = pdata;
516 struct clk *ahbclk, *phyclk;
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");
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");
530 usbpdata->phyclk = phyclk;
531 usbpdata->ahbclk = ahbclk;
534 static void rk_ehci_clock_enable(void *pdata, int enable)
536 struct rkehci_platform_data *usbpdata = pdata;
538 if (enable == usbpdata->clk_status)
541 clk_prepare_enable(usbpdata->ahbclk);
542 clk_prepare_enable(usbpdata->phyclk);
543 usbpdata->clk_status = 1;
545 clk_disable_unprepare(usbpdata->ahbclk);
546 clk_disable_unprepare(usbpdata->phyclk);
547 usbpdata->clk_status = 0;
551 static void rk_ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
553 struct rkehci_platform_data *usbpdata = pdata;
554 struct reset_control *rst_host0_h, *rst_host0_p,
555 *rst_host0_c , *rst_host0;
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");
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);
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);
579 static int rk_ehci_get_status(int id)
584 case USB_STATUS_DPDM:
586 ret = control_usb->grf_soc_status2_rk3288->host0_linestate;
589 ret = control_usb->chip_id;
591 case USB_REMOTE_WAKEUP:
592 ret = control_usb->remote_wakeup;
595 ret = control_usb->usb_irq_wakeup;
604 struct rkehci_platform_data rkehci_pdata_rk3288 = {
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,
618 #ifdef CONFIG_USB_OHCI_HCD_RK
619 static void rk_ohci_hw_init(void)
621 /* usb phy config init */
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);
630 static void rk_ohci_clock_init(void *pdata)
632 struct rkehci_platform_data *usbpdata = pdata;
633 struct clk *ahbclk, *phyclk;
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");
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");
647 usbpdata->phyclk = phyclk;
648 usbpdata->ahbclk = ahbclk;
651 static void rk_ohci_clock_enable(void *pdata, int enable)
653 struct rkehci_platform_data *usbpdata = pdata;
655 if (enable == usbpdata->clk_status)
658 clk_prepare_enable(usbpdata->ahbclk);
659 clk_prepare_enable(usbpdata->phyclk);
660 usbpdata->clk_status = 1;
662 clk_disable_unprepare(usbpdata->ahbclk);
663 clk_disable_unprepare(usbpdata->phyclk);
664 usbpdata->clk_status = 0;
668 static void rk_ohci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
672 struct rkehci_platform_data rkohci_pdata_rk3288 = {
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,
683 /*********************************************************************
684 rk3288 usb detections
685 *********************************************************************/
687 #define WAKE_LOCK_TIMEOUT (HZ * 10)
688 static inline void do_wakeup(struct work_struct *work)
690 /* wake up the system */
691 rk_send_wakeup_key();
694 static void usb_battery_charger_detect_work(struct work_struct *work)
696 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
699 /********** handler for bvalid irq **********/
700 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
703 control_usb->grf_uoc0_base->CON4 = (0x0008 | (0x0008 << 16));
705 /* usb otg dp/dm switch to usb phy */
706 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
708 if (control_usb->usb_irq_wakeup) {
709 wake_lock_timeout(&control_usb->usb_wakelock,
711 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
714 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
719 /***** handler for otg id rise and fall edge *****/
720 static irqreturn_t id_irq_handler(int irq, void *dev_id)
722 unsigned int uoc_con;
725 uoc_con = control_usb->grf_uoc0_base->CON4;
728 if (uoc_con & (1 << 5)) {
729 /* clear id rise irq pandding */
730 control_usb->grf_uoc0_base->CON4 = ((1 << 5) | (1 << 21));
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));
741 if (control_usb->usb_irq_wakeup) {
742 wake_lock_timeout(&control_usb->usb_wakelock,
744 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
751 #ifdef USB_LINESTATE_IRQ
752 /***** handler for usb line status change *****/
754 static irqreturn_t line_irq_handler(int irq, void *dev_id)
758 if (control_usb->grf_uoc0_base->CON0 & 1 << 15)
759 control_usb->grf_uoc0_base->CON0 = (1 << 15 | 1 << 31);
761 if (control_usb->grf_uoc1_base->CON0 & 1 << 15)
762 control_usb->grf_uoc1_base->CON0 = (1 << 15 | 1 << 31);
764 if (control_usb->grf_uoc2_base->CON0 & 1 << 15)
765 control_usb->grf_uoc2_base->CON0 = (1 << 15 | 1 << 31);
767 if (control_usb->usb_irq_wakeup) {
768 wake_lock_timeout(&control_usb->usb_wakelock,
770 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
778 /************* register usb detection irqs **************/
779 static int otg_irq_detect_init(struct platform_device *pdev)
783 if (control_usb->usb_irq_wakeup) {
784 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
786 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
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);
795 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
797 /* enable bvalid irq */
798 control_usb->grf_uoc0_base->CON4 = 0x000c000c;
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);
807 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
809 /* enable otg_id irq */
810 control_usb->grf_uoc0_base->CON4 = 0x00f000f0;
814 /*register otg_linestate irq */
815 irq = platform_get_irq_byname(pdev, "otg_linestate");
818 request_irq(irq, line_irq_handler, 0, "otg_linestate",
821 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
824 control_usb->grf_uoc0_base->CON0 = 0xc000c000;
825 if (control_usb->usb_irq_wakeup)
826 enable_irq_wake(irq);
830 /*register host0_linestate irq */
831 irq = platform_get_irq_byname(pdev, "host0_linestate");
834 request_irq(irq, line_irq_handler, 0, "host0_linestate",
837 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
840 control_usb->grf_uoc1_base->CON0 = 0xc000c000;
841 if (control_usb->usb_irq_wakeup)
842 enable_irq_wake(irq);
846 /*register host1_linestate irq */
847 irq = platform_get_irq_byname(pdev, "host1_linestate");
850 request_irq(irq, line_irq_handler, 0, "host1_linestate",
853 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
856 control_usb->grf_uoc2_base->CON0 = 0xc000c000;
857 if (control_usb->usb_irq_wakeup)
858 enable_irq_wake(irq);
865 /********** end of rk3288 usb detections **********/
867 static int usb_grf_ioremap(struct platform_device *pdev)
870 struct resource *res;
871 void *grf_soc_status1;
872 void *grf_soc_status2;
873 void *grf_soc_status19;
874 void *grf_soc_status21;
881 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
883 grf_soc_status1 = devm_ioremap_resource(&pdev->dev, res);
884 if (IS_ERR(grf_soc_status1)) {
885 ret = PTR_ERR(grf_soc_status1);
888 control_usb->grf_soc_status1_rk3288 =
889 (pGRF_SOC_STATUS1_RK3288) grf_soc_status1;
891 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
893 grf_soc_status2 = devm_ioremap_resource(&pdev->dev, res);
894 if (IS_ERR(grf_soc_status2)) {
895 ret = PTR_ERR(grf_soc_status2);
898 control_usb->grf_soc_status2_rk3288 =
899 (pGRF_SOC_STATUS2_RK3288) grf_soc_status2;
901 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
903 grf_soc_status19 = devm_ioremap_resource(&pdev->dev, res);
904 if (IS_ERR(grf_soc_status19)) {
905 ret = PTR_ERR(grf_soc_status19);
908 control_usb->grf_soc_status19_rk3288 =
909 (pGRF_SOC_STATUS19_RK3288) grf_soc_status19;
911 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
913 grf_soc_status21 = devm_ioremap_resource(&pdev->dev, res);
914 if (IS_ERR(grf_soc_status21)) {
915 ret = PTR_ERR(grf_soc_status21);
918 control_usb->grf_soc_status21_rk3288 =
919 (pGRF_SOC_STATUS21_RK3288) grf_soc_status21;
921 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
923 grf_uoc0_base = devm_ioremap_resource(&pdev->dev, res);
924 if (IS_ERR(grf_uoc0_base)) {
925 ret = PTR_ERR(grf_uoc0_base);
928 control_usb->grf_uoc0_base = (pGRF_UOC0_REG) grf_uoc0_base;
930 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
932 grf_uoc1_base = devm_ioremap_resource(&pdev->dev, res);
933 if (IS_ERR(grf_uoc1_base)) {
934 ret = PTR_ERR(grf_uoc1_base);
937 control_usb->grf_uoc1_base = (pGRF_UOC1_REG) grf_uoc1_base;
939 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
941 grf_uoc2_base = devm_ioremap_resource(&pdev->dev, res);
942 if (IS_ERR(grf_uoc2_base)) {
943 ret = PTR_ERR(grf_uoc2_base);
946 control_usb->grf_uoc2_base = (pGRF_UOC2_REG) grf_uoc2_base;
948 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
950 grf_uoc3_base = devm_ioremap_resource(&pdev->dev, res);
951 if (IS_ERR(grf_uoc3_base)) {
952 ret = PTR_ERR(grf_uoc3_base);
955 control_usb->grf_uoc3_base = (pGRF_UOC3_REG) grf_uoc3_base;
957 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
959 grf_uoc4_base = devm_ioremap_resource(&pdev->dev, res);
960 if (IS_ERR(grf_uoc4_base)) {
961 ret = PTR_ERR(grf_uoc4_base);
964 control_usb->grf_uoc4_base = (pGRF_UOC4_REG) grf_uoc4_base;
969 static int rk_usb_control_probe(struct platform_device *pdev)
972 struct device_node *np = pdev->dev.of_node;
976 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
978 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
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");
989 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
990 usb_battery_charger_detect_work);
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");
1000 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
1001 control_usb->host_gpios->gpio = gpio;
1003 if (!gpio_is_valid(gpio)) {
1004 dev_warn(&pdev->dev, "host_drv_gpio is not specified or invalid\n");
1006 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
1009 "failed to request GPIO%d for host_drv\n",
1014 gpio_direction_output(control_usb->host_gpios->gpio, 1);
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");
1025 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
1026 control_usb->otg_gpios->gpio = gpio;
1028 if (!gpio_is_valid(gpio)) {
1029 dev_warn(&pdev->dev, "otg_drv_gpio is not specified or invalid\n");
1031 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
1034 "failed to request GPIO%d for otg_drv\n", gpio);
1038 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
1046 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
1048 .compatible = "rockchip,rk3288-dwc-control-usb",
1054 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
1058 ret = rk_usb_control_probe(pdev);
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;
1068 clk_prepare_enable(control_usb->hclk_usb_peri);
1070 ret = usb_grf_ioremap(pdev);
1072 dev_err(&pdev->dev, "Failed to ioremap usb grf\n");
1075 #ifdef CONFIG_USB20_OTG
1076 if (usb20otg_get_status(USB_STATUS_BVABLID))
1077 schedule_delayed_work(&control_usb->usb_charger_det_work,
1081 ret = otg_irq_detect_init(pdev);
1088 clk_disable_unprepare(control_usb->hclk_usb_peri);
1092 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
1094 clk_disable_unprepare(control_usb->hclk_usb_peri);
1098 static struct platform_driver dwc_otg_control_usb_driver = {
1099 .probe = dwc_otg_control_usb_probe,
1100 .remove = dwc_otg_control_usb_remove,
1102 .name = "rk3288-dwc-control-usb",
1103 .owner = THIS_MODULE,
1104 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
1108 static int __init dwc_otg_control_usb_init(void)
1112 retval = platform_driver_register(&dwc_otg_control_usb_driver);
1115 printk(KERN_ERR "%s retval=%d\n", __func__, retval);
1121 subsys_initcall(dwc_otg_control_usb_init);
1123 static void __exit dwc_otg_control_usb_exit(void)
1125 platform_driver_unregister(&dwc_otg_control_usb_driver);
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");