blob: c9a5ba2350258a9914e94c8b3c647e39dc867bdc [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>
Amy Maloche2b59bab2011-10-13 16:08:16 -070020#include <linux/input.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
Amy Maloche2b59bab2011-10-13 16:08:16 -0700542 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
543 finger[id].status != MXT_RELEASE ?
544 finger[id].area : 0);
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 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550
Amy Maloche2b59bab2011-10-13 16:08:16 -0700551 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700552 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700553 else
554 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700555 }
556
557 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
558
Iiro Valkonen7686b102011-02-02 23:21:58 -0800559 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700560 input_report_abs(input_dev, ABS_X, finger[single_id].x);
561 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
562 }
563
564 input_sync(input_dev);
565}
566
Iiro Valkonen7686b102011-02-02 23:21:58 -0800567static void mxt_input_touchevent(struct mxt_data *data,
568 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800570 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700571 struct device *dev = &data->client->dev;
572 u8 status = message->message[0];
573 int x;
574 int y;
575 int area;
576
577 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800578 if (!(status & MXT_DETECT)) {
579 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580 dev_dbg(dev, "[%d] released\n", id);
581
Iiro Valkonen7686b102011-02-02 23:21:58 -0800582 finger[id].status = MXT_RELEASE;
583 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700584 }
585 return;
586 }
587
588 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590 return;
591
Joonyoung Shim910d8052011-04-12 23:14:38 -0700592 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
593 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
594 if (data->max_x < 1024)
595 x = x >> 2;
596 if (data->max_y < 1024)
597 y = y >> 2;
598
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599 area = message->message[4];
600
601 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800602 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603 x, y, area);
604
Iiro Valkonen7686b102011-02-02 23:21:58 -0800605 finger[id].status = status & MXT_MOVE ?
606 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700607 finger[id].x = x;
608 finger[id].y = y;
609 finger[id].area = area;
610
Iiro Valkonen7686b102011-02-02 23:21:58 -0800611 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700612}
613
Iiro Valkonen7686b102011-02-02 23:21:58 -0800614static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700615{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800616 struct mxt_data *data = dev_id;
617 struct mxt_message message;
618 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700619 struct device *dev = &data->client->dev;
620 int id;
621 u8 reportid;
622 u8 max_reportid;
623 u8 min_reportid;
624
625 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800626 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700627 dev_err(dev, "Failed to read message\n");
628 goto end;
629 }
630
631 reportid = message.reportid;
632
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 /* whether reportid is thing of MXT_TOUCH_MULTI */
634 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635 if (!object)
636 goto end;
637
638 max_reportid = object->max_reportid;
639 min_reportid = max_reportid - object->num_report_ids + 1;
640 id = reportid - min_reportid;
641
642 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800643 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700644 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646 } while (reportid != 0xff);
647
648end:
649 return IRQ_HANDLED;
650}
651
Iiro Valkonen7686b102011-02-02 23:21:58 -0800652static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800654 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800655 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656 struct device *dev = &data->client->dev;
657 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800658 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700659
Iiro Valkonen71749f52011-02-15 13:36:52 -0800660 if (!pdata->config) {
661 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
662 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700663 }
664
665 for (i = 0; i < data->info.object_num; i++) {
666 object = data->object_table + i;
667
Iiro Valkonen7686b102011-02-02 23:21:58 -0800668 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669 continue;
670
Iiro Valkonen71749f52011-02-15 13:36:52 -0800671 for (j = 0; j < object->size + 1; j++) {
672 config_offset = index + j;
673 if (config_offset > pdata->config_length) {
674 dev_err(dev, "Not enough config data!\n");
675 return -EINVAL;
676 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800677 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800678 pdata->config[config_offset]);
679 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680 index += object->size + 1;
681 }
682
683 return 0;
684}
685
Iiro Valkonen7686b102011-02-02 23:21:58 -0800686static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687{
688 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800689 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700690 int count = 10;
691 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692
693 /* Read dummy message to make high CHG pin */
694 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800695 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696 if (error)
697 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800698 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699
700 if (!count) {
701 dev_err(dev, "CHG pin isn't cleared\n");
702 return -EBUSY;
703 }
704
705 return 0;
706}
707
Iiro Valkonen7686b102011-02-02 23:21:58 -0800708static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700709{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800710 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 u8 voltage;
712
713 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800714 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717 pdata->y_line);
718
719 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800720 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721 pdata->orient);
722
723 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724 mxt_write_object(data, MXT_TOUCH_MULTI,
725 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700726
727 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800728 mxt_write_object(data, MXT_TOUCH_MULTI,
729 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730
731 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800732 mxt_write_object(data, MXT_TOUCH_MULTI,
733 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
734 mxt_write_object(data, MXT_TOUCH_MULTI,
735 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
736 mxt_write_object(data, MXT_TOUCH_MULTI,
737 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
738 mxt_write_object(data, MXT_TOUCH_MULTI,
739 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700740
741 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700742 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800743 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
744 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
745 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700746 voltage = 0xff - voltage + 1;
747 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800748 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
749 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700750
Iiro Valkonen7686b102011-02-02 23:21:58 -0800751 mxt_write_object(data, MXT_SPT_CTECONFIG,
752 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700753 }
754}
755
Iiro Valkonen7686b102011-02-02 23:21:58 -0800756static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700757{
758 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800759 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700760 int error;
761 u8 val;
762
Iiro Valkonen7686b102011-02-02 23:21:58 -0800763 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700764 if (error)
765 return error;
766 info->family_id = val;
767
Iiro Valkonen7686b102011-02-02 23:21:58 -0800768 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700769 if (error)
770 return error;
771 info->variant_id = val;
772
Iiro Valkonen7686b102011-02-02 23:21:58 -0800773 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700774 if (error)
775 return error;
776 info->version = val;
777
Iiro Valkonen7686b102011-02-02 23:21:58 -0800778 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779 if (error)
780 return error;
781 info->build = val;
782
Iiro Valkonen7686b102011-02-02 23:21:58 -0800783 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700784 if (error)
785 return error;
786 info->object_num = val;
787
788 return 0;
789}
790
Iiro Valkonen7686b102011-02-02 23:21:58 -0800791static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700792{
793 int error;
794 int i;
795 u16 reg;
796 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800797 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798
799 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800800 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801
Iiro Valkonen7686b102011-02-02 23:21:58 -0800802 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
803 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700804 if (error)
805 return error;
806
807 object->type = buf[0];
808 object->start_address = (buf[2] << 8) | buf[1];
809 object->size = buf[3];
810 object->instances = buf[4];
811 object->num_report_ids = buf[5];
812
813 if (object->num_report_ids) {
814 reportid += object->num_report_ids *
815 (object->instances + 1);
816 object->max_reportid = reportid;
817 }
818 }
819
820 return 0;
821}
822
Amy Maloche7e447432011-09-14 11:36:30 -0700823static void mxt_reset_delay(struct mxt_data *data)
824{
825 struct mxt_info *info = &data->info;
826
827 switch (info->family_id) {
828 case MXT224_ID:
829 msleep(MXT224_RESET_TIME);
830 break;
831 case MXT1386_ID:
832 msleep(MXT1386_RESET_TIME);
833 break;
834 default:
835 msleep(MXT_RESET_TIME);
836 }
837}
838
Iiro Valkonen7686b102011-02-02 23:21:58 -0800839static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700840{
841 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800842 struct mxt_info *info = &data->info;
Amy Maloche52262212011-09-15 16:46:57 -0700843 int error, i;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700844 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700846 u8 command_register;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700847
Iiro Valkonen7686b102011-02-02 23:21:58 -0800848 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700849 if (error)
850 return error;
851
852 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800853 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700854 GFP_KERNEL);
855 if (!data->object_table) {
856 dev_err(&client->dev, "Failed to allocate memory\n");
857 return -ENOMEM;
858 }
859
860 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800861 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700862 if (error)
863 return error;
864
865 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800866 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700867 if (error)
868 return error;
869
Amy Maloche52262212011-09-15 16:46:57 -0700870 /* Store T7 and T9 locally, used in suspend/resume operations */
871 for (i = 0; i < T7_DATA_SIZE; i++) {
872 error = mxt_read_object(data, MXT_GEN_POWER, i,
873 &data->t7_data[i]);
874 if (error < 0) {
875 dev_err(&client->dev,
876 "failed to save current power state\n");
877 return error;
878 }
879 }
880 error = mxt_read_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL,
881 &data->t9_ctrl);
882 if (error < 0) {
883 dev_err(&client->dev, "failed to save current touch object\n");
884 return error;
885 }
886
Iiro Valkonen7686b102011-02-02 23:21:58 -0800887 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700888
889 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800890 mxt_write_object(data, MXT_GEN_COMMAND,
891 MXT_COMMAND_BACKUPNV,
892 MXT_BACKUP_VALUE);
893 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700894 do {
895 error = mxt_read_object(data, MXT_GEN_COMMAND,
896 MXT_COMMAND_BACKUPNV,
897 &command_register);
898 if (error)
899 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700900 usleep_range(1000, 2000);
901 } while ((command_register != 0) && (++timeout_counter <= 100));
902 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700903 dev_err(&client->dev, "No response after backup!\n");
904 return -EIO;
905 }
906
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700907
908 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800909 mxt_write_object(data, MXT_GEN_COMMAND,
910 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700911
Amy Maloche7e447432011-09-14 11:36:30 -0700912 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700913
914 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800915 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700916 if (error)
917 return error;
918 info->matrix_xsize = val;
919
Iiro Valkonen7686b102011-02-02 23:21:58 -0800920 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700921 if (error)
922 return error;
923 info->matrix_ysize = val;
924
925 dev_info(&client->dev,
926 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
927 info->family_id, info->variant_id, info->version,
928 info->build);
929
930 dev_info(&client->dev,
931 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
932 info->matrix_xsize, info->matrix_ysize,
933 info->object_num);
934
935 return 0;
936}
937
Joonyoung Shim910d8052011-04-12 23:14:38 -0700938static void mxt_calc_resolution(struct mxt_data *data)
939{
940 unsigned int max_x = data->pdata->x_size - 1;
941 unsigned int max_y = data->pdata->y_size - 1;
942
943 if (data->pdata->orient & MXT_XY_SWITCH) {
944 data->max_x = max_y;
945 data->max_y = max_x;
946 } else {
947 data->max_x = max_x;
948 data->max_y = max_y;
949 }
950}
951
Iiro Valkonen7686b102011-02-02 23:21:58 -0800952static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953 struct device_attribute *attr, char *buf)
954{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 struct mxt_data *data = dev_get_drvdata(dev);
956 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700957 int count = 0;
958 int i, j;
959 int error;
960 u8 val;
961
962 for (i = 0; i < data->info.object_num; i++) {
963 object = data->object_table + i;
964
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530965 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700966 "Object Table Element %d(Type %d)\n",
967 i + 1, object->type);
968
Iiro Valkonen7686b102011-02-02 23:21:58 -0800969 if (!mxt_object_readable(object->type)) {
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530970 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700971 continue;
972 }
973
974 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800975 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700976 object->type, j, &val);
977 if (error)
978 return error;
979
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530980 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700981 " Byte %d: 0x%x (%d)\n", j, val, val);
982 }
983
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530984 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700985 }
986
987 return count;
988}
989
Iiro Valkonen7686b102011-02-02 23:21:58 -0800990static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993 struct i2c_client *client = data->client;
994 const struct firmware *fw = NULL;
995 unsigned int frame_size;
996 unsigned int pos = 0;
997 int ret;
998
999 ret = request_firmware(&fw, fn, dev);
1000 if (ret) {
1001 dev_err(dev, "Unable to open firmware %s\n", fn);
1002 return ret;
1003 }
1004
1005 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001006 mxt_write_object(data, MXT_GEN_COMMAND,
1007 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001008
1009 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010
1011 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001012 if (client->addr == MXT_APP_LOW)
1013 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001014 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001015 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018 if (ret)
1019 goto out;
1020
1021 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001023
1024 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001025 ret = mxt_check_bootloader(client,
1026 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027 if (ret)
1028 goto out;
1029
1030 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1031
1032 /* We should add 2 at frame size as the the firmware data is not
1033 * included the CRC bytes.
1034 */
1035 frame_size += 2;
1036
1037 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001038 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001039
Iiro Valkonen7686b102011-02-02 23:21:58 -08001040 ret = mxt_check_bootloader(client,
1041 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042 if (ret)
1043 goto out;
1044
1045 pos += frame_size;
1046
1047 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1048 }
1049
1050out:
1051 release_firmware(fw);
1052
1053 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001054 if (client->addr == MXT_BOOT_LOW)
1055 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001056 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001057 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001058
1059 return ret;
1060}
1061
Iiro Valkonen7686b102011-02-02 23:21:58 -08001062static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063 struct device_attribute *attr,
1064 const char *buf, size_t count)
1065{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067 int error;
1068
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001069 disable_irq(data->irq);
1070
Iiro Valkonen7686b102011-02-02 23:21:58 -08001071 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001072 if (error) {
1073 dev_err(dev, "The firmware update failed(%d)\n", error);
1074 count = error;
1075 } else {
1076 dev_dbg(dev, "The firmware update succeeded\n");
1077
1078 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080
1081 kfree(data->object_table);
1082 data->object_table = NULL;
1083
Iiro Valkonen7686b102011-02-02 23:21:58 -08001084 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001085 }
1086
1087 enable_irq(data->irq);
1088
Iiro Valkonen08960a02011-04-12 23:16:40 -07001089 error = mxt_make_highchg(data);
1090 if (error)
1091 return error;
1092
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093 return count;
1094}
1095
Iiro Valkonen7686b102011-02-02 23:21:58 -08001096static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1097static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001098
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100 &dev_attr_object.attr,
1101 &dev_attr_update_fw.attr,
1102 NULL
1103};
1104
Iiro Valkonen7686b102011-02-02 23:21:58 -08001105static const struct attribute_group mxt_attr_group = {
1106 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001107};
1108
Amy Maloche52262212011-09-15 16:46:57 -07001109static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001110{
Amy Maloche52262212011-09-15 16:46:57 -07001111 int i, error;
1112 /* restore the old power state values and reenable touch */
1113 for (i = 0; i < T7_DATA_SIZE; i++) {
1114 error = mxt_write_object(data, MXT_GEN_POWER, i,
1115 data->t7_data[i]);
1116 if (error < 0) {
1117 dev_err(&data->client->dev,
1118 "failed to restore old power state\n");
1119 return error;
1120 }
1121 }
1122 error = mxt_write_object(data,
1123 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1124 if (error < 0) {
1125 dev_err(&data->client->dev, "failed to restore touch\n");
1126 return error;
1127 }
1128
1129 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001130}
1131
Amy Maloche52262212011-09-15 16:46:57 -07001132static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001133{
Amy Maloche52262212011-09-15 16:46:57 -07001134 int i, error;
1135 /* configure deep sleep mode and disable touch */
1136 for (i = 0; i < T7_DATA_SIZE; i++) {
1137 error = mxt_write_object(data, MXT_GEN_POWER, i, 0);
1138 if (error < 0) {
1139 dev_err(&data->client->dev,
1140 "failed to configure deep sleep mode\n");
1141 return error;
1142 }
1143 }
1144
1145 error = mxt_write_object(data,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001146 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Amy Maloche52262212011-09-15 16:46:57 -07001147 if (error < 0) {
1148 dev_err(&data->client->dev,
1149 "failed to disable touch\n");
1150 return error;
1151 }
1152
1153 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001154}
1155
Iiro Valkonen7686b102011-02-02 23:21:58 -08001156static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001157{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001158 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001159 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001160
Amy Maloche52262212011-09-15 16:46:57 -07001161 error = mxt_start(data);
1162 if (error < 0) {
1163 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1164 return error;
1165 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166
1167 return 0;
1168}
1169
Iiro Valkonen7686b102011-02-02 23:21:58 -08001170static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001171{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001172 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001173 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001174
Amy Maloche52262212011-09-15 16:46:57 -07001175 error = mxt_stop(data);
1176 if (error < 0)
1177 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1178
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001179}
1180
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301181static int mxt_power_on(struct mxt_data *data, bool on)
1182{
1183 int rc;
1184
1185 if (on == false)
1186 goto power_off;
1187
1188 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1189 if (rc < 0) {
1190 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1191 rc);
1192 return rc;
1193 }
1194
1195 rc = regulator_enable(data->vcc);
1196 if (rc) {
1197 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1198 rc);
1199 goto error_reg_en_vcc;
1200 }
1201
1202 if (data->pdata->i2c_pull_up) {
1203 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1204 if (rc < 0) {
1205 dev_err(&data->client->dev,
1206 "Regulator set_opt failed rc=%d\n", rc);
1207 goto error_reg_opt_i2c;
1208 }
1209
1210 rc = regulator_enable(data->vcc_i2c);
1211 if (rc) {
1212 dev_err(&data->client->dev,
1213 "Regulator enable failed rc=%d\n", rc);
1214 goto error_reg_en_vcc_i2c;
1215 }
1216 }
1217
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001218 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301219
1220 return 0;
1221
1222error_reg_en_vcc_i2c:
1223 if (data->pdata->i2c_pull_up)
1224 regulator_set_optimum_mode(data->vcc_i2c, 0);
1225error_reg_opt_i2c:
1226 regulator_disable(data->vcc);
1227error_reg_en_vcc:
1228 regulator_set_optimum_mode(data->vcc, 0);
1229 return rc;
1230
1231power_off:
1232 regulator_set_optimum_mode(data->vcc, 0);
1233 regulator_disable(data->vcc);
1234 if (data->pdata->i2c_pull_up) {
1235 regulator_set_optimum_mode(data->vcc_i2c, 0);
1236 regulator_disable(data->vcc_i2c);
1237 }
1238 msleep(50);
1239 return 0;
1240}
1241
1242static int mxt_regulator_configure(struct mxt_data *data, bool on)
1243{
1244 int rc;
1245
1246 if (on == false)
1247 goto hw_shutdown;
1248
1249 data->vcc = regulator_get(&data->client->dev, "vdd");
1250 if (IS_ERR(data->vcc)) {
1251 rc = PTR_ERR(data->vcc);
1252 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1253 rc);
1254 return rc;
1255 }
1256
1257 if (regulator_count_voltages(data->vcc) > 0) {
1258 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1259 MXT_VTG_MAX_UV);
1260 if (rc) {
1261 dev_err(&data->client->dev,
1262 "regulator set_vtg failed rc=%d\n", rc);
1263 goto error_set_vtg_vcc;
1264 }
1265 }
1266
1267 if (data->pdata->i2c_pull_up) {
1268 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1269 if (IS_ERR(data->vcc_i2c)) {
1270 rc = PTR_ERR(data->vcc_i2c);
1271 dev_err(&data->client->dev,
1272 "Regulator get failed rc=%d\n", rc);
1273 goto error_get_vtg_i2c;
1274 }
1275 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1276 rc = regulator_set_voltage(data->vcc_i2c,
1277 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1278 if (rc) {
1279 dev_err(&data->client->dev,
1280 "regulator set_vtg failed rc=%d\n", rc);
1281 goto error_set_vtg_i2c;
1282 }
1283 }
1284 }
1285
1286 return 0;
1287
1288error_set_vtg_i2c:
1289 regulator_put(data->vcc_i2c);
1290error_get_vtg_i2c:
1291 if (regulator_count_voltages(data->vcc) > 0)
1292 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1293error_set_vtg_vcc:
1294 regulator_put(data->vcc);
1295 return rc;
1296
1297hw_shutdown:
1298 if (regulator_count_voltages(data->vcc) > 0)
1299 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1300 regulator_put(data->vcc);
1301 if (data->pdata->i2c_pull_up) {
1302 if (regulator_count_voltages(data->vcc_i2c) > 0)
1303 regulator_set_voltage(data->vcc_i2c, 0,
1304 MXT_I2C_VTG_MAX_UV);
1305 regulator_put(data->vcc_i2c);
1306 }
1307 return 0;
1308}
1309
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301310#ifdef CONFIG_PM
1311static int mxt_suspend(struct device *dev)
1312{
1313 struct i2c_client *client = to_i2c_client(dev);
1314 struct mxt_data *data = i2c_get_clientdata(client);
1315 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001316 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301317
1318 mutex_lock(&input_dev->mutex);
1319
Amy Maloche52262212011-09-15 16:46:57 -07001320 if (input_dev->users) {
1321 error = mxt_stop(data);
1322 if (error < 0) {
1323 dev_err(&client->dev, "mxt_stop failed in suspend\n");
1324 mutex_unlock(&input_dev->mutex);
1325 return error;
1326 }
1327
1328 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301329
1330 mutex_unlock(&input_dev->mutex);
1331
1332 return 0;
1333}
1334
1335static int mxt_resume(struct device *dev)
1336{
1337 struct i2c_client *client = to_i2c_client(dev);
1338 struct mxt_data *data = i2c_get_clientdata(client);
1339 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001340 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301341 /* Soft reset */
1342 mxt_write_object(data, MXT_GEN_COMMAND,
1343 MXT_COMMAND_RESET, 1);
1344
Amy Maloche7e447432011-09-14 11:36:30 -07001345 mxt_reset_delay(data);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301346
1347 mutex_lock(&input_dev->mutex);
1348
Amy Maloche52262212011-09-15 16:46:57 -07001349 if (input_dev->users) {
1350 error = mxt_start(data);
1351 if (error < 0) {
1352 dev_err(&client->dev, "mxt_start failed in resume\n");
1353 mutex_unlock(&input_dev->mutex);
1354 return error;
1355 }
1356 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301357
1358 mutex_unlock(&input_dev->mutex);
1359
1360 return 0;
1361}
1362
1363#if defined(CONFIG_HAS_EARLYSUSPEND)
1364static void mxt_early_suspend(struct early_suspend *h)
1365{
1366 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1367
1368 mxt_suspend(&data->client->dev);
1369}
1370
1371static void mxt_late_resume(struct early_suspend *h)
1372{
1373 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1374
1375 mxt_resume(&data->client->dev);
1376}
1377#endif
1378
1379static const struct dev_pm_ops mxt_pm_ops = {
1380#ifndef CONFIG_HAS_EARLYSUSPEND
1381 .suspend = mxt_suspend,
1382 .resume = mxt_resume,
1383#endif
1384};
1385#endif
1386
Iiro Valkonen7686b102011-02-02 23:21:58 -08001387static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001388 const struct i2c_device_id *id)
1389{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001390 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001391 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392 struct input_dev *input_dev;
1393 int error;
1394
Iiro Valkonen919ed892011-02-15 13:36:52 -08001395 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001396 return -EINVAL;
1397
Iiro Valkonen7686b102011-02-02 23:21:58 -08001398 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001399 input_dev = input_allocate_device();
1400 if (!data || !input_dev) {
1401 dev_err(&client->dev, "Failed to allocate memory\n");
1402 error = -ENOMEM;
1403 goto err_free_mem;
1404 }
1405
Iiro Valkonen7686b102011-02-02 23:21:58 -08001406 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001407 input_dev->id.bustype = BUS_I2C;
1408 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001409 input_dev->open = mxt_input_open;
1410 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001411
Joonyoung Shim910d8052011-04-12 23:14:38 -07001412 data->client = client;
1413 data->input_dev = input_dev;
1414 data->pdata = pdata;
1415 data->irq = client->irq;
1416
1417 mxt_calc_resolution(data);
1418
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001419 __set_bit(EV_ABS, input_dev->evbit);
1420 __set_bit(EV_KEY, input_dev->evbit);
1421 __set_bit(BTN_TOUCH, input_dev->keybit);
1422
1423 /* For single touch */
1424 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001425 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001426 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001427 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001428
1429 /* For multi touch */
1430 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001431 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001432 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001433 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001434 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001435 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001436
1437 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001438 i2c_set_clientdata(client, data);
1439
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301440 if (pdata->init_hw)
1441 error = pdata->init_hw(true);
1442 else
1443 error = mxt_regulator_configure(data, true);
1444 if (error) {
1445 dev_err(&client->dev, "Failed to intialize hardware\n");
1446 goto err_free_object;
1447 }
1448
1449 if (pdata->power_on)
1450 error = pdata->power_on(true);
1451 else
1452 error = mxt_power_on(data, true);
1453 if (error) {
1454 dev_err(&client->dev, "Failed to power on hardware\n");
1455 goto err_regulator_on;
1456 }
1457
Iiro Valkonen7686b102011-02-02 23:21:58 -08001458 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001459 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301460 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001461
Iiro Valkonen7686b102011-02-02 23:21:58 -08001462 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001463 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001464 if (error) {
1465 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301466 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001467 }
1468
Iiro Valkonen08960a02011-04-12 23:16:40 -07001469 error = mxt_make_highchg(data);
1470 if (error)
1471 goto err_free_irq;
1472
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001473 error = input_register_device(input_dev);
1474 if (error)
1475 goto err_free_irq;
1476
Iiro Valkonen7686b102011-02-02 23:21:58 -08001477 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001478 if (error)
1479 goto err_unregister_device;
1480
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301481#if defined(CONFIG_HAS_EARLYSUSPEND)
1482 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1483 MXT_SUSPEND_LEVEL;
1484 data->early_suspend.suspend = mxt_early_suspend;
1485 data->early_suspend.resume = mxt_late_resume;
1486 register_early_suspend(&data->early_suspend);
1487#endif
1488
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001489 return 0;
1490
1491err_unregister_device:
1492 input_unregister_device(input_dev);
1493 input_dev = NULL;
1494err_free_irq:
1495 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301496err_power_on:
1497 if (pdata->power_on)
1498 pdata->power_on(false);
1499 else
1500 mxt_power_on(data, false);
1501err_regulator_on:
1502 if (pdata->init_hw)
1503 pdata->init_hw(false);
1504 else
1505 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001506err_free_object:
1507 kfree(data->object_table);
1508err_free_mem:
1509 input_free_device(input_dev);
1510 kfree(data);
1511 return error;
1512}
1513
Iiro Valkonen7686b102011-02-02 23:21:58 -08001514static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001515{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001516 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001517
Iiro Valkonen7686b102011-02-02 23:21:58 -08001518 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001519 free_irq(data->irq, data);
1520 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301521#if defined(CONFIG_HAS_EARLYSUSPEND)
1522 unregister_early_suspend(&data->early_suspend);
1523#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301524
1525 if (data->pdata->power_on)
1526 data->pdata->power_on(false);
1527 else
1528 mxt_power_on(data, false);
1529
1530 if (data->pdata->init_hw)
1531 data->pdata->init_hw(false);
1532 else
1533 mxt_regulator_configure(data, false);
1534
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001535 kfree(data->object_table);
1536 kfree(data);
1537
1538 return 0;
1539}
1540
Iiro Valkonen7686b102011-02-02 23:21:58 -08001541static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001542 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001543 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001544 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001545 { }
1546};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001547MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001548
Iiro Valkonen7686b102011-02-02 23:21:58 -08001549static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001550 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001551 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001552 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001553#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001554 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001555#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001556 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001557 .probe = mxt_probe,
1558 .remove = __devexit_p(mxt_remove),
1559 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001560};
1561
Iiro Valkonen7686b102011-02-02 23:21:58 -08001562static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001563{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001564 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001565}
1566
Iiro Valkonen7686b102011-02-02 23:21:58 -08001567static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001568{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001569 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001570}
1571
Iiro Valkonen7686b102011-02-02 23:21:58 -08001572module_init(mxt_init);
1573module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001574
1575/* Module information */
1576MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001577MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001578MODULE_LICENSE("GPL");