2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
3 * Author: Chao Xie <chao.xie@marvell.com>
4 * Neil Zhang <zhangwm@marvell.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
15 #include <linux/uaccess.h>
16 #include <linux/device.h>
17 #include <linux/proc_fs.h>
18 #include <linux/clk.h>
19 #include <linux/workqueue.h>
20 #include <linux/platform_device.h>
22 #include <linux/usb.h>
23 #include <linux/usb/ch9.h>
24 #include <linux/usb/otg.h>
25 #include <linux/usb/gadget.h>
26 #include <linux/usb/hcd.h>
27 #include <linux/platform_data/mv_usb.h>
29 #include "phy-mv-usb.h"
31 #define DRIVER_DESC "Marvell USB OTG transceiver driver"
33 MODULE_DESCRIPTION(DRIVER_DESC);
34 MODULE_LICENSE("GPL");
36 static const char driver_name[] = "mv-otg";
38 static char *state_string[] = {
55 static int mv_otg_set_vbus(struct usb_otg *otg, bool on)
57 struct mv_otg *mvotg = container_of(otg->usb_phy, struct mv_otg, phy);
58 if (mvotg->pdata->set_vbus == NULL)
61 return mvotg->pdata->set_vbus(on);
64 static int mv_otg_set_host(struct usb_otg *otg,
72 static int mv_otg_set_peripheral(struct usb_otg *otg,
73 struct usb_gadget *gadget)
80 static void mv_otg_run_state_machine(struct mv_otg *mvotg,
83 dev_dbg(&mvotg->pdev->dev, "transceiver is updated\n");
87 queue_delayed_work(mvotg->qwork, &mvotg->work, delay);
90 static void mv_otg_timer_await_bcon(unsigned long data)
92 struct mv_otg *mvotg = (struct mv_otg *) data;
94 mvotg->otg_ctrl.a_wait_bcon_timeout = 1;
96 dev_info(&mvotg->pdev->dev, "B Device No Response!\n");
98 if (spin_trylock(&mvotg->wq_lock)) {
99 mv_otg_run_state_machine(mvotg, 0);
100 spin_unlock(&mvotg->wq_lock);
104 static int mv_otg_cancel_timer(struct mv_otg *mvotg, unsigned int id)
106 struct timer_list *timer;
108 if (id >= OTG_TIMER_NUM)
111 timer = &mvotg->otg_ctrl.timer[id];
113 if (timer_pending(timer))
119 static int mv_otg_set_timer(struct mv_otg *mvotg, unsigned int id,
120 unsigned long interval,
121 void (*callback) (unsigned long))
123 struct timer_list *timer;
125 if (id >= OTG_TIMER_NUM)
128 timer = &mvotg->otg_ctrl.timer[id];
129 if (timer_pending(timer)) {
130 dev_err(&mvotg->pdev->dev, "Timer%d is already running\n", id);
135 timer->data = (unsigned long) mvotg;
136 timer->function = callback;
137 timer->expires = jiffies + interval;
143 static int mv_otg_reset(struct mv_otg *mvotg)
148 /* Stop the controller */
149 tmp = readl(&mvotg->op_regs->usbcmd);
150 tmp &= ~USBCMD_RUN_STOP;
151 writel(tmp, &mvotg->op_regs->usbcmd);
153 /* Reset the controller to get default values */
154 writel(USBCMD_CTRL_RESET, &mvotg->op_regs->usbcmd);
157 while (readl(&mvotg->op_regs->usbcmd) & USBCMD_CTRL_RESET) {
159 dev_err(&mvotg->pdev->dev,
160 "Wait for RESET completed TIMEOUT\n");
167 writel(0x0, &mvotg->op_regs->usbintr);
168 tmp = readl(&mvotg->op_regs->usbsts);
169 writel(tmp, &mvotg->op_regs->usbsts);
174 static void mv_otg_init_irq(struct mv_otg *mvotg)
178 mvotg->irq_en = OTGSC_INTR_A_SESSION_VALID
179 | OTGSC_INTR_A_VBUS_VALID;
180 mvotg->irq_status = OTGSC_INTSTS_A_SESSION_VALID
181 | OTGSC_INTSTS_A_VBUS_VALID;
183 if (mvotg->pdata->vbus == NULL) {
184 mvotg->irq_en |= OTGSC_INTR_B_SESSION_VALID
185 | OTGSC_INTR_B_SESSION_END;
186 mvotg->irq_status |= OTGSC_INTSTS_B_SESSION_VALID
187 | OTGSC_INTSTS_B_SESSION_END;
190 if (mvotg->pdata->id == NULL) {
191 mvotg->irq_en |= OTGSC_INTR_USB_ID;
192 mvotg->irq_status |= OTGSC_INTSTS_USB_ID;
195 otgsc = readl(&mvotg->op_regs->otgsc);
196 otgsc |= mvotg->irq_en;
197 writel(otgsc, &mvotg->op_regs->otgsc);
200 static void mv_otg_start_host(struct mv_otg *mvotg, int on)
203 struct usb_otg *otg = mvotg->phy.otg;
209 dev_info(&mvotg->pdev->dev, "%s host\n", on ? "start" : "stop");
211 hcd = bus_to_hcd(otg->host);
214 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
215 device_wakeup_enable(hcd->self.controller);
219 #endif /* CONFIG_USB */
222 static void mv_otg_start_periphrals(struct mv_otg *mvotg, int on)
224 struct usb_otg *otg = mvotg->phy.otg;
229 dev_info(mvotg->phy.dev, "gadget %s\n", on ? "on" : "off");
232 usb_gadget_vbus_connect(otg->gadget);
234 usb_gadget_vbus_disconnect(otg->gadget);
237 static void otg_clock_enable(struct mv_otg *mvotg)
239 clk_prepare_enable(mvotg->clk);
242 static void otg_clock_disable(struct mv_otg *mvotg)
244 clk_disable_unprepare(mvotg->clk);
247 static int mv_otg_enable_internal(struct mv_otg *mvotg)
254 dev_dbg(&mvotg->pdev->dev, "otg enabled\n");
256 otg_clock_enable(mvotg);
257 if (mvotg->pdata->phy_init) {
258 retval = mvotg->pdata->phy_init(mvotg->phy_regs);
260 dev_err(&mvotg->pdev->dev,
261 "init phy error %d\n", retval);
262 otg_clock_disable(mvotg);
272 static int mv_otg_enable(struct mv_otg *mvotg)
274 if (mvotg->clock_gating)
275 return mv_otg_enable_internal(mvotg);
280 static void mv_otg_disable_internal(struct mv_otg *mvotg)
283 dev_dbg(&mvotg->pdev->dev, "otg disabled\n");
284 if (mvotg->pdata->phy_deinit)
285 mvotg->pdata->phy_deinit(mvotg->phy_regs);
286 otg_clock_disable(mvotg);
291 static void mv_otg_disable(struct mv_otg *mvotg)
293 if (mvotg->clock_gating)
294 mv_otg_disable_internal(mvotg);
297 static void mv_otg_update_inputs(struct mv_otg *mvotg)
299 struct mv_otg_ctrl *otg_ctrl = &mvotg->otg_ctrl;
302 otgsc = readl(&mvotg->op_regs->otgsc);
304 if (mvotg->pdata->vbus) {
305 if (mvotg->pdata->vbus->poll() == VBUS_HIGH) {
306 otg_ctrl->b_sess_vld = 1;
307 otg_ctrl->b_sess_end = 0;
309 otg_ctrl->b_sess_vld = 0;
310 otg_ctrl->b_sess_end = 1;
313 otg_ctrl->b_sess_vld = !!(otgsc & OTGSC_STS_B_SESSION_VALID);
314 otg_ctrl->b_sess_end = !!(otgsc & OTGSC_STS_B_SESSION_END);
317 if (mvotg->pdata->id)
318 otg_ctrl->id = !!mvotg->pdata->id->poll();
320 otg_ctrl->id = !!(otgsc & OTGSC_STS_USB_ID);
322 if (mvotg->pdata->otg_force_a_bus_req && !otg_ctrl->id)
323 otg_ctrl->a_bus_req = 1;
325 otg_ctrl->a_sess_vld = !!(otgsc & OTGSC_STS_A_SESSION_VALID);
326 otg_ctrl->a_vbus_vld = !!(otgsc & OTGSC_STS_A_VBUS_VALID);
328 dev_dbg(&mvotg->pdev->dev, "%s: ", __func__);
329 dev_dbg(&mvotg->pdev->dev, "id %d\n", otg_ctrl->id);
330 dev_dbg(&mvotg->pdev->dev, "b_sess_vld %d\n", otg_ctrl->b_sess_vld);
331 dev_dbg(&mvotg->pdev->dev, "b_sess_end %d\n", otg_ctrl->b_sess_end);
332 dev_dbg(&mvotg->pdev->dev, "a_vbus_vld %d\n", otg_ctrl->a_vbus_vld);
333 dev_dbg(&mvotg->pdev->dev, "a_sess_vld %d\n", otg_ctrl->a_sess_vld);
336 static void mv_otg_update_state(struct mv_otg *mvotg)
338 struct mv_otg_ctrl *otg_ctrl = &mvotg->otg_ctrl;
339 int old_state = mvotg->phy.otg->state;
342 case OTG_STATE_UNDEFINED:
343 mvotg->phy.otg->state = OTG_STATE_B_IDLE;
345 case OTG_STATE_B_IDLE:
346 if (otg_ctrl->id == 0)
347 mvotg->phy.otg->state = OTG_STATE_A_IDLE;
348 else if (otg_ctrl->b_sess_vld)
349 mvotg->phy.otg->state = OTG_STATE_B_PERIPHERAL;
351 case OTG_STATE_B_PERIPHERAL:
352 if (!otg_ctrl->b_sess_vld || otg_ctrl->id == 0)
353 mvotg->phy.otg->state = OTG_STATE_B_IDLE;
355 case OTG_STATE_A_IDLE:
357 mvotg->phy.otg->state = OTG_STATE_B_IDLE;
358 else if (!(otg_ctrl->a_bus_drop) &&
359 (otg_ctrl->a_bus_req || otg_ctrl->a_srp_det))
360 mvotg->phy.otg->state = OTG_STATE_A_WAIT_VRISE;
362 case OTG_STATE_A_WAIT_VRISE:
363 if (otg_ctrl->a_vbus_vld)
364 mvotg->phy.otg->state = OTG_STATE_A_WAIT_BCON;
366 case OTG_STATE_A_WAIT_BCON:
367 if (otg_ctrl->id || otg_ctrl->a_bus_drop
368 || otg_ctrl->a_wait_bcon_timeout) {
369 mv_otg_cancel_timer(mvotg, A_WAIT_BCON_TIMER);
370 mvotg->otg_ctrl.a_wait_bcon_timeout = 0;
371 mvotg->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
372 otg_ctrl->a_bus_req = 0;
373 } else if (!otg_ctrl->a_vbus_vld) {
374 mv_otg_cancel_timer(mvotg, A_WAIT_BCON_TIMER);
375 mvotg->otg_ctrl.a_wait_bcon_timeout = 0;
376 mvotg->phy.otg->state = OTG_STATE_A_VBUS_ERR;
377 } else if (otg_ctrl->b_conn) {
378 mv_otg_cancel_timer(mvotg, A_WAIT_BCON_TIMER);
379 mvotg->otg_ctrl.a_wait_bcon_timeout = 0;
380 mvotg->phy.otg->state = OTG_STATE_A_HOST;
383 case OTG_STATE_A_HOST:
384 if (otg_ctrl->id || !otg_ctrl->b_conn
385 || otg_ctrl->a_bus_drop)
386 mvotg->phy.otg->state = OTG_STATE_A_WAIT_BCON;
387 else if (!otg_ctrl->a_vbus_vld)
388 mvotg->phy.otg->state = OTG_STATE_A_VBUS_ERR;
390 case OTG_STATE_A_WAIT_VFALL:
392 || (!otg_ctrl->b_conn && otg_ctrl->a_sess_vld)
393 || otg_ctrl->a_bus_req)
394 mvotg->phy.otg->state = OTG_STATE_A_IDLE;
396 case OTG_STATE_A_VBUS_ERR:
397 if (otg_ctrl->id || otg_ctrl->a_clr_err
398 || otg_ctrl->a_bus_drop) {
399 otg_ctrl->a_clr_err = 0;
400 mvotg->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
408 static void mv_otg_work(struct work_struct *work)
410 struct mv_otg *mvotg;
415 mvotg = container_of(to_delayed_work(work), struct mv_otg, work);
418 /* work queue is single thread, or we need spin_lock to protect */
420 otg = mvotg->phy.otg;
421 old_state = otg->state;
426 mv_otg_update_inputs(mvotg);
427 mv_otg_update_state(mvotg);
429 if (old_state != mvotg->phy.otg->state) {
430 dev_info(&mvotg->pdev->dev, "change from state %s to %s\n",
431 state_string[old_state],
432 state_string[mvotg->phy.otg->state]);
434 switch (mvotg->phy.otg->state) {
435 case OTG_STATE_B_IDLE:
437 if (old_state == OTG_STATE_B_PERIPHERAL)
438 mv_otg_start_periphrals(mvotg, 0);
440 mv_otg_disable(mvotg);
441 usb_phy_set_event(&mvotg->phy, USB_EVENT_NONE);
443 case OTG_STATE_B_PERIPHERAL:
444 mv_otg_enable(mvotg);
445 mv_otg_start_periphrals(mvotg, 1);
446 usb_phy_set_event(&mvotg->phy, USB_EVENT_ENUMERATED);
448 case OTG_STATE_A_IDLE:
450 mv_otg_enable(mvotg);
451 if (old_state == OTG_STATE_A_WAIT_VFALL)
452 mv_otg_start_host(mvotg, 0);
455 case OTG_STATE_A_WAIT_VRISE:
456 mv_otg_set_vbus(otg, 1);
458 case OTG_STATE_A_WAIT_BCON:
459 if (old_state != OTG_STATE_A_HOST)
460 mv_otg_start_host(mvotg, 1);
461 mv_otg_set_timer(mvotg, A_WAIT_BCON_TIMER,
463 mv_otg_timer_await_bcon);
465 * Now, we directly enter A_HOST. So set b_conn = 1
466 * here. In fact, it need host driver to notify us.
468 mvotg->otg_ctrl.b_conn = 1;
470 case OTG_STATE_A_HOST:
472 case OTG_STATE_A_WAIT_VFALL:
474 * Now, we has exited A_HOST. So set b_conn = 0
475 * here. In fact, it need host driver to notify us.
477 mvotg->otg_ctrl.b_conn = 0;
478 mv_otg_set_vbus(otg, 0);
480 case OTG_STATE_A_VBUS_ERR:
489 static irqreturn_t mv_otg_irq(int irq, void *dev)
491 struct mv_otg *mvotg = dev;
494 otgsc = readl(&mvotg->op_regs->otgsc);
495 writel(otgsc, &mvotg->op_regs->otgsc);
498 * if we have vbus, then the vbus detection for B-device
499 * will be done by mv_otg_inputs_irq().
501 if (mvotg->pdata->vbus)
502 if ((otgsc & OTGSC_STS_USB_ID) &&
503 !(otgsc & OTGSC_INTSTS_USB_ID))
506 if ((otgsc & mvotg->irq_status) == 0)
509 mv_otg_run_state_machine(mvotg, 0);
514 static irqreturn_t mv_otg_inputs_irq(int irq, void *dev)
516 struct mv_otg *mvotg = dev;
518 /* The clock may disabled at this time */
519 if (!mvotg->active) {
520 mv_otg_enable(mvotg);
521 mv_otg_init_irq(mvotg);
524 mv_otg_run_state_machine(mvotg, 0);
530 get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
532 struct mv_otg *mvotg = dev_get_drvdata(dev);
533 return scnprintf(buf, PAGE_SIZE, "%d\n",
534 mvotg->otg_ctrl.a_bus_req);
538 set_a_bus_req(struct device *dev, struct device_attribute *attr,
539 const char *buf, size_t count)
541 struct mv_otg *mvotg = dev_get_drvdata(dev);
546 /* We will use this interface to change to A device */
547 if (mvotg->phy.otg->state != OTG_STATE_B_IDLE
548 && mvotg->phy.otg->state != OTG_STATE_A_IDLE)
551 /* The clock may disabled and we need to set irq for ID detected */
552 mv_otg_enable(mvotg);
553 mv_otg_init_irq(mvotg);
556 mvotg->otg_ctrl.a_bus_req = 1;
557 mvotg->otg_ctrl.a_bus_drop = 0;
558 dev_dbg(&mvotg->pdev->dev,
559 "User request: a_bus_req = 1\n");
561 if (spin_trylock(&mvotg->wq_lock)) {
562 mv_otg_run_state_machine(mvotg, 0);
563 spin_unlock(&mvotg->wq_lock);
570 static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req,
574 set_a_clr_err(struct device *dev, struct device_attribute *attr,
575 const char *buf, size_t count)
577 struct mv_otg *mvotg = dev_get_drvdata(dev);
578 if (!mvotg->phy.otg->default_a)
585 mvotg->otg_ctrl.a_clr_err = 1;
586 dev_dbg(&mvotg->pdev->dev,
587 "User request: a_clr_err = 1\n");
590 if (spin_trylock(&mvotg->wq_lock)) {
591 mv_otg_run_state_machine(mvotg, 0);
592 spin_unlock(&mvotg->wq_lock);
598 static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err);
601 get_a_bus_drop(struct device *dev, struct device_attribute *attr,
604 struct mv_otg *mvotg = dev_get_drvdata(dev);
605 return scnprintf(buf, PAGE_SIZE, "%d\n",
606 mvotg->otg_ctrl.a_bus_drop);
610 set_a_bus_drop(struct device *dev, struct device_attribute *attr,
611 const char *buf, size_t count)
613 struct mv_otg *mvotg = dev_get_drvdata(dev);
614 if (!mvotg->phy.otg->default_a)
621 mvotg->otg_ctrl.a_bus_drop = 0;
622 dev_dbg(&mvotg->pdev->dev,
623 "User request: a_bus_drop = 0\n");
624 } else if (buf[0] == '1') {
625 mvotg->otg_ctrl.a_bus_drop = 1;
626 mvotg->otg_ctrl.a_bus_req = 0;
627 dev_dbg(&mvotg->pdev->dev,
628 "User request: a_bus_drop = 1\n");
629 dev_dbg(&mvotg->pdev->dev,
630 "User request: and a_bus_req = 0\n");
633 if (spin_trylock(&mvotg->wq_lock)) {
634 mv_otg_run_state_machine(mvotg, 0);
635 spin_unlock(&mvotg->wq_lock);
641 static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR,
642 get_a_bus_drop, set_a_bus_drop);
644 static struct attribute *inputs_attrs[] = {
645 &dev_attr_a_bus_req.attr,
646 &dev_attr_a_clr_err.attr,
647 &dev_attr_a_bus_drop.attr,
651 static const struct attribute_group inputs_attr_group = {
653 .attrs = inputs_attrs,
656 static int mv_otg_remove(struct platform_device *pdev)
658 struct mv_otg *mvotg = platform_get_drvdata(pdev);
660 sysfs_remove_group(&mvotg->pdev->dev.kobj, &inputs_attr_group);
663 flush_workqueue(mvotg->qwork);
664 destroy_workqueue(mvotg->qwork);
667 mv_otg_disable(mvotg);
669 usb_remove_phy(&mvotg->phy);
674 static int mv_otg_probe(struct platform_device *pdev)
676 struct mv_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
677 struct mv_otg *mvotg;
683 dev_err(&pdev->dev, "failed to get platform data\n");
687 mvotg = devm_kzalloc(&pdev->dev, sizeof(*mvotg), GFP_KERNEL);
691 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
695 platform_set_drvdata(pdev, mvotg);
698 mvotg->pdata = pdata;
700 mvotg->clk = devm_clk_get(&pdev->dev, NULL);
701 if (IS_ERR(mvotg->clk))
702 return PTR_ERR(mvotg->clk);
704 mvotg->qwork = create_singlethread_workqueue("mv_otg_queue");
706 dev_dbg(&pdev->dev, "cannot create workqueue for OTG\n");
710 INIT_DELAYED_WORK(&mvotg->work, mv_otg_work);
712 /* OTG common part */
714 mvotg->phy.dev = &pdev->dev;
715 mvotg->phy.otg = otg;
716 mvotg->phy.label = driver_name;
718 otg->state = OTG_STATE_UNDEFINED;
719 otg->usb_phy = &mvotg->phy;
720 otg->set_host = mv_otg_set_host;
721 otg->set_peripheral = mv_otg_set_peripheral;
722 otg->set_vbus = mv_otg_set_vbus;
724 for (i = 0; i < OTG_TIMER_NUM; i++)
725 init_timer(&mvotg->otg_ctrl.timer[i]);
727 r = platform_get_resource_byname(mvotg->pdev,
728 IORESOURCE_MEM, "phyregs");
730 dev_err(&pdev->dev, "no phy I/O memory resource defined\n");
732 goto err_destroy_workqueue;
735 mvotg->phy_regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
736 if (mvotg->phy_regs == NULL) {
737 dev_err(&pdev->dev, "failed to map phy I/O memory\n");
739 goto err_destroy_workqueue;
742 r = platform_get_resource_byname(mvotg->pdev,
743 IORESOURCE_MEM, "capregs");
745 dev_err(&pdev->dev, "no I/O memory resource defined\n");
747 goto err_destroy_workqueue;
750 mvotg->cap_regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
751 if (mvotg->cap_regs == NULL) {
752 dev_err(&pdev->dev, "failed to map I/O memory\n");
754 goto err_destroy_workqueue;
757 /* we will acces controller register, so enable the udc controller */
758 retval = mv_otg_enable_internal(mvotg);
760 dev_err(&pdev->dev, "mv otg enable error %d\n", retval);
761 goto err_destroy_workqueue;
765 (struct mv_otg_regs __iomem *) ((unsigned long) mvotg->cap_regs
766 + (readl(mvotg->cap_regs) & CAPLENGTH_MASK));
769 retval = devm_request_threaded_irq(&pdev->dev, pdata->id->irq,
770 NULL, mv_otg_inputs_irq,
771 IRQF_ONESHOT, "id", mvotg);
774 "Failed to request irq for ID\n");
780 mvotg->clock_gating = 1;
781 retval = devm_request_threaded_irq(&pdev->dev, pdata->vbus->irq,
782 NULL, mv_otg_inputs_irq,
783 IRQF_ONESHOT, "vbus", mvotg);
786 "Failed to request irq for VBUS, "
787 "disable clock gating\n");
788 mvotg->clock_gating = 0;
793 if (pdata->disable_otg_clock_gating)
794 mvotg->clock_gating = 0;
797 mv_otg_init_irq(mvotg);
799 r = platform_get_resource(mvotg->pdev, IORESOURCE_IRQ, 0);
801 dev_err(&pdev->dev, "no IRQ resource defined\n");
803 goto err_disable_clk;
806 mvotg->irq = r->start;
807 if (devm_request_irq(&pdev->dev, mvotg->irq, mv_otg_irq, IRQF_SHARED,
808 driver_name, mvotg)) {
809 dev_err(&pdev->dev, "Request irq %d for OTG failed\n",
813 goto err_disable_clk;
816 retval = usb_add_phy(&mvotg->phy, USB_PHY_TYPE_USB2);
818 dev_err(&pdev->dev, "can't register transceiver, %d\n",
820 goto err_disable_clk;
823 retval = sysfs_create_group(&pdev->dev.kobj, &inputs_attr_group);
826 "Can't register sysfs attr group: %d\n", retval);
830 spin_lock_init(&mvotg->wq_lock);
831 if (spin_trylock(&mvotg->wq_lock)) {
832 mv_otg_run_state_machine(mvotg, 2 * HZ);
833 spin_unlock(&mvotg->wq_lock);
837 "successful probe OTG device %s clock gating.\n",
838 mvotg->clock_gating ? "with" : "without");
843 usb_remove_phy(&mvotg->phy);
845 mv_otg_disable_internal(mvotg);
846 err_destroy_workqueue:
847 flush_workqueue(mvotg->qwork);
848 destroy_workqueue(mvotg->qwork);
854 static int mv_otg_suspend(struct platform_device *pdev, pm_message_t state)
856 struct mv_otg *mvotg = platform_get_drvdata(pdev);
858 if (mvotg->phy.otg->state != OTG_STATE_B_IDLE) {
860 "OTG state is not B_IDLE, it is %d!\n",
861 mvotg->phy.otg->state);
865 if (!mvotg->clock_gating)
866 mv_otg_disable_internal(mvotg);
871 static int mv_otg_resume(struct platform_device *pdev)
873 struct mv_otg *mvotg = platform_get_drvdata(pdev);
876 if (!mvotg->clock_gating) {
877 mv_otg_enable_internal(mvotg);
879 otgsc = readl(&mvotg->op_regs->otgsc);
880 otgsc |= mvotg->irq_en;
881 writel(otgsc, &mvotg->op_regs->otgsc);
883 if (spin_trylock(&mvotg->wq_lock)) {
884 mv_otg_run_state_machine(mvotg, 0);
885 spin_unlock(&mvotg->wq_lock);
892 static struct platform_driver mv_otg_driver = {
893 .probe = mv_otg_probe,
894 .remove = mv_otg_remove,
899 .suspend = mv_otg_suspend,
900 .resume = mv_otg_resume,
903 module_platform_driver(mv_otg_driver);