1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 * Copyright (c) 2020 Krzysztof Kozlowski <krzk@kernel.org>
6 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * Author: Krzysztof Kozlowski <krzk@kernel.org>
9 * Samsung Exynos SoC Adaptive Supply Voltage support
12 #include <linux/cpu.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
16 #include <linux/pm_opp.h>
17 #include <linux/regmap.h>
18 #include <linux/soc/samsung/exynos-chipid.h>
20 #include "exynos-asv.h"
21 #include "exynos5422-asv.h"
25 static int exynos_asv_update_cpu_opps(struct exynos_asv *asv,
28 struct exynos_asv_subsys *subsys = NULL;
29 struct dev_pm_opp *opp;
30 unsigned int opp_freq;
33 for (i = 0; i < ARRAY_SIZE(asv->subsys); i++) {
34 if (of_device_is_compatible(cpu->of_node,
35 asv->subsys[i].cpu_dt_compat)) {
36 subsys = &asv->subsys[i];
43 for (i = 0; i < subsys->table.num_rows; i++) {
44 unsigned int new_volt, volt;
47 opp_freq = exynos_asv_opp_get_frequency(subsys, i);
49 opp = dev_pm_opp_find_freq_exact(cpu, opp_freq * MHZ, true);
51 dev_info(asv->dev, "cpu%d opp%d, freq: %u missing\n",
52 cpu->id, i, opp_freq);
57 volt = dev_pm_opp_get_voltage(opp);
58 new_volt = asv->opp_get_voltage(subsys, i, volt);
64 ret = dev_pm_opp_adjust_voltage(cpu, opp_freq * MHZ,
65 new_volt, new_volt, new_volt);
68 "Failed to adjust OPP %u Hz/%u uV for cpu%d\n",
69 opp_freq, new_volt, cpu->id);
72 "Adjusted OPP %u Hz/%u -> %u uV, cpu%d\n",
73 opp_freq, volt, new_volt, cpu->id);
79 static int exynos_asv_update_opps(struct exynos_asv *asv)
81 struct opp_table *last_opp_table = NULL;
85 for_each_possible_cpu(cpuid) {
86 struct opp_table *opp_table;
88 cpu = get_cpu_device(cpuid);
92 opp_table = dev_pm_opp_get_opp_table(cpu);
93 if (IS_ERR(opp_table))
96 if (!last_opp_table || opp_table != last_opp_table) {
97 last_opp_table = opp_table;
99 ret = exynos_asv_update_cpu_opps(asv, cpu);
101 dev_err(asv->dev, "Couldn't udate OPPs for cpu%d\n",
105 dev_pm_opp_put_opp_table(opp_table);
111 int exynos_asv_init(struct device *dev, struct regmap *regmap)
113 int (*probe_func)(struct exynos_asv *asv);
114 struct exynos_asv *asv;
115 struct device *cpu_dev;
119 asv = devm_kzalloc(dev, sizeof(*asv), GFP_KERNEL);
123 asv->chipid_regmap = regmap;
125 ret = regmap_read(asv->chipid_regmap, EXYNOS_CHIPID_REG_PRO_ID,
128 dev_err(dev, "Cannot read revision from ChipID: %d\n", ret);
132 switch (product_id & EXYNOS_MASK) {
134 probe_func = exynos5422_asv_init;
137 dev_dbg(dev, "No ASV support for this SoC\n");
138 devm_kfree(dev, asv);
142 cpu_dev = get_cpu_device(0);
143 ret = dev_pm_opp_get_opp_count(cpu_dev);
145 return -EPROBE_DEFER;
147 ret = of_property_read_u32(dev->of_node, "samsung,asv-bin",
150 asv->of_bin = -EINVAL;
152 for (i = 0; i < ARRAY_SIZE(asv->subsys); i++)
153 asv->subsys[i].asv = asv;
155 ret = probe_func(asv);
159 return exynos_asv_update_opps(asv);