2 * Copyright (C) 2010 Google, Inc.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that 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
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <linux/usb.h>
20 #include <linux/usb/otg.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/hcd.h>
23 #include <linux/platform_device.h>
24 #include <linux/clk.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28 #include <mach/usb_phy.h>
29 #include <mach/legacy_irq.h>
31 #define TEGRA_USB_PHY_WAKEUP_REG_OFFSET 0x408
32 #define TEGRA_VBUS_WAKEUP_SW_VALUE (1 << 12)
33 #define TEGRA_VBUS_WAKEUP_SW_ENABLE (1 << 11)
34 #define TEGRA_ID_SW_VALUE (1 << 4)
35 #define TEGRA_ID_SW_ENABLE (1 << 3)
37 struct cpcap_otg_data {
38 struct otg_transceiver otg;
39 struct notifier_block nb;
45 static const char *cpcap_state_name(enum usb_otg_state state)
47 if (state == OTG_STATE_A_HOST)
49 if (state == OTG_STATE_B_PERIPHERAL)
51 if (state == OTG_STATE_A_SUSPEND)
56 static irqreturn_t cpcap_otg_irq(int irq, void *data)
58 if (tegra_legacy_force_irq_status(irq))
59 tegra_legacy_force_irq_clr(irq);
63 static int cpcap_otg_notify(struct notifier_block *nb, unsigned long event,
66 struct cpcap_otg_data *cpcap;
67 struct otg_transceiver *otg;
68 enum usb_otg_state from;
69 enum usb_otg_state to;
73 cpcap = container_of(nb, struct cpcap_otg_data, nb);
77 if (event == USB_EVENT_VBUS)
78 to = OTG_STATE_B_PERIPHERAL;
79 else if (event == USB_EVENT_ID)
80 to = OTG_STATE_A_HOST;
82 to = OTG_STATE_A_SUSPEND;
88 dev_info(cpcap->otg.dev, "%s --> %s", cpcap_state_name(from),
89 cpcap_state_name(to));
91 if ((to == OTG_STATE_A_HOST) && (from == OTG_STATE_A_SUSPEND)
93 hcd = (struct usb_hcd *)otg->host;
95 clk_enable(cpcap->clk);
97 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
98 val &= ~TEGRA_ID_SW_VALUE;
99 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
101 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
103 } else if ((to == OTG_STATE_A_SUSPEND) && (from == OTG_STATE_A_HOST)
105 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
106 val |= TEGRA_ID_SW_VALUE;
107 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
109 clk_disable(cpcap->clk);
111 } else if ((to == OTG_STATE_B_PERIPHERAL)
112 && (from == OTG_STATE_A_SUSPEND)
114 clk_enable(cpcap->clk);
116 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
117 val |= TEGRA_VBUS_WAKEUP_SW_VALUE;
118 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
120 } else if ((to == OTG_STATE_A_SUSPEND)
121 && (from == OTG_STATE_B_PERIPHERAL)
123 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
124 val &= ~TEGRA_VBUS_WAKEUP_SW_VALUE;
125 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
127 clk_disable(cpcap->clk);
131 tegra_legacy_force_irq_set(cpcap->irq);
135 static int cpcap_otg_set_peripheral(struct otg_transceiver *otg,
136 struct usb_gadget *gadget)
138 otg->gadget = gadget;
142 static int cpcap_otg_set_host(struct otg_transceiver *otg,
143 struct usb_bus *host)
149 static int cpcap_otg_set_power(struct otg_transceiver *otg, unsigned mA)
154 static int cpcap_otg_set_suspend(struct otg_transceiver *otg, int suspend)
159 static int cpcap_otg_probe(struct platform_device *pdev)
161 struct cpcap_otg_data *cpcap;
162 struct resource *res;
166 cpcap = kzalloc(sizeof(struct cpcap_otg_data), GFP_KERNEL);
170 cpcap->otg.dev = &pdev->dev;
171 cpcap->otg.label = "cpcap-otg";
172 cpcap->otg.state = OTG_STATE_UNDEFINED;
173 cpcap->otg.set_host = cpcap_otg_set_host;
174 cpcap->otg.set_peripheral = cpcap_otg_set_peripheral;
175 cpcap->otg.set_suspend = cpcap_otg_set_suspend;
176 cpcap->otg.set_power = cpcap_otg_set_power;
178 platform_set_drvdata(pdev, cpcap);
180 cpcap->clk = clk_get(&pdev->dev, NULL);
181 if (IS_ERR(cpcap->clk)) {
182 dev_err(&pdev->dev, "Can't get otg clock\n");
183 err = PTR_ERR(cpcap->clk);
187 err = clk_enable(cpcap->clk);
191 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 dev_err(&pdev->dev, "Failed to get I/O memory\n");
197 cpcap->regs = ioremap(res->start, resource_size(res));
203 cpcap->irq = platform_get_irq(pdev, 0);
204 if (cpcap->irq < 0) {
205 dev_err(&pdev->dev, "Failed to get IRQ\n");
210 err = request_irq(cpcap->irq, cpcap_otg_irq, IRQF_SHARED,
213 dev_err(&pdev->dev, "cannot request irq %d err %d\n",
218 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
219 val |= TEGRA_VBUS_WAKEUP_SW_ENABLE | TEGRA_ID_SW_ENABLE;
220 val |= TEGRA_ID_SW_VALUE;
221 val &= ~(TEGRA_VBUS_WAKEUP_SW_VALUE);
222 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
224 clk_disable(cpcap->clk);
225 cpcap->otg.state = OTG_STATE_A_SUSPEND;
227 BLOCKING_INIT_NOTIFIER_HEAD(&cpcap->otg.notifier);
228 cpcap->nb.notifier_call = cpcap_otg_notify;
229 otg_register_notifier(&cpcap->otg, &cpcap->nb);
231 err = otg_set_transceiver(&cpcap->otg);
233 dev_err(&pdev->dev, "can't register transceiver (%d)\n", err);
240 free_irq(cpcap->irq, cpcap);
242 iounmap(cpcap->regs);
244 clk_disable(cpcap->clk);
248 platform_set_drvdata(pdev, NULL);
253 static int __exit cpcap_otg_remove(struct platform_device *pdev)
255 struct cpcap_otg_data *cpcap = platform_get_drvdata(pdev);
257 otg_set_transceiver(NULL);
258 free_irq(cpcap->irq, cpcap);
259 iounmap(cpcap->regs);
260 clk_disable(cpcap->clk);
262 platform_set_drvdata(pdev, NULL);
268 static struct platform_driver cpcap_otg_driver = {
272 .remove = __exit_p(cpcap_otg_remove),
273 .probe = cpcap_otg_probe,
276 static int __init cpcap_otg_init(void)
278 return platform_driver_register(&cpcap_otg_driver);
280 module_init(cpcap_otg_init);
282 static void __exit cpcap_otg_exit(void)
284 platform_driver_unregister(&cpcap_otg_driver);
286 module_exit(cpcap_otg_exit);