Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 1 | /* |
| 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 Gortmaker | 09cf6a2 | 2011-08-01 12:55:26 -0400 | [diff] [blame] | 21 | #include <linux/export.h> |
Hans-Christian Egtvedt | 848cb94 | 2013-09-16 18:56:41 +0530 | [diff] [blame^] | 22 | #include <linux/slab.h> |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 23 | |
| 24 | static struct clk *cpuclk; |
Hans-Christian Egtvedt | 848cb94 | 2013-09-16 18:56:41 +0530 | [diff] [blame^] | 25 | static struct cpufreq_frequency_table *freq_table; |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 26 | |
| 27 | static int at32_verify_speed(struct cpufreq_policy *policy) |
| 28 | { |
| 29 | if (policy->cpu != 0) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 33 | policy->cpuinfo.max_freq); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static unsigned int at32_get_speed(unsigned int cpu) |
| 38 | { |
| 39 | /* No SMP support */ |
| 40 | if (cpu) |
| 41 | return 0; |
| 42 | return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000); |
| 43 | } |
| 44 | |
Haavard Skinnemoen | e3f91ca | 2008-10-23 11:23:08 +0200 | [diff] [blame] | 45 | static unsigned int ref_freq; |
| 46 | static unsigned long loops_per_jiffy_ref; |
| 47 | |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 48 | static int at32_set_target(struct cpufreq_policy *policy, |
| 49 | unsigned int target_freq, |
| 50 | unsigned int relation) |
| 51 | { |
| 52 | struct cpufreq_freqs freqs; |
| 53 | long freq; |
| 54 | |
| 55 | /* Convert target_freq from kHz to Hz */ |
| 56 | freq = clk_round_rate(cpuclk, target_freq * 1000); |
| 57 | |
| 58 | /* Check if policy->min <= new_freq <= policy->max */ |
| 59 | if(freq < (policy->min * 1000) || freq > (policy->max * 1000)) |
| 60 | return -EINVAL; |
| 61 | |
| 62 | pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000); |
| 63 | |
| 64 | freqs.old = at32_get_speed(0); |
| 65 | freqs.new = (freq + 500) / 1000; |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 66 | freqs.flags = 0; |
| 67 | |
Haavard Skinnemoen | e3f91ca | 2008-10-23 11:23:08 +0200 | [diff] [blame] | 68 | if (!ref_freq) { |
| 69 | ref_freq = freqs.old; |
| 70 | loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy; |
| 71 | } |
| 72 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 73 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE); |
Haavard Skinnemoen | e3f91ca | 2008-10-23 11:23:08 +0200 | [diff] [blame] | 74 | if (freqs.old < freqs.new) |
| 75 | boot_cpu_data.loops_per_jiffy = cpufreq_scale( |
| 76 | loops_per_jiffy_ref, ref_freq, freqs.new); |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 77 | clk_set_rate(cpuclk, freq); |
Haavard Skinnemoen | e3f91ca | 2008-10-23 11:23:08 +0200 | [diff] [blame] | 78 | if (freqs.new < freqs.old) |
| 79 | boot_cpu_data.loops_per_jiffy = cpufreq_scale( |
| 80 | loops_per_jiffy_ref, ref_freq, freqs.new); |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 81 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE); |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 82 | |
| 83 | pr_debug("cpufreq: set frequency %lu Hz\n", freq); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy) |
| 89 | { |
Hans-Christian Egtvedt | 848cb94 | 2013-09-16 18:56:41 +0530 | [diff] [blame^] | 90 | unsigned int frequency, rate; |
| 91 | int retval, steps, i; |
| 92 | |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 93 | if (policy->cpu != 0) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | cpuclk = clk_get(NULL, "cpu"); |
| 97 | if (IS_ERR(cpuclk)) { |
| 98 | pr_debug("cpufreq: could not get CPU clk\n"); |
Hans-Christian Egtvedt | 848cb94 | 2013-09-16 18:56:41 +0530 | [diff] [blame^] | 99 | retval = PTR_ERR(cpuclk); |
| 100 | goto out_err; |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000; |
| 104 | policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000; |
| 105 | policy->cpuinfo.transition_latency = 0; |
| 106 | policy->cur = at32_get_speed(0); |
| 107 | policy->min = policy->cpuinfo.min_freq; |
| 108 | policy->max = policy->cpuinfo.max_freq; |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 109 | |
Hans-Christian Egtvedt | 848cb94 | 2013-09-16 18:56:41 +0530 | [diff] [blame^] | 110 | /* |
| 111 | * AVR32 CPU frequency rate scales in power of two between maximum and |
| 112 | * minimum, also add space for the table end marker. |
| 113 | * |
| 114 | * Further validate that the frequency is usable, and append it to the |
| 115 | * frequency table. |
| 116 | */ |
| 117 | steps = fls(policy->cpuinfo.max_freq / policy->cpuinfo.min_freq) + 1; |
| 118 | freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table), |
| 119 | GFP_KERNEL); |
| 120 | if (!freq_table) { |
| 121 | retval = -ENOMEM; |
| 122 | goto out_err_put_clk; |
| 123 | } |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 124 | |
Hans-Christian Egtvedt | 848cb94 | 2013-09-16 18:56:41 +0530 | [diff] [blame^] | 125 | frequency = policy->cpuinfo.max_freq; |
| 126 | for (i = 0; i < (steps - 1); i++) { |
| 127 | rate = clk_round_rate(cpuclk, frequency * 1000) / 1000; |
| 128 | |
| 129 | if (rate != frequency) |
| 130 | freq_table[i].frequency = CPUFREQ_ENTRY_INVALID; |
| 131 | else |
| 132 | freq_table[i].frequency = frequency; |
| 133 | |
| 134 | frequency /= 2; |
| 135 | } |
| 136 | |
| 137 | freq_table[steps - 1].frequency = CPUFREQ_TABLE_END; |
| 138 | |
| 139 | retval = cpufreq_table_validate_and_show(policy, freq_table); |
| 140 | if (!retval) { |
| 141 | printk("cpufreq: AT32AP CPU frequency driver\n"); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | kfree(freq_table); |
| 146 | out_err_put_clk: |
| 147 | clk_put(cpuclk); |
| 148 | out_err: |
| 149 | return retval; |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static struct cpufreq_driver at32_driver = { |
| 153 | .name = "at32ap", |
Hans-Christian Egtvedt | 9e58e18 | 2007-06-04 16:10:57 +0200 | [diff] [blame] | 154 | .init = at32_cpufreq_driver_init, |
| 155 | .verify = at32_verify_speed, |
| 156 | .target = at32_set_target, |
| 157 | .get = at32_get_speed, |
| 158 | .flags = CPUFREQ_STICKY, |
| 159 | }; |
| 160 | |
| 161 | static int __init at32_cpufreq_init(void) |
| 162 | { |
| 163 | return cpufreq_register_driver(&at32_driver); |
| 164 | } |
Haavard Skinnemoen | f04d264 | 2008-05-27 09:37:42 +0200 | [diff] [blame] | 165 | late_initcall(at32_cpufreq_init); |