diff options
author | Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> | 2009-03-28 11:18:51 -0400 |
---|---|---|
committer | Eric Miao <eric.y.miao@gmail.com> | 2009-06-11 10:20:22 -0400 |
commit | 78731d33c1868f4ba43bafcca8dcaf938872c1f2 (patch) | |
tree | a5d9f2ba40173182de56cae3628a5cb3eccfb6fa /arch/arm/mach-pxa/sharpsl_pm.c | |
parent | 391c5111c9ce346edc3c3d8f907eb178a6decd16 (diff) |
[ARM] pxa/sharpsl_pm: merge the two sharpsl_pm.c since it's now pxa specific
collie_pm was the only non-PXA user of sharpsl_pm. Now as it's gone we
can merge code into one single file to allow further cleanup.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Signed-off-by: Eric Miao <eric.miao@marvell.com>
Diffstat (limited to 'arch/arm/mach-pxa/sharpsl_pm.c')
-rw-r--r-- | arch/arm/mach-pxa/sharpsl_pm.c | 834 |
1 files changed, 833 insertions, 1 deletions
diff --git a/arch/arm/mach-pxa/sharpsl_pm.c b/arch/arm/mach-pxa/sharpsl_pm.c index 16b4ec67e3b6..9d61c4916968 100644 --- a/arch/arm/mach-pxa/sharpsl_pm.c +++ b/arch/arm/mach-pxa/sharpsl_pm.c | |||
@@ -21,13 +21,63 @@ | |||
21 | #include <linux/irq.h> | 21 | #include <linux/irq.h> |
22 | #include <linux/platform_device.h> | 22 | #include <linux/platform_device.h> |
23 | #include <linux/apm-emulation.h> | 23 | #include <linux/apm-emulation.h> |
24 | #include <linux/timer.h> | ||
25 | #include <linux/apm_bios.h> | ||
26 | #include <linux/delay.h> | ||
27 | #include <linux/leds.h> | ||
28 | #include <linux/suspend.h> | ||
24 | 29 | ||
25 | #include <mach/hardware.h> | 30 | #include <mach/hardware.h> |
26 | #include <asm/mach-types.h> | 31 | #include <asm/mach-types.h> |
27 | #include <mach/pm.h> | 32 | #include <mach/pm.h> |
33 | #include <mach/pxa2xx-regs.h> | ||
28 | #include <mach/pxa2xx-gpio.h> | 34 | #include <mach/pxa2xx-gpio.h> |
35 | #include <mach/regs-rtc.h> | ||
29 | #include <mach/sharpsl.h> | 36 | #include <mach/sharpsl.h> |
30 | #include "sharpsl.h" | 37 | #include <mach/sharpsl_pm.h> |
38 | |||
39 | /* | ||
40 | * Constants | ||
41 | */ | ||
42 | #define SHARPSL_CHARGE_ON_TIME_INTERVAL (msecs_to_jiffies(1*60*1000)) /* 1 min */ | ||
43 | #define SHARPSL_CHARGE_FINISH_TIME (msecs_to_jiffies(10*60*1000)) /* 10 min */ | ||
44 | #define SHARPSL_BATCHK_TIME (msecs_to_jiffies(15*1000)) /* 15 sec */ | ||
45 | #define SHARPSL_BATCHK_TIME_SUSPEND (60*10) /* 10 min */ | ||
46 | |||
47 | #define SHARPSL_WAIT_CO_TIME 15 /* 15 sec */ | ||
48 | #define SHARPSL_WAIT_DISCHARGE_ON 100 /* 100 msec */ | ||
49 | #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP 10 /* 10 msec */ | ||
50 | #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT 10 /* 10 msec */ | ||
51 | #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN 10 /* 10 msec */ | ||
52 | #define SHARPSL_CHARGE_WAIT_TIME 15 /* 15 msec */ | ||
53 | #define SHARPSL_CHARGE_CO_CHECK_TIME 5 /* 5 msec */ | ||
54 | #define SHARPSL_CHARGE_RETRY_CNT 1 /* eqv. 10 min */ | ||
55 | |||
56 | /* | ||
57 | * Prototypes | ||
58 | */ | ||
59 | #ifdef CONFIG_PM | ||
60 | static int sharpsl_off_charge_battery(void); | ||
61 | static int sharpsl_check_battery_voltage(void); | ||
62 | static int sharpsl_fatal_check(void); | ||
63 | #endif | ||
64 | static int sharpsl_check_battery_temp(void); | ||
65 | static int sharpsl_ac_check(void); | ||
66 | static int sharpsl_average_value(int ad); | ||
67 | static void sharpsl_average_clear(void); | ||
68 | static void sharpsl_charge_toggle(struct work_struct *private_); | ||
69 | static void sharpsl_battery_thread(struct work_struct *private_); | ||
70 | |||
71 | |||
72 | /* | ||
73 | * Variables | ||
74 | */ | ||
75 | struct sharpsl_pm_status sharpsl_pm; | ||
76 | DECLARE_DELAYED_WORK(toggle_charger, sharpsl_charge_toggle); | ||
77 | DECLARE_DELAYED_WORK(sharpsl_bat, sharpsl_battery_thread); | ||
78 | DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger); | ||
79 | |||
80 | |||
31 | 81 | ||
32 | struct battery_thresh spitz_battery_levels_acin[] = { | 82 | struct battery_thresh spitz_battery_levels_acin[] = { |
33 | { 213, 100}, | 83 | { 213, 100}, |
@@ -189,3 +239,785 @@ void sharpsl_pm_pxa_remove(void) | |||
189 | if (sharpsl_pm.machinfo->batfull_irq) | 239 | if (sharpsl_pm.machinfo->batfull_irq) |
190 | free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr); | 240 | free_irq(IRQ_GPIO(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr); |
191 | } | 241 | } |
242 | |||
243 | static int get_percentage(int voltage) | ||
244 | { | ||
245 | int i = sharpsl_pm.machinfo->bat_levels - 1; | ||
246 | int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0; | ||
247 | struct battery_thresh *thresh; | ||
248 | |||
249 | if (sharpsl_pm.charge_mode == CHRG_ON) | ||
250 | thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin; | ||
251 | else | ||
252 | thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac; | ||
253 | |||
254 | while (i > 0 && (voltage > thresh[i].voltage)) | ||
255 | i--; | ||
256 | |||
257 | return thresh[i].percentage; | ||
258 | } | ||
259 | |||
260 | static int get_apm_status(int voltage) | ||
261 | { | ||
262 | int low_thresh, high_thresh; | ||
263 | |||
264 | if (sharpsl_pm.charge_mode == CHRG_ON) { | ||
265 | high_thresh = sharpsl_pm.machinfo->status_high_acin; | ||
266 | low_thresh = sharpsl_pm.machinfo->status_low_acin; | ||
267 | } else { | ||
268 | high_thresh = sharpsl_pm.machinfo->status_high_noac; | ||
269 | low_thresh = sharpsl_pm.machinfo->status_low_noac; | ||
270 | } | ||
271 | |||
272 | if (voltage >= high_thresh) | ||
273 | return APM_BATTERY_STATUS_HIGH; | ||
274 | if (voltage >= low_thresh) | ||
275 | return APM_BATTERY_STATUS_LOW; | ||
276 | return APM_BATTERY_STATUS_CRITICAL; | ||
277 | } | ||
278 | |||
279 | void sharpsl_battery_kick(void) | ||
280 | { | ||
281 | schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125)); | ||
282 | } | ||
283 | EXPORT_SYMBOL(sharpsl_battery_kick); | ||
284 | |||
285 | |||
286 | static void sharpsl_battery_thread(struct work_struct *private_) | ||
287 | { | ||
288 | int voltage, percent, apm_status, i = 0; | ||
289 | |||
290 | if (!sharpsl_pm.machinfo) | ||
291 | return; | ||
292 | |||
293 | sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE); | ||
294 | |||
295 | /* Corgi cannot confirm when battery fully charged so periodically kick! */ | ||
296 | if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON) | ||
297 | && time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_ON_TIME_INTERVAL)) | ||
298 | schedule_delayed_work(&toggle_charger, 0); | ||
299 | |||
300 | while(1) { | ||
301 | voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT); | ||
302 | |||
303 | if (voltage > 0) break; | ||
304 | if (i++ > 5) { | ||
305 | voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage; | ||
306 | dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n"); | ||
307 | break; | ||
308 | } | ||
309 | } | ||
310 | |||
311 | voltage = sharpsl_average_value(voltage); | ||
312 | apm_status = get_apm_status(voltage); | ||
313 | percent = get_percentage(voltage); | ||
314 | |||
315 | /* At low battery voltages, the voltage has a tendency to start | ||
316 | creeping back up so we try to avoid this here */ | ||
317 | if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE) || (apm_status == APM_BATTERY_STATUS_HIGH) || percent <= sharpsl_pm.battstat.mainbat_percent) { | ||
318 | sharpsl_pm.battstat.mainbat_voltage = voltage; | ||
319 | sharpsl_pm.battstat.mainbat_status = apm_status; | ||
320 | sharpsl_pm.battstat.mainbat_percent = percent; | ||
321 | } | ||
322 | |||
323 | dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage, | ||
324 | sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies); | ||
325 | |||
326 | #ifdef CONFIG_BACKLIGHT_CORGI | ||
327 | /* If battery is low. limit backlight intensity to save power. */ | ||
328 | if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE) | ||
329 | && ((sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_LOW) || | ||
330 | (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL))) { | ||
331 | if (!(sharpsl_pm.flags & SHARPSL_BL_LIMIT)) { | ||
332 | sharpsl_pm.machinfo->backlight_limit(1); | ||
333 | sharpsl_pm.flags |= SHARPSL_BL_LIMIT; | ||
334 | } | ||
335 | } else if (sharpsl_pm.flags & SHARPSL_BL_LIMIT) { | ||
336 | sharpsl_pm.machinfo->backlight_limit(0); | ||
337 | sharpsl_pm.flags &= ~SHARPSL_BL_LIMIT; | ||
338 | } | ||
339 | #endif | ||
340 | |||
341 | /* Suspend if critical battery level */ | ||
342 | if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE) | ||
343 | && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL) | ||
344 | && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) { | ||
345 | sharpsl_pm.flags |= SHARPSL_APM_QUEUED; | ||
346 | dev_err(sharpsl_pm.dev, "Fatal Off\n"); | ||
347 | apm_queue_event(APM_CRITICAL_SUSPEND); | ||
348 | } | ||
349 | |||
350 | schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME); | ||
351 | } | ||
352 | |||
353 | void sharpsl_pm_led(int val) | ||
354 | { | ||
355 | if (val == SHARPSL_LED_ERROR) { | ||
356 | dev_err(sharpsl_pm.dev, "Charging Error!\n"); | ||
357 | } else if (val == SHARPSL_LED_ON) { | ||
358 | dev_dbg(sharpsl_pm.dev, "Charge LED On\n"); | ||
359 | led_trigger_event(sharpsl_charge_led_trigger, LED_FULL); | ||
360 | } else { | ||
361 | dev_dbg(sharpsl_pm.dev, "Charge LED Off\n"); | ||
362 | led_trigger_event(sharpsl_charge_led_trigger, LED_OFF); | ||
363 | } | ||
364 | } | ||
365 | |||
366 | static void sharpsl_charge_on(void) | ||
367 | { | ||
368 | dev_dbg(sharpsl_pm.dev, "Turning Charger On\n"); | ||
369 | |||
370 | sharpsl_pm.full_count = 0; | ||
371 | sharpsl_pm.charge_mode = CHRG_ON; | ||
372 | schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250)); | ||
373 | schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500)); | ||
374 | } | ||
375 | |||
376 | static void sharpsl_charge_off(void) | ||
377 | { | ||
378 | dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n"); | ||
379 | |||
380 | sharpsl_pm.machinfo->charge(0); | ||
381 | sharpsl_pm_led(SHARPSL_LED_OFF); | ||
382 | sharpsl_pm.charge_mode = CHRG_OFF; | ||
383 | |||
384 | schedule_delayed_work(&sharpsl_bat, 0); | ||
385 | } | ||
386 | |||
387 | static void sharpsl_charge_error(void) | ||
388 | { | ||
389 | sharpsl_pm_led(SHARPSL_LED_ERROR); | ||
390 | sharpsl_pm.machinfo->charge(0); | ||
391 | sharpsl_pm.charge_mode = CHRG_ERROR; | ||
392 | } | ||
393 | |||
394 | static void sharpsl_charge_toggle(struct work_struct *private_) | ||
395 | { | ||
396 | dev_dbg(sharpsl_pm.dev, "Toogling Charger at time: %lx\n", jiffies); | ||
397 | |||
398 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) { | ||
399 | sharpsl_charge_off(); | ||
400 | return; | ||
401 | } else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) { | ||
402 | sharpsl_charge_error(); | ||
403 | return; | ||
404 | } | ||
405 | |||
406 | sharpsl_pm_led(SHARPSL_LED_ON); | ||
407 | sharpsl_pm.machinfo->charge(0); | ||
408 | mdelay(SHARPSL_CHARGE_WAIT_TIME); | ||
409 | sharpsl_pm.machinfo->charge(1); | ||
410 | |||
411 | sharpsl_pm.charge_start_time = jiffies; | ||
412 | } | ||
413 | |||
414 | static void sharpsl_ac_timer(unsigned long data) | ||
415 | { | ||
416 | int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); | ||
417 | |||
418 | dev_dbg(sharpsl_pm.dev, "AC Status: %d\n",acin); | ||
419 | |||
420 | sharpsl_average_clear(); | ||
421 | if (acin && (sharpsl_pm.charge_mode != CHRG_ON)) | ||
422 | sharpsl_charge_on(); | ||
423 | else if (sharpsl_pm.charge_mode == CHRG_ON) | ||
424 | sharpsl_charge_off(); | ||
425 | |||
426 | schedule_delayed_work(&sharpsl_bat, 0); | ||
427 | } | ||
428 | |||
429 | |||
430 | irqreturn_t sharpsl_ac_isr(int irq, void *dev_id) | ||
431 | { | ||
432 | /* Delay the event slightly to debounce */ | ||
433 | /* Must be a smaller delay than the chrg_full_isr below */ | ||
434 | mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250)); | ||
435 | |||
436 | return IRQ_HANDLED; | ||
437 | } | ||
438 | |||
439 | static void sharpsl_chrg_full_timer(unsigned long data) | ||
440 | { | ||
441 | dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies); | ||
442 | |||
443 | sharpsl_pm.full_count++; | ||
444 | |||
445 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) { | ||
446 | dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n"); | ||
447 | if (sharpsl_pm.charge_mode == CHRG_ON) | ||
448 | sharpsl_charge_off(); | ||
449 | } else if (sharpsl_pm.full_count < 2) { | ||
450 | dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n"); | ||
451 | schedule_delayed_work(&toggle_charger, 0); | ||
452 | } else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) { | ||
453 | dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n"); | ||
454 | schedule_delayed_work(&toggle_charger, 0); | ||
455 | } else { | ||
456 | sharpsl_charge_off(); | ||
457 | sharpsl_pm.charge_mode = CHRG_DONE; | ||
458 | dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n"); | ||
459 | } | ||
460 | } | ||
461 | |||
462 | /* Charging Finished Interrupt (Not present on Corgi) */ | ||
463 | /* Can trigger at the same time as an AC status change so | ||
464 | delay until after that has been processed */ | ||
465 | irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id) | ||
466 | { | ||
467 | if (sharpsl_pm.flags & SHARPSL_SUSPENDED) | ||
468 | return IRQ_HANDLED; | ||
469 | |||
470 | /* delay until after any ac interrupt */ | ||
471 | mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500)); | ||
472 | |||
473 | return IRQ_HANDLED; | ||
474 | } | ||
475 | |||
476 | irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id) | ||
477 | { | ||
478 | int is_fatal = 0; | ||
479 | |||
480 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) { | ||
481 | dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n"); | ||
482 | is_fatal = 1; | ||
483 | } | ||
484 | |||
485 | if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) { | ||
486 | dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n"); | ||
487 | is_fatal = 1; | ||
488 | } | ||
489 | |||
490 | if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) { | ||
491 | sharpsl_pm.flags |= SHARPSL_APM_QUEUED; | ||
492 | apm_queue_event(APM_CRITICAL_SUSPEND); | ||
493 | } | ||
494 | |||
495 | return IRQ_HANDLED; | ||
496 | } | ||
497 | |||
498 | /* | ||
499 | * Maintain an average of the last 10 readings | ||
500 | */ | ||
501 | #define SHARPSL_CNV_VALUE_NUM 10 | ||
502 | static int sharpsl_ad_index; | ||
503 | |||
504 | static void sharpsl_average_clear(void) | ||
505 | { | ||
506 | sharpsl_ad_index = 0; | ||
507 | } | ||
508 | |||
509 | static int sharpsl_average_value(int ad) | ||
510 | { | ||
511 | int i, ad_val = 0; | ||
512 | static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1]; | ||
513 | |||
514 | if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) { | ||
515 | sharpsl_ad_index = 0; | ||
516 | return ad; | ||
517 | } | ||
518 | |||
519 | sharpsl_ad[sharpsl_ad_index] = ad; | ||
520 | sharpsl_ad_index++; | ||
521 | if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) { | ||
522 | for (i=0; i < (SHARPSL_CNV_VALUE_NUM-1); i++) | ||
523 | sharpsl_ad[i] = sharpsl_ad[i+1]; | ||
524 | sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1; | ||
525 | } | ||
526 | for (i=0; i < sharpsl_ad_index; i++) | ||
527 | ad_val += sharpsl_ad[i]; | ||
528 | |||
529 | return (ad_val / sharpsl_ad_index); | ||
530 | } | ||
531 | |||
532 | /* | ||
533 | * Take an array of 5 integers, remove the maximum and minimum values | ||
534 | * and return the average. | ||
535 | */ | ||
536 | static int get_select_val(int *val) | ||
537 | { | ||
538 | int i, j, k, temp, sum = 0; | ||
539 | |||
540 | /* Find MAX val */ | ||
541 | temp = val[0]; | ||
542 | j=0; | ||
543 | for (i=1; i<5; i++) { | ||
544 | if (temp < val[i]) { | ||
545 | temp = val[i]; | ||
546 | j = i; | ||
547 | } | ||
548 | } | ||
549 | |||
550 | /* Find MIN val */ | ||
551 | temp = val[4]; | ||
552 | k=4; | ||
553 | for (i=3; i>=0; i--) { | ||
554 | if (temp > val[i]) { | ||
555 | temp = val[i]; | ||
556 | k = i; | ||
557 | } | ||
558 | } | ||
559 | |||
560 | for (i=0; i<5; i++) | ||
561 | if (i != j && i != k ) | ||
562 | sum += val[i]; | ||
563 | |||
564 | 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]); | ||
565 | |||
566 | return (sum/3); | ||
567 | } | ||
568 | |||
569 | static int sharpsl_check_battery_temp(void) | ||
570 | { | ||
571 | int val, i, buff[5]; | ||
572 | |||
573 | /* Check battery temperature */ | ||
574 | for (i=0; i<5; i++) { | ||
575 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP); | ||
576 | sharpsl_pm.machinfo->measure_temp(1); | ||
577 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP); | ||
578 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP); | ||
579 | sharpsl_pm.machinfo->measure_temp(0); | ||
580 | } | ||
581 | |||
582 | val = get_select_val(buff); | ||
583 | |||
584 | dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val); | ||
585 | if (val > sharpsl_pm.machinfo->charge_on_temp) { | ||
586 | printk(KERN_WARNING "Not charging: temperature out of limits.\n"); | ||
587 | return -1; | ||
588 | } | ||
589 | |||
590 | return 0; | ||
591 | } | ||
592 | |||
593 | #ifdef CONFIG_PM | ||
594 | static int sharpsl_check_battery_voltage(void) | ||
595 | { | ||
596 | int val, i, buff[5]; | ||
597 | |||
598 | /* disable charge, enable discharge */ | ||
599 | sharpsl_pm.machinfo->charge(0); | ||
600 | sharpsl_pm.machinfo->discharge(1); | ||
601 | mdelay(SHARPSL_WAIT_DISCHARGE_ON); | ||
602 | |||
603 | if (sharpsl_pm.machinfo->discharge1) | ||
604 | sharpsl_pm.machinfo->discharge1(1); | ||
605 | |||
606 | /* Check battery voltage */ | ||
607 | for (i=0; i<5; i++) { | ||
608 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT); | ||
609 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT); | ||
610 | } | ||
611 | |||
612 | if (sharpsl_pm.machinfo->discharge1) | ||
613 | sharpsl_pm.machinfo->discharge1(0); | ||
614 | |||
615 | sharpsl_pm.machinfo->discharge(0); | ||
616 | |||
617 | val = get_select_val(buff); | ||
618 | dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val); | ||
619 | |||
620 | if (val < sharpsl_pm.machinfo->charge_on_volt) | ||
621 | return -1; | ||
622 | |||
623 | return 0; | ||
624 | } | ||
625 | #endif | ||
626 | |||
627 | static int sharpsl_ac_check(void) | ||
628 | { | ||
629 | int temp, i, buff[5]; | ||
630 | |||
631 | for (i=0; i<5; i++) { | ||
632 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT); | ||
633 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN); | ||
634 | } | ||
635 | |||
636 | temp = get_select_val(buff); | ||
637 | dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n",temp); | ||
638 | |||
639 | if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) { | ||
640 | dev_err(sharpsl_pm.dev, "Error: AC check failed.\n"); | ||
641 | return -1; | ||
642 | } | ||
643 | |||
644 | return 0; | ||
645 | } | ||
646 | |||
647 | #ifdef CONFIG_PM | ||
648 | static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state) | ||
649 | { | ||
650 | sharpsl_pm.flags |= SHARPSL_SUSPENDED; | ||
651 | flush_scheduled_work(); | ||
652 | |||
653 | if (sharpsl_pm.charge_mode == CHRG_ON) | ||
654 | sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG; | ||
655 | else | ||
656 | sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG; | ||
657 | |||
658 | return 0; | ||
659 | } | ||
660 | |||
661 | static int sharpsl_pm_resume(struct platform_device *pdev) | ||
662 | { | ||
663 | /* Clear the reset source indicators as they break the bootloader upon reboot */ | ||
664 | RCSR = 0x0f; | ||
665 | sharpsl_average_clear(); | ||
666 | sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED; | ||
667 | sharpsl_pm.flags &= ~SHARPSL_SUSPENDED; | ||
668 | |||
669 | return 0; | ||
670 | } | ||
671 | |||
672 | static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state) | ||
673 | { | ||
674 | dev_dbg(sharpsl_pm.dev, "Time is: %08x\n",RCNR); | ||
675 | |||
676 | dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n",sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG); | ||
677 | /* not charging and AC-IN! */ | ||
678 | |||
679 | if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) { | ||
680 | dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n"); | ||
681 | sharpsl_pm.charge_mode = CHRG_OFF; | ||
682 | sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG; | ||
683 | sharpsl_off_charge_battery(); | ||
684 | } | ||
685 | |||
686 | sharpsl_pm.machinfo->presuspend(); | ||
687 | |||
688 | PEDR = 0xffffffff; /* clear it */ | ||
689 | |||
690 | sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE; | ||
691 | if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) { | ||
692 | RTSR &= RTSR_ALE; | ||
693 | RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND; | ||
694 | dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n",RTAR); | ||
695 | sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE; | ||
696 | } else if (alarm_enable) { | ||
697 | RTSR &= RTSR_ALE; | ||
698 | RTAR = alarm_time; | ||
699 | dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n",RTAR); | ||
700 | } else { | ||
701 | dev_dbg(sharpsl_pm.dev, "No alarms set.\n"); | ||
702 | } | ||
703 | |||
704 | pxa_pm_enter(state); | ||
705 | |||
706 | sharpsl_pm.machinfo->postsuspend(); | ||
707 | |||
708 | dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n",PEDR); | ||
709 | } | ||
710 | |||
711 | static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state) | ||
712 | { | ||
713 | if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable) ) | ||
714 | { | ||
715 | if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) { | ||
716 | dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n"); | ||
717 | corgi_goto_sleep(alarm_time, alarm_enable, state); | ||
718 | return 1; | ||
719 | } | ||
720 | if(sharpsl_off_charge_battery()) { | ||
721 | dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n"); | ||
722 | corgi_goto_sleep(alarm_time, alarm_enable, state); | ||
723 | return 1; | ||
724 | } | ||
725 | dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n"); | ||
726 | } | ||
727 | |||
728 | if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) || (sharpsl_fatal_check() < 0) ) | ||
729 | { | ||
730 | dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n"); | ||
731 | corgi_goto_sleep(alarm_time, alarm_enable, state); | ||
732 | return 1; | ||
733 | } | ||
734 | |||
735 | return 0; | ||
736 | } | ||
737 | |||
738 | static int corgi_pxa_pm_enter(suspend_state_t state) | ||
739 | { | ||
740 | unsigned long alarm_time = RTAR; | ||
741 | unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0); | ||
742 | |||
743 | dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n"); | ||
744 | |||
745 | corgi_goto_sleep(alarm_time, alarm_status, state); | ||
746 | |||
747 | while (corgi_enter_suspend(alarm_time,alarm_status,state)) | ||
748 | {} | ||
749 | |||
750 | if (sharpsl_pm.machinfo->earlyresume) | ||
751 | sharpsl_pm.machinfo->earlyresume(); | ||
752 | |||
753 | dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n"); | ||
754 | |||
755 | return 0; | ||
756 | } | ||
757 | |||
758 | /* | ||
759 | * Check for fatal battery errors | ||
760 | * Fatal returns -1 | ||
761 | */ | ||
762 | static int sharpsl_fatal_check(void) | ||
763 | { | ||
764 | int buff[5], temp, i, acin; | ||
765 | |||
766 | dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check entered\n"); | ||
767 | |||
768 | /* Check AC-Adapter */ | ||
769 | acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN); | ||
770 | |||
771 | if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) { | ||
772 | sharpsl_pm.machinfo->charge(0); | ||
773 | udelay(100); | ||
774 | sharpsl_pm.machinfo->discharge(1); /* enable discharge */ | ||
775 | mdelay(SHARPSL_WAIT_DISCHARGE_ON); | ||
776 | } | ||
777 | |||
778 | if (sharpsl_pm.machinfo->discharge1) | ||
779 | sharpsl_pm.machinfo->discharge1(1); | ||
780 | |||
781 | /* Check battery : check inserting battery ? */ | ||
782 | for (i=0; i<5; i++) { | ||
783 | buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT); | ||
784 | mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT); | ||
785 | } | ||
786 | |||
787 | if (sharpsl_pm.machinfo->discharge1) | ||
788 | sharpsl_pm.machinfo->discharge1(0); | ||
789 | |||
790 | if (acin && (sharpsl_pm.charge_mode == CHRG_ON)) { | ||
791 | udelay(100); | ||
792 | sharpsl_pm.machinfo->charge(1); | ||
793 | sharpsl_pm.machinfo->discharge(0); | ||
794 | } | ||
795 | |||
796 | temp = get_select_val(buff); | ||
797 | dev_dbg(sharpsl_pm.dev, "sharpsl_fatal_check: acin: %d, discharge voltage: %d, no discharge: %ld\n", acin, temp, sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT)); | ||
798 | |||
799 | if ((acin && (temp < sharpsl_pm.machinfo->fatal_acin_volt)) || | ||
800 | (!acin && (temp < sharpsl_pm.machinfo->fatal_noacin_volt))) | ||
801 | return -1; | ||
802 | return 0; | ||
803 | } | ||
804 | |||
805 | static int sharpsl_off_charge_error(void) | ||
806 | { | ||
807 | dev_err(sharpsl_pm.dev, "Offline Charger: Error occurred.\n"); | ||
808 | sharpsl_pm.machinfo->charge(0); | ||
809 | sharpsl_pm_led(SHARPSL_LED_ERROR); | ||
810 | sharpsl_pm.charge_mode = CHRG_ERROR; | ||
811 | return 1; | ||
812 | } | ||
813 | |||
814 | /* | ||
815 | * Charging Control while suspended | ||
816 | * Return 1 - go straight to sleep | ||
817 | * Return 0 - sleep or wakeup depending on other factors | ||
818 | */ | ||
819 | static int sharpsl_off_charge_battery(void) | ||
820 | { | ||
821 | int time; | ||
822 | |||
823 | dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode); | ||
824 | |||
825 | if (sharpsl_pm.charge_mode == CHRG_OFF) { | ||
826 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n"); | ||
827 | |||
828 | /* AC Check */ | ||
829 | if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0)) | ||
830 | return sharpsl_off_charge_error(); | ||
831 | |||
832 | /* Start Charging */ | ||
833 | sharpsl_pm_led(SHARPSL_LED_ON); | ||
834 | sharpsl_pm.machinfo->charge(0); | ||
835 | mdelay(SHARPSL_CHARGE_WAIT_TIME); | ||
836 | sharpsl_pm.machinfo->charge(1); | ||
837 | |||
838 | sharpsl_pm.charge_mode = CHRG_ON; | ||
839 | sharpsl_pm.full_count = 0; | ||
840 | |||
841 | return 1; | ||
842 | } else if (sharpsl_pm.charge_mode != CHRG_ON) { | ||
843 | return 1; | ||
844 | } | ||
845 | |||
846 | if (sharpsl_pm.full_count == 0) { | ||
847 | int time; | ||
848 | |||
849 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n"); | ||
850 | |||
851 | if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0)) | ||
852 | return sharpsl_off_charge_error(); | ||
853 | |||
854 | sharpsl_pm.machinfo->charge(0); | ||
855 | mdelay(SHARPSL_CHARGE_WAIT_TIME); | ||
856 | sharpsl_pm.machinfo->charge(1); | ||
857 | sharpsl_pm.charge_mode = CHRG_ON; | ||
858 | |||
859 | mdelay(SHARPSL_CHARGE_CO_CHECK_TIME); | ||
860 | |||
861 | time = RCNR; | ||
862 | while(1) { | ||
863 | /* Check if any wakeup event had occurred */ | ||
864 | if (sharpsl_pm.machinfo->charger_wakeup() != 0) | ||
865 | return 0; | ||
866 | /* Check for timeout */ | ||
867 | if ((RCNR - time) > SHARPSL_WAIT_CO_TIME) | ||
868 | return 1; | ||
869 | if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) { | ||
870 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occurred. Retrying to check\n"); | ||
871 | sharpsl_pm.full_count++; | ||
872 | sharpsl_pm.machinfo->charge(0); | ||
873 | mdelay(SHARPSL_CHARGE_WAIT_TIME); | ||
874 | sharpsl_pm.machinfo->charge(1); | ||
875 | return 1; | ||
876 | } | ||
877 | } | ||
878 | } | ||
879 | |||
880 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n"); | ||
881 | |||
882 | mdelay(SHARPSL_CHARGE_CO_CHECK_TIME); | ||
883 | |||
884 | time = RCNR; | ||
885 | while(1) { | ||
886 | /* Check if any wakeup event had occurred */ | ||
887 | if (sharpsl_pm.machinfo->charger_wakeup() != 0) | ||
888 | return 0; | ||
889 | /* Check for timeout */ | ||
890 | if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) { | ||
891 | if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) { | ||
892 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n"); | ||
893 | sharpsl_pm.full_count = 0; | ||
894 | } | ||
895 | sharpsl_pm.full_count++; | ||
896 | return 1; | ||
897 | } | ||
898 | if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) { | ||
899 | dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n"); | ||
900 | sharpsl_pm_led(SHARPSL_LED_OFF); | ||
901 | sharpsl_pm.machinfo->charge(0); | ||
902 | sharpsl_pm.charge_mode = CHRG_DONE; | ||
903 | return 1; | ||
904 | } | ||
905 | } | ||
906 | } | ||
907 | #else | ||
908 | #define sharpsl_pm_suspend NULL | ||
909 | #define sharpsl_pm_resume NULL | ||
910 | #endif | ||
911 | |||
912 | static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf) | ||
913 | { | ||
914 | return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_percent); | ||
915 | } | ||
916 | |||
917 | static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf) | ||
918 | { | ||
919 | return sprintf(buf, "%d\n",sharpsl_pm.battstat.mainbat_voltage); | ||
920 | } | ||
921 | |||
922 | static DEVICE_ATTR(battery_percentage, 0444, battery_percentage_show, NULL); | ||
923 | static DEVICE_ATTR(battery_voltage, 0444, battery_voltage_show, NULL); | ||
924 | |||
925 | extern void (*apm_get_power_status)(struct apm_power_info *); | ||
926 | |||
927 | static void sharpsl_apm_get_power_status(struct apm_power_info *info) | ||
928 | { | ||
929 | info->ac_line_status = sharpsl_pm.battstat.ac_status; | ||
930 | |||
931 | if (sharpsl_pm.charge_mode == CHRG_ON) | ||
932 | info->battery_status = APM_BATTERY_STATUS_CHARGING; | ||
933 | else | ||
934 | info->battery_status = sharpsl_pm.battstat.mainbat_status; | ||
935 | |||
936 | info->battery_flag = (1 << info->battery_status); | ||
937 | info->battery_life = sharpsl_pm.battstat.mainbat_percent; | ||
938 | } | ||
939 | |||
940 | #ifdef CONFIG_PM | ||
941 | static struct platform_suspend_ops sharpsl_pm_ops = { | ||
942 | .enter = corgi_pxa_pm_enter, | ||
943 | .valid = suspend_valid_only_mem, | ||
944 | }; | ||
945 | #endif | ||
946 | |||
947 | static int __init sharpsl_pm_probe(struct platform_device *pdev) | ||
948 | { | ||
949 | int ret; | ||
950 | |||
951 | if (!pdev->dev.platform_data) | ||
952 | return -EINVAL; | ||
953 | |||
954 | sharpsl_pm.dev = &pdev->dev; | ||
955 | sharpsl_pm.machinfo = pdev->dev.platform_data; | ||
956 | sharpsl_pm.charge_mode = CHRG_OFF; | ||
957 | sharpsl_pm.flags = 0; | ||
958 | |||
959 | init_timer(&sharpsl_pm.ac_timer); | ||
960 | sharpsl_pm.ac_timer.function = sharpsl_ac_timer; | ||
961 | |||
962 | init_timer(&sharpsl_pm.chrg_full_timer); | ||
963 | sharpsl_pm.chrg_full_timer.function = sharpsl_chrg_full_timer; | ||
964 | |||
965 | led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger); | ||
966 | |||
967 | sharpsl_pm.machinfo->init(); | ||
968 | |||
969 | ret = device_create_file(&pdev->dev, &dev_attr_battery_percentage); | ||
970 | ret |= device_create_file(&pdev->dev, &dev_attr_battery_voltage); | ||
971 | if (ret != 0) | ||
972 | dev_warn(&pdev->dev, "Failed to register attributes (%d)\n", ret); | ||
973 | |||
974 | apm_get_power_status = sharpsl_apm_get_power_status; | ||
975 | |||
976 | #ifdef CONFIG_PM | ||
977 | suspend_set_ops(&sharpsl_pm_ops); | ||
978 | #endif | ||
979 | |||
980 | mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250)); | ||
981 | |||
982 | return 0; | ||
983 | } | ||
984 | |||
985 | static int sharpsl_pm_remove(struct platform_device *pdev) | ||
986 | { | ||
987 | suspend_set_ops(NULL); | ||
988 | |||
989 | device_remove_file(&pdev->dev, &dev_attr_battery_percentage); | ||
990 | device_remove_file(&pdev->dev, &dev_attr_battery_voltage); | ||
991 | |||
992 | led_trigger_unregister_simple(sharpsl_charge_led_trigger); | ||
993 | |||
994 | sharpsl_pm.machinfo->exit(); | ||
995 | |||
996 | del_timer_sync(&sharpsl_pm.chrg_full_timer); | ||
997 | del_timer_sync(&sharpsl_pm.ac_timer); | ||
998 | |||
999 | return 0; | ||
1000 | } | ||
1001 | |||
1002 | static struct platform_driver sharpsl_pm_driver = { | ||
1003 | .probe = sharpsl_pm_probe, | ||
1004 | .remove = sharpsl_pm_remove, | ||
1005 | .suspend = sharpsl_pm_suspend, | ||
1006 | .resume = sharpsl_pm_resume, | ||
1007 | .driver = { | ||
1008 | .name = "sharpsl-pm", | ||
1009 | }, | ||
1010 | }; | ||
1011 | |||
1012 | static int __devinit sharpsl_pm_init(void) | ||
1013 | { | ||
1014 | return platform_driver_register(&sharpsl_pm_driver); | ||
1015 | } | ||
1016 | |||
1017 | static void sharpsl_pm_exit(void) | ||
1018 | { | ||
1019 | platform_driver_unregister(&sharpsl_pm_driver); | ||
1020 | } | ||
1021 | |||
1022 | late_initcall(sharpsl_pm_init); | ||
1023 | module_exit(sharpsl_pm_exit); | ||