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>
25 #include <linux/gpio.h>
28 #include <mach/hardware.h>
29 #include <asm/hardware/scoop.h>
31 #include <mach/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 gpio_request(COLLIE_GPIO_CHARGE_ON, "charge on");
63 gpio_direction_output(COLLIE_GPIO_CHARGE_ON, 1);
65 ucb1x00_io_set_dir(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON | COLLIE_TC35143_GPIO_TMP_ON |
66 COLLIE_TC35143_GPIO_BBAT_ON);
70 static void collie_measure_temp(int on)
73 ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
75 ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
78 static void collie_charge(int on)
80 /* Zaurus seems to contain LTC1731; it should know when to
81 * stop charging itself, so setting charge on should be
82 * relatively harmless (as long as it is not done too often).
84 gpio_set_value(COLLIE_GPIO_CHARGE_ON, on);
87 static void collie_discharge(int on)
91 static void collie_discharge1(int on)
95 static void collie_presuspend(void)
99 static void collie_postsuspend(void)
103 static int collie_should_wakeup(unsigned int resume_on_alarm)
108 static unsigned long collie_charger_wakeup(void)
113 int collie_read_backup_battery(void)
117 ucb1x00_adc_enable(ucb);
119 ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_BBAT_ON, 0);
120 voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
122 ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
123 ucb1x00_adc_disable(ucb);
125 printk("Backup battery = %d(%d)\n", ADCtoPower(voltage), voltage);
127 return ADCtoPower(voltage);
130 int collie_read_main_battery(void)
132 int voltage, voltage_rev, voltage_volts;
134 ucb1x00_adc_enable(ucb);
135 ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
136 ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_MBAT_ON, 0);
139 voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
141 ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON);
142 ucb1x00_adc_disable(ucb);
144 voltage_rev = voltage + ((ad_revise * voltage) / 652);
145 voltage_volts = ADCtoPower(voltage_rev);
147 printk("Main battery = %d(%d)\n", voltage_volts, voltage);
150 return voltage_volts;
155 int collie_read_temp(void)
159 /* According to Sharp, temp must be > 973, main battery must be < 465,
160 FIXME: sharpsl_pm.c has both conditions negated? FIXME: values
161 are way out of range? */
163 ucb1x00_adc_enable(ucb);
164 ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
165 /* >1010 = battery removed, 460 = 22C ?, higher = lower temp ? */
166 voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD0, UCB_SYNC);
167 ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
168 ucb1x00_adc_disable(ucb);
170 printk("Battery temp = %d\n", voltage);
174 static unsigned long read_devdata(int which)
177 case SHARPSL_BATT_VOLT:
178 return collie_read_main_battery();
179 case SHARPSL_BATT_TEMP:
180 return collie_read_temp();
181 case SHARPSL_ACIN_VOLT:
183 case SHARPSL_STATUS_ACIN: {
184 int ret = GPLR & COLLIE_GPIO_AC_IN;
185 printk("AC status = %d\n", ret);
188 case SHARPSL_STATUS_FATAL: {
189 int ret = GPLR & COLLIE_GPIO_MAIN_BAT_LOW;
190 printk("Fatal bat = %d\n", ret);
198 struct battery_thresh collie_battery_levels_acin[] = {
213 struct battery_thresh collie_battery_levels[] = {
218 { 368, 75}, /* From sharp code: battery high with frontlight */
219 { 366, 70}, /* 60..90 -- fake values invented by me for testing */
223 { 358, 25}, /* From sharp code: battery low with frontlight */
224 { 356, 5}, /* From sharp code: battery verylow with frontlight */
228 struct sharpsl_charger_machinfo collie_pm_machinfo = {
229 .init = collie_charger_init,
230 .read_devdata = read_devdata,
231 .discharge = collie_discharge,
232 .discharge1 = collie_discharge1,
233 .charge = collie_charge,
234 .measure_temp = collie_measure_temp,
235 .presuspend = collie_presuspend,
236 .postsuspend = collie_postsuspend,
237 .charger_wakeup = collie_charger_wakeup,
238 .should_wakeup = collie_should_wakeup,
240 .bat_levels_noac = collie_battery_levels,
241 .bat_levels_acin = collie_battery_levels_acin,
242 .status_high_acin = 368,
243 .status_low_acin = 358,
244 .status_high_noac = 368,
245 .status_low_noac = 358,
246 .charge_on_volt = 350, /* spitz uses 2.90V, but lets play it safe. */
247 .charge_on_temp = 550,
248 .charge_acin_high = 550, /* collie does not seem to have sensor for this, anyway */
249 .charge_acin_low = 450, /* ignored, too */
250 .fatal_acin_volt = 356,
251 .fatal_noacin_volt = 356,
253 .batfull_irq = 1, /* We do not want periodical charge restarts */
256 static int __init collie_pm_ucb_add(struct ucb1x00_dev *pdev)
258 sharpsl_pm.machinfo = &collie_pm_machinfo;
263 static struct ucb1x00_driver collie_pm_ucb_driver = {
264 .add = collie_pm_ucb_add,
267 static struct platform_device *collie_pm_device;
269 static int __init collie_pm_init(void)
273 collie_pm_device = platform_device_alloc("sharpsl-pm", -1);
274 if (!collie_pm_device)
277 collie_pm_device->dev.platform_data = &collie_pm_machinfo;
278 ret = platform_device_add(collie_pm_device);
281 platform_device_put(collie_pm_device);
284 ret = ucb1x00_register_driver(&collie_pm_ucb_driver);
289 static void __exit collie_pm_exit(void)
291 ucb1x00_unregister_driver(&collie_pm_ucb_driver);
292 platform_device_unregister(collie_pm_device);
295 module_init(collie_pm_init);
296 module_exit(collie_pm_exit);