blob: f3746c8c982a1b60cad1ee84250557efb0201003 [file] [log] [blame]
Karl Kurbjuna56b6532007-09-30 16:29:21 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 by Karl Kurbjun
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Karl Kurbjuna56b6532007-09-30 16:29:21 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "config.h"
23#include "adc.h"
24#include "powermgmt.h"
Jonathan Gordon4c06ad62007-10-22 15:28:40 +000025#include "tsc2100.h"
26#include "kernel.h"
Karl Kurbjuna56b6532007-09-30 16:29:21 +000027
Jonathan Gordon4c06ad62007-10-22 15:28:40 +000028unsigned short current_voltage = 3910;
Karl Kurbjuna56b6532007-09-30 16:29:21 +000029const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] =
30{
Jonathan Gordoneed41ff2007-10-23 14:33:16 +000031 0
Karl Kurbjuna56b6532007-09-30 16:29:21 +000032};
33
34const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] =
35{
Jonathan Gordoneed41ff2007-10-23 14:33:16 +000036 0
Karl Kurbjuna56b6532007-09-30 16:29:21 +000037};
38
39/* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
40const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
41{
Jonathan Gordoneed41ff2007-10-23 14:33:16 +000042 { 100, 300, 400, 500, 600, 700, 800, 900, 1000, 1200, 1320 },
Karl Kurbjuna56b6532007-09-30 16:29:21 +000043};
44
45/* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
46const unsigned short percent_to_volt_charge[11] =
47{
Jonathan Gordoneed41ff2007-10-23 14:33:16 +000048 100, 300, 400, 500, 600, 700, 800, 900, 1000, 1200, 1320,
Karl Kurbjuna56b6532007-09-30 16:29:21 +000049};
Jonathan Gordon4c06ad62007-10-22 15:28:40 +000050void read_battery_inputs(void)
51{
52 short tsadc = tsc2100_readreg(TSADC_PAGE, TSADC_ADDRESS);
53 short adscm = (tsadc&TSADC_ADSCM_MASK)>>TSADC_ADSCM_SHIFT;
54 if (adscm == 0xb) /* battery is available */
55 {
56 current_voltage = tsc2100_readreg(0, 5); /* BAT1 */
57 tsc2100_readreg(0, 6); /* BAT2 */
58 tsc2100_readreg(0, 7); /* AUX */
59 /* reset the TSC2100 to read touches */
60 tsadc &= ~(TSADC_PSTCM|TSADC_ADST|TSADC_ADSCM_MASK);
61 tsadc |= TSADC_PSTCM|(0x2<<TSADC_ADSCM_SHIFT);
62 tsc2100_writereg(TSADC_PAGE, TSADC_ADDRESS, tsadc);
63 tsc2100_writereg(TSSTAT_PAGE, TSSTAT_ADDRESS, 2<<TSSTAT_PINTDAV_SHIFT);
64 }
65}
66
Karl Kurbjuna56b6532007-09-30 16:29:21 +000067/* Returns battery voltage from ADC [millivolts] */
68unsigned int battery_adc_voltage(void)
69{
Jonathan Gordon4c06ad62007-10-22 15:28:40 +000070 static unsigned last_tick = 0;
71 short tsadc = tsc2100_readreg(TSADC_PAGE, TSADC_ADDRESS);
72 if (TIME_BEFORE(last_tick+2*HZ, current_tick))
73 {
74 tsadc &= ~(TSADC_PSTCM|TSADC_ADST|TSADC_ADSCM_MASK);
75 tsadc |= 0xb<<TSADC_ADSCM_SHIFT;
76 tsc2100_writereg(TSADC_PAGE, TSADC_ADDRESS, tsadc&(~(1<<15)));
77 tsc2100_writereg(TSSTAT_PAGE, TSSTAT_ADDRESS, 2<<TSSTAT_PINTDAV_SHIFT);
78 last_tick = current_tick;
79 }
80 else
81 read_battery_inputs();
82 return current_voltage;
Karl Kurbjuna56b6532007-09-30 16:29:21 +000083}
84