summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/battery/compal_e88.c
blob: 12cdff9d22830d83d2d6ff9432ec7f2f490eae3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Battery management for compal_e88 */

/* (C) 2010 by Christian Vogel <vogelchr@vogel.cx>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/*
 * ___C123 (compal e88) very simplified diagram of charging circuitry___
 *
 *                           ICTL
 *                            |
 *                            v
 *  Charger -> OVP -------> P-Mosfet --->[0.15 Ohm]---> Battery
 *             (NCP345,  |             |            |
 *              6.85V)   v             v            v
 *                      VCHG          VCCS        VBAT &
 *                                                VBATS
 *
 * Inputs to IOTA:
 *  VCHG:        senses voltage on the charger input
 *  VCSS/VBATS:  to difference amplifier, to measure charge current
 *  VBAT:        senses voltage on the battery terminal
 * Outputs from IOTA:
 *  ICTL:        Control signal for the switching mosfet
 *
 */

#include <battery/battery.h>
#include <battery/compal_e88.h>

#include <stdint.h>
#include <abb/twl3025.h>
#include <comm/timer.h>
#include <stdio.h>

/* ADC calibration, scale is LSB/uV or LSB/uA, physical unit is mV or mA */
#define ADC_TO_PHYSICAL(adc,scale) (((adc)*(scale)+500)/1000)
#define PHYSICAL_TO_ADC(phy,scale) (((phy)*1000+(scale)/2)/(scale))

/* conversion factors for internal IOTA battery charging/sensing circuitry */
#define VREF_LSB_uV 1709  /* VREF = 1.75V    --> 1.709 mV/LSB */
#define VBAT_LSB_uV 6836  /* VBAT = 7.0 V FS --> 6.836 mV/LSB */
#define VCHG_LSB_uV 8545  /* VCHG = 8.75V FS --> 8.545 mV/LSB */
#define ICHG_LSB_uA 854   /* ICHG = 875mA FS --> 0.854 mA/LSB */

/* charger is considered plugged in/removed when over/under... */
#define VCHG_thr_on  PHYSICAL_TO_ADC(4500,VCHG_LSB_uV)
#define VCHG_thr_off PHYSICAL_TO_ADC(3200,VCHG_LSB_uV)

/* battery is considered full/empty at these thresholds... */
#define VBAT_full   PHYSICAL_TO_ADC(4100,VBAT_LSB_uV)
#define VBAT_empty  PHYSICAL_TO_ADC(3200,VBAT_LSB_uV)

/* we declare overvoltage at this point... */
#define VBAT_fail   PHYSICAL_TO_ADC(4250,VBAT_LSB_uV)

/* charging current in ADC LSBs */
#define ICHG_set    PHYSICAL_TO_ADC(200,ICHG_LSB_uA)
#define VCHG_set    VBAT_full

/* global battery info */
struct osmocom_battery_info osmocom_battery_info;

/* internal bookkeeping */
static uint16_t compal_e88_madc[8];	/* remembering last ADC values */

enum battery_compal_e88_status {
	ADC_CONVERSION = 1 << 0
};
static uint32_t battery_compal_e88_status;

static const int BATTERY_TIMER_DELAY=5000; /* 5000ms for control loop */
static const int ADC_TIMER_DELAY=100;      /*  100ms for ADC conversion */

/* thermistor sense current, turn it up to eleven! */
#define  TH_SENS	       (THSENS0|THSENS1|THSENS2|THEN)
#define  BATTERY_ALL_SENSE     (TH_SENS|MESBAT|TYPEN)

