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
5 changes: 3 additions & 2 deletions arch/arm64/boot/dts/apple/t8011-pro2.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
/* The boot loader adds this unit's ADT calibration table. */
/* A conservative window, not Apple's production limits. */
apple,pack-limits = <150 350 3400000 4200000 (-2000000) 2000000>;
/* 1.5 A only while the Lightning VBUS reads 4.75-5.5 V. */
/* Up to 1.5 A while Lightning VBUS stays in the 5 V window. */
input-current-limit-microamp = <1500000>;
io-channels = <&orion_vbus 0>;
io-channel-names = "vbus";
Expand Down Expand Up @@ -504,8 +504,9 @@
};
};

/* The Lightning charger sets the data role once the source is known. */
/* The Lightning charger sets the data role and receives the USB host budget. */
&usb2dev {
usb-psy-name = "lightning";
usb-role-switch;

port {
Expand Down
82 changes: 68 additions & 14 deletions drivers/power/supply/apple_tristar_charger.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ static const struct tristar_pmic tristar_d2355 = {
#define TRISTAR_EVENT_DEBOUNCE_MS 50
/* More source events than this per second means something is stuck. */
#define TRISTAR_IRQ_STORM 200
/*
* Lightning VBUS telemetry bounds (mV) for low-voltage (5 V) charging:
* overvoltage (> 5.5 V, e.g. high-voltage PD) and severe brownout (< 4.35 V)
* fault to the fallback; normal cable/adapter droop below 4.5 V caps the
* input current by backing off a step instead of terminating charging.
*/
#define TRISTAR_VBUS_MAX_MV 5500
#define TRISTAR_VBUS_DROOP_MV 4500
#define TRISTAR_VBUS_FAULT_MV 4350
#define TRISTAR_VBUS_READ_RETRIES 3

enum tristar_phase {
TRISTAR_EMPTY,
Expand Down Expand Up @@ -251,6 +261,8 @@ struct tristar_charger {
u32 adc_base;
unsigned int max_ma;
struct iio_channel *vbus;
unsigned int vbus_limit_ma;
unsigned int vbus_read_errors;
bool vbus_fault;

/* Serializes policy state and PMIC/Tristar sequences. */
Expand Down Expand Up @@ -884,14 +896,16 @@ static void tristar_set_usb(struct tristar_charger *chg, bool on)
}
}

/* The highest grant the source allows, up to the board maximum. */
/* The highest grant the source allows, up to the board and VBUS maximums. */
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;

if (ma <= tristar_code_ma(chg, chg->chip->fallback_code))
return chg->chip->fallback_code;
return tristar_ma_code(chg, min(ma, chg->max_ma));
return tristar_ma_code(chg, min(ma, cap));
}

/* Follow a changed allowance once the source is identified. Caller holds lock. */
Expand Down Expand Up @@ -924,6 +938,8 @@ 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->vbus_read_errors = 0;
chg->vbus_fault = false;
chg->phase = TRISTAR_SETTLING;
chg->attach_ns = now;
Expand Down Expand Up @@ -1275,7 +1291,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;
bool old_online, old_present, old_usb_on, vbus_hold = false;
u64 now = ktime_get_ns(), line_ns;

mutex_lock(&chg->lock);
Expand All @@ -1300,6 +1316,8 @@ 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->vbus_read_errors = 0;
chg->usb_ma = 0;
chg->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
if (tristar_set_code(chg, chg->chip->handoff_code))
Expand All @@ -1320,16 +1338,51 @@ static void tristar_poll(struct work_struct *work)
goto again;
}
if (IS_REACHABLE(CONFIG_IIO) && chg->vbus && !chg->vbus_fault &&
(chg->phase == TRISTAR_RAMPING || chg->phase == TRISTAR_CHARGING) &&
tristar_code_ma(chg, chg->target_code) > 1000) {
int mv, ret;

ret = iio_read_channel_processed(chg->vbus, &mv);
if (ret || mv < 4750 || mv > 5500) {
chg->vbus_fault = true;
chg->phase = TRISTAR_FAULT;
dev_warn(chg->dev, "VBUS guard tripped (sample %d mV, error %d)\n",
ret ? 0 : mv, ret);
(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)
Expand Down Expand Up @@ -1365,7 +1418,8 @@ static void tristar_poll(struct work_struct *work)
tristar_run_identification(chg, now);
break;
case TRISTAR_RAMPING:
tristar_ramp_step(chg, now);
if (!vbus_hold)
tristar_ramp_step(chg, now);
break;
case TRISTAR_CHARGING:
tristar_guard(chg, now);
Expand Down
Loading