Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de> |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 3 | * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support) |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 4 | * Lothar Waßmann <LW@KARO-electronics.de> (DT support) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * This is a driver for the EDT "Polytouch" family of touch controllers |
| 22 | * based on the FocalTech FT5x06 line of chips. |
| 23 | * |
| 24 | * Development of this driver has been sponsored by Glyn: |
| 25 | * http://www.glyn.com/Products/Displays |
| 26 | */ |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/ratelimit.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/input.h> |
| 32 | #include <linux/i2c.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/debugfs.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/gpio.h> |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 38 | #include <linux/of_gpio.h> |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 39 | #include <linux/input/mt.h> |
| 40 | #include <linux/input/edt-ft5x06.h> |
| 41 | |
| 42 | #define MAX_SUPPORT_POINTS 5 |
| 43 | |
| 44 | #define WORK_REGISTER_THRESHOLD 0x00 |
| 45 | #define WORK_REGISTER_REPORT_RATE 0x08 |
| 46 | #define WORK_REGISTER_GAIN 0x30 |
| 47 | #define WORK_REGISTER_OFFSET 0x31 |
| 48 | #define WORK_REGISTER_NUM_X 0x33 |
| 49 | #define WORK_REGISTER_NUM_Y 0x34 |
| 50 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 51 | #define M09_REGISTER_THRESHOLD 0x80 |
| 52 | #define M09_REGISTER_GAIN 0x92 |
| 53 | #define M09_REGISTER_OFFSET 0x93 |
| 54 | #define M09_REGISTER_NUM_X 0x94 |
| 55 | #define M09_REGISTER_NUM_Y 0x95 |
| 56 | |
| 57 | #define NO_REGISTER 0xff |
| 58 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 59 | #define WORK_REGISTER_OPMODE 0x3c |
| 60 | #define FACTORY_REGISTER_OPMODE 0x01 |
| 61 | |
| 62 | #define TOUCH_EVENT_DOWN 0x00 |
| 63 | #define TOUCH_EVENT_UP 0x01 |
| 64 | #define TOUCH_EVENT_ON 0x02 |
| 65 | #define TOUCH_EVENT_RESERVED 0x03 |
| 66 | |
| 67 | #define EDT_NAME_LEN 23 |
| 68 | #define EDT_SWITCH_MODE_RETRIES 10 |
| 69 | #define EDT_SWITCH_MODE_DELAY 5 /* msec */ |
| 70 | #define EDT_RAW_DATA_RETRIES 100 |
| 71 | #define EDT_RAW_DATA_DELAY 1 /* msec */ |
| 72 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 73 | enum edt_ver { |
| 74 | M06, |
| 75 | M09, |
| 76 | }; |
| 77 | |
| 78 | struct edt_reg_addr { |
| 79 | int reg_threshold; |
| 80 | int reg_report_rate; |
| 81 | int reg_gain; |
| 82 | int reg_offset; |
| 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; |
| 90 | u16 num_x; |
| 91 | u16 num_y; |
| 92 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 93 | int reset_pin; |
| 94 | int irq_pin; |
| 95 | int wake_pin; |
| 96 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 97 | #if defined(CONFIG_DEBUG_FS) |
| 98 | struct dentry *debug_dir; |
| 99 | u8 *raw_buffer; |
| 100 | size_t raw_bufsize; |
| 101 | #endif |
| 102 | |
| 103 | struct mutex mutex; |
| 104 | bool factory_mode; |
| 105 | int threshold; |
| 106 | int gain; |
| 107 | int offset; |
| 108 | int report_rate; |
| 109 | |
| 110 | char name[EDT_NAME_LEN]; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 111 | |
| 112 | struct edt_reg_addr reg_addr; |
| 113 | enum edt_ver version; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static int edt_ft5x06_ts_readwrite(struct i2c_client *client, |
| 117 | u16 wr_len, u8 *wr_buf, |
| 118 | u16 rd_len, u8 *rd_buf) |
| 119 | { |
| 120 | struct i2c_msg wrmsg[2]; |
| 121 | int i = 0; |
| 122 | int ret; |
| 123 | |
| 124 | if (wr_len) { |
| 125 | wrmsg[i].addr = client->addr; |
| 126 | wrmsg[i].flags = 0; |
| 127 | wrmsg[i].len = wr_len; |
| 128 | wrmsg[i].buf = wr_buf; |
| 129 | i++; |
| 130 | } |
| 131 | if (rd_len) { |
| 132 | wrmsg[i].addr = client->addr; |
| 133 | wrmsg[i].flags = I2C_M_RD; |
| 134 | wrmsg[i].len = rd_len; |
| 135 | wrmsg[i].buf = rd_buf; |
| 136 | i++; |
| 137 | } |
| 138 | |
| 139 | ret = i2c_transfer(client->adapter, wrmsg, i); |
| 140 | if (ret < 0) |
| 141 | return ret; |
| 142 | if (ret != i) |
| 143 | return -EIO; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata, |
| 149 | u8 *buf, int buflen) |
| 150 | { |
| 151 | int i; |
| 152 | u8 crc = 0; |
| 153 | |
| 154 | for (i = 0; i < buflen - 1; i++) |
| 155 | crc ^= buf[i]; |
| 156 | |
| 157 | if (crc != buf[buflen-1]) { |
| 158 | dev_err_ratelimited(&tsdata->client->dev, |
| 159 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 160 | crc, buf[buflen-1]); |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id) |
| 168 | { |
| 169 | struct edt_ft5x06_ts_data *tsdata = dev_id; |
| 170 | struct device *dev = &tsdata->client->dev; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 171 | u8 cmd; |
| 172 | u8 rdbuf[29]; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 173 | int i, type, x, y, id; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 174 | int offset, tplen, datalen; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 175 | int error; |
| 176 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 177 | switch (tsdata->version) { |
| 178 | case M06: |
| 179 | cmd = 0xf9; /* tell the controller to send touch data */ |
| 180 | offset = 5; /* where the actual touch data starts */ |
| 181 | tplen = 4; /* data comes in so called frames */ |
| 182 | datalen = 26; /* how much bytes to listen for */ |
| 183 | break; |
| 184 | |
| 185 | case M09: |
| 186 | cmd = 0x02; |
| 187 | offset = 1; |
| 188 | tplen = 6; |
| 189 | datalen = 29; |
| 190 | break; |
| 191 | |
| 192 | default: |
| 193 | goto out; |
| 194 | } |
| 195 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 196 | memset(rdbuf, 0, sizeof(rdbuf)); |
| 197 | |
| 198 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 199 | sizeof(cmd), &cmd, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 200 | datalen, rdbuf); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 201 | if (error) { |
| 202 | dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n", |
| 203 | error); |
| 204 | goto out; |
| 205 | } |
| 206 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 207 | /* M09 does not send header or CRC */ |
| 208 | if (tsdata->version == M06) { |
| 209 | if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa || |
| 210 | rdbuf[2] != datalen) { |
| 211 | dev_err_ratelimited(dev, |
| 212 | "Unexpected header: %02x%02x%02x!\n", |
| 213 | rdbuf[0], rdbuf[1], rdbuf[2]); |
| 214 | goto out; |
| 215 | } |
| 216 | |
| 217 | if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen)) |
| 218 | goto out; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 221 | for (i = 0; i < MAX_SUPPORT_POINTS; i++) { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 222 | u8 *buf = &rdbuf[i * tplen + offset]; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 223 | bool down; |
| 224 | |
| 225 | type = buf[0] >> 6; |
| 226 | /* ignore Reserved events */ |
| 227 | if (type == TOUCH_EVENT_RESERVED) |
| 228 | continue; |
| 229 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 230 | /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */ |
| 231 | if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN) |
Lothar Waßmann | ee3e946 | 2014-03-28 09:28:17 -0700 | [diff] [blame] | 232 | continue; |
| 233 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 234 | x = ((buf[0] << 8) | buf[1]) & 0x0fff; |
| 235 | y = ((buf[2] << 8) | buf[3]) & 0x0fff; |
| 236 | id = (buf[2] >> 4) & 0x0f; |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 237 | down = type != TOUCH_EVENT_UP; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 238 | |
| 239 | input_mt_slot(tsdata->input, id); |
| 240 | input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down); |
| 241 | |
| 242 | if (!down) |
| 243 | continue; |
| 244 | |
| 245 | input_report_abs(tsdata->input, ABS_MT_POSITION_X, x); |
| 246 | input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y); |
| 247 | } |
| 248 | |
| 249 | input_mt_report_pointer_emulation(tsdata->input, true); |
| 250 | input_sync(tsdata->input); |
| 251 | |
| 252 | out: |
| 253 | return IRQ_HANDLED; |
| 254 | } |
| 255 | |
| 256 | static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata, |
| 257 | u8 addr, u8 value) |
| 258 | { |
| 259 | u8 wrbuf[4]; |
| 260 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 261 | switch (tsdata->version) { |
| 262 | case M06: |
| 263 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 264 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
| 265 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
| 266 | wrbuf[2] = value; |
| 267 | wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2]; |
| 268 | return edt_ft5x06_ts_readwrite(tsdata->client, 4, |
| 269 | wrbuf, 0, NULL); |
| 270 | case M09: |
| 271 | wrbuf[0] = addr; |
| 272 | wrbuf[1] = value; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 273 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 274 | return edt_ft5x06_ts_readwrite(tsdata->client, 3, |
| 275 | wrbuf, 0, NULL); |
| 276 | |
| 277 | default: |
| 278 | return -EINVAL; |
| 279 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata, |
| 283 | u8 addr) |
| 284 | { |
| 285 | u8 wrbuf[2], rdbuf[2]; |
| 286 | int error; |
| 287 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 288 | switch (tsdata->version) { |
| 289 | case M06: |
| 290 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 291 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
| 292 | wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 293 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 294 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 295 | 2, wrbuf, 2, rdbuf); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 296 | return error; |
| 297 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 298 | if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) { |
| 299 | dev_err(&tsdata->client->dev, |
| 300 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 301 | wrbuf[0] ^ wrbuf[1] ^ rdbuf[0], |
| 302 | rdbuf[1]); |
| 303 | return -EIO; |
| 304 | } |
| 305 | break; |
| 306 | |
| 307 | case M09: |
| 308 | wrbuf[0] = addr; |
| 309 | error = edt_ft5x06_ts_readwrite(tsdata->client, 1, |
| 310 | wrbuf, 1, rdbuf); |
| 311 | if (error) |
| 312 | return error; |
| 313 | break; |
| 314 | |
| 315 | default: |
| 316 | return -EINVAL; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | return rdbuf[0]; |
| 320 | } |
| 321 | |
| 322 | struct edt_ft5x06_attribute { |
| 323 | struct device_attribute dattr; |
| 324 | size_t field_offset; |
| 325 | u8 limit_low; |
| 326 | u8 limit_high; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 327 | u8 addr_m06; |
| 328 | u8 addr_m09; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 331 | #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \ |
| 332 | _limit_low, _limit_high) \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 333 | struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \ |
| 334 | .dattr = __ATTR(_field, _mode, \ |
| 335 | edt_ft5x06_setting_show, \ |
| 336 | edt_ft5x06_setting_store), \ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 337 | .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \ |
| 338 | .addr_m06 = _addr_m06, \ |
| 339 | .addr_m09 = _addr_m09, \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 340 | .limit_low = _limit_low, \ |
| 341 | .limit_high = _limit_high, \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static ssize_t edt_ft5x06_setting_show(struct device *dev, |
| 345 | struct device_attribute *dattr, |
| 346 | char *buf) |
| 347 | { |
| 348 | struct i2c_client *client = to_i2c_client(dev); |
| 349 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 350 | struct edt_ft5x06_attribute *attr = |
| 351 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 352 | u8 *field = (u8 *)tsdata + attr->field_offset; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 353 | int val; |
| 354 | size_t count = 0; |
| 355 | int error = 0; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 356 | u8 addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 357 | |
| 358 | mutex_lock(&tsdata->mutex); |
| 359 | |
| 360 | if (tsdata->factory_mode) { |
| 361 | error = -EIO; |
| 362 | goto out; |
| 363 | } |
| 364 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 365 | switch (tsdata->version) { |
| 366 | case M06: |
| 367 | addr = attr->addr_m06; |
| 368 | break; |
| 369 | |
| 370 | case M09: |
| 371 | addr = attr->addr_m09; |
| 372 | break; |
| 373 | |
| 374 | default: |
| 375 | error = -ENODEV; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 376 | goto out; |
| 377 | } |
| 378 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 379 | if (addr != NO_REGISTER) { |
| 380 | val = edt_ft5x06_register_read(tsdata, addr); |
| 381 | if (val < 0) { |
| 382 | error = val; |
| 383 | dev_err(&tsdata->client->dev, |
| 384 | "Failed to fetch attribute %s, error %d\n", |
| 385 | dattr->attr.name, error); |
| 386 | goto out; |
| 387 | } |
| 388 | } else { |
| 389 | val = *field; |
| 390 | } |
| 391 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 392 | if (val != *field) { |
| 393 | dev_warn(&tsdata->client->dev, |
| 394 | "%s: read (%d) and stored value (%d) differ\n", |
| 395 | dattr->attr.name, val, *field); |
| 396 | *field = val; |
| 397 | } |
| 398 | |
| 399 | count = scnprintf(buf, PAGE_SIZE, "%d\n", val); |
| 400 | out: |
| 401 | mutex_unlock(&tsdata->mutex); |
| 402 | return error ?: count; |
| 403 | } |
| 404 | |
| 405 | static ssize_t edt_ft5x06_setting_store(struct device *dev, |
| 406 | struct device_attribute *dattr, |
| 407 | const char *buf, size_t count) |
| 408 | { |
| 409 | struct i2c_client *client = to_i2c_client(dev); |
| 410 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 411 | struct edt_ft5x06_attribute *attr = |
| 412 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 413 | u8 *field = (u8 *)tsdata + attr->field_offset; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 414 | unsigned int val; |
| 415 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 416 | u8 addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 417 | |
| 418 | mutex_lock(&tsdata->mutex); |
| 419 | |
| 420 | if (tsdata->factory_mode) { |
| 421 | error = -EIO; |
| 422 | goto out; |
| 423 | } |
| 424 | |
| 425 | error = kstrtouint(buf, 0, &val); |
| 426 | if (error) |
| 427 | goto out; |
| 428 | |
| 429 | if (val < attr->limit_low || val > attr->limit_high) { |
| 430 | error = -ERANGE; |
| 431 | goto out; |
| 432 | } |
| 433 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 434 | switch (tsdata->version) { |
| 435 | case M06: |
| 436 | addr = attr->addr_m06; |
| 437 | break; |
| 438 | |
| 439 | case M09: |
| 440 | addr = attr->addr_m09; |
| 441 | break; |
| 442 | |
| 443 | default: |
| 444 | error = -ENODEV; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 445 | goto out; |
| 446 | } |
| 447 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 448 | if (addr != NO_REGISTER) { |
| 449 | error = edt_ft5x06_register_write(tsdata, addr, val); |
| 450 | if (error) { |
| 451 | dev_err(&tsdata->client->dev, |
| 452 | "Failed to update attribute %s, error: %d\n", |
| 453 | dattr->attr.name, error); |
| 454 | goto out; |
| 455 | } |
| 456 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 457 | *field = val; |
| 458 | |
| 459 | out: |
| 460 | mutex_unlock(&tsdata->mutex); |
| 461 | return error ?: count; |
| 462 | } |
| 463 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 464 | static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN, |
| 465 | M09_REGISTER_GAIN, 0, 31); |
| 466 | static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET, |
| 467 | M09_REGISTER_OFFSET, 0, 31); |
| 468 | static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD, |
| 469 | M09_REGISTER_THRESHOLD, 20, 80); |
| 470 | static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE, |
| 471 | NO_REGISTER, 3, 14); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 472 | |
| 473 | static struct attribute *edt_ft5x06_attrs[] = { |
| 474 | &edt_ft5x06_attr_gain.dattr.attr, |
| 475 | &edt_ft5x06_attr_offset.dattr.attr, |
| 476 | &edt_ft5x06_attr_threshold.dattr.attr, |
| 477 | &edt_ft5x06_attr_report_rate.dattr.attr, |
| 478 | NULL |
| 479 | }; |
| 480 | |
| 481 | static const struct attribute_group edt_ft5x06_attr_group = { |
| 482 | .attrs = edt_ft5x06_attrs, |
| 483 | }; |
| 484 | |
| 485 | #ifdef CONFIG_DEBUG_FS |
| 486 | static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata) |
| 487 | { |
| 488 | struct i2c_client *client = tsdata->client; |
| 489 | int retries = EDT_SWITCH_MODE_RETRIES; |
| 490 | int ret; |
| 491 | int error; |
| 492 | |
| 493 | disable_irq(client->irq); |
| 494 | |
| 495 | if (!tsdata->raw_buffer) { |
| 496 | tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y * |
| 497 | sizeof(u16); |
| 498 | tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL); |
| 499 | if (!tsdata->raw_buffer) { |
| 500 | error = -ENOMEM; |
| 501 | goto err_out; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* mode register is 0x3c when in the work mode */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 506 | if (tsdata->version == M09) |
| 507 | goto m09_out; |
| 508 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 509 | error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03); |
| 510 | if (error) { |
| 511 | dev_err(&client->dev, |
| 512 | "failed to switch to factory mode, error %d\n", error); |
| 513 | goto err_out; |
| 514 | } |
| 515 | |
| 516 | tsdata->factory_mode = true; |
| 517 | do { |
| 518 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 519 | /* mode register is 0x01 when in factory mode */ |
| 520 | ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE); |
| 521 | if (ret == 0x03) |
| 522 | break; |
| 523 | } while (--retries > 0); |
| 524 | |
| 525 | if (retries == 0) { |
| 526 | dev_err(&client->dev, "not in factory mode after %dms.\n", |
| 527 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 528 | error = -EIO; |
| 529 | goto err_out; |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | |
| 534 | err_out: |
| 535 | kfree(tsdata->raw_buffer); |
| 536 | tsdata->raw_buffer = NULL; |
| 537 | tsdata->factory_mode = false; |
| 538 | enable_irq(client->irq); |
| 539 | |
| 540 | return error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 541 | |
| 542 | m09_out: |
| 543 | dev_err(&client->dev, "No factory mode support for M09\n"); |
| 544 | return -EINVAL; |
| 545 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata) |
| 549 | { |
| 550 | struct i2c_client *client = tsdata->client; |
| 551 | int retries = EDT_SWITCH_MODE_RETRIES; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 552 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 553 | int ret; |
| 554 | int error; |
| 555 | |
| 556 | /* mode register is 0x01 when in the factory mode */ |
| 557 | error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1); |
| 558 | if (error) { |
| 559 | dev_err(&client->dev, |
| 560 | "failed to switch to work mode, error: %d\n", error); |
| 561 | return error; |
| 562 | } |
| 563 | |
| 564 | tsdata->factory_mode = false; |
| 565 | |
| 566 | do { |
| 567 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 568 | /* mode register is 0x01 when in factory mode */ |
| 569 | ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE); |
| 570 | if (ret == 0x01) |
| 571 | break; |
| 572 | } while (--retries > 0); |
| 573 | |
| 574 | if (retries == 0) { |
| 575 | dev_err(&client->dev, "not in work mode after %dms.\n", |
| 576 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 577 | tsdata->factory_mode = true; |
| 578 | return -EIO; |
| 579 | } |
| 580 | |
Sachin Kamat | 8efcc50 | 2013-03-28 01:14:42 -0700 | [diff] [blame] | 581 | kfree(tsdata->raw_buffer); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 582 | tsdata->raw_buffer = NULL; |
| 583 | |
| 584 | /* restore parameters */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 585 | edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 586 | tsdata->threshold); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 587 | edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 588 | tsdata->gain); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 589 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 590 | tsdata->offset); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 591 | if (reg_addr->reg_report_rate) |
| 592 | edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 593 | tsdata->report_rate); |
| 594 | |
| 595 | enable_irq(client->irq); |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode) |
| 601 | { |
| 602 | struct edt_ft5x06_ts_data *tsdata = data; |
| 603 | |
| 604 | *mode = tsdata->factory_mode; |
| 605 | |
| 606 | return 0; |
| 607 | }; |
| 608 | |
| 609 | static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode) |
| 610 | { |
| 611 | struct edt_ft5x06_ts_data *tsdata = data; |
| 612 | int retval = 0; |
| 613 | |
| 614 | if (mode > 1) |
| 615 | return -ERANGE; |
| 616 | |
| 617 | mutex_lock(&tsdata->mutex); |
| 618 | |
| 619 | if (mode != tsdata->factory_mode) { |
| 620 | retval = mode ? edt_ft5x06_factory_mode(tsdata) : |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 621 | edt_ft5x06_work_mode(tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | mutex_unlock(&tsdata->mutex); |
| 625 | |
| 626 | return retval; |
| 627 | }; |
| 628 | |
| 629 | DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get, |
| 630 | edt_ft5x06_debugfs_mode_set, "%llu\n"); |
| 631 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 632 | static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file, |
| 633 | char __user *buf, size_t count, loff_t *off) |
| 634 | { |
| 635 | struct edt_ft5x06_ts_data *tsdata = file->private_data; |
| 636 | struct i2c_client *client = tsdata->client; |
| 637 | int retries = EDT_RAW_DATA_RETRIES; |
| 638 | int val, i, error; |
| 639 | size_t read = 0; |
| 640 | int colbytes; |
| 641 | char wrbuf[3]; |
| 642 | u8 *rdbuf; |
| 643 | |
| 644 | if (*off < 0 || *off >= tsdata->raw_bufsize) |
| 645 | return 0; |
| 646 | |
| 647 | mutex_lock(&tsdata->mutex); |
| 648 | |
| 649 | if (!tsdata->factory_mode || !tsdata->raw_buffer) { |
| 650 | error = -EIO; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | error = edt_ft5x06_register_write(tsdata, 0x08, 0x01); |
| 655 | if (error) { |
| 656 | dev_dbg(&client->dev, |
| 657 | "failed to write 0x08 register, error %d\n", error); |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | do { |
| 662 | msleep(EDT_RAW_DATA_DELAY); |
| 663 | val = edt_ft5x06_register_read(tsdata, 0x08); |
| 664 | if (val < 1) |
| 665 | break; |
| 666 | } while (--retries > 0); |
| 667 | |
| 668 | if (val < 0) { |
| 669 | error = val; |
| 670 | dev_dbg(&client->dev, |
| 671 | "failed to read 0x08 register, error %d\n", error); |
| 672 | goto out; |
| 673 | } |
| 674 | |
| 675 | if (retries == 0) { |
| 676 | dev_dbg(&client->dev, |
| 677 | "timed out waiting for register to settle\n"); |
| 678 | error = -ETIMEDOUT; |
| 679 | goto out; |
| 680 | } |
| 681 | |
| 682 | rdbuf = tsdata->raw_buffer; |
| 683 | colbytes = tsdata->num_y * sizeof(u16); |
| 684 | |
| 685 | wrbuf[0] = 0xf5; |
| 686 | wrbuf[1] = 0x0e; |
| 687 | for (i = 0; i < tsdata->num_x; i++) { |
| 688 | wrbuf[2] = i; /* column index */ |
| 689 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 690 | sizeof(wrbuf), wrbuf, |
| 691 | colbytes, rdbuf); |
| 692 | if (error) |
| 693 | goto out; |
| 694 | |
| 695 | rdbuf += colbytes; |
| 696 | } |
| 697 | |
| 698 | read = min_t(size_t, count, tsdata->raw_bufsize - *off); |
Axel Lin | 35b1da4 | 2012-09-19 15:56:23 -0700 | [diff] [blame] | 699 | if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) { |
| 700 | error = -EFAULT; |
| 701 | goto out; |
| 702 | } |
| 703 | |
| 704 | *off += read; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 705 | out: |
| 706 | mutex_unlock(&tsdata->mutex); |
| 707 | return error ?: read; |
| 708 | }; |
| 709 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 710 | static const struct file_operations debugfs_raw_data_fops = { |
Wei Yongjun | f6c0df6 | 2012-10-17 23:55:54 -0700 | [diff] [blame] | 711 | .open = simple_open, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 712 | .read = edt_ft5x06_debugfs_raw_data_read, |
| 713 | }; |
| 714 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 715 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 716 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 717 | const char *debugfs_name) |
| 718 | { |
| 719 | tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL); |
| 720 | if (!tsdata->debug_dir) |
| 721 | return; |
| 722 | |
| 723 | debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x); |
| 724 | debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y); |
| 725 | |
| 726 | debugfs_create_file("mode", S_IRUSR | S_IWUSR, |
| 727 | tsdata->debug_dir, tsdata, &debugfs_mode_fops); |
| 728 | debugfs_create_file("raw_data", S_IRUSR, |
| 729 | tsdata->debug_dir, tsdata, &debugfs_raw_data_fops); |
| 730 | } |
| 731 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 732 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 733 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 734 | { |
| 735 | if (tsdata->debug_dir) |
| 736 | debugfs_remove_recursive(tsdata->debug_dir); |
Guenter Roeck | a1d0fa7 | 2012-08-21 22:01:45 -0700 | [diff] [blame] | 737 | kfree(tsdata->raw_buffer); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | #else |
| 741 | |
| 742 | static inline void |
| 743 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 744 | const char *debugfs_name) |
| 745 | { |
| 746 | } |
| 747 | |
| 748 | static inline void |
| 749 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 750 | { |
| 751 | } |
| 752 | |
| 753 | #endif /* CONFIG_DEBUGFS */ |
| 754 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 755 | static int edt_ft5x06_ts_reset(struct i2c_client *client, |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 756 | struct edt_ft5x06_ts_data *tsdata) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 757 | { |
| 758 | int error; |
| 759 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 760 | if (gpio_is_valid(tsdata->wake_pin)) { |
| 761 | error = devm_gpio_request_one(&client->dev, |
| 762 | tsdata->wake_pin, GPIOF_OUT_INIT_LOW, |
| 763 | "edt-ft5x06 wake"); |
| 764 | if (error) { |
| 765 | dev_err(&client->dev, |
| 766 | "Failed to request GPIO %d as wake pin, error %d\n", |
| 767 | tsdata->wake_pin, error); |
| 768 | return error; |
| 769 | } |
| 770 | |
Lothar Waßmann | c080846 | 2014-03-28 09:27:50 -0700 | [diff] [blame] | 771 | msleep(5); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 772 | gpio_set_value(tsdata->wake_pin, 1); |
| 773 | } |
| 774 | if (gpio_is_valid(tsdata->reset_pin)) { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 775 | /* this pulls reset down, enabling the low active reset */ |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 776 | error = devm_gpio_request_one(&client->dev, |
| 777 | tsdata->reset_pin, GPIOF_OUT_INIT_LOW, |
| 778 | "edt-ft5x06 reset"); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 779 | if (error) { |
| 780 | dev_err(&client->dev, |
| 781 | "Failed to request GPIO %d as reset pin, error %d\n", |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 782 | tsdata->reset_pin, error); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 783 | return error; |
| 784 | } |
| 785 | |
Lothar Waßmann | c080846 | 2014-03-28 09:27:50 -0700 | [diff] [blame] | 786 | msleep(5); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 787 | gpio_set_value(tsdata->reset_pin, 1); |
Lothar Waßmann | c080846 | 2014-03-28 09:27:50 -0700 | [diff] [blame] | 788 | msleep(300); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 794 | static int edt_ft5x06_ts_identify(struct i2c_client *client, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 795 | struct edt_ft5x06_ts_data *tsdata, |
| 796 | char *fw_version) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 797 | { |
| 798 | u8 rdbuf[EDT_NAME_LEN]; |
| 799 | char *p; |
| 800 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 801 | char *model_name = tsdata->name; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 802 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 803 | /* see what we find if we assume it is a M06 * |
| 804 | * if we get less than EDT_NAME_LEN, we don't want |
| 805 | * to have garbage in there |
| 806 | */ |
| 807 | memset(rdbuf, 0, sizeof(rdbuf)); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 808 | error = edt_ft5x06_ts_readwrite(client, 1, "\xbb", |
| 809 | EDT_NAME_LEN - 1, rdbuf); |
| 810 | if (error) |
| 811 | return error; |
| 812 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 813 | /* if we find something consistent, stay with that assumption |
| 814 | * at least M09 won't send 3 bytes here |
| 815 | */ |
| 816 | if (!(strnicmp(rdbuf + 1, "EP0", 3))) { |
| 817 | tsdata->version = M06; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 818 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 819 | /* remove last '$' end marker */ |
| 820 | rdbuf[EDT_NAME_LEN - 1] = '\0'; |
| 821 | if (rdbuf[EDT_NAME_LEN - 2] == '$') |
| 822 | rdbuf[EDT_NAME_LEN - 2] = '\0'; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 823 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 824 | /* look for Model/Version separator */ |
| 825 | p = strchr(rdbuf, '*'); |
| 826 | if (p) |
| 827 | *p++ = '\0'; |
| 828 | strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN); |
| 829 | strlcpy(fw_version, p ? p : "", EDT_NAME_LEN); |
| 830 | } else { |
| 831 | /* since there are only two versions around (M06, M09) */ |
| 832 | tsdata->version = M09; |
| 833 | |
| 834 | error = edt_ft5x06_ts_readwrite(client, 1, "\xA6", |
| 835 | 2, rdbuf); |
| 836 | if (error) |
| 837 | return error; |
| 838 | |
| 839 | strlcpy(fw_version, rdbuf, 2); |
| 840 | |
| 841 | error = edt_ft5x06_ts_readwrite(client, 1, "\xA8", |
| 842 | 1, rdbuf); |
| 843 | if (error) |
| 844 | return error; |
| 845 | |
| 846 | snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09", |
| 847 | rdbuf[0] >> 4, rdbuf[0] & 0x0F); |
| 848 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | #define EDT_ATTR_CHECKSET(name, reg) \ |
| 854 | if (pdata->name >= edt_ft5x06_attr_##name.limit_low && \ |
| 855 | pdata->name <= edt_ft5x06_attr_##name.limit_high) \ |
| 856 | edt_ft5x06_register_write(tsdata, reg, pdata->name) |
| 857 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 858 | #define EDT_GET_PROP(name, reg) { \ |
| 859 | u32 val; \ |
| 860 | if (of_property_read_u32(np, #name, &val) == 0) \ |
| 861 | edt_ft5x06_register_write(tsdata, reg, val); \ |
| 862 | } |
| 863 | |
| 864 | static void edt_ft5x06_ts_get_dt_defaults(struct device_node *np, |
| 865 | struct edt_ft5x06_ts_data *tsdata) |
| 866 | { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 867 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 868 | |
| 869 | EDT_GET_PROP(threshold, reg_addr->reg_threshold); |
| 870 | EDT_GET_PROP(gain, reg_addr->reg_gain); |
| 871 | EDT_GET_PROP(offset, reg_addr->reg_offset); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 874 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 875 | edt_ft5x06_ts_get_defaults(struct edt_ft5x06_ts_data *tsdata, |
| 876 | const struct edt_ft5x06_platform_data *pdata) |
| 877 | { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 878 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 879 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 880 | if (!pdata->use_parameters) |
| 881 | return; |
| 882 | |
| 883 | /* pick up defaults from the platform data */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 884 | EDT_ATTR_CHECKSET(threshold, reg_addr->reg_threshold); |
| 885 | EDT_ATTR_CHECKSET(gain, reg_addr->reg_gain); |
| 886 | EDT_ATTR_CHECKSET(offset, reg_addr->reg_offset); |
| 887 | if (reg_addr->reg_report_rate != NO_REGISTER) |
| 888 | EDT_ATTR_CHECKSET(report_rate, reg_addr->reg_report_rate); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 891 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 892 | edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata) |
| 893 | { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 894 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 895 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 896 | tsdata->threshold = edt_ft5x06_register_read(tsdata, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 897 | reg_addr->reg_threshold); |
| 898 | tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain); |
| 899 | tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset); |
| 900 | if (reg_addr->reg_report_rate != NO_REGISTER) |
| 901 | tsdata->report_rate = edt_ft5x06_register_read(tsdata, |
| 902 | reg_addr->reg_report_rate); |
| 903 | tsdata->num_x = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_x); |
| 904 | tsdata->num_y = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_y); |
| 905 | } |
| 906 | |
| 907 | static void |
| 908 | edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata) |
| 909 | { |
| 910 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 911 | |
| 912 | switch (tsdata->version) { |
| 913 | case M06: |
| 914 | reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD; |
| 915 | reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE; |
| 916 | reg_addr->reg_gain = WORK_REGISTER_GAIN; |
| 917 | reg_addr->reg_offset = WORK_REGISTER_OFFSET; |
| 918 | reg_addr->reg_num_x = WORK_REGISTER_NUM_X; |
| 919 | reg_addr->reg_num_y = WORK_REGISTER_NUM_Y; |
| 920 | break; |
| 921 | |
| 922 | case M09: |
| 923 | reg_addr->reg_threshold = M09_REGISTER_THRESHOLD; |
| 924 | reg_addr->reg_gain = M09_REGISTER_GAIN; |
| 925 | reg_addr->reg_offset = M09_REGISTER_OFFSET; |
| 926 | reg_addr->reg_num_x = M09_REGISTER_NUM_X; |
| 927 | reg_addr->reg_num_y = M09_REGISTER_NUM_Y; |
| 928 | break; |
| 929 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 932 | #ifdef CONFIG_OF |
| 933 | static int edt_ft5x06_i2c_ts_probe_dt(struct device *dev, |
| 934 | struct edt_ft5x06_ts_data *tsdata) |
| 935 | { |
| 936 | struct device_node *np = dev->of_node; |
| 937 | |
| 938 | /* |
| 939 | * irq_pin is not needed for DT setup. |
| 940 | * irq is associated via 'interrupts' property in DT |
| 941 | */ |
| 942 | tsdata->irq_pin = -EINVAL; |
| 943 | tsdata->reset_pin = of_get_named_gpio(np, "reset-gpios", 0); |
| 944 | tsdata->wake_pin = of_get_named_gpio(np, "wake-gpios", 0); |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | #else |
| 949 | static inline int edt_ft5x06_i2c_ts_probe_dt(struct device *dev, |
| 950 | struct edt_ft5x06_ts_data *tsdata) |
| 951 | { |
| 952 | return -ENODEV; |
| 953 | } |
| 954 | #endif |
| 955 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 956 | static int edt_ft5x06_ts_probe(struct i2c_client *client, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 957 | const struct i2c_device_id *id) |
| 958 | { |
| 959 | const struct edt_ft5x06_platform_data *pdata = |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 960 | dev_get_platdata(&client->dev); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 961 | struct edt_ft5x06_ts_data *tsdata; |
| 962 | struct input_dev *input; |
| 963 | int error; |
| 964 | char fw_version[EDT_NAME_LEN]; |
| 965 | |
| 966 | dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n"); |
| 967 | |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 968 | tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL); |
| 969 | if (!tsdata) { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 970 | dev_err(&client->dev, "failed to allocate driver data.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 971 | return -ENOMEM; |
| 972 | } |
| 973 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 974 | if (!pdata) { |
| 975 | error = edt_ft5x06_i2c_ts_probe_dt(&client->dev, tsdata); |
| 976 | if (error) { |
| 977 | dev_err(&client->dev, |
| 978 | "DT probe failed and no platform data present\n"); |
| 979 | return error; |
| 980 | } |
| 981 | } else { |
| 982 | tsdata->reset_pin = pdata->reset_pin; |
| 983 | tsdata->irq_pin = pdata->irq_pin; |
| 984 | tsdata->wake_pin = -EINVAL; |
| 985 | } |
| 986 | |
| 987 | error = edt_ft5x06_ts_reset(client, tsdata); |
| 988 | if (error) |
| 989 | return error; |
| 990 | |
| 991 | if (gpio_is_valid(tsdata->irq_pin)) { |
| 992 | error = devm_gpio_request_one(&client->dev, tsdata->irq_pin, |
| 993 | GPIOF_IN, "edt-ft5x06 irq"); |
| 994 | if (error) { |
| 995 | dev_err(&client->dev, |
| 996 | "Failed to request GPIO %d, error %d\n", |
| 997 | tsdata->irq_pin, error); |
| 998 | return error; |
| 999 | } |
| 1000 | } |
| 1001 | |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1002 | input = devm_input_allocate_device(&client->dev); |
| 1003 | if (!input) { |
| 1004 | dev_err(&client->dev, "failed to allocate input device.\n"); |
| 1005 | return -ENOMEM; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | mutex_init(&tsdata->mutex); |
| 1009 | tsdata->client = client; |
| 1010 | tsdata->input = input; |
| 1011 | tsdata->factory_mode = false; |
| 1012 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 1013 | error = edt_ft5x06_ts_identify(client, tsdata, fw_version); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1014 | if (error) { |
| 1015 | dev_err(&client->dev, "touchscreen probe failed\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1016 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame^] | 1019 | edt_ft5x06_ts_set_regs(tsdata); |
| 1020 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1021 | if (!pdata) |
| 1022 | edt_ft5x06_ts_get_dt_defaults(client->dev.of_node, tsdata); |
| 1023 | else |
| 1024 | edt_ft5x06_ts_get_defaults(tsdata, pdata); |
| 1025 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1026 | edt_ft5x06_ts_get_parameters(tsdata); |
| 1027 | |
| 1028 | dev_dbg(&client->dev, |
| 1029 | "Model \"%s\", Rev. \"%s\", %dx%d sensors\n", |
| 1030 | tsdata->name, fw_version, tsdata->num_x, tsdata->num_y); |
| 1031 | |
| 1032 | input->name = tsdata->name; |
| 1033 | input->id.bustype = BUS_I2C; |
| 1034 | input->dev.parent = &client->dev; |
| 1035 | |
| 1036 | __set_bit(EV_SYN, input->evbit); |
| 1037 | __set_bit(EV_KEY, input->evbit); |
| 1038 | __set_bit(EV_ABS, input->evbit); |
| 1039 | __set_bit(BTN_TOUCH, input->keybit); |
| 1040 | input_set_abs_params(input, ABS_X, 0, tsdata->num_x * 64 - 1, 0, 0); |
| 1041 | input_set_abs_params(input, ABS_Y, 0, tsdata->num_y * 64 - 1, 0, 0); |
| 1042 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 1043 | 0, tsdata->num_x * 64 - 1, 0, 0); |
| 1044 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 1045 | 0, tsdata->num_y * 64 - 1, 0, 0); |
Henrik Rydberg | b4adbbe | 2012-08-11 22:07:55 +0200 | [diff] [blame] | 1046 | error = input_mt_init_slots(input, MAX_SUPPORT_POINTS, 0); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1047 | if (error) { |
| 1048 | dev_err(&client->dev, "Unable to init MT slots.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1049 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | input_set_drvdata(input, tsdata); |
| 1053 | i2c_set_clientdata(client, tsdata); |
| 1054 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1055 | error = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 1056 | edt_ft5x06_ts_isr, |
| 1057 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 1058 | client->name, tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1059 | if (error) { |
| 1060 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1061 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 1065 | if (error) |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 1066 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1067 | |
| 1068 | error = input_register_device(input); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1069 | if (error) |
| 1070 | goto err_remove_attrs; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1071 | |
| 1072 | edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev)); |
| 1073 | device_init_wakeup(&client->dev, 1); |
| 1074 | |
| 1075 | dev_dbg(&client->dev, |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1076 | "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n", |
| 1077 | client->irq, tsdata->wake_pin, tsdata->reset_pin); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1078 | |
| 1079 | return 0; |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1080 | |
| 1081 | err_remove_attrs: |
| 1082 | sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 1083 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 1086 | static int edt_ft5x06_ts_remove(struct i2c_client *client) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1087 | { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1088 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 1089 | |
| 1090 | edt_ft5x06_ts_teardown_debugfs(tsdata); |
| 1091 | sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 1092 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | #ifdef CONFIG_PM_SLEEP |
| 1097 | static int edt_ft5x06_ts_suspend(struct device *dev) |
| 1098 | { |
| 1099 | struct i2c_client *client = to_i2c_client(dev); |
| 1100 | |
| 1101 | if (device_may_wakeup(dev)) |
| 1102 | enable_irq_wake(client->irq); |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | static int edt_ft5x06_ts_resume(struct device *dev) |
| 1108 | { |
| 1109 | struct i2c_client *client = to_i2c_client(dev); |
| 1110 | |
| 1111 | if (device_may_wakeup(dev)) |
| 1112 | disable_irq_wake(client->irq); |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | #endif |
| 1117 | |
| 1118 | static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops, |
| 1119 | edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume); |
| 1120 | |
| 1121 | static const struct i2c_device_id edt_ft5x06_ts_id[] = { |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 1122 | { "edt-ft5x06", 0, }, |
| 1123 | { /* sentinel */ } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1124 | }; |
| 1125 | MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id); |
| 1126 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1127 | #ifdef CONFIG_OF |
| 1128 | static const struct of_device_id edt_ft5x06_of_match[] = { |
| 1129 | { .compatible = "edt,edt-ft5206", }, |
| 1130 | { .compatible = "edt,edt-ft5306", }, |
| 1131 | { .compatible = "edt,edt-ft5406", }, |
| 1132 | { /* sentinel */ } |
| 1133 | }; |
| 1134 | MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match); |
| 1135 | #endif |
| 1136 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1137 | static struct i2c_driver edt_ft5x06_ts_driver = { |
| 1138 | .driver = { |
| 1139 | .owner = THIS_MODULE, |
| 1140 | .name = "edt_ft5x06", |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1141 | .of_match_table = of_match_ptr(edt_ft5x06_of_match), |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1142 | .pm = &edt_ft5x06_ts_pm_ops, |
| 1143 | }, |
| 1144 | .id_table = edt_ft5x06_ts_id, |
| 1145 | .probe = edt_ft5x06_ts_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 1146 | .remove = edt_ft5x06_ts_remove, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1147 | }; |
| 1148 | |
| 1149 | module_i2c_driver(edt_ft5x06_ts_driver); |
| 1150 | |
| 1151 | MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>"); |
| 1152 | MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver"); |
| 1153 | MODULE_LICENSE("GPL"); |