Lines Matching +full:i2c +full:- +full:hid
1 // SPDX-License-Identifier: GPL-2.0+
4 * keyboard dock for the Asus TF103C 2-in-1 tablet.
6 * This keyboard dock has its own I2C attached embedded controller
7 * and the keyboard and touchpad are also connected over I2C,
19 #include <linux/hid.h>
20 #include <linux/i2c.h>
35 "can be pressed to change this to F1-F12. Set this to 1 to "
42 /*** Touchpad I2C device defines ***/
45 /*** Keyboard I2C device defines **A*/
57 /*** EC innterrupt data I2C device defines ***/
87 /*** EC (dockram) I2C device defines ***/
109 struct hid_device *hid; member
126 .dev_id = "i2c-" TF103C_DOCK_DEV_NAME,
172 struct i2c_client *client = dock->kbd_client; in tf103c_dock_kbd_read()
173 struct device *dev = &dock->ec_client->dev; in tf103c_dock_kbd_read()
181 msgs[0].addr = client->addr; in tf103c_dock_kbd_read()
186 msgs[1].addr = client->addr; in tf103c_dock_kbd_read()
189 msgs[1].buf = dock->kbd_buf; in tf103c_dock_kbd_read()
191 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); in tf103c_dock_kbd_read()
194 return -EIO; in tf103c_dock_kbd_read()
202 struct device *dev = &dock->ec_client->dev; in tf103c_dock_kbd_write()
209 ret = i2c_master_send(dock->kbd_client, buf, sizeof(buf)); in tf103c_dock_kbd_write()
214 /* HID ll_driver functions for forwarding input-reports from the kbd_client */
215 static int tf103c_dock_hid_parse(struct hid_device *hid) in tf103c_dock_hid_parse() argument
217 return hid_parse_report(hid, tf103c_dock_kbd_hid_desc, in tf103c_dock_hid_parse()
221 static int tf103c_dock_hid_start(struct hid_device *hid) in tf103c_dock_hid_start() argument
226 static void tf103c_dock_hid_stop(struct hid_device *hid) in tf103c_dock_hid_stop() argument
228 hid->claimed = 0; in tf103c_dock_hid_stop()
231 static int tf103c_dock_hid_open(struct hid_device *hid) in tf103c_dock_hid_open() argument
233 struct tf103c_dock_data *dock = hid->driver_data; in tf103c_dock_hid_open()
235 set_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags); in tf103c_dock_hid_open()
239 static void tf103c_dock_hid_close(struct hid_device *hid) in tf103c_dock_hid_close() argument
241 struct tf103c_dock_data *dock = hid->driver_data; in tf103c_dock_hid_close()
243 clear_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags); in tf103c_dock_hid_close()
247 static int tf103c_dock_hid_raw_request(struct hid_device *hid, u8 reportnum, in tf103c_dock_hid_raw_request() argument
281 u8 *esc, *buf = dock->kbd_buf; in tf103c_dock_report_toprow_kbd_hook()
290 dock->altgr_pressed = buf[TF103C_DOCK_KBD_DATA_MODIFIERS] & 0x40; in tf103c_dock_report_toprow_kbd_hook()
293 input_report_key(dock->input, KEY_RIGHTALT, dock->altgr_pressed); in tf103c_dock_report_toprow_kbd_hook()
294 input_sync(dock->input); in tf103c_dock_report_toprow_kbd_hook()
298 size = TF103C_DOCK_KBD_DATA_MAX_LENGTH - TF103C_DOCK_KBD_DATA_KEYS; in tf103c_dock_report_toprow_kbd_hook()
300 if (!dock->esc_pressed && esc) { in tf103c_dock_report_toprow_kbd_hook()
301 if (dock->altgr_pressed) { in tf103c_dock_report_toprow_kbd_hook()
303 dock->filter_esc = true; in tf103c_dock_report_toprow_kbd_hook()
306 if (esc && dock->filter_esc) in tf103c_dock_report_toprow_kbd_hook()
309 dock->filter_esc = false; in tf103c_dock_report_toprow_kbd_hook()
311 dock->esc_pressed = esc != NULL; in tf103c_dock_report_toprow_kbd_hook()
320 if (dock->altgr_pressed) { in tf103c_dock_toprow_press()
321 input_report_key(dock->input, KEY_RIGHTALT, false); in tf103c_dock_toprow_press()
322 input_sync(dock->input); in tf103c_dock_toprow_press()
325 input_report_key(dock->input, key_code, true); in tf103c_dock_toprow_press()
326 input_sync(dock->input); in tf103c_dock_toprow_press()
331 input_report_key(dock->input, key_code, false); in tf103c_dock_toprow_release()
332 input_sync(dock->input); in tf103c_dock_toprow_release()
334 if (dock->altgr_pressed) { in tf103c_dock_toprow_release()
335 input_report_key(dock->input, KEY_RIGHTALT, true); in tf103c_dock_toprow_release()
336 input_sync(dock->input); in tf103c_dock_toprow_release()
343 int key_code, fn = dock->altgr_pressed ^ fnlock; in tf103c_dock_toprow_event()
363 * The keyboard sends what appears to be standard I2C-HID input-reports,
364 * except that a 16 bit register address of where the I2C-HID format
365 * input-reports are stored must be send before reading it in a single
366 * (I2C repeated-start) I2C transaction.
368 * Its unknown how to get the HID descriptors but they are easy to reconstruct:
370 * Input report id 0x11 is 8 bytes long and contain standard USB HID intf-class,
375 * However the top row keys (where a normal keyboard has F1-F12 + Print-Screen)
377 * events; and these need special handling to allow actually sending F1-F12,
381 * So only forward the 0x11 reports to HID and handle the top-row keys here.
385 struct device *dev = &dock->ec_client->dev; in tf103c_dock_kbd_interrupt()
386 u8 *buf = dock->kbd_buf; in tf103c_dock_kbd_interrupt()
395 dev_err(dev, "error reported kbd pkt size %d is out of range %d-%d\n", size, in tf103c_dock_kbd_interrupt()
408 if (test_bit(TF103C_DOCK_FLAG_HID_OPEN, &dock->flags)) in tf103c_dock_kbd_interrupt()
409 hid_input_report(dock->hid, HID_INPUT_REPORT, buf + 2, size - 2, 1); in tf103c_dock_kbd_interrupt()
417 tf103c_dock_toprow_event(dock, -1, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
420 tf103c_dock_toprow_event(dock, 3, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
423 tf103c_dock_toprow_event(dock, 4, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
426 tf103c_dock_toprow_event(dock, 7, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
429 tf103c_dock_toprow_event(dock, 8, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
432 tf103c_dock_toprow_event(dock, 9, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
435 tf103c_dock_toprow_event(dock, 10, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
438 tf103c_dock_toprow_event(dock, 11, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
441 tf103c_dock_toprow_event(dock, 12, &dock->last_press_0x13); in tf103c_dock_kbd_interrupt()
451 tf103c_dock_toprow_event(dock, -1, &dock->last_press_0x14); in tf103c_dock_kbd_interrupt()
454 tf103c_dock_toprow_event(dock, 0, &dock->last_press_0x14); in tf103c_dock_kbd_interrupt()
482 struct device *dev = &dock->ec_client->dev; in tf103c_dock_enable_touchpad()
485 if (dock->tp_enabled) { in tf103c_dock_enable_touchpad()
487 ret = device_reprobe(&dock->tp_client->dev); in tf103c_dock_enable_touchpad()
489 dev_err_probe(dev, ret, "reprobing tp-client\n"); in tf103c_dock_enable_touchpad()
495 board_info.dev_name = TF103C_DOCK_DEV_NAME "-tp"; in tf103c_dock_enable_touchpad()
496 board_info.irq = dock->tp_irq; in tf103c_dock_enable_touchpad()
499 dock->tp_client = i2c_new_client_device(dock->ec_client->adapter, &board_info); in tf103c_dock_enable_touchpad()
500 if (IS_ERR(dock->tp_client)) { in tf103c_dock_enable_touchpad()
501 dev_err(dev, "error %ld creating tp client\n", PTR_ERR(dock->tp_client)); in tf103c_dock_enable_touchpad()
505 dock->tp_enabled = true; in tf103c_dock_enable_touchpad()
510 if (!dock->tp_enabled) in tf103c_dock_disable_touchpad()
513 i2c_unregister_device(dock->tp_client); in tf103c_dock_disable_touchpad()
515 dock->tp_enabled = false; in tf103c_dock_disable_touchpad()
521 struct device *dev = &dock->ec_client->dev; in tf103c_dock_ec_cmd()
524 ret = i2c_smbus_write_i2c_block_data(dock->ec_client, TF103C_DOCK_EC_CMD_REG, in tf103c_dock_ec_cmd()
533 struct device *dev = &dock->ec_client->dev; in tf103c_dock_sci()
555 struct device *dev = &dock->ec_client->dev; in tf103c_dock_smi()
578 struct device *dev = &dock->ec_client->dev; in tf103c_dock_irq()
582 ret = i2c_smbus_read_i2c_block_data(dock->intr_client, TF103C_DOCK_INTR_DATA_REG, in tf103c_dock_irq()
595 /* intr_data[3] seems to contain a HID input report id */ in tf103c_dock_irq()
598 handle_nested_irq(dock->tp_irq); in tf103c_dock_irq()
632 if (dock->enabled) in tf103c_dock_enable()
635 if (dock->board_rev != 2) in tf103c_dock_enable()
636 gpiod_set_value(dock->pwr_en, 1); in tf103c_dock_enable()
639 enable_irq(dock->irq); in tf103c_dock_enable()
641 dock->enabled = true; in tf103c_dock_enable()
646 if (!dock->enabled) in tf103c_dock_disable()
649 disable_irq(dock->irq); in tf103c_dock_disable()
651 if (dock->board_rev != 2) in tf103c_dock_disable()
652 gpiod_set_value(dock->pwr_en, 0); in tf103c_dock_disable()
654 dock->enabled = false; in tf103c_dock_disable()
662 if (gpiod_get_value(dock->hpd_gpio)) in tf103c_dock_hpd_work()
672 mod_delayed_work(system_long_wq, &dock->hpd_work, TF103C_DOCK_HPD_DEBOUNCE); in tf103c_dock_hpd_irq()
678 enable_irq(dock->hpd_irq); in tf103c_dock_start_hpd()
680 queue_delayed_work(system_long_wq, &dock->hpd_work, TF103C_DOCK_HPD_DEBOUNCE); in tf103c_dock_start_hpd()
685 disable_irq(dock->hpd_irq); in tf103c_dock_stop_hpd()
686 cancel_delayed_work_sync(&dock->hpd_work); in tf103c_dock_stop_hpd()
705 if (dock->tp_irq_domain) in tf103c_dock_non_devm_cleanup()
706 irq_domain_remove(dock->tp_irq_domain); in tf103c_dock_non_devm_cleanup()
708 if (!IS_ERR_OR_NULL(dock->hid)) in tf103c_dock_non_devm_cleanup()
709 hid_destroy_device(dock->hid); in tf103c_dock_non_devm_cleanup()
711 i2c_unregister_device(dock->kbd_client); in tf103c_dock_non_devm_cleanup()
712 i2c_unregister_device(dock->intr_client); in tf103c_dock_non_devm_cleanup()
719 struct device *dev = &client->dev; in tf103c_dock_probe()
727 return -ENODEV; in tf103c_dock_probe()
731 return -ENOMEM; in tf103c_dock_probe()
733 INIT_DELAYED_WORK(&dock->hpd_work, tf103c_dock_hpd_work); in tf103c_dock_probe()
744 * the gpio-crystalcove.c switches off the internal pull-down replacing in tf103c_dock_probe()
745 * it with a pull-up. in tf103c_dock_probe()
750 dock->board_rev = gpiod_get_value_cansleep(board_rev_gpio) + 1; in tf103c_dock_probe()
754 * The Android driver drives the dock-pwr-en pin high at probe for in tf103c_dock_probe()
759 flags = (dock->board_rev == 2) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; in tf103c_dock_probe()
760 dock->pwr_en = devm_gpiod_get(dev, "dock_pwr_en", flags); in tf103c_dock_probe()
761 if (IS_ERR(dock->pwr_en)) in tf103c_dock_probe()
762 return dev_err_probe(dev, PTR_ERR(dock->pwr_en), "requesting pwr_en GPIO\n"); in tf103c_dock_probe()
764 dock->irq_gpio = devm_gpiod_get(dev, "dock_irq", GPIOD_IN); in tf103c_dock_probe()
765 if (IS_ERR(dock->irq_gpio)) in tf103c_dock_probe()
766 return dev_err_probe(dev, PTR_ERR(dock->irq_gpio), "requesting IRQ GPIO\n"); in tf103c_dock_probe()
768 dock->irq = gpiod_to_irq(dock->irq_gpio); in tf103c_dock_probe()
769 if (dock->irq < 0) in tf103c_dock_probe()
770 return dev_err_probe(dev, dock->irq, "getting dock IRQ"); in tf103c_dock_probe()
772 ret = devm_request_threaded_irq(dev, dock->irq, NULL, tf103c_dock_irq, in tf103c_dock_probe()
778 dock->hpd_gpio = devm_gpiod_get(dev, "dock_hpd", GPIOD_IN); in tf103c_dock_probe()
779 if (IS_ERR(dock->hpd_gpio)) in tf103c_dock_probe()
780 return dev_err_probe(dev, PTR_ERR(dock->hpd_gpio), "requesting HPD GPIO\n"); in tf103c_dock_probe()
782 dock->hpd_irq = gpiod_to_irq(dock->hpd_gpio); in tf103c_dock_probe()
783 if (dock->hpd_irq < 0) in tf103c_dock_probe()
784 return dev_err_probe(dev, dock->hpd_irq, "getting HPD IRQ"); in tf103c_dock_probe()
786 ret = devm_request_irq(dev, dock->hpd_irq, tf103c_dock_hpd_irq, in tf103c_dock_probe()
793 * 2. Create I2C clients. The dock uses 4 different i2c addresses, in tf103c_dock_probe()
796 dock->ec_client = client; in tf103c_dock_probe()
798 strscpy(board_info.type, "tf103c-dock-intr"); in tf103c_dock_probe()
800 board_info.dev_name = TF103C_DOCK_DEV_NAME "-intr"; in tf103c_dock_probe()
802 dock->intr_client = i2c_new_client_device(client->adapter, &board_info); in tf103c_dock_probe()
803 if (IS_ERR(dock->intr_client)) in tf103c_dock_probe()
804 return dev_err_probe(dev, PTR_ERR(dock->intr_client), "creating intr client\n"); in tf103c_dock_probe()
806 strscpy(board_info.type, "tf103c-dock-kbd"); in tf103c_dock_probe()
808 board_info.dev_name = TF103C_DOCK_DEV_NAME "-kbd"; in tf103c_dock_probe()
810 dock->kbd_client = i2c_new_client_device(client->adapter, &board_info); in tf103c_dock_probe()
811 if (IS_ERR(dock->kbd_client)) in tf103c_dock_probe()
812 return dev_err_probe(dev, PTR_ERR(dock->kbd_client), "creating kbd client\n"); in tf103c_dock_probe()
815 dock->input = devm_input_allocate_device(dev); in tf103c_dock_probe()
816 if (!dock->input) in tf103c_dock_probe()
817 return -ENOMEM; in tf103c_dock_probe()
819 dock->input->name = "Asus TF103C Dock Top Row Keys"; in tf103c_dock_probe()
820 dock->input->phys = dev_name(dev); in tf103c_dock_probe()
821 dock->input->dev.parent = dev; in tf103c_dock_probe()
822 dock->input->id.bustype = BUS_I2C; in tf103c_dock_probe()
823 dock->input->id.vendor = /* USB_VENDOR_ID_ASUSTEK */ in tf103c_dock_probe()
824 dock->input->id.product = /* From TF-103-C */ in tf103c_dock_probe()
825 dock->input->id.version = 0x0100; /* 1.0 */ in tf103c_dock_probe()
828 input_set_capability(dock->input, EV_KEY, tf103c_dock_toprow_codes[i][0]); in tf103c_dock_probe()
829 input_set_capability(dock->input, EV_KEY, tf103c_dock_toprow_codes[i][1]); in tf103c_dock_probe()
831 input_set_capability(dock->input, EV_KEY, KEY_RIGHTALT); in tf103c_dock_probe()
833 ret = input_register_device(dock->input); in tf103c_dock_probe()
837 /* 4. Create HID device for the keyboard */ in tf103c_dock_probe()
838 dock->hid = hid_allocate_device(); in tf103c_dock_probe()
839 if (IS_ERR(dock->hid)) in tf103c_dock_probe()
840 return dev_err_probe(dev, PTR_ERR(dock->hid), "allocating hid dev\n"); in tf103c_dock_probe()
842 dock->hid->driver_data = dock; in tf103c_dock_probe()
843 dock->hid->ll_driver = &tf103c_dock_hid_ll_driver; in tf103c_dock_probe()
844 dock->hid->dev.parent = &client->dev; in tf103c_dock_probe()
845 dock->hid->bus = BUS_I2C; in tf103c_dock_probe()
846 dock->hid->vendor = 0x0b05; /* USB_VENDOR_ID_ASUSTEK */ in tf103c_dock_probe()
847 dock->hid->product = 0x0103; /* From TF-103-C */ in tf103c_dock_probe()
848 dock->hid->version = 0x0100; /* 1.0 */ in tf103c_dock_probe()
849 strscpy(dock->hid->name, "Asus TF103C Dock Keyboard"); in tf103c_dock_probe()
850 strscpy(dock->hid->phys, dev_name(dev)); in tf103c_dock_probe()
852 ret = hid_add_device(dock->hid); in tf103c_dock_probe()
854 return dev_err_probe(dev, ret, "adding hid dev\n"); in tf103c_dock_probe()
856 /* 5. Setup irqchip for touchpad IRQ pass-through */ in tf103c_dock_probe()
857 dock->tp_irqchip.name = KBUILD_MODNAME; in tf103c_dock_probe()
859 dock->tp_irq_domain = irq_domain_add_linear(NULL, 1, &irq_domain_simple_ops, NULL); in tf103c_dock_probe()
860 if (!dock->tp_irq_domain) in tf103c_dock_probe()
861 return -ENOMEM; in tf103c_dock_probe()
863 dock->tp_irq = irq_create_mapping(dock->tp_irq_domain, 0); in tf103c_dock_probe()
864 if (!dock->tp_irq) in tf103c_dock_probe()
865 return -ENOMEM; in tf103c_dock_probe()
867 irq_set_chip_data(dock->tp_irq, dock); in tf103c_dock_probe()
868 irq_set_chip_and_handler(dock->tp_irq, &dock->tp_irqchip, handle_simple_irq); in tf103c_dock_probe()
869 irq_set_nested_thread(dock->tp_irq, true); in tf103c_dock_probe()
870 irq_set_noprobe(dock->tp_irq); in tf103c_dock_probe()
872 dev_info(dev, "Asus TF103C board-revision: %d\n", dock->board_rev); in tf103c_dock_probe()
895 if (dock->enabled) { in tf103c_dock_suspend()
899 enable_irq_wake(dock->irq); in tf103c_dock_suspend()
909 if (dock->enabled) { in tf103c_dock_resume()
911 disable_irq_wake(dock->irq); in tf103c_dock_resume()
914 if (gpiod_get_value(dock->hpd_gpio)) in tf103c_dock_resume()
932 .name = "asus-tf103c-dock",