aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm
diff options
context:
space:
mode:
authorRichard Purdie <rpurdie@rpsys.net>2006-01-05 15:44:55 -0500
committerRussell King <rmk+kernel@arm.linux.org.uk>2006-01-05 15:44:55 -0500
commitb7557de41a04346cb545d4dda7088760cb96e713 (patch)
treedf9acef34fdf1460858ed39b35d447f6bfdc95d1 /arch/arm
parent3125c68d70e3433c21234431a9df9e7336efa29f (diff)
[ARM] 3228/1: SharpSL: Move PM code to arch/arm/common
Patch from Richard Purdie This patch moves a large chunk of the sharpsl_pm driver to arch/arm/common so that it can be reused on other devices such as the SL-5500 (collie). It also abstracts some functions from the core into the machine and platform specific parts of the driver to aid reuse. Signed-off-by: Richard Purdie <rpurdie@rpsys.net> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
Diffstat (limited to 'arch/arm')
-rw-r--r--arch/arm/common/Kconfig3
-rw-r--r--arch/arm/common/Makefile1
-rw-r--r--arch/arm/common/sharpsl_pm.c839
-rw-r--r--arch/arm/mach-pxa/Kconfig2
-rw-r--r--arch/arm/mach-pxa/corgi_pm.c45
-rw-r--r--arch/arm/mach-pxa/sharpsl.h100
-rw-r--r--arch/arm/mach-pxa/sharpsl_pm.c825
-rw-r--r--arch/arm/mach-pxa/spitz_pm.c47
8 files changed, 921 insertions, 941 deletions
diff --git a/arch/arm/common/Kconfig b/arch/arm/common/Kconfig
index 666ba393575b..d7509c7a3c5e 100644
--- a/arch/arm/common/Kconfig
+++ b/arch/arm/common/Kconfig
@@ -23,5 +23,8 @@ config SHARP_LOCOMO
23config SHARP_PARAM 23config SHARP_PARAM
24 bool 24 bool
25 25
26config SHARPSL_PM
27 bool
28
26config SHARP_SCOOP 29config SHARP_SCOOP
27 bool 30 bool
diff --git a/arch/arm/common/Makefile b/arch/arm/common/Makefile
index a87886564b19..2685051b3013 100644
--- a/arch/arm/common/Makefile
+++ b/arch/arm/common/Makefile
@@ -13,4 +13,5 @@ obj-$(CONFIG_DMABOUNCE) += dmabounce.o
13obj-$(CONFIG_TIMER_ACORN) += time-acorn.o 13obj-$(CONFIG_TIMER_ACORN) += time-acorn.o
14obj-$(CONFIG_SHARP_LOCOMO) += locomo.o 14obj-$(CONFIG_SHARP_LOCOMO) += locomo.o
15obj-$(CONFIG_SHARP_PARAM) += sharpsl_param.o 15obj-$(CONFIG_SHARP_PARAM) += sharpsl_param.o
16obj-$(CONFIG_SHARPSL_PM) += sharpsl_pm.o
16obj-$(CONFIG_SHARP_SCOOP) += scoop.o 17obj-$(CONFIG_SHARP_SCOOP) += scoop.o
diff --git a/arch/arm/common/sharpsl_pm.c b/arch/arm/common/sharpsl_pm.c
new file mode 100644
index 000000000000..978d32e82d39
--- /dev/null
+++ b/arch/arm/common/sharpsl_pm.c
@@ -0,0 +1,839 @@
1/*
2 * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
3 * series of PDAs
4 *
5 * Copyright (c) 2004-2005 Richard Purdie
6 *
7 * Based on code written by Sharp for 2.4 kernels
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#undef DEBUG
16
17#include <linux/module.h>
18#include <linux/timer.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/apm_bios.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/platform_device.h>
25
26#include <asm/hardware.h>
27#include <asm/mach-types.h>
28#include <asm/irq.h>
29#include <asm/apm.h>
30#include <asm/arch/pm.h>
31#include <asm/arch/pxa-regs.h>
32#include <asm/arch/sharpsl.h>
33#include <asm/hardware/sharpsl_pm.h>
34
35/*
36 * Constants
37 */
38#define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */
39#define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */
40#define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */
41#define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */
42#define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */
43#define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */
44#define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */
45#define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */
46#define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */
47#define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */
48#define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */
49#define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */
50
51#define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
52#define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
53#define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
54#define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
55#define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
56#define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
57
58/*
59 * Prototypes
60 */
61static int sharpsl_off_charge_battery(void);
62static int sharpsl_check_battery_temp(void);
63static int sharpsl_check_battery_voltage(void);
64static int sharpsl_ac_check(void);
65static int sharpsl_fatal_check(void);
66static int sharpsl_average_value(int ad);
67static void sharpsl_average_clear(void);
68static void sharpsl_charge_toggle(void *private_);
69static void sharpsl_battery_thread(void *private_);
70
71
72/*
73 * Variables
74 */
75struct sharpsl_pm_status sharpsl_pm;
76DECLARE_WORK(toggle_charger, sharpsl_charge_toggle, NULL);
77DECLARE_WORK(sharpsl_bat, sharpsl_battery_thread, NULL);
78
79
80static int get_percentage(int voltage)
81{
82 int i = sharpsl_pm.machinfo->bat_levels - 1;
83 struct battery_thresh *thresh;
84
85 if (sharpsl_pm.charge_mode == CHRG_ON)
86 thresh=sharpsl_pm.machinfo->bat_levels_acin;
87 else
88 thresh=sharpsl_pm.machinfo->bat_levels_noac;
89
90 while (i > 0 && (voltage > thresh[i].voltage))
91 i--;
92
93 return thresh[i].percentage;
94}
95
96static int get_apm_status(int voltage)
97{
98 int low_thresh, high_thresh;
99
100 if (sharpsl_pm.charge_mode == CHRG_ON) {
101 high_thresh = sharpsl_pm.machinfo->status_high_acin;
102 low_thresh = sharpsl_pm.machinfo->status_low_acin;
103 } else {
104 high_thresh = sharpsl_pm.machinfo->status_high_noac;
105 low_thresh = sharpsl_pm.machinfo->status_low_noac;
106 }
107
108 if (voltage >= high_thresh)
109 return APM_BATTERY_STATUS_HIGH;
110 if (voltage >= low_thresh)
111 return APM_BATTERY_STATUS_LOW;
112 return APM_BATTERY_STATUS_CRITICAL;
113}
114
115void sharpsl_battery_kick(void)
116{
117 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
118}
119EXPORT_SYMBOL(sharpsl_battery_kick);
120
121
122static void sharpsl_battery_thread(void *private_)
123{
124 int voltage, percent, apm_status, i = 0;
125
126 if (!sharpsl_pm.machinfo)
127 return;
128
129 sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
130
131 /* Corgi cannot confirm when battery fully charged so periodically kick! */
132 if (machine_is_corgi() && (sharpsl_pm.charge_mode == CHRG_ON)
133 && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL))
134 schedule_work(&toggle_charger);
135
136 while(1) {
137 voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
138
139 if (voltage > 0) break;
140 if (i++ > 5) {
141 voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
142 dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
143 break;
144 }
145 }
146
147 voltage = sharpsl_average_value(voltage);
148 apm_status = get_apm_status(voltage);
149 percent = get_percentage(voltage);
150
151 /* At low battery voltages, the voltage has a tendency to start
152 creeping back up so we try to avoid this here */
153 if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) || percent <= sharpsl_pm.battstat.mainbat_percent) {
154 sharpsl_pm.battstat.mainbat_voltage = voltage;
155 sharpsl_pm.battstat.mainbat_status = apm_status;
156 sharpsl_pm.battstat.mainbat_percent = percent;
157 }
158
159 dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %d\n", voltage,
160 sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
161
162 /* If battery is low. limit backlight intensity to save power. */
163 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
164 && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) ||
165 (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) {
166 if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) {
167 corgibl_limit_intensity(1);
168 sharpsl_pm.flags |= SHARPSL_BL_LIMIT;
169 }
170 } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) {
171 corgibl_limit_intensity(0);
172 sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT;
173 }
174
175 /* Suspend if critical battery level */
176 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
177 && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
178 && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
179 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
180 dev_err(sharpsl_pm.dev, "Fatal Off\n");
181 apm_queue_event(APM_CRITICAL_SUSPEND);
182 }
183
184 schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
185}
186
187void sharpsl_pm_led(int val)
188{
189 if (val == SHARPSL_LED_ERROR) {
190 dev_err(sharpsl_pm.dev, "Charging Error!\n");
191 } else if (val == SHARPSL_LED_ON) {
192 dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
193
194 } else {
195 dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
196
197 }
198}
199
200static void sharpsl_charge_on(void)
201{
202 dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
203
204 sharpsl_pm.full_count = 0;
205 sharpsl_pm.charge_mode = CHRG_ON;
206 schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
207 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
208}
209
210static void sharpsl_charge_off(void)
211{
212 dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
213
214 sharpsl_pm.machinfo->charge(0);
215 sharpsl_pm_led(SHARPSL_LED_OFF);
216 sharpsl_pm.charge_mode = CHRG_OFF;
217
218 schedule_work(&sharpsl_bat);
219}
220
221static void sharpsl_charge_error(void)
222{
223 sharpsl_pm_led(SHARPSL_LED_ERROR);
224 sharpsl_pm.machinfo->charge(0);
225 sharpsl_pm.charge_mode = CHRG_ERROR;
226}
227
228static void sharpsl_charge_toggle(void *private_)
229{
230 dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies);
231
232 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
233 sharpsl_charge_off();
234 return;
235 } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
236 sharpsl_charge_error();
237 return;
238 }
239
240 sharpsl_pm_led(SHARPSL_LED_ON);
241 sharpsl_pm.machinfo->charge(0);
242 mdelay(SHARPSL_CHARGE_WAIT_TIME);
243 sharpsl_pm.machinfo->charge(1);
244
245 sharpsl_pm.charge_start_time = jiffies;
246}
247
248static void sharpsl_ac_timer(unsigned long data)
249{
250 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
251
252 dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin);
253
254 sharpsl_average_clear();
255 if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
256 sharpsl_charge_on();
257 else if (sharpsl_pm.charge_mode == CHRG_ON)
258 sharpsl_charge_off();
259
260 schedule_work(&sharpsl_bat);
261}
262
263
264irqreturn_t sharpsl_ac_isr(int irq, void *dev_id, struct pt_regs *fp)
265{
266 /* Delay the event slightly to debounce */
267 /* Must be a smaller delay than the chrg_full_isr below */
268 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
269
270 return IRQ_HANDLED;
271}
272
273static void sharpsl_chrg_full_timer(unsigned long data)
274{
275 dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
276
277 sharpsl_pm.full_count++;
278
279 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
280 dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
281 if (sharpsl_pm.charge_mode == CHRG_ON)
282 sharpsl_charge_off();
283 } else if (sharpsl_pm.full_count < 2) {
284 dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
285 schedule_work(&toggle_charger);
286 } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
287 dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
288 schedule_work(&toggle_charger);
289 } else {
290 sharpsl_charge_off();
291 sharpsl_pm.charge_mode = CHRG_DONE;
292 dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
293 }
294}
295
296/* Charging Finished Interrupt (Not present on Corgi) */
297/* Can trigger at the same time as an AC staus change so
298 delay until after that has been processed */
299irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id, struct pt_regs *fp)
300{
301 if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
302 return IRQ_HANDLED;
303
304 /* delay until after any ac interrupt */
305 mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
306
307 return IRQ_HANDLED;
308}
309
310irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id, struct pt_regs *fp)
311{
312 int is_fatal = 0;
313
314 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
315 dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
316 is_fatal = 1;
317 }
318
319 if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
320 dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
321 is_fatal = 1;
322 }
323
324 if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
325 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
326 apm_queue_event(APM_CRITICAL_SUSPEND);
327 }
328
329 return IRQ_HANDLED;
330}
331
332/*
333 * Maintain an average of the last 10 readings
334 */
335#define SHARPSL_CNV_VALUE_NUM 10
336static int sharpsl_ad_index;
337
338static void sharpsl_average_clear(void)
339{
340 sharpsl_ad_index = 0;
341}
342
343static int sharpsl_average_value(int ad)
344{
345 int i, ad_val = 0;
346 static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
347
348 if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
349 sharpsl_ad_index = 0;
350 return ad;
351 }
352
353 sharpsl_ad[sharpsl_ad_index] = ad;
354 sharpsl_ad_index++;
355 if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
356 for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
357 sharpsl_ad[i] = sharpsl_ad[i+1];
358 sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
359 }
360 for (i=0; i < sharpsl_ad_index; i++)
361 ad_val += sharpsl_ad[i];
362
363 return (ad_val / sharpsl_ad_index);
364}
365
366/*
367 * Take an array of 5 integers, remove the maximum and minimum values
368 * and return the average.
369 */
370static int get_select_val(int *val)
371{
372 int i, j, k, temp, sum = 0;
373
374 /* Find MAX val */
375 temp = val[0];
376 j=0;
377 for (i=1; i<5; i++) {
378 if (temp < val[i]) {
379 temp = val[i];
380 j = i;
381 }
382 }
383
384 /* Find MIN val */
385 temp = val[4];
386 k=4;
387 for (i=3; i>=0; i--) {
388 if (temp > val[i]) {
389 temp = val[i];
390 k = i;
391 }
392 }
393
394 for (i=0; i<5; i++)
395 if (i != j && i != k )
396 sum += val[i];
397
398 dev_dbg(sharpsl_pm.dev, "Average: %d from values: %d, %d, %d, %d, %d\n", sum/3, val[0], val[1], val[2], val[3], val[4]);
399
400 return (sum/3);
401}
402
403static int sharpsl_check_battery_temp(void)
404{
405 int val, i, buff[5];
406
407 /* Check battery temperature */
408 for (i=0; i<5; i++) {
409 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
410 sharpsl_pm.machinfo->measure_temp(1);
411 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
412 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
413 sharpsl_pm.machinfo->measure_temp(0);
414 }
415
416 val = get_select_val(buff);
417
418 dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
419 if (val > SHARPSL_CHARGE_ON_TEMP)
420 return -1;
421
422 return 0;
423}
424
425static int sharpsl_check_battery_voltage(void)
426{
427 int val, i, buff[5];
428
429 /* disable charge, enable discharge */
430 sharpsl_pm.machinfo->charge(0);
431 sharpsl_pm.machinfo->discharge(1);
432 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
433
434 if (sharpsl_pm.machinfo->discharge1)
435 sharpsl_pm.machinfo->discharge1(1);
436
437 /* Check battery voltage */
438 for (i=0; i<5; i++) {
439 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
440 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
441 }
442
443 if (sharpsl_pm.machinfo->discharge1)
444 sharpsl_pm.machinfo->discharge1(0);
445
446 sharpsl_pm.machinfo->discharge(0);
447
448 val = get_select_val(buff);
449 dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
450
451 if (val < SHARPSL_CHARGE_ON_VOLT)
452 return -1;
453
454 return 0;
455}
456
457static int sharpsl_ac_check(void)
458{
459 int temp, i, buff[5];
460
461 for (i=0; i<5; i++) {
462 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
463 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
464 }
465
466 temp = get_select_val(buff);
467 dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp);
468
469 if ((temp > SHARPSL_CHARGE_ON_ACIN_HIGH) || (temp < SHARPSL_CHARGE_ON_ACIN_LOW)) {
470 dev_err(sharpsl_pm.dev, "Error: AC check failed.\n");
471 return -1;
472 }
473
474 return 0;
475}
476
477#ifdef CONFIG_PM
478static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
479{
480 sharpsl_pm.flags |= SHARPSL_SUSPENDED;
481 flush_scheduled_work();
482
483 if (sharpsl_pm.charge_mode == CHRG_ON)
484 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
485 else
486 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
487
488 return 0;
489}
490
491static int sharpsl_pm_resume(struct platform_device *pdev)
492{
493 /* Clear the reset source indicators as they break the bootloader upon reboot */
494 RCSR = 0x0f;
495 sharpsl_average_clear();
496 sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
497 sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
498
499 return 0;
500}
501
502static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
503{
504 dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR);
505
506 dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
507 /* not charging and AC-IN! */
508
509 if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
510 dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
511 sharpsl_pm.charge_mode = CHRG_OFF;
512 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
513 sharpsl_off_charge_battery();
514 }
515
516 sharpsl_pm.machinfo->presuspend();
517
518 PEDR = 0xffffffff; /* clear it */
519
520 sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
521 if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
522 RTSR &= RTSR_ALE;
523 RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
524 dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR);
525 sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
526 } else if (alarm_enable) {
527 RTSR &= RTSR_ALE;
528 RTAR = alarm_time;
529 dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR);
530 } else {
531 dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
532 }
533
534 pxa_pm_enter(state);
535
536 sharpsl_pm.machinfo->postsuspend();
537
538 dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR);
539}
540
541static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
542{
543 if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) )
544 {
545 if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
546 dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
547 corgi_goto_sleep(alarm_time, alarm_enable, state);
548 return 1;
549 }
550 if(sharpsl_off_charge_battery()) {
551 dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
552 corgi_goto_sleep(alarm_time, alarm_enable, state);
553 return 1;
554 }
555 dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
556 }
557
558 if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) || (sharpsl_fatal_check() < 0) )
559 {
560 dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
561 corgi_goto_sleep(alarm_time, alarm_enable, state);
562 return 1;
563 }
564
565 return 0;
566}
567
568static int corgi_pxa_pm_enter(suspend_state_t state)
569{
570 unsigned long alarm_time = RTAR;
571 unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
572
573 dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
574
575 corgi_goto_sleep(alarm_time, alarm_status, state);
576
577 while (corgi_enter_suspend(alarm_time,alarm_status,state))
578 {}
579
580 dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
581
582 return 0;
583}
584#endif
585
586
587/*
588 * Check for fatal battery errors
589 * Fatal returns -1
590 */
591static int sharpsl_fatal_check(void)
592{
593 int buff[5], temp, i, acin;
594
595 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n");
596
597 /* Check AC-Adapter */
598 acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
599
600 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
601 sharpsl_pm.machinfo->charge(0);
602 udelay(100);
603 sharpsl_pm.machinfo->discharge(1); /* enable discharge */
604 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
605 }
606
607 if (sharpsl_pm.machinfo->discharge1)
608 sharpsl_pm.machinfo->discharge1(1);
609
610 /* Check battery : check inserting battery ? */
611 for (i=0; i<5; i++) {
612 buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
613 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
614 }
615
616 if (sharpsl_pm.machinfo->discharge1)
617 sharpsl_pm.machinfo->discharge1(0);
618
619 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
620 udelay(100);
621 sharpsl_pm.machinfo->charge(1);
622 sharpsl_pm.machinfo->discharge(0);
623 }
624
625 temp = get_select_val(buff);
626 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %d\n", acin, temp, sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT));
627
628 if ((acin && (temp < SHARPSL_FATAL_ACIN_VOLT)) ||
629 (!acin && (temp < SHARPSL_FATAL_NOACIN_VOLT)))
630 return -1;
631 return 0;
632}
633
634static int sharpsl_off_charge_error(void)
635{
636 dev_err(sharpsl_pm.dev, "Offline Charger: Error occured.\n");
637 sharpsl_pm.machinfo->charge(0);
638 sharpsl_pm_led(SHARPSL_LED_ERROR);
639 sharpsl_pm.charge_mode = CHRG_ERROR;
640 return 1;
641}
642
643/*
644 * Charging Control while suspended
645 * Return 1 - go straight to sleep
646 * Return 0 - sleep or wakeup depending on other factors
647 */
648static int sharpsl_off_charge_battery(void)
649{
650 int time;
651
652 dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
653
654 if (sharpsl_pm.charge_mode == CHRG_OFF) {
655 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
656
657 /* AC Check */
658 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
659 return sharpsl_off_charge_error();
660
661 /* Start Charging */
662 sharpsl_pm_led(SHARPSL_LED_ON);
663 sharpsl_pm.machinfo->charge(0);
664 mdelay(SHARPSL_CHARGE_WAIT_TIME);
665 sharpsl_pm.machinfo->charge(1);
666
667 sharpsl_pm.charge_mode = CHRG_ON;
668 sharpsl_pm.full_count = 0;
669
670 return 1;
671 } else if (sharpsl_pm.charge_mode != CHRG_ON) {
672 return 1;
673 }
674
675 if (sharpsl_pm.full_count == 0) {
676 int time;
677
678 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
679
680 if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
681 return sharpsl_off_charge_error();
682
683 sharpsl_pm.machinfo->charge(0);
684 mdelay(SHARPSL_CHARGE_WAIT_TIME);
685 sharpsl_pm.machinfo->charge(1);
686 sharpsl_pm.charge_mode = CHRG_ON;
687
688 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
689
690 time = RCNR;
691 while(1) {
692 /* Check if any wakeup event had occured */
693 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
694 return 0;
695 /* Check for timeout */
696 if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
697 return 1;
698 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
699 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occured. Retrying to check\n");
700 sharpsl_pm.full_count++;
701 sharpsl_pm.machinfo->charge(0);
702 mdelay(SHARPSL_CHARGE_WAIT_TIME);
703 sharpsl_pm.machinfo->charge(1);
704 return 1;
705 }
706 }
707 }
708
709 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
710
711 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
712
713 time = RCNR;
714 while(1) {
715 /* Check if any wakeup event had occured */
716 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
717 return 0;
718 /* Check for timeout */
719 if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
720 if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
721 dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
722 sharpsl_pm.full_count = 0;
723 }
724 sharpsl_pm.full_count++;
725 return 1;
726 }
727 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
728 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
729 sharpsl_pm_led(SHARPSL_LED_OFF);
730 sharpsl_pm.machinfo->charge(0);
731 sharpsl_pm.charge_mode = CHRG_DONE;
732 return 1;
733 }
734 }
735}
736
737
738static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
739{
740 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent);
741}
742
743static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
744{
745 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage);
746}
747
748static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL);
749static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL);
750
751extern void (*apm_get_power_status)(struct apm_power_info *);
752
753static void sharpsl_apm_get_power_status(struct apm_power_info *info)
754{
755 info->ac_line_status = sharpsl_pm.battstat.ac_status;
756
757 if (sharpsl_pm.charge_mode == CHRG_ON)
758 info->battery_status = APM_BATTERY_STATUS_CHARGING;
759 else
760 info->battery_status = sharpsl_pm.battstat.mainbat_status;
761
762 info->battery_flag = (1 << info->battery_status);
763 info->battery_life = sharpsl_pm.battstat.mainbat_percent;
764}
765
766static struct pm_ops sharpsl_pm_ops = {
767 .pm_disk_mode = PM_DISK_FIRMWARE,
768 .prepare = pxa_pm_prepare,
769 .enter = corgi_pxa_pm_enter,
770 .finish = pxa_pm_finish,
771};
772
773static int __init sharpsl_pm_probe(struct platform_device *pdev)
774{
775 if (!pdev->dev.platform_data)
776 return -EINVAL;
777
778 sharpsl_pm.dev = &pdev->dev;
779 sharpsl_pm.machinfo = pdev->dev.platform_data;
780 sharpsl_pm.charge_mode = CHRG_OFF;
781 sharpsl_pm.flags = 0;
782
783 init_timer(&sharpsl_pm.ac_timer);
784 sharpsl_pm.ac_timer.function = sharpsl_ac_timer;
785
786 init_timer(&sharpsl_pm.chrg_full_timer);
787 sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer;
788
789 sharpsl_pm.machinfo->init();
790
791 device_create_file(&pdev->dev, &dev_attr_battery_percentage);
792 device_create_file(&pdev->dev, &dev_attr_battery_voltage);
793
794 apm_get_power_status = sharpsl_apm_get_power_status;
795
796 pm_set_ops(&sharpsl_pm_ops);
797
798 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
799
800 return 0;
801}
802
803static int sharpsl_pm_remove(struct platform_device *pdev)
804{
805 pm_set_ops(NULL);
806
807 device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
808 device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
809
810 sharpsl_pm.machinfo->exit();
811
812 del_timer_sync(&sharpsl_pm.chrg_full_timer);
813 del_timer_sync(&sharpsl_pm.ac_timer);
814
815 return 0;
816}
817
818static struct platform_driver sharpsl_pm_driver = {
819 .probe = sharpsl_pm_probe,
820 .remove = sharpsl_pm_remove,
821 .suspend = sharpsl_pm_suspend,
822 .resume = sharpsl_pm_resume,
823 .driver = {
824 .name = "sharpsl-pm",
825 },
826};
827
828static int __devinit sharpsl_pm_init(void)
829{
830 return platform_driver_register(&sharpsl_pm_driver);
831}
832
833static void sharpsl_pm_exit(void)
834{
835 platform_driver_unregister(&sharpsl_pm_driver);
836}
837
838late_initcall(sharpsl_pm_init);
839module_exit(sharpsl_pm_exit);
diff --git a/arch/arm/mach-pxa/Kconfig b/arch/arm/mach-pxa/Kconfig
index 2a58499c0968..c1d77f5b3823 100644
--- a/arch/arm/mach-pxa/Kconfig
+++ b/arch/arm/mach-pxa/Kconfig
@@ -112,12 +112,14 @@ config IWMMXT
112config PXA_SHARP_C7xx 112config PXA_SHARP_C7xx
113 bool 113 bool
114 select PXA_SSP 114 select PXA_SSP
115 select SHARPSL_PM
115 help 116 help
116 Enable support for all Sharp C7xx models 117 Enable support for all Sharp C7xx models
117 118
118config PXA_SHARP_Cxx00 119config PXA_SHARP_Cxx00
119 bool 120 bool
120 select PXA_SSP 121 select PXA_SSP
122 select SHARPSL_PM
121 help 123 help
122 Enable common support for Sharp Cxx00 models 124 Enable common support for Sharp Cxx00 models
123 125
diff --git a/arch/arm/mach-pxa/corgi_pm.c b/arch/arm/mach-pxa/corgi_pm.c
index 599be14754f9..de8b2403c929 100644
--- a/arch/arm/mach-pxa/corgi_pm.c
+++ b/arch/arm/mach-pxa/corgi_pm.c
@@ -21,6 +21,7 @@
21#include <asm/mach-types.h> 21#include <asm/mach-types.h>
22#include <asm/hardware.h> 22#include <asm/hardware.h>
23#include <asm/hardware/scoop.h> 23#include <asm/hardware/scoop.h>
24#include <asm/hardware/sharpsl_pm.h>
24 25
25#include <asm/arch/sharpsl.h> 26#include <asm/arch/sharpsl.h>
26#include <asm/arch/corgi.h> 27#include <asm/arch/corgi.h>
@@ -33,19 +34,7 @@ static void corgi_charger_init(void)
33 pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT); 34 pxa_gpio_mode(CORGI_GPIO_CHRG_ON | GPIO_OUT);
34 pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT); 35 pxa_gpio_mode(CORGI_GPIO_CHRG_UKN | GPIO_OUT);
35 pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN); 36 pxa_gpio_mode(CORGI_GPIO_KEY_INT | GPIO_IN);
36} 37 sharpsl_pm_pxa_init();
37
38static void corgi_charge_led(int val)
39{
40 if (val == SHARPSL_LED_ERROR) {
41 dev_dbg(sharpsl_pm.dev, "Charge LED Error\n");
42 } else if (val == SHARPSL_LED_ON) {
43 dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
44 GPSR0 = GPIO_bit(CORGI_GPIO_LED_ORANGE);
45 } else {
46 dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
47 GPCR0 = GPIO_bit(CORGI_GPIO_LED_ORANGE);
48 }
49} 38}
50 39
51static void corgi_measure_temp(int on) 40static void corgi_measure_temp(int on)
@@ -138,15 +127,15 @@ static int corgi_should_wakeup(unsigned int resume_on_alarm)
138 dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR); 127 dev_dbg(sharpsl_pm.dev, "GPLR0 = %x,%x\n", GPLR0, PEDR);
139 128
140 if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) { 129 if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
141 if (STATUS_AC_IN()) { 130 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
142 /* charge on */ 131 /* charge on */
143 dev_dbg(sharpsl_pm.dev, "ac insert\n"); 132 dev_dbg(sharpsl_pm.dev, "ac insert\n");
144 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG; 133 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
145 } else { 134 } else {
146 /* charge off */ 135 /* charge off */
147 dev_dbg(sharpsl_pm.dev, "ac remove\n"); 136 dev_dbg(sharpsl_pm.dev, "ac remove\n");
148 CHARGE_LED_OFF(); 137 sharpsl_pm_led(SHARPSL_LED_OFF);
149 CHARGE_OFF(); 138 sharpsl_pm.machinfo->charge(0);
150 sharpsl_pm.charge_mode = CHRG_OFF; 139 sharpsl_pm.charge_mode = CHRG_OFF;
151 } 140 }
152 } 141 }
@@ -172,23 +161,39 @@ static unsigned long corgi_charger_wakeup(void)
172 return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) ); 161 return ~GPLR0 & ( GPIO_bit(CORGI_GPIO_AC_IN) | GPIO_bit(CORGI_GPIO_KEY_INT) | GPIO_bit(CORGI_GPIO_WAKEUP) );
173} 162}
174 163
175static int corgi_acin_status(void) 164unsigned long corgipm_read_devdata(int type)
176{ 165{
177 return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0); 166 switch(type) {
167 case SHARPSL_STATUS_ACIN:
168 return ((GPLR(CORGI_GPIO_AC_IN) & GPIO_bit(CORGI_GPIO_AC_IN)) != 0);
169 case SHARPSL_STATUS_LOCK:
170 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
171 case SHARPSL_STATUS_CHRGFULL:
172 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
173 case SHARPSL_STATUS_FATAL:
174 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
175 case SHARPSL_ACIN_VOLT:
176 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
177 case SHARPSL_BATT_TEMP:
178 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
179 case SHARPSL_BATT_VOLT:
180 default:
181 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
182 }
178} 183}
179 184
180static struct sharpsl_charger_machinfo corgi_pm_machinfo = { 185static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
181 .init = corgi_charger_init, 186 .init = corgi_charger_init,
187 .exit = sharpsl_pm_pxa_remove,
182 .gpio_batlock = CORGI_GPIO_BAT_COVER, 188 .gpio_batlock = CORGI_GPIO_BAT_COVER,
183 .gpio_acin = CORGI_GPIO_AC_IN, 189 .gpio_acin = CORGI_GPIO_AC_IN,
184 .gpio_batfull = CORGI_GPIO_CHRG_FULL, 190 .gpio_batfull = CORGI_GPIO_CHRG_FULL,
185 .status_acin = corgi_acin_status,
186 .discharge = corgi_discharge, 191 .discharge = corgi_discharge,
187 .charge = corgi_charge, 192 .charge = corgi_charge,
188 .chargeled = corgi_charge_led,
189 .measure_temp = corgi_measure_temp, 193 .measure_temp = corgi_measure_temp,
190 .presuspend = corgi_presuspend, 194 .presuspend = corgi_presuspend,
191 .postsuspend = corgi_postsuspend, 195 .postsuspend = corgi_postsuspend,
196 .read_devdata = corgipm_read_devdata,
192 .charger_wakeup = corgi_charger_wakeup, 197 .charger_wakeup = corgi_charger_wakeup,
193 .should_wakeup = corgi_should_wakeup, 198 .should_wakeup = corgi_should_wakeup,
194 .bat_levels = 40, 199 .bat_levels = 40,
diff --git a/arch/arm/mach-pxa/sharpsl.h b/arch/arm/mach-pxa/sharpsl.h
index b0c40a1d6671..92b793050509 100644
--- a/arch/arm/mach-pxa/sharpsl.h
+++ b/arch/arm/mach-pxa/sharpsl.h
@@ -1,7 +1,16 @@
1/* 1/*
2 * SharpSL SSP Driver 2 * Copyright (c) 2004-2005 Richard Purdie
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
3 */ 8 */
4 9
10
11/*
12 * SharpSL SSP Driver
13 */
5struct corgissp_machinfo { 14struct corgissp_machinfo {
6 int port; 15 int port;
7 int cs_lcdcon; 16 int cs_lcdcon;
@@ -14,18 +23,18 @@ struct corgissp_machinfo {
14 23
15void corgi_ssp_set_machinfo(struct corgissp_machinfo *machinfo); 24void corgi_ssp_set_machinfo(struct corgissp_machinfo *machinfo);
16 25
26
17/* 27/*
18 * SharpSL Backlight 28 * SharpSL Backlight
19 */ 29 */
20
21void corgi_bl_set_intensity(int intensity); 30void corgi_bl_set_intensity(int intensity);
22void spitz_bl_set_intensity(int intensity); 31void spitz_bl_set_intensity(int intensity);
23void akita_bl_set_intensity(int intensity); 32void akita_bl_set_intensity(int intensity);
24 33
34
25/* 35/*
26 * SharpSL Touchscreen Driver 36 * SharpSL Touchscreen Driver
27 */ 37 */
28
29unsigned long corgi_get_hsync_len(void); 38unsigned long corgi_get_hsync_len(void);
30unsigned long spitz_get_hsync_len(void); 39unsigned long spitz_get_hsync_len(void);
31void corgi_put_hsync(void); 40void corgi_put_hsync(void);
@@ -33,89 +42,22 @@ void spitz_put_hsync(void);
33void corgi_wait_hsync(void); 42void corgi_wait_hsync(void);
34void spitz_wait_hsync(void); 43void spitz_wait_hsync(void);
35 44
45
36/* 46/*
37 * SharpSL Battery/PM Driver 47 * SharpSL Battery/PM Driver
38 */ 48 */
39 49
40struct sharpsl_charger_machinfo { 50#define READ_GPIO_BIT(x) (GPLR(x) & GPIO_bit(x))
41 void (*init)(void);
42 int gpio_acin;
43 int gpio_batfull;
44 int gpio_batlock;
45 int gpio_fatal;
46 int (*status_acin)(void);
47 void (*discharge)(int);
48 void (*discharge1)(int);
49 void (*charge)(int);
50 void (*chargeled)(int);
51 void (*measure_temp)(int);
52 void (*presuspend)(void);
53 void (*postsuspend)(void);
54 unsigned long (*charger_wakeup)(void);
55 int (*should_wakeup)(unsigned int resume_on_alarm);
56 int bat_levels;
57 struct battery_thresh *bat_levels_noac;
58 struct battery_thresh *bat_levels_acin;
59 int status_high_acin;
60 int status_low_acin;
61 int status_high_noac;
62 int status_low_noac;
63};
64
65struct battery_thresh {
66 int voltage;
67 int percentage;
68};
69
70struct battery_stat {
71 int ac_status; /* APM AC Present/Not Present */
72 int mainbat_status; /* APM Main Battery Status */
73 int mainbat_percent; /* Main Battery Percentage Charge */
74 int mainbat_voltage; /* Main Battery Voltage */
75};
76
77struct sharpsl_pm_status {
78 struct device *dev;
79 struct timer_list ac_timer;
80 struct timer_list chrg_full_timer;
81
82 int charge_mode;
83#define CHRG_ERROR (-1)
84#define CHRG_OFF (0)
85#define CHRG_ON (1)
86#define CHRG_DONE (2)
87
88 unsigned int flags;
89#define SHARPSL_SUSPENDED (1 << 0) /* Device is Suspended */
90#define SHARPSL_ALARM_ACTIVE (1 << 1) /* Alarm is for charging event (not user) */
91#define SHARPSL_BL_LIMIT (1 << 2) /* Backlight Intensity Limited */
92#define SHARPSL_APM_QUEUED (1 << 3) /* APM Event Queued */
93#define SHARPSL_DO_OFFLINE_CHRG (1 << 4) /* Trigger the offline charger */
94 51
95 int full_count; 52/* MAX1111 Channel Definitions */
96 unsigned long charge_start_time; 53#define MAX1111_BATT_VOLT 4u
97 struct sharpsl_charger_machinfo *machinfo; 54#define MAX1111_BATT_TEMP 2u
98 struct battery_stat battstat; 55#define MAX1111_ACIN_VOLT 6u
99};
100 56
101extern struct sharpsl_pm_status sharpsl_pm;
102extern struct battery_thresh spitz_battery_levels_acin[]; 57extern struct battery_thresh spitz_battery_levels_acin[];
103extern struct battery_thresh spitz_battery_levels_noac[]; 58extern struct battery_thresh spitz_battery_levels_noac[];
59void sharpsl_pm_pxa_init(void);
60void sharpsl_pm_pxa_remove(void);
61int sharpsl_pm_pxa_read_max1111(int channel);
104 62
105#define READ_GPIO_BIT(x) (GPLR(x) & GPIO_bit(x))
106
107#define SHARPSL_LED_ERROR 2
108#define SHARPSL_LED_ON 1
109#define SHARPSL_LED_OFF 0
110 63
111#define CHARGE_ON() sharpsl_pm.machinfo->charge(1)
112#define CHARGE_OFF() sharpsl_pm.machinfo->charge(0)
113#define CHARGE_LED_ON() sharpsl_pm.machinfo->chargeled(SHARPSL_LED_ON)
114#define CHARGE_LED_OFF() sharpsl_pm.machinfo->chargeled(SHARPSL_LED_OFF)
115#define CHARGE_LED_ERR() sharpsl_pm.machinfo->chargeled(SHARPSL_LED_ERROR)
116#define DISCHARGE_ON() sharpsl_pm.machinfo->discharge(1)
117#define DISCHARGE_OFF() sharpsl_pm.machinfo->discharge(0)
118#define STATUS_AC_IN() sharpsl_pm.machinfo->status_acin()
119#define STATUS_BATT_LOCKED() READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock)
120#define STATUS_CHRG_FULL() READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull)
121#define STATUS_FATAL() READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal)
diff --git a/arch/arm/mach-pxa/sharpsl_pm.c b/arch/arm/mach-pxa/sharpsl_pm.c
index c10be00fb526..f6fefb181411 100644
--- a/arch/arm/mach-pxa/sharpsl_pm.c
+++ b/arch/arm/mach-pxa/sharpsl_pm.c
@@ -15,48 +15,21 @@
15#undef DEBUG 15#undef DEBUG
16 16
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/timer.h>
19#include <linux/init.h> 18#include <linux/init.h>
20#include <linux/kernel.h> 19#include <linux/kernel.h>
21#include <linux/apm_bios.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h> 20#include <linux/interrupt.h>
24#include <linux/platform_device.h> 21#include <linux/platform_device.h>
25 22
26#include <asm/hardware.h> 23#include <asm/hardware.h>
27#include <asm/hardware/scoop.h>
28#include <asm/mach-types.h> 24#include <asm/mach-types.h>
29#include <asm/irq.h> 25#include <asm/irq.h>
30#include <asm/apm.h> 26#include <asm/apm.h>
31
32#include <asm/arch/pm.h> 27#include <asm/arch/pm.h>
33#include <asm/arch/pxa-regs.h> 28#include <asm/arch/pxa-regs.h>
34#include <asm/arch/sharpsl.h> 29#include <asm/arch/sharpsl.h>
30#include <asm/hardware/sharpsl_pm.h>
35#include "sharpsl.h" 31#include "sharpsl.h"
36 32
37/*
38 * Constants
39 */
40#define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */
41#define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */
42#define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */
43#define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */
44#define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */
45#define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */
46#define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */
47#define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */
48#define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */
49#define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */
50#define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */
51#define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */
52
53#define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
54#define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
55#define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
56#define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
57#define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
58#define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
59
60struct battery_thresh spitz_battery_levels_acin[] = { 33struct battery_thresh spitz_battery_levels_acin[] = {
61 { 213, 100}, 34 { 213, 100},
62 { 212, 98}, 35 { 212, 98},
@@ -151,763 +124,17 @@ struct battery_thresh spitz_battery_levels_noac[] = {
151#define MAXCTRL_SEL_SH 4 124#define MAXCTRL_SEL_SH 4
152#define MAXCTRL_STR 1u << 7 125#define MAXCTRL_STR 1u << 7
153 126
154/* MAX1111 Channel Definitions */
155#define BATT_AD 4u
156#define BATT_THM 2u
157#define JK_VAD 6u
158
159
160/*
161 * Prototypes
162 */
163static int sharpsl_read_main_battery(void);
164static int sharpsl_off_charge_battery(void);
165static int sharpsl_check_battery_temp(void);
166static int sharpsl_check_battery_voltage(void);
167static int sharpsl_ac_check(void);
168static int sharpsl_fatal_check(void);
169static int sharpsl_average_value(int ad);
170static void sharpsl_average_clear(void);
171static void sharpsl_charge_toggle(void *private_);
172static void sharpsl_battery_thread(void *private_);
173
174
175/*
176 * Variables
177 */
178struct sharpsl_pm_status sharpsl_pm;
179DECLARE_WORK(toggle_charger, sharpsl_charge_toggle, NULL);
180DECLARE_WORK(sharpsl_bat, sharpsl_battery_thread, NULL);
181
182
183static int get_percentage(int voltage)
184{
185 int i = sharpsl_pm.machinfo->bat_levels - 1;
186 struct battery_thresh *thresh;
187
188 if (sharpsl_pm.charge_mode == CHRG_ON)
189 thresh=sharpsl_pm.machinfo->bat_levels_acin;
190 else
191 thresh=sharpsl_pm.machinfo->bat_levels_noac;
192
193 while (i > 0 && (voltage > thresh[i].voltage))
194 i--;
195
196 return thresh[i].percentage;
197}
198
199static int get_apm_status(int voltage)
200{
201 int low_thresh, high_thresh;
202
203 if (sharpsl_pm.charge_mode == CHRG_ON) {
204 high_thresh = sharpsl_pm.machinfo->status_high_acin;
205 low_thresh = sharpsl_pm.machinfo->status_low_acin;
206 } else {
207 high_thresh = sharpsl_pm.machinfo->status_high_noac;
208 low_thresh = sharpsl_pm.machinfo->status_low_noac;
209 }
210
211 if (voltage >= high_thresh)
212 return APM_BATTERY_STATUS_HIGH;
213 if (voltage >= low_thresh)
214 return APM_BATTERY_STATUS_LOW;
215 return APM_BATTERY_STATUS_CRITICAL;
216}
217
218void sharpsl_battery_kick(void)
219{
220 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
221}
222EXPORT_SYMBOL(sharpsl_battery_kick);
223
224
225static void sharpsl_battery_thread(void *private_)
226{
227 int voltage, percent, apm_status, i = 0;
228
229 if (!sharpsl_pm.machinfo)
230 return;
231
232 sharpsl_pm.battstat.ac_status = (STATUS_AC_IN() ? APM_AC_ONLINE : APM_AC_OFFLINE);
233
234 /* Corgi cannot confirm when battery fully charged so periodically kick! */
235 if (machine_is_corgi() && (sharpsl_pm.charge_mode == CHRG_ON)
236 && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL))
237 schedule_work(&toggle_charger);
238
239 while(1) {
240 voltage = sharpsl_read_main_battery();
241 if (voltage > 0) break;
242 if (i++ > 5) {
243 voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
244 dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
245 break;
246 }
247 }
248
249 voltage = sharpsl_average_value(voltage);
250 apm_status = get_apm_status(voltage);
251 percent = get_percentage(voltage);
252
253 /* At low battery voltages, the voltage has a tendency to start
254 creeping back up so we try to avoid this here */
255 if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) || percent <= sharpsl_pm.battstat.mainbat_percent) {
256 sharpsl_pm.battstat.mainbat_voltage = voltage;
257 sharpsl_pm.battstat.mainbat_status = apm_status;
258 sharpsl_pm.battstat.mainbat_percent = percent;
259 }
260
261 dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %d\n", voltage,
262 sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
263
264 /* If battery is low. limit backlight intensity to save power. */
265 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
266 && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) ||
267 (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) {
268 if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) {
269 corgibl_limit_intensity(1);
270 sharpsl_pm.flags |= SHARPSL_BL_LIMIT;
271 }
272 } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) {
273 corgibl_limit_intensity(0);
274 sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT;
275 }
276
277 /* Suspend if critical battery level */
278 if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
279 && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
280 && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
281 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
282 dev_err(sharpsl_pm.dev, "Fatal Off\n");
283 apm_queue_event(APM_CRITICAL_SUSPEND);
284 }
285
286 schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
287}
288
289static void sharpsl_charge_on(void)
290{
291 dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
292
293 sharpsl_pm.full_count = 0;
294 sharpsl_pm.charge_mode = CHRG_ON;
295 schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
296 schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
297}
298
299static void sharpsl_charge_off(void)
300{
301 dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
302
303 CHARGE_OFF();
304 CHARGE_LED_OFF();
305 sharpsl_pm.charge_mode = CHRG_OFF;
306
307 schedule_work(&sharpsl_bat);
308}
309
310static void sharpsl_charge_error(void)
311{
312 CHARGE_LED_ERR();
313 CHARGE_OFF();
314 sharpsl_pm.charge_mode = CHRG_ERROR;
315}
316
317static void sharpsl_charge_toggle(void *private_)
318{
319 dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies);
320
321 if (STATUS_AC_IN() == 0) {
322 sharpsl_charge_off();
323 return;
324 } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
325 sharpsl_charge_error();
326 return;
327 }
328
329 CHARGE_LED_ON();
330 CHARGE_OFF();
331 mdelay(SHARPSL_CHARGE_WAIT_TIME);
332 CHARGE_ON();
333
334 sharpsl_pm.charge_start_time = jiffies;
335}
336
337static void sharpsl_ac_timer(unsigned long data)
338{
339 int acin = STATUS_AC_IN();
340
341 dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin);
342
343 sharpsl_average_clear();
344 if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
345 sharpsl_charge_on();
346 else if (sharpsl_pm.charge_mode == CHRG_ON)
347 sharpsl_charge_off();
348
349 schedule_work(&sharpsl_bat);
350}
351
352
353static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id, struct pt_regs *fp)
354{
355 /* Delay the event slightly to debounce */
356 /* Must be a smaller delay than the chrg_full_isr below */
357 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
358
359 return IRQ_HANDLED;
360}
361
362static void sharpsl_chrg_full_timer(unsigned long data)
363{
364 dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
365
366 sharpsl_pm.full_count++;
367
368 if (STATUS_AC_IN() == 0) {
369 dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
370 if (sharpsl_pm.charge_mode == CHRG_ON)
371 sharpsl_charge_off();
372 } else if (sharpsl_pm.full_count < 2) {
373 dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
374 schedule_work(&toggle_charger);
375 } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
376 dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
377 schedule_work(&toggle_charger);
378 } else {
379 sharpsl_charge_off();
380 sharpsl_pm.charge_mode = CHRG_DONE;
381 dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
382 }
383}
384
385/* Charging Finished Interrupt (Not present on Corgi) */
386/* Can trigger at the same time as an AC staus change so
387 delay until after that has been processed */
388static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id, struct pt_regs *fp)
389{
390 if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
391 return IRQ_HANDLED;
392
393 /* delay until after any ac interrupt */
394 mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
395
396 return IRQ_HANDLED;
397}
398
399static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id, struct pt_regs *fp)
400{
401 int is_fatal = 0;
402
403 if (STATUS_BATT_LOCKED() == 0) {
404 dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
405 is_fatal = 1;
406 }
407
408 if (sharpsl_pm.machinfo->gpio_fatal && (STATUS_FATAL() == 0)) {
409 dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
410 is_fatal = 1;
411 }
412
413 if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
414 sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
415 apm_queue_event(APM_CRITICAL_SUSPEND);
416 }
417
418 return IRQ_HANDLED;
419}
420
421/*
422 * Maintain an average of the last 10 readings
423 */
424#define SHARPSL_CNV_VALUE_NUM 10
425static int sharpsl_ad_index;
426
427static void sharpsl_average_clear(void)
428{
429 sharpsl_ad_index = 0;
430}
431
432static int sharpsl_average_value(int ad)
433{
434 int i, ad_val = 0;
435 static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
436
437 if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
438 sharpsl_ad_index = 0;
439 return ad;
440 }
441
442 sharpsl_ad[sharpsl_ad_index] = ad;
443 sharpsl_ad_index++;
444 if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
445 for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
446 sharpsl_ad[i] = sharpsl_ad[i+1];
447 sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
448 }
449 for (i=0; i < sharpsl_ad_index; i++)
450 ad_val += sharpsl_ad[i];
451
452 return (ad_val / sharpsl_ad_index);
453}
454
455
456/* 127/*
457 * Read MAX1111 ADC 128 * Read MAX1111 ADC
458 */ 129 */
459static int read_max1111(int channel) 130int sharpsl_pm_pxa_read_max1111(int channel)
460{ 131{
461 return corgi_ssp_max1111_get((channel << MAXCTRL_SEL_SH) | MAXCTRL_PD0 | MAXCTRL_PD1 132 return corgi_ssp_max1111_get((channel << MAXCTRL_SEL_SH) | MAXCTRL_PD0 | MAXCTRL_PD1
462 | MAXCTRL_SGL | MAXCTRL_UNI | MAXCTRL_STR); 133 | MAXCTRL_SGL | MAXCTRL_UNI | MAXCTRL_STR);
463} 134}
464 135
465static int sharpsl_read_main_battery(void) 136void sharpsl_pm_pxa_init(void)
466{
467 return read_max1111(BATT_AD);
468}
469
470static int sharpsl_read_temp(void)
471{ 137{
472 int temp;
473
474 sharpsl_pm.machinfo->measure_temp(1);
475
476 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
477 temp = read_max1111(BATT_THM);
478
479 sharpsl_pm.machinfo->measure_temp(0);
480
481 return temp;
482}
483
484static int sharpsl_read_acin(void)
485{
486 return read_max1111(JK_VAD);
487}
488
489/*
490 * Take an array of 5 integers, remove the maximum and minimum values
491 * and return the average.
492 */
493static int get_select_val(int *val)
494{
495 int i, j, k, temp, sum = 0;
496
497 /* Find MAX val */
498 temp = val[0];
499 j=0;
500 for (i=1; i<5; i++) {
501 if (temp < val[i]) {
502 temp = val[i];
503 j = i;
504 }
505 }
506
507 /* Find MIN val */
508 temp = val[4];
509 k=4;
510 for (i=3; i>=0; i--) {
511 if (temp > val[i]) {
512 temp = val[i];
513 k = i;
514 }
515 }
516
517 for (i=0; i<5; i++)
518 if (i != j && i != k )
519 sum += val[i];
520
521 dev_dbg(sharpsl_pm.dev, "Average: %d from values: %d, %d, %d, %d, %d\n", sum/3, val[0], val[1], val[2], val[3], val[4]);
522
523 return (sum/3);
524}
525
526static int sharpsl_check_battery_temp(void)
527{
528 int val, i, buff[5];
529
530 /* Check battery temperature */
531 for (i=0; i<5; i++) {
532 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
533 buff[i] = sharpsl_read_temp();
534 }
535
536 val = get_select_val(buff);
537
538 dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
539 if (val > SHARPSL_CHARGE_ON_TEMP)
540 return -1;
541
542 return 0;
543}
544
545static int sharpsl_check_battery_voltage(void)
546{
547 int val, i, buff[5];
548
549 /* disable charge, enable discharge */
550 CHARGE_OFF();
551 DISCHARGE_ON();
552 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
553
554 if (sharpsl_pm.machinfo->discharge1)
555 sharpsl_pm.machinfo->discharge1(1);
556
557 /* Check battery voltage */
558 for (i=0; i<5; i++) {
559 buff[i] = sharpsl_read_main_battery();
560 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
561 }
562
563 if (sharpsl_pm.machinfo->discharge1)
564 sharpsl_pm.machinfo->discharge1(0);
565
566 DISCHARGE_OFF();
567
568 val = get_select_val(buff);
569 dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
570
571 if (val < SHARPSL_CHARGE_ON_VOLT)
572 return -1;
573
574 return 0;
575}
576
577static int sharpsl_ac_check(void)
578{
579 int temp, i, buff[5];
580
581 for (i=0; i<5; i++) {
582 buff[i] = sharpsl_read_acin();
583 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
584 }
585
586 temp = get_select_val(buff);
587 dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp);
588
589 if ((temp > SHARPSL_CHARGE_ON_ACIN_HIGH) || (temp < SHARPSL_CHARGE_ON_ACIN_LOW)) {
590 dev_err(sharpsl_pm.dev, "Error: AC check failed.\n");
591 return -1;
592 }
593
594 return 0;
595}
596
597#ifdef CONFIG_PM
598static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
599{
600 sharpsl_pm.flags |= SHARPSL_SUSPENDED;
601 flush_scheduled_work();
602
603 if (sharpsl_pm.charge_mode == CHRG_ON)
604 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
605 else
606 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
607
608 return 0;
609}
610
611static int sharpsl_pm_resume(struct platform_device *pdev)
612{
613 /* Clear the reset source indicators as they break the bootloader upon reboot */
614 RCSR = 0x0f;
615 sharpsl_average_clear();
616 sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
617 sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
618
619 return 0;
620}
621
622static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
623{
624 dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR);
625
626 dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
627 /* not charging and AC-IN! */
628
629 if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (STATUS_AC_IN() != 0)) {
630 dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
631 sharpsl_pm.charge_mode = CHRG_OFF;
632 sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
633 sharpsl_off_charge_battery();
634 }
635
636 sharpsl_pm.machinfo->presuspend();
637
638 PEDR = 0xffffffff; /* clear it */
639
640 sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
641 if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
642 RTSR &= RTSR_ALE;
643 RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
644 dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR);
645 sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
646 } else if (alarm_enable) {
647 RTSR &= RTSR_ALE;
648 RTAR = alarm_time;
649 dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR);
650 } else {
651 dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
652 }
653
654 pxa_pm_enter(state);
655
656 sharpsl_pm.machinfo->postsuspend();
657
658 dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR);
659}
660
661static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
662{
663 if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) )
664 {
665 if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
666 dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
667 corgi_goto_sleep(alarm_time, alarm_enable, state);
668 return 1;
669 }
670 if(sharpsl_off_charge_battery()) {
671 dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
672 corgi_goto_sleep(alarm_time, alarm_enable, state);
673 return 1;
674 }
675 dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
676 }
677
678 if ((STATUS_BATT_LOCKED() == 0) || (sharpsl_fatal_check() < 0) )
679 {
680 dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
681 corgi_goto_sleep(alarm_time, alarm_enable, state);
682 return 1;
683 }
684
685 return 0;
686}
687
688static int corgi_pxa_pm_enter(suspend_state_t state)
689{
690 unsigned long alarm_time = RTAR;
691 unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
692
693 dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
694
695 corgi_goto_sleep(alarm_time, alarm_status, state);
696
697 while (corgi_enter_suspend(alarm_time,alarm_status,state))
698 {}
699
700 dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
701
702 return 0;
703}
704#endif
705
706
707/*
708 * Check for fatal battery errors
709 * Fatal returns -1
710 */
711static int sharpsl_fatal_check(void)
712{
713 int buff[5], temp, i, acin;
714
715 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n");
716
717 /* Check AC-Adapter */
718 acin = STATUS_AC_IN();
719
720 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
721 CHARGE_OFF();
722 udelay(100);
723 DISCHARGE_ON(); /* enable discharge */
724 mdelay(SHARPSL_WAIT_DISCHARGE_ON);
725 }
726
727 if (sharpsl_pm.machinfo->discharge1)
728 sharpsl_pm.machinfo->discharge1(1);
729
730 /* Check battery : check inserting battery ? */
731 for (i=0; i<5; i++) {
732 buff[i] = sharpsl_read_main_battery();
733 mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
734 }
735
736 if (sharpsl_pm.machinfo->discharge1)
737 sharpsl_pm.machinfo->discharge1(0);
738
739 if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) {
740 udelay(100);
741 CHARGE_ON();
742 DISCHARGE_OFF();
743 }
744
745 temp = get_select_val(buff);
746 dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %d\n", acin, temp, sharpsl_read_main_battery());
747
748 if ((acin && (temp < SHARPSL_FATAL_ACIN_VOLT)) ||
749 (!acin && (temp < SHARPSL_FATAL_NOACIN_VOLT)))
750 return -1;
751 return 0;
752}
753
754static int sharpsl_off_charge_error(void)
755{
756 dev_err(sharpsl_pm.dev, "Offline Charger: Error occured.\n");
757 CHARGE_OFF();
758 CHARGE_LED_ERR();
759 sharpsl_pm.charge_mode = CHRG_ERROR;
760 return 1;
761}
762
763/*
764 * Charging Control while suspended
765 * Return 1 - go straight to sleep
766 * Return 0 - sleep or wakeup depending on other factors
767 */
768static int sharpsl_off_charge_battery(void)
769{
770 int time;
771
772 dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
773
774 if (sharpsl_pm.charge_mode == CHRG_OFF) {
775 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
776
777 /* AC Check */
778 if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
779 return sharpsl_off_charge_error();
780
781 /* Start Charging */
782 CHARGE_LED_ON();
783 CHARGE_OFF();
784 mdelay(SHARPSL_CHARGE_WAIT_TIME);
785 CHARGE_ON();
786
787 sharpsl_pm.charge_mode = CHRG_ON;
788 sharpsl_pm.full_count = 0;
789
790 return 1;
791 } else if (sharpsl_pm.charge_mode != CHRG_ON) {
792 return 1;
793 }
794
795 if (sharpsl_pm.full_count == 0) {
796 int time;
797
798 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
799
800 if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
801 return sharpsl_off_charge_error();
802
803 CHARGE_OFF();
804 mdelay(SHARPSL_CHARGE_WAIT_TIME);
805 CHARGE_ON();
806 sharpsl_pm.charge_mode = CHRG_ON;
807
808 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
809
810 time = RCNR;
811 while(1) {
812 /* Check if any wakeup event had occured */
813 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
814 return 0;
815 /* Check for timeout */
816 if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
817 return 1;
818 if (STATUS_CHRG_FULL()) {
819 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occured. Retrying to check\n");
820 sharpsl_pm.full_count++;
821 CHARGE_OFF();
822 mdelay(SHARPSL_CHARGE_WAIT_TIME);
823 CHARGE_ON();
824 return 1;
825 }
826 }
827 }
828
829 dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
830
831 mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
832
833 time = RCNR;
834 while(1) {
835 /* Check if any wakeup event had occured */
836 if (sharpsl_pm.machinfo->charger_wakeup() != 0)
837 return 0;
838 /* Check for timeout */
839 if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
840 if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
841 dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
842 sharpsl_pm.full_count = 0;
843 }
844 sharpsl_pm.full_count++;
845 return 1;
846 }
847 if (STATUS_CHRG_FULL()) {
848 dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
849 CHARGE_LED_OFF();
850 CHARGE_OFF();
851 sharpsl_pm.charge_mode = CHRG_DONE;
852 return 1;
853 }
854 }
855}
856
857
858static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
859{
860 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent);
861}
862
863static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
864{
865 return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage);
866}
867
868static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL);
869static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL);
870
871extern void (*apm_get_power_status)(struct apm_power_info *);
872
873static void sharpsl_apm_get_power_status(struct apm_power_info *info)
874{
875 info->ac_line_status = sharpsl_pm.battstat.ac_status;
876
877 if (sharpsl_pm.charge_mode == CHRG_ON)
878 info->battery_status = APM_BATTERY_STATUS_CHARGING;
879 else
880 info->battery_status = sharpsl_pm.battstat.mainbat_status;
881
882 info->battery_flag = (1 << info->battery_status);
883 info->battery_life = sharpsl_pm.battstat.mainbat_percent;
884}
885
886static struct pm_ops sharpsl_pm_ops = {
887 .pm_disk_mode = PM_DISK_FIRMWARE,
888 .prepare = pxa_pm_prepare,
889 .enter = corgi_pxa_pm_enter,
890 .finish = pxa_pm_finish,
891};
892
893static int __init sharpsl_pm_probe(struct platform_device *pdev)
894{
895 if (!pdev->dev.platform_data)
896 return -EINVAL;
897
898 sharpsl_pm.dev = &pdev->dev;
899 sharpsl_pm.machinfo = pdev->dev.platform_data;
900 sharpsl_pm.charge_mode = CHRG_OFF;
901 sharpsl_pm.flags = 0;
902
903 sharpsl_pm.machinfo->init();
904
905 init_timer(&sharpsl_pm.ac_timer);
906 sharpsl_pm.ac_timer.function = sharpsl_ac_timer;
907
908 init_timer(&sharpsl_pm.chrg_full_timer);
909 sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer;
910
911 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_acin | GPIO_IN); 138 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_acin | GPIO_IN);
912 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batfull | GPIO_IN); 139 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batfull | GPIO_IN);
913 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batlock | GPIO_IN); 140 pxa_gpio_mode(sharpsl_pm.machinfo->gpio_batlock | GPIO_IN);
@@ -938,26 +165,10 @@ static int __init sharpsl_pm_probe(struct platform_device *pdev)
938 } 165 }
939 else set_irq_type(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull),IRQT_RISING); 166 else set_irq_type(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull),IRQT_RISING);
940 } 167 }
941
942 device_create_file(&pdev->dev, &dev_attr_battery_percentage);
943 device_create_file(&pdev->dev, &dev_attr_battery_voltage);
944
945 apm_get_power_status = sharpsl_apm_get_power_status;
946
947 pm_set_ops(&sharpsl_pm_ops);
948
949 mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
950
951 return 0;
952} 168}
953 169
954static int sharpsl_pm_remove(struct platform_device *pdev) 170void sharpsl_pm_pxa_remove(void)
955{ 171{
956 pm_set_ops(NULL);
957
958 device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
959 device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
960
961 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr); 172 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
962 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr); 173 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
963 174
@@ -966,32 +177,4 @@ static int sharpsl_pm_remove(struct platform_device *pdev)
966 177
967 if (!machine_is_corgi()) 178 if (!machine_is_corgi())
968 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr); 179 free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
969
970 del_timer_sync(&sharpsl_pm.chrg_full_timer);
971 del_timer_sync(&sharpsl_pm.ac_timer);
972
973 return 0;
974} 180}
975
976static struct platform_driver sharpsl_pm_driver = {
977 .probe = sharpsl_pm_probe,
978 .remove = sharpsl_pm_remove,
979 .suspend = sharpsl_pm_suspend,
980 .resume = sharpsl_pm_resume,
981 .driver = {
982 .name = "sharpsl-pm",
983 },
984};
985
986static int __devinit sharpsl_pm_init(void)
987{
988 return platform_driver_register(&sharpsl_pm_driver);
989}
990
991static void sharpsl_pm_exit(void)
992{
993 platform_driver_unregister(&sharpsl_pm_driver);
994}
995
996late_initcall(sharpsl_pm_init);
997module_exit(sharpsl_pm_exit);
diff --git a/arch/arm/mach-pxa/spitz_pm.c b/arch/arm/mach-pxa/spitz_pm.c
index 3ce7486daa51..76a5c26dea0b 100644
--- a/arch/arm/mach-pxa/spitz_pm.c
+++ b/arch/arm/mach-pxa/spitz_pm.c
@@ -21,6 +21,7 @@
21#include <asm/mach-types.h> 21#include <asm/mach-types.h>
22#include <asm/hardware.h> 22#include <asm/hardware.h>
23#include <asm/hardware/scoop.h> 23#include <asm/hardware/scoop.h>
24#include <asm/hardware/sharpsl_pm.h>
24 25
25#include <asm/arch/sharpsl.h> 26#include <asm/arch/sharpsl.h>
26#include <asm/arch/spitz.h> 27#include <asm/arch/spitz.h>
@@ -33,19 +34,7 @@ static void spitz_charger_init(void)
33{ 34{
34 pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN); 35 pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
35 pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN); 36 pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
36} 37 sharpsl_pm_pxa_init();
37
38static void spitz_charge_led(int val)
39{
40 if (val == SHARPSL_LED_ERROR) {
41 dev_dbg(sharpsl_pm.dev, "Charge LED Error\n");
42 } else if (val == SHARPSL_LED_ON) {
43 dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
44 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_ORANGE);
45 } else {
46 dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
47 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_LED_ORANGE);
48 }
49} 38}
50 39
51static void spitz_measure_temp(int on) 40static void spitz_measure_temp(int on)
@@ -92,7 +81,7 @@ static void spitz_discharge1(int on)
92 81
93static void spitz_presuspend(void) 82static void spitz_presuspend(void)
94{ 83{
95 spitz_last_ac_status = STATUS_AC_IN(); 84 spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
96 85
97 /* GPIO Sleep Register */ 86 /* GPIO Sleep Register */
98 PGSR0 = 0x00144018; 87 PGSR0 = 0x00144018;
@@ -138,7 +127,7 @@ static void spitz_postsuspend(void)
138static int spitz_should_wakeup(unsigned int resume_on_alarm) 127static int spitz_should_wakeup(unsigned int resume_on_alarm)
139{ 128{
140 int is_resume = 0; 129 int is_resume = 0;
141 int acin = STATUS_AC_IN(); 130 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
142 131
143 if (spitz_last_ac_status != acin) { 132 if (spitz_last_ac_status != acin) {
144 if (acin) { 133 if (acin) {
@@ -148,8 +137,8 @@ static int spitz_should_wakeup(unsigned int resume_on_alarm)
148 } else { 137 } else {
149 /* charge off */ 138 /* charge off */
150 dev_dbg(sharpsl_pm.dev, "AC Removed\n"); 139 dev_dbg(sharpsl_pm.dev, "AC Removed\n");
151 CHARGE_LED_OFF(); 140 sharpsl_pm_led(SHARPSL_LED_OFF);
152 CHARGE_OFF(); 141 sharpsl_pm.machinfo->charge(0);
153 sharpsl_pm.charge_mode = CHRG_OFF; 142 sharpsl_pm.charge_mode = CHRG_OFF;
154 } 143 }
155 spitz_last_ac_status = acin; 144 spitz_last_ac_status = acin;
@@ -175,25 +164,41 @@ static unsigned long spitz_charger_wakeup(void)
175 return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC)); 164 return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
176} 165}
177 166
178static int spitz_acin_status(void) 167unsigned long spitzpm_read_devdata(int type)
179{ 168{
180 return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0); 169 switch(type) {
170 case SHARPSL_STATUS_ACIN:
171 return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
172 case SHARPSL_STATUS_LOCK:
173 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
174 case SHARPSL_STATUS_CHRGFULL:
175 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
176 case SHARPSL_STATUS_FATAL:
177 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
178 case SHARPSL_ACIN_VOLT:
179 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
180 case SHARPSL_BATT_TEMP:
181 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
182 case SHARPSL_BATT_VOLT:
183 default:
184 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
185 }
181} 186}
182 187
183struct sharpsl_charger_machinfo spitz_pm_machinfo = { 188struct sharpsl_charger_machinfo spitz_pm_machinfo = {
184 .init = spitz_charger_init, 189 .init = spitz_charger_init,
190 .exit = sharpsl_pm_pxa_remove,
185 .gpio_batlock = SPITZ_GPIO_BAT_COVER, 191 .gpio_batlock = SPITZ_GPIO_BAT_COVER,
186 .gpio_acin = SPITZ_GPIO_AC_IN, 192 .gpio_acin = SPITZ_GPIO_AC_IN,
187 .gpio_batfull = SPITZ_GPIO_CHRG_FULL, 193 .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
188 .gpio_fatal = SPITZ_GPIO_FATAL_BAT, 194 .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
189 .status_acin = spitz_acin_status,
190 .discharge = spitz_discharge, 195 .discharge = spitz_discharge,
191 .discharge1 = spitz_discharge1, 196 .discharge1 = spitz_discharge1,
192 .charge = spitz_charge, 197 .charge = spitz_charge,
193 .chargeled = spitz_charge_led,
194 .measure_temp = spitz_measure_temp, 198 .measure_temp = spitz_measure_temp,
195 .presuspend = spitz_presuspend, 199 .presuspend = spitz_presuspend,
196 .postsuspend = spitz_postsuspend, 200 .postsuspend = spitz_postsuspend,
201 .read_devdata = spitzpm_read_devdata,
197 .charger_wakeup = spitz_charger_wakeup, 202 .charger_wakeup = spitz_charger_wakeup,
198 .should_wakeup = spitz_should_wakeup, 203 .should_wakeup = spitz_should_wakeup,
199 .bat_levels = 40, 204 .bat_levels = 40,