diff --git a/Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml b/Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml index 95ca972ee2dab3..5cbefdbcb865d5 100644 --- a/Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml +++ b/Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml @@ -120,6 +120,25 @@ properties: firmware-name: maxItems: 1 + description: + Firmware file name. With apple,firmware-prefix, the file used when + the bootloader passes no personality. + + apple,firmware-prefix: + $ref: /schemas/types.yaml#/definitions/string + description: + Start of the firmware file names on a board whose controllers come + with more than one firmware personality. The driver loads + -.bin, the personality in lower case with its + comma as a dash (C1F5D,4 gives -c1f5d-4.bin), and leaves the + touchscreen off if that file is missing. + + apple,hid-merge-personality: + $ref: /schemas/types.yaml#/definitions/string + description: + Touch firmware personality from this unit's Apple Device Tree. + The bootloader supplies it together with the calibration, so it is + absent from the static DTB. apple,z2-cal-blob: $ref: /schemas/types.yaml#/definitions/uint8-array diff --git a/arch/arm64/boot/dts/apple/t8015-8.dtsi b/arch/arm64/boot/dts/apple/t8015-8.dtsi index 29b6cd81aa4810..e34843e12dbcf3 100644 --- a/arch/arm64/boot/dts/apple/t8015-8.dtsi +++ b/arch/arm64/boot/dts/apple/t8015-8.dtsi @@ -60,6 +60,7 @@ hv-supply = <&touch_hv>; clocks = <&touchclk>; firmware-name = "apple/dfrmtfw-d20-c1f5d-3.bin"; + apple,firmware-prefix = "apple/dfrmtfw-d20"; touchscreen-size-x = <750>; touchscreen-size-y = <1334>; }; diff --git a/arch/arm64/boot/dts/apple/t8015-8plus.dtsi b/arch/arm64/boot/dts/apple/t8015-8plus.dtsi index f5e252976d68ed..3b9caf84b58f4c 100644 --- a/arch/arm64/boot/dts/apple/t8015-8plus.dtsi +++ b/arch/arm64/boot/dts/apple/t8015-8plus.dtsi @@ -9,13 +9,16 @@ #include "t8015-8.dtsi" /* - * The D21 and D211 ADTs carry the same touch node: personality C1F5E,3, - * the same pins, power and clock functions. Their IPSW has one multitouch - * file, D21.mtprops, whose C1F5E,4 entry is the controller firmware. + * The D21 and D211 ADTs carry the same touch node: the same pins, power + * and clock functions. Their IPSW has one multitouch file, D21.mtprops, + * with a C1F5E,3 and a C1F5E,4 firmware; the unit's ADT personality picks + * one (the IPSW's ADT says C1F5E,3, the boot ADT of each unit says which + * its controller needs). C1F5E,4 is the one tested. */ &touchscreen { compatible = "apple,d21-touchscreen"; firmware-name = "apple/dfrmtfw-d21-c1f5e-4.bin"; + apple,firmware-prefix = "apple/dfrmtfw-d21"; touchscreen-size-x = <1080>; touchscreen-size-y = <1920>; }; diff --git a/arch/arm64/boot/dts/apple/t8015-d20.dts b/arch/arm64/boot/dts/apple/t8015-d20.dts index 8f418f3eec168c..f0b20bacf07fa7 100644 --- a/arch/arm64/boot/dts/apple/t8015-d20.dts +++ b/arch/arm64/boot/dts/apple/t8015-d20.dts @@ -13,11 +13,6 @@ model = "Apple iPhone 8 (Global)"; }; -/* The ADT personality is C1F5D,3; use the D20 mtprops-derived container. */ -&touchscreen { - firmware-name = "apple/dfrmtfw-d20-c1f5d-3.bin"; -}; - /* The D20's Wi-Fi module, as its ADT's wlan module-instance names it */ &wifi0 { brcm,board-type = "apple,apolloe"; diff --git a/arch/arm64/boot/dts/apple/t8015-d201.dts b/arch/arm64/boot/dts/apple/t8015-d201.dts index 58e2b193df39de..5fa310dc5e00f6 100644 --- a/arch/arm64/boot/dts/apple/t8015-d201.dts +++ b/arch/arm64/boot/dts/apple/t8015-d201.dts @@ -14,10 +14,11 @@ }; -/* Same C1F5D,3 personality; pinctrl/PMIC phandle numbers differ in the ADT. */ +/* The bootloader supplies this unit's C1F5D personality and calibration. */ &touchscreen { compatible = "apple,d201-touchscreen"; firmware-name = "apple/dfrmtfw-d201-c1f5d-3.bin"; + apple,firmware-prefix = "apple/dfrmtfw-d201"; }; /* The D201's Wi-Fi module, as its ADT's wlan module-instance names it */ diff --git a/drivers/input/touchscreen/apple_z2.c b/drivers/input/touchscreen/apple_z2.c index 76e68d797a39af..a915a920cd1135 100644 --- a/drivers/input/touchscreen/apple_z2.c +++ b/drivers/input/touchscreen/apple_z2.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -23,11 +24,54 @@ #include #include #include +#include #include #include #include #include +/* + * Apple's touch firmware comes in variants ("personalities"), and each + * unit's ADT names the one its controller needs; m1n1 passes that on as + * apple,hid-merge-personality. Where a board's IPSW carries more than one, + * the device tree names the files' common start in apple,firmware-prefix, + * and a unit's file is -.bin, the personality in lower + * case with its comma as a dash (C1F5D,4: -c1f5d-4.bin). If that + * file is missing, the touchscreen stays off and the log names it: a unit + * never gets another personality's firmware. + * + * Without apple,firmware-prefix, or when the bootloader passes no + * personality, firmware-name is the file, as before. + */ +static const char *apple_z2_fw_name(struct device *dev, const char *fw_name) +{ + const char *prefix, *personality, *c; + char *name, *p; + + if (device_property_read_string(dev, "apple,firmware-prefix", &prefix)) + return fw_name; + if (device_property_read_string(dev, "apple,hid-merge-personality", + &personality)) { + dev_info(dev, "no touch personality from the bootloader, loading %s\n", + fw_name); + return fw_name; + } + for (c = personality; *c; c++) + if (!isalnum(*c) && *c != ',' && *c != '-') + break; + if (*c || c == personality) { + dev_err(dev, "bad touch personality \"%s\"\n", personality); + return ERR_PTR(-EINVAL); + } + name = devm_kasprintf(dev, GFP_KERNEL, "%s-%s.bin", prefix, personality); + if (!name) + return ERR_PTR(-ENOMEM); + for (p = name + strlen(prefix) + 1; *p; p++) + *p = *p == ',' ? '-' : tolower(*p); + dev_info(dev, "touch personality %s: %s\n", personality, name); + return name; +} + #include "apple_z2_n61.h" #include "apple_z2_j72.h" #include "apple_z2_d21.h" @@ -6207,6 +6251,9 @@ static int apple_z2_probe(struct spi_device *spi) error = device_property_read_string(dev, "firmware-name", &z2->fw_name); if (error) return dev_err_probe(dev, error, "unable to get firmware name\n"); + z2->fw_name = apple_z2_fw_name(dev, z2->fw_name); + if (IS_ERR(z2->fw_name)) + return PTR_ERR(z2->fw_name); if (apple_z2_is_iphone7_plus(z2)) { static const struct { const char *name; diff --git a/drivers/input/touchscreen/apple_z2_d21.h b/drivers/input/touchscreen/apple_z2_d21.h index 8fd4d98d1d5e17..58935a72e4cf12 100644 --- a/drivers/input/touchscreen/apple_z2_d21.h +++ b/drivers/input/touchscreen/apple_z2_d21.h @@ -129,6 +129,7 @@ static int apple_z2_d21_smc_rw(struct apple_smc *smc, smc_key key, #define APPLE_Z2_D21_FW_CONFIG_CPHA BIT(3) #define APPLE_Z2_D21_FW_CONFIG_CPOL BIT(4) #define APPLE_Z2_D21_FW_CONFIG_BOOT_TIMEOUT BIT(8) +#define APPLE_Z2_D21_FW_CONFIG_HBPP_VERSION BIT(11) enum apple_z2_d21_protocol_state { APPLE_Z2_D21_STATE_OFF, @@ -197,6 +198,7 @@ struct apple_z2_d21 { unsigned int z2_inter_packet_delay_us; unsigned int z2_cs_delay_us; unsigned int boot_timeout_ms; + u32 hbpp_version; s16 sensor_max_x; s16 sensor_min_x; s16 sensor_max_y; @@ -2978,15 +2980,10 @@ static const u8 *apple_z2_d21_build_iphone7_cal(struct apple_z2_d21 *z2, put_unaligned_be16(0x18e1, blob_data); put_unaligned_be16(APPLE_Z2_D21_HBPP_CMD_BLOB, blob_data + 2); - /* - * The length field counts the payload words less one. Apple's own - * container for this part stores its firmware blobs that way (63592 - * bytes carries 15897, 17312 carries 4327), and the calibration packet - * shares the framing. Sending the plain word count makes the packet one - * word too long, so the controller reads into the trailing checksum and - * rejects every calibration blob. + /* Legacy containers retain their minus-one framing. A container that + * explicitly declares HBPP 0x103 uses the full payload word count. */ - put_unaligned_be16(words - 1, blob_data + 4); + put_unaligned_be16(words - (z2->hbpp_version >= 0x104), blob_data + 4); put_unaligned_be16(address, blob_data + 6); put_unaligned_be16(address >> 16, blob_data + 8); header_checksum = 0; @@ -3234,6 +3231,13 @@ static int apple_z2_d21_apply_fw_config(struct apple_z2_d21 *z2, const u8 *data, cpha = apple_z2_d21_fw_config_word(data, 4); cpol = apple_z2_d21_fw_config_word(data, 5); boot_timeout = apple_z2_d21_fw_config_word(data, 9); + if (valid & APPLE_Z2_D21_FW_CONFIG_HBPP_VERSION) { + u32 version = apple_z2_d21_fw_config_word(data, 10); + + if (version != 0x103 && version != 0x104) + return -EINVAL; + z2->hbpp_version = version; + } if ((valid & APPLE_Z2_D21_FW_CONFIG_MIN_DMA) && min_dma) { if (apple_z2_d21_uses_iphone_protocol(z2)) { if (min_dma > APPLE_Z2_D21_RX_BUF_SIZE) @@ -5355,6 +5359,8 @@ static int apple_z2_d21_probe(struct spi_device *spi, z2->spidev = spi; z2->variant = info->variant; + /* Preserve existing containers that do not declare HBPP framing. */ + z2->hbpp_version = 0x104; z2->boot_timeout_ms = 20; if (apple_z2_d21_uses_iphone_protocol(z2)) { z2->z2_inter_packet_delay_us = 1000; @@ -5509,6 +5515,9 @@ static int apple_z2_d21_probe(struct spi_device *spi, error = device_property_read_string(dev, "firmware-name", &z2->fw_name); if (error) return dev_err_probe(dev, error, "unable to get firmware name\n"); + z2->fw_name = apple_z2_fw_name(dev, z2->fw_name); + if (IS_ERR(z2->fw_name)) + return PTR_ERR(z2->fw_name); if (apple_z2_d21_uses_iphone_protocol(z2)) { static const struct { const char *name;