2 * Based on spitz_pm.c and sharp code.
4 * Copyright (C) 2001 SHARP
5 * Copyright 2005 Pavel Machek <pavel@suse.cz>
7 * Distribute under GPLv2.
9 * Li-ion batteries are angry beasts, and they like to explode. This driver is not finished,
10 * and sometimes charges them when it should not. If it makes angry lithium to come your way...
11 * ...well, you have been warned.
13 * Actually, this should be quite safe, it seems sharp leaves charger enabled by default,
14 * and my collie did not explode (yet).
17 #include <linux/module.h>
18 #include <linux/stat.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
27 #include <asm/mach-types.h>
28 #include <asm/hardware.h>
29 #include <asm/hardware/scoop.h>
31 #include <asm/arch/collie.h>
32 #include <asm/mach/sharpsl_param.h>
33 #include <asm/hardware/sharpsl_pm.h>
35 #include "../drivers/mfd/ucb1x00.h"
37 static struct ucb1x00
*ucb
;
40 #define ADCtoPower(x) ((330 * x * 2) / 1024)
42 static void collie_charger_init(void)
46 if (sharpsl_param
.adadj
!= -1)
47 ad_revise
= sharpsl_param
.adadj
;
49 /* Register interrupt handler. */
50 if ((err
= request_irq(COLLIE_IRQ_GPIO_AC_IN
, sharpsl_ac_isr
, IRQF_DISABLED
,
51 "ACIN", sharpsl_ac_isr
))) {
52 printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_AC_IN
);
55 if ((err
= request_irq(COLLIE_IRQ_GPIO_CO
, sharpsl_chrg_full_isr
, IRQF_DISABLED
,
56 "CO", sharpsl_chrg_full_isr
))) {
57 free_irq(COLLIE_IRQ_GPIO_AC_IN
, sharpsl_ac_isr
);
58 printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_CO
);
62 ucb1x00_io_set_dir(ucb
, 0, COLLIE_TC35143_GPIO_MBAT_ON
| COLLIE_TC35143_GPIO_TMP_ON
|
63 COLLIE_TC35143_GPIO_BBAT_ON
);
67 static void collie_measure_temp(int on
)
70 ucb1x00_io_write(ucb
, COLLIE_TC35143_GPIO_TMP_ON
, 0);
72 ucb1x00_io_write(ucb
, 0, COLLIE_TC35143_GPIO_TMP_ON
);
75 static void collie_charge(int on
)
77 extern struct platform_device colliescoop_device
;
79 /* Zaurus seems to contain LTC1731; it should know when to
80 * stop charging itself, so setting charge on should be
81 * relatively harmless (as long as it is not done too often).
84 set_scoop_gpio(&colliescoop_device
.dev
, COLLIE_SCP_CHARGE_ON
);
86 reset_scoop_gpio(&colliescoop_device
.dev
, COLLIE_SCP_CHARGE_ON
);
90 static void collie_discharge(int on
)
94 static void collie_discharge1(int on
)
98 static void collie_presuspend(void)
102 static void collie_postsuspend(void)
106 static int collie_should_wakeup(unsigned int resume_on_alarm
)
111 static unsigned long collie_charger_wakeup(void)
116 int collie_read_backup_battery(void)
120 ucb1x00_adc_enable(ucb
);
122 ucb1x00_io_write(ucb
, COLLIE_TC35143_GPIO_BBAT_ON
, 0);
123 voltage
= ucb1x00_adc_read(ucb
, UCB_ADC_INP_AD1
, UCB_SYNC
);
125 ucb1x00_io_write(ucb
, 0, COLLIE_TC35143_GPIO_BBAT_ON
);
126 ucb1x00_adc_disable(ucb
);
128 printk("Backup battery = %d(%d)\n", ADCtoPower(voltage
), voltage
);
130 return ADCtoPower(voltage
);
133 int collie_read_main_battery(void)
135 int voltage
, voltage_rev
, voltage_volts
;
137 ucb1x00_adc_enable(ucb
);
138 ucb1x00_io_write(ucb
, 0, COLLIE_TC35143_GPIO_BBAT_ON
);
139 ucb1x00_io_write(ucb
, COLLIE_TC35143_GPIO_MBAT_ON
, 0);
142 voltage
= ucb1x00_adc_read(ucb
, UCB_ADC_INP_AD1
, UCB_SYNC
);
144 ucb1x00_io_write(ucb
, 0, COLLIE_TC35143_GPIO_MBAT_ON
);
145 ucb1x00_adc_disable(ucb
);
147 voltage_rev
= voltage
+ ((ad_revise
* voltage
) / 652);
148 voltage_volts
= ADCtoPower(voltage_rev
);
150 printk("Main battery = %d(%d)\n", voltage_volts
, voltage
);
153 return voltage_volts
;
158 int collie_read_temp(void)
162 /* According to Sharp, temp must be > 973, main battery must be < 465,
163 FIXME: sharpsl_pm.c has both conditions negated? FIXME: values
164 are way out of range? */
166 ucb1x00_adc_enable(ucb
);
167 ucb1x00_io_write(ucb
, COLLIE_TC35143_GPIO_TMP_ON
, 0);
168 /* >1010 = battery removed, 460 = 22C ?, higher = lower temp ? */
169 voltage
= ucb1x00_adc_read(ucb
, UCB_ADC_INP_AD0
, UCB_SYNC
);
170 ucb1x00_io_write(ucb
, 0, COLLIE_TC35143_GPIO_TMP_ON
);
171 ucb1x00_adc_disable(ucb
);
173 printk("Battery temp = %d\n", voltage
);
177 static unsigned long read_devdata(int which
)
180 case SHARPSL_BATT_VOLT
:
181 return collie_read_main_battery();
182 case SHARPSL_BATT_TEMP
:
183 return collie_read_temp();
184 case SHARPSL_ACIN_VOLT
:
186 case SHARPSL_STATUS_ACIN
: {
187 int ret
= GPLR
& COLLIE_GPIO_AC_IN
;
188 printk("AC status = %d\n", ret
);
191 case SHARPSL_STATUS_FATAL
: {
192 int ret
= GPLR
& COLLIE_GPIO_MAIN_BAT_LOW
;
193 printk("Fatal bat = %d\n", ret
);
201 struct battery_thresh collie_battery_levels_acin
[] = {
216 struct battery_thresh collie_battery_levels
[] = {
221 { 368, 75}, /* From sharp code: battery high with frontlight */
222 { 366, 70}, /* 60..90 -- fake values invented by me for testing */
226 { 358, 25}, /* From sharp code: battery low with frontlight */
227 { 356, 5}, /* From sharp code: battery verylow with frontlight */
231 struct sharpsl_charger_machinfo collie_pm_machinfo
= {
232 .init
= collie_charger_init
,
233 .read_devdata
= read_devdata
,
234 .discharge
= collie_discharge
,
235 .discharge1
= collie_discharge1
,
236 .charge
= collie_charge
,
237 .measure_temp
= collie_measure_temp
,
238 .presuspend
= collie_presuspend
,
239 .postsuspend
= collie_postsuspend
,
240 .charger_wakeup
= collie_charger_wakeup
,
241 .should_wakeup
= collie_should_wakeup
,
243 .bat_levels_noac
= collie_battery_levels
,
244 .bat_levels_acin
= collie_battery_levels_acin
,
245 .status_high_acin
= 368,
246 .status_low_acin
= 358,
247 .status_high_noac
= 368,
248 .status_low_noac
= 358,
249 .charge_on_volt
= 350, /* spitz uses 2.90V, but lets play it safe. */
250 .charge_on_temp
= 550,
251 .charge_acin_high
= 550, /* collie does not seem to have sensor for this, anyway */
252 .charge_acin_low
= 450, /* ignored, too */
253 .fatal_acin_volt
= 356,
254 .fatal_noacin_volt
= 356,
256 .batfull_irq
= 1, /* We do not want periodical charge restarts */
259 static int __init
collie_pm_ucb_add(struct ucb1x00_dev
*pdev
)
261 sharpsl_pm
.machinfo
= &collie_pm_machinfo
;
266 static struct ucb1x00_driver collie_pm_ucb_driver
= {
267 .add
= collie_pm_ucb_add
,
270 static struct platform_device
*collie_pm_device
;
272 static int __init
collie_pm_init(void)
276 collie_pm_device
= platform_device_alloc("sharpsl-pm", -1);
277 if (!collie_pm_device
)
280 collie_pm_device
->dev
.platform_data
= &collie_pm_machinfo
;
281 ret
= platform_device_add(collie_pm_device
);
284 platform_device_put(collie_pm_device
);
287 ret
= ucb1x00_register_driver(&collie_pm_ucb_driver
);
292 static void __exit
collie_pm_exit(void)
294 ucb1x00_unregister_driver(&collie_pm_ucb_driver
);
295 platform_device_unregister(collie_pm_device
);
298 module_init(collie_pm_init
);
299 module_exit(collie_pm_exit
);