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