Gigabeat S: Extend the upper temperature range for battery charging to 50C: OK and...
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / powermgmt-imx31.c
blob6986a0605d3177b4f583a5b61c39be8c2596a15e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2008 by Michael Sevakis
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 #include <stdlib.h>
22 #include "config.h"
23 #include "system.h"
24 #include "thread.h"
25 #include "mc13783.h"
26 #include "adc.h"
27 #include "powermgmt.h"
28 #include "power.h"
29 #include "power-imx31.h"
31 /* TODO: Battery tests to get the right values! */
32 const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] =
34 3450
37 const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] =
39 3400
42 /* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
43 const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
45 /* Toshiba Gigabeat Li Ion 830mAH figured from discharge curve */
46 { 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990 },
49 /* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
50 const unsigned short percent_to_volt_charge[11] =
52 /* Toshiba Gigabeat Li Ion 830mAH */
53 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990
56 /* Returns battery voltage from ADC [millivolts] */
57 unsigned int battery_adc_voltage(void)
59 /* ADC reading 0-1023 = 2400mV-4700mV */
60 return ((adc_read(ADC_BATTERY) * 2303) >> 10) + 2400;
63 /* Returns the application supply voltage from ADC [millvolts] */
64 unsigned int application_supply_adc_voltage(void)
66 return ((adc_read(ADC_APPLICATION_SUPPLY) * 2303) >> 10) + 2400;
69 unsigned int chrgraw_adc_voltage(void)
71 return (adc_read(ADC_CHARGER_VOLTAGE) * 23023) >> 10;
74 /* Returns battery charge current from ADC [milliamps] */
75 int battery_adc_charge_current(void)
77 /* Positive reading = charger to battery
78 * Negative reading = battery to charger terminal
79 * ADC reading -512-511 = -2875mA-2875mA */
80 unsigned int value = adc_read(ADC_CHARGER_CURRENT);
81 int I;
83 if (value == ADC_READ_ERROR)
84 return INT_MIN;
86 I = ((((int32_t)value << 22) >> 22) * 2881) >> 9;
87 return ILEVEL_ADJUST_IN(I);
90 /* Estimate power dissipation in the charge path regulator in mW. */
91 unsigned int cccv_regulator_dissipation(void)
93 /* BATTISNS is shorted to BATT so we don't need to use the
94 * battery current reading. */
95 int chrgraw = (adc_read(ADC_CHARGER_VOLTAGE) * 230225) >> 10;
96 int batt = ((adc_read(ADC_BATTERY) * 23023) >> 10) + 24000;
97 int ichrgsn = adc_read(ADC_CHARGER_CURRENT);
98 ichrgsn = ((((int32_t)ichrgsn << 22) >> 22) * 2881) >> 9;
99 ichrgsn = abs(ichrgsn);
101 return (chrgraw - ichrgsn - batt)*ILEVEL_ADJUST_IN(ichrgsn) / 10000;
104 /* Returns battery temperature from ADC [deg-C] */
105 int battery_adc_temp(void)
107 /* E[volts] = value * 2.3V / 1023
108 * R[ohms] = E/I = E[volts] / 0.00002[A] (Thermistor bias current source)
110 * Steinhart-Hart thermistor equation:
111 * [A + B*ln(R) + D*ln^3(R)] = 1 / T[°K]
113 * Coeffients that fit experimental data (one thermistor so far, one run):
114 * A = 0.0013002631685462800
115 * B = 0.0002000841932612330
116 * D = 0.0000000640446750919
118 static const unsigned short ntc_table[] =
120 #if 0 /* These have degree deltas > 1 (except the final two) so leave them
121 * out. 70 deg C upper limit is quite sufficient. */
122 0, /* INF */ 1, /* 171 */ 2, /* 145 */ 3, /* 130 */
123 4, /* 121 */ 5, /* 114 */ 6, /* 108 */ 7, /* 104 */
124 8, /* 100 */ 9, /* 96 */ 10, /* 93 */ 11, /* 91 */
125 12, /* 88 */ 13, /* 86 */ 14, /* 84 */ 15, /* 82 */
126 16, /* 81 */ 17, /* 79 */ 18, /* 78 */ 19, /* 76 */
127 20, /* 75 */ 21, /* 74 */ 22, /* 72 */ 23, /* 71 */
128 #endif
129 24, /* 70 */ 25, /* 69 */ 26, /* 68 */ 27, /* 67 */
130 28, /* 66 */ 30, /* 65 */ 31, /* 64 */ 32, /* 63 */
131 33, /* 62 */ 35, /* 61 */ 36, /* 60 */ 38, /* 59 */
132 39, /* 58 */ 41, /* 57 */ 43, /* 56 */ 45, /* 55 */
133 47, /* 54 */ 49, /* 53 */ 51, /* 52 */ 53, /* 51 */
134 56, /* 50 */ 58, /* 49 */ 61, /* 48 */ 63, /* 47 */
135 66, /* 46 */ 69, /* 45 */ 73, /* 44 */ 76, /* 43 */
136 80, /* 42 */ 83, /* 41 */ 87, /* 40 */ 92, /* 39 */
137 96, /* 38 */ 101, /* 37 */ 106, /* 36 */ 111, /* 35 */
138 116, /* 34 */ 122, /* 33 */ 128, /* 32 */ 135, /* 31 */
139 142, /* 30 */ 149, /* 29 */ 156, /* 28 */ 164, /* 27 */
140 173, /* 26 */ 182, /* 25 */ 192, /* 24 */ 202, /* 23 */
141 212, /* 22 */ 224, /* 21 */ 236, /* 20 */ 249, /* 19 */
142 262, /* 18 */ 277, /* 17 */ 292, /* 16 */ 308, /* 15 */
143 325, /* 14 */ 344, /* 13 */ 363, /* 12 */ 384, /* 11 */
144 406, /* 10 */ 429, /* 9 */ 454, /* 8 */ 480, /* 7 */
145 509, /* 6 */ 539, /* 5 */ 571, /* 4 */ 605, /* 3 */
146 641, /* 2 */ 680, /* 1 */ 722, /* 0 */ 766, /* -1 */
147 813, /* -2 */ 864, /* -3 */ 918, /* -4 */ 976, /* -5 */
150 unsigned int value = adc_read(ADC_BATTERY_TEMP);
152 if (value > 0)
154 unsigned i;
156 for (i = 1; i < ARRAYLEN(ntc_table); i++)
158 if (ntc_table[i] > value)
159 break;
162 return 71 - i;
165 return INT_MIN;
168 /** Charger control **/
170 /* All code has a preference for the main charger being connected over
171 * USB. USB is considered in the algorithm only if it is the sole source. */
172 static uint32_t int_sense0 = 0; /* Interrupt Sense 0 bits */
173 static unsigned int last_inputs = POWER_INPUT_NONE; /* Detect input changes */
174 static int charger_total_timer = 0; /* Total allowed charging time */
175 static int icharger_ave = 0; /* Filtered charging current */
176 static bool charger_close = false; /* Shutdown notification */
177 static bool service_wdt = true; /* Service the watchdog timer, if things
178 go very wrong, cease and shut down. */
179 static uint32_t charger_setting = 0; /* Current ICHRG and VCHRG regulator
180 * setting (register bits) */
181 #define CHARGER_ADJUST ((uint32_t)-1)/* Force change in regulator setting */
182 static int autorecharge_counter = 0 ; /* Battery < threshold debounce */
183 static int chgcurr_timer = 0; /* Countdown to CHGCURR error */
184 #define AUTORECHARGE_COUNTDOWN (10*2) /* 10s debounce */
185 #define WATCHDOG_TIMEOUT (10*2) /* If not serviced, poweroff in 10s */
186 #define CHGCURR_TIMEOUT (4*2) /* 4s debounce */
188 /* Temperature monitoring */
189 static enum
191 TEMP_STATE_NORMAL = 0, /* Within range */
192 TEMP_STATE_WAIT = 1, /* Went out of range, wait to come back */
193 TEMP_LOW_LIMIT = 0, /* Min temp */
194 TEMP_HIGH_LIMIT = 1, /* Max temp */
195 } temp_state = TEMP_STATE_NORMAL;
197 /* Set power thread priority for charging mode or not */
198 static inline void charging_set_thread_priority(bool charging)
200 #ifdef HAVE_PRIORITY_SCHEDULING
201 thread_set_priority(THREAD_ID_CURRENT,
202 charging ? PRIORITY_REALTIME : PRIORITY_SYSTEM);
203 #endif
204 (void)charging;
207 /* Update filtered charger current - exponential moving average */
208 static bool charger_current_filter_step(void)
210 int value = battery_adc_charge_current();
212 if (value == ADC_READ_ERROR)
213 return false;
215 icharger_ave += value - (icharger_ave / ICHARGER_AVE_SAMPLES);
216 return true;
219 /* Return true if the main charger is connected. */
220 static bool main_charger_connected(void)
222 return (last_inputs &
223 POWER_INPUT_MAIN_CHARGER &
224 POWER_INPUT_CHARGER) != 0;
227 /* Return the voltage level which should automatically trigger
228 * another recharge cycle based upon which power source is available.
229 * Assumes at least one is. */
230 static unsigned int auto_recharge_voltage(void)
232 if (main_charger_connected())
233 return BATT_VAUTO_RECHARGE;
234 else
235 return BATT_USB_VAUTO_RECHARGE;
238 /* Return greater of supply (BP) or filtered battery voltage. */
239 unsigned int input_millivolts(void)
241 unsigned int app_millivolts = application_supply_adc_voltage();
242 unsigned int bat_millivolts = battery_voltage();
244 return MAX(app_millivolts, bat_millivolts);
247 /* Get smoothed readings for initializing filtered data. */
248 static int stat_battery_reading(int type)
250 int high = INT_MIN, low = INT_MAX;
251 int value = 0;
252 int i;
254 for (i = 0; i < 7; i++)
256 int reading = ADC_READ_ERROR;
258 sleep(2); /* Get unique readings */
260 switch (type)
262 case ADC_BATTERY:
263 reading = battery_adc_voltage();
264 break;
266 case ADC_CHARGER_CURRENT:
267 reading = battery_adc_charge_current();
268 break;
271 if (reading == ADC_READ_ERROR)
272 return INT_MIN;
274 if (reading > high)
275 high = reading;
277 if (reading < low)
278 low = reading;
280 value += reading;
283 /* Discard extremes */
284 return (value - high - low) / 5;
287 /* Update filtered battery voltage instead of waiting for filter
288 * decay. */
289 static bool update_filtered_battery_voltage(void)
291 int millivolts = stat_battery_reading(ADC_BATTERY);
293 if (millivolts != INT_MIN)
295 reset_battery_filter(millivolts);
296 return true;
299 return false;
302 /* Sets the charge current limit based upon state. charge_state should be
303 * set before calling. */
304 static bool adjust_charger_current(void)
306 static const uint8_t charger_bits[][2] =
308 [DISCHARGING] =
310 /* These are actually zeros but reflect this setting */
311 MC13783_ICHRG_0MA | MC13783_VCHRG_4_050V,
312 MC13783_ICHRG_0MA | MC13783_VCHRG_4_050V,
314 /* Main(+USB): Charge slowly from the adapter until voltage is
315 * sufficient for normal charging.
317 * USB: The truth is that things will probably not make it this far.
318 * Cover the case, just in case the disk isn't used and it is
319 * manageable. */
320 [TRICKLE] =
322 BATTERY_ITRICKLE | BATTERY_VCHARGING,
323 BATTERY_ITRICKLE_USB | BATTERY_VCHARGING
325 [TOPOFF] =
327 BATTERY_IFAST | BATTERY_VCHARGING,
328 BATTERY_IFAST_USB | BATTERY_VCHARGING
330 [CHARGING] =
332 BATTERY_IFAST | BATTERY_VCHARGING,
333 BATTERY_IFAST_USB | BATTERY_VCHARGING
335 /* Must maintain battery when on USB power only - utterly nasty
336 * but true and something retailos does (it will even end up charging
337 * the battery but not reporting that it is doing so).
338 * Float lower than MAX - could end up slightly discharging after
339 * a full charge but this is safer than maxing it out. */
340 [CHARGING+1] =
342 BATTERY_IFLOAT_USB | BATTERY_VFLOAT_USB,
343 BATTERY_IMAINTAIN_USB | BATTERY_VMAINTAIN_USB
345 #if 0
346 /* Slower settings to so that the linear regulator doesn't dissipate
347 * an excessive amount of power when coming out of precharge state. */
348 [CHARGING+2] =
350 BATTERY_ISLOW | BATTERY_VCHARGING,
351 BATTERY_ISLOW_USB | BATTEYR_VCHARGING
353 #endif
356 bool success = false;
357 int usb_select;
358 uint32_t i;
360 usb_select = ((last_inputs & POWER_INPUT) == POWER_INPUT_USB)
361 ? 1 : 0;
363 if (charge_state == DISCHARGING && usb_select == 1)
365 /* USB-only, DISCHARGING, = maintaining battery */
366 int select = (last_inputs & POWER_INPUT_CHARGER) ? 0 : 1;
367 charger_setting = charger_bits[CHARGING+1][select];
369 else
371 /* Take very good care not to write garbage. */
372 int state = charge_state;
374 if (state < DISCHARGING || state > CHARGING)
375 state = DISCHARGING;
377 charger_setting = charger_bits[state][usb_select];
380 if (charger_setting != 0)
382 charging_set_thread_priority(true);
384 /* Turn regulator logically ON. Hardware may still override. */
385 i = mc13783_write_masked(MC13783_CHARGER,
386 charger_setting | MC13783_CHRGRAWPDEN,
387 MC13783_ICHRG | MC13783_VCHRG |
388 MC13783_CHRGRAWPDEN);
390 if (i != MC13783_DATA_ERROR)
392 int icharger;
394 /* Enable charge current conversion */
395 adc_enable_channel(ADC_CHARGER_CURRENT, true);
397 /* Charge path regulator turn on takes ~100ms max. */
398 sleep(HZ/10);
400 icharger = stat_battery_reading(ADC_CHARGER_CURRENT);
402 if (icharger != INT_MIN)
404 icharger_ave = icharger * ICHARGER_AVE_SAMPLES;
406 if (update_filtered_battery_voltage())
407 return true;
411 /* Force regulator OFF. */
412 charge_state = CHARGE_STATE_ERROR;
415 /* Turn regulator OFF. */
416 icharger_ave = 0;
417 i = mc13783_write_masked(MC13783_CHARGER, charger_bits[0][0],
418 MC13783_ICHRG | MC13783_VCHRG |
419 MC13783_CHRGRAWPDEN);
421 if (MC13783_DATA_ERROR == i)
423 /* Failed. Force poweroff by not servicing the watchdog. */
424 service_wdt = false;
426 else if (0 == charger_setting)
428 /* Here because OFF was requested state */
429 success = true;
432 charger_setting = 0;
434 adc_enable_channel(ADC_CHARGER_CURRENT, false);
435 update_filtered_battery_voltage();
436 charging_set_thread_priority(false);
438 return success;
441 /* Stop the charger - if USB only then the regulator will not really be
442 * turned off. ERROR or DISABLED will turn it off however. */
443 static void stop_charger(void)
445 charger_total_timer = 0;
447 if (charge_state > DISCHARGING)
448 charge_state = DISCHARGING;
450 adjust_charger_current();
453 /* Return OK if it is acceptable to start the regulator. */
454 static bool charging_ok(void)
456 bool ok = charge_state >= DISCHARGING; /* Not an error condition? */
458 if (ok)
460 /* Is the battery even connected? */
461 ok = (last_inputs & POWER_INPUT_BATTERY) != 0;
464 if (ok)
466 /* No tolerance for any over/under temp - wait for it to
467 * come back into safe range. */
468 static const signed char temp_ranges[2][2] =
470 /* Temperature range before beginning charging */
471 { BATTERY_CHARGE_MIN,
472 BATTERY_CHARGE_MAX },
473 /* Temperature range after out-of-range detected -
474 charging will self-resume */
475 { BATTERY_CHARGE_RESTART_MIN,
476 BATTERY_CHARGE_RESTART_MAX },
479 int temp = battery_adc_temp();
480 const signed char *range = temp_ranges[temp_state];
482 ok = temp >= range[TEMP_LOW_LIMIT] &&
483 temp <= range[TEMP_HIGH_LIMIT];
485 switch (temp_state)
487 case TEMP_STATE_NORMAL:
488 if (!ok)
489 temp_state = TEMP_STATE_WAIT;
490 break;
492 case TEMP_STATE_WAIT:
493 if (ok)
494 temp_state = TEMP_STATE_NORMAL;
495 break;
497 default:
498 break;
502 if (ok)
504 /* Any events that should stop the regulator? */
506 /* Overvoltage at CHRGRAW? */
507 ok = (int_sense0 & MC13783_CHGOVS) == 0;
509 if (ok)
511 /* CHGCURR sensed? */
512 ok = (int_sense0 & MC13783_CHGCURRS) != 0;
514 if (!ok)
516 /* Debounce transient states */
517 if (chgcurr_timer > 0)
519 chgcurr_timer--;
520 ok = true;
523 else
525 chgcurr_timer = CHGCURR_TIMEOUT;
529 /* Charger may need to be reinserted */
530 if (!ok)
531 charge_state = CHARGE_STATE_ERROR;
534 if (charger_setting != 0)
536 if (ok)
538 /* Watch to not overheat FET (nothing should go over about 1012.7mW).
539 * Trying a higher voltage AC adapter can work (up to 6.90V) but
540 * we'll just reject that. Reducing current for adapters that bring
541 * CHRGRAW to > 4.900V is another possible action. */
542 ok = cccv_regulator_dissipation() < 1150;
543 if (!ok)
544 charge_state = CHARGE_STATE_ERROR;
547 if (!ok)
549 int state = charge_state;
551 if (state > DISCHARGING)
552 state = DISCHARGING;
554 /* Force off for all states including maintaining the battery level
555 * on USB. */
556 charge_state = CHARGE_STATE_ERROR;
557 stop_charger();
558 charge_state = state;
562 return ok;
565 void powermgmt_init_target(void)
567 #ifdef IMX31_ALLOW_CHARGING
568 const uint32_t regval_w =
569 MC13783_VCHRG_4_050V | MC13783_ICHRG_0MA |
570 MC13783_ICHRGTR_0MA | MC13783_OVCTRL_6_90V;
572 /* Use watchdog to shut system down if we lose control of the charging
573 * hardware. */
574 watchdog_init(WATCHDOG_TIMEOUT);
576 mc13783_write(MC13783_CHARGER, regval_w);
578 if (mc13783_read(MC13783_CHARGER) == regval_w)
580 /* Divide CHRGRAW input by 10 */
581 mc13783_clear(MC13783_ADC0, MC13783_CHRGRAWDIV);
582 /* Turn off BATTDETB. It's worthless on MESx0V since the battery
583 * isn't removable (nor the thermistor). */
584 mc13783_clear(MC13783_POWER_CONTROL0, MC13783_BATTDETEN);
586 else
588 /* Register has the wrong value - set error condition and disable
589 * since something is wrong. */
590 charge_state = CHARGE_STATE_DISABLED;
591 stop_charger();
593 #else
594 /* Disable charger use */
595 charge_state = CHARGE_STATE_DISABLED;
596 #endif
599 /* Returns true if the unit is charging the batteries. */
600 bool charging_state(void)
602 switch (charge_state)
604 case TRICKLE:
605 case TOPOFF:
606 case CHARGING:
607 return true;
608 default:
609 return false;
613 /* Filtered battery charge current */
614 int battery_charge_current(void)
616 return icharger_ave / ICHARGER_AVE_SAMPLES;
619 static void charger_plugged(void)
621 adc_enable_channel(ADC_BATTERY_TEMP, true);
622 autorecharge_counter = -1;
625 static void charger_unplugged(void)
627 /* Charger pulled - turn off current sources (though hardware
628 * will have done that anyway). */
629 if (charge_state > CHARGE_STATE_DISABLED)
631 /* Reset state and clear any error. If disabled, the charger
632 * will not have been started or will have been stopped already. */
633 stop_charger();
634 charge_state = DISCHARGING;
637 /* Might need to reevaluate these bits in charger_none. */
638 last_inputs &= ~(POWER_INPUT | POWER_INPUT_CHARGER);
639 temp_state = TEMP_STATE_NORMAL;
640 autorecharge_counter = 0;
641 chgcurr_timer = 0;
643 adc_enable_channel(ADC_BATTERY_TEMP, false);
646 static void charger_none(void)
648 unsigned int pwr = power_thread_inputs;
650 if (last_inputs != pwr)
652 last_inputs = pwr;
654 if (charge_state == CHARGE_STATE_DISABLED)
655 return;
657 if ((pwr & (POWER_INPUT | POWER_INPUT_CHARGER)) == POWER_INPUT_USB)
659 /* USB connected but not configured. Maintain battery to the
660 * greatest degree possible. It probably won't be enough but the
661 * discharge won't be so severe. */
662 charger_plugged();
663 charger_setting = CHARGER_ADJUST;
665 else
667 charger_unplugged();
668 last_inputs = pwr; /* Restore status */
671 else if (charger_setting != 0)
673 /* Maintaining - keep filter going and check charge state */
674 int_sense0 = mc13783_read(MC13783_INTERRUPT_SENSE0);
676 if (!charger_current_filter_step())
678 /* Failed to read current */
679 charge_state = CHARGE_STATE_ERROR;
682 charging_ok();
686 static void charger_control(void)
688 unsigned int pwr = power_thread_inputs;
690 if (last_inputs != pwr)
692 unsigned int changed = last_inputs ^ pwr;
694 last_inputs = pwr;
696 if (charger_setting != 0)
697 charger_setting = CHARGER_ADJUST;
699 if (charge_state == DISCHARGING)
701 if (main_charger_connected())
703 /* If main is connected, ignore USB plugs. */
704 if (changed & POWER_INPUT_MAIN_CHARGER)
706 /* Main charger plugged - try charge */
707 autorecharge_counter = -1;
710 else if (pwr & POWER_INPUT_USB_CHARGER
711 & POWER_INPUT_CHARGER)
713 /* USB power only */
714 if (changed & POWER_INPUT_USB_CHARGER)
716 /* USB charger plugged - try charge */
717 autorecharge_counter = -1;
719 else if (changed & POWER_INPUT_MAIN_CHARGER)
721 /* Main charger pulled - go to battery maintenence. */
722 charger_setting = CHARGER_ADJUST;
728 if (charger_setting != 0 && !charger_current_filter_step())
730 /* Failed to read current */
731 charge_state = CHARGE_STATE_ERROR;
734 int_sense0 = mc13783_read(MC13783_INTERRUPT_SENSE0);
736 if (!charging_ok())
737 return;
739 switch (charge_state)
741 case DISCHARGING:
743 /* Battery voltage may have dropped and a charge cycle should
744 * start again. Debounced. */
745 if (autorecharge_counter < 0 &&
746 battery_adc_voltage() < BATT_FULL_VOLTAGE)
748 /* Try starting a cycle now if battery isn't already topped
749 * off to allow user to ensure the battery is full. */
751 else if (battery_voltage() > auto_recharge_voltage())
753 /* Still above threshold - reset counter */
754 autorecharge_counter = AUTORECHARGE_COUNTDOWN;
755 break;
757 else if (autorecharge_counter > 0)
759 /* Coundown to restart */
760 autorecharge_counter--;
761 break;
764 autorecharge_counter = 0;
766 charging_set_thread_priority(true);
768 if (stat_battery_reading(ADC_BATTERY) < BATT_VTRICKLE_CHARGE)
770 /* Battery is deeply discharged - precharge at lower current. */
771 charge_state = TRICKLE;
773 else
775 /* Ok for fast charge */
776 charge_state = CHARGING;
779 charger_setting = CHARGER_ADJUST;
780 charger_total_timer = CHARGER_TOTAL_TIMER*60*2;
781 break;
782 } /* DISCHARGING: */
784 case TRICKLE: /* Very low - precharge */
786 if (battery_voltage() <= BATT_VTRICKLE_CHARGE)
787 break;
789 /* Switch to normal charge mode. */
790 charge_state = CHARGING;
791 charger_setting = CHARGER_ADJUST;
792 break;
793 } /* TRICKLE: */
795 case CHARGING: /* Constant-current stage */
796 case TOPOFF: /* Constant-voltage stage */
798 /* Reg. mode is more informative than an operational necessity. */
799 charge_state = (int_sense0 & MC13783_CCCVS) ? TOPOFF : CHARGING;
801 if (main_charger_connected())
803 /* Monitor and stop if current drops below threshold. */
804 if (battery_charge_current() > BATTERY_ICHARGE_COMPLETE)
805 break;
807 else
809 /* Accurate I-level can't be determined since device also
810 * powers through the I sense. This simply stops the reporting
811 * of charging but the regulator remains on. */
812 if (battery_voltage() <= BATT_USB_VSTOP)
813 break;
816 stop_charger();
817 break;
818 } /* CHARGING: TOPOFF: */
820 default:
821 break;
822 } /* switch */
824 /* Check if charger timer expired and stop it if so. */
825 if (charger_total_timer > 0 && --charger_total_timer == 0)
827 charge_state = CHARGE_STATE_ERROR;
828 stop_charger(); /* Time ran out - error */
832 /* Main charging algorithm - called from powermgmt.c */
833 void charging_algorithm_step(void)
835 #ifdef IMX31_ALLOW_CHARGING
836 if (service_wdt)
837 watchdog_service();
838 #endif
840 /* Switch by input state */
841 switch (charger_input_state)
843 case NO_CHARGER:
844 charger_none();
845 break;
847 case CHARGER_PLUGGED:
848 charger_plugged();
849 break;
851 case CHARGER:
852 charger_control();
853 break;
855 case CHARGER_UNPLUGGED:
856 charger_unplugged();
857 break;
858 } /* switch */
860 if (charger_close)
862 if (charge_state != CHARGE_STATE_DISABLED)
864 /* Disable starts while shutting down */
865 charge_state = CHARGE_STATE_DISABLED;
866 stop_charger();
869 charger_close = false;
870 return;
873 if (charger_setting != 0)
875 if ((mc13783_read(MC13783_CHARGER) & (MC13783_ICHRG | MC13783_VCHRG)) !=
876 charger_setting)
878 /* The hardware setting doesn't match. It could have turned the
879 * charger off in a race of plugging/unplugging or the setting
880 * was changed in one of the calls. */
881 adjust_charger_current();
886 /* Disable the charger and prepare for poweroff - called off-thread so we
887 * signal the charging thread to prepare to quit. */
888 void charging_algorithm_close(void)
890 charger_close = true;
892 /* Power management thread will set it false again */
893 while (charger_close)
894 sleep(HZ/10);