summaryrefslogtreecommitdiff
path: root/driver/charger/bd99955.c
blob: 0880668dd0d6ac1168d631f4dd1c7bb9c9853b40 (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
/* Copyright 2016 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.
 *
 * ROHM BD99955 battery charger driver.
 */

#include "battery.h"
#include "battery_smart.h"
#include "bd99955.h"
#include "charger.h"
#include "console.h"
#include "i2c.h"
#include "task.h"
#include "util.h"

/* Console output macros */
#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)

/* Charger parameters */
static const struct charger_info bd99955_charger_info = {
	.name         = CHARGER_NAME,
	.voltage_max  = CHARGE_V_MAX,
	.voltage_min  = CHARGE_V_MIN,
	.voltage_step = CHARGE_V_STEP,
	.current_max  = CHARGE_I_MAX,
	.current_min  = CHARGE_I_MIN,
	.current_step = CHARGE_I_STEP,
	.input_current_max  = INPUT_I_MAX,
	.input_current_min  = INPUT_I_MIN,
	.input_current_step = INPUT_I_STEP,
};

/* Charge command code map */
static enum bd99955_command charger_map_cmd = BD99955_INVALID_COMMAND;

static struct mutex bd99955_map_mutex;

static inline int ch_raw_read16(int cmd, int *param,
				enum bd99955_command map_cmd)
{
	int rv;

	/* Map the Charge command code to appropriate region */
	mutex_lock(&bd99955_map_mutex);
	if (charger_map_cmd != map_cmd) {
		rv = i2c_write16(I2C_PORT_CHARGER, I2C_ADDR_CHARGER,
				 BD99955_CMD_MAP_SET, map_cmd);
		if (rv)
			goto bd99955_read_cleanup;

		charger_map_cmd = map_cmd;
	}

	rv = i2c_read16(I2C_PORT_CHARGER, I2C_ADDR_CHARGER, cmd, param);

bd99955_read_cleanup:
	mutex_unlock(&bd99955_map_mutex);

	return rv;
}

static inline int ch_raw_write16(int cmd, int param,
					enum bd99955_command map_cmd)
{
	int rv;

	/* Map the Charge command code to appropriate region */
	mutex_lock(&bd99955_map_mutex);
	if (charger_map_cmd != map_cmd) {
		rv = i2c_write16(I2C_PORT_CHARGER, I2C_ADDR_CHARGER,
					BD99955_CMD_MAP_SET, map_cmd);
		if (rv)
			goto bd99955_write_cleanup;

		charger_map_cmd = map_cmd;
	}

	rv = i2c_write16(I2C_PORT_CHARGER, I2C_ADDR_CHARGER, cmd, param);

bd99955_write_cleanup:
	mutex_unlock(&bd99955_map_mutex);

	return rv;
}

/* chip specific interfaces */

int charger_set_input_current(int input_current)
{
	int rv;

	/* Input current step 32 mA */
	input_current &= ~0x1F;

	rv = ch_raw_write16(BD99955_CMD_IBUS_LIM_SET, input_current,
				BD99955_BAT_CHG_COMMAND);
	if (rv)
		return rv;

	return ch_raw_write16(BD99955_CMD_ICC_LIM_SET, input_current,
				BD99955_BAT_CHG_COMMAND);
}

int charger_get_input_current(int *input_current)
{
	return ch_raw_read16(BD99955_CMD_CUR_ILIM_VAL, input_current,
			     BD99955_EXTENDED_COMMAND);
}

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

int charger_device_id(int *id)
{
	return ch_raw_read16(BD99955_CMD_CHIP_ID, id, BD99955_EXTENDED_COMMAND);
}

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

int charger_set_option(int option)
{
	return EC_ERROR_UNIMPLEMENTED;
}

/* Charger interfaces */

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

int charger_get_status(int *status)
{
	*status = CHARGER_LEVEL_2;

	return EC_SUCCESS;
}

int charger_set_mode(int mode)
{
	/* BD99955 does not support inhibit mode setting. */
	return EC_SUCCESS;
}

int charger_get_current(int *current)
{
	return ch_raw_read16(BD99955_CMD_CHG_CURRENT, current,
				BD99955_BAT_CHG_COMMAND);
}

int charger_set_current(int current)
{
	/* Charge current step 64 mA */
	current &= ~0x3F;

	return ch_raw_write16(BD99955_CMD_CHG_CURRENT, current,
				BD99955_BAT_CHG_COMMAND);
}

int charger_get_voltage(int *voltage)
{
	return ch_raw_read16(BD99955_CMD_CHG_VOLTAGE, voltage,
				BD99955_BAT_CHG_COMMAND);
}

int charger_set_voltage(int voltage)
{
	/*
	 * The BD99955 will drop voltage to as low as requested. As the
	 * charger state machine will pass in 0 voltage, protect the system
	 * voltage by capping to the minimum. The reason is that the BD99955
	 * only can regulate the system voltage which will kill the board's
	 * power if below 0.
	 */
	if (voltage == 0) {
		const struct battery_info *bi = battery_get_info();

		voltage = bi->voltage_min;
	}

	/* Charge voltage step 16 mV */
	voltage &= ~0x0F;
	return ch_raw_write16(BD99955_CMD_CHG_VOLTAGE, voltage,
				BD99955_BAT_CHG_COMMAND);
}

int charger_post_init(void)
{
	int rv;

	/*
	 * TODO: Disable charger & re-enable to initialize it.
	 */
	rv = charger_discharge_on_ac(1);
	if (rv)
		return rv;

	return charger_discharge_on_ac(0);
}

int charger_discharge_on_ac(int enable)
{
	int rv;
	int reg;

	rv = ch_raw_read16(BD99955_CMD_CHGOP_SET2, &reg,
				BD99955_EXTENDED_COMMAND);
	if (rv)
		return rv;

	if (enable) {
		reg |= BD99955_CHGOP_SET2_BATT_LEARN;
		reg &= ~BD99955_CHGOP_SET2_CHG_EN;
	} else {
		reg &= ~BD99955_CHGOP_SET2_BATT_LEARN;
		reg |= BD99955_CHGOP_SET2_CHG_EN;
	}

	return ch_raw_write16(BD99955_CMD_CHGOP_SET2, reg,
				BD99955_EXTENDED_COMMAND);
}

/*** Non-standard interface functions ***/

int bd99955_extpower_is_present(void)
{
	int reg;

	if (ch_raw_read16(BD99955_CMD_VBUS_VCC_STATUS, &reg,
			  BD99955_EXTENDED_COMMAND))
		return 0;

	reg &= (BD99955_CMD_VBUS_VCC_STATUS_VCC_DETECT |
		BD99955_CMD_VBUS_VCC_STATUS_VBUS_DETECT);
	return !!reg;
}

int bd99955_select_input_port(enum bd99955_charge_port port)
{
	int rv;
	int reg;

	rv = ch_raw_read16(BD99955_CMD_VIN_CTRL_SET, &reg,
			   BD99955_EXTENDED_COMMAND);
	if (rv)
		return rv;

	if (port == BD99955_CHARGE_PORT_NONE) {
		reg &= ~(BD99955_CMD_VIN_CTRL_SET_VBUS_EN |
			 BD99955_CMD_VIN_CTRL_SET_VBUS_EN);
	} else if (port == BD99955_CHARGE_PORT_VBUS) {
		reg |= BD99955_CMD_VIN_CTRL_SET_VBUS_EN;
		reg &= ~BD99955_CMD_VIN_CTRL_SET_VCC_EN;
	} else if (port == BD99955_CHARGE_PORT_VCC) {
		reg |= BD99955_CMD_VIN_CTRL_SET_VCC_EN;
		reg &= ~BD99955_CMD_VIN_CTRL_SET_VBUS_EN;
	} else {
		/* Invalid charge port */
		panic("Invalid charge port");
	}

	return ch_raw_write16(BD99955_CMD_VIN_CTRL_SET, reg,
			      BD99955_EXTENDED_COMMAND);
}

#ifdef CONFIG_CMD_CHARGER
static int read_bat(uint8_t cmd)
{
	int read = 0;

	ch_raw_read16(cmd, &read, BD99955_BAT_CHG_COMMAND);
	return read;
}

static int read_ext(uint8_t cmd)
{
	int read = 0;

	ch_raw_read16(cmd, &read, BD99955_EXTENDED_COMMAND);
	return read;
}

/* Dump all readable registers on bd99955 */
static int console_bd99955_dump_regs(int argc, char **argv)
{
	int i;
	uint8_t regs[] = { 0x14, 0x15, 0x3c, 0x3d, 0x3e, 0x3f };

	/* Battery group registers */
	for (i = 0; i < ARRAY_SIZE(regs); ++i)
		ccprintf("BAT REG %4x:  %4x\n", regs[i], read_bat(regs[i]));

	/* Extended group registers */
	for (i = 0; i < 0x7f; ++i)
		ccprintf("EXT REG %4x:  %4x\n", i, read_ext(i));

	return 0;
}
DECLARE_CONSOLE_COMMAND(bd99955_dump, console_bd99955_dump_regs,
			NULL,
			"Dump all charger registers",
			NULL);

static int console_command_bd99955(int argc, char **argv)
{
	int rv, reg, data, val;
	char rw, *e;
	enum bd99955_command cmd;

	rw = argv[1][0];
	if (rw == 'r') {
		if (argc < 4)
			return EC_ERROR_PARAM_COUNT;
	} else if (rw == 'w') {
		if (argc < 5)
			return EC_ERROR_PARAM_COUNT;
	} else
		return EC_ERROR_PARAM_COUNT;

	reg = strtoi(argv[2], &e, 16);
	if (*e || reg < 0)
		return EC_ERROR_PARAM2;

	cmd = strtoi(argv[3], &e, 0);
	if (*e || cmd < 0)
		return EC_ERROR_INVAL;

	if (argc == 5) {
		val = strtoi(argv[4], &e, 16);
		if (*e || val < 0)
			return EC_ERROR_INVAL;
	}

	if (rw == 'r')
		rv = ch_raw_read16(reg, &data, cmd);
	else {
		rv = ch_raw_write16(reg, val, cmd);
		if (rv == EC_SUCCESS)
			rv = ch_raw_read16(reg, &data, cmd);
	}

	CPRINTS("register 0x%x [%d] = 0x%x [%d]", reg, reg, data, data);

	return rv;
}
DECLARE_CONSOLE_COMMAND(bd99955, console_command_bd99955,
			"bd99955 <r/w> <reg_hex> <cmd_type> | <val_hex>",
			"Read or write a charger register",
			NULL);
#endif /* CONFIG_CMD_CHARGER */