diff mbox series

[v7,2/2] input: add driver for Hynitron CST816X touchscreen

Message ID 20240912132823.123409-2-kuzhylol@gmail.com (mailing list archive)
State New
Headers show
Series [v7,1/2] dt-bindings: input: touchscreen: add Hynitron CST816X | expand

Commit Message

Oleh Kuzhylnyi Sept. 12, 2024, 1:28 p.m. UTC
Introduce support for the Hynitron CST816X touchscreen controller
used for 240×240 1.28-inch Round LCD Display Module manufactured
by Waveshare Electronics. The driver is designed based on an Arduino
implementation marked as under MIT License. This driver is written
for a particular round display based on the CST816S controller, which
is not compatiable with existing driver for Hynitron controllers.

Signed-off-by: Oleh Kuzhylnyi <kuzhylol@gmail.com>
---

Changes in v7:
 - Update commit based on Jeffs's feedback:
 - Move event_map table to Device Tree
 - Implement DT parsing functionality
 - Use "int" type for iterators
 - Get rid of "*dev" entry from private struct
 - Use touch_info directly as a buffer for I2C
 - Fix coding style tweaks

Changes in v6:
 - No code changes

Changes in v5:
 - Update commit based on Dmitry's feedback:
 - Make GPIO reset optional
 - Combine declaration and initialization for i2c_xfer
 - Return 0 explicitly where possible
 - Rename rc (return code) to error
 - Make Touch processing call return boolean
 - Improve error handling for i2c_transfer
 - Use get_unaligned_be16 for getting coordinates
 - Move touch event completeness upper to irq callback

Changes in v4:
 - Update commit based on Dmitry's feedback:
 - Move abs_x and abs_y to u16
 - Remove __packed qualifier for touch_info struct
 - Hide tiny touch irq context to stack
 - Extend cst816x_i2c_read_register() with buf and buf_size
 - Remove loop from event lookup

Changes in v3:
 - Drop timer and delayed work
 - Process touch in threaded IRQ context
 - Fix chip reset sequence
 - Move input_register() before devm_request_threaded_irq()
 - Re-arrange and document input reporting
 - Set u16 data type for event_code
 - Remove double tap event to prevent continuous double side sliding

Changes in v2:
 - Apply dev_err_probe() for better error handling
 - Remove redundant printing, remove dev_warn() message spamming
 - Get rid of PM since the touchscreen goes into sleep mode automatically
 - Get rid of IRQ control and IRQF_NO_AUTOEN flag
 - Reduce timer timeout up to 10ms to handle touch events faster
 - Skip registering of non-gesture CST816X_SWIPE event
 - Shift input_register_device() as a final call in probe() callback
 - Specify name of i2c_device_id explicitly
 - Update module description and fix typo
 - Add necessary spaces between lines

 drivers/input/touchscreen/Kconfig            |  12 +
 drivers/input/touchscreen/Makefile           |   1 +
 drivers/input/touchscreen/hynitron-cst816x.c | 227 +++++++++++++++++++
 3 files changed, 240 insertions(+)
 create mode 100644 drivers/input/touchscreen/hynitron-cst816x.c

Comments

kernel test robot Sept. 14, 2024, 10:59 a.m. UTC | #1
Hi Oleh,

kernel test robot noticed the following build warnings:

