]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/host/ohci-at91.c
Merge remote-tracking branches 'asoc/topic/sta529', 'asoc/topic/sti', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / host / ohci-at91.c
CommitLineData
39a269c0
AV
1/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * Copyright (C) 2004 SAN People (Pty) Ltd.
5 * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
6 *
0365ee0a 7 * AT91 Bus Glue
39a269c0
AV
8 *
9 * Based on fragments of 2.4 driver by Rick Bronson.
10 * Based on ohci-omap.c
11 *
12 * This file is licenced under the GPL.
13 */
14
15#include <linux/clk.h>
e3825b48 16#include <linux/dma-mapping.h>
054d4b7b 17#include <linux/gpio/consumer.h>
2419730f 18#include <linux/of_platform.h>
e3825b48 19#include <linux/platform_device.h>
bcd2360c 20#include <linux/platform_data/atmel.h>
e3825b48
MG
21#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
2e2aa1bc
WY
24#include <linux/mfd/syscon.h>
25#include <linux/regmap.h>
e3825b48
MG
26#include <linux/usb.h>
27#include <linux/usb/hcd.h>
2e2aa1bc 28#include <soc/at91/atmel-sfr.h>
39a269c0 29
e3825b48 30#include "ohci.h"
39a269c0 31
0ee6d1ee
NF
32#define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
33#define at91_for_each_port(index) \
34 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
35
6b0a1cf7 36/* interface, function and usb clocks; sometimes also an AHB clock */
1b207459
SR
37#define hcd_to_ohci_at91_priv(h) \
38 ((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
39
b4f19470
AB
40#define AT91_MAX_USBH_PORTS 3
41struct at91_usbh_data {
054d4b7b
WY
42 struct gpio_desc *vbus_pin[AT91_MAX_USBH_PORTS];
43 struct gpio_desc *overcurrent_pin[AT91_MAX_USBH_PORTS];
b4f19470
AB
44 u8 ports; /* number of ports on root hub */
45 u8 overcurrent_supported;
b4f19470
AB
46 u8 overcurrent_status[AT91_MAX_USBH_PORTS];
47 u8 overcurrent_changed[AT91_MAX_USBH_PORTS];
48};
49
1b207459
SR
50struct ohci_at91_priv {
51 struct clk *iclk;
52 struct clk *fclk;
1b207459
SR
53 struct clk *hclk;
54 bool clocked;
1cee6b8d 55 bool wakeup; /* Saved wake-up state for resume */
2e2aa1bc 56 struct regmap *sfr_regmap;
1b207459 57};
e3825b48
MG
58/* interface and function clocks; sometimes also an AHB clock */
59
60#define DRIVER_DESC "OHCI Atmel driver"
61
62static const char hcd_name[] = "ohci-atmel";
63
64static struct hc_driver __read_mostly ohci_at91_hc_driver;
1b207459
SR
65
66static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
67 .extra_priv_size = sizeof(struct ohci_at91_priv),
68};
39a269c0 69
39a269c0
AV
70/*-------------------------------------------------------------------------*/
71
1b207459 72static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
ed077bb7 73{
1cee6b8d
SR
74 if (ohci_at91->clocked)
75 return;
963719c8
BB
76
77 clk_set_rate(ohci_at91->fclk, 48000000);
1b207459
SR
78 clk_prepare_enable(ohci_at91->hclk);
79 clk_prepare_enable(ohci_at91->iclk);
80 clk_prepare_enable(ohci_at91->fclk);
81 ohci_at91->clocked = true;
ed077bb7
AV
82}
83
1b207459 84static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
ed077bb7 85{
1cee6b8d
SR
86 if (!ohci_at91->clocked)
87 return;
963719c8 88
1b207459
SR
89 clk_disable_unprepare(ohci_at91->fclk);
90 clk_disable_unprepare(ohci_at91->iclk);
91 clk_disable_unprepare(ohci_at91->hclk);
1b207459 92 ohci_at91->clocked = false;
ed077bb7
AV
93}
94
39a269c0
AV
95static void at91_start_hc(struct platform_device *pdev)
96{
97 struct usb_hcd *hcd = platform_get_drvdata(pdev);
98 struct ohci_regs __iomem *regs = hcd->regs;
1b207459 99 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
39a269c0 100
0365ee0a 101 dev_dbg(&pdev->dev, "start\n");
39a269c0
AV
102
103 /*
104 * Start the USB clocks.
105 */
1b207459 106 at91_start_clock(ohci_at91);
39a269c0
AV
107
108 /*
109 * The USB host controller must remain in reset.
110 */
111 writel(0, &regs->control);
112}
113
114static void at91_stop_hc(struct platform_device *pdev)
115{
116 struct usb_hcd *hcd = platform_get_drvdata(pdev);
117 struct ohci_regs __iomem *regs = hcd->regs;
1b207459 118 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
39a269c0 119
0365ee0a 120 dev_dbg(&pdev->dev, "stop\n");
39a269c0
AV
121
122 /*
123 * Put the USB host controller into reset.
124 */
125 writel(0, &regs->control);
126
127 /*
128 * Stop the USB clocks.
129 */
1b207459 130 at91_stop_clock(ohci_at91);
39a269c0
AV
131}
132
133
134/*-------------------------------------------------------------------------*/
135
fb4e98ab 136static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
39a269c0 137
9cdd8e11 138static struct regmap *at91_dt_syscon_sfr(void)
2e2aa1bc
WY
139{
140 struct regmap *regmap;
141
142 regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
143 if (IS_ERR(regmap))
144 regmap = NULL;
145
146 return regmap;
147}
148
39a269c0
AV
149/* configure so an HC device and id are always provided */
150/* always called with process context; sleeping is OK */
151
152
153/**
0365ee0a 154 * usb_hcd_at91_probe - initialize AT91-based HCDs
39a269c0
AV
155 * Context: !in_interrupt()
156 *
157 * Allocates basic resources for this USB host controller, and
158 * then invokes the start() method for the HCD associated with it
159 * through the hotplug entry's driver_data.
39a269c0 160 */
41ac7b3a 161static int usb_hcd_at91_probe(const struct hc_driver *driver,
0365ee0a 162 struct platform_device *pdev)
39a269c0 163{
e3825b48
MG
164 struct at91_usbh_data *board;
165 struct ohci_hcd *ohci;
39a269c0 166 int retval;
3b3394af 167 struct usb_hcd *hcd;
1b207459 168 struct ohci_at91_priv *ohci_at91;
fb5f1834
BB
169 struct device *dev = &pdev->dev;
170 struct resource *res;
171 int irq;
172
fb5f1834
BB
173 irq = platform_get_irq(pdev, 0);
174 if (irq < 0) {
175 dev_dbg(dev, "hcd probe: missing irq resource\n");
176 return irq;
39a269c0
AV
177 }
178
5b218a07 179 hcd = usb_create_hcd(driver, dev, "at91");
39a269c0
AV
180 if (!hcd)
181 return -ENOMEM;
1b207459 182 ohci_at91 = hcd_to_ohci_at91_priv(hcd);
39a269c0 183
04dc3150 184 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
cca2bbb3
BB
185 hcd->regs = devm_ioremap_resource(dev, res);
186 if (IS_ERR(hcd->regs)) {
187 retval = PTR_ERR(hcd->regs);
188 goto err;
39a269c0 189 }
04dc3150
VB
190 hcd->rsrc_start = res->start;
191 hcd->rsrc_len = resource_size(res);
39a269c0 192
1b207459
SR
193 ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
194 if (IS_ERR(ohci_at91->iclk)) {
5b218a07 195 dev_err(dev, "failed to get ohci_clk\n");
1b207459 196 retval = PTR_ERR(ohci_at91->iclk);
cca2bbb3 197 goto err;
6813463c 198 }
1b207459
SR
199 ohci_at91->fclk = devm_clk_get(dev, "uhpck");
200 if (IS_ERR(ohci_at91->fclk)) {
5b218a07 201 dev_err(dev, "failed to get uhpck\n");
1b207459 202 retval = PTR_ERR(ohci_at91->fclk);
fc7a3252 203 goto err;
6813463c 204 }
1b207459
SR
205 ohci_at91->hclk = devm_clk_get(dev, "hclk");
206 if (IS_ERR(ohci_at91->hclk)) {
5b218a07 207 dev_err(dev, "failed to get hclk\n");
1b207459 208 retval = PTR_ERR(ohci_at91->hclk);
fc7a3252 209 goto err;
6813463c 210 }
39a269c0 211
2e2aa1bc
WY
212 ohci_at91->sfr_regmap = at91_dt_syscon_sfr();
213 if (!ohci_at91->sfr_regmap)
214 dev_warn(dev, "failed to find sfr node\n");
215
e3825b48
MG
216 board = hcd->self.controller->platform_data;
217 ohci = hcd_to_ohci(hcd);
218 ohci->num_ports = board->ports;
39a269c0 219 at91_start_hc(pdev);
39a269c0 220
ed19ece1
WY
221 /*
222 * The RemoteWakeupConnected bit has to be set explicitly
223 * before calling ohci_run. The reset value of this bit is 0.
224 */
225 ohci->hc_control = OHCI_CTRL_RWC;
226
fb5f1834 227 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
1f53b485 228 if (retval == 0) {
3c9740a1 229 device_wakeup_enable(hcd->self.controller);
39a269c0 230 return retval;
3c9740a1 231 }
39a269c0
AV
232
233 /* Error handling */
234 at91_stop_hc(pdev);
235
cca2bbb3 236 err:
39a269c0
AV
237 usb_put_hcd(hcd);
238 return retval;
239}
240
241
39a269c0
AV
242/* may be called with controller, bus, and devices active */
243
244/**
0365ee0a 245 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
39a269c0
AV
246 * @dev: USB Host Controller being removed
247 * Context: !in_interrupt()
248 *
249 * Reverses the effect of usb_hcd_at91_probe(), first invoking
250 * the HCD's stop() method. It is always called from a thread
0365ee0a 251 * context, "rmmod" or something similar.
39a269c0
AV
252 *
253 */
fb4e98ab 254static void usb_hcd_at91_remove(struct usb_hcd *hcd,
0365ee0a 255 struct platform_device *pdev)
39a269c0
AV
256{
257 usb_remove_hcd(hcd);
258 at91_stop_hc(pdev);
421b4bf5 259 usb_put_hcd(hcd);
39a269c0
AV
260}
261
262/*-------------------------------------------------------------------------*/
aa6e52a3
TP
263static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
264{
0ee6d1ee 265 if (!valid_port(port))
aa6e52a3
TP
266 return;
267
8f12dc24 268 gpiod_set_value(pdata->vbus_pin[port], enable);
aa6e52a3
TP
269}
270
271static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
272{
0ee6d1ee 273 if (!valid_port(port))
aa6e52a3
TP
274 return -EINVAL;
275
8f12dc24 276 return gpiod_get_value(pdata->vbus_pin[port]);
aa6e52a3
TP
277}
278
279/*
280 * Update the status data from the hub with the over-current indicator change.
281 */
282static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
283{
e3825b48 284 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
42b59eba 285 int length = ohci_hub_status_data(hcd, buf);
aa6e52a3
TP
286 int port;
287
0ee6d1ee 288 at91_for_each_port(port) {
aa6e52a3 289 if (pdata->overcurrent_changed[port]) {
0ee6d1ee 290 if (!length)
aa6e52a3
TP
291 length = 1;
292 buf[0] |= 1 << (port + 1);
293 }
294 }
295
296 return length;
297}
298
2e2aa1bc
WY
299static int ohci_at91_port_suspend(struct regmap *regmap, u8 set)
300{
301 u32 regval;
302 int ret;
303
304 if (!regmap)
305 return 0;
306
307 ret = regmap_read(regmap, AT91_SFR_OHCIICR, &regval);
308 if (ret)
309 return ret;
310
311 if (set)
312 regval |= AT91_OHCIICR_USB_SUSPEND;
313 else
314 regval &= ~AT91_OHCIICR_USB_SUSPEND;
315
316 regmap_write(regmap, AT91_SFR_OHCIICR, regval);
317
318 return 0;
319}
320
aa6e52a3
TP
321/*
322 * Look at the control requests to the root hub and see if we need to override.
323 */
324static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
325 u16 wIndex, char *buf, u16 wLength)
326{
d4f09e28 327 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
2e2aa1bc 328 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
aa6e52a3
TP
329 struct usb_hub_descriptor *desc;
330 int ret = -EINVAL;
331 u32 *data = (u32 *)buf;
332
333 dev_dbg(hcd->self.controller,
334 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
335 hcd, typeReq, wValue, wIndex, buf, wLength);
336
0ee6d1ee
NF
337 wIndex--;
338
aa6e52a3
TP
339 switch (typeReq) {
340 case SetPortFeature:
2e2aa1bc
WY
341 switch (wValue) {
342 case USB_PORT_FEAT_POWER:
aa6e52a3 343 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
0ee6d1ee
NF
344 if (valid_port(wIndex)) {
345 ohci_at91_usb_set_power(pdata, wIndex, 1);
346 ret = 0;
347 }
348
aa6e52a3 349 goto out;
2e2aa1bc
WY
350
351 case USB_PORT_FEAT_SUSPEND:
352 dev_dbg(hcd->self.controller, "SetPortFeat: SUSPEND\n");
85550f91 353 if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
2e2aa1bc
WY
354 ohci_at91_port_suspend(ohci_at91->sfr_regmap,
355 1);
356 return 0;
357 }
358 break;
aa6e52a3
TP
359 }
360 break;
361
362 case ClearPortFeature:
363 switch (wValue) {
364 case USB_PORT_FEAT_C_OVER_CURRENT:
365 dev_dbg(hcd->self.controller,
366 "ClearPortFeature: C_OVER_CURRENT\n");
367
0ee6d1ee
NF
368 if (valid_port(wIndex)) {
369 pdata->overcurrent_changed[wIndex] = 0;
370 pdata->overcurrent_status[wIndex] = 0;
aa6e52a3
TP
371 }
372
373 goto out;
374
375 case USB_PORT_FEAT_OVER_CURRENT:
376 dev_dbg(hcd->self.controller,
377 "ClearPortFeature: OVER_CURRENT\n");
378
0ee6d1ee
NF
379 if (valid_port(wIndex))
380 pdata->overcurrent_status[wIndex] = 0;
aa6e52a3
TP
381
382 goto out;
383
384 case USB_PORT_FEAT_POWER:
385 dev_dbg(hcd->self.controller,
386 "ClearPortFeature: POWER\n");
387
0ee6d1ee
NF
388 if (valid_port(wIndex)) {
389 ohci_at91_usb_set_power(pdata, wIndex, 0);
aa6e52a3
TP
390 return 0;
391 }
2e2aa1bc
WY
392 break;
393
394 case USB_PORT_FEAT_SUSPEND:
395 dev_dbg(hcd->self.controller, "ClearPortFeature: SUSPEND\n");
85550f91 396 if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
2e2aa1bc
WY
397 ohci_at91_port_suspend(ohci_at91->sfr_regmap,
398 0);
399 return 0;
400 }
401 break;
aa6e52a3
TP
402 }
403 break;
404 }
405
42b59eba 406 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
aa6e52a3
TP
407 if (ret)
408 goto out;
409
410 switch (typeReq) {
411 case GetHubDescriptor:
412
413 /* update the hub's descriptor */
414
415 desc = (struct usb_hub_descriptor *)buf;
416
417 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
418 desc->wHubCharacteristics);
419
420 /* remove the old configurations for power-switching, and
421 * over-current protection, and insert our new configuration
422 */
423
424 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
a9c49bcd
SS
425 desc->wHubCharacteristics |=
426 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
aa6e52a3
TP
427
428 if (pdata->overcurrent_supported) {
429 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
a9c49bcd
SS
430 desc->wHubCharacteristics |=
431 cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
aa6e52a3
TP
432 }
433
434 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
435 desc->wHubCharacteristics);
436
437 return ret;
438
439 case GetPortStatus:
440 /* check port status */
441
442 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
443
0ee6d1ee
NF
444 if (valid_port(wIndex)) {
445 if (!ohci_at91_usb_get_power(pdata, wIndex))
aa6e52a3 446 *data &= ~cpu_to_le32(RH_PS_PPS);
aa6e52a3 447
0ee6d1ee 448 if (pdata->overcurrent_changed[wIndex])
aa6e52a3 449 *data |= cpu_to_le32(RH_PS_OCIC);
aa6e52a3 450
0ee6d1ee 451 if (pdata->overcurrent_status[wIndex])
aa6e52a3 452 *data |= cpu_to_le32(RH_PS_POCI);
aa6e52a3
TP
453 }
454 }
455
456 out:
457 return ret;
458}
459
39a269c0
AV
460/*-------------------------------------------------------------------------*/
461
aa6e52a3
TP
462static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
463{
464 struct platform_device *pdev = data;
d4f09e28 465 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
054d4b7b 466 int val, port;
aa6e52a3
TP
467
468 /* From the GPIO notifying the over-current situation, find
469 * out the corresponding port */
0ee6d1ee 470 at91_for_each_port(port) {
054d4b7b 471 if (gpiod_to_irq(pdata->overcurrent_pin[port]) == irq)
aa6e52a3
TP
472 break;
473 }
474
0ee6d1ee 475 if (port == AT91_MAX_USBH_PORTS) {
aa6e52a3
TP
476 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
477 return IRQ_HANDLED;
478 }
479
054d4b7b 480 val = gpiod_get_value(pdata->overcurrent_pin[port]);
aa6e52a3
TP
481
482 /* When notified of an over-current situation, disable power
483 on the corresponding port, and mark this port in
484 over-current. */
0ee6d1ee 485 if (!val) {
aa6e52a3
TP
486 ohci_at91_usb_set_power(pdata, port, 0);
487 pdata->overcurrent_status[port] = 1;
488 pdata->overcurrent_changed[port] = 1;
489 }
490
491 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
492 val ? "exited" : "notified");
493
494 return IRQ_HANDLED;
495}
496
2419730f
JCPV
497static const struct of_device_id at91_ohci_dt_ids[] = {
498 { .compatible = "atmel,at91rm9200-ohci" },
499 { /* sentinel */ }
500};
501
502MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
503
bd73bfcd
AB
504/*-------------------------------------------------------------------------*/
505
506static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
2419730f
JCPV
507{
508 struct device_node *np = pdev->dev.of_node;
2419730f 509 struct at91_usbh_data *pdata;
bd73bfcd 510 int i;
bd73bfcd 511 int ret;
054d4b7b 512 int err;
bd73bfcd 513 u32 ports;
2419730f
JCPV
514
515 /* Right now device-tree probed devices don't get dma_mask set.
516 * Since shared usb code relies on it, set it here for now.
517 * Once we have dma capability bindings this can go away.
518 */
e1fd7341 519 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
22d9d8e8
RK
520 if (ret)
521 return ret;
2419730f
JCPV
522
523 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
524 if (!pdata)
525 return -ENOMEM;
526
4a0c4c36
AB
527 pdev->dev.platform_data = pdata;
528
2419730f
JCPV
529 if (!of_property_read_u32(np, "num-ports", &ports))
530 pdata->ports = ports;
531
bd73bfcd 532 at91_for_each_port(i) {
8f12dc24
PR
533 if (i >= pdata->ports)
534 break;
535
536 pdata->vbus_pin[i] =
537 devm_gpiod_get_index_optional(&pdev->dev, "atmel,vbus",
538 i, GPIOD_OUT_HIGH);
054d4b7b
WY
539 if (IS_ERR(pdata->vbus_pin[i])) {
540 err = PTR_ERR(pdata->vbus_pin[i]);
541 dev_err(&pdev->dev, "unable to claim gpio \"vbus\": %d\n", err);
e4df9227 542 continue;
bd73bfcd 543 }
bd73bfcd 544 }
aaf9f5fc 545
bd73bfcd 546 at91_for_each_port(i) {
4a0c4c36
AB
547 if (i >= pdata->ports)
548 break;
e4df9227
AB
549
550 pdata->overcurrent_pin[i] =
8f12dc24
PR
551 devm_gpiod_get_index_optional(&pdev->dev, "atmel,oc",
552 i, GPIOD_IN);
054d4b7b
WY
553 if (IS_ERR(pdata->overcurrent_pin[i])) {
554 err = PTR_ERR(pdata->overcurrent_pin[i]);
555 dev_err(&pdev->dev, "unable to claim gpio \"overcurrent\": %d\n", err);
bd73bfcd 556 continue;
aa6e52a3
TP
557 }
558
054d4b7b
WY
559 ret = devm_request_irq(&pdev->dev,
560 gpiod_to_irq(pdata->overcurrent_pin[i]),
561 ohci_hcd_at91_overcurrent_irq,
562 IRQF_SHARED,
563 "ohci_overcurrent", pdev);
564 if (ret)
565 dev_info(&pdev->dev, "failed to request gpio \"overcurrent\" IRQ\n");
4bde4a4c
DB
566 }
567
0365ee0a
DB
568 device_init_wakeup(&pdev->dev, 1);
569 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
39a269c0
AV
570}
571
fb4e98ab 572static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
39a269c0 573{
d4f09e28 574 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
4bde4a4c
DB
575 int i;
576
577 if (pdata) {
054d4b7b 578 at91_for_each_port(i)
aa6e52a3 579 ohci_at91_usb_set_power(pdata, i, 0);
4bde4a4c
DB
580 }
581
0365ee0a 582 device_init_wakeup(&pdev->dev, 0);
421b4bf5
PZ
583 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
584 return 0;
39a269c0
AV
585}
586
d2905193 587static int __maybe_unused
94ac0e5d 588ohci_hcd_at91_drv_suspend(struct device *dev)
68ba61b8 589{
94ac0e5d 590 struct usb_hcd *hcd = dev_get_drvdata(dev);
0365ee0a 591 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
1b207459 592 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
a9d3840e 593 int ret;
0365ee0a 594
1cee6b8d
SR
595 /*
596 * Disable wakeup if we are going to sleep with slow clock mode
597 * enabled.
598 */
599 ohci_at91->wakeup = device_may_wakeup(dev)
600 && !at91_suspend_entering_slow_clock();
601
602 if (ohci_at91->wakeup)
0365ee0a 603 enable_irq_wake(hcd->irq);
0365ee0a 604
2e2aa1bc
WY
605 ohci_at91_port_suspend(ohci_at91->sfr_regmap, 1);
606
1cee6b8d 607 ret = ohci_suspend(hcd, ohci_at91->wakeup);
a9d3840e 608 if (ret) {
1cee6b8d
SR
609 if (ohci_at91->wakeup)
610 disable_irq_wake(hcd->irq);
a9d3840e
MG
611 return ret;
612 }
0365ee0a
DB
613 /*
614 * The integrated transceivers seem unable to notice disconnect,
615 * reconnect, or wakeup without the 48 MHz clock active. so for
616 * correctness, always discard connection state (using reset).
617 *
618 * REVISIT: some boards will be able to turn VBUS off...
619 */
1cee6b8d 620 if (!ohci_at91->wakeup) {
e3825b48
MG
621 ohci->rh_state = OHCI_RH_HALTED;
622
869aa98c
PV
623 /* flush the writes */
624 (void) ohci_readl (ohci, &ohci->regs->control);
1b207459 625 at91_stop_clock(ohci_at91);
0365ee0a 626 }
39a269c0 627
a9d3840e 628 return ret;
39a269c0
AV
629}
630
d2905193
AB
631static int __maybe_unused
632ohci_hcd_at91_drv_resume(struct device *dev)
39a269c0 633{
94ac0e5d 634 struct usb_hcd *hcd = dev_get_drvdata(dev);
1b207459 635 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
6dde896e 636
1cee6b8d 637 if (ohci_at91->wakeup)
6dde896e
MP
638 disable_irq_wake(hcd->irq);
639
1cee6b8d 640 at91_start_clock(ohci_at91);
39a269c0 641
cfa49b4b 642 ohci_resume(hcd, false);
2e2aa1bc
WY
643
644 ohci_at91_port_suspend(ohci_at91->sfr_regmap, 0);
645
39a269c0
AV
646 return 0;
647}
39a269c0 648
94ac0e5d
SR
649static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
650 ohci_hcd_at91_drv_resume);
651
39a269c0
AV
652static struct platform_driver ohci_hcd_at91_driver = {
653 .probe = ohci_hcd_at91_drv_probe,
7690417d 654 .remove = ohci_hcd_at91_drv_remove,
64a21d02 655 .shutdown = usb_hcd_platform_shutdown,
39a269c0 656 .driver = {
0365ee0a 657 .name = "at91_ohci",
94ac0e5d 658 .pm = &ohci_hcd_at91_pm_ops,
be12be54 659 .of_match_table = at91_ohci_dt_ids,
39a269c0
AV
660 },
661};
e3825b48
MG
662
663static int __init ohci_at91_init(void)
664{
665 if (usb_disabled())
666 return -ENODEV;
667
668 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
1b207459 669 ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
e3825b48
MG
670
671 /*
672 * The Atmel HW has some unusual quirks, which require Atmel-specific
673 * workarounds. We override certain hc_driver functions here to
674 * achieve that. We explicitly do not enhance ohci_driver_overrides to
675 * allow this more easily, since this is an unusual case, and we don't
676 * want to encourage others to override these functions by making it
677 * too easy.
678 */
679
e3825b48
MG
680 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
681 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
682
683 return platform_driver_register(&ohci_hcd_at91_driver);
684}
685module_init(ohci_at91_init);
686
687static void __exit ohci_at91_cleanup(void)
688{
689 platform_driver_unregister(&ohci_hcd_at91_driver);
690}
691module_exit(ohci_at91_cleanup);
692
693MODULE_DESCRIPTION(DRIVER_DESC);
694MODULE_LICENSE("GPL");
695MODULE_ALIAS("platform:at91_ohci");