Driver for Apple Z2 touchscreens.
Subsystems (4)
v5 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 v5:
- Added missing \ns, removed 4kb-ish of wasted ram
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Dropped another allocation, other fixes.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Tx/Rx buffers used in interrupt handler are now allocated in probe and reused
- Other various style fixes
- Link to v2: https://lore.kernel.org/r/[email protected]
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 | 70 ++++ MAINTAINERS | 2 + arch/arm64/boot/dts/apple/t8103-j293.dts | 27 ++ arch/arm64/boot/dts/apple/t8103.dtsi | 19 + arch/arm64/boot/dts/apple/t8112-j493.dts | 23 ++ arch/arm64/boot/dts/apple/t8112.dtsi | 14 + drivers/input/touchscreen/Kconfig | 13 + drivers/input/touchscreen/Makefile | 1 + drivers/input/touchscreen/apple_z2.c | 453 +++++++++++++++++++++ 9 files changed, 622 insertions(+)
---
base-commit: b62cef9a5c673f1b8083159f5dc03c1c5daced2f
change-id: 20241124-z2-c012b528ea0d
PATCH v5 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 | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+)
Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml new file +70 -0
@@ -0,0 +1,70 @@ +# 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 + maxItems: 4096 + 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.48.0
PATCH v5 2/4 input: apple_z2: Add a driver for Apple Z2 touchscreens
In reply to Sasha FinkelsteinFrom: 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 | 453 +++++++++++++++++++++++++++++++++++ 3 files changed, 467 insertions(+)
drivers/input/touchscreen/Kconfig +13 -0
@@ -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
@@ -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 +453 -0
453 lines not on this page · raw message
Signature · 1 line
2.48.0
Dmitry Torokhov
maintainerRe: [PATCH v5 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens
Hi Sasha,
Quoted 3 lines · On Sat, Jan 18, 2025 at 11:42:23PM +0100, Sasha Finkelstein via B4 Relay wrote:
From: Sasha Finkelstein <[email protected]>
Adds a driver for Apple touchscreens using the Z2 protocol.
A few more comments.
Quoted 123 lines
Signed-off-by Janne Grunau <[email protected]>Reviewed-by Neal Gompa <[email protected]>Signed-off-by Sasha Finkelstein <[email protected]>--- drivers/input/touchscreen/Kconfig | 13 + drivers/input/touchscreen/Makefile | 1 + drivers/input/touchscreen/apple_z2.c | 453 +++++++++++++++++++++++++++++++++++ 3 files changed, 467 insertions(+)drivers/input/touchscreen/Kconfig +13
-0@@ -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 && OFdrivers/input/touchscreen/Makefile +1
-0@@ -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.odrivers/input/touchscreen/apple_z2.c new file +71
-0@@ -0,0 +1,453 @@ +// 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 +#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; +bool booted; +int index_parity; +struct touchscreen_properties props; +const char *fw_name; +u8 *tx_buf; +u8 *rx_buf; +}; + +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; +} __packed; + +struct apple_z2_hbpp_blob_hdr { +__le16 cmd; +__le16 len; +__le32 addr; +__le16 checksum; +}; + +struct apple_z2_fw_hdr { +__le32 magic; +__le32 version;
drivers/input/touchscreen/apple_z2.c line 81
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+}; + +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)
const u8 *msg here and elsewhere were char * is used for binary data.
Quoted 51 lines
drivers/input/touchscreen/apple_z2.c new file +51
-0+{ +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); +} + +static int apple_z2_read_packet (struct apple_z2 *z2) +{ +struct apple_z2_read_interrupt_cmd *len_cmd = (void *)z2->tx_buf; +struct spi_transfer xfer; +int error; +size_t pkt_len; + +memset (&xfer,0 ,sizeof (xfer)); + len_cmd->cmd =APPLE_Z2_CMD_READ_INTERRUPT_DATA ; + len_cmd->counter = z2->index_parity +1 ; + len_cmd->checksum = +cpu_to_le16 (APPLE_Z2_CMD_READ_INTERRUPT_DATA + len_cmd->counter);
drivers/input/touchscreen/apple_z2.c line 142
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+ z2->index_parity = !z2->index_parity; + xfer.tx_buf = z2->tx_buf; + xfer.rx_buf = z2->rx_buf; + xfer.len =sizeof (*len_cmd); + + error =spi_sync_transfer (z2->spidev, &xfer,1 ); +if (error) +return error; + + pkt_len = (get_unaligned_le16 (z2->rx_buf +1 ) +8 ) & (-4 );
Negative mask is unusual. Can you replace it with a hex value? Do you know what is in the discarded bits?
Quoted 4 lines
drivers/input/touchscreen/apple_z2.c new file +4
-0+ + error =spi_read (z2->spidev, z2->rx_buf, pkt_len); +if (error) +return error;
drivers/input/touchscreen/apple_z2.c line 156
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+ +apple_z2_parse_touches (z2, z2->rx_buf +5 , pkt_len -5 ); + +return 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 you pass z2 to devm_request_threaded_irq you can avoid this call.
drivers/input/touchscreen/apple_z2.c line 168
Quoted 12 lines
drivers/input/touchscreen/apple_z2.c new file +12
-0+ +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 ;
I think this can be "round_up(cal_size, 4)".
Quoted 112 lines
drivers/input/touchscreen/apple_z2.c new file +112
-0+size_t hdr_size =sizeof (struct apple_z2_hbpp_blob_hdr ); +u32 checksum =0 ; +u16 checksum_hdr =0 ; +int i; +struct apple_z2_hbpp_blob_hdr *hdr; +int error; + + 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 + hdr_size, cal_size); +if (error) +return error; + +for (i =0 ; i < cal_size; i++) + checksum += data[i + hdr_size]; + +put_unaligned_le32 (checksum, data + cal_size + hdr_size); +return 0 ; +} + +static int apple_z2_send_firmware_blob (struct apple_z2 *z2,const char *data,u32 size,bool init) +{ +struct spi_message msg; +struct spi_transfer blob_xfer, ack_xfer; +int error; + + z2->tx_buf[0 ] =0x1a ; + z2->tx_buf[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 = init ?8 :16 ; +spi_message_add_tail (&blob_xfer, &msg); + + ack_xfer.tx_buf = z2->tx_buf; + ack_xfer.len =2 ; +spi_message_add_tail (&ack_xfer, &msg); + +reinit_completion (&z2->boot_irq); + error =spi_sync (z2->spidev, &msg); +if (error) +return error; + +/* Irq only happens sometimes, but the thing boots reliably nonetheless */ +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 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; +char *data; +bool init; +size_t cal_size; + + const struct firmware *fw __free(firmware) = NULL; + error =request_firmware (&fw, z2->fw_name, &z2->spidev->dev); +if (error) { +dev_err (&z2->spidev->dev,"unable to load firmware\n" ); +return error; + } + + fw_hdr = (const struct apple_z2_fw_hdr *)fw->data; +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\n" ); +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 +* on boot anyway, this is fine. +*/ +while (fw_idx < fw->size) { +if (fw->size - fw_idx <8 ) { +dev_err (&z2->spidev->dev,"firmware malformed\n" ); +return -EINVAL ; + } + + load_cmd =le32_to_cpu (*(__le32 *)(fw->data + fw_idx)); + fw_idx +=sizeof (u32); +if (load_cmd ==LOAD_COMMAND_INIT_PAYLOAD || load_cmd ==LOAD_COMMAND_SEND_BLOB ) { + size =le32_to_cpu (*(__le32 *)(fw->data + fw_idx)); + fw_idx +=sizeof (u32); +if (fw->size - fw_idx < size) { +dev_err (&z2->spidev->dev,"firmware malformed\n" ); +return -EINVAL ; + } + init = load_cmd ==LOAD_COMMAND_INIT_PAYLOAD ; + error =apple_z2_send_firmware_blob (z2, fw->data + fw_idx, + size, init); +if (error) +return error; + fw_idx += size;
drivers/input/touchscreen/apple_z2.c line 290
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+ }else if (load_cmd ==LOAD_COMMAND_SEND_CALIBRATION ) { + address =le32_to_cpu (*(u32 *)(fw->data + fw_idx)); + fw_idx +=sizeof (u32); + 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 );
I assume "16" supposed to be "true".
Overall I find the logic a bit confusing here. Can you please see if the patch at the bottom works for you?
drivers/input/touchscreen/apple_z2.c line 300
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+kfree (data); +if (error) +return error; + } + }else { +dev_err (&z2->spidev->dev,"firmware malformed\n" ); +return -EINVAL ; + } +if (fw_idx %4 !=0 ) + fw_idx +=4 - (fw_idx %4 );
I think this is "round_up(fw_idx, 4)" as well.
Quoted 9 lines
drivers/input/touchscreen/apple_z2.c new file +9
-0+ } + + + z2->booted = true; +apple_z2_read_packet (z2); +return 0 ; +} + +static int apple_z2_boot (struct apple_z2 *z2)
drivers/input/touchscreen/apple_z2.c line 319
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+{ +int timeout; +int error; + +reinit_completion (&z2->boot_irq); +enable_irq (z2->spidev->irq); +gpiod_set_value (z2->reset_gpio,0 ); + timeout =wait_for_completion_timeout (&z2->boot_irq,msecs_to_jiffies (20 )); +if (timeout ==0 ) +return -ETIMEDOUT ;
Shorter
if (!wait_for_completion_timeout(&z2->boot_irq, msecs_to_jiffies(20)) return -ETIMEDOUT:
drivers/input/touchscreen/apple_z2.c line 324
Quoted 5 lines
drivers/input/touchscreen/apple_z2.c new file +5
-0+ + error =apple_z2_upload_firmware (z2); +if (error) { +gpiod_set_value (z2->reset_gpio,1 ); +disable_irq (z2->spidev->irq);
return error;
drivers/input/touchscreen/apple_z2.c line 326
Quoted 2 lines
drivers/input/touchscreen/apple_z2.c new file +2
-0+ } +return error;
return 0;
Quoted 7 lines
drivers/input/touchscreen/apple_z2.c new file +7
-0+} + +static int apple_z2_probe (struct spi_device *spi) +{ +struct device *dev = &spi->dev; +struct apple_z2 *z2; +int error;
drivers/input/touchscreen/apple_z2.c line 343
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+ + z2 =devm_kzalloc (dev,sizeof (*z2),GFP_KERNEL ); +if (!z2) +return -ENOMEM ; + + z2->tx_buf =devm_kzalloc (dev,sizeof (struct apple_z2_read_interrupt_cmd ),GFP_KERNEL ); +/* 4096 will end up being rounded up to 8192 due to devres header */ + z2->rx_buf =devm_kzalloc (dev,4000 ,GFP_KERNEL ); +if (!z2->tx_buf || !z2->rx_buf) +return -ENOMEM ;
Can we please check immediately after allocation instead of combining the checks?
Quoted 15 lines
drivers/input/touchscreen/apple_z2.c new file +15
-0+ + z2->spidev = spi; +init_completion (&z2->boot_irq); +spi_set_drvdata (spi, z2); + +/* Reset the device on boot */ + z2->reset_gpio =devm_gpiod_get (dev,"reset" ,GPIOD_OUT_HIGH ); +if (IS_ERR (z2->reset_gpio)) +return dev_err_probe (dev,PTR_ERR (z2->reset_gpio),"unable to get reset\n" ); + + error =devm_request_threaded_irq (dev, z2->spidev->irq,NULL , + apple_z2_irq,IRQF_ONESHOT |IRQF_NO_AUTOEN , +"apple-z2-irq" , spi); +if (error) +return dev_err_probe (dev, error,"unable to request irq\n" );
drivers/input/touchscreen/apple_z2.c line 368
Quoted 10 lines
drivers/input/touchscreen/apple_z2.c new file +10
-0+ + 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->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" ;
Phys is supposed to be unique, however my understanding there could be 2 devices in the system?
Thanks.
Signature · 1 line
Dmitry
drivers/input/touchscreen/apple_z2.c +54 -34
@@ -78,7 +78,8 @@ struct apple_z2_read_interrupt_cmd {__le16 checksum; }; -static void apple_z2_parse_touches (struct apple_z2 *z2,char *msg,size_t msg_len) +static void apple_z2_parse_touches (struct apple_z2 *z2, +const u8 *msg,size_t msg_len) {int i;int nfingers;
@@ -101,8 +102,10 @@ static void apple_z2_parse_touches(struct apple_z2 *z2, char *msg, size_t msg_leinput_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); +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,
@@ -163,38 +166,58 @@ static irqreturn_t apple_z2_irq(int irq, void *data)return IRQ_HANDLED ; } -static int apple_z2_build_cal_blob (struct apple_z2 *z2,u32 address,size_t cal_size,char *data) +/* Build calibration blob, caller is responsible for freeing the blob data. */ +static const u8 *apple_z2_build_cal_blob (struct apple_z2 *z2, +u32 address,size_t *size) { -u16 len_words = (cal_size +3 ) /4 ; -size_t hdr_size =sizeof (struct apple_z2_hbpp_blob_hdr ); -u32 checksum =0 ; -u16 checksum_hdr =0 ; +u8 *cal_data; +int cal_size; +size_t blob_size; +u32 checksum; +u16 checksum_hdr;int i;struct apple_z2_hbpp_blob_hdr *hdr;int error; - hdr = (struct apple_z2_hbpp_blob_hdr *)data; +if (!device_property_present (&z2->spidev->dev,CAL_PROP_NAME )) +return NULL ; + + cal_size =device_property_count_u8 (&z2->spidev->dev,CAL_PROP_NAME ); +if (cal_size <0 ) +return ERR_PTR (cal_size); + + blob_size =sizeof (struct apple_z2_hbpp_blob_hdr ) + cal_size +sizeof (__le32); + u8 *blob_data __free(kfree) = kzalloc(blob_size, GFP_KERNEL); +if (!blob_data) +return ERR_PTR (-ENOMEM ); + + hdr = (struct apple_z2_hbpp_blob_hdr *)blob_data; hdr->cmd =cpu_to_le16 (APPLE_Z2_HBPP_CMD_BLOB ); - hdr->len =cpu_to_le16 (len_words); + hdr->len =cpu_to_le16 (round_up (cal_size,4 )); hdr->addr =cpu_to_le32 (address); + checksum_hdr =0 ;for (i =2 ; i <8 ; i++) - checksum_hdr += data[i]; - + checksum_hdr += blob_data[i]; hdr->checksum =cpu_to_le16 (checksum_hdr); + + cal_data = blob_data +sizeof (struct apple_z2_hbpp_blob_hdr ); error =device_property_read_u8_array (&z2->spidev->dev,CAL_PROP_NAME , - data + hdr_size, cal_size); + cal_data, cal_size);if (error) -return error; +return ERR_PTR (error); + checksum =0 ;for (i =0 ; i < cal_size; i++) - checksum += data[i + hdr_size]; + checksum += cal_data[i]; +put_unaligned_le32 (checksum, cal_data + cal_size); -put_unaligned_le32 (checksum, data + cal_size + hdr_size); -return 0 ; + *size = blob_size; +return no_free_ptr (blob_data); } -static int apple_z2_send_firmware_blob (struct apple_z2 *z2,const char *data,u32 size,bool init) +static int apple_z2_send_firmware_blob (struct apple_z2 *z2,const u8 *data, +u32 size,bool init) {struct spi_message msg;struct spi_transfer blob_xfer, ack_xfer;
@@ -233,11 +256,9 @@ static int apple_z2_upload_firmware(struct apple_z2 *z2)size_t fw_idx =sizeof (struct apple_z2_fw_hdr );int error;u32 load_cmd; -u32 size;u32 address; -char *data;bool init; -size_t cal_size; +size_t size; const struct firmware *fw __free(firmware) = NULL; error =request_firmware (&fw, z2->fw_name, &z2->spidev->dev);
@@ -263,10 +284,10 @@ static int apple_z2_upload_firmware(struct apple_z2 *z2)return -EINVAL ; } - load_cmd =le32_to_cpu (*(__le32 *)(fw->data + fw_idx)); + load_cmd =le32_to_cpup ((__force __le32 *)(fw->data + fw_idx)); fw_idx +=sizeof (u32);if (load_cmd ==LOAD_COMMAND_INIT_PAYLOAD || load_cmd ==LOAD_COMMAND_SEND_BLOB ) { - size =le32_to_cpu (*(__le32 *)(fw->data + fw_idx)); + size =le32_to_cpup ((__force __le32 *)(fw->data + fw_idx)); fw_idx +=sizeof (u32);if (fw->size - fw_idx < size) {dev_err (&z2->spidev->dev,"firmware malformed" );
@@ -279,16 +300,16 @@ static int apple_z2_upload_firmware(struct apple_z2 *z2)return error; fw_idx += size; }else if (load_cmd ==LOAD_COMMAND_SEND_CALIBRATION ) { - address =le32_to_cpu (*(u32 *)(fw->data + fw_idx)); + address =le32_to_cpup ((__force __le32 *)(fw->data + fw_idx)); fw_idx +=sizeof (u32); - 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); + + const u8 *data __free(kfree) = +apple_z2_build_cal_blob (z2, address, &size); +if (IS_ERR (data)) +return PTR_ERR (data); + +if (data) { + error =apple_z2_send_firmware_blob (z2, data, size, true);if (error)return error; }
@@ -296,8 +317,7 @@ static int apple_z2_upload_firmware(struct apple_z2 *z2)dev_err (&z2->spidev->dev,"firmware malformed" );return -EINVAL ; } -if (fw_idx %4 !=0 ) - fw_idx +=4 - (fw_idx %4 ); + fw_idx =round_up (fw_idx,4 ); }
Re: [PATCH v5 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens
Quoted 4 lines · Dmitry Torokhov
Quoted 1 line
+
z2->input_dev->phys= "apple_z2";Phys is supposed to be unique, however my understanding there could be 2 devices in the system?
All existing devices have at most one z2 device, and while i do not have visibility
into future apple product decisions, judging by the current stack, it seems unlikely for them to make one that needs two of them.
drivers/input/touchscreen/apple_z2.c static irqreturn_t apple_z2_irq(int irq, void *data) · line 169
Quoted 2 lines
drivers/input/touchscreen/apple_z2.c +1
-1-static int apple_z2_build_cal_blob (struct apple_z2 *z2,u32 address,size_t cal_size,char *data) +/* Build calibration blob, caller is responsible for freeing the blob data. */
A comment on a previous version of this patch requested to not have functions that require the caller to free the return value
https://lore.kernel.org/all/ZAlM2DzMmwzWIZEF@nixie71/
Dmitry Torokhov
maintainerRe: [PATCH v5 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens
Quoted 11 lines · Sasha Finkelstein
Quoted 4 lines · Dmitry Torokhov
Quoted 1 line
+
z2->input_dev->phys= "apple_z2";Phys is supposed to be unique, however my understanding there could be 2 devices in the system?
All existing devices have at most one z2 device, and while i do not have visibility
into future apple product decisions, judging by the current stack, it seems unlikely for them to make one that needs two of them.
Quoted 6 lines
drivers/input/touchscreen/apple_z2.c static irqreturn_t apple_z2_irq(int irq, void *data) · line 169
Quoted 2 lines
drivers/input/touchscreen/apple_z2.c +1
-1-static int apple_z2_build_cal_blob (struct apple_z2 *z2,u32 address,size_t cal_size,char *data) +/* Build calibration blob, caller is responsible for freeing the blob data. */ A comment on a previous version of this patch requested to not have functions that require the caller to free the return value
https://lore.kernel.org/all/ZAlM2DzMmwzWIZEF@nixie71/
You have to pick your poison. Either the caller has to inspect the property to figure out the size of the allocation, handle errors, and provide diagnostic, and then have apple_z2_build_cal_blob() re-parse the property and fill the provided buffer, or you can hide it all in apple_z2_build_cal_blob() and task the caller with freeing the blob when they're done with it. Similar to request_firmware() and put_firmware().
I think the latter works better in this particular case.
Thanks.
Signature · 1 line
Dmitry
Re: [PATCH v5 2/4] input: apple_z2: Add a driver for Apple Z2 touchscreens
In reply to Dmitry Torokhovdrivers/input/touchscreen/apple_z2.c
Quoted 2 lines · Dmitry Torokhov
drivers/input/touchscreen/apple_z2.c +0
-0
Since i will be incorporating this into my patch, do you want a co-developed-by?
PATCH v5 3/4 arm64: dts: apple: Add touchbar digitizer nodes
In reply to Sasha FinkelsteinFrom: Sasha Finkelstein <[email protected]>
Adds device tree entries for the touchbar digitizer
--- arch/arm64/boot/dts/apple/t8103-j293.dts | 27 +++++++++++++++++++++++++++ arch/arm64/boot/dts/apple/t8103.dtsi | 19 +++++++++++++++++++ arch/arm64/boot/dts/apple/t8112-j493.dts | 23 +++++++++++++++++++++++ arch/arm64/boot/dts/apple/t8112.dtsi | 14 ++++++++++++++ 4 files changed, 83 insertions(+)
arch/arm64/boot/dts/apple/t8103-j293.dts +27 -0
@@ -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,22 @@ &i2c4 { &fpwm1 { status = "okay"; }; + +&spi0 { + cs-gpios = <&pinctrl_ap 109 GPIO_ACTIVE_LOW>; + 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 +19 -0
@@ -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,18 @@ 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>; + 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 +23 -0
@@ -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,21 @@ &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
@@ -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"; /* only used in J493 */ + }; + serial0: serial@235200000 { compatible = "apple,s5l-uart"; reg = <0x2 0x35200000 0x0 0x1000>;
Signature · 1 line
2.48.0
PATCH v5 4/4 MAINTAINERS: Add entries for Apple Z2 touchscreen driver
In reply to Sasha FinkelsteinAdd the MAINTAINERS entries for the driver
--- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
@@ -2191,6 +2191,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
@@ -2211,6 +2212,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.48.0