blob: 71bdcde66c3d2fd8fb09cf3cbe4483d39f7a8a59 [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 Maloche21115eb2011-11-02 09:04:37 -0700306 struct regulator *vcc_ana;
307 struct regulator *vcc_dig;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530308 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530309#if defined(CONFIG_HAS_EARLYSUSPEND)
310 struct early_suspend early_suspend;
311#endif
Jing Lin36aee812011-10-17 17:17:28 -0700312
Amy Maloche52262212011-09-15 16:46:57 -0700313 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700314 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700315 u8 t9_ctrl;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800316 u32 keyarray_old;
317 u32 keyarray_new;
318 u8 t9_max_reportid;
319 u8 t9_min_reportid;
320 u8 t15_max_reportid;
321 u8 t15_min_reportid;
Jing Lindc4413c2012-01-16 15:22:52 -0800322 u8 curr_cfg_version;
323 int cfg_version_idx;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700324};
325
Jing Lin6cfc00e2011-11-02 15:15:30 -0700326static struct dentry *debug_base;
327
Iiro Valkonen7686b102011-02-02 23:21:58 -0800328static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700329{
330 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800331 case MXT_GEN_MESSAGE_T5:
332 case MXT_GEN_COMMAND_T6:
333 case MXT_GEN_POWER_T7:
334 case MXT_GEN_ACQUIRE_T8:
335 case MXT_GEN_DATASOURCE_T53:
336 case MXT_TOUCH_MULTI_T9:
337 case MXT_TOUCH_KEYARRAY_T15:
338 case MXT_TOUCH_PROXIMITY_T23:
339 case MXT_TOUCH_PROXKEY_T52:
340 case MXT_PROCI_GRIPFACE_T20:
341 case MXT_PROCG_NOISE_T22:
342 case MXT_PROCI_ONETOUCH_T24:
343 case MXT_PROCI_TWOTOUCH_T27:
344 case MXT_PROCI_GRIP_T40:
345 case MXT_PROCI_PALM_T41:
346 case MXT_PROCI_TOUCHSUPPRESSION_T42:
347 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800348 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800349 case MXT_PROCG_NOISESUPPRESSION_T48:
350 case MXT_SPT_COMMSCONFIG_T18:
351 case MXT_SPT_GPIOPWM_T19:
352 case MXT_SPT_SELFTEST_T25:
353 case MXT_SPT_CTECONFIG_T28:
354 case MXT_SPT_USERDATA_T38:
355 case MXT_SPT_DIGITIZER_T43:
356 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700357 return true;
358 default:
359 return false;
360 }
361}
362
Iiro Valkonen7686b102011-02-02 23:21:58 -0800363static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700364{
365 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800366 case MXT_GEN_COMMAND_T6:
367 case MXT_GEN_POWER_T7:
368 case MXT_GEN_ACQUIRE_T8:
369 case MXT_TOUCH_MULTI_T9:
370 case MXT_TOUCH_KEYARRAY_T15:
371 case MXT_TOUCH_PROXIMITY_T23:
372 case MXT_TOUCH_PROXKEY_T52:
373 case MXT_PROCI_GRIPFACE_T20:
374 case MXT_PROCG_NOISE_T22:
375 case MXT_PROCI_ONETOUCH_T24:
376 case MXT_PROCI_TWOTOUCH_T27:
377 case MXT_PROCI_GRIP_T40:
378 case MXT_PROCI_PALM_T41:
379 case MXT_PROCI_TOUCHSUPPRESSION_T42:
380 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800381 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800382 case MXT_PROCG_NOISESUPPRESSION_T48:
383 case MXT_SPT_COMMSCONFIG_T18:
384 case MXT_SPT_GPIOPWM_T19:
385 case MXT_SPT_SELFTEST_T25:
386 case MXT_SPT_CTECONFIG_T28:
387 case MXT_SPT_USERDATA_T38:
388 case MXT_SPT_DIGITIZER_T43:
389 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700390 return true;
391 default:
392 return false;
393 }
394}
395
Iiro Valkonen7686b102011-02-02 23:21:58 -0800396static void mxt_dump_message(struct device *dev,
397 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700398{
399 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
400 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
401 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
402 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
403 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
404 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
405 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
406 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
407 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
408}
409
Iiro Valkonen7686b102011-02-02 23:21:58 -0800410static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700411 unsigned int state)
412{
413 u8 val;
414
415recheck:
416 if (i2c_master_recv(client, &val, 1) != 1) {
417 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
418 return -EIO;
419 }
420
421 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800422 case MXT_WAITING_BOOTLOAD_CMD:
423 case MXT_WAITING_FRAME_DATA:
424 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700425 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800426 case MXT_FRAME_CRC_PASS:
427 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700428 goto recheck;
429 break;
430 default:
431 return -EINVAL;
432 }
433
434 if (val != state) {
435 dev_err(&client->dev, "Unvalid bootloader mode state\n");
436 return -EINVAL;
437 }
438
439 return 0;
440}
441
Iiro Valkonen7686b102011-02-02 23:21:58 -0800442static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700443{
444 u8 buf[2];
445
Iiro Valkonen7686b102011-02-02 23:21:58 -0800446 buf[0] = MXT_UNLOCK_CMD_LSB;
447 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700448
449 if (i2c_master_send(client, buf, 2) != 2) {
450 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
451 return -EIO;
452 }
453
454 return 0;
455}
456
Iiro Valkonen7686b102011-02-02 23:21:58 -0800457static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700458 const u8 *data, unsigned int frame_size)
459{
460 if (i2c_master_send(client, data, frame_size) != frame_size) {
461 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
462 return -EIO;
463 }
464
465 return 0;
466}
467
Iiro Valkonen7686b102011-02-02 23:21:58 -0800468static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700469 u16 reg, u16 len, void *val)
470{
471 struct i2c_msg xfer[2];
472 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700473 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700474
475 buf[0] = reg & 0xff;
476 buf[1] = (reg >> 8) & 0xff;
477
478 /* Write register */
479 xfer[0].addr = client->addr;
480 xfer[0].flags = 0;
481 xfer[0].len = 2;
482 xfer[0].buf = buf;
483
484 /* Read data */
485 xfer[1].addr = client->addr;
486 xfer[1].flags = I2C_M_RD;
487 xfer[1].len = len;
488 xfer[1].buf = val;
489
Jing Lin36aee812011-10-17 17:17:28 -0700490 do {
491 if (i2c_transfer(client->adapter, xfer, 2) == 2)
492 return 0;
493 msleep(MXT_WAKE_TIME);
494 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495
Jing Lin36aee812011-10-17 17:17:28 -0700496 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
497 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498}
499
Iiro Valkonen7686b102011-02-02 23:21:58 -0800500static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700501{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800502 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700503}
504
Jing Lin36aee812011-10-17 17:17:28 -0700505static int __mxt_write_reg(struct i2c_client *client,
506 u16 addr, u16 length, u8 *value)
507{
508 u8 buf[MXT_BLOCK_SIZE + 2];
509 int i, tries = 0;
510
511 if (length > MXT_BLOCK_SIZE)
512 return -EINVAL;
513
514 buf[0] = addr & 0xff;
515 buf[1] = (addr >> 8) & 0xff;
516 for (i = 0; i < length; i++)
517 buf[i + 2] = *value++;
518
519 do {
520 if (i2c_master_send(client, buf, length + 2) == (length + 2))
521 return 0;
522 msleep(MXT_WAKE_TIME);
523 } while (++tries < MXT_MAX_RW_TRIES);
524
525 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
526 return -EIO;
527}
528
Iiro Valkonen7686b102011-02-02 23:21:58 -0800529static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530{
Jing Lin36aee812011-10-17 17:17:28 -0700531 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532}
533
Iiro Valkonen7686b102011-02-02 23:21:58 -0800534static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700535 u16 reg, u8 *object_buf)
536{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538 object_buf);
539}
540
Iiro Valkonen7686b102011-02-02 23:21:58 -0800541static struct mxt_object *
542mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700543{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800544 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545 int i;
546
547 for (i = 0; i < data->info.object_num; i++) {
548 object = data->object_table + i;
549 if (object->type == type)
550 return object;
551 }
552
553 dev_err(&data->client->dev, "Invalid object type\n");
554 return NULL;
555}
556
Iiro Valkonen7686b102011-02-02 23:21:58 -0800557static int mxt_read_message(struct mxt_data *data,
558 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700559{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561 u16 reg;
562
Iiro Valkonene8645592011-11-18 12:56:19 -0800563 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564 if (!object)
565 return -EINVAL;
566
567 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800568 return __mxt_read_reg(data->client, reg,
569 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700570}
571
Iiro Valkonen7686b102011-02-02 23:21:58 -0800572static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700573 u8 type, u8 offset, u8 *val)
574{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800575 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700576 u16 reg;
577
Iiro Valkonen7686b102011-02-02 23:21:58 -0800578 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700579 if (!object)
580 return -EINVAL;
581
582 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800583 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700584}
585
Iiro Valkonen7686b102011-02-02 23:21:58 -0800586static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700587 u8 type, u8 offset, u8 val)
588{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590 u16 reg;
591
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 if (!object)
594 return -EINVAL;
595
596 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800597 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700598}
599
Iiro Valkonen7686b102011-02-02 23:21:58 -0800600static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800602 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603 struct input_dev *input_dev = data->input_dev;
604 int status = finger[single_id].status;
605 int finger_num = 0;
606 int id;
607
Iiro Valkonen7686b102011-02-02 23:21:58 -0800608 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609 if (!finger[id].status)
610 continue;
611
Amy Maloche2b59bab2011-10-13 16:08:16 -0700612 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
613 finger[id].status != MXT_RELEASE ?
614 finger[id].area : 0);
615 input_report_abs(input_dev, ABS_MT_POSITION_X,
616 finger[id].x);
617 input_report_abs(input_dev, ABS_MT_POSITION_Y,
618 finger[id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700619 input_report_abs(input_dev, ABS_MT_PRESSURE,
Mohan Pallaka5e7343f2012-01-02 18:30:16 +0800620 finger[id].status != MXT_RELEASE ?
621 finger[id].pressure : 0);
Amy Maloche2b59bab2011-10-13 16:08:16 -0700622 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700623
Amy Maloche2b59bab2011-10-13 16:08:16 -0700624 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700625 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700626 else
627 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700628 }
629
630 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
631
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700633 input_report_abs(input_dev, ABS_X, finger[single_id].x);
634 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700635 input_report_abs(input_dev,
636 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637 }
638
639 input_sync(input_dev);
640}
641
Iiro Valkonen7686b102011-02-02 23:21:58 -0800642static void mxt_input_touchevent(struct mxt_data *data,
643 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700644{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646 struct device *dev = &data->client->dev;
647 u8 status = message->message[0];
648 int x;
649 int y;
650 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700651 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700652
653 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800654 if (!(status & MXT_DETECT)) {
655 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656 dev_dbg(dev, "[%d] released\n", id);
657
Iiro Valkonen7686b102011-02-02 23:21:58 -0800658 finger[id].status = MXT_RELEASE;
659 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660 }
661 return;
662 }
663
664 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 return;
667
Joonyoung Shim910d8052011-04-12 23:14:38 -0700668 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
669 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700670 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700671 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700672 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700673 y = y >> 2;
674
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700676 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677
678 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800679 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680 x, y, area);
681
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682 finger[id].status = status & MXT_MOVE ?
683 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684 finger[id].x = x;
685 finger[id].y = y;
686 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700687 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700688
Iiro Valkonen7686b102011-02-02 23:21:58 -0800689 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700690}
691
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800692static void mxt_handle_key_array(struct mxt_data *data,
693 struct mxt_message *message)
694{
695 u32 keys_changed;
696 int i;
697
698 if (!data->pdata->key_codes) {
699 dev_err(&data->client->dev, "keyarray is not supported\n");
700 return;
701 }
702
703 data->keyarray_new = message->message[1] |
704 (message->message[2] << 8) |
705 (message->message[3] << 16) |
706 (message->message[4] << 24);
707
708 keys_changed = data->keyarray_old ^ data->keyarray_new;
709
710 if (!keys_changed) {
711 dev_dbg(&data->client->dev, "no keys changed\n");
712 return;
713 }
714
715 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
716 if (!(keys_changed & (1 << i)))
717 continue;
718
719 input_report_key(data->input_dev, data->pdata->key_codes[i],
720 (data->keyarray_new & (1 << i)));
721 input_sync(data->input_dev);
722 }
723
724 data->keyarray_old = data->keyarray_new;
725}
726
Iiro Valkonen7686b102011-02-02 23:21:58 -0800727static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729 struct mxt_data *data = dev_id;
730 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731 struct device *dev = &data->client->dev;
732 int id;
733 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734
735 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800736 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700737 dev_err(dev, "Failed to read message\n");
738 goto end;
739 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700740 reportid = message.reportid;
741
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800742 if (!reportid) {
743 dev_dbg(dev, "Report id 0 is reserved\n");
744 continue;
745 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700746
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800747 /* check whether report id is part of T9 or T15 */
748 id = reportid - data->t9_min_reportid;
749
750 if (reportid >= data->t9_min_reportid &&
751 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800752 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800753 else if (reportid >= data->t15_min_reportid &&
754 reportid <= data->t15_max_reportid)
755 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800757 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758 } while (reportid != 0xff);
759
760end:
761 return IRQ_HANDLED;
762}
763
Iiro Valkonen7686b102011-02-02 23:21:58 -0800764static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700765{
Jing Lindc4413c2012-01-16 15:22:52 -0800766 const struct mxt_config_info *config_info = data->config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800767 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700768 struct device *dev = &data->client->dev;
769 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800770 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771
Jing Lindc4413c2012-01-16 15:22:52 -0800772 if (!config_info) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800773 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
774 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700775 }
776
777 for (i = 0; i < data->info.object_num; i++) {
778 object = data->object_table + i;
779
Iiro Valkonen7686b102011-02-02 23:21:58 -0800780 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700781 continue;
782
Iiro Valkonen71749f52011-02-15 13:36:52 -0800783 for (j = 0; j < object->size + 1; j++) {
784 config_offset = index + j;
Jing Lindc4413c2012-01-16 15:22:52 -0800785 if (config_offset > config_info->config_length) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800786 dev_err(dev, "Not enough config data!\n");
787 return -EINVAL;
788 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800789 mxt_write_object(data, object->type, j,
Jing Lindc4413c2012-01-16 15:22:52 -0800790 config_info->config[config_offset]);
Iiro Valkonen71749f52011-02-15 13:36:52 -0800791 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700792 index += object->size + 1;
793 }
794
795 return 0;
796}
797
Iiro Valkonen7686b102011-02-02 23:21:58 -0800798static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700799{
800 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800801 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700802 int count = 10;
803 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700804
805 /* Read dummy message to make high CHG pin */
806 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800807 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700808 if (error)
809 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800810 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811
812 if (!count) {
813 dev_err(dev, "CHG pin isn't cleared\n");
814 return -EBUSY;
815 }
816
817 return 0;
818}
819
Iiro Valkonen7686b102011-02-02 23:21:58 -0800820static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700821{
822 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800823 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824 int error;
825 u8 val;
826
Iiro Valkonen7686b102011-02-02 23:21:58 -0800827 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700828 if (error)
829 return error;
830 info->family_id = val;
831
Iiro Valkonen7686b102011-02-02 23:21:58 -0800832 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700833 if (error)
834 return error;
835 info->variant_id = val;
836
Iiro Valkonen7686b102011-02-02 23:21:58 -0800837 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700838 if (error)
839 return error;
840 info->version = val;
841
Iiro Valkonen7686b102011-02-02 23:21:58 -0800842 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700843 if (error)
844 return error;
845 info->build = val;
846
Iiro Valkonen7686b102011-02-02 23:21:58 -0800847 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700848 if (error)
849 return error;
850 info->object_num = val;
851
852 return 0;
853}
854
Iiro Valkonen7686b102011-02-02 23:21:58 -0800855static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856{
857 int error;
858 int i;
859 u16 reg;
860 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800861 u8 buf[MXT_OBJECT_SIZE];
Jing Lindc4413c2012-01-16 15:22:52 -0800862 bool found_t38 = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700863
864 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800865 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700866
Iiro Valkonen7686b102011-02-02 23:21:58 -0800867 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
868 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700869 if (error)
870 return error;
871
872 object->type = buf[0];
873 object->start_address = (buf[2] << 8) | buf[1];
874 object->size = buf[3];
875 object->instances = buf[4];
876 object->num_report_ids = buf[5];
877
878 if (object->num_report_ids) {
879 reportid += object->num_report_ids *
880 (object->instances + 1);
881 object->max_reportid = reportid;
882 }
Jing Lindc4413c2012-01-16 15:22:52 -0800883
884 /* Calculate index for config major version in config array.
885 * Major version is the first byte in object T38.
886 */
887 if (object->type == MXT_SPT_USERDATA_T38)
888 found_t38 = true;
889 if (!found_t38 && mxt_object_writable(object->type))
890 data->cfg_version_idx += object->size + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700891 }
892
893 return 0;
894}
895
Jing Lindc4413c2012-01-16 15:22:52 -0800896static int mxt_search_config_array(struct mxt_data *data, bool version_match)
897{
898
899 const struct mxt_platform_data *pdata = data->pdata;
900 const struct mxt_config_info *cfg_info;
901 struct mxt_info *info = &data->info;
902 int i;
903 u8 cfg_version;
904
905 for (i = 0; i < pdata->config_array_size; i++) {
906
907 cfg_info = &pdata->config_array[i];
908
909 if (!cfg_info->config || !cfg_info->config_length)
910 continue;
911
912 if (info->family_id == cfg_info->family_id &&
913 info->variant_id == cfg_info->variant_id &&
914 info->version == cfg_info->version &&
915 info->build == cfg_info->build) {
916
917 cfg_version = cfg_info->config[data->cfg_version_idx];
918 if (data->curr_cfg_version == cfg_version ||
919 !version_match) {
920 data->config_info = cfg_info;
921 return 0;
922 }
923 }
924 }
925
926 dev_info(&data->client->dev,
927 "Config not found: F: %d, V: %d, FW: %d.%d.%d, CFG: %d\n",
928 info->family_id, info->variant_id,
929 info->version >> 4, info->version & 0xF, info->build,
930 data->curr_cfg_version);
931 return -EINVAL;
932}
933
934static int mxt_get_config(struct mxt_data *data)
935{
936 const struct mxt_platform_data *pdata = data->pdata;
937 struct device *dev = &data->client->dev;
938 struct mxt_object *object;
939 int error;
940
941 if (!pdata->config_array || !pdata->config_array_size) {
942 dev_dbg(dev, "No cfg data provided by platform data\n");
943 return 0;
944 }
945
946 /* Get current config version */
947 object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
948 if (!object) {
949 dev_err(dev, "Unable to obtain USERDATA object\n");
950 return -EINVAL;
951 }
952
953 error = mxt_read_reg(data->client, object->start_address,
954 &data->curr_cfg_version);
955 if (error) {
956 dev_err(dev, "Unable to read config version\n");
957 return error;
958 }
959
960 /* It is possible that the config data on the controller is not
961 * versioned and the version number returns 0. In this case,
962 * find a match without the config version checking.
963 */
964 error = mxt_search_config_array(data,
965 data->curr_cfg_version != 0 ? true : false);
966 if (error)
967 return error;
968
969 return 0;
970}
Amy Maloche7e447432011-09-14 11:36:30 -0700971static void mxt_reset_delay(struct mxt_data *data)
972{
973 struct mxt_info *info = &data->info;
974
975 switch (info->family_id) {
976 case MXT224_ID:
977 msleep(MXT224_RESET_TIME);
978 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -0700979 case MXT224E_ID:
980 msleep(MXT224E_RESET_TIME);
981 break;
Amy Maloche7e447432011-09-14 11:36:30 -0700982 case MXT1386_ID:
983 msleep(MXT1386_RESET_TIME);
984 break;
985 default:
986 msleep(MXT_RESET_TIME);
987 }
988}
989
Iiro Valkonen7686b102011-02-02 23:21:58 -0800990static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991{
992 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800993 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700994 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700995 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700997 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700998 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800999 struct mxt_object *t9_object;
1000 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003 if (error)
1004 return error;
1005
1006 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001007 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001008 GFP_KERNEL);
1009 if (!data->object_table) {
1010 dev_err(&client->dev, "Failed to allocate memory\n");
1011 return -ENOMEM;
1012 }
1013
1014 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001015 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001017 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
Jing Lindc4413c2012-01-16 15:22:52 -08001019 /* Get config data from platform data */
1020 error = mxt_get_config(data);
1021 if (error)
1022 dev_dbg(&client->dev, "Config info not found.\n");
1023
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001024 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001025 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001026 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001027 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001028
Amy Maloche52262212011-09-15 16:46:57 -07001029 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -08001030 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -07001031 if (!t7_object) {
1032 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -07001033 error = -EINVAL;
1034 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -07001035 }
1036
1037 data->t7_start_addr = t7_object->start_address;
1038 error = __mxt_read_reg(client, data->t7_start_addr,
1039 T7_DATA_SIZE, data->t7_data);
1040 if (error < 0) {
1041 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -07001042 "Failed to save current power state\n");
1043 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001044 }
Iiro Valkonene8645592011-11-18 12:56:19 -08001045 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -07001046 &data->t9_ctrl);
1047 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -07001048 dev_err(&client->dev, "Failed to save current touch object\n");
1049 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001050 }
1051
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001052 /* Store T9, T15's min and max report ids */
1053 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1054 if (!t9_object) {
1055 dev_err(&client->dev, "Failed to get T9 object\n");
1056 error = -EINVAL;
1057 goto free_object_table;
1058 }
1059 data->t9_max_reportid = t9_object->max_reportid;
1060 data->t9_min_reportid = t9_object->max_reportid -
1061 t9_object->num_report_ids + 1;
1062
1063 if (data->pdata->key_codes) {
1064 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
1065 if (!t15_object)
1066 dev_dbg(&client->dev, "T15 object is not available\n");
1067 else {
1068 data->t15_max_reportid = t15_object->max_reportid;
1069 data->t15_min_reportid = t15_object->max_reportid -
1070 t15_object->num_report_ids + 1;
1071 }
1072 }
1073
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -08001075 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001076 MXT_COMMAND_BACKUPNV,
1077 MXT_BACKUP_VALUE);
1078 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001079 do {
Iiro Valkonene8645592011-11-18 12:56:19 -08001080 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001081 MXT_COMMAND_BACKUPNV,
1082 &command_register);
1083 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001084 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -07001085 usleep_range(1000, 2000);
1086 } while ((command_register != 0) && (++timeout_counter <= 100));
1087 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001088 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -07001089 error = -EIO;
1090 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001091 }
1092
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093
1094 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -08001095 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001096 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001097
Amy Maloche7e447432011-09-14 11:36:30 -07001098 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001099
1100 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001101 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001102 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001103 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104 info->matrix_xsize = val;
1105
Iiro Valkonen7686b102011-02-02 23:21:58 -08001106 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001107 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001108 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109 info->matrix_ysize = val;
1110
1111 dev_info(&client->dev,
1112 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
1113 info->family_id, info->variant_id, info->version,
1114 info->build);
1115
1116 dev_info(&client->dev,
1117 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
1118 info->matrix_xsize, info->matrix_ysize,
1119 info->object_num);
1120
1121 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001122
1123free_object_table:
1124 kfree(data->object_table);
1125 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001126}
1127
Iiro Valkonen7686b102011-02-02 23:21:58 -08001128static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129 struct device_attribute *attr, char *buf)
1130{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001131 struct mxt_data *data = dev_get_drvdata(dev);
1132 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001133 int count = 0;
1134 int i, j;
1135 int error;
1136 u8 val;
1137
1138 for (i = 0; i < data->info.object_num; i++) {
1139 object = data->object_table + i;
1140
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001141 count += snprintf(buf + count, PAGE_SIZE - count,
1142 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001144 if (count >= PAGE_SIZE)
1145 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146
Iiro Valkonen7686b102011-02-02 23:21:58 -08001147 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001148 count += snprintf(buf + count, PAGE_SIZE - count,
1149 "\n");
1150 if (count >= PAGE_SIZE)
1151 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001152 continue;
1153 }
1154
1155 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001156 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001157 object->type, j, &val);
1158 if (error)
1159 return error;
1160
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001161 count += snprintf(buf + count, PAGE_SIZE - count,
1162 "\t[%2d]: %02x (%d)\n", j, val, val);
1163 if (count >= PAGE_SIZE)
1164 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001165 }
1166
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001167 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1168 if (count >= PAGE_SIZE)
1169 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001170 }
1171
1172 return count;
1173}
1174
Iiro Valkonen7686b102011-02-02 23:21:58 -08001175static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001177 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001178 struct i2c_client *client = data->client;
1179 const struct firmware *fw = NULL;
1180 unsigned int frame_size;
1181 unsigned int pos = 0;
1182 int ret;
1183
1184 ret = request_firmware(&fw, fn, dev);
1185 if (ret) {
1186 dev_err(dev, "Unable to open firmware %s\n", fn);
1187 return ret;
1188 }
1189
1190 /* Change to the bootloader mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001191 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001192 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001193
1194 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001195
1196 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001197 if (client->addr == MXT_APP_LOW)
1198 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001199 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001200 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201
Iiro Valkonen7686b102011-02-02 23:21:58 -08001202 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001203 if (ret)
1204 goto out;
1205
1206 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001207 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001208
1209 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001210 ret = mxt_check_bootloader(client,
1211 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001212 if (ret)
1213 goto out;
1214
1215 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1216
1217 /* We should add 2 at frame size as the the firmware data is not
1218 * included the CRC bytes.
1219 */
1220 frame_size += 2;
1221
1222 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001223 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001224
Iiro Valkonen7686b102011-02-02 23:21:58 -08001225 ret = mxt_check_bootloader(client,
1226 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001227 if (ret)
1228 goto out;
1229
1230 pos += frame_size;
1231
1232 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1233 }
1234
1235out:
1236 release_firmware(fw);
1237
1238 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001239 if (client->addr == MXT_BOOT_LOW)
1240 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001241 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001242 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001243
1244 return ret;
1245}
1246
Iiro Valkonen7686b102011-02-02 23:21:58 -08001247static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001248 struct device_attribute *attr,
1249 const char *buf, size_t count)
1250{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001251 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001252 int error;
1253
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001254 disable_irq(data->irq);
1255
Iiro Valkonen7686b102011-02-02 23:21:58 -08001256 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001257 if (error) {
1258 dev_err(dev, "The firmware update failed(%d)\n", error);
1259 count = error;
1260 } else {
1261 dev_dbg(dev, "The firmware update succeeded\n");
1262
1263 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001264 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265
1266 kfree(data->object_table);
1267 data->object_table = NULL;
1268
Iiro Valkonen7686b102011-02-02 23:21:58 -08001269 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001270 }
1271
1272 enable_irq(data->irq);
1273
Iiro Valkonen08960a02011-04-12 23:16:40 -07001274 error = mxt_make_highchg(data);
1275 if (error)
1276 return error;
1277
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001278 return count;
1279}
1280
Iiro Valkonen7686b102011-02-02 23:21:58 -08001281static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1282static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001283
Iiro Valkonen7686b102011-02-02 23:21:58 -08001284static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001285 &dev_attr_object.attr,
1286 &dev_attr_update_fw.attr,
1287 NULL
1288};
1289
Iiro Valkonen7686b102011-02-02 23:21:58 -08001290static const struct attribute_group mxt_attr_group = {
1291 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001292};
1293
Amy Maloche52262212011-09-15 16:46:57 -07001294static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001295{
Jing Lin36aee812011-10-17 17:17:28 -07001296 int error;
1297
Amy Maloche52262212011-09-15 16:46:57 -07001298 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001299 error = __mxt_write_reg(data->client, data->t7_start_addr,
1300 T7_DATA_SIZE, data->t7_data);
1301 if (error < 0) {
1302 dev_err(&data->client->dev,
1303 "failed to restore old power state\n");
1304 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001305 }
Jing Lin36aee812011-10-17 17:17:28 -07001306
Amy Maloche52262212011-09-15 16:46:57 -07001307 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001308 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001309 if (error < 0) {
1310 dev_err(&data->client->dev, "failed to restore touch\n");
1311 return error;
1312 }
1313
1314 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001315}
1316
Amy Maloche52262212011-09-15 16:46:57 -07001317static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001318{
Jing Lin36aee812011-10-17 17:17:28 -07001319 int error;
1320 u8 t7_data[T7_DATA_SIZE] = {0};
1321
1322 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001323 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001324 if (error < 0) {
1325 dev_err(&data->client->dev, "failed to disable touch\n");
1326 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001327 }
1328
Jing Lin36aee812011-10-17 17:17:28 -07001329 error = __mxt_write_reg(data->client, data->t7_start_addr,
1330 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001331 if (error < 0) {
1332 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001333 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001334 return error;
1335 }
1336
1337 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001338}
1339
Iiro Valkonen7686b102011-02-02 23:21:58 -08001340static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001341{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001342 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001343 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001344
Amy Maloche52262212011-09-15 16:46:57 -07001345 error = mxt_start(data);
1346 if (error < 0) {
1347 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1348 return error;
1349 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001350
1351 return 0;
1352}
1353
Iiro Valkonen7686b102011-02-02 23:21:58 -08001354static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001355{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001356 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001357 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358
Amy Maloche52262212011-09-15 16:46:57 -07001359 error = mxt_stop(data);
1360 if (error < 0)
1361 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1362
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001363}
1364
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301365static int mxt_power_on(struct mxt_data *data, bool on)
1366{
1367 int rc;
1368
1369 if (on == false)
1370 goto power_off;
1371
Amy Maloche21115eb2011-11-02 09:04:37 -07001372 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301373 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001374 dev_err(&data->client->dev,
1375 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301376 return rc;
1377 }
1378
Amy Maloche21115eb2011-11-02 09:04:37 -07001379 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301380 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001381 dev_err(&data->client->dev,
1382 "Regulator vcc_ana enable failed rc=%d\n", rc);
1383 goto error_reg_en_vcc_ana;
1384 }
1385
1386 if (data->pdata->digital_pwr_regulator) {
1387 rc = regulator_set_optimum_mode(data->vcc_dig,
1388 MXT_ACTIVE_LOAD_DIG_UA);
1389 if (rc < 0) {
1390 dev_err(&data->client->dev,
1391 "Regulator vcc_dig set_opt failed rc=%d\n",
1392 rc);
1393 goto error_reg_opt_vcc_dig;
1394 }
1395
1396 rc = regulator_enable(data->vcc_dig);
1397 if (rc) {
1398 dev_err(&data->client->dev,
1399 "Regulator vcc_dig enable failed rc=%d\n", rc);
1400 goto error_reg_en_vcc_dig;
1401 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301402 }
1403
1404 if (data->pdata->i2c_pull_up) {
1405 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1406 if (rc < 0) {
1407 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001408 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301409 goto error_reg_opt_i2c;
1410 }
1411
1412 rc = regulator_enable(data->vcc_i2c);
1413 if (rc) {
1414 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001415 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301416 goto error_reg_en_vcc_i2c;
1417 }
1418 }
1419
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001420 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301421
1422 return 0;
1423
1424error_reg_en_vcc_i2c:
1425 if (data->pdata->i2c_pull_up)
1426 regulator_set_optimum_mode(data->vcc_i2c, 0);
1427error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001428 if (data->pdata->digital_pwr_regulator)
1429 regulator_disable(data->vcc_dig);
1430error_reg_en_vcc_dig:
1431 if (data->pdata->digital_pwr_regulator)
1432 regulator_set_optimum_mode(data->vcc_dig, 0);
1433error_reg_opt_vcc_dig:
1434 regulator_disable(data->vcc_ana);
1435error_reg_en_vcc_ana:
1436 regulator_set_optimum_mode(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301437 return rc;
1438
1439power_off:
Amy Maloche21115eb2011-11-02 09:04:37 -07001440 regulator_set_optimum_mode(data->vcc_ana, 0);
1441 regulator_disable(data->vcc_ana);
1442 if (data->pdata->digital_pwr_regulator) {
1443 regulator_set_optimum_mode(data->vcc_dig, 0);
1444 regulator_disable(data->vcc_dig);
1445 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301446 if (data->pdata->i2c_pull_up) {
1447 regulator_set_optimum_mode(data->vcc_i2c, 0);
1448 regulator_disable(data->vcc_i2c);
1449 }
1450 msleep(50);
1451 return 0;
1452}
1453
1454static int mxt_regulator_configure(struct mxt_data *data, bool on)
1455{
1456 int rc;
1457
1458 if (on == false)
1459 goto hw_shutdown;
1460
Amy Maloche21115eb2011-11-02 09:04:37 -07001461 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1462 if (IS_ERR(data->vcc_ana)) {
1463 rc = PTR_ERR(data->vcc_ana);
1464 dev_err(&data->client->dev,
1465 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301466 return rc;
1467 }
1468
Amy Maloche21115eb2011-11-02 09:04:37 -07001469 if (regulator_count_voltages(data->vcc_ana) > 0) {
1470 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301471 MXT_VTG_MAX_UV);
1472 if (rc) {
1473 dev_err(&data->client->dev,
1474 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001475 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301476 }
1477 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001478 if (data->pdata->digital_pwr_regulator) {
1479 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1480 if (IS_ERR(data->vcc_dig)) {
1481 rc = PTR_ERR(data->vcc_dig);
1482 dev_err(&data->client->dev,
1483 "Regulator get dig failed rc=%d\n", rc);
1484 goto error_get_vtg_vcc_dig;
1485 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301486
Amy Maloche21115eb2011-11-02 09:04:37 -07001487 if (regulator_count_voltages(data->vcc_dig) > 0) {
1488 rc = regulator_set_voltage(data->vcc_dig,
1489 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1490 if (rc) {
1491 dev_err(&data->client->dev,
1492 "regulator set_vtg failed rc=%d\n", rc);
1493 goto error_set_vtg_vcc_dig;
1494 }
1495 }
1496 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301497 if (data->pdata->i2c_pull_up) {
1498 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1499 if (IS_ERR(data->vcc_i2c)) {
1500 rc = PTR_ERR(data->vcc_i2c);
1501 dev_err(&data->client->dev,
1502 "Regulator get failed rc=%d\n", rc);
1503 goto error_get_vtg_i2c;
1504 }
1505 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1506 rc = regulator_set_voltage(data->vcc_i2c,
1507 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1508 if (rc) {
1509 dev_err(&data->client->dev,
1510 "regulator set_vtg failed rc=%d\n", rc);
1511 goto error_set_vtg_i2c;
1512 }
1513 }
1514 }
1515
1516 return 0;
1517
1518error_set_vtg_i2c:
1519 regulator_put(data->vcc_i2c);
1520error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001521 if (data->pdata->digital_pwr_regulator)
1522 if (regulator_count_voltages(data->vcc_dig) > 0)
1523 regulator_set_voltage(data->vcc_dig, 0,
1524 MXT_VTG_DIG_MAX_UV);
1525error_set_vtg_vcc_dig:
1526 if (data->pdata->digital_pwr_regulator)
1527 regulator_put(data->vcc_dig);
1528error_get_vtg_vcc_dig:
1529 if (regulator_count_voltages(data->vcc_ana) > 0)
1530 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1531error_set_vtg_vcc_ana:
1532 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301533 return rc;
1534
1535hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001536 if (regulator_count_voltages(data->vcc_ana) > 0)
1537 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1538 regulator_put(data->vcc_ana);
1539 if (data->pdata->digital_pwr_regulator) {
1540 if (regulator_count_voltages(data->vcc_dig) > 0)
1541 regulator_set_voltage(data->vcc_dig, 0,
1542 MXT_VTG_DIG_MAX_UV);
1543 regulator_put(data->vcc_dig);
1544 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301545 if (data->pdata->i2c_pull_up) {
1546 if (regulator_count_voltages(data->vcc_i2c) > 0)
1547 regulator_set_voltage(data->vcc_i2c, 0,
1548 MXT_I2C_VTG_MAX_UV);
1549 regulator_put(data->vcc_i2c);
1550 }
1551 return 0;
1552}
1553
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301554#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001555static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1556{
1557
1558 int rc;
1559
1560 if (on == false)
1561 goto regulator_hpm;
1562
Amy Maloche21115eb2011-11-02 09:04:37 -07001563 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001564 if (rc < 0) {
1565 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001566 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001567 goto fail_regulator_lpm;
1568 }
1569
Amy Maloche21115eb2011-11-02 09:04:37 -07001570 if (data->pdata->digital_pwr_regulator) {
1571 rc = regulator_set_optimum_mode(data->vcc_dig,
1572 MXT_LPM_LOAD_DIG_UA);
1573 if (rc < 0) {
1574 dev_err(&data->client->dev,
1575 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1576 goto fail_regulator_lpm;
1577 }
1578 }
1579
Jing Linbace50b2011-10-18 22:55:47 -07001580 if (data->pdata->i2c_pull_up) {
1581 rc = regulator_set_optimum_mode(data->vcc_i2c,
1582 MXT_I2C_LPM_LOAD_UA);
1583 if (rc < 0) {
1584 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001585 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001586 goto fail_regulator_lpm;
1587 }
1588 }
1589
1590 return 0;
1591
1592regulator_hpm:
1593
Amy Maloche21115eb2011-11-02 09:04:37 -07001594 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001595 if (rc < 0) {
1596 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001597 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001598 goto fail_regulator_hpm;
1599 }
1600
Amy Maloche21115eb2011-11-02 09:04:37 -07001601 if (data->pdata->digital_pwr_regulator) {
1602 rc = regulator_set_optimum_mode(data->vcc_dig,
1603 MXT_ACTIVE_LOAD_DIG_UA);
1604 if (rc < 0) {
1605 dev_err(&data->client->dev,
1606 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1607 goto fail_regulator_hpm;
1608 }
1609 }
1610
Jing Linbace50b2011-10-18 22:55:47 -07001611 if (data->pdata->i2c_pull_up) {
1612 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1613 if (rc < 0) {
1614 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001615 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001616 goto fail_regulator_hpm;
1617 }
1618 }
1619
1620 return 0;
1621
1622fail_regulator_lpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001623 regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
1624 if (data->pdata->digital_pwr_regulator)
1625 regulator_set_optimum_mode(data->vcc_dig,
1626 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001627 if (data->pdata->i2c_pull_up)
1628 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1629
1630 return rc;
1631
1632fail_regulator_hpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001633 regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
1634 if (data->pdata->digital_pwr_regulator)
1635 regulator_set_optimum_mode(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001636 if (data->pdata->i2c_pull_up)
1637 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1638
1639 return rc;
1640}
1641
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301642static int mxt_suspend(struct device *dev)
1643{
1644 struct i2c_client *client = to_i2c_client(dev);
1645 struct mxt_data *data = i2c_get_clientdata(client);
1646 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001647 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301648
1649 mutex_lock(&input_dev->mutex);
1650
Amy Maloche52262212011-09-15 16:46:57 -07001651 if (input_dev->users) {
1652 error = mxt_stop(data);
1653 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001654 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001655 mutex_unlock(&input_dev->mutex);
1656 return error;
1657 }
1658
1659 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301660
1661 mutex_unlock(&input_dev->mutex);
1662
Jing Linbace50b2011-10-18 22:55:47 -07001663 /* put regulators in low power mode */
1664 error = mxt_regulator_lpm(data, true);
1665 if (error < 0) {
1666 dev_err(dev, "failed to enter low power mode\n");
1667 return error;
1668 }
1669
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301670 return 0;
1671}
1672
1673static int mxt_resume(struct device *dev)
1674{
1675 struct i2c_client *client = to_i2c_client(dev);
1676 struct mxt_data *data = i2c_get_clientdata(client);
1677 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001678 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301679
Jing Linbace50b2011-10-18 22:55:47 -07001680 /* put regulators in high power mode */
1681 error = mxt_regulator_lpm(data, false);
1682 if (error < 0) {
1683 dev_err(dev, "failed to enter high power mode\n");
1684 return error;
1685 }
1686
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301687 mutex_lock(&input_dev->mutex);
1688
Amy Maloche52262212011-09-15 16:46:57 -07001689 if (input_dev->users) {
1690 error = mxt_start(data);
1691 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001692 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001693 mutex_unlock(&input_dev->mutex);
1694 return error;
1695 }
1696 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301697
1698 mutex_unlock(&input_dev->mutex);
1699
1700 return 0;
1701}
1702
1703#if defined(CONFIG_HAS_EARLYSUSPEND)
1704static void mxt_early_suspend(struct early_suspend *h)
1705{
1706 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1707
1708 mxt_suspend(&data->client->dev);
1709}
1710
1711static void mxt_late_resume(struct early_suspend *h)
1712{
1713 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1714
1715 mxt_resume(&data->client->dev);
1716}
1717#endif
1718
1719static const struct dev_pm_ops mxt_pm_ops = {
1720#ifndef CONFIG_HAS_EARLYSUSPEND
1721 .suspend = mxt_suspend,
1722 .resume = mxt_resume,
1723#endif
1724};
1725#endif
1726
Jing Lin6cfc00e2011-11-02 15:15:30 -07001727static int mxt_debugfs_object_show(struct seq_file *m, void *v)
1728{
1729 struct mxt_data *data = m->private;
1730 struct mxt_object *object;
1731 struct device *dev = &data->client->dev;
1732 int i, j, k;
1733 int error;
1734 int obj_size;
1735 u8 val;
1736
1737 for (i = 0; i < data->info.object_num; i++) {
1738 object = data->object_table + i;
1739 obj_size = object->size + 1;
1740
1741 seq_printf(m, "Object[%d] (Type %d)\n", i + 1, object->type);
1742
1743 for (j = 0; j < object->instances + 1; j++) {
1744 seq_printf(m, "[Instance %d]\n", j);
1745
1746 for (k = 0; k < obj_size; k++) {
1747 error = mxt_read_object(data, object->type,
1748 j * obj_size + k, &val);
1749 if (error) {
1750 dev_err(dev,
1751 "Failed to read object %d "
1752 "instance %d at offset %d\n",
1753 object->type, j, k);
1754 return error;
1755 }
1756
1757 seq_printf(m, "Byte %d: 0x%02x (%d)\n",
1758 k, val, val);
1759 }
1760 }
1761 }
1762
1763 return 0;
1764}
1765
1766static int mxt_debugfs_object_open(struct inode *inode, struct file *file)
1767{
1768 return single_open(file, mxt_debugfs_object_show, inode->i_private);
1769}
1770
1771static const struct file_operations mxt_object_fops = {
1772 .owner = THIS_MODULE,
1773 .open = mxt_debugfs_object_open,
1774 .read = seq_read,
1775 .release = single_release,
1776};
1777
1778static void __init mxt_debugfs_init(struct mxt_data *data)
1779{
1780 debug_base = debugfs_create_dir(MXT_DEBUGFS_DIR, NULL);
1781 if (IS_ERR_OR_NULL(debug_base))
1782 pr_err("atmel_mxt_ts: Failed to create debugfs dir\n");
1783 if (IS_ERR_OR_NULL(debugfs_create_file(MXT_DEBUGFS_FILE,
1784 0444,
1785 debug_base,
1786 data,
1787 &mxt_object_fops))) {
1788 pr_err("atmel_mxt_ts: Failed to create object file\n");
1789 debugfs_remove_recursive(debug_base);
1790 }
1791}
1792
Iiro Valkonen7686b102011-02-02 23:21:58 -08001793static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001794 const struct i2c_device_id *id)
1795{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001796 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001797 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001798 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001799 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001800
Iiro Valkonen919ed892011-02-15 13:36:52 -08001801 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001802 return -EINVAL;
1803
Iiro Valkonen7686b102011-02-02 23:21:58 -08001804 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001805 input_dev = input_allocate_device();
1806 if (!data || !input_dev) {
1807 dev_err(&client->dev, "Failed to allocate memory\n");
1808 error = -ENOMEM;
1809 goto err_free_mem;
1810 }
1811
Iiro Valkonen7686b102011-02-02 23:21:58 -08001812 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001813 input_dev->id.bustype = BUS_I2C;
1814 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001815 input_dev->open = mxt_input_open;
1816 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001817
Joonyoung Shim910d8052011-04-12 23:14:38 -07001818 data->client = client;
1819 data->input_dev = input_dev;
1820 data->pdata = pdata;
1821 data->irq = client->irq;
1822
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001823 __set_bit(EV_ABS, input_dev->evbit);
1824 __set_bit(EV_KEY, input_dev->evbit);
1825 __set_bit(BTN_TOUCH, input_dev->keybit);
1826
1827 /* For single touch */
1828 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001829 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001830 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001831 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001832 input_set_abs_params(input_dev, ABS_PRESSURE,
1833 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001834
1835 /* For multi touch */
1836 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001837 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001838 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001839 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001840 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001841 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001842 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1843 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001844
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001845 /* set key array supported keys */
1846 if (pdata->key_codes) {
1847 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
1848 if (pdata->key_codes[i])
1849 input_set_capability(input_dev, EV_KEY,
1850 pdata->key_codes[i]);
1851 }
1852 }
1853
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001854 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001855 i2c_set_clientdata(client, data);
1856
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301857 if (pdata->init_hw)
1858 error = pdata->init_hw(true);
1859 else
1860 error = mxt_regulator_configure(data, true);
1861 if (error) {
1862 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001863 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301864 }
1865
1866 if (pdata->power_on)
1867 error = pdata->power_on(true);
1868 else
1869 error = mxt_power_on(data, true);
1870 if (error) {
1871 dev_err(&client->dev, "Failed to power on hardware\n");
1872 goto err_regulator_on;
1873 }
1874
Amy Maloche08266db2011-11-04 11:07:16 -07001875 if (gpio_is_valid(pdata->irq_gpio)) {
1876 /* configure touchscreen irq gpio */
1877 error = gpio_request(pdata->irq_gpio,
1878 "mxt_irq_gpio");
1879 if (error) {
1880 pr_err("%s: unable to request gpio [%d]\n", __func__,
1881 pdata->irq_gpio);
1882 goto err_power_on;
1883 }
1884 error = gpio_direction_input(pdata->irq_gpio);
1885 if (error) {
1886 pr_err("%s: unable to set_direction for gpio [%d]\n",
1887 __func__, pdata->irq_gpio);
1888 goto err_irq_gpio_req;
1889 }
1890 }
1891
1892 if (gpio_is_valid(pdata->reset_gpio)) {
1893 /* configure touchscreen reset out gpio */
1894 error = gpio_request(pdata->reset_gpio,
1895 "mxt_reset_gpio");
1896 if (error) {
1897 pr_err("%s: unable to request reset gpio %d\n",
1898 __func__, pdata->reset_gpio);
1899 goto err_irq_gpio_req;
1900 }
1901
1902 error = gpio_direction_output(
1903 pdata->reset_gpio, 1);
1904 if (error) {
1905 pr_err("%s: unable to set direction for gpio %d\n",
1906 __func__, pdata->reset_gpio);
1907 goto err_reset_gpio_req;
1908 }
1909 }
1910
1911 mxt_reset_delay(data);
1912
Iiro Valkonen7686b102011-02-02 23:21:58 -08001913 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001914 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07001915 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001916
Iiro Valkonen7686b102011-02-02 23:21:58 -08001917 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001918 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001919 if (error) {
1920 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001921 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001922 }
1923
Iiro Valkonen08960a02011-04-12 23:16:40 -07001924 error = mxt_make_highchg(data);
1925 if (error)
1926 goto err_free_irq;
1927
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001928 error = input_register_device(input_dev);
1929 if (error)
1930 goto err_free_irq;
1931
Iiro Valkonen7686b102011-02-02 23:21:58 -08001932 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001933 if (error)
1934 goto err_unregister_device;
1935
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301936#if defined(CONFIG_HAS_EARLYSUSPEND)
1937 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1938 MXT_SUSPEND_LEVEL;
1939 data->early_suspend.suspend = mxt_early_suspend;
1940 data->early_suspend.resume = mxt_late_resume;
1941 register_early_suspend(&data->early_suspend);
1942#endif
1943
Jing Lin6cfc00e2011-11-02 15:15:30 -07001944 mxt_debugfs_init(data);
1945
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001946 return 0;
1947
1948err_unregister_device:
1949 input_unregister_device(input_dev);
1950 input_dev = NULL;
1951err_free_irq:
1952 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001953err_free_object:
1954 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07001955err_reset_gpio_req:
1956 if (gpio_is_valid(pdata->reset_gpio))
1957 gpio_free(pdata->reset_gpio);
1958err_irq_gpio_req:
1959 if (gpio_is_valid(pdata->irq_gpio))
1960 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301961err_power_on:
1962 if (pdata->power_on)
1963 pdata->power_on(false);
1964 else
1965 mxt_power_on(data, false);
1966err_regulator_on:
1967 if (pdata->init_hw)
1968 pdata->init_hw(false);
1969 else
1970 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001971err_free_mem:
1972 input_free_device(input_dev);
1973 kfree(data);
1974 return error;
1975}
1976
Iiro Valkonen7686b102011-02-02 23:21:58 -08001977static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001978{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001979 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001980
Iiro Valkonen7686b102011-02-02 23:21:58 -08001981 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001982 free_irq(data->irq, data);
1983 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301984#if defined(CONFIG_HAS_EARLYSUSPEND)
1985 unregister_early_suspend(&data->early_suspend);
1986#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301987
1988 if (data->pdata->power_on)
1989 data->pdata->power_on(false);
1990 else
1991 mxt_power_on(data, false);
1992
1993 if (data->pdata->init_hw)
1994 data->pdata->init_hw(false);
1995 else
1996 mxt_regulator_configure(data, false);
1997
Mohan Pallakabfe8f302012-01-02 18:32:08 +08001998 if (gpio_is_valid(data->pdata->reset_gpio))
1999 gpio_free(data->pdata->reset_gpio);
2000
2001 if (gpio_is_valid(data->pdata->irq_gpio))
2002 gpio_free(data->pdata->irq_gpio);
2003
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002004 kfree(data->object_table);
2005 kfree(data);
2006
Jing Lin6cfc00e2011-11-02 15:15:30 -07002007 debugfs_remove_recursive(debug_base);
2008
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002009 return 0;
2010}
2011
Iiro Valkonen7686b102011-02-02 23:21:58 -08002012static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002013 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002014 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08002015 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002016 { }
2017};
Iiro Valkonen7686b102011-02-02 23:21:58 -08002018MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002019
Iiro Valkonen7686b102011-02-02 23:21:58 -08002020static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002021 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08002022 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002023 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002024#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08002025 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002026#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002027 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002028 .probe = mxt_probe,
2029 .remove = __devexit_p(mxt_remove),
2030 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002031};
2032
Iiro Valkonen7686b102011-02-02 23:21:58 -08002033static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002034{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002035 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002036}
2037
Iiro Valkonen7686b102011-02-02 23:21:58 -08002038static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002039{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002040 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002041}
2042
Iiro Valkonen7686b102011-02-02 23:21:58 -08002043module_init(mxt_init);
2044module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002045
2046/* Module information */
2047MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08002048MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002049MODULE_LICENSE("GPL");