From c62a9ea727b99559903397ee78e757009b9a5124 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 17 Dec 2017 15:17:06 -0600 Subject: [PATCH] drivers/input/ft5x06.c: Add a driver for the FT5x06 capacitive, multi-touch, touchscreen controller. configs/lpcxpresso-lpc54628: Add support for the the FT5x06. Enable the driver as well as the apps/examples touchscreen test. Untested on initial commit. --- configs/lpcxpresso-lpc54628/README.txt | 4 + .../lpcxpresso-lpc54628/src/lpc54_ft5x06.c | 246 ++++ configs/lpcxpresso-lpc54628/src/lpc54_i2c.c | 107 ++ .../lpcxpresso-lpc54628/src/lpc54_i2ctool.c | 132 +++ drivers/input/ft5x06.c | 1003 +++++++++++++++++ drivers/input/ft5x06.h | 196 ++++ include/nuttx/input/ft5x06.h | 182 +++ 7 files changed, 1870 insertions(+) create mode 100644 configs/lpcxpresso-lpc54628/src/lpc54_ft5x06.c create mode 100644 configs/lpcxpresso-lpc54628/src/lpc54_i2c.c create mode 100644 configs/lpcxpresso-lpc54628/src/lpc54_i2ctool.c create mode 100644 drivers/input/ft5x06.c create mode 100644 drivers/input/ft5x06.h create mode 100644 include/nuttx/input/ft5x06.h diff --git a/configs/lpcxpresso-lpc54628/README.txt b/configs/lpcxpresso-lpc54628/README.txt index baaa7ddcb18..6b3c81b5fdf 100644 --- a/configs/lpcxpresso-lpc54628/README.txt +++ b/configs/lpcxpresso-lpc54628/README.txt @@ -57,6 +57,10 @@ STATUS are processed. This, I suspect, is a consequence of the strong glitch filtering that is enbled in the pin configuration. Snappier response my be obtainble with filtering off. + 2017-12-17: Added a driver for the FT5x06 capacitive, multi-touch + controller. Add support logic for the LPCXpresso-LPC54528 to + initialize and the register the FT5x06 driver. Untested on initial + commit Configurations ============== diff --git a/configs/lpcxpresso-lpc54628/src/lpc54_ft5x06.c b/configs/lpcxpresso-lpc54628/src/lpc54_ft5x06.c new file mode 100644 index 00000000000..4fd722243fa --- /dev/null +++ b/configs/lpcxpresso-lpc54628/src/lpc54_ft5x06.c @@ -0,0 +1,246 @@ +/**************************************************************************** + * configs/lpcxpresso-lpc54628/src/lpc54_ft5x06.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include + +#include "lpc54_config.h" +#include "lpc54_gpio.h" +#include "lpcxpresso-lpc54628.h" + +#ifdef HAVE_FT5x06 + +/**************************************************************************** + * Pre-processor Defintions + ****************************************************************************/ + +#define FT5x06_FREQUENCY 400000 /* For now, will boost later */ + +/**************************************************************************** + * Private Function Ptototypes + ****************************************************************************/ + +static int lpc54_ft5x06_attach(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc, xcpt_t isr, FAR void *arg); +static void lpc54_ft5x06_enable(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc, bool enable); +static void lpc54_ft5x06_clear(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc); +static void lpc54_ft5x06_nreset(FAR const struct ft5x06_config_s *config, + bool state); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct ft5x06_config_s g_ft5x06_config = +{ + .address = FT5x06_I2C_ADDRESS, + .frequency = FT5x06_FREQUENCY, + .attach = lpc54_ft5x06_attach, + .enable = lpc54_ft5x06_enable, + .clear = lpc54_ft5x06_clear, + .nreset = lpc54_ft5x06_nreset +}; + +static uint8_t g_ft5x06_irq; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lpc54_ft5x06_attach + * + * Description: + * Attach an FT5x06 interrupt handler to a GPIO interrupt + * + ****************************************************************************/ + +static int lpc54_ft5x06_attach(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc, xcpt_t isr, + FAR void *arg) +{ + if (irqsrc == FT5X06_DATA_SOURCE) + { + return irq_attach(g_ft5x06_irq, isr, arg); + } + else + { + return -ENOSYS; + } +} + +/**************************************************************************** + * Name: lpc54_ft5x06_enable + * + * Description: + * Enable or disable a GPIO interrupt + * + ****************************************************************************/ + +static void lpc54_ft5x06_enable(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc, bool enable) +{ + if (irqsrc == FT5X06_DATA_SOURCE) + { + up_enable_irq(g_ft5x06_irq); + } +} + +/**************************************************************************** + * Name: lpc54_ft5x06_clear + * + * Description: + * Acknowledge/clear any pending GPIO interrupt + * + ****************************************************************************/ + +static void lpc54_ft5x06_clear(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc) +{ + if (irqsrc == FT5X06_DATA_SOURCE) + { + (void)lpc54_gpio_ackedge(g_ft5x06_irq); + } +} + +/**************************************************************************** + * Name: lpc54_ft5x06_nreset + * + * Description: + * Control the chip reset pin (active low) + * + ****************************************************************************/ + +static void lpc54_ft5x06_nreset(FAR const struct ft5x06_config_s *config, + bool nstate) +{ + lpc54_gpio_write(GPIO_FT5x06_CTRSTn, nstate); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lpc54_ft5x06_register + * + * Description: + * Register the FT5x06 touch panel driver + * + ****************************************************************************/ + +int lpc54_ft5x06_register(void) +{ + FAR struct i2c_master_s *i2c; + int irq; + int ret; + + /* Initialize GPIO pins. NOTE: The nRST pin was already configured during + * early LCD initialization. The Part is in reset now. + */ + + lpc54_gpio_config(GPIO_FT5x06_INTR); + irq = lpc54_gpio_irqno(GPIO_FT5x06_INTR); + DEBUGASSERT(irq > 0 && irq < UINT8_MAX); + g_ft5x06_irq = (uint8_t)irq; + + /* Make sure that the interrupt is disabled at the NVIC */ + + lpc54_gpio_ackedge(irq); + up_disable_irq(irq); + + /* Take the FT5x06 out of reset */ + + lpc54_gpio_write(GPIO_FT5x06_CTRSTn, true); + + /* The FT5x06 is on I2C2. Get the handle and register the F5x06 device */ + + i2c = lpc54_i2c_handle(2, I2C2NDX); + if (i2c == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to get I2C2 interface\n"); + return -ENODEV; + } + else + { + ret = ft5x06_register(i2c, &g_ft5x06_config, 0); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to register FT5x06 driver: %d\n", + ret); + + lpc54_gpio_write(GPIO_FT5x06_CTRSTn, false); + lpc54_i2c_free(I2C2NDX); + return ret; + } + } + + return OK; +} + +/**************************************************************************** + * Name: board_tsc_setup and board_tsc_teardown + * + * Description: + * Stubs for expected interfaces. This implementation does not permit the + * application to mange the touch screen controller. + * + ****************************************************************************/ + +#ifdef CONFIG_BOARDCTL_TSCTEST +int board_tsc_setup(int minor) +{ + DEBUGASSERT(minor == 0); + return OK; +} + +void board_tsc_teardown(void) +{ +} +#endif + +#endif /* HAVE_FT5x06*/ diff --git a/configs/lpcxpresso-lpc54628/src/lpc54_i2c.c b/configs/lpcxpresso-lpc54628/src/lpc54_i2c.c new file mode 100644 index 00000000000..56c0a0bf667 --- /dev/null +++ b/configs/lpcxpresso-lpc54628/src/lpc54_i2c.c @@ -0,0 +1,107 @@ +/**************************************************************************** + * configs/lpcxpresso-lpc54628/src/lpc54_i2c.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include + +#include "lpc54_config.h" +#include "lpc54_i2c_master.h" +#include "lpcxpresso-lpc54628.h" + +#if defined(HAVE_I2CTOOL) || defined(HAVE_FT5x06) + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static FAR struct i2c_master_s *g_i2c_handle[NI2C]; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lpc54_i2c_handle + * + * Description: + * Create (or reuse) an I2C handle + * + ****************************************************************************/ + +FAR struct i2c_master_s *lpc54_i2c_handle(int bus, int ndx) +{ + FAR struct i2c_master_s *i2c = g_i2c_handle[ndx]; + + if (i2c == NULL) + { + i2c = lpc54_i2cbus_initialize(bus); + if (i2c == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to get I2C%d interface\n", bus); + } + else + { + g_i2c_handle[ndx] = i2c; + } + } + + return i2c; +} + +/**************************************************************************** + * Name: lpc54_i2c_free + * + * Description: + * Free an I2C handle created by lpc54_i2c_handle + * + ****************************************************************************/ + +void lpc54_i2c_free(int ndx) +{ + if (g_i2c_handle[ndx] != NULL) + { + lpc54_i2cbus_uninitialize(g_i2c_handle[ndx]); + g_i2c_handle[ndx] = NULL; + } +} + +#endif /* HAVE_I2CTOOL || HAVE_FT5x06*/ diff --git a/configs/lpcxpresso-lpc54628/src/lpc54_i2ctool.c b/configs/lpcxpresso-lpc54628/src/lpc54_i2ctool.c new file mode 100644 index 00000000000..f9f3c2500a7 --- /dev/null +++ b/configs/lpcxpresso-lpc54628/src/lpc54_i2ctool.c @@ -0,0 +1,132 @@ +/**************************************************************************** + * configs/lpcxpresso-lpc54628/src/lpc54_i2ctool.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include + +#include "lpc54_config.h" +#include "lpc54_i2c_master.h" +#include "lpcxpresso-lpc54628.h" + +#ifdef HAVE_I2CTOOL + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lpc54_i2c_register + * + * Description: + * Register one I2C drivers for the I2C tool. + * + ****************************************************************************/ + +static void lpc54_i2c_register(int bus, int ndx) +{ + FAR struct i2c_master_s *i2c; + int ret; + + i2c = lpc54_i2c_handle(bus, ndx); + if (i2c == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to get I2C%d interface\n", bus); + } + else + { + ret = i2c_register(i2c, bus); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to register I2C%d driver: %d\n", + bus, ret); + lpc54_i2cbus_uninitialize(i2c); + } + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lpc54_i2ctool + * + * Description: + * Register I2C drivers for the I2C tool. + * + ****************************************************************************/ + +void lpc54_i2ctool(void) +{ +#ifdef CONFIG_LPC54_I2C0_MASTER + lpc54_i2c_register(0, I2C0NDX); +#endif +#ifdef CONFIG_LPC54_I2C1_MASTER + lpc54_i2c_register(1, I2C1NDX); +#endif +#ifdef CONFIG_LPC54_I2C2_MASTER + lpc54_i2c_register(2, I2C2NDX); +#endif +#ifdef CONFIG_LPC54_I2C3_MASTER + lpc54_i2c_register(3, I2C3NDX); +#endif +#ifdef CONFIG_LPC54_I2C4_MASTER + lpc54_i2c_register(4, I2C4NDX); +#endif +#ifdef CONFIG_LPC54_I2C5_MASTER + lpc54_i2c_register(5, I2C5NDX); +#endif +#ifdef CONFIG_LPC54_I2C6_MASTER + lpc54_i2c_register(6, I2C6NDX); +#endif +#ifdef CONFIG_LPC54_I2C7_MASTER + lpc54_i2c_register(7, I2C7NDX); +#endif +#ifdef CONFIG_LPC54_I2C8_MASTER + lpc54_i2c_register(8, I2C8NDX); +#endif +#ifdef CONFIG_LPC54_I2C9_MASTER + lpc54_i2c_register(9, I2C9NDX); +#endif +} + +#endif /* HAVE_I2CTOOL */ diff --git a/drivers/input/ft5x06.c b/drivers/input/ft5x06.c new file mode 100644 index 00000000000..ac13d88ceb7 --- /dev/null +++ b/drivers/input/ft5x06.c @@ -0,0 +1,1003 @@ +/**************************************************************************** + * drivers/input/ft5x06.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * References: + * "FT5x06", FocalTech Systems Co., Ltd, D-FT5x06-1212-V4.0, Revised + * Dec. 18, 2012 + * + * Some of this driver was developed with input from NXP sample code for + * the LPCXpresso-LPC54628 baord. That sample code as a compatible BSD + * license: + * + * Copyright (c) 2016, Freescale Semiconductor, Inc. + * Copyright 2016-2017 NXP + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/* The FT5x06 Series ICs are single-chip capacitive touch panel controller + * ICs with a built-in 8 bit Micro-controller unit (MCU). They adopt the + * mutual capacitance approach, which supports true multi-touch capability. + * In conjunction with a mutual capacitive touch panel, the FT5x06 have + * user-friendly input functions, which can be applied on many portable + * devices, such as cellular phones, MIDs, netbook and notebook personal + * computers. + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "ft5x06.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Driver support ***********************************************************/ +/* This format is used to construct the /dev/input[n] device driver path. It + * defined here so that it will be used consistently in all places. + */ + +#define DEV_FORMAT "/dev/input%d" +#define DEV_NAMELEN 16 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* This structure describes the state of one FT5x06 driver instance */ + +struct ft5x06_dev_s +{ + uint8_t crefs; /* Number of times the device + * has been opened */ + uint8_t nwaiters; /* Number of threads waiting for + * FT5x06 data */ + volatile bool valid; /* True: New, valid touch data in + * touchbuf[] */ + sem_t devsem; /* Manages exclusive access to this + * structure */ + sem_t waitsem; /* Used to wait for the + * availability of data */ + uint32_t frequency; /* Current I2C frequency */ + + FAR const struct ft5x06_config_s *config; /* Board configuration data */ + FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ + struct work_s work; /* Supports the interrupt handling + * "bottom half" */ + uint8_t touchbuf[FT5x06_TOUCH_DATA_LEN]; /* Raw touch data */ + +#ifndef CONFIG_DISABLE_POLL + /* The following is a list if poll structures of threads waiting for + * driver events. The 'struct pollfd' reference for each open is also + * retained in the f_priv field of the 'struct file'. + */ + + struct pollfd *fds[CONFIG_FT5X06_NPOLLWAITERS]; +#endif +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void ft5x06_notify(FAR struct ft5x06_dev_s *priv); +static void ft5x06_data_worker(FAR void *arg); +static int ft5x06_data_interrupt(int irq, FAR void *context, FAR void *arg); +static ssize_t ft5x06_sample(FAR struct ft5x06_dev_s *priv, FAR char *buffer, + size_t len); +static ssize_t ft5x06_waitsample(FAR struct ft5x06_dev_s *priv, + FAR char *buffer, size_t len); +static int ft5x06_bringup(FAR struct ft5x06_dev_s *priv); +static void ft5x06_shutdown(FAR struct ft5x06_dev_s *priv); + +/* Character driver methods */ + +static int ft5x06_open(FAR struct file *filep); +static int ft5x06_close(FAR struct file *filep); +static ssize_t ft5x06_read(FAR struct file *filep, FAR char *buffer, + size_t len); +static int ft5x06_ioctl(FAR struct file *filep, int cmd, + unsigned long arg); +#ifndef CONFIG_DISABLE_POLL +static int ft5x06_poll(FAR struct file *filep, struct pollfd *fds, + bool setup); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* This the vtable that supports the character driver interface */ + +static const struct file_operations ft5x06_fops = +{ + ft5x06_open, /* open */ + ft5x06_close, /* close */ + ft5x06_read, /* read */ + NULL, /* write */ + NULL, /* seek */ + ft5x06_ioctl /* ioctl */ +#ifndef CONFIG_DISABLE_POLL + , ft5x06_poll /* poll */ +#endif +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + , NULL /* unlink */ +#endif +}; + +/* Maps FT5x06 touch events into bit encoded representation used by NuttX */ + +static const uint8_t g_event_map[4] = +{ + (TOUCH_DOWN | TOUCH_ID_VALID | TOUCH_POS_VALID), /* FT5x06_DOWN */ + (TOUCH_UP | TOUCH_ID_VALID | TOUCH_POS_VALID), /* FT5x06_UP */ + (TOUCH_MOVE | TOUCH_ID_VALID | TOUCH_POS_VALID), /* FT5x06_CONTACT */ + TOUCH_ID_VALID /* FT5x06_INVALID */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ft5x06_notify + ****************************************************************************/ + +static void ft5x06_notify(FAR struct ft5x06_dev_s *priv) +{ +#ifndef CONFIG_DISABLE_POLL + int i; +#endif + + /* If there are threads waiting for read data, then signal one of them + * that the read data is available. + */ + + if (priv->nwaiters > 0) + { + /* After posting this semaphore, we need to exit because the FT5x06 + * is no longer available. + */ + + nxsem_post(&priv->waitsem); + } + +#ifndef CONFIG_DISABLE_POLL + /* If there are threads waiting on poll() for FT5x06 data to become available, + * then wake them up now. NOTE: we wake up all waiting threads because we + * do not know that they are going to do. If they all try to read the data, + * then some make end up blocking after all. + */ + + for (i = 0; i < CONFIG_FT5X06_NPOLLWAITERS; i++) + { + struct pollfd *fds = priv->fds[i]; + if (fds) + { + fds->revents |= POLLIN; + iinfo("Report events: %02x\n", fds->revents); + nxsem_post(fds->sem); + } + } +#endif +} + +/**************************************************************************** + * Name: ft5x06_data_worker + ****************************************************************************/ + +static void ft5x06_data_worker(FAR void *arg) +{ + FAR struct ft5x06_dev_s *priv = (FAR struct ft5x06_dev_s *)arg; + FAR const struct ft5x06_config_s *config; + struct i2c_msg_s msg[2]; + uint8_t regaddr; + int ret; + + /* Get a pointer the callbacks for convenience */ + + DEBUGASSERT(priv != NULL && priv->config != NULL); + config = priv->config; + + /* We need to have exclusive access to the touchbuf so that we do not + * corrupt any read operation that is in place. + */ + + do + { + ret = nxsem_wait(&priv->devsem); + DEBUGASSERT(ret >= 0 || ret == -EINTR); + } + while (ret < 0); + + /* Read touch data */ + /* Set up the address write operation */ + + regaddr = FT5x06_TOUCH_DATA_STARTREG; + + msg[0].frequency = priv->frequency; /* I2C frequency */ + msg[0].addr = config->address; /* 7-bit address */ + msg[0].flags = 0; /* Write transaction with START */ + msg[0].buffer = ®addr; /* Send one byte of data (no STOP) */ + msg[0].length = 1; + + /* Set up the data read operation */ + + msg[0].frequency = priv->frequency; /* I2C frequency */ + msg[0].addr = config->address; /* 7-bit address */ + msg[0].flags = I2C_M_READ; /* Read transaction with Re-START */ + msg[0].buffer = priv->touchbuf; /* Read all touch data */ + msg[0].length = FT5x06_TOUCH_DATA_LEN; + + + ret = I2C_TRANSFER(priv->i2c, msg, 2); + if (ret >= 0) + { + /* Notify any waiters that new FT5x06 data is available */ + + priv->valid = true; + ft5x06_notify(priv); + } + + /* Exit, re-enabling FT5x06 interrupts */ + + config->enable(config, FT5X06_DATA_SOURCE, true); + nxsem_post(&priv->devsem); +} + +/**************************************************************************** + * Name: ft5x06_data_interrupt + ****************************************************************************/ + +static int ft5x06_data_interrupt(int irq, FAR void *context, FAR void *arg) +{ + FAR struct ft5x06_dev_s *priv = (FAR struct ft5x06_dev_s *)arg; + FAR const struct ft5x06_config_s *config; + int ret; + + /* Get a pointer the callbacks for convenience (and so the code is not so + * ugly). + */ + + config = priv->config; + DEBUGASSERT(config != NULL); + + /* Disable further interrupts */ + + config->enable(config, FT5X06_DATA_SOURCE, false); + + /* Transfer processing to the worker thread. Since FT5x06 interrupts are + * disabled while the work is pending, no special action should be required + * to protected the work queue. + */ + + DEBUGASSERT(priv->work.worker == NULL); + ret = work_queue(HPWORK, &priv->work, ft5x06_data_worker, priv, 0); + if (ret != 0) + { + ierr("ERROR: Failed to queue work: %d\n", ret); + } + + /* Clear any pending interrupts and return success */ + + config->clear(config, FT5X06_DATA_SOURCE); + return OK; +} + +/**************************************************************************** + * Name: ft5x06_sample + ****************************************************************************/ + +static ssize_t ft5x06_sample(FAR struct ft5x06_dev_s *priv, FAR char *buffer, + size_t len) +{ + FAR struct ft5x06_touch_data_s *raw; + FAR struct ft5x06_touch_point_s *touch; + FAR struct touch_sample_s *sample; + FAR struct touch_point_s *point; + unsigned int ntouches; + unsigned int maxtouches; + int i; + + maxtouches = (len - sizeof(int)) / sizeof(struct touch_point_s); + DEBUGASSERT(maxtouches > 0); /* Already verified */ + + if (!priv->valid) + { + return 0; /* Nothing to read */ + } + + /* Raw data pointers (source) */ + + raw = (FAR struct ft5x06_touch_data_s *)priv->touchbuf; + touch = raw->touch; + + /* Decode number of touches */ + + ntouches = raw->tdstatus; + if (ntouches > maxtouches) + { + ntouches = maxtouches; + } + + /* User data buffer points (sink) */ + + sample = (FAR struct touch_sample_s *)buffer; + point = sample->point; + + /* Return the number of touches read */ + + sample->npoints = ntouches; + + /* Decode and return the touch points */ + + for (i = 0; i < raw->tdstatus; i++) + { + int event = TOUCH_POINT_GET_EVENT(touch[i]); + + point[i].id = TOUCH_POINT_GET_ID(touch[i]); + point[i].flags = g_event_map[event]; + point[i].x = TOUCH_POINT_GET_X(touch[i]); + point[i].y = TOUCH_POINT_GET_Y(touch[i]); + point[i].h = 0; + point[i].w = 0; + point[i].pressure = 0; + } + + priv->valid = false; + return SIZEOF_TOUCH_SAMPLE_S(ntouches); +} + +/**************************************************************************** + * Name: ft5x06_waitsample + ****************************************************************************/ + +static ssize_t ft5x06_waitsample(FAR struct ft5x06_dev_s *priv, + FAR char *buffer, size_t len) +{ + int ret; + + /* Disable pre-emption to prevent other threads from getting control while + * we muck with the semaphores. + */ + + sched_lock(); + + /* Now release the semaphore that manages mutually exclusive access to + * the device structure. This may cause other tasks to become ready to + * run, but they cannot run yet because pre-emption is disabled. + */ + + nxsem_post(&priv->devsem); + + /* Try to get the a sample... if we cannot, then wait on the semaphore + * that is posted when new sample data is available. + */ + + while (!priv->valid) + { + /* Wait for a change in the FT5x06 state */ + + priv->nwaiters++; + ret = nxsem_wait(&priv->waitsem); + priv->nwaiters--; + + if (ret < 0) + { + /* If we are awakened by a signal, then we need to return + * the failure now. + */ + + ierr("ERROR: nxsem_wait failed: %d\n", ret); + DEBUGASSERT(ret == -EINTR); + goto errout; + } + } + + /* Re-acquire the semaphore that manages mutually exclusive access to + * the device structure. We may have to wait here. But we have our sample. + * Interrupts and pre-emption will be re-enabled while we wait. + */ + + ret = nxsem_wait(&priv->devsem); + if (ret >= 0) + { + /* Now sample the data. + * + * REVISIT: Is it safe to assume that priv->valid will always be + * true? I think that sched_lock() whould protect the setting. + */ + + ret = ft5x06_sample(priv, buffer, len); + } + +errout: + /* Restore pre-emption. We might get suspended here but that is okay + * because we already have our sample. Note: this means that if there + * were two threads reading from the FT5x06 for some reason, the data + * might be read out of order. + */ + + sched_unlock(); + return ret; +} + +/**************************************************************************** + * Name: ft5x06_bringup + ****************************************************************************/ + +static int ft5x06_bringup(FAR struct ft5x06_dev_s *priv) +{ + FAR const struct ft5x06_config_s *config; + struct i2c_msg_s msg; + uint8_t data[2]; + int ret; + + /* Get a pointer the callbacks for convenience (and so the code is not so + * ugly). + */ + + config = priv->config; + DEBUGASSERT(config != NULL); + + /* Set device mode to normal operation */ + + data[0] = FT5x06_TOUCH_MODE_REG; /* Register address */ + data[1] = FT5x06_DEV_MODE_WORKING; /* Normal mode */ + + msg.frequency = priv->frequency; /* I2C frequency */ + msg.addr = config->address; /* 7-bit address */ + msg.flags = 0; /* Write transaction with START */ + msg.buffer = data; /* Send two bytes followed by STOP */ + msg.length = 2; + + ret = I2C_TRANSFER(priv->i2c, &msg, 1); + if (ret < 0) + { + return ret; + } + + /* Enable FT5x06 interrupts */ + + config->clear(config, FT5X06_DATA_SOURCE); + config->enable(config, FT5X06_DATA_SOURCE, true); + return OK; +} + +/**************************************************************************** + * Name: ft5x06_shutdown + ****************************************************************************/ + +static void ft5x06_shutdown(FAR struct ft5x06_dev_s *priv) +{ + FAR const struct ft5x06_config_s *config = priv->config; + + /* Make sure that interrupts are disabled */ + + config->clear(config, FT5X06_DATA_SOURCE); + config->enable(config, FT5X06_DATA_SOURCE, false); + + config->clear(config, FT5X06_WAKE_SOURCE); + config->enable(config, FT5X06_WAKE_SOURCE, false); + + /* Attach the interrupt handler */ + + (void)config->attach(config, FT5X06_DATA_SOURCE, NULL, NULL); +} + +/**************************************************************************** + * Name: ft5x06_open + ****************************************************************************/ + +static int ft5x06_open(FAR struct file *filep) +{ + FAR struct inode *inode; + FAR struct ft5x06_dev_s *priv; + uint8_t tmp; + int ret; + + DEBUGASSERT(filep); + inode = filep->f_inode; + + DEBUGASSERT(inode && inode->i_private); + priv = (FAR struct ft5x06_dev_s *)inode->i_private; + + /* Get exclusive access to the driver data structure */ + + ret = nxsem_wait(&priv->devsem); + if (ret < 0) + { + /* This should only happen if the wait was cancelled by an signal */ + + ierr("ERROR: nxsem_wait failed: %d\n", ret); + DEBUGASSERT(ret == -EINTR); + return ret; + } + + /* Increment the reference count */ + + tmp = priv->crefs + 1; + if (tmp == 0) + { + /* More than 255 opens; uint8_t overflows to zero */ + + ret = -EMFILE; + goto errout_with_sem; + } + + /* When the reference increments to 1, this is the first open event + * on the driver.. and the time when we must initialize the driver. + */ + + if (tmp == 1) + { + ret = ft5x06_bringup(priv); + if (ret < 0) + { + ierr("ERROR: nxsem_wait failed: %d\n", ret); + goto errout_with_sem; + } + } + + /* Save the new open count on success */ + + priv->crefs = tmp; + +errout_with_sem: + nxsem_post(&priv->devsem); + return ret; +} + +/**************************************************************************** + * Name: ft5x06_close + ****************************************************************************/ + +static int ft5x06_close(FAR struct file *filep) +{ + FAR struct inode *inode; + FAR struct ft5x06_dev_s *priv; + int ret; + + DEBUGASSERT(filep); + inode = filep->f_inode; + + DEBUGASSERT(inode && inode->i_private); + priv = (FAR struct ft5x06_dev_s *)inode->i_private; + + /* Get exclusive access to the driver data structure */ + + ret = nxsem_wait(&priv->devsem); + if (ret < 0) + { + /* This should only happen if the wait was cancelled by an signal */ + + ierr("ERROR: nxsem_wait failed: %d\n", ret); + DEBUGASSERT(ret == -EINTR); + return ret; + } + + /* Decrement the reference count unless it would decrement a negative + * value. + */ + + if (priv->crefs >= 1) + { + priv->crefs--; + } + + /* When the count decrements to zero, there are no further open references + * to the driver and it can be uninitialized. + */ + + if (priv->crefs == 0) + { + ft5x06_shutdown(priv); + } + + nxsem_post(&priv->devsem); + return OK; +} + +/**************************************************************************** + * Name: ft5x06_read + ****************************************************************************/ + +static ssize_t ft5x06_read(FAR struct file *filep, FAR char *buffer, + size_t len) +{ + FAR struct inode *inode; + FAR struct ft5x06_dev_s *priv; + int ret; + + DEBUGASSERT(filep); + inode = filep->f_inode; + + DEBUGASSERT(inode && inode->i_private); + priv = (FAR struct ft5x06_dev_s *)inode->i_private; + + /* Verify that the caller has provided a buffer large enough to receive + * the touch data. + */ + + if (len < SIZEOF_TOUCH_SAMPLE_S(1)) + { + /* We could provide logic to break up a touch report into segments and + * handle smaller reads... but why? + */ + + return -ENOSYS; + } + + /* Get exclusive access to the driver data structure */ + + ret = nxsem_wait(&priv->devsem); + if (ret < 0) + { + /* This should only happen if the wait was cancelled by an signal */ + + ierr("ERROR: nxsem_wait failed: %d\n", ret); + DEBUGASSERT(ret == -EINTR); + return ret; + } + + /* Try to read sample data. */ + + ret = ft5x06_sample(priv, buffer, len); + while (ret == 0) + { + /* Sample data is not available now. We would have to wait to receive + * sample data. If the user has specified the O_NONBLOCK option, then + * just return an error. + */ + + if (filep->f_oflags & O_NONBLOCK) + { + ret = -EAGAIN; + goto errout; + } + + /* Wait for sample data */ + + ret = ft5x06_waitsample(priv, buffer, len); + if (ret < 0) + { + /* We might have been awakened by a signal */ + + goto errout; + } + } + + ret = SIZEOF_TOUCH_SAMPLE_S(1); + +errout: + nxsem_post(&priv->devsem); + return ret; +} + +/**************************************************************************** + * Name:ft5x06_ioctl + ****************************************************************************/ + +static int ft5x06_ioctl(FAR struct file *filep, int cmd, unsigned long arg) +{ + FAR struct inode *inode; + FAR struct ft5x06_dev_s *priv; + int ret; + + iinfo("cmd: %d arg: %ld\n", cmd, arg); + DEBUGASSERT(filep); + inode = filep->f_inode; + + DEBUGASSERT(inode && inode->i_private); + priv = (FAR struct ft5x06_dev_s *)inode->i_private; + + /* Get exclusive access to the driver data structure */ + + ret = nxsem_wait(&priv->devsem); + if (ret < 0) + { + /* This should only happen if the wait was cancelled by an signal */ + + ierr("ERROR: nxsem_wait failed: %d\n", ret); + DEBUGASSERT(ret == -EINTR); + return ret; + } + + /* Process the IOCTL by command */ + + switch (cmd) + { + case TSIOC_SETFREQUENCY: /* arg: Pointer to uint32_t frequency value */ + { + FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg); + DEBUGASSERT(priv->config != NULL && ptr != NULL); + priv->frequency = *ptr; + } + break; + + case TSIOC_GETFREQUENCY: /* arg: Pointer to uint32_t frequency value */ + { + FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg); + DEBUGASSERT(priv->config != NULL && ptr != NULL); + *ptr = priv->frequency; + } + break; + + default: + ret = -ENOTTY; + break; + } + + nxsem_post(&priv->devsem); + return ret; +} + +/**************************************************************************** + * Name: ft5x06_poll + ****************************************************************************/ + +#ifndef CONFIG_DISABLE_POLL +static int ft5x06_poll(FAR struct file *filep, FAR struct pollfd *fds, + bool setup) +{ + FAR struct inode *inode; + FAR struct ft5x06_dev_s *priv; + int ret; + int i; + + iinfo("setup: %d\n", (int)setup); + DEBUGASSERT(filep && fds); + inode = filep->f_inode; + + DEBUGASSERT(inode && inode->i_private); + priv = (FAR struct ft5x06_dev_s *)inode->i_private; + + /* Are we setting up the poll? Or tearing it down? */ + + ret = nxsem_wait(&priv->devsem); + if (ret < 0) + { + /* This should only happen if the wait was cancelled by an signal */ + + ierr("ERROR: nxsem_wait failed: %d\n", ret); + DEBUGASSERT(ret == -EINTR); + return ret; + } + + if (setup) + { + /* Ignore waits that do not include POLLIN */ + + if ((fds->events & POLLIN) == 0) + { + ierr("ERROR: Missing POLLIN: revents: %08x\n", fds->revents); + ret = -EDEADLK; + goto errout; + } + + /* This is a request to set up the poll. Find an available + * slot for the poll structure reference + */ + + for (i = 0; i < CONFIG_FT5X06_NPOLLWAITERS; i++) + { + /* Find an available slot */ + + if (!priv->fds[i]) + { + /* Bind the poll structure and this slot */ + + priv->fds[i] = fds; + fds->priv = &priv->fds[i]; + break; + } + } + + if (i >= CONFIG_FT5X06_NPOLLWAITERS) + { + ierr("ERROR: No available slot found: %d\n", i); + fds->priv = NULL; + ret = -EBUSY; + goto errout; + } + + /* Should we immediately notify on any of the requested events? */ + + if (priv->valid) + { + ft5x06_notify(priv); + } + } + else if (fds->priv) + { + /* This is a request to tear down the poll. */ + + struct pollfd **slot = (struct pollfd **)fds->priv; + DEBUGASSERT(slot != NULL); + + /* Remove all memory of the poll setup */ + + *slot = NULL; + fds->priv = NULL; + } + +errout: + nxsem_post(&priv->devsem); + return ret; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ft5x06_register + * + * Description: + * Configure the FT5x06 to use the provided I2C device instance. This + * will register the driver as /dev/inputN where N is the minor device + * number + * + * Input Parameters: + * dev - An I2C driver instance + * config - Persistant board configuration data + * minor - The input device minor number + * + * Returned Value: + * Zero is returned on success. Otherwise, a negated errno value is + * returned to indicate the nature of the failure. + * + ****************************************************************************/ + +int ft5x06_register(FAR struct i2c_master_s *i2c, + FAR const struct ft5x06_config_s *config, int minor) +{ + FAR struct ft5x06_dev_s *priv; + char devname[DEV_NAMELEN]; + int ret; + + iinfo("i2c: %p minor: %d\n", i2c, minor); + + /* Debug-only sanity checks */ + + DEBUGASSERT(i2c != NULL && config != NULL && minor >= 0 && minor < 100); + DEBUGASSERT(config->attach != NULL && config->enable != NULL && + config->clear != NULL && config->nreset != NULL); + + /* Create and initialize a FT5x06 device driver instance */ + + priv = (FAR struct ft5x06_dev_s *)kmm_zalloc(sizeof(struct ft5x06_dev_s)); + if (!priv) + { + ierr("ERROR: kmm_malloc(%d) failed\n", sizeof(struct ft5x06_dev_s)); + return -ENOMEM; + } + + /* Initialize the FT5x06 device driver instance */ + + priv->i2c = i2c; /* Save the I2C device handle */ + priv->config = config; /* Save the board configuration */ + priv->frequency = config->frequency; /* Set the current I2C frequency */ + + nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ + nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ + + /* The event wait semaphore is used for signaling and, hence, should not + * have priority inheritance enabled. + */ + + nxsem_setprotocol(&priv->waitsem, SEM_PRIO_NONE); + + /* Make sure that interrupts are disabled */ + + config->clear(config, FT5X06_DATA_SOURCE); + config->enable(config, FT5X06_DATA_SOURCE, false); + + config->clear(config, FT5X06_WAKE_SOURCE); + config->enable(config, FT5X06_WAKE_SOURCE, false); + + /* Attach the interrupt handler */ + + ret = config->attach(config, FT5X06_DATA_SOURCE, ft5x06_data_interrupt, + priv); + if (ret < 0) + { + ierr("ERROR: Failed to attach interrupt\n"); + goto errout_with_priv; + } + + /* Register the device as an input device */ + + (void)snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor); + iinfo("Registering %s\n", devname); + + ret = register_driver(devname, &ft5x06_fops, 0666, priv); + if (ret < 0) + { + ierr("ERROR: register_driver() failed: %d\n", ret); + goto errout_with_priv; + } + + /* Schedule work to perform the initial sampling and to set the data + * availability conditions. + */ + + ret = work_queue(HPWORK, &priv->work, ft5x06_data_worker, priv, 0); + if (ret != 0) + { + ierr("ERROR: Failed to queue work: %d\n", ret); + goto errout_with_priv; + } + + /* And return success */ + + return OK; + +errout_with_priv: + nxsem_destroy(&priv->devsem); + kmm_free(priv); + return ret; +} diff --git a/drivers/input/ft5x06.h b/drivers/input/ft5x06.h new file mode 100644 index 00000000000..b309f8dc1f0 --- /dev/null +++ b/drivers/input/ft5x06.h @@ -0,0 +1,196 @@ +/**************************************************************************** + * drivers/input/ft5x06.h + * + * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * References: + * "FT5x06", FocalTech Systems Co., Ltd, D-FT5x06-1212-V4.0, Revised + * Dec. 18, 2012 + * + * Some of this driver was developed with input from NXP sample code for + * the LPCXpresso-LPC54628 baord. That sample code as a compatible BSD + * license: + * + * Copyright (c) 2016, Freescale Semiconductor, Inc. + * Copyright 2016-2017 NXP + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/* The FT5x06 Series ICs are single-chip capacitive touch panel controller + * ICs with a built-in 8 bit Micro-controller unit (MCU). They adopt the + * mutual capacitance approach, which supports true multi-touch capability. + * In conjunction with a mutual capacitive touch panel, the FT5x06 have + * user-friendly input functions, which can be applied on many portable + * devices, such as cellular phones, MIDs, netbook and notebook personal + * computers. + */ + +#ifndef __DRIVERS_INPUT_FT5X06_H +#define __DRIVERS_INPUT_FT5X06_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* WARNING: Some definitions may apply only to the FT5336 */ +/* FT5x06 maximum number of simultaneously detected touches. */ + +#define FT5x06_MAX_TOUCHES (5) + +/* FT5x06 raw touch data length. */ + +#define FT5x06_TOUCH_DATA_LEN (0x20) + +/* FT5x06 register addresses */ + +#define FT5x06_TOUCH_MODE_REG (0x00) /* Mode register */ +#define FT5x06_TOUCH_GESTID_REG (0x01) /* Gesture ID register */ +#define FT5x06_TOUCH_STAT_REG (0x02) /* Touch data status */ + /* See struct ft5x06_touch_point_s */ +#define FT5x06_TH_GROUP_REG (0x80) /* Threshold for touch detection */ +#define FT5x06_TH_DIFF_REG (0x85) /* Filter function coefficients */ +#define FT5x06_CTRL_REG (0x86) /* Control register */ +#define FT5x06_TIMEENTERMONITOR_REG (0x87) /* Time switching Active to Monitor */ +#define FT5x06_PERIODACTIVE_REG (0x88) /* Report rate in Active mode */ +#define FT5x06_PERIODMONITOR_REG (0x89) /* Report rate in Monitor mode */ +#define FT5x06_RADIAN_VALUE_REG (0x91) /* Minimum allowing angle */ +#define FT5x06_OFFSET_LEFT_RIGHT_REG (0x92) /* Mimimum offset */ +#define FT5x06_OFFSET_UP_DOWN_REG (0x93) /* Maximum offset */ +#define FT5x06_DISTANCE_LEFT_RIGHT_REG (0x94) /* Minimum distance */ +#define FT5x06_DISTANCE_UP_DOWN_REG (0x95) /* Minimum distance */ +#define FT5x06_DISTANCE_ZOOM_REG (0x96) /* Maximum distance */ +#define FT5x06_LIB_VER_H_REG (0xa1) /* MS LIB version */ +#define FT5x06_LIB_VER_L_REG (0xa2) /* LS LIB version */ +#define FT5x06_CIPHER_REG (0xa3) /* Chip selecting */ +#define FT5x06_GMODE_REG (0xa4) /* Interrupt mode */ +#define FT5x06_PWR_MODE_REG (0xa5) /* Power mode */ +#define FT5x06_FIRMID_REG (0xa6) /* Firmware version */ +#define FT5x06_CHIP_ID_REG (0xa8) /* Chip ID */ +#define FT5x06_RELEASE_CODE_ID_REG (0xaf) /* Release code version */ +#define FT5x06_STATE_REG (0xbc) /* Current operating mode */ + +#define FT5x06_TOUCH_DATA_STARTREG (1) /* Address where data begins */ + +/* Possible values of the DEV_MODE register */ + +#define FT5x06_DEV_MODE_WORKING (0x00) +#define FT5x06_DEV_MODE_FACTORY (0x04) + +/* Possible values of the GEST_ID register */ + +#define FT5x06_GEST_ID_NO_GESTURE (0x00) +#define FT5x06_GEST_ID_MOVE_UP (0x10) +#define FT5x06_GEST_ID_MOVE_RIGHT (0x14) +#define FT5x06_GEST_ID_MOVE_DOWN (0x18) +#define FT5x06_GEST_ID_MOVE_LEFT (0x1C) +#define FT5x06_GEST_ID_SINGLE_CLICK (0x20) +#define FT5x06_GEST_ID_DOUBLE_CLICK (0x22) +#define FT5x06_GEST_ID_ROTATE_CLOCKWISE (0x28) +#define FT5x06_GEST_ID_ROTATE_C_CLOCKWISE (0x29) +#define FT5x06_GEST_ID_ZOOM_IN (0x40) +#define FT5x06_GEST_ID_ZOOM_OUT (0x49) + +/* Values related to FT5x06_CTRL_REG */ + +#define FT5x06_CTRL_KEEP_ACTIVE_MODE (0x00) +#define FT5x06_CTRL_KEEP_AUTO_SWITCH_MONITOR_MODE (0x01) + +/* Possible values of FT5x06_GMODE_REG */ + +#define FT5x06_G_MODE_INTERRUPT_POLLING (0x00) +#define FT5x06_G_MODE_INTERRUPT_TRIGGER (0x01) + +/* Possible values of FT5x06_CHIP_ID_REG */ + +#define FT5x06_ID_VALUE (0x51) + +/* Operations on struct ft5x06_touch_point_s */ + +#define TOUCH_POINT_GET_EVENT(t) ((t).xh >> 6) +#define TOUCH_POINT_GET_ID(t) ((t).yh >> 4) +#define TOUCH_POINT_GET_X(t) ((((t).xh & 0x0f) << 8) | (t).xl) +#define TOUCH_POINT_GET_Y(t) ((((t).yh & 0x0f) << 8) | (t).yl) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +enum touch_event_e +{ + FT5x06_DOWN = 0, /* The state changed to touched */ + FT5x06_UP = 1, /* The state changed to not touched */ + FT5x06_CONTACT = 2, /* There is a continuous touch being detected */ + FT5x06_INVALID = 3 /* No touch information available */ +}; + +/* Describes on touchpoint returned by the FT5x06 */ + +struct ft5x06_touch_point_s +{ + uint8_t xh; + uint8_t xl; + uint8_t yh; + uint8_t yl; + uint8_t weight; + uint8_t area; +}; + +/* Describes all touch data returned by the FT5x06 */ + +struct ft5x06_touch_data_s +{ + uint8_t gestid; /* Gesture ID */ + uint8_t tdstatus; /* Touch status */ + struct ft5x06_touch_point_s touch[FT5x06_MAX_TOUCHES]; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __DRIVERS_INPUT_FT5X06_H */ diff --git a/include/nuttx/input/ft5x06.h b/include/nuttx/input/ft5x06.h new file mode 100644 index 00000000000..fb38ce00141 --- /dev/null +++ b/include/nuttx/input/ft5x06.h @@ -0,0 +1,182 @@ +/**************************************************************************** + * include/nuttx/input/ft5x06.h + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * References: + * "FT5x06", FocalTech Systems Co., Ltd, D-FT5x06-1212-V4.0, Revised + * Dec. 18, 2012 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/* The FT5x06 Series ICs are single-chip capacitive touch panel controller + * ICs with a built-in 8 bit Micro-controller unit (MCU). They adopt the + * mutual capacitance approach, which supports true multi-touch capability. + * In conjunction with a mutual capacitive touch panel, the FT5x06 have + * user-friendly input functions, which can be applied on many portable + * devices, such as cellular phones, MIDs, netbook and notebook personal + * computers. + */ + +#ifndef __INCLUDE_NUTTX_INPUT_FT5X06_H +#define __INCLUDE_NUTTX_INPUT_FT5X06_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#ifdef CONFIG_INPUT_FT5X06 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* Configuration ************************************************************/ +/* Maximum number of threads than can be waiting for POLL events */ + +#ifndef CONFIG_FT5X06_NPOLLWAITERS +# define CONFIG_FT5X06_NPOLLWAITERS 2 +#endif + +/* Check for some required settings. This can save the user a lot of time + * in getting the right configuration. + */ + +#ifdef CONFIG_DISABLE_SIGNALS +# error "Signals are required. CONFIG_DISABLE_SIGNALS must not be selected." +#endif + +#ifndef CONFIG_SCHED_WORKQUEUE +# error "Work queue support required. CONFIG_SCHED_WORKQUEUE must be selected." +#endif + +/**************************************************************************** + * Public Types + ****************************************************************************/ +/* The FT5x08 provides two interrupts pins: + * + * INT -A n interrupt signal to inform the host processor that touch data + * is ready for ready to be read. + * WAKE - An interrupt signal for the host to change FT5x06 from Hibernate + * to Active mode. + * + * A value from this enumeration must be passed to each interrupt-related + * interface method to distinguish the interrupt sources. + */ + +enum ft5x06_irqsource_e +{ + FT5X06_DATA_SOURCE = 0, + FT5X06_WAKE_SOURCE, +}; + +/* A reference to a structure of this type must be passed to the FT5X06 + * driver. This structure provides information about the configuration + * of the FT5x06 and provides some board-specific hooks. + * + * Memory for this structure is provided by the caller. It is not copied + * by the driver and is presumed to persist while the driver is active. The + * memory must be writeable because, under certain circumstances, the driver + * may modify frequency or X plate resistance values. + */ + +struct ft5x06_config_s +{ + /* Device characterization */ + + uint8_t address; /* 7-bit I2C address (only bits 0-6 used) */ + uint32_t frequency; /* Default I2C frequency */ + + /* IRQ/GPIO access callbacks. These operations all hidden behind + * callbacks to isolate the FT5X06 driver from differences in GPIO + * interrupt handling by varying boards and MCUs. + * + * attach - Attach an FT5x06 interrupt handler to a GPIO interrupt + * enable - Enable or disable a GPIO interrupt + * clear - Acknowledge/clear any pending GPIO interrupt + * nreset - Control the chip reset pin (active low) + */ + + int (*attach)(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc, xcpt_t isr, FAR void *arg); + void (*enable)(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc, bool enable); + void (*clear)(FAR const struct ft5x06_config_s *config, + enum ft5x06_irqsource_e irqsrc); + void (*nreset)(FAR const struct ft5x06_config_s *config, + bool state); +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: ft5x06_register + * + * Description: + * Configure the FT5x06 to use the provided I2C device instance. This + * will register the driver as /dev/inputN where N is the minor device + * number + * + * Input Parameters: + * i2c - An I2C driver instance + * config - Persistent board configuration data + * minor - The input device minor number + * + * Returned Value: + * Zero is returned on success. Otherwise, a negated errno value is + * returned to indicate the nature of the failure. + * + ****************************************************************************/ + +int ft5x06_register(FAR struct i2c_master_s *i2c, + FAR const struct ft5x06_config_s *config, int minor); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_INPUT_FT5X06 */ +#endif /* __INCLUDE_NUTTX_INPUT_FT5X06_H */