blob: bb76aec4bafeed6dd7ea5e9e58e24c6d296f013f [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
755static int powergate_open(struct inode *inode, struct file *file)
756{
757 return single_open(file, powergate_show, inode->i_private);
758}
759
760static const struct file_operations powergate_fops = {
761 .open = powergate_open,
762 .read = seq_read,
763 .llseek = seq_lseek,
764 .release = single_release,
765};
766
767static int tegra_powergate_debugfs_init(void)
768{
Jon Hunter3195ac62015-12-04 14:57:05 +0000769 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
770 &powergate_fops);
771 if (!pmc->debugfs)
Thierry Reding72323982014-07-11 13:19:06 +0200772 return -ENOMEM;
773
774 return 0;
775}
776
Jon Huntera3804512016-03-30 10:15:15 +0100777static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
778 struct device_node *np)
779{
780 struct clk *clk;
781 unsigned int i, count;
782 int err;
783
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +0200784 count = of_clk_get_parent_count(np);
Jon Huntera3804512016-03-30 10:15:15 +0100785 if (count == 0)
786 return -ENODEV;
787
788 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
789 if (!pg->clks)
790 return -ENOMEM;
791
792 for (i = 0; i < count; i++) {
793 pg->clks[i] = of_clk_get(np, i);
794 if (IS_ERR(pg->clks[i])) {
795 err = PTR_ERR(pg->clks[i]);
796 goto err;
797 }
798 }
799
800 pg->num_clks = count;
801
802 return 0;
803
804err:
805 while (i--)
806 clk_put(pg->clks[i]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200807
Jon Huntera3804512016-03-30 10:15:15 +0100808 kfree(pg->clks);
809
810 return err;
811}
812
813static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
Jon Hunter05cfb982016-06-29 10:17:47 +0100814 struct device_node *np, bool off)
Jon Huntera3804512016-03-30 10:15:15 +0100815{
Jon Huntera3804512016-03-30 10:15:15 +0100816 int err;
817
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200818 pg->reset = of_reset_control_array_get_exclusive(np);
819 if (IS_ERR(pg->reset)) {
820 err = PTR_ERR(pg->reset);
821 pr_err("failed to get device resets: %d\n", err);
822 return err;
Jon Huntera3804512016-03-30 10:15:15 +0100823 }
824
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200825 if (off)
826 err = reset_control_assert(pg->reset);
827 else
828 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100829
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200830 if (err)
831 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100832
833 return err;
834}
835
836static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
837{
838 struct tegra_powergate *pg;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100839 int id, err;
Jon Huntera3804512016-03-30 10:15:15 +0100840 bool off;
Jon Huntera3804512016-03-30 10:15:15 +0100841
842 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
843 if (!pg)
Jon Hunterc2710ac2016-06-30 11:56:24 +0100844 return;
Jon Huntera3804512016-03-30 10:15:15 +0100845
846 id = tegra_powergate_lookup(pmc, np->name);
Jon Hunterc2710ac2016-06-30 11:56:24 +0100847 if (id < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500848 pr_err("powergate lookup failed for %pOFn: %d\n", np, id);
Jon Huntera3804512016-03-30 10:15:15 +0100849 goto free_mem;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100850 }
Jon Huntera3804512016-03-30 10:15:15 +0100851
852 /*
853 * Clear the bit for this powergate so it cannot be managed
854 * directly via the legacy APIs for controlling powergates.
855 */
856 clear_bit(id, pmc->powergates_available);
857
858 pg->id = id;
859 pg->genpd.name = np->name;
860 pg->genpd.power_off = tegra_genpd_power_off;
861 pg->genpd.power_on = tegra_genpd_power_on;
862 pg->pmc = pmc;
863
Jon Hunter05cfb982016-06-29 10:17:47 +0100864 off = !tegra_powergate_is_powered(pg->id);
865
Jon Hunterc2710ac2016-06-30 11:56:24 +0100866 err = tegra_powergate_of_get_clks(pg, np);
867 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500868 pr_err("failed to get clocks for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100869 goto set_available;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100870 }
Jon Huntera3804512016-03-30 10:15:15 +0100871
Jon Hunterc2710ac2016-06-30 11:56:24 +0100872 err = tegra_powergate_of_get_resets(pg, np, off);
873 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500874 pr_err("failed to get resets for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100875 goto remove_clks;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100876 }
Jon Huntera3804512016-03-30 10:15:15 +0100877
Jon Hunter0b137342016-10-22 20:23:56 +0100878 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
879 if (off)
880 WARN_ON(tegra_powergate_power_up(pg, true));
881
882 goto remove_resets;
883 }
Jon Huntere2d17962016-06-30 11:56:25 +0100884
Jon Huntercd5ceda2016-10-22 20:23:55 +0100885 err = pm_genpd_init(&pg->genpd, NULL, off);
886 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500887 pr_err("failed to initialise PM domain %pOFn: %d\n", np,
Jon Huntercd5ceda2016-10-22 20:23:55 +0100888 err);
889 goto remove_resets;
890 }
Jon Huntera3804512016-03-30 10:15:15 +0100891
Jon Hunterc2710ac2016-06-30 11:56:24 +0100892 err = of_genpd_add_provider_simple(np, &pg->genpd);
893 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500894 pr_err("failed to add PM domain provider for %pOFn: %d\n",
895 np, err);
Jon Hunter0b137342016-10-22 20:23:56 +0100896 goto remove_genpd;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100897 }
Jon Huntera3804512016-03-30 10:15:15 +0100898
Thierry Reding45221122016-11-08 11:05:03 +0100899 pr_debug("added PM domain %s\n", pg->genpd.name);
Jon Huntera3804512016-03-30 10:15:15 +0100900
901 return;
902
Jon Hunter0b137342016-10-22 20:23:56 +0100903remove_genpd:
904 pm_genpd_remove(&pg->genpd);
Jon Huntere2d17962016-06-30 11:56:25 +0100905
Jon Huntera3804512016-03-30 10:15:15 +0100906remove_resets:
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200907 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100908
909remove_clks:
910 while (pg->num_clks--)
911 clk_put(pg->clks[pg->num_clks]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200912
Jon Huntera3804512016-03-30 10:15:15 +0100913 kfree(pg->clks);
914
915set_available:
916 set_bit(id, pmc->powergates_available);
917
918free_mem:
919 kfree(pg);
Jon Huntera3804512016-03-30 10:15:15 +0100920}
921
Jon Huntere2d17962016-06-30 11:56:25 +0100922static void tegra_powergate_init(struct tegra_pmc *pmc,
923 struct device_node *parent)
Jon Huntera3804512016-03-30 10:15:15 +0100924{
925 struct device_node *np, *child;
Jon Huntere2d17962016-06-30 11:56:25 +0100926 unsigned int i;
Jon Huntera3804512016-03-30 10:15:15 +0100927
Jon Huntere2d17962016-06-30 11:56:25 +0100928 /* Create a bitmap of the available and valid partitions */
929 for (i = 0; i < pmc->soc->num_powergates; i++)
930 if (pmc->soc->powergates[i])
931 set_bit(i, pmc->powergates_available);
932
933 np = of_get_child_by_name(parent, "powergates");
Jon Huntera3804512016-03-30 10:15:15 +0100934 if (!np)
935 return;
936
Tuomas Tynkkynen0c106e52017-07-29 02:58:43 +0300937 for_each_child_of_node(np, child)
Jon Huntera3804512016-03-30 10:15:15 +0100938 tegra_powergate_add(pmc, child);
Jon Huntera3804512016-03-30 10:15:15 +0100939
940 of_node_put(np);
941}
942
Laxman Dewangan21b49912016-10-10 15:14:34 +0200943static const struct tegra_io_pad_soc *
944tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +0200945{
Laxman Dewangan21b49912016-10-10 15:14:34 +0200946 unsigned int i;
947
948 for (i = 0; i < pmc->soc->num_io_pads; i++)
949 if (pmc->soc->io_pads[i].id == id)
950 return &pmc->soc->io_pads[i];
951
952 return NULL;
953}
954
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300955static int tegra_io_pad_get_dpd_register_bit(enum tegra_io_pad id,
956 unsigned long *request,
957 unsigned long *status,
958 u32 *mask)
Laxman Dewangan21b49912016-10-10 15:14:34 +0200959{
960 const struct tegra_io_pad_soc *pad;
Thierry Reding72323982014-07-11 13:19:06 +0200961
Laxman Dewangan21b49912016-10-10 15:14:34 +0200962 pad = tegra_io_pad_find(pmc, id);
Thierry Reding54e24722016-11-08 10:58:32 +0100963 if (!pad) {
964 pr_err("invalid I/O pad ID %u\n", id);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200965 return -ENOENT;
Thierry Reding54e24722016-11-08 10:58:32 +0100966 }
Thierry Reding72323982014-07-11 13:19:06 +0200967
Laxman Dewangan21b49912016-10-10 15:14:34 +0200968 if (pad->dpd == UINT_MAX)
969 return -ENOTSUPP;
Thierry Reding72323982014-07-11 13:19:06 +0200970
Jon Hunter27b12b4e2016-10-22 20:23:53 +0100971 *mask = BIT(pad->dpd % 32);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200972
973 if (pad->dpd < 32) {
Thierry Reding5be22552017-08-30 12:32:58 +0200974 *status = pmc->soc->regs->dpd_status;
975 *request = pmc->soc->regs->dpd_req;
Thierry Reding72323982014-07-11 13:19:06 +0200976 } else {
Thierry Reding5be22552017-08-30 12:32:58 +0200977 *status = pmc->soc->regs->dpd2_status;
978 *request = pmc->soc->regs->dpd2_req;
Thierry Reding72323982014-07-11 13:19:06 +0200979 }
980
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300981 return 0;
982}
983
984static int tegra_io_pad_prepare(enum tegra_io_pad id, unsigned long *request,
985 unsigned long *status, u32 *mask)
986{
987 unsigned long rate, value;
988 int err;
989
990 err = tegra_io_pad_get_dpd_register_bit(id, request, status, mask);
991 if (err)
992 return err;
993
Thierry Reding5be22552017-08-30 12:32:58 +0200994 if (pmc->clk) {
995 rate = clk_get_rate(pmc->clk);
996 if (!rate) {
997 pr_err("failed to get clock rate\n");
998 return -ENODEV;
999 }
1000
1001 tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
1002
1003 /* must be at least 200 ns, in APB (PCLK) clock cycles */
1004 value = DIV_ROUND_UP(1000000000, rate);
1005 value = DIV_ROUND_UP(200, value);
1006 tegra_pmc_writel(value, SEL_DPD_TIM);
Thierry Reding54e24722016-11-08 10:58:32 +01001007 }
Thierry Reding72323982014-07-11 13:19:06 +02001008
Thierry Reding72323982014-07-11 13:19:06 +02001009 return 0;
1010}
1011
Laxman Dewangan21b49912016-10-10 15:14:34 +02001012static int tegra_io_pad_poll(unsigned long offset, u32 mask,
1013 u32 val, unsigned long timeout)
Thierry Reding72323982014-07-11 13:19:06 +02001014{
Laxman Dewangan84cf85e2016-06-17 18:36:13 +05301015 u32 value;
Thierry Reding72323982014-07-11 13:19:06 +02001016
1017 timeout = jiffies + msecs_to_jiffies(timeout);
1018
1019 while (time_after(timeout, jiffies)) {
1020 value = tegra_pmc_readl(offset);
1021 if ((value & mask) == val)
1022 return 0;
1023
1024 usleep_range(250, 1000);
1025 }
1026
1027 return -ETIMEDOUT;
1028}
1029
Laxman Dewangan21b49912016-10-10 15:14:34 +02001030static void tegra_io_pad_unprepare(void)
Thierry Reding72323982014-07-11 13:19:06 +02001031{
Thierry Reding5be22552017-08-30 12:32:58 +02001032 if (pmc->clk)
1033 tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
Thierry Reding72323982014-07-11 13:19:06 +02001034}
1035
Laxman Dewangan21b49912016-10-10 15:14:34 +02001036/**
1037 * tegra_io_pad_power_enable() - enable power to I/O pad
1038 * @id: Tegra I/O pad ID for which to enable power
1039 *
1040 * Returns: 0 on success or a negative error code on failure.
1041 */
1042int tegra_io_pad_power_enable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001043{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001044 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001045 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001046 int err;
1047
Jon Huntere8cf6612016-02-11 18:03:21 +00001048 mutex_lock(&pmc->powergates_lock);
1049
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001050 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001051 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001052 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001053 goto unlock;
1054 }
Thierry Reding72323982014-07-11 13:19:06 +02001055
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001056 tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001057
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001058 err = tegra_io_pad_poll(status, mask, 0, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001059 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001060 pr_err("failed to enable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001061 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001062 }
Thierry Reding72323982014-07-11 13:19:06 +02001063
Laxman Dewangan21b49912016-10-10 15:14:34 +02001064 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001065
Laxman Dewangan21b49912016-10-10 15:14:34 +02001066unlock:
Jon Huntere8cf6612016-02-11 18:03:21 +00001067 mutex_unlock(&pmc->powergates_lock);
Jon Huntere8cf6612016-02-11 18:03:21 +00001068 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001069}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001070EXPORT_SYMBOL(tegra_io_pad_power_enable);
Thierry Reding72323982014-07-11 13:19:06 +02001071
Laxman Dewangan21b49912016-10-10 15:14:34 +02001072/**
1073 * tegra_io_pad_power_disable() - disable power to I/O pad
1074 * @id: Tegra I/O pad ID for which to disable power
1075 *
1076 * Returns: 0 on success or a negative error code on failure.
1077 */
1078int tegra_io_pad_power_disable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001079{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001080 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001081 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001082 int err;
1083
Jon Huntere8cf6612016-02-11 18:03:21 +00001084 mutex_lock(&pmc->powergates_lock);
1085
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001086 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001087 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001088 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001089 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001090 }
Thierry Reding72323982014-07-11 13:19:06 +02001091
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001092 tegra_pmc_writel(IO_DPD_REQ_CODE_ON | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001093
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001094 err = tegra_io_pad_poll(status, mask, mask, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001095 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001096 pr_err("failed to disable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001097 goto unlock;
1098 }
Thierry Reding72323982014-07-11 13:19:06 +02001099
Laxman Dewangan21b49912016-10-10 15:14:34 +02001100 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001101
Laxman Dewangan21b49912016-10-10 15:14:34 +02001102unlock:
1103 mutex_unlock(&pmc->powergates_lock);
1104 return err;
1105}
1106EXPORT_SYMBOL(tegra_io_pad_power_disable);
1107
Aapo Vienamof142b9d2018-08-10 21:08:09 +03001108static int tegra_io_pad_is_powered(enum tegra_io_pad id)
1109{
1110 unsigned long request, status;
1111 u32 mask, value;
1112 int err;
1113
1114 err = tegra_io_pad_get_dpd_register_bit(id, &request, &status, &mask);
1115 if (err)
1116 return err;
1117
1118 value = tegra_pmc_readl(status);
1119
1120 return !(value & mask);
1121}
1122
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001123static int tegra_io_pad_set_voltage(enum tegra_io_pad id, int voltage)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001124{
1125 const struct tegra_io_pad_soc *pad;
1126 u32 value;
1127
1128 pad = tegra_io_pad_find(pmc, id);
1129 if (!pad)
1130 return -ENOENT;
1131
1132 if (pad->voltage == UINT_MAX)
1133 return -ENOTSUPP;
1134
1135 mutex_lock(&pmc->powergates_lock);
1136
Aapo Vienamo13136a42018-08-10 21:08:07 +03001137 if (pmc->soc->has_impl_33v_pwr) {
1138 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001139
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001140 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001141 value &= ~BIT(pad->voltage);
1142 else
1143 value |= BIT(pad->voltage);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001144
Aapo Vienamo13136a42018-08-10 21:08:07 +03001145 tegra_pmc_writel(value, PMC_IMPL_E_33V_PWR);
1146 } else {
1147 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1148 value = tegra_pmc_readl(PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001149 value |= BIT(pad->voltage);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001150 tegra_pmc_writel(value, PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001151
Aapo Vienamo13136a42018-08-10 21:08:07 +03001152 /* update I/O voltage */
1153 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
1154
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001155 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001156 value &= ~BIT(pad->voltage);
1157 else
1158 value |= BIT(pad->voltage);
1159
1160 tegra_pmc_writel(value, PMC_PWR_DET_VALUE);
1161 }
Laxman Dewangan21b49912016-10-10 15:14:34 +02001162
Jon Huntere8cf6612016-02-11 18:03:21 +00001163 mutex_unlock(&pmc->powergates_lock);
1164
Laxman Dewangan21b49912016-10-10 15:14:34 +02001165 usleep_range(100, 250);
1166
1167 return 0;
1168}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001169
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001170static int tegra_io_pad_get_voltage(enum tegra_io_pad id)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001171{
1172 const struct tegra_io_pad_soc *pad;
1173 u32 value;
1174
1175 pad = tegra_io_pad_find(pmc, id);
1176 if (!pad)
1177 return -ENOENT;
1178
1179 if (pad->voltage == UINT_MAX)
1180 return -ENOTSUPP;
1181
Aapo Vienamo13136a42018-08-10 21:08:07 +03001182 if (pmc->soc->has_impl_33v_pwr)
1183 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
1184 else
1185 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001186
1187 if ((value & BIT(pad->voltage)) == 0)
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001188 return TEGRA_IO_PAD_VOLTAGE_1V8;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001189
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001190 return TEGRA_IO_PAD_VOLTAGE_3V3;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001191}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001192
1193/**
1194 * tegra_io_rail_power_on() - enable power to I/O rail
1195 * @id: Tegra I/O pad ID for which to enable power
1196 *
1197 * See also: tegra_io_pad_power_enable()
1198 */
1199int tegra_io_rail_power_on(unsigned int id)
1200{
1201 return tegra_io_pad_power_enable(id);
1202}
1203EXPORT_SYMBOL(tegra_io_rail_power_on);
1204
1205/**
1206 * tegra_io_rail_power_off() - disable power to I/O rail
1207 * @id: Tegra I/O pad ID for which to disable power
1208 *
1209 * See also: tegra_io_pad_power_disable()
1210 */
1211int tegra_io_rail_power_off(unsigned int id)
1212{
1213 return tegra_io_pad_power_disable(id);
Thierry Reding72323982014-07-11 13:19:06 +02001214}
1215EXPORT_SYMBOL(tegra_io_rail_power_off);
1216
1217#ifdef CONFIG_PM_SLEEP
1218enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1219{
1220 return pmc->suspend_mode;
1221}
1222
1223void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1224{
1225 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1226 return;
1227
1228 pmc->suspend_mode = mode;
1229}
1230
1231void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1232{
1233 unsigned long long rate = 0;
1234 u32 value;
1235
1236 switch (mode) {
1237 case TEGRA_SUSPEND_LP1:
1238 rate = 32768;
1239 break;
1240
1241 case TEGRA_SUSPEND_LP2:
1242 rate = clk_get_rate(pmc->clk);
1243 break;
1244
1245 default:
1246 break;
1247 }
1248
1249 if (WARN_ON_ONCE(rate == 0))
1250 rate = 100000000;
1251
1252 if (rate != pmc->rate) {
1253 u64 ticks;
1254
1255 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1256 do_div(ticks, USEC_PER_SEC);
1257 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1258
1259 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1260 do_div(ticks, USEC_PER_SEC);
1261 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1262
1263 wmb();
1264
1265 pmc->rate = rate;
1266 }
1267
1268 value = tegra_pmc_readl(PMC_CNTRL);
1269 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1270 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1271 tegra_pmc_writel(value, PMC_CNTRL);
1272}
1273#endif
1274
1275static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1276{
1277 u32 value, values[2];
1278
1279 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1280 } else {
1281 switch (value) {
1282 case 0:
1283 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1284 break;
1285
1286 case 1:
1287 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1288 break;
1289
1290 case 2:
1291 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1292 break;
1293
1294 default:
1295 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1296 break;
1297 }
1298 }
1299
1300 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1301
1302 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1303 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1304
1305 pmc->cpu_good_time = value;
1306
1307 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1308 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1309
1310 pmc->cpu_off_time = value;
1311
1312 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1313 values, ARRAY_SIZE(values)))
1314 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1315
1316 pmc->core_osc_time = values[0];
1317 pmc->core_pmu_time = values[1];
1318
1319 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1320 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1321
1322 pmc->core_off_time = value;
1323
1324 pmc->corereq_high = of_property_read_bool(np,
1325 "nvidia,core-power-req-active-high");
1326
1327 pmc->sysclkreq_high = of_property_read_bool(np,
1328 "nvidia,sys-clock-req-active-high");
1329
1330 pmc->combined_req = of_property_read_bool(np,
1331 "nvidia,combined-power-req");
1332
1333 pmc->cpu_pwr_good_en = of_property_read_bool(np,
1334 "nvidia,cpu-pwr-good-en");
1335
1336 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1337 ARRAY_SIZE(values)))
1338 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1339 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1340
1341 pmc->lp0_vec_phys = values[0];
1342 pmc->lp0_vec_size = values[1];
1343
1344 return 0;
1345}
1346
1347static void tegra_pmc_init(struct tegra_pmc *pmc)
1348{
Thierry Reding5be22552017-08-30 12:32:58 +02001349 if (pmc->soc->init)
1350 pmc->soc->init(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001351}
1352
Jon Hunter1e52efdf2015-12-04 14:57:04 +00001353static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
Mikko Perttunen3568df32015-01-06 12:52:58 +02001354{
1355 static const char disabled[] = "emergency thermal reset disabled";
1356 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1357 struct device *dev = pmc->dev;
1358 struct device_node *np;
1359 u32 value, checksum;
1360
1361 if (!pmc->soc->has_tsense_reset)
Thierry Reding95169cd2015-07-09 09:59:55 +02001362 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001363
Johan Hovold1dc6bd52017-11-15 10:44:58 +01001364 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
Mikko Perttunen3568df32015-01-06 12:52:58 +02001365 if (!np) {
1366 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
Thierry Reding95169cd2015-07-09 09:59:55 +02001367 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001368 }
1369
1370 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1371 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1372 goto out;
1373 }
1374
1375 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1376 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1377 goto out;
1378 }
1379
1380 if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1381 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1382 goto out;
1383 }
1384
1385 if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1386 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1387 goto out;
1388 }
1389
1390 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1391 pinmux = 0;
1392
1393 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1394 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1395 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1396
1397 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1398 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1399 tegra_pmc_writel(value, PMC_SCRATCH54);
1400
1401 value = PMC_SCRATCH55_RESET_TEGRA;
1402 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1403 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1404 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1405
1406 /*
1407 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1408 * contain the checksum and are currently zero, so they are not added.
1409 */
1410 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1411 + ((value >> 24) & 0xff);
1412 checksum &= 0xff;
1413 checksum = 0x100 - checksum;
1414
1415 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1416
1417 tegra_pmc_writel(value, PMC_SCRATCH55);
1418
1419 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1420 value |= PMC_SENSOR_CTRL_ENABLE_RST;
1421 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1422
1423 dev_info(pmc->dev, "emergency thermal reset enabled\n");
1424
1425out:
1426 of_node_put(np);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001427}
1428
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001429static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
1430{
1431 return pmc->soc->num_io_pads;
1432}
1433
1434static const char *tegra_io_pad_pinctrl_get_group_name(
1435 struct pinctrl_dev *pctl, unsigned int group)
1436{
1437 return pmc->soc->io_pads[group].name;
1438}
1439
1440static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
1441 unsigned int group,
1442 const unsigned int **pins,
1443 unsigned int *num_pins)
1444{
1445 *pins = &pmc->soc->io_pads[group].id;
1446 *num_pins = 1;
1447 return 0;
1448}
1449
1450static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
1451 .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
1452 .get_group_name = tegra_io_pad_pinctrl_get_group_name,
1453 .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
1454 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1455 .dt_free_map = pinconf_generic_dt_free_map,
1456};
1457
1458static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
1459 unsigned int pin, unsigned long *config)
1460{
1461 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1462 enum pin_config_param param = pinconf_to_config_param(*config);
1463 int ret;
1464 u32 arg;
1465
1466 if (!pad)
1467 return -EINVAL;
1468
1469 switch (param) {
1470 case PIN_CONFIG_POWER_SOURCE:
1471 ret = tegra_io_pad_get_voltage(pad->id);
1472 if (ret < 0)
1473 return ret;
1474 arg = ret;
1475 break;
1476 case PIN_CONFIG_LOW_POWER_MODE:
1477 ret = tegra_io_pad_is_powered(pad->id);
1478 if (ret < 0)
1479 return ret;
1480 arg = !ret;
1481 break;
1482 default:
1483 return -EINVAL;
1484 }
1485
1486 *config = pinconf_to_config_packed(param, arg);
1487
1488 return 0;
1489}
1490
1491static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
1492 unsigned int pin, unsigned long *configs,
1493 unsigned int num_configs)
1494{
1495 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1496 enum pin_config_param param;
1497 unsigned int i;
1498 int err;
1499 u32 arg;
1500
1501 if (!pad)
1502 return -EINVAL;
1503
1504 for (i = 0; i < num_configs; ++i) {
1505 param = pinconf_to_config_param(configs[i]);
1506 arg = pinconf_to_config_argument(configs[i]);
1507
1508 switch (param) {
1509 case PIN_CONFIG_LOW_POWER_MODE:
1510 if (arg)
1511 err = tegra_io_pad_power_disable(pad->id);
1512 else
1513 err = tegra_io_pad_power_enable(pad->id);
1514 if (err)
1515 return err;
1516 break;
1517 case PIN_CONFIG_POWER_SOURCE:
1518 if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
1519 arg != TEGRA_IO_PAD_VOLTAGE_3V3)
1520 return -EINVAL;
1521 err = tegra_io_pad_set_voltage(pad->id, arg);
1522 if (err)
1523 return err;
1524 break;
1525 default:
1526 return -EINVAL;
1527 }
1528 }
1529
1530 return 0;
1531}
1532
1533static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
1534 .pin_config_get = tegra_io_pad_pinconf_get,
1535 .pin_config_set = tegra_io_pad_pinconf_set,
1536 .is_generic = true,
1537};
1538
1539static struct pinctrl_desc tegra_pmc_pctl_desc = {
1540 .pctlops = &tegra_io_pad_pinctrl_ops,
1541 .confops = &tegra_io_pad_pinconf_ops,
1542};
1543
1544static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
1545{
1546 int err = 0;
1547
1548 if (!pmc->soc->num_pin_descs)
1549 return 0;
1550
1551 tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
1552 tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
1553 tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
1554
1555 pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
1556 pmc);
1557 if (IS_ERR(pmc->pctl_dev)) {
1558 err = PTR_ERR(pmc->pctl_dev);
1559 dev_err(pmc->dev, "unable to register pinctrl, %d\n", err);
1560 }
1561
1562 return err;
1563}
1564
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301565static ssize_t reset_reason_show(struct device *dev,
1566 struct device_attribute *attr, char *buf)
1567{
1568 u32 value, rst_src;
1569
1570 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1571 rst_src = (value & pmc->soc->regs->rst_source_mask) >>
1572 pmc->soc->regs->rst_source_shift;
1573
1574 return sprintf(buf, "%s\n", pmc->soc->reset_sources[rst_src]);
1575}
1576
1577static DEVICE_ATTR_RO(reset_reason);
1578
1579static ssize_t reset_level_show(struct device *dev,
1580 struct device_attribute *attr, char *buf)
1581{
1582 u32 value, rst_lvl;
1583
1584 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1585 rst_lvl = (value & pmc->soc->regs->rst_level_mask) >>
1586 pmc->soc->regs->rst_level_shift;
1587
1588 return sprintf(buf, "%s\n", pmc->soc->reset_levels[rst_lvl]);
1589}
1590
1591static DEVICE_ATTR_RO(reset_level);
1592
1593static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
1594{
1595 struct device *dev = pmc->dev;
1596 int err = 0;
1597
1598 if (pmc->soc->reset_sources) {
1599 err = device_create_file(dev, &dev_attr_reset_reason);
1600 if (err < 0)
1601 dev_warn(dev,
1602 "failed to create attr \"reset_reason\": %d\n",
1603 err);
1604 }
1605
1606 if (pmc->soc->reset_levels) {
1607 err = device_create_file(dev, &dev_attr_reset_level);
1608 if (err < 0)
1609 dev_warn(dev,
1610 "failed to create attr \"reset_level\": %d\n",
1611 err);
1612 }
1613}
1614
Thierry Reding72323982014-07-11 13:19:06 +02001615static int tegra_pmc_probe(struct platform_device *pdev)
1616{
Jon Huntere8cf6612016-02-11 18:03:21 +00001617 void __iomem *base;
Thierry Reding72323982014-07-11 13:19:06 +02001618 struct resource *res;
1619 int err;
1620
Jon Huntera83f1fc2016-06-28 11:38:28 +01001621 /*
1622 * Early initialisation should have configured an initial
1623 * register mapping and setup the soc data pointer. If these
1624 * are not valid then something went badly wrong!
1625 */
1626 if (WARN_ON(!pmc->base || !pmc->soc))
1627 return -ENODEV;
1628
Thierry Reding72323982014-07-11 13:19:06 +02001629 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1630 if (err < 0)
1631 return err;
1632
1633 /* take over the memory region from the early initialization */
1634 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jon Hunter0259f522016-02-11 18:03:20 +00001635 base = devm_ioremap_resource(&pdev->dev, res);
1636 if (IS_ERR(base))
1637 return PTR_ERR(base);
Thierry Reding72323982014-07-11 13:19:06 +02001638
Thierry Redingc641ec62017-08-30 12:42:34 +02001639 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
1640 if (res) {
1641 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
1642 if (IS_ERR(pmc->wake))
1643 return PTR_ERR(pmc->wake);
1644 } else {
1645 pmc->wake = base;
1646 }
1647
1648 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
1649 if (res) {
1650 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
1651 if (IS_ERR(pmc->aotag))
1652 return PTR_ERR(pmc->aotag);
1653 } else {
1654 pmc->aotag = base;
1655 }
1656
1657 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
1658 if (res) {
1659 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
1660 if (IS_ERR(pmc->scratch))
1661 return PTR_ERR(pmc->scratch);
1662 } else {
1663 pmc->scratch = base;
1664 }
Thierry Reding5be22552017-08-30 12:32:58 +02001665
Thierry Reding72323982014-07-11 13:19:06 +02001666 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1667 if (IS_ERR(pmc->clk)) {
1668 err = PTR_ERR(pmc->clk);
Thierry Reding5be22552017-08-30 12:32:58 +02001669
1670 if (err != -ENOENT) {
1671 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1672 return err;
1673 }
1674
1675 pmc->clk = NULL;
Thierry Reding72323982014-07-11 13:19:06 +02001676 }
1677
Mikko Perttunen3568df32015-01-06 12:52:58 +02001678 pmc->dev = &pdev->dev;
1679
Thierry Reding72323982014-07-11 13:19:06 +02001680 tegra_pmc_init(pmc);
1681
Mikko Perttunen3568df32015-01-06 12:52:58 +02001682 tegra_pmc_init_tsense_reset(pmc);
1683
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301684 tegra_pmc_reset_sysfs_init(pmc);
1685
Thierry Reding72323982014-07-11 13:19:06 +02001686 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1687 err = tegra_powergate_debugfs_init();
1688 if (err < 0)
1689 return err;
1690 }
1691
David Riley78921582015-03-18 10:52:25 +01001692 err = register_restart_handler(&tegra_pmc_restart_handler);
1693 if (err) {
1694 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1695 err);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001696 goto cleanup_debugfs;
David Riley78921582015-03-18 10:52:25 +01001697 }
1698
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001699 err = tegra_pmc_pinctrl_init(pmc);
1700 if (err)
1701 goto cleanup_restart_handler;
1702
Jon Huntere8cf6612016-02-11 18:03:21 +00001703 mutex_lock(&pmc->powergates_lock);
1704 iounmap(pmc->base);
Jon Hunter0259f522016-02-11 18:03:20 +00001705 pmc->base = base;
Jon Huntere8cf6612016-02-11 18:03:21 +00001706 mutex_unlock(&pmc->powergates_lock);
Jon Hunter0259f522016-02-11 18:03:20 +00001707
Thierry Reding72323982014-07-11 13:19:06 +02001708 return 0;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001709
1710cleanup_restart_handler:
1711 unregister_restart_handler(&tegra_pmc_restart_handler);
1712cleanup_debugfs:
1713 debugfs_remove(pmc->debugfs);
1714 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001715}
1716
Paul Walmsley2b20b612014-12-09 22:36:50 +00001717#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02001718static int tegra_pmc_suspend(struct device *dev)
1719{
1720 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1721
1722 return 0;
1723}
1724
1725static int tegra_pmc_resume(struct device *dev)
1726{
1727 tegra_pmc_writel(0x0, PMC_SCRATCH41);
1728
1729 return 0;
1730}
Thierry Reding72323982014-07-11 13:19:06 +02001731
1732static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1733
Paul Walmsley2b20b612014-12-09 22:36:50 +00001734#endif
1735
Thierry Reding72323982014-07-11 13:19:06 +02001736static const char * const tegra20_powergates[] = {
1737 [TEGRA_POWERGATE_CPU] = "cpu",
1738 [TEGRA_POWERGATE_3D] = "3d",
1739 [TEGRA_POWERGATE_VENC] = "venc",
1740 [TEGRA_POWERGATE_VDEC] = "vdec",
1741 [TEGRA_POWERGATE_PCIE] = "pcie",
1742 [TEGRA_POWERGATE_L2] = "l2",
1743 [TEGRA_POWERGATE_MPE] = "mpe",
1744};
1745
Thierry Reding5be22552017-08-30 12:32:58 +02001746static const struct tegra_pmc_regs tegra20_pmc_regs = {
1747 .scratch0 = 0x50,
1748 .dpd_req = 0x1b8,
1749 .dpd_status = 0x1bc,
1750 .dpd2_req = 0x1c0,
1751 .dpd2_status = 0x1c4,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301752 .rst_status = 0x1b4,
1753 .rst_source_shift = 0x0,
1754 .rst_source_mask = 0x7,
1755 .rst_level_shift = 0x0,
1756 .rst_level_mask = 0x0,
Thierry Reding5be22552017-08-30 12:32:58 +02001757};
1758
1759static void tegra20_pmc_init(struct tegra_pmc *pmc)
1760{
1761 u32 value;
1762
1763 /* Always enable CPU power request */
1764 value = tegra_pmc_readl(PMC_CNTRL);
1765 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1766 tegra_pmc_writel(value, PMC_CNTRL);
1767
1768 value = tegra_pmc_readl(PMC_CNTRL);
1769
1770 if (pmc->sysclkreq_high)
1771 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1772 else
1773 value |= PMC_CNTRL_SYSCLK_POLARITY;
1774
1775 /* configure the output polarity while the request is tristated */
1776 tegra_pmc_writel(value, PMC_CNTRL);
1777
1778 /* now enable the request */
1779 value = tegra_pmc_readl(PMC_CNTRL);
1780 value |= PMC_CNTRL_SYSCLK_OE;
1781 tegra_pmc_writel(value, PMC_CNTRL);
1782}
1783
1784static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
1785 struct device_node *np,
1786 bool invert)
1787{
1788 u32 value;
1789
1790 value = tegra_pmc_readl(PMC_CNTRL);
1791
1792 if (invert)
1793 value |= PMC_CNTRL_INTR_POLARITY;
1794 else
1795 value &= ~PMC_CNTRL_INTR_POLARITY;
1796
1797 tegra_pmc_writel(value, PMC_CNTRL);
1798}
1799
Thierry Reding72323982014-07-11 13:19:06 +02001800static const struct tegra_pmc_soc tegra20_pmc_soc = {
1801 .num_powergates = ARRAY_SIZE(tegra20_powergates),
1802 .powergates = tegra20_powergates,
1803 .num_cpu_powergates = 0,
1804 .cpu_powergates = NULL,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001805 .has_tsense_reset = false,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001806 .has_gpu_clamps = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001807 .num_io_pads = 0,
1808 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001809 .num_pin_descs = 0,
1810 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001811 .regs = &tegra20_pmc_regs,
1812 .init = tegra20_pmc_init,
1813 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301814 .reset_sources = NULL,
1815 .num_reset_sources = 0,
1816 .reset_levels = NULL,
1817 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001818};
1819
1820static const char * const tegra30_powergates[] = {
1821 [TEGRA_POWERGATE_CPU] = "cpu0",
1822 [TEGRA_POWERGATE_3D] = "3d0",
1823 [TEGRA_POWERGATE_VENC] = "venc",
1824 [TEGRA_POWERGATE_VDEC] = "vdec",
1825 [TEGRA_POWERGATE_PCIE] = "pcie",
1826 [TEGRA_POWERGATE_L2] = "l2",
1827 [TEGRA_POWERGATE_MPE] = "mpe",
1828 [TEGRA_POWERGATE_HEG] = "heg",
1829 [TEGRA_POWERGATE_SATA] = "sata",
1830 [TEGRA_POWERGATE_CPU1] = "cpu1",
1831 [TEGRA_POWERGATE_CPU2] = "cpu2",
1832 [TEGRA_POWERGATE_CPU3] = "cpu3",
1833 [TEGRA_POWERGATE_CELP] = "celp",
1834 [TEGRA_POWERGATE_3D1] = "3d1",
1835};
1836
1837static const u8 tegra30_cpu_powergates[] = {
1838 TEGRA_POWERGATE_CPU,
1839 TEGRA_POWERGATE_CPU1,
1840 TEGRA_POWERGATE_CPU2,
1841 TEGRA_POWERGATE_CPU3,
1842};
1843
1844static const struct tegra_pmc_soc tegra30_pmc_soc = {
1845 .num_powergates = ARRAY_SIZE(tegra30_powergates),
1846 .powergates = tegra30_powergates,
1847 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
1848 .cpu_powergates = tegra30_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001849 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001850 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001851 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001852 .num_io_pads = 0,
1853 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001854 .num_pin_descs = 0,
1855 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001856 .regs = &tegra20_pmc_regs,
1857 .init = tegra20_pmc_init,
1858 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301859 .reset_sources = tegra30_reset_sources,
1860 .num_reset_sources = 5,
1861 .reset_levels = NULL,
1862 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001863};
1864
1865static const char * const tegra114_powergates[] = {
1866 [TEGRA_POWERGATE_CPU] = "crail",
1867 [TEGRA_POWERGATE_3D] = "3d",
1868 [TEGRA_POWERGATE_VENC] = "venc",
1869 [TEGRA_POWERGATE_VDEC] = "vdec",
1870 [TEGRA_POWERGATE_MPE] = "mpe",
1871 [TEGRA_POWERGATE_HEG] = "heg",
1872 [TEGRA_POWERGATE_CPU1] = "cpu1",
1873 [TEGRA_POWERGATE_CPU2] = "cpu2",
1874 [TEGRA_POWERGATE_CPU3] = "cpu3",
1875 [TEGRA_POWERGATE_CELP] = "celp",
1876 [TEGRA_POWERGATE_CPU0] = "cpu0",
1877 [TEGRA_POWERGATE_C0NC] = "c0nc",
1878 [TEGRA_POWERGATE_C1NC] = "c1nc",
1879 [TEGRA_POWERGATE_DIS] = "dis",
1880 [TEGRA_POWERGATE_DISB] = "disb",
1881 [TEGRA_POWERGATE_XUSBA] = "xusba",
1882 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1883 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1884};
1885
1886static const u8 tegra114_cpu_powergates[] = {
1887 TEGRA_POWERGATE_CPU0,
1888 TEGRA_POWERGATE_CPU1,
1889 TEGRA_POWERGATE_CPU2,
1890 TEGRA_POWERGATE_CPU3,
1891};
1892
1893static const struct tegra_pmc_soc tegra114_pmc_soc = {
1894 .num_powergates = ARRAY_SIZE(tegra114_powergates),
1895 .powergates = tegra114_powergates,
1896 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1897 .cpu_powergates = tegra114_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001898 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001899 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001900 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001901 .num_io_pads = 0,
1902 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001903 .num_pin_descs = 0,
1904 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001905 .regs = &tegra20_pmc_regs,
1906 .init = tegra20_pmc_init,
1907 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301908 .reset_sources = tegra30_reset_sources,
1909 .num_reset_sources = 5,
1910 .reset_levels = NULL,
1911 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001912};
1913
1914static const char * const tegra124_powergates[] = {
1915 [TEGRA_POWERGATE_CPU] = "crail",
1916 [TEGRA_POWERGATE_3D] = "3d",
1917 [TEGRA_POWERGATE_VENC] = "venc",
1918 [TEGRA_POWERGATE_PCIE] = "pcie",
1919 [TEGRA_POWERGATE_VDEC] = "vdec",
Thierry Reding72323982014-07-11 13:19:06 +02001920 [TEGRA_POWERGATE_MPE] = "mpe",
1921 [TEGRA_POWERGATE_HEG] = "heg",
1922 [TEGRA_POWERGATE_SATA] = "sata",
1923 [TEGRA_POWERGATE_CPU1] = "cpu1",
1924 [TEGRA_POWERGATE_CPU2] = "cpu2",
1925 [TEGRA_POWERGATE_CPU3] = "cpu3",
1926 [TEGRA_POWERGATE_CELP] = "celp",
1927 [TEGRA_POWERGATE_CPU0] = "cpu0",
1928 [TEGRA_POWERGATE_C0NC] = "c0nc",
1929 [TEGRA_POWERGATE_C1NC] = "c1nc",
1930 [TEGRA_POWERGATE_SOR] = "sor",
1931 [TEGRA_POWERGATE_DIS] = "dis",
1932 [TEGRA_POWERGATE_DISB] = "disb",
1933 [TEGRA_POWERGATE_XUSBA] = "xusba",
1934 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1935 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1936 [TEGRA_POWERGATE_VIC] = "vic",
1937 [TEGRA_POWERGATE_IRAM] = "iram",
1938};
1939
1940static const u8 tegra124_cpu_powergates[] = {
1941 TEGRA_POWERGATE_CPU0,
1942 TEGRA_POWERGATE_CPU1,
1943 TEGRA_POWERGATE_CPU2,
1944 TEGRA_POWERGATE_CPU3,
1945};
1946
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001947#define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \
1948 ((struct tegra_io_pad_soc) { \
1949 .id = (_id), \
1950 .dpd = (_dpd), \
1951 .voltage = (_voltage), \
1952 .name = (_name), \
1953 })
1954
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001955#define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \
1956 ((struct pinctrl_pin_desc) { \
1957 .number = (_id), \
1958 .name = (_name) \
1959 })
1960
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001961#define TEGRA124_IO_PAD_TABLE(_pad) \
1962 /* .id .dpd .voltage .name */ \
1963 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
1964 _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \
1965 _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \
1966 _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \
1967 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
1968 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \
1969 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \
1970 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
1971 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
1972 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
1973 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
1974 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
1975 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
1976 _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \
1977 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
1978 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
1979 _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \
1980 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
1981 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
1982 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
1983 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
1984 _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \
1985 _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \
1986 _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \
1987 _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \
1988 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
1989 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
1990 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
1991 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
1992 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias")
1993
Laxman Dewangan21b49912016-10-10 15:14:34 +02001994static const struct tegra_io_pad_soc tegra124_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001995 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001996};
1997
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001998static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
1999 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Reding72323982014-07-11 13:19:06 +02002000};
2001
2002static const struct tegra_pmc_soc tegra124_pmc_soc = {
2003 .num_powergates = ARRAY_SIZE(tegra124_powergates),
2004 .powergates = tegra124_powergates,
2005 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
2006 .cpu_powergates = tegra124_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002007 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002008 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002009 .has_impl_33v_pwr = false,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002010 .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
2011 .io_pads = tegra124_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002012 .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
2013 .pin_descs = tegra124_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002014 .regs = &tegra20_pmc_regs,
2015 .init = tegra20_pmc_init,
2016 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302017 .reset_sources = tegra30_reset_sources,
2018 .num_reset_sources = 5,
2019 .reset_levels = NULL,
2020 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002021};
2022
Thierry Redingc2fe4692015-03-23 11:31:29 +01002023static const char * const tegra210_powergates[] = {
2024 [TEGRA_POWERGATE_CPU] = "crail",
2025 [TEGRA_POWERGATE_3D] = "3d",
2026 [TEGRA_POWERGATE_VENC] = "venc",
2027 [TEGRA_POWERGATE_PCIE] = "pcie",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002028 [TEGRA_POWERGATE_MPE] = "mpe",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002029 [TEGRA_POWERGATE_SATA] = "sata",
2030 [TEGRA_POWERGATE_CPU1] = "cpu1",
2031 [TEGRA_POWERGATE_CPU2] = "cpu2",
2032 [TEGRA_POWERGATE_CPU3] = "cpu3",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002033 [TEGRA_POWERGATE_CPU0] = "cpu0",
2034 [TEGRA_POWERGATE_C0NC] = "c0nc",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002035 [TEGRA_POWERGATE_SOR] = "sor",
2036 [TEGRA_POWERGATE_DIS] = "dis",
2037 [TEGRA_POWERGATE_DISB] = "disb",
2038 [TEGRA_POWERGATE_XUSBA] = "xusba",
2039 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2040 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2041 [TEGRA_POWERGATE_VIC] = "vic",
2042 [TEGRA_POWERGATE_IRAM] = "iram",
2043 [TEGRA_POWERGATE_NVDEC] = "nvdec",
2044 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
2045 [TEGRA_POWERGATE_AUD] = "aud",
2046 [TEGRA_POWERGATE_DFD] = "dfd",
2047 [TEGRA_POWERGATE_VE2] = "ve2",
2048};
2049
2050static const u8 tegra210_cpu_powergates[] = {
2051 TEGRA_POWERGATE_CPU0,
2052 TEGRA_POWERGATE_CPU1,
2053 TEGRA_POWERGATE_CPU2,
2054 TEGRA_POWERGATE_CPU3,
2055};
2056
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002057#define TEGRA210_IO_PAD_TABLE(_pad) \
2058 /* .id .dpd .voltage .name */ \
2059 _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \
2060 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \
2061 _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \
2062 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2063 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2064 _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \
2065 _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \
2066 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \
2067 _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \
2068 _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \
2069 _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \
2070 _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \
2071 _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \
2072 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2073 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
2074 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
2075 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
2076 _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \
2077 _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \
2078 _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \
2079 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
2080 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2081 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
2082 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2083 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
2084 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
2085 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2086 _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \
2087 _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \
2088 _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \
2089 _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \
2090 _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \
2091 _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \
2092 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2093 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2094 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2095 _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \
2096 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias")
2097
Laxman Dewangan21b49912016-10-10 15:14:34 +02002098static const struct tegra_io_pad_soc tegra210_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002099 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002100};
2101
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002102static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
2103 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc2fe4692015-03-23 11:31:29 +01002104};
2105
2106static const struct tegra_pmc_soc tegra210_pmc_soc = {
2107 .num_powergates = ARRAY_SIZE(tegra210_powergates),
2108 .powergates = tegra210_powergates,
2109 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
2110 .cpu_powergates = tegra210_cpu_powergates,
2111 .has_tsense_reset = true,
2112 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002113 .has_impl_33v_pwr = false,
Peter De Schrijvera263394a2018-01-25 16:00:13 +02002114 .needs_mbist_war = true,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002115 .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
2116 .io_pads = tegra210_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002117 .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
2118 .pin_descs = tegra210_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002119 .regs = &tegra20_pmc_regs,
2120 .init = tegra20_pmc_init,
2121 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302122 .reset_sources = tegra30_reset_sources,
2123 .num_reset_sources = 5,
2124 .reset_levels = NULL,
2125 .num_reset_levels = 0,
Thierry Redingc2fe4692015-03-23 11:31:29 +01002126};
2127
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002128#define TEGRA186_IO_PAD_TABLE(_pad) \
2129 /* .id .dpd .voltage .name */ \
2130 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2131 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2132 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2133 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2134 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \
2135 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \
2136 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2137 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \
2138 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2139 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2140 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2141 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \
2142 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2143 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2144 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2145 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \
2146 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \
2147 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \
2148 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2149 _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \
2150 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \
2151 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \
2152 _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \
2153 _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \
2154 _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \
2155 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \
2156 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \
2157 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \
2158 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \
2159 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \
2160 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \
2161 _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \
2162 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \
2163 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \
2164 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \
2165 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \
2166 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \
2167 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv")
2168
Thierry Redingc641ec62017-08-30 12:42:34 +02002169static const struct tegra_io_pad_soc tegra186_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002170 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
Thierry Redingc641ec62017-08-30 12:42:34 +02002171};
2172
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002173static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
2174 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc641ec62017-08-30 12:42:34 +02002175};
2176
2177static const struct tegra_pmc_regs tegra186_pmc_regs = {
2178 .scratch0 = 0x2000,
2179 .dpd_req = 0x74,
2180 .dpd_status = 0x78,
2181 .dpd2_req = 0x7c,
2182 .dpd2_status = 0x80,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302183 .rst_status = 0x70,
2184 .rst_source_shift = 0x2,
2185 .rst_source_mask = 0x3C,
2186 .rst_level_shift = 0x0,
2187 .rst_level_mask = 0x3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002188};
2189
2190static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2191 struct device_node *np,
2192 bool invert)
2193{
2194 struct resource regs;
2195 void __iomem *wake;
2196 u32 value;
2197 int index;
2198
2199 index = of_property_match_string(np, "reg-names", "wake");
2200 if (index < 0) {
2201 pr_err("failed to find PMC wake registers\n");
2202 return;
2203 }
2204
2205 of_address_to_resource(np, index, &regs);
2206
2207 wake = ioremap_nocache(regs.start, resource_size(&regs));
2208 if (!wake) {
2209 pr_err("failed to map PMC wake registers\n");
2210 return;
2211 }
2212
2213 value = readl(wake + WAKE_AOWAKE_CTRL);
2214
2215 if (invert)
2216 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
2217 else
2218 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
2219
2220 writel(value, wake + WAKE_AOWAKE_CTRL);
2221
2222 iounmap(wake);
2223}
2224
2225static const struct tegra_pmc_soc tegra186_pmc_soc = {
2226 .num_powergates = 0,
2227 .powergates = NULL,
2228 .num_cpu_powergates = 0,
2229 .cpu_powergates = NULL,
2230 .has_tsense_reset = false,
2231 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002232 .has_impl_33v_pwr = true,
Thierry Redingc641ec62017-08-30 12:42:34 +02002233 .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
2234 .io_pads = tegra186_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002235 .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
2236 .pin_descs = tegra186_pin_descs,
Thierry Redingc641ec62017-08-30 12:42:34 +02002237 .regs = &tegra186_pmc_regs,
2238 .init = NULL,
2239 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302240 .reset_sources = tegra186_reset_sources,
2241 .num_reset_sources = 14,
2242 .reset_levels = tegra186_reset_levels,
2243 .num_reset_levels = 3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002244};
2245
Thierry Reding72323982014-07-11 13:19:06 +02002246static const struct of_device_id tegra_pmc_match[] = {
Mikko Perttunen56327f52018-02-20 13:58:08 +02002247 { .compatible = "nvidia,tegra194-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc641ec62017-08-30 12:42:34 +02002248 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc2fe4692015-03-23 11:31:29 +01002249 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
Thierry Reding7d71e9032015-04-29 12:42:28 +02002250 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
Thierry Reding72323982014-07-11 13:19:06 +02002251 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
2252 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
2253 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
2254 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
2255 { }
2256};
2257
2258static struct platform_driver tegra_pmc_driver = {
2259 .driver = {
2260 .name = "tegra-pmc",
2261 .suppress_bind_attrs = true,
2262 .of_match_table = tegra_pmc_match,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002263#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02002264 .pm = &tegra_pmc_pm_ops,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002265#endif
Thierry Reding72323982014-07-11 13:19:06 +02002266 },
2267 .probe = tegra_pmc_probe,
2268};
Paul Gortmaker7d4d9ed2015-05-01 20:10:57 -04002269builtin_platform_driver(tegra_pmc_driver);
Thierry Reding72323982014-07-11 13:19:06 +02002270
2271/*
2272 * Early initialization to allow access to registers in the very early boot
2273 * process.
2274 */
2275static int __init tegra_pmc_early_init(void)
2276{
2277 const struct of_device_id *match;
2278 struct device_node *np;
2279 struct resource regs;
2280 bool invert;
Thierry Reding72323982014-07-11 13:19:06 +02002281
Jon Hunter61fd2842016-06-28 11:38:26 +01002282 mutex_init(&pmc->powergates_lock);
2283
Thierry Reding72323982014-07-11 13:19:06 +02002284 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
2285 if (!np) {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002286 /*
2287 * Fall back to legacy initialization for 32-bit ARM only. All
2288 * 64-bit ARM device tree files for Tegra are required to have
2289 * a PMC node.
2290 *
2291 * This is for backwards-compatibility with old device trees
2292 * that didn't contain a PMC node. Note that in this case the
2293 * SoC data can't be matched and therefore powergating is
2294 * disabled.
2295 */
2296 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
2297 pr_warn("DT node not found, powergating disabled\n");
Thierry Reding72323982014-07-11 13:19:06 +02002298
Thierry Reding7d71e9032015-04-29 12:42:28 +02002299 regs.start = 0x7000e400;
2300 regs.end = 0x7000e7ff;
2301 regs.flags = IORESOURCE_MEM;
Thierry Reding72323982014-07-11 13:19:06 +02002302
Thierry Reding7d71e9032015-04-29 12:42:28 +02002303 pr_warn("Using memory region %pR\n", &regs);
2304 } else {
2305 /*
2306 * At this point we're not running on Tegra, so play
2307 * nice with multi-platform kernels.
2308 */
2309 return 0;
2310 }
Thierry Reding72323982014-07-11 13:19:06 +02002311 } else {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002312 /*
2313 * Extract information from the device tree if we've found a
2314 * matching node.
2315 */
2316 if (of_address_to_resource(np, 0, &regs) < 0) {
2317 pr_err("failed to get PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002318 of_node_put(np);
Thierry Reding7d71e9032015-04-29 12:42:28 +02002319 return -ENXIO;
2320 }
Thierry Reding72323982014-07-11 13:19:06 +02002321 }
2322
2323 pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
2324 if (!pmc->base) {
2325 pr_err("failed to map PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002326 of_node_put(np);
Thierry Reding72323982014-07-11 13:19:06 +02002327 return -ENXIO;
2328 }
2329
Jon Hunter11131892016-06-28 11:38:24 +01002330 if (np) {
Jon Hunter718a2422016-06-28 11:38:25 +01002331 pmc->soc = match->data;
2332
Jon Huntere2d17962016-06-30 11:56:25 +01002333 tegra_powergate_init(pmc, np);
Thierry Reding72323982014-07-11 13:19:06 +02002334
Jon Hunter11131892016-06-28 11:38:24 +01002335 /*
2336 * Invert the interrupt polarity if a PMC device tree node
2337 * exists and contains the nvidia,invert-interrupt property.
2338 */
2339 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
Thierry Reding72323982014-07-11 13:19:06 +02002340
Thierry Reding5be22552017-08-30 12:32:58 +02002341 pmc->soc->setup_irq_polarity(pmc, np, invert);
Jon Hunterb69a6252016-06-28 11:38:27 +01002342
2343 of_node_put(np);
Jon Hunter11131892016-06-28 11:38:24 +01002344 }
Thierry Reding72323982014-07-11 13:19:06 +02002345
2346 return 0;
2347}
2348early_initcall(tegra_pmc_early_init);