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