Skip to contentSkip to conversation

Driver for Apple Z2 touchscreens.

PATCH Superseded Sasha Finkelstein 8 participants 15 messages Nov 28, 2024 – Dec 6, 2024

Subsystems (4)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Driver for Apple Z2 touchscreens.

Hi.

This series adds support for Apple touchscreens using the Z2 protocol. Those are used as the primary touchscreen on mobile Apple devices, and for the touchbar on laptops using the M-series chips. (T1/T2 laptops have a coprocessor in charge of speaking Z2 to the touchbar).

Originally sent as a RFC at https://lore.kernel.org/all/[email protected]/
The changes since then mostly address the review feedback, but also
add another machine that has this specific controller.

---
Changes in v2:
- In a separate patch, fixed an issue that prevented the SPI controller from using GPIO CS, and as such, moved the hardware quirk to there
- Went back to uploading the firmware in probe() instad of open()
- Other changes addressing the review feedback
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Sasha Finkelstein (4):
dt-bindings: input: touchscreen: Add Z2 controller
input: apple_z2: Add a driver for Apple Z2 touchscreens
arm64: dts: apple: Add touchbar digitizer nodes
MAINTAINERS: Add entries for Apple Z2 touchscreen driver

 .../input/touchscreen/apple,z2-multitouch.yaml     |  69 ++++
 MAINTAINERS                                        |   2 +
 arch/arm64/boot/dts/apple/t8103-j293.dts           |  26 ++
 arch/arm64/boot/dts/apple/t8103.dtsi               |  20 +
 arch/arm64/boot/dts/apple/t8112-j493.dts           |  24 ++
 arch/arm64/boot/dts/apple/t8112.dtsi               |  14 +
 drivers/input/touchscreen/Kconfig                  |  13 +
 drivers/input/touchscreen/Makefile                 |   1 +
 drivers/input/touchscreen/apple_z2.c               | 458 +++++++++++++++++++++
 9 files changed, 627 insertions(+)

---
base-commit: 9f16d5e6f220661f73b36a4be1b21575651d8833
change-id: 20241124-z2-c012b528ea0d

#1 lore raw

PATCH v2 1/4 dt-bindings: input: touchscreen: Add Z2 controller

From: Sasha Finkelstein <[email protected]>

Add bindings for touchscreen controllers attached using the Z2 protocol. Those are present in most Apple devices.

---
 .../input/touchscreen/apple,z2-multitouch.yaml     | 69 ++++++++++++++++++++++
 1 file changed, 69 insertions(+)
Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml new file +69 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c4c0b41178728d5e0a782979d6eecd32e0a83618
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/touchscreen/apple,z2-multitouch.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Apple touchscreens attached using the Z2 protocol
+
+maintainers:
+  - Sasha Finkelstein <[email protected]>
+
+description: A series of touschscreen controllers used in Apple products
+
+allOf:
+  - $ref: touchscreen.yaml#
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    enum:
+      - apple,j293-touchbar
+      - apple,j493-touchbar
+
+  interrupts:
+    maxItems: 1
+
+  reset-gpios:
+    maxItems: 1
+
+  firmware-name:
+    maxItems: 1
+
+  apple,z2-cal-blob:
+    $ref: /schemas/types.yaml#/definitions/uint8-array
+    description:
+      Calibration blob supplied by the bootloader
+
+required:
+  - compatible
+  - interrupts
+  - reset-gpios
+  - firmware-name
+  - touchscreen-size-x
+  - touchscreen-size-y
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        touchscreen@0 {
+            compatible = "apple,j293-touchbar";
+            reg = <0>;
+            spi-max-frequency = <11500000>;
+            reset-gpios = <&pinctrl_ap 139 GPIO_ACTIVE_LOW>;
+            interrupts-extended = <&pinctrl_ap 194 IRQ_TYPE_EDGE_FALLING>;
+            firmware-name = "apple/dfrmtfw-j293.bin";
+            touchscreen-size-x = <23045>;
+            touchscreen-size-y = <640>;
+        };
+    };
+
+...
Signature · 1 line
2.47.1
#5 lore raw

PATCH v2 2/4 input: apple_z2: Add a driver for Apple Z2 touchscreens

In reply to Sasha Finkelstein

Adds a driver for Apple touchscreens using the Z2 protocol.

---
 drivers/input/touchscreen/Kconfig    |  13 +
 drivers/input/touchscreen/Makefile   |   1 +
 drivers/input/touchscreen/apple_z2.c | 458 +++++++++++++++++++++++++++++++++++
 3 files changed, 472 insertions(+)
drivers/input/touchscreen/Kconfig +13 -0
index 1ac26fc2e3eb94a4f62a49962c25ec853b567a43..4ad5002191f77a17414f9e1494b0afd6447355c0 100644
@@ -103,6 +103,19 @@ config TOUCHSCREEN_ADC
 	  To compile this driver as a module, choose M here: the
 	  module will be called resistive-adc-touch.ko.
 
+config TOUCHSCREEN_APPLE_Z2
+	tristate "Apple Z2 touchscreens"
+	default ARCH_APPLE
+	depends on SPI
+	help
+	  Say Y here if you have an Apple device with
+	  a touchscreen or a touchbar.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called apple_z2.
+
 config TOUCHSCREEN_AR1021_I2C
 	tristate "Microchip AR1020/1021 i2c touchscreen"
 	depends on I2C && OF
drivers/input/touchscreen/Makefile +1 -0
index 82bc837ca01e2ee18c5e9c578765d55ef9fab6d4..97a025c6a3770fb80255246eb63c11688ebd79eb 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_TOUCHSCREEN_AD7879_I2C)	+= ad7879-i2c.o
 obj-$(CONFIG_TOUCHSCREEN_AD7879_SPI)	+= ad7879-spi.o
 obj-$(CONFIG_TOUCHSCREEN_ADC)		+= resistive-adc-touch.o
 obj-$(CONFIG_TOUCHSCREEN_ADS7846)	+= ads7846.o
+obj-$(CONFIG_TOUCHSCREEN_APPLE_Z2)	+= apple_z2.o
 obj-$(CONFIG_TOUCHSCREEN_AR1021_I2C)	+= ar1021_i2c.o
 obj-$(CONFIG_TOUCHSCREEN_ATMEL_MXT)	+= atmel_mxt_ts.o
 obj-$(CONFIG_TOUCHSCREEN_AUO_PIXCIR)	+= auo-pixcir-ts.o
drivers/input/touchscreen/apple_z2.c new file +458 -0

458 lines not on this page · raw message

Signature · 1 line
2.47.1
#10 lore raw

PATCH v2 3/4 arm64: dts: apple: Add touchbar digitizer nodes

In reply to Sasha Finkelstein

From: Sasha Finkelstein <[email protected]>

Adds device tree entries for the touchbar digitizer

---
 arch/arm64/boot/dts/apple/t8103-j293.dts | 26 ++++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8103.dtsi     | 20 ++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112-j493.dts | 24 ++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112.dtsi     | 14 ++++++++++++++
 4 files changed, 84 insertions(+)
