Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Generic platform ohci driver |
| 3 | * |
| 4 | * Copyright 2007 Michael Buesch <m@bues.ch> |
| 5 | * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de> |
| 6 | * |
| 7 | * Derived from the OCHI-SSB driver |
| 8 | * Derived from the OHCI-PCI driver |
| 9 | * Copyright 1999 Roman Weissgaerber |
| 10 | * Copyright 2000-2002 David Brownell |
| 11 | * Copyright 1999 Linus Torvalds |
| 12 | * Copyright 1999 Gregory P. Smith |
| 13 | * |
| 14 | * Licensed under the GNU/GPL. See COPYING for details. |
| 15 | */ |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/usb/ohci_pdriver.h> |
| 18 | |
| 19 | static int ohci_platform_reset(struct usb_hcd *hcd) |
| 20 | { |
| 21 | struct platform_device *pdev = to_platform_device(hcd->self.controller); |
| 22 | struct usb_ohci_pdata *pdata = pdev->dev.platform_data; |
| 23 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 24 | int err; |
| 25 | |
| 26 | if (pdata->big_endian_desc) |
| 27 | ohci->flags |= OHCI_QUIRK_BE_DESC; |
| 28 | if (pdata->big_endian_mmio) |
| 29 | ohci->flags |= OHCI_QUIRK_BE_MMIO; |
| 30 | if (pdata->no_big_frame_no) |
| 31 | ohci->flags |= OHCI_QUIRK_FRAME_NO; |
| 32 | |
| 33 | ohci_hcd_init(ohci); |
Florian Fainelli | 2b16e39 | 2012-10-08 15:11:26 +0200 | [diff] [blame] | 34 | |
| 35 | if (pdata->num_ports) |
| 36 | ohci->num_ports = pdata->num_ports; |
| 37 | |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 38 | err = ohci_init(ohci); |
| 39 | |
| 40 | return err; |
| 41 | } |
| 42 | |
| 43 | static int ohci_platform_start(struct usb_hcd *hcd) |
| 44 | { |
| 45 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 46 | int err; |
| 47 | |
| 48 | err = ohci_run(ohci); |
| 49 | if (err < 0) { |
| 50 | ohci_err(ohci, "can't start\n"); |
| 51 | ohci_stop(hcd); |
| 52 | } |
| 53 | |
| 54 | return err; |
| 55 | } |
| 56 | |
| 57 | static const struct hc_driver ohci_platform_hc_driver = { |
| 58 | .description = hcd_name, |
| 59 | .product_desc = "Generic Platform OHCI Controller", |
| 60 | .hcd_priv_size = sizeof(struct ohci_hcd), |
| 61 | |
| 62 | .irq = ohci_irq, |
| 63 | .flags = HCD_MEMORY | HCD_USB11, |
| 64 | |
| 65 | .reset = ohci_platform_reset, |
| 66 | .start = ohci_platform_start, |
| 67 | .stop = ohci_stop, |
| 68 | .shutdown = ohci_shutdown, |
| 69 | |
| 70 | .urb_enqueue = ohci_urb_enqueue, |
| 71 | .urb_dequeue = ohci_urb_dequeue, |
| 72 | .endpoint_disable = ohci_endpoint_disable, |
| 73 | |
| 74 | .get_frame_number = ohci_get_frame, |
| 75 | |
| 76 | .hub_status_data = ohci_hub_status_data, |
| 77 | .hub_control = ohci_hub_control, |
| 78 | #ifdef CONFIG_PM |
| 79 | .bus_suspend = ohci_bus_suspend, |
| 80 | .bus_resume = ohci_bus_resume, |
| 81 | #endif |
| 82 | |
| 83 | .start_port_reset = ohci_start_port_reset, |
| 84 | }; |
| 85 | |
| 86 | static int __devinit ohci_platform_probe(struct platform_device *dev) |
| 87 | { |
| 88 | struct usb_hcd *hcd; |
| 89 | struct resource *res_mem; |
Kuninori Morimoto | b6dd245 | 2012-08-06 18:07:30 -0700 | [diff] [blame] | 90 | struct usb_ohci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 91 | int irq; |
| 92 | int err = -ENOMEM; |
| 93 | |
Kuninori Morimoto | b6dd245 | 2012-08-06 18:07:30 -0700 | [diff] [blame] | 94 | if (!pdata) { |
| 95 | WARN_ON(1); |
| 96 | return -ENODEV; |
| 97 | } |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 98 | |
| 99 | if (usb_disabled()) |
| 100 | return -ENODEV; |
| 101 | |
| 102 | irq = platform_get_irq(dev, 0); |
| 103 | if (irq < 0) { |
Masanari Iida | 1c1301d | 2012-04-19 00:04:46 +0900 | [diff] [blame] | 104 | pr_err("no irq provided"); |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 105 | return irq; |
| 106 | } |
| 107 | |
| 108 | res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 109 | if (!res_mem) { |
Masanari Iida | 1c1301d | 2012-04-19 00:04:46 +0900 | [diff] [blame] | 110 | pr_err("no memory recourse provided"); |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 111 | return -ENXIO; |
| 112 | } |
| 113 | |
Kuninori Morimoto | e4d37ae | 2012-08-06 18:09:10 -0700 | [diff] [blame] | 114 | if (pdata->power_on) { |
| 115 | err = pdata->power_on(dev); |
| 116 | if (err < 0) |
| 117 | return err; |
| 118 | } |
| 119 | |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 120 | hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev, |
| 121 | dev_name(&dev->dev)); |
Kuninori Morimoto | e4d37ae | 2012-08-06 18:09:10 -0700 | [diff] [blame] | 122 | if (!hcd) { |
| 123 | err = -ENOMEM; |
| 124 | goto err_power; |
| 125 | } |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 126 | |
| 127 | hcd->rsrc_start = res_mem->start; |
| 128 | hcd->rsrc_len = resource_size(res_mem); |
| 129 | |
| 130 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) { |
| 131 | pr_err("controller already in use"); |
| 132 | err = -EBUSY; |
| 133 | goto err_put_hcd; |
| 134 | } |
| 135 | |
| 136 | hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); |
Julia Lawall | aece389 | 2012-08-14 08:47:37 +0200 | [diff] [blame] | 137 | if (!hcd->regs) { |
| 138 | err = -ENOMEM; |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 139 | goto err_release_region; |
Julia Lawall | aece389 | 2012-08-14 08:47:37 +0200 | [diff] [blame] | 140 | } |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 141 | err = usb_add_hcd(hcd, irq, IRQF_SHARED); |
| 142 | if (err) |
| 143 | goto err_iounmap; |
| 144 | |
| 145 | platform_set_drvdata(dev, hcd); |
| 146 | |
| 147 | return err; |
| 148 | |
| 149 | err_iounmap: |
| 150 | iounmap(hcd->regs); |
| 151 | err_release_region: |
| 152 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 153 | err_put_hcd: |
| 154 | usb_put_hcd(hcd); |
Kuninori Morimoto | e4d37ae | 2012-08-06 18:09:10 -0700 | [diff] [blame] | 155 | err_power: |
| 156 | if (pdata->power_off) |
| 157 | pdata->power_off(dev); |
| 158 | |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 159 | return err; |
| 160 | } |
| 161 | |
| 162 | static int __devexit ohci_platform_remove(struct platform_device *dev) |
| 163 | { |
| 164 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
Kuninori Morimoto | e4d37ae | 2012-08-06 18:09:10 -0700 | [diff] [blame] | 165 | struct usb_ohci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 166 | |
| 167 | usb_remove_hcd(hcd); |
| 168 | iounmap(hcd->regs); |
| 169 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 170 | usb_put_hcd(hcd); |
| 171 | platform_set_drvdata(dev, NULL); |
| 172 | |
Kuninori Morimoto | e4d37ae | 2012-08-06 18:09:10 -0700 | [diff] [blame] | 173 | if (pdata->power_off) |
| 174 | pdata->power_off(dev); |
| 175 | |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | #ifdef CONFIG_PM |
| 180 | |
| 181 | static int ohci_platform_suspend(struct device *dev) |
| 182 | { |
Kuninori Morimoto | e4d37ae | 2012-08-06 18:09:10 -0700 | [diff] [blame] | 183 | struct usb_ohci_pdata *pdata = dev->platform_data; |
| 184 | struct platform_device *pdev = |
| 185 | container_of(dev, struct platform_device, dev); |
| 186 | |
| 187 | if (pdata->power_suspend) |
| 188 | pdata->power_suspend(pdev); |
| 189 | |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int ohci_platform_resume(struct device *dev) |
| 194 | { |
| 195 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Kuninori Morimoto | e4d37ae | 2012-08-06 18:09:10 -0700 | [diff] [blame] | 196 | struct usb_ohci_pdata *pdata = dev->platform_data; |
| 197 | struct platform_device *pdev = |
| 198 | container_of(dev, struct platform_device, dev); |
| 199 | |
| 200 | if (pdata->power_on) { |
| 201 | int err = pdata->power_on(pdev); |
| 202 | if (err < 0) |
| 203 | return err; |
| 204 | } |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 205 | |
Florian Fainelli | cfa49b4 | 2012-10-08 15:11:29 +0200 | [diff] [blame^] | 206 | ohci_resume(hcd, false); |
Hauke Mehrtens | fa3364b | 2012-03-13 01:04:47 +0100 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | #else /* !CONFIG_PM */ |
| 211 | #define ohci_platform_suspend NULL |
| 212 | #define ohci_platform_resume NULL |
| 213 | #endif /* CONFIG_PM */ |
| 214 | |
| 215 | static const struct platform_device_id ohci_platform_table[] = { |
| 216 | { "ohci-platform", 0 }, |
| 217 | { } |
| 218 | }; |
| 219 | MODULE_DEVICE_TABLE(platform, ohci_platform_table); |
| 220 | |
| 221 | static const struct dev_pm_ops ohci_platform_pm_ops = { |
| 222 | .suspend = ohci_platform_suspend, |
| 223 | .resume = ohci_platform_resume, |
| 224 | }; |
| 225 | |
| 226 | static struct platform_driver ohci_platform_driver = { |
| 227 | .id_table = ohci_platform_table, |
| 228 | .probe = ohci_platform_probe, |
| 229 | .remove = __devexit_p(ohci_platform_remove), |
| 230 | .shutdown = usb_hcd_platform_shutdown, |
| 231 | .driver = { |
| 232 | .owner = THIS_MODULE, |
| 233 | .name = "ohci-platform", |
| 234 | .pm = &ohci_platform_pm_ops, |
| 235 | } |
| 236 | }; |