Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2007 by Karl Kurbjun |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 16 | * |
| 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 Gordon | 4c06ad6 | 2007-10-22 15:28:40 +0000 | [diff] [blame] | 25 | #include "tsc2100.h" |
| 26 | #include "kernel.h" |
Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 27 | |
Jonathan Gordon | 4c06ad6 | 2007-10-22 15:28:40 +0000 | [diff] [blame] | 28 | unsigned short current_voltage = 3910; |
Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 29 | const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = |
| 30 | { |
Jonathan Gordon | eed41ff | 2007-10-23 14:33:16 +0000 | [diff] [blame] | 31 | 0 |
Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] = |
| 35 | { |
Jonathan Gordon | eed41ff | 2007-10-23 14:33:16 +0000 | [diff] [blame] | 36 | 0 |
Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */ |
| 40 | const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] = |
| 41 | { |
Jonathan Gordon | eed41ff | 2007-10-23 14:33:16 +0000 | [diff] [blame] | 42 | { 100, 300, 400, 500, 600, 700, 800, 900, 1000, 1200, 1320 }, |
Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */ |
| 46 | const unsigned short percent_to_volt_charge[11] = |
| 47 | { |
Jonathan Gordon | eed41ff | 2007-10-23 14:33:16 +0000 | [diff] [blame] | 48 | 100, 300, 400, 500, 600, 700, 800, 900, 1000, 1200, 1320, |
Karl Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 49 | }; |
Jonathan Gordon | 4c06ad6 | 2007-10-22 15:28:40 +0000 | [diff] [blame] | 50 | void 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 Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 67 | /* Returns battery voltage from ADC [millivolts] */ |
| 68 | unsigned int battery_adc_voltage(void) |
| 69 | { |
Jonathan Gordon | 4c06ad6 | 2007-10-22 15:28:40 +0000 | [diff] [blame] | 70 | 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 Kurbjun | a56b653 | 2007-09-30 16:29:21 +0000 | [diff] [blame] | 83 | } |
| 84 | |