Merge remote-tracking branch 'mmc-uh/next'
[deliverable/linux.git] / drivers / input / touchscreen / atmel_mxt_ts.c
1 /*
2 * Atmel maXTouch Touchscreen driver
3 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Copyright (C) 2011-2014 Atmel Corporation
6 * Copyright (C) 2012 Google, Inc.
7 *
8 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17 #include <linux/acpi.h>
18 #include <linux/dmi.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/completion.h>
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/i2c.h>
25 #include <linux/platform_data/atmel_mxt_ts.h>
26 #include <linux/input/mt.h>
27 #include <linux/interrupt.h>
28 #include <linux/of.h>
29 #include <linux/slab.h>
30 #include <asm/unaligned.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/videobuf2-v4l2.h>
34 #include <media/videobuf2-vmalloc.h>
35
36 /* Firmware files */
37 #define MXT_FW_NAME "maxtouch.fw"
38 #define MXT_CFG_NAME "maxtouch.cfg"
39 #define MXT_CFG_MAGIC "OBP_RAW V1"
40
41 /* Registers */
42 #define MXT_OBJECT_START 0x07
43 #define MXT_OBJECT_SIZE 6
44 #define MXT_INFO_CHECKSUM_SIZE 3
45 #define MXT_MAX_BLOCK_WRITE 256
46
47 /* Object types */
48 #define MXT_DEBUG_DIAGNOSTIC_T37 37
49 #define MXT_GEN_MESSAGE_T5 5
50 #define MXT_GEN_COMMAND_T6 6
51 #define MXT_GEN_POWER_T7 7
52 #define MXT_GEN_ACQUIRE_T8 8
53 #define MXT_GEN_DATASOURCE_T53 53
54 #define MXT_TOUCH_MULTI_T9 9
55 #define MXT_TOUCH_KEYARRAY_T15 15
56 #define MXT_TOUCH_PROXIMITY_T23 23
57 #define MXT_TOUCH_PROXKEY_T52 52
58 #define MXT_PROCI_GRIPFACE_T20 20
59 #define MXT_PROCG_NOISE_T22 22
60 #define MXT_PROCI_ONETOUCH_T24 24
61 #define MXT_PROCI_TWOTOUCH_T27 27
62 #define MXT_PROCI_GRIP_T40 40
63 #define MXT_PROCI_PALM_T41 41
64 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
65 #define MXT_PROCI_STYLUS_T47 47
66 #define MXT_PROCG_NOISESUPPRESSION_T48 48
67 #define MXT_SPT_COMMSCONFIG_T18 18
68 #define MXT_SPT_GPIOPWM_T19 19
69 #define MXT_SPT_SELFTEST_T25 25
70 #define MXT_SPT_CTECONFIG_T28 28
71 #define MXT_SPT_USERDATA_T38 38
72 #define MXT_SPT_DIGITIZER_T43 43
73 #define MXT_SPT_MESSAGECOUNT_T44 44
74 #define MXT_SPT_CTECONFIG_T46 46
75 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
76
77 /* MXT_GEN_MESSAGE_T5 object */
78 #define MXT_RPTID_NOMSG 0xff
79
80 /* MXT_GEN_COMMAND_T6 field */
81 #define MXT_COMMAND_RESET 0
82 #define MXT_COMMAND_BACKUPNV 1
83 #define MXT_COMMAND_CALIBRATE 2
84 #define MXT_COMMAND_REPORTALL 3
85 #define MXT_COMMAND_DIAGNOSTIC 5
86
87 /* Define for T6 status byte */
88 #define MXT_T6_STATUS_RESET (1 << 7)
89 #define MXT_T6_STATUS_OFL (1 << 6)
90 #define MXT_T6_STATUS_SIGERR (1 << 5)
91 #define MXT_T6_STATUS_CAL (1 << 4)
92 #define MXT_T6_STATUS_CFGERR (1 << 3)
93 #define MXT_T6_STATUS_COMSERR (1 << 2)
94
95 /* MXT_GEN_POWER_T7 field */
96 struct t7_config {
97 u8 idle;
98 u8 active;
99 } __packed;
100
101 #define MXT_POWER_CFG_RUN 0
102 #define MXT_POWER_CFG_DEEPSLEEP 1
103
104 /* MXT_TOUCH_MULTI_T9 field */
105 #define MXT_T9_CTRL 0
106 #define MXT_T9_XSIZE 3
107 #define MXT_T9_YSIZE 4
108 #define MXT_T9_ORIENT 9
109 #define MXT_T9_RANGE 18
110
111 /* MXT_TOUCH_MULTI_T9 status */
112 #define MXT_T9_UNGRIP (1 << 0)
113 #define MXT_T9_SUPPRESS (1 << 1)
114 #define MXT_T9_AMP (1 << 2)
115 #define MXT_T9_VECTOR (1 << 3)
116 #define MXT_T9_MOVE (1 << 4)
117 #define MXT_T9_RELEASE (1 << 5)
118 #define MXT_T9_PRESS (1 << 6)
119 #define MXT_T9_DETECT (1 << 7)
120
121 struct t9_range {
122 __le16 x;
123 __le16 y;
124 } __packed;
125
126 /* MXT_TOUCH_MULTI_T9 orient */
127 #define MXT_T9_ORIENT_SWITCH (1 << 0)
128 #define MXT_T9_ORIENT_INVERTX (1 << 1)
129 #define MXT_T9_ORIENT_INVERTY (1 << 2)
130
131 /* MXT_SPT_COMMSCONFIG_T18 */
132 #define MXT_COMMS_CTRL 0
133 #define MXT_COMMS_CMD 1
134
135 /* MXT_DEBUG_DIAGNOSTIC_T37 */
136 #define MXT_DIAGNOSTIC_PAGEUP 0x01
137 #define MXT_DIAGNOSTIC_DELTAS 0x10
138 #define MXT_DIAGNOSTIC_REFS 0x11
139 #define MXT_DIAGNOSTIC_SIZE 128
140
141 #define MXT_FAMILY_1386 160
142 #define MXT1386_COLUMNS 3
143 #define MXT1386_PAGES_PER_COLUMN 8
144
145 struct t37_debug {
146 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
147 u8 mode;
148 u8 page;
149 u8 data[MXT_DIAGNOSTIC_SIZE];
150 #endif
151 };
152
153 /* Define for MXT_GEN_COMMAND_T6 */
154 #define MXT_BOOT_VALUE 0xa5
155 #define MXT_RESET_VALUE 0x01
156 #define MXT_BACKUP_VALUE 0x55
157
158 /* T100 Multiple Touch Touchscreen */
159 #define MXT_T100_CTRL 0
160 #define MXT_T100_CFG1 1
161 #define MXT_T100_TCHAUX 3
162 #define MXT_T100_XSIZE 9
163 #define MXT_T100_XRANGE 13
164 #define MXT_T100_YSIZE 20
165 #define MXT_T100_YRANGE 24
166
167 #define MXT_T100_CFG_SWITCHXY BIT(5)
168 #define MXT_T100_CFG_INVERTY BIT(6)
169 #define MXT_T100_CFG_INVERTX BIT(7)
170
171 #define MXT_T100_TCHAUX_VECT BIT(0)
172 #define MXT_T100_TCHAUX_AMPL BIT(1)
173 #define MXT_T100_TCHAUX_AREA BIT(2)
174
175 #define MXT_T100_DETECT BIT(7)
176 #define MXT_T100_TYPE_MASK 0x70
177
178 enum t100_type {
179 MXT_T100_TYPE_FINGER = 1,
180 MXT_T100_TYPE_PASSIVE_STYLUS = 2,
181 MXT_T100_TYPE_HOVERING_FINGER = 4,
182 MXT_T100_TYPE_GLOVE = 5,
183 MXT_T100_TYPE_LARGE_TOUCH = 6,
184 };
185
186 #define MXT_DISTANCE_ACTIVE_TOUCH 0
187 #define MXT_DISTANCE_HOVERING 1
188
189 #define MXT_TOUCH_MAJOR_DEFAULT 1
190 #define MXT_PRESSURE_DEFAULT 1
191
192 /* Delay times */
193 #define MXT_BACKUP_TIME 50 /* msec */
194 #define MXT_RESET_TIME 200 /* msec */
195 #define MXT_RESET_TIMEOUT 3000 /* msec */
196 #define MXT_CRC_TIMEOUT 1000 /* msec */
197 #define MXT_FW_RESET_TIME 3000 /* msec */
198 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
199
200 /* Command to unlock bootloader */
201 #define MXT_UNLOCK_CMD_MSB 0xaa
202 #define MXT_UNLOCK_CMD_LSB 0xdc
203
204 /* Bootloader mode status */
205 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
206 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
207 #define MXT_FRAME_CRC_CHECK 0x02
208 #define MXT_FRAME_CRC_FAIL 0x03
209 #define MXT_FRAME_CRC_PASS 0x04
210 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
211 #define MXT_BOOT_STATUS_MASK 0x3f
212 #define MXT_BOOT_EXTENDED_ID (1 << 5)
213 #define MXT_BOOT_ID_MASK 0x1f
214
215 /* Touchscreen absolute values */
216 #define MXT_MAX_AREA 0xff
217
218 #define MXT_PIXELS_PER_MM 20
219
220 struct mxt_info {
221 u8 family_id;
222 u8 variant_id;
223 u8 version;
224 u8 build;
225 u8 matrix_xsize;
226 u8 matrix_ysize;
227 u8 object_num;
228 };
229
230 struct mxt_object {
231 u8 type;
232 u16 start_address;
233 u8 size_minus_one;
234 u8 instances_minus_one;
235 u8 num_report_ids;
236 } __packed;
237
238 struct mxt_dbg {
239 u16 t37_address;
240 u16 diag_cmd_address;
241 struct t37_debug *t37_buf;
242 unsigned int t37_pages;
243 unsigned int t37_nodes;
244
245 struct v4l2_device v4l2;
246 struct v4l2_pix_format format;
247 struct video_device vdev;
248 struct vb2_queue queue;
249 struct mutex lock;
250 int input;
251 };
252
253 enum v4l_dbg_inputs {
254 MXT_V4L_INPUT_DELTAS,
255 MXT_V4L_INPUT_REFS,
256 MXT_V4L_INPUT_MAX,
257 };
258
259 static const struct v4l2_file_operations mxt_video_fops = {
260 .owner = THIS_MODULE,
261 .open = v4l2_fh_open,
262 .release = vb2_fop_release,
263 .unlocked_ioctl = video_ioctl2,
264 .read = vb2_fop_read,
265 .mmap = vb2_fop_mmap,
266 .poll = vb2_fop_poll,
267 };
268
269 /* Each client has this additional data */
270 struct mxt_data {
271 struct i2c_client *client;
272 struct input_dev *input_dev;
273 char phys[64]; /* device physical location */
274 const struct mxt_platform_data *pdata;
275 struct mxt_object *object_table;
276 struct mxt_info info;
277 unsigned int irq;
278 unsigned int max_x;
279 unsigned int max_y;
280 bool invertx;
281 bool inverty;
282 bool xy_switch;
283 u8 xsize;
284 u8 ysize;
285 bool in_bootloader;
286 u16 mem_size;
287 u8 t100_aux_ampl;
288 u8 t100_aux_area;
289 u8 t100_aux_vect;
290 u8 max_reportid;
291 u32 config_crc;
292 u32 info_crc;
293 u8 bootloader_addr;
294 u8 *msg_buf;
295 u8 t6_status;
296 bool update_input;
297 u8 last_message_count;
298 u8 num_touchids;
299 u8 multitouch;
300 struct t7_config t7_cfg;
301 struct mxt_dbg dbg;
302
303 /* Cached parameters from object table */
304 u16 T5_address;
305 u8 T5_msg_size;
306 u8 T6_reportid;
307 u16 T6_address;
308 u16 T7_address;
309 u8 T9_reportid_min;
310 u8 T9_reportid_max;
311 u8 T19_reportid;
312 u16 T44_address;
313 u8 T100_reportid_min;
314 u8 T100_reportid_max;
315
316 /* for fw update in bootloader */
317 struct completion bl_completion;
318
319 /* for reset handling */
320 struct completion reset_completion;
321
322 /* for config update handling */
323 struct completion crc_completion;
324 };
325
326 struct mxt_vb2_buffer {
327 struct vb2_buffer vb;
328 struct list_head list;
329 };
330
331 static size_t mxt_obj_size(const struct mxt_object *obj)
332 {
333 return obj->size_minus_one + 1;
334 }
335
336 static size_t mxt_obj_instances(const struct mxt_object *obj)
337 {
338 return obj->instances_minus_one + 1;
339 }
340
341 static bool mxt_object_readable(unsigned int type)
342 {
343 switch (type) {
344 case MXT_GEN_COMMAND_T6:
345 case MXT_GEN_POWER_T7:
346 case MXT_GEN_ACQUIRE_T8:
347 case MXT_GEN_DATASOURCE_T53:
348 case MXT_TOUCH_MULTI_T9:
349 case MXT_TOUCH_KEYARRAY_T15:
350 case MXT_TOUCH_PROXIMITY_T23:
351 case MXT_TOUCH_PROXKEY_T52:
352 case MXT_PROCI_GRIPFACE_T20:
353 case MXT_PROCG_NOISE_T22:
354 case MXT_PROCI_ONETOUCH_T24:
355 case MXT_PROCI_TWOTOUCH_T27:
356 case MXT_PROCI_GRIP_T40:
357 case MXT_PROCI_PALM_T41:
358 case MXT_PROCI_TOUCHSUPPRESSION_T42:
359 case MXT_PROCI_STYLUS_T47:
360 case MXT_PROCG_NOISESUPPRESSION_T48:
361 case MXT_SPT_COMMSCONFIG_T18:
362 case MXT_SPT_GPIOPWM_T19:
363 case MXT_SPT_SELFTEST_T25:
364 case MXT_SPT_CTECONFIG_T28:
365 case MXT_SPT_USERDATA_T38:
366 case MXT_SPT_DIGITIZER_T43:
367 case MXT_SPT_CTECONFIG_T46:
368 return true;
369 default:
370 return false;
371 }
372 }
373
374 static void mxt_dump_message(struct mxt_data *data, u8 *message)
375 {
376 dev_dbg(&data->client->dev, "message: %*ph\n",
377 data->T5_msg_size, message);
378 }
379
380 static int mxt_wait_for_completion(struct mxt_data *data,
381 struct completion *comp,
382 unsigned int timeout_ms)
383 {
384 struct device *dev = &data->client->dev;
385 unsigned long timeout = msecs_to_jiffies(timeout_ms);
386 long ret;
387
388 ret = wait_for_completion_interruptible_timeout(comp, timeout);
389 if (ret < 0) {
390 return ret;
391 } else if (ret == 0) {
392 dev_err(dev, "Wait for completion timed out.\n");
393 return -ETIMEDOUT;
394 }
395 return 0;
396 }
397
398 static int mxt_bootloader_read(struct mxt_data *data,
399 u8 *val, unsigned int count)
400 {
401 int ret;
402 struct i2c_msg msg;
403
404 msg.addr = data->bootloader_addr;
405 msg.flags = data->client->flags & I2C_M_TEN;
406 msg.flags |= I2C_M_RD;
407 msg.len = count;
408 msg.buf = val;
409
410 ret = i2c_transfer(data->client->adapter, &msg, 1);
411 if (ret == 1) {
412 ret = 0;
413 } else {
414 ret = ret < 0 ? ret : -EIO;
415 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
416 __func__, ret);
417 }
418
419 return ret;
420 }
421
422 static int mxt_bootloader_write(struct mxt_data *data,
423 const u8 * const val, unsigned int count)
424 {
425 int ret;
426 struct i2c_msg msg;
427
428 msg.addr = data->bootloader_addr;
429 msg.flags = data->client->flags & I2C_M_TEN;
430 msg.len = count;
431 msg.buf = (u8 *)val;
432
433 ret = i2c_transfer(data->client->adapter, &msg, 1);
434 if (ret == 1) {
435 ret = 0;
436 } else {
437 ret = ret < 0 ? ret : -EIO;
438 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
439 __func__, ret);
440 }
441
442 return ret;
443 }
444
445 static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
446 {
447 u8 appmode = data->client->addr;
448 u8 bootloader;
449
450 switch (appmode) {
451 case 0x4a:
452 case 0x4b:
453 /* Chips after 1664S use different scheme */
454 if (retry || data->info.family_id >= 0xa2) {
455 bootloader = appmode - 0x24;
456 break;
457 }
458 /* Fall through for normal case */
459 case 0x4c:
460 case 0x4d:
461 case 0x5a:
462 case 0x5b:
463 bootloader = appmode - 0x26;
464 break;
465
466 default:
467 dev_err(&data->client->dev,
468 "Appmode i2c address 0x%02x not found\n",
469 appmode);
470 return -EINVAL;
471 }
472
473 data->bootloader_addr = bootloader;
474 return 0;
475 }
476
477 static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
478 {
479 struct device *dev = &data->client->dev;
480 int error;
481 u8 val;
482 bool crc_failure;
483
484 error = mxt_lookup_bootloader_address(data, alt_address);
485 if (error)
486 return error;
487
488 error = mxt_bootloader_read(data, &val, 1);
489 if (error)
490 return error;
491
492 /* Check app crc fail mode */
493 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
494
495 dev_err(dev, "Detected bootloader, status:%02X%s\n",
496 val, crc_failure ? ", APP_CRC_FAIL" : "");
497
498 return 0;
499 }
500
501 static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
502 {
503 struct device *dev = &data->client->dev;
504 u8 buf[3];
505
506 if (val & MXT_BOOT_EXTENDED_ID) {
507 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
508 dev_err(dev, "%s: i2c failure\n", __func__);
509 return val;
510 }
511
512 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
513
514 return buf[0];
515 } else {
516 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
517
518 return val;
519 }
520 }
521
522 static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
523 bool wait)
524 {
525 struct device *dev = &data->client->dev;
526 u8 val;
527 int ret;
528
529 recheck:
530 if (wait) {
531 /*
532 * In application update mode, the interrupt
533 * line signals state transitions. We must wait for the
534 * CHG assertion before reading the status byte.
535 * Once the status byte has been read, the line is deasserted.
536 */
537 ret = mxt_wait_for_completion(data, &data->bl_completion,
538 MXT_FW_CHG_TIMEOUT);
539 if (ret) {
540 /*
541 * TODO: handle -ERESTARTSYS better by terminating
542 * fw update process before returning to userspace
543 * by writing length 0x000 to device (iff we are in
544 * WAITING_FRAME_DATA state).
545 */
546 dev_err(dev, "Update wait error %d\n", ret);
547 return ret;
548 }
549 }
550
551 ret = mxt_bootloader_read(data, &val, 1);
552 if (ret)
553 return ret;
554
555 if (state == MXT_WAITING_BOOTLOAD_CMD)
556 val = mxt_get_bootloader_version(data, val);
557
558 switch (state) {
559 case MXT_WAITING_BOOTLOAD_CMD:
560 case MXT_WAITING_FRAME_DATA:
561 case MXT_APP_CRC_FAIL:
562 val &= ~MXT_BOOT_STATUS_MASK;
563 break;
564 case MXT_FRAME_CRC_PASS:
565 if (val == MXT_FRAME_CRC_CHECK) {
566 goto recheck;
567 } else if (val == MXT_FRAME_CRC_FAIL) {
568 dev_err(dev, "Bootloader CRC fail\n");
569 return -EINVAL;
570 }
571 break;
572 default:
573 return -EINVAL;
574 }
575
576 if (val != state) {
577 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
578 val, state);
579 return -EINVAL;
580 }
581
582 return 0;
583 }
584
585 static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
586 {
587 int ret;
588 u8 buf[2];
589
590 if (unlock) {
591 buf[0] = MXT_UNLOCK_CMD_LSB;
592 buf[1] = MXT_UNLOCK_CMD_MSB;
593 } else {
594 buf[0] = 0x01;
595 buf[1] = 0x01;
596 }
597
598 ret = mxt_bootloader_write(data, buf, 2);
599 if (ret)
600 return ret;
601
602 return 0;
603 }
604
605 static int __mxt_read_reg(struct i2c_client *client,
606 u16 reg, u16 len, void *val)
607 {
608 struct i2c_msg xfer[2];
609 u8 buf[2];
610 int ret;
611
612 buf[0] = reg & 0xff;
613 buf[1] = (reg >> 8) & 0xff;
614
615 /* Write register */
616 xfer[0].addr = client->addr;
617 xfer[0].flags = 0;
618 xfer[0].len = 2;
619 xfer[0].buf = buf;
620
621 /* Read data */
622 xfer[1].addr = client->addr;
623 xfer[1].flags = I2C_M_RD;
624 xfer[1].len = len;
625 xfer[1].buf = val;
626
627 ret = i2c_transfer(client->adapter, xfer, 2);
628 if (ret == 2) {
629 ret = 0;
630 } else {
631 if (ret >= 0)
632 ret = -EIO;
633 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
634 __func__, ret);
635 }
636
637 return ret;
638 }
639
640 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
641 const void *val)
642 {
643 u8 *buf;
644 size_t count;
645 int ret;
646
647 count = len + 2;
648 buf = kmalloc(count, GFP_KERNEL);
649 if (!buf)
650 return -ENOMEM;
651
652 buf[0] = reg & 0xff;
653 buf[1] = (reg >> 8) & 0xff;
654 memcpy(&buf[2], val, len);
655
656 ret = i2c_master_send(client, buf, count);
657 if (ret == count) {
658 ret = 0;
659 } else {
660 if (ret >= 0)
661 ret = -EIO;
662 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
663 __func__, ret);
664 }
665
666 kfree(buf);
667 return ret;
668 }
669
670 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
671 {
672 return __mxt_write_reg(client, reg, 1, &val);
673 }
674
675 static struct mxt_object *
676 mxt_get_object(struct mxt_data *data, u8 type)
677 {
678 struct mxt_object *object;
679 int i;
680
681 for (i = 0; i < data->info.object_num; i++) {
682 object = data->object_table + i;
683 if (object->type == type)
684 return object;
685 }
686
687 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
688 return NULL;
689 }
690
691 static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
692 {
693 struct device *dev = &data->client->dev;
694 u8 status = msg[1];
695 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
696
697 complete(&data->crc_completion);
698
699 if (crc != data->config_crc) {
700 data->config_crc = crc;
701 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
702 }
703
704 /* Detect reset */
705 if (status & MXT_T6_STATUS_RESET)
706 complete(&data->reset_completion);
707
708 /* Output debug if status has changed */
709 if (status != data->t6_status)
710 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
711 status,
712 status == 0 ? " OK" : "",
713 status & MXT_T6_STATUS_RESET ? " RESET" : "",
714 status & MXT_T6_STATUS_OFL ? " OFL" : "",
715 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
716 status & MXT_T6_STATUS_CAL ? " CAL" : "",
717 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
718 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
719
720 /* Save current status */
721 data->t6_status = status;
722 }
723
724 static int mxt_write_object(struct mxt_data *data,
725 u8 type, u8 offset, u8 val)
726 {
727 struct mxt_object *object;
728 u16 reg;
729
730 object = mxt_get_object(data, type);
731 if (!object || offset >= mxt_obj_size(object))
732 return -EINVAL;
733
734 reg = object->start_address;
735 return mxt_write_reg(data->client, reg + offset, val);
736 }
737
738 static void mxt_input_button(struct mxt_data *data, u8 *message)
739 {
740 struct input_dev *input = data->input_dev;
741 const struct mxt_platform_data *pdata = data->pdata;
742 int i;
743
744 for (i = 0; i < pdata->t19_num_keys; i++) {
745 if (pdata->t19_keymap[i] == KEY_RESERVED)
746 continue;
747
748 /* Active-low switch */
749 input_report_key(input, pdata->t19_keymap[i],
750 !(message[1] & BIT(i)));
751 }
752 }
753
754 static void mxt_input_sync(struct mxt_data *data)
755 {
756 input_mt_report_pointer_emulation(data->input_dev,
757 data->pdata->t19_num_keys);
758 input_sync(data->input_dev);
759 }
760
761 static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
762 {
763 struct device *dev = &data->client->dev;
764 struct input_dev *input_dev = data->input_dev;
765 int id;
766 u8 status;
767 int x;
768 int y;
769 int area;
770 int amplitude;
771
772 id = message[0] - data->T9_reportid_min;
773 status = message[1];
774 x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
775 y = (message[3] << 4) | ((message[4] & 0xf));
776
777 /* Handle 10/12 bit switching */
778 if (data->max_x < 1024)
779 x >>= 2;
780 if (data->max_y < 1024)
781 y >>= 2;
782
783 area = message[5];
784 amplitude = message[6];
785
786 dev_dbg(dev,
787 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
788 id,
789 (status & MXT_T9_DETECT) ? 'D' : '.',
790 (status & MXT_T9_PRESS) ? 'P' : '.',
791 (status & MXT_T9_RELEASE) ? 'R' : '.',
792 (status & MXT_T9_MOVE) ? 'M' : '.',
793 (status & MXT_T9_VECTOR) ? 'V' : '.',
794 (status & MXT_T9_AMP) ? 'A' : '.',
795 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
796 (status & MXT_T9_UNGRIP) ? 'U' : '.',
797 x, y, area, amplitude);
798
799 input_mt_slot(input_dev, id);
800
801 if (status & MXT_T9_DETECT) {
802 /*
803 * Multiple bits may be set if the host is slow to read
804 * the status messages, indicating all the events that
805 * have happened.
806 */
807 if (status & MXT_T9_RELEASE) {
808 input_mt_report_slot_state(input_dev,
809 MT_TOOL_FINGER, 0);
810 mxt_input_sync(data);
811 }
812
813 /* Touch active */
814 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
815 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
816 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
817 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
818 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
819 } else {
820 /* Touch no longer active, close out slot */
821 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
822 }
823
824 data->update_input = true;
825 }
826
827 static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
828 {
829 struct device *dev = &data->client->dev;
830 struct input_dev *input_dev = data->input_dev;
831 int id;
832 u8 status;
833 u8 type = 0;
834 u16 x;
835 u16 y;
836 int distance = 0;
837 int tool = 0;
838 u8 major = 0;
839 u8 pressure = 0;
840 u8 orientation = 0;
841
842 id = message[0] - data->T100_reportid_min - 2;
843
844 /* ignore SCRSTATUS events */
845 if (id < 0)
846 return;
847
848 status = message[1];
849 x = get_unaligned_le16(&message[2]);
850 y = get_unaligned_le16(&message[4]);
851
852 if (status & MXT_T100_DETECT) {
853 type = (status & MXT_T100_TYPE_MASK) >> 4;
854
855 switch (type) {
856 case MXT_T100_TYPE_HOVERING_FINGER:
857 tool = MT_TOOL_FINGER;
858 distance = MXT_DISTANCE_HOVERING;
859
860 if (data->t100_aux_vect)
861 orientation = message[data->t100_aux_vect];
862
863 break;
864
865 case MXT_T100_TYPE_FINGER:
866 case MXT_T100_TYPE_GLOVE:
867 tool = MT_TOOL_FINGER;
868 distance = MXT_DISTANCE_ACTIVE_TOUCH;
869
870 if (data->t100_aux_area)
871 major = message[data->t100_aux_area];
872
873 if (data->t100_aux_ampl)
874 pressure = message[data->t100_aux_ampl];
875
876 if (data->t100_aux_vect)
877 orientation = message[data->t100_aux_vect];
878
879 break;
880
881 case MXT_T100_TYPE_PASSIVE_STYLUS:
882 tool = MT_TOOL_PEN;
883
884 /*
885 * Passive stylus is reported with size zero so
886 * hardcode.
887 */
888 major = MXT_TOUCH_MAJOR_DEFAULT;
889
890 if (data->t100_aux_ampl)
891 pressure = message[data->t100_aux_ampl];
892
893 break;
894
895 case MXT_T100_TYPE_LARGE_TOUCH:
896 /* Ignore suppressed touch */
897 break;
898
899 default:
900 dev_dbg(dev, "Unexpected T100 type\n");
901 return;
902 }
903 }
904
905 /*
906 * Values reported should be non-zero if tool is touching the
907 * device
908 */
909 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
910 pressure = MXT_PRESSURE_DEFAULT;
911
912 input_mt_slot(input_dev, id);
913
914 if (status & MXT_T100_DETECT) {
915 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
916 id, type, x, y, major, pressure, orientation);
917
918 input_mt_report_slot_state(input_dev, tool, 1);
919 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
920 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
921 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
922 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
923 input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
924 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
925 } else {
926 dev_dbg(dev, "[%u] release\n", id);
927
928 /* close out slot */
929 input_mt_report_slot_state(input_dev, 0, 0);
930 }
931
932 data->update_input = true;
933 }
934
935 static int mxt_proc_message(struct mxt_data *data, u8 *message)
936 {
937 u8 report_id = message[0];
938
939 if (report_id == MXT_RPTID_NOMSG)
940 return 0;
941
942 if (report_id == data->T6_reportid) {
943 mxt_proc_t6_messages(data, message);
944 } else if (!data->input_dev) {
945 /*
946 * Do not report events if input device
947 * is not yet registered.
948 */
949 mxt_dump_message(data, message);
950 } else if (report_id >= data->T9_reportid_min &&
951 report_id <= data->T9_reportid_max) {
952 mxt_proc_t9_message(data, message);
953 } else if (report_id >= data->T100_reportid_min &&
954 report_id <= data->T100_reportid_max) {
955 mxt_proc_t100_message(data, message);
956 } else if (report_id == data->T19_reportid) {
957 mxt_input_button(data, message);
958 data->update_input = true;
959 } else {
960 mxt_dump_message(data, message);
961 }
962
963 return 1;
964 }
965
966 static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
967 {
968 struct device *dev = &data->client->dev;
969 int ret;
970 int i;
971 u8 num_valid = 0;
972
973 /* Safety check for msg_buf */
974 if (count > data->max_reportid)
975 return -EINVAL;
976
977 /* Process remaining messages if necessary */
978 ret = __mxt_read_reg(data->client, data->T5_address,
979 data->T5_msg_size * count, data->msg_buf);
980 if (ret) {
981 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
982 return ret;
983 }
984
985 for (i = 0; i < count; i++) {
986 ret = mxt_proc_message(data,
987 data->msg_buf + data->T5_msg_size * i);
988
989 if (ret == 1)
990 num_valid++;
991 }
992
993 /* return number of messages read */
994 return num_valid;
995 }
996
997 static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
998 {
999 struct device *dev = &data->client->dev;
1000 int ret;
1001 u8 count, num_left;
1002
1003 /* Read T44 and T5 together */
1004 ret = __mxt_read_reg(data->client, data->T44_address,
1005 data->T5_msg_size + 1, data->msg_buf);
1006 if (ret) {
1007 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
1008 return IRQ_NONE;
1009 }
1010
1011 count = data->msg_buf[0];
1012
1013 /*
1014 * This condition may be caused by the CHG line being configured in
1015 * Mode 0. It results in unnecessary I2C operations but it is benign.
1016 */
1017 if (count == 0)
1018 return IRQ_NONE;
1019
1020 if (count > data->max_reportid) {
1021 dev_warn(dev, "T44 count %d exceeded max report id\n", count);
1022 count = data->max_reportid;
1023 }
1024
1025 /* Process first message */
1026 ret = mxt_proc_message(data, data->msg_buf + 1);
1027 if (ret < 0) {
1028 dev_warn(dev, "Unexpected invalid message\n");
1029 return IRQ_NONE;
1030 }
1031
1032 num_left = count - 1;
1033
1034 /* Process remaining messages if necessary */
1035 if (num_left) {
1036 ret = mxt_read_and_process_messages(data, num_left);
1037 if (ret < 0)
1038 goto end;
1039 else if (ret != num_left)
1040 dev_warn(dev, "Unexpected invalid message\n");
1041 }
1042
1043 end:
1044 if (data->update_input) {
1045 mxt_input_sync(data);
1046 data->update_input = false;
1047 }
1048
1049 return IRQ_HANDLED;
1050 }
1051
1052 static int mxt_process_messages_until_invalid(struct mxt_data *data)
1053 {
1054 struct device *dev = &data->client->dev;
1055 int count, read;
1056 u8 tries = 2;
1057
1058 count = data->max_reportid;
1059
1060 /* Read messages until we force an invalid */
1061 do {
1062 read = mxt_read_and_process_messages(data, count);
1063 if (read < count)
1064 return 0;
1065 } while (--tries);
1066
1067 if (data->update_input) {
1068 mxt_input_sync(data);
1069 data->update_input = false;
1070 }
1071
1072 dev_err(dev, "CHG pin isn't cleared\n");
1073 return -EBUSY;
1074 }
1075
1076 static irqreturn_t mxt_process_messages(struct mxt_data *data)
1077 {
1078 int total_handled, num_handled;
1079 u8 count = data->last_message_count;
1080
1081 if (count < 1 || count > data->max_reportid)
1082 count = 1;
1083
1084 /* include final invalid message */
1085 total_handled = mxt_read_and_process_messages(data, count + 1);
1086 if (total_handled < 0)
1087 return IRQ_NONE;
1088 /* if there were invalid messages, then we are done */
1089 else if (total_handled <= count)
1090 goto update_count;
1091
1092 /* keep reading two msgs until one is invalid or reportid limit */
1093 do {
1094 num_handled = mxt_read_and_process_messages(data, 2);
1095 if (num_handled < 0)
1096 return IRQ_NONE;
1097
1098 total_handled += num_handled;
1099
1100 if (num_handled < 2)
1101 break;
1102 } while (total_handled < data->num_touchids);
1103
1104 update_count:
1105 data->last_message_count = total_handled;
1106
1107 if (data->update_input) {
1108 mxt_input_sync(data);
1109 data->update_input = false;
1110 }
1111
1112 return IRQ_HANDLED;
1113 }
1114
1115 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
1116 {
1117 struct mxt_data *data = dev_id;
1118
1119 if (data->in_bootloader) {
1120 /* bootloader state transition completion */
1121 complete(&data->bl_completion);
1122 return IRQ_HANDLED;
1123 }
1124
1125 if (!data->object_table)
1126 return IRQ_HANDLED;
1127
1128 if (data->T44_address) {
1129 return mxt_process_messages_t44(data);
1130 } else {
1131 return mxt_process_messages(data);
1132 }
1133 }
1134
1135 static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
1136 u8 value, bool wait)
1137 {
1138 u16 reg;
1139 u8 command_register;
1140 int timeout_counter = 0;
1141 int ret;
1142
1143 reg = data->T6_address + cmd_offset;
1144
1145 ret = mxt_write_reg(data->client, reg, value);
1146 if (ret)
1147 return ret;
1148
1149 if (!wait)
1150 return 0;
1151
1152 do {
1153 msleep(20);
1154 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
1155 if (ret)
1156 return ret;
1157 } while (command_register != 0 && timeout_counter++ <= 100);
1158
1159 if (timeout_counter > 100) {
1160 dev_err(&data->client->dev, "Command failed!\n");
1161 return -EIO;
1162 }
1163
1164 return 0;
1165 }
1166
1167 static int mxt_acquire_irq(struct mxt_data *data)
1168 {
1169 int error;
1170
1171 enable_irq(data->irq);
1172
1173 error = mxt_process_messages_until_invalid(data);
1174 if (error)
1175 return error;
1176
1177 return 0;
1178 }
1179
1180 static int mxt_soft_reset(struct mxt_data *data)
1181 {
1182 struct device *dev = &data->client->dev;
1183 int ret = 0;
1184
1185 dev_info(dev, "Resetting device\n");
1186
1187 disable_irq(data->irq);
1188
1189 reinit_completion(&data->reset_completion);
1190
1191 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
1192 if (ret)
1193 return ret;
1194
1195 /* Ignore CHG line for 100ms after reset */
1196 msleep(100);
1197
1198 mxt_acquire_irq(data);
1199
1200 ret = mxt_wait_for_completion(data, &data->reset_completion,
1201 MXT_RESET_TIMEOUT);
1202 if (ret)
1203 return ret;
1204
1205 return 0;
1206 }
1207
1208 static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
1209 {
1210 /*
1211 * On failure, CRC is set to 0 and config will always be
1212 * downloaded.
1213 */
1214 data->config_crc = 0;
1215 reinit_completion(&data->crc_completion);
1216
1217 mxt_t6_command(data, cmd, value, true);
1218
1219 /*
1220 * Wait for crc message. On failure, CRC is set to 0 and config will
1221 * always be downloaded.
1222 */
1223 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
1224 }
1225
1226 static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
1227 {
1228 static const unsigned int crcpoly = 0x80001B;
1229 u32 result;
1230 u32 data_word;
1231
1232 data_word = (secondbyte << 8) | firstbyte;
1233 result = ((*crc << 1) ^ data_word);
1234
1235 if (result & 0x1000000)
1236 result ^= crcpoly;
1237
1238 *crc = result;
1239 }
1240
1241 static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
1242 {
1243 u32 crc = 0;
1244 u8 *ptr = base + start_off;
1245 u8 *last_val = base + end_off - 1;
1246
1247 if (end_off < start_off)
1248 return -EINVAL;
1249
1250 while (ptr < last_val) {
1251 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
1252 ptr += 2;
1253 }
1254
1255 /* if len is odd, fill the last byte with 0 */
1256 if (ptr == last_val)
1257 mxt_calc_crc24(&crc, *ptr, 0);
1258
1259 /* Mask to 24-bit */
1260 crc &= 0x00FFFFFF;
1261
1262 return crc;
1263 }
1264
1265 static int mxt_prepare_cfg_mem(struct mxt_data *data,
1266 const struct firmware *cfg,
1267 unsigned int data_pos,
1268 unsigned int cfg_start_ofs,
1269 u8 *config_mem,
1270 size_t config_mem_size)
1271 {
1272 struct device *dev = &data->client->dev;
1273 struct mxt_object *object;
1274 unsigned int type, instance, size, byte_offset;
1275 int offset;
1276 int ret;
1277 int i;
1278 u16 reg;
1279 u8 val;
1280
1281 while (data_pos < cfg->size) {
1282 /* Read type, instance, length */
1283 ret = sscanf(cfg->data + data_pos, "%x %x %x%n",
1284 &type, &instance, &size, &offset);
1285 if (ret == 0) {
1286 /* EOF */
1287 break;
1288 } else if (ret != 3) {
1289 dev_err(dev, "Bad format: failed to parse object\n");
1290 return -EINVAL;
1291 }
1292 data_pos += offset;
1293
1294 object = mxt_get_object(data, type);
1295 if (!object) {
1296 /* Skip object */
1297 for (i = 0; i < size; i++) {
1298 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1299 &val, &offset);
1300 if (ret != 1) {
1301 dev_err(dev, "Bad format in T%d at %d\n",
1302 type, i);
1303 return -EINVAL;
1304 }
1305 data_pos += offset;
1306 }
1307 continue;
1308 }
1309
1310 if (size > mxt_obj_size(object)) {
1311 /*
1312 * Either we are in fallback mode due to wrong
1313 * config or config from a later fw version,
1314 * or the file is corrupt or hand-edited.
1315 */
1316 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1317 size - mxt_obj_size(object), type);
1318 } else if (mxt_obj_size(object) > size) {
1319 /*
1320 * If firmware is upgraded, new bytes may be added to
1321 * end of objects. It is generally forward compatible
1322 * to zero these bytes - previous behaviour will be
1323 * retained. However this does invalidate the CRC and
1324 * will force fallback mode until the configuration is
1325 * updated. We warn here but do nothing else - the
1326 * malloc has zeroed the entire configuration.
1327 */
1328 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1329 mxt_obj_size(object) - size, type);
1330 }
1331
1332 if (instance >= mxt_obj_instances(object)) {
1333 dev_err(dev, "Object instances exceeded!\n");
1334 return -EINVAL;
1335 }
1336
1337 reg = object->start_address + mxt_obj_size(object) * instance;
1338
1339 for (i = 0; i < size; i++) {
1340 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1341 &val,
1342 &offset);
1343 if (ret != 1) {
1344 dev_err(dev, "Bad format in T%d at %d\n",
1345 type, i);
1346 return -EINVAL;
1347 }
1348 data_pos += offset;
1349
1350 if (i > mxt_obj_size(object))
1351 continue;
1352
1353 byte_offset = reg + i - cfg_start_ofs;
1354
1355 if (byte_offset >= 0 && byte_offset < config_mem_size) {
1356 *(config_mem + byte_offset) = val;
1357 } else {
1358 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1359 reg, object->type, byte_offset);
1360 return -EINVAL;
1361 }
1362 }
1363 }
1364
1365 return 0;
1366 }
1367
1368 static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start,
1369 u8 *config_mem, size_t config_mem_size)
1370 {
1371 unsigned int byte_offset = 0;
1372 int error;
1373
1374 /* Write configuration as blocks */
1375 while (byte_offset < config_mem_size) {
1376 unsigned int size = config_mem_size - byte_offset;
1377
1378 if (size > MXT_MAX_BLOCK_WRITE)
1379 size = MXT_MAX_BLOCK_WRITE;
1380
1381 error = __mxt_write_reg(data->client,
1382 cfg_start + byte_offset,
1383 size, config_mem + byte_offset);
1384 if (error) {
1385 dev_err(&data->client->dev,
1386 "Config write error, ret=%d\n", error);
1387 return error;
1388 }
1389
1390 byte_offset += size;
1391 }
1392
1393 return 0;
1394 }
1395
1396 static int mxt_init_t7_power_cfg(struct mxt_data *data);
1397
1398 /*
1399 * mxt_update_cfg - download configuration to chip
1400 *
1401 * Atmel Raw Config File Format
1402 *
1403 * The first four lines of the raw config file contain:
1404 * 1) Version
1405 * 2) Chip ID Information (first 7 bytes of device memory)
1406 * 3) Chip Information Block 24-bit CRC Checksum
1407 * 4) Chip Configuration 24-bit CRC Checksum
1408 *
1409 * The rest of the file consists of one line per object instance:
1410 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1411 *
1412 * <TYPE> - 2-byte object type as hex
1413 * <INSTANCE> - 2-byte object instance number as hex
1414 * <SIZE> - 2-byte object size as hex
1415 * <CONTENTS> - array of <SIZE> 1-byte hex values
1416 */
1417 static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg)
1418 {
1419 struct device *dev = &data->client->dev;
1420 struct mxt_info cfg_info;
1421 int ret;
1422 int offset;
1423 int data_pos;
1424 int i;
1425 int cfg_start_ofs;
1426 u32 info_crc, config_crc, calculated_crc;
1427 u8 *config_mem;
1428 size_t config_mem_size;
1429
1430 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
1431
1432 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
1433 dev_err(dev, "Unrecognised config file\n");
1434 return -EINVAL;
1435 }
1436
1437 data_pos = strlen(MXT_CFG_MAGIC);
1438
1439 /* Load information block and check */
1440 for (i = 0; i < sizeof(struct mxt_info); i++) {
1441 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1442 (unsigned char *)&cfg_info + i,
1443 &offset);
1444 if (ret != 1) {
1445 dev_err(dev, "Bad format\n");
1446 return -EINVAL;
1447 }
1448
1449 data_pos += offset;
1450 }
1451
1452 if (cfg_info.family_id != data->info.family_id) {
1453 dev_err(dev, "Family ID mismatch!\n");
1454 return -EINVAL;
1455 }
1456
1457 if (cfg_info.variant_id != data->info.variant_id) {
1458 dev_err(dev, "Variant ID mismatch!\n");
1459 return -EINVAL;
1460 }
1461
1462 /* Read CRCs */
1463 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset);
1464 if (ret != 1) {
1465 dev_err(dev, "Bad format: failed to parse Info CRC\n");
1466 return -EINVAL;
1467 }
1468 data_pos += offset;
1469
1470 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset);
1471 if (ret != 1) {
1472 dev_err(dev, "Bad format: failed to parse Config CRC\n");
1473 return -EINVAL;
1474 }
1475 data_pos += offset;
1476
1477 /*
1478 * The Info Block CRC is calculated over mxt_info and the object
1479 * table. If it does not match then we are trying to load the
1480 * configuration from a different chip or firmware version, so
1481 * the configuration CRC is invalid anyway.
1482 */
1483 if (info_crc == data->info_crc) {
1484 if (config_crc == 0 || data->config_crc == 0) {
1485 dev_info(dev, "CRC zero, attempting to apply config\n");
1486 } else if (config_crc == data->config_crc) {
1487 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
1488 data->config_crc);
1489 return 0;
1490 } else {
1491 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1492 data->config_crc, config_crc);
1493 }
1494 } else {
1495 dev_warn(dev,
1496 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1497 data->info_crc, info_crc);
1498 }
1499
1500 /* Malloc memory to store configuration */
1501 cfg_start_ofs = MXT_OBJECT_START +
1502 data->info.object_num * sizeof(struct mxt_object) +
1503 MXT_INFO_CHECKSUM_SIZE;
1504 config_mem_size = data->mem_size - cfg_start_ofs;
1505 config_mem = kzalloc(config_mem_size, GFP_KERNEL);
1506 if (!config_mem) {
1507 dev_err(dev, "Failed to allocate memory\n");
1508 return -ENOMEM;
1509 }
1510
1511 ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs,
1512 config_mem, config_mem_size);
1513 if (ret)
1514 goto release_mem;
1515
1516 /* Calculate crc of the received configs (not the raw config file) */
1517 if (data->T7_address < cfg_start_ofs) {
1518 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n",
1519 data->T7_address, cfg_start_ofs);
1520 ret = 0;
1521 goto release_mem;
1522 }
1523
1524 calculated_crc = mxt_calculate_crc(config_mem,
1525 data->T7_address - cfg_start_ofs,
1526 config_mem_size);
1527
1528 if (config_crc > 0 && config_crc != calculated_crc)
1529 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n",
1530 calculated_crc, config_crc);
1531
1532 ret = mxt_upload_cfg_mem(data, cfg_start_ofs,
1533 config_mem, config_mem_size);
1534 if (ret)
1535 goto release_mem;
1536
1537 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1538
1539 ret = mxt_soft_reset(data);
1540 if (ret)
1541 goto release_mem;
1542
1543 dev_info(dev, "Config successfully updated\n");
1544
1545 /* T7 config may have changed */
1546 mxt_init_t7_power_cfg(data);
1547
1548 release_mem:
1549 kfree(config_mem);
1550 return ret;
1551 }
1552
1553 static int mxt_get_info(struct mxt_data *data)
1554 {
1555 struct i2c_client *client = data->client;
1556 struct mxt_info *info = &data->info;
1557 int error;
1558
1559 /* Read 7-byte info block starting at address 0 */
1560 error = __mxt_read_reg(client, 0, sizeof(*info), info);
1561 if (error)
1562 return error;
1563
1564 return 0;
1565 }
1566
1567 static void mxt_free_input_device(struct mxt_data *data)
1568 {
1569 if (data->input_dev) {
1570 input_unregister_device(data->input_dev);
1571 data->input_dev = NULL;
1572 }
1573 }
1574
1575 static void mxt_free_object_table(struct mxt_data *data)
1576 {
1577 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1578 video_unregister_device(&data->dbg.vdev);
1579 v4l2_device_unregister(&data->dbg.v4l2);
1580 #endif
1581
1582 kfree(data->object_table);
1583 data->object_table = NULL;
1584 kfree(data->msg_buf);
1585 data->msg_buf = NULL;
1586 data->T5_address = 0;
1587 data->T5_msg_size = 0;
1588 data->T6_reportid = 0;
1589 data->T7_address = 0;
1590 data->T9_reportid_min = 0;
1591 data->T9_reportid_max = 0;
1592 data->T19_reportid = 0;
1593 data->T44_address = 0;
1594 data->T100_reportid_min = 0;
1595 data->T100_reportid_max = 0;
1596 data->max_reportid = 0;
1597 }
1598
1599 static int mxt_get_object_table(struct mxt_data *data)
1600 {
1601 struct i2c_client *client = data->client;
1602 size_t table_size;
1603 struct mxt_object *object_table;
1604 int error;
1605 int i;
1606 u8 reportid;
1607 u16 end_address;
1608
1609 table_size = data->info.object_num * sizeof(struct mxt_object);
1610 object_table = kzalloc(table_size, GFP_KERNEL);
1611 if (!object_table) {
1612 dev_err(&data->client->dev, "Failed to allocate memory\n");
1613 return -ENOMEM;
1614 }
1615
1616 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
1617 object_table);
1618 if (error) {
1619 kfree(object_table);
1620 return error;
1621 }
1622
1623 /* Valid Report IDs start counting from 1 */
1624 reportid = 1;
1625 data->mem_size = 0;
1626 for (i = 0; i < data->info.object_num; i++) {
1627 struct mxt_object *object = object_table + i;
1628 u8 min_id, max_id;
1629
1630 le16_to_cpus(&object->start_address);
1631
1632 if (object->num_report_ids) {
1633 min_id = reportid;
1634 reportid += object->num_report_ids *
1635 mxt_obj_instances(object);
1636 max_id = reportid - 1;
1637 } else {
1638 min_id = 0;
1639 max_id = 0;
1640 }
1641
1642 dev_dbg(&data->client->dev,
1643 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
1644 object->type, object->start_address,
1645 mxt_obj_size(object), mxt_obj_instances(object),
1646 min_id, max_id);
1647
1648 switch (object->type) {
1649 case MXT_GEN_MESSAGE_T5:
1650 if (data->info.family_id == 0x80 &&
1651 data->info.version < 0x20) {
1652 /*
1653 * On mXT224 firmware versions prior to V2.0
1654 * read and discard unused CRC byte otherwise
1655 * DMA reads are misaligned.
1656 */
1657 data->T5_msg_size = mxt_obj_size(object);
1658 } else {
1659 /* CRC not enabled, so skip last byte */
1660 data->T5_msg_size = mxt_obj_size(object) - 1;
1661 }
1662 data->T5_address = object->start_address;
1663 break;
1664 case MXT_GEN_COMMAND_T6:
1665 data->T6_reportid = min_id;
1666 data->T6_address = object->start_address;
1667 break;
1668 case MXT_GEN_POWER_T7:
1669 data->T7_address = object->start_address;
1670 break;
1671 case MXT_TOUCH_MULTI_T9:
1672 data->multitouch = MXT_TOUCH_MULTI_T9;
1673 data->T9_reportid_min = min_id;
1674 data->T9_reportid_max = max_id;
1675 data->num_touchids = object->num_report_ids
1676 * mxt_obj_instances(object);
1677 break;
1678 case MXT_SPT_MESSAGECOUNT_T44:
1679 data->T44_address = object->start_address;
1680 break;
1681 case MXT_SPT_GPIOPWM_T19:
1682 data->T19_reportid = min_id;
1683 break;
1684 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1685 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
1686 data->T100_reportid_min = min_id;
1687 data->T100_reportid_max = max_id;
1688 /* first two report IDs reserved */
1689 data->num_touchids = object->num_report_ids - 2;
1690 break;
1691 }
1692
1693 end_address = object->start_address
1694 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1695
1696 if (end_address >= data->mem_size)
1697 data->mem_size = end_address + 1;
1698 }
1699
1700 /* Store maximum reportid */
1701 data->max_reportid = reportid;
1702
1703 /* If T44 exists, T5 position has to be directly after */
1704 if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
1705 dev_err(&client->dev, "Invalid T44 position\n");
1706 error = -EINVAL;
1707 goto free_object_table;
1708 }
1709
1710 data->msg_buf = kcalloc(data->max_reportid,
1711 data->T5_msg_size, GFP_KERNEL);
1712 if (!data->msg_buf) {
1713 dev_err(&client->dev, "Failed to allocate message buffer\n");
1714 error = -ENOMEM;
1715 goto free_object_table;
1716 }
1717
1718 data->object_table = object_table;
1719
1720 return 0;
1721
1722 free_object_table:
1723 mxt_free_object_table(data);
1724 return error;
1725 }
1726
1727 static int mxt_read_t9_resolution(struct mxt_data *data)
1728 {
1729 struct i2c_client *client = data->client;
1730 int error;
1731 struct t9_range range;
1732 unsigned char orient;
1733 struct mxt_object *object;
1734
1735 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1736 if (!object)
1737 return -EINVAL;
1738
1739 error = __mxt_read_reg(client,
1740 object->start_address + MXT_T9_XSIZE,
1741 sizeof(data->xsize), &data->xsize);
1742 if (error)
1743 return error;
1744
1745 error = __mxt_read_reg(client,
1746 object->start_address + MXT_T9_YSIZE,
1747 sizeof(data->ysize), &data->ysize);
1748 if (error)
1749 return error;
1750
1751 error = __mxt_read_reg(client,
1752 object->start_address + MXT_T9_RANGE,
1753 sizeof(range), &range);
1754 if (error)
1755 return error;
1756
1757 data->max_x = get_unaligned_le16(&range.x);
1758 data->max_y = get_unaligned_le16(&range.y);
1759
1760 error = __mxt_read_reg(client,
1761 object->start_address + MXT_T9_ORIENT,
1762 1, &orient);
1763 if (error)
1764 return error;
1765
1766 data->xy_switch = orient & MXT_T9_ORIENT_SWITCH;
1767 data->invertx = orient & MXT_T9_ORIENT_INVERTX;
1768 data->inverty = orient & MXT_T9_ORIENT_INVERTY;
1769
1770 return 0;
1771 }
1772
1773 static int mxt_read_t100_config(struct mxt_data *data)
1774 {
1775 struct i2c_client *client = data->client;
1776 int error;
1777 struct mxt_object *object;
1778 u16 range_x, range_y;
1779 u8 cfg, tchaux;
1780 u8 aux;
1781
1782 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
1783 if (!object)
1784 return -EINVAL;
1785
1786 /* read touchscreen dimensions */
1787 error = __mxt_read_reg(client,
1788 object->start_address + MXT_T100_XRANGE,
1789 sizeof(range_x), &range_x);
1790 if (error)
1791 return error;
1792
1793 data->max_x = get_unaligned_le16(&range_x);
1794
1795 error = __mxt_read_reg(client,
1796 object->start_address + MXT_T100_YRANGE,
1797 sizeof(range_y), &range_y);
1798 if (error)
1799 return error;
1800
1801 data->max_y = get_unaligned_le16(&range_y);
1802
1803 error = __mxt_read_reg(client,
1804 object->start_address + MXT_T100_XSIZE,
1805 sizeof(data->xsize), &data->xsize);
1806 if (error)
1807 return error;
1808
1809 error = __mxt_read_reg(client,
1810 object->start_address + MXT_T100_YSIZE,
1811 sizeof(data->ysize), &data->ysize);
1812 if (error)
1813 return error;
1814
1815 /* read orientation config */
1816 error = __mxt_read_reg(client,
1817 object->start_address + MXT_T100_CFG1,
1818 1, &cfg);
1819 if (error)
1820 return error;
1821
1822 data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY;
1823 data->invertx = cfg & MXT_T100_CFG_INVERTX;
1824 data->inverty = cfg & MXT_T100_CFG_INVERTY;
1825
1826 /* allocate aux bytes */
1827 error = __mxt_read_reg(client,
1828 object->start_address + MXT_T100_TCHAUX,
1829 1, &tchaux);
1830 if (error)
1831 return error;
1832
1833 aux = 6;
1834
1835 if (tchaux & MXT_T100_TCHAUX_VECT)
1836 data->t100_aux_vect = aux++;
1837
1838 if (tchaux & MXT_T100_TCHAUX_AMPL)
1839 data->t100_aux_ampl = aux++;
1840
1841 if (tchaux & MXT_T100_TCHAUX_AREA)
1842 data->t100_aux_area = aux++;
1843
1844 dev_dbg(&client->dev,
1845 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1846 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
1847
1848 return 0;
1849 }
1850
1851 static int mxt_input_open(struct input_dev *dev);
1852 static void mxt_input_close(struct input_dev *dev);
1853
1854 static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
1855 struct mxt_data *data)
1856 {
1857 const struct mxt_platform_data *pdata = data->pdata;
1858 int i;
1859
1860 input_dev->name = "Atmel maXTouch Touchpad";
1861
1862 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1863
1864 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1865 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1866 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1867 MXT_PIXELS_PER_MM);
1868 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1869 MXT_PIXELS_PER_MM);
1870
1871 for (i = 0; i < pdata->t19_num_keys; i++)
1872 if (pdata->t19_keymap[i] != KEY_RESERVED)
1873 input_set_capability(input_dev, EV_KEY,
1874 pdata->t19_keymap[i]);
1875 }
1876
1877 static int mxt_initialize_input_device(struct mxt_data *data)
1878 {
1879 const struct mxt_platform_data *pdata = data->pdata;
1880 struct device *dev = &data->client->dev;
1881 struct input_dev *input_dev;
1882 int error;
1883 unsigned int num_mt_slots;
1884 unsigned int mt_flags = 0;
1885
1886 switch (data->multitouch) {
1887 case MXT_TOUCH_MULTI_T9:
1888 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1889 error = mxt_read_t9_resolution(data);
1890 if (error)
1891 dev_warn(dev, "Failed to initialize T9 resolution\n");
1892 break;
1893
1894 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1895 num_mt_slots = data->num_touchids;
1896 error = mxt_read_t100_config(data);
1897 if (error)
1898 dev_warn(dev, "Failed to read T100 config\n");
1899 break;
1900
1901 default:
1902 dev_err(dev, "Invalid multitouch object\n");
1903 return -EINVAL;
1904 }
1905
1906 /* Handle default values and orientation switch */
1907 if (data->max_x == 0)
1908 data->max_x = 1023;
1909
1910 if (data->max_y == 0)
1911 data->max_y = 1023;
1912
1913 if (data->xy_switch)
1914 swap(data->max_x, data->max_y);
1915
1916 dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1917
1918 /* Register input device */
1919 input_dev = input_allocate_device();
1920 if (!input_dev) {
1921 dev_err(dev, "Failed to allocate memory\n");
1922 return -ENOMEM;
1923 }
1924
1925 input_dev->name = "Atmel maXTouch Touchscreen";
1926 input_dev->phys = data->phys;
1927 input_dev->id.bustype = BUS_I2C;
1928 input_dev->dev.parent = dev;
1929 input_dev->open = mxt_input_open;
1930 input_dev->close = mxt_input_close;
1931
1932 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
1933
1934 /* For single touch */
1935 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
1936 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
1937
1938 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1939 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1940 data->t100_aux_ampl)) {
1941 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
1942 }
1943
1944 /* If device has buttons we assume it is a touchpad */
1945 if (pdata->t19_num_keys) {
1946 mxt_set_up_as_touchpad(input_dev, data);
1947 mt_flags |= INPUT_MT_POINTER;
1948 } else {
1949 mt_flags |= INPUT_MT_DIRECT;
1950 }
1951
1952 /* For multi touch */
1953 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
1954 if (error) {
1955 dev_err(dev, "Error %d initialising slots\n", error);
1956 goto err_free_mem;
1957 }
1958
1959 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
1960 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
1961 0, MT_TOOL_MAX, 0, 0);
1962 input_set_abs_params(input_dev, ABS_MT_DISTANCE,
1963 MXT_DISTANCE_ACTIVE_TOUCH,
1964 MXT_DISTANCE_HOVERING,
1965 0, 0);
1966 }
1967
1968 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1969 0, data->max_x, 0, 0);
1970 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1971 0, data->max_y, 0, 0);
1972
1973 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1974 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1975 data->t100_aux_area)) {
1976 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1977 0, MXT_MAX_AREA, 0, 0);
1978 }
1979
1980 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1981 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1982 data->t100_aux_ampl)) {
1983 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1984 0, 255, 0, 0);
1985 }
1986
1987 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1988 data->t100_aux_vect) {
1989 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
1990 0, 255, 0, 0);
1991 }
1992
1993 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1994 data->t100_aux_ampl) {
1995 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1996 0, 255, 0, 0);
1997 }
1998
1999 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2000 data->t100_aux_vect) {
2001 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2002 0, 255, 0, 0);
2003 }
2004
2005 input_set_drvdata(input_dev, data);
2006
2007 error = input_register_device(input_dev);
2008 if (error) {
2009 dev_err(dev, "Error %d registering input device\n", error);
2010 goto err_free_mem;
2011 }
2012
2013 data->input_dev = input_dev;
2014
2015 return 0;
2016
2017 err_free_mem:
2018 input_free_device(input_dev);
2019 return error;
2020 }
2021
2022 static int mxt_configure_objects(struct mxt_data *data,
2023 const struct firmware *cfg);
2024
2025 static void mxt_config_cb(const struct firmware *cfg, void *ctx)
2026 {
2027 mxt_configure_objects(ctx, cfg);
2028 release_firmware(cfg);
2029 }
2030
2031 static int mxt_initialize(struct mxt_data *data)
2032 {
2033 struct i2c_client *client = data->client;
2034 int recovery_attempts = 0;
2035 int error;
2036
2037 while (1) {
2038 error = mxt_get_info(data);
2039 if (!error)
2040 break;
2041
2042 /* Check bootloader state */
2043 error = mxt_probe_bootloader(data, false);
2044 if (error) {
2045 dev_info(&client->dev, "Trying alternate bootloader address\n");
2046 error = mxt_probe_bootloader(data, true);
2047 if (error) {
2048 /* Chip is not in appmode or bootloader mode */
2049 return error;
2050 }
2051 }
2052
2053 /* OK, we are in bootloader, see if we can recover */
2054 if (++recovery_attempts > 1) {
2055 dev_err(&client->dev, "Could not recover from bootloader mode\n");
2056 /*
2057 * We can reflash from this state, so do not
2058 * abort initialization.
2059 */
2060 data->in_bootloader = true;
2061 return 0;
2062 }
2063
2064 /* Attempt to exit bootloader into app mode */
2065 mxt_send_bootloader_cmd(data, false);
2066 msleep(MXT_FW_RESET_TIME);
2067 }
2068
2069 /* Get object table information */
2070 error = mxt_get_object_table(data);
2071 if (error) {
2072 dev_err(&client->dev, "Error %d reading object table\n", error);
2073 return error;
2074 }
2075
2076 error = mxt_acquire_irq(data);
2077 if (error)
2078 goto err_free_object_table;
2079
2080 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
2081 &client->dev, GFP_KERNEL, data,
2082 mxt_config_cb);
2083 if (error) {
2084 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
2085 error);
2086 goto err_free_object_table;
2087 }
2088
2089 return 0;
2090
2091 err_free_object_table:
2092 mxt_free_object_table(data);
2093 return error;
2094 }
2095
2096 static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
2097 {
2098 struct device *dev = &data->client->dev;
2099 int error;
2100 struct t7_config *new_config;
2101 struct t7_config deepsleep = { .active = 0, .idle = 0 };
2102
2103 if (sleep == MXT_POWER_CFG_DEEPSLEEP)
2104 new_config = &deepsleep;
2105 else
2106 new_config = &data->t7_cfg;
2107
2108 error = __mxt_write_reg(data->client, data->T7_address,
2109 sizeof(data->t7_cfg), new_config);
2110 if (error)
2111 return error;
2112
2113 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
2114 new_config->active, new_config->idle);
2115
2116 return 0;
2117 }
2118
2119 static int mxt_init_t7_power_cfg(struct mxt_data *data)
2120 {
2121 struct device *dev = &data->client->dev;
2122 int error;
2123 bool retry = false;
2124
2125 recheck:
2126 error = __mxt_read_reg(data->client, data->T7_address,
2127 sizeof(data->t7_cfg), &data->t7_cfg);
2128 if (error)
2129 return error;
2130
2131 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
2132 if (!retry) {
2133 dev_dbg(dev, "T7 cfg zero, resetting\n");
2134 mxt_soft_reset(data);
2135 retry = true;
2136 goto recheck;
2137 } else {
2138 dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
2139 data->t7_cfg.active = 20;
2140 data->t7_cfg.idle = 100;
2141 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2142 }
2143 }
2144
2145 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
2146 data->t7_cfg.active, data->t7_cfg.idle);
2147 return 0;
2148 }
2149
2150 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2151 static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x,
2152 unsigned int y)
2153 {
2154 struct mxt_info *info = &data->info;
2155 struct mxt_dbg *dbg = &data->dbg;
2156 unsigned int ofs, page;
2157 unsigned int col = 0;
2158 unsigned int col_width;
2159
2160 if (info->family_id == MXT_FAMILY_1386) {
2161 col_width = info->matrix_ysize / MXT1386_COLUMNS;
2162 col = y / col_width;
2163 y = y % col_width;
2164 } else {
2165 col_width = info->matrix_ysize;
2166 }
2167
2168 ofs = (y + (x * col_width)) * sizeof(u16);
2169 page = ofs / MXT_DIAGNOSTIC_SIZE;
2170 ofs %= MXT_DIAGNOSTIC_SIZE;
2171
2172 if (info->family_id == MXT_FAMILY_1386)
2173 page += col * MXT1386_PAGES_PER_COLUMN;
2174
2175 return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]);
2176 }
2177
2178 static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf)
2179 {
2180 struct mxt_dbg *dbg = &data->dbg;
2181 unsigned int x = 0;
2182 unsigned int y = 0;
2183 unsigned int i, rx, ry;
2184
2185 for (i = 0; i < dbg->t37_nodes; i++) {
2186 /* Handle orientation */
2187 rx = data->xy_switch ? y : x;
2188 ry = data->xy_switch ? x : y;
2189 rx = data->invertx ? (data->xsize - 1 - rx) : rx;
2190 ry = data->inverty ? (data->ysize - 1 - ry) : ry;
2191
2192 outbuf[i] = mxt_get_debug_value(data, rx, ry);
2193
2194 /* Next value */
2195 if (++x >= (data->xy_switch ? data->ysize : data->xsize)) {
2196 x = 0;
2197 y++;
2198 }
2199 }
2200
2201 return 0;
2202 }
2203
2204 static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode,
2205 u16 *outbuf)
2206 {
2207 struct mxt_dbg *dbg = &data->dbg;
2208 int retries = 0;
2209 int page;
2210 int ret;
2211 u8 cmd = mode;
2212 struct t37_debug *p;
2213 u8 cmd_poll;
2214
2215 for (page = 0; page < dbg->t37_pages; page++) {
2216 p = dbg->t37_buf + page;
2217
2218 ret = mxt_write_reg(data->client, dbg->diag_cmd_address,
2219 cmd);
2220 if (ret)
2221 return ret;
2222
2223 retries = 0;
2224 msleep(20);
2225 wait_cmd:
2226 /* Read back command byte */
2227 ret = __mxt_read_reg(data->client, dbg->diag_cmd_address,
2228 sizeof(cmd_poll), &cmd_poll);
2229 if (ret)
2230 return ret;
2231
2232 /* Field is cleared once the command has been processed */
2233 if (cmd_poll) {
2234 if (retries++ > 100)
2235 return -EINVAL;
2236
2237 msleep(20);
2238 goto wait_cmd;
2239 }
2240
2241 /* Read T37 page */
2242 ret = __mxt_read_reg(data->client, dbg->t37_address,
2243 sizeof(struct t37_debug), p);
2244 if (ret)
2245 return ret;
2246
2247 if (p->mode != mode || p->page != page) {
2248 dev_err(&data->client->dev, "T37 page mismatch\n");
2249 return -EINVAL;
2250 }
2251
2252 dev_dbg(&data->client->dev, "%s page:%d retries:%d\n",
2253 __func__, page, retries);
2254
2255 /* For remaining pages, write PAGEUP rather than mode */
2256 cmd = MXT_DIAGNOSTIC_PAGEUP;
2257 }
2258
2259 return mxt_convert_debug_pages(data, outbuf);
2260 }
2261
2262 static int mxt_queue_setup(struct vb2_queue *q,
2263 unsigned int *nbuffers, unsigned int *nplanes,
2264 unsigned int sizes[], struct device *alloc_devs[])
2265 {
2266 struct mxt_data *data = q->drv_priv;
2267 size_t size = data->dbg.t37_nodes * sizeof(u16);
2268
2269 if (*nplanes)
2270 return sizes[0] < size ? -EINVAL : 0;
2271
2272 *nplanes = 1;
2273 sizes[0] = size;
2274
2275 return 0;
2276 }
2277
2278 static void mxt_buffer_queue(struct vb2_buffer *vb)
2279 {
2280 struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue);
2281 u16 *ptr;
2282 int ret;
2283 u8 mode;
2284
2285 ptr = vb2_plane_vaddr(vb, 0);
2286 if (!ptr) {
2287 dev_err(&data->client->dev, "Error acquiring frame ptr\n");
2288 goto fault;
2289 }
2290
2291 switch (data->dbg.input) {
2292 case MXT_V4L_INPUT_DELTAS:
2293 default:
2294 mode = MXT_DIAGNOSTIC_DELTAS;
2295 break;
2296
2297 case MXT_V4L_INPUT_REFS:
2298 mode = MXT_DIAGNOSTIC_REFS;
2299 break;
2300 }
2301
2302 ret = mxt_read_diagnostic_debug(data, mode, ptr);
2303 if (ret)
2304 goto fault;
2305
2306 vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16));
2307 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2308 return;
2309
2310 fault:
2311 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2312 }
2313
2314 /* V4L2 structures */
2315 static const struct vb2_ops mxt_queue_ops = {
2316 .queue_setup = mxt_queue_setup,
2317 .buf_queue = mxt_buffer_queue,
2318 .wait_prepare = vb2_ops_wait_prepare,
2319 .wait_finish = vb2_ops_wait_finish,
2320 };
2321
2322 static const struct vb2_queue mxt_queue = {
2323 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2324 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
2325 .buf_struct_size = sizeof(struct mxt_vb2_buffer),
2326 .ops = &mxt_queue_ops,
2327 .mem_ops = &vb2_vmalloc_memops,
2328 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
2329 .min_buffers_needed = 1,
2330 };
2331
2332 static int mxt_vidioc_querycap(struct file *file, void *priv,
2333 struct v4l2_capability *cap)
2334 {
2335 struct mxt_data *data = video_drvdata(file);
2336
2337 strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver));
2338 strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card));
2339 snprintf(cap->bus_info, sizeof(cap->bus_info),
2340 "I2C:%s", dev_name(&data->client->dev));
2341 return 0;
2342 }
2343
2344 static int mxt_vidioc_enum_input(struct file *file, void *priv,
2345 struct v4l2_input *i)
2346 {
2347 if (i->index >= MXT_V4L_INPUT_MAX)
2348 return -EINVAL;
2349
2350 i->type = V4L2_INPUT_TYPE_TOUCH;
2351
2352 switch (i->index) {
2353 case MXT_V4L_INPUT_REFS:
2354 strlcpy(i->name, "Mutual Capacitance References",
2355 sizeof(i->name));
2356 break;
2357 case MXT_V4L_INPUT_DELTAS:
2358 strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name));
2359 break;
2360 }
2361
2362 return 0;
2363 }
2364
2365 static int mxt_set_input(struct mxt_data *data, unsigned int i)
2366 {
2367 struct v4l2_pix_format *f = &data->dbg.format;
2368
2369 if (i >= MXT_V4L_INPUT_MAX)
2370 return -EINVAL;
2371
2372 if (i == MXT_V4L_INPUT_DELTAS)
2373 f->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2374 else
2375 f->pixelformat = V4L2_TCH_FMT_TU16;
2376
2377 f->width = data->xy_switch ? data->ysize : data->xsize;
2378 f->height = data->xy_switch ? data->xsize : data->ysize;
2379 f->field = V4L2_FIELD_NONE;
2380 f->colorspace = V4L2_COLORSPACE_RAW;
2381 f->bytesperline = f->width * sizeof(u16);
2382 f->sizeimage = f->width * f->height * sizeof(u16);
2383
2384 data->dbg.input = i;
2385
2386 return 0;
2387 }
2388
2389 static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i)
2390 {
2391 return mxt_set_input(video_drvdata(file), i);
2392 }
2393
2394 static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
2395 {
2396 struct mxt_data *data = video_drvdata(file);
2397
2398 *i = data->dbg.input;
2399
2400 return 0;
2401 }
2402
2403 static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f)
2404 {
2405 struct mxt_data *data = video_drvdata(file);
2406
2407 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2408 f->fmt.pix = data->dbg.format;
2409
2410 return 0;
2411 }
2412
2413 static int mxt_vidioc_enum_fmt(struct file *file, void *priv,
2414 struct v4l2_fmtdesc *fmt)
2415 {
2416 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2417 return -EINVAL;
2418
2419 switch (fmt->index) {
2420 case 0:
2421 fmt->pixelformat = V4L2_TCH_FMT_TU16;
2422 break;
2423
2424 case 1:
2425 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2426 break;
2427
2428 default:
2429 return -EINVAL;
2430 }
2431
2432 return 0;
2433 }
2434
2435 static int mxt_vidioc_g_parm(struct file *file, void *fh,
2436 struct v4l2_streamparm *a)
2437 {
2438 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2439 return -EINVAL;
2440
2441 a->parm.capture.readbuffers = 1;
2442 a->parm.capture.timeperframe.numerator = 1;
2443 a->parm.capture.timeperframe.denominator = 10;
2444 return 0;
2445 }
2446
2447 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = {
2448 .vidioc_querycap = mxt_vidioc_querycap,
2449
2450 .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt,
2451 .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt,
2452 .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt,
2453 .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt,
2454 .vidioc_g_parm = mxt_vidioc_g_parm,
2455
2456 .vidioc_enum_input = mxt_vidioc_enum_input,
2457 .vidioc_g_input = mxt_vidioc_g_input,
2458 .vidioc_s_input = mxt_vidioc_s_input,
2459
2460 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2461 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2462 .vidioc_querybuf = vb2_ioctl_querybuf,
2463 .vidioc_qbuf = vb2_ioctl_qbuf,
2464 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2465 .vidioc_expbuf = vb2_ioctl_expbuf,
2466
2467 .vidioc_streamon = vb2_ioctl_streamon,
2468 .vidioc_streamoff = vb2_ioctl_streamoff,
2469 };
2470
2471 static const struct video_device mxt_video_device = {
2472 .name = "Atmel maxTouch",
2473 .fops = &mxt_video_fops,
2474 .ioctl_ops = &mxt_video_ioctl_ops,
2475 .release = video_device_release_empty,
2476 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
2477 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
2478 };
2479
2480 static void mxt_debug_init(struct mxt_data *data)
2481 {
2482 struct mxt_info *info = &data->info;
2483 struct mxt_dbg *dbg = &data->dbg;
2484 struct mxt_object *object;
2485 int error;
2486
2487 object = mxt_get_object(data, MXT_GEN_COMMAND_T6);
2488 if (!object)
2489 goto error;
2490
2491 dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC;
2492
2493 object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37);
2494 if (!object)
2495 goto error;
2496
2497 if (mxt_obj_size(object) != sizeof(struct t37_debug)) {
2498 dev_warn(&data->client->dev, "Bad T37 size");
2499 goto error;
2500 }
2501
2502 dbg->t37_address = object->start_address;
2503
2504 /* Calculate size of data and allocate buffer */
2505 dbg->t37_nodes = data->xsize * data->ysize;
2506
2507 if (info->family_id == MXT_FAMILY_1386)
2508 dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN;
2509 else
2510 dbg->t37_pages = DIV_ROUND_UP(data->xsize *
2511 data->info.matrix_ysize *
2512 sizeof(u16),
2513 sizeof(dbg->t37_buf->data));
2514
2515 dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages,
2516 sizeof(struct t37_debug), GFP_KERNEL);
2517 if (!dbg->t37_buf)
2518 goto error;
2519
2520 /* init channel to zero */
2521 mxt_set_input(data, 0);
2522
2523 /* register video device */
2524 snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts");
2525 error = v4l2_device_register(&data->client->dev, &dbg->v4l2);
2526 if (error)
2527 goto error;
2528
2529 /* initialize the queue */
2530 mutex_init(&dbg->lock);
2531 dbg->queue = mxt_queue;
2532 dbg->queue.drv_priv = data;
2533 dbg->queue.lock = &dbg->lock;
2534 dbg->queue.dev = &data->client->dev;
2535
2536 error = vb2_queue_init(&dbg->queue);
2537 if (error)
2538 goto error_unreg_v4l2;
2539
2540 dbg->vdev = mxt_video_device;
2541 dbg->vdev.v4l2_dev = &dbg->v4l2;
2542 dbg->vdev.lock = &dbg->lock;
2543 dbg->vdev.vfl_dir = VFL_DIR_RX;
2544 dbg->vdev.queue = &dbg->queue;
2545 video_set_drvdata(&dbg->vdev, data);
2546
2547 error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1);
2548 if (error)
2549 goto error_unreg_v4l2;
2550
2551 return;
2552
2553 error_unreg_v4l2:
2554 v4l2_device_unregister(&dbg->v4l2);
2555 error:
2556 dev_warn(&data->client->dev, "Error initializing T37\n");
2557 }
2558 #else
2559 static void mxt_debug_init(struct mxt_data *data)
2560 {
2561 }
2562 #endif
2563
2564 static int mxt_configure_objects(struct mxt_data *data,
2565 const struct firmware *cfg)
2566 {
2567 struct device *dev = &data->client->dev;
2568 struct mxt_info *info = &data->info;
2569 int error;
2570
2571 error = mxt_init_t7_power_cfg(data);
2572 if (error) {
2573 dev_err(dev, "Failed to initialize power cfg\n");
2574 return error;
2575 }
2576
2577 if (cfg) {
2578 error = mxt_update_cfg(data, cfg);
2579 if (error)
2580 dev_warn(dev, "Error %d updating config\n", error);
2581 }
2582
2583 if (data->multitouch) {
2584 error = mxt_initialize_input_device(data);
2585 if (error)
2586 return error;
2587 } else {
2588 dev_warn(dev, "No touch object detected\n");
2589 }
2590
2591 mxt_debug_init(data);
2592
2593 dev_info(dev,
2594 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
2595 info->family_id, info->variant_id, info->version >> 4,
2596 info->version & 0xf, info->build, info->object_num);
2597
2598 return 0;
2599 }
2600
2601 /* Firmware Version is returned as Major.Minor.Build */
2602 static ssize_t mxt_fw_version_show(struct device *dev,
2603 struct device_attribute *attr, char *buf)
2604 {
2605 struct mxt_data *data = dev_get_drvdata(dev);
2606 struct mxt_info *info = &data->info;
2607 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
2608 info->version >> 4, info->version & 0xf, info->build);
2609 }
2610
2611 /* Hardware Version is returned as FamilyID.VariantID */
2612 static ssize_t mxt_hw_version_show(struct device *dev,
2613 struct device_attribute *attr, char *buf)
2614 {
2615 struct mxt_data *data = dev_get_drvdata(dev);
2616 struct mxt_info *info = &data->info;
2617 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
2618 info->family_id, info->variant_id);
2619 }
2620
2621 static ssize_t mxt_show_instance(char *buf, int count,
2622 struct mxt_object *object, int instance,
2623 const u8 *val)
2624 {
2625 int i;
2626
2627 if (mxt_obj_instances(object) > 1)
2628 count += scnprintf(buf + count, PAGE_SIZE - count,
2629 "Instance %u\n", instance);
2630
2631 for (i = 0; i < mxt_obj_size(object); i++)
2632 count += scnprintf(buf + count, PAGE_SIZE - count,
2633 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
2634 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
2635
2636 return count;
2637 }
2638
2639 static ssize_t mxt_object_show(struct device *dev,
2640 struct device_attribute *attr, char *buf)
2641 {
2642 struct mxt_data *data = dev_get_drvdata(dev);
2643 struct mxt_object *object;
2644 int count = 0;
2645 int i, j;
2646 int error;
2647 u8 *obuf;
2648
2649 /* Pre-allocate buffer large enough to hold max sized object. */
2650 obuf = kmalloc(256, GFP_KERNEL);
2651 if (!obuf)
2652 return -ENOMEM;
2653
2654 error = 0;
2655 for (i = 0; i < data->info.object_num; i++) {
2656 object = data->object_table + i;
2657
2658 if (!mxt_object_readable(object->type))
2659 continue;
2660
2661 count += scnprintf(buf + count, PAGE_SIZE - count,
2662 "T%u:\n", object->type);
2663
2664 for (j = 0; j < mxt_obj_instances(object); j++) {
2665 u16 size = mxt_obj_size(object);
2666 u16 addr = object->start_address + j * size;
2667
2668 error = __mxt_read_reg(data->client, addr, size, obuf);
2669 if (error)
2670 goto done;
2671
2672 count = mxt_show_instance(buf, count, object, j, obuf);
2673 }
2674 }
2675
2676 done:
2677 kfree(obuf);
2678 return error ?: count;
2679 }
2680
2681 static int mxt_check_firmware_format(struct device *dev,
2682 const struct firmware *fw)
2683 {
2684 unsigned int pos = 0;
2685 char c;
2686
2687 while (pos < fw->size) {
2688 c = *(fw->data + pos);
2689
2690 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
2691 return 0;
2692
2693 pos++;
2694 }
2695
2696 /*
2697 * To convert file try:
2698 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2699 */
2700 dev_err(dev, "Aborting: firmware file must be in binary format\n");
2701
2702 return -EINVAL;
2703 }
2704
2705 static int mxt_load_fw(struct device *dev, const char *fn)
2706 {
2707 struct mxt_data *data = dev_get_drvdata(dev);
2708 const struct firmware *fw = NULL;
2709 unsigned int frame_size;
2710 unsigned int pos = 0;
2711 unsigned int retry = 0;
2712 unsigned int frame = 0;
2713 int ret;
2714
2715 ret = request_firmware(&fw, fn, dev);
2716 if (ret) {
2717 dev_err(dev, "Unable to open firmware %s\n", fn);
2718 return ret;
2719 }
2720
2721 /* Check for incorrect enc file */
2722 ret = mxt_check_firmware_format(dev, fw);
2723 if (ret)
2724 goto release_firmware;
2725
2726 if (!data->in_bootloader) {
2727 /* Change to the bootloader mode */
2728 data->in_bootloader = true;
2729
2730 ret = mxt_t6_command(data, MXT_COMMAND_RESET,
2731 MXT_BOOT_VALUE, false);
2732 if (ret)
2733 goto release_firmware;
2734
2735 msleep(MXT_RESET_TIME);
2736
2737 /* Do not need to scan since we know family ID */
2738 ret = mxt_lookup_bootloader_address(data, 0);
2739 if (ret)
2740 goto release_firmware;
2741
2742 mxt_free_input_device(data);
2743 mxt_free_object_table(data);
2744 } else {
2745 enable_irq(data->irq);
2746 }
2747
2748 reinit_completion(&data->bl_completion);
2749
2750 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
2751 if (ret) {
2752 /* Bootloader may still be unlocked from previous attempt */
2753 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
2754 if (ret)
2755 goto disable_irq;
2756 } else {
2757 dev_info(dev, "Unlocking bootloader\n");
2758
2759 /* Unlock bootloader */
2760 ret = mxt_send_bootloader_cmd(data, true);
2761 if (ret)
2762 goto disable_irq;
2763 }
2764
2765 while (pos < fw->size) {
2766 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
2767 if (ret)
2768 goto disable_irq;
2769
2770 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
2771
2772 /* Take account of CRC bytes */
2773 frame_size += 2;
2774
2775 /* Write one frame to device */
2776 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
2777 if (ret)
2778 goto disable_irq;
2779
2780 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
2781 if (ret) {
2782 retry++;
2783
2784 /* Back off by 20ms per retry */
2785 msleep(retry * 20);
2786
2787 if (retry > 20) {
2788 dev_err(dev, "Retry count exceeded\n");
2789 goto disable_irq;
2790 }
2791 } else {
2792 retry = 0;
2793 pos += frame_size;
2794 frame++;
2795 }
2796
2797 if (frame % 50 == 0)
2798 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
2799 frame, pos, fw->size);
2800 }
2801
2802 /* Wait for flash. */
2803 ret = mxt_wait_for_completion(data, &data->bl_completion,
2804 MXT_FW_RESET_TIME);
2805 if (ret)
2806 goto disable_irq;
2807
2808 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
2809
2810 /*
2811 * Wait for device to reset. Some bootloader versions do not assert
2812 * the CHG line after bootloading has finished, so ignore potential
2813 * errors.
2814 */
2815 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
2816
2817 data->in_bootloader = false;
2818
2819 disable_irq:
2820 disable_irq(data->irq);
2821 release_firmware:
2822 release_firmware(fw);
2823 return ret;
2824 }
2825
2826 static ssize_t mxt_update_fw_store(struct device *dev,
2827 struct device_attribute *attr,
2828 const char *buf, size_t count)
2829 {
2830 struct mxt_data *data = dev_get_drvdata(dev);
2831 int error;
2832
2833 error = mxt_load_fw(dev, MXT_FW_NAME);
2834 if (error) {
2835 dev_err(dev, "The firmware update failed(%d)\n", error);
2836 count = error;
2837 } else {
2838 dev_info(dev, "The firmware update succeeded\n");
2839
2840 error = mxt_initialize(data);
2841 if (error)
2842 return error;
2843 }
2844
2845 return count;
2846 }
2847
2848 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
2849 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
2850 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
2851 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
2852
2853 static struct attribute *mxt_attrs[] = {
2854 &dev_attr_fw_version.attr,
2855 &dev_attr_hw_version.attr,
2856 &dev_attr_object.attr,
2857 &dev_attr_update_fw.attr,
2858 NULL
2859 };
2860
2861 static const struct attribute_group mxt_attr_group = {
2862 .attrs = mxt_attrs,
2863 };
2864
2865 static void mxt_start(struct mxt_data *data)
2866 {
2867 switch (data->pdata->suspend_mode) {
2868 case MXT_SUSPEND_T9_CTRL:
2869 mxt_soft_reset(data);
2870
2871 /* Touch enable */
2872 /* 0x83 = SCANEN | RPTEN | ENABLE */
2873 mxt_write_object(data,
2874 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
2875 break;
2876
2877 case MXT_SUSPEND_DEEP_SLEEP:
2878 default:
2879 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2880
2881 /* Recalibrate since chip has been in deep sleep */
2882 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
2883 break;
2884 }
2885
2886 }
2887
2888 static void mxt_stop(struct mxt_data *data)
2889 {
2890 switch (data->pdata->suspend_mode) {
2891 case MXT_SUSPEND_T9_CTRL:
2892 /* Touch disable */
2893 mxt_write_object(data,
2894 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
2895 break;
2896
2897 case MXT_SUSPEND_DEEP_SLEEP:
2898 default:
2899 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
2900 break;
2901 }
2902 }
2903
2904 static int mxt_input_open(struct input_dev *dev)
2905 {
2906 struct mxt_data *data = input_get_drvdata(dev);
2907
2908 mxt_start(data);
2909
2910 return 0;
2911 }
2912
2913 static void mxt_input_close(struct input_dev *dev)
2914 {
2915 struct mxt_data *data = input_get_drvdata(dev);
2916
2917 mxt_stop(data);
2918 }
2919
2920 #ifdef CONFIG_OF
2921 static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
2922 {
2923 struct mxt_platform_data *pdata;
2924 struct device_node *np = client->dev.of_node;
2925 u32 *keymap;
2926 int proplen, ret;
2927
2928 if (!np)
2929 return ERR_PTR(-ENOENT);
2930
2931 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
2932 if (!pdata)
2933 return ERR_PTR(-ENOMEM);
2934
2935 if (of_find_property(np, "linux,gpio-keymap", &proplen)) {
2936 pdata->t19_num_keys = proplen / sizeof(u32);
2937
2938 keymap = devm_kzalloc(&client->dev,
2939 pdata->t19_num_keys * sizeof(keymap[0]),
2940 GFP_KERNEL);
2941 if (!keymap)
2942 return ERR_PTR(-ENOMEM);
2943
2944 ret = of_property_read_u32_array(np, "linux,gpio-keymap",
2945 keymap, pdata->t19_num_keys);
2946 if (ret)
2947 dev_warn(&client->dev,
2948 "Couldn't read linux,gpio-keymap: %d\n", ret);
2949
2950 pdata->t19_keymap = keymap;
2951 }
2952
2953 pdata->suspend_mode = MXT_SUSPEND_DEEP_SLEEP;
2954
2955 return pdata;
2956 }
2957 #else
2958 static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
2959 {
2960 return ERR_PTR(-ENOENT);
2961 }
2962 #endif
2963
2964 #ifdef CONFIG_ACPI
2965
2966 struct mxt_acpi_platform_data {
2967 const char *hid;
2968 struct mxt_platform_data pdata;
2969 };
2970
2971 static unsigned int samus_touchpad_buttons[] = {
2972 KEY_RESERVED,
2973 KEY_RESERVED,
2974 KEY_RESERVED,
2975 BTN_LEFT
2976 };
2977
2978 static struct mxt_acpi_platform_data samus_platform_data[] = {
2979 {
2980 /* Touchpad */
2981 .hid = "ATML0000",
2982 .pdata = {
2983 .t19_num_keys = ARRAY_SIZE(samus_touchpad_buttons),
2984 .t19_keymap = samus_touchpad_buttons,
2985 },
2986 },
2987 {
2988 /* Touchscreen */
2989 .hid = "ATML0001",
2990 },
2991 { }
2992 };
2993
2994 static unsigned int chromebook_tp_buttons[] = {
2995 KEY_RESERVED,
2996 KEY_RESERVED,
2997 KEY_RESERVED,
2998 KEY_RESERVED,
2999 KEY_RESERVED,
3000 BTN_LEFT
3001 };
3002
3003 static struct mxt_acpi_platform_data chromebook_platform_data[] = {
3004 {
3005 /* Touchpad */
3006 .hid = "ATML0000",
3007 .pdata = {
3008 .t19_num_keys = ARRAY_SIZE(chromebook_tp_buttons),
3009 .t19_keymap = chromebook_tp_buttons,
3010 },
3011 },
3012 {
3013 /* Touchscreen */
3014 .hid = "ATML0001",
3015 },
3016 { }
3017 };
3018
3019 static const struct dmi_system_id mxt_dmi_table[] = {
3020 {
3021 /* 2015 Google Pixel */
3022 .ident = "Chromebook Pixel 2",
3023 .matches = {
3024 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
3025 DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
3026 },
3027 .driver_data = samus_platform_data,
3028 },
3029 {
3030 /* Other Google Chromebooks */
3031 .ident = "Chromebook",
3032 .matches = {
3033 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
3034 },
3035 .driver_data = chromebook_platform_data,
3036 },
3037 { }
3038 };
3039
3040 static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
3041 {
3042 struct acpi_device *adev;
3043 const struct dmi_system_id *system_id;
3044 const struct mxt_acpi_platform_data *acpi_pdata;
3045
3046 /*
3047 * Ignore ACPI devices representing bootloader mode.
3048 *
3049 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3050 * devices for both application and bootloader modes, but we are
3051 * interested in application mode only (if device is in bootloader
3052 * mode we'll end up switching into application anyway). So far
3053 * application mode addresses were all above 0x40, so we'll use it
3054 * as a threshold.
3055 */
3056 if (client->addr < 0x40)
3057 return ERR_PTR(-ENXIO);
3058
3059 adev = ACPI_COMPANION(&client->dev);
3060 if (!adev)
3061 return ERR_PTR(-ENOENT);
3062
3063 system_id = dmi_first_match(mxt_dmi_table);
3064 if (!system_id)
3065 return ERR_PTR(-ENOENT);
3066
3067 acpi_pdata = system_id->driver_data;
3068 if (!acpi_pdata)
3069 return ERR_PTR(-ENOENT);
3070
3071 while (acpi_pdata->hid) {
3072 if (!strcmp(acpi_device_hid(adev), acpi_pdata->hid))
3073 return &acpi_pdata->pdata;
3074
3075 acpi_pdata++;
3076 }
3077
3078 return ERR_PTR(-ENOENT);
3079 }
3080 #else
3081 static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
3082 {
3083 return ERR_PTR(-ENOENT);
3084 }
3085 #endif
3086
3087 static const struct mxt_platform_data *
3088 mxt_get_platform_data(struct i2c_client *client)
3089 {
3090 const struct mxt_platform_data *pdata;
3091
3092 pdata = dev_get_platdata(&client->dev);
3093 if (pdata)
3094 return pdata;
3095
3096 pdata = mxt_parse_dt(client);
3097 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
3098 return pdata;
3099
3100 pdata = mxt_parse_acpi(client);
3101 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
3102 return pdata;
3103
3104 dev_err(&client->dev, "No platform data specified\n");
3105 return ERR_PTR(-EINVAL);
3106 }
3107
3108 static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
3109 {
3110 struct mxt_data *data;
3111 const struct mxt_platform_data *pdata;
3112 int error;
3113
3114 pdata = mxt_get_platform_data(client);
3115 if (IS_ERR(pdata))
3116 return PTR_ERR(pdata);
3117
3118 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
3119 if (!data) {
3120 dev_err(&client->dev, "Failed to allocate memory\n");
3121 return -ENOMEM;
3122 }
3123
3124 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
3125 client->adapter->nr, client->addr);
3126
3127 data->client = client;
3128 data->pdata = pdata;
3129 data->irq = client->irq;
3130 i2c_set_clientdata(client, data);
3131
3132 init_completion(&data->bl_completion);
3133 init_completion(&data->reset_completion);
3134 init_completion(&data->crc_completion);
3135
3136 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
3137 pdata->irqflags | IRQF_ONESHOT,
3138 client->name, data);
3139 if (error) {
3140 dev_err(&client->dev, "Failed to register interrupt\n");
3141 goto err_free_mem;
3142 }
3143
3144 disable_irq(client->irq);
3145
3146 error = mxt_initialize(data);
3147 if (error)
3148 goto err_free_irq;
3149
3150 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
3151 if (error) {
3152 dev_err(&client->dev, "Failure %d creating sysfs group\n",
3153 error);
3154 goto err_free_object;
3155 }
3156
3157 return 0;
3158
3159 err_free_object:
3160 mxt_free_input_device(data);
3161 mxt_free_object_table(data);
3162 err_free_irq:
3163 free_irq(client->irq, data);
3164 err_free_mem:
3165 kfree(data);
3166 return error;
3167 }
3168
3169 static int mxt_remove(struct i2c_client *client)
3170 {
3171 struct mxt_data *data = i2c_get_clientdata(client);
3172
3173 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
3174 free_irq(data->irq, data);
3175 mxt_free_input_device(data);
3176 mxt_free_object_table(data);
3177 kfree(data);
3178
3179 return 0;
3180 }
3181
3182 static int __maybe_unused mxt_suspend(struct device *dev)
3183 {
3184 struct i2c_client *client = to_i2c_client(dev);
3185 struct mxt_data *data = i2c_get_clientdata(client);
3186 struct input_dev *input_dev = data->input_dev;
3187
3188 if (!input_dev)
3189 return 0;
3190
3191 mutex_lock(&input_dev->mutex);
3192
3193 if (input_dev->users)
3194 mxt_stop(data);
3195
3196 mutex_unlock(&input_dev->mutex);
3197
3198 return 0;
3199 }
3200
3201 static int __maybe_unused mxt_resume(struct device *dev)
3202 {
3203 struct i2c_client *client = to_i2c_client(dev);
3204 struct mxt_data *data = i2c_get_clientdata(client);
3205 struct input_dev *input_dev = data->input_dev;
3206
3207 if (!input_dev)
3208 return 0;
3209
3210 mutex_lock(&input_dev->mutex);
3211
3212 if (input_dev->users)
3213 mxt_start(data);
3214
3215 mutex_unlock(&input_dev->mutex);
3216
3217 return 0;
3218 }
3219
3220 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
3221
3222 static const struct of_device_id mxt_of_match[] = {
3223 { .compatible = "atmel,maxtouch", },
3224 {},
3225 };
3226 MODULE_DEVICE_TABLE(of, mxt_of_match);
3227
3228 #ifdef CONFIG_ACPI
3229 static const struct acpi_device_id mxt_acpi_id[] = {
3230 { "ATML0000", 0 }, /* Touchpad */
3231 { "ATML0001", 0 }, /* Touchscreen */
3232 { }
3233 };
3234 MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
3235 #endif
3236
3237 static const struct i2c_device_id mxt_id[] = {
3238 { "qt602240_ts", 0 },
3239 { "atmel_mxt_ts", 0 },
3240 { "atmel_mxt_tp", 0 },
3241 { "maxtouch", 0 },
3242 { "mXT224", 0 },
3243 { }
3244 };
3245 MODULE_DEVICE_TABLE(i2c, mxt_id);
3246
3247 static struct i2c_driver mxt_driver = {
3248 .driver = {
3249 .name = "atmel_mxt_ts",
3250 .of_match_table = of_match_ptr(mxt_of_match),
3251 .acpi_match_table = ACPI_PTR(mxt_acpi_id),
3252 .pm = &mxt_pm_ops,
3253 },
3254 .probe = mxt_probe,
3255 .remove = mxt_remove,
3256 .id_table = mxt_id,
3257 };
3258
3259 module_i2c_driver(mxt_driver);
3260
3261 /* Module information */
3262 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
3263 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
3264 MODULE_LICENSE("GPL");
This page took 0.124832 seconds and 5 git commands to generate.