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