dpmc.c 3.8 KB

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