pwm.c 8.6 KB

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