blob: 40aae6915b4d1e139c1242c668adb4a5f053212e [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
Mohan Pallaka5e7343f2012-01-02 18:30:16 +08006 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/firmware.h>
19#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080020#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shimcd473222012-02-14 18:32:48 -080021#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070022#include <linux/interrupt.h>
23#include <linux/slab.h>
Amy Maloche08266db2011-11-04 11:07:16 -070024#include <linux/gpio.h>
Jing Lin6cfc00e2011-11-02 15:15:30 -070025#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053027#include <linux/regulator/consumer.h>
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
Joonyoung Shimcd473222012-02-14 18:32:48 -0800614 input_mt_slot(input_dev, id);
615 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
616 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700617
Joonyoung Shimcd473222012-02-14 18:32:48 -0800618 if (finger[id].status != MXT_RELEASE) {
Amy Maloche2b59bab2011-10-13 16:08:16 -0700619 finger_num++;
Joonyoung Shimcd473222012-02-14 18:32:48 -0800620 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
621 finger[id].area);
622 input_report_abs(input_dev, ABS_MT_POSITION_X,
623 finger[id].x);
624 input_report_abs(input_dev, ABS_MT_POSITION_Y,
625 finger[id].y);
626 input_report_abs(input_dev, ABS_MT_PRESSURE,
627 finger[id].area);
628 } else {
629 finger[id].status = 0;
630 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631 }
632
633 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
634
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636 input_report_abs(input_dev, ABS_X, finger[single_id].x);
637 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700638 input_report_abs(input_dev,
639 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700640 }
641
642 input_sync(input_dev);
643}
644
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645static void mxt_input_touchevent(struct mxt_data *data,
646 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700647{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800648 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700649 struct device *dev = &data->client->dev;
650 u8 status = message->message[0];
651 int x;
652 int y;
653 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700654 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700655
656 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800657 if (!(status & MXT_DETECT)) {
658 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700659 dev_dbg(dev, "[%d] released\n", id);
660
Iiro Valkonen7686b102011-02-02 23:21:58 -0800661 finger[id].status = MXT_RELEASE;
662 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700663 }
664 return;
665 }
666
667 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800668 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669 return;
670
Joonyoung Shim910d8052011-04-12 23:14:38 -0700671 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
672 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Amy Maloche83c385a2012-02-01 10:32:03 +0530673 if (data->touch_x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700674 x = x >> 2;
Amy Maloche83c385a2012-02-01 10:32:03 +0530675 if (data->touch_y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700676 y = y >> 2;
677
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700678 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700679 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680
681 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700683 x, y, area);
684
Iiro Valkonen7686b102011-02-02 23:21:58 -0800685 finger[id].status = status & MXT_MOVE ?
686 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687 finger[id].x = x;
688 finger[id].y = y;
689 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700690 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691
Iiro Valkonen7686b102011-02-02 23:21:58 -0800692 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700693}
694
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800695static void mxt_handle_key_array(struct mxt_data *data,
696 struct mxt_message *message)
697{
698 u32 keys_changed;
699 int i;
700
701 if (!data->pdata->key_codes) {
702 dev_err(&data->client->dev, "keyarray is not supported\n");
703 return;
704 }
705
706 data->keyarray_new = message->message[1] |
707 (message->message[2] << 8) |
708 (message->message[3] << 16) |
709 (message->message[4] << 24);
710
711 keys_changed = data->keyarray_old ^ data->keyarray_new;
712
713 if (!keys_changed) {
714 dev_dbg(&data->client->dev, "no keys changed\n");
715 return;
716 }
717
718 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
719 if (!(keys_changed & (1 << i)))
720 continue;
721
722 input_report_key(data->input_dev, data->pdata->key_codes[i],
723 (data->keyarray_new & (1 << i)));
724 input_sync(data->input_dev);
725 }
726
727 data->keyarray_old = data->keyarray_new;
728}
729
Iiro Valkonen7686b102011-02-02 23:21:58 -0800730static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800732 struct mxt_data *data = dev_id;
733 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 struct device *dev = &data->client->dev;
735 int id;
736 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700737
738 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800739 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700740 dev_err(dev, "Failed to read message\n");
741 goto end;
742 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743 reportid = message.reportid;
744
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800745 if (!reportid) {
746 dev_dbg(dev, "Report id 0 is reserved\n");
747 continue;
748 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800750 /* check whether report id is part of T9 or T15 */
751 id = reportid - data->t9_min_reportid;
752
753 if (reportid >= data->t9_min_reportid &&
754 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800755 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800756 else if (reportid >= data->t15_min_reportid &&
757 reportid <= data->t15_max_reportid)
758 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700759 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800760 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761 } while (reportid != 0xff);
762
763end:
764 return IRQ_HANDLED;
765}
766
Iiro Valkonen7686b102011-02-02 23:21:58 -0800767static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700768{
Jing Lindc4413c2012-01-16 15:22:52 -0800769 const struct mxt_config_info *config_info = data->config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771 struct device *dev = &data->client->dev;
772 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800773 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700774
Jing Lindc4413c2012-01-16 15:22:52 -0800775 if (!config_info) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800776 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
777 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700778 }
779
780 for (i = 0; i < data->info.object_num; i++) {
781 object = data->object_table + i;
782
Iiro Valkonen7686b102011-02-02 23:21:58 -0800783 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700784 continue;
785
Iiro Valkonen71749f52011-02-15 13:36:52 -0800786 for (j = 0; j < object->size + 1; j++) {
787 config_offset = index + j;
Jing Lindc4413c2012-01-16 15:22:52 -0800788 if (config_offset > config_info->config_length) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800789 dev_err(dev, "Not enough config data!\n");
790 return -EINVAL;
791 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800792 mxt_write_object(data, object->type, j,
Jing Lindc4413c2012-01-16 15:22:52 -0800793 config_info->config[config_offset]);
Iiro Valkonen71749f52011-02-15 13:36:52 -0800794 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700795 index += object->size + 1;
796 }
797
798 return 0;
799}
800
Iiro Valkonen7686b102011-02-02 23:21:58 -0800801static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700802{
803 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800804 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700805 int count = 10;
806 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700807
808 /* Read dummy message to make high CHG pin */
809 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800810 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811 if (error)
812 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800813 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814
815 if (!count) {
816 dev_err(dev, "CHG pin isn't cleared\n");
817 return -EBUSY;
818 }
819
820 return 0;
821}
822
Iiro Valkonen7686b102011-02-02 23:21:58 -0800823static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824{
825 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800826 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700827 int error;
828 u8 val;
829
Iiro Valkonen7686b102011-02-02 23:21:58 -0800830 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831 if (error)
832 return error;
833 info->family_id = val;
834
Iiro Valkonen7686b102011-02-02 23:21:58 -0800835 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700836 if (error)
837 return error;
838 info->variant_id = val;
839
Iiro Valkonen7686b102011-02-02 23:21:58 -0800840 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700841 if (error)
842 return error;
843 info->version = val;
844
Iiro Valkonen7686b102011-02-02 23:21:58 -0800845 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700846 if (error)
847 return error;
848 info->build = val;
849
Iiro Valkonen7686b102011-02-02 23:21:58 -0800850 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700851 if (error)
852 return error;
853 info->object_num = val;
854
855 return 0;
856}
857
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700859{
860 int error;
861 int i;
862 u16 reg;
863 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800864 u8 buf[MXT_OBJECT_SIZE];
Jing Lindc4413c2012-01-16 15:22:52 -0800865 bool found_t38 = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700866
867 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800868 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700869
Iiro Valkonen7686b102011-02-02 23:21:58 -0800870 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
871 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700872 if (error)
873 return error;
874
875 object->type = buf[0];
876 object->start_address = (buf[2] << 8) | buf[1];
877 object->size = buf[3];
878 object->instances = buf[4];
879 object->num_report_ids = buf[5];
880
881 if (object->num_report_ids) {
882 reportid += object->num_report_ids *
883 (object->instances + 1);
884 object->max_reportid = reportid;
885 }
Jing Lindc4413c2012-01-16 15:22:52 -0800886
887 /* Calculate index for config major version in config array.
888 * Major version is the first byte in object T38.
889 */
890 if (object->type == MXT_SPT_USERDATA_T38)
891 found_t38 = true;
892 if (!found_t38 && mxt_object_writable(object->type))
893 data->cfg_version_idx += object->size + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700894 }
895
896 return 0;
897}
898
Jing Lindc4413c2012-01-16 15:22:52 -0800899static int mxt_search_config_array(struct mxt_data *data, bool version_match)
900{
901
902 const struct mxt_platform_data *pdata = data->pdata;
903 const struct mxt_config_info *cfg_info;
904 struct mxt_info *info = &data->info;
905 int i;
906 u8 cfg_version;
907
908 for (i = 0; i < pdata->config_array_size; i++) {
909
910 cfg_info = &pdata->config_array[i];
911
912 if (!cfg_info->config || !cfg_info->config_length)
913 continue;
914
915 if (info->family_id == cfg_info->family_id &&
916 info->variant_id == cfg_info->variant_id &&
917 info->version == cfg_info->version &&
918 info->build == cfg_info->build) {
919
920 cfg_version = cfg_info->config[data->cfg_version_idx];
921 if (data->curr_cfg_version == cfg_version ||
922 !version_match) {
923 data->config_info = cfg_info;
924 return 0;
925 }
926 }
927 }
928
929 dev_info(&data->client->dev,
930 "Config not found: F: %d, V: %d, FW: %d.%d.%d, CFG: %d\n",
931 info->family_id, info->variant_id,
932 info->version >> 4, info->version & 0xF, info->build,
933 data->curr_cfg_version);
934 return -EINVAL;
935}
936
937static int mxt_get_config(struct mxt_data *data)
938{
939 const struct mxt_platform_data *pdata = data->pdata;
940 struct device *dev = &data->client->dev;
941 struct mxt_object *object;
942 int error;
943
944 if (!pdata->config_array || !pdata->config_array_size) {
945 dev_dbg(dev, "No cfg data provided by platform data\n");
946 return 0;
947 }
948
949 /* Get current config version */
950 object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
951 if (!object) {
952 dev_err(dev, "Unable to obtain USERDATA object\n");
953 return -EINVAL;
954 }
955
956 error = mxt_read_reg(data->client, object->start_address,
957 &data->curr_cfg_version);
958 if (error) {
959 dev_err(dev, "Unable to read config version\n");
960 return error;
961 }
962
963 /* It is possible that the config data on the controller is not
964 * versioned and the version number returns 0. In this case,
965 * find a match without the config version checking.
966 */
967 error = mxt_search_config_array(data,
968 data->curr_cfg_version != 0 ? true : false);
969 if (error)
970 return error;
971
972 return 0;
973}
Amy Maloche7e447432011-09-14 11:36:30 -0700974static void mxt_reset_delay(struct mxt_data *data)
975{
976 struct mxt_info *info = &data->info;
977
978 switch (info->family_id) {
979 case MXT224_ID:
980 msleep(MXT224_RESET_TIME);
981 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -0700982 case MXT224E_ID:
983 msleep(MXT224E_RESET_TIME);
984 break;
Amy Maloche7e447432011-09-14 11:36:30 -0700985 case MXT1386_ID:
986 msleep(MXT1386_RESET_TIME);
987 break;
988 default:
989 msleep(MXT_RESET_TIME);
990 }
991}
992
Iiro Valkonen7686b102011-02-02 23:21:58 -0800993static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700994{
995 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800996 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700997 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700998 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700999 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001000 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -07001001 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001002 struct mxt_object *t9_object;
1003 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001004
Iiro Valkonen7686b102011-02-02 23:21:58 -08001005 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006 if (error)
1007 return error;
1008
1009 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001010 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001011 GFP_KERNEL);
1012 if (!data->object_table) {
1013 dev_err(&client->dev, "Failed to allocate memory\n");
1014 return -ENOMEM;
1015 }
1016
1017 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001018 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001019 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001020 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021
Jing Lindc4413c2012-01-16 15:22:52 -08001022 /* Get config data from platform data */
1023 error = mxt_get_config(data);
1024 if (error)
1025 dev_dbg(&client->dev, "Config info not found.\n");
1026
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001028 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001030 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001031
Amy Maloche52262212011-09-15 16:46:57 -07001032 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -08001033 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -07001034 if (!t7_object) {
1035 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -07001036 error = -EINVAL;
1037 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -07001038 }
1039
1040 data->t7_start_addr = t7_object->start_address;
1041 error = __mxt_read_reg(client, data->t7_start_addr,
1042 T7_DATA_SIZE, data->t7_data);
1043 if (error < 0) {
1044 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -07001045 "Failed to save current power state\n");
1046 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001047 }
Iiro Valkonene8645592011-11-18 12:56:19 -08001048 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -07001049 &data->t9_ctrl);
1050 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -07001051 dev_err(&client->dev, "Failed to save current touch object\n");
1052 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001053 }
1054
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001055 /* Store T9, T15's min and max report ids */
1056 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1057 if (!t9_object) {
1058 dev_err(&client->dev, "Failed to get T9 object\n");
1059 error = -EINVAL;
1060 goto free_object_table;
1061 }
1062 data->t9_max_reportid = t9_object->max_reportid;
1063 data->t9_min_reportid = t9_object->max_reportid -
1064 t9_object->num_report_ids + 1;
1065
1066 if (data->pdata->key_codes) {
1067 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
1068 if (!t15_object)
1069 dev_dbg(&client->dev, "T15 object is not available\n");
1070 else {
1071 data->t15_max_reportid = t15_object->max_reportid;
1072 data->t15_min_reportid = t15_object->max_reportid -
1073 t15_object->num_report_ids + 1;
1074 }
1075 }
1076
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001077 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -08001078 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079 MXT_COMMAND_BACKUPNV,
1080 MXT_BACKUP_VALUE);
1081 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001082 do {
Iiro Valkonene8645592011-11-18 12:56:19 -08001083 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001084 MXT_COMMAND_BACKUPNV,
1085 &command_register);
1086 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001087 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -07001088 usleep_range(1000, 2000);
1089 } while ((command_register != 0) && (++timeout_counter <= 100));
1090 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001091 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -07001092 error = -EIO;
1093 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001094 }
1095
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001096
1097 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -08001098 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001100
Amy Maloche7e447432011-09-14 11:36:30 -07001101 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001102
1103 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001104 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001105 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001106 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001107 info->matrix_xsize = val;
1108
Iiro Valkonen7686b102011-02-02 23:21:58 -08001109 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001110 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001111 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001112 info->matrix_ysize = val;
1113
1114 dev_info(&client->dev,
1115 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
1116 info->family_id, info->variant_id, info->version,
1117 info->build);
1118
1119 dev_info(&client->dev,
1120 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
1121 info->matrix_xsize, info->matrix_ysize,
1122 info->object_num);
1123
1124 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001125
1126free_object_table:
1127 kfree(data->object_table);
1128 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129}
1130
Iiro Valkonen7686b102011-02-02 23:21:58 -08001131static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132 struct device_attribute *attr, char *buf)
1133{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001134 struct mxt_data *data = dev_get_drvdata(dev);
1135 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001136 int count = 0;
1137 int i, j;
1138 int error;
1139 u8 val;
1140
1141 for (i = 0; i < data->info.object_num; i++) {
1142 object = data->object_table + i;
1143
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001144 count += snprintf(buf + count, PAGE_SIZE - count,
1145 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001147 if (count >= PAGE_SIZE)
1148 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001149
Iiro Valkonen7686b102011-02-02 23:21:58 -08001150 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001151 count += snprintf(buf + count, PAGE_SIZE - count,
1152 "\n");
1153 if (count >= PAGE_SIZE)
1154 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001155 continue;
1156 }
1157
1158 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001159 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001160 object->type, j, &val);
1161 if (error)
1162 return error;
1163
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001164 count += snprintf(buf + count, PAGE_SIZE - count,
1165 "\t[%2d]: %02x (%d)\n", j, val, val);
1166 if (count >= PAGE_SIZE)
1167 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168 }
1169
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001170 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1171 if (count >= PAGE_SIZE)
1172 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001173 }
1174
1175 return count;
1176}
1177
Iiro Valkonen7686b102011-02-02 23:21:58 -08001178static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001179{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001180 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001181 struct i2c_client *client = data->client;
1182 const struct firmware *fw = NULL;
1183 unsigned int frame_size;
1184 unsigned int pos = 0;
1185 int ret;
1186
1187 ret = request_firmware(&fw, fn, dev);
1188 if (ret) {
1189 dev_err(dev, "Unable to open firmware %s\n", fn);
1190 return ret;
1191 }
1192
1193 /* Change to the bootloader mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001194 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001195 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001196
1197 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001198
1199 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001200 if (client->addr == MXT_APP_LOW)
1201 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001202 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001203 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001204
Iiro Valkonen7686b102011-02-02 23:21:58 -08001205 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001206 if (ret)
1207 goto out;
1208
1209 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001210 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001211
1212 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001213 ret = mxt_check_bootloader(client,
1214 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001215 if (ret)
1216 goto out;
1217
1218 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1219
1220 /* We should add 2 at frame size as the the firmware data is not
1221 * included the CRC bytes.
1222 */
1223 frame_size += 2;
1224
1225 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001226 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001227
Iiro Valkonen7686b102011-02-02 23:21:58 -08001228 ret = mxt_check_bootloader(client,
1229 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001230 if (ret)
1231 goto out;
1232
1233 pos += frame_size;
1234
1235 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1236 }
1237
1238out:
1239 release_firmware(fw);
1240
1241 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001242 if (client->addr == MXT_BOOT_LOW)
1243 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001244 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001245 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001246
1247 return ret;
1248}
1249
Iiro Valkonen7686b102011-02-02 23:21:58 -08001250static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001251 struct device_attribute *attr,
1252 const char *buf, size_t count)
1253{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001254 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001255 int error;
1256
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001257 disable_irq(data->irq);
1258
Iiro Valkonen7686b102011-02-02 23:21:58 -08001259 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001260 if (error) {
1261 dev_err(dev, "The firmware update failed(%d)\n", error);
1262 count = error;
1263 } else {
1264 dev_dbg(dev, "The firmware update succeeded\n");
1265
1266 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001267 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001268
1269 kfree(data->object_table);
1270 data->object_table = NULL;
1271
Iiro Valkonen7686b102011-02-02 23:21:58 -08001272 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273 }
1274
1275 enable_irq(data->irq);
1276
Iiro Valkonen08960a02011-04-12 23:16:40 -07001277 error = mxt_make_highchg(data);
1278 if (error)
1279 return error;
1280
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001281 return count;
1282}
1283
Iiro Valkonen7686b102011-02-02 23:21:58 -08001284static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1285static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001286
Iiro Valkonen7686b102011-02-02 23:21:58 -08001287static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001288 &dev_attr_object.attr,
1289 &dev_attr_update_fw.attr,
1290 NULL
1291};
1292
Iiro Valkonen7686b102011-02-02 23:21:58 -08001293static const struct attribute_group mxt_attr_group = {
1294 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001295};
1296
Amy Maloche52262212011-09-15 16:46:57 -07001297static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001298{
Jing Lin36aee812011-10-17 17:17:28 -07001299 int error;
1300
Amy Maloche52262212011-09-15 16:46:57 -07001301 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001302 error = __mxt_write_reg(data->client, data->t7_start_addr,
1303 T7_DATA_SIZE, data->t7_data);
1304 if (error < 0) {
1305 dev_err(&data->client->dev,
1306 "failed to restore old power state\n");
1307 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001308 }
Jing Lin36aee812011-10-17 17:17:28 -07001309
Amy Maloche52262212011-09-15 16:46:57 -07001310 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001311 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001312 if (error < 0) {
1313 dev_err(&data->client->dev, "failed to restore touch\n");
1314 return error;
1315 }
1316
1317 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001318}
1319
Amy Maloche52262212011-09-15 16:46:57 -07001320static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001321{
Jing Lin36aee812011-10-17 17:17:28 -07001322 int error;
1323 u8 t7_data[T7_DATA_SIZE] = {0};
1324
1325 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001326 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001327 if (error < 0) {
1328 dev_err(&data->client->dev, "failed to disable touch\n");
1329 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001330 }
1331
Jing Lin36aee812011-10-17 17:17:28 -07001332 error = __mxt_write_reg(data->client, data->t7_start_addr,
1333 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001334 if (error < 0) {
1335 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001336 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001337 return error;
1338 }
1339
1340 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001341}
1342
Iiro Valkonen7686b102011-02-02 23:21:58 -08001343static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001344{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001345 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001346 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001347
Amy Maloche52262212011-09-15 16:46:57 -07001348 error = mxt_start(data);
1349 if (error < 0) {
1350 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1351 return error;
1352 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001353
1354 return 0;
1355}
1356
Iiro Valkonen7686b102011-02-02 23:21:58 -08001357static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001359 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001360 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001361
Amy Maloche52262212011-09-15 16:46:57 -07001362 error = mxt_stop(data);
1363 if (error < 0)
1364 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1365
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001366}
1367
Amy Malochec331f842012-01-24 10:33:47 -08001368static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1369{
1370 return (regulator_count_voltages(reg) > 0) ?
1371 regulator_set_optimum_mode(reg, load_uA) : 0;
1372}
1373
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301374static int mxt_power_on(struct mxt_data *data, bool on)
1375{
1376 int rc;
1377
1378 if (on == false)
1379 goto power_off;
1380
Amy Malochec331f842012-01-24 10:33:47 -08001381 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301382 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001383 dev_err(&data->client->dev,
1384 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301385 return rc;
1386 }
1387
Amy Maloche21115eb2011-11-02 09:04:37 -07001388 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301389 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001390 dev_err(&data->client->dev,
1391 "Regulator vcc_ana enable failed rc=%d\n", rc);
1392 goto error_reg_en_vcc_ana;
1393 }
1394
1395 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001396 rc = reg_set_optimum_mode_check(data->vcc_dig,
1397 MXT_ACTIVE_LOAD_DIG_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001398 if (rc < 0) {
1399 dev_err(&data->client->dev,
1400 "Regulator vcc_dig set_opt failed rc=%d\n",
1401 rc);
1402 goto error_reg_opt_vcc_dig;
1403 }
1404
1405 rc = regulator_enable(data->vcc_dig);
1406 if (rc) {
1407 dev_err(&data->client->dev,
1408 "Regulator vcc_dig enable failed rc=%d\n", rc);
1409 goto error_reg_en_vcc_dig;
1410 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301411 }
1412
1413 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001414 rc = reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301415 if (rc < 0) {
1416 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001417 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301418 goto error_reg_opt_i2c;
1419 }
1420
1421 rc = regulator_enable(data->vcc_i2c);
1422 if (rc) {
1423 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001424 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301425 goto error_reg_en_vcc_i2c;
1426 }
1427 }
1428
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001429 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301430
1431 return 0;
1432
1433error_reg_en_vcc_i2c:
1434 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001435 reg_set_optimum_mode_check(data->vcc_i2c, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301436error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001437 if (data->pdata->digital_pwr_regulator)
1438 regulator_disable(data->vcc_dig);
1439error_reg_en_vcc_dig:
1440 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001441 reg_set_optimum_mode_check(data->vcc_dig, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001442error_reg_opt_vcc_dig:
1443 regulator_disable(data->vcc_ana);
1444error_reg_en_vcc_ana:
Amy Malochec331f842012-01-24 10:33:47 -08001445 reg_set_optimum_mode_check(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301446 return rc;
1447
1448power_off:
Amy Malochec331f842012-01-24 10:33:47 -08001449 reg_set_optimum_mode_check(data->vcc_ana, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001450 regulator_disable(data->vcc_ana);
1451 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001452 reg_set_optimum_mode_check(data->vcc_dig, 0);
Amy Maloche21115eb2011-11-02 09:04:37 -07001453 regulator_disable(data->vcc_dig);
1454 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301455 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001456 reg_set_optimum_mode_check(data->vcc_i2c, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301457 regulator_disable(data->vcc_i2c);
1458 }
1459 msleep(50);
1460 return 0;
1461}
1462
1463static int mxt_regulator_configure(struct mxt_data *data, bool on)
1464{
1465 int rc;
1466
1467 if (on == false)
1468 goto hw_shutdown;
1469
Amy Maloche21115eb2011-11-02 09:04:37 -07001470 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1471 if (IS_ERR(data->vcc_ana)) {
1472 rc = PTR_ERR(data->vcc_ana);
1473 dev_err(&data->client->dev,
1474 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301475 return rc;
1476 }
1477
Amy Maloche21115eb2011-11-02 09:04:37 -07001478 if (regulator_count_voltages(data->vcc_ana) > 0) {
1479 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301480 MXT_VTG_MAX_UV);
1481 if (rc) {
1482 dev_err(&data->client->dev,
1483 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001484 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301485 }
1486 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001487 if (data->pdata->digital_pwr_regulator) {
1488 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1489 if (IS_ERR(data->vcc_dig)) {
1490 rc = PTR_ERR(data->vcc_dig);
1491 dev_err(&data->client->dev,
1492 "Regulator get dig failed rc=%d\n", rc);
1493 goto error_get_vtg_vcc_dig;
1494 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301495
Amy Maloche21115eb2011-11-02 09:04:37 -07001496 if (regulator_count_voltages(data->vcc_dig) > 0) {
1497 rc = regulator_set_voltage(data->vcc_dig,
1498 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1499 if (rc) {
1500 dev_err(&data->client->dev,
1501 "regulator set_vtg failed rc=%d\n", rc);
1502 goto error_set_vtg_vcc_dig;
1503 }
1504 }
1505 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301506 if (data->pdata->i2c_pull_up) {
1507 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1508 if (IS_ERR(data->vcc_i2c)) {
1509 rc = PTR_ERR(data->vcc_i2c);
1510 dev_err(&data->client->dev,
1511 "Regulator get failed rc=%d\n", rc);
1512 goto error_get_vtg_i2c;
1513 }
1514 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1515 rc = regulator_set_voltage(data->vcc_i2c,
1516 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1517 if (rc) {
1518 dev_err(&data->client->dev,
1519 "regulator set_vtg failed rc=%d\n", rc);
1520 goto error_set_vtg_i2c;
1521 }
1522 }
1523 }
1524
1525 return 0;
1526
1527error_set_vtg_i2c:
1528 regulator_put(data->vcc_i2c);
1529error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001530 if (data->pdata->digital_pwr_regulator)
1531 if (regulator_count_voltages(data->vcc_dig) > 0)
1532 regulator_set_voltage(data->vcc_dig, 0,
1533 MXT_VTG_DIG_MAX_UV);
1534error_set_vtg_vcc_dig:
1535 if (data->pdata->digital_pwr_regulator)
1536 regulator_put(data->vcc_dig);
1537error_get_vtg_vcc_dig:
1538 if (regulator_count_voltages(data->vcc_ana) > 0)
1539 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1540error_set_vtg_vcc_ana:
1541 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301542 return rc;
1543
1544hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001545 if (regulator_count_voltages(data->vcc_ana) > 0)
1546 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1547 regulator_put(data->vcc_ana);
1548 if (data->pdata->digital_pwr_regulator) {
1549 if (regulator_count_voltages(data->vcc_dig) > 0)
1550 regulator_set_voltage(data->vcc_dig, 0,
1551 MXT_VTG_DIG_MAX_UV);
1552 regulator_put(data->vcc_dig);
1553 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301554 if (data->pdata->i2c_pull_up) {
1555 if (regulator_count_voltages(data->vcc_i2c) > 0)
1556 regulator_set_voltage(data->vcc_i2c, 0,
1557 MXT_I2C_VTG_MAX_UV);
1558 regulator_put(data->vcc_i2c);
1559 }
1560 return 0;
1561}
1562
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301563#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001564static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1565{
1566
1567 int rc;
1568
1569 if (on == false)
1570 goto regulator_hpm;
1571
Amy Malochec331f842012-01-24 10:33:47 -08001572 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001573 if (rc < 0) {
1574 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001575 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001576 goto fail_regulator_lpm;
1577 }
1578
Amy Maloche21115eb2011-11-02 09:04:37 -07001579 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001580 rc = reg_set_optimum_mode_check(data->vcc_dig,
Amy Maloche21115eb2011-11-02 09:04:37 -07001581 MXT_LPM_LOAD_DIG_UA);
1582 if (rc < 0) {
1583 dev_err(&data->client->dev,
1584 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1585 goto fail_regulator_lpm;
1586 }
1587 }
1588
Jing Linbace50b2011-10-18 22:55:47 -07001589 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001590 rc = reg_set_optimum_mode_check(data->vcc_i2c,
Jing Linbace50b2011-10-18 22:55:47 -07001591 MXT_I2C_LPM_LOAD_UA);
1592 if (rc < 0) {
1593 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001594 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001595 goto fail_regulator_lpm;
1596 }
1597 }
1598
1599 return 0;
1600
1601regulator_hpm:
1602
Amy Malochec331f842012-01-24 10:33:47 -08001603 rc = reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001604 if (rc < 0) {
1605 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001606 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001607 goto fail_regulator_hpm;
1608 }
1609
Amy Maloche21115eb2011-11-02 09:04:37 -07001610 if (data->pdata->digital_pwr_regulator) {
Amy Malochec331f842012-01-24 10:33:47 -08001611 rc = reg_set_optimum_mode_check(data->vcc_dig,
Amy Maloche21115eb2011-11-02 09:04:37 -07001612 MXT_ACTIVE_LOAD_DIG_UA);
1613 if (rc < 0) {
1614 dev_err(&data->client->dev,
1615 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1616 goto fail_regulator_hpm;
1617 }
1618 }
1619
Jing Linbace50b2011-10-18 22:55:47 -07001620 if (data->pdata->i2c_pull_up) {
Amy Malochec331f842012-01-24 10:33:47 -08001621 rc = reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001622 if (rc < 0) {
1623 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001624 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001625 goto fail_regulator_hpm;
1626 }
1627 }
1628
1629 return 0;
1630
1631fail_regulator_lpm:
Amy Malochec331f842012-01-24 10:33:47 -08001632 reg_set_optimum_mode_check(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001633 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001634 reg_set_optimum_mode_check(data->vcc_dig,
1635 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001636 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001637 reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001638
1639 return rc;
1640
1641fail_regulator_hpm:
Amy Malochec331f842012-01-24 10:33:47 -08001642 reg_set_optimum_mode_check(data->vcc_ana, MXT_LPM_LOAD_UA);
Amy Maloche21115eb2011-11-02 09:04:37 -07001643 if (data->pdata->digital_pwr_regulator)
Amy Malochec331f842012-01-24 10:33:47 -08001644 reg_set_optimum_mode_check(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001645 if (data->pdata->i2c_pull_up)
Amy Malochec331f842012-01-24 10:33:47 -08001646 reg_set_optimum_mode_check(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001647
1648 return rc;
1649}
1650
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301651static int mxt_suspend(struct device *dev)
1652{
1653 struct i2c_client *client = to_i2c_client(dev);
1654 struct mxt_data *data = i2c_get_clientdata(client);
1655 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001656 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301657
1658 mutex_lock(&input_dev->mutex);
1659
Amy Maloche52262212011-09-15 16:46:57 -07001660 if (input_dev->users) {
1661 error = mxt_stop(data);
1662 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001663 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001664 mutex_unlock(&input_dev->mutex);
1665 return error;
1666 }
1667
1668 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301669
1670 mutex_unlock(&input_dev->mutex);
1671
Jing Linbace50b2011-10-18 22:55:47 -07001672 /* put regulators in low power mode */
1673 error = mxt_regulator_lpm(data, true);
1674 if (error < 0) {
1675 dev_err(dev, "failed to enter low power mode\n");
1676 return error;
1677 }
1678
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301679 return 0;
1680}
1681
1682static int mxt_resume(struct device *dev)
1683{
1684 struct i2c_client *client = to_i2c_client(dev);
1685 struct mxt_data *data = i2c_get_clientdata(client);
1686 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001687 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301688
Jing Linbace50b2011-10-18 22:55:47 -07001689 /* put regulators in high power mode */
1690 error = mxt_regulator_lpm(data, false);
1691 if (error < 0) {
1692 dev_err(dev, "failed to enter high power mode\n");
1693 return error;
1694 }
1695
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301696 mutex_lock(&input_dev->mutex);
1697
Amy Maloche52262212011-09-15 16:46:57 -07001698 if (input_dev->users) {
1699 error = mxt_start(data);
1700 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001701 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001702 mutex_unlock(&input_dev->mutex);
1703 return error;
1704 }
1705 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301706
1707 mutex_unlock(&input_dev->mutex);
1708
1709 return 0;
1710}
1711
1712#if defined(CONFIG_HAS_EARLYSUSPEND)
1713static void mxt_early_suspend(struct early_suspend *h)
1714{
1715 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1716
1717 mxt_suspend(&data->client->dev);
1718}
1719
1720static void mxt_late_resume(struct early_suspend *h)
1721{
1722 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1723
1724 mxt_resume(&data->client->dev);
1725}
1726#endif
1727
1728static const struct dev_pm_ops mxt_pm_ops = {
1729#ifndef CONFIG_HAS_EARLYSUSPEND
1730 .suspend = mxt_suspend,
1731 .resume = mxt_resume,
1732#endif
1733};
1734#endif
1735
Jing Lin6cfc00e2011-11-02 15:15:30 -07001736static int mxt_debugfs_object_show(struct seq_file *m, void *v)
1737{
1738 struct mxt_data *data = m->private;
1739 struct mxt_object *object;
1740 struct device *dev = &data->client->dev;
1741 int i, j, k;
1742 int error;
1743 int obj_size;
1744 u8 val;
1745
1746 for (i = 0; i < data->info.object_num; i++) {
1747 object = data->object_table + i;
1748 obj_size = object->size + 1;
1749
1750 seq_printf(m, "Object[%d] (Type %d)\n", i + 1, object->type);
1751
1752 for (j = 0; j < object->instances + 1; j++) {
1753 seq_printf(m, "[Instance %d]\n", j);
1754
1755 for (k = 0; k < obj_size; k++) {
1756 error = mxt_read_object(data, object->type,
1757 j * obj_size + k, &val);
1758 if (error) {
1759 dev_err(dev,
1760 "Failed to read object %d "
1761 "instance %d at offset %d\n",
1762 object->type, j, k);
1763 return error;
1764 }
1765
1766 seq_printf(m, "Byte %d: 0x%02x (%d)\n",
1767 k, val, val);
1768 }
1769 }
1770 }
1771
1772 return 0;
1773}
1774
1775static int mxt_debugfs_object_open(struct inode *inode, struct file *file)
1776{
1777 return single_open(file, mxt_debugfs_object_show, inode->i_private);
1778}
1779
1780static const struct file_operations mxt_object_fops = {
1781 .owner = THIS_MODULE,
1782 .open = mxt_debugfs_object_open,
1783 .read = seq_read,
1784 .release = single_release,
1785};
1786
1787static void __init mxt_debugfs_init(struct mxt_data *data)
1788{
1789 debug_base = debugfs_create_dir(MXT_DEBUGFS_DIR, NULL);
1790 if (IS_ERR_OR_NULL(debug_base))
1791 pr_err("atmel_mxt_ts: Failed to create debugfs dir\n");
1792 if (IS_ERR_OR_NULL(debugfs_create_file(MXT_DEBUGFS_FILE,
1793 0444,
1794 debug_base,
1795 data,
1796 &mxt_object_fops))) {
1797 pr_err("atmel_mxt_ts: Failed to create object file\n");
1798 debugfs_remove_recursive(debug_base);
1799 }
1800}
1801
Iiro Valkonen7686b102011-02-02 23:21:58 -08001802static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001803 const struct i2c_device_id *id)
1804{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001805 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001806 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001807 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001808 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001809
Iiro Valkonen919ed892011-02-15 13:36:52 -08001810 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001811 return -EINVAL;
1812
Iiro Valkonen7686b102011-02-02 23:21:58 -08001813 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001814 input_dev = input_allocate_device();
1815 if (!data || !input_dev) {
1816 dev_err(&client->dev, "Failed to allocate memory\n");
1817 error = -ENOMEM;
1818 goto err_free_mem;
1819 }
1820
Amy Maloche83c385a2012-02-01 10:32:03 +05301821 input_dev->name = "atmel_mxt_ts";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001822 input_dev->id.bustype = BUS_I2C;
1823 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001824 input_dev->open = mxt_input_open;
1825 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001826
Joonyoung Shim910d8052011-04-12 23:14:38 -07001827 data->client = client;
1828 data->input_dev = input_dev;
1829 data->pdata = pdata;
1830 data->irq = client->irq;
1831
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001832 __set_bit(EV_ABS, input_dev->evbit);
1833 __set_bit(EV_KEY, input_dev->evbit);
1834 __set_bit(BTN_TOUCH, input_dev->keybit);
1835
1836 /* For single touch */
1837 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001838 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001839 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001840 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001841 input_set_abs_params(input_dev, ABS_PRESSURE,
1842 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001843
1844 /* For multi touch */
Joonyoung Shimcd473222012-02-14 18:32:48 -08001845 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001846 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001847 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001848 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001849 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001850 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001851 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001852 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1853 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001854
Amy Maloche83c385a2012-02-01 10:32:03 +05301855 if (pdata->touch_x_size)
1856 data->touch_x_size = pdata->touch_x_size;
1857 else
1858 data->touch_x_size = pdata->x_size;
1859
1860 if (pdata->touch_y_size)
1861 data->touch_y_size = pdata->touch_y_size;
1862 else
1863 data->touch_y_size = pdata->y_size;
1864
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001865 /* set key array supported keys */
1866 if (pdata->key_codes) {
1867 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
1868 if (pdata->key_codes[i])
1869 input_set_capability(input_dev, EV_KEY,
1870 pdata->key_codes[i]);
1871 }
1872 }
1873
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001874 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001875 i2c_set_clientdata(client, data);
1876
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301877 if (pdata->init_hw)
1878 error = pdata->init_hw(true);
1879 else
1880 error = mxt_regulator_configure(data, true);
1881 if (error) {
1882 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001883 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301884 }
1885
1886 if (pdata->power_on)
1887 error = pdata->power_on(true);
1888 else
1889 error = mxt_power_on(data, true);
1890 if (error) {
1891 dev_err(&client->dev, "Failed to power on hardware\n");
1892 goto err_regulator_on;
1893 }
1894
Amy Maloche08266db2011-11-04 11:07:16 -07001895 if (gpio_is_valid(pdata->irq_gpio)) {
1896 /* configure touchscreen irq gpio */
1897 error = gpio_request(pdata->irq_gpio,
1898 "mxt_irq_gpio");
1899 if (error) {
1900 pr_err("%s: unable to request gpio [%d]\n", __func__,
1901 pdata->irq_gpio);
1902 goto err_power_on;
1903 }
1904 error = gpio_direction_input(pdata->irq_gpio);
1905 if (error) {
1906 pr_err("%s: unable to set_direction for gpio [%d]\n",
1907 __func__, pdata->irq_gpio);
1908 goto err_irq_gpio_req;
1909 }
1910 }
1911
1912 if (gpio_is_valid(pdata->reset_gpio)) {
1913 /* configure touchscreen reset out gpio */
1914 error = gpio_request(pdata->reset_gpio,
1915 "mxt_reset_gpio");
1916 if (error) {
1917 pr_err("%s: unable to request reset gpio %d\n",
1918 __func__, pdata->reset_gpio);
1919 goto err_irq_gpio_req;
1920 }
1921
1922 error = gpio_direction_output(
1923 pdata->reset_gpio, 1);
1924 if (error) {
1925 pr_err("%s: unable to set direction for gpio %d\n",
1926 __func__, pdata->reset_gpio);
1927 goto err_reset_gpio_req;
1928 }
1929 }
1930
1931 mxt_reset_delay(data);
1932
Iiro Valkonen7686b102011-02-02 23:21:58 -08001933 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001934 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07001935 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001936
Iiro Valkonen7686b102011-02-02 23:21:58 -08001937 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001938 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001939 if (error) {
1940 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001941 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001942 }
1943
Iiro Valkonen08960a02011-04-12 23:16:40 -07001944 error = mxt_make_highchg(data);
1945 if (error)
1946 goto err_free_irq;
1947
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001948 error = input_register_device(input_dev);
1949 if (error)
1950 goto err_free_irq;
1951
Iiro Valkonen7686b102011-02-02 23:21:58 -08001952 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001953 if (error)
1954 goto err_unregister_device;
1955
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301956#if defined(CONFIG_HAS_EARLYSUSPEND)
1957 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1958 MXT_SUSPEND_LEVEL;
1959 data->early_suspend.suspend = mxt_early_suspend;
1960 data->early_suspend.resume = mxt_late_resume;
1961 register_early_suspend(&data->early_suspend);
1962#endif
1963
Jing Lin6cfc00e2011-11-02 15:15:30 -07001964 mxt_debugfs_init(data);
1965
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001966 return 0;
1967
1968err_unregister_device:
1969 input_unregister_device(input_dev);
1970 input_dev = NULL;
1971err_free_irq:
1972 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001973err_free_object:
1974 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07001975err_reset_gpio_req:
1976 if (gpio_is_valid(pdata->reset_gpio))
1977 gpio_free(pdata->reset_gpio);
1978err_irq_gpio_req:
1979 if (gpio_is_valid(pdata->irq_gpio))
1980 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301981err_power_on:
1982 if (pdata->power_on)
1983 pdata->power_on(false);
1984 else
1985 mxt_power_on(data, false);
1986err_regulator_on:
1987 if (pdata->init_hw)
1988 pdata->init_hw(false);
1989 else
1990 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001991err_free_mem:
1992 input_free_device(input_dev);
1993 kfree(data);
1994 return error;
1995}
1996
Iiro Valkonen7686b102011-02-02 23:21:58 -08001997static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001998{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001999 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002000
Iiro Valkonen7686b102011-02-02 23:21:58 -08002001 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002002 free_irq(data->irq, data);
2003 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05302004#if defined(CONFIG_HAS_EARLYSUSPEND)
2005 unregister_early_suspend(&data->early_suspend);
2006#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05302007
2008 if (data->pdata->power_on)
2009 data->pdata->power_on(false);
2010 else
2011 mxt_power_on(data, false);
2012
2013 if (data->pdata->init_hw)
2014 data->pdata->init_hw(false);
2015 else
2016 mxt_regulator_configure(data, false);
2017
Mohan Pallakabfe8f302012-01-02 18:32:08 +08002018 if (gpio_is_valid(data->pdata->reset_gpio))
2019 gpio_free(data->pdata->reset_gpio);
2020
2021 if (gpio_is_valid(data->pdata->irq_gpio))
2022 gpio_free(data->pdata->irq_gpio);
2023
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002024 kfree(data->object_table);
2025 kfree(data);
2026
Jing Lin6cfc00e2011-11-02 15:15:30 -07002027 debugfs_remove_recursive(debug_base);
2028
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002029 return 0;
2030}
2031
Iiro Valkonen7686b102011-02-02 23:21:58 -08002032static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002033 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002034 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08002035 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002036 { }
2037};
Iiro Valkonen7686b102011-02-02 23:21:58 -08002038MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002039
Iiro Valkonen7686b102011-02-02 23:21:58 -08002040static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002041 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08002042 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002043 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002044#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08002045 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002046#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002047 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002048 .probe = mxt_probe,
2049 .remove = __devexit_p(mxt_remove),
2050 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002051};
2052
Iiro Valkonen7686b102011-02-02 23:21:58 -08002053static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002054{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002055 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002056}
2057
Iiro Valkonen7686b102011-02-02 23:21:58 -08002058static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002059{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002060 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002061}
2062
Iiro Valkonen7686b102011-02-02 23:21:58 -08002063module_init(mxt_init);
2064module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002065
2066/* Module information */
2067MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08002068MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002069MODULE_LICENSE("GPL");