From: <z7...@us...> - 2007-01-31 20:16:02
|
Revision: 787 http://svn.sourceforge.net/hackndev/?rev=787&view=rev Author: z72ka Date: 2007-01-31 12:14:56 -0800 (Wed, 31 Jan 2007) Log Message: ----------- Palmz72: Added new experimental driver for battery monitor based on latest WM9712 driver Modified Paths: -------------- linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Kconfig linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Makefile Added Paths: ----------- linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/palmz72_battery.c Modified: linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Kconfig =================================================================== --- linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Kconfig 2007-01-31 18:52:36 UTC (rev 786) +++ linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Kconfig 2007-01-31 20:14:56 UTC (rev 787) @@ -19,6 +19,13 @@ default y if MACH_PALMZ72 help Enable support for suspend/resume the PalmOne Zire 72 PDA. + +config PALMZ72_BATTERY + tristate "Palm Zire 72 Battery support" + depends on MACH_PALMZ72 + default y if MACH_PALMZ72 + help + Battery. config GPIOED tristate "GPIOED" Modified: linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Makefile =================================================================== --- linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Makefile 2007-01-31 18:52:36 UTC (rev 786) +++ linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/Makefile 2007-01-31 20:14:56 UTC (rev 787) @@ -5,5 +5,6 @@ obj-$(CONFIG_MACH_PALMZ72) += palmz72.o obj-$(CONFIG_PALMZ72_AC97) += palmz72_ac97.o obj-$(CONFIG_PALMZ72_PM) += palmz72_pm.o +obj-$(CONFIG_PALMZ72_BATTERY) += palmz72_battery.o obj-$(CONFIG_GPIOED) += gpioed.o obj-$(CONFIG_GPIOEDNG) += gpioed-ng.o Added: linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/palmz72_battery.c =================================================================== --- linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/palmz72_battery.c (rev 0) +++ linux4palm/linux/trunk/arch/arm/mach-pxa/palmz72/palmz72_battery.c 2007-01-31 20:14:56 UTC (rev 787) @@ -0,0 +1,256 @@ +/************************************************************************ + * linux/arch/arm/mach-pxa/palmz72/palmz72_ac97.c * + * * + * Touchscreen/battery driver for Palm Zire 72 WM9712 AC97 codec * + * Author: Jan Herman <2h...@se...> * + * Based on palmld_ac97.c code from Alex Osborne * + * * + ************************************************************************/ + + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/moduleparam.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/input.h> +#include <linux/device.h> +#include <linux/workqueue.h> +#include <linux/battery.h> + +#include <asm/apm.h> +#include <asm/delay.h> +#include <asm/mach-types.h> +#include <asm/mach/arch.h> +#include <asm/mach/map.h> +#include <asm/arch/hardware.h> +#include <asm/arch/pxa-regs.h> +#include <asm/arch/irqs.h> + +#include <sound/driver.h> +#include <sound/core.h> +#include <sound/pcm.h> +#include <sound/initval.h> +#include <linux/wm97xx.h> +//#include <sound/wm9712.h> +#include <asm/arch/palmz72-gpio.h> +#include <asm/arch/palmz72-init.h> + + +#define palmz72_ac97_WORK_QUEUE_NAME "palmz72_ac97_workqueue" + + +/********************* + * Module parameters * + *********************/ +static DECLARE_MUTEX(battery_update_mutex); + +//static struct work_struct palmz72_ac97_irq_task; + +//struct device *palmz72_ac97_dev; + +//static ac97_t *ac97; + +static int battery_registered = 0; +static unsigned long last_battery_update = 0; +static int current_voltage; +static int previous_voltage; +//static u16 d2base; + + + +//EXPERIMENTAL DRIVER... + +/*********** + * Battery * + ***********/ + + +void palmz72_battery_read_adc(unsigned long data) +{ + //unsigned long data; + u16 vread; + //int force; + + struct wm97xx *wm = (struct wm97xx*) data; + //struct wm97xx* wm; + + //if(wm == NULL) { + // printk("Batt update error: struct wm97xx* wm is null!\n"); + // return; + //} + + //if(!force && ((last_battery_update + 10 *HZ) > jiffies)) + // return; + // + //if(down_trylock(&battery_update_mutex)) + // return; + + //aux_waiting:1; + //vread = wm97xx_read_aux_adc(wm, WM97XX_AUX_ID3); + + vread = 1680; // THIS MUST READ AUX_ID3 on WM9712 -- for slapin.. other of battery works + + previous_voltage = current_voltage; + current_voltage = vread & 0xfff; + last_battery_update = jiffies; + + printk("Battery: %d\n", current_voltage); + + up(&battery_update_mutex); +} + + +int palmz72_battery_min_voltage(struct battery *b) +{ + return PALMZ72_BAT_MIN_VOLTAGE; +} + + +int palmz72_battery_max_voltage(struct battery *b) +{ + return PALMZ72_BAT_MAX_VOLTAGE; /* mV */ +} + +/* + This formula is based on battery life of my battery 1100mAh. Original battery in Zire72 is Li-On 920mAh + V_batt = ADCSEL_BMON * 1,889 + 767,8 [mV] +*/ + +int palmz72_battery_get_voltage(struct battery *b) +{ + if (battery_registered){ + palmz72_battery_read_adc(0); + printk("Battery [mV]: %d\n", current_voltage * 1889/1000 + 7678/10 ); + return current_voltage * 1889/1000 + 7678/10; + } + else{ + printk("palmz72_battery: cannot get voltage -> battery driver unregistered\n"); + return 0; + } +} + + +int palmz72_battery_get_status(struct battery *b) +{ + int ac_connected = GET_GPIO(GPIO_NR_PALMZ72_POWER_DETECT); + int usb_connected = !GET_GPIO(GPIO_NR_PALMZ72_USB_DETECT); + + if (current_voltage <= 0) + return BATTERY_STATUS_UNKNOWN; + + if (ac_connected || usb_connected){ + if ( ( current_voltage > previous_voltage ) || (current_voltage <= PALMZ72_BAT_MAX_VOLTAGE) ) + return BATTERY_STATUS_CHARGING; + return BATTERY_STATUS_NOT_CHARGING; + } + else + return BATTERY_STATUS_DISCHARGING; +} + + +struct battery palmz72_battery = { + .name = "palmz72_battery", + .id = "battery0", + .get_min_voltage = palmz72_battery_min_voltage, + .get_max_voltage = palmz72_battery_max_voltage, + .get_voltage = palmz72_battery_get_voltage, + .get_status = palmz72_battery_get_status, +}; + + + +static int palmz72_ac_is_connected (void){ + /* when charger is plugged in and USB is not connected, then status is ONLINE */ + int ret = (!(GET_GPIO(GPIO_NR_PALMZ72_USB_PULLUP)) && !(GET_GPIO(GPIO_NR_PALMZ72_USB_DETECT)));; + if (ret) + ret = 1; + else + ret = 0; + + return ret; +} + +/******* + * APM * + *******/ + +static void palmz72_apm_get_power_status(struct apm_power_info *info) +{ + int min, max, curr, percent; + + curr = palmz72_battery_get_voltage(NULL); + min = palmz72_battery_min_voltage(NULL); + max = palmz72_battery_max_voltage(NULL); + + curr = curr - min; + if (curr < 0) curr = 0; + max = max - min; + + percent = curr*100/max; + + info->battery_life = percent; + + info->ac_line_status = palmz72_ac_is_connected() ? APM_AC_ONLINE : APM_AC_OFFLINE; + + if (info->ac_line_status) { + info->battery_status = APM_BATTERY_STATUS_CHARGING; + } else { + if (percent > 50) + info->battery_status = APM_BATTERY_STATUS_HIGH; + else if (percent < 5) + info->battery_status = APM_BATTERY_STATUS_CRITICAL; + else + info->battery_status = APM_BATTERY_STATUS_LOW; + } + + info->time = percent * PALMZ72_MAX_LIFE_MINS/100; + info->units = APM_UNITS_MINS; +} + +typedef void (*apm_get_power_status_t)(struct apm_power_info*); + +int set_apm_get_power_status(apm_get_power_status_t t) +{ + apm_get_power_status = t; + + return 0; +} + + +/* end of APM implementing */ + + + +static int __init palmz72_ac97_init(void) +{ + +/* register battery to APM layer */ + + if(battery_class_register(&palmz72_battery)) { + printk(KERN_ERR "palmz72_ac97_probe: could not register battery class\n"); + } + else{ + battery_registered = 1; + printk("Battery registered\n"); + } +#ifdef CONFIG_PM + apm_get_power_status = palmz72_apm_get_power_status; + return 0; +#endif + +} + + +static void __exit palmz72_ac97_exit(void) +{ + driver_unregister(&palmz72_battery); +} + + +module_init(palmz72_ac97_init); +module_exit(palmz72_ac97_exit); + +MODULE_AUTHOR ("Jan Herman <2h...@se...>"); +MODULE_DESCRIPTION ("WM9712 battery support for PalmOne Zire 72"); +MODULE_LICENSE ("GPL"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |