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