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>
33 #define MXT_FW_NAME "maxtouch.fw"
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
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)
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
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
216 u8 instances_minus_one
;
225 /* Each client has this additional 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
;
240 /* Cached parameters from object table */
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
)
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
:
300 static bool mxt_object_writable(unsigned int 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
:
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
);
346 ret
= wait_for_completion_interruptible_timeout(comp
, timeout
);
349 } else if (ret
== 0) {
350 dev_err(dev
, "Wait for completion timed out.\n");
356 static int mxt_bootloader_read(struct mxt_data
*data
,
357 u8
*val
, unsigned int count
)
362 msg
.addr
= data
->bootloader_addr
;
363 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
364 msg
.flags
|= I2C_M_RD
;
368 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
373 ret
= ret
< 0 ? ret
: -EIO
;
374 dev_err(&data
->client
->dev
, "%s: i2c recv failed (%d)\n",
381 static int mxt_bootloader_write(struct mxt_data
*data
,
382 const u8
* const val
, unsigned int count
)
387 msg
.addr
= data
->bootloader_addr
;
388 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
392 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
396 ret
= ret
< 0 ? ret
: -EIO
;
397 dev_err(&data
->client
->dev
, "%s: i2c send failed (%d)\n",
404 static int mxt_lookup_bootloader_address(struct mxt_data
*data
)
406 u8 appmode
= data
->client
->addr
;
416 bootloader
= appmode
- 0x26;
419 dev_err(&data
->client
->dev
,
420 "Appmode i2c address 0x%02x not found\n",
425 data
->bootloader_addr
= bootloader
;
429 static u8
mxt_get_bootloader_version(struct mxt_data
*data
, u8 val
)
431 struct device
*dev
= &data
->client
->dev
;
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__
);
440 dev_dbg(dev
, "Bootloader ID:%d Version:%d\n", buf
[1], buf
[2]);
444 dev_dbg(dev
, "Bootloader ID:%d\n", val
& MXT_BOOT_ID_MASK
);
450 static int mxt_check_bootloader(struct mxt_data
*data
, unsigned int state
)
452 struct device
*dev
= &data
->client
->dev
;
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
,
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
);
478 ret
= mxt_bootloader_read(data
, &val
, 1);
482 if (state
== MXT_WAITING_BOOTLOAD_CMD
)
483 val
= mxt_get_bootloader_version(data
, val
);
486 case MXT_WAITING_BOOTLOAD_CMD
:
487 case MXT_WAITING_FRAME_DATA
:
488 val
&= ~MXT_BOOT_STATUS_MASK
;
490 case MXT_FRAME_CRC_PASS
:
491 if (val
== MXT_FRAME_CRC_CHECK
) {
493 } else if (val
== MXT_FRAME_CRC_FAIL
) {
494 dev_err(dev
, "Bootloader CRC fail\n");
503 dev_err(dev
, "Invalid bootloader state %02X != %02X\n",
511 static int mxt_unlock_bootloader(struct mxt_data
*data
)
516 buf
[0] = MXT_UNLOCK_CMD_LSB
;
517 buf
[1] = MXT_UNLOCK_CMD_MSB
;
519 ret
= mxt_bootloader_write(data
, buf
, 2);
526 static int __mxt_read_reg(struct i2c_client
*client
,
527 u16 reg
, u16 len
, void *val
)
529 struct i2c_msg xfer
[2];
534 buf
[1] = (reg
>> 8) & 0xff;
537 xfer
[0].addr
= client
->addr
;
543 xfer
[1].addr
= client
->addr
;
544 xfer
[1].flags
= I2C_M_RD
;
548 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
554 dev_err(&client
->dev
, "%s: i2c transfer failed (%d)\n",
561 static int __mxt_write_reg(struct i2c_client
*client
, u16 reg
, u16 len
,
569 buf
= kmalloc(count
, GFP_KERNEL
);
574 buf
[1] = (reg
>> 8) & 0xff;
575 memcpy(&buf
[2], val
, len
);
577 ret
= i2c_master_send(client
, buf
, count
);
583 dev_err(&client
->dev
, "%s: i2c send failed (%d)\n",
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
;
602 for (i
= 0; i
< data
->info
.object_num
; i
++) {
603 object
= data
->object_table
+ i
;
604 if (object
->type
== type
)
608 dev_err(&data
->client
->dev
, "Invalid object type T%u\n", type
);
612 static int mxt_read_message(struct mxt_data
*data
,
613 struct mxt_message
*message
)
615 struct mxt_object
*object
;
618 object
= mxt_get_object(data
, MXT_GEN_MESSAGE_T5
);
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
;
633 object
= mxt_get_object(data
, type
);
634 if (!object
|| offset
>= mxt_obj_size(object
))
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
;
648 /* Active-low switch */
649 for (i
= 0; i
< pdata
->t19_num_keys
; i
++) {
650 if (pdata
->t19_keymap
[i
] == KEY_RESERVED
)
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
;
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)
680 if (data
->max_y
< 1024)
683 area
= message
->message
[4];
684 amplitude
= message
->message
[5];
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
707 if (status
& MXT_T9_RELEASE
) {
708 input_mt_report_slot_state(input_dev
,
710 mxt_input_sync(input_dev
);
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
);
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
;
742 bool update_input
= false;
746 if (mxt_read_message(data
, &message
)) {
747 dev_err(dev
, "Failed to read message\n");
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
);
771 } else if (message
.reportid
== data
->T19_reportid
) {
772 mxt_input_button(data
, &message
);
775 mxt_dump_message(dev
, &message
);
777 } while (reportid
!= 0xff);
780 mxt_input_sync(data
->input_dev
);
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
);
795 return mxt_process_messages_until_invalid(data
);
798 static int mxt_t6_command(struct mxt_data
*data
, u16 cmd_offset
,
803 int timeout_counter
= 0;
806 reg
= data
->T6_address
+ cmd_offset
;
808 ret
= mxt_write_reg(data
->client
, reg
, value
);
817 ret
= __mxt_read_reg(data
->client
, reg
, 1, &command_register
);
820 } while (command_register
!= 0 && timeout_counter
++ <= 100);
822 if (timeout_counter
> 100) {
823 dev_err(&data
->client
->dev
, "Command failed!\n");
830 static int mxt_soft_reset(struct mxt_data
*data
)
832 struct device
*dev
= &data
->client
->dev
;
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);
843 ret
= mxt_wait_for_completion(data
, &data
->reset_completion
,
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
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
;
878 if (!pdata
->config
) {
879 dev_dbg(dev
, "No cfg data defined, skipping reg init\n");
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
);
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
))
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");
905 ret
= __mxt_write_reg(data
->client
, object
->start_address
,
906 size
, &pdata
->config
[index
]);
912 mxt_update_crc(data
, MXT_COMMAND_BACKUPNV
, MXT_BACKUP_VALUE
);
914 ret
= mxt_soft_reset(data
);
918 dev_info(dev
, "Config successfully updated\n");
923 static int mxt_make_highchg(struct mxt_data
*data
)
925 struct device
*dev
= &data
->client
->dev
;
926 struct mxt_message message
;
930 /* Read dummy message to make high CHG pin */
932 error
= mxt_read_message(data
, &message
);
935 } while (message
.reportid
!= 0xff && --count
);
938 dev_err(dev
, "CHG pin isn't cleared\n");
945 static int mxt_get_info(struct mxt_data
*data
)
947 struct i2c_client
*client
= data
->client
;
948 struct mxt_info
*info
= &data
->info
;
951 /* Read 7-byte info block starting at address 0 */
952 error
= __mxt_read_reg(client
, MXT_INFO
, sizeof(*info
), info
);
959 static int mxt_get_object_table(struct mxt_data
*data
)
961 struct i2c_client
*client
= data
->client
;
967 table_size
= data
->info
.object_num
* sizeof(struct mxt_object
);
968 error
= __mxt_read_reg(client
, MXT_OBJECT_START
, table_size
,
973 /* Valid Report IDs start counting from 1 */
975 for (i
= 0; i
< data
->info
.object_num
; i
++) {
976 struct mxt_object
*object
= data
->object_table
+ i
;
979 le16_to_cpus(&object
->start_address
);
981 if (object
->num_report_ids
) {
983 reportid
+= object
->num_report_ids
*
984 mxt_obj_instances(object
);
985 max_id
= reportid
- 1;
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
),
997 switch (object
->type
) {
998 case MXT_GEN_COMMAND_T6
:
999 data
->T6_reportid
= min_id
;
1000 data
->T6_address
= object
->start_address
;
1002 case MXT_TOUCH_MULTI_T9
:
1003 data
->T9_reportid_min
= min_id
;
1004 data
->T9_reportid_max
= max_id
;
1006 case MXT_SPT_GPIOPWM_T19
:
1007 data
->T19_reportid
= min_id
;
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
;
1029 struct t9_range range
;
1030 unsigned char orient
;
1031 struct mxt_object
*object
;
1033 object
= mxt_get_object(data
, MXT_TOUCH_MULTI_T9
);
1037 error
= __mxt_read_reg(client
,
1038 object
->start_address
+ MXT_T9_RANGE
,
1039 sizeof(range
), &range
);
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
,
1052 /* Handle default values */
1059 if (orient
& MXT_T9_ORIENT_SWITCH
) {
1060 data
->max_x
= range
.y
;
1061 data
->max_y
= range
.x
;
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
);
1073 static int mxt_initialize(struct mxt_data
*data
)
1075 struct i2c_client
*client
= data
->client
;
1076 struct mxt_info
*info
= &data
->info
;
1079 error
= mxt_get_info(data
);
1083 data
->object_table
= kcalloc(info
->object_num
,
1084 sizeof(struct mxt_object
),
1086 if (!data
->object_table
) {
1087 dev_err(&client
->dev
, "Failed to allocate memory\n");
1091 /* Get object table information */
1092 error
= mxt_get_object_table(data
);
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
);
1101 dev_err(&client
->dev
, "Error %d initializing configuration\n",
1103 goto err_free_object_table
;
1106 error
= mxt_read_t9_resolution(data
);
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
);
1119 err_free_object_table
:
1120 mxt_free_object_table(data
);
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
,
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");
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
;
1172 /* Pre-allocate buffer large enough to hold max sized object. */
1173 obuf
= kmalloc(256, GFP_KERNEL
);
1178 for (i
= 0; i
< data
->info
.object_num
; i
++) {
1179 object
= data
->object_table
+ i
;
1181 if (!mxt_object_readable(object
->type
))
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
);
1195 count
= mxt_show_instance(buf
, count
, object
, j
, 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;
1210 while (pos
< fw
->size
) {
1211 c
= *(fw
->data
+ pos
);
1213 if (c
< '0' || (c
> '9' && c
< 'A') || c
> 'F')
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");
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;
1238 ret
= request_firmware(&fw
, fn
, dev
);
1240 dev_err(dev
, "Unable to open firmware %s\n", fn
);
1244 /* Check for incorrect enc file */
1245 ret
= mxt_check_firmware_format(dev
, fw
);
1247 goto release_firmware
;
1249 ret
= mxt_lookup_bootloader_address(data
);
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);
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
);
1268 /* Unlock bootloader */
1269 mxt_unlock_bootloader(data
);
1271 while (pos
< fw
->size
) {
1272 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
);
1276 frame_size
= ((*(fw
->data
+ pos
) << 8) | *(fw
->data
+ pos
+ 1));
1278 /* Take account of CRC bytes */
1281 /* Write one frame to device */
1282 ret
= mxt_bootloader_write(data
, fw
->data
+ pos
, frame_size
);
1286 ret
= mxt_check_bootloader(data
, MXT_FRAME_CRC_PASS
);
1290 /* Back off by 20ms per retry */
1294 dev_err(dev
, "Retry count exceeded\n");
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
,
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
1321 mxt_wait_for_completion(data
, &data
->bl_completion
, MXT_FW_RESET_TIME
);
1323 data
->in_bootloader
= false;
1326 disable_irq(data
->irq
);
1328 release_firmware(fw
);
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
);
1339 error
= mxt_load_fw(dev
, MXT_FW_NAME
);
1341 dev_err(dev
, "The firmware update failed(%d)\n", error
);
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
);
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
,
1373 static const struct attribute_group mxt_attr_group
= {
1377 static void mxt_start(struct mxt_data
*data
)
1380 mxt_write_object(data
,
1381 MXT_TOUCH_MULTI_T9
, MXT_TOUCH_CTRL
, 0x83);
1384 static void mxt_stop(struct mxt_data
*data
)
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
);
1400 static void mxt_input_close(struct input_dev
*dev
)
1402 struct mxt_data
*data
= input_get_drvdata(dev
);
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
;
1414 unsigned int num_mt_slots
;
1415 unsigned int mt_flags
= 0;
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");
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
);
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
,
1471 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
,
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
,
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
);
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
,
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
);
1506 dev_err(&client
->dev
, "Failed to register interrupt\n");
1507 goto err_free_object
;
1510 error
= mxt_make_highchg(data
);
1514 error
= input_register_device(input_dev
);
1516 dev_err(&client
->dev
, "Error %d registering input device\n",
1521 error
= sysfs_create_group(&client
->dev
.kobj
, &mxt_attr_group
);
1523 dev_err(&client
->dev
, "Failure %d creating sysfs group\n",
1525 goto err_unregister_device
;
1530 err_unregister_device
:
1531 input_unregister_device(input_dev
);
1534 free_irq(client
->irq
, data
);
1536 kfree(data
->object_table
);
1538 input_free_device(input_dev
);
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
);
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
)
1568 mutex_unlock(&input_dev
->mutex
);
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
)
1586 mutex_unlock(&input_dev
->mutex
);
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 },
1601 MODULE_DEVICE_TABLE(i2c
, mxt_id
);
1603 static struct i2c_driver mxt_driver
= {
1605 .name
= "atmel_mxt_ts",
1606 .owner
= THIS_MODULE
,
1610 .remove
= mxt_remove
,
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");