dlink-dir685-touchkeys.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * D-Link DIR-685 router I2C-based Touchkeys input driver
  4. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  5. *
  6. * This is a one-off touchkey controller based on the Cypress Semiconductor
  7. * CY8C214 MCU with some firmware in its internal 8KB flash. The circuit
  8. * board inside the router is named E119921
  9. */
  10. #include <linux/module.h>
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/delay.h>
  14. #include <linux/input.h>
  15. #include <linux/slab.h>
  16. #include <linux/bitops.h>
  17. struct dir685_touchkeys {
  18. struct device *dev;
  19. struct i2c_client *client;
  20. struct input_dev *input;
  21. unsigned long cur_key;
  22. u16 codes[7];
  23. };
  24. static irqreturn_t dir685_tk_irq_thread(int irq, void *data)
  25. {
  26. struct dir685_touchkeys *tk = data;
  27. const int num_bits = min_t(int, ARRAY_SIZE(tk->codes), 16);
  28. unsigned long changed;
  29. u8 buf[6];
  30. unsigned long key;
  31. int i;
  32. int err;
  33. memset(buf, 0, sizeof(buf));
  34. err = i2c_master_recv(tk->client, buf, sizeof(buf));
  35. if (err != sizeof(buf)) {
  36. dev_err(tk->dev, "short read %d\n", err);
  37. return IRQ_HANDLED;
  38. }
  39. dev_dbg(tk->dev, "IN: %*ph\n", (int)sizeof(buf), buf);
  40. key = be16_to_cpup((__be16 *) &buf[4]);
  41. /* Figure out if any bits went high or low since last message */
  42. changed = tk->cur_key ^ key;
  43. for_each_set_bit(i, &changed, num_bits) {
  44. dev_dbg(tk->dev, "key %d is %s\n", i,
  45. test_bit(i, &key) ? "down" : "up");
  46. input_report_key(tk->input, tk->codes[i], test_bit(i, &key));
  47. }
  48. /* Store currently down keys */
  49. tk->cur_key = key;
  50. input_sync(tk->input);
  51. return IRQ_HANDLED;
  52. }
  53. static int dir685_tk_probe(struct i2c_client *client,
  54. const struct i2c_device_id *id)
  55. {
  56. struct dir685_touchkeys *tk;
  57. struct device *dev = &client->dev;
  58. u8 bl_data[] = { 0xa7, 0x40 };
  59. int err;
  60. int i;
  61. tk = devm_kzalloc(&client->dev, sizeof(*tk), GFP_KERNEL);
  62. if (!tk)
  63. return -ENOMEM;
  64. tk->input = devm_input_allocate_device(dev);
  65. if (!tk->input)
  66. return -ENOMEM;
  67. tk->client = client;
  68. tk->dev = dev;
  69. tk->input->keycodesize = sizeof(u16);
  70. tk->input->keycodemax = ARRAY_SIZE(tk->codes);
  71. tk->input->keycode = tk->codes;
  72. tk->codes[0] = KEY_UP;
  73. tk->codes[1] = KEY_DOWN;
  74. tk->codes[2] = KEY_LEFT;
  75. tk->codes[3] = KEY_RIGHT;
  76. tk->codes[4] = KEY_ENTER;
  77. tk->codes[5] = KEY_WPS_BUTTON;
  78. /*
  79. * This key appears in the vendor driver, but I have
  80. * not been able to activate it.
  81. */
  82. tk->codes[6] = KEY_RESERVED;
  83. __set_bit(EV_KEY, tk->input->evbit);
  84. for (i = 0; i < ARRAY_SIZE(tk->codes); i++)
  85. __set_bit(tk->codes[i], tk->input->keybit);
  86. __clear_bit(KEY_RESERVED, tk->input->keybit);
  87. tk->input->name = "D-Link DIR-685 touchkeys";
  88. tk->input->id.bustype = BUS_I2C;
  89. err = input_register_device(tk->input);
  90. if (err)
  91. return err;
  92. /* Set the brightness to max level */
  93. err = i2c_master_send(client, bl_data, sizeof(bl_data));
  94. if (err != sizeof(bl_data))
  95. dev_warn(tk->dev, "error setting brightness level\n");
  96. if (!client->irq) {
  97. dev_err(dev, "no IRQ on the I2C device\n");
  98. return -ENODEV;
  99. }
  100. err = devm_request_threaded_irq(dev, client->irq,
  101. NULL, dir685_tk_irq_thread,
  102. IRQF_ONESHOT,
  103. "dir685-tk", tk);
  104. if (err) {
  105. dev_err(dev, "can't request IRQ\n");
  106. return err;
  107. }
  108. return 0;
  109. }
  110. static const struct i2c_device_id dir685_tk_id[] = {
  111. { "dir685tk", 0 },
  112. { }
  113. };
  114. MODULE_DEVICE_TABLE(i2c, dir685_tk_id);
  115. #ifdef CONFIG_OF
  116. static const struct of_device_id dir685_tk_of_match[] = {
  117. { .compatible = "dlink,dir685-touchkeys" },
  118. {},
  119. };
  120. MODULE_DEVICE_TABLE(of, dir685_tk_of_match);
  121. #endif
  122. static struct i2c_driver dir685_tk_i2c_driver = {
  123. .driver = {
  124. .name = "dlink-dir685-touchkeys",
  125. .of_match_table = of_match_ptr(dir685_tk_of_match),
  126. },
  127. .probe = dir685_tk_probe,
  128. .id_table = dir685_tk_id,
  129. };
  130. module_i2c_driver(dir685_tk_i2c_driver);
  131. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  132. MODULE_DESCRIPTION("D-Link DIR-685 touchkeys driver");
  133. MODULE_LICENSE("GPL");