pwm-clock.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <mach/hardware.h>
  20. #include <asm/irq.h>
  21. #include <mach/regs-clock.h>
  22. #include <mach/regs-gpio.h>
  23. #include <asm/plat-s3c24xx/clock.h>
  24. #include <asm/plat-s3c24xx/cpu.h>
  25. #include <asm/plat-s3c/regs-timer.h>
  26. /* Each of the timers 0 through 5 go through the following
  27. * clock tree, with the inputs depending on the timers.
  28. *
  29. * pclk ---- [ prescaler 0 ] -+---> timer 0
  30. * +---> timer 1
  31. *
  32. * pclk ---- [ prescaler 1 ] -+---> timer 2
  33. * +---> timer 3
  34. * \---> timer 4
  35. *
  36. * Which are fed into the timers as so:
  37. *
  38. * prescaled 0 ---- [ div 2,4,8,16 ] ---\
  39. * [mux] -> timer 0
  40. * tclk 0 ------------------------------/
  41. *
  42. * prescaled 0 ---- [ div 2,4,8,16 ] ---\
  43. * [mux] -> timer 1
  44. * tclk 0 ------------------------------/
  45. *
  46. *
  47. * prescaled 1 ---- [ div 2,4,8,16 ] ---\
  48. * [mux] -> timer 2
  49. * tclk 1 ------------------------------/
  50. *
  51. * prescaled 1 ---- [ div 2,4,8,16 ] ---\
  52. * [mux] -> timer 3
  53. * tclk 1 ------------------------------/
  54. *
  55. * prescaled 1 ---- [ div 2,4,8, 16 ] --\
  56. * [mux] -> timer 4
  57. * tclk 1 ------------------------------/
  58. *
  59. * Since the mux and the divider are tied together in the
  60. * same register space, it is impossible to set the parent
  61. * and the rate at the same time. To avoid this, we add an
  62. * intermediate 'prescaled-and-divided' clock to select
  63. * as the parent for the timer input clock called tdiv.
  64. *
  65. * prescaled clk --> pwm-tdiv ---\
  66. * [ mux ] --> timer X
  67. * tclk -------------------------/
  68. */
  69. static unsigned long clk_pwm_scaler_getrate(struct clk *clk)
  70. {
  71. unsigned long tcfg0 = __raw_readl(S3C2410_TCFG0);
  72. if (clk->id == 1) {
  73. tcfg0 &= S3C2410_TCFG_PRESCALER1_MASK;
  74. tcfg0 >>= S3C2410_TCFG_PRESCALER1_SHIFT;
  75. } else {
  76. tcfg0 &= S3C2410_TCFG_PRESCALER0_MASK;
  77. }
  78. return clk_get_rate(clk->parent) / (tcfg0 + 1);
  79. }
  80. /* TODO - add set rate calls. */
  81. static struct clk clk_timer_scaler[] = {
  82. [0] = {
  83. .name = "pwm-scaler0",
  84. .id = -1,
  85. .get_rate = clk_pwm_scaler_getrate,
  86. },
  87. [1] = {
  88. .name = "pwm-scaler1",
  89. .id = -1,
  90. .get_rate = clk_pwm_scaler_getrate,
  91. },
  92. };
  93. static struct clk clk_timer_tclk[] = {
  94. [0] = {
  95. .name = "pwm-tclk0",
  96. .id = -1,
  97. },
  98. [1] = {
  99. .name = "pwm-tclk1",
  100. .id = -1,
  101. },
  102. };
  103. struct pwm_tdiv_clk {
  104. struct clk clk;
  105. unsigned int divisor;
  106. };
  107. static inline struct pwm_tdiv_clk *to_tdiv(struct clk *clk)
  108. {
  109. return container_of(clk, struct pwm_tdiv_clk, clk);
  110. }
  111. static inline unsigned long tcfg_to_divisor(unsigned long tcfg1)
  112. {
  113. return 1 << (1 + tcfg1);
  114. }
  115. static unsigned long clk_pwm_tdiv_get_rate(struct clk *clk)
  116. {
  117. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  118. unsigned int divisor;
  119. tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
  120. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  121. if (tcfg1 == S3C2410_TCFG1_MUX_TCLK)
  122. divisor = to_tdiv(clk)->divisor;
  123. else
  124. divisor = tcfg_to_divisor(tcfg1);
  125. return clk_get_rate(clk->parent) / divisor;
  126. }
  127. static unsigned long clk_pwm_tdiv_round_rate(struct clk *clk,
  128. unsigned long rate)
  129. {
  130. unsigned long parent_rate;
  131. unsigned long divisor;
  132. parent_rate = clk_get_rate(clk->parent);
  133. divisor = parent_rate / rate;
  134. if (divisor <= 2)
  135. divisor = 2;
  136. else if (divisor <= 4)
  137. divisor = 4;
  138. else if (divisor <= 8)
  139. divisor = 8;
  140. else
  141. divisor = 16;
  142. return parent_rate / divisor;
  143. }
  144. static unsigned long clk_pwm_tdiv_bits(struct pwm_tdiv_clk *divclk)
  145. {
  146. unsigned long bits;
  147. switch (divclk->divisor) {
  148. case 2:
  149. bits = S3C2410_TCFG1_MUX_DIV2;
  150. break;
  151. case 4:
  152. bits = S3C2410_TCFG1_MUX_DIV4;
  153. break;
  154. case 8:
  155. bits = S3C2410_TCFG1_MUX_DIV8;
  156. break;
  157. case 16:
  158. default:
  159. bits = S3C2410_TCFG1_MUX_DIV16;
  160. break;
  161. }
  162. return bits;
  163. }
  164. static void clk_pwm_tdiv_update(struct pwm_tdiv_clk *divclk)
  165. {
  166. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  167. unsigned long bits = clk_pwm_tdiv_bits(divclk);
  168. unsigned long flags;
  169. unsigned long shift = S3C2410_TCFG1_SHIFT(divclk->clk.id);
  170. local_irq_save(flags);
  171. tcfg1 = __raw_readl(S3C2410_TCFG1);
  172. tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
  173. tcfg1 |= bits << shift;
  174. __raw_writel(tcfg1, S3C2410_TCFG1);
  175. local_irq_restore(flags);
  176. }
  177. static int clk_pwm_tdiv_set_rate(struct clk *clk, unsigned long rate)
  178. {
  179. struct pwm_tdiv_clk *divclk = to_tdiv(clk);
  180. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  181. unsigned long parent_rate = clk_get_rate(clk->parent);
  182. unsigned long divisor;
  183. tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
  184. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  185. rate = clk_round_rate(clk, rate);
  186. divisor = parent_rate / rate;
  187. if (divisor > 16)
  188. return -EINVAL;
  189. divclk->divisor = divisor;
  190. /* Update the current MUX settings if we are currently
  191. * selected as the clock source for this clock. */
  192. if (tcfg1 != S3C2410_TCFG1_MUX_TCLK)
  193. clk_pwm_tdiv_update(divclk);
  194. return 0;
  195. }
  196. static struct pwm_tdiv_clk clk_timer_tdiv[] = {
  197. [0] = {
  198. .clk = {
  199. .name = "pwm-tdiv",
  200. .parent = &clk_timer_scaler[0],
  201. .get_rate = clk_pwm_tdiv_get_rate,
  202. .set_rate = clk_pwm_tdiv_set_rate,
  203. .round_rate = clk_pwm_tdiv_round_rate,
  204. },
  205. },
  206. [1] = {
  207. .clk = {
  208. .name = "pwm-tdiv",
  209. .parent = &clk_timer_scaler[0],
  210. .get_rate = clk_pwm_tdiv_get_rate,
  211. .set_rate = clk_pwm_tdiv_set_rate,
  212. .round_rate = clk_pwm_tdiv_round_rate,
  213. }
  214. },
  215. [2] = {
  216. .clk = {
  217. .name = "pwm-tdiv",
  218. .parent = &clk_timer_scaler[1],
  219. .get_rate = clk_pwm_tdiv_get_rate,
  220. .set_rate = clk_pwm_tdiv_set_rate,
  221. .round_rate = clk_pwm_tdiv_round_rate,
  222. },
  223. },
  224. [3] = {
  225. .clk = {
  226. .name = "pwm-tdiv",
  227. .parent = &clk_timer_scaler[1],
  228. .get_rate = clk_pwm_tdiv_get_rate,
  229. .set_rate = clk_pwm_tdiv_set_rate,
  230. .round_rate = clk_pwm_tdiv_round_rate,
  231. },
  232. },
  233. [4] = {
  234. .clk = {
  235. .name = "pwm-tdiv",
  236. .parent = &clk_timer_scaler[1],
  237. .get_rate = clk_pwm_tdiv_get_rate,
  238. .set_rate = clk_pwm_tdiv_set_rate,
  239. .round_rate = clk_pwm_tdiv_round_rate,
  240. },
  241. },
  242. };
  243. static int __init clk_pwm_tdiv_register(unsigned int id)
  244. {
  245. struct pwm_tdiv_clk *divclk = &clk_timer_tdiv[id];
  246. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  247. tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
  248. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  249. divclk->clk.id = id;
  250. divclk->divisor = tcfg_to_divisor(tcfg1);
  251. return s3c24xx_register_clock(&divclk->clk);
  252. }
  253. static inline struct clk *s3c24xx_pwmclk_tclk(unsigned int id)
  254. {
  255. return (id >= 2) ? &clk_timer_tclk[1] : &clk_timer_tclk[0];
  256. }
  257. static inline struct clk *s3c24xx_pwmclk_tdiv(unsigned int id)
  258. {
  259. return &clk_timer_tdiv[id].clk;
  260. }
  261. static int clk_pwm_tin_set_parent(struct clk *clk, struct clk *parent)
  262. {
  263. unsigned int id = clk->id;
  264. unsigned long tcfg1;
  265. unsigned long flags;
  266. unsigned long bits;
  267. unsigned long shift = S3C2410_TCFG1_SHIFT(id);
  268. if (parent == s3c24xx_pwmclk_tclk(id))
  269. bits = S3C2410_TCFG1_MUX_TCLK << shift;
  270. else if (parent == s3c24xx_pwmclk_tdiv(id))
  271. bits = clk_pwm_tdiv_bits(to_tdiv(clk)) << shift;
  272. else
  273. return -EINVAL;
  274. clk->parent = parent;
  275. local_irq_save(flags);
  276. tcfg1 = __raw_readl(S3C2410_TCFG1);
  277. tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
  278. __raw_writel(tcfg1 | bits, S3C2410_TCFG1);
  279. local_irq_restore(flags);
  280. return 0;
  281. }
  282. static struct clk clk_tin[] = {
  283. [0] = {
  284. .name = "pwm-tin",
  285. .id = 0,
  286. .set_parent = clk_pwm_tin_set_parent,
  287. },
  288. [1] = {
  289. .name = "pwm-tin",
  290. .id = 1,
  291. .set_parent = clk_pwm_tin_set_parent,
  292. },
  293. [2] = {
  294. .name = "pwm-tin",
  295. .id = 2,
  296. .set_parent = clk_pwm_tin_set_parent,
  297. },
  298. [3] = {
  299. .name = "pwm-tin",
  300. .id = 3,
  301. .set_parent = clk_pwm_tin_set_parent,
  302. },
  303. [4] = {
  304. .name = "pwm-tin",
  305. .id = 4,
  306. .set_parent = clk_pwm_tin_set_parent,
  307. },
  308. };
  309. static __init int clk_pwm_tin_register(struct clk *pwm)
  310. {
  311. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  312. unsigned int id = pwm->id;
  313. struct clk *parent;
  314. int ret;
  315. ret = s3c24xx_register_clock(pwm);
  316. if (ret < 0)
  317. return ret;
  318. tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
  319. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  320. if (tcfg1 == S3C2410_TCFG1_MUX_TCLK)
  321. parent = s3c24xx_pwmclk_tclk(id);
  322. else
  323. parent = s3c24xx_pwmclk_tdiv(id);
  324. return clk_set_parent(pwm, parent);
  325. }
  326. static __init int s3c24xx_pwmclk_init(void)
  327. {
  328. struct clk *clk_timers;
  329. unsigned int clk;
  330. int ret;
  331. clk_timers = clk_get(NULL, "timers");
  332. if (IS_ERR(clk_timers)) {
  333. printk(KERN_ERR "%s: no parent clock\n", __func__);
  334. return -EINVAL;
  335. }
  336. for (clk = 0; clk < ARRAY_SIZE(clk_timer_scaler); clk++) {
  337. clk_timer_scaler[clk].parent = clk_timers;
  338. ret = s3c24xx_register_clock(&clk_timer_scaler[clk]);
  339. if (ret < 0) {
  340. printk(KERN_ERR "error adding pwm scaler%d clock\n", clk);
  341. goto err;
  342. }
  343. }
  344. for (clk = 0; clk < ARRAY_SIZE(clk_timer_tclk); clk++) {
  345. ret = s3c24xx_register_clock(&clk_timer_tclk[clk]);
  346. if (ret < 0) {
  347. printk(KERN_ERR "error adding pww tclk%d\n", clk);
  348. goto err;
  349. }
  350. }
  351. for (clk = 0; clk < ARRAY_SIZE(clk_timer_tdiv); clk++) {
  352. ret = clk_pwm_tdiv_register(clk);
  353. if (ret < 0) {
  354. printk(KERN_ERR "error adding pwm%d tdiv clock\n", clk);
  355. goto err;
  356. }
  357. }
  358. for (clk = 0; clk < ARRAY_SIZE(clk_tin); clk++) {
  359. ret = clk_pwm_tin_register(&clk_tin[clk]);
  360. if (ret < 0) {
  361. printk(KERN_ERR "error adding pwm%d tin clock\n", clk);
  362. goto err;
  363. }
  364. }
  365. return 0;
  366. err:
  367. return ret;
  368. }
  369. arch_initcall(s3c24xx_pwmclk_init);