]> git.ipfire.org Git - people/arne_f/kernel.git/blob - drivers/usb/host/ohci-da8xx.c
usb: ohci-da8xx: disable the regulator if the overcurrent irq fired
[people/arne_f/kernel.git] / drivers / usb / host / ohci-da8xx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * OHCI HCD (Host Controller Driver) for USB.
4 *
5 * TI DA8xx (OMAP-L1x) Bus Glue
6 *
7 * Derived from: ohci-omap.c and ohci-s3c2410.c
8 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
9 */
10
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>
25
26 #include "ohci.h"
27
28 #define DRIVER_DESC "DA8XX"
29 #define DRV_NAME "ohci-da8xx"
30
31 static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
32
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);
36
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;
46 };
47
48 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
49
50 /* Over-current indicator change bitmask */
51 static volatile u16 ocic_mask;
52
53 static int ohci_da8xx_enable(struct usb_hcd *hcd)
54 {
55 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
56 int ret;
57
58 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
59 if (ret)
60 return ret;
61
62 ret = phy_init(da8xx_ohci->usb11_phy);
63 if (ret)
64 goto err_phy_init;
65
66 ret = phy_power_on(da8xx_ohci->usb11_phy);
67 if (ret)
68 goto err_phy_power_on;
69
70 return 0;
71
72 err_phy_power_on:
73 phy_exit(da8xx_ohci->usb11_phy);
74 err_phy_init:
75 clk_disable_unprepare(da8xx_ohci->usb11_clk);
76
77 return ret;
78 }
79
80 static void ohci_da8xx_disable(struct usb_hcd *hcd)
81 {
82 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
83
84 phy_power_off(da8xx_ohci->usb11_phy);
85 phy_exit(da8xx_ohci->usb11_phy);
86 clk_disable_unprepare(da8xx_ohci->usb11_clk);
87 }
88
89 static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
90 {
91 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
92 struct device *dev = hcd->self.controller;
93 int ret;
94
95 if (da8xx_ohci->vbus_gpio) {
96 gpiod_set_value_cansleep(da8xx_ohci->vbus_gpio, on);
97 return 0;
98 }
99
100 if (!da8xx_ohci->vbus_reg)
101 return 0;
102
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;
108 }
109 da8xx_ohci->reg_enabled = 1;
110
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;
116 }
117 da8xx_ohci->reg_enabled = 0;
118 }
119
120 return 0;
121 }
122
123 static int ohci_da8xx_get_power(struct usb_hcd *hcd)
124 {
125 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
126
127 if (da8xx_ohci->vbus_gpio)
128 return gpiod_get_value_cansleep(da8xx_ohci->vbus_gpio);
129
130 if (da8xx_ohci->vbus_reg)
131 return regulator_is_enabled(da8xx_ohci->vbus_reg);
132
133 return 1;
134 }
135
136 static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
137 {
138 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
139 unsigned int flags;
140 int ret;
141
142 if (da8xx_ohci->oc_gpio)
143 return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio);
144
145 if (!da8xx_ohci->vbus_reg)
146 return 0;
147
148 ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
149 if (ret)
150 return ret;
151
152 if (flags & REGULATOR_ERROR_OVER_CURRENT)
153 return 1;
154
155 return 0;
156 }
157
158 static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
159 {
160 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
161
162 if (da8xx_ohci->vbus_gpio)
163 return 1;
164
165 if (da8xx_ohci->vbus_reg)
166 return 1;
167
168 return 0;
169 }
170
171 static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
172 {
173 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
174
175 if (da8xx_ohci->oc_gpio)
176 return 1;
177
178 if (da8xx_ohci->vbus_reg)
179 return 1;
180
181 return 0;
182 }
183
184 static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
185 {
186 struct device *dev = hcd->self.controller;
187 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
188
189 if (hub && hub->potpgt)
190 return 1;
191
192 return 0;
193 }
194
195 static int ohci_da8xx_regulator_event(struct notifier_block *nb,
196 unsigned long event, void *data)
197 {
198 struct da8xx_ohci_hcd *da8xx_ohci =
199 container_of(nb, struct da8xx_ohci_hcd, nb);
200
201 if (event & REGULATOR_EVENT_OVER_CURRENT) {
202 ocic_mask |= 1 << 1;
203 ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
204 }
205
206 return 0;
207 }
208
209 static irqreturn_t ohci_da8xx_oc_thread(int irq, void *data)
210 {
211 struct da8xx_ohci_hcd *da8xx_ohci = data;
212 struct device *dev = da8xx_ohci->hcd->self.controller;
213 int ret;
214
215 if (gpiod_get_value_cansleep(da8xx_ohci->oc_gpio)) {
216 if (da8xx_ohci->vbus_gpio) {
217 gpiod_set_value_cansleep(da8xx_ohci->vbus_gpio, 0);
218 } else if (da8xx_ohci->vbus_reg) {
219 ret = regulator_disable(da8xx_ohci->vbus_reg);
220 if (ret)
221 dev_err(dev,
222 "Failed to disable regulator: %d\n",
223 ret);
224 }
225 }
226
227 return IRQ_HANDLED;
228 }
229
230 static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
231 {
232 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
233 struct device *dev = hcd->self.controller;
234 int ret = 0;
235
236 if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) {
237 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
238 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
239 &da8xx_ohci->nb);
240 }
241
242 if (ret)
243 dev_err(dev, "Failed to register notifier: %d\n", ret);
244
245 return ret;
246 }
247
248 static int ohci_da8xx_reset(struct usb_hcd *hcd)
249 {
250 struct device *dev = hcd->self.controller;
251 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
252 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
253 int result;
254 u32 rh_a;
255
256 dev_dbg(dev, "starting USB controller\n");
257
258 result = ohci_da8xx_enable(hcd);
259 if (result < 0)
260 return result;
261
262 /*
263 * DA8xx only have 1 port connected to the pins but the HC root hub
264 * register A reports 2 ports, thus we'll have to override it...
265 */
266 ohci->num_ports = 1;
267
268 result = ohci_setup(hcd);
269 if (result < 0) {
270 ohci_da8xx_disable(hcd);
271 return result;
272 }
273
274 /*
275 * Since we're providing a board-specific root hub port power control
276 * and over-current reporting, we have to override the HC root hub A
277 * register's default value, so that ohci_hub_control() could return
278 * the correct hub descriptor...
279 */
280 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
281 if (ohci_da8xx_has_set_power(hcd)) {
282 rh_a &= ~RH_A_NPS;
283 rh_a |= RH_A_PSM;
284 }
285 if (ohci_da8xx_has_oci(hcd)) {
286 rh_a &= ~RH_A_NOCP;
287 rh_a |= RH_A_OCPM;
288 }
289 if (ohci_da8xx_has_potpgt(hcd)) {
290 rh_a &= ~RH_A_POTPGT;
291 rh_a |= hub->potpgt << 24;
292 }
293 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
294
295 return result;
296 }
297
298 /*
299 * Update the status data from the hub with the over-current indicator change.
300 */
301 static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
302 {
303 int length = orig_ohci_hub_status_data(hcd, buf);
304
305 /* See if we have OCIC bit set on port 1 */
306 if (ocic_mask & (1 << 1)) {
307 dev_dbg(hcd->self.controller, "over-current indicator change "
308 "on port 1\n");
309
310 if (!length)
311 length = 1;
312
313 buf[0] |= 1 << 1;
314 }
315 return length;
316 }
317
318 /*
319 * Look at the control requests to the root hub and see if we need to override.
320 */
321 static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
322 u16 wIndex, char *buf, u16 wLength)
323 {
324 struct device *dev = hcd->self.controller;
325 int temp;
326
327 switch (typeReq) {
328 case GetPortStatus:
329 /* Check the port number */
330 if (wIndex != 1)
331 break;
332
333 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
334
335 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
336
337 /* The port power status (PPS) bit defaults to 1 */
338 if (!ohci_da8xx_get_power(hcd))
339 temp &= ~RH_PS_PPS;
340
341 /* The port over-current indicator (POCI) bit is always 0 */
342 if (ohci_da8xx_get_oci(hcd) > 0)
343 temp |= RH_PS_POCI;
344
345 /* The over-current indicator change (OCIC) bit is 0 too */
346 if (ocic_mask & (1 << wIndex))
347 temp |= RH_PS_OCIC;
348
349 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
350 return 0;
351 case SetPortFeature:
352 temp = 1;
353 goto check_port;
354 case ClearPortFeature:
355 temp = 0;
356
357 check_port:
358 /* Check the port number */
359 if (wIndex != 1)
360 break;
361
362 switch (wValue) {
363 case USB_PORT_FEAT_POWER:
364 dev_dbg(dev, "%sPortFeature(%u): %s\n",
365 temp ? "Set" : "Clear", wIndex, "POWER");
366
367 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
368 case USB_PORT_FEAT_C_OVER_CURRENT:
369 dev_dbg(dev, "%sPortFeature(%u): %s\n",
370 temp ? "Set" : "Clear", wIndex,
371 "C_OVER_CURRENT");
372
373 if (temp)
374 ocic_mask |= 1 << wIndex;
375 else
376 ocic_mask &= ~(1 << wIndex);
377 return 0;
378 }
379 }
380
381 return orig_ohci_hub_control(hcd, typeReq, wValue,
382 wIndex, buf, wLength);
383 }
384
385 /*-------------------------------------------------------------------------*/
386 #ifdef CONFIG_OF
387 static const struct of_device_id da8xx_ohci_ids[] = {
388 { .compatible = "ti,da830-ohci" },
389 { }
390 };
391 MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
392 #endif
393
394 static int ohci_da8xx_probe(struct platform_device *pdev)
395 {
396 struct da8xx_ohci_hcd *da8xx_ohci;
397 struct device *dev = &pdev->dev;
398 int error, hcd_irq, oc_irq;
399 struct usb_hcd *hcd;
400 struct resource *mem;
401
402 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev));
403 if (!hcd)
404 return -ENOMEM;
405
406 da8xx_ohci = to_da8xx_ohci(hcd);
407 da8xx_ohci->hcd = hcd;
408
409 da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL);
410 if (IS_ERR(da8xx_ohci->usb11_clk)) {
411 error = PTR_ERR(da8xx_ohci->usb11_clk);
412 if (error != -EPROBE_DEFER)
413 dev_err(dev, "Failed to get clock.\n");
414 goto err;
415 }
416
417 da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy");
418 if (IS_ERR(da8xx_ohci->usb11_phy)) {
419 error = PTR_ERR(da8xx_ohci->usb11_phy);
420 if (error != -EPROBE_DEFER)
421 dev_err(dev, "Failed to get phy.\n");
422 goto err;
423 }
424
425 da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus");
426 if (IS_ERR(da8xx_ohci->vbus_reg)) {
427 error = PTR_ERR(da8xx_ohci->vbus_reg);
428 if (error == -ENODEV) {
429 da8xx_ohci->vbus_reg = NULL;
430 } else if (error == -EPROBE_DEFER) {
431 goto err;
432 } else {
433 dev_err(dev, "Failed to get regulator\n");
434 goto err;
435 }
436 }
437
438 da8xx_ohci->vbus_gpio = devm_gpiod_get_optional(dev, "vbus",
439 GPIOD_OUT_HIGH);
440 if (IS_ERR(da8xx_ohci->vbus_gpio))
441 goto err;
442
443 da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN);
444 if (IS_ERR(da8xx_ohci->oc_gpio))
445 goto err;
446
447 if (da8xx_ohci->oc_gpio) {
448 oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio);
449 if (oc_irq < 0)
450 goto err;
451
452 error = devm_request_threaded_irq(dev, oc_irq, NULL,
453 ohci_da8xx_oc_thread, IRQF_TRIGGER_RISING |
454 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
455 "OHCI over-current indicator", da8xx_ohci);
456 if (error)
457 goto err;
458 }
459
460 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
461 hcd->regs = devm_ioremap_resource(dev, mem);
462 if (IS_ERR(hcd->regs)) {
463 error = PTR_ERR(hcd->regs);
464 goto err;
465 }
466 hcd->rsrc_start = mem->start;
467 hcd->rsrc_len = resource_size(mem);
468
469 hcd_irq = platform_get_irq(pdev, 0);
470 if (hcd_irq < 0) {
471 error = -ENODEV;
472 goto err;
473 }
474
475 error = usb_add_hcd(hcd, hcd_irq, 0);
476 if (error)
477 goto err;
478
479 device_wakeup_enable(hcd->self.controller);
480
481 error = ohci_da8xx_register_notify(hcd);
482 if (error)
483 goto err_remove_hcd;
484
485 return 0;
486
487 err_remove_hcd:
488 usb_remove_hcd(hcd);
489 err:
490 usb_put_hcd(hcd);
491 return error;
492 }
493
494 static int ohci_da8xx_remove(struct platform_device *pdev)
495 {
496 struct usb_hcd *hcd = platform_get_drvdata(pdev);
497
498 usb_remove_hcd(hcd);
499 usb_put_hcd(hcd);
500
501 return 0;
502 }
503
504 #ifdef CONFIG_PM
505 static int ohci_da8xx_suspend(struct platform_device *pdev,
506 pm_message_t message)
507 {
508 struct usb_hcd *hcd = platform_get_drvdata(pdev);
509 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
510 bool do_wakeup = device_may_wakeup(&pdev->dev);
511 int ret;
512
513
514 if (time_before(jiffies, ohci->next_statechange))
515 msleep(5);
516 ohci->next_statechange = jiffies;
517
518 ret = ohci_suspend(hcd, do_wakeup);
519 if (ret)
520 return ret;
521
522 ohci_da8xx_disable(hcd);
523 hcd->state = HC_STATE_SUSPENDED;
524
525 return ret;
526 }
527
528 static int ohci_da8xx_resume(struct platform_device *dev)
529 {
530 struct usb_hcd *hcd = platform_get_drvdata(dev);
531 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
532 int ret;
533
534 if (time_before(jiffies, ohci->next_statechange))
535 msleep(5);
536 ohci->next_statechange = jiffies;
537
538 ret = ohci_da8xx_enable(hcd);
539 if (ret)
540 return ret;
541
542 ohci_resume(hcd, false);
543
544 return 0;
545 }
546 #endif
547
548 static const struct ohci_driver_overrides da8xx_overrides __initconst = {
549 .reset = ohci_da8xx_reset,
550 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
551 };
552
553 /*
554 * Driver definition to register with platform structure.
555 */
556 static struct platform_driver ohci_hcd_da8xx_driver = {
557 .probe = ohci_da8xx_probe,
558 .remove = ohci_da8xx_remove,
559 .shutdown = usb_hcd_platform_shutdown,
560 #ifdef CONFIG_PM
561 .suspend = ohci_da8xx_suspend,
562 .resume = ohci_da8xx_resume,
563 #endif
564 .driver = {
565 .name = DRV_NAME,
566 .of_match_table = of_match_ptr(da8xx_ohci_ids),
567 },
568 };
569
570 static int __init ohci_da8xx_init(void)
571 {
572
573 if (usb_disabled())
574 return -ENODEV;
575
576 pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
577 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
578
579 /*
580 * The Davinci da8xx HW has some unusual quirks, which require
581 * da8xx-specific workarounds. We override certain hc_driver
582 * functions here to achieve that. We explicitly do not enhance
583 * ohci_driver_overrides to allow this more easily, since this
584 * is an unusual case, and we don't want to encourage others to
585 * override these functions by making it too easy.
586 */
587
588 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
589 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
590
591 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
592 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
593
594 return platform_driver_register(&ohci_hcd_da8xx_driver);
595 }
596 module_init(ohci_da8xx_init);
597
598 static void __exit ohci_da8xx_exit(void)
599 {
600 platform_driver_unregister(&ohci_hcd_da8xx_driver);
601 }
602 module_exit(ohci_da8xx_exit);
603 MODULE_DESCRIPTION(DRIVER_DESC);
604 MODULE_LICENSE("GPL");
605 MODULE_ALIAS("platform:" DRV_NAME);