blob: 7b612c8bb09ea917f39083bf13e98c2caa775f0e [file] [log] [blame]
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +02001/*
2 * Copyright (C) 2004-2007 Atmel Corporation
3 *
4 * Based on MIPS implementation arch/mips/kernel/time.c
5 * Copyright 2001 MontaVista Software Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12/*#define DEBUG*/
13
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/init.h>
17#include <linux/cpufreq.h>
18#include <linux/io.h>
19#include <linux/clk.h>
20#include <linux/err.h>
Paul Gortmaker09cf6a22011-08-01 12:55:26 -040021#include <linux/export.h>
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053022#include <linux/slab.h>
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020023
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053024static struct cpufreq_frequency_table *freq_table;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020025
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020026static unsigned int ref_freq;
27static unsigned long loops_per_jiffy_ref;
28
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053029static int at32_set_target(struct cpufreq_policy *policy, unsigned int index)
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020030{
Viresh Kumard4019f02013-08-14 19:38:24 +053031 unsigned int old_freq, new_freq;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020032
Viresh Kumar652ed952014-01-09 20:38:43 +053033 old_freq = policy->cur;
Viresh Kumard4019f02013-08-14 19:38:24 +053034 new_freq = freq_table[index].frequency;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020035
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020036 if (!ref_freq) {
Viresh Kumard4019f02013-08-14 19:38:24 +053037 ref_freq = old_freq;
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020038 loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
39 }
40
Viresh Kumard4019f02013-08-14 19:38:24 +053041 if (old_freq < new_freq)
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020042 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
Viresh Kumard4019f02013-08-14 19:38:24 +053043 loops_per_jiffy_ref, ref_freq, new_freq);
Viresh Kumar652ed952014-01-09 20:38:43 +053044 clk_set_rate(policy->clk, new_freq * 1000);
Viresh Kumard4019f02013-08-14 19:38:24 +053045 if (new_freq < old_freq)
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020046 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
Viresh Kumard4019f02013-08-14 19:38:24 +053047 loops_per_jiffy_ref, ref_freq, new_freq);
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020048
49 return 0;
50}
51
Matthias Bruggerfa1513f2013-12-05 10:59:57 +010052static int at32_cpufreq_driver_init(struct cpufreq_policy *policy)
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020053{
Viresh Kumar017189b2013-10-03 20:28:33 +053054 unsigned int frequency, rate, min_freq;
Viresh Kumar0ca97882014-04-03 20:20:36 +053055 struct clk *cpuclk;
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053056 int retval, steps, i;
57
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020058 if (policy->cpu != 0)
59 return -EINVAL;
60
61 cpuclk = clk_get(NULL, "cpu");
62 if (IS_ERR(cpuclk)) {
63 pr_debug("cpufreq: could not get CPU clk\n");
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053064 retval = PTR_ERR(cpuclk);
65 goto out_err;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020066 }
67
Viresh Kumar017189b2013-10-03 20:28:33 +053068 min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
69 frequency = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020070 policy->cpuinfo.transition_latency = 0;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020071
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053072 /*
73 * AVR32 CPU frequency rate scales in power of two between maximum and
74 * minimum, also add space for the table end marker.
75 *
76 * Further validate that the frequency is usable, and append it to the
77 * frequency table.
78 */
Viresh Kumar017189b2013-10-03 20:28:33 +053079 steps = fls(frequency / min_freq) + 1;
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053080 freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table),
81 GFP_KERNEL);
82 if (!freq_table) {
83 retval = -ENOMEM;
84 goto out_err_put_clk;
85 }
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020086
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053087 for (i = 0; i < (steps - 1); i++) {
88 rate = clk_round_rate(cpuclk, frequency * 1000) / 1000;
89
90 if (rate != frequency)
91 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
92 else
93 freq_table[i].frequency = frequency;
94
95 frequency /= 2;
96 }
97
Viresh Kumar652ed952014-01-09 20:38:43 +053098 policy->clk = cpuclk;
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053099 freq_table[steps - 1].frequency = CPUFREQ_TABLE_END;
100
101 retval = cpufreq_table_validate_and_show(policy, freq_table);
102 if (!retval) {
103 printk("cpufreq: AT32AP CPU frequency driver\n");
104 return 0;
105 }
106
107 kfree(freq_table);
108out_err_put_clk:
109 clk_put(cpuclk);
110out_err:
111 return retval;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200112}
113
114static struct cpufreq_driver at32_driver = {
115 .name = "at32ap",
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200116 .init = at32_cpufreq_driver_init,
Viresh Kumar5ae68f42013-10-03 20:27:58 +0530117 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530118 .target_index = at32_set_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530119 .get = cpufreq_generic_get,
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200120 .flags = CPUFREQ_STICKY,
121};
122
123static int __init at32_cpufreq_init(void)
124{
125 return cpufreq_register_driver(&at32_driver);
126}
Haavard Skinnemoenf04d2642008-05-27 09:37:42 +0200127late_initcall(at32_cpufreq_init);