Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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