blob: a4276f6bc8a37cd9fe425765ff0ed4b9cea5d826 [file] [log] [blame]
Thierry Reding72323982014-07-11 13:19:06 +02001/*
2 * drivers/soc/tegra/pmc.c
3 *
4 * Copyright (c) 2010 Google, Inc
Sandipan Patra5f84bb12018-10-24 12:38:00 +05305 * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
Thierry Reding72323982014-07-11 13:19:06 +02006 *
7 * Author:
8 * Colin Cross <ccross@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
Thierry Reding7d71e9032015-04-29 12:42:28 +020021#define pr_fmt(fmt) "tegra-pmc: " fmt
22
Thierry Reding72323982014-07-11 13:19:06 +020023#include <linux/kernel.h>
24#include <linux/clk.h>
25#include <linux/clk/tegra.h>
26#include <linux/debugfs.h>
27#include <linux/delay.h>
28#include <linux/err.h>
29#include <linux/export.h>
30#include <linux/init.h>
31#include <linux/io.h>
Jon Hunter0a2d87e2016-02-26 15:48:40 +000032#include <linux/iopoll.h>
Thierry Reding72323982014-07-11 13:19:06 +020033#include <linux/of.h>
34#include <linux/of_address.h>
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +020035#include <linux/of_clk.h>
Jon Huntera3804512016-03-30 10:15:15 +010036#include <linux/of_platform.h>
Aapo Vienamo4a37f112018-08-10 21:08:12 +030037#include <linux/pinctrl/pinctrl.h>
38#include <linux/pinctrl/pinconf.h>
39#include <linux/pinctrl/pinconf-generic.h>
Thierry Reding72323982014-07-11 13:19:06 +020040#include <linux/platform_device.h>
Jon Huntera3804512016-03-30 10:15:15 +010041#include <linux/pm_domain.h>
Thierry Reding72323982014-07-11 13:19:06 +020042#include <linux/reboot.h>
43#include <linux/reset.h>
44#include <linux/seq_file.h>
Jon Huntera3804512016-03-30 10:15:15 +010045#include <linux/slab.h>
Thierry Reding72323982014-07-11 13:19:06 +020046#include <linux/spinlock.h>
47
48#include <soc/tegra/common.h>
49#include <soc/tegra/fuse.h>
50#include <soc/tegra/pmc.h>
51
Aapo Vienamofccf0f72018-08-10 21:08:11 +030052#include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
53
Thierry Reding72323982014-07-11 13:19:06 +020054#define PMC_CNTRL 0x0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053055#define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */
Thierry Reding95b780b2016-10-10 13:13:36 +020056#define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */
57#define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */
58#define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */
59#define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */
60#define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */
61#define PMC_CNTRL_MAIN_RST BIT(4)
Thierry Reding72323982014-07-11 13:19:06 +020062
63#define DPD_SAMPLE 0x020
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053064#define DPD_SAMPLE_ENABLE BIT(0)
Thierry Reding72323982014-07-11 13:19:06 +020065#define DPD_SAMPLE_DISABLE (0 << 0)
66
67#define PWRGATE_TOGGLE 0x30
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053068#define PWRGATE_TOGGLE_START BIT(8)
Thierry Reding72323982014-07-11 13:19:06 +020069
70#define REMOVE_CLAMPING 0x34
71
72#define PWRGATE_STATUS 0x38
73
Aapo Vienamo13136a42018-08-10 21:08:07 +030074#define PMC_IMPL_E_33V_PWR 0x40
75
Laxman Dewangan21b49912016-10-10 15:14:34 +020076#define PMC_PWR_DET 0x48
77
Thierry Reding5be22552017-08-30 12:32:58 +020078#define PMC_SCRATCH0_MODE_RECOVERY BIT(31)
79#define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30)
80#define PMC_SCRATCH0_MODE_RCM BIT(1)
81#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
Thierry Reding72323982014-07-11 13:19:06 +020082 PMC_SCRATCH0_MODE_BOOTLOADER | \
83 PMC_SCRATCH0_MODE_RCM)
84
85#define PMC_CPUPWRGOOD_TIMER 0xc8
86#define PMC_CPUPWROFF_TIMER 0xcc
87
Laxman Dewangan21b49912016-10-10 15:14:34 +020088#define PMC_PWR_DET_VALUE 0xe4
89
Thierry Reding72323982014-07-11 13:19:06 +020090#define PMC_SCRATCH41 0x140
91
Mikko Perttunen3568df32015-01-06 12:52:58 +020092#define PMC_SENSOR_CTRL 0x1b0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053093#define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2)
94#define PMC_SENSOR_CTRL_ENABLE_RST BIT(1)
Mikko Perttunen3568df32015-01-06 12:52:58 +020095
Thierry Redingf5353c62015-12-30 17:13:29 +010096#define PMC_RST_STATUS_POR 0
97#define PMC_RST_STATUS_WATCHDOG 1
98#define PMC_RST_STATUS_SENSOR 2
99#define PMC_RST_STATUS_SW_MAIN 3
100#define PMC_RST_STATUS_LP0 4
101#define PMC_RST_STATUS_AOTAG 5
102
Thierry Reding72323982014-07-11 13:19:06 +0200103#define IO_DPD_REQ 0x1b8
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530104#define IO_DPD_REQ_CODE_IDLE (0U << 30)
105#define IO_DPD_REQ_CODE_OFF (1U << 30)
106#define IO_DPD_REQ_CODE_ON (2U << 30)
107#define IO_DPD_REQ_CODE_MASK (3U << 30)
Thierry Reding72323982014-07-11 13:19:06 +0200108
109#define IO_DPD_STATUS 0x1bc
110#define IO_DPD2_REQ 0x1c0
111#define IO_DPD2_STATUS 0x1c4
112#define SEL_DPD_TIM 0x1c8
113
Mikko Perttunen3568df32015-01-06 12:52:58 +0200114#define PMC_SCRATCH54 0x258
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530115#define PMC_SCRATCH54_DATA_SHIFT 8
116#define PMC_SCRATCH54_ADDR_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200117
118#define PMC_SCRATCH55 0x25c
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530119#define PMC_SCRATCH55_RESET_TEGRA BIT(31)
120#define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
121#define PMC_SCRATCH55_PINMUX_SHIFT 24
122#define PMC_SCRATCH55_16BITOP BIT(15)
123#define PMC_SCRATCH55_CHECKSUM_SHIFT 16
124#define PMC_SCRATCH55_I2CSLV1_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200125
Thierry Reding72323982014-07-11 13:19:06 +0200126#define GPU_RG_CNTRL 0x2d4
127
Thierry Redingc641ec62017-08-30 12:42:34 +0200128/* Tegra186 and later */
129#define WAKE_AOWAKE_CTRL 0x4f4
130#define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
131
Jon Huntera3804512016-03-30 10:15:15 +0100132struct tegra_powergate {
133 struct generic_pm_domain genpd;
134 struct tegra_pmc *pmc;
135 unsigned int id;
136 struct clk **clks;
137 unsigned int num_clks;
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200138 struct reset_control *reset;
Jon Huntera3804512016-03-30 10:15:15 +0100139};
140
Laxman Dewangan21b49912016-10-10 15:14:34 +0200141struct tegra_io_pad_soc {
142 enum tegra_io_pad id;
143 unsigned int dpd;
144 unsigned int voltage;
Aapo Vienamo437c4f22018-08-10 21:08:10 +0300145 const char *name;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200146};
147
Thierry Reding5be22552017-08-30 12:32:58 +0200148struct tegra_pmc_regs {
149 unsigned int scratch0;
150 unsigned int dpd_req;
151 unsigned int dpd_status;
152 unsigned int dpd2_req;
153 unsigned int dpd2_status;
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530154 unsigned int rst_status;
155 unsigned int rst_source_shift;
156 unsigned int rst_source_mask;
157 unsigned int rst_level_shift;
158 unsigned int rst_level_mask;
Thierry Reding5be22552017-08-30 12:32:58 +0200159};
160
Thierry Reding72323982014-07-11 13:19:06 +0200161struct tegra_pmc_soc {
162 unsigned int num_powergates;
163 const char *const *powergates;
164 unsigned int num_cpu_powergates;
165 const u8 *cpu_powergates;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100166
Mikko Perttunen3568df32015-01-06 12:52:58 +0200167 bool has_tsense_reset;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100168 bool has_gpu_clamps;
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200169 bool needs_mbist_war;
Aapo Vienamo13136a42018-08-10 21:08:07 +0300170 bool has_impl_33v_pwr;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200171
172 const struct tegra_io_pad_soc *io_pads;
173 unsigned int num_io_pads;
Thierry Reding5be22552017-08-30 12:32:58 +0200174
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300175 const struct pinctrl_pin_desc *pin_descs;
176 unsigned int num_pin_descs;
177
Thierry Reding5be22552017-08-30 12:32:58 +0200178 const struct tegra_pmc_regs *regs;
179 void (*init)(struct tegra_pmc *pmc);
180 void (*setup_irq_polarity)(struct tegra_pmc *pmc,
181 struct device_node *np,
182 bool invert);
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530183
184 const char * const *reset_sources;
185 unsigned int num_reset_sources;
186 const char * const *reset_levels;
187 unsigned int num_reset_levels;
188};
189
190static const char * const tegra186_reset_sources[] = {
191 "SYS_RESET",
192 "AOWDT",
193 "MCCPLEXWDT",
194 "BPMPWDT",
195 "SCEWDT",
196 "SPEWDT",
197 "APEWDT",
198 "BCCPLEXWDT",
199 "SENSOR",
200 "AOTAG",
201 "VFSENSOR",
202 "SWREST",
203 "SC7",
204 "HSM",
205 "CORESIGHT"
206};
207
208static const char * const tegra186_reset_levels[] = {
209 "L0", "L1", "L2", "WARM"
210};
211
212static const char * const tegra30_reset_sources[] = {
213 "POWER_ON_RESET",
214 "WATCHDOG",
215 "SENSOR",
216 "SW_MAIN",
217 "LP0",
218 "AOTAG"
Thierry Reding72323982014-07-11 13:19:06 +0200219};
220
221/**
222 * struct tegra_pmc - NVIDIA Tegra PMC
Jon Hunter35b67292015-12-04 14:57:03 +0000223 * @dev: pointer to PMC device structure
Thierry Reding72323982014-07-11 13:19:06 +0200224 * @base: pointer to I/O remapped register region
225 * @clk: pointer to pclk clock
Jon Hunter35b67292015-12-04 14:57:03 +0000226 * @soc: pointer to SoC data structure
Jon Hunter3195ac62015-12-04 14:57:05 +0000227 * @debugfs: pointer to debugfs entry
Thierry Reding72323982014-07-11 13:19:06 +0200228 * @rate: currently configured rate of pclk
229 * @suspend_mode: lowest suspend mode available
230 * @cpu_good_time: CPU power good time (in microseconds)
231 * @cpu_off_time: CPU power off time (in microsecends)
232 * @core_osc_time: core power good OSC time (in microseconds)
233 * @core_pmu_time: core power good PMU time (in microseconds)
234 * @core_off_time: core power off time (in microseconds)
235 * @corereq_high: core power request is active-high
236 * @sysclkreq_high: system clock request is active-high
237 * @combined_req: combined power request for CPU & core
238 * @cpu_pwr_good_en: CPU power good signal is enabled
239 * @lp0_vec_phys: physical base address of the LP0 warm boot code
240 * @lp0_vec_size: size of the LP0 warm boot code
Jon Huntera3804512016-03-30 10:15:15 +0100241 * @powergates_available: Bitmap of available power gates
Thierry Reding72323982014-07-11 13:19:06 +0200242 * @powergates_lock: mutex for power gate register access
243 */
244struct tegra_pmc {
Mikko Perttunen3568df32015-01-06 12:52:58 +0200245 struct device *dev;
Thierry Reding72323982014-07-11 13:19:06 +0200246 void __iomem *base;
Thierry Redingc641ec62017-08-30 12:42:34 +0200247 void __iomem *wake;
248 void __iomem *aotag;
Thierry Reding5be22552017-08-30 12:32:58 +0200249 void __iomem *scratch;
Thierry Reding72323982014-07-11 13:19:06 +0200250 struct clk *clk;
Jon Hunter3195ac62015-12-04 14:57:05 +0000251 struct dentry *debugfs;
Thierry Reding72323982014-07-11 13:19:06 +0200252
253 const struct tegra_pmc_soc *soc;
254
255 unsigned long rate;
256
257 enum tegra_suspend_mode suspend_mode;
258 u32 cpu_good_time;
259 u32 cpu_off_time;
260 u32 core_osc_time;
261 u32 core_pmu_time;
262 u32 core_off_time;
263 bool corereq_high;
264 bool sysclkreq_high;
265 bool combined_req;
266 bool cpu_pwr_good_en;
267 u32 lp0_vec_phys;
268 u32 lp0_vec_size;
Jon Huntera3804512016-03-30 10:15:15 +0100269 DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
Thierry Reding72323982014-07-11 13:19:06 +0200270
271 struct mutex powergates_lock;
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300272
273 struct pinctrl_dev *pctl_dev;
Thierry Reding72323982014-07-11 13:19:06 +0200274};
275
276static struct tegra_pmc *pmc = &(struct tegra_pmc) {
277 .base = NULL,
278 .suspend_mode = TEGRA_SUSPEND_NONE,
279};
280
Jon Huntera3804512016-03-30 10:15:15 +0100281static inline struct tegra_powergate *
282to_powergate(struct generic_pm_domain *domain)
283{
284 return container_of(domain, struct tegra_powergate, genpd);
285}
286
Thierry Reding72323982014-07-11 13:19:06 +0200287static u32 tegra_pmc_readl(unsigned long offset)
288{
289 return readl(pmc->base + offset);
290}
291
292static void tegra_pmc_writel(u32 value, unsigned long offset)
293{
294 writel(value, pmc->base + offset);
295}
296
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000297static inline bool tegra_powergate_state(int id)
298{
Jon Hunterbc9af232016-02-15 12:38:11 +0000299 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
300 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
301 else
302 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000303}
304
Jon Hunter0a243bd2016-02-11 18:03:24 +0000305static inline bool tegra_powergate_is_valid(int id)
306{
307 return (pmc->soc && pmc->soc->powergates[id]);
308}
309
Jon Huntera3804512016-03-30 10:15:15 +0100310static inline bool tegra_powergate_is_available(int id)
311{
312 return test_bit(id, pmc->powergates_available);
313}
314
315static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
316{
317 unsigned int i;
318
319 if (!pmc || !pmc->soc || !name)
320 return -EINVAL;
321
322 for (i = 0; i < pmc->soc->num_powergates; i++) {
323 if (!tegra_powergate_is_valid(i))
324 continue;
325
326 if (!strcmp(name, pmc->soc->powergates[i]))
327 return i;
328 }
329
Jon Huntera3804512016-03-30 10:15:15 +0100330 return -ENODEV;
331}
332
Thierry Reding72323982014-07-11 13:19:06 +0200333/**
334 * tegra_powergate_set() - set the state of a partition
335 * @id: partition ID
336 * @new_state: new state of the partition
337 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000338static int tegra_powergate_set(unsigned int id, bool new_state)
Thierry Reding72323982014-07-11 13:19:06 +0200339{
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000340 bool status;
341 int err;
342
Jon Hunterbc9af232016-02-15 12:38:11 +0000343 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
344 return -EINVAL;
345
Thierry Reding72323982014-07-11 13:19:06 +0200346 mutex_lock(&pmc->powergates_lock);
347
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000348 if (tegra_powergate_state(id) == new_state) {
Thierry Reding72323982014-07-11 13:19:06 +0200349 mutex_unlock(&pmc->powergates_lock);
350 return 0;
351 }
352
353 tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
354
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000355 err = readx_poll_timeout(tegra_powergate_state, id, status,
356 status == new_state, 10, 100000);
357
Thierry Reding72323982014-07-11 13:19:06 +0200358 mutex_unlock(&pmc->powergates_lock);
359
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000360 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200361}
362
Jon Huntera3804512016-03-30 10:15:15 +0100363static int __tegra_powergate_remove_clamping(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200364{
365 u32 mask;
366
Jon Huntere8cf6612016-02-11 18:03:21 +0000367 mutex_lock(&pmc->powergates_lock);
368
Thierry Reding72323982014-07-11 13:19:06 +0200369 /*
Thierry Redinga9a40a42015-01-09 11:15:33 +0100370 * On Tegra124 and later, the clamps for the GPU are controlled by a
371 * separate register (with different semantics).
Thierry Reding72323982014-07-11 13:19:06 +0200372 */
Thierry Redinga9a40a42015-01-09 11:15:33 +0100373 if (id == TEGRA_POWERGATE_3D) {
374 if (pmc->soc->has_gpu_clamps) {
Thierry Reding72323982014-07-11 13:19:06 +0200375 tegra_pmc_writel(0, GPU_RG_CNTRL);
Jon Huntere8cf6612016-02-11 18:03:21 +0000376 goto out;
Thierry Reding72323982014-07-11 13:19:06 +0200377 }
378 }
379
380 /*
381 * Tegra 2 has a bug where PCIE and VDE clamping masks are
382 * swapped relatively to the partition ids
383 */
384 if (id == TEGRA_POWERGATE_VDEC)
385 mask = (1 << TEGRA_POWERGATE_PCIE);
386 else if (id == TEGRA_POWERGATE_PCIE)
387 mask = (1 << TEGRA_POWERGATE_VDEC);
388 else
389 mask = (1 << id);
390
391 tegra_pmc_writel(mask, REMOVE_CLAMPING);
392
Jon Huntere8cf6612016-02-11 18:03:21 +0000393out:
394 mutex_unlock(&pmc->powergates_lock);
395
Thierry Reding72323982014-07-11 13:19:06 +0200396 return 0;
397}
Jon Huntera3804512016-03-30 10:15:15 +0100398
399static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
400{
401 unsigned int i;
402
403 for (i = 0; i < pg->num_clks; i++)
404 clk_disable_unprepare(pg->clks[i]);
405}
406
407static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
408{
409 unsigned int i;
410 int err;
411
412 for (i = 0; i < pg->num_clks; i++) {
413 err = clk_prepare_enable(pg->clks[i]);
414 if (err)
415 goto out;
416 }
417
418 return 0;
419
420out:
421 while (i--)
422 clk_disable_unprepare(pg->clks[i]);
423
424 return err;
425}
426
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200427int __weak tegra210_clk_handle_mbist_war(unsigned int id)
428{
429 return 0;
430}
431
Jon Huntera3804512016-03-30 10:15:15 +0100432static int tegra_powergate_power_up(struct tegra_powergate *pg,
433 bool disable_clocks)
434{
435 int err;
436
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200437 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100438 if (err)
439 return err;
440
441 usleep_range(10, 20);
442
443 err = tegra_powergate_set(pg->id, true);
444 if (err < 0)
445 return err;
446
447 usleep_range(10, 20);
448
449 err = tegra_powergate_enable_clocks(pg);
450 if (err)
451 goto disable_clks;
452
453 usleep_range(10, 20);
454
455 err = __tegra_powergate_remove_clamping(pg->id);
456 if (err)
457 goto disable_clks;
458
459 usleep_range(10, 20);
460
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200461 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100462 if (err)
463 goto powergate_off;
464
465 usleep_range(10, 20);
466
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200467 if (pg->pmc->soc->needs_mbist_war)
468 err = tegra210_clk_handle_mbist_war(pg->id);
469 if (err)
470 goto disable_clks;
471
Jon Huntera3804512016-03-30 10:15:15 +0100472 if (disable_clocks)
473 tegra_powergate_disable_clocks(pg);
474
475 return 0;
476
477disable_clks:
478 tegra_powergate_disable_clocks(pg);
479 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200480
Jon Huntera3804512016-03-30 10:15:15 +0100481powergate_off:
482 tegra_powergate_set(pg->id, false);
483
484 return err;
485}
486
487static int tegra_powergate_power_down(struct tegra_powergate *pg)
488{
489 int err;
490
491 err = tegra_powergate_enable_clocks(pg);
492 if (err)
493 return err;
494
495 usleep_range(10, 20);
496
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200497 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100498 if (err)
499 goto disable_clks;
500
501 usleep_range(10, 20);
502
503 tegra_powergate_disable_clocks(pg);
504
505 usleep_range(10, 20);
506
507 err = tegra_powergate_set(pg->id, false);
508 if (err)
509 goto assert_resets;
510
511 return 0;
512
513assert_resets:
514 tegra_powergate_enable_clocks(pg);
515 usleep_range(10, 20);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200516 reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100517 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200518
Jon Huntera3804512016-03-30 10:15:15 +0100519disable_clks:
520 tegra_powergate_disable_clocks(pg);
521
522 return err;
523}
524
525static int tegra_genpd_power_on(struct generic_pm_domain *domain)
526{
527 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100528 int err;
529
530 err = tegra_powergate_power_up(pg, true);
531 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100532 pr_err("failed to turn on PM domain %s: %d\n", pg->genpd.name,
533 err);
Jon Huntera3804512016-03-30 10:15:15 +0100534
535 return err;
536}
537
538static int tegra_genpd_power_off(struct generic_pm_domain *domain)
539{
540 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100541 int err;
542
543 err = tegra_powergate_power_down(pg);
544 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100545 pr_err("failed to turn off PM domain %s: %d\n",
546 pg->genpd.name, err);
Jon Huntera3804512016-03-30 10:15:15 +0100547
548 return err;
549}
550
551/**
552 * tegra_powergate_power_on() - power on partition
553 * @id: partition ID
554 */
555int tegra_powergate_power_on(unsigned int id)
556{
557 if (!tegra_powergate_is_available(id))
558 return -EINVAL;
559
560 return tegra_powergate_set(id, true);
561}
562
563/**
564 * tegra_powergate_power_off() - power off partition
565 * @id: partition ID
566 */
567int tegra_powergate_power_off(unsigned int id)
568{
569 if (!tegra_powergate_is_available(id))
570 return -EINVAL;
571
572 return tegra_powergate_set(id, false);
573}
574EXPORT_SYMBOL(tegra_powergate_power_off);
575
576/**
577 * tegra_powergate_is_powered() - check if partition is powered
578 * @id: partition ID
579 */
580int tegra_powergate_is_powered(unsigned int id)
581{
Jon Huntera3804512016-03-30 10:15:15 +0100582 if (!tegra_powergate_is_valid(id))
583 return -EINVAL;
584
Dmitry Osipenkob6e1fd12018-10-21 21:36:14 +0300585 return tegra_powergate_state(id);
Jon Huntera3804512016-03-30 10:15:15 +0100586}
587
588/**
589 * tegra_powergate_remove_clamping() - remove power clamps for partition
590 * @id: partition ID
591 */
592int tegra_powergate_remove_clamping(unsigned int id)
593{
594 if (!tegra_powergate_is_available(id))
595 return -EINVAL;
596
597 return __tegra_powergate_remove_clamping(id);
598}
Thierry Reding72323982014-07-11 13:19:06 +0200599EXPORT_SYMBOL(tegra_powergate_remove_clamping);
600
601/**
602 * tegra_powergate_sequence_power_up() - power up partition
603 * @id: partition ID
604 * @clk: clock for partition
605 * @rst: reset for partition
606 *
607 * Must be called with clk disabled, and returns with clk enabled.
608 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000609int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
Thierry Reding72323982014-07-11 13:19:06 +0200610 struct reset_control *rst)
611{
Viresh Kumar495ac332018-05-03 13:56:17 +0530612 struct tegra_powergate *pg;
Jon Huntera3804512016-03-30 10:15:15 +0100613 int err;
Thierry Reding72323982014-07-11 13:19:06 +0200614
Jon Hunter403db2d2016-06-28 11:38:23 +0100615 if (!tegra_powergate_is_available(id))
616 return -EINVAL;
617
Viresh Kumar495ac332018-05-03 13:56:17 +0530618 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
619 if (!pg)
620 return -ENOMEM;
Thierry Reding72323982014-07-11 13:19:06 +0200621
Viresh Kumar495ac332018-05-03 13:56:17 +0530622 pg->id = id;
623 pg->clks = &clk;
624 pg->num_clks = 1;
625 pg->reset = rst;
626 pg->pmc = pmc;
627
628 err = tegra_powergate_power_up(pg, false);
Jon Huntera3804512016-03-30 10:15:15 +0100629 if (err)
630 pr_err("failed to turn on partition %d: %d\n", id, err);
Thierry Reding72323982014-07-11 13:19:06 +0200631
Viresh Kumar495ac332018-05-03 13:56:17 +0530632 kfree(pg);
633
Jon Huntera3804512016-03-30 10:15:15 +0100634 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200635}
636EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
637
638#ifdef CONFIG_SMP
639/**
640 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
641 * @cpuid: CPU partition ID
642 *
643 * Returns the partition ID corresponding to the CPU partition ID or a
644 * negative error code on failure.
645 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000646static int tegra_get_cpu_powergate_id(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200647{
Jon Hunter70293ed2016-02-11 18:03:22 +0000648 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
Thierry Reding72323982014-07-11 13:19:06 +0200649 return pmc->soc->cpu_powergates[cpuid];
650
651 return -EINVAL;
652}
653
654/**
655 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
656 * @cpuid: CPU partition ID
657 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000658bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200659{
660 int id;
661
662 id = tegra_get_cpu_powergate_id(cpuid);
663 if (id < 0)
664 return false;
665
666 return tegra_powergate_is_powered(id);
667}
668
669/**
670 * tegra_pmc_cpu_power_on() - power on CPU partition
671 * @cpuid: CPU partition ID
672 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000673int tegra_pmc_cpu_power_on(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200674{
675 int id;
676
677 id = tegra_get_cpu_powergate_id(cpuid);
678 if (id < 0)
679 return id;
680
681 return tegra_powergate_set(id, true);
682}
683
684/**
685 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
686 * @cpuid: CPU partition ID
687 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000688int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200689{
690 int id;
691
692 id = tegra_get_cpu_powergate_id(cpuid);
693 if (id < 0)
694 return id;
695
696 return tegra_powergate_remove_clamping(id);
697}
698#endif /* CONFIG_SMP */
699
David Riley78921582015-03-18 10:52:25 +0100700static int tegra_pmc_restart_notify(struct notifier_block *this,
701 unsigned long action, void *data)
Thierry Reding72323982014-07-11 13:19:06 +0200702{
David Riley78921582015-03-18 10:52:25 +0100703 const char *cmd = data;
Thierry Reding72323982014-07-11 13:19:06 +0200704 u32 value;
705
Thierry Reding5be22552017-08-30 12:32:58 +0200706 value = readl(pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200707 value &= ~PMC_SCRATCH0_MODE_MASK;
708
709 if (cmd) {
710 if (strcmp(cmd, "recovery") == 0)
711 value |= PMC_SCRATCH0_MODE_RECOVERY;
712
713 if (strcmp(cmd, "bootloader") == 0)
714 value |= PMC_SCRATCH0_MODE_BOOTLOADER;
715
716 if (strcmp(cmd, "forced-recovery") == 0)
717 value |= PMC_SCRATCH0_MODE_RCM;
718 }
719
Thierry Reding5be22552017-08-30 12:32:58 +0200720 writel(value, pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200721
Thierry Redingf5353c62015-12-30 17:13:29 +0100722 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
723 value = tegra_pmc_readl(PMC_CNTRL);
724 value |= PMC_CNTRL_MAIN_RST;
725 tegra_pmc_writel(value, PMC_CNTRL);
David Riley78921582015-03-18 10:52:25 +0100726
727 return NOTIFY_DONE;
Thierry Reding72323982014-07-11 13:19:06 +0200728}
729
David Riley78921582015-03-18 10:52:25 +0100730static struct notifier_block tegra_pmc_restart_handler = {
731 .notifier_call = tegra_pmc_restart_notify,
732 .priority = 128,
733};
734
Thierry Reding72323982014-07-11 13:19:06 +0200735static int powergate_show(struct seq_file *s, void *data)
736{
737 unsigned int i;
Jon Hunterc3ea2972016-02-11 18:03:25 +0000738 int status;
Thierry Reding72323982014-07-11 13:19:06 +0200739
740 seq_printf(s, " powergate powered\n");
741 seq_printf(s, "------------------\n");
742
743 for (i = 0; i < pmc->soc->num_powergates; i++) {
Jon Hunterc3ea2972016-02-11 18:03:25 +0000744 status = tegra_powergate_is_powered(i);
745 if (status < 0)
Thierry Reding72323982014-07-11 13:19:06 +0200746 continue;
747
748 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
Jon Hunterc3ea2972016-02-11 18:03:25 +0000749 status ? "yes" : "no");
Thierry Reding72323982014-07-11 13:19:06 +0200750 }
751
752 return 0;
753}
754
Yangtao Li57ba33d2018-11-22 08:12:07 -0500755DEFINE_SHOW_ATTRIBUTE(powergate);
Thierry Reding72323982014-07-11 13:19:06 +0200756
757static int tegra_powergate_debugfs_init(void)
758{
Jon Hunter3195ac62015-12-04 14:57:05 +0000759 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
760 &powergate_fops);
761 if (!pmc->debugfs)
Thierry Reding72323982014-07-11 13:19:06 +0200762 return -ENOMEM;
763
764 return 0;
765}
766
Jon Huntera3804512016-03-30 10:15:15 +0100767static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
768 struct device_node *np)
769{
770 struct clk *clk;
771 unsigned int i, count;
772 int err;
773
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +0200774 count = of_clk_get_parent_count(np);
Jon Huntera3804512016-03-30 10:15:15 +0100775 if (count == 0)
776 return -ENODEV;
777
778 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
779 if (!pg->clks)
780 return -ENOMEM;
781
782 for (i = 0; i < count; i++) {
783 pg->clks[i] = of_clk_get(np, i);
784 if (IS_ERR(pg->clks[i])) {
785 err = PTR_ERR(pg->clks[i]);
786 goto err;
787 }
788 }
789
790 pg->num_clks = count;
791
792 return 0;
793
794err:
795 while (i--)
796 clk_put(pg->clks[i]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200797
Jon Huntera3804512016-03-30 10:15:15 +0100798 kfree(pg->clks);
799
800 return err;
801}
802
803static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
Jon Hunter05cfb982016-06-29 10:17:47 +0100804 struct device_node *np, bool off)
Jon Huntera3804512016-03-30 10:15:15 +0100805{
Jon Huntera3804512016-03-30 10:15:15 +0100806 int err;
807
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200808 pg->reset = of_reset_control_array_get_exclusive(np);
809 if (IS_ERR(pg->reset)) {
810 err = PTR_ERR(pg->reset);
811 pr_err("failed to get device resets: %d\n", err);
812 return err;
Jon Huntera3804512016-03-30 10:15:15 +0100813 }
814
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200815 if (off)
816 err = reset_control_assert(pg->reset);
817 else
818 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100819
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200820 if (err)
821 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100822
823 return err;
824}
825
826static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
827{
828 struct tegra_powergate *pg;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100829 int id, err;
Jon Huntera3804512016-03-30 10:15:15 +0100830 bool off;
Jon Huntera3804512016-03-30 10:15:15 +0100831
832 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
833 if (!pg)
Jon Hunterc2710ac2016-06-30 11:56:24 +0100834 return;
Jon Huntera3804512016-03-30 10:15:15 +0100835
836 id = tegra_powergate_lookup(pmc, np->name);
Jon Hunterc2710ac2016-06-30 11:56:24 +0100837 if (id < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500838 pr_err("powergate lookup failed for %pOFn: %d\n", np, id);
Jon Huntera3804512016-03-30 10:15:15 +0100839 goto free_mem;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100840 }
Jon Huntera3804512016-03-30 10:15:15 +0100841
842 /*
843 * Clear the bit for this powergate so it cannot be managed
844 * directly via the legacy APIs for controlling powergates.
845 */
846 clear_bit(id, pmc->powergates_available);
847
848 pg->id = id;
849 pg->genpd.name = np->name;
850 pg->genpd.power_off = tegra_genpd_power_off;
851 pg->genpd.power_on = tegra_genpd_power_on;
852 pg->pmc = pmc;
853
Jon Hunter05cfb982016-06-29 10:17:47 +0100854 off = !tegra_powergate_is_powered(pg->id);
855
Jon Hunterc2710ac2016-06-30 11:56:24 +0100856 err = tegra_powergate_of_get_clks(pg, np);
857 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500858 pr_err("failed to get clocks for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100859 goto set_available;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100860 }
Jon Huntera3804512016-03-30 10:15:15 +0100861
Jon Hunterc2710ac2016-06-30 11:56:24 +0100862 err = tegra_powergate_of_get_resets(pg, np, off);
863 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500864 pr_err("failed to get resets for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100865 goto remove_clks;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100866 }
Jon Huntera3804512016-03-30 10:15:15 +0100867
Jon Hunter0b137342016-10-22 20:23:56 +0100868 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
869 if (off)
870 WARN_ON(tegra_powergate_power_up(pg, true));
871
872 goto remove_resets;
873 }
Jon Huntere2d17962016-06-30 11:56:25 +0100874
Jon Huntercd5ceda2016-10-22 20:23:55 +0100875 err = pm_genpd_init(&pg->genpd, NULL, off);
876 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500877 pr_err("failed to initialise PM domain %pOFn: %d\n", np,
Jon Huntercd5ceda2016-10-22 20:23:55 +0100878 err);
879 goto remove_resets;
880 }
Jon Huntera3804512016-03-30 10:15:15 +0100881
Jon Hunterc2710ac2016-06-30 11:56:24 +0100882 err = of_genpd_add_provider_simple(np, &pg->genpd);
883 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500884 pr_err("failed to add PM domain provider for %pOFn: %d\n",
885 np, err);
Jon Hunter0b137342016-10-22 20:23:56 +0100886 goto remove_genpd;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100887 }
Jon Huntera3804512016-03-30 10:15:15 +0100888
Thierry Reding45221122016-11-08 11:05:03 +0100889 pr_debug("added PM domain %s\n", pg->genpd.name);
Jon Huntera3804512016-03-30 10:15:15 +0100890
891 return;
892
Jon Hunter0b137342016-10-22 20:23:56 +0100893remove_genpd:
894 pm_genpd_remove(&pg->genpd);
Jon Huntere2d17962016-06-30 11:56:25 +0100895
Jon Huntera3804512016-03-30 10:15:15 +0100896remove_resets:
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200897 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100898
899remove_clks:
900 while (pg->num_clks--)
901 clk_put(pg->clks[pg->num_clks]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200902
Jon Huntera3804512016-03-30 10:15:15 +0100903 kfree(pg->clks);
904
905set_available:
906 set_bit(id, pmc->powergates_available);
907
908free_mem:
909 kfree(pg);
Jon Huntera3804512016-03-30 10:15:15 +0100910}
911
Jon Huntere2d17962016-06-30 11:56:25 +0100912static void tegra_powergate_init(struct tegra_pmc *pmc,
913 struct device_node *parent)
Jon Huntera3804512016-03-30 10:15:15 +0100914{
915 struct device_node *np, *child;
Jon Huntere2d17962016-06-30 11:56:25 +0100916 unsigned int i;
Jon Huntera3804512016-03-30 10:15:15 +0100917
Jon Huntere2d17962016-06-30 11:56:25 +0100918 /* Create a bitmap of the available and valid partitions */
919 for (i = 0; i < pmc->soc->num_powergates; i++)
920 if (pmc->soc->powergates[i])
921 set_bit(i, pmc->powergates_available);
922
923 np = of_get_child_by_name(parent, "powergates");
Jon Huntera3804512016-03-30 10:15:15 +0100924 if (!np)
925 return;
926
Tuomas Tynkkynen0c106e52017-07-29 02:58:43 +0300927 for_each_child_of_node(np, child)
Jon Huntera3804512016-03-30 10:15:15 +0100928 tegra_powergate_add(pmc, child);
Jon Huntera3804512016-03-30 10:15:15 +0100929
930 of_node_put(np);
931}
932
Laxman Dewangan21b49912016-10-10 15:14:34 +0200933static const struct tegra_io_pad_soc *
934tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +0200935{
Laxman Dewangan21b49912016-10-10 15:14:34 +0200936 unsigned int i;
937
938 for (i = 0; i < pmc->soc->num_io_pads; i++)
939 if (pmc->soc->io_pads[i].id == id)
940 return &pmc->soc->io_pads[i];
941
942 return NULL;
943}
944
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300945static int tegra_io_pad_get_dpd_register_bit(enum tegra_io_pad id,
946 unsigned long *request,
947 unsigned long *status,
948 u32 *mask)
Laxman Dewangan21b49912016-10-10 15:14:34 +0200949{
950 const struct tegra_io_pad_soc *pad;
Thierry Reding72323982014-07-11 13:19:06 +0200951
Laxman Dewangan21b49912016-10-10 15:14:34 +0200952 pad = tegra_io_pad_find(pmc, id);
Thierry Reding54e24722016-11-08 10:58:32 +0100953 if (!pad) {
954 pr_err("invalid I/O pad ID %u\n", id);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200955 return -ENOENT;
Thierry Reding54e24722016-11-08 10:58:32 +0100956 }
Thierry Reding72323982014-07-11 13:19:06 +0200957
Laxman Dewangan21b49912016-10-10 15:14:34 +0200958 if (pad->dpd == UINT_MAX)
959 return -ENOTSUPP;
Thierry Reding72323982014-07-11 13:19:06 +0200960
Jon Hunter27b12b4e2016-10-22 20:23:53 +0100961 *mask = BIT(pad->dpd % 32);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200962
963 if (pad->dpd < 32) {
Thierry Reding5be22552017-08-30 12:32:58 +0200964 *status = pmc->soc->regs->dpd_status;
965 *request = pmc->soc->regs->dpd_req;
Thierry Reding72323982014-07-11 13:19:06 +0200966 } else {
Thierry Reding5be22552017-08-30 12:32:58 +0200967 *status = pmc->soc->regs->dpd2_status;
968 *request = pmc->soc->regs->dpd2_req;
Thierry Reding72323982014-07-11 13:19:06 +0200969 }
970
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300971 return 0;
972}
973
974static int tegra_io_pad_prepare(enum tegra_io_pad id, unsigned long *request,
975 unsigned long *status, u32 *mask)
976{
977 unsigned long rate, value;
978 int err;
979
980 err = tegra_io_pad_get_dpd_register_bit(id, request, status, mask);
981 if (err)
982 return err;
983
Thierry Reding5be22552017-08-30 12:32:58 +0200984 if (pmc->clk) {
985 rate = clk_get_rate(pmc->clk);
986 if (!rate) {
987 pr_err("failed to get clock rate\n");
988 return -ENODEV;
989 }
990
991 tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
992
993 /* must be at least 200 ns, in APB (PCLK) clock cycles */
994 value = DIV_ROUND_UP(1000000000, rate);
995 value = DIV_ROUND_UP(200, value);
996 tegra_pmc_writel(value, SEL_DPD_TIM);
Thierry Reding54e24722016-11-08 10:58:32 +0100997 }
Thierry Reding72323982014-07-11 13:19:06 +0200998
Thierry Reding72323982014-07-11 13:19:06 +0200999 return 0;
1000}
1001
Laxman Dewangan21b49912016-10-10 15:14:34 +02001002static int tegra_io_pad_poll(unsigned long offset, u32 mask,
1003 u32 val, unsigned long timeout)
Thierry Reding72323982014-07-11 13:19:06 +02001004{
Laxman Dewangan84cf85e2016-06-17 18:36:13 +05301005 u32 value;
Thierry Reding72323982014-07-11 13:19:06 +02001006
1007 timeout = jiffies + msecs_to_jiffies(timeout);
1008
1009 while (time_after(timeout, jiffies)) {
1010 value = tegra_pmc_readl(offset);
1011 if ((value & mask) == val)
1012 return 0;
1013
1014 usleep_range(250, 1000);
1015 }
1016
1017 return -ETIMEDOUT;
1018}
1019
Laxman Dewangan21b49912016-10-10 15:14:34 +02001020static void tegra_io_pad_unprepare(void)
Thierry Reding72323982014-07-11 13:19:06 +02001021{
Thierry Reding5be22552017-08-30 12:32:58 +02001022 if (pmc->clk)
1023 tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
Thierry Reding72323982014-07-11 13:19:06 +02001024}
1025
Laxman Dewangan21b49912016-10-10 15:14:34 +02001026/**
1027 * tegra_io_pad_power_enable() - enable power to I/O pad
1028 * @id: Tegra I/O pad ID for which to enable power
1029 *
1030 * Returns: 0 on success or a negative error code on failure.
1031 */
1032int tegra_io_pad_power_enable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001033{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001034 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001035 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001036 int err;
1037
Jon Huntere8cf6612016-02-11 18:03:21 +00001038 mutex_lock(&pmc->powergates_lock);
1039
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001040 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001041 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001042 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001043 goto unlock;
1044 }
Thierry Reding72323982014-07-11 13:19:06 +02001045
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001046 tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001047
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001048 err = tegra_io_pad_poll(status, mask, 0, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001049 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001050 pr_err("failed to enable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001051 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001052 }
Thierry Reding72323982014-07-11 13:19:06 +02001053
Laxman Dewangan21b49912016-10-10 15:14:34 +02001054 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001055
Laxman Dewangan21b49912016-10-10 15:14:34 +02001056unlock:
Jon Huntere8cf6612016-02-11 18:03:21 +00001057 mutex_unlock(&pmc->powergates_lock);
Jon Huntere8cf6612016-02-11 18:03:21 +00001058 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001059}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001060EXPORT_SYMBOL(tegra_io_pad_power_enable);
Thierry Reding72323982014-07-11 13:19:06 +02001061
Laxman Dewangan21b49912016-10-10 15:14:34 +02001062/**
1063 * tegra_io_pad_power_disable() - disable power to I/O pad
1064 * @id: Tegra I/O pad ID for which to disable power
1065 *
1066 * Returns: 0 on success or a negative error code on failure.
1067 */
1068int tegra_io_pad_power_disable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001069{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001070 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001071 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001072 int err;
1073
Jon Huntere8cf6612016-02-11 18:03:21 +00001074 mutex_lock(&pmc->powergates_lock);
1075
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001076 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001077 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001078 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001079 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001080 }
Thierry Reding72323982014-07-11 13:19:06 +02001081
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001082 tegra_pmc_writel(IO_DPD_REQ_CODE_ON | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001083
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001084 err = tegra_io_pad_poll(status, mask, mask, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001085 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001086 pr_err("failed to disable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001087 goto unlock;
1088 }
Thierry Reding72323982014-07-11 13:19:06 +02001089
Laxman Dewangan21b49912016-10-10 15:14:34 +02001090 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001091
Laxman Dewangan21b49912016-10-10 15:14:34 +02001092unlock:
1093 mutex_unlock(&pmc->powergates_lock);
1094 return err;
1095}
1096EXPORT_SYMBOL(tegra_io_pad_power_disable);
1097
Aapo Vienamof142b9d2018-08-10 21:08:09 +03001098static int tegra_io_pad_is_powered(enum tegra_io_pad id)
1099{
1100 unsigned long request, status;
1101 u32 mask, value;
1102 int err;
1103
1104 err = tegra_io_pad_get_dpd_register_bit(id, &request, &status, &mask);
1105 if (err)
1106 return err;
1107
1108 value = tegra_pmc_readl(status);
1109
1110 return !(value & mask);
1111}
1112
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001113static int tegra_io_pad_set_voltage(enum tegra_io_pad id, int voltage)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001114{
1115 const struct tegra_io_pad_soc *pad;
1116 u32 value;
1117
1118 pad = tegra_io_pad_find(pmc, id);
1119 if (!pad)
1120 return -ENOENT;
1121
1122 if (pad->voltage == UINT_MAX)
1123 return -ENOTSUPP;
1124
1125 mutex_lock(&pmc->powergates_lock);
1126
Aapo Vienamo13136a42018-08-10 21:08:07 +03001127 if (pmc->soc->has_impl_33v_pwr) {
1128 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001129
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001130 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001131 value &= ~BIT(pad->voltage);
1132 else
1133 value |= BIT(pad->voltage);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001134
Aapo Vienamo13136a42018-08-10 21:08:07 +03001135 tegra_pmc_writel(value, PMC_IMPL_E_33V_PWR);
1136 } else {
1137 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1138 value = tegra_pmc_readl(PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001139 value |= BIT(pad->voltage);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001140 tegra_pmc_writel(value, PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001141
Aapo Vienamo13136a42018-08-10 21:08:07 +03001142 /* update I/O voltage */
1143 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
1144
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001145 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001146 value &= ~BIT(pad->voltage);
1147 else
1148 value |= BIT(pad->voltage);
1149
1150 tegra_pmc_writel(value, PMC_PWR_DET_VALUE);
1151 }
Laxman Dewangan21b49912016-10-10 15:14:34 +02001152
Jon Huntere8cf6612016-02-11 18:03:21 +00001153 mutex_unlock(&pmc->powergates_lock);
1154
Laxman Dewangan21b49912016-10-10 15:14:34 +02001155 usleep_range(100, 250);
1156
1157 return 0;
1158}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001159
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001160static int tegra_io_pad_get_voltage(enum tegra_io_pad id)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001161{
1162 const struct tegra_io_pad_soc *pad;
1163 u32 value;
1164
1165 pad = tegra_io_pad_find(pmc, id);
1166 if (!pad)
1167 return -ENOENT;
1168
1169 if (pad->voltage == UINT_MAX)
1170 return -ENOTSUPP;
1171
Aapo Vienamo13136a42018-08-10 21:08:07 +03001172 if (pmc->soc->has_impl_33v_pwr)
1173 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
1174 else
1175 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001176
1177 if ((value & BIT(pad->voltage)) == 0)
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001178 return TEGRA_IO_PAD_VOLTAGE_1V8;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001179
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001180 return TEGRA_IO_PAD_VOLTAGE_3V3;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001181}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001182
1183/**
1184 * tegra_io_rail_power_on() - enable power to I/O rail
1185 * @id: Tegra I/O pad ID for which to enable power
1186 *
1187 * See also: tegra_io_pad_power_enable()
1188 */
1189int tegra_io_rail_power_on(unsigned int id)
1190{
1191 return tegra_io_pad_power_enable(id);
1192}
1193EXPORT_SYMBOL(tegra_io_rail_power_on);
1194
1195/**
1196 * tegra_io_rail_power_off() - disable power to I/O rail
1197 * @id: Tegra I/O pad ID for which to disable power
1198 *
1199 * See also: tegra_io_pad_power_disable()
1200 */
1201int tegra_io_rail_power_off(unsigned int id)
1202{
1203 return tegra_io_pad_power_disable(id);
Thierry Reding72323982014-07-11 13:19:06 +02001204}
1205EXPORT_SYMBOL(tegra_io_rail_power_off);
1206
1207#ifdef CONFIG_PM_SLEEP
1208enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1209{
1210 return pmc->suspend_mode;
1211}
1212
1213void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1214{
1215 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1216 return;
1217
1218 pmc->suspend_mode = mode;
1219}
1220
1221void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1222{
1223 unsigned long long rate = 0;
1224 u32 value;
1225
1226 switch (mode) {
1227 case TEGRA_SUSPEND_LP1:
1228 rate = 32768;
1229 break;
1230
1231 case TEGRA_SUSPEND_LP2:
1232 rate = clk_get_rate(pmc->clk);
1233 break;
1234
1235 default:
1236 break;
1237 }
1238
1239 if (WARN_ON_ONCE(rate == 0))
1240 rate = 100000000;
1241
1242 if (rate != pmc->rate) {
1243 u64 ticks;
1244
1245 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1246 do_div(ticks, USEC_PER_SEC);
1247 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1248
1249 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1250 do_div(ticks, USEC_PER_SEC);
1251 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1252
1253 wmb();
1254
1255 pmc->rate = rate;
1256 }
1257
1258 value = tegra_pmc_readl(PMC_CNTRL);
1259 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1260 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1261 tegra_pmc_writel(value, PMC_CNTRL);
1262}
1263#endif
1264
1265static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1266{
1267 u32 value, values[2];
1268
1269 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1270 } else {
1271 switch (value) {
1272 case 0:
1273 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1274 break;
1275
1276 case 1:
1277 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1278 break;
1279
1280 case 2:
1281 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1282 break;
1283
1284 default:
1285 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1286 break;
1287 }
1288 }
1289
1290 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1291
1292 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1293 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1294
1295 pmc->cpu_good_time = value;
1296
1297 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1298 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1299
1300 pmc->cpu_off_time = value;
1301
1302 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1303 values, ARRAY_SIZE(values)))
1304 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1305
1306 pmc->core_osc_time = values[0];
1307 pmc->core_pmu_time = values[1];
1308
1309 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1310 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1311
1312 pmc->core_off_time = value;
1313
1314 pmc->corereq_high = of_property_read_bool(np,
1315 "nvidia,core-power-req-active-high");
1316
1317 pmc->sysclkreq_high = of_property_read_bool(np,
1318 "nvidia,sys-clock-req-active-high");
1319
1320 pmc->combined_req = of_property_read_bool(np,
1321 "nvidia,combined-power-req");
1322
1323 pmc->cpu_pwr_good_en = of_property_read_bool(np,
1324 "nvidia,cpu-pwr-good-en");
1325
1326 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1327 ARRAY_SIZE(values)))
1328 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1329 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1330
1331 pmc->lp0_vec_phys = values[0];
1332 pmc->lp0_vec_size = values[1];
1333
1334 return 0;
1335}
1336
1337static void tegra_pmc_init(struct tegra_pmc *pmc)
1338{
Thierry Reding5be22552017-08-30 12:32:58 +02001339 if (pmc->soc->init)
1340 pmc->soc->init(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001341}
1342
Jon Hunter1e52efdf2015-12-04 14:57:04 +00001343static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
Mikko Perttunen3568df32015-01-06 12:52:58 +02001344{
1345 static const char disabled[] = "emergency thermal reset disabled";
1346 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1347 struct device *dev = pmc->dev;
1348 struct device_node *np;
1349 u32 value, checksum;
1350
1351 if (!pmc->soc->has_tsense_reset)
Thierry Reding95169cd2015-07-09 09:59:55 +02001352 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001353
Johan Hovold1dc6bd52017-11-15 10:44:58 +01001354 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
Mikko Perttunen3568df32015-01-06 12:52:58 +02001355 if (!np) {
1356 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
Thierry Reding95169cd2015-07-09 09:59:55 +02001357 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001358 }
1359
1360 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1361 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1362 goto out;
1363 }
1364
1365 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1366 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1367 goto out;
1368 }
1369
1370 if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1371 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1372 goto out;
1373 }
1374
1375 if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1376 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1377 goto out;
1378 }
1379
1380 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1381 pinmux = 0;
1382
1383 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1384 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1385 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1386
1387 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1388 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1389 tegra_pmc_writel(value, PMC_SCRATCH54);
1390
1391 value = PMC_SCRATCH55_RESET_TEGRA;
1392 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1393 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1394 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1395
1396 /*
1397 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1398 * contain the checksum and are currently zero, so they are not added.
1399 */
1400 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1401 + ((value >> 24) & 0xff);
1402 checksum &= 0xff;
1403 checksum = 0x100 - checksum;
1404
1405 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1406
1407 tegra_pmc_writel(value, PMC_SCRATCH55);
1408
1409 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1410 value |= PMC_SENSOR_CTRL_ENABLE_RST;
1411 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1412
1413 dev_info(pmc->dev, "emergency thermal reset enabled\n");
1414
1415out:
1416 of_node_put(np);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001417}
1418
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001419static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
1420{
1421 return pmc->soc->num_io_pads;
1422}
1423
1424static const char *tegra_io_pad_pinctrl_get_group_name(
1425 struct pinctrl_dev *pctl, unsigned int group)
1426{
1427 return pmc->soc->io_pads[group].name;
1428}
1429
1430static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
1431 unsigned int group,
1432 const unsigned int **pins,
1433 unsigned int *num_pins)
1434{
1435 *pins = &pmc->soc->io_pads[group].id;
1436 *num_pins = 1;
1437 return 0;
1438}
1439
1440static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
1441 .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
1442 .get_group_name = tegra_io_pad_pinctrl_get_group_name,
1443 .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
1444 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1445 .dt_free_map = pinconf_generic_dt_free_map,
1446};
1447
1448static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
1449 unsigned int pin, unsigned long *config)
1450{
1451 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1452 enum pin_config_param param = pinconf_to_config_param(*config);
1453 int ret;
1454 u32 arg;
1455
1456 if (!pad)
1457 return -EINVAL;
1458
1459 switch (param) {
1460 case PIN_CONFIG_POWER_SOURCE:
1461 ret = tegra_io_pad_get_voltage(pad->id);
1462 if (ret < 0)
1463 return ret;
1464 arg = ret;
1465 break;
1466 case PIN_CONFIG_LOW_POWER_MODE:
1467 ret = tegra_io_pad_is_powered(pad->id);
1468 if (ret < 0)
1469 return ret;
1470 arg = !ret;
1471 break;
1472 default:
1473 return -EINVAL;
1474 }
1475
1476 *config = pinconf_to_config_packed(param, arg);
1477
1478 return 0;
1479}
1480
1481static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
1482 unsigned int pin, unsigned long *configs,
1483 unsigned int num_configs)
1484{
1485 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1486 enum pin_config_param param;
1487 unsigned int i;
1488 int err;
1489 u32 arg;
1490
1491 if (!pad)
1492 return -EINVAL;
1493
1494 for (i = 0; i < num_configs; ++i) {
1495 param = pinconf_to_config_param(configs[i]);
1496 arg = pinconf_to_config_argument(configs[i]);
1497
1498 switch (param) {
1499 case PIN_CONFIG_LOW_POWER_MODE:
1500 if (arg)
1501 err = tegra_io_pad_power_disable(pad->id);
1502 else
1503 err = tegra_io_pad_power_enable(pad->id);
1504 if (err)
1505 return err;
1506 break;
1507 case PIN_CONFIG_POWER_SOURCE:
1508 if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
1509 arg != TEGRA_IO_PAD_VOLTAGE_3V3)
1510 return -EINVAL;
1511 err = tegra_io_pad_set_voltage(pad->id, arg);
1512 if (err)
1513 return err;
1514 break;
1515 default:
1516 return -EINVAL;
1517 }
1518 }
1519
1520 return 0;
1521}
1522
1523static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
1524 .pin_config_get = tegra_io_pad_pinconf_get,
1525 .pin_config_set = tegra_io_pad_pinconf_set,
1526 .is_generic = true,
1527};
1528
1529static struct pinctrl_desc tegra_pmc_pctl_desc = {
1530 .pctlops = &tegra_io_pad_pinctrl_ops,
1531 .confops = &tegra_io_pad_pinconf_ops,
1532};
1533
1534static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
1535{
1536 int err = 0;
1537
1538 if (!pmc->soc->num_pin_descs)
1539 return 0;
1540
1541 tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
1542 tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
1543 tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
1544
1545 pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
1546 pmc);
1547 if (IS_ERR(pmc->pctl_dev)) {
1548 err = PTR_ERR(pmc->pctl_dev);
1549 dev_err(pmc->dev, "unable to register pinctrl, %d\n", err);
1550 }
1551
1552 return err;
1553}
1554
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301555static ssize_t reset_reason_show(struct device *dev,
1556 struct device_attribute *attr, char *buf)
1557{
1558 u32 value, rst_src;
1559
1560 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1561 rst_src = (value & pmc->soc->regs->rst_source_mask) >>
1562 pmc->soc->regs->rst_source_shift;
1563
1564 return sprintf(buf, "%s\n", pmc->soc->reset_sources[rst_src]);
1565}
1566
1567static DEVICE_ATTR_RO(reset_reason);
1568
1569static ssize_t reset_level_show(struct device *dev,
1570 struct device_attribute *attr, char *buf)
1571{
1572 u32 value, rst_lvl;
1573
1574 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1575 rst_lvl = (value & pmc->soc->regs->rst_level_mask) >>
1576 pmc->soc->regs->rst_level_shift;
1577
1578 return sprintf(buf, "%s\n", pmc->soc->reset_levels[rst_lvl]);
1579}
1580
1581static DEVICE_ATTR_RO(reset_level);
1582
1583static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
1584{
1585 struct device *dev = pmc->dev;
1586 int err = 0;
1587
1588 if (pmc->soc->reset_sources) {
1589 err = device_create_file(dev, &dev_attr_reset_reason);
1590 if (err < 0)
1591 dev_warn(dev,
1592 "failed to create attr \"reset_reason\": %d\n",
1593 err);
1594 }
1595
1596 if (pmc->soc->reset_levels) {
1597 err = device_create_file(dev, &dev_attr_reset_level);
1598 if (err < 0)
1599 dev_warn(dev,
1600 "failed to create attr \"reset_level\": %d\n",
1601 err);
1602 }
1603}
1604
Thierry Reding72323982014-07-11 13:19:06 +02001605static int tegra_pmc_probe(struct platform_device *pdev)
1606{
Jon Huntere8cf6612016-02-11 18:03:21 +00001607 void __iomem *base;
Thierry Reding72323982014-07-11 13:19:06 +02001608 struct resource *res;
1609 int err;
1610
Jon Huntera83f1fc2016-06-28 11:38:28 +01001611 /*
1612 * Early initialisation should have configured an initial
1613 * register mapping and setup the soc data pointer. If these
1614 * are not valid then something went badly wrong!
1615 */
1616 if (WARN_ON(!pmc->base || !pmc->soc))
1617 return -ENODEV;
1618
Thierry Reding72323982014-07-11 13:19:06 +02001619 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1620 if (err < 0)
1621 return err;
1622
1623 /* take over the memory region from the early initialization */
1624 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jon Hunter0259f522016-02-11 18:03:20 +00001625 base = devm_ioremap_resource(&pdev->dev, res);
1626 if (IS_ERR(base))
1627 return PTR_ERR(base);
Thierry Reding72323982014-07-11 13:19:06 +02001628
Thierry Redingc641ec62017-08-30 12:42:34 +02001629 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
1630 if (res) {
1631 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
1632 if (IS_ERR(pmc->wake))
1633 return PTR_ERR(pmc->wake);
1634 } else {
1635 pmc->wake = base;
1636 }
1637
1638 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
1639 if (res) {
1640 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
1641 if (IS_ERR(pmc->aotag))
1642 return PTR_ERR(pmc->aotag);
1643 } else {
1644 pmc->aotag = base;
1645 }
1646
1647 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
1648 if (res) {
1649 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
1650 if (IS_ERR(pmc->scratch))
1651 return PTR_ERR(pmc->scratch);
1652 } else {
1653 pmc->scratch = base;
1654 }
Thierry Reding5be22552017-08-30 12:32:58 +02001655
Thierry Reding72323982014-07-11 13:19:06 +02001656 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1657 if (IS_ERR(pmc->clk)) {
1658 err = PTR_ERR(pmc->clk);
Thierry Reding5be22552017-08-30 12:32:58 +02001659
1660 if (err != -ENOENT) {
1661 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1662 return err;
1663 }
1664
1665 pmc->clk = NULL;
Thierry Reding72323982014-07-11 13:19:06 +02001666 }
1667
Mikko Perttunen3568df32015-01-06 12:52:58 +02001668 pmc->dev = &pdev->dev;
1669
Thierry Reding72323982014-07-11 13:19:06 +02001670 tegra_pmc_init(pmc);
1671
Mikko Perttunen3568df32015-01-06 12:52:58 +02001672 tegra_pmc_init_tsense_reset(pmc);
1673
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301674 tegra_pmc_reset_sysfs_init(pmc);
1675
Thierry Reding72323982014-07-11 13:19:06 +02001676 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1677 err = tegra_powergate_debugfs_init();
1678 if (err < 0)
1679 return err;
1680 }
1681
David Riley78921582015-03-18 10:52:25 +01001682 err = register_restart_handler(&tegra_pmc_restart_handler);
1683 if (err) {
1684 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1685 err);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001686 goto cleanup_debugfs;
David Riley78921582015-03-18 10:52:25 +01001687 }
1688
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001689 err = tegra_pmc_pinctrl_init(pmc);
1690 if (err)
1691 goto cleanup_restart_handler;
1692
Jon Huntere8cf6612016-02-11 18:03:21 +00001693 mutex_lock(&pmc->powergates_lock);
1694 iounmap(pmc->base);
Jon Hunter0259f522016-02-11 18:03:20 +00001695 pmc->base = base;
Jon Huntere8cf6612016-02-11 18:03:21 +00001696 mutex_unlock(&pmc->powergates_lock);
Jon Hunter0259f522016-02-11 18:03:20 +00001697
Thierry Reding72323982014-07-11 13:19:06 +02001698 return 0;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001699
1700cleanup_restart_handler:
1701 unregister_restart_handler(&tegra_pmc_restart_handler);
1702cleanup_debugfs:
1703 debugfs_remove(pmc->debugfs);
1704 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001705}
1706
Paul Walmsley2b20b612014-12-09 22:36:50 +00001707#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02001708static int tegra_pmc_suspend(struct device *dev)
1709{
1710 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1711
1712 return 0;
1713}
1714
1715static int tegra_pmc_resume(struct device *dev)
1716{
1717 tegra_pmc_writel(0x0, PMC_SCRATCH41);
1718
1719 return 0;
1720}
Thierry Reding72323982014-07-11 13:19:06 +02001721
1722static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1723
Paul Walmsley2b20b612014-12-09 22:36:50 +00001724#endif
1725
Thierry Reding72323982014-07-11 13:19:06 +02001726static const char * const tegra20_powergates[] = {
1727 [TEGRA_POWERGATE_CPU] = "cpu",
1728 [TEGRA_POWERGATE_3D] = "3d",
1729 [TEGRA_POWERGATE_VENC] = "venc",
1730 [TEGRA_POWERGATE_VDEC] = "vdec",
1731 [TEGRA_POWERGATE_PCIE] = "pcie",
1732 [TEGRA_POWERGATE_L2] = "l2",
1733 [TEGRA_POWERGATE_MPE] = "mpe",
1734};
1735
Thierry Reding5be22552017-08-30 12:32:58 +02001736static const struct tegra_pmc_regs tegra20_pmc_regs = {
1737 .scratch0 = 0x50,
1738 .dpd_req = 0x1b8,
1739 .dpd_status = 0x1bc,
1740 .dpd2_req = 0x1c0,
1741 .dpd2_status = 0x1c4,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301742 .rst_status = 0x1b4,
1743 .rst_source_shift = 0x0,
1744 .rst_source_mask = 0x7,
1745 .rst_level_shift = 0x0,
1746 .rst_level_mask = 0x0,
Thierry Reding5be22552017-08-30 12:32:58 +02001747};
1748
1749static void tegra20_pmc_init(struct tegra_pmc *pmc)
1750{
1751 u32 value;
1752
1753 /* Always enable CPU power request */
1754 value = tegra_pmc_readl(PMC_CNTRL);
1755 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1756 tegra_pmc_writel(value, PMC_CNTRL);
1757
1758 value = tegra_pmc_readl(PMC_CNTRL);
1759
1760 if (pmc->sysclkreq_high)
1761 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1762 else
1763 value |= PMC_CNTRL_SYSCLK_POLARITY;
1764
1765 /* configure the output polarity while the request is tristated */
1766 tegra_pmc_writel(value, PMC_CNTRL);
1767
1768 /* now enable the request */
1769 value = tegra_pmc_readl(PMC_CNTRL);
1770 value |= PMC_CNTRL_SYSCLK_OE;
1771 tegra_pmc_writel(value, PMC_CNTRL);
1772}
1773
1774static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
1775 struct device_node *np,
1776 bool invert)
1777{
1778 u32 value;
1779
1780 value = tegra_pmc_readl(PMC_CNTRL);
1781
1782 if (invert)
1783 value |= PMC_CNTRL_INTR_POLARITY;
1784 else
1785 value &= ~PMC_CNTRL_INTR_POLARITY;
1786
1787 tegra_pmc_writel(value, PMC_CNTRL);
1788}
1789
Thierry Reding72323982014-07-11 13:19:06 +02001790static const struct tegra_pmc_soc tegra20_pmc_soc = {
1791 .num_powergates = ARRAY_SIZE(tegra20_powergates),
1792 .powergates = tegra20_powergates,
1793 .num_cpu_powergates = 0,
1794 .cpu_powergates = NULL,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001795 .has_tsense_reset = false,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001796 .has_gpu_clamps = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001797 .num_io_pads = 0,
1798 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001799 .num_pin_descs = 0,
1800 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001801 .regs = &tegra20_pmc_regs,
1802 .init = tegra20_pmc_init,
1803 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301804 .reset_sources = NULL,
1805 .num_reset_sources = 0,
1806 .reset_levels = NULL,
1807 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001808};
1809
1810static const char * const tegra30_powergates[] = {
1811 [TEGRA_POWERGATE_CPU] = "cpu0",
1812 [TEGRA_POWERGATE_3D] = "3d0",
1813 [TEGRA_POWERGATE_VENC] = "venc",
1814 [TEGRA_POWERGATE_VDEC] = "vdec",
1815 [TEGRA_POWERGATE_PCIE] = "pcie",
1816 [TEGRA_POWERGATE_L2] = "l2",
1817 [TEGRA_POWERGATE_MPE] = "mpe",
1818 [TEGRA_POWERGATE_HEG] = "heg",
1819 [TEGRA_POWERGATE_SATA] = "sata",
1820 [TEGRA_POWERGATE_CPU1] = "cpu1",
1821 [TEGRA_POWERGATE_CPU2] = "cpu2",
1822 [TEGRA_POWERGATE_CPU3] = "cpu3",
1823 [TEGRA_POWERGATE_CELP] = "celp",
1824 [TEGRA_POWERGATE_3D1] = "3d1",
1825};
1826
1827static const u8 tegra30_cpu_powergates[] = {
1828 TEGRA_POWERGATE_CPU,
1829 TEGRA_POWERGATE_CPU1,
1830 TEGRA_POWERGATE_CPU2,
1831 TEGRA_POWERGATE_CPU3,
1832};
1833
1834static const struct tegra_pmc_soc tegra30_pmc_soc = {
1835 .num_powergates = ARRAY_SIZE(tegra30_powergates),
1836 .powergates = tegra30_powergates,
1837 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
1838 .cpu_powergates = tegra30_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001839 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001840 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001841 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001842 .num_io_pads = 0,
1843 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001844 .num_pin_descs = 0,
1845 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001846 .regs = &tegra20_pmc_regs,
1847 .init = tegra20_pmc_init,
1848 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301849 .reset_sources = tegra30_reset_sources,
1850 .num_reset_sources = 5,
1851 .reset_levels = NULL,
1852 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001853};
1854
1855static const char * const tegra114_powergates[] = {
1856 [TEGRA_POWERGATE_CPU] = "crail",
1857 [TEGRA_POWERGATE_3D] = "3d",
1858 [TEGRA_POWERGATE_VENC] = "venc",
1859 [TEGRA_POWERGATE_VDEC] = "vdec",
1860 [TEGRA_POWERGATE_MPE] = "mpe",
1861 [TEGRA_POWERGATE_HEG] = "heg",
1862 [TEGRA_POWERGATE_CPU1] = "cpu1",
1863 [TEGRA_POWERGATE_CPU2] = "cpu2",
1864 [TEGRA_POWERGATE_CPU3] = "cpu3",
1865 [TEGRA_POWERGATE_CELP] = "celp",
1866 [TEGRA_POWERGATE_CPU0] = "cpu0",
1867 [TEGRA_POWERGATE_C0NC] = "c0nc",
1868 [TEGRA_POWERGATE_C1NC] = "c1nc",
1869 [TEGRA_POWERGATE_DIS] = "dis",
1870 [TEGRA_POWERGATE_DISB] = "disb",
1871 [TEGRA_POWERGATE_XUSBA] = "xusba",
1872 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1873 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1874};
1875
1876static const u8 tegra114_cpu_powergates[] = {
1877 TEGRA_POWERGATE_CPU0,
1878 TEGRA_POWERGATE_CPU1,
1879 TEGRA_POWERGATE_CPU2,
1880 TEGRA_POWERGATE_CPU3,
1881};
1882
1883static const struct tegra_pmc_soc tegra114_pmc_soc = {
1884 .num_powergates = ARRAY_SIZE(tegra114_powergates),
1885 .powergates = tegra114_powergates,
1886 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1887 .cpu_powergates = tegra114_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001888 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001889 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001890 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001891 .num_io_pads = 0,
1892 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001893 .num_pin_descs = 0,
1894 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001895 .regs = &tegra20_pmc_regs,
1896 .init = tegra20_pmc_init,
1897 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301898 .reset_sources = tegra30_reset_sources,
1899 .num_reset_sources = 5,
1900 .reset_levels = NULL,
1901 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001902};
1903
1904static const char * const tegra124_powergates[] = {
1905 [TEGRA_POWERGATE_CPU] = "crail",
1906 [TEGRA_POWERGATE_3D] = "3d",
1907 [TEGRA_POWERGATE_VENC] = "venc",
1908 [TEGRA_POWERGATE_PCIE] = "pcie",
1909 [TEGRA_POWERGATE_VDEC] = "vdec",
Thierry Reding72323982014-07-11 13:19:06 +02001910 [TEGRA_POWERGATE_MPE] = "mpe",
1911 [TEGRA_POWERGATE_HEG] = "heg",
1912 [TEGRA_POWERGATE_SATA] = "sata",
1913 [TEGRA_POWERGATE_CPU1] = "cpu1",
1914 [TEGRA_POWERGATE_CPU2] = "cpu2",
1915 [TEGRA_POWERGATE_CPU3] = "cpu3",
1916 [TEGRA_POWERGATE_CELP] = "celp",
1917 [TEGRA_POWERGATE_CPU0] = "cpu0",
1918 [TEGRA_POWERGATE_C0NC] = "c0nc",
1919 [TEGRA_POWERGATE_C1NC] = "c1nc",
1920 [TEGRA_POWERGATE_SOR] = "sor",
1921 [TEGRA_POWERGATE_DIS] = "dis",
1922 [TEGRA_POWERGATE_DISB] = "disb",
1923 [TEGRA_POWERGATE_XUSBA] = "xusba",
1924 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1925 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1926 [TEGRA_POWERGATE_VIC] = "vic",
1927 [TEGRA_POWERGATE_IRAM] = "iram",
1928};
1929
1930static const u8 tegra124_cpu_powergates[] = {
1931 TEGRA_POWERGATE_CPU0,
1932 TEGRA_POWERGATE_CPU1,
1933 TEGRA_POWERGATE_CPU2,
1934 TEGRA_POWERGATE_CPU3,
1935};
1936
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001937#define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \
1938 ((struct tegra_io_pad_soc) { \
1939 .id = (_id), \
1940 .dpd = (_dpd), \
1941 .voltage = (_voltage), \
1942 .name = (_name), \
1943 })
1944
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001945#define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \
1946 ((struct pinctrl_pin_desc) { \
1947 .number = (_id), \
1948 .name = (_name) \
1949 })
1950
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001951#define TEGRA124_IO_PAD_TABLE(_pad) \
1952 /* .id .dpd .voltage .name */ \
1953 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
1954 _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \
1955 _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \
1956 _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \
1957 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
1958 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \
1959 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \
1960 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
1961 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
1962 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
1963 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
1964 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
1965 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
1966 _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \
1967 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
1968 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
1969 _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \
1970 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
1971 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
1972 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
1973 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
1974 _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \
1975 _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \
1976 _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \
1977 _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \
1978 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
1979 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
1980 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
1981 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
1982 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias")
1983
Laxman Dewangan21b49912016-10-10 15:14:34 +02001984static const struct tegra_io_pad_soc tegra124_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001985 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001986};
1987
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001988static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
1989 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Reding72323982014-07-11 13:19:06 +02001990};
1991
1992static const struct tegra_pmc_soc tegra124_pmc_soc = {
1993 .num_powergates = ARRAY_SIZE(tegra124_powergates),
1994 .powergates = tegra124_powergates,
1995 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
1996 .cpu_powergates = tegra124_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001997 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001998 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001999 .has_impl_33v_pwr = false,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002000 .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
2001 .io_pads = tegra124_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002002 .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
2003 .pin_descs = tegra124_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002004 .regs = &tegra20_pmc_regs,
2005 .init = tegra20_pmc_init,
2006 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302007 .reset_sources = tegra30_reset_sources,
2008 .num_reset_sources = 5,
2009 .reset_levels = NULL,
2010 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002011};
2012
Thierry Redingc2fe4692015-03-23 11:31:29 +01002013static const char * const tegra210_powergates[] = {
2014 [TEGRA_POWERGATE_CPU] = "crail",
2015 [TEGRA_POWERGATE_3D] = "3d",
2016 [TEGRA_POWERGATE_VENC] = "venc",
2017 [TEGRA_POWERGATE_PCIE] = "pcie",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002018 [TEGRA_POWERGATE_MPE] = "mpe",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002019 [TEGRA_POWERGATE_SATA] = "sata",
2020 [TEGRA_POWERGATE_CPU1] = "cpu1",
2021 [TEGRA_POWERGATE_CPU2] = "cpu2",
2022 [TEGRA_POWERGATE_CPU3] = "cpu3",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002023 [TEGRA_POWERGATE_CPU0] = "cpu0",
2024 [TEGRA_POWERGATE_C0NC] = "c0nc",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002025 [TEGRA_POWERGATE_SOR] = "sor",
2026 [TEGRA_POWERGATE_DIS] = "dis",
2027 [TEGRA_POWERGATE_DISB] = "disb",
2028 [TEGRA_POWERGATE_XUSBA] = "xusba",
2029 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2030 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2031 [TEGRA_POWERGATE_VIC] = "vic",
2032 [TEGRA_POWERGATE_IRAM] = "iram",
2033 [TEGRA_POWERGATE_NVDEC] = "nvdec",
2034 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
2035 [TEGRA_POWERGATE_AUD] = "aud",
2036 [TEGRA_POWERGATE_DFD] = "dfd",
2037 [TEGRA_POWERGATE_VE2] = "ve2",
2038};
2039
2040static const u8 tegra210_cpu_powergates[] = {
2041 TEGRA_POWERGATE_CPU0,
2042 TEGRA_POWERGATE_CPU1,
2043 TEGRA_POWERGATE_CPU2,
2044 TEGRA_POWERGATE_CPU3,
2045};
2046
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002047#define TEGRA210_IO_PAD_TABLE(_pad) \
2048 /* .id .dpd .voltage .name */ \
2049 _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \
2050 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \
2051 _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \
2052 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2053 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2054 _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \
2055 _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \
2056 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \
2057 _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \
2058 _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \
2059 _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \
2060 _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \
2061 _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \
2062 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2063 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
2064 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
2065 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
2066 _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \
2067 _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \
2068 _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \
2069 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
2070 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2071 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
2072 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2073 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
2074 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
2075 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2076 _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \
2077 _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \
2078 _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \
2079 _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \
2080 _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \
2081 _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \
2082 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2083 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2084 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2085 _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \
2086 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias")
2087
Laxman Dewangan21b49912016-10-10 15:14:34 +02002088static const struct tegra_io_pad_soc tegra210_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002089 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002090};
2091
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002092static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
2093 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc2fe4692015-03-23 11:31:29 +01002094};
2095
2096static const struct tegra_pmc_soc tegra210_pmc_soc = {
2097 .num_powergates = ARRAY_SIZE(tegra210_powergates),
2098 .powergates = tegra210_powergates,
2099 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
2100 .cpu_powergates = tegra210_cpu_powergates,
2101 .has_tsense_reset = true,
2102 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002103 .has_impl_33v_pwr = false,
Peter De Schrijvera263394a2018-01-25 16:00:13 +02002104 .needs_mbist_war = true,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002105 .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
2106 .io_pads = tegra210_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002107 .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
2108 .pin_descs = tegra210_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002109 .regs = &tegra20_pmc_regs,
2110 .init = tegra20_pmc_init,
2111 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302112 .reset_sources = tegra30_reset_sources,
2113 .num_reset_sources = 5,
2114 .reset_levels = NULL,
2115 .num_reset_levels = 0,
Thierry Redingc2fe4692015-03-23 11:31:29 +01002116};
2117
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002118#define TEGRA186_IO_PAD_TABLE(_pad) \
2119 /* .id .dpd .voltage .name */ \
2120 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2121 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2122 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2123 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2124 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \
2125 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \
2126 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2127 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \
2128 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2129 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2130 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2131 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \
2132 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2133 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2134 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2135 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \
2136 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \
2137 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \
2138 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2139 _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \
2140 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \
2141 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \
2142 _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \
2143 _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \
2144 _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \
2145 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \
2146 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \
2147 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \
2148 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \
2149 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \
2150 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \
2151 _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \
2152 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \
2153 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \
2154 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \
2155 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \
2156 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \
2157 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv")
2158
Thierry Redingc641ec62017-08-30 12:42:34 +02002159static const struct tegra_io_pad_soc tegra186_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002160 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
Thierry Redingc641ec62017-08-30 12:42:34 +02002161};
2162
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002163static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
2164 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc641ec62017-08-30 12:42:34 +02002165};
2166
2167static const struct tegra_pmc_regs tegra186_pmc_regs = {
2168 .scratch0 = 0x2000,
2169 .dpd_req = 0x74,
2170 .dpd_status = 0x78,
2171 .dpd2_req = 0x7c,
2172 .dpd2_status = 0x80,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302173 .rst_status = 0x70,
2174 .rst_source_shift = 0x2,
2175 .rst_source_mask = 0x3C,
2176 .rst_level_shift = 0x0,
2177 .rst_level_mask = 0x3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002178};
2179
2180static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2181 struct device_node *np,
2182 bool invert)
2183{
2184 struct resource regs;
2185 void __iomem *wake;
2186 u32 value;
2187 int index;
2188
2189 index = of_property_match_string(np, "reg-names", "wake");
2190 if (index < 0) {
2191 pr_err("failed to find PMC wake registers\n");
2192 return;
2193 }
2194
2195 of_address_to_resource(np, index, &regs);
2196
2197 wake = ioremap_nocache(regs.start, resource_size(&regs));
2198 if (!wake) {
2199 pr_err("failed to map PMC wake registers\n");
2200 return;
2201 }
2202
2203 value = readl(wake + WAKE_AOWAKE_CTRL);
2204
2205 if (invert)
2206 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
2207 else
2208 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
2209
2210 writel(value, wake + WAKE_AOWAKE_CTRL);
2211
2212 iounmap(wake);
2213}
2214
2215static const struct tegra_pmc_soc tegra186_pmc_soc = {
2216 .num_powergates = 0,
2217 .powergates = NULL,
2218 .num_cpu_powergates = 0,
2219 .cpu_powergates = NULL,
2220 .has_tsense_reset = false,
2221 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002222 .has_impl_33v_pwr = true,
Thierry Redingc641ec62017-08-30 12:42:34 +02002223 .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
2224 .io_pads = tegra186_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002225 .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
2226 .pin_descs = tegra186_pin_descs,
Thierry Redingc641ec62017-08-30 12:42:34 +02002227 .regs = &tegra186_pmc_regs,
2228 .init = NULL,
2229 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302230 .reset_sources = tegra186_reset_sources,
2231 .num_reset_sources = 14,
2232 .reset_levels = tegra186_reset_levels,
2233 .num_reset_levels = 3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002234};
2235
Thierry Reding72323982014-07-11 13:19:06 +02002236static const struct of_device_id tegra_pmc_match[] = {
Mikko Perttunen56327f52018-02-20 13:58:08 +02002237 { .compatible = "nvidia,tegra194-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc641ec62017-08-30 12:42:34 +02002238 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc2fe4692015-03-23 11:31:29 +01002239 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
Thierry Reding7d71e9032015-04-29 12:42:28 +02002240 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
Thierry Reding72323982014-07-11 13:19:06 +02002241 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
2242 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
2243 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
2244 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
2245 { }
2246};
2247
2248static struct platform_driver tegra_pmc_driver = {
2249 .driver = {
2250 .name = "tegra-pmc",
2251 .suppress_bind_attrs = true,
2252 .of_match_table = tegra_pmc_match,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002253#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02002254 .pm = &tegra_pmc_pm_ops,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002255#endif
Thierry Reding72323982014-07-11 13:19:06 +02002256 },
2257 .probe = tegra_pmc_probe,
2258};
Paul Gortmaker7d4d9ed2015-05-01 20:10:57 -04002259builtin_platform_driver(tegra_pmc_driver);
Thierry Reding72323982014-07-11 13:19:06 +02002260
2261/*
2262 * Early initialization to allow access to registers in the very early boot
2263 * process.
2264 */
2265static int __init tegra_pmc_early_init(void)
2266{
2267 const struct of_device_id *match;
2268 struct device_node *np;
2269 struct resource regs;
2270 bool invert;
Thierry Reding72323982014-07-11 13:19:06 +02002271
Jon Hunter61fd2842016-06-28 11:38:26 +01002272 mutex_init(&pmc->powergates_lock);
2273
Thierry Reding72323982014-07-11 13:19:06 +02002274 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
2275 if (!np) {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002276 /*
2277 * Fall back to legacy initialization for 32-bit ARM only. All
2278 * 64-bit ARM device tree files for Tegra are required to have
2279 * a PMC node.
2280 *
2281 * This is for backwards-compatibility with old device trees
2282 * that didn't contain a PMC node. Note that in this case the
2283 * SoC data can't be matched and therefore powergating is
2284 * disabled.
2285 */
2286 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
2287 pr_warn("DT node not found, powergating disabled\n");
Thierry Reding72323982014-07-11 13:19:06 +02002288
Thierry Reding7d71e9032015-04-29 12:42:28 +02002289 regs.start = 0x7000e400;
2290 regs.end = 0x7000e7ff;
2291 regs.flags = IORESOURCE_MEM;
Thierry Reding72323982014-07-11 13:19:06 +02002292
Thierry Reding7d71e9032015-04-29 12:42:28 +02002293 pr_warn("Using memory region %pR\n", &regs);
2294 } else {
2295 /*
2296 * At this point we're not running on Tegra, so play
2297 * nice with multi-platform kernels.
2298 */
2299 return 0;
2300 }
Thierry Reding72323982014-07-11 13:19:06 +02002301 } else {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002302 /*
2303 * Extract information from the device tree if we've found a
2304 * matching node.
2305 */
2306 if (of_address_to_resource(np, 0, &regs) < 0) {
2307 pr_err("failed to get PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002308 of_node_put(np);
Thierry Reding7d71e9032015-04-29 12:42:28 +02002309 return -ENXIO;
2310 }
Thierry Reding72323982014-07-11 13:19:06 +02002311 }
2312
2313 pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
2314 if (!pmc->base) {
2315 pr_err("failed to map PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002316 of_node_put(np);
Thierry Reding72323982014-07-11 13:19:06 +02002317 return -ENXIO;
2318 }
2319
Jon Hunter11131892016-06-28 11:38:24 +01002320 if (np) {
Jon Hunter718a2422016-06-28 11:38:25 +01002321 pmc->soc = match->data;
2322
Jon Huntere2d17962016-06-30 11:56:25 +01002323 tegra_powergate_init(pmc, np);
Thierry Reding72323982014-07-11 13:19:06 +02002324
Jon Hunter11131892016-06-28 11:38:24 +01002325 /*
2326 * Invert the interrupt polarity if a PMC device tree node
2327 * exists and contains the nvidia,invert-interrupt property.
2328 */
2329 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
Thierry Reding72323982014-07-11 13:19:06 +02002330
Thierry Reding5be22552017-08-30 12:32:58 +02002331 pmc->soc->setup_irq_polarity(pmc, np, invert);
Jon Hunterb69a6252016-06-28 11:38:27 +01002332
2333 of_node_put(np);
Jon Hunter11131892016-06-28 11:38:24 +01002334 }
Thierry Reding72323982014-07-11 13:19:06 +02002335
2336 return 0;
2337}
2338early_initcall(tegra_pmc_early_init);