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/energy_model.h>
15 #include <linux/errno.h>
17 #include <linux/pm_opp.h>
18 #include <linux/regmap.h>
19 #include <linux/soc/samsung/exynos-chipid.h>
21 #include "exynos-asv.h"
22 #include "exynos5422-asv.h"
26 static int exynos_asv_update_cpu_opps(struct exynos_asv
*asv
,
29 struct exynos_asv_subsys
*subsys
= NULL
;
30 struct dev_pm_opp
*opp
;
31 unsigned int opp_freq
;
34 for (i
= 0; i
< ARRAY_SIZE(asv
->subsys
); i
++) {
35 if (of_device_is_compatible(cpu
->of_node
,
36 asv
->subsys
[i
].cpu_dt_compat
)) {
37 subsys
= &asv
->subsys
[i
];
44 for (i
= 0; i
< subsys
->table
.num_rows
; i
++) {
45 unsigned int new_volt
, volt
;
48 opp_freq
= exynos_asv_opp_get_frequency(subsys
, i
);
50 opp
= dev_pm_opp_find_freq_exact(cpu
, opp_freq
* MHZ
, true);
52 dev_info(asv
->dev
, "cpu%d opp%d, freq: %u missing\n",
53 cpu
->id
, i
, opp_freq
);
58 volt
= dev_pm_opp_get_voltage(opp
);
59 new_volt
= asv
->opp_get_voltage(subsys
, i
, volt
);
65 ret
= dev_pm_opp_adjust_voltage(cpu
, opp_freq
* MHZ
,
66 new_volt
, new_volt
, new_volt
);
69 "Failed to adjust OPP %u Hz/%u uV for cpu%d\n",
70 opp_freq
, new_volt
, cpu
->id
);
73 "Adjusted OPP %u Hz/%u -> %u uV, cpu%d\n",
74 opp_freq
, volt
, new_volt
, cpu
->id
);
80 static int exynos_asv_update_opps(struct exynos_asv
*asv
)
82 struct opp_table
*last_opp_table
= NULL
;
86 for_each_possible_cpu(cpuid
) {
87 struct opp_table
*opp_table
;
89 cpu
= get_cpu_device(cpuid
);
93 opp_table
= dev_pm_opp_get_opp_table(cpu
);
94 if (IS_ERR(opp_table
))
97 if (!last_opp_table
|| opp_table
!= last_opp_table
) {
98 last_opp_table
= opp_table
;
100 ret
= exynos_asv_update_cpu_opps(asv
, cpu
);
103 * Update EM power values since OPP
104 * voltage values may have changed.
106 em_dev_update_chip_binning(cpu
);
108 dev_err(asv
->dev
, "Couldn't udate OPPs for cpu%d\n",
113 dev_pm_opp_put_opp_table(opp_table
);
119 int exynos_asv_init(struct device
*dev
, struct regmap
*regmap
)
121 int (*probe_func
)(struct exynos_asv
*asv
);
122 struct exynos_asv
*asv
;
123 struct device
*cpu_dev
;
127 asv
= devm_kzalloc(dev
, sizeof(*asv
), GFP_KERNEL
);
131 asv
->chipid_regmap
= regmap
;
133 ret
= regmap_read(asv
->chipid_regmap
, EXYNOS_CHIPID_REG_PRO_ID
,
136 dev_err(dev
, "Cannot read revision from ChipID: %d\n", ret
);
140 switch (product_id
& EXYNOS_MASK
) {
142 probe_func
= exynos5422_asv_init
;
145 dev_dbg(dev
, "No ASV support for this SoC\n");
146 devm_kfree(dev
, asv
);
150 cpu_dev
= get_cpu_device(0);
151 ret
= dev_pm_opp_get_opp_count(cpu_dev
);
153 return -EPROBE_DEFER
;
155 ret
= of_property_read_u32(dev
->of_node
, "samsung,asv-bin",
158 asv
->of_bin
= -EINVAL
;
160 for (i
= 0; i
< ARRAY_SIZE(asv
->subsys
); i
++)
161 asv
->subsys
[i
].asv
= asv
;
163 ret
= probe_func(asv
);
167 return exynos_asv_update_opps(asv
);