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