Path: blob/master/arch/avr32/mach-at32ap/cpufreq.c
10817 views
/*1* Copyright (C) 2004-2007 Atmel Corporation2*3* Based on MIPS implementation arch/mips/kernel/time.c4* Copyright 2001 MontaVista Software Inc.5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License version 2 as8* published by the Free Software Foundation.9*/1011/*#define DEBUG*/1213#include <linux/kernel.h>14#include <linux/types.h>15#include <linux/init.h>16#include <linux/cpufreq.h>17#include <linux/io.h>18#include <linux/clk.h>19#include <linux/err.h>20#include <asm/system.h>2122static struct clk *cpuclk;2324static int at32_verify_speed(struct cpufreq_policy *policy)25{26if (policy->cpu != 0)27return -EINVAL;2829cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,30policy->cpuinfo.max_freq);31return 0;32}3334static unsigned int at32_get_speed(unsigned int cpu)35{36/* No SMP support */37if (cpu)38return 0;39return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);40}4142static unsigned int ref_freq;43static unsigned long loops_per_jiffy_ref;4445static int at32_set_target(struct cpufreq_policy *policy,46unsigned int target_freq,47unsigned int relation)48{49struct cpufreq_freqs freqs;50long freq;5152/* Convert target_freq from kHz to Hz */53freq = clk_round_rate(cpuclk, target_freq * 1000);5455/* Check if policy->min <= new_freq <= policy->max */56if(freq < (policy->min * 1000) || freq > (policy->max * 1000))57return -EINVAL;5859pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);6061freqs.old = at32_get_speed(0);62freqs.new = (freq + 500) / 1000;63freqs.cpu = 0;64freqs.flags = 0;6566if (!ref_freq) {67ref_freq = freqs.old;68loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;69}7071cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);72if (freqs.old < freqs.new)73boot_cpu_data.loops_per_jiffy = cpufreq_scale(74loops_per_jiffy_ref, ref_freq, freqs.new);75clk_set_rate(cpuclk, freq);76if (freqs.new < freqs.old)77boot_cpu_data.loops_per_jiffy = cpufreq_scale(78loops_per_jiffy_ref, ref_freq, freqs.new);79cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);8081pr_debug("cpufreq: set frequency %lu Hz\n", freq);8283return 0;84}8586static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)87{88if (policy->cpu != 0)89return -EINVAL;9091cpuclk = clk_get(NULL, "cpu");92if (IS_ERR(cpuclk)) {93pr_debug("cpufreq: could not get CPU clk\n");94return PTR_ERR(cpuclk);95}9697policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;98policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;99policy->cpuinfo.transition_latency = 0;100policy->cur = at32_get_speed(0);101policy->min = policy->cpuinfo.min_freq;102policy->max = policy->cpuinfo.max_freq;103104printk("cpufreq: AT32AP CPU frequency driver\n");105106return 0;107}108109static struct cpufreq_driver at32_driver = {110.name = "at32ap",111.owner = THIS_MODULE,112.init = at32_cpufreq_driver_init,113.verify = at32_verify_speed,114.target = at32_set_target,115.get = at32_get_speed,116.flags = CPUFREQ_STICKY,117};118119static int __init at32_cpufreq_init(void)120{121return cpufreq_register_driver(&at32_driver);122}123late_initcall(at32_cpufreq_init);124125126