blob: 5f3cd0a89b98a68e424c89ad19e3e2adf9883c47 [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 */
Amy Maloche7e447432011-09-14 11:36:30 -0700193#define MXT1386_RESET_TIME 250 /* msec */
194#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700195#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
Amy Maloche7e447432011-09-14 11:36:30 -0700820static void mxt_reset_delay(struct mxt_data *data)
821{
822 struct mxt_info *info = &data->info;
823
824 switch (info->family_id) {
825 case MXT224_ID:
826 msleep(MXT224_RESET_TIME);
827 break;
828 case MXT1386_ID:
829 msleep(MXT1386_RESET_TIME);
830 break;
831 default:
832 msleep(MXT_RESET_TIME);
833 }
834}
835
Iiro Valkonen7686b102011-02-02 23:21:58 -0800836static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700837{
838 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800839 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700840 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700841 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700843 u8 command_register;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700844
Iiro Valkonen7686b102011-02-02 23:21:58 -0800845 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700846 if (error)
847 return error;
848
849 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800850 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700851 GFP_KERNEL);
852 if (!data->object_table) {
853 dev_err(&client->dev, "Failed to allocate memory\n");
854 return -ENOMEM;
855 }
856
857 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700859 if (error)
860 return error;
861
862 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800863 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700864 if (error)
865 return error;
866
Iiro Valkonen7686b102011-02-02 23:21:58 -0800867 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700868
869 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800870 mxt_write_object(data, MXT_GEN_COMMAND,
871 MXT_COMMAND_BACKUPNV,
872 MXT_BACKUP_VALUE);
873 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700874 do {
875 error = mxt_read_object(data, MXT_GEN_COMMAND,
876 MXT_COMMAND_BACKUPNV,
877 &command_register);
878 if (error)
879 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700880 usleep_range(1000, 2000);
881 } while ((command_register != 0) && (++timeout_counter <= 100));
882 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700883 dev_err(&client->dev, "No response after backup!\n");
884 return -EIO;
885 }
886
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700887
888 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800889 mxt_write_object(data, MXT_GEN_COMMAND,
890 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700891
Amy Maloche7e447432011-09-14 11:36:30 -0700892 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893
894 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800895 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700896 if (error)
897 return error;
898 info->matrix_xsize = val;
899
Iiro Valkonen7686b102011-02-02 23:21:58 -0800900 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901 if (error)
902 return error;
903 info->matrix_ysize = val;
904
905 dev_info(&client->dev,
906 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
907 info->family_id, info->variant_id, info->version,
908 info->build);
909
910 dev_info(&client->dev,
911 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
912 info->matrix_xsize, info->matrix_ysize,
913 info->object_num);
914
915 return 0;
916}
917
Joonyoung Shim910d8052011-04-12 23:14:38 -0700918static void mxt_calc_resolution(struct mxt_data *data)
919{
920 unsigned int max_x = data->pdata->x_size - 1;
921 unsigned int max_y = data->pdata->y_size - 1;
922
923 if (data->pdata->orient & MXT_XY_SWITCH) {
924 data->max_x = max_y;
925 data->max_y = max_x;
926 } else {
927 data->max_x = max_x;
928 data->max_y = max_y;
929 }
930}
931
Iiro Valkonen7686b102011-02-02 23:21:58 -0800932static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700933 struct device_attribute *attr, char *buf)
934{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800935 struct mxt_data *data = dev_get_drvdata(dev);
936 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937 int count = 0;
938 int i, j;
939 int error;
940 u8 val;
941
942 for (i = 0; i < data->info.object_num; i++) {
943 object = data->object_table + i;
944
945 count += sprintf(buf + count,
946 "Object Table Element %d(Type %d)\n",
947 i + 1, object->type);
948
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950 count += sprintf(buf + count, "\n");
951 continue;
952 }
953
954 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956 object->type, j, &val);
957 if (error)
958 return error;
959
960 count += sprintf(buf + count,
961 " Byte %d: 0x%x (%d)\n", j, val, val);
962 }
963
964 count += sprintf(buf + count, "\n");
965 }
966
967 return count;
968}
969
Iiro Valkonen7686b102011-02-02 23:21:58 -0800970static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700971{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800972 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700973 struct i2c_client *client = data->client;
974 const struct firmware *fw = NULL;
975 unsigned int frame_size;
976 unsigned int pos = 0;
977 int ret;
978
979 ret = request_firmware(&fw, fn, dev);
980 if (ret) {
981 dev_err(dev, "Unable to open firmware %s\n", fn);
982 return ret;
983 }
984
985 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800986 mxt_write_object(data, MXT_GEN_COMMAND,
987 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700988
989 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700990
991 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992 if (client->addr == MXT_APP_LOW)
993 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700994 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996
Iiro Valkonen7686b102011-02-02 23:21:58 -0800997 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998 if (ret)
999 goto out;
1000
1001 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003
1004 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001005 ret = mxt_check_bootloader(client,
1006 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 if (ret)
1008 goto out;
1009
1010 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1011
1012 /* We should add 2 at frame size as the the firmware data is not
1013 * included the CRC bytes.
1014 */
1015 frame_size += 2;
1016
1017 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001018 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001019
Iiro Valkonen7686b102011-02-02 23:21:58 -08001020 ret = mxt_check_bootloader(client,
1021 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022 if (ret)
1023 goto out;
1024
1025 pos += frame_size;
1026
1027 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1028 }
1029
1030out:
1031 release_firmware(fw);
1032
1033 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001034 if (client->addr == MXT_BOOT_LOW)
1035 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001036 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001037 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038
1039 return ret;
1040}
1041
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043 struct device_attribute *attr,
1044 const char *buf, size_t count)
1045{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001046 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001047 int error;
1048
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001049 disable_irq(data->irq);
1050
Iiro Valkonen7686b102011-02-02 23:21:58 -08001051 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001052 if (error) {
1053 dev_err(dev, "The firmware update failed(%d)\n", error);
1054 count = error;
1055 } else {
1056 dev_dbg(dev, "The firmware update succeeded\n");
1057
1058 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001059 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001060
1061 kfree(data->object_table);
1062 data->object_table = NULL;
1063
Iiro Valkonen7686b102011-02-02 23:21:58 -08001064 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065 }
1066
1067 enable_irq(data->irq);
1068
Iiro Valkonen08960a02011-04-12 23:16:40 -07001069 error = mxt_make_highchg(data);
1070 if (error)
1071 return error;
1072
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001073 return count;
1074}
1075
Iiro Valkonen7686b102011-02-02 23:21:58 -08001076static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1077static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080 &dev_attr_object.attr,
1081 &dev_attr_update_fw.attr,
1082 NULL
1083};
1084
Iiro Valkonen7686b102011-02-02 23:21:58 -08001085static const struct attribute_group mxt_attr_group = {
1086 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001087};
1088
Iiro Valkonen7686b102011-02-02 23:21:58 -08001089static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001090{
1091 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001092 mxt_write_object(data,
1093 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001094}
1095
Iiro Valkonen7686b102011-02-02 23:21:58 -08001096static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097{
1098 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099 mxt_write_object(data,
1100 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001101}
1102
Iiro Valkonen7686b102011-02-02 23:21:58 -08001103static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001105 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001106
Iiro Valkonen7686b102011-02-02 23:21:58 -08001107 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001108
1109 return 0;
1110}
1111
Iiro Valkonen7686b102011-02-02 23:21:58 -08001112static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001113{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001114 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001115
Iiro Valkonen7686b102011-02-02 23:21:58 -08001116 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001117}
1118
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301119static int mxt_power_on(struct mxt_data *data, bool on)
1120{
1121 int rc;
1122
1123 if (on == false)
1124 goto power_off;
1125
1126 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1127 if (rc < 0) {
1128 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1129 rc);
1130 return rc;
1131 }
1132
1133 rc = regulator_enable(data->vcc);
1134 if (rc) {
1135 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1136 rc);
1137 goto error_reg_en_vcc;
1138 }
1139
1140 if (data->pdata->i2c_pull_up) {
1141 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1142 if (rc < 0) {
1143 dev_err(&data->client->dev,
1144 "Regulator set_opt failed rc=%d\n", rc);
1145 goto error_reg_opt_i2c;
1146 }
1147
1148 rc = regulator_enable(data->vcc_i2c);
1149 if (rc) {
1150 dev_err(&data->client->dev,
1151 "Regulator enable failed rc=%d\n", rc);
1152 goto error_reg_en_vcc_i2c;
1153 }
1154 }
1155
Amy Maloche7e447432011-09-14 11:36:30 -07001156 msleep(100);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301157
1158 return 0;
1159
1160error_reg_en_vcc_i2c:
1161 if (data->pdata->i2c_pull_up)
1162 regulator_set_optimum_mode(data->vcc_i2c, 0);
1163error_reg_opt_i2c:
1164 regulator_disable(data->vcc);
1165error_reg_en_vcc:
1166 regulator_set_optimum_mode(data->vcc, 0);
1167 return rc;
1168
1169power_off:
1170 regulator_set_optimum_mode(data->vcc, 0);
1171 regulator_disable(data->vcc);
1172 if (data->pdata->i2c_pull_up) {
1173 regulator_set_optimum_mode(data->vcc_i2c, 0);
1174 regulator_disable(data->vcc_i2c);
1175 }
1176 msleep(50);
1177 return 0;
1178}
1179
1180static int mxt_regulator_configure(struct mxt_data *data, bool on)
1181{
1182 int rc;
1183
1184 if (on == false)
1185 goto hw_shutdown;
1186
1187 data->vcc = regulator_get(&data->client->dev, "vdd");
1188 if (IS_ERR(data->vcc)) {
1189 rc = PTR_ERR(data->vcc);
1190 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1191 rc);
1192 return rc;
1193 }
1194
1195 if (regulator_count_voltages(data->vcc) > 0) {
1196 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1197 MXT_VTG_MAX_UV);
1198 if (rc) {
1199 dev_err(&data->client->dev,
1200 "regulator set_vtg failed rc=%d\n", rc);
1201 goto error_set_vtg_vcc;
1202 }
1203 }
1204
1205 if (data->pdata->i2c_pull_up) {
1206 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1207 if (IS_ERR(data->vcc_i2c)) {
1208 rc = PTR_ERR(data->vcc_i2c);
1209 dev_err(&data->client->dev,
1210 "Regulator get failed rc=%d\n", rc);
1211 goto error_get_vtg_i2c;
1212 }
1213 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1214 rc = regulator_set_voltage(data->vcc_i2c,
1215 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1216 if (rc) {
1217 dev_err(&data->client->dev,
1218 "regulator set_vtg failed rc=%d\n", rc);
1219 goto error_set_vtg_i2c;
1220 }
1221 }
1222 }
1223
1224 return 0;
1225
1226error_set_vtg_i2c:
1227 regulator_put(data->vcc_i2c);
1228error_get_vtg_i2c:
1229 if (regulator_count_voltages(data->vcc) > 0)
1230 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1231error_set_vtg_vcc:
1232 regulator_put(data->vcc);
1233 return rc;
1234
1235hw_shutdown:
1236 if (regulator_count_voltages(data->vcc) > 0)
1237 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1238 regulator_put(data->vcc);
1239 if (data->pdata->i2c_pull_up) {
1240 if (regulator_count_voltages(data->vcc_i2c) > 0)
1241 regulator_set_voltage(data->vcc_i2c, 0,
1242 MXT_I2C_VTG_MAX_UV);
1243 regulator_put(data->vcc_i2c);
1244 }
1245 return 0;
1246}
1247
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301248#ifdef CONFIG_PM
1249static int mxt_suspend(struct device *dev)
1250{
1251 struct i2c_client *client = to_i2c_client(dev);
1252 struct mxt_data *data = i2c_get_clientdata(client);
1253 struct input_dev *input_dev = data->input_dev;
1254
1255 mutex_lock(&input_dev->mutex);
1256
1257 if (input_dev->users)
1258 mxt_stop(data);
1259
1260 mutex_unlock(&input_dev->mutex);
1261
1262 return 0;
1263}
1264
1265static int mxt_resume(struct device *dev)
1266{
1267 struct i2c_client *client = to_i2c_client(dev);
1268 struct mxt_data *data = i2c_get_clientdata(client);
1269 struct input_dev *input_dev = data->input_dev;
1270
1271 /* Soft reset */
1272 mxt_write_object(data, MXT_GEN_COMMAND,
1273 MXT_COMMAND_RESET, 1);
1274
Amy Maloche7e447432011-09-14 11:36:30 -07001275 mxt_reset_delay(data);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301276
1277 mutex_lock(&input_dev->mutex);
1278
1279 if (input_dev->users)
1280 mxt_start(data);
1281
1282 mutex_unlock(&input_dev->mutex);
1283
1284 return 0;
1285}
1286
1287#if defined(CONFIG_HAS_EARLYSUSPEND)
1288static void mxt_early_suspend(struct early_suspend *h)
1289{
1290 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1291
1292 mxt_suspend(&data->client->dev);
1293}
1294
1295static void mxt_late_resume(struct early_suspend *h)
1296{
1297 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1298
1299 mxt_resume(&data->client->dev);
1300}
1301#endif
1302
1303static const struct dev_pm_ops mxt_pm_ops = {
1304#ifndef CONFIG_HAS_EARLYSUSPEND
1305 .suspend = mxt_suspend,
1306 .resume = mxt_resume,
1307#endif
1308};
1309#endif
1310
Iiro Valkonen7686b102011-02-02 23:21:58 -08001311static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001312 const struct i2c_device_id *id)
1313{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001314 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001315 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001316 struct input_dev *input_dev;
1317 int error;
1318
Iiro Valkonen919ed892011-02-15 13:36:52 -08001319 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001320 return -EINVAL;
1321
Iiro Valkonen7686b102011-02-02 23:21:58 -08001322 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001323 input_dev = input_allocate_device();
1324 if (!data || !input_dev) {
1325 dev_err(&client->dev, "Failed to allocate memory\n");
1326 error = -ENOMEM;
1327 goto err_free_mem;
1328 }
1329
Iiro Valkonen7686b102011-02-02 23:21:58 -08001330 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001331 input_dev->id.bustype = BUS_I2C;
1332 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001333 input_dev->open = mxt_input_open;
1334 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001335
Joonyoung Shim910d8052011-04-12 23:14:38 -07001336 data->client = client;
1337 data->input_dev = input_dev;
1338 data->pdata = pdata;
1339 data->irq = client->irq;
1340
1341 mxt_calc_resolution(data);
1342
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001343 __set_bit(EV_ABS, input_dev->evbit);
1344 __set_bit(EV_KEY, input_dev->evbit);
1345 __set_bit(BTN_TOUCH, input_dev->keybit);
1346
1347 /* For single touch */
1348 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001349 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001350 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001351 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001352
1353 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001354 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001355 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001356 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001357 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001358 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001359 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001360 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001361
1362 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001363 i2c_set_clientdata(client, data);
1364
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301365 if (pdata->init_hw)
1366 error = pdata->init_hw(true);
1367 else
1368 error = mxt_regulator_configure(data, true);
1369 if (error) {
1370 dev_err(&client->dev, "Failed to intialize hardware\n");
1371 goto err_free_object;
1372 }
1373
1374 if (pdata->power_on)
1375 error = pdata->power_on(true);
1376 else
1377 error = mxt_power_on(data, true);
1378 if (error) {
1379 dev_err(&client->dev, "Failed to power on hardware\n");
1380 goto err_regulator_on;
1381 }
1382
Iiro Valkonen7686b102011-02-02 23:21:58 -08001383 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001384 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301385 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001386
Iiro Valkonen7686b102011-02-02 23:21:58 -08001387 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001388 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389 if (error) {
1390 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301391 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392 }
1393
Iiro Valkonen08960a02011-04-12 23:16:40 -07001394 error = mxt_make_highchg(data);
1395 if (error)
1396 goto err_free_irq;
1397
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001398 error = input_register_device(input_dev);
1399 if (error)
1400 goto err_free_irq;
1401
Iiro Valkonen7686b102011-02-02 23:21:58 -08001402 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001403 if (error)
1404 goto err_unregister_device;
1405
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301406#if defined(CONFIG_HAS_EARLYSUSPEND)
1407 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1408 MXT_SUSPEND_LEVEL;
1409 data->early_suspend.suspend = mxt_early_suspend;
1410 data->early_suspend.resume = mxt_late_resume;
1411 register_early_suspend(&data->early_suspend);
1412#endif
1413
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001414 return 0;
1415
1416err_unregister_device:
1417 input_unregister_device(input_dev);
1418 input_dev = NULL;
1419err_free_irq:
1420 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301421err_power_on:
1422 if (pdata->power_on)
1423 pdata->power_on(false);
1424 else
1425 mxt_power_on(data, false);
1426err_regulator_on:
1427 if (pdata->init_hw)
1428 pdata->init_hw(false);
1429 else
1430 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001431err_free_object:
1432 kfree(data->object_table);
1433err_free_mem:
1434 input_free_device(input_dev);
1435 kfree(data);
1436 return error;
1437}
1438
Iiro Valkonen7686b102011-02-02 23:21:58 -08001439static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001440{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001441 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001442
Iiro Valkonen7686b102011-02-02 23:21:58 -08001443 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001444 free_irq(data->irq, data);
1445 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301446#if defined(CONFIG_HAS_EARLYSUSPEND)
1447 unregister_early_suspend(&data->early_suspend);
1448#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301449
1450 if (data->pdata->power_on)
1451 data->pdata->power_on(false);
1452 else
1453 mxt_power_on(data, false);
1454
1455 if (data->pdata->init_hw)
1456 data->pdata->init_hw(false);
1457 else
1458 mxt_regulator_configure(data, false);
1459
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001460 kfree(data->object_table);
1461 kfree(data);
1462
1463 return 0;
1464}
1465
Iiro Valkonen7686b102011-02-02 23:21:58 -08001466static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001467 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001468 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001469 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470 { }
1471};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001472MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001473
Iiro Valkonen7686b102011-02-02 23:21:58 -08001474static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001475 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001476 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001477 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001478#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001479 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001480#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001481 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001482 .probe = mxt_probe,
1483 .remove = __devexit_p(mxt_remove),
1484 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001485};
1486
Iiro Valkonen7686b102011-02-02 23:21:58 -08001487static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001488{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001489 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001490}
1491
Iiro Valkonen7686b102011-02-02 23:21:58 -08001492static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001493{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001494 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001495}
1496
Iiro Valkonen7686b102011-02-02 23:21:58 -08001497module_init(mxt_init);
1498module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001499
1500/* Module information */
1501MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001502MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001503MODULE_LICENSE("GPL");