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