1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 by Michael Sevakis
11 * Copyright (C) 2008 by Bertrik Sikken
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 ****************************************************************************/
28 #include "powermgmt.h"
32 /*===========================================================================
33 * These parameters may be defined per target:
35 * BATT_FULL_VOLTAGE - Upon connect a charge cycle begins if the reading is
36 * lower than this value (millivolts).
38 * BATT_VAUTO_RECHARGE - While left plugged after cycle completion, the
39 * charger restarts automatically if the reading drops
40 * below this value (millivolts). Must be less than
43 * ADC_BATTERY - ADC channel from which to read the battery voltage
45 * BATT_CHG_V - Charger voltage regulation setting (as3514 regval)
47 * BATT_CHG_I - Charger current regulation setting (as3514 regval)
49 * CHARGER_TOTAL_TIMER - Maximum allowed charging time (1/2-second steps)
50 *===========================================================================
53 /* This code assumes USB power input is not distinguishable from main
54 * power and charger connect cannot wait for USB configuration before
55 * considering USB charging available. Where they are distinguishable,
56 * things get more complicated. */
57 static bool charger_close
= false; /* Shutting down? */
58 static int charger_total_timer
= 0; /* Timeout in algorithm steps */
60 /* Current battery threshold for (re)charge:
61 * First plugged = BATT_FULL_VOLTAGE
62 * After charge cycle or non-start = BATT_VAUTO_RECHARGE
64 static unsigned int batt_threshold
= 0;
66 /* ADC should read 0x3ff=5.12V */
67 /* full-scale ADC readout (2^10) in millivolt */
69 /* Returns battery voltage from ADC [millivolts] */
70 int _battery_voltage(void)
72 return (adc_read(ADC_BATTERY
) * 5125 + 512) >> 10;
75 /* Returns true if the unit is charging the batteries. */
76 bool charging_state(void)
78 return charge_state
== CHARGING
;
81 /* Reset the battery filter to a new voltage */
82 static void battery_voltage_sync(void)
87 for (i
= 0, mv
= 0; i
< 5; i
++)
88 mv
+= _battery_voltage();
90 reset_battery_filter(mv
/ 5);
93 /* Disable charger and minimize all settings. Reset timers, etc. */
94 static void disable_charger(void)
96 ascodec_write_charger(TMPSUP_OFF
| CHG_OFF
);
98 if (charge_state
> DISCHARGING
)
99 charge_state
= DISCHARGING
; /* Not an error state already */
101 charger_total_timer
= 0;
102 battery_voltage_sync();
105 /* Enable charger with specified settings. Start timers, etc. */
106 static void enable_charger(void)
108 ascodec_write_charger(BATT_CHG_I
| BATT_CHG_V
);
110 sleep(HZ
/10); /* Allow charger turn-on time (it could be gradual). */
112 #if CONFIG_CPU != AS3525v2
113 /* acknowledge first end of charging interrupt, it seems to happen both
114 * at charger plug and charger unplug
115 * It doesn't happen on newer AS3543
120 charge_state
= CHARGING
;
121 charger_total_timer
= CHARGER_TOTAL_TIMER
;
122 battery_voltage_sync();
125 void powermgmt_init_target(void)
127 /* Everything CHARGER, OFF! */
128 ascodec_monitor_endofch();
129 ascodec_write_charger(TMPSUP_OFF
| CHG_OFF
);
132 static inline void charger_plugged(void)
134 batt_threshold
= BATT_FULL_VOLTAGE
; /* Start with topped value. */
135 battery_voltage_sync();
138 static inline void charger_control(void)
140 switch (charge_state
)
144 unsigned int millivolts
;
145 unsigned int thresh
= batt_threshold
;
147 if (BATT_FULL_VOLTAGE
== thresh
)
149 /* Wait for CHG_status to be indicated. */
150 if (!ascodec_chg_status())
153 batt_threshold
= BATT_VAUTO_RECHARGE
;
156 millivolts
= battery_voltage();
158 if (millivolts
<= thresh
)
165 if (!ascodec_endofch())
167 if (--charger_total_timer
> 0)
170 /* Timer ran out - require replug. */
171 charge_state
= CHARGE_STATE_ERROR
;
173 /* else end of charge */
180 /* DISABLED, ERROR */
185 static inline void charger_unplugged(void)
188 if (charge_state
>= CHARGE_STATE_ERROR
)
189 charge_state
= DISCHARGING
; /* Reset error */
192 /* Main charging algorithm - called from powermgmt.c */
193 void charging_algorithm_step(void)
195 switch (charger_input_state
)
201 case CHARGER_PLUGGED
:
209 case CHARGER_UNPLUGGED
:
216 /* Disable further charging and ack. */
217 charge_state
= CHARGE_STATE_DISABLED
;
219 charger_close
= false;
223 /* Disable the charger and prepare for poweroff - called off-thread so we
224 * signal the charging thread to prepare to quit. */
225 void charging_algorithm_close(void)
227 charger_close
= true;
229 /* Power management thread will set it false again. */
230 while (charger_close
)