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