1 /* 2 * Copyright (C) 2011 Marvell International Ltd. All rights reserved. 3 * Author: Chao Xie <chao.xie@marvell.com> 4 * Neil Zhang <zhangwm@marvell.com> 5 * 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. 10 */ 11 12 #include <linux/module.h> 13 #include <linux/kernel.h> 14 #include <linux/io.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> 21 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> 28 29 #include "phy-mv-usb.h" 30 31 #define DRIVER_DESC "Marvell USB OTG transceiver driver" 32 33 MODULE_DESCRIPTION(DRIVER_DESC); 34 MODULE_LICENSE("GPL"); 35 36 static const char driver_name[] = "mv-otg"; 37 38 static char *state_string[] = { 39 "undefined", 40 "b_idle", 41 "b_srp_init", 42 "b_peripheral", 43 "b_wait_acon", 44 "b_host", 45 "a_idle", 46 "a_wait_vrise", 47 "a_wait_bcon", 48 "a_host", 49 "a_suspend", 50 "a_peripheral", 51 "a_wait_vfall", 52 "a_vbus_err" 53 }; 54 55 static int mv_otg_set_vbus(struct usb_otg *otg, bool on) 56 { 57 struct mv_otg *mvotg = container_of(otg->usb_phy, struct mv_otg, phy); 58 if (mvotg->pdata->set_vbus == NULL) 59 return -ENODEV; 60 61 return mvotg->pdata->set_vbus(on); 62 } 63 64 static int mv_otg_set_host(struct usb_otg *otg, 65 struct usb_bus *host) 66 { 67 otg->host = host; 68 69 return 0; 70 } 71 72 static int mv_otg_set_peripheral(struct usb_otg *otg, 73 struct usb_gadget *gadget) 74 { 75 otg->gadget = gadget; 76 77 return 0; 78 } 79 80 static void mv_otg_run_state_machine(struct mv_otg *mvotg, 81 unsigned long delay) 82 { 83 dev_dbg(&mvotg->pdev->dev, "transceiver is updated\n"); 84 if (!mvotg->qwork) 85 return; 86 87 queue_delayed_work(mvotg->qwork, &mvotg->work, delay); 88 } 89 90 static void mv_otg_timer_await_bcon(unsigned long data) 91 { 92 struct mv_otg *mvotg = (struct mv_otg *) data; 93 94 mvotg->otg_ctrl.a_wait_bcon_timeout = 1; 95 96 dev_info(&mvotg->pdev->dev, "B Device No Response!\n"); 97 98 if (spin_trylock(&mvotg->wq_lock)) { 99 mv_otg_run_state_machine(mvotg, 0); 100 spin_unlock(&mvotg->wq_lock); 101 } 102 } 103 104 static int mv_otg_cancel_timer(struct mv_otg *mvotg, unsigned int id) 105 { 106 struct timer_list *timer; 107 108 if (id >= OTG_TIMER_NUM) 109 return -EINVAL; 110 111 timer = &mvotg->otg_ctrl.timer[id]; 112 113 if (timer_pending(timer)) 114 del_timer(timer); 115 116 return 0; 117 } 118 119 static int mv_otg_set_timer(struct mv_otg *mvotg, unsigned int id, 120 unsigned long interval, 121 void (*callback) (unsigned long)) 122 { 123 struct timer_list *timer; 124 125 if (id >= OTG_TIMER_NUM) 126 return -EINVAL; 127 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); 131 return -EBUSY; 132 } 133 134 init_timer(timer); 135 timer->data = (unsigned long) mvotg; 136 timer->function = callback; 137 timer->expires = jiffies + interval; 138 add_timer(timer); 139 140 return 0; 141 } 142 143 static int mv_otg_reset(struct mv_otg *mvotg) 144 { 145 unsigned int loops; 146 u32 tmp; 147 148 /* Stop the controller */ 149 tmp = readl(&mvotg->op_regs->usbcmd); 150 tmp &= ~USBCMD_RUN_STOP; 151 writel(tmp, &mvotg->op_regs->usbcmd); 152 153 /* Reset the controller to get default values */ 154 writel(USBCMD_CTRL_RESET, &mvotg->op_regs->usbcmd); 155 156 loops = 500; 157 while (readl(&mvotg->op_regs->usbcmd) & USBCMD_CTRL_RESET) { 158 if (loops == 0) { 159 dev_err(&mvotg->pdev->dev, 160 "Wait for RESET completed TIMEOUT\n"); 161 return -ETIMEDOUT; 162 } 163 loops--; 164 udelay(20); 165 } 166 167 writel(0x0, &mvotg->op_regs->usbintr); 168 tmp = readl(&mvotg->op_regs->usbsts); 169 writel(tmp, &mvotg->op_regs->usbsts); 170 171 return 0; 172 } 173 174 static void mv_otg_init_irq(struct mv_otg *mvotg) 175 { 176 u32 otgsc; 177 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; 182 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; 188 } 189 190 if (mvotg->pdata->id == NULL) { 191 mvotg->irq_en |= OTGSC_INTR_USB_ID; 192 mvotg->irq_status |= OTGSC_INTSTS_USB_ID; 193 } 194 195 otgsc = readl(&mvotg->op_regs->otgsc); 196 otgsc |= mvotg->irq_en; 197 writel(otgsc, &mvotg->op_regs->otgsc); 198 } 199 200 static void mv_otg_start_host(struct mv_otg *mvotg, int on) 201 { 202 #ifdef CONFIG_USB 203 struct usb_otg *otg = mvotg->phy.otg; 204 struct usb_hcd *hcd; 205 206 if (!otg->host) 207 return; 208 209 dev_info(&mvotg->pdev->dev, "%s host\n", on ? "start" : "stop"); 210 211 hcd = bus_to_hcd(otg->host); 212 213 if (on) { 214 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED); 215 device_wakeup_enable(hcd->self.controller); 216 } else { 217 usb_remove_hcd(hcd); 218 } 219 #endif /* CONFIG_USB */ 220 } 221 222 static void mv_otg_start_periphrals(struct mv_otg *mvotg, int on) 223 { 224 struct usb_otg *otg = mvotg->phy.otg; 225 226 if (!otg->gadget) 227 return; 228 229 dev_info(mvotg->phy.dev, "gadget %s\n", on ? "on" : "off"); 230 231 if (on) 232 usb_gadget_vbus_connect(otg->gadget); 233 else 234 usb_gadget_vbus_disconnect(otg->gadget); 235 } 236 237 static void otg_clock_enable(struct mv_otg *mvotg) 238 { 239 clk_prepare_enable(mvotg->clk); 240 } 241 242 static void otg_clock_disable(struct mv_otg *mvotg) 243 { 244 clk_disable_unprepare(mvotg->clk); 245 } 246 247 static int mv_otg_enable_internal(struct mv_otg *mvotg) 248 { 249 int retval = 0; 250 251 if (mvotg->active) 252 return 0; 253 254 dev_dbg(&mvotg->pdev->dev, "otg enabled\n"); 255 256 otg_clock_enable(mvotg); 257 if (mvotg->pdata->phy_init) { 258 retval = mvotg->pdata->phy_init(mvotg->phy_regs); 259 if (retval) { 260 dev_err(&mvotg->pdev->dev, 261 "init phy error %d\n", retval); 262 otg_clock_disable(mvotg); 263 return retval; 264 } 265 } 266 mvotg->active = 1; 267 268 return 0; 269 270 } 271 272 static int mv_otg_enable(struct mv_otg *mvotg) 273 { 274 if (mvotg->clock_gating) 275 return mv_otg_enable_internal(mvotg); 276 277 return 0; 278 } 279 280 static void mv_otg_disable_internal(struct mv_otg *mvotg) 281 { 282 if (mvotg->active) { 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); 287 mvotg->active = 0; 288 } 289 } 290 291 static void mv_otg_disable(struct mv_otg *mvotg) 292 { 293 if (mvotg->clock_gating) 294 mv_otg_disable_internal(mvotg); 295 } 296 297 static void mv_otg_update_inputs(struct mv_otg *mvotg) 298 { 299 struct mv_otg_ctrl *otg_ctrl = &mvotg->otg_ctrl; 300 u32 otgsc; 301 302 otgsc = readl(&mvotg->op_regs->otgsc); 303 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; 308 } else { 309 otg_ctrl->b_sess_vld = 0; 310 otg_ctrl->b_sess_end = 1; 311 } 312 } else { 313 otg_ctrl->b_sess_vld = !!(otgsc & OTGSC_STS_B_SESSION_VALID); 314 otg_ctrl->b_sess_end = !!(otgsc & OTGSC_STS_B_SESSION_END); 315 } 316 317 if (mvotg->pdata->id) 318 otg_ctrl->id = !!mvotg->pdata->id->poll(); 319 else 320 otg_ctrl->id = !!(otgsc & OTGSC_STS_USB_ID); 321 322 if (mvotg->pdata->otg_force_a_bus_req && !otg_ctrl->id) 323 otg_ctrl->a_bus_req = 1; 324 325 otg_ctrl->a_sess_vld = !!(otgsc & OTGSC_STS_A_SESSION_VALID); 326 otg_ctrl->a_vbus_vld = !!(otgsc & OTGSC_STS_A_VBUS_VALID); 327 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); 334 } 335 336 static void mv_otg_update_state(struct mv_otg *mvotg) 337 { 338 struct mv_otg_ctrl *otg_ctrl = &mvotg->otg_ctrl; 339 int old_state = mvotg->phy.otg->state; 340 341 switch (old_state) { 342 case OTG_STATE_UNDEFINED: 343 mvotg->phy.otg->state = OTG_STATE_B_IDLE; 344 /* FALL THROUGH */ 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; 350 break; 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; 354 break; 355 case OTG_STATE_A_IDLE: 356 if (otg_ctrl->id) 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; 361 break; 362 case OTG_STATE_A_WAIT_VRISE: 363 if (otg_ctrl->a_vbus_vld) 364 mvotg->phy.otg->state = OTG_STATE_A_WAIT_BCON; 365 break; 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; 381 } 382 break; 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; 389 break; 390 case OTG_STATE_A_WAIT_VFALL: 391 if (otg_ctrl->id 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; 395 break; 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; 401 } 402 break; 403 default: 404 break; 405 } 406 } 407 408 static void mv_otg_work(struct work_struct *work) 409 { 410 struct mv_otg *mvotg; 411 struct usb_phy *phy; 412 struct usb_otg *otg; 413 int old_state; 414 415 mvotg = container_of(to_delayed_work(work), struct mv_otg, work); 416 417 run: 418 /* work queue is single thread, or we need spin_lock to protect */ 419 phy = &mvotg->phy; 420 otg = mvotg->phy.otg; 421 old_state = otg->state; 422 423 if (!mvotg->active) 424 return; 425 426 mv_otg_update_inputs(mvotg); 427 mv_otg_update_state(mvotg); 428 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]); 433 434 switch (mvotg->phy.otg->state) { 435 case OTG_STATE_B_IDLE: 436 otg->default_a = 0; 437 if (old_state == OTG_STATE_B_PERIPHERAL) 438 mv_otg_start_periphrals(mvotg, 0); 439 mv_otg_reset(mvotg); 440 mv_otg_disable(mvotg); 441 usb_phy_set_event(&mvotg->phy, USB_EVENT_NONE); 442 break; 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); 447 break; 448 case OTG_STATE_A_IDLE: 449 otg->default_a = 1; 450 mv_otg_enable(mvotg); 451 if (old_state == OTG_STATE_A_WAIT_VFALL) 452 mv_otg_start_host(mvotg, 0); 453 mv_otg_reset(mvotg); 454 break; 455 case OTG_STATE_A_WAIT_VRISE: 456 mv_otg_set_vbus(otg, 1); 457 break; 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, 462 T_A_WAIT_BCON, 463 mv_otg_timer_await_bcon); 464 /* 465 * Now, we directly enter A_HOST. So set b_conn = 1 466 * here. In fact, it need host driver to notify us. 467 */ 468 mvotg->otg_ctrl.b_conn = 1; 469 break; 470 case OTG_STATE_A_HOST: 471 break; 472 case OTG_STATE_A_WAIT_VFALL: 473 /* 474 * Now, we has exited A_HOST. So set b_conn = 0 475 * here. In fact, it need host driver to notify us. 476 */ 477 mvotg->otg_ctrl.b_conn = 0; 478 mv_otg_set_vbus(otg, 0); 479 break; 480 case OTG_STATE_A_VBUS_ERR: 481 break; 482 default: 483 break; 484 } 485 goto run; 486 } 487 } 488 489 static irqreturn_t mv_otg_irq(int irq, void *dev) 490 { 491 struct mv_otg *mvotg = dev; 492 u32 otgsc; 493 494 otgsc = readl(&mvotg->op_regs->otgsc); 495 writel(otgsc, &mvotg->op_regs->otgsc); 496 497 /* 498 * if we have vbus, then the vbus detection for B-device 499 * will be done by mv_otg_inputs_irq(). 500 */ 501 if (mvotg->pdata->vbus) 502 if ((otgsc & OTGSC_STS_USB_ID) && 503 !(otgsc & OTGSC_INTSTS_USB_ID)) 504 return IRQ_NONE; 505 506 if ((otgsc & mvotg->irq_status) == 0) 507 return IRQ_NONE; 508 509 mv_otg_run_state_machine(mvotg, 0); 510 511 return IRQ_HANDLED; 512 } 513 514 static irqreturn_t mv_otg_inputs_irq(int irq, void *dev) 515 { 516 struct mv_otg *mvotg = dev; 517 518 /* The clock may disabled at this time */ 519 if (!mvotg->active) { 520 mv_otg_enable(mvotg); 521 mv_otg_init_irq(mvotg); 522 } 523 524 mv_otg_run_state_machine(mvotg, 0); 525 526 return IRQ_HANDLED; 527 } 528 529 static ssize_t 530 get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf) 531 { 532 struct mv_otg *mvotg = dev_get_drvdata(dev); 533 return scnprintf(buf, PAGE_SIZE, "%d\n", 534 mvotg->otg_ctrl.a_bus_req); 535 } 536 537 static ssize_t 538 set_a_bus_req(struct device *dev, struct device_attribute *attr, 539 const char *buf, size_t count) 540 { 541 struct mv_otg *mvotg = dev_get_drvdata(dev); 542 543 if (count > 2) 544 return -1; 545 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) 549 return -1; 550 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); 554 555 if (buf[0] == '1') { 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"); 560 561 if (spin_trylock(&mvotg->wq_lock)) { 562 mv_otg_run_state_machine(mvotg, 0); 563 spin_unlock(&mvotg->wq_lock); 564 } 565 } 566 567 return count; 568 } 569 570 static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUSR, get_a_bus_req, 571 set_a_bus_req); 572 573 static ssize_t 574 set_a_clr_err(struct device *dev, struct device_attribute *attr, 575 const char *buf, size_t count) 576 { 577 struct mv_otg *mvotg = dev_get_drvdata(dev); 578 if (!mvotg->phy.otg->default_a) 579 return -1; 580 581 if (count > 2) 582 return -1; 583 584 if (buf[0] == '1') { 585 mvotg->otg_ctrl.a_clr_err = 1; 586 dev_dbg(&mvotg->pdev->dev, 587 "User request: a_clr_err = 1\n"); 588 } 589 590 if (spin_trylock(&mvotg->wq_lock)) { 591 mv_otg_run_state_machine(mvotg, 0); 592 spin_unlock(&mvotg->wq_lock); 593 } 594 595 return count; 596 } 597 598 static DEVICE_ATTR(a_clr_err, S_IWUSR, NULL, set_a_clr_err); 599 600 static ssize_t 601 get_a_bus_drop(struct device *dev, struct device_attribute *attr, 602 char *buf) 603 { 604 struct mv_otg *mvotg = dev_get_drvdata(dev); 605 return scnprintf(buf, PAGE_SIZE, "%d\n", 606 mvotg->otg_ctrl.a_bus_drop); 607 } 608 609 static ssize_t 610 set_a_bus_drop(struct device *dev, struct device_attribute *attr, 611 const char *buf, size_t count) 612 { 613 struct mv_otg *mvotg = dev_get_drvdata(dev); 614 if (!mvotg->phy.otg->default_a) 615 return -1; 616 617 if (count > 2) 618 return -1; 619 620 if (buf[0] == '0') { 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"); 631 } 632 633 if (spin_trylock(&mvotg->wq_lock)) { 634 mv_otg_run_state_machine(mvotg, 0); 635 spin_unlock(&mvotg->wq_lock); 636 } 637 638 return count; 639 } 640 641 static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUSR, 642 get_a_bus_drop, set_a_bus_drop); 643 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, 648 NULL, 649 }; 650 651 static const struct attribute_group inputs_attr_group = { 652 .name = "inputs", 653 .attrs = inputs_attrs, 654 }; 655 656 static int mv_otg_remove(struct platform_device *pdev) 657 { 658 struct mv_otg *mvotg = platform_get_drvdata(pdev); 659 660 sysfs_remove_group(&mvotg->pdev->dev.kobj, &inputs_attr_group); 661 662 if (mvotg->qwork) { 663 flush_workqueue(mvotg->qwork); 664 destroy_workqueue(mvotg->qwork); 665 } 666 667 mv_otg_disable(mvotg); 668 669 usb_remove_phy(&mvotg->phy); 670 671 return 0; 672 } 673 674 static int mv_otg_probe(struct platform_device *pdev) 675 { 676 struct mv_usb_platform_data *pdata = dev_get_platdata(&pdev->dev); 677 struct mv_otg *mvotg; 678 struct usb_otg *otg; 679 struct resource *r; 680 int retval = 0, i; 681 682 if (pdata == NULL) { 683 dev_err(&pdev->dev, "failed to get platform data\n"); 684 return -ENODEV; 685 } 686 687 mvotg = devm_kzalloc(&pdev->dev, sizeof(*mvotg), GFP_KERNEL); 688 if (!mvotg) 689 return -ENOMEM; 690 691 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); 692 if (!otg) 693 return -ENOMEM; 694 695 platform_set_drvdata(pdev, mvotg); 696 697 mvotg->pdev = pdev; 698 mvotg->pdata = pdata; 699 700 mvotg->clk = devm_clk_get(&pdev->dev, NULL); 701 if (IS_ERR(mvotg->clk)) 702 return PTR_ERR(mvotg->clk); 703 704 mvotg->qwork = create_singlethread_workqueue("mv_otg_queue"); 705 if (!mvotg->qwork) { 706 dev_dbg(&pdev->dev, "cannot create workqueue for OTG\n"); 707 return -ENOMEM; 708 } 709 710 INIT_DELAYED_WORK(&mvotg->work, mv_otg_work); 711 712 /* OTG common part */ 713 mvotg->pdev = pdev; 714 mvotg->phy.dev = &pdev->dev; 715 mvotg->phy.otg = otg; 716 mvotg->phy.label = driver_name; 717 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; 723 724 for (i = 0; i < OTG_TIMER_NUM; i++) 725 init_timer(&mvotg->otg_ctrl.timer[i]); 726 727 r = platform_get_resource_byname(mvotg->pdev, 728 IORESOURCE_MEM, "phyregs"); 729 if (r == NULL) { 730 dev_err(&pdev->dev, "no phy I/O memory resource defined\n"); 731 retval = -ENODEV; 732 goto err_destroy_workqueue; 733 } 734 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"); 738 retval = -EFAULT; 739 goto err_destroy_workqueue; 740 } 741 742 r = platform_get_resource_byname(mvotg->pdev, 743 IORESOURCE_MEM, "capregs"); 744 if (r == NULL) { 745 dev_err(&pdev->dev, "no I/O memory resource defined\n"); 746 retval = -ENODEV; 747 goto err_destroy_workqueue; 748 } 749 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"); 753 retval = -EFAULT; 754 goto err_destroy_workqueue; 755 } 756 757 /* we will acces controller register, so enable the udc controller */ 758 retval = mv_otg_enable_internal(mvotg); 759 if (retval) { 760 dev_err(&pdev->dev, "mv otg enable error %d\n", retval); 761 goto err_destroy_workqueue; 762 } 763 764 mvotg->op_regs = 765 (struct mv_otg_regs __iomem *) ((unsigned long) mvotg->cap_regs 766 + (readl(mvotg->cap_regs) & CAPLENGTH_MASK)); 767 768 if (pdata->id) { 769 retval = devm_request_threaded_irq(&pdev->dev, pdata->id->irq, 770 NULL, mv_otg_inputs_irq, 771 IRQF_ONESHOT, "id", mvotg); 772 if (retval) { 773 dev_info(&pdev->dev, 774 "Failed to request irq for ID\n"); 775 pdata->id = NULL; 776 } 777 } 778 779 if (pdata->vbus) { 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); 784 if (retval) { 785 dev_info(&pdev->dev, 786 "Failed to request irq for VBUS, " 787 "disable clock gating\n"); 788 mvotg->clock_gating = 0; 789 pdata->vbus = NULL; 790 } 791 } 792 793 if (pdata->disable_otg_clock_gating) 794 mvotg->clock_gating = 0; 795 796 mv_otg_reset(mvotg); 797 mv_otg_init_irq(mvotg); 798 799 r = platform_get_resource(mvotg->pdev, IORESOURCE_IRQ, 0); 800 if (r == NULL) { 801 dev_err(&pdev->dev, "no IRQ resource defined\n"); 802 retval = -ENODEV; 803 goto err_disable_clk; 804 } 805 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", 810 mvotg->irq); 811 mvotg->irq = 0; 812 retval = -ENODEV; 813 goto err_disable_clk; 814 } 815 816 retval = usb_add_phy(&mvotg->phy, USB_PHY_TYPE_USB2); 817 if (retval < 0) { 818 dev_err(&pdev->dev, "can't register transceiver, %d\n", 819 retval); 820 goto err_disable_clk; 821 } 822 823 retval = sysfs_create_group(&pdev->dev.kobj, &inputs_attr_group); 824 if (retval < 0) { 825 dev_dbg(&pdev->dev, 826 "Can't register sysfs attr group: %d\n", retval); 827 goto err_remove_phy; 828 } 829 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); 834 } 835 836 dev_info(&pdev->dev, 837 "successful probe OTG device %s clock gating.\n", 838 mvotg->clock_gating ? "with" : "without"); 839 840 return 0; 841 842 err_remove_phy: 843 usb_remove_phy(&mvotg->phy); 844 err_disable_clk: 845 mv_otg_disable_internal(mvotg); 846 err_destroy_workqueue: 847 flush_workqueue(mvotg->qwork); 848 destroy_workqueue(mvotg->qwork); 849 850 return retval; 851 } 852 853 #ifdef CONFIG_PM 854 static int mv_otg_suspend(struct platform_device *pdev, pm_message_t state) 855 { 856 struct mv_otg *mvotg = platform_get_drvdata(pdev); 857 858 if (mvotg->phy.otg->state != OTG_STATE_B_IDLE) { 859 dev_info(&pdev->dev, 860 "OTG state is not B_IDLE, it is %d!\n", 861 mvotg->phy.otg->state); 862 return -EAGAIN; 863 } 864 865 if (!mvotg->clock_gating) 866 mv_otg_disable_internal(mvotg); 867 868 return 0; 869 } 870 871 static int mv_otg_resume(struct platform_device *pdev) 872 { 873 struct mv_otg *mvotg = platform_get_drvdata(pdev); 874 u32 otgsc; 875 876 if (!mvotg->clock_gating) { 877 mv_otg_enable_internal(mvotg); 878 879 otgsc = readl(&mvotg->op_regs->otgsc); 880 otgsc |= mvotg->irq_en; 881 writel(otgsc, &mvotg->op_regs->otgsc); 882 883 if (spin_trylock(&mvotg->wq_lock)) { 884 mv_otg_run_state_machine(mvotg, 0); 885 spin_unlock(&mvotg->wq_lock); 886 } 887 } 888 return 0; 889 } 890 #endif 891 892 static struct platform_driver mv_otg_driver = { 893 .probe = mv_otg_probe, 894 .remove = mv_otg_remove, 895 .driver = { 896 .name = driver_name, 897 }, 898 #ifdef CONFIG_PM 899 .suspend = mv_otg_suspend, 900 .resume = mv_otg_resume, 901 #endif 902 }; 903 module_platform_driver(mv_otg_driver); 904