xref: /linux/drivers/input/keyboard/dlink-dir685-touchkeys.c (revision d4a596eddb90114f5f5f32a440057a175517b090)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2131b3de7SLinus Walleij /*
3131b3de7SLinus Walleij  * D-Link DIR-685 router I2C-based Touchkeys input driver
4131b3de7SLinus Walleij  * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
5131b3de7SLinus Walleij  *
6131b3de7SLinus Walleij  * This is a one-off touchkey controller based on the Cypress Semiconductor
7131b3de7SLinus Walleij  * CY8C214 MCU with some firmware in its internal 8KB flash. The circuit
8131b3de7SLinus Walleij  * board inside the router is named E119921
9131b3de7SLinus Walleij  */
10131b3de7SLinus Walleij 
11131b3de7SLinus Walleij #include <linux/module.h>
12131b3de7SLinus Walleij #include <linux/i2c.h>
13131b3de7SLinus Walleij #include <linux/interrupt.h>
14131b3de7SLinus Walleij #include <linux/delay.h>
15131b3de7SLinus Walleij #include <linux/input.h>
16131b3de7SLinus Walleij #include <linux/slab.h>
17131b3de7SLinus Walleij #include <linux/bitops.h>
18131b3de7SLinus Walleij 
19131b3de7SLinus Walleij struct dir685_touchkeys {
20131b3de7SLinus Walleij 	struct device		*dev;
21131b3de7SLinus Walleij 	struct i2c_client	*client;
22131b3de7SLinus Walleij 	struct input_dev	*input;
23131b3de7SLinus Walleij 	unsigned long		cur_key;
24131b3de7SLinus Walleij 	u16			codes[7];
25131b3de7SLinus Walleij };
26131b3de7SLinus Walleij 
27131b3de7SLinus Walleij static irqreturn_t dir685_tk_irq_thread(int irq, void *data)
28131b3de7SLinus Walleij {
29131b3de7SLinus Walleij 	struct dir685_touchkeys *tk = data;
30131b3de7SLinus Walleij 	const int num_bits = min_t(int, ARRAY_SIZE(tk->codes), 16);
31131b3de7SLinus Walleij 	unsigned long changed;
32131b3de7SLinus Walleij 	u8 buf[6];
33131b3de7SLinus Walleij 	unsigned long key;
34131b3de7SLinus Walleij 	int i;
35131b3de7SLinus Walleij 	int err;
36131b3de7SLinus Walleij 
37131b3de7SLinus Walleij 	memset(buf, 0, sizeof(buf));
38131b3de7SLinus Walleij 	err = i2c_master_recv(tk->client, buf, sizeof(buf));
39131b3de7SLinus Walleij 	if (err != sizeof(buf)) {
40131b3de7SLinus Walleij 		dev_err(tk->dev, "short read %d\n", err);
41131b3de7SLinus Walleij 		return IRQ_HANDLED;
42131b3de7SLinus Walleij 	}
43131b3de7SLinus Walleij 
44131b3de7SLinus Walleij 	dev_dbg(tk->dev, "IN: %*ph\n", (int)sizeof(buf), buf);
45131b3de7SLinus Walleij 	key = be16_to_cpup((__be16 *) &buf[4]);
46131b3de7SLinus Walleij 
47131b3de7SLinus Walleij 	/* Figure out if any bits went high or low since last message */
48131b3de7SLinus Walleij 	changed = tk->cur_key ^ key;
49131b3de7SLinus Walleij 	for_each_set_bit(i, &changed, num_bits) {
50131b3de7SLinus Walleij 		dev_dbg(tk->dev, "key %d is %s\n", i,
51131b3de7SLinus Walleij 			test_bit(i, &key) ? "down" : "up");
52131b3de7SLinus Walleij 		input_report_key(tk->input, tk->codes[i], test_bit(i, &key));
53131b3de7SLinus Walleij 	}
54131b3de7SLinus Walleij 
55131b3de7SLinus Walleij 	/* Store currently down keys */
56131b3de7SLinus Walleij 	tk->cur_key = key;
57131b3de7SLinus Walleij 	input_sync(tk->input);
58131b3de7SLinus Walleij 
59131b3de7SLinus Walleij 	return IRQ_HANDLED;
60131b3de7SLinus Walleij }
61131b3de7SLinus Walleij 
62131b3de7SLinus Walleij static int dir685_tk_probe(struct i2c_client *client,
63131b3de7SLinus Walleij 			   const struct i2c_device_id *id)
64131b3de7SLinus Walleij {
65*d4a596edSColin Ian King 	static const u8 bl_data[] = { 0xa7, 0x40 };
66131b3de7SLinus Walleij 	struct device *dev = &client->dev;
67*d4a596edSColin Ian King 	struct dir685_touchkeys *tk;
68131b3de7SLinus Walleij 	int err;
69131b3de7SLinus Walleij 	int i;
70131b3de7SLinus Walleij 
71131b3de7SLinus Walleij 	tk = devm_kzalloc(&client->dev, sizeof(*tk), GFP_KERNEL);
72131b3de7SLinus Walleij 	if (!tk)
73131b3de7SLinus Walleij 		return -ENOMEM;
74131b3de7SLinus Walleij 
75131b3de7SLinus Walleij 	tk->input = devm_input_allocate_device(dev);
76131b3de7SLinus Walleij 	if (!tk->input)
77131b3de7SLinus Walleij 		return -ENOMEM;
78131b3de7SLinus Walleij 
79131b3de7SLinus Walleij 	tk->client = client;
80131b3de7SLinus Walleij 	tk->dev = dev;
81131b3de7SLinus Walleij 
82131b3de7SLinus Walleij 	tk->input->keycodesize = sizeof(u16);
83131b3de7SLinus Walleij 	tk->input->keycodemax = ARRAY_SIZE(tk->codes);
84131b3de7SLinus Walleij 	tk->input->keycode = tk->codes;
85131b3de7SLinus Walleij 	tk->codes[0] = KEY_UP;
86131b3de7SLinus Walleij 	tk->codes[1] = KEY_DOWN;
87131b3de7SLinus Walleij 	tk->codes[2] = KEY_LEFT;
88131b3de7SLinus Walleij 	tk->codes[3] = KEY_RIGHT;
89131b3de7SLinus Walleij 	tk->codes[4] = KEY_ENTER;
90131b3de7SLinus Walleij 	tk->codes[5] = KEY_WPS_BUTTON;
91131b3de7SLinus Walleij 	/*
92131b3de7SLinus Walleij 	 * This key appears in the vendor driver, but I have
93131b3de7SLinus Walleij 	 * not been able to activate it.
94131b3de7SLinus Walleij 	 */
95131b3de7SLinus Walleij 	tk->codes[6] = KEY_RESERVED;
96131b3de7SLinus Walleij 
97131b3de7SLinus Walleij 	__set_bit(EV_KEY, tk->input->evbit);
98131b3de7SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(tk->codes); i++)
99131b3de7SLinus Walleij 		__set_bit(tk->codes[i], tk->input->keybit);
100131b3de7SLinus Walleij 	__clear_bit(KEY_RESERVED, tk->input->keybit);
101131b3de7SLinus Walleij 
102131b3de7SLinus Walleij 	tk->input->name = "D-Link DIR-685 touchkeys";
103131b3de7SLinus Walleij 	tk->input->id.bustype = BUS_I2C;
104131b3de7SLinus Walleij 
105131b3de7SLinus Walleij 	err = input_register_device(tk->input);
106131b3de7SLinus Walleij 	if (err)
107131b3de7SLinus Walleij 		return err;
108131b3de7SLinus Walleij 
109131b3de7SLinus Walleij 	/* Set the brightness to max level */
110131b3de7SLinus Walleij 	err = i2c_master_send(client, bl_data, sizeof(bl_data));
111131b3de7SLinus Walleij 	if (err != sizeof(bl_data))
112131b3de7SLinus Walleij 		dev_warn(tk->dev, "error setting brightness level\n");
113131b3de7SLinus Walleij 
114131b3de7SLinus Walleij 	if (!client->irq) {
115131b3de7SLinus Walleij 		dev_err(dev, "no IRQ on the I2C device\n");
116131b3de7SLinus Walleij 		return -ENODEV;
117131b3de7SLinus Walleij 	}
118131b3de7SLinus Walleij 	err = devm_request_threaded_irq(dev, client->irq,
119131b3de7SLinus Walleij 					NULL, dir685_tk_irq_thread,
120131b3de7SLinus Walleij 					IRQF_ONESHOT,
121131b3de7SLinus Walleij 					"dir685-tk", tk);
122131b3de7SLinus Walleij 	if (err) {
123131b3de7SLinus Walleij 		dev_err(dev, "can't request IRQ\n");
124131b3de7SLinus Walleij 		return err;
125131b3de7SLinus Walleij 	}
126131b3de7SLinus Walleij 
127131b3de7SLinus Walleij 	return 0;
128131b3de7SLinus Walleij }
129131b3de7SLinus Walleij 
130131b3de7SLinus Walleij static const struct i2c_device_id dir685_tk_id[] = {
131131b3de7SLinus Walleij 	{ "dir685tk", 0 },
132131b3de7SLinus Walleij 	{ }
133131b3de7SLinus Walleij };
134131b3de7SLinus Walleij MODULE_DEVICE_TABLE(i2c, dir685_tk_id);
135131b3de7SLinus Walleij 
136131b3de7SLinus Walleij #ifdef CONFIG_OF
137131b3de7SLinus Walleij static const struct of_device_id dir685_tk_of_match[] = {
138131b3de7SLinus Walleij 	{ .compatible = "dlink,dir685-touchkeys" },
139131b3de7SLinus Walleij 	{},
140131b3de7SLinus Walleij };
141131b3de7SLinus Walleij MODULE_DEVICE_TABLE(of, dir685_tk_of_match);
142131b3de7SLinus Walleij #endif
143131b3de7SLinus Walleij 
144131b3de7SLinus Walleij static struct i2c_driver dir685_tk_i2c_driver = {
145131b3de7SLinus Walleij 	.driver = {
14638347374SChristophe JAILLET 		.name	= "dlink-dir685-touchkeys",
147131b3de7SLinus Walleij 		.of_match_table = of_match_ptr(dir685_tk_of_match),
148131b3de7SLinus Walleij 	},
149131b3de7SLinus Walleij 	.probe		= dir685_tk_probe,
150131b3de7SLinus Walleij 	.id_table	= dir685_tk_id,
151131b3de7SLinus Walleij };
152131b3de7SLinus Walleij module_i2c_driver(dir685_tk_i2c_driver);
153131b3de7SLinus Walleij 
154131b3de7SLinus Walleij MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
155131b3de7SLinus Walleij MODULE_DESCRIPTION("D-Link DIR-685 touchkeys driver");
156131b3de7SLinus Walleij MODULE_LICENSE("GPL");
157