0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/clk.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/io.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/jiffies.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/phy/phy.h>
0020 #include <linux/platform_data/usb-davinci.h>
0021 #include <linux/regulator/consumer.h>
0022 #include <linux/usb.h>
0023 #include <linux/usb/hcd.h>
0024 #include <asm/unaligned.h>
0025
0026 #include "ohci.h"
0027
0028 #define DRIVER_DESC "DA8XX"
0029 #define DRV_NAME "ohci-da8xx"
0030
0031 static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
0032
0033 static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
0034 u16 wValue, u16 wIndex, char *buf, u16 wLength);
0035 static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
0036
0037 struct da8xx_ohci_hcd {
0038 struct usb_hcd *hcd;
0039 struct clk *usb11_clk;
0040 struct phy *usb11_phy;
0041 struct regulator *vbus_reg;
0042 struct notifier_block nb;
0043 struct gpio_desc *oc_gpio;
0044 };
0045
0046 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
0047
0048
0049 static volatile u16 ocic_mask;
0050
0051 static int ohci_da8xx_enable(struct usb_hcd *hcd)
0052 {
0053 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0054 int ret;
0055
0056 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
0057 if (ret)
0058 return ret;
0059
0060 ret = phy_init(da8xx_ohci->usb11_phy);
0061 if (ret)
0062 goto err_phy_init;
0063
0064 ret = phy_power_on(da8xx_ohci->usb11_phy);
0065 if (ret)
0066 goto err_phy_power_on;
0067
0068 return 0;
0069
0070 err_phy_power_on:
0071 phy_exit(da8xx_ohci->usb11_phy);
0072 err_phy_init:
0073 clk_disable_unprepare(da8xx_ohci->usb11_clk);
0074
0075 return ret;
0076 }
0077
0078 static void ohci_da8xx_disable(struct usb_hcd *hcd)
0079 {
0080 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0081
0082 phy_power_off(da8xx_ohci->usb11_phy);
0083 phy_exit(da8xx_ohci->usb11_phy);
0084 clk_disable_unprepare(da8xx_ohci->usb11_clk);
0085 }
0086
0087 static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
0088 {
0089 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0090 struct device *dev = hcd->self.controller;
0091 int ret;
0092
0093 if (!da8xx_ohci->vbus_reg)
0094 return 0;
0095
0096 if (on) {
0097 ret = regulator_enable(da8xx_ohci->vbus_reg);
0098 if (ret) {
0099 dev_err(dev, "Failed to enable regulator: %d\n", ret);
0100 return ret;
0101 }
0102 } else {
0103 ret = regulator_disable(da8xx_ohci->vbus_reg);
0104 if (ret) {
0105 dev_err(dev, "Failed to disable regulator: %d\n", ret);
0106 return ret;
0107 }
0108 }
0109
0110 return 0;
0111 }
0112
0113 static int ohci_da8xx_get_power(struct usb_hcd *hcd)
0114 {
0115 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0116
0117 if (da8xx_ohci->vbus_reg)
0118 return regulator_is_enabled(da8xx_ohci->vbus_reg);
0119
0120 return 1;
0121 }
0122
0123 static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
0124 {
0125 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0126 unsigned int flags;
0127 int ret;
0128
0129 if (da8xx_ohci->oc_gpio)
0130 return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio);
0131
0132 if (!da8xx_ohci->vbus_reg)
0133 return 0;
0134
0135 ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
0136 if (ret)
0137 return ret;
0138
0139 if (flags & REGULATOR_ERROR_OVER_CURRENT)
0140 return 1;
0141
0142 return 0;
0143 }
0144
0145 static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
0146 {
0147 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0148
0149 if (da8xx_ohci->vbus_reg)
0150 return 1;
0151
0152 return 0;
0153 }
0154
0155 static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
0156 {
0157 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0158
0159 if (da8xx_ohci->oc_gpio)
0160 return 1;
0161
0162 if (da8xx_ohci->vbus_reg)
0163 return 1;
0164
0165 return 0;
0166 }
0167
0168 static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
0169 {
0170 struct device *dev = hcd->self.controller;
0171 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
0172
0173 if (hub && hub->potpgt)
0174 return 1;
0175
0176 return 0;
0177 }
0178
0179 static int ohci_da8xx_regulator_event(struct notifier_block *nb,
0180 unsigned long event, void *data)
0181 {
0182 struct da8xx_ohci_hcd *da8xx_ohci =
0183 container_of(nb, struct da8xx_ohci_hcd, nb);
0184
0185 if (event & REGULATOR_EVENT_OVER_CURRENT) {
0186 ocic_mask |= 1 << 1;
0187 ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
0188 }
0189
0190 return 0;
0191 }
0192
0193 static irqreturn_t ohci_da8xx_oc_thread(int irq, void *data)
0194 {
0195 struct da8xx_ohci_hcd *da8xx_ohci = data;
0196 struct device *dev = da8xx_ohci->hcd->self.controller;
0197 int ret;
0198
0199 if (gpiod_get_value_cansleep(da8xx_ohci->oc_gpio) &&
0200 da8xx_ohci->vbus_reg) {
0201 ret = regulator_disable(da8xx_ohci->vbus_reg);
0202 if (ret)
0203 dev_err(dev, "Failed to disable regulator: %d\n", ret);
0204 }
0205
0206 return IRQ_HANDLED;
0207 }
0208
0209 static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
0210 {
0211 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
0212 struct device *dev = hcd->self.controller;
0213 int ret = 0;
0214
0215 if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) {
0216 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
0217 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
0218 &da8xx_ohci->nb);
0219 }
0220
0221 if (ret)
0222 dev_err(dev, "Failed to register notifier: %d\n", ret);
0223
0224 return ret;
0225 }
0226
0227 static int ohci_da8xx_reset(struct usb_hcd *hcd)
0228 {
0229 struct device *dev = hcd->self.controller;
0230 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
0231 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
0232 int result;
0233 u32 rh_a;
0234
0235 dev_dbg(dev, "starting USB controller\n");
0236
0237 result = ohci_da8xx_enable(hcd);
0238 if (result < 0)
0239 return result;
0240
0241
0242
0243
0244
0245 ohci->num_ports = 1;
0246
0247 result = ohci_setup(hcd);
0248 if (result < 0) {
0249 ohci_da8xx_disable(hcd);
0250 return result;
0251 }
0252
0253
0254
0255
0256
0257
0258
0259 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
0260 if (ohci_da8xx_has_set_power(hcd)) {
0261 rh_a &= ~RH_A_NPS;
0262 rh_a |= RH_A_PSM;
0263 }
0264 if (ohci_da8xx_has_oci(hcd)) {
0265 rh_a &= ~RH_A_NOCP;
0266 rh_a |= RH_A_OCPM;
0267 }
0268 if (ohci_da8xx_has_potpgt(hcd)) {
0269 rh_a &= ~RH_A_POTPGT;
0270 rh_a |= hub->potpgt << 24;
0271 }
0272 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
0273
0274 return result;
0275 }
0276
0277
0278
0279
0280 static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
0281 {
0282 int length = orig_ohci_hub_status_data(hcd, buf);
0283
0284
0285 if (ocic_mask & (1 << 1)) {
0286 dev_dbg(hcd->self.controller, "over-current indicator change "
0287 "on port 1\n");
0288
0289 if (!length)
0290 length = 1;
0291
0292 buf[0] |= 1 << 1;
0293 }
0294 return length;
0295 }
0296
0297
0298
0299
0300 static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
0301 u16 wIndex, char *buf, u16 wLength)
0302 {
0303 struct device *dev = hcd->self.controller;
0304 int temp;
0305
0306 switch (typeReq) {
0307 case GetPortStatus:
0308
0309 if (wIndex != 1)
0310 break;
0311
0312 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
0313
0314 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
0315
0316
0317 if (!ohci_da8xx_get_power(hcd))
0318 temp &= ~RH_PS_PPS;
0319
0320
0321 if (ohci_da8xx_get_oci(hcd) > 0)
0322 temp |= RH_PS_POCI;
0323
0324
0325 if (ocic_mask & (1 << wIndex))
0326 temp |= RH_PS_OCIC;
0327
0328 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
0329 return 0;
0330 case SetPortFeature:
0331 temp = 1;
0332 goto check_port;
0333 case ClearPortFeature:
0334 temp = 0;
0335
0336 check_port:
0337
0338 if (wIndex != 1)
0339 break;
0340
0341 switch (wValue) {
0342 case USB_PORT_FEAT_POWER:
0343 dev_dbg(dev, "%sPortFeature(%u): %s\n",
0344 temp ? "Set" : "Clear", wIndex, "POWER");
0345
0346 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
0347 case USB_PORT_FEAT_C_OVER_CURRENT:
0348 dev_dbg(dev, "%sPortFeature(%u): %s\n",
0349 temp ? "Set" : "Clear", wIndex,
0350 "C_OVER_CURRENT");
0351
0352 if (temp)
0353 ocic_mask |= 1 << wIndex;
0354 else
0355 ocic_mask &= ~(1 << wIndex);
0356 return 0;
0357 }
0358 }
0359
0360 return orig_ohci_hub_control(hcd, typeReq, wValue,
0361 wIndex, buf, wLength);
0362 }
0363
0364
0365 #ifdef CONFIG_OF
0366 static const struct of_device_id da8xx_ohci_ids[] = {
0367 { .compatible = "ti,da830-ohci" },
0368 { }
0369 };
0370 MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
0371 #endif
0372
0373 static int ohci_da8xx_probe(struct platform_device *pdev)
0374 {
0375 struct da8xx_ohci_hcd *da8xx_ohci;
0376 struct device *dev = &pdev->dev;
0377 int error, hcd_irq, oc_irq;
0378 struct usb_hcd *hcd;
0379 struct resource *mem;
0380
0381 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev));
0382 if (!hcd)
0383 return -ENOMEM;
0384
0385 da8xx_ohci = to_da8xx_ohci(hcd);
0386 da8xx_ohci->hcd = hcd;
0387
0388 da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL);
0389 if (IS_ERR(da8xx_ohci->usb11_clk)) {
0390 error = PTR_ERR(da8xx_ohci->usb11_clk);
0391 if (error != -EPROBE_DEFER)
0392 dev_err(dev, "Failed to get clock.\n");
0393 goto err;
0394 }
0395
0396 da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy");
0397 if (IS_ERR(da8xx_ohci->usb11_phy)) {
0398 error = PTR_ERR(da8xx_ohci->usb11_phy);
0399 if (error != -EPROBE_DEFER)
0400 dev_err(dev, "Failed to get phy.\n");
0401 goto err;
0402 }
0403
0404 da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus");
0405 if (IS_ERR(da8xx_ohci->vbus_reg)) {
0406 error = PTR_ERR(da8xx_ohci->vbus_reg);
0407 if (error == -ENODEV) {
0408 da8xx_ohci->vbus_reg = NULL;
0409 } else if (error == -EPROBE_DEFER) {
0410 goto err;
0411 } else {
0412 dev_err(dev, "Failed to get regulator\n");
0413 goto err;
0414 }
0415 }
0416
0417 da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN);
0418 if (IS_ERR(da8xx_ohci->oc_gpio)) {
0419 error = PTR_ERR(da8xx_ohci->oc_gpio);
0420 goto err;
0421 }
0422
0423 if (da8xx_ohci->oc_gpio) {
0424 oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio);
0425 if (oc_irq < 0) {
0426 error = oc_irq;
0427 goto err;
0428 }
0429
0430 error = devm_request_threaded_irq(dev, oc_irq, NULL,
0431 ohci_da8xx_oc_thread, IRQF_TRIGGER_RISING |
0432 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0433 "OHCI over-current indicator", da8xx_ohci);
0434 if (error)
0435 goto err;
0436 }
0437
0438 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0439 hcd->regs = devm_ioremap_resource(dev, mem);
0440 if (IS_ERR(hcd->regs)) {
0441 error = PTR_ERR(hcd->regs);
0442 goto err;
0443 }
0444 hcd->rsrc_start = mem->start;
0445 hcd->rsrc_len = resource_size(mem);
0446
0447 hcd_irq = platform_get_irq(pdev, 0);
0448 if (hcd_irq < 0) {
0449 error = -ENODEV;
0450 goto err;
0451 }
0452
0453 error = usb_add_hcd(hcd, hcd_irq, 0);
0454 if (error)
0455 goto err;
0456
0457 device_wakeup_enable(hcd->self.controller);
0458
0459 error = ohci_da8xx_register_notify(hcd);
0460 if (error)
0461 goto err_remove_hcd;
0462
0463 return 0;
0464
0465 err_remove_hcd:
0466 usb_remove_hcd(hcd);
0467 err:
0468 usb_put_hcd(hcd);
0469 return error;
0470 }
0471
0472 static int ohci_da8xx_remove(struct platform_device *pdev)
0473 {
0474 struct usb_hcd *hcd = platform_get_drvdata(pdev);
0475
0476 usb_remove_hcd(hcd);
0477 usb_put_hcd(hcd);
0478
0479 return 0;
0480 }
0481
0482 #ifdef CONFIG_PM
0483 static int ohci_da8xx_suspend(struct platform_device *pdev,
0484 pm_message_t message)
0485 {
0486 struct usb_hcd *hcd = platform_get_drvdata(pdev);
0487 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
0488 bool do_wakeup = device_may_wakeup(&pdev->dev);
0489 int ret;
0490
0491
0492 if (time_before(jiffies, ohci->next_statechange))
0493 msleep(5);
0494 ohci->next_statechange = jiffies;
0495
0496 ret = ohci_suspend(hcd, do_wakeup);
0497 if (ret)
0498 return ret;
0499
0500 ohci_da8xx_disable(hcd);
0501 hcd->state = HC_STATE_SUSPENDED;
0502
0503 return ret;
0504 }
0505
0506 static int ohci_da8xx_resume(struct platform_device *dev)
0507 {
0508 struct usb_hcd *hcd = platform_get_drvdata(dev);
0509 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
0510 int ret;
0511
0512 if (time_before(jiffies, ohci->next_statechange))
0513 msleep(5);
0514 ohci->next_statechange = jiffies;
0515
0516 ret = ohci_da8xx_enable(hcd);
0517 if (ret)
0518 return ret;
0519
0520 ohci_resume(hcd, false);
0521
0522 return 0;
0523 }
0524 #endif
0525
0526 static const struct ohci_driver_overrides da8xx_overrides __initconst = {
0527 .reset = ohci_da8xx_reset,
0528 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
0529 };
0530
0531
0532
0533
0534 static struct platform_driver ohci_hcd_da8xx_driver = {
0535 .probe = ohci_da8xx_probe,
0536 .remove = ohci_da8xx_remove,
0537 .shutdown = usb_hcd_platform_shutdown,
0538 #ifdef CONFIG_PM
0539 .suspend = ohci_da8xx_suspend,
0540 .resume = ohci_da8xx_resume,
0541 #endif
0542 .driver = {
0543 .name = DRV_NAME,
0544 .of_match_table = of_match_ptr(da8xx_ohci_ids),
0545 },
0546 };
0547
0548 static int __init ohci_da8xx_init(void)
0549 {
0550
0551 if (usb_disabled())
0552 return -ENODEV;
0553
0554 pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
0555 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
0567 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
0568
0569 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
0570 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
0571
0572 return platform_driver_register(&ohci_hcd_da8xx_driver);
0573 }
0574 module_init(ohci_da8xx_init);
0575
0576 static void __exit ohci_da8xx_exit(void)
0577 {
0578 platform_driver_unregister(&ohci_hcd_da8xx_driver);
0579 }
0580 module_exit(ohci_da8xx_exit);
0581 MODULE_DESCRIPTION(DRIVER_DESC);
0582 MODULE_LICENSE("GPL");
0583 MODULE_ALIAS("platform:" DRV_NAME);