26 #include <linux/module.h>
29 #include <linux/input.h>
30 #include <linux/i2c.h>
34 #include <linux/slab.h>
39 #define MAX_SUPPORT_POINTS 5
41 #define WORK_REGISTER_THRESHOLD 0x00
42 #define WORK_REGISTER_REPORT_RATE 0x08
43 #define WORK_REGISTER_GAIN 0x30
44 #define WORK_REGISTER_OFFSET 0x31
45 #define WORK_REGISTER_NUM_X 0x33
46 #define WORK_REGISTER_NUM_Y 0x34
48 #define WORK_REGISTER_OPMODE 0x3c
49 #define FACTORY_REGISTER_OPMODE 0x01
51 #define TOUCH_EVENT_DOWN 0x00
52 #define TOUCH_EVENT_UP 0x01
53 #define TOUCH_EVENT_ON 0x02
54 #define TOUCH_EVENT_RESERVED 0x03
56 #define EDT_NAME_LEN 23
57 #define EDT_SWITCH_MODE_RETRIES 10
58 #define EDT_SWITCH_MODE_DELAY 5
59 #define EDT_RAW_DATA_RETRIES 100
60 #define EDT_RAW_DATA_DELAY 1
68 #if defined(CONFIG_DEBUG_FS)
85 u16 wr_len,
u8 *wr_buf,
86 u16 rd_len,
u8 *rd_buf)
95 wrmsg[
i].
len = wr_len;
96 wrmsg[
i].
buf = wr_buf;
102 wrmsg[
i].
len = rd_len;
103 wrmsg[
i].
buf = rd_buf;
122 for (i = 0; i < buflen - 1; i++)
125 if (crc != buf[buflen-1]) {
127 "crc error: 0x%02x expected, got 0x%02x\n",
144 memset(rdbuf, 0,
sizeof(rdbuf));
146 error = edt_ft5x06_ts_readwrite(tsdata->
client,
148 sizeof(rdbuf), rdbuf);
155 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa || rdbuf[2] != 26) {
157 rdbuf[0], rdbuf[1], rdbuf[2]);
161 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, 26))
165 u8 *buf = &rdbuf[i * 4 + 5];
173 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
174 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
175 id = (buf[2] >> 4) & 0x0f;
189 input_sync(tsdata->
input);
201 wrbuf[1] = tsdata->
factory_mode ? addr & 0x7f : addr & 0x3f;
203 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
205 return edt_ft5x06_ts_readwrite(tsdata->
client, 4, wrbuf, 0,
NULL);
211 u8 wrbuf[2], rdbuf[2];
215 wrbuf[1] = tsdata->
factory_mode ? addr & 0x7f : addr & 0x3f;
218 error = edt_ft5x06_ts_readwrite(tsdata->
client, 2, wrbuf, 2, rdbuf);
222 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
224 "crc error: 0x%02x expected, got 0x%02x\n",
225 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0], rdbuf[1]);
240 #define EDT_ATTR(_field, _mode, _addr, _limit_low, _limit_high) \
241 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
242 .dattr = __ATTR(_field, _mode, \
243 edt_ft5x06_setting_show, \
244 edt_ft5x06_setting_store), \
246 offsetof(struct edt_ft5x06_ts_data, _field), \
247 .limit_low = _limit_low, \
248 .limit_high = _limit_high, \
272 val = edt_ft5x06_register_read(tsdata, attr->
addr);
276 "Failed to fetch attribute %s, error %d\n",
277 dattr->
attr.name, error);
283 "%s: read (%d) and stored value (%d) differ\n",
284 dattr->
attr.name, val, *field);
291 return error ?:
count;
296 const char *buf,
size_t count)
322 error = edt_ft5x06_register_write(tsdata, attr->
addr, val);
325 "Failed to update attribute %s, error: %d\n",
326 dattr->
attr.name, error);
334 return error ?:
count;
344 static struct attribute *edt_ft5x06_attrs[] = {
345 &edt_ft5x06_attr_gain.dattr.attr,
346 &edt_ft5x06_attr_offset.dattr.attr,
347 &edt_ft5x06_attr_threshold.dattr.attr,
348 &edt_ft5x06_attr_report_rate.dattr.attr,
353 .attrs = edt_ft5x06_attrs,
356 #ifdef CONFIG_DEBUG_FS
366 if (!tsdata->raw_buffer) {
367 tsdata->raw_bufsize = tsdata->
num_x * tsdata->
num_y *
369 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize,
GFP_KERNEL);
370 if (!tsdata->raw_buffer) {
380 "failed to switch to factory mode, error %d\n", error);
391 }
while (--retries > 0);
394 dev_err(&client->
dev,
"not in factory mode after %dms.\n",
403 kfree(tsdata->raw_buffer);
404 tsdata->raw_buffer =
NULL;
422 "failed to switch to work mode, error: %d\n", error);
434 }
while (--retries > 0);
437 dev_err(&client->
dev,
"not in work mode after %dms.\n",
443 if (tsdata->raw_buffer)
444 kfree(tsdata->raw_buffer);
445 tsdata->raw_buffer =
NULL;
462 static int edt_ft5x06_debugfs_mode_get(
void *
data,
u64 *
mode)
471 static int edt_ft5x06_debugfs_mode_set(
void *
data,
u64 mode)
482 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
483 edt_ft5x06_work_mode(tsdata);
492 edt_ft5x06_debugfs_mode_set,
"%llu\n");
494 static int edt_ft5x06_debugfs_raw_data_open(
struct inode *
inode,
503 char __user *buf,
size_t count, loff_t *off)
514 if (*off < 0 || *off >= tsdata->raw_bufsize)
524 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
527 "failed to write 0x08 register, error %d\n", error);
533 val = edt_ft5x06_register_read(tsdata, 0x08);
536 }
while (--retries > 0);
541 "failed to read 0x08 register, error %d\n", error);
547 "timed out waiting for register to settle\n");
552 rdbuf = tsdata->raw_buffer;
553 colbytes = tsdata->
num_y *
sizeof(
u16);
557 for (i = 0; i < tsdata->
num_x; i++) {
559 error = edt_ft5x06_ts_readwrite(tsdata->
client,
560 sizeof(wrbuf), wrbuf,
568 read =
min_t(
size_t, count, tsdata->raw_bufsize - *off);
569 if (
copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
577 return error ?:
read;
582 .
open = edt_ft5x06_debugfs_raw_data_open,
583 .read = edt_ft5x06_debugfs_raw_data_read,
588 const char *debugfs_name)
591 if (!tsdata->debug_dir)
598 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
600 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
606 if (tsdata->debug_dir)
608 kfree(tsdata->raw_buffer);
615 const char *debugfs_name)
633 if (gpio_is_valid(reset_pin)) {
639 "Failed to request GPIO %d as reset pin, error %d\n",
660 error = edt_ft5x06_ts_readwrite(client, 1,
"\xbb",
681 #define EDT_ATTR_CHECKSET(name, reg) \
682 if (pdata->name >= edt_ft5x06_attr_##name.limit_low && \
683 pdata->name <= edt_ft5x06_attr_##name.limit_high) \
684 edt_ft5x06_register_write(tsdata, reg, pdata->name)
703 tsdata->
threshold = edt_ft5x06_register_read(tsdata,
707 tsdata->
report_rate = edt_ft5x06_register_read(tsdata,
717 client->
dev.platform_data;
719 struct input_dev *
input;
723 dev_dbg(&client->
dev,
"probing for EDT FT5x06 I2C\n");
730 error = edt_ft5x06_ts_reset(client, pdata->
reset_pin);
734 if (gpio_is_valid(pdata->
irq_pin)) {
739 "Failed to request GPIO %d, error %d\n",
745 tsdata = kzalloc(
sizeof(*tsdata),
GFP_KERNEL);
746 input = input_allocate_device();
747 if (!tsdata || !input) {
748 dev_err(&client->
dev,
"failed to allocate driver data.\n");
758 error = edt_ft5x06_ts_identify(client, tsdata->
name, fw_version);
760 dev_err(&client->
dev,
"touchscreen probe failed\n");
764 edt_ft5x06_ts_get_defaults(tsdata, pdata);
765 edt_ft5x06_ts_get_parameters(tsdata);
768 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
771 input->name = tsdata->
name;
773 input->dev.parent = &client->
dev;
779 input_set_abs_params(input,
ABS_X, 0, tsdata->
num_x * 64 - 1, 0, 0);
780 input_set_abs_params(input,
ABS_Y, 0, tsdata->
num_y * 64 - 1, 0, 0);
782 0, tsdata->
num_x * 64 - 1, 0, 0);
784 0, tsdata->
num_y * 64 - 1, 0, 0);
787 dev_err(&client->
dev,
"Unable to init MT slots.\n");
791 input_set_drvdata(input, tsdata);
792 i2c_set_clientdata(client, tsdata);
796 client->
name, tsdata);
798 dev_err(&client->
dev,
"Unable to request touchscreen IRQ.\n");
806 error = input_register_device(input);
808 goto err_remove_attrs;
814 "EDT FT5x06 initialized: IRQ pin %d, Reset pin %d.\n",
824 input_free_device(input);
827 if (gpio_is_valid(pdata->
irq_pin))
836 dev_get_platdata(&client->
dev);
839 edt_ft5x06_ts_teardown_debugfs(tsdata);
843 input_unregister_device(tsdata->
input);
845 if (gpio_is_valid(pdata->
irq_pin))
855 #ifdef CONFIG_PM_SLEEP
856 static int edt_ft5x06_ts_suspend(
struct device *dev)
860 if (device_may_wakeup(dev))
861 enable_irq_wake(client->
irq);
866 static int edt_ft5x06_ts_resume(
struct device *dev)
870 if (device_may_wakeup(dev))
871 disable_irq_wake(client->
irq);
878 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
886 static struct i2c_driver edt_ft5x06_ts_driver = {
889 .name =
"edt_ft5x06",
890 .pm = &edt_ft5x06_ts_pm_ops,
892 .id_table = edt_ft5x06_ts_id,
893 .probe = edt_ft5x06_ts_probe,