1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 MediaTek Inc.
7 #include <linux/devfreq.h>
8 #include <linux/minmax.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_opp.h>
13 #include <linux/regulator/consumer.h>
15 struct mtk_ccifreq_platform_data {
23 struct mtk_ccifreq_drv {
25 struct devfreq *devfreq;
26 struct regulator *proc_reg;
27 struct regulator *sram_reg;
29 struct clk *inter_clk;
31 unsigned long pre_freq;
32 /* Avoid race condition for regulators between notify and policy */
33 struct mutex reg_lock;
34 struct notifier_block opp_nb;
35 const struct mtk_ccifreq_platform_data *soc_data;
39 static int mtk_ccifreq_set_voltage(struct mtk_ccifreq_drv *drv, int new_voltage)
41 const struct mtk_ccifreq_platform_data *soc_data = drv->soc_data;
42 struct device *dev = drv->dev;
43 int pre_voltage, pre_vsram, new_vsram, vsram, voltage, ret;
44 int retry_max = drv->vtrack_max;
47 ret = regulator_set_voltage(drv->proc_reg, new_voltage,
48 drv->soc_data->proc_max_volt);
52 pre_voltage = regulator_get_voltage(drv->proc_reg);
53 if (pre_voltage < 0) {
54 dev_err(dev, "invalid vproc value: %d\n", pre_voltage);
58 pre_vsram = regulator_get_voltage(drv->sram_reg);
60 dev_err(dev, "invalid vsram value: %d\n", pre_vsram);
64 new_vsram = clamp(new_voltage + soc_data->min_volt_shift,
65 soc_data->sram_min_volt, soc_data->sram_max_volt);
68 if (pre_voltage <= new_voltage) {
69 vsram = clamp(pre_voltage + soc_data->max_volt_shift,
70 soc_data->sram_min_volt, new_vsram);
71 ret = regulator_set_voltage(drv->sram_reg, vsram,
72 soc_data->sram_max_volt);
76 if (vsram == soc_data->sram_max_volt ||
77 new_vsram == soc_data->sram_min_volt)
78 voltage = new_voltage;
80 voltage = vsram - soc_data->min_volt_shift;
82 ret = regulator_set_voltage(drv->proc_reg, voltage,
83 soc_data->proc_max_volt);
85 regulator_set_voltage(drv->sram_reg, pre_vsram,
86 soc_data->sram_max_volt);
89 } else if (pre_voltage > new_voltage) {
90 voltage = max(new_voltage,
91 pre_vsram - soc_data->max_volt_shift);
92 ret = regulator_set_voltage(drv->proc_reg, voltage,
93 soc_data->proc_max_volt);
97 if (voltage == new_voltage)
100 vsram = max(new_vsram,
101 voltage + soc_data->min_volt_shift);
103 ret = regulator_set_voltage(drv->sram_reg, vsram,
104 soc_data->sram_max_volt);
106 regulator_set_voltage(drv->proc_reg, pre_voltage,
107 soc_data->proc_max_volt);
112 pre_voltage = voltage;
115 if (--retry_max < 0) {
117 "over loop count, failed to set voltage\n");
120 } while (voltage != new_voltage || vsram != new_vsram);
125 static int mtk_ccifreq_target(struct device *dev, unsigned long *freq,
128 struct mtk_ccifreq_drv *drv = dev_get_drvdata(dev);
130 struct dev_pm_opp *opp;
131 unsigned long opp_rate;
132 int voltage, pre_voltage, inter_voltage, target_voltage, ret;
137 if (drv->pre_freq == *freq)
140 inter_voltage = drv->inter_voltage;
141 cci_pll = clk_get_parent(drv->cci_clk);
144 opp = devfreq_recommended_opp(dev, &opp_rate, 1);
146 dev_err(dev, "failed to find opp for freq: %ld\n", opp_rate);
150 mutex_lock(&drv->reg_lock);
152 voltage = dev_pm_opp_get_voltage(opp);
155 pre_voltage = regulator_get_voltage(drv->proc_reg);
156 if (pre_voltage < 0) {
157 dev_err(dev, "invalid vproc value: %d\n", pre_voltage);
162 /* scale up: set voltage first then freq. */
163 target_voltage = max(inter_voltage, voltage);
164 if (pre_voltage <= target_voltage) {
165 ret = mtk_ccifreq_set_voltage(drv, target_voltage);
167 dev_err(dev, "failed to scale up voltage\n");
168 goto out_restore_voltage;
172 /* switch the cci clock to intermediate clock source. */
173 ret = clk_set_parent(drv->cci_clk, drv->inter_clk);
175 dev_err(dev, "failed to re-parent cci clock\n");
176 goto out_restore_voltage;
179 /* set the original clock to target rate. */
180 ret = clk_set_rate(cci_pll, *freq);
182 dev_err(dev, "failed to set cci pll rate: %d\n", ret);
183 clk_set_parent(drv->cci_clk, cci_pll);
184 goto out_restore_voltage;
187 /* switch the cci clock back to the original clock source. */
188 ret = clk_set_parent(drv->cci_clk, cci_pll);
190 dev_err(dev, "failed to re-parent cci clock\n");
191 mtk_ccifreq_set_voltage(drv, inter_voltage);
196 * If the new voltage is lower than the intermediate voltage or the
197 * original voltage, scale down to the new voltage.
199 if (voltage < inter_voltage || voltage < pre_voltage) {
200 ret = mtk_ccifreq_set_voltage(drv, voltage);
202 dev_err(dev, "failed to scale down voltage\n");
207 drv->pre_freq = *freq;
208 mutex_unlock(&drv->reg_lock);
213 mtk_ccifreq_set_voltage(drv, pre_voltage);
216 mutex_unlock(&drv->reg_lock);
220 static int mtk_ccifreq_opp_notifier(struct notifier_block *nb,
221 unsigned long event, void *data)
223 struct dev_pm_opp *opp = data;
224 struct mtk_ccifreq_drv *drv;
225 unsigned long freq, volt;
227 drv = container_of(nb, struct mtk_ccifreq_drv, opp_nb);
229 if (event == OPP_EVENT_ADJUST_VOLTAGE) {
230 freq = dev_pm_opp_get_freq(opp);
232 mutex_lock(&drv->reg_lock);
233 /* current opp item is changed */
234 if (freq == drv->pre_freq) {
235 volt = dev_pm_opp_get_voltage(opp);
236 mtk_ccifreq_set_voltage(drv, volt);
238 mutex_unlock(&drv->reg_lock);
244 static struct devfreq_dev_profile mtk_ccifreq_profile = {
245 .target = mtk_ccifreq_target,
248 static int mtk_ccifreq_probe(struct platform_device *pdev)
250 struct device *dev = &pdev->dev;
251 struct mtk_ccifreq_drv *drv;
252 struct devfreq_passive_data *passive_data;
253 struct dev_pm_opp *opp;
254 unsigned long rate, opp_volt;
257 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
262 drv->soc_data = (const struct mtk_ccifreq_platform_data *)
263 of_device_get_match_data(&pdev->dev);
264 mutex_init(&drv->reg_lock);
265 platform_set_drvdata(pdev, drv);
267 drv->cci_clk = devm_clk_get(dev, "cci");
268 if (IS_ERR(drv->cci_clk)) {
269 ret = PTR_ERR(drv->cci_clk);
270 return dev_err_probe(dev, ret, "failed to get cci clk\n");
273 drv->inter_clk = devm_clk_get(dev, "intermediate");
274 if (IS_ERR(drv->inter_clk)) {
275 ret = PTR_ERR(drv->inter_clk);
276 return dev_err_probe(dev, ret,
277 "failed to get intermediate clk\n");
280 drv->proc_reg = devm_regulator_get_optional(dev, "proc");
281 if (IS_ERR(drv->proc_reg)) {
282 ret = PTR_ERR(drv->proc_reg);
283 return dev_err_probe(dev, ret,
284 "failed to get proc regulator\n");
287 ret = regulator_enable(drv->proc_reg);
289 dev_err(dev, "failed to enable proc regulator\n");
293 drv->sram_reg = devm_regulator_get_optional(dev, "sram");
294 if (IS_ERR(drv->sram_reg)) {
295 ret = PTR_ERR(drv->sram_reg);
296 if (ret == -EPROBE_DEFER)
297 goto out_free_resources;
299 drv->sram_reg = NULL;
301 ret = regulator_enable(drv->sram_reg);
303 dev_err(dev, "failed to enable sram regulator\n");
304 goto out_free_resources;
309 * We assume min voltage is 0 and tracking target voltage using
310 * min_volt_shift for each iteration.
311 * The retry_max is 3 times of expected iteration count.
313 drv->vtrack_max = 3 * DIV_ROUND_UP(max(drv->soc_data->sram_max_volt,
314 drv->soc_data->proc_max_volt),
315 drv->soc_data->min_volt_shift);
317 ret = clk_prepare_enable(drv->cci_clk);
319 goto out_free_resources;
321 ret = dev_pm_opp_of_add_table(dev);
323 dev_err(dev, "failed to add opp table: %d\n", ret);
324 goto out_disable_cci_clk;
327 rate = clk_get_rate(drv->inter_clk);
328 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
331 dev_err(dev, "failed to get intermediate opp: %d\n", ret);
332 goto out_remove_opp_table;
334 drv->inter_voltage = dev_pm_opp_get_voltage(opp);
338 opp = dev_pm_opp_find_freq_floor(drv->dev, &rate);
340 dev_err(dev, "failed to get opp\n");
342 goto out_remove_opp_table;
345 opp_volt = dev_pm_opp_get_voltage(opp);
347 ret = mtk_ccifreq_set_voltage(drv, opp_volt);
349 dev_err(dev, "failed to scale to highest voltage %lu in proc_reg\n",
351 goto out_remove_opp_table;
354 passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
357 goto out_remove_opp_table;
360 passive_data->parent_type = CPUFREQ_PARENT_DEV;
361 drv->devfreq = devm_devfreq_add_device(dev, &mtk_ccifreq_profile,
364 if (IS_ERR(drv->devfreq)) {
366 dev_err(dev, "failed to add devfreq device: %ld\n",
367 PTR_ERR(drv->devfreq));
368 goto out_remove_opp_table;
371 drv->opp_nb.notifier_call = mtk_ccifreq_opp_notifier;
372 ret = dev_pm_opp_register_notifier(dev, &drv->opp_nb);
374 dev_err(dev, "failed to register opp notifier: %d\n", ret);
375 goto out_remove_opp_table;
379 out_remove_opp_table:
380 dev_pm_opp_of_remove_table(dev);
383 clk_disable_unprepare(drv->cci_clk);
386 if (regulator_is_enabled(drv->proc_reg))
387 regulator_disable(drv->proc_reg);
388 if (drv->sram_reg && regulator_is_enabled(drv->sram_reg))
389 regulator_disable(drv->sram_reg);
394 static int mtk_ccifreq_remove(struct platform_device *pdev)
396 struct device *dev = &pdev->dev;
397 struct mtk_ccifreq_drv *drv;
399 drv = platform_get_drvdata(pdev);
401 dev_pm_opp_unregister_notifier(dev, &drv->opp_nb);
402 dev_pm_opp_of_remove_table(dev);
403 clk_disable_unprepare(drv->cci_clk);
404 regulator_disable(drv->proc_reg);
406 regulator_disable(drv->sram_reg);
411 static const struct mtk_ccifreq_platform_data mt8183_platform_data = {
412 .min_volt_shift = 100000,
413 .max_volt_shift = 200000,
414 .proc_max_volt = 1150000,
417 static const struct mtk_ccifreq_platform_data mt8186_platform_data = {
418 .min_volt_shift = 100000,
419 .max_volt_shift = 250000,
420 .proc_max_volt = 1118750,
421 .sram_min_volt = 850000,
422 .sram_max_volt = 1118750,
425 static const struct of_device_id mtk_ccifreq_machines[] = {
426 { .compatible = "mediatek,mt8183-cci", .data = &mt8183_platform_data },
427 { .compatible = "mediatek,mt8186-cci", .data = &mt8186_platform_data },
430 MODULE_DEVICE_TABLE(of, mtk_ccifreq_machines);
432 static struct platform_driver mtk_ccifreq_platdrv = {
433 .probe = mtk_ccifreq_probe,
434 .remove = mtk_ccifreq_remove,
436 .name = "mtk-ccifreq",
437 .of_match_table = mtk_ccifreq_machines,
440 module_platform_driver(mtk_ccifreq_platdrv);
442 MODULE_DESCRIPTION("MediaTek CCI devfreq driver");
443 MODULE_AUTHOR("Jia-Wei Chang <jia-wei.chang@mediatek.com>");
444 MODULE_LICENSE("GPL v2");