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