summaryrefslogtreecommitdiff
path: root/zephyr/program/corsola/src/krabby/charger_workaround.c
blob: be046a947bf7973a30a79f2583f6d8aba4a2e290 (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
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "charger.h"
#include "driver/charger/rt9490.h"
#include "hooks.h"
#include "i2c.h"
#include "system.h"

#include <zephyr/sys/util.h>

/*
 * This workaround and the board id checks only apply to krabby and early
 * tentacruel devices.
 * Newer project should have all of these fixed.
 */
BUILD_ASSERT(IS_ENABLED(CONFIG_BOARD_KRABBY) ||
	     IS_ENABLED(CONFIG_BOARD_TENTACRUEL) || IS_ENABLED(CONFIG_TEST));

/* b/194967754#comment5: work around for IBUS ADC unstable issue */
static void ibus_adc_workaround(void)
{
	if (system_get_board_version() != 0) {
		return;
	}

	i2c_update8(chg_chips[CHARGER_SOLO].i2c_port,
		    chg_chips[CHARGER_SOLO].i2c_addr_flags,
		    RT9490_REG_ADC_CHANNEL0, RT9490_VSYS_ADC_DIS, MASK_SET);

	rt9490_enable_hidden_mode(CHARGER_SOLO, true);
	/* undocumented registers... */
	i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
		   chg_chips[CHARGER_SOLO].i2c_addr_flags, 0x52, 0xC4);

	i2c_update8(chg_chips[CHARGER_SOLO].i2c_port,
		    chg_chips[CHARGER_SOLO].i2c_addr_flags,
		    RT9490_REG_ADC_CHANNEL0, RT9490_VSYS_ADC_DIS, MASK_CLR);
	rt9490_enable_hidden_mode(CHARGER_SOLO, false);
}

/* b/214880220#comment44: lock i2c at 400khz */
static void i2c_speed_workaround(void)
{
	if (system_get_board_version() >= 3) {
		return;
	}

	rt9490_enable_hidden_mode(CHARGER_SOLO, true);
	/* Set to Auto mode, default run at 400kHz */
	i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
		   chg_chips[CHARGER_SOLO].i2c_addr_flags, 0x71, 0x22);
	/* Manually select for 400kHz, valid only when 0x71[7] == 1 */
	i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
		   chg_chips[CHARGER_SOLO].i2c_addr_flags, 0xF7, 0x14);
	rt9490_enable_hidden_mode(CHARGER_SOLO, false);
}

static void eoc_deglitch_workaround(void)
{
	if (system_get_board_version() != 1) {
		return;
	}

	/* set end-of-charge deglitch time to 2ms */
	i2c_update8(chg_chips[CHARGER_SOLO].i2c_port,
		    chg_chips[CHARGER_SOLO].i2c_addr_flags,
		    RT9490_REG_ADD_CTRL0, RT9490_TD_EOC, MASK_CLR);
}

static void disable_safety_timer(void)
{
	if (system_get_board_version() >= 2) {
		return;
	}
	/* Disable charge timer */
	i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
		   chg_chips[CHARGER_SOLO].i2c_addr_flags,
		   RT9490_REG_SAFETY_TMR_CTRL,
		   RT9490_EN_TRICHG_TMR | RT9490_EN_PRECHG_TMR |
			   RT9490_EN_FASTCHG_TMR);
}

static void board_rt9490_workaround(void)
{
	ibus_adc_workaround();
	i2c_speed_workaround();
	eoc_deglitch_workaround();
	disable_safety_timer();
}
DECLARE_HOOK(HOOK_INIT, board_rt9490_workaround, HOOK_PRIO_DEFAULT);