xref: /linux/drivers/soc/tegra/pmc.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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-2024, 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/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/iopoll.h>
29 #include <linux/irqdomain.h>
30 #include <linux/irq.h>
31 #include <linux/irq_work.h>
32 #include <linux/kernel.h>
33 #include <linux/of_address.h>
34 #include <linux/of_clk.h>
35 #include <linux/of.h>
36 #include <linux/of_irq.h>
37 #include <linux/of_platform.h>
38 #include <linux/pinctrl/pinconf-generic.h>
39 #include <linux/pinctrl/pinconf.h>
40 #include <linux/pinctrl/pinctrl.h>
41 #include <linux/platform_device.h>
42 #include <linux/pm_domain.h>
43 #include <linux/pm_opp.h>
44 #include <linux/power_supply.h>
45 #include <linux/reboot.h>
46 #include <linux/regmap.h>
47 #include <linux/reset.h>
48 #include <linux/seq_file.h>
49 #include <linux/slab.h>
50 #include <linux/spinlock.h>
51 #include <linux/string_choices.h>
52 #include <linux/syscore_ops.h>
53 
54 #include <soc/tegra/common.h>
55 #include <soc/tegra/fuse.h>
56 #include <soc/tegra/pmc.h>
57 
58 #include <dt-bindings/interrupt-controller/arm-gic.h>
59 #include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
60 #include <dt-bindings/gpio/tegra186-gpio.h>
61 #include <dt-bindings/gpio/tegra194-gpio.h>
62 #include <dt-bindings/gpio/tegra234-gpio.h>
63 #include <dt-bindings/gpio/nvidia,tegra264-gpio.h>
64 #include <dt-bindings/soc/tegra-pmc.h>
65 
66 #define PMC_CNTRL			0x0
67 #define  PMC_CNTRL_INTR_POLARITY	BIT(17) /* inverts INTR polarity */
68 #define  PMC_CNTRL_CPU_PWRREQ_OE	BIT(16) /* CPU pwr req enable */
69 #define  PMC_CNTRL_CPU_PWRREQ_POLARITY	BIT(15) /* CPU pwr req polarity */
70 #define  PMC_CNTRL_SIDE_EFFECT_LP0	BIT(14) /* LP0 when CPU pwr gated */
71 #define  PMC_CNTRL_SYSCLK_OE		BIT(11) /* system clock enable */
72 #define  PMC_CNTRL_SYSCLK_POLARITY	BIT(10) /* sys clk polarity */
73 #define  PMC_CNTRL_PWRREQ_POLARITY	BIT(8)
74 #define  PMC_CNTRL_BLINK_EN		7
75 #define  PMC_CNTRL_MAIN_RST		BIT(4)
76 
77 #define PMC_WAKE_MASK			0x0c
78 #define PMC_WAKE_LEVEL			0x10
79 #define PMC_WAKE_STATUS			0x14
80 #define PMC_SW_WAKE_STATUS		0x18
81 #define PMC_DPD_PADS_ORIDE		0x1c
82 #define  PMC_DPD_PADS_ORIDE_BLINK	20
83 
84 #define DPD_SAMPLE			0x020
85 #define  DPD_SAMPLE_ENABLE		BIT(0)
86 #define  DPD_SAMPLE_DISABLE		(0 << 0)
87 
88 #define PWRGATE_TOGGLE			0x30
89 #define  PWRGATE_TOGGLE_START		BIT(8)
90 
91 #define REMOVE_CLAMPING			0x34
92 
93 #define PWRGATE_STATUS			0x38
94 
95 #define PMC_BLINK_TIMER			0x40
96 #define PMC_IMPL_E_33V_PWR		0x40
97 
98 #define PMC_PWR_DET			0x48
99 
100 #define PMC_SCRATCH0_MODE_RECOVERY	BIT(31)
101 #define PMC_SCRATCH0_MODE_BOOTLOADER	BIT(30)
102 #define PMC_SCRATCH0_MODE_RCM		BIT(1)
103 #define PMC_SCRATCH0_MODE_MASK		(PMC_SCRATCH0_MODE_RECOVERY | \
104 					 PMC_SCRATCH0_MODE_BOOTLOADER | \
105 					 PMC_SCRATCH0_MODE_RCM)
106 
107 #define PMC_CPUPWRGOOD_TIMER		0xc8
108 #define PMC_CPUPWROFF_TIMER		0xcc
109 #define PMC_COREPWRGOOD_TIMER		0x3c
110 #define PMC_COREPWROFF_TIMER		0xe0
111 
112 #define PMC_PWR_DET_VALUE		0xe4
113 
114 #define PMC_USB_DEBOUNCE_DEL		0xec
115 #define PMC_USB_AO			0xf0
116 
117 #define PMC_SCRATCH37			0x130
118 #define PMC_SCRATCH41			0x140
119 
120 #define PMC_WAKE2_MASK			0x160
121 #define PMC_WAKE2_LEVEL			0x164
122 #define PMC_WAKE2_STATUS		0x168
123 #define PMC_SW_WAKE2_STATUS		0x16c
124 
125 #define PMC_CLK_OUT_CNTRL		0x1a8
126 #define  PMC_CLK_OUT_MUX_MASK		GENMASK(1, 0)
127 #define PMC_SENSOR_CTRL			0x1b0
128 #define  PMC_SENSOR_CTRL_SCRATCH_WRITE	BIT(2)
129 #define  PMC_SENSOR_CTRL_ENABLE_RST	BIT(1)
130 
131 #define  PMC_RST_STATUS_POR		0
132 #define  PMC_RST_STATUS_WATCHDOG	1
133 #define  PMC_RST_STATUS_SENSOR		2
134 #define  PMC_RST_STATUS_SW_MAIN		3
135 #define  PMC_RST_STATUS_LP0		4
136 #define  PMC_RST_STATUS_AOTAG		5
137 
138 #define IO_DPD_REQ			0x1b8
139 #define  IO_DPD_REQ_CODE_IDLE		(0U << 30)
140 #define  IO_DPD_REQ_CODE_OFF		(1U << 30)
141 #define  IO_DPD_REQ_CODE_ON		(2U << 30)
142 #define  IO_DPD_REQ_CODE_MASK		(3U << 30)
143 
144 #define IO_DPD_STATUS			0x1bc
145 #define IO_DPD2_REQ			0x1c0
146 #define IO_DPD2_STATUS			0x1c4
147 #define SEL_DPD_TIM			0x1c8
148 
149 #define PMC_UTMIP_UHSIC_TRIGGERS	0x1ec
150 #define PMC_UTMIP_UHSIC_SAVED_STATE	0x1f0
151 
152 #define PMC_UTMIP_TERM_PAD_CFG		0x1f8
153 #define PMC_UTMIP_UHSIC_SLEEP_CFG	0x1fc
154 #define PMC_UTMIP_UHSIC_FAKE		0x218
155 
156 #define PMC_SCRATCH54			0x258
157 #define  PMC_SCRATCH54_DATA_SHIFT	8
158 #define  PMC_SCRATCH54_ADDR_SHIFT	0
159 
160 #define PMC_SCRATCH55			0x25c
161 #define  PMC_SCRATCH55_RESET_TEGRA	BIT(31)
162 #define  PMC_SCRATCH55_CNTRL_ID_SHIFT	27
163 #define  PMC_SCRATCH55_PINMUX_SHIFT	24
164 #define  PMC_SCRATCH55_16BITOP		BIT(15)
165 #define  PMC_SCRATCH55_CHECKSUM_SHIFT	16
166 #define  PMC_SCRATCH55_I2CSLV1_SHIFT	0
167 
168 #define  PMC_UTMIP_UHSIC_LINE_WAKEUP	0x26c
169 
170 #define PMC_UTMIP_BIAS_MASTER_CNTRL	0x270
171 #define PMC_UTMIP_MASTER_CONFIG		0x274
172 #define PMC_UTMIP_UHSIC2_TRIGGERS	0x27c
173 #define PMC_UTMIP_MASTER2_CONFIG	0x29c
174 
175 #define GPU_RG_CNTRL			0x2d4
176 
177 #define PMC_UTMIP_PAD_CFG0		0x4c0
178 #define PMC_UTMIP_UHSIC_SLEEP_CFG1	0x4d0
179 #define PMC_UTMIP_SLEEPWALK_P3		0x4e0
180 /* Tegra186 and later */
181 #define WAKE_AOWAKE_CNTRL(x) (0x000 + ((x) << 2))
182 #define WAKE_AOWAKE_CNTRL_LEVEL (1 << 3)
183 #define WAKE_AOWAKE_CNTRL_SR_CAPTURE_EN (1 << 1)
184 #define WAKE_AOWAKE_MASK_W(_pmc, x) \
185 	((_pmc)->soc->regs->aowake_mask_w + ((x) << 2))
186 #define WAKE_AOWAKE_STATUS_W(_pmc, x) \
187 	((_pmc)->soc->regs->aowake_status_w + ((x) << 2))
188 #define WAKE_AOWAKE_STATUS_R(_pmc, x) \
189 	((_pmc)->soc->regs->aowake_status_r + ((x) << 2))
190 #define WAKE_AOWAKE_TIER2_ROUTING(_pmc, x) \
191 	((_pmc)->soc->regs->aowake_tier2_routing + ((x) << 2))
192 #define WAKE_AOWAKE_SW_STATUS(_pmc, x) \
193 	((_pmc)->soc->regs->aowake_sw_status + ((x) << 2))
194 
195 #define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
196 
197 #define SW_WAKE_ID		83 /* wake83 */
198 
199 /* for secure PMC */
200 #define TEGRA_SMC_PMC		0xc2fffe00
201 #define  TEGRA_SMC_PMC_READ	0xaa
202 #define  TEGRA_SMC_PMC_WRITE	0xbb
203 
204 /* Tegra264 and later */
205 #define PMC_IMPL_SDMMC1_HV_PADCTL_0	0x41004
206 
207 struct pmc_clk {
208 	struct clk_hw hw;
209 	struct tegra_pmc *pmc;
210 	unsigned long offs;
211 	u32 mux_shift;
212 	u32 force_en_shift;
213 };
214 
215 #define to_pmc_clk(_hw) container_of(_hw, struct pmc_clk, hw)
216 
217 struct pmc_clk_gate {
218 	struct clk_hw hw;
219 	struct tegra_pmc *pmc;
220 	unsigned long offs;
221 	u32 shift;
222 };
223 
224 #define to_pmc_clk_gate(_hw) container_of(_hw, struct pmc_clk_gate, hw)
225 
226 struct pmc_clk_init_data {
227 	char *name;
228 	const char *const *parents;
229 	int num_parents;
230 	int clk_id;
231 	u8 mux_shift;
232 	u8 force_en_shift;
233 };
234 
235 static const char * const clk_out1_parents[] = { "osc", "osc_div2",
236 	"osc_div4", "extern1",
237 };
238 
239 static const char * const clk_out2_parents[] = { "osc", "osc_div2",
240 	"osc_div4", "extern2",
241 };
242 
243 static const char * const clk_out3_parents[] = { "osc", "osc_div2",
244 	"osc_div4", "extern3",
245 };
246 
247 static const struct pmc_clk_init_data tegra_pmc_clks_data[] = {
248 	{
249 		.name = "pmc_clk_out_1",
250 		.parents = clk_out1_parents,
251 		.num_parents = ARRAY_SIZE(clk_out1_parents),
252 		.clk_id = TEGRA_PMC_CLK_OUT_1,
253 		.mux_shift = 6,
254 		.force_en_shift = 2,
255 	},
256 	{
257 		.name = "pmc_clk_out_2",
258 		.parents = clk_out2_parents,
259 		.num_parents = ARRAY_SIZE(clk_out2_parents),
260 		.clk_id = TEGRA_PMC_CLK_OUT_2,
261 		.mux_shift = 14,
262 		.force_en_shift = 10,
263 	},
264 	{
265 		.name = "pmc_clk_out_3",
266 		.parents = clk_out3_parents,
267 		.num_parents = ARRAY_SIZE(clk_out3_parents),
268 		.clk_id = TEGRA_PMC_CLK_OUT_3,
269 		.mux_shift = 22,
270 		.force_en_shift = 18,
271 	},
272 };
273 
274 struct tegra_pmc_core_pd {
275 	struct generic_pm_domain genpd;
276 	struct tegra_pmc *pmc;
277 };
278 
279 static inline struct tegra_pmc_core_pd *
280 to_core_pd(struct generic_pm_domain *genpd)
281 {
282 	return container_of(genpd, struct tegra_pmc_core_pd, genpd);
283 }
284 
285 struct tegra_powergate {
286 	struct generic_pm_domain genpd;
287 	struct tegra_pmc *pmc;
288 	unsigned int id;
289 	struct clk **clks;
290 	unsigned int num_clks;
291 	unsigned long *clk_rates;
292 	struct reset_control *reset;
293 };
294 
295 struct tegra_io_pad_soc {
296 	enum tegra_io_pad id;
297 	unsigned int dpd;
298 	unsigned int request;
299 	unsigned int status;
300 	const char *name;
301 };
302 
303 struct tegra_io_pad_vctrl {
304 	enum tegra_io_pad id;
305 	unsigned int offset;
306 	unsigned int ena_3v3;
307 	unsigned int ena_1v8;
308 };
309 
310 struct tegra_pmc_regs {
311 	unsigned int scratch0;
312 	unsigned int rst_status;
313 	unsigned int rst_source_shift;
314 	unsigned int rst_source_mask;
315 	unsigned int rst_level_shift;
316 	unsigned int rst_level_mask;
317 	unsigned int aowake_mask_w;
318 	unsigned int aowake_status_w;
319 	unsigned int aowake_status_r;
320 	unsigned int aowake_tier2_routing;
321 	unsigned int aowake_sw_status_w;
322 	unsigned int aowake_sw_status;
323 	unsigned int aowake_latch_sw;
324 	unsigned int aowake_ctrl;
325 };
326 
327 struct tegra_wake_event {
328 	const char *name;
329 	unsigned int id;
330 	unsigned int irq;
331 	struct {
332 		unsigned int instance;
333 		unsigned int pin;
334 	} gpio;
335 };
336 
337 #define TEGRA_WAKE_SIMPLE(_name, _id)			\
338 	{						\
339 		.name = _name,				\
340 		.id = _id,				\
341 		.irq = 0,				\
342 		.gpio = {				\
343 			.instance = UINT_MAX,		\
344 			.pin = UINT_MAX,		\
345 		},					\
346 	}
347 
348 #define TEGRA_WAKE_IRQ(_name, _id, _irq)		\
349 	{						\
350 		.name = _name,				\
351 		.id = _id,				\
352 		.irq = _irq,				\
353 		.gpio = {				\
354 			.instance = UINT_MAX,		\
355 			.pin = UINT_MAX,		\
356 		},					\
357 	}
358 
359 #define TEGRA_WAKE_GPIO(_name, _id, _instance, _pin)	\
360 	{						\
361 		.name = _name,				\
362 		.id = _id,				\
363 		.irq = 0,				\
364 		.gpio = {				\
365 			.instance = _instance,		\
366 			.pin = _pin,			\
367 		},					\
368 	}
369 
370 struct tegra_pmc_soc {
371 	unsigned int num_powergates;
372 	const char *const *powergates;
373 	unsigned int num_cpu_powergates;
374 	const u8 *cpu_powergates;
375 
376 	bool has_tsense_reset;
377 	bool has_gpu_clamps;
378 	bool needs_mbist_war;
379 	bool has_io_pad_wren;
380 	bool maybe_tz_only;
381 
382 	const struct tegra_io_pad_soc *io_pads;
383 	unsigned int num_io_pads;
384 	const struct tegra_io_pad_vctrl *io_pad_vctrls;
385 	unsigned int num_io_pad_vctrls;
386 
387 	const struct pinctrl_pin_desc *pin_descs;
388 	unsigned int num_pin_descs;
389 
390 	const struct tegra_pmc_regs *regs;
391 	void (*init)(struct tegra_pmc *pmc);
392 	void (*setup_irq_polarity)(struct tegra_pmc *pmc,
393 				   struct device_node *np);
394 	void (*set_wake_filters)(struct tegra_pmc *pmc);
395 	int (*irq_set_wake)(struct irq_data *data, unsigned int on);
396 	int (*irq_set_type)(struct irq_data *data, unsigned int type);
397 	int (*powergate_set)(struct tegra_pmc *pmc, unsigned int id,
398 			     bool new_state);
399 
400 	const char * const *reset_sources;
401 	unsigned int num_reset_sources;
402 	const char * const *reset_levels;
403 	unsigned int num_reset_levels;
404 
405 	/*
406 	 * These describe events that can wake the system from sleep (i.e.
407 	 * LP0 or SC7). Wakeup from other sleep states (such as LP1 or LP2)
408 	 * are dealt with in the LIC.
409 	 */
410 	const struct tegra_wake_event *wake_events;
411 	unsigned int num_wake_events;
412 	unsigned int max_wake_events;
413 	unsigned int max_wake_vectors;
414 
415 	const struct pmc_clk_init_data *pmc_clks_data;
416 	unsigned int num_pmc_clks;
417 	bool has_blink_output;
418 	bool has_usb_sleepwalk;
419 	bool supports_core_domain;
420 	bool has_single_mmio_aperture;
421 };
422 
423 /**
424  * struct tegra_pmc - NVIDIA Tegra PMC
425  * @dev: pointer to PMC device structure
426  * @base: pointer to I/O remapped register region
427  * @wake: pointer to I/O remapped region for WAKE registers
428  * @aotag: pointer to I/O remapped region for AOTAG registers
429  * @scratch: pointer to I/O remapped region for scratch registers
430  * @clk: pointer to pclk clock
431  * @soc: pointer to SoC data structure
432  * @tz_only: flag specifying if the PMC can only be accessed via TrustZone
433  * @rate: currently configured rate of pclk
434  * @suspend_mode: lowest suspend mode available
435  * @cpu_good_time: CPU power good time (in microseconds)
436  * @cpu_off_time: CPU power off time (in microsecends)
437  * @core_osc_time: core power good OSC time (in microseconds)
438  * @core_pmu_time: core power good PMU time (in microseconds)
439  * @core_off_time: core power off time (in microseconds)
440  * @corereq_high: core power request is active-high
441  * @sysclkreq_high: system clock request is active-high
442  * @combined_req: combined power request for CPU & core
443  * @cpu_pwr_good_en: CPU power good signal is enabled
444  * @lp0_vec_phys: physical base address of the LP0 warm boot code
445  * @lp0_vec_size: size of the LP0 warm boot code
446  * @invert_irq: polarity of the PMU IRQ is inverted
447  * @powergates_available: Bitmap of available power gates
448  * @powergates_lock: mutex for power gate register access
449  * @pctl_dev: pin controller exposed by the PMC
450  * @domain: IRQ domain provided by the PMC
451  * @irq: chip implementation for the IRQ domain
452  * @clk_nb: pclk clock changes handler
453  * @core_domain_state_synced: flag marking the core domain's state as synced
454  * @wake_type_level_map: Bitmap indicating level type for non-dual edge wakes
455  * @wake_type_dual_edge_map: Bitmap indicating if a wake is dual-edge or not
456  * @wake_sw_status_map: Bitmap to hold raw status of wakes without mask
457  * @wake_cntrl_level_map: Bitmap to hold wake levels to be programmed in
458  *     cntrl register associated with each wake during system suspend.
459  * @reboot_notifier: PMC reboot notifier handler
460  * @syscore: syscore suspend/resume callbacks
461  * @wake_work: IRQ work handler for processing wake-up events.
462  * @wake_status: Status of wake-up events.
463  */
464 struct tegra_pmc {
465 	struct device *dev;
466 	void __iomem *base;
467 	void __iomem *wake;
468 	void __iomem *aotag;
469 	void __iomem *scratch;
470 	struct clk *clk;
471 
472 	const struct tegra_pmc_soc *soc;
473 	bool tz_only;
474 
475 	unsigned long rate;
476 
477 	enum tegra_suspend_mode suspend_mode;
478 	u32 cpu_good_time;
479 	u32 cpu_off_time;
480 	u32 core_osc_time;
481 	u32 core_pmu_time;
482 	u32 core_off_time;
483 	bool corereq_high;
484 	bool sysclkreq_high;
485 	bool combined_req;
486 	bool cpu_pwr_good_en;
487 	u32 lp0_vec_phys;
488 	u32 lp0_vec_size;
489 	bool invert_irq;
490 
491 	DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
492 
493 	struct mutex powergates_lock;
494 
495 	struct pinctrl_dev *pctl_dev;
496 
497 	struct irq_domain *domain;
498 	struct irq_chip irq;
499 
500 	struct notifier_block clk_nb;
501 
502 	bool core_domain_state_synced;
503 
504 	unsigned long *wake_type_level_map;
505 	unsigned long *wake_type_dual_edge_map;
506 	unsigned long *wake_sw_status_map;
507 	unsigned long *wake_cntrl_level_map;
508 
509 	struct notifier_block reboot_notifier;
510 	struct syscore syscore;
511 
512 	/* Pending wake IRQ processing */
513 	struct irq_work wake_work;
514 	u32 *wake_status;
515 };
516 
517 #if defined(CONFIG_ARM)
518 static struct tegra_pmc *early_pmc = &(struct tegra_pmc) {
519 	.base = NULL,
520 	.suspend_mode = TEGRA_SUSPEND_NOT_READY,
521 };
522 #endif
523 
524 static inline struct tegra_powergate *
525 to_powergate(struct generic_pm_domain *domain)
526 {
527 	return container_of(domain, struct tegra_powergate, genpd);
528 }
529 
530 static u32 tegra_pmc_readl(struct tegra_pmc *pmc, unsigned long offset)
531 {
532 	struct arm_smccc_res res;
533 
534 	if (pmc->tz_only) {
535 		arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_READ, offset, 0, 0,
536 			      0, 0, 0, &res);
537 		if (res.a0) {
538 			if (pmc->dev)
539 				dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
540 					 __func__, res.a0);
541 			else
542 				pr_warn("%s(): SMC failed: %lu\n", __func__,
543 					res.a0);
544 		}
545 
546 		return res.a1;
547 	}
548 
549 	return readl(pmc->base + offset);
550 }
551 
552 static void tegra_pmc_writel(struct tegra_pmc *pmc, u32 value,
553 			     unsigned long offset)
554 {
555 	struct arm_smccc_res res;
556 
557 	if (pmc->tz_only) {
558 		arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_WRITE, offset,
559 			      value, 0, 0, 0, 0, &res);
560 		if (res.a0) {
561 			if (pmc->dev)
562 				dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
563 					 __func__, res.a0);
564 			else
565 				pr_warn("%s(): SMC failed: %lu\n", __func__,
566 					res.a0);
567 		}
568 	} else {
569 		writel(value, pmc->base + offset);
570 	}
571 }
572 
573 static u32 tegra_pmc_scratch_readl(struct tegra_pmc *pmc, unsigned long offset)
574 {
575 	if (pmc->tz_only)
576 		return tegra_pmc_readl(pmc, offset);
577 
578 	return readl(pmc->scratch + offset);
579 }
580 
581 static void tegra_pmc_scratch_writel(struct tegra_pmc *pmc, u32 value,
582 				     unsigned long offset)
583 {
584 	if (pmc->tz_only)
585 		tegra_pmc_writel(pmc, value, offset);
586 	else
587 		writel(value, pmc->scratch + offset);
588 }
589 
590 static inline bool tegra_powergate_state(struct tegra_pmc *pmc, int id)
591 {
592 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
593 		return (tegra_pmc_readl(pmc, GPU_RG_CNTRL) & 0x1) == 0;
594 	else
595 		return (tegra_pmc_readl(pmc, PWRGATE_STATUS) & BIT(id)) != 0;
596 }
597 
598 static inline bool tegra_powergate_is_valid(struct tegra_pmc *pmc, int id)
599 {
600 	return (pmc->soc && pmc->soc->powergates[id]);
601 }
602 
603 static inline bool tegra_powergate_is_available(struct tegra_pmc *pmc, int id)
604 {
605 	return test_bit(id, pmc->powergates_available);
606 }
607 
608 static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
609 {
610 	unsigned int i;
611 
612 	if (!pmc || !pmc->soc || !name)
613 		return -EINVAL;
614 
615 	for (i = 0; i < pmc->soc->num_powergates; i++) {
616 		if (!tegra_powergate_is_valid(pmc, i))
617 			continue;
618 
619 		if (!strcmp(name, pmc->soc->powergates[i]))
620 			return i;
621 	}
622 
623 	return -ENODEV;
624 }
625 
626 static int tegra20_powergate_set(struct tegra_pmc *pmc, unsigned int id,
627 				 bool new_state)
628 {
629 	unsigned int retries = 100;
630 	bool status;
631 	int ret;
632 
633 	/*
634 	 * As per TRM documentation, the toggle command will be dropped by PMC
635 	 * if there is contention with a HW-initiated toggling (i.e. CPU core
636 	 * power-gated), the command should be retried in that case.
637 	 */
638 	do {
639 		tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
640 
641 		/* wait for PMC to execute the command */
642 		ret = read_poll_timeout(tegra_powergate_state, status,
643 					status == new_state, 1, 10, false,
644 					pmc, id);
645 	} while (ret == -ETIMEDOUT && retries--);
646 
647 	return ret;
648 }
649 
650 static inline bool tegra_powergate_toggle_ready(struct tegra_pmc *pmc)
651 {
652 	return !(tegra_pmc_readl(pmc, PWRGATE_TOGGLE) & PWRGATE_TOGGLE_START);
653 }
654 
655 static int tegra114_powergate_set(struct tegra_pmc *pmc, unsigned int id,
656 				  bool new_state)
657 {
658 	bool status;
659 	int err;
660 
661 	/* wait while PMC power gating is contended */
662 	err = readx_poll_timeout(tegra_powergate_toggle_ready, pmc, status,
663 				 status == true, 1, 100);
664 	if (err)
665 		return err;
666 
667 	tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
668 
669 	/* wait for PMC to accept the command */
670 	err = readx_poll_timeout(tegra_powergate_toggle_ready, pmc, status,
671 				 status == true, 1, 100);
672 	if (err)
673 		return err;
674 
675 	/* wait for PMC to execute the command */
676 	err = read_poll_timeout(tegra_powergate_state, status,
677 				status == new_state, 10, 100000, false,
678 				pmc, id);
679 	if (err)
680 		return err;
681 
682 	return 0;
683 }
684 
685 /**
686  * tegra_powergate_set() - set the state of a partition
687  * @pmc: power management controller
688  * @id: partition ID
689  * @new_state: new state of the partition
690  */
691 static int tegra_powergate_set(struct tegra_pmc *pmc, unsigned int id,
692 			       bool new_state)
693 {
694 	int err;
695 
696 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
697 		return -EINVAL;
698 
699 	mutex_lock(&pmc->powergates_lock);
700 
701 	if (tegra_powergate_state(pmc, id) == new_state) {
702 		mutex_unlock(&pmc->powergates_lock);
703 		return 0;
704 	}
705 
706 	err = pmc->soc->powergate_set(pmc, id, new_state);
707 
708 	mutex_unlock(&pmc->powergates_lock);
709 
710 	return err;
711 }
712 
713 static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc,
714 					     unsigned int id)
715 {
716 	u32 mask;
717 
718 	mutex_lock(&pmc->powergates_lock);
719 
720 	/*
721 	 * On Tegra124 and later, the clamps for the GPU are controlled by a
722 	 * separate register (with different semantics).
723 	 */
724 	if (id == TEGRA_POWERGATE_3D) {
725 		if (pmc->soc->has_gpu_clamps) {
726 			tegra_pmc_writel(pmc, 0, GPU_RG_CNTRL);
727 			goto out;
728 		}
729 	}
730 
731 	/*
732 	 * Tegra 2 has a bug where PCIE and VDE clamping masks are
733 	 * swapped relatively to the partition ids
734 	 */
735 	if (id == TEGRA_POWERGATE_VDEC)
736 		mask = (1 << TEGRA_POWERGATE_PCIE);
737 	else if (id == TEGRA_POWERGATE_PCIE)
738 		mask = (1 << TEGRA_POWERGATE_VDEC);
739 	else
740 		mask = (1 << id);
741 
742 	tegra_pmc_writel(pmc, mask, REMOVE_CLAMPING);
743 
744 out:
745 	mutex_unlock(&pmc->powergates_lock);
746 
747 	return 0;
748 }
749 
750 static int tegra_powergate_prepare_clocks(struct tegra_powergate *pg)
751 {
752 	unsigned long safe_rate = 100 * 1000 * 1000;
753 	unsigned int i;
754 	int err;
755 
756 	for (i = 0; i < pg->num_clks; i++) {
757 		pg->clk_rates[i] = clk_get_rate(pg->clks[i]);
758 
759 		if (!pg->clk_rates[i]) {
760 			err = -EINVAL;
761 			goto out;
762 		}
763 
764 		if (pg->clk_rates[i] <= safe_rate)
765 			continue;
766 
767 		/*
768 		 * We don't know whether voltage state is okay for the
769 		 * current clock rate, hence it's better to temporally
770 		 * switch clock to a safe rate which is suitable for
771 		 * all voltages, before enabling the clock.
772 		 */
773 		err = clk_set_rate(pg->clks[i], safe_rate);
774 		if (err)
775 			goto out;
776 	}
777 
778 	return 0;
779 
780 out:
781 	while (i--)
782 		clk_set_rate(pg->clks[i], pg->clk_rates[i]);
783 
784 	return err;
785 }
786 
787 static int tegra_powergate_unprepare_clocks(struct tegra_powergate *pg)
788 {
789 	unsigned int i;
790 	int err;
791 
792 	for (i = 0; i < pg->num_clks; i++) {
793 		err = clk_set_rate(pg->clks[i], pg->clk_rates[i]);
794 		if (err)
795 			return err;
796 	}
797 
798 	return 0;
799 }
800 
801 static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
802 {
803 	unsigned int i;
804 
805 	for (i = 0; i < pg->num_clks; i++)
806 		clk_disable_unprepare(pg->clks[i]);
807 }
808 
809 static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
810 {
811 	unsigned int i;
812 	int err;
813 
814 	for (i = 0; i < pg->num_clks; i++) {
815 		err = clk_prepare_enable(pg->clks[i]);
816 		if (err)
817 			goto out;
818 	}
819 
820 	return 0;
821 
822 out:
823 	while (i--)
824 		clk_disable_unprepare(pg->clks[i]);
825 
826 	return err;
827 }
828 
829 static int tegra_powergate_power_up(struct tegra_powergate *pg,
830 				    bool disable_clocks)
831 {
832 	int err;
833 
834 	err = reset_control_assert(pg->reset);
835 	if (err)
836 		return err;
837 
838 	usleep_range(10, 20);
839 
840 	err = tegra_powergate_set(pg->pmc, pg->id, true);
841 	if (err < 0)
842 		return err;
843 
844 	usleep_range(10, 20);
845 
846 	err = tegra_powergate_prepare_clocks(pg);
847 	if (err)
848 		goto powergate_off;
849 
850 	err = tegra_powergate_enable_clocks(pg);
851 	if (err)
852 		goto unprepare_clks;
853 
854 	usleep_range(10, 20);
855 
856 	err = __tegra_powergate_remove_clamping(pg->pmc, pg->id);
857 	if (err)
858 		goto disable_clks;
859 
860 	usleep_range(10, 20);
861 
862 	err = reset_control_deassert(pg->reset);
863 	if (err)
864 		goto disable_clks;
865 
866 	usleep_range(10, 20);
867 
868 	if (pg->pmc->soc->needs_mbist_war)
869 		err = tegra210_clk_handle_mbist_war(pg->id);
870 	if (err)
871 		goto disable_clks;
872 
873 	if (disable_clocks)
874 		tegra_powergate_disable_clocks(pg);
875 
876 	err = tegra_powergate_unprepare_clocks(pg);
877 	if (err)
878 		return err;
879 
880 	return 0;
881 
882 disable_clks:
883 	tegra_powergate_disable_clocks(pg);
884 	usleep_range(10, 20);
885 
886 unprepare_clks:
887 	tegra_powergate_unprepare_clocks(pg);
888 
889 powergate_off:
890 	tegra_powergate_set(pg->pmc, pg->id, false);
891 
892 	return err;
893 }
894 
895 static int tegra_powergate_power_down(struct tegra_powergate *pg)
896 {
897 	int err;
898 
899 	err = tegra_powergate_prepare_clocks(pg);
900 	if (err)
901 		return err;
902 
903 	err = tegra_powergate_enable_clocks(pg);
904 	if (err)
905 		goto unprepare_clks;
906 
907 	usleep_range(10, 20);
908 
909 	err = reset_control_assert(pg->reset);
910 	if (err)
911 		goto disable_clks;
912 
913 	usleep_range(10, 20);
914 
915 	tegra_powergate_disable_clocks(pg);
916 
917 	usleep_range(10, 20);
918 
919 	err = tegra_powergate_set(pg->pmc, pg->id, false);
920 	if (err)
921 		goto assert_resets;
922 
923 	err = tegra_powergate_unprepare_clocks(pg);
924 	if (err)
925 		return err;
926 
927 	return 0;
928 
929 assert_resets:
930 	tegra_powergate_enable_clocks(pg);
931 	usleep_range(10, 20);
932 	reset_control_deassert(pg->reset);
933 	usleep_range(10, 20);
934 
935 disable_clks:
936 	tegra_powergate_disable_clocks(pg);
937 
938 unprepare_clks:
939 	tegra_powergate_unprepare_clocks(pg);
940 
941 	return err;
942 }
943 
944 static int tegra_genpd_power_on(struct generic_pm_domain *domain)
945 {
946 	struct tegra_powergate *pg = to_powergate(domain);
947 	struct device *dev = pg->pmc->dev;
948 	int err;
949 
950 	err = tegra_powergate_power_up(pg, true);
951 	if (err) {
952 		dev_err(dev, "failed to turn on PM domain %s: %d\n",
953 			pg->genpd.name, err);
954 		goto out;
955 	}
956 
957 	reset_control_release(pg->reset);
958 
959 out:
960 	return err;
961 }
962 
963 static int tegra_genpd_power_off(struct generic_pm_domain *domain)
964 {
965 	struct tegra_powergate *pg = to_powergate(domain);
966 	struct device *dev = pg->pmc->dev;
967 	int err;
968 
969 	err = reset_control_acquire(pg->reset);
970 	if (err < 0) {
971 		dev_err(dev, "failed to acquire resets for PM domain %s: %d\n",
972 			pg->genpd.name, err);
973 		return err;
974 	}
975 
976 	err = tegra_powergate_power_down(pg);
977 	if (err) {
978 		dev_err(dev, "failed to turn off PM domain %s: %d\n",
979 			pg->genpd.name, err);
980 		reset_control_release(pg->reset);
981 	}
982 
983 	return err;
984 }
985 
986 static void tegra_pmc_put_device(void *data)
987 {
988 	struct tegra_pmc *pmc = data;
989 
990 	put_device(pmc->dev);
991 }
992 
993 static const struct of_device_id tegra_pmc_match[];
994 
995 static struct tegra_pmc *tegra_pmc_get(struct device *dev)
996 {
997 	struct platform_device *pdev;
998 	struct device_node *np;
999 	struct tegra_pmc *pmc;
1000 
1001 	np = of_parse_phandle(dev->of_node, "nvidia,pmc", 0);
1002 	if (!np) {
1003 		struct device_node *parent = of_node_get(dev->of_node);
1004 
1005 		while ((parent = of_get_next_parent(parent)) != NULL) {
1006 			np = of_find_matching_node(parent, tegra_pmc_match);
1007 			if (np)
1008 				break;
1009 		}
1010 
1011 		of_node_put(parent);
1012 
1013 		if (!np)
1014 			return ERR_PTR(-ENODEV);
1015 	}
1016 
1017 	pdev = of_find_device_by_node(np);
1018 	of_node_put(np);
1019 
1020 	if (!pdev)
1021 		return ERR_PTR(-ENODEV);
1022 
1023 	pmc = platform_get_drvdata(pdev);
1024 	if (!pmc) {
1025 		put_device(&pdev->dev);
1026 		return ERR_PTR(-EPROBE_DEFER);
1027 	}
1028 
1029 	return pmc;
1030 }
1031 
1032 /**
1033  * devm_tegra_pmc_get() - find the PMC for a given device
1034  * @dev: device for which to find the PMC
1035  *
1036  * Returns a pointer to the PMC on success or an ERR_PTR()-encoded error code
1037  * otherwise.
1038  */
1039 struct tegra_pmc *devm_tegra_pmc_get(struct device *dev)
1040 {
1041 	struct tegra_pmc *pmc;
1042 	int err;
1043 
1044 	pmc = tegra_pmc_get(dev);
1045 	if (IS_ERR(pmc))
1046 		return pmc;
1047 
1048 	err = devm_add_action_or_reset(dev, tegra_pmc_put_device, pmc);
1049 	if (err < 0)
1050 		return ERR_PTR(err);
1051 
1052 	return pmc;
1053 }
1054 EXPORT_SYMBOL(devm_tegra_pmc_get);
1055 
1056 /**
1057  * tegra_pmc_powergate_power_on() - power on partition
1058  * @pmc: power management controller
1059  * @id: partition ID
1060  */
1061 int tegra_pmc_powergate_power_on(struct tegra_pmc *pmc, unsigned int id)
1062 {
1063 	if (!tegra_powergate_is_available(pmc, id))
1064 		return -EINVAL;
1065 
1066 	return tegra_powergate_set(pmc, id, true);
1067 }
1068 EXPORT_SYMBOL(tegra_pmc_powergate_power_on);
1069 
1070 /**
1071  * tegra_pmc_powergate_power_off() - power off partition
1072  * @pmc: power management controller
1073  * @id: partition ID
1074  */
1075 int tegra_pmc_powergate_power_off(struct tegra_pmc *pmc, unsigned int id)
1076 {
1077 	if (!tegra_powergate_is_available(pmc, id))
1078 		return -EINVAL;
1079 
1080 	return tegra_powergate_set(pmc, id, false);
1081 }
1082 EXPORT_SYMBOL(tegra_pmc_powergate_power_off);
1083 
1084 /**
1085  * tegra_powergate_is_powered() - check if partition is powered
1086  * @pmc: power management controller
1087  * @id: partition ID
1088  */
1089 static int tegra_powergate_is_powered(struct tegra_pmc *pmc, unsigned int id)
1090 {
1091 	if (!tegra_powergate_is_valid(pmc, id))
1092 		return -EINVAL;
1093 
1094 	return tegra_powergate_state(pmc, id);
1095 }
1096 
1097 /**
1098  * tegra_pmc_powergate_remove_clamping() - remove power clamps for partition
1099  * @pmc: power management controller
1100  * @id: partition ID
1101  */
1102 int tegra_pmc_powergate_remove_clamping(struct tegra_pmc *pmc, unsigned int id)
1103 {
1104 	if (!tegra_powergate_is_available(pmc, id))
1105 		return -EINVAL;
1106 
1107 	return __tegra_powergate_remove_clamping(pmc, id);
1108 }
1109 EXPORT_SYMBOL(tegra_pmc_powergate_remove_clamping);
1110 
1111 /**
1112  * tegra_pmc_powergate_sequence_power_up() - power up partition
1113  * @pmc: power management controller
1114  * @id: partition ID
1115  * @clk: clock for partition
1116  * @rst: reset for partition
1117  *
1118  * Must be called with clk disabled, and returns with clk enabled.
1119  */
1120 int tegra_pmc_powergate_sequence_power_up(struct tegra_pmc *pmc,
1121 					  unsigned int id, struct clk *clk,
1122 					  struct reset_control *rst)
1123 {
1124 	struct tegra_powergate *pg;
1125 	int err;
1126 
1127 	if (!tegra_powergate_is_available(pmc, id))
1128 		return -EINVAL;
1129 
1130 	pg = kzalloc_obj(*pg);
1131 	if (!pg)
1132 		return -ENOMEM;
1133 
1134 	pg->clk_rates = kzalloc_obj(*pg->clk_rates);
1135 	if (!pg->clk_rates) {
1136 		kfree(pg->clks);
1137 		return -ENOMEM;
1138 	}
1139 
1140 	pg->id = id;
1141 	pg->clks = &clk;
1142 	pg->num_clks = 1;
1143 	pg->reset = rst;
1144 	pg->pmc = pmc;
1145 
1146 	err = tegra_powergate_power_up(pg, false);
1147 	if (err)
1148 		dev_err(pmc->dev, "failed to turn on partition %d: %d\n", id,
1149 			err);
1150 
1151 	kfree(pg->clk_rates);
1152 	kfree(pg);
1153 
1154 	return err;
1155 }
1156 EXPORT_SYMBOL(tegra_pmc_powergate_sequence_power_up);
1157 
1158 static void tegra_pmc_program_reboot_reason(struct tegra_pmc *pmc,
1159 					    const char *cmd)
1160 {
1161 	u32 value;
1162 
1163 	value = tegra_pmc_scratch_readl(pmc, pmc->soc->regs->scratch0);
1164 	value &= ~PMC_SCRATCH0_MODE_MASK;
1165 
1166 	if (cmd) {
1167 		if (strcmp(cmd, "recovery") == 0)
1168 			value |= PMC_SCRATCH0_MODE_RECOVERY;
1169 
1170 		if (strcmp(cmd, "bootloader") == 0)
1171 			value |= PMC_SCRATCH0_MODE_BOOTLOADER;
1172 
1173 		if (strcmp(cmd, "forced-recovery") == 0)
1174 			value |= PMC_SCRATCH0_MODE_RCM;
1175 	}
1176 
1177 	tegra_pmc_scratch_writel(pmc, value, pmc->soc->regs->scratch0);
1178 }
1179 
1180 static int tegra_pmc_reboot_notify(struct notifier_block *this,
1181 				   unsigned long action, void *data)
1182 {
1183 	struct tegra_pmc *pmc = container_of(this, struct tegra_pmc,
1184 					     reboot_notifier);
1185 	if (action == SYS_RESTART)
1186 		tegra_pmc_program_reboot_reason(pmc, data);
1187 
1188 	return NOTIFY_DONE;
1189 }
1190 
1191 static void tegra_pmc_restart(struct tegra_pmc *pmc)
1192 {
1193 	u32 value;
1194 
1195 	/* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
1196 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
1197 	value |= PMC_CNTRL_MAIN_RST;
1198 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
1199 }
1200 
1201 static int tegra_pmc_restart_handler(struct sys_off_data *data)
1202 {
1203 	struct tegra_pmc *pmc = data->cb_data;
1204 
1205 	tegra_pmc_restart(pmc);
1206 
1207 	return NOTIFY_DONE;
1208 }
1209 
1210 static int tegra_pmc_grouper_power_off_handler(struct sys_off_data *data)
1211 {
1212 	struct tegra_pmc *pmc = data->cb_data;
1213 
1214 	/*
1215 	 * Reboot Nexus 7 into special bootloader mode if USB cable is
1216 	 * connected in order to display battery status and power off.
1217 	 */
1218 	if (power_supply_is_system_supplied()) {
1219 		const u32 go_to_charger_mode = 0xa5a55a5a;
1220 
1221 		tegra_pmc_writel(pmc, go_to_charger_mode, PMC_SCRATCH37);
1222 		tegra_pmc_restart(pmc);
1223 	}
1224 
1225 	return NOTIFY_DONE;
1226 }
1227 
1228 static int powergate_show(struct seq_file *s, void *data)
1229 {
1230 	struct tegra_pmc *pmc = data;
1231 	unsigned int i;
1232 	int status;
1233 
1234 	seq_printf(s, " powergate powered\n");
1235 	seq_printf(s, "------------------\n");
1236 
1237 	for (i = 0; i < pmc->soc->num_powergates; i++) {
1238 		status = tegra_powergate_is_powered(pmc, i);
1239 		if (status < 0)
1240 			continue;
1241 
1242 		seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
1243 			   str_yes_no(status));
1244 	}
1245 
1246 	return 0;
1247 }
1248 
1249 DEFINE_SHOW_ATTRIBUTE(powergate);
1250 
1251 static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
1252 				       struct device_node *np)
1253 {
1254 	struct clk *clk;
1255 	unsigned int i, count;
1256 	int err;
1257 
1258 	count = of_clk_get_parent_count(np);
1259 	if (count == 0)
1260 		return -ENODEV;
1261 
1262 	pg->clks = kzalloc_objs(clk, count);
1263 	if (!pg->clks)
1264 		return -ENOMEM;
1265 
1266 	pg->clk_rates = kcalloc(count, sizeof(*pg->clk_rates), GFP_KERNEL);
1267 	if (!pg->clk_rates) {
1268 		kfree(pg->clks);
1269 		return -ENOMEM;
1270 	}
1271 
1272 	for (i = 0; i < count; i++) {
1273 		pg->clks[i] = of_clk_get(np, i);
1274 		if (IS_ERR(pg->clks[i])) {
1275 			err = PTR_ERR(pg->clks[i]);
1276 			goto err;
1277 		}
1278 	}
1279 
1280 	pg->num_clks = count;
1281 
1282 	return 0;
1283 
1284 err:
1285 	while (i--)
1286 		clk_put(pg->clks[i]);
1287 
1288 	kfree(pg->clk_rates);
1289 	kfree(pg->clks);
1290 
1291 	return err;
1292 }
1293 
1294 static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
1295 					 struct device_node *np)
1296 {
1297 	struct device *dev = pg->pmc->dev;
1298 	int err;
1299 
1300 	pg->reset = of_reset_control_array_get_exclusive_released(np);
1301 	if (IS_ERR(pg->reset)) {
1302 		err = PTR_ERR(pg->reset);
1303 		dev_err(dev, "failed to get device resets: %d\n", err);
1304 		return err;
1305 	}
1306 
1307 	err = reset_control_acquire(pg->reset);
1308 	if (err < 0) {
1309 		pr_err("failed to acquire resets: %d\n", err);
1310 		reset_control_put(pg->reset);
1311 	}
1312 
1313 	return err;
1314 }
1315 
1316 static int tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
1317 {
1318 	struct device *dev = pmc->dev;
1319 	struct tegra_powergate *pg;
1320 	int id, err = 0;
1321 	bool off;
1322 
1323 	pg = kzalloc_obj(*pg);
1324 	if (!pg)
1325 		return -ENOMEM;
1326 
1327 	id = tegra_powergate_lookup(pmc, np->name);
1328 	if (id < 0) {
1329 		dev_err(dev, "powergate lookup failed for %pOFn: %d\n", np, id);
1330 		err = -ENODEV;
1331 		goto free_mem;
1332 	}
1333 
1334 	/*
1335 	 * Clear the bit for this powergate so it cannot be managed
1336 	 * directly via the legacy APIs for controlling powergates.
1337 	 */
1338 	clear_bit(id, pmc->powergates_available);
1339 
1340 	pg->id = id;
1341 	pg->genpd.name = np->name;
1342 	pg->genpd.flags = GENPD_FLAG_NO_SYNC_STATE;
1343 	pg->genpd.power_off = tegra_genpd_power_off;
1344 	pg->genpd.power_on = tegra_genpd_power_on;
1345 	pg->pmc = pmc;
1346 
1347 	off = !tegra_powergate_is_powered(pmc, pg->id);
1348 
1349 	err = tegra_powergate_of_get_clks(pg, np);
1350 	if (err < 0) {
1351 		dev_err(dev, "failed to get clocks for %pOFn: %d\n", np, err);
1352 		goto set_available;
1353 	}
1354 
1355 	err = tegra_powergate_of_get_resets(pg, np);
1356 	if (err < 0) {
1357 		dev_err(dev, "failed to get resets for %pOFn: %d\n", np, err);
1358 		goto remove_clks;
1359 	}
1360 
1361 	/*
1362 	 * If the power-domain is off, then ensure the resets are asserted.
1363 	 * If the power-domain is on, then power down to ensure that when is
1364 	 * it turned on the power-domain, clocks and resets are all in the
1365 	 * expected state.
1366 	 */
1367 	if (off) {
1368 		err = reset_control_assert(pg->reset);
1369 		if (err) {
1370 			pr_err("failed to assert resets: %d\n", err);
1371 			goto remove_resets;
1372 		}
1373 	} else {
1374 		err = tegra_powergate_power_down(pg);
1375 		if (err) {
1376 			dev_err(dev, "failed to turn off PM domain %s: %d\n",
1377 				pg->genpd.name, err);
1378 			goto remove_resets;
1379 		}
1380 	}
1381 
1382 	/*
1383 	 * If PM_GENERIC_DOMAINS is not enabled, power-on
1384 	 * the domain and skip the genpd registration.
1385 	 */
1386 	if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
1387 		WARN_ON(tegra_powergate_power_up(pg, true));
1388 		goto remove_resets;
1389 	}
1390 
1391 	err = pm_genpd_init(&pg->genpd, NULL, true);
1392 	if (err < 0) {
1393 		dev_err(dev, "failed to initialise PM domain %pOFn: %d\n", np,
1394 		       err);
1395 		goto remove_resets;
1396 	}
1397 
1398 	err = of_genpd_add_provider_simple(np, &pg->genpd);
1399 	if (err < 0) {
1400 		dev_err(dev, "failed to add PM domain provider for %pOFn: %d\n",
1401 			np, err);
1402 		goto remove_genpd;
1403 	}
1404 
1405 	dev_dbg(dev, "added PM domain %s\n", pg->genpd.name);
1406 
1407 	return 0;
1408 
1409 remove_genpd:
1410 	pm_genpd_remove(&pg->genpd);
1411 
1412 remove_resets:
1413 	reset_control_put(pg->reset);
1414 
1415 remove_clks:
1416 	while (pg->num_clks--)
1417 		clk_put(pg->clks[pg->num_clks]);
1418 
1419 	kfree(pg->clks);
1420 
1421 set_available:
1422 	set_bit(id, pmc->powergates_available);
1423 
1424 free_mem:
1425 	kfree(pg);
1426 
1427 	return err;
1428 }
1429 
1430 static int
1431 tegra_pmc_core_pd_set_performance_state(struct generic_pm_domain *genpd,
1432 					unsigned int level)
1433 {
1434 	struct tegra_pmc_core_pd *pd = to_core_pd(genpd);
1435 	struct tegra_pmc *pmc = pd->pmc;
1436 	struct dev_pm_opp *opp;
1437 	int err;
1438 
1439 	opp = dev_pm_opp_find_level_ceil(&genpd->dev, &level);
1440 	if (IS_ERR(opp)) {
1441 		dev_err(&genpd->dev, "failed to find OPP for level %u: %pe\n",
1442 			level, opp);
1443 		return PTR_ERR(opp);
1444 	}
1445 
1446 	mutex_lock(&pmc->powergates_lock);
1447 	err = dev_pm_opp_set_opp(pmc->dev, opp);
1448 	mutex_unlock(&pmc->powergates_lock);
1449 
1450 	dev_pm_opp_put(opp);
1451 
1452 	if (err) {
1453 		dev_err(&genpd->dev, "failed to set voltage to %duV: %d\n",
1454 			level, err);
1455 		return err;
1456 	}
1457 
1458 	return 0;
1459 }
1460 
1461 static int tegra_pmc_core_pd_add(struct tegra_pmc *pmc, struct device_node *np)
1462 {
1463 	const char *rname[] = { "core", NULL};
1464 	struct tegra_pmc_core_pd *pd;
1465 	int err;
1466 
1467 	pd = devm_kzalloc(pmc->dev, sizeof(*pd), GFP_KERNEL);
1468 	if (!pd)
1469 		return -ENOMEM;
1470 
1471 	pd->genpd.name = "core";
1472 	pd->genpd.flags = GENPD_FLAG_NO_SYNC_STATE;
1473 	pd->genpd.set_performance_state = tegra_pmc_core_pd_set_performance_state;
1474 	pd->pmc = pmc;
1475 
1476 	err = devm_pm_opp_set_regulators(pmc->dev, rname);
1477 	if (err)
1478 		return dev_err_probe(pmc->dev, err,
1479 				     "failed to set core OPP regulator\n");
1480 
1481 	err = pm_genpd_init(&pd->genpd, NULL, false);
1482 	if (err) {
1483 		dev_err(pmc->dev, "failed to init core genpd: %d\n", err);
1484 		return err;
1485 	}
1486 
1487 	err = of_genpd_add_provider_simple(np, &pd->genpd);
1488 	if (err) {
1489 		dev_err(pmc->dev, "failed to add core genpd: %d\n", err);
1490 		goto remove_genpd;
1491 	}
1492 
1493 	return 0;
1494 
1495 remove_genpd:
1496 	pm_genpd_remove(&pd->genpd);
1497 
1498 	return err;
1499 }
1500 
1501 static int tegra_powergate_init(struct tegra_pmc *pmc,
1502 				struct device_node *parent)
1503 {
1504 	struct of_phandle_args child_args, parent_args;
1505 	struct device_node *np;
1506 	int err = 0;
1507 
1508 	/*
1509 	 * Core power domain is the parent of powergate domains, hence it
1510 	 * should be registered first.
1511 	 */
1512 	np = of_get_child_by_name(parent, "core-domain");
1513 	if (np) {
1514 		err = tegra_pmc_core_pd_add(pmc, np);
1515 		of_node_put(np);
1516 		if (err)
1517 			return err;
1518 	}
1519 
1520 	np = of_get_child_by_name(parent, "powergates");
1521 	if (!np)
1522 		return 0;
1523 
1524 	for_each_child_of_node_scoped(np, child) {
1525 		err = tegra_powergate_add(pmc, child);
1526 		if (err < 0)
1527 			break;
1528 
1529 		if (of_parse_phandle_with_args(child, "power-domains",
1530 					       "#power-domain-cells",
1531 					       0, &parent_args))
1532 			continue;
1533 
1534 		child_args.np = child;
1535 		child_args.args_count = 0;
1536 
1537 		err = of_genpd_add_subdomain(&parent_args, &child_args);
1538 		of_node_put(parent_args.np);
1539 		if (err)
1540 			break;
1541 	}
1542 
1543 	of_node_put(np);
1544 
1545 	return err;
1546 }
1547 
1548 static void tegra_powergate_remove(struct generic_pm_domain *genpd)
1549 {
1550 	struct tegra_powergate *pg = to_powergate(genpd);
1551 
1552 	reset_control_put(pg->reset);
1553 
1554 	while (pg->num_clks--)
1555 		clk_put(pg->clks[pg->num_clks]);
1556 
1557 	kfree(pg->clks);
1558 
1559 	set_bit(pg->id, pg->pmc->powergates_available);
1560 
1561 	kfree(pg);
1562 }
1563 
1564 static void tegra_powergate_remove_all(struct device_node *parent)
1565 {
1566 	struct generic_pm_domain *genpd;
1567 	struct device_node *np, *child;
1568 
1569 	np = of_get_child_by_name(parent, "powergates");
1570 	if (!np)
1571 		return;
1572 
1573 	for_each_child_of_node(np, child) {
1574 		of_genpd_del_provider(child);
1575 
1576 		genpd = of_genpd_remove_last(child);
1577 		if (IS_ERR(genpd))
1578 			continue;
1579 
1580 		tegra_powergate_remove(genpd);
1581 	}
1582 
1583 	of_node_put(np);
1584 
1585 	np = of_get_child_by_name(parent, "core-domain");
1586 	if (np) {
1587 		of_genpd_del_provider(np);
1588 		of_genpd_remove_last(np);
1589 	}
1590 }
1591 
1592 static const struct tegra_io_pad_soc *
1593 tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
1594 {
1595 	unsigned int i;
1596 
1597 	for (i = 0; i < pmc->soc->num_io_pads; i++)
1598 		if (pmc->soc->io_pads[i].id == id)
1599 			return &pmc->soc->io_pads[i];
1600 
1601 	return NULL;
1602 }
1603 
1604 static const struct tegra_io_pad_vctrl *
1605 tegra_io_pad_vctrl_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
1606 {
1607 	unsigned int i;
1608 
1609 	for (i = 0; i < pmc->soc->num_io_pad_vctrls; i++)
1610 		if (pmc->soc->io_pad_vctrls[i].id == id)
1611 			return &pmc->soc->io_pad_vctrls[i];
1612 
1613 	return NULL;
1614 }
1615 
1616 static int tegra_io_pad_prepare(struct tegra_pmc *pmc,
1617 				const struct tegra_io_pad_soc *pad,
1618 				unsigned long *request,
1619 				unsigned long *status,
1620 				u32 *mask)
1621 {
1622 	unsigned long rate, value;
1623 
1624 	if (pad->dpd == UINT_MAX)
1625 		return -EINVAL;
1626 
1627 	*request = pad->request;
1628 	*status = pad->status;
1629 	*mask = BIT(pad->dpd);
1630 
1631 	if (pmc->clk) {
1632 		rate = pmc->rate;
1633 		if (!rate) {
1634 			dev_err(pmc->dev, "failed to get clock rate\n");
1635 			return -ENODEV;
1636 		}
1637 
1638 		tegra_pmc_writel(pmc, DPD_SAMPLE_ENABLE, DPD_SAMPLE);
1639 
1640 		/* must be at least 200 ns, in APB (PCLK) clock cycles */
1641 		value = DIV_ROUND_UP(1000000000, rate);
1642 		value = DIV_ROUND_UP(200, value);
1643 		tegra_pmc_writel(pmc, value, SEL_DPD_TIM);
1644 	}
1645 
1646 	return 0;
1647 }
1648 
1649 static int tegra_io_pad_poll(struct tegra_pmc *pmc, unsigned long offset,
1650 			     u32 mask, u32 val, unsigned long timeout)
1651 {
1652 	u32 value;
1653 
1654 	timeout = jiffies + msecs_to_jiffies(timeout);
1655 
1656 	while (time_after(timeout, jiffies)) {
1657 		value = tegra_pmc_readl(pmc, offset);
1658 		if ((value & mask) == val)
1659 			return 0;
1660 
1661 		usleep_range(250, 1000);
1662 	}
1663 
1664 	return -ETIMEDOUT;
1665 }
1666 
1667 static void tegra_io_pad_unprepare(struct tegra_pmc *pmc)
1668 {
1669 	if (pmc->clk)
1670 		tegra_pmc_writel(pmc, DPD_SAMPLE_DISABLE, DPD_SAMPLE);
1671 }
1672 
1673 /**
1674  * tegra_pmc_io_pad_power_enable() - enable power to I/O pad
1675  * @pmc: power management controller
1676  * @id: Tegra I/O pad ID for which to enable power
1677  *
1678  * Returns: 0 on success or a negative error code on failure.
1679  */
1680 int tegra_pmc_io_pad_power_enable(struct tegra_pmc *pmc, enum tegra_io_pad id)
1681 {
1682 	const struct tegra_io_pad_soc *pad;
1683 	unsigned long request, status;
1684 	u32 mask;
1685 	int err;
1686 
1687 	pad = tegra_io_pad_find(pmc, id);
1688 	if (!pad) {
1689 		dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
1690 		return -ENOENT;
1691 	}
1692 
1693 	mutex_lock(&pmc->powergates_lock);
1694 
1695 	err = tegra_io_pad_prepare(pmc, pad, &request, &status, &mask);
1696 	if (err < 0) {
1697 		dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
1698 		goto unlock;
1699 	}
1700 
1701 	tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_OFF | mask, request);
1702 
1703 	err = tegra_io_pad_poll(pmc, status, mask, 0, 250);
1704 	if (err < 0) {
1705 		dev_err(pmc->dev, "failed to enable I/O pad: %d\n", err);
1706 		goto unlock;
1707 	}
1708 
1709 	tegra_io_pad_unprepare(pmc);
1710 
1711 unlock:
1712 	mutex_unlock(&pmc->powergates_lock);
1713 	return err;
1714 }
1715 EXPORT_SYMBOL(tegra_pmc_io_pad_power_enable);
1716 
1717 /**
1718  * tegra_pmc_io_pad_power_disable() - disable power to I/O pad
1719  * @pmc: power management controller
1720  * @id: Tegra I/O pad ID for which to disable power
1721  *
1722  * Returns: 0 on success or a negative error code on failure.
1723  */
1724 int tegra_pmc_io_pad_power_disable(struct tegra_pmc *pmc, enum tegra_io_pad id)
1725 {
1726 	const struct tegra_io_pad_soc *pad;
1727 	unsigned long request, status;
1728 	u32 mask;
1729 	int err;
1730 
1731 	pad = tegra_io_pad_find(pmc, id);
1732 	if (!pad) {
1733 		dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
1734 		return -ENOENT;
1735 	}
1736 
1737 	mutex_lock(&pmc->powergates_lock);
1738 
1739 	err = tegra_io_pad_prepare(pmc, pad, &request, &status, &mask);
1740 	if (err < 0) {
1741 		dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
1742 		goto unlock;
1743 	}
1744 
1745 	tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_ON | mask, request);
1746 
1747 	err = tegra_io_pad_poll(pmc, status, mask, mask, 250);
1748 	if (err < 0) {
1749 		dev_err(pmc->dev, "failed to disable I/O pad: %d\n", err);
1750 		goto unlock;
1751 	}
1752 
1753 	tegra_io_pad_unprepare(pmc);
1754 
1755 unlock:
1756 	mutex_unlock(&pmc->powergates_lock);
1757 	return err;
1758 }
1759 EXPORT_SYMBOL(tegra_pmc_io_pad_power_disable);
1760 
1761 static int tegra_io_pad_is_powered(struct tegra_pmc *pmc, enum tegra_io_pad id)
1762 {
1763 	const struct tegra_io_pad_soc *pad;
1764 	unsigned long status;
1765 	u32 mask, value;
1766 
1767 	pad = tegra_io_pad_find(pmc, id);
1768 	if (!pad) {
1769 		dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
1770 		return -ENOENT;
1771 	}
1772 
1773 	if (pad->dpd == UINT_MAX)
1774 		return -EINVAL;
1775 
1776 	status = pad->status;
1777 	mask = BIT(pad->dpd);
1778 
1779 	value = tegra_pmc_readl(pmc, status);
1780 
1781 	return !(value & mask);
1782 }
1783 
1784 static int tegra_io_pad_set_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id,
1785 				    int voltage)
1786 {
1787 	const struct tegra_io_pad_vctrl *pad;
1788 	u32 value;
1789 
1790 	pad = tegra_io_pad_vctrl_find(pmc, id);
1791 	if (!pad)
1792 		return -ENOENT;
1793 
1794 	mutex_lock(&pmc->powergates_lock);
1795 
1796 	if (pmc->soc->has_io_pad_wren) {
1797 		/* write-enable PMC_PWR_DET_VALUE[pad->ena_3v3] */
1798 		value = tegra_pmc_readl(pmc, PMC_PWR_DET);
1799 		value |= BIT(pad->ena_3v3);
1800 		tegra_pmc_writel(pmc, value, PMC_PWR_DET);
1801 	}
1802 
1803 	value = tegra_pmc_readl(pmc, pad->offset);
1804 
1805 	if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8) {
1806 		value &= ~BIT(pad->ena_3v3);
1807 
1808 		if (pad->ena_1v8)
1809 			value |= pad->ena_1v8;
1810 	} else {
1811 		value |= BIT(pad->ena_3v3);
1812 
1813 		if (pad->ena_1v8)
1814 			value &= ~pad->ena_1v8;
1815 	}
1816 
1817 	tegra_pmc_writel(pmc, value, pad->offset);
1818 
1819 	mutex_unlock(&pmc->powergates_lock);
1820 
1821 	usleep_range(100, 250);
1822 
1823 	return 0;
1824 }
1825 
1826 static int tegra_io_pad_get_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id)
1827 {
1828 	const struct tegra_io_pad_vctrl *pad;
1829 	u32 value;
1830 
1831 	pad = tegra_io_pad_vctrl_find(pmc, id);
1832 	if (!pad)
1833 		return -ENOENT;
1834 
1835 	value = tegra_pmc_readl(pmc, pad->offset);
1836 
1837 	if ((value & BIT(pad->ena_3v3)) == 0)
1838 		return TEGRA_IO_PAD_VOLTAGE_1V8;
1839 
1840 	return TEGRA_IO_PAD_VOLTAGE_3V3;
1841 }
1842 
1843 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1844 {
1845 	u32 value, values[2];
1846 
1847 	if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1848 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1849 	} else {
1850 		switch (value) {
1851 		case 0:
1852 			pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1853 			break;
1854 
1855 		case 1:
1856 			pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1857 			break;
1858 
1859 		case 2:
1860 			pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1861 			break;
1862 
1863 		default:
1864 			pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1865 			break;
1866 		}
1867 	}
1868 
1869 	pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1870 
1871 	if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1872 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1873 
1874 	pmc->cpu_good_time = value;
1875 
1876 	if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1877 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1878 
1879 	pmc->cpu_off_time = value;
1880 
1881 	if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1882 				       values, ARRAY_SIZE(values)))
1883 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1884 
1885 	pmc->core_osc_time = values[0];
1886 	pmc->core_pmu_time = values[1];
1887 
1888 	if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1889 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1890 
1891 	pmc->core_off_time = value;
1892 
1893 	pmc->corereq_high = of_property_read_bool(np,
1894 				"nvidia,core-power-req-active-high");
1895 
1896 	pmc->sysclkreq_high = of_property_read_bool(np,
1897 				"nvidia,sys-clock-req-active-high");
1898 
1899 	pmc->combined_req = of_property_read_bool(np,
1900 				"nvidia,combined-power-req");
1901 
1902 	pmc->cpu_pwr_good_en = of_property_read_bool(np,
1903 				"nvidia,cpu-pwr-good-en");
1904 
1905 	if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1906 				       ARRAY_SIZE(values)))
1907 		if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1908 			pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1909 
1910 	pmc->lp0_vec_phys = values[0];
1911 	pmc->lp0_vec_size = values[1];
1912 
1913 	pmc->invert_irq = of_property_read_bool(np, "nvidia,invert-interrupt");
1914 
1915 	return 0;
1916 }
1917 
1918 /* translate sc7 wake sources back into IRQs to catch edge triggered wakeups */
1919 static void tegra186_pmc_wake_handler(struct irq_work *work)
1920 {
1921 	struct tegra_pmc *pmc = container_of(work, struct tegra_pmc, wake_work);
1922 	unsigned int i, wake;
1923 
1924 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
1925 		unsigned long status = pmc->wake_status[i];
1926 
1927 		for_each_set_bit(wake, &status, 32) {
1928 			irq_hw_number_t hwirq = wake + (i * 32);
1929 			struct irq_desc *desc;
1930 			unsigned int irq;
1931 
1932 			irq = irq_find_mapping(pmc->domain, hwirq);
1933 			if (!irq) {
1934 				dev_warn(pmc->dev,
1935 					 "No IRQ found for WAKE#%lu!\n",
1936 					 hwirq);
1937 				continue;
1938 			}
1939 
1940 			dev_dbg(pmc->dev,
1941 				"Resume caused by WAKE#%lu mapped to IRQ#%u\n",
1942 				hwirq, irq);
1943 
1944 			desc = irq_to_desc(irq);
1945 			if (!desc) {
1946 				dev_warn(pmc->dev,
1947 					 "No descriptor found for IRQ#%u\n",
1948 					 irq);
1949 				continue;
1950 			}
1951 
1952 			if (!desc->action || !desc->action->name)
1953 				continue;
1954 
1955 			generic_handle_irq(irq);
1956 		}
1957 
1958 		pmc->wake_status[i] = 0;
1959 	}
1960 }
1961 
1962 static int tegra_pmc_init(struct tegra_pmc *pmc)
1963 {
1964 	if (pmc->soc->max_wake_events > 0) {
1965 		pmc->wake_type_level_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1966 		if (!pmc->wake_type_level_map)
1967 			return -ENOMEM;
1968 
1969 		pmc->wake_type_dual_edge_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1970 		if (!pmc->wake_type_dual_edge_map)
1971 			return -ENOMEM;
1972 
1973 		pmc->wake_sw_status_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1974 		if (!pmc->wake_sw_status_map)
1975 			return -ENOMEM;
1976 
1977 		pmc->wake_cntrl_level_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1978 		if (!pmc->wake_cntrl_level_map)
1979 			return -ENOMEM;
1980 
1981 		pmc->wake_status = kcalloc(pmc->soc->max_wake_vectors, sizeof(u32), GFP_KERNEL);
1982 		if (!pmc->wake_status)
1983 			return -ENOMEM;
1984 
1985 		/*
1986 		 * Initialize IRQ work for processing wake IRQs. Must use
1987 		 * HARD_IRQ variant to run in hard IRQ context on PREEMPT_RT
1988 		 * because we call generic_handle_irq() which requires hard
1989 		 * IRQ context.
1990 		 */
1991 		pmc->wake_work = IRQ_WORK_INIT_HARD(tegra186_pmc_wake_handler);
1992 	}
1993 
1994 	if (pmc->soc->init)
1995 		pmc->soc->init(pmc);
1996 
1997 	return 0;
1998 }
1999 
2000 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
2001 {
2002 	static const char disabled[] = "emergency thermal reset disabled";
2003 	u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
2004 	struct device *dev = pmc->dev;
2005 	struct device_node *np;
2006 	u32 value, checksum;
2007 
2008 	if (!pmc->soc->has_tsense_reset)
2009 		return;
2010 
2011 	np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
2012 	if (!np) {
2013 		dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
2014 		return;
2015 	}
2016 
2017 	if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
2018 		dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
2019 		goto out;
2020 	}
2021 
2022 	if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
2023 		dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
2024 		goto out;
2025 	}
2026 
2027 	if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
2028 		dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
2029 		goto out;
2030 	}
2031 
2032 	if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
2033 		dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
2034 		goto out;
2035 	}
2036 
2037 	if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
2038 		pinmux = 0;
2039 
2040 	value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
2041 	value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
2042 	tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
2043 
2044 	value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
2045 		(reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
2046 	tegra_pmc_writel(pmc, value, PMC_SCRATCH54);
2047 
2048 	value = PMC_SCRATCH55_RESET_TEGRA;
2049 	value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
2050 	value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
2051 	value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
2052 
2053 	/*
2054 	 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
2055 	 * contain the checksum and are currently zero, so they are not added.
2056 	 */
2057 	checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
2058 		+ ((value >> 24) & 0xff);
2059 	checksum &= 0xff;
2060 	checksum = 0x100 - checksum;
2061 
2062 	value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
2063 
2064 	tegra_pmc_writel(pmc, value, PMC_SCRATCH55);
2065 
2066 	value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
2067 	value |= PMC_SENSOR_CTRL_ENABLE_RST;
2068 	tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
2069 
2070 	dev_info(pmc->dev, "emergency thermal reset enabled\n");
2071 
2072 out:
2073 	of_node_put(np);
2074 }
2075 
2076 static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
2077 {
2078 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2079 
2080 	return pmc->soc->num_io_pads;
2081 }
2082 
2083 static const char *tegra_io_pad_pinctrl_get_group_name(struct pinctrl_dev *pctl,
2084 						       unsigned int group)
2085 {
2086 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl);
2087 
2088 	return pmc->soc->io_pads[group].name;
2089 }
2090 
2091 static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
2092 					       unsigned int group,
2093 					       const unsigned int **pins,
2094 					       unsigned int *num_pins)
2095 {
2096 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2097 
2098 	*pins = &pmc->soc->io_pads[group].id;
2099 	*num_pins = 1;
2100 
2101 	return 0;
2102 }
2103 
2104 static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
2105 	.get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
2106 	.get_group_name = tegra_io_pad_pinctrl_get_group_name,
2107 	.get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
2108 	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
2109 	.dt_free_map = pinconf_generic_dt_free_map,
2110 };
2111 
2112 static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
2113 				    unsigned int pin, unsigned long *config)
2114 {
2115 	enum pin_config_param param = pinconf_to_config_param(*config);
2116 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2117 	const struct tegra_io_pad_soc *pad;
2118 	int ret;
2119 	u32 arg;
2120 
2121 	pad = tegra_io_pad_find(pmc, pin);
2122 	if (!pad)
2123 		return -EINVAL;
2124 
2125 	switch (param) {
2126 	case PIN_CONFIG_POWER_SOURCE:
2127 		ret = tegra_io_pad_get_voltage(pmc, pad->id);
2128 		if (ret < 0)
2129 			return ret;
2130 
2131 		arg = ret;
2132 		break;
2133 
2134 	case PIN_CONFIG_MODE_LOW_POWER:
2135 		ret = tegra_io_pad_is_powered(pmc, pad->id);
2136 		if (ret < 0)
2137 			return ret;
2138 
2139 		arg = !ret;
2140 		break;
2141 
2142 	default:
2143 		return -EINVAL;
2144 	}
2145 
2146 	*config = pinconf_to_config_packed(param, arg);
2147 
2148 	return 0;
2149 }
2150 
2151 static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
2152 				    unsigned int pin, unsigned long *configs,
2153 				    unsigned int num_configs)
2154 {
2155 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2156 	const struct tegra_io_pad_soc *pad;
2157 	enum pin_config_param param;
2158 	unsigned int i;
2159 	int err;
2160 	u32 arg;
2161 
2162 	pad = tegra_io_pad_find(pmc, pin);
2163 	if (!pad)
2164 		return -EINVAL;
2165 
2166 	for (i = 0; i < num_configs; ++i) {
2167 		param = pinconf_to_config_param(configs[i]);
2168 		arg = pinconf_to_config_argument(configs[i]);
2169 
2170 		switch (param) {
2171 		case PIN_CONFIG_MODE_LOW_POWER:
2172 			if (arg)
2173 				err = tegra_pmc_io_pad_power_disable(pmc, pad->id);
2174 			else
2175 				err = tegra_pmc_io_pad_power_enable(pmc, pad->id);
2176 			if (err)
2177 				return err;
2178 			break;
2179 		case PIN_CONFIG_POWER_SOURCE:
2180 			if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
2181 			    arg != TEGRA_IO_PAD_VOLTAGE_3V3)
2182 				return -EINVAL;
2183 			err = tegra_io_pad_set_voltage(pmc, pad->id, arg);
2184 			if (err)
2185 				return err;
2186 			break;
2187 		default:
2188 			return -EINVAL;
2189 		}
2190 	}
2191 
2192 	return 0;
2193 }
2194 
2195 static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
2196 	.pin_config_get = tegra_io_pad_pinconf_get,
2197 	.pin_config_set = tegra_io_pad_pinconf_set,
2198 	.is_generic = true,
2199 };
2200 
2201 static struct pinctrl_desc tegra_pmc_pctl_desc = {
2202 	.pctlops = &tegra_io_pad_pinctrl_ops,
2203 	.confops = &tegra_io_pad_pinconf_ops,
2204 };
2205 
2206 static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
2207 {
2208 	int err;
2209 
2210 	if (!pmc->soc->num_pin_descs)
2211 		return 0;
2212 
2213 	tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
2214 	tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
2215 	tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
2216 
2217 	pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
2218 					      pmc);
2219 	if (IS_ERR(pmc->pctl_dev)) {
2220 		err = PTR_ERR(pmc->pctl_dev);
2221 		dev_err(pmc->dev, "failed to register pin controller: %d\n",
2222 			err);
2223 		return err;
2224 	}
2225 
2226 	return 0;
2227 }
2228 
2229 static ssize_t reset_reason_show(struct device *dev,
2230 				 struct device_attribute *attr, char *buf)
2231 {
2232 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
2233 	u32 value;
2234 
2235 	value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
2236 	value &= pmc->soc->regs->rst_source_mask;
2237 	value >>= pmc->soc->regs->rst_source_shift;
2238 
2239 	if (WARN_ON(value >= pmc->soc->num_reset_sources))
2240 		return sprintf(buf, "%s\n", "UNKNOWN");
2241 
2242 	return sprintf(buf, "%s\n", pmc->soc->reset_sources[value]);
2243 }
2244 
2245 static DEVICE_ATTR_RO(reset_reason);
2246 
2247 static ssize_t reset_level_show(struct device *dev,
2248 				struct device_attribute *attr, char *buf)
2249 {
2250 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
2251 	u32 value;
2252 
2253 	value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
2254 	value &= pmc->soc->regs->rst_level_mask;
2255 	value >>= pmc->soc->regs->rst_level_shift;
2256 
2257 	if (WARN_ON(value >= pmc->soc->num_reset_levels))
2258 		return sprintf(buf, "%s\n", "UNKNOWN");
2259 
2260 	return sprintf(buf, "%s\n", pmc->soc->reset_levels[value]);
2261 }
2262 
2263 static DEVICE_ATTR_RO(reset_level);
2264 
2265 static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
2266 {
2267 	struct device *dev = pmc->dev;
2268 	int err = 0;
2269 
2270 	if (pmc->soc->reset_sources) {
2271 		err = device_create_file(dev, &dev_attr_reset_reason);
2272 		if (err < 0)
2273 			dev_warn(dev,
2274 				 "failed to create attr \"reset_reason\": %d\n",
2275 				 err);
2276 	}
2277 
2278 	if (pmc->soc->reset_levels) {
2279 		err = device_create_file(dev, &dev_attr_reset_level);
2280 		if (err < 0)
2281 			dev_warn(dev,
2282 				 "failed to create attr \"reset_level\": %d\n",
2283 				 err);
2284 	}
2285 }
2286 
2287 static int tegra_pmc_irq_translate(struct irq_domain *domain,
2288 				   struct irq_fwspec *fwspec,
2289 				   unsigned long *hwirq,
2290 				   unsigned int *type)
2291 {
2292 	if (WARN_ON(fwspec->param_count < 2))
2293 		return -EINVAL;
2294 
2295 	*hwirq = fwspec->param[0];
2296 	*type = fwspec->param[1];
2297 
2298 	return 0;
2299 }
2300 
2301 static int tegra_pmc_irq_alloc(struct irq_domain *domain, unsigned int virq,
2302 			       unsigned int num_irqs, void *data)
2303 {
2304 	struct tegra_pmc *pmc = domain->host_data;
2305 	const struct tegra_pmc_soc *soc = pmc->soc;
2306 	struct irq_fwspec *fwspec = data;
2307 	unsigned int i;
2308 	int err = 0;
2309 
2310 	if (WARN_ON(num_irqs > 1))
2311 		return -EINVAL;
2312 
2313 	for (i = 0; i < soc->num_wake_events; i++) {
2314 		const struct tegra_wake_event *event = &soc->wake_events[i];
2315 
2316 		/* IRQ and simple wake events */
2317 		if (fwspec->param_count == 2) {
2318 			struct irq_fwspec spec;
2319 
2320 			if (event->id != fwspec->param[0])
2321 				continue;
2322 
2323 			err = irq_domain_set_hwirq_and_chip(domain, virq,
2324 							    event->id,
2325 							    &pmc->irq, pmc);
2326 			if (err < 0)
2327 				break;
2328 
2329 			/* simple hierarchies stop at the PMC level */
2330 			if (event->irq == 0) {
2331 				err = irq_domain_disconnect_hierarchy(domain->parent, virq);
2332 				break;
2333 			}
2334 
2335 			spec.fwnode = &pmc->dev->of_node->fwnode;
2336 			spec.param_count = 3;
2337 			spec.param[0] = GIC_SPI;
2338 			spec.param[1] = event->irq;
2339 			spec.param[2] = fwspec->param[1];
2340 
2341 			err = irq_domain_alloc_irqs_parent(domain, virq,
2342 							   num_irqs, &spec);
2343 
2344 			break;
2345 		}
2346 
2347 		/* GPIO wake events */
2348 		if (fwspec->param_count == 3) {
2349 			if (event->gpio.instance != fwspec->param[0] ||
2350 			    event->gpio.pin != fwspec->param[1])
2351 				continue;
2352 
2353 			err = irq_domain_set_hwirq_and_chip(domain, virq,
2354 							    event->id,
2355 							    &pmc->irq, pmc);
2356 
2357 			/* GPIO hierarchies stop at the PMC level */
2358 			if (!err && domain->parent)
2359 				err = irq_domain_disconnect_hierarchy(domain->parent,
2360 								      virq);
2361 			break;
2362 		}
2363 	}
2364 
2365 	/* If there is no wake-up event, there is no PMC mapping */
2366 	if (i == soc->num_wake_events)
2367 		err = irq_domain_disconnect_hierarchy(domain, virq);
2368 
2369 	return err;
2370 }
2371 
2372 static const struct irq_domain_ops tegra_pmc_irq_domain_ops = {
2373 	.translate = tegra_pmc_irq_translate,
2374 	.alloc = tegra_pmc_irq_alloc,
2375 };
2376 
2377 static int tegra210_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
2378 {
2379 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2380 	unsigned int offset, bit;
2381 	u32 value;
2382 
2383 	offset = data->hwirq / 32;
2384 	bit = data->hwirq % 32;
2385 
2386 	/* clear wake status */
2387 	tegra_pmc_writel(pmc, 0, PMC_SW_WAKE_STATUS);
2388 	tegra_pmc_writel(pmc, 0, PMC_SW_WAKE2_STATUS);
2389 
2390 	tegra_pmc_writel(pmc, 0, PMC_WAKE_STATUS);
2391 	tegra_pmc_writel(pmc, 0, PMC_WAKE2_STATUS);
2392 
2393 	/* enable PMC wake */
2394 	if (data->hwirq >= 32)
2395 		offset = PMC_WAKE2_MASK;
2396 	else
2397 		offset = PMC_WAKE_MASK;
2398 
2399 	value = tegra_pmc_readl(pmc, offset);
2400 
2401 	if (on)
2402 		value |= BIT(bit);
2403 	else
2404 		value &= ~BIT(bit);
2405 
2406 	tegra_pmc_writel(pmc, value, offset);
2407 
2408 	return 0;
2409 }
2410 
2411 static int tegra210_pmc_irq_set_type(struct irq_data *data, unsigned int type)
2412 {
2413 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2414 	unsigned int offset, bit;
2415 	u32 value;
2416 
2417 	offset = data->hwirq / 32;
2418 	bit = data->hwirq % 32;
2419 
2420 	if (data->hwirq >= 32)
2421 		offset = PMC_WAKE2_LEVEL;
2422 	else
2423 		offset = PMC_WAKE_LEVEL;
2424 
2425 	value = tegra_pmc_readl(pmc, offset);
2426 
2427 	switch (type) {
2428 	case IRQ_TYPE_EDGE_RISING:
2429 	case IRQ_TYPE_LEVEL_HIGH:
2430 		value |= BIT(bit);
2431 		break;
2432 
2433 	case IRQ_TYPE_EDGE_FALLING:
2434 	case IRQ_TYPE_LEVEL_LOW:
2435 		value &= ~BIT(bit);
2436 		break;
2437 
2438 	case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
2439 		value ^= BIT(bit);
2440 		break;
2441 
2442 	default:
2443 		return -EINVAL;
2444 	}
2445 
2446 	tegra_pmc_writel(pmc, value, offset);
2447 
2448 	return 0;
2449 }
2450 
2451 static void tegra186_pmc_set_wake_filters(struct tegra_pmc *pmc)
2452 {
2453 	u32 value;
2454 
2455 	/* SW Wake (wake83) needs SR_CAPTURE filter to be enabled */
2456 	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(SW_WAKE_ID));
2457 	value |= WAKE_AOWAKE_CNTRL_SR_CAPTURE_EN;
2458 	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(SW_WAKE_ID));
2459 	dev_dbg(pmc->dev, "WAKE_AOWAKE_CNTRL_83 = 0x%x\n", value);
2460 }
2461 
2462 static int tegra186_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
2463 {
2464 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2465 	unsigned int offset, bit;
2466 	u32 value;
2467 
2468 	offset = data->hwirq / 32;
2469 	bit = data->hwirq % 32;
2470 
2471 	/* clear wake status */
2472 	writel(0x1, pmc->wake + WAKE_AOWAKE_STATUS_W(pmc, data->hwirq));
2473 
2474 	/* route wake to tier 2 */
2475 	value = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(pmc, offset));
2476 
2477 	if (!on)
2478 		value &= ~(1 << bit);
2479 	else
2480 		value |= 1 << bit;
2481 
2482 	writel(value, pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(pmc, offset));
2483 
2484 	/* enable wakeup event */
2485 	writel(!!on, pmc->wake + WAKE_AOWAKE_MASK_W(pmc, data->hwirq));
2486 
2487 	return 0;
2488 }
2489 
2490 static int tegra186_pmc_irq_set_type(struct irq_data *data, unsigned int type)
2491 {
2492 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2493 	u32 value;
2494 
2495 	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
2496 
2497 	switch (type) {
2498 	case IRQ_TYPE_EDGE_RISING:
2499 	case IRQ_TYPE_LEVEL_HIGH:
2500 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
2501 		set_bit(data->hwirq, pmc->wake_type_level_map);
2502 		clear_bit(data->hwirq, pmc->wake_type_dual_edge_map);
2503 		break;
2504 
2505 	case IRQ_TYPE_EDGE_FALLING:
2506 	case IRQ_TYPE_LEVEL_LOW:
2507 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
2508 		clear_bit(data->hwirq, pmc->wake_type_level_map);
2509 		clear_bit(data->hwirq, pmc->wake_type_dual_edge_map);
2510 		break;
2511 
2512 	case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
2513 		value ^= WAKE_AOWAKE_CNTRL_LEVEL;
2514 		clear_bit(data->hwirq, pmc->wake_type_level_map);
2515 		set_bit(data->hwirq, pmc->wake_type_dual_edge_map);
2516 		break;
2517 
2518 	default:
2519 		return -EINVAL;
2520 	}
2521 
2522 	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
2523 
2524 	return 0;
2525 }
2526 
2527 static void tegra_irq_mask_parent(struct irq_data *data)
2528 {
2529 	if (data->parent_data)
2530 		irq_chip_mask_parent(data);
2531 }
2532 
2533 static void tegra_irq_unmask_parent(struct irq_data *data)
2534 {
2535 	if (data->parent_data)
2536 		irq_chip_unmask_parent(data);
2537 }
2538 
2539 static void tegra_irq_eoi_parent(struct irq_data *data)
2540 {
2541 	if (data->parent_data)
2542 		irq_chip_eoi_parent(data);
2543 }
2544 
2545 static int tegra_irq_set_affinity_parent(struct irq_data *data,
2546 					 const struct cpumask *dest,
2547 					 bool force)
2548 {
2549 	if (data->parent_data)
2550 		return irq_chip_set_affinity_parent(data, dest, force);
2551 
2552 	return -EINVAL;
2553 }
2554 
2555 static int tegra_pmc_irq_init(struct tegra_pmc *pmc)
2556 {
2557 	struct irq_domain *parent = NULL;
2558 	struct device_node *np;
2559 
2560 	np = of_irq_find_parent(pmc->dev->of_node);
2561 	if (np) {
2562 		parent = irq_find_host(np);
2563 		of_node_put(np);
2564 	}
2565 
2566 	if (!parent)
2567 		return 0;
2568 
2569 	pmc->irq.name = dev_name(pmc->dev);
2570 	pmc->irq.irq_mask = tegra_irq_mask_parent;
2571 	pmc->irq.irq_unmask = tegra_irq_unmask_parent;
2572 	pmc->irq.irq_eoi = tegra_irq_eoi_parent;
2573 	pmc->irq.irq_set_affinity = tegra_irq_set_affinity_parent;
2574 	pmc->irq.irq_set_type = pmc->soc->irq_set_type;
2575 	pmc->irq.irq_set_wake = pmc->soc->irq_set_wake;
2576 
2577 	pmc->domain = irq_domain_create_hierarchy(parent, 0, 96, dev_fwnode(pmc->dev),
2578 						  &tegra_pmc_irq_domain_ops, pmc);
2579 	if (!pmc->domain) {
2580 		dev_err(pmc->dev, "failed to allocate domain\n");
2581 		return -ENOMEM;
2582 	}
2583 
2584 	return 0;
2585 }
2586 
2587 static int tegra_pmc_clk_notify_cb(struct notifier_block *nb,
2588 				   unsigned long action, void *ptr)
2589 {
2590 	struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, clk_nb);
2591 	struct clk_notifier_data *data = ptr;
2592 
2593 	switch (action) {
2594 	case PRE_RATE_CHANGE:
2595 		mutex_lock(&pmc->powergates_lock);
2596 		break;
2597 
2598 	case POST_RATE_CHANGE:
2599 		pmc->rate = data->new_rate;
2600 		fallthrough;
2601 
2602 	case ABORT_RATE_CHANGE:
2603 		mutex_unlock(&pmc->powergates_lock);
2604 		break;
2605 
2606 	default:
2607 		WARN_ON_ONCE(1);
2608 		return notifier_from_errno(-EINVAL);
2609 	}
2610 
2611 	return NOTIFY_OK;
2612 }
2613 
2614 static void pmc_clk_fence_udelay(struct tegra_pmc *pmc, u32 offset)
2615 {
2616 	tegra_pmc_readl(pmc, offset);
2617 	/* pmc clk propagation delay 2 us */
2618 	udelay(2);
2619 }
2620 
2621 static u8 pmc_clk_mux_get_parent(struct clk_hw *hw)
2622 {
2623 	struct pmc_clk *clk = to_pmc_clk(hw);
2624 	u32 val;
2625 
2626 	val = tegra_pmc_readl(clk->pmc, clk->offs) >> clk->mux_shift;
2627 	val &= PMC_CLK_OUT_MUX_MASK;
2628 
2629 	return val;
2630 }
2631 
2632 static int pmc_clk_mux_set_parent(struct clk_hw *hw, u8 index)
2633 {
2634 	struct pmc_clk *clk = to_pmc_clk(hw);
2635 	u32 val;
2636 
2637 	val = tegra_pmc_readl(clk->pmc, clk->offs);
2638 	val &= ~(PMC_CLK_OUT_MUX_MASK << clk->mux_shift);
2639 	val |= index << clk->mux_shift;
2640 	tegra_pmc_writel(clk->pmc, val, clk->offs);
2641 	pmc_clk_fence_udelay(clk->pmc, clk->offs);
2642 
2643 	return 0;
2644 }
2645 
2646 static int pmc_clk_is_enabled(struct clk_hw *hw)
2647 {
2648 	struct pmc_clk *clk = to_pmc_clk(hw);
2649 	u32 val;
2650 
2651 	val = tegra_pmc_readl(clk->pmc, clk->offs) & BIT(clk->force_en_shift);
2652 
2653 	return val ? 1 : 0;
2654 }
2655 
2656 static void pmc_clk_set_state(struct tegra_pmc *pmc, unsigned long offs,
2657 			      u32 shift, int state)
2658 {
2659 	u32 val;
2660 
2661 	val = tegra_pmc_readl(pmc, offs);
2662 	val = state ? (val | BIT(shift)) : (val & ~BIT(shift));
2663 	tegra_pmc_writel(pmc, val, offs);
2664 	pmc_clk_fence_udelay(pmc, offs);
2665 }
2666 
2667 static int pmc_clk_enable(struct clk_hw *hw)
2668 {
2669 	struct pmc_clk *clk = to_pmc_clk(hw);
2670 
2671 	pmc_clk_set_state(clk->pmc, clk->offs, clk->force_en_shift, 1);
2672 
2673 	return 0;
2674 }
2675 
2676 static void pmc_clk_disable(struct clk_hw *hw)
2677 {
2678 	struct pmc_clk *clk = to_pmc_clk(hw);
2679 
2680 	pmc_clk_set_state(clk->pmc, clk->offs, clk->force_en_shift, 0);
2681 }
2682 
2683 static const struct clk_ops pmc_clk_ops = {
2684 	.get_parent = pmc_clk_mux_get_parent,
2685 	.set_parent = pmc_clk_mux_set_parent,
2686 	.determine_rate = __clk_mux_determine_rate,
2687 	.is_enabled = pmc_clk_is_enabled,
2688 	.enable = pmc_clk_enable,
2689 	.disable = pmc_clk_disable,
2690 };
2691 
2692 static struct clk *
2693 tegra_pmc_clk_out_register(struct tegra_pmc *pmc,
2694 			   const struct pmc_clk_init_data *data,
2695 			   unsigned long offset)
2696 {
2697 	struct clk_init_data init;
2698 	struct pmc_clk *pmc_clk;
2699 
2700 	pmc_clk = devm_kzalloc(pmc->dev, sizeof(*pmc_clk), GFP_KERNEL);
2701 	if (!pmc_clk)
2702 		return ERR_PTR(-ENOMEM);
2703 
2704 	init.name = data->name;
2705 	init.ops = &pmc_clk_ops;
2706 	init.parent_names = data->parents;
2707 	init.num_parents = data->num_parents;
2708 	init.flags = CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT |
2709 		     CLK_SET_PARENT_GATE;
2710 
2711 	pmc_clk->hw.init = &init;
2712 	pmc_clk->pmc = pmc;
2713 	pmc_clk->offs = offset;
2714 	pmc_clk->mux_shift = data->mux_shift;
2715 	pmc_clk->force_en_shift = data->force_en_shift;
2716 
2717 	return clk_register(NULL, &pmc_clk->hw);
2718 }
2719 
2720 static int pmc_clk_gate_is_enabled(struct clk_hw *hw)
2721 {
2722 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2723 	u32 value = tegra_pmc_readl(gate->pmc, gate->offs);
2724 
2725 	return value & BIT(gate->shift) ? 1 : 0;
2726 }
2727 
2728 static int pmc_clk_gate_enable(struct clk_hw *hw)
2729 {
2730 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2731 
2732 	pmc_clk_set_state(gate->pmc, gate->offs, gate->shift, 1);
2733 
2734 	return 0;
2735 }
2736 
2737 static void pmc_clk_gate_disable(struct clk_hw *hw)
2738 {
2739 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2740 
2741 	pmc_clk_set_state(gate->pmc, gate->offs, gate->shift, 0);
2742 }
2743 
2744 static const struct clk_ops pmc_clk_gate_ops = {
2745 	.is_enabled = pmc_clk_gate_is_enabled,
2746 	.enable = pmc_clk_gate_enable,
2747 	.disable = pmc_clk_gate_disable,
2748 };
2749 
2750 static struct clk *
2751 tegra_pmc_clk_gate_register(struct tegra_pmc *pmc, const char *name,
2752 			    const char *parent_name, unsigned long offset,
2753 			    u32 shift)
2754 {
2755 	struct clk_init_data init;
2756 	struct pmc_clk_gate *gate;
2757 
2758 	gate = devm_kzalloc(pmc->dev, sizeof(*gate), GFP_KERNEL);
2759 	if (!gate)
2760 		return ERR_PTR(-ENOMEM);
2761 
2762 	init.name = name;
2763 	init.ops = &pmc_clk_gate_ops;
2764 	init.parent_names = &parent_name;
2765 	init.num_parents = 1;
2766 	init.flags = 0;
2767 
2768 	gate->hw.init = &init;
2769 	gate->pmc = pmc;
2770 	gate->offs = offset;
2771 	gate->shift = shift;
2772 
2773 	return clk_register(NULL, &gate->hw);
2774 }
2775 
2776 static void tegra_pmc_clock_register(struct tegra_pmc *pmc,
2777 				     struct device_node *np)
2778 {
2779 	struct clk *clk;
2780 	struct clk_onecell_data *clk_data;
2781 	unsigned int num_clks;
2782 	int i, err;
2783 
2784 	num_clks = pmc->soc->num_pmc_clks;
2785 	if (pmc->soc->has_blink_output)
2786 		num_clks += 1;
2787 
2788 	if (!num_clks)
2789 		return;
2790 
2791 	clk_data = devm_kmalloc(pmc->dev, sizeof(*clk_data), GFP_KERNEL);
2792 	if (!clk_data)
2793 		return;
2794 
2795 	clk_data->clks = devm_kcalloc(pmc->dev, TEGRA_PMC_CLK_MAX,
2796 				      sizeof(*clk_data->clks), GFP_KERNEL);
2797 	if (!clk_data->clks)
2798 		return;
2799 
2800 	clk_data->clk_num = TEGRA_PMC_CLK_MAX;
2801 
2802 	for (i = 0; i < TEGRA_PMC_CLK_MAX; i++)
2803 		clk_data->clks[i] = ERR_PTR(-ENOENT);
2804 
2805 	for (i = 0; i < pmc->soc->num_pmc_clks; i++) {
2806 		const struct pmc_clk_init_data *data;
2807 
2808 		data = pmc->soc->pmc_clks_data + i;
2809 
2810 		clk = tegra_pmc_clk_out_register(pmc, data, PMC_CLK_OUT_CNTRL);
2811 		if (IS_ERR(clk)) {
2812 			dev_warn(pmc->dev, "unable to register clock %s: %d\n",
2813 				 data->name, PTR_ERR_OR_ZERO(clk));
2814 			return;
2815 		}
2816 
2817 		err = clk_register_clkdev(clk, data->name, NULL);
2818 		if (err) {
2819 			dev_warn(pmc->dev,
2820 				 "unable to register %s clock lookup: %d\n",
2821 				 data->name, err);
2822 			return;
2823 		}
2824 
2825 		clk_data->clks[data->clk_id] = clk;
2826 	}
2827 
2828 	if (pmc->soc->has_blink_output) {
2829 		tegra_pmc_writel(pmc, 0x0, PMC_BLINK_TIMER);
2830 		clk = tegra_pmc_clk_gate_register(pmc,
2831 						  "pmc_blink_override",
2832 						  "clk_32k",
2833 						  PMC_DPD_PADS_ORIDE,
2834 						  PMC_DPD_PADS_ORIDE_BLINK);
2835 		if (IS_ERR(clk)) {
2836 			dev_warn(pmc->dev,
2837 				 "unable to register pmc_blink_override: %d\n",
2838 				 PTR_ERR_OR_ZERO(clk));
2839 			return;
2840 		}
2841 
2842 		clk = tegra_pmc_clk_gate_register(pmc, "pmc_blink",
2843 						  "pmc_blink_override",
2844 						  PMC_CNTRL,
2845 						  PMC_CNTRL_BLINK_EN);
2846 		if (IS_ERR(clk)) {
2847 			dev_warn(pmc->dev,
2848 				 "unable to register pmc_blink: %d\n",
2849 				 PTR_ERR_OR_ZERO(clk));
2850 			return;
2851 		}
2852 
2853 		err = clk_register_clkdev(clk, "pmc_blink", NULL);
2854 		if (err) {
2855 			dev_warn(pmc->dev,
2856 				 "unable to register pmc_blink lookup: %d\n",
2857 				 err);
2858 			return;
2859 		}
2860 
2861 		clk_data->clks[TEGRA_PMC_CLK_BLINK] = clk;
2862 	}
2863 
2864 	err = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
2865 	if (err)
2866 		dev_warn(pmc->dev, "failed to add pmc clock provider: %d\n",
2867 			 err);
2868 }
2869 
2870 static const struct regmap_range pmc_usb_sleepwalk_ranges[] = {
2871 	regmap_reg_range(PMC_USB_DEBOUNCE_DEL, PMC_USB_AO),
2872 	regmap_reg_range(PMC_UTMIP_UHSIC_TRIGGERS, PMC_UTMIP_UHSIC_SAVED_STATE),
2873 	regmap_reg_range(PMC_UTMIP_TERM_PAD_CFG, PMC_UTMIP_UHSIC_FAKE),
2874 	regmap_reg_range(PMC_UTMIP_UHSIC_LINE_WAKEUP, PMC_UTMIP_UHSIC_LINE_WAKEUP),
2875 	regmap_reg_range(PMC_UTMIP_BIAS_MASTER_CNTRL, PMC_UTMIP_MASTER_CONFIG),
2876 	regmap_reg_range(PMC_UTMIP_UHSIC2_TRIGGERS, PMC_UTMIP_MASTER2_CONFIG),
2877 	regmap_reg_range(PMC_UTMIP_PAD_CFG0, PMC_UTMIP_UHSIC_SLEEP_CFG1),
2878 	regmap_reg_range(PMC_UTMIP_SLEEPWALK_P3, PMC_UTMIP_SLEEPWALK_P3),
2879 };
2880 
2881 static const struct regmap_access_table pmc_usb_sleepwalk_table = {
2882 	.yes_ranges = pmc_usb_sleepwalk_ranges,
2883 	.n_yes_ranges = ARRAY_SIZE(pmc_usb_sleepwalk_ranges),
2884 };
2885 
2886 static int tegra_pmc_regmap_readl(void *context, unsigned int offset, unsigned int *value)
2887 {
2888 	struct tegra_pmc *pmc = context;
2889 
2890 	*value = tegra_pmc_readl(pmc, offset);
2891 	return 0;
2892 }
2893 
2894 static int tegra_pmc_regmap_writel(void *context, unsigned int offset, unsigned int value)
2895 {
2896 	struct tegra_pmc *pmc = context;
2897 
2898 	tegra_pmc_writel(pmc, value, offset);
2899 	return 0;
2900 }
2901 
2902 static const struct regmap_config usb_sleepwalk_regmap_config = {
2903 	.name = "usb_sleepwalk",
2904 	.reg_bits = 32,
2905 	.val_bits = 32,
2906 	.reg_stride = 4,
2907 	.fast_io = true,
2908 	.rd_table = &pmc_usb_sleepwalk_table,
2909 	.wr_table = &pmc_usb_sleepwalk_table,
2910 	.reg_read = tegra_pmc_regmap_readl,
2911 	.reg_write = tegra_pmc_regmap_writel,
2912 };
2913 
2914 static int tegra_pmc_regmap_init(struct tegra_pmc *pmc)
2915 {
2916 	struct regmap *regmap;
2917 	int err;
2918 
2919 	if (pmc->soc->has_usb_sleepwalk) {
2920 		regmap = devm_regmap_init(pmc->dev, NULL, pmc, &usb_sleepwalk_regmap_config);
2921 		if (IS_ERR(regmap)) {
2922 			err = PTR_ERR(regmap);
2923 			dev_err(pmc->dev, "failed to allocate register map (%d)\n", err);
2924 			return err;
2925 		}
2926 	}
2927 
2928 	return 0;
2929 }
2930 
2931 static bool tegra_pmc_detect_tz_only(struct tegra_pmc *pmc)
2932 {
2933 	u32 value, saved;
2934 
2935 	saved = readl(pmc->base + pmc->soc->regs->scratch0);
2936 	value = saved ^ 0xffffffff;
2937 
2938 	if (value == 0xffffffff)
2939 		value = 0xdeadbeef;
2940 
2941 	/* write pattern and read it back */
2942 	writel(value, pmc->base + pmc->soc->regs->scratch0);
2943 	value = readl(pmc->base + pmc->soc->regs->scratch0);
2944 
2945 	/* if we read all-zeroes, access is restricted to TZ only */
2946 	if (value == 0) {
2947 		pr_info("access to PMC is restricted to TZ\n");
2948 		return true;
2949 	}
2950 
2951 	/* restore original value */
2952 	writel(saved, pmc->base + pmc->soc->regs->scratch0);
2953 
2954 	return false;
2955 }
2956 
2957 static void tegra_pmc_init_common(struct tegra_pmc *pmc, struct device_node *np)
2958 {
2959 	unsigned int i;
2960 
2961 	if (pmc->soc->maybe_tz_only)
2962 		pmc->tz_only = tegra_pmc_detect_tz_only(pmc);
2963 
2964 	/* Create a bitmap of the available and valid partitions */
2965 	for (i = 0; i < pmc->soc->num_powergates; i++)
2966 		if (pmc->soc->powergates[i])
2967 			set_bit(i, pmc->powergates_available);
2968 
2969 	/*
2970 	 * Invert the interrupt polarity if a PMC device tree node exists and
2971 	 * contains the nvidia,invert-interrupt property.
2972 	 */
2973 	pmc->soc->setup_irq_polarity(pmc, np);
2974 }
2975 
2976 static void tegra_pmc_reset_suspend_mode(void *data)
2977 {
2978 	struct tegra_pmc *pmc = data;
2979 
2980 	pmc->suspend_mode = TEGRA_SUSPEND_NOT_READY;
2981 }
2982 
2983 static int tegra_pmc_probe(struct platform_device *pdev)
2984 {
2985 	struct tegra_pmc *pmc;
2986 	struct resource *res;
2987 	int err;
2988 
2989 #if defined(CONFIG_ARM)
2990 	/*
2991 	 * Early initialisation should have configured an initial
2992 	 * register mapping and setup the soc data pointer. If these
2993 	 * are not valid then something went badly wrong!
2994 	 */
2995 	if (WARN_ON(!early_pmc->base || !early_pmc->soc))
2996 		return -ENODEV;
2997 #endif
2998 
2999 	pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
3000 	if (!pmc)
3001 		return -ENOMEM;
3002 
3003 	pmc->soc = device_get_match_data(&pdev->dev);
3004 	mutex_init(&pmc->powergates_lock);
3005 	pmc->dev = &pdev->dev;
3006 
3007 	err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
3008 	if (err < 0)
3009 		return dev_err_probe(&pdev->dev, err, "failed to parse DT\n");
3010 
3011 	err = devm_add_action_or_reset(&pdev->dev, tegra_pmc_reset_suspend_mode,
3012 				       pmc);
3013 	if (err)
3014 		return err;
3015 
3016 	/* take over the memory region from the early initialization */
3017 	pmc->base = devm_platform_ioremap_resource(pdev, 0);
3018 	if (IS_ERR(pmc->base))
3019 		return PTR_ERR(pmc->base);
3020 
3021 	if (pmc->soc->has_single_mmio_aperture) {
3022 		pmc->wake = pmc->base;
3023 		pmc->aotag = pmc->base;
3024 		pmc->scratch = pmc->base;
3025 	} else {
3026 		pmc->wake = devm_platform_ioremap_resource_byname(pdev, "wake");
3027 		if (IS_ERR(pmc->wake))
3028 			return PTR_ERR(pmc->wake);
3029 
3030 		/* "aotag" is an optional aperture */
3031 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3032 						   "aotag");
3033 		if (res) {
3034 			pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
3035 			if (IS_ERR(pmc->aotag))
3036 				return PTR_ERR(pmc->aotag);
3037 		} else {
3038 			pmc->aotag = NULL;
3039 		}
3040 
3041 		/* "scratch" is an optional aperture */
3042 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3043 						   "scratch");
3044 		if (res) {
3045 			pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
3046 			if (IS_ERR(pmc->scratch))
3047 				return PTR_ERR(pmc->scratch);
3048 		} else {
3049 			pmc->scratch = NULL;
3050 		}
3051 	}
3052 
3053 	pmc->clk = devm_clk_get_optional(&pdev->dev, "pclk");
3054 	if (IS_ERR(pmc->clk))
3055 		return dev_err_probe(&pdev->dev, PTR_ERR(pmc->clk),
3056 				     "failed to get pclk\n");
3057 
3058 	tegra_pmc_init_common(pmc, pdev->dev.of_node);
3059 
3060 	/*
3061 	 * PMC should be last resort for restarting since it soft-resets
3062 	 * CPU without resetting everything else.
3063 	 */
3064 	if (pmc->scratch) {
3065 		pmc->reboot_notifier.notifier_call = tegra_pmc_reboot_notify;
3066 
3067 		err = devm_register_reboot_notifier(&pdev->dev,
3068 						    &pmc->reboot_notifier);
3069 		if (err) {
3070 			dev_err(&pdev->dev,
3071 				"unable to register reboot notifier, %d\n",
3072 				err);
3073 			return err;
3074 		}
3075 	}
3076 
3077 	err = devm_register_sys_off_handler(&pdev->dev,
3078 					    SYS_OFF_MODE_RESTART,
3079 					    SYS_OFF_PRIO_LOW,
3080 					    tegra_pmc_restart_handler,
3081 					    pmc);
3082 	if (err) {
3083 		dev_err(&pdev->dev, "failed to register sys-off handler: %d\n",
3084 			err);
3085 		return err;
3086 	}
3087 
3088 	/*
3089 	 * PMC should be primary power-off method on Grouper if it soft-resets
3090 	 * CPU, asking bootloader to shutdown hardware.
3091 	 */
3092 	if (of_machine_is_compatible("asus,grouper")) {
3093 		err = devm_register_sys_off_handler(&pdev->dev,
3094 						    SYS_OFF_MODE_POWER_OFF,
3095 						    SYS_OFF_PRIO_FIRMWARE,
3096 						    tegra_pmc_grouper_power_off_handler,
3097 						    pmc);
3098 		if (err) {
3099 			dev_err(&pdev->dev, "failed to register sys-off handler: %d\n",
3100 				err);
3101 			return err;
3102 		}
3103 	}
3104 
3105 	/*
3106 	 * PCLK clock rate can't be retrieved using CLK API because it
3107 	 * causes lockup if CPU enters LP2 idle state from some other
3108 	 * CLK notifier, hence we're caching the rate's value locally.
3109 	 */
3110 	if (pmc->clk) {
3111 		pmc->clk_nb.notifier_call = tegra_pmc_clk_notify_cb;
3112 		err = devm_clk_notifier_register(&pdev->dev, pmc->clk,
3113 						 &pmc->clk_nb);
3114 		if (err) {
3115 			dev_err(&pdev->dev,
3116 				"failed to register clk notifier\n");
3117 			return err;
3118 		}
3119 
3120 		pmc->rate = clk_get_rate(pmc->clk);
3121 	}
3122 
3123 	err = tegra_pmc_init(pmc);
3124 	if (err < 0) {
3125 		dev_err(&pdev->dev, "failed to initialize PMC: %d\n", err);
3126 		return err;
3127 	}
3128 
3129 	tegra_pmc_init_tsense_reset(pmc);
3130 
3131 	tegra_pmc_reset_sysfs_init(pmc);
3132 
3133 	err = tegra_pmc_pinctrl_init(pmc);
3134 	if (err)
3135 		goto cleanup_sysfs;
3136 
3137 	err = tegra_pmc_regmap_init(pmc);
3138 	if (err < 0)
3139 		goto cleanup_sysfs;
3140 
3141 	err = tegra_powergate_init(pmc, pdev->dev.of_node);
3142 	if (err < 0)
3143 		goto cleanup_powergates;
3144 
3145 	err = tegra_pmc_irq_init(pmc);
3146 	if (err < 0)
3147 		goto cleanup_powergates;
3148 
3149 #if defined(CONFIG_ARM)
3150 	mutex_lock(&early_pmc->powergates_lock);
3151 	iounmap(early_pmc->base);
3152 	early_pmc->base = pmc->base;
3153 	mutex_unlock(&early_pmc->powergates_lock);
3154 #endif
3155 
3156 	tegra_pmc_clock_register(pmc, pdev->dev.of_node);
3157 	platform_set_drvdata(pdev, pmc);
3158 	tegra_pm_init_suspend();
3159 
3160 	/* Some wakes require specific filter configuration */
3161 	if (pmc->soc->set_wake_filters)
3162 		pmc->soc->set_wake_filters(pmc);
3163 
3164 	if (pmc->soc->num_powergates)
3165 		debugfs_create_file("powergate", 0444, NULL, pmc, &powergate_fops);
3166 
3167 	return 0;
3168 
3169 cleanup_powergates:
3170 	tegra_powergate_remove_all(pdev->dev.of_node);
3171 cleanup_sysfs:
3172 	device_remove_file(&pdev->dev, &dev_attr_reset_reason);
3173 	device_remove_file(&pdev->dev, &dev_attr_reset_level);
3174 
3175 	return err;
3176 }
3177 
3178 /*
3179  * Ensures that sufficient time is passed for a register write to
3180  * serialize into the 32KHz domain.
3181  */
3182 static void wke_32kwritel(struct tegra_pmc *pmc, u32 value, unsigned int offset)
3183 {
3184 	writel(value, pmc->wake + offset);
3185 	udelay(130);
3186 }
3187 
3188 static void wke_write_wake_level(struct tegra_pmc *pmc, int wake, int level)
3189 {
3190 	unsigned int offset = WAKE_AOWAKE_CNTRL(wake);
3191 	u32 value;
3192 
3193 	value = readl(pmc->wake + offset);
3194 	if (level)
3195 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
3196 	else
3197 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
3198 
3199 	writel(value, pmc->wake + offset);
3200 }
3201 
3202 static void wke_write_wake_levels(struct tegra_pmc *pmc)
3203 {
3204 	unsigned int i;
3205 
3206 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3207 		wke_write_wake_level(pmc, i, test_bit(i, pmc->wake_cntrl_level_map));
3208 }
3209 
3210 static void wke_clear_sw_wake_status(struct tegra_pmc *pmc)
3211 {
3212 	wke_32kwritel(pmc, 1, pmc->soc->regs->aowake_sw_status_w);
3213 }
3214 
3215 static void wke_read_sw_wake_status(struct tegra_pmc *pmc)
3216 {
3217 	unsigned long status;
3218 	unsigned int wake, i;
3219 
3220 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3221 		wke_write_wake_level(pmc, i, 0);
3222 
3223 	wke_clear_sw_wake_status(pmc);
3224 
3225 	wke_32kwritel(pmc, 1, pmc->soc->regs->aowake_latch_sw);
3226 
3227 	/*
3228 	 * WAKE_AOWAKE_SW_STATUS is edge triggered, so in order to
3229 	 * obtain the current status of the input wake signals, change
3230 	 * the polarity of the wake level from 0->1 while latching to force
3231 	 * a positive edge if the sampled signal is '1'.
3232 	 */
3233 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3234 		wke_write_wake_level(pmc, i, 1);
3235 
3236 	/*
3237 	 * Wait for the update to be synced into the 32kHz domain,
3238 	 * and let enough time lapse, so that the wake signals have time to
3239 	 * be sampled.
3240 	 */
3241 	udelay(300);
3242 
3243 	wke_32kwritel(pmc, 0, pmc->soc->regs->aowake_latch_sw);
3244 
3245 	bitmap_zero(pmc->wake_sw_status_map, pmc->soc->max_wake_events);
3246 
3247 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3248 		status = readl(pmc->wake + WAKE_AOWAKE_SW_STATUS(pmc, i));
3249 
3250 		for_each_set_bit(wake, &status, 32)
3251 			set_bit(wake + (i * 32), pmc->wake_sw_status_map);
3252 	}
3253 }
3254 
3255 static void wke_clear_wake_status(struct tegra_pmc *pmc)
3256 {
3257 	unsigned long status;
3258 	unsigned int i, wake;
3259 	u32 mask;
3260 
3261 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3262 		mask = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(pmc, i));
3263 		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(pmc, i)) & mask;
3264 
3265 		for_each_set_bit(wake, &status, 32)
3266 			wke_32kwritel(pmc, 0x1, WAKE_AOWAKE_STATUS_W(pmc,
3267 							(i * 32) + wake));
3268 	}
3269 }
3270 
3271 static void tegra186_pmc_wake_syscore_resume(void *data)
3272 {
3273 	struct tegra_pmc *pmc = data;
3274 	unsigned int i;
3275 	u32 mask;
3276 
3277 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3278 		mask = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(pmc, i));
3279 		pmc->wake_status[i] = readl(pmc->wake +
3280 					    WAKE_AOWAKE_STATUS_R(pmc, i)) & mask;
3281 	}
3282 
3283 	/* Schedule IRQ work to process wake IRQs (if any) */
3284 	irq_work_queue(&pmc->wake_work);
3285 }
3286 
3287 static int tegra186_pmc_wake_syscore_suspend(void *data)
3288 {
3289 	struct tegra_pmc *pmc = data;
3290 	unsigned int i;
3291 
3292 	/* Check if there are unhandled wake IRQs */
3293 	for (i = 0; i < pmc->soc->max_wake_vectors; i++)
3294 		if (pmc->wake_status[i])
3295 			dev_warn(pmc->dev,
3296 				 "Unhandled wake IRQs pending vector[%u]: 0x%x\n",
3297 				 i, pmc->wake_status[i]);
3298 
3299 	wke_read_sw_wake_status(pmc);
3300 
3301 	/* flip the wakeup trigger for dual-edge triggered pads
3302 	 * which are currently asserting as wakeups
3303 	 */
3304 	bitmap_andnot(pmc->wake_cntrl_level_map, pmc->wake_type_dual_edge_map,
3305 		      pmc->wake_sw_status_map, pmc->soc->max_wake_events);
3306 	bitmap_or(pmc->wake_cntrl_level_map, pmc->wake_cntrl_level_map,
3307 		  pmc->wake_type_level_map, pmc->soc->max_wake_events);
3308 
3309 	/* Clear PMC Wake Status registers while going to suspend */
3310 	wke_clear_wake_status(pmc);
3311 	wke_write_wake_levels(pmc);
3312 
3313 	return 0;
3314 }
3315 
3316 static const struct syscore_ops tegra186_pmc_wake_syscore_ops = {
3317 	.suspend = tegra186_pmc_wake_syscore_suspend,
3318 	.resume = tegra186_pmc_wake_syscore_resume,
3319 };
3320 
3321 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
3322 static int tegra_pmc_suspend(struct device *dev)
3323 {
3324 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
3325 
3326 	tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);
3327 
3328 	return 0;
3329 }
3330 
3331 static int tegra_pmc_resume(struct device *dev)
3332 {
3333 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
3334 
3335 	tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
3336 
3337 	return 0;
3338 }
3339 
3340 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
3341 
3342 #endif
3343 
3344 static const char * const tegra20_powergates[] = {
3345 	[TEGRA_POWERGATE_CPU] = "cpu",
3346 	[TEGRA_POWERGATE_3D] = "td",
3347 	[TEGRA_POWERGATE_VENC] = "venc",
3348 	[TEGRA_POWERGATE_VDEC] = "vdec",
3349 	[TEGRA_POWERGATE_PCIE] = "pcie",
3350 	[TEGRA_POWERGATE_L2] = "l2",
3351 	[TEGRA_POWERGATE_MPE] = "mpe",
3352 };
3353 
3354 static const struct tegra_pmc_regs tegra20_pmc_regs = {
3355 	.scratch0 = 0x50,
3356 	.rst_status = 0x1b4,
3357 	.rst_source_shift = 0x0,
3358 	.rst_source_mask = 0x7,
3359 	.rst_level_shift = 0x0,
3360 	.rst_level_mask = 0x0,
3361 };
3362 
3363 static void tegra20_pmc_init(struct tegra_pmc *pmc)
3364 {
3365 	u32 value, osc, pmu, off;
3366 
3367 	/* Always enable CPU power request */
3368 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3369 	value |= PMC_CNTRL_CPU_PWRREQ_OE;
3370 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3371 
3372 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3373 
3374 	if (pmc->sysclkreq_high)
3375 		value &= ~PMC_CNTRL_SYSCLK_POLARITY;
3376 	else
3377 		value |= PMC_CNTRL_SYSCLK_POLARITY;
3378 
3379 	if (pmc->corereq_high)
3380 		value &= ~PMC_CNTRL_PWRREQ_POLARITY;
3381 	else
3382 		value |= PMC_CNTRL_PWRREQ_POLARITY;
3383 
3384 	/* configure the output polarity while the request is tristated */
3385 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3386 
3387 	/* now enable the request */
3388 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3389 	value |= PMC_CNTRL_SYSCLK_OE;
3390 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3391 
3392 	/* program core timings which are applicable only for suspend state */
3393 	if (pmc->suspend_mode != TEGRA_SUSPEND_NONE) {
3394 		osc = DIV_ROUND_UP(pmc->core_osc_time * 8192, 1000000);
3395 		pmu = DIV_ROUND_UP(pmc->core_pmu_time * 32768, 1000000);
3396 		off = DIV_ROUND_UP(pmc->core_off_time * 32768, 1000000);
3397 		tegra_pmc_writel(pmc, ((osc << 8) & 0xff00) | (pmu & 0xff),
3398 				 PMC_COREPWRGOOD_TIMER);
3399 		tegra_pmc_writel(pmc, off, PMC_COREPWROFF_TIMER);
3400 	}
3401 }
3402 
3403 static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
3404 					   struct device_node *np)
3405 {
3406 	u32 value;
3407 
3408 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3409 
3410 	if (pmc->invert_irq)
3411 		value |= PMC_CNTRL_INTR_POLARITY;
3412 	else
3413 		value &= ~PMC_CNTRL_INTR_POLARITY;
3414 
3415 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3416 }
3417 
3418 static const struct tegra_pmc_soc tegra20_pmc_soc = {
3419 	.supports_core_domain = true,
3420 	.num_powergates = ARRAY_SIZE(tegra20_powergates),
3421 	.powergates = tegra20_powergates,
3422 	.num_cpu_powergates = 0,
3423 	.cpu_powergates = NULL,
3424 	.has_tsense_reset = false,
3425 	.has_gpu_clamps = false,
3426 	.needs_mbist_war = false,
3427 	.has_io_pad_wren = true,
3428 	.maybe_tz_only = false,
3429 	.num_io_pads = 0,
3430 	.io_pads = NULL,
3431 	.num_pin_descs = 0,
3432 	.pin_descs = NULL,
3433 	.regs = &tegra20_pmc_regs,
3434 	.init = tegra20_pmc_init,
3435 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3436 	.powergate_set = tegra20_powergate_set,
3437 	.reset_sources = NULL,
3438 	.num_reset_sources = 0,
3439 	.reset_levels = NULL,
3440 	.num_reset_levels = 0,
3441 	.pmc_clks_data = NULL,
3442 	.num_pmc_clks = 0,
3443 	.has_blink_output = true,
3444 	.has_usb_sleepwalk = true,
3445 	.has_single_mmio_aperture = true,
3446 };
3447 
3448 static const char * const tegra30_powergates[] = {
3449 	[TEGRA_POWERGATE_CPU] = "cpu0",
3450 	[TEGRA_POWERGATE_3D] = "td",
3451 	[TEGRA_POWERGATE_VENC] = "venc",
3452 	[TEGRA_POWERGATE_VDEC] = "vdec",
3453 	[TEGRA_POWERGATE_PCIE] = "pcie",
3454 	[TEGRA_POWERGATE_L2] = "l2",
3455 	[TEGRA_POWERGATE_MPE] = "mpe",
3456 	[TEGRA_POWERGATE_HEG] = "heg",
3457 	[TEGRA_POWERGATE_SATA] = "sata",
3458 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3459 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3460 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3461 	[TEGRA_POWERGATE_CELP] = "celp",
3462 	[TEGRA_POWERGATE_3D1] = "td2",
3463 };
3464 
3465 static const u8 tegra30_cpu_powergates[] = {
3466 	TEGRA_POWERGATE_CPU,
3467 	TEGRA_POWERGATE_CPU1,
3468 	TEGRA_POWERGATE_CPU2,
3469 	TEGRA_POWERGATE_CPU3,
3470 };
3471 
3472 static const char * const tegra30_reset_sources[] = {
3473 	"POWER_ON_RESET",
3474 	"WATCHDOG",
3475 	"SENSOR",
3476 	"SW_MAIN",
3477 	"LP0"
3478 };
3479 
3480 static const struct tegra_pmc_soc tegra30_pmc_soc = {
3481 	.supports_core_domain = true,
3482 	.num_powergates = ARRAY_SIZE(tegra30_powergates),
3483 	.powergates = tegra30_powergates,
3484 	.num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
3485 	.cpu_powergates = tegra30_cpu_powergates,
3486 	.has_tsense_reset = true,
3487 	.has_gpu_clamps = false,
3488 	.needs_mbist_war = false,
3489 	.has_io_pad_wren = true,
3490 	.maybe_tz_only = false,
3491 	.num_io_pads = 0,
3492 	.io_pads = NULL,
3493 	.num_pin_descs = 0,
3494 	.pin_descs = NULL,
3495 	.regs = &tegra20_pmc_regs,
3496 	.init = tegra20_pmc_init,
3497 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3498 	.powergate_set = tegra20_powergate_set,
3499 	.reset_sources = tegra30_reset_sources,
3500 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3501 	.reset_levels = NULL,
3502 	.num_reset_levels = 0,
3503 	.pmc_clks_data = tegra_pmc_clks_data,
3504 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3505 	.has_blink_output = true,
3506 	.has_usb_sleepwalk = true,
3507 	.has_single_mmio_aperture = true,
3508 };
3509 
3510 static const char * const tegra114_powergates[] = {
3511 	[TEGRA_POWERGATE_CPU] = "crail",
3512 	[TEGRA_POWERGATE_3D] = "td",
3513 	[TEGRA_POWERGATE_VENC] = "venc",
3514 	[TEGRA_POWERGATE_VDEC] = "vdec",
3515 	[TEGRA_POWERGATE_MPE] = "mpe",
3516 	[TEGRA_POWERGATE_HEG] = "heg",
3517 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3518 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3519 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3520 	[TEGRA_POWERGATE_CELP] = "celp",
3521 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3522 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3523 	[TEGRA_POWERGATE_C1NC] = "c1nc",
3524 	[TEGRA_POWERGATE_DIS] = "dis",
3525 	[TEGRA_POWERGATE_DISB] = "disb",
3526 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3527 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3528 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3529 };
3530 
3531 static const u8 tegra114_cpu_powergates[] = {
3532 	TEGRA_POWERGATE_CPU0,
3533 	TEGRA_POWERGATE_CPU1,
3534 	TEGRA_POWERGATE_CPU2,
3535 	TEGRA_POWERGATE_CPU3,
3536 };
3537 
3538 static const struct tegra_pmc_soc tegra114_pmc_soc = {
3539 	.supports_core_domain = true,
3540 	.num_powergates = ARRAY_SIZE(tegra114_powergates),
3541 	.powergates = tegra114_powergates,
3542 	.num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
3543 	.cpu_powergates = tegra114_cpu_powergates,
3544 	.has_tsense_reset = true,
3545 	.has_gpu_clamps = false,
3546 	.needs_mbist_war = false,
3547 	.has_io_pad_wren = true,
3548 	.maybe_tz_only = false,
3549 	.num_io_pads = 0,
3550 	.io_pads = NULL,
3551 	.num_pin_descs = 0,
3552 	.pin_descs = NULL,
3553 	.regs = &tegra20_pmc_regs,
3554 	.init = tegra20_pmc_init,
3555 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3556 	.powergate_set = tegra114_powergate_set,
3557 	.reset_sources = tegra30_reset_sources,
3558 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3559 	.reset_levels = NULL,
3560 	.num_reset_levels = 0,
3561 	.pmc_clks_data = tegra_pmc_clks_data,
3562 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3563 	.has_blink_output = true,
3564 	.has_usb_sleepwalk = true,
3565 	.has_single_mmio_aperture = true,
3566 };
3567 
3568 static const char * const tegra124_powergates[] = {
3569 	[TEGRA_POWERGATE_CPU] = "crail",
3570 	[TEGRA_POWERGATE_3D] = "3d",
3571 	[TEGRA_POWERGATE_VENC] = "venc",
3572 	[TEGRA_POWERGATE_PCIE] = "pcie",
3573 	[TEGRA_POWERGATE_VDEC] = "vdec",
3574 	[TEGRA_POWERGATE_MPE] = "mpe",
3575 	[TEGRA_POWERGATE_HEG] = "heg",
3576 	[TEGRA_POWERGATE_SATA] = "sata",
3577 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3578 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3579 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3580 	[TEGRA_POWERGATE_CELP] = "celp",
3581 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3582 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3583 	[TEGRA_POWERGATE_C1NC] = "c1nc",
3584 	[TEGRA_POWERGATE_SOR] = "sor",
3585 	[TEGRA_POWERGATE_DIS] = "dis",
3586 	[TEGRA_POWERGATE_DISB] = "disb",
3587 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3588 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3589 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3590 	[TEGRA_POWERGATE_VIC] = "vic",
3591 	[TEGRA_POWERGATE_IRAM] = "iram",
3592 };
3593 
3594 static const u8 tegra124_cpu_powergates[] = {
3595 	TEGRA_POWERGATE_CPU0,
3596 	TEGRA_POWERGATE_CPU1,
3597 	TEGRA_POWERGATE_CPU2,
3598 	TEGRA_POWERGATE_CPU3,
3599 };
3600 
3601 #define TEGRA_IO_PAD(_id, _dpd, _request, _status, _name)	\
3602 	((struct tegra_io_pad_soc) {					\
3603 		.id		= (_id),				\
3604 		.dpd		= (_dpd),				\
3605 		.request	= (_request),				\
3606 		.status		= (_status),				\
3607 		.name		= (_name),				\
3608 	})
3609 
3610 #define TEGRA_IO_PAD_VCTRL(_id, _offset, _ena_3v3)			\
3611 	((struct tegra_io_pad_vctrl) {					\
3612 		.id		= (_id),				\
3613 		.offset		= (_offset),				\
3614 		.ena_3v3	= (_ena_3v3),				\
3615 		.ena_1v8	= 0,					\
3616 	})
3617 
3618 #define TEGRA_IO_PIN_DESC(_id, _name)	\
3619 	((struct pinctrl_pin_desc) {	\
3620 		.number	= (_id),	\
3621 		.name	= (_name),	\
3622 	})
3623 
3624 static const struct tegra_io_pad_soc tegra124_io_pads[] = {
3625 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x1b8, 0x1bc, "audio"),
3626 	TEGRA_IO_PAD(TEGRA_IO_PAD_BB, 15, 0x1b8, 0x1bc, "bb"),
3627 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 4, 0x1c0, 0x1c4, "cam"),
3628 	TEGRA_IO_PAD(TEGRA_IO_PAD_COMP, 22, 0x1b8, 0x1bc, "comp"),
3629 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x1b8, 0x1bc, "csia"),
3630 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x1b8, 0x1bc, "csib"),
3631 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 12, 0x1c0, 0x1c4, "csie"),
3632 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x1b8, 0x1bc, "dsi"),
3633 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 7, 0x1c0, 0x1c4, "dsib"),
3634 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 8, 0x1c0, 0x1c4, "dsic"),
3635 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 9, 0x1c0, 0x1c4, "dsid"),
3636 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI, 28, 0x1b8, 0x1bc, "hdmi"),
3637 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x1b8, 0x1bc, "hsic"),
3638 	TEGRA_IO_PAD(TEGRA_IO_PAD_HV, 6, 0x1c0, 0x1c4, "hv"),
3639 	TEGRA_IO_PAD(TEGRA_IO_PAD_LVDS, 25, 0x1c0, 0x1c4, "lvds"),
3640 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x1b8, 0x1bc, "mipi-bias"),
3641 	TEGRA_IO_PAD(TEGRA_IO_PAD_NAND, 13, 0x1b8, 0x1bc, "nand"),
3642 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_BIAS, 4, 0x1b8, 0x1bc, "pex-bias"),
3643 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 5, 0x1b8, 0x1bc, "pex-clk1"),
3644 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x1b8, 0x1bc, "pex-clk2"),
3645 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x1c0, 0x1c4, "pex-cntrl"),
3646 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1, 1, 0x1c0, 0x1c4, "sdmmc1"),
3647 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3, 2, 0x1c0, 0x1c4, "sdmmc3"),
3648 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 3, 0x1c0, 0x1c4, "sdmmc4"),
3649 	TEGRA_IO_PAD(TEGRA_IO_PAD_SYS_DDC, 26, 0x1c0, 0x1c4, "sys_ddc"),
3650 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x1b8, 0x1bc, "uart"),
3651 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x1b8, 0x1bc, "usb0"),
3652 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x1b8, 0x1bc, "usb1"),
3653 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x1b8, 0x1bc, "usb2"),
3654 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x1b8, 0x1bc, "usb_bias"),
3655 };
3656 
3657 static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
3658 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3659 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_BB, "bb"),
3660 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3661 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_COMP, "comp"),
3662 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3663 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3664 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3665 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3666 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3667 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3668 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3669 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI, "hdmi"),
3670 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3671 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HV, "hv"),
3672 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_LVDS, "lvds"),
3673 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3674 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_NAND, "nand"),
3675 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_BIAS, "pex-bias"),
3676 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3677 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3678 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3679 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1, "sdmmc1"),
3680 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3, "sdmmc3"),
3681 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
3682 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SYS_DDC, "sys_ddc"),
3683 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3684 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3685 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3686 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3687 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb_bias"),
3688 };
3689 
3690 static const struct tegra_pmc_soc tegra124_pmc_soc = {
3691 	.supports_core_domain = false,
3692 	.num_powergates = ARRAY_SIZE(tegra124_powergates),
3693 	.powergates = tegra124_powergates,
3694 	.num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
3695 	.cpu_powergates = tegra124_cpu_powergates,
3696 	.has_tsense_reset = true,
3697 	.has_gpu_clamps = true,
3698 	.needs_mbist_war = false,
3699 	.has_io_pad_wren = true,
3700 	.maybe_tz_only = false,
3701 	.num_io_pads = ARRAY_SIZE(tegra124_io_pads),
3702 	.io_pads = tegra124_io_pads,
3703 	.num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
3704 	.pin_descs = tegra124_pin_descs,
3705 	.regs = &tegra20_pmc_regs,
3706 	.init = tegra20_pmc_init,
3707 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3708 	.powergate_set = tegra114_powergate_set,
3709 	.reset_sources = tegra30_reset_sources,
3710 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3711 	.reset_levels = NULL,
3712 	.num_reset_levels = 0,
3713 	.pmc_clks_data = tegra_pmc_clks_data,
3714 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3715 	.has_blink_output = true,
3716 	.has_usb_sleepwalk = true,
3717 	.has_single_mmio_aperture = true,
3718 };
3719 
3720 static const char * const tegra210_powergates[] = {
3721 	[TEGRA_POWERGATE_CPU] = "crail",
3722 	[TEGRA_POWERGATE_3D] = "3d",
3723 	[TEGRA_POWERGATE_VENC] = "venc",
3724 	[TEGRA_POWERGATE_PCIE] = "pcie",
3725 	[TEGRA_POWERGATE_MPE] = "mpe",
3726 	[TEGRA_POWERGATE_SATA] = "sata",
3727 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3728 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3729 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3730 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3731 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3732 	[TEGRA_POWERGATE_SOR] = "sor",
3733 	[TEGRA_POWERGATE_DIS] = "dis",
3734 	[TEGRA_POWERGATE_DISB] = "disb",
3735 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3736 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3737 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3738 	[TEGRA_POWERGATE_VIC] = "vic",
3739 	[TEGRA_POWERGATE_IRAM] = "iram",
3740 	[TEGRA_POWERGATE_NVDEC] = "nvdec",
3741 	[TEGRA_POWERGATE_NVJPG] = "nvjpg",
3742 	[TEGRA_POWERGATE_AUD] = "aud",
3743 	[TEGRA_POWERGATE_DFD] = "dfd",
3744 	[TEGRA_POWERGATE_VE2] = "ve2",
3745 };
3746 
3747 static const u8 tegra210_cpu_powergates[] = {
3748 	TEGRA_POWERGATE_CPU0,
3749 	TEGRA_POWERGATE_CPU1,
3750 	TEGRA_POWERGATE_CPU2,
3751 	TEGRA_POWERGATE_CPU3,
3752 };
3753 
3754 static const struct tegra_io_pad_soc tegra210_io_pads[] = {
3755 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x1b8, 0x1bc, "audio"),
3756 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x1c0, 0x1c4, "audio-hv"),
3757 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 4, 0x1c0, 0x1c4, "cam"),
3758 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x1b8, 0x1bc, "csia"),
3759 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x1b8, 0x1bc, "csib"),
3760 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 10, 0x1c0, 0x1c4, "csic"),
3761 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 11, 0x1c0, 0x1c4, "csid"),
3762 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 12, 0x1c0, 0x1c4, "csie"),
3763 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 13, 0x1c0, 0x1c4, "csif"),
3764 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x1b8, 0x1bc, "dbg"),
3765 	TEGRA_IO_PAD(TEGRA_IO_PAD_DEBUG_NONAO, 26, 0x1b8, 0x1bc, "debug-nonao"),
3766 	TEGRA_IO_PAD(TEGRA_IO_PAD_DMIC, 18, 0x1c0, 0x1c4, "dmic"),
3767 	TEGRA_IO_PAD(TEGRA_IO_PAD_DP, 19, 0x1c0, 0x1c4, "dp"),
3768 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x1b8, 0x1bc, "dsi"),
3769 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 7, 0x1c0, 0x1c4, "dsib"),
3770 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 8, 0x1c0, 0x1c4, "dsic"),
3771 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 9, 0x1c0, 0x1c4, "dsid"),
3772 	TEGRA_IO_PAD(TEGRA_IO_PAD_EMMC, 3, 0x1c0, 0x1c4, "emmc"),
3773 	TEGRA_IO_PAD(TEGRA_IO_PAD_EMMC2, 5, 0x1c0, 0x1c4, "emmc2"),
3774 	TEGRA_IO_PAD(TEGRA_IO_PAD_GPIO, 27, 0x1b8, 0x1bc, "gpio"),
3775 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI, 28, 0x1b8, 0x1bc, "hdmi"),
3776 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x1b8, 0x1bc, "hsic"),
3777 	TEGRA_IO_PAD(TEGRA_IO_PAD_LVDS, 25, 0x1c0, 0x1c4, "lvds"),
3778 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x1b8, 0x1bc, "mipi-bias"),
3779 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_BIAS, 4, 0x1b8, 0x1bc, "pex-bias"),
3780 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 5, 0x1b8, 0x1bc, "pex-clk1"),
3781 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x1b8, 0x1bc, "pex-clk2"),
3782 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, UINT_MAX, UINT_MAX, "pex-cntrl"),
3783 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1, 1, 0x1c0, 0x1c4, "sdmmc1"),
3784 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3, 2, 0x1c0, 0x1c4, "sdmmc3"),
3785 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 14, 0x1c0, 0x1c4, "spi"),
3786 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI_HV, 15, 0x1c0, 0x1c4, "spi-hv"),
3787 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x1b8, 0x1bc, "uart"),
3788 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x1b8, 0x1bc, "usb0"),
3789 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x1b8, 0x1bc, "usb1"),
3790 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x1b8, 0x1bc, "usb2"),
3791 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB3, 18, 0x1b8, 0x1bc, "usb3"),
3792 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x1b8, 0x1bc, "usb-bias"),
3793 };
3794 
3795 static const struct tegra_io_pad_vctrl tegra210_io_pad_vctrls[] = {
3796 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AUDIO, PMC_PWR_DET_VALUE, 5),
3797 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AUDIO_HV, PMC_PWR_DET_VALUE, 18),
3798 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_CAM, PMC_PWR_DET_VALUE, 10),
3799 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_DBG, PMC_PWR_DET_VALUE, 19),
3800 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_DMIC, PMC_PWR_DET_VALUE, 20),
3801 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_GPIO, PMC_PWR_DET_VALUE, 21),
3802 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_PEX_CNTRL, PMC_PWR_DET_VALUE, 11),
3803 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC1, PMC_PWR_DET_VALUE, 12),
3804 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC3, PMC_PWR_DET_VALUE, 13),
3805 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SPI, PMC_PWR_DET_VALUE, 22),
3806 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SPI_HV, PMC_PWR_DET_VALUE, 23),
3807 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_UART, PMC_PWR_DET_VALUE, 2),
3808 };
3809 static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
3810 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3811 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
3812 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3813 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3814 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3815 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
3816 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
3817 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3818 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
3819 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
3820 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DEBUG_NONAO, "debug-nonao"),
3821 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DMIC, "dmic"),
3822 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DP, "dp"),
3823 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3824 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3825 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3826 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3827 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EMMC, "emmc"),
3828 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EMMC2, "emmc2"),
3829 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GPIO, "gpio"),
3830 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI, "hdmi"),
3831 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3832 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_LVDS, "lvds"),
3833 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3834 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_BIAS, "pex-bias"),
3835 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3836 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3837 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3838 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1, "sdmmc1"),
3839 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3, "sdmmc3"),
3840 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
3841 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI_HV, "spi-hv"),
3842 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3843 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3844 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3845 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3846 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB3, "usb3"),
3847 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb-bias"),
3848 };
3849 
3850 static const char * const tegra210_reset_sources[] = {
3851 	"POWER_ON_RESET",
3852 	"WATCHDOG",
3853 	"SENSOR",
3854 	"SW_MAIN",
3855 	"LP0",
3856 	"AOTAG"
3857 };
3858 
3859 static const struct tegra_wake_event tegra210_wake_events[] = {
3860 	TEGRA_WAKE_IRQ("rtc", 16, 2),
3861 	TEGRA_WAKE_IRQ("pmu", 51, 86),
3862 };
3863 
3864 static const struct tegra_pmc_soc tegra210_pmc_soc = {
3865 	.supports_core_domain = false,
3866 	.num_powergates = ARRAY_SIZE(tegra210_powergates),
3867 	.powergates = tegra210_powergates,
3868 	.num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
3869 	.cpu_powergates = tegra210_cpu_powergates,
3870 	.has_tsense_reset = true,
3871 	.has_gpu_clamps = true,
3872 	.needs_mbist_war = true,
3873 	.has_io_pad_wren = true,
3874 	.maybe_tz_only = true,
3875 	.num_io_pads = ARRAY_SIZE(tegra210_io_pads),
3876 	.io_pads = tegra210_io_pads,
3877 	.num_io_pad_vctrls = ARRAY_SIZE(tegra210_io_pad_vctrls),
3878 	.io_pad_vctrls = tegra210_io_pad_vctrls,
3879 	.num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
3880 	.pin_descs = tegra210_pin_descs,
3881 	.regs = &tegra20_pmc_regs,
3882 	.init = tegra20_pmc_init,
3883 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3884 	.powergate_set = tegra114_powergate_set,
3885 	.irq_set_wake = tegra210_pmc_irq_set_wake,
3886 	.irq_set_type = tegra210_pmc_irq_set_type,
3887 	.reset_sources = tegra210_reset_sources,
3888 	.num_reset_sources = ARRAY_SIZE(tegra210_reset_sources),
3889 	.reset_levels = NULL,
3890 	.num_reset_levels = 0,
3891 	.num_wake_events = ARRAY_SIZE(tegra210_wake_events),
3892 	.wake_events = tegra210_wake_events,
3893 	.pmc_clks_data = tegra_pmc_clks_data,
3894 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3895 	.has_blink_output = true,
3896 	.has_usb_sleepwalk = true,
3897 	.has_single_mmio_aperture = true,
3898 };
3899 
3900 static const struct tegra_io_pad_soc tegra186_io_pads[] = {
3901 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x74, 0x78, "csia"),
3902 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x74, 0x78, "csib"),
3903 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x74, 0x78, "dsi"),
3904 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x74, 0x78, "mipi-bias"),
3905 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, 0x74, 0x78, "pex-clk-bias"),
3906 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK3, 5, 0x74, 0x78, "pex-clk3"),
3907 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x74, 0x78, "pex-clk2"),
3908 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 7, 0x74, 0x78, "pex-clk1"),
3909 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x74, 0x78, "usb0"),
3910 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x74, 0x78, "usb1"),
3911 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x74, 0x78, "usb2"),
3912 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x74, 0x78, "usb-bias"),
3913 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x74, 0x78, "uart"),
3914 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x74, 0x78, "audio"),
3915 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x74, 0x78, "hsic"),
3916 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x74, 0x78, "dbg"),
3917 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 28, 0x74, 0x78, "hdmi-dp0"),
3918 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP1, 29, 0x74, 0x78, "hdmi-dp1"),
3919 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x7c, 0x80, "pex-cntrl"),
3920 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC2_HV, 2, 0x7c, 0x80, "sdmmc2-hv"),
3921 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 4, 0x7c, 0x80, "sdmmc4"),
3922 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 6, 0x7c, 0x80, "cam"),
3923 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 8, 0x7c, 0x80, "dsib"),
3924 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 9, 0x7c, 0x80, "dsic"),
3925 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 10, 0x7c, 0x80, "dsid"),
3926 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 11, 0x7c, 0x80, "csic"),
3927 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 12, 0x7c, 0x80, "csid"),
3928 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 13, 0x7c, 0x80, "csie"),
3929 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 14, 0x7c, 0x80, "csif"),
3930 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 15, 0x7c, 0x80, "spi"),
3931 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 17, 0x7c, 0x80, "ufs"),
3932 	TEGRA_IO_PAD(TEGRA_IO_PAD_DMIC_HV, 20, 0x7c, 0x80, "dmic-hv"),
3933 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 21, 0x7c, 0x80, "edp"),
3934 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 23, 0x7c, 0x80, "sdmmc1-hv"),
3935 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, 24, 0x7c, 0x80, "sdmmc3-hv"),
3936 	TEGRA_IO_PAD(TEGRA_IO_PAD_CONN, 28, 0x7c, 0x80, "conn"),
3937 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x7c, 0x80, "audio-hv"),
3938 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, "ao-hv"),
3939 };
3940 
3941 static const struct tegra_io_pad_vctrl tegra186_io_pad_vctrls[] = {
3942 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC2_HV, PMC_IMPL_E_33V_PWR, 5),
3943 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_DMIC_HV,  PMC_IMPL_E_33V_PWR, 2),
3944 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC1_HV, PMC_IMPL_E_33V_PWR, 4),
3945 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC3_HV, PMC_IMPL_E_33V_PWR, 6),
3946 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AUDIO_HV, PMC_IMPL_E_33V_PWR, 1),
3947 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AO_HV, PMC_IMPL_E_33V_PWR, 0),
3948 };
3949 
3950 static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
3951 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3952 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3953 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3954 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3955 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_BIAS, "pex-clk-bias"),
3956 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK3, "pex-clk3"),
3957 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3958 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3959 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3960 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3961 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3962 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb-bias"),
3963 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3964 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3965 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3966 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
3967 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
3968 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP1, "hdmi-dp1"),
3969 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3970 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC2_HV, "sdmmc2-hv"),
3971 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
3972 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3973 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3974 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3975 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3976 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
3977 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
3978 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3979 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
3980 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
3981 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
3982 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DMIC_HV, "dmic-hv"),
3983 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
3984 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
3985 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
3986 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CONN, "conn"),
3987 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
3988 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
3989 };
3990 
3991 static const struct tegra_pmc_regs tegra186_pmc_regs = {
3992 	.scratch0 = 0x2000,
3993 	.rst_status = 0x70,
3994 	.rst_source_shift = 0x2,
3995 	.rst_source_mask = 0x3c,
3996 	.rst_level_shift = 0x0,
3997 	.rst_level_mask = 0x3,
3998 	.aowake_mask_w = 0x180,
3999 	.aowake_status_w = 0x30c,
4000 	.aowake_status_r = 0x48c,
4001 	.aowake_tier2_routing = 0x4cc,
4002 	.aowake_sw_status_w = 0x49c,
4003 	.aowake_sw_status = 0x4a0,
4004 	.aowake_latch_sw = 0x498,
4005 	.aowake_ctrl = 0x4f4,
4006 };
4007 
4008 static void tegra186_pmc_init(struct tegra_pmc *pmc)
4009 {
4010 	pmc->syscore.ops = &tegra186_pmc_wake_syscore_ops;
4011 	pmc->syscore.data = pmc;
4012 	register_syscore(&pmc->syscore);
4013 }
4014 
4015 static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
4016 					    struct device_node *np)
4017 {
4018 	struct resource regs;
4019 	void __iomem *wake;
4020 	u32 value;
4021 	int index;
4022 
4023 	index = of_property_match_string(np, "reg-names", "wake");
4024 	if (index < 0) {
4025 		dev_err(pmc->dev, "failed to find PMC wake registers\n");
4026 		return;
4027 	}
4028 
4029 	of_address_to_resource(np, index, &regs);
4030 
4031 	wake = ioremap(regs.start, resource_size(&regs));
4032 	if (!wake) {
4033 		dev_err(pmc->dev, "failed to map PMC wake registers\n");
4034 		return;
4035 	}
4036 
4037 	value = readl(wake + pmc->soc->regs->aowake_ctrl);
4038 
4039 	if (pmc->invert_irq)
4040 		value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
4041 	else
4042 		value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
4043 
4044 	writel(value, wake + pmc->soc->regs->aowake_ctrl);
4045 
4046 	iounmap(wake);
4047 }
4048 
4049 static const char * const tegra186_reset_sources[] = {
4050 	"SYS_RESET",
4051 	"AOWDT",
4052 	"MCCPLEXWDT",
4053 	"BPMPWDT",
4054 	"SCEWDT",
4055 	"SPEWDT",
4056 	"APEWDT",
4057 	"BCCPLEXWDT",
4058 	"SENSOR",
4059 	"AOTAG",
4060 	"VFSENSOR",
4061 	"SWREST",
4062 	"SC7",
4063 	"HSM",
4064 	"CORESIGHT"
4065 };
4066 
4067 static const char * const tegra186_reset_levels[] = {
4068 	"L0", "L1", "L2", "WARM"
4069 };
4070 
4071 static const struct tegra_wake_event tegra186_wake_events[] = {
4072 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4073 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA186_AON_GPIO(FF, 0)),
4074 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4075 };
4076 
4077 static const struct tegra_pmc_soc tegra186_pmc_soc = {
4078 	.supports_core_domain = false,
4079 	.num_powergates = 0,
4080 	.powergates = NULL,
4081 	.num_cpu_powergates = 0,
4082 	.cpu_powergates = NULL,
4083 	.has_tsense_reset = false,
4084 	.has_gpu_clamps = false,
4085 	.needs_mbist_war = false,
4086 	.has_io_pad_wren = false,
4087 	.maybe_tz_only = false,
4088 	.num_io_pads = ARRAY_SIZE(tegra186_io_pads),
4089 	.io_pads = tegra186_io_pads,
4090 	.num_io_pad_vctrls = ARRAY_SIZE(tegra186_io_pad_vctrls),
4091 	.io_pad_vctrls = tegra186_io_pad_vctrls,
4092 	.num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
4093 	.pin_descs = tegra186_pin_descs,
4094 	.regs = &tegra186_pmc_regs,
4095 	.init = tegra186_pmc_init,
4096 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4097 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4098 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4099 	.irq_set_type = tegra186_pmc_irq_set_type,
4100 	.reset_sources = tegra186_reset_sources,
4101 	.num_reset_sources = ARRAY_SIZE(tegra186_reset_sources),
4102 	.reset_levels = tegra186_reset_levels,
4103 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4104 	.num_wake_events = ARRAY_SIZE(tegra186_wake_events),
4105 	.wake_events = tegra186_wake_events,
4106 	.max_wake_events = 96,
4107 	.max_wake_vectors = 3,
4108 	.pmc_clks_data = NULL,
4109 	.num_pmc_clks = 0,
4110 	.has_blink_output = false,
4111 	.has_usb_sleepwalk = false,
4112 	.has_single_mmio_aperture = false,
4113 };
4114 
4115 static const struct tegra_io_pad_soc tegra194_io_pads[] = {
4116 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x74, 0x78, "csia"),
4117 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x74, 0x78, "csib"),
4118 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x74, 0x78, "mipi-bias"),
4119 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, 0x74, 0x78, "pex-clk-bias"),
4120 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK3, 5, 0x74, 0x78, "pex-clk3"),
4121 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x74, 0x78, "pex-clk2"),
4122 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 7, 0x74, 0x78, "pex-clk1"),
4123 	TEGRA_IO_PAD(TEGRA_IO_PAD_EQOS, 8, 0x74, 0x78, "eqos"),
4124 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_2_BIAS, 9, 0x74, 0x78, "pex-clk-2-bias"),
4125 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_2, 10, 0x74, 0x78, "pex-clk-2"),
4126 	TEGRA_IO_PAD(TEGRA_IO_PAD_DAP3, 11, 0x74, 0x78, "dap3"),
4127 	TEGRA_IO_PAD(TEGRA_IO_PAD_DAP5, 12, 0x74, 0x78, "dap5"),
4128 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x74, 0x78, "uart"),
4129 	TEGRA_IO_PAD(TEGRA_IO_PAD_PWR_CTL, 15, 0x74, 0x78, "pwr-ctl"),
4130 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO53, 16, 0x74, 0x78, "soc-gpio53"),
4131 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x74, 0x78, "audio"),
4132 	TEGRA_IO_PAD(TEGRA_IO_PAD_GP_PWM2, 18, 0x74, 0x78, "gp-pwm2"),
4133 	TEGRA_IO_PAD(TEGRA_IO_PAD_GP_PWM3, 19, 0x74, 0x78, "gp-pwm3"),
4134 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO12, 20, 0x74, 0x78, "soc-gpio12"),
4135 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO13, 21, 0x74, 0x78, "soc-gpio13"),
4136 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO10, 22, 0x74, 0x78, "soc-gpio10"),
4137 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART4, 23, 0x74, 0x78, "uart4"),
4138 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART5, 24, 0x74, 0x78, "uart5"),
4139 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x74, 0x78, "dbg"),
4140 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP3, 26, 0x74, 0x78, "hdmi-dp3"),
4141 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP2, 27, 0x74, 0x78, "hdmi-dp2"),
4142 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 28, 0x74, 0x78, "hdmi-dp0"),
4143 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP1, 29, 0x74, 0x78, "hdmi-dp1"),
4144 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x7c, 0x80, "pex-cntrl"),
4145 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CTL2, 1, 0x7c, 0x80, "pex-ctl2"),
4146 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L0_RST, 2, 0x7c, 0x80, "pex-l0-rst"),
4147 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L1_RST, 3, 0x7c, 0x80, "pex-l1-rst"),
4148 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 4, 0x7c, 0x80, "sdmmc4"),
4149 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L5_RST, 5, 0x7c, 0x80, "pex-l5-rst"),
4150 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 6, 0x7c, 0x80, "cam"),
4151 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 11, 0x7c, 0x80, "csic"),
4152 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 12, 0x7c, 0x80, "csid"),
4153 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 13, 0x7c, 0x80, "csie"),
4154 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 14, 0x7c, 0x80, "csif"),
4155 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 15, 0x7c, 0x80, "spi"),
4156 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 17, 0x7c, 0x80, "ufs"),
4157 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIG, 18, 0x7c, 0x80, "csig"),
4158 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIH, 19, 0x7c, 0x80, "csih"),
4159 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 21, 0x7c, 0x80, "edp"),
4160 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 23, 0x7c, 0x80, "sdmmc1-hv"),
4161 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, 24, 0x7c, 0x80, "sdmmc3-hv"),
4162 	TEGRA_IO_PAD(TEGRA_IO_PAD_CONN, 28, 0x7c, 0x80, "conn"),
4163 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x7c, 0x80, "audio-hv"),
4164 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, "ao-hv"),
4165 };
4166 
4167 static const struct tegra_io_pad_vctrl tegra194_io_pad_vctrls[] = {
4168 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC1_HV, PMC_IMPL_E_33V_PWR, 4),
4169 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC3_HV, PMC_IMPL_E_33V_PWR, 6),
4170 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AUDIO_HV, PMC_IMPL_E_33V_PWR, 1),
4171 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AO_HV, PMC_IMPL_E_33V_PWR, 0),
4172 };
4173 
4174 static const struct pinctrl_pin_desc tegra194_pin_descs[] = {
4175 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
4176 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
4177 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
4178 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_BIAS, "pex-clk-bias"),
4179 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK3, "pex-clk3"),
4180 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
4181 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
4182 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EQOS, "eqos"),
4183 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_2_BIAS, "pex-clk-2-bias"),
4184 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_2, "pex-clk-2"),
4185 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DAP3, "dap3"),
4186 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DAP5, "dap5"),
4187 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
4188 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PWR_CTL, "pwr-ctl"),
4189 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO53, "soc-gpio53"),
4190 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
4191 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GP_PWM2, "gp-pwm2"),
4192 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GP_PWM3, "gp-pwm3"),
4193 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO12, "soc-gpio12"),
4194 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO13, "soc-gpio13"),
4195 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO10, "soc-gpio10"),
4196 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART4, "uart4"),
4197 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART5, "uart5"),
4198 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
4199 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP3, "hdmi-dp3"),
4200 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP2, "hdmi-dp2"),
4201 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4202 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP1, "hdmi-dp1"),
4203 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
4204 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CTL2, "pex-ctl2"),
4205 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L0_RST, "pex-l0-rst"),
4206 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L1_RST, "pex-l1-rst"),
4207 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
4208 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L5_RST, "pex-l5-rst"),
4209 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
4210 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
4211 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
4212 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
4213 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
4214 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
4215 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
4216 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIG, "csig"),
4217 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIH, "csih"),
4218 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4219 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4220 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
4221 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CONN, "conn"),
4222 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
4223 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
4224 };
4225 
4226 static const struct tegra_pmc_regs tegra194_pmc_regs = {
4227 	.scratch0 = 0x2000,
4228 	.rst_status = 0x70,
4229 	.rst_source_shift = 0x2,
4230 	.rst_source_mask = 0x7c,
4231 	.rst_level_shift = 0x0,
4232 	.rst_level_mask = 0x3,
4233 	.aowake_mask_w = 0x180,
4234 	.aowake_status_w = 0x30c,
4235 	.aowake_status_r = 0x48c,
4236 	.aowake_tier2_routing = 0x4cc,
4237 	.aowake_sw_status_w = 0x49c,
4238 	.aowake_sw_status = 0x4a0,
4239 	.aowake_latch_sw = 0x498,
4240 	.aowake_ctrl = 0x4f4,
4241 };
4242 
4243 static const char * const tegra194_reset_sources[] = {
4244 	"SYS_RESET_N",
4245 	"AOWDT",
4246 	"BCCPLEXWDT",
4247 	"BPMPWDT",
4248 	"SCEWDT",
4249 	"SPEWDT",
4250 	"APEWDT",
4251 	"LCCPLEXWDT",
4252 	"SENSOR",
4253 	"AOTAG",
4254 	"VFSENSOR",
4255 	"MAINSWRST",
4256 	"SC7",
4257 	"HSM",
4258 	"CSITE",
4259 	"RCEWDT",
4260 	"PVA0WDT",
4261 	"PVA1WDT",
4262 	"L1A_ASYNC",
4263 	"BPMPBOOT",
4264 	"FUSECRC",
4265 };
4266 
4267 static const struct tegra_wake_event tegra194_wake_events[] = {
4268 	TEGRA_WAKE_GPIO("eqos", 20, 0, TEGRA194_MAIN_GPIO(G, 4)),
4269 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4270 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA194_AON_GPIO(EE, 4)),
4271 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4272 	TEGRA_WAKE_SIMPLE("usb3-port-0", 76),
4273 	TEGRA_WAKE_SIMPLE("usb3-port-1", 77),
4274 	TEGRA_WAKE_SIMPLE("usb3-port-2-3", 78),
4275 	TEGRA_WAKE_SIMPLE("usb2-port-0", 79),
4276 	TEGRA_WAKE_SIMPLE("usb2-port-1", 80),
4277 	TEGRA_WAKE_SIMPLE("usb2-port-2", 81),
4278 	TEGRA_WAKE_SIMPLE("usb2-port-3", 82),
4279 };
4280 
4281 static const struct tegra_pmc_soc tegra194_pmc_soc = {
4282 	.supports_core_domain = false,
4283 	.num_powergates = 0,
4284 	.powergates = NULL,
4285 	.num_cpu_powergates = 0,
4286 	.cpu_powergates = NULL,
4287 	.has_tsense_reset = false,
4288 	.has_gpu_clamps = false,
4289 	.needs_mbist_war = false,
4290 	.has_io_pad_wren = false,
4291 	.maybe_tz_only = false,
4292 	.num_io_pads = ARRAY_SIZE(tegra194_io_pads),
4293 	.io_pads = tegra194_io_pads,
4294 	.num_io_pad_vctrls = ARRAY_SIZE(tegra194_io_pad_vctrls),
4295 	.io_pad_vctrls = tegra194_io_pad_vctrls,
4296 	.num_pin_descs = ARRAY_SIZE(tegra194_pin_descs),
4297 	.pin_descs = tegra194_pin_descs,
4298 	.regs = &tegra194_pmc_regs,
4299 	.init = tegra186_pmc_init,
4300 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4301 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4302 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4303 	.irq_set_type = tegra186_pmc_irq_set_type,
4304 	.reset_sources = tegra194_reset_sources,
4305 	.num_reset_sources = ARRAY_SIZE(tegra194_reset_sources),
4306 	.reset_levels = tegra186_reset_levels,
4307 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4308 	.num_wake_events = ARRAY_SIZE(tegra194_wake_events),
4309 	.wake_events = tegra194_wake_events,
4310 	.max_wake_events = 96,
4311 	.max_wake_vectors = 3,
4312 	.pmc_clks_data = NULL,
4313 	.num_pmc_clks = 0,
4314 	.has_blink_output = false,
4315 	.has_usb_sleepwalk = false,
4316 	.has_single_mmio_aperture = false,
4317 };
4318 
4319 static const struct tegra_io_pad_soc tegra234_io_pads[] = {
4320 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0xe0c0, 0xe0c4, "csia"),
4321 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0xe0c0, 0xe0c4, "csib"),
4322 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 0, 0xe0d0, 0xe0d4, "hdmi-dp0"),
4323 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 2, 0xe0c0, 0xe0c4, "csic"),
4324 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 3, 0xe0c0, 0xe0c4, "csid"),
4325 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 4, 0xe0c0, 0xe0c4, "csie"),
4326 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 5, 0xe0c0, 0xe0c4, "csif"),
4327 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 0, 0xe064, 0xe068, "ufs"),
4328 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 1, 0xe05c, 0xe060, "edp"),
4329 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 0, 0xe054, 0xe058, "sdmmc1-hv"),
4330 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, UINT_MAX, UINT_MAX, UINT_MAX, "sdmmc3-hv"),
4331 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, UINT_MAX, UINT_MAX, UINT_MAX, "audio-hv"),
4332 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, "ao-hv"),
4333 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIG, 6, 0xe0c0, 0xe0c4, "csig"),
4334 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIH, 7, 0xe0c0, 0xe0c4, "csih"),
4335 };
4336 
4337 static const struct tegra_io_pad_vctrl tegra234_io_pad_vctrls[] = {
4338 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC1_HV, PMC_IMPL_E_33V_PWR, 4),
4339 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC3_HV, PMC_IMPL_E_33V_PWR, 6),
4340 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AUDIO_HV, PMC_IMPL_E_33V_PWR, 1),
4341 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AO_HV, PMC_IMPL_E_33V_PWR, 0),
4342 };
4343 
4344 static const struct pinctrl_pin_desc tegra234_pin_descs[] = {
4345 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
4346 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
4347 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4348 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
4349 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
4350 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
4351 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
4352 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
4353 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4354 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4355 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
4356 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
4357 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
4358 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIG, "csig"),
4359 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIH, "csih"),
4360 };
4361 
4362 static const struct tegra_pmc_regs tegra234_pmc_regs = {
4363 	.scratch0 = 0x2000,
4364 	.rst_status = 0x70,
4365 	.rst_source_shift = 0x2,
4366 	.rst_source_mask = 0xfc,
4367 	.rst_level_shift = 0x0,
4368 	.rst_level_mask = 0x3,
4369 	.aowake_mask_w = 0x180,
4370 	.aowake_status_w = 0x30c,
4371 	.aowake_status_r = 0x48c,
4372 	.aowake_tier2_routing = 0x4cc,
4373 	.aowake_sw_status_w = 0x49c,
4374 	.aowake_sw_status = 0x4a0,
4375 	.aowake_latch_sw = 0x498,
4376 	.aowake_ctrl = 0x4f4,
4377 };
4378 
4379 static const char * const tegra234_reset_sources[] = {
4380 	"SYS_RESET_N",	/* 0x0 */
4381 	"AOWDT",
4382 	"BCCPLEXWDT",
4383 	"BPMPWDT",
4384 	"SCEWDT",
4385 	"SPEWDT",
4386 	"APEWDT",
4387 	"LCCPLEXWDT",
4388 	"SENSOR",	/* 0x8 */
4389 	NULL,
4390 	NULL,
4391 	"MAINSWRST",
4392 	"SC7",
4393 	"HSM",
4394 	NULL,
4395 	"RCEWDT",
4396 	NULL,		/* 0x10 */
4397 	NULL,
4398 	NULL,
4399 	"BPMPBOOT",
4400 	"FUSECRC",
4401 	"DCEWDT",
4402 	"PSCWDT",
4403 	"PSC",
4404 	"CSITE_SW",	/* 0x18 */
4405 	"POD",
4406 	"SCPM",
4407 	"VREFRO_POWERBAD",
4408 	"VMON",
4409 	"FMON",
4410 	"FSI_R5WDT",
4411 	"FSI_THERM",
4412 	"FSI_R52C0WDT",	/* 0x20 */
4413 	"FSI_R52C1WDT",
4414 	"FSI_R52C2WDT",
4415 	"FSI_R52C3WDT",
4416 	"FSI_FMON",
4417 	"FSI_VMON",	/* 0x25 */
4418 };
4419 
4420 static const struct tegra_wake_event tegra234_wake_events[] = {
4421 	TEGRA_WAKE_GPIO("sd-wake", 8, 0, TEGRA234_MAIN_GPIO(G, 7)),
4422 	TEGRA_WAKE_GPIO("eqos", 20, 0, TEGRA234_MAIN_GPIO(G, 4)),
4423 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4424 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA234_AON_GPIO(EE, 4)),
4425 	TEGRA_WAKE_GPIO("mgbe", 56, 0, TEGRA234_MAIN_GPIO(Y, 3)),
4426 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4427 	TEGRA_WAKE_IRQ("usb3-port-0", 76, 167),
4428 	TEGRA_WAKE_IRQ("usb3-port-1", 77, 167),
4429 	TEGRA_WAKE_IRQ("usb3-port-2-3", 78, 167),
4430 	TEGRA_WAKE_IRQ("usb2-port-0", 79, 167),
4431 	TEGRA_WAKE_IRQ("usb2-port-1", 80, 167),
4432 	TEGRA_WAKE_IRQ("usb2-port-2", 81, 167),
4433 	TEGRA_WAKE_IRQ("usb2-port-3", 82, 167),
4434 	TEGRA_WAKE_IRQ("sw-wake", SW_WAKE_ID, 179),
4435 };
4436 
4437 static const struct tegra_pmc_soc tegra234_pmc_soc = {
4438 	.supports_core_domain = false,
4439 	.num_powergates = 0,
4440 	.powergates = NULL,
4441 	.num_cpu_powergates = 0,
4442 	.cpu_powergates = NULL,
4443 	.has_tsense_reset = false,
4444 	.has_gpu_clamps = false,
4445 	.needs_mbist_war = false,
4446 	.has_io_pad_wren = false,
4447 	.maybe_tz_only = false,
4448 	.num_io_pads = ARRAY_SIZE(tegra234_io_pads),
4449 	.io_pads = tegra234_io_pads,
4450 	.num_io_pad_vctrls = ARRAY_SIZE(tegra234_io_pad_vctrls),
4451 	.io_pad_vctrls = tegra234_io_pad_vctrls,
4452 	.num_pin_descs = ARRAY_SIZE(tegra234_pin_descs),
4453 	.pin_descs = tegra234_pin_descs,
4454 	.regs = &tegra234_pmc_regs,
4455 	.init = tegra186_pmc_init,
4456 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4457 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4458 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4459 	.irq_set_type = tegra186_pmc_irq_set_type,
4460 	.reset_sources = tegra234_reset_sources,
4461 	.num_reset_sources = ARRAY_SIZE(tegra234_reset_sources),
4462 	.reset_levels = tegra186_reset_levels,
4463 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4464 	.num_wake_events = ARRAY_SIZE(tegra234_wake_events),
4465 	.wake_events = tegra234_wake_events,
4466 	.max_wake_events = 96,
4467 	.max_wake_vectors = 3,
4468 	.pmc_clks_data = NULL,
4469 	.num_pmc_clks = 0,
4470 	.has_blink_output = false,
4471 	.has_single_mmio_aperture = false,
4472 };
4473 
4474 static const struct tegra_io_pad_soc tegra238_io_pads[] = {
4475 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 0, 0xe028, 0xe02c, "hdmi-dp0"),
4476 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 0, 0xe06c, 0xe070, "ufs"),
4477 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 2, 0xe040, 0xe044, "edp"),
4478 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 0, 0xe058, 0xe05c, "sdmmc1-hv"),
4479 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, UINT_MAX, UINT_MAX, UINT_MAX, "sdmmc3-hv"),
4480 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, UINT_MAX, UINT_MAX, UINT_MAX, "audio-hv"),
4481 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, "ao-hv"),
4482 };
4483 
4484 static const struct tegra_io_pad_vctrl tegra238_io_pad_vctrls[] = {
4485 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC1_HV, PMC_IMPL_E_33V_PWR, 4),
4486 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC3_HV, PMC_IMPL_E_33V_PWR, 6),
4487 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AUDIO_HV, PMC_IMPL_E_33V_PWR, 1),
4488 	TEGRA_IO_PAD_VCTRL(TEGRA_IO_PAD_AO_HV, PMC_IMPL_E_33V_PWR, 0),
4489 };
4490 
4491 static const struct pinctrl_pin_desc tegra238_pin_descs[] = {
4492 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4493 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
4494 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4495 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4496 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
4497 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
4498 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
4499 };
4500 
4501 static const struct tegra_pmc_regs tegra238_pmc_regs = {
4502 	.scratch0 = 0x2000,
4503 	.rst_status = 0x70,
4504 	.rst_source_shift = 0x2,
4505 	.rst_source_mask = 0xfc,
4506 	.rst_level_shift = 0x0,
4507 	.rst_level_mask = 0x3,
4508 	.aowake_mask_w = 0x180,
4509 	.aowake_status_w = 0x30c,
4510 	.aowake_status_r = 0x48c,
4511 	.aowake_tier2_routing = 0x4cc,
4512 	.aowake_sw_status_w = 0x49c,
4513 	.aowake_sw_status = 0x4a0,
4514 	.aowake_latch_sw = 0x498,
4515 	.aowake_ctrl = 0x4f4,
4516 };
4517 
4518 static const char * const tegra238_reset_sources[] = {
4519 	"SYS_RESET_N",		/* 0 */
4520 	"AOWDT",
4521 	NULL,
4522 	"BPMPWDT",
4523 	NULL,
4524 	"SPEWDT",		/* 5 */
4525 	NULL,
4526 	NULL,
4527 	"SENSOR",
4528 	NULL,
4529 	NULL,			/* 10 */
4530 	"MAINSWRST",
4531 	"SC7",
4532 	NULL,
4533 	NULL,
4534 	NULL,			/* 15 */
4535 	NULL,
4536 	NULL,
4537 	"RTC_XTAL_CSDC",
4538 	"BPMPBOOT",
4539 	"FUSECRC",		/* 20 */
4540 	NULL,
4541 	"PSCWDT",
4542 	"PSC_SW",
4543 	"CSITE_SW",
4544 	NULL,			/* 25 */
4545 	NULL,
4546 	"VREFRO_POWERBAD",
4547 	NULL,
4548 	NULL,
4549 	NULL,			/* 30 */
4550 	NULL,
4551 	NULL,
4552 	NULL,
4553 	NULL,
4554 	NULL,			/* 35 */
4555 	NULL,
4556 	NULL,
4557 	"TOP0WDT",
4558 	"TOP1WDT",
4559 	"TOP2WDT",		/* 40 */
4560 	"APE_C0WDT",
4561 	"APE_C1WDT",
4562 	"APE_C2WDT",
4563 	"APE_C3WDT",
4564 	"SCPM_SOC_XTAL",	/* 45 */
4565 	"SCPM_RTC_XTAL",
4566 	"SCPM_BPMP_CORE_CLK",
4567 	"SCPM_PSC_SE_CLK",
4568 	"FMON_32K",
4569 	"FMON_OSC",		/* 50 */
4570 	"VMON_SOC",
4571 	"VMON_CPU0",
4572 	NULL,
4573 	"POD_CPU",
4574 	"POD_GPU",		/* 55 */
4575 	"POD_RTC",
4576 	NULL,
4577 	"POD_IO",
4578 	"POD_PLUS_SOC",
4579 	"POD_PLUS_IO_VMON",	/* 60 */
4580 	"POD_PLUS_IO_PSCPLL",
4581 	"VMON_PLUS_0",
4582 	"VMON_PLUS_1",		/* 63 */
4583 };
4584 
4585 static const struct tegra_wake_event tegra238_wake_events[] = {
4586 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4587 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4588 	TEGRA_WAKE_IRQ("usb3-port-0", 76, 167),
4589 	TEGRA_WAKE_IRQ("usb3-port-1", 77, 167),
4590 	TEGRA_WAKE_IRQ("usb3-port-2", 78, 167),
4591 	TEGRA_WAKE_IRQ("usb2-port-0", 79, 167),
4592 	TEGRA_WAKE_IRQ("usb2-port-1", 80, 167),
4593 	TEGRA_WAKE_IRQ("usb2-port-2", 81, 167),
4594 };
4595 
4596 static const struct tegra_pmc_soc tegra238_pmc_soc = {
4597 	.num_powergates = 0,
4598 	.powergates = NULL,
4599 	.num_cpu_powergates = 0,
4600 	.cpu_powergates = NULL,
4601 	.has_tsense_reset = false,
4602 	.has_gpu_clamps = false,
4603 	.needs_mbist_war = false,
4604 	.has_io_pad_wren = false,
4605 	.maybe_tz_only = false,
4606 	.num_io_pads = ARRAY_SIZE(tegra238_io_pads),
4607 	.io_pads = tegra238_io_pads,
4608 	.num_io_pad_vctrls = ARRAY_SIZE(tegra238_io_pad_vctrls),
4609 	.io_pad_vctrls = tegra238_io_pad_vctrls,
4610 	.num_pin_descs = ARRAY_SIZE(tegra238_pin_descs),
4611 	.pin_descs = tegra238_pin_descs,
4612 	.regs = &tegra238_pmc_regs,
4613 	.init = tegra186_pmc_init,
4614 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4615 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4616 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4617 	.irq_set_type = tegra186_pmc_irq_set_type,
4618 	.reset_sources = tegra238_reset_sources,
4619 	.num_reset_sources = ARRAY_SIZE(tegra238_reset_sources),
4620 	.reset_levels = tegra186_reset_levels,
4621 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4622 	.num_wake_events = ARRAY_SIZE(tegra238_wake_events),
4623 	.wake_events = tegra238_wake_events,
4624 	.max_wake_events = 96,
4625 	.max_wake_vectors = 3,
4626 	.pmc_clks_data = NULL,
4627 	.num_pmc_clks = 0,
4628 	.has_blink_output = false,
4629 	.has_single_mmio_aperture = false,
4630 };
4631 
4632 #define TEGRA264_IO_PAD_VCTRL(_id, _offset, _ena_3v3, _ena_1v8)		\
4633 	((struct tegra_io_pad_vctrl) {					\
4634 		.id		= (_id),				\
4635 		.offset		= (_offset),				\
4636 		.ena_3v3	= (_ena_3v3),				\
4637 		.ena_1v8	= (_ena_1v8),				\
4638 	})
4639 
4640 static const struct tegra_io_pad_soc tegra264_io_pads[] = {
4641 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x41020, 0x41024, "csia"),
4642 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x41020, 0x41024, "csib"),
4643 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 0, 0x41050, 0x41054, "hdmi-dp0"),
4644 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 2, 0x41020, 0x41024, "csic"),
4645 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 3, 0x41020, 0x41024, "csid"),
4646 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 4, 0x41020, 0x41024, "csie"),
4647 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 5, 0x41020, 0x41024, "csif"),
4648 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 4, 0x41040, 0x41044, "ufs0"),
4649 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 0, 0x41028, 0x4102c, "edp"),
4650 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1, 0, 0x41090, 0x41094, "sdmmc1"),
4651 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, UINT_MAX, UINT_MAX, UINT_MAX, "sdmmc1-hv"),
4652 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIG, 6, 0x41020, 0x41024, "csig"),
4653 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIH, 7, 0x41020, 0x41024, "csih"),
4654 };
4655 
4656 static const struct tegra_io_pad_vctrl tegra264_io_pad_vctrls[] = {
4657 	TEGRA264_IO_PAD_VCTRL(TEGRA_IO_PAD_SDMMC1_HV, PMC_IMPL_SDMMC1_HV_PADCTL_0, 0, 0x6),
4658 };
4659 
4660 static const struct pinctrl_pin_desc tegra264_pin_descs[] = {
4661 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
4662 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
4663 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4664 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
4665 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
4666 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
4667 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
4668 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs0"),
4669 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4670 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1, "sdmmc1"),
4671 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4672 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIG, "csig"),
4673 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIH, "csih"),
4674 };
4675 
4676 static const struct tegra_pmc_regs tegra264_pmc_regs = {
4677 	.scratch0 = 0x684,
4678 	.rst_status = 0x4,
4679 	.rst_source_shift = 0x2,
4680 	.rst_source_mask = 0x1fc,
4681 	.rst_level_shift = 0x0,
4682 	.rst_level_mask = 0x3,
4683 	.aowake_mask_w = 0x200,
4684 	.aowake_status_w = 0x410,
4685 	.aowake_status_r = 0x610,
4686 	.aowake_tier2_routing = 0x660,
4687 	.aowake_sw_status_w = 0x624,
4688 	.aowake_sw_status = 0x628,
4689 	.aowake_latch_sw = 0x620,
4690 	.aowake_ctrl = 0x68c,
4691 };
4692 
4693 static const char * const tegra264_reset_sources[] = {
4694 	"SYS_RESET_N",		/* 0x0 */
4695 	"CSDC_RTC_XTAL",
4696 	"VREFRO_POWER_BAD",
4697 	"SCPM_SOC_XTAL",
4698 	"SCPM_RTC_XTAL",
4699 	"FMON_32K",
4700 	"FMON_OSC",
4701 	"POD_RTC",
4702 	"POD_IO",		/* 0x8 */
4703 	"POD_PLUS_IO_SPLL",
4704 	"POD_PLUS_SOC",
4705 	"VMON_PLUS_UV",
4706 	"VMON_PLUS_OV",
4707 	"FUSECRC_FAULT",
4708 	"OSC_FAULT",
4709 	"BPMP_BOOT_FAULT",
4710 	"SCPM_BPMP_CORE_CLK",	/* 0x10 */
4711 	"SCPM_PSC_SE_CLK",
4712 	"VMON_SOC_MIN",
4713 	"VMON_SOC_MAX",
4714 	"VMON_MSS_MIN",
4715 	"VMON_MSS_MAX",
4716 	"POD_PLUS_IO_VMON",
4717 	"NVJTAG_SEL_MONITOR",
4718 	"NV_THERM_FAULT",	/* 0x18 */
4719 	"FSI_THERM_FAULT",
4720 	"PSC_SW",
4721 	"SCPM_OESP_SE_CLK",
4722 	"SCPM_SB_SE_CLK",
4723 	"POD_CPU",
4724 	"POD_GPU",
4725 	"DCLS_GPU",
4726 	"POD_MSS",		/* 0x20 */
4727 	"FMON_FSI",
4728 	"POD_FSI",
4729 	"VMON_FSI_MIN",
4730 	"VMON_FSI_MAX",
4731 	"VMON_CPU0_MIN",
4732 	"VMON_CPU0_MAX",
4733 	"BPMP_FMON",
4734 	"AO_WDT_POR",		/* 0x28 */
4735 	"BPMP_WDT_POR",
4736 	"AO_TKE_WDT_POR",
4737 	"RCE0_WDT_POR",
4738 	"RCE1_WDT_POR",
4739 	"DCE_WDT_POR",
4740 	"FSI_R5_WDT_POR",
4741 	"FSI_R52_0_WDT_POR",
4742 	"FSI_R52_1_WDT_POR",	/* 0x30 */
4743 	"FSI_R52_2_WDT_POR",
4744 	"FSI_R52_3_WDT_POR",
4745 	"TOP_0_WDT_POR",
4746 	"TOP_1_WDT_POR",
4747 	"TOP_2_WDT_POR",
4748 	"APE_C0_WDT_POR",
4749 	"APE_C1_WDT_POR",
4750 	"GPU_TKE_WDT_POR",	/* 0x38 */
4751 	"PSC_WDT_POR",
4752 	"OESP_WDT_POR",
4753 	"SB_WDT_POR",
4754 	"SW_MAIN",
4755 	"L0L1_RST_OUT_N",
4756 	"FSI_HSM",
4757 	"CSITE_SW",
4758 	"AO_WDT_DBG",		/* 0x40 */
4759 	"BPMP_WDT_DBG",
4760 	"AO_TKE_WDT_DBG",
4761 	"RCE0_WDT_DBG",
4762 	"RCE1_WDT_DBG",
4763 	"DCE_WDT_DBG",
4764 	"FSI_R5_WDT_DBG",
4765 	"FSI_R52_0_WDT_DBG",
4766 	"FSI_R52_1_WDT_DBG",	/* 0x48 */
4767 	"FSI_R52_2_WDT_DBG",
4768 	"FSI_R52_3_WDT_DBG",
4769 	"TOP_0_WDT_DBG",
4770 	"TOP_1_WDT_DBG",
4771 	"TOP_2_WDT_DBG",
4772 	"APE_C0_WDT_DBG",
4773 	"APE_C1_WDT_DBG",
4774 	"PSC_WDT_DBG",		/* 0x50 */
4775 	"OESP_WDT_DBG",
4776 	"SB_WDT_DBG",
4777 	"TSC_0_WDT_DBG",
4778 	"TSC_1_WDT_DBG",
4779 	"L2_RST_OUT_N",
4780 	"SC7"
4781 };
4782 
4783 static const struct tegra_wake_event tegra264_wake_events[] = {
4784 	TEGRA_WAKE_IRQ("pmu", 0, 727),
4785 	TEGRA_WAKE_GPIO("power", 5, 1, TEGRA264_AON_GPIO(AA, 5)),
4786 	TEGRA_WAKE_IRQ("rtc", 65, 548),
4787 	TEGRA_WAKE_IRQ("usb3-port-0", 79, 965),
4788 	TEGRA_WAKE_IRQ("usb3-port-1", 80, 965),
4789 	TEGRA_WAKE_IRQ("usb3-port-3", 82, 965),
4790 	TEGRA_WAKE_IRQ("usb2-port-0", 83, 965),
4791 	TEGRA_WAKE_IRQ("usb2-port-1", 84, 965),
4792 	TEGRA_WAKE_IRQ("usb2-port-2", 85, 965),
4793 	TEGRA_WAKE_IRQ("usb2-port-3", 86, 965),
4794 };
4795 
4796 static const struct tegra_pmc_soc tegra264_pmc_soc = {
4797 	.has_io_pad_wren = false,
4798 	.num_io_pads = ARRAY_SIZE(tegra264_io_pads),
4799 	.io_pads = tegra264_io_pads,
4800 	.num_io_pad_vctrls = ARRAY_SIZE(tegra264_io_pad_vctrls),
4801 	.io_pad_vctrls = tegra264_io_pad_vctrls,
4802 	.num_pin_descs = ARRAY_SIZE(tegra264_pin_descs),
4803 	.pin_descs = tegra264_pin_descs,
4804 	.regs = &tegra264_pmc_regs,
4805 	.init = tegra186_pmc_init,
4806 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4807 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4808 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4809 	.irq_set_type = tegra186_pmc_irq_set_type,
4810 	.reset_sources = tegra264_reset_sources,
4811 	.num_reset_sources = ARRAY_SIZE(tegra264_reset_sources),
4812 	.reset_levels = tegra186_reset_levels,
4813 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4814 	.wake_events = tegra264_wake_events,
4815 	.num_wake_events = ARRAY_SIZE(tegra264_wake_events),
4816 	.max_wake_events = 128,
4817 	.max_wake_vectors = 4,
4818 };
4819 
4820 static const struct of_device_id tegra_pmc_match[] = {
4821 	{ .compatible = "nvidia,tegra264-pmc", .data = &tegra264_pmc_soc },
4822 	{ .compatible = "nvidia,tegra238-pmc", .data = &tegra238_pmc_soc },
4823 	{ .compatible = "nvidia,tegra234-pmc", .data = &tegra234_pmc_soc },
4824 	{ .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc },
4825 	{ .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
4826 	{ .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
4827 	{ .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
4828 	{ .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
4829 	{ .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
4830 	{ .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
4831 	{ .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
4832 	{ }
4833 };
4834 
4835 static void tegra_pmc_sync_state(struct device *dev)
4836 {
4837 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
4838 	struct device_node *np, *child;
4839 	int err;
4840 
4841 	np = of_get_child_by_name(dev->of_node, "powergates");
4842 	if (!np)
4843 		return;
4844 
4845 	for_each_child_of_node(np, child)
4846 		of_genpd_sync_state(child);
4847 
4848 	of_node_put(np);
4849 
4850 	np = of_get_child_by_name(dev->of_node, "core-domain");
4851 	if (!np)
4852 		return;
4853 
4854 	of_genpd_sync_state(np);
4855 	of_node_put(np);
4856 
4857 	/*
4858 	 * Newer device-trees have power domains, but we need to prepare all
4859 	 * device drivers with runtime PM and OPP support first, otherwise
4860 	 * state syncing is unsafe.
4861 	 */
4862 	if (!pmc->soc->supports_core_domain)
4863 		return;
4864 
4865 	/*
4866 	 * Older device-trees don't have core PD, and thus, there are
4867 	 * no dependencies that will block the state syncing. We shouldn't
4868 	 * mark the domain as synced in this case.
4869 	 */
4870 	pmc->core_domain_state_synced = true;
4871 
4872 	/* this is a no-op if core regulator isn't used */
4873 	mutex_lock(&pmc->powergates_lock);
4874 	err = dev_pm_opp_sync_regulators(dev);
4875 	mutex_unlock(&pmc->powergates_lock);
4876 
4877 	if (err)
4878 		dev_err(dev, "failed to sync regulators: %d\n", err);
4879 }
4880 
4881 static struct platform_driver tegra_pmc_driver = {
4882 	.driver = {
4883 		.name = "tegra-pmc",
4884 		.suppress_bind_attrs = true,
4885 		.of_match_table = tegra_pmc_match,
4886 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
4887 		.pm = &tegra_pmc_pm_ops,
4888 #endif
4889 		.sync_state = tegra_pmc_sync_state,
4890 	},
4891 	.probe = tegra_pmc_probe,
4892 };
4893 builtin_platform_driver(tegra_pmc_driver);
4894 
4895 #if defined(CONFIG_ARM)
4896 /**
4897  * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
4898  * @pmc: power management controller
4899  * @cpuid: CPU partition ID
4900  *
4901  * Returns the partition ID corresponding to the CPU partition ID or a
4902  * negative error code on failure.
4903  */
4904 static int tegra_get_cpu_powergate_id(struct tegra_pmc *pmc,
4905 				      unsigned int cpuid)
4906 {
4907 	if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
4908 		return pmc->soc->cpu_powergates[cpuid];
4909 
4910 	return -EINVAL;
4911 }
4912 
4913 /**
4914  * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
4915  * @cpuid: CPU partition ID
4916  */
4917 bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
4918 {
4919 	int id;
4920 
4921 	id = tegra_get_cpu_powergate_id(early_pmc, cpuid);
4922 	if (id < 0)
4923 		return false;
4924 
4925 	return tegra_powergate_is_powered(early_pmc, id);
4926 }
4927 
4928 /**
4929  * tegra_pmc_cpu_power_on() - power on CPU partition
4930  * @cpuid: CPU partition ID
4931  */
4932 int tegra_pmc_cpu_power_on(unsigned int cpuid)
4933 {
4934 	int id;
4935 
4936 	id = tegra_get_cpu_powergate_id(early_pmc, cpuid);
4937 	if (id < 0)
4938 		return id;
4939 
4940 	return tegra_powergate_set(early_pmc, id, true);
4941 }
4942 
4943 /**
4944  * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
4945  * @cpuid: CPU partition ID
4946  */
4947 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
4948 {
4949 	int id;
4950 
4951 	id = tegra_get_cpu_powergate_id(early_pmc, cpuid);
4952 	if (id < 0)
4953 		return id;
4954 
4955 	return tegra_pmc_powergate_remove_clamping(early_pmc, id);
4956 }
4957 
4958 bool tegra_pmc_core_domain_state_synced(void)
4959 {
4960 	return early_pmc->core_domain_state_synced;
4961 }
4962 
4963 #ifdef CONFIG_PM_SLEEP
4964 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
4965 {
4966 	return early_pmc->suspend_mode;
4967 }
4968 
4969 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
4970 {
4971 	if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
4972 		return;
4973 
4974 	early_pmc->suspend_mode = mode;
4975 }
4976 
4977 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
4978 {
4979 	unsigned long long rate = 0;
4980 	u64 ticks;
4981 	u32 value;
4982 
4983 	switch (mode) {
4984 	case TEGRA_SUSPEND_LP1:
4985 		rate = 32768;
4986 		break;
4987 
4988 	case TEGRA_SUSPEND_LP2:
4989 		rate = early_pmc->rate;
4990 		break;
4991 
4992 	default:
4993 		break;
4994 	}
4995 
4996 	if (WARN_ON_ONCE(rate == 0))
4997 		rate = 100000000;
4998 
4999 	ticks = early_pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
5000 	do_div(ticks, USEC_PER_SEC);
5001 	tegra_pmc_writel(early_pmc, ticks, PMC_CPUPWRGOOD_TIMER);
5002 
5003 	ticks = early_pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
5004 	do_div(ticks, USEC_PER_SEC);
5005 	tegra_pmc_writel(early_pmc, ticks, PMC_CPUPWROFF_TIMER);
5006 
5007 	value = tegra_pmc_readl(early_pmc, PMC_CNTRL);
5008 	value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
5009 	value |= PMC_CNTRL_CPU_PWRREQ_OE;
5010 	tegra_pmc_writel(early_pmc, value, PMC_CNTRL);
5011 }
5012 #endif /* CONFIG_PM_SLEEP */
5013 
5014 /*
5015  * Early initialization to allow access to registers in the very early boot
5016  * process.
5017  */
5018 static int __init tegra_pmc_early_init(void)
5019 {
5020 	const struct of_device_id *match;
5021 	struct device_node *np;
5022 	struct resource regs;
5023 	int err;
5024 
5025 	mutex_init(&early_pmc->powergates_lock);
5026 
5027 	np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
5028 	if (!np) {
5029 		/*
5030 		 * Fall back to legacy initialization for 32-bit ARM only. All
5031 		 * 64-bit ARM device tree files for Tegra are required to have
5032 		 * a PMC node.
5033 		 *
5034 		 * This is for backwards-compatibility with old device trees
5035 		 * that didn't contain a PMC node. Note that in this case the
5036 		 * SoC data can't be matched and therefore powergating is
5037 		 * disabled.
5038 		 */
5039 		if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
5040 			pr_warn("DT node not found, powergating disabled\n");
5041 
5042 			regs.start = 0x7000e400;
5043 			regs.end = 0x7000e7ff;
5044 			regs.flags = IORESOURCE_MEM;
5045 
5046 			pr_warn("Using memory region %pR\n", &regs);
5047 		} else {
5048 			/*
5049 			 * At this point we're not running on Tegra, so play
5050 			 * nice with multi-platform kernels.
5051 			 */
5052 			return 0;
5053 		}
5054 	} else {
5055 		/*
5056 		 * Extract information from the device tree if we've found a
5057 		 * matching node.
5058 		 */
5059 		if (of_address_to_resource(np, 0, &regs) < 0) {
5060 			pr_err("failed to get PMC registers\n");
5061 			of_node_put(np);
5062 			return -ENXIO;
5063 		}
5064 	}
5065 
5066 	early_pmc->base = ioremap(regs.start, resource_size(&regs));
5067 	if (!early_pmc->base) {
5068 		pr_err("failed to map PMC registers\n");
5069 		of_node_put(np);
5070 		return -ENXIO;
5071 	}
5072 
5073 	if (of_device_is_available(np)) {
5074 		early_pmc->soc = match->data;
5075 
5076 		err = tegra_pmc_parse_dt(early_pmc, np);
5077 		if (err < 0) {
5078 			pr_err("failed to parse DT: %d\n", err);
5079 			of_node_put(np);
5080 			return err;
5081 		}
5082 
5083 		tegra_pmc_init_common(early_pmc, np);
5084 
5085 		of_node_put(np);
5086 	}
5087 
5088 	return 0;
5089 }
5090 early_initcall(tegra_pmc_early_init);
5091 #endif /* CONFIG_ARM */
5092