sync hh.org
[hh.org.git] / arch / arm / mach-pxa / himalaya / himalaya_battery.c
blob97e2271375c76279a5a3ae838225061b1c74e3b4
1 /*
2 * Driver interface to the battery on the HTC Himalaya
4 * Use consistent with the GNU GPL is permitted,
5 * provided that this copyright notice is
6 * preserved in its entirety in all copies and derived works.
8 */
10 #include <linux/module.h>
11 #include <linux/version.h>
12 #include <linux/config.h>
14 #include <asm/arch/hardware.h>
15 #include <asm/arch/pxa-regs.h>
16 #include <linux/battery.h>
18 #include "tsc2200.h"
19 #include "pxa_nssp.h"
21 static void bat_tsc2200_start_conv_ps(void)
23 tsc2200_write(TSC2200_CTRLREG_REF, 31);
24 tsc2200_write(TSC2200_CTRLREG_ADC,
25 TSC2200_CTRLREG_ADC_AD3 |
26 TSC2200_CTRLREG_ADC_AD1 |
27 TSC2200_CTRLREG_ADC_AD0 |
28 TSC2200_CTRLREG_ADC_RES (TSC2200_CTRLREG_ADC_RES_12BITP) |
29 TSC2200_CTRLREG_ADC_AVG (TSC2200_CTRLREG_ADC_8AVG) |
30 TSC2200_CTRLREG_ADC_CL (TSC2200_CTRLREG_ADC_CL_2MHZ_12BIT) |
31 TSC2200_CTRLREG_ADC_PV (TSC2200_CTRLREG_ADC_PV_500uS) );
34 static void bat_tsc2200_start_conv_temp(void)
36 tsc2200_write(TSC2200_CTRLREG_ADC,
37 TSC2200_CTRLREG_ADC_AD3 |
38 TSC2200_CTRLREG_ADC_AD1 |
39 TSC2200_CTRLREG_ADC_RES (TSC2200_CTRLREG_ADC_RES_12BITP) |
40 TSC2200_CTRLREG_ADC_AVG (TSC2200_CTRLREG_ADC_8AVG) |
41 TSC2200_CTRLREG_ADC_CL (TSC2200_CTRLREG_ADC_CL_2MHZ_12BIT) |
42 TSC2200_CTRLREG_ADC_PV (TSC2200_CTRLREG_ADC_PV_500uS) );
45 static int himalaya_battery_get_status(struct battery *bat)
47 printk("%s: in.\n", __FUNCTION__);
49 if ( GPLR(63) & GPIO_bit(63) ) {
50 return BATTERY_STATUS_CHARGING;
51 } else {
52 return BATTERY_STATUS_NOT_CHARGING;
56 static int himalaya_battery_get_voltage(struct battery *bat)
58 int retval;
60 printk("%s: in.\n", __FUNCTION__);
62 down_interruptible(&tsc2200_sem);
63 bat_tsc2200_start_conv_ps();
65 while ( !(tsc2200_read(TSC2200_CTRLREG_ADC) & 0x4000) &&
66 !tsc2200_dav()) {
67 printk("1: %X\n", tsc2200_read(TSC2200_CTRLREG_ADC));
68 msleep(1);
71 retval = tsc2200_read(TSC2200_DATAREG_BAT1);
72 printk("%s: BAT1: %03X\nBAT2: %03x\nAUX1: %03x\nAUX2: %03x\nTEMP1: %03x\nTEMP2: %03x\n", __FUNCTION__, tsc2200_read(TSC2200_DATAREG_BAT1), tsc2200_read(TSC2200_DATAREG_BAT2), tsc2200_read(TSC2200_DATAREG_AUX1), tsc2200_read(TSC2200_DATAREG_AUX2), tsc2200_read(TSC2200_DATAREG_TEMP1), tsc2200_read(TSC2200_DATAREG_TEMP2));
74 up(&tsc2200_sem);
75 return retval;
78 static int himalaya_battery_get_temp(struct battery *bat)
80 int retval;
82 printk("%s: in.\n", __FUNCTION__);
84 down_interruptible(&tsc2200_sem);
85 bat_tsc2200_start_conv_temp();
87 while ( !(tsc2200_read(TSC2200_CTRLREG_ADC) & 0x4000) &&
88 !tsc2200_dav()) {
89 printk("1: %X\n", tsc2200_read(TSC2200_CTRLREG_ADC));
90 msleep(1);
93 retval = tsc2200_read(TSC2200_DATAREG_TEMP1);
95 up(&tsc2200_sem);
96 return retval;
99 struct battery battery_dev = {
100 .name = "main battery",
101 .id = "battery0",
102 .get_voltage = himalaya_battery_get_voltage,
103 .get_status = himalaya_battery_get_status,
104 .get_temp = himalaya_battery_get_temp,
107 static struct device bat_tsc2200_dev = {
108 .parent = &nssp_bus,
109 .bus = &nssp_bus_type,
110 .bus_id = "bat_tsc2200",
113 static int bat_setup(void)
115 device_register(&bat_tsc2200_dev);
116 battery_class_register(&battery_dev);
118 return 0;
121 static int __init bat_init(void)
123 bat_setup();
125 return 0;
128 static void __exit bat_exit(void)
130 battery_class_unregister(&battery_dev);
131 device_unregister(&bat_tsc2200_dev);
134 module_init(bat_init)
135 module_exit(bat_exit)
137 MODULE_AUTHOR("Matthias Burghardt");
138 MODULE_DESCRIPTION("Battery (TI TSC2200) support for the HTC Himalaya");