drm/nouveau/tegra: Fix error handling
[linux/fpc-iii.git] / drivers / pinctrl / tegra / pinctrl-tegra-xusb.c
blob6f68a9eebc58ba85703293dd836c4c5a29af7d92
1 /*
2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
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.
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.
14 #include <linux/delay.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/phy/phy.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/platform_device.h>
22 #include <linux/reset.h>
23 #include <linux/slab.h>
25 #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
27 #include "../core.h"
28 #include "../pinctrl-utils.h"
30 #define XUSB_PADCTL_ELPG_PROGRAM 0x01c
31 #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN (1 << 26)
32 #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY (1 << 25)
33 #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN (1 << 24)
35 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1 0x040
36 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET (1 << 19)
37 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK (0xf << 12)
38 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST (1 << 1)
40 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2 0x044
41 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN (1 << 6)
42 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN (1 << 5)
43 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL (1 << 4)
45 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1 0x138
46 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET (1 << 27)
47 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE (1 << 24)
48 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD (1 << 3)
49 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST (1 << 1)
50 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ (1 << 0)
52 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1 0x148
53 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD (1 << 1)
54 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ (1 << 0)
56 struct tegra_xusb_padctl_function {
57 const char *name;
58 const char * const *groups;
59 unsigned int num_groups;
62 struct tegra_xusb_padctl_soc {
63 const struct pinctrl_pin_desc *pins;
64 unsigned int num_pins;
66 const struct tegra_xusb_padctl_function *functions;
67 unsigned int num_functions;
69 const struct tegra_xusb_padctl_lane *lanes;
70 unsigned int num_lanes;
73 struct tegra_xusb_padctl_lane {
74 const char *name;
76 unsigned int offset;
77 unsigned int shift;
78 unsigned int mask;
79 unsigned int iddq;
81 const unsigned int *funcs;
82 unsigned int num_funcs;
85 struct tegra_xusb_padctl {
86 struct device *dev;
87 void __iomem *regs;
88 struct mutex lock;
89 struct reset_control *rst;
91 const struct tegra_xusb_padctl_soc *soc;
92 struct pinctrl_dev *pinctrl;
93 struct pinctrl_desc desc;
95 struct phy_provider *provider;
96 struct phy *phys[2];
98 unsigned int enable;
101 static inline void padctl_writel(struct tegra_xusb_padctl *padctl, u32 value,
102 unsigned long offset)
104 writel(value, padctl->regs + offset);
107 static inline u32 padctl_readl(struct tegra_xusb_padctl *padctl,
108 unsigned long offset)
110 return readl(padctl->regs + offset);
113 static int tegra_xusb_padctl_get_groups_count(struct pinctrl_dev *pinctrl)
115 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
117 return padctl->soc->num_pins;
120 static const char *tegra_xusb_padctl_get_group_name(struct pinctrl_dev *pinctrl,
121 unsigned int group)
123 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
125 return padctl->soc->pins[group].name;
128 static int tegra_xusb_padctl_get_group_pins(struct pinctrl_dev *pinctrl,
129 unsigned group,
130 const unsigned **pins,
131 unsigned *num_pins)
134 * For the tegra-xusb pad controller groups are synonomous
135 * with lanes/pins and there is always one lane/pin per group.
137 *pins = &pinctrl->desc->pins[group].number;
138 *num_pins = 1;
140 return 0;
143 enum tegra_xusb_padctl_param {
144 TEGRA_XUSB_PADCTL_IDDQ,
147 static const struct tegra_xusb_padctl_property {
148 const char *name;
149 enum tegra_xusb_padctl_param param;
150 } properties[] = {
151 { "nvidia,iddq", TEGRA_XUSB_PADCTL_IDDQ },
154 #define TEGRA_XUSB_PADCTL_PACK(param, value) ((param) << 16 | (value))
155 #define TEGRA_XUSB_PADCTL_UNPACK_PARAM(config) ((config) >> 16)
156 #define TEGRA_XUSB_PADCTL_UNPACK_VALUE(config) ((config) & 0xffff)
158 static int tegra_xusb_padctl_parse_subnode(struct tegra_xusb_padctl *padctl,
159 struct device_node *np,
160 struct pinctrl_map **maps,
161 unsigned int *reserved_maps,
162 unsigned int *num_maps)
164 unsigned int i, reserve = 0, num_configs = 0;
165 unsigned long config, *configs = NULL;
166 const char *function, *group;
167 struct property *prop;
168 int err = 0;
169 u32 value;
171 err = of_property_read_string(np, "nvidia,function", &function);
172 if (err < 0) {
173 if (err != -EINVAL)
174 return err;
176 function = NULL;
179 for (i = 0; i < ARRAY_SIZE(properties); i++) {
180 err = of_property_read_u32(np, properties[i].name, &value);
181 if (err < 0) {
182 if (err == -EINVAL)
183 continue;
185 goto out;
188 config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, value);
190 err = pinctrl_utils_add_config(padctl->pinctrl, &configs,
191 &num_configs, config);
192 if (err < 0)
193 goto out;
196 if (function)
197 reserve++;
199 if (num_configs)
200 reserve++;
202 err = of_property_count_strings(np, "nvidia,lanes");
203 if (err < 0)
204 goto out;
206 reserve *= err;
208 err = pinctrl_utils_reserve_map(padctl->pinctrl, maps, reserved_maps,
209 num_maps, reserve);
210 if (err < 0)
211 goto out;
213 of_property_for_each_string(np, "nvidia,lanes", prop, group) {
214 if (function) {
215 err = pinctrl_utils_add_map_mux(padctl->pinctrl, maps,
216 reserved_maps, num_maps, group,
217 function);
218 if (err < 0)
219 goto out;
222 if (num_configs) {
223 err = pinctrl_utils_add_map_configs(padctl->pinctrl,
224 maps, reserved_maps, num_maps, group,
225 configs, num_configs,
226 PIN_MAP_TYPE_CONFIGS_GROUP);
227 if (err < 0)
228 goto out;
232 err = 0;
234 out:
235 kfree(configs);
236 return err;
239 static int tegra_xusb_padctl_dt_node_to_map(struct pinctrl_dev *pinctrl,
240 struct device_node *parent,
241 struct pinctrl_map **maps,
242 unsigned int *num_maps)
244 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
245 unsigned int reserved_maps = 0;
246 struct device_node *np;
247 int err;
249 *num_maps = 0;
250 *maps = NULL;
252 for_each_child_of_node(parent, np) {
253 err = tegra_xusb_padctl_parse_subnode(padctl, np, maps,
254 &reserved_maps,
255 num_maps);
256 if (err < 0) {
257 of_node_put(np);
258 return err;
262 return 0;
265 static const struct pinctrl_ops tegra_xusb_padctl_pinctrl_ops = {
266 .get_groups_count = tegra_xusb_padctl_get_groups_count,
267 .get_group_name = tegra_xusb_padctl_get_group_name,
268 .get_group_pins = tegra_xusb_padctl_get_group_pins,
269 .dt_node_to_map = tegra_xusb_padctl_dt_node_to_map,
270 .dt_free_map = pinctrl_utils_free_map,
273 static int tegra_xusb_padctl_get_functions_count(struct pinctrl_dev *pinctrl)
275 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
277 return padctl->soc->num_functions;
280 static const char *
281 tegra_xusb_padctl_get_function_name(struct pinctrl_dev *pinctrl,
282 unsigned int function)
284 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
286 return padctl->soc->functions[function].name;
289 static int tegra_xusb_padctl_get_function_groups(struct pinctrl_dev *pinctrl,
290 unsigned int function,
291 const char * const **groups,
292 unsigned * const num_groups)
294 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
296 *num_groups = padctl->soc->functions[function].num_groups;
297 *groups = padctl->soc->functions[function].groups;
299 return 0;
302 static int tegra_xusb_padctl_pinmux_set(struct pinctrl_dev *pinctrl,
303 unsigned int function,
304 unsigned int group)
306 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
307 const struct tegra_xusb_padctl_lane *lane;
308 unsigned int i;
309 u32 value;
311 lane = &padctl->soc->lanes[group];
313 for (i = 0; i < lane->num_funcs; i++)
314 if (lane->funcs[i] == function)
315 break;
317 if (i >= lane->num_funcs)
318 return -EINVAL;
320 value = padctl_readl(padctl, lane->offset);
321 value &= ~(lane->mask << lane->shift);
322 value |= i << lane->shift;
323 padctl_writel(padctl, value, lane->offset);
325 return 0;
328 static const struct pinmux_ops tegra_xusb_padctl_pinmux_ops = {
329 .get_functions_count = tegra_xusb_padctl_get_functions_count,
330 .get_function_name = tegra_xusb_padctl_get_function_name,
331 .get_function_groups = tegra_xusb_padctl_get_function_groups,
332 .set_mux = tegra_xusb_padctl_pinmux_set,
335 static int tegra_xusb_padctl_pinconf_group_get(struct pinctrl_dev *pinctrl,
336 unsigned int group,
337 unsigned long *config)
339 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
340 const struct tegra_xusb_padctl_lane *lane;
341 enum tegra_xusb_padctl_param param;
342 u32 value;
344 param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(*config);
345 lane = &padctl->soc->lanes[group];
347 switch (param) {
348 case TEGRA_XUSB_PADCTL_IDDQ:
349 /* lanes with iddq == 0 don't support this parameter */
350 if (lane->iddq == 0)
351 return -EINVAL;
353 value = padctl_readl(padctl, lane->offset);
355 if (value & BIT(lane->iddq))
356 value = 0;
357 else
358 value = 1;
360 *config = TEGRA_XUSB_PADCTL_PACK(param, value);
361 break;
363 default:
364 dev_err(padctl->dev, "invalid configuration parameter: %04x\n",
365 param);
366 return -ENOTSUPP;
369 return 0;
372 static int tegra_xusb_padctl_pinconf_group_set(struct pinctrl_dev *pinctrl,
373 unsigned int group,
374 unsigned long *configs,
375 unsigned int num_configs)
377 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
378 const struct tegra_xusb_padctl_lane *lane;
379 enum tegra_xusb_padctl_param param;
380 unsigned long value;
381 unsigned int i;
382 u32 regval;
384 lane = &padctl->soc->lanes[group];
386 for (i = 0; i < num_configs; i++) {
387 param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(configs[i]);
388 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(configs[i]);
390 switch (param) {
391 case TEGRA_XUSB_PADCTL_IDDQ:
392 /* lanes with iddq == 0 don't support this parameter */
393 if (lane->iddq == 0)
394 return -EINVAL;
396 regval = padctl_readl(padctl, lane->offset);
398 if (value)
399 regval &= ~BIT(lane->iddq);
400 else
401 regval |= BIT(lane->iddq);
403 padctl_writel(padctl, regval, lane->offset);
404 break;
406 default:
407 dev_err(padctl->dev,
408 "invalid configuration parameter: %04x\n",
409 param);
410 return -ENOTSUPP;
414 return 0;
417 #ifdef CONFIG_DEBUG_FS
418 static const char *strip_prefix(const char *s)
420 const char *comma = strchr(s, ',');
421 if (!comma)
422 return s;
424 return comma + 1;
427 static void
428 tegra_xusb_padctl_pinconf_group_dbg_show(struct pinctrl_dev *pinctrl,
429 struct seq_file *s,
430 unsigned int group)
432 unsigned int i;
434 for (i = 0; i < ARRAY_SIZE(properties); i++) {
435 unsigned long config, value;
436 int err;
438 config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, 0);
440 err = tegra_xusb_padctl_pinconf_group_get(pinctrl, group,
441 &config);
442 if (err < 0)
443 continue;
445 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
447 seq_printf(s, "\n\t%s=%lu\n", strip_prefix(properties[i].name),
448 value);
452 static void
453 tegra_xusb_padctl_pinconf_config_dbg_show(struct pinctrl_dev *pinctrl,
454 struct seq_file *s,
455 unsigned long config)
457 enum tegra_xusb_padctl_param param;
458 const char *name = "unknown";
459 unsigned long value;
460 unsigned int i;
462 param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(config);
463 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
465 for (i = 0; i < ARRAY_SIZE(properties); i++) {
466 if (properties[i].param == param) {
467 name = properties[i].name;
468 break;
472 seq_printf(s, "%s=%lu", strip_prefix(name), value);
474 #endif
476 static const struct pinconf_ops tegra_xusb_padctl_pinconf_ops = {
477 .pin_config_group_get = tegra_xusb_padctl_pinconf_group_get,
478 .pin_config_group_set = tegra_xusb_padctl_pinconf_group_set,
479 #ifdef CONFIG_DEBUG_FS
480 .pin_config_group_dbg_show = tegra_xusb_padctl_pinconf_group_dbg_show,
481 .pin_config_config_dbg_show = tegra_xusb_padctl_pinconf_config_dbg_show,
482 #endif
485 static int tegra_xusb_padctl_enable(struct tegra_xusb_padctl *padctl)
487 u32 value;
489 mutex_lock(&padctl->lock);
491 if (padctl->enable++ > 0)
492 goto out;
494 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
495 value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
496 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
498 usleep_range(100, 200);
500 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
501 value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
502 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
504 usleep_range(100, 200);
506 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
507 value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
508 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
510 out:
511 mutex_unlock(&padctl->lock);
512 return 0;
515 static int tegra_xusb_padctl_disable(struct tegra_xusb_padctl *padctl)
517 u32 value;
519 mutex_lock(&padctl->lock);
521 if (WARN_ON(padctl->enable == 0))
522 goto out;
524 if (--padctl->enable > 0)
525 goto out;
527 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
528 value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
529 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
531 usleep_range(100, 200);
533 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
534 value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
535 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
537 usleep_range(100, 200);
539 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
540 value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
541 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
543 out:
544 mutex_unlock(&padctl->lock);
545 return 0;
548 static int tegra_xusb_phy_init(struct phy *phy)
550 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
552 return tegra_xusb_padctl_enable(padctl);
555 static int tegra_xusb_phy_exit(struct phy *phy)
557 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
559 return tegra_xusb_padctl_disable(padctl);
562 static int pcie_phy_power_on(struct phy *phy)
564 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
565 unsigned long timeout;
566 int err = -ETIMEDOUT;
567 u32 value;
569 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
570 value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK;
571 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
573 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
574 value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN |
575 XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN |
576 XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL;
577 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
579 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
580 value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
581 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
583 timeout = jiffies + msecs_to_jiffies(50);
585 while (time_before(jiffies, timeout)) {
586 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
587 if (value & XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET) {
588 err = 0;
589 break;
592 usleep_range(100, 200);
595 return err;
598 static int pcie_phy_power_off(struct phy *phy)
600 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
601 u32 value;
603 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
604 value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
605 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
607 return 0;
610 static const struct phy_ops pcie_phy_ops = {
611 .init = tegra_xusb_phy_init,
612 .exit = tegra_xusb_phy_exit,
613 .power_on = pcie_phy_power_on,
614 .power_off = pcie_phy_power_off,
615 .owner = THIS_MODULE,
618 static int sata_phy_power_on(struct phy *phy)
620 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
621 unsigned long timeout;
622 int err = -ETIMEDOUT;
623 u32 value;
625 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
626 value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
627 value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
628 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
630 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
631 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
632 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
633 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
635 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
636 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
637 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
639 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
640 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
641 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
643 timeout = jiffies + msecs_to_jiffies(50);
645 while (time_before(jiffies, timeout)) {
646 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
647 if (value & XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET) {
648 err = 0;
649 break;
652 usleep_range(100, 200);
655 return err;
658 static int sata_phy_power_off(struct phy *phy)
660 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
661 u32 value;
663 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
664 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
665 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
667 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
668 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
669 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
671 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
672 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
673 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
674 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
676 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
677 value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
678 value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
679 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
681 return 0;
684 static const struct phy_ops sata_phy_ops = {
685 .init = tegra_xusb_phy_init,
686 .exit = tegra_xusb_phy_exit,
687 .power_on = sata_phy_power_on,
688 .power_off = sata_phy_power_off,
689 .owner = THIS_MODULE,
692 static struct phy *tegra_xusb_padctl_xlate(struct device *dev,
693 struct of_phandle_args *args)
695 struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
696 unsigned int index = args->args[0];
698 if (args->args_count <= 0)
699 return ERR_PTR(-EINVAL);
701 if (index >= ARRAY_SIZE(padctl->phys))
702 return ERR_PTR(-EINVAL);
704 return padctl->phys[index];
707 #define PIN_OTG_0 0
708 #define PIN_OTG_1 1
709 #define PIN_OTG_2 2
710 #define PIN_ULPI_0 3
711 #define PIN_HSIC_0 4
712 #define PIN_HSIC_1 5
713 #define PIN_PCIE_0 6
714 #define PIN_PCIE_1 7
715 #define PIN_PCIE_2 8
716 #define PIN_PCIE_3 9
717 #define PIN_PCIE_4 10
718 #define PIN_SATA_0 11
720 static const struct pinctrl_pin_desc tegra124_pins[] = {
721 PINCTRL_PIN(PIN_OTG_0, "otg-0"),
722 PINCTRL_PIN(PIN_OTG_1, "otg-1"),
723 PINCTRL_PIN(PIN_OTG_2, "otg-2"),
724 PINCTRL_PIN(PIN_ULPI_0, "ulpi-0"),
725 PINCTRL_PIN(PIN_HSIC_0, "hsic-0"),
726 PINCTRL_PIN(PIN_HSIC_1, "hsic-1"),
727 PINCTRL_PIN(PIN_PCIE_0, "pcie-0"),
728 PINCTRL_PIN(PIN_PCIE_1, "pcie-1"),
729 PINCTRL_PIN(PIN_PCIE_2, "pcie-2"),
730 PINCTRL_PIN(PIN_PCIE_3, "pcie-3"),
731 PINCTRL_PIN(PIN_PCIE_4, "pcie-4"),
732 PINCTRL_PIN(PIN_SATA_0, "sata-0"),
735 static const char * const tegra124_snps_groups[] = {
736 "otg-0",
737 "otg-1",
738 "otg-2",
739 "ulpi-0",
740 "hsic-0",
741 "hsic-1",
744 static const char * const tegra124_xusb_groups[] = {
745 "otg-0",
746 "otg-1",
747 "otg-2",
748 "ulpi-0",
749 "hsic-0",
750 "hsic-1",
753 static const char * const tegra124_uart_groups[] = {
754 "otg-0",
755 "otg-1",
756 "otg-2",
759 static const char * const tegra124_pcie_groups[] = {
760 "pcie-0",
761 "pcie-1",
762 "pcie-2",
763 "pcie-3",
764 "pcie-4",
767 static const char * const tegra124_usb3_groups[] = {
768 "pcie-0",
769 "pcie-1",
770 "sata-0",
773 static const char * const tegra124_sata_groups[] = {
774 "sata-0",
777 static const char * const tegra124_rsvd_groups[] = {
778 "otg-0",
779 "otg-1",
780 "otg-2",
781 "pcie-0",
782 "pcie-1",
783 "pcie-2",
784 "pcie-3",
785 "pcie-4",
786 "sata-0",
789 #define TEGRA124_FUNCTION(_name) \
791 .name = #_name, \
792 .num_groups = ARRAY_SIZE(tegra124_##_name##_groups), \
793 .groups = tegra124_##_name##_groups, \
796 static struct tegra_xusb_padctl_function tegra124_functions[] = {
797 TEGRA124_FUNCTION(snps),
798 TEGRA124_FUNCTION(xusb),
799 TEGRA124_FUNCTION(uart),
800 TEGRA124_FUNCTION(pcie),
801 TEGRA124_FUNCTION(usb3),
802 TEGRA124_FUNCTION(sata),
803 TEGRA124_FUNCTION(rsvd),
806 enum tegra124_function {
807 TEGRA124_FUNC_SNPS,
808 TEGRA124_FUNC_XUSB,
809 TEGRA124_FUNC_UART,
810 TEGRA124_FUNC_PCIE,
811 TEGRA124_FUNC_USB3,
812 TEGRA124_FUNC_SATA,
813 TEGRA124_FUNC_RSVD,
816 static const unsigned int tegra124_otg_functions[] = {
817 TEGRA124_FUNC_SNPS,
818 TEGRA124_FUNC_XUSB,
819 TEGRA124_FUNC_UART,
820 TEGRA124_FUNC_RSVD,
823 static const unsigned int tegra124_usb_functions[] = {
824 TEGRA124_FUNC_SNPS,
825 TEGRA124_FUNC_XUSB,
828 static const unsigned int tegra124_pci_functions[] = {
829 TEGRA124_FUNC_PCIE,
830 TEGRA124_FUNC_USB3,
831 TEGRA124_FUNC_SATA,
832 TEGRA124_FUNC_RSVD,
835 #define TEGRA124_LANE(_name, _offset, _shift, _mask, _iddq, _funcs) \
837 .name = _name, \
838 .offset = _offset, \
839 .shift = _shift, \
840 .mask = _mask, \
841 .iddq = _iddq, \
842 .num_funcs = ARRAY_SIZE(tegra124_##_funcs##_functions), \
843 .funcs = tegra124_##_funcs##_functions, \
846 static const struct tegra_xusb_padctl_lane tegra124_lanes[] = {
847 TEGRA124_LANE("otg-0", 0x004, 0, 0x3, 0, otg),
848 TEGRA124_LANE("otg-1", 0x004, 2, 0x3, 0, otg),
849 TEGRA124_LANE("otg-2", 0x004, 4, 0x3, 0, otg),
850 TEGRA124_LANE("ulpi-0", 0x004, 12, 0x1, 0, usb),
851 TEGRA124_LANE("hsic-0", 0x004, 14, 0x1, 0, usb),
852 TEGRA124_LANE("hsic-1", 0x004, 15, 0x1, 0, usb),
853 TEGRA124_LANE("pcie-0", 0x134, 16, 0x3, 1, pci),
854 TEGRA124_LANE("pcie-1", 0x134, 18, 0x3, 2, pci),
855 TEGRA124_LANE("pcie-2", 0x134, 20, 0x3, 3, pci),
856 TEGRA124_LANE("pcie-3", 0x134, 22, 0x3, 4, pci),
857 TEGRA124_LANE("pcie-4", 0x134, 24, 0x3, 5, pci),
858 TEGRA124_LANE("sata-0", 0x134, 26, 0x3, 6, pci),
861 static const struct tegra_xusb_padctl_soc tegra124_soc = {
862 .num_pins = ARRAY_SIZE(tegra124_pins),
863 .pins = tegra124_pins,
864 .num_functions = ARRAY_SIZE(tegra124_functions),
865 .functions = tegra124_functions,
866 .num_lanes = ARRAY_SIZE(tegra124_lanes),
867 .lanes = tegra124_lanes,
870 static const struct of_device_id tegra_xusb_padctl_of_match[] = {
871 { .compatible = "nvidia,tegra124-xusb-padctl", .data = &tegra124_soc },
874 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
876 int tegra_xusb_padctl_legacy_probe(struct platform_device *pdev)
878 struct tegra_xusb_padctl *padctl;
879 const struct of_device_id *match;
880 struct resource *res;
881 struct phy *phy;
882 int err;
884 padctl = devm_kzalloc(&pdev->dev, sizeof(*padctl), GFP_KERNEL);
885 if (!padctl)
886 return -ENOMEM;
888 platform_set_drvdata(pdev, padctl);
889 mutex_init(&padctl->lock);
890 padctl->dev = &pdev->dev;
892 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
893 padctl->soc = match->data;
895 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
896 padctl->regs = devm_ioremap_resource(&pdev->dev, res);
897 if (IS_ERR(padctl->regs))
898 return PTR_ERR(padctl->regs);
900 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
901 if (IS_ERR(padctl->rst))
902 return PTR_ERR(padctl->rst);
904 err = reset_control_deassert(padctl->rst);
905 if (err < 0)
906 return err;
908 memset(&padctl->desc, 0, sizeof(padctl->desc));
909 padctl->desc.name = dev_name(padctl->dev);
910 padctl->desc.pins = tegra124_pins;
911 padctl->desc.npins = ARRAY_SIZE(tegra124_pins);
912 padctl->desc.pctlops = &tegra_xusb_padctl_pinctrl_ops;
913 padctl->desc.pmxops = &tegra_xusb_padctl_pinmux_ops;
914 padctl->desc.confops = &tegra_xusb_padctl_pinconf_ops;
915 padctl->desc.owner = THIS_MODULE;
917 padctl->pinctrl = devm_pinctrl_register(&pdev->dev, &padctl->desc,
918 padctl);
919 if (IS_ERR(padctl->pinctrl)) {
920 dev_err(&pdev->dev, "failed to register pincontrol\n");
921 err = PTR_ERR(padctl->pinctrl);
922 goto reset;
925 phy = devm_phy_create(&pdev->dev, NULL, &pcie_phy_ops);
926 if (IS_ERR(phy)) {
927 err = PTR_ERR(phy);
928 goto reset;
931 padctl->phys[TEGRA_XUSB_PADCTL_PCIE] = phy;
932 phy_set_drvdata(phy, padctl);
934 phy = devm_phy_create(&pdev->dev, NULL, &sata_phy_ops);
935 if (IS_ERR(phy)) {
936 err = PTR_ERR(phy);
937 goto reset;
940 padctl->phys[TEGRA_XUSB_PADCTL_SATA] = phy;
941 phy_set_drvdata(phy, padctl);
943 padctl->provider = devm_of_phy_provider_register(&pdev->dev,
944 tegra_xusb_padctl_xlate);
945 if (IS_ERR(padctl->provider)) {
946 err = PTR_ERR(padctl->provider);
947 dev_err(&pdev->dev, "failed to register PHYs: %d\n", err);
948 goto reset;
951 return 0;
953 reset:
954 reset_control_assert(padctl->rst);
955 return err;
957 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_legacy_probe);
959 int tegra_xusb_padctl_legacy_remove(struct platform_device *pdev)
961 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
962 int err;
964 err = reset_control_assert(padctl->rst);
965 if (err < 0)
966 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
968 return err;
970 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_legacy_remove);