[PATCH v5 5/7] Input: novatek-nvt-ts - add NT36536 packet decoding

From: Qianhui Xu

Date: Wed Dec 02 2026 - 04:10:41 EST


NT36xxx controllers differ from the NT11205 in the touch packet layout:
the report starts with the touch records and the checksum of their first
64 bytes follows at offset 64, coordinates are 16 bit big endian and the
fifth byte of a record holds the contact width. They also have no
parameter block, so the geometry comes from the device tree and the
number of slots from the chip data.

Add a per chip report callback and move the NT11205 decoding behind it, add
the NT36xxx decoding with its packet checksum, and make the parameter block
read conditional on the chip data advertising a chip id.

The NT36536 itself is a no-flash part and needs its firmware downloaded by
the host before it reports touches; that is a separate patch, so this one
only adds the SPI match entries and the binding compatible.

Signed-off-by: Qianhui Xu <xiaqinghuan@xxxxxxxxxxxxxxxx>
---
Changes in v4:
- only register ABS_MT_TOUCH_MAJOR for controllers whose packets carry the
contact width; the NT11205 and NT36672A do not report one.
- the NT36xxx report is 108 bytes, the first 64 bytes are summed up by the
byte at offset 64; read that much instead of 109 bytes.

Changes in v3:
- include <linux/bitfield.h> for FIELD_GET(); it is currently only reachable
through architecture headers, so the file does not build on x86_64.
- decode the touch records from the start of the report and checksum its
first 64 bytes: the first byte of a register read is the first report
byte, so the previous offsets shifted every record by one.
- set up ABS_MT_TOUCH_MAJOR, otherwise the input core drops the width that
the NT36xxx decoder reports.
- for controllers without a parameter block, set the X and Y capabilities
before touchscreen_parse_properties() so that the device tree geometry is
applied to them, and reject a device tree that carries none.
- keep EXPORT_SYMBOL_GPL(nvt_nt36672a_ts_data) next to its definition.

*** AI-assistance disclosure ***
This patch was prepared with the help of an LLM coding assistant. The
author reviewed every line, checked every quoted claim against the tree,
and ran the gates quoted in the cover letter; the design decisions and
any errors are the author's.

.../input/touchscreen/novatek-nvt-ts-core.c | 192 +++++++++++++++---
.../input/touchscreen/novatek-nvt-ts-i2c.c | 2 +-
.../input/touchscreen/novatek-nvt-ts-spi.c | 4 +-
drivers/input/touchscreen/novatek-nvt-ts.h | 30 ++-
4 files changed, 189 insertions(+), 39 deletions(-)

diff --git a/drivers/input/touchscreen/novatek-nvt-ts-core.c b/drivers/input/touchscreen/novatek-nvt-ts-core.c
index d0e2c8bf22157..89e7f1eaf39c9 100644
--- a/drivers/input/touchscreen/novatek-nvt-ts-core.c
+++ b/drivers/input/touchscreen/novatek-nvt-ts-core.c
@@ -9,8 +9,10 @@
* Copyright (c) 2023 Hans de Goede <hdegoede@xxxxxxxxxx>
*/

+#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
+#include <linux/irq.h>
#include <linux/input/mt.h>
#include <linux/module.h>
#include <linux/property.h>
@@ -31,9 +33,12 @@ static int nvt_ts_read_data(struct nvt_ts_data *data, u8 reg, u8 *buf, size_t co
return data->bus_ops->read(data, reg, buf, count);
}

