atmel_pwm.c 9.1 KB

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