xref: /linux/drivers/input/touchscreen/apple_z2.c (revision c31f4aa8fed048fa70e742c4bb49bb48dc489ab3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Apple Z2 touchscreen driver
4  *
5  * Copyright (C) The Asahi Linux Contributors
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/firmware.h>
10 #include <linux/input.h>
11 #include <linux/input/mt.h>
12 #include <linux/input/touchscreen.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/spi/spi.h>
17 #include <linux/unaligned.h>
18 
19 #define APPLE_Z2_NUM_FINGERS_OFFSET      16
20 #define APPLE_Z2_FINGERS_OFFSET          24
21 #define APPLE_Z2_TOUCH_STARTED           3
22 #define APPLE_Z2_TOUCH_MOVED             4
23 #define APPLE_Z2_CMD_READ_INTERRUPT_DATA 0xEB
24 #define APPLE_Z2_REPLY_INTERRUPT_DATA    0xE1
25 #define APPLE_Z2_HBPP_CMD_BLOB           0x3001
26 #define APPLE_Z2_FW_MAGIC                0x5746325A
27 #define LOAD_COMMAND_INIT_PAYLOAD        0
28 #define LOAD_COMMAND_SEND_BLOB           1
29 #define LOAD_COMMAND_SEND_CALIBRATION    2
30 #define CAL_PROP_NAME                    "apple,z2-cal-blob"
31 
32 struct apple_z2 {
33 	struct spi_device *spidev;
34 	struct gpio_desc *reset_gpio;
35 	struct input_dev *input_dev;
36 	struct completion boot_irq;
37 	bool booted;
38 	int index_parity;
39 	struct touchscreen_properties props;
40 	const char *fw_name;
41 	u8 *tx_buf;
42 	u8 *rx_buf;
43 };
44 
45 struct apple_z2_finger {
46 	u8 finger;
47 	u8 state;
48 	__le16 unknown2;
49 	__le16 abs_x;
50 	__le16 abs_y;
51 	__le16 rel_x;
52 	__le16 rel_y;
53 	__le16 tool_major;
54 	__le16 tool_minor;
55 	__le16 orientation;
56 	__le16 touch_major;
57 	__le16 touch_minor;
58 	__le16 unused[2];
59 	__le16 pressure;
60 	__le16 multi;
61 } __packed;
62 
63 struct apple_z2_hbpp_blob_hdr {
64 	__le16 cmd;
65 	__le16 len;
66 	__le32 addr;
67 	__le16 checksum;
68 };
69 
70 struct apple_z2_fw_hdr {
71 	__le32 magic;
72 	__le32 version;
73 };
74 
75 struct apple_z2_read_interrupt_cmd {
76 	u8 cmd;
77 	u8 counter;
78 	u8 unused[12];
79 	__le16 checksum;
80 };
81 
82 static void apple_z2_parse_touches(struct apple_z2 *z2,
83 				   const u8 *msg, size_t msg_len)
84 {
85 	int i;
86 	int nfingers;
87 	int slot;
88 	int slot_valid;
89 	struct apple_z2_finger *fingers;
90 
91 	if (msg_len <= APPLE_Z2_NUM_FINGERS_OFFSET)
92 		return;
93 	nfingers = msg[APPLE_Z2_NUM_FINGERS_OFFSET];
94 	fingers = (struct apple_z2_finger *)(msg + APPLE_Z2_FINGERS_OFFSET);
95 	for (i = 0; i < nfingers; i++) {
96 		slot = input_mt_get_slot_by_key(z2->input_dev, fingers[i].finger);
97 		if (slot < 0) {
98 			dev_warn(&z2->spidev->dev, "unable to get slot for finger\n");
99 			continue;
100 		}
101 		slot_valid = fingers[i].state == APPLE_Z2_TOUCH_STARTED ||
102 			     fingers[i].state == APPLE_Z2_TOUCH_MOVED;
103 		input_mt_slot(z2->input_dev, slot);
104 		if (!input_mt_report_slot_state(z2->input_dev, MT_TOOL_FINGER, slot_valid))
105 			continue;
106 		touchscreen_report_pos(z2->input_dev, &z2->props,
107 				       le16_to_cpu(fingers[i].abs_x),
108 				       le16_to_cpu(fingers[i].abs_y),
109 				       true);
110 		input_report_abs(z2->input_dev, ABS_MT_WIDTH_MAJOR,
111 				 le16_to_cpu(fingers[i].tool_major));
112 		input_report_abs(z2->input_dev, ABS_MT_WIDTH_MINOR,
113 				 le16_to_cpu(fingers[i].tool_minor));
114 		input_report_abs(z2->input_dev, ABS_MT_ORIENTATION,
115 				 le16_to_cpu(fingers[i].orientation));
116 		input_report_abs(z2->input_dev, ABS_MT_TOUCH_MAJOR,
117 				 le16_to_cpu(fingers[i].touch_major));
118 		input_report_abs(z2->input_dev, ABS_MT_TOUCH_MINOR,
119 				 le16_to_cpu(fingers[i].touch_minor));
120 	}
121 	input_mt_sync_frame(z2->input_dev);
122 	input_sync(z2->input_dev);
123 }
124 
125 static int apple_z2_read_packet(struct apple_z2 *z2)
126 {
127 	struct apple_z2_read_interrupt_cmd *len_cmd = (void *)z2->tx_buf;
128 	struct spi_transfer xfer;
129 	int error;
130 	size_t pkt_len;
131 
132 	memset(&xfer, 0, sizeof(xfer));
133 	len_cmd->cmd = APPLE_Z2_CMD_READ_INTERRUPT_DATA;
134 	len_cmd->counter = z2->index_parity + 1;
135 	len_cmd->checksum =
136 		cpu_to_le16(APPLE_Z2_CMD_READ_INTERRUPT_DATA + len_cmd->counter);
137 	z2->index_parity = !z2->index_parity;
138 	xfer.tx_buf = z2->tx_buf;
139 	xfer.rx_buf = z2->rx_buf;
140 	xfer.len = sizeof(*len_cmd);
141 
142 	error = spi_sync_transfer(z2->spidev, &xfer, 1);
143 	if (error)
144 		return error;
145 
146 	if (z2->rx_buf[0] != APPLE_Z2_REPLY_INTERRUPT_DATA)
147 		return 0;
148 
149 	pkt_len = (get_unaligned_le16(z2->rx_buf + 1) + 8) & 0xfffffffc;
150 
151 	error = spi_read(z2->spidev, z2->rx_buf, pkt_len);
152 	if (error)
153 		return error;
154 
155 	apple_z2_parse_touches(z2, z2->rx_buf + 5, pkt_len - 5);
156 
157 	return 0;
158 }
159 
160 static irqreturn_t apple_z2_irq(int irq, void *data)
161 {
162 	struct apple_z2 *z2 = data;
163 
164 	if (unlikely(!z2->booted))
165 		complete(&z2->boot_irq);
166 	else
167 		apple_z2_read_packet(z2);
168 
169 	return IRQ_HANDLED;
170 }
171 
172 /* Build calibration blob, caller is responsible for freeing the blob data. */
173 static const u8 *apple_z2_build_cal_blob(struct apple_z2 *z2,
174 					 u32 address, size_t *size)
175 {
176 	u8 *cal_data;
177 	int cal_size;
178 	size_t blob_size;
179 	u32 checksum;
180 	u16 checksum_hdr;
181 	int i;
182 	struct apple_z2_hbpp_blob_hdr *hdr;
183 	int error;
184 
185 	if (!device_property_present(&z2->spidev->dev, CAL_PROP_NAME))
186 		return NULL;
187 
188 	cal_size = device_property_count_u8(&z2->spidev->dev, CAL_PROP_NAME);
189 	if (cal_size < 0)
190 		return ERR_PTR(cal_size);
191 
192 	blob_size = sizeof(struct apple_z2_hbpp_blob_hdr) + cal_size + sizeof(__le32);
193 	u8 *blob_data __free(kfree) = kzalloc(blob_size, GFP_KERNEL);
194 	if (!blob_data)
195 		return ERR_PTR(-ENOMEM);
196 
197 	hdr = (struct apple_z2_hbpp_blob_hdr *)blob_data;
198 	hdr->cmd = cpu_to_le16(APPLE_Z2_HBPP_CMD_BLOB);
199 	hdr->len = cpu_to_le16(round_up(cal_size, 4) / 4);
200 	hdr->addr = cpu_to_le32(address);
201 
202 	checksum_hdr = 0;
203 	for (i = 2; i < 8; i++)
204 		checksum_hdr += blob_data[i];
205 	hdr->checksum = cpu_to_le16(checksum_hdr);
206 
207 	cal_data = blob_data + sizeof(struct apple_z2_hbpp_blob_hdr);
208 	error = device_property_read_u8_array(&z2->spidev->dev, CAL_PROP_NAME,
209 					      cal_data, cal_size);
210 	if (error)
211 		return ERR_PTR(error);
212 
213 	checksum = 0;
214 	for (i = 0; i < cal_size; i++)
215 		checksum += cal_data[i];
216 	put_unaligned_le32(checksum, cal_data + cal_size);
217 
218 	*size = blob_size;
219 	return no_free_ptr(blob_data);
220 }
221 
222 static int apple_z2_send_firmware_blob(struct apple_z2 *z2, const u8 *data,
223 				       u32 size, bool init)
224 {
225 	struct spi_message msg;
226 	struct spi_transfer blob_xfer, ack_xfer;
227 	int error;
228 
229 	z2->tx_buf[0] = 0x1a;
230 	z2->tx_buf[1] = 0xa1;
231 
232 	spi_message_init(&msg);
233 	memset(&blob_xfer, 0, sizeof(blob_xfer));
234 	memset(&ack_xfer, 0, sizeof(ack_xfer));
235 
236 	blob_xfer.tx_buf = data;
237 	blob_xfer.len = size;
238 	blob_xfer.bits_per_word = init ? 8 : 16;
239 	spi_message_add_tail(&blob_xfer, &msg);
240 
241 	ack_xfer.tx_buf = z2->tx_buf;
242 	ack_xfer.len = 2;
243 	spi_message_add_tail(&ack_xfer, &msg);
244 
245 	reinit_completion(&z2->boot_irq);
246 	error = spi_sync(z2->spidev, &msg);
247 	if (error)
248 		return error;
249 
250 	/* Irq only happens sometimes, but the thing boots reliably nonetheless */
251 	wait_for_completion_timeout(&z2->boot_irq, msecs_to_jiffies(20));
252 
253 	return 0;
254 }
255 
256 static int apple_z2_upload_firmware(struct apple_z2 *z2)
257 {
258 	const struct apple_z2_fw_hdr *fw_hdr;
259 	size_t fw_idx = sizeof(struct apple_z2_fw_hdr);
260 	int error;
261 	u32 load_cmd;
262 	u32 address;
263 	bool init;
264 	size_t size;
265 
266 	const struct firmware *fw __free(firmware) = NULL;
267 	error = request_firmware(&fw, z2->fw_name, &z2->spidev->dev);
268 	if (error) {
269 		dev_err(&z2->spidev->dev, "unable to load firmware\n");
270 		return error;
271 	}
272 
273 	fw_hdr = (const struct apple_z2_fw_hdr *)fw->data;
274 	if (le32_to_cpu(fw_hdr->magic) != APPLE_Z2_FW_MAGIC || le32_to_cpu(fw_hdr->version) != 1) {
275 		dev_err(&z2->spidev->dev, "invalid firmware header\n");
276 		return -EINVAL;
277 	}
278 
279 	/*
280 	 * This will interrupt the upload half-way if the file is malformed
281 	 * As the device has no non-volatile storage to corrupt, and gets reset
282 	 * on boot anyway, this is fine.
283 	 */
284 	while (fw_idx < fw->size) {
285 		if (fw->size - fw_idx < 8) {
286 			dev_err(&z2->spidev->dev, "firmware malformed\n");
287 			return -EINVAL;
288 		}
289 
290 		load_cmd = le32_to_cpup((__force __le32 *)(fw->data + fw_idx));
291 		fw_idx += sizeof(u32);
292 		if (load_cmd == LOAD_COMMAND_INIT_PAYLOAD || load_cmd == LOAD_COMMAND_SEND_BLOB) {
293 			size = le32_to_cpup((__force __le32 *)(fw->data + fw_idx));
294 			fw_idx += sizeof(u32);
295 			if (fw->size - fw_idx < size) {
296 				dev_err(&z2->spidev->dev, "firmware malformed\n");
297 				return -EINVAL;
298 			}
299 			init = load_cmd == LOAD_COMMAND_INIT_PAYLOAD;
300 			error = apple_z2_send_firmware_blob(z2, fw->data + fw_idx,
301 							    size, init);
302 			if (error)
303 				return error;
304 			fw_idx += size;
305 		} else if (load_cmd == LOAD_COMMAND_SEND_CALIBRATION) {
306 			address = le32_to_cpup((__force __le32 *)(fw->data + fw_idx));
307 			fw_idx += sizeof(u32);
308 
309 			const u8 *data __free(kfree) =
310 				apple_z2_build_cal_blob(z2, address, &size);
311 			if (IS_ERR(data))
312 				return PTR_ERR(data);
313 
314 			if (data) {
315 				error = apple_z2_send_firmware_blob(z2, data, size, false);
316 				if (error)
317 					return error;
318 			}
319 		} else {
320 			dev_err(&z2->spidev->dev, "firmware malformed\n");
321 			return -EINVAL;
322 		}
323 		fw_idx = round_up(fw_idx, 4);
324 	}
325 
326 
327 	z2->booted = true;
328 	apple_z2_read_packet(z2);
329 	return 0;
330 }
331 
332 static int apple_z2_boot(struct apple_z2 *z2)
333 {
334 	int error;
335 
336 	reinit_completion(&z2->boot_irq);
337 	enable_irq(z2->spidev->irq);
338 	gpiod_set_value(z2->reset_gpio, 0);
339 	if (!wait_for_completion_timeout(&z2->boot_irq, msecs_to_jiffies(20)))
340 		return -ETIMEDOUT;
341 
342 	error = apple_z2_upload_firmware(z2);
343 	if (error) {
344 		gpiod_set_value(z2->reset_gpio, 1);
345 		disable_irq(z2->spidev->irq);
346 		return error;
347 	}
348 
349 	return 0;
350 }
351 
352 static int apple_z2_probe(struct spi_device *spi)
353 {
354 	struct device *dev = &spi->dev;
355 	struct apple_z2 *z2;
356 	int error;
357 
358 	z2 = devm_kzalloc(dev, sizeof(*z2), GFP_KERNEL);
359 	if (!z2)
360 		return -ENOMEM;
361 
362 	z2->tx_buf = devm_kzalloc(dev, sizeof(struct apple_z2_read_interrupt_cmd), GFP_KERNEL);
363 	if (!z2->tx_buf)
364 		return -ENOMEM;
365 	/* 4096 will end up being rounded up to 8192 due to devres header */
366 	z2->rx_buf = devm_kzalloc(dev, 4000, GFP_KERNEL);
367 	if (!z2->rx_buf)
368 		return -ENOMEM;
369 
370 	z2->spidev = spi;
371 	init_completion(&z2->boot_irq);
372 	spi_set_drvdata(spi, z2);
373 
374 	/* Reset the device on boot */
375 	z2->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
376 	if (IS_ERR(z2->reset_gpio))
377 		return dev_err_probe(dev, PTR_ERR(z2->reset_gpio), "unable to get reset\n");
378 
379 	error = devm_request_threaded_irq(dev, z2->spidev->irq, NULL, apple_z2_irq,
380 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
381 					  "apple-z2-irq", z2);
382 	if (error)
383 		return dev_err_probe(dev, error, "unable to request irq\n");
384 
385 	error = device_property_read_string(dev, "firmware-name", &z2->fw_name);
386 	if (error)
387 		return dev_err_probe(dev, error, "unable to get firmware name\n");
388 
389 	z2->input_dev = devm_input_allocate_device(dev);
390 	if (!z2->input_dev)
391 		return -ENOMEM;
392 
393 	z2->input_dev->name = (char *)spi_get_device_id(spi)->driver_data;
394 	z2->input_dev->phys = "apple_z2";
395 	z2->input_dev->id.bustype = BUS_SPI;
396 
397 	/* Allocate the axes before setting from DT */
398 	input_set_abs_params(z2->input_dev, ABS_MT_POSITION_X, 0, 0, 0, 0);
399 	input_set_abs_params(z2->input_dev, ABS_MT_POSITION_Y, 0, 0, 0, 0);
400 	touchscreen_parse_properties(z2->input_dev, true, &z2->props);
401 	input_abs_set_res(z2->input_dev, ABS_MT_POSITION_X, 100);
402 	input_abs_set_res(z2->input_dev, ABS_MT_POSITION_Y, 100);
403 	input_set_abs_params(z2->input_dev, ABS_MT_WIDTH_MAJOR, 0, 65535, 0, 0);
404 	input_set_abs_params(z2->input_dev, ABS_MT_WIDTH_MINOR, 0, 65535, 0, 0);
405 	input_set_abs_params(z2->input_dev, ABS_MT_TOUCH_MAJOR, 0, 65535, 0, 0);
406 	input_set_abs_params(z2->input_dev, ABS_MT_TOUCH_MINOR, 0, 65535, 0, 0);
407 	input_set_abs_params(z2->input_dev, ABS_MT_ORIENTATION, -32768, 32767, 0, 0);
408 
409 	error = input_mt_init_slots(z2->input_dev, 256, INPUT_MT_DIRECT);
410 	if (error)
411 		return dev_err_probe(dev, error, "unable to initialize multitouch slots\n");
412 
413 	error = input_register_device(z2->input_dev);
414 	if (error)
415 		return dev_err_probe(dev, error, "unable to register input device\n");
416 
417 	/* Wait for device reset to finish */
418 	usleep_range(5000, 10000);
419 	error = apple_z2_boot(z2);
420 	if (error)
421 		return error;
422 
423 	return 0;
424 }
425 
426 static void apple_z2_shutdown(struct spi_device *spi)
427 {
428 	struct apple_z2 *z2 = spi_get_drvdata(spi);
429 
430 	disable_irq(z2->spidev->irq);
431 	gpiod_direction_output(z2->reset_gpio, 1);
432 	z2->booted = false;
433 }
434 
435 static int apple_z2_suspend(struct device *dev)
436 {
437 	apple_z2_shutdown(to_spi_device(dev));
438 
439 	return 0;
440 }
441 
442 static int apple_z2_resume(struct device *dev)
443 {
444 	struct apple_z2 *z2 = spi_get_drvdata(to_spi_device(dev));
445 
446 	return apple_z2_boot(z2);
447 }
448 
449 static DEFINE_SIMPLE_DEV_PM_OPS(apple_z2_pm, apple_z2_suspend, apple_z2_resume);
450 
451 static const struct of_device_id apple_z2_of_match[] = {
452 	{ .compatible = "apple,j293-touchbar" },
453 	{ .compatible = "apple,j493-touchbar" },
454 	{}
455 };
456 MODULE_DEVICE_TABLE(of, apple_z2_of_match);
457 
458 static struct spi_device_id apple_z2_of_id[] = {
459 	{ .name = "j293-touchbar", .driver_data = (kernel_ulong_t)"MacBookPro17,1 Touch Bar" },
460 	{ .name = "j493-touchbar", .driver_data = (kernel_ulong_t)"Mac14,7 Touch Bar" },
461 	{}
462 };
463 MODULE_DEVICE_TABLE(spi, apple_z2_of_id);
464 
465 static struct spi_driver apple_z2_driver = {
466 	.driver = {
467 		.name	= "apple-z2",
468 		.pm	= pm_sleep_ptr(&apple_z2_pm),
469 		.of_match_table = apple_z2_of_match,
470 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
471 	},
472 	.id_table = apple_z2_of_id,
473 	.probe    = apple_z2_probe,
474 	.remove   = apple_z2_shutdown,
475 };
476 
477 module_spi_driver(apple_z2_driver);
478 
479 MODULE_LICENSE("GPL");
480 MODULE_FIRMWARE("apple/dfrmtfw-*.bin");
481 MODULE_DESCRIPTION("Apple Z2 touchscreens driver");
482