diff --git a/Documentation/devicetree/bindings/gpio/apple,d2365-pmic-gpio.yaml b/Documentation/devicetree/bindings/gpio/apple,d2365-pmic-gpio.yaml index 68f2403eab2012..c2f858b3a0babd 100644 --- a/Documentation/devicetree/bindings/gpio/apple,d2365-pmic-gpio.yaml +++ b/Documentation/devicetree/bindings/gpio/apple,d2365-pmic-gpio.yaml @@ -19,6 +19,10 @@ description: | currently supported; direction switching and PMU event interrupts are not. Boards must reserve lines that must remain under firmware control. + The Aria D2257 used with the Apple A9X has 22 GPIOs whose input levels are + read as on the D2365 and latches 248 PMU events (31 bytes at 0x140/0x1c0), + with GPIO line N being event 0x30 + N. + The August D2400 used with the Apple A10X has 16 GPIOs whose input levels are read as on the D2365; its line configuration is not known, so they are inputs as the boot firmware set them up. It latches 256 PMU @@ -55,6 +59,7 @@ properties: - apple,d2186-pmic-gpio - apple,d2207-pmic-gpio - apple,d2255-pmic-gpio + - apple,d2257-pmic-gpio - apple,d2333-pmic-gpio - apple,d2355-pmic-gpio - apple,d2365-pmic-gpio @@ -163,6 +168,7 @@ allOf: properties: compatible: enum: + - apple,d2257-pmic-gpio - apple,d2365-pmic-gpio - apple,d2400-pmic-gpio then: diff --git a/Documentation/devicetree/bindings/power/supply/apple,d2355-tristar-charger.yaml b/Documentation/devicetree/bindings/power/supply/apple,d2355-tristar-charger.yaml index 028750632ad98e..f5bf928ad84589 100644 --- a/Documentation/devicetree/bindings/power/supply/apple,d2355-tristar-charger.yaml +++ b/Documentation/devicetree/bindings/power/supply/apple,d2355-tristar-charger.yaml @@ -10,10 +10,10 @@ maintainers: - Henrik Klee description: | - The D2355 sets the Lightning input current on the iPad Pro 12.9-inch (2017). - A Tristar port controller identifies the attached source using the ADC in the - August PMIC. The charger controls the role of the USB device controller while - identifying the source. + The D2355 sets the Lightning input current on the iPad Pro 9.7-inch (2016) + and iPad Pro 12.9-inch (2017). A Tristar port controller identifies the + attached source using the ADC in the Aria or August PMIC. The charger + controls the role of the USB device controller while identifying the source. allOf: - $ref: power-supply.yaml# @@ -32,7 +32,7 @@ properties: items: - description: Charger status register bank. - description: Charger control register bank. - - description: ADC register bank in the August PMIC. + - description: ADC register bank in the Aria or August PMIC. reg-names: items: @@ -43,7 +43,7 @@ properties: interrupts-extended: items: - description: D2355 parent-line notification. - - description: August ADC conversion complete. + - description: Aria or August ADC conversion complete. - description: D2355 source event 40. - description: D2355 source event 42. - description: D2355 source event 53. @@ -54,7 +54,15 @@ properties: apple,adc-pmic: $ref: /schemas/types.yaml#/definitions/phandle - description: The August PMIC providing the ADC registers. + description: The Aria or August PMIC providing the ADC registers. + + apple,usb-input-limit-calibration: + $ref: /schemas/types.yaml#/definitions/uint8-array + minItems: 24 + maxItems: 24 + description: + This unit's low-voltage input-current calibration, the ADT's /charger + usb-input-limit-calibration, filled in by the boot loader. apple,usb-input-limit-calibration-hv: $ref: /schemas/types.yaml#/definitions/uint8-array diff --git a/arch/arm64/boot/dts/apple/s8001-j127-j128.dtsi b/arch/arm64/boot/dts/apple/s8001-j127-j128.dtsi index 8a2bbda4609c39..4fd17bbf164bda 100644 --- a/arch/arm64/boot/dts/apple/s8001-j127-j128.dtsi +++ b/arch/arm64/boot/dts/apple/s8001-j127-j128.dtsi @@ -35,6 +35,53 @@ status = "okay"; }; +&i2c0 { + /* + * D2355 Lightning charger PMU (ADT i2c0/usb-charger): PMU events come + * on AP GPIO 211; the Aria PMIC's ADC samples the Tristar's lines + * (conversion complete is Aria event 184). + */ + d2355_pmic: pmic@75 { + compatible = "apple,d2355-pmic", "apple,i2c-pmic"; + reg = <0x75>; + #address-cells = <1>; + #size-cells = <1>; + + d2355_events: gpio { + compatible = "apple,d2355-pmic-gpio"; + interrupt-controller; + #interrupt-cells = <2>; + interrupt-parent = <&pinctrl_ap>; + interrupts = <211 IRQ_TYPE_LEVEL_LOW>; + }; + + tristar_charger: charger@80 { + compatible = "apple,d2355-tristar-charger"; + reg = <0x80 0x0a>, <0x443 0x0c>, <0x4000 0x03>; + reg-names = "status", "control", "adc"; + /* The PMU line, Aria ADC done, then the source events. */ + interrupts-extended = <&d2355_events 80 IRQ_TYPE_EDGE_RISING>, + <&aria_gpio 184 IRQ_TYPE_EDGE_RISING>, + <&d2355_events 40 IRQ_TYPE_EDGE_RISING>, + <&d2355_events 42 IRQ_TYPE_EDGE_RISING>, + <&d2355_events 53 IRQ_TYPE_EDGE_RISING>; + apple,tristar = <&tristar>; + apple,adc-pmic = <&pmic0>; + /* The boot loader adds this unit's 24-byte ADT calibration table. */ + /* A conservative window, not Apple's production limits. */ + apple,pack-limits = <150 350 3400000 4200000 (-2000000) 2000000>; + /* ADT /charger usb-input-limit-max = 2400 (iPadOS 16.7.16 20H392, J127/J128). */ + input-current-limit-microamp = <2400000>; + + port { + charger_usb: endpoint { + remote-endpoint = <&usbdev_role>; + }; + }; + }; + }; +}; + /* The two CT709 ambient light sensors; m1n1 copies the calibration to als0. */ &i2c3 { status = "okay"; @@ -71,7 +118,6 @@ touchscreen-size-x = <1536>; touchscreen-size-y = <2048>; }; - &pmic0 { backlight@b00 { compatible = "apple,aria-pmic-bl"; @@ -87,6 +133,18 @@ compatible = "apple,s8001-bcm4355b3-bt"; }; +/* The Lightning charger sets the data role once the source is known. */ +&usbdev { + usb-psy-name = "lightning"; + usb-role-switch; + + port { + usbdev_role: endpoint { + remote-endpoint = <&charger_usb>; + }; + }; +}; + &wifi { compatible = "pci14e4,43dc"; }; diff --git a/arch/arm64/boot/dts/apple/s8001-pro.dtsi b/arch/arm64/boot/dts/apple/s8001-pro.dtsi index 029861db5b350b..9c0933d596af5d 100644 --- a/arch/arm64/boot/dts/apple/s8001-pro.dtsi +++ b/arch/arm64/boot/dts/apple/s8001-pro.dtsi @@ -73,12 +73,29 @@ &i2c0 { status = "okay"; + /* ADT i2c0/tristar (tristar,cbtl1610 at 0x1a) on J98a/J99a/J127/J128. */ + tristar: port-controller@1a { + compatible = "nxp,cbtl1610"; + reg = <0x1a>; + }; + pmic0: pmic@3c { compatible = "apple,aria-pmic", "apple,i2c-pmic"; reg = <0x3c>; #address-cells = <1>; #size-cells = <1>; + /* ADT i2c0/pmu (pmu,d2257, AP GPIO 215) on J98a/J99a/J127/J128. */ + aria_gpio: gpio { + compatible = "apple,d2257-pmic-gpio"; + gpio-controller; + #gpio-cells = <2>; + interrupt-controller; + #interrupt-cells = <2>; + interrupt-parent = <&pinctrl_ap>; + interrupts = <215 IRQ_TYPE_LEVEL_LOW>; + }; + rtc@500 { compatible = "apple,aria-pmic-rtc", "apple,antigua-pmic-rtc"; reg = <0x500 0x10>; diff --git a/drivers/gpio/gpio-apple-d2365.c b/drivers/gpio/gpio-apple-d2365.c index 95ff7814747dfc..b611dbac2f4515 100644 --- a/drivers/gpio/gpio-apple-d2365.c +++ b/drivers/gpio/gpio-apple-d2365.c @@ -1243,6 +1243,30 @@ static const struct apple_pmic_gpio_info d2255_gpio_info = { .output_update_mask = BIT(4) | BIT(3) | BIT(0), }; +/* + * Aria/D2257 (A9X): 31 event bytes at 0x140/0x1c0 (248 events), + * GPIO line N is event 0x30 + N, and ADC completion is event 184 (0xb8). + */ +static const struct apple_pmic_event_info d2257_event_info = { + .status_base = 0x140, + .mask_base = 0x1c0, + .bytes = 31, + .gpio_event = 0x30, +}; + +/* + * D2257 has 22 GPIOs whose input levels sit at 0x186, eight lines per byte, + * as on the D2333, D2365 and D2400. Leave direction and output setup to the + * boot firmware for now. + */ +static const struct apple_pmic_gpio_info d2257_gpio_info = { + .label = "d2257-pmic-gpio", + .ngpio = 22, + .input_base = 0x186, + .input_only = true, + .events = &d2257_event_info, +}; + static const struct apple_pmic_gpio_info d2333_gpio_info = { .label = "d2333-pmic-gpio", .ngpio = ARRAY_SIZE(d2333_gpio_config_regs), @@ -1358,6 +1382,10 @@ static const struct of_device_id d2365_gpio_of_match[] = { .compatible = "apple,d2255-pmic-gpio", .data = &d2255_gpio_info, }, + { + .compatible = "apple,d2257-pmic-gpio", + .data = &d2257_gpio_info, + }, { .compatible = "apple,d2333-pmic-gpio", .data = &d2333_gpio_info, diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 4190aadf7327a2..a3c1ae281979ba 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -57,9 +57,8 @@ obj-$(CONFIG_APPLE_BELLATRIX_POWER) += apple_bellatrix_power.o obj-$(CONFIG_APPLE_D2365_CHARGER) += apple_d2365_charger.o obj-$(CONFIG_BATTERY_APPLE_D2186) += apple_d2186_battery.o obj-$(CONFIG_APPLE_TRISTAR_CHARGER) += apple-tristar-charger.o -apple-tristar-charger-y := apple_tristar_charger.o -apple-tristar-charger-$(CONFIG_APPLE_TRISTAR_ACCESSORY) += apple_tristar_accessory.o \ - apple_tristar_idbus.o +apple-tristar-charger-y := apple_tristar_charger.o apple_tristar_idbus.o +apple-tristar-charger-$(CONFIG_APPLE_TRISTAR_ACCESSORY) += apple_tristar_accessory.o obj-$(CONFIG_BATTERY_DA9030) += da9030_battery.o obj-$(CONFIG_BATTERY_DA9052) += da9052-battery.o obj-$(CONFIG_CHARGER_DA9150) += da9150-charger.o diff --git a/drivers/power/supply/apple_d2355_calibration.h b/drivers/power/supply/apple_d2355_calibration.h index 585398d1798a9f..3f934a54970f6d 100644 --- a/drivers/power/supply/apple_d2355_calibration.h +++ b/drivers/power/supply/apple_d2355_calibration.h @@ -10,6 +10,7 @@ #define D2355_CAL_POINTS 5 #define D2355_CAL_RANGES 4 +#define D2355_CAL_LV_BYTES 24 #define D2355_CAL_HV_BYTES 84 #define D2355_CAL_ONE 65536 @@ -46,6 +47,35 @@ static inline int d2355_calibration_parse_hv(const void *data, size_t len, return 0; } +/* + * Low-voltage 24-byte format (A9X iPad Pro 9.7-inch): 5 Q16.16 measured + * points for range 0 followed by the trailing format selector word. + */ +static inline int d2355_calibration_parse_lv(const void *data, size_t len, + struct d2355_calibration *cal) +{ + struct d2355_calibration parsed = {}; + const u8 *bytes = data; + unsigned int point; + u32 word; + + if (!data || !cal || len != D2355_CAL_LV_BYTES) + return -EINVAL; + parsed.selector = get_unaligned_le32(bytes + 4 * D2355_CAL_POINTS); + if (parsed.selector < 1 || parsed.selector > 3) + return -EINVAL; + for (point = 0; point < D2355_CAL_POINTS; point++) { + word = get_unaligned_le32(bytes + 4 * point); + if (!word || word > S32_MAX) + return -EINVAL; + if (point && word <= (u32)parsed.measured[0][point - 1]) + return -EINVAL; + parsed.measured[0][point] = word; + } + *cal = parsed; + return 0; +} + /* Native public nominal landmarks; high ranges depend on format selector. */ static const s32 d2355_calibration_nominal[4][5] = { { 100, 500, 1000, 2100, 2400 }, diff --git a/drivers/power/supply/apple_tristar_charger.c b/drivers/power/supply/apple_tristar_charger.c index 54a9023d77cf90..69fbc0ce6348b0 100644 --- a/drivers/power/supply/apple_tristar_charger.c +++ b/drivers/power/supply/apple_tristar_charger.c @@ -59,6 +59,7 @@ #include #include "apple_tristar_accessory.h" +#include "apple_tristar_idbus.h" /* PMIC connection state: bit 0 is "connected" on every PMIC */ #define TRISTAR_CHG_CONNECTED_NOW BIT(0) @@ -76,16 +77,20 @@ enum tristar_sag { TRISTAR_SAG_FAULT, /* outside the safe range: fall back */ }; -static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg); +static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg, + unsigned int *at_ma); /* 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. + * could report the PMIC's input-collapse state instead. @at_ma starts + * as the current grant and may be overridden when the chip caught the + * collapse before withdrawal. */ - enum tristar_sag (*input_sag)(struct tristar_charger *chg); + enum tristar_sag (*input_sag)(struct tristar_charger *chg, + unsigned int *at_ma); int (*prepare)(struct tristar_charger *chg); bool (*pack_allows)(struct tristar_charger *chg, const struct tristar_pack *pack); unsigned int status_size, control_size; @@ -330,6 +335,8 @@ struct tristar_charger { unsigned int epoch_seen; /* Lightning accessory policy, on boards with a role switch */ struct tristar_accessory *acc; + /* ID-bus GetID ran for this attachment (boards without chg->acc) */ + bool id_read; /* USB data is on, but the device controller is held off for a USB host role. */ bool usb_held; }; @@ -967,6 +974,7 @@ static void tristar_new_attachment(struct tristar_charger *chg, u64 now) chg->source_ma = 0; chg->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; chg->cable_known = false; + chg->id_read = false; chg->epoch_seen = atomic_read(&chg->epoch); WRITE_ONCE(chg->line_ns, 0); if (tristar_set_code(chg, chg->chip->attach_code)) @@ -1061,12 +1069,27 @@ static void tristar_run_identification(struct tristar_charger *chg, u64 now) return; } /* - * iOS measures the data lines only on the USB route that the digital - * ID selects. While the accessory policy reads that ID, leave the mux - * alone, but never beyond the hold. + * iOS reads the cable's digital ID on every attach before touching the + * data lines, then samples only the route the ID selects. Boards with + * an accessory policy do that through chg->acc; on all other boards, + * issue one GetID here so the Tristar's capability window is populated + * before tristar_identify() reads it. Session-time re-reads take the + * tristar_reconfirm() path above and never touch the ID bus. */ - if (!expired && tristar_accessory_identifying(chg->acc)) - return; + if (chg->acc) { + if (!expired && tristar_accessory_identifying(chg->acc)) + return; + } else if (!chg->id_read) { + struct tristar_idbus ib = { .client = chg->tristar }; + u8 id[TRISTAR_IDBUS_ID_LEN]; + int id_ret; + + chg->id_read = true; + id_ret = tristar_idbus_identify(&ib, id); + memzero_explicit(id, sizeof(id)); + if (id_ret && id_ret != -ENODEV) + dev_dbg(chg->dev, "ID-bus GetID before identification: %d\n", id_ret); + } ret = tristar_identify(chg, &id); if (ret == -EUCLEAN) { tristar_mux_fault(chg); @@ -1104,7 +1127,8 @@ static bool tristar_debouncing(struct tristar_charger *chg, u64 now) * 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) +static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg, + unsigned int *at_ma) { int mv, ret; @@ -1149,6 +1173,7 @@ static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg) static bool tristar_check_sag(struct tristar_charger *chg) { unsigned int cur_ma = tristar_code_ma(chg, chg->code); + unsigned int sag_ma = cur_ma; unsigned int fallback_ma = tristar_code_ma(chg, chg->chip->fallback_code); enum tristar_sag sag; @@ -1157,7 +1182,7 @@ static bool tristar_check_sag(struct tristar_charger *chg) if (cur_ma <= fallback_ma && tristar_code_ma(chg, chg->target_code) <= 1000) return false; - sag = chg->chip->input_sag(chg); + sag = chg->chip->input_sag(chg, &sag_ma); if (sag == TRISTAR_SAG_DROOP && !chg->sag_seen) { chg->sag_seen = true; return true; @@ -1175,13 +1200,17 @@ static bool tristar_check_sag(struct tristar_charger *chg) 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->sag_limit_ma = sag_ma > fallback_ma + TRISTAR_RAMP_STEP_MA ? + sag_ma - TRISTAR_RAMP_STEP_MA : fallback_ma; chg->target_code = tristar_target_code(chg); - tristar_withdraw(chg, chg->target_code); - chg->phase = TRISTAR_CHARGING; + if (chg->code > chg->target_code) { + tristar_withdraw(chg, chg->target_code); + chg->phase = TRISTAR_CHARGING; + } else if (chg->code == 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)); + sag_ma, tristar_code_ma(chg, chg->target_code)); return true; } return false; @@ -1431,6 +1460,7 @@ static void tristar_poll(struct work_struct *work) chg->sag_limit_ma = 0; chg->vbus_read_errors = 0; chg->sag_seen = false; + chg->id_read = false; chg->usb_ma = 0; chg->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; if (tristar_set_code(chg, chg->chip->handoff_code)) @@ -1503,7 +1533,7 @@ static void tristar_poll(struct work_struct *work) */ if (chg->nirqs > TRISTAR_IRQ_SOURCE && (chg->phase == TRISTAR_EMPTY || chg->phase == TRISTAR_CHARGING) && - !ramp_delay && !READ_ONCE(chg->line_ns)) + !ramp_delay && !chg->sag_seen && !READ_ONCE(chg->line_ns)) poll_ms = TRISTAR_IDLE_POLL_MS; mod_delayed_work(chg->wq, &chg->poll_work, msecs_to_jiffies(poll_ms)); diff --git a/drivers/power/supply/apple_tristar_d2355.h b/drivers/power/supply/apple_tristar_d2355.h index ead158e4a5f9be..333176cebe7ce8 100644 --- a/drivers/power/supply/apple_tristar_d2355.h +++ b/drivers/power/supply/apple_tristar_d2355.h @@ -3,16 +3,42 @@ #include "apple_d2355_current.h" #define D2355_TRISTAR_STATUS_ONLINE 0x42 +/* + * D2355 hardware input-limit collapse (iOS 16.7.16 AppleD2355PMUPowerSource:: + * handleInterrupt): when a verified limit >= 200 mA (raw >= 0x14) collapses + * the source, the D2355 drops control_base + 0xb (0x44e) to 0x0a (100 mA) + * while status[1] (0x81) bit 7 stays set. + */ +#define D2355_IIN_COLLAPSE_MIN_RAW 0x14 +#define D2355_IIN_COLLAPSE_RAW 0x0a struct tristar_d2355 { struct d2355_calibration calibration; struct mutex io_lock; s32 pack_limits[6]; unsigned int requested; + unsigned int collapse_ma; u8 raw, control; bool verified, fault; }; +/* Caller holds hw->io_lock. */ +static bool tristar_d2355_hw_collapsed(struct tristar_charger *chg) +{ + struct tristar_d2355 *hw = chg->chip_data; + unsigned int control, raw, status1; + + if (!hw->verified || hw->raw < D2355_IIN_COLLAPSE_MIN_RAW || + (hw->control & BIT(2))) + return false; + if (regmap_read(chg->regmap, chg->control_base, &control) || + regmap_read(chg->regmap, chg->control_base + 0xb, &raw) || + regmap_read(chg->regmap, chg->status_base + 1, &status1)) + return false; + return !(control & BIT(2)) && raw == D2355_IIN_COLLAPSE_RAW && + (status1 & BIT(7)); +} + static int tristar_d2355_disable(struct tristar_charger *chg) { unsigned int control, check; @@ -120,6 +146,16 @@ static int tristar_d2355_write_limit(struct tristar_charger *chg, unsigned int c goto out; } } + if (!code) { + hw->collapse_ma = 0; + } else if (code <= chg->chip->fallback_code && + tristar_d2355_hw_collapsed(chg)) { + unsigned int ma = hw->requested * 10; + + hw->collapse_ma = hw->collapse_ma ? min(hw->collapse_ma, ma) : ma; + dev_dbg(chg->dev, "D2355 collapsed input limit at %u mA before withdrawal\n", + ma); + } hw->verified = false; ret = tristar_d2355_disable(chg); if (ret) { @@ -233,9 +269,14 @@ static int tristar_d2355_prepare(struct tristar_charger *chg) return -EINVAL; /* The unit's ADT /charger table, copied by the boot loader */ table = of_get_property(dev->of_node, "apple,usb-input-limit-calibration-hv", &len); - if (!table) - return -ENOENT; - ret = d2355_calibration_parse_hv(table, len, &hw->calibration); + if (table) { + ret = d2355_calibration_parse_hv(table, len, &hw->calibration); + } else { + table = of_get_property(dev->of_node, "apple,usb-input-limit-calibration", &len); + if (!table) + return -ENOENT; + ret = d2355_calibration_parse_lv(table, len, &hw->calibration); + } if (ret) return ret; np = of_parse_phandle(dev->of_node, "apple,adc-pmic", 0); @@ -257,8 +298,32 @@ static int tristar_d2355_prepare(struct tristar_charger *chg) return 0; } +static enum tristar_sag tristar_d2355_input_sag(struct tristar_charger *chg, + unsigned int *at_ma) +{ + struct tristar_d2355 *hw = chg->chip_data; + enum tristar_sag sag = tristar_vbus_sag(chg, at_ma); + + if (sag != TRISTAR_SAG_NONE) + return sag; + + mutex_lock(&hw->io_lock); + if (hw->collapse_ma) { + *at_ma = hw->collapse_ma; + if (chg->sag_seen) + hw->collapse_ma = 0; + sag = TRISTAR_SAG_DROOP; + } else if (tristar_d2355_hw_collapsed(chg)) { + dev_dbg(chg->dev, "D2355 collapsed input limit at %u mA\n", + hw->requested * 10); + sag = TRISTAR_SAG_DROOP; + } + mutex_unlock(&hw->io_lock); + return sag; +} + static const struct tristar_chip_info tristar_d2355_chip = { - .input_sag = tristar_vbus_sag, + .input_sag = tristar_d2355_input_sag, .prepare = tristar_d2355_prepare, .pack_allows = tristar_d2355_pack_allows, .status_size = 10,