pwm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics
  3. *
  4. * Donghwa Lee <dh09.lee@samsung.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <errno.h>
  26. #include <pwm.h>
  27. #include <asm/io.h>
  28. #include <asm/arch/pwm.h>
  29. #include <asm/arch/clk.h>
  30. int pwm_enable(int pwm_id)
  31. {
  32. const struct s5p_timer *pwm =
  33. (struct s5p_timer *)samsung_get_base_timer();
  34. unsigned long tcon;
  35. tcon = readl(&pwm->tcon);
  36. tcon |= TCON_START(pwm_id);
  37. writel(tcon, &pwm->tcon);
  38. return 0;
  39. }
  40. void pwm_disable(int pwm_id)
  41. {
  42. const struct s5p_timer *pwm =
  43. (struct s5p_timer *)samsung_get_base_timer();
  44. unsigned long tcon;
  45. tcon = readl(&pwm->tcon);
  46. tcon &= ~TCON_START(pwm_id);
  47. writel(tcon, &pwm->tcon);
  48. }
  49. static unsigned long pwm_calc_tin(int pwm_id, unsigned long freq)
  50. {
  51. unsigned long tin_parent_rate;
  52. unsigned int div;
  53. tin_parent_rate = get_pwm_clk();
  54. for (div = 2; div <= 16; div *= 2) {
  55. if ((tin_parent_rate / (div << 16)) < freq)
  56. return tin_parent_rate / div;
  57. }
  58. return tin_parent_rate / 16;
  59. }
  60. #define NS_IN_HZ (1000000000UL)
  61. int pwm_config(int pwm_id, int duty_ns, int period_ns)
  62. {
  63. const struct s5p_timer *pwm =
  64. (struct s5p_timer *)samsung_get_base_timer();
  65. unsigned int offset;
  66. unsigned long tin_rate;
  67. unsigned long tin_ns;
  68. unsigned long period;
  69. unsigned long tcon;
  70. unsigned long tcnt;
  71. unsigned long timer_rate_hz;
  72. unsigned long tcmp;
  73. /*
  74. * We currently avoid using 64bit arithmetic by using the
  75. * fact that anything faster than 1GHz is easily representable
  76. * by 32bits.
  77. */
  78. if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
  79. return -ERANGE;
  80. if (duty_ns > period_ns)
  81. return -EINVAL;
  82. period = NS_IN_HZ / period_ns;
  83. /* Check to see if we are changing the clock rate of the PWM */
  84. tin_rate = pwm_calc_tin(pwm_id, period);
  85. timer_rate_hz = tin_rate;
  86. tin_ns = NS_IN_HZ / tin_rate;
  87. tcnt = period_ns / tin_ns;
  88. /* Note, counters count down */
  89. tcmp = duty_ns / tin_ns;
  90. tcmp = tcnt - tcmp;
  91. /*
  92. * the pwm hw only checks the compare register after a decrement,
  93. * so the pin never toggles if tcmp = tcnt
  94. */
  95. if (tcmp == tcnt)
  96. tcmp--;
  97. if (tcmp < 0)
  98. tcmp = 0;
  99. /* Update the PWM register block. */
  100. offset = pwm_id * 3;
  101. if (pwm_id < 4) {
  102. writel(tcnt, &pwm->tcntb0 + offset);
  103. writel(tcmp, &pwm->tcmpb0 + offset);
  104. }
  105. tcon = readl(&pwm->tcon);
  106. tcon |= TCON_UPDATE(pwm_id);
  107. if (pwm_id < 4)
  108. tcon |= TCON_AUTO_RELOAD(pwm_id);
  109. else
  110. tcon |= TCON4_AUTO_RELOAD;
  111. writel(tcon, &pwm->tcon);
  112. tcon &= ~TCON_UPDATE(pwm_id);
  113. writel(tcon, &pwm->tcon);
  114. return 0;
  115. }
  116. int pwm_init(int pwm_id, int div, int invert)
  117. {
  118. u32 val;
  119. const struct s5p_timer *pwm =
  120. (struct s5p_timer *)samsung_get_base_timer();
  121. unsigned long timer_rate_hz;
  122. unsigned int offset, prescaler;
  123. /*
  124. * Timer Freq(HZ) =
  125. * PWM_CLK / { (prescaler_value + 1) * (divider_value) }
  126. */
  127. val = readl(&pwm->tcfg0);
  128. if (pwm_id < 2) {
  129. prescaler = PRESCALER_0;
  130. val &= ~0xff;
  131. val |= (prescaler & 0xff);
  132. } else {
  133. prescaler = PRESCALER_1;
  134. val &= ~(0xff << 8);
  135. val |= (prescaler & 0xff) << 8;
  136. }
  137. writel(val, &pwm->tcfg0);
  138. val = readl(&pwm->tcfg1);
  139. val &= ~(0xf << MUX_DIV_SHIFT(pwm_id));
  140. val |= (div & 0xf) << MUX_DIV_SHIFT(pwm_id);
  141. writel(val, &pwm->tcfg1);
  142. timer_rate_hz = get_pwm_clk() / ((prescaler + 1) *
  143. (div + 1));
  144. timer_rate_hz = timer_rate_hz / 100;
  145. /* set count value */
  146. offset = pwm_id * 3;
  147. writel(timer_rate_hz, &pwm->tcntb0 + offset);
  148. val = readl(&pwm->tcon) & ~(0xf << TCON_OFFSET(pwm_id));
  149. if (invert && (pwm_id < 4))
  150. val |= TCON_INVERTER(pwm_id);
  151. writel(val, &pwm->tcon);
  152. pwm_enable(pwm_id);
  153. return 0;
  154. }