staging: vboxvideo: Add vbox_bo_k[un]map helper functions
[linux/fpc-iii.git] / drivers / usb / host / ohci-da8xx.c
bloba55cbba40a5abc55a41249ad08c6cdb0d0c256df
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * OHCI HCD (Host Controller Driver) for USB.
5 * TI DA8xx (OMAP-L1x) Bus Glue
7 * Derived from: ohci-omap.c and ohci-s3c2410.c
8 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
9 */
11 #include <linux/clk.h>
12 #include <linux/io.h>
13 #include <linux/interrupt.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_data/usb-davinci.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/usb.h>
22 #include <linux/usb/hcd.h>
23 #include <asm/unaligned.h>
25 #include "ohci.h"
27 #define DRIVER_DESC "DA8XX"
28 #define DRV_NAME "ohci-da8xx"
30 static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
32 static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
33 u16 wValue, u16 wIndex, char *buf, u16 wLength);
34 static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
36 struct da8xx_ohci_hcd {
37 struct usb_hcd *hcd;
38 struct clk *usb11_clk;
39 struct phy *usb11_phy;
40 struct regulator *vbus_reg;
41 struct notifier_block nb;
42 unsigned int reg_enabled;
45 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
47 /* Over-current indicator change bitmask */
48 static volatile u16 ocic_mask;
50 static int ohci_da8xx_enable(struct usb_hcd *hcd)
52 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
53 int ret;
55 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
56 if (ret)
57 return ret;
59 ret = phy_init(da8xx_ohci->usb11_phy);
60 if (ret)
61 goto err_phy_init;
63 ret = phy_power_on(da8xx_ohci->usb11_phy);
64 if (ret)
65 goto err_phy_power_on;
67 return 0;
69 err_phy_power_on:
70 phy_exit(da8xx_ohci->usb11_phy);
71 err_phy_init:
72 clk_disable_unprepare(da8xx_ohci->usb11_clk);
74 return ret;
77 static void ohci_da8xx_disable(struct usb_hcd *hcd)
79 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
81 phy_power_off(da8xx_ohci->usb11_phy);
82 phy_exit(da8xx_ohci->usb11_phy);
83 clk_disable_unprepare(da8xx_ohci->usb11_clk);
86 static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
88 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
89 struct device *dev = hcd->self.controller;
90 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
91 int ret;
93 if (hub && hub->set_power)
94 return hub->set_power(1, on);
96 if (!da8xx_ohci->vbus_reg)
97 return 0;
99 if (on && !da8xx_ohci->reg_enabled) {
100 ret = regulator_enable(da8xx_ohci->vbus_reg);
101 if (ret) {
102 dev_err(dev, "Failed to enable regulator: %d\n", ret);
103 return ret;
105 da8xx_ohci->reg_enabled = 1;
107 } else if (!on && da8xx_ohci->reg_enabled) {
108 ret = regulator_disable(da8xx_ohci->vbus_reg);
109 if (ret) {
110 dev_err(dev, "Failed to disable regulator: %d\n", ret);
111 return ret;
113 da8xx_ohci->reg_enabled = 0;
116 return 0;
119 static int ohci_da8xx_get_power(struct usb_hcd *hcd)
121 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
122 struct device *dev = hcd->self.controller;
123 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
125 if (hub && hub->get_power)
126 return hub->get_power(1);
128 if (da8xx_ohci->vbus_reg)
129 return regulator_is_enabled(da8xx_ohci->vbus_reg);
131 return 1;
134 static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
136 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
137 struct device *dev = hcd->self.controller;
138 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
139 unsigned int flags;
140 int ret;
142 if (hub && hub->get_oci)
143 return hub->get_oci(1);
145 if (!da8xx_ohci->vbus_reg)
146 return 0;
148 ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
149 if (ret)
150 return ret;
152 if (flags & REGULATOR_ERROR_OVER_CURRENT)
153 return 1;
155 return 0;
158 static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
160 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
161 struct device *dev = hcd->self.controller;
162 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
164 if (hub && hub->set_power)
165 return 1;
167 if (da8xx_ohci->vbus_reg)
168 return 1;
170 return 0;
173 static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
175 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
176 struct device *dev = hcd->self.controller;
177 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
179 if (hub && hub->get_oci)
180 return 1;
182 if (da8xx_ohci->vbus_reg)
183 return 1;
185 return 0;
188 static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
190 struct device *dev = hcd->self.controller;
191 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
193 if (hub && hub->potpgt)
194 return 1;
196 return 0;
200 * Handle the port over-current indicator change.
202 static void ohci_da8xx_ocic_handler(struct da8xx_ohci_root_hub *hub,
203 unsigned port)
205 ocic_mask |= 1 << port;
207 /* Once over-current is detected, the port needs to be powered down */
208 if (hub->get_oci(port) > 0)
209 hub->set_power(port, 0);
212 static int ohci_da8xx_regulator_event(struct notifier_block *nb,
213 unsigned long event, void *data)
215 struct da8xx_ohci_hcd *da8xx_ohci =
216 container_of(nb, struct da8xx_ohci_hcd, nb);
218 if (event & REGULATOR_EVENT_OVER_CURRENT) {
219 ocic_mask |= 1 << 1;
220 ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
223 return 0;
226 static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
228 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
229 struct device *dev = hcd->self.controller;
230 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
231 int ret = 0;
233 if (hub && hub->ocic_notify) {
234 ret = hub->ocic_notify(ohci_da8xx_ocic_handler);
235 } else if (da8xx_ohci->vbus_reg) {
236 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
237 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
238 &da8xx_ohci->nb);
241 if (ret)
242 dev_err(dev, "Failed to register notifier: %d\n", ret);
244 return ret;
247 static void ohci_da8xx_unregister_notify(struct usb_hcd *hcd)
249 struct device *dev = hcd->self.controller;
250 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
252 if (hub && hub->ocic_notify)
253 hub->ocic_notify(NULL);
256 static int ohci_da8xx_reset(struct usb_hcd *hcd)
258 struct device *dev = hcd->self.controller;
259 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
260 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
261 int result;
262 u32 rh_a;
264 dev_dbg(dev, "starting USB controller\n");
266 result = ohci_da8xx_enable(hcd);
267 if (result < 0)
268 return result;
271 * DA8xx only have 1 port connected to the pins but the HC root hub
272 * register A reports 2 ports, thus we'll have to override it...
274 ohci->num_ports = 1;
276 result = ohci_setup(hcd);
277 if (result < 0) {
278 ohci_da8xx_disable(hcd);
279 return result;
283 * Since we're providing a board-specific root hub port power control
284 * and over-current reporting, we have to override the HC root hub A
285 * register's default value, so that ohci_hub_control() could return
286 * the correct hub descriptor...
288 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
289 if (ohci_da8xx_has_set_power(hcd)) {
290 rh_a &= ~RH_A_NPS;
291 rh_a |= RH_A_PSM;
293 if (ohci_da8xx_has_oci(hcd)) {
294 rh_a &= ~RH_A_NOCP;
295 rh_a |= RH_A_OCPM;
297 if (ohci_da8xx_has_potpgt(hcd)) {
298 rh_a &= ~RH_A_POTPGT;
299 rh_a |= hub->potpgt << 24;
301 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
303 return result;
307 * Update the status data from the hub with the over-current indicator change.
309 static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
311 int length = orig_ohci_hub_status_data(hcd, buf);
313 /* See if we have OCIC bit set on port 1 */
314 if (ocic_mask & (1 << 1)) {
315 dev_dbg(hcd->self.controller, "over-current indicator change "
316 "on port 1\n");
318 if (!length)
319 length = 1;
321 buf[0] |= 1 << 1;
323 return length;
327 * Look at the control requests to the root hub and see if we need to override.
329 static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
330 u16 wIndex, char *buf, u16 wLength)
332 struct device *dev = hcd->self.controller;
333 int temp;
335 switch (typeReq) {
336 case GetPortStatus:
337 /* Check the port number */
338 if (wIndex != 1)
339 break;
341 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
343 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
345 /* The port power status (PPS) bit defaults to 1 */
346 if (!ohci_da8xx_get_power(hcd))
347 temp &= ~RH_PS_PPS;
349 /* The port over-current indicator (POCI) bit is always 0 */
350 if (ohci_da8xx_get_oci(hcd) > 0)
351 temp |= RH_PS_POCI;
353 /* The over-current indicator change (OCIC) bit is 0 too */
354 if (ocic_mask & (1 << wIndex))
355 temp |= RH_PS_OCIC;
357 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
358 return 0;
359 case SetPortFeature:
360 temp = 1;
361 goto check_port;
362 case ClearPortFeature:
363 temp = 0;
365 check_port:
366 /* Check the port number */
367 if (wIndex != 1)
368 break;
370 switch (wValue) {
371 case USB_PORT_FEAT_POWER:
372 dev_dbg(dev, "%sPortFeature(%u): %s\n",
373 temp ? "Set" : "Clear", wIndex, "POWER");
375 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
376 case USB_PORT_FEAT_C_OVER_CURRENT:
377 dev_dbg(dev, "%sPortFeature(%u): %s\n",
378 temp ? "Set" : "Clear", wIndex,
379 "C_OVER_CURRENT");
381 if (temp)
382 ocic_mask |= 1 << wIndex;
383 else
384 ocic_mask &= ~(1 << wIndex);
385 return 0;
389 return orig_ohci_hub_control(hcd, typeReq, wValue,
390 wIndex, buf, wLength);
393 /*-------------------------------------------------------------------------*/
394 #ifdef CONFIG_OF
395 static const struct of_device_id da8xx_ohci_ids[] = {
396 { .compatible = "ti,da830-ohci" },
399 MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
400 #endif
402 static int ohci_da8xx_probe(struct platform_device *pdev)
404 struct da8xx_ohci_hcd *da8xx_ohci;
405 struct usb_hcd *hcd;
406 struct resource *mem;
407 int error, irq;
408 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, &pdev->dev,
409 dev_name(&pdev->dev));
410 if (!hcd)
411 return -ENOMEM;
413 da8xx_ohci = to_da8xx_ohci(hcd);
414 da8xx_ohci->hcd = hcd;
416 da8xx_ohci->usb11_clk = devm_clk_get(&pdev->dev, NULL);
417 if (IS_ERR(da8xx_ohci->usb11_clk)) {
418 error = PTR_ERR(da8xx_ohci->usb11_clk);
419 if (error != -EPROBE_DEFER)
420 dev_err(&pdev->dev, "Failed to get clock.\n");
421 goto err;
424 da8xx_ohci->usb11_phy = devm_phy_get(&pdev->dev, "usb-phy");
425 if (IS_ERR(da8xx_ohci->usb11_phy)) {
426 error = PTR_ERR(da8xx_ohci->usb11_phy);
427 if (error != -EPROBE_DEFER)
428 dev_err(&pdev->dev, "Failed to get phy.\n");
429 goto err;
432 da8xx_ohci->vbus_reg = devm_regulator_get_optional(&pdev->dev, "vbus");
433 if (IS_ERR(da8xx_ohci->vbus_reg)) {
434 error = PTR_ERR(da8xx_ohci->vbus_reg);
435 if (error == -ENODEV) {
436 da8xx_ohci->vbus_reg = NULL;
437 } else if (error == -EPROBE_DEFER) {
438 goto err;
439 } else {
440 dev_err(&pdev->dev, "Failed to get regulator\n");
441 goto err;
445 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
446 hcd->regs = devm_ioremap_resource(&pdev->dev, mem);
447 if (IS_ERR(hcd->regs)) {
448 error = PTR_ERR(hcd->regs);
449 goto err;
451 hcd->rsrc_start = mem->start;
452 hcd->rsrc_len = resource_size(mem);
454 irq = platform_get_irq(pdev, 0);
455 if (irq < 0) {
456 error = -ENODEV;
457 goto err;
460 error = usb_add_hcd(hcd, irq, 0);
461 if (error)
462 goto err;
464 device_wakeup_enable(hcd->self.controller);
466 error = ohci_da8xx_register_notify(hcd);
467 if (error)
468 goto err_remove_hcd;
470 return 0;
472 err_remove_hcd:
473 usb_remove_hcd(hcd);
474 err:
475 usb_put_hcd(hcd);
476 return error;
479 static int ohci_da8xx_remove(struct platform_device *pdev)
481 struct usb_hcd *hcd = platform_get_drvdata(pdev);
483 ohci_da8xx_unregister_notify(hcd);
484 usb_remove_hcd(hcd);
485 usb_put_hcd(hcd);
487 return 0;
490 #ifdef CONFIG_PM
491 static int ohci_da8xx_suspend(struct platform_device *pdev,
492 pm_message_t message)
494 struct usb_hcd *hcd = platform_get_drvdata(pdev);
495 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
496 bool do_wakeup = device_may_wakeup(&pdev->dev);
497 int ret;
500 if (time_before(jiffies, ohci->next_statechange))
501 msleep(5);
502 ohci->next_statechange = jiffies;
504 ret = ohci_suspend(hcd, do_wakeup);
505 if (ret)
506 return ret;
508 ohci_da8xx_disable(hcd);
509 hcd->state = HC_STATE_SUSPENDED;
511 return ret;
514 static int ohci_da8xx_resume(struct platform_device *dev)
516 struct usb_hcd *hcd = platform_get_drvdata(dev);
517 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
518 int ret;
520 if (time_before(jiffies, ohci->next_statechange))
521 msleep(5);
522 ohci->next_statechange = jiffies;
524 ret = ohci_da8xx_enable(hcd);
525 if (ret)
526 return ret;
528 ohci_resume(hcd, false);
530 return 0;
532 #endif
534 static const struct ohci_driver_overrides da8xx_overrides __initconst = {
535 .reset = ohci_da8xx_reset,
536 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
540 * Driver definition to register with platform structure.
542 static struct platform_driver ohci_hcd_da8xx_driver = {
543 .probe = ohci_da8xx_probe,
544 .remove = ohci_da8xx_remove,
545 .shutdown = usb_hcd_platform_shutdown,
546 #ifdef CONFIG_PM
547 .suspend = ohci_da8xx_suspend,
548 .resume = ohci_da8xx_resume,
549 #endif
550 .driver = {
551 .name = DRV_NAME,
552 .of_match_table = of_match_ptr(da8xx_ohci_ids),
556 static int __init ohci_da8xx_init(void)
559 if (usb_disabled())
560 return -ENODEV;
562 pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
563 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
566 * The Davinci da8xx HW has some unusual quirks, which require
567 * da8xx-specific workarounds. We override certain hc_driver
568 * functions here to achieve that. We explicitly do not enhance
569 * ohci_driver_overrides to allow this more easily, since this
570 * is an unusual case, and we don't want to encourage others to
571 * override these functions by making it too easy.
574 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
575 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
577 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
578 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
580 return platform_driver_register(&ohci_hcd_da8xx_driver);
582 module_init(ohci_da8xx_init);
584 static void __exit ohci_da8xx_exit(void)
586 platform_driver_unregister(&ohci_hcd_da8xx_driver);
588 module_exit(ohci_da8xx_exit);
589 MODULE_DESCRIPTION(DRIVER_DESC);
590 MODULE_LICENSE("GPL");
591 MODULE_ALIAS("platform:" DRV_NAME);