dpmc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2008 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later.
  5. */
  6. #include <linux/cdev.h>
  7. #include <linux/device.h>
  8. #include <linux/errno.h>
  9. #include <linux/fs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/types.h>
  14. #include <linux/cpufreq.h>
  15. #include <asm/delay.h>
  16. #include <asm/dpmc.h>
  17. #define DRIVER_NAME "bfin dpmc"
  18. struct bfin_dpmc_platform_data *pdata;
  19. /**
  20. * bfin_set_vlev - Update VLEV field in VR_CTL Reg.
  21. * Avoid BYPASS sequence
  22. */
  23. static void bfin_set_vlev(unsigned int vlev)
  24. {
  25. unsigned pll_lcnt;
  26. pll_lcnt = bfin_read_PLL_LOCKCNT();
  27. bfin_write_PLL_LOCKCNT(1);
  28. bfin_write_VR_CTL((bfin_read_VR_CTL() & ~VLEV) | vlev);
  29. bfin_write_PLL_LOCKCNT(pll_lcnt);
  30. }
  31. /**
  32. * bfin_get_vlev - Get CPU specific VLEV from platform device data
  33. */
  34. static unsigned int bfin_get_vlev(unsigned int freq)
  35. {
  36. int i;
  37. if (!pdata)
  38. goto err_out;
  39. freq >>= 16;
  40. for (i = 0; i < pdata->tabsize; i++)
  41. if (freq <= (pdata->tuple_tab[i] & 0xFFFF))
  42. return pdata->tuple_tab[i] >> 16;
  43. err_out:
  44. printk(KERN_WARNING "DPMC: No suitable CCLK VDDINT voltage pair found\n");
  45. return VLEV_120;
  46. }
  47. #ifdef CONFIG_CPU_FREQ
  48. # ifdef CONFIG_SMP
  49. static void bfin_idle_this_cpu(void *info)
  50. {
  51. unsigned long flags = 0;
  52. unsigned long iwr0, iwr1, iwr2;
  53. unsigned int cpu = smp_processor_id();
  54. local_irq_save_hw(flags);
  55. bfin_iwr_set_sup0(&iwr0, &iwr1, &iwr2);
  56. platform_clear_ipi(cpu, IRQ_SUPPLE_0);
  57. SSYNC();
  58. asm("IDLE;");
  59. bfin_iwr_restore(iwr0, iwr1, iwr2);
  60. local_irq_restore_hw(flags);
  61. }
  62. static void bfin_idle_cpu(void)
  63. {
  64. smp_call_function(bfin_idle_this_cpu, NULL, 0);
  65. }
  66. static void bfin_wakeup_cpu(void)
  67. {
  68. unsigned int cpu;
  69. unsigned int this_cpu = smp_processor_id();
  70. cpumask_t mask;
  71. cpumask_copy(&mask, cpu_online_mask);
  72. cpumask_clear_cpu(this_cpu, &mask);
  73. for_each_cpu(cpu, &mask)
  74. platform_send_ipi_cpu(cpu, IRQ_SUPPLE_0);
  75. }
  76. # else
  77. static void bfin_idle_cpu(void) {}
  78. static void bfin_wakeup_cpu(void) {}
  79. # endif
  80. static int
  81. vreg_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  82. {
  83. struct cpufreq_freqs *freq = data;
  84. if (freq->cpu != CPUFREQ_CPU)
  85. return 0;
  86. if (val == CPUFREQ_PRECHANGE && freq->old < freq->new) {
  87. bfin_idle_cpu();
  88. bfin_set_vlev(bfin_get_vlev(freq->new));
  89. udelay(pdata->vr_settling_time); /* Wait until Volatge settled */
  90. bfin_wakeup_cpu();
  91. } else if (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) {
  92. bfin_idle_cpu();
  93. bfin_set_vlev(bfin_get_vlev(freq->new));
  94. bfin_wakeup_cpu();
  95. }
  96. return 0;
  97. }
  98. static struct notifier_block vreg_cpufreq_notifier_block = {
  99. .notifier_call = vreg_cpufreq_notifier
  100. };
  101. #endif /* CONFIG_CPU_FREQ */
  102. /**
  103. * bfin_dpmc_probe -
  104. *
  105. */
  106. static int __devinit bfin_dpmc_probe(struct platform_device *pdev)
  107. {
  108. if (pdev->dev.platform_data)
  109. pdata = pdev->dev.platform_data;
  110. else
  111. return -EINVAL;
  112. return cpufreq_register_notifier(&vreg_cpufreq_notifier_block,
  113. CPUFREQ_TRANSITION_NOTIFIER);
  114. }
  115. /**
  116. * bfin_dpmc_remove -
  117. */
  118. static int __devexit bfin_dpmc_remove(struct platform_device *pdev)
  119. {
  120. pdata = NULL;
  121. return cpufreq_unregister_notifier(&vreg_cpufreq_notifier_block,
  122. CPUFREQ_TRANSITION_NOTIFIER);
  123. }
  124. struct platform_driver bfin_dpmc_device_driver = {
  125. .probe = bfin_dpmc_probe,
  126. .remove = __devexit_p(bfin_dpmc_remove),
  127. .driver = {
  128. .name = DRIVER_NAME,
  129. }
  130. };
  131. /**
  132. * bfin_dpmc_init - Init driver
  133. */
  134. static int __init bfin_dpmc_init(void)
  135. {
  136. return platform_driver_register(&bfin_dpmc_device_driver);
  137. }
  138. module_init(bfin_dpmc_init);
  139. /**
  140. * bfin_dpmc_exit - break down driver
  141. */
  142. static void __exit bfin_dpmc_exit(void)
  143. {
  144. platform_driver_unregister(&bfin_dpmc_device_driver);
  145. }
  146. module_exit(bfin_dpmc_exit);
  147. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  148. MODULE_DESCRIPTION("cpu power management driver for Blackfin");
  149. MODULE_LICENSE("GPL");