Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 124 additions & 60 deletions drivers/power/supply/apple_tristar_charger.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,24 @@
struct tristar_charger;
struct tristar_pack;

/* How the source holds up under the present grant, as a chip can tell. */
enum tristar_sag {
TRISTAR_SAG_NONE, /* the input holds */
TRISTAR_SAG_UNSURE, /* no usable sample this time: hold the step */
TRISTAR_SAG_DROOP, /* the source sags: back off a step */
TRISTAR_SAG_FAULT, /* outside the safe range: fall back */
};

static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg);

/* Keep shared charging policy independent of the PMIC input-limit registers. */
struct tristar_chip_info {
/*
* Optional: tell input sag while a grant is above the fallback or
* aims above 1 A. A board with a VBUS ADC reads it; one without
* could report the PMIC's input-collapse state instead.
*/
enum tristar_sag (*input_sag)(struct tristar_charger *chg);
int (*prepare)(struct tristar_charger *chg);
bool (*pack_allows)(struct tristar_charger *chg, const struct tristar_pack *pack);
unsigned int status_size, control_size;
Expand Down Expand Up @@ -261,9 +277,10 @@ struct tristar_charger {
u32 adc_base;
unsigned int max_ma;
struct iio_channel *vbus;
unsigned int vbus_limit_ma;
unsigned int sag_limit_ma; /* input cap found by sag, 0 for none */
unsigned int vbus_read_errors;
bool vbus_fault;
bool sag_seen; /* one sag sample, awaiting a second */
bool input_fault;

/* Serializes policy state and PMIC/Tristar sequences. */
struct mutex lock;
Expand Down Expand Up @@ -350,6 +367,7 @@ static int tristar_d2365_read_status(struct tristar_charger *chg,
}

static const struct tristar_chip_info tristar_d2365_chip = {
.input_sag = tristar_vbus_sag,
.adc_error_mask = 0x80,
.read_limit = tristar_d2365_read_limit,
.write_limit = tristar_d2365_write_limit,
Expand Down Expand Up @@ -900,8 +918,8 @@ static void tristar_set_usb(struct tristar_charger *chg, bool on)
static unsigned int tristar_target_code(struct tristar_charger *chg)
{
unsigned int ma = max(chg->source_ma, tristar_usb_ma(chg));
unsigned int cap = chg->vbus_limit_ma ?
min(chg->max_ma, chg->vbus_limit_ma) : chg->max_ma;
unsigned int cap = chg->sag_limit_ma ?
min(chg->max_ma, chg->sag_limit_ma) : chg->max_ma;

if (ma <= tristar_code_ma(chg, chg->chip->fallback_code))
return chg->chip->fallback_code;
Expand Down Expand Up @@ -938,9 +956,10 @@ static void tristar_new_attachment(struct tristar_charger *chg, u64 now)
/* Before anything else: a host must not start a session yet. */
tristar_set_usb(chg, false);
chg->present = true;
chg->vbus_limit_ma = 0;
chg->sag_limit_ma = 0;
chg->vbus_read_errors = 0;
chg->vbus_fault = false;
chg->sag_seen = false;
chg->input_fault = false;
chg->phase = TRISTAR_SETTLING;
chg->attach_ns = now;
chg->hold_ns = now;
Expand Down Expand Up @@ -1080,12 +1099,103 @@ static bool tristar_debouncing(struct tristar_charger *chg, u64 now)
(s64)(now - line_ns) < (s64)TRISTAR_EVENT_DEBOUNCE_MS * NSEC_PER_MSEC;
}

/*
* VBUS at the connector, after the cable's and adapter's IR drop, for boards
* whose PMIC samples it. A failed read holds the step a few times before it
* counts as a fault.
*/
static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg)
{
int mv, ret;

if (!IS_REACHABLE(CONFIG_IIO) || !chg->vbus)
return TRISTAR_SAG_NONE;
ret = iio_read_channel_processed(chg->vbus, &mv);
if (ret) {
if (++chg->vbus_read_errors >= TRISTAR_VBUS_READ_RETRIES) {
dev_warn(chg->dev, "VBUS unreadable (error %d after %u attempts)\n",
ret, chg->vbus_read_errors);
return TRISTAR_SAG_FAULT;
}
dev_dbg(chg->dev, "VBUS read error %d (%u/%u), holding step\n",
ret, chg->vbus_read_errors, TRISTAR_VBUS_READ_RETRIES);
return TRISTAR_SAG_UNSURE;
}
chg->vbus_read_errors = 0;
if (chg->phase == TRISTAR_RAMPING)
dev_dbg(chg->dev, "VBUS %d mV at %u mA (target %u mA)\n", mv,
tristar_code_ma(chg, chg->code),
tristar_code_ma(chg, chg->target_code));
if (mv < TRISTAR_VBUS_FAULT_MV || mv > TRISTAR_VBUS_MAX_MV) {
dev_warn(chg->dev, "VBUS %d mV outside the safe range\n", mv);
return TRISTAR_SAG_FAULT;
}
if (mv < TRISTAR_VBUS_DROOP_MV) {
dev_dbg(chg->dev, "VBUS droop: %d mV\n", mv);
return TRISTAR_SAG_DROOP;
}
return TRISTAR_SAG_NONE;
}

/*
* Ask the chip how the source holds up while there is a grant worth
* watching: a fault falls back, a sag backs off one step and caps the input
* for this attachment. A sag must show in two samples in a row before the
* limit is lowered, as the SN2400 driver's foldback debounce does: a single
* low sample during a load step must not cost the attachment its current.
* Returns true when the ramp or guard must not go on this time. Caller
* holds lock.
*/
static bool tristar_check_sag(struct tristar_charger *chg)
{
unsigned int cur_ma = tristar_code_ma(chg, chg->code);
unsigned int fallback_ma = tristar_code_ma(chg, chg->chip->fallback_code);
enum tristar_sag sag;

if (!chg->chip->input_sag || chg->input_fault)
return false;
if (cur_ma <= fallback_ma && tristar_code_ma(chg, chg->target_code) <= 1000)
return false;

sag = chg->chip->input_sag(chg);
if (sag == TRISTAR_SAG_DROOP && !chg->sag_seen) {
chg->sag_seen = true;
return true;
}
chg->sag_seen = false;
switch (sag) {
case TRISTAR_SAG_NONE:
return false;
case TRISTAR_SAG_UNSURE:
return true;
case TRISTAR_SAG_FAULT:
chg->input_fault = true;
chg->phase = TRISTAR_FAULT;
tristar_withdraw(chg, chg->chip->fallback_code);
dev_warn(chg->dev, "input guard tripped; holding the fallback\n");
return true;
case TRISTAR_SAG_DROOP:
chg->sag_limit_ma = cur_ma > fallback_ma + TRISTAR_RAMP_STEP_MA ?
cur_ma - TRISTAR_RAMP_STEP_MA : fallback_ma;
chg->target_code = tristar_target_code(chg);
tristar_withdraw(chg, chg->target_code);
chg->phase = TRISTAR_CHARGING;
dev_info(chg->dev, "input sags at %u mA; capping input to %u mA\n",
cur_ma, tristar_code_ma(chg, chg->target_code));
return true;
}
return false;
}

static void tristar_ramp_step(struct tristar_charger *chg, u64 now)
{
struct tristar_pack pack;
unsigned int next, from_ma = tristar_code_ma(chg, chg->code);
unsigned int next, from_ma;
u64 wait;

if (tristar_check_sag(chg))
return;
from_ma = tristar_code_ma(chg, chg->code);
if (tristar_debouncing(chg, now) ||
(chg->step_ns && now - chg->step_ns < chg->step_wait_ns))
return;
Expand Down Expand Up @@ -1186,6 +1296,8 @@ static void tristar_guard(struct tristar_charger *chg, u64 now)
{
struct tristar_pack pack;

if (tristar_check_sag(chg))
return;
if (now - chg->guard_ns < NSEC_PER_SEC)
return;
chg->guard_ns = now;
Expand Down Expand Up @@ -1291,7 +1403,7 @@ static void tristar_poll(struct work_struct *work)
struct tristar_charger *chg = container_of(to_delayed_work(work),
struct tristar_charger, poll_work);
unsigned int status, old_code;
bool old_online, old_present, old_usb_on, vbus_hold = false;
bool old_online, old_present, old_usb_on;
u64 now = ktime_get_ns(), line_ns;

mutex_lock(&chg->lock);
Expand All @@ -1316,8 +1428,9 @@ static void tristar_poll(struct work_struct *work)
chg->phase = TRISTAR_EMPTY;
chg->source[0] = '\0';
chg->source_ma = 0;
chg->vbus_limit_ma = 0;
chg->sag_limit_ma = 0;
chg->vbus_read_errors = 0;
chg->sag_seen = false;
chg->usb_ma = 0;
chg->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
if (tristar_set_code(chg, chg->chip->handoff_code))
Expand All @@ -1337,55 +1450,7 @@ static void tristar_poll(struct work_struct *work)
tristar_new_attachment(chg, now);
goto again;
}
if (IS_REACHABLE(CONFIG_IIO) && chg->vbus && !chg->vbus_fault &&
(chg->phase == TRISTAR_RAMPING || chg->phase == TRISTAR_CHARGING)) {
unsigned int cur_ma = tristar_code_ma(chg, chg->code);
unsigned int target_ma = tristar_code_ma(chg, chg->target_code);
unsigned int fallback_ma = tristar_code_ma(chg, chg->chip->fallback_code);

if (cur_ma > fallback_ma || target_ma > 1000) {
int mv, ret;

ret = iio_read_channel_processed(chg->vbus, &mv);
if (ret) {
if (++chg->vbus_read_errors >= TRISTAR_VBUS_READ_RETRIES) {
chg->vbus_fault = true;
chg->phase = TRISTAR_FAULT;
dev_warn(chg->dev,
"VBUS guard tripped (sample 0 mV, error %d after %u attempts)\n",
ret, chg->vbus_read_errors);
} else {
dev_dbg(chg->dev,
"VBUS read error %d (%u/%u), holding step\n",
ret, chg->vbus_read_errors,
TRISTAR_VBUS_READ_RETRIES);
vbus_hold = true;
}
} else {
chg->vbus_read_errors = 0;
if (chg->phase == TRISTAR_RAMPING)
dev_dbg(chg->dev, "VBUS %d mV at %u mA (target %u mA)\n",
mv, cur_ma, target_ma);
if (mv < TRISTAR_VBUS_FAULT_MV || mv > TRISTAR_VBUS_MAX_MV) {
chg->vbus_fault = true;
chg->phase = TRISTAR_FAULT;
dev_warn(chg->dev, "VBUS guard tripped (sample %d mV, error 0)\n",
mv);
} else if (mv < TRISTAR_VBUS_DROOP_MV) {
unsigned int limit_ma = cur_ma > fallback_ma + TRISTAR_RAMP_STEP_MA ?
cur_ma - TRISTAR_RAMP_STEP_MA : fallback_ma;

chg->vbus_limit_ma = limit_ma;
chg->target_code = tristar_target_code(chg);
tristar_withdraw(chg, chg->target_code);
chg->phase = TRISTAR_CHARGING;
dev_info(chg->dev, "VBUS droop (%d mV at %u mA); capping input to %u mA\n",
mv, cur_ma, tristar_code_ma(chg, chg->target_code));
}
}
}
}
if (chg->vbus_fault)
if (chg->input_fault)
tristar_withdraw(chg, chg->chip->fallback_code);
if (chg->phase == TRISTAR_FAULT) {
/* USB data must not depend on charging working. */
Expand Down Expand Up @@ -1418,8 +1483,7 @@ static void tristar_poll(struct work_struct *work)
tristar_run_identification(chg, now);
break;
case TRISTAR_RAMPING:
if (!vbus_hold)
tristar_ramp_step(chg, now);
tristar_ramp_step(chg, now);
break;
case TRISTAR_CHARGING:
tristar_guard(chg, now);
Expand Down
1 change: 1 addition & 0 deletions drivers/power/supply/apple_tristar_d2355.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ static int tristar_d2355_prepare(struct tristar_charger *chg)
}

static const struct tristar_chip_info tristar_d2355_chip = {
.input_sag = tristar_vbus_sag,
.prepare = tristar_d2355_prepare,
.pack_allows = tristar_d2355_pack_allows,
.status_size = 10,
Expand Down
Loading