pwm-clock.c 11 KB

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