summaryrefslogtreecommitdiff
path: root/driver/charger/rt9490.c
blob: 85dac52bdc7352edf5f19a4954eb3b6ff6c0b855 (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* Copyright 2022 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.
 *
 * Richtek 5A 1-4 cell buck-boost switching battery charger driver.
 */

#include "battery.h"
#include "battery_smart.h"
#include "builtin/endian.h"
#include "charger.h"
#include "charge_manager.h"
#include "common.h"
#include "config.h"
#include "console.h"
#include "hooks.h"
#include "i2c.h"
#include "rt9490.h"
#include "task.h"
#include "usb_charge.h"
#include "usb_pd.h"
#include "util.h"

/* Console output macros */
#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)
#define CPRINTS(format, args...) \
	cprints(CC_CHARGER, "%s " format, "RT9490", ## args)

/* Charger parameters */
#define CHARGER_NAME    "rt9490"
#define CHARGE_V_MAX    18800
#define CHARGE_V_MIN    3000
#define CHARGE_V_STEP   10
#define CHARGE_I_MAX    5000
#define CHARGE_I_MIN    50
#define CHARGE_I_STEP   10
#define INPUT_I_MAX     3300
#define INPUT_I_MIN     100
#define INPUT_I_STEP    10

/* Charger parameters */
static const struct charger_info rt9490_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,
};

static const struct rt9490_init_setting default_init_setting = {
	.eoc_current = 200,
	.mivr = 4000,
	.boost_voltage = 5050,
	.boost_current = 1500,
};

static enum ec_error_list rt9490_read8(int chgnum, int reg, int *val)
{
	return i2c_read8(chg_chips[chgnum].i2c_port,
			chg_chips[chgnum].i2c_addr_flags, reg, val);
}

static enum ec_error_list rt9490_write8(int chgnum, int reg, int val)
{
	return i2c_write8(chg_chips[chgnum].i2c_port,
			chg_chips[chgnum].i2c_addr_flags, reg, val);
}

static enum ec_error_list rt9490_read16(int chgnum, int reg, uint16_t *val)
{
	int reg_val;

	RETURN_ERROR(i2c_read16(chg_chips[chgnum].i2c_port,
			chg_chips[chgnum].i2c_addr_flags, reg, &reg_val));

	*val = be16toh(reg_val);

	return EC_SUCCESS;
}

static enum ec_error_list rt9490_write16(int chgnum, int reg, uint16_t val)
{
	int reg_val = htobe16(val);

	return i2c_write16(chg_chips[chgnum].i2c_port,
			chg_chips[chgnum].i2c_addr_flags, reg, reg_val);
}

static int rt9490_field_update8(int chgnum, int reg, int mask, int val)
{
	return i2c_field_update8(chg_chips[chgnum].i2c_port,
				 chg_chips[chgnum].i2c_addr_flags,
				 reg, mask, val);
}

static inline int rt9490_update8(int chgnum, int reg, int mask,
				 enum mask_update_action action)
{
	return i2c_update8(chg_chips[chgnum].i2c_port,
			   chg_chips[chgnum].i2c_addr_flags,
			   reg, mask, action);
}

static inline int rt9490_set_bit(int chgnum, int reg, int mask)
{
	return rt9490_update8(chgnum, reg, mask, MASK_SET);
}

static inline int rt9490_clr_bit(int chgnum, int reg, int mask)
{
	return rt9490_update8(chgnum, reg, mask, MASK_CLR);
}

static inline int rt9490_enable_hz(int chgnum, bool en)
{
	return rt9490_update8(chgnum, RT9490_REG_CHG_CTRL0, RT9490_EN_HZ,
			      en ? MASK_SET : MASK_CLR);
}

static const struct charger_info *rt9490_get_info(int chgnum)
{
	return &rt9490_charger_info;
}

static enum ec_error_list rt9490_get_current(int chgnum, int *current)
{
	uint16_t val = 0;
	const struct charger_info * const info = rt9490_get_info(chgnum);

	RETURN_ERROR(rt9490_read16(chgnum, RT9490_REG_ICHG_CTRL, &val));

	val = (val & RT9490_ICHG_MASK) >> RT9490_ICHG_SHIFT;
	val *= info->current_step;
	*current = CLAMP(val, info->current_min, info->current_max);

	return EC_SUCCESS;
}

static enum ec_error_list rt9490_set_current(int chgnum, int current)
{
	uint16_t reg_ichg;
	const struct charger_info *const info = rt9490_get_info(chgnum);

	if (current == 0)
		current = info->current_min;

	if (!IN_RANGE(current, info->current_min, info->current_max + 1))
		return EC_ERROR_PARAM2;
	reg_ichg = current / info->current_step;

	return rt9490_write16(chgnum, RT9490_REG_ICHG_CTRL, reg_ichg);
}

static enum ec_error_list rt9490_get_voltage(int chgnum, int *voltage)
{
	uint16_t val = 0;
	const struct charger_info * const info = rt9490_get_info(chgnum);

	RETURN_ERROR(rt9490_read16(chgnum, RT9490_REG_VCHG_CTRL, &val));

	val = val & RT9490_CV_MASK;
	val *= info->voltage_step;
	*voltage = CLAMP(val, info->voltage_min, info->voltage_max);

	return EC_SUCCESS;
}

static enum ec_error_list rt9490_set_voltage(int chgnum, int voltage)
{
	uint16_t reg_cv;
	const struct charger_info *const info = rt9490_get_info(chgnum);

	if (voltage == 0)
		voltage = info->voltage_min;

	if (!IN_RANGE(voltage, info->voltage_min, info->voltage_max + 1))
		return EC_ERROR_PARAM2;
	reg_cv = voltage / info->voltage_step;

	return rt9490_write16(chgnum, RT9490_REG_VCHG_CTRL, reg_cv);
}

#ifdef CONFIG_CHARGER_OTG
static enum ec_error_list rt9490_enable_otg_power(int chgnum, int enabled)
{
	return rt9490_update8(chgnum, RT9490_REG_CHG_CTRL3, RT9490_EN_OTG,
			      enabled ? MASK_SET : MASK_CLR);
}

enum ec_error_list rt9490_set_otg_current_voltage(int chgnum,
						  int output_current,
						  int output_voltage)
{
	uint16_t reg_cur, reg_vol;

	if (!IN_RANGE(output_current, RT9490_IOTG_MIN, RT9490_IOTG_MAX + 1))
		return EC_ERROR_PARAM2;
	if (!IN_RANGE(output_voltage, RT9490_VOTG_MIN, RT9490_VOTG_MAX + 1))
		return EC_ERROR_PARAM3;

	reg_cur = (output_current - RT9490_IOTG_MIN) / RT9490_IOTG_STEP;
	reg_vol = (output_voltage - RT9490_VOTG_MIN) / RT9490_VOTG_STEP;
	RETURN_ERROR(rt9490_write16(chgnum, RT9490_REG_IOTG_REGU, reg_cur));

	return rt9490_write16(chgnum, RT9490_REG_VOTG_REGU, reg_vol);
}

static int rt9490_is_sourcing_otg_power(int chgnum, int port)
{
	int val;

	if (rt9490_read8(chgnum, RT9490_REG_CHG_CTRL3, &val))
		return 0;
	return !!(val & RT9490_EN_OTG);
}
#endif

/* Reset all registers' value to default */
static int rt9490_reset_chip(int chgnum)
{
	/* disable hz before reset chip */
	RETURN_ERROR(rt9490_enable_hz(chgnum, false));

	return rt9490_set_bit(chgnum, RT9490_REG_EOC_CTRL, RT9490_RST_ALL_MASK);
}

static inline int rt9490_enable_chgdet_flow(int chgnum, bool en)
{
	return rt9490_update8(chgnum, RT9490_REG_CHG_CTRL2, RT9490_BC12_EN,
			      en ? MASK_SET : MASK_CLR);
}

static inline int rt9490_enable_wdt(int chgnum, bool en)
{
	int val = en ? RT9490_WATCHDOG_40_SEC : RT9490_WATCHDOG_DISABLE;

	return rt9490_field_update8(chgnum, RT9490_REG_CHG_CTRL1,
				    RT9490_WATCHDOG_MASK, val);
}

static inline int rt9490_set_mivr(int chgnum, unsigned int mivr)
{
	uint8_t reg_mivr = mivr / RT9490_MIVR_STEP;

	return rt9490_write8(chgnum, RT9490_REG_MIVR_CTRL, reg_mivr);
}

static inline int rt9490_set_ieoc(int chgnum, unsigned int ieoc)
{
	uint8_t reg_ieoc = ieoc / RT9490_IEOC_STEP;

	return rt9490_field_update8(chgnum, RT9490_REG_EOC_CTRL,
				    RT9490_IEOC_MASK, reg_ieoc);
}

static inline int rt9490_enable_jeita(int chgnum, bool en)
{
	return rt9490_update8(chgnum, RT9490_REG_JEITA_CTRL1, RT9490_JEITA_DIS,
			      en ? MASK_CLR : MASK_SET);
}

static inline int rt9490_enable_adc(int chgnum, bool en)
{
	return rt9490_update8(chgnum, RT9490_REG_ADC_CTRL, RT9490_ADC_EN,
			      en ? MASK_SET : MASK_CLR);
}

static int rt9490_set_iprec(int chgnum, unsigned int iprec)
{
	uint8_t reg_iprec = iprec / RT9490_IPRE_CHG_STEP;

	return rt9490_field_update8(chgnum, RT9490_REG_PRE_CHG,
				    RT9490_IPRE_CHG_MASK,
				    reg_iprec << RT9490_IPREC_SHIFT);
}

static int rt9490_init_setting(int chgnum)
{
	const struct battery_info *batt_info = battery_get_info();

#ifdef CONFIG_CHARGER_OTG
	/*  Disable boost-mode output voltage */
	RETURN_ERROR(rt9490_enable_otg_power(chgnum, 0));
	RETURN_ERROR(rt9490_set_otg_current_voltage(
			chgnum,
			default_init_setting.boost_current,
			default_init_setting.boost_voltage));
#endif
	/* Disable ILIM_HZ pin current limit */
	RETURN_ERROR(rt9490_clr_bit(
			chgnum, RT9490_REG_CHG_CTRL5, RT9490_ILIM_HZ_EN));
	/* Disable BC 1.2 detection by default. It will be enabled on demand */
	RETURN_ERROR(rt9490_enable_chgdet_flow(chgnum, false));
	/* Disable WDT */
	RETURN_ERROR(rt9490_enable_wdt(chgnum, false));
	/* Disable battery thermal protection */
	RETURN_ERROR(rt9490_set_bit(
			chgnum, RT9490_REG_ADD_CTRL0, RT9490_JEITA_COLD_HOT));
	/* Disable AUTO_AICR / AUTO_MIVR */
	RETURN_ERROR(rt9490_clr_bit(
			chgnum,
			RT9490_REG_ADD_CTRL0,
			RT9490_AUTO_AICR | RT9490_AUTO_MIVR));
	/* Disable charge timer */
	RETURN_ERROR(rt9490_clr_bit(
			chgnum,
			RT9490_REG_SAFETY_TMR_CTRL,
			RT9490_EN_TRICHG_TMR |
			RT9490_EN_PRECHG_TMR |
			RT9490_EN_FASTCHG_TMR));
	RETURN_ERROR(rt9490_set_mivr(chgnum, default_init_setting.mivr));
	RETURN_ERROR(rt9490_set_ieoc(chgnum, default_init_setting.eoc_current));
	RETURN_ERROR(rt9490_set_iprec(chgnum, batt_info->precharge_current));
	RETURN_ERROR(rt9490_enable_adc(chgnum, true));
	RETURN_ERROR(rt9490_enable_jeita(chgnum, false));
	RETURN_ERROR(rt9490_field_update8(
			chgnum, RT9490_REG_CHG_CTRL1, RT9490_VAC_OVP_MASK,
			RT9490_VAC_OVP_26V << RT9490_VAC_OVP_SHIFT));


	return EC_SUCCESS;
}

static void rt9490_init(int chgnum)
{
	int ret = rt9490_init_setting(chgnum);

	CPRINTS("init%d %s(%d)", chgnum, ret ? "fail" : "good", ret);
}

static enum ec_error_list rt9490_get_status(int chgnum, int *status)
{
	int val = 0;

	*status = 0;

	RETURN_ERROR(rt9490_read8(chgnum, RT9490_REG_CHG_CTRL0, &val));
	if (!(val & RT9490_EN_CHG))
		*status |= CHARGER_CHARGE_INHIBITED;

	RETURN_ERROR(rt9490_read8(chgnum, RT9490_REG_FAULT_STATUS0, &val));
	if (val & RT9490_VBAT_OVP_STAT)
		*status |= CHARGER_VOLTAGE_OR;

	RETURN_ERROR(rt9490_read8(chgnum, RT9490_REG_CHG_STATUS4, &val));
	if (val & RT9490_JEITA_COLD_MASK) {
		*status |= CHARGER_RES_COLD;
		*status |= CHARGER_RES_UR;
	}
	if (val & RT9490_JEITA_COOL_MASK) {
		*status |= CHARGER_RES_COLD;
	}
	if (val & RT9490_JEITA_WARM_MASK) {
		*status |= CHARGER_RES_HOT;
	}
	if (val & RT9490_JEITA_HOT_MASK) {
		*status |= CHARGER_RES_HOT;
		*status |= CHARGER_RES_OR;
	}
	return EC_SUCCESS;
}

static int rt9490_reset_to_zero(int chgnum)
{
	RETURN_ERROR(rt9490_set_current(chgnum, 0));
	RETURN_ERROR(rt9490_set_voltage(chgnum, 0));
	RETURN_ERROR(rt9490_enable_hz(chgnum, true));

	return EC_SUCCESS;
}

static enum ec_error_list rt9490_set_mode(int chgnum, int mode)
{
	if (mode & CHARGE_FLAG_POR_RESET)
		RETURN_ERROR(rt9490_reset_chip(chgnum));
	if (mode & CHARGE_FLAG_RESET_TO_ZERO)
		RETURN_ERROR(rt9490_reset_to_zero(chgnum));
	return EC_SUCCESS;
}

static enum ec_error_list rt9490_get_actual_current(int chgnum, int *current)
{
	uint16_t reg_val;

	RETURN_ERROR(rt9490_read16(chgnum, RT9490_REG_IBAT_ADC, &reg_val));
	*current = (int)reg_val * 1000;
	return EC_SUCCESS;
}

static enum ec_error_list rt9490_get_actual_voltage(int chgnum, int *voltage)
{
	uint16_t reg_val;

	RETURN_ERROR(rt9490_read16(chgnum, RT9490_REG_VBAT_ADC, &reg_val));
	*voltage = (int)reg_val * 1000;
	return EC_SUCCESS;
}

static enum ec_error_list rt9490_discharge_on_ac(int chgnum, int enable)
{
	return rt9490_enable_hz(chgnum, enable);
}

static enum ec_error_list rt9490_get_vbus_voltage(int chgnum, int port,
						  int *voltage)
{
	uint16_t reg_val;

	RETURN_ERROR(rt9490_read16(chgnum, RT9490_REG_VBUS_ADC, &reg_val));
	*voltage = (int)reg_val * 1000;
	return EC_SUCCESS;
}

static enum ec_error_list rt9490_set_input_current_limit(int chgnum,
							 int input_current)
{
	uint16_t reg_val = input_current / RT9490_AICR_STEP;

	return rt9490_write16(chgnum, RT9490_REG_AICR_CTRL, reg_val);
}

static enum ec_error_list rt9490_get_input_current_limit(int chgnum,
							 int *input_current)
{
	uint16_t val = 0;

	RETURN_ERROR(rt9490_read16(chgnum, RT9490_REG_AICR_CTRL, &val));
	val = (val & RT9490_AICR_MASK) >> RT9490_AICR_SHIFT;
	val *= RT9490_AICR_STEP;
	*input_current = CLAMP(val, RT9490_AICR_MIN, RT9490_AICR_MAX);
	return EC_SUCCESS;
}

static enum ec_error_list rt9490_get_input_current(int chgnum,
						   int *input_current)
{
	uint16_t reg_val;

	RETURN_ERROR(rt9490_read16(chgnum, RT9490_REG_IBUS_ADC, &reg_val));
	*input_current = (int)reg_val * 1000;
	return EC_SUCCESS;
}

static enum ec_error_list rt9490_device_id(int chgnum, int *id)
{
	RETURN_ERROR(rt9490_read8(chgnum, RT9490_REG_DEVICE_INFO, id));
	*id &= RT9490_DEVICE_INFO_MASK;
	return EC_SUCCESS;
}

#ifdef CONFIG_CHARGE_RAMP_HW
static enum ec_error_list rt9490_set_hw_ramp(int chgnum, int enable)
{
	if (enable) {
		RETURN_ERROR(rt9490_set_bit(chgnum, RT9490_REG_CHG_CTRL0,
					    RT9490_EN_AICC));
		RETURN_ERROR(rt9490_set_bit(chgnum, RT9490_REG_CHG_CTRL0,
					    RT9490_FORCE_AICC));
	} else {
		RETURN_ERROR(rt9490_clr_bit(chgnum, RT9490_REG_CHG_CTRL0,
					    RT9490_EN_AICC));
	}
	return EC_SUCCESS;
}

static int rt9490_ramp_is_stable(int chgnum)
{
	int rv;
	int val = 0;

	rv = rt9490_read8(chgnum, RT9490_REG_CHG_CTRL0, &val);
	return !rv && !(val & RT9490_FORCE_AICC);
}

static int rt9490_ramp_is_detected(int chgnum)
{
	return true;
}

static int rt9490_ramp_get_current_limit(int chgnum)
{
	int rv;
	int input_current = 0;

	rv = rt9490_get_input_current_limit(chgnum, &input_current);
	return rv ? -1 : input_current;
}
#endif

const struct charger_drv rt9490_drv = {
	.init = &rt9490_init,
	.get_info = &rt9490_get_info,
	.get_status = &rt9490_get_status,
	.set_mode = &rt9490_set_mode,
#ifdef CONFIG_CHARGER_OTG
	.enable_otg_power = &rt9490_enable_otg_power,
	.set_otg_current_voltage = &rt9490_set_otg_current_voltage,
	.is_sourcing_otg_power = &rt9490_is_sourcing_otg_power,
#endif
	.get_current = &rt9490_get_current,
	.set_current = &rt9490_set_current,
	.get_voltage = &rt9490_get_voltage,
	.set_voltage = &rt9490_set_voltage,
	.get_actual_current = &rt9490_get_actual_current,
	.get_actual_voltage = &rt9490_get_actual_voltage,
	.discharge_on_ac = &rt9490_discharge_on_ac,
	.get_vbus_voltage = &rt9490_get_vbus_voltage,
	.set_input_current_limit = &rt9490_set_input_current_limit,
	.get_input_current_limit = &rt9490_get_input_current_limit,
	.get_input_current = &rt9490_get_input_current,
	.device_id = &rt9490_device_id,
#ifdef CONFIG_CHARGE_RAMP_HW
	.set_hw_ramp = &rt9490_set_hw_ramp,
	.ramp_is_stable = &rt9490_ramp_is_stable,
	.ramp_is_detected = &rt9490_ramp_is_detected,
	.ramp_get_current_limit = &rt9490_ramp_get_current_limit,
#endif
};