blob: e5bd99b9ed591ba42705833e0c62edcf011e7059 [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
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530231#define MXT_BUFF_SIZE 100
Amy Maloche52262212011-09-15 16:46:57 -0700232#define T7_DATA_SIZE 3
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530233
Iiro Valkonen7686b102011-02-02 23:21:58 -0800234struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235 u8 family_id;
236 u8 variant_id;
237 u8 version;
238 u8 build;
239 u8 matrix_xsize;
240 u8 matrix_ysize;
241 u8 object_num;
242};
243
Iiro Valkonen7686b102011-02-02 23:21:58 -0800244struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700245 u8 type;
246 u16 start_address;
247 u8 size;
248 u8 instances;
249 u8 num_report_ids;
250
251 /* to map object and message */
252 u8 max_reportid;
253};
254
Iiro Valkonen7686b102011-02-02 23:21:58 -0800255struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700256 u8 reportid;
257 u8 message[7];
258 u8 checksum;
259};
260
Iiro Valkonen7686b102011-02-02 23:21:58 -0800261struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700262 int status;
263 int x;
264 int y;
265 int area;
266};
267
268/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800269struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700270 struct i2c_client *client;
271 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800272 const struct mxt_platform_data *pdata;
273 struct mxt_object *object_table;
274 struct mxt_info info;
275 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700276 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700277 unsigned int max_x;
278 unsigned int max_y;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530279 struct regulator *vcc;
280 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530281#if defined(CONFIG_HAS_EARLYSUSPEND)
282 struct early_suspend early_suspend;
283#endif
Amy Maloche52262212011-09-15 16:46:57 -0700284 u8 t7_data[T7_DATA_SIZE];
285 u8 t9_ctrl;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700286};
287
Iiro Valkonen7686b102011-02-02 23:21:58 -0800288static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700289{
290 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800291 case MXT_GEN_MESSAGE:
292 case MXT_GEN_COMMAND:
293 case MXT_GEN_POWER:
294 case MXT_GEN_ACQUIRE:
295 case MXT_TOUCH_MULTI:
296 case MXT_TOUCH_KEYARRAY:
297 case MXT_TOUCH_PROXIMITY:
298 case MXT_PROCI_GRIPFACE:
299 case MXT_PROCG_NOISE:
300 case MXT_PROCI_ONETOUCH:
301 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700302 case MXT_PROCI_GRIP:
303 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800304 case MXT_SPT_COMMSCONFIG:
305 case MXT_SPT_GPIOPWM:
306 case MXT_SPT_SELFTEST:
307 case MXT_SPT_CTECONFIG:
308 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530309 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700310 return true;
311 default:
312 return false;
313 }
314}
315
Iiro Valkonen7686b102011-02-02 23:21:58 -0800316static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700317{
318 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800319 case MXT_GEN_COMMAND:
320 case MXT_GEN_POWER:
321 case MXT_GEN_ACQUIRE:
322 case MXT_TOUCH_MULTI:
323 case MXT_TOUCH_KEYARRAY:
324 case MXT_TOUCH_PROXIMITY:
325 case MXT_PROCI_GRIPFACE:
326 case MXT_PROCG_NOISE:
327 case MXT_PROCI_ONETOUCH:
328 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700329 case MXT_PROCI_GRIP:
330 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800331 case MXT_SPT_GPIOPWM:
332 case MXT_SPT_SELFTEST:
333 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530334 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530335 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700336 return true;
337 default:
338 return false;
339 }
340}
341
Iiro Valkonen7686b102011-02-02 23:21:58 -0800342static void mxt_dump_message(struct device *dev,
343 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700344{
345 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
346 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
347 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
348 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
349 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
350 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
351 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
352 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
353 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
354}
355
Iiro Valkonen7686b102011-02-02 23:21:58 -0800356static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700357 unsigned int state)
358{
359 u8 val;
360
361recheck:
362 if (i2c_master_recv(client, &val, 1) != 1) {
363 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
364 return -EIO;
365 }
366
367 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800368 case MXT_WAITING_BOOTLOAD_CMD:
369 case MXT_WAITING_FRAME_DATA:
370 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700371 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800372 case MXT_FRAME_CRC_PASS:
373 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700374 goto recheck;
375 break;
376 default:
377 return -EINVAL;
378 }
379
380 if (val != state) {
381 dev_err(&client->dev, "Unvalid bootloader mode state\n");
382 return -EINVAL;
383 }
384
385 return 0;
386}
387
Iiro Valkonen7686b102011-02-02 23:21:58 -0800388static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700389{
390 u8 buf[2];
391
Iiro Valkonen7686b102011-02-02 23:21:58 -0800392 buf[0] = MXT_UNLOCK_CMD_LSB;
393 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700394
395 if (i2c_master_send(client, buf, 2) != 2) {
396 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
397 return -EIO;
398 }
399
400 return 0;
401}
402
Iiro Valkonen7686b102011-02-02 23:21:58 -0800403static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700404 const u8 *data, unsigned int frame_size)
405{
406 if (i2c_master_send(client, data, frame_size) != frame_size) {
407 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
408 return -EIO;
409 }
410
411 return 0;
412}
413
Iiro Valkonen7686b102011-02-02 23:21:58 -0800414static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700415 u16 reg, u16 len, void *val)
416{
417 struct i2c_msg xfer[2];
418 u8 buf[2];
419
420 buf[0] = reg & 0xff;
421 buf[1] = (reg >> 8) & 0xff;
422
423 /* Write register */
424 xfer[0].addr = client->addr;
425 xfer[0].flags = 0;
426 xfer[0].len = 2;
427 xfer[0].buf = buf;
428
429 /* Read data */
430 xfer[1].addr = client->addr;
431 xfer[1].flags = I2C_M_RD;
432 xfer[1].len = len;
433 xfer[1].buf = val;
434
435 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
436 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
437 return -EIO;
438 }
439
440 return 0;
441}
442
Iiro Valkonen7686b102011-02-02 23:21:58 -0800443static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700444{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800445 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700446}
447
Iiro Valkonen7686b102011-02-02 23:21:58 -0800448static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700449{
450 u8 buf[3];
451
452 buf[0] = reg & 0xff;
453 buf[1] = (reg >> 8) & 0xff;
454 buf[2] = val;
455
456 if (i2c_master_send(client, buf, 3) != 3) {
457 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
458 return -EIO;
459 }
460
461 return 0;
462}
463
Iiro Valkonen7686b102011-02-02 23:21:58 -0800464static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700465 u16 reg, u8 *object_buf)
466{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800467 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700468 object_buf);
469}
470
Iiro Valkonen7686b102011-02-02 23:21:58 -0800471static struct mxt_object *
472mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700473{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800474 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700475 int i;
476
477 for (i = 0; i < data->info.object_num; i++) {
478 object = data->object_table + i;
479 if (object->type == type)
480 return object;
481 }
482
483 dev_err(&data->client->dev, "Invalid object type\n");
484 return NULL;
485}
486
Iiro Valkonen7686b102011-02-02 23:21:58 -0800487static int mxt_read_message(struct mxt_data *data,
488 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700489{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491 u16 reg;
492
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700494 if (!object)
495 return -EINVAL;
496
497 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800498 return __mxt_read_reg(data->client, reg,
499 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700500}
501
Iiro Valkonen7686b102011-02-02 23:21:58 -0800502static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700503 u8 type, u8 offset, u8 *val)
504{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800505 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700506 u16 reg;
507
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700509 if (!object)
510 return -EINVAL;
511
512 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800513 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514}
515
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517 u8 type, u8 offset, u8 val)
518{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800519 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700520 u16 reg;
521
Iiro Valkonen7686b102011-02-02 23:21:58 -0800522 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700523 if (!object)
524 return -EINVAL;
525
526 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528}
529
Iiro Valkonen7686b102011-02-02 23:21:58 -0800530static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700531{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800532 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700533 struct input_dev *input_dev = data->input_dev;
534 int status = finger[single_id].status;
535 int finger_num = 0;
536 int id;
537
Iiro Valkonen7686b102011-02-02 23:21:58 -0800538 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700539 if (!finger[id].status)
540 continue;
541
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700542 input_mt_slot(input_dev, id);
543 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
544 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700546 if (finger[id].status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700547 finger_num++;
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700548 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
549 finger[id].area);
550 input_report_abs(input_dev, ABS_MT_POSITION_X,
551 finger[id].x);
552 input_report_abs(input_dev, ABS_MT_POSITION_Y,
553 finger[id].y);
554 } else {
555 finger[id].status = 0;
556 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557 }
558
559 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
560
Iiro Valkonen7686b102011-02-02 23:21:58 -0800561 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700562 input_report_abs(input_dev, ABS_X, finger[single_id].x);
563 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
564 }
565
566 input_sync(input_dev);
567}
568
Iiro Valkonen7686b102011-02-02 23:21:58 -0800569static void mxt_input_touchevent(struct mxt_data *data,
570 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700571{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800572 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700573 struct device *dev = &data->client->dev;
574 u8 status = message->message[0];
575 int x;
576 int y;
577 int area;
578
579 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800580 if (!(status & MXT_DETECT)) {
581 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 dev_dbg(dev, "[%d] released\n", id);
583
Iiro Valkonen7686b102011-02-02 23:21:58 -0800584 finger[id].status = MXT_RELEASE;
585 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586 }
587 return;
588 }
589
590 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800591 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592 return;
593
Joonyoung Shim910d8052011-04-12 23:14:38 -0700594 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
595 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
596 if (data->max_x < 1024)
597 x = x >> 2;
598 if (data->max_y < 1024)
599 y = y >> 2;
600
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601 area = message->message[4];
602
603 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700605 x, y, area);
606
Iiro Valkonen7686b102011-02-02 23:21:58 -0800607 finger[id].status = status & MXT_MOVE ?
608 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609 finger[id].x = x;
610 finger[id].y = y;
611 finger[id].area = area;
612
Iiro Valkonen7686b102011-02-02 23:21:58 -0800613 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700614}
615
Iiro Valkonen7686b102011-02-02 23:21:58 -0800616static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700617{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800618 struct mxt_data *data = dev_id;
619 struct mxt_message message;
620 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700621 struct device *dev = &data->client->dev;
622 int id;
623 u8 reportid;
624 u8 max_reportid;
625 u8 min_reportid;
626
627 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800628 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700629 dev_err(dev, "Failed to read message\n");
630 goto end;
631 }
632
633 reportid = message.reportid;
634
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635 /* whether reportid is thing of MXT_TOUCH_MULTI */
636 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637 if (!object)
638 goto end;
639
640 max_reportid = object->max_reportid;
641 min_reportid = max_reportid - object->num_report_ids + 1;
642 id = reportid - min_reportid;
643
644 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800647 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700648 } while (reportid != 0xff);
649
650end:
651 return IRQ_HANDLED;
652}
653
Iiro Valkonen7686b102011-02-02 23:21:58 -0800654static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700655{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800656 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800657 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658 struct device *dev = &data->client->dev;
659 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800660 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700661
Iiro Valkonen71749f52011-02-15 13:36:52 -0800662 if (!pdata->config) {
663 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
664 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665 }
666
667 for (i = 0; i < data->info.object_num; i++) {
668 object = data->object_table + i;
669
Iiro Valkonen7686b102011-02-02 23:21:58 -0800670 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700671 continue;
672
Iiro Valkonen71749f52011-02-15 13:36:52 -0800673 for (j = 0; j < object->size + 1; j++) {
674 config_offset = index + j;
675 if (config_offset > pdata->config_length) {
676 dev_err(dev, "Not enough config data!\n");
677 return -EINVAL;
678 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800679 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800680 pdata->config[config_offset]);
681 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700682 index += object->size + 1;
683 }
684
685 return 0;
686}
687
Iiro Valkonen7686b102011-02-02 23:21:58 -0800688static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689{
690 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800691 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692 int count = 10;
693 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700694
695 /* Read dummy message to make high CHG pin */
696 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800697 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698 if (error)
699 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800700 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701
702 if (!count) {
703 dev_err(dev, "CHG pin isn't cleared\n");
704 return -EBUSY;
705 }
706
707 return 0;
708}
709
Iiro Valkonen7686b102011-02-02 23:21:58 -0800710static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800712 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700713 u8 voltage;
714
715 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800718 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719 pdata->y_line);
720
721 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800722 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700723 pdata->orient);
724
725 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800726 mxt_write_object(data, MXT_TOUCH_MULTI,
727 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728
729 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800730 mxt_write_object(data, MXT_TOUCH_MULTI,
731 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700732
733 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800734 mxt_write_object(data, MXT_TOUCH_MULTI,
735 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
736 mxt_write_object(data, MXT_TOUCH_MULTI,
737 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
738 mxt_write_object(data, MXT_TOUCH_MULTI,
739 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
740 mxt_write_object(data, MXT_TOUCH_MULTI,
741 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700742
743 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700744 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800745 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
746 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
747 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748 voltage = 0xff - voltage + 1;
749 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800750 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
751 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752
Iiro Valkonen7686b102011-02-02 23:21:58 -0800753 mxt_write_object(data, MXT_SPT_CTECONFIG,
754 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700755 }
756}
757
Iiro Valkonen7686b102011-02-02 23:21:58 -0800758static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700759{
760 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800761 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700762 int error;
763 u8 val;
764
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766 if (error)
767 return error;
768 info->family_id = val;
769
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771 if (error)
772 return error;
773 info->variant_id = val;
774
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700776 if (error)
777 return error;
778 info->version = val;
779
Iiro Valkonen7686b102011-02-02 23:21:58 -0800780 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700781 if (error)
782 return error;
783 info->build = val;
784
Iiro Valkonen7686b102011-02-02 23:21:58 -0800785 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786 if (error)
787 return error;
788 info->object_num = val;
789
790 return 0;
791}
792
Iiro Valkonen7686b102011-02-02 23:21:58 -0800793static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794{
795 int error;
796 int i;
797 u16 reg;
798 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800799 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700800
801 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800802 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700803
Iiro Valkonen7686b102011-02-02 23:21:58 -0800804 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
805 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700806 if (error)
807 return error;
808
809 object->type = buf[0];
810 object->start_address = (buf[2] << 8) | buf[1];
811 object->size = buf[3];
812 object->instances = buf[4];
813 object->num_report_ids = buf[5];
814
815 if (object->num_report_ids) {
816 reportid += object->num_report_ids *
817 (object->instances + 1);
818 object->max_reportid = reportid;
819 }
820 }
821
822 return 0;
823}
824
Amy Maloche7e447432011-09-14 11:36:30 -0700825static void mxt_reset_delay(struct mxt_data *data)
826{
827 struct mxt_info *info = &data->info;
828
829 switch (info->family_id) {
830 case MXT224_ID:
831 msleep(MXT224_RESET_TIME);
832 break;
833 case MXT1386_ID:
834 msleep(MXT1386_RESET_TIME);
835 break;
836 default:
837 msleep(MXT_RESET_TIME);
838 }
839}
840
Iiro Valkonen7686b102011-02-02 23:21:58 -0800841static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842{
843 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800844 struct mxt_info *info = &data->info;
Amy Maloche52262212011-09-15 16:46:57 -0700845 int error, i;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700846 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700847 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700848 u8 command_register;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700849
Iiro Valkonen7686b102011-02-02 23:21:58 -0800850 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700851 if (error)
852 return error;
853
854 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800855 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856 GFP_KERNEL);
857 if (!data->object_table) {
858 dev_err(&client->dev, "Failed to allocate memory\n");
859 return -ENOMEM;
860 }
861
862 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800863 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700864 if (error)
865 return error;
866
867 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800868 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700869 if (error)
870 return error;
871
Amy Maloche52262212011-09-15 16:46:57 -0700872 /* Store T7 and T9 locally, used in suspend/resume operations */
873 for (i = 0; i < T7_DATA_SIZE; i++) {
874 error = mxt_read_object(data, MXT_GEN_POWER, i,
875 &data->t7_data[i]);
876 if (error < 0) {
877 dev_err(&client->dev,
878 "failed to save current power state\n");
879 return error;
880 }
881 }
882 error = mxt_read_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL,
883 &data->t9_ctrl);
884 if (error < 0) {
885 dev_err(&client->dev, "failed to save current touch object\n");
886 return error;
887 }
888
Iiro Valkonen7686b102011-02-02 23:21:58 -0800889 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700890
891 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800892 mxt_write_object(data, MXT_GEN_COMMAND,
893 MXT_COMMAND_BACKUPNV,
894 MXT_BACKUP_VALUE);
895 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700896 do {
897 error = mxt_read_object(data, MXT_GEN_COMMAND,
898 MXT_COMMAND_BACKUPNV,
899 &command_register);
900 if (error)
901 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700902 usleep_range(1000, 2000);
903 } while ((command_register != 0) && (++timeout_counter <= 100));
904 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700905 dev_err(&client->dev, "No response after backup!\n");
906 return -EIO;
907 }
908
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909
910 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800911 mxt_write_object(data, MXT_GEN_COMMAND,
912 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700913
Amy Maloche7e447432011-09-14 11:36:30 -0700914 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700915
916 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800917 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700918 if (error)
919 return error;
920 info->matrix_xsize = val;
921
Iiro Valkonen7686b102011-02-02 23:21:58 -0800922 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700923 if (error)
924 return error;
925 info->matrix_ysize = val;
926
927 dev_info(&client->dev,
928 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
929 info->family_id, info->variant_id, info->version,
930 info->build);
931
932 dev_info(&client->dev,
933 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
934 info->matrix_xsize, info->matrix_ysize,
935 info->object_num);
936
937 return 0;
938}
939
Joonyoung Shim910d8052011-04-12 23:14:38 -0700940static void mxt_calc_resolution(struct mxt_data *data)
941{
942 unsigned int max_x = data->pdata->x_size - 1;
943 unsigned int max_y = data->pdata->y_size - 1;
944
945 if (data->pdata->orient & MXT_XY_SWITCH) {
946 data->max_x = max_y;
947 data->max_y = max_x;
948 } else {
949 data->max_x = max_x;
950 data->max_y = max_y;
951 }
952}
953
Iiro Valkonen7686b102011-02-02 23:21:58 -0800954static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955 struct device_attribute *attr, char *buf)
956{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957 struct mxt_data *data = dev_get_drvdata(dev);
958 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700959 int count = 0;
960 int i, j;
961 int error;
962 u8 val;
963
964 for (i = 0; i < data->info.object_num; i++) {
965 object = data->object_table + i;
966
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530967 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700968 "Object Table Element %d(Type %d)\n",
969 i + 1, object->type);
970
Iiro Valkonen7686b102011-02-02 23:21:58 -0800971 if (!mxt_object_readable(object->type)) {
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530972 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700973 continue;
974 }
975
976 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800977 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700978 object->type, j, &val);
979 if (error)
980 return error;
981
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530982 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983 " Byte %d: 0x%x (%d)\n", j, val, val);
984 }
985
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530986 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700987 }
988
989 return count;
990}
991
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700995 struct i2c_client *client = data->client;
996 const struct firmware *fw = NULL;
997 unsigned int frame_size;
998 unsigned int pos = 0;
999 int ret;
1000
1001 ret = request_firmware(&fw, fn, dev);
1002 if (ret) {
1003 dev_err(dev, "Unable to open firmware %s\n", fn);
1004 return ret;
1005 }
1006
1007 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001008 mxt_write_object(data, MXT_GEN_COMMAND,
1009 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001010
1011 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012
1013 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014 if (client->addr == MXT_APP_LOW)
1015 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
Iiro Valkonen7686b102011-02-02 23:21:58 -08001019 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020 if (ret)
1021 goto out;
1022
1023 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001024 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025
1026 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027 ret = mxt_check_bootloader(client,
1028 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029 if (ret)
1030 goto out;
1031
1032 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1033
1034 /* We should add 2 at frame size as the the firmware data is not
1035 * included the CRC bytes.
1036 */
1037 frame_size += 2;
1038
1039 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001040 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001041
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042 ret = mxt_check_bootloader(client,
1043 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001044 if (ret)
1045 goto out;
1046
1047 pos += frame_size;
1048
1049 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1050 }
1051
1052out:
1053 release_firmware(fw);
1054
1055 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001056 if (client->addr == MXT_BOOT_LOW)
1057 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001058 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001059 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001060
1061 return ret;
1062}
1063
Iiro Valkonen7686b102011-02-02 23:21:58 -08001064static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065 struct device_attribute *attr,
1066 const char *buf, size_t count)
1067{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001068 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001069 int error;
1070
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001071 disable_irq(data->irq);
1072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074 if (error) {
1075 dev_err(dev, "The firmware update failed(%d)\n", error);
1076 count = error;
1077 } else {
1078 dev_dbg(dev, "The firmware update succeeded\n");
1079
1080 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001081 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001082
1083 kfree(data->object_table);
1084 data->object_table = NULL;
1085
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001087 }
1088
1089 enable_irq(data->irq);
1090
Iiro Valkonen08960a02011-04-12 23:16:40 -07001091 error = mxt_make_highchg(data);
1092 if (error)
1093 return error;
1094
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095 return count;
1096}
1097
Iiro Valkonen7686b102011-02-02 23:21:58 -08001098static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1099static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100
Iiro Valkonen7686b102011-02-02 23:21:58 -08001101static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001102 &dev_attr_object.attr,
1103 &dev_attr_update_fw.attr,
1104 NULL
1105};
1106
Iiro Valkonen7686b102011-02-02 23:21:58 -08001107static const struct attribute_group mxt_attr_group = {
1108 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109};
1110
Amy Maloche52262212011-09-15 16:46:57 -07001111static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001112{
Amy Maloche52262212011-09-15 16:46:57 -07001113 int i, error;
1114 /* restore the old power state values and reenable touch */
1115 for (i = 0; i < T7_DATA_SIZE; i++) {
1116 error = mxt_write_object(data, MXT_GEN_POWER, i,
1117 data->t7_data[i]);
1118 if (error < 0) {
1119 dev_err(&data->client->dev,
1120 "failed to restore old power state\n");
1121 return error;
1122 }
1123 }
1124 error = mxt_write_object(data,
1125 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1126 if (error < 0) {
1127 dev_err(&data->client->dev, "failed to restore touch\n");
1128 return error;
1129 }
1130
1131 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132}
1133
Amy Maloche52262212011-09-15 16:46:57 -07001134static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001135{
Amy Maloche52262212011-09-15 16:46:57 -07001136 int i, error;
1137 /* configure deep sleep mode and disable touch */
1138 for (i = 0; i < T7_DATA_SIZE; i++) {
1139 error = mxt_write_object(data, MXT_GEN_POWER, i, 0);
1140 if (error < 0) {
1141 dev_err(&data->client->dev,
1142 "failed to configure deep sleep mode\n");
1143 return error;
1144 }
1145 }
1146
1147 error = mxt_write_object(data,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001148 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Amy Maloche52262212011-09-15 16:46:57 -07001149 if (error < 0) {
1150 dev_err(&data->client->dev,
1151 "failed to disable touch\n");
1152 return error;
1153 }
1154
1155 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001156}
1157
Iiro Valkonen7686b102011-02-02 23:21:58 -08001158static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001159{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001160 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001161 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001162
Amy Maloche52262212011-09-15 16:46:57 -07001163 error = mxt_start(data);
1164 if (error < 0) {
1165 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1166 return error;
1167 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168
1169 return 0;
1170}
1171
Iiro Valkonen7686b102011-02-02 23:21:58 -08001172static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001173{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001174 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001175 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176
Amy Maloche52262212011-09-15 16:46:57 -07001177 error = mxt_stop(data);
1178 if (error < 0)
1179 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1180
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001181}
1182
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301183static int mxt_power_on(struct mxt_data *data, bool on)
1184{
1185 int rc;
1186
1187 if (on == false)
1188 goto power_off;
1189
1190 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1191 if (rc < 0) {
1192 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1193 rc);
1194 return rc;
1195 }
1196
1197 rc = regulator_enable(data->vcc);
1198 if (rc) {
1199 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1200 rc);
1201 goto error_reg_en_vcc;
1202 }
1203
1204 if (data->pdata->i2c_pull_up) {
1205 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1206 if (rc < 0) {
1207 dev_err(&data->client->dev,
1208 "Regulator set_opt failed rc=%d\n", rc);
1209 goto error_reg_opt_i2c;
1210 }
1211
1212 rc = regulator_enable(data->vcc_i2c);
1213 if (rc) {
1214 dev_err(&data->client->dev,
1215 "Regulator enable failed rc=%d\n", rc);
1216 goto error_reg_en_vcc_i2c;
1217 }
1218 }
1219
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001220 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301221
1222 return 0;
1223
1224error_reg_en_vcc_i2c:
1225 if (data->pdata->i2c_pull_up)
1226 regulator_set_optimum_mode(data->vcc_i2c, 0);
1227error_reg_opt_i2c:
1228 regulator_disable(data->vcc);
1229error_reg_en_vcc:
1230 regulator_set_optimum_mode(data->vcc, 0);
1231 return rc;
1232
1233power_off:
1234 regulator_set_optimum_mode(data->vcc, 0);
1235 regulator_disable(data->vcc);
1236 if (data->pdata->i2c_pull_up) {
1237 regulator_set_optimum_mode(data->vcc_i2c, 0);
1238 regulator_disable(data->vcc_i2c);
1239 }
1240 msleep(50);
1241 return 0;
1242}
1243
1244static int mxt_regulator_configure(struct mxt_data *data, bool on)
1245{
1246 int rc;
1247
1248 if (on == false)
1249 goto hw_shutdown;
1250
1251 data->vcc = regulator_get(&data->client->dev, "vdd");
1252 if (IS_ERR(data->vcc)) {
1253 rc = PTR_ERR(data->vcc);
1254 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1255 rc);
1256 return rc;
1257 }
1258
1259 if (regulator_count_voltages(data->vcc) > 0) {
1260 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1261 MXT_VTG_MAX_UV);
1262 if (rc) {
1263 dev_err(&data->client->dev,
1264 "regulator set_vtg failed rc=%d\n", rc);
1265 goto error_set_vtg_vcc;
1266 }
1267 }
1268
1269 if (data->pdata->i2c_pull_up) {
1270 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1271 if (IS_ERR(data->vcc_i2c)) {
1272 rc = PTR_ERR(data->vcc_i2c);
1273 dev_err(&data->client->dev,
1274 "Regulator get failed rc=%d\n", rc);
1275 goto error_get_vtg_i2c;
1276 }
1277 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1278 rc = regulator_set_voltage(data->vcc_i2c,
1279 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1280 if (rc) {
1281 dev_err(&data->client->dev,
1282 "regulator set_vtg failed rc=%d\n", rc);
1283 goto error_set_vtg_i2c;
1284 }
1285 }
1286 }
1287
1288 return 0;
1289
1290error_set_vtg_i2c:
1291 regulator_put(data->vcc_i2c);
1292error_get_vtg_i2c:
1293 if (regulator_count_voltages(data->vcc) > 0)
1294 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1295error_set_vtg_vcc:
1296 regulator_put(data->vcc);
1297 return rc;
1298
1299hw_shutdown:
1300 if (regulator_count_voltages(data->vcc) > 0)
1301 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1302 regulator_put(data->vcc);
1303 if (data->pdata->i2c_pull_up) {
1304 if (regulator_count_voltages(data->vcc_i2c) > 0)
1305 regulator_set_voltage(data->vcc_i2c, 0,
1306 MXT_I2C_VTG_MAX_UV);
1307 regulator_put(data->vcc_i2c);
1308 }
1309 return 0;
1310}
1311
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301312#ifdef CONFIG_PM
1313static int mxt_suspend(struct device *dev)
1314{
1315 struct i2c_client *client = to_i2c_client(dev);
1316 struct mxt_data *data = i2c_get_clientdata(client);
1317 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001318 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301319
1320 mutex_lock(&input_dev->mutex);
1321
Amy Maloche52262212011-09-15 16:46:57 -07001322 if (input_dev->users) {
1323 error = mxt_stop(data);
1324 if (error < 0) {
1325 dev_err(&client->dev, "mxt_stop failed in suspend\n");
1326 mutex_unlock(&input_dev->mutex);
1327 return error;
1328 }
1329
1330 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301331
1332 mutex_unlock(&input_dev->mutex);
1333
1334 return 0;
1335}
1336
1337static int mxt_resume(struct device *dev)
1338{
1339 struct i2c_client *client = to_i2c_client(dev);
1340 struct mxt_data *data = i2c_get_clientdata(client);
1341 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001342 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301343 /* Soft reset */
1344 mxt_write_object(data, MXT_GEN_COMMAND,
1345 MXT_COMMAND_RESET, 1);
1346
Amy Maloche7e447432011-09-14 11:36:30 -07001347 mxt_reset_delay(data);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301348
1349 mutex_lock(&input_dev->mutex);
1350
Amy Maloche52262212011-09-15 16:46:57 -07001351 if (input_dev->users) {
1352 error = mxt_start(data);
1353 if (error < 0) {
1354 dev_err(&client->dev, "mxt_start failed in resume\n");
1355 mutex_unlock(&input_dev->mutex);
1356 return error;
1357 }
1358 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301359
1360 mutex_unlock(&input_dev->mutex);
1361
1362 return 0;
1363}
1364
1365#if defined(CONFIG_HAS_EARLYSUSPEND)
1366static void mxt_early_suspend(struct early_suspend *h)
1367{
1368 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1369
1370 mxt_suspend(&data->client->dev);
1371}
1372
1373static void mxt_late_resume(struct early_suspend *h)
1374{
1375 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1376
1377 mxt_resume(&data->client->dev);
1378}
1379#endif
1380
1381static const struct dev_pm_ops mxt_pm_ops = {
1382#ifndef CONFIG_HAS_EARLYSUSPEND
1383 .suspend = mxt_suspend,
1384 .resume = mxt_resume,
1385#endif
1386};
1387#endif
1388
Iiro Valkonen7686b102011-02-02 23:21:58 -08001389static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001390 const struct i2c_device_id *id)
1391{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001392 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001393 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001394 struct input_dev *input_dev;
1395 int error;
1396
Iiro Valkonen919ed892011-02-15 13:36:52 -08001397 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001398 return -EINVAL;
1399
Iiro Valkonen7686b102011-02-02 23:21:58 -08001400 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001401 input_dev = input_allocate_device();
1402 if (!data || !input_dev) {
1403 dev_err(&client->dev, "Failed to allocate memory\n");
1404 error = -ENOMEM;
1405 goto err_free_mem;
1406 }
1407
Iiro Valkonen7686b102011-02-02 23:21:58 -08001408 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001409 input_dev->id.bustype = BUS_I2C;
1410 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001411 input_dev->open = mxt_input_open;
1412 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001413
Joonyoung Shim910d8052011-04-12 23:14:38 -07001414 data->client = client;
1415 data->input_dev = input_dev;
1416 data->pdata = pdata;
1417 data->irq = client->irq;
1418
1419 mxt_calc_resolution(data);
1420
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001421 __set_bit(EV_ABS, input_dev->evbit);
1422 __set_bit(EV_KEY, input_dev->evbit);
1423 __set_bit(BTN_TOUCH, input_dev->keybit);
1424
1425 /* For single touch */
1426 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001427 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001428 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001429 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001430
1431 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001432 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001433 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001434 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001435 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001436 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001437 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001438 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001439
1440 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001441 i2c_set_clientdata(client, data);
1442
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301443 if (pdata->init_hw)
1444 error = pdata->init_hw(true);
1445 else
1446 error = mxt_regulator_configure(data, true);
1447 if (error) {
1448 dev_err(&client->dev, "Failed to intialize hardware\n");
1449 goto err_free_object;
1450 }
1451
1452 if (pdata->power_on)
1453 error = pdata->power_on(true);
1454 else
1455 error = mxt_power_on(data, true);
1456 if (error) {
1457 dev_err(&client->dev, "Failed to power on hardware\n");
1458 goto err_regulator_on;
1459 }
1460
Iiro Valkonen7686b102011-02-02 23:21:58 -08001461 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001462 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301463 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001464
Iiro Valkonen7686b102011-02-02 23:21:58 -08001465 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001466 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001467 if (error) {
1468 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301469 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470 }
1471
Iiro Valkonen08960a02011-04-12 23:16:40 -07001472 error = mxt_make_highchg(data);
1473 if (error)
1474 goto err_free_irq;
1475
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001476 error = input_register_device(input_dev);
1477 if (error)
1478 goto err_free_irq;
1479
Iiro Valkonen7686b102011-02-02 23:21:58 -08001480 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001481 if (error)
1482 goto err_unregister_device;
1483
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301484#if defined(CONFIG_HAS_EARLYSUSPEND)
1485 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1486 MXT_SUSPEND_LEVEL;
1487 data->early_suspend.suspend = mxt_early_suspend;
1488 data->early_suspend.resume = mxt_late_resume;
1489 register_early_suspend(&data->early_suspend);
1490#endif
1491
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001492 return 0;
1493
1494err_unregister_device:
1495 input_unregister_device(input_dev);
1496 input_dev = NULL;
1497err_free_irq:
1498 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301499err_power_on:
1500 if (pdata->power_on)
1501 pdata->power_on(false);
1502 else
1503 mxt_power_on(data, false);
1504err_regulator_on:
1505 if (pdata->init_hw)
1506 pdata->init_hw(false);
1507 else
1508 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001509err_free_object:
1510 kfree(data->object_table);
1511err_free_mem:
1512 input_free_device(input_dev);
1513 kfree(data);
1514 return error;
1515}
1516
Iiro Valkonen7686b102011-02-02 23:21:58 -08001517static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001518{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001519 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001520
Iiro Valkonen7686b102011-02-02 23:21:58 -08001521 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001522 free_irq(data->irq, data);
1523 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301524#if defined(CONFIG_HAS_EARLYSUSPEND)
1525 unregister_early_suspend(&data->early_suspend);
1526#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301527
1528 if (data->pdata->power_on)
1529 data->pdata->power_on(false);
1530 else
1531 mxt_power_on(data, false);
1532
1533 if (data->pdata->init_hw)
1534 data->pdata->init_hw(false);
1535 else
1536 mxt_regulator_configure(data, false);
1537
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001538 kfree(data->object_table);
1539 kfree(data);
1540
1541 return 0;
1542}
1543
Iiro Valkonen7686b102011-02-02 23:21:58 -08001544static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001545 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001546 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001547 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001548 { }
1549};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001550MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001551
Iiro Valkonen7686b102011-02-02 23:21:58 -08001552static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001553 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001554 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001555 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001556#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001557 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001558#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001559 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001560 .probe = mxt_probe,
1561 .remove = __devexit_p(mxt_remove),
1562 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001563};
1564
Iiro Valkonen7686b102011-02-02 23:21:58 -08001565static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001566{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001567 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001568}
1569
Iiro Valkonen7686b102011-02-02 23:21:58 -08001570static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001571{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001572 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001573}
1574
Iiro Valkonen7686b102011-02-02 23:21:58 -08001575module_init(mxt_init);
1576module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001577
1578/* Module information */
1579MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001580MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001581MODULE_LICENSE("GPL");