1 /* Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/platform_device.h>
22 #include <linux/clk.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/err.h>
26 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/uaccess.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32 #include <linux/pm_runtime.h>
34 #include <linux/usb.h>
35 #include <linux/usb/otg.h>
36 #include <linux/usb/ulpi.h>
37 #include <linux/usb/gadget.h>
38 #include <linux/usb/hcd.h>
39 #include <linux/usb/msm_hsusb.h>
40 #include <linux/usb/msm_hsusb_hw.h>
44 #define MSM_USB_BASE (motg->regs)
45 #define DRIVER_NAME "msm_otg"
47 #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
48 static int ulpi_read(struct otg_transceiver *otg, u32 reg)
50 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
53 /* initiate read operation */
54 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
57 /* wait for completion */
58 while (cnt < ULPI_IO_TIMEOUT_USEC) {
59 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
65 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
66 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
67 readl(USB_ULPI_VIEWPORT));
70 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
73 static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
75 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
78 /* initiate write operation */
79 writel(ULPI_RUN | ULPI_WRITE |
80 ULPI_ADDR(reg) | ULPI_DATA(val),
83 /* wait for completion */
84 while (cnt < ULPI_IO_TIMEOUT_USEC) {
85 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
91 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
92 dev_err(otg->dev, "ulpi_write: timeout\n");
98 static struct otg_io_access_ops msm_otg_io_ops = {
103 static void ulpi_init(struct msm_otg *motg)
105 struct msm_otg_platform_data *pdata = motg->pdata;
106 int *seq = pdata->phy_init_seq;
111 while (seq[0] >= 0) {
112 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
114 ulpi_write(&motg->otg, seq[0], seq[1]);
119 static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
124 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
126 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
128 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
130 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
135 static int msm_otg_phy_clk_reset(struct msm_otg *motg)
139 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
141 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
144 usleep_range(10000, 12000);
145 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
147 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
151 static int msm_otg_phy_reset(struct msm_otg *motg)
157 ret = msm_otg_link_clk_reset(motg, 1);
160 ret = msm_otg_phy_clk_reset(motg);
163 ret = msm_otg_link_clk_reset(motg, 0);
167 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
168 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
170 for (retries = 3; retries > 0; retries--) {
171 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
172 ULPI_CLR(ULPI_FUNC_CTRL));
175 ret = msm_otg_phy_clk_reset(motg);
182 /* This reset calibrates the phy, if the above write succeeded */
183 ret = msm_otg_phy_clk_reset(motg);
187 for (retries = 3; retries > 0; retries--) {
188 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
189 if (ret != -ETIMEDOUT)
191 ret = msm_otg_phy_clk_reset(motg);
198 dev_info(motg->otg.dev, "phy_reset: success\n");
202 #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
203 static int msm_otg_reset(struct otg_transceiver *otg)
205 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
206 struct msm_otg_platform_data *pdata = motg->pdata;
212 ret = msm_otg_phy_reset(motg);
214 dev_err(otg->dev, "phy_reset failed\n");
220 writel(USBCMD_RESET, USB_USBCMD);
221 while (cnt < LINK_RESET_TIMEOUT_USEC) {
222 if (!(readl(USB_USBCMD) & USBCMD_RESET))
227 if (cnt >= LINK_RESET_TIMEOUT_USEC)
230 /* select ULPI phy */
231 writel(0x80000000, USB_PORTSC);
235 writel(0x0, USB_AHBBURST);
236 writel(0x00, USB_AHBMODE);
238 if (pdata->otg_control == OTG_PHY_CONTROL) {
239 val = readl(USB_OTGSC);
240 if (pdata->mode == USB_OTG) {
241 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
242 val |= OTGSC_IDIE | OTGSC_BSVIE;
243 } else if (pdata->mode == USB_PERIPHERAL) {
244 ulpi_val = ULPI_INT_SESS_VALID;
247 writel(val, USB_OTGSC);
248 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
249 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
255 #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
256 static int msm_otg_suspend(struct msm_otg *motg)
258 struct otg_transceiver *otg = &motg->otg;
259 struct usb_bus *bus = otg->host;
260 struct msm_otg_platform_data *pdata = motg->pdata;
263 if (atomic_read(&motg->in_lpm))
266 disable_irq(motg->irq);
268 * Interrupt Latch Register auto-clear feature is not present
269 * in all PHY versions. Latch register is clear on read type.
270 * Clear latch register to avoid spurious wakeup from
271 * low power mode (LPM).
273 ulpi_read(otg, 0x14);
276 * PHY comparators are disabled when PHY enters into low power
277 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
278 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
279 * PHY comparators. This save significant amount of power.
281 if (pdata->otg_control == OTG_PHY_CONTROL)
282 ulpi_write(otg, 0x01, 0x30);
285 * PLL is not turned off when PHY enters into low power mode (LPM).
286 * Disable PLL for maximum power savings.
288 ulpi_write(otg, 0x08, 0x09);
291 * PHY may take some time or even fail to enter into low power
292 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
295 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
296 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
297 if (readl(USB_PORTSC) & PORTSC_PHCD)
303 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
304 dev_err(otg->dev, "Unable to suspend PHY\n");
306 enable_irq(motg->irq);
311 * PHY has capability to generate interrupt asynchronously in low
312 * power mode (LPM). This interrupt is level triggered. So USB IRQ
313 * line must be disabled till async interrupt enable bit is cleared
314 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
315 * block data communication from PHY.
317 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
319 clk_disable(motg->pclk);
320 clk_disable(motg->clk);
322 clk_disable(motg->core_clk);
324 if (device_may_wakeup(otg->dev))
325 enable_irq_wake(motg->irq);
327 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
329 atomic_set(&motg->in_lpm, 1);
330 enable_irq(motg->irq);
332 dev_info(otg->dev, "USB in low power mode\n");
337 #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
338 static int msm_otg_resume(struct msm_otg *motg)
340 struct otg_transceiver *otg = &motg->otg;
341 struct usb_bus *bus = otg->host;
345 if (!atomic_read(&motg->in_lpm))
348 clk_enable(motg->pclk);
349 clk_enable(motg->clk);
351 clk_enable(motg->core_clk);
353 temp = readl(USB_USBCMD);
354 temp &= ~ASYNC_INTR_CTRL;
355 temp &= ~ULPI_STP_CTRL;
356 writel(temp, USB_USBCMD);
359 * PHY comes out of low power mode (LPM) in case of wakeup
360 * from asynchronous interrupt.
362 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
363 goto skip_phy_resume;
365 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
366 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
367 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
373 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
375 * This is a fatal error. Reset the link and
376 * PHY. USB state can not be restored. Re-insertion
377 * of USB cable is the only way to get USB working.
379 dev_err(otg->dev, "Unable to resume USB."
380 "Re-plugin the cable\n");
385 if (device_may_wakeup(otg->dev))
386 disable_irq_wake(motg->irq);
388 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
390 if (motg->async_int) {
392 pm_runtime_put(otg->dev);
393 enable_irq(motg->irq);
396 atomic_set(&motg->in_lpm, 0);
398 dev_info(otg->dev, "USB exited from low power mode\n");
403 static void msm_otg_start_host(struct otg_transceiver *otg, int on)
405 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
406 struct msm_otg_platform_data *pdata = motg->pdata;
412 hcd = bus_to_hcd(otg->host);
415 dev_dbg(otg->dev, "host on\n");
417 if (pdata->vbus_power)
418 pdata->vbus_power(1);
420 * Some boards have a switch cotrolled by gpio
421 * to enable/disable internal HUB. Enable internal
422 * HUB before kicking the host.
424 if (pdata->setup_gpio)
425 pdata->setup_gpio(OTG_STATE_A_HOST);
427 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
430 dev_dbg(otg->dev, "host off\n");
435 if (pdata->setup_gpio)
436 pdata->setup_gpio(OTG_STATE_UNDEFINED);
437 if (pdata->vbus_power)
438 pdata->vbus_power(0);
442 static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
444 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
448 * Fail host registration if this board can support
449 * only peripheral configuration.
451 if (motg->pdata->mode == USB_PERIPHERAL) {
452 dev_info(otg->dev, "Host mode is not supported\n");
457 if (otg->state == OTG_STATE_A_HOST) {
458 pm_runtime_get_sync(otg->dev);
459 msm_otg_start_host(otg, 0);
461 otg->state = OTG_STATE_UNDEFINED;
462 schedule_work(&motg->sm_work);
470 hcd = bus_to_hcd(host);
471 hcd->power_budget = motg->pdata->power_budget;
474 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
477 * Kick the state machine work, if peripheral is not supported
478 * or peripheral is already registered with us.
480 if (motg->pdata->mode == USB_HOST || otg->gadget) {
481 pm_runtime_get_sync(otg->dev);
482 schedule_work(&motg->sm_work);
488 static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
490 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
491 struct msm_otg_platform_data *pdata = motg->pdata;
497 dev_dbg(otg->dev, "gadget on\n");
499 * Some boards have a switch cotrolled by gpio
500 * to enable/disable internal HUB. Disable internal
501 * HUB before kicking the gadget.
503 if (pdata->setup_gpio)
504 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
505 usb_gadget_vbus_connect(otg->gadget);
507 dev_dbg(otg->dev, "gadget off\n");
508 usb_gadget_vbus_disconnect(otg->gadget);
509 if (pdata->setup_gpio)
510 pdata->setup_gpio(OTG_STATE_UNDEFINED);
515 static int msm_otg_set_peripheral(struct otg_transceiver *otg,
516 struct usb_gadget *gadget)
518 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
521 * Fail peripheral registration if this board can support
522 * only host configuration.
524 if (motg->pdata->mode == USB_HOST) {
525 dev_info(otg->dev, "Peripheral mode is not supported\n");
530 if (otg->state == OTG_STATE_B_PERIPHERAL) {
531 pm_runtime_get_sync(otg->dev);
532 msm_otg_start_peripheral(otg, 0);
534 otg->state = OTG_STATE_UNDEFINED;
535 schedule_work(&motg->sm_work);
542 otg->gadget = gadget;
543 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
546 * Kick the state machine work, if host is not supported
547 * or host is already registered with us.
549 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
550 pm_runtime_get_sync(otg->dev);
551 schedule_work(&motg->sm_work);
558 * We support OTG, Peripheral only and Host only configurations. In case
559 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
560 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
561 * enabled when switch is controlled by user and default mode is supplied
562 * by board file, which can be changed by userspace later.
564 static void msm_otg_init_sm(struct msm_otg *motg)
566 struct msm_otg_platform_data *pdata = motg->pdata;
567 u32 otgsc = readl(USB_OTGSC);
569 switch (pdata->mode) {
571 if (pdata->otg_control == OTG_PHY_CONTROL) {
572 if (otgsc & OTGSC_ID)
573 set_bit(ID, &motg->inputs);
575 clear_bit(ID, &motg->inputs);
577 if (otgsc & OTGSC_BSV)
578 set_bit(B_SESS_VLD, &motg->inputs);
580 clear_bit(B_SESS_VLD, &motg->inputs);
581 } else if (pdata->otg_control == OTG_USER_CONTROL) {
582 if (pdata->default_mode == USB_HOST) {
583 clear_bit(ID, &motg->inputs);
584 } else if (pdata->default_mode == USB_PERIPHERAL) {
585 set_bit(ID, &motg->inputs);
586 set_bit(B_SESS_VLD, &motg->inputs);
588 set_bit(ID, &motg->inputs);
589 clear_bit(B_SESS_VLD, &motg->inputs);
594 clear_bit(ID, &motg->inputs);
597 set_bit(ID, &motg->inputs);
598 if (otgsc & OTGSC_BSV)
599 set_bit(B_SESS_VLD, &motg->inputs);
601 clear_bit(B_SESS_VLD, &motg->inputs);
608 static void msm_otg_sm_work(struct work_struct *w)
610 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
611 struct otg_transceiver *otg = &motg->otg;
613 switch (otg->state) {
614 case OTG_STATE_UNDEFINED:
615 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
617 msm_otg_init_sm(motg);
618 otg->state = OTG_STATE_B_IDLE;
620 case OTG_STATE_B_IDLE:
621 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
622 if (!test_bit(ID, &motg->inputs) && otg->host) {
623 /* disable BSV bit */
624 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
625 msm_otg_start_host(otg, 1);
626 otg->state = OTG_STATE_A_HOST;
627 } else if (test_bit(B_SESS_VLD, &motg->inputs) && otg->gadget) {
628 msm_otg_start_peripheral(otg, 1);
629 otg->state = OTG_STATE_B_PERIPHERAL;
631 pm_runtime_put_sync(otg->dev);
633 case OTG_STATE_B_PERIPHERAL:
634 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
635 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
636 !test_bit(ID, &motg->inputs)) {
637 msm_otg_start_peripheral(otg, 0);
638 otg->state = OTG_STATE_B_IDLE;
643 case OTG_STATE_A_HOST:
644 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
645 if (test_bit(ID, &motg->inputs)) {
646 msm_otg_start_host(otg, 0);
647 otg->state = OTG_STATE_B_IDLE;
657 static irqreturn_t msm_otg_irq(int irq, void *data)
659 struct msm_otg *motg = data;
660 struct otg_transceiver *otg = &motg->otg;
663 if (atomic_read(&motg->in_lpm)) {
664 disable_irq_nosync(irq);
666 pm_runtime_get(otg->dev);
670 otgsc = readl(USB_OTGSC);
671 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
674 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
675 if (otgsc & OTGSC_ID)
676 set_bit(ID, &motg->inputs);
678 clear_bit(ID, &motg->inputs);
679 dev_dbg(otg->dev, "ID set/clear\n");
680 pm_runtime_get_noresume(otg->dev);
681 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
682 if (otgsc & OTGSC_BSV)
683 set_bit(B_SESS_VLD, &motg->inputs);
685 clear_bit(B_SESS_VLD, &motg->inputs);
686 dev_dbg(otg->dev, "BSV set/clear\n");
687 pm_runtime_get_noresume(otg->dev);
690 writel(otgsc, USB_OTGSC);
691 schedule_work(&motg->sm_work);
695 static int msm_otg_mode_show(struct seq_file *s, void *unused)
697 struct msm_otg *motg = s->private;
698 struct otg_transceiver *otg = &motg->otg;
700 switch (otg->state) {
701 case OTG_STATE_A_HOST:
702 seq_printf(s, "host\n");
704 case OTG_STATE_B_PERIPHERAL:
705 seq_printf(s, "peripheral\n");
708 seq_printf(s, "none\n");
715 static int msm_otg_mode_open(struct inode *inode, struct file *file)
717 return single_open(file, msm_otg_mode_show, inode->i_private);
720 static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
721 size_t count, loff_t *ppos)
723 struct msm_otg *motg = file->private_data;
725 struct otg_transceiver *otg = &motg->otg;
727 enum usb_mode_type req_mode;
729 memset(buf, 0x00, sizeof(buf));
731 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
736 if (!strncmp(buf, "host", 4)) {
738 } else if (!strncmp(buf, "peripheral", 10)) {
739 req_mode = USB_PERIPHERAL;
740 } else if (!strncmp(buf, "none", 4)) {
749 switch (otg->state) {
750 case OTG_STATE_A_HOST:
751 case OTG_STATE_B_PERIPHERAL:
752 set_bit(ID, &motg->inputs);
753 clear_bit(B_SESS_VLD, &motg->inputs);
760 switch (otg->state) {
761 case OTG_STATE_B_IDLE:
762 case OTG_STATE_A_HOST:
763 set_bit(ID, &motg->inputs);
764 set_bit(B_SESS_VLD, &motg->inputs);
771 switch (otg->state) {
772 case OTG_STATE_B_IDLE:
773 case OTG_STATE_B_PERIPHERAL:
774 clear_bit(ID, &motg->inputs);
784 pm_runtime_get_sync(otg->dev);
785 schedule_work(&motg->sm_work);
790 const struct file_operations msm_otg_mode_fops = {
791 .open = msm_otg_mode_open,
793 .write = msm_otg_mode_write,
795 .release = single_release,
798 static struct dentry *msm_otg_dbg_root;
799 static struct dentry *msm_otg_dbg_mode;
801 static int msm_otg_debugfs_init(struct msm_otg *motg)
803 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
805 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
808 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
809 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
810 if (!msm_otg_dbg_mode) {
811 debugfs_remove(msm_otg_dbg_root);
812 msm_otg_dbg_root = NULL;
819 static void msm_otg_debugfs_cleanup(void)
821 debugfs_remove(msm_otg_dbg_mode);
822 debugfs_remove(msm_otg_dbg_root);
825 static int __init msm_otg_probe(struct platform_device *pdev)
828 struct resource *res;
829 struct msm_otg *motg;
830 struct otg_transceiver *otg;
832 dev_info(&pdev->dev, "msm_otg probe\n");
833 if (!pdev->dev.platform_data) {
834 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
838 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
840 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
844 motg->pdata = pdev->dev.platform_data;
846 otg->dev = &pdev->dev;
848 motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
849 if (IS_ERR(motg->phy_reset_clk)) {
850 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
851 ret = PTR_ERR(motg->phy_reset_clk);
855 motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
856 if (IS_ERR(motg->clk)) {
857 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
858 ret = PTR_ERR(motg->clk);
859 goto put_phy_reset_clk;
862 motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
863 if (IS_ERR(motg->pclk)) {
864 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
865 ret = PTR_ERR(motg->pclk);
870 * USB core clock is not present on all MSM chips. This
871 * clock is introduced to remove the dependency on AXI
874 motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
875 if (IS_ERR(motg->core_clk))
876 motg->core_clk = NULL;
878 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
880 dev_err(&pdev->dev, "failed to get platform resource mem\n");
885 motg->regs = ioremap(res->start, resource_size(res));
887 dev_err(&pdev->dev, "ioremap failed\n");
891 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
893 motg->irq = platform_get_irq(pdev, 0);
895 dev_err(&pdev->dev, "platform_get_irq failed\n");
900 clk_enable(motg->clk);
901 clk_enable(motg->pclk);
903 clk_enable(motg->core_clk);
905 writel(0, USB_USBINTR);
906 writel(0, USB_OTGSC);
908 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
909 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
912 dev_err(&pdev->dev, "request irq failed\n");
916 otg->init = msm_otg_reset;
917 otg->set_host = msm_otg_set_host;
918 otg->set_peripheral = msm_otg_set_peripheral;
920 otg->io_ops = &msm_otg_io_ops;
922 ret = otg_set_transceiver(&motg->otg);
924 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
928 platform_set_drvdata(pdev, motg);
929 device_init_wakeup(&pdev->dev, 1);
931 if (motg->pdata->mode == USB_OTG &&
932 motg->pdata->otg_control == OTG_USER_CONTROL) {
933 ret = msm_otg_debugfs_init(motg);
935 dev_dbg(&pdev->dev, "mode debugfs file is"
939 pm_runtime_set_active(&pdev->dev);
940 pm_runtime_enable(&pdev->dev);
944 free_irq(motg->irq, motg);
946 clk_disable(motg->pclk);
947 clk_disable(motg->clk);
952 clk_put(motg->core_clk);
957 clk_put(motg->phy_reset_clk);
963 static int __devexit msm_otg_remove(struct platform_device *pdev)
965 struct msm_otg *motg = platform_get_drvdata(pdev);
966 struct otg_transceiver *otg = &motg->otg;
969 if (otg->host || otg->gadget)
972 msm_otg_debugfs_cleanup();
973 cancel_work_sync(&motg->sm_work);
975 msm_otg_resume(motg);
977 device_init_wakeup(&pdev->dev, 0);
978 pm_runtime_disable(&pdev->dev);
980 otg_set_transceiver(NULL);
981 free_irq(motg->irq, motg);
984 * Put PHY in low power mode.
986 ulpi_read(otg, 0x14);
987 ulpi_write(otg, 0x08, 0x09);
989 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
990 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
991 if (readl(USB_PORTSC) & PORTSC_PHCD)
996 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
997 dev_err(otg->dev, "Unable to suspend PHY\n");
999 clk_disable(motg->pclk);
1000 clk_disable(motg->clk);
1002 clk_disable(motg->core_clk);
1004 iounmap(motg->regs);
1005 pm_runtime_set_suspended(&pdev->dev);
1007 clk_put(motg->phy_reset_clk);
1008 clk_put(motg->pclk);
1011 clk_put(motg->core_clk);
1018 #ifdef CONFIG_PM_RUNTIME
1019 static int msm_otg_runtime_idle(struct device *dev)
1021 struct msm_otg *motg = dev_get_drvdata(dev);
1022 struct otg_transceiver *otg = &motg->otg;
1024 dev_dbg(dev, "OTG runtime idle\n");
1027 * It is observed some times that a spurious interrupt
1028 * comes when PHY is put into LPM immediately after PHY reset.
1029 * This 1 sec delay also prevents entering into LPM immediately
1030 * after asynchronous interrupt.
1032 if (otg->state != OTG_STATE_UNDEFINED)
1033 pm_schedule_suspend(dev, 1000);
1038 static int msm_otg_runtime_suspend(struct device *dev)
1040 struct msm_otg *motg = dev_get_drvdata(dev);
1042 dev_dbg(dev, "OTG runtime suspend\n");
1043 return msm_otg_suspend(motg);
1046 static int msm_otg_runtime_resume(struct device *dev)
1048 struct msm_otg *motg = dev_get_drvdata(dev);
1050 dev_dbg(dev, "OTG runtime resume\n");
1051 return msm_otg_resume(motg);
1054 #define msm_otg_runtime_idle NULL
1055 #define msm_otg_runtime_suspend NULL
1056 #define msm_otg_runtime_resume NULL
1060 static int msm_otg_pm_suspend(struct device *dev)
1062 struct msm_otg *motg = dev_get_drvdata(dev);
1064 dev_dbg(dev, "OTG PM suspend\n");
1065 return msm_otg_suspend(motg);
1068 static int msm_otg_pm_resume(struct device *dev)
1070 struct msm_otg *motg = dev_get_drvdata(dev);
1073 dev_dbg(dev, "OTG PM resume\n");
1075 ret = msm_otg_resume(motg);
1080 * Runtime PM Documentation recommends bringing the
1081 * device to full powered state upon resume.
1083 pm_runtime_disable(dev);
1084 pm_runtime_set_active(dev);
1085 pm_runtime_enable(dev);
1090 #define msm_otg_pm_suspend NULL
1091 #define msm_otg_pm_resume NULL
1094 static const struct dev_pm_ops msm_otg_dev_pm_ops = {
1095 .runtime_suspend = msm_otg_runtime_suspend,
1096 .runtime_resume = msm_otg_runtime_resume,
1097 .runtime_idle = msm_otg_runtime_idle,
1098 .suspend = msm_otg_pm_suspend,
1099 .resume = msm_otg_pm_resume,
1102 static struct platform_driver msm_otg_driver = {
1103 .remove = __devexit_p(msm_otg_remove),
1105 .name = DRIVER_NAME,
1106 .owner = THIS_MODULE,
1107 .pm = &msm_otg_dev_pm_ops,
1111 static int __init msm_otg_init(void)
1113 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
1116 static void __exit msm_otg_exit(void)
1118 platform_driver_unregister(&msm_otg_driver);
1121 module_init(msm_otg_init);
1122 module_exit(msm_otg_exit);
1124 MODULE_LICENSE("GPL v2");
1125 MODULE_DESCRIPTION("MSM USB transceiver driver");