On 25.06.2023 22:25, Dmitry Baryshkov wrote: > Add a simple driver that handles scaling of L2 frequency and voltages. > > Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@xxxxxxxxxx> > --- > drivers/soc/qcom/Kconfig | 9 ++ > drivers/soc/qcom/Makefile | 1 + > drivers/soc/qcom/krait-l2-cache.c | 190 ++++++++++++++++++++++++++++++ > 3 files changed, 200 insertions(+) > create mode 100644 drivers/soc/qcom/krait-l2-cache.c > > diff --git a/drivers/soc/qcom/Kconfig b/drivers/soc/qcom/Kconfig > index e597799e8121..01090b7a3c06 100644 > --- a/drivers/soc/qcom/Kconfig > +++ b/drivers/soc/qcom/Kconfig > @@ -70,6 +70,15 @@ config QCOM_LLCC > SDM845. This provides interfaces to clients that use the LLCC. > Say yes here to enable LLCC slice driver. > > +config QCOM_KRAIT_L2_CACHE > + tristate "Qualcomm Krait L2 cache scaling" > + depends on ARCH_QCOM && ARM || COMPILE_TEST > + select INTERCONNECT > + select INTERCONNECT_CLK > + default ARM_QCOM_CPUFREQ_NVMEM > + help > + The driver for scaling the L2 cache frequency on Qualcomm Krait platforms. > + > config QCOM_KRYO_L2_ACCESSORS > bool > depends on (ARCH_QCOM || COMPILE_TEST) && ARM64 > diff --git a/drivers/soc/qcom/Makefile b/drivers/soc/qcom/Makefile > index 99114c71092b..cdd1cc96e9f9 100644 > --- a/drivers/soc/qcom/Makefile > +++ b/drivers/soc/qcom/Makefile > @@ -31,6 +31,7 @@ obj-$(CONFIG_QCOM_APR) += apr.o > obj-$(CONFIG_QCOM_LLCC) += llcc-qcom.o > obj-$(CONFIG_QCOM_RPMHPD) += rpmhpd.o > obj-$(CONFIG_QCOM_RPMPD) += rpmpd.o > +obj-$(CONFIG_QCOM_KRAIT_L2_CACHE) += krait-l2-cache.o > obj-$(CONFIG_QCOM_KRYO_L2_ACCESSORS) += kryo-l2-accessors.o > obj-$(CONFIG_QCOM_ICC_BWMON) += icc-bwmon.o > qcom_ice-objs += ice.o > diff --git a/drivers/soc/qcom/krait-l2-cache.c b/drivers/soc/qcom/krait-l2-cache.c > new file mode 100644 > index 000000000000..af9e7b955daf > --- /dev/null > +++ b/drivers/soc/qcom/krait-l2-cache.c > @@ -0,0 +1,190 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (c) 2023, Linaro Ltd. > + */ > + > +#include <linux/clk.h> > +#include <linux/interconnect-clk.h> > +#include <linux/interconnect-provider.h> > +#include <linux/of_device.h> > +#include <linux/platform_device.h> > +#include <linux/pm_opp.h> > +#include <linux/regulator/consumer.h> > + > +#include <dt-bindings/soc/qcom,krait-l2-cache.h> > + > +/* Random ID that doesn't clash with main qnoc and OSM */ > +#define L2_MASTER_NODE 2000 > + > +/* vdd-mem and vdd-dig */ > +#define NUM_SUPPLIES 2 I *think* the code would be much, much simpler if you made the debug print, regulator set and error message a separate function and then called them explicitly, without a for-loop. > +static int krait_l2_config_regulators(struct device *dev, > + struct dev_pm_opp *old_opp, > + struct dev_pm_opp *new_opp, > + struct regulator **regulators, > + unsigned int count) > +{ > + struct dev_pm_opp_supply supplies[NUM_SUPPLIES]; > + unsigned long old_freq, freq; > + unsigned int i; > + int ret; > + > + if (WARN_ON_ONCE(count != NUM_SUPPLIES)) > + return -EINVAL; > + > + ret = dev_pm_opp_get_supplies(new_opp, supplies); > + if (WARN_ON(ret)) > + return ret; > + > + old_freq = dev_pm_opp_get_freq(old_opp); > + freq = dev_pm_opp_get_freq(new_opp); > + > + WARN_ON(!old_freq || !freq); > + if (freq > old_freq) { > + for (i = 0; i < count; i++) { > + struct regulator *reg = regulators[i]; > + struct dev_pm_opp_supply *supply = &supplies[i]; > + > + dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, > + supply->u_volt_min, supply->u_volt, supply->u_volt_max); > + > + ret = regulator_set_voltage_triplet(reg, > + supply->u_volt_min, > + supply->u_volt, > + supply->u_volt_max); > + if (ret) { > + dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", > + __func__, supply->u_volt_min, supply->u_volt, > + supply->u_volt_max, ret); > + goto restore_backwards; > + } > + } > + } else { > + for (i = count; i > 0; i--) { > + struct regulator *reg = regulators[i - 1]; > + struct dev_pm_opp_supply *supply = &supplies[i - 1]; > + > + dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, > + supply->u_volt_min, supply->u_volt, supply->u_volt_max); > + > + ret = regulator_set_voltage_triplet(reg, > + supply->u_volt_min, > + supply->u_volt, > + supply->u_volt_max); > + if (ret) { > + dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", > + __func__, supply->u_volt_min, supply->u_volt, > + supply->u_volt_max, ret); > + goto restore_forward; > + } > + } > + } > + > + return 0; > + > +restore_backwards: > + > + dev_pm_opp_get_supplies(old_opp, supplies); > + > + for (; i > 0; i--) { > + struct regulator *reg = regulators[i - 1]; > + struct dev_pm_opp_supply *supply = &supplies[i - 1]; > + > + dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, > + supply->u_volt_min, supply->u_volt, supply->u_volt_max); > + > + regulator_set_voltage_triplet(reg, > + supply->u_volt_min, > + supply->u_volt, > + supply->u_volt_max); > + } > + > + return ret; > + > +restore_forward: > + > + dev_pm_opp_get_supplies(old_opp, supplies); > + > + for ( ; i < count; i++) { > + struct regulator *reg = regulators[i]; > + struct dev_pm_opp_supply *supply = &supplies[i]; > + > + dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, > + supply->u_volt_min, supply->u_volt, supply->u_volt_max); > + > + regulator_set_voltage_triplet(reg, > + supply->u_volt_min, > + supply->u_volt, > + supply->u_volt_max); > + } > + > + return ret; > +} > + > +static int krait_l2_probe(struct platform_device *pdev) > +{ > + struct dev_pm_opp_config krait_l2_cfg = { > + .clk_names = (const char * const[]) { NULL, NULL }, > + .config_regulators = krait_l2_config_regulators, > + .regulator_names = (const char * const[]) { "vdd-mem", "vdd-dig", NULL }, > + }; > + struct icc_clk_data data[] = { > + { .name = "l2", .opp = true}, true} -> true } Konrad > + }; > + > + struct device *dev = &pdev->dev; > + struct icc_provider *provider; > + struct clk *clk; > + int ret; > + > + clk = devm_clk_get(dev, NULL); > + if (IS_ERR(clk)) > + return PTR_ERR(clk); > + > + ret = devm_pm_opp_set_config(dev, &krait_l2_cfg); > + if (ret) > + return ret; > + > + ret = devm_pm_opp_of_add_table(dev); > + if (ret) > + return ret; > + > + data[0].clk = clk; > + provider = icc_clk_register(dev, L2_MASTER_NODE, ARRAY_SIZE(data), data); > + if (IS_ERR(provider)) > + return PTR_ERR(provider); > + > + platform_set_drvdata(pdev, provider); > + > + return 0; > +} > + > +static int krait_l2_remove(struct platform_device *pdev) > +{ > + struct icc_provider *provider = platform_get_drvdata(pdev); > + > + icc_clk_unregister(provider); > + > + return 0; > +} > + > +static const struct of_device_id krait_l2_match_table[] = { > + { .compatible = "qcom,krait-l2-cache" }, > + {} > +}; > +MODULE_DEVICE_TABLE(of, krait_l2_match_table); > + > +static struct platform_driver krait_l2_driver = { > + .probe = krait_l2_probe, > + .remove = krait_l2_remove, > + .driver = { > + .name = "qcom-krait-l2", > + .of_match_table = krait_l2_match_table, > + .sync_state = icc_sync_state, > + }, > +}; > + > +module_platform_driver(krait_l2_driver); > + > +MODULE_DESCRIPTION("Qualcomm Krait L2 scaling driver"); > +MODULE_LICENSE("GPL v2");