1 #include <linux/module.h>
5 #include <linux/interrupt.h>
6 #include <linux/platform_device.h>
7 #include <linux/atmel_pwm.h>
11 * This is a simple driver for the PWM controller found in various newer
12 * Atmel SOCs, including the AVR32 series and the AT91sam9263.
14 * Chips with current Linux ports have only 4 PWM channels, out of max 32.
15 * AT32UC3A and AT32UC3B chips have 7 channels (but currently no Linux).
16 * Docs are inconsistent about the width of the channel counter registers;
17 * it's at least 16 bits, but several places say 20 bits.
19 #define PWM_NCHAN 4 /* max 32 */
23 struct platform_device
*pdev
;
28 struct pwm_channel
*channel
[PWM_NCHAN
];
29 void (*handler
[PWM_NCHAN
])(struct pwm_channel
*);
33 /* global PWM controller registers */
43 static inline void pwm_writel(const struct pwm
*p
, unsigned offset
, u32 val
)
45 __raw_writel(val
, p
->base
+ offset
);
48 static inline u32
pwm_readl(const struct pwm
*p
, unsigned offset
)
50 return __raw_readl(p
->base
+ offset
);
53 static inline void __iomem
*pwmc_regs(const struct pwm
*p
, int index
)
55 return p
->base
+ 0x200 + index
* 0x20;
58 static struct pwm
*pwm
;
60 static void pwm_dumpregs(struct pwm_channel
*ch
, char *tag
)
62 struct device
*dev
= &pwm
->pdev
->dev
;
64 dev_dbg(dev
, "%s: mr %08x, sr %08x, imr %08x\n",
66 pwm_readl(pwm
, PWM_MR
),
67 pwm_readl(pwm
, PWM_SR
),
68 pwm_readl(pwm
, PWM_IMR
));
70 "pwm ch%d - mr %08x, dty %u, prd %u, cnt %u\n",
72 pwm_channel_readl(ch
, PWM_CMR
),
73 pwm_channel_readl(ch
, PWM_CDTY
),
74 pwm_channel_readl(ch
, PWM_CPRD
),
75 pwm_channel_readl(ch
, PWM_CCNT
));
80 * pwm_channel_alloc - allocate an unused PWM channel
81 * @index: identifies the channel
82 * @ch: structure to be initialized
84 * Drivers allocate PWM channels according to the board's wiring, and
85 * matching board-specific setup code. Returns zero or negative errno.
87 int pwm_channel_alloc(int index
, struct pwm_channel
*ch
)
92 /* insist on PWM init, with this signal pinned out */
93 if (!pwm
|| !(pwm
->mask
& 1 << index
))
96 if (index
< 0 || index
>= PWM_NCHAN
|| !ch
)
98 memset(ch
, 0, sizeof *ch
);
100 spin_lock_irqsave(&pwm
->lock
, flags
);
101 if (pwm
->channel
[index
])
104 clk_enable(pwm
->clk
);
106 ch
->regs
= pwmc_regs(pwm
, index
);
109 /* REVISIT: ap7000 seems to go 2x as fast as we expect!! */
110 ch
->mck
= clk_get_rate(pwm
->clk
);
112 pwm
->channel
[index
] = ch
;
113 pwm
->handler
[index
] = NULL
;
115 /* channel and irq are always disabled when we return */
116 pwm_writel(pwm
, PWM_DIS
, 1 << index
);
117 pwm_writel(pwm
, PWM_IDR
, 1 << index
);
119 spin_unlock_irqrestore(&pwm
->lock
, flags
);
122 EXPORT_SYMBOL(pwm_channel_alloc
);
124 static int pwmcheck(struct pwm_channel
*ch
)
133 if (index
< 0 || index
>= PWM_NCHAN
|| pwm
->channel
[index
] != ch
)
140 * pwm_channel_free - release a previously allocated channel
141 * @ch: the channel being released
143 * The channel is completely shut down (counter and IRQ disabled),
144 * and made available for re-use. Returns zero, or negative errno.
146 int pwm_channel_free(struct pwm_channel
*ch
)
151 spin_lock_irqsave(&pwm
->lock
, flags
);
154 pwm
->channel
[t
] = NULL
;
155 pwm
->handler
[t
] = NULL
;
157 /* channel and irq are always disabled when we return */
158 pwm_writel(pwm
, PWM_DIS
, 1 << t
);
159 pwm_writel(pwm
, PWM_IDR
, 1 << t
);
161 clk_disable(pwm
->clk
);
164 spin_unlock_irqrestore(&pwm
->lock
, flags
);
167 EXPORT_SYMBOL(pwm_channel_free
);
169 int __pwm_channel_onoff(struct pwm_channel
*ch
, int enabled
)
174 /* OMITTED FUNCTIONALITY: starting several channels in synch */
176 spin_lock_irqsave(&pwm
->lock
, flags
);
179 pwm_writel(pwm
, enabled
? PWM_ENA
: PWM_DIS
, 1 << t
);
181 pwm_dumpregs(ch
, enabled
? "enable" : "disable");
183 spin_unlock_irqrestore(&pwm
->lock
, flags
);
187 EXPORT_SYMBOL(__pwm_channel_onoff
);
190 * pwm_clk_alloc - allocate and configure CLKA or CLKB
191 * @prescale: from 0..10, the power of two used to divide MCK
192 * @div: from 1..255, the linear divisor to use
194 * Returns PWM_CPR_CLKA, PWM_CPR_CLKB, or negative errno. The allocated
195 * clock will run with a period of (2^prescale * div) / MCK, or twice as
196 * long if center aligned PWM output is used. The clock must later be
197 * deconfigured using pwm_clk_free().
199 int pwm_clk_alloc(unsigned prescale
, unsigned div
)
203 u32 val
= (prescale
<< 8) | div
;
206 if (prescale
>= 10 || div
== 0 || div
> 255)
209 spin_lock_irqsave(&pwm
->lock
, flags
);
210 mr
= pwm_readl(pwm
, PWM_MR
);
211 if ((mr
& 0xffff) == 0) {
215 if ((mr
& (0xffff << 16)) == 0) {
220 pwm_writel(pwm
, PWM_MR
, mr
);
221 spin_unlock_irqrestore(&pwm
->lock
, flags
);
224 EXPORT_SYMBOL(pwm_clk_alloc
);
227 * pwm_clk_free - deconfigure and release CLKA or CLKB
229 * Reverses the effect of pwm_clk_alloc().
231 void pwm_clk_free(unsigned clk
)
236 spin_lock_irqsave(&pwm
->lock
, flags
);
237 mr
= pwm_readl(pwm
, PWM_MR
);
238 if (clk
== PWM_CPR_CLKA
)
239 pwm_writel(pwm
, PWM_MR
, mr
& ~(0xffff << 0));
240 if (clk
== PWM_CPR_CLKB
)
241 pwm_writel(pwm
, PWM_MR
, mr
& ~(0xffff << 16));
242 spin_unlock_irqrestore(&pwm
->lock
, flags
);
244 EXPORT_SYMBOL(pwm_clk_free
);
247 * pwm_channel_handler - manage channel's IRQ handler
249 * @handler: the handler to use, possibly NULL
251 * If the handler is non-null, the handler will be called after every
252 * period of this PWM channel. If the handler is null, this channel
253 * won't generate an IRQ.
255 int pwm_channel_handler(struct pwm_channel
*ch
,
256 void (*handler
)(struct pwm_channel
*ch
))
261 spin_lock_irqsave(&pwm
->lock
, flags
);
264 pwm
->handler
[t
] = handler
;
265 pwm_writel(pwm
, handler
? PWM_IER
: PWM_IDR
, 1 << t
);
268 spin_unlock_irqrestore(&pwm
->lock
, flags
);
272 EXPORT_SYMBOL(pwm_channel_handler
);
274 static irqreturn_t
pwm_irq(int id
, void *_pwm
)
276 struct pwm
*p
= _pwm
;
277 irqreturn_t handled
= IRQ_NONE
;
283 /* ack irqs, then handle them */
284 irqstat
= pwm_readl(pwm
, PWM_ISR
);
287 struct pwm_channel
*ch
;
288 void (*handler
)(struct pwm_channel
*ch
);
290 index
= ffs(irqstat
) - 1;
291 irqstat
&= ~(1 << index
);
292 ch
= pwm
->channel
[index
];
293 handler
= pwm
->handler
[index
];
295 spin_unlock(&p
->lock
);
298 handled
= IRQ_HANDLED
;
302 spin_unlock(&p
->lock
);
306 static int __init
pwm_probe(struct platform_device
*pdev
)
308 struct resource
*r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
309 int irq
= platform_get_irq(pdev
, 0);
310 u32
*mp
= pdev
->dev
.platform_data
;
316 if (!r
|| irq
< 0 || !mp
|| !*mp
)
318 if (*mp
& ~((1<<PWM_NCHAN
)-1)) {
319 dev_warn(&pdev
->dev
, "mask 0x%x ... more than %d channels\n",
324 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
328 spin_lock_init(&p
->lock
);
332 p
->base
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
335 p
->clk
= clk_get(&pdev
->dev
, "mck");
336 if (IS_ERR(p
->clk
)) {
337 status
= PTR_ERR(p
->clk
);
342 status
= request_irq(irq
, pwm_irq
, 0, pdev
->name
, p
);
347 platform_set_drvdata(pdev
, p
);
361 static int __exit
pwm_remove(struct platform_device
*pdev
)
363 struct pwm
*p
= platform_get_drvdata(pdev
);
368 clk_enable(pwm
->clk
);
369 pwm_writel(pwm
, PWM_DIS
, (1 << PWM_NCHAN
) - 1);
370 pwm_writel(pwm
, PWM_IDR
, (1 << PWM_NCHAN
) - 1);
371 clk_disable(pwm
->clk
);
383 static struct platform_driver atmel_pwm_driver
= {
386 .owner
= THIS_MODULE
,
388 .remove
= __exit_p(pwm_remove
),
390 /* NOTE: PWM can keep running in AVR32 "idle" and "frozen" states;
391 * and all AT91sam9263 states, albeit at reduced clock rate if
392 * MCK becomes the slow clock (i.e. what Linux labels STR).
396 static int __init
pwm_init(void)
398 return platform_driver_probe(&atmel_pwm_driver
, pwm_probe
);
400 module_init(pwm_init
);
402 static void __exit
pwm_exit(void)
404 platform_driver_unregister(&atmel_pwm_driver
);
406 module_exit(pwm_exit
);
408 MODULE_DESCRIPTION("Driver for AT32/AT91 PWM module");
409 MODULE_LICENSE("GPL");