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 600mv */
27 uoc_write(UOC_HIWORD_UPDATE(0, 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);
88 static void usb20otg_clock_init(void *pdata)
90 struct dwc_otg_platform_data *usbpdata = pdata;
91 struct clk *ahbclk, *phyclk;
93 ahbclk = devm_clk_get(usbpdata->dev, "hclk_otg");
95 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
99 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
100 if (IS_ERR(phyclk)) {
101 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
105 usbpdata->phyclk = phyclk;
106 usbpdata->ahbclk = ahbclk;
109 static void usb20otg_clock_enable(void *pdata, int enable)
111 struct dwc_otg_platform_data *usbpdata = pdata;
114 clk_prepare_enable(usbpdata->ahbclk);
115 clk_prepare_enable(usbpdata->phyclk);
117 clk_disable_unprepare(usbpdata->ahbclk);
118 clk_disable_unprepare(usbpdata->phyclk);
122 static int usb20otg_get_status(int id)
125 u32 soc_status15 = uoc_read(0x4bc);
128 case USB_STATUS_BVABLID:
130 ret = soc_status15 & (0x1 << 23);
132 case USB_STATUS_DPDM:
134 ret = soc_status15 & (0x3 << 24);
138 ret = soc_status15 & (0x1 << 26);
141 ret = control_usb->chip_id;
143 case USB_REMOTE_WAKEUP:
144 ret = control_usb->remote_wakeup;
147 ret = control_usb->usb_irq_wakeup;
155 #ifdef CONFIG_RK_USB_UART
157 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
159 * Returns true if the status property of node "usb_uart" is set to "okay"
160 * or "ok", if this property is absent it will use the default status "ok"
163 static bool dwc_otg_uart_enabled(void)
168 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
172 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
177 static void usb20otg_power_enable(int enable)
180 /* disable otg_drv power */
181 if (gpio_is_valid(control_usb->otg_gpios->gpio))
182 gpio_set_value(control_usb->otg_gpios->gpio, 0);
183 } else if (1 == enable) {
184 /* enable otg_drv power */
185 if (gpio_is_valid(control_usb->otg_gpios->gpio))
186 gpio_set_value(control_usb->otg_gpios->gpio, 1);
190 struct dwc_otg_platform_data usb20otg_pdata_rk3368 = {
195 .hw_init = usb20otg_hw_init,
196 .phy_suspend = usb20otg_phy_suspend,
197 .soft_reset = usb20otg_soft_reset,
198 .clock_init = usb20otg_clock_init,
199 .clock_enable = usb20otg_clock_enable,
200 .get_status = usb20otg_get_status,
201 .power_enable = usb20otg_power_enable,
202 .dwc_otg_uart_mode = dwc_otg_uart_mode,
203 .bc_detect_cb = rk_battery_charger_detect_cb,
207 #ifdef CONFIG_USB_EHCI_RK
208 static void usb20ehci_hw_init(void)
210 /* Turn off differential receiver in suspend mode */
211 uoc_write(UOC_HIWORD_UPDATE(0, 1, 2), 0x7b8);
212 /* Set disconnect detection trigger point to 600mv */
213 uoc_write(UOC_HIWORD_UPDATE(1, 0xf, 11), 0x7bc);
215 /* other haredware init,include:
216 * DRV_VBUS GPIO init */
217 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
218 if (!gpio_get_value(control_usb->host_gpios->gpio))
219 gpio_set_value(control_usb->host_gpios->gpio, 1);
223 static void usb20ehci_phy_suspend(void *pdata, int suspend)
225 struct rkehci_platform_data *usbpdata = pdata;
228 /* enable soft control */
229 uoc_write(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0), 0x728);
230 usbpdata->phy_status = 1;
233 uoc_write(UOC_HIWORD_UPDATE(0x0, 0x1, 0), 0x728);
234 usbpdata->phy_status = 0;
238 static void usb20ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
240 struct rkehci_platform_data *usbpdata = pdata;
241 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
243 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
244 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
245 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
246 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
247 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
254 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x728);
255 reset_control_assert(rst_host_p);
257 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x728);
260 reset_control_deassert(rst_host_p);
262 /* Controller reset */
263 reset_control_assert(rst_host_c);
264 reset_control_assert(rst_host_h);
268 reset_control_deassert(rst_host_c);
269 reset_control_deassert(rst_host_h);
277 static void usb20ehci_clock_init(void *pdata)
281 static void usb20ehci_clock_enable(void *pdata, int enable)
285 static int usb20ehci_get_status(int id)
287 /* For HOST port in rk336x can not get any info from GRF */
291 struct rkehci_platform_data usb20ehci_pdata_rk3368 = {
295 .hw_init = usb20ehci_hw_init,
296 .phy_suspend = usb20ehci_phy_suspend,
297 .soft_reset = usb20ehci_soft_reset,
298 .clock_init = usb20ehci_clock_init,
299 .clock_enable = usb20ehci_clock_enable,
300 .get_status = usb20ehci_get_status,
304 struct dwc_otg_platform_data usb20ohci_pdata_rk3368;
307 static const struct of_device_id rk_usb_control_id_table[] = {
309 .compatible = "rockchip,rk3368-usb-control",
314 /*********************************************************************
315 rk3126 usb detections
316 *********************************************************************/
318 #define WAKE_LOCK_TIMEOUT (HZ * 10)
319 static inline void do_wakeup(struct work_struct *work)
321 /* wake up the system */
322 rk_send_wakeup_key();
325 static void usb_battery_charger_detect_work(struct work_struct *work)
327 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
330 /********** handler for bvalid irq **********/
331 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
334 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x6a0);
335 #ifdef CONFIG_RK_USB_UART
336 /* usb otg dp/dm switch to usb phy */
337 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
340 if (control_usb->usb_irq_wakeup) {
341 wake_lock_timeout(&control_usb->usb_wakelock,
343 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
347 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
352 /************* register usb detection irqs **************/
353 static int otg_irq_detect_init(struct platform_device *pdev)
358 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
360 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
362 /*register otg_bvalid irq */
363 irq = platform_get_irq_byname(pdev, "otg_bvalid");
364 if ((irq > 0) && control_usb->usb_irq_wakeup) {
365 ret = request_irq(irq, bvalid_irq_handler,
366 0, "otg_bvalid", NULL);
368 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
370 /* enable bvalid irq */
371 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x680);
378 /********** end of usb detections **********/
380 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
382 .compatible = "rockchip,rk3368-dwc-control-usb",
387 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
389 struct device *dev = &pdev->dev;
390 struct device_node *np = dev->of_node;
391 struct clk *hclk_usb_peri;
395 control_usb = devm_kzalloc(dev, sizeof(*control_usb), GFP_KERNEL);
397 dev_err(&pdev->dev, "Unable to alloc memory for control usb\n");
401 /* Init regmap GRF */
402 grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
404 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
407 control_usb->grf = grf;
409 /* Init Vbus-drv GPIOs */
410 control_usb->host_gpios =
411 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
412 if (!control_usb->host_gpios) {
413 dev_err(&pdev->dev, "Unable to alloc memory for host_gpios\n");
417 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
418 control_usb->host_gpios->gpio = gpio;
420 if (gpio_is_valid(gpio)) {
421 if (devm_gpio_request(&pdev->dev, gpio, "usb_host_drv")) {
423 "Failed to request GPIO%d for host_drv\n",
427 gpio_direction_output(control_usb->host_gpios->gpio, 1);
430 control_usb->otg_gpios =
431 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
432 if (!control_usb->otg_gpios) {
433 dev_err(&pdev->dev, "Unable to alloc memory for otg_gpios\n");
437 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
438 control_usb->otg_gpios->gpio = gpio;
440 if (gpio_is_valid(gpio)) {
441 if (devm_gpio_request(&pdev->dev, gpio, "usb_otg_drv")) {
443 "failed to request GPIO%d for otg_drv\n", gpio);
446 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
450 control_usb->remote_wakeup = of_property_read_bool(np,
451 "rockchip,remote_wakeup");
452 control_usb->usb_irq_wakeup = of_property_read_bool(np,
453 "rockchip,usb_irq_wakeup");
455 /* Init hclk_usb_peri */
456 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
457 if (IS_ERR(hclk_usb_peri)) {
458 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
459 return PTR_ERR(hclk_usb_peri);
461 control_usb->hclk_usb_peri = hclk_usb_peri;
462 clk_prepare_enable(hclk_usb_peri);
464 #ifdef CONFIG_USB20_OTG
465 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
466 usb_battery_charger_detect_work);
468 if (usb20otg_get_status(USB_STATUS_BVABLID))
469 schedule_delayed_work(&control_usb->usb_charger_det_work,
473 ret = otg_irq_detect_init(pdev);
480 clk_disable_unprepare(hclk_usb_peri);
484 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
486 clk_disable_unprepare(control_usb->hclk_usb_peri);
490 static struct platform_driver dwc_otg_control_usb_driver = {
491 .probe = dwc_otg_control_usb_probe,
492 .remove = dwc_otg_control_usb_remove,
494 .name = "rk3368-dwc-control-usb",
495 .owner = THIS_MODULE,
496 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
500 static int __init dwc_otg_control_usb_init(void)
504 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
508 subsys_initcall(dwc_otg_control_usb_init);
510 static void __exit dwc_otg_control_usb_exit(void)
512 platform_driver_unregister(&dwc_otg_control_usb_driver);
515 module_exit(dwc_otg_control_usb_exit);
516 MODULE_ALIAS("platform: dwc_control_usb");
517 MODULE_AUTHOR("RockChip Inc.");
518 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
519 MODULE_LICENSE("GPL v2");