blob: f5bfc7b880de29610f6c927f836e4c0e5004d0ed [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
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -0800517static int mxt_get_bootloader_id(struct i2c_client *client)
518{
519 u8 val;
520 u8 buf[3];
521
522 if (i2c_master_recv(client, &val, 1) != 1) {
523 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
524 return -EIO;
525 }
526
527 if (val | MXT_BOOT_EXTENDED_ID) {
528 if (i2c_master_recv(client, &buf[0], 3) != 3) {
529 dev_err(&client->dev, "%s: i2c recv failed\n",
530 __func__);
531 return -EIO;
532 }
533 return buf[1];
534 } else {
535 dev_info(&client->dev, "Bootloader ID:%d",
536 val & MXT_BOOT_ID_MASK);
537
538 return val & MXT_BOOT_ID_MASK;
539 }
540}
541
Iiro Valkonen7686b102011-02-02 23:21:58 -0800542static int mxt_check_bootloader(struct i2c_client *client,
Nick Dyer0a4016c2012-01-18 15:17:59 +0530543 unsigned int state)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544{
545 u8 val;
546
547recheck:
548 if (i2c_master_recv(client, &val, 1) != 1) {
549 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
550 return -EIO;
551 }
552
553 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800554 case MXT_WAITING_BOOTLOAD_CMD:
Nick Dyer0a4016c2012-01-18 15:17:59 +0530555 val = mxt_get_bootloader_version(client, val);
556 val &= ~MXT_BOOT_STATUS_MASK;
557 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800558 case MXT_WAITING_FRAME_DATA:
Nick Dyer0a4016c2012-01-18 15:17:59 +0530559 case MXT_APP_CRC_FAIL:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800562 case MXT_FRAME_CRC_PASS:
563 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564 goto recheck;
Nick Dyer0a4016c2012-01-18 15:17:59 +0530565 if (val == MXT_FRAME_CRC_FAIL) {
566 dev_err(&client->dev, "Bootloader CRC fail\n");
567 return -EINVAL;
568 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569 break;
570 default:
571 return -EINVAL;
572 }
573
574 if (val != state) {
Nick Dyer0a4016c2012-01-18 15:17:59 +0530575 dev_err(&client->dev, "Invalid bootloader mode state %X\n",
576 val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 return -EINVAL;
578 }
579
580 return 0;
581}
582
Iiro Valkonen7686b102011-02-02 23:21:58 -0800583static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700584{
585 u8 buf[2];
586
Iiro Valkonen7686b102011-02-02 23:21:58 -0800587 buf[0] = MXT_UNLOCK_CMD_LSB;
588 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589
590 if (i2c_master_send(client, buf, 2) != 2) {
591 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
592 return -EIO;
593 }
594
595 return 0;
596}
597
Iiro Valkonen7686b102011-02-02 23:21:58 -0800598static int mxt_fw_write(struct i2c_client *client,
Nick Dyer0a4016c2012-01-18 15:17:59 +0530599 const u8 *data, unsigned int frame_size)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600{
601 if (i2c_master_send(client, data, frame_size) != frame_size) {
602 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
603 return -EIO;
604 }
605
606 return 0;
607}
608
Iiro Valkonen7686b102011-02-02 23:21:58 -0800609static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700610 u16 reg, u16 len, void *val)
611{
612 struct i2c_msg xfer[2];
613 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700614 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700615
616 buf[0] = reg & 0xff;
617 buf[1] = (reg >> 8) & 0xff;
618
619 /* Write register */
620 xfer[0].addr = client->addr;
621 xfer[0].flags = 0;
622 xfer[0].len = 2;
623 xfer[0].buf = buf;
624
625 /* Read data */
626 xfer[1].addr = client->addr;
627 xfer[1].flags = I2C_M_RD;
628 xfer[1].len = len;
629 xfer[1].buf = val;
630
Jing Lin36aee812011-10-17 17:17:28 -0700631 do {
632 if (i2c_transfer(client->adapter, xfer, 2) == 2)
633 return 0;
634 msleep(MXT_WAKE_TIME);
635 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636
Jing Lin36aee812011-10-17 17:17:28 -0700637 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
638 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639}
640
Iiro Valkonen7686b102011-02-02 23:21:58 -0800641static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700642{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800643 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700644}
645
Jing Lin36aee812011-10-17 17:17:28 -0700646static int __mxt_write_reg(struct i2c_client *client,
647 u16 addr, u16 length, u8 *value)
648{
649 u8 buf[MXT_BLOCK_SIZE + 2];
650 int i, tries = 0;
651
652 if (length > MXT_BLOCK_SIZE)
653 return -EINVAL;
654
655 buf[0] = addr & 0xff;
656 buf[1] = (addr >> 8) & 0xff;
657 for (i = 0; i < length; i++)
658 buf[i + 2] = *value++;
659
660 do {
661 if (i2c_master_send(client, buf, length + 2) == (length + 2))
662 return 0;
663 msleep(MXT_WAKE_TIME);
664 } while (++tries < MXT_MAX_RW_TRIES);
665
666 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
667 return -EIO;
668}
669
Iiro Valkonen7686b102011-02-02 23:21:58 -0800670static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700671{
Jing Lin36aee812011-10-17 17:17:28 -0700672 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700673}
674
Iiro Valkonen7686b102011-02-02 23:21:58 -0800675static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700676 u16 reg, u8 *object_buf)
677{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800678 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679 object_buf);
680}
681
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682static struct mxt_object *
683mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800685 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686 int i;
687
688 for (i = 0; i < data->info.object_num; i++) {
689 object = data->object_table + i;
690 if (object->type == type)
691 return object;
692 }
693
694 dev_err(&data->client->dev, "Invalid object type\n");
695 return NULL;
696}
697
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698static int mxt_read_message(struct mxt_data *data,
699 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800701 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702 u16 reg;
703
Iiro Valkonene8645592011-11-18 12:56:19 -0800704 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705 if (!object)
706 return -EINVAL;
707
708 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800709 return __mxt_read_reg(data->client, reg,
710 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711}
712
Iiro Valkonen7686b102011-02-02 23:21:58 -0800713static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714 u8 type, u8 offset, u8 *val)
715{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717 u16 reg;
718
Iiro Valkonen7686b102011-02-02 23:21:58 -0800719 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720 if (!object)
721 return -EINVAL;
722
723 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725}
726
Iiro Valkonen7686b102011-02-02 23:21:58 -0800727static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728 u8 type, u8 offset, u8 val)
729{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800730 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731 u16 reg;
732
Iiro Valkonen7686b102011-02-02 23:21:58 -0800733 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 if (!object)
735 return -EINVAL;
736
737 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739}
740
Iiro Valkonen7686b102011-02-02 23:21:58 -0800741static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700742{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800743 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700744 struct input_dev *input_dev = data->input_dev;
745 int status = finger[single_id].status;
746 int finger_num = 0;
747 int id;
748
Iiro Valkonen7686b102011-02-02 23:21:58 -0800749 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700750 if (!finger[id].status)
751 continue;
752
Joonyoung Shimcd473222012-02-14 18:32:48 -0800753 input_mt_slot(input_dev, id);
754 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
755 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756
Joonyoung Shimcd473222012-02-14 18:32:48 -0800757 if (finger[id].status != MXT_RELEASE) {
Amy Maloche2b59bab2011-10-13 16:08:16 -0700758 finger_num++;
Joonyoung Shimcd473222012-02-14 18:32:48 -0800759 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
760 finger[id].area);
761 input_report_abs(input_dev, ABS_MT_POSITION_X,
762 finger[id].x);
763 input_report_abs(input_dev, ABS_MT_POSITION_Y,
764 finger[id].y);
765 input_report_abs(input_dev, ABS_MT_PRESSURE,
766 finger[id].area);
767 } else {
768 finger[id].status = 0;
769 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700770 }
771
772 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
773
Iiro Valkonen7686b102011-02-02 23:21:58 -0800774 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700775 input_report_abs(input_dev, ABS_X, finger[single_id].x);
776 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700777 input_report_abs(input_dev,
778 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779 }
780
781 input_sync(input_dev);
782}
783
Iiro Valkonen7686b102011-02-02 23:21:58 -0800784static void mxt_input_touchevent(struct mxt_data *data,
785 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800787 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788 struct device *dev = &data->client->dev;
789 u8 status = message->message[0];
790 int x;
791 int y;
792 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700793 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794
795 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800796 if (!(status & MXT_DETECT)) {
797 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798 dev_dbg(dev, "[%d] released\n", id);
799
Iiro Valkonen7686b102011-02-02 23:21:58 -0800800 finger[id].status = MXT_RELEASE;
801 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700802 }
803 return;
804 }
805
806 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800807 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700808 return;
809
Joonyoung Shim910d8052011-04-12 23:14:38 -0700810 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
811 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Amy Maloche83c385a2012-02-01 10:32:03 +0530812 if (data->touch_x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700813 x = x >> 2;
Amy Maloche83c385a2012-02-01 10:32:03 +0530814 if (data->touch_y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700815 y = y >> 2;
816
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700817 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700818 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700819
820 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800821 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700822 x, y, area);
823
Iiro Valkonen7686b102011-02-02 23:21:58 -0800824 finger[id].status = status & MXT_MOVE ?
825 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700826 finger[id].x = x;
827 finger[id].y = y;
828 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700829 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700830
Iiro Valkonen7686b102011-02-02 23:21:58 -0800831 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700832}
833
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800834static void mxt_handle_key_array(struct mxt_data *data,
835 struct mxt_message *message)
836{
837 u32 keys_changed;
838 int i;
839
840 if (!data->pdata->key_codes) {
841 dev_err(&data->client->dev, "keyarray is not supported\n");
842 return;
843 }
844
845 data->keyarray_new = message->message[1] |
846 (message->message[2] << 8) |
847 (message->message[3] << 16) |
848 (message->message[4] << 24);
849
850 keys_changed = data->keyarray_old ^ data->keyarray_new;
851
852 if (!keys_changed) {
853 dev_dbg(&data->client->dev, "no keys changed\n");
854 return;
855 }
856
857 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
858 if (!(keys_changed & (1 << i)))
859 continue;
860
861 input_report_key(data->input_dev, data->pdata->key_codes[i],
862 (data->keyarray_new & (1 << i)));
863 input_sync(data->input_dev);
864 }
865
866 data->keyarray_old = data->keyarray_new;
867}
868
Iiro Valkonen7686b102011-02-02 23:21:58 -0800869static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700870{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800871 struct mxt_data *data = dev_id;
872 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700873 struct device *dev = &data->client->dev;
874 int id;
875 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700876
Nick Dyer0a4016c2012-01-18 15:17:59 +0530877 if (data->state != APPMODE) {
878 dev_err(dev, "Ignoring IRQ - not in APPMODE state\n");
879 return IRQ_HANDLED;
880 }
881
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700882 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800883 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700884 dev_err(dev, "Failed to read message\n");
885 goto end;
886 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700887 reportid = message.reportid;
888
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800889 if (!reportid) {
890 dev_dbg(dev, "Report id 0 is reserved\n");
891 continue;
892 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800894 /* check whether report id is part of T9 or T15 */
895 id = reportid - data->t9_min_reportid;
896
897 if (reportid >= data->t9_min_reportid &&
898 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800899 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800900 else if (reportid >= data->t15_min_reportid &&
901 reportid <= data->t15_max_reportid)
902 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700903 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800904 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700905 } while (reportid != 0xff);
906
907end:
908 return IRQ_HANDLED;
909}
910
Iiro Valkonen7686b102011-02-02 23:21:58 -0800911static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700912{
Jing Lindc4413c2012-01-16 15:22:52 -0800913 const struct mxt_config_info *config_info = data->config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800914 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700915 struct device *dev = &data->client->dev;
916 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800917 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700918
Jing Lindc4413c2012-01-16 15:22:52 -0800919 if (!config_info) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800920 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
921 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700922 }
923
924 for (i = 0; i < data->info.object_num; i++) {
925 object = data->object_table + i;
926
Iiro Valkonen7686b102011-02-02 23:21:58 -0800927 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700928 continue;
929
Iiro Valkonen71749f52011-02-15 13:36:52 -0800930 for (j = 0; j < object->size + 1; j++) {
931 config_offset = index + j;
Jing Lindc4413c2012-01-16 15:22:52 -0800932 if (config_offset > config_info->config_length) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800933 dev_err(dev, "Not enough config data!\n");
934 return -EINVAL;
935 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800936 mxt_write_object(data, object->type, j,
Jing Lindc4413c2012-01-16 15:22:52 -0800937 config_info->config[config_offset]);
Iiro Valkonen71749f52011-02-15 13:36:52 -0800938 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700939 index += object->size + 1;
940 }
941
942 return 0;
943}
944
Iiro Valkonen7686b102011-02-02 23:21:58 -0800945static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946{
947 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800948 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700949 int count = 10;
950 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700951
952 /* Read dummy message to make high CHG pin */
953 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800954 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955 if (error)
956 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800957 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958
959 if (!count) {
960 dev_err(dev, "CHG pin isn't cleared\n");
961 return -EBUSY;
962 }
963
964 return 0;
965}
966
Iiro Valkonen7686b102011-02-02 23:21:58 -0800967static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700968{
969 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800970 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700971 int error;
972 u8 val;
973
Iiro Valkonen7686b102011-02-02 23:21:58 -0800974 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700975 if (error)
976 return error;
977 info->family_id = val;
978
Iiro Valkonen7686b102011-02-02 23:21:58 -0800979 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980 if (error)
981 return error;
982 info->variant_id = val;
983
Iiro Valkonen7686b102011-02-02 23:21:58 -0800984 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700985 if (error)
986 return error;
987 info->version = val;
988
Iiro Valkonen7686b102011-02-02 23:21:58 -0800989 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700990 if (error)
991 return error;
992 info->build = val;
993
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700995 if (error)
996 return error;
997 info->object_num = val;
998
999 return 0;
1000}
1001
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003{
1004 int error;
1005 int i;
1006 u16 reg;
1007 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001008 u8 buf[MXT_OBJECT_SIZE];
Jing Lindc4413c2012-01-16 15:22:52 -08001009 bool found_t38 = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010
1011 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001012 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001013
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
1015 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016 if (error)
1017 return error;
1018
1019 object->type = buf[0];
1020 object->start_address = (buf[2] << 8) | buf[1];
1021 object->size = buf[3];
1022 object->instances = buf[4];
1023 object->num_report_ids = buf[5];
1024
1025 if (object->num_report_ids) {
1026 reportid += object->num_report_ids *
1027 (object->instances + 1);
1028 object->max_reportid = reportid;
1029 }
Jing Lindc4413c2012-01-16 15:22:52 -08001030
1031 /* Calculate index for config major version in config array.
1032 * Major version is the first byte in object T38.
1033 */
1034 if (object->type == MXT_SPT_USERDATA_T38)
1035 found_t38 = true;
1036 if (!found_t38 && mxt_object_writable(object->type))
1037 data->cfg_version_idx += object->size + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038 }
1039
1040 return 0;
1041}
1042
Jing Lindc4413c2012-01-16 15:22:52 -08001043static int mxt_search_config_array(struct mxt_data *data, bool version_match)
1044{
1045
1046 const struct mxt_platform_data *pdata = data->pdata;
1047 const struct mxt_config_info *cfg_info;
1048 struct mxt_info *info = &data->info;
1049 int i;
1050 u8 cfg_version;
1051
1052 for (i = 0; i < pdata->config_array_size; i++) {
1053
1054 cfg_info = &pdata->config_array[i];
1055
1056 if (!cfg_info->config || !cfg_info->config_length)
1057 continue;
1058
1059 if (info->family_id == cfg_info->family_id &&
1060 info->variant_id == cfg_info->variant_id &&
1061 info->version == cfg_info->version &&
1062 info->build == cfg_info->build) {
1063
1064 cfg_version = cfg_info->config[data->cfg_version_idx];
1065 if (data->curr_cfg_version == cfg_version ||
1066 !version_match) {
1067 data->config_info = cfg_info;
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001068 data->fw_name = pdata->config_array[i].fw_name;
Jing Lindc4413c2012-01-16 15:22:52 -08001069 return 0;
1070 }
1071 }
1072 }
1073
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001074 data->fw_name = NULL;
Jing Lindc4413c2012-01-16 15:22:52 -08001075 dev_info(&data->client->dev,
1076 "Config not found: F: %d, V: %d, FW: %d.%d.%d, CFG: %d\n",
1077 info->family_id, info->variant_id,
1078 info->version >> 4, info->version & 0xF, info->build,
1079 data->curr_cfg_version);
1080 return -EINVAL;
1081}
1082
1083static int mxt_get_config(struct mxt_data *data)
1084{
1085 const struct mxt_platform_data *pdata = data->pdata;
1086 struct device *dev = &data->client->dev;
1087 struct mxt_object *object;
1088 int error;
1089
1090 if (!pdata->config_array || !pdata->config_array_size) {
1091 dev_dbg(dev, "No cfg data provided by platform data\n");
1092 return 0;
1093 }
1094
1095 /* Get current config version */
1096 object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
1097 if (!object) {
1098 dev_err(dev, "Unable to obtain USERDATA object\n");
1099 return -EINVAL;
1100 }
1101
1102 error = mxt_read_reg(data->client, object->start_address,
1103 &data->curr_cfg_version);
1104 if (error) {
1105 dev_err(dev, "Unable to read config version\n");
1106 return error;
1107 }
1108
1109 /* It is possible that the config data on the controller is not
1110 * versioned and the version number returns 0. In this case,
1111 * find a match without the config version checking.
1112 */
1113 error = mxt_search_config_array(data,
1114 data->curr_cfg_version != 0 ? true : false);
1115 if (error)
1116 return error;
1117
1118 return 0;
1119}
Amy Maloche7e447432011-09-14 11:36:30 -07001120static void mxt_reset_delay(struct mxt_data *data)
1121{
1122 struct mxt_info *info = &data->info;
1123
1124 switch (info->family_id) {
1125 case MXT224_ID:
1126 msleep(MXT224_RESET_TIME);
1127 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -07001128 case MXT224E_ID:
1129 msleep(MXT224E_RESET_TIME);
1130 break;
Amy Maloche7e447432011-09-14 11:36:30 -07001131 case MXT1386_ID:
1132 msleep(MXT1386_RESET_TIME);
1133 break;
1134 default:
1135 msleep(MXT_RESET_TIME);
1136 }
1137}
1138
Iiro Valkonen7686b102011-02-02 23:21:58 -08001139static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140{
1141 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001142 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -07001143 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001144 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001145 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001146 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -07001147 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001148 struct mxt_object *t9_object;
1149 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001150
Iiro Valkonen7686b102011-02-02 23:21:58 -08001151 error = mxt_get_info(data);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301152 if (error) {
1153 /* Try bootloader mode */
1154 error = mxt_switch_to_bootloader_address(data);
1155 if (error)
1156 return error;
1157
1158 error = mxt_check_bootloader(client, MXT_APP_CRC_FAIL);
1159 if (error)
1160 return error;
1161
1162 dev_err(&client->dev, "Application CRC failure\n");
1163 data->state = BOOTLOADER;
1164
1165 return 0;
1166 }
1167
1168 dev_info(&client->dev,
1169 "Family ID: %d Variant ID: %d Version: %d.%d "
1170 "Build: 0x%02X Object Num: %d\n",
1171 info->family_id, info->variant_id,
1172 info->version >> 4, info->version & 0xf,
1173 info->build, info->object_num);
1174
1175 data->state = APPMODE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176
1177 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001178 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001179 GFP_KERNEL);
1180 if (!data->object_table) {
1181 dev_err(&client->dev, "Failed to allocate memory\n");
1182 return -ENOMEM;
1183 }
1184
1185 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001186 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001187 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001188 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001189
Jing Lindc4413c2012-01-16 15:22:52 -08001190 /* Get config data from platform data */
1191 error = mxt_get_config(data);
1192 if (error)
1193 dev_dbg(&client->dev, "Config info not found.\n");
1194
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001195 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001196 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001197 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001198 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001199
Amy Maloche52262212011-09-15 16:46:57 -07001200 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -08001201 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -07001202 if (!t7_object) {
1203 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -07001204 error = -EINVAL;
1205 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -07001206 }
1207
1208 data->t7_start_addr = t7_object->start_address;
1209 error = __mxt_read_reg(client, data->t7_start_addr,
1210 T7_DATA_SIZE, data->t7_data);
1211 if (error < 0) {
1212 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -07001213 "Failed to save current power state\n");
1214 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001215 }
Iiro Valkonene8645592011-11-18 12:56:19 -08001216 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -07001217 &data->t9_ctrl);
1218 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -07001219 dev_err(&client->dev, "Failed to save current touch object\n");
1220 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001221 }
1222
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001223 /* Store T9, T15's min and max report ids */
1224 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1225 if (!t9_object) {
1226 dev_err(&client->dev, "Failed to get T9 object\n");
1227 error = -EINVAL;
1228 goto free_object_table;
1229 }
1230 data->t9_max_reportid = t9_object->max_reportid;
1231 data->t9_min_reportid = t9_object->max_reportid -
1232 t9_object->num_report_ids + 1;
1233
1234 if (data->pdata->key_codes) {
1235 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
1236 if (!t15_object)
1237 dev_dbg(&client->dev, "T15 object is not available\n");
1238 else {
1239 data->t15_max_reportid = t15_object->max_reportid;
1240 data->t15_min_reportid = t15_object->max_reportid -
1241 t15_object->num_report_ids + 1;
1242 }
1243 }
1244
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001245 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -08001246 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001247 MXT_COMMAND_BACKUPNV,
1248 MXT_BACKUP_VALUE);
1249 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001250 do {
Iiro Valkonene8645592011-11-18 12:56:19 -08001251 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001252 MXT_COMMAND_BACKUPNV,
1253 &command_register);
1254 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001255 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -07001256 usleep_range(1000, 2000);
1257 } while ((command_register != 0) && (++timeout_counter <= 100));
1258 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001259 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -07001260 error = -EIO;
1261 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001262 }
1263
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001264
1265 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -08001266 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001267 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001268
Amy Maloche7e447432011-09-14 11:36:30 -07001269 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001270
1271 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001272 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001274 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001275 info->matrix_xsize = val;
1276
Iiro Valkonen7686b102011-02-02 23:21:58 -08001277 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001278 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001279 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280 info->matrix_ysize = val;
1281
1282 dev_info(&client->dev,
Nick Dyer0a4016c2012-01-18 15:17:59 +05301283 "Matrix X Size: %d Matrix Y Size: %d\n",
1284 info->matrix_xsize, info->matrix_ysize);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001285
1286 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001287
1288free_object_table:
1289 kfree(data->object_table);
1290 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001291}
1292
Iiro Valkonen7686b102011-02-02 23:21:58 -08001293static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001294 struct device_attribute *attr, char *buf)
1295{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001296 struct mxt_data *data = dev_get_drvdata(dev);
1297 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001298 int count = 0;
1299 int i, j;
1300 int error;
1301 u8 val;
1302
1303 for (i = 0; i < data->info.object_num; i++) {
1304 object = data->object_table + i;
1305
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001306 count += snprintf(buf + count, PAGE_SIZE - count,
1307 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001308 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001309 if (count >= PAGE_SIZE)
1310 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001311
Iiro Valkonen7686b102011-02-02 23:21:58 -08001312 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001313 count += snprintf(buf + count, PAGE_SIZE - count,
1314 "\n");
1315 if (count >= PAGE_SIZE)
1316 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001317 continue;
1318 }
1319
1320 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001321 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001322 object->type, j, &val);
1323 if (error)
1324 return error;
1325
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001326 count += snprintf(buf + count, PAGE_SIZE - count,
1327 "\t[%2d]: %02x (%d)\n", j, val, val);
1328 if (count >= PAGE_SIZE)
1329 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001330 }
1331
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001332 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1333 if (count >= PAGE_SIZE)
1334 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001335 }
1336
1337 return count;
1338}
1339
Jing Lincc974cb2012-02-01 23:13:14 -08001340static int strtobyte(const char *data, u8 *value)
1341{
1342 char str[3];
1343
1344 str[0] = data[0];
1345 str[1] = data[1];
1346 str[2] = '\0';
1347
1348 return kstrtou8(str, 16, value);
1349}
1350
Iiro Valkonen7686b102011-02-02 23:21:58 -08001351static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001352{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001353 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001354 struct i2c_client *client = data->client;
1355 const struct firmware *fw = NULL;
1356 unsigned int frame_size;
Nick Dyer0a4016c2012-01-18 15:17:59 +05301357 unsigned int retry = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358 unsigned int pos = 0;
Jing Lincc974cb2012-02-01 23:13:14 -08001359 int ret, i, max_frame_size;
1360 u8 *frame;
1361
1362 switch (data->info.family_id) {
1363 case MXT224_ID:
1364 max_frame_size = MXT_SINGLE_FW_MAX_FRAME_SIZE;
1365 break;
1366 case MXT1386_ID:
1367 max_frame_size = MXT_CHIPSET_FW_MAX_FRAME_SIZE;
1368 break;
1369 default:
1370 return -EINVAL;
1371 }
1372
1373 frame = kmalloc(max_frame_size, GFP_KERNEL);
1374 if (!frame) {
1375 dev_err(dev, "Unable to allocate memory for frame data\n");
1376 return -ENOMEM;
1377 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001378
1379 ret = request_firmware(&fw, fn, dev);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301380 if (ret < 0) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001381 dev_err(dev, "Unable to open firmware %s\n", fn);
Jing Lincc974cb2012-02-01 23:13:14 -08001382 goto free_frame;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001383 }
1384
Nick Dyer0a4016c2012-01-18 15:17:59 +05301385 if (data->state != BOOTLOADER) {
1386 /* Change to the bootloader mode */
1387 mxt_write_object(data, MXT_GEN_COMMAND_T6,
1388 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
1389 mxt_reset_delay(data);
Amy Maloche7e447432011-09-14 11:36:30 -07001390
Nick Dyer0a4016c2012-01-18 15:17:59 +05301391 ret = mxt_switch_to_bootloader_address(data);
1392 if (ret)
1393 goto release_firmware;
1394 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001395
Iiro Valkonen7686b102011-02-02 23:21:58 -08001396 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301397 if (ret) {
1398 /* Bootloader may still be unlocked from previous update
1399 * attempt */
1400 ret = mxt_check_bootloader(client,
1401 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001402
Nick Dyer0a4016c2012-01-18 15:17:59 +05301403 if (ret)
1404 goto return_to_app_mode;
1405 } else {
1406 dev_info(dev, "Unlocking bootloader\n");
1407 /* Unlock bootloader */
1408 mxt_unlock_bootloader(client);
1409 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001410
1411 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001412 ret = mxt_check_bootloader(client,
1413 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001414 if (ret)
Nick Dyer0a4016c2012-01-18 15:17:59 +05301415 goto release_firmware;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001416
Jing Lincc974cb2012-02-01 23:13:14 -08001417 /* Get frame length MSB */
1418 ret = strtobyte(fw->data + pos, frame);
1419 if (ret)
1420 goto release_firmware;
1421
1422 /* Get frame length LSB */
1423 ret = strtobyte(fw->data + pos + 2, frame + 1);
1424 if (ret)
1425 goto release_firmware;
1426
1427 frame_size = ((*frame << 8) | *(frame + 1));
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001428
1429 /* We should add 2 at frame size as the the firmware data is not
1430 * included the CRC bytes.
1431 */
1432 frame_size += 2;
1433
Jing Lincc974cb2012-02-01 23:13:14 -08001434 if (frame_size > max_frame_size) {
1435 dev_err(dev, "Invalid frame size - %d\n", frame_size);
1436 ret = -EINVAL;
1437 goto release_firmware;
1438 }
1439
1440 /* Convert frame data and CRC from hex to binary */
1441 for (i = 2; i < frame_size; i++) {
1442 ret = strtobyte(fw->data + pos + i * 2, frame + i);
1443 if (ret)
1444 goto release_firmware;
1445 }
1446
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001447 /* Write one frame to device */
Jing Lincc974cb2012-02-01 23:13:14 -08001448 mxt_fw_write(client, frame, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001449
Iiro Valkonen7686b102011-02-02 23:21:58 -08001450 ret = mxt_check_bootloader(client,
1451 MXT_FRAME_CRC_PASS);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301452 if (ret) {
1453 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001454
Nick Dyer0a4016c2012-01-18 15:17:59 +05301455 /* Back off by 20ms per retry */
1456 msleep(retry * 20);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001457
Nick Dyer0a4016c2012-01-18 15:17:59 +05301458 if (retry > 20)
1459 goto release_firmware;
1460 } else {
1461 retry = 0;
Jing Lincc974cb2012-02-01 23:13:14 -08001462 pos += frame_size * 2;
1463 dev_dbg(dev, "Updated %d/%zd bytes\n", pos, fw->size);
Nick Dyer0a4016c2012-01-18 15:17:59 +05301464 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001465 }
1466
Nick Dyer0a4016c2012-01-18 15:17:59 +05301467return_to_app_mode:
1468 mxt_switch_to_appmode_address(data);
1469release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470 release_firmware(fw);
Jing Lincc974cb2012-02-01 23:13:14 -08001471free_frame:
1472 kfree(frame);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001473
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001474 return ret;
1475}
1476
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -08001477static const char *
1478mxt_search_fw_name(struct mxt_data *data, u8 bootldr_id)
1479{
1480 const struct mxt_platform_data *pdata = data->pdata;
1481 const struct mxt_config_info *cfg_info;
1482 const char *fw_name = NULL;
1483 int i;
1484
1485 for (i = 0; i < pdata->config_array_size; i++) {
1486 cfg_info = &pdata->config_array[i];
1487 if (bootldr_id == cfg_info->bootldr_id && cfg_info->fw_name) {
1488 data->config_info = cfg_info;
1489 data->info.family_id = cfg_info->family_id;
1490 fw_name = cfg_info->fw_name;
1491 }
1492 }
1493
1494 return fw_name;
1495}
1496
Iiro Valkonen7686b102011-02-02 23:21:58 -08001497static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001498 struct device_attribute *attr,
1499 const char *buf, size_t count)
1500{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001501 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001502 int error;
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -08001503 const char *fw_name;
1504 u8 bootldr_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001505
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001506 /* If fw_name is set, then the existing firmware has an upgrade */
1507 if (!data->fw_name) {
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -08001508 /*
1509 * If the device boots up in the bootloader mode, check if
1510 * there is a firmware to upgrade.
1511 */
1512 if (data->state == BOOTLOADER) {
1513 bootldr_id = mxt_get_bootloader_id(data->client);
1514 if (bootldr_id <= 0) {
1515 dev_err(dev,
1516 "Unable to retrieve bootloader id\n");
1517 return -EINVAL;
1518 }
1519 fw_name = mxt_search_fw_name(data, bootldr_id);
1520 if (fw_name == NULL) {
1521 dev_err(dev,
1522 "Unable to find fw from bootloader id\n");
1523 return -EINVAL;
1524 }
1525 } else {
1526 /* In APPMODE, if the f/w name does not exist, quit */
1527 dev_err(dev,
1528 "Firmware name not specified in platform data\n");
1529 return -EINVAL;
1530 }
1531 } else {
1532 fw_name = data->fw_name;
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001533 }
1534
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -08001535 dev_info(dev, "Upgrading the firmware file to %s\n", fw_name);
Anirudh Ghayal0bcb5b52012-02-17 14:58:25 -08001536
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001537 disable_irq(data->irq);
1538
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -08001539 error = mxt_load_fw(dev, fw_name);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001540 if (error) {
1541 dev_err(dev, "The firmware update failed(%d)\n", error);
1542 count = error;
1543 } else {
Nick Dyer0a4016c2012-01-18 15:17:59 +05301544 dev_info(dev, "The firmware update succeeded\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001545
1546 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001547 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001548
Nick Dyer0a4016c2012-01-18 15:17:59 +05301549 data->state = INIT;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001550 kfree(data->object_table);
1551 data->object_table = NULL;
Jing Lincc974cb2012-02-01 23:13:14 -08001552 data->cfg_version_idx = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001553
Iiro Valkonen7686b102011-02-02 23:21:58 -08001554 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001555 }
1556
Nick Dyer0a4016c2012-01-18 15:17:59 +05301557 if (data->state == APPMODE) {
1558 enable_irq(data->irq);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001559
Nick Dyer0a4016c2012-01-18 15:17:59 +05301560 error = mxt_make_highchg(data);
1561 if (error)
1562 return error;
1563 }
Iiro Valkonen08960a02011-04-12 23:16:40 -07001564
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001565 return count;
1566}
1567
Iiro Valkonen7686b102011-02-02 23:21:58 -08001568static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1569static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001570
Iiro Valkonen7686b102011-02-02 23:21:58 -08001571static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001572 &dev_attr_object.attr,
1573 &dev_attr_update_fw.attr,
1574 NULL
1575};
1576
Iiro Valkonen7686b102011-02-02 23:21:58 -08001577static const struct attribute_group mxt_attr_group = {
1578 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001579};
1580
Amy Maloche52262212011-09-15 16:46:57 -07001581static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001582{
Jing Lin36aee812011-10-17 17:17:28 -07001583 int error;
1584
Amy Maloche52262212011-09-15 16:46:57 -07001585 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001586 error = __mxt_write_reg(data->client, data->t7_start_addr,
1587 T7_DATA_SIZE, data->t7_data);
1588 if (error < 0) {
1589 dev_err(&data->client->dev,
1590 "failed to restore old power state\n");
1591 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001592 }
Jing Lin36aee812011-10-17 17:17:28 -07001593
Amy Maloche52262212011-09-15 16:46:57 -07001594 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001595 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001596 if (error < 0) {
1597 dev_err(&data->client->dev, "failed to restore touch\n");
1598 return error;
1599 }
1600
1601 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001602}
1603
Amy Maloche52262212011-09-15 16:46:57 -07001604static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001605{
Jing Lin36aee812011-10-17 17:17:28 -07001606 int error;
1607 u8 t7_data[T7_DATA_SIZE] = {0};
1608
1609 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001610 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001611 if (error < 0) {
1612 dev_err(&data->client->dev, "failed to disable touch\n");
1613 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001614 }
1615
Jing Lin36aee812011-10-17 17:17:28 -07001616 error = __mxt_write_reg(data->client, data->t7_start_addr,
1617 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001618 if (error < 0) {
1619 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001620 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001621 return error;
1622 }
1623
1624 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001625}
1626
Iiro Valkonen7686b102011-02-02 23:21:58 -08001627static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001628{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001629 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001630 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001631
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -08001632 if (data->state == APPMODE) {
1633 error = mxt_start(data);
1634 if (error < 0) {
1635 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1636 return error;
1637 }
Amy Maloche52262212011-09-15 16:46:57 -07001638 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001639
1640 return 0;
1641}
1642
Iiro Valkonen7686b102011-02-02 23:21:58 -08001643static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001644{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001645 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001646 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001647
Anirudh Ghayal6c3b37b2012-02-23 18:39:42 -08001648 if (data->state == APPMODE) {
1649 error = mxt_stop(data);
1650 if (error < 0)
1651 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1652 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001653}
1654
Amy Malochec331f842012-01-24 10:33:47 -08001655static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1656{
1657 return (regulator_count_voltages(reg) > 0) ?
1658 regulator_set_optimum_mode(reg, load_uA) : 0;
1659}
1660
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301661static int mxt_power_on(struct mxt_data *data, bool on)
1662{
1663 int rc;
1664
1665 if (on == false)
1666 goto power_off;
1667
Amy Malochec331f842012-01-24 10:33:47 -08001668 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301669 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001670 dev_err(&data->client->dev,
1671 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301672 return rc;
1673 }
1674
Amy Maloche21115eb2011-11-02 09:04:37 -07001675 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301676 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001677 dev_err(&data->client->dev,
1678 "Regulator vcc_ana enable failed rc=%d\n", rc);
1679 goto error_reg_en_vcc_ana;
1680 }
1681
1682 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001683 rc = reg_set_optimum_mode_check(data->vcc_dig,
1684 MXT_ACTIVE_LOAD_DIG_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001685 if (rc < 0) {
1686 dev_err(&data->client->dev,
1687 "Regulator vcc_dig set_opt failed rc=%d\n",
1688 rc);
1689 goto error_reg_opt_vcc_dig;
1690 }
1691
1692 rc = regulator_enable(data->vcc_dig);
1693 if (rc) {
1694 dev_err(&data->client->dev,
1695 "Regulator vcc_dig enable failed rc=%d\n", rc);
1696 goto error_reg_en_vcc_dig;
1697 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301698 }
1699
1700 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001701 rc = reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301702 if (rc < 0) {
1703 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001704 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301705 goto error_reg_opt_i2c;
1706 }
1707
1708 rc = regulator_enable(data->vcc_i2c);
1709 if (rc) {
1710 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001711 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301712 goto error_reg_en_vcc_i2c;
1713 }
1714 }
1715
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001716 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301717
1718 return 0;
1719
1720error_reg_en_vcc_i2c:
1721 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001722 reg_set_optimum_mode_check(data->vcc_i2c, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301723error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001724 if (data->pdata->digital_pwr_regulator)
1725 regulator_disable(data->vcc_dig);
1726error_reg_en_vcc_dig:
1727 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001728 reg_set_optimum_mode_check(data->vcc_dig, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001729error_reg_opt_vcc_dig:
1730 regulator_disable(data->vcc_ana);
1731error_reg_en_vcc_ana:
Amy Malochec331f842012-01-24 10:33:47 -08001732 reg_set_optimum_mode_check(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301733 return rc;
1734
1735power_off:
Amy Malochec331f842012-01-24 10:33:47 -08001736 reg_set_optimum_mode_check(data->vcc_ana, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001737 regulator_disable(data->vcc_ana);
1738 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001739 reg_set_optimum_mode_check(data->vcc_dig, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001740 regulator_disable(data->vcc_dig);
1741 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301742 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001743 reg_set_optimum_mode_check(data->vcc_i2c, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301744 regulator_disable(data->vcc_i2c);
1745 }
1746 msleep(50);
1747 return 0;
1748}
1749
1750static int mxt_regulator_configure(struct mxt_data *data, bool on)
1751{
1752 int rc;
1753
1754 if (on == false)
1755 goto hw_shutdown;
1756
Amy Maloche21115eb2011-11-02 09:04:37 -07001757 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1758 if (IS_ERR(data->vcc_ana)) {
1759 rc = PTR_ERR(data->vcc_ana);
1760 dev_err(&data->client->dev,
1761 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301762 return rc;
1763 }
1764
Amy Maloche21115eb2011-11-02 09:04:37 -07001765 if (regulator_count_voltages(data->vcc_ana) > 0) {
1766 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301767 MXT_VTG_MAX_UV);
1768 if (rc) {
1769 dev_err(&data->client->dev,
1770 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001771 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301772 }
1773 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001774 if (data->pdata->digital_pwr_regulator) {
1775 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1776 if (IS_ERR(data->vcc_dig)) {
1777 rc = PTR_ERR(data->vcc_dig);
1778 dev_err(&data->client->dev,
1779 "Regulator get dig failed rc=%d\n", rc);
1780 goto error_get_vtg_vcc_dig;
1781 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301782
Amy Maloche21115eb2011-11-02 09:04:37 -07001783 if (regulator_count_voltages(data->vcc_dig) > 0) {
1784 rc = regulator_set_voltage(data->vcc_dig,
1785 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1786 if (rc) {
1787 dev_err(&data->client->dev,
1788 "regulator set_vtg failed rc=%d\n", rc);
1789 goto error_set_vtg_vcc_dig;
1790 }
1791 }
1792 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301793 if (data->pdata->i2c_pull_up) {
1794 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1795 if (IS_ERR(data->vcc_i2c)) {
1796 rc = PTR_ERR(data->vcc_i2c);
1797 dev_err(&data->client->dev,
1798 "Regulator get failed rc=%d\n", rc);
1799 goto error_get_vtg_i2c;
1800 }
1801 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1802 rc = regulator_set_voltage(data->vcc_i2c,
1803 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1804 if (rc) {
1805 dev_err(&data->client->dev,
1806 "regulator set_vtg failed rc=%d\n", rc);
1807 goto error_set_vtg_i2c;
1808 }
1809 }
1810 }
1811
1812 return 0;
1813
1814error_set_vtg_i2c:
1815 regulator_put(data->vcc_i2c);
1816error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001817 if (data->pdata->digital_pwr_regulator)
1818 if (regulator_count_voltages(data->vcc_dig) > 0)
1819 regulator_set_voltage(data->vcc_dig, 0,
1820 MXT_VTG_DIG_MAX_UV);
1821error_set_vtg_vcc_dig:
1822 if (data->pdata->digital_pwr_regulator)
1823 regulator_put(data->vcc_dig);
1824error_get_vtg_vcc_dig:
1825 if (regulator_count_voltages(data->vcc_ana) > 0)
1826 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1827error_set_vtg_vcc_ana:
1828 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301829 return rc;
1830
1831hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001832 if (regulator_count_voltages(data->vcc_ana) > 0)
1833 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1834 regulator_put(data->vcc_ana);
1835 if (data->pdata->digital_pwr_regulator) {
1836 if (regulator_count_voltages(data->vcc_dig) > 0)
1837 regulator_set_voltage(data->vcc_dig, 0,
1838 MXT_VTG_DIG_MAX_UV);
1839 regulator_put(data->vcc_dig);
1840 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301841 if (data->pdata->i2c_pull_up) {
1842 if (regulator_count_voltages(data->vcc_i2c) > 0)
1843 regulator_set_voltage(data->vcc_i2c, 0,
1844 MXT_I2C_VTG_MAX_UV);
1845 regulator_put(data->vcc_i2c);
1846 }
1847 return 0;
1848}
1849
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301850#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001851static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1852{
1853
1854 int rc;
1855
1856 if (on == false)
1857 goto regulator_hpm;
1858
Amy Malochec331f842012-01-24 10:33:47 -08001859 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001860 if (rc < 0) {
1861 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001862 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001863 goto fail_regulator_lpm;
1864 }
1865
Amy Maloche21115eb2011-11-02 09:04:37 -07001866 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001867 rc = reg_set_optimum_mode_check(data->vcc_dig,
Amy Maloche21115eb2011-11-02 09:04:37 -07001868 MXT_LPM_LOAD_DIG_UA);
1869 if (rc < 0) {
1870 dev_err(&data->client->dev,
1871 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1872 goto fail_regulator_lpm;
1873 }
1874 }
1875
Jing Linbace50b2011-10-18 22:55:47 -07001876 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001877 rc = reg_set_optimum_mode_check(data->vcc_i2c,
Jing Linbace50b2011-10-18 22:55:47 -07001878 MXT_I2C_LPM_LOAD_UA);
1879 if (rc < 0) {
1880 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001881 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001882 goto fail_regulator_lpm;
1883 }
1884 }
1885
1886 return 0;
1887
1888regulator_hpm:
1889
Amy Malochec331f842012-01-24 10:33:47 -08001890 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001891 if (rc < 0) {
1892 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001893 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001894 goto fail_regulator_hpm;
1895 }
1896
Amy Maloche21115eb2011-11-02 09:04:37 -07001897 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001898 rc = reg_set_optimum_mode_check(data->vcc_dig,
Amy Maloche21115eb2011-11-02 09:04:37 -07001899 MXT_ACTIVE_LOAD_DIG_UA);
1900 if (rc < 0) {
1901 dev_err(&data->client->dev,
1902 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1903 goto fail_regulator_hpm;
1904 }
1905 }
1906
Jing Linbace50b2011-10-18 22:55:47 -07001907 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001908 rc = reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001909 if (rc < 0) {
1910 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001911 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001912 goto fail_regulator_hpm;
1913 }
1914 }
1915
1916 return 0;
1917
1918fail_regulator_lpm:
Amy Malochec331f842012-01-24 10:33:47 -08001919 reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001920 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001921 reg_set_optimum_mode_check(data->vcc_dig,
1922 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001923 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001924 reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001925
1926 return rc;
1927
1928fail_regulator_hpm:
Amy Malochec331f842012-01-24 10:33:47 -08001929 reg_set_optimum_mode_check(data->vcc_ana, MXT_LPM_LOAD_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001930 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001931 reg_set_optimum_mode_check(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001932 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001933 reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001934
1935 return rc;
1936}
1937
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301938static int mxt_suspend(struct device *dev)
1939{
1940 struct i2c_client *client = to_i2c_client(dev);
1941 struct mxt_data *data = i2c_get_clientdata(client);
1942 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001943 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301944
1945 mutex_lock(&input_dev->mutex);
1946
Amy Maloche52262212011-09-15 16:46:57 -07001947 if (input_dev->users) {
1948 error = mxt_stop(data);
1949 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001950 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001951 mutex_unlock(&input_dev->mutex);
1952 return error;
1953 }
1954
1955 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301956
1957 mutex_unlock(&input_dev->mutex);
1958
Jing Linbace50b2011-10-18 22:55:47 -07001959 /* put regulators in low power mode */
1960 error = mxt_regulator_lpm(data, true);
1961 if (error < 0) {
1962 dev_err(dev, "failed to enter low power mode\n");
1963 return error;
1964 }
1965
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301966 return 0;
1967}
1968
1969static int mxt_resume(struct device *dev)
1970{
1971 struct i2c_client *client = to_i2c_client(dev);
1972 struct mxt_data *data = i2c_get_clientdata(client);
1973 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001974 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301975
Jing Linbace50b2011-10-18 22:55:47 -07001976 /* put regulators in high power mode */
1977 error = mxt_regulator_lpm(data, false);
1978 if (error < 0) {
1979 dev_err(dev, "failed to enter high power mode\n");
1980 return error;
1981 }
1982
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301983 mutex_lock(&input_dev->mutex);
1984
Amy Maloche52262212011-09-15 16:46:57 -07001985 if (input_dev->users) {
1986 error = mxt_start(data);
1987 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001988 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001989 mutex_unlock(&input_dev->mutex);
1990 return error;
1991 }
1992 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301993
1994 mutex_unlock(&input_dev->mutex);
1995
1996 return 0;
1997}
1998
1999#if defined(CONFIG_HAS_EARLYSUSPEND)
2000static void mxt_early_suspend(struct early_suspend *h)
2001{
2002 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
2003
2004 mxt_suspend(&data->client->dev);
2005}
2006
2007static void mxt_late_resume(struct early_suspend *h)
2008{
2009 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
2010
2011 mxt_resume(&data->client->dev);
2012}
2013#endif
2014
2015static const struct dev_pm_ops mxt_pm_ops = {
2016#ifndef CONFIG_HAS_EARLYSUSPEND
2017 .suspend = mxt_suspend,
2018 .resume = mxt_resume,
2019#endif
2020};
2021#endif
2022
Jing Lin6cfc00e2011-11-02 15:15:30 -07002023static int mxt_debugfs_object_show(struct seq_file *m, void *v)
2024{
2025 struct mxt_data *data = m->private;
2026 struct mxt_object *object;
2027 struct device *dev = &data->client->dev;
2028 int i, j, k;
2029 int error;
2030 int obj_size;
2031 u8 val;
2032
2033 for (i = 0; i < data->info.object_num; i++) {
2034 object = data->object_table + i;
2035 obj_size = object->size + 1;
2036
2037 seq_printf(m, "Object[%d] (Type %d)\n", i + 1, object->type);
2038
2039 for (j = 0; j < object->instances + 1; j++) {
2040 seq_printf(m, "[Instance %d]\n", j);
2041
2042 for (k = 0; k < obj_size; k++) {
2043 error = mxt_read_object(data, object->type,
2044 j * obj_size + k, &val);
2045 if (error) {
2046 dev_err(dev,
2047 "Failed to read object %d "
2048 "instance %d at offset %d\n",
2049 object->type, j, k);
2050 return error;
2051 }
2052
2053 seq_printf(m, "Byte %d: 0x%02x (%d)\n",
2054 k, val, val);
2055 }
2056 }
2057 }
2058
2059 return 0;
2060}
2061
2062static int mxt_debugfs_object_open(struct inode *inode, struct file *file)
2063{
2064 return single_open(file, mxt_debugfs_object_show, inode->i_private);
2065}
2066
2067static const struct file_operations mxt_object_fops = {
2068 .owner = THIS_MODULE,
2069 .open = mxt_debugfs_object_open,
2070 .read = seq_read,
2071 .release = single_release,
2072};
2073
2074static void __init mxt_debugfs_init(struct mxt_data *data)
2075{
2076 debug_base = debugfs_create_dir(MXT_DEBUGFS_DIR, NULL);
2077 if (IS_ERR_OR_NULL(debug_base))
2078 pr_err("atmel_mxt_ts: Failed to create debugfs dir\n");
2079 if (IS_ERR_OR_NULL(debugfs_create_file(MXT_DEBUGFS_FILE,
2080 0444,
2081 debug_base,
2082 data,
2083 &mxt_object_fops))) {
2084 pr_err("atmel_mxt_ts: Failed to create object file\n");
2085 debugfs_remove_recursive(debug_base);
2086 }
2087}
2088
Iiro Valkonen7686b102011-02-02 23:21:58 -08002089static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002090 const struct i2c_device_id *id)
2091{
Iiro Valkonen919ed892011-02-15 13:36:52 -08002092 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08002093 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002094 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08002095 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002096
Iiro Valkonen919ed892011-02-15 13:36:52 -08002097 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002098 return -EINVAL;
2099
Iiro Valkonen7686b102011-02-02 23:21:58 -08002100 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002101 input_dev = input_allocate_device();
2102 if (!data || !input_dev) {
2103 dev_err(&client->dev, "Failed to allocate memory\n");
2104 error = -ENOMEM;
2105 goto err_free_mem;
2106 }
2107
Nick Dyer0a4016c2012-01-18 15:17:59 +05302108 data->state = INIT;
Amy Maloche83c385a2012-02-01 10:32:03 +05302109 input_dev->name = "atmel_mxt_ts";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002110 input_dev->id.bustype = BUS_I2C;
2111 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08002112 input_dev->open = mxt_input_open;
2113 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002114
Joonyoung Shim910d8052011-04-12 23:14:38 -07002115 data->client = client;
2116 data->input_dev = input_dev;
2117 data->pdata = pdata;
2118 data->irq = client->irq;
2119
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002120 __set_bit(EV_ABS, input_dev->evbit);
2121 __set_bit(EV_KEY, input_dev->evbit);
2122 __set_bit(BTN_TOUCH, input_dev->keybit);
2123
2124 /* For single touch */
2125 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07002126 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002127 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07002128 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07002129 input_set_abs_params(input_dev, ABS_PRESSURE,
2130 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002131
2132 /* For multi touch */
Joonyoung Shimcd473222012-02-14 18:32:48 -08002133 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002134 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08002135 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002136 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07002137 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002138 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07002139 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07002140 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
2141 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002142
Amy Maloche83c385a2012-02-01 10:32:03 +05302143 if (pdata->touch_x_size)
2144 data->touch_x_size = pdata->touch_x_size;
2145 else
2146 data->touch_x_size = pdata->x_size;
2147
2148 if (pdata->touch_y_size)
2149 data->touch_y_size = pdata->touch_y_size;
2150 else
2151 data->touch_y_size = pdata->y_size;
2152
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08002153 /* set key array supported keys */
2154 if (pdata->key_codes) {
2155 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
2156 if (pdata->key_codes[i])
2157 input_set_capability(input_dev, EV_KEY,
2158 pdata->key_codes[i]);
2159 }
2160 }
2161
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002162 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002163 i2c_set_clientdata(client, data);
2164
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302165 if (pdata->init_hw)
2166 error = pdata->init_hw(true);
2167 else
2168 error = mxt_regulator_configure(data, true);
2169 if (error) {
2170 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07002171 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302172 }
2173
2174 if (pdata->power_on)
2175 error = pdata->power_on(true);
2176 else
2177 error = mxt_power_on(data, true);
2178 if (error) {
2179 dev_err(&client->dev, "Failed to power on hardware\n");
2180 goto err_regulator_on;
2181 }
2182
Amy Maloche08266db2011-11-04 11:07:16 -07002183 if (gpio_is_valid(pdata->irq_gpio)) {
2184 /* configure touchscreen irq gpio */
2185 error = gpio_request(pdata->irq_gpio,
2186 "mxt_irq_gpio");
2187 if (error) {
2188 pr_err("%s: unable to request gpio [%d]\n", __func__,
2189 pdata->irq_gpio);
2190 goto err_power_on;
2191 }
2192 error = gpio_direction_input(pdata->irq_gpio);
2193 if (error) {
2194 pr_err("%s: unable to set_direction for gpio [%d]\n",
2195 __func__, pdata->irq_gpio);
2196 goto err_irq_gpio_req;
2197 }
2198 }
2199
2200 if (gpio_is_valid(pdata->reset_gpio)) {
2201 /* configure touchscreen reset out gpio */
2202 error = gpio_request(pdata->reset_gpio,
2203 "mxt_reset_gpio");
2204 if (error) {
2205 pr_err("%s: unable to request reset gpio %d\n",
2206 __func__, pdata->reset_gpio);
2207 goto err_irq_gpio_req;
2208 }
2209
2210 error = gpio_direction_output(
2211 pdata->reset_gpio, 1);
2212 if (error) {
2213 pr_err("%s: unable to set direction for gpio %d\n",
2214 __func__, pdata->reset_gpio);
2215 goto err_reset_gpio_req;
2216 }
2217 }
2218
2219 mxt_reset_delay(data);
2220
Iiro Valkonen7686b102011-02-02 23:21:58 -08002221 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002222 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07002223 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002224
Iiro Valkonen7686b102011-02-02 23:21:58 -08002225 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08002226 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002227 if (error) {
2228 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07002229 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002230 }
2231
Nick Dyer0a4016c2012-01-18 15:17:59 +05302232 if (data->state == APPMODE) {
2233 error = mxt_make_highchg(data);
2234 if (error) {
2235 dev_err(&client->dev, "Failed to make high CHG\n");
2236 goto err_free_irq;
2237 }
2238 }
Iiro Valkonen08960a02011-04-12 23:16:40 -07002239
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002240 error = input_register_device(input_dev);
2241 if (error)
2242 goto err_free_irq;
2243
Iiro Valkonen7686b102011-02-02 23:21:58 -08002244 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002245 if (error)
2246 goto err_unregister_device;
2247
Anirudh Ghayal253ce122011-08-09 19:32:57 +05302248#if defined(CONFIG_HAS_EARLYSUSPEND)
2249 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
2250 MXT_SUSPEND_LEVEL;
2251 data->early_suspend.suspend = mxt_early_suspend;
2252 data->early_suspend.resume = mxt_late_resume;
2253 register_early_suspend(&data->early_suspend);
2254#endif
2255
Jing Lin6cfc00e2011-11-02 15:15:30 -07002256 mxt_debugfs_init(data);
2257
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002258 return 0;
2259
2260err_unregister_device:
2261 input_unregister_device(input_dev);
2262 input_dev = NULL;
2263err_free_irq:
2264 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07002265err_free_object:
2266 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07002267err_reset_gpio_req:
2268 if (gpio_is_valid(pdata->reset_gpio))
2269 gpio_free(pdata->reset_gpio);
2270err_irq_gpio_req:
2271 if (gpio_is_valid(pdata->irq_gpio))
2272 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302273err_power_on:
2274 if (pdata->power_on)
2275 pdata->power_on(false);
2276 else
2277 mxt_power_on(data, false);
2278err_regulator_on:
2279 if (pdata->init_hw)
2280 pdata->init_hw(false);
2281 else
2282 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002283err_free_mem:
2284 input_free_device(input_dev);
2285 kfree(data);
2286 return error;
2287}
2288
Iiro Valkonen7686b102011-02-02 23:21:58 -08002289static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002290{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002291 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002292
Iiro Valkonen7686b102011-02-02 23:21:58 -08002293 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002294 free_irq(data->irq, data);
2295 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05302296#if defined(CONFIG_HAS_EARLYSUSPEND)
2297 unregister_early_suspend(&data->early_suspend);
2298#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302299
2300 if (data->pdata->power_on)
2301 data->pdata->power_on(false);
2302 else
2303 mxt_power_on(data, false);
2304
2305 if (data->pdata->init_hw)
2306 data->pdata->init_hw(false);
2307 else
2308 mxt_regulator_configure(data, false);
2309
Mohan Pallakabfe8f302012-01-02 18:32:08 +08002310 if (gpio_is_valid(data->pdata->reset_gpio))
2311 gpio_free(data->pdata->reset_gpio);
2312
2313 if (gpio_is_valid(data->pdata->irq_gpio))
2314 gpio_free(data->pdata->irq_gpio);
2315
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002316 kfree(data->object_table);
2317 kfree(data);
2318
Jing Lin6cfc00e2011-11-02 15:15:30 -07002319 debugfs_remove_recursive(debug_base);
2320
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002321 return 0;
2322}
2323
Iiro Valkonen7686b102011-02-02 23:21:58 -08002324static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002325 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002326 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08002327 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002328 { }
2329};
Iiro Valkonen7686b102011-02-02 23:21:58 -08002330MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002331
Iiro Valkonen7686b102011-02-02 23:21:58 -08002332static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002333 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08002334 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002335 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002336#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08002337 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002338#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002339 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002340 .probe = mxt_probe,
2341 .remove = __devexit_p(mxt_remove),
2342 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002343};
2344
Iiro Valkonen7686b102011-02-02 23:21:58 -08002345static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002346{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002347 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002348}
2349
Iiro Valkonen7686b102011-02-02 23:21:58 -08002350static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002351{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002352 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002353}
2354
Iiro Valkonen7686b102011-02-02 23:21:58 -08002355module_init(mxt_init);
2356module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002357
2358/* Module information */
2359MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08002360MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002361MODULE_LICENSE("GPL");