summaryrefslogtreecommitdiff
path: root/driver/charger/bq25710.c
blob: 4d4826b3c8c3ab9d80b95b9f566bef5acde70b27 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * TI bq25710 battery charger driver.
 */

#include "battery_smart.h"
#include "bq25710.h"
#include "charger.h"
#include "common.h"
#include "console.h"
#include "i2c.h"
#include "timer.h"

/* Sense resistor configurations and macros */
#define DEFAULT_SENSE_RESISTOR 10

#define INPUT_RESISTOR_RATIO \
	((CONFIG_CHARGER_SENSE_RESISTOR_AC) / DEFAULT_SENSE_RESISTOR)
#define REG_TO_INPUT_CURRENT(REG) ((REG + 1) * 50 / INPUT_RESISTOR_RATIO)
#define INPUT_CURRENT_TO_REG(CUR) (((CUR) * INPUT_RESISTOR_RATIO / 50) - 1)

#define CHARGING_RESISTOR_RATIO \
	((CONFIG_CHARGER_SENSE_RESISTOR) / DEFAULT_SENSE_RESISTOR)
#define REG_TO_CHARGING_CURRENT(REG) ((REG) / CHARGING_RESISTOR_RATIO)
#define CHARGING_CURRENT_TO_REG(CUR) ((CUR) * CHARGING_RESISTOR_RATIO)

/* Console output macros */
#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)

/* Charger parameters */
static const struct charger_info bq25710_charger_info = {
	.name         = "bq25710",
	.voltage_max  = 19200,
	.voltage_min  = 1024,
	.voltage_step = 16,
	.current_max  = 8128 / CHARGING_RESISTOR_RATIO,
	.current_min  = 64 / CHARGING_RESISTOR_RATIO,
	.current_step = 64 / CHARGING_RESISTOR_RATIO,
	.input_current_max  = 6400 / INPUT_RESISTOR_RATIO,
	.input_current_min  = 50 / INPUT_RESISTOR_RATIO,
	.input_current_step = 50 / INPUT_RESISTOR_RATIO,
};

static inline int raw_read16(int offset, int *value)
{
	return i2c_read16(I2C_PORT_CHARGER, BQ25710_SMBUS_ADDR1_FLAGS,
			  offset, value);
}

static inline int raw_write16(int offset, int value)
{
	return i2c_write16(I2C_PORT_CHARGER, BQ25710_SMBUS_ADDR1_FLAGS,
			   offset, value);
}

#ifdef CONFIG_CHARGE_RAMP_HW
static int bq25710_get_low_power_mode(int *mode)
{
	int rv;
	int reg;

	rv = raw_read16(BQ25710_REG_CHARGE_OPTION_0, &reg);
	if (rv)
		return rv;

	*mode = !!(reg & BQ25710_CHARGE_OPTION_0_LOW_POWER_MODE);

	return EC_SUCCESS;
}

static int bq25710_set_low_power_mode(int enable)
{
	int rv;
	int reg;

	rv = raw_read16(BQ25710_REG_CHARGE_OPTION_0, &reg);
	if (rv)
		return rv;

	if (enable)
		reg |= BQ25710_CHARGE_OPTION_0_LOW_POWER_MODE;
	else
		reg &= ~BQ25710_CHARGE_OPTION_0_LOW_POWER_MODE;

	rv = raw_write16(BQ25710_REG_CHARGE_OPTION_0, reg);
	if (rv)
		return rv;

	return EC_SUCCESS;
}
#endif

/* Charger interfaces */

const struct charger_info *charger_get_info(void)
{
	return &bq25710_charger_info;
}

int charger_post_init(void)
{
	/*
	 * Note: bq25710 power on reset state is:
	 *	watch dog timer     = 175 sec
	 *	input current limit = ~1/2 maximum setting
	 *	charging voltage    = 0 mV
	 *	charging current    = 0 mA
	 *	discharge on AC     = disabled
	 */

	/* Set charger input current limit */
	return charger_set_input_current(CONFIG_CHARGER_INPUT_CURRENT);
}

