summaryrefslogtreecommitdiff
path: root/driver/charger/bq2589x.c
blob: 3e9e4bfd36b960df818ada62aa44734878fdf87d (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
/* Copyright 2015 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 bq25890/bq25892/bq25895 battery charger driver.
 */

#include "config.h"
#include "bq2589x.h"
#include "charger.h"
#include "common.h"
#include "console.h"
#include "hooks.h"
#include "i2c.h"
#include "printf.h"
#include "util.h"

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

/* Charger information */
static const struct charger_info bq2589x_charger_info = {
	.name         = "bq2589x",
	.voltage_max  = 4608,
	.voltage_min  = 3840,
	.voltage_step = 16,
	.current_max  = 5056,
	.current_min  = 0,
	.current_step = 64,
	.input_current_max  = 3250,
	.input_current_min  = 100,
	.input_current_step = 50,
};

static int bq2589x_read(int reg, int *value)
{
	return i2c_read8(I2C_PORT_CHARGER, BQ2589X_ADDR, reg, value);
}

static int bq2589x_write(int reg, int value)
{
	return i2c_write8(I2C_PORT_CHARGER, BQ2589X_ADDR, reg, value);
}

static int bq2589x_watchdog_reset(void)
{
	int rv, val;

	rv = bq2589x_read(BQ2589X_REG_CFG2, &val);
	if (rv)
		return rv;
	val |= (1 << 6);
	return bq2589x_write(BQ2589X_REG_CFG2, val);
}

static int bq2589x_set_terminate_current(int current)
{
	int reg_val, rv;
	int val = (current - 64) / 64;

	rv = bq2589x_read(BQ2589X_REG_PRE_CHG_CURR, &reg_val);
	if (rv)
		return rv;
	reg_val = (reg_val & ~0xf) | (val & 0xf);
	return bq2589x_write(BQ2589X_REG_PRE_CHG_CURR, reg_val);
}

int charger_enable_otg_power(int enabled)
{
	int val, rv;

	rv = bq2589x_read(BQ2589X_REG_CFG2, &val);
	if (rv)
		return rv;
	val = (val & ~0x30) | (enabled ? 0x20 : 0x10);
	return bq2589x_write(BQ2589X_REG_CFG2, val);
}

int charger_set_input_current(int input_current)
{
	int value, rv;
	const struct charger_info * const info = charger_get_info();

	input_current -= info->input_current_min;
	if (input_current < 0)
		input_current = 0;

	rv = bq2589x_read(BQ2589X_REG_INPUT_CURR, &value);
	if (rv)
		return rv;
	value = value & ~(0x3f);
	value |= (input_current / info->input_current_step) & 0x3f;
	return bq2589x_write(BQ2589X_REG_INPUT_CURR, value);
}

int charger_get_input_current(int *input_current)
{
	int rv, value;
	const struct charger_info * const info = charger_get_info();

	rv = bq2589x_read(BQ2589X_REG_INPUT_CURR, &value);
	if (rv)
		return rv;

	*input_current = (value & 0x3f) * info->input_current_step
			+ info->input_current_min;

	return EC_SUCCESS;
}

int charger_manufacturer_id(int *id)
{
	return EC_ERROR_UNIMPLEMENTED;
}

int charger_device_id(int *id)
{
	int res = bq2589x_read(BQ2589X_REG_ID, id);

	if (res == EC_SUCCESS)
		*id &= BQ2589X_DEVICE_ID_MASK;

	return res;
}

int charger_get_option(int *option)
{
	return EC_ERROR_UNIMPLEMENTED;
}

int charger_set_option(int option)
{
	return EC_ERROR_UNIMPLEMENTED;
}

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

int charger_get_status(int *status)
{
	return EC_ERROR_UNIMPLEMENTED;
}

int charger_set_mode(int mode)
{
	return EC_ERROR_UNIMPLEMENTED;
}

int charger_get_current(int *current)
{
	int rv, val;
	const struct charger_info * const info = charger_get_info();

	rv = bq2589x_read(BQ2589X_REG_CHG_CURR, &val);
	if (rv)
		return rv;
	*current = val * info->current_step + info->current_min;
	return EC_SUCCESS;
}

int charger_set_current(int current)
{
	const struct charger_info * const info = charger_get_info();

	current = charger_closest_current(current);

	return bq2589x_write(BQ2589X_REG_CHG_CURR,
			     current / info->current_step);
}

int charger_get_voltage(int *voltage)
{
	int rv, val;
	const struct charger_info * const info = charger_get_info();

	rv = bq2589x_read(BQ2589X_REG_CHG_VOLT, &val);
	if (rv)
		return rv;
	val = (val >> 2) & 0x3f;
	*voltage = val * info->voltage_step + info->voltage_min;
	return EC_SUCCESS;
}

int charger_set_voltage(int voltage)
{
	int rv, val;
	const struct charger_info * const info = charger_get_info();

	voltage = charger_closest_current(voltage);

	rv = bq2589x_read(BQ2589X_REG_CHG_VOLT, &val);
	if (rv)
		return rv;
	val = val & 0x3;
	val |= ((voltage - info->voltage_min) / info->voltage_step) << 2;
	return bq2589x_write(BQ2589X_REG_CHG_VOLT, val);
}

int charger_discharge_on_ac(int enable)
{
	return EC_SUCCESS;
}

/* Charging power state initialization */
int charger_post_init(void)
{
#ifdef CONFIG_CHARGER_ILIM_PIN_DISABLED
	int val, rv;
	rv = bq2589x_read(BQ2589X_REG_INPUT_CURR, &val);
	if (rv)
		return rv;
	val &= ~0x40;
	rv = bq2589x_write(BQ2589X_REG_INPUT_CURR, val);
	if (rv)
		return rv;
#endif /* CONFIG_CHARGER_ILIM_PIN_DISABLED */

	/* Input current controlled by extpower module. Do nothing here. */
	return EC_SUCCESS;
}


/*****************************************************************************/
/* Hooks */

static void bq2589x_init(void)
{
	int val;

	if (charger_device_id(&val) || val != BQ2589X_DEVICE_ID) {
		CPRINTF("BQ2589X incorrent ID: 0x%02x\n", val);
		return;
	}

	/*
	 * Disable I2C watchdog timer.
	 *
	 * TODO(crosbug.com/p/38603): Re-enable watchdog timer and kick it
	 * periodically in charger task.
	 */
	if (bq2589x_read(BQ2589X_REG_TIMER, &val))
		return;
	val &= ~0x30;
	if (bq2589x_write(BQ2589X_REG_TIMER, val))
		return;

	if (bq2589x_set_terminate_current(64))
		return;

	if (bq2589x_watchdog_reset())
		return;

	CPRINTF("BQ2589%c initialized\n",
		BQ2589X_DEVICE_ID == BQ25890_DEVICE_ID ? '0' :
		(BQ2589X_DEVICE_ID == BQ25895_DEVICE_ID ? '5' : '2'));
}
DECLARE_HOOK(HOOK_INIT, bq2589x_init, HOOK_PRIO_LAST);

/*****************************************************************************/
/* Console commands */

static int command_bq2589x(int argc, char **argv)
{
	int i;
	int value;
	int rv;
	int batt_mv, sys_mv, vbus_mv, chg_ma, input_ma;

	/* Trigger one ADC conversion */
	bq2589x_read(BQ2589X_REG_CFG1, &value);
	bq2589x_write(BQ2589X_REG_CFG1, value | 0x80);
	do {
		bq2589x_read(BQ2589X_REG_CFG1, &value);
	} while (value & 0x80); /* Wait for End of Conversion */

	bq2589x_read(BQ2589X_REG_ADC_BATT_VOLT, &batt_mv);
	bq2589x_read(BQ2589X_REG_ADC_SYS_VOLT, &sys_mv);
	bq2589x_read(BQ2589X_REG_ADC_VBUS_VOLT, &vbus_mv);
	bq2589x_read(BQ2589X_REG_ADC_CHG_CURR, &chg_ma);
	bq2589x_read(BQ2589X_REG_ADC_INPUT_CURR, &input_ma);
	ccprintf("ADC Batt %dmV Sys %dmV VBUS %dmV Chg %dmA Input %dmA\n",
		2304 + (batt_mv & 0x7f) * 20, 2304 + sys_mv * 20,
		2600 + (vbus_mv & 0x7f) * 100,
		chg_ma * 50, 100 + (input_ma & 0x3f) * 50);

	ccprintf("REG:");
	for (i = 0; i <= 0x14; ++i)
		ccprintf(" %02x", i);
	ccprintf("\n");

	ccprintf("VAL:");
	for (i = 0; i <= 0x14; ++i) {
		rv = bq2589x_read(i, &value);
		if (rv)
			return rv;
		ccprintf(" %02x", value);
	}
	ccprintf("\n");

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(bq2589x, command_bq2589x,
			NULL, NULL, NULL);