aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-sa1100/jornada720_apm.c
blob: 05736a6b7fbb2d9b805f45f7a4b7a6a22beaad46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * arch/arm/mach-sa1100/jornada720_apm.c
 *
 * HP Jornada 710/720/728 battery detection (apm) platform driver
 *
 * Copyright (C) 2007,2008 Kristoffer Ericson <Kristoffer.Ericson@gmail.com>
 *  Copyright (C) 2006 Filip Zyzniewski <filip.zyzniewski@tefnet.pl>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 (or later) as
 * published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/apm-emulation.h>

#include <asm/hardware.h>
#include <asm/arch/jornada720.h>

MODULE_AUTHOR("Filip Zyzniewski <filip.zyzniewski@tefnet.pl>, Kristoffer Ericson <kristoffer.ericson@gmail.com>");
MODULE_DESCRIPTION("HP Jornada 710/720/728 battery status reporting");
MODULE_LICENSE("GPL");

/**
 * voltage && detection
 */
#define max_voltage 670					/* max voltage without ac power */
#define min_voltage 430					/* min voltage */
#define ac_connected()   ((GPLR & GPIO_GPIO4) ? 0 : 1)	/* is ac connected? */
#define ac_charging()    (! (GPLR & GPIO_GPIO26) )	/* are we charging? */

/**
 * battery calculations
 */
#define main_diff        (max_voltage - min_voltage)	/* just to keep defines small */
#define main_ac_coeff    100 / 105			/* correcting battery values when with ac */
#define main_divisor     (main_diff * main_diff) / 100	/* battery power to percent */
#define main_lin_corr    (main_diff * main_diff) / 2	/* adjusting for non-linearity */

int jornada720_apm_get_battery_raw(int battnum)
{
	unsigned char low_byte, high_byte, msb;

	jornada_ssp_start();

	/* if ssp connection fails we bail out */
	if(jornada_ssp_inout(GETBATTERYDATA) < 0) {
	    printk(KERN_WARNING "APM: Failed trying to aquire battery data \n");
	    jornada_ssp_end();
	    return -1;
	}

	low_byte  = jornada_ssp_inout(TXDUMMY);		/* backup battery value */
	high_byte = jornada_ssp_inout(TXDUMMY);		/* main battery value */
	msb = jornada_ssp_inout(TXDUMMY);		/* status */

	jornada_ssp_end();

	/* main battery */
	if (battnum) {
		if ((msb & 0x03) == 0x03) return -1;	/* main battery absent */
		return ((msb & 0x03) << 8) + low_byte;	/* wrapping values */
	}
	/* backup battery */
	else {
		if ((msb & 0x0c) == 0x00) return -1;	/* backup battery abset */
		return ((msb & 0x0c) << 6) + high_byte;	/* wrapping values */
	}
}

int jornada720_apm_get_battery(int battnum)
{
	int ret = jornada720_apm_get_battery_raw(battnum);

	if (ret == -1)
		return ret;

	/* main battery only, cannot calculate backup battery */
	if (battnum) {
		ret -= min_voltage;			/* we want 0 for fully drained battery */

		/* trying to get values more linear */
		ret *= ret;
		if (ret > 37000)
		    ret = ret * 3/2 - main_lin_corr;
		else
		    ret = ret * 7/10;

		ret /= main_divisor;			/* 0-100% range */

		if (ac_connected())			/* adjusting for ac fluctuations */
		    ret = ret * main_ac_coeff;

		if (ret > 100) 
		    ret = 100;			/* should never report above 100% */
	}
	return ret;
}

static void jornada720_apm_get_power_status(struct apm_power_info *info)
{

	info->battery_life = jornada720_apm_get_battery(1); /* main battery */

	if (info->battery_life == -1) {
		info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;
		info->battery_flag = APM_BATTERY_FLAG_NOT_PRESENT;

	} else if (info->battery_life < 30) {
		info->battery_status = APM_BATTERY_STATUS_LOW;
		info->battery_flag = APM_BATTERY_FLAG_LOW;

	} else if (info->battery_life < 5) {
		info->battery_status = APM_BATTERY_STATUS_CRITICAL;
		info->battery_flag = APM_BATTERY_FLAG_CRITICAL;

	} else {
		info->battery_status = APM_BATTERY_STATUS_HIGH;
		info->battery_flag = APM_BATTERY_FLAG_HIGH;
	}

	if (ac_charging())
		info->battery_status = APM_BATTERY_STATUS_CHARGING;

	info->ac_line_status = ac_connected();
}

static int __devinit jornada720_apm_probe(struct platform_device *pdev)
{
	printk(KERN_INFO "jornada720_apm: Initializing\n");
	
	apm_get_power_status = jornada720_apm_get_power_status;
	return 0;
}

static int __devexit jornada720_apm_remove(struct platform_device *pdev)
{
	if (apm_get_power_status == jornada720_apm_get_power_status)
		apm_get_power_status=NULL;
	return 0;
}

static struct platform_driver jornada720_apm_driver = {
	.driver = {
		.name	= "jornada_apm",
	},
	.probe	= jornada720_apm_probe,
	.remove = __devexit_p(jornada720_apm_remove),
};

static int __init jornada720_apm_init(void)
{
	return platform_driver_register(&jornada720_apm_driver);
}

static void __exit jornada720_apm_exit(void) {
	platform_driver_unregister(&jornada720_apm_driver);
}

module_init(jornada720_apm_init);
module_exit(jornada720_apm_exit);