pwm.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* arch/arm/plat-s3c/pwm.c
  2. *
  3. * Copyright (c) 2007 Ben Dooks
  4. * Copyright (c) 2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
  6. *
  7. * S3C series PWM device core
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/pwm.h>
  21. #include <mach/irqs.h>
  22. #include <mach/map.h>
  23. #include <plat/devs.h>
  24. #include <plat/regs-timer.h>
  25. struct pwm_device {
  26. struct list_head list;
  27. struct platform_device *pdev;
  28. struct clk *clk_div;
  29. struct clk *clk;
  30. const char *label;
  31. unsigned int period_ns;
  32. unsigned int duty_ns;
  33. unsigned char tcon_base;
  34. unsigned char running;
  35. unsigned char use_count;
  36. unsigned char pwm_id;
  37. };
  38. #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
  39. static struct clk *clk_scaler[2];
  40. /* Standard setup for a timer block. */
  41. #define TIMER_RESOURCE_SIZE (1)
  42. #define TIMER_RESOURCE(_tmr, _irq) \
  43. (struct resource [TIMER_RESOURCE_SIZE]) { \
  44. [0] = { \
  45. .start = _irq, \
  46. .end = _irq, \
  47. .flags = IORESOURCE_IRQ \
  48. } \
  49. }
  50. #define DEFINE_S3C_TIMER(_tmr_no, _irq) \
  51. .name = "s3c24xx-pwm", \
  52. .id = _tmr_no, \
  53. .num_resources = TIMER_RESOURCE_SIZE, \
  54. .resource = TIMER_RESOURCE(_tmr_no, _irq), \
  55. /* since we already have an static mapping for the timer, we do not
  56. * bother setting any IO resource for the base.
  57. */
  58. struct platform_device s3c_device_timer[] = {
  59. [0] = { DEFINE_S3C_TIMER(0, IRQ_TIMER0) },
  60. [1] = { DEFINE_S3C_TIMER(1, IRQ_TIMER1) },
  61. [2] = { DEFINE_S3C_TIMER(2, IRQ_TIMER2) },
  62. [3] = { DEFINE_S3C_TIMER(3, IRQ_TIMER3) },
  63. [4] = { DEFINE_S3C_TIMER(4, IRQ_TIMER4) },
  64. };
  65. static inline int pwm_is_tdiv(struct pwm_device *pwm)
  66. {
  67. return clk_get_parent(pwm->clk) == pwm->clk_div;
  68. }
  69. static DEFINE_MUTEX(pwm_lock);
  70. static LIST_HEAD(pwm_list);
  71. struct pwm_device *pwm_request(int pwm_id, const char *label)
  72. {
  73. struct pwm_device *pwm;
  74. int found = 0;
  75. mutex_lock(&pwm_lock);
  76. list_for_each_entry(pwm, &pwm_list, list) {
  77. if (pwm->pwm_id == pwm_id) {
  78. found = 1;
  79. break;
  80. }
  81. }
  82. if (found) {
  83. if (pwm->use_count == 0) {
  84. pwm->use_count = 1;
  85. pwm->label = label;
  86. } else
  87. pwm = ERR_PTR(-EBUSY);
  88. } else
  89. pwm = ERR_PTR(-ENOENT);
  90. mutex_unlock(&pwm_lock);
  91. return pwm;
  92. }
  93. EXPORT_SYMBOL(pwm_request);
  94. void pwm_free(struct pwm_device *pwm)
  95. {
  96. mutex_lock(&pwm_lock);
  97. if (pwm->use_count) {
  98. pwm->use_count--;
  99. pwm->label = NULL;
  100. } else
  101. printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
  102. mutex_unlock(&pwm_lock);
  103. }
  104. EXPORT_SYMBOL(pwm_free);
  105. #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
  106. #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
  107. #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
  108. #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
  109. int pwm_enable(struct pwm_device *pwm)
  110. {
  111. unsigned long flags;
  112. unsigned long tcon;
  113. local_irq_save(flags);
  114. tcon = __raw_readl(S3C2410_TCON);
  115. tcon |= pwm_tcon_start(pwm);
  116. __raw_writel(tcon, S3C2410_TCON);
  117. local_irq_restore(flags);
  118. pwm->running = 1;
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(pwm_enable);
  122. void pwm_disable(struct pwm_device *pwm)
  123. {
  124. unsigned long flags;
  125. unsigned long tcon;
  126. local_irq_save(flags);
  127. tcon = __raw_readl(S3C2410_TCON);
  128. tcon &= ~pwm_tcon_start(pwm);
  129. __raw_writel(tcon, S3C2410_TCON);
  130. local_irq_restore(flags);
  131. pwm->running = 0;
  132. }
  133. EXPORT_SYMBOL(pwm_disable);
  134. static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
  135. {
  136. unsigned long tin_parent_rate;
  137. unsigned int div;
  138. tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div));
  139. pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate);
  140. for (div = 2; div <= 16; div *= 2) {
  141. if ((tin_parent_rate / (div << 16)) < freq)
  142. return tin_parent_rate / div;
  143. }
  144. return tin_parent_rate / 16;
  145. }
  146. #define NS_IN_HZ (1000000000UL)
  147. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  148. {
  149. unsigned long tin_rate;
  150. unsigned long tin_ns;
  151. unsigned long period;
  152. unsigned long flags;
  153. unsigned long tcon;
  154. unsigned long tcnt;
  155. long tcmp;
  156. /* We currently avoid using 64bit arithmetic by using the
  157. * fact that anything faster than 1Hz is easily representable
  158. * by 32bits. */
  159. if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
  160. return -ERANGE;
  161. if (duty_ns > period_ns)
  162. return -EINVAL;
  163. if (period_ns == pwm->period_ns &&
  164. duty_ns == pwm->duty_ns)
  165. return 0;
  166. /* The TCMP and TCNT can be read without a lock, they're not
  167. * shared between the timers. */
  168. tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
  169. tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
  170. period = NS_IN_HZ / period_ns;
  171. pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
  172. duty_ns, period_ns, period);
  173. /* Check to see if we are changing the clock rate of the PWM */
  174. if (pwm->period_ns != period_ns) {
  175. if (pwm_is_tdiv(pwm)) {
  176. tin_rate = pwm_calc_tin(pwm, period);
  177. clk_set_rate(pwm->clk_div, tin_rate);
  178. } else
  179. tin_rate = clk_get_rate(pwm->clk);
  180. pwm->period_ns = period_ns;
  181. pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
  182. tin_ns = NS_IN_HZ / tin_rate;
  183. tcnt = period_ns / tin_ns;
  184. } else
  185. tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
  186. /* Note, counters count down */
  187. tcmp = duty_ns / tin_ns;
  188. tcmp = tcnt - tcmp;
  189. /* the pwm hw only checks the compare register after a decrement,
  190. so the pin never toggles if tcmp = tcnt */
  191. if (tcmp == tcnt)
  192. tcmp--;
  193. pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
  194. if (tcmp < 0)
  195. tcmp = 0;
  196. /* Update the PWM register block. */
  197. local_irq_save(flags);
  198. __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
  199. __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
  200. tcon = __raw_readl(S3C2410_TCON);
  201. tcon |= pwm_tcon_manulupdate(pwm);
  202. tcon |= pwm_tcon_autoreload(pwm);
  203. __raw_writel(tcon, S3C2410_TCON);
  204. tcon &= ~pwm_tcon_manulupdate(pwm);
  205. __raw_writel(tcon, S3C2410_TCON);
  206. local_irq_restore(flags);
  207. return 0;
  208. }
  209. EXPORT_SYMBOL(pwm_config);
  210. static int pwm_register(struct pwm_device *pwm)
  211. {
  212. pwm->duty_ns = -1;
  213. pwm->period_ns = -1;
  214. mutex_lock(&pwm_lock);
  215. list_add_tail(&pwm->list, &pwm_list);
  216. mutex_unlock(&pwm_lock);
  217. return 0;
  218. }
  219. static int s3c_pwm_probe(struct platform_device *pdev)
  220. {
  221. struct device *dev = &pdev->dev;
  222. struct pwm_device *pwm;
  223. unsigned long flags;
  224. unsigned long tcon;
  225. unsigned int id = pdev->id;
  226. int ret;
  227. if (id == 4) {
  228. dev_err(dev, "TIMER4 is currently not supported\n");
  229. return -ENXIO;
  230. }
  231. pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
  232. if (pwm == NULL) {
  233. dev_err(dev, "failed to allocate pwm_device\n");
  234. return -ENOMEM;
  235. }
  236. pwm->pdev = pdev;
  237. pwm->pwm_id = id;
  238. /* calculate base of control bits in TCON */
  239. pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
  240. pwm->clk = clk_get(dev, "pwm-tin");
  241. if (IS_ERR(pwm->clk)) {
  242. dev_err(dev, "failed to get pwm tin clk\n");
  243. ret = PTR_ERR(pwm->clk);
  244. goto err_alloc;
  245. }
  246. pwm->clk_div = clk_get(dev, "pwm-tdiv");
  247. if (IS_ERR(pwm->clk_div)) {
  248. dev_err(dev, "failed to get pwm tdiv clk\n");
  249. ret = PTR_ERR(pwm->clk_div);
  250. goto err_clk_tin;
  251. }
  252. local_irq_save(flags);
  253. tcon = __raw_readl(S3C2410_TCON);
  254. tcon |= pwm_tcon_invert(pwm);
  255. __raw_writel(tcon, S3C2410_TCON);
  256. local_irq_restore(flags);
  257. ret = pwm_register(pwm);
  258. if (ret) {
  259. dev_err(dev, "failed to register pwm\n");
  260. goto err_clk_tdiv;
  261. }
  262. pwm_dbg(pwm, "config bits %02x\n",
  263. (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
  264. dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
  265. clk_get_rate(pwm->clk),
  266. clk_get_rate(pwm->clk_div),
  267. pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
  268. platform_set_drvdata(pdev, pwm);
  269. return 0;
  270. err_clk_tdiv:
  271. clk_put(pwm->clk_div);
  272. err_clk_tin:
  273. clk_put(pwm->clk);
  274. err_alloc:
  275. kfree(pwm);
  276. return ret;
  277. }
  278. static int __devexit s3c_pwm_remove(struct platform_device *pdev)
  279. {
  280. struct pwm_device *pwm = platform_get_drvdata(pdev);
  281. clk_put(pwm->clk_div);
  282. clk_put(pwm->clk);
  283. kfree(pwm);
  284. return 0;
  285. }
  286. #ifdef CONFIG_PM
  287. static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state)
  288. {
  289. struct pwm_device *pwm = platform_get_drvdata(pdev);
  290. /* No one preserve these values during suspend so reset them
  291. * Otherwise driver leaves PWM unconfigured if same values
  292. * passed to pwm_config
  293. */
  294. pwm->period_ns = 0;
  295. pwm->duty_ns = 0;
  296. return 0;
  297. }
  298. static int s3c_pwm_resume(struct platform_device *pdev)
  299. {
  300. struct pwm_device *pwm = platform_get_drvdata(pdev);
  301. unsigned long tcon;
  302. /* Restore invertion */
  303. tcon = __raw_readl(S3C2410_TCON);
  304. tcon |= pwm_tcon_invert(pwm);
  305. __raw_writel(tcon, S3C2410_TCON);
  306. return 0;
  307. }
  308. #else
  309. #define s3c_pwm_suspend NULL
  310. #define s3c_pwm_resume NULL
  311. #endif
  312. static struct platform_driver s3c_pwm_driver = {
  313. .driver = {
  314. .name = "s3c24xx-pwm",
  315. .owner = THIS_MODULE,
  316. },
  317. .probe = s3c_pwm_probe,
  318. .remove = __devexit_p(s3c_pwm_remove),
  319. .suspend = s3c_pwm_suspend,
  320. .resume = s3c_pwm_resume,
  321. };
  322. static int __init pwm_init(void)
  323. {
  324. int ret;
  325. clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
  326. clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
  327. if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
  328. printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
  329. return -EINVAL;
  330. }
  331. ret = platform_driver_register(&s3c_pwm_driver);
  332. if (ret)
  333. printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
  334. return ret;
  335. }
  336. arch_initcall(pwm_init);