arch/arm64/boot/dts/apple/t8103-j293.dts +26 -0
index 56b0c67bfcda321b60c621de092643017693ff91..c31eb3f6f54268cafc9197a9244a5954fbb42802 100644
@@ -17,6 +17,14 @@ / {
 	compatible = "apple,j293", "apple,t8103", "apple,arm-platform";
 	model = "Apple MacBook Pro (13-inch, M1, 2020)";
 
+	/*
+	 * All of those are used by the bootloader to pass calibration
+	 * blobs and other device-specific properties
+	 */
+	aliases {
+		touchbar0 = &touchbar0;
+	};
+
 	led-controller {
 		compatible = "pwm-leds";
 		led-0 {
@@ -49,3 +57,21 @@ &i2c4 {
 &fpwm1 {
 	status = "okay";
 };
+
+&spi0 {
+	status = "okay";
+
+	touchbar0: touchbar@0 {
+		compatible = "apple,j293-touchbar";
+		reg = <0>;
+		spi-max-frequency = <11500000>;
+		spi-cs-setup-delay-ns = <2000>;
+		spi-cs-hold-delay-ns = <2000>;
+		reset-gpios = <&pinctrl_ap 139 GPIO_ACTIVE_LOW>;
+		interrupts-extended = <&pinctrl_ap 194 IRQ_TYPE_EDGE_FALLING>;
+		firmware-name = "apple/dfrmtfw-j293.bin";
+		touchscreen-size-x = <23045>;
+		touchscreen-size-y = <640>;
+		touchscreen-inverted-y;
+	};
+};
arch/arm64/boot/dts/apple/t8103.dtsi +20 -0
index 9b0dad6b618444ac6b1c9735c50cccfc3965f947..9b83341a799d9a37578e5461e6b184f81ee7435c 100644
@@ -326,6 +326,13 @@ clkref: clock-ref {
 		clock-output-names = "clkref";
 	};
 
+	clk_200m: clock-200m {
+		compatible = "fixed-clock";
+		#clock-cells = <0>;
+		clock-frequency = <200000000>;
+		clock-output-names = "clk_200m";
+	};
+
 	/*
 	 * This is a fabulated representation of the input clock
 	 * to NCO since we don't know the true clock tree.
@@ -441,6 +448,19 @@ fpwm1: pwm@235044000 {
 			status = "disabled";
 		};
 
+		spi0: spi@235100000 {
+			compatible = "apple,t8103-spi", "apple,spi";
+			reg = <0x2 0x35100000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 614 IRQ_TYPE_LEVEL_HIGH>;
+			cs-gpios = <&pinctrl_ap 109 GPIO_ACTIVE_LOW>;
+			clocks = <&clk_200m>;
+			power-domains = <&ps_spi0>;
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "disabled"; /* only used in J293 */
+		};
+
 		serial0: serial@235200000 {
 			compatible = "apple,s5l-uart";
 			reg = <0x2 0x35200000 0x0 0x1000>;
arch/arm64/boot/dts/apple/t8112-j493.dts +24 -0
index 0ad908349f55406783942735a2e9dad54cda00ec..3332cc87cdf1a418c4c2247639baf5d2a42ed3c2 100644
@@ -17,8 +17,13 @@ / {
 	compatible = "apple,j493", "apple,t8112", "apple,arm-platform";
 	model = "Apple MacBook Pro (13-inch, M2, 2022)";
 
+	/*
+	 * All of those are used by the bootloader to pass calibration
+	 * blobs and other device-specific properties
+	 */
 	aliases {
 		bluetooth0 = &bluetooth0;
+		touchbar0 = &touchbar0;
 		wifi0 = &wifi0;
 	};
 
@@ -67,3 +72,22 @@ &i2c4 {
 &fpwm1 {
 	status = "okay";
 };
+
+&spi3 {
+	status = "okay";
+
+	touchbar0: touchbar@0 {
+		compatible = "apple,j493-touchbar";
+		reg = <0>;
+		spi-max-frequency = <8000000>;
+		spi-cs-setup-delay-ns = <2000>;
+		spi-cs-hold-delay-ns = <2000>;
+
+		reset-gpios = <&pinctrl_ap 170 GPIO_ACTIVE_LOW>;
+		interrupts-extended = <&pinctrl_ap 174 IRQ_TYPE_EDGE_FALLING>;
+		firmware-name = "apple/dfrmtfw-j493.bin";
+		touchscreen-size-x = <23045>;
+		touchscreen-size-y = <640>;
+		touchscreen-inverted-y;
+	};
+};
arch/arm64/boot/dts/apple/t8112.dtsi +14 -0
index 1666e6ab250bc0be9b8318e3c8fc903ccd3f3760..977c1ca5e8c1b566bb3876b6619ea8812b98e072 100644
@@ -467,6 +467,20 @@ fpwm1: pwm@235044000 {
 			status = "disabled";
 		};
 
+		spi3: spi@23510c000 {
+			compatible = "apple,t8112-spi", "apple,spi";
+			reg = <0x2 0x3510c000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 751 IRQ_TYPE_LEVEL_HIGH>;
+			clocks = <&clkref>;
+			pinctrl-0 = <&spi3_pins>;
+			pinctrl-names = "default";
+			power-domains = <&ps_spi3>;
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "disabled";
+		};
+
 		serial0: serial@235200000 {
 			compatible = "apple,s5l-uart";
 			reg = <0x2 0x35200000 0x0 0x1000>;
Signature · 1 line
2.47.1
#13 lore raw

PATCH v2 4/4 MAINTAINERS: Add entries for Apple Z2 touchscreen driver

In reply to Sasha Finkelstein

Add the MAINTAINERS entries for the driver

---
 MAINTAINERS | 2 ++
 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
index e7f0170977013889ca7c39b17727ba36d32e92dc..9f75fff12fa1912b70251cade845d6b3d5e28c48 100644
@@ -2166,6 +2166,7 @@ F:	Documentation/devicetree/bindings/clock/apple,nco.yaml
 F:	Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml
 F:	Documentation/devicetree/bindings/dma/apple,admac.yaml
 F:	Documentation/devicetree/bindings/i2c/apple,i2c.yaml
+F:	Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml
 F:	Documentation/devicetree/bindings/interrupt-controller/apple,*
 F:	Documentation/devicetree/bindings/iommu/apple,dart.yaml
 F:	Documentation/devicetree/bindings/iommu/apple,sart.yaml
@@ -2186,6 +2187,7 @@ F:	drivers/dma/apple-admac.c
 F:	drivers/pmdomain/apple/
 F:	drivers/i2c/busses/i2c-pasemi-core.c
 F:	drivers/i2c/busses/i2c-pasemi-platform.c
+F:	drivers/input/touchscreen/apple_z2.c
 F:	drivers/iommu/apple-dart.c
 F:	drivers/iommu/io-pgtable-dart.c
 F:	drivers/irqchip/irq-apple-aic.c
Signature · 1 line
2.47.1
#11 lore raw

Re: [PATCH v2 3/4] arm64: dts: apple: Add touchbar digitizer nodes

In reply to Sasha Finkelstein

Sasha Finkelstein via B4 Relay 於 2024/11/29 早上6:29 寫道:

Quoted 116 lines

From: Sasha Finkelstein <[email protected]>

Adds device tree entries for the touchbar digitizer

---
 arch/arm64/boot/dts/apple/t8103-j293.dts | 26 ++++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8103.dtsi     | 20 ++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112-j493.dts | 24 ++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112.dtsi     | 14 ++++++++++++++
 4 files changed, 84 insertions(+)
arch/arm64/boot/dts/apple/t8103-j293.dts +26 -0
index 56b0c67bfcda321b60c621de092643017693ff91..c31eb3f6f54268cafc9197a9244a5954fbb42802 100644
@@ -17,6 +17,14 @@ / {
 	compatible = "apple,j293", "apple,t8103", "apple,arm-platform";
 	model = "Apple MacBook Pro (13-inch, M1, 2020)";
 
+	/*
+	 * All of those are used by the bootloader to pass calibration
+	 * blobs and other device-specific properties
+	 */
+	aliases {
+		touchbar0 = &touchbar0;
+	};
+
 	led-controller {
 		compatible = "pwm-leds";
 		led-0 {
@@ -49,3 +57,21 @@ &i2c4 {
 &fpwm1 {
 	status = "okay";
 };
+
+&spi0 {
+	status = "okay";
+
+	touchbar0: touchbar@0 {
+		compatible = "apple,j293-touchbar";
+		reg = <0>;
+		spi-max-frequency = <11500000>;
+		spi-cs-setup-delay-ns = <2000>;
+		spi-cs-hold-delay-ns = <2000>;
+		reset-gpios = <&pinctrl_ap 139 GPIO_ACTIVE_LOW>;
+		interrupts-extended = <&pinctrl_ap 194 IRQ_TYPE_EDGE_FALLING>;
+		firmware-name = "apple/dfrmtfw-j293.bin";
+		touchscreen-size-x = <23045>;
+		touchscreen-size-y = <640>;
+		touchscreen-inverted-y;
+	};
+};
arch/arm64/boot/dts/apple/t8103.dtsi +20 -0
index 9b0dad6b618444ac6b1c9735c50cccfc3965f947..9b83341a799d9a37578e5461e6b184f81ee7435c 100644
@@ -326,6 +326,13 @@ clkref: clock-ref {
 		clock-output-names = "clkref";
 	};
 
+	clk_200m: clock-200m {
+		compatible = "fixed-clock";
+		#clock-cells = <0>;
+		clock-frequency = <200000000>;
+		clock-output-names = "clk_200m";
+	};
+
 	/*
 	 * This is a fabulated representation of the input clock
 	 * to NCO since we don't know the true clock tree.
@@ -441,6 +448,19 @@ fpwm1: pwm@235044000 {
 			status = "disabled";
 		};
 
+		spi0: spi@235100000 {
+			compatible = "apple,t8103-spi", "apple,spi";
+			reg = <0x2 0x35100000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 614 IRQ_TYPE_LEVEL_HIGH>;
+			cs-gpios = <&pinctrl_ap 109 GPIO_ACTIVE_LOW>;
+			clocks = <&clk_200m>;
+			power-domains = <&ps_spi0>;
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "disabled"; /* only used in J293 */
+		};
+
 		serial0: serial@235200000 {
 			compatible = "apple,s5l-uart";
 			reg = <0x2 0x35200000 0x0 0x1000>;
arch/arm64/boot/dts/apple/t8112-j493.dts +6 -0
index 0ad908349f55406783942735a2e9dad54cda00ec..3332cc87cdf1a418c4c2247639baf5d2a42ed3c2 100644
@@ -17,8 +17,13 @@ / {
 	compatible = "apple,j493", "apple,t8112", "apple,arm-platform";
 	model = "Apple MacBook Pro (13-inch, M2, 2022)";
 
+	/*
+	 * All of those are used by the bootloader to pass calibration
+	 * blobs and other device-specific properties
+	 */
 	aliases {
 		bluetooth0 = &bluetooth0;
+		touchbar0 = &touchbar0;
 		wifi0 = &wifi0;
 	};
 
@@ -67,3 +72,22 @@ &i2c4 {
 &fpwm1 {
 	status = "okay";
 };
+

arch/arm64/boot/dts/apple/t8112-j493.dts &i2c4 { · line 85

Quoted 10 lines
arch/arm64/boot/dts/apple/t8112-j493.dts +10 -0
+&spi3 {
+	status = "okay";
+
+	touchbar0: touchbar@0 {
+		compatible = "apple,j493-touchbar";
+		reg = <0>;
+		spi-max-frequency = <8000000>;
+		spi-cs-setup-delay-ns = <2000>;
+		spi-cs-hold-delay-ns = <2000>;
+

Remove the empty line

Quoted 18 lines
arch/arm64/boot/dts/apple/t8112-j493.dts +8 -0
+		reset-gpios = <&pinctrl_ap 170 GPIO_ACTIVE_LOW>;
+		interrupts-extended = <&pinctrl_ap 174 IRQ_TYPE_EDGE_FALLING>;
+		firmware-name = "apple/dfrmtfw-j493.bin";
+		touchscreen-size-x = <23045>;
+		touchscreen-size-y = <640>;
+		touchscreen-inverted-y;
+	};
+};
arch/arm64/boot/dts/apple/t8112.dtsi +2 -0
index 1666e6ab250bc0be9b8318e3c8fc903ccd3f3760..977c1ca5e8c1b566bb3876b6619ea8812b98e072 100644
@@ -467,6 +467,20 @@ fpwm1: pwm@235044000 {
 			status = "disabled";
 		};
 
+		spi3: spi@23510c000 {
+			compatible = "apple,t8112-spi", "apple,spi";

arch/arm64/boot/dts/apple/t8112.dtsi fpwm1: pwm@235044000 { · line 481

Quoted 10 lines
arch/arm64/boot/dts/apple/t8112.dtsi +10 -0
+			reg = <0x2 0x3510c000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 751 IRQ_TYPE_LEVEL_HIGH>;
+			clocks = <&clkref>;
+			pinctrl-0 = <&spi3_pins>;
+			pinctrl-names = "default";
+			power-domains = <&ps_spi3>;
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "disabled";

/* only used in J493 */ ?

arch/arm64/boot/dts/apple/t8112.dtsi fpwm1: pwm@235044000 { · line 486

Quoted 6 lines
arch/arm64/boot/dts/apple/t8112.dtsi +2 -0
+		};
+
 		serial0: serial@235200000 {
 			compatible = "apple,s5l-uart";
 			reg = <0x2 0x35200000 0x0 0x1000>;

Nick Chan

#2 lore raw

Re: [PATCH v2 1/4] dt-bindings: input: touchscreen: Add Z2 controller

In reply to Sasha Finkelstein
Quoted 40 lines · Sasha Finkelstein

Add bindings for touchscreen controllers attached using the Z2 protocol. Those are present in most Apple devices.

---
 .../input/touchscreen/apple,z2-multitouch.yaml     | 69 ++++++++++++++++++++++
 1 file changed, 69 insertions(+)
Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c4c0b41178728d5e0a782979d6eecd32e0a83618
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/touchscreen/apple,z2-multitouch.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Apple touchscreens attached using the Z2 protocol
+
+maintainers:
+  - Sasha Finkelstein <[email protected]>
+
+description: A series of touschscreen controllers used in Apple products
+
+allOf:
+  - $ref: touchscreen.yaml#
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    enum:
+      - apple,j293-touchbar
+      - apple,j493-touchbar
+
+  interrupts:
+    maxItems: 1
+

Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml line 36

Quoted 10 lines
Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml new file +10 -0
+  reset-gpios:
+    maxItems: 1
+
+  firmware-name:
+    maxItems: 1
+
+  apple,z2-cal-blob:
+    $ref: /schemas/types.yaml#/definitions/uint8-array
+    description:
+      Calibration blob supplied by the bootloader

What is the expected size? Fixed size or maximum?

Add it to the example.

Best regards,
Krzysztof

#3 lore raw

Re: [PATCH v2 1/4] dt-bindings: input: touchscreen: Add Z2 controller

Quoted 4 lines · Krzysztof Kozlowski

Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml line 36

Quoted 2 lines
Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml new file +2 -0
+    description:
+      Calibration blob supplied by the bootloader

What is the expected size? Fixed size or maximum?

Unspecified, 1688 bytes on my machine, but the firmware has a size field. Most likely less than 4k.

Quoted 1 line

Add it to the example.

That seems counterproductive - it is a ~1.5kb blob, and it is supposed to be set by the bootloader, not by person writing the dts.

#4 lore raw

Re: [PATCH v2 1/4] dt-bindings: input: touchscreen: Add Z2 controller

Quoted 8 lines · Sasha Finkelstein
Quoted 4 lines · Krzysztof Kozlowski

Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml line 36

Quoted 2 lines
Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml new file +2 -0
+    description:
+      Calibration blob supplied by the bootloader

What is the expected size? Fixed size or maximum?

Unspecified, 1688 bytes on my machine, but the firmware has a size field. Most likely less than 4k.

I suggest putting upper limit. You can still later grow it if different bootloader uses bigger size.

Quoted 6 lines
Quoted 2 lines

Add it to the example.

That seems counterproductive - it is a ~1.5kb blob, and it is supposed to be set by the bootloader, not by person writing the dts.

Indeed, let's skip it.

Anyway:

Best regards,
Krzysztof

#6 lore raw

Re: [PATCH v2 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens

In reply to Sasha Finkelstein
Quoted 7 lines · On 11/28/24 14:29, Sasha Finkelstein via B4 Relay wrote:

From: Sasha Finkelstein <[email protected]>

Adds a driver for Apple touchscreens using the Z2 protocol.

---

...

drivers/input/touchscreen/apple_z2.c line 458

Quoted 2 lines
drivers/input/touchscreen/apple_z2.c new file +2 -0
+MODULE_LICENSE("GPL");
+MODULE_FIRMWARE("apple/dfrmtfw-*.bin");

Since commit 1fffe7a34c89 ("script: modpost: emit a warning when the description is missing"), a module without a MODULE_DESCRIPTION() will result in a warning when built with make W=1.

Please add the missing MODULE_DESCRIPTION()

reviewer Reviewed-by on the v2 series
#14 lore raw
In reply to Sasha Finkelstein
Quoted 38 lines · On Thu, Nov 28, 2024 at 5:29 PM Sasha Finkelstein via B4 Relay…

Hi.

This series adds support for Apple touchscreens using the Z2 protocol. Those are used as the primary touchscreen on mobile Apple devices, and for the touchbar on laptops using the M-series chips. (T1/T2 laptops have a coprocessor in charge of speaking Z2 to the touchbar).

Originally sent as a RFC at https://lore.kernel.org/all/[email protected]/
The changes since then mostly address the review feedback, but also
add another machine that has this specific controller.

---
Changes in v2:
- In a separate patch, fixed an issue that prevented the SPI controller from using GPIO CS, and as such, moved the hardware quirk to there
- Went back to uploading the firmware in probe() instad of open()
- Other changes addressing the review feedback
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Sasha Finkelstein (4):
dt-bindings: input: touchscreen: Add Z2 controller
input: apple_z2: Add a driver for Apple Z2 touchscreens
arm64: dts: apple: Add touchbar digitizer nodes
MAINTAINERS: Add entries for Apple Z2 touchscreen driver

 .../input/touchscreen/apple,z2-multitouch.yaml     |  69 ++++
 MAINTAINERS                                        |   2 +
 arch/arm64/boot/dts/apple/t8103-j293.dts           |  26 ++
 arch/arm64/boot/dts/apple/t8103.dtsi               |  20 +
 arch/arm64/boot/dts/apple/t8112-j493.dts           |  24 ++
 arch/arm64/boot/dts/apple/t8112.dtsi               |  14 +
 drivers/input/touchscreen/Kconfig                  |  13 +
 drivers/input/touchscreen/Makefile                 |   1 +
 drivers/input/touchscreen/apple_z2.c               | 458 +++++++++++++++++++++
 9 files changed, 627 insertions(+)
Quoted 4 lines

---
base-commit: 9f16d5e6f220661f73b36a4be1b21575651d8833
change-id: 20241124-z2-c012b528ea0d

Series LGTM.

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
maintainer
#12 lore raw

Re: [PATCH v2 3/4] arm64: dts: apple: Add touchbar digitizer nodes

In reply to Sasha Finkelstein
Quoted 74 lines · On Thu, Nov 28, 2024 at 11:29:18PM +0100, Sasha Finkelstein via B4 Relay wrote:

From: Sasha Finkelstein <[email protected]>

Adds device tree entries for the touchbar digitizer

---
 arch/arm64/boot/dts/apple/t8103-j293.dts | 26 ++++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8103.dtsi     | 20 ++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112-j493.dts | 24 ++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112.dtsi     | 14 ++++++++++++++
 4 files changed, 84 insertions(+)
arch/arm64/boot/dts/apple/t8103-j293.dts +26 -0
index 56b0c67bfcda321b60c621de092643017693ff91..c31eb3f6f54268cafc9197a9244a5954fbb42802 100644
@@ -17,6 +17,14 @@ / {
 	compatible = "apple,j293", "apple,t8103", "apple,arm-platform";
 	model = "Apple MacBook Pro (13-inch, M1, 2020)";
 
+	/*
+	 * All of those are used by the bootloader to pass calibration
+	 * blobs and other device-specific properties
+	 */
+	aliases {
+		touchbar0 = &touchbar0;
+	};
+
 	led-controller {
 		compatible = "pwm-leds";
 		led-0 {
@@ -49,3 +57,21 @@ &i2c4 {
 &fpwm1 {
 	status = "okay";
 };
+
+&spi0 {
+	status = "okay";
+
+	touchbar0: touchbar@0 {
+		compatible = "apple,j293-touchbar";
+		reg = <0>;
+		spi-max-frequency = <11500000>;
+		spi-cs-setup-delay-ns = <2000>;
+		spi-cs-hold-delay-ns = <2000>;
+		reset-gpios = <&pinctrl_ap 139 GPIO_ACTIVE_LOW>;
+		interrupts-extended = <&pinctrl_ap 194 IRQ_TYPE_EDGE_FALLING>;
+		firmware-name = "apple/dfrmtfw-j293.bin";
+		touchscreen-size-x = <23045>;
+		touchscreen-size-y = <640>;
+		touchscreen-inverted-y;
+	};
+};
arch/arm64/boot/dts/apple/t8103.dtsi +7 -0
index 9b0dad6b618444ac6b1c9735c50cccfc3965f947..9b83341a799d9a37578e5461e6b184f81ee7435c 100644
@@ -326,6 +326,13 @@ clkref: clock-ref {
 		clock-output-names = "clkref";
 	};
 
+	clk_200m: clock-200m {
+		compatible = "fixed-clock";
+		#clock-cells = <0>;
+		clock-frequency = <200000000>;
+		clock-output-names = "clk_200m";
+	};
+
 	/*
 	 * This is a fabulated representation of the input clock
 	 * to NCO since we don't know the true clock tree.

arch/arm64/boot/dts/apple/t8103.dtsi fpwm1: pwm@235044000 { · line 456

Quoted 9 lines
arch/arm64/boot/dts/apple/t8103.dtsi +6 -0
@@ -441,6 +448,19 @@ fpwm1: pwm@235044000 {
 			status = "disabled";
 		};
 
+		spi0: spi@235100000 {
+			compatible = "apple,t8103-spi", "apple,spi";
+			reg = <0x2 0x35100000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 614 IRQ_TYPE_LEVEL_HIGH>;
+			cs-gpios = <&pinctrl_ap 109 GPIO_ACTIVE_LOW>;

Since this appears to be a regular GPIO pin I think it should go into the spi0 override in t8103-j293.dts. Kind of academic since that's the only device using spi0. It will however allow to easily rebase this on top of "Add Apple SPI controller and spi-nor dt nodes:
https://lore.kernel.org/asahi/[email protected]/T/#u

Janne

maintainer
#7 lore raw

Re: [PATCH v2 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens

In reply to Sasha Finkelstein

Hi Sasha,

Quoted 80 lines · On Thu, Nov 28, 2024 at 11:29:17PM +0100, Sasha Finkelstein via B4 Relay wrote:

From: Sasha Finkelstein <[email protected]>

Adds a driver for Apple touchscreens using the Z2 protocol.

---
 drivers/input/touchscreen/Kconfig    |  13 +
 drivers/input/touchscreen/Makefile   |   1 +
 drivers/input/touchscreen/apple_z2.c | 458 +++++++++++++++++++++++++++++++++++
 3 files changed, 472 insertions(+)
drivers/input/touchscreen/Kconfig +13 -0
index 1ac26fc2e3eb94a4f62a49962c25ec853b567a43..4ad5002191f77a17414f9e1494b0afd6447355c0 100644
@@ -103,6 +103,19 @@ config TOUCHSCREEN_ADC
 	  To compile this driver as a module, choose M here: the
 	  module will be called resistive-adc-touch.ko.
 
+config TOUCHSCREEN_APPLE_Z2
+	tristate "Apple Z2 touchscreens"
+	default ARCH_APPLE
+	depends on SPI
+	help
+	  Say Y here if you have an Apple device with
+	  a touchscreen or a touchbar.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called apple_z2.
+
 config TOUCHSCREEN_AR1021_I2C
 	tristate "Microchip AR1020/1021 i2c touchscreen"
 	depends on I2C && OF
drivers/input/touchscreen/Makefile +1 -0
index 82bc837ca01e2ee18c5e9c578765d55ef9fab6d4..97a025c6a3770fb80255246eb63c11688ebd79eb 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_TOUCHSCREEN_AD7879_I2C)	+= ad7879-i2c.o
 obj-$(CONFIG_TOUCHSCREEN_AD7879_SPI)	+= ad7879-spi.o
 obj-$(CONFIG_TOUCHSCREEN_ADC)		+= resistive-adc-touch.o
 obj-$(CONFIG_TOUCHSCREEN_ADS7846)	+= ads7846.o
+obj-$(CONFIG_TOUCHSCREEN_APPLE_Z2)	+= apple_z2.o
 obj-$(CONFIG_TOUCHSCREEN_AR1021_I2C)	+= ar1021_i2c.o
 obj-$(CONFIG_TOUCHSCREEN_ATMEL_MXT)	+= atmel_mxt_ts.o
 obj-$(CONFIG_TOUCHSCREEN_AUO_PIXCIR)	+= auo-pixcir-ts.o
drivers/input/touchscreen/apple_z2.c new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..05f8e8d01a242415ad79f1b7bae2306dd30d94e3
@@ -0,0 +1,458 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Z2 touchscreen driver
+ *
+ * Copyright (C) The Asahi Linux Contributors
+ */
+
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/input.h>
+#include <linux/input/mt.h>
+#include <linux/input/touchscreen.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/spi/spi.h>
+#include <linux/unaligned.h>
+
+#define APPLE_Z2_NUM_FINGERS_OFFSET      16
+#define APPLE_Z2_FINGERS_OFFSET          24
+#define APPLE_Z2_TOUCH_STARTED           3
+#define APPLE_Z2_TOUCH_MOVED             4
+#define APPLE_Z2_CMD_READ_INTERRUPT_DATA 0xEB
+#define APPLE_Z2_HBPP_CMD_BLOB           0x3001
+#define APPLE_Z2_FW_MAGIC                0x5746325A
+#define LOAD_COMMAND_INIT_PAYLOAD        0

drivers/input/touchscreen/apple_z2.c line 36

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+#define LOAD_COMMAND_SEND_BLOB           1
+#define LOAD_COMMAND_SEND_CALIBRATION    2
+#define CAL_PROP_NAME                    "apple,z2-cal-blob"
+
+struct apple_z2 {
+	struct spi_device *spidev;
+	struct gpio_desc *reset_gpio;
+	struct input_dev *input_dev;
+	struct completion boot_irq;
+	int booted;

bool.

Quoted 81 lines
drivers/input/touchscreen/apple_z2.c new file +81 -0
+	int counter;
+	struct touchscreen_properties props;
+	const char *fw_name;
+};
+
+struct apple_z2_finger {
+	u8 finger;
+	u8 state;
+	__le16 unknown2;
+	__le16 abs_x;
+	__le16 abs_y;
+	__le16 rel_x;
+	__le16 rel_y;
+	__le16 tool_major;
+	__le16 tool_minor;
+	__le16 orientation;
+	__le16 touch_major;
+	__le16 touch_minor;
+	__le16 unused[2];
+	__le16 pressure;
+	__le16 multi;
+};
+
+struct apple_z2_hbpp_blob_hdr {
+	__le16 cmd;
+	__le16 len;
+	__le32 addr;
+	__le16 checksum;
+};
+
+struct apple_z2_fw_hdr {
+	__le32 magic;
+	__le32 version;
+};
+
+struct apple_z2_read_interrupt_cmd {
+	u8 cmd;
+	u8 counter;
+	u8 unused[12];
+	__le16 checksum;
+};
+
+static void apple_z2_parse_touches(struct apple_z2 *z2, char *msg, size_t msg_len)
+{
+	int i;
+	int nfingers;
+	int slot;
+	int slot_valid;
+	struct apple_z2_finger *fingers;
+
+	if (msg_len <= APPLE_Z2_NUM_FINGERS_OFFSET)
+		return;
+	nfingers = msg[APPLE_Z2_NUM_FINGERS_OFFSET];
+	fingers = (struct apple_z2_finger *)(msg + APPLE_Z2_FINGERS_OFFSET);
+	for (i = 0; i < nfingers; i++) {
+		slot = input_mt_get_slot_by_key(z2->input_dev, fingers[i].finger);
+		if (slot < 0) {
+			dev_warn(&z2->spidev->dev, "unable to get slot for finger\n");
+			continue;
+		}
+		slot_valid = fingers[i].state == APPLE_Z2_TOUCH_STARTED ||
+			     fingers[i].state == APPLE_Z2_TOUCH_MOVED;
+		input_mt_slot(z2->input_dev, slot);
+		if (!input_mt_report_slot_state(z2->input_dev, MT_TOOL_FINGER, slot_valid))
+			continue;
+		touchscreen_report_pos(z2->input_dev, &z2->props, le16_to_cpu(fingers[i].abs_x),
+				       le16_to_cpu(fingers[i].abs_y), true);
+		input_report_abs(z2->input_dev, ABS_MT_WIDTH_MAJOR,
+				 le16_to_cpu(fingers[i].tool_major));
+		input_report_abs(z2->input_dev, ABS_MT_WIDTH_MINOR,
+				 le16_to_cpu(fingers[i].tool_minor));
+		input_report_abs(z2->input_dev, ABS_MT_ORIENTATION,
+				 le16_to_cpu(fingers[i].orientation));
+		input_report_abs(z2->input_dev, ABS_MT_TOUCH_MAJOR,
+				 le16_to_cpu(fingers[i].touch_major));
+		input_report_abs(z2->input_dev, ABS_MT_TOUCH_MINOR,
+				 le16_to_cpu(fingers[i].touch_minor));
+	}
+	input_mt_sync_frame(z2->input_dev);
+	input_sync(z2->input_dev);
+}

drivers/input/touchscreen/apple_z2.c line 127

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+
+static int apple_z2_read_packet(struct apple_z2 *z2)
+{
+	struct spi_message msg;
+	struct spi_transfer xfer;
+	int error;
+	size_t pkt_len;
+	struct apple_z2_read_interrupt_cmd *len_cmd __free(kfree) = NULL;
+	char *len_rx __free(kfree) = NULL;
+	char *pkt_rx __free(kfree) = NULL;

Have you considered pre-allocating rx and tx buffers instead of doing these allocations in the interrupt handler? You can probably have tx buffer as part of apple_z2 structure annotated as ____cacheline_aligned, and rx allocated separately (depending on how large it is).

drivers/input/touchscreen/apple_z2.c line 139

Quoted 12 lines
drivers/input/touchscreen/apple_z2.c new file +12 -0
+
+	spi_message_init(&msg);
+	memset(&xfer, 0, sizeof(xfer));
+	len_cmd = kzalloc(sizeof(*len_cmd), GFP_KERNEL);
+	len_rx = kzalloc(16, GFP_KERNEL);
+	if (!len_cmd || !len_rx)
+		return -ENOMEM;
+
+	len_cmd->cmd = APPLE_Z2_CMD_READ_INTERRUPT_DATA;
+	len_cmd->counter = z2->counter + 1;
+	len_cmd->checksum = cpu_to_le16(APPLE_Z2_CMD_READ_INTERRUPT_DATA + 1 + z2->counter);
+	z2->counter = 1 - z2->counter;

Is this toggling between 1 and 0? Should it be "counter = !counter"? And is there better name than counter?

Quoted 16 lines
drivers/input/touchscreen/apple_z2.c new file +16 -0
+	xfer.tx_buf = len_cmd;
+	xfer.rx_buf = len_rx;
+	xfer.len = sizeof(*len_cmd);
+
+	spi_message_add_tail(&xfer, &msg);
+	error = spi_sync(z2->spidev, &msg);
+	if (error)
+		return error;
+
+	pkt_len = (get_unaligned_le16(len_rx + 1) + 8) & (-4);
+	pkt_rx = kzalloc(pkt_len, GFP_KERNEL);
+	if (!pkt_rx)
+		return -ENOMEM;
+
+	spi_message_init(&msg);
+	memset(&xfer, 0, sizeof(xfer));

drivers/input/touchscreen/apple_z2.c line 165

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+	xfer.rx_buf = pkt_rx;
+	xfer.len = pkt_len;
+
+	spi_message_add_tail(&xfer, &msg);
+	error = spi_sync(z2->spidev, &msg);
+
+	if (!error)
+		apple_z2_parse_touches(z2, pkt_rx + 5, pkt_len - 5);
+
+	return error;

The preferred form is:

	error = spi_sync(...);
	if (error)
		return error;

	apple_z2_parse_touches(...);

	return 0;
Quoted 24 lines
drivers/input/touchscreen/apple_z2.c new file +24 -0
+}
+
+static irqreturn_t apple_z2_irq(int irq, void *data)
+{
+	struct spi_device *spi = data;
+	struct apple_z2 *z2 = spi_get_drvdata(spi);
+
+	if (unlikely(!z2->booted))
+		complete(&z2->boot_irq);
+	else
+		apple_z2_read_packet(z2);
+
+	return IRQ_HANDLED;
+}
+
+static int apple_z2_build_cal_blob(struct apple_z2 *z2, u32 address, size_t cal_size, char *data)
+{
+	u16 len_words = (cal_size + 3) / 4;
+	u32 checksum = 0;
+	u16 checksum_hdr = 0;
+	int i;
+	struct apple_z2_hbpp_blob_hdr *hdr;
+	int error;
+

drivers/input/touchscreen/apple_z2.c line 199

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+	hdr = (struct apple_z2_hbpp_blob_hdr *)data;
+	hdr->cmd = cpu_to_le16(APPLE_Z2_HBPP_CMD_BLOB);
+	hdr->len = cpu_to_le16(len_words);
+	hdr->addr = cpu_to_le32(address);
+
+	for (i = 2; i < 8; i++)
+		checksum_hdr += data[i];
+
+	hdr->checksum = cpu_to_le16(checksum_hdr);
+	error = device_property_read_u8_array(&z2->spidev->dev, CAL_PROP_NAME, data + 10, cal_size);

What is 10? Can we have a #define for it?

drivers/input/touchscreen/apple_z2.c line 200

Quoted 1 line
drivers/input/touchscreen/apple_z2.c new file +1 -0
+	if (error < 0)

Does device_property_read_u8_array() return positive values on success? If not then just "if (error)". Same for the rest of the checks.

Quoted 36 lines
drivers/input/touchscreen/apple_z2.c new file +36 -0
+		return error;
+
+	for (i = 0; i < cal_size; i++)
+		checksum += data[i + 10];
+
+	put_unaligned_le32(checksum, data + cal_size + 10);
+	return 0;
+}
+
+static int apple_z2_send_firmware_blob(struct apple_z2 *z2, const char *data, u32 size, u8 bpw)
+{
+	struct spi_message msg;
+	struct spi_transfer blob_xfer, ack_xfer;
+	int error;
+	char *int_ack __free(kfree) = NULL;
+
+	int_ack = kzalloc(2, GFP_KERNEL);
+	if (!int_ack)
+		return -ENOMEM;
+	int_ack[0] = 0x1a;
+	int_ack[1] = 0xa1;
+
+	spi_message_init(&msg);
+	memset(&blob_xfer, 0, sizeof(blob_xfer));
+	memset(&ack_xfer, 0, sizeof(ack_xfer));
+
+	blob_xfer.tx_buf = data;
+	blob_xfer.len = size;
+	blob_xfer.bits_per_word = bpw;
+	spi_message_add_tail(&blob_xfer, &msg);
+
+	ack_xfer.tx_buf = int_ack;
+	ack_xfer.len = 2;
+	spi_message_add_tail(&ack_xfer, &msg);
+
+	reinit_completion(&z2->boot_irq);

drivers/input/touchscreen/apple_z2.c line 246

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+	error = spi_sync(z2->spidev, &msg);
+	if (error)
+		return error;
+	wait_for_completion_timeout(&z2->boot_irq, msecs_to_jiffies(20));
+	return 0;
+}
+
+static int apple_z2_upload_firmware(struct apple_z2 *z2)
+{
+	const struct firmware *fw __free(firmware) = NULL;

Please keep __free() annotated definitions close to the site of first use. In this case directly above call to request_firmware().

Quoted 6 lines
drivers/input/touchscreen/apple_z2.c new file +6 -0
+	struct apple_z2_fw_hdr *fw_hdr;
+	size_t fw_idx = sizeof(struct apple_z2_fw_hdr);
+	int error;
+	u32 load_cmd;
+	u32 size;
+	u32 address;

drivers/input/touchscreen/apple_z2.c line 263

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+	char *data;
+	u8 bits_per_word;
+	size_t cal_size;
+	error = request_firmware(&fw, z2->fw_name, &z2->spidev->dev);
+	if (error) {
+		dev_err(&z2->spidev->dev, "unable to load firmware");
+		return error;
+	}
+
+	fw_hdr = (struct apple_z2_fw_hdr *)fw->data;

You are losing constness think.

Quoted 8 lines
drivers/input/touchscreen/apple_z2.c new file +8 -0
+	if (le32_to_cpu(fw_hdr->magic) != APPLE_Z2_FW_MAGIC || le32_to_cpu(fw_hdr->version) != 1) {
+		dev_err(&z2->spidev->dev, "invalid firmware header");
+		return -EINVAL;
+	}
+
+	/*
+	 * This will interrupt the upload half-way if the file is malformed
+	 * As the device has no non-volatile storage to corrupt, and gets reset

drivers/input/touchscreen/apple_z2.c line 281

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+	 * on boot anyway, this is fine.
+	 */
+	while (fw_idx < fw->size) {
+		if (fw->size - fw_idx < 8) {
+			dev_err(&z2->spidev->dev, "firmware malformed");
+			return -EINVAL;
+		}
+
+		load_cmd = le32_to_cpu(*(__le32 *)(fw->data + fw_idx));
+		fw_idx += 4;

sizeof(u32)?

Quoted 111 lines
drivers/input/touchscreen/apple_z2.c new file +111 -0
+		if (load_cmd == LOAD_COMMAND_INIT_PAYLOAD || load_cmd == LOAD_COMMAND_SEND_BLOB) {
+			size = le32_to_cpu(*(__le32 *)(fw->data + fw_idx));
+			fw_idx += 4;
+			if (fw->size - fw_idx < size) {
+				dev_err(&z2->spidev->dev, "firmware malformed");
+				return -EINVAL;
+			}
+			bits_per_word = load_cmd == LOAD_COMMAND_SEND_BLOB ? 16 : 8;
+			error = apple_z2_send_firmware_blob(z2, fw->data + fw_idx,
+							    size, bits_per_word);
+			if (error)
+				return error;
+			fw_idx += size;
+		} else if (load_cmd == 2) {
+			address = le32_to_cpu(*(u32 *)(fw->data + fw_idx));
+			fw_idx += 4;
+			cal_size = device_property_count_u8(&z2->spidev->dev, CAL_PROP_NAME);
+			if (cal_size != 0) {
+				size = cal_size + sizeof(struct apple_z2_hbpp_blob_hdr) + 4;
+				data = kzalloc(size, GFP_KERNEL);
+				error = apple_z2_build_cal_blob(z2, address, cal_size, data);
+				if (!error)
+					error = apple_z2_send_firmware_blob(z2, data, size, 16);
+				kfree(data);
+				if (error)
+					return error;
+			}
+		} else {
+			dev_err(&z2->spidev->dev, "firmware malformed");
+			return -EINVAL;
+		}
+		if (fw_idx % 4 != 0)
+			fw_idx += 4 - (fw_idx % 4);
+	}
+
+
+	z2->booted = 1;
+	apple_z2_read_packet(z2);
+	return 0;
+}
+
+static int apple_z2_boot(struct apple_z2 *z2)
+{
+	int timeout;
+	int error;
+
+	enable_irq(z2->spidev->irq);
+	gpiod_direction_output(z2->reset_gpio, 0);
+	timeout = wait_for_completion_timeout(&z2->boot_irq, msecs_to_jiffies(20));
+	if (timeout == 0)
+		return -ETIMEDOUT;
+
+	error = apple_z2_upload_firmware(z2);
+	if (error) {
+		gpiod_direction_output(z2->reset_gpio, 1);
+		disable_irq(z2->spidev->irq);
+	}
+	return error;
+}
+
+static int apple_z2_probe(struct spi_device *spi)
+{
+	struct device *dev = &spi->dev;
+	struct apple_z2 *z2;
+	int error;
+
+	z2 = devm_kzalloc(dev, sizeof(*z2), GFP_KERNEL);
+	if (!z2)
+		return -ENOMEM;
+
+	z2->spidev = spi;
+	init_completion(&z2->boot_irq);
+	spi_set_drvdata(spi, z2);
+
+	z2->reset_gpio = devm_gpiod_get_index(dev, "reset", 0, 0);
+	if (IS_ERR(z2->reset_gpio))
+		return dev_err_probe(dev, PTR_ERR(z2->reset_gpio), "unable to get reset");
+
+	error = devm_request_threaded_irq(dev, z2->spidev->irq, NULL,
+					apple_z2_irq, IRQF_ONESHOT | IRQF_NO_AUTOEN,
+					"apple-z2-irq", spi);
+	if (error < 0)
+		return dev_err_probe(dev, z2->spidev->irq, "unable to request irq");
+
+	error = device_property_read_string(dev, "firmware-name", &z2->fw_name);
+	if (error)
+		return dev_err_probe(dev, error, "unable to get firmware name");
+
+	z2->input_dev = devm_input_allocate_device(dev);
+	if (!z2->input_dev)
+		return -ENOMEM;
+	z2->input_dev->name = (char *)spi_get_device_id(spi)->driver_data;
+	z2->input_dev->phys = "apple_z2";
+	z2->input_dev->id.bustype = BUS_SPI;
+
+	/* Allocate the axes before setting from DT */
+	input_set_abs_params(z2->input_dev, ABS_MT_POSITION_X, 0, 0, 0, 0);
+	input_set_abs_params(z2->input_dev, ABS_MT_POSITION_Y, 0, 0, 0, 0);
+	touchscreen_parse_properties(z2->input_dev, true, &z2->props);
+	input_abs_set_res(z2->input_dev, ABS_MT_POSITION_X, 100);
+	input_abs_set_res(z2->input_dev, ABS_MT_POSITION_Y, 100);
+	input_set_abs_params(z2->input_dev, ABS_MT_WIDTH_MAJOR, 0, 65535, 0, 0);
+	input_set_abs_params(z2->input_dev, ABS_MT_WIDTH_MINOR, 0, 65535, 0, 0);
+	input_set_abs_params(z2->input_dev, ABS_MT_TOUCH_MAJOR, 0, 65535, 0, 0);
+	input_set_abs_params(z2->input_dev, ABS_MT_TOUCH_MINOR, 0, 65535, 0, 0);
+	input_set_abs_params(z2->input_dev, ABS_MT_ORIENTATION, -32768, 32767, 0, 0);
+
+	input_set_drvdata(z2->input_dev, z2);
+
+	error = input_mt_init_slots(z2->input_dev, 256, INPUT_MT_DIRECT);
+	if (error < 0)

drivers/input/touchscreen/apple_z2.c line 402

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+		return dev_err_probe(dev, error, "unable to initialize multitouch slots");
+
+	error = input_register_device(z2->input_dev);
+	if (error < 0)
+		return dev_err_probe(dev, error, "unable to register input device");
+
+	/* Reset the device on boot */
+	gpiod_direction_output(z2->reset_gpio, 1);
+	usleep_range(5000, 10000);
+	return apple_z2_boot(z2);
	error = apple_z2_boot(z2);
	if (error)
		return error;

	return 0;
Quoted 39 lines
drivers/input/touchscreen/apple_z2.c new file +39 -0
+}
+
+static void apple_z2_shutdown(struct spi_device *spi)
+{
+	struct apple_z2 *z2 = spi_get_drvdata(spi);
+
+	disable_irq(z2->spidev->irq);
+	gpiod_direction_output(z2->reset_gpio, 1);
+	z2->booted = 0;
+}
+
+static int apple_z2_suspend(struct device *dev)
+{
+	apple_z2_shutdown(to_spi_device(dev));
+	return 0;
+}
+
+static int apple_z2_resume(struct device *dev)
+{
+	struct apple_z2 *z2 = spi_get_drvdata(to_spi_device(dev));
+
+	return apple_z2_boot(z2);
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(apple_z2_pm, apple_z2_suspend, apple_z2_resume);
+
+static const struct of_device_id apple_z2_of_match[] = {
+	{ .compatible = "apple,j293-touchbar" },
+	{ .compatible = "apple,j493-touchbar" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, apple_z2_of_match);
+
+static struct spi_device_id apple_z2_of_id[] = {
+	{ .name = "j293-touchbar", .driver_data = (kernel_ulong_t)"MacBookPro17,1 Touch Bar" },
+	{ .name = "j493-touchbar", .driver_data = (kernel_ulong_t)"Mac14,7 Touch Bar" },
+	{}
+};
+MODULE_DEVICE_TABLE(spi, apple_z2_of_id);

drivers/input/touchscreen/apple_z2.c line 451

Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10 -0
+
+static struct spi_driver apple_z2_driver = {
+	.driver = {
+		.name	= "apple-z2",
+		.pm	= pm_sleep_ptr(&apple_z2_pm),
+		.of_match_table = apple_z2_of_match,
+	},
+	.id_table = apple_z2_of_id,
+	.probe    = apple_z2_probe,
+	.remove   = apple_z2_shutdown,

Purring the device into reset state before tearing down interrupt handler, etc, may lead to weird errors. Why do you need to do this on removal instead of letting devm tear down the device?

drivers/input/touchscreen/apple_z2.c line 452

Quoted 1 line
drivers/input/touchscreen/apple_z2.c new file +1 -0
+	.shutdown = apple_z2_shutdown,

Are you sure you need shutdown? Why?

Quoted 11 lines
drivers/input/touchscreen/apple_z2.c new file +6 -0
+};
+
+module_spi_driver(apple_z2_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_FIRMWARE("apple/dfrmtfw-*.bin");
Signature · 2 lines
-- 
2.47.1

Thanks.

Signature · 1 line
Dmitry
#8 lore raw

Re: [PATCH v2 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens

Quoted 4 lines · Dmitry Torokhov
Quoted 1 line

+ z2->counter = 1 - z2->counter;

Is this toggling between 1 and 0? Should it be "counter = !counter"? And is there better name than counter?

message_index_parity? Kind of a mouthful.

Quoted 5 lines
Quoted 1 line

+ .remove = apple_z2_shutdown,

Purring the device into reset state before tearing down interrupt handler, etc, may lead to weird errors. Why do you need to do this on removal instead of letting devm tear down the device?

The interrupt is masked before putting the device into reset, and i do need to put it into reset when removing it.

#9 lore raw

Re: [PATCH v2 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens

In reply to Sasha Finkelstein
kernel test robot noticed the following build warnings:

Hi Sasha,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 9f16d5e6f220661f73b36a4be1b21575651d8833]

url:    https://github.com/intel-lab-lkp/linux/commits/Sasha-Finkelstein-via-B4-Relay/dt-bindings-input-touchscreen-Add-Z2-controller/20241129-063212
base:   9f16d5e6f220661f73b36a4be1b21575651d8833
patch link:    https://lore.kernel.org/r/20241128-z2-v2-2-76cc59bbf117%40gmail.com
patch subject: [PATCH v2 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens
config: um-randconfig-r123-20241206 (https://download.01.org/0day-ci/archive/20241207/[email protected]/config)
compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241207/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> drivers/input/touchscreen/apple_z2.c:296:35: sparse: sparse: cast to restricted __le32

drivers/input/touchscreen/apple_z2.c: note: in included file (through arch/um/include/linux/time-internal.h, arch/x86/um/asm/processor.h, include/linux/sched.h, ...):
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true

vim +296 drivers/input/touchscreen/apple_z2.c

243
244 static int apple_z2_upload_firmware(struct apple_z2 *z2)
245 {
246 const struct firmware *fw __free(firmware) = NULL;
247 struct apple_z2_fw_hdr *fw_hdr;
248 size_t fw_idx = sizeof(struct apple_z2_fw_hdr);
249 int error;
250 u32 load_cmd;
251 u32 size;
252 u32 address;
253 char *data;
254 u8 bits_per_word;
255 size_t cal_size;
256
257 error = request_firmware(&fw, z2->fw_name, &z2->spidev->dev);
258 if (error) {
259 dev_err(&z2->spidev->dev, "unable to load firmware");
260 return error;
261 }
262
263 fw_hdr = (struct apple_z2_fw_hdr *)fw->data;
264 if (le32_to_cpu(fw_hdr->magic) != APPLE_Z2_FW_MAGIC || le32_to_cpu(fw_hdr->version) != 1) {
265 dev_err(&z2->spidev->dev, "invalid firmware header");
266 return -EINVAL;
267 }
268
269 /*
270 * This will interrupt the upload half-way if the file is malformed
271 * As the device has no non-volatile storage to corrupt, and gets reset
272 * on boot anyway, this is fine.
273 */
274 while (fw_idx < fw->size) {
275 if (fw->size - fw_idx < 8) {
276 dev_err(&z2->spidev->dev, "firmware malformed");
277 return -EINVAL;
278 }
279
280 load_cmd = le32_to_cpu(*(__le32 *)(fw->data + fw_idx));
281 fw_idx += 4;
282 if (load_cmd == LOAD_COMMAND_INIT_PAYLOAD || load_cmd == LOAD_COMMAND_SEND_BLOB) {
283 size = le32_to_cpu(*(__le32 *)(fw->data + fw_idx));
284 fw_idx += 4;
285 if (fw->size - fw_idx < size) {
286 dev_err(&z2->spidev->dev, "firmware malformed");
287 return -EINVAL;
288 }
289 bits_per_word = load_cmd == LOAD_COMMAND_SEND_BLOB ? 16 : 8;
290 error = apple_z2_send_firmware_blob(z2, fw->data + fw_idx,
291 size, bits_per_word);
292 if (error)
293 return error;
294 fw_idx += size;
295 } else if (load_cmd == 2) {

 > 296				address = le32_to_cpu(*(u32 *)(fw->data + fw_idx));

297 fw_idx += 4;
298 cal_size = device_property_count_u8(&z2->spidev->dev, CAL_PROP_NAME);
299 if (cal_size != 0) {
300 size = cal_size + sizeof(struct apple_z2_hbpp_blob_hdr) + 4;
301 data = kzalloc(size, GFP_KERNEL);
302 error = apple_z2_build_cal_blob(z2, address, cal_size, data);
303 if (!error)
304 error = apple_z2_send_firmware_blob(z2, data, size, 16);
305 kfree(data);
306 if (error)
307 return error;
308 }
309 } else {
310 dev_err(&z2->spidev->dev, "firmware malformed");
311 return -EINVAL;
312 }
313 if (fw_idx % 4 != 0)
314 fw_idx += 4 - (fw_idx % 4);
315 }
316
317
318 z2->booted = 1;
319 apple_z2_read_packet(z2);
320 return 0;
321 }
322

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki