GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / phy / tegra / xusb.c
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
32 static 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
58 static 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 };
73 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
74
75 static struct device_node *
76 tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
77 {
78         struct device_node *pads, *np;
79
80         pads = of_get_child_by_name(padctl->dev->of_node, "pads");
81         if (!pads)
82                 return NULL;
83
84         np = of_get_child_by_name(pads, name);
85         of_node_put(pads);
86
87         return np;
88 }
89
90 static struct device_node *
91 tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
92 {
93         struct device_node *np, *lanes;
94
95         lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
96         if (!lanes)
97                 return NULL;
98
99         np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
100         of_node_put(lanes);
101
102         return np;
103 }
104
105 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
106                              struct device_node *np)
107 {
108         struct device *dev = &lane->pad->dev;
109         const char *function;
110         int err;
111
112         err = of_property_read_string(np, "nvidia,function", &function);
113         if (err < 0)
114                 return err;
115
116         err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
117         if (err < 0) {
118                 dev_err(dev, "invalid function \"%s\" for lane \"%s\"\n",
119                         function, np->name);
120                 return err;
121         }
122
123         lane->function = err;
124
125         return 0;
126 }
127
128 static void tegra_xusb_lane_destroy(struct phy *phy)
129 {
130         if (phy) {
131                 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
132
133                 lane->pad->ops->remove(lane);
134                 phy_destroy(phy);
135         }
136 }
137
138 static void tegra_xusb_pad_release(struct device *dev)
139 {
140         struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
141
142         pad->soc->ops->remove(pad);
143 }
144
145 static struct device_type tegra_xusb_pad_type = {
146         .release = tegra_xusb_pad_release,
147 };
148
149 int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
150                         struct tegra_xusb_padctl *padctl,
151                         struct device_node *np)
152 {
153         int err;
154
155         device_initialize(&pad->dev);
156         INIT_LIST_HEAD(&pad->list);
157         pad->dev.parent = padctl->dev;
158         pad->dev.type = &tegra_xusb_pad_type;
159         pad->dev.of_node = np;
160         pad->padctl = padctl;
161
162         err = dev_set_name(&pad->dev, "%s", pad->soc->name);
163         if (err < 0)
164                 goto unregister;
165
166         err = device_add(&pad->dev);
167         if (err < 0)
168                 goto unregister;
169
170         return 0;
171
172 unregister:
173         device_unregister(&pad->dev);
174         return err;
175 }
176
177 int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
178                             const struct phy_ops *ops)
179 {
180         struct device_node *children;
181         struct phy *lane;
182         unsigned int i;
183         int err;
184
185         children = of_get_child_by_name(pad->dev.of_node, "lanes");
186         if (!children)
187                 return -ENODEV;
188
189         pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
190                                   GFP_KERNEL);
191         if (!pad->lanes) {
192                 of_node_put(children);
193                 return -ENOMEM;
194         }
195
196         for (i = 0; i < pad->soc->num_lanes; i++) {
197                 struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
198                 struct tegra_xusb_lane *lane;
199
200                 /* skip disabled lanes */
201                 if (!np || !of_device_is_available(np)) {
202                         of_node_put(np);
203                         continue;
204                 }
205
206                 pad->lanes[i] = phy_create(&pad->dev, np, ops);
207                 if (IS_ERR(pad->lanes[i])) {
208                         err = PTR_ERR(pad->lanes[i]);
209                         of_node_put(np);
210                         goto remove;
211                 }
212
213                 lane = pad->ops->probe(pad, np, i);
214                 if (IS_ERR(lane)) {
215                         phy_destroy(pad->lanes[i]);
216                         err = PTR_ERR(lane);
217                         goto remove;
218                 }
219
220                 list_add_tail(&lane->list, &pad->padctl->lanes);
221                 phy_set_drvdata(pad->lanes[i], lane);
222         }
223
224         pad->provider = of_phy_provider_register_full(&pad->dev, children,
225                                                       tegra_xusb_pad_of_xlate);
226         if (IS_ERR(pad->provider)) {
227                 err = PTR_ERR(pad->provider);
228                 goto remove;
229         }
230
231         return 0;
232
233 remove:
234         while (i--)
235                 tegra_xusb_lane_destroy(pad->lanes[i]);
236
237         of_node_put(children);
238
239         return err;
240 }
241
242 void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
243 {
244         unsigned int i = pad->soc->num_lanes;
245
246         of_phy_provider_unregister(pad->provider);
247
248         while (i--)
249                 tegra_xusb_lane_destroy(pad->lanes[i]);
250
251         device_unregister(&pad->dev);
252 }
253
254 static struct tegra_xusb_pad *
255 tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
256                       const struct tegra_xusb_pad_soc *soc)
257 {
258         struct tegra_xusb_pad *pad;
259         struct device_node *np;
260         int err;
261
262         np = tegra_xusb_find_pad_node(padctl, soc->name);
263         if (!np || !of_device_is_available(np))
264                 return NULL;
265
266         pad = soc->ops->probe(padctl, soc, np);
267         if (IS_ERR(pad)) {
268                 err = PTR_ERR(pad);
269                 dev_err(padctl->dev, "failed to create pad %s: %d\n",
270                         soc->name, err);
271                 return ERR_PTR(err);
272         }
273
274         /* XXX move this into ->probe() to avoid string comparison */
275         if (strcmp(soc->name, "pcie") == 0)
276                 padctl->pcie = pad;
277
278         if (strcmp(soc->name, "sata") == 0)
279                 padctl->sata = pad;
280
281         if (strcmp(soc->name, "usb2") == 0)
282                 padctl->usb2 = pad;
283
284         if (strcmp(soc->name, "ulpi") == 0)
285                 padctl->ulpi = pad;
286
287         if (strcmp(soc->name, "hsic") == 0)
288                 padctl->hsic = pad;
289
290         return pad;
291 }
292
293 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
294 {
295         struct tegra_xusb_pad *pad, *tmp;
296
297         list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
298                 list_del(&pad->list);
299                 tegra_xusb_pad_unregister(pad);
300         }
301 }
302
303 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
304 {
305         mutex_lock(&padctl->lock);
306         __tegra_xusb_remove_pads(padctl);
307         mutex_unlock(&padctl->lock);
308 }
309
310 static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
311 {
312         struct tegra_xusb_padctl *padctl = lane->pad->padctl;
313         const struct tegra_xusb_lane_soc *soc = lane->soc;
314         u32 value;
315
316         /* choose function */
317         value = padctl_readl(padctl, soc->offset);
318         value &= ~(soc->mask << soc->shift);
319         value |= lane->function << soc->shift;
320         padctl_writel(padctl, value, soc->offset);
321 }
322
323 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
324 {
325         unsigned int i;
326
327         for (i = 0; i < pad->soc->num_lanes; i++) {
328                 struct tegra_xusb_lane *lane;
329
330                 if (pad->lanes[i]) {
331                         lane = phy_get_drvdata(pad->lanes[i]);
332                         tegra_xusb_lane_program(lane);
333                 }
334         }
335 }
336
337 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
338 {
339         struct tegra_xusb_pad *pad;
340         unsigned int i;
341
342         mutex_lock(&padctl->lock);
343
344         for (i = 0; i < padctl->soc->num_pads; i++) {
345                 const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
346                 int err;
347
348                 pad = tegra_xusb_pad_create(padctl, soc);
349                 if (IS_ERR(pad)) {
350                         err = PTR_ERR(pad);
351                         dev_err(padctl->dev, "failed to create pad %s: %d\n",
352                                 soc->name, err);
353                         __tegra_xusb_remove_pads(padctl);
354                         mutex_unlock(&padctl->lock);
355                         return err;
356                 }
357
358                 if (!pad)
359                         continue;
360
361                 list_add_tail(&pad->list, &padctl->pads);
362         }
363
364         list_for_each_entry(pad, &padctl->pads, list)
365                 tegra_xusb_pad_program(pad);
366
367         mutex_unlock(&padctl->lock);
368         return 0;
369 }
370
371 static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
372                                   const char *function)
373 {
374         const char *func = lane->soc->funcs[lane->function];
375
376         return strcmp(function, func) == 0;
377 }
378
379 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
380                                              const char *type,
381                                              unsigned int index)
382 {
383         struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
384         char *name;
385
386         name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
387         if (!name)
388                 return ERR_PTR(-ENOMEM);
389
390         list_for_each_entry(lane, &padctl->lanes, list) {
391                 if (strcmp(lane->soc->name, name) == 0) {
392                         hit = lane;
393                         break;
394                 }
395         }
396
397         kfree(name);
398         return hit;
399 }
400
401 struct tegra_xusb_lane *
402 tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
403                           const struct tegra_xusb_lane_map *map,
404                           const char *function)
405 {
406         struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
407
408         for (; map->type; map++) {
409                 if (port->index != map->port)
410                         continue;
411
412                 lane = tegra_xusb_find_lane(port->padctl, map->type,
413                                             map->index);
414                 if (IS_ERR(lane))
415                         continue;
416
417                 if (!tegra_xusb_lane_check(lane, function))
418                         continue;
419
420                 if (!IS_ERR(match))
421                         dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
422                                 map->type, map->index, match->soc->name);
423                 else
424                         match = lane;
425         }
426
427         return match;
428 }
429
430 static struct device_node *
431 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
432                           unsigned int index)
433 {
434         struct device_node *ports, *np;
435         char *name;
436
437         ports = of_get_child_by_name(padctl->dev->of_node, "ports");
438         if (!ports)
439                 return NULL;
440
441         name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
442         if (!name) {
443                 of_node_put(ports);
444                 return ERR_PTR(-ENOMEM);
445         }
446         np = of_get_child_by_name(ports, name);
447         kfree(name);
448         of_node_put(ports);
449
450         return np;
451 }
452
453 struct tegra_xusb_port *
454 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
455                      unsigned int index)
456 {
457         struct tegra_xusb_port *port;
458         struct device_node *np;
459
460         np = tegra_xusb_find_port_node(padctl, type, index);
461         if (!np)
462                 return NULL;
463
464         list_for_each_entry(port, &padctl->ports, list) {
465                 if (np == port->dev.of_node) {
466                         of_node_put(np);
467                         return port;
468                 }
469         }
470
471         of_node_put(np);
472
473         return NULL;
474 }
475
476 struct tegra_xusb_usb2_port *
477 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
478 {
479         struct tegra_xusb_port *port;
480
481         port = tegra_xusb_find_port(padctl, "usb2", index);
482         if (port)
483                 return to_usb2_port(port);
484
485         return NULL;
486 }
487
488 struct tegra_xusb_usb3_port *
489 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
490 {
491         struct tegra_xusb_port *port;
492
493         port = tegra_xusb_find_port(padctl, "usb3", index);
494         if (port)
495                 return to_usb3_port(port);
496
497         return NULL;
498 }
499
500 static void tegra_xusb_port_release(struct device *dev)
501 {
502 }
503
504 static struct device_type tegra_xusb_port_type = {
505         .release = tegra_xusb_port_release,
506 };
507
508 static int tegra_xusb_port_init(struct tegra_xusb_port *port,
509                                 struct tegra_xusb_padctl *padctl,
510                                 struct device_node *np,
511                                 const char *name,
512                                 unsigned int index)
513 {
514         int err;
515
516         INIT_LIST_HEAD(&port->list);
517         port->padctl = padctl;
518         port->index = index;
519
520         device_initialize(&port->dev);
521         port->dev.type = &tegra_xusb_port_type;
522         port->dev.of_node = of_node_get(np);
523         port->dev.parent = padctl->dev;
524
525         err = dev_set_name(&port->dev, "%s-%u", name, index);
526         if (err < 0)
527                 goto unregister;
528
529         err = device_add(&port->dev);
530         if (err < 0)
531                 goto unregister;
532
533         return 0;
534
535 unregister:
536         device_unregister(&port->dev);
537         return err;
538 }
539
540 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
541 {
542         device_unregister(&port->dev);
543 }
544
545 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
546 {
547         struct tegra_xusb_port *port = &usb2->base;
548         struct device_node *np = port->dev.of_node;
549
550         usb2->internal = of_property_read_bool(np, "nvidia,internal");
551
552         usb2->supply = devm_regulator_get(&port->dev, "vbus");
553         return PTR_ERR_OR_ZERO(usb2->supply);
554 }
555
556 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
557                                     unsigned int index)
558 {
559         struct tegra_xusb_usb2_port *usb2;
560         struct device_node *np;
561         int err = 0;
562
563         /*
564          * USB2 ports don't require additional properties, but if the port is
565          * marked as disabled there is no reason to register it.
566          */
567         np = tegra_xusb_find_port_node(padctl, "usb2", index);
568         if (!np || !of_device_is_available(np))
569                 goto out;
570
571         usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
572         if (!usb2) {
573                 err = -ENOMEM;
574                 goto out;
575         }
576
577         err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
578         if (err < 0)
579                 goto out;
580
581         usb2->base.ops = padctl->soc->ports.usb2.ops;
582
583         usb2->base.lane = usb2->base.ops->map(&usb2->base);
584         if (IS_ERR(usb2->base.lane)) {
585                 err = PTR_ERR(usb2->base.lane);
586                 tegra_xusb_port_unregister(&usb2->base);
587                 goto out;
588         }
589
590         err = tegra_xusb_usb2_port_parse_dt(usb2);
591         if (err < 0) {
592                 tegra_xusb_port_unregister(&usb2->base);
593                 goto out;
594         }
595
596         list_add_tail(&usb2->base.list, &padctl->ports);
597
598 out:
599         of_node_put(np);
600         return err;
601 }
602
603 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
604 {
605         struct tegra_xusb_port *port = &ulpi->base;
606         struct device_node *np = port->dev.of_node;
607
608         ulpi->internal = of_property_read_bool(np, "nvidia,internal");
609
610         return 0;
611 }
612
613 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
614                                     unsigned int index)
615 {
616         struct tegra_xusb_ulpi_port *ulpi;
617         struct device_node *np;
618         int err = 0;
619
620         np = tegra_xusb_find_port_node(padctl, "ulpi", index);
621         if (!np || !of_device_is_available(np))
622                 goto out;
623
624         ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
625         if (!ulpi) {
626                 err = -ENOMEM;
627                 goto out;
628         }
629
630         err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
631         if (err < 0)
632                 goto out;
633
634         ulpi->base.ops = padctl->soc->ports.ulpi.ops;
635
636         ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
637         if (IS_ERR(ulpi->base.lane)) {
638                 err = PTR_ERR(ulpi->base.lane);
639                 tegra_xusb_port_unregister(&ulpi->base);
640                 goto out;
641         }
642
643         err = tegra_xusb_ulpi_port_parse_dt(ulpi);
644         if (err < 0) {
645                 tegra_xusb_port_unregister(&ulpi->base);
646                 goto out;
647         }
648
649         list_add_tail(&ulpi->base.list, &padctl->ports);
650
651 out:
652         of_node_put(np);
653         return err;
654 }
655
656 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
657 {
658         /* XXX */
659         return 0;
660 }
661
662 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
663                                     unsigned int index)
664 {
665         struct tegra_xusb_hsic_port *hsic;
666         struct device_node *np;
667         int err = 0;
668
669         np = tegra_xusb_find_port_node(padctl, "hsic", index);
670         if (!np || !of_device_is_available(np))
671                 goto out;
672
673         hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
674         if (!hsic) {
675                 err = -ENOMEM;
676                 goto out;
677         }
678
679         err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
680         if (err < 0)
681                 goto out;
682
683         hsic->base.ops = padctl->soc->ports.hsic.ops;
684
685         hsic->base.lane = hsic->base.ops->map(&hsic->base);
686         if (IS_ERR(hsic->base.lane)) {
687                 err = PTR_ERR(hsic->base.lane);
688                 goto out;
689         }
690
691         err = tegra_xusb_hsic_port_parse_dt(hsic);
692         if (err < 0) {
693                 tegra_xusb_port_unregister(&hsic->base);
694                 goto out;
695         }
696
697         list_add_tail(&hsic->base.list, &padctl->ports);
698
699 out:
700         of_node_put(np);
701         return err;
702 }
703
704 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
705 {
706         struct tegra_xusb_port *port = &usb3->base;
707         struct device_node *np = port->dev.of_node;
708         u32 value;
709         int err;
710
711         err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
712         if (err < 0) {
713                 dev_err(&port->dev, "failed to read port: %d\n", err);
714                 return err;
715         }
716
717         usb3->port = value;
718
719         usb3->internal = of_property_read_bool(np, "nvidia,internal");
720
721         usb3->supply = devm_regulator_get(&port->dev, "vbus");
722         return PTR_ERR_OR_ZERO(usb3->supply);
723 }
724
725 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
726                                     unsigned int index)
727 {
728         struct tegra_xusb_usb3_port *usb3;
729         struct device_node *np;
730         int err = 0;
731
732         /*
733          * If there is no supplemental configuration in the device tree the
734          * port is unusable. But it is valid to configure only a single port,
735          * hence return 0 instead of an error to allow ports to be optional.
736          */
737         np = tegra_xusb_find_port_node(padctl, "usb3", index);
738         if (!np || !of_device_is_available(np))
739                 goto out;
740
741         usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
742         if (!usb3) {
743                 err = -ENOMEM;
744                 goto out;
745         }
746
747         err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
748         if (err < 0)
749                 goto out;
750
751         usb3->base.ops = padctl->soc->ports.usb3.ops;
752
753         usb3->base.lane = usb3->base.ops->map(&usb3->base);
754         if (IS_ERR(usb3->base.lane)) {
755                 err = PTR_ERR(usb3->base.lane);
756                 goto out;
757         }
758
759         err = tegra_xusb_usb3_port_parse_dt(usb3);
760         if (err < 0) {
761                 tegra_xusb_port_unregister(&usb3->base);
762                 goto out;
763         }
764
765         list_add_tail(&usb3->base.list, &padctl->ports);
766
767 out:
768         of_node_put(np);
769         return err;
770 }
771
772 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
773 {
774         struct tegra_xusb_port *port, *tmp;
775
776         list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
777                 list_del(&port->list);
778                 tegra_xusb_port_unregister(port);
779         }
780 }
781
782 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
783 {
784         struct tegra_xusb_port *port;
785         unsigned int i;
786         int err = 0;
787
788         mutex_lock(&padctl->lock);
789
790         for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
791                 err = tegra_xusb_add_usb2_port(padctl, i);
792                 if (err < 0)
793                         goto remove_ports;
794         }
795
796         for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
797                 err = tegra_xusb_add_ulpi_port(padctl, i);
798                 if (err < 0)
799                         goto remove_ports;
800         }
801
802         for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
803                 err = tegra_xusb_add_hsic_port(padctl, i);
804                 if (err < 0)
805                         goto remove_ports;
806         }
807
808         for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
809                 err = tegra_xusb_add_usb3_port(padctl, i);
810                 if (err < 0)
811                         goto remove_ports;
812         }
813
814         list_for_each_entry(port, &padctl->ports, list) {
815                 err = port->ops->enable(port);
816                 if (err < 0)
817                         dev_err(padctl->dev, "failed to enable port %s: %d\n",
818                                 dev_name(&port->dev), err);
819         }
820
821         goto unlock;
822
823 remove_ports:
824         __tegra_xusb_remove_ports(padctl);
825 unlock:
826         mutex_unlock(&padctl->lock);
827         return err;
828 }
829
830 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
831 {
832         mutex_lock(&padctl->lock);
833         __tegra_xusb_remove_ports(padctl);
834         mutex_unlock(&padctl->lock);
835 }
836
837 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
838 {
839         struct device_node *np = pdev->dev.of_node;
840         const struct tegra_xusb_padctl_soc *soc;
841         struct tegra_xusb_padctl *padctl;
842         const struct of_device_id *match;
843         struct resource *res;
844         int err;
845
846         /* for backwards compatibility with old device trees */
847         np = of_get_child_by_name(np, "pads");
848         if (!np) {
849                 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
850                 return tegra_xusb_padctl_legacy_probe(pdev);
851         }
852
853         of_node_put(np);
854
855         match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
856         soc = match->data;
857
858         padctl = soc->ops->probe(&pdev->dev, soc);
859         if (IS_ERR(padctl))
860                 return PTR_ERR(padctl);
861
862         platform_set_drvdata(pdev, padctl);
863         INIT_LIST_HEAD(&padctl->ports);
864         INIT_LIST_HEAD(&padctl->lanes);
865         INIT_LIST_HEAD(&padctl->pads);
866         mutex_init(&padctl->lock);
867
868         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
869         padctl->regs = devm_ioremap_resource(&pdev->dev, res);
870         if (IS_ERR(padctl->regs)) {
871                 err = PTR_ERR(padctl->regs);
872                 goto remove;
873         }
874
875         padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
876         if (IS_ERR(padctl->rst)) {
877                 err = PTR_ERR(padctl->rst);
878                 goto remove;
879         }
880
881         err = reset_control_deassert(padctl->rst);
882         if (err < 0)
883                 goto remove;
884
885         err = tegra_xusb_setup_pads(padctl);
886         if (err < 0) {
887                 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
888                 goto reset;
889         }
890
891         err = tegra_xusb_setup_ports(padctl);
892         if (err) {
893                 dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
894                 goto remove_pads;
895         }
896
897         return 0;
898
899 remove_pads:
900         tegra_xusb_remove_pads(padctl);
901 reset:
902         reset_control_assert(padctl->rst);
903 remove:
904         platform_set_drvdata(pdev, NULL);
905         soc->ops->remove(padctl);
906         return err;
907 }
908
909 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
910 {
911         struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
912         int err;
913
914         tegra_xusb_remove_ports(padctl);
915         tegra_xusb_remove_pads(padctl);
916
917         err = reset_control_assert(padctl->rst);
918         if (err < 0)
919                 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
920
921         padctl->soc->ops->remove(padctl);
922
923         return err;
924 }
925
926 static struct platform_driver tegra_xusb_padctl_driver = {
927         .driver = {
928                 .name = "tegra-xusb-padctl",
929                 .of_match_table = tegra_xusb_padctl_of_match,
930         },
931         .probe = tegra_xusb_padctl_probe,
932         .remove = tegra_xusb_padctl_remove,
933 };
934 module_platform_driver(tegra_xusb_padctl_driver);
935
936 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
937 {
938         struct tegra_xusb_padctl *padctl;
939         struct platform_device *pdev;
940         struct device_node *np;
941
942         np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
943         if (!np)
944                 return ERR_PTR(-EINVAL);
945
946         /*
947          * This is slightly ugly. A better implementation would be to keep a
948          * registry of pad controllers, but since there will almost certainly
949          * only ever be one per SoC that would be a little overkill.
950          */
951         pdev = of_find_device_by_node(np);
952         if (!pdev) {
953                 of_node_put(np);
954                 return ERR_PTR(-ENODEV);
955         }
956
957         of_node_put(np);
958
959         padctl = platform_get_drvdata(pdev);
960         if (!padctl) {
961                 put_device(&pdev->dev);
962                 return ERR_PTR(-EPROBE_DEFER);
963         }
964
965         return padctl;
966 }
967 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
968
969 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
970 {
971         if (padctl)
972                 put_device(padctl->dev);
973 }
974 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
975
976 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
977                                         unsigned int port)
978 {
979         if (padctl->soc->ops->usb3_save_context)
980                 return padctl->soc->ops->usb3_save_context(padctl, port);
981
982         return -ENOSYS;
983 }
984 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
985
986 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
987                                     unsigned int port, bool idle)
988 {
989         if (padctl->soc->ops->hsic_set_idle)
990                 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
991
992         return -ENOSYS;
993 }
994 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
995
996 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
997                                            unsigned int port, bool enable)
998 {
999         if (padctl->soc->ops->usb3_set_lfps_detect)
1000                 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1001                                                               enable);
1002
1003         return -ENOSYS;
1004 }
1005 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1006
1007 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1008 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1009 MODULE_LICENSE("GPL v2");