drivers: cpufreq: Support H616

This commit is contained in:
orangepi-xunlong 2023-06-13 16:43:32 +08:00
parent f9660829b4
commit 1dc411a145
2 changed files with 81 additions and 37 deletions

View File

@ -102,6 +102,7 @@ static const struct of_device_id allowlist[] __initconst = {
*/
static const struct of_device_id blocklist[] __initconst = {
{ .compatible = "allwinner,sun50i-h6", },
{ .compatible = "allwinner,sun50i-h616", },
{ .compatible = "arm,vexpress", },

View File

@ -19,25 +19,62 @@
#define MAX_NAME_LEN 7
#define NVMEM_MASK 0x7
#define NVMEM_SHIFT 5
#define SUN50I_H616_NVMEM_MASK 0x22
#define SUN50I_H616_NVMEM_SHIFT 5
#define SUN50I_H6_NVMEM_MASK 0x7
#define SUN50I_H6_NVMEM_SHIFT 5
struct sunxi_cpufreq_soc_data {
u32 (*efuse_xlate) (void *efuse);
};
static struct platform_device *cpufreq_dt_pdev, *sun50i_cpufreq_pdev;
static u32 sun50i_h616_efuse_xlate(void *efuse)
{
u32 efuse_value = (*(u32 *)efuse >> SUN50I_H616_NVMEM_SHIFT) &
SUN50I_H616_NVMEM_MASK;
/* Tested as h616 soc. Expected efuse values are 1 -3,
slowest to fastest */
if (efuse_value >=1 && efuse_value <= 3)
return efuse_value - 1;
else
return 0;
};
static u32 sun50i_h6_efuse_xlate(void *efuse)
{
u32 efuse_value = (*(u32 *)efuse >> SUN50I_H6_NVMEM_SHIFT) &
SUN50I_H6_NVMEM_MASK;
/*
* We treat unexpected efuse values as if the SoC was from
* the slowest bin. Expected efuse values are 1 -3, slowest
* to fastest.
*/
if (efuse_value >= 1 && efuse_value <= 3)
return efuse_value - 1;
else
return 0;
};
/**
* sun50i_cpufreq_get_efuse() - Determine speed grade from efuse value
* @soc_data: pointer to sunxi_cpufreq_soc_data context
* @versions: Set to the value parsed from efuse
*
* Returns 0 if success.
*/
static int sun50i_cpufreq_get_efuse(u32 *versions)
static int sun50i_cpufreq_get_efuse(const struct sunxi_cpufreq_soc_data *soc_data,
u32 *versions)
{
struct nvmem_cell *speedbin_nvmem;
struct device_node *np;
struct device *cpu_dev;
u32 *speedbin, efuse_value;
u32 *speedbin;
size_t len;
int ret;
cpu_dev = get_cpu_device(0);
if (!cpu_dev)
@ -47,9 +84,9 @@ static int sun50i_cpufreq_get_efuse(u32 *versions)
if (!np)
return -ENOENT;
ret = of_device_is_compatible(np,
"allwinner,sun50i-h6-operating-points");
if (!ret) {
if (of_device_is_compatible(np, "allwinner,sun50i-h6-operating-points")) {}
else if (of_device_is_compatible(np, "allwinner,sun50i-h616-operating-points")) {}
else {
of_node_put(np);
return -ENOENT;
}
@ -65,17 +102,7 @@ static int sun50i_cpufreq_get_efuse(u32 *versions)
if (IS_ERR(speedbin))
return PTR_ERR(speedbin);
efuse_value = (*speedbin >> NVMEM_SHIFT) & NVMEM_MASK;
/*
* We treat unexpected efuse values as if the SoC was from
* the slowest bin. Expected efuse values are 1-3, slowest
* to fastest.
*/
if (efuse_value >= 1 && efuse_value <= 3)
*versions = efuse_value - 1;
else
*versions = 0;
*versions = soc_data->efuse_xlate(speedbin);
kfree(speedbin);
return 0;
@ -83,23 +110,31 @@ static int sun50i_cpufreq_get_efuse(u32 *versions)
static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
{
int *opp_tokens;
const struct of_device_id *match;
struct opp_table **opp_tables;
char name[MAX_NAME_LEN];
unsigned int cpu;
u32 speed = 0;
int ret;
opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
match = dev_get_platdata(&pdev->dev);
if (!match) {
return -EINVAL;
}
opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables),
GFP_KERNEL);
if (!opp_tokens)
if (!opp_tables)
return -ENOMEM;
ret = sun50i_cpufreq_get_efuse(&speed);
ret = sun50i_cpufreq_get_efuse(match->data, &speed);
if (ret) {
kfree(opp_tokens);
kfree(opp_tables);
return ret;
}
printk("sun50i-cpufreq-nvmem: will use speed%d CPU OPPs\n", speed);
snprintf(name, MAX_NAME_LEN, "speed%d", speed);
for_each_possible_cpu(cpu) {
@ -110,9 +145,9 @@ static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
goto free_opp;
}
opp_tokens[cpu] = dev_pm_opp_set_prop_name(cpu_dev, name);
if (opp_tokens[cpu] < 0) {
ret = opp_tokens[cpu];
opp_tables[cpu] = dev_pm_opp_set_prop_name(cpu_dev, name);
if (opp_tables[cpu] < 0) {
ret = opp_tables[cpu];
pr_err("Failed to set prop name\n");
goto free_opp;
}
@ -121,7 +156,7 @@ static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
NULL, 0);
if (!IS_ERR(cpufreq_dt_pdev)) {
platform_set_drvdata(pdev, opp_tokens);
platform_set_drvdata(pdev, opp_tables);
return 0;
}
@ -130,23 +165,23 @@ static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
free_opp:
for_each_possible_cpu(cpu)
dev_pm_opp_put_prop_name(opp_tokens[cpu]);
kfree(opp_tokens);
dev_pm_opp_put_prop_name(opp_tables[cpu]);
kfree(opp_tables);
return ret;
}
static int sun50i_cpufreq_nvmem_remove(struct platform_device *pdev)
{
int *opp_tokens = platform_get_drvdata(pdev);
int *opp_tables = platform_get_drvdata(pdev);
unsigned int cpu;
platform_device_unregister(cpufreq_dt_pdev);
for_each_possible_cpu(cpu)
dev_pm_opp_put_prop_name(opp_tokens[cpu]);
dev_pm_opp_put_prop_name(opp_tables[cpu]);
kfree(opp_tokens);
kfree(opp_tables);
return 0;
}
@ -159,8 +194,17 @@ static struct platform_driver sun50i_cpufreq_driver = {
},
};
static const struct sunxi_cpufreq_soc_data sun50i_h616_data = {
.efuse_xlate = sun50i_h616_efuse_xlate,
};
static const struct sunxi_cpufreq_soc_data sun50i_h6_data = {
.efuse_xlate = sun50i_h6_efuse_xlate,
};
static const struct of_device_id sun50i_cpufreq_match_list[] = {
{ .compatible = "allwinner,sun50i-h6" },
{ .compatible = "allwinner,sun50i-h6", .data = &sun50i_h6_data },
{ .compatible = "allwinner,sun50i-h616", .data = &sun50i_h616_data },
{}
};
MODULE_DEVICE_TABLE(of, sun50i_cpufreq_match_list);
@ -195,9 +239,8 @@ static int __init sun50i_cpufreq_init(void)
if (unlikely(ret < 0))
return ret;
sun50i_cpufreq_pdev =
platform_device_register_simple("sun50i-cpufreq-nvmem",
-1, NULL, 0);
sun50i_cpufreq_pdev = platform_device_register_data(NULL,
"sun50i-cpufreq-nvmem", -1, match, sizeof(*match));
ret = PTR_ERR_OR_ZERO(sun50i_cpufreq_pdev);
if (ret == 0)
return 0;