1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
11 * Revisions copyright (C) 2005 by Gerald Van Baren
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
26 #include "powermgmt.h"
29 #define BATT_MINMVOLT 3300 /* minimum millivolts of battery */
30 #define BATT_MAXMVOLT 4300 /* maximum millivolts of battery */
31 #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in
33 /* Number of millivolts to discharge the battery every second */
34 #define BATT_DISCHARGE_STEP ((BATT_MAXMVOLT - BATT_MINMVOLT) / 100)
35 /* Number of millivolts to charge the battery every second */
36 #define BATT_CHARGE_STEP (BATT_DISCHARGE_STEP * 2)
37 #if CONFIG_CHARGING >= CHARGING_MONITOR
38 /* Number of seconds to externally power before discharging again */
39 #define POWER_AFTER_CHARGE_TICKS (8 * HZ)
42 extern int battery_percent
;
43 static bool charging
= false;
44 static unsigned int battery_millivolts
= BATT_MAXMVOLT
;
46 void powermgmt_init_target(void) {}
48 static void battery_status_update(void)
50 /* Delay next battery update until tick */
51 static long update_after_tick
= 0;
52 #if CONFIG_CHARGING >= CHARGING_MONITOR
53 /* When greater than 0, the tick to unplug the external power at */
54 static unsigned int ext_power_until_tick
= 0;
57 if TIME_BEFORE(current_tick
, update_after_tick
)
60 update_after_tick
= current_tick
+ HZ
;
62 #if CONFIG_CHARGING >= CHARGING_MONITOR
63 /* Handle period of being externally powered */
64 if (ext_power_until_tick
> 0) {
65 if (TIME_AFTER(current_tick
, ext_power_until_tick
)) {
66 /* Pretend the charger was disconnected */
67 charger_input_state
= CHARGER_UNPLUGGED
;
68 ext_power_until_tick
= 0;
75 battery_millivolts
+= BATT_CHARGE_STEP
;
76 if (battery_millivolts
>= BATT_MAXMVOLT
) {
78 battery_percent
= 100;
79 #if CONFIG_CHARGING >= CHARGING_MONITOR
80 /* Keep external power until tick */
81 ext_power_until_tick
= current_tick
+ POWER_AFTER_CHARGE_TICKS
;
83 /* Pretend the charger was disconnected */
84 charger_input_state
= CHARGER_UNPLUGGED
;
89 battery_millivolts
-= BATT_DISCHARGE_STEP
;
90 if (battery_millivolts
<= BATT_MINMVOLT
) {
94 /* Pretend the charger was connected */
95 charger_input_state
= CHARGER_PLUGGED
;
101 battery_percent
= 100 * (battery_millivolts
- BATT_MINMVOLT
) /
102 (BATT_MAXMVOLT
- BATT_MINMVOLT
);
105 const unsigned short battery_level_dangerous
[BATTERY_TYPES_COUNT
] = { 3200 };
106 const unsigned short battery_level_shutoff
[BATTERY_TYPES_COUNT
] = { 3200 };
108 /* make the simulated curve nicely linear */
109 const unsigned short percent_to_volt_discharge
[BATTERY_TYPES_COUNT
][11] =
110 { { 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300 } };
111 const unsigned short percent_to_volt_charge
[11] =
112 { 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300 };
115 int _battery_voltage(void)
117 battery_status_update();
118 return battery_millivolts
;
122 unsigned int power_input_status(void)
124 unsigned int status
= charger_input_state
>= CHARGER_PLUGGED
125 ? POWER_INPUT_CHARGER
: POWER_INPUT_NONE
;
127 #ifdef HAVE_BATTERY_SWITCH
128 status
|= POWER_INPUT_BATTERY
;
134 bool charging_state(void)
140 #ifdef HAVE_ACCESSORY_SUPPLY
141 void accessory_supply_set(bool enable
)
147 #ifdef HAVE_LINEOUT_POWEROFF
148 void lineout_set(bool enable
)
154 #ifdef HAVE_REMOTE_LCD
155 bool remote_detect(void)
161 #ifdef HAVE_BATTERY_SWITCH
162 unsigned int input_millivolts(void)
164 if ((power_input_status() & POWER_INPUT_BATTERY
) == 0) {
165 /* Just return a safe value if battery isn't connected */
168 return battery_voltage();;