1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * This is a driver for the keyboard, touchpad and USB port of the
4 * keyboard dock for the Asus TF103C 2-in-1 tablet.
5 *
6 * This keyboard dock has its own I2C attached embedded controller
7 * and the keyboard and touchpad are also connected over I2C,
8 * instead of using the usual USB connection. This means that the
9 * keyboard dock requires this special driver to function.
10 *
11 * Copyright (C) 2021 Hans de Goede <hdegoede@redhat.com>
12 */
13
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/dmi.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/hid.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/irq.h>
23 #include <linux/irqdomain.h>
24 #include <linux/moduleparam.h>
25 #include <linux/module.h>
26 #include <linux/pm.h>
27 #include <linux/workqueue.h>
28 #include <linux/unaligned.h>
29
30 static bool fnlock;
31 module_param(fnlock, bool, 0644);
32 MODULE_PARM_DESC(fnlock,
33 "By default the kbd toprow sends multimedia key presses. AltGr "
34 "can be pressed to change this to F1-F12. Set this to 1 to "
35 "change the default. Press AltGr + Esc to toggle at runtime.");
36
37 #define TF103C_DOCK_DEV_NAME "NPCE69A:00"
38
39 #define TF103C_DOCK_HPD_DEBOUNCE msecs_to_jiffies(20)
40
41 /*** Touchpad I2C device defines ***/
42 #define TF103C_DOCK_TP_ADDR 0x15
43
44 /*** Keyboard I2C device defines **A*/
45 #define TF103C_DOCK_KBD_ADDR 0x16
46
47 #define TF103C_DOCK_KBD_DATA_REG 0x73
48 #define TF103C_DOCK_KBD_DATA_MIN_LENGTH 4
49 #define TF103C_DOCK_KBD_DATA_MAX_LENGTH 11
50 #define TF103C_DOCK_KBD_DATA_MODIFIERS 3
51 #define TF103C_DOCK_KBD_DATA_KEYS 5
52 #define TF103C_DOCK_KBD_CMD_REG 0x75
53
54 #define TF103C_DOCK_KBD_CMD_ENABLE 0x0800
55
56 /*** EC innterrupt data I2C device defines ***/
57 #define TF103C_DOCK_INTR_ADDR 0x19
58 #define TF103C_DOCK_INTR_DATA_REG 0x6a
59
60 #define TF103C_DOCK_INTR_DATA1_OBF_MASK 0x01
61 #define TF103C_DOCK_INTR_DATA1_KEY_MASK 0x04
62 #define TF103C_DOCK_INTR_DATA1_KBC_MASK 0x08
63 #define TF103C_DOCK_INTR_DATA1_AUX_MASK 0x20
64 #define TF103C_DOCK_INTR_DATA1_SCI_MASK 0x40
65 #define TF103C_DOCK_INTR_DATA1_SMI_MASK 0x80
66 /* Special values for the OOB data on kbd_client / tp_client */
67 #define TF103C_DOCK_INTR_DATA1_OOB_VALUE 0xc1
68 #define TF103C_DOCK_INTR_DATA2_OOB_VALUE 0x04
69
70 #define TF103C_DOCK_SMI_AC_EVENT 0x31
71 #define TF103C_DOCK_SMI_HANDSHAKING 0x50
72 #define TF103C_DOCK_SMI_EC_WAKEUP 0x53
73 #define TF103C_DOCK_SMI_BOOTBLOCK_RESET 0x5e
74 #define TF103C_DOCK_SMI_WATCHDOG_RESET 0x5f
75 #define TF103C_DOCK_SMI_ADAPTER_CHANGE 0x60
76 #define TF103C_DOCK_SMI_DOCK_INSERT 0x61
77 #define TF103C_DOCK_SMI_DOCK_REMOVE 0x62
78 #define TF103C_DOCK_SMI_PAD_BL_CHANGE 0x63
79 #define TF103C_DOCK_SMI_HID_STATUS_CHANGED 0x64
80 #define TF103C_DOCK_SMI_HID_WAKEUP 0x65
81 #define TF103C_DOCK_SMI_S3 0x83
82 #define TF103C_DOCK_SMI_S5 0x85
83 #define TF103C_DOCK_SMI_NOTIFY_SHUTDOWN 0x90
84 #define TF103C_DOCK_SMI_RESUME 0x91
85
86 /*** EC (dockram) I2C device defines ***/
87 #define TF103C_DOCK_EC_ADDR 0x1b
88
89 #define TF103C_DOCK_EC_CMD_REG 0x0a
90 #define TF103C_DOCK_EC_CMD_LEN 9
91
92 enum {
93 TF103C_DOCK_FLAG_HID_OPEN,
94 };
95
96 struct tf103c_dock_data {
97 struct delayed_work hpd_work;
98 struct irq_chip tp_irqchip;
99 struct irq_domain *tp_irq_domain;
100 struct i2c_client *ec_client;
101 struct i2c_client *intr_client;
102 struct i2c_client *kbd_client;
103 struct i2c_client *tp_client;
104 struct gpio_desc *pwr_en;
105 struct gpio_desc *irq_gpio;
106 struct gpio_desc *hpd_gpio;
107 struct input_dev *input;
108 struct hid_device *hid;
109 unsigned long flags;
110 int board_rev;
111 int irq;
112 int hpd_irq;
113 int tp_irq;
114 int last_press_0x13;
115 int last_press_0x14;
116 bool enabled;
117 bool tp_enabled;
118 bool altgr_pressed;
119 bool esc_pressed;
120 bool filter_esc;
121 u8 kbd_buf[TF103C_DOCK_KBD_DATA_MAX_LENGTH];
122 };
123
124 static struct gpiod_lookup_table tf103c_dock_gpios = {
125 .dev_id = "i2c-" TF103C_DOCK_DEV_NAME,
126 .table = {
127 GPIO_LOOKUP("INT33FC:00", 55, "dock_pwr_en", GPIO_ACTIVE_HIGH),
128 GPIO_LOOKUP("INT33FC:02", 1, "dock_irq", GPIO_ACTIVE_HIGH),
129 GPIO_LOOKUP("INT33FC:02", 29, "dock_hpd", GPIO_ACTIVE_HIGH),
130 GPIO_LOOKUP("gpio_crystalcove", 2, "board_rev", GPIO_ACTIVE_HIGH),
131 {}
132 },
133 };
134
135 /* Byte 0 is the length of the rest of the packet */
136 static const u8 tf103c_dock_enable_cmd[9] = { 8, 0x20, 0, 0, 0, 0, 0x20, 0, 0 };
137 static const u8 tf103c_dock_usb_enable_cmd[9] = { 8, 0, 0, 0, 0, 0, 0, 0x40, 0 };
138 static const u8 tf103c_dock_suspend_cmd[9] = { 8, 0, 0x20, 0, 0, 0x22, 0, 0, 0 };
139
140 /*** keyboard related code ***/
141
142 static u8 tf103c_dock_kbd_hid_desc[] = {
143 0x05, 0x01, /* Usage Page (Desktop), */
144 0x09, 0x06, /* Usage (Keyboard), */
145 0xA1, 0x01, /* Collection (Application), */
146 0x85, 0x11, /* Report ID (17), */
147 0x95, 0x08, /* Report Count (8), */
148 0x75, 0x01, /* Report Size (1), */
149 0x15, 0x00, /* Logical Minimum (0), */
150 0x25, 0x01, /* Logical Maximum (1), */
151 0x05, 0x07, /* Usage Page (Keyboard), */
152 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */
153 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */
154 0x81, 0x02, /* Input (Variable), */
155 0x95, 0x01, /* Report Count (1), */
156 0x75, 0x08, /* Report Size (8), */
157 0x81, 0x01, /* Input (Constant), */
158 0x95, 0x06, /* Report Count (6), */
159 0x75, 0x08, /* Report Size (8), */
160 0x15, 0x00, /* Logical Minimum (0), */
161 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
162 0x05, 0x07, /* Usage Page (Keyboard), */
163 0x19, 0x00, /* Usage Minimum (None), */
164 0x2A, 0xFF, 0x00, /* Usage Maximum (FFh), */
165 0x81, 0x00, /* Input, */
166 0xC0 /* End Collection */
167 };
168
tf103c_dock_kbd_read(struct tf103c_dock_data * dock)169 static int tf103c_dock_kbd_read(struct tf103c_dock_data *dock)
170 {
171 struct i2c_client *client = dock->kbd_client;
172 struct device *dev = &dock->ec_client->dev;
173 struct i2c_msg msgs[2];
174 u8 reg[2];
175 int ret;
176
177 reg[0] = TF103C_DOCK_KBD_DATA_REG & 0xff;
178 reg[1] = TF103C_DOCK_KBD_DATA_REG >> 8;
179
180 msgs[0].addr = client->addr;
181 msgs[0].flags = 0;
182 msgs[0].len = sizeof(reg);
183 msgs[0].buf = reg;
184
185 msgs[1].addr = client->addr;
186 msgs[1].flags = I2C_M_RD;
187 msgs[1].len = TF103C_DOCK_KBD_DATA_MAX_LENGTH;
188 msgs[1].buf = dock->kbd_buf;
189
190 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
191 if (ret != ARRAY_SIZE(msgs)) {
192 dev_err(dev, "error %d reading kbd data\n", ret);
193 return -EIO;
194 }
195
196 return 0;
197 }
198
tf103c_dock_kbd_write(struct tf103c_dock_data * dock,u16 cmd)199 static void tf103c_dock_kbd_write(struct tf103c_dock_data *dock, u16 cmd)
200 {
201 struct device *dev = &dock->ec_client->dev;
202 u8 buf[4];
203 int ret;
204
205 put_unaligned_le16(TF103C_DOCK_KBD_CMD_REG, &buf[0]);
206 put_unaligned_le16(cmd, &buf[2]);
207
208 ret = i2c_master_send(dock->kbd_client, buf, sizeof(buf));
209 if (ret != sizeof(buf))
210 dev_err(dev, "error %d writing kbd cmd\n", ret);
211 }
212
213 /* HID ll_driver functions for forwarding input-reports from the kbd_client */
tf103c_dock_hid_parse(struct hid_device * hid)214 static int tf103c_dock_hid_parse(struct hid_device *hid)
215 {
216 return hid_parse_report(hid, tf103c_dock_kbd_hid_desc,
217 sizeof(tf103c_dock_kbd_hid_desc));
218 }
219
tf103c_dock_hid_start(struct hid_device * hid)220 static int tf103c_dock_hid_start(struct hid_device *hid)
221 {
222 return 0;
223 }
224
tf103c_dock_hid_stop(struct hid_device * hid)225 static void tf103c_dock_hid_stop(struct hid_device *hid)
226 {
227 hid->claimed = 0;
228 }
229
tf103c_dock_hid_open(struct hid_device * hid)230 static int tf103c_dock_hid_open(struct hid_device *hid)
231 {
232 struct tf103c_dock_data *dock = hid->driver_data;
233
234 set_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags);
235 return 0;
236 }
237
tf103c_dock_hid_close(struct hid_device * hid)238 static void tf103c_dock_hid_close(struct hid_device *hid)
239 {
240 struct tf103c_dock_data *dock = hid->driver_data;
241
242 clear_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags);
243 }
244
245 /* Mandatory, but not used */
tf103c_dock_hid_raw_request(struct hid_device * hid,u8 reportnum,u8 * buf,size_t len,u8 rtype,int reqtype)246 static int tf103c_dock_hid_raw_request(struct hid_device *hid, u8 reportnum,
247 u8 *buf, size_t len, u8 rtype, int reqtype)
248 {
249 return 0;
250 }
251
252 static const struct hid_ll_driver tf103c_dock_hid_ll_driver = {
253 .parse = tf103c_dock_hid_parse,
254 .start = tf103c_dock_hid_start,
255 .stop = tf103c_dock_hid_stop,
256 .open = tf103c_dock_hid_open,
257 .close = tf103c_dock_hid_close,
258 .raw_request = tf103c_dock_hid_raw_request,
259 };
260
261 static const int tf103c_dock_toprow_codes[13][2] = {
262 /* Normal, AltGr pressed */
263 { KEY_POWER, KEY_F1 },
264 { KEY_RFKILL, KEY_F2 },
265 { KEY_F21, KEY_F3 }, /* Touchpad toggle, userspace expects F21 */
266 { KEY_BRIGHTNESSDOWN, KEY_F4 },
267 { KEY_BRIGHTNESSUP, KEY_F5 },
268 { KEY_CAMERA, KEY_F6 },
269 { KEY_CONFIG, KEY_F7 },
270 { KEY_PREVIOUSSONG, KEY_F8 },
271 { KEY_PLAYPAUSE, KEY_F9 },
272 { KEY_NEXTSONG, KEY_F10 },
273 { KEY_MUTE, KEY_F11 },
274 { KEY_VOLUMEDOWN, KEY_F12 },
275 { KEY_VOLUMEUP, KEY_SYSRQ },
276 };
277
tf103c_dock_report_toprow_kbd_hook(struct tf103c_dock_data * dock)278 static void tf103c_dock_report_toprow_kbd_hook(struct tf103c_dock_data *dock)
279 {
280 u8 *esc, *buf = dock->kbd_buf;
281 int size;
282
283 /*
284 * Stop AltGr reports from getting reported on the "Asus TF103C Dock
285 * Keyboard" input_dev, since this gets used as "Fn" key for the toprow
286 * keys. Instead we report this on the "Asus TF103C Dock Top Row Keys"
287 * input_dev, when not used to modify the toprow keys.
288 */
289 dock->altgr_pressed = buf[TF103C_DOCK_KBD_DATA_MODIFIERS] & 0x40;
290 buf[TF103C_DOCK_KBD_DATA_MODIFIERS] &= ~0x40;
291
292 input_report_key(dock->input, KEY_RIGHTALT, dock->altgr_pressed);
293 input_sync(dock->input);
294
295 /* Toggle fnlock on AltGr + Esc press */
296 buf = buf + TF103C_DOCK_KBD_DATA_KEYS;
297 size = TF103C_DOCK_KBD_DATA_MAX_LENGTH - TF103C_DOCK_KBD_DATA_KEYS;
298 esc = memchr(buf, 0x29, size);
299 if (!dock->esc_pressed && esc) {
300 if (dock->altgr_pressed) {
301 fnlock = !fnlock;
302 dock->filter_esc = true;
303 }
304 }
305 if (esc && dock->filter_esc)
306 *esc = 0;
307 else
308 dock->filter_esc = false;
309
310 dock->esc_pressed = esc != NULL;
311 }
312
tf103c_dock_toprow_press(struct tf103c_dock_data * dock,int key_code)313 static void tf103c_dock_toprow_press(struct tf103c_dock_data *dock, int key_code)
314 {
315 /*
316 * Release AltGr before reporting the toprow key, so that userspace
317 * sees e.g. just KEY_SUSPEND and not AltGr + KEY_SUSPEND.
318 */
319 if (dock->altgr_pressed) {
320 input_report_key(dock->input, KEY_RIGHTALT, false);
321 input_sync(dock->input);
322 }
323
324 input_report_key(dock->input, key_code, true);
325 input_sync(dock->input);
326 }
327
tf103c_dock_toprow_release(struct tf103c_dock_data * dock,int key_code)328 static void tf103c_dock_toprow_release(struct tf103c_dock_data *dock, int key_code)
329 {
330 input_report_key(dock->input, key_code, false);
331 input_sync(dock->input);
332
333 if (dock->altgr_pressed) {
334 input_report_key(dock->input, KEY_RIGHTALT, true);
335 input_sync(dock->input);
336 }
337 }
338
tf103c_dock_toprow_event(struct tf103c_dock_data * dock,int toprow_index,int * last_press)339 static void tf103c_dock_toprow_event(struct tf103c_dock_data *dock,
340 int toprow_index, int *last_press)
341 {
342 int key_code, fn = dock->altgr_pressed ^ fnlock;
343
344 if (last_press && *last_press) {
345 tf103c_dock_toprow_release(dock, *last_press);
346 *last_press = 0;
347 }
348
349 if (toprow_index < 0)
350 return;
351
352 key_code = tf103c_dock_toprow_codes[toprow_index][fn];
353 tf103c_dock_toprow_press(dock, key_code);
354
355 if (last_press)
356 *last_press = key_code;
357 else
358 tf103c_dock_toprow_release(dock, key_code);
359 }
360
361 /*
362 * The keyboard sends what appears to be standard I2C-HID input-reports,
363 * except that a 16 bit register address of where the I2C-HID format
364 * input-reports are stored must be send before reading it in a single
365 * (I2C repeated-start) I2C transaction.
366 *
367 * Its unknown how to get the HID descriptors but they are easy to reconstruct:
368 *
369 * Input report id 0x11 is 8 bytes long and contain standard USB HID intf-class,
370 * Boot Interface Subclass reports.
371 * Input report id 0x13 is 2 bytes long and sends Consumer Control events
372 * Input report id 0x14 is 1 byte long and sends System Control events
373 *
374 * However the top row keys (where a normal keyboard has F1-F12 + Print-Screen)
375 * are a mess, using a mix of the 0x13 and 0x14 input reports as well as EC SCI
376 * events; and these need special handling to allow actually sending F1-F12,
377 * since the Fn key on the keyboard only works on the cursor keys and the top
378 * row keys always send their special "Multimedia hotkey" codes.
379 *
380 * So only forward the 0x11 reports to HID and handle the top-row keys here.
381 */
tf103c_dock_kbd_interrupt(struct tf103c_dock_data * dock)382 static void tf103c_dock_kbd_interrupt(struct tf103c_dock_data *dock)
383 {
384 struct device *dev = &dock->ec_client->dev;
385 u8 *buf = dock->kbd_buf;
386 int size;
387
388 if (tf103c_dock_kbd_read(dock))
389 return;
390
391 size = buf[0] | buf[1] << 8;
392 if (size < TF103C_DOCK_KBD_DATA_MIN_LENGTH ||
393 size > TF103C_DOCK_KBD_DATA_MAX_LENGTH) {
394 dev_err(dev, "error reported kbd pkt size %d is out of range %d-%d\n", size,
395 TF103C_DOCK_KBD_DATA_MIN_LENGTH,
396 TF103C_DOCK_KBD_DATA_MAX_LENGTH);
397 return;
398 }
399
400 switch (buf[2]) {
401 case 0x11:
402 if (size != 11)
403 break;
404
405 tf103c_dock_report_toprow_kbd_hook(dock);
406
407 if (test_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags))
408 hid_input_report(dock->hid, HID_INPUT_REPORT, buf + 2, size - 2, 1);
409 return;
410 case 0x13:
411 if (size != 5)
412 break;
413
414 switch (buf[3] | buf[4] << 8) {
415 case 0:
416 tf103c_dock_toprow_event(dock, -1, &dock->last_press_0x13);
417 return;
418 case 0x70:
419 tf103c_dock_toprow_event(dock, 3, &dock->last_press_0x13);
420 return;
421 case 0x6f:
422 tf103c_dock_toprow_event(dock, 4, &dock->last_press_0x13);
423 return;
424 case 0xb6:
425 tf103c_dock_toprow_event(dock, 7, &dock->last_press_0x13);
426 return;
427 case 0xcd:
428 tf103c_dock_toprow_event(dock, 8, &dock->last_press_0x13);
429 return;
430 case 0xb5:
431 tf103c_dock_toprow_event(dock, 9, &dock->last_press_0x13);
432 return;
433 case 0xe2:
434 tf103c_dock_toprow_event(dock, 10, &dock->last_press_0x13);
435 return;
436 case 0xea:
437 tf103c_dock_toprow_event(dock, 11, &dock->last_press_0x13);
438 return;
439 case 0xe9:
440 tf103c_dock_toprow_event(dock, 12, &dock->last_press_0x13);
441 return;
442 }
443 break;
444 case 0x14:
445 if (size != 4)
446 break;
447
448 switch (buf[3]) {
449 case 0:
450 tf103c_dock_toprow_event(dock, -1, &dock->last_press_0x14);
451 return;
452 case 1:
453 tf103c_dock_toprow_event(dock, 0, &dock->last_press_0x14);
454 return;
455 }
456 break;
457 }
458
459 dev_warn(dev, "warning unknown kbd data: %*ph\n", size, buf);
460 }
461
462 /*** touchpad related code ***/
463
464 static const struct property_entry tf103c_dock_touchpad_props[] = {
465 PROPERTY_ENTRY_BOOL("elan,clickpad"),
466 { }
467 };
468
469 static const struct software_node tf103c_dock_touchpad_sw_node = {
470 .properties = tf103c_dock_touchpad_props,
471 };
472
473 /*
474 * tf103c_enable_touchpad() is only called from the threaded interrupt handler
475 * and tf103c_disable_touchpad() is only called after the irq is disabled,
476 * so no locking is necessary.
477 */
tf103c_dock_enable_touchpad(struct tf103c_dock_data * dock)478 static void tf103c_dock_enable_touchpad(struct tf103c_dock_data *dock)
479 {
480 struct i2c_board_info board_info = { };
481 struct device *dev = &dock->ec_client->dev;
482 int ret;
483
484 if (dock->tp_enabled) {
485 /* Happens after resume, the tp needs to be reinitialized */
486 ret = device_reprobe(&dock->tp_client->dev);
487 if (ret)
488 dev_err_probe(dev, ret, "reprobing tp-client\n");
489 return;
490 }
491
492 strscpy(board_info.type, "elan_i2c");
493 board_info.addr = TF103C_DOCK_TP_ADDR;
494 board_info.dev_name = TF103C_DOCK_DEV_NAME "-tp";
495 board_info.irq = dock->tp_irq;
496 board_info.swnode = &tf103c_dock_touchpad_sw_node;
497
498 dock->tp_client = i2c_new_client_device(dock->ec_client->adapter, &board_info);
499 if (IS_ERR(dock->tp_client)) {
500 dev_err(dev, "error %ld creating tp client\n", PTR_ERR(dock->tp_client));
501 return;
502 }
503
504 dock->tp_enabled = true;
505 }
506
tf103c_dock_disable_touchpad(struct tf103c_dock_data * dock)507 static void tf103c_dock_disable_touchpad(struct tf103c_dock_data *dock)
508 {
509 if (!dock->tp_enabled)
510 return;
511
512 i2c_unregister_device(dock->tp_client);
513
514 dock->tp_enabled = false;
515 }
516
517 /*** interrupt handling code ***/
tf103c_dock_ec_cmd(struct tf103c_dock_data * dock,const u8 * cmd)518 static void tf103c_dock_ec_cmd(struct tf103c_dock_data *dock, const u8 *cmd)
519 {
520 struct device *dev = &dock->ec_client->dev;
521 int ret;
522
523 ret = i2c_smbus_write_i2c_block_data(dock->ec_client, TF103C_DOCK_EC_CMD_REG,
524 TF103C_DOCK_EC_CMD_LEN, cmd);
525 if (ret)
526 dev_err(dev, "error %d sending %*ph cmd\n", ret,
527 TF103C_DOCK_EC_CMD_LEN, cmd);
528 }
529
tf103c_dock_sci(struct tf103c_dock_data * dock,u8 val)530 static void tf103c_dock_sci(struct tf103c_dock_data *dock, u8 val)
531 {
532 struct device *dev = &dock->ec_client->dev;
533
534 switch (val) {
535 case 2:
536 tf103c_dock_toprow_event(dock, 1, NULL);
537 return;
538 case 4:
539 tf103c_dock_toprow_event(dock, 2, NULL);
540 return;
541 case 8:
542 tf103c_dock_toprow_event(dock, 5, NULL);
543 return;
544 case 17:
545 tf103c_dock_toprow_event(dock, 6, NULL);
546 return;
547 }
548
549 dev_warn(dev, "warning unknown SCI value: 0x%02x\n", val);
550 }
551
tf103c_dock_smi(struct tf103c_dock_data * dock,u8 val)552 static void tf103c_dock_smi(struct tf103c_dock_data *dock, u8 val)
553 {
554 struct device *dev = &dock->ec_client->dev;
555
556 switch (val) {
557 case TF103C_DOCK_SMI_EC_WAKEUP:
558 tf103c_dock_ec_cmd(dock, tf103c_dock_enable_cmd);
559 tf103c_dock_ec_cmd(dock, tf103c_dock_usb_enable_cmd);
560 tf103c_dock_kbd_write(dock, TF103C_DOCK_KBD_CMD_ENABLE);
561 break;
562 case TF103C_DOCK_SMI_PAD_BL_CHANGE:
563 /* There is no backlight, but the EC still sends this */
564 break;
565 case TF103C_DOCK_SMI_HID_STATUS_CHANGED:
566 tf103c_dock_enable_touchpad(dock);
567 break;
568 default:
569 dev_warn(dev, "warning unknown SMI value: 0x%02x\n", val);
570 break;
571 }
572 }
573
tf103c_dock_irq(int irq,void * data)574 static irqreturn_t tf103c_dock_irq(int irq, void *data)
575 {
576 struct tf103c_dock_data *dock = data;
577 struct device *dev = &dock->ec_client->dev;
578 u8 intr_data[8];
579 int ret;
580
581 ret = i2c_smbus_read_i2c_block_data(dock->intr_client, TF103C_DOCK_INTR_DATA_REG,
582 sizeof(intr_data), intr_data);
583 if (ret != sizeof(intr_data)) {
584 dev_err(dev, "error %d reading intr data\n", ret);
585 return IRQ_NONE;
586 }
587
588 if (!(intr_data[1] & TF103C_DOCK_INTR_DATA1_OBF_MASK))
589 return IRQ_NONE;
590
591 /* intr_data[0] is the length of the rest of the packet */
592 if (intr_data[0] == 3 && intr_data[1] == TF103C_DOCK_INTR_DATA1_OOB_VALUE &&
593 intr_data[2] == TF103C_DOCK_INTR_DATA2_OOB_VALUE) {
594 /* intr_data[3] seems to contain a HID input report id */
595 switch (intr_data[3]) {
596 case 0x01:
597 handle_nested_irq(dock->tp_irq);
598 break;
599 case 0x11:
600 case 0x13:
601 case 0x14:
602 tf103c_dock_kbd_interrupt(dock);
603 break;
604 default:
605 dev_warn(dev, "warning unknown intr_data[3]: 0x%02x\n", intr_data[3]);
606 break;
607 }
608 return IRQ_HANDLED;
609 }
610
611 if (intr_data[1] & TF103C_DOCK_INTR_DATA1_SCI_MASK) {
612 tf103c_dock_sci(dock, intr_data[2]);
613 return IRQ_HANDLED;
614 }
615
616 if (intr_data[1] & TF103C_DOCK_INTR_DATA1_SMI_MASK) {
617 tf103c_dock_smi(dock, intr_data[2]);
618 return IRQ_HANDLED;
619 }
620
621 dev_warn(dev, "warning unknown intr data: %*ph\n", 8, intr_data);
622 return IRQ_NONE;
623 }
624
625 /*
626 * tf103c_dock_[dis|en]able only run from hpd_work or at times when
627 * hpd_work cannot run (hpd_irq disabled), so no locking is necessary.
628 */
tf103c_dock_enable(struct tf103c_dock_data * dock)629 static void tf103c_dock_enable(struct tf103c_dock_data *dock)
630 {
631 if (dock->enabled)
632 return;
633
634 if (dock->board_rev != 2)
635 gpiod_set_value(dock->pwr_en, 1);
636
637 msleep(500);
638 enable_irq(dock->irq);
639
640 dock->enabled = true;
641 }
642
tf103c_dock_disable(struct tf103c_dock_data * dock)643 static void tf103c_dock_disable(struct tf103c_dock_data *dock)
644 {
645 if (!dock->enabled)
646 return;
647
648 disable_irq(dock->irq);
649 tf103c_dock_disable_touchpad(dock);
650 if (dock->board_rev != 2)
651 gpiod_set_value(dock->pwr_en, 0);
652
653 dock->enabled = false;
654 }
655
tf103c_dock_hpd_work(struct work_struct * work)656 static void tf103c_dock_hpd_work(struct work_struct *work)
657 {
658 struct tf103c_dock_data *dock =
659 container_of(work, struct tf103c_dock_data, hpd_work.work);
660
661 if (gpiod_get_value(dock->hpd_gpio))
662 tf103c_dock_enable(dock);
663 else
664 tf103c_dock_disable(dock);
665 }
666
tf103c_dock_hpd_irq(int irq,void * data)667 static irqreturn_t tf103c_dock_hpd_irq(int irq, void *data)
668 {
669 struct tf103c_dock_data *dock = data;
670
671 mod_delayed_work(system_long_wq, &dock->hpd_work, TF103C_DOCK_HPD_DEBOUNCE);
672 return IRQ_HANDLED;
673 }
674
tf103c_dock_start_hpd(struct tf103c_dock_data * dock)675 static void tf103c_dock_start_hpd(struct tf103c_dock_data *dock)
676 {
677 enable_irq(dock->hpd_irq);
678 /* Sync current HPD status */
679 queue_delayed_work(system_long_wq, &dock->hpd_work, TF103C_DOCK_HPD_DEBOUNCE);
680 }
681
tf103c_dock_stop_hpd(struct tf103c_dock_data * dock)682 static void tf103c_dock_stop_hpd(struct tf103c_dock_data *dock)
683 {
684 disable_irq(dock->hpd_irq);
685 cancel_delayed_work_sync(&dock->hpd_work);
686 }
687
688 /*** probe ***/
689
690 static const struct dmi_system_id tf103c_dock_dmi_ids[] = {
691 {
692 .matches = {
693 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
694 DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
695 },
696 },
697 { }
698 };
699
tf103c_dock_non_devm_cleanup(void * data)700 static void tf103c_dock_non_devm_cleanup(void *data)
701 {
702 struct tf103c_dock_data *dock = data;
703
704 if (dock->tp_irq_domain)
705 irq_domain_remove(dock->tp_irq_domain);
706
707 if (!IS_ERR_OR_NULL(dock->hid))
708 hid_destroy_device(dock->hid);
709
710 i2c_unregister_device(dock->kbd_client);
711 i2c_unregister_device(dock->intr_client);
712 gpiod_remove_lookup_table(&tf103c_dock_gpios);
713 }
714
tf103c_dock_probe(struct i2c_client * client)715 static int tf103c_dock_probe(struct i2c_client *client)
716 {
717 struct i2c_board_info board_info = { };
718 struct device *dev = &client->dev;
719 struct gpio_desc *board_rev_gpio;
720 struct tf103c_dock_data *dock;
721 enum gpiod_flags flags;
722 int i, ret;
723
724 /* GPIOs are hardcoded for the Asus TF103C, don't bind on other devs */
725 if (!dmi_check_system(tf103c_dock_dmi_ids))
726 return -ENODEV;
727
728 dock = devm_kzalloc(dev, sizeof(*dock), GFP_KERNEL);
729 if (!dock)
730 return -ENOMEM;
731
732 INIT_DELAYED_WORK(&dock->hpd_work, tf103c_dock_hpd_work);
733
734 /* 1. Get GPIOs and their IRQs */
735 gpiod_add_lookup_table(&tf103c_dock_gpios);
736
737 ret = devm_add_action_or_reset(dev, tf103c_dock_non_devm_cleanup, dock);
738 if (ret)
739 return ret;
740
741 /*
742 * The pin is configured as input by default, use ASIS because otherwise
743 * the gpio-crystalcove.c switches off the internal pull-down replacing
744 * it with a pull-up.
745 */
746 board_rev_gpio = gpiod_get(dev, "board_rev", GPIOD_ASIS);
747 if (IS_ERR(board_rev_gpio))
748 return dev_err_probe(dev, PTR_ERR(board_rev_gpio), "requesting board_rev GPIO\n");
749 dock->board_rev = gpiod_get_value_cansleep(board_rev_gpio) + 1;
750 gpiod_put(board_rev_gpio);
751
752 /*
753 * The Android driver drives the dock-pwr-en pin high at probe for
754 * revision 2 boards and then never touches it again?
755 * This code has only been tested on a revision 1 board, so for now
756 * just mimick what Android does on revision 2 boards.
757 */
758 flags = (dock->board_rev == 2) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
759 dock->pwr_en = devm_gpiod_get(dev, "dock_pwr_en", flags);
760 if (IS_ERR(dock->pwr_en))
761 return dev_err_probe(dev, PTR_ERR(dock->pwr_en), "requesting pwr_en GPIO\n");
762
763 dock->irq_gpio = devm_gpiod_get(dev, "dock_irq", GPIOD_IN);
764 if (IS_ERR(dock->irq_gpio))
765 return dev_err_probe(dev, PTR_ERR(dock->irq_gpio), "requesting IRQ GPIO\n");
766
767 dock->irq = gpiod_to_irq(dock->irq_gpio);
768 if (dock->irq < 0)
769 return dev_err_probe(dev, dock->irq, "getting dock IRQ");
770
771 ret = devm_request_threaded_irq(dev, dock->irq, NULL, tf103c_dock_irq,
772 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_NO_AUTOEN,
773 "dock_irq", dock);
774 if (ret)
775 return dev_err_probe(dev, ret, "requesting dock IRQ");
776
777 dock->hpd_gpio = devm_gpiod_get(dev, "dock_hpd", GPIOD_IN);
778 if (IS_ERR(dock->hpd_gpio))
779 return dev_err_probe(dev, PTR_ERR(dock->hpd_gpio), "requesting HPD GPIO\n");
780
781 dock->hpd_irq = gpiod_to_irq(dock->hpd_gpio);
782 if (dock->hpd_irq < 0)
783 return dev_err_probe(dev, dock->hpd_irq, "getting HPD IRQ");
784
785 ret = devm_request_irq(dev, dock->hpd_irq, tf103c_dock_hpd_irq,
786 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN,
787 "dock_hpd", dock);
788 if (ret)
789 return ret;
790
791 /*
792 * 2. Create I2C clients. The dock uses 4 different i2c addresses,
793 * the ACPI NPCE69A node being probed points to the EC address.
794 */
795 dock->ec_client = client;
796
797 strscpy(board_info.type, "tf103c-dock-intr");
798 board_info.addr = TF103C_DOCK_INTR_ADDR;
799 board_info.dev_name = TF103C_DOCK_DEV_NAME "-intr";
800
801 dock->intr_client = i2c_new_client_device(client->adapter, &board_info);
802 if (IS_ERR(dock->intr_client))
803 return dev_err_probe(dev, PTR_ERR(dock->intr_client), "creating intr client\n");
804
805 strscpy(board_info.type, "tf103c-dock-kbd");
806 board_info.addr = TF103C_DOCK_KBD_ADDR;
807 board_info.dev_name = TF103C_DOCK_DEV_NAME "-kbd";
808
809 dock->kbd_client = i2c_new_client_device(client->adapter, &board_info);
810 if (IS_ERR(dock->kbd_client))
811 return dev_err_probe(dev, PTR_ERR(dock->kbd_client), "creating kbd client\n");
812
813 /* 3. Create input_dev for the top row of the keyboard */
814 dock->input = devm_input_allocate_device(dev);
815 if (!dock->input)
816 return -ENOMEM;
817
818 dock->input->name = "Asus TF103C Dock Top Row Keys";
819 dock->input->phys = dev_name(dev);
820 dock->input->dev.parent = dev;
821 dock->input->id.bustype = BUS_I2C;
822 dock->input->id.vendor = /* USB_VENDOR_ID_ASUSTEK */
823 dock->input->id.product = /* From TF-103-C */
824 dock->input->id.version = 0x0100; /* 1.0 */
825
826 for (i = 0; i < ARRAY_SIZE(tf103c_dock_toprow_codes); i++) {
827 input_set_capability(dock->input, EV_KEY, tf103c_dock_toprow_codes[i][0]);
828 input_set_capability(dock->input, EV_KEY, tf103c_dock_toprow_codes[i][1]);
829 }
830 input_set_capability(dock->input, EV_KEY, KEY_RIGHTALT);
831
832 ret = input_register_device(dock->input);
833 if (ret)
834 return ret;
835
836 /* 4. Create HID device for the keyboard */
837 dock->hid = hid_allocate_device();
838 if (IS_ERR(dock->hid))
839 return dev_err_probe(dev, PTR_ERR(dock->hid), "allocating hid dev\n");
840
841 dock->hid->driver_data = dock;
842 dock->hid->ll_driver = &tf103c_dock_hid_ll_driver;
843 dock->hid->dev.parent = &client->dev;
844 dock->hid->bus = BUS_I2C;
845 dock->hid->vendor = 0x0b05; /* USB_VENDOR_ID_ASUSTEK */
846 dock->hid->product = 0x0103; /* From TF-103-C */
847 dock->hid->version = 0x0100; /* 1.0 */
848 strscpy(dock->hid->name, "Asus TF103C Dock Keyboard");
849 strscpy(dock->hid->phys, dev_name(dev));
850
851 ret = hid_add_device(dock->hid);
852 if (ret)
853 return dev_err_probe(dev, ret, "adding hid dev\n");
854
855 /* 5. Setup irqchip for touchpad IRQ pass-through */
856 dock->tp_irqchip.name = KBUILD_MODNAME;
857
858 dock->tp_irq_domain = irq_domain_create_linear(NULL, 1, &irq_domain_simple_ops, NULL);
859 if (!dock->tp_irq_domain)
860 return -ENOMEM;
861
862 dock->tp_irq = irq_create_mapping(dock->tp_irq_domain, 0);
863 if (!dock->tp_irq)
864 return -ENOMEM;
865
866 irq_set_chip_data(dock->tp_irq, dock);
867 irq_set_chip_and_handler(dock->tp_irq, &dock->tp_irqchip, handle_simple_irq);
868 irq_set_nested_thread(dock->tp_irq, true);
869 irq_set_noprobe(dock->tp_irq);
870
871 dev_info(dev, "Asus TF103C board-revision: %d\n", dock->board_rev);
872
873 tf103c_dock_start_hpd(dock);
874
875 device_init_wakeup(dev, true);
876 i2c_set_clientdata(client, dock);
877 return 0;
878 }
879
tf103c_dock_remove(struct i2c_client * client)880 static void tf103c_dock_remove(struct i2c_client *client)
881 {
882 struct tf103c_dock_data *dock = i2c_get_clientdata(client);
883
884 tf103c_dock_stop_hpd(dock);
885 tf103c_dock_disable(dock);
886 }
887
tf103c_dock_suspend(struct device * dev)888 static int __maybe_unused tf103c_dock_suspend(struct device *dev)
889 {
890 struct tf103c_dock_data *dock = dev_get_drvdata(dev);
891
892 tf103c_dock_stop_hpd(dock);
893
894 if (dock->enabled) {
895 tf103c_dock_ec_cmd(dock, tf103c_dock_suspend_cmd);
896
897 if (device_may_wakeup(dev))
898 enable_irq_wake(dock->irq);
899 }
900
901 return 0;
902 }
903
tf103c_dock_resume(struct device * dev)904 static int __maybe_unused tf103c_dock_resume(struct device *dev)
905 {
906 struct tf103c_dock_data *dock = dev_get_drvdata(dev);
907
908 if (dock->enabled) {
909 if (device_may_wakeup(dev))
910 disable_irq_wake(dock->irq);
911
912 /* Don't try to resume if the dock was unplugged during suspend */
913 if (gpiod_get_value(dock->hpd_gpio))
914 tf103c_dock_ec_cmd(dock, tf103c_dock_enable_cmd);
915 }
916
917 tf103c_dock_start_hpd(dock);
918 return 0;
919 }
920
921 static SIMPLE_DEV_PM_OPS(tf103c_dock_pm_ops, tf103c_dock_suspend, tf103c_dock_resume);
922
923 static const struct acpi_device_id tf103c_dock_acpi_match[] = {
924 {"NPCE69A"},
925 { }
926 };
927 MODULE_DEVICE_TABLE(acpi, tf103c_dock_acpi_match);
928
929 static struct i2c_driver tf103c_dock_driver = {
930 .driver = {
931 .name = "asus-tf103c-dock",
932 .pm = &tf103c_dock_pm_ops,
933 .acpi_match_table = tf103c_dock_acpi_match,
934 },
935 .probe = tf103c_dock_probe,
936 .remove = tf103c_dock_remove,
937 };
938 module_i2c_driver(tf103c_dock_driver);
939
940 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com");
941 MODULE_DESCRIPTION("X86 Android tablets DSDT fixups driver");
942 MODULE_LICENSE("GPL");
943