calculationOfAverageCurrent.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* linux/arch/arm/plat-s3c24xx/pwm-clock.c
  2. *
  3. * Copyright (c) 2007 Simtec Electronics
  4. * Copyright (c) 2007, 2008 Ben Dooks
  5. * Ben Dooks <ben-linux@fluff.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/errno.h>
  16. #include <linux/log2.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <mach/hardware.h>
  21. #include <mach/map.h>
  22. #include <asm/irq.h>
  23. #include <plat/clock.h>
  24. #include <plat/cpu.h>
  25. #include <plat/regs-timer.h>
  26. #include <plat/pwm-clock.h>
  27. /* Each of the timers 0 through 5 go through the following
  28. * clock tree, with the inputs depending on the timers.
  29. *
  30. * pclk ---- [ prescaler 0 ] -+---> timer 0
  31. * +---> timer 1
  32. *
  33. * pclk ---- [ prescaler 1 ] -+---> timer 2
  34. * +---> timer 3
  35. * \---> timer 4
  36. *
  37. * Which are fed into the timers as so:
  38. *
  39. * prescaled 0 ---- [ div 2,4,8,16 ] ---\
  40. * [mux] -> timer 0
  41. * tclk 0 ------------------------------/
  42. *
  43. * prescaled 0 ---- [ div 2,4,8,16 ] ---\
  44. * [mux] -> timer 1
  45. * tclk 0 ------------------------------/
  46. *
  47. *
  48. * prescaled 1 ---- [ div 2,4,8,16 ] ---\
  49. * [mux] -> timer 2
  50. * tclk 1 ------------------------------/
  51. *
  52. * prescaled 1 ---- [ div 2,4,8,16 ] ---\
  53. * [mux] -> timer 3
  54. * tclk 1 ------------------------------/
  55. *
  56. * prescaled 1 ---- [ div 2,4,8, 16 ] --\
  57. * [mux] -> timer 4
  58. * tclk 1 ------------------------------/
  59. *
  60. * Since the mux and the divider are tied together in the
  61. * same register space, it is impossible to set the parent
  62. * and the rate at the same time. To avoid this, we add an
  63. * intermediate 'prescaled-and-divided' clock to select
  64. * as the parent for the timer input clock called tdiv.
  65. *
  66. * prescaled clk --> pwm-tdiv ---\
  67. * [ mux ] --> timer X
  68. * tclk -------------------------/
  69. */
  70. static struct clk clk_timer_scaler[];
  71. static unsigned long clk_pwm_scaler_get_rate(struct clk *clk)
  72. {
  73. unsigned long tcfg0 = __raw_readl(S3C2410_TCFG0);
  74. if (clk == &clk_timer_scaler[1]) {
  75. tcfg0 &= S3C2410_TCFG_PRESCALER1_MASK;
  76. tcfg0 >>= S3C2410_TCFG_PRESCALER1_SHIFT;
  77. } else {
  78. tcfg0 &= S3C2410_TCFG_PRESCALER0_MASK;
  79. }
  80. return clk_get_rate(clk->parent) / (tcfg0 + 1);
  81. }
  82. static unsigned long clk_pwm_scaler_round_rate(struct clk *clk,
  83. unsigned long rate)
  84. {
  85. unsigned long parent_rate = clk_get_rate(clk->parent);
  86. unsigned long divisor = parent_rate / rate;
  87. if (divisor > 256)
  88. divisor = 256;
  89. else if (divisor < 2)
  90. divisor = 2;
  91. return parent_rate / divisor;
  92. }
  93. static int clk_pwm_scaler_set_rate(struct clk *clk, unsigned long rate)
  94. {
  95. unsigned long round = clk_pwm_scaler_round_rate(clk, rate);
  96. unsigned long tcfg0;
  97. unsigned long divisor;
  98. unsigned long flags;
  99. divisor = clk_get_rate(clk->parent) / round;
  100. divisor--;
  101. local_irq_save(flags);
  102. tcfg0 = __raw_readl(S3C2410_TCFG0);
  103. if (clk == &clk_timer_scaler[1]) {
  104. tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
  105. tcfg0 |= divisor << S3C2410_TCFG_PRESCALER1_SHIFT;
  106. } else {
  107. tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
  108. tcfg0 |= divisor;
  109. }
  110. __raw_writel(tcfg0, S3C2410_TCFG0);
  111. local_irq_restore(flags);
  112. return 0;
  113. }
  114. static struct clk_ops clk_pwm_scaler_ops = {
  115. .get_rate = clk_pwm_scaler_get_rate,
  116. .set_rate = clk_pwm_scaler_set_rate,
  117. .round_rate = clk_pwm_scaler_round_rate,
  118. };
  119. static struct clk clk_timer_scaler[] = {
  120. [0] = {
  121. .name = "pwm-scaler0",
  122. .id = -1,
  123. .ops = &clk_pwm_scaler_ops,
  124. },
  125. [1] = {
  126. .name = "pwm-scaler1",
  127. .id = -1,
  128. .ops = &clk_pwm_scaler_ops,
  129. },
  130. };
  131. static struct clk clk_timer_tclk[] = {
  132. [0] = {
  133. .name = "pwm-tclk0",
  134. .id = -1,
  135. },
  136. [1] = {
  137. .name = "pwm-tclk1",
  138. .id = -1,
  139. },
  140. };
  141. struct pwm_tdiv_clk {
  142. struct clk clk;
  143. unsigned int divisor;
  144. };
  145. static inline struct pwm_tdiv_clk *to_tdiv(struct clk *clk)
  146. {
  147. return container_of(clk, struct pwm_tdiv_clk, clk);
  148. }
  149. static unsigned long clk_pwm_tdiv_get_rate(struct clk *clk)
  150. {
  151. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  152. unsigned int divisor;
  153. tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
  154. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  155. if (pwm_cfg_src_is_tclk(tcfg1))
  156. divisor = to_tdiv(clk)->divisor;
  157. else
  158. divisor = tcfg_to_divisor(tcfg1);
  159. return clk_get_rate(clk->parent) / divisor;
  160. }
  161. static unsigned long clk_pwm_tdiv_round_rate(struct clk *clk,
  162. unsigned long rate)
  163. {
  164. unsigned long parent_rate;
  165. unsigned long divisor;
  166. parent_rate = clk_get_rate(clk->parent);
  167. divisor = parent_rate / rate;
  168. if (divisor <= 1 && pwm_tdiv_has_div1())
  169. divisor = 1;
  170. else if (divisor <= 2)
  171. divisor = 2;
  172. else if (divisor <= 4)
  173. divisor = 4;
  174. else if (divisor <= 8)
  175. divisor = 8;
  176. else
  177. divisor = 16;
  178. return parent_rate / divisor;
  179. }
  180. static unsigned long clk_pwm_tdiv_bits(struct pwm_tdiv_clk *divclk)