int charger_get_status(int *status)
{
	int rv;
	int option;

	rv = charger_get_option(&option);
	if (rv)
		return rv;

	/* Default status */
	*status = CHARGER_LEVEL_2;

	if (option & BQ25710_CHARGE_OPTION_0_CHRG_INHIBIT)
		*status |= CHARGER_CHARGE_INHIBITED;

	return EC_SUCCESS;
}

int charger_set_mode(int mode)
{
	int rv;
	int option;

	rv = charger_get_option(&option);
	if (rv)
		return rv;

	if (mode & CHARGER_CHARGE_INHIBITED)
		option |= BQ25710_CHARGE_OPTION_0_CHRG_INHIBIT;
	else
		option &= ~BQ25710_CHARGE_OPTION_0_CHRG_INHIBIT;

	return charger_set_option(option);
}

int charger_enable_otg_power(int enabled)
{
	/* This is controlled with the EN_OTG pin. Support not added yet. */
	return EC_ERROR_UNIMPLEMENTED;
}

int charger_set_otg_current_voltage(int output_current, int output_voltage)
{
	/* Add when needed. */
	return EC_ERROR_UNIMPLEMENTED;
}

int charger_is_sourcing_otg_power(int port)
{
	/* Add when needed. */
	return EC_ERROR_UNIMPLEMENTED;
}

int charger_get_current(int *current)
{
	int rv, reg;

	rv = raw_read16(BQ25710_REG_CHARGE_CURRENT, &reg);
	if (!rv)
		*current = REG_TO_CHARGING_CURRENT(reg);

	return rv;
}

int charger_set_current(int current)
{
	return raw_write16(BQ25710_REG_CHARGE_CURRENT,
		CHARGING_CURRENT_TO_REG(current));
}

/* Get/set charge voltage limit in mV */
int charger_get_voltage(int *voltage)
{
	return raw_read16(BQ25710_REG_MAX_CHARGE_VOLTAGE, voltage);
}

int charger_set_voltage(int voltage)
{
	return raw_write16(BQ25710_REG_MAX_CHARGE_VOLTAGE, voltage);
}

/* Discharge battery when on AC power. */
int charger_discharge_on_ac(int enable)
{
	int rv, option;

	rv = charger_get_option(&option);
	if (rv)
		return rv;

	if (enable)
		option |= BQ25710_CHARGE_OPTION_0_EN_LEARN;
	else
		option &= ~BQ25710_CHARGE_OPTION_0_EN_LEARN;

	return charger_set_option(option);
}

int charger_set_input_current(int input_current)
{
	int num_steps = INPUT_CURRENT_TO_REG(input_current);

	return raw_write16(BQ25710_REG_IIN_HOST, num_steps <<
			  BQ25710_CHARGE_IIN_BIT_0FFSET);
}

int charger_get_input_current(int *input_current)
{
	int rv, reg;

	/*
	 * IIN_DPM register reflects the actual input current limit programmed
	 * in the register, either from host or from ICO. After ICO, the
	 * current limit used by DPM regulation may differ from the IIN_HOST
	 * register settings.
	 */
	rv = raw_read16(BQ25710_REG_IIN_DPM, &reg);
	if (!rv)
		*input_current =
			REG_TO_INPUT_CURRENT((reg >>
					      BQ25710_CHARGE_IIN_BIT_0FFSET));

	return rv;
}

int charger_manufacturer_id(int *id)
{
	return raw_read16(BQ25710_REG_MANUFACTURER_ID, id);
}

int charger_device_id(int *id)
{
	return raw_read16(BQ25710_REG_DEVICE_ADDRESS, id);
}

int charger_get_option(int *option)
{
	/* There are 4 option registers, but we only need the first for now. */
	return raw_read16(BQ25710_REG_CHARGE_OPTION_0, option);
}

int charger_set_option(int option)
{
	/* There are 4 option registers, but we only need the first for now. */
	return raw_write16(BQ25710_REG_CHARGE_OPTION_0, option);
}

#ifdef CONFIG_CHARGE_RAMP_HW

