blob: 790a9e1fd69b0c212bbc353b72dc0ed99ff78c89 [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;
272};
273
274/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800275struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700276 struct i2c_client *client;
277 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800278 const struct mxt_platform_data *pdata;
279 struct mxt_object *object_table;
280 struct mxt_info info;
281 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700282 unsigned int irq;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530283 struct regulator *vcc;
284 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530285#if defined(CONFIG_HAS_EARLYSUSPEND)
286 struct early_suspend early_suspend;
287#endif
Jing Lin36aee812011-10-17 17:17:28 -0700288
Amy Maloche52262212011-09-15 16:46:57 -0700289 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700290 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700291 u8 t9_ctrl;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700292};
293
Iiro Valkonen7686b102011-02-02 23:21:58 -0800294static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700295{
296 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800297 case MXT_GEN_MESSAGE:
298 case MXT_GEN_COMMAND:
299 case MXT_GEN_POWER:
300 case MXT_GEN_ACQUIRE:
301 case MXT_TOUCH_MULTI:
302 case MXT_TOUCH_KEYARRAY:
303 case MXT_TOUCH_PROXIMITY:
304 case MXT_PROCI_GRIPFACE:
305 case MXT_PROCG_NOISE:
306 case MXT_PROCI_ONETOUCH:
307 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700308 case MXT_PROCI_GRIP:
309 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800310 case MXT_SPT_COMMSCONFIG:
311 case MXT_SPT_GPIOPWM:
312 case MXT_SPT_SELFTEST:
313 case MXT_SPT_CTECONFIG:
314 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530315 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700316 return true;
317 default:
318 return false;
319 }
320}
321
Iiro Valkonen7686b102011-02-02 23:21:58 -0800322static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700323{
324 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800325 case MXT_GEN_COMMAND:
326 case MXT_GEN_POWER:
327 case MXT_GEN_ACQUIRE:
328 case MXT_TOUCH_MULTI:
329 case MXT_TOUCH_KEYARRAY:
330 case MXT_TOUCH_PROXIMITY:
331 case MXT_PROCI_GRIPFACE:
332 case MXT_PROCG_NOISE:
333 case MXT_PROCI_ONETOUCH:
334 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700335 case MXT_PROCI_GRIP:
336 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800337 case MXT_SPT_GPIOPWM:
338 case MXT_SPT_SELFTEST:
339 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530340 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530341 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700342 return true;
343 default:
344 return false;
345 }
346}
347
Iiro Valkonen7686b102011-02-02 23:21:58 -0800348static void mxt_dump_message(struct device *dev,
349 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700350{
351 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
352 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
353 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
354 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
355 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
356 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
357 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
358 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
359 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
360}
361
Iiro Valkonen7686b102011-02-02 23:21:58 -0800362static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700363 unsigned int state)
364{
365 u8 val;
366
367recheck:
368 if (i2c_master_recv(client, &val, 1) != 1) {
369 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
370 return -EIO;
371 }
372
373 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800374 case MXT_WAITING_BOOTLOAD_CMD:
375 case MXT_WAITING_FRAME_DATA:
376 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700377 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800378 case MXT_FRAME_CRC_PASS:
379 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700380 goto recheck;
381 break;
382 default:
383 return -EINVAL;
384 }
385
386 if (val != state) {
387 dev_err(&client->dev, "Unvalid bootloader mode state\n");
388 return -EINVAL;
389 }
390
391 return 0;
392}
393
Iiro Valkonen7686b102011-02-02 23:21:58 -0800394static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700395{
396 u8 buf[2];
397
Iiro Valkonen7686b102011-02-02 23:21:58 -0800398 buf[0] = MXT_UNLOCK_CMD_LSB;
399 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700400
401 if (i2c_master_send(client, buf, 2) != 2) {
402 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
403 return -EIO;
404 }
405
406 return 0;
407}
408
Iiro Valkonen7686b102011-02-02 23:21:58 -0800409static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700410 const u8 *data, unsigned int frame_size)
411{
412 if (i2c_master_send(client, data, frame_size) != frame_size) {
413 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
414 return -EIO;
415 }
416
417 return 0;
418}
419
Iiro Valkonen7686b102011-02-02 23:21:58 -0800420static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700421 u16 reg, u16 len, void *val)
422{
423 struct i2c_msg xfer[2];
424 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700425 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700426
427 buf[0] = reg & 0xff;
428 buf[1] = (reg >> 8) & 0xff;
429
430 /* Write register */
431 xfer[0].addr = client->addr;
432 xfer[0].flags = 0;
433 xfer[0].len = 2;
434 xfer[0].buf = buf;
435
436 /* Read data */
437 xfer[1].addr = client->addr;
438 xfer[1].flags = I2C_M_RD;
439 xfer[1].len = len;
440 xfer[1].buf = val;
441
Jing Lin36aee812011-10-17 17:17:28 -0700442 do {
443 if (i2c_transfer(client->adapter, xfer, 2) == 2)
444 return 0;
445 msleep(MXT_WAKE_TIME);
446 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700447
Jing Lin36aee812011-10-17 17:17:28 -0700448 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
449 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700450}
451
Iiro Valkonen7686b102011-02-02 23:21:58 -0800452static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700453{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800454 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700455}
456
Jing Lin36aee812011-10-17 17:17:28 -0700457static int __mxt_write_reg(struct i2c_client *client,
458 u16 addr, u16 length, u8 *value)
459{
460 u8 buf[MXT_BLOCK_SIZE + 2];
461 int i, tries = 0;
462
463 if (length > MXT_BLOCK_SIZE)
464 return -EINVAL;
465
466 buf[0] = addr & 0xff;
467 buf[1] = (addr >> 8) & 0xff;
468 for (i = 0; i < length; i++)
469 buf[i + 2] = *value++;
470
471 do {
472 if (i2c_master_send(client, buf, length + 2) == (length + 2))
473 return 0;
474 msleep(MXT_WAKE_TIME);
475 } while (++tries < MXT_MAX_RW_TRIES);
476
477 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
478 return -EIO;
479}
480
Iiro Valkonen7686b102011-02-02 23:21:58 -0800481static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482{
Jing Lin36aee812011-10-17 17:17:28 -0700483 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700484}
485
Iiro Valkonen7686b102011-02-02 23:21:58 -0800486static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700487 u16 reg, u8 *object_buf)
488{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800489 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700490 object_buf);
491}
492
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493static struct mxt_object *
494mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800496 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497 int i;
498
499 for (i = 0; i < data->info.object_num; i++) {
500 object = data->object_table + i;
501 if (object->type == type)
502 return object;
503 }
504
505 dev_err(&data->client->dev, "Invalid object type\n");
506 return NULL;
507}
508
Iiro Valkonen7686b102011-02-02 23:21:58 -0800509static int mxt_read_message(struct mxt_data *data,
510 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800512 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700513 u16 reg;
514
Iiro Valkonen7686b102011-02-02 23:21:58 -0800515 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700516 if (!object)
517 return -EINVAL;
518
519 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800520 return __mxt_read_reg(data->client, reg,
521 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522}
523
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525 u8 type, u8 offset, u8 *val)
526{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528 u16 reg;
529
Iiro Valkonen7686b102011-02-02 23:21:58 -0800530 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700531 if (!object)
532 return -EINVAL;
533
534 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800535 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536}
537
Iiro Valkonen7686b102011-02-02 23:21:58 -0800538static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700539 u8 type, u8 offset, u8 val)
540{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800541 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542 u16 reg;
543
Iiro Valkonen7686b102011-02-02 23:21:58 -0800544 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545 if (!object)
546 return -EINVAL;
547
548 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800549 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550}
551
Iiro Valkonen7686b102011-02-02 23:21:58 -0800552static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700553{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800554 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700555 struct input_dev *input_dev = data->input_dev;
556 int status = finger[single_id].status;
557 int finger_num = 0;
558 int id;
559
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561 if (!finger[id].status)
562 continue;
563
Amy Maloche2b59bab2011-10-13 16:08:16 -0700564 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
565 finger[id].status != MXT_RELEASE ?
566 finger[id].area : 0);
567 input_report_abs(input_dev, ABS_MT_POSITION_X,
568 finger[id].x);
569 input_report_abs(input_dev, ABS_MT_POSITION_Y,
570 finger[id].y);
571 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572
Amy Maloche2b59bab2011-10-13 16:08:16 -0700573 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700574 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700575 else
576 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 }
578
579 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
580
Iiro Valkonen7686b102011-02-02 23:21:58 -0800581 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 input_report_abs(input_dev, ABS_X, finger[single_id].x);
583 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
584 }
585
586 input_sync(input_dev);
587}
588
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589static void mxt_input_touchevent(struct mxt_data *data,
590 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 struct device *dev = &data->client->dev;
594 u8 status = message->message[0];
595 int x;
596 int y;
597 int area;
598
599 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800600 if (!(status & MXT_DETECT)) {
601 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700602 dev_dbg(dev, "[%d] released\n", id);
603
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604 finger[id].status = MXT_RELEASE;
605 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 }
607 return;
608 }
609
610 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800611 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700612 return;
613
Joonyoung Shim910d8052011-04-12 23:14:38 -0700614 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
615 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700616 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700617 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700618 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700619 y = y >> 2;
620
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700621 area = message->message[4];
622
623 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800624 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625 x, y, area);
626
Iiro Valkonen7686b102011-02-02 23:21:58 -0800627 finger[id].status = status & MXT_MOVE ?
628 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700629 finger[id].x = x;
630 finger[id].y = y;
631 finger[id].area = area;
632
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634}
635
Iiro Valkonen7686b102011-02-02 23:21:58 -0800636static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800638 struct mxt_data *data = dev_id;
639 struct mxt_message message;
640 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641 struct device *dev = &data->client->dev;
642 int id;
643 u8 reportid;
644 u8 max_reportid;
645 u8 min_reportid;
646
647 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800648 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700649 dev_err(dev, "Failed to read message\n");
650 goto end;
651 }
652
653 reportid = message.reportid;
654
Iiro Valkonen7686b102011-02-02 23:21:58 -0800655 /* whether reportid is thing of MXT_TOUCH_MULTI */
656 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700657 if (!object)
658 goto end;
659
660 max_reportid = object->max_reportid;
661 min_reportid = max_reportid - object->num_report_ids + 1;
662 id = reportid - min_reportid;
663
664 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800667 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 } while (reportid != 0xff);
669
670end:
671 return IRQ_HANDLED;
672}
673
Iiro Valkonen7686b102011-02-02 23:21:58 -0800674static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800676 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800677 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700678 struct device *dev = &data->client->dev;
679 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800680 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700681
Iiro Valkonen71749f52011-02-15 13:36:52 -0800682 if (!pdata->config) {
683 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
684 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700685 }
686
687 for (i = 0; i < data->info.object_num; i++) {
688 object = data->object_table + i;
689
Iiro Valkonen7686b102011-02-02 23:21:58 -0800690 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691 continue;
692
Iiro Valkonen71749f52011-02-15 13:36:52 -0800693 for (j = 0; j < object->size + 1; j++) {
694 config_offset = index + j;
695 if (config_offset > pdata->config_length) {
696 dev_err(dev, "Not enough config data!\n");
697 return -EINVAL;
698 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800699 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800700 pdata->config[config_offset]);
701 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702 index += object->size + 1;
703 }
704
705 return 0;
706}
707
Iiro Valkonen7686b102011-02-02 23:21:58 -0800708static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700709{
710 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800711 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700712 int count = 10;
713 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714
715 /* Read dummy message to make high CHG pin */
716 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800717 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718 if (error)
719 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800720 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721
722 if (!count) {
723 dev_err(dev, "CHG pin isn't cleared\n");
724 return -EBUSY;
725 }
726
727 return 0;
728}
729
Iiro Valkonen7686b102011-02-02 23:21:58 -0800730static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731{
732 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800733 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 int error;
735 u8 val;
736
Iiro Valkonen7686b102011-02-02 23:21:58 -0800737 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700738 if (error)
739 return error;
740 info->family_id = val;
741
Iiro Valkonen7686b102011-02-02 23:21:58 -0800742 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743 if (error)
744 return error;
745 info->variant_id = val;
746
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748 if (error)
749 return error;
750 info->version = val;
751
Iiro Valkonen7686b102011-02-02 23:21:58 -0800752 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700753 if (error)
754 return error;
755 info->build = val;
756
Iiro Valkonen7686b102011-02-02 23:21:58 -0800757 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758 if (error)
759 return error;
760 info->object_num = val;
761
762 return 0;
763}
764
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766{
767 int error;
768 int i;
769 u16 reg;
770 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800771 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700772
773 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800774 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700775
Iiro Valkonen7686b102011-02-02 23:21:58 -0800776 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
777 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700778 if (error)
779 return error;
780
781 object->type = buf[0];
782 object->start_address = (buf[2] << 8) | buf[1];
783 object->size = buf[3];
784 object->instances = buf[4];
785 object->num_report_ids = buf[5];
786
787 if (object->num_report_ids) {
788 reportid += object->num_report_ids *
789 (object->instances + 1);
790 object->max_reportid = reportid;
791 }
792 }
793
794 return 0;
795}
796
Amy Maloche7e447432011-09-14 11:36:30 -0700797static void mxt_reset_delay(struct mxt_data *data)
798{
799 struct mxt_info *info = &data->info;
800
801 switch (info->family_id) {
802 case MXT224_ID:
803 msleep(MXT224_RESET_TIME);
804 break;
805 case MXT1386_ID:
806 msleep(MXT1386_RESET_TIME);
807 break;
808 default:
809 msleep(MXT_RESET_TIME);
810 }
811}
812
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814{
815 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800816 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700817 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700818 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700819 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700820 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700821 struct mxt_object *t7_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700822
Iiro Valkonen7686b102011-02-02 23:21:58 -0800823 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824 if (error)
825 return error;
826
827 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800828 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700829 GFP_KERNEL);
830 if (!data->object_table) {
831 dev_err(&client->dev, "Failed to allocate memory\n");
832 return -ENOMEM;
833 }
834
835 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800836 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700837 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700838 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700839
840 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800841 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700843 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700844
Amy Maloche52262212011-09-15 16:46:57 -0700845 /* Store T7 and T9 locally, used in suspend/resume operations */
Jing Lin36aee812011-10-17 17:17:28 -0700846 t7_object = mxt_get_object(data, MXT_GEN_POWER);
847 if (!t7_object) {
848 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -0700849 error = -EINVAL;
850 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -0700851 }
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,
Jing Lin32c72532011-11-03 12:02:33 -0700858 "Failed to save current power state\n");
859 goto free_object_table;
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) {
Jing Lin32c72532011-11-03 12:02:33 -0700864 dev_err(&client->dev, "Failed to save current touch object\n");
865 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -0700866 }
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)
Jing Lin32c72532011-11-03 12:02:33 -0700878 goto free_object_table;
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");
Jing Lin32c72532011-11-03 12:02:33 -0700883 error = -EIO;
884 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700885 }
886
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700887
888 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800889 mxt_write_object(data, MXT_GEN_COMMAND,
890 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700891
Amy Maloche7e447432011-09-14 11:36:30 -0700892 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893
894 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800895 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700896 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700897 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700898 info->matrix_xsize = val;
899
Iiro Valkonen7686b102011-02-02 23:21:58 -0800900 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700902 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700903 info->matrix_ysize = val;
904
905 dev_info(&client->dev,
906 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
907 info->family_id, info->variant_id, info->version,
908 info->build);
909
910 dev_info(&client->dev,
911 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
912 info->matrix_xsize, info->matrix_ysize,
913 info->object_num);
914
915 return 0;
Jing Lin32c72532011-11-03 12:02:33 -0700916
917free_object_table:
918 kfree(data->object_table);
919 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700920}
921
Iiro Valkonen7686b102011-02-02 23:21:58 -0800922static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700923 struct device_attribute *attr, char *buf)
924{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800925 struct mxt_data *data = dev_get_drvdata(dev);
926 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700927 int count = 0;
928 int i, j;
929 int error;
930 u8 val;
931
932 for (i = 0; i < data->info.object_num; i++) {
933 object = data->object_table + i;
934
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700935 count += snprintf(buf + count, PAGE_SIZE - count,
936 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700938 if (count >= PAGE_SIZE)
939 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700940
Iiro Valkonen7686b102011-02-02 23:21:58 -0800941 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700942 count += snprintf(buf + count, PAGE_SIZE - count,
943 "\n");
944 if (count >= PAGE_SIZE)
945 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946 continue;
947 }
948
949 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800950 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700951 object->type, j, &val);
952 if (error)
953 return error;
954
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700955 count += snprintf(buf + count, PAGE_SIZE - count,
956 "\t[%2d]: %02x (%d)\n", j, val, val);
957 if (count >= PAGE_SIZE)
958 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700959 }
960
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700961 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
962 if (count >= PAGE_SIZE)
963 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700964 }
965
966 return count;
967}
968
Iiro Valkonen7686b102011-02-02 23:21:58 -0800969static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700970{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800971 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972 struct i2c_client *client = data->client;
973 const struct firmware *fw = NULL;
974 unsigned int frame_size;
975 unsigned int pos = 0;
976 int ret;
977
978 ret = request_firmware(&fw, fn, dev);
979 if (ret) {
980 dev_err(dev, "Unable to open firmware %s\n", fn);
981 return ret;
982 }
983
984 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800985 mxt_write_object(data, MXT_GEN_COMMAND,
986 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700987
988 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700989
990 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800991 if (client->addr == MXT_APP_LOW)
992 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700995
Iiro Valkonen7686b102011-02-02 23:21:58 -0800996 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997 if (ret)
998 goto out;
999
1000 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001001 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001002
1003 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001004 ret = mxt_check_bootloader(client,
1005 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006 if (ret)
1007 goto out;
1008
1009 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1010
1011 /* We should add 2 at frame size as the the firmware data is not
1012 * included the CRC bytes.
1013 */
1014 frame_size += 2;
1015
1016 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
Iiro Valkonen7686b102011-02-02 23:21:58 -08001019 ret = mxt_check_bootloader(client,
1020 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021 if (ret)
1022 goto out;
1023
1024 pos += frame_size;
1025
1026 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1027 }
1028
1029out:
1030 release_firmware(fw);
1031
1032 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001033 if (client->addr == MXT_BOOT_LOW)
1034 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001036 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001037
1038 return ret;
1039}
1040
Iiro Valkonen7686b102011-02-02 23:21:58 -08001041static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042 struct device_attribute *attr,
1043 const char *buf, size_t count)
1044{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001045 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001046 int error;
1047
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001048 disable_irq(data->irq);
1049
Iiro Valkonen7686b102011-02-02 23:21:58 -08001050 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001051 if (error) {
1052 dev_err(dev, "The firmware update failed(%d)\n", error);
1053 count = error;
1054 } else {
1055 dev_dbg(dev, "The firmware update succeeded\n");
1056
1057 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001058 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001059
1060 kfree(data->object_table);
1061 data->object_table = NULL;
1062
Iiro Valkonen7686b102011-02-02 23:21:58 -08001063 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001064 }
1065
1066 enable_irq(data->irq);
1067
Iiro Valkonen08960a02011-04-12 23:16:40 -07001068 error = mxt_make_highchg(data);
1069 if (error)
1070 return error;
1071
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001072 return count;
1073}
1074
Iiro Valkonen7686b102011-02-02 23:21:58 -08001075static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1076static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001077
Iiro Valkonen7686b102011-02-02 23:21:58 -08001078static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001079 &dev_attr_object.attr,
1080 &dev_attr_update_fw.attr,
1081 NULL
1082};
1083
Iiro Valkonen7686b102011-02-02 23:21:58 -08001084static const struct attribute_group mxt_attr_group = {
1085 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001086};
1087
Amy Maloche52262212011-09-15 16:46:57 -07001088static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001089{
Jing Lin36aee812011-10-17 17:17:28 -07001090 int error;
1091
Amy Maloche52262212011-09-15 16:46:57 -07001092 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001093 error = __mxt_write_reg(data->client, data->t7_start_addr,
1094 T7_DATA_SIZE, data->t7_data);
1095 if (error < 0) {
1096 dev_err(&data->client->dev,
1097 "failed to restore old power state\n");
1098 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001099 }
Jing Lin36aee812011-10-17 17:17:28 -07001100
Amy Maloche52262212011-09-15 16:46:57 -07001101 error = mxt_write_object(data,
1102 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1103 if (error < 0) {
1104 dev_err(&data->client->dev, "failed to restore touch\n");
1105 return error;
1106 }
1107
1108 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109}
1110
Amy Maloche52262212011-09-15 16:46:57 -07001111static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001112{
Jing Lin36aee812011-10-17 17:17:28 -07001113 int error;
1114 u8 t7_data[T7_DATA_SIZE] = {0};
1115
1116 /* disable touch and configure deep sleep mode */
1117 error = mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
1118 if (error < 0) {
1119 dev_err(&data->client->dev, "failed to disable touch\n");
1120 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001121 }
1122
Jing Lin36aee812011-10-17 17:17:28 -07001123 error = __mxt_write_reg(data->client, data->t7_start_addr,
1124 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001125 if (error < 0) {
1126 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001127 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001128 return error;
1129 }
1130
1131 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132}
1133
Iiro Valkonen7686b102011-02-02 23:21:58 -08001134static int mxt_input_open(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_start(data);
1140 if (error < 0) {
1141 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1142 return error;
1143 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001144
1145 return 0;
1146}
1147
Iiro Valkonen7686b102011-02-02 23:21:58 -08001148static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001149{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001150 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001151 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001152
Amy Maloche52262212011-09-15 16:46:57 -07001153 error = mxt_stop(data);
1154 if (error < 0)
1155 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1156
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001157}
1158
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301159static int mxt_power_on(struct mxt_data *data, bool on)
1160{
1161 int rc;
1162
1163 if (on == false)
1164 goto power_off;
1165
1166 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1167 if (rc < 0) {
1168 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1169 rc);
1170 return rc;
1171 }
1172
1173 rc = regulator_enable(data->vcc);
1174 if (rc) {
1175 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1176 rc);
1177 goto error_reg_en_vcc;
1178 }
1179
1180 if (data->pdata->i2c_pull_up) {
1181 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1182 if (rc < 0) {
1183 dev_err(&data->client->dev,
1184 "Regulator set_opt failed rc=%d\n", rc);
1185 goto error_reg_opt_i2c;
1186 }
1187
1188 rc = regulator_enable(data->vcc_i2c);
1189 if (rc) {
1190 dev_err(&data->client->dev,
1191 "Regulator enable failed rc=%d\n", rc);
1192 goto error_reg_en_vcc_i2c;
1193 }
1194 }
1195
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001196 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301197
1198 return 0;
1199
1200error_reg_en_vcc_i2c:
1201 if (data->pdata->i2c_pull_up)
1202 regulator_set_optimum_mode(data->vcc_i2c, 0);
1203error_reg_opt_i2c:
1204 regulator_disable(data->vcc);
1205error_reg_en_vcc:
1206 regulator_set_optimum_mode(data->vcc, 0);
1207 return rc;
1208
1209power_off:
1210 regulator_set_optimum_mode(data->vcc, 0);
1211 regulator_disable(data->vcc);
1212 if (data->pdata->i2c_pull_up) {
1213 regulator_set_optimum_mode(data->vcc_i2c, 0);
1214 regulator_disable(data->vcc_i2c);
1215 }
1216 msleep(50);
1217 return 0;
1218}
1219
1220static int mxt_regulator_configure(struct mxt_data *data, bool on)
1221{
1222 int rc;
1223
1224 if (on == false)
1225 goto hw_shutdown;
1226
1227 data->vcc = regulator_get(&data->client->dev, "vdd");
1228 if (IS_ERR(data->vcc)) {
1229 rc = PTR_ERR(data->vcc);
1230 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1231 rc);
1232 return rc;
1233 }
1234
1235 if (regulator_count_voltages(data->vcc) > 0) {
1236 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1237 MXT_VTG_MAX_UV);
1238 if (rc) {
1239 dev_err(&data->client->dev,
1240 "regulator set_vtg failed rc=%d\n", rc);
1241 goto error_set_vtg_vcc;
1242 }
1243 }
1244
1245 if (data->pdata->i2c_pull_up) {
1246 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1247 if (IS_ERR(data->vcc_i2c)) {
1248 rc = PTR_ERR(data->vcc_i2c);
1249 dev_err(&data->client->dev,
1250 "Regulator get failed rc=%d\n", rc);
1251 goto error_get_vtg_i2c;
1252 }
1253 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1254 rc = regulator_set_voltage(data->vcc_i2c,
1255 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1256 if (rc) {
1257 dev_err(&data->client->dev,
1258 "regulator set_vtg failed rc=%d\n", rc);
1259 goto error_set_vtg_i2c;
1260 }
1261 }
1262 }
1263
1264 return 0;
1265
1266error_set_vtg_i2c:
1267 regulator_put(data->vcc_i2c);
1268error_get_vtg_i2c:
1269 if (regulator_count_voltages(data->vcc) > 0)
1270 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1271error_set_vtg_vcc:
1272 regulator_put(data->vcc);
1273 return rc;
1274
1275hw_shutdown:
1276 if (regulator_count_voltages(data->vcc) > 0)
1277 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1278 regulator_put(data->vcc);
1279 if (data->pdata->i2c_pull_up) {
1280 if (regulator_count_voltages(data->vcc_i2c) > 0)
1281 regulator_set_voltage(data->vcc_i2c, 0,
1282 MXT_I2C_VTG_MAX_UV);
1283 regulator_put(data->vcc_i2c);
1284 }
1285 return 0;
1286}
1287
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301288#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001289static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1290{
1291
1292 int rc;
1293
1294 if (on == false)
1295 goto regulator_hpm;
1296
1297 rc = regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1298 if (rc < 0) {
1299 dev_err(&data->client->dev,
1300 "Regulator set_opt failed rc=%d\n", rc);
1301 goto fail_regulator_lpm;
1302 }
1303
1304 if (data->pdata->i2c_pull_up) {
1305 rc = regulator_set_optimum_mode(data->vcc_i2c,
1306 MXT_I2C_LPM_LOAD_UA);
1307 if (rc < 0) {
1308 dev_err(&data->client->dev,
1309 "Regulator set_opt failed rc=%d\n", rc);
1310 goto fail_regulator_lpm;
1311 }
1312 }
1313
1314 return 0;
1315
1316regulator_hpm:
1317
1318 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1319 if (rc < 0) {
1320 dev_err(&data->client->dev,
1321 "Regulator set_opt failed rc=%d\n", rc);
1322 goto fail_regulator_hpm;
1323 }
1324
1325 if (data->pdata->i2c_pull_up) {
1326 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_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
1334 return 0;
1335
1336fail_regulator_lpm:
1337 regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1338 if (data->pdata->i2c_pull_up)
1339 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1340
1341 return rc;
1342
1343fail_regulator_hpm:
1344 regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1345 if (data->pdata->i2c_pull_up)
1346 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1347
1348 return rc;
1349}
1350
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301351static int mxt_suspend(struct device *dev)
1352{
1353 struct i2c_client *client = to_i2c_client(dev);
1354 struct mxt_data *data = i2c_get_clientdata(client);
1355 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001356 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301357
1358 mutex_lock(&input_dev->mutex);
1359
Amy Maloche52262212011-09-15 16:46:57 -07001360 if (input_dev->users) {
1361 error = mxt_stop(data);
1362 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001363 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001364 mutex_unlock(&input_dev->mutex);
1365 return error;
1366 }
1367
1368 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301369
1370 mutex_unlock(&input_dev->mutex);
1371
Jing Linbace50b2011-10-18 22:55:47 -07001372 /* put regulators in low power mode */
1373 error = mxt_regulator_lpm(data, true);
1374 if (error < 0) {
1375 dev_err(dev, "failed to enter low power mode\n");
1376 return error;
1377 }
1378
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301379 return 0;
1380}
1381
1382static int mxt_resume(struct device *dev)
1383{
1384 struct i2c_client *client = to_i2c_client(dev);
1385 struct mxt_data *data = i2c_get_clientdata(client);
1386 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001387 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301388
Jing Linbace50b2011-10-18 22:55:47 -07001389 /* put regulators in high power mode */
1390 error = mxt_regulator_lpm(data, false);
1391 if (error < 0) {
1392 dev_err(dev, "failed to enter high power mode\n");
1393 return error;
1394 }
1395
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301396 mutex_lock(&input_dev->mutex);
1397
Amy Maloche52262212011-09-15 16:46:57 -07001398 if (input_dev->users) {
1399 error = mxt_start(data);
1400 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001401 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001402 mutex_unlock(&input_dev->mutex);
1403 return error;
1404 }
1405 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301406
1407 mutex_unlock(&input_dev->mutex);
1408
1409 return 0;
1410}
1411
1412#if defined(CONFIG_HAS_EARLYSUSPEND)
1413static void mxt_early_suspend(struct early_suspend *h)
1414{
1415 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1416
1417 mxt_suspend(&data->client->dev);
1418}
1419
1420static void mxt_late_resume(struct early_suspend *h)
1421{
1422 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1423
1424 mxt_resume(&data->client->dev);
1425}
1426#endif
1427
1428static const struct dev_pm_ops mxt_pm_ops = {
1429#ifndef CONFIG_HAS_EARLYSUSPEND
1430 .suspend = mxt_suspend,
1431 .resume = mxt_resume,
1432#endif
1433};
1434#endif
1435
Iiro Valkonen7686b102011-02-02 23:21:58 -08001436static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001437 const struct i2c_device_id *id)
1438{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001439 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001440 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001441 struct input_dev *input_dev;
1442 int error;
1443
Iiro Valkonen919ed892011-02-15 13:36:52 -08001444 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001445 return -EINVAL;
1446
Iiro Valkonen7686b102011-02-02 23:21:58 -08001447 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001448 input_dev = input_allocate_device();
1449 if (!data || !input_dev) {
1450 dev_err(&client->dev, "Failed to allocate memory\n");
1451 error = -ENOMEM;
1452 goto err_free_mem;
1453 }
1454
Iiro Valkonen7686b102011-02-02 23:21:58 -08001455 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001456 input_dev->id.bustype = BUS_I2C;
1457 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001458 input_dev->open = mxt_input_open;
1459 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001460
Joonyoung Shim910d8052011-04-12 23:14:38 -07001461 data->client = client;
1462 data->input_dev = input_dev;
1463 data->pdata = pdata;
1464 data->irq = client->irq;
1465
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001466 __set_bit(EV_ABS, input_dev->evbit);
1467 __set_bit(EV_KEY, input_dev->evbit);
1468 __set_bit(BTN_TOUCH, input_dev->keybit);
1469
1470 /* For single touch */
1471 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001472 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001473 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001474 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001475
1476 /* For multi touch */
1477 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001478 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001479 input_set_abs_params(input_dev, ABS_MT_POSITION_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_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001482 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001483
1484 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001485 i2c_set_clientdata(client, data);
1486
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301487 if (pdata->init_hw)
1488 error = pdata->init_hw(true);
1489 else
1490 error = mxt_regulator_configure(data, true);
1491 if (error) {
1492 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001493 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301494 }
1495
1496 if (pdata->power_on)
1497 error = pdata->power_on(true);
1498 else
1499 error = mxt_power_on(data, true);
1500 if (error) {
1501 dev_err(&client->dev, "Failed to power on hardware\n");
1502 goto err_regulator_on;
1503 }
1504
Iiro Valkonen7686b102011-02-02 23:21:58 -08001505 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001506 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301507 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001508
Iiro Valkonen7686b102011-02-02 23:21:58 -08001509 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001510 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001511 if (error) {
1512 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001513 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001514 }
1515
Iiro Valkonen08960a02011-04-12 23:16:40 -07001516 error = mxt_make_highchg(data);
1517 if (error)
1518 goto err_free_irq;
1519
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001520 error = input_register_device(input_dev);
1521 if (error)
1522 goto err_free_irq;
1523
Iiro Valkonen7686b102011-02-02 23:21:58 -08001524 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001525 if (error)
1526 goto err_unregister_device;
1527
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301528#if defined(CONFIG_HAS_EARLYSUSPEND)
1529 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1530 MXT_SUSPEND_LEVEL;
1531 data->early_suspend.suspend = mxt_early_suspend;
1532 data->early_suspend.resume = mxt_late_resume;
1533 register_early_suspend(&data->early_suspend);
1534#endif
1535
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001536 return 0;
1537
1538err_unregister_device:
1539 input_unregister_device(input_dev);
1540 input_dev = NULL;
1541err_free_irq:
1542 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001543err_free_object:
1544 kfree(data->object_table);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301545err_power_on:
1546 if (pdata->power_on)
1547 pdata->power_on(false);
1548 else
1549 mxt_power_on(data, false);
1550err_regulator_on:
1551 if (pdata->init_hw)
1552 pdata->init_hw(false);
1553 else
1554 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001555err_free_mem:
1556 input_free_device(input_dev);
1557 kfree(data);
1558 return error;
1559}
1560
Iiro Valkonen7686b102011-02-02 23:21:58 -08001561static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001562{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001563 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001564
Iiro Valkonen7686b102011-02-02 23:21:58 -08001565 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001566 free_irq(data->irq, data);
1567 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301568#if defined(CONFIG_HAS_EARLYSUSPEND)
1569 unregister_early_suspend(&data->early_suspend);
1570#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301571
1572 if (data->pdata->power_on)
1573 data->pdata->power_on(false);
1574 else
1575 mxt_power_on(data, false);
1576
1577 if (data->pdata->init_hw)
1578 data->pdata->init_hw(false);
1579 else
1580 mxt_regulator_configure(data, false);
1581
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001582 kfree(data->object_table);
1583 kfree(data);
1584
1585 return 0;
1586}
1587
Iiro Valkonen7686b102011-02-02 23:21:58 -08001588static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001589 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001590 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001591 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001592 { }
1593};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001594MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001595
Iiro Valkonen7686b102011-02-02 23:21:58 -08001596static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001597 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001598 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001599 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001600#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001601 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001602#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001603 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001604 .probe = mxt_probe,
1605 .remove = __devexit_p(mxt_remove),
1606 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001607};
1608
Iiro Valkonen7686b102011-02-02 23:21:58 -08001609static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001610{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001611 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001612}
1613
Iiro Valkonen7686b102011-02-02 23:21:58 -08001614static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001615{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001616 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001617}
1618
Iiro Valkonen7686b102011-02-02 23:21:58 -08001619module_init(mxt_init);
1620module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001621
1622/* Module information */
1623MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001624MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001625MODULE_LICENSE("GPL");