dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / usb / host / ohci-da8xx.c
blobca8a94f15ac05b9eff29c3c04139cd6d8bc4d222
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/gpio/consumer.h>
13 #include <linux/io.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_data/usb-davinci.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
24 #include <asm/unaligned.h>
26 #include "ohci.h"
28 #define DRIVER_DESC "DA8XX"
29 #define DRV_NAME "ohci-da8xx"
31 static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
33 static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
34 u16 wValue, u16 wIndex, char *buf, u16 wLength);
35 static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
37 struct da8xx_ohci_hcd {
38 struct usb_hcd *hcd;
39 struct clk *usb11_clk;
40 struct phy *usb11_phy;
41 struct regulator *vbus_reg;
42 struct notifier_block nb;
43 unsigned int reg_enabled;
44 struct gpio_desc *vbus_gpio;
45 struct gpio_desc *oc_gpio;
48 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
50 /* Over-current indicator change bitmask */
51 static volatile u16 ocic_mask;
53 static int ohci_da8xx_enable(struct usb_hcd *hcd)
55 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
56 int ret;
58 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
59 if (ret)
60 return ret;
62 ret = phy_init(da8xx_ohci->usb11_phy);
63 if (ret)
64 goto err_phy_init;
66 ret = phy_power_on(da8xx_ohci->usb11_phy);
67 if (ret)
68 goto err_phy_power_on;
70 return 0;
72 err_phy_power_on:
73 phy_exit(da8xx_ohci->usb11_phy);
74 err_phy_init:
75 clk_disable_unprepare(da8xx_ohci->usb11_clk);
77 return ret;
80 static void ohci_da8xx_disable(struct usb_hcd *hcd)
82 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
84 phy_power_off(da8xx_ohci->usb11_phy);
85 phy_exit(da8xx_ohci->usb11_phy);
86 clk_disable_unprepare(da8xx_ohci->usb11_clk);
89 static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
91 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
92 struct device *dev = hcd->self.controller;
93 int ret;
95 if (da8xx_ohci->vbus_gpio) {
96 gpiod_set_value_cansleep(da8xx_ohci->vbus_gpio, on);
97 return 0;
100 if (!da8xx_ohci->vbus_reg)
101 return 0;
103 if (on && !da8xx_ohci->reg_enabled) {
104 ret = regulator_enable(da8xx_ohci->vbus_reg);
105 if (ret) {
106 dev_err(dev, "Failed to enable regulator: %d\n", ret);
107 return ret;
109 da8xx_ohci->reg_enabled = 1;
111 } else if (!on && da8xx_ohci->reg_enabled) {
112 ret = regulator_disable(da8xx_ohci->vbus_reg);
113 if (ret) {
114 dev_err(dev, "Failed to disable regulator: %d\n", ret);
115 return ret;
117 da8xx_ohci->reg_enabled = 0;
120 return 0;
123 static int ohci_da8xx_get_power(struct usb_hcd *hcd)
125 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
127 if (da8xx_ohci->vbus_gpio)
128 return gpiod_get_value_cansleep(da8xx_ohci->vbus_gpio);
130 if (da8xx_ohci->vbus_reg)
131 return regulator_is_enabled(da8xx_ohci->vbus_reg);
133 return 1;
136 static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
138 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
139 unsigned int flags;
140 int ret;
142 if (da8xx_ohci->oc_gpio)
143 return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio);
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);
162 if (da8xx_ohci->vbus_gpio)
163 return 1;
165 if (da8xx_ohci->vbus_reg)
166 return 1;
168 return 0;
171 static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
173 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
175 if (da8xx_ohci->oc_gpio)
176 return 1;
178 if (da8xx_ohci->vbus_reg)
179 return 1;
181 return 0;
184 static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
186 struct device *dev = hcd->self.controller;
187 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
189 if (hub && hub->potpgt)
190 return 1;
192 return 0;
195 static int ohci_da8xx_regulator_event(struct notifier_block *nb,
196 unsigned long event, void *data)
198 struct da8xx_ohci_hcd *da8xx_ohci =
199 container_of(nb, struct da8xx_ohci_hcd, nb);
201 if (event & REGULATOR_EVENT_OVER_CURRENT) {
202 ocic_mask |= 1 << 1;
203 ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
206 return 0;
209 static irqreturn_t ohci_da8xx_oc_handler(int irq, void *data)
211 struct da8xx_ohci_hcd *da8xx_ohci = data;
213 if (gpiod_get_value(da8xx_ohci->oc_gpio))
214 gpiod_set_value(da8xx_ohci->vbus_gpio, 0);
216 return IRQ_HANDLED;
219 static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
221 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
222 struct device *dev = hcd->self.controller;
223 int ret = 0;
225 if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) {
226 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
227 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
228 &da8xx_ohci->nb);
231 if (ret)
232 dev_err(dev, "Failed to register notifier: %d\n", ret);
234 return ret;
237 static int ohci_da8xx_reset(struct usb_hcd *hcd)
239 struct device *dev = hcd->self.controller;
240 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
241 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
242 int result;
243 u32 rh_a;
245 dev_dbg(dev, "starting USB controller\n");
247 result = ohci_da8xx_enable(hcd);
248 if (result < 0)
249 return result;
252 * DA8xx only have 1 port connected to the pins but the HC root hub
253 * register A reports 2 ports, thus we'll have to override it...
255 ohci->num_ports = 1;
257 result = ohci_setup(hcd);
258 if (result < 0) {
259 ohci_da8xx_disable(hcd);
260 return result;
264 * Since we're providing a board-specific root hub port power control
265 * and over-current reporting, we have to override the HC root hub A
266 * register's default value, so that ohci_hub_control() could return
267 * the correct hub descriptor...
269 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
270 if (ohci_da8xx_has_set_power(hcd)) {
271 rh_a &= ~RH_A_NPS;
272 rh_a |= RH_A_PSM;
274 if (ohci_da8xx_has_oci(hcd)) {
275 rh_a &= ~RH_A_NOCP;
276 rh_a |= RH_A_OCPM;
278 if (ohci_da8xx_has_potpgt(hcd)) {
279 rh_a &= ~RH_A_POTPGT;
280 rh_a |= hub->potpgt << 24;
282 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
284 return result;
288 * Update the status data from the hub with the over-current indicator change.
290 static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
292 int length = orig_ohci_hub_status_data(hcd, buf);
294 /* See if we have OCIC bit set on port 1 */
295 if (ocic_mask & (1 << 1)) {
296 dev_dbg(hcd->self.controller, "over-current indicator change "
297 "on port 1\n");
299 if (!length)
300 length = 1;
302 buf[0] |= 1 << 1;
304 return length;
308 * Look at the control requests to the root hub and see if we need to override.
310 static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
311 u16 wIndex, char *buf, u16 wLength)
313 struct device *dev = hcd->self.controller;
314 int temp;
316 switch (typeReq) {
317 case GetPortStatus:
318 /* Check the port number */
319 if (wIndex != 1)
320 break;
322 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
324 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
326 /* The port power status (PPS) bit defaults to 1 */
327 if (!ohci_da8xx_get_power(hcd))
328 temp &= ~RH_PS_PPS;
330 /* The port over-current indicator (POCI) bit is always 0 */
331 if (ohci_da8xx_get_oci(hcd) > 0)
332 temp |= RH_PS_POCI;
334 /* The over-current indicator change (OCIC) bit is 0 too */
335 if (ocic_mask & (1 << wIndex))
336 temp |= RH_PS_OCIC;
338 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
339 return 0;
340 case SetPortFeature:
341 temp = 1;
342 goto check_port;
343 case ClearPortFeature:
344 temp = 0;
346 check_port:
347 /* Check the port number */
348 if (wIndex != 1)
349 break;
351 switch (wValue) {
352 case USB_PORT_FEAT_POWER:
353 dev_dbg(dev, "%sPortFeature(%u): %s\n",
354 temp ? "Set" : "Clear", wIndex, "POWER");
356 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
357 case USB_PORT_FEAT_C_OVER_CURRENT:
358 dev_dbg(dev, "%sPortFeature(%u): %s\n",
359 temp ? "Set" : "Clear", wIndex,
360 "C_OVER_CURRENT");
362 if (temp)
363 ocic_mask |= 1 << wIndex;
364 else
365 ocic_mask &= ~(1 << wIndex);
366 return 0;
370 return orig_ohci_hub_control(hcd, typeReq, wValue,
371 wIndex, buf, wLength);
374 /*-------------------------------------------------------------------------*/
375 #ifdef CONFIG_OF
376 static const struct of_device_id da8xx_ohci_ids[] = {
377 { .compatible = "ti,da830-ohci" },
380 MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
381 #endif
383 static int ohci_da8xx_probe(struct platform_device *pdev)
385 struct da8xx_ohci_hcd *da8xx_ohci;
386 struct device *dev = &pdev->dev;
387 int error, hcd_irq, oc_irq;
388 struct usb_hcd *hcd;
389 struct resource *mem;
391 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev));
392 if (!hcd)
393 return -ENOMEM;
395 da8xx_ohci = to_da8xx_ohci(hcd);
396 da8xx_ohci->hcd = hcd;
398 da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL);
399 if (IS_ERR(da8xx_ohci->usb11_clk)) {
400 error = PTR_ERR(da8xx_ohci->usb11_clk);
401 if (error != -EPROBE_DEFER)
402 dev_err(dev, "Failed to get clock.\n");
403 goto err;
406 da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy");
407 if (IS_ERR(da8xx_ohci->usb11_phy)) {
408 error = PTR_ERR(da8xx_ohci->usb11_phy);
409 if (error != -EPROBE_DEFER)
410 dev_err(dev, "Failed to get phy.\n");
411 goto err;
414 da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus");
415 if (IS_ERR(da8xx_ohci->vbus_reg)) {
416 error = PTR_ERR(da8xx_ohci->vbus_reg);
417 if (error == -ENODEV) {
418 da8xx_ohci->vbus_reg = NULL;
419 } else if (error == -EPROBE_DEFER) {
420 goto err;
421 } else {
422 dev_err(dev, "Failed to get regulator\n");
423 goto err;
427 da8xx_ohci->vbus_gpio = devm_gpiod_get_optional(dev, "vbus",
428 GPIOD_OUT_HIGH);
429 if (IS_ERR(da8xx_ohci->vbus_gpio))
430 goto err;
432 da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN);
433 if (IS_ERR(da8xx_ohci->oc_gpio))
434 goto err;
436 if (da8xx_ohci->oc_gpio) {
437 oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio);
438 if (oc_irq < 0)
439 goto err;
441 error = devm_request_irq(dev, oc_irq, ohci_da8xx_oc_handler,
442 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
443 "OHCI over-current indicator", da8xx_ohci);
444 if (error)
445 goto err;
448 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449 hcd->regs = devm_ioremap_resource(dev, mem);
450 if (IS_ERR(hcd->regs)) {
451 error = PTR_ERR(hcd->regs);
452 goto err;
454 hcd->rsrc_start = mem->start;
455 hcd->rsrc_len = resource_size(mem);
457 hcd_irq = platform_get_irq(pdev, 0);
458 if (hcd_irq < 0) {
459 error = -ENODEV;
460 goto err;
463 error = usb_add_hcd(hcd, hcd_irq, 0);
464 if (error)
465 goto err;
467 device_wakeup_enable(hcd->self.controller);
469 error = ohci_da8xx_register_notify(hcd);
470 if (error)
471 goto err_remove_hcd;
473 return 0;
475 err_remove_hcd:
476 usb_remove_hcd(hcd);
477 err:
478 usb_put_hcd(hcd);
479 return error;
482 static int ohci_da8xx_remove(struct platform_device *pdev)
484 struct usb_hcd *hcd = platform_get_drvdata(pdev);
486 usb_remove_hcd(hcd);
487 usb_put_hcd(hcd);
489 return 0;
492 #ifdef CONFIG_PM
493 static int ohci_da8xx_suspend(struct platform_device *pdev,
494 pm_message_t message)
496 struct usb_hcd *hcd = platform_get_drvdata(pdev);
497 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
498 bool do_wakeup = device_may_wakeup(&pdev->dev);
499 int ret;
502 if (time_before(jiffies, ohci->next_statechange))
503 msleep(5);
504 ohci->next_statechange = jiffies;
506 ret = ohci_suspend(hcd, do_wakeup);
507 if (ret)
508 return ret;
510 ohci_da8xx_disable(hcd);
511 hcd->state = HC_STATE_SUSPENDED;
513 return ret;
516 static int ohci_da8xx_resume(struct platform_device *dev)
518 struct usb_hcd *hcd = platform_get_drvdata(dev);
519 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
520 int ret;
522 if (time_before(jiffies, ohci->next_statechange))
523 msleep(5);
524 ohci->next_statechange = jiffies;
526 ret = ohci_da8xx_enable(hcd);
527 if (ret)
528 return ret;
530 ohci_resume(hcd, false);
532 return 0;
534 #endif
536 static const struct ohci_driver_overrides da8xx_overrides __initconst = {
537 .reset = ohci_da8xx_reset,
538 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
542 * Driver definition to register with platform structure.
544 static struct platform_driver ohci_hcd_da8xx_driver = {
545 .probe = ohci_da8xx_probe,
546 .remove = ohci_da8xx_remove,
547 .shutdown = usb_hcd_platform_shutdown,
548 #ifdef CONFIG_PM
549 .suspend = ohci_da8xx_suspend,
550 .resume = ohci_da8xx_resume,
551 #endif
552 .driver = {
553 .name = DRV_NAME,
554 .of_match_table = of_match_ptr(da8xx_ohci_ids),
558 static int __init ohci_da8xx_init(void)
561 if (usb_disabled())
562 return -ENODEV;
564 pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
565 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
568 * The Davinci da8xx HW has some unusual quirks, which require
569 * da8xx-specific workarounds. We override certain hc_driver
570 * functions here to achieve that. We explicitly do not enhance
571 * ohci_driver_overrides to allow this more easily, since this
572 * is an unusual case, and we don't want to encourage others to
573 * override these functions by making it too easy.
576 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
577 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
579 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
580 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
582 return platform_driver_register(&ohci_hcd_da8xx_driver);
584 module_init(ohci_da8xx_init);
586 static void __exit ohci_da8xx_exit(void)
588 platform_driver_unregister(&ohci_hcd_da8xx_driver);
590 module_exit(ohci_da8xx_exit);
591 MODULE_DESCRIPTION(DRIVER_DESC);
592 MODULE_LICENSE("GPL");
593 MODULE_ALIAS("platform:" DRV_NAME);