summaryrefslogtreecommitdiff
path: root/common/smart_battery.c
blob: 609c3acd2e41a442e9ca474cfb1fb70c5743b47e (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
/* Copyright (c) 2012 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.
 *
 * Smart battery driver.
 */

#include "host_command.h"
#include "smart_battery.h"
#include "timer.h"

test_mockable int sbc_read(int cmd, int *param)
{
	return i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, cmd, param);
}

test_mockable int sbc_write(int cmd, int param)
{
	return i2c_write16(I2C_PORT_CHARGER, CHARGER_ADDR, cmd, param);
}

int sb_read(int cmd, int *param)
{
	return i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, cmd, param);
}

int sb_write(int cmd, int param)
{
	return i2c_write16(I2C_PORT_BATTERY, BATTERY_ADDR, cmd, param);
}

/* Get/set battery mode */
int battery_get_battery_mode(int *mode)
{
	return sb_read(SB_BATTERY_MODE, mode);
}

int battery_set_battery_mode(int mode)
{
	return sb_write(SB_BATTERY_MODE, mode);
}

int battery_is_in_10mw_mode(int *ret)
{
	int val;
	int rv = battery_get_battery_mode(&val);
	if (rv)
		return rv;
	*ret = val & MODE_CAPACITY;
	return EC_SUCCESS;
}

int battery_set_10mw_mode(int enabled)
{
	int val, rv;
	rv = battery_get_battery_mode(&val);
	if (rv)
		return rv;
	if (enabled)
		val |= MODE_CAPACITY;
	else
		val &= ~MODE_CAPACITY;
	return battery_set_battery_mode(val);
}

/* Read battery temperature
 * unit: 0.1 K
 */
int battery_temperature(int *deci_kelvin)
{
	return sb_read(SB_TEMPERATURE, deci_kelvin);
}

/* Read battery voltage
 * unit: mV
 */
int battery_voltage(int *voltage)
{
	return sb_read(SB_VOLTAGE, voltage);
}

/* Relative state of charge in percent */
int battery_state_of_charge(int *percent)
{
	return sb_read(SB_RELATIVE_STATE_OF_CHARGE, percent);
}

/* Absolute state of charge in percent */
int battery_state_of_charge_abs(int *percent)
{
	return sb_read(SB_ABSOLUTE_STATE_OF_CHARGE, percent);
}

/* Battery remaining capacity
 * unit: mAh or 10mW, depends on battery mode
 */
int battery_remaining_capacity(int *capacity)
{
	return sb_read(SB_REMAINING_CAPACITY, capacity);
}

/* Battery full charge capacity */
int battery_full_charge_capacity(int *capacity)
{
	return sb_read(SB_FULL_CHARGE_CAPACITY, capacity);
}

/* Time in minutes left when discharging */
int battery_time_to_empty(int *minutes)
{
	return sb_read(SB_AVERAGE_TIME_TO_EMPTY, minutes);
}

int battery_run_time_to_empty(int *minutes)
{
	return sb_read(SB_RUN_TIME_TO_EMPTY, minutes);
}

/* Time in minutes to full when charging */
int battery_time_to_full(int *minutes)
{
	return sb_read(SB_AVERAGE_TIME_TO_FULL, minutes);
}

/* The current battery desired to charge
 * unit: mA
 */
int battery_desired_current(int *current)
{
	return sb_read(SB_CHARGING_CURRENT, current);
}

/* The voltage battery desired to charge
 * unit: mV
 */
int battery_desired_voltage(int *voltage)
{
	return sb_read(SB_CHARGING_VOLTAGE, voltage);
}

/* Check if battery allows charging */
int battery_charging_allowed(int *allowed)
{
	int v, c, rv;

	rv = battery_desired_voltage(&v) | battery_desired_current(&c);
	if (rv)
		return rv;
	*allowed = (v != 0) && (c != 0);
	return EC_SUCCESS;
}

/* Read battery status */
int battery_status(int *status)
{
	return sb_read(SB_BATTERY_STATUS, status);
}

/* Battery charge cycle count */
int battery_cycle_count(int *count)
{
	return sb_read(SB_CYCLE_COUNT, count);
}

/* Designed battery capacity
 * unit: mAh or 10mW depends on battery mode
 */
int battery_design_capacity(int *capacity)
{
	return sb_read(SB_DESIGN_CAPACITY, capacity);
}

/* Designed battery output voltage
 * unit: mV
 */
int battery_design_voltage(int *voltage)
{
	return sb_read(SB_DESIGN_VOLTAGE, voltage);
}

/* Read serial number */
int battery_serial_number(int *serial)
{
	return sb_read(SB_SERIAL_NUMBER, serial);
}

/* Read battery discharging current
 * unit: mA
 * negative value: charging
 */
int battery_current(int *current)
{
	int rv, d;

	rv = sb_read(SB_CURRENT, &d);
	if (rv)
		return rv;

	*current = (int16_t)d;
	return EC_SUCCESS;
}


int battery_average_current(int *current)
{
	int rv, d;

	rv = sb_read(SB_AVERAGE_CURRENT, &d);
	if (rv)
		return rv;

	*current = (int16_t)d;
	return EC_SUCCESS;
}

