Common power driver for Linux gadgets
RFC PATCH 2/7 Common power driver for Linux gadgets
This driver used to stop code/logic duplication through different machines we porting at handhelds.org. pda_power register machs' power supplies, and will take care about notifying batteries about power changes through external power interface.
This driver should be suitable for almost every Linux gadget today.
Here is brief example how we use it:
static int h5000_is_ac_online(void)
{
return !!(samcop_get_gpio_a(&h5400_samcop.dev) &
SAMCOP_GPIO_GPA_ADP_IN_STATUS);
}static int h5000_is_usb_online(void)
{
return !!(samcop_get_gpio_a(&h5400_samcop.dev) &
SAMCOP_GPIO_GPA_USB_DETECT);
}static void h5000_set_charge(int flags)
{
SET_H5400_GPIO(CHG_EN, !!flags);
SET_H5400_GPIO(EXT_CHG_RATE, !!(flags & PDA_POWER_CHARGE_AC));
SET_H5400_GPIO(USB_CHG_RATE, !!(flags & PDA_POWER_CHARGE_USB));
return;
}static struct pda_power_pdata h5000_power_pdata = {
.is_ac_online = h5000_is_ac_online,
.is_usb_online = h5000_is_usb_online,
.set_charge = h5000_set_charge,
};static struct resource h5000_power_resourses[] = {
[0] = {
.name = "ac",
.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE |
IORESOURCE_IRQ_LOWEDGE,
},
[1] = {
.name = "usb",
.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE |
IORESOURCE_IRQ_LOWEDGE,
},
};static struct platform_device h5000_power_pdev = {
.name = "pda-power",
.id = -1,
.resource = h5000_power_resourses,
.num_resources = ARRAY_SIZE(h5000_power_resourses),
.dev = {
.platform_data = &h5000_power_pdata,
},
};--- drivers/power/Kconfig | 8 ++ drivers/power/Makefile | 1 + drivers/power/pda_power.c | 218 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/pda_power.h | 27 ++++++ 4 files changed, 254 insertions(+), 0 deletions(-) create mode 100644 drivers/power/pda_power.c create mode 100644 include/linux/pda_power.h
drivers/power/Kconfig +8 -0
@@ -10,4 +10,12 @@ config EXTERNAL_POWER This interface is mandatory for battery class support. +config PDA_POWER + tristate "Generic PDA/phone power driver" + depends on EXTERNAL_POWER + help + Say Y here to enable generic power driver for PDAs and phones with + one or two external power supplies (AC/USB) connected to main and + backup batteries, and optional builtin charger. + endmenu
drivers/power/Makefile +1 -0
@@ -1 +1,2 @@ obj-$(CONFIG_EXTERNAL_POWER) += external_power.o +obj-$(CONFIG_PDA_POWER) += pda_power.o
drivers/power/pda_power.c new file +218 -0
218 lines not on this page · raw message
include/linux/pda_power.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Common power driver for PDAs and phones with one or two external +* power supplies (AC/USB) connected to main and backup batteries, +* and optional builtin charger. +* +* Copyright 2007 Anton Vorontsov <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PDA_POWER_H__ +#define __PDA_POWER_H__ + +#include <linux/external_power.h> + +#define PDA_POWER_CHARGE_AC (1 << 0) +#define PDA_POWER_CHARGE_USB (1 << 1) + +struct pda_power_pdata { +int (*is_ac_online)(void ); +int (*is_usb_online)(void ); +void (*set_charge)(int flags); +}; + +#endif /* __PDA_POWER_H__ */
Signature · 1 line
1.5.0.5-dirty
Quoted 6 lines · Anton Vorontsov
This driver used to stop code/logic duplication through different machines we porting at handhelds.org.
pda_powerregister machs' power supplies, and will take care about notifying batteries about power changes through external power interface.This driver should be suitable for almost every Linux gadget today.
Changes:
- implement timer, it's used for two purposes:
1) on some machines you can't read is_{ac,usb}_online() values just after interrupt. Should wait a bit to read reliable values.
2) irq debouncing
- cleanups
Subject: [PATCH] [take2] pda_power driver
--- drivers/power/Kconfig | 8 ++ drivers/power/Makefile | 1 + drivers/power/pda_power.c | 231 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/pda_power.h | 25 +++++ 4 files changed, 265 insertions(+), 0 deletions(-) create mode 100644 drivers/power/pda_power.c create mode 100644 include/linux/pda_power.h
drivers/power/Kconfig +8 -0
@@ -10,4 +10,12 @@ config EXTERNAL_POWER This interface is mandatory for battery class support. +config PDA_POWER + tristate "Generic PDA/phone power driver" + depends on EXTERNAL_POWER + help + Say Y here to enable generic power driver for PDAs and phones with + one or two external power supplies (AC/USB) connected to main and + backup batteries, and optional builtin charger. + endmenu
drivers/power/Makefile +1 -0
@@ -1 +1,2 @@ obj-$(CONFIG_EXTERNAL_POWER) += external_power.o +obj-$(CONFIG_PDA_POWER) += pda_power.o
drivers/power/pda_power.c new file +231 -0
231 lines not on this page · raw message
include/linux/pda_power.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Common power driver for PDAs and phones with one or two external +* power supplies (AC/USB) connected to main and backup batteries, +* and optional builtin charger. +* +* Copyright 2007 Anton Vorontsov <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PDA_POWER_H__ +#define __PDA_POWER_H__ + +#define PDA_POWER_CHARGE_AC (1 << 0) +#define PDA_POWER_CHARGE_USB (1 << 1) + +struct pda_power_pdata { +int (*is_ac_online)(void ); +int (*is_usb_online)(void ); +void (*set_charge)(int flags); +}; + +#endif /* __PDA_POWER_H__ */
Signature · 1 line
1.5.0.5-dirty
Quoted 11 lines · Anton Vorontsov
drivers/power/pda_power.c new file +11
-0+/* +* include/linux/ioport.h does not provide flags for generic IRQ trigger +* types. So, we're using "ISA PnP IRQ specific bits", and converting them. +*/ +static unsigned int get_irq_flags (struct resource *res) +{ +unsigned int flags =IRQF_DISABLED ; + +if (res->flags &IORESOURCE_IRQ_HIGHEDGE ) + flags |=IRQF_TRIGGER_RISING ; +if (res->flags &IORESOURCE_IRQ_LOWEDGE )
drivers/power/pda_power.c line 40
Quoted 10 lines
drivers/power/pda_power.c new file +10
-0+ flags |=IRQF_TRIGGER_FALLING ; +if (res->flags &IORESOURCE_IRQ_HIGHLEVEL ) + flags |=IRQF_TRIGGER_HIGH ; +if (res->flags &IORESOURCE_IRQ_LOWLEVEL ) + flags |=IRQF_TRIGGER_LOW ; +if (res->flags &IORESOURCE_IRQ_SHAREABLE ) + flags |=IRQF_SHARED ; + +return flags; +}
Eww. The IORESOURCE IRQ bits are intentionally chosen to be the same as the IRQF bits:
include/linux/interrupt.h:
/*
* These correspond to the IORESOURCE_IRQ_* defines in
* linux/ioport.h to select the interrupt line behaviour. When
* requesting an interrupt without specifying a IRQF_TRIGGER, the
* setting should be assumed to be "as already configured", which
* may be as per machine or firmware initialisation.
*/
The exception is IRQF_SHARED which should be a _driver_ choice not a _platform_ choice, and therefore makes no sense in your "get_irq_flags" definition.
Plus, if we ever did want to introduce such a function, it should be a generic thing, not specific to some random power subsystem de jour.
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of:
Quoted 26 lines · Russell King
Quoted 11 lines · Anton Vorontsov
drivers/power/pda_power.c new file +11
-0+/* +* include/linux/ioport.h does not provide flags for generic IRQ trigger +* types. So, we're using "ISA PnP IRQ specific bits", and converting them. +*/ +static unsigned int get_irq_flags (struct resource *res) +{ +unsigned int flags =IRQF_DISABLED ; + +if (res->flags &IORESOURCE_IRQ_HIGHEDGE ) + flags |=IRQF_TRIGGER_RISING ; +if (res->flags &IORESOURCE_IRQ_LOWEDGE )drivers/power/pda_power.c line 40
Quoted 10 lines
drivers/power/pda_power.c new file +10
-0+ flags |=IRQF_TRIGGER_FALLING ; +if (res->flags &IORESOURCE_IRQ_HIGHLEVEL ) + flags |=IRQF_TRIGGER_HIGH ; +if (res->flags &IORESOURCE_IRQ_LOWLEVEL ) + flags |=IRQF_TRIGGER_LOW ; +if (res->flags &IORESOURCE_IRQ_SHAREABLE ) + flags |=IRQF_SHARED ; + +return flags; +}Eww. The IORESOURCE IRQ bits are intentionally chosen to be the same as the IRQF bits:
Quoted 3 lines
include/linux/interrupt.h:
/*
* These correspond to the IORESOURCE_IRQ_* defines in
Yeah, few days ago I noticed it myself, already fixed it in my tree. Thanks anyway.
Quoted 9 lines
*
linux/ioport.hto select the interrupt line behaviour. When
* requesting an interrupt without specifying aIRQF_TRIGGER, the
* setting should be assumed to be "as already configured", which
* may be as per machine or firmware initialisation.
*/The exception is
IRQF_SHAREDwhich should be a _driver_ choice not a _platform_ choice, and therefore makes no sense in your "get_irq_flags" definition.
Ugh, I see...
Quoted 8 lines
Plus, if we ever did want to introduce such a function, it should be a generic thing, not specific to some random power subsystem de jour.
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of:
Thanks for comments.
Signature · 4 lines
Anton Vorontsov email: [email protected] backup email: [email protected] irc://irc.freenode.org/bd2