-static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
+/*
+ * NT11205/NT36672A packet: one record per touch slot, 6 bytes each, 12 bit
+ * coordinates packed as x = b[1] << 4 | b[3] >> 4, y = b[2] << 4 | b[3] & 0xf.
+ */
+static int nvt_ts_report_nt11205(struct nvt_ts_data *data)
{
- struct nvt_ts_data *data = dev_id;
struct device *dev = data->dev;
int i, error, slot, x, y;
bool active;
@@ -42,7 +47,7 @@ static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
error = nvt_ts_read_data(data, NVT_TS_TOUCH_START, data->buf,
data->max_touches * NVT_TS_TOUCH_SIZE);
if (error)
- return IRQ_HANDLED;
+ return error;

for (i = 0; i < data->max_touches; i++) {
touch = &data->buf[i * NVT_TS_TOUCH_SIZE];
@@ -81,6 +86,77 @@ static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
input_mt_sync_frame(data->input);
input_sync(data->input);

+ return 0;
+}
+
+/*
+ * NT36xxx checksum: the byte at NVT_TS_NT36XXX_CHECKSUM is the two's
+ * complement of the sum of the first 64 bytes of the report.
+ */
+static bool nvt_ts_nt36xxx_checksum_ok(const u8 *buf)
+{
+ u8 sum = 0;
+ int i;
+
+ for (i = 0; i < NVT_TS_NT36XXX_CHECKSUM; i++)
+ sum += buf[i];
+
+ return (u8)(~sum + 1) == buf[NVT_TS_NT36XXX_CHECKSUM];
+}
+
+/*
+ * NT36xxx packet: 6 byte touch records at the start of the report with the
+ * checksum at offset 64, coordinates are 16 bit big endian and b[5] carries
+ * the width.
+ */
+static int nvt_ts_report_nt36xxx(struct nvt_ts_data *data)
+{
+ struct device *dev = data->dev;
+ int i, error;
+
+ error = nvt_ts_read_data(data, NVT_TS_TOUCH_START, data->buf,
+ NVT_TS_NT36XXX_PACKET_SIZE);
+ if (error)
+ return error;
+
+ if (!nvt_ts_nt36xxx_checksum_ok(data->buf)) {
+ dev_dbg(dev, "bad touch packet checksum\n");
+ return -EIO;
+ }
+
+ for (i = 0; i < data->max_touches; i++) {
+ const u8 *touch = &data->buf[i * NVT_TS_TOUCH_SIZE];
+ u8 state = FIELD_GET(NVT_TS_TOUCH_TYPE_MASK, touch[0]);
+ unsigned int id = (touch[0] >> NVT_TS_TOUCH_SLOT_SHIFT) - 1;
+ unsigned int x, y, width;
+
+ if (state != NVT_TS_TOUCH_NEW && state != NVT_TS_TOUCH_UPDATE)
+ continue;
+ if (id >= data->max_touches)
+ continue;
+
+ x = get_unaligned_be16(&touch[1]);
+ y = get_unaligned_be16(&touch[3]);
+ width = touch[5] ?: 1;
+
+ input_mt_slot(data->input, id);
+ input_mt_report_slot_state(data->input, MT_TOOL_FINGER, true);
+ touchscreen_report_pos(data->input, &data->prop, x, y, true);
+ input_report_abs(data->input, ABS_MT_TOUCH_MAJOR, width);
+ }
+
+ input_mt_sync_frame(data->input);
+ input_sync(data->input);
+
+ return 0;
+}
+
+static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
+{
+ struct nvt_ts_data *data = dev_id;
+
+ data->chip->report_touches(data);
+
return IRQ_HANDLED;
}

@@ -137,9 +213,10 @@ int nvt_ts_resume(struct device *dev)
EXPORT_SYMBOL_GPL(nvt_ts_resume);

int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,
- const struct nvt_ts_i2c_chip_data *chip, int irq,
+ const struct nvt_ts_chip_data *chip, int irq,
unsigned int bustype, const char *name)
{
+ unsigned long irq_flags;
int error, width, height, irq_type;
struct nvt_ts_data *data;
struct input_dev *input;
@@ -157,6 +234,7 @@ int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,

data->dev = dev;
data->bus_ops = bus_ops;
+ data->chip = chip;
data->irq = irq;

/*
@@ -191,33 +269,52 @@ int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,

/* Wait for controller to come out of reset before params read */
msleep(100);
- error = nvt_ts_read_data(data, NVT_TS_PARAMETERS_START,
- data->buf, NVT_TS_PARAMS_SIZE);
+
+ width = 0;
+ height = 0;
+ irq_type = -1;
+
+ if (chip->chip_id) {
+ /*
+ * NT11205/NT36672A describe themselves: geometry, slot count
+ * and interrupt type all come from the parameter block.
+ */
+ error = nvt_ts_read_data(data, NVT_TS_PARAMETERS_START,
+ data->buf, NVT_TS_PARAMS_SIZE);
+ if (error)
+ goto out_power_down;
+
+ width = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_WIDTH]);
+ height = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_HEIGHT]);
+ data->max_touches = data->buf[NVT_TS_PARAMS_MAX_TOUCH];
+ irq_type = data->buf[NVT_TS_PARAMS_IRQ_TYPE];
+
+ if (width > NVT_TS_MAX_SIZE || height >= NVT_TS_MAX_SIZE ||
+ data->max_touches > NVT_TS_MAX_TOUCHES ||
+ irq_type >= ARRAY_SIZE(nvt_ts_irq_type) ||
+ data->buf[NVT_TS_PARAMS_CHIP_ID] != chip->chip_id) {
+ dev_err(dev, "Unsupported touchscreen parameters: %*ph\n",
+ NVT_TS_PARAMS_SIZE, data->buf);
+ error = -EIO;
+ goto out_power_down;
+ }
+
+ dev_dbg(dev, "Detected %dx%d touchscreen with %d max touches\n",
+ width, height, data->max_touches);
+
+ if (data->buf[NVT_TS_PARAMS_MAX_BUTTONS])
+ dev_warn(dev, "Touchscreen buttons are not supported\n");
+ } else {
+ /* No parameter block: geometry comes from DT, slots from chip data */
+ data->max_touches = chip->max_touches;
+ }
+
+out_power_down:
gpiod_set_value_cansleep(data->reset_gpio, 1); /* Put back in reset */
regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
if (error)
return error;

