This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 8fd5d0383d0 drivers/input: Add CST92xx capacitive touch controller
driver.
8fd5d0383d0 is described below
commit 8fd5d0383d0068466c7a9514aec64178a06d6b45
Author: ViezeVingertjes <[email protected]>
AuthorDate: Wed Nov 25 17:54:04 2026 +0100
drivers/input: Add CST92xx capacitive touch controller driver.
Add a touchscreen lower-half driver for the Hynitron CST92xx family
(CST9217, CST9220) of I2C capacitive touch controllers, used for example
on the Waveshare ESP32-S3-Touch-AMOLED-2.16.
At registration the driver optionally pulses RST through a board
callback, enters the controller information mode to verify the firmware
check code and chip type and to read the native resolution, returns the
controller to normal mode and registers /dev/inputN with the common
touchscreen upper half.
The INT pin is handled through board attach/enable callbacks, so the
driver has no MCU dependency. Each interrupt queues a worker on HPWORK
that reads the report block at 0xd000, acknowledges it by writing 0xab
and turns the reported contacts into TOUCH_DOWN/MOVE/UP events with
stable IDs and timestamps. Up to CONFIG_INPUT_CST92XX_MAX_POINTS
contacts are reported. Output resolution and swap/mirror flags are set
in the board configuration. An optional polled mode is available for
boards without INT.
Tested on Waveshare ESP32-S3-Touch-AMOLED-2.16 (CST9220) with the LVGL
widgets demo: taps and scrolling land at the right positions. Built in
interrupt and polled mode, with debug output enabled, no warnings.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: ViezeVingertjes <[email protected]>
---
.../os/drivers/character/input/cst92xx.rst | 99 +++
Documentation/os/drivers/character/input/index.rst | 1 +
drivers/input/CMakeLists.txt | 4 +
drivers/input/Kconfig | 52 ++
drivers/input/Make.defs | 4 +
drivers/input/cst92xx.c | 692 +++++++++++++++++++++
include/nuttx/input/cst92xx.h | 144 +++++
7 files changed, 996 insertions(+)
diff --git a/Documentation/os/drivers/character/input/cst92xx.rst
b/Documentation/os/drivers/character/input/cst92xx.rst
new file mode 100644
index 00000000000..04e5bb2ff08
--- /dev/null
+++ b/Documentation/os/drivers/character/input/cst92xx.rst
@@ -0,0 +1,99 @@
+=======================================
+Hynitron CST92xx Capacitive Touchscreen
+=======================================
+
+The Hynitron CST92xx family (CST9217, CST9220) are I2C capacitive
+multi-touch controllers found on small round and square AMOLED panels,
+for example the Waveshare ESP32-S3-Touch-AMOLED-2.16. They use 16-bit
+register addresses and live at the 7-bit I2C address ``0x5a``.
+
+The driver is a touchscreen lower half for the common touchscreen upper
+half (see :doc:`../touchscreen`). Once registered the device appears as
+``/dev/inputN`` and applications read ``struct touch_sample_s`` samples.
+
+- Header: ``include/nuttx/input/cst92xx.h``
+- Driver: ``drivers/input/cst92xx.c``
+
+Configuration
+=============
+
+- ``CONFIG_INPUT=y``
+- ``CONFIG_INPUT_TOUCHSCREEN=y``
+- ``CONFIG_INPUT_CST92XX=y`` (selects ``CONFIG_SCHED_HPWORK``)
+- ``CONFIG_INPUT_CST92XX_MAX_POINTS``: number of contacts reported per
+ sample (default 1). Values above 1 enable multi-touch; readers must
+ then read ``SIZEOF_TOUCH_SAMPLE_S(maxpoint)`` bytes per sample.
+- ``CONFIG_INPUT_CST92XX_NBUFFERS``: samples buffered per open file.
+- ``CONFIG_INPUT_CST92XX_POLLMODE`` / ``CONFIG_INPUT_CST92XX_POLL_PERIOD``:
+ poll the controller while the device is open instead of using INT.
+
+Board Support
+=============
+
+The board provides an initialized I2C master and a persistent
+``struct cst92xx_config_s``:
+
+- ``address``, ``frequency``: I2C address (``CST92XX_I2C_ADDRESS``) and
+ bus speed.
+- ``xres``, ``yres``: output resolution. If zero, the resolution read
+ from the controller firmware is used.
+- ``flags``: any of ``TOUCH_FLAG_SWAPXY``, ``TOUCH_FLAG_MIRRORX`` and
+ ``TOUCH_FLAG_MIRRORY``. The driver swaps first and then mirrors
+ against the output resolution.
+- ``attach``: attach the driver interrupt handler to the INT GPIO
+ (active low, falling edge). Required unless polled mode is used.
+- ``enable``: enable or disable the INT GPIO interrupt. Required unless
+ polled mode is used.
+- ``reset``: drive the RST pin (``true`` holds the controller in reset).
+ Optional.
+
+Then call ``cst92xx_register(i2c, &config, minor)`` during board
+bring-up:
+
+.. code-block:: c
+
+ static const struct cst92xx_config_s g_cst92xx_config =
+ {
+ .address = CST92XX_I2C_ADDRESS,
+ .frequency = 400000,
+ .xres = 480,
+ .yres = 480,
+ .attach = board_cst92xx_attach,
+ .enable = board_cst92xx_enable,
+ .reset = board_cst92xx_reset,
+ };
+
+ ret = cst92xx_register(i2c, &g_cst92xx_config, 0);
+
+Operation
+=========
+
+``cst92xx_register()`` pulses RST (if provided), switches the controller
+into its information mode to verify the firmware check code and chip
+type (``0x9217`` or ``0x9220``) and to read the native resolution, then
+returns it to normal reporting mode, registers ``/dev/inputN`` and
+attaches the interrupt. The interrupt is enabled while at least one
+file is open.
+
+Each INT edge queues a worker on the high-priority work queue. The
+worker reads the touch report at ``0xd000`` and acknowledges it by
+writing ``0xab`` back to that address. Reports without the ``0xab``
+marker, or still holding the previous acknowledge, are ignored.
+Contacts are tracked by their hardware ID and reported as:
+
+- first contact: ``TOUCH_DOWN | TOUCH_ID_VALID | TOUCH_POS_VALID``
+- continued contact: ``TOUCH_MOVE | TOUCH_ID_VALID | TOUCH_POS_VALID``
+- lost contact: ``TOUCH_UP | TOUCH_ID_VALID | TOUCH_POS_VALID`` at the
+ last reported position
+
+When the controller signals that the panel is covered (palm detect) all
+contacts are released.
+
+Testing
+=======
+
+``apps/examples/touchscreen`` (``tc``) reads single-point samples from
+``CONFIG_EXAMPLES_TOUCHSCREEN_DEVPATH`` (default ``/dev/input0``) and
+works with ``CONFIG_INPUT_CST92XX_MAX_POINTS=1``. With more points a
+sample carries one point per active or just-released contact, so its
+size varies with ``npoints``.
diff --git a/Documentation/os/drivers/character/input/index.rst
b/Documentation/os/drivers/character/input/index.rst
index d4237932fd2..c169f4e8c5d 100644
--- a/Documentation/os/drivers/character/input/index.rst
+++ b/Documentation/os/drivers/character/input/index.rst
@@ -5,6 +5,7 @@ Input Devices
.. toctree::
:caption: Supported Drivers
+ cst92xx.rst
keypad-keyboard.rst
keypad.rst
mpr121.rst
diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt
index 9891db6fa03..2185ace2e1a 100644
--- a/drivers/input/CMakeLists.txt
+++ b/drivers/input/CMakeLists.txt
@@ -94,6 +94,10 @@ if(CONFIG_INPUT)
list(APPEND SRCS gt9xx.c)
endif()
+ if(CONFIG_INPUT_CST92XX)
+ list(APPEND SRCS cst92xx.c)
+ endif()
+
if(CONFIG_INPUT_ST7123)
list(APPEND SRCS st7123.c)
endif()
diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
index e5287b38a14..186c5a04104 100644
--- a/drivers/input/Kconfig
+++ b/drivers/input/Kconfig
@@ -187,6 +187,58 @@ config UINPUT_KEYBOARD_BUFNUMBER
endif
+config INPUT_CST92XX
+ bool "Hynitron CST92xx capacitive touch controller"
+ default n
+ select I2C
+ select INPUT_TOUCHSCREEN
+ select SCHED_HPWORK
+ ---help---
+ Enable support for the Hynitron CST92xx (CST9217, CST9220) I2C
+ capacitive multi-touch controllers.
+
+if INPUT_CST92XX
+
+config INPUT_CST92XX_MAX_POINTS
+ int "Maximum number of contacts"
+ default 1
+ range 1 10
+ ---help---
+ Maximum number of simultaneous contacts read from each touch
+ report. The CST9217/CST9220 firmware commonly reports up to
two.
+ Samples carry up to this many points, so readers that only read
+ a single-point struct touch_sample_s (e.g. LVGL or
+ examples/touchscreen) need this to be 1.
+
+config INPUT_CST92XX_NBUFFERS
+ int "Number of touch sample buffers"
+ default 8
+ ---help---
+ Number of touch samples buffered per open file by the
touchscreen
+ upper half.
+
+config INPUT_CST92XX_POLLMODE
+ bool "Polled mode"
+ default n
+ ---help---
+ Read touch reports periodically while the device is open instead
+ of on the INT interrupt. Use only if INT is not connected; it
adds
+ latency and I2C traffic.
+
+if INPUT_CST92XX_POLLMODE
+
+config INPUT_CST92XX_POLL_PERIOD
+ int "Poll period (ms)"
+ default 20
+ range 1 1000
+ ---help---
+ Interval between two reads of the touch report when the
+ interrupt line is not used.
+
+endif # INPUT_CST92XX_POLLMODE
+
+endif # INPUT_CST92XX
+
config INPUT_ST7123
bool "ST7123 Touch Screen Controller"
default n
diff --git a/drivers/input/Make.defs b/drivers/input/Make.defs
index c7c22a56e25..5f1e04dc207 100644
--- a/drivers/input/Make.defs
+++ b/drivers/input/Make.defs
@@ -143,6 +143,10 @@ ifeq ($(CONFIG_INPUT_MPR121_KEYPAD),y)
CSRCS += mpr121.c
endif
+ifeq ($(CONFIG_INPUT_CST92XX),y)
+ CSRCS += cst92xx.c
+endif
+
ifeq ($(CONFIG_INPUT_ST7123),y)
CSRCS += st7123.c
endif
diff --git a/drivers/input/cst92xx.c b/drivers/input/cst92xx.c
new file mode 100644
index 00000000000..46191051018
--- /dev/null
+++ b/drivers/input/cst92xx.c
@@ -0,0 +1,692 @@
+/****************************************************************************
+ * drivers/input/cst92xx.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <nuttx/debug.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/input/touchscreen.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/signal.h>
+#include <nuttx/wqueue.h>
+
+#include <nuttx/input/cst92xx.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* 16-bit register / command addresses. Commands are issued by writing the
+ * address alone, registers are read by writing the address and then
+ * reading back with a repeated start.
+ */
+
+#define CST92XX_REG_POINTS 0xd000 /* Touch report, write ACK here */
+#define CST92XX_CMD_INFO_MODE 0xd101 /* Enter debug/info mode */
+#define CST92XX_CMD_NORMAL_MODE 0xd109 /* Return to normal reporting */
+#define CST92XX_REG_RESOLUTION 0xd1f8 /* X/Y resolution, LE16 each */
+#define CST92XX_REG_CHECKCODE 0xd1fc /* Firmware check code, LE32 */
+#define CST92XX_REG_CHIPTYPE 0xd204 /* Project ID, chip type, LE16 */
+#define CST92XX_REG_FWVERSION 0xd208 /* Firmware version, checksum */
+
+#define CST92XX_ACK 0xab /* Report marker and ACK value */
+#define CST92XX_CHECKCODE_MASK 0xffff0000
+#define CST92XX_CHECKCODE 0xcaca0000
+#define CST92XX_FW_ERASED 0xa5a5a5a5
+
+#define CST92XX_CHIP_CST9217 0x9217
+#define CST92XX_CHIP_CST9220 0x9220
+
+/* Touch report layout. The first contact occupies bytes 0-4, followed by
+ * a status byte with the contact count (byte 5) and the 0xab marker
+ * (byte 6). Further contacts follow at 7 + 5 * (n - 1).
+ */
+
+#define CST92XX_POINT_SIZE 5
+#define CST92XX_COUNT_OFFSET 5
+#define CST92XX_MARKER_OFFSET 6
+#define CST92XX_COUNT_MASK 0x7f
+#define CST92XX_COVER_MASK 0x80 /* In byte 4: palm/cover detect */
+#define CST92XX_EVENT_MASK 0x0f
+#define CST92XX_EVENT_PRESSED 0x06
+
+#define CST92XX_POINT_OFFSET(n) \
+ ((n) == 0 ? 0 : CST92XX_POINT_SIZE * (n) + 2)
+#define CST92XX_FRAME_SIZE(n) (CST92XX_POINT_SIZE * (n) + 2)
+
+#define CST92XX_MAX_POINTS CONFIG_INPUT_CST92XX_MAX_POINTS
+
+/* Timing */
+
+#define CST92XX_RESET_HOLD_US 10000
+#define CST92XX_RESET_BOOT_US 50000
+#define CST92XX_MODE_DELAY_US 10000
+
+/* Driver registration */
+
+#define DEV_FORMAT "/dev/input%d"
+#define DEV_NAMELEN 16
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct cst92xx_dev_s
+{
+ /* Must be the first member: the upper half passes this pointer back */
+
+ struct touch_lowerhalf_s lower;
+
+ FAR const struct cst92xx_config_s *config;
+ FAR struct i2c_master_s *i2c;
+ struct work_s work;
+ mutex_t lock;
+ uint8_t crefs;
+
+ /* Contacts reported as down by the previous report, one bit per ID, and
+ * their last transformed position so that the release can be reported
+ * where the contact was lost.
+ */
+
+ uint16_t downmap;
+ int16_t lastx[CST92XX_MAX_POINTS];
+ int16_t lasty[CST92XX_MAX_POINTS];
+
+ uint8_t frame[CST92XX_FRAME_SIZE(CST92XX_MAX_POINTS)];
+
+ /* Report buffer, aligned for struct touch_sample_s */
+
+ union
+ {
+ struct touch_sample_s sample;
+ uint8_t buf[SIZEOF_TOUCH_SAMPLE_S(CST92XX_MAX_POINTS)];
+ } report;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: cst92xx_read
+ *
+ * Description:
+ * Read a block starting at a 16-bit register address.
+ *
+ ****************************************************************************/
+
+static int cst92xx_read(FAR struct cst92xx_dev_s *priv, uint16_t reg,
+ FAR uint8_t *buffer, size_t buflen)
+{
+ struct i2c_config_s config;
+ uint8_t addr[2];
+
+ config.frequency = priv->config->frequency;
+ config.address = priv->config->address;
+ config.addrlen = 7;
+
+ addr[0] = reg >> 8;
+ addr[1] = reg & 0xff;
+
+ return i2c_writeread(priv->i2c, &config, addr, sizeof(addr),
+ buffer, buflen);
+}
+
+/****************************************************************************
+ * Name: cst92xx_write
+ *
+ * Description:
+ * Write a 16-bit register/command address optionally followed by one
+ * data byte.
+ *
+ ****************************************************************************/
+
+static int cst92xx_write(FAR struct cst92xx_dev_s *priv, uint16_t reg,
+ FAR const uint8_t *data, size_t datalen)
+{
+ struct i2c_config_s config;
+ uint8_t buffer[3];
+
+ DEBUGASSERT(datalen <= 1);
+
+ config.frequency = priv->config->frequency;
+ config.address = priv->config->address;
+ config.addrlen = 7;
+
+ buffer[0] = reg >> 8;
+ buffer[1] = reg & 0xff;
+ if (datalen > 0)
+ {
+ buffer[2] = data[0];
+ }
+
+ return i2c_write(priv->i2c, &config, buffer, 2 + datalen);
+}
+
+/****************************************************************************
+ * Name: cst92xx_transform
+ *
+ * Description:
+ * Apply the board swap/mirror configuration to a raw coordinate pair.
+ *
+ ****************************************************************************/
+
+static void cst92xx_transform(FAR struct cst92xx_dev_s *priv,
+ uint16_t rawx, uint16_t rawy,
+ FAR int16_t *x, FAR int16_t *y)
+{
+ uint8_t flags = priv->config->flags;
+ uint16_t xres = priv->lower.xres;
+ uint16_t yres = priv->lower.yres;
+ uint16_t tmp;
+
+ if (flags & TOUCH_FLAG_SWAPXY)
+ {
+ tmp = rawx;
+ rawx = rawy;
+ rawy = tmp;
+ }
+
+ if (xres > 0)
+ {
+ if (rawx >= xres)
+ {
+ rawx = xres - 1;
+ }
+
+ if (flags & TOUCH_FLAG_MIRRORX)
+ {
+ rawx = xres - 1 - rawx;
+ }
+ }
+
+ if (yres > 0)
+ {
+ if (rawy >= yres)
+ {
+ rawy = yres - 1;
+ }
+
+ if (flags & TOUCH_FLAG_MIRRORY)
+ {
+ rawy = yres - 1 - rawy;
+ }
+ }
+
+ *x = (int16_t)rawx;
+ *y = (int16_t)rawy;
+}
+
+/****************************************************************************
+ * Name: cst92xx_worker
+ *
+ * Description:
+ * Read one touch report, acknowledge it and convert the reported contacts
+ * into DOWN/MOVE/UP events for the upper half. Runs on the high priority
+ * work queue, either from the interrupt handler or, in polled mode,
+ * periodically while the device is open.
+ *
+ ****************************************************************************/
+
+static void cst92xx_worker(FAR void *arg)
+{
+ FAR struct cst92xx_dev_s *priv = arg;
+ FAR struct touch_sample_s *sample = &priv->report.sample;
+ FAR struct touch_point_s *point;
+ FAR const uint8_t *frame = priv->frame;
+ uint16_t rawx[CST92XX_MAX_POINTS];
+ uint16_t rawy[CST92XX_MAX_POINTS];
+ uint16_t pressed = 0;
+ uint64_t timestamp;
+ uint8_t maxpoint = priv->lower.maxpoint;
+ uint8_t ack = CST92XX_ACK;
+ int npoints = 0;
+ int count;
+ int ret;
+ int i;
+
+ memset(rawx, 0, sizeof(rawx));
+ memset(rawy, 0, sizeof(rawy));
+
+ if (nxmutex_lock(&priv->lock) < 0)
+ {
+ return;
+ }
+
+ ret = cst92xx_read(priv, CST92XX_REG_POINTS, priv->frame,
+ CST92XX_FRAME_SIZE(maxpoint));
+
+ /* Always acknowledge, otherwise the controller stops reporting */
+
+ cst92xx_write(priv, CST92XX_REG_POINTS, &ack, 1);
+
+ if (ret < 0)
+ {
+ ierr("ERROR: Failed to read touch report: %d\n", ret);
+ goto out;
+ }
+
+ /* The first byte still holds a previous ACK if there is no new report,
+ * and the marker byte must be present for the report to be valid.
+ */
+
+ if (frame[0] == CST92XX_ACK || frame[CST92XX_MARKER_OFFSET] != CST92XX_ACK)
+ {
+ goto out;
+ }
+
+ if ((frame[4] & CST92XX_COVER_MASK) != 0)
+ {
+ /* The panel is covered (palm detect): treat as all contacts lost */
+
+ iinfo("Cover detected\n");
+ }
+ else
+ {
+ count = frame[CST92XX_COUNT_OFFSET] & CST92XX_COUNT_MASK;
+ if (count > maxpoint)
+ {
+ count = maxpoint;
+ }
+
+ for (i = 0; i < count; i++)
+ {
+ FAR const uint8_t *p = &frame[CST92XX_POINT_OFFSET(i)];
+ uint8_t id = p[0] >> 4;
+
+ if (id >= maxpoint ||
+ (p[0] & CST92XX_EVENT_MASK) != CST92XX_EVENT_PRESSED)
+ {
+ continue;
+ }
+
+ rawx[id] = ((uint16_t)p[1] << 4) | (p[3] >> 4);
+ rawy[id] = ((uint16_t)p[2] << 4) | (p[3] & 0x0f);
+ pressed |= 1 << id;
+ }
+ }
+
+ timestamp = touch_get_time();
+ memset(&priv->report, 0, sizeof(priv->report));
+
+ for (i = 0; i < maxpoint; i++)
+ {
+ bool was_down = (priv->downmap & (1 << i)) != 0;
+ bool is_down = (pressed & (1 << i)) != 0;
+
+ if (!was_down && !is_down)
+ {
+ continue;
+ }
+
+ point = &sample->point[npoints++];
+
+ if (is_down)
+ {
+ cst92xx_transform(priv, rawx[i], rawy[i],
+ &priv->lastx[i], &priv->lasty[i]);
+ point->flags = was_down ? TOUCH_MOVE : TOUCH_DOWN;
+ }
+ else
+ {
+ point->flags = TOUCH_UP;
+ }
+
+ point->flags |= TOUCH_ID_VALID | TOUCH_POS_VALID;
+ point->id = i;
+ point->x = priv->lastx[i];
+ point->y = priv->lasty[i];
+ point->timestamp = timestamp;
+ }
+
+ priv->downmap = pressed;
+
+ if (npoints > 0)
+ {
+ sample->npoints = npoints;
+ touch_event(priv->lower.priv, sample);
+ }
+
+out:
+#ifdef CONFIG_INPUT_CST92XX_POLLMODE
+ if (priv->crefs > 0)
+ {
+ work_queue(HPWORK, &priv->work, cst92xx_worker, priv,
+ MSEC2TICK(CONFIG_INPUT_CST92XX_POLL_PERIOD));
+ }
+#endif
+
+ nxmutex_unlock(&priv->lock);
+}
+
+#ifndef CONFIG_INPUT_CST92XX_POLLMODE
+/****************************************************************************
+ * Name: cst92xx_interrupt
+ *
+ * Description:
+ * INT GPIO handler attached by the board through config->attach().
+ *
+ ****************************************************************************/
+
+static int cst92xx_interrupt(int irq, FAR void *context, FAR void *arg)
+{
+ FAR struct cst92xx_dev_s *priv = arg;
+
+ DEBUGASSERT(priv != NULL);
+
+ if (work_available(&priv->work))
+ {
+ work_queue(HPWORK, &priv->work, cst92xx_worker, priv, 0);
+ }
+
+ return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: cst92xx_open
+ ****************************************************************************/
+
+static int cst92xx_open(FAR struct touch_lowerhalf_s *lower)
+{
+ FAR struct cst92xx_dev_s *priv = (FAR struct cst92xx_dev_s *)lower;
+ int ret;
+
+ ret = nxmutex_lock(&priv->lock);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ if (priv->crefs == UINT8_MAX)
+ {
+ nxmutex_unlock(&priv->lock);
+ return -EMFILE;
+ }
+
+ if (priv->crefs++ == 0)
+ {
+ priv->downmap = 0;
+
+#ifndef CONFIG_INPUT_CST92XX_POLLMODE
+ priv->config->enable(priv->config, true);
+#endif
+
+ /* Drain any report that is already pending (and, in polled mode,
+ * start polling).
+ */
+
+ work_queue(HPWORK, &priv->work, cst92xx_worker, priv, 0);
+ }
+
+ nxmutex_unlock(&priv->lock);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: cst92xx_close
+ ****************************************************************************/
+
+static int cst92xx_close(FAR struct touch_lowerhalf_s *lower)
+{
+ FAR struct cst92xx_dev_s *priv = (FAR struct cst92xx_dev_s *)lower;
+ int ret;
+
+ ret = nxmutex_lock(&priv->lock);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ if (priv->crefs > 0 && --priv->crefs == 0)
+ {
+#ifndef CONFIG_INPUT_CST92XX_POLLMODE
+ priv->config->enable(priv->config, false);
+#endif
+ work_cancel(HPWORK, &priv->work);
+ }
+
+ nxmutex_unlock(&priv->lock);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: cst92xx_probe
+ *
+ * Description:
+ * Reset the controller, read its identification and resolution in info
+ * mode and return it to normal reporting mode.
+ *
+ ****************************************************************************/
+
+static int cst92xx_probe(FAR struct cst92xx_dev_s *priv,
+ FAR uint16_t *resx, FAR uint16_t *resy)
+{
+ FAR const struct cst92xx_config_s *config = priv->config;
+ uint32_t checkcode;
+ uint32_t fwversion;
+ uint16_t chiptype;
+ uint8_t buf[8];
+ int ret;
+
+ if (config->reset != NULL)
+ {
+ config->reset(config, true);
+ nxsig_usleep(CST92XX_RESET_HOLD_US);
+ config->reset(config, false);
+ nxsig_usleep(CST92XX_RESET_BOOT_US);
+ }
+
+ ret = cst92xx_write(priv, CST92XX_CMD_INFO_MODE, NULL, 0);
+ if (ret < 0)
+ {
+ ierr("ERROR: No response at 0x%02x: %d\n", config->address, ret);
+ return ret;
+ }
+
+ nxsig_usleep(CST92XX_MODE_DELAY_US);
+
+ ret = cst92xx_read(priv, CST92XX_REG_CHECKCODE, buf, 4);
+ if (ret < 0)
+ {
+ goto errout;
+ }
+
+ checkcode = ((uint32_t)buf[3] << 24) | ((uint32_t)buf[2] << 16) |
+ ((uint32_t)buf[1] << 8) | buf[0];
+
+ ret = cst92xx_read(priv, CST92XX_REG_RESOLUTION, buf, 4);
+ if (ret < 0)
+ {
+ goto errout;
+ }
+
+ *resx = ((uint16_t)buf[1] << 8) | buf[0];
+ *resy = ((uint16_t)buf[3] << 8) | buf[2];
+
+ ret = cst92xx_read(priv, CST92XX_REG_CHIPTYPE, buf, 4);
+ if (ret < 0)
+ {
+ goto errout;
+ }
+
+ chiptype = ((uint16_t)buf[3] << 8) | buf[2];
+
+ ret = cst92xx_read(priv, CST92XX_REG_FWVERSION, buf, 8);
+ if (ret < 0)
+ {
+ goto errout;
+ }
+
+ fwversion = ((uint32_t)buf[3] << 24) | ((uint32_t)buf[2] << 16) |
+ ((uint32_t)buf[1] << 8) | buf[0];
+
+ iinfo("CST%04x: checkcode 0x%08" PRIx32 " fw 0x%08" PRIx32
+ " resolution %ux%u\n", chiptype, checkcode, fwversion,
+ *resx, *resy);
+
+ if (fwversion == CST92XX_FW_ERASED)
+ {
+ ierr("ERROR: Controller has no firmware\n");
+ ret = -ENODEV;
+ goto errout;
+ }
+
+ if ((checkcode & CST92XX_CHECKCODE_MASK) != CST92XX_CHECKCODE)
+ {
+ ierr("ERROR: Bad firmware check code 0x%08" PRIx32 "\n", checkcode);
+ ret = -ENODEV;
+ goto errout;
+ }
+
+ if (chiptype != CST92XX_CHIP_CST9217 && chiptype != CST92XX_CHIP_CST9220)
+ {
+ ierr("ERROR: Unsupported chip type 0x%04x\n", chiptype);
+ ret = -ENODEV;
+ goto errout;
+ }
+
+ ret = cst92xx_write(priv, CST92XX_CMD_NORMAL_MODE, NULL, 0);
+ nxsig_usleep(CST92XX_MODE_DELAY_US);
+ return ret;
+
+errout:
+ cst92xx_write(priv, CST92XX_CMD_NORMAL_MODE, NULL, 0);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: cst92xx_register
+ *
+ * Description:
+ * Reset and probe the CST92xx over I2C, configure the touchscreen lower
+ * half and register it as /dev/inputN where N is the given minor number.
+ *
+ * Input Parameters:
+ * i2c - I2C master used to talk to the controller
+ * config - Persistent board configuration
+ * minor - Device minor number used to form /dev/inputN
+ *
+ * Returned Value:
+ * Zero (OK) on success. Otherwise, a negated errno value is returned to
+ * indicate the nature of the failure.
+ *
+ ****************************************************************************/
+
+int cst92xx_register(FAR struct i2c_master_s *i2c,
+ FAR const struct cst92xx_config_s *config, int minor)
+{
+ FAR struct cst92xx_dev_s *priv;
+ char devname[DEV_NAMELEN];
+ uint16_t resx = 0;
+ uint16_t resy = 0;
+ int ret;
+
+ DEBUGASSERT(i2c != NULL && config != NULL);
+
+#ifndef CONFIG_INPUT_CST92XX_POLLMODE
+ if (config->attach == NULL || config->enable == NULL)
+ {
+ ierr("ERROR: attach() and enable() are required\n");
+ return -EINVAL;
+ }
+#endif
+
+ priv = kmm_zalloc(sizeof(struct cst92xx_dev_s));
+ if (priv == NULL)
+ {
+ return -ENOMEM;
+ }
+
+ priv->config = config;
+ priv->i2c = i2c;
+ nxmutex_init(&priv->lock);
+
+ ret = cst92xx_probe(priv, &resx, &resy);
+ if (ret < 0)
+ {
+ goto errout_with_priv;
+ }
+
+ if (config->flags & TOUCH_FLAG_SWAPXY)
+ {
+ uint16_t tmp = resx;
+
+ resx = resy;
+ resy = tmp;
+ }
+
+ priv->lower.maxpoint = CST92XX_MAX_POINTS;
+ priv->lower.xres = config->xres != 0 ? config->xres : resx;
+ priv->lower.yres = config->yres != 0 ? config->yres : resy;
+ priv->lower.flags = 0; /* Swap/mirror is handled by this driver */
+ priv->lower.open = cst92xx_open;
+ priv->lower.close = cst92xx_close;
+
+ snprintf(devname, sizeof(devname), DEV_FORMAT, minor);
+ ret = touch_register(&priv->lower, devname, CONFIG_INPUT_CST92XX_NBUFFERS);
+ if (ret < 0)
+ {
+ ierr("ERROR: Failed to register %s: %d\n", devname, ret);
+ goto errout_with_priv;
+ }
+
+#ifndef CONFIG_INPUT_CST92XX_POLLMODE
+ ret = config->attach(config, cst92xx_interrupt, priv);
+ if (ret < 0)
+ {
+ ierr("ERROR: Failed to attach interrupt: %d\n", ret);
+ touch_unregister(&priv->lower, devname);
+ goto errout_with_priv;
+ }
+
+ /* Keep the interrupt disabled until the device is opened */
+
+ config->enable(config, false);
+#endif
+
+ iinfo("Registered %s, %ux%u, %u points\n", devname,
+ priv->lower.xres, priv->lower.yres, priv->lower.maxpoint);
+ return OK;
+
+errout_with_priv:
+ nxmutex_destroy(&priv->lock);
+ kmm_free(priv);
+ return ret;
+}
diff --git a/include/nuttx/input/cst92xx.h b/include/nuttx/input/cst92xx.h
new file mode 100644
index 00000000000..bdf8024cfb8
--- /dev/null
+++ b/include/nuttx/input/cst92xx.h
@@ -0,0 +1,144 @@
+/****************************************************************************
+ * include/nuttx/input/cst92xx.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_INPUT_CST92XX_H
+#define __INCLUDE_NUTTX_INPUT_CST92XX_H
+
+/* The Hynitron CST92xx family (CST9217, CST9220) are I2C capacitive
+ * multi-touch controllers that use 16-bit register addresses. This header
+ * exposes the board-facing registration API. Once registered, the device
+ * appears as /dev/inputN and reports samples through the common touchscreen
+ * upper half.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/irq.h>
+
+#ifdef CONFIG_INPUT_CST92XX
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Default 7-bit I2C address of the CST92xx */
+
+#define CST92XX_I2C_ADDRESS 0x5a
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* Board-specific configuration. Memory for this structure is provided by
+ * the caller, is not copied by the driver, and must persist while the
+ * driver is active.
+ *
+ * Coordinate transformation: the raw controller coordinates are first
+ * swapped (TOUCH_FLAG_SWAPXY) and then mirrored (TOUCH_FLAG_MIRRORX /
+ * TOUCH_FLAG_MIRRORY) against the output resolution. The transformation is
+ * done by this driver; the flags are not passed on to the upper half.
+ */
+
+struct cst92xx_config_s
+{
+ uint8_t address; /* 7-bit I2C address, normally CST92XX_I2C_ADDRESS */
+ uint32_t frequency; /* I2C frequency in Hz */
+
+ /* Output resolution in pixels, after the optional swap. If zero, the
+ * resolution reported by the controller firmware is used.
+ */
+
+ uint16_t xres;
+ uint16_t yres;
+
+ /* Coordinate transformation, any of TOUCH_FLAG_SWAPXY,
+ * TOUCH_FLAG_MIRRORX and TOUCH_FLAG_MIRRORY from
+ * <nuttx/input/touchscreen.h>.
+ */
+
+ uint8_t flags;
+
+ /* IRQ/GPIO access callbacks. These isolate the driver from the GPIO and
+ * interrupt handling of the board and MCU.
+ *
+ * attach - Attach the driver interrupt handler to the INT GPIO (active
+ * low, falling edge). The driver disables the interrupt right
+ * after attaching and enables it while the device is open.
+ * Required unless CONFIG_INPUT_CST92XX_POLLMODE is enabled.
+ * enable - Enable or disable the INT GPIO interrupt. Required unless
+ * CONFIG_INPUT_CST92XX_POLLMODE is enabled.
+ * reset - Drive the RST pin: true asserts reset (pin low), false
+ * releases it. Optional; may be NULL if RST is not wired.
+ */
+
+ CODE int (*attach)(FAR const struct cst92xx_config_s *config,
+ xcpt_t isr, FAR void *arg);
+ CODE void (*enable)(FAR const struct cst92xx_config_s *config,
+ bool enable);
+ CODE void (*reset)(FAR const struct cst92xx_config_s *config,
+ bool assert);
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Name: cst92xx_register
+ *
+ * Description:
+ * Reset and probe the CST92xx over I2C, configure the touchscreen lower
+ * half and register it as /dev/inputN where N is the given minor number.
+ *
+ * Input Parameters:
+ * i2c - I2C master used to talk to the controller
+ * config - Persistent board configuration
+ * minor - Device minor number used to form /dev/inputN
+ *
+ * Returned Value:
+ * Zero (OK) on success. Otherwise, a negated errno value is returned to
+ * indicate the nature of the failure.
+ *
+ ****************************************************************************/
+
+int cst92xx_register(FAR struct i2c_master_s *i2c,
+ FAR const struct cst92xx_config_s *config, int minor);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CONFIG_INPUT_CST92XX */
+#endif /* __INCLUDE_NUTTX_INPUT_CST92XX_H */