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/init.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/i2c.h>
19 #include <linux/i2c/atmel_mxt_ts.h>
20 #include <linux/input/mt.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
30 #define MXT_APP_LOW 0x4a
31 #define MXT_APP_HIGH 0x4b
32 #define MXT_BOOT_LOW 0x24
33 #define MXT_BOOT_HIGH 0x25
36 #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 25 /* msec */
179 #define MXT_RESET_TIME 65 /* msec */
181 #define MXT_FWRESET_TIME 175 /* msec */
183 /* Command to unlock bootloader */
184 #define MXT_UNLOCK_CMD_MSB 0xaa
185 #define MXT_UNLOCK_CMD_LSB 0xdc
187 /* Bootloader mode status */
188 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
189 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
190 #define MXT_FRAME_CRC_CHECK 0x02
191 #define MXT_FRAME_CRC_FAIL 0x03
192 #define MXT_FRAME_CRC_PASS 0x04
193 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
194 #define MXT_BOOT_STATUS_MASK 0x3f
197 #define MXT_SUPPRESS (1 << 1)
198 #define MXT_AMP (1 << 2)
199 #define MXT_VECTOR (1 << 3)
200 #define MXT_MOVE (1 << 4)
201 #define MXT_RELEASE (1 << 5)
202 #define MXT_PRESS (1 << 6)
203 #define MXT_DETECT (1 << 7)
205 /* Touch orient bits */
206 #define MXT_XY_SWITCH (1 << 0)
207 #define MXT_X_INVERT (1 << 1)
208 #define MXT_Y_INVERT (1 << 2)
210 /* Touchscreen absolute values */
211 #define MXT_MAX_AREA 0xff
213 #define MXT_MAX_FINGER 10
232 /* to map object and message */
250 /* Each client has this additional data */
252 struct i2c_client
*client
;
253 struct input_dev
*input_dev
;
254 const struct mxt_platform_data
*pdata
;
255 struct mxt_object
*object_table
;
256 struct mxt_info info
;
257 struct mxt_finger finger
[MXT_MAX_FINGER
];
263 static bool mxt_object_readable(unsigned int type
)
266 case MXT_GEN_MESSAGE_T5
:
267 case MXT_GEN_COMMAND_T6
:
268 case MXT_GEN_POWER_T7
:
269 case MXT_GEN_ACQUIRE_T8
:
270 case MXT_GEN_DATASOURCE_T53
:
271 case MXT_TOUCH_MULTI_T9
:
272 case MXT_TOUCH_KEYARRAY_T15
:
273 case MXT_TOUCH_PROXIMITY_T23
:
274 case MXT_TOUCH_PROXKEY_T52
:
275 case MXT_PROCI_GRIPFACE_T20
:
276 case MXT_PROCG_NOISE_T22
:
277 case MXT_PROCI_ONETOUCH_T24
:
278 case MXT_PROCI_TWOTOUCH_T27
:
279 case MXT_PROCI_GRIP_T40
:
280 case MXT_PROCI_PALM_T41
:
281 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
282 case MXT_PROCI_STYLUS_T47
:
283 case MXT_PROCG_NOISESUPPRESSION_T48
:
284 case MXT_SPT_COMMSCONFIG_T18
:
285 case MXT_SPT_GPIOPWM_T19
:
286 case MXT_SPT_SELFTEST_T25
:
287 case MXT_SPT_CTECONFIG_T28
:
288 case MXT_SPT_USERDATA_T38
:
289 case MXT_SPT_DIGITIZER_T43
:
290 case MXT_SPT_CTECONFIG_T46
:
297 static bool mxt_object_writable(unsigned int type
)
300 case MXT_GEN_COMMAND_T6
:
301 case MXT_GEN_POWER_T7
:
302 case MXT_GEN_ACQUIRE_T8
:
303 case MXT_TOUCH_MULTI_T9
:
304 case MXT_TOUCH_KEYARRAY_T15
:
305 case MXT_TOUCH_PROXIMITY_T23
:
306 case MXT_TOUCH_PROXKEY_T52
:
307 case MXT_PROCI_GRIPFACE_T20
:
308 case MXT_PROCG_NOISE_T22
:
309 case MXT_PROCI_ONETOUCH_T24
:
310 case MXT_PROCI_TWOTOUCH_T27
:
311 case MXT_PROCI_GRIP_T40
:
312 case MXT_PROCI_PALM_T41
:
313 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
314 case MXT_PROCI_STYLUS_T47
:
315 case MXT_PROCG_NOISESUPPRESSION_T48
:
316 case MXT_SPT_COMMSCONFIG_T18
:
317 case MXT_SPT_GPIOPWM_T19
:
318 case MXT_SPT_SELFTEST_T25
:
319 case MXT_SPT_CTECONFIG_T28
:
320 case MXT_SPT_DIGITIZER_T43
:
321 case MXT_SPT_CTECONFIG_T46
:
328 static void mxt_dump_message(struct device
*dev
,
329 struct mxt_message
*message
)
331 dev_dbg(dev
, "reportid:\t0x%x\n", message
->reportid
);
332 dev_dbg(dev
, "message1:\t0x%x\n", message
->message
[0]);
333 dev_dbg(dev
, "message2:\t0x%x\n", message
->message
[1]);
334 dev_dbg(dev
, "message3:\t0x%x\n", message
->message
[2]);
335 dev_dbg(dev
, "message4:\t0x%x\n", message
->message
[3]);
336 dev_dbg(dev
, "message5:\t0x%x\n", message
->message
[4]);
337 dev_dbg(dev
, "message6:\t0x%x\n", message
->message
[5]);
338 dev_dbg(dev
, "message7:\t0x%x\n", message
->message
[6]);
339 dev_dbg(dev
, "checksum:\t0x%x\n", message
->checksum
);
342 static int mxt_check_bootloader(struct i2c_client
*client
,
348 if (i2c_master_recv(client
, &val
, 1) != 1) {
349 dev_err(&client
->dev
, "%s: i2c recv failed\n", __func__
);
354 case MXT_WAITING_BOOTLOAD_CMD
:
355 case MXT_WAITING_FRAME_DATA
:
356 val
&= ~MXT_BOOT_STATUS_MASK
;
358 case MXT_FRAME_CRC_PASS
:
359 if (val
== MXT_FRAME_CRC_CHECK
)
367 dev_err(&client
->dev
, "Unvalid bootloader mode state\n");
374 static int mxt_unlock_bootloader(struct i2c_client
*client
)
378 buf
[0] = MXT_UNLOCK_CMD_LSB
;
379 buf
[1] = MXT_UNLOCK_CMD_MSB
;
381 if (i2c_master_send(client
, buf
, 2) != 2) {
382 dev_err(&client
->dev
, "%s: i2c send failed\n", __func__
);
389 static int mxt_fw_write(struct i2c_client
*client
,
390 const u8
*data
, unsigned int frame_size
)
392 if (i2c_master_send(client
, data
, frame_size
) != frame_size
) {
393 dev_err(&client
->dev
, "%s: i2c send failed\n", __func__
);
400 static int __mxt_read_reg(struct i2c_client
*client
,
401 u16 reg
, u16 len
, void *val
)
403 struct i2c_msg xfer
[2];
407 buf
[1] = (reg
>> 8) & 0xff;
410 xfer
[0].addr
= client
->addr
;
416 xfer
[1].addr
= client
->addr
;
417 xfer
[1].flags
= I2C_M_RD
;
421 if (i2c_transfer(client
->adapter
, xfer
, 2) != 2) {
422 dev_err(&client
->dev
, "%s: i2c transfer failed\n", __func__
);
429 static int mxt_read_reg(struct i2c_client
*client
, u16 reg
, u8
*val
)
431 return __mxt_read_reg(client
, reg
, 1, val
);
434 static int mxt_write_reg(struct i2c_client
*client
, u16 reg
, u8 val
)
439 buf
[1] = (reg
>> 8) & 0xff;
442 if (i2c_master_send(client
, buf
, 3) != 3) {
443 dev_err(&client
->dev
, "%s: i2c send failed\n", __func__
);
450 static int mxt_read_object_table(struct i2c_client
*client
,
451 u16 reg
, u8
*object_buf
)
453 return __mxt_read_reg(client
, reg
, MXT_OBJECT_SIZE
,
457 static struct mxt_object
*
458 mxt_get_object(struct mxt_data
*data
, u8 type
)
460 struct mxt_object
*object
;
463 for (i
= 0; i
< data
->info
.object_num
; i
++) {
464 object
= data
->object_table
+ i
;
465 if (object
->type
== type
)
469 dev_err(&data
->client
->dev
, "Invalid object type\n");
473 static int mxt_read_message(struct mxt_data
*data
,
474 struct mxt_message
*message
)
476 struct mxt_object
*object
;
479 object
= mxt_get_object(data
, MXT_GEN_MESSAGE_T5
);
483 reg
= object
->start_address
;
484 return __mxt_read_reg(data
->client
, reg
,
485 sizeof(struct mxt_message
), message
);
488 static int mxt_read_object(struct mxt_data
*data
,
489 u8 type
, u8 offset
, u8
*val
)
491 struct mxt_object
*object
;
494 object
= mxt_get_object(data
, type
);
498 reg
= object
->start_address
;
499 return __mxt_read_reg(data
->client
, reg
+ offset
, 1, val
);
502 static int mxt_write_object(struct mxt_data
*data
,
503 u8 type
, u8 offset
, u8 val
)
505 struct mxt_object
*object
;
508 object
= mxt_get_object(data
, type
);
512 reg
= object
->start_address
;
513 return mxt_write_reg(data
->client
, reg
+ offset
, val
);
516 static void mxt_input_report(struct mxt_data
*data
, int single_id
)
518 struct mxt_finger
*finger
= data
->finger
;
519 struct input_dev
*input_dev
= data
->input_dev
;
520 int status
= finger
[single_id
].status
;
524 for (id
= 0; id
< MXT_MAX_FINGER
; id
++) {
525 if (!finger
[id
].status
)
528 input_mt_slot(input_dev
, id
);
529 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
,
530 finger
[id
].status
!= MXT_RELEASE
);
532 if (finger
[id
].status
!= MXT_RELEASE
) {
534 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
,
536 input_report_abs(input_dev
, ABS_MT_POSITION_X
,
538 input_report_abs(input_dev
, ABS_MT_POSITION_Y
,
540 input_report_abs(input_dev
, ABS_MT_PRESSURE
,
541 finger
[id
].pressure
);
543 finger
[id
].status
= 0;
547 input_report_key(input_dev
, BTN_TOUCH
, finger_num
> 0);
549 if (status
!= MXT_RELEASE
) {
550 input_report_abs(input_dev
, ABS_X
, finger
[single_id
].x
);
551 input_report_abs(input_dev
, ABS_Y
, finger
[single_id
].y
);
552 input_report_abs(input_dev
,
553 ABS_PRESSURE
, finger
[single_id
].pressure
);
556 input_sync(input_dev
);
559 static void mxt_input_touchevent(struct mxt_data
*data
,
560 struct mxt_message
*message
, int id
)
562 struct mxt_finger
*finger
= data
->finger
;
563 struct device
*dev
= &data
->client
->dev
;
564 u8 status
= message
->message
[0];
570 /* Check the touch is present on the screen */
571 if (!(status
& MXT_DETECT
)) {
572 if (status
& MXT_RELEASE
) {
573 dev_dbg(dev
, "[%d] released\n", id
);
575 finger
[id
].status
= MXT_RELEASE
;
576 mxt_input_report(data
, id
);
581 /* Check only AMP detection */
582 if (!(status
& (MXT_PRESS
| MXT_MOVE
)))
585 x
= (message
->message
[1] << 4) | ((message
->message
[3] >> 4) & 0xf);
586 y
= (message
->message
[2] << 4) | ((message
->message
[3] & 0xf));
587 if (data
->max_x
< 1024)
589 if (data
->max_y
< 1024)
592 area
= message
->message
[4];
593 pressure
= message
->message
[5];
595 dev_dbg(dev
, "[%d] %s x: %d, y: %d, area: %d\n", id
,
596 status
& MXT_MOVE
? "moved" : "pressed",
599 finger
[id
].status
= status
& MXT_MOVE
?
600 MXT_MOVE
: MXT_PRESS
;
603 finger
[id
].area
= area
;
604 finger
[id
].pressure
= pressure
;
606 mxt_input_report(data
, id
);
609 static irqreturn_t
mxt_interrupt(int irq
, void *dev_id
)
611 struct mxt_data
*data
= dev_id
;
612 struct mxt_message message
;
613 struct mxt_object
*object
;
614 struct device
*dev
= &data
->client
->dev
;
621 if (mxt_read_message(data
, &message
)) {
622 dev_err(dev
, "Failed to read message\n");
626 reportid
= message
.reportid
;
628 /* whether reportid is thing of MXT_TOUCH_MULTI_T9 */
629 object
= mxt_get_object(data
, MXT_TOUCH_MULTI_T9
);
633 max_reportid
= object
->max_reportid
;
634 min_reportid
= max_reportid
- object
->num_report_ids
+ 1;
635 id
= reportid
- min_reportid
;
637 if (reportid
>= min_reportid
&& reportid
<= max_reportid
)
638 mxt_input_touchevent(data
, &message
, id
);
640 mxt_dump_message(dev
, &message
);
641 } while (reportid
!= 0xff);
647 static int mxt_check_reg_init(struct mxt_data
*data
)
649 const struct mxt_platform_data
*pdata
= data
->pdata
;
650 struct mxt_object
*object
;
651 struct device
*dev
= &data
->client
->dev
;
653 int i
, j
, config_offset
;
655 if (!pdata
->config
) {
656 dev_dbg(dev
, "No cfg data defined, skipping reg init\n");
660 for (i
= 0; i
< data
->info
.object_num
; i
++) {
661 object
= data
->object_table
+ i
;
663 if (!mxt_object_writable(object
->type
))
667 j
< (object
->size
+ 1) * (object
->instances
+ 1);
669 config_offset
= index
+ j
;
670 if (config_offset
> pdata
->config_length
) {
671 dev_err(dev
, "Not enough config data!\n");
674 mxt_write_object(data
, object
->type
, j
,
675 pdata
->config
[config_offset
]);
677 index
+= (object
->size
+ 1) * (object
->instances
+ 1);
683 static int mxt_make_highchg(struct mxt_data
*data
)
685 struct device
*dev
= &data
->client
->dev
;
686 struct mxt_message message
;
690 /* Read dummy message to make high CHG pin */
692 error
= mxt_read_message(data
, &message
);
695 } while (message
.reportid
!= 0xff && --count
);
698 dev_err(dev
, "CHG pin isn't cleared\n");
705 static void mxt_handle_pdata(struct mxt_data
*data
)
707 const struct mxt_platform_data
*pdata
= data
->pdata
;
710 /* Set touchscreen lines */
711 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
, MXT_TOUCH_XSIZE
,
713 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
, MXT_TOUCH_YSIZE
,
716 /* Set touchscreen orient */
717 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
, MXT_TOUCH_ORIENT
,
720 /* Set touchscreen burst length */
721 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
722 MXT_TOUCH_BLEN
, pdata
->blen
);
724 /* Set touchscreen threshold */
725 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
726 MXT_TOUCH_TCHTHR
, pdata
->threshold
);
728 /* Set touchscreen resolution */
729 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
730 MXT_TOUCH_XRANGE_LSB
, (pdata
->x_size
- 1) & 0xff);
731 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
732 MXT_TOUCH_XRANGE_MSB
, (pdata
->x_size
- 1) >> 8);
733 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
734 MXT_TOUCH_YRANGE_LSB
, (pdata
->y_size
- 1) & 0xff);
735 mxt_write_object(data
, MXT_TOUCH_MULTI_T9
,
736 MXT_TOUCH_YRANGE_MSB
, (pdata
->y_size
- 1) >> 8);
738 /* Set touchscreen voltage */
739 if (pdata
->voltage
) {
740 if (pdata
->voltage
< MXT_VOLTAGE_DEFAULT
) {
741 voltage
= (MXT_VOLTAGE_DEFAULT
- pdata
->voltage
) /
743 voltage
= 0xff - voltage
+ 1;
745 voltage
= (pdata
->voltage
- MXT_VOLTAGE_DEFAULT
) /
748 mxt_write_object(data
, MXT_SPT_CTECONFIG_T28
,
749 MXT_CTE_VOLTAGE
, voltage
);
753 static int mxt_get_info(struct mxt_data
*data
)
755 struct i2c_client
*client
= data
->client
;
756 struct mxt_info
*info
= &data
->info
;
760 error
= mxt_read_reg(client
, MXT_FAMILY_ID
, &val
);
763 info
->family_id
= val
;
765 error
= mxt_read_reg(client
, MXT_VARIANT_ID
, &val
);
768 info
->variant_id
= val
;
770 error
= mxt_read_reg(client
, MXT_VERSION
, &val
);
775 error
= mxt_read_reg(client
, MXT_BUILD
, &val
);
780 error
= mxt_read_reg(client
, MXT_OBJECT_NUM
, &val
);
783 info
->object_num
= val
;
788 static int mxt_get_object_table(struct mxt_data
*data
)
794 u8 buf
[MXT_OBJECT_SIZE
];
796 for (i
= 0; i
< data
->info
.object_num
; i
++) {
797 struct mxt_object
*object
= data
->object_table
+ i
;
799 reg
= MXT_OBJECT_START
+ MXT_OBJECT_SIZE
* i
;
800 error
= mxt_read_object_table(data
->client
, reg
, buf
);
804 object
->type
= buf
[0];
805 object
->start_address
= (buf
[2] << 8) | buf
[1];
806 object
->size
= buf
[3];
807 object
->instances
= buf
[4];
808 object
->num_report_ids
= buf
[5];
810 if (object
->num_report_ids
) {
811 reportid
+= object
->num_report_ids
*
812 (object
->instances
+ 1);
813 object
->max_reportid
= reportid
;
820 static int mxt_initialize(struct mxt_data
*data
)
822 struct i2c_client
*client
= data
->client
;
823 struct mxt_info
*info
= &data
->info
;
827 error
= mxt_get_info(data
);
831 data
->object_table
= kcalloc(info
->object_num
,
832 sizeof(struct mxt_object
),
834 if (!data
->object_table
) {
835 dev_err(&client
->dev
, "Failed to allocate memory\n");
839 /* Get object table information */
840 error
= mxt_get_object_table(data
);
844 /* Check register init values */
845 error
= mxt_check_reg_init(data
);
849 mxt_handle_pdata(data
);
851 /* Backup to memory */
852 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
853 MXT_COMMAND_BACKUPNV
,
855 msleep(MXT_BACKUP_TIME
);
858 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
859 MXT_COMMAND_RESET
, 1);
860 msleep(MXT_RESET_TIME
);
862 /* Update matrix size at info struct */
863 error
= mxt_read_reg(client
, MXT_MATRIX_X_SIZE
, &val
);
866 info
->matrix_xsize
= val
;
868 error
= mxt_read_reg(client
, MXT_MATRIX_Y_SIZE
, &val
);
871 info
->matrix_ysize
= val
;
873 dev_info(&client
->dev
,
874 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
875 info
->family_id
, info
->variant_id
, info
->version
,
878 dev_info(&client
->dev
,
879 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
880 info
->matrix_xsize
, info
->matrix_ysize
,
886 static void mxt_calc_resolution(struct mxt_data
*data
)
888 unsigned int max_x
= data
->pdata
->x_size
- 1;
889 unsigned int max_y
= data
->pdata
->y_size
- 1;
891 if (data
->pdata
->orient
& MXT_XY_SWITCH
) {
900 static ssize_t
mxt_object_show(struct device
*dev
,
901 struct device_attribute
*attr
, char *buf
)
903 struct mxt_data
*data
= dev_get_drvdata(dev
);
904 struct mxt_object
*object
;
910 for (i
= 0; i
< data
->info
.object_num
; i
++) {
911 object
= data
->object_table
+ i
;
913 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
,
914 "Object[%d] (Type %d)\n",
915 i
+ 1, object
->type
);
916 if (count
>= PAGE_SIZE
)
917 return PAGE_SIZE
- 1;
919 if (!mxt_object_readable(object
->type
)) {
920 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
,
922 if (count
>= PAGE_SIZE
)
923 return PAGE_SIZE
- 1;
927 for (j
= 0; j
< object
->size
+ 1; j
++) {
928 error
= mxt_read_object(data
,
929 object
->type
, j
, &val
);
933 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
,
934 "\t[%2d]: %02x (%d)\n", j
, val
, val
);
935 if (count
>= PAGE_SIZE
)
936 return PAGE_SIZE
- 1;
939 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
940 if (count
>= PAGE_SIZE
)
941 return PAGE_SIZE
- 1;
947 static int mxt_load_fw(struct device
*dev
, const char *fn
)
949 struct mxt_data
*data
= dev_get_drvdata(dev
);
950 struct i2c_client
*client
= data
->client
;
951 const struct firmware
*fw
= NULL
;
952 unsigned int frame_size
;
953 unsigned int pos
= 0;
956 ret
= request_firmware(&fw
, fn
, dev
);
958 dev_err(dev
, "Unable to open firmware %s\n", fn
);
962 /* Change to the bootloader mode */
963 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
964 MXT_COMMAND_RESET
, MXT_BOOT_VALUE
);
965 msleep(MXT_RESET_TIME
);
967 /* Change to slave address of bootloader */
968 if (client
->addr
== MXT_APP_LOW
)
969 client
->addr
= MXT_BOOT_LOW
;
971 client
->addr
= MXT_BOOT_HIGH
;
973 ret
= mxt_check_bootloader(client
, MXT_WAITING_BOOTLOAD_CMD
);
977 /* Unlock bootloader */
978 mxt_unlock_bootloader(client
);
980 while (pos
< fw
->size
) {
981 ret
= mxt_check_bootloader(client
,
982 MXT_WAITING_FRAME_DATA
);
986 frame_size
= ((*(fw
->data
+ pos
) << 8) | *(fw
->data
+ pos
+ 1));
988 /* We should add 2 at frame size as the the firmware data is not
989 * included the CRC bytes.
993 /* Write one frame to device */
994 mxt_fw_write(client
, fw
->data
+ pos
, frame_size
);
996 ret
= mxt_check_bootloader(client
,
1003 dev_dbg(dev
, "Updated %d bytes / %zd bytes\n", pos
, fw
->size
);
1007 release_firmware(fw
);
1009 /* Change to slave address of application */
1010 if (client
->addr
== MXT_BOOT_LOW
)
1011 client
->addr
= MXT_APP_LOW
;
1013 client
->addr
= MXT_APP_HIGH
;
1018 static ssize_t
mxt_update_fw_store(struct device
*dev
,
1019 struct device_attribute
*attr
,
1020 const char *buf
, size_t count
)
1022 struct mxt_data
*data
= dev_get_drvdata(dev
);
1025 disable_irq(data
->irq
);
1027 error
= mxt_load_fw(dev
, MXT_FW_NAME
);
1029 dev_err(dev
, "The firmware update failed(%d)\n", error
);
1032 dev_dbg(dev
, "The firmware update succeeded\n");
1034 /* Wait for reset */
1035 msleep(MXT_FWRESET_TIME
);
1037 kfree(data
->object_table
);
1038 data
->object_table
= NULL
;
1040 mxt_initialize(data
);
1043 enable_irq(data
->irq
);
1045 error
= mxt_make_highchg(data
);
1052 static DEVICE_ATTR(object
, 0444, mxt_object_show
, NULL
);
1053 static DEVICE_ATTR(update_fw
, 0664, NULL
, mxt_update_fw_store
);
1055 static struct attribute
*mxt_attrs
[] = {
1056 &dev_attr_object
.attr
,
1057 &dev_attr_update_fw
.attr
,
1061 static const struct attribute_group mxt_attr_group
= {
1065 static void mxt_start(struct mxt_data
*data
)
1068 mxt_write_object(data
,
1069 MXT_TOUCH_MULTI_T9
, MXT_TOUCH_CTRL
, 0x83);
1072 static void mxt_stop(struct mxt_data
*data
)
1075 mxt_write_object(data
,
1076 MXT_TOUCH_MULTI_T9
, MXT_TOUCH_CTRL
, 0);
1079 static int mxt_input_open(struct input_dev
*dev
)
1081 struct mxt_data
*data
= input_get_drvdata(dev
);
1088 static void mxt_input_close(struct input_dev
*dev
)
1090 struct mxt_data
*data
= input_get_drvdata(dev
);
1095 static int __devinit
mxt_probe(struct i2c_client
*client
,
1096 const struct i2c_device_id
*id
)
1098 const struct mxt_platform_data
*pdata
= client
->dev
.platform_data
;
1099 struct mxt_data
*data
;
1100 struct input_dev
*input_dev
;
1106 data
= kzalloc(sizeof(struct mxt_data
), GFP_KERNEL
);
1107 input_dev
= input_allocate_device();
1108 if (!data
|| !input_dev
) {
1109 dev_err(&client
->dev
, "Failed to allocate memory\n");
1114 input_dev
->name
= "Atmel maXTouch Touchscreen";
1115 input_dev
->id
.bustype
= BUS_I2C
;
1116 input_dev
->dev
.parent
= &client
->dev
;
1117 input_dev
->open
= mxt_input_open
;
1118 input_dev
->close
= mxt_input_close
;
1120 data
->client
= client
;
1121 data
->input_dev
= input_dev
;
1122 data
->pdata
= pdata
;
1123 data
->irq
= client
->irq
;
1125 mxt_calc_resolution(data
);
1127 __set_bit(EV_ABS
, input_dev
->evbit
);
1128 __set_bit(EV_KEY
, input_dev
->evbit
);
1129 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
1131 /* For single touch */
1132 input_set_abs_params(input_dev
, ABS_X
,
1133 0, data
->max_x
, 0, 0);
1134 input_set_abs_params(input_dev
, ABS_Y
,
1135 0, data
->max_y
, 0, 0);
1136 input_set_abs_params(input_dev
, ABS_PRESSURE
,
1139 /* For multi touch */
1140 input_mt_init_slots(input_dev
, MXT_MAX_FINGER
);
1141 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
1142 0, MXT_MAX_AREA
, 0, 0);
1143 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
1144 0, data
->max_x
, 0, 0);
1145 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
1146 0, data
->max_y
, 0, 0);
1147 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
1150 input_set_drvdata(input_dev
, data
);
1151 i2c_set_clientdata(client
, data
);
1153 error
= mxt_initialize(data
);
1155 goto err_free_object
;
1157 error
= request_threaded_irq(client
->irq
, NULL
, mxt_interrupt
,
1158 pdata
->irqflags
, client
->dev
.driver
->name
, data
);
1160 dev_err(&client
->dev
, "Failed to register interrupt\n");
1161 goto err_free_object
;
1164 error
= mxt_make_highchg(data
);
1168 error
= input_register_device(input_dev
);
1172 error
= sysfs_create_group(&client
->dev
.kobj
, &mxt_attr_group
);
1174 goto err_unregister_device
;
1178 err_unregister_device
:
1179 input_unregister_device(input_dev
);
1182 free_irq(client
->irq
, data
);
1184 kfree(data
->object_table
);
1186 input_free_device(input_dev
);
1191 static int __devexit
mxt_remove(struct i2c_client
*client
)
1193 struct mxt_data
*data
= i2c_get_clientdata(client
);
1195 sysfs_remove_group(&client
->dev
.kobj
, &mxt_attr_group
);
1196 free_irq(data
->irq
, data
);
1197 input_unregister_device(data
->input_dev
);
1198 kfree(data
->object_table
);
1205 static int mxt_suspend(struct device
*dev
)
1207 struct i2c_client
*client
= to_i2c_client(dev
);
1208 struct mxt_data
*data
= i2c_get_clientdata(client
);
1209 struct input_dev
*input_dev
= data
->input_dev
;
1211 mutex_lock(&input_dev
->mutex
);
1213 if (input_dev
->users
)
1216 mutex_unlock(&input_dev
->mutex
);
1221 static int mxt_resume(struct device
*dev
)
1223 struct i2c_client
*client
= to_i2c_client(dev
);
1224 struct mxt_data
*data
= i2c_get_clientdata(client
);
1225 struct input_dev
*input_dev
= data
->input_dev
;
1228 mxt_write_object(data
, MXT_GEN_COMMAND_T6
,
1229 MXT_COMMAND_RESET
, 1);
1231 msleep(MXT_RESET_TIME
);
1233 mutex_lock(&input_dev
->mutex
);
1235 if (input_dev
->users
)
1238 mutex_unlock(&input_dev
->mutex
);
1243 static const struct dev_pm_ops mxt_pm_ops
= {
1244 .suspend
= mxt_suspend
,
1245 .resume
= mxt_resume
,
1249 static const struct i2c_device_id mxt_id
[] = {
1250 { "qt602240_ts", 0 },
1251 { "atmel_mxt_ts", 0 },
1255 MODULE_DEVICE_TABLE(i2c
, mxt_id
);
1257 static struct i2c_driver mxt_driver
= {
1259 .name
= "atmel_mxt_ts",
1260 .owner
= THIS_MODULE
,
1266 .remove
= __devexit_p(mxt_remove
),
1270 static int __init
mxt_init(void)
1272 return i2c_add_driver(&mxt_driver
);
1275 static void __exit
mxt_exit(void)
1277 i2c_del_driver(&mxt_driver
);
1280 module_init(mxt_init
);
1281 module_exit(mxt_exit
);
1283 /* Module information */
1284 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
1285 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
1286 MODULE_LICENSE("GPL");