2 * Atmel maXTouch Touchscreen driver
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/firmware.h>
17 #include <linux/i2c.h>
18 #include <linux/i2c/atmel_mxt_ts.h>
19 #include <linux/input/mt.h>
20 #include <linux/interrupt.h>
21 #include <linux/slab.h>
29 #define MXT_APP_LOW 0x4a
30 #define MXT_APP_HIGH 0x4b
31 #define MXT_BOOT_LOW 0x24
32 #define MXT_BOOT_HIGH 0x25
35 #define MXT_FW_NAME "maxtouch.fw"
39 #define MXT_FAMILY_ID 0x00
40 #define MXT_VARIANT_ID 0x01
41 #define MXT_VERSION 0x02
42 #define MXT_BUILD 0x03
43 #define MXT_MATRIX_X_SIZE 0x04
44 #define MXT_MATRIX_Y_SIZE 0x05
45 #define MXT_OBJECT_NUM 0x06
46 #define MXT_OBJECT_START 0x07
48 #define MXT_OBJECT_SIZE 6
51 #define MXT_DEBUG_DIAGNOSTIC_T37 37
52 #define MXT_GEN_MESSAGE_T5 5
53 #define MXT_GEN_COMMAND_T6 6
54 #define MXT_GEN_POWER_T7 7
55 #define MXT_GEN_ACQUIRE_T8 8
56 #define MXT_GEN_DATASOURCE_T53 53
57 #define MXT_TOUCH_MULTI_T9 9
58 #define MXT_TOUCH_KEYARRAY_T15 15
59 #define MXT_TOUCH_PROXIMITY_T23 23
60 #define MXT_TOUCH_PROXKEY_T52 52
61 #define MXT_PROCI_GRIPFACE_T20 20
62 #define MXT_PROCG_NOISE_T22 22
63 #define MXT_PROCI_ONETOUCH_T24 24
64 #define MXT_PROCI_TWOTOUCH_T27 27
65 #define MXT_PROCI_GRIP_T40 40
66 #define MXT_PROCI_PALM_T41 41
67 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
68 #define MXT_PROCI_STYLUS_T47 47
69 #define MXT_PROCG_NOISESUPPRESSION_T48 48
70 #define MXT_SPT_COMMSCONFIG_T18 18
71 #define MXT_SPT_GPIOPWM_T19 19
72 #define MXT_SPT_SELFTEST_T25 25
73 #define MXT_SPT_CTECONFIG_T28 28
74 #define MXT_SPT_USERDATA_T38 38
75 #define MXT_SPT_DIGITIZER_T43 43
76 #define MXT_SPT_MESSAGECOUNT_T44 44
77 #define MXT_SPT_CTECONFIG_T46 46
79 /* MXT_GEN_COMMAND_T6 field */
80 #define MXT_COMMAND_RESET 0
81 #define MXT_COMMAND_BACKUPNV 1
82 #define MXT_COMMAND_CALIBRATE 2
83 #define MXT_COMMAND_REPORTALL 3
84 #define MXT_COMMAND_DIAGNOSTIC 5
86 /* MXT_GEN_POWER_T7 field */
87 #define MXT_POWER_IDLEACQINT 0
88 #define MXT_POWER_ACTVACQINT 1
89 #define MXT_POWER_ACTV2IDLETO 2
91 /* MXT_GEN_ACQUIRE_T8 field */
92 #define MXT_ACQUIRE_CHRGTIME 0
93 #define MXT_ACQUIRE_TCHDRIFT 2
94 #define MXT_ACQUIRE_DRIFTST 3
95 #define MXT_ACQUIRE_TCHAUTOCAL 4
96 #define MXT_ACQUIRE_SYNC 5
97 #define MXT_ACQUIRE_ATCHCALST 6
98 #define MXT_ACQUIRE_ATCHCALSTHR 7
100 /* MXT_TOUCH_MULTI_T9 field */
101 #define MXT_TOUCH_CTRL 0
102 #define MXT_TOUCH_XORIGIN 1
103 #define MXT_TOUCH_YORIGIN 2
104 #define MXT_TOUCH_XSIZE 3
105 #define MXT_TOUCH_YSIZE 4
106 #define MXT_TOUCH_BLEN 6
107 #define MXT_TOUCH_TCHTHR 7
108 #define MXT_TOUCH_TCHDI 8
109 #define MXT_TOUCH_ORIENT 9
110 #define MXT_TOUCH_MOVHYSTI 11
111 #define MXT_TOUCH_MOVHYSTN 12
112 #define MXT_TOUCH_NUMTOUCH 14
113 #define MXT_TOUCH_MRGHYST 15
114 #define MXT_TOUCH_MRGTHR 16
115 #define MXT_TOUCH_AMPHYST 17
116 #define MXT_TOUCH_XRANGE_LSB 18
117 #define MXT_TOUCH_XRANGE_MSB 19
118 #define MXT_TOUCH_YRANGE_LSB 20
119 #define MXT_TOUCH_YRANGE_MSB 21
120 #define MXT_TOUCH_XLOCLIP 22
121 #define MXT_TOUCH_XHICLIP 23
122 #define MXT_TOUCH_YLOCLIP 24
123 #define MXT_TOUCH_YHICLIP 25
124 #define MXT_TOUCH_XEDGECTRL 26
125 #define MXT_TOUCH_XEDGEDIST 27
126 #define MXT_TOUCH_YEDGECTRL 28
127 #define MXT_TOUCH_YEDGEDIST 29
128 #define MXT_TOUCH_JUMPLIMIT 30
130 /* MXT_PROCI_GRIPFACE_T20 field */
131 #define MXT_GRIPFACE_CTRL 0
132 #define MXT_GRIPFACE_XLOGRIP 1
133 #define MXT_GRIPFACE_XHIGRIP 2
134 #define MXT_GRIPFACE_YLOGRIP 3
135 #define MXT_GRIPFACE_YHIGRIP 4
136 #define MXT_GRIPFACE_MAXTCHS 5
137 #define MXT_GRIPFACE_SZTHR1 7
138 #define MXT_GRIPFACE_SZTHR2 8
139 #define MXT_GRIPFACE_SHPTHR1 9
140 #define MXT_GRIPFACE_SHPTHR2 10
141 #define MXT_GRIPFACE_SUPEXTTO 11
143 /* MXT_PROCI_NOISE field */
144 #define MXT_NOISE_CTRL 0
145 #define MXT_NOISE_OUTFLEN 1
146 #define MXT_NOISE_GCAFUL_LSB 3
147 #define MXT_NOISE_GCAFUL_MSB 4
148 #define MXT_NOISE_GCAFLL_LSB 5
149 #define MXT_NOISE_GCAFLL_MSB 6
150 #define MXT_NOISE_ACTVGCAFVALID 7
151 #define MXT_NOISE_NOISETHR 8
152 #define MXT_NOISE_FREQHOPSCALE 10
153 #define MXT_NOISE_FREQ0 11
154 #define MXT_NOISE_FREQ1 12
155 #define MXT_NOISE_FREQ2 13
156 #define MXT_NOISE_FREQ3 14
157 #define MXT_NOISE_FREQ4 15
158 #define MXT_NOISE_IDLEGCAFVALID 16
160 /* MXT_SPT_COMMSCONFIG_T18 */
161 #define MXT_COMMS_CTRL 0
162 #define MXT_COMMS_CMD 1
164 /* MXT_SPT_CTECONFIG_T28 field */
165 #define MXT_CTE_CTRL 0
166 #define MXT_CTE_CMD 1
167 #define MXT_CTE_MODE 2
168 #define MXT_CTE_IDLEGCAFDEPTH 3
169 #define MXT_CTE_ACTVGCAFDEPTH 4
170 #define MXT_CTE_VOLTAGE 5
172 #define MXT_VOLTAGE_DEFAULT 2700000
173 #define MXT_VOLTAGE_STEP 10000
175 /* Define for MXT_GEN_COMMAND_T6 */
176 #define MXT_BOOT_VALUE 0xa5
177 #define MXT_BACKUP_VALUE 0x55
178 #define MXT_BACKUP_TIME 50 /* msec */
179 #define MXT_RESET_TIME 200 /* msec */
181 #define MXT_FWRESET_TIME 175 /* msec */
183 /* MXT_SPT_GPIOPWM_T19 field */
184 #define MXT_GPIO0_MASK 0x04
185 #define MXT_GPIO1_MASK 0x08
186 #define MXT_GPIO2_MASK 0x10
187 #define MXT_GPIO3_MASK 0x20
189 /* Command to unlock bootloader */
190 #define MXT_UNLOCK_CMD_MSB 0xaa
191 #define MXT_UNLOCK_CMD_LSB 0xdc
193 /* Bootloader mode status */
194 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
195 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
196 #define MXT_FRAME_CRC_CHECK 0x02
197 #define MXT_FRAME_CRC_FAIL 0x03
198 #define MXT_FRAME_CRC_PASS 0x04
199 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
200 #define MXT_BOOT_STATUS_MASK 0x3f
203 #define MXT_UNGRIP (1 << 0)
204 #define MXT_SUPPRESS (1 << 1)
205 #define MXT_AMP (1 << 2)
206 #define MXT_VECTOR (1 << 3)
207 #define MXT_MOVE (1 << 4)
208 #define MXT_RELEASE (1 << 5)
209 #define MXT_PRESS (1 << 6)
210 #define MXT_DETECT (1 << 7)
212 /* Touch orient bits */
213 #define MXT_XY_SWITCH (1 << 0)
214 #define MXT_X_INVERT (1 << 1)
215 #define MXT_Y_INVERT (1 << 2)
217 /* Touchscreen absolute values */
218 #define MXT_MAX_AREA 0xff
220 #define MXT_PIXELS_PER_MM 20
235 u8 size
; /* Size of each instance - 1 */
236 u8 instances
; /* Number of instances - 1 */
245 /* Each client has this additional data */
247 struct i2c_client
*client
;
248 struct input_dev
*input_dev
;
249 char phys
[64]; /* device physical location */
250 const struct mxt_platform_data
*pdata
;
251 struct mxt_object
*object_table
;
252 struct mxt_info info
;
259 /* Cached parameters from object table */
266 static bool mxt_object_readable(unsigned int type
)
269 case MXT_GEN_COMMAND_T6
:
270 case MXT_GEN_POWER_T7
:
271 case MXT_GEN_ACQUIRE_T8
:
272 case MXT_GEN_DATASOURCE_T53
:
273 case MXT_TOUCH_MULTI_T9
:
274 case MXT_TOUCH_KEYARRAY_T15
:
275 case MXT_TOUCH_PROXIMITY_T23
:
276 case MXT_TOUCH_PROXKEY_T52
:
277 case MXT_PROCI_GRIPFACE_T20
:
278 case MXT_PROCG_NOISE_T22
:
279 case MXT_PROCI_ONETOUCH_T24
:
280 case MXT_PROCI_TWOTOUCH_T27
:
281 case MXT_PROCI_GRIP_T40
:
282 case MXT_PROCI_PALM_T41
:
283 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
284 case MXT_PROCI_STYLUS_T47
:
285 case MXT_PROCG_NOISESUPPRESSION_T48
:
286 case MXT_SPT_COMMSCONFIG_T18
:
287 case MXT_SPT_GPIOPWM_T19
:
288 case MXT_SPT_SELFTEST_T25
:
289 case MXT_SPT_CTECONFIG_T28
:
290 case MXT_SPT_USERDATA_T38
:
291 case MXT_SPT_DIGITIZER_T43
:
292 case MXT_SPT_CTECONFIG_T46
:
299 static bool mxt_object_writable(unsigned int type
)
302 case MXT_GEN_COMMAND_T6
:
303 case MXT_GEN_POWER_T7
:
304 case MXT_GEN_ACQUIRE_T8
:
305 case MXT_TOUCH_MULTI_T9
:
306 case MXT_TOUCH_KEYARRAY_T15
:
307 case MXT_TOUCH_PROXIMITY_T23
:
308 case MXT_TOUCH_PROXKEY_T52
:
309 case MXT_PROCI_GRIPFACE_T20
:
310 case MXT_PROCG_NOISE_T22
:
311 case MXT_PROCI_ONETOUCH_T24
:
312 case MXT_PROCI_TWOTOUCH_T27
:
313 case MXT_PROCI_GRIP_T40
:
314 case MXT_PROCI_PALM_T41
:
315 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
316 case MXT_PROCI_STYLUS_T47
:
317 case MXT_PROCG_NOISESUPPRESSION_T48
:
318 case MXT_SPT_COMMSCONFIG_T18
:
319 case MXT_SPT_GPIOPWM_T19
:
320 case MXT_SPT_SELFTEST_T25
:
321 case MXT_SPT_CTECONFIG_T28
:
322 case MXT_SPT_DIGITIZER_T43
:
323 case MXT_SPT_CTECONFIG_T46
:
330 static void mxt_dump_message(struct device
*dev
,
331 struct mxt_message
*message
)
333 dev_dbg(dev
, "reportid: %u\tmessage: %*ph\n",
334 message
->reportid
, 7, message
->message
);
337 static int mxt_check_bootloader(struct i2c_client
*client
,
343 if (i2c_master_recv(client
, &val
, 1) != 1) {
344 dev_err(&client
->dev
, "%s: i2c recv failed\n", __func__
);
349 case MXT_WAITING_BOOTLOAD_CMD
:
350 case MXT_WAITING_FRAME_DATA
:
351 val
&= ~MXT_BOOT_STATUS_MASK
;
353 case MXT_FRAME_CRC_PASS
:
354 if (val
== MXT_FRAME_CRC_CHECK
)
362 dev_err(&client
->dev
, "Unvalid bootloader mode state\n");
369 static int mxt_unlock_bootloader(struct i2c_client
*client
)
373 buf
[0] = MXT_UNLOCK_CMD_LSB
;
374 buf
[1] = MXT_UNLOCK_CMD_MSB
;
376 if (i2c_master_send(client
, buf
, 2) != 2) {
377 dev_err(&client
->dev
, "%s: i2c send failed\n", __func__
);
384 static int mxt_fw_write(struct i2c_client
*client
,
385 const u8
*data
, unsigned int frame_size
)
387 if (i2c_master_send(client
, data
, frame_size
) != frame_size
) {
388 dev_err(&client
->dev
, "%s: i2c send failed\n", __func__
);
395 static int __mxt_read_reg(struct i2c_client
*client
,
396 u16 reg
, u16 len
, void *val
)
398 struct i2c_msg xfer
[2];
403 buf
[1] = (reg
>> 8) & 0xff;
406 xfer
[0].addr
= client
->addr
;
412 xfer
[1].addr
= client
->addr
;
413 xfer
[1].flags
= I2C_M_RD
;
417 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
423 dev_err(&client
->dev
, "%s: i2c transfer failed (%d)\n",
430 static int mxt_read_reg(struct i2c_client
*client
, u16 reg
, u8
*val
)
432 return __mxt_read_reg(client
, reg
, 1, val
);
435 static int __mxt_write_reg(struct i2c_client
*client
, u16 reg
, u16 len
,
443 buf
= kmalloc(count
, GFP_KERNEL
);
448 buf
[1] = (reg
>> 8) & 0xff;
449 memcpy(&buf
[2], val
, len
);
451 ret
= i2c_master_send(client
, buf
, count
);
457 dev_err(&client
->dev
, "%s: i2c send failed (%d)\n",
465 static int mxt_write_reg(struct i2c_client
*client
, u16 reg
, u8 val
)
467 return __mxt_write_reg(client
, reg
, 1, &val
);
470 static struct mxt_object
*
471 mxt_get_object(struct mxt_data
*data
, u8 type
)
473 struct mxt_object
*object
;
476 for (i
= 0; i
< data
->info
.object_num
; i
++) {
477 object
= data
->object_table
+ i
;
478 if (object
->type
== type
)
482 dev_err(&data
->client
->dev
, "Invalid object type\n");
486 static int mxt_read_message(struct mxt_data
*data
,
487 struct mxt_message
*message
)
489 struct mxt_object
*object
;
492 object
= mxt_get_object(data
, MXT_GEN_MESSAGE_T5
);
496 reg
= object
->start_address
;
497 return __mxt_read_reg(data
->client
, reg
,
498 sizeof(struct mxt_message
), message
);
501 static int mxt_write_object(struct mxt_data
*data
,
502 u8 type
, u8 offset
, u8 val
)
504 struct mxt_object
*object
;
507 object
= mxt_get_object(data
, type
);
508 if (!object
|| offset
>= object
->size
+ 1)
511 reg
= object
->start_address
;
512 return mxt_write_reg(data
->client
, reg
+ offset
, val
);
515 static void mxt_input_button(struct mxt_data
*data
, struct mxt_message
*message
)
517 struct input_dev
*input
= data
->input_dev
;
521 /* Active-low switch */
522 for (i
= 0; i
< MXT_NUM_GPIO
; i
++) {
523 if (data
->pdata
->key_map
[i
] == KEY_RESERVED
)
525 button
= !(message
->message
[0] & MXT_GPIO0_MASK
<< i
);
526 input_report_key(input
, data
->pdata
->key_map
[i
], button
);
530 static void mxt_input_touchevent(struct mxt_data
*data
,
531 struct mxt_message
*message
, int id
)
533 struct device
*dev
= &data
->client
->dev
;
534 u8 status
= message
->message
[0];
535 struct input_dev
*input_dev
= data
->input_dev
;
541 x
= (message
->message
[1] << 4) | ((message
->message
[3] >> 4) & 0xf);
542 y
= (message
->message
[2] << 4) | ((message
->message
[3] & 0xf));
543 if (data
->max_x
< 1024)
545 if (data
->max_y
< 1024)
548 area
= message
->message
[4];
549 pressure
= message
->message
[5];
552 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
554 (status
& MXT_DETECT
) ? 'D' : '.',
555 (status
& MXT_PRESS
) ? 'P' : '.',
556 (status
& MXT_RELEASE
) ? 'R' : '.',
557 (status
& MXT_MOVE
) ? 'M' : '.',
558 (status
& MXT_VECTOR
) ? 'V' : '.',
559 (status
& MXT_AMP
) ? 'A' : '.',
560 (status
& MXT_SUPPRESS
) ? 'S' : '.',
561 (status
& MXT_UNGRIP
) ? 'U' : '.',
562 x
, y
, area
, pressure
);
564 input_mt_slot(input_dev
, id
);
565 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
,
566 status
& MXT_DETECT
);
568 if (status
& MXT_DETECT
) {
569 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
570 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
571 input_report_abs(input_dev
, ABS_MT_PRESSURE
, pressure
);
572 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, area
);
576 static unsigned mxt_extract_T6_csum(const u8
*csum
)
578 return csum
[0] | (csum
[1] << 8) | (csum
[2] << 16);
581 static bool mxt_is_T9_message(struct mxt_data
*data
, struct mxt_message
*msg
)
583 u8 id
= msg
->reportid
;
584 return (id
>= data
->T9_reportid_min
&& id
<= data
->T9_reportid_max
);
587 static irqreturn_t
mxt_interrupt(int irq
, void *dev_id
)
589 struct mxt_data
*data
= dev_id
;
590 struct mxt_message message
;
591 const u8
*payload
= &message
.message
[0];
592 struct device
*dev
= &data
->client
->dev
;
594 bool update_input
= false;
597 if (mxt_read_message(data
, &message
)) {
598 dev_err(dev
, "Failed to read message\n");
602 reportid
= message
.reportid
;
604 if (reportid
== data
->T6_reportid
) {
605 u8 status
= payload
[0];
606 unsigned csum
= mxt_extract_T6_csum(&payload
[1]);
607 dev_dbg(dev
, "Status: %02x Config Checksum: %06x\n",
609 } else if (mxt_is_T9_message(data
, &message
)) {
610 int id
= reportid
- data
->T9_reportid_min
;
611 mxt_input_touchevent(data
, &message
, id
);
613 } else if (message
.reportid
== data
->T19_reportid
) {
614 mxt_input_button(data
, &message
);
617 mxt_dump_message(dev
, &message
);
619 } while (reportid
!= 0xff);
622 input_mt_report_pointer_emulation(data
->input_dev
, false);
623 input_sync(data
->input_dev
);
630 static int mxt_check_reg_init(struct mxt_data
*data
)
632 const struct mxt_platform_data
*pdata
= data
->pdata
;
633 struct mxt_object
*object
;
634 struct device
*dev
= &data
->client
->dev
;
639 if (!pdata
->config
) {
640 dev_dbg(dev
, "No cfg data defined, skipping reg init\n");
644 for (i
= 0; i
< data
->info
.object_num
; i
++) {
645 object
= data
->object_table
+ i
;
647 if (!mxt_object_writable(object
->type
))
650 size
= (object
->size
+ 1) * (object
->instances
+ 1);
651 if (index
+ size
> pdata
->config_length
) {
652 dev_err(dev
, "Not enough config data!\n");
656 ret
= __mxt_write_reg(data
->client
, object
->start_address
,
657 size
, &pdata
->config
[index
]);
666 static int mxt_make_highchg(struct mxt_data
*data
)
668 struct device
*dev
= &data
->client
->dev
;
669 struct mxt_message message
;
673 /* Read dummy message to make high CHG pin */
675 error
= mxt_read_message(data
, &message
);
678 } while (message
.reportid
!= 0xff && --count
);
681 dev_err(dev
, "CHG pin isn't cleared\n");
688 static void mxt_handle_pdata(struct mxt_data
*data
)
690 const struct mxt_platform_data
*pdata
= data
->pdata
;
693 /* Set touchscreen lines */
694 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
, MXT_TOUCH_XSIZE
,
696 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
, MXT_TOUCH_YSIZE
,
699 /* Set touchscreen orient */
700 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
, MXT_TOUCH_ORIENT
,
703 /* Set touchscreen burst length */
704 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
705 MXT_TOUCH_BLEN
, pdata
->blen
);
707 /* Set touchscreen threshold */
708 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
709 MXT_TOUCH_TCHTHR
, pdata
->threshold
);
711 /* Set touchscreen resolution */
712 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
713 MXT_TOUCH_XRANGE_LSB
, (pdata
->x_size
- 1) & 0xff);
714 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
715 MXT_TOUCH_XRANGE_MSB
, (pdata
->x_size
- 1) >> 8);
716 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
717 MXT_TOUCH_YRANGE_LSB
, (pdata
->y_size
- 1) & 0xff);
718 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
719 MXT_TOUCH_YRANGE_MSB
, (pdata
->y_size
- 1) >> 8);
721 /* Set touchscreen voltage */
722 if (pdata
->voltage
) {
723 if (pdata
->voltage
< MXT_VOLTAGE_DEFAULT
) {
724 voltage
= (MXT_VOLTAGE_DEFAULT
- pdata
->voltage
) /
726 voltage
= 0xff - voltage
+ 1;
728 voltage
= (pdata
->voltage
- MXT_VOLTAGE_DEFAULT
) /
731 mxt_write_object(data
, MXT_SPT_CTECONFIG_T28
,
732 MXT_CTE_VOLTAGE
, voltage
);
736 static int mxt_get_info(struct mxt_data
*data
)
738 struct i2c_client
*client
= data
->client
;
739 struct mxt_info
*info
= &data
->info
;
742 /* Read 7-byte info block starting at address 0 */
743 error
= __mxt_read_reg(client
, MXT_INFO
, sizeof(*info
), info
);
750 static int mxt_get_object_table(struct mxt_data
*data
)
752 struct i2c_client
*client
= data
->client
;
758 table_size
= data
->info
.object_num
* sizeof(struct mxt_object
);
759 error
= __mxt_read_reg(client
, MXT_OBJECT_START
, table_size
,
764 /* Valid Report IDs start counting from 1 */
766 for (i
= 0; i
< data
->info
.object_num
; i
++) {
767 struct mxt_object
*object
= data
->object_table
+ i
;
770 le16_to_cpus(&object
->start_address
);
772 if (object
->num_report_ids
) {
774 reportid
+= object
->num_report_ids
*
775 (object
->instances
+ 1);
776 max_id
= reportid
- 1;
782 dev_dbg(&data
->client
->dev
,
783 "Type %2d Start %3d Size %3d Instances %2d ReportIDs %3u : %3u\n",
784 object
->type
, object
->start_address
, object
->size
+ 1,
785 object
->instances
+ 1, min_id
, max_id
);
787 switch (object
->type
) {
788 case MXT_GEN_COMMAND_T6
:
789 data
->T6_reportid
= min_id
;
791 case MXT_TOUCH_MULTI_T9
:
792 data
->T9_reportid_min
= min_id
;
793 data
->T9_reportid_max
= max_id
;
795 case MXT_SPT_GPIOPWM_T19
:
796 data
->T19_reportid
= min_id
;
804 static void mxt_free_object_table(struct mxt_data
*data
)
806 kfree(data
->object_table
);
807 data
->object_table
= NULL
;
808 data
->T6_reportid
= 0;
809 data
->T9_reportid_min
= 0;
810 data
->T9_reportid_max
= 0;
811 data
->T19_reportid
= 0;
814 static int mxt_initialize(struct mxt_data
*data
)
816 struct i2c_client
*client
= data
->client
;
817 struct mxt_info
*info
= &data
->info
;
821 error
= mxt_get_info(data
);
825 data
->object_table
= kcalloc(info
->object_num
,
826 sizeof(struct mxt_object
),
828 if (!data
->object_table
) {
829 dev_err(&client
->dev
, "Failed to allocate memory\n");
833 /* Get object table information */
834 error
= mxt_get_object_table(data
);
836 goto err_free_object_table
;
838 /* Check register init values */
839 error
= mxt_check_reg_init(data
);
841 goto err_free_object_table
;
843 mxt_handle_pdata(data
);
845 /* Backup to memory */
846 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
847 MXT_COMMAND_BACKUPNV
,
849 msleep(MXT_BACKUP_TIME
);
852 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
853 MXT_COMMAND_RESET
, 1);
854 msleep(MXT_RESET_TIME
);
856 /* Update matrix size at info struct */
857 error
= mxt_read_reg(client
, MXT_MATRIX_X_SIZE
, &val
);
859 goto err_free_object_table
;
860 info
->matrix_xsize
= val
;
862 error
= mxt_read_reg(client
, MXT_MATRIX_Y_SIZE
, &val
);
864 goto err_free_object_table
;
865 info
->matrix_ysize
= val
;
867 dev_info(&client
->dev
,
868 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
869 info
->family_id
, info
->variant_id
, info
->version
>> 4,
870 info
->version
& 0xf, info
->build
);
872 dev_info(&client
->dev
,
873 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
874 info
->matrix_xsize
, info
->matrix_ysize
,
879 err_free_object_table
:
880 mxt_free_object_table(data
);
884 static void mxt_calc_resolution(struct mxt_data
*data
)
886 unsigned int max_x
= data
->pdata
->x_size
- 1;
887 unsigned int max_y
= data
->pdata
->y_size
- 1;
889 if (data
->pdata
->orient
& MXT_XY_SWITCH
) {
898 /* Firmware Version is returned as Major.Minor.Build */
899 static ssize_t
mxt_fw_version_show(struct device
*dev
,
900 struct device_attribute
*attr
, char *buf
)
902 struct mxt_data
*data
= dev_get_drvdata(dev
);
903 struct mxt_info
*info
= &data
->info
;
904 return scnprintf(buf
, PAGE_SIZE
, "%u.%u.%02X\n",
905 info
->version
>> 4, info
->version
& 0xf, info
->build
);
908 /* Hardware Version is returned as FamilyID.VariantID */
909 static ssize_t
mxt_hw_version_show(struct device
*dev
,
910 struct device_attribute
*attr
, char *buf
)
912 struct mxt_data
*data
= dev_get_drvdata(dev
);
913 struct mxt_info
*info
= &data
->info
;
914 return scnprintf(buf
, PAGE_SIZE
, "%u.%u\n",
915 info
->family_id
, info
->variant_id
);
918 static ssize_t
mxt_show_instance(char *buf
, int count
,
919 struct mxt_object
*object
, int instance
,
924 if (object
->instances
> 0)
925 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
926 "Instance %u\n", instance
);
928 for (i
= 0; i
< object
->size
+ 1; i
++)
929 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
930 "\t[%2u]: %02x (%d)\n", i
, val
[i
], val
[i
]);
931 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
936 static ssize_t
mxt_object_show(struct device
*dev
,
937 struct device_attribute
*attr
, char *buf
)
939 struct mxt_data
*data
= dev_get_drvdata(dev
);
940 struct mxt_object
*object
;
946 /* Pre-allocate buffer large enough to hold max sized object. */
947 obuf
= kmalloc(256, GFP_KERNEL
);
952 for (i
= 0; i
< data
->info
.object_num
; i
++) {
953 object
= data
->object_table
+ i
;
955 if (!mxt_object_readable(object
->type
))
958 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
959 "T%u:\n", object
->type
);
961 for (j
= 0; j
< object
->instances
+ 1; j
++) {
962 u16 size
= object
->size
+ 1;
963 u16 addr
= object
->start_address
+ j
* size
;
965 error
= __mxt_read_reg(data
->client
, addr
, size
, obuf
);
969 count
= mxt_show_instance(buf
, count
, object
, j
, obuf
);
975 return error
?: count
;
978 static int mxt_load_fw(struct device
*dev
, const char *fn
)
980 struct mxt_data
*data
= dev_get_drvdata(dev
);
981 struct i2c_client
*client
= data
->client
;
982 const struct firmware
*fw
= NULL
;
983 unsigned int frame_size
;
984 unsigned int pos
= 0;
987 ret
= request_firmware(&fw
, fn
, dev
);
989 dev_err(dev
, "Unable to open firmware %s\n", fn
);
993 /* Change to the bootloader mode */
994 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
995 MXT_COMMAND_RESET
, MXT_BOOT_VALUE
);
996 msleep(MXT_RESET_TIME
);
998 /* Change to slave address of bootloader */
999 if (client
->addr
== MXT_APP_LOW
)
1000 client
->addr
= MXT_BOOT_LOW
;
1002 client
->addr
= MXT_BOOT_HIGH
;
1004 ret
= mxt_check_bootloader(client
, MXT_WAITING_BOOTLOAD_CMD
);
1008 /* Unlock bootloader */
1009 mxt_unlock_bootloader(client
);
1011 while (pos
< fw
->size
) {
1012 ret
= mxt_check_bootloader(client
,
1013 MXT_WAITING_FRAME_DATA
);
1017 frame_size
= ((*(fw
->data
+ pos
) << 8) | *(fw
->data
+ pos
+ 1));
1019 /* We should add 2 at frame size as the the firmware data is not
1020 * included the CRC bytes.
1024 /* Write one frame to device */
1025 mxt_fw_write(client
, fw
->data
+ pos
, frame_size
);
1027 ret
= mxt_check_bootloader(client
,
1028 MXT_FRAME_CRC_PASS
);
1034 dev_dbg(dev
, "Updated %d bytes / %zd bytes\n", pos
, fw
->size
);
1038 release_firmware(fw
);
1040 /* Change to slave address of application */
1041 if (client
->addr
== MXT_BOOT_LOW
)
1042 client
->addr
= MXT_APP_LOW
;
1044 client
->addr
= MXT_APP_HIGH
;
1049 static ssize_t
mxt_update_fw_store(struct device
*dev
,
1050 struct device_attribute
*attr
,
1051 const char *buf
, size_t count
)
1053 struct mxt_data
*data
= dev_get_drvdata(dev
);
1056 disable_irq(data
->irq
);
1058 error
= mxt_load_fw(dev
, MXT_FW_NAME
);
1060 dev_err(dev
, "The firmware update failed(%d)\n", error
);
1063 dev_dbg(dev
, "The firmware update succeeded\n");
1065 /* Wait for reset */
1066 msleep(MXT_FWRESET_TIME
);
1068 mxt_free_object_table(data
);
1070 mxt_initialize(data
);
1073 enable_irq(data
->irq
);
1075 error
= mxt_make_highchg(data
);
1082 static DEVICE_ATTR(fw_version
, S_IRUGO
, mxt_fw_version_show
, NULL
);
1083 static DEVICE_ATTR(hw_version
, S_IRUGO
, mxt_hw_version_show
, NULL
);
1084 static DEVICE_ATTR(object
, S_IRUGO
, mxt_object_show
, NULL
);
1085 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, mxt_update_fw_store
);
1087 static struct attribute
*mxt_attrs
[] = {
1088 &dev_attr_fw_version
.attr
,
1089 &dev_attr_hw_version
.attr
,
1090 &dev_attr_object
.attr
,
1091 &dev_attr_update_fw
.attr
,
1095 static const struct attribute_group mxt_attr_group
= {
1099 static void mxt_start(struct mxt_data
*data
)
1102 mxt_write_object(data
,
1103 MXT_TOUCH_MULTI_T9
, MXT_TOUCH_CTRL
, 0x83);
1106 static void mxt_stop(struct mxt_data
*data
)
1109 mxt_write_object(data
,
1110 MXT_TOUCH_MULTI_T9
, MXT_TOUCH_CTRL
, 0);
1113 static int mxt_input_open(struct input_dev
*dev
)
1115 struct mxt_data
*data
= input_get_drvdata(dev
);
1122 static void mxt_input_close(struct input_dev
*dev
)
1124 struct mxt_data
*data
= input_get_drvdata(dev
);
1129 static int mxt_probe(struct i2c_client
*client
,
1130 const struct i2c_device_id
*id
)
1132 const struct mxt_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
1133 struct mxt_data
*data
;
1134 struct input_dev
*input_dev
;
1136 unsigned int num_mt_slots
;
1141 data
= kzalloc(sizeof(struct mxt_data
), GFP_KERNEL
);
1142 input_dev
= input_allocate_device();
1143 if (!data
|| !input_dev
) {
1144 dev_err(&client
->dev
, "Failed to allocate memory\n");
1149 data
->is_tp
= pdata
&& pdata
->is_tp
;
1151 input_dev
->name
= (data
->is_tp
) ? "Atmel maXTouch Touchpad" :
1152 "Atmel maXTouch Touchscreen";
1153 snprintf(data
->phys
, sizeof(data
->phys
), "i2c-%u-%04x/input0",
1154 client
->adapter
->nr
, client
->addr
);
1156 input_dev
->phys
= data
->phys
;
1158 input_dev
->id
.bustype
= BUS_I2C
;
1159 input_dev
->dev
.parent
= &client
->dev
;
1160 input_dev
->open
= mxt_input_open
;
1161 input_dev
->close
= mxt_input_close
;
1163 data
->client
= client
;
1164 data
->input_dev
= input_dev
;
1165 data
->pdata
= pdata
;
1166 data
->irq
= client
->irq
;
1168 mxt_calc_resolution(data
);
1170 error
= mxt_initialize(data
);
1174 __set_bit(EV_ABS
, input_dev
->evbit
);
1175 __set_bit(EV_KEY
, input_dev
->evbit
);
1176 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
1180 __set_bit(INPUT_PROP_POINTER
, input_dev
->propbit
);
1181 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
1183 for (i
= 0; i
< MXT_NUM_GPIO
; i
++)
1184 if (pdata
->key_map
[i
] != KEY_RESERVED
)
1185 __set_bit(pdata
->key_map
[i
], input_dev
->keybit
);
1187 __set_bit(BTN_TOOL_FINGER
, input_dev
->keybit
);
1188 __set_bit(BTN_TOOL_DOUBLETAP
, input_dev
->keybit
);
1189 __set_bit(BTN_TOOL_TRIPLETAP
, input_dev
->keybit
);
1190 __set_bit(BTN_TOOL_QUADTAP
, input_dev
->keybit
);
1191 __set_bit(BTN_TOOL_QUINTTAP
, input_dev
->keybit
);
1193 input_abs_set_res(input_dev
, ABS_X
, MXT_PIXELS_PER_MM
);
1194 input_abs_set_res(input_dev
, ABS_Y
, MXT_PIXELS_PER_MM
);
1195 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
,
1197 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
,
1201 /* For single touch */
1202 input_set_abs_params(input_dev
, ABS_X
,
1203 0, data
->max_x
, 0, 0);
1204 input_set_abs_params(input_dev
, ABS_Y
,
1205 0, data
->max_y
, 0, 0);
1206 input_set_abs_params(input_dev
, ABS_PRESSURE
,
1209 /* For multi touch */
1210 num_mt_slots
= data
->T9_reportid_max
- data
->T9_reportid_min
+ 1;
1211 error
= input_mt_init_slots(input_dev
, num_mt_slots
, 0);
1213 goto err_free_object
;
1214 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
1215 0, MXT_MAX_AREA
, 0, 0);
1216 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
1217 0, data
->max_x
, 0, 0);
1218 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
1219 0, data
->max_y
, 0, 0);
1220 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
1223 input_set_drvdata(input_dev
, data
);
1224 i2c_set_clientdata(client
, data
);
1226 error
= request_threaded_irq(client
->irq
, NULL
, mxt_interrupt
,
1227 pdata
->irqflags
| IRQF_ONESHOT
,
1228 client
->name
, data
);
1230 dev_err(&client
->dev
, "Failed to register interrupt\n");
1231 goto err_free_object
;
1234 error
= mxt_make_highchg(data
);
1238 error
= input_register_device(input_dev
);
1242 error
= sysfs_create_group(&client
->dev
.kobj
, &mxt_attr_group
);
1244 goto err_unregister_device
;
1248 err_unregister_device
:
1249 input_unregister_device(input_dev
);
1252 free_irq(client
->irq
, data
);
1254 kfree(data
->object_table
);
1256 input_free_device(input_dev
);
1261 static int mxt_remove(struct i2c_client
*client
)
1263 struct mxt_data
*data
= i2c_get_clientdata(client
);
1265 sysfs_remove_group(&client
->dev
.kobj
, &mxt_attr_group
);
1266 free_irq(data
->irq
, data
);
1267 input_unregister_device(data
->input_dev
);
1268 kfree(data
->object_table
);
1274 #ifdef CONFIG_PM_SLEEP
1275 static int mxt_suspend(struct device
*dev
)
1277 struct i2c_client
*client
= to_i2c_client(dev
);
1278 struct mxt_data
*data
= i2c_get_clientdata(client
);
1279 struct input_dev
*input_dev
= data
->input_dev
;
1281 mutex_lock(&input_dev
->mutex
);
1283 if (input_dev
->users
)
1286 mutex_unlock(&input_dev
->mutex
);
1291 static int mxt_resume(struct device
*dev
)
1293 struct i2c_client
*client
= to_i2c_client(dev
);
1294 struct mxt_data
*data
= i2c_get_clientdata(client
);
1295 struct input_dev
*input_dev
= data
->input_dev
;
1298 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
1299 MXT_COMMAND_RESET
, 1);
1301 msleep(MXT_RESET_TIME
);
1303 mutex_lock(&input_dev
->mutex
);
1305 if (input_dev
->users
)
1308 mutex_unlock(&input_dev
->mutex
);
1314 static SIMPLE_DEV_PM_OPS(mxt_pm_ops
, mxt_suspend
, mxt_resume
);
1316 static const struct i2c_device_id mxt_id
[] = {
1317 { "qt602240_ts", 0 },
1318 { "atmel_mxt_ts", 0 },
1319 { "atmel_mxt_tp", 0 },
1323 MODULE_DEVICE_TABLE(i2c
, mxt_id
);
1325 static struct i2c_driver mxt_driver
= {
1327 .name
= "atmel_mxt_ts",
1328 .owner
= THIS_MODULE
,
1332 .remove
= mxt_remove
,
1336 module_i2c_driver(mxt_driver
);
1338 /* Module information */
1339 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
1340 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
1341 MODULE_LICENSE("GPL");