db8500-cpufreq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) STMicroelectronics 2009
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * License Terms: GNU General Public License v2
  6. * Author: Sundar Iyer <sundar.iyer@stericsson.com>
  7. * Author: Martin Persson <martin.persson@stericsson.com>
  8. * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/mfd/dbx500-prcmu.h>
  16. #include <mach/id.h>
  17. static struct cpufreq_frequency_table freq_table[] = {
  18. [0] = {
  19. .index = 0,
  20. .frequency = 200000,
  21. },
  22. [1] = {
  23. .index = 1,
  24. .frequency = 300000,
  25. },
  26. [2] = {
  27. .index = 2,
  28. .frequency = 600000,
  29. },
  30. [3] = {
  31. /* Used for MAX_OPP, if available */
  32. .index = 3,
  33. .frequency = CPUFREQ_TABLE_END,
  34. },
  35. [4] = {
  36. .index = 4,
  37. .frequency = CPUFREQ_TABLE_END,
  38. },
  39. };
  40. static enum arm_opp idx2opp[] = {
  41. ARM_EXTCLK,
  42. ARM_50_OPP,
  43. ARM_100_OPP,
  44. ARM_MAX_OPP
  45. };
  46. static struct freq_attr *db8500_cpufreq_attr[] = {
  47. &cpufreq_freq_attr_scaling_available_freqs,
  48. NULL,
  49. };
  50. static int db8500_cpufreq_verify_speed(struct cpufreq_policy *policy)
  51. {
  52. return cpufreq_frequency_table_verify(policy, freq_table);
  53. }
  54. static int db8500_cpufreq_target(struct cpufreq_policy *policy,
  55. unsigned int target_freq,
  56. unsigned int relation)
  57. {
  58. struct cpufreq_freqs freqs;
  59. unsigned int idx;
  60. /* scale the target frequency to one of the extremes supported */
  61. if (target_freq < policy->cpuinfo.min_freq)
  62. target_freq = policy->cpuinfo.min_freq;
  63. if (target_freq > policy->cpuinfo.max_freq)
  64. target_freq = policy->cpuinfo.max_freq;
  65. /* Lookup the next frequency */
  66. if (cpufreq_frequency_table_target
  67. (policy, freq_table, target_freq, relation, &idx)) {
  68. return -EINVAL;
  69. }
  70. freqs.old = policy->cur;
  71. freqs.new = freq_table[idx].frequency;
  72. if (freqs.old == freqs.new)
  73. return 0;
  74. /* pre-change notification */
  75. for_each_cpu(freqs.cpu, policy->cpus)
  76. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  77. /* request the PRCM unit for opp change */
  78. if (prcmu_set_arm_opp(idx2opp[idx])) {
  79. pr_err("db8500-cpufreq: Failed to set OPP level\n");
  80. return -EINVAL;
  81. }
  82. /* post change notification */
  83. for_each_cpu(freqs.cpu, policy->cpus)
  84. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  85. return 0;
  86. }
  87. static unsigned int db8500_cpufreq_getspeed(unsigned int cpu)
  88. {
  89. int i;
  90. /* request the prcm to get the current ARM opp */
  91. for (i = 0; prcmu_get_arm_opp() != idx2opp[i]; i++)
  92. ;
  93. return freq_table[i].frequency;
  94. }
  95. static int __cpuinit db8500_cpufreq_init(struct cpufreq_policy *policy)
  96. {
  97. int res;
  98. BUILD_BUG_ON(ARRAY_SIZE(idx2opp) + 1 != ARRAY_SIZE(freq_table));
  99. if (!prcmu_is_u8400()) {
  100. freq_table[1].frequency = 400000;
  101. freq_table[2].frequency = 800000;
  102. if (prcmu_has_arm_maxopp())
  103. freq_table[3].frequency = 1000000;
  104. }
  105. pr_info("db8500-cpufreq : Available frequencies:\n");
  106. while (freq_table[i].frequency != CPUFREQ_TABLE_END)
  107. pr_info(" %d Mhz\n", freq_table[i++].frequency/1000);
  108. /* get policy fields based on the table */
  109. res = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  110. if (!res)
  111. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  112. else {
  113. pr_err("db8500-cpufreq : Failed to read policy table\n");
  114. return res;
  115. }
  116. policy->min = policy->cpuinfo.min_freq;
  117. policy->max = policy->cpuinfo.max_freq;
  118. policy->cur = db8500_cpufreq_getspeed(policy->cpu);
  119. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  120. /*
  121. * FIXME : Need to take time measurement across the target()
  122. * function with no/some/all drivers in the notification
  123. * list.
  124. */
  125. policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
  126. /* policy sharing between dual CPUs */
  127. cpumask_copy(policy->cpus, &cpu_present_map);
  128. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  129. return 0;
  130. }
  131. static struct cpufreq_driver db8500_cpufreq_driver = {
  132. .flags = CPUFREQ_STICKY,
  133. .verify = db8500_cpufreq_verify_speed,
  134. .target = db8500_cpufreq_target,
  135. .get = db8500_cpufreq_getspeed,
  136. .init = db8500_cpufreq_init,
  137. .name = "DB8500",
  138. .attr = db8500_cpufreq_attr,
  139. };
  140. static int __init db8500_cpufreq_register(void)
  141. {
  142. if (!cpu_is_u8500v20_or_later())
  143. return -ENODEV;
  144. pr_info("cpufreq for DB8500 started\n");
  145. return cpufreq_register_driver(&db8500_cpufreq_driver);
  146. }
  147. device_initcall(db8500_cpufreq_register);