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/platform_data/tegra_usb.h>
25 #include <linux/clk.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <linux/wakelock.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;
42 struct platform_device *host;
43 struct platform_device *pdev;
44 struct wake_lock wake_lock;
47 static const char *cpcap_state_name(enum usb_otg_state state)
49 if (state == OTG_STATE_A_HOST)
51 if (state == OTG_STATE_B_PERIPHERAL)
53 if (state == OTG_STATE_A_SUSPEND)
58 void cpcap_start_host(struct cpcap_otg_data *cpcap)
61 struct platform_device *pdev;
62 struct platform_device *host = cpcap->host;
65 pdev = platform_device_alloc(host->name, host->id);
70 retval = platform_device_add_resources(pdev, host->resource,
76 pdev->dev.dma_mask = host->dev.dma_mask;
77 pdev->dev.coherent_dma_mask = host->dev.coherent_dma_mask;
79 platform_data = kmalloc(sizeof(struct tegra_ehci_platform_data), GFP_KERNEL);
83 memcpy(platform_data, host->dev.platform_data,
84 sizeof(struct tegra_ehci_platform_data));
85 pdev->dev.platform_data = platform_data;
87 retval = platform_device_add(pdev);
97 pr_err("%s: failed to add the host contoller device\n", __func__);
98 platform_device_put(pdev);
101 void cpcap_stop_host(struct cpcap_otg_data *cpcap)
104 platform_device_unregister(cpcap->pdev);
109 static int cpcap_otg_notify(struct notifier_block *nb, unsigned long event,
112 struct cpcap_otg_data *cpcap;
113 struct otg_transceiver *otg;
114 enum usb_otg_state from;
115 enum usb_otg_state to;
119 cpcap = container_of(nb, struct cpcap_otg_data, nb);
123 if (event == USB_EVENT_VBUS)
124 to = OTG_STATE_B_PERIPHERAL;
125 else if (event == USB_EVENT_ID)
126 to = OTG_STATE_A_HOST;
128 to = OTG_STATE_A_SUSPEND;
134 if (to == OTG_STATE_B_PERIPHERAL)
135 wake_lock(&cpcap->wake_lock);
137 dev_info(cpcap->otg.dev, "%s --> %s", cpcap_state_name(from),
138 cpcap_state_name(to));
140 clk_enable(cpcap->clk);
142 if ((to == OTG_STATE_A_HOST) && (from == OTG_STATE_A_SUSPEND)
144 hcd = (struct usb_hcd *)otg->host;
146 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
147 val &= ~TEGRA_ID_SW_VALUE;
148 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
150 cpcap_start_host(cpcap);
152 } else if ((to == OTG_STATE_A_SUSPEND) && (from == OTG_STATE_A_HOST)
154 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
155 val |= TEGRA_ID_SW_VALUE;
156 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
158 cpcap_stop_host(cpcap);
160 } else if ((to == OTG_STATE_B_PERIPHERAL)
161 && (from == OTG_STATE_A_SUSPEND)
163 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
164 val |= TEGRA_VBUS_WAKEUP_SW_VALUE;
165 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
167 usb_gadget_vbus_connect(otg->gadget);
169 } else if ((to == OTG_STATE_A_SUSPEND)
170 && (from == OTG_STATE_B_PERIPHERAL)
172 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
173 val &= ~TEGRA_VBUS_WAKEUP_SW_VALUE;
174 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
176 usb_gadget_vbus_disconnect(otg->gadget);
179 clk_disable(cpcap->clk);
181 if (to == OTG_STATE_A_SUSPEND)
182 wake_unlock(&cpcap->wake_lock);
187 static int cpcap_otg_set_peripheral(struct otg_transceiver *otg,
188 struct usb_gadget *gadget)
190 otg->gadget = gadget;
194 static int cpcap_otg_set_host(struct otg_transceiver *otg,
195 struct usb_bus *host)
201 static int cpcap_otg_set_power(struct otg_transceiver *otg, unsigned mA)
206 static int cpcap_otg_set_suspend(struct otg_transceiver *otg, int suspend)
211 static int cpcap_otg_probe(struct platform_device *pdev)
213 struct cpcap_otg_data *cpcap;
214 struct resource *res;
218 cpcap = kzalloc(sizeof(struct cpcap_otg_data), GFP_KERNEL);
222 cpcap->otg.dev = &pdev->dev;
223 cpcap->otg.label = "cpcap-otg";
224 cpcap->otg.state = OTG_STATE_UNDEFINED;
225 cpcap->otg.set_host = cpcap_otg_set_host;
226 cpcap->otg.set_peripheral = cpcap_otg_set_peripheral;
227 cpcap->otg.set_suspend = cpcap_otg_set_suspend;
228 cpcap->otg.set_power = cpcap_otg_set_power;
229 cpcap->host = pdev->dev.platform_data;
230 wake_lock_init(&cpcap->wake_lock, WAKE_LOCK_SUSPEND, "cpcap_otg");
232 platform_set_drvdata(pdev, cpcap);
234 cpcap->clk = clk_get(&pdev->dev, NULL);
235 if (IS_ERR(cpcap->clk)) {
236 dev_err(&pdev->dev, "Can't get otg clock\n");
237 err = PTR_ERR(cpcap->clk);
241 err = clk_enable(cpcap->clk);
245 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
247 dev_err(&pdev->dev, "Failed to get I/O memory\n");
251 cpcap->regs = ioremap(res->start, resource_size(res));
257 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
258 val |= TEGRA_VBUS_WAKEUP_SW_ENABLE | TEGRA_ID_SW_ENABLE;
259 val |= TEGRA_ID_SW_VALUE;
260 val &= ~(TEGRA_VBUS_WAKEUP_SW_VALUE);
261 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
263 clk_disable(cpcap->clk);
264 cpcap->otg.state = OTG_STATE_A_SUSPEND;
266 BLOCKING_INIT_NOTIFIER_HEAD(&cpcap->otg.notifier);
267 cpcap->nb.notifier_call = cpcap_otg_notify;
268 otg_register_notifier(&cpcap->otg, &cpcap->nb);
270 err = otg_set_transceiver(&cpcap->otg);
272 dev_err(&pdev->dev, "can't register transceiver (%d)\n", err);
279 iounmap(cpcap->regs);
281 clk_disable(cpcap->clk);
285 wake_lock_destroy(&cpcap->wake_lock);
286 platform_set_drvdata(pdev, NULL);
291 static int __exit cpcap_otg_remove(struct platform_device *pdev)
293 struct cpcap_otg_data *cpcap = platform_get_drvdata(pdev);
295 otg_set_transceiver(NULL);
296 iounmap(cpcap->regs);
297 clk_disable(cpcap->clk);
299 wake_lock_destroy(&cpcap->wake_lock);
300 platform_set_drvdata(pdev, NULL);
306 static struct platform_driver cpcap_otg_driver = {
310 .remove = __exit_p(cpcap_otg_remove),
311 .probe = cpcap_otg_probe,
314 static int __init cpcap_otg_init(void)
316 return platform_driver_register(&cpcap_otg_driver);
318 module_init(cpcap_otg_init);
320 static void __exit cpcap_otg_exit(void)
322 platform_driver_unregister(&cpcap_otg_driver);
324 module_exit(cpcap_otg_exit);