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 /* other haredware init,include:
10 * DRV_VBUS GPIO init */
11 if (gpio_is_valid(control_usb->otg_gpios->gpio)) {
12 if (gpio_get_value(control_usb->otg_gpios->gpio))
13 gpio_set_value(control_usb->otg_gpios->gpio, 0);
17 static void usb20otg_phy_suspend(void *pdata, int suspend)
19 struct dwc_otg_platform_data *usbpdata = pdata;
21 /* enable soft control */
22 writel(UOC_HIWORD_UPDATE(0x55, 0x7f, 0),
23 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
24 usbpdata->phy_status = 1;
27 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
28 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
29 usbpdata->phy_status = 0;
33 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
35 struct dwc_otg_platform_data *usbpdata = pdata;
36 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
38 rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
39 rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
40 rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
41 if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
42 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
49 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
50 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
51 reset_control_assert(rst_otg_p);
53 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
54 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
56 reset_control_deassert(rst_otg_p);
59 /* Controller reset */
60 reset_control_assert(rst_otg_c);
61 reset_control_assert(rst_otg_h);
65 reset_control_deassert(rst_otg_c);
66 reset_control_deassert(rst_otg_h);
74 static void usb20otg_clock_init(void *pdata)
76 struct dwc_otg_platform_data *usbpdata = pdata;
77 struct clk *ahbclk, *phyclk;
79 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
81 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
85 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
87 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
91 usbpdata->phyclk = phyclk;
92 usbpdata->ahbclk = ahbclk;
95 static void usb20otg_clock_enable(void *pdata, int enable)
97 struct dwc_otg_platform_data *usbpdata = pdata;
100 clk_prepare_enable(usbpdata->ahbclk);
101 clk_prepare_enable(usbpdata->phyclk);
103 clk_disable_unprepare(usbpdata->ahbclk);
105 clk_disable_unprepare(usbpdata->phyclk);
110 static int usb20otg_get_status(int id)
113 u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
115 case USB_STATUS_BVABLID:
117 ret = soc_status0 & (0x1 << 5);
119 case USB_STATUS_DPDM:
121 ret = soc_status0 & (0x3 << 6);
125 ret = soc_status0 & (0x1 << 8);
128 ret = control_usb->chip_id;
130 case USB_REMOTE_WAKEUP:
131 ret = control_usb->remote_wakeup;
134 ret = control_usb->usb_irq_wakeup;
142 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
144 #ifdef CONFIG_RK_USB_UART
145 if (1 == enter_usb_uart_mode) {
146 /* bypass dm, enter uart mode */
147 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12),
148 RK_GRF_VIRT + RK312X_GRF_UOC1_CON4);
149 } else if (0 == enter_usb_uart_mode) {
151 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12),
152 RK_GRF_VIRT + RK312X_GRF_UOC1_CON4);
157 static void usb20otg_power_enable(int enable)
160 /* disable otg_drv power */
161 if (gpio_is_valid(control_usb->otg_gpios->gpio))
162 gpio_set_value(control_usb->otg_gpios->gpio, 0);
163 } else if (1 == enable) {
164 /* enable otg_drv power */
165 if (gpio_is_valid(control_usb->otg_gpios->gpio))
166 gpio_set_value(control_usb->otg_gpios->gpio, 1);
170 struct dwc_otg_platform_data usb20otg_pdata_rk3126 = {
175 .hw_init = usb20otg_hw_init,
176 .phy_suspend = usb20otg_phy_suspend,
177 .soft_reset = usb20otg_soft_reset,
178 .clock_init = usb20otg_clock_init,
179 .clock_enable = usb20otg_clock_enable,
180 .get_status = usb20otg_get_status,
181 .power_enable = usb20otg_power_enable,
182 .dwc_otg_uart_mode = dwc_otg_uart_mode,
183 .bc_detect_cb = usb20otg_battery_charger_detect_cb,
187 #ifdef CONFIG_USB20_HOST
188 static void usb20host_hw_init(void)
190 writel(UOC_HIWORD_UPDATE(1, 1, 3),
191 RK_GRF_VIRT + RK312X_GRF_SOC_CON2);
192 /* other haredware init,include:
193 * DRV_VBUS GPIO init */
194 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
195 if (!gpio_get_value(control_usb->host_gpios->gpio))
196 gpio_set_value(control_usb->host_gpios->gpio, 1);
200 static void usb20host_phy_suspend(void *pdata, int suspend)
202 struct dwc_otg_platform_data *usbpdata = pdata;
205 /* enable soft control */
206 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
207 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
208 usbpdata->phy_status = 1;
211 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
212 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
213 usbpdata->phy_status = 0;
217 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
219 struct dwc_otg_platform_data *usbpdata = pdata;
220 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
222 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
223 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
224 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
225 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
226 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
233 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
234 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
235 reset_control_assert(rst_host_p);
237 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
238 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
241 reset_control_deassert(rst_host_p);
243 /* Controller reset */
244 reset_control_assert(rst_host_c);
245 reset_control_assert(rst_host_h);
249 reset_control_deassert(rst_host_c);
250 reset_control_deassert(rst_host_h);
258 static void usb20host_clock_init(void *pdata)
260 struct dwc_otg_platform_data *usbpdata = pdata;
261 struct clk *ahbclk, *phyclk;
263 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
264 if (IS_ERR(ahbclk)) {
265 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
269 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
270 if (IS_ERR(phyclk)) {
271 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
275 usbpdata->phyclk = phyclk;
276 usbpdata->ahbclk = ahbclk;
279 static void usb20host_clock_enable(void *pdata, int enable)
281 struct dwc_otg_platform_data *usbpdata = pdata;
284 clk_prepare_enable(usbpdata->ahbclk);
285 clk_prepare_enable(usbpdata->phyclk);
287 clk_disable_unprepare(usbpdata->ahbclk);
288 clk_disable_unprepare(usbpdata->phyclk);
292 static int usb20host_get_status(int id)
295 u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
298 case USB_STATUS_BVABLID:
300 ret = soc_status0 & (0x1 << 10);
302 case USB_STATUS_DPDM:
304 ret = soc_status0 & (0x3 << 11);
311 ret = control_usb->chip_id;
313 case USB_REMOTE_WAKEUP:
314 ret = control_usb->remote_wakeup;
317 ret = control_usb->usb_irq_wakeup;
325 static void usb20host_power_enable(int enable)
328 /* disable host_drv power */
329 /* do not disable power in default */
330 } else if (1 == enable) {
331 /* enable host_drv power */
332 if (gpio_is_valid(control_usb->host_gpios->gpio))
333 gpio_set_value(control_usb->host_gpios->gpio, 1);
337 struct dwc_otg_platform_data usb20host_pdata_rk3126 = {
342 .hw_init = usb20host_hw_init,
343 .phy_suspend = usb20host_phy_suspend,
344 .soft_reset = usb20host_soft_reset,
345 .clock_init = usb20host_clock_init,
346 .clock_enable = usb20host_clock_enable,
347 .get_status = usb20host_get_status,
348 .power_enable = usb20host_power_enable,
353 static const struct of_device_id rk_usb_control_id_table[] = {
355 .compatible = "rockchip,rk3126-usb-control",
360 /*********************************************************************
361 rk3126 usb detections
362 *********************************************************************/
364 #define WAKE_LOCK_TIMEOUT (HZ * 10)
365 static inline void do_wakeup(struct work_struct *work)
367 /* wake up the system */
368 rk_send_wakeup_key();
371 static void usb_battery_charger_detect_work(struct work_struct *work)
373 rk_usb_charger_status = usb_battery_charger_detect(0);
376 /********** handler for bvalid irq **********/
377 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
380 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
381 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
382 #ifdef CONFIG_RK_USB_UART
383 /* usb otg dp/dm switch to usb phy */
384 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
387 if (control_usb->usb_irq_wakeup) {
388 wake_lock_timeout(&control_usb->usb_wakelock,
390 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
394 rk_usb_charger_status = USB_BC_TYPE_SDP;
395 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
400 /************* register usb detection irqs **************/
401 static int otg_irq_detect_init(struct platform_device *pdev)
406 if (control_usb->usb_irq_wakeup) {
407 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
409 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
412 /*register otg_bvalid irq */
413 irq = platform_get_irq_byname(pdev, "otg_bvalid");
414 if ((irq > 0) && control_usb->usb_irq_wakeup) {
415 ret = request_irq(irq, bvalid_irq_handler,
416 0, "otg_bvalid", NULL);
418 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
420 /* enable bvalid irq */
421 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
422 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
428 /********** end of rk3126 usb detections **********/
429 static int rk_usb_control_probe(struct platform_device *pdev)
432 struct device_node *np = pdev->dev.of_node;
436 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
438 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
443 control_usb->chip_id = RK3126_USB_CTLR;
444 control_usb->remote_wakeup = of_property_read_bool(np,
445 "rockchip,remote_wakeup");
446 control_usb->usb_irq_wakeup = of_property_read_bool(np,
447 "rockchip,usb_irq_wakeup");
449 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
450 usb_battery_charger_detect_work);
452 control_usb->host_gpios =
453 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
454 if (!control_usb->host_gpios) {
455 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
460 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
461 control_usb->host_gpios->gpio = gpio;
463 if (!gpio_is_valid(gpio)) {
464 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
466 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
469 "failed to request GPIO%d for host_drv\n",
474 gpio_direction_output(control_usb->host_gpios->gpio, 1);
477 control_usb->otg_gpios =
478 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
479 if (!control_usb->otg_gpios) {
480 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
485 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
486 control_usb->otg_gpios->gpio = gpio;
488 if (!gpio_is_valid(gpio)) {
489 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
491 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
494 "failed to request GPIO%d for otg_drv\n", gpio);
498 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
505 static int rk_usb_control_remove(struct platform_device *pdev)
510 static struct platform_driver rk_usb_control_driver = {
511 .probe = rk_usb_control_probe,
512 .remove = rk_usb_control_remove,
514 .name = "rk3126-usb-control",
515 .owner = THIS_MODULE,
516 .of_match_table = of_match_ptr(rk_usb_control_id_table),
522 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
524 .compatible = "rockchip,rk3126-dwc-control-usb",
530 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
532 struct clk *hclk_usb_peri;
536 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
541 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
542 if (IS_ERR(hclk_usb_peri)) {
543 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
548 control_usb->hclk_usb_peri = hclk_usb_peri;
549 clk_prepare_enable(hclk_usb_peri);
551 #ifdef CONFIG_USB20_OTG
552 if (usb20otg_get_status(USB_STATUS_BVABLID)) {
553 rk_usb_charger_status = USB_BC_TYPE_SDP;
554 schedule_delayed_work(&control_usb->usb_charger_det_work,
559 ret = otg_irq_detect_init(pdev);
566 clk_disable_unprepare(hclk_usb_peri);
571 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
573 clk_disable_unprepare(control_usb->hclk_usb_peri);
577 static struct platform_driver dwc_otg_control_usb_driver = {
578 .probe = dwc_otg_control_usb_probe,
579 .remove = dwc_otg_control_usb_remove,
581 .name = "rk3126-dwc-control-usb",
582 .owner = THIS_MODULE,
583 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
587 static int __init dwc_otg_control_usb_init(void)
591 retval |= platform_driver_register(&rk_usb_control_driver);
592 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
596 subsys_initcall(dwc_otg_control_usb_init);
598 static void __exit dwc_otg_control_usb_exit(void)
600 platform_driver_unregister(&rk_usb_control_driver);
601 platform_driver_unregister(&dwc_otg_control_usb_driver);
604 module_exit(dwc_otg_control_usb_exit);
605 MODULE_ALIAS("platform: dwc_control_usb");
606 MODULE_AUTHOR("RockChip Inc.");
607 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
608 MODULE_LICENSE("GPL v2");