at32ap-cpufreq.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2004-2007 Atmel Corporation
 *
 * Based on MIPS implementation arch/mips/kernel/time.c
 *   Copyright 2001 MontaVista Software Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*#define DEBUG*/

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/err.h>
21
#include <linux/export.h>
22
#include <linux/slab.h>
23 24

static struct clk *cpuclk;
25
static struct cpufreq_frequency_table *freq_table;
26 27 28 29 30 31 32 33 34

static unsigned int at32_get_speed(unsigned int cpu)
{
	/* No SMP support */
	if (cpu)
		return 0;
	return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);
}

35 36 37
static unsigned int	ref_freq;
static unsigned long	loops_per_jiffy_ref;

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static int at32_set_target(struct cpufreq_policy *policy,
			  unsigned int target_freq,
			  unsigned int relation)
{
	struct cpufreq_freqs freqs;
	long freq;

	/* Convert target_freq from kHz to Hz */
	freq = clk_round_rate(cpuclk, target_freq * 1000);

	/* Check if policy->min <= new_freq <= policy->max */
	if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
		return -EINVAL;

	pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);

	freqs.old = at32_get_speed(0);
	freqs.new = (freq + 500) / 1000;
	freqs.flags = 0;

58 59 60 61 62
	if (!ref_freq) {
		ref_freq = freqs.old;
		loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
	}

63
	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
64 65 66
	if (freqs.old < freqs.new)
		boot_cpu_data.loops_per_jiffy = cpufreq_scale(
				loops_per_jiffy_ref, ref_freq, freqs.new);
67
	clk_set_rate(cpuclk, freq);
68 69 70
	if (freqs.new < freqs.old)
		boot_cpu_data.loops_per_jiffy = cpufreq_scale(
				loops_per_jiffy_ref, ref_freq, freqs.new);
71
	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
72 73 74 75 76 77 78 79

	pr_debug("cpufreq: set frequency %lu Hz\n", freq);

	return 0;
}

static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
{
80
	unsigned int frequency, rate, min_freq;
81 82
	int retval, steps, i;

83 84 85 86 87 88
	if (policy->cpu != 0)
		return -EINVAL;

	cpuclk = clk_get(NULL, "cpu");
	if (IS_ERR(cpuclk)) {
		pr_debug("cpufreq: could not get CPU clk\n");
89 90
		retval = PTR_ERR(cpuclk);
		goto out_err;
91 92
	}

93 94
	min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
	frequency = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
95 96
	policy->cpuinfo.transition_latency = 0;

97 98 99 100 101 102 103
	/*
	 * AVR32 CPU frequency rate scales in power of two between maximum and
	 * minimum, also add space for the table end marker.
	 *
	 * Further validate that the frequency is usable, and append it to the
	 * frequency table.
	 */
104
	steps = fls(frequency / min_freq) + 1;
105 106 107 108 109 110
	freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table),
			GFP_KERNEL);
	if (!freq_table) {
		retval = -ENOMEM;
		goto out_err_put_clk;
	}
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	for (i = 0; i < (steps - 1); i++) {
		rate = clk_round_rate(cpuclk, frequency * 1000) / 1000;

		if (rate != frequency)
			freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
		else
			freq_table[i].frequency = frequency;

		frequency /= 2;
	}

	freq_table[steps - 1].frequency = CPUFREQ_TABLE_END;

	retval = cpufreq_table_validate_and_show(policy, freq_table);
	if (!retval) {
		printk("cpufreq: AT32AP CPU frequency driver\n");
		return 0;
	}

	kfree(freq_table);
out_err_put_clk:
	clk_put(cpuclk);
out_err:
	return retval;
136 137 138 139 140
}

static struct cpufreq_driver at32_driver = {
	.name		= "at32ap",
	.init		= at32_cpufreq_driver_init,
141
	.verify		= cpufreq_generic_frequency_table_verify,
142 143 144 145 146 147 148 149 150
	.target		= at32_set_target,
	.get		= at32_get_speed,
	.flags		= CPUFREQ_STICKY,
};

static int __init at32_cpufreq_init(void)
{
	return cpufreq_register_driver(&at32_driver);
}
151
late_initcall(at32_cpufreq_init);