11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e13cf046SDave Gerlach /*
3e13cf046SDave Gerlach * TI CPUFreq/OPP hw-supported driver
4e13cf046SDave Gerlach *
5e13cf046SDave Gerlach * Copyright (C) 2016-2017 Texas Instruments, Inc.
6e13cf046SDave Gerlach * Dave Gerlach <d-gerlach@ti.com>
7e13cf046SDave Gerlach */
8e13cf046SDave Gerlach
9e13cf046SDave Gerlach #include <linux/cpu.h>
10e13cf046SDave Gerlach #include <linux/io.h>
11e13cf046SDave Gerlach #include <linux/mfd/syscon.h>
12db410b2bSDave Gerlach #include <linux/module.h>
13149ab864SPaul Gortmaker #include <linux/init.h>
14e13cf046SDave Gerlach #include <linux/of.h>
15a70eb93aSRob Herring #include <linux/platform_device.h>
16e13cf046SDave Gerlach #include <linux/pm_opp.h>
17e13cf046SDave Gerlach #include <linux/regmap.h>
18e13cf046SDave Gerlach #include <linux/slab.h>
19e13cf046SDave Gerlach
20e13cf046SDave Gerlach #define REVISION_MASK 0xF
21e13cf046SDave Gerlach #define REVISION_SHIFT 28
22e13cf046SDave Gerlach
23e13cf046SDave Gerlach #define AM33XX_800M_ARM_MPU_MAX_FREQ 0x1E2F
24e13cf046SDave Gerlach #define AM43XX_600M_ARM_MPU_MAX_FREQ 0xFFA
25e13cf046SDave Gerlach
26e13cf046SDave Gerlach #define DRA7_EFUSE_HAS_OD_MPU_OPP 11
27e13cf046SDave Gerlach #define DRA7_EFUSE_HAS_HIGH_MPU_OPP 15
280ea4fb29SLokesh Vutla #define DRA76_EFUSE_HAS_PLUS_MPU_OPP 18
29e13cf046SDave Gerlach #define DRA7_EFUSE_HAS_ALL_MPU_OPP 23
300ea4fb29SLokesh Vutla #define DRA76_EFUSE_HAS_ALL_MPU_OPP 24
31e13cf046SDave Gerlach
32e13cf046SDave Gerlach #define DRA7_EFUSE_NOM_MPU_OPP BIT(0)
33e13cf046SDave Gerlach #define DRA7_EFUSE_OD_MPU_OPP BIT(1)
34e13cf046SDave Gerlach #define DRA7_EFUSE_HIGH_MPU_OPP BIT(2)
350ea4fb29SLokesh Vutla #define DRA76_EFUSE_PLUS_MPU_OPP BIT(3)
36e13cf046SDave Gerlach
37b4bc9f9eSH. Nikolaus Schaller #define OMAP3_CONTROL_DEVICE_STATUS 0x4800244C
38b4bc9f9eSH. Nikolaus Schaller #define OMAP3_CONTROL_IDCODE 0x4830A204
39b4bc9f9eSH. Nikolaus Schaller #define OMAP34xx_ProdID_SKUID 0x4830A20C
40b4bc9f9eSH. Nikolaus Schaller #define OMAP3_SYSCON_BASE (0x48000000 + 0x2000 + 0x270)
41b4bc9f9eSH. Nikolaus Schaller
42aac0293aSDave Gerlach #define AM625_EFUSE_K_MPU_OPP 11
43aac0293aSDave Gerlach #define AM625_EFUSE_S_MPU_OPP 19
44aac0293aSDave Gerlach #define AM625_EFUSE_T_MPU_OPP 20
45aac0293aSDave Gerlach
46aac0293aSDave Gerlach #define AM625_SUPPORT_K_MPU_OPP BIT(0)
47aac0293aSDave Gerlach #define AM625_SUPPORT_S_MPU_OPP BIT(1)
48aac0293aSDave Gerlach #define AM625_SUPPORT_T_MPU_OPP BIT(2)
49aac0293aSDave Gerlach
50e13cf046SDave Gerlach #define VERSION_COUNT 2
51e13cf046SDave Gerlach
52e13cf046SDave Gerlach struct ti_cpufreq_data;
53e13cf046SDave Gerlach
54e13cf046SDave Gerlach struct ti_cpufreq_soc_data {
5542e52616SH. Nikolaus Schaller const char * const *reg_names;
56e13cf046SDave Gerlach unsigned long (*efuse_xlate)(struct ti_cpufreq_data *opp_data,
57e13cf046SDave Gerlach unsigned long efuse);
58e13cf046SDave Gerlach unsigned long efuse_fallback;
59e13cf046SDave Gerlach unsigned long efuse_offset;
60e13cf046SDave Gerlach unsigned long efuse_mask;
61e13cf046SDave Gerlach unsigned long efuse_shift;
62e13cf046SDave Gerlach unsigned long rev_offset;
63c8343e83SDave Gerlach bool multi_regulator;
64*4d3608aeSNishanth Menon /* Backward compatibility hack: Might have missing syscon */
65*4d3608aeSNishanth Menon #define TI_QUIRK_SYSCON_MAY_BE_MISSING 0x1
66*4d3608aeSNishanth Menon u8 quirks;
67e13cf046SDave Gerlach };
68e13cf046SDave Gerlach
69e13cf046SDave Gerlach struct ti_cpufreq_data {
70e13cf046SDave Gerlach struct device *cpu_dev;
71e13cf046SDave Gerlach struct device_node *opp_node;
72e13cf046SDave Gerlach struct regmap *syscon;
73e13cf046SDave Gerlach const struct ti_cpufreq_soc_data *soc_data;
74e13cf046SDave Gerlach };
75e13cf046SDave Gerlach
amx3_efuse_xlate(struct ti_cpufreq_data * opp_data,unsigned long efuse)76e13cf046SDave Gerlach static unsigned long amx3_efuse_xlate(struct ti_cpufreq_data *opp_data,
77e13cf046SDave Gerlach unsigned long efuse)
78e13cf046SDave Gerlach {
79e13cf046SDave Gerlach if (!efuse)
80e13cf046SDave Gerlach efuse = opp_data->soc_data->efuse_fallback;
81e13cf046SDave Gerlach /* AM335x and AM437x use "OPP disable" bits, so invert */
82e13cf046SDave Gerlach return ~efuse;
83e13cf046SDave Gerlach }
84e13cf046SDave Gerlach
dra7_efuse_xlate(struct ti_cpufreq_data * opp_data,unsigned long efuse)85e13cf046SDave Gerlach static unsigned long dra7_efuse_xlate(struct ti_cpufreq_data *opp_data,
86e13cf046SDave Gerlach unsigned long efuse)
87e13cf046SDave Gerlach {
88e13cf046SDave Gerlach unsigned long calculated_efuse = DRA7_EFUSE_NOM_MPU_OPP;
89e13cf046SDave Gerlach
90e13cf046SDave Gerlach /*
91e13cf046SDave Gerlach * The efuse on dra7 and am57 parts contains a specific
92e13cf046SDave Gerlach * value indicating the highest available OPP.
93e13cf046SDave Gerlach */
94e13cf046SDave Gerlach
95e13cf046SDave Gerlach switch (efuse) {
960ea4fb29SLokesh Vutla case DRA76_EFUSE_HAS_PLUS_MPU_OPP:
970ea4fb29SLokesh Vutla case DRA76_EFUSE_HAS_ALL_MPU_OPP:
980ea4fb29SLokesh Vutla calculated_efuse |= DRA76_EFUSE_PLUS_MPU_OPP;
99df561f66SGustavo A. R. Silva fallthrough;
100e13cf046SDave Gerlach case DRA7_EFUSE_HAS_ALL_MPU_OPP:
101e13cf046SDave Gerlach case DRA7_EFUSE_HAS_HIGH_MPU_OPP:
102e13cf046SDave Gerlach calculated_efuse |= DRA7_EFUSE_HIGH_MPU_OPP;
103df561f66SGustavo A. R. Silva fallthrough;
104e13cf046SDave Gerlach case DRA7_EFUSE_HAS_OD_MPU_OPP:
105e13cf046SDave Gerlach calculated_efuse |= DRA7_EFUSE_OD_MPU_OPP;
106e13cf046SDave Gerlach }
107e13cf046SDave Gerlach
108e13cf046SDave Gerlach return calculated_efuse;
109e13cf046SDave Gerlach }
110e13cf046SDave Gerlach
omap3_efuse_xlate(struct ti_cpufreq_data * opp_data,unsigned long efuse)111b4bc9f9eSH. Nikolaus Schaller static unsigned long omap3_efuse_xlate(struct ti_cpufreq_data *opp_data,
112b4bc9f9eSH. Nikolaus Schaller unsigned long efuse)
113b4bc9f9eSH. Nikolaus Schaller {
114b4bc9f9eSH. Nikolaus Schaller /* OPP enable bit ("Speed Binned") */
115b4bc9f9eSH. Nikolaus Schaller return BIT(efuse);
116b4bc9f9eSH. Nikolaus Schaller }
117b4bc9f9eSH. Nikolaus Schaller
am625_efuse_xlate(struct ti_cpufreq_data * opp_data,unsigned long efuse)118aac0293aSDave Gerlach static unsigned long am625_efuse_xlate(struct ti_cpufreq_data *opp_data,
119aac0293aSDave Gerlach unsigned long efuse)
120aac0293aSDave Gerlach {
121aac0293aSDave Gerlach unsigned long calculated_efuse = AM625_SUPPORT_K_MPU_OPP;
122aac0293aSDave Gerlach
123aac0293aSDave Gerlach switch (efuse) {
124aac0293aSDave Gerlach case AM625_EFUSE_T_MPU_OPP:
125aac0293aSDave Gerlach calculated_efuse |= AM625_SUPPORT_T_MPU_OPP;
126aac0293aSDave Gerlach fallthrough;
127aac0293aSDave Gerlach case AM625_EFUSE_S_MPU_OPP:
128aac0293aSDave Gerlach calculated_efuse |= AM625_SUPPORT_S_MPU_OPP;
129aac0293aSDave Gerlach fallthrough;
130aac0293aSDave Gerlach case AM625_EFUSE_K_MPU_OPP:
131aac0293aSDave Gerlach calculated_efuse |= AM625_SUPPORT_K_MPU_OPP;
132aac0293aSDave Gerlach }
133aac0293aSDave Gerlach
134aac0293aSDave Gerlach return calculated_efuse;
135aac0293aSDave Gerlach }
136aac0293aSDave Gerlach
137e13cf046SDave Gerlach static struct ti_cpufreq_soc_data am3x_soc_data = {
138e13cf046SDave Gerlach .efuse_xlate = amx3_efuse_xlate,
139e13cf046SDave Gerlach .efuse_fallback = AM33XX_800M_ARM_MPU_MAX_FREQ,
140e13cf046SDave Gerlach .efuse_offset = 0x07fc,
141e13cf046SDave Gerlach .efuse_mask = 0x1fff,
142e13cf046SDave Gerlach .rev_offset = 0x600,
143c8343e83SDave Gerlach .multi_regulator = false,
144e13cf046SDave Gerlach };
145e13cf046SDave Gerlach
146e13cf046SDave Gerlach static struct ti_cpufreq_soc_data am4x_soc_data = {
147e13cf046SDave Gerlach .efuse_xlate = amx3_efuse_xlate,
148e13cf046SDave Gerlach .efuse_fallback = AM43XX_600M_ARM_MPU_MAX_FREQ,
149e13cf046SDave Gerlach .efuse_offset = 0x0610,
150e13cf046SDave Gerlach .efuse_mask = 0x3f,
151e13cf046SDave Gerlach .rev_offset = 0x600,
152c8343e83SDave Gerlach .multi_regulator = false,
153e13cf046SDave Gerlach };
154e13cf046SDave Gerlach
155e13cf046SDave Gerlach static struct ti_cpufreq_soc_data dra7_soc_data = {
156e13cf046SDave Gerlach .efuse_xlate = dra7_efuse_xlate,
157e13cf046SDave Gerlach .efuse_offset = 0x020c,
158e13cf046SDave Gerlach .efuse_mask = 0xf80000,
159e13cf046SDave Gerlach .efuse_shift = 19,
160e13cf046SDave Gerlach .rev_offset = 0x204,
161c8343e83SDave Gerlach .multi_regulator = true,
162e13cf046SDave Gerlach };
163e13cf046SDave Gerlach
164b4bc9f9eSH. Nikolaus Schaller /*
165b4bc9f9eSH. Nikolaus Schaller * OMAP35x TRM (SPRUF98K):
166b4bc9f9eSH. Nikolaus Schaller * CONTROL_IDCODE (0x4830 A204) describes Silicon revisions.
167b4bc9f9eSH. Nikolaus Schaller * Control OMAP Status Register 15:0 (Address 0x4800 244C)
168b4bc9f9eSH. Nikolaus Schaller * to separate between omap3503, omap3515, omap3525, omap3530
169b4bc9f9eSH. Nikolaus Schaller * and feature presence.
170b4bc9f9eSH. Nikolaus Schaller * There are encodings for versions limited to 400/266MHz
171b4bc9f9eSH. Nikolaus Schaller * but we ignore.
172b4bc9f9eSH. Nikolaus Schaller * Not clear if this also holds for omap34xx.
173b4bc9f9eSH. Nikolaus Schaller * some eFuse values e.g. CONTROL_FUSE_OPP1_VDD1
174b4bc9f9eSH. Nikolaus Schaller * are stored in the SYSCON register range
175b4bc9f9eSH. Nikolaus Schaller * Register 0x4830A20C [ProdID.SKUID] [0:3]
176b4bc9f9eSH. Nikolaus Schaller * 0x0 for normal 600/430MHz device.
177b4bc9f9eSH. Nikolaus Schaller * 0x8 for 720/520MHz device.
178b4bc9f9eSH. Nikolaus Schaller * Not clear what omap34xx value is.
179b4bc9f9eSH. Nikolaus Schaller */
180b4bc9f9eSH. Nikolaus Schaller
181b4bc9f9eSH. Nikolaus Schaller static struct ti_cpufreq_soc_data omap34xx_soc_data = {
182b4bc9f9eSH. Nikolaus Schaller .efuse_xlate = omap3_efuse_xlate,
183b4bc9f9eSH. Nikolaus Schaller .efuse_offset = OMAP34xx_ProdID_SKUID - OMAP3_SYSCON_BASE,
184b4bc9f9eSH. Nikolaus Schaller .efuse_shift = 3,
185b4bc9f9eSH. Nikolaus Schaller .efuse_mask = BIT(3),
186b4bc9f9eSH. Nikolaus Schaller .rev_offset = OMAP3_CONTROL_IDCODE - OMAP3_SYSCON_BASE,
187b4bc9f9eSH. Nikolaus Schaller .multi_regulator = false,
188*4d3608aeSNishanth Menon .quirks = TI_QUIRK_SYSCON_MAY_BE_MISSING,
189b4bc9f9eSH. Nikolaus Schaller };
190b4bc9f9eSH. Nikolaus Schaller
191b4bc9f9eSH. Nikolaus Schaller /*
192b4bc9f9eSH. Nikolaus Schaller * AM/DM37x TRM (SPRUGN4M)
193b4bc9f9eSH. Nikolaus Schaller * CONTROL_IDCODE (0x4830 A204) describes Silicon revisions.
194b4bc9f9eSH. Nikolaus Schaller * Control Device Status Register 15:0 (Address 0x4800 244C)
195b4bc9f9eSH. Nikolaus Schaller * to separate between am3703, am3715, dm3725, dm3730
196b4bc9f9eSH. Nikolaus Schaller * and feature presence.
197b4bc9f9eSH. Nikolaus Schaller * Speed Binned = Bit 9
198b4bc9f9eSH. Nikolaus Schaller * 0 800/600 MHz
199b4bc9f9eSH. Nikolaus Schaller * 1 1000/800 MHz
200b4bc9f9eSH. Nikolaus Schaller * some eFuse values e.g. CONTROL_FUSE_OPP 1G_VDD1
201b4bc9f9eSH. Nikolaus Schaller * are stored in the SYSCON register range.
202b4bc9f9eSH. Nikolaus Schaller * There is no 0x4830A20C [ProdID.SKUID] register (exists but
203b4bc9f9eSH. Nikolaus Schaller * seems to always read as 0).
204b4bc9f9eSH. Nikolaus Schaller */
205b4bc9f9eSH. Nikolaus Schaller
20687686cc8SViresh Kumar static const char * const omap3_reg_names[] = {"cpu0", "vbb", NULL};
20742e52616SH. Nikolaus Schaller
208b4bc9f9eSH. Nikolaus Schaller static struct ti_cpufreq_soc_data omap36xx_soc_data = {
20942e52616SH. Nikolaus Schaller .reg_names = omap3_reg_names,
210b4bc9f9eSH. Nikolaus Schaller .efuse_xlate = omap3_efuse_xlate,
211b4bc9f9eSH. Nikolaus Schaller .efuse_offset = OMAP3_CONTROL_DEVICE_STATUS - OMAP3_SYSCON_BASE,
212b4bc9f9eSH. Nikolaus Schaller .efuse_shift = 9,
213b4bc9f9eSH. Nikolaus Schaller .efuse_mask = BIT(9),
214b4bc9f9eSH. Nikolaus Schaller .rev_offset = OMAP3_CONTROL_IDCODE - OMAP3_SYSCON_BASE,
215341afbc9SH. Nikolaus Schaller .multi_regulator = true,
216*4d3608aeSNishanth Menon .quirks = TI_QUIRK_SYSCON_MAY_BE_MISSING,
217b4bc9f9eSH. Nikolaus Schaller };
218b4bc9f9eSH. Nikolaus Schaller
2193fbeef39SAdam Ford /*
2203fbeef39SAdam Ford * AM3517 is quite similar to AM/DM37x except that it has no
2213fbeef39SAdam Ford * high speed grade eFuse and no abb ldo
2223fbeef39SAdam Ford */
2233fbeef39SAdam Ford
2243fbeef39SAdam Ford static struct ti_cpufreq_soc_data am3517_soc_data = {
2253fbeef39SAdam Ford .efuse_xlate = omap3_efuse_xlate,
2263fbeef39SAdam Ford .efuse_offset = OMAP3_CONTROL_DEVICE_STATUS - OMAP3_SYSCON_BASE,
2273fbeef39SAdam Ford .efuse_shift = 0,
2283fbeef39SAdam Ford .efuse_mask = 0,
2293fbeef39SAdam Ford .rev_offset = OMAP3_CONTROL_IDCODE - OMAP3_SYSCON_BASE,
2303fbeef39SAdam Ford .multi_regulator = false,
231*4d3608aeSNishanth Menon .quirks = TI_QUIRK_SYSCON_MAY_BE_MISSING,
2323fbeef39SAdam Ford };
2333fbeef39SAdam Ford
234aac0293aSDave Gerlach static struct ti_cpufreq_soc_data am625_soc_data = {
235aac0293aSDave Gerlach .efuse_xlate = am625_efuse_xlate,
236aac0293aSDave Gerlach .efuse_offset = 0x0018,
237aac0293aSDave Gerlach .efuse_mask = 0x07c0,
238aac0293aSDave Gerlach .efuse_shift = 0x6,
239aac0293aSDave Gerlach .rev_offset = 0x0014,
240aac0293aSDave Gerlach .multi_regulator = false,
241aac0293aSDave Gerlach };
2423fbeef39SAdam Ford
243e13cf046SDave Gerlach /**
244e13cf046SDave Gerlach * ti_cpufreq_get_efuse() - Parse and return efuse value present on SoC
245e13cf046SDave Gerlach * @opp_data: pointer to ti_cpufreq_data context
246e13cf046SDave Gerlach * @efuse_value: Set to the value parsed from efuse
247e13cf046SDave Gerlach *
248e13cf046SDave Gerlach * Returns error code if efuse not read properly.
249e13cf046SDave Gerlach */
ti_cpufreq_get_efuse(struct ti_cpufreq_data * opp_data,u32 * efuse_value)250e13cf046SDave Gerlach static int ti_cpufreq_get_efuse(struct ti_cpufreq_data *opp_data,
251e13cf046SDave Gerlach u32 *efuse_value)
252e13cf046SDave Gerlach {
253e13cf046SDave Gerlach struct device *dev = opp_data->cpu_dev;
254e13cf046SDave Gerlach u32 efuse;
255e13cf046SDave Gerlach int ret;
256e13cf046SDave Gerlach
257e13cf046SDave Gerlach ret = regmap_read(opp_data->syscon, opp_data->soc_data->efuse_offset,
258e13cf046SDave Gerlach &efuse);
259*4d3608aeSNishanth Menon if (opp_data->soc_data->quirks & TI_QUIRK_SYSCON_MAY_BE_MISSING && ret == -EIO) {
260b4bc9f9eSH. Nikolaus Schaller /* not a syscon register! */
261b4bc9f9eSH. Nikolaus Schaller void __iomem *regs = ioremap(OMAP3_SYSCON_BASE +
262b4bc9f9eSH. Nikolaus Schaller opp_data->soc_data->efuse_offset, 4);
263b4bc9f9eSH. Nikolaus Schaller
264b4bc9f9eSH. Nikolaus Schaller if (!regs)
265b4bc9f9eSH. Nikolaus Schaller return -ENOMEM;
266b4bc9f9eSH. Nikolaus Schaller efuse = readl(regs);
267b4bc9f9eSH. Nikolaus Schaller iounmap(regs);
268b4bc9f9eSH. Nikolaus Schaller }
269b4bc9f9eSH. Nikolaus Schaller else if (ret) {
270e13cf046SDave Gerlach dev_err(dev,
271e13cf046SDave Gerlach "Failed to read the efuse value from syscon: %d\n",
272e13cf046SDave Gerlach ret);
273e13cf046SDave Gerlach return ret;
274e13cf046SDave Gerlach }
275e13cf046SDave Gerlach
276e13cf046SDave Gerlach efuse = (efuse & opp_data->soc_data->efuse_mask);
277e13cf046SDave Gerlach efuse >>= opp_data->soc_data->efuse_shift;
278e13cf046SDave Gerlach
279e13cf046SDave Gerlach *efuse_value = opp_data->soc_data->efuse_xlate(opp_data, efuse);
280e13cf046SDave Gerlach
281e13cf046SDave Gerlach return 0;
282e13cf046SDave Gerlach }
283e13cf046SDave Gerlach
284e13cf046SDave Gerlach /**
285e13cf046SDave Gerlach * ti_cpufreq_get_rev() - Parse and return rev value present on SoC
286e13cf046SDave Gerlach * @opp_data: pointer to ti_cpufreq_data context
287e13cf046SDave Gerlach * @revision_value: Set to the value parsed from revision register
288e13cf046SDave Gerlach *
289e13cf046SDave Gerlach * Returns error code if revision not read properly.
290e13cf046SDave Gerlach */
ti_cpufreq_get_rev(struct ti_cpufreq_data * opp_data,u32 * revision_value)291e13cf046SDave Gerlach static int ti_cpufreq_get_rev(struct ti_cpufreq_data *opp_data,
292e13cf046SDave Gerlach u32 *revision_value)
293e13cf046SDave Gerlach {
294e13cf046SDave Gerlach struct device *dev = opp_data->cpu_dev;
295e13cf046SDave Gerlach u32 revision;
296e13cf046SDave Gerlach int ret;
297e13cf046SDave Gerlach
298e13cf046SDave Gerlach ret = regmap_read(opp_data->syscon, opp_data->soc_data->rev_offset,
299e13cf046SDave Gerlach &revision);
300*4d3608aeSNishanth Menon if (opp_data->soc_data->quirks & TI_QUIRK_SYSCON_MAY_BE_MISSING && ret == -EIO) {
301b4bc9f9eSH. Nikolaus Schaller /* not a syscon register! */
302b4bc9f9eSH. Nikolaus Schaller void __iomem *regs = ioremap(OMAP3_SYSCON_BASE +
303b4bc9f9eSH. Nikolaus Schaller opp_data->soc_data->rev_offset, 4);
304b4bc9f9eSH. Nikolaus Schaller
305b4bc9f9eSH. Nikolaus Schaller if (!regs)
306b4bc9f9eSH. Nikolaus Schaller return -ENOMEM;
307b4bc9f9eSH. Nikolaus Schaller revision = readl(regs);
308b4bc9f9eSH. Nikolaus Schaller iounmap(regs);
309b4bc9f9eSH. Nikolaus Schaller }
310b4bc9f9eSH. Nikolaus Schaller else if (ret) {
311e13cf046SDave Gerlach dev_err(dev,
312e13cf046SDave Gerlach "Failed to read the revision number from syscon: %d\n",
313e13cf046SDave Gerlach ret);
314e13cf046SDave Gerlach return ret;
315e13cf046SDave Gerlach }
316e13cf046SDave Gerlach
317e13cf046SDave Gerlach *revision_value = BIT((revision >> REVISION_SHIFT) & REVISION_MASK);
318e13cf046SDave Gerlach
319e13cf046SDave Gerlach return 0;
320e13cf046SDave Gerlach }
321e13cf046SDave Gerlach
ti_cpufreq_setup_syscon_register(struct ti_cpufreq_data * opp_data)322e13cf046SDave Gerlach static int ti_cpufreq_setup_syscon_register(struct ti_cpufreq_data *opp_data)
323e13cf046SDave Gerlach {
324e13cf046SDave Gerlach struct device *dev = opp_data->cpu_dev;
325e13cf046SDave Gerlach struct device_node *np = opp_data->opp_node;
326e13cf046SDave Gerlach
327e13cf046SDave Gerlach opp_data->syscon = syscon_regmap_lookup_by_phandle(np,
328e13cf046SDave Gerlach "syscon");
329e13cf046SDave Gerlach if (IS_ERR(opp_data->syscon)) {
330e13cf046SDave Gerlach dev_err(dev,
331e13cf046SDave Gerlach "\"syscon\" is missing, cannot use OPPv2 table.\n");
332e13cf046SDave Gerlach return PTR_ERR(opp_data->syscon);
333e13cf046SDave Gerlach }
334e13cf046SDave Gerlach
335e13cf046SDave Gerlach return 0;
336e13cf046SDave Gerlach }
337e13cf046SDave Gerlach
338e13cf046SDave Gerlach static const struct of_device_id ti_cpufreq_of_match[] = {
339e13cf046SDave Gerlach { .compatible = "ti,am33xx", .data = &am3x_soc_data, },
3403fbeef39SAdam Ford { .compatible = "ti,am3517", .data = &am3517_soc_data, },
341039cc1c1SDave Gerlach { .compatible = "ti,am43", .data = &am4x_soc_data, },
342e13cf046SDave Gerlach { .compatible = "ti,dra7", .data = &dra7_soc_data },
343b4bc9f9eSH. Nikolaus Schaller { .compatible = "ti,omap34xx", .data = &omap34xx_soc_data, },
344b4bc9f9eSH. Nikolaus Schaller { .compatible = "ti,omap36xx", .data = &omap36xx_soc_data, },
345aac0293aSDave Gerlach { .compatible = "ti,am625", .data = &am625_soc_data, },
3465008e4c8SVibhore Vardhan { .compatible = "ti,am62a7", .data = &am625_soc_data, },
347b4bc9f9eSH. Nikolaus Schaller /* legacy */
348b4bc9f9eSH. Nikolaus Schaller { .compatible = "ti,omap3430", .data = &omap34xx_soc_data, },
349b4bc9f9eSH. Nikolaus Schaller { .compatible = "ti,omap3630", .data = &omap36xx_soc_data, },
350e13cf046SDave Gerlach {},
351e13cf046SDave Gerlach };
352e13cf046SDave Gerlach
ti_cpufreq_match_node(void)353d98ccfc3SDave Gerlach static const struct of_device_id *ti_cpufreq_match_node(void)
354d98ccfc3SDave Gerlach {
355d98ccfc3SDave Gerlach struct device_node *np;
356d98ccfc3SDave Gerlach const struct of_device_id *match;
357d98ccfc3SDave Gerlach
358d98ccfc3SDave Gerlach np = of_find_node_by_path("/");
359d98ccfc3SDave Gerlach match = of_match_node(ti_cpufreq_of_match, np);
360d98ccfc3SDave Gerlach of_node_put(np);
361d98ccfc3SDave Gerlach
362d98ccfc3SDave Gerlach return match;
363d98ccfc3SDave Gerlach }
364d98ccfc3SDave Gerlach
ti_cpufreq_probe(struct platform_device * pdev)365db410b2bSDave Gerlach static int ti_cpufreq_probe(struct platform_device *pdev)
366e13cf046SDave Gerlach {
367e13cf046SDave Gerlach u32 version[VERSION_COUNT];
368e13cf046SDave Gerlach const struct of_device_id *match;
369e13cf046SDave Gerlach struct ti_cpufreq_data *opp_data;
37087686cc8SViresh Kumar const char * const default_reg_names[] = {"vdd", "vbb", NULL};
371e13cf046SDave Gerlach int ret;
372f88d152dSViresh Kumar struct dev_pm_opp_config config = {
373f88d152dSViresh Kumar .supported_hw = version,
374f88d152dSViresh Kumar .supported_hw_count = ARRAY_SIZE(version),
375f88d152dSViresh Kumar };
376e13cf046SDave Gerlach
377d98ccfc3SDave Gerlach match = dev_get_platdata(&pdev->dev);
378e13cf046SDave Gerlach if (!match)
379e13cf046SDave Gerlach return -ENODEV;
380e13cf046SDave Gerlach
381d7231f99SSuman Anna opp_data = devm_kzalloc(&pdev->dev, sizeof(*opp_data), GFP_KERNEL);
382e13cf046SDave Gerlach if (!opp_data)
383e13cf046SDave Gerlach return -ENOMEM;
384e13cf046SDave Gerlach
385e13cf046SDave Gerlach opp_data->soc_data = match->data;
386e13cf046SDave Gerlach
387e13cf046SDave Gerlach opp_data->cpu_dev = get_cpu_device(0);
388e13cf046SDave Gerlach if (!opp_data->cpu_dev) {
389e13cf046SDave Gerlach pr_err("%s: Failed to get device for CPU0\n", __func__);
390d7231f99SSuman Anna return -ENODEV;
391e13cf046SDave Gerlach }
392e13cf046SDave Gerlach
393e13cf046SDave Gerlach opp_data->opp_node = dev_pm_opp_of_get_opp_desc_node(opp_data->cpu_dev);
394e13cf046SDave Gerlach if (!opp_data->opp_node) {
395e13cf046SDave Gerlach dev_info(opp_data->cpu_dev,
396e13cf046SDave Gerlach "OPP-v2 not supported, cpufreq-dt will attempt to use legacy tables.\n");
397e13cf046SDave Gerlach goto register_cpufreq_dt;
398e13cf046SDave Gerlach }
399e13cf046SDave Gerlach
400e13cf046SDave Gerlach ret = ti_cpufreq_setup_syscon_register(opp_data);
401e13cf046SDave Gerlach if (ret)
402e13cf046SDave Gerlach goto fail_put_node;
403e13cf046SDave Gerlach
404e13cf046SDave Gerlach /*
405e13cf046SDave Gerlach * OPPs determine whether or not they are supported based on
406e13cf046SDave Gerlach * two metrics:
407e13cf046SDave Gerlach * 0 - SoC Revision
408e13cf046SDave Gerlach * 1 - eFuse value
409e13cf046SDave Gerlach */
410e13cf046SDave Gerlach ret = ti_cpufreq_get_rev(opp_data, &version[0]);
411e13cf046SDave Gerlach if (ret)
412e13cf046SDave Gerlach goto fail_put_node;
413e13cf046SDave Gerlach
414e13cf046SDave Gerlach ret = ti_cpufreq_get_efuse(opp_data, &version[1]);
415e13cf046SDave Gerlach if (ret)
416e13cf046SDave Gerlach goto fail_put_node;
417e13cf046SDave Gerlach
418c8343e83SDave Gerlach if (opp_data->soc_data->multi_regulator) {
41942e52616SH. Nikolaus Schaller if (opp_data->soc_data->reg_names)
420f88d152dSViresh Kumar config.regulator_names = opp_data->soc_data->reg_names;
421f88d152dSViresh Kumar else
422f88d152dSViresh Kumar config.regulator_names = default_reg_names;
423c8343e83SDave Gerlach }
424f88d152dSViresh Kumar
425f88d152dSViresh Kumar ret = dev_pm_opp_set_config(opp_data->cpu_dev, &config);
426f88d152dSViresh Kumar if (ret < 0) {
427d94eb194SPrimoz Fiser dev_err_probe(opp_data->cpu_dev, ret, "Failed to set OPP config\n");
428f88d152dSViresh Kumar goto fail_put_node;
429c8343e83SDave Gerlach }
430c8343e83SDave Gerlach
431c8343e83SDave Gerlach of_node_put(opp_data->opp_node);
432f88d152dSViresh Kumar
433e13cf046SDave Gerlach register_cpufreq_dt:
434e13cf046SDave Gerlach platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
435e13cf046SDave Gerlach
436e13cf046SDave Gerlach return 0;
437e13cf046SDave Gerlach
438e13cf046SDave Gerlach fail_put_node:
439e13cf046SDave Gerlach of_node_put(opp_data->opp_node);
440e13cf046SDave Gerlach
441e13cf046SDave Gerlach return ret;
442e13cf046SDave Gerlach }
443db410b2bSDave Gerlach
ti_cpufreq_init(void)444f7968c22SXiu Jianfeng static int __init ti_cpufreq_init(void)
445db410b2bSDave Gerlach {
446d98ccfc3SDave Gerlach const struct of_device_id *match;
447d98ccfc3SDave Gerlach
448d98ccfc3SDave Gerlach /* Check to ensure we are on a compatible platform */
449d98ccfc3SDave Gerlach match = ti_cpufreq_match_node();
450d98ccfc3SDave Gerlach if (match)
451d98ccfc3SDave Gerlach platform_device_register_data(NULL, "ti-cpufreq", -1, match,
452d98ccfc3SDave Gerlach sizeof(*match));
453d98ccfc3SDave Gerlach
454db410b2bSDave Gerlach return 0;
455db410b2bSDave Gerlach }
456db410b2bSDave Gerlach module_init(ti_cpufreq_init);
457db410b2bSDave Gerlach
458db410b2bSDave Gerlach static struct platform_driver ti_cpufreq_driver = {
459db410b2bSDave Gerlach .probe = ti_cpufreq_probe,
460db410b2bSDave Gerlach .driver = {
461db410b2bSDave Gerlach .name = "ti-cpufreq",
462db410b2bSDave Gerlach },
463db410b2bSDave Gerlach };
46444a264eeSViresh Kumar builtin_platform_driver(ti_cpufreq_driver);
465db410b2bSDave Gerlach
466db410b2bSDave Gerlach MODULE_DESCRIPTION("TI CPUFreq/OPP hw-supported driver");
467db410b2bSDave Gerlach MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");
468db410b2bSDave Gerlach MODULE_LICENSE("GPL v2");
469