atmel_pwm.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include <linux/module.h>
  2. #include <linux/clk.h>
  3. #include <linux/err.h>
  4. #include <linux/slab.h>
  5. #include <linux/io.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/atmel_pwm.h>
  9. /*
  10. * This is a simple driver for the PWM controller found in various newer
  11. * Atmel SOCs, including the AVR32 series and the AT91sam9263.
  12. *
  13. * Chips with current Linux ports have only 4 PWM channels, out of max 32.
  14. * AT32UC3A and AT32UC3B chips have 7 channels (but currently no Linux).
  15. * Docs are inconsistent about the width of the channel counter registers;
  16. * it's at least 16 bits, but several places say 20 bits.
  17. */
  18. #define PWM_NCHAN 4 /* max 32 */
  19. struct pwm {
  20. spinlock_t lock;
  21. struct platform_device *pdev;
  22. u32 mask;
  23. int irq;
  24. void __iomem *base;
  25. struct clk *clk;
  26. struct pwm_channel *channel[PWM_NCHAN];
  27. void (*handler[PWM_NCHAN])(struct pwm_channel *);
  28. };
  29. /* global PWM controller registers */
  30. #define PWM_MR 0x00
  31. #define PWM_ENA 0x04
  32. #define PWM_DIS 0x08
  33. #define PWM_SR 0x0c
  34. #define PWM_IER 0x10
  35. #define PWM_IDR 0x14
  36. #define PWM_IMR 0x18
  37. #define PWM_ISR 0x1c
  38. static inline void pwm_writel(const struct pwm *p, unsigned offset, u32 val)
  39. {
  40. __raw_writel(val, p->base + offset);
  41. }
  42. static inline u32 pwm_readl(const struct pwm *p, unsigned offset)
  43. {
  44. return __raw_readl(p->base + offset);
  45. }
  46. static inline void __iomem *pwmc_regs(const struct pwm *p, int index)
  47. {
  48. return p->base + 0x200 + index * 0x20;
  49. }
  50. static struct pwm *pwm;
  51. static void pwm_dumpregs(struct pwm_channel *ch, char *tag)
  52. {
  53. struct device *dev = &pwm->pdev->dev;
  54. dev_dbg(dev, "%s: mr %08x, sr %08x, imr %08x\n",
  55. tag,
  56. pwm_readl(pwm, PWM_MR),
  57. pwm_readl(pwm, PWM_SR),
  58. pwm_readl(pwm, PWM_IMR));
  59. dev_dbg(dev,
  60. "pwm ch%d - mr %08x, dty %u, prd %u, cnt %u\n",
  61. ch->index,
  62. pwm_channel_readl(ch, PWM_CMR),
  63. pwm_channel_readl(ch, PWM_CDTY),
  64. pwm_channel_readl(ch, PWM_CPRD),
  65. pwm_channel_readl(ch, PWM_CCNT));
  66. }
  67. /**
  68. * pwm_channel_alloc - allocate an unused PWM channel
  69. * @index: identifies the channel
  70. * @ch: structure to be initialized
  71. *
  72. * Drivers allocate PWM channels according to the board's wiring, and
  73. * matching board-specific setup code. Returns zero or negative errno.
  74. */
  75. int pwm_channel_alloc(int index, struct pwm_channel *ch)
  76. {
  77. unsigned long flags;
  78. int status = 0;
  79. /* insist on PWM init, with this signal pinned out */
  80. if (!pwm || !(pwm->mask & 1 << index))
  81. return -ENODEV;
  82. if (index < 0 || index >= PWM_NCHAN || !ch)
  83. return -EINVAL;
  84. memset(ch, 0, sizeof *ch);
  85. spin_lock_irqsave(&pwm->lock, flags);
  86. if (pwm->channel[index])
  87. status = -EBUSY;
  88. else {
  89. clk_enable(pwm->clk);
  90. ch->regs = pwmc_regs(pwm, index);
  91. ch->index = index;
  92. /* REVISIT: ap7000 seems to go 2x as fast as we expect!! */
  93. ch->mck = clk_get_rate(pwm->clk);
  94. pwm->channel[index] = ch;
  95. pwm->handler[index] = NULL;
  96. /* channel and irq are always disabled when we return */
  97. pwm_writel(pwm, PWM_DIS, 1 << index);
  98. pwm_writel(pwm, PWM_IDR, 1 << index);
  99. }
  100. spin_unlock_irqrestore(&pwm->lock, flags);
  101. return status;
  102. }
  103. EXPORT_SYMBOL(pwm_channel_alloc);
  104. static int pwmcheck(struct pwm_channel *ch)
  105. {
  106. int index;
  107. if (!pwm)
  108. return -ENODEV;
  109. if (!ch)
  110. return -EINVAL;
  111. index = ch->index;
  112. if (index < 0 || index >= PWM_NCHAN || pwm->channel[index] != ch)
  113. return -EINVAL;
  114. return index;
  115. }
  116. /**
  117. * pwm_channel_free - release a previously allocated channel
  118. * @ch: the channel being released
  119. *
  120. * The channel is completely shut down (counter and IRQ disabled),
  121. * and made available for re-use. Returns zero, or negative errno.
  122. */
  123. int pwm_channel_free(struct pwm_channel *ch)
  124. {
  125. unsigned long flags;
  126. int t;
  127. spin_lock_irqsave(&pwm->lock, flags);
  128. t = pwmcheck(ch);
  129. if (t >= 0) {
  130. pwm->channel[t] = NULL;
  131. pwm->handler[t] = NULL;
  132. /* channel and irq are always disabled when we return */
  133. pwm_writel(pwm, PWM_DIS, 1 << t);
  134. pwm_writel(pwm, PWM_IDR, 1 << t);
  135. clk_disable(pwm->clk);
  136. t = 0;
  137. }
  138. spin_unlock_irqrestore(&pwm->lock, flags);
  139. return t;
  140. }
  141. EXPORT_SYMBOL(pwm_channel_free);
  142. int __pwm_channel_onoff(struct pwm_channel *ch, int enabled)
  143. {
  144. unsigned long flags;
  145. int t;
  146. /* OMITTED FUNCTIONALITY: starting several channels in synch */
  147. spin_lock_irqsave(&pwm->lock, flags);
  148. t = pwmcheck(ch);
  149. if (t >= 0) {
  150. pwm_writel(pwm, enabled ? PWM_ENA : PWM_DIS, 1 << t);
  151. t = 0;
  152. pwm_dumpregs(ch, enabled ? "enable" : "disable");
  153. }
  154. spin_unlock_irqrestore(&pwm->lock, flags);
  155. return t;
  156. }
  157. EXPORT_SYMBOL(__pwm_channel_onoff);
  158. /**
  159. * pwm_clk_alloc - allocate and configure CLKA or CLKB
  160. * @prescale: from 0..10, the power of two used to divide MCK
  161. * @div: from 1..255, the linear divisor to use
  162. *
  163. * Returns PWM_CPR_CLKA, PWM_CPR_CLKB, or negative errno. The allocated
  164. * clock will run with a period of (2^prescale * div) / MCK, or twice as
  165. * long if center aligned PWM output is used. The clock must later be
  166. * deconfigured using pwm_clk_free().
  167. */
  168. int pwm_clk_alloc(unsigned prescale, unsigned div)
  169. {
  170. unsigned long flags;
  171. u32 mr;
  172. u32 val = (prescale << 8) | div;
  173. int ret = -EBUSY;
  174. if (prescale >= 10 || div == 0 || div > 255)
  175. return -EINVAL;
  176. spin_lock_irqsave(&pwm->lock, flags);
  177. mr = pwm_readl(pwm, PWM_MR);
  178. if ((mr & 0xffff) == 0) {
  179. mr |= val;
  180. ret = PWM_CPR_CLKA;
  181. } else if ((mr & (0xffff << 16)) == 0) {
  182. mr |= val << 16;
  183. ret = PWM_CPR_CLKB;
  184. }
  185. if (ret > 0)
  186. pwm_writel(pwm, PWM_MR, mr);
  187. spin_unlock_irqrestore(&pwm->lock, flags);
  188. return ret;
  189. }
  190. EXPORT_SYMBOL(pwm_clk_alloc);
  191. /**
  192. * pwm_clk_free - deconfigure and release CLKA or CLKB
  193. *
  194. * Reverses the effect of pwm_clk_alloc().
  195. */
  196. void pwm_clk_free(unsigned clk)
  197. {
  198. unsigned long flags;
  199. u32 mr;
  200. spin_lock_irqsave(&pwm->lock, flags);
  201. mr = pwm_readl(pwm, PWM_MR);
  202. if (clk == PWM_CPR_CLKA)
  203. pwm_writel(pwm, PWM_MR, mr & ~(0xffff << 0));
  204. if (clk == PWM_CPR_CLKB)
  205. pwm_writel(pwm, PWM_MR, mr & ~(0xffff << 16));
  206. spin_unlock_irqrestore(&pwm->lock, flags);
  207. }
  208. EXPORT_SYMBOL(pwm_clk_free);
  209. /**
  210. * pwm_channel_handler - manage channel's IRQ handler
  211. * @ch: the channel
  212. * @handler: the handler to use, possibly NULL
  213. *
  214. * If the handler is non-null, the handler will be called after every
  215. * period of this PWM channel. If the handler is null, this channel
  216. * won't generate an IRQ.
  217. */
  218. int pwm_channel_handler(struct pwm_channel *ch,
  219. void (*handler)(struct pwm_channel *ch))
  220. {
  221. unsigned long flags;
  222. int t;
  223. spin_lock_irqsave(&pwm->lock, flags);
  224. t = pwmcheck(ch);
  225. if (t >= 0) {
  226. pwm->handler[t] = handler;
  227. pwm_writel(pwm, handler ? PWM_IER : PWM_IDR, 1 << t);
  228. t = 0;
  229. }
  230. spin_unlock_irqrestore(&pwm->lock, flags);
  231. return t;
  232. }
  233. EXPORT_SYMBOL(pwm_channel_handler);
  234. static irqreturn_t pwm_irq(int id, void *_pwm)
  235. {
  236. struct pwm *p = _pwm;
  237. irqreturn_t handled = IRQ_NONE;
  238. u32 irqstat;
  239. int index;
  240. spin_lock(&p->lock);
  241. /* ack irqs, then handle them */
  242. irqstat = pwm_readl(pwm, PWM_ISR);
  243. while (irqstat) {
  244. struct pwm_channel *ch;
  245. void (*handler)(struct pwm_channel *ch);
  246. index = ffs(irqstat) - 1;
  247. irqstat &= ~(1 << index);
  248. ch = pwm->channel[index];
  249. handler = pwm->handler[index];
  250. if (handler && ch) {
  251. spin_unlock(&p->lock);
  252. handler(ch);
  253. spin_lock(&p->lock);
  254. handled = IRQ_HANDLED;
  255. }
  256. }
  257. spin_unlock(&p->lock);
  258. return handled;
  259. }
  260. static int __init pwm_probe(struct platform_device *pdev)
  261. {
  262. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  263. int irq = platform_get_irq(pdev, 0);
  264. u32 *mp = pdev->dev.platform_data;
  265. struct pwm *p;
  266. int status = -EIO;
  267. if (pwm)
  268. return -EBUSY;
  269. if (!r || irq < 0 || !mp || !*mp)
  270. return -ENODEV;
  271. if (*mp & ~((1<<PWM_NCHAN)-1)) {
  272. dev_warn(&pdev->dev, "mask 0x%x ... more than %d channels\n",
  273. *mp, PWM_NCHAN);
  274. return -EINVAL;
  275. }
  276. p = kzalloc(sizeof(*p), GFP_KERNEL);
  277. if (!p)
  278. return -ENOMEM;
  279. spin_lock_init(&p->lock);
  280. p->pdev = pdev;
  281. p->mask = *mp;
  282. p->irq = irq;
  283. p->base = ioremap(r->start, r->end - r->start + 1);
  284. if (!p->base)
  285. goto fail;
  286. p->clk = clk_get(&pdev->dev, "pwm_clk");
  287. if (IS_ERR(p->clk)) {
  288. status = PTR_ERR(p->clk);
  289. p->clk = NULL;
  290. goto fail;
  291. }
  292. status = request_irq(irq, pwm_irq, 0, pdev->name, p);
  293. if (status < 0)
  294. goto fail;
  295. pwm = p;
  296. platform_set_drvdata(pdev, p);
  297. return 0;
  298. fail:
  299. if (p->clk)
  300. clk_put(p->clk);
  301. if (p->base)
  302. iounmap(p->base);
  303. kfree(p);
  304. return status;
  305. }
  306. static int __exit pwm_remove(struct platform_device *pdev)
  307. {
  308. struct pwm *p = platform_get_drvdata(pdev);
  309. if (p != pwm)
  310. return -EINVAL;
  311. clk_enable(pwm->clk);
  312. pwm_writel(pwm, PWM_DIS, (1 << PWM_NCHAN) - 1);
  313. pwm_writel(pwm, PWM_IDR, (1 << PWM_NCHAN) - 1);
  314. clk_disable(pwm->clk);
  315. pwm = NULL;
  316. free_irq(p->irq, p);
  317. clk_put(p->clk);
  318. iounmap(p->base);
  319. kfree(p);
  320. return 0;
  321. }
  322. static struct platform_driver atmel_pwm_driver = {
  323. .driver = {
  324. .name = "atmel_pwm",
  325. .owner = THIS_MODULE,
  326. },
  327. .remove = __exit_p(pwm_remove),
  328. /* NOTE: PWM can keep running in AVR32 "idle" and "frozen" states;
  329. * and all AT91sam9263 states, albeit at reduced clock rate if
  330. * MCK becomes the slow clock (i.e. what Linux labels STR).
  331. */
  332. };
  333. static int __init pwm_init(void)
  334. {
  335. return platform_driver_probe(&atmel_pwm_driver, pwm_probe);
  336. }
  337. module_init(pwm_init);
  338. static void __exit pwm_exit(void)
  339. {
  340. platform_driver_unregister(&atmel_pwm_driver);
  341. }
  342. module_exit(pwm_exit);
  343. MODULE_DESCRIPTION("Driver for AT32/AT91 PWM module");
  344. MODULE_LICENSE("GPL");
  345. MODULE_ALIAS("platform:atmel_pwm");