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_USBPHY1_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);
131 ret = control_usb->chip_id;
133 case USB_REMOTE_WAKEUP:
134 ret = control_usb->remote_wakeup;
137 ret = control_usb->usb_irq_wakeup;
145 #ifdef CONFIG_RK_USB_UART
147 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
149 * Returns true if the status property of node "usb_uart" is set to "okay"
150 * or "ok", if this property is absent it will use the default status "ok"
153 static bool dwc_otg_uart_enabled(void)
155 struct device_node *np;
157 np = of_find_node_by_name(NULL, "usb_uart");
158 if (np && of_device_is_available(np))
164 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
166 if ((1 == enter_usb_uart_mode) && dwc_otg_uart_enabled()) {
167 /* bypass dm, enter uart mode */
168 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12), RK_GRF_VIRT +
169 RK312X_GRF_UOC1_CON4);
171 } else if (0 == enter_usb_uart_mode) {
173 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12), RK_GRF_VIRT +
174 RK312X_GRF_UOC1_CON4);
178 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
183 static void usb20otg_power_enable(int enable)
186 /* disable otg_drv power */
187 if (gpio_is_valid(control_usb->otg_gpios->gpio))
188 gpio_set_value(control_usb->otg_gpios->gpio, 0);
189 } else if (1 == enable) {
190 /* enable otg_drv power */
191 if (gpio_is_valid(control_usb->otg_gpios->gpio))
192 gpio_set_value(control_usb->otg_gpios->gpio, 1);
196 struct dwc_otg_platform_data usb20otg_pdata_rk3126 = {
201 .hw_init = usb20otg_hw_init,
202 .phy_suspend = usb20otg_phy_suspend,
203 .soft_reset = usb20otg_soft_reset,
204 .clock_init = usb20otg_clock_init,
205 .clock_enable = usb20otg_clock_enable,
206 .get_status = usb20otg_get_status,
207 .power_enable = usb20otg_power_enable,
208 .dwc_otg_uart_mode = dwc_otg_uart_mode,
209 .bc_detect_cb = usb20otg_battery_charger_detect_cb,
213 #ifdef CONFIG_USB20_HOST
214 static void usb20host_hw_init(void)
216 /* Switch to DWC HOST */
217 writel(UOC_HIWORD_UPDATE(1, 1, 3),
218 RK_GRF_VIRT + RK312X_GRF_SOC_CON2);
219 /* Turn off differential receiver in suspend mode */
220 writel(UOC_HIWORD_UPDATE(0, 1, 2),
221 RK_GRF_VIRT + RK312X_GRF_USBPHY0_CON6);
222 /* other haredware init,include:
223 * DRV_VBUS GPIO init */
224 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
225 if (!gpio_get_value(control_usb->host_gpios->gpio))
226 gpio_set_value(control_usb->host_gpios->gpio, 1);
230 static void usb20host_phy_suspend(void *pdata, int suspend)
232 struct dwc_otg_platform_data *usbpdata = pdata;
235 /* enable soft control */
236 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
237 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
238 usbpdata->phy_status = 1;
241 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
242 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
243 usbpdata->phy_status = 0;
247 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
249 struct dwc_otg_platform_data *usbpdata = pdata;
250 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
252 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
253 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
254 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
255 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
256 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
263 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
264 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
265 reset_control_assert(rst_host_p);
267 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
268 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
271 reset_control_deassert(rst_host_p);
273 /* Controller reset */
274 reset_control_assert(rst_host_c);
275 reset_control_assert(rst_host_h);
279 reset_control_deassert(rst_host_c);
280 reset_control_deassert(rst_host_h);
288 static void usb20host_clock_init(void *pdata)
290 struct dwc_otg_platform_data *usbpdata = pdata;
291 struct clk *ahbclk, *phyclk;
293 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
294 if (IS_ERR(ahbclk)) {
295 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
299 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
300 if (IS_ERR(phyclk)) {
301 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
305 usbpdata->phyclk = phyclk;
306 usbpdata->ahbclk = ahbclk;
309 static void usb20host_clock_enable(void *pdata, int enable)
311 struct dwc_otg_platform_data *usbpdata = pdata;
314 clk_prepare_enable(usbpdata->ahbclk);
315 clk_prepare_enable(usbpdata->phyclk);
317 clk_disable_unprepare(usbpdata->ahbclk);
318 clk_disable_unprepare(usbpdata->phyclk);
322 static int usb20host_get_status(int id)
325 u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
328 case USB_STATUS_BVABLID:
330 ret = soc_status0 & (0x1 << 10);
332 case USB_STATUS_DPDM:
334 ret = soc_status0 & (0x3 << 11);
341 ret = control_usb->chip_id;
343 case USB_REMOTE_WAKEUP:
344 ret = control_usb->remote_wakeup;
347 ret = control_usb->usb_irq_wakeup;
355 static void usb20host_power_enable(int enable)
358 /* disable host_drv power */
359 /* do not disable power in default */
360 } else if (1 == enable) {
361 /* enable host_drv power */
362 if (gpio_is_valid(control_usb->host_gpios->gpio))
363 gpio_set_value(control_usb->host_gpios->gpio, 1);
367 struct dwc_otg_platform_data usb20host_pdata_rk3126 = {
372 .hw_init = usb20host_hw_init,
373 .phy_suspend = usb20host_phy_suspend,
374 .soft_reset = usb20host_soft_reset,
375 .clock_init = usb20host_clock_init,
376 .clock_enable = usb20host_clock_enable,
377 .get_status = usb20host_get_status,
378 .power_enable = usb20host_power_enable,
383 static const struct of_device_id rk_usb_control_id_table[] = {
385 .compatible = "rockchip,rk3126-usb-control",
390 /*********************************************************************
391 rk3126 usb detections
392 *********************************************************************/
394 #define WAKE_LOCK_TIMEOUT (HZ * 10)
395 static inline void do_wakeup(struct work_struct *work)
397 /* wake up the system */
398 rk_send_wakeup_key();
401 static void usb_battery_charger_detect_work(struct work_struct *work)
403 rk_usb_charger_status = usb_battery_charger_detect(0);
406 /********** handler for bvalid irq **********/
407 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
410 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
411 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
412 #ifdef CONFIG_RK_USB_UART
413 /* usb otg dp/dm switch to usb phy */
414 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
417 if (control_usb->usb_irq_wakeup) {
418 wake_lock_timeout(&control_usb->usb_wakelock,
420 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
424 rk_usb_charger_status = USB_BC_TYPE_SDP;
425 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
430 /************* register usb detection irqs **************/
431 static int otg_irq_detect_init(struct platform_device *pdev)
436 if (control_usb->usb_irq_wakeup) {
437 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
439 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
442 /*register otg_bvalid irq */
443 irq = platform_get_irq_byname(pdev, "otg_bvalid");
444 if ((irq > 0) && control_usb->usb_irq_wakeup) {
445 ret = request_irq(irq, bvalid_irq_handler,
446 0, "otg_bvalid", NULL);
448 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
450 /* enable bvalid irq */
451 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
452 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
458 /********** end of rk3126 usb detections **********/
459 static int rk_usb_control_probe(struct platform_device *pdev)
462 struct device_node *np = pdev->dev.of_node;
466 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
468 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
473 control_usb->chip_id = RK3126_USB_CTLR;
474 control_usb->remote_wakeup = of_property_read_bool(np,
475 "rockchip,remote_wakeup");
476 control_usb->usb_irq_wakeup = of_property_read_bool(np,
477 "rockchip,usb_irq_wakeup");
479 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
480 usb_battery_charger_detect_work);
482 control_usb->host_gpios =
483 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
484 if (!control_usb->host_gpios) {
485 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
490 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
491 control_usb->host_gpios->gpio = gpio;
493 if (!gpio_is_valid(gpio)) {
494 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
496 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
499 "failed to request GPIO%d for host_drv\n",
504 gpio_direction_output(control_usb->host_gpios->gpio, 1);
507 control_usb->otg_gpios =
508 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
509 if (!control_usb->otg_gpios) {
510 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
515 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
516 control_usb->otg_gpios->gpio = gpio;
518 if (!gpio_is_valid(gpio)) {
519 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
521 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
524 "failed to request GPIO%d for otg_drv\n", gpio);
528 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
535 static int rk_usb_control_remove(struct platform_device *pdev)
540 static struct platform_driver rk_usb_control_driver = {
541 .probe = rk_usb_control_probe,
542 .remove = rk_usb_control_remove,
544 .name = "rk3126-usb-control",
545 .owner = THIS_MODULE,
546 .of_match_table = of_match_ptr(rk_usb_control_id_table),
552 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
554 .compatible = "rockchip,rk3126-dwc-control-usb",
560 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
562 struct clk *hclk_usb_peri;
566 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
571 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
572 if (IS_ERR(hclk_usb_peri)) {
573 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
578 control_usb->hclk_usb_peri = hclk_usb_peri;
579 clk_prepare_enable(hclk_usb_peri);
581 #ifdef CONFIG_USB20_OTG
582 if (usb20otg_get_status(USB_STATUS_BVABLID)) {
583 rk_usb_charger_status = USB_BC_TYPE_SDP;
584 schedule_delayed_work(&control_usb->usb_charger_det_work,
589 ret = otg_irq_detect_init(pdev);
596 clk_disable_unprepare(hclk_usb_peri);
601 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
603 clk_disable_unprepare(control_usb->hclk_usb_peri);
607 static struct platform_driver dwc_otg_control_usb_driver = {
608 .probe = dwc_otg_control_usb_probe,
609 .remove = dwc_otg_control_usb_remove,
611 .name = "rk3126-dwc-control-usb",
612 .owner = THIS_MODULE,
613 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
617 static int __init dwc_otg_control_usb_init(void)
621 retval |= platform_driver_register(&rk_usb_control_driver);
622 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
626 subsys_initcall(dwc_otg_control_usb_init);
628 static void __exit dwc_otg_control_usb_exit(void)
630 platform_driver_unregister(&rk_usb_control_driver);
631 platform_driver_unregister(&dwc_otg_control_usb_driver);
634 module_exit(dwc_otg_control_usb_exit);
635 MODULE_ALIAS("platform: dwc_control_usb");
636 MODULE_AUTHOR("RockChip Inc.");
637 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
638 MODULE_LICENSE("GPL v2");