Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
4 */
5
6#include <linux/delay.h>
7#include <linux/io.h>
8#include <linux/mailbox_client.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/of_device.h>
12#include <linux/phy/phy.h>
13#include <linux/phy/tegra/xusb.h>
14#include <linux/platform_device.h>
15#include <linux/regulator/consumer.h>
16#include <linux/reset.h>
17#include <linux/slab.h>
18#include <linux/workqueue.h>
19
20#include <soc/tegra/fuse.h>
21
22#include "xusb.h"
23
24static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
25 struct of_phandle_args *args)
26{
27 struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
28 struct phy *phy = NULL;
29 unsigned int i;
30
31 if (args->args_count != 0)
32 return ERR_PTR(-EINVAL);
33
34 for (i = 0; i < pad->soc->num_lanes; i++) {
35 if (!pad->lanes[i])
36 continue;
37
38 if (pad->lanes[i]->dev.of_node == args->np) {
39 phy = pad->lanes[i];
40 break;
41 }
42 }
43
44 if (phy == NULL)
45 phy = ERR_PTR(-ENODEV);
46
47 return phy;
48}
49
50static const struct of_device_id tegra_xusb_padctl_of_match[] = {
51#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
52 {
53 .compatible = "nvidia,tegra124-xusb-padctl",
54 .data = &tegra124_xusb_padctl_soc,
55 },
56#endif
57#if defined(CONFIG_ARCH_TEGRA_210_SOC)
58 {
59 .compatible = "nvidia,tegra210-xusb-padctl",
60 .data = &tegra210_xusb_padctl_soc,
61 },
62#endif
63#if defined(CONFIG_ARCH_TEGRA_186_SOC)
64 {
65 .compatible = "nvidia,tegra186-xusb-padctl",
66 .data = &tegra186_xusb_padctl_soc,
67 },
68#endif
69#if defined(CONFIG_ARCH_TEGRA_194_SOC)
70 {
71 .compatible = "nvidia,tegra194-xusb-padctl",
72 .data = &tegra194_xusb_padctl_soc,
73 },
74#endif
75 { }
76};
77MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
78
79static struct device_node *
80tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
81{
82 struct device_node *pads, *np;
83
84 pads = of_get_child_by_name(padctl->dev->of_node, "pads");
85 if (!pads)
86 return NULL;
87
88 np = of_get_child_by_name(pads, name);
89 of_node_put(pads);
90
91 return np;
92}
93
94static struct device_node *
95tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
96{
97 struct device_node *np, *lanes;
98
99 lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
100 if (!lanes)
101 return NULL;
102
103 np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
104 of_node_put(lanes);
105
106 return np;
107}
108
109int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
110 struct device_node *np)
111{
112 struct device *dev = &lane->pad->dev;
113 const char *function;
114 int err;
115
116 err = of_property_read_string(np, "nvidia,function", &function);
117 if (err < 0)
118 return err;
119
120 err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
121 if (err < 0) {
122 dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
123 function, np);
124 return err;
125 }
126
127 lane->function = err;
128
129 return 0;
130}
131
132static void tegra_xusb_lane_destroy(struct phy *phy)
133{
134 if (phy) {
135 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
136
137 lane->pad->ops->remove(lane);
138 phy_destroy(phy);
139 }
140}
141
142static void tegra_xusb_pad_release(struct device *dev)
143{
144 struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
145
146 pad->soc->ops->remove(pad);
147}
148
149static struct device_type tegra_xusb_pad_type = {
150 .release = tegra_xusb_pad_release,
151};
152
153int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
154 struct tegra_xusb_padctl *padctl,
155 struct device_node *np)
156{
157 int err;
158
159 device_initialize(&pad->dev);
160 INIT_LIST_HEAD(&pad->list);
161 pad->dev.parent = padctl->dev;
162 pad->dev.type = &tegra_xusb_pad_type;
163 pad->dev.of_node = np;
164 pad->padctl = padctl;
165
166 err = dev_set_name(&pad->dev, "%s", pad->soc->name);
167 if (err < 0)
168 goto unregister;
169
170 err = device_add(&pad->dev);
171 if (err < 0)
172 goto unregister;
173
174 return 0;
175
176unregister:
177 device_unregister(&pad->dev);
178 return err;
179}
180
181int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
182 const struct phy_ops *ops)
183{
184 struct device_node *children;
185 struct phy *lane;
186 unsigned int i;
187 int err;
188
189 children = of_get_child_by_name(pad->dev.of_node, "lanes");
190 if (!children)
191 return -ENODEV;
192
193 pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
194 GFP_KERNEL);
195 if (!pad->lanes) {
196 of_node_put(children);
197 return -ENOMEM;
198 }
199
200 for (i = 0; i < pad->soc->num_lanes; i++) {
201 struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
202 struct tegra_xusb_lane *lane;
203
204 /* skip disabled lanes */
205 if (!np || !of_device_is_available(np)) {
206 of_node_put(np);
207 continue;
208 }
209
210 pad->lanes[i] = phy_create(&pad->dev, np, ops);
211 if (IS_ERR(pad->lanes[i])) {
212 err = PTR_ERR(pad->lanes[i]);
213 of_node_put(np);
214 goto remove;
215 }
216
217 lane = pad->ops->probe(pad, np, i);
218 if (IS_ERR(lane)) {
219 phy_destroy(pad->lanes[i]);
220 err = PTR_ERR(lane);
221 goto remove;
222 }
223
224 list_add_tail(&lane->list, &pad->padctl->lanes);
225 phy_set_drvdata(pad->lanes[i], lane);
226 }
227
228 pad->provider = of_phy_provider_register_full(&pad->dev, children,
229 tegra_xusb_pad_of_xlate);
230 if (IS_ERR(pad->provider)) {
231 err = PTR_ERR(pad->provider);
232 goto remove;
233 }
234
235 return 0;
236
237remove:
238 while (i--)
239 tegra_xusb_lane_destroy(pad->lanes[i]);
240
241 of_node_put(children);
242
243 return err;
244}
245
246void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
247{
248 unsigned int i = pad->soc->num_lanes;
249
250 of_phy_provider_unregister(pad->provider);
251
252 while (i--)
253 tegra_xusb_lane_destroy(pad->lanes[i]);
254
255 device_unregister(&pad->dev);
256}
257
258static struct tegra_xusb_pad *
259tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
260 const struct tegra_xusb_pad_soc *soc)
261{
262 struct tegra_xusb_pad *pad;
263 struct device_node *np;
264 int err;
265
266 np = tegra_xusb_find_pad_node(padctl, soc->name);
267 if (!np || !of_device_is_available(np))
268 return NULL;
269
270 pad = soc->ops->probe(padctl, soc, np);
271 if (IS_ERR(pad)) {
272 err = PTR_ERR(pad);
273 dev_err(padctl->dev, "failed to create pad %s: %d\n",
274 soc->name, err);
275 return ERR_PTR(err);
276 }
277
278 /* XXX move this into ->probe() to avoid string comparison */
279 if (strcmp(soc->name, "pcie") == 0)
280 padctl->pcie = pad;
281
282 if (strcmp(soc->name, "sata") == 0)
283 padctl->sata = pad;
284
285 if (strcmp(soc->name, "usb2") == 0)
286 padctl->usb2 = pad;
287
288 if (strcmp(soc->name, "ulpi") == 0)
289 padctl->ulpi = pad;
290
291 if (strcmp(soc->name, "hsic") == 0)
292 padctl->hsic = pad;
293
294 return pad;
295}
296
297static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
298{
299 struct tegra_xusb_pad *pad, *tmp;
300
301 list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
302 list_del(&pad->list);
303 tegra_xusb_pad_unregister(pad);
304 }
305}
306
307static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
308{
309 mutex_lock(&padctl->lock);
310 __tegra_xusb_remove_pads(padctl);
311 mutex_unlock(&padctl->lock);
312}
313
314static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
315{
316 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
317 const struct tegra_xusb_lane_soc *soc = lane->soc;
318 u32 value;
319
320 /* skip single function lanes */
321 if (soc->num_funcs < 2)
322 return;
323
324 /* choose function */
325 value = padctl_readl(padctl, soc->offset);
326 value &= ~(soc->mask << soc->shift);
327 value |= lane->function << soc->shift;
328 padctl_writel(padctl, value, soc->offset);
329}
330
331static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
332{
333 unsigned int i;
334
335 for (i = 0; i < pad->soc->num_lanes; i++) {
336 struct tegra_xusb_lane *lane;
337
338 if (pad->lanes[i]) {
339 lane = phy_get_drvdata(pad->lanes[i]);
340 tegra_xusb_lane_program(lane);
341 }
342 }
343}
344
345static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
346{
347 struct tegra_xusb_pad *pad;
348 unsigned int i;
349
350 mutex_lock(&padctl->lock);
351
352 for (i = 0; i < padctl->soc->num_pads; i++) {
353 const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
354 int err;
355
356 pad = tegra_xusb_pad_create(padctl, soc);
357 if (IS_ERR(pad)) {
358 err = PTR_ERR(pad);
359 dev_err(padctl->dev, "failed to create pad %s: %d\n",
360 soc->name, err);
361 __tegra_xusb_remove_pads(padctl);
362 mutex_unlock(&padctl->lock);
363 return err;
364 }
365
366 if (!pad)
367 continue;
368
369 list_add_tail(&pad->list, &padctl->pads);
370 }
371
372 list_for_each_entry(pad, &padctl->pads, list)
373 tegra_xusb_pad_program(pad);
374
375 mutex_unlock(&padctl->lock);
376 return 0;
377}
378
379static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
380 const char *function)
381{
382 const char *func = lane->soc->funcs[lane->function];
383
384 return strcmp(function, func) == 0;
385}
386
387struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
388 const char *type,
389 unsigned int index)
390{
391 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
392 char *name;
393
394 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
395 if (!name)
396 return ERR_PTR(-ENOMEM);
397
398 list_for_each_entry(lane, &padctl->lanes, list) {
399 if (strcmp(lane->soc->name, name) == 0) {
400 hit = lane;
401 break;
402 }
403 }
404
405 kfree(name);
406 return hit;
407}
408
409struct tegra_xusb_lane *
410tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
411 const struct tegra_xusb_lane_map *map,
412 const char *function)
413{
414 struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
415
416 for (; map->type; map++) {
417 if (port->index != map->port)
418 continue;
419
420 lane = tegra_xusb_find_lane(port->padctl, map->type,
421 map->index);
422 if (IS_ERR(lane))
423 continue;
424
425 if (!tegra_xusb_lane_check(lane, function))
426 continue;
427
428 if (!IS_ERR(match))
429 dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
430 map->type, map->index, match->soc->name);
431 else
432 match = lane;
433 }
434
435 return match;
436}
437
438static struct device_node *
439tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
440 unsigned int index)
441{
442 struct device_node *ports, *np;
443 char *name;
444
445 ports = of_get_child_by_name(padctl->dev->of_node, "ports");
446 if (!ports)
447 return NULL;
448
449 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
450 if (!name) {
451 of_node_put(ports);
452 return ERR_PTR(-ENOMEM);
453 }
454 np = of_get_child_by_name(ports, name);
455 kfree(name);
456 of_node_put(ports);
457
458 return np;
459}
460
461struct tegra_xusb_port *
462tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
463 unsigned int index)
464{
465 struct tegra_xusb_port *port;
466 struct device_node *np;
467
468 np = tegra_xusb_find_port_node(padctl, type, index);
469 if (!np)
470 return NULL;
471
472 list_for_each_entry(port, &padctl->ports, list) {
473 if (np == port->dev.of_node) {
474 of_node_put(np);
475 return port;
476 }
477 }
478
479 of_node_put(np);
480
481 return NULL;
482}
483
484struct tegra_xusb_usb2_port *
485tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
486{
487 struct tegra_xusb_port *port;
488
489 port = tegra_xusb_find_port(padctl, "usb2", index);
490 if (port)
491 return to_usb2_port(port);
492
493 return NULL;
494}
495
496struct tegra_xusb_usb3_port *
497tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
498{
499 struct tegra_xusb_port *port;
500
501 port = tegra_xusb_find_port(padctl, "usb3", index);
502 if (port)
503 return to_usb3_port(port);
504
505 return NULL;
506}
507
508static void tegra_xusb_port_release(struct device *dev)
509{
510 struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
511
512 if (port->ops->release)
513 port->ops->release(port);
514}
515
516static struct device_type tegra_xusb_port_type = {
517 .release = tegra_xusb_port_release,
518};
519
520static int tegra_xusb_port_init(struct tegra_xusb_port *port,
521 struct tegra_xusb_padctl *padctl,
522 struct device_node *np,
523 const char *name,
524 unsigned int index)
525{
526 int err;
527
528 INIT_LIST_HEAD(&port->list);
529 port->padctl = padctl;
530 port->index = index;
531
532 device_initialize(&port->dev);
533 port->dev.type = &tegra_xusb_port_type;
534 port->dev.of_node = of_node_get(np);
535 port->dev.parent = padctl->dev;
536
537 err = dev_set_name(&port->dev, "%s-%u", name, index);
538 if (err < 0)
539 goto unregister;
540
541 err = device_add(&port->dev);
542 if (err < 0)
543 goto unregister;
544
545 return 0;
546
547unregister:
548 device_unregister(&port->dev);
549 return err;
550}
551
552static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
553{
554 if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
555 of_platform_depopulate(&port->dev);
556 usb_role_switch_unregister(port->usb_role_sw);
557 cancel_work_sync(&port->usb_phy_work);
558 usb_remove_phy(&port->usb_phy);
559 }
560
561 if (port->ops->remove)
562 port->ops->remove(port);
563
564 device_unregister(&port->dev);
565}
566
567static const char *const modes[] = {
568 [USB_DR_MODE_UNKNOWN] = "",
569 [USB_DR_MODE_HOST] = "host",
570 [USB_DR_MODE_PERIPHERAL] = "peripheral",
571 [USB_DR_MODE_OTG] = "otg",
572};
573
574static const char * const usb_roles[] = {
575 [USB_ROLE_NONE] = "none",
576 [USB_ROLE_HOST] = "host",
577 [USB_ROLE_DEVICE] = "device",
578};
579
580static enum usb_phy_events to_usb_phy_event(enum usb_role role)
581{
582 switch (role) {
583 case USB_ROLE_DEVICE:
584 return USB_EVENT_VBUS;
585
586 case USB_ROLE_HOST:
587 return USB_EVENT_ID;
588
589 default:
590 return USB_EVENT_NONE;
591 }
592}
593
594static void tegra_xusb_usb_phy_work(struct work_struct *work)
595{
596 struct tegra_xusb_port *port = container_of(work,
597 struct tegra_xusb_port,
598 usb_phy_work);
599 enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
600
601 usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
602
603 dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
604 usb_roles[role]);
605
606 atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
607}
608
609static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
610 enum usb_role role)
611{
612 struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
613
614 dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
615
616 schedule_work(&port->usb_phy_work);
617
618 return 0;
619}
620
621static int tegra_xusb_set_peripheral(struct usb_otg *otg,
622 struct usb_gadget *gadget)
623{
624 struct tegra_xusb_port *port = container_of(otg->usb_phy,
625 struct tegra_xusb_port,
626 usb_phy);
627
628 if (gadget != NULL)
629 schedule_work(&port->usb_phy_work);
630
631 return 0;
632}
633
634static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
635{
636 struct tegra_xusb_port *port = container_of(otg->usb_phy,
637 struct tegra_xusb_port,
638 usb_phy);
639
640 if (host != NULL)
641 schedule_work(&port->usb_phy_work);
642
643 return 0;
644}
645
646
647static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
648{
649 struct tegra_xusb_lane *lane;
650 struct usb_role_switch_desc role_sx_desc = {
651 .fwnode = dev_fwnode(&port->dev),
652 .set = tegra_xusb_role_sw_set,
653 };
654 int err = 0;
655
656 /*
657 * USB role switch driver needs parent driver owner info. This is a
658 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
659 * where an optimal solution is possible with changes to USB role
660 * switch driver.
661 */
662 port->dev.driver = devm_kzalloc(&port->dev,
663 sizeof(struct device_driver),
664 GFP_KERNEL);
665 port->dev.driver->owner = THIS_MODULE;
666
667 port->usb_role_sw = usb_role_switch_register(&port->dev,
668 &role_sx_desc);
669 if (IS_ERR(port->usb_role_sw)) {
670 err = PTR_ERR(port->usb_role_sw);
671 dev_err(&port->dev, "failed to register USB role switch: %d",
672 err);
673 return err;
674 }
675
676 INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
677 usb_role_switch_set_drvdata(port->usb_role_sw, port);
678
679 port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
680 GFP_KERNEL);
681 if (!port->usb_phy.otg)
682 return -ENOMEM;
683
684 lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
685
686 /*
687 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
688 * reference to retrieve usb-phy details.
689 */
690 port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
691 port->usb_phy.dev->driver = port->padctl->dev->driver;
692 port->usb_phy.otg->usb_phy = &port->usb_phy;
693 port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
694 port->usb_phy.otg->set_host = tegra_xusb_set_host;
695
696 err = usb_add_phy_dev(&port->usb_phy);
697 if (err < 0) {
698 dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
699 return err;
700 }
701
702 /* populate connector entry */
703 of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
704
705 return err;
706}
707
708static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
709{
710 struct tegra_xusb_port *port = &usb2->base;
711 struct device_node *np = port->dev.of_node;
712 const char *mode;
713 int err;
714
715 usb2->internal = of_property_read_bool(np, "nvidia,internal");
716
717 if (!of_property_read_string(np, "mode", &mode)) {
718 int err = match_string(modes, ARRAY_SIZE(modes), mode);
719 if (err < 0) {
720 dev_err(&port->dev, "invalid value %s for \"mode\"\n",
721 mode);
722 usb2->mode = USB_DR_MODE_UNKNOWN;
723 } else {
724 usb2->mode = err;
725 }
726 } else {
727 usb2->mode = USB_DR_MODE_HOST;
728 }
729
730 /* usb-role-switch property is mandatory for OTG/Peripheral modes */
731 if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
732 usb2->mode == USB_DR_MODE_OTG) {
733 if (of_property_read_bool(np, "usb-role-switch")) {
734 err = tegra_xusb_setup_usb_role_switch(port);
735 if (err < 0)
736 return err;
737 } else {
738 dev_err(&port->dev, "usb-role-switch not found for %s mode",
739 modes[usb2->mode]);
740 return -EINVAL;
741 }
742 }
743
744 usb2->supply = regulator_get(&port->dev, "vbus");
745 return PTR_ERR_OR_ZERO(usb2->supply);
746}
747
748static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
749 unsigned int index)
750{
751 struct tegra_xusb_usb2_port *usb2;
752 struct device_node *np;
753 int err = 0;
754
755 /*
756 * USB2 ports don't require additional properties, but if the port is
757 * marked as disabled there is no reason to register it.
758 */
759 np = tegra_xusb_find_port_node(padctl, "usb2", index);
760 if (!np || !of_device_is_available(np))
761 goto out;
762
763 usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
764 if (!usb2) {
765 err = -ENOMEM;
766 goto out;
767 }
768
769 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
770 if (err < 0)
771 goto out;
772
773 usb2->base.ops = padctl->soc->ports.usb2.ops;
774
775 usb2->base.lane = usb2->base.ops->map(&usb2->base);
776 if (IS_ERR(usb2->base.lane)) {
777 err = PTR_ERR(usb2->base.lane);
778 goto out;
779 }
780
781 err = tegra_xusb_usb2_port_parse_dt(usb2);
782 if (err < 0) {
783 tegra_xusb_port_unregister(&usb2->base);
784 goto out;
785 }
786
787 list_add_tail(&usb2->base.list, &padctl->ports);
788
789out:
790 of_node_put(np);
791 return err;
792}
793
794void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
795{
796 struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
797
798 kfree(usb2);
799}
800
801void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
802{
803 struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
804
805 regulator_put(usb2->supply);
806}
807
808static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
809{
810 struct tegra_xusb_port *port = &ulpi->base;
811 struct device_node *np = port->dev.of_node;
812
813 ulpi->internal = of_property_read_bool(np, "nvidia,internal");
814
815 return 0;
816}
817
818static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
819 unsigned int index)
820{
821 struct tegra_xusb_ulpi_port *ulpi;
822 struct device_node *np;
823 int err = 0;
824
825 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
826 if (!np || !of_device_is_available(np))
827 goto out;
828
829 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
830 if (!ulpi) {
831 err = -ENOMEM;
832 goto out;
833 }
834
835 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
836 if (err < 0)
837 goto out;
838
839 ulpi->base.ops = padctl->soc->ports.ulpi.ops;
840
841 ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
842 if (IS_ERR(ulpi->base.lane)) {
843 err = PTR_ERR(ulpi->base.lane);
844 goto out;
845 }
846
847 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
848 if (err < 0) {
849 tegra_xusb_port_unregister(&ulpi->base);
850 goto out;
851 }
852
853 list_add_tail(&ulpi->base.list, &padctl->ports);
854
855out:
856 of_node_put(np);
857 return err;
858}
859
860void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
861{
862 struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
863
864 kfree(ulpi);
865}
866
867static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
868{
869 /* XXX */
870 return 0;
871}
872
873static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
874 unsigned int index)
875{
876 struct tegra_xusb_hsic_port *hsic;
877 struct device_node *np;
878 int err = 0;
879
880 np = tegra_xusb_find_port_node(padctl, "hsic", index);
881 if (!np || !of_device_is_available(np))
882 goto out;
883
884 hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
885 if (!hsic) {
886 err = -ENOMEM;
887 goto out;
888 }
889
890 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
891 if (err < 0)
892 goto out;
893
894 hsic->base.ops = padctl->soc->ports.hsic.ops;
895
896 hsic->base.lane = hsic->base.ops->map(&hsic->base);
897 if (IS_ERR(hsic->base.lane)) {
898 err = PTR_ERR(hsic->base.lane);
899 goto out;
900 }
901
902 err = tegra_xusb_hsic_port_parse_dt(hsic);
903 if (err < 0) {
904 tegra_xusb_port_unregister(&hsic->base);
905 goto out;
906 }
907
908 list_add_tail(&hsic->base.list, &padctl->ports);
909
910out:
911 of_node_put(np);
912 return err;
913}
914
915void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
916{
917 struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
918
919 kfree(hsic);
920}
921
922static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
923{
924 struct tegra_xusb_port *port = &usb3->base;
925 struct device_node *np = port->dev.of_node;
926 enum usb_device_speed maximum_speed;
927 u32 value;
928 int err;
929
930 err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
931 if (err < 0) {
932 dev_err(&port->dev, "failed to read port: %d\n", err);
933 return err;
934 }
935
936 usb3->port = value;
937
938 usb3->internal = of_property_read_bool(np, "nvidia,internal");
939
940 if (device_property_present(&port->dev, "maximum-speed")) {
941 maximum_speed = usb_get_maximum_speed(&port->dev);
942 if (maximum_speed == USB_SPEED_SUPER)
943 usb3->disable_gen2 = true;
944 else if (maximum_speed == USB_SPEED_SUPER_PLUS)
945 usb3->disable_gen2 = false;
946 else
947 return -EINVAL;
948 }
949
950 usb3->supply = regulator_get(&port->dev, "vbus");
951 return PTR_ERR_OR_ZERO(usb3->supply);
952}
953
954static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
955 unsigned int index)
956{
957 struct tegra_xusb_usb3_port *usb3;
958 struct device_node *np;
959 int err = 0;
960
961 /*
962 * If there is no supplemental configuration in the device tree the
963 * port is unusable. But it is valid to configure only a single port,
964 * hence return 0 instead of an error to allow ports to be optional.
965 */
966 np = tegra_xusb_find_port_node(padctl, "usb3", index);
967 if (!np || !of_device_is_available(np))
968 goto out;
969
970 usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
971 if (!usb3) {
972 err = -ENOMEM;
973 goto out;
974 }
975
976 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
977 if (err < 0)
978 goto out;
979
980 usb3->base.ops = padctl->soc->ports.usb3.ops;
981
982 usb3->base.lane = usb3->base.ops->map(&usb3->base);
983 if (IS_ERR(usb3->base.lane)) {
984 err = PTR_ERR(usb3->base.lane);
985 goto out;
986 }
987
988 err = tegra_xusb_usb3_port_parse_dt(usb3);
989 if (err < 0) {
990 tegra_xusb_port_unregister(&usb3->base);
991 goto out;
992 }
993
994 list_add_tail(&usb3->base.list, &padctl->ports);
995
996out:
997 of_node_put(np);
998 return err;
999}
1000
1001void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1002{
1003 struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1004
1005 kfree(usb3);
1006}
1007
1008void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
1009{
1010 struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1011
1012 regulator_put(usb3->supply);
1013}
1014
1015static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1016{
1017 struct tegra_xusb_port *port, *tmp;
1018
1019 list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1020 list_del(&port->list);
1021 tegra_xusb_port_unregister(port);
1022 }
1023}
1024
1025static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1026{
1027 struct device_node *np;
1028 unsigned int i;
1029
1030 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1031 np = tegra_xusb_find_port_node(padctl, "usb3", i);
1032 if (!np || !of_device_is_available(np))
1033 return i;
1034 }
1035
1036 return -ENODEV;
1037}
1038
1039static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1040{
1041 unsigned int i;
1042 struct tegra_xusb_usb3_port *usb3;
1043 struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1044
1045 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1046 usb3 = tegra_xusb_find_usb3_port(padctl, i);
1047 if (usb3 && usb3->port == usb2->base.index)
1048 return true;
1049 }
1050
1051 return false;
1052}
1053
1054static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1055{
1056 int fake;
1057
1058 /* Disable usb3_port_fake usage by default and assign if needed */
1059 usb2->usb3_port_fake = -1;
1060
1061 if ((usb2->mode == USB_DR_MODE_OTG ||
1062 usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1063 !tegra_xusb_port_is_companion(usb2)) {
1064 fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1065 if (fake < 0) {
1066 dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1067 return -ENODEV;
1068 }
1069
1070 dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1071 usb2->usb3_port_fake = fake;
1072 }
1073
1074 return 0;
1075}
1076
1077static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1078{
1079 struct tegra_xusb_port *port;
1080 struct tegra_xusb_usb2_port *usb2;
1081 unsigned int i;
1082 int err = 0;
1083
1084 mutex_lock(&padctl->lock);
1085
1086 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1087 err = tegra_xusb_add_usb2_port(padctl, i);
1088 if (err < 0)
1089 goto remove_ports;
1090 }
1091
1092 for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1093 err = tegra_xusb_add_ulpi_port(padctl, i);
1094 if (err < 0)
1095 goto remove_ports;
1096 }
1097
1098 for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1099 err = tegra_xusb_add_hsic_port(padctl, i);
1100 if (err < 0)
1101 goto remove_ports;
1102 }
1103
1104 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1105 err = tegra_xusb_add_usb3_port(padctl, i);
1106 if (err < 0)
1107 goto remove_ports;
1108 }
1109
1110 if (padctl->soc->need_fake_usb3_port) {
1111 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1112 usb2 = tegra_xusb_find_usb2_port(padctl, i);
1113 if (!usb2)
1114 continue;
1115
1116 err = tegra_xusb_update_usb3_fake_port(usb2);
1117 if (err < 0)
1118 goto remove_ports;
1119 }
1120 }
1121
1122 list_for_each_entry(port, &padctl->ports, list) {
1123 err = port->ops->enable(port);
1124 if (err < 0)
1125 dev_err(padctl->dev, "failed to enable port %s: %d\n",
1126 dev_name(&port->dev), err);
1127 }
1128
1129 goto unlock;
1130
1131remove_ports:
1132 __tegra_xusb_remove_ports(padctl);
1133unlock:
1134 mutex_unlock(&padctl->lock);
1135 return err;
1136}
1137
1138static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1139{
1140 mutex_lock(&padctl->lock);
1141 __tegra_xusb_remove_ports(padctl);
1142 mutex_unlock(&padctl->lock);
1143}
1144
1145static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1146{
1147 struct device_node *np = pdev->dev.of_node;
1148 const struct tegra_xusb_padctl_soc *soc;
1149 struct tegra_xusb_padctl *padctl;
1150 const struct of_device_id *match;
1151 struct resource *res;
1152 int err;
1153
1154 /* for backwards compatibility with old device trees */
1155 np = of_get_child_by_name(np, "pads");
1156 if (!np) {
1157 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1158 return tegra_xusb_padctl_legacy_probe(pdev);
1159 }
1160
1161 of_node_put(np);
1162
1163 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1164 soc = match->data;
1165
1166 padctl = soc->ops->probe(&pdev->dev, soc);
1167 if (IS_ERR(padctl))
1168 return PTR_ERR(padctl);
1169
1170 platform_set_drvdata(pdev, padctl);
1171 INIT_LIST_HEAD(&padctl->ports);
1172 INIT_LIST_HEAD(&padctl->lanes);
1173 INIT_LIST_HEAD(&padctl->pads);
1174 mutex_init(&padctl->lock);
1175
1176 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1177 padctl->regs = devm_ioremap_resource(&pdev->dev, res);
1178 if (IS_ERR(padctl->regs)) {
1179 err = PTR_ERR(padctl->regs);
1180 goto remove;
1181 }
1182
1183 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1184 if (IS_ERR(padctl->rst)) {
1185 err = PTR_ERR(padctl->rst);
1186 goto remove;
1187 }
1188
1189 padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1190 sizeof(*padctl->supplies), GFP_KERNEL);
1191 if (!padctl->supplies) {
1192 err = -ENOMEM;
1193 goto remove;
1194 }
1195
1196 regulator_bulk_set_supply_names(padctl->supplies,
1197 padctl->soc->supply_names,
1198 padctl->soc->num_supplies);
1199
1200 err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1201 padctl->supplies);
1202 if (err < 0) {
1203 dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
1204 goto remove;
1205 }
1206
1207 err = reset_control_deassert(padctl->rst);
1208 if (err < 0)
1209 goto remove;
1210
1211 err = regulator_bulk_enable(padctl->soc->num_supplies,
1212 padctl->supplies);
1213 if (err < 0) {
1214 dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1215 goto reset;
1216 }
1217
1218 err = tegra_xusb_setup_pads(padctl);
1219 if (err < 0) {
1220 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1221 goto power_down;
1222 }
1223
1224 err = tegra_xusb_setup_ports(padctl);
1225 if (err) {
1226 const char *level = KERN_ERR;
1227
1228 if (err == -EPROBE_DEFER)
1229 level = KERN_DEBUG;
1230
1231 dev_printk(level, &pdev->dev,
1232 dev_fmt("failed to setup XUSB ports: %d\n"), err);
1233 goto remove_pads;
1234 }
1235
1236 return 0;
1237
1238remove_pads:
1239 tegra_xusb_remove_pads(padctl);
1240power_down:
1241 regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1242reset:
1243 reset_control_assert(padctl->rst);
1244remove:
1245 soc->ops->remove(padctl);
1246 return err;
1247}
1248
1249static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1250{
1251 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1252 int err;
1253
1254 tegra_xusb_remove_ports(padctl);
1255 tegra_xusb_remove_pads(padctl);
1256
1257 err = regulator_bulk_disable(padctl->soc->num_supplies,
1258 padctl->supplies);
1259 if (err < 0)
1260 dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1261
1262 err = reset_control_assert(padctl->rst);
1263 if (err < 0)
1264 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1265
1266 padctl->soc->ops->remove(padctl);
1267
1268 return err;
1269}
1270
1271static struct platform_driver tegra_xusb_padctl_driver = {
1272 .driver = {
1273 .name = "tegra-xusb-padctl",
1274 .of_match_table = tegra_xusb_padctl_of_match,
1275 },
1276 .probe = tegra_xusb_padctl_probe,
1277 .remove = tegra_xusb_padctl_remove,
1278};
1279module_platform_driver(tegra_xusb_padctl_driver);
1280
1281struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1282{
1283 struct tegra_xusb_padctl *padctl;
1284 struct platform_device *pdev;
1285 struct device_node *np;
1286
1287 np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1288 if (!np)
1289 return ERR_PTR(-EINVAL);
1290
1291 /*
1292 * This is slightly ugly. A better implementation would be to keep a
1293 * registry of pad controllers, but since there will almost certainly
1294 * only ever be one per SoC that would be a little overkill.
1295 */
1296 pdev = of_find_device_by_node(np);
1297 if (!pdev) {
1298 of_node_put(np);
1299 return ERR_PTR(-ENODEV);
1300 }
1301
1302 of_node_put(np);
1303
1304 padctl = platform_get_drvdata(pdev);
1305 if (!padctl) {
1306 put_device(&pdev->dev);
1307 return ERR_PTR(-EPROBE_DEFER);
1308 }
1309
1310 return padctl;
1311}
1312EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1313
1314void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1315{
1316 if (padctl)
1317 put_device(padctl->dev);
1318}
1319EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1320
1321int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1322 unsigned int port)
1323{
1324 if (padctl->soc->ops->usb3_save_context)
1325 return padctl->soc->ops->usb3_save_context(padctl, port);
1326
1327 return -ENOSYS;
1328}
1329EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1330
1331int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1332 unsigned int port, bool idle)
1333{
1334 if (padctl->soc->ops->hsic_set_idle)
1335 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1336
1337 return -ENOSYS;
1338}
1339EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1340
1341int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1342 unsigned int port, bool enable)
1343{
1344 if (padctl->soc->ops->usb3_set_lfps_detect)
1345 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1346 enable);
1347
1348 return -ENOSYS;
1349}
1350EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1351
1352int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1353 bool val)
1354{
1355 if (padctl->soc->ops->vbus_override)
1356 return padctl->soc->ops->vbus_override(padctl, val);
1357
1358 return -ENOTSUPP;
1359}
1360EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1361
1362int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1363{
1364 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1365 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1366
1367 if (padctl->soc->ops->utmi_port_reset)
1368 return padctl->soc->ops->utmi_port_reset(phy);
1369
1370 return -ENOTSUPP;
1371}
1372EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1373
1374int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1375 unsigned int port)
1376{
1377 struct tegra_xusb_usb2_port *usb2;
1378 struct tegra_xusb_usb3_port *usb3;
1379 int i;
1380
1381 usb2 = tegra_xusb_find_usb2_port(padctl, port);
1382 if (!usb2)
1383 return -EINVAL;
1384
1385 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1386 usb3 = tegra_xusb_find_usb3_port(padctl, i);
1387 if (usb3 && usb3->port == usb2->base.index)
1388 return usb3->base.index;
1389 }
1390
1391 return -ENODEV;
1392}
1393EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1394
1395MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1396MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1397MODULE_LICENSE("GPL v2");
1/*
2 * Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 */
13
14#include <linux/delay.h>
15#include <linux/io.h>
16#include <linux/mailbox_client.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
20#include <linux/phy/phy.h>
21#include <linux/phy/tegra/xusb.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/consumer.h>
24#include <linux/reset.h>
25#include <linux/slab.h>
26#include <linux/workqueue.h>
27
28#include <soc/tegra/fuse.h>
29
30#include "xusb.h"
31
32static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
33 struct of_phandle_args *args)
34{
35 struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
36 struct phy *phy = NULL;
37 unsigned int i;
38
39 if (args->args_count != 0)
40 return ERR_PTR(-EINVAL);
41
42 for (i = 0; i < pad->soc->num_lanes; i++) {
43 if (!pad->lanes[i])
44 continue;
45
46 if (pad->lanes[i]->dev.of_node == args->np) {
47 phy = pad->lanes[i];
48 break;
49 }
50 }
51
52 if (phy == NULL)
53 phy = ERR_PTR(-ENODEV);
54
55 return phy;
56}
57
58static const struct of_device_id tegra_xusb_padctl_of_match[] = {
59#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
60 {
61 .compatible = "nvidia,tegra124-xusb-padctl",
62 .data = &tegra124_xusb_padctl_soc,
63 },
64#endif
65#if defined(CONFIG_ARCH_TEGRA_210_SOC)
66 {
67 .compatible = "nvidia,tegra210-xusb-padctl",
68 .data = &tegra210_xusb_padctl_soc,
69 },
70#endif
71 { }
72};
73MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
74
75static struct device_node *
76tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
77{
78 /*
79 * of_find_node_by_name() drops a reference, so make sure to grab one.
80 */
81 struct device_node *np = of_node_get(padctl->dev->of_node);
82
83 np = of_find_node_by_name(np, "pads");
84 if (np)
85 np = of_find_node_by_name(np, name);
86
87 return np;
88}
89
90static struct device_node *
91tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
92{
93 /*
94 * of_find_node_by_name() drops a reference, so make sure to grab one.
95 */
96 struct device_node *np = of_node_get(pad->dev.of_node);
97
98 np = of_find_node_by_name(np, "lanes");
99 if (!np)
100 return NULL;
101
102 return of_find_node_by_name(np, pad->soc->lanes[index].name);
103}
104
105static int
106tegra_xusb_lane_lookup_function(struct tegra_xusb_lane *lane,
107 const char *function)
108{
109 unsigned int i;
110
111 for (i = 0; i < lane->soc->num_funcs; i++)
112 if (strcmp(function, lane->soc->funcs[i]) == 0)
113 return i;
114
115 return -EINVAL;
116}
117
118int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
119 struct device_node *np)
120{
121 struct device *dev = &lane->pad->dev;
122 const char *function;
123 int err;
124
125 err = of_property_read_string(np, "nvidia,function", &function);
126 if (err < 0)
127 return err;
128
129 err = tegra_xusb_lane_lookup_function(lane, function);
130 if (err < 0) {
131 dev_err(dev, "invalid function \"%s\" for lane \"%s\"\n",
132 function, np->name);
133 return err;
134 }
135
136 lane->function = err;
137
138 return 0;
139}
140
141static void tegra_xusb_lane_destroy(struct phy *phy)
142{
143 if (phy) {
144 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
145
146 lane->pad->ops->remove(lane);
147 phy_destroy(phy);
148 }
149}
150
151static void tegra_xusb_pad_release(struct device *dev)
152{
153 struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
154
155 pad->soc->ops->remove(pad);
156}
157
158static struct device_type tegra_xusb_pad_type = {
159 .release = tegra_xusb_pad_release,
160};
161
162int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
163 struct tegra_xusb_padctl *padctl,
164 struct device_node *np)
165{
166 int err;
167
168 device_initialize(&pad->dev);
169 INIT_LIST_HEAD(&pad->list);
170 pad->dev.parent = padctl->dev;
171 pad->dev.type = &tegra_xusb_pad_type;
172 pad->dev.of_node = np;
173 pad->padctl = padctl;
174
175 err = dev_set_name(&pad->dev, "%s", pad->soc->name);
176 if (err < 0)
177 goto unregister;
178
179 err = device_add(&pad->dev);
180 if (err < 0)
181 goto unregister;
182
183 return 0;
184
185unregister:
186 device_unregister(&pad->dev);
187 return err;
188}
189
190int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
191 const struct phy_ops *ops)
192{
193 struct device_node *children;
194 struct phy *lane;
195 unsigned int i;
196 int err;
197
198 children = of_find_node_by_name(pad->dev.of_node, "lanes");
199 if (!children)
200 return -ENODEV;
201
202 pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
203 GFP_KERNEL);
204 if (!pad->lanes) {
205 of_node_put(children);
206 return -ENOMEM;
207 }
208
209 for (i = 0; i < pad->soc->num_lanes; i++) {
210 struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
211 struct tegra_xusb_lane *lane;
212
213 /* skip disabled lanes */
214 if (!np || !of_device_is_available(np)) {
215 of_node_put(np);
216 continue;
217 }
218
219 pad->lanes[i] = phy_create(&pad->dev, np, ops);
220 if (IS_ERR(pad->lanes[i])) {
221 err = PTR_ERR(pad->lanes[i]);
222 of_node_put(np);
223 goto remove;
224 }
225
226 lane = pad->ops->probe(pad, np, i);
227 if (IS_ERR(lane)) {
228 phy_destroy(pad->lanes[i]);
229 err = PTR_ERR(lane);
230 goto remove;
231 }
232
233 list_add_tail(&lane->list, &pad->padctl->lanes);
234 phy_set_drvdata(pad->lanes[i], lane);
235 }
236
237 pad->provider = of_phy_provider_register_full(&pad->dev, children,
238 tegra_xusb_pad_of_xlate);
239 if (IS_ERR(pad->provider)) {
240 err = PTR_ERR(pad->provider);
241 goto remove;
242 }
243
244 return 0;
245
246remove:
247 while (i--)
248 tegra_xusb_lane_destroy(pad->lanes[i]);
249
250 of_node_put(children);
251
252 return err;
253}
254
255void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
256{
257 unsigned int i = pad->soc->num_lanes;
258
259 of_phy_provider_unregister(pad->provider);
260
261 while (i--)
262 tegra_xusb_lane_destroy(pad->lanes[i]);
263
264 device_unregister(&pad->dev);
265}
266
267static struct tegra_xusb_pad *
268tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
269 const struct tegra_xusb_pad_soc *soc)
270{
271 struct tegra_xusb_pad *pad;
272 struct device_node *np;
273 int err;
274
275 np = tegra_xusb_find_pad_node(padctl, soc->name);
276 if (!np || !of_device_is_available(np))
277 return NULL;
278
279 pad = soc->ops->probe(padctl, soc, np);
280 if (IS_ERR(pad)) {
281 err = PTR_ERR(pad);
282 dev_err(padctl->dev, "failed to create pad %s: %d\n",
283 soc->name, err);
284 return ERR_PTR(err);
285 }
286
287 /* XXX move this into ->probe() to avoid string comparison */
288 if (strcmp(soc->name, "pcie") == 0)
289 padctl->pcie = pad;
290
291 if (strcmp(soc->name, "sata") == 0)
292 padctl->sata = pad;
293
294 if (strcmp(soc->name, "usb2") == 0)
295 padctl->usb2 = pad;
296
297 if (strcmp(soc->name, "ulpi") == 0)
298 padctl->ulpi = pad;
299
300 if (strcmp(soc->name, "hsic") == 0)
301 padctl->hsic = pad;
302
303 return pad;
304}
305
306static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
307{
308 struct tegra_xusb_pad *pad, *tmp;
309
310 list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
311 list_del(&pad->list);
312 tegra_xusb_pad_unregister(pad);
313 }
314}
315
316static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
317{
318 mutex_lock(&padctl->lock);
319 __tegra_xusb_remove_pads(padctl);
320 mutex_unlock(&padctl->lock);
321}
322
323static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
324{
325 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
326 const struct tegra_xusb_lane_soc *soc = lane->soc;
327 u32 value;
328
329 /* choose function */
330 value = padctl_readl(padctl, soc->offset);
331 value &= ~(soc->mask << soc->shift);
332 value |= lane->function << soc->shift;
333 padctl_writel(padctl, value, soc->offset);
334}
335
336static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
337{
338 unsigned int i;
339
340 for (i = 0; i < pad->soc->num_lanes; i++) {
341 struct tegra_xusb_lane *lane;
342
343 if (pad->lanes[i]) {
344 lane = phy_get_drvdata(pad->lanes[i]);
345 tegra_xusb_lane_program(lane);
346 }
347 }
348}
349
350static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
351{
352 struct tegra_xusb_pad *pad;
353 unsigned int i;
354
355 mutex_lock(&padctl->lock);
356
357 for (i = 0; i < padctl->soc->num_pads; i++) {
358 const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
359 int err;
360
361 pad = tegra_xusb_pad_create(padctl, soc);
362 if (IS_ERR(pad)) {
363 err = PTR_ERR(pad);
364 dev_err(padctl->dev, "failed to create pad %s: %d\n",
365 soc->name, err);
366 __tegra_xusb_remove_pads(padctl);
367 mutex_unlock(&padctl->lock);
368 return err;
369 }
370
371 if (!pad)
372 continue;
373
374 list_add_tail(&pad->list, &padctl->pads);
375 }
376
377 list_for_each_entry(pad, &padctl->pads, list)
378 tegra_xusb_pad_program(pad);
379
380 mutex_unlock(&padctl->lock);
381 return 0;
382}
383
384static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
385 const char *function)
386{
387 const char *func = lane->soc->funcs[lane->function];
388
389 return strcmp(function, func) == 0;
390}
391
392struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
393 const char *type,
394 unsigned int index)
395{
396 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
397 char *name;
398
399 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
400 if (!name)
401 return ERR_PTR(-ENOMEM);
402
403 list_for_each_entry(lane, &padctl->lanes, list) {
404 if (strcmp(lane->soc->name, name) == 0) {
405 hit = lane;
406 break;
407 }
408 }
409
410 kfree(name);
411 return hit;
412}
413
414struct tegra_xusb_lane *
415tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
416 const struct tegra_xusb_lane_map *map,
417 const char *function)
418{
419 struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
420
421 for (map = map; map->type; map++) {
422 if (port->index != map->port)
423 continue;
424
425 lane = tegra_xusb_find_lane(port->padctl, map->type,
426 map->index);
427 if (IS_ERR(lane))
428 continue;
429
430 if (!tegra_xusb_lane_check(lane, function))
431 continue;
432
433 if (!IS_ERR(match))
434 dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
435 map->type, map->index, match->soc->name);
436 else
437 match = lane;
438 }
439
440 return match;
441}
442
443static struct device_node *
444tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
445 unsigned int index)
446{
447 /*
448 * of_find_node_by_name() drops a reference, so make sure to grab one.
449 */
450 struct device_node *np = of_node_get(padctl->dev->of_node);
451
452 np = of_find_node_by_name(np, "ports");
453 if (np) {
454 char *name;
455
456 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
457 np = of_find_node_by_name(np, name);
458 kfree(name);
459 }
460
461 return np;
462}
463
464struct tegra_xusb_port *
465tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
466 unsigned int index)
467{
468 struct tegra_xusb_port *port;
469 struct device_node *np;
470
471 np = tegra_xusb_find_port_node(padctl, type, index);
472 if (!np)
473 return NULL;
474
475 list_for_each_entry(port, &padctl->ports, list) {
476 if (np == port->dev.of_node) {
477 of_node_put(np);
478 return port;
479 }
480 }
481
482 of_node_put(np);
483
484 return NULL;
485}
486
487struct tegra_xusb_usb2_port *
488tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
489{
490 struct tegra_xusb_port *port;
491
492 port = tegra_xusb_find_port(padctl, "usb2", index);
493 if (port)
494 return to_usb2_port(port);
495
496 return NULL;
497}
498
499struct tegra_xusb_usb3_port *
500tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
501{
502 struct tegra_xusb_port *port;
503
504 port = tegra_xusb_find_port(padctl, "usb3", index);
505 if (port)
506 return to_usb3_port(port);
507
508 return NULL;
509}
510
511static void tegra_xusb_port_release(struct device *dev)
512{
513}
514
515static struct device_type tegra_xusb_port_type = {
516 .release = tegra_xusb_port_release,
517};
518
519static int tegra_xusb_port_init(struct tegra_xusb_port *port,
520 struct tegra_xusb_padctl *padctl,
521 struct device_node *np,
522 const char *name,
523 unsigned int index)
524{
525 int err;
526
527 INIT_LIST_HEAD(&port->list);
528 port->padctl = padctl;
529 port->index = index;
530
531 device_initialize(&port->dev);
532 port->dev.type = &tegra_xusb_port_type;
533 port->dev.of_node = of_node_get(np);
534 port->dev.parent = padctl->dev;
535
536 err = dev_set_name(&port->dev, "%s-%u", name, index);
537 if (err < 0)
538 goto unregister;
539
540 err = device_add(&port->dev);
541 if (err < 0)
542 goto unregister;
543
544 return 0;
545
546unregister:
547 device_unregister(&port->dev);
548 return err;
549}
550
551static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
552{
553 device_unregister(&port->dev);
554}
555
556static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
557{
558 struct tegra_xusb_port *port = &usb2->base;
559 struct device_node *np = port->dev.of_node;
560
561 usb2->internal = of_property_read_bool(np, "nvidia,internal");
562
563 usb2->supply = devm_regulator_get(&port->dev, "vbus");
564 return PTR_ERR_OR_ZERO(usb2->supply);
565}
566
567static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
568 unsigned int index)
569{
570 struct tegra_xusb_usb2_port *usb2;
571 struct device_node *np;
572 int err = 0;
573
574 /*
575 * USB2 ports don't require additional properties, but if the port is
576 * marked as disabled there is no reason to register it.
577 */
578 np = tegra_xusb_find_port_node(padctl, "usb2", index);
579 if (!np || !of_device_is_available(np))
580 goto out;
581
582 usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
583 if (!usb2) {
584 err = -ENOMEM;
585 goto out;
586 }
587
588 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
589 if (err < 0)
590 goto out;
591
592 usb2->base.ops = padctl->soc->ports.usb2.ops;
593
594 usb2->base.lane = usb2->base.ops->map(&usb2->base);
595 if (IS_ERR(usb2->base.lane)) {
596 err = PTR_ERR(usb2->base.lane);
597 goto out;
598 }
599
600 err = tegra_xusb_usb2_port_parse_dt(usb2);
601 if (err < 0) {
602 tegra_xusb_port_unregister(&usb2->base);
603 goto out;
604 }
605
606 list_add_tail(&usb2->base.list, &padctl->ports);
607
608out:
609 of_node_put(np);
610 return err;
611}
612
613static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
614{
615 struct tegra_xusb_port *port = &ulpi->base;
616 struct device_node *np = port->dev.of_node;
617
618 ulpi->internal = of_property_read_bool(np, "nvidia,internal");
619
620 return 0;
621}
622
623static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
624 unsigned int index)
625{
626 struct tegra_xusb_ulpi_port *ulpi;
627 struct device_node *np;
628 int err = 0;
629
630 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
631 if (!np || !of_device_is_available(np))
632 goto out;
633
634 ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
635 if (!ulpi) {
636 err = -ENOMEM;
637 goto out;
638 }
639
640 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
641 if (err < 0)
642 goto out;
643
644 ulpi->base.ops = padctl->soc->ports.ulpi.ops;
645
646 ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
647 if (IS_ERR(ulpi->base.lane)) {
648 err = PTR_ERR(ulpi->base.lane);
649 goto out;
650 }
651
652 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
653 if (err < 0) {
654 tegra_xusb_port_unregister(&ulpi->base);
655 goto out;
656 }
657
658 list_add_tail(&ulpi->base.list, &padctl->ports);
659
660out:
661 of_node_put(np);
662 return err;
663}
664
665static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
666{
667 /* XXX */
668 return 0;
669}
670
671static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
672 unsigned int index)
673{
674 struct tegra_xusb_hsic_port *hsic;
675 struct device_node *np;
676 int err = 0;
677
678 np = tegra_xusb_find_port_node(padctl, "hsic", index);
679 if (!np || !of_device_is_available(np))
680 goto out;
681
682 hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
683 if (!hsic) {
684 err = -ENOMEM;
685 goto out;
686 }
687
688 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
689 if (err < 0)
690 goto out;
691
692 hsic->base.ops = padctl->soc->ports.hsic.ops;
693
694 hsic->base.lane = hsic->base.ops->map(&hsic->base);
695 if (IS_ERR(hsic->base.lane)) {
696 err = PTR_ERR(hsic->base.lane);
697 goto out;
698 }
699
700 err = tegra_xusb_hsic_port_parse_dt(hsic);
701 if (err < 0) {
702 tegra_xusb_port_unregister(&hsic->base);
703 goto out;
704 }
705
706 list_add_tail(&hsic->base.list, &padctl->ports);
707
708out:
709 of_node_put(np);
710 return err;
711}
712
713static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
714{
715 struct tegra_xusb_port *port = &usb3->base;
716 struct device_node *np = port->dev.of_node;
717 u32 value;
718 int err;
719
720 err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
721 if (err < 0) {
722 dev_err(&port->dev, "failed to read port: %d\n", err);
723 return err;
724 }
725
726 usb3->port = value;
727
728 usb3->internal = of_property_read_bool(np, "nvidia,internal");
729
730 usb3->supply = devm_regulator_get(&port->dev, "vbus");
731 return PTR_ERR_OR_ZERO(usb3->supply);
732}
733
734static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
735 unsigned int index)
736{
737 struct tegra_xusb_usb3_port *usb3;
738 struct device_node *np;
739 int err = 0;
740
741 /*
742 * If there is no supplemental configuration in the device tree the
743 * port is unusable. But it is valid to configure only a single port,
744 * hence return 0 instead of an error to allow ports to be optional.
745 */
746 np = tegra_xusb_find_port_node(padctl, "usb3", index);
747 if (!np || !of_device_is_available(np))
748 goto out;
749
750 usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
751 if (!usb3) {
752 err = -ENOMEM;
753 goto out;
754 }
755
756 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
757 if (err < 0)
758 goto out;
759
760 usb3->base.ops = padctl->soc->ports.usb3.ops;
761
762 usb3->base.lane = usb3->base.ops->map(&usb3->base);
763 if (IS_ERR(usb3->base.lane)) {
764 err = PTR_ERR(usb3->base.lane);
765 goto out;
766 }
767
768 err = tegra_xusb_usb3_port_parse_dt(usb3);
769 if (err < 0) {
770 tegra_xusb_port_unregister(&usb3->base);
771 goto out;
772 }
773
774 list_add_tail(&usb3->base.list, &padctl->ports);
775
776out:
777 of_node_put(np);
778 return err;
779}
780
781static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
782{
783 struct tegra_xusb_port *port, *tmp;
784
785 list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
786 list_del(&port->list);
787 tegra_xusb_port_unregister(port);
788 }
789}
790
791static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
792{
793 struct tegra_xusb_port *port;
794 unsigned int i;
795 int err = 0;
796
797 mutex_lock(&padctl->lock);
798
799 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
800 err = tegra_xusb_add_usb2_port(padctl, i);
801 if (err < 0)
802 goto remove_ports;
803 }
804
805 for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
806 err = tegra_xusb_add_ulpi_port(padctl, i);
807 if (err < 0)
808 goto remove_ports;
809 }
810
811 for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
812 err = tegra_xusb_add_hsic_port(padctl, i);
813 if (err < 0)
814 goto remove_ports;
815 }
816
817 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
818 err = tegra_xusb_add_usb3_port(padctl, i);
819 if (err < 0)
820 goto remove_ports;
821 }
822
823 list_for_each_entry(port, &padctl->ports, list) {
824 err = port->ops->enable(port);
825 if (err < 0)
826 dev_err(padctl->dev, "failed to enable port %s: %d\n",
827 dev_name(&port->dev), err);
828 }
829
830 goto unlock;
831
832remove_ports:
833 __tegra_xusb_remove_ports(padctl);
834unlock:
835 mutex_unlock(&padctl->lock);
836 return err;
837}
838
839static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
840{
841 mutex_lock(&padctl->lock);
842 __tegra_xusb_remove_ports(padctl);
843 mutex_unlock(&padctl->lock);
844}
845
846static int tegra_xusb_padctl_probe(struct platform_device *pdev)
847{
848 struct device_node *np = of_node_get(pdev->dev.of_node);
849 const struct tegra_xusb_padctl_soc *soc;
850 struct tegra_xusb_padctl *padctl;
851 const struct of_device_id *match;
852 struct resource *res;
853 int err;
854
855 /* for backwards compatibility with old device trees */
856 np = of_find_node_by_name(np, "pads");
857 if (!np) {
858 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
859 return tegra_xusb_padctl_legacy_probe(pdev);
860 }
861
862 of_node_put(np);
863
864 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
865 soc = match->data;
866
867 padctl = soc->ops->probe(&pdev->dev, soc);
868 if (IS_ERR(padctl))
869 return PTR_ERR(padctl);
870
871 platform_set_drvdata(pdev, padctl);
872 INIT_LIST_HEAD(&padctl->ports);
873 INIT_LIST_HEAD(&padctl->lanes);
874 INIT_LIST_HEAD(&padctl->pads);
875 mutex_init(&padctl->lock);
876
877 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
878 padctl->regs = devm_ioremap_resource(&pdev->dev, res);
879 if (IS_ERR(padctl->regs)) {
880 err = PTR_ERR(padctl->regs);
881 goto remove;
882 }
883
884 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
885 if (IS_ERR(padctl->rst)) {
886 err = PTR_ERR(padctl->rst);
887 goto remove;
888 }
889
890 err = reset_control_deassert(padctl->rst);
891 if (err < 0)
892 goto remove;
893
894 err = tegra_xusb_setup_pads(padctl);
895 if (err < 0) {
896 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
897 goto reset;
898 }
899
900 err = tegra_xusb_setup_ports(padctl);
901 if (err) {
902 dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
903 goto remove_pads;
904 }
905
906 return 0;
907
908remove_pads:
909 tegra_xusb_remove_pads(padctl);
910reset:
911 reset_control_assert(padctl->rst);
912remove:
913 soc->ops->remove(padctl);
914 return err;
915}
916
917static int tegra_xusb_padctl_remove(struct platform_device *pdev)
918{
919 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
920 int err;
921
922 tegra_xusb_remove_ports(padctl);
923 tegra_xusb_remove_pads(padctl);
924
925 err = reset_control_assert(padctl->rst);
926 if (err < 0)
927 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
928
929 padctl->soc->ops->remove(padctl);
930
931 return err;
932}
933
934static struct platform_driver tegra_xusb_padctl_driver = {
935 .driver = {
936 .name = "tegra-xusb-padctl",
937 .of_match_table = tegra_xusb_padctl_of_match,
938 },
939 .probe = tegra_xusb_padctl_probe,
940 .remove = tegra_xusb_padctl_remove,
941};
942module_platform_driver(tegra_xusb_padctl_driver);
943
944struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
945{
946 struct tegra_xusb_padctl *padctl;
947 struct platform_device *pdev;
948 struct device_node *np;
949
950 np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
951 if (!np)
952 return ERR_PTR(-EINVAL);
953
954 /*
955 * This is slightly ugly. A better implementation would be to keep a
956 * registry of pad controllers, but since there will almost certainly
957 * only ever be one per SoC that would be a little overkill.
958 */
959 pdev = of_find_device_by_node(np);
960 if (!pdev) {
961 of_node_put(np);
962 return ERR_PTR(-ENODEV);
963 }
964
965 of_node_put(np);
966
967 padctl = platform_get_drvdata(pdev);
968 if (!padctl) {
969 put_device(&pdev->dev);
970 return ERR_PTR(-EPROBE_DEFER);
971 }
972
973 return padctl;
974}
975EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
976
977void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
978{
979 if (padctl)
980 put_device(padctl->dev);
981}
982EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
983
984int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
985 unsigned int port)
986{
987 if (padctl->soc->ops->usb3_save_context)
988 return padctl->soc->ops->usb3_save_context(padctl, port);
989
990 return -ENOSYS;
991}
992EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
993
994int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
995 unsigned int port, bool idle)
996{
997 if (padctl->soc->ops->hsic_set_idle)
998 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
999
1000 return -ENOSYS;
1001}
1002EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1003
1004int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1005 unsigned int port, bool enable)
1006{
1007 if (padctl->soc->ops->usb3_set_lfps_detect)
1008 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1009 enable);
1010
1011 return -ENOSYS;
1012}
1013EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1014
1015MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1016MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1017MODULE_LICENSE("GPL v2");