1 // SPDX-License-Identifier: GPL-2.0-only
3 * Match running platform with pre-defined OPP values for CPUFreq
5 * Author: Ajit Pal Singh <ajitpal.singh@st.com>
6 * Lee Jones <lee.jones@linaro.org>
8 * Copyright (C) 2015 STMicroelectronics (R&D) Limited
11 #include <linux/cpu.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_opp.h>
18 #include <linux/regmap.h>
20 #define VERSION_ELEMENTS 3
21 #define MAX_PCODE_NAME_LEN 16
23 #define VERSION_SHIFT 28
24 #define HW_INFO_INDEX 1
25 #define MAJOR_ID_INDEX 1
26 #define MINOR_ID_INDEX 2
29 * Only match on "suitable for ALL versions" entries
31 * This will be used with the BIT() macro. It sets the
32 * top bit of a 32bit value and is equal to 0x80000000.
34 #define DEFAULT_VERSION 31
43 * struct sti_cpufreq_ddata - ST CPUFreq Driver Data
46 * @syscfg_eng: Engineering Syscon register map
47 * @syscfg: Syscon register map
49 static struct sti_cpufreq_ddata
{
51 struct regmap
*syscfg_eng
;
52 struct regmap
*syscfg
;
55 static int sti_cpufreq_fetch_major(void) {
56 struct device_node
*np
= ddata
.cpu
->of_node
;
57 struct device
*dev
= ddata
.cpu
;
58 unsigned int major_offset
;
62 ret
= of_property_read_u32_index(np
, "st,syscfg",
63 MAJOR_ID_INDEX
, &major_offset
);
65 dev_err(dev
, "No major number offset provided in %pOF [%d]\n",
70 ret
= regmap_read(ddata
.syscfg
, major_offset
, &socid
);
72 dev_err(dev
, "Failed to read major number from syscon [%d]\n",
77 return ((socid
>> VERSION_SHIFT
) & 0xf) + 1;
80 static int sti_cpufreq_fetch_minor(void)
82 struct device
*dev
= ddata
.cpu
;
83 struct device_node
*np
= dev
->of_node
;
84 unsigned int minor_offset
;
88 ret
= of_property_read_u32_index(np
, "st,syscfg-eng",
89 MINOR_ID_INDEX
, &minor_offset
);
92 "No minor number offset provided %pOF [%d]\n",
97 ret
= regmap_read(ddata
.syscfg_eng
, minor_offset
, &minid
);
100 "Failed to read the minor number from syscon [%d]\n",
108 static int sti_cpufreq_fetch_regmap_field(const struct reg_field
*reg_fields
,
109 int hw_info_offset
, int field
)
111 struct regmap_field
*regmap_field
;
112 struct reg_field reg_field
= reg_fields
[field
];
113 struct device
*dev
= ddata
.cpu
;
117 reg_field
.reg
= hw_info_offset
;
118 regmap_field
= devm_regmap_field_alloc(dev
,
121 if (IS_ERR(regmap_field
)) {
122 dev_err(dev
, "Failed to allocate reg field\n");
123 return PTR_ERR(regmap_field
);
126 ret
= regmap_field_read(regmap_field
, &value
);
128 dev_err(dev
, "Failed to read %s code\n",
129 field
? "SUBSTRATE" : "PCODE");
136 static const struct reg_field sti_stih407_dvfs_regfields
[DVFS_MAX_REGFIELDS
] = {
137 [PCODE
] = REG_FIELD(0, 16, 19),
138 [SUBSTRATE
] = REG_FIELD(0, 0, 2),
141 static const struct reg_field
*sti_cpufreq_match(void)
143 if (of_machine_is_compatible("st,stih407") ||
144 of_machine_is_compatible("st,stih410") ||
145 of_machine_is_compatible("st,stih418"))
146 return sti_stih407_dvfs_regfields
;
151 static int sti_cpufreq_set_opp_info(void)
153 struct device
*dev
= ddata
.cpu
;
154 struct device_node
*np
= dev
->of_node
;
155 const struct reg_field
*reg_fields
;
156 unsigned int hw_info_offset
;
157 unsigned int version
[VERSION_ELEMENTS
];
158 int pcode
, substrate
, major
, minor
;
160 char name
[MAX_PCODE_NAME_LEN
];
161 struct dev_pm_opp_config config
= {
162 .supported_hw
= version
,
163 .supported_hw_count
= ARRAY_SIZE(version
),
167 reg_fields
= sti_cpufreq_match();
169 dev_err(dev
, "This SoC doesn't support voltage scaling\n");
173 ret
= of_property_read_u32_index(np
, "st,syscfg-eng",
174 HW_INFO_INDEX
, &hw_info_offset
);
176 dev_warn(dev
, "Failed to read HW info offset from DT\n");
177 substrate
= DEFAULT_VERSION
;
182 pcode
= sti_cpufreq_fetch_regmap_field(reg_fields
,
186 dev_warn(dev
, "Failed to obtain process code\n");
187 /* Use default pcode */
191 substrate
= sti_cpufreq_fetch_regmap_field(reg_fields
,
195 dev_warn(dev
, "Failed to obtain substrate code\n");
196 /* Use default substrate */
197 substrate
= DEFAULT_VERSION
;
201 major
= sti_cpufreq_fetch_major();
203 dev_err(dev
, "Failed to obtain major version\n");
204 /* Use default major number */
205 major
= DEFAULT_VERSION
;
208 minor
= sti_cpufreq_fetch_minor();
210 dev_err(dev
, "Failed to obtain minor version\n");
211 /* Use default minor number */
212 minor
= DEFAULT_VERSION
;
215 snprintf(name
, MAX_PCODE_NAME_LEN
, "pcode%d", pcode
);
217 version
[0] = BIT(major
);
218 version
[1] = BIT(minor
);
219 version
[2] = BIT(substrate
);
221 opp_token
= dev_pm_opp_set_config(dev
, &config
);
223 dev_err(dev
, "Failed to set OPP config\n");
227 dev_dbg(dev
, "pcode: %d major: %d minor: %d substrate: %d\n",
228 pcode
, major
, minor
, substrate
);
229 dev_dbg(dev
, "version[0]: %x version[1]: %x version[2]: %x\n",
230 version
[0], version
[1], version
[2]);
235 static int sti_cpufreq_fetch_syscon_registers(void)
237 struct device
*dev
= ddata
.cpu
;
238 struct device_node
*np
= dev
->of_node
;
240 ddata
.syscfg
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
241 if (IS_ERR(ddata
.syscfg
)) {
242 dev_err(dev
, "\"st,syscfg\" not supplied\n");
243 return PTR_ERR(ddata
.syscfg
);
246 ddata
.syscfg_eng
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg-eng");
247 if (IS_ERR(ddata
.syscfg_eng
)) {
248 dev_err(dev
, "\"st,syscfg-eng\" not supplied\n");
249 return PTR_ERR(ddata
.syscfg_eng
);
255 static int __init
sti_cpufreq_init(void)
259 if ((!of_machine_is_compatible("st,stih407")) &&
260 (!of_machine_is_compatible("st,stih410")) &&
261 (!of_machine_is_compatible("st,stih418")))
264 ddata
.cpu
= get_cpu_device(0);
266 dev_err(ddata
.cpu
, "Failed to get device for CPU0\n");
267 goto skip_voltage_scaling
;
270 if (!of_property_present(ddata
.cpu
->of_node
, "operating-points-v2")) {
271 dev_err(ddata
.cpu
, "OPP-v2 not supported\n");
272 goto skip_voltage_scaling
;
275 ret
= sti_cpufreq_fetch_syscon_registers();
277 goto skip_voltage_scaling
;
279 ret
= sti_cpufreq_set_opp_info();
281 goto register_cpufreq_dt
;
283 skip_voltage_scaling
:
284 dev_err(ddata
.cpu
, "Not doing voltage scaling\n");
287 platform_device_register_simple("cpufreq-dt", -1, NULL
, 0);
291 module_init(sti_cpufreq_init
);
293 static const struct of_device_id __maybe_unused sti_cpufreq_of_match
[] = {
294 { .compatible
= "st,stih407" },
295 { .compatible
= "st,stih410" },
296 { .compatible
= "st,stih418" },
299 MODULE_DEVICE_TABLE(of
, sti_cpufreq_of_match
);
301 MODULE_DESCRIPTION("STMicroelectronics CPUFreq/OPP driver");
302 MODULE_AUTHOR("Ajitpal Singh <ajitpal.singh@st.com>");
303 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
304 MODULE_LICENSE("GPL v2");