blob: f18c2d052c323b4bcf1d74ed0e11c40b2baaa9e8 [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>
Mohan Pallaka5e7343f2012-01-02 18:30:16 +08006 * Copyright (c) 2011-2012, 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>
Joonyoung Shimcd473222012-02-14 18:32:48 -080021#include <linux/input/mt.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>
Jing Lin6cfc00e2011-11-02 15:15:30 -070025#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053027#include <linux/regulator/consumer.h>
Jing Lincc974cb2012-02-01 23:13:14 -080028#include <linux/string.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070029
Anirudh Ghayal253ce122011-08-09 19:32:57 +053030#if defined(CONFIG_HAS_EARLYSUSPEND)
31#include <linux/earlysuspend.h>
32/* Early-suspend level */
33#define MXT_SUSPEND_LEVEL 1
34#endif
35
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070036/* Family ID */
37#define MXT224_ID 0x80
Amy Maloche380cc0b2011-11-03 12:55:04 -070038#define MXT224E_ID 0x81
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070039#define MXT1386_ID 0xA0
40
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070041/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080042#define MXT_VER_20 20
43#define MXT_VER_21 21
44#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070045
Nick Dyer0a4016c2012-01-18 15:17:59 +053046/* I2C slave address pairs */
47struct mxt_address_pair {
48 int bootloader;
49 int application;
50};
51
52static const struct mxt_address_pair mxt_slave_addresses[] = {
53 { 0x24, 0x4a },
54 { 0x25, 0x4b },
55 { 0x25, 0x4b },
56 { 0x26, 0x4c },
57 { 0x27, 0x4d },
58 { 0x34, 0x5a },
59 { 0x35, 0x5b },
60 { 0 },
61};
62
63enum mxt_device_state { INIT, APPMODE, BOOTLOADER };
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070064
65/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080066#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070067
Jing Lincc974cb2012-02-01 23:13:14 -080068/* Firmware frame size including frame data and CRC */
69#define MXT_SINGLE_FW_MAX_FRAME_SIZE 278
70#define MXT_CHIPSET_FW_MAX_FRAME_SIZE 534
71
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070072/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080073#define MXT_FAMILY_ID 0x00
74#define MXT_VARIANT_ID 0x01
75#define MXT_VERSION 0x02
76#define MXT_BUILD 0x03
77#define MXT_MATRIX_X_SIZE 0x04
78#define MXT_MATRIX_Y_SIZE 0x05
79#define MXT_OBJECT_NUM 0x06
80#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070081
Iiro Valkonen7686b102011-02-02 23:21:58 -080082#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070083
84/* Object types */
Iiro Valkonene8645592011-11-18 12:56:19 -080085#define MXT_DEBUG_DIAGNOSTIC_T37 37
86#define MXT_GEN_MESSAGE_T5 5
87#define MXT_GEN_COMMAND_T6 6
88#define MXT_GEN_POWER_T7 7
89#define MXT_GEN_ACQUIRE_T8 8
90#define MXT_GEN_DATASOURCE_T53 53
91#define MXT_TOUCH_MULTI_T9 9
92#define MXT_TOUCH_KEYARRAY_T15 15
93#define MXT_TOUCH_PROXIMITY_T23 23
94#define MXT_TOUCH_PROXKEY_T52 52
95#define MXT_PROCI_GRIPFACE_T20 20
96#define MXT_PROCG_NOISE_T22 22
97#define MXT_PROCI_ONETOUCH_T24 24
98#define MXT_PROCI_TWOTOUCH_T27 27
99#define MXT_PROCI_GRIP_T40 40
100#define MXT_PROCI_PALM_T41 41
101#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
102#define MXT_PROCI_STYLUS_T47 47
Jing Linc7fc4052011-12-21 16:16:19 -0800103#define MXT_PROCI_SHIELDLESS_T56 56
Iiro Valkonene8645592011-11-18 12:56:19 -0800104#define MXT_PROCG_NOISESUPPRESSION_T48 48
105#define MXT_SPT_COMMSCONFIG_T18 18
106#define MXT_SPT_GPIOPWM_T19 19
107#define MXT_SPT_SELFTEST_T25 25
108#define MXT_SPT_CTECONFIG_T28 28
109#define MXT_SPT_USERDATA_T38 38
110#define MXT_SPT_DIGITIZER_T43 43
111#define MXT_SPT_MESSAGECOUNT_T44 44
112#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700113
Iiro Valkonene8645592011-11-18 12:56:19 -0800114/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800115#define MXT_COMMAND_RESET 0
116#define MXT_COMMAND_BACKUPNV 1
117#define MXT_COMMAND_CALIBRATE 2
118#define MXT_COMMAND_REPORTALL 3
119#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700120
Iiro Valkonene8645592011-11-18 12:56:19 -0800121/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800122#define MXT_POWER_IDLEACQINT 0
123#define MXT_POWER_ACTVACQINT 1
124#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700125
Iiro Valkonene8645592011-11-18 12:56:19 -0800126/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800127#define MXT_ACQUIRE_CHRGTIME 0
128#define MXT_ACQUIRE_TCHDRIFT 2
129#define MXT_ACQUIRE_DRIFTST 3
130#define MXT_ACQUIRE_TCHAUTOCAL 4
131#define MXT_ACQUIRE_SYNC 5
132#define MXT_ACQUIRE_ATCHCALST 6
133#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700134
Iiro Valkonene8645592011-11-18 12:56:19 -0800135/* MXT_TOUCH_MULT_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800136#define MXT_TOUCH_CTRL 0
137#define MXT_TOUCH_XORIGIN 1
138#define MXT_TOUCH_YORIGIN 2
139#define MXT_TOUCH_XSIZE 3
140#define MXT_TOUCH_YSIZE 4
141#define MXT_TOUCH_BLEN 6
142#define MXT_TOUCH_TCHTHR 7
143#define MXT_TOUCH_TCHDI 8
144#define MXT_TOUCH_ORIENT 9
145#define MXT_TOUCH_MOVHYSTI 11
146#define MXT_TOUCH_MOVHYSTN 12
147#define MXT_TOUCH_NUMTOUCH 14
148#define MXT_TOUCH_MRGHYST 15
149#define MXT_TOUCH_MRGTHR 16
150#define MXT_TOUCH_AMPHYST 17
151#define MXT_TOUCH_XRANGE_LSB 18
152#define MXT_TOUCH_XRANGE_MSB 19
153#define MXT_TOUCH_YRANGE_LSB 20
154#define MXT_TOUCH_YRANGE_MSB 21
155#define MXT_TOUCH_XLOCLIP 22
156#define MXT_TOUCH_XHICLIP 23
157#define MXT_TOUCH_YLOCLIP 24
158#define MXT_TOUCH_YHICLIP 25
159#define MXT_TOUCH_XEDGECTRL 26
160#define MXT_TOUCH_XEDGEDIST 27
161#define MXT_TOUCH_YEDGECTRL 28
162#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700163#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonene8645592011-11-18 12:56:19 -0800165/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166#define MXT_GRIPFACE_CTRL 0
167#define MXT_GRIPFACE_XLOGRIP 1
168#define MXT_GRIPFACE_XHIGRIP 2
169#define MXT_GRIPFACE_YLOGRIP 3
170#define MXT_GRIPFACE_YHIGRIP 4
171#define MXT_GRIPFACE_MAXTCHS 5
172#define MXT_GRIPFACE_SZTHR1 7
173#define MXT_GRIPFACE_SZTHR2 8
174#define MXT_GRIPFACE_SHPTHR1 9
175#define MXT_GRIPFACE_SHPTHR2 10
176#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700177
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178/* MXT_PROCI_NOISE field */
179#define MXT_NOISE_CTRL 0
180#define MXT_NOISE_OUTFLEN 1
181#define MXT_NOISE_GCAFUL_LSB 3
182#define MXT_NOISE_GCAFUL_MSB 4
183#define MXT_NOISE_GCAFLL_LSB 5
184#define MXT_NOISE_GCAFLL_MSB 6
185#define MXT_NOISE_ACTVGCAFVALID 7
186#define MXT_NOISE_NOISETHR 8
187#define MXT_NOISE_FREQHOPSCALE 10
188#define MXT_NOISE_FREQ0 11
189#define MXT_NOISE_FREQ1 12
190#define MXT_NOISE_FREQ2 13
191#define MXT_NOISE_FREQ3 14
192#define MXT_NOISE_FREQ4 15
193#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700194
Iiro Valkonene8645592011-11-18 12:56:19 -0800195/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800196#define MXT_COMMS_CTRL 0
197#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700198
Iiro Valkonene8645592011-11-18 12:56:19 -0800199/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_CTE_CTRL 0
201#define MXT_CTE_CMD 1
202#define MXT_CTE_MODE 2
203#define MXT_CTE_IDLEGCAFDEPTH 3
204#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700205#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700206
Iiro Valkonen7686b102011-02-02 23:21:58 -0800207#define MXT_VOLTAGE_DEFAULT 2700000
208#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700209
Amy Maloche21115eb2011-11-02 09:04:37 -0700210/* Analog voltage @2.7 V */
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530211#define MXT_VTG_MIN_UV 2700000
212#define MXT_VTG_MAX_UV 3300000
213#define MXT_ACTIVE_LOAD_UA 15000
Jing Linbace50b2011-10-18 22:55:47 -0700214#define MXT_LPM_LOAD_UA 10
Amy Maloche21115eb2011-11-02 09:04:37 -0700215/* Digital voltage @1.8 V */
216#define MXT_VTG_DIG_MIN_UV 1800000
217#define MXT_VTG_DIG_MAX_UV 1800000
218#define MXT_ACTIVE_LOAD_DIG_UA 10000
219#define MXT_LPM_LOAD_DIG_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530220
221#define MXT_I2C_VTG_MIN_UV 1800000
222#define MXT_I2C_VTG_MAX_UV 1800000
223#define MXT_I2C_LOAD_UA 10000
Jing Linbace50b2011-10-18 22:55:47 -0700224#define MXT_I2C_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530225
Iiro Valkonene8645592011-11-18 12:56:19 -0800226/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800227#define MXT_BOOT_VALUE 0xa5
228#define MXT_BACKUP_VALUE 0x55
229#define MXT_BACKUP_TIME 25 /* msec */
Jing Linc7fc4052011-12-21 16:16:19 -0800230#define MXT224_RESET_TIME 65 /* msec */
231#define MXT224E_RESET_TIME 22 /* msec */
232#define MXT1386_RESET_TIME 250 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700233#define MXT_RESET_TIME 250 /* msec */
Jing Linc7fc4052011-12-21 16:16:19 -0800234#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235
Nick Dyer0a4016c2012-01-18 15:17:59 +0530236#define MXT_FWRESET_TIME 1000 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700237
Jing Lin36aee812011-10-17 17:17:28 -0700238#define MXT_WAKE_TIME 25
239
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700240/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800241#define MXT_UNLOCK_CMD_MSB 0xaa
242#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700243
244/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800245#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
246#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
247#define MXT_FRAME_CRC_CHECK 0x02
248#define MXT_FRAME_CRC_FAIL 0x03
249#define MXT_FRAME_CRC_PASS 0x04
250#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
251#define MXT_BOOT_STATUS_MASK 0x3f
Nick Dyer0a4016c2012-01-18 15:17:59 +0530252#define MXT_BOOT_EXTENDED_ID (1 << 5)
253#define MXT_BOOT_ID_MASK 0x1f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700254
255/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800256#define MXT_SUPPRESS (1 << 1)
257#define MXT_AMP (1 << 2)
258#define MXT_VECTOR (1 << 3)
259#define MXT_MOVE (1 << 4)
260#define MXT_RELEASE (1 << 5)
261#define MXT_PRESS (1 << 6)
262#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700263
Joonyoung Shim910d8052011-04-12 23:14:38 -0700264/* Touch orient bits */
265#define MXT_XY_SWITCH (1 << 0)
266#define MXT_X_INVERT (1 << 1)
267#define MXT_Y_INVERT (1 << 2)
268
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700269/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800270#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700271
Iiro Valkonen7686b102011-02-02 23:21:58 -0800272#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700273
Jing Lin36aee812011-10-17 17:17:28 -0700274#define T7_DATA_SIZE 3
275#define MXT_MAX_RW_TRIES 3
276#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530277
Jing Lin6cfc00e2011-11-02 15:15:30 -0700278#define MXT_DEBUGFS_DIR "atmel_mxt_ts"
279#define MXT_DEBUGFS_FILE "object"
280
Iiro Valkonen7686b102011-02-02 23:21:58 -0800281struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700282 u8 family_id;
283 u8 variant_id;
284 u8 version;
285 u8 build;
286 u8 matrix_xsize;
287 u8 matrix_ysize;
288 u8 object_num;
289};
290
Iiro Valkonen7686b102011-02-02 23:21:58 -0800291struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700292 u8 type;
293 u16 start_address;
294 u8 size;
295 u8 instances;
296 u8 num_report_ids;
297
298 /* to map object and message */
299 u8 max_reportid;
300};
301
Iiro Valkonen7686b102011-02-02 23:21:58 -0800302struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700303 u8 reportid;
304 u8 message[7];
305 u8 checksum;
306};
307
Iiro Valkonen7686b102011-02-02 23:21:58 -0800308struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700309 int status;
310 int x;
311 int y;
312 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700313 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700314};
315
316/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800317struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700318 struct i2c_client *client;
319 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800320 const struct mxt_platform_data *pdata;
Jing Lindc4413c2012-01-16 15:22:52 -0800321 const struct mxt_config_info *config_info;
Nick Dyer0a4016c2012-01-18 15:17:59 +0530322 enum mxt_device_state state;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800323 struct mxt_object *object_table;
324 struct mxt_info info;
325 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700326 unsigned int irq;
Amy Maloche83c385a2012-02-01 10:32:03 +0530327 unsigned int touch_x_size;
328 unsigned int touch_y_size;
Amy Maloche21115eb2011-11-02 09:04:37 -0700329 struct regulator *vcc_ana;
330 struct regulator *vcc_dig;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530331 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530332#if defined(CONFIG_HAS_EARLYSUSPEND)
333 struct early_suspend early_suspend;
334#endif
Jing Lin36aee812011-10-17 17:17:28 -0700335
Amy Maloche52262212011-09-15 16:46:57 -0700336 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700337 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700338 u8 t9_ctrl;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800339 u32 keyarray_old;
340 u32 keyarray_new;
341 u8 t9_max_reportid;
342 u8 t9_min_reportid;
343 u8 t15_max_reportid;
344 u8 t15_min_reportid;
Jing Lindc4413c2012-01-16 15:22:52 -0800345 u8 curr_cfg_version;
346 int cfg_version_idx;
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -0800347 const char *fw_name;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700348};
349
Jing Lin6cfc00e2011-11-02 15:15:30 -0700350static struct dentry *debug_base;
351
Iiro Valkonen7686b102011-02-02 23:21:58 -0800352static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700353{
354 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800355 case MXT_GEN_MESSAGE_T5:
356 case MXT_GEN_COMMAND_T6:
357 case MXT_GEN_POWER_T7:
358 case MXT_GEN_ACQUIRE_T8:
359 case MXT_GEN_DATASOURCE_T53:
360 case MXT_TOUCH_MULTI_T9:
361 case MXT_TOUCH_KEYARRAY_T15:
362 case MXT_TOUCH_PROXIMITY_T23:
363 case MXT_TOUCH_PROXKEY_T52:
364 case MXT_PROCI_GRIPFACE_T20:
365 case MXT_PROCG_NOISE_T22:
366 case MXT_PROCI_ONETOUCH_T24:
367 case MXT_PROCI_TWOTOUCH_T27:
368 case MXT_PROCI_GRIP_T40:
369 case MXT_PROCI_PALM_T41:
370 case MXT_PROCI_TOUCHSUPPRESSION_T42:
371 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800372 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800373 case MXT_PROCG_NOISESUPPRESSION_T48:
374 case MXT_SPT_COMMSCONFIG_T18:
375 case MXT_SPT_GPIOPWM_T19:
376 case MXT_SPT_SELFTEST_T25:
377 case MXT_SPT_CTECONFIG_T28:
378 case MXT_SPT_USERDATA_T38:
379 case MXT_SPT_DIGITIZER_T43:
380 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700381 return true;
382 default:
383 return false;
384 }
385}
386
Iiro Valkonen7686b102011-02-02 23:21:58 -0800387static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700388{
389 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800390 case MXT_GEN_COMMAND_T6:
391 case MXT_GEN_POWER_T7:
392 case MXT_GEN_ACQUIRE_T8:
393 case MXT_TOUCH_MULTI_T9:
394 case MXT_TOUCH_KEYARRAY_T15:
395 case MXT_TOUCH_PROXIMITY_T23:
396 case MXT_TOUCH_PROXKEY_T52:
397 case MXT_PROCI_GRIPFACE_T20:
398 case MXT_PROCG_NOISE_T22:
399 case MXT_PROCI_ONETOUCH_T24:
400 case MXT_PROCI_TWOTOUCH_T27:
401 case MXT_PROCI_GRIP_T40:
402 case MXT_PROCI_PALM_T41:
403 case MXT_PROCI_TOUCHSUPPRESSION_T42:
404 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800405 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800406 case MXT_PROCG_NOISESUPPRESSION_T48:
407 case MXT_SPT_COMMSCONFIG_T18:
408 case MXT_SPT_GPIOPWM_T19:
409 case MXT_SPT_SELFTEST_T25:
410 case MXT_SPT_CTECONFIG_T28:
411 case MXT_SPT_USERDATA_T38:
412 case MXT_SPT_DIGITIZER_T43:
413 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700414 return true;
415 default:
416 return false;
417 }
418}
419
Iiro Valkonen7686b102011-02-02 23:21:58 -0800420static void mxt_dump_message(struct device *dev,
421 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700422{
423 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
424 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
425 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
426 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
427 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
428 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
429 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
430 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
431 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
432}
433
Nick Dyer0a4016c2012-01-18 15:17:59 +0530434static int mxt_switch_to_bootloader_address(struct mxt_data *data)
435{
436 int i;
437 struct i2c_client *client = data->client;
438
439 if (data->state == BOOTLOADER) {
440 dev_err(&client->dev, "Already in BOOTLOADER state\n");
441 return -EINVAL;
442 }
443
444 for (i = 0; mxt_slave_addresses[i].application != 0; i++) {
445 if (mxt_slave_addresses[i].application == client->addr) {
446 dev_info(&client->dev, "Changing to bootloader address: "
447 "%02x -> %02x",
448 client->addr,
449 mxt_slave_addresses[i].bootloader);
450
451 client->addr = mxt_slave_addresses[i].bootloader;
452 data->state = BOOTLOADER;
453 return 0;
454 }
455 }
456
457 dev_err(&client->dev, "Address 0x%02x not found in address table",
458 client->addr);
459 return -EINVAL;
460}
461
462static int mxt_switch_to_appmode_address(struct mxt_data *data)
463{
464 int i;
465 struct i2c_client *client = data->client;
466
467 if (data->state == APPMODE) {
468 dev_err(&client->dev, "Already in APPMODE state\n");
469 return -EINVAL;
470 }
471
472 for (i = 0; mxt_slave_addresses[i].application != 0; i++) {
473 if (mxt_slave_addresses[i].bootloader == client->addr) {
474 dev_info(&client->dev,
475 "Changing to application mode address: "
476 "0x%02x -> 0x%02x",
477 client->addr,
478 mxt_slave_addresses[i].application);
479
480 client->addr = mxt_slave_addresses[i].application;
481 data->state = APPMODE;
482 return 0;
483 }
484 }
485
486 dev_err(&client->dev, "Address 0x%02x not found in address table",
487 client->addr);
488 return -EINVAL;
489}
490
491static int mxt_get_bootloader_version(struct i2c_client *client, u8 val)
492{
493 u8 buf[3];
494
495 if (val | MXT_BOOT_EXTENDED_ID) {
496 dev_dbg(&client->dev,
497 "Retrieving extended mode ID information");
498
499 if (i2c_master_recv(client, &buf[0], 3) != 3) {
500 dev_err(&client->dev, "%s: i2c recv failed\n",
501 __func__);
502 return -EIO;
503 }
504
505 dev_info(&client->dev, "Bootloader ID:%d Version:%d",
506 buf[1], buf[2]);
507
508 return buf[0];
509 } else {
510 dev_info(&client->dev, "Bootloader ID:%d",
511 val & MXT_BOOT_ID_MASK);
512
513 return val;
514 }
515}
516
Iiro Valkonen7686b102011-02-02 23:21:58 -0800517static int mxt_check_bootloader(struct i2c_client *client,
Nick Dyer0a4016c2012-01-18 15:17:59 +0530518 unsigned int state)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700519{
520 u8 val;
521
522recheck:
523 if (i2c_master_recv(client, &val, 1) != 1) {
524 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
525 return -EIO;
526 }
527
528 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800529 case MXT_WAITING_BOOTLOAD_CMD:
Nick Dyer0a4016c2012-01-18 15:17:59 +0530530 val = mxt_get_bootloader_version(client, val);
531 val &= ~MXT_BOOT_STATUS_MASK;
532 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800533 case MXT_WAITING_FRAME_DATA:
Nick Dyer0a4016c2012-01-18 15:17:59 +0530534 case MXT_APP_CRC_FAIL:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800535 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537 case MXT_FRAME_CRC_PASS:
538 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700539 goto recheck;
Nick Dyer0a4016c2012-01-18 15:17:59 +0530540 if (val == MXT_FRAME_CRC_FAIL) {
541 dev_err(&client->dev, "Bootloader CRC fail\n");
542 return -EINVAL;
543 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544 break;
545 default:
546 return -EINVAL;
547 }
548
549 if (val != state) {
Nick Dyer0a4016c2012-01-18 15:17:59 +0530550 dev_err(&client->dev, "Invalid bootloader mode state %X\n",
551 val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700552 return -EINVAL;
553 }
554
555 return 0;
556}
557
Iiro Valkonen7686b102011-02-02 23:21:58 -0800558static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700559{
560 u8 buf[2];
561
Iiro Valkonen7686b102011-02-02 23:21:58 -0800562 buf[0] = MXT_UNLOCK_CMD_LSB;
563 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564
565 if (i2c_master_send(client, buf, 2) != 2) {
566 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
567 return -EIO;
568 }
569
570 return 0;
571}
572
Iiro Valkonen7686b102011-02-02 23:21:58 -0800573static int mxt_fw_write(struct i2c_client *client,
Nick Dyer0a4016c2012-01-18 15:17:59 +0530574 const u8 *data, unsigned int frame_size)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700575{
576 if (i2c_master_send(client, data, frame_size) != frame_size) {
577 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
578 return -EIO;
579 }
580
581 return 0;
582}
583
Iiro Valkonen7686b102011-02-02 23:21:58 -0800584static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700585 u16 reg, u16 len, void *val)
586{
587 struct i2c_msg xfer[2];
588 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700589 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590
591 buf[0] = reg & 0xff;
592 buf[1] = (reg >> 8) & 0xff;
593
594 /* Write register */
595 xfer[0].addr = client->addr;
596 xfer[0].flags = 0;
597 xfer[0].len = 2;
598 xfer[0].buf = buf;
599
600 /* Read data */
601 xfer[1].addr = client->addr;
602 xfer[1].flags = I2C_M_RD;
603 xfer[1].len = len;
604 xfer[1].buf = val;
605
Jing Lin36aee812011-10-17 17:17:28 -0700606 do {
607 if (i2c_transfer(client->adapter, xfer, 2) == 2)
608 return 0;
609 msleep(MXT_WAKE_TIME);
610 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611
Jing Lin36aee812011-10-17 17:17:28 -0700612 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
613 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700614}
615
Iiro Valkonen7686b102011-02-02 23:21:58 -0800616static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700617{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800618 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700619}
620
Jing Lin36aee812011-10-17 17:17:28 -0700621static int __mxt_write_reg(struct i2c_client *client,
622 u16 addr, u16 length, u8 *value)
623{
624 u8 buf[MXT_BLOCK_SIZE + 2];
625 int i, tries = 0;
626
627 if (length > MXT_BLOCK_SIZE)
628 return -EINVAL;
629
630 buf[0] = addr & 0xff;
631 buf[1] = (addr >> 8) & 0xff;
632 for (i = 0; i < length; i++)
633 buf[i + 2] = *value++;
634
635 do {
636 if (i2c_master_send(client, buf, length + 2) == (length + 2))
637 return 0;
638 msleep(MXT_WAKE_TIME);
639 } while (++tries < MXT_MAX_RW_TRIES);
640
641 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
642 return -EIO;
643}
644
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646{
Jing Lin36aee812011-10-17 17:17:28 -0700647 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700648}
649
Iiro Valkonen7686b102011-02-02 23:21:58 -0800650static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700651 u16 reg, u8 *object_buf)
652{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800653 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700654 object_buf);
655}
656
Iiro Valkonen7686b102011-02-02 23:21:58 -0800657static struct mxt_object *
658mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700659{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800660 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700661 int i;
662
663 for (i = 0; i < data->info.object_num; i++) {
664 object = data->object_table + i;
665 if (object->type == type)
666 return object;
667 }
668
669 dev_err(&data->client->dev, "Invalid object type\n");
670 return NULL;
671}
672
Iiro Valkonen7686b102011-02-02 23:21:58 -0800673static int mxt_read_message(struct mxt_data *data,
674 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800676 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677 u16 reg;
678
Iiro Valkonene8645592011-11-18 12:56:19 -0800679 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680 if (!object)
681 return -EINVAL;
682
683 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800684 return __mxt_read_reg(data->client, reg,
685 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686}
687
Iiro Valkonen7686b102011-02-02 23:21:58 -0800688static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689 u8 type, u8 offset, u8 *val)
690{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800691 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692 u16 reg;
693
Iiro Valkonen7686b102011-02-02 23:21:58 -0800694 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700695 if (!object)
696 return -EINVAL;
697
698 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800699 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700}
701
Iiro Valkonen7686b102011-02-02 23:21:58 -0800702static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700703 u8 type, u8 offset, u8 val)
704{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800705 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700706 u16 reg;
707
Iiro Valkonen7686b102011-02-02 23:21:58 -0800708 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700709 if (!object)
710 return -EINVAL;
711
712 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800713 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714}
715
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800718 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719 struct input_dev *input_dev = data->input_dev;
720 int status = finger[single_id].status;
721 int finger_num = 0;
722 int id;
723
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725 if (!finger[id].status)
726 continue;
727
Joonyoung Shimcd473222012-02-14 18:32:48 -0800728 input_mt_slot(input_dev, id);
729 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
730 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731
Joonyoung Shimcd473222012-02-14 18:32:48 -0800732 if (finger[id].status != MXT_RELEASE) {
Amy Maloche2b59bab2011-10-13 16:08:16 -0700733 finger_num++;
Joonyoung Shimcd473222012-02-14 18:32:48 -0800734 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
735 finger[id].area);
736 input_report_abs(input_dev, ABS_MT_POSITION_X,
737 finger[id].x);
738 input_report_abs(input_dev, ABS_MT_POSITION_Y,
739 finger[id].y);
740 input_report_abs(input_dev, ABS_MT_PRESSURE,
741 finger[id].area);
742 } else {
743 finger[id].status = 0;
744 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700745 }
746
747 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
748
Iiro Valkonen7686b102011-02-02 23:21:58 -0800749 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700750 input_report_abs(input_dev, ABS_X, finger[single_id].x);
751 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700752 input_report_abs(input_dev,
753 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700754 }
755
756 input_sync(input_dev);
757}
758
Iiro Valkonen7686b102011-02-02 23:21:58 -0800759static void mxt_input_touchevent(struct mxt_data *data,
760 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800762 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700763 struct device *dev = &data->client->dev;
764 u8 status = message->message[0];
765 int x;
766 int y;
767 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700768 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700769
770 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800771 if (!(status & MXT_DETECT)) {
772 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700773 dev_dbg(dev, "[%d] released\n", id);
774
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 finger[id].status = MXT_RELEASE;
776 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700777 }
778 return;
779 }
780
781 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800782 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783 return;
784
Joonyoung Shim910d8052011-04-12 23:14:38 -0700785 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
786 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Amy Maloche83c385a2012-02-01 10:32:03 +0530787 if (data->touch_x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700788 x = x >> 2;
Amy Maloche83c385a2012-02-01 10:32:03 +0530789 if (data->touch_y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700790 y = y >> 2;
791
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700792 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700793 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794
795 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800796 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700797 x, y, area);
798
Iiro Valkonen7686b102011-02-02 23:21:58 -0800799 finger[id].status = status & MXT_MOVE ?
800 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801 finger[id].x = x;
802 finger[id].y = y;
803 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700804 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700805
Iiro Valkonen7686b102011-02-02 23:21:58 -0800806 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700807}
808
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800809static void mxt_handle_key_array(struct mxt_data *data,
810 struct mxt_message *message)
811{
812 u32 keys_changed;
813 int i;
814
815 if (!data->pdata->key_codes) {
816 dev_err(&data->client->dev, "keyarray is not supported\n");
817 return;
818 }
819
820 data->keyarray_new = message->message[1] |
821 (message->message[2] << 8) |
822 (message->message[3] << 16) |
823 (message->message[4] << 24);
824
825 keys_changed = data->keyarray_old ^ data->keyarray_new;
826
827 if (!keys_changed) {
828 dev_dbg(&data->client->dev, "no keys changed\n");
829 return;
830 }
831
832 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
833 if (!(keys_changed & (1 << i)))
834 continue;
835
836 input_report_key(data->input_dev, data->pdata->key_codes[i],
837 (data->keyarray_new & (1 << i)));
838 input_sync(data->input_dev);
839 }
840
841 data->keyarray_old = data->keyarray_new;
842}
843
Iiro Valkonen7686b102011-02-02 23:21:58 -0800844static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800846 struct mxt_data *data = dev_id;
847 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700848 struct device *dev = &data->client->dev;
849 int id;
850 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700851
Nick Dyer0a4016c2012-01-18 15:17:59 +0530852 if (data->state != APPMODE) {
853 dev_err(dev, "Ignoring IRQ - not in APPMODE state\n");
854 return IRQ_HANDLED;
855 }
856
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700857 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700859 dev_err(dev, "Failed to read message\n");
860 goto end;
861 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700862 reportid = message.reportid;
863
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800864 if (!reportid) {
865 dev_dbg(dev, "Report id 0 is reserved\n");
866 continue;
867 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700868
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800869 /* check whether report id is part of T9 or T15 */
870 id = reportid - data->t9_min_reportid;
871
872 if (reportid >= data->t9_min_reportid &&
873 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800874 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800875 else if (reportid >= data->t15_min_reportid &&
876 reportid <= data->t15_max_reportid)
877 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700878 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800879 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700880 } while (reportid != 0xff);
881
882end:
883 return IRQ_HANDLED;
884}
885
Iiro Valkonen7686b102011-02-02 23:21:58 -0800886static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700887{
Jing Lindc4413c2012-01-16 15:22:52 -0800888 const struct mxt_config_info *config_info = data->config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800889 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700890 struct device *dev = &data->client->dev;
891 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800892 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893
Jing Lindc4413c2012-01-16 15:22:52 -0800894 if (!config_info) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800895 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
896 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700897 }
898
899 for (i = 0; i < data->info.object_num; i++) {
900 object = data->object_table + i;
901
Iiro Valkonen7686b102011-02-02 23:21:58 -0800902 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700903 continue;
904
Iiro Valkonen71749f52011-02-15 13:36:52 -0800905 for (j = 0; j < object->size + 1; j++) {
906 config_offset = index + j;
Jing Lindc4413c2012-01-16 15:22:52 -0800907 if (config_offset > config_info->config_length) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800908 dev_err(dev, "Not enough config data!\n");
909 return -EINVAL;
910 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800911 mxt_write_object(data, object->type, j,
Jing Lindc4413c2012-01-16 15:22:52 -0800912 config_info->config[config_offset]);
Iiro Valkonen71749f52011-02-15 13:36:52 -0800913 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700914 index += object->size + 1;
915 }
916
917 return 0;
918}
919
Iiro Valkonen7686b102011-02-02 23:21:58 -0800920static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700921{
922 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800923 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700924 int count = 10;
925 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700926
927 /* Read dummy message to make high CHG pin */
928 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800929 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700930 if (error)
931 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800932 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700933
934 if (!count) {
935 dev_err(dev, "CHG pin isn't cleared\n");
936 return -EBUSY;
937 }
938
939 return 0;
940}
941
Iiro Valkonen7686b102011-02-02 23:21:58 -0800942static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700943{
944 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800945 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946 int error;
947 u8 val;
948
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950 if (error)
951 return error;
952 info->family_id = val;
953
Iiro Valkonen7686b102011-02-02 23:21:58 -0800954 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955 if (error)
956 return error;
957 info->variant_id = val;
958
Iiro Valkonen7686b102011-02-02 23:21:58 -0800959 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700960 if (error)
961 return error;
962 info->version = val;
963
Iiro Valkonen7686b102011-02-02 23:21:58 -0800964 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700965 if (error)
966 return error;
967 info->build = val;
968
Iiro Valkonen7686b102011-02-02 23:21:58 -0800969 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700970 if (error)
971 return error;
972 info->object_num = val;
973
974 return 0;
975}
976
Iiro Valkonen7686b102011-02-02 23:21:58 -0800977static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700978{
979 int error;
980 int i;
981 u16 reg;
982 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800983 u8 buf[MXT_OBJECT_SIZE];
Jing Lindc4413c2012-01-16 15:22:52 -0800984 bool found_t38 = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700985
986 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800987 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700988
Iiro Valkonen7686b102011-02-02 23:21:58 -0800989 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
990 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991 if (error)
992 return error;
993
994 object->type = buf[0];
995 object->start_address = (buf[2] << 8) | buf[1];
996 object->size = buf[3];
997 object->instances = buf[4];
998 object->num_report_ids = buf[5];
999
1000 if (object->num_report_ids) {
1001 reportid += object->num_report_ids *
1002 (object->instances + 1);
1003 object->max_reportid = reportid;
1004 }
Jing Lindc4413c2012-01-16 15:22:52 -08001005
1006 /* Calculate index for config major version in config array.
1007 * Major version is the first byte in object T38.
1008 */
1009 if (object->type == MXT_SPT_USERDATA_T38)
1010 found_t38 = true;
1011 if (!found_t38 && mxt_object_writable(object->type))
1012 data->cfg_version_idx += object->size + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001013 }
1014
1015 return 0;
1016}
1017
Jing Lindc4413c2012-01-16 15:22:52 -08001018static int mxt_search_config_array(struct mxt_data *data, bool version_match)
1019{
1020
1021 const struct mxt_platform_data *pdata = data->pdata;
1022 const struct mxt_config_info *cfg_info;
1023 struct mxt_info *info = &data->info;
1024 int i;
1025 u8 cfg_version;
1026
1027 for (i = 0; i < pdata->config_array_size; i++) {
1028
1029 cfg_info = &pdata->config_array[i];
1030
1031 if (!cfg_info->config || !cfg_info->config_length)
1032 continue;
1033
1034 if (info->family_id == cfg_info->family_id &&
1035 info->variant_id == cfg_info->variant_id &&
1036 info->version == cfg_info->version &&
1037 info->build == cfg_info->build) {
1038
1039 cfg_version = cfg_info->config[data->cfg_version_idx];
1040 if (data->curr_cfg_version == cfg_version ||
1041 !version_match) {
1042 data->config_info = cfg_info;
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001043 data->fw_name = pdata->config_array[i].fw_name;
Jing Lindc4413c2012-01-16 15:22:52 -08001044 return 0;
1045 }
1046 }
1047 }
1048
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001049 data->fw_name = NULL;
Jing Lindc4413c2012-01-16 15:22:52 -08001050 dev_info(&data->client->dev,
1051 "Config not found: F: %d, V: %d, FW: %d.%d.%d, CFG: %d\n",
1052 info->family_id, info->variant_id,
1053 info->version >> 4, info->version & 0xF, info->build,
1054 data->curr_cfg_version);
1055 return -EINVAL;
1056}
1057
1058static int mxt_get_config(struct mxt_data *data)
1059{
1060 const struct mxt_platform_data *pdata = data->pdata;
1061 struct device *dev = &data->client->dev;
1062 struct mxt_object *object;
1063 int error;
1064
1065 if (!pdata->config_array || !pdata->config_array_size) {
1066 dev_dbg(dev, "No cfg data provided by platform data\n");
1067 return 0;
1068 }
1069
1070 /* Get current config version */
1071 object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
1072 if (!object) {
1073 dev_err(dev, "Unable to obtain USERDATA object\n");
1074 return -EINVAL;
1075 }
1076
1077 error = mxt_read_reg(data->client, object->start_address,
1078 &data->curr_cfg_version);
1079 if (error) {
1080 dev_err(dev, "Unable to read config version\n");
1081 return error;
1082 }
1083
1084 /* It is possible that the config data on the controller is not
1085 * versioned and the version number returns 0. In this case,
1086 * find a match without the config version checking.
1087 */
1088 error = mxt_search_config_array(data,
1089 data->curr_cfg_version != 0 ? true : false);
1090 if (error)
1091 return error;
1092
1093 return 0;
1094}
Amy Maloche7e447432011-09-14 11:36:30 -07001095static void mxt_reset_delay(struct mxt_data *data)
1096{
1097 struct mxt_info *info = &data->info;
1098
1099 switch (info->family_id) {
1100 case MXT224_ID:
1101 msleep(MXT224_RESET_TIME);
1102 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -07001103 case MXT224E_ID:
1104 msleep(MXT224E_RESET_TIME);
1105 break;
Amy Maloche7e447432011-09-14 11:36:30 -07001106 case MXT1386_ID:
1107 msleep(MXT1386_RESET_TIME);
1108 break;
1109 default:
1110 msleep(MXT_RESET_TIME);
1111 }
1112}
1113
Iiro Valkonen7686b102011-02-02 23:21:58 -08001114static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001115{
1116 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001117 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -07001118 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001119 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001120 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001121 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -07001122 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001123 struct mxt_object *t9_object;
1124 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001125
Iiro Valkonen7686b102011-02-02 23:21:58 -08001126 error = mxt_get_info(data);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301127 if (error) {
1128 /* Try bootloader mode */
1129 error = mxt_switch_to_bootloader_address(data);
1130 if (error)
1131 return error;
1132
1133 error = mxt_check_bootloader(client, MXT_APP_CRC_FAIL);
1134 if (error)
1135 return error;
1136
1137 dev_err(&client->dev, "Application CRC failure\n");
1138 data->state = BOOTLOADER;
1139
1140 return 0;
1141 }
1142
1143 dev_info(&client->dev,
1144 "Family ID: %d Variant ID: %d Version: %d.%d "
1145 "Build: 0x%02X Object Num: %d\n",
1146 info->family_id, info->variant_id,
1147 info->version >> 4, info->version & 0xf,
1148 info->build, info->object_num);
1149
1150 data->state = APPMODE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001151
1152 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001153 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001154 GFP_KERNEL);
1155 if (!data->object_table) {
1156 dev_err(&client->dev, "Failed to allocate memory\n");
1157 return -ENOMEM;
1158 }
1159
1160 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001161 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001162 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001163 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001164
Jing Lindc4413c2012-01-16 15:22:52 -08001165 /* Get config data from platform data */
1166 error = mxt_get_config(data);
1167 if (error)
1168 dev_dbg(&client->dev, "Config info not found.\n");
1169
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001170 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001171 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001172 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001173 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001174
Amy Maloche52262212011-09-15 16:46:57 -07001175 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -08001176 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -07001177 if (!t7_object) {
1178 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -07001179 error = -EINVAL;
1180 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -07001181 }
1182
1183 data->t7_start_addr = t7_object->start_address;
1184 error = __mxt_read_reg(client, data->t7_start_addr,
1185 T7_DATA_SIZE, data->t7_data);
1186 if (error < 0) {
1187 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -07001188 "Failed to save current power state\n");
1189 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001190 }
Iiro Valkonene8645592011-11-18 12:56:19 -08001191 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -07001192 &data->t9_ctrl);
1193 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -07001194 dev_err(&client->dev, "Failed to save current touch object\n");
1195 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001196 }
1197
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001198 /* Store T9, T15's min and max report ids */
1199 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1200 if (!t9_object) {
1201 dev_err(&client->dev, "Failed to get T9 object\n");
1202 error = -EINVAL;
1203 goto free_object_table;
1204 }
1205 data->t9_max_reportid = t9_object->max_reportid;
1206 data->t9_min_reportid = t9_object->max_reportid -
1207 t9_object->num_report_ids + 1;
1208
1209 if (data->pdata->key_codes) {
1210 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
1211 if (!t15_object)
1212 dev_dbg(&client->dev, "T15 object is not available\n");
1213 else {
1214 data->t15_max_reportid = t15_object->max_reportid;
1215 data->t15_min_reportid = t15_object->max_reportid -
1216 t15_object->num_report_ids + 1;
1217 }
1218 }
1219
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001220 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -08001221 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001222 MXT_COMMAND_BACKUPNV,
1223 MXT_BACKUP_VALUE);
1224 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001225 do {
Iiro Valkonene8645592011-11-18 12:56:19 -08001226 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001227 MXT_COMMAND_BACKUPNV,
1228 &command_register);
1229 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001230 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -07001231 usleep_range(1000, 2000);
1232 } while ((command_register != 0) && (++timeout_counter <= 100));
1233 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001234 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -07001235 error = -EIO;
1236 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001237 }
1238
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001239
1240 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -08001241 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001242 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001243
Amy Maloche7e447432011-09-14 11:36:30 -07001244 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001245
1246 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001247 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001248 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001249 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001250 info->matrix_xsize = val;
1251
Iiro Valkonen7686b102011-02-02 23:21:58 -08001252 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001253 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001254 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001255 info->matrix_ysize = val;
1256
1257 dev_info(&client->dev,
Nick Dyer0a4016c2012-01-18 15:17:59 +05301258 "Matrix X Size: %d Matrix Y Size: %d\n",
1259 info->matrix_xsize, info->matrix_ysize);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001260
1261 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001262
1263free_object_table:
1264 kfree(data->object_table);
1265 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001266}
1267
Iiro Valkonen7686b102011-02-02 23:21:58 -08001268static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269 struct device_attribute *attr, char *buf)
1270{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001271 struct mxt_data *data = dev_get_drvdata(dev);
1272 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273 int count = 0;
1274 int i, j;
1275 int error;
1276 u8 val;
1277
1278 for (i = 0; i < data->info.object_num; i++) {
1279 object = data->object_table + i;
1280
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001281 count += snprintf(buf + count, PAGE_SIZE - count,
1282 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001283 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001284 if (count >= PAGE_SIZE)
1285 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001286
Iiro Valkonen7686b102011-02-02 23:21:58 -08001287 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001288 count += snprintf(buf + count, PAGE_SIZE - count,
1289 "\n");
1290 if (count >= PAGE_SIZE)
1291 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001292 continue;
1293 }
1294
1295 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001296 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001297 object->type, j, &val);
1298 if (error)
1299 return error;
1300
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001301 count += snprintf(buf + count, PAGE_SIZE - count,
1302 "\t[%2d]: %02x (%d)\n", j, val, val);
1303 if (count >= PAGE_SIZE)
1304 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001305 }
1306
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001307 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1308 if (count >= PAGE_SIZE)
1309 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001310 }
1311
1312 return count;
1313}
1314
Jing Lincc974cb2012-02-01 23:13:14 -08001315static int strtobyte(const char *data, u8 *value)
1316{
1317 char str[3];
1318
1319 str[0] = data[0];
1320 str[1] = data[1];
1321 str[2] = '\0';
1322
1323 return kstrtou8(str, 16, value);
1324}
1325
Iiro Valkonen7686b102011-02-02 23:21:58 -08001326static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001327{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001328 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001329 struct i2c_client *client = data->client;
1330 const struct firmware *fw = NULL;
1331 unsigned int frame_size;
Nick Dyer0a4016c2012-01-18 15:17:59 +05301332 unsigned int retry = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001333 unsigned int pos = 0;
Jing Lincc974cb2012-02-01 23:13:14 -08001334 int ret, i, max_frame_size;
1335 u8 *frame;
1336
1337 switch (data->info.family_id) {
1338 case MXT224_ID:
1339 max_frame_size = MXT_SINGLE_FW_MAX_FRAME_SIZE;
1340 break;
1341 case MXT1386_ID:
1342 max_frame_size = MXT_CHIPSET_FW_MAX_FRAME_SIZE;
1343 break;
1344 default:
1345 return -EINVAL;
1346 }
1347
1348 frame = kmalloc(max_frame_size, GFP_KERNEL);
1349 if (!frame) {
1350 dev_err(dev, "Unable to allocate memory for frame data\n");
1351 return -ENOMEM;
1352 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001353
1354 ret = request_firmware(&fw, fn, dev);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301355 if (ret < 0) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001356 dev_err(dev, "Unable to open firmware %s\n", fn);
Jing Lincc974cb2012-02-01 23:13:14 -08001357 goto free_frame;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358 }
1359
Nick Dyer0a4016c2012-01-18 15:17:59 +05301360 if (data->state != BOOTLOADER) {
1361 /* Change to the bootloader mode */
1362 mxt_write_object(data, MXT_GEN_COMMAND_T6,
1363 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
1364 mxt_reset_delay(data);
Amy Maloche7e447432011-09-14 11:36:30 -07001365
Nick Dyer0a4016c2012-01-18 15:17:59 +05301366 ret = mxt_switch_to_bootloader_address(data);
1367 if (ret)
1368 goto release_firmware;
1369 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001370
Iiro Valkonen7686b102011-02-02 23:21:58 -08001371 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301372 if (ret) {
1373 /* Bootloader may still be unlocked from previous update
1374 * attempt */
1375 ret = mxt_check_bootloader(client,
1376 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001377
Nick Dyer0a4016c2012-01-18 15:17:59 +05301378 if (ret)
1379 goto return_to_app_mode;
1380 } else {
1381 dev_info(dev, "Unlocking bootloader\n");
1382 /* Unlock bootloader */
1383 mxt_unlock_bootloader(client);
1384 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001385
1386 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001387 ret = mxt_check_bootloader(client,
1388 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389 if (ret)
Nick Dyer0a4016c2012-01-18 15:17:59 +05301390 goto release_firmware;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001391
Jing Lincc974cb2012-02-01 23:13:14 -08001392 /* Get frame length MSB */
1393 ret = strtobyte(fw->data + pos, frame);
1394 if (ret)
1395 goto release_firmware;
1396
1397 /* Get frame length LSB */
1398 ret = strtobyte(fw->data + pos + 2, frame + 1);
1399 if (ret)
1400 goto release_firmware;
1401
1402 frame_size = ((*frame << 8) | *(frame + 1));
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001403
1404 /* We should add 2 at frame size as the the firmware data is not
1405 * included the CRC bytes.
1406 */
1407 frame_size += 2;
1408
Jing Lincc974cb2012-02-01 23:13:14 -08001409 if (frame_size > max_frame_size) {
1410 dev_err(dev, "Invalid frame size - %d\n", frame_size);
1411 ret = -EINVAL;
1412 goto release_firmware;
1413 }
1414
1415 /* Convert frame data and CRC from hex to binary */
1416 for (i = 2; i < frame_size; i++) {
1417 ret = strtobyte(fw->data + pos + i * 2, frame + i);
1418 if (ret)
1419 goto release_firmware;
1420 }
1421
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001422 /* Write one frame to device */
Jing Lincc974cb2012-02-01 23:13:14 -08001423 mxt_fw_write(client, frame, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001424
Iiro Valkonen7686b102011-02-02 23:21:58 -08001425 ret = mxt_check_bootloader(client,
1426 MXT_FRAME_CRC_PASS);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301427 if (ret) {
1428 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001429
Nick Dyer0a4016c2012-01-18 15:17:59 +05301430 /* Back off by 20ms per retry */
1431 msleep(retry * 20);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001432
Nick Dyer0a4016c2012-01-18 15:17:59 +05301433 if (retry > 20)
1434 goto release_firmware;
1435 } else {
1436 retry = 0;
Jing Lincc974cb2012-02-01 23:13:14 -08001437 pos += frame_size * 2;
1438 dev_dbg(dev, "Updated %d/%zd bytes\n", pos, fw->size);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301439 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001440 }
1441
Nick Dyer0a4016c2012-01-18 15:17:59 +05301442return_to_app_mode:
1443 mxt_switch_to_appmode_address(data);
1444release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001445 release_firmware(fw);
Jing Lincc974cb2012-02-01 23:13:14 -08001446free_frame:
1447 kfree(frame);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001448
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001449 return ret;
1450}
1451
Iiro Valkonen7686b102011-02-02 23:21:58 -08001452static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001453 struct device_attribute *attr,
1454 const char *buf, size_t count)
1455{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001456 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001457 int error;
1458
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001459 /* If fw_name is set, then the existing firmware has an upgrade */
1460 if (!data->fw_name) {
1461 dev_err(dev, "Firmware name not specifed in platform data\n");
1462 return -EINVAL;
1463 }
1464
1465 dev_info(dev, "Upgrading the firmware file to %s\n", data->fw_name);
1466
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001467 disable_irq(data->irq);
1468
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001469 error = mxt_load_fw(dev, data->fw_name);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470 if (error) {
1471 dev_err(dev, "The firmware update failed(%d)\n", error);
1472 count = error;
1473 } else {
Nick Dyer0a4016c2012-01-18 15:17:59 +05301474 dev_info(dev, "The firmware update succeeded\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001475
1476 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001477 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001478
Nick Dyer0a4016c2012-01-18 15:17:59 +05301479 data->state = INIT;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001480 kfree(data->object_table);
1481 data->object_table = NULL;
Jing Lincc974cb2012-02-01 23:13:14 -08001482 data->cfg_version_idx = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001483
Iiro Valkonen7686b102011-02-02 23:21:58 -08001484 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001485 }
1486
Nick Dyer0a4016c2012-01-18 15:17:59 +05301487 if (data->state == APPMODE) {
1488 enable_irq(data->irq);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001489
Nick Dyer0a4016c2012-01-18 15:17:59 +05301490 error = mxt_make_highchg(data);
1491 if (error)
1492 return error;
1493 }
Iiro Valkonen08960a02011-04-12 23:16:40 -07001494
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001495 return count;
1496}
1497
Iiro Valkonen7686b102011-02-02 23:21:58 -08001498static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1499static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001500
Iiro Valkonen7686b102011-02-02 23:21:58 -08001501static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001502 &dev_attr_object.attr,
1503 &dev_attr_update_fw.attr,
1504 NULL
1505};
1506
Iiro Valkonen7686b102011-02-02 23:21:58 -08001507static const struct attribute_group mxt_attr_group = {
1508 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001509};
1510
Amy Maloche52262212011-09-15 16:46:57 -07001511static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001512{
Jing Lin36aee812011-10-17 17:17:28 -07001513 int error;
1514
Amy Maloche52262212011-09-15 16:46:57 -07001515 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001516 error = __mxt_write_reg(data->client, data->t7_start_addr,
1517 T7_DATA_SIZE, data->t7_data);
1518 if (error < 0) {
1519 dev_err(&data->client->dev,
1520 "failed to restore old power state\n");
1521 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001522 }
Jing Lin36aee812011-10-17 17:17:28 -07001523
Amy Maloche52262212011-09-15 16:46:57 -07001524 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001525 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001526 if (error < 0) {
1527 dev_err(&data->client->dev, "failed to restore touch\n");
1528 return error;
1529 }
1530
1531 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001532}
1533
Amy Maloche52262212011-09-15 16:46:57 -07001534static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001535{
Jing Lin36aee812011-10-17 17:17:28 -07001536 int error;
1537 u8 t7_data[T7_DATA_SIZE] = {0};
1538
1539 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001540 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001541 if (error < 0) {
1542 dev_err(&data->client->dev, "failed to disable touch\n");
1543 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001544 }
1545
Jing Lin36aee812011-10-17 17:17:28 -07001546 error = __mxt_write_reg(data->client, data->t7_start_addr,
1547 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001548 if (error < 0) {
1549 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001550 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001551 return error;
1552 }
1553
1554 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001555}
1556
Iiro Valkonen7686b102011-02-02 23:21:58 -08001557static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001558{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001559 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001560 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001561
Amy Maloche52262212011-09-15 16:46:57 -07001562 error = mxt_start(data);
1563 if (error < 0) {
1564 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1565 return error;
1566 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001567
1568 return 0;
1569}
1570
Iiro Valkonen7686b102011-02-02 23:21:58 -08001571static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001572{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001573 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001574 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001575
Amy Maloche52262212011-09-15 16:46:57 -07001576 error = mxt_stop(data);
1577 if (error < 0)
1578 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1579
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001580}
1581
Amy Malochec331f842012-01-24 10:33:47 -08001582static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1583{
1584 return (regulator_count_voltages(reg) > 0) ?
1585 regulator_set_optimum_mode(reg, load_uA) : 0;
1586}
1587
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301588static int mxt_power_on(struct mxt_data *data, bool on)
1589{
1590 int rc;
1591
1592 if (on == false)
1593 goto power_off;
1594
Amy Malochec331f842012-01-24 10:33:47 -08001595 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301596 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001597 dev_err(&data->client->dev,
1598 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301599 return rc;
1600 }
1601
Amy Maloche21115eb2011-11-02 09:04:37 -07001602 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301603 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001604 dev_err(&data->client->dev,
1605 "Regulator vcc_ana enable failed rc=%d\n", rc);
1606 goto error_reg_en_vcc_ana;
1607 }
1608
1609 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001610 rc = reg_set_optimum_mode_check(data->vcc_dig,
1611 MXT_ACTIVE_LOAD_DIG_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001612 if (rc < 0) {
1613 dev_err(&data->client->dev,
1614 "Regulator vcc_dig set_opt failed rc=%d\n",
1615 rc);
1616 goto error_reg_opt_vcc_dig;
1617 }
1618
1619 rc = regulator_enable(data->vcc_dig);
1620 if (rc) {
1621 dev_err(&data->client->dev,
1622 "Regulator vcc_dig enable failed rc=%d\n", rc);
1623 goto error_reg_en_vcc_dig;
1624 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301625 }
1626
1627 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001628 rc = reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301629 if (rc < 0) {
1630 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001631 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301632 goto error_reg_opt_i2c;
1633 }
1634
1635 rc = regulator_enable(data->vcc_i2c);
1636 if (rc) {
1637 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001638 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301639 goto error_reg_en_vcc_i2c;
1640 }
1641 }
1642
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001643 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301644
1645 return 0;
1646
1647error_reg_en_vcc_i2c:
1648 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001649 reg_set_optimum_mode_check(data->vcc_i2c, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301650error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001651 if (data->pdata->digital_pwr_regulator)
1652 regulator_disable(data->vcc_dig);
1653error_reg_en_vcc_dig:
1654 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001655 reg_set_optimum_mode_check(data->vcc_dig, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001656error_reg_opt_vcc_dig:
1657 regulator_disable(data->vcc_ana);
1658error_reg_en_vcc_ana:
Amy Malochec331f842012-01-24 10:33:47 -08001659 reg_set_optimum_mode_check(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301660 return rc;
1661
1662power_off:
Amy Malochec331f842012-01-24 10:33:47 -08001663 reg_set_optimum_mode_check(data->vcc_ana, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001664 regulator_disable(data->vcc_ana);
1665 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001666 reg_set_optimum_mode_check(data->vcc_dig, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001667 regulator_disable(data->vcc_dig);
1668 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301669 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001670 reg_set_optimum_mode_check(data->vcc_i2c, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301671 regulator_disable(data->vcc_i2c);
1672 }
1673 msleep(50);
1674 return 0;
1675}
1676
1677static int mxt_regulator_configure(struct mxt_data *data, bool on)
1678{
1679 int rc;
1680
1681 if (on == false)
1682 goto hw_shutdown;
1683
Amy Maloche21115eb2011-11-02 09:04:37 -07001684 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1685 if (IS_ERR(data->vcc_ana)) {
1686 rc = PTR_ERR(data->vcc_ana);
1687 dev_err(&data->client->dev,
1688 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301689 return rc;
1690 }
1691
Amy Maloche21115eb2011-11-02 09:04:37 -07001692 if (regulator_count_voltages(data->vcc_ana) > 0) {
1693 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301694 MXT_VTG_MAX_UV);
1695 if (rc) {
1696 dev_err(&data->client->dev,
1697 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001698 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301699 }
1700 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001701 if (data->pdata->digital_pwr_regulator) {
1702 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1703 if (IS_ERR(data->vcc_dig)) {
1704 rc = PTR_ERR(data->vcc_dig);
1705 dev_err(&data->client->dev,
1706 "Regulator get dig failed rc=%d\n", rc);
1707 goto error_get_vtg_vcc_dig;
1708 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301709
Amy Maloche21115eb2011-11-02 09:04:37 -07001710 if (regulator_count_voltages(data->vcc_dig) > 0) {
1711 rc = regulator_set_voltage(data->vcc_dig,
1712 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1713 if (rc) {
1714 dev_err(&data->client->dev,
1715 "regulator set_vtg failed rc=%d\n", rc);
1716 goto error_set_vtg_vcc_dig;
1717 }
1718 }
1719 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301720 if (data->pdata->i2c_pull_up) {
1721 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1722 if (IS_ERR(data->vcc_i2c)) {
1723 rc = PTR_ERR(data->vcc_i2c);
1724 dev_err(&data->client->dev,
1725 "Regulator get failed rc=%d\n", rc);
1726 goto error_get_vtg_i2c;
1727 }
1728 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1729 rc = regulator_set_voltage(data->vcc_i2c,
1730 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1731 if (rc) {
1732 dev_err(&data->client->dev,
1733 "regulator set_vtg failed rc=%d\n", rc);
1734 goto error_set_vtg_i2c;
1735 }
1736 }
1737 }
1738
1739 return 0;
1740
1741error_set_vtg_i2c:
1742 regulator_put(data->vcc_i2c);
1743error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001744 if (data->pdata->digital_pwr_regulator)
1745 if (regulator_count_voltages(data->vcc_dig) > 0)
1746 regulator_set_voltage(data->vcc_dig, 0,
1747 MXT_VTG_DIG_MAX_UV);
1748error_set_vtg_vcc_dig:
1749 if (data->pdata->digital_pwr_regulator)
1750 regulator_put(data->vcc_dig);
1751error_get_vtg_vcc_dig:
1752 if (regulator_count_voltages(data->vcc_ana) > 0)
1753 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1754error_set_vtg_vcc_ana:
1755 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301756 return rc;
1757
1758hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001759 if (regulator_count_voltages(data->vcc_ana) > 0)
1760 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1761 regulator_put(data->vcc_ana);
1762 if (data->pdata->digital_pwr_regulator) {
1763 if (regulator_count_voltages(data->vcc_dig) > 0)
1764 regulator_set_voltage(data->vcc_dig, 0,
1765 MXT_VTG_DIG_MAX_UV);
1766 regulator_put(data->vcc_dig);
1767 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301768 if (data->pdata->i2c_pull_up) {
1769 if (regulator_count_voltages(data->vcc_i2c) > 0)
1770 regulator_set_voltage(data->vcc_i2c, 0,
1771 MXT_I2C_VTG_MAX_UV);
1772 regulator_put(data->vcc_i2c);
1773 }
1774 return 0;
1775}
1776
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301777#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001778static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1779{
1780
1781 int rc;
1782
1783 if (on == false)
1784 goto regulator_hpm;
1785
Amy Malochec331f842012-01-24 10:33:47 -08001786 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001787 if (rc < 0) {
1788 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001789 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001790 goto fail_regulator_lpm;
1791 }
1792
Amy Maloche21115eb2011-11-02 09:04:37 -07001793 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001794 rc = reg_set_optimum_mode_check(data->vcc_dig,
Amy Maloche21115eb2011-11-02 09:04:37 -07001795 MXT_LPM_LOAD_DIG_UA);
1796 if (rc < 0) {
1797 dev_err(&data->client->dev,
1798 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1799 goto fail_regulator_lpm;
1800 }
1801 }
1802
Jing Linbace50b2011-10-18 22:55:47 -07001803 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001804 rc = reg_set_optimum_mode_check(data->vcc_i2c,
Jing Linbace50b2011-10-18 22:55:47 -07001805 MXT_I2C_LPM_LOAD_UA);
1806 if (rc < 0) {
1807 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001808 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001809 goto fail_regulator_lpm;
1810 }
1811 }
1812
1813 return 0;
1814
1815regulator_hpm:
1816
Amy Malochec331f842012-01-24 10:33:47 -08001817 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001818 if (rc < 0) {
1819 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001820 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001821 goto fail_regulator_hpm;
1822 }
1823
Amy Maloche21115eb2011-11-02 09:04:37 -07001824 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001825 rc = reg_set_optimum_mode_check(data->vcc_dig,
Amy Maloche21115eb2011-11-02 09:04:37 -07001826 MXT_ACTIVE_LOAD_DIG_UA);
1827 if (rc < 0) {
1828 dev_err(&data->client->dev,
1829 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1830 goto fail_regulator_hpm;
1831 }
1832 }
1833
Jing Linbace50b2011-10-18 22:55:47 -07001834 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001835 rc = reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001836 if (rc < 0) {
1837 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001838 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001839 goto fail_regulator_hpm;
1840 }
1841 }
1842
1843 return 0;
1844
1845fail_regulator_lpm:
Amy Malochec331f842012-01-24 10:33:47 -08001846 reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001847 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001848 reg_set_optimum_mode_check(data->vcc_dig,
1849 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001850 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001851 reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001852
1853 return rc;
1854
1855fail_regulator_hpm:
Amy Malochec331f842012-01-24 10:33:47 -08001856 reg_set_optimum_mode_check(data->vcc_ana, MXT_LPM_LOAD_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001857 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001858 reg_set_optimum_mode_check(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001859 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001860 reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001861
1862 return rc;
1863}
1864
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301865static int mxt_suspend(struct device *dev)
1866{
1867 struct i2c_client *client = to_i2c_client(dev);
1868 struct mxt_data *data = i2c_get_clientdata(client);
1869 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001870 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301871
1872 mutex_lock(&input_dev->mutex);
1873
Amy Maloche52262212011-09-15 16:46:57 -07001874 if (input_dev->users) {
1875 error = mxt_stop(data);
1876 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001877 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001878 mutex_unlock(&input_dev->mutex);
1879 return error;
1880 }
1881
1882 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301883
1884 mutex_unlock(&input_dev->mutex);
1885
Jing Linbace50b2011-10-18 22:55:47 -07001886 /* put regulators in low power mode */
1887 error = mxt_regulator_lpm(data, true);
1888 if (error < 0) {
1889 dev_err(dev, "failed to enter low power mode\n");
1890 return error;
1891 }
1892
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301893 return 0;
1894}
1895
1896static int mxt_resume(struct device *dev)
1897{
1898 struct i2c_client *client = to_i2c_client(dev);
1899 struct mxt_data *data = i2c_get_clientdata(client);
1900 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001901 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301902
Jing Linbace50b2011-10-18 22:55:47 -07001903 /* put regulators in high power mode */
1904 error = mxt_regulator_lpm(data, false);
1905 if (error < 0) {
1906 dev_err(dev, "failed to enter high power mode\n");
1907 return error;
1908 }
1909
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301910 mutex_lock(&input_dev->mutex);
1911
Amy Maloche52262212011-09-15 16:46:57 -07001912 if (input_dev->users) {
1913 error = mxt_start(data);
1914 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001915 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001916 mutex_unlock(&input_dev->mutex);
1917 return error;
1918 }
1919 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301920
1921 mutex_unlock(&input_dev->mutex);
1922
1923 return 0;
1924}
1925
1926#if defined(CONFIG_HAS_EARLYSUSPEND)
1927static void mxt_early_suspend(struct early_suspend *h)
1928{
1929 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1930
1931 mxt_suspend(&data->client->dev);
1932}
1933
1934static void mxt_late_resume(struct early_suspend *h)
1935{
1936 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1937
1938 mxt_resume(&data->client->dev);
1939}
1940#endif
1941
1942static const struct dev_pm_ops mxt_pm_ops = {
1943#ifndef CONFIG_HAS_EARLYSUSPEND
1944 .suspend = mxt_suspend,
1945 .resume = mxt_resume,
1946#endif
1947};
1948#endif
1949
Jing Lin6cfc00e2011-11-02 15:15:30 -07001950static int mxt_debugfs_object_show(struct seq_file *m, void *v)
1951{
1952 struct mxt_data *data = m->private;
1953 struct mxt_object *object;
1954 struct device *dev = &data->client->dev;
1955 int i, j, k;
1956 int error;
1957 int obj_size;
1958 u8 val;
1959
1960 for (i = 0; i < data->info.object_num; i++) {
1961 object = data->object_table + i;
1962 obj_size = object->size + 1;
1963
1964 seq_printf(m, "Object[%d] (Type %d)\n", i + 1, object->type);
1965
1966 for (j = 0; j < object->instances + 1; j++) {
1967 seq_printf(m, "[Instance %d]\n", j);
1968
1969 for (k = 0; k < obj_size; k++) {
1970 error = mxt_read_object(data, object->type,
1971 j * obj_size + k, &val);
1972 if (error) {
1973 dev_err(dev,
1974 "Failed to read object %d "
1975 "instance %d at offset %d\n",
1976 object->type, j, k);
1977 return error;
1978 }
1979
1980 seq_printf(m, "Byte %d: 0x%02x (%d)\n",
1981 k, val, val);
1982 }
1983 }
1984 }
1985
1986 return 0;
1987}
1988
1989static int mxt_debugfs_object_open(struct inode *inode, struct file *file)
1990{
1991 return single_open(file, mxt_debugfs_object_show, inode->i_private);
1992}
1993
1994static const struct file_operations mxt_object_fops = {
1995 .owner = THIS_MODULE,
1996 .open = mxt_debugfs_object_open,
1997 .read = seq_read,
1998 .release = single_release,
1999};
2000
2001static void __init mxt_debugfs_init(struct mxt_data *data)
2002{
2003 debug_base = debugfs_create_dir(MXT_DEBUGFS_DIR, NULL);
2004 if (IS_ERR_OR_NULL(debug_base))
2005 pr_err("atmel_mxt_ts: Failed to create debugfs dir\n");
2006 if (IS_ERR_OR_NULL(debugfs_create_file(MXT_DEBUGFS_FILE,
2007 0444,
2008 debug_base,
2009 data,
2010 &mxt_object_fops))) {
2011 pr_err("atmel_mxt_ts: Failed to create object file\n");
2012 debugfs_remove_recursive(debug_base);
2013 }
2014}
2015
Iiro Valkonen7686b102011-02-02 23:21:58 -08002016static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002017 const struct i2c_device_id *id)
2018{
Iiro Valkonen919ed892011-02-15 13:36:52 -08002019 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08002020 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002021 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08002022 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002023
Iiro Valkonen919ed892011-02-15 13:36:52 -08002024 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002025 return -EINVAL;
2026
Iiro Valkonen7686b102011-02-02 23:21:58 -08002027 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002028 input_dev = input_allocate_device();
2029 if (!data || !input_dev) {
2030 dev_err(&client->dev, "Failed to allocate memory\n");
2031 error = -ENOMEM;
2032 goto err_free_mem;
2033 }
2034
Nick Dyer0a4016c2012-01-18 15:17:59 +05302035 data->state = INIT;
Amy Maloche83c385a2012-02-01 10:32:03 +05302036 input_dev->name = "atmel_mxt_ts";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002037 input_dev->id.bustype = BUS_I2C;
2038 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08002039 input_dev->open = mxt_input_open;
2040 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002041
Joonyoung Shim910d8052011-04-12 23:14:38 -07002042 data->client = client;
2043 data->input_dev = input_dev;
2044 data->pdata = pdata;
2045 data->irq = client->irq;
2046
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002047 __set_bit(EV_ABS, input_dev->evbit);
2048 __set_bit(EV_KEY, input_dev->evbit);
2049 __set_bit(BTN_TOUCH, input_dev->keybit);
2050
2051 /* For single touch */
2052 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07002053 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002054 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07002055 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07002056 input_set_abs_params(input_dev, ABS_PRESSURE,
2057 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002058
2059 /* For multi touch */
Joonyoung Shimcd473222012-02-14 18:32:48 -08002060 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002061 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08002062 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002063 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07002064 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002065 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07002066 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07002067 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
2068 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002069
Amy Maloche83c385a2012-02-01 10:32:03 +05302070 if (pdata->touch_x_size)
2071 data->touch_x_size = pdata->touch_x_size;
2072 else
2073 data->touch_x_size = pdata->x_size;
2074
2075 if (pdata->touch_y_size)
2076 data->touch_y_size = pdata->touch_y_size;
2077 else
2078 data->touch_y_size = pdata->y_size;
2079
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08002080 /* set key array supported keys */
2081 if (pdata->key_codes) {
2082 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
2083 if (pdata->key_codes[i])
2084 input_set_capability(input_dev, EV_KEY,
2085 pdata->key_codes[i]);
2086 }
2087 }
2088
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002089 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002090 i2c_set_clientdata(client, data);
2091
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302092 if (pdata->init_hw)
2093 error = pdata->init_hw(true);
2094 else
2095 error = mxt_regulator_configure(data, true);
2096 if (error) {
2097 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07002098 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302099 }
2100
2101 if (pdata->power_on)
2102 error = pdata->power_on(true);
2103 else
2104 error = mxt_power_on(data, true);
2105 if (error) {
2106 dev_err(&client->dev, "Failed to power on hardware\n");
2107 goto err_regulator_on;
2108 }
2109
Amy Maloche08266db2011-11-04 11:07:16 -07002110 if (gpio_is_valid(pdata->irq_gpio)) {
2111 /* configure touchscreen irq gpio */
2112 error = gpio_request(pdata->irq_gpio,
2113 "mxt_irq_gpio");
2114 if (error) {
2115 pr_err("%s: unable to request gpio [%d]\n", __func__,
2116 pdata->irq_gpio);
2117 goto err_power_on;
2118 }
2119 error = gpio_direction_input(pdata->irq_gpio);
2120 if (error) {
2121 pr_err("%s: unable to set_direction for gpio [%d]\n",
2122 __func__, pdata->irq_gpio);
2123 goto err_irq_gpio_req;
2124 }
2125 }
2126
2127 if (gpio_is_valid(pdata->reset_gpio)) {
2128 /* configure touchscreen reset out gpio */
2129 error = gpio_request(pdata->reset_gpio,
2130 "mxt_reset_gpio");
2131 if (error) {
2132 pr_err("%s: unable to request reset gpio %d\n",
2133 __func__, pdata->reset_gpio);
2134 goto err_irq_gpio_req;
2135 }
2136
2137 error = gpio_direction_output(
2138 pdata->reset_gpio, 1);
2139 if (error) {
2140 pr_err("%s: unable to set direction for gpio %d\n",
2141 __func__, pdata->reset_gpio);
2142 goto err_reset_gpio_req;
2143 }
2144 }
2145
2146 mxt_reset_delay(data);
2147
Iiro Valkonen7686b102011-02-02 23:21:58 -08002148 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002149 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07002150 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002151
Iiro Valkonen7686b102011-02-02 23:21:58 -08002152 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08002153 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002154 if (error) {
2155 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07002156 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002157 }
2158
Nick Dyer0a4016c2012-01-18 15:17:59 +05302159 if (data->state == APPMODE) {
2160 error = mxt_make_highchg(data);
2161 if (error) {
2162 dev_err(&client->dev, "Failed to make high CHG\n");
2163 goto err_free_irq;
2164 }
2165 }
Iiro Valkonen08960a02011-04-12 23:16:40 -07002166
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002167 error = input_register_device(input_dev);
2168 if (error)
2169 goto err_free_irq;
2170
Iiro Valkonen7686b102011-02-02 23:21:58 -08002171 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002172 if (error)
2173 goto err_unregister_device;
2174
Anirudh Ghayal253ce122011-08-09 19:32:57 +05302175#if defined(CONFIG_HAS_EARLYSUSPEND)
2176 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
2177 MXT_SUSPEND_LEVEL;
2178 data->early_suspend.suspend = mxt_early_suspend;
2179 data->early_suspend.resume = mxt_late_resume;
2180 register_early_suspend(&data->early_suspend);
2181#endif
2182
Jing Lin6cfc00e2011-11-02 15:15:30 -07002183 mxt_debugfs_init(data);
2184
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002185 return 0;
2186
2187err_unregister_device:
2188 input_unregister_device(input_dev);
2189 input_dev = NULL;
2190err_free_irq:
2191 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07002192err_free_object:
2193 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07002194err_reset_gpio_req:
2195 if (gpio_is_valid(pdata->reset_gpio))
2196 gpio_free(pdata->reset_gpio);
2197err_irq_gpio_req:
2198 if (gpio_is_valid(pdata->irq_gpio))
2199 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302200err_power_on:
2201 if (pdata->power_on)
2202 pdata->power_on(false);
2203 else
2204 mxt_power_on(data, false);
2205err_regulator_on:
2206 if (pdata->init_hw)
2207 pdata->init_hw(false);
2208 else
2209 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002210err_free_mem:
2211 input_free_device(input_dev);
2212 kfree(data);
2213 return error;
2214}
2215
Iiro Valkonen7686b102011-02-02 23:21:58 -08002216static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002217{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002218 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002219
Iiro Valkonen7686b102011-02-02 23:21:58 -08002220 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002221 free_irq(data->irq, data);
2222 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05302223#if defined(CONFIG_HAS_EARLYSUSPEND)
2224 unregister_early_suspend(&data->early_suspend);
2225#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302226
2227 if (data->pdata->power_on)
2228 data->pdata->power_on(false);
2229 else
2230 mxt_power_on(data, false);
2231
2232 if (data->pdata->init_hw)
2233 data->pdata->init_hw(false);
2234 else
2235 mxt_regulator_configure(data, false);
2236
Mohan Pallakabfe8f302012-01-02 18:32:08 +08002237 if (gpio_is_valid(data->pdata->reset_gpio))
2238 gpio_free(data->pdata->reset_gpio);
2239
2240 if (gpio_is_valid(data->pdata->irq_gpio))
2241 gpio_free(data->pdata->irq_gpio);
2242
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002243 kfree(data->object_table);
2244 kfree(data);
2245
Jing Lin6cfc00e2011-11-02 15:15:30 -07002246 debugfs_remove_recursive(debug_base);
2247
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002248 return 0;
2249}
2250
Iiro Valkonen7686b102011-02-02 23:21:58 -08002251static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002252 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002253 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08002254 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002255 { }
2256};
Iiro Valkonen7686b102011-02-02 23:21:58 -08002257MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002258
Iiro Valkonen7686b102011-02-02 23:21:58 -08002259static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002260 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08002261 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002262 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002263#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08002264 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002265#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002266 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002267 .probe = mxt_probe,
2268 .remove = __devexit_p(mxt_remove),
2269 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002270};
2271
Iiro Valkonen7686b102011-02-02 23:21:58 -08002272static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002273{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002274 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002275}
2276
Iiro Valkonen7686b102011-02-02 23:21:58 -08002277static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002278{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002279 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002280}
2281
Iiro Valkonen7686b102011-02-02 23:21:58 -08002282module_init(mxt_init);
2283module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002284
2285/* Module information */
2286MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08002287MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002288MODULE_LICENSE("GPL");