pwm-clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <plat/clock.h>
  24. #include <plat/cpu.h>
  25. #include <plat/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_get_rate(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. static unsigned long clk_pwm_scaler_round_rate(struct clk *clk,
  81. unsigned long rate)
  82. {
  83. unsigned long parent_rate = clk_get_rate(clk->parent);
  84. unsigned long divisor = parent_rate / rate;
  85. if (divisor > 256)
  86. divisor = 256;
  87. else if (divisor < 2)
  88. divisor = 2;
  89. return parent_rate / divisor;
  90. }
  91. static int clk_pwm_scaler_set_rate(struct clk *clk, unsigned long rate)
  92. {
  93. unsigned long round = clk_pwm_scaler_round_rate(clk, rate);
  94. unsigned long tcfg0;
  95. unsigned long divisor;
  96. unsigned long flags;
  97. divisor = clk_get_rate(clk->parent) / round;
  98. divisor--;
  99. local_irq_save(flags);
  100. tcfg0 = __raw_readl(S3C2410_TCFG0);
  101. if (clk->id == 1) {
  102. tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
  103. tcfg0 |= divisor << S3C2410_TCFG_PRESCALER1_SHIFT;
  104. } else {
  105. tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
  106. tcfg0 |= divisor;
  107. }
  108. __raw_writel(tcfg0, S3C2410_TCFG0);
  109. local_irq_restore(flags);
  110. return 0;
  111. }
  112. static struct clk clk_timer_scaler[] = {
  113. [0] = {
  114. .name = "pwm-scaler0",
  115. .id = -1,
  116. .get_rate = clk_pwm_scaler_get_rate,
  117. .set_rate = clk_pwm_scaler_set_rate,
  118. .round_rate = clk_pwm_scaler_round_rate,
  119. },
  120. [1] = {
  121. .name = "pwm-scaler1",
  122. .id = -1,
  123. .get_rate = clk_pwm_scaler_get_rate,
  124. .set_rate = clk_pwm_scaler_set_rate,
  125. .round_rate = clk_pwm_scaler_round_rate,
  126. },
  127. };
  128. static struct clk clk_timer_tclk[] = {
  129. [0] = {
  130. .name = "pwm-tclk0",
  131. .id = -1,
  132. },
  133. [1] = {
  134. .name = "pwm-tclk1",
  135. .id = -1,
  136. },
  137. };
  138. struct pwm_tdiv_clk {
  139. struct clk clk;
  140. unsigned int divisor;
  141. };
  142. static inline struct pwm_tdiv_clk *to_tdiv(struct clk *clk)
  143. {
  144. return container_of(clk, struct pwm_tdiv_clk, clk);
  145. }
  146. static inline unsigned long tcfg_to_divisor(unsigned long tcfg1)
  147. {
  148. return 1 << (1 + tcfg1);
  149. }
  150. static unsigned long clk_pwm_tdiv_get_rate(struct clk *clk)
  151. {
  152. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  153. unsigned int divisor;
  154. tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
  155. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  156. if (tcfg1 == S3C2410_TCFG1_MUX_TCLK)
  157. divisor = to_tdiv(clk)->divisor;
  158. else
  159. divisor = tcfg_to_divisor(tcfg1);
  160. return clk_get_rate(clk->parent) / divisor;
  161. }
  162. static unsigned long clk_pwm_tdiv_round_rate(struct clk *clk,
  163. unsigned long rate)
  164. {
  165. unsigned long parent_rate;
  166. unsigned long divisor;
  167. parent_rate = clk_get_rate(clk->parent);
  168. divisor = parent_rate / rate;
  169. if (divisor <= 2)
  170. divisor = 2;
  171. else if (divisor <= 4)
  172. divisor = 4;
  173. else if (divisor <= 8)
  174. divisor = 8;
  175. else
  176. divisor = 16;
  177. return parent_rate / divisor;
  178. }
  179. static unsigned long clk_pwm_tdiv_bits(struct pwm_tdiv_clk *divclk)
  180. {
  181. unsigned long bits;
  182. switch (divclk->divisor) {
  183. case 2:
  184. bits = S3C2410_TCFG1_MUX_DIV2;
  185. break;
  186. case 4:
  187. bits = S3C2410_TCFG1_MUX_DIV4;
  188. break;
  189. case 8:
  190. bits = S3C2410_TCFG1_MUX_DIV8;
  191. break;
  192. case 16:
  193. default:
  194. bits = S3C2410_TCFG1_MUX_DIV16;
  195. break;
  196. }
  197. return bits;
  198. }
  199. static void clk_pwm_tdiv_update(struct pwm_tdiv_clk *divclk)
  200. {
  201. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  202. unsigned long bits = clk_pwm_tdiv_bits(divclk);
  203. unsigned long flags;
  204. unsigned long shift = S3C2410_TCFG1_SHIFT(divclk->clk.id);
  205. local_irq_save(flags);
  206. tcfg1 = __raw_readl(S3C2410_TCFG1);
  207. tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
  208. tcfg1 |= bits << shift;
  209. __raw_writel(tcfg1, S3C2410_TCFG1);
  210. local_irq_restore(flags);
  211. }
  212. static int clk_pwm_tdiv_set_rate(struct clk *clk, unsigned long rate)
  213. {
  214. struct pwm_tdiv_clk *divclk = to_tdiv(clk);
  215. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  216. unsigned long parent_rate = clk_get_rate(clk->parent);
  217. unsigned long divisor;
  218. tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
  219. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  220. rate = clk_round_rate(clk, rate);
  221. divisor = parent_rate / rate;
  222. if (divisor > 16)
  223. return -EINVAL;
  224. divclk->divisor = divisor;
  225. /* Update the current MUX settings if we are currently
  226. * selected as the clock source for this clock. */
  227. if (tcfg1 != S3C2410_TCFG1_MUX_TCLK)
  228. clk_pwm_tdiv_update(divclk);
  229. return 0;
  230. }
  231. static struct pwm_tdiv_clk clk_timer_tdiv[] = {
  232. [0] = {
  233. .clk = {
  234. .name = "pwm-tdiv",
  235. .parent = &clk_timer_scaler[0],
  236. .get_rate = clk_pwm_tdiv_get_rate,
  237. .set_rate = clk_pwm_tdiv_set_rate,
  238. .round_rate = clk_pwm_tdiv_round_rate,
  239. },
  240. },
  241. [1] = {
  242. .clk = {
  243. .name = "pwm-tdiv",
  244. .parent = &clk_timer_scaler[0],
  245. .get_rate = clk_pwm_tdiv_get_rate,
  246. .set_rate = clk_pwm_tdiv_set_rate,
  247. .round_rate = clk_pwm_tdiv_round_rate,
  248. }
  249. },
  250. [2] = {
  251. .clk = {
  252. .name = "pwm-tdiv",
  253. .parent = &clk_timer_scaler[1],
  254. .get_rate = clk_pwm_tdiv_get_rate,
  255. .set_rate = clk_pwm_tdiv_set_rate,
  256. .round_rate = clk_pwm_tdiv_round_rate,
  257. },
  258. },
  259. [3] = {
  260. .clk = {
  261. .name = "pwm-tdiv",
  262. .parent = &clk_timer_scaler[1],
  263. .get_rate = clk_pwm_tdiv_get_rate,
  264. .set_rate = clk_pwm_tdiv_set_rate,
  265. .round_rate = clk_pwm_tdiv_round_rate,
  266. },
  267. },
  268. [4] = {
  269. .clk = {
  270. .name = "pwm-tdiv",
  271. .parent = &clk_timer_scaler[1],
  272. .get_rate = clk_pwm_tdiv_get_rate,
  273. .set_rate = clk_pwm_tdiv_set_rate,
  274. .round_rate = clk_pwm_tdiv_round_rate,
  275. },
  276. },
  277. };
  278. static int __init clk_pwm_tdiv_register(unsigned int id)
  279. {
  280. struct pwm_tdiv_clk *divclk = &clk_timer_tdiv[id];
  281. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  282. tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
  283. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  284. divclk->clk.id = id;
  285. divclk->divisor = tcfg_to_divisor(tcfg1);
  286. return s3c24xx_register_clock(&divclk->clk);
  287. }
  288. static inline struct clk *s3c24xx_pwmclk_tclk(unsigned int id)
  289. {
  290. return (id >= 2) ? &clk_timer_tclk[1] : &clk_timer_tclk[0];
  291. }
  292. static inline struct clk *s3c24xx_pwmclk_tdiv(unsigned int id)
  293. {
  294. return &clk_timer_tdiv[id].clk;
  295. }
  296. static int clk_pwm_tin_set_parent(struct clk *clk, struct clk *parent)
  297. {
  298. unsigned int id = clk->id;
  299. unsigned long tcfg1;
  300. unsigned long flags;
  301. unsigned long bits;
  302. unsigned long shift = S3C2410_TCFG1_SHIFT(id);
  303. if (parent == s3c24xx_pwmclk_tclk(id))
  304. bits = S3C2410_TCFG1_MUX_TCLK << shift;
  305. else if (parent == s3c24xx_pwmclk_tdiv(id))
  306. bits = clk_pwm_tdiv_bits(to_tdiv(parent)) << shift;
  307. else
  308. return -EINVAL;
  309. clk->parent = parent;
  310. local_irq_save(flags);
  311. tcfg1 = __raw_readl(S3C2410_TCFG1);
  312. tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
  313. __raw_writel(tcfg1 | bits, S3C2410_TCFG1);
  314. local_irq_restore(flags);
  315. return 0;
  316. }
  317. static struct clk clk_tin[] = {
  318. [0] = {
  319. .name = "pwm-tin",
  320. .id = 0,
  321. .set_parent = clk_pwm_tin_set_parent,
  322. },
  323. [1] = {
  324. .name = "pwm-tin",
  325. .id = 1,
  326. .set_parent = clk_pwm_tin_set_parent,
  327. },
  328. [2] = {
  329. .name = "pwm-tin",
  330. .id = 2,
  331. .set_parent = clk_pwm_tin_set_parent,
  332. },
  333. [3] = {
  334. .name = "pwm-tin",
  335. .id = 3,
  336. .set_parent = clk_pwm_tin_set_parent,
  337. },
  338. [4] = {
  339. .name = "pwm-tin",
  340. .id = 4,
  341. .set_parent = clk_pwm_tin_set_parent,
  342. },
  343. };
  344. static __init int clk_pwm_tin_register(struct clk *pwm)
  345. {
  346. unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
  347. unsigned int id = pwm->id;
  348. struct clk *parent;
  349. int ret;
  350. ret = s3c24xx_register_clock(pwm);
  351. if (ret < 0)
  352. return ret;
  353. tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
  354. tcfg1 &= S3C2410_TCFG1_MUX_MASK;
  355. if (tcfg1 == S3C2410_TCFG1_MUX_TCLK)
  356. parent = s3c24xx_pwmclk_tclk(id);
  357. else
  358. parent = s3c24xx_pwmclk_tdiv(id);
  359. return clk_set_parent(pwm, parent);
  360. }
  361. static __init int s3c24xx_pwmclk_init(void)
  362. {
  363. struct clk *clk_timers;
  364. unsigned int clk;
  365. int ret;
  366. clk_timers = clk_get(NULL, "timers");
  367. if (IS_ERR(clk_timers)) {
  368. printk(KERN_ERR "%s: no parent clock\n", __func__);
  369. return -EINVAL;
  370. }
  371. for (clk = 0; clk < ARRAY_SIZE(clk_timer_scaler); clk++) {
  372. clk_timer_scaler[clk].parent = clk_timers;
  373. ret = s3c24xx_register_clock(&clk_timer_scaler[clk]);
  374. if (ret < 0) {
  375. printk(KERN_ERR "error adding pwm scaler%d clock\n", clk);
  376. goto err;
  377. }
  378. }
  379. for (clk = 0; clk < ARRAY_SIZE(clk_timer_tclk); clk++) {
  380. ret = s3c24xx_register_clock(&clk_timer_tclk[clk]);
  381. if (ret < 0) {
  382. printk(KERN_ERR "error adding pww tclk%d\n", clk);
  383. goto err;
  384. }
  385. }
  386. for (clk = 0; clk < ARRAY_SIZE(clk_timer_tdiv); clk++) {
  387. ret = clk_pwm_tdiv_register(clk);
  388. if (ret < 0) {
  389. printk(KERN_ERR "error adding pwm%d tdiv clock\n", clk);
  390. goto err;
  391. }
  392. }
  393. for (clk = 0; clk < ARRAY_SIZE(clk_tin); clk++) {
  394. ret = clk_pwm_tin_register(&clk_tin[clk]);
  395. if (ret < 0) {
  396. printk(KERN_ERR "error adding pwm%d tin clock\n", clk);
  397. goto err;
  398. }
  399. }
  400. return 0;
  401. err:
  402. return ret;
  403. }
  404. arch_initcall(s3c24xx_pwmclk_init);