blob: 064aff4658a820a9ab3057a97943f0da09a806f5 [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
Jing Lin36aee812011-10-17 17:17:28 -0700236#define T7_DATA_SIZE 3
237#define MXT_MAX_RW_TRIES 3
238#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530239
Iiro Valkonen7686b102011-02-02 23:21:58 -0800240struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700241 u8 family_id;
242 u8 variant_id;
243 u8 version;
244 u8 build;
245 u8 matrix_xsize;
246 u8 matrix_ysize;
247 u8 object_num;
248};
249
Iiro Valkonen7686b102011-02-02 23:21:58 -0800250struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700251 u8 type;
252 u16 start_address;
253 u8 size;
254 u8 instances;
255 u8 num_report_ids;
256
257 /* to map object and message */
258 u8 max_reportid;
259};
260
Iiro Valkonen7686b102011-02-02 23:21:58 -0800261struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700262 u8 reportid;
263 u8 message[7];
264 u8 checksum;
265};
266
Iiro Valkonen7686b102011-02-02 23:21:58 -0800267struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700268 int status;
269 int x;
270 int y;
271 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700272 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700273};
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);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700572 input_report_abs(input_dev, ABS_MT_PRESSURE,
573 finger[id].pressure);
Amy Maloche2b59bab2011-10-13 16:08:16 -0700574 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700575
Amy Maloche2b59bab2011-10-13 16:08:16 -0700576 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700577 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700578 else
579 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580 }
581
582 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
583
Iiro Valkonen7686b102011-02-02 23:21:58 -0800584 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700585 input_report_abs(input_dev, ABS_X, finger[single_id].x);
586 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700587 input_report_abs(input_dev,
588 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589 }
590
591 input_sync(input_dev);
592}
593
Iiro Valkonen7686b102011-02-02 23:21:58 -0800594static void mxt_input_touchevent(struct mxt_data *data,
595 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700596{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800597 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700598 struct device *dev = &data->client->dev;
599 u8 status = message->message[0];
600 int x;
601 int y;
602 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700603 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700604
605 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800606 if (!(status & MXT_DETECT)) {
607 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700608 dev_dbg(dev, "[%d] released\n", id);
609
Iiro Valkonen7686b102011-02-02 23:21:58 -0800610 finger[id].status = MXT_RELEASE;
611 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700612 }
613 return;
614 }
615
616 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800617 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700618 return;
619
Joonyoung Shim910d8052011-04-12 23:14:38 -0700620 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
621 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700622 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700623 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700624 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700625 y = y >> 2;
626
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700627 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700628 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700629
630 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800631 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700632 x, y, area);
633
Iiro Valkonen7686b102011-02-02 23:21:58 -0800634 finger[id].status = status & MXT_MOVE ?
635 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636 finger[id].x = x;
637 finger[id].y = y;
638 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700639 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700640
Iiro Valkonen7686b102011-02-02 23:21:58 -0800641 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700642}
643
Iiro Valkonen7686b102011-02-02 23:21:58 -0800644static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700645{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800646 struct mxt_data *data = dev_id;
647 struct mxt_message message;
648 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700649 struct device *dev = &data->client->dev;
650 int id;
651 u8 reportid;
652 u8 max_reportid;
653 u8 min_reportid;
654
655 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800656 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700657 dev_err(dev, "Failed to read message\n");
658 goto end;
659 }
660
661 reportid = message.reportid;
662
Iiro Valkonen7686b102011-02-02 23:21:58 -0800663 /* whether reportid is thing of MXT_TOUCH_MULTI */
664 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665 if (!object)
666 goto end;
667
668 max_reportid = object->max_reportid;
669 min_reportid = max_reportid - object->num_report_ids + 1;
670 id = reportid - min_reportid;
671
672 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800673 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800675 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700676 } while (reportid != 0xff);
677
678end:
679 return IRQ_HANDLED;
680}
681
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700683{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800684 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800685 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686 struct device *dev = &data->client->dev;
687 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800688 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689
Iiro Valkonen71749f52011-02-15 13:36:52 -0800690 if (!pdata->config) {
691 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
692 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700693 }
694
695 for (i = 0; i < data->info.object_num; i++) {
696 object = data->object_table + i;
697
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699 continue;
700
Iiro Valkonen71749f52011-02-15 13:36:52 -0800701 for (j = 0; j < object->size + 1; j++) {
702 config_offset = index + j;
703 if (config_offset > pdata->config_length) {
704 dev_err(dev, "Not enough config data!\n");
705 return -EINVAL;
706 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800707 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800708 pdata->config[config_offset]);
709 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700710 index += object->size + 1;
711 }
712
713 return 0;
714}
715
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717{
718 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800719 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720 int count = 10;
721 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700722
723 /* Read dummy message to make high CHG pin */
724 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800725 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700726 if (error)
727 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800728 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700729
730 if (!count) {
731 dev_err(dev, "CHG pin isn't cleared\n");
732 return -EBUSY;
733 }
734
735 return 0;
736}
737
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739{
740 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800741 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700742 int error;
743 u8 val;
744
Iiro Valkonen7686b102011-02-02 23:21:58 -0800745 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700746 if (error)
747 return error;
748 info->family_id = val;
749
Iiro Valkonen7686b102011-02-02 23:21:58 -0800750 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700751 if (error)
752 return error;
753 info->variant_id = val;
754
Iiro Valkonen7686b102011-02-02 23:21:58 -0800755 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756 if (error)
757 return error;
758 info->version = val;
759
Iiro Valkonen7686b102011-02-02 23:21:58 -0800760 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761 if (error)
762 return error;
763 info->build = val;
764
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766 if (error)
767 return error;
768 info->object_num = val;
769
770 return 0;
771}
772
Iiro Valkonen7686b102011-02-02 23:21:58 -0800773static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700774{
775 int error;
776 int i;
777 u16 reg;
778 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700780
781 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800782 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783
Iiro Valkonen7686b102011-02-02 23:21:58 -0800784 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
785 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786 if (error)
787 return error;
788
789 object->type = buf[0];
790 object->start_address = (buf[2] << 8) | buf[1];
791 object->size = buf[3];
792 object->instances = buf[4];
793 object->num_report_ids = buf[5];
794
795 if (object->num_report_ids) {
796 reportid += object->num_report_ids *
797 (object->instances + 1);
798 object->max_reportid = reportid;
799 }
800 }
801
802 return 0;
803}
804
Amy Maloche7e447432011-09-14 11:36:30 -0700805static void mxt_reset_delay(struct mxt_data *data)
806{
807 struct mxt_info *info = &data->info;
808
809 switch (info->family_id) {
810 case MXT224_ID:
811 msleep(MXT224_RESET_TIME);
812 break;
813 case MXT1386_ID:
814 msleep(MXT1386_RESET_TIME);
815 break;
816 default:
817 msleep(MXT_RESET_TIME);
818 }
819}
820
Iiro Valkonen7686b102011-02-02 23:21:58 -0800821static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700822{
823 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800824 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700825 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700826 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700827 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700828 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700829 struct mxt_object *t7_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700830
Iiro Valkonen7686b102011-02-02 23:21:58 -0800831 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700832 if (error)
833 return error;
834
835 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800836 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700837 GFP_KERNEL);
838 if (!data->object_table) {
839 dev_err(&client->dev, "Failed to allocate memory\n");
840 return -ENOMEM;
841 }
842
843 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800844 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700846 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700847
848 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800849 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700850 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700851 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700852
Amy Maloche52262212011-09-15 16:46:57 -0700853 /* Store T7 and T9 locally, used in suspend/resume operations */
Jing Lin36aee812011-10-17 17:17:28 -0700854 t7_object = mxt_get_object(data, MXT_GEN_POWER);
855 if (!t7_object) {
856 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -0700857 error = -EINVAL;
858 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -0700859 }
860
861 data->t7_start_addr = t7_object->start_address;
862 error = __mxt_read_reg(client, data->t7_start_addr,
863 T7_DATA_SIZE, data->t7_data);
864 if (error < 0) {
865 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -0700866 "Failed to save current power state\n");
867 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -0700868 }
869 error = mxt_read_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL,
870 &data->t9_ctrl);
871 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -0700872 dev_err(&client->dev, "Failed to save current touch object\n");
873 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -0700874 }
875
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700876 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800877 mxt_write_object(data, MXT_GEN_COMMAND,
878 MXT_COMMAND_BACKUPNV,
879 MXT_BACKUP_VALUE);
880 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700881 do {
882 error = mxt_read_object(data, MXT_GEN_COMMAND,
883 MXT_COMMAND_BACKUPNV,
884 &command_register);
885 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700886 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -0700887 usleep_range(1000, 2000);
888 } while ((command_register != 0) && (++timeout_counter <= 100));
889 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700890 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -0700891 error = -EIO;
892 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700893 }
894
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700895
896 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800897 mxt_write_object(data, MXT_GEN_COMMAND,
898 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700899
Amy Maloche7e447432011-09-14 11:36:30 -0700900 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901
902 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800903 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700904 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700905 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700906 info->matrix_xsize = val;
907
Iiro Valkonen7686b102011-02-02 23:21:58 -0800908 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700910 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700911 info->matrix_ysize = val;
912
913 dev_info(&client->dev,
914 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
915 info->family_id, info->variant_id, info->version,
916 info->build);
917
918 dev_info(&client->dev,
919 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
920 info->matrix_xsize, info->matrix_ysize,
921 info->object_num);
922
923 return 0;
Jing Lin32c72532011-11-03 12:02:33 -0700924
925free_object_table:
926 kfree(data->object_table);
927 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700928}
929
Iiro Valkonen7686b102011-02-02 23:21:58 -0800930static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931 struct device_attribute *attr, char *buf)
932{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800933 struct mxt_data *data = dev_get_drvdata(dev);
934 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700935 int count = 0;
936 int i, j;
937 int error;
938 u8 val;
939
940 for (i = 0; i < data->info.object_num; i++) {
941 object = data->object_table + i;
942
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700943 count += snprintf(buf + count, PAGE_SIZE - count,
944 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700945 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700946 if (count >= PAGE_SIZE)
947 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700948
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700950 count += snprintf(buf + count, PAGE_SIZE - count,
951 "\n");
952 if (count >= PAGE_SIZE)
953 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954 continue;
955 }
956
957 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800958 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700959 object->type, j, &val);
960 if (error)
961 return error;
962
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700963 count += snprintf(buf + count, PAGE_SIZE - count,
964 "\t[%2d]: %02x (%d)\n", j, val, val);
965 if (count >= PAGE_SIZE)
966 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967 }
968
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700969 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
970 if (count >= PAGE_SIZE)
971 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972 }
973
974 return count;
975}
976
Iiro Valkonen7686b102011-02-02 23:21:58 -0800977static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700978{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800979 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980 struct i2c_client *client = data->client;
981 const struct firmware *fw = NULL;
982 unsigned int frame_size;
983 unsigned int pos = 0;
984 int ret;
985
986 ret = request_firmware(&fw, fn, dev);
987 if (ret) {
988 dev_err(dev, "Unable to open firmware %s\n", fn);
989 return ret;
990 }
991
992 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800993 mxt_write_object(data, MXT_GEN_COMMAND,
994 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700995
996 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997
998 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800999 if (client->addr == MXT_APP_LOW)
1000 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003
Iiro Valkonen7686b102011-02-02 23:21:58 -08001004 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005 if (ret)
1006 goto out;
1007
1008 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001009 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010
1011 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001012 ret = mxt_check_bootloader(client,
1013 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001014 if (ret)
1015 goto out;
1016
1017 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1018
1019 /* We should add 2 at frame size as the the firmware data is not
1020 * included the CRC bytes.
1021 */
1022 frame_size += 2;
1023
1024 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001025 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001026
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027 ret = mxt_check_bootloader(client,
1028 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029 if (ret)
1030 goto out;
1031
1032 pos += frame_size;
1033
1034 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1035 }
1036
1037out:
1038 release_firmware(fw);
1039
1040 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001041 if (client->addr == MXT_BOOT_LOW)
1042 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001044 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045
1046 return ret;
1047}
1048
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050 struct device_attribute *attr,
1051 const char *buf, size_t count)
1052{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001053 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054 int error;
1055
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001056 disable_irq(data->irq);
1057
Iiro Valkonen7686b102011-02-02 23:21:58 -08001058 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001059 if (error) {
1060 dev_err(dev, "The firmware update failed(%d)\n", error);
1061 count = error;
1062 } else {
1063 dev_dbg(dev, "The firmware update succeeded\n");
1064
1065 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067
1068 kfree(data->object_table);
1069 data->object_table = NULL;
1070
Iiro Valkonen7686b102011-02-02 23:21:58 -08001071 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001072 }
1073
1074 enable_irq(data->irq);
1075
Iiro Valkonen08960a02011-04-12 23:16:40 -07001076 error = mxt_make_highchg(data);
1077 if (error)
1078 return error;
1079
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080 return count;
1081}
1082
Iiro Valkonen7686b102011-02-02 23:21:58 -08001083static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1084static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001085
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001087 &dev_attr_object.attr,
1088 &dev_attr_update_fw.attr,
1089 NULL
1090};
1091
Iiro Valkonen7686b102011-02-02 23:21:58 -08001092static const struct attribute_group mxt_attr_group = {
1093 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001094};
1095
Amy Maloche52262212011-09-15 16:46:57 -07001096static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097{
Jing Lin36aee812011-10-17 17:17:28 -07001098 int error;
1099
Amy Maloche52262212011-09-15 16:46:57 -07001100 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001101 error = __mxt_write_reg(data->client, data->t7_start_addr,
1102 T7_DATA_SIZE, data->t7_data);
1103 if (error < 0) {
1104 dev_err(&data->client->dev,
1105 "failed to restore old power state\n");
1106 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001107 }
Jing Lin36aee812011-10-17 17:17:28 -07001108
Amy Maloche52262212011-09-15 16:46:57 -07001109 error = mxt_write_object(data,
1110 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1111 if (error < 0) {
1112 dev_err(&data->client->dev, "failed to restore touch\n");
1113 return error;
1114 }
1115
1116 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001117}
1118
Amy Maloche52262212011-09-15 16:46:57 -07001119static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001120{
Jing Lin36aee812011-10-17 17:17:28 -07001121 int error;
1122 u8 t7_data[T7_DATA_SIZE] = {0};
1123
1124 /* disable touch and configure deep sleep mode */
1125 error = mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
1126 if (error < 0) {
1127 dev_err(&data->client->dev, "failed to disable touch\n");
1128 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001129 }
1130
Jing Lin36aee812011-10-17 17:17:28 -07001131 error = __mxt_write_reg(data->client, data->t7_start_addr,
1132 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001133 if (error < 0) {
1134 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001135 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001136 return error;
1137 }
1138
1139 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140}
1141
Iiro Valkonen7686b102011-02-02 23:21:58 -08001142static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001144 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001145 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146
Amy Maloche52262212011-09-15 16:46:57 -07001147 error = mxt_start(data);
1148 if (error < 0) {
1149 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1150 return error;
1151 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001152
1153 return 0;
1154}
1155
Iiro Valkonen7686b102011-02-02 23:21:58 -08001156static void mxt_input_close(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_stop(data);
1162 if (error < 0)
1163 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1164
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001165}
1166
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301167static int mxt_power_on(struct mxt_data *data, bool on)
1168{
1169 int rc;
1170
1171 if (on == false)
1172 goto power_off;
1173
1174 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1175 if (rc < 0) {
1176 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1177 rc);
1178 return rc;
1179 }
1180
1181 rc = regulator_enable(data->vcc);
1182 if (rc) {
1183 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1184 rc);
1185 goto error_reg_en_vcc;
1186 }
1187
1188 if (data->pdata->i2c_pull_up) {
1189 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1190 if (rc < 0) {
1191 dev_err(&data->client->dev,
1192 "Regulator set_opt failed rc=%d\n", rc);
1193 goto error_reg_opt_i2c;
1194 }
1195
1196 rc = regulator_enable(data->vcc_i2c);
1197 if (rc) {
1198 dev_err(&data->client->dev,
1199 "Regulator enable failed rc=%d\n", rc);
1200 goto error_reg_en_vcc_i2c;
1201 }
1202 }
1203
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001204 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301205
1206 return 0;
1207
1208error_reg_en_vcc_i2c:
1209 if (data->pdata->i2c_pull_up)
1210 regulator_set_optimum_mode(data->vcc_i2c, 0);
1211error_reg_opt_i2c:
1212 regulator_disable(data->vcc);
1213error_reg_en_vcc:
1214 regulator_set_optimum_mode(data->vcc, 0);
1215 return rc;
1216
1217power_off:
1218 regulator_set_optimum_mode(data->vcc, 0);
1219 regulator_disable(data->vcc);
1220 if (data->pdata->i2c_pull_up) {
1221 regulator_set_optimum_mode(data->vcc_i2c, 0);
1222 regulator_disable(data->vcc_i2c);
1223 }
1224 msleep(50);
1225 return 0;
1226}
1227
1228static int mxt_regulator_configure(struct mxt_data *data, bool on)
1229{
1230 int rc;
1231
1232 if (on == false)
1233 goto hw_shutdown;
1234
1235 data->vcc = regulator_get(&data->client->dev, "vdd");
1236 if (IS_ERR(data->vcc)) {
1237 rc = PTR_ERR(data->vcc);
1238 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1239 rc);
1240 return rc;
1241 }
1242
1243 if (regulator_count_voltages(data->vcc) > 0) {
1244 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1245 MXT_VTG_MAX_UV);
1246 if (rc) {
1247 dev_err(&data->client->dev,
1248 "regulator set_vtg failed rc=%d\n", rc);
1249 goto error_set_vtg_vcc;
1250 }
1251 }
1252
1253 if (data->pdata->i2c_pull_up) {
1254 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1255 if (IS_ERR(data->vcc_i2c)) {
1256 rc = PTR_ERR(data->vcc_i2c);
1257 dev_err(&data->client->dev,
1258 "Regulator get failed rc=%d\n", rc);
1259 goto error_get_vtg_i2c;
1260 }
1261 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1262 rc = regulator_set_voltage(data->vcc_i2c,
1263 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1264 if (rc) {
1265 dev_err(&data->client->dev,
1266 "regulator set_vtg failed rc=%d\n", rc);
1267 goto error_set_vtg_i2c;
1268 }
1269 }
1270 }
1271
1272 return 0;
1273
1274error_set_vtg_i2c:
1275 regulator_put(data->vcc_i2c);
1276error_get_vtg_i2c:
1277 if (regulator_count_voltages(data->vcc) > 0)
1278 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1279error_set_vtg_vcc:
1280 regulator_put(data->vcc);
1281 return rc;
1282
1283hw_shutdown:
1284 if (regulator_count_voltages(data->vcc) > 0)
1285 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1286 regulator_put(data->vcc);
1287 if (data->pdata->i2c_pull_up) {
1288 if (regulator_count_voltages(data->vcc_i2c) > 0)
1289 regulator_set_voltage(data->vcc_i2c, 0,
1290 MXT_I2C_VTG_MAX_UV);
1291 regulator_put(data->vcc_i2c);
1292 }
1293 return 0;
1294}
1295
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301296#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001297static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1298{
1299
1300 int rc;
1301
1302 if (on == false)
1303 goto regulator_hpm;
1304
1305 rc = regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1306 if (rc < 0) {
1307 dev_err(&data->client->dev,
1308 "Regulator set_opt failed rc=%d\n", rc);
1309 goto fail_regulator_lpm;
1310 }
1311
1312 if (data->pdata->i2c_pull_up) {
1313 rc = regulator_set_optimum_mode(data->vcc_i2c,
1314 MXT_I2C_LPM_LOAD_UA);
1315 if (rc < 0) {
1316 dev_err(&data->client->dev,
1317 "Regulator set_opt failed rc=%d\n", rc);
1318 goto fail_regulator_lpm;
1319 }
1320 }
1321
1322 return 0;
1323
1324regulator_hpm:
1325
1326 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1327 if (rc < 0) {
1328 dev_err(&data->client->dev,
1329 "Regulator set_opt failed rc=%d\n", rc);
1330 goto fail_regulator_hpm;
1331 }
1332
1333 if (data->pdata->i2c_pull_up) {
1334 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1335 if (rc < 0) {
1336 dev_err(&data->client->dev,
1337 "Regulator set_opt failed rc=%d\n", rc);
1338 goto fail_regulator_hpm;
1339 }
1340 }
1341
1342 return 0;
1343
1344fail_regulator_lpm:
1345 regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1346 if (data->pdata->i2c_pull_up)
1347 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1348
1349 return rc;
1350
1351fail_regulator_hpm:
1352 regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1353 if (data->pdata->i2c_pull_up)
1354 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1355
1356 return rc;
1357}
1358
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301359static int mxt_suspend(struct device *dev)
1360{
1361 struct i2c_client *client = to_i2c_client(dev);
1362 struct mxt_data *data = i2c_get_clientdata(client);
1363 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001364 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301365
1366 mutex_lock(&input_dev->mutex);
1367
Amy Maloche52262212011-09-15 16:46:57 -07001368 if (input_dev->users) {
1369 error = mxt_stop(data);
1370 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001371 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001372 mutex_unlock(&input_dev->mutex);
1373 return error;
1374 }
1375
1376 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301377
1378 mutex_unlock(&input_dev->mutex);
1379
Jing Linbace50b2011-10-18 22:55:47 -07001380 /* put regulators in low power mode */
1381 error = mxt_regulator_lpm(data, true);
1382 if (error < 0) {
1383 dev_err(dev, "failed to enter low power mode\n");
1384 return error;
1385 }
1386
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301387 return 0;
1388}
1389
1390static int mxt_resume(struct device *dev)
1391{
1392 struct i2c_client *client = to_i2c_client(dev);
1393 struct mxt_data *data = i2c_get_clientdata(client);
1394 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001395 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301396
Jing Linbace50b2011-10-18 22:55:47 -07001397 /* put regulators in high power mode */
1398 error = mxt_regulator_lpm(data, false);
1399 if (error < 0) {
1400 dev_err(dev, "failed to enter high power mode\n");
1401 return error;
1402 }
1403
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301404 mutex_lock(&input_dev->mutex);
1405
Amy Maloche52262212011-09-15 16:46:57 -07001406 if (input_dev->users) {
1407 error = mxt_start(data);
1408 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001409 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001410 mutex_unlock(&input_dev->mutex);
1411 return error;
1412 }
1413 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301414
1415 mutex_unlock(&input_dev->mutex);
1416
1417 return 0;
1418}
1419
1420#if defined(CONFIG_HAS_EARLYSUSPEND)
1421static void mxt_early_suspend(struct early_suspend *h)
1422{
1423 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1424
1425 mxt_suspend(&data->client->dev);
1426}
1427
1428static void mxt_late_resume(struct early_suspend *h)
1429{
1430 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1431
1432 mxt_resume(&data->client->dev);
1433}
1434#endif
1435
1436static const struct dev_pm_ops mxt_pm_ops = {
1437#ifndef CONFIG_HAS_EARLYSUSPEND
1438 .suspend = mxt_suspend,
1439 .resume = mxt_resume,
1440#endif
1441};
1442#endif
1443
Iiro Valkonen7686b102011-02-02 23:21:58 -08001444static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001445 const struct i2c_device_id *id)
1446{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001447 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001448 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001449 struct input_dev *input_dev;
1450 int error;
1451
Iiro Valkonen919ed892011-02-15 13:36:52 -08001452 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001453 return -EINVAL;
1454
Iiro Valkonen7686b102011-02-02 23:21:58 -08001455 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001456 input_dev = input_allocate_device();
1457 if (!data || !input_dev) {
1458 dev_err(&client->dev, "Failed to allocate memory\n");
1459 error = -ENOMEM;
1460 goto err_free_mem;
1461 }
1462
Iiro Valkonen7686b102011-02-02 23:21:58 -08001463 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001464 input_dev->id.bustype = BUS_I2C;
1465 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001466 input_dev->open = mxt_input_open;
1467 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001468
Joonyoung Shim910d8052011-04-12 23:14:38 -07001469 data->client = client;
1470 data->input_dev = input_dev;
1471 data->pdata = pdata;
1472 data->irq = client->irq;
1473
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001474 __set_bit(EV_ABS, input_dev->evbit);
1475 __set_bit(EV_KEY, input_dev->evbit);
1476 __set_bit(BTN_TOUCH, input_dev->keybit);
1477
1478 /* For single touch */
1479 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001480 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001481 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001482 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001483 input_set_abs_params(input_dev, ABS_PRESSURE,
1484 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001485
1486 /* For multi touch */
1487 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001488 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001489 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001490 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001491 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001492 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001493 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1494 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001495
1496 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001497 i2c_set_clientdata(client, data);
1498
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301499 if (pdata->init_hw)
1500 error = pdata->init_hw(true);
1501 else
1502 error = mxt_regulator_configure(data, true);
1503 if (error) {
1504 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001505 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301506 }
1507
1508 if (pdata->power_on)
1509 error = pdata->power_on(true);
1510 else
1511 error = mxt_power_on(data, true);
1512 if (error) {
1513 dev_err(&client->dev, "Failed to power on hardware\n");
1514 goto err_regulator_on;
1515 }
1516
Iiro Valkonen7686b102011-02-02 23:21:58 -08001517 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001518 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301519 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001520
Iiro Valkonen7686b102011-02-02 23:21:58 -08001521 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001522 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001523 if (error) {
1524 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001525 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001526 }
1527
Iiro Valkonen08960a02011-04-12 23:16:40 -07001528 error = mxt_make_highchg(data);
1529 if (error)
1530 goto err_free_irq;
1531
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001532 error = input_register_device(input_dev);
1533 if (error)
1534 goto err_free_irq;
1535
Iiro Valkonen7686b102011-02-02 23:21:58 -08001536 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001537 if (error)
1538 goto err_unregister_device;
1539
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301540#if defined(CONFIG_HAS_EARLYSUSPEND)
1541 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1542 MXT_SUSPEND_LEVEL;
1543 data->early_suspend.suspend = mxt_early_suspend;
1544 data->early_suspend.resume = mxt_late_resume;
1545 register_early_suspend(&data->early_suspend);
1546#endif
1547
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001548 return 0;
1549
1550err_unregister_device:
1551 input_unregister_device(input_dev);
1552 input_dev = NULL;
1553err_free_irq:
1554 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001555err_free_object:
1556 kfree(data->object_table);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301557err_power_on:
1558 if (pdata->power_on)
1559 pdata->power_on(false);
1560 else
1561 mxt_power_on(data, false);
1562err_regulator_on:
1563 if (pdata->init_hw)
1564 pdata->init_hw(false);
1565 else
1566 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001567err_free_mem:
1568 input_free_device(input_dev);
1569 kfree(data);
1570 return error;
1571}
1572
Iiro Valkonen7686b102011-02-02 23:21:58 -08001573static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001574{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001575 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001576
Iiro Valkonen7686b102011-02-02 23:21:58 -08001577 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001578 free_irq(data->irq, data);
1579 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301580#if defined(CONFIG_HAS_EARLYSUSPEND)
1581 unregister_early_suspend(&data->early_suspend);
1582#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301583
1584 if (data->pdata->power_on)
1585 data->pdata->power_on(false);
1586 else
1587 mxt_power_on(data, false);
1588
1589 if (data->pdata->init_hw)
1590 data->pdata->init_hw(false);
1591 else
1592 mxt_regulator_configure(data, false);
1593
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001594 kfree(data->object_table);
1595 kfree(data);
1596
1597 return 0;
1598}
1599
Iiro Valkonen7686b102011-02-02 23:21:58 -08001600static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001601 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001602 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001603 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001604 { }
1605};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001606MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001607
Iiro Valkonen7686b102011-02-02 23:21:58 -08001608static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001609 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001610 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001611 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001612#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001613 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001614#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001615 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001616 .probe = mxt_probe,
1617 .remove = __devexit_p(mxt_remove),
1618 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001619};
1620
Iiro Valkonen7686b102011-02-02 23:21:58 -08001621static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001622{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001623 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001624}
1625
Iiro Valkonen7686b102011-02-02 23:21:58 -08001626static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001627{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001628 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001629}
1630
Iiro Valkonen7686b102011-02-02 23:21:58 -08001631module_init(mxt_init);
1632module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001633
1634/* Module information */
1635MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001636MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001637MODULE_LICENSE("GPL");