From b63f4ba93789c77287ce575c533c57a745e3f019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:58 +0100 Subject: [PATCH 1/9] mfd: apple-pmu-i2c: Add the Dialog D1972 PMU MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The iPhone 5 (N42, ADT "pmu,d1972") has a Dialog D1972 PMU. Move the GPIO count and the power-off register sequence into per-chip data, and add "apple,pmu-d1972", from AppleD1972PMU (iPhone5,2 iOS 10.3.4): - 8-bit register addresses, 12 GPIOs at 0x68 + n; - power off (_shutdownGated): 0x7d = 0, clear 0x01..0x09, wake masks 0x12..0x1a (88 ff ff ff ff ae ff ff ff), alarm control 0x62 = 0x01, 0xa2 = 0, 0xa4 = 0, boot flags 0xb0 &= ~0xd0, clear the wake bit of the input GPIOs (>= 0x60), standby 0x1b |= 0x01. The D1946, D1974, D2333 and Chestnut paths are unchanged. Tested on an iPhone5,2: the PMU probes, and its GPIOs drive the Bluetooth and Wi-Fi power lines. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/mfd/apple-pmu-i2c.c | 179 ++++++++++++++++++++++------------ include/linux/apple-pmu-i2c.h | 3 + 2 files changed, 117 insertions(+), 65 deletions(-) diff --git a/drivers/mfd/apple-pmu-i2c.c b/drivers/mfd/apple-pmu-i2c.c index aa69d2e3626281..f9c3c9a36d535e 100644 --- a/drivers/mfd/apple-pmu-i2c.c +++ b/drivers/mfd/apple-pmu-i2c.c @@ -20,9 +20,74 @@ #include #include +struct apple_pmu_off_info { + u8 pre_zero_reg; /* written 0 first (WLED/boost disable) */ + u8 events_reg; /* read nevents bytes to clear pending events */ + u8 masks_reg; /* write nevents off_masks bytes */ + u8 nevents; + const u8 *off_masks; + u8 alarm_ctrl_reg; /* written 0x01 (no RTC wake alarm) */ + u8 zero_reg1; /* written 0 */ + u8 zero_reg2; /* optional second register written 0 */ + u8 boot_flags_reg; /* cleared with mask 0xd0 for iBoot */ + u8 standby_reg; /* ORed with 0x01 in atomic power-off */ +}; + struct apple_pmu_i2c_info { unsigned reg_bits, val_bits; unsigned gpio_cfg; /* GPIO n's configuration register: gpio_cfg + n */ + unsigned ngpio; + const struct apple_pmu_off_info *off; +}; + +#define APPLE_PMU_GPIO_WAKE 0x02 + +static const u8 d1946_off_masks[9] = { + 0x88, 0xff, 0xff, 0xff, 0xff, 0xff, 0xae, 0xff, 0xff, +}; + +static const struct apple_pmu_off_info d1946_off_info = { + .pre_zero_reg = 0xd2, + .events_reg = 0x01, + .masks_reg = 0x12, + .nevents = ARRAY_SIZE(d1946_off_masks), + .off_masks = d1946_off_masks, + .alarm_ctrl_reg = 0x5b, + .zero_reg1 = 0xa1, + .boot_flags_reg = 0xaf, + .standby_reg = 0x1b, +}; + +/* + * D1972 (iPhone 5, S5L8950X, ADT "pmu,d1972"): 8-bit register addressing, + * 12 GPIOs at 0x68 + n (AppleD1972PMU vtable +0x3c4, 14G61 0x810e7678), and + * the same shutdown sequence as D1946 with shifted registers + * (AppleD1972PMU::_shutdownGated, 14G61 0x810e7a80): + * + * 0x7d = 0 pre-shutdown disable + * read 0x01..0x09 clear pending events + * 0x12..0x1a = mask table wake masks (0xae is at byte 5, not byte 6) + * 0x62 = 0x01 no RTC wake alarm (RTC counter is 0x64..0x67) + * 0xa2 = 0, 0xa4 = 0 + * 0xb0 &= ~0xd0 boot flags for iBoot + * 0x68..0x73: bit 1 off clear wake bit on all 12 input GPIOs (>= 0x60) + * 0x1b |= 1 PMU standby + */ +static const u8 d1972_off_masks[9] = { + 0x88, 0xff, 0xff, 0xff, 0xff, 0xae, 0xff, 0xff, 0xff, +}; + +static const struct apple_pmu_off_info d1972_off_info = { + .pre_zero_reg = 0x7d, + .events_reg = 0x01, + .masks_reg = 0x12, + .nevents = ARRAY_SIZE(d1972_off_masks), + .off_masks = d1972_off_masks, + .alarm_ctrl_reg = 0x62, + .zero_reg1 = 0xa2, + .zero_reg2 = 0xa4, + .boot_flags_reg = 0xb0, + .standby_reg = 0x1b, }; static const struct apple_pmu_i2c_info apple_pmu_i2c_d2333_info = { .reg_bits = 16, .val_bits = 8 }; @@ -46,7 +111,12 @@ static const struct apple_pmu_i2c_info apple_pmu_i2c_chestnut_info = { .reg_bits * _setLDO's bounds check of 0x16. */ static const struct apple_pmu_i2c_info apple_pmu_i2c_d1946_info = { - .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x61, + .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x61, .ngpio = 18, + .off = &d1946_off_info, +}; +static const struct apple_pmu_i2c_info apple_pmu_i2c_d1972_info = { + .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x68, .ngpio = 12, + .off = &d1972_off_info, }; /* * The iPad 3's D1974 (ADT "pmu,d1974") takes the same 8-bit addressing and @@ -56,75 +126,51 @@ static const struct apple_pmu_i2c_info apple_pmu_i2c_d1946_info = { * D1946's power-off is not checked against this chip. */ static const struct apple_pmu_i2c_info apple_pmu_i2c_d1974_info = { - .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x64, + .reg_bits = 8, .val_bits = 8, .gpio_cfg = 0x64, .ngpio = 18, }; + /* - * Power-off, the way AppleD1946PMU::_shutdownGated does it (kernelcache - * 12H321, 0x80ecec80; vtable slot 0x3a4). In order: - * - * 0xd2 = 0 the backlight's strings off - * read 0x01..0x09 the event registers, which clears them, so - * a stale event does not wake it at once - * 0x12..0x1a = mask table the events allowed to wake it: iOS's table - * 0x5b = 0x01 no RTC wake alarm (0x41 with one) - * 0xa1 = 0 - * 0xaf &= ~0xd0 flags for iBoot; a restart sets 0x90 instead - * 0x61..0x72: bit 1 off wake off on every PMU GPIO configured as an - * input (config >= 0x60) -- "cannot turn off GPIO - * wakes" is iOS's message when this fails - * 0x1b |= 1 "pmu go stdby": the PMU drops every rail - * - * Read on this iPad under Linux before any of it: the masks all 0x00, 0x5b - * 0x03, 0xaf 0x40, 0x1b 0x00. iOS's other branch, an external shutdown - * function, needs function-external_shutdown in the ADT, which P105's PMU - * does not have. Hold starts the iPad again, into iOS. - * - * All of it but the last write runs in the power-off prepare stage, where - * regmap may still sleep; the last one runs with interrupts off, straight - * through the I2C adapter's atomic transfer. + * Power-off, the way AppleD1946PMU::_shutdownGated (12H321, 0x80ecec80) and + * AppleD1972PMU::_shutdownGated (14G61, 0x810e7a80) do it. All of it but the + * last write runs in the power-off prepare stage, where regmap may still sleep; + * the last one runs with interrupts off, straight through the I2C adapter's + * atomic transfer. */ -#define D1946_EVENTS 0x01 -#define D1946_EVENT_MASKS 0x12 -#define D1946_NEVENTS 9 -#define D1946_STANDBY 0x1b -#define D1946_ALARM_CTRL 0x5b -#define D1946_GPIO_CFG 0x61 -#define D1946_NGPIO 18 -#define D1946_GPIO_WAKE 0x02 -#define D1946_REG_A1 0xa1 -#define D1946_BOOT_FLAGS 0xaf -#define D1946_WLED_ENABLE 0xd2 - -static const u8 d1946_off_masks[D1946_NEVENTS] = { - 0x88, 0xff, 0xff, 0xff, 0xff, 0xff, 0xae, 0xff, 0xff, -}; - -static int apple_pmu_d1946_power_off_prepare(struct sys_off_data *data) +static int apple_pmu_power_off_prepare(struct sys_off_data *data) { - struct regmap *map = ((struct apple_pmu_i2c *)data->cb_data)->regmap; + struct apple_pmu_i2c *pmu = data->cb_data; + const struct apple_pmu_i2c_info *info = pmu->info; + const struct apple_pmu_off_info *off = info->off; + struct regmap *map = pmu->regmap; unsigned int i, v; - regmap_write(map, D1946_WLED_ENABLE, 0); - for (i = 0; i < D1946_NEVENTS; i++) - regmap_read(map, D1946_EVENTS + i, &v); - for (i = 0; i < D1946_NEVENTS; i++) - regmap_write(map, D1946_EVENT_MASKS + i, d1946_off_masks[i]); - regmap_write(map, D1946_ALARM_CTRL, 0x01); - regmap_write(map, D1946_REG_A1, 0); - regmap_update_bits(map, D1946_BOOT_FLAGS, 0xd0, 0); - for (i = 0; i < D1946_NGPIO; i++) - if (!regmap_read(map, D1946_GPIO_CFG + i, &v) && v >= 0x60 && (v & D1946_GPIO_WAKE)) - regmap_write(map, D1946_GPIO_CFG + i, v & ~D1946_GPIO_WAKE); + if (off->pre_zero_reg) + regmap_write(map, off->pre_zero_reg, 0); + for (i = 0; i < off->nevents; i++) + regmap_read(map, off->events_reg + i, &v); + for (i = 0; i < off->nevents; i++) + regmap_write(map, off->masks_reg + i, off->off_masks[i]); + regmap_write(map, off->alarm_ctrl_reg, 0x01); + if (off->zero_reg1) + regmap_write(map, off->zero_reg1, 0); + if (off->zero_reg2) + regmap_write(map, off->zero_reg2, 0); + regmap_update_bits(map, off->boot_flags_reg, 0xd0, 0); + for (i = 0; i < info->ngpio; i++) + if (!regmap_read(map, info->gpio_cfg + i, &v) && v >= 0x60 && (v & APPLE_PMU_GPIO_WAKE)) + regmap_write(map, info->gpio_cfg + i, v & ~APPLE_PMU_GPIO_WAKE); return NOTIFY_DONE; } -static int apple_pmu_d1946_power_off(struct sys_off_data *data) +static int apple_pmu_power_off(struct sys_off_data *data) { - struct i2c_client *client = to_i2c_client(((struct apple_pmu_i2c *)data->cb_data)->dev); - int v = i2c_smbus_read_byte_data(client, D1946_STANDBY); + struct apple_pmu_i2c *pmu = data->cb_data; + const struct apple_pmu_off_info *off = pmu->info->off; + struct i2c_client *client = to_i2c_client(pmu->dev); + int v = i2c_smbus_read_byte_data(client, off->standby_reg); if (v >= 0) - i2c_smbus_write_byte_data(client, D1946_STANDBY, v | 0x01); + i2c_smbus_write_byte_data(client, off->standby_reg, v | 0x01); mdelay(1000); pr_emerg("apple-pmu-i2c: the PMU did not power off (standby %d)\n", v); return NOTIFY_DONE; @@ -224,7 +270,8 @@ static int apple_pmu_gpio_direction_input(struct gpio_chip *gc, unsigned int n) return apple_pmu_gpio_get_direction(gc, n) == GPIO_LINE_DIRECTION_IN ? 0 : -EPERM; } -static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_base) +static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_base, + unsigned int ngpio) { struct apple_pmu_gpio *g = devm_kzalloc(pmu->dev, sizeof(*g), GFP_KERNEL); struct gpio_chip *gc; @@ -238,7 +285,7 @@ static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_b gc->parent = pmu->dev; gc->owner = THIS_MODULE; gc->base = -1; - gc->ngpio = D1946_NGPIO; + gc->ngpio = ngpio; gc->can_sleep = true; gc->get_direction = apple_pmu_gpio_get_direction; gc->direction_input = apple_pmu_gpio_direction_input; @@ -250,6 +297,7 @@ static int apple_pmu_gpio_register(struct apple_pmu_i2c *pmu, unsigned int cfg_b static const struct of_device_id apple_pmu_i2c_of_match[] = { { .compatible = "apple,pmu-d1974", .data = &apple_pmu_i2c_d1974_info }, + { .compatible = "apple,pmu-d1972", .data = &apple_pmu_i2c_d1972_info }, { .compatible = "apple,pmu-d1946", .data = &apple_pmu_i2c_d1946_info }, { .compatible = "apple,pmu-d2333", .data = &apple_pmu_i2c_d2333_info }, { .compatible = "apple,pmu-chestnut", .data = &apple_pmu_i2c_chestnut_info }, @@ -276,6 +324,7 @@ static int apple_pmu_i2c_probe(struct i2c_client *i2c) info = of_id->data; pmu->dev = &i2c->dev; + pmu->info = info; i2c_set_clientdata(i2c, pmu); pmu->config.reg_bits = info->reg_bits; @@ -321,20 +370,20 @@ static int apple_pmu_i2c_probe(struct i2c_client *i2c) } } - if (info == &apple_pmu_i2c_d1946_info && of_device_is_system_power_controller(node)) { + if (info->off && of_device_is_system_power_controller(node)) { ret = devm_register_sys_off_handler(&i2c->dev, SYS_OFF_MODE_POWER_OFF_PREPARE, SYS_OFF_PRIO_DEFAULT, - apple_pmu_d1946_power_off_prepare, pmu); + apple_pmu_power_off_prepare, pmu); if (!ret) ret = devm_register_sys_off_handler(&i2c->dev, SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT, - apple_pmu_d1946_power_off, pmu); + apple_pmu_power_off, pmu); if (ret) dev_warn(&i2c->dev, "no power-off: %d\n", ret); } - if (info->gpio_cfg && of_property_read_bool(node, "gpio-controller")) { - ret = apple_pmu_gpio_register(pmu, info->gpio_cfg); + if (info->gpio_cfg && info->ngpio && of_property_read_bool(node, "gpio-controller")) { + ret = apple_pmu_gpio_register(pmu, info->gpio_cfg, info->ngpio); if (ret) dev_warn(&i2c->dev, "no GPIOs: %d\n", ret); } diff --git a/include/linux/apple-pmu-i2c.h b/include/linux/apple-pmu-i2c.h index 8d227b3d5ee543..6390967c13e65d 100644 --- a/include/linux/apple-pmu-i2c.h +++ b/include/linux/apple-pmu-i2c.h @@ -10,10 +10,13 @@ #include #include +struct apple_pmu_i2c_info; + struct apple_pmu_i2c { struct device *dev; struct regmap *regmap; struct regmap_config config; + const struct apple_pmu_i2c_info *info; }; #endif From d76d731a6afc4a87e5f80ac95d63777443dd2da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:58 +0100 Subject: [PATCH 2/9] power: supply: apple-pmu-charger: Add the Dialog D1972, read-only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the per-chip charger registers and the current encoding into struct pmu_chg_chip and add "apple,pmu-d1972-charger" for the iPhone 5, from AppleD1972PMUPowerSource (iPhone5,2 iOS 10.3.4): - the USB input current limit is register 0x3f (0x3d on the D1946); - the code is linear, 10 mA per step from 70 mA: (mA - 70) / 10; - VBUS online is status register 0x0a bit 6; a VBUS sag drops 0x3f to 0x03 (100 mA). The battery temperature comes from the HDQ gauge, not a PMU ADC channel, and corrupt gauge words once alternated "battery at 6272.0 C" with good readings. On the D1972 a reading outside -20..+70 C counts as no reading (the existing 100 mA fallback), and leaving the fallback takes three good readings in a row. Entering it on a missing reading is logged once. The D1946 path is unchanged. The driver keeps its armed=0 default: on the iPhone 5 it reports VBUS and the input limit, and logs the limit it would set without writing it. Tested on an iPhone5,2: VBUS online and the 100 mA limit read, no write. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/power/supply/apple-pmu-charger.c | 267 +++++++++++++++++------ 1 file changed, 204 insertions(+), 63 deletions(-) diff --git a/drivers/power/supply/apple-pmu-charger.c b/drivers/power/supply/apple-pmu-charger.c index cf265c512f3bc5..a2e89c1b1fc510 100644 --- a/drivers/power/supply/apple-pmu-charger.c +++ b/drivers/power/supply/apple-pmu-charger.c @@ -40,6 +40,11 @@ * - 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. + * On the D1972 a reading outside -20..+70 C counts as missing, and it + * takes three cool readings in a row to leave the fallback. + * + * The temperature is the battery gauge's ("battery", POWER_SUPPLY_PROP_TEMP), + * on every chip; this driver reads no PMU ADC channel. */ #include #include @@ -58,11 +63,8 @@ #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 */ -#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 @@ -71,9 +73,106 @@ #define HOT_DECI_C 450 #define COOL_DECI_C 400 +/* + * temp_guard (D1972 / iPhone 5): the battery temperature is the gauge's, over + * HDQ. A reading outside -20..+70 C is no reading at all, and leaving the + * 100 mA fallback takes COOL_SAMPLES cool readings in a row, so neither a + * corrupt word nor a single good one between failures moves the limit up. + */ +#define GUARD_MIN_DECI_C (-200) +#define GUARD_MAX_DECI_C 700 +#define COOL_SAMPLES 3 + +struct pmu_chg_chip { + const char *name; + u8 status_reg; + u8 vbus_mask; + u8 ilim_reg; + u8 min_code; /* < 100 mA; also the PMU's own VBUS-sag drop value */ + u8 ramp_step; /* codes per ramp step (~100 mA) */ + unsigned int max_ma; + u8 (*ma_to_code)(unsigned int ma); + unsigned int (*code_to_ma)(u8 code); + bool temp_guard; +}; + +/* D1946: iBEC's encoding, 0x9ff0c618..0x9ff0c662 (register 0x3d) */ +static u8 d1946_ma_to_code(unsigned int ma) +{ + unsigned int code; + + ma = min_t(unsigned int, ma, ILIM_HARD_MAX_MA); + if (ma < 100) + return 0x02; + code = (8 * ma - 600) / 100; + if (code >= 0x73) + return code - 4; + if (code > 0x6e) + return 0x6e; + if (code >= 0x4b) + return code - 2; + if (code > 0x48) + return 0x48; + return code; +} + +static unsigned int d1946_code_to_ma(u8 code) +{ + return code <= 0x02 ? 100 : (code * 100 + 600) / 8; +} + +/* + * D1972 (iPhone 5, S5L8950X): register 0x3f. + * AppleD1972PMUPowerSource (14G61 0x810e9bf4..0x810e9c2f): linear 10 mA/LSB + * from 70 mA (code = (mA - 70) / 10; < 70 mA suspends, > 2619 mA clamps to + * 0xff). On VBUS sag the PMU drops 0x3f back to 0x03 (100 mA, checked at + * 0x810e99e6). Online is status register 0x0a bit 6 (0x810e9ec8), as on D1946. + */ +static u8 d1972_ma_to_code(unsigned int ma) +{ + ma = min_t(unsigned int, ma, ILIM_HARD_MAX_MA); + if (ma < 100) + return 0x03; + return (ma - 70) / 10; +} + +static unsigned int d1972_code_to_ma(u8 code) +{ + return code <= 0x03 ? 100 : 70 + code * 10; +} + +static const struct pmu_chg_chip pmu_chg_d1946 = { + .name = "d1946", + .status_reg = PMU_STATUS0, + .vbus_mask = PMU_STATUS0_VBUS, + .ilim_reg = 0x3d, + .min_code = 0x02, + .ramp_step = 8, + .max_ma = ILIM_HARD_MAX_MA, + .ma_to_code = d1946_ma_to_code, + .code_to_ma = d1946_code_to_ma, +}; + +static const struct pmu_chg_chip pmu_chg_d1972 = { + .name = "d1972", + .status_reg = PMU_STATUS0, + .vbus_mask = PMU_STATUS0_VBUS, + .ilim_reg = 0x3f, + .min_code = 0x03, + .ramp_step = 10, + .max_ma = ILIM_HARD_MAX_MA, + .ma_to_code = d1972_ma_to_code, + .code_to_ma = d1972_code_to_ma, + .temp_guard = true, +}; + +static bool d1972_armed; +module_param(d1972_armed, bool, 0644); +MODULE_PARM_DESC(d1972_armed, "enable D1972 charger writes (default off)"); + static bool armed = true; module_param(armed, bool, 0644); -MODULE_PARM_DESC(armed, "write 0x3d (default on; off: log only)"); +MODULE_PARM_DESC(armed, "enable D1946 charger writes (default on)"); static int max_input_ma = ILIM_DEFAULT_MA; module_param(max_input_ma, int, 0644); @@ -82,52 +181,33 @@ MODULE_PARM_DESC(max_input_ma, "ceiling for input_current_limit, 100..2100 mA (d struct pmu_chg { struct device *dev; struct regmap *map; + const struct pmu_chg_chip *chip; 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 */ + u8 probe_code; /* ILIM register as iBoot left it */ u8 last_written; /* 0 = nothing written while online */ u8 ceiling; /* after a sag, until unplugged; 0xff = none */ bool online; bool was_armed; bool hot; + u8 cool_samples; /* temp_guard: cool readings in a row */ 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) -{ - unsigned int code; - - ma = min_t(unsigned int, ma, ILIM_HARD_MAX_MA); - if (ma < 100) - return ILIM_MIN_CODE; - code = (8 * ma - 600) / 100; - if (code >= 0x73) - return code - 4; - if (code > 0x6e) - return 0x6e; - if (code >= 0x4b) - return code - 2; - if (code > 0x48) - return 0x48; - return code; -} - -/* The nominal current of a code, for the log only. */ -static unsigned int pmu_chg_code_to_ma(u8 code) +static bool pmu_chg_armed(struct pmu_chg *c) { - return code <= ILIM_MIN_CODE ? 100 : (code * 100 + 600) / 8; + return c->chip == &pmu_chg_d1972 ? READ_ONCE(d1972_armed) : READ_ONCE(armed); } 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); @@ -153,8 +233,10 @@ static void pmu_chg_log_vbus(struct pmu_chg *c, u8 ilim, bool is_armed) 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"); + if (c->chip != &pmu_chg_d1946) + return; for (i = 0; i < ARRAY_SIZE(regs); i++) { ret = pmu_chg_read(c, regs[i], &val); if (ret) @@ -167,42 +249,90 @@ static void pmu_chg_log_vbus(struct pmu_chg *c, u8 ilim, bool is_armed) static void pmu_chg_sagged(struct pmu_chg *c, u8 at) { - c->ceiling = max_t(int, ILIM_MIN_CODE, at - ILIM_STEP); + c->ceiling = max_t(int, c->chip->min_code, at - c->chip->ramp_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, c->chip->code_to_ma(c->ceiling)); } -/* Ramp 0x3d from cur up to target, 8 codes every 10 ms, watching for a sag. */ +/* Ramp ILIM from cur up to target, ramp_step 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; + u8 next = min_t(int, cur + c->chip->ramp_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; - if (rb == ILIM_MIN_CODE && next > ILIM_MIN_CODE) { + if (rb == c->chip->min_code && next > c->chip->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; } } +static bool pmu_chg_battery_hot_guarded(struct pmu_chg *c) +{ + union power_supply_propval t = {}; + struct power_supply *bat = power_supply_get_by_name("battery"); + int ret = -ENODEV; + + if (bat) { + ret = power_supply_get_property(bat, POWER_SUPPLY_PROP_TEMP, &t); + power_supply_put(bat); + if (!ret && (t.intval < GUARD_MIN_DECI_C || t.intval > GUARD_MAX_DECI_C)) + ret = -ERANGE; + } + + if (ret) { + /* no reading: the 100 mA fallback, and the cool count starts over */ + if (!c->hot) + dev_warn(c->dev, "battery temperature unavailable (%d%s): input limited to 100 mA\n", + ret == -ERANGE ? t.intval : ret, + ret == -ERANGE ? " deci-C, out of range" : ""); + c->hot = true; + c->cool_samples = 0; + return true; + } + + if (t.intval > HOT_DECI_C) { + if (!c->hot) + dev_warn(c->dev, "battery at %s%d.%d C: input limited to 100 mA\n", + t.intval < 0 ? "-" : "", abs(t.intval) / 10, abs(t.intval) % 10); + c->hot = true; + c->cool_samples = 0; + } else if (c->hot) { + if (t.intval < COOL_DECI_C && ++c->cool_samples >= COOL_SAMPLES) { + c->hot = false; + c->cool_samples = 0; + dev_info(c->dev, "battery back to %s%d.%d C (%d readings in a row)\n", + t.intval < 0 ? "-" : "", abs(t.intval) / 10, + abs(t.intval) % 10, COOL_SAMPLES); + } else if (t.intval >= COOL_DECI_C) { + c->cool_samples = 0; + } + } + return c->hot; +} + static bool pmu_chg_battery_hot(struct pmu_chg *c) { union power_supply_propval t; - struct power_supply *bat = power_supply_get_by_name("battery"); + struct power_supply *bat; + if (c->chip->temp_guard) + return pmu_chg_battery_hot_guarded(c); + + bat = power_supply_get_by_name("battery"); if (!bat) { c->hot = true; return true; @@ -230,35 +360,37 @@ 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); + clamp(READ_ONCE(max_input_ma), 100, (int)c->chip->max_ma)); + u8 code = c->chip->ma_to_code(ma); if (c->ceiling != 0xff) code = min(code, c->ceiling); if (pmu_chg_battery_hot(c)) - code = ILIM_MIN_CODE; + code = c->chip->min_code; return code; } 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; + bool is_armed = pmu_chg_armed(c), online, changed = false; u8 st, cur, target; mutex_lock(&c->lock); if (c->was_armed && !is_armed) { - if (pmu_chg_write(c, PMU_ILIM, c->probe_code)) + if (pmu_chg_write(c, c->chip->ilim_reg, 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); + dev_info(c->dev, "disarmed: 0x%02x back to 0x%02x\n", + c->chip->ilim_reg, c->probe_code); c->last_written = 0; c->logged_code = 0; c->was_armed = false; goto out; } - if (pmu_chg_read(c, PMU_STATUS0, &st) || pmu_chg_read(c, PMU_ILIM, &cur)) + if (pmu_chg_read(c, c->chip->status_reg, &st) || + pmu_chg_read(c, c->chip->ilim_reg, &cur)) goto out; - online = st & PMU_STATUS0_VBUS; + online = !!(st & c->chip->vbus_mask); changed = online != c->online; c->online = online; if (changed) { @@ -276,8 +408,9 @@ static void pmu_chg_work(struct work_struct *w) 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, + c->chip->code_to_ma(target)); c->logged_code = target; } goto out; @@ -285,14 +418,14 @@ static void pmu_chg_work(struct work_struct *w) 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) { + if (cur == c->chip->min_code && c->last_written > c->chip->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); out: mutex_unlock(&c->lock); if (changed) @@ -325,7 +458,7 @@ static int pmu_chg_set(struct power_supply *psy, enum power_supply_property psp, 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)) + if (ma < 100 || ma > clamp(READ_ONCE(max_input_ma), 100, (int)c->chip->max_ma)) return -ERANGE; mutex_lock(&c->lock); c->req_ma = ma; @@ -360,25 +493,31 @@ static const struct power_supply_desc pmu_chg_desc = { 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; int ret; + if (!chip) + return -ENODEV; + c = devm_kzalloc(&pdev->dev, sizeof(*c), GFP_KERNEL); if (!c) return -ENOMEM; c->dev = &pdev->dev; c->map = pmu->regmap; + c->chip = chip; c->req_ma = ILIM_DEFAULT_MA; c->ceiling = 0xff; mutex_init(&c->lock); - if (pmu_chg_read(c, PMU_ILIM, &c->probe_code) || pmu_chg_read(c, PMU_STATUS0, &st)) + if (pmu_chg_read(c, chip->ilim_reg, &c->probe_code) || + pmu_chg_read(c, chip->status_reg, &st)) return -EIO; - c->online = st & PMU_STATUS0_VBUS; + c->online = !!(st & chip->vbus_mask); if (c->online) - pmu_chg_log_vbus(c, c->probe_code, READ_ONCE(armed)); + pmu_chg_log_vbus(c, c->probe_code, pmu_chg_armed(c)); cfg.drv_data = c; cfg.of_node = pdev->dev.of_node; @@ -393,14 +532,16 @@ static int pmu_chg_probe(struct platform_device *pdev) 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)"); + dev_info(&pdev->dev, "%s: 0x%02x = 0x%02x (%u mA) at probe, VBUS %s, %s\n", + chip->name, chip->ilim_reg, c->probe_code, + chip->code_to_ma(c->probe_code), + c->online ? "on" : "off", pmu_chg_armed(c) ? "armed" : "disarmed (log only)"); return 0; } static const struct of_device_id pmu_chg_of_match[] = { - { .compatible = "apple,pmu-d1946-charger" }, + { .compatible = "apple,pmu-d1946-charger", .data = &pmu_chg_d1946 }, + { .compatible = "apple,pmu-d1972-charger", .data = &pmu_chg_d1972 }, { } }; MODULE_DEVICE_TABLE(of, pmu_chg_of_match); @@ -414,5 +555,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 (D1946/D1972) USB input current limit"); MODULE_LICENSE("GPL"); From 77fead475816a7b4d82e5babde5d3a088b1803c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:59 +0100 Subject: [PATCH 3/9] backlight: Add the Apple LM3534 and DWI backlight MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The iPhone 5's D1972 PMU has no WLED strings, unlike the A5's D1946 and the A5X's D1974. Its panel backlight is an external TI LM3534 on I2C0 (0x63), whose brightness comes over the SoC's DWI block (0x3f107000, version 1). Add apple-lm3534, after AppleLMBacklight and AppleS5L8940XDWI (iPhone5,2 iOS 10.3.4): - enable and disable with LM3534 register 0x10 bit 0, keeping the other bits iBoot set, and wait 600 us after enabling; - send 11-bit brightness (0..2047) on the DWI sync channel: +0x14 = 0xa0000000 | level, then +0x10 = 0x71 once busy (bit 0) is clear. Tested on an iPhone5,2: "LM3534 reg 0x10=0x03 (on), DWI v1 attached", and a brightness of 300 is written and read back. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/video/backlight/Kconfig | 9 + drivers/video/backlight/Makefile | 1 + drivers/video/backlight/apple-lm3534.c | 245 +++++++++++++++++++++++++ 3 files changed, 255 insertions(+) create mode 100644 drivers/video/backlight/apple-lm3534.c diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig index 361b50ea226ee7..b5ddc2c7ba3196 100644 --- a/drivers/video/backlight/Kconfig +++ b/drivers/video/backlight/Kconfig @@ -458,6 +458,15 @@ config BACKLIGHT_APPLE_PMU_WLED The iPad mini 1's backlight, driven by the white-LED strings of its D1946 PMU, as /sys/class/backlight/apple-pmu-wled (0..2047). +config BACKLIGHT_APPLE_LM3534 + tristate "Apple TI LM3534 + DWI backlight (iPhone 5 / N42)" + depends on I2C && OF + select REGMAP_I2C + help + Backlight driver for Apple S5L8950X devices (iPhone 5) using an + external TI LM3534 LED driver over I2C0 for enable control and the + SoC DWI block for 11-bit brightness updates (0..2047). + config BACKLIGHT_GPIO tristate "Generic GPIO based Backlight Driver" depends on GPIOLIB || COMPILE_TEST diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile index 050be5fcab7df9..9547c361a9bd07 100644 --- a/drivers/video/backlight/Makefile +++ b/drivers/video/backlight/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_BACKLIGHT_DA903X) += da903x_bl.o obj-$(CONFIG_BACKLIGHT_DA9052) += da9052_bl.o obj-$(CONFIG_BACKLIGHT_EP93XX) += ep93xx_bl.o obj-$(CONFIG_BACKLIGHT_APPLE_PMU_WLED) += apple-pmu-wled.o +obj-$(CONFIG_BACKLIGHT_APPLE_LM3534) += apple-lm3534.o obj-$(CONFIG_BACKLIGHT_GPIO) += gpio_backlight.o obj-$(CONFIG_BACKLIGHT_HP680) += hp680_bl.o obj-$(CONFIG_BACKLIGHT_HP700) += jornada720_bl.o diff --git a/drivers/video/backlight/apple-lm3534.c b/drivers/video/backlight/apple-lm3534.c new file mode 100644 index 00000000000000..df11961ac0f7cd --- /dev/null +++ b/drivers/video/backlight/apple-lm3534.c @@ -0,0 +1,245 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * TI LM3534 backlight driver with Apple S5L DWI brightness updates (iPhone 5 / N42). + * + * On N42 (S5L8950X), the Dialog D1972 PMU has no white-LED strings; the display + * backlight is driven by an external TI LM3534 on I2C0 (0x63, ADT "backlight,lm3534", + * using-LM3534 = 1) paired with the SoC's DWI block (0x3f700000, version = 1): + * + * - Enable/disable (AppleLMBacklight, 14G61 0x80e0c914): I2C register 0x10 + * bit 0 switches the output; enabling is followed by a 600 us delay. + * Other bits of 0x10 (boost/OVP/DWI mode set up by iBoot) are preserved. + * - Brightness (AppleS5L8940XDWI::sendWLED, 14G61 0x812d2860): 11-bit level + * 0..2047 encoded for DWI version 1 as (0xa0000000 | (level & 0x7ff)), + * written to DWI+0x14 after polling DWI+0x10 bit 0 clear, then triggered + * by writing 0x71 to DWI+0x10. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LM3534_REG_ENABLE 0x10 +#define LM3534_ENABLE_BIT BIT(0) +#define LM3534_ENABLE_DELAY_US 600 + +#define LM3534_MAX_BRIGHTNESS 2047 +#define LM3534_DEFAULT_BRIGHTNESS 1024 + +#define DWI_SYNC_CTRL 0x10 +#define DWI_SYNC_DATA 0x14 +#define DWI_CTRL_BUSY BIT(0) +#define DWI_CTRL_TRIGGER 0x71 + +struct apple_lm3534 { + struct device *dev; + struct regmap *regmap; + void __iomem *dwi_base; + struct clk *dwi_clk; + u32 dwi_version; + bool enabled; +}; + +static const struct regmap_config apple_lm3534_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = 0xff, +}; + +static u32 apple_dwi_encode_wled(u32 level, u32 version) +{ + level &= 0x7ff; + if (version == 0) + return 0x20000000u | ((level & 0x3u) << 9) | (level >> 2); + return 0xa0000000u | level; +} + +static int apple_lm3534_dwi_send(struct apple_lm3534 *bl, u32 level) +{ + u32 val, word; + int ret; + + if (!bl->dwi_base) + return 0; + + ret = readl_poll_timeout(bl->dwi_base + DWI_SYNC_CTRL, val, + !(val & DWI_CTRL_BUSY), 10, 10000); + if (ret) { + dev_warn_ratelimited(bl->dev, "DWI busy timeout (ctrl=0x%08x)\n", val); + return ret; + } + + word = apple_dwi_encode_wled(level, bl->dwi_version); + writel(word, bl->dwi_base + DWI_SYNC_DATA); + writel(DWI_CTRL_TRIGGER, bl->dwi_base + DWI_SYNC_CTRL); + return 0; +} + +static int apple_lm3534_set_enable(struct apple_lm3534 *bl, bool enable) +{ + int ret; + + if (bl->enabled == enable) + return 0; + + ret = regmap_update_bits(bl->regmap, LM3534_REG_ENABLE, + LM3534_ENABLE_BIT, + enable ? LM3534_ENABLE_BIT : 0); + if (ret) + return ret; + + if (enable) + usleep_range(LM3534_ENABLE_DELAY_US, LM3534_ENABLE_DELAY_US + 200); + + bl->enabled = enable; + return 0; +} + +static int apple_lm3534_update_status(struct backlight_device *bd) +{ + struct apple_lm3534 *bl = bl_get_data(bd); + int level = backlight_get_brightness(bd); + int ret; + + if (!level) + return apple_lm3534_set_enable(bl, false); + + ret = apple_lm3534_set_enable(bl, true); + if (ret) + return ret; + + return apple_lm3534_dwi_send(bl, level); +} + +static const struct backlight_ops apple_lm3534_ops = { + .options = BL_CORE_SUSPENDRESUME, + .update_status = apple_lm3534_update_status, +}; + +static void apple_lm3534_dwi_clk_cleanup(void *clk) +{ + clk_disable_unprepare(clk); + clk_put(clk); +} + +static int apple_lm3534_attach_dwi(struct apple_lm3534 *bl) +{ + struct device_node *dwi_np; + struct resource res; + int ret; + + dwi_np = of_parse_phandle(bl->dev->of_node, "apple,dwi", 0); + if (!dwi_np) { + dev_warn(bl->dev, "no apple,dwi phandle: only on/off control available\n"); + return 0; + } + + bl->dwi_version = 1; + of_property_read_u32(dwi_np, "apple,dwi-version", &bl->dwi_version); + + ret = of_address_to_resource(dwi_np, 0, &res); + if (ret) { + of_node_put(dwi_np); + return ret; + } + + bl->dwi_base = devm_ioremap_resource(bl->dev, &res); + if (IS_ERR(bl->dwi_base)) { + of_node_put(dwi_np); + return PTR_ERR(bl->dwi_base); + } + + bl->dwi_clk = of_clk_get(dwi_np, 0); + of_node_put(dwi_np); + if (IS_ERR(bl->dwi_clk)) { + ret = PTR_ERR(bl->dwi_clk); + if (ret == -ENOENT) { + bl->dwi_clk = NULL; + return 0; + } + return dev_err_probe(bl->dev, ret, "failed to get DWI clock\n"); + } + + ret = clk_prepare_enable(bl->dwi_clk); + if (ret) { + clk_put(bl->dwi_clk); + bl->dwi_clk = NULL; + return ret; + } + + return devm_add_action_or_reset(bl->dev, apple_lm3534_dwi_clk_cleanup, + bl->dwi_clk); +} + +static int apple_lm3534_probe(struct i2c_client *i2c) +{ + struct backlight_properties props = {}; + struct backlight_device *bd; + struct apple_lm3534 *bl; + unsigned int reg_en = 0; + u32 def_level = LM3534_DEFAULT_BRIGHTNESS; + int ret; + + bl = devm_kzalloc(&i2c->dev, sizeof(*bl), GFP_KERNEL); + if (!bl) + return -ENOMEM; + + bl->dev = &i2c->dev; + bl->regmap = devm_regmap_init_i2c(i2c, &apple_lm3534_regmap_config); + if (IS_ERR(bl->regmap)) + return PTR_ERR(bl->regmap); + + ret = apple_lm3534_attach_dwi(bl); + if (ret) + return ret; + + ret = regmap_read(bl->regmap, LM3534_REG_ENABLE, ®_en); + if (ret) + return dev_err_probe(&i2c->dev, ret, "failed to read LM3534 enable reg\n"); + + bl->enabled = !!(reg_en & LM3534_ENABLE_BIT); + of_property_read_u32(i2c->dev.of_node, "default-brightness-level", &def_level); + if (def_level > LM3534_MAX_BRIGHTNESS) + def_level = LM3534_MAX_BRIGHTNESS; + + props.type = BACKLIGHT_RAW; + props.scale = BACKLIGHT_SCALE_NON_LINEAR; + props.max_brightness = LM3534_MAX_BRIGHTNESS; + props.brightness = bl->enabled ? def_level : 0; + props.power = bl->enabled ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF; + + bd = devm_backlight_device_register(&i2c->dev, "apple-lm3534", &i2c->dev, bl, + &apple_lm3534_ops, &props); + if (IS_ERR(bd)) + return PTR_ERR(bd); + + i2c_set_clientdata(i2c, bd); + dev_info(&i2c->dev, "LM3534 reg 0x10=0x%02x (%s), DWI v%u %s\n", + reg_en, bl->enabled ? "on" : "off", bl->dwi_version, + bl->dwi_base ? "attached" : "absent"); + return 0; +} + +static const struct of_device_id apple_lm3534_of_match[] = { + { .compatible = "apple,lm3534-backlight" }, + { } +}; +MODULE_DEVICE_TABLE(of, apple_lm3534_of_match); + +static struct i2c_driver apple_lm3534_driver = { + .driver = { + .name = "apple-lm3534", + .of_match_table = apple_lm3534_of_match, + }, + .probe = apple_lm3534_probe, +}; +module_i2c_driver(apple_lm3534_driver); + +MODULE_DESCRIPTION("Apple TI LM3534 + DWI display backlight driver"); +MODULE_LICENSE("GPL"); From 0300f534b5a7fdbfad793e859bd0d84b7f19b37b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:59 +0100 Subject: [PATCH 4/9] ARM: configs: orchard_applearmv7: Enable the LM3534 backlight MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The iPhone 5's backlight needs it. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/configs/orchard_applearmv7_defconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm/configs/orchard_applearmv7_defconfig b/arch/arm/configs/orchard_applearmv7_defconfig index 8bb1bdafb0cf49..55d68b10e74f18 100644 --- a/arch/arm/configs/orchard_applearmv7_defconfig +++ b/arch/arm/configs/orchard_applearmv7_defconfig @@ -522,6 +522,7 @@ CONFIG_BACKLIGHT_CLASS_DEVICE=y CONFIG_BACKLIGHT_PWM=y CONFIG_BACKLIGHT_AS3711=y CONFIG_BACKLIGHT_APPLE_PMU_WLED=y +CONFIG_BACKLIGHT_APPLE_LM3534=y CONFIG_BACKLIGHT_GPIO=y CONFIG_FRAMEBUFFER_CONSOLE_LEGACY_ACCELERATION=y CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=y From e2099a0223d4a19c0c3374a6e798e48f07a22879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:43:59 +0100 Subject: [PATCH 5/9] power: supply: apple-hdq-battery: Use Apple's HDQ transaction on the S5L8950X MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On the iPhone 5 most bq27540 reads failed ("no answer for 0x14") and the rest decoded to garbage: 65404 mV (0xff7c), and temperatures of 6272 C and -119 C (0xffab, 0x05ff) that the charger then acted on. The driver took "the last 8 frames" after a fixed 4.5 ms sleep, never checked the echo and read 16-bit values without a consistency check, so a missing or extra frame came through as a valid reading. Add "apple,s5l8950x-hdq-battery", which runs the transaction the way the N42 iBEC (12H321) and iOS AppleHDQGasGaugeControl-109.30.4 (_sendBreak, _hdqRead8, hdqRead16) do: - break: UCON bit 4 held for 200 us and cleared by hand, 40 us, drain; - a 0 is sent as 0xC0 (not 0xE0), a 1 as 0xFE; - each echoed frame is read back with a deadline and must equal what was sent; then 8 gauge frames, where a frame >= 0xF9 is a 1; frames with a UERSTAT error are dropped like iBEC's uart_getc does; - 8-bit reads get 4 tries 70 us apart, and 16-bit values are read hi, lo, hi and taken only when both hi reads agree (10 tries). A refresh reads into temporaries, rejects values no bq27540 reports (2500..5000 mV, -40..+90 C, at most 100%, ...), and commits all of them together. After a failure the bus rests for 5 s and the last good values are served for up to 30 s, so one bad transaction never reaches a consumer. Each failure or rejection logs one line with the raw frames and their UERSTAT bytes. The A5 boards ("apple,s5l8940x-hdq-battery") keep the original path. Tested on an iPhone5,2: 100%, 4229 mV, -84 mA, 25.9 C, 1448/1450 mAh (design 1440), 160 cycles. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/power/supply/apple-hdq-battery.c | 317 ++++++++++++++++++++++- 1 file changed, 315 insertions(+), 2 deletions(-) diff --git a/drivers/power/supply/apple-hdq-battery.c b/drivers/power/supply/apple-hdq-battery.c index 785bc0072f11a1..3ae2a1080c7efc 100644 --- a/drivers/power/supply/apple-hdq-battery.c +++ b/drivers/power/supply/apple-hdq-battery.c @@ -19,11 +19,28 @@ * Only reads (command bit 7 clear) are ever sent: register numbers are the * bq27541 family's standard commands, 16 bits as two 8-bit reads. * README ("Battery gauge") has the summary. + * + * The iPhone 5 (S5L8950X, N42) uses the transaction exactly as Apple writes + * it, in its iBEC (12H321, 0xbff143b4 / 0xbff14554 / 0xbff14248) and in iOS + * (AppleHDQGasGaugeControl 109.30.4: _sendBreak, _hdqRead8, hdqRead16): + * + * - break: UCON bit 4 set, 200 us, cleared by hand, 40 us, RX drained; + * - a 0 is sent as 0xC0 (start + six low bits, ~121 us), a 1 as 0xFE; + * - the 8 echoed frames are read back one by one and must equal what was + * sent, then 8 more are the gauge's: a frame of 0xF9 or more is a 1. + * Frames with a receive error (UERSTAT) are dropped, as iBEC does; + * - one 8-bit read is tried up to 4 times, 70 us apart (iOS); + * - a 16-bit value is read hi, lo, hi and taken only when both hi reads + * agree, up to 10 times (iBEC and iOS alike). + * + * That path is selected by "apple,s5l8950x-hdq-battery"; the A5 boards keep + * the original one unchanged. */ #include #include #include #include +#include #include #include #include @@ -36,6 +53,7 @@ #define UFCON 0x08 #define UTRSTAT 0x10 #define UTRSTAT_RXRDY BIT(0) /* Apple's UFSTAT layout differs from Samsung's */ +#define UERSTAT 0x14 /* error of the frame at the RX FIFO head */ #define UTXH 0x20 #define URXH 0x24 #define UBRDIV 0x28 @@ -60,6 +78,39 @@ #define BQ_SOC 0x2c /* % */ #define BQ_DCAP 0x3c /* mAh */ +struct hdq_variant { + bool apple_txn; /* iBEC/iOS transaction (S5L8950X) */ +}; + +static const struct hdq_variant hdq_s5l8940x = { .apple_txn = false }; +static const struct hdq_variant hdq_s5l8950x = { .apple_txn = true }; + +/* Apple transaction (S5L8950X path) */ +#define HDQ_TX1 0xFE +#define HDQ_TX0 0xC0 +#define HDQ_RX1_MIN 0xF9 +#define HDQ_BREAK_US 200 +#define HDQ_BREAK_REC_US 40 +#define HDQ_READ8_TRIES 4 +#define HDQ_READ8_GAP_US 70 +#define HDQ_READ16_TRIES 10 +#define HDQ_TXN_DEADLINE_US 10000 /* 16 frames are ~4 ms */ +#define HDQ_TRACE_LEN 24 /* 16 frames + dropped error frames */ +#define HDQ_STALE_OK (30 * HZ) /* last good values stay served */ + +/* the last failed (or rejected) 8-bit transaction, for the log */ +struct hdq_trace { + u8 reg; + u8 n; + const char *why; + u8 rx[HDQ_TRACE_LEN]; + u8 err[HDQ_TRACE_LEN]; +}; + +struct hdq_regs { + int soc, volt, ai, temp, rm, fcc, flags, cyct, dcap; +}; + struct hdq_battery { struct device *dev; void __iomem *base; @@ -68,6 +119,12 @@ struct hdq_battery { unsigned long stamp; /* jiffies of the last refresh */ int soc, volt, ai, temp, rm, fcc, flags, cyct, dcap; bool valid; + /* S5L8950X path only */ + const struct hdq_variant *var; + unsigned long fail_stamp; /* jiffies of the last failed refresh */ + int fail_ret; + unsigned int fails, retries; + struct hdq_trace trace; }; static void hdq_drain(struct hdq_battery *b) @@ -120,6 +177,235 @@ static int hdq_read16(struct hdq_battery *b, u8 reg) return -EIO; } +/* --- S5L8950X: Apple's transaction ------------------------------------- */ + +static void hdq_apple_drain(struct hdq_battery *b) +{ + int i; + + for (i = 0; i < 512 && (readl(b->base + UTRSTAT) & UTRSTAT_RXRDY); i++) { + readl(b->base + UERSTAT); + readl(b->base + URXH); + } +} + +/* iBEC 0xbff14554, iOS _sendBreak: the break bit is held and cleared by hand */ +static void hdq_apple_break(struct hdq_battery *b) +{ + writel(readl(b->base + UCON) | UCON_SBREAK, b->base + UCON); + udelay(HDQ_BREAK_US); + writel(readl(b->base + UCON) & ~UCON_SBREAK, b->base + UCON); + udelay(HDQ_BREAK_REC_US); + hdq_apple_drain(b); +} + +/* one frame and its UERSTAT (read first, as iBEC does), or -ETIMEDOUT */ +static int hdq_apple_rx(struct hdq_battery *b, ktime_t deadline, u8 *c, u8 *err) +{ + for (;;) { + if (readl(b->base + UTRSTAT) & UTRSTAT_RXRDY) { + *err = readl(b->base + UERSTAT) & 0xff; + *c = readl(b->base + URXH) & 0xff; + return 0; + } + if (ktime_after(ktime_get(), deadline)) + return -ETIMEDOUT; + /* the 16-deep FIFO holds a whole transaction */ + usleep_range(100, 200); + } +} + +/* one transaction: break, 8 command bits, their echo, the gauge's 8 bits */ +static int hdq_apple_txn8(struct hdq_battery *b, u8 reg, u8 *val) +{ + struct hdq_trace *t = &b->trace; + ktime_t deadline; + u8 tx[8], c, e; + int i, got = 0, v = 0; + + t->reg = reg; + t->n = 0; + t->why = NULL; + for (i = 0; i < 8; i++) /* LSB first; bit 7 clear: read */ + tx[i] = ((reg >> i) & 1) ? HDQ_TX1 : HDQ_TX0; + + hdq_apple_break(b); + for (i = 0; i < 8; i++) + writel(tx[i], b->base + UTXH); + + deadline = ktime_add_us(ktime_get(), HDQ_TXN_DEADLINE_US); + while (got < 16) { + if (t->n >= HDQ_TRACE_LEN) { + t->why = "too many error frames"; + return -EIO; + } + if (hdq_apple_rx(b, deadline, &c, &e)) { + t->why = got < 8 ? "echo timeout" : "no answer"; + return -EIO; + } + t->rx[t->n] = c; + t->err[t->n] = e; + t->n++; + if (e) + continue; /* dropped, as iBEC's uart_getc does */ + if (got < 8 && c != tx[got]) { + t->why = "echo mismatch"; + return -EIO; + } + if (got >= 8 && c >= HDQ_RX1_MIN) + v |= 1 << (got - 8); + got++; + } + *val = v; + return 0; +} + +/* iOS _hdqRead8 around it: up to 4 tries, 70 us apart */ +static int hdq_apple_read8(struct hdq_battery *b, u8 reg) +{ + u8 v; + int i; + + for (i = 0; i < HDQ_READ8_TRIES; i++) { + if (i) { + b->retries++; + udelay(HDQ_READ8_GAP_US); + } + if (!hdq_apple_txn8(b, reg, &v)) + return v; + } + return -EIO; +} + +/* iBEC 0xbff14248, iOS hdqRead16: hi, lo, hi again until both hi agree */ +static int hdq_apple_read16(struct hdq_battery *b, u8 reg) +{ + int hi, lo, hi2, i; + + for (i = 0; i < HDQ_READ16_TRIES; i++) { + if (i) { + b->retries++; + udelay(10); + } + hi = hdq_apple_read8(b, reg + 1); + if (hi < 0) + return hi; + lo = hdq_apple_read8(b, reg); + if (lo < 0) + return lo; + hi2 = hdq_apple_read8(b, reg + 1); + if (hi2 < 0) + return hi2; + if (hi == hi2) + return lo | hi << 8; + } + b->trace.reg = reg; + b->trace.why = "high byte unstable (hi lo hi)"; + b->trace.n = 3; + b->trace.rx[0] = hi; + b->trace.rx[1] = lo; + b->trace.rx[2] = hi2; + memset(b->trace.err, 0, 3); + return -EIO; +} + +/* a value no bq27540 reports means the wire lied, whatever the echo said */ +static const char *hdq_apple_implausible(const struct hdq_regs *r) +{ + if (r->soc > 100) + return "soc"; + if (r->volt < 2500 || r->volt > 5000) + return "volt"; + if (r->temp < 2731 - 400 || r->temp > 2731 + 900) /* -40..+90 C */ + return "temp"; + if (r->rm > 0x7fff || r->fcc > 0x7fff) + return "charge"; + if (r->dcap < 100 || r->dcap > 0x7fff) + return "dcap"; + if (r->cyct > 10000) + return "cycles"; + return NULL; +} + +/* + * Everything at once into temporaries, checked, then committed together. + * After a failure the bus is left alone for 5 s, and the last good values + * are served for up to 30 s, so one bad transaction reaches no consumer. + */ +static int hdq_apple_refresh(struct hdq_battery *b) +{ + struct hdq_regs r; + struct { u8 reg; int *dst; } m[] = { + { BQ_SOC, &r.soc }, { BQ_VOLT, &r.volt }, { BQ_AI, &r.ai }, + { BQ_TEMP, &r.temp }, { BQ_RM, &r.rm }, { BQ_FCC, &r.fcc }, + { BQ_FLAGS, &r.flags }, { BQ_CYCT, &r.cyct }, { BQ_DCAP, &r.dcap }, + }; + const struct hdq_trace *t = &b->trace; + const char *bad; + int i, v; + + if (b->valid && time_before(jiffies, b->stamp + 5 * HZ)) + return 0; + if (b->fail_ret && time_before(jiffies, b->fail_stamp + 5 * HZ)) + goto stale; + + for (i = 0; i < ARRAY_SIZE(m); i++) { + v = hdq_apple_read16(b, m[i].reg); + if (v < 0) { + b->fails++; + dev_warn_ratelimited(b->dev, + "refresh failed: reg %#04x (byte %#04x) %s; rx %u: %*ph; uerstat: %*ph; fails %u retries %u\n", + m[i].reg, t->reg, t->why ?: "?", t->n, t->n, t->rx, + t->n, t->err, b->fails, b->retries); + b->fail_ret = v; + goto failed; + } + *m[i].dst = v; + } + + bad = hdq_apple_implausible(&r); + if (bad) { + b->fails++; + dev_warn_ratelimited(b->dev, + "refresh rejected (%s): soc %04x volt %04x ai %04x temp %04x rm %04x fcc %04x flags %04x cyc %04x dcap %04x; fails %u retries %u\n", + bad, r.soc, r.volt, r.ai, r.temp, r.rm, r.fcc, r.flags, + r.cyct, r.dcap, b->fails, b->retries); + b->fail_ret = -EIO; + goto failed; + } + + b->soc = r.soc; + b->volt = r.volt; + b->ai = (s16)r.ai; + b->temp = r.temp; + b->rm = r.rm; + b->fcc = r.fcc; + b->flags = r.flags; + b->cyct = r.cyct; + b->dcap = r.dcap; + b->stamp = jiffies; + b->valid = true; + b->fail_ret = 0; + return 0; + +failed: + b->fail_stamp = jiffies; +stale: + if (b->valid && time_before(jiffies, b->stamp + HDQ_STALE_OK)) + return 0; + return b->fail_ret; +} + +/* UART5 as the boot chain left it, and whether the break bit clears itself */ +static void hdq_apple_probe_log(struct hdq_battery *b) +{ + u32 ulcon = readl(b->base + ULCON), ucon = readl(b->base + UCON); + u32 ufcon = readl(b->base + UFCON), ubrdiv = readl(b->base + UBRDIV); + + dev_dbg(b->dev, "UART5 as found: ULCON %#x UCON %#x UFCON %#x UBRDIV %#x\n", + ulcon, ucon, ufcon, ubrdiv); +} + /* everything at once, at most every 5 s: a 16-bit read is ~10 ms of bus */ static int hdq_refresh(struct hdq_battery *b) { @@ -130,6 +416,8 @@ static int hdq_refresh(struct hdq_battery *b) }; int i, v; + if (b->var->apple_txn) + return hdq_apple_refresh(b); if (b->valid && time_before(jiffies, b->stamp + 5 * HZ)) return 0; for (i = 0; i < ARRAY_SIZE(r); i++) { @@ -236,6 +524,9 @@ static int hdq_probe(struct platform_device *pdev) if (!b) return -ENOMEM; b->dev = &pdev->dev; + b->var = of_device_get_match_data(&pdev->dev); + if (!b->var) + b->var = &hdq_s5l8940x; mutex_init(&b->lock); b->base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(b->base)) @@ -246,17 +537,38 @@ static int hdq_probe(struct platform_device *pdev) if (IS_ERR(clk)) return dev_err_probe(&pdev->dev, PTR_ERR(clk), "UART5 power\n"); + if (b->var->apple_txn) + hdq_apple_probe_log(b); + writel(HDQ_ULCON, b->base + ULCON); writel(HDQ_UFCON, b->base + UFCON); writel(HDQ_UBRDIV, b->base + UBRDIV); writel(0, b->base + UFRACVAL); writel(HDQ_UCON, b->base + UCON); + if (b->var->apple_txn) { + /* does this UART end a break by itself? Apple never relies on it */ + writel(HDQ_UCON | UCON_SBREAK, b->base + UCON); + udelay(HDQ_BREAK_US); + dev_dbg(&pdev->dev, "break bit %s after %d us\n", + readl(b->base + UCON) & UCON_SBREAK ? "still set" : "cleared by the UART", + HDQ_BREAK_US); + writel(HDQ_UCON, b->base + UCON); + udelay(HDQ_BREAK_REC_US); + hdq_apple_drain(b); + } + mutex_lock(&b->lock); soc = hdq_refresh(b) ? -1 : b->soc; mutex_unlock(&b->lock); if (soc < 0) dev_warn(&pdev->dev, "the gauge does not answer yet\n"); + else if (b->var->apple_txn) + dev_info(&pdev->dev, "bq27540: %d%%, %d mV, %d mA, %s%d.%d C, %d/%d mAh (design %d), %d cycles, flags %#06x, retries %u\n", + b->soc, b->volt, b->ai, b->temp < 2731 ? "-" : "", + abs(b->temp - 2731) / 10, abs(b->temp - 2731) % 10, + b->rm, b->fcc, b->dcap, + b->cyct, b->flags, b->retries); else dev_info(&pdev->dev, "bq27540: %d%%, %d mV, %d mA, %d/%d mAh, %d cycles\n", b->soc, b->volt, b->ai, b->rm, b->fcc, b->cyct); @@ -268,7 +580,8 @@ static int hdq_probe(struct platform_device *pdev) } static const struct of_device_id hdq_of_match[] = { - { .compatible = "apple,s5l8940x-hdq-battery" }, + { .compatible = "apple,s5l8950x-hdq-battery", .data = &hdq_s5l8950x }, + { .compatible = "apple,s5l8940x-hdq-battery", .data = &hdq_s5l8940x }, { } }; MODULE_DEVICE_TABLE(of, hdq_of_match); @@ -282,5 +595,5 @@ static struct platform_driver hdq_driver = { }; module_platform_driver(hdq_driver); -MODULE_DESCRIPTION("bq27540 gas gauge on HDQ through UART5 (iPad mini 1)"); +MODULE_DESCRIPTION("bq27540 gas gauge on HDQ through UART5 (A5 iPads, iPhone 5)"); MODULE_LICENSE("GPL"); From 5d5d7853eacc75ffb4c5c75b74943fdad66629b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:44:00 +0100 Subject: [PATCH 6/9] Input: apple-z2: Boot the N42 with the in-kernel N1 loader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The P105 already uploads its N1 firmware in the kernel. The N42 supplies a raw HBPP firmware packet and separate proximity and panel calibration blobs. Load the blobs from the DT or firmware files, and take the reference clock divider from apple,ref-clk-div-val. The scan-start SET report (0xbf) takes its value from the per-board apple,scan-start-report property. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/input/touchscreen/apple-z2.c | 73 +++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/drivers/input/touchscreen/apple-z2.c b/drivers/input/touchscreen/apple-z2.c index 1e5bd28bafde71..b060b0895ef84f 100644 --- a/drivers/input/touchscreen/apple-z2.c +++ b/drivers/input/touchscreen/apple-z2.c @@ -91,6 +91,8 @@ struct hx_touch_data { struct delayed_work lost_work; unsigned lost_frame; const char *firmware_name; + bool raw_packet; + u32 ref_clk_div; u32 execute_addr; u32 cal_addr; u32 fll_mval; @@ -819,7 +821,7 @@ static int hx_touch_hbpp_upload(struct hx_touch_data *hxt, const u8 *packet, } static int hx_touch_hbpp_cal_data(struct hx_touch_data *hxt, - const struct firmware *cal) + const struct firmware *cal, u32 addr) { const u8 ack[] = { 0x1a, 0xa1 }; u8 *pkt = hxt->tx_data; @@ -832,7 +834,7 @@ static int hx_touch_hbpp_cal_data(struct hx_touch_data *hxt, padded = ALIGN(len, 4); put_unaligned_be32(0x18e13001, pkt); put_unaligned_be16(padded / 4, pkt + 4); - hx_touch_hbpp_put32(pkt + 6, hxt->cal_addr + off); + hx_touch_hbpp_put32(pkt + 6, addr + off); put_unaligned_be16(hx_touch_sum16(pkt + 4, 6), pkt + 10); sum = 0; @@ -926,7 +928,7 @@ static int hx_touch_n1_calibrate(struct hx_touch_data *hxt) u32 addr, value; } regs[] = { { 0x10003060, hxt->fll_mval }, /* FLL M value */ - { 0x1000305c, 0x20 }, /* reference clock divider */ + { 0x1000305c, hxt->ref_clk_div }, /* reference clock divider */ { 0x10003058, 6 }, { 0x10003000, 3 }, /* constant calibration */ { 0x10003518, 1 }, /* clk32 enable */ @@ -1076,6 +1078,32 @@ static int hx_touch_n1_start_scan(struct hx_touch_data *hxt) return 0; } +/* The bootloader can supply per-unit calibration directly in the DT. */ +static int hx_touch_n42_cal(struct hx_touch_data *hxt, const char *prop, + const char *name, u32 addr, size_t limit) +{ + struct device *dev = &hxt->spi->dev; + const struct firmware *file = NULL; + struct firmware blob = {}; + int len, ret; + + blob.data = of_get_property(dev->of_node, prop, &len); + if (blob.data && len > 0 && memchr_inv(blob.data, 0, len)) { + blob.size = len; + } else { + ret = firmware_request_nowarn(&file, name, dev); + if (ret) + return ret == -ENOENT ? 0 : ret; + blob = *file; + } + if (!blob.size || blob.size > limit) + ret = -EINVAL; + else + ret = hx_touch_hbpp_cal_data(hxt, &blob, addr); + release_firmware(file); + return ret; +} + static int hx_touch_n1_boot(struct hx_touch_data *hxt, const u8 *packet, size_t len, const struct firmware *cal) { @@ -1107,9 +1135,17 @@ static int hx_touch_n1_boot(struct hx_touch_data *hxt, const u8 *packet, ret = hx_touch_hbpp_upload(hxt, packet, len); if (ret) return ret; - /* P105 has no prox-calibration in its ADT. */ - if (cal) { - ret = hx_touch_hbpp_cal_data(hxt, cal); + if (hxt->raw_packet) { + ret = hx_touch_n42_cal(hxt, "apple,z2-prox-cal-blob", + "apple/z2-prox-cal.bin", 0x10009600, 128); + if (ret) + return ret; + ret = hx_touch_n42_cal(hxt, "apple,z2-cal-blob", "apple/z2-cal.bin", + hxt->cal_addr, 0x600); + if (ret) + return ret; + } else if (cal) { + ret = hx_touch_hbpp_cal_data(hxt, cal, hxt->cal_addr); if (ret) return ret; } @@ -1133,6 +1169,25 @@ static void hx_touch_boot_work(struct work_struct *work) ret = request_firmware(&fw, hxt->firmware_name, dev); if (ret) return; + if (hxt->raw_packet) { + ret = -EINVAL; + if (fw->size >= 20 && fw->size <= SZ_128K && + !memcmp(fw->data, "\x18\xe1\x30\x01", 4)) { + ret = hx_touch_power_on(hxt); + if (!ret) { + ret = hx_touch_n1_boot(hxt, fw->data, fw->size, NULL); + if (ret) + hx_touch_power_off(hxt); + } + } + release_firmware(fw); + if (ret) { + dev_err(dev, "touch controller boot failed: %pe\n", ERR_PTR(ret)); + return; + } + packet = NULL; + goto ready; + } len = hx_touch_decode_mtprops(fw, &packet); release_firmware(fw); if (len < 0) { @@ -1162,6 +1217,7 @@ static void hx_touch_boot_work(struct work_struct *work) goto release_cal; } + ready: mutex_lock(&hxt->mutex); hxt->ready = 1; hx_touch_read_all_reports(hxt); @@ -1245,6 +1301,9 @@ static int hx_touch_spi_probe(struct spi_device *spi) if (device_get_match_data(&spi->dev)) { struct device_node *np = spi->dev.of_node; + hxt->raw_packet = of_device_is_compatible(np, "apple,n42-z2-multitouch"); + hxt->ref_clk_div = 0x20; + of_property_read_u32(np, "apple,ref-clk-div-val", &hxt->ref_clk_div); if (of_property_read_string(np, "firmware-name", &hxt->firmware_name) || of_property_read_u32(np, "apple,firmware-execute-address", &hxt->execute_addr) || of_property_read_u32(np, "apple,calibration-address", &hxt->cal_addr) || @@ -1379,6 +1438,7 @@ static void hx_touch_spi_remove(struct spi_device *spi) static const bool hx_touch_n1 = true; static const struct of_device_id hx_touch_of_match[] = { + { .compatible = "apple,n42-z2-multitouch", .data = &hx_touch_n1 }, { .compatible = "apple,p105-z2-multitouch", .data = &hx_touch_n1 }, { .compatible = "apple,z2-multitouch" }, { }, @@ -1386,6 +1446,7 @@ static const struct of_device_id hx_touch_of_match[] = { MODULE_DEVICE_TABLE(of, hx_touch_of_match); static const struct spi_device_id hx_touch_spi_ids[] = { + { "n42-z2-multitouch" }, { "p105-z2-multitouch" }, { "z2-multitouch" }, { } From 29045abcbb8e4ac9de9a35eea86ecee3ce7e1a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:44:00 +0100 Subject: [PATCH 7/9] ARM: dts: apple: iPhone 5: Describe the peripherals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add what the N41 and N42 share to s5l8950x-5.dtsi, from the iPhone5,2 iOS 8.4.1 (12H321) ADT and the 10.3.4 (14G61) ADT and kernelcache: - gpio-keys: Home, Hold, Volume Up and Down, and the ringer switch; - I2C0: the D1972 PMU (0x74) with its charger, the touch power switches and WL_REG_ON; the LM3534 backlight (0x63) and the DWI block; - I2C1: the LIS3DSH accelerometer and AK8963 compass (no driver binds them yet); - SPI1: the Gen1 Z2 touch controller, its 32768 Hz PWM clock, and the firmware name and clock values that let the kernel boot it; - UART3: the BCM4334 Bluetooth; - UART5 as HDQ for the bq27540 gauge, with Apple's transaction; - EHCI with the BCM4334 Wi-Fi on HSIC port 4; - the CLCD and DSIM, disabled until there is a display driver. Each consumer's PMGR gate is enabled next to it. The I2C controllers take their gates as their clock, so the driver holds them from probe. The wifi0, bluetooth0 and touchscreen0 aliases let the boot loader fill in the unit's MAC and Bluetooth addresses and its touch calibration. Without the Wi-Fi address brcmfmac would use the NVRAM placeholder 02:00:00:00:00:00. Tested on an iPhone5,2: WPA2 on 5 GHz with DHCP, DNS and HTTP; the Bluetooth patchram loads; the gauge, charger, backlight and touch work as their driver commits describe; the buttons probe. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/boot/dts/apple/s5l8950x-5.dtsi | 402 ++++++++++++++++++++++++ 1 file changed, 402 insertions(+) diff --git a/arch/arm/boot/dts/apple/s5l8950x-5.dtsi b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi index 0d8858f3ece075..f5700a87df45e1 100644 --- a/arch/arm/boot/dts/apple/s5l8950x-5.dtsi +++ b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi @@ -13,6 +13,56 @@ / { aliases { serial0 = &uart6; + bluetooth0 = &bluetooth; + touchscreen0 = &touchscreen; + wifi0 = &wifi; + }; + + /* + * Physical buttons and mute switch (ADT /buttons): active-low on SoC + * GPIOs 0..4, identical layout to P105 (iPad mini 1). + * Codes: KEY_HOMEPAGE 172, KEY_POWER 116, KEY_VOLUMEUP 115, + * KEY_VOLUMEDOWN 114, EV_SW 5, SW_MUTE_DEVICE 14. + */ + gpio-keys { + compatible = "gpio-keys"; + autorepeat; + + home { + label = "Home"; + gpios = <&gpio 0 1>; + linux,code = <172>; + debounce-interval = <20>; + }; + + hold { + label = "Hold"; + gpios = <&gpio 1 1>; + linux,code = <116>; + debounce-interval = <20>; + }; + + volume-up { + label = "Volume Up"; + gpios = <&gpio 2 1>; + linux,code = <115>; + debounce-interval = <20>; + }; + + volume-down { + label = "Volume Down"; + gpios = <&gpio 3 1>; + linux,code = <114>; + debounce-interval = <20>; + }; + + ringer-switch { + label = "Ringer Switch"; + gpios = <&gpio 4 1>; + linux,input-type = <5>; + linux,code = <14>; + debounce-interval = <50>; + }; }; }; @@ -28,3 +78,355 @@ &uart6 { status = "okay"; }; + +&gpio { + /* + * UART3 (Bluetooth): ADT function-tx = 0x0400 (pin 32), function-rts = + * 0x0402 (pin 34), so TX=32, RX=33, RTS=34, CTS=35. + */ + uart3_pins: uart3-pins { + pins = "gpio32", "gpio33", "gpio34", "gpio35"; + function = "periph"; + }; +}; + +/* + * The PMGR gates of the consumers below (s5l8950x.dtsi keeps every gate + * disabled until a board has a consumer for it). Each consumer holds its + * gate from probe: I2C, SPI and HDQ enable their clock at probe, the + * LM3534 backlight enables the DWI's, and the UART3 serial port its own + * while the Bluetooth line discipline has it open. + */ +&i2c0_ps { + status = "okay"; +}; + +&i2c1_ps { + status = "okay"; +}; + +&spi1_ps { + status = "okay"; +}; + +&uart3_ps { + status = "okay"; +}; + +&uart5_ps { + status = "okay"; +}; + +/* + * UART3: BCM4334 Bluetooth half (ADT uart3/bluetooth, "bluetooth,n88"). + * function-power_enable = PMU GPIO 8 (0x68 + 8 = 0x70) + * function-bt_wake = SoC GPIO 7 (0x0007) + * Patchram: "BCM4334B1 37.4 MHz Centennial Murata" -> brcm/BCM4334B0.hcd. + * local-bd-address is filled at boot by m1n1 via the bluetooth0 alias. + */ +&uart3 { + pinctrl-names = "default"; + pinctrl-0 = <&uart3_pins>; + status = "okay"; + + bluetooth: bluetooth { + compatible = "brcm,bcm4334-bt"; + shutdown-gpios = <&pmu 8 0>; + device-wakeup-gpios = <&gpio 7 0>; + max-speed = <1500000>; + local-bd-address = [00 00 00 00 00 00]; + }; +}; + +/* + * Attach Wi-Fi WL_REG_ON (PMU GPIO 10 = reg 0x72) to the shared S5L8950X + * USB/HSIC PHY defined in s5l8950x.dtsi. + */ +&otgphy { + hsic-supply = <&wlan_reg_on>; +}; + +&soc { + /* + * DWI power-state switch (PMGR device-clocks index 0x4d -> 0xf101134). + * SPI1/UART3/UART5/I2C* power-state gates come from s5l8950x.dtsi. + */ + dwi_ps: dwi-ps@f101134 { + compatible = "apple,s5l8940x-pmgr-clk-gate"; + #clock-cells = <0>; + reg = <0xf101134 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "dwi_ps"; + }; + + /* + * Touch 32768 Hz reference clock: PWM channel 2 (live ADT + * /arm-io/pwm/grape-clk, reg = 2, default-hz = 32768; multi-touch + * function-clock_enable = Cmwp). PMGR gate is index 0x49 = 0xf101124. + * + * Note: the vibrator (live ADT /arm-io/pwm/vibrator, PWM ch 1, + * default-hz = 32000, enable = SoC GPIO 95 = 0x0b07) shares this + * 0x33500000 MMIO window and is deferred until PWM is split into a + * shared provider. + */ + touchclk: touchclk@3500000 { + compatible = "apple,s5l8940x-pwm-clk"; + #clock-cells = <0>; + reg = <0x3500000 0x1000>, <0xf101124 0x4>; + clocks = <&refclk24mhz>; + clock-output-names = "touchclk"; + clock-frequency = <32768>; + apple,pwm-channel = <2>; + }; + + /* + * Digital Wire Interface (live ADT /arm-io/dwi@F700000, reg 0xf700000, + * version = 1): carries 11-bit brightness commands to the external TI + * LM3534 backlight. iBoot leaves it set up (+0x00 = 1, +0x04 = 0x78) + * with its last level in +0x14. 0xf107000 lies past the PMGR's range: + * no level ever reached the LM3534, and once dark the panel never lit + * again. + */ + dwi: dwi@f700000 { + compatible = "apple,s5l8950x-dwi", "apple,s5l8940x-dwi"; + reg = <0xf700000 0x1000>; + clocks = <&dwi_ps>; + apple,dwi-version = <1>; + }; + + /* + * I2C0: Dialog D1972 PMU (0x74), TI LM3534 backlight (0x63), plus + * audio/gyro/tristar (deferred). Confirmed from live 12H321 ADT. + */ + i2c0: i2c@3200000 { + compatible = "apple,s5l8940x-i2c"; + reg = <0x3200000 0x1000>; + clock-frequency = <100000>; + interrupts = <18>; + clocks = <&i2c0_ps>; + clock-names = "i2c"; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + pmu: pmu@74 { + compatible = "apple,pmu-d1972"; + reg = <0x74>; + system-power-controller; + gpio-controller; + #gpio-cells = <2>; + #address-cells = <1>; + #size-cells = <0>; + + /* + * Live ADT multi-touch function-power_ana = 0x020c (LDO 12): + * AppleD1972PMU LDO table (0x810ece34) gives en_reg = 0x22, + * en_mask = 0x04. Whether the scan also needs LDO 15 (0x22 + * bit 0x40), as on the iPad mini 1, is not known yet. + */ + touch_ana: touch_ana@22 { + compatible = "apple,pmu-pwrsw"; + reg = <0x22>; + mask = <0x04>; + enable = <0x04>; + }; + + /* + * Live ADT multi-touch function-power_ldo = 0x0213 (LDO 19): + * AppleD1972PMU LDO table gives en_reg = 0x23, en_mask = 0x40. + */ + touch_ldo: touch_ldo@23 { + compatible = "apple,pmu-pwrsw"; + reg = <0x23>; + mask = <0x40>; + enable = <0x40>; + }; + + /* + * BCM4334 WL_REG_ON: live ADT wlan function-device_reset is + * PMU GPIO 10 (0x68 + 10 = 0x72), bit 1 is output level. + */ + wlan_reg_on: wlan_reg_on@72 { + compatible = "apple,pmu-pwrsw"; + reg = <0x72>; + mask = <0x02>; + enable = <0x02>; + }; + + /* + * D1972 USB input current limit (register 0x3f, 10 mA/LSB + * from 70 mA; VBUS online = 0x0a bit 6; sag = 0x03). + * Log-only until apple_pmu_charger.d1972_armed=1. + */ + charger@3f { + compatible = "apple,pmu-d1972-charger"; + reg = <0x3f>; + }; + }; + + /* + * External TI LM3534 display backlight (live ADT "backlight,lm3534", + * using-LM3534 = 1): enable via I2C reg 0x10 bit 0, 11-bit + * brightness via DWI block. + */ + backlight: lm3534@63 { + compatible = "apple,lm3534-backlight"; + reg = <0x63>; + apple,dwi = <&dwi>; + }; + }; + + /* + * I2C1: motion sensors (LIS3DSH accelerometer @ 0x1d, AK8963 compass @ 0x0c). + */ + i2c1: i2c@3300000 { + compatible = "apple,s5l8940x-i2c"; + reg = <0x3300000 0x1000>; + clock-frequency = <100000>; + interrupts = <19>; + clocks = <&i2c1_ps>; + clock-names = "i2c"; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + accelerometer@1d { + compatible = "st,lis3dsh-accel"; + reg = <0x1d>; + }; + + compass@c { + compatible = "asahi-kasei,ak8963"; + reg = <0x0c>; + }; + }; + + /* + * SPI1: Gen1 Z2 multi-touch controller (live ADT multi-touch,n41; + * AppleMultitouchN1SPI C1F57,1, Family 0x57, 25x10, 5000x8900, N42.mtprops). + * GPIOs (port << 8 | pin): + * function-enable_cs = 0x0601 -> SoC GPIO 49 + * function-reset = 0x1c07 -> SoC GPIO 231 + * ATTN interrupt = 0xc9 -> SoC GPIO 201 (active-low) + */ + spi1: spi@2100000 { + compatible = "apple,s5l8940x-spi"; + reg = <0x2100000 0x1000>; + interrupts = <29>; + clocks = <&spi1_ps>; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + /* + * The kernel boots the digitizer, as iOS 8.4.1's + * AppleMultitouchN1SPI does. firmware-name is the "Constructed + * Firmware" of the C1F57,1 entry in the firmware's N42.mtprops. + * The panel and prox calibration come from the boot loader + * (ADT multi-touch-calibration and prox-calibration, copied + * through the touchscreen0 alias). The clock values are those + * of the "AppleMultitouchSPI - C1,N41" personality: fll-mval + * 6099, ref-clk-div-val 2, clk32-clock-enable-val 1. + */ + touchscreen: touch@0 { + compatible = "apple,n42-z2-multitouch"; + reg = <0>; + spi-max-frequency = <5000000>; + generation = <1>; + firmware-name = "apple/z2-n42.bin"; + apple,fll-mval = <0x17d3>; + apple,ref-clk-div-val = <2>; + apple,firmware-execute-address = <0x10003400>; + apple,calibration-address = <0x10009000>; + apple,scan-start-report = [9b 0b 0b 02]; + hv-supply = <&touch_ana>; + core-supply = <&touch_ldo>; + reset-gpios = <&gpio 231 0>; + cs-gpios = <&gpio 49 0>; + irq-gpios = <&gpio 201 1>; + touchscreen-inverted-y; + clocks = <&touchclk>; + }; + }; + + /* + * UART5 as 1-wire HDQ for the TI bq27540 gas gauge (live ADT + * uart5/gas-gauge, pin 44 = 0x0504, PMGR gate uart5_ps @ 0xf10110c). + * Delete the disabled UART5 serial node from s5l8950x.dtsi so there + * is no duplicate unit-address at @2a00000. + */ + /delete-node/ serial@2a00000; + + /* + * s5l8950x: the driver runs Apple's own HDQ transaction here (iBEC + * and AppleHDQGasGaugeControl): 0xC0 for a 0, an echo check, 0xF9 + * threshold, hi-lo-hi 16-bit reads. + */ + battery: hdq@2a00000 { + compatible = "apple,s5l8950x-hdq-battery", "apple,s5l8940x-hdq-battery"; + reg = <0x2a00000 0x1000>; + clocks = <&uart5_ps>; + status = "okay"; + }; + + /* + * EHCI host for BCM4334 B3 Murata Wi-Fi (M=HEIN m=6.9 V=m, 0a5c:bd1a, + * centennial.trx + heineken-m-ms.txt) on HSIC port 4. + * Port 2 (cellular baseband) is intentionally masked out. + */ + ehci: usb@6400000 { + compatible = "apple,s5l8950x-ehci", "apple,s5l8940x-ehci", + "generic-ehci"; + reg = <0x6400000 0x10000>; + interrupts = <12>; + phys = <&otgphy 1>; + phy-names = "usb"; + apple,hsic-ports = <0x8>; + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + /* + * brcm,bcm4329-fmac lets brcmfmac read local-mac-address from + * this node; m1n1 fills it from the ADT's mac-address-wifi0 + * through the wifi0 alias (otherwise the NVRAM placeholder + * 02:00:00:00:00:00 is the MAC). + */ + wifi: wifi@4 { + compatible = "usba5c,bd1a", "brcm,bcm4329-fmac"; + reg = <4>; + /* Live ADT wlan: HOST_READY 0x0606 = 54, DEVICE_READY 0x0701 = 57 */ + apple,host-ready-gpios = <&gpio 54 0>; + apple,device-ready-gpios = <&gpio 57 0>; + }; + }; + + /* + * Display pipeline (live AppleMobileCLCD: 640x1136, stride 2560, BGRA / + * a8r8g8b8, PixelClock 102.6 MHz; lcd,pinot reset GPIO 202, LDO 13). + * First-stage display uses framebuffer0 (simple-framebuffer) in + * s5l8950x.dtsi; CLCD and MIPI-DSIM stay disabled until KMS bring-up. + */ + clcd: display-controller@a104000 { + compatible = "apple,s5l8950x-clcd", "apple,s5l8940x-clcd"; + reg = <0xa104000 0x1000>, <0x9200000 0x1000>; + status = "disabled"; + }; + + dsim: dsi@9500000 { + compatible = "apple,s5l8950x-mipi-dsim", "apple,s5l8940x-mipi-dsim", + "samsung,mipi-dsim-1"; + reg = <0x9500000 0x1000>; + interrupts = <41>; + #address-cells = <1>; + #size-cells = <0>; + apple,lanes = <4>; + status = "disabled"; + + panel@0 { + compatible = "apple,lcd-pinot"; + reg = <0>; + status = "disabled"; + }; + }; +}; From 0b8d3eb53db893ce0ab32aaf58f787be6ab811e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Tue, 1 Dec 2026 18:02:51 +0100 Subject: [PATCH 8/9] dt-bindings: input: Describe the N42 Z2 touchscreen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Assisted-by: LLM Signed-off-by: Berkay Kösebay --- .../touchscreen/apple,p105-z2-multitouch.yaml | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml b/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml index 6366261e5799d6..b27edd46162987 100644 --- a/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml +++ b/Documentation/devicetree/bindings/input/touchscreen/apple,p105-z2-multitouch.yaml @@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/input/touchscreen/apple,p105-z2-multitouch.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: Apple P105 Z2 N1 touchscreen +title: Apple Z2 N1 touchscreen maintainers: - Alexandre Figueiredo <17xande@gmail.com> @@ -15,7 +15,9 @@ allOf: properties: compatible: - const: apple,p105-z2-multitouch + enum: + - apple,n42-z2-multitouch + - apple,p105-z2-multitouch reg: maxItems: 1 @@ -43,7 +45,7 @@ properties: firmware-name: maxItems: 1 - description: mtprops file containing the preconstructed HBPP DATA packet + description: HBPP DATA packet on N42 or an mtprops file containing it on P105 generation: $ref: /schemas/types.yaml#/definitions/uint32 @@ -62,6 +64,21 @@ properties: $ref: /schemas/types.yaml#/definitions/uint32 description: Frequency-locked loop multiplier used to calibrate the digitizer + apple,ref-clk-div-val: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Reference clock divider for the digitizer calibration + default: 32 + + apple,z2-prox-cal-blob: + $ref: /schemas/types.yaml#/definitions/uint8-array + maxItems: 128 + description: Per-device proximity calibration supplied by the bootloader + + apple,z2-cal-blob: + $ref: /schemas/types.yaml#/definitions/uint8-array + maxItems: 1536 + description: Per-device panel calibration supplied by the bootloader + apple,scan-start-report: $ref: /schemas/types.yaml#/definitions/uint8-array minItems: 4 From f981ec9837662e140ae78321d503a6b412d9c42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Fri, 4 Dec 2026 13:23:23 +0100 Subject: [PATCH 9/9] ARM: dts: apple: iPhone 5: Report the ringer switch as KEY_MUTE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Report the side switch the way the iPad mini (P105) and iPad 2 (K93) do: KEY_MUTE while on silent instead of the SW_MUTE_DEVICE switch event, so desktops that bind the mute key (Omarchy's mute switch handler) act on it. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- arch/arm/boot/dts/apple/s5l8950x-5.dtsi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/arm/boot/dts/apple/s5l8950x-5.dtsi b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi index f5700a87df45e1..c89be3a107e1a8 100644 --- a/arch/arm/boot/dts/apple/s5l8950x-5.dtsi +++ b/arch/arm/boot/dts/apple/s5l8950x-5.dtsi @@ -59,8 +59,7 @@ ringer-switch { label = "Ringer Switch"; gpios = <&gpio 4 1>; - linux,input-type = <5>; - linux,code = <14>; + linux,code = <113>; /* KEY_MUTE, held while on silent */ debounce-interval = <50>; }; };