diff --git a/arch/arm/boot/dts/apple/s5l8945x-ipad3.dtsi b/arch/arm/boot/dts/apple/s5l8945x-ipad3.dtsi index 6be27a88a2025f..9895acd7f894eb 100644 --- a/arch/arm/boot/dts/apple/s5l8945x-ipad3.dtsi +++ b/arch/arm/boot/dts/apple/s5l8945x-ipad3.dtsi @@ -559,6 +559,26 @@ mask = <0x80>; enable = <0x80>; }; + + /* + * D1974 USB charger: input current limit at 0x3e, + * VBUS online at 0x0b bit 3, 12-bit ADC at 0x44..0x46 + * (ch 6 wired to otgphyctrl0 DMON), ceiling 2400 mA + * per ADT /charger usb-input-limit-max 0x960. + */ + charger@3e { + compatible = "apple,pmu-d1974-charger"; + reg = <0x3e>; + phys = <&otgphy 0>; + phy-names = "dmon"; + input-current-limit-microamp = <2400000>; + + port { + charger_usb_role: endpoint { + remote-endpoint = <&dwc2_role_sw>; + }; + }; + }; }; /* @@ -726,10 +746,18 @@ phys = <&otgphy 0>; phy-names = "usb2-phy"; dr_mode = "peripheral"; + usb-role-switch; + usb-psy-name = "apple-pmu-usb"; g-rx-fifo-size = <512>; g-np-tx-fifo-size = <128>; g-tx-fifo-size = <128 128 128 128 128 128 128 128>; status = "okay"; + + port { + dwc2_role_sw: endpoint { + remote-endpoint = <&charger_usb_role>; + }; + }; }; /* diff --git a/drivers/phy/phy-apple-s5l-usb.c b/drivers/phy/phy-apple-s5l-usb.c index 5219a3b36b0f5c..3582f60b24f953 100644 --- a/drivers/phy/phy-apple-s5l-usb.c +++ b/drivers/phy/phy-apple-s5l-usb.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,12 @@ #define OPHYUNK1_OTG_ENABLE BIT(1) #define OPHY_UOTGTUNE1 0x40 #define OPHY_UOTGTUNE2 0x44 +#define OPHY_DMON 0x48 +#define OPHY_DMON_MASK 0x7e06 +#define OPHY_DMON_DP 0x0002 +#define OPHY_DMON_DM 0x0004 +#define OPHY_DMON_BC12_DCD 0x6602 +#define OPHY_DMON_BC12_PRIMARY 0x1e04 #define OPHY_UOTGTUNE1_DEVICE 0x549 #define OPHY_UOTGTUNE1_S5L8950X_DEVICE 0x749 #define OPHY_UOTGTUNE2_DEVICE 0x2ff3 @@ -117,6 +124,7 @@ struct s5l_usbphy_soc_data { bool cold_init; + bool dmon_bc12; /* phy-version >= 4: BC1.2 DMON modes in +0x48 */ u32 pmgr_uperf; u32 pmgr_usb_otg; u32 pmgr_usbreg; @@ -158,6 +166,7 @@ static const struct s5l_usbphy_soc_data s5l8942x_usbphy_data = { */ static const struct s5l_usbphy_soc_data s5l8950x_usbphy_data = { .cold_init = true, + .dmon_bc12 = true, .pmgr_uperf = 0x1084, .pmgr_usb_otg = 0x1088, .pmgr_usbreg = 0x109c, @@ -182,6 +191,7 @@ static const struct s5l_usbphy_soc_data s5l8950x_usbphy_data = { */ static const struct s5l_usbphy_soc_data s5l8955x_usbphy_data = { .cold_init = false, + .dmon_bc12 = true, .pmgr_uperf = 0x1084, .pmgr_usb_otg = 0x1088, .pmgr_usbreg = 0x109c, @@ -477,6 +487,67 @@ static const struct phy_ops s5l_usbphy_ops = { .owner = THIS_MODULE, }; +/* + * Route USB D+/D- (and, on phy-version >= 4, BC1.2 test sources) to the PMU + * ADC via otgphyctrl +0x48 (AppleS5L8930XUSBPhy DMON). Callers must only + * enable a monitor mode when no USB host session is active, and must always + * restore APPLE_S5L_USBPHY_DMON_OFF after sampling. + */ +int apple_s5l_usbphy_set_dmon(struct phy *phy, + enum apple_s5l_usbphy_dmon_mode mode) +{ + struct s5l_usbphy *p; + u32 val, bits; + int ret = 0; + + if (!phy || phy->ops != &s5l_usbphy_ops) + return -EINVAL; + + p = phy_get_drvdata(phy); + switch (mode) { + case APPLE_S5L_USBPHY_DMON_OFF: + bits = 0; + break; + case APPLE_S5L_USBPHY_DMON_DP: + bits = OPHY_DMON_DP; + break; + case APPLE_S5L_USBPHY_DMON_DM: + bits = OPHY_DMON_DM; + break; + case APPLE_S5L_USBPHY_DMON_BC12_DCD: + if (!p->data || !p->data->dmon_bc12) + return -EOPNOTSUPP; + bits = OPHY_DMON_BC12_DCD; + break; + case APPLE_S5L_USBPHY_DMON_BC12_PRIMARY: + if (!p->data || !p->data->dmon_bc12) + return -EOPNOTSUPP; + bits = OPHY_DMON_BC12_PRIMARY; + break; + default: + return -EINVAL; + } + + mutex_lock(&p->lock); + if (!p->init_users) { + ret = s5l_pmgr_on(p, p->data->pmgr_usb_otg); + if (ret) + goto out; + if (p->data->cold_init) { + ret = s5l_pmgr_on(p, p->data->pmgr_usbreg); + if (ret) + goto out; + } + } + val = readl(p->base + OPHY_DMON); + val = (val & ~OPHY_DMON_MASK) | bits; + writel(val, p->base + OPHY_DMON); +out: + mutex_unlock(&p->lock); + return ret; +} +EXPORT_SYMBOL_GPL(apple_s5l_usbphy_set_dmon); + static int s5l_hostphy_power_on(struct phy *phy) { struct s5l_usbphy *p = phy_get_drvdata(phy); diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index b8a7413e22fca4..6902d9acf24de2 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -272,13 +272,19 @@ config BATTERY_APPLE_HDQ capacities and cycle count, as the "battery" power supply. config CHARGER_APPLE_PMU - tristate "Apple iPad mini 1 USB input current limit (D1946 PMU)" + tristate "Apple Dialog PMU adaptive USB charger (D1946/D1974)" depends on MFD_APPLE_PMU_I2C && POWER_SUPPLY - help - Raises the D1946 PMU's USB input current limit (register 0x3d) the - way iBEC does, so the iPad mini 1 charges under Linux. The driver - is armed by default; apple_pmu_charger.armed=0 on the kernel - command line disables writes. + depends on USB_ROLE_SWITCH || !USB_ROLE_SWITCH + depends on PHY_APPLE_S5L_USB || !PHY_APPLE_S5L_USB + help + Adaptive USB input current limit driver for Apple Dialog PMUs + (D1946 on iPad 2/mini 1, D1974 on iPad 3). Identifies attached + sources via USB PHY D+/D- monitoring (Apple BrickID, BC1.2 DCP) + and USB device controller vbus_draw (SDP and Apple host current + grants), ramps the input current limit with VBUS sag backoff and + battery thermal derating, and drops back to 100 mA on unplug or + source swaps. apple_pmu_charger.armed=0 on the kernel command + line disables PMU input-limit writes. config BATTERY_BQ27XXX tristate "BQ27xxx battery driver" diff --git a/drivers/power/supply/apple-pmu-charger.c b/drivers/power/supply/apple-pmu-charger.c index cf265c512f3bc5..dc3c37851433fc 100644 --- a/drivers/power/supply/apple-pmu-charger.c +++ b/drivers/power/supply/apple-pmu-charger.c @@ -1,45 +1,42 @@ // SPDX-License-Identifier: GPL-2.0 /* - * The iPad mini 1's USB input current limit: D1946 PMU register 0x3d. + * Apple Dialog PMU USB charger (D1946, D1974; extensible by match data). * - * iBEC hands over with 0x3d at 0x02, about 100 mA, and nothing in Linux used - * to raise it, so the iPad drained on any cable -- measured -386 mA on a - * host port and -388 mA on a wall charger that charges iOS at +1082 mA. - * The charger itself is already on: 0x3f and the temperature bands - * 0x4b..0x53 are left as iBoot set them, and so is the input-suspend pair - * 0x1b bit 2 / 0x1c bit 7. This driver only moves the input limit, the - * way iBEC's own apply step does it (0x9ff0c540 in the 12H321 iBEC): + * iBEC hands over with the PMU USB input current limit at 0x02 (~100 mA). + * Rather than applying a fixed limit regardless of what is plugged in, this + * driver identifies each attached power source before raising the limit: * - * code = (8 * mA - 600) / 100, then iBEC's correction: below 0x49 as is, - * 0x49..0x4a -> 0x48, 0x4b..0x6e -> -2, 0x6f..0x72 -> 0x6e, 0x73 and up - * -> -4; under 100 mA it is 0x02, and nothing goes above 2100 mA. + * - Start every attachment (and probe, unplug, or fast swap) at 100 mA + * (code 0x02), holding the USB gadget pull-up off via usb-role-switch + * when wired so D+/D- dividers are not disturbed. + * - Sample D- and D+ through the S5L USB PHY data monitor (otgphyctrl +0x48) + * and the PMU's 12-bit ADC to classify Apple BrickID chargers using iOS + * AppleARMPMUCharger / iBEC thresholds (500 / 1000 / 2100 / 2400 mA). + * - On phy-version >= 4 PHYs, run BC1.2 primary detection (1500 mA). + * - Otherwise release the USB gadget so a USB host (Mac, PC, or hub) + * enumerates and reports its allowed current through dwc2's vbus_draw + * path (100 mA unconfigured, 500 mA configured, 2 mA suspended, or up to + * the board maximum when an Apple host sends vendor request 0x40). + * - If no USB host enumerates within 1.5 s, passively sample D- once while + * the gadget's 1.5 kOhm D+ pull-up is active (zero active USB traffic): + * a BC1.2 DCP charger with shorted D+/D- pulls D- high (>= 1500 mV) and + * is granted 1500 mA; an unidentified source falls back to 500 mA. + * - Ramp upwards 8 codes (~100 mA) every 10 ms, capping at the board DT + * input-current-limit-microamp (and chip hard max), keeping VBUS sag + * backoff (0x02 readback -> step down 8 codes) as a safety net, and + * derating to 100 mA above 45 C (or on missing battery temperature) until + * below 40 C. + * - A board without the data monitor PHY ("dmon" in phys) has no way to + * tell sources apart, so it keeps the fixed 1000 mA target the driver + * always gave it (iBEC's value for a charger it does not know; a Mac's + * port gave it without a sag), with the same sag backoff, instead of + * the 500 mA fallback for unidentified sources. * - * Upwards it ramps, 8 codes every 10 ms. If 0x3d then reads back 0x02 - * the PMU has dropped the limit itself because VBUS sagged; iBEC and - * iOS (AppleD1946PMUPowerSource::handleInterrupt) both treat that as - * "this source cannot give more" and back off 8 codes. So does this. - * - * Online is 0x0a bit 6: 0x6b plugged, 0x03 or 0x23 not, the same for a host - * port and a charger -- telling those apart takes the D+/D- measurement on - * the PMU's ADC that iBEC makes, which is not here. Until it is, the - * target is 1000 mA (Cascadia's choice: iBEC's for a charger it does not - * know; a Mac's USB-C port gave it with no sag, +752 mA into the battery - * against +253 at 500), and a source that cannot give it sags: the limit - * then stays 8 codes under where it sagged until the cable comes out (a - * cable out and in again within the 1 s poll looks like a sag: 900 mA until - * the next time). More - * has to be asked for by hand: raise max_input_ma, then write - * input_current_limit. - * - * The rules this file keeps: - * - 0x3d is the only register it writes. pmu_chg_write() refuses any - * other. It never reads 0x01..0x09: those clear on read. - * - It is armed by default (module parameter armed=0 turns it off); disarmed - * it writes nothing and logs what it would do. Disarming puts back the - * value it found at probe. Unbinding does not restore 0x3d. - * - Above 45 C on the battery gauge it drops to 100 mA, and stays there - * until below 40 C. Missing/failed temperature also selects this fallback. - * iBEC's charge table ends at 45 C. This is derating, not charge inhibition. + * Register write rules: + * - Only the chip's input-limit register (ilim_reg) and, during ADC + * sampling, its ADC control register (adc.ctl_reg) are ever written. + * - Clear-on-read PMU event registers (0x01..0x09 on D1946, 0x01..0x0a on + * D1974) are never read. */ #include #include @@ -48,59 +45,146 @@ #include #include #include +#include +#include #include #include #include +#include #include #include -#define PMU_STATUS0 0x0a /* bit 6: VBUS there */ -#define PMU_STATUS0_VBUS BIT(6) -#define PMU_INSUSP0 0x1b /* bit 2: input suspend */ -#define PMU_INSUSP1 0x1c /* bit 7: input suspend */ -#define PMU_ILIM 0x3d /* the only register written */ -#define PMU_ICHG 0x3f /* charge current setting */ +/* Dialog D1946 (iPad 2 K93, iPad mini 1 P105) */ +#define D1946_STATUS0 0x0a /* bit 6: VBUS present */ +#define D1946_STATUS0_VBUS BIT(6) +#define D1946_INSUSP0 0x1b /* bit 2: input suspend */ +#define D1946_INSUSP1 0x1c /* bit 7: input suspend */ +#define D1946_ILIM 0x3d +#define D1946_ICHG 0x3f +#define D1946_HARD_MAX_MA 2100 -#define ILIM_MIN_CODE 0x02 /* < 100 mA; also the PMU's own sag value */ -#define ILIM_STEP 8 /* codes per ramp step, 100 mA */ +/* + * Dialog D1974 (iPad 3: J1/J2/J2A). Verified from 13G37 iBEC + * (0xbff0d59c, 0xbff0d704, 0xbff0d05c) and 12H321 AppleD1974PMU: + * - 0x00 low nibble is chip revision (rev 0 caps ilim code at 0xb2). + * - 0x0b bit 3 is VBUS online (0x01..0x0a are clear-on-read events). + * - 0x3e is USB input current limit (drops to 0x02 on VBUS sag / unplug). + * - 0x1f bit 2 is input suspend; 0x40 is charge current setting. + * - 0x44 is 12-bit ADC control (bit 5 = START, bits 4:0 = channel); + * 0x45 low nibble = bits 3:0, 0x46 = bits 11:4; ch 6 = USB DMON, + * mV = 40 + (raw * 5000) / 4096. + */ +#define D1974_CHIP_ID 0x00 +#define D1974_STATUS0 0x0b /* bit 3: VBUS present */ +#define D1974_STATUS0_VBUS BIT(3) +#define D1974_INSUSP0 0x1f /* bit 2: input suspend */ +#define D1974_ILIM 0x3e +#define D1974_ICHG 0x40 +#define D1974_ADC_CTL 0x44 +#define D1974_ADC_START BIT(5) +#define D1974_ADC_CH_MASK 0x1f +#define D1974_ADC_LO 0x45 +#define D1974_ADC_HI 0x46 +#define D1974_ADC_CH_DMON 6 +#define D1974_HARD_MAX_MA 2400 +#define D1974_REV0_MAX_CODE 0xb2 + +#define ILIM_MIN_CODE 0x02 /* ~100 mA; also the PMU's own sag value */ +#define ILIM_STEP 8 /* codes per ramp step (~100 mA) */ #define ILIM_STEP_MS 10 -#define ILIM_HARD_MAX_MA 2100 -#define ILIM_DEFAULT_MA 1000 +#define ILIM_HANDOFF_MA 100 +#define ILIM_FALLBACK_MA 500 +#define ILIM_UNDETECTED_MA 1000 /* no D+/D- wiring: the fixed target */ +#define ILIM_BC12_DCP_MA 1500 -#define POLL_MS 1000 +#define ATTACH_SETTLE_MS 50 +#define DMON_SETTLE_MS 2 +#define DMON_BC12_SETTLE_MS 42 +#define ADC_TIMEOUT_MS 50 +#define DCP_WAIT_MS 1500 +#define POLL_ONLINE_MS 250 +#define POLL_OFFLINE_MS 1000 #define HOT_DECI_C 450 #define COOL_DECI_C 400 +/* Apple BrickID / BC1.2 thresholds (AppleARMPMUCharger / iBEC) */ +#define BRICK_FLOOR_MV 1000 +#define BRICK_BIN0_MAX_MV 2220 +#define BRICK_BIN1_MAX_MV 2890 +#define BC12_PRI_MIN_MV 251 +#define BC12_PRI_MAX_MV 799 +#define SHORTED_DCP_DM_MV 1500 + static bool armed = true; module_param(armed, bool, 0644); -MODULE_PARM_DESC(armed, "write 0x3d (default on; off: log only)"); +MODULE_PARM_DESC(armed, "write PMU USB input current limit (default on; off: log only)"); -static int max_input_ma = ILIM_DEFAULT_MA; +static int max_input_ma; module_param(max_input_ma, int, 0644); -MODULE_PARM_DESC(max_input_ma, "ceiling for input_current_limit, 100..2100 mA (default 1000)"); +MODULE_PARM_DESC(max_input_ma, "optional ceiling in mA (0 = use board DT max, else 100..2400)"); + +struct pmu_chg; + +struct pmu_chg_adc { + u8 ctl_reg; + u8 start_bit; + u8 ch_mask; + u8 lo_reg; + u8 hi_reg; + u8 dmon_ch; + unsigned int mv_offset; + unsigned int mv_fullscale; +}; + +struct pmu_chg_chip { + const char *name; + u8 status0_reg; + u8 vbus_mask; + u8 ilim_reg; + u8 log_regs[3]; + u8 n_log_regs; + bool has_chip_rev; + unsigned int hard_max_ma; + u8 (*ma_to_code)(const struct pmu_chg *c, unsigned int ma); + unsigned int (*code_to_ma)(const struct pmu_chg *c, u8 code); + struct pmu_chg_adc adc; +}; struct pmu_chg { struct device *dev; struct regmap *map; + const struct pmu_chg_chip *chip; + struct phy *dmon_phy; + struct usb_role_switch *role_sw; struct power_supply *psy; struct delayed_work work; - struct mutex lock; /* everything below */ - unsigned int req_ma; /* asked for: default or input_current_limit */ - u8 probe_code; /* 0x3d as iBEC left it */ + struct mutex lock; /* serializes state and PMU/DMON accesses */ + unsigned int board_max_ma; /* DT input-current-limit-microamp / chip max */ + unsigned int source_ma; /* BrickID, BC1.2 DCP, or unidentified fallback */ + unsigned int usb_ma; /* USB host vbus_draw allowance */ + enum power_supply_usb_type usb_type; + unsigned long usb_enable_jiffies; + u8 chip_rev; /* reg 0x00 low nibble (when has_chip_rev) */ + u8 probe_code; /* ilim_reg as iBEC left it */ + u8 cur_code; /* last observed ilim_reg value */ u8 last_written; /* 0 = nothing written while online */ u8 ceiling; /* after a sag, until unplugged; 0xff = none */ bool online; + bool identified; + bool usb_on; + bool usb_enumerated; + bool dcp_checked; bool was_armed; bool hot; u8 logged_code; /* last "would set" logged, to say it once */ }; -/* iBEC's encoding, 0x9ff0c618..0x9ff0c662 */ -static u8 pmu_chg_ma_to_code(unsigned int ma) +/* D1946 iBEC encoding, 0x9ff0c618..0x9ff0c662 */ +static u8 d1946_ma_to_code(const struct pmu_chg *c, unsigned int ma) { unsigned int code; - ma = min_t(unsigned int, ma, ILIM_HARD_MAX_MA); + ma = min_t(unsigned int, ma, D1946_HARD_MAX_MA); if (ma < 100) return ILIM_MIN_CODE; code = (8 * ma - 600) / 100; @@ -115,24 +199,122 @@ static u8 pmu_chg_ma_to_code(unsigned int ma) return code; } -/* The nominal current of a code, for the log only. */ -static unsigned int pmu_chg_code_to_ma(u8 code) +static unsigned int d1946_code_to_ma(const struct pmu_chg *c, u8 code) { return code <= ILIM_MIN_CODE ? 100 : (code * 100 + 600) / 8; } +/* D1974 iBEC encoding, 0xbff0d7a2..0xbff0d836 (13G37) */ +static u8 d1974_ma_to_code(const struct pmu_chg *c, unsigned int ma) +{ + unsigned int code; + + ma = min_t(unsigned int, ma, D1974_HARD_MAX_MA); + if (ma < 100) + return ILIM_MIN_CODE; + code = (8 * ma - 600) / 100; + if (code >= 0xaa) + code -= 5; + else if (code >= 0x72) + code -= 4; + else if (code >= 0x36) + code -= 3; + else if (code >= 0x0e) + code -= 1; + if (code >= 0xb3 && c->chip_rev == 0) + code = D1974_REV0_MAX_CODE; + return code; +} + +/* Inverse of D1974 iBEC offset table for sysfs reporting and logging. */ +static unsigned int d1974_code_to_ma(const struct pmu_chg *c, u8 code) +{ + unsigned int raw = code; + + if (code <= ILIM_MIN_CODE) + return 100; + if (code >= 0xa5) + raw += 5; + else if (code >= 0x6e) + raw += 4; + else if (code >= 0x33) + raw += 3; + else if (code >= 0x0d) + raw += 1; + return (raw * 100 + 600) / 8; +} + +static const struct pmu_chg_chip pmu_d1946_chip = { + .name = "D1946", + .status0_reg = D1946_STATUS0, + .vbus_mask = D1946_STATUS0_VBUS, + .ilim_reg = D1946_ILIM, + .log_regs = { D1946_INSUSP0, D1946_INSUSP1, D1946_ICHG }, + .n_log_regs = 3, + .hard_max_ma = D1946_HARD_MAX_MA, + .ma_to_code = d1946_ma_to_code, + .code_to_ma = d1946_code_to_ma, +}; + +static const struct pmu_chg_chip pmu_d1974_chip = { + .name = "D1974", + .status0_reg = D1974_STATUS0, + .vbus_mask = D1974_STATUS0_VBUS, + .ilim_reg = D1974_ILIM, + .log_regs = { D1974_INSUSP0, D1974_ICHG }, + .n_log_regs = 2, + .has_chip_rev = true, + .hard_max_ma = D1974_HARD_MAX_MA, + .ma_to_code = d1974_ma_to_code, + .code_to_ma = d1974_code_to_ma, + .adc = { + .ctl_reg = D1974_ADC_CTL, + .start_bit = D1974_ADC_START, + .ch_mask = D1974_ADC_CH_MASK, + .lo_reg = D1974_ADC_LO, + .hi_reg = D1974_ADC_HI, + .dmon_ch = D1974_ADC_CH_DMON, + .mv_offset = 40, + .mv_fullscale = 5000, + }, +}; + +static unsigned int pmu_chg_code_to_ma(const struct pmu_chg *c, u8 code) +{ + return c->chip->code_to_ma(c, code); +} + +static unsigned int pmu_chg_max_ma(const struct pmu_chg *c) +{ + int cap = READ_ONCE(max_input_ma); + + if (cap > 0) + return min_t(unsigned int, c->board_max_ma, + clamp(cap, ILIM_HANDOFF_MA, (int)c->chip->hard_max_ma)); + return c->board_max_ma; +} + +static unsigned int pmu_chg_allowed_ma(const struct pmu_chg *c) +{ + if (!c->online) + return 0; + return min(max(c->source_ma, c->usb_ma), pmu_chg_max_ma(c)); +} + static int pmu_chg_write(struct pmu_chg *c, unsigned int reg, u8 val) { int ret; - if (reg != PMU_ILIM) { + if (reg != c->chip->ilim_reg) { dev_err(c->dev, "refusing PMU write 0x%02x = 0x%02x: only 0x%02x is ours\n", - reg, val, PMU_ILIM); + reg, val, c->chip->ilim_reg); return -EPERM; } ret = regmap_write(c->map, reg, val); - if (!ret) + if (!ret) { c->last_written = val; + c->cur_code = val; + } return ret; } @@ -146,52 +328,337 @@ static int pmu_chg_read(struct pmu_chg *c, unsigned int reg, u8 *val) return ret; } +/* + * Single 12-bit conversion on the PMU's ADC channel (e.g. D1974 reg 0x44..0x46, + * iBEC 0xbff0d05c / AppleD1974PMU 0x80f43904). Always clears ctl_reg on exit. + */ +static int pmu_chg_adc_read_mv(struct pmu_chg *c, u8 ch, unsigned int *mv_out) +{ + const struct pmu_chg_adc *adc = &c->chip->adc; + unsigned int v = 0, raw; + u8 lo, hi; + int ret, i; + + if (!adc->ctl_reg) + return -EOPNOTSUPP; + + ret = regmap_write(c->map, adc->ctl_reg, adc->start_bit | (ch & adc->ch_mask)); + if (ret) + return ret; + + for (i = 0; i < ADC_TIMEOUT_MS; i++) { + usleep_range(1000, 1500); + ret = regmap_read(c->map, adc->ctl_reg, &v); + if (ret) + goto clear; + if (!(v & adc->start_bit)) + break; + } + if (v & adc->start_bit) { + ret = -ETIMEDOUT; + goto clear; + } + + ret = pmu_chg_read(c, adc->lo_reg, &lo); + if (ret) + goto clear; + ret = pmu_chg_read(c, adc->hi_reg, &hi); + if (ret) + goto clear; + + raw = ((unsigned int)hi << 4) | (lo & 0x0f); + *mv_out = adc->mv_offset + (raw * adc->mv_fullscale) / 4096; + +clear: + regmap_write(c->map, adc->ctl_reg, 0); + return ret; +} + +/* Whether the board is wired for D+/D- source detection. */ +static bool pmu_chg_can_detect(const struct pmu_chg *c) +{ + return c->dmon_phy && c->chip->adc.ctl_reg; +} + +/* Sample one USB data-monitor mode through the S5L PHY + PMU ADC. */ +static int pmu_chg_measure_dmon(struct pmu_chg *c, + enum apple_s5l_usbphy_dmon_mode mode, + unsigned int settle_ms, + unsigned int *mv_out) +{ + int ret; + + if (!pmu_chg_can_detect(c)) + return -EOPNOTSUPP; + + ret = apple_s5l_usbphy_set_dmon(c->dmon_phy, mode); + if (ret) + return ret; + + msleep(settle_ms); + ret = pmu_chg_adc_read_mv(c, c->chip->adc.dmon_ch, mv_out); + apple_s5l_usbphy_set_dmon(c->dmon_phy, APPLE_S5L_USBPHY_DMON_OFF); + return ret; +} + +static unsigned int pmu_chg_brick_bin(unsigned int mv) +{ + if (mv <= BRICK_BIN0_MAX_MV) + return 0; + if (mv <= BRICK_BIN1_MAX_MV) + return 1; + return 2; +} + +/* + * Apple USB-A BrickID classifier (12H321 AppleARMPMUCharger 0x804c7ba8 + + * 0x804c8014, 13G37 iBEC 0xbff22d84). Returns granted mA, or 0 if no valid + * BrickID divider signature is present. + */ +static unsigned int pmu_chg_classify_brick(unsigned int dp_mv, unsigned int dm_mv, + unsigned int *id_out) +{ + static const unsigned int brick_ma[5] = { 500, 1000, 1000, 2100, 2400 }; + unsigned int ratio, id; + + if (dp_mv < BRICK_FLOOR_MV || dm_mv < BRICK_FLOOR_MV) + return 0; + + ratio = (1000 * dm_mv) / dp_mv; + if (ratio < 500 || ratio > 2000) + return 0; + + if (ratio >= 1291 && ratio <= 1505) + id = 2; + else if (ratio >= 665 && ratio <= 775) + id = 4; + else + id = 1 + pmu_chg_brick_bin(dm_mv) + 3 * pmu_chg_brick_bin(dp_mv); + + *id_out = id; + if (id >= 1 && id <= ARRAY_SIZE(brick_ma)) + return brick_ma[id - 1]; + return 1000; +} + +static void pmu_chg_set_usb(struct pmu_chg *c, bool on) +{ + int ret; + + if (c->role_sw) { + ret = usb_role_switch_set_role(c->role_sw, + on ? USB_ROLE_DEVICE : USB_ROLE_NONE); + if (ret) + dev_warn(c->dev, "cannot set USB role %s: %d\n", + on ? "device" : "none", ret); + c->usb_on = on || ret; + } else { + c->usb_on = on; + } + + if (!c->usb_on) { + c->usb_ma = 0; + c->usb_enumerated = false; + } else { + c->usb_enable_jiffies = jiffies; + } +} + +static void pmu_chg_reset_session(struct pmu_chg *c) +{ + c->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; + c->source_ma = 0; + c->usb_ma = 0; + c->usb_enumerated = false; + c->dcp_checked = false; + c->identified = false; + c->ceiling = 0xff; + c->last_written = 0; + c->logged_code = 0; +} + static void pmu_chg_log_vbus(struct pmu_chg *c, u8 ilim, bool is_armed) { - static const unsigned int regs[] = { PMU_INSUSP0, PMU_INSUSP1, PMU_ICHG }; unsigned int i; int ret; u8 val; - dev_info(c->dev, "VBUS on: 0x3d = 0x%02x, %s\n", ilim, + dev_info(c->dev, "VBUS on: 0x%02x = 0x%02x, %s\n", c->chip->ilim_reg, ilim, is_armed ? "armed" : "disarmed"); - for (i = 0; i < ARRAY_SIZE(regs); i++) { - ret = pmu_chg_read(c, regs[i], &val); + for (i = 0; i < c->chip->n_log_regs; i++) { + unsigned int reg = c->chip->log_regs[i]; + + ret = pmu_chg_read(c, reg, &val); if (ret) dev_warn(c->dev, "VBUS on: failed to read 0x%02x: %d\n", - regs[i], ret); + reg, ret); else - dev_info(c->dev, "VBUS on: 0x%02x = 0x%02x\n", regs[i], val); + dev_info(c->dev, "VBUS on: 0x%02x = 0x%02x\n", reg, val); } } +/* + * Identify the newly attached power source with the input limit parked at + * 100 mA and the USB gadget pull-up held off. Caller holds c->lock. + */ +static void pmu_chg_identify(struct pmu_chg *c, bool is_armed) +{ + unsigned int dp_mv = 0, dm_mv = 0, pri_mv = 0, brick_id = 0, brick_ma; + u8 st = 0; + int ret; + + pmu_chg_reset_session(c); + + /* Start every attachment conservative at 100 mA. */ + if (is_armed && c->cur_code > ILIM_MIN_CODE) + pmu_chg_write(c, c->chip->ilim_reg, ILIM_MIN_CODE); + + /* + * Hold USB pull-up off while measuring D+/D- so the 1.5 kOhm D+ + * pull-up does not distort Apple BrickID voltage dividers. + */ + if (pmu_chg_can_detect(c)) { + pmu_chg_set_usb(c, false); + msleep(ATTACH_SETTLE_MS); + + if (pmu_chg_read(c, c->chip->status0_reg, &st) || + !(st & c->chip->vbus_mask)) { + c->online = false; + return; + } + + ret = pmu_chg_measure_dmon(c, APPLE_S5L_USBPHY_DMON_DM, + DMON_SETTLE_MS, &dm_mv); + if (!ret) + ret = pmu_chg_measure_dmon(c, APPLE_S5L_USBPHY_DMON_DP, + DMON_SETTLE_MS, &dp_mv); + if (ret) { + dev_warn(c->dev, "D+/D- measurement failed: %d\n", ret); + } else { + brick_ma = pmu_chg_classify_brick(dp_mv, dm_mv, &brick_id); + if (brick_ma) { + c->usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID; + c->source_ma = min(brick_ma, pmu_chg_max_ma(c)); + c->identified = true; + dev_info(c->dev, + "source: Apple BrickID %u (D+ %u mV, D- %u mV), %u mA\n", + brick_id, dp_mv, dm_mv, c->source_ma); + return; + } + + /* Hardware BC1.2 primary detection on phy-version >= 4 */ + ret = pmu_chg_measure_dmon(c, APPLE_S5L_USBPHY_DMON_BC12_PRIMARY, + DMON_BC12_SETTLE_MS, &pri_mv); + if (!ret && pri_mv >= BC12_PRI_MIN_MV && pri_mv <= BC12_PRI_MAX_MV) { + c->usb_type = POWER_SUPPLY_USB_TYPE_DCP; + c->source_ma = min_t(unsigned int, ILIM_BC12_DCP_MA, + pmu_chg_max_ma(c)); + c->identified = true; + pmu_chg_set_usb(c, true); + dev_info(c->dev, + "source: BC1.2 charging port (primary %u mV), %u mA\n", + pri_mv, c->source_ma); + return; + } + + dev_info(c->dev, + "source: no BrickID (D+ %u mV, D- %u mV); awaiting USB host\n", + dp_mv, dm_mv); + } + } + + /* No BrickID or hardware BC1.2: release USB so a host can enumerate. */ + pmu_chg_set_usb(c, true); + c->identified = true; + + /* + * Without D+/D- wiring nothing here can tell a charger from a host + * port, and the 1.5 s check below would end at the 500 mA fallback on + * every source. Keep the fixed target such boards always had; a source + * that cannot give it sags and gets the usual backoff. + */ + if (!pmu_chg_can_detect(c)) { + c->source_ma = min_t(unsigned int, ILIM_UNDETECTED_MA, + pmu_chg_max_ma(c)); + dev_info(c->dev, "source: not detectable on this board, %u mA\n", + c->source_ma); + } +} + +/* + * After DCP_WAIT_MS with no USB host bus reset or configuration, passively + * check whether the gadget's 1.5 kOhm D+ pull-up has pulled D- high (shorted + * D+/D- BC1.2 DCP charger) or left D- low (unidentified wall charger). + * Only ever runs when !c->usb_enumerated, so active USB sessions are untouched. + */ +static bool pmu_chg_check_no_host(struct pmu_chg *c) +{ + unsigned int dm_mv = 0; + int ret; + + if (!c->online || !c->identified || c->usb_enumerated || + c->source_ma || c->dcp_checked) + return false; + + if (time_before(jiffies, c->usb_enable_jiffies + msecs_to_jiffies(DCP_WAIT_MS))) + return false; + + c->dcp_checked = true; + + if (pmu_chg_can_detect(c)) { + ret = pmu_chg_measure_dmon(c, APPLE_S5L_USBPHY_DMON_DM, + DMON_SETTLE_MS, &dm_mv); + if (!ret && dm_mv >= SHORTED_DCP_DM_MV) { + c->usb_type = POWER_SUPPLY_USB_TYPE_DCP; + c->source_ma = min_t(unsigned int, ILIM_BC12_DCP_MA, + pmu_chg_max_ma(c)); + pmu_chg_set_usb(c, false); + dev_info(c->dev, + "source: BC1.2 DCP (shorted D+/D-, D- %u mV), %u mA\n", + dm_mv, c->source_ma); + return true; + } + dev_info(c->dev, + "source: unidentified (no USB host, D- %u mV), %u mA fallback\n", + dm_mv, ILIM_FALLBACK_MA); + } else { + dev_info(c->dev, "source: unidentified, %u mA fallback\n", + ILIM_FALLBACK_MA); + } + + c->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; + c->source_ma = min_t(unsigned int, ILIM_FALLBACK_MA, pmu_chg_max_ma(c)); + return true; +} + static void pmu_chg_sagged(struct pmu_chg *c, u8 at) { c->ceiling = max_t(int, ILIM_MIN_CODE, at - ILIM_STEP); dev_warn(c->dev, "VBUS sagged at 0x%02x: limiting USB input current to %u mA until unplugged\n", - at, pmu_chg_code_to_ma(c->ceiling)); + at, pmu_chg_code_to_ma(c, c->ceiling)); } -/* Ramp 0x3d from cur up to target, 8 codes every 10 ms, watching for a sag. */ +/* Ramp ilim_reg from cur up to target, 8 codes every 10 ms, watching for a sag. */ static void pmu_chg_ramp(struct pmu_chg *c, u8 cur, u8 target) { while (cur < target) { u8 next = min_t(int, cur + ILIM_STEP, target), rb; - if (pmu_chg_write(c, PMU_ILIM, next)) + if (pmu_chg_write(c, c->chip->ilim_reg, next)) return; msleep(ILIM_STEP_MS); - if (pmu_chg_read(c, PMU_ILIM, &rb)) + if (pmu_chg_read(c, c->chip->ilim_reg, &rb)) return; + c->cur_code = rb; if (rb == ILIM_MIN_CODE && next > ILIM_MIN_CODE) { pmu_chg_sagged(c, next); if (c->ceiling < next) - pmu_chg_write(c, PMU_ILIM, c->ceiling); + pmu_chg_write(c, c->chip->ilim_reg, c->ceiling); return; } if (rb != next) { - dev_warn(c->dev, "0x3d reads 0x%02x after writing 0x%02x; stopping the ramp\n", - rb, next); + dev_warn(c->dev, "0x%02x reads 0x%02x after writing 0x%02x; stopping the ramp\n", + c->chip->ilim_reg, rb, next); return; } cur = rb; @@ -229,10 +696,13 @@ static bool pmu_chg_battery_hot(struct pmu_chg *c) static u8 pmu_chg_target(struct pmu_chg *c) { - unsigned int ma = min_t(unsigned int, c->req_ma, - clamp(READ_ONCE(max_input_ma), 100, ILIM_HARD_MAX_MA)); - u8 code = pmu_chg_ma_to_code(ma); + unsigned int ma = pmu_chg_allowed_ma(c); + u8 code; + + if (ma <= ILIM_HANDOFF_MA) + return ILIM_MIN_CODE; + code = c->chip->ma_to_code(c, ma); if (c->ceiling != 0xff) code = min(code, c->ceiling); if (pmu_chg_battery_hot(c)) @@ -244,60 +714,138 @@ static void pmu_chg_work(struct work_struct *w) { struct pmu_chg *c = container_of(to_delayed_work(w), struct pmu_chg, work); bool is_armed = READ_ONCE(armed), online, changed = false; - u8 st, cur, target; + unsigned int poll_ms = POLL_OFFLINE_MS; + u8 st, cur, target, old_code; mutex_lock(&c->lock); + old_code = c->cur_code; + if (c->was_armed && !is_armed) { - if (pmu_chg_write(c, PMU_ILIM, c->probe_code)) - goto out; /* Leave restoration pending for the next poll. */ - dev_info(c->dev, "disarmed: 0x3d back to 0x%02x\n", c->probe_code); + if (pmu_chg_write(c, c->chip->ilim_reg, ILIM_MIN_CODE)) + goto out; + dev_info(c->dev, "disarmed: 0x%02x back to 0x%02x (%u mA)\n", + c->chip->ilim_reg, ILIM_MIN_CODE, + pmu_chg_code_to_ma(c, ILIM_MIN_CODE)); c->last_written = 0; c->logged_code = 0; c->was_armed = false; + changed = true; goto out; } - if (pmu_chg_read(c, PMU_STATUS0, &st) || pmu_chg_read(c, PMU_ILIM, &cur)) + + if (pmu_chg_read(c, c->chip->status0_reg, &st) || + pmu_chg_read(c, c->chip->ilim_reg, &cur)) goto out; - online = st & PMU_STATUS0_VBUS; - changed = online != c->online; - c->online = online; - if (changed) { - /* a source's sag is that source's: a new cable starts afresh */ - c->ceiling = 0xff; + c->cur_code = cur; + + online = !!(st & c->chip->vbus_mask); + if (online != c->online) { + c->online = online; + changed = true; if (online) { pmu_chg_log_vbus(c, cur, is_armed); - c->last_written = 0; /* a new session: a low 0x3d now is not a sag */ + pmu_chg_identify(c, is_armed); + if (!c->online) + goto out; + if (pmu_chg_read(c, c->chip->ilim_reg, &cur)) + goto out; + c->cur_code = cur; + } else { + dev_info(c->dev, "VBUS off: dropping 0x%02x to 0x%02x\n", + c->chip->ilim_reg, ILIM_MIN_CODE); + if (is_armed && cur > ILIM_MIN_CODE) + pmu_chg_write(c, c->chip->ilim_reg, ILIM_MIN_CODE); + pmu_chg_reset_session(c); + pmu_chg_set_usb(c, false); + goto out; } } if (!online) goto out; + poll_ms = POLL_ONLINE_MS; + + /* First work run after probe with VBUS already present. */ + if (!c->identified) { + pmu_chg_identify(c, is_armed); + changed = true; + if (!c->online) + goto out; + if (pmu_chg_read(c, c->chip->ilim_reg, &cur)) + goto out; + c->cur_code = cur; + } + + /* + * Steady state: if the PMU dropped ilim back to 0x02, it was either a + * VBUS sag on the current charger or a fast unplug/replug within one + * poll interval. + * + * During a USB host session (a computer or hub port) it is a sag: an + * unplug ends the session through vbus_draw(0) before this runs. Step + * the host-port limit down and keep it there until unplug; never drop + * the USB link for it, USB serial and SSH run over it. A board that + * cannot detect sources has nothing to re-identify either. + */ + if (is_armed && cur == ILIM_MIN_CODE && c->last_written > ILIM_MIN_CODE && + (c->usb_enumerated || !pmu_chg_can_detect(c))) { + pmu_chg_sagged(c, c->last_written); + c->last_written = ILIM_MIN_CODE; + changed = true; + } else if (is_armed && cur == ILIM_MIN_CODE && + c->last_written > ILIM_MIN_CODE) { + /* + * A dedicated source: re-identify so a stale grant never + * carries across a swap; if the same source returns, apply + * sag backoff. + */ + enum power_supply_usb_type prev_type = c->usb_type; + unsigned int prev_source = c->source_ma; + u8 sagged_at = c->last_written; + + dev_info(c->dev, "0x%02x dropped to 0x02 from 0x%02x; re-identifying\n", + c->chip->ilim_reg, sagged_at); + pmu_chg_identify(c, is_armed); + changed = true; + if (!c->online) + goto out; + if (c->usb_type == prev_type && c->source_ma == prev_source && + prev_source > 0) + pmu_chg_sagged(c, sagged_at); + if (pmu_chg_read(c, c->chip->ilim_reg, &cur)) + goto out; + c->cur_code = cur; + } + + if (pmu_chg_check_no_host(c)) + changed = true; + target = pmu_chg_target(c); if (!is_armed) { if (cur != target && target != c->logged_code) { - dev_info(c->dev, "disarmed: would set 0x3d 0x%02x -> 0x%02x (%u mA)\n", - cur, target, pmu_chg_code_to_ma(target)); + dev_info(c->dev, "disarmed: would set 0x%02x 0x%02x -> 0x%02x (%u mA)\n", + c->chip->ilim_reg, cur, target, + pmu_chg_code_to_ma(c, target)); c->logged_code = target; } goto out; } c->was_armed = true; - /* steady state: the PMU dropped what we set, so the source sagged */ - if (cur == ILIM_MIN_CODE && c->last_written > ILIM_MIN_CODE) { - pmu_chg_sagged(c, c->last_written); - target = min(target, c->ceiling); - } if (cur < target) pmu_chg_ramp(c, cur, target); else if (cur > target) - pmu_chg_write(c, PMU_ILIM, target); + pmu_chg_write(c, c->chip->ilim_reg, target); + + if (c->cur_code != old_code) + changed = true; + out: mutex_unlock(&c->lock); if (changed) power_supply_changed(c->psy); - schedule_delayed_work(&c->work, msecs_to_jiffies(POLL_MS)); + schedule_delayed_work(&c->work, msecs_to_jiffies(poll_ms)); } static int pmu_chg_get(struct power_supply *psy, enum power_supply_property psp, @@ -305,43 +853,83 @@ static int pmu_chg_get(struct power_supply *psy, enum power_supply_property psp, { struct pmu_chg *c = power_supply_get_drvdata(psy); + mutex_lock(&c->lock); switch (psp) { case POWER_SUPPLY_PROP_ONLINE: val->intval = c->online; - return 0; + break; + case POWER_SUPPLY_PROP_USB_TYPE: + val->intval = c->online ? c->usb_type : POWER_SUPPLY_USB_TYPE_UNKNOWN; + break; + case POWER_SUPPLY_PROP_CURRENT_MAX: + val->intval = pmu_chg_allowed_ma(c) * 1000; + break; case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: - val->intval = c->req_ma * 1000; - return 0; + val->intval = pmu_chg_code_to_ma(c, c->cur_code) * 1000; + break; default: + mutex_unlock(&c->lock); return -EINVAL; } + mutex_unlock(&c->lock); + return 0; } +/* + * In-kernel USB gadget vbus_draw callback from dwc2 (via usb-psy-name): + * receives 0 on disconnect, 100 mA on reset/unconfigured, 500 mA on + * SET_CONFIGURATION, 2 mA on suspend, or 500 + extra on an Apple host's + * 0x40 vendor grant. Not writable from userspace. + */ static int pmu_chg_set(struct power_supply *psy, enum power_supply_property psp, const union power_supply_propval *val) { struct pmu_chg *c = power_supply_get_drvdata(psy); - int ma = val->intval / 1000; + unsigned int report_ma, accept_ma, old_ma; + bool changed = false; if (psp != POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT) return -EINVAL; - if (ma < 100 || ma > clamp(READ_ONCE(max_input_ma), 100, ILIM_HARD_MAX_MA)) - return -ERANGE; + mutex_lock(&c->lock); - c->req_ma = ma; - c->logged_code = 0; + old_ma = c->usb_ma; + report_ma = max(val->intval, 0) / 1000; + accept_ma = c->online && c->usb_on ? min(report_ma, pmu_chg_max_ma(c)) : 0; + + if (c->online && c->usb_on && report_ma > 0) { + c->usb_enumerated = true; + c->dcp_checked = true; + if (c->usb_type == POWER_SUPPLY_USB_TYPE_UNKNOWN) { + c->usb_type = POWER_SUPPLY_USB_TYPE_SDP; + /* Undetectable boards keep their fixed target. */ + if (pmu_chg_can_detect(c)) + c->source_ma = 0; + changed = true; + } + } else if (report_ma == 0) { + c->usb_enumerated = false; + } + + c->usb_ma = accept_ma; + if (c->usb_ma != old_ma) { + dev_info(c->dev, "USB host allowance: %u mA (reported %u mA)\n", + c->usb_ma, report_ma); + c->logged_code = 0; + changed = true; + } mutex_unlock(&c->lock); - mod_delayed_work(system_wq, &c->work, 0); - return 0; -} -static int pmu_chg_writeable(struct power_supply *psy, enum power_supply_property psp) -{ - return psp == POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT; + if (changed) { + power_supply_changed(psy); + mod_delayed_work(system_wq, &c->work, 0); + } + return 0; } static enum power_supply_property pmu_chg_props[] = { POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_USB_TYPE, + POWER_SUPPLY_PROP_CURRENT_MAX, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, }; @@ -350,35 +938,77 @@ static char *pmu_chg_supplied_to[] = { "battery" }; static const struct power_supply_desc pmu_chg_desc = { .name = "apple-pmu-usb", .type = POWER_SUPPLY_TYPE_USB, + .usb_types = BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN) | + BIT(POWER_SUPPLY_USB_TYPE_SDP) | + BIT(POWER_SUPPLY_USB_TYPE_DCP) | + BIT(POWER_SUPPLY_USB_TYPE_CDP) | + BIT(POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID), .properties = pmu_chg_props, .num_properties = ARRAY_SIZE(pmu_chg_props), .get_property = pmu_chg_get, .set_property = pmu_chg_set, - .property_is_writeable = pmu_chg_writeable, }; +static void pmu_chg_put_role_switch(void *data) +{ + usb_role_switch_put(data); +} + static int pmu_chg_probe(struct platform_device *pdev) { struct apple_pmu_i2c *pmu = dev_get_drvdata(pdev->dev.parent); + const struct pmu_chg_chip *chip = of_device_get_match_data(&pdev->dev); struct power_supply_config cfg = {}; struct pmu_chg *c; - u8 st; + u32 dt_max_ua; + u8 st, id = 0; int ret; + if (!chip) + return -EINVAL; + c = devm_kzalloc(&pdev->dev, sizeof(*c), GFP_KERNEL); if (!c) return -ENOMEM; c->dev = &pdev->dev; c->map = pmu->regmap; - c->req_ma = ILIM_DEFAULT_MA; + c->chip = chip; + c->board_max_ma = chip->hard_max_ma; + if (!of_property_read_u32(pdev->dev.of_node, + "input-current-limit-microamp", &dt_max_ua)) + c->board_max_ma = clamp_t(unsigned int, dt_max_ua / 1000, + ILIM_HANDOFF_MA, chip->hard_max_ma); c->ceiling = 0xff; + c->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; mutex_init(&c->lock); - if (pmu_chg_read(c, PMU_ILIM, &c->probe_code) || pmu_chg_read(c, PMU_STATUS0, &st)) + c->dmon_phy = devm_phy_optional_get(&pdev->dev, "dmon"); + if (IS_ERR(c->dmon_phy)) + return dev_err_probe(&pdev->dev, PTR_ERR(c->dmon_phy), + "USB data monitor PHY not ready\n"); + + c->role_sw = usb_role_switch_get(&pdev->dev); + if (IS_ERR(c->role_sw)) + return dev_err_probe(&pdev->dev, PTR_ERR(c->role_sw), + "USB role switch not ready\n"); + if (c->role_sw) { + ret = devm_add_action_or_reset(&pdev->dev, pmu_chg_put_role_switch, + c->role_sw); + if (ret) + return ret; + } + + if (pmu_chg_read(c, chip->ilim_reg, &c->probe_code) || + pmu_chg_read(c, chip->status0_reg, &st)) return -EIO; - c->online = st & PMU_STATUS0_VBUS; + c->cur_code = c->probe_code; + if (chip->has_chip_rev && !pmu_chg_read(c, D1974_CHIP_ID, &id)) + c->chip_rev = id & 0x0f; + c->online = !!(st & chip->vbus_mask); if (c->online) pmu_chg_log_vbus(c, c->probe_code, READ_ONCE(armed)); + else + pmu_chg_set_usb(c, false); cfg.drv_data = c; cfg.of_node = pdev->dev.of_node; @@ -392,21 +1022,42 @@ static int pmu_chg_probe(struct platform_device *pdev) ret = devm_delayed_work_autocancel(&pdev->dev, &c->work, pmu_chg_work); if (ret) return ret; - schedule_delayed_work(&c->work, msecs_to_jiffies(POLL_MS)); - dev_info(&pdev->dev, "0x3d = 0x%02x (%u mA) at probe, VBUS %s, %s\n", - c->probe_code, pmu_chg_code_to_ma(c->probe_code), - c->online ? "on" : "off", armed ? "armed" : "disarmed (log only)"); + /* Identify immediately when VBUS is already present at probe. */ + schedule_delayed_work(&c->work, c->online ? 0 : msecs_to_jiffies(POLL_OFFLINE_MS)); + + dev_info(&pdev->dev, + "%s: 0x%02x = 0x%02x (%u mA) at probe, status0 0x%02x (VBUS %s), max %u mA, %s\n", + chip->name, chip->ilim_reg, c->probe_code, + pmu_chg_code_to_ma(c, c->probe_code), st, + c->online ? "on" : "off", c->board_max_ma, + armed ? "armed" : "disarmed (log only)"); return 0; } +static void pmu_chg_shutdown(struct platform_device *pdev) +{ + struct pmu_chg *c = platform_get_drvdata(pdev); + + if (!c) + return; + cancel_delayed_work_sync(&c->work); + mutex_lock(&c->lock); + if (READ_ONCE(armed)) + pmu_chg_write(c, c->chip->ilim_reg, ILIM_MIN_CODE); + pmu_chg_set_usb(c, true); + mutex_unlock(&c->lock); +} + static const struct of_device_id pmu_chg_of_match[] = { - { .compatible = "apple,pmu-d1946-charger" }, + { .compatible = "apple,pmu-d1946-charger", .data = &pmu_d1946_chip }, + { .compatible = "apple,pmu-d1974-charger", .data = &pmu_d1974_chip }, { } }; MODULE_DEVICE_TABLE(of, pmu_chg_of_match); static struct platform_driver pmu_chg_driver = { .probe = pmu_chg_probe, + .shutdown = pmu_chg_shutdown, .driver = { .name = "apple-pmu-charger", .of_match_table = pmu_chg_of_match, @@ -414,5 +1065,5 @@ static struct platform_driver pmu_chg_driver = { }; module_platform_driver(pmu_chg_driver); -MODULE_DESCRIPTION("D1946 PMU USB input current limit (iPad mini 1)"); +MODULE_DESCRIPTION("Apple Dialog PMU adaptive USB charger (D1946/D1974)"); MODULE_LICENSE("GPL"); diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h index 2bd74f3033ed08..d7f9c94c251b39 100644 --- a/drivers/usb/dwc2/core.h +++ b/drivers/usb/dwc2/core.h @@ -9,6 +9,7 @@ #define __DWC2_CORE_H__ #include +#include #include #include #include @@ -864,6 +865,12 @@ struct dwc2_hregs_backup { * @role_sw: usb_role_switch handle * @role_sw_default_mode: default operation mode of controller while usb role * is USB_ROLE_NONE + * @role_sw_lock: Serializes role changes against the fallback + * @role_sw_fallback: Connects a peripheral-only controller as a device if + * nothing sets a role in time + * @role_sw_driven: A role has been set through the role switch + * @role_sw_hold: A peripheral-only controller without a default mode + * holds its pull-up off: no role, or no device role, set * @hcd_enabled: Host mode sub-driver initialization indicator. * @gadget_enabled: Peripheral mode sub-driver initialization indicator. * @ll_hw_enabled: Status of low-level hardware resources. @@ -1049,6 +1056,12 @@ struct dwc2_hregs_backup { * @new_connection: Used in host mode. True if there are new connected * device * @enabled: Indicates the enabling state of controller + * @usb_psy_name: Name of the power supply told what the host allows + * the device to draw (vbus_draw), or NULL + * @vbus_draw_work: Work passing @vbus_draw_ma to that power supply + * @vbus_draw_ma: Current the gadget may draw, from vbus_draw + * @vbus_draw_pending: A new vbus_draw report must reach the supply + * @usb_psy_nb: Re-report the current allowance when the supply changes * */ struct dwc2_hsotg { @@ -1062,6 +1075,10 @@ struct dwc2_hsotg { enum usb_dr_mode dr_mode; struct usb_role_switch *role_sw; enum usb_dr_mode role_sw_default_mode; + struct mutex role_sw_lock; + struct delayed_work role_sw_fallback; + bool role_sw_driven; + bool role_sw_hold; unsigned int hcd_enabled:1; unsigned int gadget_enabled:1; unsigned int ll_hw_enabled:1; @@ -1219,6 +1236,11 @@ struct dwc2_hsotg { unsigned int remote_wakeup_allowed:1; struct dwc2_hsotg_ep *eps_in[MAX_EPS_CHANNELS]; struct dwc2_hsotg_ep *eps_out[MAX_EPS_CHANNELS]; + const char *usb_psy_name; + struct work_struct vbus_draw_work; + struct notifier_block usb_psy_nb; + unsigned int vbus_draw_ma; + bool vbus_draw_pending; #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */ }; diff --git a/drivers/usb/dwc2/drd.c b/drivers/usb/dwc2/drd.c index 1ad8fa3f862a15..ee8c1f562bdbce 100644 --- a/drivers/usb/dwc2/drd.c +++ b/drivers/usb/dwc2/drd.c @@ -13,6 +13,14 @@ #include #include "core.h" +/* + * A peripheral-only controller whose role switch has no default mode stays + * disconnected until a connector driver sets a role. If none has within this + * time (missing, deferred or stuck), connect as a device anyway so that the + * gadget stays reachable. + */ +#define DWC2_ROLE_SW_FALLBACK_MS 1000 + #define dwc2_ovr_gotgctl(gotgctl) \ ((gotgctl) |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN | \ GOTGCTL_DBNCE_FLTR_BYPASS) @@ -83,12 +91,56 @@ static int dwc2_ovr_bvalid(struct dwc2_hsotg *hsotg, bool valid) return 0; } -static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role) +/* + * A peripheral-only controller without a default mode: no session override. + * Forcing B-valid low on the Apple A10's controller left an interrupt + * pending that nothing cleared, and CPU 0 never left its handler. A role is + * the pull-up instead: held off (DCTL.SFTDISCON) until the device role. + */ +static bool dwc2_drd_soft_role(struct dwc2_hsotg *hsotg) +{ + return hsotg->dr_mode == USB_DR_MODE_PERIPHERAL && + hsotg->role_sw_default_mode == USB_DR_MODE_UNKNOWN; +} + +static int dwc2_drd_set_soft_role(struct dwc2_hsotg *hsotg, enum usb_role role) +{ + unsigned long flags; + bool was_held; + + if (role == USB_ROLE_HOST) + return -EINVAL; + + spin_lock_irqsave(&hsotg->lock, flags); + was_held = hsotg->role_sw_hold; + hsotg->role_sw_hold = role != USB_ROLE_DEVICE; + if (dwc2_is_device_enabled(hsotg)) { + if (hsotg->role_sw_hold && !was_held) { + /* Stop the old session with its pull-up still on. */ + dwc2_hsotg_disconnect(hsotg); + dwc2_hsotg_core_disconnect(hsotg); + } else if (!hsotg->role_sw_hold && was_held) { + dwc2_hsotg_core_init_disconnected(hsotg, false); + dwc2_enable_acg(hsotg); + dwc2_hsotg_core_connect(hsotg); + } else if (!hsotg->role_sw_hold) { + dwc2_hsotg_core_connect(hsotg); + } + } + spin_unlock_irqrestore(&hsotg->lock, flags); + + dev_dbg(hsotg->dev, "pull-up %s\n", hsotg->role_sw_hold ? "held off" : "released"); + return 0; +} + +static int dwc2_drd_set_role(struct dwc2_hsotg *hsotg, enum usb_role role) { - struct dwc2_hsotg *hsotg = usb_role_switch_get_drvdata(sw); unsigned long flags; int already = 0; + if (dwc2_drd_soft_role(hsotg)) + return dwc2_drd_set_soft_role(hsotg, role); + /* Skip session not in line with dr_mode */ if ((role == USB_ROLE_DEVICE && hsotg->dr_mode == USB_DR_MODE_HOST) || (role == USB_ROLE_HOST && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)) @@ -170,6 +222,33 @@ static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role) return 0; } +static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role) +{ + struct dwc2_hsotg *hsotg = usb_role_switch_get_drvdata(sw); + int ret; + + mutex_lock(&hsotg->role_sw_lock); + hsotg->role_sw_driven = true; + ret = dwc2_drd_set_role(hsotg, role); + mutex_unlock(&hsotg->role_sw_lock); + return ret; +} + +static void dwc2_drd_role_sw_fallback(struct work_struct *work) +{ + struct dwc2_hsotg *hsotg = container_of(to_delayed_work(work), + struct dwc2_hsotg, + role_sw_fallback); + + mutex_lock(&hsotg->role_sw_lock); + if (!hsotg->role_sw_driven) { + dev_warn(hsotg->dev, "no role set within %u ms, connecting as a device\n", + DWC2_ROLE_SW_FALLBACK_MS); + dwc2_drd_set_role(hsotg, USB_ROLE_DEVICE); + } + mutex_unlock(&hsotg->role_sw_lock); +} + int dwc2_drd_init(struct dwc2_hsotg *hsotg) { struct usb_role_switch_desc role_sw_desc = {0}; @@ -180,6 +259,8 @@ int dwc2_drd_init(struct dwc2_hsotg *hsotg) return 0; hsotg->role_sw_default_mode = usb_get_role_switch_default_mode(hsotg->dev); + mutex_init(&hsotg->role_sw_lock); + INIT_DELAYED_WORK(&hsotg->role_sw_fallback, dwc2_drd_role_sw_fallback); role_sw_desc.driver_data = hsotg; role_sw_desc.fwnode = dev_fwnode(hsotg->dev); role_sw_desc.set = dwc2_drd_role_sw_set; @@ -195,8 +276,18 @@ int dwc2_drd_init(struct dwc2_hsotg *hsotg) hsotg->role_sw = role_sw; - /* Enable override and initialize values */ - dwc2_ovr_init(hsotg); + if (dwc2_drd_soft_role(hsotg)) { + /* No session yet: the pull-up stays off until the device role */ + hsotg->role_sw_hold = true; + } else { + /* Enable override and initialize values */ + dwc2_ovr_init(hsotg); + } + + if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL && + hsotg->role_sw_default_mode == USB_DR_MODE_UNKNOWN) + schedule_delayed_work(&hsotg->role_sw_fallback, + msecs_to_jiffies(DWC2_ROLE_SW_FALLBACK_MS)); return 0; } @@ -254,6 +345,8 @@ void dwc2_drd_resume(struct dwc2_hsotg *hsotg) void dwc2_drd_exit(struct dwc2_hsotg *hsotg) { - if (hsotg->role_sw) + if (hsotg->role_sw) { + cancel_delayed_work_sync(&hsotg->role_sw_fallback); usb_role_switch_unregister(hsotg->role_sw); + } } diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c index f3a0a0b781c1a6..d83a08181669fc 100644 --- a/drivers/usb/dwc2/gadget.c +++ b/drivers/usb/dwc2/gadget.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include @@ -3612,6 +3614,9 @@ void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg) void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) { + /* A held soft role switch keeps the pull-up off until USB_ROLE_DEVICE */ + if (hsotg->role_sw_hold) + return; /* remove the soft-disconnect and let's go */ if (!hsotg->role_sw || (dwc2_readl(hsotg, GOTGCTL) & GOTGCTL_BSESVLD)) dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON); @@ -4762,7 +4767,9 @@ static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on) dwc2_hsotg_core_init_disconnected(hsotg, false); /* Enable ACG feature in device mode,if supported */ dwc2_enable_acg(hsotg); - dwc2_hsotg_core_connect(hsotg); + /* A held role switch connects it later (drd.c) */ + if (!hsotg->role_sw_hold) + dwc2_hsotg_core_connect(hsotg); } else { dwc2_hsotg_core_disconnect(hsotg); dwc2_hsotg_disconnect(hsotg); @@ -4819,13 +4826,74 @@ static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active) * * Report how much power the device may consume to the phy. */ +static void dwc2_hsotg_vbus_draw_work(struct work_struct *work) +{ + struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg, + vbus_draw_work); + union power_supply_propval val, allowed; + struct power_supply *psy; + bool pending; + int ret; + + psy = power_supply_get_by_name(hsotg->usb_psy_name); + if (!psy) + return; + pending = xchg(&hsotg->vbus_draw_pending, false); + val.intval = READ_ONCE(hsotg->vbus_draw_ma) * 1000; + if (!pending && + !power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, &allowed) && + allowed.intval == val.intval) + goto put; + ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, + &val); + if (ret) + dev_dbg(hsotg->dev, "setting VBUS draw (%d uA) failed: %d\n", + val.intval, ret); +put: + power_supply_put(psy); +} + +/* + * A charger can invalidate a report on a source event without ending the USB + * session. Send the current allowance again, not a saved Apple grant. The + * composite driver replaces it on reset, disconnect and suspend. Compare the + * allowance, not the ramped input limit, to avoid a notification feedback loop. + * The notifier must not call into the supply: queue the work that may sleep. + */ +static int dwc2_hsotg_psy_changed(struct notifier_block *nb, + unsigned long event, void *data) +{ + struct dwc2_hsotg *hsotg = container_of(nb, struct dwc2_hsotg, usb_psy_nb); + struct power_supply *psy = data; + + if (event == PSY_EVENT_PROP_CHANGED && + !strcmp(psy->desc->name, hsotg->usb_psy_name)) + schedule_work(&hsotg->vbus_draw_work); + return NOTIFY_OK; +} + +static void dwc2_hsotg_psy_unregister(void *data) +{ + struct dwc2_hsotg *hsotg = data; + + power_supply_unreg_notifier(&hsotg->usb_psy_nb); + cancel_work_sync(&hsotg->vbus_draw_work); +} + static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA) { struct dwc2_hsotg *hsotg = to_hsotg(gadget); - if (IS_ERR_OR_NULL(hsotg->uphy)) + if (!IS_ERR_OR_NULL(hsotg->uphy)) + return usb_phy_set_power(hsotg->uphy, mA); + if (!hsotg->usb_psy_name) return -ENOTSUPP; - return usb_phy_set_power(hsotg->uphy, mA); + + /* Called in atomic context; the power supply may sleep. */ + WRITE_ONCE(hsotg->vbus_draw_ma, mA); + WRITE_ONCE(hsotg->vbus_draw_pending, true); + schedule_work(&hsotg->vbus_draw_work); + return 0; } static void dwc2_gadget_set_speed(struct usb_gadget *g, enum usb_device_speed speed) @@ -5075,6 +5143,9 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg) hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; hsotg->gadget.name = dev_name(dev); + if (!device_property_read_string(dev, "usb-psy-name", + &hsotg->usb_psy_name)) + INIT_WORK(&hsotg->vbus_draw_work, dwc2_hsotg_vbus_draw_work); hsotg->gadget.otg_caps = &hsotg->params.otg_caps; hsotg->remote_wakeup_allowed = 0; @@ -5148,6 +5219,24 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg) dwc2_hsotg_dump(hsotg); + if (hsotg->usb_psy_name && IS_ERR_OR_NULL(hsotg->uphy)) { + hsotg->usb_psy_nb.notifier_call = dwc2_hsotg_psy_changed; + ret = power_supply_reg_notifier(&hsotg->usb_psy_nb); + if (ret) { + dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, + hsotg->ctrl_req); + return ret; + } + ret = devm_add_action_or_reset(dev, dwc2_hsotg_psy_unregister, + hsotg); + if (ret) { + dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, + hsotg->ctrl_req); + return ret; + } + /* The named supply may already have registered. */ + schedule_work(&hsotg->vbus_draw_work); + } return 0; } @@ -5159,6 +5248,12 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg) int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg) { usb_del_gadget_udc(&hsotg->gadget); + if (hsotg->usb_psy_name) { + if (IS_ERR_OR_NULL(hsotg->uphy)) + devm_release_action(hsotg->dev, dwc2_hsotg_psy_unregister, + hsotg); + cancel_work_sync(&hsotg->vbus_draw_work); + } dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req); return 0; diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c index f25dd2cb5d03b1..bce868942631e7 100644 --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -944,6 +944,36 @@ static void reset_config(struct usb_composite_dev *cdev) cdev->delayed_status = 0; } +/* + * Apple hosts (macOS, and Windows with Apple's device support) grant an + * Apple device more current than USB 2.0 allows with a zero-length vendor + * request: bmRequestType 0x40, bRequest 0x40, wIndex the extra active current + * and wValue the suspend current, both in mA and signed. The host sends it + * only to Apple devices it knows, before or after SET_CONFIGURATION. Like + * iOS (IOUSBDeviceFamily), allow 500 mA plus the extra while the host allows + * any current at all; a bus reset, disconnect or suspend ends the grant. + */ +#define USB_APPLE_VENDOR_ID 0x05ac +#define USB_APPLE_REQ_CURRENT_LIMIT 0x40 + +static void composite_vbus_draw(struct usb_composite_dev *cdev, unsigned int mA) +{ + cdev->vbus_draw_ma = mA; + if (mA && !cdev->suspended && cdev->apple_extra_ma) + mA = max(500 + cdev->apple_extra_ma, 0); + usb_gadget_vbus_draw(cdev->gadget, mA); +} + +static int composite_apple_current_limit(struct usb_composite_dev *cdev, + u16 w_value, u16 w_index) +{ + cdev->apple_extra_ma = (s16)w_index; + dev_info(&cdev->gadget->dev, "Apple host current grant: extra %d mA, suspend %d mA\n", + (s16)w_index, (s16)w_value); + composite_vbus_draw(cdev, cdev->vbus_draw_ma); + return 0; +} + static int set_config(struct usb_composite_dev *cdev, const struct usb_ctrlrequest *ctrl, unsigned number) { @@ -1055,7 +1085,7 @@ static int set_config(struct usb_composite_dev *cdev, else usb_gadget_clear_selfpowered(gadget); - usb_gadget_vbus_draw(gadget, power); + composite_vbus_draw(cdev, power); if (result >= 0 && cdev->delayed_status) result = USB_GADGET_DELAYED_STATUS; return result; @@ -2200,6 +2230,15 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) goto check_value; } + if ((ctrl->bRequestType & ~USB_DIR_IN) == (USB_TYPE_VENDOR | + USB_RECIP_DEVICE) && + ctrl->bRequest == USB_APPLE_REQ_CURRENT_LIMIT && !w_length && + le16_to_cpu(cdev->desc.idVendor) == USB_APPLE_VENDOR_ID) { + value = composite_apple_current_limit(cdev, w_value, + w_index); + goto check_value; + } + VDBG(cdev, "non-core control req%02x.%02x v%04x i%04x l%d\n", ctrl->bRequestType, ctrl->bRequest, @@ -2313,18 +2352,24 @@ static void __composite_disconnect(struct usb_gadget *gadget) void composite_disconnect(struct usb_gadget *gadget) { - usb_gadget_vbus_draw(gadget, 0); + struct usb_composite_dev *cdev = get_gadget_data(gadget); + + cdev->apple_extra_ma = 0; + composite_vbus_draw(cdev, 0); __composite_disconnect(gadget); } void composite_reset(struct usb_gadget *gadget) { + struct usb_composite_dev *cdev = get_gadget_data(gadget); + /* * Section 1.4.13 Standard Downstream Port of the USB battery charging * specification v1.2 states that a device connected on a SDP shall only * draw at max 100mA while in a connected, but unconfigured state. */ - usb_gadget_vbus_draw(gadget, 100); + cdev->apple_extra_ma = 0; + composite_vbus_draw(cdev, 100); __composite_disconnect(gadget); } @@ -2604,7 +2649,10 @@ void composite_suspend(struct usb_gadget *gadget) cdev->suspended = 1; usb_gadget_set_selfpowered(gadget); - usb_gadget_vbus_draw(gadget, 2); + + /* A grant applies to the active bus; resuming needs a new one. */ + cdev->apple_extra_ma = 0; + composite_vbus_draw(cdev, 2); } void composite_resume(struct usb_gadget *gadget) @@ -2619,6 +2667,7 @@ void composite_resume(struct usb_gadget *gadget) DBG(cdev, "resume\n"); if (cdev->driver->resume) cdev->driver->resume(cdev); + cdev->suspended = 0; if (cdev->config) { list_for_each_entry(f, &cdev->config->functions, list) { /* @@ -2640,14 +2689,12 @@ void composite_resume(struct usb_gadget *gadget) if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW) usb_gadget_clear_selfpowered(gadget); - usb_gadget_vbus_draw(gadget, maxpower); + composite_vbus_draw(cdev, maxpower); } else { maxpower = CONFIG_USB_GADGET_VBUS_DRAW; maxpower = min(maxpower, 100U); - usb_gadget_vbus_draw(gadget, maxpower); + composite_vbus_draw(cdev, maxpower); } - - cdev->suspended = 0; } /*-------------------------------------------------------------------------*/ diff --git a/include/linux/phy/apple-s5l-usb.h b/include/linux/phy/apple-s5l-usb.h new file mode 100644 index 00000000000000..b827ceb3c0cb1e --- /dev/null +++ b/include/linux/phy/apple-s5l-usb.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_PHY_APPLE_S5L_USB_H +#define _LINUX_PHY_APPLE_S5L_USB_H + +#include + +struct phy; + +enum apple_s5l_usbphy_dmon_mode { + APPLE_S5L_USBPHY_DMON_OFF = 0, + APPLE_S5L_USBPHY_DMON_DP, + APPLE_S5L_USBPHY_DMON_DM, + APPLE_S5L_USBPHY_DMON_BC12_DCD, + APPLE_S5L_USBPHY_DMON_BC12_PRIMARY, +}; + +#if IS_ENABLED(CONFIG_PHY_APPLE_S5L_USB) +int apple_s5l_usbphy_set_dmon(struct phy *phy, + enum apple_s5l_usbphy_dmon_mode mode); +#else +static inline int apple_s5l_usbphy_set_dmon(struct phy *phy, + enum apple_s5l_usbphy_dmon_mode mode) +{ + return -EOPNOTSUPP; +} +#endif + +#endif /* _LINUX_PHY_APPLE_S5L_USB_H */ diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h index 6e38fb9d21174a..0f7a49e5a4a1a5 100644 --- a/include/linux/usb/composite.h +++ b/include/linux/usb/composite.h @@ -502,6 +502,10 @@ struct usb_composite_dev { /* protects deactivations and delayed_status counts*/ spinlock_t lock; + /* current allowed by the configuration state, and an Apple host grant */ + unsigned int vbus_draw_ma; + int apple_extra_ma; + /* public: */ unsigned int setup_pending:1; unsigned int os_desc_pending:1;