soc/tegra: pmc: Add driver state syncing
[linux-2.6-microblaze.git] / drivers / soc / tegra / pmc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/soc/tegra/pmc.c
4  *
5  * Copyright (c) 2010 Google, Inc
6  * Copyright (c) 2018-2020, NVIDIA CORPORATION. All rights reserved.
7  *
8  * Author:
9  *      Colin Cross <ccross@google.com>
10  */
11
12 #define pr_fmt(fmt) "tegra-pmc: " fmt
13
14 #include <linux/arm-smccc.h>
15 #include <linux/clk.h>
16 #include <linux/clk-provider.h>
17 #include <linux/clkdev.h>
18 #include <linux/clk/clk-conf.h>
19 #include <linux/clk/tegra.h>
20 #include <linux/debugfs.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/err.h>
24 #include <linux/export.h>
25 #include <linux/init.h>
26 #include <linux/io.h>
27 #include <linux/iopoll.h>
28 #include <linux/irqdomain.h>
29 #include <linux/irq.h>
30 #include <linux/kernel.h>
31 #include <linux/of_address.h>
32 #include <linux/of_clk.h>
33 #include <linux/of.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_platform.h>
36 #include <linux/pinctrl/pinconf-generic.h>
37 #include <linux/pinctrl/pinconf.h>
38 #include <linux/pinctrl/pinctrl.h>
39 #include <linux/platform_device.h>
40 #include <linux/pm_domain.h>
41 #include <linux/pm_opp.h>
42 #include <linux/reboot.h>
43 #include <linux/regmap.h>
44 #include <linux/reset.h>
45 #include <linux/seq_file.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48
49 #include <soc/tegra/common.h>
50 #include <soc/tegra/fuse.h>
51 #include <soc/tegra/pmc.h>
52
53 #include <dt-bindings/interrupt-controller/arm-gic.h>
54 #include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
55 #include <dt-bindings/gpio/tegra186-gpio.h>
56 #include <dt-bindings/gpio/tegra194-gpio.h>
57 #include <dt-bindings/soc/tegra-pmc.h>
58
59 #define PMC_CNTRL                       0x0
60 #define  PMC_CNTRL_INTR_POLARITY        BIT(17) /* inverts INTR polarity */
61 #define  PMC_CNTRL_CPU_PWRREQ_OE        BIT(16) /* CPU pwr req enable */
62 #define  PMC_CNTRL_CPU_PWRREQ_POLARITY  BIT(15) /* CPU pwr req polarity */
63 #define  PMC_CNTRL_SIDE_EFFECT_LP0      BIT(14) /* LP0 when CPU pwr gated */
64 #define  PMC_CNTRL_SYSCLK_OE            BIT(11) /* system clock enable */
65 #define  PMC_CNTRL_SYSCLK_POLARITY      BIT(10) /* sys clk polarity */
66 #define  PMC_CNTRL_PWRREQ_POLARITY      BIT(8)
67 #define  PMC_CNTRL_BLINK_EN             7
68 #define  PMC_CNTRL_MAIN_RST             BIT(4)
69
70 #define PMC_WAKE_MASK                   0x0c
71 #define PMC_WAKE_LEVEL                  0x10
72 #define PMC_WAKE_STATUS                 0x14
73 #define PMC_SW_WAKE_STATUS              0x18
74 #define PMC_DPD_PADS_ORIDE              0x1c
75 #define  PMC_DPD_PADS_ORIDE_BLINK       20
76
77 #define DPD_SAMPLE                      0x020
78 #define  DPD_SAMPLE_ENABLE              BIT(0)
79 #define  DPD_SAMPLE_DISABLE             (0 << 0)
80
81 #define PWRGATE_TOGGLE                  0x30
82 #define  PWRGATE_TOGGLE_START           BIT(8)
83
84 #define REMOVE_CLAMPING                 0x34
85
86 #define PWRGATE_STATUS                  0x38
87
88 #define PMC_BLINK_TIMER                 0x40
89 #define PMC_IMPL_E_33V_PWR              0x40
90
91 #define PMC_PWR_DET                     0x48
92
93 #define PMC_SCRATCH0_MODE_RECOVERY      BIT(31)
94 #define PMC_SCRATCH0_MODE_BOOTLOADER    BIT(30)
95 #define PMC_SCRATCH0_MODE_RCM           BIT(1)
96 #define PMC_SCRATCH0_MODE_MASK          (PMC_SCRATCH0_MODE_RECOVERY | \
97                                          PMC_SCRATCH0_MODE_BOOTLOADER | \
98                                          PMC_SCRATCH0_MODE_RCM)
99
100 #define PMC_CPUPWRGOOD_TIMER            0xc8
101 #define PMC_CPUPWROFF_TIMER             0xcc
102 #define PMC_COREPWRGOOD_TIMER           0x3c
103 #define PMC_COREPWROFF_TIMER            0xe0
104
105 #define PMC_PWR_DET_VALUE               0xe4
106
107 #define PMC_USB_DEBOUNCE_DEL            0xec
108 #define PMC_USB_AO                      0xf0
109
110 #define PMC_SCRATCH41                   0x140
111
112 #define PMC_WAKE2_MASK                  0x160
113 #define PMC_WAKE2_LEVEL                 0x164
114 #define PMC_WAKE2_STATUS                0x168
115 #define PMC_SW_WAKE2_STATUS             0x16c
116
117 #define PMC_CLK_OUT_CNTRL               0x1a8
118 #define  PMC_CLK_OUT_MUX_MASK           GENMASK(1, 0)
119 #define PMC_SENSOR_CTRL                 0x1b0
120 #define  PMC_SENSOR_CTRL_SCRATCH_WRITE  BIT(2)
121 #define  PMC_SENSOR_CTRL_ENABLE_RST     BIT(1)
122
123 #define  PMC_RST_STATUS_POR             0
124 #define  PMC_RST_STATUS_WATCHDOG        1
125 #define  PMC_RST_STATUS_SENSOR          2
126 #define  PMC_RST_STATUS_SW_MAIN         3
127 #define  PMC_RST_STATUS_LP0             4
128 #define  PMC_RST_STATUS_AOTAG           5
129
130 #define IO_DPD_REQ                      0x1b8
131 #define  IO_DPD_REQ_CODE_IDLE           (0U << 30)
132 #define  IO_DPD_REQ_CODE_OFF            (1U << 30)
133 #define  IO_DPD_REQ_CODE_ON             (2U << 30)
134 #define  IO_DPD_REQ_CODE_MASK           (3U << 30)
135
136 #define IO_DPD_STATUS                   0x1bc
137 #define IO_DPD2_REQ                     0x1c0
138 #define IO_DPD2_STATUS                  0x1c4
139 #define SEL_DPD_TIM                     0x1c8
140
141 #define PMC_UTMIP_UHSIC_TRIGGERS        0x1ec
142 #define PMC_UTMIP_UHSIC_SAVED_STATE     0x1f0
143
144 #define PMC_UTMIP_TERM_PAD_CFG          0x1f8
145 #define PMC_UTMIP_UHSIC_SLEEP_CFG       0x1fc
146 #define PMC_UTMIP_UHSIC_FAKE            0x218
147
148 #define PMC_SCRATCH54                   0x258
149 #define  PMC_SCRATCH54_DATA_SHIFT       8
150 #define  PMC_SCRATCH54_ADDR_SHIFT       0
151
152 #define PMC_SCRATCH55                   0x25c
153 #define  PMC_SCRATCH55_RESET_TEGRA      BIT(31)
154 #define  PMC_SCRATCH55_CNTRL_ID_SHIFT   27
155 #define  PMC_SCRATCH55_PINMUX_SHIFT     24
156 #define  PMC_SCRATCH55_16BITOP          BIT(15)
157 #define  PMC_SCRATCH55_CHECKSUM_SHIFT   16
158 #define  PMC_SCRATCH55_I2CSLV1_SHIFT    0
159
160 #define  PMC_UTMIP_UHSIC_LINE_WAKEUP    0x26c
161
162 #define PMC_UTMIP_BIAS_MASTER_CNTRL     0x270
163 #define PMC_UTMIP_MASTER_CONFIG         0x274
164 #define PMC_UTMIP_UHSIC2_TRIGGERS       0x27c
165 #define PMC_UTMIP_MASTER2_CONFIG        0x29c
166
167 #define GPU_RG_CNTRL                    0x2d4
168
169 #define PMC_UTMIP_PAD_CFG0              0x4c0
170 #define PMC_UTMIP_UHSIC_SLEEP_CFG1      0x4d0
171 #define PMC_UTMIP_SLEEPWALK_P3          0x4e0
172 /* Tegra186 and later */
173 #define WAKE_AOWAKE_CNTRL(x) (0x000 + ((x) << 2))
174 #define WAKE_AOWAKE_CNTRL_LEVEL (1 << 3)
175 #define WAKE_AOWAKE_MASK_W(x) (0x180 + ((x) << 2))
176 #define WAKE_AOWAKE_MASK_R(x) (0x300 + ((x) << 2))
177 #define WAKE_AOWAKE_STATUS_W(x) (0x30c + ((x) << 2))
178 #define WAKE_AOWAKE_STATUS_R(x) (0x48c + ((x) << 2))
179 #define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))
180 #define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2))
181 #define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))
182
183 #define WAKE_AOWAKE_CTRL 0x4f4
184 #define  WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
185
186 /* for secure PMC */
187 #define TEGRA_SMC_PMC           0xc2fffe00
188 #define  TEGRA_SMC_PMC_READ     0xaa
189 #define  TEGRA_SMC_PMC_WRITE    0xbb
190
191 struct pmc_clk {
192         struct clk_hw   hw;
193         unsigned long   offs;
194         u32             mux_shift;
195         u32             force_en_shift;
196 };
197
198 #define to_pmc_clk(_hw) container_of(_hw, struct pmc_clk, hw)
199
200 struct pmc_clk_gate {
201         struct clk_hw   hw;
202         unsigned long   offs;
203         u32             shift;
204 };
205
206 #define to_pmc_clk_gate(_hw) container_of(_hw, struct pmc_clk_gate, hw)
207
208 struct pmc_clk_init_data {
209         char *name;
210         const char *const *parents;
211         int num_parents;
212         int clk_id;
213         u8 mux_shift;
214         u8 force_en_shift;
215 };
216
217 static const char * const clk_out1_parents[] = { "osc", "osc_div2",
218         "osc_div4", "extern1",
219 };
220
221 static const char * const clk_out2_parents[] = { "osc", "osc_div2",
222         "osc_div4", "extern2",
223 };
224
225 static const char * const clk_out3_parents[] = { "osc", "osc_div2",
226         "osc_div4", "extern3",
227 };
228
229 static const struct pmc_clk_init_data tegra_pmc_clks_data[] = {
230         {
231                 .name = "pmc_clk_out_1",
232                 .parents = clk_out1_parents,
233                 .num_parents = ARRAY_SIZE(clk_out1_parents),
234                 .clk_id = TEGRA_PMC_CLK_OUT_1,
235                 .mux_shift = 6,
236                 .force_en_shift = 2,
237         },
238         {
239                 .name = "pmc_clk_out_2",
240                 .parents = clk_out2_parents,
241                 .num_parents = ARRAY_SIZE(clk_out2_parents),
242                 .clk_id = TEGRA_PMC_CLK_OUT_2,
243                 .mux_shift = 14,
244                 .force_en_shift = 10,
245         },
246         {
247                 .name = "pmc_clk_out_3",
248                 .parents = clk_out3_parents,
249                 .num_parents = ARRAY_SIZE(clk_out3_parents),
250                 .clk_id = TEGRA_PMC_CLK_OUT_3,
251                 .mux_shift = 22,
252                 .force_en_shift = 18,
253         },
254 };
255
256 struct tegra_powergate {
257         struct generic_pm_domain genpd;
258         struct tegra_pmc *pmc;
259         unsigned int id;
260         struct clk **clks;
261         unsigned int num_clks;
262         unsigned long *clk_rates;
263         struct reset_control *reset;
264 };
265
266 struct tegra_io_pad_soc {
267         enum tegra_io_pad id;
268         unsigned int dpd;
269         unsigned int voltage;
270         const char *name;
271 };
272
273 struct tegra_pmc_regs {
274         unsigned int scratch0;
275         unsigned int dpd_req;
276         unsigned int dpd_status;
277         unsigned int dpd2_req;
278         unsigned int dpd2_status;
279         unsigned int rst_status;
280         unsigned int rst_source_shift;
281         unsigned int rst_source_mask;
282         unsigned int rst_level_shift;
283         unsigned int rst_level_mask;
284 };
285
286 struct tegra_wake_event {
287         const char *name;
288         unsigned int id;
289         unsigned int irq;
290         struct {
291                 unsigned int instance;
292                 unsigned int pin;
293         } gpio;
294 };
295
296 #define TEGRA_WAKE_IRQ(_name, _id, _irq)                \
297         {                                               \
298                 .name = _name,                          \
299                 .id = _id,                              \
300                 .irq = _irq,                            \
301                 .gpio = {                               \
302                         .instance = UINT_MAX,           \
303                         .pin = UINT_MAX,                \
304                 },                                      \
305         }
306
307 #define TEGRA_WAKE_GPIO(_name, _id, _instance, _pin)    \
308         {                                               \
309                 .name = _name,                          \
310                 .id = _id,                              \
311                 .irq = 0,                               \
312                 .gpio = {                               \
313                         .instance = _instance,          \
314                         .pin = _pin,                    \
315                 },                                      \
316         }
317
318 struct tegra_pmc_soc {
319         unsigned int num_powergates;
320         const char *const *powergates;
321         unsigned int num_cpu_powergates;
322         const u8 *cpu_powergates;
323
324         bool has_tsense_reset;
325         bool has_gpu_clamps;
326         bool needs_mbist_war;
327         bool has_impl_33v_pwr;
328         bool maybe_tz_only;
329
330         const struct tegra_io_pad_soc *io_pads;
331         unsigned int num_io_pads;
332
333         const struct pinctrl_pin_desc *pin_descs;
334         unsigned int num_pin_descs;
335
336         const struct tegra_pmc_regs *regs;
337         void (*init)(struct tegra_pmc *pmc);
338         void (*setup_irq_polarity)(struct tegra_pmc *pmc,
339                                    struct device_node *np,
340                                    bool invert);
341         int (*irq_set_wake)(struct irq_data *data, unsigned int on);
342         int (*irq_set_type)(struct irq_data *data, unsigned int type);
343         int (*powergate_set)(struct tegra_pmc *pmc, unsigned int id,
344                              bool new_state);
345
346         const char * const *reset_sources;
347         unsigned int num_reset_sources;
348         const char * const *reset_levels;
349         unsigned int num_reset_levels;
350
351         /*
352          * These describe events that can wake the system from sleep (i.e.
353          * LP0 or SC7). Wakeup from other sleep states (such as LP1 or LP2)
354          * are dealt with in the LIC.
355          */
356         const struct tegra_wake_event *wake_events;
357         unsigned int num_wake_events;
358
359         const struct pmc_clk_init_data *pmc_clks_data;
360         unsigned int num_pmc_clks;
361         bool has_blink_output;
362         bool has_usb_sleepwalk;
363 };
364
365 /**
366  * struct tegra_pmc - NVIDIA Tegra PMC
367  * @dev: pointer to PMC device structure
368  * @base: pointer to I/O remapped register region
369  * @wake: pointer to I/O remapped region for WAKE registers
370  * @aotag: pointer to I/O remapped region for AOTAG registers
371  * @scratch: pointer to I/O remapped region for scratch registers
372  * @clk: pointer to pclk clock
373  * @soc: pointer to SoC data structure
374  * @tz_only: flag specifying if the PMC can only be accessed via TrustZone
375  * @debugfs: pointer to debugfs entry
376  * @rate: currently configured rate of pclk
377  * @suspend_mode: lowest suspend mode available
378  * @cpu_good_time: CPU power good time (in microseconds)
379  * @cpu_off_time: CPU power off time (in microsecends)
380  * @core_osc_time: core power good OSC time (in microseconds)
381  * @core_pmu_time: core power good PMU time (in microseconds)
382  * @core_off_time: core power off time (in microseconds)
383  * @corereq_high: core power request is active-high
384  * @sysclkreq_high: system clock request is active-high
385  * @combined_req: combined power request for CPU & core
386  * @cpu_pwr_good_en: CPU power good signal is enabled
387  * @lp0_vec_phys: physical base address of the LP0 warm boot code
388  * @lp0_vec_size: size of the LP0 warm boot code
389  * @powergates_available: Bitmap of available power gates
390  * @powergates_lock: mutex for power gate register access
391  * @pctl_dev: pin controller exposed by the PMC
392  * @domain: IRQ domain provided by the PMC
393  * @irq: chip implementation for the IRQ domain
394  * @clk_nb: pclk clock changes handler
395  */
396 struct tegra_pmc {
397         struct device *dev;
398         void __iomem *base;
399         void __iomem *wake;
400         void __iomem *aotag;
401         void __iomem *scratch;
402         struct clk *clk;
403         struct dentry *debugfs;
404
405         const struct tegra_pmc_soc *soc;
406         bool tz_only;
407
408         unsigned long rate;
409
410         enum tegra_suspend_mode suspend_mode;
411         u32 cpu_good_time;
412         u32 cpu_off_time;
413         u32 core_osc_time;
414         u32 core_pmu_time;
415         u32 core_off_time;
416         bool corereq_high;
417         bool sysclkreq_high;
418         bool combined_req;
419         bool cpu_pwr_good_en;
420         u32 lp0_vec_phys;
421         u32 lp0_vec_size;
422         DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
423
424         struct mutex powergates_lock;
425
426         struct pinctrl_dev *pctl_dev;
427
428         struct irq_domain *domain;
429         struct irq_chip irq;
430
431         struct notifier_block clk_nb;
432
433         bool core_domain_state_synced;
434         bool core_domain_registered;
435 };
436
437 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
438         .base = NULL,
439         .suspend_mode = TEGRA_SUSPEND_NONE,
440 };
441
442 static inline struct tegra_powergate *
443 to_powergate(struct generic_pm_domain *domain)
444 {
445         return container_of(domain, struct tegra_powergate, genpd);
446 }
447
448 static u32 tegra_pmc_readl(struct tegra_pmc *pmc, unsigned long offset)
449 {
450         struct arm_smccc_res res;
451
452         if (pmc->tz_only) {
453                 arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_READ, offset, 0, 0,
454                               0, 0, 0, &res);
455                 if (res.a0) {
456                         if (pmc->dev)
457                                 dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
458                                          __func__, res.a0);
459                         else
460                                 pr_warn("%s(): SMC failed: %lu\n", __func__,
461                                         res.a0);
462                 }
463
464                 return res.a1;
465         }
466
467         return readl(pmc->base + offset);
468 }
469
470 static void tegra_pmc_writel(struct tegra_pmc *pmc, u32 value,
471                              unsigned long offset)
472 {
473         struct arm_smccc_res res;
474
475         if (pmc->tz_only) {
476                 arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_WRITE, offset,
477                               value, 0, 0, 0, 0, &res);
478                 if (res.a0) {
479                         if (pmc->dev)
480                                 dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
481                                          __func__, res.a0);
482                         else
483                                 pr_warn("%s(): SMC failed: %lu\n", __func__,
484                                         res.a0);
485                 }
486         } else {
487                 writel(value, pmc->base + offset);
488         }
489 }
490
491 static u32 tegra_pmc_scratch_readl(struct tegra_pmc *pmc, unsigned long offset)
492 {
493         if (pmc->tz_only)
494                 return tegra_pmc_readl(pmc, offset);
495
496         return readl(pmc->scratch + offset);
497 }
498
499 static void tegra_pmc_scratch_writel(struct tegra_pmc *pmc, u32 value,
500                                      unsigned long offset)
501 {
502         if (pmc->tz_only)
503                 tegra_pmc_writel(pmc, value, offset);
504         else
505                 writel(value, pmc->scratch + offset);
506 }
507
508 /*
509  * TODO Figure out a way to call this with the struct tegra_pmc * passed in.
510  * This currently doesn't work because readx_poll_timeout() can only operate
511  * on functions that take a single argument.
512  */
513 static inline bool tegra_powergate_state(int id)
514 {
515         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
516                 return (tegra_pmc_readl(pmc, GPU_RG_CNTRL) & 0x1) == 0;
517         else
518                 return (tegra_pmc_readl(pmc, PWRGATE_STATUS) & BIT(id)) != 0;
519 }
520
521 static inline bool tegra_powergate_is_valid(struct tegra_pmc *pmc, int id)
522 {
523         return (pmc->soc && pmc->soc->powergates[id]);
524 }
525
526 static inline bool tegra_powergate_is_available(struct tegra_pmc *pmc, int id)
527 {
528         return test_bit(id, pmc->powergates_available);
529 }
530
531 static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
532 {
533         unsigned int i;
534
535         if (!pmc || !pmc->soc || !name)
536                 return -EINVAL;
537
538         for (i = 0; i < pmc->soc->num_powergates; i++) {
539                 if (!tegra_powergate_is_valid(pmc, i))
540                         continue;
541
542                 if (!strcmp(name, pmc->soc->powergates[i]))
543                         return i;
544         }
545
546         return -ENODEV;
547 }
548
549 static int tegra20_powergate_set(struct tegra_pmc *pmc, unsigned int id,
550                                  bool new_state)
551 {
552         unsigned int retries = 100;
553         bool status;
554         int ret;
555
556         /*
557          * As per TRM documentation, the toggle command will be dropped by PMC
558          * if there is contention with a HW-initiated toggling (i.e. CPU core
559          * power-gated), the command should be retried in that case.
560          */
561         do {
562                 tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
563
564                 /* wait for PMC to execute the command */
565                 ret = readx_poll_timeout(tegra_powergate_state, id, status,
566                                          status == new_state, 1, 10);
567         } while (ret == -ETIMEDOUT && retries--);
568
569         return ret;
570 }
571
572 static inline bool tegra_powergate_toggle_ready(struct tegra_pmc *pmc)
573 {
574         return !(tegra_pmc_readl(pmc, PWRGATE_TOGGLE) & PWRGATE_TOGGLE_START);
575 }
576
577 static int tegra114_powergate_set(struct tegra_pmc *pmc, unsigned int id,
578                                   bool new_state)
579 {
580         bool status;
581         int err;
582
583         /* wait while PMC power gating is contended */
584         err = readx_poll_timeout(tegra_powergate_toggle_ready, pmc, status,
585                                  status == true, 1, 100);
586         if (err)
587                 return err;
588
589         tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
590
591         /* wait for PMC to accept the command */
592         err = readx_poll_timeout(tegra_powergate_toggle_ready, pmc, status,
593                                  status == true, 1, 100);
594         if (err)
595                 return err;
596
597         /* wait for PMC to execute the command */
598         err = readx_poll_timeout(tegra_powergate_state, id, status,
599                                  status == new_state, 10, 100000);
600         if (err)
601                 return err;
602
603         return 0;
604 }
605
606 /**
607  * tegra_powergate_set() - set the state of a partition
608  * @pmc: power management controller
609  * @id: partition ID
610  * @new_state: new state of the partition
611  */
612 static int tegra_powergate_set(struct tegra_pmc *pmc, unsigned int id,
613                                bool new_state)
614 {
615         int err;
616
617         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
618                 return -EINVAL;
619
620         mutex_lock(&pmc->powergates_lock);
621
622         if (tegra_powergate_state(id) == new_state) {
623                 mutex_unlock(&pmc->powergates_lock);
624                 return 0;
625         }
626
627         err = pmc->soc->powergate_set(pmc, id, new_state);
628
629         mutex_unlock(&pmc->powergates_lock);
630
631         return err;
632 }
633
634 static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc,
635                                              unsigned int id)
636 {
637         u32 mask;
638
639         mutex_lock(&pmc->powergates_lock);
640
641         /*
642          * On Tegra124 and later, the clamps for the GPU are controlled by a
643          * separate register (with different semantics).
644          */
645         if (id == TEGRA_POWERGATE_3D) {
646                 if (pmc->soc->has_gpu_clamps) {
647                         tegra_pmc_writel(pmc, 0, GPU_RG_CNTRL);
648                         goto out;
649                 }
650         }
651
652         /*
653          * Tegra 2 has a bug where PCIE and VDE clamping masks are
654          * swapped relatively to the partition ids
655          */
656         if (id == TEGRA_POWERGATE_VDEC)
657                 mask = (1 << TEGRA_POWERGATE_PCIE);
658         else if (id == TEGRA_POWERGATE_PCIE)
659                 mask = (1 << TEGRA_POWERGATE_VDEC);
660         else
661                 mask = (1 << id);
662
663         tegra_pmc_writel(pmc, mask, REMOVE_CLAMPING);
664
665 out:
666         mutex_unlock(&pmc->powergates_lock);
667
668         return 0;
669 }
670
671 static int tegra_powergate_prepare_clocks(struct tegra_powergate *pg)
672 {
673         unsigned long safe_rate = 100 * 1000 * 1000;
674         unsigned int i;
675         int err;
676
677         for (i = 0; i < pg->num_clks; i++) {
678                 pg->clk_rates[i] = clk_get_rate(pg->clks[i]);
679
680                 if (!pg->clk_rates[i]) {
681                         err = -EINVAL;
682                         goto out;
683                 }
684
685                 if (pg->clk_rates[i] <= safe_rate)
686                         continue;
687
688                 /*
689                  * We don't know whether voltage state is okay for the
690                  * current clock rate, hence it's better to temporally
691                  * switch clock to a safe rate which is suitable for
692                  * all voltages, before enabling the clock.
693                  */
694                 err = clk_set_rate(pg->clks[i], safe_rate);
695                 if (err)
696                         goto out;
697         }
698
699         return 0;
700
701 out:
702         while (i--)
703                 clk_set_rate(pg->clks[i], pg->clk_rates[i]);
704
705         return err;
706 }
707
708 static int tegra_powergate_unprepare_clocks(struct tegra_powergate *pg)
709 {
710         unsigned int i;
711         int err;
712
713         for (i = 0; i < pg->num_clks; i++) {
714                 err = clk_set_rate(pg->clks[i], pg->clk_rates[i]);
715                 if (err)
716                         return err;
717         }
718
719         return 0;
720 }
721
722 static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
723 {
724         unsigned int i;
725
726         for (i = 0; i < pg->num_clks; i++)
727                 clk_disable_unprepare(pg->clks[i]);
728 }
729
730 static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
731 {
732         unsigned int i;
733         int err;
734
735         for (i = 0; i < pg->num_clks; i++) {
736                 err = clk_prepare_enable(pg->clks[i]);
737                 if (err)
738                         goto out;
739         }
740
741         return 0;
742
743 out:
744         while (i--)
745                 clk_disable_unprepare(pg->clks[i]);
746
747         return err;
748 }
749
750 int __weak tegra210_clk_handle_mbist_war(unsigned int id)
751 {
752         return 0;
753 }
754
755 static int tegra_powergate_power_up(struct tegra_powergate *pg,
756                                     bool disable_clocks)
757 {
758         int err;
759
760         err = reset_control_assert(pg->reset);
761         if (err)
762                 return err;
763
764         usleep_range(10, 20);
765
766         err = tegra_powergate_set(pg->pmc, pg->id, true);
767         if (err < 0)
768                 return err;
769
770         usleep_range(10, 20);
771
772         err = tegra_powergate_prepare_clocks(pg);
773         if (err)
774                 goto powergate_off;
775
776         err = tegra_powergate_enable_clocks(pg);
777         if (err)
778                 goto unprepare_clks;
779
780         usleep_range(10, 20);
781
782         err = __tegra_powergate_remove_clamping(pg->pmc, pg->id);
783         if (err)
784                 goto disable_clks;
785
786         usleep_range(10, 20);
787
788         err = reset_control_deassert(pg->reset);
789         if (err)
790                 goto powergate_off;
791
792         usleep_range(10, 20);
793
794         if (pg->pmc->soc->needs_mbist_war)
795                 err = tegra210_clk_handle_mbist_war(pg->id);
796         if (err)
797                 goto disable_clks;
798
799         if (disable_clocks)
800                 tegra_powergate_disable_clocks(pg);
801
802         err = tegra_powergate_unprepare_clocks(pg);
803         if (err)
804                 return err;
805
806         return 0;
807
808 disable_clks:
809         tegra_powergate_disable_clocks(pg);
810         usleep_range(10, 20);
811
812 unprepare_clks:
813         tegra_powergate_unprepare_clocks(pg);
814
815 powergate_off:
816         tegra_powergate_set(pg->pmc, pg->id, false);
817
818         return err;
819 }
820
821 static int tegra_powergate_power_down(struct tegra_powergate *pg)
822 {
823         int err;
824
825         err = tegra_powergate_prepare_clocks(pg);
826         if (err)
827                 return err;
828
829         err = tegra_powergate_enable_clocks(pg);
830         if (err)
831                 goto unprepare_clks;
832
833         usleep_range(10, 20);
834
835         err = reset_control_assert(pg->reset);
836         if (err)
837                 goto disable_clks;
838
839         usleep_range(10, 20);
840
841         tegra_powergate_disable_clocks(pg);
842
843         usleep_range(10, 20);
844
845         err = tegra_powergate_set(pg->pmc, pg->id, false);
846         if (err)
847                 goto assert_resets;
848
849         err = tegra_powergate_unprepare_clocks(pg);
850         if (err)
851                 return err;
852
853         return 0;
854
855 assert_resets:
856         tegra_powergate_enable_clocks(pg);
857         usleep_range(10, 20);
858         reset_control_deassert(pg->reset);
859         usleep_range(10, 20);
860
861 disable_clks:
862         tegra_powergate_disable_clocks(pg);
863
864 unprepare_clks:
865         tegra_powergate_unprepare_clocks(pg);
866
867         return err;
868 }
869
870 static int tegra_genpd_power_on(struct generic_pm_domain *domain)
871 {
872         struct tegra_powergate *pg = to_powergate(domain);
873         struct device *dev = pg->pmc->dev;
874         int err;
875
876         err = tegra_powergate_power_up(pg, true);
877         if (err) {
878                 dev_err(dev, "failed to turn on PM domain %s: %d\n",
879                         pg->genpd.name, err);
880                 goto out;
881         }
882
883         reset_control_release(pg->reset);
884
885 out:
886         return err;
887 }
888
889 static int tegra_genpd_power_off(struct generic_pm_domain *domain)
890 {
891         struct tegra_powergate *pg = to_powergate(domain);
892         struct device *dev = pg->pmc->dev;
893         int err;
894
895         err = reset_control_acquire(pg->reset);
896         if (err < 0) {
897                 dev_err(dev, "failed to acquire resets for PM domain %s: %d\n",
898                         pg->genpd.name, err);
899                 return err;
900         }
901
902         err = tegra_powergate_power_down(pg);
903         if (err) {
904                 dev_err(dev, "failed to turn off PM domain %s: %d\n",
905                         pg->genpd.name, err);
906                 reset_control_release(pg->reset);
907         }
908
909         return err;
910 }
911
912 /**
913  * tegra_powergate_power_on() - power on partition
914  * @id: partition ID
915  */
916 int tegra_powergate_power_on(unsigned int id)
917 {
918         if (!tegra_powergate_is_available(pmc, id))
919                 return -EINVAL;
920
921         return tegra_powergate_set(pmc, id, true);
922 }
923 EXPORT_SYMBOL(tegra_powergate_power_on);
924
925 /**
926  * tegra_powergate_power_off() - power off partition
927  * @id: partition ID
928  */
929 int tegra_powergate_power_off(unsigned int id)
930 {
931         if (!tegra_powergate_is_available(pmc, id))
932                 return -EINVAL;
933
934         return tegra_powergate_set(pmc, id, false);
935 }
936 EXPORT_SYMBOL(tegra_powergate_power_off);
937
938 /**
939  * tegra_powergate_is_powered() - check if partition is powered
940  * @pmc: power management controller
941  * @id: partition ID
942  */
943 static int tegra_powergate_is_powered(struct tegra_pmc *pmc, unsigned int id)
944 {
945         if (!tegra_powergate_is_valid(pmc, id))
946                 return -EINVAL;
947
948         return tegra_powergate_state(id);
949 }
950
951 /**
952  * tegra_powergate_remove_clamping() - remove power clamps for partition
953  * @id: partition ID
954  */
955 int tegra_powergate_remove_clamping(unsigned int id)
956 {
957         if (!tegra_powergate_is_available(pmc, id))
958                 return -EINVAL;
959
960         return __tegra_powergate_remove_clamping(pmc, id);
961 }
962 EXPORT_SYMBOL(tegra_powergate_remove_clamping);
963
964 /**
965  * tegra_powergate_sequence_power_up() - power up partition
966  * @id: partition ID
967  * @clk: clock for partition
968  * @rst: reset for partition
969  *
970  * Must be called with clk disabled, and returns with clk enabled.
971  */
972 int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
973                                       struct reset_control *rst)
974 {
975         struct tegra_powergate *pg;
976         int err;
977
978         if (!tegra_powergate_is_available(pmc, id))
979                 return -EINVAL;
980
981         pg = kzalloc(sizeof(*pg), GFP_KERNEL);
982         if (!pg)
983                 return -ENOMEM;
984
985         pg->clk_rates = kzalloc(sizeof(*pg->clk_rates), GFP_KERNEL);
986         if (!pg->clk_rates) {
987                 kfree(pg->clks);
988                 return -ENOMEM;
989         }
990
991         pg->id = id;
992         pg->clks = &clk;
993         pg->num_clks = 1;
994         pg->reset = rst;
995         pg->pmc = pmc;
996
997         err = tegra_powergate_power_up(pg, false);
998         if (err)
999                 dev_err(pmc->dev, "failed to turn on partition %d: %d\n", id,
1000                         err);
1001
1002         kfree(pg->clk_rates);
1003         kfree(pg);
1004
1005         return err;
1006 }
1007 EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
1008
1009 /**
1010  * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
1011  * @pmc: power management controller
1012  * @cpuid: CPU partition ID
1013  *
1014  * Returns the partition ID corresponding to the CPU partition ID or a
1015  * negative error code on failure.
1016  */
1017 static int tegra_get_cpu_powergate_id(struct tegra_pmc *pmc,
1018                                       unsigned int cpuid)
1019 {
1020         if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
1021                 return pmc->soc->cpu_powergates[cpuid];
1022
1023         return -EINVAL;
1024 }
1025
1026 /**
1027  * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
1028  * @cpuid: CPU partition ID
1029  */
1030 bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
1031 {
1032         int id;
1033
1034         id = tegra_get_cpu_powergate_id(pmc, cpuid);
1035         if (id < 0)
1036                 return false;
1037
1038         return tegra_powergate_is_powered(pmc, id);
1039 }
1040
1041 /**
1042  * tegra_pmc_cpu_power_on() - power on CPU partition
1043  * @cpuid: CPU partition ID
1044  */
1045 int tegra_pmc_cpu_power_on(unsigned int cpuid)
1046 {
1047         int id;
1048
1049         id = tegra_get_cpu_powergate_id(pmc, cpuid);
1050         if (id < 0)
1051                 return id;
1052
1053         return tegra_powergate_set(pmc, id, true);
1054 }
1055
1056 /**
1057  * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
1058  * @cpuid: CPU partition ID
1059  */
1060 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
1061 {
1062         int id;
1063
1064         id = tegra_get_cpu_powergate_id(pmc, cpuid);
1065         if (id < 0)
1066                 return id;
1067
1068         return tegra_powergate_remove_clamping(id);
1069 }
1070
1071 static int tegra_pmc_restart_notify(struct notifier_block *this,
1072                                     unsigned long action, void *data)
1073 {
1074         const char *cmd = data;
1075         u32 value;
1076
1077         value = tegra_pmc_scratch_readl(pmc, pmc->soc->regs->scratch0);
1078         value &= ~PMC_SCRATCH0_MODE_MASK;
1079
1080         if (cmd) {
1081                 if (strcmp(cmd, "recovery") == 0)
1082                         value |= PMC_SCRATCH0_MODE_RECOVERY;
1083
1084                 if (strcmp(cmd, "bootloader") == 0)
1085                         value |= PMC_SCRATCH0_MODE_BOOTLOADER;
1086
1087                 if (strcmp(cmd, "forced-recovery") == 0)
1088                         value |= PMC_SCRATCH0_MODE_RCM;
1089         }
1090
1091         tegra_pmc_scratch_writel(pmc, value, pmc->soc->regs->scratch0);
1092
1093         /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
1094         value = tegra_pmc_readl(pmc, PMC_CNTRL);
1095         value |= PMC_CNTRL_MAIN_RST;
1096         tegra_pmc_writel(pmc, value, PMC_CNTRL);
1097
1098         return NOTIFY_DONE;
1099 }
1100
1101 static struct notifier_block tegra_pmc_restart_handler = {
1102         .notifier_call = tegra_pmc_restart_notify,
1103         .priority = 128,
1104 };
1105
1106 static int powergate_show(struct seq_file *s, void *data)
1107 {
1108         unsigned int i;
1109         int status;
1110
1111         seq_printf(s, " powergate powered\n");
1112         seq_printf(s, "------------------\n");
1113
1114         for (i = 0; i < pmc->soc->num_powergates; i++) {
1115                 status = tegra_powergate_is_powered(pmc, i);
1116                 if (status < 0)
1117                         continue;
1118
1119                 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
1120                            status ? "yes" : "no");
1121         }
1122
1123         return 0;
1124 }
1125
1126 DEFINE_SHOW_ATTRIBUTE(powergate);
1127
1128 static int tegra_powergate_debugfs_init(void)
1129 {
1130         pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
1131                                            &powergate_fops);
1132         if (!pmc->debugfs)
1133                 return -ENOMEM;
1134
1135         return 0;
1136 }
1137
1138 static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
1139                                        struct device_node *np)
1140 {
1141         struct clk *clk;
1142         unsigned int i, count;
1143         int err;
1144
1145         count = of_clk_get_parent_count(np);
1146         if (count == 0)
1147                 return -ENODEV;
1148
1149         pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
1150         if (!pg->clks)
1151                 return -ENOMEM;
1152
1153         pg->clk_rates = kcalloc(count, sizeof(*pg->clk_rates), GFP_KERNEL);
1154         if (!pg->clk_rates) {
1155                 kfree(pg->clks);
1156                 return -ENOMEM;
1157         }
1158
1159         for (i = 0; i < count; i++) {
1160                 pg->clks[i] = of_clk_get(np, i);
1161                 if (IS_ERR(pg->clks[i])) {
1162                         err = PTR_ERR(pg->clks[i]);
1163                         goto err;
1164                 }
1165         }
1166
1167         pg->num_clks = count;
1168
1169         return 0;
1170
1171 err:
1172         while (i--)
1173                 clk_put(pg->clks[i]);
1174
1175         kfree(pg->clk_rates);
1176         kfree(pg->clks);
1177
1178         return err;
1179 }
1180
1181 static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
1182                                          struct device_node *np, bool off)
1183 {
1184         struct device *dev = pg->pmc->dev;
1185         int err;
1186
1187         pg->reset = of_reset_control_array_get_exclusive_released(np);
1188         if (IS_ERR(pg->reset)) {
1189                 err = PTR_ERR(pg->reset);
1190                 dev_err(dev, "failed to get device resets: %d\n", err);
1191                 return err;
1192         }
1193
1194         err = reset_control_acquire(pg->reset);
1195         if (err < 0) {
1196                 pr_err("failed to acquire resets: %d\n", err);
1197                 goto out;
1198         }
1199
1200         if (off) {
1201                 err = reset_control_assert(pg->reset);
1202         } else {
1203                 err = reset_control_deassert(pg->reset);
1204                 if (err < 0)
1205                         goto out;
1206
1207                 reset_control_release(pg->reset);
1208         }
1209
1210 out:
1211         if (err) {
1212                 reset_control_release(pg->reset);
1213                 reset_control_put(pg->reset);
1214         }
1215
1216         return err;
1217 }
1218
1219 static int tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
1220 {
1221         struct device *dev = pmc->dev;
1222         struct tegra_powergate *pg;
1223         int id, err = 0;
1224         bool off;
1225
1226         pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1227         if (!pg)
1228                 return -ENOMEM;
1229
1230         id = tegra_powergate_lookup(pmc, np->name);
1231         if (id < 0) {
1232                 dev_err(dev, "powergate lookup failed for %pOFn: %d\n", np, id);
1233                 err = -ENODEV;
1234                 goto free_mem;
1235         }
1236
1237         /*
1238          * Clear the bit for this powergate so it cannot be managed
1239          * directly via the legacy APIs for controlling powergates.
1240          */
1241         clear_bit(id, pmc->powergates_available);
1242
1243         pg->id = id;
1244         pg->genpd.name = np->name;
1245         pg->genpd.power_off = tegra_genpd_power_off;
1246         pg->genpd.power_on = tegra_genpd_power_on;
1247         pg->pmc = pmc;
1248
1249         off = !tegra_powergate_is_powered(pmc, pg->id);
1250
1251         err = tegra_powergate_of_get_clks(pg, np);
1252         if (err < 0) {
1253                 dev_err(dev, "failed to get clocks for %pOFn: %d\n", np, err);
1254                 goto set_available;
1255         }
1256
1257         err = tegra_powergate_of_get_resets(pg, np, off);
1258         if (err < 0) {
1259                 dev_err(dev, "failed to get resets for %pOFn: %d\n", np, err);
1260                 goto remove_clks;
1261         }
1262
1263         if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
1264                 if (off)
1265                         WARN_ON(tegra_powergate_power_up(pg, true));
1266
1267                 goto remove_resets;
1268         }
1269
1270         err = pm_genpd_init(&pg->genpd, NULL, off);
1271         if (err < 0) {
1272                 dev_err(dev, "failed to initialise PM domain %pOFn: %d\n", np,
1273                        err);
1274                 goto remove_resets;
1275         }
1276
1277         err = of_genpd_add_provider_simple(np, &pg->genpd);
1278         if (err < 0) {
1279                 dev_err(dev, "failed to add PM domain provider for %pOFn: %d\n",
1280                         np, err);
1281                 goto remove_genpd;
1282         }
1283
1284         dev_dbg(dev, "added PM domain %s\n", pg->genpd.name);
1285
1286         return 0;
1287
1288 remove_genpd:
1289         pm_genpd_remove(&pg->genpd);
1290
1291 remove_resets:
1292         reset_control_put(pg->reset);
1293
1294 remove_clks:
1295         while (pg->num_clks--)
1296                 clk_put(pg->clks[pg->num_clks]);
1297
1298         kfree(pg->clks);
1299
1300 set_available:
1301         set_bit(id, pmc->powergates_available);
1302
1303 free_mem:
1304         kfree(pg);
1305
1306         return err;
1307 }
1308
1309 bool tegra_pmc_core_domain_state_synced(void)
1310 {
1311         return pmc->core_domain_state_synced;
1312 }
1313
1314 static int
1315 tegra_pmc_core_pd_set_performance_state(struct generic_pm_domain *genpd,
1316                                         unsigned int level)
1317 {
1318         struct dev_pm_opp *opp;
1319         int err;
1320
1321         opp = dev_pm_opp_find_level_ceil(&genpd->dev, &level);
1322         if (IS_ERR(opp)) {
1323                 dev_err(&genpd->dev, "failed to find OPP for level %u: %pe\n",
1324                         level, opp);
1325                 return PTR_ERR(opp);
1326         }
1327
1328         mutex_lock(&pmc->powergates_lock);
1329         err = dev_pm_opp_set_opp(pmc->dev, opp);
1330         mutex_unlock(&pmc->powergates_lock);
1331
1332         dev_pm_opp_put(opp);
1333
1334         if (err) {
1335                 dev_err(&genpd->dev, "failed to set voltage to %duV: %d\n",
1336                         level, err);
1337                 return err;
1338         }
1339
1340         return 0;
1341 }
1342
1343 static unsigned int
1344 tegra_pmc_core_pd_opp_to_performance_state(struct generic_pm_domain *genpd,
1345                                            struct dev_pm_opp *opp)
1346 {
1347         return dev_pm_opp_get_level(opp);
1348 }
1349
1350 static int tegra_pmc_core_pd_add(struct tegra_pmc *pmc, struct device_node *np)
1351 {
1352         struct generic_pm_domain *genpd;
1353         const char *rname = "core";
1354         int err;
1355
1356         genpd = devm_kzalloc(pmc->dev, sizeof(*genpd), GFP_KERNEL);
1357         if (!genpd)
1358                 return -ENOMEM;
1359
1360         genpd->name = np->name;
1361         genpd->set_performance_state = tegra_pmc_core_pd_set_performance_state;
1362         genpd->opp_to_performance_state = tegra_pmc_core_pd_opp_to_performance_state;
1363
1364         err = devm_pm_opp_set_regulators(pmc->dev, &rname, 1);
1365         if (err)
1366                 return dev_err_probe(pmc->dev, err,
1367                                      "failed to set core OPP regulator\n");
1368
1369         err = pm_genpd_init(genpd, NULL, false);
1370         if (err) {
1371                 dev_err(pmc->dev, "failed to init core genpd: %d\n", err);
1372                 return err;
1373         }
1374
1375         err = of_genpd_add_provider_simple(np, genpd);
1376         if (err) {
1377                 dev_err(pmc->dev, "failed to add core genpd: %d\n", err);
1378                 goto remove_genpd;
1379         }
1380
1381         pmc->core_domain_registered = true;
1382
1383         return 0;
1384
1385 remove_genpd:
1386         pm_genpd_remove(genpd);
1387
1388         return err;
1389 }
1390
1391 static int tegra_powergate_init(struct tegra_pmc *pmc,
1392                                 struct device_node *parent)
1393 {
1394         struct of_phandle_args child_args, parent_args;
1395         struct device_node *np, *child;
1396         int err = 0;
1397
1398         /*
1399          * Core power domain is the parent of powergate domains, hence it
1400          * should be registered first.
1401          */
1402         np = of_get_child_by_name(parent, "core-domain");
1403         if (np) {
1404                 err = tegra_pmc_core_pd_add(pmc, np);
1405                 of_node_put(np);
1406                 if (err)
1407                         return err;
1408         }
1409
1410         np = of_get_child_by_name(parent, "powergates");
1411         if (!np)
1412                 return 0;
1413
1414         for_each_child_of_node(np, child) {
1415                 err = tegra_powergate_add(pmc, child);
1416                 if (err < 0) {
1417                         of_node_put(child);
1418                         break;
1419                 }
1420
1421                 if (of_parse_phandle_with_args(child, "power-domains",
1422                                                "#power-domain-cells",
1423                                                0, &parent_args))
1424                         continue;
1425
1426                 child_args.np = child;
1427                 child_args.args_count = 0;
1428
1429                 err = of_genpd_add_subdomain(&parent_args, &child_args);
1430                 of_node_put(parent_args.np);
1431                 if (err) {
1432                         of_node_put(child);
1433                         break;
1434                 }
1435         }
1436
1437         of_node_put(np);
1438
1439         return err;
1440 }
1441
1442 static void tegra_powergate_remove(struct generic_pm_domain *genpd)
1443 {
1444         struct tegra_powergate *pg = to_powergate(genpd);
1445
1446         reset_control_put(pg->reset);
1447
1448         while (pg->num_clks--)
1449                 clk_put(pg->clks[pg->num_clks]);
1450
1451         kfree(pg->clks);
1452
1453         set_bit(pg->id, pmc->powergates_available);
1454
1455         kfree(pg);
1456 }
1457
1458 static void tegra_powergate_remove_all(struct device_node *parent)
1459 {
1460         struct generic_pm_domain *genpd;
1461         struct device_node *np, *child;
1462
1463         np = of_get_child_by_name(parent, "powergates");
1464         if (!np)
1465                 return;
1466
1467         for_each_child_of_node(np, child) {
1468                 of_genpd_del_provider(child);
1469
1470                 genpd = of_genpd_remove_last(child);
1471                 if (IS_ERR(genpd))
1472                         continue;
1473
1474                 tegra_powergate_remove(genpd);
1475         }
1476
1477         of_node_put(np);
1478
1479         np = of_get_child_by_name(parent, "core-domain");
1480         if (np) {
1481                 of_genpd_del_provider(np);
1482                 of_genpd_remove_last(np);
1483         }
1484 }
1485
1486 static const struct tegra_io_pad_soc *
1487 tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
1488 {
1489         unsigned int i;
1490
1491         for (i = 0; i < pmc->soc->num_io_pads; i++)
1492                 if (pmc->soc->io_pads[i].id == id)
1493                         return &pmc->soc->io_pads[i];
1494
1495         return NULL;
1496 }
1497
1498 static int tegra_io_pad_get_dpd_register_bit(struct tegra_pmc *pmc,
1499                                              enum tegra_io_pad id,
1500                                              unsigned long *request,
1501                                              unsigned long *status,
1502                                              u32 *mask)
1503 {
1504         const struct tegra_io_pad_soc *pad;
1505
1506         pad = tegra_io_pad_find(pmc, id);
1507         if (!pad) {
1508                 dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
1509                 return -ENOENT;
1510         }
1511
1512         if (pad->dpd == UINT_MAX)
1513                 return -ENOTSUPP;
1514
1515         *mask = BIT(pad->dpd % 32);
1516
1517         if (pad->dpd < 32) {
1518                 *status = pmc->soc->regs->dpd_status;
1519                 *request = pmc->soc->regs->dpd_req;
1520         } else {
1521                 *status = pmc->soc->regs->dpd2_status;
1522                 *request = pmc->soc->regs->dpd2_req;
1523         }
1524
1525         return 0;
1526 }
1527
1528 static int tegra_io_pad_prepare(struct tegra_pmc *pmc, enum tegra_io_pad id,
1529                                 unsigned long *request, unsigned long *status,
1530                                 u32 *mask)
1531 {
1532         unsigned long rate, value;
1533         int err;
1534
1535         err = tegra_io_pad_get_dpd_register_bit(pmc, id, request, status, mask);
1536         if (err)
1537                 return err;
1538
1539         if (pmc->clk) {
1540                 rate = pmc->rate;
1541                 if (!rate) {
1542                         dev_err(pmc->dev, "failed to get clock rate\n");
1543                         return -ENODEV;
1544                 }
1545
1546                 tegra_pmc_writel(pmc, DPD_SAMPLE_ENABLE, DPD_SAMPLE);
1547
1548                 /* must be at least 200 ns, in APB (PCLK) clock cycles */
1549                 value = DIV_ROUND_UP(1000000000, rate);
1550                 value = DIV_ROUND_UP(200, value);
1551                 tegra_pmc_writel(pmc, value, SEL_DPD_TIM);
1552         }
1553
1554         return 0;
1555 }
1556
1557 static int tegra_io_pad_poll(struct tegra_pmc *pmc, unsigned long offset,
1558                              u32 mask, u32 val, unsigned long timeout)
1559 {
1560         u32 value;
1561
1562         timeout = jiffies + msecs_to_jiffies(timeout);
1563
1564         while (time_after(timeout, jiffies)) {
1565                 value = tegra_pmc_readl(pmc, offset);
1566                 if ((value & mask) == val)
1567                         return 0;
1568
1569                 usleep_range(250, 1000);
1570         }
1571
1572         return -ETIMEDOUT;
1573 }
1574
1575 static void tegra_io_pad_unprepare(struct tegra_pmc *pmc)
1576 {
1577         if (pmc->clk)
1578                 tegra_pmc_writel(pmc, DPD_SAMPLE_DISABLE, DPD_SAMPLE);
1579 }
1580
1581 /**
1582  * tegra_io_pad_power_enable() - enable power to I/O pad
1583  * @id: Tegra I/O pad ID for which to enable power
1584  *
1585  * Returns: 0 on success or a negative error code on failure.
1586  */
1587 int tegra_io_pad_power_enable(enum tegra_io_pad id)
1588 {
1589         unsigned long request, status;
1590         u32 mask;
1591         int err;
1592
1593         mutex_lock(&pmc->powergates_lock);
1594
1595         err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask);
1596         if (err < 0) {
1597                 dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
1598                 goto unlock;
1599         }
1600
1601         tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_OFF | mask, request);
1602
1603         err = tegra_io_pad_poll(pmc, status, mask, 0, 250);
1604         if (err < 0) {
1605                 dev_err(pmc->dev, "failed to enable I/O pad: %d\n", err);
1606                 goto unlock;
1607         }
1608
1609         tegra_io_pad_unprepare(pmc);
1610
1611 unlock:
1612         mutex_unlock(&pmc->powergates_lock);
1613         return err;
1614 }
1615 EXPORT_SYMBOL(tegra_io_pad_power_enable);
1616
1617 /**
1618  * tegra_io_pad_power_disable() - disable power to I/O pad
1619  * @id: Tegra I/O pad ID for which to disable power
1620  *
1621  * Returns: 0 on success or a negative error code on failure.
1622  */
1623 int tegra_io_pad_power_disable(enum tegra_io_pad id)
1624 {
1625         unsigned long request, status;
1626         u32 mask;
1627         int err;
1628
1629         mutex_lock(&pmc->powergates_lock);
1630
1631         err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask);
1632         if (err < 0) {
1633                 dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
1634                 goto unlock;
1635         }
1636
1637         tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_ON | mask, request);
1638
1639         err = tegra_io_pad_poll(pmc, status, mask, mask, 250);
1640         if (err < 0) {
1641                 dev_err(pmc->dev, "failed to disable I/O pad: %d\n", err);
1642                 goto unlock;
1643         }
1644
1645         tegra_io_pad_unprepare(pmc);
1646
1647 unlock:
1648         mutex_unlock(&pmc->powergates_lock);
1649         return err;
1650 }
1651 EXPORT_SYMBOL(tegra_io_pad_power_disable);
1652
1653 static int tegra_io_pad_is_powered(struct tegra_pmc *pmc, enum tegra_io_pad id)
1654 {
1655         unsigned long request, status;
1656         u32 mask, value;
1657         int err;
1658
1659         err = tegra_io_pad_get_dpd_register_bit(pmc, id, &request, &status,
1660                                                 &mask);
1661         if (err)
1662                 return err;
1663
1664         value = tegra_pmc_readl(pmc, status);
1665
1666         return !(value & mask);
1667 }
1668
1669 static int tegra_io_pad_set_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id,
1670                                     int voltage)
1671 {
1672         const struct tegra_io_pad_soc *pad;
1673         u32 value;
1674
1675         pad = tegra_io_pad_find(pmc, id);
1676         if (!pad)
1677                 return -ENOENT;
1678
1679         if (pad->voltage == UINT_MAX)
1680                 return -ENOTSUPP;
1681
1682         mutex_lock(&pmc->powergates_lock);
1683
1684         if (pmc->soc->has_impl_33v_pwr) {
1685                 value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
1686
1687                 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
1688                         value &= ~BIT(pad->voltage);
1689                 else
1690                         value |= BIT(pad->voltage);
1691
1692                 tegra_pmc_writel(pmc, value, PMC_IMPL_E_33V_PWR);
1693         } else {
1694                 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1695                 value = tegra_pmc_readl(pmc, PMC_PWR_DET);
1696                 value |= BIT(pad->voltage);
1697                 tegra_pmc_writel(pmc, value, PMC_PWR_DET);
1698
1699                 /* update I/O voltage */
1700                 value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
1701
1702                 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
1703                         value &= ~BIT(pad->voltage);
1704                 else
1705                         value |= BIT(pad->voltage);
1706
1707                 tegra_pmc_writel(pmc, value, PMC_PWR_DET_VALUE);
1708         }
1709
1710         mutex_unlock(&pmc->powergates_lock);
1711
1712         usleep_range(100, 250);
1713
1714         return 0;
1715 }
1716
1717 static int tegra_io_pad_get_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id)
1718 {
1719         const struct tegra_io_pad_soc *pad;
1720         u32 value;
1721
1722         pad = tegra_io_pad_find(pmc, id);
1723         if (!pad)
1724                 return -ENOENT;
1725
1726         if (pad->voltage == UINT_MAX)
1727                 return -ENOTSUPP;
1728
1729         if (pmc->soc->has_impl_33v_pwr)
1730                 value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
1731         else
1732                 value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
1733
1734         if ((value & BIT(pad->voltage)) == 0)
1735                 return TEGRA_IO_PAD_VOLTAGE_1V8;
1736
1737         return TEGRA_IO_PAD_VOLTAGE_3V3;
1738 }
1739
1740 /**
1741  * tegra_io_rail_power_on() - enable power to I/O rail
1742  * @id: Tegra I/O pad ID for which to enable power
1743  *
1744  * See also: tegra_io_pad_power_enable()
1745  */
1746 int tegra_io_rail_power_on(unsigned int id)
1747 {
1748         return tegra_io_pad_power_enable(id);
1749 }
1750 EXPORT_SYMBOL(tegra_io_rail_power_on);
1751
1752 /**
1753  * tegra_io_rail_power_off() - disable power to I/O rail
1754  * @id: Tegra I/O pad ID for which to disable power
1755  *
1756  * See also: tegra_io_pad_power_disable()
1757  */
1758 int tegra_io_rail_power_off(unsigned int id)
1759 {
1760         return tegra_io_pad_power_disable(id);
1761 }
1762 EXPORT_SYMBOL(tegra_io_rail_power_off);
1763
1764 #ifdef CONFIG_PM_SLEEP
1765 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1766 {
1767         return pmc->suspend_mode;
1768 }
1769
1770 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1771 {
1772         if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1773                 return;
1774
1775         pmc->suspend_mode = mode;
1776 }
1777
1778 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1779 {
1780         unsigned long long rate = 0;
1781         u64 ticks;
1782         u32 value;
1783
1784         switch (mode) {
1785         case TEGRA_SUSPEND_LP1:
1786                 rate = 32768;
1787                 break;
1788
1789         case TEGRA_SUSPEND_LP2:
1790                 rate = pmc->rate;
1791                 break;
1792
1793         default:
1794                 break;
1795         }
1796
1797         if (WARN_ON_ONCE(rate == 0))
1798                 rate = 100000000;
1799
1800         ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1801         do_div(ticks, USEC_PER_SEC);
1802         tegra_pmc_writel(pmc, ticks, PMC_CPUPWRGOOD_TIMER);
1803
1804         ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1805         do_div(ticks, USEC_PER_SEC);
1806         tegra_pmc_writel(pmc, ticks, PMC_CPUPWROFF_TIMER);
1807
1808         value = tegra_pmc_readl(pmc, PMC_CNTRL);
1809         value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1810         value |= PMC_CNTRL_CPU_PWRREQ_OE;
1811         tegra_pmc_writel(pmc, value, PMC_CNTRL);
1812 }
1813 #endif
1814
1815 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1816 {
1817         u32 value, values[2];
1818
1819         if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1820         } else {
1821                 switch (value) {
1822                 case 0:
1823                         pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1824                         break;
1825
1826                 case 1:
1827                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1828                         break;
1829
1830                 case 2:
1831                         pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1832                         break;
1833
1834                 default:
1835                         pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1836                         break;
1837                 }
1838         }
1839
1840         pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1841
1842         if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1843                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1844
1845         pmc->cpu_good_time = value;
1846
1847         if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1848                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1849
1850         pmc->cpu_off_time = value;
1851
1852         if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1853                                        values, ARRAY_SIZE(values)))
1854                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1855
1856         pmc->core_osc_time = values[0];
1857         pmc->core_pmu_time = values[1];
1858
1859         if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1860                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1861
1862         pmc->core_off_time = value;
1863
1864         pmc->corereq_high = of_property_read_bool(np,
1865                                 "nvidia,core-power-req-active-high");
1866
1867         pmc->sysclkreq_high = of_property_read_bool(np,
1868                                 "nvidia,sys-clock-req-active-high");
1869
1870         pmc->combined_req = of_property_read_bool(np,
1871                                 "nvidia,combined-power-req");
1872
1873         pmc->cpu_pwr_good_en = of_property_read_bool(np,
1874                                 "nvidia,cpu-pwr-good-en");
1875
1876         if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1877                                        ARRAY_SIZE(values)))
1878                 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1879                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1880
1881         pmc->lp0_vec_phys = values[0];
1882         pmc->lp0_vec_size = values[1];
1883
1884         return 0;
1885 }
1886
1887 static void tegra_pmc_init(struct tegra_pmc *pmc)
1888 {
1889         if (pmc->soc->init)
1890                 pmc->soc->init(pmc);
1891 }
1892
1893 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
1894 {
1895         static const char disabled[] = "emergency thermal reset disabled";
1896         u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1897         struct device *dev = pmc->dev;
1898         struct device_node *np;
1899         u32 value, checksum;
1900
1901         if (!pmc->soc->has_tsense_reset)
1902                 return;
1903
1904         np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
1905         if (!np) {
1906                 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
1907                 return;
1908         }
1909
1910         if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1911                 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1912                 goto out;
1913         }
1914
1915         if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1916                 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1917                 goto out;
1918         }
1919
1920         if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1921                 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1922                 goto out;
1923         }
1924
1925         if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1926                 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1927                 goto out;
1928         }
1929
1930         if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1931                 pinmux = 0;
1932
1933         value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
1934         value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1935         tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
1936
1937         value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1938                 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1939         tegra_pmc_writel(pmc, value, PMC_SCRATCH54);
1940
1941         value = PMC_SCRATCH55_RESET_TEGRA;
1942         value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1943         value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1944         value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1945
1946         /*
1947          * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1948          * contain the checksum and are currently zero, so they are not added.
1949          */
1950         checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1951                 + ((value >> 24) & 0xff);
1952         checksum &= 0xff;
1953         checksum = 0x100 - checksum;
1954
1955         value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1956
1957         tegra_pmc_writel(pmc, value, PMC_SCRATCH55);
1958
1959         value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
1960         value |= PMC_SENSOR_CTRL_ENABLE_RST;
1961         tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
1962
1963         dev_info(pmc->dev, "emergency thermal reset enabled\n");
1964
1965 out:
1966         of_node_put(np);
1967 }
1968
1969 static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
1970 {
1971         struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
1972
1973         return pmc->soc->num_io_pads;
1974 }
1975
1976 static const char *tegra_io_pad_pinctrl_get_group_name(struct pinctrl_dev *pctl,
1977                                                        unsigned int group)
1978 {
1979         struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl);
1980
1981         return pmc->soc->io_pads[group].name;
1982 }
1983
1984 static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
1985                                                unsigned int group,
1986                                                const unsigned int **pins,
1987                                                unsigned int *num_pins)
1988 {
1989         struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
1990
1991         *pins = &pmc->soc->io_pads[group].id;
1992         *num_pins = 1;
1993
1994         return 0;
1995 }
1996
1997 static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
1998         .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
1999         .get_group_name = tegra_io_pad_pinctrl_get_group_name,
2000         .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
2001         .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
2002         .dt_free_map = pinconf_generic_dt_free_map,
2003 };
2004
2005 static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
2006                                     unsigned int pin, unsigned long *config)
2007 {
2008         enum pin_config_param param = pinconf_to_config_param(*config);
2009         struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2010         const struct tegra_io_pad_soc *pad;
2011         int ret;
2012         u32 arg;
2013
2014         pad = tegra_io_pad_find(pmc, pin);
2015         if (!pad)
2016                 return -EINVAL;
2017
2018         switch (param) {
2019         case PIN_CONFIG_POWER_SOURCE:
2020                 ret = tegra_io_pad_get_voltage(pmc, pad->id);
2021                 if (ret < 0)
2022                         return ret;
2023
2024                 arg = ret;
2025                 break;
2026
2027         case PIN_CONFIG_MODE_LOW_POWER:
2028                 ret = tegra_io_pad_is_powered(pmc, pad->id);
2029                 if (ret < 0)
2030                         return ret;
2031
2032                 arg = !ret;
2033                 break;
2034
2035         default:
2036                 return -EINVAL;
2037         }
2038
2039         *config = pinconf_to_config_packed(param, arg);
2040
2041         return 0;
2042 }
2043
2044 static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
2045                                     unsigned int pin, unsigned long *configs,
2046                                     unsigned int num_configs)
2047 {
2048         struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2049         const struct tegra_io_pad_soc *pad;
2050         enum pin_config_param param;
2051         unsigned int i;
2052         int err;
2053         u32 arg;
2054
2055         pad = tegra_io_pad_find(pmc, pin);
2056         if (!pad)
2057                 return -EINVAL;
2058
2059         for (i = 0; i < num_configs; ++i) {
2060                 param = pinconf_to_config_param(configs[i]);
2061                 arg = pinconf_to_config_argument(configs[i]);
2062
2063                 switch (param) {
2064                 case PIN_CONFIG_MODE_LOW_POWER:
2065                         if (arg)
2066                                 err = tegra_io_pad_power_disable(pad->id);
2067                         else
2068                                 err = tegra_io_pad_power_enable(pad->id);
2069                         if (err)
2070                                 return err;
2071                         break;
2072                 case PIN_CONFIG_POWER_SOURCE:
2073                         if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
2074                             arg != TEGRA_IO_PAD_VOLTAGE_3V3)
2075                                 return -EINVAL;
2076                         err = tegra_io_pad_set_voltage(pmc, pad->id, arg);
2077                         if (err)
2078                                 return err;
2079                         break;
2080                 default:
2081                         return -EINVAL;
2082                 }
2083         }
2084
2085         return 0;
2086 }
2087
2088 static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
2089         .pin_config_get = tegra_io_pad_pinconf_get,
2090         .pin_config_set = tegra_io_pad_pinconf_set,
2091         .is_generic = true,
2092 };
2093
2094 static struct pinctrl_desc tegra_pmc_pctl_desc = {
2095         .pctlops = &tegra_io_pad_pinctrl_ops,
2096         .confops = &tegra_io_pad_pinconf_ops,
2097 };
2098
2099 static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
2100 {
2101         int err;
2102
2103         if (!pmc->soc->num_pin_descs)
2104                 return 0;
2105
2106         tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
2107         tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
2108         tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
2109
2110         pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
2111                                               pmc);
2112         if (IS_ERR(pmc->pctl_dev)) {
2113                 err = PTR_ERR(pmc->pctl_dev);
2114                 dev_err(pmc->dev, "failed to register pin controller: %d\n",
2115                         err);
2116                 return err;
2117         }
2118
2119         return 0;
2120 }
2121
2122 static ssize_t reset_reason_show(struct device *dev,
2123                                  struct device_attribute *attr, char *buf)
2124 {
2125         u32 value;
2126
2127         value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
2128         value &= pmc->soc->regs->rst_source_mask;
2129         value >>= pmc->soc->regs->rst_source_shift;
2130
2131         if (WARN_ON(value >= pmc->soc->num_reset_sources))
2132                 return sprintf(buf, "%s\n", "UNKNOWN");
2133
2134         return sprintf(buf, "%s\n", pmc->soc->reset_sources[value]);
2135 }
2136
2137 static DEVICE_ATTR_RO(reset_reason);
2138
2139 static ssize_t reset_level_show(struct device *dev,
2140                                 struct device_attribute *attr, char *buf)
2141 {
2142         u32 value;
2143
2144         value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
2145         value &= pmc->soc->regs->rst_level_mask;
2146         value >>= pmc->soc->regs->rst_level_shift;
2147
2148         if (WARN_ON(value >= pmc->soc->num_reset_levels))
2149                 return sprintf(buf, "%s\n", "UNKNOWN");
2150
2151         return sprintf(buf, "%s\n", pmc->soc->reset_levels[value]);
2152 }
2153
2154 static DEVICE_ATTR_RO(reset_level);
2155
2156 static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
2157 {
2158         struct device *dev = pmc->dev;
2159         int err = 0;
2160
2161         if (pmc->soc->reset_sources) {
2162                 err = device_create_file(dev, &dev_attr_reset_reason);
2163                 if (err < 0)
2164                         dev_warn(dev,
2165                                  "failed to create attr \"reset_reason\": %d\n",
2166                                  err);
2167         }
2168
2169         if (pmc->soc->reset_levels) {
2170                 err = device_create_file(dev, &dev_attr_reset_level);
2171                 if (err < 0)
2172                         dev_warn(dev,
2173                                  "failed to create attr \"reset_level\": %d\n",
2174                                  err);
2175         }
2176 }
2177
2178 static int tegra_pmc_irq_translate(struct irq_domain *domain,
2179                                    struct irq_fwspec *fwspec,
2180                                    unsigned long *hwirq,
2181                                    unsigned int *type)
2182 {
2183         if (WARN_ON(fwspec->param_count < 2))
2184                 return -EINVAL;
2185
2186         *hwirq = fwspec->param[0];
2187         *type = fwspec->param[1];
2188
2189         return 0;
2190 }
2191
2192 static int tegra_pmc_irq_alloc(struct irq_domain *domain, unsigned int virq,
2193                                unsigned int num_irqs, void *data)
2194 {
2195         struct tegra_pmc *pmc = domain->host_data;
2196         const struct tegra_pmc_soc *soc = pmc->soc;
2197         struct irq_fwspec *fwspec = data;
2198         unsigned int i;
2199         int err = 0;
2200
2201         if (WARN_ON(num_irqs > 1))
2202                 return -EINVAL;
2203
2204         for (i = 0; i < soc->num_wake_events; i++) {
2205                 const struct tegra_wake_event *event = &soc->wake_events[i];
2206
2207                 if (fwspec->param_count == 2) {
2208                         struct irq_fwspec spec;
2209
2210                         if (event->id != fwspec->param[0])
2211                                 continue;
2212
2213                         err = irq_domain_set_hwirq_and_chip(domain, virq,
2214                                                             event->id,
2215                                                             &pmc->irq, pmc);
2216                         if (err < 0)
2217                                 break;
2218
2219                         spec.fwnode = &pmc->dev->of_node->fwnode;
2220                         spec.param_count = 3;
2221                         spec.param[0] = GIC_SPI;
2222                         spec.param[1] = event->irq;
2223                         spec.param[2] = fwspec->param[1];
2224
2225                         err = irq_domain_alloc_irqs_parent(domain, virq,
2226                                                            num_irqs, &spec);
2227
2228                         break;
2229                 }
2230
2231                 if (fwspec->param_count == 3) {
2232                         if (event->gpio.instance != fwspec->param[0] ||
2233                             event->gpio.pin != fwspec->param[1])
2234                                 continue;
2235
2236                         err = irq_domain_set_hwirq_and_chip(domain, virq,
2237                                                             event->id,
2238                                                             &pmc->irq, pmc);
2239
2240                         /* GPIO hierarchies stop at the PMC level */
2241                         if (!err && domain->parent)
2242                                 err = irq_domain_disconnect_hierarchy(domain->parent,
2243                                                                       virq);
2244                         break;
2245                 }
2246         }
2247
2248         /* If there is no wake-up event, there is no PMC mapping */
2249         if (i == soc->num_wake_events)
2250                 err = irq_domain_disconnect_hierarchy(domain, virq);
2251
2252         return err;
2253 }
2254
2255 static const struct irq_domain_ops tegra_pmc_irq_domain_ops = {
2256         .translate = tegra_pmc_irq_translate,
2257         .alloc = tegra_pmc_irq_alloc,
2258 };
2259
2260 static int tegra210_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
2261 {
2262         struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2263         unsigned int offset, bit;
2264         u32 value;
2265
2266         offset = data->hwirq / 32;
2267         bit = data->hwirq % 32;
2268
2269         /* clear wake status */
2270         tegra_pmc_writel(pmc, 0, PMC_SW_WAKE_STATUS);
2271         tegra_pmc_writel(pmc, 0, PMC_SW_WAKE2_STATUS);
2272
2273         tegra_pmc_writel(pmc, 0, PMC_WAKE_STATUS);
2274         tegra_pmc_writel(pmc, 0, PMC_WAKE2_STATUS);
2275
2276         /* enable PMC wake */
2277         if (data->hwirq >= 32)
2278                 offset = PMC_WAKE2_MASK;
2279         else
2280                 offset = PMC_WAKE_MASK;
2281
2282         value = tegra_pmc_readl(pmc, offset);
2283
2284         if (on)
2285                 value |= BIT(bit);
2286         else
2287                 value &= ~BIT(bit);
2288
2289         tegra_pmc_writel(pmc, value, offset);
2290
2291         return 0;
2292 }
2293
2294 static int tegra210_pmc_irq_set_type(struct irq_data *data, unsigned int type)
2295 {
2296         struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2297         unsigned int offset, bit;
2298         u32 value;
2299
2300         offset = data->hwirq / 32;
2301         bit = data->hwirq % 32;
2302
2303         if (data->hwirq >= 32)
2304                 offset = PMC_WAKE2_LEVEL;
2305         else
2306                 offset = PMC_WAKE_LEVEL;
2307
2308         value = tegra_pmc_readl(pmc, offset);
2309
2310         switch (type) {
2311         case IRQ_TYPE_EDGE_RISING:
2312         case IRQ_TYPE_LEVEL_HIGH:
2313                 value |= BIT(bit);
2314                 break;
2315
2316         case IRQ_TYPE_EDGE_FALLING:
2317         case IRQ_TYPE_LEVEL_LOW:
2318                 value &= ~BIT(bit);
2319                 break;
2320
2321         case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
2322                 value ^= BIT(bit);
2323                 break;
2324
2325         default:
2326                 return -EINVAL;
2327         }
2328
2329         tegra_pmc_writel(pmc, value, offset);
2330
2331         return 0;
2332 }
2333
2334 static int tegra186_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
2335 {
2336         struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2337         unsigned int offset, bit;
2338         u32 value;
2339
2340         offset = data->hwirq / 32;
2341         bit = data->hwirq % 32;
2342
2343         /* clear wake status */
2344         writel(0x1, pmc->wake + WAKE_AOWAKE_STATUS_W(data->hwirq));
2345
2346         /* route wake to tier 2 */
2347         value = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
2348
2349         if (!on)
2350                 value &= ~(1 << bit);
2351         else
2352                 value |= 1 << bit;
2353
2354         writel(value, pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
2355
2356         /* enable wakeup event */
2357         writel(!!on, pmc->wake + WAKE_AOWAKE_MASK_W(data->hwirq));
2358
2359         return 0;
2360 }
2361
2362 static int tegra186_pmc_irq_set_type(struct irq_data *data, unsigned int type)
2363 {
2364         struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2365         u32 value;
2366
2367         value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
2368
2369         switch (type) {
2370         case IRQ_TYPE_EDGE_RISING:
2371         case IRQ_TYPE_LEVEL_HIGH:
2372                 value |= WAKE_AOWAKE_CNTRL_LEVEL;
2373                 break;
2374
2375         case IRQ_TYPE_EDGE_FALLING:
2376         case IRQ_TYPE_LEVEL_LOW:
2377                 value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
2378                 break;
2379
2380         case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
2381                 value ^= WAKE_AOWAKE_CNTRL_LEVEL;
2382                 break;
2383
2384         default:
2385                 return -EINVAL;
2386         }
2387
2388         writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
2389
2390         return 0;
2391 }
2392
2393 static void tegra_irq_mask_parent(struct irq_data *data)
2394 {
2395         if (data->parent_data)
2396                 irq_chip_mask_parent(data);
2397 }
2398
2399 static void tegra_irq_unmask_parent(struct irq_data *data)
2400 {
2401         if (data->parent_data)
2402                 irq_chip_unmask_parent(data);
2403 }
2404
2405 static void tegra_irq_eoi_parent(struct irq_data *data)
2406 {
2407         if (data->parent_data)
2408                 irq_chip_eoi_parent(data);
2409 }
2410
2411 static int tegra_irq_set_affinity_parent(struct irq_data *data,
2412                                          const struct cpumask *dest,
2413                                          bool force)
2414 {
2415         if (data->parent_data)
2416                 return irq_chip_set_affinity_parent(data, dest, force);
2417
2418         return -EINVAL;
2419 }
2420
2421 static int tegra_pmc_irq_init(struct tegra_pmc *pmc)
2422 {
2423         struct irq_domain *parent = NULL;
2424         struct device_node *np;
2425
2426         np = of_irq_find_parent(pmc->dev->of_node);
2427         if (np) {
2428                 parent = irq_find_host(np);
2429                 of_node_put(np);
2430         }
2431
2432         if (!parent)
2433                 return 0;
2434
2435         pmc->irq.name = dev_name(pmc->dev);
2436         pmc->irq.irq_mask = tegra_irq_mask_parent;
2437         pmc->irq.irq_unmask = tegra_irq_unmask_parent;
2438         pmc->irq.irq_eoi = tegra_irq_eoi_parent;
2439         pmc->irq.irq_set_affinity = tegra_irq_set_affinity_parent;
2440         pmc->irq.irq_set_type = pmc->soc->irq_set_type;
2441         pmc->irq.irq_set_wake = pmc->soc->irq_set_wake;
2442
2443         pmc->domain = irq_domain_add_hierarchy(parent, 0, 96, pmc->dev->of_node,
2444                                                &tegra_pmc_irq_domain_ops, pmc);
2445         if (!pmc->domain) {
2446                 dev_err(pmc->dev, "failed to allocate domain\n");
2447                 return -ENOMEM;
2448         }
2449
2450         return 0;
2451 }
2452
2453 static int tegra_pmc_clk_notify_cb(struct notifier_block *nb,
2454                                    unsigned long action, void *ptr)
2455 {
2456         struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, clk_nb);
2457         struct clk_notifier_data *data = ptr;
2458
2459         switch (action) {
2460         case PRE_RATE_CHANGE:
2461                 mutex_lock(&pmc->powergates_lock);
2462                 break;
2463
2464         case POST_RATE_CHANGE:
2465                 pmc->rate = data->new_rate;
2466                 fallthrough;
2467
2468         case ABORT_RATE_CHANGE:
2469                 mutex_unlock(&pmc->powergates_lock);
2470                 break;
2471
2472         default:
2473                 WARN_ON_ONCE(1);
2474                 return notifier_from_errno(-EINVAL);
2475         }
2476
2477         return NOTIFY_OK;
2478 }
2479
2480 static void pmc_clk_fence_udelay(u32 offset)
2481 {
2482         tegra_pmc_readl(pmc, offset);
2483         /* pmc clk propagation delay 2 us */
2484         udelay(2);
2485 }
2486
2487 static u8 pmc_clk_mux_get_parent(struct clk_hw *hw)
2488 {
2489         struct pmc_clk *clk = to_pmc_clk(hw);
2490         u32 val;
2491
2492         val = tegra_pmc_readl(pmc, clk->offs) >> clk->mux_shift;
2493         val &= PMC_CLK_OUT_MUX_MASK;
2494
2495         return val;
2496 }
2497
2498 static int pmc_clk_mux_set_parent(struct clk_hw *hw, u8 index)
2499 {
2500         struct pmc_clk *clk = to_pmc_clk(hw);
2501         u32 val;
2502
2503         val = tegra_pmc_readl(pmc, clk->offs);
2504         val &= ~(PMC_CLK_OUT_MUX_MASK << clk->mux_shift);
2505         val |= index << clk->mux_shift;
2506         tegra_pmc_writel(pmc, val, clk->offs);
2507         pmc_clk_fence_udelay(clk->offs);
2508
2509         return 0;
2510 }
2511
2512 static int pmc_clk_is_enabled(struct clk_hw *hw)
2513 {
2514         struct pmc_clk *clk = to_pmc_clk(hw);
2515         u32 val;
2516
2517         val = tegra_pmc_readl(pmc, clk->offs) & BIT(clk->force_en_shift);
2518
2519         return val ? 1 : 0;
2520 }
2521
2522 static void pmc_clk_set_state(unsigned long offs, u32 shift, int state)
2523 {
2524         u32 val;
2525
2526         val = tegra_pmc_readl(pmc, offs);
2527         val = state ? (val | BIT(shift)) : (val & ~BIT(shift));
2528         tegra_pmc_writel(pmc, val, offs);
2529         pmc_clk_fence_udelay(offs);
2530 }
2531
2532 static int pmc_clk_enable(struct clk_hw *hw)
2533 {
2534         struct pmc_clk *clk = to_pmc_clk(hw);
2535
2536         pmc_clk_set_state(clk->offs, clk->force_en_shift, 1);
2537
2538         return 0;
2539 }
2540
2541 static void pmc_clk_disable(struct clk_hw *hw)
2542 {
2543         struct pmc_clk *clk = to_pmc_clk(hw);
2544
2545         pmc_clk_set_state(clk->offs, clk->force_en_shift, 0);
2546 }
2547
2548 static const struct clk_ops pmc_clk_ops = {
2549         .get_parent = pmc_clk_mux_get_parent,
2550         .set_parent = pmc_clk_mux_set_parent,
2551         .determine_rate = __clk_mux_determine_rate,
2552         .is_enabled = pmc_clk_is_enabled,
2553         .enable = pmc_clk_enable,
2554         .disable = pmc_clk_disable,
2555 };
2556
2557 static struct clk *
2558 tegra_pmc_clk_out_register(struct tegra_pmc *pmc,
2559                            const struct pmc_clk_init_data *data,
2560                            unsigned long offset)
2561 {
2562         struct clk_init_data init;
2563         struct pmc_clk *pmc_clk;
2564
2565         pmc_clk = devm_kzalloc(pmc->dev, sizeof(*pmc_clk), GFP_KERNEL);
2566         if (!pmc_clk)
2567                 return ERR_PTR(-ENOMEM);
2568
2569         init.name = data->name;
2570         init.ops = &pmc_clk_ops;
2571         init.parent_names = data->parents;
2572         init.num_parents = data->num_parents;
2573         init.flags = CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT |
2574                      CLK_SET_PARENT_GATE;
2575
2576         pmc_clk->hw.init = &init;
2577         pmc_clk->offs = offset;
2578         pmc_clk->mux_shift = data->mux_shift;
2579         pmc_clk->force_en_shift = data->force_en_shift;
2580
2581         return clk_register(NULL, &pmc_clk->hw);
2582 }
2583
2584 static int pmc_clk_gate_is_enabled(struct clk_hw *hw)
2585 {
2586         struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2587
2588         return tegra_pmc_readl(pmc, gate->offs) & BIT(gate->shift) ? 1 : 0;
2589 }
2590
2591 static int pmc_clk_gate_enable(struct clk_hw *hw)
2592 {
2593         struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2594
2595         pmc_clk_set_state(gate->offs, gate->shift, 1);
2596
2597         return 0;
2598 }
2599
2600 static void pmc_clk_gate_disable(struct clk_hw *hw)
2601 {
2602         struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2603
2604         pmc_clk_set_state(gate->offs, gate->shift, 0);
2605 }
2606
2607 static const struct clk_ops pmc_clk_gate_ops = {
2608         .is_enabled = pmc_clk_gate_is_enabled,
2609         .enable = pmc_clk_gate_enable,
2610         .disable = pmc_clk_gate_disable,
2611 };
2612
2613 static struct clk *
2614 tegra_pmc_clk_gate_register(struct tegra_pmc *pmc, const char *name,
2615                             const char *parent_name, unsigned long offset,
2616                             u32 shift)
2617 {
2618         struct clk_init_data init;
2619         struct pmc_clk_gate *gate;
2620
2621         gate = devm_kzalloc(pmc->dev, sizeof(*gate), GFP_KERNEL);
2622         if (!gate)
2623                 return ERR_PTR(-ENOMEM);
2624
2625         init.name = name;
2626         init.ops = &pmc_clk_gate_ops;
2627         init.parent_names = &parent_name;
2628         init.num_parents = 1;
2629         init.flags = 0;
2630
2631         gate->hw.init = &init;
2632         gate->offs = offset;
2633         gate->shift = shift;
2634
2635         return clk_register(NULL, &gate->hw);
2636 }
2637
2638 static void tegra_pmc_clock_register(struct tegra_pmc *pmc,
2639                                      struct device_node *np)
2640 {
2641         struct clk *clk;
2642         struct clk_onecell_data *clk_data;
2643         unsigned int num_clks;
2644         int i, err;
2645
2646         num_clks = pmc->soc->num_pmc_clks;
2647         if (pmc->soc->has_blink_output)
2648                 num_clks += 1;
2649
2650         if (!num_clks)
2651                 return;
2652
2653         clk_data = devm_kmalloc(pmc->dev, sizeof(*clk_data), GFP_KERNEL);
2654         if (!clk_data)
2655                 return;
2656
2657         clk_data->clks = devm_kcalloc(pmc->dev, TEGRA_PMC_CLK_MAX,
2658                                       sizeof(*clk_data->clks), GFP_KERNEL);
2659         if (!clk_data->clks)
2660                 return;
2661
2662         clk_data->clk_num = TEGRA_PMC_CLK_MAX;
2663
2664         for (i = 0; i < TEGRA_PMC_CLK_MAX; i++)
2665                 clk_data->clks[i] = ERR_PTR(-ENOENT);
2666
2667         for (i = 0; i < pmc->soc->num_pmc_clks; i++) {
2668                 const struct pmc_clk_init_data *data;
2669
2670                 data = pmc->soc->pmc_clks_data + i;
2671
2672                 clk = tegra_pmc_clk_out_register(pmc, data, PMC_CLK_OUT_CNTRL);
2673                 if (IS_ERR(clk)) {
2674                         dev_warn(pmc->dev, "unable to register clock %s: %d\n",
2675                                  data->name, PTR_ERR_OR_ZERO(clk));
2676                         return;
2677                 }
2678
2679                 err = clk_register_clkdev(clk, data->name, NULL);
2680                 if (err) {
2681                         dev_warn(pmc->dev,
2682                                  "unable to register %s clock lookup: %d\n",
2683                                  data->name, err);
2684                         return;
2685                 }
2686
2687                 clk_data->clks[data->clk_id] = clk;
2688         }
2689
2690         if (pmc->soc->has_blink_output) {
2691                 tegra_pmc_writel(pmc, 0x0, PMC_BLINK_TIMER);
2692                 clk = tegra_pmc_clk_gate_register(pmc,
2693                                                   "pmc_blink_override",
2694                                                   "clk_32k",
2695                                                   PMC_DPD_PADS_ORIDE,
2696                                                   PMC_DPD_PADS_ORIDE_BLINK);
2697                 if (IS_ERR(clk)) {
2698                         dev_warn(pmc->dev,
2699                                  "unable to register pmc_blink_override: %d\n",
2700                                  PTR_ERR_OR_ZERO(clk));
2701                         return;
2702                 }
2703
2704                 clk = tegra_pmc_clk_gate_register(pmc, "pmc_blink",
2705                                                   "pmc_blink_override",
2706                                                   PMC_CNTRL,
2707                                                   PMC_CNTRL_BLINK_EN);
2708                 if (IS_ERR(clk)) {
2709                         dev_warn(pmc->dev,
2710                                  "unable to register pmc_blink: %d\n",
2711                                  PTR_ERR_OR_ZERO(clk));
2712                         return;
2713                 }
2714
2715                 err = clk_register_clkdev(clk, "pmc_blink", NULL);
2716                 if (err) {
2717                         dev_warn(pmc->dev,
2718                                  "unable to register pmc_blink lookup: %d\n",
2719                                  err);
2720                         return;
2721                 }
2722
2723                 clk_data->clks[TEGRA_PMC_CLK_BLINK] = clk;
2724         }
2725
2726         err = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
2727         if (err)
2728                 dev_warn(pmc->dev, "failed to add pmc clock provider: %d\n",
2729                          err);
2730 }
2731
2732 static const struct regmap_range pmc_usb_sleepwalk_ranges[] = {
2733         regmap_reg_range(PMC_USB_DEBOUNCE_DEL, PMC_USB_AO),
2734         regmap_reg_range(PMC_UTMIP_UHSIC_TRIGGERS, PMC_UTMIP_UHSIC_SAVED_STATE),
2735         regmap_reg_range(PMC_UTMIP_TERM_PAD_CFG, PMC_UTMIP_UHSIC_FAKE),
2736         regmap_reg_range(PMC_UTMIP_UHSIC_LINE_WAKEUP, PMC_UTMIP_UHSIC_LINE_WAKEUP),
2737         regmap_reg_range(PMC_UTMIP_BIAS_MASTER_CNTRL, PMC_UTMIP_MASTER_CONFIG),
2738         regmap_reg_range(PMC_UTMIP_UHSIC2_TRIGGERS, PMC_UTMIP_MASTER2_CONFIG),
2739         regmap_reg_range(PMC_UTMIP_PAD_CFG0, PMC_UTMIP_UHSIC_SLEEP_CFG1),
2740         regmap_reg_range(PMC_UTMIP_SLEEPWALK_P3, PMC_UTMIP_SLEEPWALK_P3),
2741 };
2742
2743 static const struct regmap_access_table pmc_usb_sleepwalk_table = {
2744         .yes_ranges = pmc_usb_sleepwalk_ranges,
2745         .n_yes_ranges = ARRAY_SIZE(pmc_usb_sleepwalk_ranges),
2746 };
2747
2748 static int tegra_pmc_regmap_readl(void *context, unsigned int offset, unsigned int *value)
2749 {
2750         struct tegra_pmc *pmc = context;
2751
2752         *value = tegra_pmc_readl(pmc, offset);
2753         return 0;
2754 }
2755
2756 static int tegra_pmc_regmap_writel(void *context, unsigned int offset, unsigned int value)
2757 {
2758         struct tegra_pmc *pmc = context;
2759
2760         tegra_pmc_writel(pmc, value, offset);
2761         return 0;
2762 }
2763
2764 static const struct regmap_config usb_sleepwalk_regmap_config = {
2765         .name = "usb_sleepwalk",
2766         .reg_bits = 32,
2767         .val_bits = 32,
2768         .reg_stride = 4,
2769         .fast_io = true,
2770         .rd_table = &pmc_usb_sleepwalk_table,
2771         .wr_table = &pmc_usb_sleepwalk_table,
2772         .reg_read = tegra_pmc_regmap_readl,
2773         .reg_write = tegra_pmc_regmap_writel,
2774 };
2775
2776 static int tegra_pmc_regmap_init(struct tegra_pmc *pmc)
2777 {
2778         struct regmap *regmap;
2779         int err;
2780
2781         if (pmc->soc->has_usb_sleepwalk) {
2782                 regmap = devm_regmap_init(pmc->dev, NULL, pmc, &usb_sleepwalk_regmap_config);
2783                 if (IS_ERR(regmap)) {
2784                         err = PTR_ERR(regmap);
2785                         dev_err(pmc->dev, "failed to allocate register map (%d)\n", err);
2786                         return err;
2787                 }
2788         }
2789
2790         return 0;
2791 }
2792
2793 static int tegra_pmc_probe(struct platform_device *pdev)
2794 {
2795         void __iomem *base;
2796         struct resource *res;
2797         int err;
2798
2799         /*
2800          * Early initialisation should have configured an initial
2801          * register mapping and setup the soc data pointer. If these
2802          * are not valid then something went badly wrong!
2803          */
2804         if (WARN_ON(!pmc->base || !pmc->soc))
2805                 return -ENODEV;
2806
2807         err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
2808         if (err < 0)
2809                 return err;
2810
2811         /* take over the memory region from the early initialization */
2812         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2813         base = devm_ioremap_resource(&pdev->dev, res);
2814         if (IS_ERR(base))
2815                 return PTR_ERR(base);
2816
2817         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
2818         if (res) {
2819                 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
2820                 if (IS_ERR(pmc->wake))
2821                         return PTR_ERR(pmc->wake);
2822         } else {
2823                 pmc->wake = base;
2824         }
2825
2826         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
2827         if (res) {
2828                 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
2829                 if (IS_ERR(pmc->aotag))
2830                         return PTR_ERR(pmc->aotag);
2831         } else {
2832                 pmc->aotag = base;
2833         }
2834
2835         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
2836         if (res) {
2837                 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
2838                 if (IS_ERR(pmc->scratch))
2839                         return PTR_ERR(pmc->scratch);
2840         } else {
2841                 pmc->scratch = base;
2842         }
2843
2844         pmc->clk = devm_clk_get(&pdev->dev, "pclk");
2845         if (IS_ERR(pmc->clk)) {
2846                 err = PTR_ERR(pmc->clk);
2847
2848                 if (err != -ENOENT) {
2849                         dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
2850                         return err;
2851                 }
2852
2853                 pmc->clk = NULL;
2854         }
2855
2856         /*
2857          * PCLK clock rate can't be retrieved using CLK API because it
2858          * causes lockup if CPU enters LP2 idle state from some other
2859          * CLK notifier, hence we're caching the rate's value locally.
2860          */
2861         if (pmc->clk) {
2862                 pmc->clk_nb.notifier_call = tegra_pmc_clk_notify_cb;
2863                 err = clk_notifier_register(pmc->clk, &pmc->clk_nb);
2864                 if (err) {
2865                         dev_err(&pdev->dev,
2866                                 "failed to register clk notifier\n");
2867                         return err;
2868                 }
2869
2870                 pmc->rate = clk_get_rate(pmc->clk);
2871         }
2872
2873         pmc->dev = &pdev->dev;
2874
2875         tegra_pmc_init(pmc);
2876
2877         tegra_pmc_init_tsense_reset(pmc);
2878
2879         tegra_pmc_reset_sysfs_init(pmc);
2880
2881         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2882                 err = tegra_powergate_debugfs_init();
2883                 if (err < 0)
2884                         goto cleanup_sysfs;
2885         }
2886
2887         err = register_restart_handler(&tegra_pmc_restart_handler);
2888         if (err) {
2889                 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
2890                         err);
2891                 goto cleanup_debugfs;
2892         }
2893
2894         err = tegra_pmc_pinctrl_init(pmc);
2895         if (err)
2896                 goto cleanup_restart_handler;
2897
2898         err = tegra_pmc_regmap_init(pmc);
2899         if (err < 0)
2900                 goto cleanup_restart_handler;
2901
2902         err = tegra_powergate_init(pmc, pdev->dev.of_node);
2903         if (err < 0)
2904                 goto cleanup_powergates;
2905
2906         err = tegra_pmc_irq_init(pmc);
2907         if (err < 0)
2908                 goto cleanup_powergates;
2909
2910         mutex_lock(&pmc->powergates_lock);
2911         iounmap(pmc->base);
2912         pmc->base = base;
2913         mutex_unlock(&pmc->powergates_lock);
2914
2915         tegra_pmc_clock_register(pmc, pdev->dev.of_node);
2916         platform_set_drvdata(pdev, pmc);
2917
2918         return 0;
2919
2920 cleanup_powergates:
2921         tegra_powergate_remove_all(pdev->dev.of_node);
2922 cleanup_restart_handler:
2923         unregister_restart_handler(&tegra_pmc_restart_handler);
2924 cleanup_debugfs:
2925         debugfs_remove(pmc->debugfs);
2926 cleanup_sysfs:
2927         device_remove_file(&pdev->dev, &dev_attr_reset_reason);
2928         device_remove_file(&pdev->dev, &dev_attr_reset_level);
2929         clk_notifier_unregister(pmc->clk, &pmc->clk_nb);
2930
2931         return err;
2932 }
2933
2934 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
2935 static int tegra_pmc_suspend(struct device *dev)
2936 {
2937         struct tegra_pmc *pmc = dev_get_drvdata(dev);
2938
2939         tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);
2940
2941         return 0;
2942 }
2943
2944 static int tegra_pmc_resume(struct device *dev)
2945 {
2946         struct tegra_pmc *pmc = dev_get_drvdata(dev);
2947
2948         tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
2949
2950         return 0;
2951 }
2952
2953 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
2954
2955 #endif
2956
2957 static const char * const tegra20_powergates[] = {
2958         [TEGRA_POWERGATE_CPU] = "cpu",
2959         [TEGRA_POWERGATE_3D] = "3d",
2960         [TEGRA_POWERGATE_VENC] = "venc",
2961         [TEGRA_POWERGATE_VDEC] = "vdec",
2962         [TEGRA_POWERGATE_PCIE] = "pcie",
2963         [TEGRA_POWERGATE_L2] = "l2",
2964         [TEGRA_POWERGATE_MPE] = "mpe",
2965 };
2966
2967 static const struct tegra_pmc_regs tegra20_pmc_regs = {
2968         .scratch0 = 0x50,
2969         .dpd_req = 0x1b8,
2970         .dpd_status = 0x1bc,
2971         .dpd2_req = 0x1c0,
2972         .dpd2_status = 0x1c4,
2973         .rst_status = 0x1b4,
2974         .rst_source_shift = 0x0,
2975         .rst_source_mask = 0x7,
2976         .rst_level_shift = 0x0,
2977         .rst_level_mask = 0x0,
2978 };
2979
2980 static void tegra20_pmc_init(struct tegra_pmc *pmc)
2981 {
2982         u32 value, osc, pmu, off;
2983
2984         /* Always enable CPU power request */
2985         value = tegra_pmc_readl(pmc, PMC_CNTRL);
2986         value |= PMC_CNTRL_CPU_PWRREQ_OE;
2987         tegra_pmc_writel(pmc, value, PMC_CNTRL);
2988
2989         value = tegra_pmc_readl(pmc, PMC_CNTRL);
2990
2991         if (pmc->sysclkreq_high)
2992                 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
2993         else
2994                 value |= PMC_CNTRL_SYSCLK_POLARITY;
2995
2996         if (pmc->corereq_high)
2997                 value &= ~PMC_CNTRL_PWRREQ_POLARITY;
2998         else
2999                 value |= PMC_CNTRL_PWRREQ_POLARITY;
3000
3001         /* configure the output polarity while the request is tristated */
3002         tegra_pmc_writel(pmc, value, PMC_CNTRL);
3003
3004         /* now enable the request */
3005         value = tegra_pmc_readl(pmc, PMC_CNTRL);
3006         value |= PMC_CNTRL_SYSCLK_OE;
3007         tegra_pmc_writel(pmc, value, PMC_CNTRL);
3008
3009         /* program core timings which are applicable only for suspend state */
3010         if (pmc->suspend_mode != TEGRA_SUSPEND_NONE) {
3011                 osc = DIV_ROUND_UP(pmc->core_osc_time * 8192, 1000000);
3012                 pmu = DIV_ROUND_UP(pmc->core_pmu_time * 32768, 1000000);
3013                 off = DIV_ROUND_UP(pmc->core_off_time * 32768, 1000000);
3014                 tegra_pmc_writel(pmc, ((osc << 8) & 0xff00) | (pmu & 0xff),
3015                                  PMC_COREPWRGOOD_TIMER);
3016                 tegra_pmc_writel(pmc, off, PMC_COREPWROFF_TIMER);
3017         }
3018 }
3019
3020 static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
3021                                            struct device_node *np,
3022                                            bool invert)
3023 {
3024         u32 value;
3025
3026         value = tegra_pmc_readl(pmc, PMC_CNTRL);
3027
3028         if (invert)
3029                 value |= PMC_CNTRL_INTR_POLARITY;
3030         else
3031                 value &= ~PMC_CNTRL_INTR_POLARITY;
3032
3033         tegra_pmc_writel(pmc, value, PMC_CNTRL);
3034 }
3035
3036 static const struct tegra_pmc_soc tegra20_pmc_soc = {
3037         .num_powergates = ARRAY_SIZE(tegra20_powergates),
3038         .powergates = tegra20_powergates,
3039         .num_cpu_powergates = 0,
3040         .cpu_powergates = NULL,
3041         .has_tsense_reset = false,
3042         .has_gpu_clamps = false,
3043         .needs_mbist_war = false,
3044         .has_impl_33v_pwr = false,
3045         .maybe_tz_only = false,
3046         .num_io_pads = 0,
3047         .io_pads = NULL,
3048         .num_pin_descs = 0,
3049         .pin_descs = NULL,
3050         .regs = &tegra20_pmc_regs,
3051         .init = tegra20_pmc_init,
3052         .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3053         .powergate_set = tegra20_powergate_set,
3054         .reset_sources = NULL,
3055         .num_reset_sources = 0,
3056         .reset_levels = NULL,
3057         .num_reset_levels = 0,
3058         .pmc_clks_data = NULL,
3059         .num_pmc_clks = 0,
3060         .has_blink_output = true,
3061         .has_usb_sleepwalk = false,
3062 };
3063
3064 static const char * const tegra30_powergates[] = {
3065         [TEGRA_POWERGATE_CPU] = "cpu0",
3066         [TEGRA_POWERGATE_3D] = "3d0",
3067         [TEGRA_POWERGATE_VENC] = "venc",
3068         [TEGRA_POWERGATE_VDEC] = "vdec",
3069         [TEGRA_POWERGATE_PCIE] = "pcie",
3070         [TEGRA_POWERGATE_L2] = "l2",
3071         [TEGRA_POWERGATE_MPE] = "mpe",
3072         [TEGRA_POWERGATE_HEG] = "heg",
3073         [TEGRA_POWERGATE_SATA] = "sata",
3074         [TEGRA_POWERGATE_CPU1] = "cpu1",
3075         [TEGRA_POWERGATE_CPU2] = "cpu2",
3076         [TEGRA_POWERGATE_CPU3] = "cpu3",
3077         [TEGRA_POWERGATE_CELP] = "celp",
3078         [TEGRA_POWERGATE_3D1] = "3d1",
3079 };
3080
3081 static const u8 tegra30_cpu_powergates[] = {
3082         TEGRA_POWERGATE_CPU,
3083         TEGRA_POWERGATE_CPU1,
3084         TEGRA_POWERGATE_CPU2,
3085         TEGRA_POWERGATE_CPU3,
3086 };
3087
3088 static const char * const tegra30_reset_sources[] = {
3089         "POWER_ON_RESET",
3090         "WATCHDOG",
3091         "SENSOR",
3092         "SW_MAIN",
3093         "LP0"
3094 };
3095
3096 static const struct tegra_pmc_soc tegra30_pmc_soc = {
3097         .num_powergates = ARRAY_SIZE(tegra30_powergates),
3098         .powergates = tegra30_powergates,
3099         .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
3100         .cpu_powergates = tegra30_cpu_powergates,
3101         .has_tsense_reset = true,
3102         .has_gpu_clamps = false,
3103         .needs_mbist_war = false,
3104         .has_impl_33v_pwr = false,
3105         .maybe_tz_only = false,
3106         .num_io_pads = 0,
3107         .io_pads = NULL,
3108         .num_pin_descs = 0,
3109         .pin_descs = NULL,
3110         .regs = &tegra20_pmc_regs,
3111         .init = tegra20_pmc_init,
3112         .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3113         .powergate_set = tegra20_powergate_set,
3114         .reset_sources = tegra30_reset_sources,
3115         .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3116         .reset_levels = NULL,
3117         .num_reset_levels = 0,
3118         .pmc_clks_data = tegra_pmc_clks_data,
3119         .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3120         .has_blink_output = true,
3121         .has_usb_sleepwalk = false,
3122 };
3123
3124 static const char * const tegra114_powergates[] = {
3125         [TEGRA_POWERGATE_CPU] = "crail",
3126         [TEGRA_POWERGATE_3D] = "3d",
3127         [TEGRA_POWERGATE_VENC] = "venc",
3128         [TEGRA_POWERGATE_VDEC] = "vdec",
3129         [TEGRA_POWERGATE_MPE] = "mpe",
3130         [TEGRA_POWERGATE_HEG] = "heg",
3131         [TEGRA_POWERGATE_CPU1] = "cpu1",
3132         [TEGRA_POWERGATE_CPU2] = "cpu2",
3133         [TEGRA_POWERGATE_CPU3] = "cpu3",
3134         [TEGRA_POWERGATE_CELP] = "celp",
3135         [TEGRA_POWERGATE_CPU0] = "cpu0",
3136         [TEGRA_POWERGATE_C0NC] = "c0nc",
3137         [TEGRA_POWERGATE_C1NC] = "c1nc",
3138         [TEGRA_POWERGATE_DIS] = "dis",
3139         [TEGRA_POWERGATE_DISB] = "disb",
3140         [TEGRA_POWERGATE_XUSBA] = "xusba",
3141         [TEGRA_POWERGATE_XUSBB] = "xusbb",
3142         [TEGRA_POWERGATE_XUSBC] = "xusbc",
3143 };
3144
3145 static const u8 tegra114_cpu_powergates[] = {
3146         TEGRA_POWERGATE_CPU0,
3147         TEGRA_POWERGATE_CPU1,
3148         TEGRA_POWERGATE_CPU2,
3149         TEGRA_POWERGATE_CPU3,
3150 };
3151
3152 static const struct tegra_pmc_soc tegra114_pmc_soc = {
3153         .num_powergates = ARRAY_SIZE(tegra114_powergates),
3154         .powergates = tegra114_powergates,
3155         .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
3156         .cpu_powergates = tegra114_cpu_powergates,
3157         .has_tsense_reset = true,
3158         .has_gpu_clamps = false,
3159         .needs_mbist_war = false,
3160         .has_impl_33v_pwr = false,
3161         .maybe_tz_only = false,
3162         .num_io_pads = 0,
3163         .io_pads = NULL,
3164         .num_pin_descs = 0,
3165         .pin_descs = NULL,
3166         .regs = &tegra20_pmc_regs,
3167         .init = tegra20_pmc_init,
3168         .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3169         .powergate_set = tegra114_powergate_set,
3170         .reset_sources = tegra30_reset_sources,
3171         .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3172         .reset_levels = NULL,
3173         .num_reset_levels = 0,
3174         .pmc_clks_data = tegra_pmc_clks_data,
3175         .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3176         .has_blink_output = true,
3177         .has_usb_sleepwalk = false,
3178 };
3179
3180 static const char * const tegra124_powergates[] = {
3181         [TEGRA_POWERGATE_CPU] = "crail",
3182         [TEGRA_POWERGATE_3D] = "3d",
3183         [TEGRA_POWERGATE_VENC] = "venc",
3184         [TEGRA_POWERGATE_PCIE] = "pcie",
3185         [TEGRA_POWERGATE_VDEC] = "vdec",
3186         [TEGRA_POWERGATE_MPE] = "mpe",
3187         [TEGRA_POWERGATE_HEG] = "heg",
3188         [TEGRA_POWERGATE_SATA] = "sata",
3189         [TEGRA_POWERGATE_CPU1] = "cpu1",
3190         [TEGRA_POWERGATE_CPU2] = "cpu2",
3191         [TEGRA_POWERGATE_CPU3] = "cpu3",
3192         [TEGRA_POWERGATE_CELP] = "celp",
3193         [TEGRA_POWERGATE_CPU0] = "cpu0",
3194         [TEGRA_POWERGATE_C0NC] = "c0nc",
3195         [TEGRA_POWERGATE_C1NC] = "c1nc",
3196         [TEGRA_POWERGATE_SOR] = "sor",
3197         [TEGRA_POWERGATE_DIS] = "dis",
3198         [TEGRA_POWERGATE_DISB] = "disb",
3199         [TEGRA_POWERGATE_XUSBA] = "xusba",
3200         [TEGRA_POWERGATE_XUSBB] = "xusbb",
3201         [TEGRA_POWERGATE_XUSBC] = "xusbc",
3202         [TEGRA_POWERGATE_VIC] = "vic",
3203         [TEGRA_POWERGATE_IRAM] = "iram",
3204 };
3205
3206 static const u8 tegra124_cpu_powergates[] = {
3207         TEGRA_POWERGATE_CPU0,
3208         TEGRA_POWERGATE_CPU1,
3209         TEGRA_POWERGATE_CPU2,
3210         TEGRA_POWERGATE_CPU3,
3211 };
3212
3213 #define TEGRA_IO_PAD(_id, _dpd, _voltage, _name)        \
3214         ((struct tegra_io_pad_soc) {                    \
3215                 .id     = (_id),                        \
3216                 .dpd    = (_dpd),                       \
3217                 .voltage = (_voltage),                  \
3218                 .name   = (_name),                      \
3219         })
3220
3221 #define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name)   \
3222         ((struct pinctrl_pin_desc) {                    \
3223                 .number = (_id),                        \
3224                 .name   = (_name)                       \
3225         })
3226
3227 #define TEGRA124_IO_PAD_TABLE(_pad)                                   \
3228         /* .id                          .dpd  .voltage  .name */      \
3229         _pad(TEGRA_IO_PAD_AUDIO,        17,   UINT_MAX, "audio"),     \
3230         _pad(TEGRA_IO_PAD_BB,           15,   UINT_MAX, "bb"),        \
3231         _pad(TEGRA_IO_PAD_CAM,          36,   UINT_MAX, "cam"),       \
3232         _pad(TEGRA_IO_PAD_COMP,         22,   UINT_MAX, "comp"),      \
3233         _pad(TEGRA_IO_PAD_CSIA,         0,    UINT_MAX, "csia"),      \
3234         _pad(TEGRA_IO_PAD_CSIB,         1,    UINT_MAX, "csb"),       \
3235         _pad(TEGRA_IO_PAD_CSIE,         44,   UINT_MAX, "cse"),       \
3236         _pad(TEGRA_IO_PAD_DSI,          2,    UINT_MAX, "dsi"),       \
3237         _pad(TEGRA_IO_PAD_DSIB,         39,   UINT_MAX, "dsib"),      \
3238         _pad(TEGRA_IO_PAD_DSIC,         40,   UINT_MAX, "dsic"),      \
3239         _pad(TEGRA_IO_PAD_DSID,         41,   UINT_MAX, "dsid"),      \
3240         _pad(TEGRA_IO_PAD_HDMI,         28,   UINT_MAX, "hdmi"),      \
3241         _pad(TEGRA_IO_PAD_HSIC,         19,   UINT_MAX, "hsic"),      \
3242         _pad(TEGRA_IO_PAD_HV,           38,   UINT_MAX, "hv"),        \
3243         _pad(TEGRA_IO_PAD_LVDS,         57,   UINT_MAX, "lvds"),      \
3244         _pad(TEGRA_IO_PAD_MIPI_BIAS,    3,    UINT_MAX, "mipi-bias"), \
3245         _pad(TEGRA_IO_PAD_NAND,         13,   UINT_MAX, "nand"),      \
3246         _pad(TEGRA_IO_PAD_PEX_BIAS,     4,    UINT_MAX, "pex-bias"),  \
3247         _pad(TEGRA_IO_PAD_PEX_CLK1,     5,    UINT_MAX, "pex-clk1"),  \
3248         _pad(TEGRA_IO_PAD_PEX_CLK2,     6,    UINT_MAX, "pex-clk2"),  \
3249         _pad(TEGRA_IO_PAD_PEX_CNTRL,    32,   UINT_MAX, "pex-cntrl"), \
3250         _pad(TEGRA_IO_PAD_SDMMC1,       33,   UINT_MAX, "sdmmc1"),    \
3251         _pad(TEGRA_IO_PAD_SDMMC3,       34,   UINT_MAX, "sdmmc3"),    \
3252         _pad(TEGRA_IO_PAD_SDMMC4,       35,   UINT_MAX, "sdmmc4"),    \
3253         _pad(TEGRA_IO_PAD_SYS_DDC,      58,   UINT_MAX, "sys_ddc"),   \
3254         _pad(TEGRA_IO_PAD_UART,         14,   UINT_MAX, "uart"),      \
3255         _pad(TEGRA_IO_PAD_USB0,         9,    UINT_MAX, "usb0"),      \
3256         _pad(TEGRA_IO_PAD_USB1,         10,   UINT_MAX, "usb1"),      \
3257         _pad(TEGRA_IO_PAD_USB2,         11,   UINT_MAX, "usb2"),      \
3258         _pad(TEGRA_IO_PAD_USB_BIAS,     12,   UINT_MAX, "usb_bias")
3259
3260 static const struct tegra_io_pad_soc tegra124_io_pads[] = {
3261         TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
3262 };
3263
3264 static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
3265         TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
3266 };
3267
3268 static const struct tegra_pmc_soc tegra124_pmc_soc = {
3269         .num_powergates = ARRAY_SIZE(tegra124_powergates),
3270         .powergates = tegra124_powergates,
3271         .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
3272         .cpu_powergates = tegra124_cpu_powergates,
3273         .has_tsense_reset = true,
3274         .has_gpu_clamps = true,
3275         .needs_mbist_war = false,
3276         .has_impl_33v_pwr = false,
3277         .maybe_tz_only = false,
3278         .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
3279         .io_pads = tegra124_io_pads,
3280         .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
3281         .pin_descs = tegra124_pin_descs,
3282         .regs = &tegra20_pmc_regs,
3283         .init = tegra20_pmc_init,
3284         .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3285         .powergate_set = tegra114_powergate_set,
3286         .reset_sources = tegra30_reset_sources,
3287         .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3288         .reset_levels = NULL,
3289         .num_reset_levels = 0,
3290         .pmc_clks_data = tegra_pmc_clks_data,
3291         .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3292         .has_blink_output = true,
3293         .has_usb_sleepwalk = true,
3294 };
3295
3296 static const char * const tegra210_powergates[] = {
3297         [TEGRA_POWERGATE_CPU] = "crail",
3298         [TEGRA_POWERGATE_3D] = "3d",
3299         [TEGRA_POWERGATE_VENC] = "venc",
3300         [TEGRA_POWERGATE_PCIE] = "pcie",
3301         [TEGRA_POWERGATE_MPE] = "mpe",
3302         [TEGRA_POWERGATE_SATA] = "sata",
3303         [TEGRA_POWERGATE_CPU1] = "cpu1",
3304         [TEGRA_POWERGATE_CPU2] = "cpu2",
3305         [TEGRA_POWERGATE_CPU3] = "cpu3",
3306         [TEGRA_POWERGATE_CPU0] = "cpu0",
3307         [TEGRA_POWERGATE_C0NC] = "c0nc",
3308         [TEGRA_POWERGATE_SOR] = "sor",
3309         [TEGRA_POWERGATE_DIS] = "dis",
3310         [TEGRA_POWERGATE_DISB] = "disb",
3311         [TEGRA_POWERGATE_XUSBA] = "xusba",
3312         [TEGRA_POWERGATE_XUSBB] = "xusbb",
3313         [TEGRA_POWERGATE_XUSBC] = "xusbc",
3314         [TEGRA_POWERGATE_VIC] = "vic",
3315         [TEGRA_POWERGATE_IRAM] = "iram",
3316         [TEGRA_POWERGATE_NVDEC] = "nvdec",
3317         [TEGRA_POWERGATE_NVJPG] = "nvjpg",
3318         [TEGRA_POWERGATE_AUD] = "aud",
3319         [TEGRA_POWERGATE_DFD] = "dfd",
3320         [TEGRA_POWERGATE_VE2] = "ve2",
3321 };
3322
3323 static const u8 tegra210_cpu_powergates[] = {
3324         TEGRA_POWERGATE_CPU0,
3325         TEGRA_POWERGATE_CPU1,
3326         TEGRA_POWERGATE_CPU2,
3327         TEGRA_POWERGATE_CPU3,
3328 };
3329
3330 #define TEGRA210_IO_PAD_TABLE(_pad)                                        \
3331         /*   .id                        .dpd     .voltage  .name */        \
3332         _pad(TEGRA_IO_PAD_AUDIO,       17,       5,        "audio"),       \
3333         _pad(TEGRA_IO_PAD_AUDIO_HV,    61,       18,       "audio-hv"),    \
3334         _pad(TEGRA_IO_PAD_CAM,         36,       10,       "cam"),         \
3335         _pad(TEGRA_IO_PAD_CSIA,        0,        UINT_MAX, "csia"),        \
3336         _pad(TEGRA_IO_PAD_CSIB,        1,        UINT_MAX, "csib"),        \
3337         _pad(TEGRA_IO_PAD_CSIC,        42,       UINT_MAX, "csic"),        \
3338         _pad(TEGRA_IO_PAD_CSID,        43,       UINT_MAX, "csid"),        \
3339         _pad(TEGRA_IO_PAD_CSIE,        44,       UINT_MAX, "csie"),        \
3340         _pad(TEGRA_IO_PAD_CSIF,        45,       UINT_MAX, "csif"),        \
3341         _pad(TEGRA_IO_PAD_DBG,         25,       19,       "dbg"),         \
3342         _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26,       UINT_MAX, "debug-nonao"), \
3343         _pad(TEGRA_IO_PAD_DMIC,        50,       20,       "dmic"),        \
3344         _pad(TEGRA_IO_PAD_DP,          51,       UINT_MAX, "dp"),          \
3345         _pad(TEGRA_IO_PAD_DSI,         2,        UINT_MAX, "dsi"),         \
3346         _pad(TEGRA_IO_PAD_DSIB,        39,       UINT_MAX, "dsib"),        \
3347         _pad(TEGRA_IO_PAD_DSIC,        40,       UINT_MAX, "dsic"),        \
3348         _pad(TEGRA_IO_PAD_DSID,        41,       UINT_MAX, "dsid"),        \
3349         _pad(TEGRA_IO_PAD_EMMC,        35,       UINT_MAX, "emmc"),        \
3350         _pad(TEGRA_IO_PAD_EMMC2,       37,       UINT_MAX, "emmc2"),       \
3351         _pad(TEGRA_IO_PAD_GPIO,        27,       21,       "gpio"),        \
3352         _pad(TEGRA_IO_PAD_HDMI,        28,       UINT_MAX, "hdmi"),        \
3353         _pad(TEGRA_IO_PAD_HSIC,        19,       UINT_MAX, "hsic"),        \
3354         _pad(TEGRA_IO_PAD_LVDS,        57,       UINT_MAX, "lvds"),        \
3355         _pad(TEGRA_IO_PAD_MIPI_BIAS,   3,        UINT_MAX, "mipi-bias"),   \
3356         _pad(TEGRA_IO_PAD_PEX_BIAS,    4,        UINT_MAX, "pex-bias"),    \
3357         _pad(TEGRA_IO_PAD_PEX_CLK1,    5,        UINT_MAX, "pex-clk1"),    \
3358         _pad(TEGRA_IO_PAD_PEX_CLK2,    6,        UINT_MAX, "pex-clk2"),    \
3359         _pad(TEGRA_IO_PAD_PEX_CNTRL,   UINT_MAX, 11,       "pex-cntrl"),   \
3360         _pad(TEGRA_IO_PAD_SDMMC1,      33,       12,       "sdmmc1"),      \
3361         _pad(TEGRA_IO_PAD_SDMMC3,      34,       13,       "sdmmc3"),      \
3362         _pad(TEGRA_IO_PAD_SPI,         46,       22,       "spi"),         \
3363         _pad(TEGRA_IO_PAD_SPI_HV,      47,       23,       "spi-hv"),      \
3364         _pad(TEGRA_IO_PAD_UART,        14,       2,        "uart"),        \
3365         _pad(TEGRA_IO_PAD_USB0,        9,        UINT_MAX, "usb0"),        \
3366         _pad(TEGRA_IO_PAD_USB1,        10,       UINT_MAX, "usb1"),        \
3367         _pad(TEGRA_IO_PAD_USB2,        11,       UINT_MAX, "usb2"),        \
3368         _pad(TEGRA_IO_PAD_USB3,        18,       UINT_MAX, "usb3"),        \
3369         _pad(TEGRA_IO_PAD_USB_BIAS,    12,       UINT_MAX, "usb-bias")
3370
3371 static const struct tegra_io_pad_soc tegra210_io_pads[] = {
3372         TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
3373 };
3374
3375 static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
3376         TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
3377 };
3378
3379 static const char * const tegra210_reset_sources[] = {
3380         "POWER_ON_RESET",
3381         "WATCHDOG",
3382         "SENSOR",
3383         "SW_MAIN",
3384         "LP0",
3385         "AOTAG"
3386 };
3387
3388 static const struct tegra_wake_event tegra210_wake_events[] = {
3389         TEGRA_WAKE_IRQ("rtc", 16, 2),
3390         TEGRA_WAKE_IRQ("pmu", 51, 86),
3391 };
3392
3393 static const struct tegra_pmc_soc tegra210_pmc_soc = {
3394         .num_powergates = ARRAY_SIZE(tegra210_powergates),
3395         .powergates = tegra210_powergates,
3396         .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
3397         .cpu_powergates = tegra210_cpu_powergates,
3398         .has_tsense_reset = true,
3399         .has_gpu_clamps = true,
3400         .needs_mbist_war = true,
3401         .has_impl_33v_pwr = false,
3402         .maybe_tz_only = true,
3403         .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
3404         .io_pads = tegra210_io_pads,
3405         .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
3406         .pin_descs = tegra210_pin_descs,
3407         .regs = &tegra20_pmc_regs,
3408         .init = tegra20_pmc_init,
3409         .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3410         .powergate_set = tegra114_powergate_set,
3411         .irq_set_wake = tegra210_pmc_irq_set_wake,
3412         .irq_set_type = tegra210_pmc_irq_set_type,
3413         .reset_sources = tegra210_reset_sources,
3414         .num_reset_sources = ARRAY_SIZE(tegra210_reset_sources),
3415         .reset_levels = NULL,
3416         .num_reset_levels = 0,
3417         .num_wake_events = ARRAY_SIZE(tegra210_wake_events),
3418         .wake_events = tegra210_wake_events,
3419         .pmc_clks_data = tegra_pmc_clks_data,
3420         .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3421         .has_blink_output = true,
3422         .has_usb_sleepwalk = true,
3423 };
3424
3425 #define TEGRA186_IO_PAD_TABLE(_pad)                                          \
3426         /*   .id                        .dpd      .voltage  .name */         \
3427         _pad(TEGRA_IO_PAD_CSIA,         0,        UINT_MAX, "csia"),         \
3428         _pad(TEGRA_IO_PAD_CSIB,         1,        UINT_MAX, "csib"),         \
3429         _pad(TEGRA_IO_PAD_DSI,          2,        UINT_MAX, "dsi"),          \
3430         _pad(TEGRA_IO_PAD_MIPI_BIAS,    3,        UINT_MAX, "mipi-bias"),    \
3431         _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4,        UINT_MAX, "pex-clk-bias"), \
3432         _pad(TEGRA_IO_PAD_PEX_CLK3,     5,        UINT_MAX, "pex-clk3"),     \
3433         _pad(TEGRA_IO_PAD_PEX_CLK2,     6,        UINT_MAX, "pex-clk2"),     \
3434         _pad(TEGRA_IO_PAD_PEX_CLK1,     7,        UINT_MAX, "pex-clk1"),     \
3435         _pad(TEGRA_IO_PAD_USB0,         9,        UINT_MAX, "usb0"),         \
3436         _pad(TEGRA_IO_PAD_USB1,         10,       UINT_MAX, "usb1"),         \
3437         _pad(TEGRA_IO_PAD_USB2,         11,       UINT_MAX, "usb2"),         \
3438         _pad(TEGRA_IO_PAD_USB_BIAS,     12,       UINT_MAX, "usb-bias"),     \
3439         _pad(TEGRA_IO_PAD_UART,         14,       UINT_MAX, "uart"),         \
3440         _pad(TEGRA_IO_PAD_AUDIO,        17,       UINT_MAX, "audio"),        \
3441         _pad(TEGRA_IO_PAD_HSIC,         19,       UINT_MAX, "hsic"),         \
3442         _pad(TEGRA_IO_PAD_DBG,          25,       UINT_MAX, "dbg"),          \
3443         _pad(TEGRA_IO_PAD_HDMI_DP0,     28,       UINT_MAX, "hdmi-dp0"),     \
3444         _pad(TEGRA_IO_PAD_HDMI_DP1,     29,       UINT_MAX, "hdmi-dp1"),     \
3445         _pad(TEGRA_IO_PAD_PEX_CNTRL,    32,       UINT_MAX, "pex-cntrl"),    \
3446         _pad(TEGRA_IO_PAD_SDMMC2_HV,    34,       5,        "sdmmc2-hv"),    \
3447         _pad(TEGRA_IO_PAD_SDMMC4,       36,       UINT_MAX, "sdmmc4"),       \
3448         _pad(TEGRA_IO_PAD_CAM,          38,       UINT_MAX, "cam"),          \
3449         _pad(TEGRA_IO_PAD_DSIB,         40,       UINT_MAX, "dsib"),         \
3450         _pad(TEGRA_IO_PAD_DSIC,         41,       UINT_MAX, "dsic"),         \
3451         _pad(TEGRA_IO_PAD_DSID,         42,       UINT_MAX, "dsid"),         \
3452         _pad(TEGRA_IO_PAD_CSIC,         43,       UINT_MAX, "csic"),         \
3453         _pad(TEGRA_IO_PAD_CSID,         44,       UINT_MAX, "csid"),         \
3454         _pad(TEGRA_IO_PAD_CSIE,         45,       UINT_MAX, "csie"),         \
3455         _pad(TEGRA_IO_PAD_CSIF,         46,       UINT_MAX, "csif"),         \
3456         _pad(TEGRA_IO_PAD_SPI,          47,       UINT_MAX, "spi"),          \
3457         _pad(TEGRA_IO_PAD_UFS,          49,       UINT_MAX, "ufs"),          \
3458         _pad(TEGRA_IO_PAD_DMIC_HV,      52,       2,        "dmic-hv"),      \
3459         _pad(TEGRA_IO_PAD_EDP,          53,       UINT_MAX, "edp"),          \
3460         _pad(TEGRA_IO_PAD_SDMMC1_HV,    55,       4,        "sdmmc1-hv"),    \
3461         _pad(TEGRA_IO_PAD_SDMMC3_HV,    56,       6,        "sdmmc3-hv"),    \
3462         _pad(TEGRA_IO_PAD_CONN,         60,       UINT_MAX, "conn"),         \
3463         _pad(TEGRA_IO_PAD_AUDIO_HV,     61,       1,        "audio-hv"),     \
3464         _pad(TEGRA_IO_PAD_AO_HV,        UINT_MAX, 0,        "ao-hv")
3465
3466 static const struct tegra_io_pad_soc tegra186_io_pads[] = {
3467         TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
3468 };
3469
3470 static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
3471         TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
3472 };
3473
3474 static const struct tegra_pmc_regs tegra186_pmc_regs = {
3475         .scratch0 = 0x2000,
3476         .dpd_req = 0x74,
3477         .dpd_status = 0x78,
3478         .dpd2_req = 0x7c,
3479         .dpd2_status = 0x80,
3480         .rst_status = 0x70,
3481         .rst_source_shift = 0x2,
3482         .rst_source_mask = 0x3c,
3483         .rst_level_shift = 0x0,
3484         .rst_level_mask = 0x3,
3485 };
3486
3487 static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
3488                                             struct device_node *np,
3489                                             bool invert)
3490 {
3491         struct resource regs;
3492         void __iomem *wake;
3493         u32 value;
3494         int index;
3495
3496         index = of_property_match_string(np, "reg-names", "wake");
3497         if (index < 0) {
3498                 dev_err(pmc->dev, "failed to find PMC wake registers\n");
3499                 return;
3500         }
3501
3502         of_address_to_resource(np, index, &regs);
3503
3504         wake = ioremap(regs.start, resource_size(&regs));
3505         if (!wake) {
3506                 dev_err(pmc->dev, "failed to map PMC wake registers\n");
3507                 return;
3508         }
3509
3510         value = readl(wake + WAKE_AOWAKE_CTRL);
3511
3512         if (invert)
3513                 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
3514         else
3515                 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
3516
3517         writel(value, wake + WAKE_AOWAKE_CTRL);
3518
3519         iounmap(wake);
3520 }
3521
3522 static const char * const tegra186_reset_sources[] = {
3523         "SYS_RESET",
3524         "AOWDT",
3525         "MCCPLEXWDT",
3526         "BPMPWDT",
3527         "SCEWDT",
3528         "SPEWDT",
3529         "APEWDT",
3530         "BCCPLEXWDT",
3531         "SENSOR",
3532         "AOTAG",
3533         "VFSENSOR",
3534         "SWREST",
3535         "SC7",
3536         "HSM",
3537         "CORESIGHT"
3538 };
3539
3540 static const char * const tegra186_reset_levels[] = {
3541         "L0", "L1", "L2", "WARM"
3542 };
3543
3544 static const struct tegra_wake_event tegra186_wake_events[] = {
3545         TEGRA_WAKE_IRQ("pmu", 24, 209),
3546         TEGRA_WAKE_GPIO("power", 29, 1, TEGRA186_AON_GPIO(FF, 0)),
3547         TEGRA_WAKE_IRQ("rtc", 73, 10),
3548 };
3549
3550 static const struct tegra_pmc_soc tegra186_pmc_soc = {
3551         .num_powergates = 0,
3552         .powergates = NULL,
3553         .num_cpu_powergates = 0,
3554         .cpu_powergates = NULL,
3555         .has_tsense_reset = false,
3556         .has_gpu_clamps = false,
3557         .needs_mbist_war = false,
3558         .has_impl_33v_pwr = true,
3559         .maybe_tz_only = false,
3560         .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
3561         .io_pads = tegra186_io_pads,
3562         .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
3563         .pin_descs = tegra186_pin_descs,
3564         .regs = &tegra186_pmc_regs,
3565         .init = NULL,
3566         .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
3567         .irq_set_wake = tegra186_pmc_irq_set_wake,
3568         .irq_set_type = tegra186_pmc_irq_set_type,
3569         .reset_sources = tegra186_reset_sources,
3570         .num_reset_sources = ARRAY_SIZE(tegra186_reset_sources),
3571         .reset_levels = tegra186_reset_levels,
3572         .num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
3573         .num_wake_events = ARRAY_SIZE(tegra186_wake_events),
3574         .wake_events = tegra186_wake_events,
3575         .pmc_clks_data = NULL,
3576         .num_pmc_clks = 0,
3577         .has_blink_output = false,
3578         .has_usb_sleepwalk = false,
3579 };
3580
3581 #define TEGRA194_IO_PAD_TABLE(_pad)                                              \
3582         /*   .id                          .dpd      .voltage  .name */           \
3583         _pad(TEGRA_IO_PAD_CSIA,           0,        UINT_MAX, "csia"),           \
3584         _pad(TEGRA_IO_PAD_CSIB,           1,        UINT_MAX, "csib"),           \
3585         _pad(TEGRA_IO_PAD_MIPI_BIAS,      3,        UINT_MAX, "mipi-bias"),      \
3586         _pad(TEGRA_IO_PAD_PEX_CLK_BIAS,   4,        UINT_MAX, "pex-clk-bias"),   \
3587         _pad(TEGRA_IO_PAD_PEX_CLK3,       5,        UINT_MAX, "pex-clk3"),       \
3588         _pad(TEGRA_IO_PAD_PEX_CLK2,       6,        UINT_MAX, "pex-clk2"),       \
3589         _pad(TEGRA_IO_PAD_PEX_CLK1,       7,        UINT_MAX, "pex-clk1"),       \
3590         _pad(TEGRA_IO_PAD_EQOS,           8,        UINT_MAX, "eqos"),           \
3591         _pad(TEGRA_IO_PAD_PEX_CLK_2_BIAS, 9,        UINT_MAX, "pex-clk-2-bias"), \
3592         _pad(TEGRA_IO_PAD_PEX_CLK_2,      10,       UINT_MAX, "pex-clk-2"),      \
3593         _pad(TEGRA_IO_PAD_DAP3,           11,       UINT_MAX, "dap3"),           \
3594         _pad(TEGRA_IO_PAD_DAP5,           12,       UINT_MAX, "dap5"),           \
3595         _pad(TEGRA_IO_PAD_UART,           14,       UINT_MAX, "uart"),           \
3596         _pad(TEGRA_IO_PAD_PWR_CTL,        15,       UINT_MAX, "pwr-ctl"),        \
3597         _pad(TEGRA_IO_PAD_SOC_GPIO53,     16,       UINT_MAX, "soc-gpio53"),     \
3598         _pad(TEGRA_IO_PAD_AUDIO,          17,       UINT_MAX, "audio"),          \
3599         _pad(TEGRA_IO_PAD_GP_PWM2,        18,       UINT_MAX, "gp-pwm2"),        \
3600         _pad(TEGRA_IO_PAD_GP_PWM3,        19,       UINT_MAX, "gp-pwm3"),        \
3601         _pad(TEGRA_IO_PAD_SOC_GPIO12,     20,       UINT_MAX, "soc-gpio12"),     \
3602         _pad(TEGRA_IO_PAD_SOC_GPIO13,     21,       UINT_MAX, "soc-gpio13"),     \
3603         _pad(TEGRA_IO_PAD_SOC_GPIO10,     22,       UINT_MAX, "soc-gpio10"),     \
3604         _pad(TEGRA_IO_PAD_UART4,          23,       UINT_MAX, "uart4"),          \
3605         _pad(TEGRA_IO_PAD_UART5,          24,       UINT_MAX, "uart5"),          \
3606         _pad(TEGRA_IO_PAD_DBG,            25,       UINT_MAX, "dbg"),            \
3607         _pad(TEGRA_IO_PAD_HDMI_DP3,       26,       UINT_MAX, "hdmi-dp3"),       \
3608         _pad(TEGRA_IO_PAD_HDMI_DP2,       27,       UINT_MAX, "hdmi-dp2"),       \
3609         _pad(TEGRA_IO_PAD_HDMI_DP0,       28,       UINT_MAX, "hdmi-dp0"),       \
3610         _pad(TEGRA_IO_PAD_HDMI_DP1,       29,       UINT_MAX, "hdmi-dp1"),       \
3611         _pad(TEGRA_IO_PAD_PEX_CNTRL,      32,       UINT_MAX, "pex-cntrl"),      \
3612         _pad(TEGRA_IO_PAD_PEX_CTL2,       33,       UINT_MAX, "pex-ctl2"),       \
3613         _pad(TEGRA_IO_PAD_PEX_L0_RST_N,   34,       UINT_MAX, "pex-l0-rst"),     \
3614         _pad(TEGRA_IO_PAD_PEX_L1_RST_N,   35,       UINT_MAX, "pex-l1-rst"),     \
3615         _pad(TEGRA_IO_PAD_SDMMC4,         36,       UINT_MAX, "sdmmc4"),         \
3616         _pad(TEGRA_IO_PAD_PEX_L5_RST_N,   37,       UINT_MAX, "pex-l5-rst"),     \
3617         _pad(TEGRA_IO_PAD_CAM,            38,       UINT_MAX, "cam"),            \
3618         _pad(TEGRA_IO_PAD_CSIC,           43,       UINT_MAX, "csic"),           \
3619         _pad(TEGRA_IO_PAD_CSID,           44,       UINT_MAX, "csid"),           \
3620         _pad(TEGRA_IO_PAD_CSIE,           45,       UINT_MAX, "csie"),           \
3621         _pad(TEGRA_IO_PAD_CSIF,           46,       UINT_MAX, "csif"),           \
3622         _pad(TEGRA_IO_PAD_SPI,            47,       UINT_MAX, "spi"),            \
3623         _pad(TEGRA_IO_PAD_UFS,            49,       UINT_MAX, "ufs"),            \
3624         _pad(TEGRA_IO_PAD_CSIG,           50,       UINT_MAX, "csig"),           \
3625         _pad(TEGRA_IO_PAD_CSIH,           51,       UINT_MAX, "csih"),           \
3626         _pad(TEGRA_IO_PAD_EDP,            53,       UINT_MAX, "edp"),            \
3627         _pad(TEGRA_IO_PAD_SDMMC1_HV,      55,       4,        "sdmmc1-hv"),      \
3628         _pad(TEGRA_IO_PAD_SDMMC3_HV,      56,       6,        "sdmmc3-hv"),      \
3629         _pad(TEGRA_IO_PAD_CONN,           60,       UINT_MAX, "conn"),           \
3630         _pad(TEGRA_IO_PAD_AUDIO_HV,       61,       1,        "audio-hv"),       \
3631         _pad(TEGRA_IO_PAD_AO_HV,          UINT_MAX, 0,        "ao-hv")
3632
3633 static const struct tegra_io_pad_soc tegra194_io_pads[] = {
3634         TEGRA194_IO_PAD_TABLE(TEGRA_IO_PAD)
3635 };
3636
3637 static const struct pinctrl_pin_desc tegra194_pin_descs[] = {
3638         TEGRA194_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
3639 };
3640
3641 static const struct tegra_pmc_regs tegra194_pmc_regs = {
3642         .scratch0 = 0x2000,
3643         .dpd_req = 0x74,
3644         .dpd_status = 0x78,
3645         .dpd2_req = 0x7c,
3646         .dpd2_status = 0x80,
3647         .rst_status = 0x70,
3648         .rst_source_shift = 0x2,
3649         .rst_source_mask = 0x7c,
3650         .rst_level_shift = 0x0,
3651         .rst_level_mask = 0x3,
3652 };
3653
3654 static const char * const tegra194_reset_sources[] = {
3655         "SYS_RESET_N",
3656         "AOWDT",
3657         "BCCPLEXWDT",
3658         "BPMPWDT",
3659         "SCEWDT",
3660         "SPEWDT",
3661         "APEWDT",
3662         "LCCPLEXWDT",
3663         "SENSOR",
3664         "AOTAG",
3665         "VFSENSOR",
3666         "MAINSWRST",
3667         "SC7",
3668         "HSM",
3669         "CSITE",
3670         "RCEWDT",
3671         "PVA0WDT",
3672         "PVA1WDT",
3673         "L1A_ASYNC",
3674         "BPMPBOOT",
3675         "FUSECRC",
3676 };
3677
3678 static const struct tegra_wake_event tegra194_wake_events[] = {
3679         TEGRA_WAKE_IRQ("pmu", 24, 209),
3680         TEGRA_WAKE_GPIO("power", 29, 1, TEGRA194_AON_GPIO(EE, 4)),
3681         TEGRA_WAKE_IRQ("rtc", 73, 10),
3682 };
3683
3684 static const struct tegra_pmc_soc tegra194_pmc_soc = {
3685         .num_powergates = 0,
3686         .powergates = NULL,
3687         .num_cpu_powergates = 0,
3688         .cpu_powergates = NULL,
3689         .has_tsense_reset = false,
3690         .has_gpu_clamps = false,
3691         .needs_mbist_war = false,
3692         .has_impl_33v_pwr = true,
3693         .maybe_tz_only = false,
3694         .num_io_pads = ARRAY_SIZE(tegra194_io_pads),
3695         .io_pads = tegra194_io_pads,
3696         .num_pin_descs = ARRAY_SIZE(tegra194_pin_descs),
3697         .pin_descs = tegra194_pin_descs,
3698         .regs = &tegra194_pmc_regs,
3699         .init = NULL,
3700         .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
3701         .irq_set_wake = tegra186_pmc_irq_set_wake,
3702         .irq_set_type = tegra186_pmc_irq_set_type,
3703         .reset_sources = tegra194_reset_sources,
3704         .num_reset_sources = ARRAY_SIZE(tegra194_reset_sources),
3705         .reset_levels = tegra186_reset_levels,
3706         .num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
3707         .num_wake_events = ARRAY_SIZE(tegra194_wake_events),
3708         .wake_events = tegra194_wake_events,
3709         .pmc_clks_data = NULL,
3710         .num_pmc_clks = 0,
3711         .has_blink_output = false,
3712         .has_usb_sleepwalk = false,
3713 };
3714
3715 static const struct tegra_pmc_regs tegra234_pmc_regs = {
3716         .scratch0 = 0x2000,
3717         .dpd_req = 0,
3718         .dpd_status = 0,
3719         .dpd2_req = 0,
3720         .dpd2_status = 0,
3721         .rst_status = 0x70,
3722         .rst_source_shift = 0x2,
3723         .rst_source_mask = 0xfc,
3724         .rst_level_shift = 0x0,
3725         .rst_level_mask = 0x3,
3726 };
3727
3728 static const char * const tegra234_reset_sources[] = {
3729         "SYS_RESET_N",
3730         "AOWDT",
3731         "BCCPLEXWDT",
3732         "BPMPWDT",
3733         "SCEWDT",
3734         "SPEWDT",
3735         "APEWDT",
3736         "LCCPLEXWDT",
3737         "SENSOR",
3738         "AOTAG",
3739         "VFSENSOR",
3740         "MAINSWRST",
3741         "SC7",
3742         "HSM",
3743         "CSITE",
3744         "RCEWDT",
3745         "PVA0WDT",
3746         "PVA1WDT",
3747         "L1A_ASYNC",
3748         "BPMPBOOT",
3749         "FUSECRC",
3750 };
3751
3752 static const struct tegra_pmc_soc tegra234_pmc_soc = {
3753         .num_powergates = 0,
3754         .powergates = NULL,
3755         .num_cpu_powergates = 0,
3756         .cpu_powergates = NULL,
3757         .has_tsense_reset = false,
3758         .has_gpu_clamps = false,
3759         .needs_mbist_war = false,
3760         .has_impl_33v_pwr = true,
3761         .maybe_tz_only = false,
3762         .num_io_pads = 0,
3763         .io_pads = NULL,
3764         .num_pin_descs = 0,
3765         .pin_descs = NULL,
3766         .regs = &tegra234_pmc_regs,
3767         .init = NULL,
3768         .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
3769         .irq_set_wake = tegra186_pmc_irq_set_wake,
3770         .irq_set_type = tegra186_pmc_irq_set_type,
3771         .reset_sources = tegra234_reset_sources,
3772         .num_reset_sources = ARRAY_SIZE(tegra234_reset_sources),
3773         .reset_levels = tegra186_reset_levels,
3774         .num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
3775         .num_wake_events = 0,
3776         .wake_events = NULL,
3777         .pmc_clks_data = NULL,
3778         .num_pmc_clks = 0,
3779         .has_blink_output = false,
3780 };
3781
3782 static const struct of_device_id tegra_pmc_match[] = {
3783         { .compatible = "nvidia,tegra234-pmc", .data = &tegra234_pmc_soc },
3784         { .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc },
3785         { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
3786         { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
3787         { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
3788         { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
3789         { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
3790         { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
3791         { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
3792         { }
3793 };
3794
3795 static void tegra_pmc_sync_state(struct device *dev)
3796 {
3797         int err;
3798
3799         /*
3800          * Older device-trees don't have core PD, and thus, there are
3801          * no dependencies that will block the state syncing. We shouldn't
3802          * mark the domain as synced in this case.
3803          */
3804         if (!pmc->core_domain_registered)
3805                 return;
3806
3807         pmc->core_domain_state_synced = true;
3808
3809         /* this is a no-op if core regulator isn't used */
3810         mutex_lock(&pmc->powergates_lock);
3811         err = dev_pm_opp_sync_regulators(dev);
3812         mutex_unlock(&pmc->powergates_lock);
3813
3814         if (err)
3815                 dev_err(dev, "failed to sync regulators: %d\n", err);
3816 }
3817
3818 static struct platform_driver tegra_pmc_driver = {
3819         .driver = {
3820                 .name = "tegra-pmc",
3821                 .suppress_bind_attrs = true,
3822                 .of_match_table = tegra_pmc_match,
3823 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
3824                 .pm = &tegra_pmc_pm_ops,
3825 #endif
3826                 .sync_state = tegra_pmc_sync_state,
3827         },
3828         .probe = tegra_pmc_probe,
3829 };
3830 builtin_platform_driver(tegra_pmc_driver);
3831
3832 static bool __init tegra_pmc_detect_tz_only(struct tegra_pmc *pmc)
3833 {
3834         u32 value, saved;
3835
3836         saved = readl(pmc->base + pmc->soc->regs->scratch0);
3837         value = saved ^ 0xffffffff;
3838
3839         if (value == 0xffffffff)
3840                 value = 0xdeadbeef;
3841
3842         /* write pattern and read it back */
3843         writel(value, pmc->base + pmc->soc->regs->scratch0);
3844         value = readl(pmc->base + pmc->soc->regs->scratch0);
3845
3846         /* if we read all-zeroes, access is restricted to TZ only */
3847         if (value == 0) {
3848                 pr_info("access to PMC is restricted to TZ\n");
3849                 return true;
3850         }
3851
3852         /* restore original value */
3853         writel(saved, pmc->base + pmc->soc->regs->scratch0);
3854
3855         return false;
3856 }
3857
3858 /*
3859  * Early initialization to allow access to registers in the very early boot
3860  * process.
3861  */
3862 static int __init tegra_pmc_early_init(void)
3863 {
3864         const struct of_device_id *match;
3865         struct device_node *np;
3866         struct resource regs;
3867         unsigned int i;
3868         bool invert;
3869
3870         mutex_init(&pmc->powergates_lock);
3871
3872         np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
3873         if (!np) {
3874                 /*
3875                  * Fall back to legacy initialization for 32-bit ARM only. All
3876                  * 64-bit ARM device tree files for Tegra are required to have
3877                  * a PMC node.
3878                  *
3879                  * This is for backwards-compatibility with old device trees
3880                  * that didn't contain a PMC node. Note that in this case the
3881                  * SoC data can't be matched and therefore powergating is
3882                  * disabled.
3883                  */
3884                 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
3885                         pr_warn("DT node not found, powergating disabled\n");
3886
3887                         regs.start = 0x7000e400;
3888                         regs.end = 0x7000e7ff;
3889                         regs.flags = IORESOURCE_MEM;
3890
3891                         pr_warn("Using memory region %pR\n", &regs);
3892                 } else {
3893                         /*
3894                          * At this point we're not running on Tegra, so play
3895                          * nice with multi-platform kernels.
3896                          */
3897                         return 0;
3898                 }
3899         } else {
3900                 /*
3901                  * Extract information from the device tree if we've found a
3902                  * matching node.
3903                  */
3904                 if (of_address_to_resource(np, 0, &regs) < 0) {
3905                         pr_err("failed to get PMC registers\n");
3906                         of_node_put(np);
3907                         return -ENXIO;
3908                 }
3909         }
3910
3911         pmc->base = ioremap(regs.start, resource_size(&regs));
3912         if (!pmc->base) {
3913                 pr_err("failed to map PMC registers\n");
3914                 of_node_put(np);
3915                 return -ENXIO;
3916         }
3917
3918         if (np) {
3919                 pmc->soc = match->data;
3920
3921                 if (pmc->soc->maybe_tz_only)
3922                         pmc->tz_only = tegra_pmc_detect_tz_only(pmc);
3923
3924                 /* Create a bitmap of the available and valid partitions */
3925                 for (i = 0; i < pmc->soc->num_powergates; i++)
3926                         if (pmc->soc->powergates[i])
3927                                 set_bit(i, pmc->powergates_available);
3928
3929                 /*
3930                  * Invert the interrupt polarity if a PMC device tree node
3931                  * exists and contains the nvidia,invert-interrupt property.
3932                  */
3933                 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
3934
3935                 pmc->soc->setup_irq_polarity(pmc, np, invert);
3936
3937                 of_node_put(np);
3938         }
3939
3940         return 0;
3941 }
3942 early_initcall(tegra_pmc_early_init);