Trim down peak calculation a bit.
[kugel-rb.git] / firmware / export / powermgmt.h
blobd86118cd9b1df51b2ae93188ff3a83e5f6bab0f9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #ifndef _POWERMGMT_H_
22 #define _POWERMGMT_H_
24 #include <stdbool.h>
26 #define POWER_HISTORY_LEN 2*60 /* 2 hours of samples, one per minute */
28 enum charge_state_type
30 /* sorted by increasing charging current (do not change!) */
31 #if CONFIG_CHARGING >= CHARGING_MONITOR
32 CHARGE_STATE_DISABLED = -2, /* Disable charger use (safety measure) */
33 CHARGE_STATE_ERROR = -1, /* Some error occurred that should not allow
34 turning on the charger again by software
35 without user intervention (ie. replug) */
36 #endif
37 DISCHARGING = 0,
38 #if CONFIG_CHARGING >= CHARGING_MONITOR
39 TRICKLE, /* For NiCd, battery maintenence phase */
40 /* For LiIon, low-current precharge phase */
41 TOPOFF, /* For NiCd, waiting for dead zone */
42 /* For LiIon, constant voltage phase */
43 CHARGING, /* For NiCd, main charge phase */
44 /* For LiIon, constant current phase */
45 #endif
48 /* tells what the charger is doing */
49 extern enum charge_state_type charge_state;
51 #ifdef CONFIG_CHARGING
53 * Flag that the charger has been plugged in/removed: this is set for exactly
54 * one time through the power loop when the charger has been plugged in.
56 enum charger_input_state_type
58 NO_CHARGER = 0, /* No charger is present */
59 CHARGER_UNPLUGGED, /* Transitional state during CHARGER=>NO_CHARGER */
60 CHARGER_PLUGGED, /* Transitional state during NO_CHARGER=>CHARGER */
61 CHARGER /* Charger is present */
64 /* tells the state of the charge input */
65 extern enum charger_input_state_type charger_input_state;
67 /* Power input status saved on the power thread each loop */
68 extern unsigned int power_thread_inputs;
70 #endif /* CONFIG_CHARGING */
72 #if CONFIG_CHARGING == CHARGING_TARGET
73 /* Include target-specific definitions */
74 #include "powermgmt-target.h"
75 #endif
77 #ifndef SIMULATOR
79 /* Generic current values that are intentionally meaningless - config header
80 * should define proper numbers.*/
83 #ifndef CURRENT_BACKLIGHT
84 #define CURRENT_BACKLIGHT 5 /* additional current when backlight always on */
85 #endif
87 #if defined(HAVE_RECORDING) && !defined(CURRENT_RECORD)
88 #define CURRENT_RECORD 2 /* additional recording current */
89 #endif /* HAVE_RECORDING && !CURRENT_RECORD*/
91 #ifndef CURRENT_USB
92 #define CURRENT_USB 2 /* usual current in mA in USB mode */
93 #endif
95 #if defined(HAVE_REMOTE_LCD) && !defined(CURRENT_REMOTE)
96 #define CURRENT_REMOTE 2 /* additional current when remote connected */
97 #endif /* CURRENT_REMOTE && !HAVE_REMOTE_LCD */
99 #if CONFIG_CHARGING
100 #ifndef CURRENT_MAX_CHG
101 #define CURRENT_MAX_CHG 350 /* maximum charging current */
102 #endif
103 #endif /* CONFIG_CHARGING */
105 #ifdef CHARGING_DEBUG_FILE
106 #define POWERMGMT_DEBUG_STACK ((0x1000)/sizeof(long))
107 #else
108 #define POWERMGMT_DEBUG_STACK 0
109 #endif
111 #ifndef BATT_AVE_SAMPLES
112 /* slw filter constant unless otherwise specified */
113 #define BATT_AVE_SAMPLES 128
114 #endif
116 #ifndef POWER_THREAD_STEP_TICKS
117 /* 2HZ sample rate unless otherwise specified */
118 #define POWER_THREAD_STEP_TICKS (HZ/2)
119 #endif
121 extern unsigned short power_history[POWER_HISTORY_LEN];
122 extern const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT];
123 extern const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT];
124 extern const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11];
125 #if CONFIG_CHARGING
126 extern const unsigned short percent_to_volt_charge[11];
127 #endif
129 /* Start up power management thread */
130 void powermgmt_init(void) INIT_ATTR;
132 #endif /* SIMULATOR */
134 /* Returns battery statust */
135 int battery_level(void); /* percent */
136 int battery_time(void); /* minutes */
137 unsigned int battery_adc_voltage(void); /* voltage from ADC in millivolts */
138 unsigned int battery_voltage(void); /* filtered batt. voltage in millivolts */
140 #ifdef HAVE_BATTERY_SWITCH
141 unsigned int input_millivolts(void); /* voltage that device is running from */
142 #endif /* HAVE_BATTERY_SWITCH */
144 #if defined(HAVE_BATTERY_SWITCH) || defined(HAVE_RESET_BATTERY_FILTER)
145 /* Set the filtered battery voltage (to adjust it before beginning a charge
146 * cycle for instance where old, loaded readings will likely be invalid).
147 * Also readjust when battery switch is opened or closed.
149 void reset_battery_filter(int millivolts);
150 #endif /* HAVE_BATTERY_SWITCH || HAVE_RESET_BATTERY_FILTER */
153 /* read unfiltered battery info */
154 void battery_read_info(int *voltage, int *level);
156 /* Tells if the battery level is safe for disk writes */
157 bool battery_level_safe(void);
159 void set_poweroff_timeout(int timeout);
160 void set_battery_capacity(int capacity); /* set local battery capacity value */
161 int get_battery_capacity(void); /* get local battery capacity value */
162 void set_battery_type(int type); /* set local battery type */
164 void set_sleep_timer(int seconds);
165 int get_sleep_timer(void);
166 void set_car_adapter_mode(bool setting);
167 void reset_poweroff_timer(void);
168 void cancel_shutdown(void);
169 void shutdown_hw(void);
170 void sys_poweroff(void);
171 /* Returns true if the system should force shutdown for some reason -
172 * eg. low battery */
173 bool query_force_shutdown(void);
174 #ifdef HAVE_ACCESSORY_SUPPLY
175 void accessory_supply_set(bool);
176 #endif
177 #ifdef HAVE_LINEOUT_POWEROFF
178 void lineout_set(bool);
179 #endif
181 #endif /* _POWERMGMT_H_ */