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