summaryrefslogtreecommitdiff
path: root/firmware/powermgmt.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/powermgmt.c')
-rw-r--r--firmware/powermgmt.c68
1 files changed, 24 insertions, 44 deletions
diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c
index 15977caed6..ee01fd862d 100644
--- a/firmware/powermgmt.c
+++ b/firmware/powermgmt.c
@@ -64,7 +64,7 @@
64 * in it (one sample per minute). This is only for very low level debug. 64 * in it (one sample per minute). This is only for very low level debug.
65 */ 65 */
66#undef DEBUG_FILE 66#undef DEBUG_FILE
67#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL) 67#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
68#include "file.h" 68#include "file.h"
69#define DEBUG_FILE_NAME "/powermgmt.csv" 69#define DEBUG_FILE_NAME "/powermgmt.csv"
70#define DEBUG_MESSAGE_LEN 133 70#define DEBUG_MESSAGE_LEN 133
@@ -190,7 +190,7 @@ static const short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
190#endif 190#endif
191}; 191};
192 192
193#ifdef HAVE_CHARGING 193#ifdef CONFIG_CHARGING
194charger_input_state_type charger_input_state IDATA_ATTR; 194charger_input_state_type charger_input_state IDATA_ATTR;
195 195
196/* voltages (centivolt) of 0%, 10%, ... 100% when charging enabled */ 196/* voltages (centivolt) of 0%, 10%, ... 100% when charging enabled */
@@ -207,15 +207,13 @@ static const short percent_to_volt_charge[11] =
207 476, 544, 551, 556, 561, 564, 566, 576, 582, 584, 585 /* NiMH */ 207 476, 544, 551, 556, 561, 564, 566, 576, 582, 584, 585 /* NiMH */
208#endif 208#endif
209}; 209};
210#endif /* HAVE_CHARGING */ 210#endif /* CONFIG_CHARGING */
211 211
212#if defined(HAVE_CHARGE_CTRL) || \ 212#if CONFIG_CHARGING >= CHARGING_MONITOR
213 CONFIG_BATTERY == BATT_LIION2200 || \
214 defined(HAVE_CHARGE_STATE)
215charge_state_type charge_state; /* charging mode */ 213charge_state_type charge_state; /* charging mode */
216#endif 214#endif
217 215
218#ifdef HAVE_CHARGE_CTRL 216#if CONFIG_CHARGING == CHARGING_CONTROL
219int long_delta; /* long term delta battery voltage */ 217int long_delta; /* long term delta battery voltage */
220int short_delta; /* short term delta battery voltage */ 218int short_delta; /* short term delta battery voltage */
221bool disk_activity_last_cycle = false; /* flag set to aid charger time 219bool disk_activity_last_cycle = false; /* flag set to aid charger time
@@ -235,7 +233,7 @@ int trickle_sec = 0; /* how many seconds should the
235 charging? */ 233 charging? */
236int pid_p = 0; /* PID proportional term */ 234int pid_p = 0; /* PID proportional term */
237int pid_i = 0; /* PID integral term */ 235int pid_i = 0; /* PID integral term */
238#endif /* HAVE_CHARGE_CTRL */ 236#endif /* CONFIG_CHARGING == CHARGING_CONTROL */
239 237
240/* 238/*
241 * Average battery voltage and charger voltage, filtered via a digital 239 * Average battery voltage and charger voltage, filtered via a digital
@@ -366,7 +364,7 @@ static void battery_status_update(void)
366{ 364{
367 int level; 365 int level;
368 366
369#if defined(HAVE_CHARGE_CTRL) || defined(HAVE_CHARGE_STATE) 367#if CONFIG_CHARGING >= CHARGING_MONITOR
370 if (charge_state == DISCHARGING) { 368 if (charge_state == DISCHARGING) {
371 level = voltage_to_percent(battery_centivolts, 369 level = voltage_to_percent(battery_centivolts,
372 percent_to_volt_discharge[battery_type]); 370 percent_to_volt_discharge[battery_type]);
@@ -397,7 +395,7 @@ static void battery_status_update(void)
397 /* calculate estimated remaining running time */ 395 /* calculate estimated remaining running time */
398 /* discharging: remaining running time */ 396 /* discharging: remaining running time */
399 /* charging: remaining charging time */ 397 /* charging: remaining charging time */
400#if defined(HAVE_CHARGE_CTRL) || defined(HAVE_CHARGE_STATE) 398#if CONFIG_CHARGING >= CHARGING_MONITOR
401 if (charge_state == CHARGING) { 399 if (charge_state == CHARGING) {
402 powermgmt_est_runningtime_min = (100 - level) * battery_capacity / 100 400 powermgmt_est_runningtime_min = (100 - level) * battery_capacity / 100
403 * 60 / (CURRENT_MAX_CHG - runcurrent()); 401 * 60 / (CURRENT_MAX_CHG - runcurrent());
@@ -425,7 +423,7 @@ static void handle_auto_poweroff(void)
425 long timeout = poweroff_idle_timeout_value[poweroff_timeout]*60*HZ; 423 long timeout = poweroff_idle_timeout_value[poweroff_timeout]*60*HZ;
426 int audio_stat = audio_status(); 424 int audio_stat = audio_status();
427 425
428#ifdef HAVE_CHARGING 426#ifdef CONFIG_CHARGING
429 /* 427 /*
430 * Inhibit shutdown as long as the charger is plugged in. If it is 428 * Inhibit shutdown as long as the charger is plugged in. If it is
431 * unplugged, wait for a timeout period and then shut down. 429 * unplugged, wait for a timeout period and then shut down.
@@ -458,7 +456,7 @@ static void handle_auto_poweroff(void)
458 if(TIME_AFTER(current_tick, sleeptimer_endtick)) 456 if(TIME_AFTER(current_tick, sleeptimer_endtick))
459 { 457 {
460 audio_stop(); 458 audio_stop();
461#if defined(HAVE_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING) 459#if defined(CONFIG_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
462 if((charger_input_state == CHARGER) || 460 if((charger_input_state == CHARGER) ||
463 (charger_input_state == CHARGER_PLUGGED)) 461 (charger_input_state == CHARGER_PLUGGED))
464 { 462 {
@@ -538,7 +536,7 @@ static void power_thread_sleep(int ticks)
538 536
539 while (ticks > 0) { 537 while (ticks > 0) {
540 538
541#ifdef HAVE_CHARGING 539#ifdef CONFIG_CHARGING
542 /* 540 /*
543 * Detect charger plugged/unplugged transitions. On a plugged or 541 * Detect charger plugged/unplugged transitions. On a plugged or
544 * unplugged event, we return immediately, run once through the main 542 * unplugged event, we return immediately, run once through the main
@@ -577,7 +575,7 @@ static void power_thread_sleep(int ticks)
577 } 575 }
578 } 576 }
579#endif 577#endif
580#ifdef HAVE_CHARGE_STATE 578#if CONFIG_CHARGING == CHARGING_MONITOR
581 switch (charger_input_state) { 579 switch (charger_input_state) {
582 case CHARGER_UNPLUGGED: 580 case CHARGER_UNPLUGGED:
583 case NO_CHARGER: 581 case NO_CHARGER:
@@ -593,7 +591,7 @@ static void power_thread_sleep(int ticks)
593 break; 591 break;
594 } 592 }
595 593
596#endif /* HAVE_CHARGE_STATE */ 594#endif /* CONFIG_CHARGING == CHARGING_MONITOR */
597 595
598 small_ticks = MIN(HZ/2, ticks); 596 small_ticks = MIN(HZ/2, ticks);
599 sleep(small_ticks); 597 sleep(small_ticks);
@@ -628,13 +626,13 @@ static void power_thread_sleep(int ticks)
628 battery_status_update(); 626 battery_status_update();
629 627
630 } 628 }
631#ifdef HAVE_CHARGE_CTRL 629#if CONFIG_CHARGING == CHARGING_CONTROL
632 if (ata_disk_is_active()) { 630 if (ata_disk_is_active()) {
633 /* flag hdd use for charging calculation */ 631 /* flag hdd use for charging calculation */
634 disk_activity_last_cycle = true; 632 disk_activity_last_cycle = true;
635 } 633 }
636#endif 634#endif
637#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL) 635#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
638 /* 636 /*
639 * If we have a lot of pending writes or if the disk is spining, 637 * If we have a lot of pending writes or if the disk is spining,
640 * fsync the debug log file. 638 * fsync the debug log file.
@@ -660,7 +658,7 @@ static void power_thread(void)
660{ 658{
661 int i; 659 int i;
662 short *phps, *phpd; /* power history rotation pointers */ 660 short *phps, *phpd; /* power history rotation pointers */
663#ifdef HAVE_CHARGE_CTRL 661#if CONFIG_CHARGING == CHARGING_CONTROL
664 unsigned int target_voltage = TRICKLE_VOLTAGE; /* desired topoff/trickle 662 unsigned int target_voltage = TRICKLE_VOLTAGE; /* desired topoff/trickle
665 * voltage level */ 663 * voltage level */
666 int charge_max_time_idle = 0; /* max. charging duration, calculated at 664 int charge_max_time_idle = 0; /* max. charging duration, calculated at
@@ -678,7 +676,7 @@ static void power_thread(void)
678 BATT_AVE_SAMPLES; 676 BATT_AVE_SAMPLES;
679 battery_centivolts = avgbat / BATT_AVE_SAMPLES / 10000; 677 battery_centivolts = avgbat / BATT_AVE_SAMPLES / 10000;
680 678
681#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL) 679#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
682 fd = -1; 680 fd = -1;
683 wrcount = 0; 681 wrcount = 0;
684#endif 682#endif
@@ -694,25 +692,7 @@ static void power_thread(void)
694 /* insert new value at the start, in centivolts 8-) */ 692 /* insert new value at the start, in centivolts 8-) */
695 power_history[0] = battery_centivolts; 693 power_history[0] = battery_centivolts;
696 694
697#if CONFIG_BATTERY == BATT_LIION2200 695#if CONFIG_CHARGING == CHARGING_CONTROL
698 /* We use the information from the ADC_EXT_POWER ADC channel, which
699 tells us the charging current from the LTC1734. When DC is
700 connected (either via the external adapter, or via USB), we try
701 to determine if it is actively charging or only maintaining the
702 charge. My tests show that ADC readings below about 0x80 means
703 that the LTC1734 is only maintaining the charge. */
704 if(charger_inserted()) {
705 if(adc_read(ADC_EXT_POWER) < 0x80) {
706 charge_state = TRICKLE;
707 } else {
708 charge_state = CHARGING;
709 }
710 } else {
711 charge_state = DISCHARGING;
712 }
713#endif /* # if CONFIG_BATTERY == BATT_LIION2200 */
714
715#ifdef HAVE_CHARGE_CTRL
716 if (charger_input_state == CHARGER_PLUGGED) { 696 if (charger_input_state == CHARGER_PLUGGED) {
717 pid_p = 0; 697 pid_p = 0;
718 pid_i = 0; 698 pid_i = 0;
@@ -876,7 +856,7 @@ static void power_thread(void)
876 } 856 }
877 } 857 }
878 } 858 }
879 else if (charge_state > CHARGING) /* top off or trickle */ 859 else if (charge_state != DISCHARGING) /* top off or trickle */
880 { 860 {
881 /* 861 /*
882 *Time to switch from topoff to trickle? 862 *Time to switch from topoff to trickle?
@@ -946,11 +926,11 @@ static void power_thread(void)
946 snprintf(power_message, POWER_MESSAGE_LEN, "Charger: discharge"); 926 snprintf(power_message, POWER_MESSAGE_LEN, "Charger: discharge");
947 } 927 }
948 928
949#endif /* end HAVE_CHARGE_CTRL */ 929#endif /* CONFIG_CHARGING == CHARGING_CONTROL */
950 930
951 /* sleep for a minute */ 931 /* sleep for a minute */
952 932
953#ifdef HAVE_CHARGE_CTRL 933#if CONFIG_CHARGING == CHARGING_CONTROL
954 if(trickle_sec > 0) { 934 if(trickle_sec > 0) {
955 charger_enable(true); 935 charger_enable(true);
956 power_thread_sleep(HZ * trickle_sec); 936 power_thread_sleep(HZ * trickle_sec);
@@ -962,7 +942,7 @@ static void power_thread(void)
962 power_thread_sleep(HZ * 60); 942 power_thread_sleep(HZ * 60);
963#endif 943#endif
964 944
965#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL) 945#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
966 if(usb_inserted()) { 946 if(usb_inserted()) {
967 if(fd >= 0) { 947 if(fd >= 0) {
968 /* It is probably too late to close the file but we can try...*/ 948 /* It is probably too late to close the file but we can try...*/
@@ -992,7 +972,7 @@ static void power_thread(void)
992#endif 972#endif
993 handle_auto_poweroff(); 973 handle_auto_poweroff();
994 974
995#ifdef HAVE_CHARGE_CTRL 975#if CONFIG_CHARGING == CHARGING_CONTROL
996 powermgmt_last_cycle_startstop_min++; 976 powermgmt_last_cycle_startstop_min++;
997#endif 977#endif
998 } 978 }
@@ -1029,7 +1009,7 @@ void cancel_shutdown(void)
1029void shutdown_hw(void) 1009void shutdown_hw(void)
1030{ 1010{
1031#ifndef SIMULATOR 1011#ifndef SIMULATOR
1032#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL) 1012#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
1033 if(fd >= 0) { 1013 if(fd >= 0) {
1034 close(fd); 1014 close(fd);
1035 fd = -1; 1015 fd = -1;