Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * This is a driver for the keyboard, touchpad and USB port of the
0004  * keyboard dock for the Asus TF103C 2-in-1 tablet.
0005  *
0006  * This keyboard dock has its own I2C attached embedded controller
0007  * and the keyboard and touchpad are also connected over I2C,
0008  * instead of using the usual USB connection. This means that the
0009  * keyboard dock requires this special driver to function.
0010  *
0011  * Copyright (C) 2021 Hans de Goede <hdegoede@redhat.com>
0012  */
0013 
0014 #include <linux/acpi.h>
0015 #include <linux/delay.h>
0016 #include <linux/dmi.h>
0017 #include <linux/gpio/consumer.h>
0018 #include <linux/gpio/machine.h>
0019 #include <linux/hid.h>
0020 #include <linux/i2c.h>
0021 #include <linux/input.h>
0022 #include <linux/irq.h>
0023 #include <linux/irqdomain.h>
0024 #include <linux/mod_devicetable.h>
0025 #include <linux/moduleparam.h>
0026 #include <linux/module.h>
0027 #include <linux/pm.h>
0028 #include <linux/workqueue.h>
0029 #include <asm/unaligned.h>
0030 
0031 static bool fnlock;
0032 module_param(fnlock, bool, 0644);
0033 MODULE_PARM_DESC(fnlock,
0034          "By default the kbd toprow sends multimedia key presses. AltGr "
0035          "can be pressed to change this to F1-F12. Set this to 1 to "
0036          "change the default. Press AltGr + Esc to toggle at runtime.");
0037 
0038 #define TF103C_DOCK_DEV_NAME                "NPCE69A:00"
0039 
0040 #define TF103C_DOCK_HPD_DEBOUNCE            msecs_to_jiffies(20)
0041 
0042 /*** Touchpad I2C device defines ***/
0043 #define TF103C_DOCK_TP_ADDR             0x15
0044 
0045 /*** Keyboard I2C device defines **A*/
0046 #define TF103C_DOCK_KBD_ADDR                0x16
0047 
0048 #define TF103C_DOCK_KBD_DATA_REG            0x73
0049 #define TF103C_DOCK_KBD_DATA_MIN_LENGTH         4
0050 #define TF103C_DOCK_KBD_DATA_MAX_LENGTH         11
0051 #define TF103C_DOCK_KBD_DATA_MODIFIERS          3
0052 #define TF103C_DOCK_KBD_DATA_KEYS           5
0053 #define TF103C_DOCK_KBD_CMD_REG             0x75
0054 
0055 #define TF103C_DOCK_KBD_CMD_ENABLE          0x0800
0056 
0057 /*** EC innterrupt data I2C device defines ***/
0058 #define TF103C_DOCK_INTR_ADDR               0x19
0059 #define TF103C_DOCK_INTR_DATA_REG           0x6a
0060 
0061 #define TF103C_DOCK_INTR_DATA1_OBF_MASK         0x01
0062 #define TF103C_DOCK_INTR_DATA1_KEY_MASK         0x04
0063 #define TF103C_DOCK_INTR_DATA1_KBC_MASK         0x08
0064 #define TF103C_DOCK_INTR_DATA1_AUX_MASK         0x20
0065 #define TF103C_DOCK_INTR_DATA1_SCI_MASK         0x40
0066 #define TF103C_DOCK_INTR_DATA1_SMI_MASK         0x80
0067 /* Special values for the OOB data on kbd_client / tp_client */
0068 #define TF103C_DOCK_INTR_DATA1_OOB_VALUE        0xc1
0069 #define TF103C_DOCK_INTR_DATA2_OOB_VALUE        0x04
0070 
0071 #define TF103C_DOCK_SMI_AC_EVENT            0x31
0072 #define TF103C_DOCK_SMI_HANDSHAKING         0x50
0073 #define TF103C_DOCK_SMI_EC_WAKEUP           0x53
0074 #define TF103C_DOCK_SMI_BOOTBLOCK_RESET         0x5e
0075 #define TF103C_DOCK_SMI_WATCHDOG_RESET          0x5f
0076 #define TF103C_DOCK_SMI_ADAPTER_CHANGE          0x60
0077 #define TF103C_DOCK_SMI_DOCK_INSERT         0x61
0078 #define TF103C_DOCK_SMI_DOCK_REMOVE         0x62
0079 #define TF103C_DOCK_SMI_PAD_BL_CHANGE           0x63
0080 #define TF103C_DOCK_SMI_HID_STATUS_CHANGED      0x64
0081 #define TF103C_DOCK_SMI_HID_WAKEUP          0x65
0082 #define TF103C_DOCK_SMI_S3              0x83
0083 #define TF103C_DOCK_SMI_S5              0x85
0084 #define TF103C_DOCK_SMI_NOTIFY_SHUTDOWN         0x90
0085 #define TF103C_DOCK_SMI_RESUME              0x91
0086 
0087 /*** EC (dockram) I2C device defines ***/
0088 #define TF103C_DOCK_EC_ADDR             0x1b
0089 
0090 #define TF103C_DOCK_EC_CMD_REG              0x0a
0091 #define TF103C_DOCK_EC_CMD_LEN              9
0092 
0093 enum {
0094     TF103C_DOCK_FLAG_HID_OPEN,
0095 };
0096 
0097 struct tf103c_dock_data {
0098     struct delayed_work hpd_work;
0099     struct irq_chip tp_irqchip;
0100     struct irq_domain *tp_irq_domain;
0101     struct i2c_client *ec_client;
0102     struct i2c_client *intr_client;
0103     struct i2c_client *kbd_client;
0104     struct i2c_client *tp_client;
0105     struct gpio_desc *pwr_en;
0106     struct gpio_desc *irq_gpio;
0107     struct gpio_desc *hpd_gpio;
0108     struct input_dev *input;
0109     struct hid_device *hid;
0110     unsigned long flags;
0111     int board_rev;
0112     int irq;
0113     int hpd_irq;
0114     int tp_irq;
0115     int last_press_0x13;
0116     int last_press_0x14;
0117     bool enabled;
0118     bool tp_enabled;
0119     bool altgr_pressed;
0120     bool esc_pressed;
0121     bool filter_esc;
0122     u8 kbd_buf[TF103C_DOCK_KBD_DATA_MAX_LENGTH];
0123 };
0124 
0125 static struct gpiod_lookup_table tf103c_dock_gpios = {
0126     .dev_id = "i2c-" TF103C_DOCK_DEV_NAME,
0127     .table = {
0128         GPIO_LOOKUP("INT33FC:00",      55, "dock_pwr_en", GPIO_ACTIVE_HIGH),
0129         GPIO_LOOKUP("INT33FC:02",       1, "dock_irq", GPIO_ACTIVE_HIGH),
0130         GPIO_LOOKUP("INT33FC:02",      29, "dock_hpd", GPIO_ACTIVE_HIGH),
0131         GPIO_LOOKUP("gpio_crystalcove", 2, "board_rev", GPIO_ACTIVE_HIGH),
0132         {}
0133     },
0134 };
0135 
0136 /* Byte 0 is the length of the rest of the packet */
0137 static const u8 tf103c_dock_enable_cmd[9] = { 8, 0x20, 0, 0, 0, 0, 0x20, 0, 0 };
0138 static const u8 tf103c_dock_usb_enable_cmd[9] = { 8, 0, 0, 0, 0, 0, 0, 0x40, 0 };
0139 static const u8 tf103c_dock_suspend_cmd[9] = { 8, 0, 0x20, 0, 0, 0x22, 0, 0, 0 };
0140 
0141 /*** keyboard related code ***/
0142 
0143 static u8 tf103c_dock_kbd_hid_desc[] = {
0144     0x05, 0x01,         /*  Usage Page (Desktop),               */
0145     0x09, 0x06,         /*  Usage (Keyboard),                   */
0146     0xA1, 0x01,         /*  Collection (Application),           */
0147     0x85, 0x11,         /*      Report ID (17),                 */
0148     0x95, 0x08,         /*      Report Count (8),               */
0149     0x75, 0x01,         /*      Report Size (1),                */
0150     0x15, 0x00,         /*      Logical Minimum (0),            */
0151     0x25, 0x01,         /*      Logical Maximum (1),            */
0152     0x05, 0x07,         /*      Usage Page (Keyboard),          */
0153     0x19, 0xE0,         /*      Usage Minimum (KB Leftcontrol), */
0154     0x29, 0xE7,         /*      Usage Maximum (KB Right GUI),   */
0155     0x81, 0x02,         /*      Input (Variable),               */
0156     0x95, 0x01,         /*      Report Count (1),               */
0157     0x75, 0x08,         /*      Report Size (8),                */
0158     0x81, 0x01,         /*      Input (Constant),               */
0159     0x95, 0x06,         /*      Report Count (6),               */
0160     0x75, 0x08,         /*      Report Size (8),                */
0161     0x15, 0x00,         /*      Logical Minimum (0),            */
0162     0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
0163     0x05, 0x07,         /*      Usage Page (Keyboard),          */
0164     0x19, 0x00,         /*      Usage Minimum (None),           */
0165     0x2A, 0xFF, 0x00,   /*      Usage Maximum (FFh),            */
0166     0x81, 0x00,         /*      Input,                          */
0167     0xC0                /*  End Collection                      */
0168 };
0169 
0170 static int tf103c_dock_kbd_read(struct tf103c_dock_data *dock)
0171 {
0172     struct i2c_client *client = dock->kbd_client;
0173     struct device *dev = &dock->ec_client->dev;
0174     struct i2c_msg msgs[2];
0175     u8 reg[2];
0176     int ret;
0177 
0178     reg[0] = TF103C_DOCK_KBD_DATA_REG & 0xff;
0179     reg[1] = TF103C_DOCK_KBD_DATA_REG >> 8;
0180 
0181     msgs[0].addr = client->addr;
0182     msgs[0].flags = 0;
0183     msgs[0].len = sizeof(reg);
0184     msgs[0].buf = reg;
0185 
0186     msgs[1].addr = client->addr;
0187     msgs[1].flags = I2C_M_RD;
0188     msgs[1].len = TF103C_DOCK_KBD_DATA_MAX_LENGTH;
0189     msgs[1].buf = dock->kbd_buf;
0190 
0191     ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
0192     if (ret != ARRAY_SIZE(msgs)) {
0193         dev_err(dev, "error %d reading kbd data\n", ret);
0194         return -EIO;
0195     }
0196 
0197     return 0;
0198 }
0199 
0200 static void tf103c_dock_kbd_write(struct tf103c_dock_data *dock, u16 cmd)
0201 {
0202     struct device *dev = &dock->ec_client->dev;
0203     u8 buf[4];
0204     int ret;
0205 
0206     put_unaligned_le16(TF103C_DOCK_KBD_CMD_REG, &buf[0]);
0207     put_unaligned_le16(cmd, &buf[2]);
0208 
0209     ret = i2c_master_send(dock->kbd_client, buf, sizeof(buf));
0210     if (ret != sizeof(buf))
0211         dev_err(dev, "error %d writing kbd cmd\n", ret);
0212 }
0213 
0214 /* HID ll_driver functions for forwarding input-reports from the kbd_client */
0215 static int tf103c_dock_hid_parse(struct hid_device *hid)
0216 {
0217     return hid_parse_report(hid, tf103c_dock_kbd_hid_desc,
0218                 sizeof(tf103c_dock_kbd_hid_desc));
0219 }
0220 
0221 static int tf103c_dock_hid_start(struct hid_device *hid)
0222 {
0223     return 0;
0224 }
0225 
0226 static void tf103c_dock_hid_stop(struct hid_device *hid)
0227 {
0228     hid->claimed = 0;
0229 }
0230 
0231 static int tf103c_dock_hid_open(struct hid_device *hid)
0232 {
0233     struct tf103c_dock_data *dock = hid->driver_data;
0234 
0235     set_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags);
0236     return 0;
0237 }
0238 
0239 static void tf103c_dock_hid_close(struct hid_device *hid)
0240 {
0241     struct tf103c_dock_data *dock = hid->driver_data;
0242 
0243     clear_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags);
0244 }
0245 
0246 /* Mandatory, but not used */
0247 static int tf103c_dock_hid_raw_request(struct hid_device *hid, u8 reportnum,
0248                        u8 *buf, size_t len, u8 rtype, int reqtype)
0249 {
0250     return 0;
0251 }
0252 
0253 static struct hid_ll_driver tf103c_dock_hid_ll_driver = {
0254     .parse = tf103c_dock_hid_parse,
0255     .start = tf103c_dock_hid_start,
0256     .stop = tf103c_dock_hid_stop,
0257     .open = tf103c_dock_hid_open,
0258     .close = tf103c_dock_hid_close,
0259     .raw_request = tf103c_dock_hid_raw_request,
0260 };
0261 
0262 static int tf103c_dock_toprow_codes[13][2] = {
0263     /* Normal,            AltGr pressed */
0264     { KEY_POWER,          KEY_F1 },
0265     { KEY_RFKILL,         KEY_F2 },
0266     { KEY_F21,            KEY_F3 }, /* Touchpad toggle, userspace expects F21 */
0267     { KEY_BRIGHTNESSDOWN, KEY_F4 },
0268     { KEY_BRIGHTNESSUP,   KEY_F5 },
0269     { KEY_CAMERA,         KEY_F6 },
0270     { KEY_CONFIG,         KEY_F7 },
0271     { KEY_PREVIOUSSONG,   KEY_F8 },
0272     { KEY_PLAYPAUSE,      KEY_F9 },
0273     { KEY_NEXTSONG,       KEY_F10 },
0274     { KEY_MUTE,           KEY_F11 },
0275     { KEY_VOLUMEDOWN,     KEY_F12 },
0276     { KEY_VOLUMEUP,       KEY_SYSRQ },
0277 };
0278 
0279 static void tf103c_dock_report_toprow_kbd_hook(struct tf103c_dock_data *dock)
0280 {
0281     u8 *esc, *buf = dock->kbd_buf;
0282     int size;
0283 
0284     /*
0285      * Stop AltGr reports from getting reported on the "Asus TF103C Dock
0286      * Keyboard" input_dev, since this gets used as "Fn" key for the toprow
0287      * keys. Instead we report this on the "Asus TF103C Dock Top Row Keys"
0288      * input_dev, when not used to modify the toprow keys.
0289      */
0290     dock->altgr_pressed = buf[TF103C_DOCK_KBD_DATA_MODIFIERS] & 0x40;
0291     buf[TF103C_DOCK_KBD_DATA_MODIFIERS] &= ~0x40;
0292 
0293     input_report_key(dock->input, KEY_RIGHTALT, dock->altgr_pressed);
0294     input_sync(dock->input);
0295 
0296     /* Toggle fnlock on AltGr + Esc press */
0297     buf = buf + TF103C_DOCK_KBD_DATA_KEYS;
0298     size = TF103C_DOCK_KBD_DATA_MAX_LENGTH - TF103C_DOCK_KBD_DATA_KEYS;
0299     esc = memchr(buf, 0x29, size);
0300     if (!dock->esc_pressed && esc) {
0301         if (dock->altgr_pressed) {
0302             fnlock = !fnlock;
0303             dock->filter_esc = true;
0304         }
0305     }
0306     if (esc && dock->filter_esc)
0307         *esc = 0;
0308     else
0309         dock->filter_esc = false;
0310 
0311     dock->esc_pressed = esc != NULL;
0312 }
0313 
0314 static void tf103c_dock_toprow_press(struct tf103c_dock_data *dock, int key_code)
0315 {
0316     /*
0317      * Release AltGr before reporting the toprow key, so that userspace
0318      * sees e.g. just KEY_SUSPEND and not AltGr + KEY_SUSPEND.
0319      */
0320     if (dock->altgr_pressed) {
0321         input_report_key(dock->input, KEY_RIGHTALT, false);
0322         input_sync(dock->input);
0323     }
0324 
0325     input_report_key(dock->input, key_code, true);
0326     input_sync(dock->input);
0327 }
0328 
0329 static void tf103c_dock_toprow_release(struct tf103c_dock_data *dock, int key_code)
0330 {
0331     input_report_key(dock->input, key_code, false);
0332     input_sync(dock->input);
0333 
0334     if (dock->altgr_pressed) {
0335         input_report_key(dock->input, KEY_RIGHTALT, true);
0336         input_sync(dock->input);
0337     }
0338 }
0339 
0340 static void tf103c_dock_toprow_event(struct tf103c_dock_data *dock,
0341                         int toprow_index, int *last_press)
0342 {
0343     int key_code, fn = dock->altgr_pressed ^ fnlock;
0344 
0345     if (last_press && *last_press) {
0346         tf103c_dock_toprow_release(dock, *last_press);
0347         *last_press = 0;
0348     }
0349 
0350     if (toprow_index < 0)
0351         return;
0352 
0353     key_code = tf103c_dock_toprow_codes[toprow_index][fn];
0354     tf103c_dock_toprow_press(dock, key_code);
0355 
0356     if (last_press)
0357         *last_press = key_code;
0358     else
0359         tf103c_dock_toprow_release(dock, key_code);
0360 }
0361 
0362 /*
0363  * The keyboard sends what appears to be standard I2C-HID input-reports,
0364  * except that a 16 bit register address of where the I2C-HID format
0365  * input-reports are stored must be send before reading it in a single
0366  * (I2C repeated-start) I2C transaction.
0367  *
0368  * Its unknown how to get the HID descriptors but they are easy to reconstruct:
0369  *
0370  * Input report id 0x11 is 8 bytes long and contain standard USB HID intf-class,
0371  * Boot Interface Subclass reports.
0372  * Input report id 0x13 is 2 bytes long and sends Consumer Control events
0373  * Input report id 0x14 is 1 byte long and sends System Control events
0374  *
0375  * However the top row keys (where a normal keyboard has F1-F12 + Print-Screen)
0376  * are a mess, using a mix of the 0x13 and 0x14 input reports as well as EC SCI
0377  * events; and these need special handling to allow actually sending F1-F12,
0378  * since the Fn key on the keyboard only works on the cursor keys and the top
0379  * row keys always send their special "Multimedia hotkey" codes.
0380  *
0381  * So only forward the 0x11 reports to HID and handle the top-row keys here.
0382  */
0383 static void tf103c_dock_kbd_interrupt(struct tf103c_dock_data *dock)
0384 {
0385     struct device *dev = &dock->ec_client->dev;
0386     u8 *buf = dock->kbd_buf;
0387     int size;
0388 
0389     if (tf103c_dock_kbd_read(dock))
0390         return;
0391 
0392     size = buf[0] | buf[1] << 8;
0393     if (size < TF103C_DOCK_KBD_DATA_MIN_LENGTH ||
0394         size > TF103C_DOCK_KBD_DATA_MAX_LENGTH) {
0395         dev_err(dev, "error reported kbd pkt size %d is out of range %d-%d\n", size,
0396             TF103C_DOCK_KBD_DATA_MIN_LENGTH,
0397             TF103C_DOCK_KBD_DATA_MAX_LENGTH);
0398         return;
0399     }
0400 
0401     switch (buf[2]) {
0402     case 0x11:
0403         if (size != 11)
0404             break;
0405 
0406         tf103c_dock_report_toprow_kbd_hook(dock);
0407 
0408         if (test_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags))
0409             hid_input_report(dock->hid, HID_INPUT_REPORT, buf + 2, size - 2, 1);
0410         return;
0411     case 0x13:
0412         if (size != 5)
0413             break;
0414 
0415         switch (buf[3] | buf[4] << 8) {
0416         case 0:
0417             tf103c_dock_toprow_event(dock, -1, &dock->last_press_0x13);
0418             return;
0419         case 0x70:
0420             tf103c_dock_toprow_event(dock, 3, &dock->last_press_0x13);
0421             return;
0422         case 0x6f:
0423             tf103c_dock_toprow_event(dock, 4, &dock->last_press_0x13);
0424             return;
0425         case 0xb6:
0426             tf103c_dock_toprow_event(dock, 7, &dock->last_press_0x13);
0427             return;
0428         case 0xcd:
0429             tf103c_dock_toprow_event(dock, 8, &dock->last_press_0x13);
0430             return;
0431         case 0xb5:
0432             tf103c_dock_toprow_event(dock, 9, &dock->last_press_0x13);
0433             return;
0434         case 0xe2:
0435             tf103c_dock_toprow_event(dock, 10, &dock->last_press_0x13);
0436             return;
0437         case 0xea:
0438             tf103c_dock_toprow_event(dock, 11, &dock->last_press_0x13);
0439             return;
0440         case 0xe9:
0441             tf103c_dock_toprow_event(dock, 12, &dock->last_press_0x13);
0442             return;
0443         }
0444         break;
0445     case 0x14:
0446         if (size != 4)
0447             break;
0448 
0449         switch (buf[3]) {
0450         case 0:
0451             tf103c_dock_toprow_event(dock, -1, &dock->last_press_0x14);
0452             return;
0453         case 1:
0454             tf103c_dock_toprow_event(dock, 0, &dock->last_press_0x14);
0455             return;
0456         }
0457         break;
0458     }
0459 
0460     dev_warn(dev, "warning unknown kbd data: %*ph\n", size, buf);
0461 }
0462 
0463 /*** touchpad related code ***/
0464 
0465 static const struct property_entry tf103c_dock_touchpad_props[] = {
0466     PROPERTY_ENTRY_BOOL("elan,clickpad"),
0467     { }
0468 };
0469 
0470 static const struct software_node tf103c_dock_touchpad_sw_node = {
0471     .properties = tf103c_dock_touchpad_props,
0472 };
0473 
0474 /*
0475  * tf103c_enable_touchpad() is only called from the threaded interrupt handler
0476  * and tf103c_disable_touchpad() is only called after the irq is disabled,
0477  * so no locking is necessary.
0478  */
0479 static void tf103c_dock_enable_touchpad(struct tf103c_dock_data *dock)
0480 {
0481     struct i2c_board_info board_info = { };
0482     struct device *dev = &dock->ec_client->dev;
0483     int ret;
0484 
0485     if (dock->tp_enabled) {
0486         /* Happens after resume, the tp needs to be reinitialized */
0487         ret = device_reprobe(&dock->tp_client->dev);
0488         if (ret)
0489             dev_err_probe(dev, ret, "reprobing tp-client\n");
0490         return;
0491     }
0492 
0493     strscpy(board_info.type, "elan_i2c", I2C_NAME_SIZE);
0494     board_info.addr = TF103C_DOCK_TP_ADDR;
0495     board_info.dev_name = TF103C_DOCK_DEV_NAME "-tp";
0496     board_info.irq = dock->tp_irq;
0497     board_info.swnode = &tf103c_dock_touchpad_sw_node;
0498 
0499     dock->tp_client = i2c_new_client_device(dock->ec_client->adapter, &board_info);
0500     if (IS_ERR(dock->tp_client)) {
0501         dev_err(dev, "error %ld creating tp client\n", PTR_ERR(dock->tp_client));
0502         return;
0503     }
0504 
0505     dock->tp_enabled = true;
0506 }
0507 
0508 static void tf103c_dock_disable_touchpad(struct tf103c_dock_data *dock)
0509 {
0510     if (!dock->tp_enabled)
0511         return;
0512 
0513     i2c_unregister_device(dock->tp_client);
0514 
0515     dock->tp_enabled = false;
0516 }
0517 
0518 /*** interrupt handling code ***/
0519 static void tf103c_dock_ec_cmd(struct tf103c_dock_data *dock, const u8 *cmd)
0520 {
0521     struct device *dev = &dock->ec_client->dev;
0522     int ret;
0523 
0524     ret = i2c_smbus_write_i2c_block_data(dock->ec_client, TF103C_DOCK_EC_CMD_REG,
0525                          TF103C_DOCK_EC_CMD_LEN, cmd);
0526     if (ret)
0527         dev_err(dev, "error %d sending %*ph cmd\n", ret,
0528             TF103C_DOCK_EC_CMD_LEN, cmd);
0529 }
0530 
0531 static void tf103c_dock_sci(struct tf103c_dock_data *dock, u8 val)
0532 {
0533     struct device *dev = &dock->ec_client->dev;
0534 
0535     switch (val) {
0536     case 2:
0537         tf103c_dock_toprow_event(dock, 1, NULL);
0538         return;
0539     case 4:
0540         tf103c_dock_toprow_event(dock, 2, NULL);
0541         return;
0542     case 8:
0543         tf103c_dock_toprow_event(dock, 5, NULL);
0544         return;
0545     case 17:
0546         tf103c_dock_toprow_event(dock, 6, NULL);
0547         return;
0548     }
0549 
0550     dev_warn(dev, "warning unknown SCI value: 0x%02x\n", val);
0551 }
0552 
0553 static void tf103c_dock_smi(struct tf103c_dock_data *dock, u8 val)
0554 {
0555     struct device *dev = &dock->ec_client->dev;
0556 
0557     switch (val) {
0558     case TF103C_DOCK_SMI_EC_WAKEUP:
0559         tf103c_dock_ec_cmd(dock, tf103c_dock_enable_cmd);
0560         tf103c_dock_ec_cmd(dock, tf103c_dock_usb_enable_cmd);
0561         tf103c_dock_kbd_write(dock, TF103C_DOCK_KBD_CMD_ENABLE);
0562         break;
0563     case TF103C_DOCK_SMI_PAD_BL_CHANGE:
0564         /* There is no backlight, but the EC still sends this */
0565         break;
0566     case TF103C_DOCK_SMI_HID_STATUS_CHANGED:
0567         tf103c_dock_enable_touchpad(dock);
0568         break;
0569     default:
0570         dev_warn(dev, "warning unknown SMI value: 0x%02x\n", val);
0571         break;
0572     }
0573 }
0574 
0575 static irqreturn_t tf103c_dock_irq(int irq, void *data)
0576 {
0577     struct tf103c_dock_data *dock = data;
0578     struct device *dev = &dock->ec_client->dev;
0579     u8 intr_data[8];
0580     int ret;
0581 
0582     ret = i2c_smbus_read_i2c_block_data(dock->intr_client, TF103C_DOCK_INTR_DATA_REG,
0583                         sizeof(intr_data), intr_data);
0584     if (ret != sizeof(intr_data)) {
0585         dev_err(dev, "error %d reading intr data\n", ret);
0586         return IRQ_NONE;
0587     }
0588 
0589     if (!(intr_data[1] & TF103C_DOCK_INTR_DATA1_OBF_MASK))
0590         return IRQ_NONE;
0591 
0592     /* intr_data[0] is the length of the rest of the packet */
0593     if (intr_data[0] == 3 && intr_data[1] == TF103C_DOCK_INTR_DATA1_OOB_VALUE &&
0594                  intr_data[2] == TF103C_DOCK_INTR_DATA2_OOB_VALUE) {
0595         /* intr_data[3] seems to contain a HID input report id */
0596         switch (intr_data[3]) {
0597         case 0x01:
0598             handle_nested_irq(dock->tp_irq);
0599             break;
0600         case 0x11:
0601         case 0x13:
0602         case 0x14:
0603             tf103c_dock_kbd_interrupt(dock);
0604             break;
0605         default:
0606             dev_warn(dev, "warning unknown intr_data[3]: 0x%02x\n", intr_data[3]);
0607             break;
0608         }
0609         return IRQ_HANDLED;
0610     }
0611 
0612     if (intr_data[1] & TF103C_DOCK_INTR_DATA1_SCI_MASK) {
0613         tf103c_dock_sci(dock, intr_data[2]);
0614         return IRQ_HANDLED;
0615     }
0616 
0617     if (intr_data[1] & TF103C_DOCK_INTR_DATA1_SMI_MASK) {
0618         tf103c_dock_smi(dock, intr_data[2]);
0619         return IRQ_HANDLED;
0620     }
0621 
0622     dev_warn(dev, "warning unknown intr data: %*ph\n", 8, intr_data);
0623     return IRQ_NONE;
0624 }
0625 
0626 /*
0627  * tf103c_dock_[dis|en]able only run from hpd_work or at times when
0628  * hpd_work cannot run (hpd_irq disabled), so no locking is necessary.
0629  */
0630 static void tf103c_dock_enable(struct tf103c_dock_data *dock)
0631 {
0632     if (dock->enabled)
0633         return;
0634 
0635     if (dock->board_rev != 2)
0636         gpiod_set_value(dock->pwr_en, 1);
0637 
0638     msleep(500);
0639     enable_irq(dock->irq);
0640 
0641     dock->enabled = true;
0642 }
0643 
0644 static void tf103c_dock_disable(struct tf103c_dock_data *dock)
0645 {
0646     if (!dock->enabled)
0647         return;
0648 
0649     disable_irq(dock->irq);
0650     tf103c_dock_disable_touchpad(dock);
0651     if (dock->board_rev != 2)
0652         gpiod_set_value(dock->pwr_en, 0);
0653 
0654     dock->enabled = false;
0655 }
0656 
0657 static void tf103c_dock_hpd_work(struct work_struct *work)
0658 {
0659     struct tf103c_dock_data *dock =
0660         container_of(work, struct tf103c_dock_data, hpd_work.work);
0661 
0662     if (gpiod_get_value(dock->hpd_gpio))
0663         tf103c_dock_enable(dock);
0664     else
0665         tf103c_dock_disable(dock);
0666 }
0667 
0668 static irqreturn_t tf103c_dock_hpd_irq(int irq, void *data)
0669 {
0670     struct tf103c_dock_data *dock = data;
0671 
0672     mod_delayed_work(system_long_wq, &dock->hpd_work, TF103C_DOCK_HPD_DEBOUNCE);
0673     return IRQ_HANDLED;
0674 }
0675 
0676 static void tf103c_dock_start_hpd(struct tf103c_dock_data *dock)
0677 {
0678     enable_irq(dock->hpd_irq);
0679     /* Sync current HPD status */
0680     queue_delayed_work(system_long_wq, &dock->hpd_work, TF103C_DOCK_HPD_DEBOUNCE);
0681 }
0682 
0683 static void tf103c_dock_stop_hpd(struct tf103c_dock_data *dock)
0684 {
0685     disable_irq(dock->hpd_irq);
0686     cancel_delayed_work_sync(&dock->hpd_work);
0687 }
0688 
0689 /*** probe ***/
0690 
0691 static const struct dmi_system_id tf103c_dock_dmi_ids[] = {
0692     {
0693         .matches = {
0694             DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
0695             DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
0696         },
0697     },
0698     { }
0699 };
0700 
0701 static void tf103c_dock_non_devm_cleanup(void *data)
0702 {
0703     struct tf103c_dock_data *dock = data;
0704 
0705     if (dock->tp_irq_domain)
0706         irq_domain_remove(dock->tp_irq_domain);
0707 
0708     if (!IS_ERR_OR_NULL(dock->hid))
0709         hid_destroy_device(dock->hid);
0710 
0711     i2c_unregister_device(dock->kbd_client);
0712     i2c_unregister_device(dock->intr_client);
0713     gpiod_remove_lookup_table(&tf103c_dock_gpios);
0714 }
0715 
0716 static int tf103c_dock_probe(struct i2c_client *client)
0717 {
0718     struct i2c_board_info board_info = { };
0719     struct device *dev = &client->dev;
0720     struct gpio_desc *board_rev_gpio;
0721     struct tf103c_dock_data *dock;
0722     enum gpiod_flags flags;
0723     int i, ret;
0724 
0725     /* GPIOs are hardcoded for the Asus TF103C, don't bind on other devs */
0726     if (!dmi_check_system(tf103c_dock_dmi_ids))
0727         return -ENODEV;
0728 
0729     dock = devm_kzalloc(dev, sizeof(*dock), GFP_KERNEL);
0730     if (!dock)
0731         return -ENOMEM;
0732 
0733     INIT_DELAYED_WORK(&dock->hpd_work, tf103c_dock_hpd_work);
0734 
0735     /* 1. Get GPIOs and their IRQs */
0736     gpiod_add_lookup_table(&tf103c_dock_gpios);
0737 
0738     ret = devm_add_action_or_reset(dev, tf103c_dock_non_devm_cleanup, dock);
0739     if (ret)
0740         return ret;
0741 
0742     /*
0743      * The pin is configured as input by default, use ASIS because otherwise
0744      * the gpio-crystalcove.c switches off the internal pull-down replacing
0745      * it with a pull-up.
0746      */
0747     board_rev_gpio = gpiod_get(dev, "board_rev", GPIOD_ASIS);
0748     if (IS_ERR(board_rev_gpio))
0749         return dev_err_probe(dev, PTR_ERR(board_rev_gpio), "requesting board_rev GPIO\n");
0750     dock->board_rev = gpiod_get_value_cansleep(board_rev_gpio) + 1;
0751     gpiod_put(board_rev_gpio);
0752 
0753     /*
0754      * The Android driver drives the dock-pwr-en pin high at probe for
0755      * revision 2 boards and then never touches it again?
0756      * This code has only been tested on a revision 1 board, so for now
0757      * just mimick what Android does on revision 2 boards.
0758      */
0759     flags = (dock->board_rev == 2) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
0760     dock->pwr_en = devm_gpiod_get(dev, "dock_pwr_en", flags);
0761     if (IS_ERR(dock->pwr_en))
0762         return dev_err_probe(dev, PTR_ERR(dock->pwr_en), "requesting pwr_en GPIO\n");
0763 
0764     dock->irq_gpio = devm_gpiod_get(dev, "dock_irq", GPIOD_IN);
0765     if (IS_ERR(dock->irq_gpio))
0766         return dev_err_probe(dev, PTR_ERR(dock->irq_gpio), "requesting IRQ GPIO\n");
0767 
0768     dock->irq = gpiod_to_irq(dock->irq_gpio);
0769     if (dock->irq < 0)
0770         return dev_err_probe(dev, dock->irq, "getting dock IRQ");
0771 
0772     ret = devm_request_threaded_irq(dev, dock->irq, NULL, tf103c_dock_irq,
0773                     IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_NO_AUTOEN,
0774                     "dock_irq", dock);
0775     if (ret)
0776         return dev_err_probe(dev, ret, "requesting dock IRQ");
0777 
0778     dock->hpd_gpio = devm_gpiod_get(dev, "dock_hpd", GPIOD_IN);
0779     if (IS_ERR(dock->hpd_gpio))
0780         return dev_err_probe(dev, PTR_ERR(dock->hpd_gpio), "requesting HPD GPIO\n");
0781 
0782     dock->hpd_irq = gpiod_to_irq(dock->hpd_gpio);
0783     if (dock->hpd_irq < 0)
0784         return dev_err_probe(dev, dock->hpd_irq, "getting HPD IRQ");
0785 
0786     ret = devm_request_irq(dev, dock->hpd_irq, tf103c_dock_hpd_irq,
0787                    IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN,
0788                    "dock_hpd", dock);
0789     if (ret)
0790         return ret;
0791 
0792     /*
0793      * 2. Create I2C clients. The dock uses 4 different i2c addresses,
0794      * the ACPI NPCE69A node being probed points to the EC address.
0795      */
0796     dock->ec_client = client;
0797 
0798     strscpy(board_info.type, "tf103c-dock-intr", I2C_NAME_SIZE);
0799     board_info.addr = TF103C_DOCK_INTR_ADDR;
0800     board_info.dev_name = TF103C_DOCK_DEV_NAME "-intr";
0801 
0802     dock->intr_client = i2c_new_client_device(client->adapter, &board_info);
0803     if (IS_ERR(dock->intr_client))
0804         return dev_err_probe(dev, PTR_ERR(dock->intr_client), "creating intr client\n");
0805 
0806     strscpy(board_info.type, "tf103c-dock-kbd", I2C_NAME_SIZE);
0807     board_info.addr = TF103C_DOCK_KBD_ADDR;
0808     board_info.dev_name = TF103C_DOCK_DEV_NAME "-kbd";
0809 
0810     dock->kbd_client = i2c_new_client_device(client->adapter, &board_info);
0811     if (IS_ERR(dock->kbd_client))
0812         return dev_err_probe(dev, PTR_ERR(dock->kbd_client), "creating kbd client\n");
0813 
0814     /* 3. Create input_dev for the top row of the keyboard */
0815     dock->input = devm_input_allocate_device(dev);
0816     if (!dock->input)
0817         return -ENOMEM;
0818 
0819     dock->input->name = "Asus TF103C Dock Top Row Keys";
0820     dock->input->phys = dev_name(dev);
0821     dock->input->dev.parent = dev;
0822     dock->input->id.bustype = BUS_I2C;
0823     dock->input->id.vendor = /* USB_VENDOR_ID_ASUSTEK */
0824     dock->input->id.product = /* From TF-103-C */
0825     dock->input->id.version = 0x0100;  /* 1.0 */
0826 
0827     for (i = 0; i < ARRAY_SIZE(tf103c_dock_toprow_codes); i++) {
0828         input_set_capability(dock->input, EV_KEY, tf103c_dock_toprow_codes[i][0]);
0829         input_set_capability(dock->input, EV_KEY, tf103c_dock_toprow_codes[i][1]);
0830     }
0831     input_set_capability(dock->input, EV_KEY, KEY_RIGHTALT);
0832 
0833     ret = input_register_device(dock->input);
0834     if (ret)
0835         return ret;
0836 
0837     /* 4. Create HID device for the keyboard */
0838     dock->hid = hid_allocate_device();
0839     if (IS_ERR(dock->hid))
0840         return dev_err_probe(dev, PTR_ERR(dock->hid), "allocating hid dev\n");
0841 
0842     dock->hid->driver_data = dock;
0843     dock->hid->ll_driver = &tf103c_dock_hid_ll_driver;
0844     dock->hid->dev.parent = &client->dev;
0845     dock->hid->bus = BUS_I2C;
0846     dock->hid->vendor = 0x0b05;  /* USB_VENDOR_ID_ASUSTEK */
0847     dock->hid->product = 0x0103; /* From TF-103-C */
0848     dock->hid->version = 0x0100; /* 1.0 */
0849     strscpy(dock->hid->name, "Asus TF103C Dock Keyboard", sizeof(dock->hid->name));
0850     strscpy(dock->hid->phys, dev_name(dev), sizeof(dock->hid->phys));
0851 
0852     ret = hid_add_device(dock->hid);
0853     if (ret)
0854         return dev_err_probe(dev, ret, "adding hid dev\n");
0855 
0856     /* 5. Setup irqchip for touchpad IRQ pass-through */
0857     dock->tp_irqchip.name = KBUILD_MODNAME;
0858 
0859     dock->tp_irq_domain = irq_domain_add_linear(NULL, 1, &irq_domain_simple_ops, NULL);
0860     if (!dock->tp_irq_domain)
0861         return -ENOMEM;
0862 
0863     dock->tp_irq = irq_create_mapping(dock->tp_irq_domain, 0);
0864     if (!dock->tp_irq)
0865         return -ENOMEM;
0866 
0867     irq_set_chip_data(dock->tp_irq, dock);
0868     irq_set_chip_and_handler(dock->tp_irq, &dock->tp_irqchip, handle_simple_irq);
0869     irq_set_nested_thread(dock->tp_irq, true);
0870     irq_set_noprobe(dock->tp_irq);
0871 
0872     dev_info(dev, "Asus TF103C board-revision: %d\n", dock->board_rev);
0873 
0874     tf103c_dock_start_hpd(dock);
0875 
0876     device_init_wakeup(dev, true);
0877     i2c_set_clientdata(client, dock);
0878     return 0;
0879 }
0880 
0881 static int tf103c_dock_remove(struct i2c_client *client)
0882 {
0883     struct tf103c_dock_data *dock = i2c_get_clientdata(client);
0884 
0885     tf103c_dock_stop_hpd(dock);
0886     tf103c_dock_disable(dock);
0887 
0888     return 0;
0889 }
0890 
0891 static int __maybe_unused tf103c_dock_suspend(struct device *dev)
0892 {
0893     struct tf103c_dock_data *dock = dev_get_drvdata(dev);
0894 
0895     tf103c_dock_stop_hpd(dock);
0896 
0897     if (dock->enabled) {
0898         tf103c_dock_ec_cmd(dock, tf103c_dock_suspend_cmd);
0899 
0900         if (device_may_wakeup(dev))
0901             enable_irq_wake(dock->irq);
0902     }
0903 
0904     return 0;
0905 }
0906 
0907 static int __maybe_unused tf103c_dock_resume(struct device *dev)
0908 {
0909     struct tf103c_dock_data *dock = dev_get_drvdata(dev);
0910 
0911     if (dock->enabled) {
0912         if (device_may_wakeup(dev))
0913             disable_irq_wake(dock->irq);
0914 
0915         /* Don't try to resume if the dock was unplugged during suspend */
0916         if (gpiod_get_value(dock->hpd_gpio))
0917             tf103c_dock_ec_cmd(dock, tf103c_dock_enable_cmd);
0918     }
0919 
0920     tf103c_dock_start_hpd(dock);
0921     return 0;
0922 }
0923 
0924 static SIMPLE_DEV_PM_OPS(tf103c_dock_pm_ops, tf103c_dock_suspend, tf103c_dock_resume);
0925 
0926 static const struct acpi_device_id tf103c_dock_acpi_match[] = {
0927     {"NPCE69A"},
0928     { }
0929 };
0930 MODULE_DEVICE_TABLE(acpi, tf103c_dock_acpi_match);
0931 
0932 static struct i2c_driver tf103c_dock_driver = {
0933     .driver = {
0934         .name = "asus-tf103c-dock",
0935         .pm = &tf103c_dock_pm_ops,
0936         .acpi_match_table = tf103c_dock_acpi_match,
0937     },
0938     .probe_new = tf103c_dock_probe,
0939     .remove = tf103c_dock_remove,
0940 };
0941 module_i2c_driver(tf103c_dock_driver);
0942 
0943 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com");
0944 MODULE_DESCRIPTION("X86 Android tablets DSDT fixups driver");
0945 MODULE_LICENSE("GPL");