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