12 #include <linux/module.h>
13 #include <linux/kernel.h>
17 #include <linux/device.h>
24 #include <linux/usb/ch9.h>
32 #define DRIVER_DESC "Marvell USB OTG transceiver driver"
33 #define DRIVER_VERSION "Jan 20, 2010"
41 static char *state_string[] = {
58 static int mv_otg_set_vbus(
struct usb_otg *otg,
bool on)
64 return mvotg->
pdata->set_vbus(on);
67 static int mv_otg_set_host(
struct usb_otg *otg,
75 static int mv_otg_set_peripheral(
struct usb_otg *otg,
83 static void mv_otg_run_state_machine(
struct mv_otg *mvotg,
86 dev_dbg(&mvotg->
pdev->dev,
"transceiver is updated\n");
93 static void mv_otg_timer_await_bcon(
unsigned long data)
97 mvotg->
otg_ctrl.a_wait_bcon_timeout = 1;
101 if (spin_trylock(&mvotg->
wq_lock)) {
102 mv_otg_run_state_machine(mvotg, 0);
107 static int mv_otg_cancel_timer(
struct mv_otg *mvotg,
unsigned int id)
116 if (timer_pending(timer))
122 static int mv_otg_set_timer(
struct mv_otg *mvotg,
unsigned int id,
132 if (timer_pending(timer)) {
133 dev_err(&mvotg->
pdev->dev,
"Timer%d is already running\n",
id);
138 timer->
data = (
unsigned long) mvotg;
146 static int mv_otg_reset(
struct mv_otg *mvotg)
163 "Wait for RESET completed TIMEOUT\n");
177 static void mv_otg_init_irq(
struct mv_otg *mvotg)
203 static void mv_otg_start_host(
struct mv_otg *mvotg,
int on)
212 dev_info(&mvotg->
pdev->dev,
"%s host\n", on ?
"start" :
"stop");
214 hcd = bus_to_hcd(otg->
host);
223 static void mv_otg_start_periphrals(
struct mv_otg *mvotg,
int on)
230 dev_info(mvotg->
phy.dev,
"gadget %s\n", on ?
"on" :
"off");
233 usb_gadget_vbus_connect(otg->
gadget);
235 usb_gadget_vbus_disconnect(otg->
gadget);
238 static void otg_clock_enable(
struct mv_otg *mvotg)
242 for (i = 0; i < mvotg->
clknum; i++)
246 static void otg_clock_disable(
struct mv_otg *mvotg)
250 for (i = 0; i < mvotg->
clknum; i++)
254 static int mv_otg_enable_internal(
struct mv_otg *mvotg)
263 otg_clock_enable(mvotg);
264 if (mvotg->
pdata->phy_init) {
268 "init phy error %d\n", retval);
269 otg_clock_disable(mvotg);
279 static int mv_otg_enable(
struct mv_otg *mvotg)
282 return mv_otg_enable_internal(mvotg);
287 static void mv_otg_disable_internal(
struct mv_otg *mvotg)
291 if (mvotg->
pdata->phy_deinit)
293 otg_clock_disable(mvotg);
298 static void mv_otg_disable(
struct mv_otg *mvotg)
301 mv_otg_disable_internal(mvotg);
304 static void mv_otg_update_inputs(
struct mv_otg *mvotg)
311 if (mvotg->
pdata->vbus) {
324 if (mvotg->
pdata->id)
325 otg_ctrl->
id = !!mvotg->
pdata->id->poll();
329 if (mvotg->
pdata->otg_force_a_bus_req && !otg_ctrl->
id)
343 static void mv_otg_update_state(
struct mv_otg *mvotg)
347 int old_state = phy->
state;
354 if (otg_ctrl->
id == 0)
378 mvotg->
otg_ctrl.a_wait_bcon_timeout = 0;
383 mvotg->
otg_ctrl.a_wait_bcon_timeout = 0;
385 }
else if (otg_ctrl->
b_conn) {
387 mvotg->
otg_ctrl.a_wait_bcon_timeout = 0;
392 if (otg_ctrl->
id || !otg_ctrl->
b_conn
429 old_state = phy->
state;
434 mv_otg_update_inputs(mvotg);
435 mv_otg_update_state(mvotg);
437 if (old_state != phy->
state) {
438 dev_info(&mvotg->
pdev->dev,
"change from state %s to %s\n",
439 state_string[old_state],
440 state_string[phy->
state]);
442 switch (phy->
state) {
446 mv_otg_start_periphrals(mvotg, 0);
448 mv_otg_disable(mvotg);
451 mv_otg_enable(mvotg);
452 mv_otg_start_periphrals(mvotg, 1);
456 mv_otg_enable(mvotg);
458 mv_otg_start_host(mvotg, 0);
462 mv_otg_set_vbus(otg, 1);
466 mv_otg_start_host(mvotg, 1);
469 mv_otg_timer_await_bcon);
484 mv_otg_set_vbus(otg, 0);
507 if (mvotg->
pdata->vbus)
515 mv_otg_run_state_machine(mvotg, 0);
520 static irqreturn_t mv_otg_inputs_irq(
int irq,
void *dev)
526 mv_otg_enable(mvotg);
527 mv_otg_init_irq(mvotg);
530 mv_otg_run_state_machine(mvotg, 0);
558 mv_otg_enable(mvotg);
559 mv_otg_init_irq(mvotg);
565 "User request: a_bus_req = 1\n");
567 if (spin_trylock(&mvotg->
wq_lock)) {
568 mv_otg_run_state_machine(mvotg, 0);
581 const char *buf,
size_t count)
584 if (!mvotg->
phy.otg->default_a)
593 "User request: a_clr_err = 1\n");
596 if (spin_trylock(&mvotg->
wq_lock)) {
597 mv_otg_run_state_machine(mvotg, 0);
617 const char *buf,
size_t count)
620 if (!mvotg->
phy.otg->default_a)
629 "User request: a_bus_drop = 0\n");
630 }
else if (buf[0] ==
'1') {
634 "User request: a_bus_drop = 1\n");
636 "User request: and a_bus_req = 0\n");
639 if (spin_trylock(&mvotg->
wq_lock)) {
640 mv_otg_run_state_machine(mvotg, 0);
648 get_a_bus_drop, set_a_bus_drop);
650 static struct attribute *inputs_attrs[] = {
651 &dev_attr_a_bus_req.attr,
652 &dev_attr_a_clr_err.attr,
653 &dev_attr_a_bus_drop.attr,
659 .attrs = inputs_attrs,
664 struct mv_otg *mvotg = platform_get_drvdata(pdev);
672 if (mvotg->
pdata->vbus)
674 if (mvotg->
pdata->id)
682 mv_otg_disable(mvotg);
690 for (clk_i = 0; clk_i <= mvotg->
clknum; clk_i++)
694 platform_set_drvdata(pdev,
NULL);
708 int retval = 0, clk_i,
i;
712 dev_err(&pdev->
dev,
"failed to get platform data\n");
716 size =
sizeof(*mvotg) +
sizeof(
struct clk *) * pdata->
clknum;
719 dev_err(&pdev->
dev,
"failed to allocate memory!\n");
729 platform_set_drvdata(pdev, mvotg);
735 for (clk_i = 0; clk_i < mvotg->
clknum; clk_i++) {
737 if (IS_ERR(mvotg->
clk[clk_i])) {
738 retval = PTR_ERR(mvotg->
clk[clk_i]);
745 dev_dbg(&pdev->
dev,
"cannot create workqueue for OTG\n");
754 mvotg->
phy.dev = &pdev->
dev;
755 mvotg->
phy.otg = otg;
770 dev_err(&pdev->
dev,
"no phy I/O memory resource defined\n");
772 goto err_destroy_workqueue;
777 dev_err(&pdev->
dev,
"failed to map phy I/O memory\n");
779 goto err_destroy_workqueue;
785 dev_err(&pdev->
dev,
"no I/O memory resource defined\n");
787 goto err_unmap_phyreg;
792 dev_err(&pdev->
dev,
"failed to map I/O memory\n");
794 goto err_unmap_phyreg;
798 retval = mv_otg_enable_internal(mvotg);
800 dev_err(&pdev->
dev,
"mv otg enable error %d\n", retval);
801 goto err_unmap_capreg;
814 "Failed to request irq for ID\n");
826 "Failed to request irq for VBUS, "
827 "disable clock gating\n");
837 mv_otg_init_irq(mvotg);
841 dev_err(&pdev->
dev,
"no IRQ resource defined\n");
843 goto err_disable_clk;
849 dev_err(&pdev->
dev,
"Request irq %d for OTG failed\n",
853 goto err_disable_clk;
858 dev_err(&pdev->
dev,
"can't register transceiver, %d\n",
866 "Can't register sysfs attr group: %d\n", retval);
867 goto err_set_transceiver;
871 if (spin_trylock(&mvotg->
wq_lock)) {
872 mv_otg_run_state_machine(mvotg, 2 *
HZ);
877 "successful probe OTG device %s clock gating.\n",
891 mv_otg_disable_internal(mvotg);
896 err_destroy_workqueue:
900 for (clk_i--; clk_i >= 0; clk_i--)
903 platform_set_drvdata(pdev,
NULL);
913 struct mv_otg *mvotg = platform_get_drvdata(pdev);
917 "OTG state is not B_IDLE, it is %d!\n",
923 mv_otg_disable_internal(mvotg);
930 struct mv_otg *mvotg = platform_get_drvdata(pdev);
934 mv_otg_enable_internal(mvotg);
940 if (spin_trylock(&mvotg->
wq_lock)) {
941 mv_otg_run_state_machine(mvotg, 0);
950 .probe = mv_otg_probe,
957 .suspend = mv_otg_suspend,
958 .resume = mv_otg_resume,
962 static int __init mv_otg_init(
void)
967 static void __exit mv_otg_exit(
void)