[PATCH v5 7/7] Input: novatek-nvt-ts - add no-flash firmware download
From: Qianhui Xu
Date: Wed Dec 02 2026 - 04:11:28 EST
Controllers without on-board flash come out of reset with nothing but a
bootloader, so the host has to upload the image before they report touches.
The image is a container of up to 32 partitions, each with a file offset, a
SRAM address, a length and a CRC of the loaded data. Upload it whenever the
controller is powered up, as a no-flash part loses the firmware on power
down: identify the silicon, reset the bootloader, put the CRCs in the
checksum banks, push the partitions into SRAM in 15 KiB chunks, then assert
BOOT_RDY and wait for the reset state and the firmware info to come back.
firmware-name is optional and the image is not shipped with the kernel.
Without it the controller is used as is, which leaves a no-flash part quiet
but keeps the flash based ones working. The image seen so far is 253952
bytes in 4 partitions.
Signed-off-by: Qianhui Xu <xiaqinghuan@xxxxxxxxxxxxxxxx>
---
Changes in v5:
- define and initialise the upload buffer with __free(kfree) at the
allocation site.
Changes in v3:
- copy the command to a DMA safe buffer in the SPI write callback:
spi_write() maps the buffer for DMA instead of bouncing it, while the
upload helpers build their commands on the stack.
- track whether the controller is powered up, so that a firmware upload that
fails in nvt_ts_start() during resume does not make the next
nvt_ts_stop() disable the regulators and the interrupt a second time.
- fix the description of the cascade marker: it is the descriptor that
copies a non-zero length from the start of the file, not one with a zero
length. The search is unchanged, a zero length descriptor is simply never
added as a partition; the marker is what locates the second header of the
cascade image we have.
- release the upload buffer and the firmware with __free() instead of the
out_free label and release_firmware().
- report an unexpected trim ID, otherwise probing fails with -ENODEV and
no message at all.
- upload the last byte of every partition: the length field in the container
is the offset of that byte, not the number of bytes, so the payload is one
byte longer than the field says. Without it the boot loader never started
the automatic copy and the upload timed out on the hardware.
*** 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.yaml | 9 +-
drivers/input/touchscreen/Kconfig | 1 +
.../input/touchscreen/novatek-nvt-ts-core.c | 541 +++++++++++++++++-
.../input/touchscreen/novatek-nvt-ts-spi.c | 24 +
drivers/input/touchscreen/novatek-nvt-ts.h | 6 +
5 files changed, 579 insertions(+), 2 deletions(-)
diff --git a/Documentation/devicetree/bindings/input/touchscreen/novatek,nvt-ts.yaml b/Documentation/devicetree/bindings/input/touchscreen/novatek,nvt-ts.yaml
index 4b6b05397fee3..0911e780d16fd 100644
--- a/Documentation/devicetree/bindings/input/touchscreen/novatek,nvt-ts.yaml
+++ b/Documentation/devicetree/bindings/input/touchscreen/novatek,nvt-ts.yaml
@@ -32,6 +32,12 @@ properties:
vcc-supply: true
iovcc-supply: true
+ firmware-name:
+ description:
+ Firmware file uploaded by the host at open time. Needed by the
+ controllers that have no on-board flash, optional otherwise, and the
+ file itself is not distributed with the kernel.
+
required:
- compatible
- reg
@@ -69,13 +75,14 @@ examples:
#size-cells = <0>;
touchscreen@0 {
- compatible = "novatek,nt36672a-ts";
+ compatible = "novatek,nt36536-ts";
reg = <0>;
spi-max-frequency = <4000000>;
interrupts-extended = <&tlmm 31 IRQ_TYPE_EDGE_RISING>;
reset-gpios = <&tlmm 32 GPIO_ACTIVE_LOW>;
vcc-supply = <&vreg_l22a_2p85>;
iovcc-supply = <&vreg_l14a_1p8>;
+ firmware-name = "novatek/nt36536-fw.bin";
touchscreen-size-x = <1080>;
touchscreen-size-y = <2246>;
};
diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index 9a276729a1630..2b7866a1a4fc3 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -693,6 +693,7 @@ config TOUCHSCREEN_MTOUCH
config TOUCHSCREEN_NOVATEK_NVT_TS_CORE
tristate
+ select FW_LOADER
config TOUCHSCREEN_NOVATEK_NVT_TS
tristate "Novatek NT11205 touchscreen support"
diff --git a/drivers/input/touchscreen/novatek-nvt-ts-core.c b/drivers/input/touchscreen/novatek-nvt-ts-core.c
index 89e7f1eaf39c9..402cb195e3f01 100644
--- a/drivers/input/touchscreen/novatek-nvt-ts-core.c
+++ b/drivers/input/touchscreen/novatek-nvt-ts-core.c
@@ -11,11 +11,13 @@
#include <linux/bitfield.h>
#include <linux/delay.h>
+#include <linux/firmware.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/input/mt.h>
#include <linux/module.h>
#include <linux/property.h>
+#include <linux/slab.h>
#include <linux/unaligned.h>
@@ -33,6 +35,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 int nvt_ts_write_data(struct nvt_ts_data *data, const u8 *buf,
+ size_t count)
+{
+ return data->bus_ops->write(data, buf, count);
+}
+
/*
* 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.
@@ -160,6 +168,504 @@ static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
return IRQ_HANDLED;
}
+/*
+ * NT36xxx firmware download
+ *
+ * The no-flash parts come out of reset with nothing but a bootloader; the host
+ * has to upload the image into SRAM and start it. The image is a container of
+ * partitions, each with a file offset, a SRAM address, a length and a CRC of
+ * the data the bootloader ends up with.
+ */
+#define NVT_TS_FW_XFER_LEN 15360
+#define NVT_TS_FW_MAX_PARTITIONS 32
+#define NVT_TS_FW_END_FLAG_LEN 3
+#define NVT_TS_FW_PID 0x6097
+
+#define NVT_TS_FW_ENG_RESET_ADDR 0x7fff80
+#define NVT_TS_FW_TRIM_ADDR 0x1fb104
+#define NVT_TS_FW_BOOTLOADER_RST_ADDR 0x1fb43e
+#define NVT_TS_FW_BOOT_RDY_ADDR 0x1fb50d
+#define NVT_TS_FW_AUTO_COPY_ADDR 0x1fc825
+#define NVT_TS_FW_CASCADE_STRAP_ADDR 0x1fb12c
+#define NVT_TS_FW_ILM_DES_ADDR 0x1fcc30
+#define NVT_TS_FW_ILM_LENGTH_ADDR 0x1fcc34
+#define NVT_TS_FW_ILM_CRC_ADDR 0x1fcc38
+#define NVT_TS_FW_DLM_DES_ADDR 0x1fcc40
+#define NVT_TS_FW_DLM_LENGTH_ADDR 0x1fcc44
+#define NVT_TS_FW_DLM_CRC_ADDR 0x1fcc48
+#define NVT_TS_FW_EVENT_BUF_ADDR 0x130900
+
+#define NVT_TS_FW_EVENT_HOST_CMD 0x50
+#define NVT_TS_FW_EVENT_RESET_DONE 0x60
+#define NVT_TS_FW_EVENT_INFO 0x78
+#define NVT_TS_FW_RESET_STATE_MIN 0xa0
+#define NVT_TS_FW_RESET_STATE_MAX 0xaf
+
+struct nvt_ts_fw_partition {
+ u32 file_offs;
+ u32 sram_addr;
+ u32 size;
+ u32 crc;
+};
+
+static int nvt_ts_fw_set_page(struct nvt_ts_data *data, u32 addr)
+{
+ u8 buf[] = { 0xff, (addr >> 15) & 0xff, (addr >> 7) & 0xff };
+
+ return nvt_ts_write_data(data, buf, sizeof(buf));
+}
+
+static int nvt_ts_fw_write_byte(struct nvt_ts_data *data, u32 addr, u8 value)
+{
+ u8 buf[] = { (addr & 0x7f) | BIT(7), value };
+ int error;
+
+ error = nvt_ts_fw_set_page(data, addr);
+ if (error)
+ return error;
+
+ return nvt_ts_write_data(data, buf, sizeof(buf));
+}
+
+static int nvt_ts_fw_write_value(struct nvt_ts_data *data, u32 addr, u32 value,
+ unsigned int size)
+{
+ u8 buf[] = {
+ (addr & 0x7f) | BIT(7),
+ value & 0xff,
+ (value >> 8) & 0xff,
+ (value >> 16) & 0xff,
+ (value >> 24) & 0xff,
+ };
+ int error;
+
+ error = nvt_ts_fw_set_page(data, addr);
+ if (error)
+ return error;
+
+ return nvt_ts_write_data(data, buf, size + 1);
+}
+
+static int nvt_ts_fw_read_block(struct nvt_ts_data *data, u32 addr, u8 *buf,
+ size_t count)
+{
+ int error;
+
+ error = nvt_ts_fw_set_page(data, addr);
+ if (error)
+ return error;
+
+ return nvt_ts_read_data(data, addr & 0x7f, buf, count);
+}
+
+static int nvt_ts_fw_bootloader_reset(struct nvt_ts_data *data)
+{
+ int error;
+
+ error = nvt_ts_fw_write_byte(data, NVT_TS_FW_BOOTLOADER_RST_ADDR, 0x69);
+ if (error)
+ return error;
+
+ usleep_range(5000, 5500);
+
+ return 0;
+}
+
+static int nvt_ts_fw_identify(struct nvt_ts_data *data)
+{
+ u8 trim[7] = { (NVT_TS_FW_TRIM_ADDR & 0x7f) | BIT(7) };
+ u8 strap;
+ int error;
+
+ error = nvt_ts_fw_write_byte(data, NVT_TS_FW_ENG_RESET_ADDR, 0x5a);
+ if (error)
+ return error;
+
+ usleep_range(11000, 11500);
+
+ error = nvt_ts_fw_bootloader_reset(data);
+ if (error)
+ return error;
+
+ /* The bootloader latches the trim read address from a write */
+ error = nvt_ts_fw_set_page(data, NVT_TS_FW_TRIM_ADDR);
+ if (error)
+ return error;
+
+ error = nvt_ts_write_data(data, trim, sizeof(trim));
+ if (error)
+ return error;
+
+ error = nvt_ts_read_data(data, NVT_TS_FW_TRIM_ADDR & 0x7f, trim + 1,
+ sizeof(trim) - 1);
+ if (error)
+ return error;
+
+ /* The firmware image is only valid for this silicon */
+ if (trim[1] != 0x15 || trim[4] != 0x23 ||
+ trim[5] != 0x65 || trim[6] != 0x03) {
+ dev_err(data->dev, "unexpected trim ID %*ph\n",
+ (int)sizeof(trim) - 1, trim + 1);
+ return -ENODEV;
+ }
+
+ error = nvt_ts_fw_read_block(data, NVT_TS_FW_CASCADE_STRAP_ADDR, &strap,
+ 1);
+ if (error)
+ return error;
+
+ dev_dbg(data->dev, "cascade strap %02x\n", strap);
+
+ return 0;
+}
+
+static int nvt_ts_fw_add_partition(const struct firmware *fw,
+ struct nvt_ts_fw_partition *partitions,
+ unsigned int *num_partitions, u32 file_offs,
+ u32 sram_addr, u32 size, u32 crc)
+{
+ struct nvt_ts_fw_partition *partition;
+
+ if (!size)
+ return 0;
+ if (*num_partitions >= NVT_TS_FW_MAX_PARTITIONS)
+ return -E2BIG;
+ if (file_offs >= fw->size || size >= fw->size - file_offs)
+ return -EINVAL;
+ if (sram_addr > 0x7fffff || size > 0x7fffff - sram_addr)
+ return -EINVAL;
+
+ partition = &partitions[(*num_partitions)++];
+ partition->file_offs = file_offs;
+ partition->sram_addr = sram_addr;
+ partition->size = size;
+ partition->crc = crc;
+
+ return 0;
+}
+
+/*
+ * The image starts with a header that ends at the file offset stored in its
+ * first word, followed by the second header of a cascade pair. The end of the
+ * first header is marked by the only descriptor that describes a non-zero
+ * length copied from the start of the file.
+ */
+static int nvt_ts_fw_parse(const struct firmware *fw,
+ struct nvt_ts_fw_partition *partitions,
+ unsigned int *num_partitions, bool *cascade)
+{
+ const u8 *data = fw->data;
+ u32 header_end, limit, pos;
+ u32 file_offs, sram_addr, size, crc;
+ unsigned int i;
+ int error;
+
+ if (fw->size < 4096 ||
+ memcmp(data + fw->size - NVT_TS_FW_END_FLAG_LEN, "NVT",
+ NVT_TS_FW_END_FLAG_LEN))
+ return -EINVAL;
+ if (data[fw->size - 4096] + data[fw->size - 4095] != 0xff)
+ return -EINVAL;
+
+ header_end = get_unaligned_le32(data);
+ if (header_end < 0x30 || header_end > fw->size || header_end % 16)
+ return -EINVAL;
+
+ /* Overlay images need a separate DLM header parser */
+ if (data[0x28] & BIT(4))
+ return -EOPNOTSUPP;
+
+ *cascade = !!(data[0x20] & BIT(1));
+ if (*cascade && (data[0x29] & BIT(0)))
+ return -EOPNOTSUPP;
+
+ *num_partitions = 0;
+
+ limit = header_end;
+ for (pos = 0x30; pos <= header_end - 0x10; pos += 0x10) {
+ if (get_unaligned_le32(data + pos + 4) &&
+ !get_unaligned_le32(data + pos + 8)) {
+ limit = pos + 0x10;
+ break;
+ }
+ }
+ if (*cascade && limit > header_end / 2)
+ return -EINVAL;
+
+ for (i = 0; i < 2; i++) {
+ pos = i * 12;
+ file_offs = get_unaligned_le32(data + pos);
+ sram_addr = get_unaligned_le32(data + pos + 4);
+ size = get_unaligned_le32(data + pos + 8);
+ crc = get_unaligned_le32(data + 0x18 + i * 4);
+ if (!size)
+ return -EINVAL;
+
+ error = nvt_ts_fw_add_partition(fw, partitions, num_partitions,
+ file_offs, sram_addr, size, crc);
+ if (error)
+ return error;
+ }
+
+ for (pos = 0x30; pos <= limit - 0x10; pos += 0x10) {
+ sram_addr = get_unaligned_le32(data + pos);
+ size = get_unaligned_le32(data + pos + 4);
+ file_offs = get_unaligned_le32(data + pos + 8);
+ crc = get_unaligned_le32(data + pos + 12);
+
+ error = nvt_ts_fw_add_partition(fw, partitions, num_partitions,
+ file_offs, sram_addr, size, crc);
+ if (error)
+ return error;
+ }
+
+ if (*cascade) {
+ pos = 2 * limit - 0x10;
+ sram_addr = get_unaligned_le32(data + pos);
+ size = get_unaligned_le32(data + pos + 4);
+ file_offs = get_unaligned_le32(data + pos + 8);
+ crc = get_unaligned_le32(data + pos + 12);
+ if (!size || file_offs != limit)
+ return -EINVAL;
+
+ error = nvt_ts_fw_add_partition(fw, partitions, num_partitions,
+ file_offs, sram_addr, size, crc);
+ if (error)
+ return error;
+ }
+
+ return *num_partitions >= 2 ? 0 : -EINVAL;
+}
+
+static int nvt_ts_fw_write_partition(struct nvt_ts_data *data,
+ const struct firmware *fw,
+ const struct nvt_ts_fw_partition *partition,
+ u8 *xfer)
+{
+ /*
+ * The container stores the file offset of the last byte of a
+ * partition, not its length: both headers of a cascade image are
+ * `limit` bytes long and carry limit - 1, and the length registers are
+ * programmed with the same value. Copy that last byte as well, the
+ * boot loader does not start the automatic copy without it.
+ */
+ size_t remaining = partition->size + 1;
+ u32 file_offs = partition->file_offs;
+ u32 sram_addr = partition->sram_addr;
+ int error;
+
+ while (remaining) {
+ size_t len = min_t(size_t, remaining, NVT_TS_FW_XFER_LEN);
+
+ error = nvt_ts_fw_set_page(data, sram_addr);
+ if (error)
+ return error;
+
+ xfer[0] = (sram_addr & 0x7f) | BIT(7);
+ memcpy(xfer + 1, fw->data + file_offs, len);
+
+ error = nvt_ts_write_data(data, xfer, len + 1);
+ if (error)
+ return error;
+
+ sram_addr += len;
+ file_offs += len;
+ remaining -= len;
+ }
+
+ return 0;
+}
+
+static int nvt_ts_fw_set_crc_banks(struct nvt_ts_data *data,
+ const struct nvt_ts_fw_partition *partitions)
+{
+ int error;
+
+ error = nvt_ts_fw_write_value(data, NVT_TS_FW_ILM_DES_ADDR,
+ partitions[0].sram_addr, 3);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_write_value(data, NVT_TS_FW_ILM_LENGTH_ADDR,
+ partitions[0].size, 3);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_write_value(data, NVT_TS_FW_ILM_CRC_ADDR,
+ partitions[0].crc, 4);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_write_value(data, NVT_TS_FW_DLM_DES_ADDR,
+ partitions[1].sram_addr, 3);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_write_value(data, NVT_TS_FW_DLM_LENGTH_ADDR,
+ partitions[1].size, 3);
+ if (error)
+ return error;
+
+ return nvt_ts_fw_write_value(data, NVT_TS_FW_DLM_CRC_ADDR,
+ partitions[1].crc, 4);
+}
+
+static int nvt_ts_fw_wait_auto_copy(struct nvt_ts_data *data)
+{
+ u8 status;
+ int error, retry;
+
+ for (retry = 0; retry < 200; retry++) {
+ error = nvt_ts_fw_read_block(data, NVT_TS_FW_AUTO_COPY_ADDR,
+ &status, 1);
+ if (error)
+ return error;
+ if (!status)
+ return 0;
+
+ usleep_range(1000, 1500);
+ }
+
+ dev_err(data->dev, "auto-copy timed out, status %02x\n", status);
+
+ return -ETIMEDOUT;
+}
+
+static int nvt_ts_fw_wait_reset(struct nvt_ts_data *data)
+{
+ u8 state[5];
+ int error, retry;
+
+ for (retry = 0; retry < 100; retry++) {
+ usleep_range(10000, 11000);
+ error = nvt_ts_fw_read_block(data,
+ NVT_TS_FW_EVENT_BUF_ADDR |
+ NVT_TS_FW_EVENT_RESET_DONE,
+ state, sizeof(state));
+ if (error)
+ return error;
+ if (state[0] >= NVT_TS_FW_RESET_STATE_MIN &&
+ state[0] <= NVT_TS_FW_RESET_STATE_MAX)
+ return 0;
+ }
+
+ dev_err(data->dev, "reset state timeout: %*ph\n", (int)sizeof(state),
+ state);
+
+ return -ETIMEDOUT;
+}
+
+static int nvt_ts_fw_check_info(struct nvt_ts_data *data)
+{
+ u8 info[38];
+ u16 pid;
+ int error;
+
+ error = nvt_ts_fw_read_block(data, NVT_TS_FW_EVENT_BUF_ADDR |
+ NVT_TS_FW_EVENT_INFO, info, sizeof(info));
+ if (error)
+ return error;
+
+ if (info[0] + info[1] != 0xff)
+ return -EIO;
+
+ pid = (info[35] << 8) | info[34];
+ dev_dbg(data->dev, "firmware %02x.%02x event protocol %02x PID %04x\n",
+ info[0], info[13], info[12], pid);
+
+ return pid == NVT_TS_FW_PID ? 0 : -ENODEV;
+}
+
+static int nvt_ts_fw_upload(struct nvt_ts_data *data,
+ const struct firmware *fw)
+{
+ u8 *xfer __free(kfree) = kmalloc(NVT_TS_FW_XFER_LEN + 1, GFP_KERNEL);
+ struct nvt_ts_fw_partition partitions[NVT_TS_FW_MAX_PARTITIONS] = {};
+ unsigned int num_partitions, i;
+ u8 reset_done[7] = { NVT_TS_FW_EVENT_RESET_DONE | BIT(7) };
+ u8 host_cmd[] = { NVT_TS_FW_EVENT_HOST_CMD | BIT(7), 0xae, 0 };
+ bool cascade;
+ int error;
+
+ if (!xfer)
+ return -ENOMEM;
+
+ error = nvt_ts_fw_parse(fw, partitions, &num_partitions, &cascade);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_identify(data);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_bootloader_reset(data);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_set_crc_banks(data, partitions);
+ if (error)
+ return error;
+
+ if (cascade) {
+ error = nvt_ts_fw_write_byte(data, NVT_TS_FW_AUTO_COPY_ADDR, 0x56);
+ if (error)
+ return error;
+ }
+
+ for (i = 0; i < num_partitions; i++) {
+ error = nvt_ts_fw_write_partition(data, fw, &partitions[i], xfer);
+ if (error)
+ return error;
+ }
+
+ if (cascade) {
+ error = nvt_ts_fw_wait_auto_copy(data);
+ if (error)
+ return error;
+ }
+
+ error = nvt_ts_fw_set_page(data, NVT_TS_FW_EVENT_BUF_ADDR);
+ if (error)
+ return error;
+
+ /* Clear a pending reset event, then ask for the CRC check and boot */
+ error = nvt_ts_write_data(data, reset_done, sizeof(reset_done));
+ if (error)
+ return error;
+
+ error = nvt_ts_write_data(data, host_cmd, sizeof(host_cmd));
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_write_byte(data, NVT_TS_FW_BOOT_RDY_ADDR, 1);
+ if (error)
+ return error;
+
+ error = nvt_ts_fw_wait_reset(data);
+ if (error)
+ return error;
+
+ return nvt_ts_fw_check_info(data);
+}
+
+static int nvt_ts_load_firmware(struct nvt_ts_data *data)
+{
+ const struct firmware *fw __free(firmware) = NULL;
+ int error;
+
+ if (!data->firmware_name)
+ return 0;
+
+ error = request_firmware(&fw, data->firmware_name, data->dev);
+ if (error) {
+ dev_err(data->dev, "failed to load %s: %d\n",
+ data->firmware_name, error);
+ return error;
+ }
+
+ return nvt_ts_fw_upload(data, fw);
+}
+
static int nvt_ts_start(struct input_dev *dev)
{
struct nvt_ts_data *data = input_get_drvdata(dev);
@@ -171,9 +677,24 @@ static int nvt_ts_start(struct input_dev *dev)
return error;
}
- enable_irq(data->irq);
gpiod_set_value_cansleep(data->reset_gpio, 0);
+ /*
+ * A controller without on-board flash only starts reporting touches
+ * once its firmware has been uploaded, and it loses the firmware when
+ * it is powered down again.
+ */
+ error = nvt_ts_load_firmware(data);
+ if (error) {
+ gpiod_set_value_cansleep(data->reset_gpio, 1);
+ regulator_bulk_disable(ARRAY_SIZE(data->regulators),
+ data->regulators);
+ return error;
+ }
+
+ enable_irq(data->irq);
+ data->powered = true;
+
return 0;
}
@@ -181,6 +702,10 @@ static void nvt_ts_stop(struct input_dev *dev)
{
struct nvt_ts_data *data = input_get_drvdata(dev);
+ if (!data->powered)
+ return;
+
+ data->powered = false;
disable_irq(data->irq);
gpiod_set_value_cansleep(data->reset_gpio, 1);
regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
@@ -237,6 +762,20 @@ int nvt_ts_probe(struct device *dev, const struct nvt_ts_bus_ops *bus_ops,
data->chip = chip;
data->irq = irq;
+ /*
+ * firmware-name is optional: without it the controller is used as is,
+ * which leaves the no-flash parts without firmware and thus without
+ * touches, but keeps the flash based ones working.
+ */
+ error = device_property_read_string(dev, "firmware-name",
+ &data->firmware_name);
+ if (error) {
+ data->firmware_name = NULL;
+ } else if (!bus_ops->write) {
+ dev_err(dev, "firmware-name given, but this bus cannot write\n");
+ return -EOPNOTSUPP;
+ }
+
/*
* VCC is the analog voltage supply
* IOVCC is the digital voltage supply
diff --git a/drivers/input/touchscreen/novatek-nvt-ts-spi.c b/drivers/input/touchscreen/novatek-nvt-ts-spi.c
index 8dfe2b688181a..2b01f3efe38f2 100644
--- a/drivers/input/touchscreen/novatek-nvt-ts-spi.c
+++ b/drivers/input/touchscreen/novatek-nvt-ts-spi.c
@@ -48,8 +48,32 @@ static int nvt_ts_spi_read(struct nvt_ts_data *data, u8 reg, u8 *buf,
return error;
}
+static int nvt_ts_spi_write(struct nvt_ts_data *data, const u8 *buf,
+ size_t count)
+{
+ struct spi_device *spi = to_spi_device(data->dev);
+ u8 *xfer;
+ int error;
+
+ /*
+ * Unlike spi_write_then_read(), spi_write() does not bounce the buffer
+ * and maps it for DMA as is, while the firmware upload helpers build
+ * their commands on the stack.
+ */
+ xfer = kmalloc(count, GFP_KERNEL);
+ if (!xfer)
+ return -ENOMEM;
+
+ memcpy(xfer, buf, count);
+ error = spi_write(spi, xfer, count);
+ kfree(xfer);
+
+ return error;
+}
+
static const struct nvt_ts_bus_ops nvt_ts_spi_bus_ops = {
.read = nvt_ts_spi_read,
+ .write = nvt_ts_spi_write,
};
static int nvt_ts_spi_probe(struct spi_device *spi)
diff --git a/drivers/input/touchscreen/novatek-nvt-ts.h b/drivers/input/touchscreen/novatek-nvt-ts.h
index 7e2b207acb947..fafe394e30666 100644
--- a/drivers/input/touchscreen/novatek-nvt-ts.h
+++ b/drivers/input/touchscreen/novatek-nvt-ts.h
@@ -51,12 +51,16 @@ struct nvt_ts_data;
/**
* struct nvt_ts_bus_ops - transport specific operations
* @read: read @count bytes of the controller into @buf
+ * @write: write @count bytes from @buf, the first byte of @buf being the
+ * register address with bit 7 set; optional, only bus attachments
+ * that can upload firmware implement it
*
* The controller exposes a flat register space; transports differ only in
* how a register read is framed on the wire.
*/
struct nvt_ts_bus_ops {
int (*read)(struct nvt_ts_data *data, u8 reg, u8 *buf, size_t count);
+ int (*write)(struct nvt_ts_data *data, const u8 *buf, size_t count);
};
/**
@@ -83,7 +87,9 @@ struct nvt_ts_data {
struct gpio_desc *reset_gpio;
struct regulator_bulk_data regulators[2];
struct touchscreen_properties prop;
+ const char *firmware_name;
int max_touches;
+ bool powered; /* regulators on, controller out of reset */
u8 buf[NVT_TS_MAX_PACKET];
};