pwm.c 8.4 KB

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