blob: 4d9733140ad94f6ce2d54071330ea8ceb3bf62e5 [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>
Jing Lin2f863172011-10-17 10:56:58 -07006 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/firmware.h>
19#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080020#include <linux/i2c/atmel_mxt_ts.h>
Amy Maloche2b59bab2011-10-13 16:08:16 -070021#include <linux/input.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070022#include <linux/interrupt.h>
23#include <linux/slab.h>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053024#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070025
Anirudh Ghayal253ce122011-08-09 19:32:57 +053026#if defined(CONFIG_HAS_EARLYSUSPEND)
27#include <linux/earlysuspend.h>
28/* Early-suspend level */
29#define MXT_SUSPEND_LEVEL 1
30#endif
31
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070032/* Family ID */
33#define MXT224_ID 0x80
34#define MXT1386_ID 0xA0
35
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070036/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_VER_20 20
38#define MXT_VER_21 21
39#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070040
41/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080042#define MXT_APP_LOW 0x4a
43#define MXT_APP_HIGH 0x4b
44#define MXT_BOOT_LOW 0x24
45#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070046
47/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080048#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
50/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080051#define MXT_FAMILY_ID 0x00
52#define MXT_VARIANT_ID 0x01
53#define MXT_VERSION 0x02
54#define MXT_BUILD 0x03
55#define MXT_MATRIX_X_SIZE 0x04
56#define MXT_MATRIX_Y_SIZE 0x05
57#define MXT_OBJECT_NUM 0x06
58#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070059
Iiro Valkonen7686b102011-02-02 23:21:58 -080060#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070061
62/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080063#define MXT_DEBUG_DIAGNOSTIC 37
64#define MXT_GEN_MESSAGE 5
65#define MXT_GEN_COMMAND 6
66#define MXT_GEN_POWER 7
67#define MXT_GEN_ACQUIRE 8
68#define MXT_TOUCH_MULTI 9
69#define MXT_TOUCH_KEYARRAY 15
70#define MXT_TOUCH_PROXIMITY 23
71#define MXT_PROCI_GRIPFACE 20
72#define MXT_PROCG_NOISE 22
73#define MXT_PROCI_ONETOUCH 24
74#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070075#define MXT_PROCI_GRIP 40
76#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070077#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080078#define MXT_SPT_GPIOPWM 19
79#define MXT_SPT_SELFTEST 25
80#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070081#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070082#define MXT_SPT_DIGITIZER 43
83#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070084
Iiro Valkonen7686b102011-02-02 23:21:58 -080085/* MXT_GEN_COMMAND field */
86#define MXT_COMMAND_RESET 0
87#define MXT_COMMAND_BACKUPNV 1
88#define MXT_COMMAND_CALIBRATE 2
89#define MXT_COMMAND_REPORTALL 3
90#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen7686b102011-02-02 23:21:58 -080092/* MXT_GEN_POWER field */
93#define MXT_POWER_IDLEACQINT 0
94#define MXT_POWER_ACTVACQINT 1
95#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070096
Iiro Valkonen7686b102011-02-02 23:21:58 -080097/* MXT_GEN_ACQUIRE field */
98#define MXT_ACQUIRE_CHRGTIME 0
99#define MXT_ACQUIRE_TCHDRIFT 2
100#define MXT_ACQUIRE_DRIFTST 3
101#define MXT_ACQUIRE_TCHAUTOCAL 4
102#define MXT_ACQUIRE_SYNC 5
103#define MXT_ACQUIRE_ATCHCALST 6
104#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700105
Iiro Valkonen7686b102011-02-02 23:21:58 -0800106/* MXT_TOUCH_MULTI field */
107#define MXT_TOUCH_CTRL 0
108#define MXT_TOUCH_XORIGIN 1
109#define MXT_TOUCH_YORIGIN 2
110#define MXT_TOUCH_XSIZE 3
111#define MXT_TOUCH_YSIZE 4
112#define MXT_TOUCH_BLEN 6
113#define MXT_TOUCH_TCHTHR 7
114#define MXT_TOUCH_TCHDI 8
115#define MXT_TOUCH_ORIENT 9
116#define MXT_TOUCH_MOVHYSTI 11
117#define MXT_TOUCH_MOVHYSTN 12
118#define MXT_TOUCH_NUMTOUCH 14
119#define MXT_TOUCH_MRGHYST 15
120#define MXT_TOUCH_MRGTHR 16
121#define MXT_TOUCH_AMPHYST 17
122#define MXT_TOUCH_XRANGE_LSB 18
123#define MXT_TOUCH_XRANGE_MSB 19
124#define MXT_TOUCH_YRANGE_LSB 20
125#define MXT_TOUCH_YRANGE_MSB 21
126#define MXT_TOUCH_XLOCLIP 22
127#define MXT_TOUCH_XHICLIP 23
128#define MXT_TOUCH_YLOCLIP 24
129#define MXT_TOUCH_YHICLIP 25
130#define MXT_TOUCH_XEDGECTRL 26
131#define MXT_TOUCH_XEDGEDIST 27
132#define MXT_TOUCH_YEDGECTRL 28
133#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700134#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700135
Iiro Valkonen7686b102011-02-02 23:21:58 -0800136/* MXT_PROCI_GRIPFACE field */
137#define MXT_GRIPFACE_CTRL 0
138#define MXT_GRIPFACE_XLOGRIP 1
139#define MXT_GRIPFACE_XHIGRIP 2
140#define MXT_GRIPFACE_YLOGRIP 3
141#define MXT_GRIPFACE_YHIGRIP 4
142#define MXT_GRIPFACE_MAXTCHS 5
143#define MXT_GRIPFACE_SZTHR1 7
144#define MXT_GRIPFACE_SZTHR2 8
145#define MXT_GRIPFACE_SHPTHR1 9
146#define MXT_GRIPFACE_SHPTHR2 10
147#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700148
Iiro Valkonen7686b102011-02-02 23:21:58 -0800149/* MXT_PROCI_NOISE field */
150#define MXT_NOISE_CTRL 0
151#define MXT_NOISE_OUTFLEN 1
152#define MXT_NOISE_GCAFUL_LSB 3
153#define MXT_NOISE_GCAFUL_MSB 4
154#define MXT_NOISE_GCAFLL_LSB 5
155#define MXT_NOISE_GCAFLL_MSB 6
156#define MXT_NOISE_ACTVGCAFVALID 7
157#define MXT_NOISE_NOISETHR 8
158#define MXT_NOISE_FREQHOPSCALE 10
159#define MXT_NOISE_FREQ0 11
160#define MXT_NOISE_FREQ1 12
161#define MXT_NOISE_FREQ2 13
162#define MXT_NOISE_FREQ3 14
163#define MXT_NOISE_FREQ4 15
164#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700165
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166/* MXT_SPT_COMMSCONFIG */
167#define MXT_COMMS_CTRL 0
168#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700169
Iiro Valkonen7686b102011-02-02 23:21:58 -0800170/* MXT_SPT_CTECONFIG field */
171#define MXT_CTE_CTRL 0
172#define MXT_CTE_CMD 1
173#define MXT_CTE_MODE 2
174#define MXT_CTE_IDLEGCAFDEPTH 3
175#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700176#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700177
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_VOLTAGE_DEFAULT 2700000
179#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700180
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530181#define MXT_VTG_MIN_UV 2700000
182#define MXT_VTG_MAX_UV 3300000
183#define MXT_ACTIVE_LOAD_UA 15000
Jing Linbace50b2011-10-18 22:55:47 -0700184#define MXT_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530185
186#define MXT_I2C_VTG_MIN_UV 1800000
187#define MXT_I2C_VTG_MAX_UV 1800000
188#define MXT_I2C_LOAD_UA 10000
Jing Linbace50b2011-10-18 22:55:47 -0700189#define MXT_I2C_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530190
Iiro Valkonen7686b102011-02-02 23:21:58 -0800191/* Define for MXT_GEN_COMMAND */
192#define MXT_BOOT_VALUE 0xa5
193#define MXT_BACKUP_VALUE 0x55
194#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700195#define MXT224_RESET_TIME 65 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700196#define MXT1386_RESET_TIME 250 /* msec */
197#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700198#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700201
Jing Lin36aee812011-10-17 17:17:28 -0700202#define MXT_WAKE_TIME 25
203
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700204/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800205#define MXT_UNLOCK_CMD_MSB 0xaa
206#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700207
208/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800209#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
210#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
211#define MXT_FRAME_CRC_CHECK 0x02
212#define MXT_FRAME_CRC_FAIL 0x03
213#define MXT_FRAME_CRC_PASS 0x04
214#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
215#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700216
217/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218#define MXT_SUPPRESS (1 << 1)
219#define MXT_AMP (1 << 2)
220#define MXT_VECTOR (1 << 3)
221#define MXT_MOVE (1 << 4)
222#define MXT_RELEASE (1 << 5)
223#define MXT_PRESS (1 << 6)
224#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700225
Joonyoung Shim910d8052011-04-12 23:14:38 -0700226/* Touch orient bits */
227#define MXT_XY_SWITCH (1 << 0)
228#define MXT_X_INVERT (1 << 1)
229#define MXT_Y_INVERT (1 << 2)
230
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800232#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233
Iiro Valkonen7686b102011-02-02 23:21:58 -0800234#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530236#define MXT_BUFF_SIZE 100
Jing Lin36aee812011-10-17 17:17:28 -0700237#define T7_DATA_SIZE 3
238#define MXT_MAX_RW_TRIES 3
239#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530240
Iiro Valkonen7686b102011-02-02 23:21:58 -0800241struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700242 u8 family_id;
243 u8 variant_id;
244 u8 version;
245 u8 build;
246 u8 matrix_xsize;
247 u8 matrix_ysize;
248 u8 object_num;
249};
250
Iiro Valkonen7686b102011-02-02 23:21:58 -0800251struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700252 u8 type;
253 u16 start_address;
254 u8 size;
255 u8 instances;
256 u8 num_report_ids;
257
258 /* to map object and message */
259 u8 max_reportid;
260};
261
Iiro Valkonen7686b102011-02-02 23:21:58 -0800262struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700263 u8 reportid;
264 u8 message[7];
265 u8 checksum;
266};
267
Iiro Valkonen7686b102011-02-02 23:21:58 -0800268struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700269 int status;
270 int x;
271 int y;
272 int area;
273};
274
275/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800276struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700277 struct i2c_client *client;
278 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800279 const struct mxt_platform_data *pdata;
280 struct mxt_object *object_table;
281 struct mxt_info info;
282 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700283 unsigned int irq;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530284 struct regulator *vcc;
285 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530286#if defined(CONFIG_HAS_EARLYSUSPEND)
287 struct early_suspend early_suspend;
288#endif
Jing Lin36aee812011-10-17 17:17:28 -0700289
Amy Maloche52262212011-09-15 16:46:57 -0700290 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700291 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700292 u8 t9_ctrl;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700293};
294
Iiro Valkonen7686b102011-02-02 23:21:58 -0800295static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700296{
297 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800298 case MXT_GEN_MESSAGE:
299 case MXT_GEN_COMMAND:
300 case MXT_GEN_POWER:
301 case MXT_GEN_ACQUIRE:
302 case MXT_TOUCH_MULTI:
303 case MXT_TOUCH_KEYARRAY:
304 case MXT_TOUCH_PROXIMITY:
305 case MXT_PROCI_GRIPFACE:
306 case MXT_PROCG_NOISE:
307 case MXT_PROCI_ONETOUCH:
308 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700309 case MXT_PROCI_GRIP:
310 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800311 case MXT_SPT_COMMSCONFIG:
312 case MXT_SPT_GPIOPWM:
313 case MXT_SPT_SELFTEST:
314 case MXT_SPT_CTECONFIG:
315 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530316 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700317 return true;
318 default:
319 return false;
320 }
321}
322
Iiro Valkonen7686b102011-02-02 23:21:58 -0800323static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700324{
325 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800326 case MXT_GEN_COMMAND:
327 case MXT_GEN_POWER:
328 case MXT_GEN_ACQUIRE:
329 case MXT_TOUCH_MULTI:
330 case MXT_TOUCH_KEYARRAY:
331 case MXT_TOUCH_PROXIMITY:
332 case MXT_PROCI_GRIPFACE:
333 case MXT_PROCG_NOISE:
334 case MXT_PROCI_ONETOUCH:
335 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700336 case MXT_PROCI_GRIP:
337 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800338 case MXT_SPT_GPIOPWM:
339 case MXT_SPT_SELFTEST:
340 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530341 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530342 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700343 return true;
344 default:
345 return false;
346 }
347}
348
Iiro Valkonen7686b102011-02-02 23:21:58 -0800349static void mxt_dump_message(struct device *dev,
350 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700351{
352 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
353 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
354 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
355 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
356 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
357 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
358 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
359 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
360 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
361}
362
Iiro Valkonen7686b102011-02-02 23:21:58 -0800363static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700364 unsigned int state)
365{
366 u8 val;
367
368recheck:
369 if (i2c_master_recv(client, &val, 1) != 1) {
370 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
371 return -EIO;
372 }
373
374 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800375 case MXT_WAITING_BOOTLOAD_CMD:
376 case MXT_WAITING_FRAME_DATA:
377 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700378 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800379 case MXT_FRAME_CRC_PASS:
380 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700381 goto recheck;
382 break;
383 default:
384 return -EINVAL;
385 }
386
387 if (val != state) {
388 dev_err(&client->dev, "Unvalid bootloader mode state\n");
389 return -EINVAL;
390 }
391
392 return 0;
393}
394
Iiro Valkonen7686b102011-02-02 23:21:58 -0800395static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700396{
397 u8 buf[2];
398
Iiro Valkonen7686b102011-02-02 23:21:58 -0800399 buf[0] = MXT_UNLOCK_CMD_LSB;
400 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700401
402 if (i2c_master_send(client, buf, 2) != 2) {
403 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
404 return -EIO;
405 }
406
407 return 0;
408}
409
Iiro Valkonen7686b102011-02-02 23:21:58 -0800410static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700411 const u8 *data, unsigned int frame_size)
412{
413 if (i2c_master_send(client, data, frame_size) != frame_size) {
414 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
415 return -EIO;
416 }
417
418 return 0;
419}
420
Iiro Valkonen7686b102011-02-02 23:21:58 -0800421static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700422 u16 reg, u16 len, void *val)
423{
424 struct i2c_msg xfer[2];
425 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700426 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700427
428 buf[0] = reg & 0xff;
429 buf[1] = (reg >> 8) & 0xff;
430
431 /* Write register */
432 xfer[0].addr = client->addr;
433 xfer[0].flags = 0;
434 xfer[0].len = 2;
435 xfer[0].buf = buf;
436
437 /* Read data */
438 xfer[1].addr = client->addr;
439 xfer[1].flags = I2C_M_RD;
440 xfer[1].len = len;
441 xfer[1].buf = val;
442
Jing Lin36aee812011-10-17 17:17:28 -0700443 do {
444 if (i2c_transfer(client->adapter, xfer, 2) == 2)
445 return 0;
446 msleep(MXT_WAKE_TIME);
447 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700448
Jing Lin36aee812011-10-17 17:17:28 -0700449 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
450 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700451}
452
Iiro Valkonen7686b102011-02-02 23:21:58 -0800453static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700454{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800455 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700456}
457
Jing Lin36aee812011-10-17 17:17:28 -0700458static int __mxt_write_reg(struct i2c_client *client,
459 u16 addr, u16 length, u8 *value)
460{
461 u8 buf[MXT_BLOCK_SIZE + 2];
462 int i, tries = 0;
463
464 if (length > MXT_BLOCK_SIZE)
465 return -EINVAL;
466
467 buf[0] = addr & 0xff;
468 buf[1] = (addr >> 8) & 0xff;
469 for (i = 0; i < length; i++)
470 buf[i + 2] = *value++;
471
472 do {
473 if (i2c_master_send(client, buf, length + 2) == (length + 2))
474 return 0;
475 msleep(MXT_WAKE_TIME);
476 } while (++tries < MXT_MAX_RW_TRIES);
477
478 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
479 return -EIO;
480}
481
Iiro Valkonen7686b102011-02-02 23:21:58 -0800482static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483{
Jing Lin36aee812011-10-17 17:17:28 -0700484 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485}
486
Iiro Valkonen7686b102011-02-02 23:21:58 -0800487static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488 u16 reg, u8 *object_buf)
489{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491 object_buf);
492}
493
Iiro Valkonen7686b102011-02-02 23:21:58 -0800494static struct mxt_object *
495mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700496{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800497 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498 int i;
499
500 for (i = 0; i < data->info.object_num; i++) {
501 object = data->object_table + i;
502 if (object->type == type)
503 return object;
504 }
505
506 dev_err(&data->client->dev, "Invalid object type\n");
507 return NULL;
508}
509
Iiro Valkonen7686b102011-02-02 23:21:58 -0800510static int mxt_read_message(struct mxt_data *data,
511 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700512{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800513 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514 u16 reg;
515
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517 if (!object)
518 return -EINVAL;
519
520 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800521 return __mxt_read_reg(data->client, reg,
522 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700523}
524
Iiro Valkonen7686b102011-02-02 23:21:58 -0800525static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700526 u8 type, u8 offset, u8 *val)
527{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800528 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700529 u16 reg;
530
Iiro Valkonen7686b102011-02-02 23:21:58 -0800531 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532 if (!object)
533 return -EINVAL;
534
535 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800536 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700537}
538
Iiro Valkonen7686b102011-02-02 23:21:58 -0800539static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700540 u8 type, u8 offset, u8 val)
541{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800542 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700543 u16 reg;
544
Iiro Valkonen7686b102011-02-02 23:21:58 -0800545 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700546 if (!object)
547 return -EINVAL;
548
549 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800550 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700551}
552
Iiro Valkonen7686b102011-02-02 23:21:58 -0800553static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800555 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700556 struct input_dev *input_dev = data->input_dev;
557 int status = finger[single_id].status;
558 int finger_num = 0;
559 int id;
560
Iiro Valkonen7686b102011-02-02 23:21:58 -0800561 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700562 if (!finger[id].status)
563 continue;
564
Amy Maloche2b59bab2011-10-13 16:08:16 -0700565 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
566 finger[id].status != MXT_RELEASE ?
567 finger[id].area : 0);
568 input_report_abs(input_dev, ABS_MT_POSITION_X,
569 finger[id].x);
570 input_report_abs(input_dev, ABS_MT_POSITION_Y,
571 finger[id].y);
572 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700573
Amy Maloche2b59bab2011-10-13 16:08:16 -0700574 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700575 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700576 else
577 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700578 }
579
580 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
581
Iiro Valkonen7686b102011-02-02 23:21:58 -0800582 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700583 input_report_abs(input_dev, ABS_X, finger[single_id].x);
584 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
585 }
586
587 input_sync(input_dev);
588}
589
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590static void mxt_input_touchevent(struct mxt_data *data,
591 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800593 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594 struct device *dev = &data->client->dev;
595 u8 status = message->message[0];
596 int x;
597 int y;
598 int area;
599
600 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800601 if (!(status & MXT_DETECT)) {
602 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603 dev_dbg(dev, "[%d] released\n", id);
604
Iiro Valkonen7686b102011-02-02 23:21:58 -0800605 finger[id].status = MXT_RELEASE;
606 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700607 }
608 return;
609 }
610
611 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800612 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700613 return;
614
Joonyoung Shim910d8052011-04-12 23:14:38 -0700615 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
616 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700617 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700618 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700619 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700620 y = y >> 2;
621
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700622 area = message->message[4];
623
624 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800625 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700626 x, y, area);
627
Iiro Valkonen7686b102011-02-02 23:21:58 -0800628 finger[id].status = status & MXT_MOVE ?
629 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700630 finger[id].x = x;
631 finger[id].y = y;
632 finger[id].area = area;
633
Iiro Valkonen7686b102011-02-02 23:21:58 -0800634 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635}
636
Iiro Valkonen7686b102011-02-02 23:21:58 -0800637static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700638{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800639 struct mxt_data *data = dev_id;
640 struct mxt_message message;
641 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700642 struct device *dev = &data->client->dev;
643 int id;
644 u8 reportid;
645 u8 max_reportid;
646 u8 min_reportid;
647
648 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800649 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700650 dev_err(dev, "Failed to read message\n");
651 goto end;
652 }
653
654 reportid = message.reportid;
655
Iiro Valkonen7686b102011-02-02 23:21:58 -0800656 /* whether reportid is thing of MXT_TOUCH_MULTI */
657 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658 if (!object)
659 goto end;
660
661 max_reportid = object->max_reportid;
662 min_reportid = max_reportid - object->num_report_ids + 1;
663 id = reportid - min_reportid;
664
665 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800666 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800668 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669 } while (reportid != 0xff);
670
671end:
672 return IRQ_HANDLED;
673}
674
Iiro Valkonen7686b102011-02-02 23:21:58 -0800675static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700676{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800677 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800678 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679 struct device *dev = &data->client->dev;
680 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800681 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700682
Iiro Valkonen71749f52011-02-15 13:36:52 -0800683 if (!pdata->config) {
684 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
685 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686 }
687
688 for (i = 0; i < data->info.object_num; i++) {
689 object = data->object_table + i;
690
Iiro Valkonen7686b102011-02-02 23:21:58 -0800691 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692 continue;
693
Iiro Valkonen71749f52011-02-15 13:36:52 -0800694 for (j = 0; j < object->size + 1; j++) {
695 config_offset = index + j;
696 if (config_offset > pdata->config_length) {
697 dev_err(dev, "Not enough config data!\n");
698 return -EINVAL;
699 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800700 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800701 pdata->config[config_offset]);
702 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700703 index += object->size + 1;
704 }
705
706 return 0;
707}
708
Iiro Valkonen7686b102011-02-02 23:21:58 -0800709static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700710{
711 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800712 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700713 int count = 10;
714 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715
716 /* Read dummy message to make high CHG pin */
717 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800718 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719 if (error)
720 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800721 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700722
723 if (!count) {
724 dev_err(dev, "CHG pin isn't cleared\n");
725 return -EBUSY;
726 }
727
728 return 0;
729}
730
Iiro Valkonen7686b102011-02-02 23:21:58 -0800731static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700732{
733 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800734 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700735 int error;
736 u8 val;
737
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739 if (error)
740 return error;
741 info->family_id = val;
742
Iiro Valkonen7686b102011-02-02 23:21:58 -0800743 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700744 if (error)
745 return error;
746 info->variant_id = val;
747
Iiro Valkonen7686b102011-02-02 23:21:58 -0800748 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749 if (error)
750 return error;
751 info->version = val;
752
Iiro Valkonen7686b102011-02-02 23:21:58 -0800753 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700754 if (error)
755 return error;
756 info->build = val;
757
Iiro Valkonen7686b102011-02-02 23:21:58 -0800758 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700759 if (error)
760 return error;
761 info->object_num = val;
762
763 return 0;
764}
765
Iiro Valkonen7686b102011-02-02 23:21:58 -0800766static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700767{
768 int error;
769 int i;
770 u16 reg;
771 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800772 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700773
774 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700776
Iiro Valkonen7686b102011-02-02 23:21:58 -0800777 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
778 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779 if (error)
780 return error;
781
782 object->type = buf[0];
783 object->start_address = (buf[2] << 8) | buf[1];
784 object->size = buf[3];
785 object->instances = buf[4];
786 object->num_report_ids = buf[5];
787
788 if (object->num_report_ids) {
789 reportid += object->num_report_ids *
790 (object->instances + 1);
791 object->max_reportid = reportid;
792 }
793 }
794
795 return 0;
796}
797
Amy Maloche7e447432011-09-14 11:36:30 -0700798static void mxt_reset_delay(struct mxt_data *data)
799{
800 struct mxt_info *info = &data->info;
801
802 switch (info->family_id) {
803 case MXT224_ID:
804 msleep(MXT224_RESET_TIME);
805 break;
806 case MXT1386_ID:
807 msleep(MXT1386_RESET_TIME);
808 break;
809 default:
810 msleep(MXT_RESET_TIME);
811 }
812}
813
Iiro Valkonen7686b102011-02-02 23:21:58 -0800814static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700815{
816 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700818 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700819 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700820 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700821 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700822 struct mxt_object *t7_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700823
Iiro Valkonen7686b102011-02-02 23:21:58 -0800824 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700825 if (error)
826 return error;
827
828 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800829 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700830 GFP_KERNEL);
831 if (!data->object_table) {
832 dev_err(&client->dev, "Failed to allocate memory\n");
833 return -ENOMEM;
834 }
835
836 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800837 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700838 if (error)
839 return error;
840
841 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800842 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700843 if (error)
844 return error;
845
Amy Maloche52262212011-09-15 16:46:57 -0700846 /* Store T7 and T9 locally, used in suspend/resume operations */
Jing Lin36aee812011-10-17 17:17:28 -0700847 t7_object = mxt_get_object(data, MXT_GEN_POWER);
848 if (!t7_object) {
849 dev_err(&client->dev, "Failed to get T7 object\n");
850 return -EINVAL;
851 }
852
853 data->t7_start_addr = t7_object->start_address;
854 error = __mxt_read_reg(client, data->t7_start_addr,
855 T7_DATA_SIZE, data->t7_data);
856 if (error < 0) {
857 dev_err(&client->dev,
858 "failed to save current power state\n");
859 return error;
Amy Maloche52262212011-09-15 16:46:57 -0700860 }
861 error = mxt_read_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL,
862 &data->t9_ctrl);
863 if (error < 0) {
864 dev_err(&client->dev, "failed to save current touch object\n");
865 return error;
866 }
867
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700868 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800869 mxt_write_object(data, MXT_GEN_COMMAND,
870 MXT_COMMAND_BACKUPNV,
871 MXT_BACKUP_VALUE);
872 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700873 do {
874 error = mxt_read_object(data, MXT_GEN_COMMAND,
875 MXT_COMMAND_BACKUPNV,
876 &command_register);
877 if (error)
878 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700879 usleep_range(1000, 2000);
880 } while ((command_register != 0) && (++timeout_counter <= 100));
881 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700882 dev_err(&client->dev, "No response after backup!\n");
883 return -EIO;
884 }
885
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700886
887 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800888 mxt_write_object(data, MXT_GEN_COMMAND,
889 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700890
Amy Maloche7e447432011-09-14 11:36:30 -0700891 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700892
893 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800894 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700895 if (error)
896 return error;
897 info->matrix_xsize = val;
898
Iiro Valkonen7686b102011-02-02 23:21:58 -0800899 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700900 if (error)
901 return error;
902 info->matrix_ysize = val;
903
904 dev_info(&client->dev,
905 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
906 info->family_id, info->variant_id, info->version,
907 info->build);
908
909 dev_info(&client->dev,
910 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
911 info->matrix_xsize, info->matrix_ysize,
912 info->object_num);
913
914 return 0;
915}
916
Iiro Valkonen7686b102011-02-02 23:21:58 -0800917static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700918 struct device_attribute *attr, char *buf)
919{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800920 struct mxt_data *data = dev_get_drvdata(dev);
921 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700922 int count = 0;
923 int i, j;
924 int error;
925 u8 val;
926
927 for (i = 0; i < data->info.object_num; i++) {
928 object = data->object_table + i;
929
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530930 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931 "Object Table Element %d(Type %d)\n",
932 i + 1, object->type);
933
Iiro Valkonen7686b102011-02-02 23:21:58 -0800934 if (!mxt_object_readable(object->type)) {
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530935 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700936 continue;
937 }
938
939 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800940 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700941 object->type, j, &val);
942 if (error)
943 return error;
944
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530945 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946 " Byte %d: 0x%x (%d)\n", j, val, val);
947 }
948
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530949 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950 }
951
952 return count;
953}
954
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958 struct i2c_client *client = data->client;
959 const struct firmware *fw = NULL;
960 unsigned int frame_size;
961 unsigned int pos = 0;
962 int ret;
963
964 ret = request_firmware(&fw, fn, dev);
965 if (ret) {
966 dev_err(dev, "Unable to open firmware %s\n", fn);
967 return ret;
968 }
969
970 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800971 mxt_write_object(data, MXT_GEN_COMMAND,
972 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700973
974 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700975
976 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800977 if (client->addr == MXT_APP_LOW)
978 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700979 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800980 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700981
Iiro Valkonen7686b102011-02-02 23:21:58 -0800982 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983 if (ret)
984 goto out;
985
986 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800987 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700988
989 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800990 ret = mxt_check_bootloader(client,
991 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700992 if (ret)
993 goto out;
994
995 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
996
997 /* We should add 2 at frame size as the the firmware data is not
998 * included the CRC bytes.
999 */
1000 frame_size += 2;
1001
1002 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001003 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001004
Iiro Valkonen7686b102011-02-02 23:21:58 -08001005 ret = mxt_check_bootloader(client,
1006 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 if (ret)
1008 goto out;
1009
1010 pos += frame_size;
1011
1012 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1013 }
1014
1015out:
1016 release_firmware(fw);
1017
1018 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001019 if (client->addr == MXT_BOOT_LOW)
1020 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001023
1024 return ret;
1025}
1026
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001028 struct device_attribute *attr,
1029 const char *buf, size_t count)
1030{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001031 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001032 int error;
1033
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001034 disable_irq(data->irq);
1035
Iiro Valkonen7686b102011-02-02 23:21:58 -08001036 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001037 if (error) {
1038 dev_err(dev, "The firmware update failed(%d)\n", error);
1039 count = error;
1040 } else {
1041 dev_dbg(dev, "The firmware update succeeded\n");
1042
1043 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001044 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045
1046 kfree(data->object_table);
1047 data->object_table = NULL;
1048
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050 }
1051
1052 enable_irq(data->irq);
1053
Iiro Valkonen08960a02011-04-12 23:16:40 -07001054 error = mxt_make_highchg(data);
1055 if (error)
1056 return error;
1057
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001058 return count;
1059}
1060
Iiro Valkonen7686b102011-02-02 23:21:58 -08001061static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1062static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063
Iiro Valkonen7686b102011-02-02 23:21:58 -08001064static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065 &dev_attr_object.attr,
1066 &dev_attr_update_fw.attr,
1067 NULL
1068};
1069
Iiro Valkonen7686b102011-02-02 23:21:58 -08001070static const struct attribute_group mxt_attr_group = {
1071 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001072};
1073
Amy Maloche52262212011-09-15 16:46:57 -07001074static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001075{
Jing Lin36aee812011-10-17 17:17:28 -07001076 int error;
1077
Amy Maloche52262212011-09-15 16:46:57 -07001078 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001079 error = __mxt_write_reg(data->client, data->t7_start_addr,
1080 T7_DATA_SIZE, data->t7_data);
1081 if (error < 0) {
1082 dev_err(&data->client->dev,
1083 "failed to restore old power state\n");
1084 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001085 }
Jing Lin36aee812011-10-17 17:17:28 -07001086
Amy Maloche52262212011-09-15 16:46:57 -07001087 error = mxt_write_object(data,
1088 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1089 if (error < 0) {
1090 dev_err(&data->client->dev, "failed to restore touch\n");
1091 return error;
1092 }
1093
1094 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095}
1096
Amy Maloche52262212011-09-15 16:46:57 -07001097static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001098{
Jing Lin36aee812011-10-17 17:17:28 -07001099 int error;
1100 u8 t7_data[T7_DATA_SIZE] = {0};
1101
1102 /* disable touch and configure deep sleep mode */
1103 error = mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
1104 if (error < 0) {
1105 dev_err(&data->client->dev, "failed to disable touch\n");
1106 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001107 }
1108
Jing Lin36aee812011-10-17 17:17:28 -07001109 error = __mxt_write_reg(data->client, data->t7_start_addr,
1110 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001111 if (error < 0) {
1112 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001113 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001114 return error;
1115 }
1116
1117 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001118}
1119
Iiro Valkonen7686b102011-02-02 23:21:58 -08001120static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001121{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001122 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001123 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001124
Amy Maloche52262212011-09-15 16:46:57 -07001125 error = mxt_start(data);
1126 if (error < 0) {
1127 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1128 return error;
1129 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001130
1131 return 0;
1132}
1133
Iiro Valkonen7686b102011-02-02 23:21:58 -08001134static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001135{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001136 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001137 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001138
Amy Maloche52262212011-09-15 16:46:57 -07001139 error = mxt_stop(data);
1140 if (error < 0)
1141 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1142
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143}
1144
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301145static int mxt_power_on(struct mxt_data *data, bool on)
1146{
1147 int rc;
1148
1149 if (on == false)
1150 goto power_off;
1151
1152 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1153 if (rc < 0) {
1154 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1155 rc);
1156 return rc;
1157 }
1158
1159 rc = regulator_enable(data->vcc);
1160 if (rc) {
1161 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1162 rc);
1163 goto error_reg_en_vcc;
1164 }
1165
1166 if (data->pdata->i2c_pull_up) {
1167 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1168 if (rc < 0) {
1169 dev_err(&data->client->dev,
1170 "Regulator set_opt failed rc=%d\n", rc);
1171 goto error_reg_opt_i2c;
1172 }
1173
1174 rc = regulator_enable(data->vcc_i2c);
1175 if (rc) {
1176 dev_err(&data->client->dev,
1177 "Regulator enable failed rc=%d\n", rc);
1178 goto error_reg_en_vcc_i2c;
1179 }
1180 }
1181
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001182 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301183
1184 return 0;
1185
1186error_reg_en_vcc_i2c:
1187 if (data->pdata->i2c_pull_up)
1188 regulator_set_optimum_mode(data->vcc_i2c, 0);
1189error_reg_opt_i2c:
1190 regulator_disable(data->vcc);
1191error_reg_en_vcc:
1192 regulator_set_optimum_mode(data->vcc, 0);
1193 return rc;
1194
1195power_off:
1196 regulator_set_optimum_mode(data->vcc, 0);
1197 regulator_disable(data->vcc);
1198 if (data->pdata->i2c_pull_up) {
1199 regulator_set_optimum_mode(data->vcc_i2c, 0);
1200 regulator_disable(data->vcc_i2c);
1201 }
1202 msleep(50);
1203 return 0;
1204}
1205
1206static int mxt_regulator_configure(struct mxt_data *data, bool on)
1207{
1208 int rc;
1209
1210 if (on == false)
1211 goto hw_shutdown;
1212
1213 data->vcc = regulator_get(&data->client->dev, "vdd");
1214 if (IS_ERR(data->vcc)) {
1215 rc = PTR_ERR(data->vcc);
1216 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1217 rc);
1218 return rc;
1219 }
1220
1221 if (regulator_count_voltages(data->vcc) > 0) {
1222 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1223 MXT_VTG_MAX_UV);
1224 if (rc) {
1225 dev_err(&data->client->dev,
1226 "regulator set_vtg failed rc=%d\n", rc);
1227 goto error_set_vtg_vcc;
1228 }
1229 }
1230
1231 if (data->pdata->i2c_pull_up) {
1232 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1233 if (IS_ERR(data->vcc_i2c)) {
1234 rc = PTR_ERR(data->vcc_i2c);
1235 dev_err(&data->client->dev,
1236 "Regulator get failed rc=%d\n", rc);
1237 goto error_get_vtg_i2c;
1238 }
1239 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1240 rc = regulator_set_voltage(data->vcc_i2c,
1241 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1242 if (rc) {
1243 dev_err(&data->client->dev,
1244 "regulator set_vtg failed rc=%d\n", rc);
1245 goto error_set_vtg_i2c;
1246 }
1247 }
1248 }
1249
1250 return 0;
1251
1252error_set_vtg_i2c:
1253 regulator_put(data->vcc_i2c);
1254error_get_vtg_i2c:
1255 if (regulator_count_voltages(data->vcc) > 0)
1256 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1257error_set_vtg_vcc:
1258 regulator_put(data->vcc);
1259 return rc;
1260
1261hw_shutdown:
1262 if (regulator_count_voltages(data->vcc) > 0)
1263 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1264 regulator_put(data->vcc);
1265 if (data->pdata->i2c_pull_up) {
1266 if (regulator_count_voltages(data->vcc_i2c) > 0)
1267 regulator_set_voltage(data->vcc_i2c, 0,
1268 MXT_I2C_VTG_MAX_UV);
1269 regulator_put(data->vcc_i2c);
1270 }
1271 return 0;
1272}
1273
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301274#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001275static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1276{
1277
1278 int rc;
1279
1280 if (on == false)
1281 goto regulator_hpm;
1282
1283 rc = regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1284 if (rc < 0) {
1285 dev_err(&data->client->dev,
1286 "Regulator set_opt failed rc=%d\n", rc);
1287 goto fail_regulator_lpm;
1288 }
1289
1290 if (data->pdata->i2c_pull_up) {
1291 rc = regulator_set_optimum_mode(data->vcc_i2c,
1292 MXT_I2C_LPM_LOAD_UA);
1293 if (rc < 0) {
1294 dev_err(&data->client->dev,
1295 "Regulator set_opt failed rc=%d\n", rc);
1296 goto fail_regulator_lpm;
1297 }
1298 }
1299
1300 return 0;
1301
1302regulator_hpm:
1303
1304 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1305 if (rc < 0) {
1306 dev_err(&data->client->dev,
1307 "Regulator set_opt failed rc=%d\n", rc);
1308 goto fail_regulator_hpm;
1309 }
1310
1311 if (data->pdata->i2c_pull_up) {
1312 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1313 if (rc < 0) {
1314 dev_err(&data->client->dev,
1315 "Regulator set_opt failed rc=%d\n", rc);
1316 goto fail_regulator_hpm;
1317 }
1318 }
1319
1320 return 0;
1321
1322fail_regulator_lpm:
1323 regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1324 if (data->pdata->i2c_pull_up)
1325 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1326
1327 return rc;
1328
1329fail_regulator_hpm:
1330 regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1331 if (data->pdata->i2c_pull_up)
1332 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1333
1334 return rc;
1335}
1336
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301337static int mxt_suspend(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
1344 mutex_lock(&input_dev->mutex);
1345
Amy Maloche52262212011-09-15 16:46:57 -07001346 if (input_dev->users) {
1347 error = mxt_stop(data);
1348 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001349 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001350 mutex_unlock(&input_dev->mutex);
1351 return error;
1352 }
1353
1354 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301355
1356 mutex_unlock(&input_dev->mutex);
1357
Jing Linbace50b2011-10-18 22:55:47 -07001358 /* put regulators in low power mode */
1359 error = mxt_regulator_lpm(data, true);
1360 if (error < 0) {
1361 dev_err(dev, "failed to enter low power mode\n");
1362 return error;
1363 }
1364
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301365 return 0;
1366}
1367
1368static int mxt_resume(struct device *dev)
1369{
1370 struct i2c_client *client = to_i2c_client(dev);
1371 struct mxt_data *data = i2c_get_clientdata(client);
1372 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001373 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301374
Jing Linbace50b2011-10-18 22:55:47 -07001375 /* put regulators in high power mode */
1376 error = mxt_regulator_lpm(data, false);
1377 if (error < 0) {
1378 dev_err(dev, "failed to enter high power mode\n");
1379 return error;
1380 }
1381
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301382 mutex_lock(&input_dev->mutex);
1383
Amy Maloche52262212011-09-15 16:46:57 -07001384 if (input_dev->users) {
1385 error = mxt_start(data);
1386 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001387 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001388 mutex_unlock(&input_dev->mutex);
1389 return error;
1390 }
1391 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301392
1393 mutex_unlock(&input_dev->mutex);
1394
1395 return 0;
1396}
1397
1398#if defined(CONFIG_HAS_EARLYSUSPEND)
1399static void mxt_early_suspend(struct early_suspend *h)
1400{
1401 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1402
1403 mxt_suspend(&data->client->dev);
1404}
1405
1406static void mxt_late_resume(struct early_suspend *h)
1407{
1408 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1409
1410 mxt_resume(&data->client->dev);
1411}
1412#endif
1413
1414static const struct dev_pm_ops mxt_pm_ops = {
1415#ifndef CONFIG_HAS_EARLYSUSPEND
1416 .suspend = mxt_suspend,
1417 .resume = mxt_resume,
1418#endif
1419};
1420#endif
1421
Iiro Valkonen7686b102011-02-02 23:21:58 -08001422static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001423 const struct i2c_device_id *id)
1424{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001425 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001426 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001427 struct input_dev *input_dev;
1428 int error;
1429
Iiro Valkonen919ed892011-02-15 13:36:52 -08001430 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001431 return -EINVAL;
1432
Iiro Valkonen7686b102011-02-02 23:21:58 -08001433 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001434 input_dev = input_allocate_device();
1435 if (!data || !input_dev) {
1436 dev_err(&client->dev, "Failed to allocate memory\n");
1437 error = -ENOMEM;
1438 goto err_free_mem;
1439 }
1440
Iiro Valkonen7686b102011-02-02 23:21:58 -08001441 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001442 input_dev->id.bustype = BUS_I2C;
1443 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001444 input_dev->open = mxt_input_open;
1445 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001446
Joonyoung Shim910d8052011-04-12 23:14:38 -07001447 data->client = client;
1448 data->input_dev = input_dev;
1449 data->pdata = pdata;
1450 data->irq = client->irq;
1451
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001452 __set_bit(EV_ABS, input_dev->evbit);
1453 __set_bit(EV_KEY, input_dev->evbit);
1454 __set_bit(BTN_TOUCH, input_dev->keybit);
1455
1456 /* For single touch */
1457 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001458 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001459 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001460 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001461
1462 /* For multi touch */
1463 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001464 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001465 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001466 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001467 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001468 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001469
1470 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001471 i2c_set_clientdata(client, data);
1472
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301473 if (pdata->init_hw)
1474 error = pdata->init_hw(true);
1475 else
1476 error = mxt_regulator_configure(data, true);
1477 if (error) {
1478 dev_err(&client->dev, "Failed to intialize hardware\n");
1479 goto err_free_object;
1480 }
1481
1482 if (pdata->power_on)
1483 error = pdata->power_on(true);
1484 else
1485 error = mxt_power_on(data, true);
1486 if (error) {
1487 dev_err(&client->dev, "Failed to power on hardware\n");
1488 goto err_regulator_on;
1489 }
1490
Iiro Valkonen7686b102011-02-02 23:21:58 -08001491 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001492 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301493 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001494
Iiro Valkonen7686b102011-02-02 23:21:58 -08001495 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001496 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001497 if (error) {
1498 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301499 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001500 }
1501
Iiro Valkonen08960a02011-04-12 23:16:40 -07001502 error = mxt_make_highchg(data);
1503 if (error)
1504 goto err_free_irq;
1505
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001506 error = input_register_device(input_dev);
1507 if (error)
1508 goto err_free_irq;
1509
Iiro Valkonen7686b102011-02-02 23:21:58 -08001510 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001511 if (error)
1512 goto err_unregister_device;
1513
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301514#if defined(CONFIG_HAS_EARLYSUSPEND)
1515 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1516 MXT_SUSPEND_LEVEL;
1517 data->early_suspend.suspend = mxt_early_suspend;
1518 data->early_suspend.resume = mxt_late_resume;
1519 register_early_suspend(&data->early_suspend);
1520#endif
1521
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001522 return 0;
1523
1524err_unregister_device:
1525 input_unregister_device(input_dev);
1526 input_dev = NULL;
1527err_free_irq:
1528 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301529err_power_on:
1530 if (pdata->power_on)
1531 pdata->power_on(false);
1532 else
1533 mxt_power_on(data, false);
1534err_regulator_on:
1535 if (pdata->init_hw)
1536 pdata->init_hw(false);
1537 else
1538 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001539err_free_object:
1540 kfree(data->object_table);
1541err_free_mem:
1542 input_free_device(input_dev);
1543 kfree(data);
1544 return error;
1545}
1546
Iiro Valkonen7686b102011-02-02 23:21:58 -08001547static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001548{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001549 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001550
Iiro Valkonen7686b102011-02-02 23:21:58 -08001551 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001552 free_irq(data->irq, data);
1553 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301554#if defined(CONFIG_HAS_EARLYSUSPEND)
1555 unregister_early_suspend(&data->early_suspend);
1556#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301557
1558 if (data->pdata->power_on)
1559 data->pdata->power_on(false);
1560 else
1561 mxt_power_on(data, false);
1562
1563 if (data->pdata->init_hw)
1564 data->pdata->init_hw(false);
1565 else
1566 mxt_regulator_configure(data, false);
1567
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001568 kfree(data->object_table);
1569 kfree(data);
1570
1571 return 0;
1572}
1573
Iiro Valkonen7686b102011-02-02 23:21:58 -08001574static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001575 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001576 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001577 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001578 { }
1579};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001580MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001581
Iiro Valkonen7686b102011-02-02 23:21:58 -08001582static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001583 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001584 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001585 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001586#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001587 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001588#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001589 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001590 .probe = mxt_probe,
1591 .remove = __devexit_p(mxt_remove),
1592 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001593};
1594
Iiro Valkonen7686b102011-02-02 23:21:58 -08001595static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001596{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001597 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001598}
1599
Iiro Valkonen7686b102011-02-02 23:21:58 -08001600static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001601{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001602 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001603}
1604
Iiro Valkonen7686b102011-02-02 23:21:58 -08001605module_init(mxt_init);
1606module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001607
1608/* Module information */
1609MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001610MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001611MODULE_LICENSE("GPL");