2 * Intel Langwell USB OTG transceiver driver
3 * Copyright (C) 2008 - 2010, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 /* This driver helps to switch Langwell OTG controller function between host
20 * and peripheral. It works with EHCI driver and Langwell client controller
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/pci.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
29 #include <linux/device.h>
30 #include <linux/moduleparam.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb.h>
34 #include <linux/usb/otg.h>
35 #include <linux/usb/hcd.h>
36 #include <linux/notifier.h>
37 #include <linux/delay.h>
38 #include <asm/intel_scu_ipc.h>
40 #include <linux/usb/langwell_otg.h>
42 #define DRIVER_DESC "Intel Langwell USB OTG transceiver driver"
43 #define DRIVER_VERSION "July 10, 2010"
45 MODULE_DESCRIPTION(DRIVER_DESC);
46 MODULE_AUTHOR("Henry Yuan <hang.yuan@intel.com>, Hao Wu <hao.wu@intel.com>");
47 MODULE_VERSION(DRIVER_VERSION);
48 MODULE_LICENSE("GPL");
50 static const char driver_name[] = "langwell_otg";
52 static int langwell_otg_probe(struct pci_dev *pdev,
53 const struct pci_device_id *id);
54 static void langwell_otg_remove(struct pci_dev *pdev);
55 static int langwell_otg_suspend(struct pci_dev *pdev, pm_message_t message);
56 static int langwell_otg_resume(struct pci_dev *pdev);
58 static int langwell_otg_set_host(struct otg_transceiver *otg,
59 struct usb_bus *host);
60 static int langwell_otg_set_peripheral(struct otg_transceiver *otg,
61 struct usb_gadget *gadget);
62 static int langwell_otg_start_srp(struct otg_transceiver *otg);
64 static const struct pci_device_id pci_ids[] = {{
65 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
69 .subvendor = PCI_ANY_ID,
70 .subdevice = PCI_ANY_ID,
71 }, { /* end: all zeroes */ }
74 static struct pci_driver otg_pci_driver = {
75 .name = (char *) driver_name,
78 .probe = langwell_otg_probe,
79 .remove = langwell_otg_remove,
81 .suspend = langwell_otg_suspend,
82 .resume = langwell_otg_resume,
85 static const char *state_string(enum usb_otg_state state)
88 case OTG_STATE_A_IDLE:
90 case OTG_STATE_A_WAIT_VRISE:
91 return "a_wait_vrise";
92 case OTG_STATE_A_WAIT_BCON:
94 case OTG_STATE_A_HOST:
96 case OTG_STATE_A_SUSPEND:
98 case OTG_STATE_A_PERIPHERAL:
99 return "a_peripheral";
100 case OTG_STATE_A_WAIT_VFALL:
101 return "a_wait_vfall";
102 case OTG_STATE_A_VBUS_ERR:
104 case OTG_STATE_B_IDLE:
106 case OTG_STATE_B_SRP_INIT:
108 case OTG_STATE_B_PERIPHERAL:
109 return "b_peripheral";
110 case OTG_STATE_B_WAIT_ACON:
111 return "b_wait_acon";
112 case OTG_STATE_B_HOST:
120 static inline struct langwell_otg_timer *otg_timer_initializer
121 (void (*function)(unsigned long), unsigned long expires, unsigned long data)
123 struct langwell_otg_timer *timer;
124 timer = kmalloc(sizeof(struct langwell_otg_timer), GFP_KERNEL);
128 timer->function = function;
129 timer->expires = expires;
134 static struct langwell_otg_timer *a_wait_vrise_tmr, *a_aidl_bdis_tmr,
135 *b_se0_srp_tmr, *b_srp_init_tmr;
137 static struct list_head active_timers;
139 static struct langwell_otg *the_transceiver;
141 /* host/client notify transceiver when event affects HNP state */
142 void langwell_update_transceiver(void)
144 struct langwell_otg *lnw = the_transceiver;
146 dev_dbg(lnw->dev, "transceiver is updated\n");
151 queue_work(lnw->qwork, &lnw->work);
153 EXPORT_SYMBOL(langwell_update_transceiver);
155 static int langwell_otg_set_host(struct otg_transceiver *otg,
156 struct usb_bus *host)
163 static int langwell_otg_set_peripheral(struct otg_transceiver *otg,
164 struct usb_gadget *gadget)
166 otg->gadget = gadget;
171 static int langwell_otg_set_power(struct otg_transceiver *otg,
177 /* A-device drives vbus, controlled through PMIC CHRGCNTL register*/
178 static int langwell_otg_set_vbus(struct otg_transceiver *otg, bool enabled)
180 struct langwell_otg *lnw = the_transceiver;
183 dev_dbg(lnw->dev, "%s <--- %s\n", __func__, enabled ? "on" : "off");
185 /* FIXME: surely we should cache this on the first read. If not use
186 readv to avoid two transactions */
187 if (intel_scu_ipc_ioread8(0x00, &r) < 0) {
188 dev_dbg(lnw->dev, "Failed to read PMIC register 0xD2");
191 if ((r & 0x03) != 0x02) {
192 dev_dbg(lnw->dev, "not NEC PMIC attached\n");
196 if (intel_scu_ipc_ioread8(0x20, &r) < 0) {
197 dev_dbg(lnw->dev, "Failed to read PMIC register 0xD2");
201 if ((r & 0x20) == 0) {
202 dev_dbg(lnw->dev, "no battery attached\n");
206 /* Workaround for battery attachment issue */
208 dev_dbg(lnw->dev, "no battery attached on SH\n");
212 dev_dbg(lnw->dev, "battery attached. 2 reg = %x\n", r);
214 /* workaround: FW detect writing 0x20/0xc0 to d4 event.
215 * this is only for NEC PMIC.
218 if (intel_scu_ipc_iowrite8(0xD4, enabled ? 0x20 : 0xC0))
219 dev_dbg(lnw->dev, "Failed to write PMIC.\n");
221 dev_dbg(lnw->dev, "%s --->\n", __func__);
226 /* charge vbus or discharge vbus through a resistor to ground */
227 static void langwell_otg_chrg_vbus(int on)
229 struct langwell_otg *lnw = the_transceiver;
232 val = readl(lnw->iotg.base + CI_OTGSC);
235 writel((val & ~OTGSC_INTSTS_MASK) | OTGSC_VC,
236 lnw->iotg.base + CI_OTGSC);
238 writel((val & ~OTGSC_INTSTS_MASK) | OTGSC_VD,
239 lnw->iotg.base + CI_OTGSC);
243 static int langwell_otg_start_srp(struct otg_transceiver *otg)
245 struct langwell_otg *lnw = the_transceiver;
246 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
249 dev_dbg(lnw->dev, "%s --->\n", __func__);
251 val = readl(iotg->base + CI_OTGSC);
253 writel((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP,
254 iotg->base + CI_OTGSC);
256 /* Check if the data plus is finished or not */
258 val = readl(iotg->base + CI_OTGSC);
259 if (val & (OTGSC_HADP | OTGSC_DP))
260 dev_dbg(lnw->dev, "DataLine SRP Error\n");
262 /* Disable interrupt - b_sess_vld */
263 val = readl(iotg->base + CI_OTGSC);
264 val &= (~(OTGSC_BSVIE | OTGSC_BSEIE));
265 writel(val, iotg->base + CI_OTGSC);
267 /* Start VBus SRP, drive vbus to generate VBus pulse */
268 iotg->otg.set_vbus(&iotg->otg, true);
270 iotg->otg.set_vbus(&iotg->otg, false);
272 /* Enable interrupt - b_sess_vld*/
273 val = readl(iotg->base + CI_OTGSC);
274 dev_dbg(lnw->dev, "after VBUS pulse otgsc = %x\n", val);
276 val |= (OTGSC_BSVIE | OTGSC_BSEIE);
277 writel(val, iotg->base + CI_OTGSC);
279 /* If Vbus is valid, then update the hsm */
280 if (val & OTGSC_BSV) {
281 dev_dbg(lnw->dev, "no b_sess_vld interrupt\n");
283 lnw->iotg.hsm.b_sess_vld = 1;
284 langwell_update_transceiver();
287 dev_dbg(lnw->dev, "%s <---\n", __func__);
291 /* stop SOF via bus_suspend */
292 static void langwell_otg_loc_sof(int on)
294 struct langwell_otg *lnw = the_transceiver;
298 dev_dbg(lnw->dev, "%s ---> %s\n", __func__, on ? "suspend" : "resume");
300 hcd = bus_to_hcd(lnw->iotg.otg.host);
302 err = hcd->driver->bus_resume(hcd);
304 err = hcd->driver->bus_suspend(hcd);
307 dev_dbg(lnw->dev, "Fail to resume/suspend USB bus - %d\n", err);
309 dev_dbg(lnw->dev, "%s <---\n", __func__);
312 static int langwell_otg_check_otgsc(void)
314 struct langwell_otg *lnw = the_transceiver;
317 dev_dbg(lnw->dev, "check sync OTGSC and USBCFG registers\n");
319 otgsc = readl(lnw->iotg.base + CI_OTGSC);
320 usbcfg = readl(lnw->usbcfg);
322 dev_dbg(lnw->dev, "OTGSC = %08x, USBCFG = %08x\n",
324 dev_dbg(lnw->dev, "OTGSC_AVV = %d\n", !!(otgsc & OTGSC_AVV));
325 dev_dbg(lnw->dev, "USBCFG.VBUSVAL = %d\n",
326 !!(usbcfg & USBCFG_VBUSVAL));
327 dev_dbg(lnw->dev, "OTGSC_ASV = %d\n", !!(otgsc & OTGSC_ASV));
328 dev_dbg(lnw->dev, "USBCFG.AVALID = %d\n",
329 !!(usbcfg & USBCFG_AVALID));
330 dev_dbg(lnw->dev, "OTGSC_BSV = %d\n", !!(otgsc & OTGSC_BSV));
331 dev_dbg(lnw->dev, "USBCFG.BVALID = %d\n",
332 !!(usbcfg & USBCFG_BVALID));
333 dev_dbg(lnw->dev, "OTGSC_BSE = %d\n", !!(otgsc & OTGSC_BSE));
334 dev_dbg(lnw->dev, "USBCFG.SESEND = %d\n",
335 !!(usbcfg & USBCFG_SESEND));
337 /* Check USBCFG VBusValid/AValid/BValid/SessEnd */
338 if (!!(otgsc & OTGSC_AVV) ^ !!(usbcfg & USBCFG_VBUSVAL)) {
339 dev_dbg(lnw->dev, "OTGSC.AVV != USBCFG.VBUSVAL\n");
342 if (!!(otgsc & OTGSC_ASV) ^ !!(usbcfg & USBCFG_AVALID)) {
343 dev_dbg(lnw->dev, "OTGSC.ASV != USBCFG.AVALID\n");
346 if (!!(otgsc & OTGSC_BSV) ^ !!(usbcfg & USBCFG_BVALID)) {
347 dev_dbg(lnw->dev, "OTGSC.BSV != USBCFG.BVALID\n");
350 if (!!(otgsc & OTGSC_BSE) ^ !!(usbcfg & USBCFG_SESEND)) {
351 dev_dbg(lnw->dev, "OTGSC.BSE != USBCFG.SESSEN\n");
355 dev_dbg(lnw->dev, "OTGSC and USBCFG are synced\n");
360 dev_warn(lnw->dev, "OTGSC isn't equal to USBCFG\n");
365 static void langwell_otg_phy_low_power(int on)
367 struct langwell_otg *lnw = the_transceiver;
368 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
372 dev_dbg(lnw->dev, "%s ---> %s mode\n",
373 __func__, on ? "Low power" : "Normal");
377 val = readb(iotg->base + CI_HOSTPC1 + 2);
380 /* Due to hardware issue, after set PHCD, sync will failed
381 * between USBCFG and OTGSC, so before set PHCD, check if
382 * sync is in process now. If the answer is "yes", then do
383 * not touch PHCD bit */
384 retval = langwell_otg_check_otgsc();
386 dev_dbg(lnw->dev, "Skip PHCD programming..\n");
390 writeb(val | phcd, iotg->base + CI_HOSTPC1 + 2);
392 writeb(val & ~phcd, iotg->base + CI_HOSTPC1 + 2);
394 dev_dbg(lnw->dev, "%s <--- done\n", __func__);
397 /* After drv vbus, add 2 ms delay to set PHCD */
398 static void langwell_otg_phy_low_power_wait(int on)
400 struct langwell_otg *lnw = the_transceiver;
402 dev_dbg(lnw->dev, "add 2ms delay before programing PHCD\n");
405 langwell_otg_phy_low_power(on);
408 /* Enable/Disable OTG interrupt */
409 static void langwell_otg_intr(int on)
411 struct langwell_otg *lnw = the_transceiver;
412 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
415 dev_dbg(lnw->dev, "%s ---> %s\n", __func__, on ? "on" : "off");
417 val = readl(iotg->base + CI_OTGSC);
419 /* OTGSC_INT_MASK doesn't contains 1msInt */
421 val = val | (OTGSC_INT_MASK);
422 writel(val, iotg->base + CI_OTGSC);
424 val = val & ~(OTGSC_INT_MASK);
425 writel(val, iotg->base + CI_OTGSC);
428 dev_dbg(lnw->dev, "%s <---\n", __func__);
431 /* set HAAR: Hardware Assist Auto-Reset */
432 static void langwell_otg_HAAR(int on)
434 struct langwell_otg *lnw = the_transceiver;
435 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
438 dev_dbg(lnw->dev, "%s ---> %s\n", __func__, on ? "on" : "off");
440 val = readl(iotg->base + CI_OTGSC);
442 writel((val & ~OTGSC_INTSTS_MASK) | OTGSC_HAAR,
443 iotg->base + CI_OTGSC);
445 writel((val & ~OTGSC_INTSTS_MASK) & ~OTGSC_HAAR,
446 iotg->base + CI_OTGSC);
448 dev_dbg(lnw->dev, "%s <---\n", __func__);
451 /* set HABA: Hardware Assist B-Disconnect to A-Connect */
452 static void langwell_otg_HABA(int on)
454 struct langwell_otg *lnw = the_transceiver;
455 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
458 dev_dbg(lnw->dev, "%s ---> %s\n", __func__, on ? "on" : "off");
460 val = readl(iotg->base + CI_OTGSC);
462 writel((val & ~OTGSC_INTSTS_MASK) | OTGSC_HABA,
463 iotg->base + CI_OTGSC);
465 writel((val & ~OTGSC_INTSTS_MASK) & ~OTGSC_HABA,
466 iotg->base + CI_OTGSC);
468 dev_dbg(lnw->dev, "%s <---\n", __func__);
471 static int langwell_otg_check_se0_srp(int on)
473 struct langwell_otg *lnw = the_transceiver;
474 int delay_time = TB_SE0_SRP * 10;
477 dev_dbg(lnw->dev, "%s --->\n", __func__);
483 val = readl(lnw->iotg.base + CI_PORTSC1);
487 dev_dbg(lnw->dev, "%s <---\n", __func__);
491 /* The timeout callback function to set time out bit */
492 static void set_tmout(unsigned long indicator)
494 *(int *)indicator = 1;
497 void langwell_otg_nsf_msg(unsigned long indicator)
499 struct langwell_otg *lnw = the_transceiver;
507 "OTG:NSF-%lu - deivce not responding\n", indicator);
511 "OTG:NSF-%lu - deivce not supported\n", indicator);
514 dev_warn(lnw->dev, "Do not have this kind of NSF\n");
519 /* Initialize timers */
520 static int langwell_otg_init_timers(struct otg_hsm *hsm)
522 /* HSM used timers */
523 a_wait_vrise_tmr = otg_timer_initializer(&set_tmout, TA_WAIT_VRISE,
524 (unsigned long)&hsm->a_wait_vrise_tmout);
525 if (a_wait_vrise_tmr == NULL)
527 a_aidl_bdis_tmr = otg_timer_initializer(&set_tmout, TA_AIDL_BDIS,
528 (unsigned long)&hsm->a_aidl_bdis_tmout);
529 if (a_aidl_bdis_tmr == NULL)
531 b_se0_srp_tmr = otg_timer_initializer(&set_tmout, TB_SE0_SRP,
532 (unsigned long)&hsm->b_se0_srp);
533 if (b_se0_srp_tmr == NULL)
535 b_srp_init_tmr = otg_timer_initializer(&set_tmout, TB_SRP_INIT,
536 (unsigned long)&hsm->b_srp_init_tmout);
537 if (b_srp_init_tmr == NULL)
544 static void langwell_otg_free_timers(void)
546 kfree(a_wait_vrise_tmr);
547 kfree(a_aidl_bdis_tmr);
548 kfree(b_se0_srp_tmr);
549 kfree(b_srp_init_tmr);
552 /* The timeout callback function to set time out bit */
553 static void langwell_otg_timer_fn(unsigned long indicator)
555 struct langwell_otg *lnw = the_transceiver;
557 *(int *)indicator = 1;
559 dev_dbg(lnw->dev, "kernel timer - timeout\n");
561 langwell_update_transceiver();
564 /* kernel timer used instead of HW based interrupt */
565 static void langwell_otg_add_ktimer(enum langwell_otg_timer_type timers)
567 struct langwell_otg *lnw = the_transceiver;
568 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
569 unsigned long j = jiffies;
570 unsigned long data, time;
573 case TA_WAIT_VRISE_TMR:
574 iotg->hsm.a_wait_vrise_tmout = 0;
575 data = (unsigned long)&iotg->hsm.a_wait_vrise_tmout;
576 time = TA_WAIT_VRISE;
578 case TA_WAIT_BCON_TMR:
579 iotg->hsm.a_wait_bcon_tmout = 0;
580 data = (unsigned long)&iotg->hsm.a_wait_bcon_tmout;
583 case TA_AIDL_BDIS_TMR:
584 iotg->hsm.a_aidl_bdis_tmout = 0;
585 data = (unsigned long)&iotg->hsm.a_aidl_bdis_tmout;
588 case TB_ASE0_BRST_TMR:
589 iotg->hsm.b_ase0_brst_tmout = 0;
590 data = (unsigned long)&iotg->hsm.b_ase0_brst_tmout;
593 case TB_SRP_INIT_TMR:
594 iotg->hsm.b_srp_init_tmout = 0;
595 data = (unsigned long)&iotg->hsm.b_srp_init_tmout;
598 case TB_SRP_FAIL_TMR:
599 iotg->hsm.b_srp_fail_tmout = 0;
600 data = (unsigned long)&iotg->hsm.b_srp_fail_tmout;
603 case TB_BUS_SUSPEND_TMR:
604 iotg->hsm.b_bus_suspend_tmout = 0;
605 data = (unsigned long)&iotg->hsm.b_bus_suspend_tmout;
606 time = TB_BUS_SUSPEND;
609 dev_dbg(lnw->dev, "unkown timer, cannot enable it\n");
613 lnw->hsm_timer.data = data;
614 lnw->hsm_timer.function = langwell_otg_timer_fn;
615 lnw->hsm_timer.expires = j + time * HZ / 1000; /* milliseconds */
617 add_timer(&lnw->hsm_timer);
619 dev_dbg(lnw->dev, "add timer successfully\n");
622 /* Add timer to timer list */
623 static void langwell_otg_add_timer(void *gtimer)
625 struct langwell_otg_timer *timer = (struct langwell_otg_timer *)gtimer;
626 struct langwell_otg_timer *tmp_timer;
627 struct intel_mid_otg_xceiv *iotg = &the_transceiver->iotg;
630 /* Check if the timer is already in the active list,
631 * if so update timer count
633 list_for_each_entry(tmp_timer, &active_timers, list)
634 if (tmp_timer == timer) {
635 timer->count = timer->expires;
638 timer->count = timer->expires;
640 if (list_empty(&active_timers)) {
641 val32 = readl(iotg->base + CI_OTGSC);
642 writel(val32 | OTGSC_1MSE, iotg->base + CI_OTGSC);
645 list_add_tail(&timer->list, &active_timers);
648 /* Remove timer from the timer list; clear timeout status */
649 static void langwell_otg_del_timer(void *gtimer)
651 struct langwell_otg *lnw = the_transceiver;
652 struct langwell_otg_timer *timer = (struct langwell_otg_timer *)gtimer;
653 struct langwell_otg_timer *tmp_timer, *del_tmp;
656 list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list)
657 if (tmp_timer == timer)
658 list_del(&timer->list);
660 if (list_empty(&active_timers)) {
661 val32 = readl(lnw->iotg.base + CI_OTGSC);
662 writel(val32 & ~OTGSC_1MSE, lnw->iotg.base + CI_OTGSC);
666 /* Reduce timer count by 1, and find timeout conditions.*/
667 static int langwell_otg_tick_timer(u32 *int_sts)
669 struct langwell_otg *lnw = the_transceiver;
670 struct langwell_otg_timer *tmp_timer, *del_tmp;
673 list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list) {
675 /* check if timer expires */
676 if (!tmp_timer->count) {
677 list_del(&tmp_timer->list);
678 tmp_timer->function(tmp_timer->data);
683 if (list_empty(&active_timers)) {
684 dev_dbg(lnw->dev, "tick timer: disable 1ms int\n");
685 *int_sts = *int_sts & ~OTGSC_1MSE;
690 static void reset_otg(void)
692 struct langwell_otg *lnw = the_transceiver;
693 int delay_time = 1000;
696 dev_dbg(lnw->dev, "reseting OTG controller ...\n");
697 val = readl(lnw->iotg.base + CI_USBCMD);
698 writel(val | USBCMD_RST, lnw->iotg.base + CI_USBCMD);
702 dev_dbg(lnw->dev, "reset timeout\n");
703 val = readl(lnw->iotg.base + CI_USBCMD);
706 dev_dbg(lnw->dev, "reset done.\n");
709 static void set_host_mode(void)
711 struct langwell_otg *lnw = the_transceiver;
715 val = readl(lnw->iotg.base + CI_USBMODE);
716 val = (val & (~USBMODE_CM)) | USBMODE_HOST;
717 writel(val, lnw->iotg.base + CI_USBMODE);
720 static void set_client_mode(void)
722 struct langwell_otg *lnw = the_transceiver;
726 val = readl(lnw->iotg.base + CI_USBMODE);
727 val = (val & (~USBMODE_CM)) | USBMODE_DEVICE;
728 writel(val, lnw->iotg.base + CI_USBMODE);
731 static void init_hsm(void)
733 struct langwell_otg *lnw = the_transceiver;
734 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
737 /* read OTGSC after reset */
738 val32 = readl(lnw->iotg.base + CI_OTGSC);
739 dev_dbg(lnw->dev, "%s: OTGSC init value = 0x%x\n", __func__, val32);
742 if (val32 & OTGSC_ID) {
744 iotg->otg.default_a = 0;
746 iotg->otg.state = OTG_STATE_B_IDLE;
749 iotg->otg.default_a = 1;
751 iotg->otg.state = OTG_STATE_A_IDLE;
754 /* set session indicator */
755 if (val32 & OTGSC_BSE)
756 iotg->hsm.b_sess_end = 1;
757 if (val32 & OTGSC_BSV)
758 iotg->hsm.b_sess_vld = 1;
759 if (val32 & OTGSC_ASV)
760 iotg->hsm.a_sess_vld = 1;
761 if (val32 & OTGSC_AVV)
762 iotg->hsm.a_vbus_vld = 1;
764 /* defautly power the bus */
765 iotg->hsm.a_bus_req = 1;
766 iotg->hsm.a_bus_drop = 0;
767 /* defautly don't request bus as B device */
768 iotg->hsm.b_bus_req = 0;
769 /* no system error */
770 iotg->hsm.a_clr_err = 0;
772 langwell_otg_phy_low_power_wait(1);
775 static void update_hsm(void)
777 struct langwell_otg *lnw = the_transceiver;
778 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
782 val32 = readl(lnw->iotg.base + CI_OTGSC);
783 dev_dbg(lnw->dev, "%s: OTGSC value = 0x%x\n", __func__, val32);
785 iotg->hsm.id = !!(val32 & OTGSC_ID);
786 iotg->hsm.b_sess_end = !!(val32 & OTGSC_BSE);
787 iotg->hsm.b_sess_vld = !!(val32 & OTGSC_BSV);
788 iotg->hsm.a_sess_vld = !!(val32 & OTGSC_ASV);
789 iotg->hsm.a_vbus_vld = !!(val32 & OTGSC_AVV);
792 static irqreturn_t otg_dummy_irq(int irq, void *_dev)
794 struct langwell_otg *lnw = the_transceiver;
795 void __iomem *reg_base = _dev;
799 val = readl(reg_base + CI_USBMODE);
800 if ((val & USBMODE_CM) != USBMODE_DEVICE)
803 val = readl(reg_base + CI_USBSTS);
804 int_mask = val & INTR_DUMMY_MASK;
809 /* clear hsm.b_conn here since host driver can't detect it
810 * otg_dummy_irq called means B-disconnect happened.
812 if (lnw->iotg.hsm.b_conn) {
813 lnw->iotg.hsm.b_conn = 0;
814 if (spin_trylock(&lnw->wq_lock)) {
815 langwell_update_transceiver();
816 spin_unlock(&lnw->wq_lock);
820 /* Clear interrupts */
821 writel(int_mask, reg_base + CI_USBSTS);
825 static irqreturn_t otg_irq(int irq, void *_dev)
827 struct langwell_otg *lnw = _dev;
828 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
833 int_sts = readl(lnw->iotg.base + CI_OTGSC);
834 int_en = (int_sts & OTGSC_INTEN_MASK) >> 8;
835 int_mask = int_sts & int_en;
839 if (int_mask & OTGSC_IDIS) {
840 dev_dbg(lnw->dev, "%s: id change int\n", __func__);
841 iotg->hsm.id = (int_sts & OTGSC_ID) ? 1 : 0;
842 dev_dbg(lnw->dev, "id = %d\n", iotg->hsm.id);
845 if (int_mask & OTGSC_DPIS) {
846 dev_dbg(lnw->dev, "%s: data pulse int\n", __func__);
847 iotg->hsm.a_srp_det = (int_sts & OTGSC_DPS) ? 1 : 0;
848 dev_dbg(lnw->dev, "data pulse = %d\n", iotg->hsm.a_srp_det);
851 if (int_mask & OTGSC_BSEIS) {
852 dev_dbg(lnw->dev, "%s: b session end int\n", __func__);
853 iotg->hsm.b_sess_end = (int_sts & OTGSC_BSE) ? 1 : 0;
854 dev_dbg(lnw->dev, "b_sess_end = %d\n", iotg->hsm.b_sess_end);
857 if (int_mask & OTGSC_BSVIS) {
858 dev_dbg(lnw->dev, "%s: b session valid int\n", __func__);
859 iotg->hsm.b_sess_vld = (int_sts & OTGSC_BSV) ? 1 : 0;
860 dev_dbg(lnw->dev, "b_sess_vld = %d\n", iotg->hsm.b_sess_end);
863 if (int_mask & OTGSC_ASVIS) {
864 dev_dbg(lnw->dev, "%s: a session valid int\n", __func__);
865 iotg->hsm.a_sess_vld = (int_sts & OTGSC_ASV) ? 1 : 0;
866 dev_dbg(lnw->dev, "a_sess_vld = %d\n", iotg->hsm.a_sess_vld);
869 if (int_mask & OTGSC_AVVIS) {
870 dev_dbg(lnw->dev, "%s: a vbus valid int\n", __func__);
871 iotg->hsm.a_vbus_vld = (int_sts & OTGSC_AVV) ? 1 : 0;
872 dev_dbg(lnw->dev, "a_vbus_vld = %d\n", iotg->hsm.a_vbus_vld);
876 if (int_mask & OTGSC_1MSS) {
877 /* need to schedule otg_work if any timer is expired */
878 if (langwell_otg_tick_timer(&int_sts))
882 writel((int_sts & ~OTGSC_INTSTS_MASK) | int_mask,
883 lnw->iotg.base + CI_OTGSC);
885 langwell_update_transceiver();
890 static int langwell_otg_iotg_notify(struct notifier_block *nb,
891 unsigned long action, void *data)
893 struct langwell_otg *lnw = the_transceiver;
894 struct intel_mid_otg_xceiv *iotg = data;
904 case MID_OTG_NOTIFY_CONNECT:
905 dev_dbg(lnw->dev, "Lnw OTG Notify Connect Event\n");
906 if (iotg->otg.default_a == 1)
907 iotg->hsm.b_conn = 1;
909 iotg->hsm.a_conn = 1;
912 case MID_OTG_NOTIFY_DISCONN:
913 dev_dbg(lnw->dev, "Lnw OTG Notify Disconnect Event\n");
914 if (iotg->otg.default_a == 1)
915 iotg->hsm.b_conn = 0;
917 iotg->hsm.a_conn = 0;
920 case MID_OTG_NOTIFY_HSUSPEND:
921 dev_dbg(lnw->dev, "Lnw OTG Notify Host Bus suspend Event\n");
922 if (iotg->otg.default_a == 1)
923 iotg->hsm.a_suspend_req = 1;
925 iotg->hsm.b_bus_req = 0;
928 case MID_OTG_NOTIFY_HRESUME:
929 dev_dbg(lnw->dev, "Lnw OTG Notify Host Bus resume Event\n");
930 if (iotg->otg.default_a == 1)
931 iotg->hsm.b_bus_resume = 1;
934 case MID_OTG_NOTIFY_CSUSPEND:
935 dev_dbg(lnw->dev, "Lnw OTG Notify Client Bus suspend Event\n");
936 if (iotg->otg.default_a == 1) {
937 if (iotg->hsm.b_bus_suspend_vld == 2) {
938 iotg->hsm.b_bus_suspend = 1;
939 iotg->hsm.b_bus_suspend_vld = 0;
942 iotg->hsm.b_bus_suspend_vld++;
946 if (iotg->hsm.a_bus_suspend == 0) {
947 iotg->hsm.a_bus_suspend = 1;
952 case MID_OTG_NOTIFY_CRESUME:
953 dev_dbg(lnw->dev, "Lnw OTG Notify Client Bus resume Event\n");
954 if (iotg->otg.default_a == 0)
955 iotg->hsm.a_bus_suspend = 0;
958 case MID_OTG_NOTIFY_HOSTADD:
959 dev_dbg(lnw->dev, "Lnw OTG Nofity Host Driver Add\n");
962 case MID_OTG_NOTIFY_HOSTREMOVE:
963 dev_dbg(lnw->dev, "Lnw OTG Nofity Host Driver remove\n");
966 case MID_OTG_NOTIFY_CLIENTADD:
967 dev_dbg(lnw->dev, "Lnw OTG Nofity Client Driver Add\n");
970 case MID_OTG_NOTIFY_CLIENTREMOVE:
971 dev_dbg(lnw->dev, "Lnw OTG Nofity Client Driver remove\n");
975 dev_dbg(lnw->dev, "Lnw OTG Nofity unknown notify message\n");
980 langwell_update_transceiver();
985 static void langwell_otg_work(struct work_struct *work)
987 struct langwell_otg *lnw;
988 struct intel_mid_otg_xceiv *iotg;
990 struct pci_dev *pdev;
992 lnw = container_of(work, struct langwell_otg, work);
994 pdev = to_pci_dev(lnw->dev);
996 dev_dbg(lnw->dev, "%s: old state = %s\n", __func__,
997 state_string(iotg->otg.state));
999 switch (iotg->otg.state) {
1000 case OTG_STATE_UNDEFINED:
1001 case OTG_STATE_B_IDLE:
1002 if (!iotg->hsm.id) {
1003 langwell_otg_del_timer(b_srp_init_tmr);
1004 del_timer_sync(&lnw->hsm_timer);
1006 iotg->otg.default_a = 1;
1007 iotg->hsm.a_srp_det = 0;
1009 langwell_otg_chrg_vbus(0);
1011 langwell_otg_phy_low_power(1);
1013 iotg->otg.state = OTG_STATE_A_IDLE;
1014 langwell_update_transceiver();
1015 } else if (iotg->hsm.b_sess_vld) {
1016 langwell_otg_del_timer(b_srp_init_tmr);
1017 del_timer_sync(&lnw->hsm_timer);
1018 iotg->hsm.b_sess_end = 0;
1019 iotg->hsm.a_bus_suspend = 0;
1020 langwell_otg_chrg_vbus(0);
1022 if (lnw->iotg.start_peripheral) {
1023 lnw->iotg.start_peripheral(&lnw->iotg);
1024 iotg->otg.state = OTG_STATE_B_PERIPHERAL;
1026 dev_dbg(lnw->dev, "client driver not loaded\n");
1028 } else if (iotg->hsm.b_srp_init_tmout) {
1029 iotg->hsm.b_srp_init_tmout = 0;
1030 dev_warn(lnw->dev, "SRP init timeout\n");
1031 } else if (iotg->hsm.b_srp_fail_tmout) {
1032 iotg->hsm.b_srp_fail_tmout = 0;
1033 iotg->hsm.b_bus_req = 0;
1035 /* No silence failure */
1036 langwell_otg_nsf_msg(6);
1037 } else if (iotg->hsm.b_bus_req && iotg->hsm.b_sess_end) {
1038 del_timer_sync(&lnw->hsm_timer);
1039 /* workaround for b_se0_srp detection */
1040 retval = langwell_otg_check_se0_srp(0);
1042 iotg->hsm.b_bus_req = 0;
1043 dev_dbg(lnw->dev, "LS isn't SE0, try later\n");
1045 /* clear the PHCD before start srp */
1046 langwell_otg_phy_low_power(0);
1049 langwell_otg_add_timer(b_srp_init_tmr);
1050 iotg->otg.start_srp(&iotg->otg);
1051 langwell_otg_del_timer(b_srp_init_tmr);
1052 langwell_otg_add_ktimer(TB_SRP_FAIL_TMR);
1054 /* reset PHY low power mode here */
1055 langwell_otg_phy_low_power_wait(1);
1059 case OTG_STATE_B_SRP_INIT:
1060 if (!iotg->hsm.id) {
1061 iotg->otg.default_a = 1;
1062 iotg->hsm.a_srp_det = 0;
1065 iotg->otg.set_vbus(&iotg->otg, false);
1066 langwell_otg_chrg_vbus(0);
1068 langwell_otg_phy_low_power(1);
1069 iotg->otg.state = OTG_STATE_A_IDLE;
1070 langwell_update_transceiver();
1071 } else if (iotg->hsm.b_sess_vld) {
1072 langwell_otg_chrg_vbus(0);
1073 if (lnw->iotg.start_peripheral) {
1074 lnw->iotg.start_peripheral(&lnw->iotg);
1075 iotg->otg.state = OTG_STATE_B_PERIPHERAL;
1077 dev_dbg(lnw->dev, "client driver not loaded\n");
1080 case OTG_STATE_B_PERIPHERAL:
1081 if (!iotg->hsm.id) {
1082 iotg->otg.default_a = 1;
1083 iotg->hsm.a_srp_det = 0;
1085 langwell_otg_chrg_vbus(0);
1087 if (lnw->iotg.stop_peripheral)
1088 lnw->iotg.stop_peripheral(&lnw->iotg);
1091 "client driver has been removed.\n");
1094 langwell_otg_phy_low_power(1);
1095 iotg->otg.state = OTG_STATE_A_IDLE;
1096 langwell_update_transceiver();
1097 } else if (!iotg->hsm.b_sess_vld) {
1098 iotg->hsm.b_hnp_enable = 0;
1100 if (lnw->iotg.stop_peripheral)
1101 lnw->iotg.stop_peripheral(&lnw->iotg);
1104 "client driver has been removed.\n");
1106 iotg->otg.state = OTG_STATE_B_IDLE;
1107 } else if (iotg->hsm.b_bus_req && iotg->otg.gadget &&
1108 iotg->otg.gadget->b_hnp_enable &&
1109 iotg->hsm.a_bus_suspend) {
1111 if (lnw->iotg.stop_peripheral)
1112 lnw->iotg.stop_peripheral(&lnw->iotg);
1115 "client driver has been removed.\n");
1117 langwell_otg_HAAR(1);
1118 iotg->hsm.a_conn = 0;
1120 if (lnw->iotg.start_host) {
1121 lnw->iotg.start_host(&lnw->iotg);
1122 iotg->otg.state = OTG_STATE_B_WAIT_ACON;
1125 "host driver not loaded.\n");
1127 iotg->hsm.a_bus_resume = 0;
1128 langwell_otg_add_ktimer(TB_ASE0_BRST_TMR);
1132 case OTG_STATE_B_WAIT_ACON:
1133 if (!iotg->hsm.id) {
1134 /* delete hsm timer for b_ase0_brst_tmr */
1135 del_timer_sync(&lnw->hsm_timer);
1137 iotg->otg.default_a = 1;
1138 iotg->hsm.a_srp_det = 0;
1140 langwell_otg_chrg_vbus(0);
1142 langwell_otg_HAAR(0);
1143 if (lnw->iotg.stop_host)
1144 lnw->iotg.stop_host(&lnw->iotg);
1147 "host driver has been removed.\n");
1150 langwell_otg_phy_low_power(1);
1151 iotg->otg.state = OTG_STATE_A_IDLE;
1152 langwell_update_transceiver();
1153 } else if (!iotg->hsm.b_sess_vld) {
1154 /* delete hsm timer for b_ase0_brst_tmr */
1155 del_timer_sync(&lnw->hsm_timer);
1157 iotg->hsm.b_hnp_enable = 0;
1158 iotg->hsm.b_bus_req = 0;
1160 langwell_otg_chrg_vbus(0);
1161 langwell_otg_HAAR(0);
1163 if (lnw->iotg.stop_host)
1164 lnw->iotg.stop_host(&lnw->iotg);
1167 "host driver has been removed.\n");
1170 langwell_otg_phy_low_power(1);
1171 iotg->otg.state = OTG_STATE_B_IDLE;
1172 } else if (iotg->hsm.a_conn) {
1173 /* delete hsm timer for b_ase0_brst_tmr */
1174 del_timer_sync(&lnw->hsm_timer);
1176 langwell_otg_HAAR(0);
1177 iotg->otg.state = OTG_STATE_B_HOST;
1178 langwell_update_transceiver();
1179 } else if (iotg->hsm.a_bus_resume ||
1180 iotg->hsm.b_ase0_brst_tmout) {
1181 /* delete hsm timer for b_ase0_brst_tmr */
1182 del_timer_sync(&lnw->hsm_timer);
1184 langwell_otg_HAAR(0);
1185 langwell_otg_nsf_msg(7);
1187 if (lnw->iotg.stop_host)
1188 lnw->iotg.stop_host(&lnw->iotg);
1191 "host driver has been removed.\n");
1193 iotg->hsm.a_bus_suspend = 0;
1194 iotg->hsm.b_bus_req = 0;
1196 if (lnw->iotg.start_peripheral)
1197 lnw->iotg.start_peripheral(&lnw->iotg);
1200 "client driver not loaded.\n");
1202 iotg->otg.state = OTG_STATE_B_PERIPHERAL;
1206 case OTG_STATE_B_HOST:
1207 if (!iotg->hsm.id) {
1208 iotg->otg.default_a = 1;
1209 iotg->hsm.a_srp_det = 0;
1211 langwell_otg_chrg_vbus(0);
1213 if (lnw->iotg.stop_host)
1214 lnw->iotg.stop_host(&lnw->iotg);
1217 "host driver has been removed.\n");
1220 langwell_otg_phy_low_power(1);
1221 iotg->otg.state = OTG_STATE_A_IDLE;
1222 langwell_update_transceiver();
1223 } else if (!iotg->hsm.b_sess_vld) {
1224 iotg->hsm.b_hnp_enable = 0;
1225 iotg->hsm.b_bus_req = 0;
1227 langwell_otg_chrg_vbus(0);
1228 if (lnw->iotg.stop_host)
1229 lnw->iotg.stop_host(&lnw->iotg);
1232 "host driver has been removed.\n");
1235 langwell_otg_phy_low_power(1);
1236 iotg->otg.state = OTG_STATE_B_IDLE;
1237 } else if ((!iotg->hsm.b_bus_req) ||
1238 (!iotg->hsm.a_conn)) {
1239 iotg->hsm.b_bus_req = 0;
1240 langwell_otg_loc_sof(0);
1242 if (lnw->iotg.stop_host)
1243 lnw->iotg.stop_host(&lnw->iotg);
1246 "host driver has been removed.\n");
1248 iotg->hsm.a_bus_suspend = 0;
1250 if (lnw->iotg.start_peripheral)
1251 lnw->iotg.start_peripheral(&lnw->iotg);
1254 "client driver not loaded.\n");
1256 iotg->otg.state = OTG_STATE_B_PERIPHERAL;
1260 case OTG_STATE_A_IDLE:
1261 iotg->otg.default_a = 1;
1263 iotg->otg.default_a = 0;
1264 iotg->hsm.b_bus_req = 0;
1265 iotg->hsm.vbus_srp_up = 0;
1267 langwell_otg_chrg_vbus(0);
1269 langwell_otg_phy_low_power(1);
1270 iotg->otg.state = OTG_STATE_B_IDLE;
1271 langwell_update_transceiver();
1272 } else if (!iotg->hsm.a_bus_drop &&
1273 (iotg->hsm.a_srp_det || iotg->hsm.a_bus_req)) {
1274 langwell_otg_phy_low_power(0);
1277 iotg->otg.set_vbus(&iotg->otg, true);
1279 iotg->hsm.vbus_srp_up = 0;
1280 iotg->hsm.a_wait_vrise_tmout = 0;
1281 langwell_otg_add_timer(a_wait_vrise_tmr);
1282 iotg->otg.state = OTG_STATE_A_WAIT_VRISE;
1283 langwell_update_transceiver();
1284 } else if (!iotg->hsm.a_bus_drop && iotg->hsm.a_sess_vld) {
1285 iotg->hsm.vbus_srp_up = 1;
1286 } else if (!iotg->hsm.a_sess_vld && iotg->hsm.vbus_srp_up) {
1288 langwell_otg_phy_low_power(0);
1291 iotg->otg.set_vbus(&iotg->otg, true);
1292 iotg->hsm.a_srp_det = 1;
1293 iotg->hsm.vbus_srp_up = 0;
1294 iotg->hsm.a_wait_vrise_tmout = 0;
1295 langwell_otg_add_timer(a_wait_vrise_tmr);
1296 iotg->otg.state = OTG_STATE_A_WAIT_VRISE;
1297 langwell_update_transceiver();
1298 } else if (!iotg->hsm.a_sess_vld &&
1299 !iotg->hsm.vbus_srp_up) {
1300 langwell_otg_phy_low_power(1);
1303 case OTG_STATE_A_WAIT_VRISE:
1305 langwell_otg_del_timer(a_wait_vrise_tmr);
1306 iotg->hsm.b_bus_req = 0;
1307 iotg->otg.default_a = 0;
1310 iotg->otg.set_vbus(&iotg->otg, false);
1312 langwell_otg_phy_low_power_wait(1);
1313 iotg->otg.state = OTG_STATE_B_IDLE;
1314 } else if (iotg->hsm.a_vbus_vld) {
1315 langwell_otg_del_timer(a_wait_vrise_tmr);
1316 iotg->hsm.b_conn = 0;
1317 if (lnw->iotg.start_host)
1318 lnw->iotg.start_host(&lnw->iotg);
1320 dev_dbg(lnw->dev, "host driver not loaded.\n");
1324 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR);
1325 iotg->otg.state = OTG_STATE_A_WAIT_BCON;
1326 } else if (iotg->hsm.a_wait_vrise_tmout) {
1327 iotg->hsm.b_conn = 0;
1328 if (iotg->hsm.a_vbus_vld) {
1329 if (lnw->iotg.start_host)
1330 lnw->iotg.start_host(&lnw->iotg);
1333 "host driver not loaded.\n");
1336 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR);
1337 iotg->otg.state = OTG_STATE_A_WAIT_BCON;
1341 iotg->otg.set_vbus(&iotg->otg, false);
1342 langwell_otg_phy_low_power_wait(1);
1343 iotg->otg.state = OTG_STATE_A_VBUS_ERR;
1347 case OTG_STATE_A_WAIT_BCON:
1349 /* delete hsm timer for a_wait_bcon_tmr */
1350 del_timer_sync(&lnw->hsm_timer);
1352 iotg->otg.default_a = 0;
1353 iotg->hsm.b_bus_req = 0;
1355 if (lnw->iotg.stop_host)
1356 lnw->iotg.stop_host(&lnw->iotg);
1359 "host driver has been removed.\n");
1362 iotg->otg.set_vbus(&iotg->otg, false);
1364 langwell_otg_phy_low_power_wait(1);
1365 iotg->otg.state = OTG_STATE_B_IDLE;
1366 langwell_update_transceiver();
1367 } else if (!iotg->hsm.a_vbus_vld) {
1368 /* delete hsm timer for a_wait_bcon_tmr */
1369 del_timer_sync(&lnw->hsm_timer);
1371 if (lnw->iotg.stop_host)
1372 lnw->iotg.stop_host(&lnw->iotg);
1375 "host driver has been removed.\n");
1378 iotg->otg.set_vbus(&iotg->otg, false);
1379 langwell_otg_phy_low_power_wait(1);
1380 iotg->otg.state = OTG_STATE_A_VBUS_ERR;
1381 } else if (iotg->hsm.a_bus_drop ||
1382 (iotg->hsm.a_wait_bcon_tmout &&
1383 !iotg->hsm.a_bus_req)) {
1384 /* delete hsm timer for a_wait_bcon_tmr */
1385 del_timer_sync(&lnw->hsm_timer);
1387 if (lnw->iotg.stop_host)
1388 lnw->iotg.stop_host(&lnw->iotg);
1391 "host driver has been removed.\n");
1394 iotg->otg.set_vbus(&iotg->otg, false);
1395 iotg->otg.state = OTG_STATE_A_WAIT_VFALL;
1396 } else if (iotg->hsm.b_conn) {
1397 /* delete hsm timer for a_wait_bcon_tmr */
1398 del_timer_sync(&lnw->hsm_timer);
1400 iotg->hsm.a_suspend_req = 0;
1401 iotg->otg.state = OTG_STATE_A_HOST;
1402 if (iotg->hsm.a_srp_det && iotg->otg.host &&
1403 !iotg->otg.host->b_hnp_enable) {
1404 /* SRP capable peripheral-only device */
1405 iotg->hsm.a_bus_req = 1;
1406 iotg->hsm.a_srp_det = 0;
1407 } else if (!iotg->hsm.a_bus_req && iotg->otg.host &&
1408 iotg->otg.host->b_hnp_enable) {
1409 /* It is not safe enough to do a fast
1410 * transistion from A_WAIT_BCON to
1413 if (iotg->hsm.a_bus_req)
1416 if (request_irq(pdev->irq,
1417 otg_dummy_irq, IRQF_SHARED,
1418 driver_name, iotg->base) != 0) {
1420 "request interrupt %d fail\n",
1424 langwell_otg_HABA(1);
1425 iotg->hsm.b_bus_resume = 0;
1426 iotg->hsm.a_aidl_bdis_tmout = 0;
1428 langwell_otg_loc_sof(0);
1429 /* clear PHCD to enable HW timer */
1430 langwell_otg_phy_low_power(0);
1431 langwell_otg_add_timer(a_aidl_bdis_tmr);
1432 iotg->otg.state = OTG_STATE_A_SUSPEND;
1433 } else if (!iotg->hsm.a_bus_req && iotg->otg.host &&
1434 !iotg->otg.host->b_hnp_enable) {
1435 if (lnw->iotg.stop_host)
1436 lnw->iotg.stop_host(&lnw->iotg);
1439 "host driver removed.\n");
1442 iotg->otg.set_vbus(&iotg->otg, false);
1443 iotg->otg.state = OTG_STATE_A_WAIT_VFALL;
1447 case OTG_STATE_A_HOST:
1449 iotg->otg.default_a = 0;
1450 iotg->hsm.b_bus_req = 0;
1452 if (lnw->iotg.stop_host)
1453 lnw->iotg.stop_host(&lnw->iotg);
1456 "host driver has been removed.\n");
1459 iotg->otg.set_vbus(&iotg->otg, false);
1461 langwell_otg_phy_low_power_wait(1);
1462 iotg->otg.state = OTG_STATE_B_IDLE;
1463 langwell_update_transceiver();
1464 } else if (iotg->hsm.a_bus_drop ||
1466 !iotg->otg.host->b_hnp_enable &&
1467 !iotg->hsm.a_bus_req)) {
1468 if (lnw->iotg.stop_host)
1469 lnw->iotg.stop_host(&lnw->iotg);
1472 "host driver has been removed.\n");
1475 iotg->otg.set_vbus(&iotg->otg, false);
1476 iotg->otg.state = OTG_STATE_A_WAIT_VFALL;
1477 } else if (!iotg->hsm.a_vbus_vld) {
1478 if (lnw->iotg.stop_host)
1479 lnw->iotg.stop_host(&lnw->iotg);
1482 "host driver has been removed.\n");
1485 iotg->otg.set_vbus(&iotg->otg, false);
1486 langwell_otg_phy_low_power_wait(1);
1487 iotg->otg.state = OTG_STATE_A_VBUS_ERR;
1488 } else if (iotg->otg.host &&
1489 iotg->otg.host->b_hnp_enable &&
1490 !iotg->hsm.a_bus_req) {
1491 /* Set HABA to enable hardware assistance to signal
1492 * A-connect after receiver B-disconnect. Hardware
1493 * will then set client mode and enable URE, SLE and
1494 * PCE after the assistance. otg_dummy_irq is used to
1495 * clean these ints when client driver is not resumed.
1497 if (request_irq(pdev->irq, otg_dummy_irq, IRQF_SHARED,
1498 driver_name, iotg->base) != 0) {
1500 "request interrupt %d failed\n",
1505 langwell_otg_HABA(1);
1506 iotg->hsm.b_bus_resume = 0;
1507 iotg->hsm.a_aidl_bdis_tmout = 0;
1508 langwell_otg_loc_sof(0);
1509 /* clear PHCD to enable HW timer */
1510 langwell_otg_phy_low_power(0);
1511 langwell_otg_add_timer(a_aidl_bdis_tmr);
1512 iotg->otg.state = OTG_STATE_A_SUSPEND;
1513 } else if (!iotg->hsm.b_conn || !iotg->hsm.a_bus_req) {
1514 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR);
1515 iotg->otg.state = OTG_STATE_A_WAIT_BCON;
1518 case OTG_STATE_A_SUSPEND:
1520 langwell_otg_del_timer(a_aidl_bdis_tmr);
1521 langwell_otg_HABA(0);
1522 free_irq(pdev->irq, iotg->base);
1523 iotg->otg.default_a = 0;
1524 iotg->hsm.b_bus_req = 0;
1526 if (lnw->iotg.stop_host)
1527 lnw->iotg.stop_host(&lnw->iotg);
1530 "host driver has been removed.\n");
1533 iotg->otg.set_vbus(&iotg->otg, false);
1535 langwell_otg_phy_low_power(1);
1536 iotg->otg.state = OTG_STATE_B_IDLE;
1537 langwell_update_transceiver();
1538 } else if (iotg->hsm.a_bus_req ||
1539 iotg->hsm.b_bus_resume) {
1540 langwell_otg_del_timer(a_aidl_bdis_tmr);
1541 langwell_otg_HABA(0);
1542 free_irq(pdev->irq, iotg->base);
1543 iotg->hsm.a_suspend_req = 0;
1544 langwell_otg_loc_sof(1);
1545 iotg->otg.state = OTG_STATE_A_HOST;
1546 } else if (iotg->hsm.a_aidl_bdis_tmout ||
1547 iotg->hsm.a_bus_drop) {
1548 langwell_otg_del_timer(a_aidl_bdis_tmr);
1549 langwell_otg_HABA(0);
1550 free_irq(pdev->irq, iotg->base);
1551 if (lnw->iotg.stop_host)
1552 lnw->iotg.stop_host(&lnw->iotg);
1555 "host driver has been removed.\n");
1558 iotg->otg.set_vbus(&iotg->otg, false);
1559 iotg->otg.state = OTG_STATE_A_WAIT_VFALL;
1560 } else if (!iotg->hsm.b_conn && iotg->otg.host &&
1561 iotg->otg.host->b_hnp_enable) {
1562 langwell_otg_del_timer(a_aidl_bdis_tmr);
1563 langwell_otg_HABA(0);
1564 free_irq(pdev->irq, iotg->base);
1566 if (lnw->iotg.stop_host)
1567 lnw->iotg.stop_host(&lnw->iotg);
1570 "host driver has been removed.\n");
1572 iotg->hsm.b_bus_suspend = 0;
1573 iotg->hsm.b_bus_suspend_vld = 0;
1576 if (lnw->iotg.start_peripheral)
1577 lnw->iotg.start_peripheral(&lnw->iotg);
1580 "client driver not loaded.\n");
1582 langwell_otg_add_ktimer(TB_BUS_SUSPEND_TMR);
1583 iotg->otg.state = OTG_STATE_A_PERIPHERAL;
1585 } else if (!iotg->hsm.a_vbus_vld) {
1586 langwell_otg_del_timer(a_aidl_bdis_tmr);
1587 langwell_otg_HABA(0);
1588 free_irq(pdev->irq, iotg->base);
1589 if (lnw->iotg.stop_host)
1590 lnw->iotg.stop_host(&lnw->iotg);
1593 "host driver has been removed.\n");
1596 iotg->otg.set_vbus(&iotg->otg, false);
1597 langwell_otg_phy_low_power_wait(1);
1598 iotg->otg.state = OTG_STATE_A_VBUS_ERR;
1601 case OTG_STATE_A_PERIPHERAL:
1603 /* delete hsm timer for b_bus_suspend_tmr */
1604 del_timer_sync(&lnw->hsm_timer);
1605 iotg->otg.default_a = 0;
1606 iotg->hsm.b_bus_req = 0;
1607 if (lnw->iotg.stop_peripheral)
1608 lnw->iotg.stop_peripheral(&lnw->iotg);
1611 "client driver has been removed.\n");
1614 iotg->otg.set_vbus(&iotg->otg, false);
1616 langwell_otg_phy_low_power_wait(1);
1617 iotg->otg.state = OTG_STATE_B_IDLE;
1618 langwell_update_transceiver();
1619 } else if (!iotg->hsm.a_vbus_vld) {
1620 /* delete hsm timer for b_bus_suspend_tmr */
1621 del_timer_sync(&lnw->hsm_timer);
1623 if (lnw->iotg.stop_peripheral)
1624 lnw->iotg.stop_peripheral(&lnw->iotg);
1627 "client driver has been removed.\n");
1630 iotg->otg.set_vbus(&iotg->otg, false);
1631 langwell_otg_phy_low_power_wait(1);
1632 iotg->otg.state = OTG_STATE_A_VBUS_ERR;
1633 } else if (iotg->hsm.a_bus_drop) {
1634 /* delete hsm timer for b_bus_suspend_tmr */
1635 del_timer_sync(&lnw->hsm_timer);
1637 if (lnw->iotg.stop_peripheral)
1638 lnw->iotg.stop_peripheral(&lnw->iotg);
1641 "client driver has been removed.\n");
1644 iotg->otg.set_vbus(&iotg->otg, false);
1645 iotg->otg.state = OTG_STATE_A_WAIT_VFALL;
1646 } else if (iotg->hsm.b_bus_suspend) {
1647 /* delete hsm timer for b_bus_suspend_tmr */
1648 del_timer_sync(&lnw->hsm_timer);
1650 if (lnw->iotg.stop_peripheral)
1651 lnw->iotg.stop_peripheral(&lnw->iotg);
1654 "client driver has been removed.\n");
1656 if (lnw->iotg.start_host)
1657 lnw->iotg.start_host(&lnw->iotg);
1660 "host driver not loaded.\n");
1661 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR);
1662 iotg->otg.state = OTG_STATE_A_WAIT_BCON;
1663 } else if (iotg->hsm.b_bus_suspend_tmout) {
1665 val = readl(lnw->iotg.base + CI_PORTSC1);
1666 if (!(val & PORTSC_SUSP))
1669 if (lnw->iotg.stop_peripheral)
1670 lnw->iotg.stop_peripheral(&lnw->iotg);
1673 "client driver has been removed.\n");
1675 if (lnw->iotg.start_host)
1676 lnw->iotg.start_host(&lnw->iotg);
1679 "host driver not loaded.\n");
1680 langwell_otg_add_ktimer(TA_WAIT_BCON_TMR);
1681 iotg->otg.state = OTG_STATE_A_WAIT_BCON;
1684 case OTG_STATE_A_VBUS_ERR:
1686 iotg->otg.default_a = 0;
1687 iotg->hsm.a_clr_err = 0;
1688 iotg->hsm.a_srp_det = 0;
1690 langwell_otg_phy_low_power(1);
1691 iotg->otg.state = OTG_STATE_B_IDLE;
1692 langwell_update_transceiver();
1693 } else if (iotg->hsm.a_clr_err) {
1694 iotg->hsm.a_clr_err = 0;
1695 iotg->hsm.a_srp_det = 0;
1698 if (iotg->otg.state == OTG_STATE_A_IDLE)
1699 langwell_update_transceiver();
1701 /* FW will clear PHCD bit when any VBus
1702 * event detected. Reset PHCD to 1 again */
1703 langwell_otg_phy_low_power(1);
1706 case OTG_STATE_A_WAIT_VFALL:
1708 iotg->otg.default_a = 0;
1710 langwell_otg_phy_low_power(1);
1711 iotg->otg.state = OTG_STATE_B_IDLE;
1712 langwell_update_transceiver();
1713 } else if (iotg->hsm.a_bus_req) {
1716 iotg->otg.set_vbus(&iotg->otg, true);
1717 iotg->hsm.a_wait_vrise_tmout = 0;
1718 langwell_otg_add_timer(a_wait_vrise_tmr);
1719 iotg->otg.state = OTG_STATE_A_WAIT_VRISE;
1720 } else if (!iotg->hsm.a_sess_vld) {
1721 iotg->hsm.a_srp_det = 0;
1723 langwell_otg_phy_low_power(1);
1724 iotg->otg.state = OTG_STATE_A_IDLE;
1731 dev_dbg(lnw->dev, "%s: new state = %s\n", __func__,
1732 state_string(iotg->otg.state));
1736 show_registers(struct device *_dev, struct device_attribute *attr, char *buf)
1738 struct langwell_otg *lnw = the_transceiver;
1745 t = scnprintf(next, size,
1749 "USBINTR = 0x%08x\n"
1750 "ASYNCLISTADDR = 0x%08x\n"
1751 "PORTSC1 = 0x%08x\n"
1752 "HOSTPC1 = 0x%08x\n"
1754 "USBMODE = 0x%08x\n",
1755 readl(lnw->iotg.base + 0x30),
1756 readl(lnw->iotg.base + 0x34),
1757 readl(lnw->iotg.base + 0x38),
1758 readl(lnw->iotg.base + 0x48),
1759 readl(lnw->iotg.base + 0x74),
1760 readl(lnw->iotg.base + 0xb4),
1761 readl(lnw->iotg.base + 0xf4),
1762 readl(lnw->iotg.base + 0xf8)
1767 return PAGE_SIZE - size;
1769 static DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
1772 show_hsm(struct device *_dev, struct device_attribute *attr, char *buf)
1774 struct langwell_otg *lnw = the_transceiver;
1775 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
1783 iotg->hsm.a_set_b_hnp_en = iotg->otg.host->b_hnp_enable;
1785 if (iotg->otg.gadget)
1786 iotg->hsm.b_hnp_enable = iotg->otg.gadget->b_hnp_enable;
1788 t = scnprintf(next, size,
1790 "current state = %s\n"
1791 "a_bus_resume = \t%d\n"
1792 "a_bus_suspend = \t%d\n"
1794 "a_sess_vld = \t%d\n"
1795 "a_srp_det = \t%d\n"
1796 "a_vbus_vld = \t%d\n"
1797 "b_bus_resume = \t%d\n"
1798 "b_bus_suspend = \t%d\n"
1800 "b_se0_srp = \t%d\n"
1801 "b_sess_end = \t%d\n"
1802 "b_sess_vld = \t%d\n"
1804 "a_set_b_hnp_en = \t%d\n"
1805 "b_srp_done = \t%d\n"
1806 "b_hnp_enable = \t%d\n"
1807 "a_wait_vrise_tmout = \t%d\n"
1808 "a_wait_bcon_tmout = \t%d\n"
1809 "a_aidl_bdis_tmout = \t%d\n"
1810 "b_ase0_brst_tmout = \t%d\n"
1811 "a_bus_drop = \t%d\n"
1812 "a_bus_req = \t%d\n"
1813 "a_clr_err = \t%d\n"
1814 "a_suspend_req = \t%d\n"
1815 "b_bus_req = \t%d\n"
1816 "b_bus_suspend_tmout = \t%d\n"
1817 "b_bus_suspend_vld = \t%d\n",
1818 state_string(iotg->otg.state),
1819 iotg->hsm.a_bus_resume,
1820 iotg->hsm.a_bus_suspend,
1822 iotg->hsm.a_sess_vld,
1823 iotg->hsm.a_srp_det,
1824 iotg->hsm.a_vbus_vld,
1825 iotg->hsm.b_bus_resume,
1826 iotg->hsm.b_bus_suspend,
1828 iotg->hsm.b_se0_srp,
1829 iotg->hsm.b_sess_end,
1830 iotg->hsm.b_sess_vld,
1832 iotg->hsm.a_set_b_hnp_en,
1833 iotg->hsm.b_srp_done,
1834 iotg->hsm.b_hnp_enable,
1835 iotg->hsm.a_wait_vrise_tmout,
1836 iotg->hsm.a_wait_bcon_tmout,
1837 iotg->hsm.a_aidl_bdis_tmout,
1838 iotg->hsm.b_ase0_brst_tmout,
1839 iotg->hsm.a_bus_drop,
1840 iotg->hsm.a_bus_req,
1841 iotg->hsm.a_clr_err,
1842 iotg->hsm.a_suspend_req,
1843 iotg->hsm.b_bus_req,
1844 iotg->hsm.b_bus_suspend_tmout,
1845 iotg->hsm.b_bus_suspend_vld
1850 return PAGE_SIZE - size;
1852 static DEVICE_ATTR(hsm, S_IRUGO, show_hsm, NULL);
1855 get_a_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
1857 struct langwell_otg *lnw = the_transceiver;
1864 t = scnprintf(next, size, "%d", lnw->iotg.hsm.a_bus_req);
1868 return PAGE_SIZE - size;
1872 set_a_bus_req(struct device *dev, struct device_attribute *attr,
1873 const char *buf, size_t count)
1875 struct langwell_otg *lnw = the_transceiver;
1876 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
1878 if (!iotg->otg.default_a)
1883 if (buf[0] == '0') {
1884 iotg->hsm.a_bus_req = 0;
1885 dev_dbg(lnw->dev, "User request: a_bus_req = 0\n");
1886 } else if (buf[0] == '1') {
1887 /* If a_bus_drop is TRUE, a_bus_req can't be set */
1888 if (iotg->hsm.a_bus_drop)
1890 iotg->hsm.a_bus_req = 1;
1891 dev_dbg(lnw->dev, "User request: a_bus_req = 1\n");
1893 if (spin_trylock(&lnw->wq_lock)) {
1894 langwell_update_transceiver();
1895 spin_unlock(&lnw->wq_lock);
1899 static DEVICE_ATTR(a_bus_req, S_IRUGO | S_IWUGO, get_a_bus_req, set_a_bus_req);
1902 get_a_bus_drop(struct device *dev, struct device_attribute *attr, char *buf)
1904 struct langwell_otg *lnw = the_transceiver;
1911 t = scnprintf(next, size, "%d", lnw->iotg.hsm.a_bus_drop);
1915 return PAGE_SIZE - size;
1919 set_a_bus_drop(struct device *dev, struct device_attribute *attr,
1920 const char *buf, size_t count)
1922 struct langwell_otg *lnw = the_transceiver;
1923 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
1925 if (!iotg->otg.default_a)
1930 if (buf[0] == '0') {
1931 iotg->hsm.a_bus_drop = 0;
1932 dev_dbg(lnw->dev, "User request: a_bus_drop = 0\n");
1933 } else if (buf[0] == '1') {
1934 iotg->hsm.a_bus_drop = 1;
1935 iotg->hsm.a_bus_req = 0;
1936 dev_dbg(lnw->dev, "User request: a_bus_drop = 1\n");
1937 dev_dbg(lnw->dev, "User request: and a_bus_req = 0\n");
1939 if (spin_trylock(&lnw->wq_lock)) {
1940 langwell_update_transceiver();
1941 spin_unlock(&lnw->wq_lock);
1945 static DEVICE_ATTR(a_bus_drop, S_IRUGO | S_IWUGO,
1946 get_a_bus_drop, set_a_bus_drop);
1949 get_b_bus_req(struct device *dev, struct device_attribute *attr, char *buf)
1951 struct langwell_otg *lnw = the_transceiver;
1958 t = scnprintf(next, size, "%d", lnw->iotg.hsm.b_bus_req);
1962 return PAGE_SIZE - size;
1966 set_b_bus_req(struct device *dev, struct device_attribute *attr,
1967 const char *buf, size_t count)
1969 struct langwell_otg *lnw = the_transceiver;
1970 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
1972 if (iotg->otg.default_a)
1978 if (buf[0] == '0') {
1979 iotg->hsm.b_bus_req = 0;
1980 dev_dbg(lnw->dev, "User request: b_bus_req = 0\n");
1981 } else if (buf[0] == '1') {
1982 iotg->hsm.b_bus_req = 1;
1983 dev_dbg(lnw->dev, "User request: b_bus_req = 1\n");
1985 if (spin_trylock(&lnw->wq_lock)) {
1986 langwell_update_transceiver();
1987 spin_unlock(&lnw->wq_lock);
1991 static DEVICE_ATTR(b_bus_req, S_IRUGO | S_IWUGO, get_b_bus_req, set_b_bus_req);
1994 set_a_clr_err(struct device *dev, struct device_attribute *attr,
1995 const char *buf, size_t count)
1997 struct langwell_otg *lnw = the_transceiver;
1998 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
2000 if (!iotg->otg.default_a)
2005 if (buf[0] == '1') {
2006 iotg->hsm.a_clr_err = 1;
2007 dev_dbg(lnw->dev, "User request: a_clr_err = 1\n");
2009 if (spin_trylock(&lnw->wq_lock)) {
2010 langwell_update_transceiver();
2011 spin_unlock(&lnw->wq_lock);
2015 static DEVICE_ATTR(a_clr_err, S_IWUGO, NULL, set_a_clr_err);
2017 static struct attribute *inputs_attrs[] = {
2018 &dev_attr_a_bus_req.attr,
2019 &dev_attr_a_bus_drop.attr,
2020 &dev_attr_b_bus_req.attr,
2021 &dev_attr_a_clr_err.attr,
2025 static struct attribute_group debug_dev_attr_group = {
2027 .attrs = inputs_attrs,
2030 static int langwell_otg_probe(struct pci_dev *pdev,
2031 const struct pci_device_id *id)
2033 unsigned long resource, len;
2034 void __iomem *base = NULL;
2037 struct langwell_otg *lnw;
2038 char qname[] = "langwell_otg_queue";
2041 dev_dbg(&pdev->dev, "\notg controller is detected.\n");
2042 if (pci_enable_device(pdev) < 0) {
2047 lnw = kzalloc(sizeof *lnw, GFP_KERNEL);
2052 the_transceiver = lnw;
2054 /* control register: BAR 0 */
2055 resource = pci_resource_start(pdev, 0);
2056 len = pci_resource_len(pdev, 0);
2057 if (!request_mem_region(resource, len, driver_name)) {
2063 base = ioremap_nocache(resource, len);
2068 lnw->iotg.base = base;
2070 if (!request_mem_region(USBCFG_ADDR, USBCFG_LEN, driver_name)) {
2074 lnw->cfg_region = 1;
2076 /* For the SCCB.USBCFG register */
2077 base = ioremap_nocache(USBCFG_ADDR, USBCFG_LEN);
2085 dev_dbg(&pdev->dev, "No IRQ.\n");
2090 lnw->qwork = create_singlethread_workqueue(qname);
2092 dev_dbg(&pdev->dev, "cannot create workqueue %s\n", qname);
2096 INIT_WORK(&lnw->work, langwell_otg_work);
2098 /* OTG common part */
2099 lnw->dev = &pdev->dev;
2100 lnw->iotg.otg.dev = lnw->dev;
2101 lnw->iotg.otg.label = driver_name;
2102 lnw->iotg.otg.set_host = langwell_otg_set_host;
2103 lnw->iotg.otg.set_peripheral = langwell_otg_set_peripheral;
2104 lnw->iotg.otg.set_power = langwell_otg_set_power;
2105 lnw->iotg.otg.set_vbus = langwell_otg_set_vbus;
2106 lnw->iotg.otg.start_srp = langwell_otg_start_srp;
2107 lnw->iotg.otg.state = OTG_STATE_UNDEFINED;
2109 if (otg_set_transceiver(&lnw->iotg.otg)) {
2110 dev_dbg(lnw->dev, "can't set transceiver\n");
2118 spin_lock_init(&lnw->lock);
2119 spin_lock_init(&lnw->wq_lock);
2120 INIT_LIST_HEAD(&active_timers);
2121 retval = langwell_otg_init_timers(&lnw->iotg.hsm);
2123 dev_dbg(&pdev->dev, "Failed to init timers\n");
2127 init_timer(&lnw->hsm_timer);
2128 ATOMIC_INIT_NOTIFIER_HEAD(&lnw->iotg.iotg_notifier);
2130 lnw->iotg_notifier.notifier_call = langwell_otg_iotg_notify;
2132 retval = intel_mid_otg_register_notifier(&lnw->iotg,
2133 &lnw->iotg_notifier);
2135 dev_dbg(lnw->dev, "Failed to register notifier\n");
2139 if (request_irq(pdev->irq, otg_irq, IRQF_SHARED,
2140 driver_name, lnw) != 0) {
2141 dev_dbg(lnw->dev, "request interrupt %d failed\n", pdev->irq);
2146 /* enable OTGSC int */
2147 val32 = OTGSC_DPIE | OTGSC_BSEIE | OTGSC_BSVIE |
2148 OTGSC_ASVIE | OTGSC_AVVIE | OTGSC_IDIE | OTGSC_IDPU;
2149 writel(val32, lnw->iotg.base + CI_OTGSC);
2151 retval = device_create_file(&pdev->dev, &dev_attr_registers);
2154 "Can't register sysfs attribute: %d\n", retval);
2158 retval = device_create_file(&pdev->dev, &dev_attr_hsm);
2160 dev_dbg(lnw->dev, "Can't hsm sysfs attribute: %d\n", retval);
2164 retval = sysfs_create_group(&pdev->dev.kobj, &debug_dev_attr_group);
2167 "Can't register sysfs attr group: %d\n", retval);
2171 if (lnw->iotg.otg.state == OTG_STATE_A_IDLE)
2172 langwell_update_transceiver();
2177 if (the_transceiver)
2178 langwell_otg_remove(pdev);
2183 static void langwell_otg_remove(struct pci_dev *pdev)
2185 struct langwell_otg *lnw = the_transceiver;
2188 flush_workqueue(lnw->qwork);
2189 destroy_workqueue(lnw->qwork);
2191 intel_mid_otg_unregister_notifier(&lnw->iotg, &lnw->iotg_notifier);
2192 langwell_otg_free_timers();
2194 /* disable OTGSC interrupt as OTGSC doesn't change in reset */
2195 writel(0, lnw->iotg.base + CI_OTGSC);
2198 free_irq(pdev->irq, lnw);
2200 iounmap(lnw->usbcfg);
2201 if (lnw->cfg_region)
2202 release_mem_region(USBCFG_ADDR, USBCFG_LEN);
2204 iounmap(lnw->iotg.base);
2206 release_mem_region(pci_resource_start(pdev, 0),
2207 pci_resource_len(pdev, 0));
2209 otg_set_transceiver(NULL);
2210 pci_disable_device(pdev);
2211 sysfs_remove_group(&pdev->dev.kobj, &debug_dev_attr_group);
2212 device_remove_file(&pdev->dev, &dev_attr_hsm);
2213 device_remove_file(&pdev->dev, &dev_attr_registers);
2218 static void transceiver_suspend(struct pci_dev *pdev)
2220 pci_save_state(pdev);
2221 pci_set_power_state(pdev, PCI_D3hot);
2222 langwell_otg_phy_low_power(1);
2225 static int langwell_otg_suspend(struct pci_dev *pdev, pm_message_t message)
2227 struct langwell_otg *lnw = the_transceiver;
2228 struct intel_mid_otg_xceiv *iotg = &lnw->iotg;
2231 /* Disbale OTG interrupts */
2232 langwell_otg_intr(0);
2235 free_irq(pdev->irq, lnw);
2237 /* Prevent more otg_work */
2238 flush_workqueue(lnw->qwork);
2239 destroy_workqueue(lnw->qwork);
2243 switch (iotg->otg.state) {
2244 case OTG_STATE_A_WAIT_VFALL:
2245 iotg->otg.state = OTG_STATE_A_IDLE;
2246 case OTG_STATE_A_IDLE:
2247 case OTG_STATE_B_IDLE:
2248 case OTG_STATE_A_VBUS_ERR:
2249 transceiver_suspend(pdev);
2251 case OTG_STATE_A_WAIT_VRISE:
2252 langwell_otg_del_timer(a_wait_vrise_tmr);
2253 iotg->hsm.a_srp_det = 0;
2256 iotg->otg.set_vbus(&iotg->otg, false);
2257 iotg->otg.state = OTG_STATE_A_IDLE;
2258 transceiver_suspend(pdev);
2260 case OTG_STATE_A_WAIT_BCON:
2261 del_timer_sync(&lnw->hsm_timer);
2262 if (lnw->iotg.stop_host)
2263 lnw->iotg.stop_host(&lnw->iotg);
2265 dev_dbg(&pdev->dev, "host driver has been removed.\n");
2267 iotg->hsm.a_srp_det = 0;
2270 iotg->otg.set_vbus(&iotg->otg, false);
2271 iotg->otg.state = OTG_STATE_A_IDLE;
2272 transceiver_suspend(pdev);
2274 case OTG_STATE_A_HOST:
2275 if (lnw->iotg.stop_host)
2276 lnw->iotg.stop_host(&lnw->iotg);
2278 dev_dbg(&pdev->dev, "host driver has been removed.\n");
2280 iotg->hsm.a_srp_det = 0;
2283 iotg->otg.set_vbus(&iotg->otg, false);
2285 iotg->otg.state = OTG_STATE_A_IDLE;
2286 transceiver_suspend(pdev);
2288 case OTG_STATE_A_SUSPEND:
2289 langwell_otg_del_timer(a_aidl_bdis_tmr);
2290 langwell_otg_HABA(0);
2291 if (lnw->iotg.stop_host)
2292 lnw->iotg.stop_host(&lnw->iotg);
2294 dev_dbg(lnw->dev, "host driver has been removed.\n");
2295 iotg->hsm.a_srp_det = 0;
2298 iotg->otg.set_vbus(&iotg->otg, false);
2299 iotg->otg.state = OTG_STATE_A_IDLE;
2300 transceiver_suspend(pdev);
2302 case OTG_STATE_A_PERIPHERAL:
2303 del_timer_sync(&lnw->hsm_timer);
2305 if (lnw->iotg.stop_peripheral)
2306 lnw->iotg.stop_peripheral(&lnw->iotg);
2309 "client driver has been removed.\n");
2310 iotg->hsm.a_srp_det = 0;
2313 iotg->otg.set_vbus(&iotg->otg, false);
2314 iotg->otg.state = OTG_STATE_A_IDLE;
2315 transceiver_suspend(pdev);
2317 case OTG_STATE_B_HOST:
2318 if (lnw->iotg.stop_host)
2319 lnw->iotg.stop_host(&lnw->iotg);
2321 dev_dbg(&pdev->dev, "host driver has been removed.\n");
2322 iotg->hsm.b_bus_req = 0;
2323 iotg->otg.state = OTG_STATE_B_IDLE;
2324 transceiver_suspend(pdev);
2326 case OTG_STATE_B_PERIPHERAL:
2327 if (lnw->iotg.stop_peripheral)
2328 lnw->iotg.stop_peripheral(&lnw->iotg);
2331 "client driver has been removed.\n");
2332 iotg->otg.state = OTG_STATE_B_IDLE;
2333 transceiver_suspend(pdev);
2335 case OTG_STATE_B_WAIT_ACON:
2336 /* delete hsm timer for b_ase0_brst_tmr */
2337 del_timer_sync(&lnw->hsm_timer);
2339 langwell_otg_HAAR(0);
2341 if (lnw->iotg.stop_host)
2342 lnw->iotg.stop_host(&lnw->iotg);
2344 dev_dbg(&pdev->dev, "host driver has been removed.\n");
2345 iotg->hsm.b_bus_req = 0;
2346 iotg->otg.state = OTG_STATE_B_IDLE;
2347 transceiver_suspend(pdev);
2350 dev_dbg(lnw->dev, "error state before suspend\n");
2357 static void transceiver_resume(struct pci_dev *pdev)
2359 pci_restore_state(pdev);
2360 pci_set_power_state(pdev, PCI_D0);
2363 static int langwell_otg_resume(struct pci_dev *pdev)
2365 struct langwell_otg *lnw = the_transceiver;
2368 transceiver_resume(pdev);
2370 lnw->qwork = create_singlethread_workqueue("langwell_otg_queue");
2372 dev_dbg(&pdev->dev, "cannot create langwell otg workqueuen");
2377 if (request_irq(pdev->irq, otg_irq, IRQF_SHARED,
2378 driver_name, lnw) != 0) {
2379 dev_dbg(&pdev->dev, "request interrupt %d failed\n", pdev->irq);
2384 /* enable OTG interrupts */
2385 langwell_otg_intr(1);
2389 langwell_update_transceiver();
2393 langwell_otg_intr(0);
2394 transceiver_suspend(pdev);
2398 static int __init langwell_otg_init(void)
2400 return pci_register_driver(&otg_pci_driver);
2402 module_init(langwell_otg_init);
2404 static void __exit langwell_otg_cleanup(void)
2406 pci_unregister_driver(&otg_pci_driver);
2408 module_exit(langwell_otg_cleanup);