blob: faa937c0bd5cc840fed8220473085f453dfa01de [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>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053023#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070024
Anirudh Ghayal253ce122011-08-09 19:32:57 +053025#if defined(CONFIG_HAS_EARLYSUSPEND)
26#include <linux/earlysuspend.h>
27/* Early-suspend level */
28#define MXT_SUSPEND_LEVEL 1
29#endif
30
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070031/* Family ID */
32#define MXT224_ID 0x80
33#define MXT1386_ID 0xA0
34
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070035/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080036#define MXT_VER_20 20
37#define MXT_VER_21 21
38#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070039
40/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080041#define MXT_APP_LOW 0x4a
42#define MXT_APP_HIGH 0x4b
43#define MXT_BOOT_LOW 0x24
44#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070045
46/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080047#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070048
49/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080050#define MXT_FAMILY_ID 0x00
51#define MXT_VARIANT_ID 0x01
52#define MXT_VERSION 0x02
53#define MXT_BUILD 0x03
54#define MXT_MATRIX_X_SIZE 0x04
55#define MXT_MATRIX_Y_SIZE 0x05
56#define MXT_OBJECT_NUM 0x06
57#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070058
Iiro Valkonen7686b102011-02-02 23:21:58 -080059#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070060
61/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080062#define MXT_DEBUG_DIAGNOSTIC 37
63#define MXT_GEN_MESSAGE 5
64#define MXT_GEN_COMMAND 6
65#define MXT_GEN_POWER 7
66#define MXT_GEN_ACQUIRE 8
67#define MXT_TOUCH_MULTI 9
68#define MXT_TOUCH_KEYARRAY 15
69#define MXT_TOUCH_PROXIMITY 23
70#define MXT_PROCI_GRIPFACE 20
71#define MXT_PROCG_NOISE 22
72#define MXT_PROCI_ONETOUCH 24
73#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070074#define MXT_PROCI_GRIP 40
75#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070076#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080077#define MXT_SPT_GPIOPWM 19
78#define MXT_SPT_SELFTEST 25
79#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070080#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070081#define MXT_SPT_DIGITIZER 43
82#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070083
Iiro Valkonen7686b102011-02-02 23:21:58 -080084/* MXT_GEN_COMMAND field */
85#define MXT_COMMAND_RESET 0
86#define MXT_COMMAND_BACKUPNV 1
87#define MXT_COMMAND_CALIBRATE 2
88#define MXT_COMMAND_REPORTALL 3
89#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070090
Iiro Valkonen7686b102011-02-02 23:21:58 -080091/* MXT_GEN_POWER field */
92#define MXT_POWER_IDLEACQINT 0
93#define MXT_POWER_ACTVACQINT 1
94#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070095
Iiro Valkonen7686b102011-02-02 23:21:58 -080096/* MXT_GEN_ACQUIRE field */
97#define MXT_ACQUIRE_CHRGTIME 0
98#define MXT_ACQUIRE_TCHDRIFT 2
99#define MXT_ACQUIRE_DRIFTST 3
100#define MXT_ACQUIRE_TCHAUTOCAL 4
101#define MXT_ACQUIRE_SYNC 5
102#define MXT_ACQUIRE_ATCHCALST 6
103#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700104
Iiro Valkonen7686b102011-02-02 23:21:58 -0800105/* MXT_TOUCH_MULTI field */
106#define MXT_TOUCH_CTRL 0
107#define MXT_TOUCH_XORIGIN 1
108#define MXT_TOUCH_YORIGIN 2
109#define MXT_TOUCH_XSIZE 3
110#define MXT_TOUCH_YSIZE 4
111#define MXT_TOUCH_BLEN 6
112#define MXT_TOUCH_TCHTHR 7
113#define MXT_TOUCH_TCHDI 8
114#define MXT_TOUCH_ORIENT 9
115#define MXT_TOUCH_MOVHYSTI 11
116#define MXT_TOUCH_MOVHYSTN 12
117#define MXT_TOUCH_NUMTOUCH 14
118#define MXT_TOUCH_MRGHYST 15
119#define MXT_TOUCH_MRGTHR 16
120#define MXT_TOUCH_AMPHYST 17
121#define MXT_TOUCH_XRANGE_LSB 18
122#define MXT_TOUCH_XRANGE_MSB 19
123#define MXT_TOUCH_YRANGE_LSB 20
124#define MXT_TOUCH_YRANGE_MSB 21
125#define MXT_TOUCH_XLOCLIP 22
126#define MXT_TOUCH_XHICLIP 23
127#define MXT_TOUCH_YLOCLIP 24
128#define MXT_TOUCH_YHICLIP 25
129#define MXT_TOUCH_XEDGECTRL 26
130#define MXT_TOUCH_XEDGEDIST 27
131#define MXT_TOUCH_YEDGECTRL 28
132#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700133#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700134
Iiro Valkonen7686b102011-02-02 23:21:58 -0800135/* MXT_PROCI_GRIPFACE field */
136#define MXT_GRIPFACE_CTRL 0
137#define MXT_GRIPFACE_XLOGRIP 1
138#define MXT_GRIPFACE_XHIGRIP 2
139#define MXT_GRIPFACE_YLOGRIP 3
140#define MXT_GRIPFACE_YHIGRIP 4
141#define MXT_GRIPFACE_MAXTCHS 5
142#define MXT_GRIPFACE_SZTHR1 7
143#define MXT_GRIPFACE_SZTHR2 8
144#define MXT_GRIPFACE_SHPTHR1 9
145#define MXT_GRIPFACE_SHPTHR2 10
146#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700147
Iiro Valkonen7686b102011-02-02 23:21:58 -0800148/* MXT_PROCI_NOISE field */
149#define MXT_NOISE_CTRL 0
150#define MXT_NOISE_OUTFLEN 1
151#define MXT_NOISE_GCAFUL_LSB 3
152#define MXT_NOISE_GCAFUL_MSB 4
153#define MXT_NOISE_GCAFLL_LSB 5
154#define MXT_NOISE_GCAFLL_MSB 6
155#define MXT_NOISE_ACTVGCAFVALID 7
156#define MXT_NOISE_NOISETHR 8
157#define MXT_NOISE_FREQHOPSCALE 10
158#define MXT_NOISE_FREQ0 11
159#define MXT_NOISE_FREQ1 12
160#define MXT_NOISE_FREQ2 13
161#define MXT_NOISE_FREQ3 14
162#define MXT_NOISE_FREQ4 15
163#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonen7686b102011-02-02 23:21:58 -0800165/* MXT_SPT_COMMSCONFIG */
166#define MXT_COMMS_CTRL 0
167#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700168
Iiro Valkonen7686b102011-02-02 23:21:58 -0800169/* MXT_SPT_CTECONFIG field */
170#define MXT_CTE_CTRL 0
171#define MXT_CTE_CMD 1
172#define MXT_CTE_MODE 2
173#define MXT_CTE_IDLEGCAFDEPTH 3
174#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700175#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700176
Iiro Valkonen7686b102011-02-02 23:21:58 -0800177#define MXT_VOLTAGE_DEFAULT 2700000
178#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700179
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530180#define MXT_VTG_MIN_UV 2700000
181#define MXT_VTG_MAX_UV 3300000
182#define MXT_ACTIVE_LOAD_UA 15000
183
184#define MXT_I2C_VTG_MIN_UV 1800000
185#define MXT_I2C_VTG_MAX_UV 1800000
186#define MXT_I2C_LOAD_UA 10000
187
Iiro Valkonen7686b102011-02-02 23:21:58 -0800188/* Define for MXT_GEN_COMMAND */
189#define MXT_BOOT_VALUE 0xa5
190#define MXT_BACKUP_VALUE 0x55
191#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700192#define MXT224_RESET_TIME 65 /* msec */
193#define MXT1386_RESET_TIME 200 /* msec */
194#define MXT_RESET_TIME 200 /* msec */
195#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700196
Iiro Valkonen7686b102011-02-02 23:21:58 -0800197#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700198
199/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_UNLOCK_CMD_MSB 0xaa
201#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700202
203/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800204#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
205#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
206#define MXT_FRAME_CRC_CHECK 0x02
207#define MXT_FRAME_CRC_FAIL 0x03
208#define MXT_FRAME_CRC_PASS 0x04
209#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
210#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700211
212/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800213#define MXT_SUPPRESS (1 << 1)
214#define MXT_AMP (1 << 2)
215#define MXT_VECTOR (1 << 3)
216#define MXT_MOVE (1 << 4)
217#define MXT_RELEASE (1 << 5)
218#define MXT_PRESS (1 << 6)
219#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700220
Joonyoung Shim910d8052011-04-12 23:14:38 -0700221/* Touch orient bits */
222#define MXT_XY_SWITCH (1 << 0)
223#define MXT_X_INVERT (1 << 1)
224#define MXT_Y_INVERT (1 << 2)
225
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700226/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800227#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700228
Iiro Valkonen7686b102011-02-02 23:21:58 -0800229#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700230
Iiro Valkonen7686b102011-02-02 23:21:58 -0800231struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700232 u8 family_id;
233 u8 variant_id;
234 u8 version;
235 u8 build;
236 u8 matrix_xsize;
237 u8 matrix_ysize;
238 u8 object_num;
239};
240
Iiro Valkonen7686b102011-02-02 23:21:58 -0800241struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700242 u8 type;
243 u16 start_address;
244 u8 size;
245 u8 instances;
246 u8 num_report_ids;
247
248 /* to map object and message */
249 u8 max_reportid;
250};
251
Iiro Valkonen7686b102011-02-02 23:21:58 -0800252struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700253 u8 reportid;
254 u8 message[7];
255 u8 checksum;
256};
257
Iiro Valkonen7686b102011-02-02 23:21:58 -0800258struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700259 int status;
260 int x;
261 int y;
262 int area;
263};
264
265/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800266struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700267 struct i2c_client *client;
268 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800269 const struct mxt_platform_data *pdata;
270 struct mxt_object *object_table;
271 struct mxt_info info;
272 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700273 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700274 unsigned int max_x;
275 unsigned int max_y;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530276 struct regulator *vcc;
277 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530278#if defined(CONFIG_HAS_EARLYSUSPEND)
279 struct early_suspend early_suspend;
280#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700281};
282
Iiro Valkonen7686b102011-02-02 23:21:58 -0800283static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700284{
285 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800286 case MXT_GEN_MESSAGE:
287 case MXT_GEN_COMMAND:
288 case MXT_GEN_POWER:
289 case MXT_GEN_ACQUIRE:
290 case MXT_TOUCH_MULTI:
291 case MXT_TOUCH_KEYARRAY:
292 case MXT_TOUCH_PROXIMITY:
293 case MXT_PROCI_GRIPFACE:
294 case MXT_PROCG_NOISE:
295 case MXT_PROCI_ONETOUCH:
296 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700297 case MXT_PROCI_GRIP:
298 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800299 case MXT_SPT_COMMSCONFIG:
300 case MXT_SPT_GPIOPWM:
301 case MXT_SPT_SELFTEST:
302 case MXT_SPT_CTECONFIG:
303 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530304 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700305 return true;
306 default:
307 return false;
308 }
309}
310
Iiro Valkonen7686b102011-02-02 23:21:58 -0800311static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700312{
313 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800314 case MXT_GEN_COMMAND:
315 case MXT_GEN_POWER:
316 case MXT_GEN_ACQUIRE:
317 case MXT_TOUCH_MULTI:
318 case MXT_TOUCH_KEYARRAY:
319 case MXT_TOUCH_PROXIMITY:
320 case MXT_PROCI_GRIPFACE:
321 case MXT_PROCG_NOISE:
322 case MXT_PROCI_ONETOUCH:
323 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700324 case MXT_PROCI_GRIP:
325 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800326 case MXT_SPT_GPIOPWM:
327 case MXT_SPT_SELFTEST:
328 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530329 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530330 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700331 return true;
332 default:
333 return false;
334 }
335}
336
Iiro Valkonen7686b102011-02-02 23:21:58 -0800337static void mxt_dump_message(struct device *dev,
338 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700339{
340 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
341 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
342 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
343 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
344 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
345 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
346 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
347 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
348 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
349}
350
Iiro Valkonen7686b102011-02-02 23:21:58 -0800351static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700352 unsigned int state)
353{
354 u8 val;
355
356recheck:
357 if (i2c_master_recv(client, &val, 1) != 1) {
358 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
359 return -EIO;
360 }
361
362 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800363 case MXT_WAITING_BOOTLOAD_CMD:
364 case MXT_WAITING_FRAME_DATA:
365 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700366 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800367 case MXT_FRAME_CRC_PASS:
368 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700369 goto recheck;
370 break;
371 default:
372 return -EINVAL;
373 }
374
375 if (val != state) {
376 dev_err(&client->dev, "Unvalid bootloader mode state\n");
377 return -EINVAL;
378 }
379
380 return 0;
381}
382
Iiro Valkonen7686b102011-02-02 23:21:58 -0800383static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700384{
385 u8 buf[2];
386
Iiro Valkonen7686b102011-02-02 23:21:58 -0800387 buf[0] = MXT_UNLOCK_CMD_LSB;
388 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700389
390 if (i2c_master_send(client, buf, 2) != 2) {
391 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
392 return -EIO;
393 }
394
395 return 0;
396}
397
Iiro Valkonen7686b102011-02-02 23:21:58 -0800398static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700399 const u8 *data, unsigned int frame_size)
400{
401 if (i2c_master_send(client, data, frame_size) != frame_size) {
402 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
403 return -EIO;
404 }
405
406 return 0;
407}
408
Iiro Valkonen7686b102011-02-02 23:21:58 -0800409static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700410 u16 reg, u16 len, void *val)
411{
412 struct i2c_msg xfer[2];
413 u8 buf[2];
414
415 buf[0] = reg & 0xff;
416 buf[1] = (reg >> 8) & 0xff;
417
418 /* Write register */
419 xfer[0].addr = client->addr;
420 xfer[0].flags = 0;
421 xfer[0].len = 2;
422 xfer[0].buf = buf;
423
424 /* Read data */
425 xfer[1].addr = client->addr;
426 xfer[1].flags = I2C_M_RD;
427 xfer[1].len = len;
428 xfer[1].buf = val;
429
430 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
431 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
432 return -EIO;
433 }
434
435 return 0;
436}
437
Iiro Valkonen7686b102011-02-02 23:21:58 -0800438static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700439{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800440 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700441}
442
Iiro Valkonen7686b102011-02-02 23:21:58 -0800443static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700444{
445 u8 buf[3];
446
447 buf[0] = reg & 0xff;
448 buf[1] = (reg >> 8) & 0xff;
449 buf[2] = val;
450
451 if (i2c_master_send(client, buf, 3) != 3) {
452 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
453 return -EIO;
454 }
455
456 return 0;
457}
458
Iiro Valkonen7686b102011-02-02 23:21:58 -0800459static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700460 u16 reg, u8 *object_buf)
461{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800462 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700463 object_buf);
464}
465
Iiro Valkonen7686b102011-02-02 23:21:58 -0800466static struct mxt_object *
467mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700468{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800469 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700470 int i;
471
472 for (i = 0; i < data->info.object_num; i++) {
473 object = data->object_table + i;
474 if (object->type == type)
475 return object;
476 }
477
478 dev_err(&data->client->dev, "Invalid object type\n");
479 return NULL;
480}
481
Iiro Valkonen7686b102011-02-02 23:21:58 -0800482static int mxt_read_message(struct mxt_data *data,
483 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700484{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800485 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700486 u16 reg;
487
Iiro Valkonen7686b102011-02-02 23:21:58 -0800488 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700489 if (!object)
490 return -EINVAL;
491
492 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493 return __mxt_read_reg(data->client, reg,
494 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495}
496
Iiro Valkonen7686b102011-02-02 23:21:58 -0800497static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498 u8 type, u8 offset, u8 *val)
499{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800500 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700501 u16 reg;
502
Iiro Valkonen7686b102011-02-02 23:21:58 -0800503 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504 if (!object)
505 return -EINVAL;
506
507 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700509}
510
Iiro Valkonen7686b102011-02-02 23:21:58 -0800511static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700512 u8 type, u8 offset, u8 val)
513{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800514 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700515 u16 reg;
516
Iiro Valkonen7686b102011-02-02 23:21:58 -0800517 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700518 if (!object)
519 return -EINVAL;
520
521 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800522 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700523}
524
Iiro Valkonen7686b102011-02-02 23:21:58 -0800525static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700526{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528 struct input_dev *input_dev = data->input_dev;
529 int status = finger[single_id].status;
530 int finger_num = 0;
531 int id;
532
Iiro Valkonen7686b102011-02-02 23:21:58 -0800533 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700534 if (!finger[id].status)
535 continue;
536
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700537 input_mt_slot(input_dev, id);
538 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
539 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700540
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700541 if (finger[id].status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542 finger_num++;
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700543 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
544 finger[id].area);
545 input_report_abs(input_dev, ABS_MT_POSITION_X,
546 finger[id].x);
547 input_report_abs(input_dev, ABS_MT_POSITION_Y,
548 finger[id].y);
549 } else {
550 finger[id].status = 0;
551 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700552 }
553
554 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
555
Iiro Valkonen7686b102011-02-02 23:21:58 -0800556 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557 input_report_abs(input_dev, ABS_X, finger[single_id].x);
558 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
559 }
560
561 input_sync(input_dev);
562}
563
Iiro Valkonen7686b102011-02-02 23:21:58 -0800564static void mxt_input_touchevent(struct mxt_data *data,
565 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700566{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800567 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700568 struct device *dev = &data->client->dev;
569 u8 status = message->message[0];
570 int x;
571 int y;
572 int area;
573
574 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800575 if (!(status & MXT_DETECT)) {
576 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 dev_dbg(dev, "[%d] released\n", id);
578
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579 finger[id].status = MXT_RELEASE;
580 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700581 }
582 return;
583 }
584
585 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800586 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700587 return;
588
Joonyoung Shim910d8052011-04-12 23:14:38 -0700589 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
590 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
591 if (data->max_x < 1024)
592 x = x >> 2;
593 if (data->max_y < 1024)
594 y = y >> 2;
595
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700596 area = message->message[4];
597
598 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800599 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600 x, y, area);
601
Iiro Valkonen7686b102011-02-02 23:21:58 -0800602 finger[id].status = status & MXT_MOVE ?
603 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700604 finger[id].x = x;
605 finger[id].y = y;
606 finger[id].area = area;
607
Iiro Valkonen7686b102011-02-02 23:21:58 -0800608 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609}
610
Iiro Valkonen7686b102011-02-02 23:21:58 -0800611static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700612{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800613 struct mxt_data *data = dev_id;
614 struct mxt_message message;
615 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616 struct device *dev = &data->client->dev;
617 int id;
618 u8 reportid;
619 u8 max_reportid;
620 u8 min_reportid;
621
622 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624 dev_err(dev, "Failed to read message\n");
625 goto end;
626 }
627
628 reportid = message.reportid;
629
Iiro Valkonen7686b102011-02-02 23:21:58 -0800630 /* whether reportid is thing of MXT_TOUCH_MULTI */
631 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700632 if (!object)
633 goto end;
634
635 max_reportid = object->max_reportid;
636 min_reportid = max_reportid - object->num_report_ids + 1;
637 id = reportid - min_reportid;
638
639 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800640 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800642 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643 } while (reportid != 0xff);
644
645end:
646 return IRQ_HANDLED;
647}
648
Iiro Valkonen7686b102011-02-02 23:21:58 -0800649static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700650{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800651 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800652 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653 struct device *dev = &data->client->dev;
654 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800655 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656
Iiro Valkonen71749f52011-02-15 13:36:52 -0800657 if (!pdata->config) {
658 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
659 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660 }
661
662 for (i = 0; i < data->info.object_num; i++) {
663 object = data->object_table + i;
664
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 continue;
667
Iiro Valkonen71749f52011-02-15 13:36:52 -0800668 for (j = 0; j < object->size + 1; j++) {
669 config_offset = index + j;
670 if (config_offset > pdata->config_length) {
671 dev_err(dev, "Not enough config data!\n");
672 return -EINVAL;
673 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800674 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800675 pdata->config[config_offset]);
676 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677 index += object->size + 1;
678 }
679
680 return 0;
681}
682
Iiro Valkonen7686b102011-02-02 23:21:58 -0800683static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684{
685 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800686 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687 int count = 10;
688 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689
690 /* Read dummy message to make high CHG pin */
691 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800692 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700693 if (error)
694 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800695 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696
697 if (!count) {
698 dev_err(dev, "CHG pin isn't cleared\n");
699 return -EBUSY;
700 }
701
702 return 0;
703}
704
Iiro Valkonen7686b102011-02-02 23:21:58 -0800705static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700706{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800707 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708 u8 voltage;
709
710 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800711 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700712 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800713 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714 pdata->y_line);
715
716 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800717 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718 pdata->orient);
719
720 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800721 mxt_write_object(data, MXT_TOUCH_MULTI,
722 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700723
724 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800725 mxt_write_object(data, MXT_TOUCH_MULTI,
726 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727
728 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729 mxt_write_object(data, MXT_TOUCH_MULTI,
730 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
731 mxt_write_object(data, MXT_TOUCH_MULTI,
732 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
733 mxt_write_object(data, MXT_TOUCH_MULTI,
734 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
735 mxt_write_object(data, MXT_TOUCH_MULTI,
736 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700737
738 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700739 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800740 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
741 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
742 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743 voltage = 0xff - voltage + 1;
744 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800745 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
746 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747
Iiro Valkonen7686b102011-02-02 23:21:58 -0800748 mxt_write_object(data, MXT_SPT_CTECONFIG,
749 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700750 }
751}
752
Iiro Valkonen7686b102011-02-02 23:21:58 -0800753static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700754{
755 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800756 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700757 int error;
758 u8 val;
759
Iiro Valkonen7686b102011-02-02 23:21:58 -0800760 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761 if (error)
762 return error;
763 info->family_id = val;
764
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766 if (error)
767 return error;
768 info->variant_id = val;
769
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771 if (error)
772 return error;
773 info->version = val;
774
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700776 if (error)
777 return error;
778 info->build = val;
779
Iiro Valkonen7686b102011-02-02 23:21:58 -0800780 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700781 if (error)
782 return error;
783 info->object_num = val;
784
785 return 0;
786}
787
Iiro Valkonen7686b102011-02-02 23:21:58 -0800788static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789{
790 int error;
791 int i;
792 u16 reg;
793 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800794 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700795
796 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800797 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798
Iiro Valkonen7686b102011-02-02 23:21:58 -0800799 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
800 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801 if (error)
802 return error;
803
804 object->type = buf[0];
805 object->start_address = (buf[2] << 8) | buf[1];
806 object->size = buf[3];
807 object->instances = buf[4];
808 object->num_report_ids = buf[5];
809
810 if (object->num_report_ids) {
811 reportid += object->num_report_ids *
812 (object->instances + 1);
813 object->max_reportid = reportid;
814 }
815 }
816
817 return 0;
818}
819
Iiro Valkonen7686b102011-02-02 23:21:58 -0800820static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700821{
822 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800823 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700825 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700826 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700827 u8 command_register;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700828
Iiro Valkonen7686b102011-02-02 23:21:58 -0800829 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700830 if (error)
831 return error;
832
833 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800834 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700835 GFP_KERNEL);
836 if (!data->object_table) {
837 dev_err(&client->dev, "Failed to allocate memory\n");
838 return -ENOMEM;
839 }
840
841 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800842 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700843 if (error)
844 return error;
845
846 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800847 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700848 if (error)
849 return error;
850
Iiro Valkonen7686b102011-02-02 23:21:58 -0800851 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700852
853 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800854 mxt_write_object(data, MXT_GEN_COMMAND,
855 MXT_COMMAND_BACKUPNV,
856 MXT_BACKUP_VALUE);
857 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700858 do {
859 error = mxt_read_object(data, MXT_GEN_COMMAND,
860 MXT_COMMAND_BACKUPNV,
861 &command_register);
862 if (error)
863 return error;
864 msleep(2);
865 } while ((command_register != 0) && (timeout_counter++ <= 100));
866 if (timeout_counter >= 100) {
867 dev_err(&client->dev, "No response after backup!\n");
868 return -EIO;
869 }
870
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700871
872 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800873 mxt_write_object(data, MXT_GEN_COMMAND,
874 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700875
876 if (data->pdata->read_chg == NULL) {
877 msleep(MXT_RESET_NOCHGREAD);
878 } else {
879 switch (info->family_id) {
880 case MXT224_ID:
881 msleep(MXT224_RESET_TIME);
882 break;
883 case MXT1386_ID:
884 msleep(MXT1386_RESET_TIME);
885 break;
886 default:
887 msleep(MXT_RESET_TIME);
888 }
889 timeout_counter = 0;
890 while ((timeout_counter++ <= 100) && data->pdata->read_chg())
891 msleep(2);
892 if (timeout_counter >= 100) {
893 dev_err(&client->dev, "No response after reset!\n");
894 return -EIO;
895 }
896 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700897
898 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800899 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700900 if (error)
901 return error;
902 info->matrix_xsize = val;
903
Iiro Valkonen7686b102011-02-02 23:21:58 -0800904 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700905 if (error)
906 return error;
907 info->matrix_ysize = val;
908
909 dev_info(&client->dev,
910 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
911 info->family_id, info->variant_id, info->version,
912 info->build);
913
914 dev_info(&client->dev,
915 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
916 info->matrix_xsize, info->matrix_ysize,
917 info->object_num);
918
919 return 0;
920}
921
Joonyoung Shim910d8052011-04-12 23:14:38 -0700922static void mxt_calc_resolution(struct mxt_data *data)
923{
924 unsigned int max_x = data->pdata->x_size - 1;
925 unsigned int max_y = data->pdata->y_size - 1;
926
927 if (data->pdata->orient & MXT_XY_SWITCH) {
928 data->max_x = max_y;
929 data->max_y = max_x;
930 } else {
931 data->max_x = max_x;
932 data->max_y = max_y;
933 }
934}
935
Iiro Valkonen7686b102011-02-02 23:21:58 -0800936static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937 struct device_attribute *attr, char *buf)
938{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800939 struct mxt_data *data = dev_get_drvdata(dev);
940 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700941 int count = 0;
942 int i, j;
943 int error;
944 u8 val;
945
946 for (i = 0; i < data->info.object_num; i++) {
947 object = data->object_table + i;
948
949 count += sprintf(buf + count,
950 "Object Table Element %d(Type %d)\n",
951 i + 1, object->type);
952
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954 count += sprintf(buf + count, "\n");
955 continue;
956 }
957
958 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800959 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700960 object->type, j, &val);
961 if (error)
962 return error;
963
964 count += sprintf(buf + count,
965 " Byte %d: 0x%x (%d)\n", j, val, val);
966 }
967
968 count += sprintf(buf + count, "\n");
969 }
970
971 return count;
972}
973
Iiro Valkonen7686b102011-02-02 23:21:58 -0800974static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700975{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800976 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700977 struct i2c_client *client = data->client;
978 const struct firmware *fw = NULL;
979 unsigned int frame_size;
980 unsigned int pos = 0;
981 int ret;
982
983 ret = request_firmware(&fw, fn, dev);
984 if (ret) {
985 dev_err(dev, "Unable to open firmware %s\n", fn);
986 return ret;
987 }
988
989 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800990 mxt_write_object(data, MXT_GEN_COMMAND,
991 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
992 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993
994 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 if (client->addr == MXT_APP_LOW)
996 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800998 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700999
Iiro Valkonen7686b102011-02-02 23:21:58 -08001000 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001 if (ret)
1002 goto out;
1003
1004 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001005 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006
1007 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001008 ret = mxt_check_bootloader(client,
1009 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010 if (ret)
1011 goto out;
1012
1013 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1014
1015 /* We should add 2 at frame size as the the firmware data is not
1016 * included the CRC bytes.
1017 */
1018 frame_size += 2;
1019
1020 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001021 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022
Iiro Valkonen7686b102011-02-02 23:21:58 -08001023 ret = mxt_check_bootloader(client,
1024 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025 if (ret)
1026 goto out;
1027
1028 pos += frame_size;
1029
1030 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1031 }
1032
1033out:
1034 release_firmware(fw);
1035
1036 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001037 if (client->addr == MXT_BOOT_LOW)
1038 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001039 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001040 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001041
1042 return ret;
1043}
1044
Iiro Valkonen7686b102011-02-02 23:21:58 -08001045static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001046 struct device_attribute *attr,
1047 const char *buf, size_t count)
1048{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050 int error;
1051
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001052 disable_irq(data->irq);
1053
Iiro Valkonen7686b102011-02-02 23:21:58 -08001054 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001055 if (error) {
1056 dev_err(dev, "The firmware update failed(%d)\n", error);
1057 count = error;
1058 } else {
1059 dev_dbg(dev, "The firmware update succeeded\n");
1060
1061 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001062 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063
1064 kfree(data->object_table);
1065 data->object_table = NULL;
1066
Iiro Valkonen7686b102011-02-02 23:21:58 -08001067 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001068 }
1069
1070 enable_irq(data->irq);
1071
Iiro Valkonen08960a02011-04-12 23:16:40 -07001072 error = mxt_make_highchg(data);
1073 if (error)
1074 return error;
1075
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001076 return count;
1077}
1078
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1080static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001081
Iiro Valkonen7686b102011-02-02 23:21:58 -08001082static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083 &dev_attr_object.attr,
1084 &dev_attr_update_fw.attr,
1085 NULL
1086};
1087
Iiro Valkonen7686b102011-02-02 23:21:58 -08001088static const struct attribute_group mxt_attr_group = {
1089 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001090};
1091
Iiro Valkonen7686b102011-02-02 23:21:58 -08001092static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093{
1094 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001095 mxt_write_object(data,
1096 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097}
1098
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100{
1101 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001102 mxt_write_object(data,
1103 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104}
1105
Iiro Valkonen7686b102011-02-02 23:21:58 -08001106static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001107{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001108 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109
Iiro Valkonen7686b102011-02-02 23:21:58 -08001110 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111
1112 return 0;
1113}
1114
Iiro Valkonen7686b102011-02-02 23:21:58 -08001115static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001116{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001117 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001118
Iiro Valkonen7686b102011-02-02 23:21:58 -08001119 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001120}
1121
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301122static int mxt_power_on(struct mxt_data *data, bool on)
1123{
1124 int rc;
1125
1126 if (on == false)
1127 goto power_off;
1128
1129 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1130 if (rc < 0) {
1131 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1132 rc);
1133 return rc;
1134 }
1135
1136 rc = regulator_enable(data->vcc);
1137 if (rc) {
1138 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1139 rc);
1140 goto error_reg_en_vcc;
1141 }
1142
1143 if (data->pdata->i2c_pull_up) {
1144 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1145 if (rc < 0) {
1146 dev_err(&data->client->dev,
1147 "Regulator set_opt failed rc=%d\n", rc);
1148 goto error_reg_opt_i2c;
1149 }
1150
1151 rc = regulator_enable(data->vcc_i2c);
1152 if (rc) {
1153 dev_err(&data->client->dev,
1154 "Regulator enable failed rc=%d\n", rc);
1155 goto error_reg_en_vcc_i2c;
1156 }
1157 }
1158
1159 msleep(50);
1160
1161 return 0;
1162
1163error_reg_en_vcc_i2c:
1164 if (data->pdata->i2c_pull_up)
1165 regulator_set_optimum_mode(data->vcc_i2c, 0);
1166error_reg_opt_i2c:
1167 regulator_disable(data->vcc);
1168error_reg_en_vcc:
1169 regulator_set_optimum_mode(data->vcc, 0);
1170 return rc;
1171
1172power_off:
1173 regulator_set_optimum_mode(data->vcc, 0);
1174 regulator_disable(data->vcc);
1175 if (data->pdata->i2c_pull_up) {
1176 regulator_set_optimum_mode(data->vcc_i2c, 0);
1177 regulator_disable(data->vcc_i2c);
1178 }
1179 msleep(50);
1180 return 0;
1181}
1182
1183static int mxt_regulator_configure(struct mxt_data *data, bool on)
1184{
1185 int rc;
1186
1187 if (on == false)
1188 goto hw_shutdown;
1189
1190 data->vcc = regulator_get(&data->client->dev, "vdd");
1191 if (IS_ERR(data->vcc)) {
1192 rc = PTR_ERR(data->vcc);
1193 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1194 rc);
1195 return rc;
1196 }
1197
1198 if (regulator_count_voltages(data->vcc) > 0) {
1199 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1200 MXT_VTG_MAX_UV);
1201 if (rc) {
1202 dev_err(&data->client->dev,
1203 "regulator set_vtg failed rc=%d\n", rc);
1204 goto error_set_vtg_vcc;
1205 }
1206 }
1207
1208 if (data->pdata->i2c_pull_up) {
1209 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1210 if (IS_ERR(data->vcc_i2c)) {
1211 rc = PTR_ERR(data->vcc_i2c);
1212 dev_err(&data->client->dev,
1213 "Regulator get failed rc=%d\n", rc);
1214 goto error_get_vtg_i2c;
1215 }
1216 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1217 rc = regulator_set_voltage(data->vcc_i2c,
1218 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1219 if (rc) {
1220 dev_err(&data->client->dev,
1221 "regulator set_vtg failed rc=%d\n", rc);
1222 goto error_set_vtg_i2c;
1223 }
1224 }
1225 }
1226
1227 return 0;
1228
1229error_set_vtg_i2c:
1230 regulator_put(data->vcc_i2c);
1231error_get_vtg_i2c:
1232 if (regulator_count_voltages(data->vcc) > 0)
1233 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1234error_set_vtg_vcc:
1235 regulator_put(data->vcc);
1236 return rc;
1237
1238hw_shutdown:
1239 if (regulator_count_voltages(data->vcc) > 0)
1240 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1241 regulator_put(data->vcc);
1242 if (data->pdata->i2c_pull_up) {
1243 if (regulator_count_voltages(data->vcc_i2c) > 0)
1244 regulator_set_voltage(data->vcc_i2c, 0,
1245 MXT_I2C_VTG_MAX_UV);
1246 regulator_put(data->vcc_i2c);
1247 }
1248 return 0;
1249}
1250
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301251#ifdef CONFIG_PM
1252static int mxt_suspend(struct device *dev)
1253{
1254 struct i2c_client *client = to_i2c_client(dev);
1255 struct mxt_data *data = i2c_get_clientdata(client);
1256 struct input_dev *input_dev = data->input_dev;
1257
1258 mutex_lock(&input_dev->mutex);
1259
1260 if (input_dev->users)
1261 mxt_stop(data);
1262
1263 mutex_unlock(&input_dev->mutex);
1264
1265 return 0;
1266}
1267
1268static int mxt_resume(struct device *dev)
1269{
1270 struct i2c_client *client = to_i2c_client(dev);
1271 struct mxt_data *data = i2c_get_clientdata(client);
1272 struct input_dev *input_dev = data->input_dev;
1273
1274 /* Soft reset */
1275 mxt_write_object(data, MXT_GEN_COMMAND,
1276 MXT_COMMAND_RESET, 1);
1277
1278 msleep(MXT_RESET_TIME);
1279
1280 mutex_lock(&input_dev->mutex);
1281
1282 if (input_dev->users)
1283 mxt_start(data);
1284
1285 mutex_unlock(&input_dev->mutex);
1286
1287 return 0;
1288}
1289
1290#if defined(CONFIG_HAS_EARLYSUSPEND)
1291static void mxt_early_suspend(struct early_suspend *h)
1292{
1293 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1294
1295 mxt_suspend(&data->client->dev);
1296}
1297
1298static void mxt_late_resume(struct early_suspend *h)
1299{
1300 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1301
1302 mxt_resume(&data->client->dev);
1303}
1304#endif
1305
1306static const struct dev_pm_ops mxt_pm_ops = {
1307#ifndef CONFIG_HAS_EARLYSUSPEND
1308 .suspend = mxt_suspend,
1309 .resume = mxt_resume,
1310#endif
1311};
1312#endif
1313
Iiro Valkonen7686b102011-02-02 23:21:58 -08001314static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001315 const struct i2c_device_id *id)
1316{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001317 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001318 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001319 struct input_dev *input_dev;
1320 int error;
1321
Iiro Valkonen919ed892011-02-15 13:36:52 -08001322 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001323 return -EINVAL;
1324
Iiro Valkonen7686b102011-02-02 23:21:58 -08001325 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001326 input_dev = input_allocate_device();
1327 if (!data || !input_dev) {
1328 dev_err(&client->dev, "Failed to allocate memory\n");
1329 error = -ENOMEM;
1330 goto err_free_mem;
1331 }
1332
Iiro Valkonen7686b102011-02-02 23:21:58 -08001333 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001334 input_dev->id.bustype = BUS_I2C;
1335 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001336 input_dev->open = mxt_input_open;
1337 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001338
Joonyoung Shim910d8052011-04-12 23:14:38 -07001339 data->client = client;
1340 data->input_dev = input_dev;
1341 data->pdata = pdata;
1342 data->irq = client->irq;
1343
1344 mxt_calc_resolution(data);
1345
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001346 __set_bit(EV_ABS, input_dev->evbit);
1347 __set_bit(EV_KEY, input_dev->evbit);
1348 __set_bit(BTN_TOUCH, input_dev->keybit);
1349
1350 /* For single touch */
1351 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001352 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001353 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001354 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001355
1356 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001357 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001359 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001360 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001361 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001362 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001363 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001364
1365 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001366 i2c_set_clientdata(client, data);
1367
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301368 if (pdata->init_hw)
1369 error = pdata->init_hw(true);
1370 else
1371 error = mxt_regulator_configure(data, true);
1372 if (error) {
1373 dev_err(&client->dev, "Failed to intialize hardware\n");
1374 goto err_free_object;
1375 }
1376
1377 if (pdata->power_on)
1378 error = pdata->power_on(true);
1379 else
1380 error = mxt_power_on(data, true);
1381 if (error) {
1382 dev_err(&client->dev, "Failed to power on hardware\n");
1383 goto err_regulator_on;
1384 }
1385
Iiro Valkonen7686b102011-02-02 23:21:58 -08001386 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001387 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301388 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389
Iiro Valkonen7686b102011-02-02 23:21:58 -08001390 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001391 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392 if (error) {
1393 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301394 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001395 }
1396
Iiro Valkonen08960a02011-04-12 23:16:40 -07001397 error = mxt_make_highchg(data);
1398 if (error)
1399 goto err_free_irq;
1400
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001401 error = input_register_device(input_dev);
1402 if (error)
1403 goto err_free_irq;
1404
Iiro Valkonen7686b102011-02-02 23:21:58 -08001405 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001406 if (error)
1407 goto err_unregister_device;
1408
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301409#if defined(CONFIG_HAS_EARLYSUSPEND)
1410 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1411 MXT_SUSPEND_LEVEL;
1412 data->early_suspend.suspend = mxt_early_suspend;
1413 data->early_suspend.resume = mxt_late_resume;
1414 register_early_suspend(&data->early_suspend);
1415#endif
1416
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001417 return 0;
1418
1419err_unregister_device:
1420 input_unregister_device(input_dev);
1421 input_dev = NULL;
1422err_free_irq:
1423 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301424err_power_on:
1425 if (pdata->power_on)
1426 pdata->power_on(false);
1427 else
1428 mxt_power_on(data, false);
1429err_regulator_on:
1430 if (pdata->init_hw)
1431 pdata->init_hw(false);
1432 else
1433 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001434err_free_object:
1435 kfree(data->object_table);
1436err_free_mem:
1437 input_free_device(input_dev);
1438 kfree(data);
1439 return error;
1440}
1441
Iiro Valkonen7686b102011-02-02 23:21:58 -08001442static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001443{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001444 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001445
Iiro Valkonen7686b102011-02-02 23:21:58 -08001446 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001447 free_irq(data->irq, data);
1448 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301449#if defined(CONFIG_HAS_EARLYSUSPEND)
1450 unregister_early_suspend(&data->early_suspend);
1451#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301452
1453 if (data->pdata->power_on)
1454 data->pdata->power_on(false);
1455 else
1456 mxt_power_on(data, false);
1457
1458 if (data->pdata->init_hw)
1459 data->pdata->init_hw(false);
1460 else
1461 mxt_regulator_configure(data, false);
1462
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001463 kfree(data->object_table);
1464 kfree(data);
1465
1466 return 0;
1467}
1468
Iiro Valkonen7686b102011-02-02 23:21:58 -08001469static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001471 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001472 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001473 { }
1474};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001475MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001476
Iiro Valkonen7686b102011-02-02 23:21:58 -08001477static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001478 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001479 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001480 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001481#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001482 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001483#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001484 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001485 .probe = mxt_probe,
1486 .remove = __devexit_p(mxt_remove),
1487 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001488};
1489
Iiro Valkonen7686b102011-02-02 23:21:58 -08001490static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001491{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001492 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001493}
1494
Iiro Valkonen7686b102011-02-02 23:21:58 -08001495static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001496{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001497 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001498}
1499
Iiro Valkonen7686b102011-02-02 23:21:58 -08001500module_init(mxt_init);
1501module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001502
1503/* Module information */
1504MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001505MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001506MODULE_LICENSE("GPL");