dbx500-cpufreq.c 4.5 KB

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