temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / usb / otg / cpcap-otg.c
1 /*
2  * Copyright (C) 2010 Google, Inc.
3  *
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.
8  *
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
12  * more details.
13  *
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.
17  */
18
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>
26 #include <linux/io.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <linux/wakelock.h>
30
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)
36
37 struct cpcap_otg_data {
38         struct otg_transceiver otg;
39         struct notifier_block nb;
40         void __iomem *regs;
41         struct clk *clk;
42         struct platform_device *host;
43         struct platform_device *pdev;
44         struct wake_lock wake_lock;
45 };
46
47 static const char *cpcap_state_name(enum usb_otg_state state)
48 {
49         if (state == OTG_STATE_A_HOST)
50                 return "HOST";
51         if (state == OTG_STATE_B_PERIPHERAL)
52                 return "PERIPHERAL";
53         if (state == OTG_STATE_A_SUSPEND)
54                 return "SUSPEND";
55         return "INVALID";
56 }
57
58 void cpcap_start_host(struct cpcap_otg_data *cpcap)
59 {
60         int retval;
61         struct platform_device *pdev;
62         struct platform_device *host = cpcap->host;
63         void *platform_data;
64
65         pdev = platform_device_alloc(host->name, host->id);
66         if (!pdev)
67                 return;
68
69         if (host->resource) {
70                 retval = platform_device_add_resources(pdev, host->resource,
71                                                         host->num_resources);
72                 if (retval)
73                         goto error;
74         }
75
76         pdev->dev.dma_mask = host->dev.dma_mask;
77         pdev->dev.coherent_dma_mask = host->dev.coherent_dma_mask;
78
79         platform_data = kmalloc(sizeof(struct tegra_ehci_platform_data), GFP_KERNEL);
80         if (!platform_data)
81                 goto error;
82
83         memcpy(platform_data, host->dev.platform_data,
84                                 sizeof(struct tegra_ehci_platform_data));
85         pdev->dev.platform_data = platform_data;
86
87         retval = platform_device_add(pdev);
88         if (retval)
89                 goto error_add;
90
91         cpcap->pdev = pdev;
92         return;
93
94 error_add:
95         kfree(platform_data);
96 error:
97         pr_err("%s: failed to add the host contoller device\n", __func__);
98         platform_device_put(pdev);
99 }
100
101 void cpcap_stop_host(struct cpcap_otg_data *cpcap)
102 {
103         if (cpcap->pdev) {
104                 platform_device_unregister(cpcap->pdev);
105                 cpcap->pdev = NULL;
106         }
107 }
108
109 static int cpcap_otg_notify(struct notifier_block *nb, unsigned long event,
110                             void *ignore)
111 {
112         struct cpcap_otg_data *cpcap;
113         struct otg_transceiver *otg;
114         enum usb_otg_state from;
115         enum usb_otg_state to;
116         unsigned long val;
117         struct usb_hcd *hcd;
118
119         cpcap = container_of(nb, struct cpcap_otg_data, nb);
120         otg = &cpcap->otg;
121
122         from = otg->state;
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;
127         else
128                 to = OTG_STATE_A_SUSPEND;
129
130         if (from == to)
131                 return 0;
132         otg->state = to;
133
134         if (to == OTG_STATE_B_PERIPHERAL)
135                 wake_lock(&cpcap->wake_lock);
136
137         dev_info(cpcap->otg.dev, "%s --> %s", cpcap_state_name(from),
138                                               cpcap_state_name(to));
139
140         clk_enable(cpcap->clk);
141
142         if ((to == OTG_STATE_A_HOST) && (from == OTG_STATE_A_SUSPEND)
143                         && cpcap->host) {
144                 hcd = (struct usb_hcd *)otg->host;
145
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);
149
150                 cpcap_start_host(cpcap);
151
152         } else if ((to == OTG_STATE_A_SUSPEND) && (from == OTG_STATE_A_HOST)
153                         && cpcap->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);
157
158                 cpcap_stop_host(cpcap);
159
160         } else if ((to == OTG_STATE_B_PERIPHERAL)
161                         && (from == OTG_STATE_A_SUSPEND)
162                         && otg->gadget) {
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);
166
167                 usb_gadget_vbus_connect(otg->gadget);
168
169         } else if ((to == OTG_STATE_A_SUSPEND)
170                         && (from == OTG_STATE_B_PERIPHERAL)
171                         && otg->gadget) {
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);
175
176                 usb_gadget_vbus_disconnect(otg->gadget);
177         }
178
179         clk_disable(cpcap->clk);
180
181         if (to == OTG_STATE_A_SUSPEND)
182                 wake_unlock(&cpcap->wake_lock);
183
184         return 0;
185 }
186
187 static int cpcap_otg_set_peripheral(struct otg_transceiver *otg,
188                                 struct usb_gadget *gadget)
189 {
190         otg->gadget = gadget;
191         return 0;
192 }
193
194 static int cpcap_otg_set_host(struct otg_transceiver *otg,
195                                 struct usb_bus *host)
196 {
197         otg->host = host;
198         return 0;
199 }
200
201 static int cpcap_otg_set_power(struct otg_transceiver *otg, unsigned mA)
202 {
203         return 0;
204 }
205
206 static int cpcap_otg_set_suspend(struct otg_transceiver *otg, int suspend)
207 {
208         return 0;
209 }
210
211 static int cpcap_otg_probe(struct platform_device *pdev)
212 {
213         struct cpcap_otg_data *cpcap;
214         struct resource *res;
215         unsigned long val;
216         int err;
217
218         cpcap = kzalloc(sizeof(struct cpcap_otg_data), GFP_KERNEL);
219         if (!cpcap)
220                 return -ENOMEM;
221
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");
231
232         platform_set_drvdata(pdev, cpcap);
233
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);
238                 goto err_clk;
239         }
240
241         err = clk_enable(cpcap->clk);
242         if (err)
243                 goto err_clken;
244
245         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
246         if (!res) {
247                 dev_err(&pdev->dev, "Failed to get I/O memory\n");
248                 err = -ENXIO;
249                 goto err_io;
250         }
251         cpcap->regs = ioremap(res->start, resource_size(res));
252         if (!cpcap->regs) {
253                 err = -ENOMEM;
254                 goto err_io;
255         }
256
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);
262
263         clk_disable(cpcap->clk);
264         cpcap->otg.state = OTG_STATE_A_SUSPEND;
265
266         BLOCKING_INIT_NOTIFIER_HEAD(&cpcap->otg.notifier);
267         cpcap->nb.notifier_call = cpcap_otg_notify;
268         otg_register_notifier(&cpcap->otg, &cpcap->nb);
269
270         err = otg_set_transceiver(&cpcap->otg);
271         if (err) {
272                 dev_err(&pdev->dev, "can't register transceiver (%d)\n", err);
273                 goto err_otg;
274         }
275
276         return 0;
277
278 err_otg:
279         iounmap(cpcap->regs);
280 err_io:
281         clk_disable(cpcap->clk);
282 err_clken:
283         clk_put(cpcap->clk);
284 err_clk:
285         wake_lock_destroy(&cpcap->wake_lock);
286         platform_set_drvdata(pdev, NULL);
287         kfree(cpcap);
288         return err;
289 }
290
291 static int __exit cpcap_otg_remove(struct platform_device *pdev)
292 {
293         struct cpcap_otg_data *cpcap = platform_get_drvdata(pdev);
294
295         otg_set_transceiver(NULL);
296         iounmap(cpcap->regs);
297         clk_disable(cpcap->clk);
298         clk_put(cpcap->clk);
299         wake_lock_destroy(&cpcap->wake_lock);
300         platform_set_drvdata(pdev, NULL);
301         kfree(cpcap);
302
303         return 0;
304 }
305
306 static struct platform_driver cpcap_otg_driver = {
307         .driver = {
308                 .name  = "cpcap-otg",
309         },
310         .remove  = __exit_p(cpcap_otg_remove),
311         .probe   = cpcap_otg_probe,
312 };
313
314 static int __init cpcap_otg_init(void)
315 {
316         return platform_driver_register(&cpcap_otg_driver);
317 }
318 module_init(cpcap_otg_init);
319
320 static void __exit cpcap_otg_exit(void)
321 {
322         platform_driver_unregister(&cpcap_otg_driver);
323 }
324 module_exit(cpcap_otg_exit);