aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/avr32/mach-at32ap/at32ap700x.c54
-rw-r--r--drivers/misc/Kconfig9
-rw-r--r--drivers/misc/Makefile1
-rw-r--r--drivers/misc/atmel_pwm.c409
-rw-r--r--include/asm-avr32/arch-at32ap/board.h3
-rw-r--r--include/linux/atmel_pwm.h70
6 files changed, 546 insertions, 0 deletions
diff --git a/arch/avr32/mach-at32ap/at32ap700x.c b/arch/avr32/mach-at32ap/at32ap700x.c
index 14e61f05e1f6..7678fee9a885 100644
--- a/arch/avr32/mach-at32ap/at32ap700x.c
+++ b/arch/avr32/mach-at32ap/at32ap700x.c
@@ -1186,6 +1186,59 @@ err_dup_modedb:
1186#endif 1186#endif
1187 1187
1188/* -------------------------------------------------------------------- 1188/* --------------------------------------------------------------------
1189 * PWM
1190 * -------------------------------------------------------------------- */
1191static struct resource atmel_pwm0_resource[] __initdata = {
1192 PBMEM(0xfff01400),
1193 IRQ(24),
1194};
1195static struct clk atmel_pwm0_mck = {
1196 .name = "mck",
1197 .parent = &pbb_clk,
1198 .mode = pbb_clk_mode,
1199 .get_rate = pbb_clk_get_rate,
1200 .index = 5,
1201};
1202
1203struct platform_device *__init at32_add_device_pwm(u32 mask)
1204{
1205 struct platform_device *pdev;
1206
1207 if (!mask)
1208 return NULL;
1209
1210 pdev = platform_device_alloc("atmel_pwm", 0);
1211 if (!pdev)
1212 return NULL;
1213
1214 if (platform_device_add_resources(pdev, atmel_pwm0_resource,
1215 ARRAY_SIZE(atmel_pwm0_resource)))
1216 goto out_free_pdev;
1217
1218 if (platform_device_add_data(pdev, &mask, sizeof(mask)))
1219 goto out_free_pdev;
1220
1221 if (mask & (1 << 0))
1222 select_peripheral(PA(28), PERIPH_A, 0);
1223 if (mask & (1 << 1))
1224 select_peripheral(PA(29), PERIPH_A, 0);
1225 if (mask & (1 << 2))
1226 select_peripheral(PA(21), PERIPH_B, 0);
1227 if (mask & (1 << 3))
1228 select_peripheral(PA(22), PERIPH_B, 0);
1229
1230 atmel_pwm0_mck.dev = &pdev->dev;
1231
1232 platform_device_add(pdev);
1233
1234 return pdev;
1235
1236out_free_pdev:
1237 platform_device_put(pdev);
1238 return NULL;
1239}
1240
1241/* --------------------------------------------------------------------
1189 * SSC 1242 * SSC
1190 * -------------------------------------------------------------------- */ 1243 * -------------------------------------------------------------------- */
1191static struct resource ssc0_resource[] = { 1244static struct resource ssc0_resource[] = {
@@ -1646,6 +1699,7 @@ struct clk *at32_clock_list[] = {
1646 &atmel_usart1_usart, 1699 &atmel_usart1_usart,
1647 &atmel_usart2_usart, 1700 &atmel_usart2_usart,
1648 &atmel_usart3_usart, 1701 &atmel_usart3_usart,
1702 &atmel_pwm0_mck,
1649#if defined(CONFIG_CPU_AT32AP7000) 1703#if defined(CONFIG_CPU_AT32AP7000)
1650 &macb0_hclk, 1704 &macb0_hclk,
1651 &macb0_pclk, 1705 &macb0_pclk,
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 7b5220ca7d7f..1941587a7aa2 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -13,6 +13,15 @@ menuconfig MISC_DEVICES
13 13
14if MISC_DEVICES 14if MISC_DEVICES
15 15
16config ATMEL_PWM
17 tristate "Atmel AT32/AT91 PWM support"
18 depends on AVR32 || ARCH_AT91
19 help
20 This option enables device driver support for the PWM channels
21 on certain Atmel prcoessors. Pulse Width Modulation is used for
22 purposes including software controlled power-efficent backlights
23 on LCD displays, motor control, and waveform generation.
24
16config IBM_ASM 25config IBM_ASM
17 tristate "Device driver for IBM RSA service processor" 26 tristate "Device driver for IBM RSA service processor"
18 depends on X86 && PCI && INPUT && EXPERIMENTAL 27 depends on X86 && PCI && INPUT && EXPERIMENTAL
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 7f13549cc87e..3b12f5da8562 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_HDPU_FEATURES) += hdpuftrs/
8obj-$(CONFIG_MSI_LAPTOP) += msi-laptop.o 8obj-$(CONFIG_MSI_LAPTOP) += msi-laptop.o
9obj-$(CONFIG_ACER_WMI) += acer-wmi.o 9obj-$(CONFIG_ACER_WMI) += acer-wmi.o
10obj-$(CONFIG_ASUS_LAPTOP) += asus-laptop.o 10obj-$(CONFIG_ASUS_LAPTOP) += asus-laptop.o
11obj-$(CONFIG_ATMEL_PWM) += atmel_pwm.o
11obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o 12obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o
12obj-$(CONFIG_TC1100_WMI) += tc1100-wmi.o 13obj-$(CONFIG_TC1100_WMI) += tc1100-wmi.o
13obj-$(CONFIG_LKDTM) += lkdtm.o 14obj-$(CONFIG_LKDTM) += lkdtm.o
diff --git a/drivers/misc/atmel_pwm.c b/drivers/misc/atmel_pwm.c
new file mode 100644
index 000000000000..f8d3b9a76cbd
--- /dev/null
+++ b/drivers/misc/atmel_pwm.c
@@ -0,0 +1,409 @@
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
10/*
11 * This is a simple driver for the PWM controller found in various newer
12 * Atmel SOCs, including the AVR32 series and the AT91sam9263.
13 *
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.
18 */
19#define PWM_NCHAN 4 /* max 32 */
20
21struct pwm {
22 spinlock_t lock;
23 struct platform_device *pdev;
24 u32 mask;
25 int irq;
26 void __iomem *base;
27 struct clk *clk;
28 struct pwm_channel *channel[PWM_NCHAN];
29 void (*handler[PWM_NCHAN])(struct pwm_channel *);
30};
31
32
33/* global PWM controller registers */
34#define PWM_MR 0x00
35#define PWM_ENA 0x04
36#define PWM_DIS 0x08
37#define PWM_SR 0x0c
38#define PWM_IER 0x10
39#define PWM_IDR 0x14
40#define PWM_IMR 0x18
41#define PWM_ISR 0x1c
42
43static inline void pwm_writel(const struct pwm *p, unsigned offset, u32 val)
44{
45 __raw_writel(val, p->base + offset);
46}
47
48static inline u32 pwm_readl(const struct pwm *p, unsigned offset)
49{
50 return __raw_readl(p->base + offset);
51}
52
53static inline void __iomem *pwmc_regs(const struct pwm *p, int index)
54{
55 return p->base + 0x200 + index * 0x20;
56}
57
58static struct pwm *pwm;
59
60static void pwm_dumpregs(struct pwm_channel *ch, char *tag)
61{
62 struct device *dev = &pwm->pdev->dev;
63
64 dev_dbg(dev, "%s: mr %08x, sr %08x, imr %08x\n",
65 tag,
66 pwm_readl(pwm, PWM_MR),
67 pwm_readl(pwm, PWM_SR),
68 pwm_readl(pwm, PWM_IMR));
69 dev_dbg(dev,
70 "pwm ch%d - mr %08x, dty %u, prd %u, cnt %u\n",
71 ch->index,
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));
76}
77
78
79/**
80 * pwm_channel_alloc - allocate an unused PWM channel
81 * @index: identifies the channel
82 * @ch: structure to be initialized
83 *
84 * Drivers allocate PWM channels according to the board's wiring, and
85 * matching board-specific setup code. Returns zero or negative errno.
86 */
87int pwm_channel_alloc(int index, struct pwm_channel *ch)
88{
89 unsigned long flags;
90 int status = 0;
91
92 /* insist on PWM init, with this signal pinned out */
93 if (!pwm || !(pwm->mask & 1 << index))
94 return -ENODEV;
95
96 if (index < 0 || index >= PWM_NCHAN || !ch)
97 return -EINVAL;
98 memset(ch, 0, sizeof *ch);
99
100 spin_lock_irqsave(&pwm->lock, flags);
101 if (pwm->channel[index])
102 status = -EBUSY;
103 else {
104 clk_enable(pwm->clk);
105
106 ch->regs = pwmc_regs(pwm, index);
107 ch->index = index;
108
109 /* REVISIT: ap7000 seems to go 2x as fast as we expect!! */
110 ch->mck = clk_get_rate(pwm->clk);
111
112 pwm->channel[index] = ch;
113 pwm->handler[index] = NULL;
114
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);
118 }
119 spin_unlock_irqrestore(&pwm->lock, flags);
120 return status;
121}
122EXPORT_SYMBOL(pwm_channel_alloc);
123
124static int pwmcheck(struct pwm_channel *ch)
125{
126 int index;
127
128 if (!pwm)
129 return -ENODEV;
130 if (!ch)
131 return -EINVAL;
132 index = ch->index;
133 if (index < 0 || index >= PWM_NCHAN || pwm->channel[index] != ch)
134 return -EINVAL;
135
136 return index;
137}
138
139/**
140 * pwm_channel_free - release a previously allocated channel
141 * @ch: the channel being released
142 *
143 * The channel is completely shut down (counter and IRQ disabled),
144 * and made available for re-use. Returns zero, or negative errno.
145 */
146int pwm_channel_free(struct pwm_channel *ch)
147{
148 unsigned long flags;
149 int t;
150
151 spin_lock_irqsave(&pwm->lock, flags);
152 t = pwmcheck(ch);
153 if (t >= 0) {
154 pwm->channel[t] = NULL;
155 pwm->handler[t] = NULL;
156
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);
160
161 clk_disable(pwm->clk);
162 t = 0;
163 }
164 spin_unlock_irqrestore(&pwm->lock, flags);
165 return t;
166}
167EXPORT_SYMBOL(pwm_channel_free);
168
169int __pwm_channel_onoff(struct pwm_channel *ch, int enabled)
170{
171 unsigned long flags;
172 int t;
173
174 /* OMITTED FUNCTIONALITY: starting several channels in synch */
175
176 spin_lock_irqsave(&pwm->lock, flags);
177 t = pwmcheck(ch);
178 if (t >= 0) {
179 pwm_writel(pwm, enabled ? PWM_ENA : PWM_DIS, 1 << t);
180 t = 0;
181 pwm_dumpregs(ch, enabled ? "enable" : "disable");
182 }
183 spin_unlock_irqrestore(&pwm->lock, flags);
184
185 return t;
186}
187EXPORT_SYMBOL(__pwm_channel_onoff);
188
189/**
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
193 *
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().
198 */
199int pwm_clk_alloc(unsigned prescale, unsigned div)
200{
201 unsigned long flags;
202 u32 mr;
203 u32 val = (prescale << 8) | div;
204 int ret = -EBUSY;
205
206 if (prescale >= 10 || div == 0 || div > 255)
207 return -EINVAL;
208
209 spin_lock_irqsave(&pwm->lock, flags);
210 mr = pwm_readl(pwm, PWM_MR);
211 if ((mr & 0xffff) == 0) {
212 mr |= val;
213 ret = PWM_CPR_CLKA;
214 }
215 if ((mr & (0xffff << 16)) == 0) {
216 mr |= val << 16;
217 ret = PWM_CPR_CLKB;
218 }
219 if (ret > 0)
220 pwm_writel(pwm, PWM_MR, mr);
221 spin_unlock_irqrestore(&pwm->lock, flags);
222 return ret;
223}
224EXPORT_SYMBOL(pwm_clk_alloc);
225
226/**
227 * pwm_clk_free - deconfigure and release CLKA or CLKB
228 *
229 * Reverses the effect of pwm_clk_alloc().
230 */
231void pwm_clk_free(unsigned clk)
232{
233 unsigned long flags;
234 u32 mr;
235
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);
243}
244EXPORT_SYMBOL(pwm_clk_free);
245
246/**
247 * pwm_channel_handler - manage channel's IRQ handler
248 * @ch: the channel
249 * @handler: the handler to use, possibly NULL
250 *
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.
254 */
255int pwm_channel_handler(struct pwm_channel *ch,
256 void (*handler)(struct pwm_channel *ch))
257{
258 unsigned long flags;
259 int t;
260
261 spin_lock_irqsave(&pwm->lock, flags);
262 t = pwmcheck(ch);
263 if (t >= 0) {
264 pwm->handler[t] = handler;
265 pwm_writel(pwm, handler ? PWM_IER : PWM_IDR, 1 << t);
266 t = 0;
267 }
268 spin_unlock_irqrestore(&pwm->lock, flags);
269
270 return t;
271}
272EXPORT_SYMBOL(pwm_channel_handler);
273
274static irqreturn_t pwm_irq(int id, void *_pwm)
275{
276 struct pwm *p = _pwm;
277 irqreturn_t handled = IRQ_NONE;
278 u32 irqstat;
279 int index;
280
281 spin_lock(&p->lock);
282
283 /* ack irqs, then handle them */
284 irqstat = pwm_readl(pwm, PWM_ISR);
285
286 while (irqstat) {
287 struct pwm_channel *ch;
288 void (*handler)(struct pwm_channel *ch);
289
290 index = ffs(irqstat) - 1;
291 irqstat &= ~(1 << index);
292 ch = pwm->channel[index];
293 handler = pwm->handler[index];
294 if (handler && ch) {
295 spin_unlock(&p->lock);
296 handler(ch);
297 spin_lock(&p->lock);
298 handled = IRQ_HANDLED;
299 }
300 }
301
302 spin_unlock(&p->lock);
303 return handled;
304}
305
306static int __init pwm_probe(struct platform_device *pdev)
307{
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;
311 struct pwm *p;
312 int status = -EIO;
313
314 if (pwm)
315 return -EBUSY;
316 if (!r || irq < 0 || !mp || !*mp)
317 return -ENODEV;
318 if (*mp & ~((1<<PWM_NCHAN)-1)) {
319 dev_warn(&pdev->dev, "mask 0x%x ... more than %d channels\n",
320 *mp, PWM_NCHAN);
321 return -EINVAL;
322 }
323
324 p = kzalloc(sizeof(*p), GFP_KERNEL);
325 if (!p)
326 return -ENOMEM;
327
328 spin_lock_init(&p->lock);
329 p->pdev = pdev;
330 p->mask = *mp;
331 p->irq = irq;
332 p->base = ioremap(r->start, r->end - r->start + 1);
333 if (!p->base)
334 goto fail;
335 p->clk = clk_get(&pdev->dev, "mck");
336 if (IS_ERR(p->clk)) {
337 status = PTR_ERR(p->clk);
338 p->clk = NULL;
339 goto fail;
340 }
341
342 status = request_irq(irq, pwm_irq, 0, pdev->name, p);
343 if (status < 0)
344 goto fail;
345
346 pwm = p;
347 platform_set_drvdata(pdev, p);
348
349 return 0;
350
351fail:
352 if (p->clk)
353 clk_put(p->clk);
354 if (p->base)
355 iounmap(p->base);
356
357 kfree(p);
358 return status;
359}
360
361static int __exit pwm_remove(struct platform_device *pdev)
362{
363 struct pwm *p = platform_get_drvdata(pdev);
364
365 if (p != pwm)
366 return -EINVAL;
367
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);
372
373 pwm = NULL;
374
375 free_irq(p->irq, p);
376 clk_put(p->clk);
377 iounmap(p->base);
378 kfree(p);
379
380 return 0;
381}
382
383static struct platform_driver atmel_pwm_driver = {
384 .driver = {
385 .name = "atmel_pwm",
386 .owner = THIS_MODULE,
387 },
388 .remove = __exit_p(pwm_remove),
389
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).
393 */
394};
395
396static int __init pwm_init(void)
397{
398 return platform_driver_probe(&atmel_pwm_driver, pwm_probe);
399}
400module_init(pwm_init);
401
402static void __exit pwm_exit(void)
403{
404 platform_driver_unregister(&atmel_pwm_driver);
405}
406module_exit(pwm_exit);
407
408MODULE_DESCRIPTION("Driver for AT32/AT91 PWM module");
409MODULE_LICENSE("GPL");
diff --git a/include/asm-avr32/arch-at32ap/board.h b/include/asm-avr32/arch-at32ap/board.h
index d6993a6b6473..7597b0bd2f01 100644
--- a/include/asm-avr32/arch-at32ap/board.h
+++ b/include/asm-avr32/arch-at32ap/board.h
@@ -51,6 +51,9 @@ struct platform_device *
51at32_add_device_ide(unsigned int id, unsigned int extint, 51at32_add_device_ide(unsigned int id, unsigned int extint,
52 struct ide_platform_data *data); 52 struct ide_platform_data *data);
53 53
54/* mask says which PWM channels to mux */
55struct platform_device *at32_add_device_pwm(u32 mask);
56
54/* depending on what's hooked up, not all SSC pins will be used */ 57/* depending on what's hooked up, not all SSC pins will be used */
55#define ATMEL_SSC_TK 0x01 58#define ATMEL_SSC_TK 0x01
56#define ATMEL_SSC_TF 0x02 59#define ATMEL_SSC_TF 0x02
diff --git a/include/linux/atmel_pwm.h b/include/linux/atmel_pwm.h
new file mode 100644
index 000000000000..ea04abb3db8e
--- /dev/null
+++ b/include/linux/atmel_pwm.h
@@ -0,0 +1,70 @@
1#ifndef __LINUX_ATMEL_PWM_H
2#define __LINUX_ATMEL_PWM_H
3
4/**
5 * struct pwm_channel - driver handle to a PWM channel
6 * @regs: base of this channel's registers
7 * @index: number of this channel (0..31)
8 * @mck: base clock rate, which can be prescaled and maybe subdivided
9 *
10 * Drivers initialize a pwm_channel structure using pwm_channel_alloc().
11 * Then they configure its clock rate (derived from MCK), alignment,
12 * polarity, and duty cycle by writing directly to the channel registers,
13 * before enabling the channel by calling pwm_channel_enable().
14 *
15 * After emitting a PWM signal for the desired length of time, drivers
16 * may then pwm_channel_disable() or pwm_channel_free(). Both of these
17 * disable the channel, but when it's freed the IRQ is deconfigured and
18 * the channel must later be re-allocated and reconfigured.
19 *
20 * Note that if the period or duty cycle need to be changed while the
21 * PWM channel is operating, drivers must use the PWM_CUPD double buffer
22 * mechanism, either polling until they change or getting implicitly
23 * notified through a once-per-period interrupt handler.
24 */
25struct pwm_channel {
26 void __iomem *regs;
27 unsigned index;
28 unsigned long mck;
29};
30
31extern int pwm_channel_alloc(int index, struct pwm_channel *ch);
32extern int pwm_channel_free(struct pwm_channel *ch);
33
34extern int pwm_clk_alloc(unsigned prescale, unsigned div);
35extern void pwm_clk_free(unsigned clk);
36
37extern int __pwm_channel_onoff(struct pwm_channel *ch, int enabled);
38
39#define pwm_channel_enable(ch) __pwm_channel_onoff((ch), 1)
40#define pwm_channel_disable(ch) __pwm_channel_onoff((ch), 0)
41
42/* periodic interrupts, mostly for CUPD changes to period or cycle */
43extern int pwm_channel_handler(struct pwm_channel *ch,
44 void (*handler)(struct pwm_channel *ch));
45
46/* per-channel registers (banked at pwm_channel->regs) */
47#define PWM_CMR 0x00 /* mode register */
48#define PWM_CPR_CPD (1 << 10) /* set: CUPD modifies period */
49#define PWM_CPR_CPOL (1 << 9) /* set: idle high */
50#define PWM_CPR_CALG (1 << 8) /* set: center align */
51#define PWM_CPR_CPRE (0xf << 0) /* mask: rate is mck/(2^pre) */
52#define PWM_CPR_CLKA (0xb << 0) /* rate CLKA */
53#define PWM_CPR_CLKB (0xc << 0) /* rate CLKB */
54#define PWM_CDTY 0x04 /* duty cycle (max of CPRD) */
55#define PWM_CPRD 0x08 /* period (count up from zero) */
56#define PWM_CCNT 0x0c /* counter (20 bits?) */
57#define PWM_CUPD 0x10 /* update CPRD (or CDTY) next period */
58
59static inline void
60pwm_channel_writel(struct pwm_channel *pwmc, unsigned offset, u32 val)
61{
62 __raw_writel(val, pwmc->regs + offset);
63}
64
65static inline u32 pwm_channel_readl(struct pwm_channel *pwmc, unsigned offset)
66{
67 return __raw_readl(pwmc->regs + offset);
68}
69
70#endif /* __LINUX_ATMEL_PWM_H */