2 #include "dwc_otg_regs.h"
4 static struct dwc_otg_control_usb *control_usb;
6 static u32 uoc_read(u32 reg)
10 regmap_read(control_usb->grf, reg, &val);
14 static void uoc_write(u32 value, u32 reg)
16 regmap_write(control_usb->grf, reg, value);
19 #ifdef CONFIG_USB20_OTG
20 static void usb20otg_hw_init(void)
22 /* Turn off differential receiver in suspend mode */
23 uoc_write(UOC_HIWORD_UPDATE(0, 1, 2), 0x798);
25 /* Set disconnect detection trigger point to 600mv */
26 uoc_write(UOC_HIWORD_UPDATE(0, 0xf, 11), 0x79c);
28 /* other haredware init,include:
29 * DRV_VBUS GPIO init */
30 if (gpio_is_valid(control_usb->otg_gpios->gpio))
31 gpio_set_value(control_usb->otg_gpios->gpio, 0);
34 static void usb20otg_phy_suspend(void *pdata, int suspend)
36 struct dwc_otg_platform_data *usbpdata = pdata;
38 /* enable soft control */
39 uoc_write(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0), 0x700);
40 usbpdata->phy_status = 1;
43 uoc_write(UOC_HIWORD_UPDATE(0x0, 0x1, 0), 0x700);
44 usbpdata->phy_status = 0;
48 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
50 struct dwc_otg_platform_data *usbpdata = pdata;
51 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
53 rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
54 rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
55 rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
56 if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
57 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
64 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x700);
65 reset_control_assert(rst_otg_p);
67 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x700);
69 reset_control_deassert(rst_otg_p);
72 /* Controller reset */
73 reset_control_assert(rst_otg_c);
74 reset_control_assert(rst_otg_h);
78 reset_control_deassert(rst_otg_c);
79 reset_control_deassert(rst_otg_h);
87 static void usb20otg_clock_init(void *pdata)
89 struct dwc_otg_platform_data *usbpdata = pdata;
90 struct clk *ahbclk, *phyclk;
92 ahbclk = devm_clk_get(usbpdata->dev, "hclk_otg");
94 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
98 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
100 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
104 usbpdata->phyclk = phyclk;
105 usbpdata->ahbclk = ahbclk;
108 static void usb20otg_clock_enable(void *pdata, int enable)
110 struct dwc_otg_platform_data *usbpdata = pdata;
113 clk_prepare_enable(usbpdata->ahbclk);
114 clk_prepare_enable(usbpdata->phyclk);
116 clk_disable_unprepare(usbpdata->ahbclk);
117 clk_disable_unprepare(usbpdata->phyclk);
121 static int usb20otg_get_status(int id)
124 u32 soc_status15 = uoc_read(0x4bc);
127 case USB_STATUS_BVABLID:
129 ret = soc_status15 & (0x1 << 23);
131 case USB_STATUS_DPDM:
133 ret = soc_status15 & (0x3 << 24);
137 ret = soc_status15 & (0x1 << 26);
140 ret = control_usb->chip_id;
142 case USB_REMOTE_WAKEUP:
143 ret = control_usb->remote_wakeup;
146 ret = control_usb->usb_irq_wakeup;
154 #ifdef CONFIG_RK_USB_UART
156 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
158 * Returns true if the status property of node "usb_uart" is set to "okay"
159 * or "ok", if this property is absent it will use the default status "ok"
162 static bool dwc_otg_uart_enabled(void)
167 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
171 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
176 static void usb20otg_power_enable(int enable)
179 /* disable otg_drv power */
180 if (gpio_is_valid(control_usb->otg_gpios->gpio))
181 gpio_set_value(control_usb->otg_gpios->gpio, 0);
182 } else if (1 == enable) {
183 /* enable otg_drv power */
184 if (gpio_is_valid(control_usb->otg_gpios->gpio))
185 gpio_set_value(control_usb->otg_gpios->gpio, 1);
189 struct dwc_otg_platform_data usb20otg_pdata_rk3368 = {
194 .hw_init = usb20otg_hw_init,
195 .phy_suspend = usb20otg_phy_suspend,
196 .soft_reset = usb20otg_soft_reset,
197 .clock_init = usb20otg_clock_init,
198 .clock_enable = usb20otg_clock_enable,
199 .get_status = usb20otg_get_status,
200 .power_enable = usb20otg_power_enable,
201 .dwc_otg_uart_mode = dwc_otg_uart_mode,
202 .bc_detect_cb = rk_battery_charger_detect_cb,
206 #ifdef CONFIG_USB_EHCI_RK
207 static void usb20ehci_hw_init(void)
209 /* Turn off differential receiver in suspend mode */
210 uoc_write(UOC_HIWORD_UPDATE(0, 1, 2), 0x7b8);
211 /* Set disconnect detection trigger point to 600mv */
212 uoc_write(UOC_HIWORD_UPDATE(1, 0xf, 11), 0x7bc);
214 /* other haredware init,include:
215 * DRV_VBUS GPIO init */
216 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
217 if (!gpio_get_value(control_usb->host_gpios->gpio))
218 gpio_set_value(control_usb->host_gpios->gpio, 1);
222 static void usb20ehci_phy_suspend(void *pdata, int suspend)
224 struct rkehci_platform_data *usbpdata = pdata;
227 /* enable soft control */
228 uoc_write(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0), 0x728);
229 usbpdata->phy_status = 1;
232 uoc_write(UOC_HIWORD_UPDATE(0x0, 0x1, 0), 0x728);
233 usbpdata->phy_status = 0;
237 static void usb20ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
239 struct rkehci_platform_data *usbpdata = pdata;
240 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
242 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
243 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
244 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
245 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
246 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
253 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x728);
254 reset_control_assert(rst_host_p);
256 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x728);
259 reset_control_deassert(rst_host_p);
261 /* Controller reset */
262 reset_control_assert(rst_host_c);
263 reset_control_assert(rst_host_h);
267 reset_control_deassert(rst_host_c);
268 reset_control_deassert(rst_host_h);
276 static void usb20ehci_clock_init(void *pdata)
280 static void usb20ehci_clock_enable(void *pdata, int enable)
284 static int usb20ehci_get_status(int id)
286 /* For HOST port in rk336x can not get any info from GRF */
290 struct rkehci_platform_data usb20ehci_pdata_rk3368 = {
294 .hw_init = usb20ehci_hw_init,
295 .phy_suspend = usb20ehci_phy_suspend,
296 .soft_reset = usb20ehci_soft_reset,
297 .clock_init = usb20ehci_clock_init,
298 .clock_enable = usb20ehci_clock_enable,
299 .get_status = usb20ehci_get_status,
303 struct dwc_otg_platform_data usb20ohci_pdata_rk3368;
306 static const struct of_device_id rk_usb_control_id_table[] = {
308 .compatible = "rockchip,rk3368-usb-control",
313 /*********************************************************************
314 rk3126 usb detections
315 *********************************************************************/
317 #define WAKE_LOCK_TIMEOUT (HZ * 10)
318 static inline void do_wakeup(struct work_struct *work)
320 /* wake up the system */
321 rk_send_wakeup_key();
324 static void usb_battery_charger_detect_work(struct work_struct *work)
326 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
329 /********** handler for bvalid irq **********/
330 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
333 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x6a0);
334 #ifdef CONFIG_RK_USB_UART
335 /* usb otg dp/dm switch to usb phy */
336 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
339 if (control_usb->usb_irq_wakeup) {
340 wake_lock_timeout(&control_usb->usb_wakelock,
342 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
346 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
351 /************* register usb detection irqs **************/
352 static int otg_irq_detect_init(struct platform_device *pdev)
357 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
359 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
361 /*register otg_bvalid irq */
362 irq = platform_get_irq_byname(pdev, "otg_bvalid");
363 if ((irq > 0) && control_usb->usb_irq_wakeup) {
364 ret = request_irq(irq, bvalid_irq_handler,
365 0, "otg_bvalid", NULL);
367 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
369 /* enable bvalid irq */
370 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x680);
377 /********** end of usb detections **********/
379 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
381 .compatible = "rockchip,rk3368-dwc-control-usb",
386 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
388 struct device *dev = &pdev->dev;
389 struct device_node *np = dev->of_node;
390 struct clk *hclk_usb_peri;
394 control_usb = devm_kzalloc(dev, sizeof(*control_usb), GFP_KERNEL);
396 dev_err(&pdev->dev, "Unable to alloc memory for control usb\n");
400 /* Init regmap GRF */
401 grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
403 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
406 control_usb->grf = grf;
408 /* Init Vbus-drv GPIOs */
409 control_usb->host_gpios =
410 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
411 if (!control_usb->host_gpios) {
412 dev_err(&pdev->dev, "Unable to alloc memory for host_gpios\n");
416 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
417 control_usb->host_gpios->gpio = gpio;
419 if (gpio_is_valid(gpio)) {
420 if (devm_gpio_request(&pdev->dev, gpio, "usb_host_drv")) {
422 "Failed to request GPIO%d for host_drv\n",
426 gpio_direction_output(control_usb->host_gpios->gpio, 1);
429 control_usb->otg_gpios =
430 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
431 if (!control_usb->otg_gpios) {
432 dev_err(&pdev->dev, "Unable to alloc memory for otg_gpios\n");
436 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
437 control_usb->otg_gpios->gpio = gpio;
439 if (gpio_is_valid(gpio)) {
440 if (devm_gpio_request(&pdev->dev, gpio, "usb_otg_drv")) {
442 "failed to request GPIO%d for otg_drv\n", gpio);
445 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
449 control_usb->remote_wakeup = of_property_read_bool(np,
450 "rockchip,remote_wakeup");
451 control_usb->usb_irq_wakeup = of_property_read_bool(np,
452 "rockchip,usb_irq_wakeup");
454 /* Init hclk_usb_peri */
455 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
456 if (IS_ERR(hclk_usb_peri)) {
457 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
458 return PTR_ERR(hclk_usb_peri);
460 control_usb->hclk_usb_peri = hclk_usb_peri;
461 clk_prepare_enable(hclk_usb_peri);
463 #ifdef CONFIG_USB20_OTG
464 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
465 usb_battery_charger_detect_work);
467 if (usb20otg_get_status(USB_STATUS_BVABLID))
468 schedule_delayed_work(&control_usb->usb_charger_det_work,
472 ret = otg_irq_detect_init(pdev);
479 clk_disable_unprepare(hclk_usb_peri);
483 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
485 clk_disable_unprepare(control_usb->hclk_usb_peri);
489 static struct platform_driver dwc_otg_control_usb_driver = {
490 .probe = dwc_otg_control_usb_probe,
491 .remove = dwc_otg_control_usb_remove,
493 .name = "rk3368-dwc-control-usb",
494 .owner = THIS_MODULE,
495 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
499 static int __init dwc_otg_control_usb_init(void)
503 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
507 subsys_initcall(dwc_otg_control_usb_init);
509 static void __exit dwc_otg_control_usb_exit(void)
511 platform_driver_unregister(&dwc_otg_control_usb_driver);
514 module_exit(dwc_otg_control_usb_exit);
515 MODULE_ALIAS("platform: dwc_control_usb");
516 MODULE_AUTHOR("RockChip Inc.");
517 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
518 MODULE_LICENSE("GPL v2");