18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * cpufreq driver for the SuperH processors.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2002 - 2012 Paul Mundt
58c2ecf20Sopenharmony_ci * Copyright (C) 2002 M. R. Brown
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Clock framework bits from arch/avr32/mach-at32ap/cpufreq.c
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *   Copyright (C) 2004-2007 Atmel Corporation
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
128c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
138c2ecf20Sopenharmony_ci * for more details.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "cpufreq: " fmt
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/types.h>
188c2ecf20Sopenharmony_ci#include <linux/cpufreq.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/init.h>
228c2ecf20Sopenharmony_ci#include <linux/err.h>
238c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
248c2ecf20Sopenharmony_ci#include <linux/cpu.h>
258c2ecf20Sopenharmony_ci#include <linux/smp.h>
268c2ecf20Sopenharmony_ci#include <linux/sched.h>	/* set_cpus_allowed() */
278c2ecf20Sopenharmony_ci#include <linux/clk.h>
288c2ecf20Sopenharmony_ci#include <linux/percpu.h>
298c2ecf20Sopenharmony_ci#include <linux/sh_clk.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct clk, sh_cpuclk);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistruct cpufreq_target {
348c2ecf20Sopenharmony_ci	struct cpufreq_policy	*policy;
358c2ecf20Sopenharmony_ci	unsigned int		freq;
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic unsigned int sh_cpufreq_get(unsigned int cpu)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	return (clk_get_rate(&per_cpu(sh_cpuclk, cpu)) + 500) / 1000;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic long __sh_cpufreq_target(void *arg)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct cpufreq_target *target = arg;
468c2ecf20Sopenharmony_ci	struct cpufreq_policy *policy = target->policy;
478c2ecf20Sopenharmony_ci	int cpu = policy->cpu;
488c2ecf20Sopenharmony_ci	struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
498c2ecf20Sopenharmony_ci	struct cpufreq_freqs freqs;
508c2ecf20Sopenharmony_ci	struct device *dev;
518c2ecf20Sopenharmony_ci	long freq;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (smp_processor_id() != cpu)
548c2ecf20Sopenharmony_ci		return -ENODEV;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	dev = get_cpu_device(cpu);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/* Convert target_freq from kHz to Hz */
598c2ecf20Sopenharmony_ci	freq = clk_round_rate(cpuclk, target->freq * 1000);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if (freq < (policy->min * 1000) || freq > (policy->max * 1000))
628c2ecf20Sopenharmony_ci		return -EINVAL;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	dev_dbg(dev, "requested frequency %u Hz\n", target->freq * 1000);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	freqs.old	= sh_cpufreq_get(cpu);
678c2ecf20Sopenharmony_ci	freqs.new	= (freq + 500) / 1000;
688c2ecf20Sopenharmony_ci	freqs.flags	= 0;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	cpufreq_freq_transition_begin(target->policy, &freqs);
718c2ecf20Sopenharmony_ci	clk_set_rate(cpuclk, freq);
728c2ecf20Sopenharmony_ci	cpufreq_freq_transition_end(target->policy, &freqs, 0);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	dev_dbg(dev, "set frequency %lu Hz\n", freq);
758c2ecf20Sopenharmony_ci	return 0;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci * Here we notify other drivers of the proposed change and the final change.
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_cistatic int sh_cpufreq_target(struct cpufreq_policy *policy,
828c2ecf20Sopenharmony_ci			     unsigned int target_freq,
838c2ecf20Sopenharmony_ci			     unsigned int relation)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	struct cpufreq_target data = { .policy = policy, .freq = target_freq };
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return work_on_cpu(policy->cpu, __sh_cpufreq_target, &data);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int sh_cpufreq_verify(struct cpufreq_policy_data *policy)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct clk *cpuclk = &per_cpu(sh_cpuclk, policy->cpu);
938c2ecf20Sopenharmony_ci	struct cpufreq_frequency_table *freq_table;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	freq_table = cpuclk->nr_freqs ? cpuclk->freq_table : NULL;
968c2ecf20Sopenharmony_ci	if (freq_table)
978c2ecf20Sopenharmony_ci		return cpufreq_frequency_table_verify(policy, freq_table);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	cpufreq_verify_within_cpu_limits(policy);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	policy->min = (clk_round_rate(cpuclk, 1) + 500) / 1000;
1028c2ecf20Sopenharmony_ci	policy->max = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	cpufreq_verify_within_cpu_limits(policy);
1058c2ecf20Sopenharmony_ci	return 0;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	unsigned int cpu = policy->cpu;
1118c2ecf20Sopenharmony_ci	struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
1128c2ecf20Sopenharmony_ci	struct cpufreq_frequency_table *freq_table;
1138c2ecf20Sopenharmony_ci	struct device *dev;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	dev = get_cpu_device(cpu);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	cpuclk = clk_get(dev, "cpu_clk");
1188c2ecf20Sopenharmony_ci	if (IS_ERR(cpuclk)) {
1198c2ecf20Sopenharmony_ci		dev_err(dev, "couldn't get CPU clk\n");
1208c2ecf20Sopenharmony_ci		return PTR_ERR(cpuclk);
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	freq_table = cpuclk->nr_freqs ? cpuclk->freq_table : NULL;
1248c2ecf20Sopenharmony_ci	if (freq_table) {
1258c2ecf20Sopenharmony_ci		policy->freq_table = freq_table;
1268c2ecf20Sopenharmony_ci	} else {
1278c2ecf20Sopenharmony_ci		dev_notice(dev, "no frequency table found, falling back "
1288c2ecf20Sopenharmony_ci			   "to rate rounding.\n");
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		policy->min = policy->cpuinfo.min_freq =
1318c2ecf20Sopenharmony_ci			(clk_round_rate(cpuclk, 1) + 500) / 1000;
1328c2ecf20Sopenharmony_ci		policy->max = policy->cpuinfo.max_freq =
1338c2ecf20Sopenharmony_ci			(clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int sh_cpufreq_cpu_exit(struct cpufreq_policy *policy)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	unsigned int cpu = policy->cpu;
1428c2ecf20Sopenharmony_ci	struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	clk_put(cpuclk);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	return 0;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic void sh_cpufreq_cpu_ready(struct cpufreq_policy *policy)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	struct device *dev = get_cpu_device(policy->cpu);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	dev_info(dev, "CPU Frequencies - Minimum %u.%03u MHz, "
1548c2ecf20Sopenharmony_ci	       "Maximum %u.%03u MHz.\n",
1558c2ecf20Sopenharmony_ci	       policy->min / 1000, policy->min % 1000,
1568c2ecf20Sopenharmony_ci	       policy->max / 1000, policy->max % 1000);
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic struct cpufreq_driver sh_cpufreq_driver = {
1608c2ecf20Sopenharmony_ci	.name		= "sh",
1618c2ecf20Sopenharmony_ci	.flags		= CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,
1628c2ecf20Sopenharmony_ci	.get		= sh_cpufreq_get,
1638c2ecf20Sopenharmony_ci	.target		= sh_cpufreq_target,
1648c2ecf20Sopenharmony_ci	.verify		= sh_cpufreq_verify,
1658c2ecf20Sopenharmony_ci	.init		= sh_cpufreq_cpu_init,
1668c2ecf20Sopenharmony_ci	.exit		= sh_cpufreq_cpu_exit,
1678c2ecf20Sopenharmony_ci	.ready		= sh_cpufreq_cpu_ready,
1688c2ecf20Sopenharmony_ci	.attr		= cpufreq_generic_attr,
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int __init sh_cpufreq_module_init(void)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	pr_notice("SuperH CPU frequency driver.\n");
1748c2ecf20Sopenharmony_ci	return cpufreq_register_driver(&sh_cpufreq_driver);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic void __exit sh_cpufreq_module_exit(void)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	cpufreq_unregister_driver(&sh_cpufreq_driver);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cimodule_init(sh_cpufreq_module_init);
1838c2ecf20Sopenharmony_cimodule_exit(sh_cpufreq_module_exit);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciMODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
1868c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("cpufreq driver for SuperH");
1878c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
188