blob: 4e815679238a1150f5e629f97e48f24b1b92102d [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>
Amy Maloche2b59bab2011-10-13 16:08:16 -070021#include <linux/input.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070022#include <linux/interrupt.h>
23#include <linux/slab.h>
Amy Maloche08266db2011-11-04 11:07:16 -070024#include <linux/gpio.h>
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>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070028
Anirudh Ghayal253ce122011-08-09 19:32:57 +053029#if defined(CONFIG_HAS_EARLYSUSPEND)
30#include <linux/earlysuspend.h>
31/* Early-suspend level */
32#define MXT_SUSPEND_LEVEL 1
33#endif
34
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070035/* Family ID */
36#define MXT224_ID 0x80
Amy Maloche380cc0b2011-11-03 12:55:04 -070037#define MXT224E_ID 0x81
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070038#define MXT1386_ID 0xA0
39
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070040/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080041#define MXT_VER_20 20
42#define MXT_VER_21 21
43#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070044
45/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080046#define MXT_APP_LOW 0x4a
47#define MXT_APP_HIGH 0x4b
48#define MXT_BOOT_LOW 0x24
49#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070050
51/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080052#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070053
54/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080055#define MXT_FAMILY_ID 0x00
56#define MXT_VARIANT_ID 0x01
57#define MXT_VERSION 0x02
58#define MXT_BUILD 0x03
59#define MXT_MATRIX_X_SIZE 0x04
60#define MXT_MATRIX_Y_SIZE 0x05
61#define MXT_OBJECT_NUM 0x06
62#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070063
Iiro Valkonen7686b102011-02-02 23:21:58 -080064#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070065
66/* Object types */
Iiro Valkonene8645592011-11-18 12:56:19 -080067#define MXT_DEBUG_DIAGNOSTIC_T37 37
68#define MXT_GEN_MESSAGE_T5 5
69#define MXT_GEN_COMMAND_T6 6
70#define MXT_GEN_POWER_T7 7
71#define MXT_GEN_ACQUIRE_T8 8
72#define MXT_GEN_DATASOURCE_T53 53
73#define MXT_TOUCH_MULTI_T9 9
74#define MXT_TOUCH_KEYARRAY_T15 15
75#define MXT_TOUCH_PROXIMITY_T23 23
76#define MXT_TOUCH_PROXKEY_T52 52
77#define MXT_PROCI_GRIPFACE_T20 20
78#define MXT_PROCG_NOISE_T22 22
79#define MXT_PROCI_ONETOUCH_T24 24
80#define MXT_PROCI_TWOTOUCH_T27 27
81#define MXT_PROCI_GRIP_T40 40
82#define MXT_PROCI_PALM_T41 41
83#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
84#define MXT_PROCI_STYLUS_T47 47
Jing Linc7fc4052011-12-21 16:16:19 -080085#define MXT_PROCI_SHIELDLESS_T56 56
Iiro Valkonene8645592011-11-18 12:56:19 -080086#define MXT_PROCG_NOISESUPPRESSION_T48 48
87#define MXT_SPT_COMMSCONFIG_T18 18
88#define MXT_SPT_GPIOPWM_T19 19
89#define MXT_SPT_SELFTEST_T25 25
90#define MXT_SPT_CTECONFIG_T28 28
91#define MXT_SPT_USERDATA_T38 38
92#define MXT_SPT_DIGITIZER_T43 43
93#define MXT_SPT_MESSAGECOUNT_T44 44
94#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070095
Iiro Valkonene8645592011-11-18 12:56:19 -080096/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080097#define MXT_COMMAND_RESET 0
98#define MXT_COMMAND_BACKUPNV 1
99#define MXT_COMMAND_CALIBRATE 2
100#define MXT_COMMAND_REPORTALL 3
101#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700102
Iiro Valkonene8645592011-11-18 12:56:19 -0800103/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800104#define MXT_POWER_IDLEACQINT 0
105#define MXT_POWER_ACTVACQINT 1
106#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700107
Iiro Valkonene8645592011-11-18 12:56:19 -0800108/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800109#define MXT_ACQUIRE_CHRGTIME 0
110#define MXT_ACQUIRE_TCHDRIFT 2
111#define MXT_ACQUIRE_DRIFTST 3
112#define MXT_ACQUIRE_TCHAUTOCAL 4
113#define MXT_ACQUIRE_SYNC 5
114#define MXT_ACQUIRE_ATCHCALST 6
115#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700116
Iiro Valkonene8645592011-11-18 12:56:19 -0800117/* MXT_TOUCH_MULT_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800118#define MXT_TOUCH_CTRL 0
119#define MXT_TOUCH_XORIGIN 1
120#define MXT_TOUCH_YORIGIN 2
121#define MXT_TOUCH_XSIZE 3
122#define MXT_TOUCH_YSIZE 4
123#define MXT_TOUCH_BLEN 6
124#define MXT_TOUCH_TCHTHR 7
125#define MXT_TOUCH_TCHDI 8
126#define MXT_TOUCH_ORIENT 9
127#define MXT_TOUCH_MOVHYSTI 11
128#define MXT_TOUCH_MOVHYSTN 12
129#define MXT_TOUCH_NUMTOUCH 14
130#define MXT_TOUCH_MRGHYST 15
131#define MXT_TOUCH_MRGTHR 16
132#define MXT_TOUCH_AMPHYST 17
133#define MXT_TOUCH_XRANGE_LSB 18
134#define MXT_TOUCH_XRANGE_MSB 19
135#define MXT_TOUCH_YRANGE_LSB 20
136#define MXT_TOUCH_YRANGE_MSB 21
137#define MXT_TOUCH_XLOCLIP 22
138#define MXT_TOUCH_XHICLIP 23
139#define MXT_TOUCH_YLOCLIP 24
140#define MXT_TOUCH_YHICLIP 25
141#define MXT_TOUCH_XEDGECTRL 26
142#define MXT_TOUCH_XEDGEDIST 27
143#define MXT_TOUCH_YEDGECTRL 28
144#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700145#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700146
Iiro Valkonene8645592011-11-18 12:56:19 -0800147/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800148#define MXT_GRIPFACE_CTRL 0
149#define MXT_GRIPFACE_XLOGRIP 1
150#define MXT_GRIPFACE_XHIGRIP 2
151#define MXT_GRIPFACE_YLOGRIP 3
152#define MXT_GRIPFACE_YHIGRIP 4
153#define MXT_GRIPFACE_MAXTCHS 5
154#define MXT_GRIPFACE_SZTHR1 7
155#define MXT_GRIPFACE_SZTHR2 8
156#define MXT_GRIPFACE_SHPTHR1 9
157#define MXT_GRIPFACE_SHPTHR2 10
158#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700159
Iiro Valkonen7686b102011-02-02 23:21:58 -0800160/* MXT_PROCI_NOISE field */
161#define MXT_NOISE_CTRL 0
162#define MXT_NOISE_OUTFLEN 1
163#define MXT_NOISE_GCAFUL_LSB 3
164#define MXT_NOISE_GCAFUL_MSB 4
165#define MXT_NOISE_GCAFLL_LSB 5
166#define MXT_NOISE_GCAFLL_MSB 6
167#define MXT_NOISE_ACTVGCAFVALID 7
168#define MXT_NOISE_NOISETHR 8
169#define MXT_NOISE_FREQHOPSCALE 10
170#define MXT_NOISE_FREQ0 11
171#define MXT_NOISE_FREQ1 12
172#define MXT_NOISE_FREQ2 13
173#define MXT_NOISE_FREQ3 14
174#define MXT_NOISE_FREQ4 15
175#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700176
Iiro Valkonene8645592011-11-18 12:56:19 -0800177/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_COMMS_CTRL 0
179#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700180
Iiro Valkonene8645592011-11-18 12:56:19 -0800181/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800182#define MXT_CTE_CTRL 0
183#define MXT_CTE_CMD 1
184#define MXT_CTE_MODE 2
185#define MXT_CTE_IDLEGCAFDEPTH 3
186#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700187#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700188
Iiro Valkonen7686b102011-02-02 23:21:58 -0800189#define MXT_VOLTAGE_DEFAULT 2700000
190#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700191
Amy Maloche21115eb2011-11-02 09:04:37 -0700192/* Analog voltage @2.7 V */
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530193#define MXT_VTG_MIN_UV 2700000
194#define MXT_VTG_MAX_UV 3300000
195#define MXT_ACTIVE_LOAD_UA 15000
Jing Linbace50b2011-10-18 22:55:47 -0700196#define MXT_LPM_LOAD_UA 10
Amy Maloche21115eb2011-11-02 09:04:37 -0700197/* Digital voltage @1.8 V */
198#define MXT_VTG_DIG_MIN_UV 1800000
199#define MXT_VTG_DIG_MAX_UV 1800000
200#define MXT_ACTIVE_LOAD_DIG_UA 10000
201#define MXT_LPM_LOAD_DIG_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530202
203#define MXT_I2C_VTG_MIN_UV 1800000
204#define MXT_I2C_VTG_MAX_UV 1800000
205#define MXT_I2C_LOAD_UA 10000
Jing Linbace50b2011-10-18 22:55:47 -0700206#define MXT_I2C_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530207
Iiro Valkonene8645592011-11-18 12:56:19 -0800208/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800209#define MXT_BOOT_VALUE 0xa5
210#define MXT_BACKUP_VALUE 0x55
211#define MXT_BACKUP_TIME 25 /* msec */
Jing Linc7fc4052011-12-21 16:16:19 -0800212#define MXT224_RESET_TIME 65 /* msec */
213#define MXT224E_RESET_TIME 22 /* msec */
214#define MXT1386_RESET_TIME 250 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700215#define MXT_RESET_TIME 250 /* msec */
Jing Linc7fc4052011-12-21 16:16:19 -0800216#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219
Jing Lin36aee812011-10-17 17:17:28 -0700220#define MXT_WAKE_TIME 25
221
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700222/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800223#define MXT_UNLOCK_CMD_MSB 0xaa
224#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700225
226/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800227#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
228#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
229#define MXT_FRAME_CRC_CHECK 0x02
230#define MXT_FRAME_CRC_FAIL 0x03
231#define MXT_FRAME_CRC_PASS 0x04
232#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
233#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700234
235/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800236#define MXT_SUPPRESS (1 << 1)
237#define MXT_AMP (1 << 2)
238#define MXT_VECTOR (1 << 3)
239#define MXT_MOVE (1 << 4)
240#define MXT_RELEASE (1 << 5)
241#define MXT_PRESS (1 << 6)
242#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700243
Joonyoung Shim910d8052011-04-12 23:14:38 -0700244/* Touch orient bits */
245#define MXT_XY_SWITCH (1 << 0)
246#define MXT_X_INVERT (1 << 1)
247#define MXT_Y_INVERT (1 << 2)
248
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700249/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800250#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700251
Iiro Valkonen7686b102011-02-02 23:21:58 -0800252#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700253
Jing Lin36aee812011-10-17 17:17:28 -0700254#define T7_DATA_SIZE 3
255#define MXT_MAX_RW_TRIES 3
256#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530257
Jing Lin6cfc00e2011-11-02 15:15:30 -0700258#define MXT_DEBUGFS_DIR "atmel_mxt_ts"
259#define MXT_DEBUGFS_FILE "object"
260
Iiro Valkonen7686b102011-02-02 23:21:58 -0800261struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700262 u8 family_id;
263 u8 variant_id;
264 u8 version;
265 u8 build;
266 u8 matrix_xsize;
267 u8 matrix_ysize;
268 u8 object_num;
269};
270
Iiro Valkonen7686b102011-02-02 23:21:58 -0800271struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700272 u8 type;
273 u16 start_address;
274 u8 size;
275 u8 instances;
276 u8 num_report_ids;
277
278 /* to map object and message */
279 u8 max_reportid;
280};
281
Iiro Valkonen7686b102011-02-02 23:21:58 -0800282struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700283 u8 reportid;
284 u8 message[7];
285 u8 checksum;
286};
287
Iiro Valkonen7686b102011-02-02 23:21:58 -0800288struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700289 int status;
290 int x;
291 int y;
292 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700293 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700294};
295
296/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800297struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700298 struct i2c_client *client;
299 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800300 const struct mxt_platform_data *pdata;
Jing Lindc4413c2012-01-16 15:22:52 -0800301 const struct mxt_config_info *config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800302 struct mxt_object *object_table;
303 struct mxt_info info;
304 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700305 unsigned int irq;
Amy Maloche83c385a2012-02-01 10:32:03 +0530306 unsigned int touch_x_size;
307 unsigned int touch_y_size;
Amy Maloche21115eb2011-11-02 09:04:37 -0700308 struct regulator *vcc_ana;
309 struct regulator *vcc_dig;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530310 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530311#if defined(CONFIG_HAS_EARLYSUSPEND)
312 struct early_suspend early_suspend;
313#endif
Jing Lin36aee812011-10-17 17:17:28 -0700314
Amy Maloche52262212011-09-15 16:46:57 -0700315 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700316 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700317 u8 t9_ctrl;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800318 u32 keyarray_old;
319 u32 keyarray_new;
320 u8 t9_max_reportid;
321 u8 t9_min_reportid;
322 u8 t15_max_reportid;
323 u8 t15_min_reportid;
Jing Lindc4413c2012-01-16 15:22:52 -0800324 u8 curr_cfg_version;
325 int cfg_version_idx;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700326};
327
Jing Lin6cfc00e2011-11-02 15:15:30 -0700328static struct dentry *debug_base;
329
Iiro Valkonen7686b102011-02-02 23:21:58 -0800330static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700331{
332 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800333 case MXT_GEN_MESSAGE_T5:
334 case MXT_GEN_COMMAND_T6:
335 case MXT_GEN_POWER_T7:
336 case MXT_GEN_ACQUIRE_T8:
337 case MXT_GEN_DATASOURCE_T53:
338 case MXT_TOUCH_MULTI_T9:
339 case MXT_TOUCH_KEYARRAY_T15:
340 case MXT_TOUCH_PROXIMITY_T23:
341 case MXT_TOUCH_PROXKEY_T52:
342 case MXT_PROCI_GRIPFACE_T20:
343 case MXT_PROCG_NOISE_T22:
344 case MXT_PROCI_ONETOUCH_T24:
345 case MXT_PROCI_TWOTOUCH_T27:
346 case MXT_PROCI_GRIP_T40:
347 case MXT_PROCI_PALM_T41:
348 case MXT_PROCI_TOUCHSUPPRESSION_T42:
349 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800350 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800351 case MXT_PROCG_NOISESUPPRESSION_T48:
352 case MXT_SPT_COMMSCONFIG_T18:
353 case MXT_SPT_GPIOPWM_T19:
354 case MXT_SPT_SELFTEST_T25:
355 case MXT_SPT_CTECONFIG_T28:
356 case MXT_SPT_USERDATA_T38:
357 case MXT_SPT_DIGITIZER_T43:
358 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700359 return true;
360 default:
361 return false;
362 }
363}
364
Iiro Valkonen7686b102011-02-02 23:21:58 -0800365static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700366{
367 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800368 case MXT_GEN_COMMAND_T6:
369 case MXT_GEN_POWER_T7:
370 case MXT_GEN_ACQUIRE_T8:
371 case MXT_TOUCH_MULTI_T9:
372 case MXT_TOUCH_KEYARRAY_T15:
373 case MXT_TOUCH_PROXIMITY_T23:
374 case MXT_TOUCH_PROXKEY_T52:
375 case MXT_PROCI_GRIPFACE_T20:
376 case MXT_PROCG_NOISE_T22:
377 case MXT_PROCI_ONETOUCH_T24:
378 case MXT_PROCI_TWOTOUCH_T27:
379 case MXT_PROCI_GRIP_T40:
380 case MXT_PROCI_PALM_T41:
381 case MXT_PROCI_TOUCHSUPPRESSION_T42:
382 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800383 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800384 case MXT_PROCG_NOISESUPPRESSION_T48:
385 case MXT_SPT_COMMSCONFIG_T18:
386 case MXT_SPT_GPIOPWM_T19:
387 case MXT_SPT_SELFTEST_T25:
388 case MXT_SPT_CTECONFIG_T28:
389 case MXT_SPT_USERDATA_T38:
390 case MXT_SPT_DIGITIZER_T43:
391 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700392 return true;
393 default:
394 return false;
395 }
396}
397
Iiro Valkonen7686b102011-02-02 23:21:58 -0800398static void mxt_dump_message(struct device *dev,
399 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700400{
401 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
402 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
403 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
404 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
405 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
406 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
407 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
408 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
409 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
410}
411
Iiro Valkonen7686b102011-02-02 23:21:58 -0800412static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700413 unsigned int state)
414{
415 u8 val;
416
417recheck:
418 if (i2c_master_recv(client, &val, 1) != 1) {
419 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
420 return -EIO;
421 }
422
423 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800424 case MXT_WAITING_BOOTLOAD_CMD:
425 case MXT_WAITING_FRAME_DATA:
426 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700427 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800428 case MXT_FRAME_CRC_PASS:
429 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430 goto recheck;
431 break;
432 default:
433 return -EINVAL;
434 }
435
436 if (val != state) {
437 dev_err(&client->dev, "Unvalid bootloader mode state\n");
438 return -EINVAL;
439 }
440
441 return 0;
442}
443
Iiro Valkonen7686b102011-02-02 23:21:58 -0800444static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700445{
446 u8 buf[2];
447
Iiro Valkonen7686b102011-02-02 23:21:58 -0800448 buf[0] = MXT_UNLOCK_CMD_LSB;
449 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700450
451 if (i2c_master_send(client, buf, 2) != 2) {
452 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
453 return -EIO;
454 }
455
456 return 0;
457}
458
Iiro Valkonen7686b102011-02-02 23:21:58 -0800459static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700460 const u8 *data, unsigned int frame_size)
461{
462 if (i2c_master_send(client, data, frame_size) != frame_size) {
463 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
464 return -EIO;
465 }
466
467 return 0;
468}
469
Iiro Valkonen7686b102011-02-02 23:21:58 -0800470static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700471 u16 reg, u16 len, void *val)
472{
473 struct i2c_msg xfer[2];
474 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700475 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700476
477 buf[0] = reg & 0xff;
478 buf[1] = (reg >> 8) & 0xff;
479
480 /* Write register */
481 xfer[0].addr = client->addr;
482 xfer[0].flags = 0;
483 xfer[0].len = 2;
484 xfer[0].buf = buf;
485
486 /* Read data */
487 xfer[1].addr = client->addr;
488 xfer[1].flags = I2C_M_RD;
489 xfer[1].len = len;
490 xfer[1].buf = val;
491
Jing Lin36aee812011-10-17 17:17:28 -0700492 do {
493 if (i2c_transfer(client->adapter, xfer, 2) == 2)
494 return 0;
495 msleep(MXT_WAKE_TIME);
496 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497
Jing Lin36aee812011-10-17 17:17:28 -0700498 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
499 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700500}
501
Iiro Valkonen7686b102011-02-02 23:21:58 -0800502static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700503{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800504 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700505}
506
Jing Lin36aee812011-10-17 17:17:28 -0700507static int __mxt_write_reg(struct i2c_client *client,
508 u16 addr, u16 length, u8 *value)
509{
510 u8 buf[MXT_BLOCK_SIZE + 2];
511 int i, tries = 0;
512
513 if (length > MXT_BLOCK_SIZE)
514 return -EINVAL;
515
516 buf[0] = addr & 0xff;
517 buf[1] = (addr >> 8) & 0xff;
518 for (i = 0; i < length; i++)
519 buf[i + 2] = *value++;
520
521 do {
522 if (i2c_master_send(client, buf, length + 2) == (length + 2))
523 return 0;
524 msleep(MXT_WAKE_TIME);
525 } while (++tries < MXT_MAX_RW_TRIES);
526
527 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
528 return -EIO;
529}
530
Iiro Valkonen7686b102011-02-02 23:21:58 -0800531static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532{
Jing Lin36aee812011-10-17 17:17:28 -0700533 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700534}
535
Iiro Valkonen7686b102011-02-02 23:21:58 -0800536static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700537 u16 reg, u8 *object_buf)
538{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800539 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700540 object_buf);
541}
542
Iiro Valkonen7686b102011-02-02 23:21:58 -0800543static struct mxt_object *
544mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800546 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700547 int i;
548
549 for (i = 0; i < data->info.object_num; i++) {
550 object = data->object_table + i;
551 if (object->type == type)
552 return object;
553 }
554
555 dev_err(&data->client->dev, "Invalid object type\n");
556 return NULL;
557}
558
Iiro Valkonen7686b102011-02-02 23:21:58 -0800559static int mxt_read_message(struct mxt_data *data,
560 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800562 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700563 u16 reg;
564
Iiro Valkonene8645592011-11-18 12:56:19 -0800565 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700566 if (!object)
567 return -EINVAL;
568
569 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800570 return __mxt_read_reg(data->client, reg,
571 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572}
573
Iiro Valkonen7686b102011-02-02 23:21:58 -0800574static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700575 u8 type, u8 offset, u8 *val)
576{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800577 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700578 u16 reg;
579
Iiro Valkonen7686b102011-02-02 23:21:58 -0800580 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700581 if (!object)
582 return -EINVAL;
583
584 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800585 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586}
587
Iiro Valkonen7686b102011-02-02 23:21:58 -0800588static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589 u8 type, u8 offset, u8 val)
590{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800591 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592 u16 reg;
593
Iiro Valkonen7686b102011-02-02 23:21:58 -0800594 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700595 if (!object)
596 return -EINVAL;
597
598 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800599 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600}
601
Iiro Valkonen7686b102011-02-02 23:21:58 -0800602static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700605 struct input_dev *input_dev = data->input_dev;
606 int status = finger[single_id].status;
607 int finger_num = 0;
608 int id;
609
Iiro Valkonen7686b102011-02-02 23:21:58 -0800610 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611 if (!finger[id].status)
612 continue;
613
Amy Maloche2b59bab2011-10-13 16:08:16 -0700614 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
615 finger[id].status != MXT_RELEASE ?
616 finger[id].area : 0);
617 input_report_abs(input_dev, ABS_MT_POSITION_X,
618 finger[id].x);
619 input_report_abs(input_dev, ABS_MT_POSITION_Y,
620 finger[id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700621 input_report_abs(input_dev, ABS_MT_PRESSURE,
Mohan Pallaka5e7343f2012-01-02 18:30:16 +0800622 finger[id].status != MXT_RELEASE ?
623 finger[id].pressure : 0);
Amy Maloche2b59bab2011-10-13 16:08:16 -0700624 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625
Amy Maloche2b59bab2011-10-13 16:08:16 -0700626 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700627 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700628 else
629 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700630 }
631
632 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
633
Iiro Valkonen7686b102011-02-02 23:21:58 -0800634 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635 input_report_abs(input_dev, ABS_X, finger[single_id].x);
636 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700637 input_report_abs(input_dev,
638 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639 }
640
641 input_sync(input_dev);
642}
643
Iiro Valkonen7686b102011-02-02 23:21:58 -0800644static void mxt_input_touchevent(struct mxt_data *data,
645 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800647 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700648 struct device *dev = &data->client->dev;
649 u8 status = message->message[0];
650 int x;
651 int y;
652 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700653 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700654
655 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800656 if (!(status & MXT_DETECT)) {
657 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658 dev_dbg(dev, "[%d] released\n", id);
659
Iiro Valkonen7686b102011-02-02 23:21:58 -0800660 finger[id].status = MXT_RELEASE;
661 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662 }
663 return;
664 }
665
666 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800667 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 return;
669
Joonyoung Shim910d8052011-04-12 23:14:38 -0700670 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
671 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Amy Maloche83c385a2012-02-01 10:32:03 +0530672 if (data->touch_x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700673 x = x >> 2;
Amy Maloche83c385a2012-02-01 10:32:03 +0530674 if (data->touch_y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700675 y = y >> 2;
676
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700678 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679
680 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800681 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700682 x, y, area);
683
Iiro Valkonen7686b102011-02-02 23:21:58 -0800684 finger[id].status = status & MXT_MOVE ?
685 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686 finger[id].x = x;
687 finger[id].y = y;
688 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700689 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700690
Iiro Valkonen7686b102011-02-02 23:21:58 -0800691 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692}
693
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800694static void mxt_handle_key_array(struct mxt_data *data,
695 struct mxt_message *message)
696{
697 u32 keys_changed;
698 int i;
699
700 if (!data->pdata->key_codes) {
701 dev_err(&data->client->dev, "keyarray is not supported\n");
702 return;
703 }
704
705 data->keyarray_new = message->message[1] |
706 (message->message[2] << 8) |
707 (message->message[3] << 16) |
708 (message->message[4] << 24);
709
710 keys_changed = data->keyarray_old ^ data->keyarray_new;
711
712 if (!keys_changed) {
713 dev_dbg(&data->client->dev, "no keys changed\n");
714 return;
715 }
716
717 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
718 if (!(keys_changed & (1 << i)))
719 continue;
720
721 input_report_key(data->input_dev, data->pdata->key_codes[i],
722 (data->keyarray_new & (1 << i)));
723 input_sync(data->input_dev);
724 }
725
726 data->keyarray_old = data->keyarray_new;
727}
728
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800731 struct mxt_data *data = dev_id;
732 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700733 struct device *dev = &data->client->dev;
734 int id;
735 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700736
737 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739 dev_err(dev, "Failed to read message\n");
740 goto end;
741 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700742 reportid = message.reportid;
743
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800744 if (!reportid) {
745 dev_dbg(dev, "Report id 0 is reserved\n");
746 continue;
747 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800749 /* check whether report id is part of T9 or T15 */
750 id = reportid - data->t9_min_reportid;
751
752 if (reportid >= data->t9_min_reportid &&
753 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800754 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800755 else if (reportid >= data->t15_min_reportid &&
756 reportid <= data->t15_max_reportid)
757 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800759 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700760 } while (reportid != 0xff);
761
762end:
763 return IRQ_HANDLED;
764}
765
Iiro Valkonen7686b102011-02-02 23:21:58 -0800766static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700767{
Jing Lindc4413c2012-01-16 15:22:52 -0800768 const struct mxt_config_info *config_info = data->config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800769 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700770 struct device *dev = &data->client->dev;
771 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800772 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700773
Jing Lindc4413c2012-01-16 15:22:52 -0800774 if (!config_info) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800775 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
776 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700777 }
778
779 for (i = 0; i < data->info.object_num; i++) {
780 object = data->object_table + i;
781
Iiro Valkonen7686b102011-02-02 23:21:58 -0800782 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783 continue;
784
Iiro Valkonen71749f52011-02-15 13:36:52 -0800785 for (j = 0; j < object->size + 1; j++) {
786 config_offset = index + j;
Jing Lindc4413c2012-01-16 15:22:52 -0800787 if (config_offset > config_info->config_length) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800788 dev_err(dev, "Not enough config data!\n");
789 return -EINVAL;
790 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800791 mxt_write_object(data, object->type, j,
Jing Lindc4413c2012-01-16 15:22:52 -0800792 config_info->config[config_offset]);
Iiro Valkonen71749f52011-02-15 13:36:52 -0800793 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794 index += object->size + 1;
795 }
796
797 return 0;
798}
799
Iiro Valkonen7686b102011-02-02 23:21:58 -0800800static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801{
802 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800803 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700804 int count = 10;
805 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700806
807 /* Read dummy message to make high CHG pin */
808 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800809 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700810 if (error)
811 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800812 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700813
814 if (!count) {
815 dev_err(dev, "CHG pin isn't cleared\n");
816 return -EBUSY;
817 }
818
819 return 0;
820}
821
Iiro Valkonen7686b102011-02-02 23:21:58 -0800822static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700823{
824 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800825 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700826 int error;
827 u8 val;
828
Iiro Valkonen7686b102011-02-02 23:21:58 -0800829 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700830 if (error)
831 return error;
832 info->family_id = val;
833
Iiro Valkonen7686b102011-02-02 23:21:58 -0800834 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700835 if (error)
836 return error;
837 info->variant_id = val;
838
Iiro Valkonen7686b102011-02-02 23:21:58 -0800839 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700840 if (error)
841 return error;
842 info->version = val;
843
Iiro Valkonen7686b102011-02-02 23:21:58 -0800844 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845 if (error)
846 return error;
847 info->build = val;
848
Iiro Valkonen7686b102011-02-02 23:21:58 -0800849 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700850 if (error)
851 return error;
852 info->object_num = val;
853
854 return 0;
855}
856
Iiro Valkonen7686b102011-02-02 23:21:58 -0800857static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700858{
859 int error;
860 int i;
861 u16 reg;
862 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800863 u8 buf[MXT_OBJECT_SIZE];
Jing Lindc4413c2012-01-16 15:22:52 -0800864 bool found_t38 = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700865
866 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800867 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700868
Iiro Valkonen7686b102011-02-02 23:21:58 -0800869 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
870 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700871 if (error)
872 return error;
873
874 object->type = buf[0];
875 object->start_address = (buf[2] << 8) | buf[1];
876 object->size = buf[3];
877 object->instances = buf[4];
878 object->num_report_ids = buf[5];
879
880 if (object->num_report_ids) {
881 reportid += object->num_report_ids *
882 (object->instances + 1);
883 object->max_reportid = reportid;
884 }
Jing Lindc4413c2012-01-16 15:22:52 -0800885
886 /* Calculate index for config major version in config array.
887 * Major version is the first byte in object T38.
888 */
889 if (object->type == MXT_SPT_USERDATA_T38)
890 found_t38 = true;
891 if (!found_t38 && mxt_object_writable(object->type))
892 data->cfg_version_idx += object->size + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893 }
894
895 return 0;
896}
897
Jing Lindc4413c2012-01-16 15:22:52 -0800898static int mxt_search_config_array(struct mxt_data *data, bool version_match)
899{
900
901 const struct mxt_platform_data *pdata = data->pdata;
902 const struct mxt_config_info *cfg_info;
903 struct mxt_info *info = &data->info;
904 int i;
905 u8 cfg_version;
906
907 for (i = 0; i < pdata->config_array_size; i++) {
908
909 cfg_info = &pdata->config_array[i];
910
911 if (!cfg_info->config || !cfg_info->config_length)
912 continue;
913
914 if (info->family_id == cfg_info->family_id &&
915 info->variant_id == cfg_info->variant_id &&
916 info->version == cfg_info->version &&
917 info->build == cfg_info->build) {
918
919 cfg_version = cfg_info->config[data->cfg_version_idx];
920 if (data->curr_cfg_version == cfg_version ||
921 !version_match) {
922 data->config_info = cfg_info;
923 return 0;
924 }
925 }
926 }
927
928 dev_info(&data->client->dev,
929 "Config not found: F: %d, V: %d, FW: %d.%d.%d, CFG: %d\n",
930 info->family_id, info->variant_id,
931 info->version >> 4, info->version & 0xF, info->build,
932 data->curr_cfg_version);
933 return -EINVAL;
934}
935
936static int mxt_get_config(struct mxt_data *data)
937{
938 const struct mxt_platform_data *pdata = data->pdata;
939 struct device *dev = &data->client->dev;
940 struct mxt_object *object;
941 int error;
942
943 if (!pdata->config_array || !pdata->config_array_size) {
944 dev_dbg(dev, "No cfg data provided by platform data\n");
945 return 0;
946 }
947
948 /* Get current config version */
949 object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
950 if (!object) {
951 dev_err(dev, "Unable to obtain USERDATA object\n");
952 return -EINVAL;
953 }
954
955 error = mxt_read_reg(data->client, object->start_address,
956 &data->curr_cfg_version);
957 if (error) {
958 dev_err(dev, "Unable to read config version\n");
959 return error;
960 }
961
962 /* It is possible that the config data on the controller is not
963 * versioned and the version number returns 0. In this case,
964 * find a match without the config version checking.
965 */
966 error = mxt_search_config_array(data,
967 data->curr_cfg_version != 0 ? true : false);
968 if (error)
969 return error;
970
971 return 0;
972}
Amy Maloche7e447432011-09-14 11:36:30 -0700973static void mxt_reset_delay(struct mxt_data *data)
974{
975 struct mxt_info *info = &data->info;
976
977 switch (info->family_id) {
978 case MXT224_ID:
979 msleep(MXT224_RESET_TIME);
980 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -0700981 case MXT224E_ID:
982 msleep(MXT224E_RESET_TIME);
983 break;
Amy Maloche7e447432011-09-14 11:36:30 -0700984 case MXT1386_ID:
985 msleep(MXT1386_RESET_TIME);
986 break;
987 default:
988 msleep(MXT_RESET_TIME);
989 }
990}
991
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993{
994 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700996 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700997 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700999 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -07001000 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001001 struct mxt_object *t9_object;
1002 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003
Iiro Valkonen7686b102011-02-02 23:21:58 -08001004 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005 if (error)
1006 return error;
1007
1008 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001009 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010 GFP_KERNEL);
1011 if (!data->object_table) {
1012 dev_err(&client->dev, "Failed to allocate memory\n");
1013 return -ENOMEM;
1014 }
1015
1016 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001019 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020
Jing Lindc4413c2012-01-16 15:22:52 -08001021 /* Get config data from platform data */
1022 error = mxt_get_config(data);
1023 if (error)
1024 dev_dbg(&client->dev, "Config info not found.\n");
1025
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001026 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001028 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001029 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001030
Amy Maloche52262212011-09-15 16:46:57 -07001031 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -08001032 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -07001033 if (!t7_object) {
1034 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -07001035 error = -EINVAL;
1036 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -07001037 }
1038
1039 data->t7_start_addr = t7_object->start_address;
1040 error = __mxt_read_reg(client, data->t7_start_addr,
1041 T7_DATA_SIZE, data->t7_data);
1042 if (error < 0) {
1043 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -07001044 "Failed to save current power state\n");
1045 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001046 }
Iiro Valkonene8645592011-11-18 12:56:19 -08001047 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -07001048 &data->t9_ctrl);
1049 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -07001050 dev_err(&client->dev, "Failed to save current touch object\n");
1051 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001052 }
1053
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001054 /* Store T9, T15's min and max report ids */
1055 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1056 if (!t9_object) {
1057 dev_err(&client->dev, "Failed to get T9 object\n");
1058 error = -EINVAL;
1059 goto free_object_table;
1060 }
1061 data->t9_max_reportid = t9_object->max_reportid;
1062 data->t9_min_reportid = t9_object->max_reportid -
1063 t9_object->num_report_ids + 1;
1064
1065 if (data->pdata->key_codes) {
1066 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
1067 if (!t15_object)
1068 dev_dbg(&client->dev, "T15 object is not available\n");
1069 else {
1070 data->t15_max_reportid = t15_object->max_reportid;
1071 data->t15_min_reportid = t15_object->max_reportid -
1072 t15_object->num_report_ids + 1;
1073 }
1074 }
1075
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001076 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -08001077 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001078 MXT_COMMAND_BACKUPNV,
1079 MXT_BACKUP_VALUE);
1080 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001081 do {
Iiro Valkonene8645592011-11-18 12:56:19 -08001082 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001083 MXT_COMMAND_BACKUPNV,
1084 &command_register);
1085 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001086 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -07001087 usleep_range(1000, 2000);
1088 } while ((command_register != 0) && (++timeout_counter <= 100));
1089 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001090 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -07001091 error = -EIO;
1092 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001093 }
1094
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095
1096 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -08001097 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001098 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001099
Amy Maloche7e447432011-09-14 11:36:30 -07001100 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001101
1102 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001103 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001105 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001106 info->matrix_xsize = val;
1107
Iiro Valkonen7686b102011-02-02 23:21:58 -08001108 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001110 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111 info->matrix_ysize = val;
1112
1113 dev_info(&client->dev,
1114 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
1115 info->family_id, info->variant_id, info->version,
1116 info->build);
1117
1118 dev_info(&client->dev,
1119 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
1120 info->matrix_xsize, info->matrix_ysize,
1121 info->object_num);
1122
1123 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001124
1125free_object_table:
1126 kfree(data->object_table);
1127 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001128}
1129
Iiro Valkonen7686b102011-02-02 23:21:58 -08001130static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001131 struct device_attribute *attr, char *buf)
1132{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001133 struct mxt_data *data = dev_get_drvdata(dev);
1134 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001135 int count = 0;
1136 int i, j;
1137 int error;
1138 u8 val;
1139
1140 for (i = 0; i < data->info.object_num; i++) {
1141 object = data->object_table + i;
1142
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001143 count += snprintf(buf + count, PAGE_SIZE - count,
1144 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001145 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001146 if (count >= PAGE_SIZE)
1147 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001148
Iiro Valkonen7686b102011-02-02 23:21:58 -08001149 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001150 count += snprintf(buf + count, PAGE_SIZE - count,
1151 "\n");
1152 if (count >= PAGE_SIZE)
1153 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001154 continue;
1155 }
1156
1157 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001158 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001159 object->type, j, &val);
1160 if (error)
1161 return error;
1162
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001163 count += snprintf(buf + count, PAGE_SIZE - count,
1164 "\t[%2d]: %02x (%d)\n", j, val, val);
1165 if (count >= PAGE_SIZE)
1166 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001167 }
1168
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001169 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1170 if (count >= PAGE_SIZE)
1171 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001172 }
1173
1174 return count;
1175}
1176
Iiro Valkonen7686b102011-02-02 23:21:58 -08001177static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001178{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001179 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001180 struct i2c_client *client = data->client;
1181 const struct firmware *fw = NULL;
1182 unsigned int frame_size;
1183 unsigned int pos = 0;
1184 int ret;
1185
1186 ret = request_firmware(&fw, fn, dev);
1187 if (ret) {
1188 dev_err(dev, "Unable to open firmware %s\n", fn);
1189 return ret;
1190 }
1191
1192 /* Change to the bootloader mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001193 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001194 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001195
1196 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001197
1198 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001199 if (client->addr == MXT_APP_LOW)
1200 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001202 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001203
Iiro Valkonen7686b102011-02-02 23:21:58 -08001204 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001205 if (ret)
1206 goto out;
1207
1208 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001209 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001210
1211 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001212 ret = mxt_check_bootloader(client,
1213 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001214 if (ret)
1215 goto out;
1216
1217 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1218
1219 /* We should add 2 at frame size as the the firmware data is not
1220 * included the CRC bytes.
1221 */
1222 frame_size += 2;
1223
1224 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001225 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001226
Iiro Valkonen7686b102011-02-02 23:21:58 -08001227 ret = mxt_check_bootloader(client,
1228 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001229 if (ret)
1230 goto out;
1231
1232 pos += frame_size;
1233
1234 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1235 }
1236
1237out:
1238 release_firmware(fw);
1239
1240 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001241 if (client->addr == MXT_BOOT_LOW)
1242 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001243 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001244 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001245
1246 return ret;
1247}
1248
Iiro Valkonen7686b102011-02-02 23:21:58 -08001249static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001250 struct device_attribute *attr,
1251 const char *buf, size_t count)
1252{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001253 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001254 int error;
1255
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001256 disable_irq(data->irq);
1257
Iiro Valkonen7686b102011-02-02 23:21:58 -08001258 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001259 if (error) {
1260 dev_err(dev, "The firmware update failed(%d)\n", error);
1261 count = error;
1262 } else {
1263 dev_dbg(dev, "The firmware update succeeded\n");
1264
1265 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001266 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001267
1268 kfree(data->object_table);
1269 data->object_table = NULL;
1270
Iiro Valkonen7686b102011-02-02 23:21:58 -08001271 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001272 }
1273
1274 enable_irq(data->irq);
1275
Iiro Valkonen08960a02011-04-12 23:16:40 -07001276 error = mxt_make_highchg(data);
1277 if (error)
1278 return error;
1279
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280 return count;
1281}
1282
Iiro Valkonen7686b102011-02-02 23:21:58 -08001283static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1284static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001285
Iiro Valkonen7686b102011-02-02 23:21:58 -08001286static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001287 &dev_attr_object.attr,
1288 &dev_attr_update_fw.attr,
1289 NULL
1290};
1291
Iiro Valkonen7686b102011-02-02 23:21:58 -08001292static const struct attribute_group mxt_attr_group = {
1293 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001294};
1295
Amy Maloche52262212011-09-15 16:46:57 -07001296static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001297{
Jing Lin36aee812011-10-17 17:17:28 -07001298 int error;
1299
Amy Maloche52262212011-09-15 16:46:57 -07001300 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001301 error = __mxt_write_reg(data->client, data->t7_start_addr,
1302 T7_DATA_SIZE, data->t7_data);
1303 if (error < 0) {
1304 dev_err(&data->client->dev,
1305 "failed to restore old power state\n");
1306 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001307 }
Jing Lin36aee812011-10-17 17:17:28 -07001308
Amy Maloche52262212011-09-15 16:46:57 -07001309 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001310 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001311 if (error < 0) {
1312 dev_err(&data->client->dev, "failed to restore touch\n");
1313 return error;
1314 }
1315
1316 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001317}
1318
Amy Maloche52262212011-09-15 16:46:57 -07001319static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001320{
Jing Lin36aee812011-10-17 17:17:28 -07001321 int error;
1322 u8 t7_data[T7_DATA_SIZE] = {0};
1323
1324 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001325 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001326 if (error < 0) {
1327 dev_err(&data->client->dev, "failed to disable touch\n");
1328 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001329 }
1330
Jing Lin36aee812011-10-17 17:17:28 -07001331 error = __mxt_write_reg(data->client, data->t7_start_addr,
1332 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001333 if (error < 0) {
1334 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001335 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001336 return error;
1337 }
1338
1339 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001340}
1341
Iiro Valkonen7686b102011-02-02 23:21:58 -08001342static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001343{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001344 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001345 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001346
Amy Maloche52262212011-09-15 16:46:57 -07001347 error = mxt_start(data);
1348 if (error < 0) {
1349 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1350 return error;
1351 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001352
1353 return 0;
1354}
1355
Iiro Valkonen7686b102011-02-02 23:21:58 -08001356static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001357{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001358 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001359 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001360
Amy Maloche52262212011-09-15 16:46:57 -07001361 error = mxt_stop(data);
1362 if (error < 0)
1363 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1364
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001365}
1366
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301367static int mxt_power_on(struct mxt_data *data, bool on)
1368{
1369 int rc;
1370
1371 if (on == false)
1372 goto power_off;
1373
Amy Maloche21115eb2011-11-02 09:04:37 -07001374 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301375 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001376 dev_err(&data->client->dev,
1377 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301378 return rc;
1379 }
1380
Amy Maloche21115eb2011-11-02 09:04:37 -07001381 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301382 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001383 dev_err(&data->client->dev,
1384 "Regulator vcc_ana enable failed rc=%d\n", rc);
1385 goto error_reg_en_vcc_ana;
1386 }
1387
1388 if (data->pdata->digital_pwr_regulator) {
1389 rc = regulator_set_optimum_mode(data->vcc_dig,
1390 MXT_ACTIVE_LOAD_DIG_UA);
1391 if (rc < 0) {
1392 dev_err(&data->client->dev,
1393 "Regulator vcc_dig set_opt failed rc=%d\n",
1394 rc);
1395 goto error_reg_opt_vcc_dig;
1396 }
1397
1398 rc = regulator_enable(data->vcc_dig);
1399 if (rc) {
1400 dev_err(&data->client->dev,
1401 "Regulator vcc_dig enable failed rc=%d\n", rc);
1402 goto error_reg_en_vcc_dig;
1403 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301404 }
1405
1406 if (data->pdata->i2c_pull_up) {
1407 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1408 if (rc < 0) {
1409 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001410 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301411 goto error_reg_opt_i2c;
1412 }
1413
1414 rc = regulator_enable(data->vcc_i2c);
1415 if (rc) {
1416 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001417 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301418 goto error_reg_en_vcc_i2c;
1419 }
1420 }
1421
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001422 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301423
1424 return 0;
1425
1426error_reg_en_vcc_i2c:
1427 if (data->pdata->i2c_pull_up)
1428 regulator_set_optimum_mode(data->vcc_i2c, 0);
1429error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001430 if (data->pdata->digital_pwr_regulator)
1431 regulator_disable(data->vcc_dig);
1432error_reg_en_vcc_dig:
1433 if (data->pdata->digital_pwr_regulator)
1434 regulator_set_optimum_mode(data->vcc_dig, 0);
1435error_reg_opt_vcc_dig:
1436 regulator_disable(data->vcc_ana);
1437error_reg_en_vcc_ana:
1438 regulator_set_optimum_mode(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301439 return rc;
1440
1441power_off:
Amy Maloche21115eb2011-11-02 09:04:37 -07001442 regulator_set_optimum_mode(data->vcc_ana, 0);
1443 regulator_disable(data->vcc_ana);
1444 if (data->pdata->digital_pwr_regulator) {
1445 regulator_set_optimum_mode(data->vcc_dig, 0);
1446 regulator_disable(data->vcc_dig);
1447 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301448 if (data->pdata->i2c_pull_up) {
1449 regulator_set_optimum_mode(data->vcc_i2c, 0);
1450 regulator_disable(data->vcc_i2c);
1451 }
1452 msleep(50);
1453 return 0;
1454}
1455
1456static int mxt_regulator_configure(struct mxt_data *data, bool on)
1457{
1458 int rc;
1459
1460 if (on == false)
1461 goto hw_shutdown;
1462
Amy Maloche21115eb2011-11-02 09:04:37 -07001463 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1464 if (IS_ERR(data->vcc_ana)) {
1465 rc = PTR_ERR(data->vcc_ana);
1466 dev_err(&data->client->dev,
1467 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301468 return rc;
1469 }
1470
Amy Maloche21115eb2011-11-02 09:04:37 -07001471 if (regulator_count_voltages(data->vcc_ana) > 0) {
1472 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301473 MXT_VTG_MAX_UV);
1474 if (rc) {
1475 dev_err(&data->client->dev,
1476 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001477 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301478 }
1479 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001480 if (data->pdata->digital_pwr_regulator) {
1481 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1482 if (IS_ERR(data->vcc_dig)) {
1483 rc = PTR_ERR(data->vcc_dig);
1484 dev_err(&data->client->dev,
1485 "Regulator get dig failed rc=%d\n", rc);
1486 goto error_get_vtg_vcc_dig;
1487 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301488
Amy Maloche21115eb2011-11-02 09:04:37 -07001489 if (regulator_count_voltages(data->vcc_dig) > 0) {
1490 rc = regulator_set_voltage(data->vcc_dig,
1491 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1492 if (rc) {
1493 dev_err(&data->client->dev,
1494 "regulator set_vtg failed rc=%d\n", rc);
1495 goto error_set_vtg_vcc_dig;
1496 }
1497 }
1498 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301499 if (data->pdata->i2c_pull_up) {
1500 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1501 if (IS_ERR(data->vcc_i2c)) {
1502 rc = PTR_ERR(data->vcc_i2c);
1503 dev_err(&data->client->dev,
1504 "Regulator get failed rc=%d\n", rc);
1505 goto error_get_vtg_i2c;
1506 }
1507 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1508 rc = regulator_set_voltage(data->vcc_i2c,
1509 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1510 if (rc) {
1511 dev_err(&data->client->dev,
1512 "regulator set_vtg failed rc=%d\n", rc);
1513 goto error_set_vtg_i2c;
1514 }
1515 }
1516 }
1517
1518 return 0;
1519
1520error_set_vtg_i2c:
1521 regulator_put(data->vcc_i2c);
1522error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001523 if (data->pdata->digital_pwr_regulator)
1524 if (regulator_count_voltages(data->vcc_dig) > 0)
1525 regulator_set_voltage(data->vcc_dig, 0,
1526 MXT_VTG_DIG_MAX_UV);
1527error_set_vtg_vcc_dig:
1528 if (data->pdata->digital_pwr_regulator)
1529 regulator_put(data->vcc_dig);
1530error_get_vtg_vcc_dig:
1531 if (regulator_count_voltages(data->vcc_ana) > 0)
1532 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1533error_set_vtg_vcc_ana:
1534 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301535 return rc;
1536
1537hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001538 if (regulator_count_voltages(data->vcc_ana) > 0)
1539 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1540 regulator_put(data->vcc_ana);
1541 if (data->pdata->digital_pwr_regulator) {
1542 if (regulator_count_voltages(data->vcc_dig) > 0)
1543 regulator_set_voltage(data->vcc_dig, 0,
1544 MXT_VTG_DIG_MAX_UV);
1545 regulator_put(data->vcc_dig);
1546 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301547 if (data->pdata->i2c_pull_up) {
1548 if (regulator_count_voltages(data->vcc_i2c) > 0)
1549 regulator_set_voltage(data->vcc_i2c, 0,
1550 MXT_I2C_VTG_MAX_UV);
1551 regulator_put(data->vcc_i2c);
1552 }
1553 return 0;
1554}
1555
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301556#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001557static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1558{
1559
1560 int rc;
1561
1562 if (on == false)
1563 goto regulator_hpm;
1564
Amy Maloche21115eb2011-11-02 09:04:37 -07001565 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001566 if (rc < 0) {
1567 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001568 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001569 goto fail_regulator_lpm;
1570 }
1571
Amy Maloche21115eb2011-11-02 09:04:37 -07001572 if (data->pdata->digital_pwr_regulator) {
1573 rc = regulator_set_optimum_mode(data->vcc_dig,
1574 MXT_LPM_LOAD_DIG_UA);
1575 if (rc < 0) {
1576 dev_err(&data->client->dev,
1577 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1578 goto fail_regulator_lpm;
1579 }
1580 }
1581
Jing Linbace50b2011-10-18 22:55:47 -07001582 if (data->pdata->i2c_pull_up) {
1583 rc = regulator_set_optimum_mode(data->vcc_i2c,
1584 MXT_I2C_LPM_LOAD_UA);
1585 if (rc < 0) {
1586 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001587 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001588 goto fail_regulator_lpm;
1589 }
1590 }
1591
1592 return 0;
1593
1594regulator_hpm:
1595
Amy Maloche21115eb2011-11-02 09:04:37 -07001596 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001597 if (rc < 0) {
1598 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001599 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001600 goto fail_regulator_hpm;
1601 }
1602
Amy Maloche21115eb2011-11-02 09:04:37 -07001603 if (data->pdata->digital_pwr_regulator) {
1604 rc = regulator_set_optimum_mode(data->vcc_dig,
1605 MXT_ACTIVE_LOAD_DIG_UA);
1606 if (rc < 0) {
1607 dev_err(&data->client->dev,
1608 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1609 goto fail_regulator_hpm;
1610 }
1611 }
1612
Jing Linbace50b2011-10-18 22:55:47 -07001613 if (data->pdata->i2c_pull_up) {
1614 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1615 if (rc < 0) {
1616 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001617 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001618 goto fail_regulator_hpm;
1619 }
1620 }
1621
1622 return 0;
1623
1624fail_regulator_lpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001625 regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
1626 if (data->pdata->digital_pwr_regulator)
1627 regulator_set_optimum_mode(data->vcc_dig,
1628 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001629 if (data->pdata->i2c_pull_up)
1630 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1631
1632 return rc;
1633
1634fail_regulator_hpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001635 regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
1636 if (data->pdata->digital_pwr_regulator)
1637 regulator_set_optimum_mode(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001638 if (data->pdata->i2c_pull_up)
1639 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1640
1641 return rc;
1642}
1643
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301644static int mxt_suspend(struct device *dev)
1645{
1646 struct i2c_client *client = to_i2c_client(dev);
1647 struct mxt_data *data = i2c_get_clientdata(client);
1648 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001649 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301650
1651 mutex_lock(&input_dev->mutex);
1652
Amy Maloche52262212011-09-15 16:46:57 -07001653 if (input_dev->users) {
1654 error = mxt_stop(data);
1655 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001656 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001657 mutex_unlock(&input_dev->mutex);
1658 return error;
1659 }
1660
1661 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301662
1663 mutex_unlock(&input_dev->mutex);
1664
Jing Linbace50b2011-10-18 22:55:47 -07001665 /* put regulators in low power mode */
1666 error = mxt_regulator_lpm(data, true);
1667 if (error < 0) {
1668 dev_err(dev, "failed to enter low power mode\n");
1669 return error;
1670 }
1671
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301672 return 0;
1673}
1674
1675static int mxt_resume(struct device *dev)
1676{
1677 struct i2c_client *client = to_i2c_client(dev);
1678 struct mxt_data *data = i2c_get_clientdata(client);
1679 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001680 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301681
Jing Linbace50b2011-10-18 22:55:47 -07001682 /* put regulators in high power mode */
1683 error = mxt_regulator_lpm(data, false);
1684 if (error < 0) {
1685 dev_err(dev, "failed to enter high power mode\n");
1686 return error;
1687 }
1688
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301689 mutex_lock(&input_dev->mutex);
1690
Amy Maloche52262212011-09-15 16:46:57 -07001691 if (input_dev->users) {
1692 error = mxt_start(data);
1693 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001694 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001695 mutex_unlock(&input_dev->mutex);
1696 return error;
1697 }
1698 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301699
1700 mutex_unlock(&input_dev->mutex);
1701
1702 return 0;
1703}
1704
1705#if defined(CONFIG_HAS_EARLYSUSPEND)
1706static void mxt_early_suspend(struct early_suspend *h)
1707{
1708 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1709
1710 mxt_suspend(&data->client->dev);
1711}
1712
1713static void mxt_late_resume(struct early_suspend *h)
1714{
1715 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1716
1717 mxt_resume(&data->client->dev);
1718}
1719#endif
1720
1721static const struct dev_pm_ops mxt_pm_ops = {
1722#ifndef CONFIG_HAS_EARLYSUSPEND
1723 .suspend = mxt_suspend,
1724 .resume = mxt_resume,
1725#endif
1726};
1727#endif
1728
Jing Lin6cfc00e2011-11-02 15:15:30 -07001729static int mxt_debugfs_object_show(struct seq_file *m, void *v)
1730{
1731 struct mxt_data *data = m->private;
1732 struct mxt_object *object;
1733 struct device *dev = &data->client->dev;
1734 int i, j, k;
1735 int error;
1736 int obj_size;
1737 u8 val;
1738
1739 for (i = 0; i < data->info.object_num; i++) {
1740 object = data->object_table + i;
1741 obj_size = object->size + 1;
1742
1743 seq_printf(m, "Object[%d] (Type %d)\n", i + 1, object->type);
1744
1745 for (j = 0; j < object->instances + 1; j++) {
1746 seq_printf(m, "[Instance %d]\n", j);
1747
1748 for (k = 0; k < obj_size; k++) {
1749 error = mxt_read_object(data, object->type,
1750 j * obj_size + k, &val);
1751 if (error) {
1752 dev_err(dev,
1753 "Failed to read object %d "
1754 "instance %d at offset %d\n",
1755 object->type, j, k);
1756 return error;
1757 }
1758
1759 seq_printf(m, "Byte %d: 0x%02x (%d)\n",
1760 k, val, val);
1761 }
1762 }
1763 }
1764
1765 return 0;
1766}
1767
1768static int mxt_debugfs_object_open(struct inode *inode, struct file *file)
1769{
1770 return single_open(file, mxt_debugfs_object_show, inode->i_private);
1771}
1772
1773static const struct file_operations mxt_object_fops = {
1774 .owner = THIS_MODULE,
1775 .open = mxt_debugfs_object_open,
1776 .read = seq_read,
1777 .release = single_release,
1778};
1779
1780static void __init mxt_debugfs_init(struct mxt_data *data)
1781{
1782 debug_base = debugfs_create_dir(MXT_DEBUGFS_DIR, NULL);
1783 if (IS_ERR_OR_NULL(debug_base))
1784 pr_err("atmel_mxt_ts: Failed to create debugfs dir\n");
1785 if (IS_ERR_OR_NULL(debugfs_create_file(MXT_DEBUGFS_FILE,
1786 0444,
1787 debug_base,
1788 data,
1789 &mxt_object_fops))) {
1790 pr_err("atmel_mxt_ts: Failed to create object file\n");
1791 debugfs_remove_recursive(debug_base);
1792 }
1793}
1794
Iiro Valkonen7686b102011-02-02 23:21:58 -08001795static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001796 const struct i2c_device_id *id)
1797{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001798 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001799 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001800 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001801 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001802
Iiro Valkonen919ed892011-02-15 13:36:52 -08001803 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001804 return -EINVAL;
1805
Iiro Valkonen7686b102011-02-02 23:21:58 -08001806 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001807 input_dev = input_allocate_device();
1808 if (!data || !input_dev) {
1809 dev_err(&client->dev, "Failed to allocate memory\n");
1810 error = -ENOMEM;
1811 goto err_free_mem;
1812 }
1813
Amy Maloche83c385a2012-02-01 10:32:03 +05301814 input_dev->name = "atmel_mxt_ts";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001815 input_dev->id.bustype = BUS_I2C;
1816 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001817 input_dev->open = mxt_input_open;
1818 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001819
Joonyoung Shim910d8052011-04-12 23:14:38 -07001820 data->client = client;
1821 data->input_dev = input_dev;
1822 data->pdata = pdata;
1823 data->irq = client->irq;
1824
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001825 __set_bit(EV_ABS, input_dev->evbit);
1826 __set_bit(EV_KEY, input_dev->evbit);
1827 __set_bit(BTN_TOUCH, input_dev->keybit);
1828
1829 /* For single touch */
1830 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001831 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001832 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001833 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001834 input_set_abs_params(input_dev, ABS_PRESSURE,
1835 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001836
1837 /* For multi touch */
1838 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001839 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001840 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001841 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001842 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001843 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001844 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1845 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001846
Amy Maloche83c385a2012-02-01 10:32:03 +05301847 if (pdata->touch_x_size)
1848 data->touch_x_size = pdata->touch_x_size;
1849 else
1850 data->touch_x_size = pdata->x_size;
1851
1852 if (pdata->touch_y_size)
1853 data->touch_y_size = pdata->touch_y_size;
1854 else
1855 data->touch_y_size = pdata->y_size;
1856
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001857 /* set key array supported keys */
1858 if (pdata->key_codes) {
1859 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
1860 if (pdata->key_codes[i])
1861 input_set_capability(input_dev, EV_KEY,
1862 pdata->key_codes[i]);
1863 }
1864 }
1865
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001866 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001867 i2c_set_clientdata(client, data);
1868
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301869 if (pdata->init_hw)
1870 error = pdata->init_hw(true);
1871 else
1872 error = mxt_regulator_configure(data, true);
1873 if (error) {
1874 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001875 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301876 }
1877
1878 if (pdata->power_on)
1879 error = pdata->power_on(true);
1880 else
1881 error = mxt_power_on(data, true);
1882 if (error) {
1883 dev_err(&client->dev, "Failed to power on hardware\n");
1884 goto err_regulator_on;
1885 }
1886
Amy Maloche08266db2011-11-04 11:07:16 -07001887 if (gpio_is_valid(pdata->irq_gpio)) {
1888 /* configure touchscreen irq gpio */
1889 error = gpio_request(pdata->irq_gpio,
1890 "mxt_irq_gpio");
1891 if (error) {
1892 pr_err("%s: unable to request gpio [%d]\n", __func__,
1893 pdata->irq_gpio);
1894 goto err_power_on;
1895 }
1896 error = gpio_direction_input(pdata->irq_gpio);
1897 if (error) {
1898 pr_err("%s: unable to set_direction for gpio [%d]\n",
1899 __func__, pdata->irq_gpio);
1900 goto err_irq_gpio_req;
1901 }
1902 }
1903
1904 if (gpio_is_valid(pdata->reset_gpio)) {
1905 /* configure touchscreen reset out gpio */
1906 error = gpio_request(pdata->reset_gpio,
1907 "mxt_reset_gpio");
1908 if (error) {
1909 pr_err("%s: unable to request reset gpio %d\n",
1910 __func__, pdata->reset_gpio);
1911 goto err_irq_gpio_req;
1912 }
1913
1914 error = gpio_direction_output(
1915 pdata->reset_gpio, 1);
1916 if (error) {
1917 pr_err("%s: unable to set direction for gpio %d\n",
1918 __func__, pdata->reset_gpio);
1919 goto err_reset_gpio_req;
1920 }
1921 }
1922
1923 mxt_reset_delay(data);
1924
Iiro Valkonen7686b102011-02-02 23:21:58 -08001925 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001926 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07001927 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001928
Iiro Valkonen7686b102011-02-02 23:21:58 -08001929 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001930 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001931 if (error) {
1932 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001933 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001934 }
1935
Iiro Valkonen08960a02011-04-12 23:16:40 -07001936 error = mxt_make_highchg(data);
1937 if (error)
1938 goto err_free_irq;
1939
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001940 error = input_register_device(input_dev);
1941 if (error)
1942 goto err_free_irq;
1943
Iiro Valkonen7686b102011-02-02 23:21:58 -08001944 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001945 if (error)
1946 goto err_unregister_device;
1947
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301948#if defined(CONFIG_HAS_EARLYSUSPEND)
1949 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1950 MXT_SUSPEND_LEVEL;
1951 data->early_suspend.suspend = mxt_early_suspend;
1952 data->early_suspend.resume = mxt_late_resume;
1953 register_early_suspend(&data->early_suspend);
1954#endif
1955
Jing Lin6cfc00e2011-11-02 15:15:30 -07001956 mxt_debugfs_init(data);
1957
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001958 return 0;
1959
1960err_unregister_device:
1961 input_unregister_device(input_dev);
1962 input_dev = NULL;
1963err_free_irq:
1964 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001965err_free_object:
1966 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07001967err_reset_gpio_req:
1968 if (gpio_is_valid(pdata->reset_gpio))
1969 gpio_free(pdata->reset_gpio);
1970err_irq_gpio_req:
1971 if (gpio_is_valid(pdata->irq_gpio))
1972 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301973err_power_on:
1974 if (pdata->power_on)
1975 pdata->power_on(false);
1976 else
1977 mxt_power_on(data, false);
1978err_regulator_on:
1979 if (pdata->init_hw)
1980 pdata->init_hw(false);
1981 else
1982 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001983err_free_mem:
1984 input_free_device(input_dev);
1985 kfree(data);
1986 return error;
1987}
1988
Iiro Valkonen7686b102011-02-02 23:21:58 -08001989static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001990{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001991 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001992
Iiro Valkonen7686b102011-02-02 23:21:58 -08001993 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001994 free_irq(data->irq, data);
1995 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301996#if defined(CONFIG_HAS_EARLYSUSPEND)
1997 unregister_early_suspend(&data->early_suspend);
1998#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301999
2000 if (data->pdata->power_on)
2001 data->pdata->power_on(false);
2002 else
2003 mxt_power_on(data, false);
2004
2005 if (data->pdata->init_hw)
2006 data->pdata->init_hw(false);
2007 else
2008 mxt_regulator_configure(data, false);
2009
Mohan Pallakabfe8f302012-01-02 18:32:08 +08002010 if (gpio_is_valid(data->pdata->reset_gpio))
2011 gpio_free(data->pdata->reset_gpio);
2012
2013 if (gpio_is_valid(data->pdata->irq_gpio))
2014 gpio_free(data->pdata->irq_gpio);
2015
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002016 kfree(data->object_table);
2017 kfree(data);
2018
Jing Lin6cfc00e2011-11-02 15:15:30 -07002019 debugfs_remove_recursive(debug_base);
2020
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002021 return 0;
2022}
2023
Iiro Valkonen7686b102011-02-02 23:21:58 -08002024static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002025 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002026 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08002027 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002028 { }
2029};
Iiro Valkonen7686b102011-02-02 23:21:58 -08002030MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002031
Iiro Valkonen7686b102011-02-02 23:21:58 -08002032static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002033 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08002034 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002035 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002036#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08002037 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002038#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002039 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002040 .probe = mxt_probe,
2041 .remove = __devexit_p(mxt_remove),
2042 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002043};
2044
Iiro Valkonen7686b102011-02-02 23:21:58 -08002045static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002046{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002047 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002048}
2049
Iiro Valkonen7686b102011-02-02 23:21:58 -08002050static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002051{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002052 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002053}
2054
Iiro Valkonen7686b102011-02-02 23:21:58 -08002055module_init(mxt_init);
2056module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002057
2058/* Module information */
2059MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08002060MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002061MODULE_LICENSE("GPL");