1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
// 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_REPLY_INTERRUPT_DATA 0xE1
#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;
};
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,
const u8 *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);
}
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);
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;
if (z2->rx_buf[0] != APPLE_Z2_REPLY_INTERRUPT_DATA)
return 0;
pkt_len = (get_unaligned_le16(z2->rx_buf + 1) + 8) & 0xfffffffc;
error = spi_read(z2->spidev, z2->rx_buf, pkt_len);
if (error)
return error;
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 apple_z2 *z2 = data;
if (unlikely(!z2->booted))
complete(&z2->boot_irq);
else
apple_z2_read_packet(z2);
return IRQ_HANDLED;
}
/* 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)
{
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;
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(round_up(cal_size, 4) / 4);
hdr->addr = cpu_to_le32(address);
checksum_hdr = 0;
for (i = 2; i < 8; 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,
cal_data, cal_size);
if (error)
return ERR_PTR(error);
checksum = 0;
for (i = 0; i < cal_size; i++)
checksum += cal_data[i];
put_unaligned_le32(checksum, cal_data + cal_size);
*size = blob_size;
return no_free_ptr(blob_data);
}
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;
int error;
z2->tx_buf[
|