dbx500-cpufreq.c 4.5 KB

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