Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de> |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this library; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * This is a driver for the EDT "Polytouch" family of touch controllers |
| 20 | * based on the FocalTech FT5x06 line of chips. |
| 21 | * |
| 22 | * Development of this driver has been sponsored by Glyn: |
| 23 | * http://www.glyn.com/Products/Displays |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/ratelimit.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/input.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/debugfs.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/gpio.h> |
| 36 | #include <linux/input/mt.h> |
| 37 | #include <linux/input/edt-ft5x06.h> |
| 38 | |
| 39 | #define MAX_SUPPORT_POINTS 5 |
| 40 | |
| 41 | #define WORK_REGISTER_THRESHOLD 0x00 |
| 42 | #define WORK_REGISTER_REPORT_RATE 0x08 |
| 43 | #define WORK_REGISTER_GAIN 0x30 |
| 44 | #define WORK_REGISTER_OFFSET 0x31 |
| 45 | #define WORK_REGISTER_NUM_X 0x33 |
| 46 | #define WORK_REGISTER_NUM_Y 0x34 |
| 47 | |
| 48 | #define WORK_REGISTER_OPMODE 0x3c |
| 49 | #define FACTORY_REGISTER_OPMODE 0x01 |
| 50 | |
| 51 | #define TOUCH_EVENT_DOWN 0x00 |
| 52 | #define TOUCH_EVENT_UP 0x01 |
| 53 | #define TOUCH_EVENT_ON 0x02 |
| 54 | #define TOUCH_EVENT_RESERVED 0x03 |
| 55 | |
| 56 | #define EDT_NAME_LEN 23 |
| 57 | #define EDT_SWITCH_MODE_RETRIES 10 |
| 58 | #define EDT_SWITCH_MODE_DELAY 5 /* msec */ |
| 59 | #define EDT_RAW_DATA_RETRIES 100 |
| 60 | #define EDT_RAW_DATA_DELAY 1 /* msec */ |
| 61 | |
| 62 | struct edt_ft5x06_ts_data { |
| 63 | struct i2c_client *client; |
| 64 | struct input_dev *input; |
| 65 | u16 num_x; |
| 66 | u16 num_y; |
| 67 | |
| 68 | #if defined(CONFIG_DEBUG_FS) |
| 69 | struct dentry *debug_dir; |
| 70 | u8 *raw_buffer; |
| 71 | size_t raw_bufsize; |
| 72 | #endif |
| 73 | |
| 74 | struct mutex mutex; |
| 75 | bool factory_mode; |
| 76 | int threshold; |
| 77 | int gain; |
| 78 | int offset; |
| 79 | int report_rate; |
| 80 | |
| 81 | char name[EDT_NAME_LEN]; |
| 82 | }; |
| 83 | |
| 84 | static int edt_ft5x06_ts_readwrite(struct i2c_client *client, |
| 85 | u16 wr_len, u8 *wr_buf, |
| 86 | u16 rd_len, u8 *rd_buf) |
| 87 | { |
| 88 | struct i2c_msg wrmsg[2]; |
| 89 | int i = 0; |
| 90 | int ret; |
| 91 | |
| 92 | if (wr_len) { |
| 93 | wrmsg[i].addr = client->addr; |
| 94 | wrmsg[i].flags = 0; |
| 95 | wrmsg[i].len = wr_len; |
| 96 | wrmsg[i].buf = wr_buf; |
| 97 | i++; |
| 98 | } |
| 99 | if (rd_len) { |
| 100 | wrmsg[i].addr = client->addr; |
| 101 | wrmsg[i].flags = I2C_M_RD; |
| 102 | wrmsg[i].len = rd_len; |
| 103 | wrmsg[i].buf = rd_buf; |
| 104 | i++; |
| 105 | } |
| 106 | |
| 107 | ret = i2c_transfer(client->adapter, wrmsg, i); |
| 108 | if (ret < 0) |
| 109 | return ret; |
| 110 | if (ret != i) |
| 111 | return -EIO; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata, |
| 117 | u8 *buf, int buflen) |
| 118 | { |
| 119 | int i; |
| 120 | u8 crc = 0; |
| 121 | |
| 122 | for (i = 0; i < buflen - 1; i++) |
| 123 | crc ^= buf[i]; |
| 124 | |
| 125 | if (crc != buf[buflen-1]) { |
| 126 | dev_err_ratelimited(&tsdata->client->dev, |
| 127 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 128 | crc, buf[buflen-1]); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id) |
| 136 | { |
| 137 | struct edt_ft5x06_ts_data *tsdata = dev_id; |
| 138 | struct device *dev = &tsdata->client->dev; |
| 139 | u8 cmd = 0xf9; |
| 140 | u8 rdbuf[26]; |
| 141 | int i, type, x, y, id; |
| 142 | int error; |
| 143 | |
| 144 | memset(rdbuf, 0, sizeof(rdbuf)); |
| 145 | |
| 146 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 147 | sizeof(cmd), &cmd, |
| 148 | sizeof(rdbuf), rdbuf); |
| 149 | if (error) { |
| 150 | dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n", |
| 151 | error); |
| 152 | goto out; |
| 153 | } |
| 154 | |
| 155 | if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa || rdbuf[2] != 26) { |
| 156 | dev_err_ratelimited(dev, "Unexpected header: %02x%02x%02x!\n", |
| 157 | rdbuf[0], rdbuf[1], rdbuf[2]); |
| 158 | goto out; |
| 159 | } |
| 160 | |
| 161 | if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, 26)) |
| 162 | goto out; |
| 163 | |
| 164 | for (i = 0; i < MAX_SUPPORT_POINTS; i++) { |
| 165 | u8 *buf = &rdbuf[i * 4 + 5]; |
| 166 | bool down; |
| 167 | |
| 168 | type = buf[0] >> 6; |
| 169 | /* ignore Reserved events */ |
| 170 | if (type == TOUCH_EVENT_RESERVED) |
| 171 | continue; |
| 172 | |
| 173 | x = ((buf[0] << 8) | buf[1]) & 0x0fff; |
| 174 | y = ((buf[2] << 8) | buf[3]) & 0x0fff; |
| 175 | id = (buf[2] >> 4) & 0x0f; |
| 176 | down = (type != TOUCH_EVENT_UP); |
| 177 | |
| 178 | input_mt_slot(tsdata->input, id); |
| 179 | input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down); |
| 180 | |
| 181 | if (!down) |
| 182 | continue; |
| 183 | |
| 184 | input_report_abs(tsdata->input, ABS_MT_POSITION_X, x); |
| 185 | input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y); |
| 186 | } |
| 187 | |
| 188 | input_mt_report_pointer_emulation(tsdata->input, true); |
| 189 | input_sync(tsdata->input); |
| 190 | |
| 191 | out: |
| 192 | return IRQ_HANDLED; |
| 193 | } |
| 194 | |
| 195 | static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata, |
| 196 | u8 addr, u8 value) |
| 197 | { |
| 198 | u8 wrbuf[4]; |
| 199 | |
| 200 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 201 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
| 202 | wrbuf[2] = value; |
| 203 | wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2]; |
| 204 | |
| 205 | return edt_ft5x06_ts_readwrite(tsdata->client, 4, wrbuf, 0, NULL); |
| 206 | } |
| 207 | |
| 208 | static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata, |
| 209 | u8 addr) |
| 210 | { |
| 211 | u8 wrbuf[2], rdbuf[2]; |
| 212 | int error; |
| 213 | |
| 214 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 215 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
| 216 | wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40; |
| 217 | |
| 218 | error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2, rdbuf); |
| 219 | if (error) |
| 220 | return error; |
| 221 | |
| 222 | if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) { |
| 223 | dev_err(&tsdata->client->dev, |
| 224 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 225 | wrbuf[0] ^ wrbuf[1] ^ rdbuf[0], rdbuf[1]); |
| 226 | return -EIO; |
| 227 | } |
| 228 | |
| 229 | return rdbuf[0]; |
| 230 | } |
| 231 | |
| 232 | struct edt_ft5x06_attribute { |
| 233 | struct device_attribute dattr; |
| 234 | size_t field_offset; |
| 235 | u8 limit_low; |
| 236 | u8 limit_high; |
| 237 | u8 addr; |
| 238 | }; |
| 239 | |
| 240 | #define EDT_ATTR(_field, _mode, _addr, _limit_low, _limit_high) \ |
| 241 | struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \ |
| 242 | .dattr = __ATTR(_field, _mode, \ |
| 243 | edt_ft5x06_setting_show, \ |
| 244 | edt_ft5x06_setting_store), \ |
| 245 | .field_offset = \ |
| 246 | offsetof(struct edt_ft5x06_ts_data, _field), \ |
| 247 | .limit_low = _limit_low, \ |
| 248 | .limit_high = _limit_high, \ |
| 249 | .addr = _addr, \ |
| 250 | } |
| 251 | |
| 252 | static ssize_t edt_ft5x06_setting_show(struct device *dev, |
| 253 | struct device_attribute *dattr, |
| 254 | char *buf) |
| 255 | { |
| 256 | struct i2c_client *client = to_i2c_client(dev); |
| 257 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 258 | struct edt_ft5x06_attribute *attr = |
| 259 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
| 260 | u8 *field = (u8 *)((char *)tsdata + attr->field_offset); |
| 261 | int val; |
| 262 | size_t count = 0; |
| 263 | int error = 0; |
| 264 | |
| 265 | mutex_lock(&tsdata->mutex); |
| 266 | |
| 267 | if (tsdata->factory_mode) { |
| 268 | error = -EIO; |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | val = edt_ft5x06_register_read(tsdata, attr->addr); |
| 273 | if (val < 0) { |
| 274 | error = val; |
| 275 | dev_err(&tsdata->client->dev, |
| 276 | "Failed to fetch attribute %s, error %d\n", |
| 277 | dattr->attr.name, error); |
| 278 | goto out; |
| 279 | } |
| 280 | |
| 281 | if (val != *field) { |
| 282 | dev_warn(&tsdata->client->dev, |
| 283 | "%s: read (%d) and stored value (%d) differ\n", |
| 284 | dattr->attr.name, val, *field); |
| 285 | *field = val; |
| 286 | } |
| 287 | |
| 288 | count = scnprintf(buf, PAGE_SIZE, "%d\n", val); |
| 289 | out: |
| 290 | mutex_unlock(&tsdata->mutex); |
| 291 | return error ?: count; |
| 292 | } |
| 293 | |
| 294 | static ssize_t edt_ft5x06_setting_store(struct device *dev, |
| 295 | struct device_attribute *dattr, |
| 296 | const char *buf, size_t count) |
| 297 | { |
| 298 | struct i2c_client *client = to_i2c_client(dev); |
| 299 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 300 | struct edt_ft5x06_attribute *attr = |
| 301 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
| 302 | u8 *field = (u8 *)((char *)tsdata + attr->field_offset); |
| 303 | unsigned int val; |
| 304 | int error; |
| 305 | |
| 306 | mutex_lock(&tsdata->mutex); |
| 307 | |
| 308 | if (tsdata->factory_mode) { |
| 309 | error = -EIO; |
| 310 | goto out; |
| 311 | } |
| 312 | |
| 313 | error = kstrtouint(buf, 0, &val); |
| 314 | if (error) |
| 315 | goto out; |
| 316 | |
| 317 | if (val < attr->limit_low || val > attr->limit_high) { |
| 318 | error = -ERANGE; |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | error = edt_ft5x06_register_write(tsdata, attr->addr, val); |
| 323 | if (error) { |
| 324 | dev_err(&tsdata->client->dev, |
| 325 | "Failed to update attribute %s, error: %d\n", |
| 326 | dattr->attr.name, error); |
| 327 | goto out; |
| 328 | } |
| 329 | |
| 330 | *field = val; |
| 331 | |
| 332 | out: |
| 333 | mutex_unlock(&tsdata->mutex); |
| 334 | return error ?: count; |
| 335 | } |
| 336 | |
| 337 | static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN, 0, 31); |
| 338 | static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET, 0, 31); |
| 339 | static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, |
| 340 | WORK_REGISTER_THRESHOLD, 20, 80); |
| 341 | static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, |
| 342 | WORK_REGISTER_REPORT_RATE, 3, 14); |
| 343 | |
| 344 | static struct attribute *edt_ft5x06_attrs[] = { |
| 345 | &edt_ft5x06_attr_gain.dattr.attr, |
| 346 | &edt_ft5x06_attr_offset.dattr.attr, |
| 347 | &edt_ft5x06_attr_threshold.dattr.attr, |
| 348 | &edt_ft5x06_attr_report_rate.dattr.attr, |
| 349 | NULL |
| 350 | }; |
| 351 | |
| 352 | static const struct attribute_group edt_ft5x06_attr_group = { |
| 353 | .attrs = edt_ft5x06_attrs, |
| 354 | }; |
| 355 | |
| 356 | #ifdef CONFIG_DEBUG_FS |
| 357 | static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata) |
| 358 | { |
| 359 | struct i2c_client *client = tsdata->client; |
| 360 | int retries = EDT_SWITCH_MODE_RETRIES; |
| 361 | int ret; |
| 362 | int error; |
| 363 | |
| 364 | disable_irq(client->irq); |
| 365 | |
| 366 | if (!tsdata->raw_buffer) { |
| 367 | tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y * |
| 368 | sizeof(u16); |
| 369 | tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL); |
| 370 | if (!tsdata->raw_buffer) { |
| 371 | error = -ENOMEM; |
| 372 | goto err_out; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* mode register is 0x3c when in the work mode */ |
| 377 | error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03); |
| 378 | if (error) { |
| 379 | dev_err(&client->dev, |
| 380 | "failed to switch to factory mode, error %d\n", error); |
| 381 | goto err_out; |
| 382 | } |
| 383 | |
| 384 | tsdata->factory_mode = true; |
| 385 | do { |
| 386 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 387 | /* mode register is 0x01 when in factory mode */ |
| 388 | ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE); |
| 389 | if (ret == 0x03) |
| 390 | break; |
| 391 | } while (--retries > 0); |
| 392 | |
| 393 | if (retries == 0) { |
| 394 | dev_err(&client->dev, "not in factory mode after %dms.\n", |
| 395 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 396 | error = -EIO; |
| 397 | goto err_out; |
| 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | |
| 402 | err_out: |
| 403 | kfree(tsdata->raw_buffer); |
| 404 | tsdata->raw_buffer = NULL; |
| 405 | tsdata->factory_mode = false; |
| 406 | enable_irq(client->irq); |
| 407 | |
| 408 | return error; |
| 409 | } |
| 410 | |
| 411 | static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata) |
| 412 | { |
| 413 | struct i2c_client *client = tsdata->client; |
| 414 | int retries = EDT_SWITCH_MODE_RETRIES; |
| 415 | int ret; |
| 416 | int error; |
| 417 | |
| 418 | /* mode register is 0x01 when in the factory mode */ |
| 419 | error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1); |
| 420 | if (error) { |
| 421 | dev_err(&client->dev, |
| 422 | "failed to switch to work mode, error: %d\n", error); |
| 423 | return error; |
| 424 | } |
| 425 | |
| 426 | tsdata->factory_mode = false; |
| 427 | |
| 428 | do { |
| 429 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 430 | /* mode register is 0x01 when in factory mode */ |
| 431 | ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE); |
| 432 | if (ret == 0x01) |
| 433 | break; |
| 434 | } while (--retries > 0); |
| 435 | |
| 436 | if (retries == 0) { |
| 437 | dev_err(&client->dev, "not in work mode after %dms.\n", |
| 438 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 439 | tsdata->factory_mode = true; |
| 440 | return -EIO; |
| 441 | } |
| 442 | |
| 443 | if (tsdata->raw_buffer) |
| 444 | kfree(tsdata->raw_buffer); |
| 445 | tsdata->raw_buffer = NULL; |
| 446 | |
| 447 | /* restore parameters */ |
| 448 | edt_ft5x06_register_write(tsdata, WORK_REGISTER_THRESHOLD, |
| 449 | tsdata->threshold); |
| 450 | edt_ft5x06_register_write(tsdata, WORK_REGISTER_GAIN, |
| 451 | tsdata->gain); |
| 452 | edt_ft5x06_register_write(tsdata, WORK_REGISTER_OFFSET, |
| 453 | tsdata->offset); |
| 454 | edt_ft5x06_register_write(tsdata, WORK_REGISTER_REPORT_RATE, |
| 455 | tsdata->report_rate); |
| 456 | |
| 457 | enable_irq(client->irq); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode) |
| 463 | { |
| 464 | struct edt_ft5x06_ts_data *tsdata = data; |
| 465 | |
| 466 | *mode = tsdata->factory_mode; |
| 467 | |
| 468 | return 0; |
| 469 | }; |
| 470 | |
| 471 | static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode) |
| 472 | { |
| 473 | struct edt_ft5x06_ts_data *tsdata = data; |
| 474 | int retval = 0; |
| 475 | |
| 476 | if (mode > 1) |
| 477 | return -ERANGE; |
| 478 | |
| 479 | mutex_lock(&tsdata->mutex); |
| 480 | |
| 481 | if (mode != tsdata->factory_mode) { |
| 482 | retval = mode ? edt_ft5x06_factory_mode(tsdata) : |
| 483 | edt_ft5x06_work_mode(tsdata); |
| 484 | } |
| 485 | |
| 486 | mutex_unlock(&tsdata->mutex); |
| 487 | |
| 488 | return retval; |
| 489 | }; |
| 490 | |
| 491 | DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get, |
| 492 | edt_ft5x06_debugfs_mode_set, "%llu\n"); |
| 493 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 494 | static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file, |
| 495 | char __user *buf, size_t count, loff_t *off) |
| 496 | { |
| 497 | struct edt_ft5x06_ts_data *tsdata = file->private_data; |
| 498 | struct i2c_client *client = tsdata->client; |
| 499 | int retries = EDT_RAW_DATA_RETRIES; |
| 500 | int val, i, error; |
| 501 | size_t read = 0; |
| 502 | int colbytes; |
| 503 | char wrbuf[3]; |
| 504 | u8 *rdbuf; |
| 505 | |
| 506 | if (*off < 0 || *off >= tsdata->raw_bufsize) |
| 507 | return 0; |
| 508 | |
| 509 | mutex_lock(&tsdata->mutex); |
| 510 | |
| 511 | if (!tsdata->factory_mode || !tsdata->raw_buffer) { |
| 512 | error = -EIO; |
| 513 | goto out; |
| 514 | } |
| 515 | |
| 516 | error = edt_ft5x06_register_write(tsdata, 0x08, 0x01); |
| 517 | if (error) { |
| 518 | dev_dbg(&client->dev, |
| 519 | "failed to write 0x08 register, error %d\n", error); |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | do { |
| 524 | msleep(EDT_RAW_DATA_DELAY); |
| 525 | val = edt_ft5x06_register_read(tsdata, 0x08); |
| 526 | if (val < 1) |
| 527 | break; |
| 528 | } while (--retries > 0); |
| 529 | |
| 530 | if (val < 0) { |
| 531 | error = val; |
| 532 | dev_dbg(&client->dev, |
| 533 | "failed to read 0x08 register, error %d\n", error); |
| 534 | goto out; |
| 535 | } |
| 536 | |
| 537 | if (retries == 0) { |
| 538 | dev_dbg(&client->dev, |
| 539 | "timed out waiting for register to settle\n"); |
| 540 | error = -ETIMEDOUT; |
| 541 | goto out; |
| 542 | } |
| 543 | |
| 544 | rdbuf = tsdata->raw_buffer; |
| 545 | colbytes = tsdata->num_y * sizeof(u16); |
| 546 | |
| 547 | wrbuf[0] = 0xf5; |
| 548 | wrbuf[1] = 0x0e; |
| 549 | for (i = 0; i < tsdata->num_x; i++) { |
| 550 | wrbuf[2] = i; /* column index */ |
| 551 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 552 | sizeof(wrbuf), wrbuf, |
| 553 | colbytes, rdbuf); |
| 554 | if (error) |
| 555 | goto out; |
| 556 | |
| 557 | rdbuf += colbytes; |
| 558 | } |
| 559 | |
| 560 | read = min_t(size_t, count, tsdata->raw_bufsize - *off); |
Axel Lin | 35b1da4 | 2012-09-19 15:56:23 -0700 | [diff] [blame] | 561 | if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) { |
| 562 | error = -EFAULT; |
| 563 | goto out; |
| 564 | } |
| 565 | |
| 566 | *off += read; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 567 | out: |
| 568 | mutex_unlock(&tsdata->mutex); |
| 569 | return error ?: read; |
| 570 | }; |
| 571 | |
| 572 | |
| 573 | static const struct file_operations debugfs_raw_data_fops = { |
Wei Yongjun | f6c0df6 | 2012-10-17 23:55:54 -0700 | [diff] [blame] | 574 | .open = simple_open, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 575 | .read = edt_ft5x06_debugfs_raw_data_read, |
| 576 | }; |
| 577 | |
| 578 | static void __devinit |
| 579 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 580 | const char *debugfs_name) |
| 581 | { |
| 582 | tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL); |
| 583 | if (!tsdata->debug_dir) |
| 584 | return; |
| 585 | |
| 586 | debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x); |
| 587 | debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y); |
| 588 | |
| 589 | debugfs_create_file("mode", S_IRUSR | S_IWUSR, |
| 590 | tsdata->debug_dir, tsdata, &debugfs_mode_fops); |
| 591 | debugfs_create_file("raw_data", S_IRUSR, |
| 592 | tsdata->debug_dir, tsdata, &debugfs_raw_data_fops); |
| 593 | } |
| 594 | |
| 595 | static void __devexit |
| 596 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 597 | { |
| 598 | if (tsdata->debug_dir) |
| 599 | debugfs_remove_recursive(tsdata->debug_dir); |
Guenter Roeck | a1d0fa7 | 2012-08-21 22:01:45 -0700 | [diff] [blame] | 600 | kfree(tsdata->raw_buffer); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | #else |
| 604 | |
| 605 | static inline void |
| 606 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 607 | const char *debugfs_name) |
| 608 | { |
| 609 | } |
| 610 | |
| 611 | static inline void |
| 612 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 613 | { |
| 614 | } |
| 615 | |
| 616 | #endif /* CONFIG_DEBUGFS */ |
| 617 | |
| 618 | |
| 619 | |
| 620 | static int __devinit edt_ft5x06_ts_reset(struct i2c_client *client, |
| 621 | int reset_pin) |
| 622 | { |
| 623 | int error; |
| 624 | |
| 625 | if (gpio_is_valid(reset_pin)) { |
| 626 | /* this pulls reset down, enabling the low active reset */ |
| 627 | error = gpio_request_one(reset_pin, GPIOF_OUT_INIT_LOW, |
| 628 | "edt-ft5x06 reset"); |
| 629 | if (error) { |
| 630 | dev_err(&client->dev, |
| 631 | "Failed to request GPIO %d as reset pin, error %d\n", |
| 632 | reset_pin, error); |
| 633 | return error; |
| 634 | } |
| 635 | |
| 636 | mdelay(50); |
| 637 | gpio_set_value(reset_pin, 1); |
| 638 | mdelay(100); |
| 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int __devinit edt_ft5x06_ts_identify(struct i2c_client *client, |
| 645 | char *model_name, |
| 646 | char *fw_version) |
| 647 | { |
| 648 | u8 rdbuf[EDT_NAME_LEN]; |
| 649 | char *p; |
| 650 | int error; |
| 651 | |
| 652 | error = edt_ft5x06_ts_readwrite(client, 1, "\xbb", |
| 653 | EDT_NAME_LEN - 1, rdbuf); |
| 654 | if (error) |
| 655 | return error; |
| 656 | |
| 657 | /* remove last '$' end marker */ |
| 658 | rdbuf[EDT_NAME_LEN - 1] = '\0'; |
| 659 | if (rdbuf[EDT_NAME_LEN - 2] == '$') |
| 660 | rdbuf[EDT_NAME_LEN - 2] = '\0'; |
| 661 | |
| 662 | /* look for Model/Version separator */ |
| 663 | p = strchr(rdbuf, '*'); |
| 664 | if (p) |
| 665 | *p++ = '\0'; |
| 666 | |
| 667 | strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN); |
| 668 | strlcpy(fw_version, p ? p : "", EDT_NAME_LEN); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | #define EDT_ATTR_CHECKSET(name, reg) \ |
| 674 | if (pdata->name >= edt_ft5x06_attr_##name.limit_low && \ |
| 675 | pdata->name <= edt_ft5x06_attr_##name.limit_high) \ |
| 676 | edt_ft5x06_register_write(tsdata, reg, pdata->name) |
| 677 | |
| 678 | static void __devinit |
| 679 | edt_ft5x06_ts_get_defaults(struct edt_ft5x06_ts_data *tsdata, |
| 680 | const struct edt_ft5x06_platform_data *pdata) |
| 681 | { |
| 682 | if (!pdata->use_parameters) |
| 683 | return; |
| 684 | |
| 685 | /* pick up defaults from the platform data */ |
| 686 | EDT_ATTR_CHECKSET(threshold, WORK_REGISTER_THRESHOLD); |
| 687 | EDT_ATTR_CHECKSET(gain, WORK_REGISTER_GAIN); |
| 688 | EDT_ATTR_CHECKSET(offset, WORK_REGISTER_OFFSET); |
| 689 | EDT_ATTR_CHECKSET(report_rate, WORK_REGISTER_REPORT_RATE); |
| 690 | } |
| 691 | |
| 692 | static void __devinit |
| 693 | edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata) |
| 694 | { |
| 695 | tsdata->threshold = edt_ft5x06_register_read(tsdata, |
| 696 | WORK_REGISTER_THRESHOLD); |
| 697 | tsdata->gain = edt_ft5x06_register_read(tsdata, WORK_REGISTER_GAIN); |
| 698 | tsdata->offset = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OFFSET); |
| 699 | tsdata->report_rate = edt_ft5x06_register_read(tsdata, |
| 700 | WORK_REGISTER_REPORT_RATE); |
| 701 | tsdata->num_x = edt_ft5x06_register_read(tsdata, WORK_REGISTER_NUM_X); |
| 702 | tsdata->num_y = edt_ft5x06_register_read(tsdata, WORK_REGISTER_NUM_Y); |
| 703 | } |
| 704 | |
| 705 | static int __devinit edt_ft5x06_ts_probe(struct i2c_client *client, |
| 706 | const struct i2c_device_id *id) |
| 707 | { |
| 708 | const struct edt_ft5x06_platform_data *pdata = |
| 709 | client->dev.platform_data; |
| 710 | struct edt_ft5x06_ts_data *tsdata; |
| 711 | struct input_dev *input; |
| 712 | int error; |
| 713 | char fw_version[EDT_NAME_LEN]; |
| 714 | |
| 715 | dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n"); |
| 716 | |
| 717 | if (!pdata) { |
| 718 | dev_err(&client->dev, "no platform data?\n"); |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | |
| 722 | error = edt_ft5x06_ts_reset(client, pdata->reset_pin); |
| 723 | if (error) |
| 724 | return error; |
| 725 | |
| 726 | if (gpio_is_valid(pdata->irq_pin)) { |
| 727 | error = gpio_request_one(pdata->irq_pin, |
| 728 | GPIOF_IN, "edt-ft5x06 irq"); |
| 729 | if (error) { |
| 730 | dev_err(&client->dev, |
| 731 | "Failed to request GPIO %d, error %d\n", |
| 732 | pdata->irq_pin, error); |
| 733 | return error; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | tsdata = kzalloc(sizeof(*tsdata), GFP_KERNEL); |
| 738 | input = input_allocate_device(); |
| 739 | if (!tsdata || !input) { |
| 740 | dev_err(&client->dev, "failed to allocate driver data.\n"); |
| 741 | error = -ENOMEM; |
| 742 | goto err_free_mem; |
| 743 | } |
| 744 | |
| 745 | mutex_init(&tsdata->mutex); |
| 746 | tsdata->client = client; |
| 747 | tsdata->input = input; |
| 748 | tsdata->factory_mode = false; |
| 749 | |
| 750 | error = edt_ft5x06_ts_identify(client, tsdata->name, fw_version); |
| 751 | if (error) { |
| 752 | dev_err(&client->dev, "touchscreen probe failed\n"); |
| 753 | goto err_free_mem; |
| 754 | } |
| 755 | |
| 756 | edt_ft5x06_ts_get_defaults(tsdata, pdata); |
| 757 | edt_ft5x06_ts_get_parameters(tsdata); |
| 758 | |
| 759 | dev_dbg(&client->dev, |
| 760 | "Model \"%s\", Rev. \"%s\", %dx%d sensors\n", |
| 761 | tsdata->name, fw_version, tsdata->num_x, tsdata->num_y); |
| 762 | |
| 763 | input->name = tsdata->name; |
| 764 | input->id.bustype = BUS_I2C; |
| 765 | input->dev.parent = &client->dev; |
| 766 | |
| 767 | __set_bit(EV_SYN, input->evbit); |
| 768 | __set_bit(EV_KEY, input->evbit); |
| 769 | __set_bit(EV_ABS, input->evbit); |
| 770 | __set_bit(BTN_TOUCH, input->keybit); |
| 771 | input_set_abs_params(input, ABS_X, 0, tsdata->num_x * 64 - 1, 0, 0); |
| 772 | input_set_abs_params(input, ABS_Y, 0, tsdata->num_y * 64 - 1, 0, 0); |
| 773 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 774 | 0, tsdata->num_x * 64 - 1, 0, 0); |
| 775 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 776 | 0, tsdata->num_y * 64 - 1, 0, 0); |
Henrik Rydberg | b4adbbe | 2012-08-11 22:07:55 +0200 | [diff] [blame] | 777 | error = input_mt_init_slots(input, MAX_SUPPORT_POINTS, 0); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 778 | if (error) { |
| 779 | dev_err(&client->dev, "Unable to init MT slots.\n"); |
| 780 | goto err_free_mem; |
| 781 | } |
| 782 | |
| 783 | input_set_drvdata(input, tsdata); |
| 784 | i2c_set_clientdata(client, tsdata); |
| 785 | |
| 786 | error = request_threaded_irq(client->irq, NULL, edt_ft5x06_ts_isr, |
| 787 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 788 | client->name, tsdata); |
| 789 | if (error) { |
| 790 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
| 791 | goto err_free_mem; |
| 792 | } |
| 793 | |
| 794 | error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 795 | if (error) |
| 796 | goto err_free_irq; |
| 797 | |
| 798 | error = input_register_device(input); |
| 799 | if (error) |
| 800 | goto err_remove_attrs; |
| 801 | |
| 802 | edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev)); |
| 803 | device_init_wakeup(&client->dev, 1); |
| 804 | |
| 805 | dev_dbg(&client->dev, |
| 806 | "EDT FT5x06 initialized: IRQ pin %d, Reset pin %d.\n", |
| 807 | pdata->irq_pin, pdata->reset_pin); |
| 808 | |
| 809 | return 0; |
| 810 | |
| 811 | err_remove_attrs: |
| 812 | sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 813 | err_free_irq: |
| 814 | free_irq(client->irq, tsdata); |
| 815 | err_free_mem: |
| 816 | input_free_device(input); |
| 817 | kfree(tsdata); |
| 818 | |
| 819 | if (gpio_is_valid(pdata->irq_pin)) |
| 820 | gpio_free(pdata->irq_pin); |
| 821 | |
| 822 | return error; |
| 823 | } |
| 824 | |
| 825 | static int __devexit edt_ft5x06_ts_remove(struct i2c_client *client) |
| 826 | { |
| 827 | const struct edt_ft5x06_platform_data *pdata = |
| 828 | dev_get_platdata(&client->dev); |
| 829 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 830 | |
| 831 | edt_ft5x06_ts_teardown_debugfs(tsdata); |
| 832 | sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 833 | |
| 834 | free_irq(client->irq, tsdata); |
| 835 | input_unregister_device(tsdata->input); |
| 836 | |
| 837 | if (gpio_is_valid(pdata->irq_pin)) |
| 838 | gpio_free(pdata->irq_pin); |
| 839 | if (gpio_is_valid(pdata->reset_pin)) |
| 840 | gpio_free(pdata->reset_pin); |
| 841 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 842 | kfree(tsdata); |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | #ifdef CONFIG_PM_SLEEP |
| 848 | static int edt_ft5x06_ts_suspend(struct device *dev) |
| 849 | { |
| 850 | struct i2c_client *client = to_i2c_client(dev); |
| 851 | |
| 852 | if (device_may_wakeup(dev)) |
| 853 | enable_irq_wake(client->irq); |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static int edt_ft5x06_ts_resume(struct device *dev) |
| 859 | { |
| 860 | struct i2c_client *client = to_i2c_client(dev); |
| 861 | |
| 862 | if (device_may_wakeup(dev)) |
| 863 | disable_irq_wake(client->irq); |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | #endif |
| 868 | |
| 869 | static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops, |
| 870 | edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume); |
| 871 | |
| 872 | static const struct i2c_device_id edt_ft5x06_ts_id[] = { |
| 873 | { "edt-ft5x06", 0 }, |
| 874 | { } |
| 875 | }; |
| 876 | MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id); |
| 877 | |
| 878 | static struct i2c_driver edt_ft5x06_ts_driver = { |
| 879 | .driver = { |
| 880 | .owner = THIS_MODULE, |
| 881 | .name = "edt_ft5x06", |
| 882 | .pm = &edt_ft5x06_ts_pm_ops, |
| 883 | }, |
| 884 | .id_table = edt_ft5x06_ts_id, |
| 885 | .probe = edt_ft5x06_ts_probe, |
| 886 | .remove = __devexit_p(edt_ft5x06_ts_remove), |
| 887 | }; |
| 888 | |
| 889 | module_i2c_driver(edt_ft5x06_ts_driver); |
| 890 | |
| 891 | MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>"); |
| 892 | MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver"); |
| 893 | MODULE_LICENSE("GPL"); |