blob: 48f3637aecaaf60a83a11e9c084030d5860e8646 [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
Iiro Valkonen7686b102011-02-02 23:21:58 -0800557static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700558{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800559 struct mxt_data *data = dev_id;
560 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561 struct device *dev = &data->client->dev;
562 int id;
563 u8 reportid;
564 u8 max_reportid;
565 u8 min_reportid;
566
567 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800568 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569 dev_err(dev, "Failed to read message\n");
570 goto end;
571 }
572
573 reportid = message.reportid;
574
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800575 max_reportid = data->T9_reportid_max;
576 min_reportid = data->T9_reportid_min;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 id = reportid - min_reportid;
578
579 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800580 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700581 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800582 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700583 } while (reportid != 0xff);
584
585end:
586 return IRQ_HANDLED;
587}
588
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800591 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 struct device *dev = &data->client->dev;
594 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800595 int i, size;
596 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597
Iiro Valkonen71749f52011-02-15 13:36:52 -0800598 if (!pdata->config) {
599 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
600 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601 }
602
603 for (i = 0; i < data->info.object_num; i++) {
604 object = data->object_table + i;
605
Iiro Valkonen7686b102011-02-02 23:21:58 -0800606 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700607 continue;
608
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800609 size = (object->size + 1) * (object->instances + 1);
610 if (index + size > pdata->config_length) {
611 dev_err(dev, "Not enough config data!\n");
612 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800613 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800614
615 ret = __mxt_write_reg(data->client, object->start_address,
616 size, &pdata->config[index]);
617 if (ret)
618 return ret;
619 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700620 }
621
622 return 0;
623}
624
Iiro Valkonen7686b102011-02-02 23:21:58 -0800625static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700626{
627 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800628 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700629 int count = 10;
630 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631
632 /* Read dummy message to make high CHG pin */
633 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800634 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635 if (error)
636 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800637 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700638
639 if (!count) {
640 dev_err(dev, "CHG pin isn't cleared\n");
641 return -EBUSY;
642 }
643
644 return 0;
645}
646
Iiro Valkonen7686b102011-02-02 23:21:58 -0800647static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700648{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800649 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700650 u8 voltage;
651
652 /* Set touchscreen lines */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700653 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700654 pdata->x_line);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700655 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656 pdata->y_line);
657
658 /* Set touchscreen orient */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700659 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660 pdata->orient);
661
662 /* Set touchscreen burst length */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700663 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800664 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665
666 /* Set touchscreen threshold */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700667 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800668 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669
670 /* Set touchscreen resolution */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700671 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800672 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
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_MSB, (pdata->x_size - 1) >> 8);
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_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
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_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679
680 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700681 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
683 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
684 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700685 voltage = 0xff - voltage + 1;
686 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800687 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
688 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700690 mxt_write_object(data, MXT_SPT_CTECONFIG_T28,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800691 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692 }
693}
694
Iiro Valkonen7686b102011-02-02 23:21:58 -0800695static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696{
697 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700
Daniel Kurtz23003a842012-06-28 21:08:14 +0800701 /* Read 7-byte info block starting at address 0 */
702 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700703 if (error)
704 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705
706 return 0;
707}
708
Iiro Valkonen7686b102011-02-02 23:21:58 -0800709static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700710{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800711 struct i2c_client *client = data->client;
712 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700713 int error;
714 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800715 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800717 table_size = data->info.object_num * sizeof(struct mxt_object);
718 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
719 data->object_table);
720 if (error)
721 return error;
722
723 /* Valid Report IDs start counting from 1 */
724 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800726 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800727 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800729 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730
731 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800732 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700733 reportid += object->num_report_ids *
734 (object->instances + 1);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800735 max_id = reportid - 1;
736 } else {
737 min_id = 0;
738 max_id = 0;
739 }
740
741 dev_dbg(&data->client->dev,
742 "Type %2d Start %3d Size %3d Instances %2d ReportIDs %3u : %3u\n",
743 object->type, object->start_address, object->size + 1,
744 object->instances + 1, min_id, max_id);
745
746 switch (object->type) {
747 case MXT_TOUCH_MULTI_T9:
748 data->T9_reportid_min = min_id;
749 data->T9_reportid_max = max_id;
750 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700751 }
752 }
753
754 return 0;
755}
756
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800757static void mxt_free_object_table(struct mxt_data *data)
758{
759 kfree(data->object_table);
760 data->object_table = NULL;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800761 data->T9_reportid_min = 0;
762 data->T9_reportid_max = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800763}
764
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766{
767 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800768 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700769 int error;
770 u8 val;
771
Iiro Valkonen7686b102011-02-02 23:21:58 -0800772 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700773 if (error)
774 return error;
775
776 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800777 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700778 GFP_KERNEL);
779 if (!data->object_table) {
780 dev_err(&client->dev, "Failed to allocate memory\n");
781 return -ENOMEM;
782 }
783
784 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800785 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800787 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788
789 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800792 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700793
Iiro Valkonen7686b102011-02-02 23:21:58 -0800794 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700795
796 /* Backup to memory */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700797 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800798 MXT_COMMAND_BACKUPNV,
799 MXT_BACKUP_VALUE);
800 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801
802 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700803 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800804 MXT_COMMAND_RESET, 1);
805 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700806
807 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800808 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700809 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800810 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811 info->matrix_xsize = val;
812
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800815 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700816 info->matrix_ysize = val;
817
818 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800819 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
820 info->family_id, info->variant_id, info->version >> 4,
821 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700822
823 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800824 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700825 info->matrix_xsize, info->matrix_ysize,
826 info->object_num);
827
828 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800829
830err_free_object_table:
831 mxt_free_object_table(data);
832 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700833}
834
Joonyoung Shim910d8052011-04-12 23:14:38 -0700835static void mxt_calc_resolution(struct mxt_data *data)
836{
837 unsigned int max_x = data->pdata->x_size - 1;
838 unsigned int max_y = data->pdata->y_size - 1;
839
840 if (data->pdata->orient & MXT_XY_SWITCH) {
841 data->max_x = max_y;
842 data->max_y = max_x;
843 } else {
844 data->max_x = max_x;
845 data->max_y = max_y;
846 }
847}
848
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +0800849/* Firmware Version is returned as Major.Minor.Build */
850static ssize_t mxt_fw_version_show(struct device *dev,
851 struct device_attribute *attr, char *buf)
852{
853 struct mxt_data *data = dev_get_drvdata(dev);
854 struct mxt_info *info = &data->info;
855 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
856 info->version >> 4, info->version & 0xf, info->build);
857}
858
859/* Hardware Version is returned as FamilyID.VariantID */
860static ssize_t mxt_hw_version_show(struct device *dev,
861 struct device_attribute *attr, char *buf)
862{
863 struct mxt_data *data = dev_get_drvdata(dev);
864 struct mxt_info *info = &data->info;
865 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
866 info->family_id, info->variant_id);
867}
868
Daniel Kurtz794eb672012-06-28 21:08:10 +0800869static ssize_t mxt_show_instance(char *buf, int count,
870 struct mxt_object *object, int instance,
871 const u8 *val)
872{
873 int i;
874
875 if (object->instances > 0)
876 count += scnprintf(buf + count, PAGE_SIZE - count,
877 "Instance %u\n", instance);
878
879 for (i = 0; i < object->size + 1; i++)
880 count += scnprintf(buf + count, PAGE_SIZE - count,
881 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
882 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
883
884 return count;
885}
886
Iiro Valkonen7686b102011-02-02 23:21:58 -0800887static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700888 struct device_attribute *attr, char *buf)
889{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800890 struct mxt_data *data = dev_get_drvdata(dev);
891 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700892 int count = 0;
893 int i, j;
894 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800895 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700896
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800897 /* Pre-allocate buffer large enough to hold max sized object. */
898 obuf = kmalloc(256, GFP_KERNEL);
899 if (!obuf)
900 return -ENOMEM;
901
902 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700903 for (i = 0; i < data->info.object_num; i++) {
904 object = data->object_table + i;
905
Daniel Kurtz91630952012-06-28 21:08:09 +0800906 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700907 continue;
Daniel Kurtz91630952012-06-28 21:08:09 +0800908
909 count += scnprintf(buf + count, PAGE_SIZE - count,
910 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700911
Daniel Kurtz794eb672012-06-28 21:08:10 +0800912 for (j = 0; j < object->instances + 1; j++) {
913 u16 size = object->size + 1;
914 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800915
Daniel Kurtz794eb672012-06-28 21:08:10 +0800916 error = __mxt_read_reg(data->client, addr, size, obuf);
917 if (error)
918 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700919
Daniel Kurtz794eb672012-06-28 21:08:10 +0800920 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700921 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700922 }
923
Daniel Kurtz794eb672012-06-28 21:08:10 +0800924done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800925 kfree(obuf);
926 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700927}
928
Iiro Valkonen7686b102011-02-02 23:21:58 -0800929static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700930{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800931 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700932 struct i2c_client *client = data->client;
933 const struct firmware *fw = NULL;
934 unsigned int frame_size;
935 unsigned int pos = 0;
936 int ret;
937
938 ret = request_firmware(&fw, fn, dev);
939 if (ret) {
940 dev_err(dev, "Unable to open firmware %s\n", fn);
941 return ret;
942 }
943
944 /* Change to the bootloader mode */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700945 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800946 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
947 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700948
949 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800950 if (client->addr == MXT_APP_LOW)
951 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700952 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956 if (ret)
957 goto out;
958
959 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800960 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700961
962 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800963 ret = mxt_check_bootloader(client,
964 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700965 if (ret)
966 goto out;
967
968 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
969
970 /* We should add 2 at frame size as the the firmware data is not
971 * included the CRC bytes.
972 */
973 frame_size += 2;
974
975 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800976 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700977
Iiro Valkonen7686b102011-02-02 23:21:58 -0800978 ret = mxt_check_bootloader(client,
979 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980 if (ret)
981 goto out;
982
983 pos += frame_size;
984
985 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
986 }
987
988out:
989 release_firmware(fw);
990
991 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992 if (client->addr == MXT_BOOT_LOW)
993 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700994 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996
997 return ret;
998}
999
Iiro Valkonen7686b102011-02-02 23:21:58 -08001000static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001 struct device_attribute *attr,
1002 const char *buf, size_t count)
1003{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001004 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005 int error;
1006
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 disable_irq(data->irq);
1008
Iiro Valkonen7686b102011-02-02 23:21:58 -08001009 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010 if (error) {
1011 dev_err(dev, "The firmware update failed(%d)\n", error);
1012 count = error;
1013 } else {
1014 dev_dbg(dev, "The firmware update succeeded\n");
1015
1016 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001019 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020
Iiro Valkonen7686b102011-02-02 23:21:58 -08001021 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022 }
1023
1024 enable_irq(data->irq);
1025
Iiro Valkonen08960a02011-04-12 23:16:40 -07001026 error = mxt_make_highchg(data);
1027 if (error)
1028 return error;
1029
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001030 return count;
1031}
1032
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001033static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1034static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001035static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1036static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001037
Iiro Valkonen7686b102011-02-02 23:21:58 -08001038static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001039 &dev_attr_fw_version.attr,
1040 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001041 &dev_attr_object.attr,
1042 &dev_attr_update_fw.attr,
1043 NULL
1044};
1045
Iiro Valkonen7686b102011-02-02 23:21:58 -08001046static const struct attribute_group mxt_attr_group = {
1047 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001048};
1049
Iiro Valkonen7686b102011-02-02 23:21:58 -08001050static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001051{
1052 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001053 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001054 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001055}
1056
Iiro Valkonen7686b102011-02-02 23:21:58 -08001057static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001058{
1059 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001060 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001061 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001062}
1063
Iiro Valkonen7686b102011-02-02 23:21:58 -08001064static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067
Iiro Valkonen7686b102011-02-02 23:21:58 -08001068 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001069
1070 return 0;
1071}
1072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001075 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001076
Iiro Valkonen7686b102011-02-02 23:21:58 -08001077 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078}
1079
Iiro Valkonen7686b102011-02-02 23:21:58 -08001080static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001081 const struct i2c_device_id *id)
1082{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001083 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001084 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001085 struct input_dev *input_dev;
1086 int error;
1087
Iiro Valkonen919ed892011-02-15 13:36:52 -08001088 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001089 return -EINVAL;
1090
Iiro Valkonen7686b102011-02-02 23:21:58 -08001091 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001092 input_dev = input_allocate_device();
1093 if (!data || !input_dev) {
1094 dev_err(&client->dev, "Failed to allocate memory\n");
1095 error = -ENOMEM;
1096 goto err_free_mem;
1097 }
1098
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001100 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1101 client->adapter->nr, client->addr);
1102 input_dev->phys = data->phys;
1103
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104 input_dev->id.bustype = BUS_I2C;
1105 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001106 input_dev->open = mxt_input_open;
1107 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001108
Joonyoung Shim910d8052011-04-12 23:14:38 -07001109 data->client = client;
1110 data->input_dev = input_dev;
1111 data->pdata = pdata;
1112 data->irq = client->irq;
1113
1114 mxt_calc_resolution(data);
1115
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001116 __set_bit(EV_ABS, input_dev->evbit);
1117 __set_bit(EV_KEY, input_dev->evbit);
1118 __set_bit(BTN_TOUCH, input_dev->keybit);
1119
1120 /* For single touch */
1121 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001122 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001123 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001124 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001125 input_set_abs_params(input_dev, ABS_PRESSURE,
1126 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001127
1128 /* For multi touch */
Daniel Kurtze1e16582012-06-28 21:08:04 +08001129 error = input_mt_init_slots(input_dev, MXT_MAX_FINGER);
1130 if (error)
1131 goto err_free_mem;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001133 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001134 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001135 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001136 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001137 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001138 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1139 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140
1141 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001142 i2c_set_clientdata(client, data);
1143
Iiro Valkonen7686b102011-02-02 23:21:58 -08001144 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001145 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001146 goto err_free_mem;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001147
Iiro Valkonen7686b102011-02-02 23:21:58 -08001148 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Daniel Kurtzc2ef9a12012-06-28 21:08:03 +08001149 pdata->irqflags, client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001150 if (error) {
1151 dev_err(&client->dev, "Failed to register interrupt\n");
1152 goto err_free_object;
1153 }
1154
Iiro Valkonen08960a02011-04-12 23:16:40 -07001155 error = mxt_make_highchg(data);
1156 if (error)
1157 goto err_free_irq;
1158
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001159 error = input_register_device(input_dev);
1160 if (error)
1161 goto err_free_irq;
1162
Iiro Valkonen7686b102011-02-02 23:21:58 -08001163 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001164 if (error)
Daniel Kurtz63990032012-06-28 21:08:05 +08001165 dev_warn(&client->dev, "error creating sysfs entries.\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166
1167 return 0;
1168
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001169err_free_irq:
1170 free_irq(client->irq, data);
1171err_free_object:
1172 kfree(data->object_table);
1173err_free_mem:
1174 input_free_device(input_dev);
1175 kfree(data);
1176 return error;
1177}
1178
Iiro Valkonen7686b102011-02-02 23:21:58 -08001179static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001180{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001181 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001182
Iiro Valkonen7686b102011-02-02 23:21:58 -08001183 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001184 free_irq(data->irq, data);
1185 input_unregister_device(data->input_dev);
1186 kfree(data->object_table);
1187 kfree(data);
1188
1189 return 0;
1190}
1191
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001192#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001193static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001194{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001195 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001196 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001197 struct input_dev *input_dev = data->input_dev;
1198
1199 mutex_lock(&input_dev->mutex);
1200
1201 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001202 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001203
1204 mutex_unlock(&input_dev->mutex);
1205
1206 return 0;
1207}
1208
Iiro Valkonen7686b102011-02-02 23:21:58 -08001209static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001210{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001211 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001212 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001213 struct input_dev *input_dev = data->input_dev;
1214
1215 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001216 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001217 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001218
Iiro Valkonen7686b102011-02-02 23:21:58 -08001219 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001220
1221 mutex_lock(&input_dev->mutex);
1222
1223 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001224 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001225
1226 mutex_unlock(&input_dev->mutex);
1227
1228 return 0;
1229}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001230#endif
1231
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001232static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1233
Iiro Valkonen7686b102011-02-02 23:21:58 -08001234static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001235 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001236 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001237 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001238 { }
1239};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001240MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001241
Iiro Valkonen7686b102011-02-02 23:21:58 -08001242static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001243 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001244 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001245 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001246 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001248 .probe = mxt_probe,
1249 .remove = __devexit_p(mxt_remove),
1250 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001251};
1252
Axel Lin1b92c1c2012-03-16 23:05:41 -07001253module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001254
1255/* Module information */
1256MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001257MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001258MODULE_LICENSE("GPL");