Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Generic platform ehci driver |
| 3 | * |
| 4 | * Copyright 2007 Steven Brown <sbrown@cortland.com> |
| 5 | * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de> |
| 6 | * |
| 7 | * Derived from the ohci-ssb driver |
| 8 | * Copyright 2007 Michael Buesch <m@bues.ch> |
| 9 | * |
| 10 | * Derived from the EHCI-PCI driver |
| 11 | * Copyright (c) 2000-2004 by David Brownell |
| 12 | * |
| 13 | * Derived from the ohci-pci driver |
| 14 | * Copyright 1999 Roman Weissgaerber |
| 15 | * Copyright 2000-2002 David Brownell |
| 16 | * Copyright 1999 Linus Torvalds |
| 17 | * Copyright 1999 Gregory P. Smith |
| 18 | * |
| 19 | * Licensed under the GNU/GPL. See COPYING for details. |
| 20 | */ |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Alan Stern | d1bb67a | 2012-11-02 10:13:24 -0400 | [diff] [blame^] | 22 | #include <linux/hrtimer.h> |
| 23 | #include <linux/io.h> |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 26 | #include <linux/usb.h> |
| 27 | #include <linux/usb/hcd.h> |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 28 | #include <linux/usb/ehci_pdriver.h> |
| 29 | |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 30 | #include "ehci.h" |
| 31 | |
| 32 | #define DRIVER_DESC "EHCI generic platform driver" |
| 33 | |
| 34 | static const char hcd_name[] = "ehci-platform"; |
| 35 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 36 | static int ehci_platform_reset(struct usb_hcd *hcd) |
| 37 | { |
| 38 | struct platform_device *pdev = to_platform_device(hcd->self.controller); |
| 39 | struct usb_ehci_pdata *pdata = pdev->dev.platform_data; |
| 40 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 41 | int retval; |
| 42 | |
| 43 | hcd->has_tt = pdata->has_tt; |
| 44 | ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug; |
| 45 | ehci->big_endian_desc = pdata->big_endian_desc; |
| 46 | ehci->big_endian_mmio = pdata->big_endian_mmio; |
| 47 | |
| 48 | ehci->caps = hcd->regs + pdata->caps_offset; |
| 49 | retval = ehci_setup(hcd); |
| 50 | if (retval) |
| 51 | return retval; |
| 52 | |
Florian Fainelli | 4534874 | 2012-10-08 15:11:21 +0200 | [diff] [blame] | 53 | if (pdata->no_io_watchdog) |
| 54 | ehci->need_io_watchdog = 0; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 58 | static struct hc_driver __read_mostly ehci_platform_hc_driver; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 59 | |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 60 | static const struct ehci_driver_overrides platform_overrides = { |
| 61 | .product_desc = "Generic Platform EHCI controller", |
| 62 | .reset = ehci_platform_reset, |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static int __devinit ehci_platform_probe(struct platform_device *dev) |
| 66 | { |
| 67 | struct usb_hcd *hcd; |
| 68 | struct resource *res_mem; |
Kuninori Morimoto | 86e4cb3 | 2012-08-06 18:06:53 -0700 | [diff] [blame] | 69 | struct usb_ehci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 70 | int irq; |
| 71 | int err = -ENOMEM; |
| 72 | |
Kuninori Morimoto | 86e4cb3 | 2012-08-06 18:06:53 -0700 | [diff] [blame] | 73 | if (!pdata) { |
| 74 | WARN_ON(1); |
| 75 | return -ENODEV; |
| 76 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 77 | |
| 78 | if (usb_disabled()) |
| 79 | return -ENODEV; |
| 80 | |
| 81 | irq = platform_get_irq(dev, 0); |
| 82 | if (irq < 0) { |
Florian Fainelli | 2350cb0 | 2012-10-08 15:11:41 +0200 | [diff] [blame] | 83 | dev_err(&dev->dev, "no irq provided"); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 84 | return irq; |
| 85 | } |
| 86 | res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 87 | if (!res_mem) { |
Florian Fainelli | 5c9b2b2 | 2012-10-08 15:11:43 +0200 | [diff] [blame] | 88 | dev_err(&dev->dev, "no memory resource provided"); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 89 | return -ENXIO; |
| 90 | } |
| 91 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 92 | if (pdata->power_on) { |
| 93 | err = pdata->power_on(dev); |
| 94 | if (err < 0) |
| 95 | return err; |
| 96 | } |
| 97 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 98 | hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev, |
| 99 | dev_name(&dev->dev)); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 100 | if (!hcd) { |
| 101 | err = -ENOMEM; |
| 102 | goto err_power; |
| 103 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 104 | |
| 105 | hcd->rsrc_start = res_mem->start; |
| 106 | hcd->rsrc_len = resource_size(res_mem); |
| 107 | |
Florian Fainelli | 61ff274 | 2012-10-08 15:11:45 +0200 | [diff] [blame] | 108 | hcd->regs = devm_request_and_ioremap(&dev->dev, res_mem); |
Julia Lawall | ec03ad8 | 2012-08-14 08:47:38 +0200 | [diff] [blame] | 109 | if (!hcd->regs) { |
| 110 | err = -ENOMEM; |
Florian Fainelli | 61ff274 | 2012-10-08 15:11:45 +0200 | [diff] [blame] | 111 | goto err_put_hcd; |
Julia Lawall | ec03ad8 | 2012-08-14 08:47:38 +0200 | [diff] [blame] | 112 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 113 | err = usb_add_hcd(hcd, irq, IRQF_SHARED); |
| 114 | if (err) |
Florian Fainelli | 61ff274 | 2012-10-08 15:11:45 +0200 | [diff] [blame] | 115 | goto err_put_hcd; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 116 | |
| 117 | platform_set_drvdata(dev, hcd); |
| 118 | |
| 119 | return err; |
| 120 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 121 | err_put_hcd: |
| 122 | usb_put_hcd(hcd); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 123 | err_power: |
| 124 | if (pdata->power_off) |
| 125 | pdata->power_off(dev); |
| 126 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 127 | return err; |
| 128 | } |
| 129 | |
| 130 | static int __devexit ehci_platform_remove(struct platform_device *dev) |
| 131 | { |
| 132 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 133 | struct usb_ehci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 134 | |
| 135 | usb_remove_hcd(hcd); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 136 | usb_put_hcd(hcd); |
| 137 | platform_set_drvdata(dev, NULL); |
| 138 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 139 | if (pdata->power_off) |
| 140 | pdata->power_off(dev); |
| 141 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | #ifdef CONFIG_PM |
| 146 | |
| 147 | static int ehci_platform_suspend(struct device *dev) |
| 148 | { |
| 149 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 150 | struct usb_ehci_pdata *pdata = dev->platform_data; |
| 151 | struct platform_device *pdev = |
| 152 | container_of(dev, struct platform_device, dev); |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 153 | bool do_wakeup = device_may_wakeup(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 154 | int ret; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 155 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 156 | ret = ehci_suspend(hcd, do_wakeup); |
| 157 | |
| 158 | if (pdata->power_suspend) |
| 159 | pdata->power_suspend(pdev); |
| 160 | |
| 161 | return ret; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static int ehci_platform_resume(struct device *dev) |
| 165 | { |
| 166 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 167 | struct usb_ehci_pdata *pdata = dev->platform_data; |
| 168 | struct platform_device *pdev = |
| 169 | container_of(dev, struct platform_device, dev); |
| 170 | |
| 171 | if (pdata->power_on) { |
| 172 | int err = pdata->power_on(pdev); |
| 173 | if (err < 0) |
| 174 | return err; |
| 175 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 176 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 177 | ehci_resume(hcd, false); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | #else /* !CONFIG_PM */ |
| 182 | #define ehci_platform_suspend NULL |
| 183 | #define ehci_platform_resume NULL |
| 184 | #endif /* CONFIG_PM */ |
| 185 | |
| 186 | static const struct platform_device_id ehci_platform_table[] = { |
| 187 | { "ehci-platform", 0 }, |
| 188 | { } |
| 189 | }; |
| 190 | MODULE_DEVICE_TABLE(platform, ehci_platform_table); |
| 191 | |
| 192 | static const struct dev_pm_ops ehci_platform_pm_ops = { |
| 193 | .suspend = ehci_platform_suspend, |
| 194 | .resume = ehci_platform_resume, |
| 195 | }; |
| 196 | |
| 197 | static struct platform_driver ehci_platform_driver = { |
| 198 | .id_table = ehci_platform_table, |
| 199 | .probe = ehci_platform_probe, |
| 200 | .remove = __devexit_p(ehci_platform_remove), |
| 201 | .shutdown = usb_hcd_platform_shutdown, |
| 202 | .driver = { |
| 203 | .owner = THIS_MODULE, |
| 204 | .name = "ehci-platform", |
| 205 | .pm = &ehci_platform_pm_ops, |
| 206 | } |
| 207 | }; |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 208 | |
| 209 | static int __init ehci_platform_init(void) |
| 210 | { |
| 211 | if (usb_disabled()) |
| 212 | return -ENODEV; |
| 213 | |
| 214 | pr_info("%s: " DRIVER_DESC "\n", hcd_name); |
| 215 | |
| 216 | ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides); |
| 217 | return platform_driver_register(&ehci_platform_driver); |
| 218 | } |
| 219 | module_init(ehci_platform_init); |
| 220 | |
| 221 | static void __exit ehci_platform_cleanup(void) |
| 222 | { |
| 223 | platform_driver_unregister(&ehci_platform_driver); |
| 224 | } |
| 225 | module_exit(ehci_platform_cleanup); |
| 226 | |
| 227 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 228 | MODULE_AUTHOR("Hauke Mehrtens"); |
| 229 | MODULE_AUTHOR("Alan Stern"); |
| 230 | MODULE_LICENSE("GPL"); |