2 #include "usbdev_grf_regs.h"
3 #include "dwc_otg_regs.h"
4 static struct dwc_otg_control_usb *control_usb;
6 #ifdef CONFIG_USB20_OTG
7 static void usb20otg_hw_init(void)
9 /* Turn off differential receiver in suspend mode */
10 writel(UOC_HIWORD_UPDATE(0, 1, 2),
11 RK_GRF_VIRT + RK312X_GRF_USBPHY0_CON6);
12 /* other haredware init,include:
13 * DRV_VBUS GPIO init */
14 if (gpio_is_valid(control_usb->otg_gpios->gpio)) {
15 if (gpio_get_value(control_usb->otg_gpios->gpio))
16 gpio_set_value(control_usb->otg_gpios->gpio, 0);
20 static void usb20otg_phy_suspend(void *pdata, int suspend)
22 struct dwc_otg_platform_data *usbpdata = pdata;
24 /* enable soft control */
25 writel(UOC_HIWORD_UPDATE(0x55, 0x7f, 0),
26 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
27 usbpdata->phy_status = 1;
30 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
31 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
32 usbpdata->phy_status = 0;
36 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
38 struct dwc_otg_platform_data *usbpdata = pdata;
39 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
41 rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
42 rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
43 rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
44 if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
45 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
52 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
53 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
54 reset_control_assert(rst_otg_p);
56 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
57 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
59 reset_control_deassert(rst_otg_p);
62 /* Controller reset */
63 reset_control_assert(rst_otg_c);
64 reset_control_assert(rst_otg_h);
68 reset_control_deassert(rst_otg_c);
69 reset_control_deassert(rst_otg_h);
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);
108 clk_disable_unprepare(usbpdata->phyclk);
113 static int usb20otg_get_status(int id)
116 u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
118 case USB_STATUS_BVABLID:
120 ret = soc_status0 & (0x1 << 5);
122 case USB_STATUS_DPDM:
124 ret = soc_status0 & (0x3 << 6);
128 ret = soc_status0 & (0x1 << 8);
130 case USB_STATUS_UARTMODE:
131 ret = readl(RK_GRF_VIRT + RK312X_GRF_UOC1_CON4) & (1 << 12);
134 ret = control_usb->chip_id;
136 case USB_REMOTE_WAKEUP:
137 ret = control_usb->remote_wakeup;
140 ret = control_usb->usb_irq_wakeup;
148 #ifdef CONFIG_RK_USB_UART
150 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
152 * Returns true if the status property of node "usb_uart" is set to "okay"
153 * or "ok", if this property is absent it will use the default status "ok"
156 static bool dwc_otg_uart_enabled(void)
158 struct device_node *np;
160 np = of_find_node_by_name(NULL, "usb_uart");
161 if (np && of_device_is_available(np))
167 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
169 if ((1 == enter_usb_uart_mode) && dwc_otg_uart_enabled()) {
170 /* bypass dm, enter uart mode */
171 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12), RK_GRF_VIRT +
172 RK312X_GRF_UOC1_CON4);
174 } else if (0 == enter_usb_uart_mode) {
176 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12), RK_GRF_VIRT +
177 RK312X_GRF_UOC1_CON4);
181 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
186 static void usb20otg_power_enable(int enable)
189 /* disable otg_drv power */
190 if (gpio_is_valid(control_usb->otg_gpios->gpio))
191 gpio_set_value(control_usb->otg_gpios->gpio, 0);
192 } else if (1 == enable) {
193 /* enable otg_drv power */
194 if (gpio_is_valid(control_usb->otg_gpios->gpio))
195 gpio_set_value(control_usb->otg_gpios->gpio, 1);
199 struct dwc_otg_platform_data usb20otg_pdata_rk3126 = {
204 .hw_init = usb20otg_hw_init,
205 .phy_suspend = usb20otg_phy_suspend,
206 .soft_reset = usb20otg_soft_reset,
207 .clock_init = usb20otg_clock_init,
208 .clock_enable = usb20otg_clock_enable,
209 .get_status = usb20otg_get_status,
210 .power_enable = usb20otg_power_enable,
211 .dwc_otg_uart_mode = dwc_otg_uart_mode,
212 .bc_detect_cb = rk_battery_charger_detect_cb,
216 #ifdef CONFIG_USB20_HOST
217 static void usb20host_hw_init(void)
219 /* Turn off differential receiver in suspend mode */
220 writel(UOC_HIWORD_UPDATE(0, 1, 2),
221 RK_GRF_VIRT + RK312X_GRF_USBPHY1_CON6);
222 /* Set disconnect detection trigger point to 600mv */
223 writel(UOC_HIWORD_UPDATE(1, 0xf, 11),
224 RK_GRF_VIRT + RK312X_GRF_USBPHY1_CON7);
225 /* other haredware init,include:
226 * DRV_VBUS GPIO init */
227 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
228 if (!gpio_get_value(control_usb->host_gpios->gpio))
229 gpio_set_value(control_usb->host_gpios->gpio, 1);
233 static void usb20host_phy_suspend(void *pdata, int suspend)
235 struct dwc_otg_platform_data *usbpdata = pdata;
238 /* enable soft control */
239 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
240 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
241 usbpdata->phy_status = 1;
244 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
245 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
246 usbpdata->phy_status = 0;
250 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
252 struct dwc_otg_platform_data *usbpdata = pdata;
253 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
255 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
256 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
257 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
258 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
259 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
266 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
267 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
268 reset_control_assert(rst_host_p);
270 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
271 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
274 reset_control_deassert(rst_host_p);
276 /* Controller reset */
277 reset_control_assert(rst_host_c);
278 reset_control_assert(rst_host_h);
282 reset_control_deassert(rst_host_c);
283 reset_control_deassert(rst_host_h);
291 static void usb20host_clock_init(void *pdata)
293 struct dwc_otg_platform_data *usbpdata = pdata;
294 struct clk *ahbclk, *phyclk;
296 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
297 if (IS_ERR(ahbclk)) {
298 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
302 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
303 if (IS_ERR(phyclk)) {
304 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
308 usbpdata->phyclk = phyclk;
309 usbpdata->ahbclk = ahbclk;
312 static void usb20host_clock_enable(void *pdata, int enable)
314 struct dwc_otg_platform_data *usbpdata = pdata;
317 clk_prepare_enable(usbpdata->ahbclk);
318 clk_prepare_enable(usbpdata->phyclk);
320 clk_disable_unprepare(usbpdata->ahbclk);
321 clk_disable_unprepare(usbpdata->phyclk);
325 static int usb20host_get_status(int id)
328 u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
331 case USB_STATUS_BVABLID:
333 ret = soc_status0 & (0x1 << 10);
335 case USB_STATUS_DPDM:
337 ret = soc_status0 & (0x3 << 11);
344 ret = control_usb->chip_id;
346 case USB_REMOTE_WAKEUP:
347 ret = control_usb->remote_wakeup;
350 ret = control_usb->usb_irq_wakeup;
358 static void usb20host_power_enable(int enable)
361 /* disable host_drv power */
362 /* do not disable power in default */
363 } else if (1 == enable) {
364 /* enable host_drv power */
365 if (gpio_is_valid(control_usb->host_gpios->gpio))
366 gpio_set_value(control_usb->host_gpios->gpio, 1);
370 struct dwc_otg_platform_data usb20host_pdata_rk3126 = {
375 .hw_init = usb20host_hw_init,
376 .phy_suspend = usb20host_phy_suspend,
377 .soft_reset = usb20host_soft_reset,
378 .clock_init = usb20host_clock_init,
379 .clock_enable = usb20host_clock_enable,
380 .get_status = usb20host_get_status,
381 .power_enable = usb20host_power_enable,
385 struct dwc_otg_platform_data usb20ohci_pdata_rk3126;
388 static const struct of_device_id rk_usb_control_id_table[] = {
390 .compatible = "rockchip,rk3126-usb-control",
395 /*********************************************************************
396 rk3126 usb detections
397 *********************************************************************/
399 #define WAKE_LOCK_TIMEOUT (HZ * 10)
400 static inline void do_wakeup(struct work_struct *work)
402 /* wake up the system */
403 rk_send_wakeup_key();
406 static void usb_battery_charger_detect_work(struct work_struct *work)
408 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
411 /********** handler for bvalid irq **********/
412 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
415 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
416 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
417 #ifdef CONFIG_RK_USB_UART
418 /* usb otg dp/dm switch to usb phy */
419 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
422 if (control_usb->usb_irq_wakeup) {
423 wake_lock_timeout(&control_usb->usb_wakelock,
425 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
429 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
434 /************* register usb detection irqs **************/
435 static int otg_irq_detect_init(struct platform_device *pdev)
440 if (control_usb->usb_irq_wakeup) {
441 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
443 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
446 /*register otg_bvalid irq */
447 irq = platform_get_irq_byname(pdev, "otg_bvalid");
448 if ((irq > 0) && control_usb->usb_irq_wakeup) {
449 ret = request_irq(irq, bvalid_irq_handler,
450 0, "otg_bvalid", NULL);
452 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
454 /* enable bvalid irq */
455 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
456 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
462 /********** end of rk3126 usb detections **********/
463 static int rk_usb_control_probe(struct platform_device *pdev)
466 struct device_node *np = pdev->dev.of_node;
470 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
472 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
477 control_usb->chip_id = RK3126_USB_CTLR;
478 control_usb->remote_wakeup = of_property_read_bool(np,
479 "rockchip,remote_wakeup");
480 control_usb->usb_irq_wakeup = of_property_read_bool(np,
481 "rockchip,usb_irq_wakeup");
483 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
484 usb_battery_charger_detect_work);
486 control_usb->host_gpios =
487 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
488 if (!control_usb->host_gpios) {
489 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
494 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
495 control_usb->host_gpios->gpio = gpio;
497 if (!gpio_is_valid(gpio)) {
498 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
500 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
503 "failed to request GPIO%d for host_drv\n",
508 gpio_direction_output(control_usb->host_gpios->gpio, 1);
511 control_usb->otg_gpios =
512 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
513 if (!control_usb->otg_gpios) {
514 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
519 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
520 control_usb->otg_gpios->gpio = gpio;
522 if (!gpio_is_valid(gpio)) {
523 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
525 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
528 "failed to request GPIO%d for otg_drv\n", gpio);
532 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
539 static int rk_usb_control_remove(struct platform_device *pdev)
544 static struct platform_driver rk_usb_control_driver = {
545 .probe = rk_usb_control_probe,
546 .remove = rk_usb_control_remove,
548 .name = "rk3126-usb-control",
549 .owner = THIS_MODULE,
550 .of_match_table = of_match_ptr(rk_usb_control_id_table),
556 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
558 .compatible = "rockchip,rk3126-dwc-control-usb",
564 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
566 struct clk *hclk_usb_peri;
570 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
575 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
576 if (IS_ERR(hclk_usb_peri)) {
577 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
582 control_usb->hclk_usb_peri = hclk_usb_peri;
583 clk_prepare_enable(hclk_usb_peri);
585 #ifdef CONFIG_USB20_OTG
586 if (usb20otg_get_status(USB_STATUS_BVABLID))
587 schedule_delayed_work(&control_usb->usb_charger_det_work,
591 ret = otg_irq_detect_init(pdev);
598 clk_disable_unprepare(hclk_usb_peri);
603 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
605 clk_disable_unprepare(control_usb->hclk_usb_peri);
609 static struct platform_driver dwc_otg_control_usb_driver = {
610 .probe = dwc_otg_control_usb_probe,
611 .remove = dwc_otg_control_usb_remove,
613 .name = "rk3126-dwc-control-usb",
614 .owner = THIS_MODULE,
615 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
619 static int __init dwc_otg_control_usb_init(void)
623 retval |= platform_driver_register(&rk_usb_control_driver);
624 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
628 subsys_initcall(dwc_otg_control_usb_init);
630 static void __exit dwc_otg_control_usb_exit(void)
632 platform_driver_unregister(&rk_usb_control_driver);
633 platform_driver_unregister(&dwc_otg_control_usb_driver);
636 module_exit(dwc_otg_control_usb_exit);
637 MODULE_ALIAS("platform: dwc_control_usb");
638 MODULE_AUTHOR("RockChip Inc.");
639 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
640 MODULE_LICENSE("GPL v2");