2017-12-17 22:17:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/input/ft5x06.c
|
|
|
|
*
|
2021-09-14 16:16:31 +02:00
|
|
|
* 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
|
2017-12-17 22:17:06 +01:00
|
|
|
*
|
2021-09-14 16:16:31 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-12-17 22:17:06 +01:00
|
|
|
*
|
2021-09-14 16:16:31 +02:00
|
|
|
* 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.
|
2017-12-17 22:17:06 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* References:
|
|
|
|
* "FT5x06", FocalTech Systems Co., Ltd, D-FT5x06-1212-V4.0, Revised
|
|
|
|
* Dec. 18, 2012
|
|
|
|
*/
|
|
|
|
|
2017-12-17 22:17:06 +01:00
|
|
|
/* 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 <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-12-17 22:17:06 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2017-12-18 15:58:07 +01:00
|
|
|
#include <nuttx/wdog.h>
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
#include <nuttx/input/touchscreen.h>
|
|
|
|
#include <nuttx/input/ft5x06.h>
|
|
|
|
|
|
|
|
#include "ft5x06.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Driver support ***********************************************************/
|
2020-03-31 00:32:20 +02:00
|
|
|
|
2017-12-17 22:17:06 +01:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2017-12-18 15:58:07 +01:00
|
|
|
#define DEV_FORMAT "/dev/input%d"
|
|
|
|
#define DEV_NAMELEN 16
|
|
|
|
|
|
|
|
/* In polled mode, the polling rate will decrease when there is no touch
|
|
|
|
* activity. These definitions represent the maximum and the minimum
|
|
|
|
* polling rates.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define POLL_MINDELAY MSEC2TICK(50)
|
|
|
|
#define POLL_MAXDELAY MSEC2TICK(200)
|
|
|
|
#define POLL_INCREMENT MSEC2TICK(10)
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
2020-03-31 00:32:20 +02:00
|
|
|
volatile bool valid; /* True: New, valid touch data
|
|
|
|
* in touchbuf[] */
|
2017-12-18 17:31:49 +01:00
|
|
|
#ifdef CONFIG_FT5X06_SINGLEPOINT
|
|
|
|
uint8_t lastid; /* Last reported touch id */
|
|
|
|
uint8_t lastevent; /* Last reported event */
|
|
|
|
int16_t lastx; /* Last reported X position */
|
|
|
|
int16_t lasty; /* Last reported Y position */
|
|
|
|
#endif
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t devlock; /* Manages exclusive access to
|
2020-03-31 00:32:20 +02:00
|
|
|
* this structure */
|
2017-12-17 22:17:06 +01:00
|
|
|
sem_t waitsem; /* Used to wait for the
|
|
|
|
* availability of data */
|
|
|
|
uint32_t frequency; /* Current I2C frequency */
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
|
|
|
uint32_t delay; /* Current poll delay */
|
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
FAR const struct ft5x06_config_s *config; /* Board configuration data */
|
|
|
|
FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */
|
2020-03-31 00:32:20 +02:00
|
|
|
struct work_s work; /* Supports the interrupt
|
|
|
|
* handling "bottom half" */
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
2020-08-04 12:31:31 +02:00
|
|
|
struct wdog_s polltimer; /* Poll timer */
|
2017-12-18 15:58:07 +01:00
|
|
|
#endif
|
2020-03-31 00:32:20 +02:00
|
|
|
uint8_t touchbuf[FT5X06_TOUCH_DATA_LEN]; /* Raw touch data */
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* 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'.
|
|
|
|
*/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd *fds[CONFIG_FT5X06_NPOLLWAITERS];
|
2017-12-17 22:17:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ft5x06_notify(FAR struct ft5x06_dev_s *priv);
|
|
|
|
static void ft5x06_data_worker(FAR void *arg);
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
2020-08-09 20:29:35 +02:00
|
|
|
static void ft5x06_poll_timeout(wdparm_t arg);
|
2017-12-18 15:58:07 +01:00
|
|
|
#else
|
2017-12-17 22:17:06 +01:00
|
|
|
static int ft5x06_data_interrupt(int irq, FAR void *context, FAR void *arg);
|
2017-12-18 15:58:07 +01:00
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
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);
|
2024-08-25 01:21:12 +02:00
|
|
|
static int ft5x06_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2017-12-17 22:17:06 +01:00
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This the vtable that supports the character driver interface */
|
|
|
|
|
2023-04-22 16:43:07 +02:00
|
|
|
static const struct file_operations g_ft5x06_fops =
|
2017-12-17 22:17:06 +01:00
|
|
|
{
|
|
|
|
ft5x06_open, /* open */
|
|
|
|
ft5x06_close, /* close */
|
|
|
|
ft5x06_read, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
ft5x06_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
ft5x06_poll /* poll */
|
2017-12-17 22:17:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Maps FT5x06 touch events into bit encoded representation used by NuttX */
|
|
|
|
|
|
|
|
static const uint8_t g_event_map[4] =
|
|
|
|
{
|
2020-03-31 00:32:20 +02:00
|
|
|
(TOUCH_DOWN | TOUCH_ID_VALID | TOUCH_POS_VALID), /* FT5X06_DOWN */
|
|
|
|
(TOUCH_UP | TOUCH_ID_VALID), /* FT5X06_UP */
|
|
|
|
(TOUCH_MOVE | TOUCH_ID_VALID | TOUCH_POS_VALID), /* FT5X06_CONTACT */
|
|
|
|
TOUCH_ID_VALID /* FT5X06_INVALID */
|
2017-12-17 22:17:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_notify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ft5x06_notify(FAR struct ft5x06_dev_s *priv)
|
|
|
|
{
|
2020-03-31 00:32:20 +02:00
|
|
|
/* 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.
|
2017-12-17 22:17:06 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(priv->fds, CONFIG_FT5X06_NPOLLWAITERS, POLLIN);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
2017-12-18 15:58:07 +01:00
|
|
|
FAR struct ft5x06_touch_data_s *sample;
|
2017-12-17 22:17:06 +01:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Read touch data */
|
2020-03-31 00:32:20 +02:00
|
|
|
|
2017-12-17 22:17:06 +01:00
|
|
|
/* Set up the address write operation */
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
regaddr = FT5X06_TOUCH_DATA_STARTREG;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-12-18 17:31:49 +01:00
|
|
|
/* Set up the data read operation.
|
|
|
|
*
|
2017-12-19 00:22:35 +01:00
|
|
|
* REVISIT: If CONFIG_FT5X06_SINGLEPOINT is selected, could we not just
|
2017-12-18 17:31:49 +01:00
|
|
|
* set the length for one sample? Or is there some reason why we have to
|
|
|
|
* read all of the points?
|
|
|
|
*/
|
2017-12-17 22:17:06 +01:00
|
|
|
|
2017-12-18 15:58:07 +01:00
|
|
|
msg[1].frequency = priv->frequency; /* I2C frequency */
|
|
|
|
msg[1].addr = config->address; /* 7-bit address */
|
|
|
|
msg[1].flags = I2C_M_READ; /* Read transaction with Re-START */
|
|
|
|
msg[1].buffer = priv->touchbuf; /* Read all touch data */
|
2020-03-31 00:32:20 +02:00
|
|
|
msg[1].length = FT5X06_TOUCH_DATA_LEN;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, msg, 2);
|
2017-12-19 00:22:35 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* In polled mode, we may read invalid touch data. If there is
|
2017-12-19 03:06:42 +01:00
|
|
|
* no touch data, the FT5x06 returns all 0xff the very first time.
|
|
|
|
* After that, it returns the same old stale data when there is
|
|
|
|
* no touch data.
|
2017-12-19 00:22:35 +01:00
|
|
|
*/
|
2017-12-18 15:58:07 +01:00
|
|
|
|
2017-12-19 00:22:35 +01:00
|
|
|
sample = (FAR struct ft5x06_touch_data_s *)priv->touchbuf;
|
2017-12-18 15:58:07 +01:00
|
|
|
|
2017-12-19 14:14:02 +01:00
|
|
|
/* Notify waiters (only if we ready some valid data).
|
|
|
|
*
|
|
|
|
* REVISIT: For improved performance consider moving the duplicate
|
|
|
|
* report and thresholding logic from ft5x06_sample() to here. That
|
|
|
|
* would save a context switch.
|
|
|
|
*/
|
2017-12-19 00:22:35 +01:00
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
if (sample->tdstatus <= FT5X06_MAX_TOUCHES)
|
2017-12-19 00:22:35 +01:00
|
|
|
{
|
|
|
|
/* Notify any waiters that new FT5x06 data is available */
|
|
|
|
|
|
|
|
priv->valid = true;
|
|
|
|
ft5x06_notify(priv);
|
|
|
|
}
|
2017-12-17 22:17:06 +01:00
|
|
|
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
2017-12-19 00:22:35 +01:00
|
|
|
/* Update the poll rate */
|
2017-12-18 15:58:07 +01:00
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
if (sample->tdstatus > 0 && sample->tdstatus <= FT5X06_MAX_TOUCHES)
|
2017-12-19 00:22:35 +01:00
|
|
|
{
|
|
|
|
/* Keep it at the minimum if touches are detected. */
|
2017-12-17 22:17:06 +01:00
|
|
|
|
2017-12-19 00:22:35 +01:00
|
|
|
priv->delay = POLL_MINDELAY;
|
|
|
|
}
|
|
|
|
else if (priv->delay < POLL_MAXDELAY)
|
|
|
|
{
|
2017-12-19 03:06:42 +01:00
|
|
|
/* Otherwise, let the poll rate rise gradually up to the maximum
|
|
|
|
* if there is no touch.
|
|
|
|
*/
|
2017-12-19 00:22:35 +01:00
|
|
|
|
|
|
|
priv->delay += POLL_INCREMENT;
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-18 15:58:07 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 00:22:35 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
2018-01-18 16:59:31 +01:00
|
|
|
/* Exit, re-starting the poll. */
|
2017-12-18 15:58:07 +01:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_start(&priv->polltimer, priv->delay,
|
2020-08-09 20:29:35 +02:00
|
|
|
ft5x06_poll_timeout, (wdparm_t)priv);
|
2017-12-18 15:58:07 +01:00
|
|
|
|
|
|
|
#else
|
2017-12-17 22:17:06 +01:00
|
|
|
/* Exit, re-enabling FT5x06 interrupts */
|
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
config->enable(config, true);
|
2017-12-18 15:58:07 +01:00
|
|
|
#endif
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 15:58:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_poll_timeout
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
2020-08-09 20:29:35 +02:00
|
|
|
static void ft5x06_poll_timeout(wdparm_t arg)
|
2017-12-18 15:58:07 +01:00
|
|
|
{
|
2020-08-09 20:29:35 +02:00
|
|
|
FAR struct ft5x06_dev_s *priv = (FAR struct ft5x06_dev_s *)arg;
|
2017-12-18 15:58:07 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Transfer processing to the worker thread. Since FT5x06 poll timer is
|
|
|
|
* disabled while the work is pending, no special action should be
|
|
|
|
* required to protected the work queue.
|
|
|
|
*/
|
|
|
|
|
2018-01-18 16:59:31 +01:00
|
|
|
DEBUGASSERT(priv->work.worker == NULL);
|
|
|
|
ret = work_queue(HPWORK, &priv->work, ft5x06_data_worker, priv, 0);
|
|
|
|
if (ret != 0)
|
2017-12-18 15:58:07 +01:00
|
|
|
{
|
2018-01-18 16:59:31 +01:00
|
|
|
ierr("ERROR: Failed to queue work: %d\n", ret);
|
2017-12-18 15:58:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-17 22:17:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_data_interrupt
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifndef CONFIG_FT5X06_POLLMODE
|
2017-12-17 22:17:06 +01:00
|
|
|
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 */
|
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
config->enable(config, false);
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
config->clear(config);
|
2017-12-17 22:17:06 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2017-12-18 15:58:07 +01:00
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_sample
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-12-18 17:31:49 +01:00
|
|
|
#ifdef CONFIG_FT5X06_SINGLEPOINT
|
|
|
|
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;
|
|
|
|
int16_t x;
|
|
|
|
int16_t y;
|
|
|
|
uint8_t event;
|
|
|
|
uint8_t id;
|
|
|
|
|
|
|
|
if (!priv->valid)
|
|
|
|
{
|
|
|
|
return 0; /* Nothing to read */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Raw data pointers (source) */
|
|
|
|
|
|
|
|
raw = (FAR struct ft5x06_touch_data_s *)priv->touchbuf;
|
2017-12-19 03:06:42 +01:00
|
|
|
touch = raw->touch;
|
2017-12-18 17:31:49 +01:00
|
|
|
|
|
|
|
/* Get the reported X and Y positions */
|
|
|
|
|
|
|
|
#ifdef CONFIG_FT5X06_SWAPXY
|
|
|
|
y = TOUCH_POINT_GET_X(touch[0]);
|
|
|
|
x = TOUCH_POINT_GET_Y(touch[0]);
|
|
|
|
#else
|
|
|
|
x = TOUCH_POINT_GET_X(touch[0]);
|
|
|
|
y = TOUCH_POINT_GET_Y(touch[0]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get the touch point ID and event */
|
|
|
|
|
|
|
|
event = TOUCH_POINT_GET_EVENT(touch[0]);
|
|
|
|
id = TOUCH_POINT_GET_ID(touch[0]);
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
if (event == FT5X06_INVALID)
|
2017-12-18 17:31:49 +01:00
|
|
|
{
|
2020-03-31 00:32:20 +02:00
|
|
|
priv->lastevent = FT5X06_INVALID;
|
2017-12-19 14:14:02 +01:00
|
|
|
goto reset_and_drop;
|
2017-12-19 03:06:42 +01:00
|
|
|
}
|
2017-12-18 17:31:49 +01:00
|
|
|
|
2017-12-19 03:06:42 +01:00
|
|
|
if (id == priv->lastid && event == priv->lastevent)
|
|
|
|
{
|
|
|
|
/* Same ID and event.. Is there positional data? */
|
2017-12-18 17:31:49 +01:00
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
if (raw->tdstatus == 0 || event == FT5X06_UP)
|
2017-12-18 17:31:49 +01:00
|
|
|
{
|
2017-12-19 03:06:42 +01:00
|
|
|
/* No... no new touch data */
|
2017-12-18 17:31:49 +01:00
|
|
|
|
2017-12-19 14:14:02 +01:00
|
|
|
goto reset_and_drop;
|
2017-12-19 03:06:42 +01:00
|
|
|
}
|
|
|
|
else
|
2017-12-18 17:31:49 +01:00
|
|
|
{
|
2017-12-19 03:06:42 +01:00
|
|
|
int16_t deltax;
|
|
|
|
int16_t deltay;
|
2017-12-18 17:31:49 +01:00
|
|
|
|
2017-12-19 03:06:42 +01:00
|
|
|
/* Compare the change in position from the last report. */
|
|
|
|
|
|
|
|
deltax = (x - priv->lastx);
|
|
|
|
if (deltax < 0)
|
2017-12-18 17:31:49 +01:00
|
|
|
{
|
2017-12-19 03:06:42 +01:00
|
|
|
deltax = -deltax;
|
2017-12-18 17:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (deltax < CONFIG_FT5X06_THRESHX)
|
|
|
|
{
|
2017-12-19 03:06:42 +01:00
|
|
|
/* There as been no significant change in X, try Y */
|
|
|
|
|
|
|
|
deltay = (y - priv->lasty);
|
|
|
|
if (deltay < 0)
|
|
|
|
{
|
|
|
|
deltay = -deltay;
|
|
|
|
}
|
2017-12-18 17:31:49 +01:00
|
|
|
|
2017-12-19 03:06:42 +01:00
|
|
|
if (deltax < CONFIG_FT5X06_THRESHX)
|
|
|
|
{
|
|
|
|
/* Ignore... no significant change in Y either */
|
|
|
|
|
|
|
|
goto drop;
|
|
|
|
}
|
2017-12-18 17:31:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->lastid = id;
|
|
|
|
priv->lastevent = event;
|
|
|
|
priv->lastx = x;
|
|
|
|
priv->lasty = y;
|
|
|
|
|
|
|
|
/* User data buffer points (sink) */
|
|
|
|
|
|
|
|
/* Return the number of touches read */
|
|
|
|
|
|
|
|
sample = (FAR struct touch_sample_s *)buffer;
|
|
|
|
sample->npoints = 1;
|
|
|
|
|
|
|
|
/* Decode and return the single touch point */
|
|
|
|
|
|
|
|
point = sample->point;
|
|
|
|
point[0].id = id;
|
|
|
|
point[0].flags = g_event_map[event];
|
|
|
|
point[0].x = x;
|
|
|
|
point[0].y = y;
|
|
|
|
point[0].h = 0;
|
|
|
|
point[0].w = 0;
|
|
|
|
point[0].pressure = 0;
|
|
|
|
|
|
|
|
priv->valid = false;
|
|
|
|
return SIZEOF_TOUCH_SAMPLE_S(1);
|
2017-12-19 03:06:42 +01:00
|
|
|
|
2017-12-19 14:14:02 +01:00
|
|
|
reset_and_drop:
|
2017-12-19 03:06:42 +01:00
|
|
|
priv->lastx = 0;
|
|
|
|
priv->lasty = 0;
|
2017-12-19 14:14:02 +01:00
|
|
|
drop:
|
2017-12-19 03:06:42 +01:00
|
|
|
priv->valid = false;
|
|
|
|
return 0; /* No new touches read. */
|
2017-12-18 17:31:49 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-12-17 22:17:06 +01:00
|
|
|
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 maxtouches;
|
2017-12-18 17:31:49 +01:00
|
|
|
unsigned int ntouches;
|
2017-12-17 22:17:06 +01:00
|
|
|
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;
|
2020-03-31 00:32:20 +02:00
|
|
|
DEBUGASSERT(ntouches <= FT5X06_MAX_TOUCHES);
|
2017-12-18 17:31:49 +01:00
|
|
|
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ntouches > maxtouches)
|
|
|
|
{
|
|
|
|
ntouches = maxtouches;
|
|
|
|
}
|
|
|
|
|
2017-12-18 00:43:20 +01:00
|
|
|
if (ntouches < 1)
|
|
|
|
{
|
|
|
|
priv->valid = false;
|
|
|
|
return 0; /* No touches read. */
|
|
|
|
}
|
|
|
|
|
2017-12-17 22:17:06 +01:00
|
|
|
/* 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 */
|
|
|
|
|
2017-12-18 17:31:49 +01:00
|
|
|
for (i = 0; i < ntouches; i++)
|
2017-12-17 22:17:06 +01:00
|
|
|
{
|
|
|
|
int event = TOUCH_POINT_GET_EVENT(touch[i]);
|
|
|
|
|
|
|
|
point[i].id = TOUCH_POINT_GET_ID(touch[i]);
|
|
|
|
point[i].flags = g_event_map[event];
|
2017-12-18 17:31:49 +01:00
|
|
|
#ifdef CONFIG_FT5X06_SWAPXY
|
|
|
|
point[i].y = TOUCH_POINT_GET_X(touch[i]);
|
|
|
|
point[i].x = TOUCH_POINT_GET_Y(touch[i]);
|
|
|
|
#else
|
2017-12-17 22:17:06 +01:00
|
|
|
point[i].x = TOUCH_POINT_GET_X(touch[i]);
|
|
|
|
point[i].y = TOUCH_POINT_GET_Y(touch[i]);
|
2017-12-18 17:31:49 +01:00
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
point[i].h = 0;
|
|
|
|
point[i].w = 0;
|
|
|
|
point[i].pressure = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->valid = false;
|
|
|
|
return SIZEOF_TOUCH_SAMPLE_S(ntouches);
|
|
|
|
}
|
2017-12-18 17:31:49 +01:00
|
|
|
#endif /* CONFIG_FT5X06_SINGLEPOINT */
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_waitsample
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ft5x06_waitsample(FAR struct ft5x06_dev_s *priv,
|
|
|
|
FAR char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2017-12-18 15:58:07 +01:00
|
|
|
/* Increment the count of waiters */
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
priv->nwaiters++;
|
2017-12-18 15:58:07 +01:00
|
|
|
|
|
|
|
/* Wait for a change in the FT5x06 state */
|
|
|
|
|
2017-12-17 22:17:06 +01:00
|
|
|
ret = nxsem_wait(&priv->waitsem);
|
|
|
|
priv->nwaiters--;
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-acquire the semaphore that manages mutually exclusive access to
|
2020-03-31 00:32:20 +02:00
|
|
|
* 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.
|
2017-12-17 22:17:06 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Now sample the data.
|
|
|
|
*
|
|
|
|
* REVISIT: Is it safe to assume that priv->valid will always be
|
2020-02-23 09:50:23 +01:00
|
|
|
* true? I think that sched_lock() would protect the setting.
|
2017-12-17 22:17:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ft5x06_sample(priv, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
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 */
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
data[0] = FT5X06_TOUCH_MODE_REG; /* Register address */
|
|
|
|
data[1] = FT5X06_DEV_MODE_WORKING; /* Normal mode */
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifndef CONFIG_FT5X06_POLLMODE
|
2017-12-17 22:17:06 +01:00
|
|
|
/* Enable FT5x06 interrupts */
|
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
config->clear(config);
|
|
|
|
config->enable(config, true);
|
2017-12-18 15:58:07 +01:00
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_shutdown
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ft5x06_shutdown(FAR struct ft5x06_dev_s *priv)
|
|
|
|
{
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
|
|
|
/* Stop the poll timer */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->polltimer);
|
2017-12-18 15:58:07 +01:00
|
|
|
|
|
|
|
#else
|
2017-12-17 22:17:06 +01:00
|
|
|
FAR const struct ft5x06_config_s *config = priv->config;
|
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
/* Make sure that the FT5x06 interrupt is disabled */
|
2017-12-17 22:17:06 +01:00
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
config->clear(config);
|
|
|
|
config->enable(config, false);
|
2017-12-18 15:58:07 +01:00
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-12-17 22:17:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the reference count */
|
|
|
|
|
|
|
|
tmp = priv->crefs + 1;
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t overflows to zero */
|
|
|
|
|
|
|
|
ret = -EMFILE;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2017-12-18 15:58:07 +01:00
|
|
|
ierr("ERROR: ft5x06_bringup failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
priv->crefs = tmp;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-12-17 22:17:06 +01:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
if (priv->crefs == 0)
|
|
|
|
{
|
|
|
|
ft5x06_shutdown(priv);
|
|
|
|
}
|
2017-12-17 22:17:06 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ft5x06_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct ft5x06_dev_s *priv;
|
2017-12-17 22:17:06 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-12-17 22:17:06 +01:00
|
|
|
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;
|
2020-03-31 00:32:20 +02:00
|
|
|
}
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* 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:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-02-01 19:17:03 +01:00
|
|
|
* Name: ft5x06_ioctl
|
2017-12-17 22:17:06 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ft5x06_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
2017-12-17 22:17:06 +01:00
|
|
|
FAR struct ft5x06_dev_s *priv;
|
2022-09-06 08:18:45 +02:00
|
|
|
int ret;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
iinfo("cmd: %d arg: %ld\n", cmd, arg);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-12-17 22:17:06 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ft5x06_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ft5x06_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2022-09-06 08:18:45 +02:00
|
|
|
bool setup)
|
2017-12-17 22:17:06 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
2017-12-17 22:17:06 +01:00
|
|
|
FAR struct ft5x06_dev_s *priv;
|
2022-09-06 08:18:45 +02:00
|
|
|
int ret;
|
|
|
|
int i;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
iinfo("setup: %d\n", (int)setup);
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(fds);
|
2017-12-17 22:17:06 +01:00
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-12-17 22:17:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* Ignore waits that do not include POLLIN */
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) == 0)
|
|
|
|
{
|
2022-04-01 14:59:55 +02:00
|
|
|
ierr("ERROR: Missing POLLIN: revents: %08" PRIx32 "\n",
|
|
|
|
fds->revents);
|
2017-12-17 22:17:06 +01:00
|
|
|
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);
|
2023-11-19 12:19:53 +01:00
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
2017-12-17 22:17:06 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we immediately notify on any of the requested events? */
|
|
|
|
|
|
|
|
if (priv->valid)
|
|
|
|
{
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, POLLIN);
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fds->priv)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
2017-12-17 22:17:06 +01:00
|
|
|
DEBUGASSERT(slot != NULL);
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-12-17 22:17:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
2020-02-22 19:31:14 +01:00
|
|
|
* config - Persistent board configuration data
|
2017-12-17 22:17:06 +01:00
|
|
|
* 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);
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
|
|
|
DEBUGASSERT(config->wakeup != NULL && config->nreset != NULL);
|
|
|
|
#else
|
|
|
|
DEBUGASSERT(config->attach != NULL && config->enable != NULL &&
|
|
|
|
config->clear != NULL && config->wakeup != NULL &&
|
|
|
|
config->nreset != NULL);
|
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Create and initialize a FT5x06 device driver instance */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct ft5x06_dev_s));
|
2017-12-17 22:17:06 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
2019-05-06 17:09:19 +02:00
|
|
|
ierr("ERROR: kmm_zalloc(%d) failed\n", sizeof(struct ft5x06_dev_s));
|
2017-12-17 22:17:06 +01:00
|
|
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->devlock); /* Initialize device structure mutex */
|
2017-12-17 22:17:06 +01:00
|
|
|
nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */
|
|
|
|
|
2017-12-18 15:58:07 +01:00
|
|
|
#ifdef CONFIG_FT5X06_POLLMODE
|
|
|
|
/* Allocate a timer for polling the FT5x06 */
|
|
|
|
|
|
|
|
priv->delay = POLL_MAXDELAY;
|
|
|
|
#else
|
2017-12-18 13:36:44 +01:00
|
|
|
/* Make sure that the FT5x06 interrupt interrupt is disabled */
|
2017-12-17 22:17:06 +01:00
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
config->clear(config);
|
|
|
|
config->enable(config, false);
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Attach the interrupt handler */
|
|
|
|
|
2017-12-18 13:36:44 +01:00
|
|
|
ret = config->attach(config, ft5x06_data_interrupt,
|
2017-12-17 22:17:06 +01:00
|
|
|
priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ierr("ERROR: Failed to attach interrupt\n");
|
2020-08-04 12:31:31 +02:00
|
|
|
goto errout_with_priv;
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
2017-12-18 15:58:07 +01:00
|
|
|
#endif
|
2017-12-17 22:17:06 +01:00
|
|
|
|
|
|
|
/* Register the device as an input device */
|
|
|
|
|
2022-04-12 12:16:13 +02:00
|
|
|
snprintf(devname, sizeof(devname), DEV_FORMAT, minor);
|
2017-12-17 22:17:06 +01:00
|
|
|
iinfo("Registering %s\n", devname);
|
|
|
|
|
2023-04-22 16:43:07 +02:00
|
|
|
ret = register_driver(devname, &g_ft5x06_fops, 0666, priv);
|
2017-12-17 22:17:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ierr("ERROR: register_driver() failed: %d\n", ret);
|
2020-08-04 12:31:31 +02:00
|
|
|
goto errout_with_priv;
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2017-12-18 20:33:37 +01:00
|
|
|
if (ret < 0)
|
2017-12-17 22:17:06 +01:00
|
|
|
{
|
|
|
|
ierr("ERROR: Failed to queue work: %d\n", ret);
|
2020-08-04 12:31:31 +02:00
|
|
|
goto errout_with_priv;
|
2017-12-17 22:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And return success */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_priv:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->devlock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_destroy(&priv->waitsem);
|
2017-12-17 22:17:06 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|