/* Calculate battery time in minutes, under a charging rate
 * rate >  0: charging, negative time to full
 * rate <  0: discharging, positive time to empty
 * rate == 0: invalid input, time = 0
 */
test_mockable int battery_time_at_rate(int rate, int *minutes)
{
	int rv;
	int ok, time;
	int loop, cmd, output_sign;

	if (rate == 0) {
		*minutes = 0;
		return EC_ERROR_INVAL;
	}

	rv = sb_write(SB_AT_RATE, rate);
	if (rv)
		return rv;
	loop = 5;
	while (loop--) {
		rv = sb_read(SB_AT_RATE_OK, &ok);
		if (rv)
			return rv;
		if (ok) {
			if (rate > 0) {
				cmd = SB_AT_RATE_TIME_TO_FULL;
				output_sign = -1;
			} else {
				cmd = SB_AT_RATE_TIME_TO_EMPTY;
				output_sign = 1;
			}
			rv = sb_read(cmd, &time);
			if (rv)
				return rv;

			*minutes = (time == 0xffff) ? 0 : output_sign * time;
			return EC_SUCCESS;
		} else {
			/* wait 10ms for AT_RATE_OK */
			msleep(10);
		}
	}
	return EC_ERROR_TIMEOUT;
}

/* Read manufacturer date */
test_mockable int battery_manufacturer_date(int *year, int *month, int *day)
{
	int rv;
	int ymd;

	rv = sb_read(SB_SPECIFICATION_INFO, &ymd);
	if (rv)
		return rv;

	/* battery date format:
	 * ymd = day + month * 32 + (year - 1980) * 256
	 */
	*year  = (ymd >> 8) + 1980;
	*month = (ymd & 0xff) / 32;
	*day   = (ymd & 0xff) % 32;

	return EC_SUCCESS;
}

/* Read manufacturer name */
test_mockable int battery_manufacturer_name(char *name, int buf_size)
{
	return i2c_read_string(I2C_PORT_BATTERY, BATTERY_ADDR,
		SB_MANUFACTURER_NAME, name, buf_size);
}

/* Read device name */
test_mockable int battery_device_name(char *device_name, int buf_size)
{
	return i2c_read_string(I2C_PORT_BATTERY, BATTERY_ADDR,
		SB_DEVICE_NAME, device_name, buf_size);
}

/* Read battery type/chemistry */
test_mockable int battery_device_chemistry(char *device_chemistry, int buf_size)
{
	return i2c_read_string(I2C_PORT_BATTERY, BATTERY_ADDR,
		SB_DEVICE_CHEMISTRY, device_chemistry, buf_size);
}

/*****************************************************************************/
/* Smart battery pass-through
 */
#ifdef CONFIG_I2C_PASSTHROUGH
static int host_command_sb_read_word(struct host_cmd_handler_args *args)
{
	int rv;
	int val;
	const struct ec_params_sb_rd *p = args->params;
	struct ec_response_sb_rd_word *r = args->response;

	if (p->reg > 0x1c)
		return EC_RES_INVALID_PARAM;
	rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, p->reg, &val);
	if (rv)
		return EC_RES_ERROR;

	r->value = val;
	args->response_size = sizeof(struct ec_response_sb_rd_word);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_SB_READ_WORD,
		     host_command_sb_read_word,
		     EC_VER_MASK(0));

static int host_command_sb_write_word(struct host_cmd_handler_args *args)
{
	int rv;
	const struct ec_params_sb_wr_word *p = args->params;

	if (p->reg > 0x1c)
		return EC_RES_INVALID_PARAM;
	rv = i2c_write16(I2C_PORT_BATTERY, BATTERY_ADDR, p->reg, p->value);
	if (rv)
		return EC_RES_ERROR;

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_SB_WRITE_WORD,
		     host_command_sb_write_word,
		     EC_VER_MASK(0));

static int host_command_sb_read_block(struct host_cmd_handler_args *args)
{
	int rv;
	const struct ec_params_sb_rd *p = args->params;
	struct ec_response_sb_rd_block *r = args->response;

	if ((p->reg != SB_MANUFACTURER_NAME) &&
	    (p->reg != SB_DEVICE_NAME) &&
	    (p->reg != SB_DEVICE_CHEMISTRY) &&
	    (p->reg != SB_MANUFACTURER_DATA))
		return EC_RES_INVALID_PARAM;
	rv = i2c_read_string(I2C_PORT_BATTERY, BATTERY_ADDR, p->reg,
			     r->data, 32);
	if (rv)
		return EC_RES_ERROR;

	args->response_size = sizeof(struct ec_response_sb_rd_block);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_SB_READ_BLOCK,
		     host_command_sb_read_block,
		     EC_VER_MASK(0));

static int host_command_sb_write_block(struct host_cmd_handler_args *args)
{
	/* Not implemented */
	return EC_RES_INVALID_COMMAND;
}
DECLARE_HOST_COMMAND(EC_CMD_SB_WRITE_BLOCK,
		     host_command_sb_write_block,
		     EC_VER_MASK(0));
#endif