[auto build test WARNING on dtor-input/next]
[also build test WARNING on dtor-input/for-linus robh/for-next krzk-dt/for-next linus/master v6.11-rc7 next-20240913]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oleh-Kuzhylnyi/input-add-driver-for-Hynitron-CST816X-touchscreen/20240912-213044
base:   https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git next
patch link:    https://lore.kernel.org/r/20240912132823.123409-2-kuzhylol%40gmail.com
patch subject: [PATCH v7 2/2] input: add driver for Hynitron CST816X touchscreen
config: sparc64-randconfig-r133-20240913 (https://download.01.org/0day-ci/archive/20240914/202409141849.QpkMdWlC-lkp@intel.com/config)
compiler: sparc64-linux-gcc (GCC) 14.1.0
reproduce: (https://download.01.org/0day-ci/archive/20240914/202409141849.QpkMdWlC-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409141849.QpkMdWlC-lkp@intel.com/

sparse warnings: (new ones prefixed by >>)
>> drivers/input/touchscreen/hynitron-cst816x.c:100:21: sparse: sparse: incorrect type in assignment (different base types) @@     expected restricted __be16 [usertype] abs_x @@     got unsigned long @@
   drivers/input/touchscreen/hynitron-cst816x.c:100:21: sparse:     expected restricted __be16 [usertype] abs_x
   drivers/input/touchscreen/hynitron-cst816x.c:100:21: sparse:     got unsigned long
>> drivers/input/touchscreen/hynitron-cst816x.c:101:21: sparse: sparse: incorrect type in assignment (different base types) @@     expected restricted __be16 [usertype] abs_y @@     got unsigned long @@
   drivers/input/touchscreen/hynitron-cst816x.c:101:21: sparse:     expected restricted __be16 [usertype] abs_y
   drivers/input/touchscreen/hynitron-cst816x.c:101:21: sparse:     got unsigned long
>> drivers/input/touchscreen/hynitron-cst816x.c:147:58: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected int value @@     got restricted __be16 [addressable] [usertype] abs_x @@
   drivers/input/touchscreen/hynitron-cst816x.c:147:58: sparse:     expected int value
   drivers/input/touchscreen/hynitron-cst816x.c:147:58: sparse:     got restricted __be16 [addressable] [usertype] abs_x
>> drivers/input/touchscreen/hynitron-cst816x.c:148:58: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected int value @@     got restricted __be16 [addressable] [usertype] abs_y @@
   drivers/input/touchscreen/hynitron-cst816x.c:148:58: sparse:     expected int value
   drivers/input/touchscreen/hynitron-cst816x.c:148:58: sparse:     got restricted __be16 [addressable] [usertype] abs_y

vim +100 drivers/input/touchscreen/hynitron-cst816x.c

    93	
    94	static bool cst816x_process_touch(struct cst816x_priv *priv,
    95					  struct cst816x_touch_info *info)
    96	{
    97		if (cst816x_i2c_read_register(priv, CST816X_FRAME, info, sizeof(*info)))
    98			return false;
    99	
 > 100		info->abs_x = get_unaligned_be16(&info->abs_x) & GENMASK(11, 0);
 > 101		info->abs_y = get_unaligned_be16(&info->abs_y) & GENMASK(11, 0);
   102	
   103		dev_dbg(&priv->client->dev, "x: %d, y: %d, t: %d, g: 0x%x\n",
   104			info->abs_x, info->abs_y, info->touch, info->gesture);
   105	
   106		return true;
   107	}
   108	
   109	static int cst816x_register_input(struct cst816x_priv *priv)
   110	{
   111		priv->input = devm_input_allocate_device(&priv->client->dev);
   112		if (!priv->input)
   113			return -ENOMEM;
   114	
   115		priv->input->name = "Hynitron CST816X Touchscreen";
   116		priv->input->phys = "input/ts";
   117		priv->input->id.bustype = BUS_I2C;
   118		input_set_drvdata(priv->input, priv);
   119	
   120		for (int i = 0; i < ARRAY_SIZE(priv->event_map); i++)
   121			input_set_capability(priv->input, EV_KEY,
   122					     priv->event_map[i].code);
   123	
   124		input_set_abs_params(priv->input, ABS_X, 0, 240, 0, 0);
   125		input_set_abs_params(priv->input, ABS_Y, 0, 240, 0, 0);
   126	
   127		return input_register_device(priv->input);
   128	}
   129	
   130	static void cst816x_reset(struct cst816x_priv *priv)
   131	{
   132		gpiod_set_value_cansleep(priv->reset, 1);
   133		msleep(50);
   134		gpiod_set_value_cansleep(priv->reset, 0);
   135		msleep(100);
   136	}
   137	
   138	static irqreturn_t cst816x_irq_cb(int irq, void *cookie)
   139	{
   140		struct cst816x_priv *priv = cookie;
   141		struct cst816x_touch_info info;
   142	
   143		if (!cst816x_process_touch(priv, &info))
   144			return IRQ_HANDLED;
   145	
   146		if (info.touch) {
 > 147			input_report_abs(priv->input, ABS_X, info.abs_x);
 > 148			input_report_abs(priv->input, ABS_Y, info.abs_y);
   149			input_report_key(priv->input, BTN_TOUCH, 1);
   150		}
   151	
   152		if (info.gesture) {
   153			input_report_key(priv->input,
   154					 priv->event_map[info.gesture & 0x0F].code,
   155					 info.touch);
   156	
   157			if (!info.touch)
   158				input_report_key(priv->input, BTN_TOUCH, 0);
   159		}
   160	
   161		input_sync(priv->input);
   162	
   163		return IRQ_HANDLED;
   164	}
   165
diff mbox series

Patch

diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index c821fe3ee794..02f40d0fbac0 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -481,6 +481,18 @@  config TOUCHSCREEN_HYNITRON_CSTXXX
 	  To compile this driver as a module, choose M here: the
 	  module will be called hynitron-cstxxx.
 
+config TOUCHSCREEN_HYNITRON_CST816X
+	tristate "Hynitron CST816X touchscreen support"
+	depends on I2C
+	help
+	  Say Y here if you have a touchscreen using a Hynitron
+	  CST816X touchscreen controller.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called hynitron-cst816x.
+
 config TOUCHSCREEN_ILI210X
 	tristate "Ilitek ILI210X based touchscreen"
 	depends on I2C
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
index a81cb5aa21a5..a92a87417a97 100644
--- a/drivers/input/touchscreen/Makefile
+++ b/drivers/input/touchscreen/Makefile
@@ -51,6 +51,7 @@  obj-$(CONFIG_TOUCHSCREEN_GOODIX_BERLIN_CORE)	+= goodix_berlin_core.o
 obj-$(CONFIG_TOUCHSCREEN_GOODIX_BERLIN_I2C)	+= goodix_berlin_i2c.o
 obj-$(CONFIG_TOUCHSCREEN_GOODIX_BERLIN_SPI)	+= goodix_berlin_spi.o
 obj-$(CONFIG_TOUCHSCREEN_HIDEEP)	+= hideep.o
+obj-$(CONFIG_TOUCHSCREEN_HYNITRON_CST816X)	+= hynitron-cst816x.o
 obj-$(CONFIG_TOUCHSCREEN_HYNITRON_CSTXXX)	+= hynitron_cstxxx.o
 obj-$(CONFIG_TOUCHSCREEN_ILI210X)	+= ili210x.o
 obj-$(CONFIG_TOUCHSCREEN_ILITEK)	+= ilitek_ts_i2c.o
diff --git a/drivers/input/touchscreen/hynitron-cst816x.c b/drivers/input/touchscreen/hynitron-cst816x.c
new file mode 100644
index 000000000000..8c5faaa2e1d1
--- /dev/null
+++ b/drivers/input/touchscreen/hynitron-cst816x.c
@@ -0,0 +1,227 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Driver for I2C connected Hynitron CST816X Touchscreen
+ *
+ * Copyright (C) 2024 Oleh Kuzhylnyi <kuzhylol@gmail.com>
+ */
+#include <asm/unaligned.h>
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+
+#define CST816X_FRAME 0x01
+
+struct cst816x_touch_info {
+	u8 gesture;
+	u8 touch;
+	__be16 abs_x;
+	__be16 abs_y;
+} __packed;
+
+struct cst816x_event_map {
+	u8 gesture;
+	u16 code;
+};
+
+struct cst816x_priv {
+	struct i2c_client *client;
+	struct gpio_desc *reset;
+	struct input_dev *input;
+	struct cst816x_event_map event_map[16];
+};
+
+static int cst816x_parse_dt_event_map(struct device *dev,
+				      struct cst816x_priv *priv)
+{
+	struct device_node *np = dev->of_node;
+	struct device_node *gst, *child;
+	u32 gesture, code;
+	u8 index;
+
+	gst = of_get_child_by_name(np, "gestures");
+	if (!gst)
+		return -ENOENT;
+
+	for_each_child_of_node(gst, child) {
+		if (of_property_read_u32(child, "cst816x,gesture", &gesture))
+			continue;
+
+		if (of_property_read_u32(child, "linux,code", &code))
+			continue;
+
+		index = gesture & 0x0F;
+
+		priv->event_map[index].gesture = gesture;
+		priv->event_map[index].code = code;
+	}
+
+	return 0;
+}
+
+static int cst816x_i2c_read_register(struct cst816x_priv *priv, u8 reg,
+				     void *buf, size_t len)
+{
+	int rc;
+	struct i2c_msg xfer[] = {
+		{
+			.addr = priv->client->addr,
+			.flags = 0,
+			.buf = &reg,
+			.len = sizeof(reg),
+		},
+		{
+			.addr = priv->client->addr,
+			.flags = I2C_M_RD,
+			.buf = buf,
+			.len = len,
+		},
+	};
+
+	rc = i2c_transfer(priv->client->adapter, xfer, ARRAY_SIZE(xfer));
+	if (rc != ARRAY_SIZE(xfer)) {
+		rc = rc < 0 ? rc : -EIO;
+		dev_err(&priv->client->dev, "i2c rx err: %d\n", rc);
+		return rc;
+	}
+
+	return 0;
+}
+
+static bool cst816x_process_touch(struct cst816x_priv *priv,
+				  struct cst816x_touch_info *info)
+{
+	if (cst816x_i2c_read_register(priv, CST816X_FRAME, info, sizeof(*info)))
+		return false;
+
+	info->abs_x = get_unaligned_be16(&info->abs_x) & GENMASK(11, 0);
+	info->abs_y = get_unaligned_be16(&info->abs_y) & GENMASK(11, 0);
+
+	dev_dbg(&priv->client->dev, "x: %d, y: %d, t: %d, g: 0x%x\n",
+		info->abs_x, info->abs_y, info->touch, info->gesture);
+
+	return true;
+}
+
+static int cst816x_register_input(struct cst816x_priv *priv)
+{
+	priv->input = devm_input_allocate_device(&priv->client->dev);
+	if (!priv->input)
+		return -ENOMEM;
+
+	priv->input->name = "Hynitron CST816X Touchscreen";
+	priv->input->phys = "input/ts";
+	priv->input->id.bustype = BUS_I2C;
+	input_set_drvdata(priv->input, priv);
+
+	for (int i = 0; i < ARRAY_SIZE(priv->event_map); i++)
+		input_set_capability(priv->input, EV_KEY,
+				     priv->event_map[i].code);
+
+	input_set_abs_params(priv->input, ABS_X, 0, 240, 0, 0);
+	input_set_abs_params(priv->input, ABS_Y, 0, 240, 0, 0);
+
+	return input_register_device(priv->input);
+}
+
+static void cst816x_reset(struct cst816x_priv *priv)
+{
+	gpiod_set_value_cansleep(priv->reset, 1);
+	msleep(50);
+	gpiod_set_value_cansleep(priv->reset, 0);
+	msleep(100);
+}
+
+static irqreturn_t cst816x_irq_cb(int irq, void *cookie)
+{
+	struct cst816x_priv *priv = cookie;
+	struct cst816x_touch_info info;
+
+	if (!cst816x_process_touch(priv, &info))
+		return IRQ_HANDLED;
+
+	if (info.touch) {
+		input_report_abs(priv->input, ABS_X, info.abs_x);
+		input_report_abs(priv->input, ABS_Y, info.abs_y);
+		input_report_key(priv->input, BTN_TOUCH, 1);
+	}
+
+	if (info.gesture) {
+		input_report_key(priv->input,
+				 priv->event_map[info.gesture & 0x0F].code,
+				 info.touch);
+
+		if (!info.touch)
+			input_report_key(priv->input, BTN_TOUCH, 0);
+	}
+
+	input_sync(priv->input);
+
+	return IRQ_HANDLED;
+}
+
+static int cst816x_probe(struct i2c_client *client)
+{
+	struct cst816x_priv *priv;
+	struct device *dev = &client->dev;
+	int error;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->client = client;
+
+	priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
+	if (IS_ERR(priv->reset))
+		return dev_err_probe(dev, PTR_ERR(priv->reset),
+				     "gpio reset request failed\n");
+
+	if (priv->reset)
+		cst816x_reset(priv);
+
+	error = cst816x_parse_dt_event_map(dev, priv);
+	if (error)
+		dev_warn(dev, "no gestures specified in dt\n");
+
+	error = cst816x_register_input(priv);
+	if (error)
+		return dev_err_probe(dev, error, "input register failed\n");
+
+	error = devm_request_threaded_irq(dev, client->irq, NULL, cst816x_irq_cb,
+					  IRQF_ONESHOT, dev->driver->name, priv);
+	if (error)
+		return dev_err_probe(dev, error, "irq request failed\n");
+
+	return 0;
+}
+
+static const struct i2c_device_id cst816x_id[] = {
+	{ .name = "cst816s", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, cst816x_id);
+
+static const struct of_device_id cst816x_of_match[] = {
+	{ .compatible = "hynitron,cst816s", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, cst816x_of_match);
+
+static struct i2c_driver cst816x_driver = {
+	.driver = {
+		.name = "cst816x",
+		.of_match_table = cst816x_of_match,
+	},
+	.id_table = cst816x_id,
+	.probe = cst816x_probe,
+};
+
+module_i2c_driver(cst816x_driver);
+
+MODULE_AUTHOR("Oleh Kuzhylnyi <kuzhylol@gmail.com>");
+MODULE_DESCRIPTION("Hynitron CST816X Touchscreen Driver");
+MODULE_LICENSE("GPL");