xref: /openbmc/linux/drivers/usb/host/ohci-da8xx.c (revision 512de1ce7bb71972e223ec179fced1945221522d)
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>
18efe7daf2SSergei Shtylyov #include <linux/platform_device.h>
196110c425SDavid Lechner #include <linux/phy/phy.h>
20ec2a0833SArnd Bergmann #include <linux/platform_data/usb-davinci.h>
21c844ff74SAxel Haslam #include <linux/regulator/consumer.h>
226c21caa3SManjunath Goudar #include <linux/usb.h>
236c21caa3SManjunath Goudar #include <linux/usb/hcd.h>
246c21caa3SManjunath Goudar #include <asm/unaligned.h>
25efe7daf2SSergei Shtylyov 
266c21caa3SManjunath Goudar #include "ohci.h"
276c21caa3SManjunath Goudar 
286c21caa3SManjunath Goudar #define DRIVER_DESC "DA8XX"
29eacae5d2SAxel Haslam #define DRV_NAME "ohci-da8xx"
306c21caa3SManjunath Goudar 
316c21caa3SManjunath Goudar static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
326c21caa3SManjunath Goudar 
336c21caa3SManjunath Goudar static int (*orig_ohci_hub_control)(struct usb_hcd  *hcd, u16 typeReq,
346c21caa3SManjunath Goudar 			u16 wValue, u16 wIndex, char *buf, u16 wLength);
356c21caa3SManjunath Goudar static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
36efe7daf2SSergei Shtylyov 
37c7a4f9f3SAxel Haslam struct da8xx_ohci_hcd {
38c844ff74SAxel Haslam 	struct usb_hcd *hcd;
39c7a4f9f3SAxel Haslam 	struct clk *usb11_clk;
40c7a4f9f3SAxel Haslam 	struct phy *usb11_phy;
41c844ff74SAxel Haslam 	struct regulator *vbus_reg;
42c844ff74SAxel Haslam 	struct notifier_block nb;
43d193abf1SBartosz Golaszewski 	struct gpio_desc *oc_gpio;
44c7a4f9f3SAxel Haslam };
45c7a4f9f3SAxel Haslam 
46c7a4f9f3SAxel Haslam #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
47efe7daf2SSergei Shtylyov 
48efe7daf2SSergei Shtylyov /* Over-current indicator change bitmask */
49efe7daf2SSergei Shtylyov static volatile u16 ocic_mask;
50efe7daf2SSergei Shtylyov 
51c7a4f9f3SAxel Haslam static int ohci_da8xx_enable(struct usb_hcd *hcd)
52efe7daf2SSergei Shtylyov {
53c7a4f9f3SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
546110c425SDavid Lechner 	int ret;
55efe7daf2SSergei Shtylyov 
56c7a4f9f3SAxel Haslam 	ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
576110c425SDavid Lechner 	if (ret)
586110c425SDavid Lechner 		return ret;
59efe7daf2SSergei Shtylyov 
60c7a4f9f3SAxel Haslam 	ret = phy_init(da8xx_ohci->usb11_phy);
616110c425SDavid Lechner 	if (ret)
626110c425SDavid Lechner 		goto err_phy_init;
63efe7daf2SSergei Shtylyov 
64c7a4f9f3SAxel Haslam 	ret = phy_power_on(da8xx_ohci->usb11_phy);
656110c425SDavid Lechner 	if (ret)
666110c425SDavid Lechner 		goto err_phy_power_on;
67efe7daf2SSergei Shtylyov 
686110c425SDavid Lechner 	return 0;
696110c425SDavid Lechner 
706110c425SDavid Lechner err_phy_power_on:
71c7a4f9f3SAxel Haslam 	phy_exit(da8xx_ohci->usb11_phy);
726110c425SDavid Lechner err_phy_init:
73c7a4f9f3SAxel Haslam 	clk_disable_unprepare(da8xx_ohci->usb11_clk);
746110c425SDavid Lechner 
756110c425SDavid Lechner 	return ret;
76efe7daf2SSergei Shtylyov }
77efe7daf2SSergei Shtylyov 
78c7a4f9f3SAxel Haslam static void ohci_da8xx_disable(struct usb_hcd *hcd)
796110c425SDavid Lechner {
80c7a4f9f3SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
81c7a4f9f3SAxel Haslam 
82c7a4f9f3SAxel Haslam 	phy_power_off(da8xx_ohci->usb11_phy);
83c7a4f9f3SAxel Haslam 	phy_exit(da8xx_ohci->usb11_phy);
84c7a4f9f3SAxel Haslam 	clk_disable_unprepare(da8xx_ohci->usb11_clk);
85efe7daf2SSergei Shtylyov }
86efe7daf2SSergei Shtylyov 
87f3c56fb3SAxel Haslam static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
88f3c56fb3SAxel Haslam {
89c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
90f3c56fb3SAxel Haslam 	struct device *dev = hcd->self.controller;
91c844ff74SAxel Haslam 	int ret;
92f3c56fb3SAxel Haslam 
93c844ff74SAxel Haslam 	if (!da8xx_ohci->vbus_reg)
94c844ff74SAxel Haslam 		return 0;
95c844ff74SAxel Haslam 
968e2f5eaeSBartosz Golaszewski 	if (on) {
97c844ff74SAxel Haslam 		ret = regulator_enable(da8xx_ohci->vbus_reg);
98c844ff74SAxel Haslam 		if (ret) {
99c844ff74SAxel Haslam 			dev_err(dev, "Failed to enable regulator: %d\n", ret);
100c844ff74SAxel Haslam 			return ret;
101c844ff74SAxel Haslam 		}
1028e2f5eaeSBartosz Golaszewski 	} else {
103c844ff74SAxel Haslam 		ret = regulator_disable(da8xx_ohci->vbus_reg);
104c844ff74SAxel Haslam 		if (ret) {
105c844ff74SAxel Haslam 			dev_err(dev, "Failed  to disable regulator: %d\n", ret);
106c844ff74SAxel Haslam 			return ret;
107c844ff74SAxel Haslam 		}
108c844ff74SAxel Haslam 	}
109c844ff74SAxel Haslam 
110f3c56fb3SAxel Haslam 	return 0;
111f3c56fb3SAxel Haslam }
112f3c56fb3SAxel Haslam 
113f3c56fb3SAxel Haslam static int ohci_da8xx_get_power(struct usb_hcd *hcd)
114f3c56fb3SAxel Haslam {
115c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
116f3c56fb3SAxel Haslam 
117c844ff74SAxel Haslam 	if (da8xx_ohci->vbus_reg)
118c844ff74SAxel Haslam 		return regulator_is_enabled(da8xx_ohci->vbus_reg);
119c844ff74SAxel Haslam 
120f3c56fb3SAxel Haslam 	return 1;
121f3c56fb3SAxel Haslam }
122f3c56fb3SAxel Haslam 
123f3c56fb3SAxel Haslam static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
124f3c56fb3SAxel Haslam {
125c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
126c844ff74SAxel Haslam 	unsigned int flags;
127c844ff74SAxel Haslam 	int ret;
128f3c56fb3SAxel Haslam 
129d193abf1SBartosz Golaszewski 	if (da8xx_ohci->oc_gpio)
130d193abf1SBartosz Golaszewski 		return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio);
131f3c56fb3SAxel Haslam 
132c844ff74SAxel Haslam 	if (!da8xx_ohci->vbus_reg)
133c844ff74SAxel Haslam 		return 0;
134c844ff74SAxel Haslam 
135c844ff74SAxel Haslam 	ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
136c844ff74SAxel Haslam 	if (ret)
137c844ff74SAxel Haslam 		return ret;
138c844ff74SAxel Haslam 
139c844ff74SAxel Haslam 	if (flags & REGULATOR_ERROR_OVER_CURRENT)
140c844ff74SAxel Haslam 		return 1;
141c844ff74SAxel Haslam 
142f3c56fb3SAxel Haslam 	return 0;
143f3c56fb3SAxel Haslam }
144f3c56fb3SAxel Haslam 
145f3c56fb3SAxel Haslam static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
146f3c56fb3SAxel Haslam {
147c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
148f3c56fb3SAxel Haslam 
149c844ff74SAxel Haslam 	if (da8xx_ohci->vbus_reg)
150c844ff74SAxel Haslam 		return 1;
151c844ff74SAxel Haslam 
152f3c56fb3SAxel Haslam 	return 0;
153f3c56fb3SAxel Haslam }
154f3c56fb3SAxel Haslam 
155f3c56fb3SAxel Haslam static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
156f3c56fb3SAxel Haslam {
157c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
158f3c56fb3SAxel Haslam 
159d193abf1SBartosz Golaszewski 	if (da8xx_ohci->oc_gpio)
160f3c56fb3SAxel Haslam 		return 1;
161f3c56fb3SAxel Haslam 
162c844ff74SAxel Haslam 	if (da8xx_ohci->vbus_reg)
163c844ff74SAxel Haslam 		return 1;
164c844ff74SAxel Haslam 
165f3c56fb3SAxel Haslam 	return 0;
166f3c56fb3SAxel Haslam }
167f3c56fb3SAxel Haslam 
168f3c56fb3SAxel Haslam static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
169f3c56fb3SAxel Haslam {
170f3c56fb3SAxel Haslam 	struct device *dev		= hcd->self.controller;
171f3c56fb3SAxel Haslam 	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
172f3c56fb3SAxel Haslam 
173f3c56fb3SAxel Haslam 	if (hub && hub->potpgt)
174f3c56fb3SAxel Haslam 		return 1;
175f3c56fb3SAxel Haslam 
176f3c56fb3SAxel Haslam 	return 0;
177f3c56fb3SAxel Haslam }
178f3c56fb3SAxel Haslam 
179c844ff74SAxel Haslam static int ohci_da8xx_regulator_event(struct notifier_block *nb,
180c844ff74SAxel Haslam 				unsigned long event, void *data)
181f3c56fb3SAxel Haslam {
182c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci =
183c844ff74SAxel Haslam 				container_of(nb, struct da8xx_ohci_hcd, nb);
184f3c56fb3SAxel Haslam 
185c844ff74SAxel Haslam 	if (event & REGULATOR_EVENT_OVER_CURRENT) {
186c844ff74SAxel Haslam 		ocic_mask |= 1 << 1;
187c844ff74SAxel Haslam 		ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
188c844ff74SAxel Haslam 	}
189f3c56fb3SAxel Haslam 
190f3c56fb3SAxel Haslam 	return 0;
191f3c56fb3SAxel Haslam }
192f3c56fb3SAxel Haslam 
193d3273301SBartosz Golaszewski static irqreturn_t ohci_da8xx_oc_thread(int irq, void *data)
194d193abf1SBartosz Golaszewski {
195d193abf1SBartosz Golaszewski 	struct da8xx_ohci_hcd *da8xx_ohci = data;
196d3273301SBartosz Golaszewski 	struct device *dev = da8xx_ohci->hcd->self.controller;
197d3273301SBartosz Golaszewski 	int ret;
198d193abf1SBartosz Golaszewski 
199*512de1ceSBartosz Golaszewski 	if (gpiod_get_value_cansleep(da8xx_ohci->oc_gpio) &&
200*512de1ceSBartosz Golaszewski 	    da8xx_ohci->vbus_reg) {
201d3273301SBartosz Golaszewski 		ret = regulator_disable(da8xx_ohci->vbus_reg);
202d3273301SBartosz Golaszewski 		if (ret)
203*512de1ceSBartosz Golaszewski 			dev_err(dev, "Failed to disable regulator: %d\n", ret);
204d3273301SBartosz Golaszewski 	}
205d193abf1SBartosz Golaszewski 
206d193abf1SBartosz Golaszewski 	return IRQ_HANDLED;
207d193abf1SBartosz Golaszewski }
208d193abf1SBartosz Golaszewski 
209c844ff74SAxel Haslam static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
210c844ff74SAxel Haslam {
211c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
212c844ff74SAxel Haslam 	struct device *dev		= hcd->self.controller;
213c844ff74SAxel Haslam 	int ret = 0;
214c844ff74SAxel Haslam 
215d193abf1SBartosz Golaszewski 	if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) {
216c844ff74SAxel Haslam 		da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
217c844ff74SAxel Haslam 		ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
218c844ff74SAxel Haslam 						&da8xx_ohci->nb);
219c844ff74SAxel Haslam 	}
220c844ff74SAxel Haslam 
221c844ff74SAxel Haslam 	if (ret)
222c844ff74SAxel Haslam 		dev_err(dev, "Failed to register notifier: %d\n", ret);
223c844ff74SAxel Haslam 
224c844ff74SAxel Haslam 	return ret;
225c844ff74SAxel Haslam }
226c844ff74SAxel Haslam 
2276c21caa3SManjunath Goudar static int ohci_da8xx_reset(struct usb_hcd *hcd)
228efe7daf2SSergei Shtylyov {
229efe7daf2SSergei Shtylyov 	struct device *dev		= hcd->self.controller;
230d4f09e28SJingoo Han 	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
231efe7daf2SSergei Shtylyov 	struct ohci_hcd	*ohci		= hcd_to_ohci(hcd);
232efe7daf2SSergei Shtylyov 	int result;
233efe7daf2SSergei Shtylyov 	u32 rh_a;
234efe7daf2SSergei Shtylyov 
235efe7daf2SSergei Shtylyov 	dev_dbg(dev, "starting USB controller\n");
236efe7daf2SSergei Shtylyov 
237c7a4f9f3SAxel Haslam 	result = ohci_da8xx_enable(hcd);
2386110c425SDavid Lechner 	if (result < 0)
2396110c425SDavid Lechner 		return result;
240efe7daf2SSergei Shtylyov 
241efe7daf2SSergei Shtylyov 	/*
242efe7daf2SSergei Shtylyov 	 * DA8xx only have 1 port connected to the pins but the HC root hub
243efe7daf2SSergei Shtylyov 	 * register A reports 2 ports, thus we'll have to override it...
244efe7daf2SSergei Shtylyov 	 */
245efe7daf2SSergei Shtylyov 	ohci->num_ports = 1;
246efe7daf2SSergei Shtylyov 
2476c21caa3SManjunath Goudar 	result = ohci_setup(hcd);
2486110c425SDavid Lechner 	if (result < 0) {
249c7a4f9f3SAxel Haslam 		ohci_da8xx_disable(hcd);
250efe7daf2SSergei Shtylyov 		return result;
2516110c425SDavid Lechner 	}
252efe7daf2SSergei Shtylyov 
253efe7daf2SSergei Shtylyov 	/*
254efe7daf2SSergei Shtylyov 	 * Since we're providing a board-specific root hub port power control
255efe7daf2SSergei Shtylyov 	 * and over-current reporting, we have to override the HC root hub A
256efe7daf2SSergei Shtylyov 	 * register's default value, so that ohci_hub_control() could return
257efe7daf2SSergei Shtylyov 	 * the correct hub descriptor...
258efe7daf2SSergei Shtylyov 	 */
259efe7daf2SSergei Shtylyov 	rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
260f3c56fb3SAxel Haslam 	if (ohci_da8xx_has_set_power(hcd)) {
261efe7daf2SSergei Shtylyov 		rh_a &= ~RH_A_NPS;
262efe7daf2SSergei Shtylyov 		rh_a |=  RH_A_PSM;
263efe7daf2SSergei Shtylyov 	}
264f3c56fb3SAxel Haslam 	if (ohci_da8xx_has_oci(hcd)) {
265efe7daf2SSergei Shtylyov 		rh_a &= ~RH_A_NOCP;
266efe7daf2SSergei Shtylyov 		rh_a |=  RH_A_OCPM;
267efe7daf2SSergei Shtylyov 	}
268f3c56fb3SAxel Haslam 	if (ohci_da8xx_has_potpgt(hcd)) {
269efe7daf2SSergei Shtylyov 		rh_a &= ~RH_A_POTPGT;
270efe7daf2SSergei Shtylyov 		rh_a |= hub->potpgt << 24;
271f3c56fb3SAxel Haslam 	}
272efe7daf2SSergei Shtylyov 	ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
273efe7daf2SSergei Shtylyov 
274efe7daf2SSergei Shtylyov 	return result;
275efe7daf2SSergei Shtylyov }
276efe7daf2SSergei Shtylyov 
277efe7daf2SSergei Shtylyov /*
278efe7daf2SSergei Shtylyov  * Update the status data from the hub with the over-current indicator change.
279efe7daf2SSergei Shtylyov  */
280efe7daf2SSergei Shtylyov static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
281efe7daf2SSergei Shtylyov {
2826c21caa3SManjunath Goudar 	int length		= orig_ohci_hub_status_data(hcd, buf);
283efe7daf2SSergei Shtylyov 
284efe7daf2SSergei Shtylyov 	/* See if we have OCIC bit set on port 1 */
285efe7daf2SSergei Shtylyov 	if (ocic_mask & (1 << 1)) {
286efe7daf2SSergei Shtylyov 		dev_dbg(hcd->self.controller, "over-current indicator change "
287efe7daf2SSergei Shtylyov 			"on port 1\n");
288efe7daf2SSergei Shtylyov 
289efe7daf2SSergei Shtylyov 		if (!length)
290efe7daf2SSergei Shtylyov 			length = 1;
291efe7daf2SSergei Shtylyov 
292efe7daf2SSergei Shtylyov 		buf[0] |= 1 << 1;
293efe7daf2SSergei Shtylyov 	}
294efe7daf2SSergei Shtylyov 	return length;
295efe7daf2SSergei Shtylyov }
296efe7daf2SSergei Shtylyov 
297efe7daf2SSergei Shtylyov /*
298efe7daf2SSergei Shtylyov  * Look at the control requests to the root hub and see if we need to override.
299efe7daf2SSergei Shtylyov  */
300efe7daf2SSergei Shtylyov static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
301efe7daf2SSergei Shtylyov 				  u16 wIndex, char *buf, u16 wLength)
302efe7daf2SSergei Shtylyov {
303efe7daf2SSergei Shtylyov 	struct device *dev		= hcd->self.controller;
304efe7daf2SSergei Shtylyov 	int temp;
305efe7daf2SSergei Shtylyov 
306efe7daf2SSergei Shtylyov 	switch (typeReq) {
307efe7daf2SSergei Shtylyov 	case GetPortStatus:
308efe7daf2SSergei Shtylyov 		/* Check the port number */
309efe7daf2SSergei Shtylyov 		if (wIndex != 1)
310efe7daf2SSergei Shtylyov 			break;
311efe7daf2SSergei Shtylyov 
312efe7daf2SSergei Shtylyov 		dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
313efe7daf2SSergei Shtylyov 
314efe7daf2SSergei Shtylyov 		temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
315efe7daf2SSergei Shtylyov 
316efe7daf2SSergei Shtylyov 		/* The port power status (PPS) bit defaults to 1 */
317f3c56fb3SAxel Haslam 		if (!ohci_da8xx_get_power(hcd))
318efe7daf2SSergei Shtylyov 			temp &= ~RH_PS_PPS;
319efe7daf2SSergei Shtylyov 
320efe7daf2SSergei Shtylyov 		/* The port over-current indicator (POCI) bit is always 0 */
321f3c56fb3SAxel Haslam 		if (ohci_da8xx_get_oci(hcd) > 0)
322efe7daf2SSergei Shtylyov 			temp |=  RH_PS_POCI;
323efe7daf2SSergei Shtylyov 
324efe7daf2SSergei Shtylyov 		/* The over-current indicator change (OCIC) bit is 0 too */
325efe7daf2SSergei Shtylyov 		if (ocic_mask & (1 << wIndex))
326efe7daf2SSergei Shtylyov 			temp |=  RH_PS_OCIC;
327efe7daf2SSergei Shtylyov 
328efe7daf2SSergei Shtylyov 		put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
329efe7daf2SSergei Shtylyov 		return 0;
330efe7daf2SSergei Shtylyov 	case SetPortFeature:
331efe7daf2SSergei Shtylyov 		temp = 1;
332efe7daf2SSergei Shtylyov 		goto check_port;
333efe7daf2SSergei Shtylyov 	case ClearPortFeature:
334efe7daf2SSergei Shtylyov 		temp = 0;
335efe7daf2SSergei Shtylyov 
336efe7daf2SSergei Shtylyov check_port:
337efe7daf2SSergei Shtylyov 		/* Check the port number */
338efe7daf2SSergei Shtylyov 		if (wIndex != 1)
339efe7daf2SSergei Shtylyov 			break;
340efe7daf2SSergei Shtylyov 
341efe7daf2SSergei Shtylyov 		switch (wValue) {
342efe7daf2SSergei Shtylyov 		case USB_PORT_FEAT_POWER:
343efe7daf2SSergei Shtylyov 			dev_dbg(dev, "%sPortFeature(%u): %s\n",
344efe7daf2SSergei Shtylyov 				temp ? "Set" : "Clear", wIndex, "POWER");
345efe7daf2SSergei Shtylyov 
346f3c56fb3SAxel Haslam 			return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
347efe7daf2SSergei Shtylyov 		case USB_PORT_FEAT_C_OVER_CURRENT:
348efe7daf2SSergei Shtylyov 			dev_dbg(dev, "%sPortFeature(%u): %s\n",
349efe7daf2SSergei Shtylyov 				temp ? "Set" : "Clear", wIndex,
350efe7daf2SSergei Shtylyov 				"C_OVER_CURRENT");
351efe7daf2SSergei Shtylyov 
352efe7daf2SSergei Shtylyov 			if (temp)
353efe7daf2SSergei Shtylyov 				ocic_mask |= 1 << wIndex;
354efe7daf2SSergei Shtylyov 			else
355efe7daf2SSergei Shtylyov 				ocic_mask &= ~(1 << wIndex);
356efe7daf2SSergei Shtylyov 			return 0;
357efe7daf2SSergei Shtylyov 		}
358efe7daf2SSergei Shtylyov 	}
359efe7daf2SSergei Shtylyov 
3606c21caa3SManjunath Goudar 	return orig_ohci_hub_control(hcd, typeReq, wValue,
3616c21caa3SManjunath Goudar 			wIndex, buf, wLength);
362efe7daf2SSergei Shtylyov }
363efe7daf2SSergei Shtylyov 
364efe7daf2SSergei Shtylyov /*-------------------------------------------------------------------------*/
365190534f6SAxel Haslam #ifdef CONFIG_OF
366190534f6SAxel Haslam static const struct of_device_id da8xx_ohci_ids[] = {
367190534f6SAxel Haslam 	{ .compatible = "ti,da830-ohci" },
368190534f6SAxel Haslam 	{ }
369190534f6SAxel Haslam };
370190534f6SAxel Haslam MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
371190534f6SAxel Haslam #endif
372efe7daf2SSergei Shtylyov 
3736c21caa3SManjunath Goudar static int ohci_da8xx_probe(struct platform_device *pdev)
374efe7daf2SSergei Shtylyov {
375c7a4f9f3SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci;
3763d2ab9f3SBartosz Golaszewski 	struct device *dev = &pdev->dev;
377d193abf1SBartosz Golaszewski 	int error, hcd_irq, oc_irq;
378efe7daf2SSergei Shtylyov 	struct usb_hcd	*hcd;
379efe7daf2SSergei Shtylyov 	struct resource *mem;
38008e46f18SBartosz Golaszewski 
3813d2ab9f3SBartosz Golaszewski 	hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev));
382644db166SJingoo Han 	if (!hcd)
383644db166SJingoo Han 		return -ENOMEM;
384efe7daf2SSergei Shtylyov 
385c7a4f9f3SAxel Haslam 	da8xx_ohci = to_da8xx_ohci(hcd);
386c844ff74SAxel Haslam 	da8xx_ohci->hcd = hcd;
387c7a4f9f3SAxel Haslam 
3883d2ab9f3SBartosz Golaszewski 	da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL);
389c7a4f9f3SAxel Haslam 	if (IS_ERR(da8xx_ohci->usb11_clk)) {
390c7a4f9f3SAxel Haslam 		error = PTR_ERR(da8xx_ohci->usb11_clk);
391c7a4f9f3SAxel Haslam 		if (error != -EPROBE_DEFER)
3923d2ab9f3SBartosz Golaszewski 			dev_err(dev, "Failed to get clock.\n");
393c7a4f9f3SAxel Haslam 		goto err;
394c7a4f9f3SAxel Haslam 	}
395c7a4f9f3SAxel Haslam 
3963d2ab9f3SBartosz Golaszewski 	da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy");
397c7a4f9f3SAxel Haslam 	if (IS_ERR(da8xx_ohci->usb11_phy)) {
398c7a4f9f3SAxel Haslam 		error = PTR_ERR(da8xx_ohci->usb11_phy);
399c7a4f9f3SAxel Haslam 		if (error != -EPROBE_DEFER)
4003d2ab9f3SBartosz Golaszewski 			dev_err(dev, "Failed to get phy.\n");
401c7a4f9f3SAxel Haslam 		goto err;
402c7a4f9f3SAxel Haslam 	}
403c7a4f9f3SAxel Haslam 
4043d2ab9f3SBartosz Golaszewski 	da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus");
405c844ff74SAxel Haslam 	if (IS_ERR(da8xx_ohci->vbus_reg)) {
406c844ff74SAxel Haslam 		error = PTR_ERR(da8xx_ohci->vbus_reg);
407c844ff74SAxel Haslam 		if (error == -ENODEV) {
408c844ff74SAxel Haslam 			da8xx_ohci->vbus_reg = NULL;
409c844ff74SAxel Haslam 		} else if (error == -EPROBE_DEFER) {
410c844ff74SAxel Haslam 			goto err;
411c844ff74SAxel Haslam 		} else {
4123d2ab9f3SBartosz Golaszewski 			dev_err(dev, "Failed to get regulator\n");
413c844ff74SAxel Haslam 			goto err;
414c844ff74SAxel Haslam 		}
415c844ff74SAxel Haslam 	}
416c844ff74SAxel Haslam 
417d193abf1SBartosz Golaszewski 	da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN);
418d193abf1SBartosz Golaszewski 	if (IS_ERR(da8xx_ohci->oc_gpio))
419d193abf1SBartosz Golaszewski 		goto err;
420d193abf1SBartosz Golaszewski 
421d193abf1SBartosz Golaszewski 	if (da8xx_ohci->oc_gpio) {
422d193abf1SBartosz Golaszewski 		oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio);
423d193abf1SBartosz Golaszewski 		if (oc_irq < 0)
424d193abf1SBartosz Golaszewski 			goto err;
425d193abf1SBartosz Golaszewski 
426d3273301SBartosz Golaszewski 		error = devm_request_threaded_irq(dev, oc_irq, NULL,
427d3273301SBartosz Golaszewski 				ohci_da8xx_oc_thread, IRQF_TRIGGER_RISING |
428d3273301SBartosz Golaszewski 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
429d193abf1SBartosz Golaszewski 				"OHCI over-current indicator", da8xx_ohci);
430d193abf1SBartosz Golaszewski 		if (error)
431d193abf1SBartosz Golaszewski 			goto err;
432d193abf1SBartosz Golaszewski 	}
433d193abf1SBartosz Golaszewski 
434efe7daf2SSergei Shtylyov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4353d2ab9f3SBartosz Golaszewski 	hcd->regs = devm_ioremap_resource(dev, mem);
436644db166SJingoo Han 	if (IS_ERR(hcd->regs)) {
437644db166SJingoo Han 		error = PTR_ERR(hcd->regs);
438644db166SJingoo Han 		goto err;
439efe7daf2SSergei Shtylyov 	}
44054891d74SVarka Bhadram 	hcd->rsrc_start = mem->start;
44154891d74SVarka Bhadram 	hcd->rsrc_len = resource_size(mem);
442efe7daf2SSergei Shtylyov 
443d193abf1SBartosz Golaszewski 	hcd_irq = platform_get_irq(pdev, 0);
444d193abf1SBartosz Golaszewski 	if (hcd_irq < 0) {
445efe7daf2SSergei Shtylyov 		error = -ENODEV;
446644db166SJingoo Han 		goto err;
447efe7daf2SSergei Shtylyov 	}
4486c21caa3SManjunath Goudar 
449d193abf1SBartosz Golaszewski 	error = usb_add_hcd(hcd, hcd_irq, 0);
450efe7daf2SSergei Shtylyov 	if (error)
451644db166SJingoo Han 		goto err;
452efe7daf2SSergei Shtylyov 
4533c9740a1SPeter Chen 	device_wakeup_enable(hcd->self.controller);
4543c9740a1SPeter Chen 
455f3c56fb3SAxel Haslam 	error = ohci_da8xx_register_notify(hcd);
456f3c56fb3SAxel Haslam 	if (error)
457f3c56fb3SAxel Haslam 		goto err_remove_hcd;
458efe7daf2SSergei Shtylyov 
459f3c56fb3SAxel Haslam 	return 0;
460f3c56fb3SAxel Haslam 
461f3c56fb3SAxel Haslam err_remove_hcd:
462efe7daf2SSergei Shtylyov 	usb_remove_hcd(hcd);
463644db166SJingoo Han err:
464efe7daf2SSergei Shtylyov 	usb_put_hcd(hcd);
465efe7daf2SSergei Shtylyov 	return error;
466efe7daf2SSergei Shtylyov }
467efe7daf2SSergei Shtylyov 
4686c21caa3SManjunath Goudar static int ohci_da8xx_remove(struct platform_device *pdev)
469efe7daf2SSergei Shtylyov {
4706c21caa3SManjunath Goudar 	struct usb_hcd	*hcd = platform_get_drvdata(pdev);
471efe7daf2SSergei Shtylyov 
472efe7daf2SSergei Shtylyov 	usb_remove_hcd(hcd);
473efe7daf2SSergei Shtylyov 	usb_put_hcd(hcd);
474efe7daf2SSergei Shtylyov 
475efe7daf2SSergei Shtylyov 	return 0;
476efe7daf2SSergei Shtylyov }
477efe7daf2SSergei Shtylyov 
478efe7daf2SSergei Shtylyov #ifdef CONFIG_PM
479933bb1f0SMajunath Goudar static int ohci_da8xx_suspend(struct platform_device *pdev,
480933bb1f0SMajunath Goudar 				pm_message_t message)
481efe7daf2SSergei Shtylyov {
482933bb1f0SMajunath Goudar 	struct usb_hcd	*hcd	= platform_get_drvdata(pdev);
483efe7daf2SSergei Shtylyov 	struct ohci_hcd	*ohci	= hcd_to_ohci(hcd);
484933bb1f0SMajunath Goudar 	bool		do_wakeup	= device_may_wakeup(&pdev->dev);
485933bb1f0SMajunath Goudar 	int		ret;
486933bb1f0SMajunath Goudar 
487efe7daf2SSergei Shtylyov 
488efe7daf2SSergei Shtylyov 	if (time_before(jiffies, ohci->next_statechange))
489efe7daf2SSergei Shtylyov 		msleep(5);
490efe7daf2SSergei Shtylyov 	ohci->next_statechange = jiffies;
491efe7daf2SSergei Shtylyov 
492933bb1f0SMajunath Goudar 	ret = ohci_suspend(hcd, do_wakeup);
493933bb1f0SMajunath Goudar 	if (ret)
494933bb1f0SMajunath Goudar 		return ret;
495933bb1f0SMajunath Goudar 
496c7a4f9f3SAxel Haslam 	ohci_da8xx_disable(hcd);
497efe7daf2SSergei Shtylyov 	hcd->state = HC_STATE_SUSPENDED;
498933bb1f0SMajunath Goudar 
499933bb1f0SMajunath Goudar 	return ret;
500efe7daf2SSergei Shtylyov }
501efe7daf2SSergei Shtylyov 
502efe7daf2SSergei Shtylyov static int ohci_da8xx_resume(struct platform_device *dev)
503efe7daf2SSergei Shtylyov {
504efe7daf2SSergei Shtylyov 	struct usb_hcd	*hcd	= platform_get_drvdata(dev);
505efe7daf2SSergei Shtylyov 	struct ohci_hcd	*ohci	= hcd_to_ohci(hcd);
5066110c425SDavid Lechner 	int ret;
507efe7daf2SSergei Shtylyov 
508efe7daf2SSergei Shtylyov 	if (time_before(jiffies, ohci->next_statechange))
509efe7daf2SSergei Shtylyov 		msleep(5);
510efe7daf2SSergei Shtylyov 	ohci->next_statechange = jiffies;
511efe7daf2SSergei Shtylyov 
512c7a4f9f3SAxel Haslam 	ret = ohci_da8xx_enable(hcd);
5136110c425SDavid Lechner 	if (ret)
5146110c425SDavid Lechner 		return ret;
5156110c425SDavid Lechner 
516640308b7SAxel Haslam 	ohci_resume(hcd, false);
5176110c425SDavid Lechner 
518efe7daf2SSergei Shtylyov 	return 0;
519efe7daf2SSergei Shtylyov }
520efe7daf2SSergei Shtylyov #endif
521efe7daf2SSergei Shtylyov 
5226c21caa3SManjunath Goudar static const struct ohci_driver_overrides da8xx_overrides __initconst = {
5236c21caa3SManjunath Goudar 	.reset		 = ohci_da8xx_reset,
524c7a4f9f3SAxel Haslam 	.extra_priv_size = sizeof(struct da8xx_ohci_hcd),
5256c21caa3SManjunath Goudar };
5266c21caa3SManjunath Goudar 
527efe7daf2SSergei Shtylyov /*
528efe7daf2SSergei Shtylyov  * Driver definition to register with platform structure.
529efe7daf2SSergei Shtylyov  */
530efe7daf2SSergei Shtylyov static struct platform_driver ohci_hcd_da8xx_driver = {
5316c21caa3SManjunath Goudar 	.probe		= ohci_da8xx_probe,
5326c21caa3SManjunath Goudar 	.remove		= ohci_da8xx_remove,
533efe7daf2SSergei Shtylyov 	.shutdown 	= usb_hcd_platform_shutdown,
534efe7daf2SSergei Shtylyov #ifdef	CONFIG_PM
535efe7daf2SSergei Shtylyov 	.suspend	= ohci_da8xx_suspend,
536efe7daf2SSergei Shtylyov 	.resume		= ohci_da8xx_resume,
537efe7daf2SSergei Shtylyov #endif
538efe7daf2SSergei Shtylyov 	.driver		= {
5396c21caa3SManjunath Goudar 		.name	= DRV_NAME,
540190534f6SAxel Haslam 		.of_match_table = of_match_ptr(da8xx_ohci_ids),
541efe7daf2SSergei Shtylyov 	},
542efe7daf2SSergei Shtylyov };
543ab59ac01SJan Luebbe 
5446c21caa3SManjunath Goudar static int __init ohci_da8xx_init(void)
5456c21caa3SManjunath Goudar {
5466c21caa3SManjunath Goudar 
5476c21caa3SManjunath Goudar 	if (usb_disabled())
5486c21caa3SManjunath Goudar 		return -ENODEV;
5496c21caa3SManjunath Goudar 
5506c21caa3SManjunath Goudar 	pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
5516c21caa3SManjunath Goudar 	ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
5526c21caa3SManjunath Goudar 
5536c21caa3SManjunath Goudar 	/*
5546c21caa3SManjunath Goudar 	 * The Davinci da8xx HW has some unusual quirks, which require
5556c21caa3SManjunath Goudar 	 * da8xx-specific workarounds. We override certain hc_driver
5566c21caa3SManjunath Goudar 	 * functions here to achieve that. We explicitly do not enhance
5576c21caa3SManjunath Goudar 	 * ohci_driver_overrides to allow this more easily, since this
5586c21caa3SManjunath Goudar 	 * is an unusual case, and we don't want to encourage others to
5596c21caa3SManjunath Goudar 	 * override these functions by making it too easy.
5606c21caa3SManjunath Goudar 	 */
5616c21caa3SManjunath Goudar 
5626c21caa3SManjunath Goudar 	orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
5636c21caa3SManjunath Goudar 	orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
5646c21caa3SManjunath Goudar 
5656c21caa3SManjunath Goudar 	ohci_da8xx_hc_driver.hub_status_data     = ohci_da8xx_hub_status_data;
5666c21caa3SManjunath Goudar 	ohci_da8xx_hc_driver.hub_control         = ohci_da8xx_hub_control;
5676c21caa3SManjunath Goudar 
5686c21caa3SManjunath Goudar 	return platform_driver_register(&ohci_hcd_da8xx_driver);
5696c21caa3SManjunath Goudar }
5706c21caa3SManjunath Goudar module_init(ohci_da8xx_init);
5716c21caa3SManjunath Goudar 
5726c21caa3SManjunath Goudar static void __exit ohci_da8xx_exit(void)
5736c21caa3SManjunath Goudar {
5746c21caa3SManjunath Goudar 	platform_driver_unregister(&ohci_hcd_da8xx_driver);
5756c21caa3SManjunath Goudar }
5766c21caa3SManjunath Goudar module_exit(ohci_da8xx_exit);
5776c21caa3SManjunath Goudar MODULE_DESCRIPTION(DRIVER_DESC);
5786c21caa3SManjunath Goudar MODULE_LICENSE("GPL");
5796c21caa3SManjunath Goudar MODULE_ALIAS("platform:" DRV_NAME);
580