pwm.c 8.6 KB

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