db8500-cpufreq.c 4.3 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/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/dbx500-prcmu.h>
  18. #include <mach/id.h>
  19. static struct cpufreq_frequency_table *freq_table;
  20. static struct freq_attr *db8500_cpufreq_attr[] = {
  21. &cpufreq_freq_attr_scaling_available_freqs,
  22. NULL,
  23. };
  24. static int db8500_cpufreq_verify_speed(struct cpufreq_policy *policy)
  25. {
  26. return cpufreq_frequency_table_verify(policy, freq_table);
  27. }
  28. static int db8500_cpufreq_target(struct cpufreq_policy *policy,
  29. unsigned int target_freq,
  30. unsigned int relation)
  31. {
  32. struct cpufreq_freqs freqs;
  33. unsigned int idx;
  34. /* scale the target frequency to one of the extremes supported */
  35. if (target_freq < policy->cpuinfo.min_freq)
  36. target_freq = policy->cpuinfo.min_freq;
  37. if (target_freq > policy->cpuinfo.max_freq)
  38. target_freq = policy->cpuinfo.max_freq;
  39. /* Lookup the next frequency */
  40. if (cpufreq_frequency_table_target
  41. (policy, freq_table, target_freq, relation, &idx)) {
  42. return -EINVAL;
  43. }
  44. freqs.old = policy->cur;
  45. freqs.new = freq_table[idx].frequency;
  46. if (freqs.old == freqs.new)
  47. return 0;
  48. /* pre-change notification */
  49. for_each_cpu(freqs.cpu, policy->cpus)
  50. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  51. /* request the PRCM unit for opp change */
  52. if (prcmu_set_arm_opp(freq_table[idx].index)) {
  53. pr_err("db8500-cpufreq: Failed to set OPP level\n");
  54. return -EINVAL;
  55. }
  56. /* post change notification */
  57. for_each_cpu(freqs.cpu, policy->cpus)
  58. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  59. return 0;
  60. }
  61. static unsigned int db8500_cpufreq_getspeed(unsigned int cpu)
  62. {
  63. int i = 0;
  64. /* request the prcm to get the current ARM opp */
  65. int opp = prcmu_get_arm_opp();
  66. while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
  67. if (opp == freq_table[i].index)
  68. return freq_table[i].frequency;
  69. i++;
  70. }
  71. /* We could not find a corresponding opp frequency. */
  72. return 0;
  73. }
  74. static int __cpuinit db8500_cpufreq_init(struct cpufreq_policy *policy)
  75. {
  76. int i = 0;
  77. int res;
  78. pr_info("db8500-cpufreq : Available frequencies:\n");
  79. while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
  80. pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
  81. i++;
  82. }
  83. /* get policy fields based on the table */
  84. res = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  85. if (!res)
  86. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  87. else {
  88. pr_err("db8500-cpufreq : Failed to read policy table\n");
  89. return res;
  90. }
  91. policy->min = policy->cpuinfo.min_freq;
  92. policy->max = policy->cpuinfo.max_freq;
  93. policy->cur = db8500_cpufreq_getspeed(policy->cpu);
  94. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  95. /*
  96. * FIXME : Need to take time measurement across the target()
  97. * function with no/some/all drivers in the notification
  98. * list.
  99. */
  100. policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
  101. /* policy sharing between dual CPUs */
  102. cpumask_copy(policy->cpus, cpu_present_mask);
  103. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  104. return 0;
  105. }
  106. static struct cpufreq_driver db8500_cpufreq_driver = {
  107. .flags = CPUFREQ_STICKY,
  108. .verify = db8500_cpufreq_verify_speed,
  109. .target = db8500_cpufreq_target,
  110. .get = db8500_cpufreq_getspeed,
  111. .init = db8500_cpufreq_init,
  112. .name = "DB8500",
  113. .attr = db8500_cpufreq_attr,
  114. };
  115. static int db8500_cpufreq_probe(struct platform_device *pdev)
  116. {
  117. freq_table = dev_get_platdata(&pdev->dev);
  118. if (!freq_table) {
  119. pr_err("db8500-cpufreq: Failed to fetch cpufreq table\n");
  120. return -ENODEV;
  121. }
  122. return cpufreq_register_driver(&db8500_cpufreq_driver);
  123. }
  124. static struct platform_driver db8500_cpufreq_plat_driver = {
  125. .driver = {
  126. .name = "cpufreq-u8500",
  127. .owner = THIS_MODULE,
  128. },
  129. .probe = db8500_cpufreq_probe,
  130. };
  131. static int __init db8500_cpufreq_register(void)
  132. {
  133. if (!cpu_is_u8500_family())
  134. return -ENODEV;
  135. pr_info("cpufreq for DB8500 started\n");
  136. return platform_driver_register(&db8500_cpufreq_plat_driver);
  137. }
  138. device_initcall(db8500_cpufreq_register);
  139. MODULE_LICENSE("GPL v2");
  140. MODULE_DESCRIPTION("cpufreq driver for DB8500");