Path: blob/master/drivers/cpufreq/db8500-cpufreq.c
15111 views
/*1* Copyright (C) STMicroelectronics 20092* Copyright (C) ST-Ericsson SA 20103*4* License Terms: GNU General Public License v25* Author: Sundar Iyer <[email protected]>6* Author: Martin Persson <[email protected]>7* Author: Jonas Aaberg <[email protected]>8*9*/10#include <linux/kernel.h>11#include <linux/cpufreq.h>12#include <linux/delay.h>13#include <linux/slab.h>14#include <linux/mfd/db8500-prcmu.h>15#include <mach/id.h>1617static struct cpufreq_frequency_table freq_table[] = {18[0] = {19.index = 0,20.frequency = 300000,21},22[1] = {23.index = 1,24.frequency = 600000,25},26[2] = {27/* Used for MAX_OPP, if available */28.index = 2,29.frequency = CPUFREQ_TABLE_END,30},31[3] = {32.index = 3,33.frequency = CPUFREQ_TABLE_END,34},35};3637static enum arm_opp idx2opp[] = {38ARM_50_OPP,39ARM_100_OPP,40ARM_MAX_OPP41};4243static struct freq_attr *db8500_cpufreq_attr[] = {44&cpufreq_freq_attr_scaling_available_freqs,45NULL,46};4748static int db8500_cpufreq_verify_speed(struct cpufreq_policy *policy)49{50return cpufreq_frequency_table_verify(policy, freq_table);51}5253static int db8500_cpufreq_target(struct cpufreq_policy *policy,54unsigned int target_freq,55unsigned int relation)56{57struct cpufreq_freqs freqs;58unsigned int idx;5960/* scale the target frequency to one of the extremes supported */61if (target_freq < policy->cpuinfo.min_freq)62target_freq = policy->cpuinfo.min_freq;63if (target_freq > policy->cpuinfo.max_freq)64target_freq = policy->cpuinfo.max_freq;6566/* Lookup the next frequency */67if (cpufreq_frequency_table_target68(policy, freq_table, target_freq, relation, &idx)) {69return -EINVAL;70}7172freqs.old = policy->cur;73freqs.new = freq_table[idx].frequency;74freqs.cpu = policy->cpu;7576if (freqs.old == freqs.new)77return 0;7879/* pre-change notification */80cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);8182/* request the PRCM unit for opp change */83if (prcmu_set_arm_opp(idx2opp[idx])) {84pr_err("db8500-cpufreq: Failed to set OPP level\n");85return -EINVAL;86}8788/* post change notification */89cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);9091return 0;92}9394static unsigned int db8500_cpufreq_getspeed(unsigned int cpu)95{96int i;97/* request the prcm to get the current ARM opp */98for (i = 0; prcmu_get_arm_opp() != idx2opp[i]; i++)99;100return freq_table[i].frequency;101}102103static int __cpuinit db8500_cpufreq_init(struct cpufreq_policy *policy)104{105int res;106int i;107108BUILD_BUG_ON(ARRAY_SIZE(idx2opp) + 1 != ARRAY_SIZE(freq_table));109110if (cpu_is_u8500v2() && !prcmu_is_u8400()) {111freq_table[0].frequency = 400000;112freq_table[1].frequency = 800000;113if (prcmu_has_arm_maxopp())114freq_table[2].frequency = 1000000;115}116117/* get policy fields based on the table */118res = cpufreq_frequency_table_cpuinfo(policy, freq_table);119if (!res)120cpufreq_frequency_table_get_attr(freq_table, policy->cpu);121else {122pr_err("db8500-cpufreq : Failed to read policy table\n");123return res;124}125126policy->min = policy->cpuinfo.min_freq;127policy->max = policy->cpuinfo.max_freq;128policy->cur = db8500_cpufreq_getspeed(policy->cpu);129130for (i = 0; freq_table[i].frequency != policy->cur; i++)131;132133policy->governor = CPUFREQ_DEFAULT_GOVERNOR;134135/*136* FIXME : Need to take time measurement across the target()137* function with no/some/all drivers in the notification138* list.139*/140policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */141142/* policy sharing between dual CPUs */143cpumask_copy(policy->cpus, &cpu_present_map);144145policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;146147return 0;148}149150static struct cpufreq_driver db8500_cpufreq_driver = {151.flags = CPUFREQ_STICKY,152.verify = db8500_cpufreq_verify_speed,153.target = db8500_cpufreq_target,154.get = db8500_cpufreq_getspeed,155.init = db8500_cpufreq_init,156.name = "DB8500",157.attr = db8500_cpufreq_attr,158};159160static int __init db8500_cpufreq_register(void)161{162if (!cpu_is_u8500v20_or_later())163return -ENODEV;164165pr_info("cpufreq for DB8500 started\n");166return cpufreq_register_driver(&db8500_cpufreq_driver);167}168device_initcall(db8500_cpufreq_register);169170171