18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * D-Link DIR-685 router I2C-based Touchkeys input driver
48c2ecf20Sopenharmony_ci * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This is a one-off touchkey controller based on the Cypress Semiconductor
78c2ecf20Sopenharmony_ci * CY8C214 MCU with some firmware in its internal 8KB flash. The circuit
88c2ecf20Sopenharmony_ci * board inside the router is named E119921
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/input.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/bitops.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct dir685_touchkeys {
208c2ecf20Sopenharmony_ci	struct device		*dev;
218c2ecf20Sopenharmony_ci	struct i2c_client	*client;
228c2ecf20Sopenharmony_ci	struct input_dev	*input;
238c2ecf20Sopenharmony_ci	unsigned long		cur_key;
248c2ecf20Sopenharmony_ci	u16			codes[7];
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic irqreturn_t dir685_tk_irq_thread(int irq, void *data)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct dir685_touchkeys *tk = data;
308c2ecf20Sopenharmony_ci	const int num_bits = min_t(int, ARRAY_SIZE(tk->codes), 16);
318c2ecf20Sopenharmony_ci	unsigned long changed;
328c2ecf20Sopenharmony_ci	u8 buf[6];
338c2ecf20Sopenharmony_ci	unsigned long key;
348c2ecf20Sopenharmony_ci	int i;
358c2ecf20Sopenharmony_ci	int err;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	memset(buf, 0, sizeof(buf));
388c2ecf20Sopenharmony_ci	err = i2c_master_recv(tk->client, buf, sizeof(buf));
398c2ecf20Sopenharmony_ci	if (err != sizeof(buf)) {
408c2ecf20Sopenharmony_ci		dev_err(tk->dev, "short read %d\n", err);
418c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	dev_dbg(tk->dev, "IN: %*ph\n", (int)sizeof(buf), buf);
458c2ecf20Sopenharmony_ci	key = be16_to_cpup((__be16 *) &buf[4]);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	/* Figure out if any bits went high or low since last message */
488c2ecf20Sopenharmony_ci	changed = tk->cur_key ^ key;
498c2ecf20Sopenharmony_ci	for_each_set_bit(i, &changed, num_bits) {
508c2ecf20Sopenharmony_ci		dev_dbg(tk->dev, "key %d is %s\n", i,
518c2ecf20Sopenharmony_ci			test_bit(i, &key) ? "down" : "up");
528c2ecf20Sopenharmony_ci		input_report_key(tk->input, tk->codes[i], test_bit(i, &key));
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* Store currently down keys */
568c2ecf20Sopenharmony_ci	tk->cur_key = key;
578c2ecf20Sopenharmony_ci	input_sync(tk->input);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int dir685_tk_probe(struct i2c_client *client,
638c2ecf20Sopenharmony_ci			   const struct i2c_device_id *id)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct dir685_touchkeys *tk;
668c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
678c2ecf20Sopenharmony_ci	u8 bl_data[] = { 0xa7, 0x40 };
688c2ecf20Sopenharmony_ci	int err;
698c2ecf20Sopenharmony_ci	int i;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	tk = devm_kzalloc(&client->dev, sizeof(*tk), GFP_KERNEL);
728c2ecf20Sopenharmony_ci	if (!tk)
738c2ecf20Sopenharmony_ci		return -ENOMEM;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	tk->input = devm_input_allocate_device(dev);
768c2ecf20Sopenharmony_ci	if (!tk->input)
778c2ecf20Sopenharmony_ci		return -ENOMEM;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	tk->client = client;
808c2ecf20Sopenharmony_ci	tk->dev = dev;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	tk->input->keycodesize = sizeof(u16);
838c2ecf20Sopenharmony_ci	tk->input->keycodemax = ARRAY_SIZE(tk->codes);
848c2ecf20Sopenharmony_ci	tk->input->keycode = tk->codes;
858c2ecf20Sopenharmony_ci	tk->codes[0] = KEY_UP;
868c2ecf20Sopenharmony_ci	tk->codes[1] = KEY_DOWN;
878c2ecf20Sopenharmony_ci	tk->codes[2] = KEY_LEFT;
888c2ecf20Sopenharmony_ci	tk->codes[3] = KEY_RIGHT;
898c2ecf20Sopenharmony_ci	tk->codes[4] = KEY_ENTER;
908c2ecf20Sopenharmony_ci	tk->codes[5] = KEY_WPS_BUTTON;
918c2ecf20Sopenharmony_ci	/*
928c2ecf20Sopenharmony_ci	 * This key appears in the vendor driver, but I have
938c2ecf20Sopenharmony_ci	 * not been able to activate it.
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ci	tk->codes[6] = KEY_RESERVED;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	__set_bit(EV_KEY, tk->input->evbit);
988c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tk->codes); i++)
998c2ecf20Sopenharmony_ci		__set_bit(tk->codes[i], tk->input->keybit);
1008c2ecf20Sopenharmony_ci	__clear_bit(KEY_RESERVED, tk->input->keybit);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	tk->input->name = "D-Link DIR-685 touchkeys";
1038c2ecf20Sopenharmony_ci	tk->input->id.bustype = BUS_I2C;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	err = input_register_device(tk->input);
1068c2ecf20Sopenharmony_ci	if (err)
1078c2ecf20Sopenharmony_ci		return err;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* Set the brightness to max level */
1108c2ecf20Sopenharmony_ci	err = i2c_master_send(client, bl_data, sizeof(bl_data));
1118c2ecf20Sopenharmony_ci	if (err != sizeof(bl_data))
1128c2ecf20Sopenharmony_ci		dev_warn(tk->dev, "error setting brightness level\n");
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if (!client->irq) {
1158c2ecf20Sopenharmony_ci		dev_err(dev, "no IRQ on the I2C device\n");
1168c2ecf20Sopenharmony_ci		return -ENODEV;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	err = devm_request_threaded_irq(dev, client->irq,
1198c2ecf20Sopenharmony_ci					NULL, dir685_tk_irq_thread,
1208c2ecf20Sopenharmony_ci					IRQF_ONESHOT,
1218c2ecf20Sopenharmony_ci					"dir685-tk", tk);
1228c2ecf20Sopenharmony_ci	if (err) {
1238c2ecf20Sopenharmony_ci		dev_err(dev, "can't request IRQ\n");
1248c2ecf20Sopenharmony_ci		return err;
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return 0;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic const struct i2c_device_id dir685_tk_id[] = {
1318c2ecf20Sopenharmony_ci	{ "dir685tk", 0 },
1328c2ecf20Sopenharmony_ci	{ }
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, dir685_tk_id);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
1378c2ecf20Sopenharmony_cistatic const struct of_device_id dir685_tk_of_match[] = {
1388c2ecf20Sopenharmony_ci	{ .compatible = "dlink,dir685-touchkeys" },
1398c2ecf20Sopenharmony_ci	{},
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, dir685_tk_of_match);
1428c2ecf20Sopenharmony_ci#endif
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic struct i2c_driver dir685_tk_i2c_driver = {
1458c2ecf20Sopenharmony_ci	.driver = {
1468c2ecf20Sopenharmony_ci		.name	= "dlink-dir685-touchkeys",
1478c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(dir685_tk_of_match),
1488c2ecf20Sopenharmony_ci	},
1498c2ecf20Sopenharmony_ci	.probe		= dir685_tk_probe,
1508c2ecf20Sopenharmony_ci	.id_table	= dir685_tk_id,
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_cimodule_i2c_driver(dir685_tk_i2c_driver);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
1558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("D-Link DIR-685 touchkeys driver");
1568c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
157