db8500-cpufreq.c 4.5 KB

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