sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / input / touchscreen / atmel_mxt_ts.c
blob6e0b4a2120d3ece91d59c9fc23e50089dc28fa82
1 /*
2 * Atmel maXTouch Touchscreen driver
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Copyright (C) 2012 Google, Inc.
7 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/completion.h>
19 #include <linux/delay.h>
20 #include <linux/firmware.h>
21 #include <linux/i2c.h>
22 #include <linux/i2c/atmel_mxt_ts.h>
23 #include <linux/input/mt.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
27 /* Version */
28 #define MXT_VER_20 20
29 #define MXT_VER_21 21
30 #define MXT_VER_22 22
32 /* Firmware */
33 #define MXT_FW_NAME "maxtouch.fw"
35 /* Registers */
36 #define MXT_INFO 0x00
37 #define MXT_FAMILY_ID 0x00
38 #define MXT_VARIANT_ID 0x01
39 #define MXT_VERSION 0x02
40 #define MXT_BUILD 0x03
41 #define MXT_MATRIX_X_SIZE 0x04
42 #define MXT_MATRIX_Y_SIZE 0x05
43 #define MXT_OBJECT_NUM 0x06
44 #define MXT_OBJECT_START 0x07
46 #define MXT_OBJECT_SIZE 6
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_PROCI_GRIPFACE_T20 20
60 #define MXT_PROCG_NOISE_T22 22
61 #define MXT_PROCI_ONETOUCH_T24 24
62 #define MXT_PROCI_TWOTOUCH_T27 27
63 #define MXT_PROCI_GRIP_T40 40
64 #define MXT_PROCI_PALM_T41 41
65 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
66 #define MXT_PROCI_STYLUS_T47 47
67 #define MXT_PROCG_NOISESUPPRESSION_T48 48
68 #define MXT_SPT_COMMSCONFIG_T18 18
69 #define MXT_SPT_GPIOPWM_T19 19
70 #define MXT_SPT_SELFTEST_T25 25
71 #define MXT_SPT_CTECONFIG_T28 28
72 #define MXT_SPT_USERDATA_T38 38
73 #define MXT_SPT_DIGITIZER_T43 43
74 #define MXT_SPT_MESSAGECOUNT_T44 44
75 #define MXT_SPT_CTECONFIG_T46 46
77 /* MXT_GEN_COMMAND_T6 field */
78 #define MXT_COMMAND_RESET 0
79 #define MXT_COMMAND_BACKUPNV 1
80 #define MXT_COMMAND_CALIBRATE 2
81 #define MXT_COMMAND_REPORTALL 3
82 #define MXT_COMMAND_DIAGNOSTIC 5
84 /* Define for T6 status byte */
85 #define MXT_T6_STATUS_RESET (1 << 7)
87 /* MXT_GEN_POWER_T7 field */
88 #define MXT_POWER_IDLEACQINT 0
89 #define MXT_POWER_ACTVACQINT 1
90 #define MXT_POWER_ACTV2IDLETO 2
92 /* MXT_GEN_ACQUIRE_T8 field */
93 #define MXT_ACQUIRE_CHRGTIME 0
94 #define MXT_ACQUIRE_TCHDRIFT 2
95 #define MXT_ACQUIRE_DRIFTST 3
96 #define MXT_ACQUIRE_TCHAUTOCAL 4
97 #define MXT_ACQUIRE_SYNC 5
98 #define MXT_ACQUIRE_ATCHCALST 6
99 #define MXT_ACQUIRE_ATCHCALSTHR 7
101 /* MXT_TOUCH_MULTI_T9 field */
102 #define MXT_TOUCH_CTRL 0
103 #define MXT_T9_ORIENT 9
104 #define MXT_T9_RANGE 18
106 /* MXT_TOUCH_MULTI_T9 status */
107 #define MXT_T9_UNGRIP (1 << 0)
108 #define MXT_T9_SUPPRESS (1 << 1)
109 #define MXT_T9_AMP (1 << 2)
110 #define MXT_T9_VECTOR (1 << 3)
111 #define MXT_T9_MOVE (1 << 4)
112 #define MXT_T9_RELEASE (1 << 5)
113 #define MXT_T9_PRESS (1 << 6)
114 #define MXT_T9_DETECT (1 << 7)
116 struct t9_range {
117 u16 x;
118 u16 y;
119 } __packed;
121 /* MXT_TOUCH_MULTI_T9 orient */
122 #define MXT_T9_ORIENT_SWITCH (1 << 0)
124 /* MXT_PROCI_GRIPFACE_T20 field */
125 #define MXT_GRIPFACE_CTRL 0
126 #define MXT_GRIPFACE_XLOGRIP 1
127 #define MXT_GRIPFACE_XHIGRIP 2
128 #define MXT_GRIPFACE_YLOGRIP 3
129 #define MXT_GRIPFACE_YHIGRIP 4
130 #define MXT_GRIPFACE_MAXTCHS 5
131 #define MXT_GRIPFACE_SZTHR1 7
132 #define MXT_GRIPFACE_SZTHR2 8
133 #define MXT_GRIPFACE_SHPTHR1 9
134 #define MXT_GRIPFACE_SHPTHR2 10
135 #define MXT_GRIPFACE_SUPEXTTO 11
137 /* MXT_PROCI_NOISE field */
138 #define MXT_NOISE_CTRL 0
139 #define MXT_NOISE_OUTFLEN 1
140 #define MXT_NOISE_GCAFUL_LSB 3
141 #define MXT_NOISE_GCAFUL_MSB 4
142 #define MXT_NOISE_GCAFLL_LSB 5
143 #define MXT_NOISE_GCAFLL_MSB 6
144 #define MXT_NOISE_ACTVGCAFVALID 7
145 #define MXT_NOISE_NOISETHR 8
146 #define MXT_NOISE_FREQHOPSCALE 10
147 #define MXT_NOISE_FREQ0 11
148 #define MXT_NOISE_FREQ1 12
149 #define MXT_NOISE_FREQ2 13
150 #define MXT_NOISE_FREQ3 14
151 #define MXT_NOISE_FREQ4 15
152 #define MXT_NOISE_IDLEGCAFVALID 16
154 /* MXT_SPT_COMMSCONFIG_T18 */
155 #define MXT_COMMS_CTRL 0
156 #define MXT_COMMS_CMD 1
158 /* MXT_SPT_CTECONFIG_T28 field */
159 #define MXT_CTE_CTRL 0
160 #define MXT_CTE_CMD 1
161 #define MXT_CTE_MODE 2
162 #define MXT_CTE_IDLEGCAFDEPTH 3
163 #define MXT_CTE_ACTVGCAFDEPTH 4
164 #define MXT_CTE_VOLTAGE 5
166 #define MXT_VOLTAGE_DEFAULT 2700000
167 #define MXT_VOLTAGE_STEP 10000
169 /* Define for MXT_GEN_COMMAND_T6 */
170 #define MXT_BOOT_VALUE 0xa5
171 #define MXT_RESET_VALUE 0x01
172 #define MXT_BACKUP_VALUE 0x55
174 /* Delay times */
175 #define MXT_BACKUP_TIME 50 /* msec */
176 #define MXT_RESET_TIME 200 /* msec */
177 #define MXT_RESET_TIMEOUT 3000 /* msec */
178 #define MXT_CRC_TIMEOUT 1000 /* msec */
179 #define MXT_FW_RESET_TIME 3000 /* msec */
180 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
182 /* Command to unlock bootloader */
183 #define MXT_UNLOCK_CMD_MSB 0xaa
184 #define MXT_UNLOCK_CMD_LSB 0xdc
186 /* Bootloader mode status */
187 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
188 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
189 #define MXT_FRAME_CRC_CHECK 0x02
190 #define MXT_FRAME_CRC_FAIL 0x03
191 #define MXT_FRAME_CRC_PASS 0x04
192 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
193 #define MXT_BOOT_STATUS_MASK 0x3f
194 #define MXT_BOOT_EXTENDED_ID (1 << 5)
195 #define MXT_BOOT_ID_MASK 0x1f
197 /* Touchscreen absolute values */
198 #define MXT_MAX_AREA 0xff
200 #define MXT_PIXELS_PER_MM 20
202 struct mxt_info {
203 u8 family_id;
204 u8 variant_id;
205 u8 version;
206 u8 build;
207 u8 matrix_xsize;
208 u8 matrix_ysize;
209 u8 object_num;
212 struct mxt_object {
213 u8 type;
214 u16 start_address;
215 u8 size_minus_one;
216 u8 instances_minus_one;
217 u8 num_report_ids;
218 } __packed;
220 struct mxt_message {
221 u8 reportid;
222 u8 message[7];
225 /* Each client has this additional data */
226 struct mxt_data {
227 struct i2c_client *client;
228 struct input_dev *input_dev;
229 char phys[64]; /* device physical location */
230 const struct mxt_platform_data *pdata;
231 struct mxt_object *object_table;
232 struct mxt_info info;
233 unsigned int irq;
234 unsigned int max_x;
235 unsigned int max_y;
236 bool in_bootloader;
237 u32 config_crc;
238 u8 bootloader_addr;
240 /* Cached parameters from object table */
241 u8 T6_reportid;
242 u16 T6_address;
243 u8 T9_reportid_min;
244 u8 T9_reportid_max;
245 u8 T19_reportid;
247 /* for fw update in bootloader */
248 struct completion bl_completion;
250 /* for reset handling */
251 struct completion reset_completion;
253 /* for config update handling */
254 struct completion crc_completion;
257 static size_t mxt_obj_size(const struct mxt_object *obj)
259 return obj->size_minus_one + 1;
262 static size_t mxt_obj_instances(const struct mxt_object *obj)
264 return obj->instances_minus_one + 1;
267 static bool mxt_object_readable(unsigned int type)
269 switch (type) {
270 case MXT_GEN_COMMAND_T6:
271 case MXT_GEN_POWER_T7:
272 case MXT_GEN_ACQUIRE_T8:
273 case MXT_GEN_DATASOURCE_T53:
274 case MXT_TOUCH_MULTI_T9:
275 case MXT_TOUCH_KEYARRAY_T15:
276 case MXT_TOUCH_PROXIMITY_T23:
277 case MXT_TOUCH_PROXKEY_T52:
278 case MXT_PROCI_GRIPFACE_T20:
279 case MXT_PROCG_NOISE_T22:
280 case MXT_PROCI_ONETOUCH_T24:
281 case MXT_PROCI_TWOTOUCH_T27:
282 case MXT_PROCI_GRIP_T40:
283 case MXT_PROCI_PALM_T41:
284 case MXT_PROCI_TOUCHSUPPRESSION_T42:
285 case MXT_PROCI_STYLUS_T47:
286 case MXT_PROCG_NOISESUPPRESSION_T48:
287 case MXT_SPT_COMMSCONFIG_T18:
288 case MXT_SPT_GPIOPWM_T19:
289 case MXT_SPT_SELFTEST_T25:
290 case MXT_SPT_CTECONFIG_T28:
291 case MXT_SPT_USERDATA_T38:
292 case MXT_SPT_DIGITIZER_T43:
293 case MXT_SPT_CTECONFIG_T46:
294 return true;
295 default:
296 return false;
300 static bool mxt_object_writable(unsigned int type)
302 switch (type) {
303 case MXT_GEN_COMMAND_T6:
304 case MXT_GEN_POWER_T7:
305 case MXT_GEN_ACQUIRE_T8:
306 case MXT_TOUCH_MULTI_T9:
307 case MXT_TOUCH_KEYARRAY_T15:
308 case MXT_TOUCH_PROXIMITY_T23:
309 case MXT_TOUCH_PROXKEY_T52:
310 case MXT_PROCI_GRIPFACE_T20:
311 case MXT_PROCG_NOISE_T22:
312 case MXT_PROCI_ONETOUCH_T24:
313 case MXT_PROCI_TWOTOUCH_T27:
314 case MXT_PROCI_GRIP_T40:
315 case MXT_PROCI_PALM_T41:
316 case MXT_PROCI_TOUCHSUPPRESSION_T42:
317 case MXT_PROCI_STYLUS_T47:
318 case MXT_PROCG_NOISESUPPRESSION_T48:
319 case MXT_SPT_COMMSCONFIG_T18:
320 case MXT_SPT_GPIOPWM_T19:
321 case MXT_SPT_SELFTEST_T25:
322 case MXT_SPT_CTECONFIG_T28:
323 case MXT_SPT_DIGITIZER_T43:
324 case MXT_SPT_CTECONFIG_T46:
325 return true;
326 default:
327 return false;
331 static void mxt_dump_message(struct device *dev,
332 struct mxt_message *message)
334 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
335 message->reportid, 7, message->message);
338 static int mxt_wait_for_completion(struct mxt_data *data,
339 struct completion *comp,
340 unsigned int timeout_ms)
342 struct device *dev = &data->client->dev;
343 unsigned long timeout = msecs_to_jiffies(timeout_ms);
344 long ret;
346 ret = wait_for_completion_interruptible_timeout(comp, timeout);
347 if (ret < 0) {
348 return ret;
349 } else if (ret == 0) {
350 dev_err(dev, "Wait for completion timed out.\n");
351 return -ETIMEDOUT;
353 return 0;
356 static int mxt_bootloader_read(struct mxt_data *data,
357 u8 *val, unsigned int count)
359 int ret;
360 struct i2c_msg msg;
362 msg.addr = data->bootloader_addr;
363 msg.flags = data->client->flags & I2C_M_TEN;
364 msg.flags |= I2C_M_RD;
365 msg.len = count;
366 msg.buf = val;
368 ret = i2c_transfer(data->client->adapter, &msg, 1);
370 if (ret == 1) {
371 ret = 0;
372 } else {
373 ret = ret < 0 ? ret : -EIO;
374 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
375 __func__, ret);
378 return ret;
381 static int mxt_bootloader_write(struct mxt_data *data,
382 const u8 * const val, unsigned int count)
384 int ret;
385 struct i2c_msg msg;
387 msg.addr = data->bootloader_addr;
388 msg.flags = data->client->flags & I2C_M_TEN;
389 msg.len = count;
390 msg.buf = (u8 *)val;
392 ret = i2c_transfer(data->client->adapter, &msg, 1);
393 if (ret == 1) {
394 ret = 0;
395 } else {
396 ret = ret < 0 ? ret : -EIO;
397 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
398 __func__, ret);
401 return ret;
404 static int mxt_lookup_bootloader_address(struct mxt_data *data)
406 u8 appmode = data->client->addr;
407 u8 bootloader;
409 switch (appmode) {
410 case 0x4a:
411 case 0x4b:
412 case 0x4c:
413 case 0x4d:
414 case 0x5a:
415 case 0x5b:
416 bootloader = appmode - 0x26;
417 break;
418 default:
419 dev_err(&data->client->dev,
420 "Appmode i2c address 0x%02x not found\n",
421 appmode);
422 return -EINVAL;
425 data->bootloader_addr = bootloader;
426 return 0;
429 static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
431 struct device *dev = &data->client->dev;
432 u8 buf[3];
434 if (val & MXT_BOOT_EXTENDED_ID) {
435 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
436 dev_err(dev, "%s: i2c failure\n", __func__);
437 return val;
440 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
442 return buf[0];
443 } else {
444 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
446 return val;
450 static int mxt_check_bootloader(struct mxt_data *data, unsigned int state)
452 struct device *dev = &data->client->dev;
453 u8 val;
454 int ret;
456 recheck:
457 if (state != MXT_WAITING_BOOTLOAD_CMD) {
459 * In application update mode, the interrupt
460 * line signals state transitions. We must wait for the
461 * CHG assertion before reading the status byte.
462 * Once the status byte has been read, the line is deasserted.
464 ret = mxt_wait_for_completion(data, &data->bl_completion,
465 MXT_FW_CHG_TIMEOUT);
466 if (ret) {
468 * TODO: handle -ERESTARTSYS better by terminating
469 * fw update process before returning to userspace
470 * by writing length 0x000 to device (iff we are in
471 * WAITING_FRAME_DATA state).
473 dev_err(dev, "Update wait error %d\n", ret);
474 return ret;
478 ret = mxt_bootloader_read(data, &val, 1);
479 if (ret)
480 return ret;
482 if (state == MXT_WAITING_BOOTLOAD_CMD)
483 val = mxt_get_bootloader_version(data, val);
485 switch (state) {
486 case MXT_WAITING_BOOTLOAD_CMD:
487 case MXT_WAITING_FRAME_DATA:
488 val &= ~MXT_BOOT_STATUS_MASK;
489 break;
490 case MXT_FRAME_CRC_PASS:
491 if (val == MXT_FRAME_CRC_CHECK) {
492 goto recheck;
493 } else if (val == MXT_FRAME_CRC_FAIL) {
494 dev_err(dev, "Bootloader CRC fail\n");
495 return -EINVAL;
497 break;
498 default:
499 return -EINVAL;
502 if (val != state) {
503 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
504 val, state);
505 return -EINVAL;
508 return 0;
511 static int mxt_unlock_bootloader(struct mxt_data *data)
513 int ret;
514 u8 buf[2];
516 buf[0] = MXT_UNLOCK_CMD_LSB;
517 buf[1] = MXT_UNLOCK_CMD_MSB;
519 ret = mxt_bootloader_write(data, buf, 2);
520 if (ret)
521 return ret;
523 return 0;
526 static int __mxt_read_reg(struct i2c_client *client,
527 u16 reg, u16 len, void *val)
529 struct i2c_msg xfer[2];
530 u8 buf[2];
531 int ret;
533 buf[0] = reg & 0xff;
534 buf[1] = (reg >> 8) & 0xff;
536 /* Write register */
537 xfer[0].addr = client->addr;
538 xfer[0].flags = 0;
539 xfer[0].len = 2;
540 xfer[0].buf = buf;
542 /* Read data */
543 xfer[1].addr = client->addr;
544 xfer[1].flags = I2C_M_RD;
545 xfer[1].len = len;
546 xfer[1].buf = val;
548 ret = i2c_transfer(client->adapter, xfer, 2);
549 if (ret == 2) {
550 ret = 0;
551 } else {
552 if (ret >= 0)
553 ret = -EIO;
554 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
555 __func__, ret);
558 return ret;
561 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
562 const void *val)
564 u8 *buf;
565 size_t count;
566 int ret;
568 count = len + 2;
569 buf = kmalloc(count, GFP_KERNEL);
570 if (!buf)
571 return -ENOMEM;
573 buf[0] = reg & 0xff;
574 buf[1] = (reg >> 8) & 0xff;
575 memcpy(&buf[2], val, len);
577 ret = i2c_master_send(client, buf, count);
578 if (ret == count) {
579 ret = 0;
580 } else {
581 if (ret >= 0)
582 ret = -EIO;
583 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
584 __func__, ret);
587 kfree(buf);
588 return ret;
591 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
593 return __mxt_write_reg(client, reg, 1, &val);
596 static struct mxt_object *
597 mxt_get_object(struct mxt_data *data, u8 type)
599 struct mxt_object *object;
600 int i;
602 for (i = 0; i < data->info.object_num; i++) {
603 object = data->object_table + i;
604 if (object->type == type)
605 return object;
608 dev_err(&data->client->dev, "Invalid object type T%u\n", type);
609 return NULL;
612 static int mxt_read_message(struct mxt_data *data,
613 struct mxt_message *message)
615 struct mxt_object *object;
616 u16 reg;
618 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
619 if (!object)
620 return -EINVAL;
622 reg = object->start_address;
623 return __mxt_read_reg(data->client, reg,
624 sizeof(struct mxt_message), message);
627 static int mxt_write_object(struct mxt_data *data,
628 u8 type, u8 offset, u8 val)
630 struct mxt_object *object;
631 u16 reg;
633 object = mxt_get_object(data, type);
634 if (!object || offset >= mxt_obj_size(object))
635 return -EINVAL;
637 reg = object->start_address;
638 return mxt_write_reg(data->client, reg + offset, val);
641 static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
643 struct input_dev *input = data->input_dev;
644 const struct mxt_platform_data *pdata = data->pdata;
645 bool button;
646 int i;
648 /* Active-low switch */
649 for (i = 0; i < pdata->t19_num_keys; i++) {
650 if (pdata->t19_keymap[i] == KEY_RESERVED)
651 continue;
652 button = !(message->message[0] & (1 << i));
653 input_report_key(input, pdata->t19_keymap[i], button);
657 static void mxt_input_sync(struct input_dev *input_dev)
659 input_mt_report_pointer_emulation(input_dev, false);
660 input_sync(input_dev);
663 static void mxt_input_touchevent(struct mxt_data *data,
664 struct mxt_message *message, int id)
666 struct device *dev = &data->client->dev;
667 u8 status = message->message[0];
668 struct input_dev *input_dev = data->input_dev;
669 int x;
670 int y;
671 int area;
672 int amplitude;
674 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
675 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
677 /* Handle 10/12 bit switching */
678 if (data->max_x < 1024)
679 x >>= 2;
680 if (data->max_y < 1024)
681 y >>= 2;
683 area = message->message[4];
684 amplitude = message->message[5];
686 dev_dbg(dev,
687 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
689 (status & MXT_T9_DETECT) ? 'D' : '.',
690 (status & MXT_T9_PRESS) ? 'P' : '.',
691 (status & MXT_T9_RELEASE) ? 'R' : '.',
692 (status & MXT_T9_MOVE) ? 'M' : '.',
693 (status & MXT_T9_VECTOR) ? 'V' : '.',
694 (status & MXT_T9_AMP) ? 'A' : '.',
695 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
696 (status & MXT_T9_UNGRIP) ? 'U' : '.',
697 x, y, area, amplitude);
699 input_mt_slot(input_dev, id);
701 if (status & MXT_T9_DETECT) {
703 * Multiple bits may be set if the host is slow to read
704 * the status messages, indicating all the events that
705 * have happened.
707 if (status & MXT_T9_RELEASE) {
708 input_mt_report_slot_state(input_dev,
709 MT_TOOL_FINGER, 0);
710 mxt_input_sync(input_dev);
713 /* Touch active */
714 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
715 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
716 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
717 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
718 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
719 } else {
720 /* Touch no longer active, close out slot */
721 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
725 static u16 mxt_extract_T6_csum(const u8 *csum)
727 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
730 static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
732 u8 id = msg->reportid;
733 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
736 static irqreturn_t mxt_process_messages_until_invalid(struct mxt_data *data)
738 struct mxt_message message;
739 const u8 *payload = &message.message[0];
740 struct device *dev = &data->client->dev;
741 u8 reportid;
742 bool update_input = false;
743 u32 crc;
745 do {
746 if (mxt_read_message(data, &message)) {
747 dev_err(dev, "Failed to read message\n");
748 return IRQ_NONE;
751 reportid = message.reportid;
753 if (reportid == data->T6_reportid) {
754 u8 status = payload[0];
756 crc = mxt_extract_T6_csum(&payload[1]);
757 if (crc != data->config_crc) {
758 data->config_crc = crc;
759 complete(&data->crc_completion);
762 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
763 status, data->config_crc);
765 if (status & MXT_T6_STATUS_RESET)
766 complete(&data->reset_completion);
767 } else if (mxt_is_T9_message(data, &message)) {
768 int id = reportid - data->T9_reportid_min;
769 mxt_input_touchevent(data, &message, id);
770 update_input = true;
771 } else if (message.reportid == data->T19_reportid) {
772 mxt_input_button(data, &message);
773 update_input = true;
774 } else {
775 mxt_dump_message(dev, &message);
777 } while (reportid != 0xff);
779 if (update_input)
780 mxt_input_sync(data->input_dev);
782 return IRQ_HANDLED;
785 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
787 struct mxt_data *data = dev_id;
789 if (data->in_bootloader) {
790 /* bootloader state transition completion */
791 complete(&data->bl_completion);
792 return IRQ_HANDLED;
795 return mxt_process_messages_until_invalid(data);
798 static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
799 u8 value, bool wait)
801 u16 reg;
802 u8 command_register;
803 int timeout_counter = 0;
804 int ret;
806 reg = data->T6_address + cmd_offset;
808 ret = mxt_write_reg(data->client, reg, value);
809 if (ret)
810 return ret;
812 if (!wait)
813 return 0;
815 do {
816 msleep(20);
817 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
818 if (ret)
819 return ret;
820 } while (command_register != 0 && timeout_counter++ <= 100);
822 if (timeout_counter > 100) {
823 dev_err(&data->client->dev, "Command failed!\n");
824 return -EIO;
827 return 0;
830 static int mxt_soft_reset(struct mxt_data *data)
832 struct device *dev = &data->client->dev;
833 int ret = 0;
835 dev_info(dev, "Resetting chip\n");
837 reinit_completion(&data->reset_completion);
839 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
840 if (ret)
841 return ret;
843 ret = mxt_wait_for_completion(data, &data->reset_completion,
844 MXT_RESET_TIMEOUT);
845 if (ret)
846 return ret;
848 return 0;
851 static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
854 * On failure, CRC is set to 0 and config will always be
855 * downloaded.
857 data->config_crc = 0;
858 reinit_completion(&data->crc_completion);
860 mxt_t6_command(data, cmd, value, true);
863 * Wait for crc message. On failure, CRC is set to 0 and config will
864 * always be downloaded.
866 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
869 static int mxt_check_reg_init(struct mxt_data *data)
871 const struct mxt_platform_data *pdata = data->pdata;
872 struct mxt_object *object;
873 struct device *dev = &data->client->dev;
874 int index = 0;
875 int i, size;
876 int ret;
878 if (!pdata->config) {
879 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
880 return 0;
883 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
885 if (data->config_crc == pdata->config_crc) {
886 dev_info(dev, "Config CRC 0x%06X: OK\n", data->config_crc);
887 return 0;
890 dev_info(dev, "Config CRC 0x%06X: does not match 0x%06X\n",
891 data->config_crc, pdata->config_crc);
893 for (i = 0; i < data->info.object_num; i++) {
894 object = data->object_table + i;
896 if (!mxt_object_writable(object->type))
897 continue;
899 size = mxt_obj_size(object) * mxt_obj_instances(object);
900 if (index + size > pdata->config_length) {
901 dev_err(dev, "Not enough config data!\n");
902 return -EINVAL;
905 ret = __mxt_write_reg(data->client, object->start_address,
906 size, &pdata->config[index]);
907 if (ret)
908 return ret;
909 index += size;
912 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
914 ret = mxt_soft_reset(data);
915 if (ret)
916 return ret;
918 dev_info(dev, "Config successfully updated\n");
920 return 0;
923 static int mxt_make_highchg(struct mxt_data *data)
925 struct device *dev = &data->client->dev;
926 struct mxt_message message;
927 int count = 10;
928 int error;
930 /* Read dummy message to make high CHG pin */
931 do {
932 error = mxt_read_message(data, &message);
933 if (error)
934 return error;
935 } while (message.reportid != 0xff && --count);
937 if (!count) {
938 dev_err(dev, "CHG pin isn't cleared\n");
939 return -EBUSY;
942 return 0;
945 static int mxt_get_info(struct mxt_data *data)
947 struct i2c_client *client = data->client;
948 struct mxt_info *info = &data->info;
949 int error;
951 /* Read 7-byte info block starting at address 0 */
952 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
953 if (error)
954 return error;
956 return 0;
959 static int mxt_get_object_table(struct mxt_data *data)
961 struct i2c_client *client = data->client;
962 size_t table_size;
963 int error;
964 int i;
965 u8 reportid;
967 table_size = data->info.object_num * sizeof(struct mxt_object);
968 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
969 data->object_table);
970 if (error)
971 return error;
973 /* Valid Report IDs start counting from 1 */
974 reportid = 1;
975 for (i = 0; i < data->info.object_num; i++) {
976 struct mxt_object *object = data->object_table + i;
977 u8 min_id, max_id;
979 le16_to_cpus(&object->start_address);
981 if (object->num_report_ids) {
982 min_id = reportid;
983 reportid += object->num_report_ids *
984 mxt_obj_instances(object);
985 max_id = reportid - 1;
986 } else {
987 min_id = 0;
988 max_id = 0;
991 dev_dbg(&data->client->dev,
992 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
993 object->type, object->start_address,
994 mxt_obj_size(object), mxt_obj_instances(object),
995 min_id, max_id);
997 switch (object->type) {
998 case MXT_GEN_COMMAND_T6:
999 data->T6_reportid = min_id;
1000 data->T6_address = object->start_address;
1001 break;
1002 case MXT_TOUCH_MULTI_T9:
1003 data->T9_reportid_min = min_id;
1004 data->T9_reportid_max = max_id;
1005 break;
1006 case MXT_SPT_GPIOPWM_T19:
1007 data->T19_reportid = min_id;
1008 break;
1012 return 0;
1015 static void mxt_free_object_table(struct mxt_data *data)
1017 kfree(data->object_table);
1018 data->object_table = NULL;
1019 data->T6_reportid = 0;
1020 data->T9_reportid_min = 0;
1021 data->T9_reportid_max = 0;
1022 data->T19_reportid = 0;
1025 static int mxt_read_t9_resolution(struct mxt_data *data)
1027 struct i2c_client *client = data->client;
1028 int error;
1029 struct t9_range range;
1030 unsigned char orient;
1031 struct mxt_object *object;
1033 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1034 if (!object)
1035 return -EINVAL;
1037 error = __mxt_read_reg(client,
1038 object->start_address + MXT_T9_RANGE,
1039 sizeof(range), &range);
1040 if (error)
1041 return error;
1043 le16_to_cpus(&range.x);
1044 le16_to_cpus(&range.y);
1046 error = __mxt_read_reg(client,
1047 object->start_address + MXT_T9_ORIENT,
1048 1, &orient);
1049 if (error)
1050 return error;
1052 /* Handle default values */
1053 if (range.x == 0)
1054 range.x = 1023;
1056 if (range.y == 0)
1057 range.y = 1023;
1059 if (orient & MXT_T9_ORIENT_SWITCH) {
1060 data->max_x = range.y;
1061 data->max_y = range.x;
1062 } else {
1063 data->max_x = range.x;
1064 data->max_y = range.y;
1067 dev_dbg(&client->dev,
1068 "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1070 return 0;
1073 static int mxt_initialize(struct mxt_data *data)
1075 struct i2c_client *client = data->client;
1076 struct mxt_info *info = &data->info;
1077 int error;
1079 error = mxt_get_info(data);
1080 if (error)
1081 return error;
1083 data->object_table = kcalloc(info->object_num,
1084 sizeof(struct mxt_object),
1085 GFP_KERNEL);
1086 if (!data->object_table) {
1087 dev_err(&client->dev, "Failed to allocate memory\n");
1088 return -ENOMEM;
1091 /* Get object table information */
1092 error = mxt_get_object_table(data);
1093 if (error) {
1094 dev_err(&client->dev, "Error %d reading object table\n", error);
1095 goto err_free_object_table;
1098 /* Check register init values */
1099 error = mxt_check_reg_init(data);
1100 if (error) {
1101 dev_err(&client->dev, "Error %d initializing configuration\n",
1102 error);
1103 goto err_free_object_table;
1106 error = mxt_read_t9_resolution(data);
1107 if (error) {
1108 dev_err(&client->dev, "Failed to initialize T9 resolution\n");
1109 goto err_free_object_table;
1112 dev_info(&client->dev,
1113 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1114 info->family_id, info->variant_id, info->version >> 4,
1115 info->version & 0xf, info->build, info->object_num);
1117 return 0;
1119 err_free_object_table:
1120 mxt_free_object_table(data);
1121 return error;
1124 /* Firmware Version is returned as Major.Minor.Build */
1125 static ssize_t mxt_fw_version_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1128 struct mxt_data *data = dev_get_drvdata(dev);
1129 struct mxt_info *info = &data->info;
1130 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
1131 info->version >> 4, info->version & 0xf, info->build);
1134 /* Hardware Version is returned as FamilyID.VariantID */
1135 static ssize_t mxt_hw_version_show(struct device *dev,
1136 struct device_attribute *attr, char *buf)
1138 struct mxt_data *data = dev_get_drvdata(dev);
1139 struct mxt_info *info = &data->info;
1140 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
1141 info->family_id, info->variant_id);
1144 static ssize_t mxt_show_instance(char *buf, int count,
1145 struct mxt_object *object, int instance,
1146 const u8 *val)
1148 int i;
1150 if (mxt_obj_instances(object) > 1)
1151 count += scnprintf(buf + count, PAGE_SIZE - count,
1152 "Instance %u\n", instance);
1154 for (i = 0; i < mxt_obj_size(object); i++)
1155 count += scnprintf(buf + count, PAGE_SIZE - count,
1156 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
1157 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
1159 return count;
1162 static ssize_t mxt_object_show(struct device *dev,
1163 struct device_attribute *attr, char *buf)
1165 struct mxt_data *data = dev_get_drvdata(dev);
1166 struct mxt_object *object;
1167 int count = 0;
1168 int i, j;
1169 int error;
1170 u8 *obuf;
1172 /* Pre-allocate buffer large enough to hold max sized object. */
1173 obuf = kmalloc(256, GFP_KERNEL);
1174 if (!obuf)
1175 return -ENOMEM;
1177 error = 0;
1178 for (i = 0; i < data->info.object_num; i++) {
1179 object = data->object_table + i;
1181 if (!mxt_object_readable(object->type))
1182 continue;
1184 count += scnprintf(buf + count, PAGE_SIZE - count,
1185 "T%u:\n", object->type);
1187 for (j = 0; j < mxt_obj_instances(object); j++) {
1188 u16 size = mxt_obj_size(object);
1189 u16 addr = object->start_address + j * size;
1191 error = __mxt_read_reg(data->client, addr, size, obuf);
1192 if (error)
1193 goto done;
1195 count = mxt_show_instance(buf, count, object, j, obuf);
1199 done:
1200 kfree(obuf);
1201 return error ?: count;
1204 static int mxt_check_firmware_format(struct device *dev,
1205 const struct firmware *fw)
1207 unsigned int pos = 0;
1208 char c;
1210 while (pos < fw->size) {
1211 c = *(fw->data + pos);
1213 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
1214 return 0;
1216 pos++;
1220 * To convert file try:
1221 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
1223 dev_err(dev, "Aborting: firmware file must be in binary format\n");
1225 return -EINVAL;
1228 static int mxt_load_fw(struct device *dev, const char *fn)
1230 struct mxt_data *data = dev_get_drvdata(dev);
1231 const struct firmware *fw = NULL;
1232 unsigned int frame_size;
1233 unsigned int pos = 0;
1234 unsigned int retry = 0;
1235 unsigned int frame = 0;
1236 int ret;
1238 ret = request_firmware(&fw, fn, dev);
1239 if (ret) {
1240 dev_err(dev, "Unable to open firmware %s\n", fn);
1241 return ret;
1244 /* Check for incorrect enc file */
1245 ret = mxt_check_firmware_format(dev, fw);
1246 if (ret)
1247 goto release_firmware;
1249 ret = mxt_lookup_bootloader_address(data);
1250 if (ret)
1251 goto release_firmware;
1253 /* Change to the bootloader mode */
1254 data->in_bootloader = true;
1256 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_BOOT_VALUE, false);
1257 if (ret)
1258 goto release_firmware;
1260 msleep(MXT_RESET_TIME);
1262 reinit_completion(&data->bl_completion);
1264 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
1265 if (ret)
1266 goto disable_irq;
1268 /* Unlock bootloader */
1269 mxt_unlock_bootloader(data);
1271 while (pos < fw->size) {
1272 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
1273 if (ret)
1274 goto disable_irq;
1276 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1278 /* Take account of CRC bytes */
1279 frame_size += 2;
1281 /* Write one frame to device */
1282 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
1283 if (ret)
1284 goto disable_irq;
1286 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
1287 if (ret) {
1288 retry++;
1290 /* Back off by 20ms per retry */
1291 msleep(retry * 20);
1293 if (retry > 20) {
1294 dev_err(dev, "Retry count exceeded\n");
1295 goto disable_irq;
1297 } else {
1298 retry = 0;
1299 pos += frame_size;
1300 frame++;
1303 if (frame % 50 == 0)
1304 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
1305 frame, pos, fw->size);
1308 /* Wait for flash. */
1309 ret = mxt_wait_for_completion(data, &data->bl_completion,
1310 MXT_FW_RESET_TIME);
1311 if (ret)
1312 goto disable_irq;
1314 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
1317 * Wait for device to reset. Some bootloader versions do not assert
1318 * the CHG line after bootloading has finished, so ignore potential
1319 * errors.
1321 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
1323 data->in_bootloader = false;
1325 disable_irq:
1326 disable_irq(data->irq);
1327 release_firmware:
1328 release_firmware(fw);
1329 return ret;
1332 static ssize_t mxt_update_fw_store(struct device *dev,
1333 struct device_attribute *attr,
1334 const char *buf, size_t count)
1336 struct mxt_data *data = dev_get_drvdata(dev);
1337 int error;
1339 error = mxt_load_fw(dev, MXT_FW_NAME);
1340 if (error) {
1341 dev_err(dev, "The firmware update failed(%d)\n", error);
1342 count = error;
1343 } else {
1344 dev_info(dev, "The firmware update succeeded\n");
1346 mxt_free_object_table(data);
1348 mxt_initialize(data);
1350 enable_irq(data->irq);
1352 error = mxt_make_highchg(data);
1353 if (error)
1354 return error;
1357 return count;
1360 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1361 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
1362 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1363 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
1365 static struct attribute *mxt_attrs[] = {
1366 &dev_attr_fw_version.attr,
1367 &dev_attr_hw_version.attr,
1368 &dev_attr_object.attr,
1369 &dev_attr_update_fw.attr,
1370 NULL
1373 static const struct attribute_group mxt_attr_group = {
1374 .attrs = mxt_attrs,
1377 static void mxt_start(struct mxt_data *data)
1379 /* Touch enable */
1380 mxt_write_object(data,
1381 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
1384 static void mxt_stop(struct mxt_data *data)
1386 /* Touch disable */
1387 mxt_write_object(data,
1388 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
1391 static int mxt_input_open(struct input_dev *dev)
1393 struct mxt_data *data = input_get_drvdata(dev);
1395 mxt_start(data);
1397 return 0;
1400 static void mxt_input_close(struct input_dev *dev)
1402 struct mxt_data *data = input_get_drvdata(dev);
1404 mxt_stop(data);
1407 static int mxt_probe(struct i2c_client *client,
1408 const struct i2c_device_id *id)
1410 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
1411 struct mxt_data *data;
1412 struct input_dev *input_dev;
1413 int error;
1414 unsigned int num_mt_slots;
1415 unsigned int mt_flags = 0;
1416 int i;
1418 if (!pdata)
1419 return -EINVAL;
1421 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
1422 input_dev = input_allocate_device();
1423 if (!data || !input_dev) {
1424 dev_err(&client->dev, "Failed to allocate memory\n");
1425 error = -ENOMEM;
1426 goto err_free_mem;
1429 input_dev->name = "Atmel maXTouch Touchscreen";
1430 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1431 client->adapter->nr, client->addr);
1433 input_dev->phys = data->phys;
1435 input_dev->id.bustype = BUS_I2C;
1436 input_dev->dev.parent = &client->dev;
1437 input_dev->open = mxt_input_open;
1438 input_dev->close = mxt_input_close;
1440 data->client = client;
1441 data->input_dev = input_dev;
1442 data->pdata = pdata;
1443 data->irq = client->irq;
1445 init_completion(&data->bl_completion);
1446 init_completion(&data->reset_completion);
1447 init_completion(&data->crc_completion);
1449 error = mxt_initialize(data);
1450 if (error)
1451 goto err_free_mem;
1453 __set_bit(EV_ABS, input_dev->evbit);
1454 __set_bit(EV_KEY, input_dev->evbit);
1455 __set_bit(BTN_TOUCH, input_dev->keybit);
1457 if (pdata->t19_num_keys) {
1458 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1460 for (i = 0; i < pdata->t19_num_keys; i++)
1461 if (pdata->t19_keymap[i] != KEY_RESERVED)
1462 input_set_capability(input_dev, EV_KEY,
1463 pdata->t19_keymap[i]);
1465 mt_flags |= INPUT_MT_POINTER;
1467 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1468 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1469 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1470 MXT_PIXELS_PER_MM);
1471 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1472 MXT_PIXELS_PER_MM);
1474 input_dev->name = "Atmel maXTouch Touchpad";
1477 /* For single touch */
1478 input_set_abs_params(input_dev, ABS_X,
1479 0, data->max_x, 0, 0);
1480 input_set_abs_params(input_dev, ABS_Y,
1481 0, data->max_y, 0, 0);
1482 input_set_abs_params(input_dev, ABS_PRESSURE,
1483 0, 255, 0, 0);
1485 /* For multi touch */
1486 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1487 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
1488 if (error)
1489 goto err_free_object;
1490 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1491 0, MXT_MAX_AREA, 0, 0);
1492 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1493 0, data->max_x, 0, 0);
1494 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1495 0, data->max_y, 0, 0);
1496 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1497 0, 255, 0, 0);
1499 input_set_drvdata(input_dev, data);
1500 i2c_set_clientdata(client, data);
1502 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
1503 pdata->irqflags | IRQF_ONESHOT,
1504 client->name, data);
1505 if (error) {
1506 dev_err(&client->dev, "Failed to register interrupt\n");
1507 goto err_free_object;
1510 error = mxt_make_highchg(data);
1511 if (error)
1512 goto err_free_irq;
1514 error = input_register_device(input_dev);
1515 if (error) {
1516 dev_err(&client->dev, "Error %d registering input device\n",
1517 error);
1518 goto err_free_irq;
1521 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
1522 if (error) {
1523 dev_err(&client->dev, "Failure %d creating sysfs group\n",
1524 error);
1525 goto err_unregister_device;
1528 return 0;
1530 err_unregister_device:
1531 input_unregister_device(input_dev);
1532 input_dev = NULL;
1533 err_free_irq:
1534 free_irq(client->irq, data);
1535 err_free_object:
1536 kfree(data->object_table);
1537 err_free_mem:
1538 input_free_device(input_dev);
1539 kfree(data);
1540 return error;
1543 static int mxt_remove(struct i2c_client *client)
1545 struct mxt_data *data = i2c_get_clientdata(client);
1547 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
1548 free_irq(data->irq, data);
1549 input_unregister_device(data->input_dev);
1550 kfree(data->object_table);
1551 kfree(data);
1553 return 0;
1556 #ifdef CONFIG_PM_SLEEP
1557 static int mxt_suspend(struct device *dev)
1559 struct i2c_client *client = to_i2c_client(dev);
1560 struct mxt_data *data = i2c_get_clientdata(client);
1561 struct input_dev *input_dev = data->input_dev;
1563 mutex_lock(&input_dev->mutex);
1565 if (input_dev->users)
1566 mxt_stop(data);
1568 mutex_unlock(&input_dev->mutex);
1570 return 0;
1573 static int mxt_resume(struct device *dev)
1575 struct i2c_client *client = to_i2c_client(dev);
1576 struct mxt_data *data = i2c_get_clientdata(client);
1577 struct input_dev *input_dev = data->input_dev;
1579 mxt_soft_reset(data);
1581 mutex_lock(&input_dev->mutex);
1583 if (input_dev->users)
1584 mxt_start(data);
1586 mutex_unlock(&input_dev->mutex);
1588 return 0;
1590 #endif
1592 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1594 static const struct i2c_device_id mxt_id[] = {
1595 { "qt602240_ts", 0 },
1596 { "atmel_mxt_ts", 0 },
1597 { "atmel_mxt_tp", 0 },
1598 { "mXT224", 0 },
1601 MODULE_DEVICE_TABLE(i2c, mxt_id);
1603 static struct i2c_driver mxt_driver = {
1604 .driver = {
1605 .name = "atmel_mxt_ts",
1606 .owner = THIS_MODULE,
1607 .pm = &mxt_pm_ops,
1609 .probe = mxt_probe,
1610 .remove = mxt_remove,
1611 .id_table = mxt_id,
1614 module_i2c_driver(mxt_driver);
1616 /* Module information */
1617 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
1618 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
1619 MODULE_LICENSE("GPL");