Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de> |
| 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 | * You should have received a copy of the GNU General Public License along |
| 10 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 11 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/regulator/consumer.h> |
| 18 | |
| 19 | struct jz4740_ohci_hcd { |
| 20 | struct ohci_hcd ohci_hcd; |
| 21 | |
| 22 | struct regulator *vbus; |
| 23 | bool vbus_enabled; |
| 24 | struct clk *clk; |
| 25 | }; |
| 26 | |
| 27 | static inline struct jz4740_ohci_hcd *hcd_to_jz4740_hcd(struct usb_hcd *hcd) |
| 28 | { |
| 29 | return (struct jz4740_ohci_hcd *)(hcd->hcd_priv); |
| 30 | } |
| 31 | |
| 32 | static inline struct usb_hcd *jz4740_hcd_to_hcd(struct jz4740_ohci_hcd *jz4740_ohci) |
| 33 | { |
| 34 | return container_of((void *)jz4740_ohci, struct usb_hcd, hcd_priv); |
| 35 | } |
| 36 | |
| 37 | static int ohci_jz4740_start(struct usb_hcd *hcd) |
| 38 | { |
| 39 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 40 | int ret; |
| 41 | |
| 42 | ret = ohci_init(ohci); |
| 43 | if (ret < 0) |
| 44 | return ret; |
| 45 | |
| 46 | ohci->num_ports = 1; |
| 47 | |
| 48 | ret = ohci_run(ohci); |
| 49 | if (ret < 0) { |
| 50 | dev_err(hcd->self.controller, "Can not start %s", |
| 51 | hcd->self.bus_name); |
| 52 | ohci_stop(hcd); |
| 53 | return ret; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int ohci_jz4740_set_vbus_power(struct jz4740_ohci_hcd *jz4740_ohci, |
| 59 | bool enabled) |
| 60 | { |
| 61 | int ret = 0; |
| 62 | |
| 63 | if (!jz4740_ohci->vbus) |
| 64 | return 0; |
| 65 | |
| 66 | if (enabled && !jz4740_ohci->vbus_enabled) { |
| 67 | ret = regulator_enable(jz4740_ohci->vbus); |
| 68 | if (ret) |
| 69 | dev_err(jz4740_hcd_to_hcd(jz4740_ohci)->self.controller, |
| 70 | "Could not power vbus\n"); |
| 71 | } else if (!enabled && jz4740_ohci->vbus_enabled) { |
| 72 | ret = regulator_disable(jz4740_ohci->vbus); |
| 73 | } |
| 74 | |
| 75 | if (ret == 0) |
| 76 | jz4740_ohci->vbus_enabled = enabled; |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static int ohci_jz4740_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, |
| 82 | u16 wIndex, char *buf, u16 wLength) |
| 83 | { |
| 84 | struct jz4740_ohci_hcd *jz4740_ohci = hcd_to_jz4740_hcd(hcd); |
Laurent Pinchart | 4d6b516 | 2014-04-14 12:08:13 +0200 | [diff] [blame] | 85 | int ret = 0; |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 86 | |
| 87 | switch (typeReq) { |
Laurent Pinchart | 166cf4a | 2014-04-14 12:08:14 +0200 | [diff] [blame] | 88 | case SetPortFeature: |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 89 | if (wValue == USB_PORT_FEAT_POWER) |
| 90 | ret = ohci_jz4740_set_vbus_power(jz4740_ohci, true); |
| 91 | break; |
Laurent Pinchart | 166cf4a | 2014-04-14 12:08:14 +0200 | [diff] [blame] | 92 | case ClearPortFeature: |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 93 | if (wValue == USB_PORT_FEAT_POWER) |
| 94 | ret = ohci_jz4740_set_vbus_power(jz4740_ohci, false); |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | return ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static const struct hc_driver ohci_jz4740_hc_driver = { |
| 106 | .description = hcd_name, |
| 107 | .product_desc = "JZ4740 OHCI", |
| 108 | .hcd_priv_size = sizeof(struct jz4740_ohci_hcd), |
| 109 | |
| 110 | /* |
| 111 | * generic hardware linkage |
| 112 | */ |
| 113 | .irq = ohci_irq, |
| 114 | .flags = HCD_USB11 | HCD_MEMORY, |
| 115 | |
| 116 | /* |
| 117 | * basic lifecycle operations |
| 118 | */ |
| 119 | .start = ohci_jz4740_start, |
| 120 | .stop = ohci_stop, |
| 121 | .shutdown = ohci_shutdown, |
| 122 | |
| 123 | /* |
| 124 | * managing i/o requests and associated device resources |
| 125 | */ |
| 126 | .urb_enqueue = ohci_urb_enqueue, |
| 127 | .urb_dequeue = ohci_urb_dequeue, |
| 128 | .endpoint_disable = ohci_endpoint_disable, |
| 129 | |
| 130 | /* |
| 131 | * scheduling support |
| 132 | */ |
| 133 | .get_frame_number = ohci_get_frame, |
| 134 | |
| 135 | /* |
| 136 | * root hub support |
| 137 | */ |
| 138 | .hub_status_data = ohci_hub_status_data, |
| 139 | .hub_control = ohci_jz4740_hub_control, |
| 140 | #ifdef CONFIG_PM |
| 141 | .bus_suspend = ohci_bus_suspend, |
| 142 | .bus_resume = ohci_bus_resume, |
| 143 | #endif |
| 144 | .start_port_reset = ohci_start_port_reset, |
| 145 | }; |
| 146 | |
| 147 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 148 | static int jz4740_ohci_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 149 | { |
| 150 | int ret; |
| 151 | struct usb_hcd *hcd; |
| 152 | struct jz4740_ohci_hcd *jz4740_ohci; |
| 153 | struct resource *res; |
| 154 | int irq; |
| 155 | |
| 156 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 157 | |
| 158 | if (!res) { |
| 159 | dev_err(&pdev->dev, "Failed to get platform resource\n"); |
| 160 | return -ENOENT; |
| 161 | } |
| 162 | |
| 163 | irq = platform_get_irq(pdev, 0); |
| 164 | if (irq < 0) { |
| 165 | dev_err(&pdev->dev, "Failed to get platform irq\n"); |
| 166 | return irq; |
| 167 | } |
| 168 | |
| 169 | hcd = usb_create_hcd(&ohci_jz4740_hc_driver, &pdev->dev, "jz4740"); |
| 170 | if (!hcd) { |
| 171 | dev_err(&pdev->dev, "Failed to create hcd.\n"); |
| 172 | return -ENOMEM; |
| 173 | } |
| 174 | |
| 175 | jz4740_ohci = hcd_to_jz4740_hcd(hcd); |
| 176 | |
Jingoo Han | c81c3b0 | 2013-12-11 16:24:17 +0900 | [diff] [blame] | 177 | hcd->rsrc_start = res->start; |
| 178 | hcd->rsrc_len = resource_size(res); |
| 179 | |
| 180 | hcd->regs = devm_ioremap_resource(&pdev->dev, res); |
| 181 | if (IS_ERR(hcd->regs)) { |
| 182 | ret = PTR_ERR(hcd->regs); |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 183 | goto err_free; |
| 184 | } |
| 185 | |
Jingoo Han | c81c3b0 | 2013-12-11 16:24:17 +0900 | [diff] [blame] | 186 | jz4740_ohci->clk = devm_clk_get(&pdev->dev, "uhc"); |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 187 | if (IS_ERR(jz4740_ohci->clk)) { |
| 188 | ret = PTR_ERR(jz4740_ohci->clk); |
| 189 | dev_err(&pdev->dev, "Failed to get clock: %d\n", ret); |
Jingoo Han | c81c3b0 | 2013-12-11 16:24:17 +0900 | [diff] [blame] | 190 | goto err_free; |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Jingoo Han | c81c3b0 | 2013-12-11 16:24:17 +0900 | [diff] [blame] | 193 | jz4740_ohci->vbus = devm_regulator_get(&pdev->dev, "vbus"); |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 194 | if (IS_ERR(jz4740_ohci->vbus)) |
| 195 | jz4740_ohci->vbus = NULL; |
| 196 | |
| 197 | |
| 198 | clk_set_rate(jz4740_ohci->clk, 48000000); |
| 199 | clk_enable(jz4740_ohci->clk); |
| 200 | if (jz4740_ohci->vbus) |
| 201 | ohci_jz4740_set_vbus_power(jz4740_ohci, true); |
| 202 | |
| 203 | platform_set_drvdata(pdev, hcd); |
| 204 | |
| 205 | ohci_hcd_init(hcd_to_ohci(hcd)); |
| 206 | |
| 207 | ret = usb_add_hcd(hcd, irq, 0); |
| 208 | if (ret) { |
| 209 | dev_err(&pdev->dev, "Failed to add hcd: %d\n", ret); |
| 210 | goto err_disable; |
| 211 | } |
Peter Chen | 3c9740a | 2013-11-05 10:46:02 +0800 | [diff] [blame] | 212 | device_wakeup_enable(hcd->self.controller); |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | |
| 216 | err_disable: |
Jingoo Han | c81c3b0 | 2013-12-11 16:24:17 +0900 | [diff] [blame] | 217 | if (jz4740_ohci->vbus) |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 218 | regulator_disable(jz4740_ohci->vbus); |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 219 | clk_disable(jz4740_ohci->clk); |
| 220 | |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 221 | err_free: |
| 222 | usb_put_hcd(hcd); |
| 223 | |
| 224 | return ret; |
| 225 | } |
| 226 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 227 | static int jz4740_ohci_remove(struct platform_device *pdev) |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 228 | { |
| 229 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 230 | struct jz4740_ohci_hcd *jz4740_ohci = hcd_to_jz4740_hcd(hcd); |
| 231 | |
| 232 | usb_remove_hcd(hcd); |
| 233 | |
Jingoo Han | c81c3b0 | 2013-12-11 16:24:17 +0900 | [diff] [blame] | 234 | if (jz4740_ohci->vbus) |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 235 | regulator_disable(jz4740_ohci->vbus); |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 236 | |
| 237 | clk_disable(jz4740_ohci->clk); |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 238 | |
| 239 | usb_put_hcd(hcd); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static struct platform_driver ohci_hcd_jz4740_driver = { |
| 245 | .probe = jz4740_ohci_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 246 | .remove = jz4740_ohci_remove, |
Lars-Peter Clausen | 2249071 | 2010-06-19 04:08:24 +0000 | [diff] [blame] | 247 | .driver = { |
| 248 | .name = "jz4740-ohci", |
| 249 | .owner = THIS_MODULE, |
| 250 | }, |
| 251 | }; |
| 252 | |
Stefan Weil | 1c0a380 | 2010-11-07 22:14:31 +0100 | [diff] [blame] | 253 | MODULE_ALIAS("platform:jz4740-ohci"); |