blob: a9e0b541c638253183e6b3eea3ec8f5772d97400 [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
6 *
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.
11 *
12 */
13
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>
Dmitry Torokhov964de522011-02-02 23:21:58 -080019#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070020#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070021#include <linux/interrupt.h>
22#include <linux/slab.h>
23
24/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080025#define MXT_VER_20 20
26#define MXT_VER_21 21
27#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070028
29/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080030#define MXT_APP_LOW 0x4a
31#define MXT_APP_HIGH 0x4b
32#define MXT_BOOT_LOW 0x24
33#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070034
35/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080036#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070037
38/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080039#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080040#define MXT_FAMILY_ID 0x00
41#define MXT_VARIANT_ID 0x01
42#define MXT_VERSION 0x02
43#define MXT_BUILD 0x03
44#define MXT_MATRIX_X_SIZE 0x04
45#define MXT_MATRIX_Y_SIZE 0x05
46#define MXT_OBJECT_NUM 0x06
47#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070048
Iiro Valkonen7686b102011-02-02 23:21:58 -080049#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070050
51/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070052#define MXT_DEBUG_DIAGNOSTIC_T37 37
53#define MXT_GEN_MESSAGE_T5 5
54#define MXT_GEN_COMMAND_T6 6
55#define MXT_GEN_POWER_T7 7
56#define MXT_GEN_ACQUIRE_T8 8
57#define MXT_GEN_DATASOURCE_T53 53
58#define MXT_TOUCH_MULTI_T9 9
59#define MXT_TOUCH_KEYARRAY_T15 15
60#define MXT_TOUCH_PROXIMITY_T23 23
61#define MXT_TOUCH_PROXKEY_T52 52
62#define MXT_PROCI_GRIPFACE_T20 20
63#define MXT_PROCG_NOISE_T22 22
64#define MXT_PROCI_ONETOUCH_T24 24
65#define MXT_PROCI_TWOTOUCH_T27 27
66#define MXT_PROCI_GRIP_T40 40
67#define MXT_PROCI_PALM_T41 41
68#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
69#define MXT_PROCI_STYLUS_T47 47
70#define MXT_PROCG_NOISESUPPRESSION_T48 48
71#define MXT_SPT_COMMSCONFIG_T18 18
72#define MXT_SPT_GPIOPWM_T19 19
73#define MXT_SPT_SELFTEST_T25 25
74#define MXT_SPT_CTECONFIG_T28 28
75#define MXT_SPT_USERDATA_T38 38
76#define MXT_SPT_DIGITIZER_T43 43
77#define MXT_SPT_MESSAGECOUNT_T44 44
78#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070079
Iiro Valkonen81c88a72011-07-04 03:08:25 -070080/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080081#define MXT_COMMAND_RESET 0
82#define MXT_COMMAND_BACKUPNV 1
83#define MXT_COMMAND_CALIBRATE 2
84#define MXT_COMMAND_REPORTALL 3
85#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070086
Iiro Valkonen81c88a72011-07-04 03:08:25 -070087/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080088#define MXT_POWER_IDLEACQINT 0
89#define MXT_POWER_ACTVACQINT 1
90#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen81c88a72011-07-04 03:08:25 -070092/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080093#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
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700100
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700101/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800102#define MXT_TOUCH_CTRL 0
103#define MXT_TOUCH_XORIGIN 1
104#define MXT_TOUCH_YORIGIN 2
105#define MXT_TOUCH_XSIZE 3
106#define MXT_TOUCH_YSIZE 4
107#define MXT_TOUCH_BLEN 6
108#define MXT_TOUCH_TCHTHR 7
109#define MXT_TOUCH_TCHDI 8
110#define MXT_TOUCH_ORIENT 9
111#define MXT_TOUCH_MOVHYSTI 11
112#define MXT_TOUCH_MOVHYSTN 12
113#define MXT_TOUCH_NUMTOUCH 14
114#define MXT_TOUCH_MRGHYST 15
115#define MXT_TOUCH_MRGTHR 16
116#define MXT_TOUCH_AMPHYST 17
117#define MXT_TOUCH_XRANGE_LSB 18
118#define MXT_TOUCH_XRANGE_MSB 19
119#define MXT_TOUCH_YRANGE_LSB 20
120#define MXT_TOUCH_YRANGE_MSB 21
121#define MXT_TOUCH_XLOCLIP 22
122#define MXT_TOUCH_XHICLIP 23
123#define MXT_TOUCH_YLOCLIP 24
124#define MXT_TOUCH_YHICLIP 25
125#define MXT_TOUCH_XEDGECTRL 26
126#define MXT_TOUCH_XEDGEDIST 27
127#define MXT_TOUCH_YEDGECTRL 28
128#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700129#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700130
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700131/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800132#define MXT_GRIPFACE_CTRL 0
133#define MXT_GRIPFACE_XLOGRIP 1
134#define MXT_GRIPFACE_XHIGRIP 2
135#define MXT_GRIPFACE_YLOGRIP 3
136#define MXT_GRIPFACE_YHIGRIP 4
137#define MXT_GRIPFACE_MAXTCHS 5
138#define MXT_GRIPFACE_SZTHR1 7
139#define MXT_GRIPFACE_SZTHR2 8
140#define MXT_GRIPFACE_SHPTHR1 9
141#define MXT_GRIPFACE_SHPTHR2 10
142#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700143
Iiro Valkonen7686b102011-02-02 23:21:58 -0800144/* MXT_PROCI_NOISE field */
145#define MXT_NOISE_CTRL 0
146#define MXT_NOISE_OUTFLEN 1
147#define MXT_NOISE_GCAFUL_LSB 3
148#define MXT_NOISE_GCAFUL_MSB 4
149#define MXT_NOISE_GCAFLL_LSB 5
150#define MXT_NOISE_GCAFLL_MSB 6
151#define MXT_NOISE_ACTVGCAFVALID 7
152#define MXT_NOISE_NOISETHR 8
153#define MXT_NOISE_FREQHOPSCALE 10
154#define MXT_NOISE_FREQ0 11
155#define MXT_NOISE_FREQ1 12
156#define MXT_NOISE_FREQ2 13
157#define MXT_NOISE_FREQ3 14
158#define MXT_NOISE_FREQ4 15
159#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700160
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700161/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800162#define MXT_COMMS_CTRL 0
163#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700165/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166#define MXT_CTE_CTRL 0
167#define MXT_CTE_CMD 1
168#define MXT_CTE_MODE 2
169#define MXT_CTE_IDLEGCAFDEPTH 3
170#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700171#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700172
Iiro Valkonen7686b102011-02-02 23:21:58 -0800173#define MXT_VOLTAGE_DEFAULT 2700000
174#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700175
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700176/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800177#define MXT_BOOT_VALUE 0xa5
178#define MXT_BACKUP_VALUE 0x55
179#define MXT_BACKUP_TIME 25 /* msec */
180#define MXT_RESET_TIME 65 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700181
Iiro Valkonen7686b102011-02-02 23:21:58 -0800182#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700183
184/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800185#define MXT_UNLOCK_CMD_MSB 0xaa
186#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700187
188/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800189#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
190#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
191#define MXT_FRAME_CRC_CHECK 0x02
192#define MXT_FRAME_CRC_FAIL 0x03
193#define MXT_FRAME_CRC_PASS 0x04
194#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
195#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700196
197/* Touch status */
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800198#define MXT_UNGRIP (1 << 0)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800199#define MXT_SUPPRESS (1 << 1)
200#define MXT_AMP (1 << 2)
201#define MXT_VECTOR (1 << 3)
202#define MXT_MOVE (1 << 4)
203#define MXT_RELEASE (1 << 5)
204#define MXT_PRESS (1 << 6)
205#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700206
Joonyoung Shim910d8052011-04-12 23:14:38 -0700207/* Touch orient bits */
208#define MXT_XY_SWITCH (1 << 0)
209#define MXT_X_INVERT (1 << 1)
210#define MXT_Y_INVERT (1 << 2)
211
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700212/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800213#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700214
Iiro Valkonen7686b102011-02-02 23:21:58 -0800215#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700216
Iiro Valkonen7686b102011-02-02 23:21:58 -0800217struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700218 u8 family_id;
219 u8 variant_id;
220 u8 version;
221 u8 build;
222 u8 matrix_xsize;
223 u8 matrix_ysize;
224 u8 object_num;
225};
226
Iiro Valkonen7686b102011-02-02 23:21:58 -0800227struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700228 u8 type;
229 u16 start_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800230 u8 size; /* Size of each instance - 1 */
231 u8 instances; /* Number of instances - 1 */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700232 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800233} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700234
Iiro Valkonen7686b102011-02-02 23:21:58 -0800235struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700236 u8 reportid;
237 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700238};
239
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700240/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800241struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700242 struct i2c_client *client;
243 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800244 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800245 const struct mxt_platform_data *pdata;
246 struct mxt_object *object_table;
247 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700248 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700249 unsigned int max_x;
250 unsigned int max_y;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800251
252 /* Cached parameters from object table */
253 u8 T9_reportid_min;
254 u8 T9_reportid_max;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700255};
256
Iiro Valkonen7686b102011-02-02 23:21:58 -0800257static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700258{
259 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700260 case MXT_GEN_COMMAND_T6:
261 case MXT_GEN_POWER_T7:
262 case MXT_GEN_ACQUIRE_T8:
263 case MXT_GEN_DATASOURCE_T53:
264 case MXT_TOUCH_MULTI_T9:
265 case MXT_TOUCH_KEYARRAY_T15:
266 case MXT_TOUCH_PROXIMITY_T23:
267 case MXT_TOUCH_PROXKEY_T52:
268 case MXT_PROCI_GRIPFACE_T20:
269 case MXT_PROCG_NOISE_T22:
270 case MXT_PROCI_ONETOUCH_T24:
271 case MXT_PROCI_TWOTOUCH_T27:
272 case MXT_PROCI_GRIP_T40:
273 case MXT_PROCI_PALM_T41:
274 case MXT_PROCI_TOUCHSUPPRESSION_T42:
275 case MXT_PROCI_STYLUS_T47:
276 case MXT_PROCG_NOISESUPPRESSION_T48:
277 case MXT_SPT_COMMSCONFIG_T18:
278 case MXT_SPT_GPIOPWM_T19:
279 case MXT_SPT_SELFTEST_T25:
280 case MXT_SPT_CTECONFIG_T28:
281 case MXT_SPT_USERDATA_T38:
282 case MXT_SPT_DIGITIZER_T43:
283 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700284 return true;
285 default:
286 return false;
287 }
288}
289
Iiro Valkonen7686b102011-02-02 23:21:58 -0800290static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700291{
292 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700293 case MXT_GEN_COMMAND_T6:
294 case MXT_GEN_POWER_T7:
295 case MXT_GEN_ACQUIRE_T8:
296 case MXT_TOUCH_MULTI_T9:
297 case MXT_TOUCH_KEYARRAY_T15:
298 case MXT_TOUCH_PROXIMITY_T23:
299 case MXT_TOUCH_PROXKEY_T52:
300 case MXT_PROCI_GRIPFACE_T20:
301 case MXT_PROCG_NOISE_T22:
302 case MXT_PROCI_ONETOUCH_T24:
303 case MXT_PROCI_TWOTOUCH_T27:
304 case MXT_PROCI_GRIP_T40:
305 case MXT_PROCI_PALM_T41:
306 case MXT_PROCI_TOUCHSUPPRESSION_T42:
307 case MXT_PROCI_STYLUS_T47:
308 case MXT_PROCG_NOISESUPPRESSION_T48:
309 case MXT_SPT_COMMSCONFIG_T18:
310 case MXT_SPT_GPIOPWM_T19:
311 case MXT_SPT_SELFTEST_T25:
312 case MXT_SPT_CTECONFIG_T28:
313 case MXT_SPT_DIGITIZER_T43:
314 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700315 return true;
316 default:
317 return false;
318 }
319}
320
Iiro Valkonen7686b102011-02-02 23:21:58 -0800321static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700322 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700323{
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700324 dev_dbg(dev, "reportid: %u\tmessage: %02x %02x %02x %02x %02x %02x %02x\n",
325 message->reportid, message->message[0], message->message[1],
326 message->message[2], message->message[3], message->message[4],
327 message->message[5], message->message[6]);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700328}
329
Iiro Valkonen7686b102011-02-02 23:21:58 -0800330static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700331 unsigned int state)
332{
333 u8 val;
334
335recheck:
336 if (i2c_master_recv(client, &val, 1) != 1) {
337 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
338 return -EIO;
339 }
340
341 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800342 case MXT_WAITING_BOOTLOAD_CMD:
343 case MXT_WAITING_FRAME_DATA:
344 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700345 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800346 case MXT_FRAME_CRC_PASS:
347 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700348 goto recheck;
349 break;
350 default:
351 return -EINVAL;
352 }
353
354 if (val != state) {
355 dev_err(&client->dev, "Unvalid bootloader mode state\n");
356 return -EINVAL;
357 }
358
359 return 0;
360}
361
Iiro Valkonen7686b102011-02-02 23:21:58 -0800362static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700363{
364 u8 buf[2];
365
Iiro Valkonen7686b102011-02-02 23:21:58 -0800366 buf[0] = MXT_UNLOCK_CMD_LSB;
367 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700368
369 if (i2c_master_send(client, buf, 2) != 2) {
370 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
371 return -EIO;
372 }
373
374 return 0;
375}
376
Iiro Valkonen7686b102011-02-02 23:21:58 -0800377static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700378 const u8 *data, unsigned int frame_size)
379{
380 if (i2c_master_send(client, data, frame_size) != frame_size) {
381 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
382 return -EIO;
383 }
384
385 return 0;
386}
387
Iiro Valkonen7686b102011-02-02 23:21:58 -0800388static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700389 u16 reg, u16 len, void *val)
390{
391 struct i2c_msg xfer[2];
392 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800393 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700394
395 buf[0] = reg & 0xff;
396 buf[1] = (reg >> 8) & 0xff;
397
398 /* Write register */
399 xfer[0].addr = client->addr;
400 xfer[0].flags = 0;
401 xfer[0].len = 2;
402 xfer[0].buf = buf;
403
404 /* Read data */
405 xfer[1].addr = client->addr;
406 xfer[1].flags = I2C_M_RD;
407 xfer[1].len = len;
408 xfer[1].buf = val;
409
Daniel Kurtz771733e2012-06-28 21:08:11 +0800410 ret = i2c_transfer(client->adapter, xfer, 2);
411 if (ret == 2) {
412 ret = 0;
413 } else {
414 if (ret >= 0)
415 ret = -EIO;
416 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
417 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700418 }
419
Daniel Kurtz771733e2012-06-28 21:08:11 +0800420 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700421}
422
Iiro Valkonen7686b102011-02-02 23:21:58 -0800423static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700424{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800425 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700426}
427
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800428static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
429 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800431 u8 *buf;
432 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800433 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700434
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800435 count = len + 2;
436 buf = kmalloc(count, GFP_KERNEL);
437 if (!buf)
438 return -ENOMEM;
439
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700440 buf[0] = reg & 0xff;
441 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800442 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700443
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800444 ret = i2c_master_send(client, buf, count);
445 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800446 ret = 0;
447 } else {
448 if (ret >= 0)
449 ret = -EIO;
450 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
451 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700452 }
453
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800454 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800455 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700456}
457
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800458static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
459{
460 return __mxt_write_reg(client, reg, 1, &val);
461}
462
Iiro Valkonen7686b102011-02-02 23:21:58 -0800463static struct mxt_object *
464mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700465{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800466 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700467 int i;
468
469 for (i = 0; i < data->info.object_num; i++) {
470 object = data->object_table + i;
471 if (object->type == type)
472 return object;
473 }
474
475 dev_err(&data->client->dev, "Invalid object type\n");
476 return NULL;
477}
478
Iiro Valkonen7686b102011-02-02 23:21:58 -0800479static int mxt_read_message(struct mxt_data *data,
480 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700481{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800482 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483 u16 reg;
484
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700485 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700486 if (!object)
487 return -EINVAL;
488
489 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 return __mxt_read_reg(data->client, reg,
491 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700492}
493
Iiro Valkonen7686b102011-02-02 23:21:58 -0800494static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495 u8 type, u8 offset, u8 val)
496{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800497 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498 u16 reg;
499
Iiro Valkonen7686b102011-02-02 23:21:58 -0800500 object = mxt_get_object(data, type);
Daniel Kurtzd1ff3202012-05-08 22:38:52 -0700501 if (!object || offset >= object->size + 1)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700502 return -EINVAL;
503
504 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800505 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700506}
507
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508static void mxt_input_touchevent(struct mxt_data *data,
509 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700510{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511 struct device *dev = &data->client->dev;
512 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800513 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514 int x;
515 int y;
516 int area;
Yufeng Shen28ac2932011-08-16 00:40:54 -0700517 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700518
Joonyoung Shim910d8052011-04-12 23:14:38 -0700519 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
520 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
521 if (data->max_x < 1024)
522 x = x >> 2;
523 if (data->max_y < 1024)
524 y = y >> 2;
525
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700526 area = message->message[4];
Yufeng Shen28ac2932011-08-16 00:40:54 -0700527 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800529 dev_dbg(dev,
530 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
531 id,
532 (status & MXT_DETECT) ? 'D' : '.',
533 (status & MXT_PRESS) ? 'P' : '.',
534 (status & MXT_RELEASE) ? 'R' : '.',
535 (status & MXT_MOVE) ? 'M' : '.',
536 (status & MXT_VECTOR) ? 'V' : '.',
537 (status & MXT_AMP) ? 'A' : '.',
538 (status & MXT_SUPPRESS) ? 'S' : '.',
539 (status & MXT_UNGRIP) ? 'U' : '.',
540 x, y, area, pressure);
541
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800542 input_mt_slot(input_dev, id);
543 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
544 status & MXT_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800546 if (status & MXT_DETECT) {
547 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
548 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
549 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
550 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
551 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700552
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800553 input_mt_report_pointer_emulation(input_dev, false);
554 input_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700555}
556
Daniel Kurtz04a79182012-06-28 21:08:21 +0800557static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
558{
559 u8 id = msg->reportid;
560 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
561}
562
Iiro Valkonen7686b102011-02-02 23:21:58 -0800563static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800565 struct mxt_data *data = dev_id;
566 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700567 struct device *dev = &data->client->dev;
568 int id;
569 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700570
571 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800572 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700573 dev_err(dev, "Failed to read message\n");
574 goto end;
575 }
576
577 reportid = message.reportid;
578
Daniel Kurtz04a79182012-06-28 21:08:21 +0800579 id = reportid - data->T9_reportid_min;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580
Daniel Kurtz04a79182012-06-28 21:08:21 +0800581 if (mxt_is_T9_message(data, &message))
Iiro Valkonen7686b102011-02-02 23:21:58 -0800582 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700583 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800584 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700585 } while (reportid != 0xff);
586
587end:
588 return IRQ_HANDLED;
589}
590
Iiro Valkonen7686b102011-02-02 23:21:58 -0800591static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800593 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800594 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700595 struct device *dev = &data->client->dev;
596 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800597 int i, size;
598 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599
Iiro Valkonen71749f52011-02-15 13:36:52 -0800600 if (!pdata->config) {
601 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
602 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603 }
604
605 for (i = 0; i < data->info.object_num; i++) {
606 object = data->object_table + i;
607
Iiro Valkonen7686b102011-02-02 23:21:58 -0800608 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609 continue;
610
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800611 size = (object->size + 1) * (object->instances + 1);
612 if (index + size > pdata->config_length) {
613 dev_err(dev, "Not enough config data!\n");
614 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800615 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800616
617 ret = __mxt_write_reg(data->client, object->start_address,
618 size, &pdata->config[index]);
619 if (ret)
620 return ret;
621 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700622 }
623
624 return 0;
625}
626
Iiro Valkonen7686b102011-02-02 23:21:58 -0800627static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700628{
629 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800630 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631 int count = 10;
632 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700633
634 /* Read dummy message to make high CHG pin */
635 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800636 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637 if (error)
638 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800639 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700640
641 if (!count) {
642 dev_err(dev, "CHG pin isn't cleared\n");
643 return -EBUSY;
644 }
645
646 return 0;
647}
648
Iiro Valkonen7686b102011-02-02 23:21:58 -0800649static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700650{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800651 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700652 u8 voltage;
653
654 /* Set touchscreen lines */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700655 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656 pdata->x_line);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700657 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658 pdata->y_line);
659
660 /* Set touchscreen orient */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700661 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662 pdata->orient);
663
664 /* Set touchscreen burst length */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700665 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800666 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667
668 /* Set touchscreen threshold */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700669 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800670 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700671
672 /* Set touchscreen resolution */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700673 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800674 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700675 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800676 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700677 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800678 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700679 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800680 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700681
682 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700683 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800684 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
685 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
686 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687 voltage = 0xff - voltage + 1;
688 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800689 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
690 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700692 mxt_write_object(data, MXT_SPT_CTECONFIG_T28,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800693 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700694 }
695}
696
Iiro Valkonen7686b102011-02-02 23:21:58 -0800697static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698{
699 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800700 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702
Daniel Kurtz23003a842012-06-28 21:08:14 +0800703 /* Read 7-byte info block starting at address 0 */
704 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705 if (error)
706 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700707
708 return 0;
709}
710
Iiro Valkonen7686b102011-02-02 23:21:58 -0800711static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700712{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800713 struct i2c_client *client = data->client;
714 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715 int error;
716 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800717 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800719 table_size = data->info.object_num * sizeof(struct mxt_object);
720 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
721 data->object_table);
722 if (error)
723 return error;
724
725 /* Valid Report IDs start counting from 1 */
726 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800728 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800729 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800731 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700732
733 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800734 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700735 reportid += object->num_report_ids *
736 (object->instances + 1);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800737 max_id = reportid - 1;
738 } else {
739 min_id = 0;
740 max_id = 0;
741 }
742
743 dev_dbg(&data->client->dev,
744 "Type %2d Start %3d Size %3d Instances %2d ReportIDs %3u : %3u\n",
745 object->type, object->start_address, object->size + 1,
746 object->instances + 1, min_id, max_id);
747
748 switch (object->type) {
749 case MXT_TOUCH_MULTI_T9:
750 data->T9_reportid_min = min_id;
751 data->T9_reportid_max = max_id;
752 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700753 }
754 }
755
756 return 0;
757}
758
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800759static void mxt_free_object_table(struct mxt_data *data)
760{
761 kfree(data->object_table);
762 data->object_table = NULL;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800763 data->T9_reportid_min = 0;
764 data->T9_reportid_max = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800765}
766
Iiro Valkonen7686b102011-02-02 23:21:58 -0800767static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700768{
769 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771 int error;
772 u8 val;
773
Iiro Valkonen7686b102011-02-02 23:21:58 -0800774 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700775 if (error)
776 return error;
777
778 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700780 GFP_KERNEL);
781 if (!data->object_table) {
782 dev_err(&client->dev, "Failed to allocate memory\n");
783 return -ENOMEM;
784 }
785
786 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800787 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800789 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700790
791 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800792 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700793 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800794 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700795
Iiro Valkonen7686b102011-02-02 23:21:58 -0800796 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700797
798 /* Backup to memory */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700799 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800800 MXT_COMMAND_BACKUPNV,
801 MXT_BACKUP_VALUE);
802 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700803
804 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700805 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800806 MXT_COMMAND_RESET, 1);
807 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700808
809 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800810 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800812 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700813 info->matrix_xsize = val;
814
Iiro Valkonen7686b102011-02-02 23:21:58 -0800815 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700816 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800817 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818 info->matrix_ysize = val;
819
820 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800821 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
822 info->family_id, info->variant_id, info->version >> 4,
823 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824
825 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800826 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700827 info->matrix_xsize, info->matrix_ysize,
828 info->object_num);
829
830 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800831
832err_free_object_table:
833 mxt_free_object_table(data);
834 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700835}
836
Joonyoung Shim910d8052011-04-12 23:14:38 -0700837static void mxt_calc_resolution(struct mxt_data *data)
838{
839 unsigned int max_x = data->pdata->x_size - 1;
840 unsigned int max_y = data->pdata->y_size - 1;
841
842 if (data->pdata->orient & MXT_XY_SWITCH) {
843 data->max_x = max_y;
844 data->max_y = max_x;
845 } else {
846 data->max_x = max_x;
847 data->max_y = max_y;
848 }
849}
850
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +0800851/* Firmware Version is returned as Major.Minor.Build */
852static ssize_t mxt_fw_version_show(struct device *dev,
853 struct device_attribute *attr, char *buf)
854{
855 struct mxt_data *data = dev_get_drvdata(dev);
856 struct mxt_info *info = &data->info;
857 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
858 info->version >> 4, info->version & 0xf, info->build);
859}
860
861/* Hardware Version is returned as FamilyID.VariantID */
862static ssize_t mxt_hw_version_show(struct device *dev,
863 struct device_attribute *attr, char *buf)
864{
865 struct mxt_data *data = dev_get_drvdata(dev);
866 struct mxt_info *info = &data->info;
867 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
868 info->family_id, info->variant_id);
869}
870
Daniel Kurtz794eb672012-06-28 21:08:10 +0800871static ssize_t mxt_show_instance(char *buf, int count,
872 struct mxt_object *object, int instance,
873 const u8 *val)
874{
875 int i;
876
877 if (object->instances > 0)
878 count += scnprintf(buf + count, PAGE_SIZE - count,
879 "Instance %u\n", instance);
880
881 for (i = 0; i < object->size + 1; i++)
882 count += scnprintf(buf + count, PAGE_SIZE - count,
883 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
884 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
885
886 return count;
887}
888
Iiro Valkonen7686b102011-02-02 23:21:58 -0800889static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700890 struct device_attribute *attr, char *buf)
891{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800892 struct mxt_data *data = dev_get_drvdata(dev);
893 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700894 int count = 0;
895 int i, j;
896 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800897 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700898
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800899 /* Pre-allocate buffer large enough to hold max sized object. */
900 obuf = kmalloc(256, GFP_KERNEL);
901 if (!obuf)
902 return -ENOMEM;
903
904 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700905 for (i = 0; i < data->info.object_num; i++) {
906 object = data->object_table + i;
907
Daniel Kurtz91630952012-06-28 21:08:09 +0800908 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909 continue;
Daniel Kurtz91630952012-06-28 21:08:09 +0800910
911 count += scnprintf(buf + count, PAGE_SIZE - count,
912 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700913
Daniel Kurtz794eb672012-06-28 21:08:10 +0800914 for (j = 0; j < object->instances + 1; j++) {
915 u16 size = object->size + 1;
916 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800917
Daniel Kurtz794eb672012-06-28 21:08:10 +0800918 error = __mxt_read_reg(data->client, addr, size, obuf);
919 if (error)
920 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700921
Daniel Kurtz794eb672012-06-28 21:08:10 +0800922 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700923 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700924 }
925
Daniel Kurtz794eb672012-06-28 21:08:10 +0800926done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800927 kfree(obuf);
928 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929}
930
Iiro Valkonen7686b102011-02-02 23:21:58 -0800931static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700932{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800933 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700934 struct i2c_client *client = data->client;
935 const struct firmware *fw = NULL;
936 unsigned int frame_size;
937 unsigned int pos = 0;
938 int ret;
939
940 ret = request_firmware(&fw, fn, dev);
941 if (ret) {
942 dev_err(dev, "Unable to open firmware %s\n", fn);
943 return ret;
944 }
945
946 /* Change to the bootloader mode */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700947 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800948 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
949 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950
951 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800952 if (client->addr == MXT_APP_LOW)
953 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958 if (ret)
959 goto out;
960
961 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800962 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700963
964 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800965 ret = mxt_check_bootloader(client,
966 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967 if (ret)
968 goto out;
969
970 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
971
972 /* We should add 2 at frame size as the the firmware data is not
973 * included the CRC bytes.
974 */
975 frame_size += 2;
976
977 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800978 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700979
Iiro Valkonen7686b102011-02-02 23:21:58 -0800980 ret = mxt_check_bootloader(client,
981 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700982 if (ret)
983 goto out;
984
985 pos += frame_size;
986
987 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
988 }
989
990out:
991 release_firmware(fw);
992
993 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 if (client->addr == MXT_BOOT_LOW)
995 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800997 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998
999 return ret;
1000}
1001
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003 struct device_attribute *attr,
1004 const char *buf, size_t count)
1005{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001006 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 int error;
1008
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009 disable_irq(data->irq);
1010
Iiro Valkonen7686b102011-02-02 23:21:58 -08001011 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012 if (error) {
1013 dev_err(dev, "The firmware update failed(%d)\n", error);
1014 count = error;
1015 } else {
1016 dev_dbg(dev, "The firmware update succeeded\n");
1017
1018 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001019 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001021 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022
Iiro Valkonen7686b102011-02-02 23:21:58 -08001023 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001024 }
1025
1026 enable_irq(data->irq);
1027
Iiro Valkonen08960a02011-04-12 23:16:40 -07001028 error = mxt_make_highchg(data);
1029 if (error)
1030 return error;
1031
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001032 return count;
1033}
1034
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001035static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1036static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001037static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1038static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001039
Iiro Valkonen7686b102011-02-02 23:21:58 -08001040static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001041 &dev_attr_fw_version.attr,
1042 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043 &dev_attr_object.attr,
1044 &dev_attr_update_fw.attr,
1045 NULL
1046};
1047
Iiro Valkonen7686b102011-02-02 23:21:58 -08001048static const struct attribute_group mxt_attr_group = {
1049 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050};
1051
Iiro Valkonen7686b102011-02-02 23:21:58 -08001052static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001053{
1054 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001055 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001056 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001057}
1058
Iiro Valkonen7686b102011-02-02 23:21:58 -08001059static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001060{
1061 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001062 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001063 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001064}
1065
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001068 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001069
Iiro Valkonen7686b102011-02-02 23:21:58 -08001070 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001071
1072 return 0;
1073}
1074
Iiro Valkonen7686b102011-02-02 23:21:58 -08001075static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001076{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001077 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080}
1081
Iiro Valkonen7686b102011-02-02 23:21:58 -08001082static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083 const struct i2c_device_id *id)
1084{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001085 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001087 struct input_dev *input_dev;
1088 int error;
1089
Iiro Valkonen919ed892011-02-15 13:36:52 -08001090 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001091 return -EINVAL;
1092
Iiro Valkonen7686b102011-02-02 23:21:58 -08001093 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001094 input_dev = input_allocate_device();
1095 if (!data || !input_dev) {
1096 dev_err(&client->dev, "Failed to allocate memory\n");
1097 error = -ENOMEM;
1098 goto err_free_mem;
1099 }
1100
Iiro Valkonen7686b102011-02-02 23:21:58 -08001101 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001102 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1103 client->adapter->nr, client->addr);
1104 input_dev->phys = data->phys;
1105
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001106 input_dev->id.bustype = BUS_I2C;
1107 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001108 input_dev->open = mxt_input_open;
1109 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001110
Joonyoung Shim910d8052011-04-12 23:14:38 -07001111 data->client = client;
1112 data->input_dev = input_dev;
1113 data->pdata = pdata;
1114 data->irq = client->irq;
1115
1116 mxt_calc_resolution(data);
1117
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001118 __set_bit(EV_ABS, input_dev->evbit);
1119 __set_bit(EV_KEY, input_dev->evbit);
1120 __set_bit(BTN_TOUCH, input_dev->keybit);
1121
1122 /* For single touch */
1123 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001124 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001125 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001126 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001127 input_set_abs_params(input_dev, ABS_PRESSURE,
1128 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129
1130 /* For multi touch */
Daniel Kurtze1e16582012-06-28 21:08:04 +08001131 error = input_mt_init_slots(input_dev, MXT_MAX_FINGER);
1132 if (error)
1133 goto err_free_mem;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001134 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001135 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001136 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001137 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001138 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001139 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001140 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1141 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001142
1143 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001144 i2c_set_clientdata(client, data);
1145
Iiro Valkonen7686b102011-02-02 23:21:58 -08001146 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001147 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001148 goto err_free_mem;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001149
Iiro Valkonen7686b102011-02-02 23:21:58 -08001150 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Daniel Kurtzc2ef9a12012-06-28 21:08:03 +08001151 pdata->irqflags, client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001152 if (error) {
1153 dev_err(&client->dev, "Failed to register interrupt\n");
1154 goto err_free_object;
1155 }
1156
Iiro Valkonen08960a02011-04-12 23:16:40 -07001157 error = mxt_make_highchg(data);
1158 if (error)
1159 goto err_free_irq;
1160
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001161 error = input_register_device(input_dev);
1162 if (error)
1163 goto err_free_irq;
1164
Iiro Valkonen7686b102011-02-02 23:21:58 -08001165 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166 if (error)
Daniel Kurtz63990032012-06-28 21:08:05 +08001167 dev_warn(&client->dev, "error creating sysfs entries.\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168
1169 return 0;
1170
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001171err_free_irq:
1172 free_irq(client->irq, data);
1173err_free_object:
1174 kfree(data->object_table);
1175err_free_mem:
1176 input_free_device(input_dev);
1177 kfree(data);
1178 return error;
1179}
1180
Iiro Valkonen7686b102011-02-02 23:21:58 -08001181static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001182{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001183 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001184
Iiro Valkonen7686b102011-02-02 23:21:58 -08001185 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001186 free_irq(data->irq, data);
1187 input_unregister_device(data->input_dev);
1188 kfree(data->object_table);
1189 kfree(data);
1190
1191 return 0;
1192}
1193
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001194#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001195static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001196{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001197 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001198 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001199 struct input_dev *input_dev = data->input_dev;
1200
1201 mutex_lock(&input_dev->mutex);
1202
1203 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001204 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001205
1206 mutex_unlock(&input_dev->mutex);
1207
1208 return 0;
1209}
1210
Iiro Valkonen7686b102011-02-02 23:21:58 -08001211static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001212{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001213 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001214 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001215 struct input_dev *input_dev = data->input_dev;
1216
1217 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001218 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001219 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001220
Iiro Valkonen7686b102011-02-02 23:21:58 -08001221 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001222
1223 mutex_lock(&input_dev->mutex);
1224
1225 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001226 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001227
1228 mutex_unlock(&input_dev->mutex);
1229
1230 return 0;
1231}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001232#endif
1233
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001234static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1235
Iiro Valkonen7686b102011-02-02 23:21:58 -08001236static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001237 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001238 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001239 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001240 { }
1241};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001242MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001243
Iiro Valkonen7686b102011-02-02 23:21:58 -08001244static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001245 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001246 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001248 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001249 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001250 .probe = mxt_probe,
1251 .remove = __devexit_p(mxt_remove),
1252 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001253};
1254
Axel Lin1b92c1c2012-03-16 23:05:41 -07001255module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001256
1257/* Module information */
1258MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001259MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001260MODULE_LICENSE("GPL");