treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / pinctrl / tegra / pinctrl-tegra-xusb.c
blob6f7b3767f453e0f209c00a10574d7cfb47f3cf5d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
4 */
6 #include <linux/delay.h>
7 #include <linux/io.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/phy/phy.h>
11 #include <linux/pinctrl/pinctrl.h>
12 #include <linux/pinctrl/pinmux.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset.h>
15 #include <linux/slab.h>
17 #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
19 #include "../core.h"
20 #include "../pinctrl-utils.h"
22 #define XUSB_PADCTL_ELPG_PROGRAM 0x01c
23 #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN (1 << 26)
24 #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY (1 << 25)
25 #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN (1 << 24)
27 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1 0x040
28 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET (1 << 19)
29 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK (0xf << 12)
30 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST (1 << 1)
32 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2 0x044
33 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN (1 << 6)
34 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN (1 << 5)
35 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL (1 << 4)
37 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1 0x138
38 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET (1 << 27)
39 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE (1 << 24)
40 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD (1 << 3)
41 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST (1 << 1)
42 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ (1 << 0)
44 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1 0x148
45 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD (1 << 1)
46 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ (1 << 0)
48 struct tegra_xusb_padctl_function {
49 const char *name;
50 const char * const *groups;
51 unsigned int num_groups;
54 struct tegra_xusb_padctl_soc {
55 const struct pinctrl_pin_desc *pins;
56 unsigned int num_pins;
58 const struct tegra_xusb_padctl_function *functions;
59 unsigned int num_functions;
61 const struct tegra_xusb_padctl_lane *lanes;
62 unsigned int num_lanes;
65 struct tegra_xusb_padctl_lane {
66 const char *name;
68 unsigned int offset;
69 unsigned int shift;
70 unsigned int mask;
71 unsigned int iddq;
73 const unsigned int *funcs;
74 unsigned int num_funcs;
77 struct tegra_xusb_padctl {
78 struct device *dev;
79 void __iomem *regs;
80 struct mutex lock;
81 struct reset_control *rst;
83 const struct tegra_xusb_padctl_soc *soc;
84 struct pinctrl_dev *pinctrl;
85 struct pinctrl_desc desc;
87 struct phy_provider *provider;
88 struct phy *phys[2];
90 unsigned int enable;
93 static inline void padctl_writel(struct tegra_xusb_padctl *padctl, u32 value,
94 unsigned long offset)
96 writel(value, padctl->regs + offset);
99 static inline u32 padctl_readl(struct tegra_xusb_padctl *padctl,
100 unsigned long offset)
102 return readl(padctl->regs + offset);
105 static int tegra_xusb_padctl_get_groups_count(struct pinctrl_dev *pinctrl)
107 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
109 return padctl->soc->num_pins;
112 static const char *tegra_xusb_padctl_get_group_name(struct pinctrl_dev *pinctrl,
113 unsigned int group)
115 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
117 return padctl->soc->pins[group].name;
120 static int tegra_xusb_padctl_get_group_pins(struct pinctrl_dev *pinctrl,
121 unsigned group,
122 const unsigned **pins,
123 unsigned *num_pins)
126 * For the tegra-xusb pad controller groups are synonomous
127 * with lanes/pins and there is always one lane/pin per group.
129 *pins = &pinctrl->desc->pins[group].number;
130 *num_pins = 1;
132 return 0;
135 enum tegra_xusb_padctl_param {
136 TEGRA_XUSB_PADCTL_IDDQ,
139 static const struct tegra_xusb_padctl_property {
140 const char *name;
141 enum tegra_xusb_padctl_param param;
142 } properties[] = {
143 { "nvidia,iddq", TEGRA_XUSB_PADCTL_IDDQ },
146 #define TEGRA_XUSB_PADCTL_PACK(param, value) ((param) << 16 | (value))
147 #define TEGRA_XUSB_PADCTL_UNPACK_PARAM(config) ((config) >> 16)
148 #define TEGRA_XUSB_PADCTL_UNPACK_VALUE(config) ((config) & 0xffff)
150 static int tegra_xusb_padctl_parse_subnode(struct tegra_xusb_padctl *padctl,
151 struct device_node *np,
152 struct pinctrl_map **maps,
153 unsigned int *reserved_maps,
154 unsigned int *num_maps)
156 unsigned int i, reserve = 0, num_configs = 0;
157 unsigned long config, *configs = NULL;
158 const char *function, *group;
159 struct property *prop;
160 int err = 0;
161 u32 value;
163 err = of_property_read_string(np, "nvidia,function", &function);
164 if (err < 0) {
165 if (err != -EINVAL)
166 return err;
168 function = NULL;
171 for (i = 0; i < ARRAY_SIZE(properties); i++) {
172 err = of_property_read_u32(np, properties[i].name, &value);
173 if (err < 0) {
174 if (err == -EINVAL)
175 continue;
177 goto out;
180 config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, value);
182 err = pinctrl_utils_add_config(padctl->pinctrl, &configs,
183 &num_configs, config);
184 if (err < 0)
185 goto out;
188 if (function)
189 reserve++;
191 if (num_configs)
192 reserve++;
194 err = of_property_count_strings(np, "nvidia,lanes");
195 if (err < 0)
196 goto out;
198 reserve *= err;
200 err = pinctrl_utils_reserve_map(padctl->pinctrl, maps, reserved_maps,
201 num_maps, reserve);
202 if (err < 0)
203 goto out;
205 of_property_for_each_string(np, "nvidia,lanes", prop, group) {
206 if (function) {
207 err = pinctrl_utils_add_map_mux(padctl->pinctrl, maps,
208 reserved_maps, num_maps, group,
209 function);
210 if (err < 0)
211 goto out;
214 if (num_configs) {
215 err = pinctrl_utils_add_map_configs(padctl->pinctrl,
216 maps, reserved_maps, num_maps, group,
217 configs, num_configs,
218 PIN_MAP_TYPE_CONFIGS_GROUP);
219 if (err < 0)
220 goto out;
224 err = 0;
226 out:
227 kfree(configs);
228 return err;
231 static int tegra_xusb_padctl_dt_node_to_map(struct pinctrl_dev *pinctrl,
232 struct device_node *parent,
233 struct pinctrl_map **maps,
234 unsigned int *num_maps)
236 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
237 unsigned int reserved_maps = 0;
238 struct device_node *np;
239 int err;
241 *num_maps = 0;
242 *maps = NULL;
244 for_each_child_of_node(parent, np) {
245 err = tegra_xusb_padctl_parse_subnode(padctl, np, maps,
246 &reserved_maps,
247 num_maps);
248 if (err < 0) {
249 of_node_put(np);
250 return err;
254 return 0;
257 static const struct pinctrl_ops tegra_xusb_padctl_pinctrl_ops = {
258 .get_groups_count = tegra_xusb_padctl_get_groups_count,
259 .get_group_name = tegra_xusb_padctl_get_group_name,
260 .get_group_pins = tegra_xusb_padctl_get_group_pins,
261 .dt_node_to_map = tegra_xusb_padctl_dt_node_to_map,
262 .dt_free_map = pinctrl_utils_free_map,
265 static int tegra_xusb_padctl_get_functions_count(struct pinctrl_dev *pinctrl)
267 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
269 return padctl->soc->num_functions;
272 static const char *
273 tegra_xusb_padctl_get_function_name(struct pinctrl_dev *pinctrl,
274 unsigned int function)
276 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
278 return padctl->soc->functions[function].name;
281 static int tegra_xusb_padctl_get_function_groups(struct pinctrl_dev *pinctrl,
282 unsigned int function,
283 const char * const **groups,
284 unsigned * const num_groups)
286 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
288 *num_groups = padctl->soc->functions[function].num_groups;
289 *groups = padctl->soc->functions[function].groups;
291 return 0;
294 static int tegra_xusb_padctl_pinmux_set(struct pinctrl_dev *pinctrl,
295 unsigned int function,
296 unsigned int group)
298 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
299 const struct tegra_xusb_padctl_lane *lane;
300 unsigned int i;
301 u32 value;
303 lane = &padctl->soc->lanes[group];
305 for (i = 0; i < lane->num_funcs; i++)
306 if (lane->funcs[i] == function)
307 break;
309 if (i >= lane->num_funcs)
310 return -EINVAL;
312 value = padctl_readl(padctl, lane->offset);
313 value &= ~(lane->mask << lane->shift);
314 value |= i << lane->shift;
315 padctl_writel(padctl, value, lane->offset);
317 return 0;
320 static const struct pinmux_ops tegra_xusb_padctl_pinmux_ops = {
321 .get_functions_count = tegra_xusb_padctl_get_functions_count,
322 .get_function_name = tegra_xusb_padctl_get_function_name,
323 .get_function_groups = tegra_xusb_padctl_get_function_groups,
324 .set_mux = tegra_xusb_padctl_pinmux_set,
327 static int tegra_xusb_padctl_pinconf_group_get(struct pinctrl_dev *pinctrl,
328 unsigned int group,
329 unsigned long *config)
331 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
332 const struct tegra_xusb_padctl_lane *lane;
333 enum tegra_xusb_padctl_param param;
334 u32 value;
336 param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(*config);
337 lane = &padctl->soc->lanes[group];
339 switch (param) {
340 case TEGRA_XUSB_PADCTL_IDDQ:
341 /* lanes with iddq == 0 don't support this parameter */
342 if (lane->iddq == 0)
343 return -EINVAL;
345 value = padctl_readl(padctl, lane->offset);
347 if (value & BIT(lane->iddq))
348 value = 0;
349 else
350 value = 1;
352 *config = TEGRA_XUSB_PADCTL_PACK(param, value);
353 break;
355 default:
356 dev_err(padctl->dev, "invalid configuration parameter: %04x\n",
357 param);
358 return -ENOTSUPP;
361 return 0;
364 static int tegra_xusb_padctl_pinconf_group_set(struct pinctrl_dev *pinctrl,
365 unsigned int group,
366 unsigned long *configs,
367 unsigned int num_configs)
369 struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
370 const struct tegra_xusb_padctl_lane *lane;
371 enum tegra_xusb_padctl_param param;
372 unsigned long value;
373 unsigned int i;
374 u32 regval;
376 lane = &padctl->soc->lanes[group];
378 for (i = 0; i < num_configs; i++) {
379 param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(configs[i]);
380 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(configs[i]);
382 switch (param) {
383 case TEGRA_XUSB_PADCTL_IDDQ:
384 /* lanes with iddq == 0 don't support this parameter */
385 if (lane->iddq == 0)
386 return -EINVAL;
388 regval = padctl_readl(padctl, lane->offset);
390 if (value)
391 regval &= ~BIT(lane->iddq);
392 else
393 regval |= BIT(lane->iddq);
395 padctl_writel(padctl, regval, lane->offset);
396 break;
398 default:
399 dev_err(padctl->dev,
400 "invalid configuration parameter: %04x\n",
401 param);
402 return -ENOTSUPP;
406 return 0;
409 #ifdef CONFIG_DEBUG_FS
410 static const char *strip_prefix(const char *s)
412 const char *comma = strchr(s, ',');
413 if (!comma)
414 return s;
416 return comma + 1;
419 static void
420 tegra_xusb_padctl_pinconf_group_dbg_show(struct pinctrl_dev *pinctrl,
421 struct seq_file *s,
422 unsigned int group)
424 unsigned int i;
426 for (i = 0; i < ARRAY_SIZE(properties); i++) {
427 unsigned long config, value;
428 int err;
430 config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, 0);
432 err = tegra_xusb_padctl_pinconf_group_get(pinctrl, group,
433 &config);
434 if (err < 0)
435 continue;
437 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
439 seq_printf(s, "\n\t%s=%lu\n", strip_prefix(properties[i].name),
440 value);
444 static void
445 tegra_xusb_padctl_pinconf_config_dbg_show(struct pinctrl_dev *pinctrl,
446 struct seq_file *s,
447 unsigned long config)
449 enum tegra_xusb_padctl_param param;
450 const char *name = "unknown";
451 unsigned long value;
452 unsigned int i;
454 param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(config);
455 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
457 for (i = 0; i < ARRAY_SIZE(properties); i++) {
458 if (properties[i].param == param) {
459 name = properties[i].name;
460 break;
464 seq_printf(s, "%s=%lu", strip_prefix(name), value);
466 #endif
468 static const struct pinconf_ops tegra_xusb_padctl_pinconf_ops = {
469 .pin_config_group_get = tegra_xusb_padctl_pinconf_group_get,
470 .pin_config_group_set = tegra_xusb_padctl_pinconf_group_set,
471 #ifdef CONFIG_DEBUG_FS
472 .pin_config_group_dbg_show = tegra_xusb_padctl_pinconf_group_dbg_show,
473 .pin_config_config_dbg_show = tegra_xusb_padctl_pinconf_config_dbg_show,
474 #endif
477 static int tegra_xusb_padctl_enable(struct tegra_xusb_padctl *padctl)
479 u32 value;
481 mutex_lock(&padctl->lock);
483 if (padctl->enable++ > 0)
484 goto out;
486 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
487 value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
488 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
490 usleep_range(100, 200);
492 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
493 value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
494 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
496 usleep_range(100, 200);
498 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
499 value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
500 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
502 out:
503 mutex_unlock(&padctl->lock);
504 return 0;
507 static int tegra_xusb_padctl_disable(struct tegra_xusb_padctl *padctl)
509 u32 value;
511 mutex_lock(&padctl->lock);
513 if (WARN_ON(padctl->enable == 0))
514 goto out;
516 if (--padctl->enable > 0)
517 goto out;
519 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
520 value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
521 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
523 usleep_range(100, 200);
525 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
526 value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
527 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
529 usleep_range(100, 200);
531 value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
532 value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
533 padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
535 out:
536 mutex_unlock(&padctl->lock);
537 return 0;
540 static int tegra_xusb_phy_init(struct phy *phy)
542 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
544 return tegra_xusb_padctl_enable(padctl);
547 static int tegra_xusb_phy_exit(struct phy *phy)
549 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
551 return tegra_xusb_padctl_disable(padctl);
554 static int pcie_phy_power_on(struct phy *phy)
556 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
557 unsigned long timeout;
558 int err = -ETIMEDOUT;
559 u32 value;
561 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
562 value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK;
563 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
565 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
566 value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN |
567 XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN |
568 XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL;
569 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
571 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
572 value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
573 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
575 timeout = jiffies + msecs_to_jiffies(50);
577 while (time_before(jiffies, timeout)) {
578 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
579 if (value & XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET) {
580 err = 0;
581 break;
584 usleep_range(100, 200);
587 return err;
590 static int pcie_phy_power_off(struct phy *phy)
592 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
593 u32 value;
595 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
596 value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
597 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
599 return 0;
602 static const struct phy_ops pcie_phy_ops = {
603 .init = tegra_xusb_phy_init,
604 .exit = tegra_xusb_phy_exit,
605 .power_on = pcie_phy_power_on,
606 .power_off = pcie_phy_power_off,
607 .owner = THIS_MODULE,
610 static int sata_phy_power_on(struct phy *phy)
612 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
613 unsigned long timeout;
614 int err = -ETIMEDOUT;
615 u32 value;
617 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
618 value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
619 value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
620 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
622 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
623 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
624 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
625 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
627 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
628 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
629 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
631 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
632 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
633 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
635 timeout = jiffies + msecs_to_jiffies(50);
637 while (time_before(jiffies, timeout)) {
638 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
639 if (value & XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET) {
640 err = 0;
641 break;
644 usleep_range(100, 200);
647 return err;
650 static int sata_phy_power_off(struct phy *phy)
652 struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
653 u32 value;
655 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
656 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
657 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
659 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
660 value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
661 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
663 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
664 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
665 value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
666 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
668 value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
669 value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
670 value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
671 padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
673 return 0;
676 static const struct phy_ops sata_phy_ops = {
677 .init = tegra_xusb_phy_init,
678 .exit = tegra_xusb_phy_exit,
679 .power_on = sata_phy_power_on,
680 .power_off = sata_phy_power_off,
681 .owner = THIS_MODULE,
684 static struct phy *tegra_xusb_padctl_xlate(struct device *dev,
685 struct of_phandle_args *args)
687 struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
688 unsigned int index = args->args[0];
690 if (args->args_count <= 0)
691 return ERR_PTR(-EINVAL);
693 if (index >= ARRAY_SIZE(padctl->phys))
694 return ERR_PTR(-EINVAL);
696 return padctl->phys[index];
699 #define PIN_OTG_0 0
700 #define PIN_OTG_1 1
701 #define PIN_OTG_2 2
702 #define PIN_ULPI_0 3
703 #define PIN_HSIC_0 4
704 #define PIN_HSIC_1 5
705 #define PIN_PCIE_0 6
706 #define PIN_PCIE_1 7
707 #define PIN_PCIE_2 8
708 #define PIN_PCIE_3 9
709 #define PIN_PCIE_4 10
710 #define PIN_SATA_0 11
712 static const struct pinctrl_pin_desc tegra124_pins[] = {
713 PINCTRL_PIN(PIN_OTG_0, "otg-0"),
714 PINCTRL_PIN(PIN_OTG_1, "otg-1"),
715 PINCTRL_PIN(PIN_OTG_2, "otg-2"),
716 PINCTRL_PIN(PIN_ULPI_0, "ulpi-0"),
717 PINCTRL_PIN(PIN_HSIC_0, "hsic-0"),
718 PINCTRL_PIN(PIN_HSIC_1, "hsic-1"),
719 PINCTRL_PIN(PIN_PCIE_0, "pcie-0"),
720 PINCTRL_PIN(PIN_PCIE_1, "pcie-1"),
721 PINCTRL_PIN(PIN_PCIE_2, "pcie-2"),
722 PINCTRL_PIN(PIN_PCIE_3, "pcie-3"),
723 PINCTRL_PIN(PIN_PCIE_4, "pcie-4"),
724 PINCTRL_PIN(PIN_SATA_0, "sata-0"),
727 static const char * const tegra124_snps_groups[] = {
728 "otg-0",
729 "otg-1",
730 "otg-2",
731 "ulpi-0",
732 "hsic-0",
733 "hsic-1",
736 static const char * const tegra124_xusb_groups[] = {
737 "otg-0",
738 "otg-1",
739 "otg-2",
740 "ulpi-0",
741 "hsic-0",
742 "hsic-1",
745 static const char * const tegra124_uart_groups[] = {
746 "otg-0",
747 "otg-1",
748 "otg-2",
751 static const char * const tegra124_pcie_groups[] = {
752 "pcie-0",
753 "pcie-1",
754 "pcie-2",
755 "pcie-3",
756 "pcie-4",
759 static const char * const tegra124_usb3_groups[] = {
760 "pcie-0",
761 "pcie-1",
762 "sata-0",
765 static const char * const tegra124_sata_groups[] = {
766 "sata-0",
769 static const char * const tegra124_rsvd_groups[] = {
770 "otg-0",
771 "otg-1",
772 "otg-2",
773 "pcie-0",
774 "pcie-1",
775 "pcie-2",
776 "pcie-3",
777 "pcie-4",
778 "sata-0",
781 #define TEGRA124_FUNCTION(_name) \
783 .name = #_name, \
784 .num_groups = ARRAY_SIZE(tegra124_##_name##_groups), \
785 .groups = tegra124_##_name##_groups, \
788 static struct tegra_xusb_padctl_function tegra124_functions[] = {
789 TEGRA124_FUNCTION(snps),
790 TEGRA124_FUNCTION(xusb),
791 TEGRA124_FUNCTION(uart),
792 TEGRA124_FUNCTION(pcie),
793 TEGRA124_FUNCTION(usb3),
794 TEGRA124_FUNCTION(sata),
795 TEGRA124_FUNCTION(rsvd),
798 enum tegra124_function {
799 TEGRA124_FUNC_SNPS,
800 TEGRA124_FUNC_XUSB,
801 TEGRA124_FUNC_UART,
802 TEGRA124_FUNC_PCIE,
803 TEGRA124_FUNC_USB3,
804 TEGRA124_FUNC_SATA,
805 TEGRA124_FUNC_RSVD,
808 static const unsigned int tegra124_otg_functions[] = {
809 TEGRA124_FUNC_SNPS,
810 TEGRA124_FUNC_XUSB,
811 TEGRA124_FUNC_UART,
812 TEGRA124_FUNC_RSVD,
815 static const unsigned int tegra124_usb_functions[] = {
816 TEGRA124_FUNC_SNPS,
817 TEGRA124_FUNC_XUSB,
820 static const unsigned int tegra124_pci_functions[] = {
821 TEGRA124_FUNC_PCIE,
822 TEGRA124_FUNC_USB3,
823 TEGRA124_FUNC_SATA,
824 TEGRA124_FUNC_RSVD,
827 #define TEGRA124_LANE(_name, _offset, _shift, _mask, _iddq, _funcs) \
829 .name = _name, \
830 .offset = _offset, \
831 .shift = _shift, \
832 .mask = _mask, \
833 .iddq = _iddq, \
834 .num_funcs = ARRAY_SIZE(tegra124_##_funcs##_functions), \
835 .funcs = tegra124_##_funcs##_functions, \
838 static const struct tegra_xusb_padctl_lane tegra124_lanes[] = {
839 TEGRA124_LANE("otg-0", 0x004, 0, 0x3, 0, otg),
840 TEGRA124_LANE("otg-1", 0x004, 2, 0x3, 0, otg),
841 TEGRA124_LANE("otg-2", 0x004, 4, 0x3, 0, otg),
842 TEGRA124_LANE("ulpi-0", 0x004, 12, 0x1, 0, usb),
843 TEGRA124_LANE("hsic-0", 0x004, 14, 0x1, 0, usb),
844 TEGRA124_LANE("hsic-1", 0x004, 15, 0x1, 0, usb),
845 TEGRA124_LANE("pcie-0", 0x134, 16, 0x3, 1, pci),
846 TEGRA124_LANE("pcie-1", 0x134, 18, 0x3, 2, pci),
847 TEGRA124_LANE("pcie-2", 0x134, 20, 0x3, 3, pci),
848 TEGRA124_LANE("pcie-3", 0x134, 22, 0x3, 4, pci),
849 TEGRA124_LANE("pcie-4", 0x134, 24, 0x3, 5, pci),
850 TEGRA124_LANE("sata-0", 0x134, 26, 0x3, 6, pci),
853 static const struct tegra_xusb_padctl_soc tegra124_soc = {
854 .num_pins = ARRAY_SIZE(tegra124_pins),
855 .pins = tegra124_pins,
856 .num_functions = ARRAY_SIZE(tegra124_functions),
857 .functions = tegra124_functions,
858 .num_lanes = ARRAY_SIZE(tegra124_lanes),
859 .lanes = tegra124_lanes,
862 static const struct of_device_id tegra_xusb_padctl_of_match[] = {
863 { .compatible = "nvidia,tegra124-xusb-padctl", .data = &tegra124_soc },
866 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
868 /* predeclare these in order to silence sparse */
869 int tegra_xusb_padctl_legacy_probe(struct platform_device *pdev);
870 int tegra_xusb_padctl_legacy_remove(struct platform_device *pdev);
872 int tegra_xusb_padctl_legacy_probe(struct platform_device *pdev)
874 struct tegra_xusb_padctl *padctl;
875 const struct of_device_id *match;
876 struct phy *phy;
877 int err;
879 padctl = devm_kzalloc(&pdev->dev, sizeof(*padctl), GFP_KERNEL);
880 if (!padctl)
881 return -ENOMEM;
883 platform_set_drvdata(pdev, padctl);
884 mutex_init(&padctl->lock);
885 padctl->dev = &pdev->dev;
888 * Note that we can't replace this by of_device_get_match_data()
889 * because we need the separate matching table for this legacy code on
890 * Tegra124. of_device_get_match_data() would attempt to use the table
891 * from the updated driver and fail.
893 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
894 padctl->soc = match->data;
896 padctl->regs = devm_platform_ioremap_resource(pdev, 0);
897 if (IS_ERR(padctl->regs))
898 return PTR_ERR(padctl->regs);
900 padctl->rst = devm_reset_control_get_exclusive(&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);