db8500-cpufreq.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/db8500-prcmu.h>
  16. #include <mach/id.h>
  17. static 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. };
  36. static enum arm_opp idx2opp[] = {
  37. ARM_50_OPP,
  38. ARM_100_OPP,
  39. ARM_MAX_OPP
  40. };
  41. static struct freq_attr *db8500_cpufreq_attr[] = {
  42. &cpufreq_freq_attr_scaling_available_freqs,
  43. NULL,
  44. };
  45. static int db8500_cpufreq_verify_speed(struct cpufreq_policy *policy)
  46. {
  47. return cpufreq_frequency_table_verify(policy, freq_table);
  48. }
  49. static int db8500_cpufreq_target(struct cpufreq_policy *policy,
  50. unsigned int target_freq,
  51. unsigned int relation)
  52. {
  53. struct cpufreq_freqs freqs;
  54. unsigned int idx;
  55. /* scale the target frequency to one of the extremes supported */
  56. if (target_freq < policy->cpuinfo.min_freq)
  57. target_freq = policy->cpuinfo.min_freq;
  58. if (target_freq > policy->cpuinfo.max_freq)
  59. target_freq = policy->cpuinfo.max_freq;
  60. /* Lookup the next frequency */
  61. if (cpufreq_frequency_table_target
  62. (policy, freq_table, target_freq, relation, &idx)) {
  63. return -EINVAL;
  64. }
  65. freqs.old = policy->cur;
  66. freqs.new = freq_table[idx].frequency;
  67. freqs.cpu = policy->cpu;
  68. if (freqs.old == freqs.new)
  69. return 0;
  70. /* pre-change notification */
  71. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  72. /* request the PRCM unit for opp change */
  73. if (prcmu_set_arm_opp(idx2opp[idx])) {
  74. pr_err("db8500-cpufreq: Failed to set OPP level\n");
  75. return -EINVAL;
  76. }
  77. /* post change notification */
  78. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  79. return 0;
  80. }
  81. static unsigned int db8500_cpufreq_getspeed(unsigned int cpu)
  82. {
  83. int i;
  84. /* request the prcm to get the current ARM opp */
  85. for (i = 0; prcmu_get_arm_opp() != idx2opp[i]; i++)
  86. ;
  87. return freq_table[i].frequency;
  88. }
  89. static int __cpuinit db8500_cpufreq_init(struct cpufreq_policy *policy)
  90. {
  91. int res;
  92. int i;
  93. BUILD_BUG_ON(ARRAY_SIZE(idx2opp) + 1 != ARRAY_SIZE(freq_table));
  94. if (cpu_is_u8500v2() && !prcmu_is_u8400()) {
  95. freq_table[0].frequency = 400000;
  96. freq_table[1].frequency = 800000;
  97. if (prcmu_has_arm_maxopp())
  98. freq_table[2].frequency = 1000000;
  99. }
  100. /* get policy fields based on the table */
  101. res = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  102. if (!res)
  103. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  104. else {
  105. pr_err("db8500-cpufreq : Failed to read policy table\n");
  106. return res;
  107. }
  108. policy->min = policy->cpuinfo.min_freq;
  109. policy->max = policy->cpuinfo.max_freq;
  110. policy->cur = db8500_cpufreq_getspeed(policy->cpu);
  111. for (i = 0; freq_table[i].frequency != policy->cur; i++)
  112. ;
  113. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  114. /*
  115. * FIXME : Need to take time measurement across the target()
  116. * function with no/some/all drivers in the notification
  117. * list.
  118. */
  119. policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
  120. /* policy sharing between dual CPUs */
  121. cpumask_copy(policy->cpus, &cpu_present_map);
  122. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  123. return 0;
  124. }
  125. static struct cpufreq_driver db8500_cpufreq_driver = {
  126. .flags = CPUFREQ_STICKY,
  127. .verify = db8500_cpufreq_verify_speed,
  128. .target = db8500_cpufreq_target,
  129. .get = db8500_cpufreq_getspeed,
  130. .init = db8500_cpufreq_init,
  131. .name = "DB8500",
  132. .attr = db8500_cpufreq_attr,
  133. };
  134. static int __init db8500_cpufreq_register(void)
  135. {
  136. if (!cpu_is_u8500v20_or_later())
  137. return -ENODEV;
  138. pr_info("cpufreq for DB8500 started\n");
  139. return cpufreq_register_driver(&db8500_cpufreq_driver);
  140. }
  141. device_initcall(db8500_cpufreq_register);