atmel_pwm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. if (!pwm)
  80. return -EPROBE_DEFER;
  81. if (!(pwm->mask & 1 << index))
  82. return -ENODEV;
  83. if (index < 0 || index >= PWM_NCHAN || !ch)
  84. return -EINVAL;
  85. memset(ch, 0, sizeof *ch);
  86. spin_lock_irqsave(&pwm->lock, flags);
  87. if (pwm->channel[index])
  88. status = -EBUSY;
  89. else {
  90. clk_enable(pwm->clk);
  91. ch->regs = pwmc_regs(pwm, index);
  92. ch->index = index;
  93. /* REVISIT: ap7000 seems to go 2x as fast as we expect!! */
  94. ch->mck = clk_get_rate(pwm->clk);
  95. pwm->channel[index] = ch;
  96. pwm->handler[index] = NULL;
  97. /* channel and irq are always disabled when we return */
  98. pwm_writel(pwm, PWM_DIS, 1 << index);
  99. pwm_writel(pwm, PWM_IDR, 1 << index);
  100. }
  101. spin_unlock_irqrestore(&pwm->lock, flags);
  102. return status;
  103. }
  104. EXPORT_SYMBOL(pwm_channel_alloc);
  105. static int pwmcheck(struct pwm_channel *ch)
  106. {
  107. int index;
  108. if (!pwm)
  109. return -ENODEV;
  110. if (!ch)
  111. return -EINVAL;
  112. index = ch->index;
  113. if (index < 0 || index >= PWM_NCHAN || pwm->channel[index] != ch)
  114. return -EINVAL;
  115. return index;
  116. }
  117. /**
  118. * pwm_channel_free - release a previously allocated channel
  119. * @ch: the channel being released
  120. *
  121. * The channel is completely shut down (counter and IRQ disabled),
  122. * and made available for re-use. Returns zero, or negative errno.
  123. */
  124. int pwm_channel_free(struct pwm_channel *ch)
  125. {
  126. unsigned long flags;
  127. int t;
  128. spin_lock_irqsave(&pwm->lock, flags);
  129. t = pwmcheck(ch);
  130. if (t >= 0) {
  131. pwm->channel[t] = NULL;
  132. pwm->handler[t] = NULL;
  133. /* channel and irq are always disabled when we return */
  134. pwm_writel(pwm, PWM_DIS, 1 << t);
  135. pwm_writel(pwm, PWM_IDR, 1 << t);
  136. clk_disable(pwm->clk);
  137. t = 0;
  138. }
  139. spin_unlock_irqrestore(&pwm->lock, flags);
  140. return t;
  141. }
  142. EXPORT_SYMBOL(pwm_channel_free);
  143. int __pwm_channel_onoff(struct pwm_channel *ch, int enabled)
  144. {
  145. unsigned long flags;
  146. int t;
  147. /* OMITTED FUNCTIONALITY: starting several channels in synch */
  148. spin_lock_irqsave(&pwm->lock, flags);
  149. t = pwmcheck(ch);
  150. if (t >= 0) {
  151. pwm_writel(pwm, enabled ? PWM_ENA : PWM_DIS, 1 << t);
  152. t = 0;
  153. pwm_dumpregs(ch, enabled ? "enable" : "disable");
  154. }
  155. spin_unlock_irqrestore(&pwm->lock, flags);
  156. return t;
  157. }
  158. EXPORT_SYMBOL(__pwm_channel_onoff);
  159. /**
  160. * pwm_clk_alloc - allocate and configure CLKA or CLKB
  161. * @prescale: from 0..10, the power of two used to divide MCK
  162. * @div: from 1..255, the linear divisor to use
  163. *
  164. * Returns PWM_CPR_CLKA, PWM_CPR_CLKB, or negative errno. The allocated
  165. * clock will run with a period of (2^prescale * div) / MCK, or twice as
  166. * long if center aligned PWM output is used. The clock must later be
  167. * deconfigured using pwm_clk_free().
  168. */
  169. int pwm_clk_alloc(unsigned prescale, unsigned div)
  170. {
  171. unsigned long flags;
  172. u32 mr;
  173. u32 val = (prescale << 8) | div;
  174. int ret = -EBUSY;
  175. if (prescale >= 10 || div == 0 || div > 255)
  176. return -EINVAL;
  177. spin_lock_irqsave(&pwm->lock, flags);
  178. mr = pwm_readl(pwm, PWM_MR);
  179. if ((mr & 0xffff) == 0) {
  180. mr |= val;
  181. ret = PWM_CPR_CLKA;
  182. } else if ((mr & (0xffff << 16)) == 0) {
  183. mr |= val << 16;
  184. ret = PWM_CPR_CLKB;
  185. }
  186. if (ret > 0)
  187. pwm_writel(pwm, PWM_MR, mr);
  188. spin_unlock_irqrestore(&pwm->lock, flags);
  189. return ret;
  190. }
  191. EXPORT_SYMBOL(pwm_clk_alloc);
  192. /**
  193. * pwm_clk_free - deconfigure and release CLKA or CLKB
  194. *
  195. * Reverses the effect of pwm_clk_alloc().
  196. */
  197. void pwm_clk_free(unsigned clk)
  198. {
  199. unsigned long flags;
  200. u32 mr;
  201. spin_lock_irqsave(&pwm->lock, flags);
  202. mr = pwm_readl(pwm, PWM_MR);
  203. if (clk == PWM_CPR_CLKA)
  204. pwm_writel(pwm, PWM_MR, mr & ~(0xffff << 0));
  205. if (clk == PWM_CPR_CLKB)
  206. pwm_writel(pwm, PWM_MR, mr & ~(0xffff << 16));
  207. spin_unlock_irqrestore(&pwm->lock, flags);
  208. }
  209. EXPORT_SYMBOL(pwm_clk_free);
  210. /**
  211. * pwm_channel_handler - manage channel's IRQ handler
  212. * @ch: the channel
  213. * @handler: the handler to use, possibly NULL
  214. *
  215. * If the handler is non-null, the handler will be called after every
  216. * period of this PWM channel. If the handler is null, this channel
  217. * won't generate an IRQ.
  218. */
  219. int pwm_channel_handler(struct pwm_channel *ch,
  220. void (*handler)(struct pwm_channel *ch))
  221. {
  222. unsigned long flags;
  223. int t;
  224. spin_lock_irqsave(&pwm->lock, flags);
  225. t = pwmcheck(ch);
  226. if (t >= 0) {
  227. pwm->handler[t] = handler;
  228. pwm_writel(pwm, handler ? PWM_IER : PWM_IDR, 1 << t);
  229. t = 0;
  230. }
  231. spin_unlock_irqrestore(&pwm->lock, flags);
  232. return t;
  233. }
  234. EXPORT_SYMBOL(pwm_channel_handler);
  235. static irqreturn_t pwm_irq(int id, void *_pwm)
  236. {
  237. struct pwm *p = _pwm;
  238. irqreturn_t handled = IRQ_NONE;
  239. u32 irqstat;
  240. int index;
  241. spin_lock(&p->lock);
  242. /* ack irqs, then handle them */
  243. irqstat = pwm_readl(pwm, PWM_ISR);
  244. while (irqstat) {
  245. struct pwm_channel *ch;
  246. void (*handler)(struct pwm_channel *ch);
  247. index = ffs(irqstat) - 1;
  248. irqstat &= ~(1 << index);
  249. ch = pwm->channel[index];
  250. handler = pwm->handler[index];
  251. if (handler && ch) {
  252. spin_unlock(&p->lock);
  253. handler(ch);
  254. spin_lock(&p->lock);
  255. handled = IRQ_HANDLED;
  256. }
  257. }
  258. spin_unlock(&p->lock);
  259. return handled;
  260. }
  261. static int __init pwm_probe(struct platform_device *pdev)
  262. {
  263. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  264. int irq = platform_get_irq(pdev, 0);
  265. u32 *mp = pdev->dev.platform_data;
  266. struct pwm *p;
  267. int status = -EIO;
  268. if (pwm)
  269. return -EBUSY;
  270. if (!r || irq < 0 || !mp || !*mp)
  271. return -ENODEV;
  272. if (*mp & ~((1<<PWM_NCHAN)-1)) {
  273. dev_warn(&pdev->dev, "mask 0x%x ... more than %d channels\n",
  274. *mp, PWM_NCHAN);
  275. return -EINVAL;
  276. }
  277. p = kzalloc(sizeof(*p), GFP_KERNEL);
  278. if (!p)
  279. return -ENOMEM;
  280. spin_lock_init(&p->lock);
  281. p->pdev = pdev;
  282. p->mask = *mp;
  283. p->irq = irq;
  284. p->base = ioremap(r->start, resource_size(r));
  285. if (!p->base)
  286. goto fail;
  287. p->clk = clk_get(&pdev->dev, "pwm_clk");
  288. if (IS_ERR(p->clk)) {
  289. status = PTR_ERR(p->clk);
  290. p->clk = NULL;
  291. goto fail;
  292. }
  293. status = request_irq(irq, pwm_irq, 0, pdev->name, p);
  294. if (status < 0)
  295. goto fail;
  296. pwm = p;
  297. platform_set_drvdata(pdev, p);
  298. return 0;
  299. fail:
  300. if (p->clk)
  301. clk_put(p->clk);
  302. if (p->base)
  303. iounmap(p->base);
  304. kfree(p);
  305. return status;
  306. }
  307. static int __exit pwm_remove(struct platform_device *pdev)
  308. {
  309. struct pwm *p = platform_get_drvdata(pdev);
  310. if (p != pwm)
  311. return -EINVAL;
  312. clk_enable(pwm->clk);
  313. pwm_writel(pwm, PWM_DIS, (1 << PWM_NCHAN) - 1);
  314. pwm_writel(pwm, PWM_IDR, (1 << PWM_NCHAN) - 1);
  315. clk_disable(pwm->clk);
  316. pwm = NULL;
  317. free_irq(p->irq, p);
  318. clk_put(p->clk);
  319. iounmap(p->base);
  320. kfree(p);
  321. return 0;
  322. }
  323. static struct platform_driver atmel_pwm_driver = {
  324. .driver = {
  325. .name = "atmel_pwm",
  326. .owner = THIS_MODULE,
  327. },
  328. .remove = __exit_p(pwm_remove),
  329. /* NOTE: PWM can keep running in AVR32 "idle" and "frozen" states;
  330. * and all AT91sam9263 states, albeit at reduced clock rate if
  331. * MCK becomes the slow clock (i.e. what Linux labels STR).
  332. */
  333. };
  334. module_platform_driver_probe(atmel_pwm_driver, pwm_probe);
  335. MODULE_DESCRIPTION("Driver for AT32/AT91 PWM module");
  336. MODULE_LICENSE("GPL");
  337. MODULE_ALIAS("platform:atmel_pwm");