db8500-cpufreq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 = 400000,
  25. },
  26. [2] = {
  27. .index = 2,
  28. .frequency = 800000,
  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 i, res;
  98. BUILD_BUG_ON(ARRAY_SIZE(idx2opp) + 1 != ARRAY_SIZE(freq_table));
  99. if (prcmu_has_arm_maxopp())
  100. freq_table[3].frequency = 1000000;
  101. pr_info("db8500-cpufreq : Available frequencies:\n");
  102. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
  103. pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
  104. /* get policy fields based on the table */
  105. res = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  106. if (!res)
  107. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  108. else {
  109. pr_err("db8500-cpufreq : Failed to read policy table\n");
  110. return res;
  111. }
  112. policy->min = policy->cpuinfo.min_freq;
  113. policy->max = policy->cpuinfo.max_freq;
  114. policy->cur = db8500_cpufreq_getspeed(policy->cpu);
  115. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  116. /*
  117. * FIXME : Need to take time measurement across the target()
  118. * function with no/some/all drivers in the notification
  119. * list.
  120. */
  121. policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
  122. /* policy sharing between dual CPUs */
  123. cpumask_copy(policy->cpus, cpu_present_mask);
  124. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  125. return 0;
  126. }
  127. static struct cpufreq_driver db8500_cpufreq_driver = {
  128. .flags = CPUFREQ_STICKY,
  129. .verify = db8500_cpufreq_verify_speed,
  130. .target = db8500_cpufreq_target,
  131. .get = db8500_cpufreq_getspeed,
  132. .init = db8500_cpufreq_init,
  133. .name = "DB8500",
  134. .attr = db8500_cpufreq_attr,
  135. };
  136. static int __init db8500_cpufreq_register(void)
  137. {
  138. if (!cpu_is_u8500v20_or_later())
  139. return -ENODEV;
  140. pr_info("cpufreq for DB8500 started\n");
  141. return cpufreq_register_driver(&db8500_cpufreq_driver);
  142. }
  143. device_initcall(db8500_cpufreq_register);