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