- width = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_WIDTH]);
- height = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_HEIGHT]);
- data->max_touches = data->buf[NVT_TS_PARAMS_MAX_TOUCH];
- irq_type = data->buf[NVT_TS_PARAMS_IRQ_TYPE];
-
- if (width > NVT_TS_MAX_SIZE || height >= NVT_TS_MAX_SIZE ||
- data->max_touches > NVT_TS_MAX_TOUCHES ||
- irq_type >= ARRAY_SIZE(nvt_ts_irq_type) ||
- data->buf[NVT_TS_PARAMS_CHIP_ID] != chip->chip_id) {
- dev_err(dev, "Unsupported touchscreen parameters: %*ph\n",
- NVT_TS_PARAMS_SIZE, data->buf);
- return -EIO;
- }
-
- dev_dbg(dev, "Detected %dx%d touchscreen with %d max touches\n",
- width, height, data->max_touches);
-
- if (data->buf[NVT_TS_PARAMS_MAX_BUTTONS])
- dev_warn(dev, "Touchscreen buttons are not supported\n");
-
input = devm_input_allocate_device(dev);
if (!input)
return -ENOMEM;
@@ -227,9 +324,23 @@ int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,
input->open = nvt_ts_start;
input->close = nvt_ts_stop;

- input_set_abs_params(input, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
- input_set_abs_params(input, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
+ if (width)
+ input_set_abs_params(input, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
+ else
+ input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
+ if (height)
+ input_set_abs_params(input, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
+ else
+ input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
+ if (chip->reports_width)
+ input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
+
touchscreen_parse_properties(input, true, &data->prop);
+ if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
+ !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
+ dev_err(dev, "touchscreen-size-x and/or -y missing\n");
+ return -EINVAL;
+ }

error = input_mt_init_slots(input, data->max_touches,
INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
@@ -239,9 +350,13 @@ int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,
data->input = input;
input_set_drvdata(input, data);

+ if (irq_type >= 0)
+ irq_flags = nvt_ts_irq_type[irq_type];
+ else
+ irq_flags = irq_get_trigger_type(data->irq) ?: IRQF_TRIGGER_FALLING;
+
error = devm_request_threaded_irq(dev, data->irq, NULL, nvt_ts_irq,
- IRQF_ONESHOT | IRQF_NO_AUTOEN |
- nvt_ts_irq_type[irq_type],
+ IRQF_ONESHOT | IRQF_NO_AUTOEN | irq_flags,
name, data);
if (error) {
dev_err(dev, "failed to request irq: %d\n", error);
@@ -258,16 +373,29 @@ int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,
}
EXPORT_SYMBOL_GPL(nvt_ts_probe);

-const struct nvt_ts_i2c_chip_data nvt_nt11205_ts_data = {
+const struct nvt_ts_chip_data nvt_nt11205_ts_data = {
.chip_id = 0x05,
+ .report_touches = nvt_ts_report_nt11205,
};
EXPORT_SYMBOL_GPL(nvt_nt11205_ts_data);

-const struct nvt_ts_i2c_chip_data nvt_nt36672a_ts_data = {
+const struct nvt_ts_chip_data nvt_nt36672a_ts_data = {
.chip_id = 0x08,
+ .report_touches = nvt_ts_report_nt11205,
};
EXPORT_SYMBOL_GPL(nvt_nt36672a_ts_data);

+/*
+ * NT36xxx parts have no parameter block and (in the no-flash variants) need
+ * their firmware downloaded by the host, so max_touches comes from here.
+ */
+const struct nvt_ts_chip_data nvt_nt36536_ts_data = {
+ .max_touches = NVT_TS_MAX_TOUCHES,
+ .reports_width = true,
+ .report_touches = nvt_ts_report_nt36xxx,
+};
+EXPORT_SYMBOL_GPL(nvt_nt36536_ts_data);
+
MODULE_DESCRIPTION("Novatek NVT touchscreen core driver");
MODULE_AUTHOR("Hans de Goede <hdegoede@xxxxxxxxxx>");
MODULE_LICENSE("GPL");
diff --git a/drivers/input/touchscreen/novatek-nvt-ts-i2c.c b/drivers/input/touchscreen/novatek-nvt-ts-i2c.c
index 8991a0d531a45..1dec7bbba7cec 100644
--- a/drivers/input/touchscreen/novatek-nvt-ts-i2c.c
+++ b/drivers/input/touchscreen/novatek-nvt-ts-i2c.c
@@ -45,7 +45,7 @@ static const struct nvt_ts_bus_ops nvt_ts_i2c_bus_ops = {

static int nvt_ts_i2c_probe(struct i2c_client *client)
{
- const struct nvt_ts_i2c_chip_data *chip = i2c_get_match_data(client);
+ const struct nvt_ts_chip_data *chip = i2c_get_match_data(client);

if (!chip)
return -EINVAL;
diff --git a/drivers/input/touchscreen/novatek-nvt-ts-spi.c b/drivers/input/touchscreen/novatek-nvt-ts-spi.c
index 88befe95c1711..8dfe2b688181a 100644
--- a/drivers/input/touchscreen/novatek-nvt-ts-spi.c
+++ b/drivers/input/touchscreen/novatek-nvt-ts-spi.c
@@ -54,7 +54,7 @@ static const struct nvt_ts_bus_ops nvt_ts_spi_bus_ops = {

static int nvt_ts_spi_probe(struct spi_device *spi)
{
- const struct nvt_ts_i2c_chip_data *chip = spi_get_device_match_data(spi);
+ const struct nvt_ts_chip_data *chip = spi_get_device_match_data(spi);

if (!chip)
return -EINVAL;
@@ -65,6 +65,7 @@ static int nvt_ts_spi_probe(struct spi_device *spi)

static const struct of_device_id nvt_ts_spi_of_match[] = {
{ .compatible = "novatek,nt11205-ts", .data = &nvt_nt11205_ts_data },
+ { .compatible = "novatek,nt36536-ts", .data = &nvt_nt36536_ts_data },
{ .compatible = "novatek,nt36672a-ts", .data = &nvt_nt36672a_ts_data },
{ }
};
@@ -72,6 +73,7 @@ MODULE_DEVICE_TABLE(of, nvt_ts_spi_of_match);

static const struct spi_device_id nvt_ts_spi_id[] = {
{ .name = "nt11205-ts", .driver_data = (unsigned long)&nvt_nt11205_ts_data },
+ { .name = "nt36536-ts", .driver_data = (unsigned long)&nvt_nt36536_ts_data },
{ .name = "nt36672a-ts", .driver_data = (unsigned long)&nvt_nt36672a_ts_data },
{ }
};
diff --git a/drivers/input/touchscreen/novatek-nvt-ts.h b/drivers/input/touchscreen/novatek-nvt-ts.h
index 096f7ade4f513..7e2b207acb947 100644
--- a/drivers/input/touchscreen/novatek-nvt-ts.h
+++ b/drivers/input/touchscreen/novatek-nvt-ts.h
@@ -32,6 +32,13 @@
#define NVT_TS_MAX_TOUCHES 10
#define NVT_TS_MAX_SIZE 4096

+/* NT36xxx touch report: 108 bytes, checksum at 64 covers the first 64 */
+#define NVT_TS_NT36XXX_PACKET_SIZE 108
+#define NVT_TS_NT36XXX_CHECKSUM 64
+
+/* Large enough for every packet this driver reads */
+#define NVT_TS_MAX_PACKET NVT_TS_NT36XXX_PACKET_SIZE
+
#define NVT_TS_TOUCH_INVALID 0xff
#define NVT_TS_TOUCH_SLOT_SHIFT 3
#define NVT_TS_TOUCH_TYPE_MASK GENMASK(2, 0)
@@ -52,30 +59,43 @@ struct nvt_ts_bus_ops {
int (*read)(struct nvt_ts_data *data, u8 reg, u8 *buf, size_t count);
};

-struct nvt_ts_i2c_chip_data {
+/**
+ * struct nvt_ts_chip_data - per controller variant data
+ * @chip_id: chip id expected in the parameter block, 0 when the controller
+ * has no parameter block at all
+ * @max_touches: touch slots, used when there is no parameter block
+ * @reports_width: the packets of this controller carry the contact width
+ * @report_touches: decode one touch packet into input events
+ */
+struct nvt_ts_chip_data {
u8 chip_id;
+ unsigned int max_touches;
+ bool reports_width;
+ int (*report_touches)(struct nvt_ts_data *data);
};

struct nvt_ts_data {
struct device *dev;
const struct nvt_ts_bus_ops *bus_ops;
+ const struct nvt_ts_chip_data *chip;
struct input_dev *input;
int irq;
struct gpio_desc *reset_gpio;
struct regulator_bulk_data regulators[2];
struct touchscreen_properties prop;
int max_touches;
- u8 buf[NVT_TS_TOUCH_SIZE * NVT_TS_MAX_TOUCHES];
+ u8 buf[NVT_TS_MAX_PACKET];
};

int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,
- const struct nvt_ts_i2c_chip_data *chip, int irq,
+ const struct nvt_ts_chip_data *chip, int irq,
unsigned int bustype, const char *name);

int nvt_ts_suspend(struct device *dev);
int nvt_ts_resume(struct device *dev);

-extern const struct nvt_ts_i2c_chip_data nvt_nt11205_ts_data;
-extern const struct nvt_ts_i2c_chip_data nvt_nt36672a_ts_data;
+extern const struct nvt_ts_chip_data nvt_nt11205_ts_data;
+extern const struct nvt_ts_chip_data nvt_nt36672a_ts_data;
+extern const struct nvt_ts_chip_data nvt_nt36536_ts_data;

#endif /* _NOVATEK_NVT_TS_H */