pwm-clock.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 <mach/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)
  181. {
  182. return pwm_tdiv_div_bits(divclk->divisor);
  183. }
  184. static void clk_pwm_tdiv_update(struct pwm_tdiv_clk *divclk)
  185. {
  186. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  187. unsigned long bits = clk_pwm_tdiv_bits(divclk);
  188. unsigned long flags;
  189. unsigned long shift = S3C2410_TCFG1_SHIFT(divclk->clk.id);
  190. local_irq_save(flags);
  191. tcfg1 = __raw_readl(S3C2410_TCFG1);
  192. tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
  193. tcfg1 |= bits << shift;
  194. __raw_writel(tcfg1, S3C2410_TCFG1);
  195. local_irq_restore(flags);
  196. }
  197. static int clk_pwm_tdiv_set_rate(struct clk *clk, unsigned long rate)
  198. {
  199. struct pwm_tdiv_clk *divclk = to_tdiv(clk);
  200. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  201. unsigned long parent_rate = clk_get_rate(clk->parent);
  202. unsigned long divisor;
  203. tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
  204. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  205. rate = clk_round_rate(clk, rate);
  206. divisor = parent_rate / rate;
  207. if (divisor > 16)
  208. return -EINVAL;
  209. divclk->divisor = divisor;
  210. /* Update the current MUX settings if we are currently
  211. * selected as the clock source for this clock. */
  212. if (!pwm_cfg_src_is_tclk(tcfg1))
  213. clk_pwm_tdiv_update(divclk);
  214. return 0;
  215. }
  216. static struct clk_ops clk_tdiv_ops = {
  217. .get_rate = clk_pwm_tdiv_get_rate,
  218. .set_rate = clk_pwm_tdiv_set_rate,
  219. .round_rate = clk_pwm_tdiv_round_rate,
  220. };
  221. static struct pwm_tdiv_clk clk_timer_tdiv[] = {
  222. [0] = {
  223. .clk = {
  224. .name = "pwm-tdiv",
  225. .ops = &clk_tdiv_ops,
  226. .parent = &clk_timer_scaler[0],
  227. },
  228. },
  229. [1] = {
  230. .clk = {
  231. .name = "pwm-tdiv",
  232. .ops = &clk_tdiv_ops,
  233. .parent = &clk_timer_scaler[0],
  234. }
  235. },
  236. [2] = {
  237. .clk = {
  238. .name = "pwm-tdiv",
  239. .ops = &clk_tdiv_ops,
  240. .parent = &clk_timer_scaler[1],
  241. },
  242. },
  243. [3] = {
  244. .clk = {
  245. .name = "pwm-tdiv",
  246. .ops = &clk_tdiv_ops,
  247. .parent = &clk_timer_scaler[1],
  248. },
  249. },
  250. [4] = {
  251. .clk = {
  252. .name = "pwm-tdiv",
  253. .ops = &clk_tdiv_ops,
  254. .parent = &clk_timer_scaler[1],
  255. },
  256. },
  257. };
  258. static int __init clk_pwm_tdiv_register(unsigned int id)
  259. {
  260. struct pwm_tdiv_clk *divclk = &clk_timer_tdiv[id];
  261. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  262. tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
  263. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  264. divclk->clk.id = id;
  265. divclk->divisor = tcfg_to_divisor(tcfg1);
  266. return s3c24xx_register_clock(&divclk->clk);
  267. }
  268. static inline struct clk *s3c24xx_pwmclk_tclk(unsigned int id)
  269. {
  270. return (id >= 2) ? &clk_timer_tclk[1] : &clk_timer_tclk[0];
  271. }
  272. static inline struct clk *s3c24xx_pwmclk_tdiv(unsigned int id)
  273. {
  274. return &clk_timer_tdiv[id].clk;
  275. }
  276. static int clk_pwm_tin_set_parent(struct clk *clk, struct clk *parent)
  277. {
  278. unsigned int id = clk->id;
  279. unsigned long tcfg1;
  280. unsigned long flags;
  281. unsigned long bits;
  282. unsigned long shift = S3C2410_TCFG1_SHIFT(id);
  283. if (parent == s3c24xx_pwmclk_tclk(id))
  284. bits = S3C_TCFG1_MUX_TCLK << shift;
  285. else if (parent == s3c24xx_pwmclk_tdiv(id))
  286. bits = clk_pwm_tdiv_bits(to_tdiv(parent)) << shift;
  287. else
  288. return -EINVAL;
  289. clk->parent = parent;
  290. local_irq_save(flags);
  291. tcfg1 = __raw_readl(S3C2410_TCFG1);
  292. tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
  293. __raw_writel(tcfg1 | bits, S3C2410_TCFG1);
  294. local_irq_restore(flags);
  295. return 0;
  296. }
  297. static struct clk_ops clk_tin_ops = {
  298. .set_parent = clk_pwm_tin_set_parent,
  299. };
  300. static struct clk clk_tin[] = {
  301. [0] = {
  302. .name = "pwm-tin",
  303. .id = 0,
  304. .ops = &clk_tin_ops,
  305. },
  306. [1] = {
  307. .name = "pwm-tin",
  308. .id = 1,
  309. .ops = &clk_tin_ops,
  310. },
  311. [2] = {
  312. .name = "pwm-tin",
  313. .id = 2,
  314. .ops = &clk_tin_ops,
  315. },
  316. [3] = {
  317. .name = "pwm-tin",
  318. .id = 3,
  319. .ops = &clk_tin_ops,
  320. },
  321. [4] = {
  322. .name = "pwm-tin",
  323. .id = 4,
  324. .ops = &clk_tin_ops,
  325. },
  326. };
  327. static __init int clk_pwm_tin_register(struct clk *pwm)
  328. {
  329. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  330. unsigned int id = pwm->id;
  331. struct clk *parent;
  332. int ret;
  333. ret = s3c24xx_register_clock(pwm);
  334. if (ret < 0)
  335. return ret;
  336. tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
  337. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  338. if (pwm_cfg_src_is_tclk(tcfg1))
  339. parent = s3c24xx_pwmclk_tclk(id);
  340. else
  341. parent = s3c24xx_pwmclk_tdiv(id);
  342. return clk_set_parent(pwm, parent);
  343. }
  344. /**
  345. * s3c_pwmclk_init() - initialise pwm clocks
  346. *
  347. * Initialise and register the clocks which provide the inputs for the
  348. * pwm timer blocks.
  349. *
  350. * Note, this call is required by the time core, so must be called after
  351. * the base clocks are added and before any of the initcalls are run.
  352. */
  353. __init void s3c_pwmclk_init(void)
  354. {
  355. struct clk *clk_timers;
  356. unsigned int clk;
  357. int ret;
  358. clk_timers = clk_get(NULL, "timers");
  359. if (IS_ERR(clk_timers)) {
  360. printk(KERN_ERR "%s: no parent clock\n", __func__);
  361. return;
  362. }
  363. for (clk = 0; clk < ARRAY_SIZE(clk_timer_scaler); clk++)
  364. clk_timer_scaler[clk].parent = clk_timers;
  365. s3c_register_clocks(clk_timer_scaler, ARRAY_SIZE(clk_timer_scaler));
  366. s3c_register_clocks(clk_timer_tclk, ARRAY_SIZE(clk_timer_tclk));
  367. for (clk = 0; clk < ARRAY_SIZE(clk_timer_tdiv); clk++) {
  368. ret = clk_pwm_tdiv_register(clk);
  369. if (ret < 0) {
  370. printk(KERN_ERR "error adding pwm%d tdiv clock\n", clk);
  371. return;
  372. }
  373. }
  374. for (clk = 0; clk < ARRAY_SIZE(clk_tin); clk++) {
  375. ret = clk_pwm_tin_register(&clk_tin[clk]);
  376. if (ret < 0) {
  377. printk(KERN_ERR "error adding pwm%d tin clock\n", clk);
  378. return;
  379. }
  380. }
  381. }