pwm.c 8.5 KB

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