Andy Shevchenko | 6d3a41ab | 2019-03-03 23:21:56 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de> |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 4 | * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support) |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 5 | * Lothar Waßmann <LW@KARO-electronics.de> (DT support) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This is a driver for the EDT "Polytouch" family of touch controllers |
| 10 | * based on the FocalTech FT5x06 line of chips. |
| 11 | * |
| 12 | * Development of this driver has been sponsored by Glyn: |
| 13 | * http://www.glyn.com/Products/Displays |
| 14 | */ |
| 15 | |
Marco Felsch | 8726e4c | 2020-01-09 17:03:32 -0800 | [diff] [blame] | 16 | #include <linux/debugfs.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/gpio/consumer.h> |
| 19 | #include <linux/i2c.h> |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/input.h> |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 22 | #include <linux/input/mt.h> |
Maxime Ripard | 2c00559 | 2015-03-21 20:18:06 -0700 | [diff] [blame] | 23 | #include <linux/input/touchscreen.h> |
Marco Felsch | 8726e4c | 2020-01-09 17:03:32 -0800 | [diff] [blame] | 24 | #include <linux/irq.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/ratelimit.h> |
Mylène Josserand | 7448bfe | 2019-10-28 20:56:23 -0700 | [diff] [blame] | 28 | #include <linux/regulator/consumer.h> |
Marco Felsch | 8726e4c | 2020-01-09 17:03:32 -0800 | [diff] [blame] | 29 | #include <linux/slab.h> |
| 30 | #include <linux/uaccess.h> |
| 31 | |
| 32 | #include <asm/unaligned.h> |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 33 | |
| 34 | #define WORK_REGISTER_THRESHOLD 0x00 |
| 35 | #define WORK_REGISTER_REPORT_RATE 0x08 |
| 36 | #define WORK_REGISTER_GAIN 0x30 |
| 37 | #define WORK_REGISTER_OFFSET 0x31 |
| 38 | #define WORK_REGISTER_NUM_X 0x33 |
| 39 | #define WORK_REGISTER_NUM_Y 0x34 |
| 40 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 41 | #define M09_REGISTER_THRESHOLD 0x80 |
| 42 | #define M09_REGISTER_GAIN 0x92 |
| 43 | #define M09_REGISTER_OFFSET 0x93 |
| 44 | #define M09_REGISTER_NUM_X 0x94 |
| 45 | #define M09_REGISTER_NUM_Y 0x95 |
| 46 | |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 47 | #define EV_REGISTER_THRESHOLD 0x40 |
| 48 | #define EV_REGISTER_GAIN 0x41 |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 49 | #define EV_REGISTER_OFFSET_Y 0x45 |
| 50 | #define EV_REGISTER_OFFSET_X 0x46 |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 51 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 52 | #define NO_REGISTER 0xff |
| 53 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 54 | #define WORK_REGISTER_OPMODE 0x3c |
| 55 | #define FACTORY_REGISTER_OPMODE 0x01 |
| 56 | |
| 57 | #define TOUCH_EVENT_DOWN 0x00 |
| 58 | #define TOUCH_EVENT_UP 0x01 |
| 59 | #define TOUCH_EVENT_ON 0x02 |
| 60 | #define TOUCH_EVENT_RESERVED 0x03 |
| 61 | |
| 62 | #define EDT_NAME_LEN 23 |
| 63 | #define EDT_SWITCH_MODE_RETRIES 10 |
| 64 | #define EDT_SWITCH_MODE_DELAY 5 /* msec */ |
| 65 | #define EDT_RAW_DATA_RETRIES 100 |
Aniroop Mathur | 0eeecf6 | 2017-01-04 10:57:51 -0800 | [diff] [blame] | 66 | #define EDT_RAW_DATA_DELAY 1000 /* usec */ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 67 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 68 | enum edt_ver { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 69 | EDT_M06, |
| 70 | EDT_M09, |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 71 | EDT_M12, |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 72 | EV_FT, |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 73 | GENERIC_FT, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | struct edt_reg_addr { |
| 77 | int reg_threshold; |
| 78 | int reg_report_rate; |
| 79 | int reg_gain; |
| 80 | int reg_offset; |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 81 | int reg_offset_x; |
| 82 | int reg_offset_y; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 83 | int reg_num_x; |
| 84 | int reg_num_y; |
| 85 | }; |
| 86 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 87 | struct edt_ft5x06_ts_data { |
| 88 | struct i2c_client *client; |
| 89 | struct input_dev *input; |
Hans de Goede | ad368eb | 2016-07-15 14:26:53 -0700 | [diff] [blame] | 90 | struct touchscreen_properties prop; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 91 | u16 num_x; |
| 92 | u16 num_y; |
Mylène Josserand | 7448bfe | 2019-10-28 20:56:23 -0700 | [diff] [blame] | 93 | struct regulator *vcc; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 94 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 95 | struct gpio_desc *reset_gpio; |
| 96 | struct gpio_desc *wake_gpio; |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 97 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 98 | #if defined(CONFIG_DEBUG_FS) |
| 99 | struct dentry *debug_dir; |
| 100 | u8 *raw_buffer; |
| 101 | size_t raw_bufsize; |
| 102 | #endif |
| 103 | |
| 104 | struct mutex mutex; |
| 105 | bool factory_mode; |
| 106 | int threshold; |
| 107 | int gain; |
| 108 | int offset; |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 109 | int offset_x; |
| 110 | int offset_y; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 111 | int report_rate; |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 112 | int max_support_points; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 113 | |
| 114 | char name[EDT_NAME_LEN]; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 115 | |
| 116 | struct edt_reg_addr reg_addr; |
| 117 | enum edt_ver version; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 120 | struct edt_i2c_chip_data { |
| 121 | int max_support_points; |
| 122 | }; |
| 123 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 124 | static int edt_ft5x06_ts_readwrite(struct i2c_client *client, |
| 125 | u16 wr_len, u8 *wr_buf, |
| 126 | u16 rd_len, u8 *rd_buf) |
| 127 | { |
| 128 | struct i2c_msg wrmsg[2]; |
| 129 | int i = 0; |
| 130 | int ret; |
| 131 | |
| 132 | if (wr_len) { |
| 133 | wrmsg[i].addr = client->addr; |
| 134 | wrmsg[i].flags = 0; |
| 135 | wrmsg[i].len = wr_len; |
| 136 | wrmsg[i].buf = wr_buf; |
| 137 | i++; |
| 138 | } |
| 139 | if (rd_len) { |
| 140 | wrmsg[i].addr = client->addr; |
| 141 | wrmsg[i].flags = I2C_M_RD; |
| 142 | wrmsg[i].len = rd_len; |
| 143 | wrmsg[i].buf = rd_buf; |
| 144 | i++; |
| 145 | } |
| 146 | |
| 147 | ret = i2c_transfer(client->adapter, wrmsg, i); |
| 148 | if (ret < 0) |
| 149 | return ret; |
| 150 | if (ret != i) |
| 151 | return -EIO; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata, |
| 157 | u8 *buf, int buflen) |
| 158 | { |
| 159 | int i; |
| 160 | u8 crc = 0; |
| 161 | |
| 162 | for (i = 0; i < buflen - 1; i++) |
| 163 | crc ^= buf[i]; |
| 164 | |
| 165 | if (crc != buf[buflen-1]) { |
| 166 | dev_err_ratelimited(&tsdata->client->dev, |
| 167 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 168 | crc, buf[buflen-1]); |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id) |
| 176 | { |
| 177 | struct edt_ft5x06_ts_data *tsdata = dev_id; |
| 178 | struct device *dev = &tsdata->client->dev; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 179 | u8 cmd; |
Franklin S Cooper Jr | 9378c02 | 2015-10-16 15:34:41 -0700 | [diff] [blame] | 180 | u8 rdbuf[63]; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 181 | int i, type, x, y, id; |
Franklin S Cooper Jr | c789f1f | 2015-10-16 15:34:05 -0700 | [diff] [blame] | 182 | int offset, tplen, datalen, crclen; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 183 | int error; |
| 184 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 185 | switch (tsdata->version) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 186 | case EDT_M06: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 187 | cmd = 0xf9; /* tell the controller to send touch data */ |
| 188 | offset = 5; /* where the actual touch data starts */ |
| 189 | tplen = 4; /* data comes in so called frames */ |
Franklin S Cooper Jr | c789f1f | 2015-10-16 15:34:05 -0700 | [diff] [blame] | 190 | crclen = 1; /* length of the crc data */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 191 | break; |
| 192 | |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 193 | case EDT_M09: |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 194 | case EDT_M12: |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 195 | case EV_FT: |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 196 | case GENERIC_FT: |
Franklin S Cooper Jr | 9378c02 | 2015-10-16 15:34:41 -0700 | [diff] [blame] | 197 | cmd = 0x0; |
| 198 | offset = 3; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 199 | tplen = 6; |
Franklin S Cooper Jr | c789f1f | 2015-10-16 15:34:05 -0700 | [diff] [blame] | 200 | crclen = 0; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 201 | break; |
| 202 | |
| 203 | default: |
| 204 | goto out; |
| 205 | } |
| 206 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 207 | memset(rdbuf, 0, sizeof(rdbuf)); |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 208 | datalen = tplen * tsdata->max_support_points + offset + crclen; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 209 | |
| 210 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 211 | sizeof(cmd), &cmd, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 212 | datalen, rdbuf); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 213 | if (error) { |
| 214 | dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n", |
| 215 | error); |
| 216 | goto out; |
| 217 | } |
| 218 | |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 219 | /* M09/M12 does not send header or CRC */ |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 220 | if (tsdata->version == EDT_M06) { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 221 | if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa || |
| 222 | rdbuf[2] != datalen) { |
| 223 | dev_err_ratelimited(dev, |
| 224 | "Unexpected header: %02x%02x%02x!\n", |
| 225 | rdbuf[0], rdbuf[1], rdbuf[2]); |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen)) |
| 230 | goto out; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 233 | for (i = 0; i < tsdata->max_support_points; i++) { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 234 | u8 *buf = &rdbuf[i * tplen + offset]; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 235 | |
| 236 | type = buf[0] >> 6; |
| 237 | /* ignore Reserved events */ |
| 238 | if (type == TOUCH_EVENT_RESERVED) |
| 239 | continue; |
| 240 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 241 | /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */ |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 242 | if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN) |
Lothar Waßmann | ee3e946 | 2014-03-28 09:28:17 -0700 | [diff] [blame] | 243 | continue; |
| 244 | |
Dmitry Torokhov | 1b9c698 | 2019-06-22 23:11:51 -0700 | [diff] [blame] | 245 | x = get_unaligned_be16(buf) & 0x0fff; |
| 246 | y = get_unaligned_be16(buf + 2) & 0x0fff; |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 247 | /* The FT5x26 send the y coordinate first */ |
| 248 | if (tsdata->version == EV_FT) |
| 249 | swap(x, y); |
| 250 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 251 | id = (buf[2] >> 4) & 0x0f; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 252 | |
| 253 | input_mt_slot(tsdata->input, id); |
Dmitry Torokhov | 17b9292 | 2019-06-22 23:21:24 -0700 | [diff] [blame] | 254 | if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, |
| 255 | type != TOUCH_EVENT_UP)) |
| 256 | touchscreen_report_pos(tsdata->input, &tsdata->prop, |
| 257 | x, y, true); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | input_mt_report_pointer_emulation(tsdata->input, true); |
| 261 | input_sync(tsdata->input); |
| 262 | |
| 263 | out: |
| 264 | return IRQ_HANDLED; |
| 265 | } |
| 266 | |
| 267 | static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata, |
| 268 | u8 addr, u8 value) |
| 269 | { |
| 270 | u8 wrbuf[4]; |
| 271 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 272 | switch (tsdata->version) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 273 | case EDT_M06: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 274 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 275 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 276 | wrbuf[2] = value; |
| 277 | wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2]; |
| 278 | return edt_ft5x06_ts_readwrite(tsdata->client, 4, |
| 279 | wrbuf, 0, NULL); |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 280 | /* fallthrough */ |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 281 | case EDT_M09: |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 282 | case EDT_M12: |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 283 | case EV_FT: |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 284 | case GENERIC_FT: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 285 | wrbuf[0] = addr; |
| 286 | wrbuf[1] = value; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 287 | |
Robert Woerle | cc071ac | 2014-06-07 22:20:23 -0700 | [diff] [blame] | 288 | return edt_ft5x06_ts_readwrite(tsdata->client, 2, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 289 | wrbuf, 0, NULL); |
| 290 | |
| 291 | default: |
| 292 | return -EINVAL; |
| 293 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata, |
| 297 | u8 addr) |
| 298 | { |
| 299 | u8 wrbuf[2], rdbuf[2]; |
| 300 | int error; |
| 301 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 302 | switch (tsdata->version) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 303 | case EDT_M06: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 304 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 305 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
| 306 | wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 307 | |
Dan Carpenter | e2c3ecf | 2014-04-03 09:17:05 -0700 | [diff] [blame] | 308 | error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2, |
| 309 | rdbuf); |
| 310 | if (error) |
| 311 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 312 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 313 | if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) { |
| 314 | dev_err(&tsdata->client->dev, |
| 315 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 316 | wrbuf[0] ^ wrbuf[1] ^ rdbuf[0], |
| 317 | rdbuf[1]); |
| 318 | return -EIO; |
| 319 | } |
| 320 | break; |
| 321 | |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 322 | /* fallthrough */ |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 323 | case EDT_M09: |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 324 | case EDT_M12: |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 325 | case EV_FT: |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 326 | case GENERIC_FT: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 327 | wrbuf[0] = addr; |
| 328 | error = edt_ft5x06_ts_readwrite(tsdata->client, 1, |
| 329 | wrbuf, 1, rdbuf); |
| 330 | if (error) |
| 331 | return error; |
| 332 | break; |
| 333 | |
| 334 | default: |
| 335 | return -EINVAL; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return rdbuf[0]; |
| 339 | } |
| 340 | |
| 341 | struct edt_ft5x06_attribute { |
| 342 | struct device_attribute dattr; |
| 343 | size_t field_offset; |
| 344 | u8 limit_low; |
| 345 | u8 limit_high; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 346 | u8 addr_m06; |
| 347 | u8 addr_m09; |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 348 | u8 addr_ev; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 351 | #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 352 | _limit_low, _limit_high) \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 353 | struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \ |
| 354 | .dattr = __ATTR(_field, _mode, \ |
| 355 | edt_ft5x06_setting_show, \ |
| 356 | edt_ft5x06_setting_store), \ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 357 | .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \ |
| 358 | .addr_m06 = _addr_m06, \ |
| 359 | .addr_m09 = _addr_m09, \ |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 360 | .addr_ev = _addr_ev, \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 361 | .limit_low = _limit_low, \ |
| 362 | .limit_high = _limit_high, \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static ssize_t edt_ft5x06_setting_show(struct device *dev, |
| 366 | struct device_attribute *dattr, |
| 367 | char *buf) |
| 368 | { |
| 369 | struct i2c_client *client = to_i2c_client(dev); |
| 370 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 371 | struct edt_ft5x06_attribute *attr = |
| 372 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 373 | u8 *field = (u8 *)tsdata + attr->field_offset; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 374 | int val; |
| 375 | size_t count = 0; |
| 376 | int error = 0; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 377 | u8 addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 378 | |
| 379 | mutex_lock(&tsdata->mutex); |
| 380 | |
| 381 | if (tsdata->factory_mode) { |
| 382 | error = -EIO; |
| 383 | goto out; |
| 384 | } |
| 385 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 386 | switch (tsdata->version) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 387 | case EDT_M06: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 388 | addr = attr->addr_m06; |
| 389 | break; |
| 390 | |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 391 | case EDT_M09: |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 392 | case EDT_M12: |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 393 | case GENERIC_FT: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 394 | addr = attr->addr_m09; |
| 395 | break; |
| 396 | |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 397 | case EV_FT: |
| 398 | addr = attr->addr_ev; |
| 399 | break; |
| 400 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 401 | default: |
| 402 | error = -ENODEV; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 403 | goto out; |
| 404 | } |
| 405 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 406 | if (addr != NO_REGISTER) { |
| 407 | val = edt_ft5x06_register_read(tsdata, addr); |
| 408 | if (val < 0) { |
| 409 | error = val; |
| 410 | dev_err(&tsdata->client->dev, |
| 411 | "Failed to fetch attribute %s, error %d\n", |
| 412 | dattr->attr.name, error); |
| 413 | goto out; |
| 414 | } |
| 415 | } else { |
| 416 | val = *field; |
| 417 | } |
| 418 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 419 | if (val != *field) { |
| 420 | dev_warn(&tsdata->client->dev, |
| 421 | "%s: read (%d) and stored value (%d) differ\n", |
| 422 | dattr->attr.name, val, *field); |
| 423 | *field = val; |
| 424 | } |
| 425 | |
| 426 | count = scnprintf(buf, PAGE_SIZE, "%d\n", val); |
| 427 | out: |
| 428 | mutex_unlock(&tsdata->mutex); |
| 429 | return error ?: count; |
| 430 | } |
| 431 | |
| 432 | static ssize_t edt_ft5x06_setting_store(struct device *dev, |
| 433 | struct device_attribute *dattr, |
| 434 | const char *buf, size_t count) |
| 435 | { |
| 436 | struct i2c_client *client = to_i2c_client(dev); |
| 437 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 438 | struct edt_ft5x06_attribute *attr = |
| 439 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 440 | u8 *field = (u8 *)tsdata + attr->field_offset; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 441 | unsigned int val; |
| 442 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 443 | u8 addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 444 | |
| 445 | mutex_lock(&tsdata->mutex); |
| 446 | |
| 447 | if (tsdata->factory_mode) { |
| 448 | error = -EIO; |
| 449 | goto out; |
| 450 | } |
| 451 | |
| 452 | error = kstrtouint(buf, 0, &val); |
| 453 | if (error) |
| 454 | goto out; |
| 455 | |
| 456 | if (val < attr->limit_low || val > attr->limit_high) { |
| 457 | error = -ERANGE; |
| 458 | goto out; |
| 459 | } |
| 460 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 461 | switch (tsdata->version) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 462 | case EDT_M06: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 463 | addr = attr->addr_m06; |
| 464 | break; |
| 465 | |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 466 | case EDT_M09: |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 467 | case EDT_M12: |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 468 | case GENERIC_FT: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 469 | addr = attr->addr_m09; |
| 470 | break; |
| 471 | |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 472 | case EV_FT: |
| 473 | addr = attr->addr_ev; |
| 474 | break; |
| 475 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 476 | default: |
| 477 | error = -ENODEV; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 478 | goto out; |
| 479 | } |
| 480 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 481 | if (addr != NO_REGISTER) { |
| 482 | error = edt_ft5x06_register_write(tsdata, addr, val); |
| 483 | if (error) { |
| 484 | dev_err(&tsdata->client->dev, |
| 485 | "Failed to update attribute %s, error: %d\n", |
| 486 | dattr->attr.name, error); |
| 487 | goto out; |
| 488 | } |
| 489 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 490 | *field = val; |
| 491 | |
| 492 | out: |
| 493 | mutex_unlock(&tsdata->mutex); |
| 494 | return error ?: count; |
| 495 | } |
| 496 | |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 497 | /* m06, m09: range 0-31, m12: range 0-5 */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 498 | static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN, |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 499 | M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31); |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 500 | /* m06, m09: range 0-31, m12: range 0-16 */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 501 | static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET, |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 502 | M09_REGISTER_OFFSET, NO_REGISTER, 0, 31); |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 503 | /* m06, m09, m12: no supported, ev_ft: range 0-80 */ |
| 504 | static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER, |
| 505 | EV_REGISTER_OFFSET_X, 0, 80); |
| 506 | /* m06, m09, m12: no supported, ev_ft: range 0-80 */ |
| 507 | static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER, |
| 508 | EV_REGISTER_OFFSET_Y, 0, 80); |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 509 | /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 510 | static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD, |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 511 | M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255); |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 512 | /* m06: range 3 to 14, m12: (0x64: 100Hz) */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 513 | static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE, |
Marco Felsch | 2ebc191 | 2019-01-13 23:10:22 -0800 | [diff] [blame] | 514 | NO_REGISTER, NO_REGISTER, 0, 255); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 515 | |
| 516 | static struct attribute *edt_ft5x06_attrs[] = { |
| 517 | &edt_ft5x06_attr_gain.dattr.attr, |
| 518 | &edt_ft5x06_attr_offset.dattr.attr, |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 519 | &edt_ft5x06_attr_offset_x.dattr.attr, |
| 520 | &edt_ft5x06_attr_offset_y.dattr.attr, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 521 | &edt_ft5x06_attr_threshold.dattr.attr, |
| 522 | &edt_ft5x06_attr_report_rate.dattr.attr, |
| 523 | NULL |
| 524 | }; |
| 525 | |
| 526 | static const struct attribute_group edt_ft5x06_attr_group = { |
| 527 | .attrs = edt_ft5x06_attrs, |
| 528 | }; |
| 529 | |
| 530 | #ifdef CONFIG_DEBUG_FS |
| 531 | static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata) |
| 532 | { |
| 533 | struct i2c_client *client = tsdata->client; |
| 534 | int retries = EDT_SWITCH_MODE_RETRIES; |
| 535 | int ret; |
| 536 | int error; |
| 537 | |
Dmitry Torokhov | 4b3e910d | 2018-01-23 10:01:57 -0800 | [diff] [blame] | 538 | if (tsdata->version != EDT_M06) { |
| 539 | dev_err(&client->dev, |
| 540 | "No factory mode support for non-M06 devices\n"); |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 544 | disable_irq(client->irq); |
| 545 | |
| 546 | if (!tsdata->raw_buffer) { |
| 547 | tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y * |
| 548 | sizeof(u16); |
| 549 | tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL); |
| 550 | if (!tsdata->raw_buffer) { |
| 551 | error = -ENOMEM; |
| 552 | goto err_out; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* mode register is 0x3c when in the work mode */ |
| 557 | error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03); |
| 558 | if (error) { |
| 559 | dev_err(&client->dev, |
| 560 | "failed to switch to factory mode, error %d\n", error); |
| 561 | goto err_out; |
| 562 | } |
| 563 | |
| 564 | tsdata->factory_mode = true; |
| 565 | do { |
| 566 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 567 | /* mode register is 0x01 when in factory mode */ |
| 568 | ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE); |
| 569 | if (ret == 0x03) |
| 570 | break; |
| 571 | } while (--retries > 0); |
| 572 | |
| 573 | if (retries == 0) { |
| 574 | dev_err(&client->dev, "not in factory mode after %dms.\n", |
| 575 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 576 | error = -EIO; |
| 577 | goto err_out; |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | |
| 582 | err_out: |
| 583 | kfree(tsdata->raw_buffer); |
| 584 | tsdata->raw_buffer = NULL; |
| 585 | tsdata->factory_mode = false; |
| 586 | enable_irq(client->irq); |
| 587 | |
| 588 | return error; |
| 589 | } |
| 590 | |
| 591 | static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata) |
| 592 | { |
| 593 | struct i2c_client *client = tsdata->client; |
| 594 | int retries = EDT_SWITCH_MODE_RETRIES; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 595 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 596 | int ret; |
| 597 | int error; |
| 598 | |
| 599 | /* mode register is 0x01 when in the factory mode */ |
| 600 | error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1); |
| 601 | if (error) { |
| 602 | dev_err(&client->dev, |
| 603 | "failed to switch to work mode, error: %d\n", error); |
| 604 | return error; |
| 605 | } |
| 606 | |
| 607 | tsdata->factory_mode = false; |
| 608 | |
| 609 | do { |
| 610 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 611 | /* mode register is 0x01 when in factory mode */ |
| 612 | ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE); |
| 613 | if (ret == 0x01) |
| 614 | break; |
| 615 | } while (--retries > 0); |
| 616 | |
| 617 | if (retries == 0) { |
| 618 | dev_err(&client->dev, "not in work mode after %dms.\n", |
| 619 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 620 | tsdata->factory_mode = true; |
| 621 | return -EIO; |
| 622 | } |
| 623 | |
Sachin Kamat | 8efcc50 | 2013-03-28 01:14:42 -0700 | [diff] [blame] | 624 | kfree(tsdata->raw_buffer); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 625 | tsdata->raw_buffer = NULL; |
| 626 | |
| 627 | /* restore parameters */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 628 | edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 629 | tsdata->threshold); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 630 | edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 631 | tsdata->gain); |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 632 | if (reg_addr->reg_offset != NO_REGISTER) |
| 633 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, |
| 634 | tsdata->offset); |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 635 | if (reg_addr->reg_offset_x != NO_REGISTER) |
| 636 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x, |
| 637 | tsdata->offset_x); |
| 638 | if (reg_addr->reg_offset_y != NO_REGISTER) |
| 639 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y, |
| 640 | tsdata->offset_y); |
Luca Ceresoli | 4701475 | 2017-09-07 14:28:28 -0700 | [diff] [blame] | 641 | if (reg_addr->reg_report_rate != NO_REGISTER) |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 642 | edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 643 | tsdata->report_rate); |
| 644 | |
| 645 | enable_irq(client->irq); |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode) |
| 651 | { |
| 652 | struct edt_ft5x06_ts_data *tsdata = data; |
| 653 | |
| 654 | *mode = tsdata->factory_mode; |
| 655 | |
| 656 | return 0; |
| 657 | }; |
| 658 | |
| 659 | static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode) |
| 660 | { |
| 661 | struct edt_ft5x06_ts_data *tsdata = data; |
| 662 | int retval = 0; |
| 663 | |
| 664 | if (mode > 1) |
| 665 | return -ERANGE; |
| 666 | |
| 667 | mutex_lock(&tsdata->mutex); |
| 668 | |
| 669 | if (mode != tsdata->factory_mode) { |
| 670 | retval = mode ? edt_ft5x06_factory_mode(tsdata) : |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 671 | edt_ft5x06_work_mode(tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | mutex_unlock(&tsdata->mutex); |
| 675 | |
| 676 | return retval; |
| 677 | }; |
| 678 | |
| 679 | DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get, |
| 680 | edt_ft5x06_debugfs_mode_set, "%llu\n"); |
| 681 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 682 | static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file, |
| 683 | char __user *buf, size_t count, loff_t *off) |
| 684 | { |
| 685 | struct edt_ft5x06_ts_data *tsdata = file->private_data; |
| 686 | struct i2c_client *client = tsdata->client; |
| 687 | int retries = EDT_RAW_DATA_RETRIES; |
| 688 | int val, i, error; |
| 689 | size_t read = 0; |
| 690 | int colbytes; |
| 691 | char wrbuf[3]; |
| 692 | u8 *rdbuf; |
| 693 | |
| 694 | if (*off < 0 || *off >= tsdata->raw_bufsize) |
| 695 | return 0; |
| 696 | |
| 697 | mutex_lock(&tsdata->mutex); |
| 698 | |
| 699 | if (!tsdata->factory_mode || !tsdata->raw_buffer) { |
| 700 | error = -EIO; |
| 701 | goto out; |
| 702 | } |
| 703 | |
| 704 | error = edt_ft5x06_register_write(tsdata, 0x08, 0x01); |
| 705 | if (error) { |
| 706 | dev_dbg(&client->dev, |
| 707 | "failed to write 0x08 register, error %d\n", error); |
| 708 | goto out; |
| 709 | } |
| 710 | |
| 711 | do { |
Aniroop Mathur | 0eeecf6 | 2017-01-04 10:57:51 -0800 | [diff] [blame] | 712 | usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 713 | val = edt_ft5x06_register_read(tsdata, 0x08); |
| 714 | if (val < 1) |
| 715 | break; |
| 716 | } while (--retries > 0); |
| 717 | |
| 718 | if (val < 0) { |
| 719 | error = val; |
| 720 | dev_dbg(&client->dev, |
| 721 | "failed to read 0x08 register, error %d\n", error); |
| 722 | goto out; |
| 723 | } |
| 724 | |
| 725 | if (retries == 0) { |
| 726 | dev_dbg(&client->dev, |
| 727 | "timed out waiting for register to settle\n"); |
| 728 | error = -ETIMEDOUT; |
| 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | rdbuf = tsdata->raw_buffer; |
| 733 | colbytes = tsdata->num_y * sizeof(u16); |
| 734 | |
| 735 | wrbuf[0] = 0xf5; |
| 736 | wrbuf[1] = 0x0e; |
| 737 | for (i = 0; i < tsdata->num_x; i++) { |
| 738 | wrbuf[2] = i; /* column index */ |
| 739 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 740 | sizeof(wrbuf), wrbuf, |
| 741 | colbytes, rdbuf); |
| 742 | if (error) |
| 743 | goto out; |
| 744 | |
| 745 | rdbuf += colbytes; |
| 746 | } |
| 747 | |
| 748 | read = min_t(size_t, count, tsdata->raw_bufsize - *off); |
Axel Lin | 35b1da4 | 2012-09-19 15:56:23 -0700 | [diff] [blame] | 749 | if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) { |
| 750 | error = -EFAULT; |
| 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | *off += read; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 755 | out: |
| 756 | mutex_unlock(&tsdata->mutex); |
| 757 | return error ?: read; |
| 758 | }; |
| 759 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 760 | static const struct file_operations debugfs_raw_data_fops = { |
Wei Yongjun | f6c0df6 | 2012-10-17 23:55:54 -0700 | [diff] [blame] | 761 | .open = simple_open, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 762 | .read = edt_ft5x06_debugfs_raw_data_read, |
| 763 | }; |
| 764 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 765 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 766 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 767 | const char *debugfs_name) |
| 768 | { |
| 769 | tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 770 | |
| 771 | debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x); |
| 772 | debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y); |
| 773 | |
| 774 | debugfs_create_file("mode", S_IRUSR | S_IWUSR, |
| 775 | tsdata->debug_dir, tsdata, &debugfs_mode_fops); |
| 776 | debugfs_create_file("raw_data", S_IRUSR, |
| 777 | tsdata->debug_dir, tsdata, &debugfs_raw_data_fops); |
| 778 | } |
| 779 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 780 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 781 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 782 | { |
Fabian Frederick | f69c6ec | 2014-07-09 09:45:57 -0700 | [diff] [blame] | 783 | debugfs_remove_recursive(tsdata->debug_dir); |
Guenter Roeck | a1d0fa7 | 2012-08-21 22:01:45 -0700 | [diff] [blame] | 784 | kfree(tsdata->raw_buffer); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | #else |
| 788 | |
| 789 | static inline void |
| 790 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 791 | const char *debugfs_name) |
| 792 | { |
| 793 | } |
| 794 | |
| 795 | static inline void |
| 796 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 797 | { |
| 798 | } |
| 799 | |
| 800 | #endif /* CONFIG_DEBUGFS */ |
| 801 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 802 | static int edt_ft5x06_ts_identify(struct i2c_client *client, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 803 | struct edt_ft5x06_ts_data *tsdata, |
| 804 | char *fw_version) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 805 | { |
| 806 | u8 rdbuf[EDT_NAME_LEN]; |
| 807 | char *p; |
| 808 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 809 | char *model_name = tsdata->name; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 810 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 811 | /* see what we find if we assume it is a M06 * |
| 812 | * if we get less than EDT_NAME_LEN, we don't want |
| 813 | * to have garbage in there |
| 814 | */ |
| 815 | memset(rdbuf, 0, sizeof(rdbuf)); |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 816 | error = edt_ft5x06_ts_readwrite(client, 1, "\xBB", |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 817 | EDT_NAME_LEN - 1, rdbuf); |
| 818 | if (error) |
| 819 | return error; |
| 820 | |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 821 | /* Probe content for something consistent. |
| 822 | * M06 starts with a response byte, M12 gives the data directly. |
| 823 | * M09/Generic does not provide model number information. |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 824 | */ |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 825 | if (!strncasecmp(rdbuf + 1, "EP0", 3)) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 826 | tsdata->version = EDT_M06; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 827 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 828 | /* remove last '$' end marker */ |
| 829 | rdbuf[EDT_NAME_LEN - 1] = '\0'; |
| 830 | if (rdbuf[EDT_NAME_LEN - 2] == '$') |
| 831 | rdbuf[EDT_NAME_LEN - 2] = '\0'; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 832 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 833 | /* look for Model/Version separator */ |
| 834 | p = strchr(rdbuf, '*'); |
| 835 | if (p) |
| 836 | *p++ = '\0'; |
| 837 | strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN); |
| 838 | strlcpy(fw_version, p ? p : "", EDT_NAME_LEN); |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 839 | } else if (!strncasecmp(rdbuf, "EP0", 3)) { |
| 840 | tsdata->version = EDT_M12; |
| 841 | |
| 842 | /* remove last '$' end marker */ |
| 843 | rdbuf[EDT_NAME_LEN - 2] = '\0'; |
| 844 | if (rdbuf[EDT_NAME_LEN - 3] == '$') |
| 845 | rdbuf[EDT_NAME_LEN - 3] = '\0'; |
| 846 | |
| 847 | /* look for Model/Version separator */ |
| 848 | p = strchr(rdbuf, '*'); |
| 849 | if (p) |
| 850 | *p++ = '\0'; |
| 851 | strlcpy(model_name, rdbuf, EDT_NAME_LEN); |
| 852 | strlcpy(fw_version, p ? p : "", EDT_NAME_LEN); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 853 | } else { |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 854 | /* If it is not an EDT M06/M12 touchscreen, then the model |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 855 | * detection is a bit hairy. The different ft5x06 |
| 856 | * firmares around don't reliably implement the |
| 857 | * identification registers. Well, we'll take a shot. |
| 858 | * |
| 859 | * The main difference between generic focaltec based |
| 860 | * touches and EDT M09 is that we know how to retrieve |
| 861 | * the max coordinates for the latter. |
| 862 | */ |
| 863 | tsdata->version = GENERIC_FT; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 864 | |
| 865 | error = edt_ft5x06_ts_readwrite(client, 1, "\xA6", |
| 866 | 2, rdbuf); |
| 867 | if (error) |
| 868 | return error; |
| 869 | |
| 870 | strlcpy(fw_version, rdbuf, 2); |
| 871 | |
| 872 | error = edt_ft5x06_ts_readwrite(client, 1, "\xA8", |
| 873 | 1, rdbuf); |
| 874 | if (error) |
| 875 | return error; |
| 876 | |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 877 | /* This "model identification" is not exact. Unfortunately |
| 878 | * not all firmwares for the ft5x06 put useful values in |
| 879 | * the identification registers. |
| 880 | */ |
| 881 | switch (rdbuf[0]) { |
| 882 | case 0x35: /* EDT EP0350M09 */ |
| 883 | case 0x43: /* EDT EP0430M09 */ |
| 884 | case 0x50: /* EDT EP0500M09 */ |
| 885 | case 0x57: /* EDT EP0570M09 */ |
| 886 | case 0x70: /* EDT EP0700M09 */ |
| 887 | tsdata->version = EDT_M09; |
| 888 | snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09", |
| 889 | rdbuf[0] >> 4, rdbuf[0] & 0x0F); |
| 890 | break; |
| 891 | case 0xa1: /* EDT EP1010ML00 */ |
| 892 | tsdata->version = EDT_M09; |
| 893 | snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00", |
| 894 | rdbuf[0] >> 4, rdbuf[0] & 0x0F); |
| 895 | break; |
| 896 | case 0x5a: /* Solomon Goldentek Display */ |
| 897 | snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0"); |
| 898 | break; |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 899 | case 0x59: /* Evervision Display with FT5xx6 TS */ |
| 900 | tsdata->version = EV_FT; |
| 901 | error = edt_ft5x06_ts_readwrite(client, 1, "\x53", |
| 902 | 1, rdbuf); |
| 903 | if (error) |
| 904 | return error; |
| 905 | strlcpy(fw_version, rdbuf, 1); |
| 906 | snprintf(model_name, EDT_NAME_LEN, |
| 907 | "EVERVISION-FT5726NEi"); |
| 908 | break; |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 909 | default: |
| 910 | snprintf(model_name, EDT_NAME_LEN, |
| 911 | "generic ft5x06 (%02x)", |
| 912 | rdbuf[0]); |
| 913 | break; |
| 914 | } |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 915 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame] | 920 | static void edt_ft5x06_ts_get_defaults(struct device *dev, |
Dmitry Torokhov | 22ddbac | 2015-09-12 09:37:03 -0700 | [diff] [blame] | 921 | struct edt_ft5x06_ts_data *tsdata) |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 922 | { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 923 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame] | 924 | u32 val; |
| 925 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 926 | |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame] | 927 | error = device_property_read_u32(dev, "threshold", &val); |
Philipp Zabel | dc262df | 2016-02-09 09:32:42 -0800 | [diff] [blame] | 928 | if (!error) { |
| 929 | edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val); |
| 930 | tsdata->threshold = val; |
| 931 | } |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame] | 932 | |
| 933 | error = device_property_read_u32(dev, "gain", &val); |
Philipp Zabel | dc262df | 2016-02-09 09:32:42 -0800 | [diff] [blame] | 934 | if (!error) { |
| 935 | edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val); |
| 936 | tsdata->gain = val; |
| 937 | } |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame] | 938 | |
| 939 | error = device_property_read_u32(dev, "offset", &val); |
Philipp Zabel | dc262df | 2016-02-09 09:32:42 -0800 | [diff] [blame] | 940 | if (!error) { |
| 941 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val); |
| 942 | tsdata->offset = val; |
| 943 | } |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 944 | |
| 945 | error = device_property_read_u32(dev, "offset-x", &val); |
| 946 | if (!error) { |
| 947 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x, val); |
| 948 | tsdata->offset_x = val; |
| 949 | } |
| 950 | |
| 951 | error = device_property_read_u32(dev, "offset-y", &val); |
| 952 | if (!error) { |
| 953 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y, val); |
| 954 | tsdata->offset_y = val; |
| 955 | } |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 958 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 959 | edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata) |
| 960 | { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 961 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 962 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 963 | tsdata->threshold = edt_ft5x06_register_read(tsdata, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 964 | reg_addr->reg_threshold); |
| 965 | tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain); |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 966 | if (reg_addr->reg_offset != NO_REGISTER) |
| 967 | tsdata->offset = |
| 968 | edt_ft5x06_register_read(tsdata, reg_addr->reg_offset); |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 969 | if (reg_addr->reg_offset_x != NO_REGISTER) |
| 970 | tsdata->offset_x = edt_ft5x06_register_read(tsdata, |
| 971 | reg_addr->reg_offset_x); |
| 972 | if (reg_addr->reg_offset_y != NO_REGISTER) |
| 973 | tsdata->offset_y = edt_ft5x06_register_read(tsdata, |
| 974 | reg_addr->reg_offset_y); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 975 | if (reg_addr->reg_report_rate != NO_REGISTER) |
| 976 | tsdata->report_rate = edt_ft5x06_register_read(tsdata, |
| 977 | reg_addr->reg_report_rate); |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 978 | if (tsdata->version == EDT_M06 || |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 979 | tsdata->version == EDT_M09 || |
| 980 | tsdata->version == EDT_M12) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 981 | tsdata->num_x = edt_ft5x06_register_read(tsdata, |
| 982 | reg_addr->reg_num_x); |
| 983 | tsdata->num_y = edt_ft5x06_register_read(tsdata, |
| 984 | reg_addr->reg_num_y); |
| 985 | } else { |
| 986 | tsdata->num_x = -1; |
| 987 | tsdata->num_y = -1; |
| 988 | } |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static void |
| 992 | edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata) |
| 993 | { |
| 994 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 995 | |
| 996 | switch (tsdata->version) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 997 | case EDT_M06: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 998 | reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD; |
| 999 | reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE; |
| 1000 | reg_addr->reg_gain = WORK_REGISTER_GAIN; |
| 1001 | reg_addr->reg_offset = WORK_REGISTER_OFFSET; |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 1002 | reg_addr->reg_offset_x = NO_REGISTER; |
| 1003 | reg_addr->reg_offset_y = NO_REGISTER; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 1004 | reg_addr->reg_num_x = WORK_REGISTER_NUM_X; |
| 1005 | reg_addr->reg_num_y = WORK_REGISTER_NUM_Y; |
| 1006 | break; |
| 1007 | |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 1008 | case EDT_M09: |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 1009 | case EDT_M12: |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 1010 | reg_addr->reg_threshold = M09_REGISTER_THRESHOLD; |
Luca Ceresoli | 4701475 | 2017-09-07 14:28:28 -0700 | [diff] [blame] | 1011 | reg_addr->reg_report_rate = NO_REGISTER; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 1012 | reg_addr->reg_gain = M09_REGISTER_GAIN; |
| 1013 | reg_addr->reg_offset = M09_REGISTER_OFFSET; |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 1014 | reg_addr->reg_offset_x = NO_REGISTER; |
| 1015 | reg_addr->reg_offset_y = NO_REGISTER; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 1016 | reg_addr->reg_num_x = M09_REGISTER_NUM_X; |
| 1017 | reg_addr->reg_num_y = M09_REGISTER_NUM_Y; |
| 1018 | break; |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 1019 | |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 1020 | case EV_FT: |
| 1021 | reg_addr->reg_threshold = EV_REGISTER_THRESHOLD; |
| 1022 | reg_addr->reg_gain = EV_REGISTER_GAIN; |
| 1023 | reg_addr->reg_offset = NO_REGISTER; |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 1024 | reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X; |
| 1025 | reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y; |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 1026 | reg_addr->reg_num_x = NO_REGISTER; |
| 1027 | reg_addr->reg_num_y = NO_REGISTER; |
| 1028 | reg_addr->reg_report_rate = NO_REGISTER; |
| 1029 | break; |
| 1030 | |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 1031 | case GENERIC_FT: |
| 1032 | /* this is a guesswork */ |
| 1033 | reg_addr->reg_threshold = M09_REGISTER_THRESHOLD; |
| 1034 | reg_addr->reg_gain = M09_REGISTER_GAIN; |
| 1035 | reg_addr->reg_offset = M09_REGISTER_OFFSET; |
Marco Felsch | b6eba86 | 2019-01-13 23:10:50 -0800 | [diff] [blame] | 1036 | reg_addr->reg_offset_x = NO_REGISTER; |
| 1037 | reg_addr->reg_offset_y = NO_REGISTER; |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 1038 | break; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 1039 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Mylène Josserand | 7448bfe | 2019-10-28 20:56:23 -0700 | [diff] [blame] | 1042 | static void edt_ft5x06_disable_regulator(void *arg) |
| 1043 | { |
| 1044 | struct edt_ft5x06_ts_data *data = arg; |
| 1045 | |
| 1046 | regulator_disable(data->vcc); |
| 1047 | } |
| 1048 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 1049 | static int edt_ft5x06_ts_probe(struct i2c_client *client, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1050 | const struct i2c_device_id *id) |
| 1051 | { |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 1052 | const struct edt_i2c_chip_data *chip_data; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1053 | struct edt_ft5x06_ts_data *tsdata; |
Philipp Zabel | e112324 | 2020-01-09 17:03:21 -0800 | [diff] [blame] | 1054 | u8 buf[2] = { 0xfc, 0x00 }; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1055 | struct input_dev *input; |
Dmitry Torokhov | f0bef75 | 2015-09-12 10:11:09 -0700 | [diff] [blame] | 1056 | unsigned long irq_flags; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1057 | int error; |
| 1058 | char fw_version[EDT_NAME_LEN]; |
| 1059 | |
| 1060 | dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n"); |
| 1061 | |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1062 | tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL); |
| 1063 | if (!tsdata) { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1064 | dev_err(&client->dev, "failed to allocate driver data.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1065 | return -ENOMEM; |
| 1066 | } |
| 1067 | |
Andy Shevchenko | fc226eb | 2019-03-03 23:21:28 -0800 | [diff] [blame] | 1068 | chip_data = device_get_match_data(&client->dev); |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 1069 | if (!chip_data) |
| 1070 | chip_data = (const struct edt_i2c_chip_data *)id->driver_data; |
| 1071 | if (!chip_data || !chip_data->max_support_points) { |
| 1072 | dev_err(&client->dev, "invalid or missing chip data\n"); |
| 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | |
| 1076 | tsdata->max_support_points = chip_data->max_support_points; |
| 1077 | |
Mylène Josserand | 7448bfe | 2019-10-28 20:56:23 -0700 | [diff] [blame] | 1078 | tsdata->vcc = devm_regulator_get(&client->dev, "vcc"); |
| 1079 | if (IS_ERR(tsdata->vcc)) { |
| 1080 | error = PTR_ERR(tsdata->vcc); |
| 1081 | if (error != -EPROBE_DEFER) |
| 1082 | dev_err(&client->dev, |
| 1083 | "failed to request regulator: %d\n", error); |
| 1084 | return error; |
| 1085 | } |
| 1086 | |
| 1087 | error = regulator_enable(tsdata->vcc); |
| 1088 | if (error < 0) { |
| 1089 | dev_err(&client->dev, "failed to enable vcc: %d\n", error); |
| 1090 | return error; |
| 1091 | } |
| 1092 | |
| 1093 | error = devm_add_action_or_reset(&client->dev, |
| 1094 | edt_ft5x06_disable_regulator, |
| 1095 | tsdata); |
| 1096 | if (error) |
| 1097 | return error; |
| 1098 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 1099 | tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev, |
| 1100 | "reset", GPIOD_OUT_HIGH); |
| 1101 | if (IS_ERR(tsdata->reset_gpio)) { |
| 1102 | error = PTR_ERR(tsdata->reset_gpio); |
| 1103 | dev_err(&client->dev, |
| 1104 | "Failed to request GPIO reset pin, error %d\n", error); |
| 1105 | return error; |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 1108 | tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev, |
| 1109 | "wake", GPIOD_OUT_LOW); |
| 1110 | if (IS_ERR(tsdata->wake_gpio)) { |
| 1111 | error = PTR_ERR(tsdata->wake_gpio); |
| 1112 | dev_err(&client->dev, |
| 1113 | "Failed to request GPIO wake pin, error %d\n", error); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1114 | return error; |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 1115 | } |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1116 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 1117 | if (tsdata->wake_gpio) { |
| 1118 | usleep_range(5000, 6000); |
| 1119 | gpiod_set_value_cansleep(tsdata->wake_gpio, 1); |
| 1120 | } |
| 1121 | |
| 1122 | if (tsdata->reset_gpio) { |
| 1123 | usleep_range(5000, 6000); |
| 1124 | gpiod_set_value_cansleep(tsdata->reset_gpio, 0); |
| 1125 | msleep(300); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1128 | input = devm_input_allocate_device(&client->dev); |
| 1129 | if (!input) { |
| 1130 | dev_err(&client->dev, "failed to allocate input device.\n"); |
| 1131 | return -ENOMEM; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | mutex_init(&tsdata->mutex); |
| 1135 | tsdata->client = client; |
| 1136 | tsdata->input = input; |
| 1137 | tsdata->factory_mode = false; |
| 1138 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 1139 | error = edt_ft5x06_ts_identify(client, tsdata, fw_version); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1140 | if (error) { |
| 1141 | dev_err(&client->dev, "touchscreen probe failed\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1142 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Philipp Zabel | e112324 | 2020-01-09 17:03:21 -0800 | [diff] [blame] | 1145 | /* |
| 1146 | * Dummy read access. EP0700MLP1 returns bogus data on the first |
| 1147 | * register read access and ignores writes. |
| 1148 | */ |
| 1149 | edt_ft5x06_ts_readwrite(tsdata->client, 2, buf, 2, buf); |
| 1150 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 1151 | edt_ft5x06_ts_set_regs(tsdata); |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame] | 1152 | edt_ft5x06_ts_get_defaults(&client->dev, tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1153 | edt_ft5x06_ts_get_parameters(tsdata); |
| 1154 | |
| 1155 | dev_dbg(&client->dev, |
| 1156 | "Model \"%s\", Rev. \"%s\", %dx%d sensors\n", |
| 1157 | tsdata->name, fw_version, tsdata->num_x, tsdata->num_y); |
| 1158 | |
| 1159 | input->name = tsdata->name; |
| 1160 | input->id.bustype = BUS_I2C; |
| 1161 | input->dev.parent = &client->dev; |
| 1162 | |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 1163 | if (tsdata->version == EDT_M06 || |
Simon Budig | aed5d0e | 2017-10-09 21:00:16 -0700 | [diff] [blame] | 1164 | tsdata->version == EDT_M09 || |
| 1165 | tsdata->version == EDT_M12) { |
Simon Budig | 169110c | 2017-10-09 20:58:11 -0700 | [diff] [blame] | 1166 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 1167 | 0, tsdata->num_x * 64 - 1, 0, 0); |
| 1168 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 1169 | 0, tsdata->num_y * 64 - 1, 0, 0); |
| 1170 | } else { |
| 1171 | /* Unknown maximum values. Specify via devicetree */ |
| 1172 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 1173 | 0, 65535, 0, 0); |
| 1174 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 1175 | 0, 65535, 0, 0); |
| 1176 | } |
Maxime Ripard | 2c00559 | 2015-03-21 20:18:06 -0700 | [diff] [blame] | 1177 | |
Hans de Goede | ad368eb | 2016-07-15 14:26:53 -0700 | [diff] [blame] | 1178 | touchscreen_parse_properties(input, true, &tsdata->prop); |
Maxime Ripard | 2c00559 | 2015-03-21 20:18:06 -0700 | [diff] [blame] | 1179 | |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 1180 | error = input_mt_init_slots(input, tsdata->max_support_points, |
| 1181 | INPUT_MT_DIRECT); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1182 | if (error) { |
| 1183 | dev_err(&client->dev, "Unable to init MT slots.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1184 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1187 | i2c_set_clientdata(client, tsdata); |
| 1188 | |
Dmitry Torokhov | f0bef75 | 2015-09-12 10:11:09 -0700 | [diff] [blame] | 1189 | irq_flags = irq_get_trigger_type(client->irq); |
| 1190 | if (irq_flags == IRQF_TRIGGER_NONE) |
| 1191 | irq_flags = IRQF_TRIGGER_FALLING; |
| 1192 | irq_flags |= IRQF_ONESHOT; |
| 1193 | |
| 1194 | error = devm_request_threaded_irq(&client->dev, client->irq, |
| 1195 | NULL, edt_ft5x06_ts_isr, irq_flags, |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1196 | client->name, tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1197 | if (error) { |
| 1198 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1199 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Andi Shyti | e3adf55 | 2017-09-29 16:42:06 -0700 | [diff] [blame] | 1202 | error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1203 | if (error) |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1204 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1205 | |
| 1206 | error = input_register_device(input); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1207 | if (error) |
Andi Shyti | e3adf55 | 2017-09-29 16:42:06 -0700 | [diff] [blame] | 1208 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1209 | |
| 1210 | edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev)); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1211 | |
| 1212 | dev_dbg(&client->dev, |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1213 | "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n", |
Franklin S Cooper Jr | 8b58cc3 | 2015-10-06 15:24:34 -0700 | [diff] [blame] | 1214 | client->irq, |
| 1215 | tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1, |
| 1216 | tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | return 0; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 1221 | static int edt_ft5x06_ts_remove(struct i2c_client *client) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1222 | { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1223 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 1224 | |
| 1225 | edt_ft5x06_ts_teardown_debugfs(tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1226 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1227 | return 0; |
| 1228 | } |
| 1229 | |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 1230 | static const struct edt_i2c_chip_data edt_ft5x06_data = { |
| 1231 | .max_support_points = 5, |
| 1232 | }; |
| 1233 | |
Franklin S Cooper Jr | af33e0a | 2015-10-16 15:34:26 -0700 | [diff] [blame] | 1234 | static const struct edt_i2c_chip_data edt_ft5506_data = { |
| 1235 | .max_support_points = 10, |
| 1236 | }; |
| 1237 | |
Hans de Goede | d187165 | 2016-08-04 08:21:19 -0700 | [diff] [blame] | 1238 | static const struct edt_i2c_chip_data edt_ft6236_data = { |
| 1239 | .max_support_points = 2, |
| 1240 | }; |
| 1241 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1242 | static const struct i2c_device_id edt_ft5x06_ts_id[] = { |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 1243 | { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data }, |
Franklin S Cooper Jr | af33e0a | 2015-10-16 15:34:26 -0700 | [diff] [blame] | 1244 | { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data }, |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 1245 | { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data }, |
Hans de Goede | d187165 | 2016-08-04 08:21:19 -0700 | [diff] [blame] | 1246 | /* Note no edt- prefix for compatibility with the ft6236.c driver */ |
| 1247 | { .name = "ft6236", .driver_data = (long)&edt_ft6236_data }, |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 1248 | { /* sentinel */ } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1249 | }; |
| 1250 | MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id); |
| 1251 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1252 | static const struct of_device_id edt_ft5x06_of_match[] = { |
Franklin S Cooper Jr | b1d2a3e | 2015-10-16 15:34:16 -0700 | [diff] [blame] | 1253 | { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data }, |
| 1254 | { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data }, |
| 1255 | { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data }, |
Franklin S Cooper Jr | af33e0a | 2015-10-16 15:34:26 -0700 | [diff] [blame] | 1256 | { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data }, |
Marco Felsch | a2f39da | 2019-01-13 23:08:32 -0800 | [diff] [blame] | 1257 | { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data }, |
Hans de Goede | d187165 | 2016-08-04 08:21:19 -0700 | [diff] [blame] | 1258 | /* Note focaltech vendor prefix for compatibility with ft6236.c */ |
| 1259 | { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data }, |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1260 | { /* sentinel */ } |
| 1261 | }; |
| 1262 | MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1263 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1264 | static struct i2c_driver edt_ft5x06_ts_driver = { |
| 1265 | .driver = { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1266 | .name = "edt_ft5x06", |
Andy Shevchenko | fc226eb | 2019-03-03 23:21:28 -0800 | [diff] [blame] | 1267 | .of_match_table = edt_ft5x06_of_match, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1268 | }, |
| 1269 | .id_table = edt_ft5x06_ts_id, |
| 1270 | .probe = edt_ft5x06_ts_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 1271 | .remove = edt_ft5x06_ts_remove, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1272 | }; |
| 1273 | |
| 1274 | module_i2c_driver(edt_ft5x06_ts_driver); |
| 1275 | |
| 1276 | MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>"); |
| 1277 | MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver"); |
Andy Shevchenko | 6d3a41ab | 2019-03-03 23:21:56 -0800 | [diff] [blame] | 1278 | MODULE_LICENSE("GPL v2"); |