static void bq25710_chg_ramp_handle(void)
{
	int ramp_curr;

	/*
	 * Once the charge ramp is stable write back the stable ramp
	 * current to input current register.
	 */
	if (chg_ramp_is_stable()) {
		ramp_curr = chg_ramp_get_current_limit();
		if (ramp_curr && !charger_set_input_current(ramp_curr))
			CPRINTF("stable ramp current=%d\n", ramp_curr);
	}
}
DECLARE_DEFERRED(bq25710_chg_ramp_handle);

int charger_set_hw_ramp(int enable)
{
	int option3_reg, option2_reg, rv;

	rv = raw_read16(BQ25710_REG_CHARGE_OPTION_3, &option3_reg);
	if (rv)
		return rv;
	rv = raw_read16(BQ25710_REG_CHARGE_OPTION_2, &option2_reg);
	if (rv)
		return rv;

	if (enable) {
		/* Set InputVoltage register to BC1.2 minimum ramp voltage */
		rv = raw_write16(BQ25710_REG_INPUT_VOLTAGE,
			BQ25710_BC12_MIN_VOLTAGE_MV);
		if (rv)
			return rv;

		/*  Enable ICO algorithm */
		option3_reg |= BQ25710_CHARGE_OPTION_3_EN_ICO_MODE;

		/* 0b: Input current limit is set by BQ25710_REG_IIN_HOST */
		option2_reg &= ~BQ25710_CHARGE_OPTION_2_EN_EXTILIM;

		/* Charge ramp may take up to 2s to settle down */
		hook_call_deferred(&bq25710_chg_ramp_handle_data, (4 * SECOND));
	} else {
		/*  Disable ICO algorithm */
		option3_reg &= ~BQ25710_CHARGE_OPTION_3_EN_ICO_MODE;

		/*
		 * 1b: Input current limit is set by the lower value of
		 * ILIM_HIZ pin and BQ25710_REG_IIN_HOST
		 */
		option2_reg |= BQ25710_CHARGE_OPTION_2_EN_EXTILIM;
	}

	rv = raw_write16(BQ25710_REG_CHARGE_OPTION_2, option2_reg);
	if (rv)
		return rv;
	return raw_write16(BQ25710_REG_CHARGE_OPTION_3, option3_reg);
}

int chg_ramp_is_stable(void)
{
	int reg;

	if (raw_read16(BQ25710_REG_CHARGER_STATUS, &reg))
		return 0;

	return reg & BQ25710_CHARGE_STATUS_ICO_DONE;
}

int chg_ramp_get_current_limit(void)
{
	int reg;
	int mode;
	int tries_left = 8;

	/* Save current mode to restore same state after ADC read */
	if (bq25710_get_low_power_mode(&mode))
		goto error;

	/* Exit low power mode so ADC conversion takes typical time */
	if (bq25710_set_low_power_mode(0))
		goto error;

	/* Turn on the ADC for one reading */
	reg = BQ25710_ADC_OPTION_ADC_START | BQ25710_ADC_OPTION_EN_ADC_IIN;
	if (raw_write16(BQ25710_REG_ADC_OPTION, reg))
		goto error;

	/*
	 * Wait until the ADC operation completes. The spec says typical
	 * conversion time is 10 msec. If low power mode isn't exited first,
	 * then the conversion time jumps to ~60 msec.
	 */
	do {
		msleep(2);
		raw_read16(BQ25710_REG_ADC_OPTION, &reg);
	} while (--tries_left && (reg & BQ25710_ADC_OPTION_ADC_START));

	/* ADC reading attempt complete, go back to low power mode */
	if (bq25710_set_low_power_mode(mode))
		goto error;

	/* Could not complete read */
	if (reg & BQ25710_ADC_OPTION_ADC_START)
		goto error;

	/* Read ADC value */
	if (raw_read16(BQ25710_REG_ADC_CMPIN_IIN, &reg))
		goto error;

	/* LSB => 50mA */
	return (reg >> BQ25710_ADC_IIN_STEP_BIT_OFFSET) *
		BQ25710_ADC_IIN_STEP_MA;

error:
	CPRINTF("Could not read input current limit ADC!\n");
	return 0;
}
#endif /* CONFIG_CHARGE_RAMP_HW */