Linux 5.1.15
[linux/fpc-iii.git] / drivers / input / touchscreen / atmel_mxt_ts.c
blob5c63d25ce84ebee9a6296de142c8cd6115a79701
1 /*
2 * Atmel maXTouch Touchscreen driver
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Copyright (C) 2011-2014 Atmel Corporation
6 * Copyright (C) 2012 Google, Inc.
7 * Copyright (C) 2016 Zodiac Inflight Innovations
9 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
18 #include <linux/acpi.h>
19 #include <linux/dmi.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/completion.h>
23 #include <linux/delay.h>
24 #include <linux/firmware.h>
25 #include <linux/i2c.h>
26 #include <linux/input/mt.h>
27 #include <linux/interrupt.h>
28 #include <linux/of.h>
29 #include <linux/property.h>
30 #include <linux/slab.h>
31 #include <linux/gpio/consumer.h>
32 #include <asm/unaligned.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/videobuf2-v4l2.h>
36 #include <media/videobuf2-vmalloc.h>
38 /* Firmware files */
39 #define MXT_FW_NAME "maxtouch.fw"
40 #define MXT_CFG_NAME "maxtouch.cfg"
41 #define MXT_CFG_MAGIC "OBP_RAW V1"
43 /* Registers */
44 #define MXT_OBJECT_START 0x07
45 #define MXT_OBJECT_SIZE 6
46 #define MXT_INFO_CHECKSUM_SIZE 3
47 #define MXT_MAX_BLOCK_WRITE 256
49 /* Object types */
50 #define MXT_DEBUG_DIAGNOSTIC_T37 37
51 #define MXT_GEN_MESSAGE_T5 5
52 #define MXT_GEN_COMMAND_T6 6
53 #define MXT_GEN_POWER_T7 7
54 #define MXT_GEN_ACQUIRE_T8 8
55 #define MXT_GEN_DATASOURCE_T53 53
56 #define MXT_TOUCH_MULTI_T9 9
57 #define MXT_TOUCH_KEYARRAY_T15 15
58 #define MXT_TOUCH_PROXIMITY_T23 23
59 #define MXT_TOUCH_PROXKEY_T52 52
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
80 /* MXT_GEN_MESSAGE_T5 object */
81 #define MXT_RPTID_NOMSG 0xff
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
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)
98 /* MXT_GEN_POWER_T7 field */
99 struct t7_config {
100 u8 idle;
101 u8 active;
102 } __packed;
104 #define MXT_POWER_CFG_RUN 0
105 #define MXT_POWER_CFG_DEEPSLEEP 1
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
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)
124 struct t9_range {
125 __le16 x;
126 __le16 y;
127 } __packed;
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)
134 /* MXT_SPT_COMMSCONFIG_T18 */
135 #define MXT_COMMS_CTRL 0
136 #define MXT_COMMS_CMD 1
138 /* MXT_DEBUG_DIAGNOSTIC_T37 */
139 #define MXT_DIAGNOSTIC_PAGEUP 0x01
140 #define MXT_DIAGNOSTIC_DELTAS 0x10
141 #define MXT_DIAGNOSTIC_REFS 0x11
142 #define MXT_DIAGNOSTIC_SIZE 128
144 #define MXT_FAMILY_1386 160
145 #define MXT1386_COLUMNS 3
146 #define MXT1386_PAGES_PER_COLUMN 8
148 struct t37_debug {
149 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
150 u8 mode;
151 u8 page;
152 u8 data[MXT_DIAGNOSTIC_SIZE];
153 #endif
156 /* Define for MXT_GEN_COMMAND_T6 */
157 #define MXT_BOOT_VALUE 0xa5
158 #define MXT_RESET_VALUE 0x01
159 #define MXT_BACKUP_VALUE 0x55
161 /* T100 Multiple Touch Touchscreen */
162 #define MXT_T100_CTRL 0
163 #define MXT_T100_CFG1 1
164 #define MXT_T100_TCHAUX 3
165 #define MXT_T100_XSIZE 9
166 #define MXT_T100_XRANGE 13
167 #define MXT_T100_YSIZE 20
168 #define MXT_T100_YRANGE 24
170 #define MXT_T100_CFG_SWITCHXY BIT(5)
171 #define MXT_T100_CFG_INVERTY BIT(6)
172 #define MXT_T100_CFG_INVERTX BIT(7)
174 #define MXT_T100_TCHAUX_VECT BIT(0)
175 #define MXT_T100_TCHAUX_AMPL BIT(1)
176 #define MXT_T100_TCHAUX_AREA BIT(2)
178 #define MXT_T100_DETECT BIT(7)
179 #define MXT_T100_TYPE_MASK 0x70
181 enum t100_type {
182 MXT_T100_TYPE_FINGER = 1,
183 MXT_T100_TYPE_PASSIVE_STYLUS = 2,
184 MXT_T100_TYPE_HOVERING_FINGER = 4,
185 MXT_T100_TYPE_GLOVE = 5,
186 MXT_T100_TYPE_LARGE_TOUCH = 6,
189 #define MXT_DISTANCE_ACTIVE_TOUCH 0
190 #define MXT_DISTANCE_HOVERING 1
192 #define MXT_TOUCH_MAJOR_DEFAULT 1
193 #define MXT_PRESSURE_DEFAULT 1
195 /* Delay times */
196 #define MXT_BACKUP_TIME 50 /* msec */
197 #define MXT_RESET_GPIO_TIME 20 /* msec */
198 #define MXT_RESET_INVALID_CHG 100 /* msec */
199 #define MXT_RESET_TIME 200 /* msec */
200 #define MXT_RESET_TIMEOUT 3000 /* msec */
201 #define MXT_CRC_TIMEOUT 1000 /* msec */
202 #define MXT_FW_RESET_TIME 3000 /* msec */
203 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
205 /* Command to unlock bootloader */
206 #define MXT_UNLOCK_CMD_MSB 0xaa
207 #define MXT_UNLOCK_CMD_LSB 0xdc
209 /* Bootloader mode status */
210 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
211 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
212 #define MXT_FRAME_CRC_CHECK 0x02
213 #define MXT_FRAME_CRC_FAIL 0x03
214 #define MXT_FRAME_CRC_PASS 0x04
215 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
216 #define MXT_BOOT_STATUS_MASK 0x3f
217 #define MXT_BOOT_EXTENDED_ID BIT(5)
218 #define MXT_BOOT_ID_MASK 0x1f
220 /* Touchscreen absolute values */
221 #define MXT_MAX_AREA 0xff
223 #define MXT_PIXELS_PER_MM 20
225 struct mxt_info {
226 u8 family_id;
227 u8 variant_id;
228 u8 version;
229 u8 build;
230 u8 matrix_xsize;
231 u8 matrix_ysize;
232 u8 object_num;
235 struct mxt_object {
236 u8 type;
237 u16 start_address;
238 u8 size_minus_one;
239 u8 instances_minus_one;
240 u8 num_report_ids;
241 } __packed;
243 struct mxt_dbg {
244 u16 t37_address;
245 u16 diag_cmd_address;
246 struct t37_debug *t37_buf;
247 unsigned int t37_pages;
248 unsigned int t37_nodes;
250 struct v4l2_device v4l2;
251 struct v4l2_pix_format format;
252 struct video_device vdev;
253 struct vb2_queue queue;
254 struct mutex lock;
255 int input;
258 enum v4l_dbg_inputs {
259 MXT_V4L_INPUT_DELTAS,
260 MXT_V4L_INPUT_REFS,
261 MXT_V4L_INPUT_MAX,
264 static const struct v4l2_file_operations mxt_video_fops = {
265 .owner = THIS_MODULE,
266 .open = v4l2_fh_open,
267 .release = vb2_fop_release,
268 .unlocked_ioctl = video_ioctl2,
269 .read = vb2_fop_read,
270 .mmap = vb2_fop_mmap,
271 .poll = vb2_fop_poll,
274 enum mxt_suspend_mode {
275 MXT_SUSPEND_DEEP_SLEEP = 0,
276 MXT_SUSPEND_T9_CTRL = 1,
279 /* Config update context */
280 struct mxt_cfg {
281 u8 *raw;
282 size_t raw_size;
283 off_t raw_pos;
285 u8 *mem;
286 size_t mem_size;
287 int start_ofs;
289 struct mxt_info info;
292 /* Each client has this additional data */
293 struct mxt_data {
294 struct i2c_client *client;
295 struct input_dev *input_dev;
296 char phys[64]; /* device physical location */
297 struct mxt_object *object_table;
298 struct mxt_info *info;
299 void *raw_info_block;
300 unsigned int irq;
301 unsigned int max_x;
302 unsigned int max_y;
303 bool invertx;
304 bool inverty;
305 bool xy_switch;
306 u8 xsize;
307 u8 ysize;
308 bool in_bootloader;
309 u16 mem_size;
310 u8 t100_aux_ampl;
311 u8 t100_aux_area;
312 u8 t100_aux_vect;
313 u8 max_reportid;
314 u32 config_crc;
315 u32 info_crc;
316 u8 bootloader_addr;
317 u8 *msg_buf;
318 u8 t6_status;
319 bool update_input;
320 u8 last_message_count;
321 u8 num_touchids;
322 u8 multitouch;
323 struct t7_config t7_cfg;
324 struct mxt_dbg dbg;
325 struct gpio_desc *reset_gpio;
327 /* Cached parameters from object table */
328 u16 T5_address;
329 u8 T5_msg_size;
330 u8 T6_reportid;
331 u16 T6_address;
332 u16 T7_address;
333 u16 T71_address;
334 u8 T9_reportid_min;
335 u8 T9_reportid_max;
336 u8 T19_reportid;
337 u16 T44_address;
338 u8 T100_reportid_min;
339 u8 T100_reportid_max;
341 /* for fw update in bootloader */
342 struct completion bl_completion;
344 /* for reset handling */
345 struct completion reset_completion;
347 /* for config update handling */
348 struct completion crc_completion;
350 u32 *t19_keymap;
351 unsigned int t19_num_keys;
353 enum mxt_suspend_mode suspend_mode;
356 struct mxt_vb2_buffer {
357 struct vb2_buffer vb;
358 struct list_head list;
361 static size_t mxt_obj_size(const struct mxt_object *obj)
363 return obj->size_minus_one + 1;
366 static size_t mxt_obj_instances(const struct mxt_object *obj)
368 return obj->instances_minus_one + 1;
371 static bool mxt_object_readable(unsigned int type)
373 switch (type) {
374 case MXT_GEN_COMMAND_T6:
375 case MXT_GEN_POWER_T7:
376 case MXT_GEN_ACQUIRE_T8:
377 case MXT_GEN_DATASOURCE_T53:
378 case MXT_TOUCH_MULTI_T9:
379 case MXT_TOUCH_KEYARRAY_T15:
380 case MXT_TOUCH_PROXIMITY_T23:
381 case MXT_TOUCH_PROXKEY_T52:
382 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
383 case MXT_PROCI_GRIPFACE_T20:
384 case MXT_PROCG_NOISE_T22:
385 case MXT_PROCI_ONETOUCH_T24:
386 case MXT_PROCI_TWOTOUCH_T27:
387 case MXT_PROCI_GRIP_T40:
388 case MXT_PROCI_PALM_T41:
389 case MXT_PROCI_TOUCHSUPPRESSION_T42:
390 case MXT_PROCI_STYLUS_T47:
391 case MXT_PROCG_NOISESUPPRESSION_T48:
392 case MXT_SPT_COMMSCONFIG_T18:
393 case MXT_SPT_GPIOPWM_T19:
394 case MXT_SPT_SELFTEST_T25:
395 case MXT_SPT_CTECONFIG_T28:
396 case MXT_SPT_USERDATA_T38:
397 case MXT_SPT_DIGITIZER_T43:
398 case MXT_SPT_CTECONFIG_T46:
399 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71:
400 return true;
401 default:
402 return false;
406 static void mxt_dump_message(struct mxt_data *data, u8 *message)
408 dev_dbg(&data->client->dev, "message: %*ph\n",
409 data->T5_msg_size, message);
412 static int mxt_wait_for_completion(struct mxt_data *data,
413 struct completion *comp,
414 unsigned int timeout_ms)
416 struct device *dev = &data->client->dev;
417 unsigned long timeout = msecs_to_jiffies(timeout_ms);
418 long ret;
420 ret = wait_for_completion_interruptible_timeout(comp, timeout);
421 if (ret < 0) {
422 return ret;
423 } else if (ret == 0) {
424 dev_err(dev, "Wait for completion timed out.\n");
425 return -ETIMEDOUT;
427 return 0;
430 static int mxt_bootloader_read(struct mxt_data *data,
431 u8 *val, unsigned int count)
433 int ret;
434 struct i2c_msg msg;
436 msg.addr = data->bootloader_addr;
437 msg.flags = data->client->flags & I2C_M_TEN;
438 msg.flags |= I2C_M_RD;
439 msg.len = count;
440 msg.buf = val;
442 ret = i2c_transfer(data->client->adapter, &msg, 1);
443 if (ret == 1) {
444 ret = 0;
445 } else {
446 ret = ret < 0 ? ret : -EIO;
447 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
448 __func__, ret);
451 return ret;
454 static int mxt_bootloader_write(struct mxt_data *data,
455 const u8 * const val, unsigned int count)
457 int ret;
458 struct i2c_msg msg;
460 msg.addr = data->bootloader_addr;
461 msg.flags = data->client->flags & I2C_M_TEN;
462 msg.len = count;
463 msg.buf = (u8 *)val;
465 ret = i2c_transfer(data->client->adapter, &msg, 1);
466 if (ret == 1) {
467 ret = 0;
468 } else {
469 ret = ret < 0 ? ret : -EIO;
470 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
471 __func__, ret);
474 return ret;
477 static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
479 u8 appmode = data->client->addr;
480 u8 bootloader;
481 u8 family_id = data->info ? data->info->family_id : 0;
483 switch (appmode) {
484 case 0x4a:
485 case 0x4b:
486 /* Chips after 1664S use different scheme */
487 if (retry || family_id >= 0xa2) {
488 bootloader = appmode - 0x24;
489 break;
491 /* Fall through - for normal case */
492 case 0x4c:
493 case 0x4d:
494 case 0x5a:
495 case 0x5b:
496 bootloader = appmode - 0x26;
497 break;
499 default:
500 dev_err(&data->client->dev,
501 "Appmode i2c address 0x%02x not found\n",
502 appmode);
503 return -EINVAL;
506 data->bootloader_addr = bootloader;
507 return 0;
510 static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
512 struct device *dev = &data->client->dev;
513 int error;
514 u8 val;
515 bool crc_failure;
517 error = mxt_lookup_bootloader_address(data, alt_address);
518 if (error)
519 return error;
521 error = mxt_bootloader_read(data, &val, 1);
522 if (error)
523 return error;
525 /* Check app crc fail mode */
526 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
528 dev_err(dev, "Detected bootloader, status:%02X%s\n",
529 val, crc_failure ? ", APP_CRC_FAIL" : "");
531 return 0;
534 static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
536 struct device *dev = &data->client->dev;
537 u8 buf[3];
539 if (val & MXT_BOOT_EXTENDED_ID) {
540 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
541 dev_err(dev, "%s: i2c failure\n", __func__);
542 return val;
545 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
547 return buf[0];
548 } else {
549 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
551 return val;
555 static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
556 bool wait)
558 struct device *dev = &data->client->dev;
559 u8 val;
560 int ret;
562 recheck:
563 if (wait) {
565 * In application update mode, the interrupt
566 * line signals state transitions. We must wait for the
567 * CHG assertion before reading the status byte.
568 * Once the status byte has been read, the line is deasserted.
570 ret = mxt_wait_for_completion(data, &data->bl_completion,
571 MXT_FW_CHG_TIMEOUT);
572 if (ret) {
574 * TODO: handle -ERESTARTSYS better by terminating
575 * fw update process before returning to userspace
576 * by writing length 0x000 to device (iff we are in
577 * WAITING_FRAME_DATA state).
579 dev_err(dev, "Update wait error %d\n", ret);
580 return ret;
584 ret = mxt_bootloader_read(data, &val, 1);
585 if (ret)
586 return ret;
588 if (state == MXT_WAITING_BOOTLOAD_CMD)
589 val = mxt_get_bootloader_version(data, val);
591 switch (state) {
592 case MXT_WAITING_BOOTLOAD_CMD:
593 case MXT_WAITING_FRAME_DATA:
594 case MXT_APP_CRC_FAIL:
595 val &= ~MXT_BOOT_STATUS_MASK;
596 break;
597 case MXT_FRAME_CRC_PASS:
598 if (val == MXT_FRAME_CRC_CHECK) {
599 goto recheck;
600 } else if (val == MXT_FRAME_CRC_FAIL) {
601 dev_err(dev, "Bootloader CRC fail\n");
602 return -EINVAL;
604 break;
605 default:
606 return -EINVAL;
609 if (val != state) {
610 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
611 val, state);
612 return -EINVAL;
615 return 0;
618 static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
620 int ret;
621 u8 buf[2];
623 if (unlock) {
624 buf[0] = MXT_UNLOCK_CMD_LSB;
625 buf[1] = MXT_UNLOCK_CMD_MSB;
626 } else {
627 buf[0] = 0x01;
628 buf[1] = 0x01;
631 ret = mxt_bootloader_write(data, buf, 2);
632 if (ret)
633 return ret;
635 return 0;
638 static int __mxt_read_reg(struct i2c_client *client,
639 u16 reg, u16 len, void *val)
641 struct i2c_msg xfer[2];
642 u8 buf[2];
643 int ret;
645 buf[0] = reg & 0xff;
646 buf[1] = (reg >> 8) & 0xff;
648 /* Write register */
649 xfer[0].addr = client->addr;
650 xfer[0].flags = 0;
651 xfer[0].len = 2;
652 xfer[0].buf = buf;
654 /* Read data */
655 xfer[1].addr = client->addr;
656 xfer[1].flags = I2C_M_RD;
657 xfer[1].len = len;
658 xfer[1].buf = val;
660 ret = i2c_transfer(client->adapter, xfer, 2);
661 if (ret == 2) {
662 ret = 0;
663 } else {
664 if (ret >= 0)
665 ret = -EIO;
666 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
667 __func__, ret);
670 return ret;
673 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
674 const void *val)
676 u8 *buf;
677 size_t count;
678 int ret;
680 count = len + 2;
681 buf = kmalloc(count, GFP_KERNEL);
682 if (!buf)
683 return -ENOMEM;
685 buf[0] = reg & 0xff;
686 buf[1] = (reg >> 8) & 0xff;
687 memcpy(&buf[2], val, len);
689 ret = i2c_master_send(client, buf, count);
690 if (ret == count) {
691 ret = 0;
692 } else {
693 if (ret >= 0)
694 ret = -EIO;
695 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
696 __func__, ret);
699 kfree(buf);
700 return ret;
703 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
705 return __mxt_write_reg(client, reg, 1, &val);
708 static struct mxt_object *
709 mxt_get_object(struct mxt_data *data, u8 type)
711 struct mxt_object *object;
712 int i;
714 for (i = 0; i < data->info->object_num; i++) {
715 object = data->object_table + i;
716 if (object->type == type)
717 return object;
720 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
721 return NULL;
724 static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
726 struct device *dev = &data->client->dev;
727 u8 status = msg[1];
728 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
730 if (crc != data->config_crc) {
731 data->config_crc = crc;
732 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
735 complete(&data->crc_completion);
737 /* Detect reset */
738 if (status & MXT_T6_STATUS_RESET)
739 complete(&data->reset_completion);
741 /* Output debug if status has changed */
742 if (status != data->t6_status)
743 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
744 status,
745 status == 0 ? " OK" : "",
746 status & MXT_T6_STATUS_RESET ? " RESET" : "",
747 status & MXT_T6_STATUS_OFL ? " OFL" : "",
748 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
749 status & MXT_T6_STATUS_CAL ? " CAL" : "",
750 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
751 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
753 /* Save current status */
754 data->t6_status = status;
757 static int mxt_write_object(struct mxt_data *data,
758 u8 type, u8 offset, u8 val)
760 struct mxt_object *object;
761 u16 reg;
763 object = mxt_get_object(data, type);
764 if (!object || offset >= mxt_obj_size(object))
765 return -EINVAL;
767 reg = object->start_address;
768 return mxt_write_reg(data->client, reg + offset, val);
771 static void mxt_input_button(struct mxt_data *data, u8 *message)
773 struct input_dev *input = data->input_dev;
774 int i;
776 for (i = 0; i < data->t19_num_keys; i++) {
777 if (data->t19_keymap[i] == KEY_RESERVED)
778 continue;
780 /* Active-low switch */
781 input_report_key(input, data->t19_keymap[i],
782 !(message[1] & BIT(i)));
786 static void mxt_input_sync(struct mxt_data *data)
788 input_mt_report_pointer_emulation(data->input_dev,
789 data->t19_num_keys);
790 input_sync(data->input_dev);
793 static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
795 struct device *dev = &data->client->dev;
796 struct input_dev *input_dev = data->input_dev;
797 int id;
798 u8 status;
799 int x;
800 int y;
801 int area;
802 int amplitude;
804 id = message[0] - data->T9_reportid_min;
805 status = message[1];
806 x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
807 y = (message[3] << 4) | ((message[4] & 0xf));
809 /* Handle 10/12 bit switching */
810 if (data->max_x < 1024)
811 x >>= 2;
812 if (data->max_y < 1024)
813 y >>= 2;
815 area = message[5];
816 amplitude = message[6];
818 dev_dbg(dev,
819 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
821 (status & MXT_T9_DETECT) ? 'D' : '.',
822 (status & MXT_T9_PRESS) ? 'P' : '.',
823 (status & MXT_T9_RELEASE) ? 'R' : '.',
824 (status & MXT_T9_MOVE) ? 'M' : '.',
825 (status & MXT_T9_VECTOR) ? 'V' : '.',
826 (status & MXT_T9_AMP) ? 'A' : '.',
827 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
828 (status & MXT_T9_UNGRIP) ? 'U' : '.',
829 x, y, area, amplitude);
831 input_mt_slot(input_dev, id);
833 if (status & MXT_T9_DETECT) {
835 * Multiple bits may be set if the host is slow to read
836 * the status messages, indicating all the events that
837 * have happened.
839 if (status & MXT_T9_RELEASE) {
840 input_mt_report_slot_state(input_dev,
841 MT_TOOL_FINGER, 0);
842 mxt_input_sync(data);
845 /* if active, pressure must be non-zero */
846 if (!amplitude)
847 amplitude = MXT_PRESSURE_DEFAULT;
849 /* Touch active */
850 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
851 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
852 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
853 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
854 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
855 } else {
856 /* Touch no longer active, close out slot */
857 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
860 data->update_input = true;
863 static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
865 struct device *dev = &data->client->dev;
866 struct input_dev *input_dev = data->input_dev;
867 int id;
868 u8 status;
869 u8 type = 0;
870 u16 x;
871 u16 y;
872 int distance = 0;
873 int tool = 0;
874 u8 major = 0;
875 u8 pressure = 0;
876 u8 orientation = 0;
878 id = message[0] - data->T100_reportid_min - 2;
880 /* ignore SCRSTATUS events */
881 if (id < 0)
882 return;
884 status = message[1];
885 x = get_unaligned_le16(&message[2]);
886 y = get_unaligned_le16(&message[4]);
888 if (status & MXT_T100_DETECT) {
889 type = (status & MXT_T100_TYPE_MASK) >> 4;
891 switch (type) {
892 case MXT_T100_TYPE_HOVERING_FINGER:
893 tool = MT_TOOL_FINGER;
894 distance = MXT_DISTANCE_HOVERING;
896 if (data->t100_aux_vect)
897 orientation = message[data->t100_aux_vect];
899 break;
901 case MXT_T100_TYPE_FINGER:
902 case MXT_T100_TYPE_GLOVE:
903 tool = MT_TOOL_FINGER;
904 distance = MXT_DISTANCE_ACTIVE_TOUCH;
906 if (data->t100_aux_area)
907 major = message[data->t100_aux_area];
909 if (data->t100_aux_ampl)
910 pressure = message[data->t100_aux_ampl];
912 if (data->t100_aux_vect)
913 orientation = message[data->t100_aux_vect];
915 break;
917 case MXT_T100_TYPE_PASSIVE_STYLUS:
918 tool = MT_TOOL_PEN;
921 * Passive stylus is reported with size zero so
922 * hardcode.
924 major = MXT_TOUCH_MAJOR_DEFAULT;
926 if (data->t100_aux_ampl)
927 pressure = message[data->t100_aux_ampl];
929 break;
931 case MXT_T100_TYPE_LARGE_TOUCH:
932 /* Ignore suppressed touch */
933 break;
935 default:
936 dev_dbg(dev, "Unexpected T100 type\n");
937 return;
942 * Values reported should be non-zero if tool is touching the
943 * device
945 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
946 pressure = MXT_PRESSURE_DEFAULT;
948 input_mt_slot(input_dev, id);
950 if (status & MXT_T100_DETECT) {
951 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
952 id, type, x, y, major, pressure, orientation);
954 input_mt_report_slot_state(input_dev, tool, 1);
955 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
956 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
957 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
958 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
959 input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
960 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
961 } else {
962 dev_dbg(dev, "[%u] release\n", id);
964 /* close out slot */
965 input_mt_report_slot_state(input_dev, 0, 0);
968 data->update_input = true;
971 static int mxt_proc_message(struct mxt_data *data, u8 *message)
973 u8 report_id = message[0];
975 if (report_id == MXT_RPTID_NOMSG)
976 return 0;
978 if (report_id == data->T6_reportid) {
979 mxt_proc_t6_messages(data, message);
980 } else if (!data->input_dev) {
982 * Do not report events if input device
983 * is not yet registered.
985 mxt_dump_message(data, message);
986 } else if (report_id >= data->T9_reportid_min &&
987 report_id <= data->T9_reportid_max) {
988 mxt_proc_t9_message(data, message);
989 } else if (report_id >= data->T100_reportid_min &&
990 report_id <= data->T100_reportid_max) {
991 mxt_proc_t100_message(data, message);
992 } else if (report_id == data->T19_reportid) {
993 mxt_input_button(data, message);
994 data->update_input = true;
995 } else {
996 mxt_dump_message(data, message);
999 return 1;
1002 static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
1004 struct device *dev = &data->client->dev;
1005 int ret;
1006 int i;
1007 u8 num_valid = 0;
1009 /* Safety check for msg_buf */
1010 if (count > data->max_reportid)
1011 return -EINVAL;
1013 /* Process remaining messages if necessary */
1014 ret = __mxt_read_reg(data->client, data->T5_address,
1015 data->T5_msg_size * count, data->msg_buf);
1016 if (ret) {
1017 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
1018 return ret;
1021 for (i = 0; i < count; i++) {
1022 ret = mxt_proc_message(data,
1023 data->msg_buf + data->T5_msg_size * i);
1025 if (ret == 1)
1026 num_valid++;
1029 /* return number of messages read */
1030 return num_valid;
1033 static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
1035 struct device *dev = &data->client->dev;
1036 int ret;
1037 u8 count, num_left;
1039 /* Read T44 and T5 together */
1040 ret = __mxt_read_reg(data->client, data->T44_address,
1041 data->T5_msg_size + 1, data->msg_buf);
1042 if (ret) {
1043 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
1044 return IRQ_NONE;
1047 count = data->msg_buf[0];
1050 * This condition may be caused by the CHG line being configured in
1051 * Mode 0. It results in unnecessary I2C operations but it is benign.
1053 if (count == 0)
1054 return IRQ_NONE;
1056 if (count > data->max_reportid) {
1057 dev_warn(dev, "T44 count %d exceeded max report id\n", count);
1058 count = data->max_reportid;
1061 /* Process first message */
1062 ret = mxt_proc_message(data, data->msg_buf + 1);
1063 if (ret < 0) {
1064 dev_warn(dev, "Unexpected invalid message\n");
1065 return IRQ_NONE;
1068 num_left = count - 1;
1070 /* Process remaining messages if necessary */
1071 if (num_left) {
1072 ret = mxt_read_and_process_messages(data, num_left);
1073 if (ret < 0)
1074 goto end;
1075 else if (ret != num_left)
1076 dev_warn(dev, "Unexpected invalid message\n");
1079 end:
1080 if (data->update_input) {
1081 mxt_input_sync(data);
1082 data->update_input = false;
1085 return IRQ_HANDLED;
1088 static int mxt_process_messages_until_invalid(struct mxt_data *data)
1090 struct device *dev = &data->client->dev;
1091 int count, read;
1092 u8 tries = 2;
1094 count = data->max_reportid;
1096 /* Read messages until we force an invalid */
1097 do {
1098 read = mxt_read_and_process_messages(data, count);
1099 if (read < count)
1100 return 0;
1101 } while (--tries);
1103 if (data->update_input) {
1104 mxt_input_sync(data);
1105 data->update_input = false;
1108 dev_err(dev, "CHG pin isn't cleared\n");
1109 return -EBUSY;
1112 static irqreturn_t mxt_process_messages(struct mxt_data *data)
1114 int total_handled, num_handled;
1115 u8 count = data->last_message_count;
1117 if (count < 1 || count > data->max_reportid)
1118 count = 1;
1120 /* include final invalid message */
1121 total_handled = mxt_read_and_process_messages(data, count + 1);
1122 if (total_handled < 0)
1123 return IRQ_NONE;
1124 /* if there were invalid messages, then we are done */
1125 else if (total_handled <= count)
1126 goto update_count;
1128 /* keep reading two msgs until one is invalid or reportid limit */
1129 do {
1130 num_handled = mxt_read_and_process_messages(data, 2);
1131 if (num_handled < 0)
1132 return IRQ_NONE;
1134 total_handled += num_handled;
1136 if (num_handled < 2)
1137 break;
1138 } while (total_handled < data->num_touchids);
1140 update_count:
1141 data->last_message_count = total_handled;
1143 if (data->update_input) {
1144 mxt_input_sync(data);
1145 data->update_input = false;
1148 return IRQ_HANDLED;
1151 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
1153 struct mxt_data *data = dev_id;
1155 if (data->in_bootloader) {
1156 /* bootloader state transition completion */
1157 complete(&data->bl_completion);
1158 return IRQ_HANDLED;
1161 if (!data->object_table)
1162 return IRQ_HANDLED;
1164 if (data->T44_address) {
1165 return mxt_process_messages_t44(data);
1166 } else {
1167 return mxt_process_messages(data);
1171 static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
1172 u8 value, bool wait)
1174 u16 reg;
1175 u8 command_register;
1176 int timeout_counter = 0;
1177 int ret;
1179 reg = data->T6_address + cmd_offset;
1181 ret = mxt_write_reg(data->client, reg, value);
1182 if (ret)
1183 return ret;
1185 if (!wait)
1186 return 0;
1188 do {
1189 msleep(20);
1190 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
1191 if (ret)
1192 return ret;
1193 } while (command_register != 0 && timeout_counter++ <= 100);
1195 if (timeout_counter > 100) {
1196 dev_err(&data->client->dev, "Command failed!\n");
1197 return -EIO;
1200 return 0;
1203 static int mxt_acquire_irq(struct mxt_data *data)
1205 int error;
1207 enable_irq(data->irq);
1209 error = mxt_process_messages_until_invalid(data);
1210 if (error)
1211 return error;
1213 return 0;
1216 static int mxt_soft_reset(struct mxt_data *data)
1218 struct device *dev = &data->client->dev;
1219 int ret = 0;
1221 dev_info(dev, "Resetting device\n");
1223 disable_irq(data->irq);
1225 reinit_completion(&data->reset_completion);
1227 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
1228 if (ret)
1229 return ret;
1231 /* Ignore CHG line for 100ms after reset */
1232 msleep(MXT_RESET_INVALID_CHG);
1234 mxt_acquire_irq(data);
1236 ret = mxt_wait_for_completion(data, &data->reset_completion,
1237 MXT_RESET_TIMEOUT);
1238 if (ret)
1239 return ret;
1241 return 0;
1244 static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
1247 * On failure, CRC is set to 0 and config will always be
1248 * downloaded.
1250 data->config_crc = 0;
1251 reinit_completion(&data->crc_completion);
1253 mxt_t6_command(data, cmd, value, true);
1256 * Wait for crc message. On failure, CRC is set to 0 and config will
1257 * always be downloaded.
1259 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
1262 static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
1264 static const unsigned int crcpoly = 0x80001B;
1265 u32 result;
1266 u32 data_word;
1268 data_word = (secondbyte << 8) | firstbyte;
1269 result = ((*crc << 1) ^ data_word);
1271 if (result & 0x1000000)
1272 result ^= crcpoly;
1274 *crc = result;
1277 static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
1279 u32 crc = 0;
1280 u8 *ptr = base + start_off;
1281 u8 *last_val = base + end_off - 1;
1283 if (end_off < start_off)
1284 return -EINVAL;
1286 while (ptr < last_val) {
1287 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
1288 ptr += 2;
1291 /* if len is odd, fill the last byte with 0 */
1292 if (ptr == last_val)
1293 mxt_calc_crc24(&crc, *ptr, 0);
1295 /* Mask to 24-bit */
1296 crc &= 0x00FFFFFF;
1298 return crc;
1301 static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
1303 struct device *dev = &data->client->dev;
1304 struct mxt_object *object;
1305 unsigned int type, instance, size, byte_offset;
1306 int offset;
1307 int ret;
1308 int i;
1309 u16 reg;
1310 u8 val;
1312 while (cfg->raw_pos < cfg->raw_size) {
1313 /* Read type, instance, length */
1314 ret = sscanf(cfg->raw + cfg->raw_pos, "%x %x %x%n",
1315 &type, &instance, &size, &offset);
1316 if (ret == 0) {
1317 /* EOF */
1318 break;
1319 } else if (ret != 3) {
1320 dev_err(dev, "Bad format: failed to parse object\n");
1321 return -EINVAL;
1323 cfg->raw_pos += offset;
1325 object = mxt_get_object(data, type);
1326 if (!object) {
1327 /* Skip object */
1328 for (i = 0; i < size; i++) {
1329 ret = sscanf(cfg->raw + cfg->raw_pos, "%hhx%n",
1330 &val, &offset);
1331 if (ret != 1) {
1332 dev_err(dev, "Bad format in T%d at %d\n",
1333 type, i);
1334 return -EINVAL;
1336 cfg->raw_pos += offset;
1338 continue;
1341 if (size > mxt_obj_size(object)) {
1343 * Either we are in fallback mode due to wrong
1344 * config or config from a later fw version,
1345 * or the file is corrupt or hand-edited.
1347 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1348 size - mxt_obj_size(object), type);
1349 } else if (mxt_obj_size(object) > size) {
1351 * If firmware is upgraded, new bytes may be added to
1352 * end of objects. It is generally forward compatible
1353 * to zero these bytes - previous behaviour will be
1354 * retained. However this does invalidate the CRC and
1355 * will force fallback mode until the configuration is
1356 * updated. We warn here but do nothing else - the
1357 * malloc has zeroed the entire configuration.
1359 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1360 mxt_obj_size(object) - size, type);
1363 if (instance >= mxt_obj_instances(object)) {
1364 dev_err(dev, "Object instances exceeded!\n");
1365 return -EINVAL;
1368 reg = object->start_address + mxt_obj_size(object) * instance;
1370 for (i = 0; i < size; i++) {
1371 ret = sscanf(cfg->raw + cfg->raw_pos, "%hhx%n",
1372 &val,
1373 &offset);
1374 if (ret != 1) {
1375 dev_err(dev, "Bad format in T%d at %d\n",
1376 type, i);
1377 return -EINVAL;
1379 cfg->raw_pos += offset;
1381 if (i > mxt_obj_size(object))
1382 continue;
1384 byte_offset = reg + i - cfg->start_ofs;
1386 if (byte_offset >= 0 && byte_offset < cfg->mem_size) {
1387 *(cfg->mem + byte_offset) = val;
1388 } else {
1389 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1390 reg, object->type, byte_offset);
1391 return -EINVAL;
1396 return 0;
1399 static int mxt_upload_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
1401 unsigned int byte_offset = 0;
1402 int error;
1404 /* Write configuration as blocks */
1405 while (byte_offset < cfg->mem_size) {
1406 unsigned int size = cfg->mem_size - byte_offset;
1408 if (size > MXT_MAX_BLOCK_WRITE)
1409 size = MXT_MAX_BLOCK_WRITE;
1411 error = __mxt_write_reg(data->client,
1412 cfg->start_ofs + byte_offset,
1413 size, cfg->mem + byte_offset);
1414 if (error) {
1415 dev_err(&data->client->dev,
1416 "Config write error, ret=%d\n", error);
1417 return error;
1420 byte_offset += size;
1423 return 0;
1426 static int mxt_init_t7_power_cfg(struct mxt_data *data);
1429 * mxt_update_cfg - download configuration to chip
1431 * Atmel Raw Config File Format
1433 * The first four lines of the raw config file contain:
1434 * 1) Version
1435 * 2) Chip ID Information (first 7 bytes of device memory)
1436 * 3) Chip Information Block 24-bit CRC Checksum
1437 * 4) Chip Configuration 24-bit CRC Checksum
1439 * The rest of the file consists of one line per object instance:
1440 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1442 * <TYPE> - 2-byte object type as hex
1443 * <INSTANCE> - 2-byte object instance number as hex
1444 * <SIZE> - 2-byte object size as hex
1445 * <CONTENTS> - array of <SIZE> 1-byte hex values
1447 static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw)
1449 struct device *dev = &data->client->dev;
1450 struct mxt_cfg cfg;
1451 int ret;
1452 int offset;
1453 int i;
1454 u32 info_crc, config_crc, calculated_crc;
1455 u16 crc_start = 0;
1457 /* Make zero terminated copy of the OBP_RAW file */
1458 cfg.raw = kmemdup_nul(fw->data, fw->size, GFP_KERNEL);
1459 if (!cfg.raw)
1460 return -ENOMEM;
1462 cfg.raw_size = fw->size;
1464 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
1466 if (strncmp(cfg.raw, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
1467 dev_err(dev, "Unrecognised config file\n");
1468 ret = -EINVAL;
1469 goto release_raw;
1472 cfg.raw_pos = strlen(MXT_CFG_MAGIC);
1474 /* Load information block and check */
1475 for (i = 0; i < sizeof(struct mxt_info); i++) {
1476 ret = sscanf(cfg.raw + cfg.raw_pos, "%hhx%n",
1477 (unsigned char *)&cfg.info + i,
1478 &offset);
1479 if (ret != 1) {
1480 dev_err(dev, "Bad format\n");
1481 ret = -EINVAL;
1482 goto release_raw;
1485 cfg.raw_pos += offset;
1488 if (cfg.info.family_id != data->info->family_id) {
1489 dev_err(dev, "Family ID mismatch!\n");
1490 ret = -EINVAL;
1491 goto release_raw;
1494 if (cfg.info.variant_id != data->info->variant_id) {
1495 dev_err(dev, "Variant ID mismatch!\n");
1496 ret = -EINVAL;
1497 goto release_raw;
1500 /* Read CRCs */
1501 ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &info_crc, &offset);
1502 if (ret != 1) {
1503 dev_err(dev, "Bad format: failed to parse Info CRC\n");
1504 ret = -EINVAL;
1505 goto release_raw;
1507 cfg.raw_pos += offset;
1509 ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &config_crc, &offset);
1510 if (ret != 1) {
1511 dev_err(dev, "Bad format: failed to parse Config CRC\n");
1512 ret = -EINVAL;
1513 goto release_raw;
1515 cfg.raw_pos += offset;
1518 * The Info Block CRC is calculated over mxt_info and the object
1519 * table. If it does not match then we are trying to load the
1520 * configuration from a different chip or firmware version, so
1521 * the configuration CRC is invalid anyway.
1523 if (info_crc == data->info_crc) {
1524 if (config_crc == 0 || data->config_crc == 0) {
1525 dev_info(dev, "CRC zero, attempting to apply config\n");
1526 } else if (config_crc == data->config_crc) {
1527 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
1528 data->config_crc);
1529 return 0;
1530 } else {
1531 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1532 data->config_crc, config_crc);
1534 } else {
1535 dev_warn(dev,
1536 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1537 data->info_crc, info_crc);
1540 /* Malloc memory to store configuration */
1541 cfg.start_ofs = MXT_OBJECT_START +
1542 data->info->object_num * sizeof(struct mxt_object) +
1543 MXT_INFO_CHECKSUM_SIZE;
1544 cfg.mem_size = data->mem_size - cfg.start_ofs;
1545 cfg.mem = kzalloc(cfg.mem_size, GFP_KERNEL);
1546 if (!cfg.mem) {
1547 ret = -ENOMEM;
1548 goto release_raw;
1551 ret = mxt_prepare_cfg_mem(data, &cfg);
1552 if (ret)
1553 goto release_mem;
1555 /* Calculate crc of the received configs (not the raw config file) */
1556 if (data->T71_address)
1557 crc_start = data->T71_address;
1558 else if (data->T7_address)
1559 crc_start = data->T7_address;
1560 else
1561 dev_warn(dev, "Could not find CRC start\n");
1563 if (crc_start > cfg.start_ofs) {
1564 calculated_crc = mxt_calculate_crc(cfg.mem,
1565 crc_start - cfg.start_ofs,
1566 cfg.mem_size);
1568 if (config_crc > 0 && config_crc != calculated_crc)
1569 dev_warn(dev, "Config CRC in file inconsistent, calculated=%06X, file=%06X\n",
1570 calculated_crc, config_crc);
1573 ret = mxt_upload_cfg_mem(data, &cfg);
1574 if (ret)
1575 goto release_mem;
1577 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1579 ret = mxt_soft_reset(data);
1580 if (ret)
1581 goto release_mem;
1583 dev_info(dev, "Config successfully updated\n");
1585 /* T7 config may have changed */
1586 mxt_init_t7_power_cfg(data);
1588 release_mem:
1589 kfree(cfg.mem);
1590 release_raw:
1591 kfree(cfg.raw);
1592 return ret;
1595 static void mxt_free_input_device(struct mxt_data *data)
1597 if (data->input_dev) {
1598 input_unregister_device(data->input_dev);
1599 data->input_dev = NULL;
1603 static void mxt_free_object_table(struct mxt_data *data)
1605 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1606 video_unregister_device(&data->dbg.vdev);
1607 v4l2_device_unregister(&data->dbg.v4l2);
1608 #endif
1609 data->object_table = NULL;
1610 data->info = NULL;
1611 kfree(data->raw_info_block);
1612 data->raw_info_block = NULL;
1613 kfree(data->msg_buf);
1614 data->msg_buf = NULL;
1615 data->T5_address = 0;
1616 data->T5_msg_size = 0;
1617 data->T6_reportid = 0;
1618 data->T7_address = 0;
1619 data->T71_address = 0;
1620 data->T9_reportid_min = 0;
1621 data->T9_reportid_max = 0;
1622 data->T19_reportid = 0;
1623 data->T44_address = 0;
1624 data->T100_reportid_min = 0;
1625 data->T100_reportid_max = 0;
1626 data->max_reportid = 0;
1629 static int mxt_parse_object_table(struct mxt_data *data,
1630 struct mxt_object *object_table)
1632 struct i2c_client *client = data->client;
1633 int i;
1634 u8 reportid;
1635 u16 end_address;
1637 /* Valid Report IDs start counting from 1 */
1638 reportid = 1;
1639 data->mem_size = 0;
1640 for (i = 0; i < data->info->object_num; i++) {
1641 struct mxt_object *object = object_table + i;
1642 u8 min_id, max_id;
1644 le16_to_cpus(&object->start_address);
1646 if (object->num_report_ids) {
1647 min_id = reportid;
1648 reportid += object->num_report_ids *
1649 mxt_obj_instances(object);
1650 max_id = reportid - 1;
1651 } else {
1652 min_id = 0;
1653 max_id = 0;
1656 dev_dbg(&data->client->dev,
1657 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
1658 object->type, object->start_address,
1659 mxt_obj_size(object), mxt_obj_instances(object),
1660 min_id, max_id);
1662 switch (object->type) {
1663 case MXT_GEN_MESSAGE_T5:
1664 if (data->info->family_id == 0x80 &&
1665 data->info->version < 0x20) {
1667 * On mXT224 firmware versions prior to V2.0
1668 * read and discard unused CRC byte otherwise
1669 * DMA reads are misaligned.
1671 data->T5_msg_size = mxt_obj_size(object);
1672 } else {
1673 /* CRC not enabled, so skip last byte */
1674 data->T5_msg_size = mxt_obj_size(object) - 1;
1676 data->T5_address = object->start_address;
1677 break;
1678 case MXT_GEN_COMMAND_T6:
1679 data->T6_reportid = min_id;
1680 data->T6_address = object->start_address;
1681 break;
1682 case MXT_GEN_POWER_T7:
1683 data->T7_address = object->start_address;
1684 break;
1685 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71:
1686 data->T71_address = object->start_address;
1687 break;
1688 case MXT_TOUCH_MULTI_T9:
1689 data->multitouch = MXT_TOUCH_MULTI_T9;
1690 /* Only handle messages from first T9 instance */
1691 data->T9_reportid_min = min_id;
1692 data->T9_reportid_max = min_id +
1693 object->num_report_ids - 1;
1694 data->num_touchids = object->num_report_ids;
1695 break;
1696 case MXT_SPT_MESSAGECOUNT_T44:
1697 data->T44_address = object->start_address;
1698 break;
1699 case MXT_SPT_GPIOPWM_T19:
1700 data->T19_reportid = min_id;
1701 break;
1702 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1703 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
1704 data->T100_reportid_min = min_id;
1705 data->T100_reportid_max = max_id;
1706 /* first two report IDs reserved */
1707 data->num_touchids = object->num_report_ids - 2;
1708 break;
1711 end_address = object->start_address
1712 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1714 if (end_address >= data->mem_size)
1715 data->mem_size = end_address + 1;
1718 /* Store maximum reportid */
1719 data->max_reportid = reportid;
1721 /* If T44 exists, T5 position has to be directly after */
1722 if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
1723 dev_err(&client->dev, "Invalid T44 position\n");
1724 return -EINVAL;
1727 data->msg_buf = kcalloc(data->max_reportid,
1728 data->T5_msg_size, GFP_KERNEL);
1729 if (!data->msg_buf)
1730 return -ENOMEM;
1732 return 0;
1735 static int mxt_read_info_block(struct mxt_data *data)
1737 struct i2c_client *client = data->client;
1738 int error;
1739 size_t size;
1740 void *id_buf, *buf;
1741 uint8_t num_objects;
1742 u32 calculated_crc;
1743 u8 *crc_ptr;
1745 /* If info block already allocated, free it */
1746 if (data->raw_info_block)
1747 mxt_free_object_table(data);
1749 /* Read 7-byte ID information block starting at address 0 */
1750 size = sizeof(struct mxt_info);
1751 id_buf = kzalloc(size, GFP_KERNEL);
1752 if (!id_buf)
1753 return -ENOMEM;
1755 error = __mxt_read_reg(client, 0, size, id_buf);
1756 if (error)
1757 goto err_free_mem;
1759 /* Resize buffer to give space for rest of info block */
1760 num_objects = ((struct mxt_info *)id_buf)->object_num;
1761 size += (num_objects * sizeof(struct mxt_object))
1762 + MXT_INFO_CHECKSUM_SIZE;
1764 buf = krealloc(id_buf, size, GFP_KERNEL);
1765 if (!buf) {
1766 error = -ENOMEM;
1767 goto err_free_mem;
1769 id_buf = buf;
1771 /* Read rest of info block */
1772 error = __mxt_read_reg(client, MXT_OBJECT_START,
1773 size - MXT_OBJECT_START,
1774 id_buf + MXT_OBJECT_START);
1775 if (error)
1776 goto err_free_mem;
1778 /* Extract & calculate checksum */
1779 crc_ptr = id_buf + size - MXT_INFO_CHECKSUM_SIZE;
1780 data->info_crc = crc_ptr[0] | (crc_ptr[1] << 8) | (crc_ptr[2] << 16);
1782 calculated_crc = mxt_calculate_crc(id_buf, 0,
1783 size - MXT_INFO_CHECKSUM_SIZE);
1786 * CRC mismatch can be caused by data corruption due to I2C comms
1787 * issue or else device is not using Object Based Protocol (eg i2c-hid)
1789 if ((data->info_crc == 0) || (data->info_crc != calculated_crc)) {
1790 dev_err(&client->dev,
1791 "Info Block CRC error calculated=0x%06X read=0x%06X\n",
1792 calculated_crc, data->info_crc);
1793 error = -EIO;
1794 goto err_free_mem;
1797 data->raw_info_block = id_buf;
1798 data->info = (struct mxt_info *)id_buf;
1800 dev_info(&client->dev,
1801 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1802 data->info->family_id, data->info->variant_id,
1803 data->info->version >> 4, data->info->version & 0xf,
1804 data->info->build, data->info->object_num);
1806 /* Parse object table information */
1807 error = mxt_parse_object_table(data, id_buf + MXT_OBJECT_START);
1808 if (error) {
1809 dev_err(&client->dev, "Error %d parsing object table\n", error);
1810 mxt_free_object_table(data);
1811 goto err_free_mem;
1814 data->object_table = (struct mxt_object *)(id_buf + MXT_OBJECT_START);
1816 return 0;
1818 err_free_mem:
1819 kfree(id_buf);
1820 return error;
1823 static int mxt_read_t9_resolution(struct mxt_data *data)
1825 struct i2c_client *client = data->client;
1826 int error;
1827 struct t9_range range;
1828 unsigned char orient;
1829 struct mxt_object *object;
1831 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1832 if (!object)
1833 return -EINVAL;
1835 error = __mxt_read_reg(client,
1836 object->start_address + MXT_T9_XSIZE,
1837 sizeof(data->xsize), &data->xsize);
1838 if (error)
1839 return error;
1841 error = __mxt_read_reg(client,
1842 object->start_address + MXT_T9_YSIZE,
1843 sizeof(data->ysize), &data->ysize);
1844 if (error)
1845 return error;
1847 error = __mxt_read_reg(client,
1848 object->start_address + MXT_T9_RANGE,
1849 sizeof(range), &range);
1850 if (error)
1851 return error;
1853 data->max_x = get_unaligned_le16(&range.x);
1854 data->max_y = get_unaligned_le16(&range.y);
1856 error = __mxt_read_reg(client,
1857 object->start_address + MXT_T9_ORIENT,
1858 1, &orient);
1859 if (error)
1860 return error;
1862 data->xy_switch = orient & MXT_T9_ORIENT_SWITCH;
1863 data->invertx = orient & MXT_T9_ORIENT_INVERTX;
1864 data->inverty = orient & MXT_T9_ORIENT_INVERTY;
1866 return 0;
1869 static int mxt_read_t100_config(struct mxt_data *data)
1871 struct i2c_client *client = data->client;
1872 int error;
1873 struct mxt_object *object;
1874 u16 range_x, range_y;
1875 u8 cfg, tchaux;
1876 u8 aux;
1878 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
1879 if (!object)
1880 return -EINVAL;
1882 /* read touchscreen dimensions */
1883 error = __mxt_read_reg(client,
1884 object->start_address + MXT_T100_XRANGE,
1885 sizeof(range_x), &range_x);
1886 if (error)
1887 return error;
1889 data->max_x = get_unaligned_le16(&range_x);
1891 error = __mxt_read_reg(client,
1892 object->start_address + MXT_T100_YRANGE,
1893 sizeof(range_y), &range_y);
1894 if (error)
1895 return error;
1897 data->max_y = get_unaligned_le16(&range_y);
1899 error = __mxt_read_reg(client,
1900 object->start_address + MXT_T100_XSIZE,
1901 sizeof(data->xsize), &data->xsize);
1902 if (error)
1903 return error;
1905 error = __mxt_read_reg(client,
1906 object->start_address + MXT_T100_YSIZE,
1907 sizeof(data->ysize), &data->ysize);
1908 if (error)
1909 return error;
1911 /* read orientation config */
1912 error = __mxt_read_reg(client,
1913 object->start_address + MXT_T100_CFG1,
1914 1, &cfg);
1915 if (error)
1916 return error;
1918 data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY;
1919 data->invertx = cfg & MXT_T100_CFG_INVERTX;
1920 data->inverty = cfg & MXT_T100_CFG_INVERTY;
1922 /* allocate aux bytes */
1923 error = __mxt_read_reg(client,
1924 object->start_address + MXT_T100_TCHAUX,
1925 1, &tchaux);
1926 if (error)
1927 return error;
1929 aux = 6;
1931 if (tchaux & MXT_T100_TCHAUX_VECT)
1932 data->t100_aux_vect = aux++;
1934 if (tchaux & MXT_T100_TCHAUX_AMPL)
1935 data->t100_aux_ampl = aux++;
1937 if (tchaux & MXT_T100_TCHAUX_AREA)
1938 data->t100_aux_area = aux++;
1940 dev_dbg(&client->dev,
1941 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1942 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
1944 return 0;
1947 static int mxt_input_open(struct input_dev *dev);
1948 static void mxt_input_close(struct input_dev *dev);
1950 static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
1951 struct mxt_data *data)
1953 int i;
1955 input_dev->name = "Atmel maXTouch Touchpad";
1957 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1959 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1960 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1961 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1962 MXT_PIXELS_PER_MM);
1963 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1964 MXT_PIXELS_PER_MM);
1966 for (i = 0; i < data->t19_num_keys; i++)
1967 if (data->t19_keymap[i] != KEY_RESERVED)
1968 input_set_capability(input_dev, EV_KEY,
1969 data->t19_keymap[i]);
1972 static int mxt_initialize_input_device(struct mxt_data *data)
1974 struct device *dev = &data->client->dev;
1975 struct input_dev *input_dev;
1976 int error;
1977 unsigned int num_mt_slots;
1978 unsigned int mt_flags = 0;
1980 switch (data->multitouch) {
1981 case MXT_TOUCH_MULTI_T9:
1982 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1983 error = mxt_read_t9_resolution(data);
1984 if (error)
1985 dev_warn(dev, "Failed to initialize T9 resolution\n");
1986 break;
1988 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1989 num_mt_slots = data->num_touchids;
1990 error = mxt_read_t100_config(data);
1991 if (error)
1992 dev_warn(dev, "Failed to read T100 config\n");
1993 break;
1995 default:
1996 dev_err(dev, "Invalid multitouch object\n");
1997 return -EINVAL;
2000 /* Handle default values and orientation switch */
2001 if (data->max_x == 0)
2002 data->max_x = 1023;
2004 if (data->max_y == 0)
2005 data->max_y = 1023;
2007 if (data->xy_switch)
2008 swap(data->max_x, data->max_y);
2010 dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
2012 /* Register input device */
2013 input_dev = input_allocate_device();
2014 if (!input_dev)
2015 return -ENOMEM;
2017 input_dev->name = "Atmel maXTouch Touchscreen";
2018 input_dev->phys = data->phys;
2019 input_dev->id.bustype = BUS_I2C;
2020 input_dev->dev.parent = dev;
2021 input_dev->open = mxt_input_open;
2022 input_dev->close = mxt_input_close;
2024 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
2026 /* For single touch */
2027 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
2028 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
2030 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2031 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2032 data->t100_aux_ampl)) {
2033 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
2036 /* If device has buttons we assume it is a touchpad */
2037 if (data->t19_num_keys) {
2038 mxt_set_up_as_touchpad(input_dev, data);
2039 mt_flags |= INPUT_MT_POINTER;
2040 } else {
2041 mt_flags |= INPUT_MT_DIRECT;
2044 /* For multi touch */
2045 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
2046 if (error) {
2047 dev_err(dev, "Error %d initialising slots\n", error);
2048 goto err_free_mem;
2051 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
2052 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
2053 0, MT_TOOL_MAX, 0, 0);
2054 input_set_abs_params(input_dev, ABS_MT_DISTANCE,
2055 MXT_DISTANCE_ACTIVE_TOUCH,
2056 MXT_DISTANCE_HOVERING,
2057 0, 0);
2060 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
2061 0, data->max_x, 0, 0);
2062 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
2063 0, data->max_y, 0, 0);
2065 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2066 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2067 data->t100_aux_area)) {
2068 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
2069 0, MXT_MAX_AREA, 0, 0);
2072 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2073 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2074 data->t100_aux_ampl)) {
2075 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
2076 0, 255, 0, 0);
2079 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2080 data->t100_aux_vect) {
2081 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2082 0, 255, 0, 0);
2085 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2086 data->t100_aux_vect) {
2087 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2088 0, 255, 0, 0);
2091 input_set_drvdata(input_dev, data);
2093 error = input_register_device(input_dev);
2094 if (error) {
2095 dev_err(dev, "Error %d registering input device\n", error);
2096 goto err_free_mem;
2099 data->input_dev = input_dev;
2101 return 0;
2103 err_free_mem:
2104 input_free_device(input_dev);
2105 return error;
2108 static int mxt_configure_objects(struct mxt_data *data,
2109 const struct firmware *cfg);
2111 static void mxt_config_cb(const struct firmware *cfg, void *ctx)
2113 mxt_configure_objects(ctx, cfg);
2114 release_firmware(cfg);
2117 static int mxt_initialize(struct mxt_data *data)
2119 struct i2c_client *client = data->client;
2120 int recovery_attempts = 0;
2121 int error;
2123 while (1) {
2124 error = mxt_read_info_block(data);
2125 if (!error)
2126 break;
2128 /* Check bootloader state */
2129 error = mxt_probe_bootloader(data, false);
2130 if (error) {
2131 dev_info(&client->dev, "Trying alternate bootloader address\n");
2132 error = mxt_probe_bootloader(data, true);
2133 if (error) {
2134 /* Chip is not in appmode or bootloader mode */
2135 return error;
2139 /* OK, we are in bootloader, see if we can recover */
2140 if (++recovery_attempts > 1) {
2141 dev_err(&client->dev, "Could not recover from bootloader mode\n");
2143 * We can reflash from this state, so do not
2144 * abort initialization.
2146 data->in_bootloader = true;
2147 return 0;
2150 /* Attempt to exit bootloader into app mode */
2151 mxt_send_bootloader_cmd(data, false);
2152 msleep(MXT_FW_RESET_TIME);
2155 error = mxt_acquire_irq(data);
2156 if (error)
2157 return error;
2159 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
2160 &client->dev, GFP_KERNEL, data,
2161 mxt_config_cb);
2162 if (error) {
2163 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
2164 error);
2165 return error;
2168 return 0;
2171 static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
2173 struct device *dev = &data->client->dev;
2174 int error;
2175 struct t7_config *new_config;
2176 struct t7_config deepsleep = { .active = 0, .idle = 0 };
2178 if (sleep == MXT_POWER_CFG_DEEPSLEEP)
2179 new_config = &deepsleep;
2180 else
2181 new_config = &data->t7_cfg;
2183 error = __mxt_write_reg(data->client, data->T7_address,
2184 sizeof(data->t7_cfg), new_config);
2185 if (error)
2186 return error;
2188 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
2189 new_config->active, new_config->idle);
2191 return 0;
2194 static int mxt_init_t7_power_cfg(struct mxt_data *data)
2196 struct device *dev = &data->client->dev;
2197 int error;
2198 bool retry = false;
2200 recheck:
2201 error = __mxt_read_reg(data->client, data->T7_address,
2202 sizeof(data->t7_cfg), &data->t7_cfg);
2203 if (error)
2204 return error;
2206 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
2207 if (!retry) {
2208 dev_dbg(dev, "T7 cfg zero, resetting\n");
2209 mxt_soft_reset(data);
2210 retry = true;
2211 goto recheck;
2212 } else {
2213 dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
2214 data->t7_cfg.active = 20;
2215 data->t7_cfg.idle = 100;
2216 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2220 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
2221 data->t7_cfg.active, data->t7_cfg.idle);
2222 return 0;
2225 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2226 static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x,
2227 unsigned int y)
2229 struct mxt_info *info = data->info;
2230 struct mxt_dbg *dbg = &data->dbg;
2231 unsigned int ofs, page;
2232 unsigned int col = 0;
2233 unsigned int col_width;
2235 if (info->family_id == MXT_FAMILY_1386) {
2236 col_width = info->matrix_ysize / MXT1386_COLUMNS;
2237 col = y / col_width;
2238 y = y % col_width;
2239 } else {
2240 col_width = info->matrix_ysize;
2243 ofs = (y + (x * col_width)) * sizeof(u16);
2244 page = ofs / MXT_DIAGNOSTIC_SIZE;
2245 ofs %= MXT_DIAGNOSTIC_SIZE;
2247 if (info->family_id == MXT_FAMILY_1386)
2248 page += col * MXT1386_PAGES_PER_COLUMN;
2250 return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]);
2253 static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf)
2255 struct mxt_dbg *dbg = &data->dbg;
2256 unsigned int x = 0;
2257 unsigned int y = 0;
2258 unsigned int i, rx, ry;
2260 for (i = 0; i < dbg->t37_nodes; i++) {
2261 /* Handle orientation */
2262 rx = data->xy_switch ? y : x;
2263 ry = data->xy_switch ? x : y;
2264 rx = data->invertx ? (data->xsize - 1 - rx) : rx;
2265 ry = data->inverty ? (data->ysize - 1 - ry) : ry;
2267 outbuf[i] = mxt_get_debug_value(data, rx, ry);
2269 /* Next value */
2270 if (++x >= (data->xy_switch ? data->ysize : data->xsize)) {
2271 x = 0;
2272 y++;
2276 return 0;
2279 static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode,
2280 u16 *outbuf)
2282 struct mxt_dbg *dbg = &data->dbg;
2283 int retries = 0;
2284 int page;
2285 int ret;
2286 u8 cmd = mode;
2287 struct t37_debug *p;
2288 u8 cmd_poll;
2290 for (page = 0; page < dbg->t37_pages; page++) {
2291 p = dbg->t37_buf + page;
2293 ret = mxt_write_reg(data->client, dbg->diag_cmd_address,
2294 cmd);
2295 if (ret)
2296 return ret;
2298 retries = 0;
2299 msleep(20);
2300 wait_cmd:
2301 /* Read back command byte */
2302 ret = __mxt_read_reg(data->client, dbg->diag_cmd_address,
2303 sizeof(cmd_poll), &cmd_poll);
2304 if (ret)
2305 return ret;
2307 /* Field is cleared once the command has been processed */
2308 if (cmd_poll) {
2309 if (retries++ > 100)
2310 return -EINVAL;
2312 msleep(20);
2313 goto wait_cmd;
2316 /* Read T37 page */
2317 ret = __mxt_read_reg(data->client, dbg->t37_address,
2318 sizeof(struct t37_debug), p);
2319 if (ret)
2320 return ret;
2322 if (p->mode != mode || p->page != page) {
2323 dev_err(&data->client->dev, "T37 page mismatch\n");
2324 return -EINVAL;
2327 dev_dbg(&data->client->dev, "%s page:%d retries:%d\n",
2328 __func__, page, retries);
2330 /* For remaining pages, write PAGEUP rather than mode */
2331 cmd = MXT_DIAGNOSTIC_PAGEUP;
2334 return mxt_convert_debug_pages(data, outbuf);
2337 static int mxt_queue_setup(struct vb2_queue *q,
2338 unsigned int *nbuffers, unsigned int *nplanes,
2339 unsigned int sizes[], struct device *alloc_devs[])
2341 struct mxt_data *data = q->drv_priv;
2342 size_t size = data->dbg.t37_nodes * sizeof(u16);
2344 if (*nplanes)
2345 return sizes[0] < size ? -EINVAL : 0;
2347 *nplanes = 1;
2348 sizes[0] = size;
2350 return 0;
2353 static void mxt_buffer_queue(struct vb2_buffer *vb)
2355 struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue);
2356 u16 *ptr;
2357 int ret;
2358 u8 mode;
2360 ptr = vb2_plane_vaddr(vb, 0);
2361 if (!ptr) {
2362 dev_err(&data->client->dev, "Error acquiring frame ptr\n");
2363 goto fault;
2366 switch (data->dbg.input) {
2367 case MXT_V4L_INPUT_DELTAS:
2368 default:
2369 mode = MXT_DIAGNOSTIC_DELTAS;
2370 break;
2372 case MXT_V4L_INPUT_REFS:
2373 mode = MXT_DIAGNOSTIC_REFS;
2374 break;
2377 ret = mxt_read_diagnostic_debug(data, mode, ptr);
2378 if (ret)
2379 goto fault;
2381 vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16));
2382 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2383 return;
2385 fault:
2386 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2389 /* V4L2 structures */
2390 static const struct vb2_ops mxt_queue_ops = {
2391 .queue_setup = mxt_queue_setup,
2392 .buf_queue = mxt_buffer_queue,
2393 .wait_prepare = vb2_ops_wait_prepare,
2394 .wait_finish = vb2_ops_wait_finish,
2397 static const struct vb2_queue mxt_queue = {
2398 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2399 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
2400 .buf_struct_size = sizeof(struct mxt_vb2_buffer),
2401 .ops = &mxt_queue_ops,
2402 .mem_ops = &vb2_vmalloc_memops,
2403 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
2404 .min_buffers_needed = 1,
2407 static int mxt_vidioc_querycap(struct file *file, void *priv,
2408 struct v4l2_capability *cap)
2410 struct mxt_data *data = video_drvdata(file);
2412 strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver));
2413 strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card));
2414 snprintf(cap->bus_info, sizeof(cap->bus_info),
2415 "I2C:%s", dev_name(&data->client->dev));
2416 return 0;
2419 static int mxt_vidioc_enum_input(struct file *file, void *priv,
2420 struct v4l2_input *i)
2422 if (i->index >= MXT_V4L_INPUT_MAX)
2423 return -EINVAL;
2425 i->type = V4L2_INPUT_TYPE_TOUCH;
2427 switch (i->index) {
2428 case MXT_V4L_INPUT_REFS:
2429 strlcpy(i->name, "Mutual Capacitance References",
2430 sizeof(i->name));
2431 break;
2432 case MXT_V4L_INPUT_DELTAS:
2433 strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name));
2434 break;
2437 return 0;
2440 static int mxt_set_input(struct mxt_data *data, unsigned int i)
2442 struct v4l2_pix_format *f = &data->dbg.format;
2444 if (i >= MXT_V4L_INPUT_MAX)
2445 return -EINVAL;
2447 if (i == MXT_V4L_INPUT_DELTAS)
2448 f->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2449 else
2450 f->pixelformat = V4L2_TCH_FMT_TU16;
2452 f->width = data->xy_switch ? data->ysize : data->xsize;
2453 f->height = data->xy_switch ? data->xsize : data->ysize;
2454 f->field = V4L2_FIELD_NONE;
2455 f->colorspace = V4L2_COLORSPACE_RAW;
2456 f->bytesperline = f->width * sizeof(u16);
2457 f->sizeimage = f->width * f->height * sizeof(u16);
2459 data->dbg.input = i;
2461 return 0;
2464 static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i)
2466 return mxt_set_input(video_drvdata(file), i);
2469 static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
2471 struct mxt_data *data = video_drvdata(file);
2473 *i = data->dbg.input;
2475 return 0;
2478 static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f)
2480 struct mxt_data *data = video_drvdata(file);
2482 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2483 f->fmt.pix = data->dbg.format;
2485 return 0;
2488 static int mxt_vidioc_enum_fmt(struct file *file, void *priv,
2489 struct v4l2_fmtdesc *fmt)
2491 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2492 return -EINVAL;
2494 switch (fmt->index) {
2495 case 0:
2496 fmt->pixelformat = V4L2_TCH_FMT_TU16;
2497 break;
2499 case 1:
2500 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2501 break;
2503 default:
2504 return -EINVAL;
2507 return 0;
2510 static int mxt_vidioc_g_parm(struct file *file, void *fh,
2511 struct v4l2_streamparm *a)
2513 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2514 return -EINVAL;
2516 a->parm.capture.readbuffers = 1;
2517 a->parm.capture.timeperframe.numerator = 1;
2518 a->parm.capture.timeperframe.denominator = 10;
2519 return 0;
2522 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = {
2523 .vidioc_querycap = mxt_vidioc_querycap,
2525 .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt,
2526 .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt,
2527 .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt,
2528 .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt,
2529 .vidioc_g_parm = mxt_vidioc_g_parm,
2531 .vidioc_enum_input = mxt_vidioc_enum_input,
2532 .vidioc_g_input = mxt_vidioc_g_input,
2533 .vidioc_s_input = mxt_vidioc_s_input,
2535 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2536 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2537 .vidioc_querybuf = vb2_ioctl_querybuf,
2538 .vidioc_qbuf = vb2_ioctl_qbuf,
2539 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2540 .vidioc_expbuf = vb2_ioctl_expbuf,
2542 .vidioc_streamon = vb2_ioctl_streamon,
2543 .vidioc_streamoff = vb2_ioctl_streamoff,
2546 static const struct video_device mxt_video_device = {
2547 .name = "Atmel maxTouch",
2548 .fops = &mxt_video_fops,
2549 .ioctl_ops = &mxt_video_ioctl_ops,
2550 .release = video_device_release_empty,
2551 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
2552 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
2555 static void mxt_debug_init(struct mxt_data *data)
2557 struct mxt_info *info = data->info;
2558 struct mxt_dbg *dbg = &data->dbg;
2559 struct mxt_object *object;
2560 int error;
2562 object = mxt_get_object(data, MXT_GEN_COMMAND_T6);
2563 if (!object)
2564 goto error;
2566 dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC;
2568 object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37);
2569 if (!object)
2570 goto error;
2572 if (mxt_obj_size(object) != sizeof(struct t37_debug)) {
2573 dev_warn(&data->client->dev, "Bad T37 size");
2574 goto error;
2577 dbg->t37_address = object->start_address;
2579 /* Calculate size of data and allocate buffer */
2580 dbg->t37_nodes = data->xsize * data->ysize;
2582 if (info->family_id == MXT_FAMILY_1386)
2583 dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN;
2584 else
2585 dbg->t37_pages = DIV_ROUND_UP(data->xsize *
2586 info->matrix_ysize *
2587 sizeof(u16),
2588 sizeof(dbg->t37_buf->data));
2590 dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages,
2591 sizeof(struct t37_debug), GFP_KERNEL);
2592 if (!dbg->t37_buf)
2593 goto error;
2595 /* init channel to zero */
2596 mxt_set_input(data, 0);
2598 /* register video device */
2599 snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts");
2600 error = v4l2_device_register(&data->client->dev, &dbg->v4l2);
2601 if (error)
2602 goto error;
2604 /* initialize the queue */
2605 mutex_init(&dbg->lock);
2606 dbg->queue = mxt_queue;
2607 dbg->queue.drv_priv = data;
2608 dbg->queue.lock = &dbg->lock;
2609 dbg->queue.dev = &data->client->dev;
2611 error = vb2_queue_init(&dbg->queue);
2612 if (error)
2613 goto error_unreg_v4l2;
2615 dbg->vdev = mxt_video_device;
2616 dbg->vdev.v4l2_dev = &dbg->v4l2;
2617 dbg->vdev.lock = &dbg->lock;
2618 dbg->vdev.vfl_dir = VFL_DIR_RX;
2619 dbg->vdev.queue = &dbg->queue;
2620 video_set_drvdata(&dbg->vdev, data);
2622 error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1);
2623 if (error)
2624 goto error_unreg_v4l2;
2626 return;
2628 error_unreg_v4l2:
2629 v4l2_device_unregister(&dbg->v4l2);
2630 error:
2631 dev_warn(&data->client->dev, "Error initializing T37\n");
2633 #else
2634 static void mxt_debug_init(struct mxt_data *data)
2637 #endif
2639 static int mxt_configure_objects(struct mxt_data *data,
2640 const struct firmware *cfg)
2642 struct device *dev = &data->client->dev;
2643 int error;
2645 error = mxt_init_t7_power_cfg(data);
2646 if (error) {
2647 dev_err(dev, "Failed to initialize power cfg\n");
2648 return error;
2651 if (cfg) {
2652 error = mxt_update_cfg(data, cfg);
2653 if (error)
2654 dev_warn(dev, "Error %d updating config\n", error);
2657 if (data->multitouch) {
2658 error = mxt_initialize_input_device(data);
2659 if (error)
2660 return error;
2661 } else {
2662 dev_warn(dev, "No touch object detected\n");
2665 mxt_debug_init(data);
2667 return 0;
2670 /* Firmware Version is returned as Major.Minor.Build */
2671 static ssize_t mxt_fw_version_show(struct device *dev,
2672 struct device_attribute *attr, char *buf)
2674 struct mxt_data *data = dev_get_drvdata(dev);
2675 struct mxt_info *info = data->info;
2676 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
2677 info->version >> 4, info->version & 0xf, info->build);
2680 /* Hardware Version is returned as FamilyID.VariantID */
2681 static ssize_t mxt_hw_version_show(struct device *dev,
2682 struct device_attribute *attr, char *buf)
2684 struct mxt_data *data = dev_get_drvdata(dev);
2685 struct mxt_info *info = data->info;
2686 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
2687 info->family_id, info->variant_id);
2690 static ssize_t mxt_show_instance(char *buf, int count,
2691 struct mxt_object *object, int instance,
2692 const u8 *val)
2694 int i;
2696 if (mxt_obj_instances(object) > 1)
2697 count += scnprintf(buf + count, PAGE_SIZE - count,
2698 "Instance %u\n", instance);
2700 for (i = 0; i < mxt_obj_size(object); i++)
2701 count += scnprintf(buf + count, PAGE_SIZE - count,
2702 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
2703 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
2705 return count;
2708 static ssize_t mxt_object_show(struct device *dev,
2709 struct device_attribute *attr, char *buf)
2711 struct mxt_data *data = dev_get_drvdata(dev);
2712 struct mxt_object *object;
2713 int count = 0;
2714 int i, j;
2715 int error;
2716 u8 *obuf;
2718 /* Pre-allocate buffer large enough to hold max sized object. */
2719 obuf = kmalloc(256, GFP_KERNEL);
2720 if (!obuf)
2721 return -ENOMEM;
2723 error = 0;
2724 for (i = 0; i < data->info->object_num; i++) {
2725 object = data->object_table + i;
2727 if (!mxt_object_readable(object->type))
2728 continue;
2730 count += scnprintf(buf + count, PAGE_SIZE - count,
2731 "T%u:\n", object->type);
2733 for (j = 0; j < mxt_obj_instances(object); j++) {
2734 u16 size = mxt_obj_size(object);
2735 u16 addr = object->start_address + j * size;
2737 error = __mxt_read_reg(data->client, addr, size, obuf);
2738 if (error)
2739 goto done;
2741 count = mxt_show_instance(buf, count, object, j, obuf);
2745 done:
2746 kfree(obuf);
2747 return error ?: count;
2750 static int mxt_check_firmware_format(struct device *dev,
2751 const struct firmware *fw)
2753 unsigned int pos = 0;
2754 char c;
2756 while (pos < fw->size) {
2757 c = *(fw->data + pos);
2759 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
2760 return 0;
2762 pos++;
2766 * To convert file try:
2767 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2769 dev_err(dev, "Aborting: firmware file must be in binary format\n");
2771 return -EINVAL;
2774 static int mxt_load_fw(struct device *dev, const char *fn)
2776 struct mxt_data *data = dev_get_drvdata(dev);
2777 const struct firmware *fw = NULL;
2778 unsigned int frame_size;
2779 unsigned int pos = 0;
2780 unsigned int retry = 0;
2781 unsigned int frame = 0;
2782 int ret;
2784 ret = request_firmware(&fw, fn, dev);
2785 if (ret) {
2786 dev_err(dev, "Unable to open firmware %s\n", fn);
2787 return ret;
2790 /* Check for incorrect enc file */
2791 ret = mxt_check_firmware_format(dev, fw);
2792 if (ret)
2793 goto release_firmware;
2795 if (!data->in_bootloader) {
2796 /* Change to the bootloader mode */
2797 data->in_bootloader = true;
2799 ret = mxt_t6_command(data, MXT_COMMAND_RESET,
2800 MXT_BOOT_VALUE, false);
2801 if (ret)
2802 goto release_firmware;
2804 msleep(MXT_RESET_TIME);
2806 /* Do not need to scan since we know family ID */
2807 ret = mxt_lookup_bootloader_address(data, 0);
2808 if (ret)
2809 goto release_firmware;
2811 mxt_free_input_device(data);
2812 mxt_free_object_table(data);
2813 } else {
2814 enable_irq(data->irq);
2817 reinit_completion(&data->bl_completion);
2819 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
2820 if (ret) {
2821 /* Bootloader may still be unlocked from previous attempt */
2822 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
2823 if (ret)
2824 goto disable_irq;
2825 } else {
2826 dev_info(dev, "Unlocking bootloader\n");
2828 /* Unlock bootloader */
2829 ret = mxt_send_bootloader_cmd(data, true);
2830 if (ret)
2831 goto disable_irq;
2834 while (pos < fw->size) {
2835 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
2836 if (ret)
2837 goto disable_irq;
2839 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
2841 /* Take account of CRC bytes */
2842 frame_size += 2;
2844 /* Write one frame to device */
2845 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
2846 if (ret)
2847 goto disable_irq;
2849 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
2850 if (ret) {
2851 retry++;
2853 /* Back off by 20ms per retry */
2854 msleep(retry * 20);
2856 if (retry > 20) {
2857 dev_err(dev, "Retry count exceeded\n");
2858 goto disable_irq;
2860 } else {
2861 retry = 0;
2862 pos += frame_size;
2863 frame++;
2866 if (frame % 50 == 0)
2867 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
2868 frame, pos, fw->size);
2871 /* Wait for flash. */
2872 ret = mxt_wait_for_completion(data, &data->bl_completion,
2873 MXT_FW_RESET_TIME);
2874 if (ret)
2875 goto disable_irq;
2877 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
2880 * Wait for device to reset. Some bootloader versions do not assert
2881 * the CHG line after bootloading has finished, so ignore potential
2882 * errors.
2884 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
2886 data->in_bootloader = false;
2888 disable_irq:
2889 disable_irq(data->irq);
2890 release_firmware:
2891 release_firmware(fw);
2892 return ret;
2895 static ssize_t mxt_update_fw_store(struct device *dev,
2896 struct device_attribute *attr,
2897 const char *buf, size_t count)
2899 struct mxt_data *data = dev_get_drvdata(dev);
2900 int error;
2902 error = mxt_load_fw(dev, MXT_FW_NAME);
2903 if (error) {
2904 dev_err(dev, "The firmware update failed(%d)\n", error);
2905 count = error;
2906 } else {
2907 dev_info(dev, "The firmware update succeeded\n");
2909 error = mxt_initialize(data);
2910 if (error)
2911 return error;
2914 return count;
2917 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
2918 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
2919 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
2920 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
2922 static struct attribute *mxt_attrs[] = {
2923 &dev_attr_fw_version.attr,
2924 &dev_attr_hw_version.attr,
2925 &dev_attr_object.attr,
2926 &dev_attr_update_fw.attr,
2927 NULL
2930 static const struct attribute_group mxt_attr_group = {
2931 .attrs = mxt_attrs,
2934 static void mxt_start(struct mxt_data *data)
2936 switch (data->suspend_mode) {
2937 case MXT_SUSPEND_T9_CTRL:
2938 mxt_soft_reset(data);
2940 /* Touch enable */
2941 /* 0x83 = SCANEN | RPTEN | ENABLE */
2942 mxt_write_object(data,
2943 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
2944 break;
2946 case MXT_SUSPEND_DEEP_SLEEP:
2947 default:
2948 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2950 /* Recalibrate since chip has been in deep sleep */
2951 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
2952 break;
2956 static void mxt_stop(struct mxt_data *data)
2958 switch (data->suspend_mode) {
2959 case MXT_SUSPEND_T9_CTRL:
2960 /* Touch disable */
2961 mxt_write_object(data,
2962 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
2963 break;
2965 case MXT_SUSPEND_DEEP_SLEEP:
2966 default:
2967 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
2968 break;
2972 static int mxt_input_open(struct input_dev *dev)
2974 struct mxt_data *data = input_get_drvdata(dev);
2976 mxt_start(data);
2978 return 0;
2981 static void mxt_input_close(struct input_dev *dev)
2983 struct mxt_data *data = input_get_drvdata(dev);
2985 mxt_stop(data);
2988 static int mxt_parse_device_properties(struct mxt_data *data)
2990 static const char keymap_property[] = "linux,gpio-keymap";
2991 struct device *dev = &data->client->dev;
2992 u32 *keymap;
2993 int n_keys;
2994 int error;
2996 if (device_property_present(dev, keymap_property)) {
2997 n_keys = device_property_read_u32_array(dev, keymap_property,
2998 NULL, 0);
2999 if (n_keys <= 0) {
3000 error = n_keys < 0 ? n_keys : -EINVAL;
3001 dev_err(dev, "invalid/malformed '%s' property: %d\n",
3002 keymap_property, error);
3003 return error;
3006 keymap = devm_kmalloc_array(dev, n_keys, sizeof(*keymap),
3007 GFP_KERNEL);
3008 if (!keymap)
3009 return -ENOMEM;
3011 error = device_property_read_u32_array(dev, keymap_property,
3012 keymap, n_keys);
3013 if (error) {
3014 dev_err(dev, "failed to parse '%s' property: %d\n",
3015 keymap_property, error);
3016 return error;
3019 data->t19_keymap = keymap;
3020 data->t19_num_keys = n_keys;
3023 return 0;
3026 static const struct dmi_system_id chromebook_T9_suspend_dmi[] = {
3028 .matches = {
3029 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
3030 DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
3034 .matches = {
3035 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
3041 static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
3043 struct mxt_data *data;
3044 int error;
3047 * Ignore devices that do not have device properties attached to
3048 * them, as we need help determining whether we are dealing with
3049 * touch screen or touchpad.
3051 * So far on x86 the only users of Atmel touch controllers are
3052 * Chromebooks, and chromeos_laptop driver will ensure that
3053 * necessary properties are provided (if firmware does not do that).
3055 if (!device_property_present(&client->dev, "compatible"))
3056 return -ENXIO;
3059 * Ignore ACPI devices representing bootloader mode.
3061 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3062 * devices for both application and bootloader modes, but we are
3063 * interested in application mode only (if device is in bootloader
3064 * mode we'll end up switching into application anyway). So far
3065 * application mode addresses were all above 0x40, so we'll use it
3066 * as a threshold.
3068 if (ACPI_COMPANION(&client->dev) && client->addr < 0x40)
3069 return -ENXIO;
3071 data = devm_kzalloc(&client->dev, sizeof(struct mxt_data), GFP_KERNEL);
3072 if (!data)
3073 return -ENOMEM;
3075 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
3076 client->adapter->nr, client->addr);
3078 data->client = client;
3079 data->irq = client->irq;
3080 i2c_set_clientdata(client, data);
3082 init_completion(&data->bl_completion);
3083 init_completion(&data->reset_completion);
3084 init_completion(&data->crc_completion);
3086 data->suspend_mode = dmi_check_system(chromebook_T9_suspend_dmi) ?
3087 MXT_SUSPEND_T9_CTRL : MXT_SUSPEND_DEEP_SLEEP;
3089 error = mxt_parse_device_properties(data);
3090 if (error)
3091 return error;
3093 data->reset_gpio = devm_gpiod_get_optional(&client->dev,
3094 "reset", GPIOD_OUT_LOW);
3095 if (IS_ERR(data->reset_gpio)) {
3096 error = PTR_ERR(data->reset_gpio);
3097 dev_err(&client->dev, "Failed to get reset gpio: %d\n", error);
3098 return error;
3101 error = devm_request_threaded_irq(&client->dev, client->irq,
3102 NULL, mxt_interrupt, IRQF_ONESHOT,
3103 client->name, data);
3104 if (error) {
3105 dev_err(&client->dev, "Failed to register interrupt\n");
3106 return error;
3109 disable_irq(client->irq);
3111 if (data->reset_gpio) {
3112 msleep(MXT_RESET_GPIO_TIME);
3113 gpiod_set_value(data->reset_gpio, 1);
3114 msleep(MXT_RESET_INVALID_CHG);
3117 error = mxt_initialize(data);
3118 if (error)
3119 return error;
3121 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
3122 if (error) {
3123 dev_err(&client->dev, "Failure %d creating sysfs group\n",
3124 error);
3125 goto err_free_object;
3128 return 0;
3130 err_free_object:
3131 mxt_free_input_device(data);
3132 mxt_free_object_table(data);
3133 return error;
3136 static int mxt_remove(struct i2c_client *client)
3138 struct mxt_data *data = i2c_get_clientdata(client);
3140 disable_irq(data->irq);
3141 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
3142 mxt_free_input_device(data);
3143 mxt_free_object_table(data);
3145 return 0;
3148 static int __maybe_unused mxt_suspend(struct device *dev)
3150 struct i2c_client *client = to_i2c_client(dev);
3151 struct mxt_data *data = i2c_get_clientdata(client);
3152 struct input_dev *input_dev = data->input_dev;
3154 if (!input_dev)
3155 return 0;
3157 mutex_lock(&input_dev->mutex);
3159 if (input_dev->users)
3160 mxt_stop(data);
3162 mutex_unlock(&input_dev->mutex);
3164 return 0;
3167 static int __maybe_unused mxt_resume(struct device *dev)
3169 struct i2c_client *client = to_i2c_client(dev);
3170 struct mxt_data *data = i2c_get_clientdata(client);
3171 struct input_dev *input_dev = data->input_dev;
3173 if (!input_dev)
3174 return 0;
3176 mutex_lock(&input_dev->mutex);
3178 if (input_dev->users)
3179 mxt_start(data);
3181 mutex_unlock(&input_dev->mutex);
3183 return 0;
3186 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
3188 static const struct of_device_id mxt_of_match[] = {
3189 { .compatible = "atmel,maxtouch", },
3190 /* Compatibles listed below are deprecated */
3191 { .compatible = "atmel,qt602240_ts", },
3192 { .compatible = "atmel,atmel_mxt_ts", },
3193 { .compatible = "atmel,atmel_mxt_tp", },
3194 { .compatible = "atmel,mXT224", },
3197 MODULE_DEVICE_TABLE(of, mxt_of_match);
3199 #ifdef CONFIG_ACPI
3200 static const struct acpi_device_id mxt_acpi_id[] = {
3201 { "ATML0000", 0 }, /* Touchpad */
3202 { "ATML0001", 0 }, /* Touchscreen */
3205 MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
3206 #endif
3208 static const struct i2c_device_id mxt_id[] = {
3209 { "qt602240_ts", 0 },
3210 { "atmel_mxt_ts", 0 },
3211 { "atmel_mxt_tp", 0 },
3212 { "maxtouch", 0 },
3213 { "mXT224", 0 },
3216 MODULE_DEVICE_TABLE(i2c, mxt_id);
3218 static struct i2c_driver mxt_driver = {
3219 .driver = {
3220 .name = "atmel_mxt_ts",
3221 .of_match_table = mxt_of_match,
3222 .acpi_match_table = ACPI_PTR(mxt_acpi_id),
3223 .pm = &mxt_pm_ops,
3225 .probe = mxt_probe,
3226 .remove = mxt_remove,
3227 .id_table = mxt_id,
3230 module_i2c_driver(mxt_driver);
3232 /* Module information */
3233 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
3234 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
3235 MODULE_LICENSE("GPL");