3 #include "dwc_otg_regs.h"
5 static struct dwc_otg_control_usb *control_usb;
7 static u32 uoc_read(u32 reg)
11 regmap_read(control_usb->grf, reg, &val);
15 static void uoc_write(u32 value, u32 reg)
17 regmap_write(control_usb->grf, reg, value);
20 #ifdef CONFIG_USB20_OTG
21 static void usb20otg_hw_init(void)
23 /* Turn off differential receiver in suspend mode */
24 uoc_write(UOC_HIWORD_UPDATE(0, 1, 2), 0x798);
26 /* Set disconnect detection trigger point to 625mv */
27 uoc_write(UOC_HIWORD_UPDATE(0x9, 0xf, 11), 0x79c);
29 /* other haredware init,include:
30 * DRV_VBUS GPIO init */
31 if (gpio_is_valid(control_usb->otg_gpios->gpio))
32 gpio_set_value(control_usb->otg_gpios->gpio, 0);
35 static void usb20otg_phy_suspend(void *pdata, int suspend)
37 struct dwc_otg_platform_data *usbpdata = pdata;
39 /* enable soft control */
40 uoc_write(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0), 0x700);
41 usbpdata->phy_status = 1;
44 uoc_write(UOC_HIWORD_UPDATE(0x0, 0x1, 0), 0x700);
45 usbpdata->phy_status = 0;
49 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
51 struct dwc_otg_platform_data *usbpdata = pdata;
52 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
54 rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
55 rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
56 rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
57 if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
58 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
65 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x700);
66 reset_control_assert(rst_otg_p);
68 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x700);
70 reset_control_deassert(rst_otg_p);
73 /* Controller reset */
74 reset_control_assert(rst_otg_c);
75 reset_control_assert(rst_otg_h);
79 reset_control_deassert(rst_otg_c);
80 reset_control_deassert(rst_otg_h);
84 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x700);
85 reset_control_assert(rst_otg_p);
87 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x700);
89 reset_control_deassert(rst_otg_p);
97 static void usb20otg_clock_init(void *pdata)
99 struct dwc_otg_platform_data *usbpdata = pdata;
100 struct clk *ahbclk, *phyclk;
102 ahbclk = devm_clk_get(usbpdata->dev, "otg");
103 if (IS_ERR(ahbclk)) {
104 dev_err(usbpdata->dev, "Failed to get otg clk\n");
108 phyclk = devm_clk_get(usbpdata->dev, "sclk_otgphy0");
109 if (IS_ERR(phyclk)) {
110 dev_err(usbpdata->dev, "Failed to get sclk_otgphy0\n");
114 usbpdata->phyclk = phyclk;
115 usbpdata->ahbclk = ahbclk;
118 static void usb20otg_clock_enable(void *pdata, int enable)
120 struct dwc_otg_platform_data *usbpdata = pdata;
123 clk_prepare_enable(usbpdata->ahbclk);
124 clk_prepare_enable(usbpdata->phyclk);
126 clk_disable_unprepare(usbpdata->ahbclk);
127 clk_disable_unprepare(usbpdata->phyclk);
131 static int usb20otg_get_status(int id)
134 u32 soc_status15 = uoc_read(control_usb->grf_otg_st_offset);
137 case USB_STATUS_BVABLID:
139 ret = soc_status15 & (0x1 << 23);
141 case USB_STATUS_DPDM:
143 ret = soc_status15 & (0x3 << 24);
147 ret = soc_status15 & (0x1 << 26);
150 ret = control_usb->chip_id;
152 case USB_REMOTE_WAKEUP:
153 ret = control_usb->remote_wakeup;
156 ret = control_usb->usb_irq_wakeup;
164 #ifdef CONFIG_RK_USB_UART
166 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
168 * Returns true if the status property of node "usb_uart" is set to "okay"
169 * or "ok", if this property is absent it will use the default status "ok"
172 static bool dwc_otg_uart_enabled(void)
177 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
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 (usb20otg_get_status(USB_STATUS_BVABLID))
193 rk_battery_charger_detect_cb(USB_OTG_POWER_OFF);
194 } else if (1 == enable) {
195 /* enable otg_drv power */
196 if (gpio_is_valid(control_usb->otg_gpios->gpio))
197 gpio_set_value(control_usb->otg_gpios->gpio, 1);
198 else if (!usb20otg_get_status(USB_STATUS_BVABLID))
199 rk_battery_charger_detect_cb(USB_OTG_POWER_ON);
203 struct dwc_otg_platform_data usb20otg_pdata_rk3368 = {
208 .hw_init = usb20otg_hw_init,
209 .phy_suspend = usb20otg_phy_suspend,
210 .soft_reset = usb20otg_soft_reset,
211 .clock_init = usb20otg_clock_init,
212 .clock_enable = usb20otg_clock_enable,
213 .get_status = usb20otg_get_status,
214 .power_enable = usb20otg_power_enable,
215 .dwc_otg_uart_mode = dwc_otg_uart_mode,
216 .bc_detect_cb = rk_battery_charger_detect_cb,
220 #ifdef CONFIG_USB_EHCI_RK
221 static void usb20ehci_hw_init(void)
223 /* Turn off differential receiver in suspend mode */
224 uoc_write(UOC_HIWORD_UPDATE(0, 1, 2), 0x7b8);
225 /* Set disconnect detection trigger point to 625mv */
226 uoc_write(UOC_HIWORD_UPDATE(0x9, 0xf, 11), 0x7bc);
228 /* other haredware init,include:
229 * DRV_VBUS GPIO init */
230 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
231 if (!gpio_get_value(control_usb->host_gpios->gpio))
232 gpio_set_value(control_usb->host_gpios->gpio, 1);
236 static void usb20ehci_phy_suspend(void *pdata, int suspend)
238 struct rkehci_platform_data *usbpdata = pdata;
241 /* enable soft control */
242 uoc_write(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0), 0x728);
243 usbpdata->phy_status = 1;
246 uoc_write(UOC_HIWORD_UPDATE(0x0, 0x1, 0), 0x728);
247 usbpdata->phy_status = 0;
251 static void usb20ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
253 struct rkehci_platform_data *usbpdata = pdata;
254 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
256 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
257 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
258 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
259 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
260 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
267 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x728);
268 reset_control_assert(rst_host_p);
270 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x728);
273 reset_control_deassert(rst_host_p);
275 /* Controller reset */
276 reset_control_assert(rst_host_c);
277 reset_control_assert(rst_host_h);
281 reset_control_deassert(rst_host_c);
282 reset_control_deassert(rst_host_h);
290 static void usb20ehci_clock_init(void *pdata)
294 static void usb20ehci_clock_enable(void *pdata, int enable)
298 static int usb20ehci_get_status(int id)
300 /* For HOST port in rk336x can not get any info from GRF */
304 struct rkehci_platform_data usb20ehci_pdata_rk3368 = {
308 .hw_init = usb20ehci_hw_init,
309 .phy_suspend = usb20ehci_phy_suspend,
310 .soft_reset = usb20ehci_soft_reset,
311 .clock_init = usb20ehci_clock_init,
312 .clock_enable = usb20ehci_clock_enable,
313 .get_status = usb20ehci_get_status,
317 struct dwc_otg_platform_data usb20ohci_pdata_rk3368;
320 static const struct of_device_id rk_usb_control_id_table[] = {
322 .compatible = "rockchip,rk3368-usb-control",
327 /*********************************************************************
328 rk3126 usb detections
329 *********************************************************************/
331 #define WAKE_LOCK_TIMEOUT (HZ * 10)
332 static inline void do_wakeup(struct work_struct *work)
334 /* wake up the system */
335 rk_send_wakeup_key();
338 static void usb_battery_charger_detect_work(struct work_struct *work)
340 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
343 /********** handler for bvalid irq **********/
344 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
347 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x6a0);
348 #ifdef CONFIG_RK_USB_UART
349 /* usb otg dp/dm switch to usb phy */
350 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
353 if (control_usb->usb_irq_wakeup) {
354 wake_lock_timeout(&control_usb->usb_wakelock,
356 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
360 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
365 /************* register usb detection irqs **************/
366 static int otg_irq_detect_init(struct platform_device *pdev)
371 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
373 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
375 /*register otg_bvalid irq */
376 irq = platform_get_irq_byname(pdev, "otg_bvalid");
377 if ((irq > 0) && control_usb->usb_irq_wakeup) {
378 ret = request_irq(irq, bvalid_irq_handler,
379 0, "otg_bvalid", NULL);
381 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
383 /* enable bvalid irq */
384 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x680);
391 /********** end of usb detections **********/
393 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
395 .compatible = "rockchip,rk3368-dwc-control-usb",
400 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
403 struct device_node *np = dev->of_node;
404 struct clk *hclk_usb_peri;
409 control_usb = devm_kzalloc(dev, sizeof(*control_usb), GFP_KERNEL);
411 dev_err(&pdev->dev, "Unable to alloc memory for control usb\n");
415 /* Init regmap GRF */
416 grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
418 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
421 control_usb->grf = grf;
423 /* get the reg offset of GRF_SOC_STATUS for USB2.0 OTG */
424 if (of_property_read_u32(np, "grf-offset", &offset)) {
425 dev_err(&pdev->dev, "missing reg property in node %s\n",
429 control_usb->grf_otg_st_offset = offset;
431 /* Init Vbus-drv GPIOs */
432 control_usb->host_gpios =
433 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
434 if (!control_usb->host_gpios) {
435 dev_err(&pdev->dev, "Unable to alloc memory for host_gpios\n");
439 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
440 control_usb->host_gpios->gpio = gpio;
442 if (gpio_is_valid(gpio)) {
443 if (devm_gpio_request(&pdev->dev, gpio, "usb_host_drv")) {
445 "Failed to request GPIO%d for host_drv\n",
449 gpio_direction_output(control_usb->host_gpios->gpio, 1);
452 control_usb->otg_gpios =
453 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
454 if (!control_usb->otg_gpios) {
455 dev_err(&pdev->dev, "Unable to alloc memory for otg_gpios\n");
459 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
460 control_usb->otg_gpios->gpio = gpio;
462 if (gpio_is_valid(gpio)) {
463 if (devm_gpio_request(&pdev->dev, gpio, "usb_otg_drv")) {
465 "failed to request GPIO%d for otg_drv\n", gpio);
468 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
472 control_usb->remote_wakeup = of_property_read_bool(np,
473 "rockchip,remote_wakeup");
474 control_usb->usb_irq_wakeup = of_property_read_bool(np,
475 "rockchip,usb_irq_wakeup");
477 /* Init hclk_usb_peri */
478 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
479 if (IS_ERR(hclk_usb_peri)) {
480 dev_info(&pdev->dev, "no hclk_usb_peri clk specified\n");
481 hclk_usb_peri = NULL;
483 control_usb->hclk_usb_peri = hclk_usb_peri;
484 clk_prepare_enable(hclk_usb_peri);
486 #ifdef CONFIG_USB20_OTG
487 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
488 usb_battery_charger_detect_work);
490 if (usb20otg_get_status(USB_STATUS_BVABLID))
491 schedule_delayed_work(&control_usb->usb_charger_det_work,
495 ret = otg_irq_detect_init(pdev);
502 clk_disable_unprepare(hclk_usb_peri);
506 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
508 clk_disable_unprepare(control_usb->hclk_usb_peri);
512 static struct platform_driver dwc_otg_control_usb_driver = {
513 .probe = dwc_otg_control_usb_probe,
514 .remove = dwc_otg_control_usb_remove,
516 .name = "rk3368-dwc-control-usb",
517 .owner = THIS_MODULE,
518 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
522 static int __init dwc_otg_control_usb_init(void)
526 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
530 subsys_initcall(dwc_otg_control_usb_init);
532 static void __exit dwc_otg_control_usb_exit(void)
534 platform_driver_unregister(&dwc_otg_control_usb_driver);
537 module_exit(dwc_otg_control_usb_exit);
538 MODULE_ALIAS("platform: dwc_control_usb");
539 MODULE_AUTHOR("RockChip Inc.");
540 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
541 MODULE_LICENSE("GPL v2");