15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2efe7daf2SSergei Shtylyov /* 3efe7daf2SSergei Shtylyov * OHCI HCD (Host Controller Driver) for USB. 4efe7daf2SSergei Shtylyov * 5efe7daf2SSergei Shtylyov * TI DA8xx (OMAP-L1x) Bus Glue 6efe7daf2SSergei Shtylyov * 7efe7daf2SSergei Shtylyov * Derived from: ohci-omap.c and ohci-s3c2410.c 8efe7daf2SSergei Shtylyov * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com> 9efe7daf2SSergei Shtylyov */ 10efe7daf2SSergei Shtylyov 116c21caa3SManjunath Goudar #include <linux/clk.h> 12d193abf1SBartosz Golaszewski #include <linux/gpio/consumer.h> 136c21caa3SManjunath Goudar #include <linux/io.h> 14efe7daf2SSergei Shtylyov #include <linux/interrupt.h> 15efe7daf2SSergei Shtylyov #include <linux/jiffies.h> 166c21caa3SManjunath Goudar #include <linux/kernel.h> 176c21caa3SManjunath Goudar #include <linux/module.h> 18484468fbSRob Herring #include <linux/of.h> 19efe7daf2SSergei Shtylyov #include <linux/platform_device.h> 206110c425SDavid Lechner #include <linux/phy/phy.h> 21ec2a0833SArnd Bergmann #include <linux/platform_data/usb-davinci.h> 22c844ff74SAxel Haslam #include <linux/regulator/consumer.h> 236c21caa3SManjunath Goudar #include <linux/usb.h> 246c21caa3SManjunath Goudar #include <linux/usb/hcd.h> 256c21caa3SManjunath Goudar #include <asm/unaligned.h> 26efe7daf2SSergei Shtylyov 276c21caa3SManjunath Goudar #include "ohci.h" 286c21caa3SManjunath Goudar 296c21caa3SManjunath Goudar #define DRIVER_DESC "DA8XX" 30eacae5d2SAxel Haslam #define DRV_NAME "ohci-da8xx" 316c21caa3SManjunath Goudar 326c21caa3SManjunath Goudar static struct hc_driver __read_mostly ohci_da8xx_hc_driver; 336c21caa3SManjunath Goudar 346c21caa3SManjunath Goudar static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq, 356c21caa3SManjunath Goudar u16 wValue, u16 wIndex, char *buf, u16 wLength); 366c21caa3SManjunath Goudar static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf); 37efe7daf2SSergei Shtylyov 38c7a4f9f3SAxel Haslam struct da8xx_ohci_hcd { 39c844ff74SAxel Haslam struct usb_hcd *hcd; 40c7a4f9f3SAxel Haslam struct clk *usb11_clk; 41c7a4f9f3SAxel Haslam struct phy *usb11_phy; 42c844ff74SAxel Haslam struct regulator *vbus_reg; 43c844ff74SAxel Haslam struct notifier_block nb; 44d193abf1SBartosz Golaszewski struct gpio_desc *oc_gpio; 45c7a4f9f3SAxel Haslam }; 46c7a4f9f3SAxel Haslam 47c7a4f9f3SAxel Haslam #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv) 48efe7daf2SSergei Shtylyov 49efe7daf2SSergei Shtylyov /* Over-current indicator change bitmask */ 50efe7daf2SSergei Shtylyov static volatile u16 ocic_mask; 51efe7daf2SSergei Shtylyov 52c7a4f9f3SAxel Haslam static int ohci_da8xx_enable(struct usb_hcd *hcd) 53efe7daf2SSergei Shtylyov { 54c7a4f9f3SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 556110c425SDavid Lechner int ret; 56efe7daf2SSergei Shtylyov 57c7a4f9f3SAxel Haslam ret = clk_prepare_enable(da8xx_ohci->usb11_clk); 586110c425SDavid Lechner if (ret) 596110c425SDavid Lechner return ret; 60efe7daf2SSergei Shtylyov 61c7a4f9f3SAxel Haslam ret = phy_init(da8xx_ohci->usb11_phy); 626110c425SDavid Lechner if (ret) 636110c425SDavid Lechner goto err_phy_init; 64efe7daf2SSergei Shtylyov 65c7a4f9f3SAxel Haslam ret = phy_power_on(da8xx_ohci->usb11_phy); 666110c425SDavid Lechner if (ret) 676110c425SDavid Lechner goto err_phy_power_on; 68efe7daf2SSergei Shtylyov 696110c425SDavid Lechner return 0; 706110c425SDavid Lechner 716110c425SDavid Lechner err_phy_power_on: 72c7a4f9f3SAxel Haslam phy_exit(da8xx_ohci->usb11_phy); 736110c425SDavid Lechner err_phy_init: 74c7a4f9f3SAxel Haslam clk_disable_unprepare(da8xx_ohci->usb11_clk); 756110c425SDavid Lechner 766110c425SDavid Lechner return ret; 77efe7daf2SSergei Shtylyov } 78efe7daf2SSergei Shtylyov 79c7a4f9f3SAxel Haslam static void ohci_da8xx_disable(struct usb_hcd *hcd) 806110c425SDavid Lechner { 81c7a4f9f3SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 82c7a4f9f3SAxel Haslam 83c7a4f9f3SAxel Haslam phy_power_off(da8xx_ohci->usb11_phy); 84c7a4f9f3SAxel Haslam phy_exit(da8xx_ohci->usb11_phy); 85c7a4f9f3SAxel Haslam clk_disable_unprepare(da8xx_ohci->usb11_clk); 86efe7daf2SSergei Shtylyov } 87efe7daf2SSergei Shtylyov 88f3c56fb3SAxel Haslam static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on) 89f3c56fb3SAxel Haslam { 90c844ff74SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 91f3c56fb3SAxel Haslam struct device *dev = hcd->self.controller; 92c844ff74SAxel Haslam int ret; 93f3c56fb3SAxel Haslam 94c844ff74SAxel Haslam if (!da8xx_ohci->vbus_reg) 95c844ff74SAxel Haslam return 0; 96c844ff74SAxel Haslam 978e2f5eaeSBartosz Golaszewski if (on) { 98c844ff74SAxel Haslam ret = regulator_enable(da8xx_ohci->vbus_reg); 99c844ff74SAxel Haslam if (ret) { 100c844ff74SAxel Haslam dev_err(dev, "Failed to enable regulator: %d\n", ret); 101c844ff74SAxel Haslam return ret; 102c844ff74SAxel Haslam } 1038e2f5eaeSBartosz Golaszewski } else { 104c844ff74SAxel Haslam ret = regulator_disable(da8xx_ohci->vbus_reg); 105c844ff74SAxel Haslam if (ret) { 106c844ff74SAxel Haslam dev_err(dev, "Failed to disable regulator: %d\n", ret); 107c844ff74SAxel Haslam return ret; 108c844ff74SAxel Haslam } 109c844ff74SAxel Haslam } 110c844ff74SAxel Haslam 111f3c56fb3SAxel Haslam return 0; 112f3c56fb3SAxel Haslam } 113f3c56fb3SAxel Haslam 114f3c56fb3SAxel Haslam static int ohci_da8xx_get_power(struct usb_hcd *hcd) 115f3c56fb3SAxel Haslam { 116c844ff74SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 117f3c56fb3SAxel Haslam 118c844ff74SAxel Haslam if (da8xx_ohci->vbus_reg) 119c844ff74SAxel Haslam return regulator_is_enabled(da8xx_ohci->vbus_reg); 120c844ff74SAxel Haslam 121f3c56fb3SAxel Haslam return 1; 122f3c56fb3SAxel Haslam } 123f3c56fb3SAxel Haslam 124f3c56fb3SAxel Haslam static int ohci_da8xx_get_oci(struct usb_hcd *hcd) 125f3c56fb3SAxel Haslam { 126c844ff74SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 127c844ff74SAxel Haslam unsigned int flags; 128c844ff74SAxel Haslam int ret; 129f3c56fb3SAxel Haslam 130d193abf1SBartosz Golaszewski if (da8xx_ohci->oc_gpio) 131d193abf1SBartosz Golaszewski return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio); 132f3c56fb3SAxel Haslam 133c844ff74SAxel Haslam if (!da8xx_ohci->vbus_reg) 134c844ff74SAxel Haslam return 0; 135c844ff74SAxel Haslam 136c844ff74SAxel Haslam ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags); 137c844ff74SAxel Haslam if (ret) 138c844ff74SAxel Haslam return ret; 139c844ff74SAxel Haslam 140c844ff74SAxel Haslam if (flags & REGULATOR_ERROR_OVER_CURRENT) 141c844ff74SAxel Haslam return 1; 142c844ff74SAxel Haslam 143f3c56fb3SAxel Haslam return 0; 144f3c56fb3SAxel Haslam } 145f3c56fb3SAxel Haslam 146f3c56fb3SAxel Haslam static int ohci_da8xx_has_set_power(struct usb_hcd *hcd) 147f3c56fb3SAxel Haslam { 148c844ff74SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 149f3c56fb3SAxel Haslam 150c844ff74SAxel Haslam if (da8xx_ohci->vbus_reg) 151c844ff74SAxel Haslam return 1; 152c844ff74SAxel Haslam 153f3c56fb3SAxel Haslam return 0; 154f3c56fb3SAxel Haslam } 155f3c56fb3SAxel Haslam 156f3c56fb3SAxel Haslam static int ohci_da8xx_has_oci(struct usb_hcd *hcd) 157f3c56fb3SAxel Haslam { 158c844ff74SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 159f3c56fb3SAxel Haslam 160d193abf1SBartosz Golaszewski if (da8xx_ohci->oc_gpio) 161f3c56fb3SAxel Haslam return 1; 162f3c56fb3SAxel Haslam 163c844ff74SAxel Haslam if (da8xx_ohci->vbus_reg) 164c844ff74SAxel Haslam return 1; 165c844ff74SAxel Haslam 166f3c56fb3SAxel Haslam return 0; 167f3c56fb3SAxel Haslam } 168f3c56fb3SAxel Haslam 169f3c56fb3SAxel Haslam static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd) 170f3c56fb3SAxel Haslam { 171f3c56fb3SAxel Haslam struct device *dev = hcd->self.controller; 172f3c56fb3SAxel Haslam struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev); 173f3c56fb3SAxel Haslam 174f3c56fb3SAxel Haslam if (hub && hub->potpgt) 175f3c56fb3SAxel Haslam return 1; 176f3c56fb3SAxel Haslam 177f3c56fb3SAxel Haslam return 0; 178f3c56fb3SAxel Haslam } 179f3c56fb3SAxel Haslam 180c844ff74SAxel Haslam static int ohci_da8xx_regulator_event(struct notifier_block *nb, 181c844ff74SAxel Haslam unsigned long event, void *data) 182f3c56fb3SAxel Haslam { 183c844ff74SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = 184c844ff74SAxel Haslam container_of(nb, struct da8xx_ohci_hcd, nb); 185f3c56fb3SAxel Haslam 186c844ff74SAxel Haslam if (event & REGULATOR_EVENT_OVER_CURRENT) { 187c844ff74SAxel Haslam ocic_mask |= 1 << 1; 188c844ff74SAxel Haslam ohci_da8xx_set_power(da8xx_ohci->hcd, 0); 189c844ff74SAxel Haslam } 190f3c56fb3SAxel Haslam 191f3c56fb3SAxel Haslam return 0; 192f3c56fb3SAxel Haslam } 193f3c56fb3SAxel Haslam 194d3273301SBartosz Golaszewski static irqreturn_t ohci_da8xx_oc_thread(int irq, void *data) 195d193abf1SBartosz Golaszewski { 196d193abf1SBartosz Golaszewski struct da8xx_ohci_hcd *da8xx_ohci = data; 197d3273301SBartosz Golaszewski struct device *dev = da8xx_ohci->hcd->self.controller; 198d3273301SBartosz Golaszewski int ret; 199d193abf1SBartosz Golaszewski 200512de1ceSBartosz Golaszewski if (gpiod_get_value_cansleep(da8xx_ohci->oc_gpio) && 201512de1ceSBartosz Golaszewski da8xx_ohci->vbus_reg) { 202d3273301SBartosz Golaszewski ret = regulator_disable(da8xx_ohci->vbus_reg); 203d3273301SBartosz Golaszewski if (ret) 204512de1ceSBartosz Golaszewski dev_err(dev, "Failed to disable regulator: %d\n", ret); 205d3273301SBartosz Golaszewski } 206d193abf1SBartosz Golaszewski 207d193abf1SBartosz Golaszewski return IRQ_HANDLED; 208d193abf1SBartosz Golaszewski } 209d193abf1SBartosz Golaszewski 210c844ff74SAxel Haslam static int ohci_da8xx_register_notify(struct usb_hcd *hcd) 211c844ff74SAxel Haslam { 212c844ff74SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 213c844ff74SAxel Haslam struct device *dev = hcd->self.controller; 214c844ff74SAxel Haslam int ret = 0; 215c844ff74SAxel Haslam 216d193abf1SBartosz Golaszewski if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) { 217c844ff74SAxel Haslam da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event; 218c844ff74SAxel Haslam ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg, 219c844ff74SAxel Haslam &da8xx_ohci->nb); 220c844ff74SAxel Haslam } 221c844ff74SAxel Haslam 222c844ff74SAxel Haslam if (ret) 223c844ff74SAxel Haslam dev_err(dev, "Failed to register notifier: %d\n", ret); 224c844ff74SAxel Haslam 225c844ff74SAxel Haslam return ret; 226c844ff74SAxel Haslam } 227c844ff74SAxel Haslam 2286c21caa3SManjunath Goudar static int ohci_da8xx_reset(struct usb_hcd *hcd) 229efe7daf2SSergei Shtylyov { 230efe7daf2SSergei Shtylyov struct device *dev = hcd->self.controller; 231d4f09e28SJingoo Han struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev); 232efe7daf2SSergei Shtylyov struct ohci_hcd *ohci = hcd_to_ohci(hcd); 233efe7daf2SSergei Shtylyov int result; 234efe7daf2SSergei Shtylyov u32 rh_a; 235efe7daf2SSergei Shtylyov 236efe7daf2SSergei Shtylyov dev_dbg(dev, "starting USB controller\n"); 237efe7daf2SSergei Shtylyov 238c7a4f9f3SAxel Haslam result = ohci_da8xx_enable(hcd); 2396110c425SDavid Lechner if (result < 0) 2406110c425SDavid Lechner return result; 241efe7daf2SSergei Shtylyov 242efe7daf2SSergei Shtylyov /* 243efe7daf2SSergei Shtylyov * DA8xx only have 1 port connected to the pins but the HC root hub 244efe7daf2SSergei Shtylyov * register A reports 2 ports, thus we'll have to override it... 245efe7daf2SSergei Shtylyov */ 246efe7daf2SSergei Shtylyov ohci->num_ports = 1; 247efe7daf2SSergei Shtylyov 2486c21caa3SManjunath Goudar result = ohci_setup(hcd); 2496110c425SDavid Lechner if (result < 0) { 250c7a4f9f3SAxel Haslam ohci_da8xx_disable(hcd); 251efe7daf2SSergei Shtylyov return result; 2526110c425SDavid Lechner } 253efe7daf2SSergei Shtylyov 254efe7daf2SSergei Shtylyov /* 255efe7daf2SSergei Shtylyov * Since we're providing a board-specific root hub port power control 256efe7daf2SSergei Shtylyov * and over-current reporting, we have to override the HC root hub A 257efe7daf2SSergei Shtylyov * register's default value, so that ohci_hub_control() could return 258efe7daf2SSergei Shtylyov * the correct hub descriptor... 259efe7daf2SSergei Shtylyov */ 260efe7daf2SSergei Shtylyov rh_a = ohci_readl(ohci, &ohci->regs->roothub.a); 261f3c56fb3SAxel Haslam if (ohci_da8xx_has_set_power(hcd)) { 262efe7daf2SSergei Shtylyov rh_a &= ~RH_A_NPS; 263efe7daf2SSergei Shtylyov rh_a |= RH_A_PSM; 264efe7daf2SSergei Shtylyov } 265f3c56fb3SAxel Haslam if (ohci_da8xx_has_oci(hcd)) { 266efe7daf2SSergei Shtylyov rh_a &= ~RH_A_NOCP; 267efe7daf2SSergei Shtylyov rh_a |= RH_A_OCPM; 268efe7daf2SSergei Shtylyov } 269f3c56fb3SAxel Haslam if (ohci_da8xx_has_potpgt(hcd)) { 270efe7daf2SSergei Shtylyov rh_a &= ~RH_A_POTPGT; 271efe7daf2SSergei Shtylyov rh_a |= hub->potpgt << 24; 272f3c56fb3SAxel Haslam } 273efe7daf2SSergei Shtylyov ohci_writel(ohci, rh_a, &ohci->regs->roothub.a); 274efe7daf2SSergei Shtylyov 275efe7daf2SSergei Shtylyov return result; 276efe7daf2SSergei Shtylyov } 277efe7daf2SSergei Shtylyov 278efe7daf2SSergei Shtylyov /* 279efe7daf2SSergei Shtylyov * Update the status data from the hub with the over-current indicator change. 280efe7daf2SSergei Shtylyov */ 281efe7daf2SSergei Shtylyov static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf) 282efe7daf2SSergei Shtylyov { 2836c21caa3SManjunath Goudar int length = orig_ohci_hub_status_data(hcd, buf); 284efe7daf2SSergei Shtylyov 285efe7daf2SSergei Shtylyov /* See if we have OCIC bit set on port 1 */ 286efe7daf2SSergei Shtylyov if (ocic_mask & (1 << 1)) { 287efe7daf2SSergei Shtylyov dev_dbg(hcd->self.controller, "over-current indicator change " 288efe7daf2SSergei Shtylyov "on port 1\n"); 289efe7daf2SSergei Shtylyov 290efe7daf2SSergei Shtylyov if (!length) 291efe7daf2SSergei Shtylyov length = 1; 292efe7daf2SSergei Shtylyov 293efe7daf2SSergei Shtylyov buf[0] |= 1 << 1; 294efe7daf2SSergei Shtylyov } 295efe7daf2SSergei Shtylyov return length; 296efe7daf2SSergei Shtylyov } 297efe7daf2SSergei Shtylyov 298efe7daf2SSergei Shtylyov /* 299efe7daf2SSergei Shtylyov * Look at the control requests to the root hub and see if we need to override. 300efe7daf2SSergei Shtylyov */ 301efe7daf2SSergei Shtylyov static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 302efe7daf2SSergei Shtylyov u16 wIndex, char *buf, u16 wLength) 303efe7daf2SSergei Shtylyov { 304efe7daf2SSergei Shtylyov struct device *dev = hcd->self.controller; 305efe7daf2SSergei Shtylyov int temp; 306efe7daf2SSergei Shtylyov 307efe7daf2SSergei Shtylyov switch (typeReq) { 308efe7daf2SSergei Shtylyov case GetPortStatus: 309efe7daf2SSergei Shtylyov /* Check the port number */ 310efe7daf2SSergei Shtylyov if (wIndex != 1) 311efe7daf2SSergei Shtylyov break; 312efe7daf2SSergei Shtylyov 313efe7daf2SSergei Shtylyov dev_dbg(dev, "GetPortStatus(%u)\n", wIndex); 314efe7daf2SSergei Shtylyov 315efe7daf2SSergei Shtylyov temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1); 316efe7daf2SSergei Shtylyov 317efe7daf2SSergei Shtylyov /* The port power status (PPS) bit defaults to 1 */ 318f3c56fb3SAxel Haslam if (!ohci_da8xx_get_power(hcd)) 319efe7daf2SSergei Shtylyov temp &= ~RH_PS_PPS; 320efe7daf2SSergei Shtylyov 321efe7daf2SSergei Shtylyov /* The port over-current indicator (POCI) bit is always 0 */ 322f3c56fb3SAxel Haslam if (ohci_da8xx_get_oci(hcd) > 0) 323efe7daf2SSergei Shtylyov temp |= RH_PS_POCI; 324efe7daf2SSergei Shtylyov 325efe7daf2SSergei Shtylyov /* The over-current indicator change (OCIC) bit is 0 too */ 326efe7daf2SSergei Shtylyov if (ocic_mask & (1 << wIndex)) 327efe7daf2SSergei Shtylyov temp |= RH_PS_OCIC; 328efe7daf2SSergei Shtylyov 329efe7daf2SSergei Shtylyov put_unaligned(cpu_to_le32(temp), (__le32 *)buf); 330efe7daf2SSergei Shtylyov return 0; 331efe7daf2SSergei Shtylyov case SetPortFeature: 332efe7daf2SSergei Shtylyov temp = 1; 333efe7daf2SSergei Shtylyov goto check_port; 334efe7daf2SSergei Shtylyov case ClearPortFeature: 335efe7daf2SSergei Shtylyov temp = 0; 336efe7daf2SSergei Shtylyov 337efe7daf2SSergei Shtylyov check_port: 338efe7daf2SSergei Shtylyov /* Check the port number */ 339efe7daf2SSergei Shtylyov if (wIndex != 1) 340efe7daf2SSergei Shtylyov break; 341efe7daf2SSergei Shtylyov 342efe7daf2SSergei Shtylyov switch (wValue) { 343efe7daf2SSergei Shtylyov case USB_PORT_FEAT_POWER: 344efe7daf2SSergei Shtylyov dev_dbg(dev, "%sPortFeature(%u): %s\n", 345efe7daf2SSergei Shtylyov temp ? "Set" : "Clear", wIndex, "POWER"); 346efe7daf2SSergei Shtylyov 347f3c56fb3SAxel Haslam return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0; 348efe7daf2SSergei Shtylyov case USB_PORT_FEAT_C_OVER_CURRENT: 349efe7daf2SSergei Shtylyov dev_dbg(dev, "%sPortFeature(%u): %s\n", 350efe7daf2SSergei Shtylyov temp ? "Set" : "Clear", wIndex, 351efe7daf2SSergei Shtylyov "C_OVER_CURRENT"); 352efe7daf2SSergei Shtylyov 353efe7daf2SSergei Shtylyov if (temp) 354efe7daf2SSergei Shtylyov ocic_mask |= 1 << wIndex; 355efe7daf2SSergei Shtylyov else 356efe7daf2SSergei Shtylyov ocic_mask &= ~(1 << wIndex); 357efe7daf2SSergei Shtylyov return 0; 358efe7daf2SSergei Shtylyov } 359efe7daf2SSergei Shtylyov } 360efe7daf2SSergei Shtylyov 3616c21caa3SManjunath Goudar return orig_ohci_hub_control(hcd, typeReq, wValue, 3626c21caa3SManjunath Goudar wIndex, buf, wLength); 363efe7daf2SSergei Shtylyov } 364efe7daf2SSergei Shtylyov 365efe7daf2SSergei Shtylyov /*-------------------------------------------------------------------------*/ 366190534f6SAxel Haslam #ifdef CONFIG_OF 367190534f6SAxel Haslam static const struct of_device_id da8xx_ohci_ids[] = { 368190534f6SAxel Haslam { .compatible = "ti,da830-ohci" }, 369190534f6SAxel Haslam { } 370190534f6SAxel Haslam }; 371190534f6SAxel Haslam MODULE_DEVICE_TABLE(of, da8xx_ohci_ids); 372190534f6SAxel Haslam #endif 373efe7daf2SSergei Shtylyov 3746c21caa3SManjunath Goudar static int ohci_da8xx_probe(struct platform_device *pdev) 375efe7daf2SSergei Shtylyov { 376c7a4f9f3SAxel Haslam struct da8xx_ohci_hcd *da8xx_ohci; 3773d2ab9f3SBartosz Golaszewski struct device *dev = &pdev->dev; 378d193abf1SBartosz Golaszewski int error, hcd_irq, oc_irq; 379efe7daf2SSergei Shtylyov struct usb_hcd *hcd; 380efe7daf2SSergei Shtylyov struct resource *mem; 38108e46f18SBartosz Golaszewski 3823d2ab9f3SBartosz Golaszewski hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev)); 383644db166SJingoo Han if (!hcd) 384644db166SJingoo Han return -ENOMEM; 385efe7daf2SSergei Shtylyov 386c7a4f9f3SAxel Haslam da8xx_ohci = to_da8xx_ohci(hcd); 387c844ff74SAxel Haslam da8xx_ohci->hcd = hcd; 388c7a4f9f3SAxel Haslam 3893d2ab9f3SBartosz Golaszewski da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL); 390c7a4f9f3SAxel Haslam if (IS_ERR(da8xx_ohci->usb11_clk)) { 391c7a4f9f3SAxel Haslam error = PTR_ERR(da8xx_ohci->usb11_clk); 392c7a4f9f3SAxel Haslam if (error != -EPROBE_DEFER) 3933d2ab9f3SBartosz Golaszewski dev_err(dev, "Failed to get clock.\n"); 394c7a4f9f3SAxel Haslam goto err; 395c7a4f9f3SAxel Haslam } 396c7a4f9f3SAxel Haslam 3973d2ab9f3SBartosz Golaszewski da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy"); 398c7a4f9f3SAxel Haslam if (IS_ERR(da8xx_ohci->usb11_phy)) { 399c7a4f9f3SAxel Haslam error = PTR_ERR(da8xx_ohci->usb11_phy); 400c7a4f9f3SAxel Haslam if (error != -EPROBE_DEFER) 4013d2ab9f3SBartosz Golaszewski dev_err(dev, "Failed to get phy.\n"); 402c7a4f9f3SAxel Haslam goto err; 403c7a4f9f3SAxel Haslam } 404c7a4f9f3SAxel Haslam 4053d2ab9f3SBartosz Golaszewski da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus"); 406c844ff74SAxel Haslam if (IS_ERR(da8xx_ohci->vbus_reg)) { 407c844ff74SAxel Haslam error = PTR_ERR(da8xx_ohci->vbus_reg); 408c844ff74SAxel Haslam if (error == -ENODEV) { 409c844ff74SAxel Haslam da8xx_ohci->vbus_reg = NULL; 410c844ff74SAxel Haslam } else if (error == -EPROBE_DEFER) { 411c844ff74SAxel Haslam goto err; 412c844ff74SAxel Haslam } else { 4133d2ab9f3SBartosz Golaszewski dev_err(dev, "Failed to get regulator\n"); 414c844ff74SAxel Haslam goto err; 415c844ff74SAxel Haslam } 416c844ff74SAxel Haslam } 417c844ff74SAxel Haslam 418d193abf1SBartosz Golaszewski da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN); 419ba9b4081SColin Ian King if (IS_ERR(da8xx_ohci->oc_gpio)) { 420ba9b4081SColin Ian King error = PTR_ERR(da8xx_ohci->oc_gpio); 421d193abf1SBartosz Golaszewski goto err; 422ba9b4081SColin Ian King } 423d193abf1SBartosz Golaszewski 424d193abf1SBartosz Golaszewski if (da8xx_ohci->oc_gpio) { 425d193abf1SBartosz Golaszewski oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio); 426ba9b4081SColin Ian King if (oc_irq < 0) { 427ba9b4081SColin Ian King error = oc_irq; 428d193abf1SBartosz Golaszewski goto err; 429ba9b4081SColin Ian King } 430d193abf1SBartosz Golaszewski 431d3273301SBartosz Golaszewski error = devm_request_threaded_irq(dev, oc_irq, NULL, 432d3273301SBartosz Golaszewski ohci_da8xx_oc_thread, IRQF_TRIGGER_RISING | 433d3273301SBartosz Golaszewski IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 434d193abf1SBartosz Golaszewski "OHCI over-current indicator", da8xx_ohci); 435d193abf1SBartosz Golaszewski if (error) 436d193abf1SBartosz Golaszewski goto err; 437d193abf1SBartosz Golaszewski } 438d193abf1SBartosz Golaszewski 439*54a15a7eSYangtao Li hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem); 440644db166SJingoo Han if (IS_ERR(hcd->regs)) { 441644db166SJingoo Han error = PTR_ERR(hcd->regs); 442644db166SJingoo Han goto err; 443efe7daf2SSergei Shtylyov } 44454891d74SVarka Bhadram hcd->rsrc_start = mem->start; 44554891d74SVarka Bhadram hcd->rsrc_len = resource_size(mem); 446efe7daf2SSergei Shtylyov 447d193abf1SBartosz Golaszewski hcd_irq = platform_get_irq(pdev, 0); 448d193abf1SBartosz Golaszewski if (hcd_irq < 0) { 449efe7daf2SSergei Shtylyov error = -ENODEV; 450644db166SJingoo Han goto err; 451efe7daf2SSergei Shtylyov } 4526c21caa3SManjunath Goudar 453d193abf1SBartosz Golaszewski error = usb_add_hcd(hcd, hcd_irq, 0); 454efe7daf2SSergei Shtylyov if (error) 455644db166SJingoo Han goto err; 456efe7daf2SSergei Shtylyov 4573c9740a1SPeter Chen device_wakeup_enable(hcd->self.controller); 4583c9740a1SPeter Chen 459f3c56fb3SAxel Haslam error = ohci_da8xx_register_notify(hcd); 460f3c56fb3SAxel Haslam if (error) 461f3c56fb3SAxel Haslam goto err_remove_hcd; 462efe7daf2SSergei Shtylyov 463f3c56fb3SAxel Haslam return 0; 464f3c56fb3SAxel Haslam 465f3c56fb3SAxel Haslam err_remove_hcd: 466efe7daf2SSergei Shtylyov usb_remove_hcd(hcd); 467644db166SJingoo Han err: 468efe7daf2SSergei Shtylyov usb_put_hcd(hcd); 469efe7daf2SSergei Shtylyov return error; 470efe7daf2SSergei Shtylyov } 471efe7daf2SSergei Shtylyov 4729053f4b1SUwe Kleine-König static void ohci_da8xx_remove(struct platform_device *pdev) 473efe7daf2SSergei Shtylyov { 4746c21caa3SManjunath Goudar struct usb_hcd *hcd = platform_get_drvdata(pdev); 475efe7daf2SSergei Shtylyov 476efe7daf2SSergei Shtylyov usb_remove_hcd(hcd); 477efe7daf2SSergei Shtylyov usb_put_hcd(hcd); 478efe7daf2SSergei Shtylyov } 479efe7daf2SSergei Shtylyov 480efe7daf2SSergei Shtylyov #ifdef CONFIG_PM 481933bb1f0SMajunath Goudar static int ohci_da8xx_suspend(struct platform_device *pdev, 482933bb1f0SMajunath Goudar pm_message_t message) 483efe7daf2SSergei Shtylyov { 484933bb1f0SMajunath Goudar struct usb_hcd *hcd = platform_get_drvdata(pdev); 485efe7daf2SSergei Shtylyov struct ohci_hcd *ohci = hcd_to_ohci(hcd); 486933bb1f0SMajunath Goudar bool do_wakeup = device_may_wakeup(&pdev->dev); 487933bb1f0SMajunath Goudar int ret; 488933bb1f0SMajunath Goudar 489efe7daf2SSergei Shtylyov 490efe7daf2SSergei Shtylyov if (time_before(jiffies, ohci->next_statechange)) 491efe7daf2SSergei Shtylyov msleep(5); 492efe7daf2SSergei Shtylyov ohci->next_statechange = jiffies; 493efe7daf2SSergei Shtylyov 494933bb1f0SMajunath Goudar ret = ohci_suspend(hcd, do_wakeup); 495933bb1f0SMajunath Goudar if (ret) 496933bb1f0SMajunath Goudar return ret; 497933bb1f0SMajunath Goudar 498c7a4f9f3SAxel Haslam ohci_da8xx_disable(hcd); 499efe7daf2SSergei Shtylyov hcd->state = HC_STATE_SUSPENDED; 500933bb1f0SMajunath Goudar 501933bb1f0SMajunath Goudar return ret; 502efe7daf2SSergei Shtylyov } 503efe7daf2SSergei Shtylyov 504efe7daf2SSergei Shtylyov static int ohci_da8xx_resume(struct platform_device *dev) 505efe7daf2SSergei Shtylyov { 506efe7daf2SSergei Shtylyov struct usb_hcd *hcd = platform_get_drvdata(dev); 507efe7daf2SSergei Shtylyov struct ohci_hcd *ohci = hcd_to_ohci(hcd); 5086110c425SDavid Lechner int ret; 509efe7daf2SSergei Shtylyov 510efe7daf2SSergei Shtylyov if (time_before(jiffies, ohci->next_statechange)) 511efe7daf2SSergei Shtylyov msleep(5); 512efe7daf2SSergei Shtylyov ohci->next_statechange = jiffies; 513efe7daf2SSergei Shtylyov 514c7a4f9f3SAxel Haslam ret = ohci_da8xx_enable(hcd); 5156110c425SDavid Lechner if (ret) 5166110c425SDavid Lechner return ret; 5176110c425SDavid Lechner 518640308b7SAxel Haslam ohci_resume(hcd, false); 5196110c425SDavid Lechner 520efe7daf2SSergei Shtylyov return 0; 521efe7daf2SSergei Shtylyov } 522efe7daf2SSergei Shtylyov #endif 523efe7daf2SSergei Shtylyov 5246c21caa3SManjunath Goudar static const struct ohci_driver_overrides da8xx_overrides __initconst = { 5256c21caa3SManjunath Goudar .reset = ohci_da8xx_reset, 526c7a4f9f3SAxel Haslam .extra_priv_size = sizeof(struct da8xx_ohci_hcd), 5276c21caa3SManjunath Goudar }; 5286c21caa3SManjunath Goudar 529efe7daf2SSergei Shtylyov /* 530efe7daf2SSergei Shtylyov * Driver definition to register with platform structure. 531efe7daf2SSergei Shtylyov */ 532efe7daf2SSergei Shtylyov static struct platform_driver ohci_hcd_da8xx_driver = { 5336c21caa3SManjunath Goudar .probe = ohci_da8xx_probe, 5349053f4b1SUwe Kleine-König .remove_new = ohci_da8xx_remove, 535efe7daf2SSergei Shtylyov .shutdown = usb_hcd_platform_shutdown, 536efe7daf2SSergei Shtylyov #ifdef CONFIG_PM 537efe7daf2SSergei Shtylyov .suspend = ohci_da8xx_suspend, 538efe7daf2SSergei Shtylyov .resume = ohci_da8xx_resume, 539efe7daf2SSergei Shtylyov #endif 540efe7daf2SSergei Shtylyov .driver = { 5416c21caa3SManjunath Goudar .name = DRV_NAME, 542190534f6SAxel Haslam .of_match_table = of_match_ptr(da8xx_ohci_ids), 543efe7daf2SSergei Shtylyov }, 544efe7daf2SSergei Shtylyov }; 545ab59ac01SJan Luebbe 5466c21caa3SManjunath Goudar static int __init ohci_da8xx_init(void) 5476c21caa3SManjunath Goudar { 5486c21caa3SManjunath Goudar 5496c21caa3SManjunath Goudar if (usb_disabled()) 5506c21caa3SManjunath Goudar return -ENODEV; 5516c21caa3SManjunath Goudar 5526c21caa3SManjunath Goudar ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides); 5536c21caa3SManjunath Goudar 5546c21caa3SManjunath Goudar /* 5556c21caa3SManjunath Goudar * The Davinci da8xx HW has some unusual quirks, which require 5566c21caa3SManjunath Goudar * da8xx-specific workarounds. We override certain hc_driver 5576c21caa3SManjunath Goudar * functions here to achieve that. We explicitly do not enhance 5586c21caa3SManjunath Goudar * ohci_driver_overrides to allow this more easily, since this 5596c21caa3SManjunath Goudar * is an unusual case, and we don't want to encourage others to 5606c21caa3SManjunath Goudar * override these functions by making it too easy. 5616c21caa3SManjunath Goudar */ 5626c21caa3SManjunath Goudar 5636c21caa3SManjunath Goudar orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control; 5646c21caa3SManjunath Goudar orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data; 5656c21caa3SManjunath Goudar 5666c21caa3SManjunath Goudar ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data; 5676c21caa3SManjunath Goudar ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control; 5686c21caa3SManjunath Goudar 5696c21caa3SManjunath Goudar return platform_driver_register(&ohci_hcd_da8xx_driver); 5706c21caa3SManjunath Goudar } 5716c21caa3SManjunath Goudar module_init(ohci_da8xx_init); 5726c21caa3SManjunath Goudar 5736c21caa3SManjunath Goudar static void __exit ohci_da8xx_exit(void) 5746c21caa3SManjunath Goudar { 5756c21caa3SManjunath Goudar platform_driver_unregister(&ohci_hcd_da8xx_driver); 5766c21caa3SManjunath Goudar } 5776c21caa3SManjunath Goudar module_exit(ohci_da8xx_exit); 5786c21caa3SManjunath Goudar MODULE_DESCRIPTION(DRIVER_DESC); 5796c21caa3SManjunath Goudar MODULE_LICENSE("GPL"); 5806c21caa3SManjunath Goudar MODULE_ALIAS("platform:" DRV_NAME); 581