/*
 * charger modes state machine
 *
 *    +------------------+-------------------+
 *    |                  |                   | lost AC power
 *    |                  ^                   ^
 *    V  on AC power     |    @VBAT_full     |
 * +-----+        +------------+ -----> +------------+
 * | OFF | -----> | CONST_CURR |        | CONST_VOLT |
 * +-----+        +------------+        +------------+
 *    ^          ^        |                   |
 *    |         /failure  v                   v failure
 * +---------+ /  gone    |                   | condition
 * | FAILURE | <----------+-------------------+
 * +---------+
 *
 *  Failure modes currently detected:
 *          + high battery voltage
 *          + high battery temperature
 */
enum charger_state {
	CHARG_OFF,
	CHARG_CONST_CURR,
	CHARG_CONST_VOLT,
	CHARG_FAIL
};
static enum charger_state charger_state;

static void
charger_goto_state(enum charger_state newstate){
	charger_state=newstate;
}

static void
battery_charger_control(){
	/* with AC power disconnected, always go to off state */
	if(!osmocom_battery_info.flags & BATTERY_CHG_CONNECTED){
		charger_goto_state(CHARG_OFF);
		return;
	}
#if 0
	/* if failure condition is detected, always goto failure state */
	if(){
		charger_goto_state(CHARG_FAIL);
	}

	switch(charger_state){
	case CHARG_OFF:
	case CHARG_CONST_CURR:
	case CHARG_CONST_VOLT:
	case CHARG_FAIL:
	default:
#endif		
}

/*
 * Charging voltage connection - state machine:
 *
 *                     VCHG > VCHG_thr_on
 * +-----------------+ ------------------> +---------------+
 * | ! CHG_CONNECTED |                     | CHG_CONNECTED |
 * +-----------------+ <------------------ +---------------+
 *                     VCHG < VCHG_thr_off
 *
 */
static void
check_charg_volt_presence(){
	/* check for presence of charging voltage */
	if(!(osmocom_battery_info.flags & BATTERY_CHG_CONNECTED)){
		if(compal_e88_madc[MADC_VCHG] > VCHG_thr_on){
			printf("CHARGER: external voltage connected!\n");
			osmocom_battery_info.flags |= BATTERY_CHG_CONNECTED;

			/* always keep ADC, voltage dividers and bias voltages on */
			twl3025_unit_enable(TWL3025_UNIT_MAD,1);
			twl3025_reg_write(BCICTL1,BATTERY_ALL_SENSE);
		}
	} else {
		if(compal_e88_madc[MADC_VCHG] < VCHG_thr_off){
			/* we'll only run ADC on demand */
			twl3025_unit_enable(TWL3025_UNIT_MAD,0);
			twl3025_reg_write(BCICTL1,0);

			osmocom_battery_info.flags &= ~ BATTERY_CHG_CONNECTED;
			printf("CHARGER: external voltage disconnected!\n");
		}
	}
}

/* ---- update voltages visible to the user ---- */
static void
battery_update_measurements(){
	int adc,i;

	osmocom_battery_info.charger_volt_mV=
		ADC_TO_PHYSICAL(compal_e88_madc[MADC_VCHG],VCHG_LSB_uV);
	osmocom_battery_info.bat_volt_mV=
		ADC_TO_PHYSICAL(compal_e88_madc[MADC_VBAT],VBAT_LSB_uV);
	osmocom_battery_info.bat_chg_curr_mA=
		ADC_TO_PHYSICAL(compal_e88_madc[MADC_ICHG],ICHG_LSB_uA);

	adc = compal_e88_madc[MADC_VBAT];
	if(adc <= VBAT_empty){
		osmocom_battery_info.battery_percent = 0;
	} else if (adc >= VBAT_full){
		osmocom_battery_info.battery_percent = 100;
	} else {
		osmocom_battery_info.battery_percent =
			(50+100*(adc-VBAT_empty))/(VBAT_full-VBAT_empty);
	}

        /* DEBUG */
        printf("BAT-ADC: ");
        for(i=0;i<MADC_NUM_CHANNELS;i++)
                printf("%3d ",compal_e88_madc[i]);
        printf("%c\n\n",32);
        printf("\tCharger at %u mV.\n",osmocom_battery_info.charger_volt_mV);
        printf("\tBattery at %u mV.\n",osmocom_battery_info.bat_volt_mV);
        printf("\tCharging at %u mA.\n",osmocom_battery_info.bat_chg_curr_mA);
        printf("\tBattery capacity is %u%%.\n",osmocom_battery_info.battery_percent);
	printf("\tBattery range is %d..%d mV.\n",
		ADC_TO_PHYSICAL(VBAT_empty,VBAT_LSB_uV),
		ADC_TO_PHYSICAL(VBAT_full,VBAT_LSB_uV));
        printf("\tBattery full at %d LSB .. full at %d LSB\n",VBAT_empty,VBAT_full);
        printf("\tCharging at %d LSB (%d mA).\n",ICHG_set,
                ADC_TO_PHYSICAL(ICHG_set,ICHG_LSB_uA));
        i = twl3025_reg_read(BCICTL2);
        printf("\tBattery charger thresholds in ADC LSBs: on %d and off %d\n",
                        VCHG_thr_on,VCHG_thr_off);
        printf("\tBCICTL2=0x%03x\n",i);      
	printf("\tosmocom-battery-info.flags=0x%08x\n",osmocom_battery_info.flags);
}

/* battery_adc_read() starts a conversion on all ADC channels
   if battery_compal_e88_status & ADC_CONVERSION is not set and
   tries to read back values (and reset ADC_CONVERSION) if it currently
   is set. If it returns zero, conversion data is available, if it
   returns non-zero a conversion has been triggered and data should
   be available "soon". */

static int
battery_adc_read(){
	int i;

	if(battery_compal_e88_status & ADC_CONVERSION){
		i = twl3025_reg_read(MADCSTAT);
		if(i & ADCBUSY)
			return 1;
		for(i=0;i<MADC_NUM_CHANNELS;i++)
			compal_e88_madc[i]=twl3025_reg_read(VBATREG+i);
		/* if charger is connected, we keep the ADC and BIAS on
		   continuously, if running on battery, we try to save power */
		if(!osmocom_battery_info.flags & BATTERY_CHG_CONNECTED){
			twl3025_reg_write(BCICTL1,0x00); /* turn off bias */
			twl3025_unit_enable(TWL3025_UNIT_MAD,0); /* & ADC */
		}
		battery_compal_e88_status &= ~ ADC_CONVERSION;
		return 0;
	} else {
		/* if running on battery, turn on ADC & BIAS on demand */
		if(!osmocom_battery_info.flags & BATTERY_CHG_CONNECTED){
			twl3025_unit_enable(TWL3025_UNIT_MAD,1);
			twl3025_reg_write(BCICTL1,BATTERY_ALL_SENSE);
		}
		twl3025_reg_write(MADCTRL,0xff); /* convert all channels */
		twl3025_reg_write(VBATREG,0);    /* trigger conversion */

		battery_compal_e88_status |= ADC_CONVERSION;
		return 1;
	}
}

static void
battery_compal_e88_timer_cb(void *p){
	struct osmo_timer_list *tmr = (struct osmo_timer_list*)p;
	int i;

	if(battery_adc_read()){ /* read back ADCs after a brief delay */
		osmo_timer_schedule(tmr,ADC_TIMER_DELAY);
		return;
	}

	battery_update_measurements();

	check_charg_volt_presence();
	battery_charger_control();

	osmo_timer_schedule(tmr,BATTERY_TIMER_DELAY);
}

/* timer that fires the charging loop regularly */
static struct osmo_timer_list battery_compal88_timer = {
	.cb = &battery_compal_e88_timer_cb,
	.data = &battery_compal88_timer
};

void
battery_compal_e88_init(){
	printf("%s: starting up\n",__FUNCTION__);
	osmo_timer_schedule(&battery_compal88_timer,BATTERY_TIMER_DELAY);
}