blob: c16dd413b37d3c34ebb494c7e0525b02546fca9f [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>
Amy Maloche08266db2011-11-04 11:07:16 -070024#include <linux/gpio.h>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053025#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070026
Anirudh Ghayal253ce122011-08-09 19:32:57 +053027#if defined(CONFIG_HAS_EARLYSUSPEND)
28#include <linux/earlysuspend.h>
29/* Early-suspend level */
30#define MXT_SUSPEND_LEVEL 1
31#endif
32
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070033/* Family ID */
34#define MXT224_ID 0x80
35#define MXT1386_ID 0xA0
36
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070037/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080038#define MXT_VER_20 20
39#define MXT_VER_21 21
40#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070041
42/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080043#define MXT_APP_LOW 0x4a
44#define MXT_APP_HIGH 0x4b
45#define MXT_BOOT_LOW 0x24
46#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070047
48/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080049#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070050
51/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080052#define MXT_FAMILY_ID 0x00
53#define MXT_VARIANT_ID 0x01
54#define MXT_VERSION 0x02
55#define MXT_BUILD 0x03
56#define MXT_MATRIX_X_SIZE 0x04
57#define MXT_MATRIX_Y_SIZE 0x05
58#define MXT_OBJECT_NUM 0x06
59#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070060
Iiro Valkonen7686b102011-02-02 23:21:58 -080061#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070062
63/* Object types */
Iiro Valkonene8645592011-11-18 12:56:19 -080064#define MXT_DEBUG_DIAGNOSTIC_T37 37
65#define MXT_GEN_MESSAGE_T5 5
66#define MXT_GEN_COMMAND_T6 6
67#define MXT_GEN_POWER_T7 7
68#define MXT_GEN_ACQUIRE_T8 8
69#define MXT_GEN_DATASOURCE_T53 53
70#define MXT_TOUCH_MULTI_T9 9
71#define MXT_TOUCH_KEYARRAY_T15 15
72#define MXT_TOUCH_PROXIMITY_T23 23
73#define MXT_TOUCH_PROXKEY_T52 52
74#define MXT_PROCI_GRIPFACE_T20 20
75#define MXT_PROCG_NOISE_T22 22
76#define MXT_PROCI_ONETOUCH_T24 24
77#define MXT_PROCI_TWOTOUCH_T27 27
78#define MXT_PROCI_GRIP_T40 40
79#define MXT_PROCI_PALM_T41 41
80#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
81#define MXT_PROCI_STYLUS_T47 47
82#define MXT_PROCG_NOISESUPPRESSION_T48 48
83#define MXT_SPT_COMMSCONFIG_T18 18
84#define MXT_SPT_GPIOPWM_T19 19
85#define MXT_SPT_SELFTEST_T25 25
86#define MXT_SPT_CTECONFIG_T28 28
87#define MXT_SPT_USERDATA_T38 38
88#define MXT_SPT_DIGITIZER_T43 43
89#define MXT_SPT_MESSAGECOUNT_T44 44
90#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonene8645592011-11-18 12:56:19 -080092/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080093#define MXT_COMMAND_RESET 0
94#define MXT_COMMAND_BACKUPNV 1
95#define MXT_COMMAND_CALIBRATE 2
96#define MXT_COMMAND_REPORTALL 3
97#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070098
Iiro Valkonene8645592011-11-18 12:56:19 -080099/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800100#define MXT_POWER_IDLEACQINT 0
101#define MXT_POWER_ACTVACQINT 1
102#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700103
Iiro Valkonene8645592011-11-18 12:56:19 -0800104/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800105#define MXT_ACQUIRE_CHRGTIME 0
106#define MXT_ACQUIRE_TCHDRIFT 2
107#define MXT_ACQUIRE_DRIFTST 3
108#define MXT_ACQUIRE_TCHAUTOCAL 4
109#define MXT_ACQUIRE_SYNC 5
110#define MXT_ACQUIRE_ATCHCALST 6
111#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700112
Iiro Valkonene8645592011-11-18 12:56:19 -0800113/* MXT_TOUCH_MULT_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800114#define MXT_TOUCH_CTRL 0
115#define MXT_TOUCH_XORIGIN 1
116#define MXT_TOUCH_YORIGIN 2
117#define MXT_TOUCH_XSIZE 3
118#define MXT_TOUCH_YSIZE 4
119#define MXT_TOUCH_BLEN 6
120#define MXT_TOUCH_TCHTHR 7
121#define MXT_TOUCH_TCHDI 8
122#define MXT_TOUCH_ORIENT 9
123#define MXT_TOUCH_MOVHYSTI 11
124#define MXT_TOUCH_MOVHYSTN 12
125#define MXT_TOUCH_NUMTOUCH 14
126#define MXT_TOUCH_MRGHYST 15
127#define MXT_TOUCH_MRGTHR 16
128#define MXT_TOUCH_AMPHYST 17
129#define MXT_TOUCH_XRANGE_LSB 18
130#define MXT_TOUCH_XRANGE_MSB 19
131#define MXT_TOUCH_YRANGE_LSB 20
132#define MXT_TOUCH_YRANGE_MSB 21
133#define MXT_TOUCH_XLOCLIP 22
134#define MXT_TOUCH_XHICLIP 23
135#define MXT_TOUCH_YLOCLIP 24
136#define MXT_TOUCH_YHICLIP 25
137#define MXT_TOUCH_XEDGECTRL 26
138#define MXT_TOUCH_XEDGEDIST 27
139#define MXT_TOUCH_YEDGECTRL 28
140#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700141#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700142
Iiro Valkonene8645592011-11-18 12:56:19 -0800143/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800144#define MXT_GRIPFACE_CTRL 0
145#define MXT_GRIPFACE_XLOGRIP 1
146#define MXT_GRIPFACE_XHIGRIP 2
147#define MXT_GRIPFACE_YLOGRIP 3
148#define MXT_GRIPFACE_YHIGRIP 4
149#define MXT_GRIPFACE_MAXTCHS 5
150#define MXT_GRIPFACE_SZTHR1 7
151#define MXT_GRIPFACE_SZTHR2 8
152#define MXT_GRIPFACE_SHPTHR1 9
153#define MXT_GRIPFACE_SHPTHR2 10
154#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700155
Iiro Valkonen7686b102011-02-02 23:21:58 -0800156/* MXT_PROCI_NOISE field */
157#define MXT_NOISE_CTRL 0
158#define MXT_NOISE_OUTFLEN 1
159#define MXT_NOISE_GCAFUL_LSB 3
160#define MXT_NOISE_GCAFUL_MSB 4
161#define MXT_NOISE_GCAFLL_LSB 5
162#define MXT_NOISE_GCAFLL_MSB 6
163#define MXT_NOISE_ACTVGCAFVALID 7
164#define MXT_NOISE_NOISETHR 8
165#define MXT_NOISE_FREQHOPSCALE 10
166#define MXT_NOISE_FREQ0 11
167#define MXT_NOISE_FREQ1 12
168#define MXT_NOISE_FREQ2 13
169#define MXT_NOISE_FREQ3 14
170#define MXT_NOISE_FREQ4 15
171#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700172
Iiro Valkonene8645592011-11-18 12:56:19 -0800173/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800174#define MXT_COMMS_CTRL 0
175#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700176
Iiro Valkonene8645592011-11-18 12:56:19 -0800177/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_CTE_CTRL 0
179#define MXT_CTE_CMD 1
180#define MXT_CTE_MODE 2
181#define MXT_CTE_IDLEGCAFDEPTH 3
182#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700183#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700184
Iiro Valkonen7686b102011-02-02 23:21:58 -0800185#define MXT_VOLTAGE_DEFAULT 2700000
186#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700187
Amy Maloche21115eb2011-11-02 09:04:37 -0700188/* Analog voltage @2.7 V */
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530189#define MXT_VTG_MIN_UV 2700000
190#define MXT_VTG_MAX_UV 3300000
191#define MXT_ACTIVE_LOAD_UA 15000
Jing Linbace50b2011-10-18 22:55:47 -0700192#define MXT_LPM_LOAD_UA 10
Amy Maloche21115eb2011-11-02 09:04:37 -0700193/* Digital voltage @1.8 V */
194#define MXT_VTG_DIG_MIN_UV 1800000
195#define MXT_VTG_DIG_MAX_UV 1800000
196#define MXT_ACTIVE_LOAD_DIG_UA 10000
197#define MXT_LPM_LOAD_DIG_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530198
199#define MXT_I2C_VTG_MIN_UV 1800000
200#define MXT_I2C_VTG_MAX_UV 1800000
201#define MXT_I2C_LOAD_UA 10000
Jing Linbace50b2011-10-18 22:55:47 -0700202#define MXT_I2C_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530203
Iiro Valkonene8645592011-11-18 12:56:19 -0800204/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800205#define MXT_BOOT_VALUE 0xa5
206#define MXT_BACKUP_VALUE 0x55
207#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700208#define MXT224_RESET_TIME 65 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700209#define MXT1386_RESET_TIME 250 /* msec */
210#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700211#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700212
Iiro Valkonen7686b102011-02-02 23:21:58 -0800213#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700214
Jing Lin36aee812011-10-17 17:17:28 -0700215#define MXT_WAKE_TIME 25
216
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218#define MXT_UNLOCK_CMD_MSB 0xaa
219#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700220
221/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800222#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
223#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
224#define MXT_FRAME_CRC_CHECK 0x02
225#define MXT_FRAME_CRC_FAIL 0x03
226#define MXT_FRAME_CRC_PASS 0x04
227#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
228#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700229
230/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800231#define MXT_SUPPRESS (1 << 1)
232#define MXT_AMP (1 << 2)
233#define MXT_VECTOR (1 << 3)
234#define MXT_MOVE (1 << 4)
235#define MXT_RELEASE (1 << 5)
236#define MXT_PRESS (1 << 6)
237#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700238
Joonyoung Shim910d8052011-04-12 23:14:38 -0700239/* Touch orient bits */
240#define MXT_XY_SWITCH (1 << 0)
241#define MXT_X_INVERT (1 << 1)
242#define MXT_Y_INVERT (1 << 2)
243
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700244/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800245#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700246
Iiro Valkonen7686b102011-02-02 23:21:58 -0800247#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700248
Jing Lin36aee812011-10-17 17:17:28 -0700249#define T7_DATA_SIZE 3
250#define MXT_MAX_RW_TRIES 3
251#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530252
Iiro Valkonen7686b102011-02-02 23:21:58 -0800253struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700254 u8 family_id;
255 u8 variant_id;
256 u8 version;
257 u8 build;
258 u8 matrix_xsize;
259 u8 matrix_ysize;
260 u8 object_num;
261};
262
Iiro Valkonen7686b102011-02-02 23:21:58 -0800263struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700264 u8 type;
265 u16 start_address;
266 u8 size;
267 u8 instances;
268 u8 num_report_ids;
269
270 /* to map object and message */
271 u8 max_reportid;
272};
273
Iiro Valkonen7686b102011-02-02 23:21:58 -0800274struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700275 u8 reportid;
276 u8 message[7];
277 u8 checksum;
278};
279
Iiro Valkonen7686b102011-02-02 23:21:58 -0800280struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700281 int status;
282 int x;
283 int y;
284 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700285 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700286};
287
288/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800289struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700290 struct i2c_client *client;
291 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800292 const struct mxt_platform_data *pdata;
293 struct mxt_object *object_table;
294 struct mxt_info info;
295 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700296 unsigned int irq;
Amy Maloche21115eb2011-11-02 09:04:37 -0700297 struct regulator *vcc_ana;
298 struct regulator *vcc_dig;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530299 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530300#if defined(CONFIG_HAS_EARLYSUSPEND)
301 struct early_suspend early_suspend;
302#endif
Jing Lin36aee812011-10-17 17:17:28 -0700303
Amy Maloche52262212011-09-15 16:46:57 -0700304 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700305 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700306 u8 t9_ctrl;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700307};
308
Iiro Valkonen7686b102011-02-02 23:21:58 -0800309static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700310{
311 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800312 case MXT_GEN_MESSAGE_T5:
313 case MXT_GEN_COMMAND_T6:
314 case MXT_GEN_POWER_T7:
315 case MXT_GEN_ACQUIRE_T8:
316 case MXT_GEN_DATASOURCE_T53:
317 case MXT_TOUCH_MULTI_T9:
318 case MXT_TOUCH_KEYARRAY_T15:
319 case MXT_TOUCH_PROXIMITY_T23:
320 case MXT_TOUCH_PROXKEY_T52:
321 case MXT_PROCI_GRIPFACE_T20:
322 case MXT_PROCG_NOISE_T22:
323 case MXT_PROCI_ONETOUCH_T24:
324 case MXT_PROCI_TWOTOUCH_T27:
325 case MXT_PROCI_GRIP_T40:
326 case MXT_PROCI_PALM_T41:
327 case MXT_PROCI_TOUCHSUPPRESSION_T42:
328 case MXT_PROCI_STYLUS_T47:
329 case MXT_PROCG_NOISESUPPRESSION_T48:
330 case MXT_SPT_COMMSCONFIG_T18:
331 case MXT_SPT_GPIOPWM_T19:
332 case MXT_SPT_SELFTEST_T25:
333 case MXT_SPT_CTECONFIG_T28:
334 case MXT_SPT_USERDATA_T38:
335 case MXT_SPT_DIGITIZER_T43:
336 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700337 return true;
338 default:
339 return false;
340 }
341}
342
Iiro Valkonen7686b102011-02-02 23:21:58 -0800343static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700344{
345 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800346 case MXT_GEN_COMMAND_T6:
347 case MXT_GEN_POWER_T7:
348 case MXT_GEN_ACQUIRE_T8:
349 case MXT_TOUCH_MULTI_T9:
350 case MXT_TOUCH_KEYARRAY_T15:
351 case MXT_TOUCH_PROXIMITY_T23:
352 case MXT_TOUCH_PROXKEY_T52:
353 case MXT_PROCI_GRIPFACE_T20:
354 case MXT_PROCG_NOISE_T22:
355 case MXT_PROCI_ONETOUCH_T24:
356 case MXT_PROCI_TWOTOUCH_T27:
357 case MXT_PROCI_GRIP_T40:
358 case MXT_PROCI_PALM_T41:
359 case MXT_PROCI_TOUCHSUPPRESSION_T42:
360 case MXT_PROCI_STYLUS_T47:
361 case MXT_PROCG_NOISESUPPRESSION_T48:
362 case MXT_SPT_COMMSCONFIG_T18:
363 case MXT_SPT_GPIOPWM_T19:
364 case MXT_SPT_SELFTEST_T25:
365 case MXT_SPT_CTECONFIG_T28:
366 case MXT_SPT_USERDATA_T38:
367 case MXT_SPT_DIGITIZER_T43:
368 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700369 return true;
370 default:
371 return false;
372 }
373}
374
Iiro Valkonen7686b102011-02-02 23:21:58 -0800375static void mxt_dump_message(struct device *dev,
376 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700377{
378 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
379 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
380 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
381 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
382 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
383 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
384 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
385 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
386 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
387}
388
Iiro Valkonen7686b102011-02-02 23:21:58 -0800389static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700390 unsigned int state)
391{
392 u8 val;
393
394recheck:
395 if (i2c_master_recv(client, &val, 1) != 1) {
396 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
397 return -EIO;
398 }
399
400 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800401 case MXT_WAITING_BOOTLOAD_CMD:
402 case MXT_WAITING_FRAME_DATA:
403 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700404 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800405 case MXT_FRAME_CRC_PASS:
406 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700407 goto recheck;
408 break;
409 default:
410 return -EINVAL;
411 }
412
413 if (val != state) {
414 dev_err(&client->dev, "Unvalid bootloader mode state\n");
415 return -EINVAL;
416 }
417
418 return 0;
419}
420
Iiro Valkonen7686b102011-02-02 23:21:58 -0800421static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700422{
423 u8 buf[2];
424
Iiro Valkonen7686b102011-02-02 23:21:58 -0800425 buf[0] = MXT_UNLOCK_CMD_LSB;
426 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700427
428 if (i2c_master_send(client, buf, 2) != 2) {
429 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
430 return -EIO;
431 }
432
433 return 0;
434}
435
Iiro Valkonen7686b102011-02-02 23:21:58 -0800436static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700437 const u8 *data, unsigned int frame_size)
438{
439 if (i2c_master_send(client, data, frame_size) != frame_size) {
440 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
441 return -EIO;
442 }
443
444 return 0;
445}
446
Iiro Valkonen7686b102011-02-02 23:21:58 -0800447static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700448 u16 reg, u16 len, void *val)
449{
450 struct i2c_msg xfer[2];
451 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700452 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700453
454 buf[0] = reg & 0xff;
455 buf[1] = (reg >> 8) & 0xff;
456
457 /* Write register */
458 xfer[0].addr = client->addr;
459 xfer[0].flags = 0;
460 xfer[0].len = 2;
461 xfer[0].buf = buf;
462
463 /* Read data */
464 xfer[1].addr = client->addr;
465 xfer[1].flags = I2C_M_RD;
466 xfer[1].len = len;
467 xfer[1].buf = val;
468
Jing Lin36aee812011-10-17 17:17:28 -0700469 do {
470 if (i2c_transfer(client->adapter, xfer, 2) == 2)
471 return 0;
472 msleep(MXT_WAKE_TIME);
473 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700474
Jing Lin36aee812011-10-17 17:17:28 -0700475 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
476 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700477}
478
Iiro Valkonen7686b102011-02-02 23:21:58 -0800479static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700480{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800481 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482}
483
Jing Lin36aee812011-10-17 17:17:28 -0700484static int __mxt_write_reg(struct i2c_client *client,
485 u16 addr, u16 length, u8 *value)
486{
487 u8 buf[MXT_BLOCK_SIZE + 2];
488 int i, tries = 0;
489
490 if (length > MXT_BLOCK_SIZE)
491 return -EINVAL;
492
493 buf[0] = addr & 0xff;
494 buf[1] = (addr >> 8) & 0xff;
495 for (i = 0; i < length; i++)
496 buf[i + 2] = *value++;
497
498 do {
499 if (i2c_master_send(client, buf, length + 2) == (length + 2))
500 return 0;
501 msleep(MXT_WAKE_TIME);
502 } while (++tries < MXT_MAX_RW_TRIES);
503
504 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
505 return -EIO;
506}
507
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700509{
Jing Lin36aee812011-10-17 17:17:28 -0700510 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511}
512
Iiro Valkonen7686b102011-02-02 23:21:58 -0800513static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514 u16 reg, u8 *object_buf)
515{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517 object_buf);
518}
519
Iiro Valkonen7686b102011-02-02 23:21:58 -0800520static struct mxt_object *
521mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800523 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700524 int i;
525
526 for (i = 0; i < data->info.object_num; i++) {
527 object = data->object_table + i;
528 if (object->type == type)
529 return object;
530 }
531
532 dev_err(&data->client->dev, "Invalid object type\n");
533 return NULL;
534}
535
Iiro Valkonen7686b102011-02-02 23:21:58 -0800536static int mxt_read_message(struct mxt_data *data,
537 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800539 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700540 u16 reg;
541
Iiro Valkonene8645592011-11-18 12:56:19 -0800542 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700543 if (!object)
544 return -EINVAL;
545
546 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800547 return __mxt_read_reg(data->client, reg,
548 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700549}
550
Iiro Valkonen7686b102011-02-02 23:21:58 -0800551static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700552 u8 type, u8 offset, u8 *val)
553{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800554 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700555 u16 reg;
556
Iiro Valkonen7686b102011-02-02 23:21:58 -0800557 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700558 if (!object)
559 return -EINVAL;
560
561 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800562 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700563}
564
Iiro Valkonen7686b102011-02-02 23:21:58 -0800565static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700566 u8 type, u8 offset, u8 val)
567{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800568 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569 u16 reg;
570
Iiro Valkonen7686b102011-02-02 23:21:58 -0800571 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572 if (!object)
573 return -EINVAL;
574
575 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800576 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577}
578
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800581 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 struct input_dev *input_dev = data->input_dev;
583 int status = finger[single_id].status;
584 int finger_num = 0;
585 int id;
586
Iiro Valkonen7686b102011-02-02 23:21:58 -0800587 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700588 if (!finger[id].status)
589 continue;
590
Amy Maloche2b59bab2011-10-13 16:08:16 -0700591 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
592 finger[id].status != MXT_RELEASE ?
593 finger[id].area : 0);
594 input_report_abs(input_dev, ABS_MT_POSITION_X,
595 finger[id].x);
596 input_report_abs(input_dev, ABS_MT_POSITION_Y,
597 finger[id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700598 input_report_abs(input_dev, ABS_MT_PRESSURE,
599 finger[id].pressure);
Amy Maloche2b59bab2011-10-13 16:08:16 -0700600 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601
Amy Maloche2b59bab2011-10-13 16:08:16 -0700602 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700603 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700604 else
605 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 }
607
608 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
609
Iiro Valkonen7686b102011-02-02 23:21:58 -0800610 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611 input_report_abs(input_dev, ABS_X, finger[single_id].x);
612 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700613 input_report_abs(input_dev,
614 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700615 }
616
617 input_sync(input_dev);
618}
619
Iiro Valkonen7686b102011-02-02 23:21:58 -0800620static void mxt_input_touchevent(struct mxt_data *data,
621 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700622{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624 struct device *dev = &data->client->dev;
625 u8 status = message->message[0];
626 int x;
627 int y;
628 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700629 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700630
631 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632 if (!(status & MXT_DETECT)) {
633 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634 dev_dbg(dev, "[%d] released\n", id);
635
Iiro Valkonen7686b102011-02-02 23:21:58 -0800636 finger[id].status = MXT_RELEASE;
637 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700638 }
639 return;
640 }
641
642 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800643 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700644 return;
645
Joonyoung Shim910d8052011-04-12 23:14:38 -0700646 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
647 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700648 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700649 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700650 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700651 y = y >> 2;
652
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700654 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700655
656 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800657 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658 x, y, area);
659
Iiro Valkonen7686b102011-02-02 23:21:58 -0800660 finger[id].status = status & MXT_MOVE ?
661 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662 finger[id].x = x;
663 finger[id].y = y;
664 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700665 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666
Iiro Valkonen7686b102011-02-02 23:21:58 -0800667 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668}
669
Iiro Valkonen7686b102011-02-02 23:21:58 -0800670static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700671{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800672 struct mxt_data *data = dev_id;
673 struct mxt_message message;
674 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675 struct device *dev = &data->client->dev;
676 int id;
677 u8 reportid;
678 u8 max_reportid;
679 u8 min_reportid;
680
681 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700683 dev_err(dev, "Failed to read message\n");
684 goto end;
685 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686 reportid = message.reportid;
687
Iiro Valkonene8645592011-11-18 12:56:19 -0800688 /* whether reportid is thing of MXT_TOUCH_MULTI_T9 */
689 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700690 if (!object)
691 goto end;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692 max_reportid = object->max_reportid;
693 min_reportid = max_reportid - object->num_report_ids + 1;
694 id = reportid - min_reportid;
695
696 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800697 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800699 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700 } while (reportid != 0xff);
701
702end:
703 return IRQ_HANDLED;
704}
705
Iiro Valkonen7686b102011-02-02 23:21:58 -0800706static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700707{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800708 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800709 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700710 struct device *dev = &data->client->dev;
711 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800712 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700713
Iiro Valkonen71749f52011-02-15 13:36:52 -0800714 if (!pdata->config) {
715 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
716 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717 }
718
719 for (i = 0; i < data->info.object_num; i++) {
720 object = data->object_table + i;
721
Iiro Valkonen7686b102011-02-02 23:21:58 -0800722 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700723 continue;
724
Iiro Valkonen71749f52011-02-15 13:36:52 -0800725 for (j = 0; j < object->size + 1; j++) {
726 config_offset = index + j;
727 if (config_offset > pdata->config_length) {
728 dev_err(dev, "Not enough config data!\n");
729 return -EINVAL;
730 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800731 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800732 pdata->config[config_offset]);
733 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 index += object->size + 1;
735 }
736
737 return 0;
738}
739
Iiro Valkonen7686b102011-02-02 23:21:58 -0800740static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700741{
742 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800743 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700744 int count = 10;
745 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700746
747 /* Read dummy message to make high CHG pin */
748 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800749 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700750 if (error)
751 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800752 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700753
754 if (!count) {
755 dev_err(dev, "CHG pin isn't cleared\n");
756 return -EBUSY;
757 }
758
759 return 0;
760}
761
Iiro Valkonen7686b102011-02-02 23:21:58 -0800762static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700763{
764 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766 int error;
767 u8 val;
768
Iiro Valkonen7686b102011-02-02 23:21:58 -0800769 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700770 if (error)
771 return error;
772 info->family_id = val;
773
Iiro Valkonen7686b102011-02-02 23:21:58 -0800774 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700775 if (error)
776 return error;
777 info->variant_id = val;
778
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700780 if (error)
781 return error;
782 info->version = val;
783
Iiro Valkonen7686b102011-02-02 23:21:58 -0800784 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700785 if (error)
786 return error;
787 info->build = val;
788
Iiro Valkonen7686b102011-02-02 23:21:58 -0800789 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700790 if (error)
791 return error;
792 info->object_num = val;
793
794 return 0;
795}
796
Iiro Valkonen7686b102011-02-02 23:21:58 -0800797static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798{
799 int error;
800 int i;
801 u16 reg;
802 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800803 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700804
805 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800806 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700807
Iiro Valkonen7686b102011-02-02 23:21:58 -0800808 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
809 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700810 if (error)
811 return error;
812
813 object->type = buf[0];
814 object->start_address = (buf[2] << 8) | buf[1];
815 object->size = buf[3];
816 object->instances = buf[4];
817 object->num_report_ids = buf[5];
818
819 if (object->num_report_ids) {
820 reportid += object->num_report_ids *
821 (object->instances + 1);
822 object->max_reportid = reportid;
823 }
824 }
825
826 return 0;
827}
828
Amy Maloche7e447432011-09-14 11:36:30 -0700829static void mxt_reset_delay(struct mxt_data *data)
830{
831 struct mxt_info *info = &data->info;
832
833 switch (info->family_id) {
834 case MXT224_ID:
835 msleep(MXT224_RESET_TIME);
836 break;
837 case MXT1386_ID:
838 msleep(MXT1386_RESET_TIME);
839 break;
840 default:
841 msleep(MXT_RESET_TIME);
842 }
843}
844
Iiro Valkonen7686b102011-02-02 23:21:58 -0800845static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700846{
847 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800848 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700849 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700850 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700851 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700852 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700853 struct mxt_object *t7_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700854
Iiro Valkonen7686b102011-02-02 23:21:58 -0800855 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856 if (error)
857 return error;
858
859 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800860 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700861 GFP_KERNEL);
862 if (!data->object_table) {
863 dev_err(&client->dev, "Failed to allocate memory\n");
864 return -ENOMEM;
865 }
866
867 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800868 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700869 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700870 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700871
872 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800873 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700874 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700875 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700876
Amy Maloche52262212011-09-15 16:46:57 -0700877 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -0800878 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -0700879 if (!t7_object) {
880 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -0700881 error = -EINVAL;
882 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -0700883 }
884
885 data->t7_start_addr = t7_object->start_address;
886 error = __mxt_read_reg(client, data->t7_start_addr,
887 T7_DATA_SIZE, data->t7_data);
888 if (error < 0) {
889 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -0700890 "Failed to save current power state\n");
891 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -0700892 }
Iiro Valkonene8645592011-11-18 12:56:19 -0800893 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -0700894 &data->t9_ctrl);
895 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -0700896 dev_err(&client->dev, "Failed to save current touch object\n");
897 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -0700898 }
899
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700900 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -0800901 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800902 MXT_COMMAND_BACKUPNV,
903 MXT_BACKUP_VALUE);
904 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700905 do {
Iiro Valkonene8645592011-11-18 12:56:19 -0800906 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700907 MXT_COMMAND_BACKUPNV,
908 &command_register);
909 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700910 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -0700911 usleep_range(1000, 2000);
912 } while ((command_register != 0) && (++timeout_counter <= 100));
913 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700914 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -0700915 error = -EIO;
916 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700917 }
918
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700919
920 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -0800921 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800922 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700923
Amy Maloche7e447432011-09-14 11:36:30 -0700924 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700925
926 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800927 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700928 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700929 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700930 info->matrix_xsize = val;
931
Iiro Valkonen7686b102011-02-02 23:21:58 -0800932 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700933 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700934 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700935 info->matrix_ysize = val;
936
937 dev_info(&client->dev,
938 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
939 info->family_id, info->variant_id, info->version,
940 info->build);
941
942 dev_info(&client->dev,
943 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
944 info->matrix_xsize, info->matrix_ysize,
945 info->object_num);
946
947 return 0;
Jing Lin32c72532011-11-03 12:02:33 -0700948
949free_object_table:
950 kfree(data->object_table);
951 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700952}
953
Iiro Valkonen7686b102011-02-02 23:21:58 -0800954static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955 struct device_attribute *attr, char *buf)
956{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957 struct mxt_data *data = dev_get_drvdata(dev);
958 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700959 int count = 0;
960 int i, j;
961 int error;
962 u8 val;
963
964 for (i = 0; i < data->info.object_num; i++) {
965 object = data->object_table + i;
966
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700967 count += snprintf(buf + count, PAGE_SIZE - count,
968 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700969 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700970 if (count >= PAGE_SIZE)
971 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972
Iiro Valkonen7686b102011-02-02 23:21:58 -0800973 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700974 count += snprintf(buf + count, PAGE_SIZE - count,
975 "\n");
976 if (count >= PAGE_SIZE)
977 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700978 continue;
979 }
980
981 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800982 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983 object->type, j, &val);
984 if (error)
985 return error;
986
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700987 count += snprintf(buf + count, PAGE_SIZE - count,
988 "\t[%2d]: %02x (%d)\n", j, val, val);
989 if (count >= PAGE_SIZE)
990 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991 }
992
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700993 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
994 if (count >= PAGE_SIZE)
995 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996 }
997
998 return count;
999}
1000
Iiro Valkonen7686b102011-02-02 23:21:58 -08001001static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001002{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001003 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001004 struct i2c_client *client = data->client;
1005 const struct firmware *fw = NULL;
1006 unsigned int frame_size;
1007 unsigned int pos = 0;
1008 int ret;
1009
1010 ret = request_firmware(&fw, fn, dev);
1011 if (ret) {
1012 dev_err(dev, "Unable to open firmware %s\n", fn);
1013 return ret;
1014 }
1015
1016 /* Change to the bootloader mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001017 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001018 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001019
1020 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021
1022 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001023 if (client->addr == MXT_APP_LOW)
1024 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001026 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027
Iiro Valkonen7686b102011-02-02 23:21:58 -08001028 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029 if (ret)
1030 goto out;
1031
1032 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001033 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001034
1035 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001036 ret = mxt_check_bootloader(client,
1037 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038 if (ret)
1039 goto out;
1040
1041 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1042
1043 /* We should add 2 at frame size as the the firmware data is not
1044 * included the CRC bytes.
1045 */
1046 frame_size += 2;
1047
1048 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050
Iiro Valkonen7686b102011-02-02 23:21:58 -08001051 ret = mxt_check_bootloader(client,
1052 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001053 if (ret)
1054 goto out;
1055
1056 pos += frame_size;
1057
1058 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1059 }
1060
1061out:
1062 release_firmware(fw);
1063
1064 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001065 if (client->addr == MXT_BOOT_LOW)
1066 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001068 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001069
1070 return ret;
1071}
1072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074 struct device_attribute *attr,
1075 const char *buf, size_t count)
1076{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001077 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078 int error;
1079
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080 disable_irq(data->irq);
1081
Iiro Valkonen7686b102011-02-02 23:21:58 -08001082 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083 if (error) {
1084 dev_err(dev, "The firmware update failed(%d)\n", error);
1085 count = error;
1086 } else {
1087 dev_dbg(dev, "The firmware update succeeded\n");
1088
1089 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001090 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001091
1092 kfree(data->object_table);
1093 data->object_table = NULL;
1094
Iiro Valkonen7686b102011-02-02 23:21:58 -08001095 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001096 }
1097
1098 enable_irq(data->irq);
1099
Iiro Valkonen08960a02011-04-12 23:16:40 -07001100 error = mxt_make_highchg(data);
1101 if (error)
1102 return error;
1103
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104 return count;
1105}
1106
Iiro Valkonen7686b102011-02-02 23:21:58 -08001107static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1108static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109
Iiro Valkonen7686b102011-02-02 23:21:58 -08001110static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111 &dev_attr_object.attr,
1112 &dev_attr_update_fw.attr,
1113 NULL
1114};
1115
Iiro Valkonen7686b102011-02-02 23:21:58 -08001116static const struct attribute_group mxt_attr_group = {
1117 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001118};
1119
Amy Maloche52262212011-09-15 16:46:57 -07001120static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001121{
Jing Lin36aee812011-10-17 17:17:28 -07001122 int error;
1123
Amy Maloche52262212011-09-15 16:46:57 -07001124 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001125 error = __mxt_write_reg(data->client, data->t7_start_addr,
1126 T7_DATA_SIZE, data->t7_data);
1127 if (error < 0) {
1128 dev_err(&data->client->dev,
1129 "failed to restore old power state\n");
1130 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001131 }
Jing Lin36aee812011-10-17 17:17:28 -07001132
Amy Maloche52262212011-09-15 16:46:57 -07001133 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001134 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001135 if (error < 0) {
1136 dev_err(&data->client->dev, "failed to restore touch\n");
1137 return error;
1138 }
1139
1140 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001141}
1142
Amy Maloche52262212011-09-15 16:46:57 -07001143static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001144{
Jing Lin36aee812011-10-17 17:17:28 -07001145 int error;
1146 u8 t7_data[T7_DATA_SIZE] = {0};
1147
1148 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001149 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001150 if (error < 0) {
1151 dev_err(&data->client->dev, "failed to disable touch\n");
1152 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001153 }
1154
Jing Lin36aee812011-10-17 17:17:28 -07001155 error = __mxt_write_reg(data->client, data->t7_start_addr,
1156 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001157 if (error < 0) {
1158 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001159 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001160 return error;
1161 }
1162
1163 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001164}
1165
Iiro Valkonen7686b102011-02-02 23:21:58 -08001166static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001167{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001168 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001169 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001170
Amy Maloche52262212011-09-15 16:46:57 -07001171 error = mxt_start(data);
1172 if (error < 0) {
1173 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1174 return error;
1175 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176
1177 return 0;
1178}
1179
Iiro Valkonen7686b102011-02-02 23:21:58 -08001180static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001181{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001182 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001183 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001184
Amy Maloche52262212011-09-15 16:46:57 -07001185 error = mxt_stop(data);
1186 if (error < 0)
1187 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1188
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001189}
1190
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301191static int mxt_power_on(struct mxt_data *data, bool on)
1192{
1193 int rc;
1194
1195 if (on == false)
1196 goto power_off;
1197
Amy Maloche21115eb2011-11-02 09:04:37 -07001198 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301199 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001200 dev_err(&data->client->dev,
1201 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301202 return rc;
1203 }
1204
Amy Maloche21115eb2011-11-02 09:04:37 -07001205 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301206 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001207 dev_err(&data->client->dev,
1208 "Regulator vcc_ana enable failed rc=%d\n", rc);
1209 goto error_reg_en_vcc_ana;
1210 }
1211
1212 if (data->pdata->digital_pwr_regulator) {
1213 rc = regulator_set_optimum_mode(data->vcc_dig,
1214 MXT_ACTIVE_LOAD_DIG_UA);
1215 if (rc < 0) {
1216 dev_err(&data->client->dev,
1217 "Regulator vcc_dig set_opt failed rc=%d\n",
1218 rc);
1219 goto error_reg_opt_vcc_dig;
1220 }
1221
1222 rc = regulator_enable(data->vcc_dig);
1223 if (rc) {
1224 dev_err(&data->client->dev,
1225 "Regulator vcc_dig enable failed rc=%d\n", rc);
1226 goto error_reg_en_vcc_dig;
1227 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301228 }
1229
1230 if (data->pdata->i2c_pull_up) {
1231 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1232 if (rc < 0) {
1233 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001234 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301235 goto error_reg_opt_i2c;
1236 }
1237
1238 rc = regulator_enable(data->vcc_i2c);
1239 if (rc) {
1240 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001241 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301242 goto error_reg_en_vcc_i2c;
1243 }
1244 }
1245
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001246 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301247
1248 return 0;
1249
1250error_reg_en_vcc_i2c:
1251 if (data->pdata->i2c_pull_up)
1252 regulator_set_optimum_mode(data->vcc_i2c, 0);
1253error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001254 if (data->pdata->digital_pwr_regulator)
1255 regulator_disable(data->vcc_dig);
1256error_reg_en_vcc_dig:
1257 if (data->pdata->digital_pwr_regulator)
1258 regulator_set_optimum_mode(data->vcc_dig, 0);
1259error_reg_opt_vcc_dig:
1260 regulator_disable(data->vcc_ana);
1261error_reg_en_vcc_ana:
1262 regulator_set_optimum_mode(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301263 return rc;
1264
1265power_off:
Amy Maloche21115eb2011-11-02 09:04:37 -07001266 regulator_set_optimum_mode(data->vcc_ana, 0);
1267 regulator_disable(data->vcc_ana);
1268 if (data->pdata->digital_pwr_regulator) {
1269 regulator_set_optimum_mode(data->vcc_dig, 0);
1270 regulator_disable(data->vcc_dig);
1271 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301272 if (data->pdata->i2c_pull_up) {
1273 regulator_set_optimum_mode(data->vcc_i2c, 0);
1274 regulator_disable(data->vcc_i2c);
1275 }
1276 msleep(50);
1277 return 0;
1278}
1279
1280static int mxt_regulator_configure(struct mxt_data *data, bool on)
1281{
1282 int rc;
1283
1284 if (on == false)
1285 goto hw_shutdown;
1286
Amy Maloche21115eb2011-11-02 09:04:37 -07001287 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1288 if (IS_ERR(data->vcc_ana)) {
1289 rc = PTR_ERR(data->vcc_ana);
1290 dev_err(&data->client->dev,
1291 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301292 return rc;
1293 }
1294
Amy Maloche21115eb2011-11-02 09:04:37 -07001295 if (regulator_count_voltages(data->vcc_ana) > 0) {
1296 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301297 MXT_VTG_MAX_UV);
1298 if (rc) {
1299 dev_err(&data->client->dev,
1300 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001301 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301302 }
1303 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001304 if (data->pdata->digital_pwr_regulator) {
1305 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1306 if (IS_ERR(data->vcc_dig)) {
1307 rc = PTR_ERR(data->vcc_dig);
1308 dev_err(&data->client->dev,
1309 "Regulator get dig failed rc=%d\n", rc);
1310 goto error_get_vtg_vcc_dig;
1311 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301312
Amy Maloche21115eb2011-11-02 09:04:37 -07001313 if (regulator_count_voltages(data->vcc_dig) > 0) {
1314 rc = regulator_set_voltage(data->vcc_dig,
1315 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1316 if (rc) {
1317 dev_err(&data->client->dev,
1318 "regulator set_vtg failed rc=%d\n", rc);
1319 goto error_set_vtg_vcc_dig;
1320 }
1321 }
1322 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301323 if (data->pdata->i2c_pull_up) {
1324 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1325 if (IS_ERR(data->vcc_i2c)) {
1326 rc = PTR_ERR(data->vcc_i2c);
1327 dev_err(&data->client->dev,
1328 "Regulator get failed rc=%d\n", rc);
1329 goto error_get_vtg_i2c;
1330 }
1331 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1332 rc = regulator_set_voltage(data->vcc_i2c,
1333 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1334 if (rc) {
1335 dev_err(&data->client->dev,
1336 "regulator set_vtg failed rc=%d\n", rc);
1337 goto error_set_vtg_i2c;
1338 }
1339 }
1340 }
1341
1342 return 0;
1343
1344error_set_vtg_i2c:
1345 regulator_put(data->vcc_i2c);
1346error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001347 if (data->pdata->digital_pwr_regulator)
1348 if (regulator_count_voltages(data->vcc_dig) > 0)
1349 regulator_set_voltage(data->vcc_dig, 0,
1350 MXT_VTG_DIG_MAX_UV);
1351error_set_vtg_vcc_dig:
1352 if (data->pdata->digital_pwr_regulator)
1353 regulator_put(data->vcc_dig);
1354error_get_vtg_vcc_dig:
1355 if (regulator_count_voltages(data->vcc_ana) > 0)
1356 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1357error_set_vtg_vcc_ana:
1358 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301359 return rc;
1360
1361hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001362 if (regulator_count_voltages(data->vcc_ana) > 0)
1363 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1364 regulator_put(data->vcc_ana);
1365 if (data->pdata->digital_pwr_regulator) {
1366 if (regulator_count_voltages(data->vcc_dig) > 0)
1367 regulator_set_voltage(data->vcc_dig, 0,
1368 MXT_VTG_DIG_MAX_UV);
1369 regulator_put(data->vcc_dig);
1370 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301371 if (data->pdata->i2c_pull_up) {
1372 if (regulator_count_voltages(data->vcc_i2c) > 0)
1373 regulator_set_voltage(data->vcc_i2c, 0,
1374 MXT_I2C_VTG_MAX_UV);
1375 regulator_put(data->vcc_i2c);
1376 }
1377 return 0;
1378}
1379
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301380#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001381static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1382{
1383
1384 int rc;
1385
1386 if (on == false)
1387 goto regulator_hpm;
1388
Amy Maloche21115eb2011-11-02 09:04:37 -07001389 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001390 if (rc < 0) {
1391 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001392 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001393 goto fail_regulator_lpm;
1394 }
1395
Amy Maloche21115eb2011-11-02 09:04:37 -07001396 if (data->pdata->digital_pwr_regulator) {
1397 rc = regulator_set_optimum_mode(data->vcc_dig,
1398 MXT_LPM_LOAD_DIG_UA);
1399 if (rc < 0) {
1400 dev_err(&data->client->dev,
1401 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1402 goto fail_regulator_lpm;
1403 }
1404 }
1405
Jing Linbace50b2011-10-18 22:55:47 -07001406 if (data->pdata->i2c_pull_up) {
1407 rc = regulator_set_optimum_mode(data->vcc_i2c,
1408 MXT_I2C_LPM_LOAD_UA);
1409 if (rc < 0) {
1410 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001411 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001412 goto fail_regulator_lpm;
1413 }
1414 }
1415
1416 return 0;
1417
1418regulator_hpm:
1419
Amy Maloche21115eb2011-11-02 09:04:37 -07001420 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001421 if (rc < 0) {
1422 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001423 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001424 goto fail_regulator_hpm;
1425 }
1426
Amy Maloche21115eb2011-11-02 09:04:37 -07001427 if (data->pdata->digital_pwr_regulator) {
1428 rc = regulator_set_optimum_mode(data->vcc_dig,
1429 MXT_ACTIVE_LOAD_DIG_UA);
1430 if (rc < 0) {
1431 dev_err(&data->client->dev,
1432 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1433 goto fail_regulator_hpm;
1434 }
1435 }
1436
Jing Linbace50b2011-10-18 22:55:47 -07001437 if (data->pdata->i2c_pull_up) {
1438 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1439 if (rc < 0) {
1440 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001441 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001442 goto fail_regulator_hpm;
1443 }
1444 }
1445
1446 return 0;
1447
1448fail_regulator_lpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001449 regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
1450 if (data->pdata->digital_pwr_regulator)
1451 regulator_set_optimum_mode(data->vcc_dig,
1452 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001453 if (data->pdata->i2c_pull_up)
1454 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1455
1456 return rc;
1457
1458fail_regulator_hpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001459 regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
1460 if (data->pdata->digital_pwr_regulator)
1461 regulator_set_optimum_mode(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001462 if (data->pdata->i2c_pull_up)
1463 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1464
1465 return rc;
1466}
1467
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301468static int mxt_suspend(struct device *dev)
1469{
1470 struct i2c_client *client = to_i2c_client(dev);
1471 struct mxt_data *data = i2c_get_clientdata(client);
1472 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001473 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301474
1475 mutex_lock(&input_dev->mutex);
1476
Amy Maloche52262212011-09-15 16:46:57 -07001477 if (input_dev->users) {
1478 error = mxt_stop(data);
1479 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001480 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001481 mutex_unlock(&input_dev->mutex);
1482 return error;
1483 }
1484
1485 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301486
1487 mutex_unlock(&input_dev->mutex);
1488
Jing Linbace50b2011-10-18 22:55:47 -07001489 /* put regulators in low power mode */
1490 error = mxt_regulator_lpm(data, true);
1491 if (error < 0) {
1492 dev_err(dev, "failed to enter low power mode\n");
1493 return error;
1494 }
1495
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301496 return 0;
1497}
1498
1499static int mxt_resume(struct device *dev)
1500{
1501 struct i2c_client *client = to_i2c_client(dev);
1502 struct mxt_data *data = i2c_get_clientdata(client);
1503 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001504 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301505
Jing Linbace50b2011-10-18 22:55:47 -07001506 /* put regulators in high power mode */
1507 error = mxt_regulator_lpm(data, false);
1508 if (error < 0) {
1509 dev_err(dev, "failed to enter high power mode\n");
1510 return error;
1511 }
1512
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301513 mutex_lock(&input_dev->mutex);
1514
Amy Maloche52262212011-09-15 16:46:57 -07001515 if (input_dev->users) {
1516 error = mxt_start(data);
1517 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001518 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001519 mutex_unlock(&input_dev->mutex);
1520 return error;
1521 }
1522 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301523
1524 mutex_unlock(&input_dev->mutex);
1525
1526 return 0;
1527}
1528
1529#if defined(CONFIG_HAS_EARLYSUSPEND)
1530static void mxt_early_suspend(struct early_suspend *h)
1531{
1532 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1533
1534 mxt_suspend(&data->client->dev);
1535}
1536
1537static void mxt_late_resume(struct early_suspend *h)
1538{
1539 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1540
1541 mxt_resume(&data->client->dev);
1542}
1543#endif
1544
1545static const struct dev_pm_ops mxt_pm_ops = {
1546#ifndef CONFIG_HAS_EARLYSUSPEND
1547 .suspend = mxt_suspend,
1548 .resume = mxt_resume,
1549#endif
1550};
1551#endif
1552
Iiro Valkonen7686b102011-02-02 23:21:58 -08001553static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001554 const struct i2c_device_id *id)
1555{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001556 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001557 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001558 struct input_dev *input_dev;
1559 int error;
1560
Iiro Valkonen919ed892011-02-15 13:36:52 -08001561 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001562 return -EINVAL;
1563
Iiro Valkonen7686b102011-02-02 23:21:58 -08001564 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001565 input_dev = input_allocate_device();
1566 if (!data || !input_dev) {
1567 dev_err(&client->dev, "Failed to allocate memory\n");
1568 error = -ENOMEM;
1569 goto err_free_mem;
1570 }
1571
Iiro Valkonen7686b102011-02-02 23:21:58 -08001572 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001573 input_dev->id.bustype = BUS_I2C;
1574 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001575 input_dev->open = mxt_input_open;
1576 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001577
Joonyoung Shim910d8052011-04-12 23:14:38 -07001578 data->client = client;
1579 data->input_dev = input_dev;
1580 data->pdata = pdata;
1581 data->irq = client->irq;
1582
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001583 __set_bit(EV_ABS, input_dev->evbit);
1584 __set_bit(EV_KEY, input_dev->evbit);
1585 __set_bit(BTN_TOUCH, input_dev->keybit);
1586
1587 /* For single touch */
1588 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001589 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001590 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001591 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001592 input_set_abs_params(input_dev, ABS_PRESSURE,
1593 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001594
1595 /* For multi touch */
1596 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001597 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001598 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001599 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001600 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001601 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001602 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1603 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001604
1605 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001606 i2c_set_clientdata(client, data);
1607
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301608 if (pdata->init_hw)
1609 error = pdata->init_hw(true);
1610 else
1611 error = mxt_regulator_configure(data, true);
1612 if (error) {
1613 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001614 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301615 }
1616
1617 if (pdata->power_on)
1618 error = pdata->power_on(true);
1619 else
1620 error = mxt_power_on(data, true);
1621 if (error) {
1622 dev_err(&client->dev, "Failed to power on hardware\n");
1623 goto err_regulator_on;
1624 }
1625
Amy Maloche08266db2011-11-04 11:07:16 -07001626 if (gpio_is_valid(pdata->irq_gpio)) {
1627 /* configure touchscreen irq gpio */
1628 error = gpio_request(pdata->irq_gpio,
1629 "mxt_irq_gpio");
1630 if (error) {
1631 pr_err("%s: unable to request gpio [%d]\n", __func__,
1632 pdata->irq_gpio);
1633 goto err_power_on;
1634 }
1635 error = gpio_direction_input(pdata->irq_gpio);
1636 if (error) {
1637 pr_err("%s: unable to set_direction for gpio [%d]\n",
1638 __func__, pdata->irq_gpio);
1639 goto err_irq_gpio_req;
1640 }
1641 }
1642
1643 if (gpio_is_valid(pdata->reset_gpio)) {
1644 /* configure touchscreen reset out gpio */
1645 error = gpio_request(pdata->reset_gpio,
1646 "mxt_reset_gpio");
1647 if (error) {
1648 pr_err("%s: unable to request reset gpio %d\n",
1649 __func__, pdata->reset_gpio);
1650 goto err_irq_gpio_req;
1651 }
1652
1653 error = gpio_direction_output(
1654 pdata->reset_gpio, 1);
1655 if (error) {
1656 pr_err("%s: unable to set direction for gpio %d\n",
1657 __func__, pdata->reset_gpio);
1658 goto err_reset_gpio_req;
1659 }
1660 }
1661
1662 mxt_reset_delay(data);
1663
Iiro Valkonen7686b102011-02-02 23:21:58 -08001664 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001665 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07001666 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001667
Iiro Valkonen7686b102011-02-02 23:21:58 -08001668 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001669 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001670 if (error) {
1671 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001672 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001673 }
1674
Iiro Valkonen08960a02011-04-12 23:16:40 -07001675 error = mxt_make_highchg(data);
1676 if (error)
1677 goto err_free_irq;
1678
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001679 error = input_register_device(input_dev);
1680 if (error)
1681 goto err_free_irq;
1682
Iiro Valkonen7686b102011-02-02 23:21:58 -08001683 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001684 if (error)
1685 goto err_unregister_device;
1686
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301687#if defined(CONFIG_HAS_EARLYSUSPEND)
1688 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1689 MXT_SUSPEND_LEVEL;
1690 data->early_suspend.suspend = mxt_early_suspend;
1691 data->early_suspend.resume = mxt_late_resume;
1692 register_early_suspend(&data->early_suspend);
1693#endif
1694
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001695 return 0;
1696
1697err_unregister_device:
1698 input_unregister_device(input_dev);
1699 input_dev = NULL;
1700err_free_irq:
1701 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001702err_free_object:
1703 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07001704err_reset_gpio_req:
1705 if (gpio_is_valid(pdata->reset_gpio))
1706 gpio_free(pdata->reset_gpio);
1707err_irq_gpio_req:
1708 if (gpio_is_valid(pdata->irq_gpio))
1709 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301710err_power_on:
1711 if (pdata->power_on)
1712 pdata->power_on(false);
1713 else
1714 mxt_power_on(data, false);
1715err_regulator_on:
1716 if (pdata->init_hw)
1717 pdata->init_hw(false);
1718 else
1719 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001720err_free_mem:
1721 input_free_device(input_dev);
1722 kfree(data);
1723 return error;
1724}
1725
Iiro Valkonen7686b102011-02-02 23:21:58 -08001726static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001727{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001728 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001729
Iiro Valkonen7686b102011-02-02 23:21:58 -08001730 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001731 free_irq(data->irq, data);
1732 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301733#if defined(CONFIG_HAS_EARLYSUSPEND)
1734 unregister_early_suspend(&data->early_suspend);
1735#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301736
1737 if (data->pdata->power_on)
1738 data->pdata->power_on(false);
1739 else
1740 mxt_power_on(data, false);
1741
1742 if (data->pdata->init_hw)
1743 data->pdata->init_hw(false);
1744 else
1745 mxt_regulator_configure(data, false);
1746
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001747 kfree(data->object_table);
1748 kfree(data);
1749
1750 return 0;
1751}
1752
Iiro Valkonen7686b102011-02-02 23:21:58 -08001753static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001754 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001755 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001756 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001757 { }
1758};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001759MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001760
Iiro Valkonen7686b102011-02-02 23:21:58 -08001761static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001762 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001763 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001764 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001765#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001766 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001767#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001768 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001769 .probe = mxt_probe,
1770 .remove = __devexit_p(mxt_remove),
1771 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001772};
1773
Iiro Valkonen7686b102011-02-02 23:21:58 -08001774static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001775{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001776 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001777}
1778
Iiro Valkonen7686b102011-02-02 23:21:58 -08001779static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001780{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001781 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001782}
1783
Iiro Valkonen7686b102011-02-02 23:21:58 -08001784module_init(mxt_init);
1785module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001786
1787/* Module information */
1788MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001789MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001790MODULE_LICENSE("GPL");