LKML Archive on lore.kernel.org help / color / mirror / Atom feed
* [PATCH -mm 0/2] AVR32 PWM driver and example user @ 2008-01-24 14:33 Haavard Skinnemoen 2008-01-24 14:33 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 Haavard Skinnemoen 0 siblings, 1 reply; 10+ messages in thread From: Haavard Skinnemoen @ 2008-01-24 14:33 UTC (permalink / raw) To: Andrew Morton Cc: David Brownell, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel Two patches from David Brownell follow. The first one implements a low-level driver/library for the PWM controller integrated on some newer AVR32- and ARM-based chips from Atmel. The second one uses this library to implement a LED driver with variable brightness. There's currently no user of this LED driver, but it can be tested on the ATSTK1000 by following the instructions here: http://avr32linux.org/twiki/bin/view/Main/AtmelPwmDriver Also, PWM functionality in the kernel has been requested by several AVR32 users with custom boards (which we hopefully will see support for in mainline later), and some AT91-based boards may be able to use the PWM LED driver by default given some suitable platform- and board code (David mentioned AT91SAM9263EK.) I'd appreciate some feedback from AT91 people about this. David Brownell (2): Basic PWM driver for AVR32 and AT91 PWM LED driver arch/avr32/mach-at32ap/at32ap700x.c | 54 +++++ drivers/leds/Kconfig | 7 + drivers/leds/Makefile | 1 + drivers/leds/leds-atmel-pwm.c | 157 +++++++++++++ drivers/misc/Kconfig | 9 + drivers/misc/Makefile | 1 + drivers/misc/atmel_pwm.c | 409 +++++++++++++++++++++++++++++++++ include/asm-avr32/arch-at32ap/board.h | 3 + include/linux/atmel_pwm.h | 70 ++++++ 9 files changed, 711 insertions(+), 0 deletions(-) create mode 100644 drivers/leds/leds-atmel-pwm.c create mode 100644 drivers/misc/atmel_pwm.c create mode 100644 include/linux/atmel_pwm.h ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 2008-01-24 14:33 [PATCH -mm 0/2] AVR32 PWM driver and example user Haavard Skinnemoen @ 2008-01-24 14:33 ` Haavard Skinnemoen 2008-01-24 14:33 ` [PATCH -mm 2/2] PWM LED driver Haavard Skinnemoen 2008-01-24 20:53 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 David Brownell 0 siblings, 2 replies; 10+ messages in thread From: Haavard Skinnemoen @ 2008-01-24 14:33 UTC (permalink / raw) To: Andrew Morton Cc: David Brownell, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel, David Brownell, Haavard Skinnemoen From: David Brownell <david-b@pacbell.net> PWM device setup, and a simple PWM driver exposing a programming interface giving access to each channel's full capabilities. Note that this doesn't support starting several channels in synch. [hskinnemoen@atmel.com: allocate platform device dynamically] Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com> --- arch/avr32/mach-at32ap/at32ap700x.c | 54 ++++ drivers/misc/Kconfig | 9 drivers/misc/Makefile | 1 drivers/misc/atmel_pwm.c | 409 ++++++++++++++++++++++++++++++++++ include/asm-avr32/arch-at32ap/board.h | 3 include/linux/atmel_pwm.h | 70 +++++ 6 files changed, 546 insertions(+) create mode 100644 drivers/misc/atmel_pwm.c create mode 100644 include/linux/atmel_pwm.h Index: linux-2.6.24-rc8-mm1/arch/avr32/mach-at32ap/at32ap700x.c =================================================================== --- linux-2.6.24-rc8-mm1.orig/arch/avr32/mach-at32ap/at32ap700x.c 2008-01-24 11:07:07.000000000 +0100 +++ linux-2.6.24-rc8-mm1/arch/avr32/mach-at32ap/at32ap700x.c 2008-01-24 15:15:50.000000000 +0100 @@ -1185,6 +1185,59 @@ err_dup_modedb: #endif /* -------------------------------------------------------------------- + * PWM + * -------------------------------------------------------------------- */ +static struct resource atmel_pwm0_resource[] __initdata = { + PBMEM(0xfff01400), + IRQ(24), +}; +static struct clk atmel_pwm0_mck = { + .name = "mck", + .parent = &pbb_clk, + .mode = pbb_clk_mode, + .get_rate = pbb_clk_get_rate, + .index = 5, +}; + +struct platform_device *__init at32_add_device_pwm(u32 mask) +{ + struct platform_device *pdev; + + if (!mask) + return NULL; + + pdev = platform_device_alloc("atmel_pwm", 0); + if (!pdev) + return NULL; + + if (platform_device_add_resources(pdev, atmel_pwm0_resource, + ARRAY_SIZE(atmel_pwm0_resource))) + goto out_free_pdev; + + if (platform_device_add_data(pdev, &mask, sizeof(mask))) + goto out_free_pdev; + + if (mask & (1 << 0)) + select_peripheral(PA(28), PERIPH_A, 0); + if (mask & (1 << 1)) + select_peripheral(PA(29), PERIPH_A, 0); + if (mask & (1 << 2)) + select_peripheral(PA(21), PERIPH_B, 0); + if (mask & (1 << 3)) + select_peripheral(PA(22), PERIPH_B, 0); + + atmel_pwm0_mck.dev = &pdev->dev; + + platform_device_add(pdev); + + return pdev; + +out_free_pdev: + platform_device_put(pdev); + return NULL; +} + +/* -------------------------------------------------------------------- * SSC * -------------------------------------------------------------------- */ static struct resource ssc0_resource[] = { @@ -1645,6 +1698,7 @@ struct clk *at32_clock_list[] = { &atmel_usart1_usart, &atmel_usart2_usart, &atmel_usart3_usart, + &atmel_pwm0_mck, #if defined(CONFIG_CPU_AT32AP7000) &macb0_hclk, &macb0_pclk, Index: linux-2.6.24-rc8-mm1/drivers/misc/Kconfig =================================================================== --- linux-2.6.24-rc8-mm1.orig/drivers/misc/Kconfig 2008-01-24 11:07:14.000000000 +0100 +++ linux-2.6.24-rc8-mm1/drivers/misc/Kconfig 2008-01-24 15:15:50.000000000 +0100 @@ -13,6 +13,15 @@ menuconfig MISC_DEVICES if MISC_DEVICES +config ATMEL_PWM + tristate "Atmel AT32/AT91 PWM support" + depends on (AVR32 || AT91) && EXPERIMENTAL + help + This option enables device driver support for the PWM channels + on certain Atmel prcoessors. Pulse Width Modulation is used for + purposes including software controlled power-efficent backlights + on LCD displays, motor control, and waveform generation. + config IBM_ASM tristate "Device driver for IBM RSA service processor" depends on X86 && PCI && INPUT && EXPERIMENTAL Index: linux-2.6.24-rc8-mm1/drivers/misc/Makefile =================================================================== --- linux-2.6.24-rc8-mm1.orig/drivers/misc/Makefile 2008-01-24 11:07:14.000000000 +0100 +++ linux-2.6.24-rc8-mm1/drivers/misc/Makefile 2008-01-24 15:16:23.000000000 +0100 @@ -7,6 +7,7 @@ obj-$(CONFIG_IBM_ASM) += ibmasm/ obj-$(CONFIG_HDPU_FEATURES) += hdpuftrs/ obj-$(CONFIG_MSI_LAPTOP) += msi-laptop.o obj-$(CONFIG_ASUS_LAPTOP) += asus-laptop.o +obj-$(CONFIG_ATMEL_PWM) += atmel_pwm.o obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o obj-$(CONFIG_LKDTM) += lkdtm.o obj-$(CONFIG_TIFM_CORE) += tifm_core.o Index: linux-2.6.24-rc8-mm1/drivers/misc/atmel_pwm.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.24-rc8-mm1/drivers/misc/atmel_pwm.c 2008-01-24 15:15:50.000000000 +0100 @@ -0,0 +1,409 @@ +#include <linux/module.h> +#include <linux/clk.h> +#include <linux/err.h> +#include <linux/io.h> +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/atmel_pwm.h> + + +/* + * This is a simple driver for the PWM controller found in various newer + * Atmel SOCs, including the AVR32 series and the AT91sam9263. + * + * Chips with current Linux ports have only 4 PWM channels, out of max 32. + * AT32UC3A and AT32UC3B chips have 7 channels (but currently no Linux). + * Docs are inconsistent about the width of the channel counter registers; + * it's at least 16 bits, but several places say 20 bits. + */ +#define PWM_NCHAN 4 /* max 32 */ + +struct pwm { + spinlock_t lock; + struct platform_device *pdev; + u32 mask; + int irq; + void __iomem *base; + struct clk *clk; + struct pwm_channel *channel[PWM_NCHAN]; + void (*handler[PWM_NCHAN])(struct pwm_channel *); +}; + + +/* global PWM controller registers */ +#define PWM_MR 0x00 +#define PWM_ENA 0x04 +#define PWM_DIS 0x08 +#define PWM_SR 0x0c +#define PWM_IER 0x10 +#define PWM_IDR 0x14 +#define PWM_IMR 0x18 +#define PWM_ISR 0x1c + +static inline void pwm_writel(const struct pwm *p, unsigned offset, u32 val) +{ + __raw_writel(val, p->base + offset); +} + +static inline u32 pwm_readl(const struct pwm *p, unsigned offset) +{ + return __raw_readl(p->base + offset); +} + +static inline void __iomem *pwmc_regs(const struct pwm *p, int index) +{ + return p->base + 0x200 + index * 0x20; +} + +static struct pwm *pwm; + +static void pwm_dumpregs(struct pwm_channel *ch, char *tag) +{ + struct device *dev = &pwm->pdev->dev; + + dev_dbg(dev, "%s: mr %08x, sr %08x, imr %08x\n", + tag, + pwm_readl(pwm, PWM_MR), + pwm_readl(pwm, PWM_SR), + pwm_readl(pwm, PWM_IMR)); + dev_dbg(dev, + "pwm ch%d - mr %08x, dty %u, prd %u, cnt %u\n", + ch->index, + pwm_channel_readl(ch, PWM_CMR), + pwm_channel_readl(ch, PWM_CDTY), + pwm_channel_readl(ch, PWM_CPRD), + pwm_channel_readl(ch, PWM_CCNT)); +} + + +/** + * pwm_channel_alloc - allocate an unused PWM channel + * @index: identifies the channel + * @ch: structure to be initialized + * + * Drivers allocate PWM channels according to the board's wiring, and + * matching board-specific setup code. Returns zero or negative errno. + */ +int pwm_channel_alloc(int index, struct pwm_channel *ch) +{ + unsigned long flags; + int status = 0; + + /* insist on PWM init, with this signal pinned out */ + if (!pwm || !(pwm->mask & 1 << index)) + return -ENODEV; + + if (index < 0 || index >= PWM_NCHAN || !ch) + return -EINVAL; + memset(ch, 0, sizeof *ch); + + spin_lock_irqsave(&pwm->lock, flags); + if (pwm->channel[index]) + status = -EBUSY; + else { + clk_enable(pwm->clk); + + ch->regs = pwmc_regs(pwm, index); + ch->index = index; + + /* REVISIT: ap7000 seems to go 2x as fast as we expect!! */ + ch->mck = clk_get_rate(pwm->clk); + + pwm->channel[index] = ch; + pwm->handler[index] = NULL; + + /* channel and irq are always disabled when we return */ + pwm_writel(pwm, PWM_DIS, 1 << index); + pwm_writel(pwm, PWM_IDR, 1 << index); + } + spin_unlock_irqrestore(&pwm->lock, flags); + return status; +} +EXPORT_SYMBOL(pwm_channel_alloc); + +static int pwmcheck(struct pwm_channel *ch) +{ + int index; + + if (!pwm) + return -ENODEV; + if (!ch) + return -EINVAL; + index = ch->index; + if (index < 0 || index >= PWM_NCHAN || pwm->channel[index] != ch) + return -EINVAL; + + return index; +} + +/** + * pwm_channel_free - release a previously allocated channel + * @ch: the channel being released + * + * The channel is completely shut down (counter and IRQ disabled), + * and made available for re-use. Returns zero, or negative errno. + */ +int pwm_channel_free(struct pwm_channel *ch) +{ + unsigned long flags; + int t; + + spin_lock_irqsave(&pwm->lock, flags); + t = pwmcheck(ch); + if (t >= 0) { + pwm->channel[t] = NULL; + pwm->handler[t] = NULL; + + /* channel and irq are always disabled when we return */ + pwm_writel(pwm, PWM_DIS, 1 << t); + pwm_writel(pwm, PWM_IDR, 1 << t); + + clk_disable(pwm->clk); + t = 0; + } + spin_unlock_irqrestore(&pwm->lock, flags); + return t; +} +EXPORT_SYMBOL(pwm_channel_free); + +int __pwm_channel_onoff(struct pwm_channel *ch, int enabled) +{ + unsigned long flags; + int t; + + /* OMITTED FUNCTIONALITY: starting several channels in synch */ + + spin_lock_irqsave(&pwm->lock, flags); + t = pwmcheck(ch); + if (t >= 0) { + pwm_writel(pwm, enabled ? PWM_ENA : PWM_DIS, 1 << t); + t = 0; + pwm_dumpregs(ch, enabled ? "enable" : "disable"); + } + spin_unlock_irqrestore(&pwm->lock, flags); + + return t; +} +EXPORT_SYMBOL(__pwm_channel_onoff); + +/** + * pwm_clk_alloc - allocate and configure CLKA or CLKB + * @prescale: from 0..10, the power of two used to divide MCK + * @div: from 1..255, the linear divisor to use + * + * Returns PWM_CPR_CLKA, PWM_CPR_CLKB, or negative errno. The allocated + * clock will run with a period of (2^prescale * div) / MCK, or twice as + * long if center aligned PWM output is used. The clock must later be + * deconfigured using pwm_clk_free(). + */ +int pwm_clk_alloc(unsigned prescale, unsigned div) +{ + unsigned long flags; + u32 mr; + u32 val = (prescale << 8) | div; + int ret = -EBUSY; + + if (prescale >= 10 || div == 0 || div > 255) + return -EINVAL; + + spin_lock_irqsave(&pwm->lock, flags); + mr = pwm_readl(pwm, PWM_MR); + if ((mr & 0xffff) == 0) { + mr |= val; + ret = PWM_CPR_CLKA; + } + if ((mr & (0xffff << 16)) == 0) { + mr |= val << 16; + ret = PWM_CPR_CLKB; + } + if (ret > 0) + pwm_writel(pwm, PWM_MR, mr); + spin_unlock_irqrestore(&pwm->lock, flags); + return ret; +} +EXPORT_SYMBOL(pwm_clk_alloc); + +/** + * pwm_clk_free - deconfigure and release CLKA or CLKB + * + * Reverses the effect of pwm_clk_alloc(). + */ +void pwm_clk_free(unsigned clk) +{ + unsigned long flags; + u32 mr; + + spin_lock_irqsave(&pwm->lock, flags); + mr = pwm_readl(pwm, PWM_MR); + if (clk == PWM_CPR_CLKA) + pwm_writel(pwm, PWM_MR, mr & ~(0xffff << 0)); + if (clk == PWM_CPR_CLKB) + pwm_writel(pwm, PWM_MR, mr & ~(0xffff << 16)); + spin_unlock_irqrestore(&pwm->lock, flags); +} +EXPORT_SYMBOL(pwm_clk_free); + +/** + * pwm_channel_handler - manage channel's IRQ handler + * @ch: the channel + * @handler: the handler to use, possibly NULL + * + * If the handler is non-null, the handler will be called after every + * period of this PWM channel. If the handler is null, this channel + * won't generate an IRQ. + */ +int pwm_channel_handler(struct pwm_channel *ch, + void (*handler)(struct pwm_channel *ch)) +{ + unsigned long flags; + int t; + + spin_lock_irqsave(&pwm->lock, flags); + t = pwmcheck(ch); + if (t >= 0) { + pwm->handler[t] = handler; + pwm_writel(pwm, handler ? PWM_IER : PWM_IDR, 1 << t); + t = 0; + } + spin_unlock_irqrestore(&pwm->lock, flags); + + return t; +} +EXPORT_SYMBOL(pwm_channel_handler); + +static irqreturn_t pwm_irq(int id, void *_pwm) +{ + struct pwm *p = _pwm; + irqreturn_t handled = IRQ_NONE; + u32 irqstat; + int index; + + spin_lock(&p->lock); + + /* ack irqs, then handle them */ + irqstat = pwm_readl(pwm, PWM_ISR); + + while (irqstat) { + struct pwm_channel *ch; + void (*handler)(struct pwm_channel *ch); + + index = ffs(irqstat) - 1; + irqstat &= ~(1 << index); + ch = pwm->channel[index]; + handler = pwm->handler[index]; + if (handler && ch) { + spin_unlock(&p->lock); + handler(ch); + spin_lock(&p->lock); + handled = IRQ_HANDLED; + } + } + + spin_unlock(&p->lock); + return handled; +} + +static int __init pwm_probe(struct platform_device *pdev) +{ + struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0); + int irq = platform_get_irq(pdev, 0); + u32 *mp = pdev->dev.platform_data; + struct pwm *p; + int status = -EIO; + + if (pwm) + return -EBUSY; + if (!r || irq < 0 || !mp || !*mp) + return -ENODEV; + if (*mp & ~((1<<PWM_NCHAN)-1)) { + dev_warn(&pdev->dev, "mask 0x%x ... more than %d channels\n", + *mp, PWM_NCHAN); + return -EINVAL; + } + + p = kzalloc(sizeof(*p), GFP_KERNEL); + if (!p) + return -ENOMEM; + + spin_lock_init(&p->lock); + p->pdev = pdev; + p->mask = *mp; + p->irq = irq; + p->base = ioremap(r->start, r->end - r->start + 1); + if (!p->base) + goto fail; + p->clk = clk_get(&pdev->dev, "mck"); + if (IS_ERR(p->clk)) { + status = PTR_ERR(p->clk); + p->clk = NULL; + goto fail; + } + + status = request_irq(irq, pwm_irq, 0, pdev->name, p); + if (status < 0) + goto fail; + + pwm = p; + platform_set_drvdata(pdev, p); + + return 0; + +fail: + if (p->clk) + clk_put(p->clk); + if (p->base) + iounmap(p->base); + + kfree(p); + return status; +} + +static int __exit pwm_remove(struct platform_device *pdev) +{ + struct pwm *p = platform_get_drvdata(pdev); + + if (p != pwm) + return -EINVAL; + + clk_enable(pwm->clk); + pwm_writel(pwm, PWM_DIS, (1 << PWM_NCHAN) - 1); + pwm_writel(pwm, PWM_IDR, (1 << PWM_NCHAN) - 1); + clk_disable(pwm->clk); + + pwm = NULL; + + free_irq(p->irq, p); + clk_put(p->clk); + iounmap(p->base); + kfree(p); + + return 0; +} + +static struct platform_driver atmel_pwm_driver = { + .driver = { + .name = "atmel_pwm", + .owner = THIS_MODULE, + }, + .remove = __exit_p(pwm_remove), + + /* NOTE: PWM can keep running in AVR32 "idle" and "frozen" states; + * and all AT91sam9263 states, albeit at reduced clock rate if + * MCK becomes the slow clock (i.e. what Linux labels STR). + */ +}; + +static int __init pwm_init(void) +{ + return platform_driver_probe(&atmel_pwm_driver, pwm_probe); +} +module_init(pwm_init); + +static void __exit pwm_exit(void) +{ + platform_driver_unregister(&atmel_pwm_driver); +} +module_exit(pwm_exit); + +MODULE_DESCRIPTION("Driver for AT32/AT91 PWM module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.24-rc8-mm1/include/asm-avr32/arch-at32ap/board.h =================================================================== --- linux-2.6.24-rc8-mm1.orig/include/asm-avr32/arch-at32ap/board.h 2008-01-16 05:22:48.000000000 +0100 +++ linux-2.6.24-rc8-mm1/include/asm-avr32/arch-at32ap/board.h 2008-01-24 15:15:50.000000000 +0100 @@ -51,6 +51,9 @@ struct platform_device * at32_add_device_ide(unsigned int id, unsigned int extint, struct ide_platform_data *data); +/* mask says which PWM channels to mux */ +struct platform_device *at32_add_device_pwm(u32 mask); + /* depending on what's hooked up, not all SSC pins will be used */ #define ATMEL_SSC_TK 0x01 #define ATMEL_SSC_TF 0x02 Index: linux-2.6.24-rc8-mm1/include/linux/atmel_pwm.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.24-rc8-mm1/include/linux/atmel_pwm.h 2008-01-24 15:15:50.000000000 +0100 @@ -0,0 +1,70 @@ +#ifndef __LINUX_ATMEL_PWM_H +#define __LINUX_ATMEL_PWM_H + +/** + * struct pwm_channel - driver handle to a PWM channel + * @regs: base of this channel's registers + * @index: number of this channel (0..31) + * @mck: base clock rate, which can be prescaled and maybe subdivided + * + * Drivers initialize a pwm_channel structure using pwm_channel_alloc(). + * Then they configure its clock rate (derived from MCK), alignment, + * polarity, and duty cycle by writing directly to the channel registers, + * before enabling the channel by calling pwm_channel_enable(). + * + * After emitting a PWM signal for the desired length of time, drivers + * may then pwm_channel_disable() or pwm_channel_free(). Both of these + * disable the channel, but when it's freed the IRQ is deconfigured and + * the channel must later be re-allocated and reconfigured. + * + * Note that if the period or duty cycle need to be changed while the + * PWM channel is operating, drivers must use the PWM_CUPD double buffer + * mechanism, either polling until they change or getting implicitly + * notified through a once-per-period interrupt handler. + */ +struct pwm_channel { + void __iomem *regs; + unsigned index; + unsigned long mck; +}; + +extern int pwm_channel_alloc(int index, struct pwm_channel *ch); +extern int pwm_channel_free(struct pwm_channel *ch); + +extern int pwm_clk_alloc(unsigned prescale, unsigned div); +extern void pwm_clk_free(unsigned clk); + +extern int __pwm_channel_onoff(struct pwm_channel *ch, int enabled); + +#define pwm_channel_enable(ch) __pwm_channel_onoff((ch), 1) +#define pwm_channel_disable(ch) __pwm_channel_onoff((ch), 0) + +/* periodic interrupts, mostly for CUPD changes to period or cycle */ +extern int pwm_channel_handler(struct pwm_channel *ch, + void (*handler)(struct pwm_channel *ch)); + +/* per-channel registers (banked at pwm_channel->regs) */ +#define PWM_CMR 0x00 /* mode register */ +#define PWM_CPR_CPD (1 << 10) /* set: CUPD modifies period */ +#define PWM_CPR_CPOL (1 << 9) /* set: idle high */ +#define PWM_CPR_CALG (1 << 8) /* set: center align */ +#define PWM_CPR_CPRE (0xf << 0) /* mask: rate is mck/(2^pre) */ +#define PWM_CPR_CLKA (0xb << 0) /* rate CLKA */ +#define PWM_CPR_CLKB (0xc << 0) /* rate CLKB */ +#define PWM_CDTY 0x04 /* duty cycle (max of CPRD) */ +#define PWM_CPRD 0x08 /* period (count up from zero) */ +#define PWM_CCNT 0x0c /* counter (20 bits?) */ +#define PWM_CUPD 0x10 /* update CPRD (or CDTY) next period */ + +static inline void +pwm_channel_writel(struct pwm_channel *pwmc, unsigned offset, u32 val) +{ + __raw_writel(val, pwmc->regs + offset); +} + +static inline u32 pwm_channel_readl(struct pwm_channel *pwmc, unsigned offset) +{ + return __raw_readl(pwmc->regs + offset); +} + +#endif /* __LINUX_ATMEL_PWM_H */ ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH -mm 2/2] PWM LED driver 2008-01-24 14:33 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 Haavard Skinnemoen @ 2008-01-24 14:33 ` Haavard Skinnemoen 2008-01-28 5:32 ` Andrew Morton 2008-01-24 20:53 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 David Brownell 1 sibling, 1 reply; 10+ messages in thread From: Haavard Skinnemoen @ 2008-01-24 14:33 UTC (permalink / raw) To: Andrew Morton Cc: David Brownell, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel, David Brownell, Haavard Skinnemoen From: David Brownell <david-b@pacbell.net> This is a LED driver using the PWM on newer SOCs from Atmel; brightness is controlled by changing the PWM duty cycle. So for example if you've set up two leds labeled "pwm0" and "pwm1": echo 0 > /sys/class/leds/pwm2/brightness # off (0%) echo 80 > /sys/class/leds/pwm2/brightness echo 255 > /sys/class/leds/pwm2/brightness # on (100%) Note that "brightness" here isn't linear; maybe that should change. Going from 4 to 8 probably doubles perceived brightness, while 244 to 248 is imperceptible. This is mostly intended to be a simple example of PWM, although it's realistic since LCD backlights are often driven with PWM to conserve battery power (and offer brightness options). Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com> --- drivers/leds/Kconfig | 7 ++ drivers/leds/Makefile | 1 + drivers/leds/leds-atmel-pwm.c | 157 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 0 deletions(-) create mode 100644 drivers/leds/leds-atmel-pwm.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index ec568fa..4e1db3b 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -18,6 +18,13 @@ config LEDS_CLASS comment "LED drivers" +config LEDS_ATMEL_PWM + tristate "LED Support using Atmel PWM outputs" + depends on LEDS_CLASS && ATMEL_PWM + help + This option enables support for LEDs driven using outputs + of the dedicated PWM controller found on newer Atmel SOCs. + config LEDS_CORGI tristate "LED Support for the Sharp SL-C7x0 series" depends on LEDS_CLASS && PXA_SHARP_C7xx diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index a60de1b..04bc850 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -5,6 +5,7 @@ obj-$(CONFIG_LEDS_CLASS) += led-class.o obj-$(CONFIG_LEDS_TRIGGERS) += led-triggers.o # LED Platform Drivers +obj-$(CONFIG_LEDS_ATMEL_PWM) += leds-atmel-pwm.o obj-$(CONFIG_LEDS_CORGI) += leds-corgi.o obj-$(CONFIG_LEDS_LOCOMO) += leds-locomo.o obj-$(CONFIG_LEDS_SPITZ) += leds-spitz.o diff --git a/drivers/leds/leds-atmel-pwm.c b/drivers/leds/leds-atmel-pwm.c new file mode 100644 index 0000000..af61f55 --- /dev/null +++ b/drivers/leds/leds-atmel-pwm.c @@ -0,0 +1,157 @@ +#include <linux/kernel.h> +#include <linux/platform_device.h> +#include <linux/leds.h> +#include <linux/io.h> +#include <linux/atmel_pwm.h> + + +struct pwmled { + struct led_classdev cdev; + struct pwm_channel pwmc; + struct gpio_led *desc; + u32 mult; + u8 active_low; +}; + + +/* + * For simplicity, we use "brightness" as if it were a linear function + * of PWM duty cycle. However, a logarithmic function of duty cycle is + * probably a better match for perceived brightness: two is half as bright + * as four, four is half as bright as eight, etc + */ +static void pwmled_brightness(struct led_classdev *cdev, enum led_brightness b) +{ + struct pwmled *led; + + /* update the duty cycle for the *next* period */ + led = container_of(cdev, struct pwmled, cdev); + pwm_channel_writel(&led->pwmc, PWM_CUPD, led->mult * (unsigned) b); +} + +/* + * NOTE: we reuse the platform_data structure of GPIO leds, + * but repurpose its "gpio" number as a PWM channel number. + */ +static int __init pwmled_probe(struct platform_device *pdev) +{ + const struct gpio_led_platform_data *pdata; + struct pwmled *leds; + unsigned i; + int status; + + pdata = pdev->dev.platform_data; + if (!pdata || pdata->num_leds < 1) + return -ENODEV; + + leds = kcalloc(pdata->num_leds, sizeof(*leds), GFP_KERNEL); + if (!leds) + return -ENOMEM; + + for (i = 0; i < pdata->num_leds; i++) { + struct pwmled *led = leds + i; + const struct gpio_led *dat = pdata->leds + i; + u32 tmp; + + led->cdev.name = dat->name; + led->cdev.brightness = LED_OFF; + led->cdev.brightness_set = pwmled_brightness; + led->cdev.default_trigger = dat->default_trigger; + + led->active_low = dat->active_low; + + status = pwm_channel_alloc(dat->gpio, &led->pwmc); + if (status < 0) + goto err; + + /* + * Prescale clock by 2^x, so PWM counts in low MHz. + * Start each cycle with the LED active, so increasing + * the duty cycle gives us more time on (== brighter). + */ + tmp = 5; + if (!led->active_low) + tmp |= PWM_CPR_CPOL; + pwm_channel_writel(&led->pwmc, PWM_CMR, tmp); + + /* + * Pick a period so PWM cycles at 100+ Hz; and a multiplier + * for scaling duty cycle: brightness * mult. + */ + tmp = (led->pwmc.mck / (1 << 5)) / 100; + tmp /= 255; + led->mult = tmp; + pwm_channel_writel(&led->pwmc, PWM_CDTY, + led->cdev.brightness * 255); + pwm_channel_writel(&led->pwmc, PWM_CPRD, + LED_FULL * tmp); + + pwm_channel_enable(&led->pwmc); + + /* Hand it over to the LED framework */ + status = led_classdev_register(&pdev->dev, &led->cdev); + if (status < 0) { + pwm_channel_free(&led->pwmc); + goto err; + } + } + + platform_set_drvdata(pdev, leds); + return 0; + +err: + if (i > 0) { + for (i = i - 1; i >= 0; i--) { + led_classdev_unregister(&leds[i].cdev); + pwm_channel_free(&leds[i].pwmc); + } + } + kfree(leds); + + return status; +} + +static int __exit pwmled_remove(struct platform_device *pdev) +{ + const struct gpio_led_platform_data *pdata; + struct pwmled *leds; + unsigned i; + + pdata = pdev->dev.platform_data; + leds = platform_get_drvdata(pdev); + + for (i = 0; i < pdata->num_leds; i++) { + struct pwmled *led = leds + i; + + led_classdev_unregister(&led->cdev); + pwm_channel_free(&led->pwmc); + } + + kfree(leds); + platform_set_drvdata(pdev, NULL); + return 0; +} + +static struct platform_driver pwmled_driver = { + .driver = { + .name = "leds-atmel-pwm", + .owner = THIS_MODULE, + }, + /* REVISIT add suspend() and resume() methods */ + .remove = __exit_p(pwmled_remove), +}; + +static int __init modinit(void) +{ + return platform_driver_probe(&pwmled_driver, pwmled_probe); +} +module_init(modinit); + +static void __exit modexit(void) +{ + platform_driver_unregister(&pwmled_driver); +} +module_exit(modexit); + +MODULE_DESCRIPTION("Driver for LEDs with PWM-controlled brightness"); +MODULE_LICENSE("GPL"); -- 1.5.3.8 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH -mm 2/2] PWM LED driver 2008-01-24 14:33 ` [PATCH -mm 2/2] PWM LED driver Haavard Skinnemoen @ 2008-01-28 5:32 ` Andrew Morton 2008-01-28 9:15 ` Haavard Skinnemoen 0 siblings, 1 reply; 10+ messages in thread From: Andrew Morton @ 2008-01-28 5:32 UTC (permalink / raw) To: Haavard Skinnemoen Cc: David Brownell, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel, David Brownell On Thu, 24 Jan 2008 15:33:45 +0100 Haavard Skinnemoen <hskinnemoen@atmel.com> wrote: > + if (i > 0) { > + for (i = i - 1; i >= 0; i--) { > + led_classdev_unregister(&leds[i].cdev); > + pwm_channel_free(&leds[i].pwmc); > + } > + } Could be: while (--i > 0) { led_classdev_unregister(&leds[i].cdev); pwm_channel_free(&leds[i].pwmc); } or thereabouts. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH -mm 2/2] PWM LED driver 2008-01-28 5:32 ` Andrew Morton @ 2008-01-28 9:15 ` Haavard Skinnemoen 2008-01-28 9:29 ` Andrew Morton 0 siblings, 1 reply; 10+ messages in thread From: Haavard Skinnemoen @ 2008-01-28 9:15 UTC (permalink / raw) To: Andrew Morton Cc: David Brownell, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel, David Brownell On Sun, 27 Jan 2008 21:32:32 -0800 Andrew Morton <akpm@linux-foundation.org> wrote: > On Thu, 24 Jan 2008 15:33:45 +0100 Haavard Skinnemoen <hskinnemoen@atmel.com> wrote: > > > + if (i > 0) { > > + for (i = i - 1; i >= 0; i--) { > > + led_classdev_unregister(&leds[i].cdev); > > + pwm_channel_free(&leds[i].pwmc); > > + } > > + } > > Could be: > > while (--i > 0) { > led_classdev_unregister(&leds[i].cdev); > pwm_channel_free(&leds[i].pwmc); > } > > or thereabouts. Almost...we need to clean up for leds[0] too. Using a postfix decrement should take care of that. How about the patch below? Haavard >From de5002ad71a1000f81817410f02a7d9fbd5d4ecd Mon Sep 17 00:00:00 2001 From: Haavard Skinnemoen <hskinnemoen@atmel.com> Date: Mon, 28 Jan 2008 10:14:14 +0100 Subject: [PATCH] PWM led driver: Simplify cleanup loop Why use a for loop inside an if() when we can get away with a simple while() loop? Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com> --- drivers/leds/leds-atmel-pwm.c | 8 +++----- 1 files changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/leds/leds-atmel-pwm.c b/drivers/leds/leds-atmel-pwm.c index af61f55..187031c 100644 --- a/drivers/leds/leds-atmel-pwm.c +++ b/drivers/leds/leds-atmel-pwm.c @@ -100,11 +100,9 @@ static int __init pwmled_probe(struct platform_device *pdev) return 0; err: - if (i > 0) { - for (i = i - 1; i >= 0; i--) { - led_classdev_unregister(&leds[i].cdev); - pwm_channel_free(&leds[i].pwmc); - } + while (i-- > 0) { + led_classdev_unregister(&leds[i].cdev); + pwm_channel_free(&leds[i].pwmc); } kfree(leds); -- 1.5.3.8 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH -mm 2/2] PWM LED driver 2008-01-28 9:15 ` Haavard Skinnemoen @ 2008-01-28 9:29 ` Andrew Morton 2008-01-28 9:32 ` Haavard Skinnemoen 2008-01-28 9:41 ` David Brownell 0 siblings, 2 replies; 10+ messages in thread From: Andrew Morton @ 2008-01-28 9:29 UTC (permalink / raw) To: Haavard Skinnemoen Cc: David Brownell, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel, David Brownell On Mon, 28 Jan 2008 10:15:51 +0100 Haavard Skinnemoen <hskinnemoen@atmel.com> wrote: > On Sun, 27 Jan 2008 21:32:32 -0800 > Andrew Morton <akpm@linux-foundation.org> wrote: > > > On Thu, 24 Jan 2008 15:33:45 +0100 Haavard Skinnemoen <hskinnemoen@atmel.com> wrote: > > > > > + if (i > 0) { > > > + for (i = i - 1; i >= 0; i--) { > > > + led_classdev_unregister(&leds[i].cdev); > > > + pwm_channel_free(&leds[i].pwmc); > > > + } > > > + } > > > > Could be: > > > > while (--i > 0) { > > led_classdev_unregister(&leds[i].cdev); > > pwm_channel_free(&leds[i].pwmc); > > } > > > > or thereabouts. > > Almost...we need to clean up for leds[0] too. Using a postfix decrement > should take care of that. How about the patch below? > > Haavard > > >From de5002ad71a1000f81817410f02a7d9fbd5d4ecd Mon Sep 17 00:00:00 2001 > From: Haavard Skinnemoen <hskinnemoen@atmel.com> > Date: Mon, 28 Jan 2008 10:14:14 +0100 > Subject: [PATCH] PWM led driver: Simplify cleanup loop > > Why use a for loop inside an if() when we can get away with a simple > while() loop? > > Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com> > --- > drivers/leds/leds-atmel-pwm.c | 8 +++----- > 1 files changed, 3 insertions(+), 5 deletions(-) > > diff --git a/drivers/leds/leds-atmel-pwm.c b/drivers/leds/leds-atmel-pwm.c > index af61f55..187031c 100644 > --- a/drivers/leds/leds-atmel-pwm.c > +++ b/drivers/leds/leds-atmel-pwm.c > @@ -100,11 +100,9 @@ static int __init pwmled_probe(struct platform_device *pdev) > return 0; > > err: > - if (i > 0) { > - for (i = i - 1; i >= 0; i--) { > - led_classdev_unregister(&leds[i].cdev); > - pwm_channel_free(&leds[i].pwmc); > - } > + while (i-- > 0) { > + led_classdev_unregister(&leds[i].cdev); > + pwm_channel_free(&leds[i].pwmc); > } Looks OK, although I'd say that `while (--i >= 0)' is more idiomatic - predecrement, postincrement and all that? ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH -mm 2/2] PWM LED driver 2008-01-28 9:29 ` Andrew Morton @ 2008-01-28 9:32 ` Haavard Skinnemoen 2008-01-28 9:41 ` David Brownell 1 sibling, 0 replies; 10+ messages in thread From: Haavard Skinnemoen @ 2008-01-28 9:32 UTC (permalink / raw) To: Andrew Morton Cc: David Brownell, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel, David Brownell On Mon, 28 Jan 2008 01:29:32 -0800 Andrew Morton <akpm@linux-foundation.org> wrote: > > - if (i > 0) { > > - for (i = i - 1; i >= 0; i--) { > > - led_classdev_unregister(&leds[i].cdev); > > - pwm_channel_free(&leds[i].pwmc); > > - } > > + while (i-- > 0) { > > + led_classdev_unregister(&leds[i].cdev); > > + pwm_channel_free(&leds[i].pwmc); > > } > > Looks OK, although I'd say that `while (--i >= 0)' is more idiomatic - > predecrement, postincrement and all that? Maybe. while (i-- > 0) has the advantage that it will work even if i is unsigned though... Haavard ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH -mm 2/2] PWM LED driver 2008-01-28 9:29 ` Andrew Morton 2008-01-28 9:32 ` Haavard Skinnemoen @ 2008-01-28 9:41 ` David Brownell 1 sibling, 0 replies; 10+ messages in thread From: David Brownell @ 2008-01-28 9:41 UTC (permalink / raw) To: Andrew Morton Cc: Haavard Skinnemoen, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel On Monday 28 January 2008, Andrew Morton wrote: > > - if (i > 0) { > > - for (i = i - 1; i >= 0; i--) { > > - led_classdev_unregister(&leds[i].cdev); > > - pwm_channel_free(&leds[i].pwmc); > > - } > > + while (i-- > 0) { > > + led_classdev_unregister(&leds[i].cdev); > > + pwm_channel_free(&leds[i].pwmc); > > } > > Looks OK, although I'd say that `while (--i >= 0)' is more idiomatic - > predecrement, postincrement and all that? Except for the "unsigned i;" declaration earlier... given that, "while (true) cpu_relax();" becomes maximally idiomatic. :) Odd how PDP-11 idioms linger. Just because C was designed on that processor ... ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 2008-01-24 14:33 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 Haavard Skinnemoen 2008-01-24 14:33 ` [PATCH -mm 2/2] PWM LED driver Haavard Skinnemoen @ 2008-01-24 20:53 ` David Brownell 2008-01-27 14:16 ` Haavard Skinnemoen 1 sibling, 1 reply; 10+ messages in thread From: David Brownell @ 2008-01-24 20:53 UTC (permalink / raw) To: Haavard Skinnemoen Cc: Andrew Morton, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel On Thursday 24 January 2008, Haavard Skinnemoen wrote: > +config ATMEL_PWM > + tristate "Atmel AT32/AT91 PWM support" > + depends on (AVR32 || AT91) && EXPERIMENTAL There's probably no need for EXPERIMENTAL except in the limited sense of "young driver". :) There's a bug there ... it should have used ARCH_AT91 instead of just AT91. Or even ARCH_AT91SAM9263 || ARCH_AT91SAM9RL. - Dave ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 2008-01-24 20:53 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 David Brownell @ 2008-01-27 14:16 ` Haavard Skinnemoen 0 siblings, 0 replies; 10+ messages in thread From: Haavard Skinnemoen @ 2008-01-27 14:16 UTC (permalink / raw) To: David Brownell Cc: Andrew Morton, Andrew Victor, Nicolas Ferre, Patrice Vilchez, Richard Purdie, linux-kernel, kernel On Thu, 24 Jan 2008 12:53:13 -0800 David Brownell <david-b@pacbell.net> wrote: > On Thursday 24 January 2008, Haavard Skinnemoen wrote: > > +config ATMEL_PWM > > + tristate "Atmel AT32/AT91 PWM support" > > + depends on (AVR32 || AT91) && EXPERIMENTAL > > There's probably no need for EXPERIMENTAL except in the > limited sense of "young driver". :) Yeah, I don't think EXPERIMENTAL makes sense here. Either you need the PWM or you don't. > There's a bug there ... it should have used ARCH_AT91 instead > of just AT91. Or even ARCH_AT91SAM9263 || ARCH_AT91SAM9RL. I've turned it into ARCH_AT91. I think listing the specific chips that have it is a bit too much; it won't break if you enable it on a chip that doesn't have it, it will just be useless. That's what defconfigs are for, and we still want people that test allmodconfigs on e.g. AT91RM9200 to report any breakage. Patch below. Haavard diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 03c0c27..23a9231 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -15,7 +15,7 @@ if MISC_DEVICES config ATMEL_PWM tristate "Atmel AT32/AT91 PWM support" - depends on (AVR32 || AT91) && EXPERIMENTAL + depends on AVR32 || ARCH_AT91 help This option enables device driver support for the PWM channels on certain Atmel prcoessors. Pulse Width Modulation is used for ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2008-01-28 9:41 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2008-01-24 14:33 [PATCH -mm 0/2] AVR32 PWM driver and example user Haavard Skinnemoen 2008-01-24 14:33 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 Haavard Skinnemoen 2008-01-24 14:33 ` [PATCH -mm 2/2] PWM LED driver Haavard Skinnemoen 2008-01-28 5:32 ` Andrew Morton 2008-01-28 9:15 ` Haavard Skinnemoen 2008-01-28 9:29 ` Andrew Morton 2008-01-28 9:32 ` Haavard Skinnemoen 2008-01-28 9:41 ` David Brownell 2008-01-24 20:53 ` [PATCH -mm 1/2] Basic PWM driver for AVR32 and AT91 David Brownell 2008-01-27 14:16 ` Haavard Skinnemoen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).