Release 4.11 drivers/input/touchscreen/edt-ft5x06.c
/*
* Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
* Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
* Lothar WaÃmann <LW@KARO-electronics.de> (DT support)
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This is a driver for the EDT "Polytouch" family of touch controllers
* based on the FocalTech FT5x06 line of chips.
*
* Development of this driver has been sponsored by Glyn:
* http://www.glyn.com/Products/Displays
*/
#include <linux/module.h>
#include <linux/ratelimit.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/gpio/consumer.h>
#include <linux/input/mt.h>
#include <linux/input/touchscreen.h>
#include <linux/of_device.h>
#define WORK_REGISTER_THRESHOLD 0x00
#define WORK_REGISTER_REPORT_RATE 0x08
#define WORK_REGISTER_GAIN 0x30
#define WORK_REGISTER_OFFSET 0x31
#define WORK_REGISTER_NUM_X 0x33
#define WORK_REGISTER_NUM_Y 0x34
#define M09_REGISTER_THRESHOLD 0x80
#define M09_REGISTER_GAIN 0x92
#define M09_REGISTER_OFFSET 0x93
#define M09_REGISTER_NUM_X 0x94
#define M09_REGISTER_NUM_Y 0x95
#define NO_REGISTER 0xff
#define WORK_REGISTER_OPMODE 0x3c
#define FACTORY_REGISTER_OPMODE 0x01
#define TOUCH_EVENT_DOWN 0x00
#define TOUCH_EVENT_UP 0x01
#define TOUCH_EVENT_ON 0x02
#define TOUCH_EVENT_RESERVED 0x03
#define EDT_NAME_LEN 23
#define EDT_SWITCH_MODE_RETRIES 10
#define EDT_SWITCH_MODE_DELAY 5
/* msec */
#define EDT_RAW_DATA_RETRIES 100
#define EDT_RAW_DATA_DELAY 1000
/* usec */
enum edt_ver {
M06,
M09,
};
struct edt_reg_addr {
int reg_threshold;
int reg_report_rate;
int reg_gain;
int reg_offset;
int reg_num_x;
int reg_num_y;
};
struct edt_ft5x06_ts_data {
struct i2c_client *client;
struct input_dev *input;
struct touchscreen_properties prop;
u16 num_x;
u16 num_y;
struct gpio_desc *reset_gpio;
struct gpio_desc *wake_gpio;
#if defined(CONFIG_DEBUG_FS)
struct dentry *debug_dir;
u8 *raw_buffer;
size_t raw_bufsize;
#endif
struct mutex mutex;
bool factory_mode;
int threshold;
int gain;
int offset;
int report_rate;
int max_support_points;
char name[EDT_NAME_LEN];
struct edt_reg_addr reg_addr;
enum edt_ver version;
};
struct edt_i2c_chip_data {
int max_support_points;
};
static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
u16 wr_len, u8 *wr_buf,
u16 rd_len, u8 *rd_buf)
{
struct i2c_msg wrmsg[2];
int i = 0;
int ret;
if (wr_len) {
wrmsg[i].addr = client->addr;
wrmsg[i].flags = 0;
wrmsg[i].len = wr_len;
wrmsg[i].buf = wr_buf;
i++;
}
if (rd_len) {
wrmsg[i].addr = client->addr;
wrmsg[i].flags = I2C_M_RD;
wrmsg[i].len = rd_len;
wrmsg[i].buf = rd_buf;
i++;
}
ret = i2c_transfer(client->adapter, wrmsg, i);
if (ret < 0)
return ret;
if (ret != i)
return -EIO;
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 169 | 100.00% | 1 | 100.00% |
Total | 169 | 100.00% | 1 | 100.00% |
static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
u8 *buf, int buflen)
{
int i;
u8 crc = 0;
for (i = 0; i < buflen - 1; i++)
crc ^= buf[i];
if (crc != buf[buflen-1]) {
dev_err_ratelimited(&tsdata->client->dev,
"crc error: 0x%02x expected, got 0x%02x\n",
crc, buf[buflen-1]);
return false;
}
return true;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 88 | 100.00% | 1 | 100.00% |
Total | 88 | 100.00% | 1 | 100.00% |
static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
{
struct edt_ft5x06_ts_data *tsdata = dev_id;
struct device *dev = &tsdata->client->dev;
u8 cmd;
u8 rdbuf[63];
int i, type, x, y, id;
int offset, tplen, datalen, crclen;
int error;
switch (tsdata->version) {
case M06:
cmd = 0xf9; /* tell the controller to send touch data */
offset = 5; /* where the actual touch data starts */
tplen = 4; /* data comes in so called frames */
crclen = 1; /* length of the crc data */
break;
case M09:
cmd = 0x0;
offset = 3;
tplen = 6;
crclen = 0;
break;
default:
goto out;
}
memset(rdbuf, 0, sizeof(rdbuf));
datalen = tplen * tsdata->max_support_points + offset + crclen;
error = edt_ft5x06_ts_readwrite(tsdata->client,
sizeof(cmd), &cmd,
datalen, rdbuf);
if (error) {
dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
error);
goto out;
}
/* M09 does not send header or CRC */
if (tsdata->version == M06) {
if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
rdbuf[2] != datalen) {
dev_err_ratelimited(dev,
"Unexpected header: %02x%02x%02x!\n",
rdbuf[0], rdbuf[1], rdbuf[2]);
goto out;
}
if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
goto out;
}
for (i = 0; i < tsdata->max_support_points; i++) {
u8 *buf = &rdbuf[i * tplen + offset];
bool down;
type = buf[0] >> 6;
/* ignore Reserved events */
if (type == TOUCH_EVENT_RESERVED)
continue;
/* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN)
continue;
x = ((buf[0] << 8) | buf[1]) & 0x0fff;
y = ((buf[2] << 8) | buf[3]) & 0x0fff;
id = (buf[2] >> 4) & 0x0f;
down = type != TOUCH_EVENT_UP;
input_mt_slot(tsdata->input, id);
input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
if (!down)
continue;
touchscreen_report_pos(tsdata->input, &tsdata->prop, x, y,
true);
}
input_mt_report_pointer_emulation(tsdata->input, true);
input_sync(tsdata->input);
out:
return IRQ_HANDLED;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 331 | 73.88% | 1 | 14.29% |
Lothar Waßmann | 86 | 19.20% | 2 | 28.57% |
Franklin S Cooper Jr | 25 | 5.58% | 3 | 42.86% |
Hans de Goede | 6 | 1.34% | 1 | 14.29% |
Total | 448 | 100.00% | 7 | 100.00% |
static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
u8 addr, u8 value)
{
u8 wrbuf[4];
switch (tsdata->version) {
case M06:
wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
wrbuf[2] = value;
wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
return edt_ft5x06_ts_readwrite(tsdata->client, 4,
wrbuf, 0, NULL);
case M09:
wrbuf[0] = addr;
wrbuf[1] = value;
return edt_ft5x06_ts_readwrite(tsdata->client, 2,
wrbuf, 0, NULL);
default:
return -EINVAL;
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 96 | 66.21% | 1 | 33.33% |
Lothar Waßmann | 48 | 33.10% | 1 | 33.33% |
Robert Woerle | 1 | 0.69% | 1 | 33.33% |
Total | 145 | 100.00% | 3 | 100.00% |
static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
u8 addr)
{
u8 wrbuf[2], rdbuf[2];
int error;
switch (tsdata->version) {
case M06:
wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
rdbuf);
if (error)
return error;
if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
dev_err(&tsdata->client->dev,
"crc error: 0x%02x expected, got 0x%02x\n",
wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
rdbuf[1]);
return -EIO;
}
break;
case M09:
wrbuf[0] = addr;
error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
wrbuf, 1, rdbuf);
if (error)
return error;
break;
default:
return -EINVAL;
}
return rdbuf[0];
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 159 | 73.95% | 1 | 33.33% |
Lothar Waßmann | 52 | 24.19% | 1 | 33.33% |
Dan Carpenter | 4 | 1.86% | 1 | 33.33% |
Total | 215 | 100.00% | 3 | 100.00% |
struct edt_ft5x06_attribute {
struct device_attribute dattr;
size_t field_offset;
u8 limit_low;
u8 limit_high;
u8 addr_m06;
u8 addr_m09;
};
#define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
_limit_low, _limit_high) \
struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
.dattr = __ATTR(_field, _mode, \
edt_ft5x06_setting_show, \
edt_ft5x06_setting_store), \
.field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
.addr_m06 = _addr_m06, \
.addr_m09 = _addr_m09, \
.limit_low = _limit_low, \
.limit_high = _limit_high, \
}
static ssize_t edt_ft5x06_setting_show(struct device *dev,
struct device_attribute *dattr,
char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
struct edt_ft5x06_attribute *attr =
container_of(dattr, struct edt_ft5x06_attribute, dattr);
u8 *field = (u8 *)tsdata + attr->field_offset;
int val;
size_t count = 0;
int error = 0;
u8 addr;
mutex_lock(&tsdata->mutex);
if (tsdata->factory_mode) {
error = -EIO;
goto out;
}
switch (tsdata->version) {
case M06:
addr = attr->addr_m06;
break;
case M09:
addr = attr->addr_m09;
break;
default:
error = -ENODEV;
goto out;
}
if (addr != NO_REGISTER) {
val = edt_ft5x06_register_read(tsdata, addr);
if (val < 0) {
error = val;
dev_err(&tsdata->client->dev,
"Failed to fetch attribute %s, error %d\n",
dattr->attr.name, error);
goto out;
}
} else {
val = *field;
}
if (val != *field) {
dev_warn(&tsdata->client->dev,
"%s: read (%d) and stored value (%d) differ\n",
dattr->attr.name, val, *field);
*field = val;
}
count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
out:
mutex_unlock(&tsdata->mutex);
return error ?: count;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 216 | 79.41% | 1 | 50.00% |
Lothar Waßmann | 56 | 20.59% | 1 | 50.00% |
Total | 272 | 100.00% | 2 | 100.00% |
static ssize_t edt_ft5x06_setting_store(struct device *dev,
struct device_attribute *dattr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
struct edt_ft5x06_attribute *attr =
container_of(dattr, struct edt_ft5x06_attribute, dattr);
u8 *field = (u8 *)tsdata + attr->field_offset;
unsigned int val;
int error;
u8 addr;
mutex_lock(&tsdata->mutex);
if (tsdata->factory_mode) {
error = -EIO;
goto out;
}
error = kstrtouint(buf, 0, &val);
if (error)
goto out;
if (val < attr->limit_low || val > attr->limit_high) {
error = -ERANGE;
goto out;
}
switch (tsdata->version) {
case M06:
addr = attr->addr_m06;
break;
case M09:
addr = attr->addr_m09;
break;
default:
error = -ENODEV;
goto out;
}
if (addr != NO_REGISTER) {
error = edt_ft5x06_register_write(tsdata, addr, val);
if (error) {
dev_err(&tsdata->client->dev,
"Failed to update attribute %s, error: %d\n",
dattr->attr.name, error);
goto out;
}
}
*field = val;
out:
mutex_unlock(&tsdata->mutex);
return error ?: count;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 208 | 81.25% | 1 | 50.00% |
Lothar Waßmann | 48 | 18.75% | 1 | 50.00% |
Total | 256 | 100.00% | 2 | 100.00% |
static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
M09_REGISTER_GAIN, 0, 31);
static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
M09_REGISTER_OFFSET, 0, 31);
static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
M09_REGISTER_THRESHOLD, 20, 80);
static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
NO_REGISTER, 3, 14);
static struct attribute *edt_ft5x06_attrs[] = {
&edt_ft5x06_attr_gain.dattr.attr,
&edt_ft5x06_attr_offset.dattr.attr,
&edt_ft5x06_attr_threshold.dattr.attr,
&edt_ft5x06_attr_report_rate.dattr.attr,
NULL
};
static const struct attribute_group edt_ft5x06_attr_group = {
.attrs = edt_ft5x06_attrs,
};
#ifdef CONFIG_DEBUG_FS
static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
{
struct i2c_client *client = tsdata->client;
int retries = EDT_SWITCH_MODE_RETRIES;
int ret;
int error;
disable_irq(client->irq);
if (!tsdata->raw_buffer) {
tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
sizeof(u16);
tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
if (!tsdata->raw_buffer) {
error = -ENOMEM;
goto err_out;
}
}
/* mode register is 0x3c when in the work mode */
if (tsdata->version == M09)
goto m09_out;
error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
if (error) {
dev_err(&client->dev,
"failed to switch to factory mode, error %d\n", error);
goto err_out;
}
tsdata->factory_mode = true;
do {
mdelay(EDT_SWITCH_MODE_DELAY);
/* mode register is 0x01 when in factory mode */
ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
if (ret == 0x03)
break;
} while (--retries > 0);
if (retries == 0) {
dev_err(&client->dev, "not in factory mode after %dms.\n",
EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
error = -EIO;
goto err_out;
}
return 0;
err_out:
kfree(tsdata->raw_buffer);
tsdata->raw_buffer = NULL;
tsdata->factory_mode = false;
enable_irq(client->irq);
return error;
m09_out:
dev_err(&client->dev, "No factory mode support for M09\n");
return -EINVAL;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 230 | 89.49% | 1 | 50.00% |
Lothar Waßmann | 27 | 10.51% | 1 | 50.00% |
Total | 257 | 100.00% | 2 | 100.00% |
static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
{
struct i2c_client *client = tsdata->client;
int retries = EDT_SWITCH_MODE_RETRIES;
struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
int ret;
int error;
/* mode register is 0x01 when in the factory mode */
error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
if (error) {
dev_err(&client->dev,
"failed to switch to work mode, error: %d\n", error);
return error;
}
tsdata->factory_mode = false;
do {
mdelay(EDT_SWITCH_MODE_DELAY);
/* mode register is 0x01 when in factory mode */
ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
if (ret == 0x01)
break;
} while (--retries > 0);
if (retries == 0) {
dev_err(&client->dev, "not in work mode after %dms.\n",
EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
tsdata->factory_mode = true;
return -EIO;
}
kfree(tsdata->raw_buffer);
tsdata->raw_buffer = NULL;
/* restore parameters */
edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
tsdata->threshold);
edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
tsdata->gain);
edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
tsdata->offset);
if (reg_addr->reg_report_rate)
edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
tsdata->report_rate);
enable_irq(client->irq);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 199 | 87.67% | 1 | 50.00% |
Lothar Waßmann | 28 | 12.33% | 1 | 50.00% |
Total | 227 | 100.00% | 2 | 100.00% |
static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
{
struct edt_ft5x06_ts_data *tsdata = data;
*mode = tsdata->factory_mode;
return 0;
}Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 31 | 100.00% | 1 | 100.00% |
Total | 31 | 100.00% | 1 | 100.00% |
;
static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
{
struct edt_ft5x06_ts_data *tsdata = data;
int retval = 0;
if (mode > 1)
return -ERANGE;
mutex_lock(&tsdata->mutex);
if (mode != tsdata->factory_mode) {
retval = mode ? edt_ft5x06_factory_mode(tsdata) :
edt_ft5x06_work_mode(tsdata);
}
mutex_unlock(&tsdata->mutex);
return retval;
}Contributors
Person | Tokens | Prop | Commits | CommitProp |
Simon Budig | 78 | 100.00% | 1 | 100.00% |
Total | 78 | 100.00% | 1 | 100.00% |
;
DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
edt_ft5x06_debugfs_mode_set, "%llu\n");
static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
char __user *buf, size_t count, loff_t *off)
{
struct edt_ft5x06_ts_data *tsdata = file->private_data;
struct i2c_client *client = tsdata->client;
int retries = EDT_RAW_DATA_RETRIES;
int val, i, error;
size_t read = 0;
int colbytes;
char wrbuf[3];
u8 *rdbuf;
if (*off < 0 || *off >= tsdata->raw_bufsize)
return 0;
mutex_lock(&tsdata->mutex);
if (!tsdata->factory_mode || !tsdata->raw_buffer) {
error = -EIO;
goto out;
}
error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
if (error) {
dev_dbg(&client->dev,
"failed to write 0x08 register, error %d\n", error);
goto out;
}
do {
usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
val = edt_ft5x06_register_read(tsdata, 0x08);
if (val < 1)
break;
} while (--retries > 0);
if (val < 0) {
error = val;
dev_dbg(&client->dev,
"failed to read 0x08 register, error %d\n", error);
goto out;
}
if (retries == 0) {
dev_dbg(&client->dev,
"timed out waiting for register to settle\n");
error = -ETIMEDOUT;
goto out;
}
rdbuf = tsdata->raw_buffer;
colbytes = tsdata->num_y * sizeof(u16);
wrbuf[0] = 0xf5;
wrbuf[1] = 0x0e;
for (i = 0; i < tsdata->num_x; i++) {
wrbuf[2] = i; /* column index */
error = edt_ft5x06_ts_readwrite(tsdata->client,
sizeof(wrbuf), wrbuf,
colbytes, rdbuf);
if (error)
goto out;
rdbuf += colbytes;
}
read = min_t(size_t, count, tsdata->raw_bufsize - *off);
if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
error = -EFAULT