blob: 705e6cf7371a74425aa7636c510310cda463043d [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>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053025#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070026
Anirudh Ghayal253ce122011-08-09 19:32:57 +053027#if defined(CONFIG_HAS_EARLYSUSPEND)
28#include <linux/earlysuspend.h>
29/* Early-suspend level */
30#define MXT_SUSPEND_LEVEL 1
31#endif
32
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070033/* Family ID */
34#define MXT224_ID 0x80
Amy Maloche380cc0b2011-11-03 12:55:04 -070035#define MXT224E_ID 0x81
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070036#define MXT1386_ID 0xA0
37
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070038/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080039#define MXT_VER_20 20
40#define MXT_VER_21 21
41#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070042
43/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080044#define MXT_APP_LOW 0x4a
45#define MXT_APP_HIGH 0x4b
46#define MXT_BOOT_LOW 0x24
47#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070048
49/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080050#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070051
52/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080053#define MXT_FAMILY_ID 0x00
54#define MXT_VARIANT_ID 0x01
55#define MXT_VERSION 0x02
56#define MXT_BUILD 0x03
57#define MXT_MATRIX_X_SIZE 0x04
58#define MXT_MATRIX_Y_SIZE 0x05
59#define MXT_OBJECT_NUM 0x06
60#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070061
Iiro Valkonen7686b102011-02-02 23:21:58 -080062#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070063
64/* Object types */
Iiro Valkonene8645592011-11-18 12:56:19 -080065#define MXT_DEBUG_DIAGNOSTIC_T37 37
66#define MXT_GEN_MESSAGE_T5 5
67#define MXT_GEN_COMMAND_T6 6
68#define MXT_GEN_POWER_T7 7
69#define MXT_GEN_ACQUIRE_T8 8
70#define MXT_GEN_DATASOURCE_T53 53
71#define MXT_TOUCH_MULTI_T9 9
72#define MXT_TOUCH_KEYARRAY_T15 15
73#define MXT_TOUCH_PROXIMITY_T23 23
74#define MXT_TOUCH_PROXKEY_T52 52
75#define MXT_PROCI_GRIPFACE_T20 20
76#define MXT_PROCG_NOISE_T22 22
77#define MXT_PROCI_ONETOUCH_T24 24
78#define MXT_PROCI_TWOTOUCH_T27 27
79#define MXT_PROCI_GRIP_T40 40
80#define MXT_PROCI_PALM_T41 41
81#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
82#define MXT_PROCI_STYLUS_T47 47
Jing Linc7fc4052011-12-21 16:16:19 -080083#define MXT_PROCI_SHIELDLESS_T56 56
Iiro Valkonene8645592011-11-18 12:56:19 -080084#define MXT_PROCG_NOISESUPPRESSION_T48 48
85#define MXT_SPT_COMMSCONFIG_T18 18
86#define MXT_SPT_GPIOPWM_T19 19
87#define MXT_SPT_SELFTEST_T25 25
88#define MXT_SPT_CTECONFIG_T28 28
89#define MXT_SPT_USERDATA_T38 38
90#define MXT_SPT_DIGITIZER_T43 43
91#define MXT_SPT_MESSAGECOUNT_T44 44
92#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070093
Iiro Valkonene8645592011-11-18 12:56:19 -080094/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080095#define MXT_COMMAND_RESET 0
96#define MXT_COMMAND_BACKUPNV 1
97#define MXT_COMMAND_CALIBRATE 2
98#define MXT_COMMAND_REPORTALL 3
99#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700100
Iiro Valkonene8645592011-11-18 12:56:19 -0800101/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800102#define MXT_POWER_IDLEACQINT 0
103#define MXT_POWER_ACTVACQINT 1
104#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700105
Iiro Valkonene8645592011-11-18 12:56:19 -0800106/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800107#define MXT_ACQUIRE_CHRGTIME 0
108#define MXT_ACQUIRE_TCHDRIFT 2
109#define MXT_ACQUIRE_DRIFTST 3
110#define MXT_ACQUIRE_TCHAUTOCAL 4
111#define MXT_ACQUIRE_SYNC 5
112#define MXT_ACQUIRE_ATCHCALST 6
113#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700114
Iiro Valkonene8645592011-11-18 12:56:19 -0800115/* MXT_TOUCH_MULT_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800116#define MXT_TOUCH_CTRL 0
117#define MXT_TOUCH_XORIGIN 1
118#define MXT_TOUCH_YORIGIN 2
119#define MXT_TOUCH_XSIZE 3
120#define MXT_TOUCH_YSIZE 4
121#define MXT_TOUCH_BLEN 6
122#define MXT_TOUCH_TCHTHR 7
123#define MXT_TOUCH_TCHDI 8
124#define MXT_TOUCH_ORIENT 9
125#define MXT_TOUCH_MOVHYSTI 11
126#define MXT_TOUCH_MOVHYSTN 12
127#define MXT_TOUCH_NUMTOUCH 14
128#define MXT_TOUCH_MRGHYST 15
129#define MXT_TOUCH_MRGTHR 16
130#define MXT_TOUCH_AMPHYST 17
131#define MXT_TOUCH_XRANGE_LSB 18
132#define MXT_TOUCH_XRANGE_MSB 19
133#define MXT_TOUCH_YRANGE_LSB 20
134#define MXT_TOUCH_YRANGE_MSB 21
135#define MXT_TOUCH_XLOCLIP 22
136#define MXT_TOUCH_XHICLIP 23
137#define MXT_TOUCH_YLOCLIP 24
138#define MXT_TOUCH_YHICLIP 25
139#define MXT_TOUCH_XEDGECTRL 26
140#define MXT_TOUCH_XEDGEDIST 27
141#define MXT_TOUCH_YEDGECTRL 28
142#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700143#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700144
Iiro Valkonene8645592011-11-18 12:56:19 -0800145/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800146#define MXT_GRIPFACE_CTRL 0
147#define MXT_GRIPFACE_XLOGRIP 1
148#define MXT_GRIPFACE_XHIGRIP 2
149#define MXT_GRIPFACE_YLOGRIP 3
150#define MXT_GRIPFACE_YHIGRIP 4
151#define MXT_GRIPFACE_MAXTCHS 5
152#define MXT_GRIPFACE_SZTHR1 7
153#define MXT_GRIPFACE_SZTHR2 8
154#define MXT_GRIPFACE_SHPTHR1 9
155#define MXT_GRIPFACE_SHPTHR2 10
156#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700157
Iiro Valkonen7686b102011-02-02 23:21:58 -0800158/* MXT_PROCI_NOISE field */
159#define MXT_NOISE_CTRL 0
160#define MXT_NOISE_OUTFLEN 1
161#define MXT_NOISE_GCAFUL_LSB 3
162#define MXT_NOISE_GCAFUL_MSB 4
163#define MXT_NOISE_GCAFLL_LSB 5
164#define MXT_NOISE_GCAFLL_MSB 6
165#define MXT_NOISE_ACTVGCAFVALID 7
166#define MXT_NOISE_NOISETHR 8
167#define MXT_NOISE_FREQHOPSCALE 10
168#define MXT_NOISE_FREQ0 11
169#define MXT_NOISE_FREQ1 12
170#define MXT_NOISE_FREQ2 13
171#define MXT_NOISE_FREQ3 14
172#define MXT_NOISE_FREQ4 15
173#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700174
Iiro Valkonene8645592011-11-18 12:56:19 -0800175/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800176#define MXT_COMMS_CTRL 0
177#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700178
Iiro Valkonene8645592011-11-18 12:56:19 -0800179/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800180#define MXT_CTE_CTRL 0
181#define MXT_CTE_CMD 1
182#define MXT_CTE_MODE 2
183#define MXT_CTE_IDLEGCAFDEPTH 3
184#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700185#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700186
Iiro Valkonen7686b102011-02-02 23:21:58 -0800187#define MXT_VOLTAGE_DEFAULT 2700000
188#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700189
Amy Maloche21115eb2011-11-02 09:04:37 -0700190/* Analog voltage @2.7 V */
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530191#define MXT_VTG_MIN_UV 2700000
192#define MXT_VTG_MAX_UV 3300000
193#define MXT_ACTIVE_LOAD_UA 15000
Jing Linbace50b2011-10-18 22:55:47 -0700194#define MXT_LPM_LOAD_UA 10
Amy Maloche21115eb2011-11-02 09:04:37 -0700195/* Digital voltage @1.8 V */
196#define MXT_VTG_DIG_MIN_UV 1800000
197#define MXT_VTG_DIG_MAX_UV 1800000
198#define MXT_ACTIVE_LOAD_DIG_UA 10000
199#define MXT_LPM_LOAD_DIG_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530200
201#define MXT_I2C_VTG_MIN_UV 1800000
202#define MXT_I2C_VTG_MAX_UV 1800000
203#define MXT_I2C_LOAD_UA 10000
Jing Linbace50b2011-10-18 22:55:47 -0700204#define MXT_I2C_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530205
Iiro Valkonene8645592011-11-18 12:56:19 -0800206/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800207#define MXT_BOOT_VALUE 0xa5
208#define MXT_BACKUP_VALUE 0x55
209#define MXT_BACKUP_TIME 25 /* msec */
Jing Linc7fc4052011-12-21 16:16:19 -0800210#define MXT224_RESET_TIME 65 /* msec */
211#define MXT224E_RESET_TIME 22 /* msec */
212#define MXT1386_RESET_TIME 250 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700213#define MXT_RESET_TIME 250 /* msec */
Jing Linc7fc4052011-12-21 16:16:19 -0800214#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700215
Iiro Valkonen7686b102011-02-02 23:21:58 -0800216#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217
Jing Lin36aee812011-10-17 17:17:28 -0700218#define MXT_WAKE_TIME 25
219
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700220/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800221#define MXT_UNLOCK_CMD_MSB 0xaa
222#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700223
224/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800225#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
226#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
227#define MXT_FRAME_CRC_CHECK 0x02
228#define MXT_FRAME_CRC_FAIL 0x03
229#define MXT_FRAME_CRC_PASS 0x04
230#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
231#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700232
233/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800234#define MXT_SUPPRESS (1 << 1)
235#define MXT_AMP (1 << 2)
236#define MXT_VECTOR (1 << 3)
237#define MXT_MOVE (1 << 4)
238#define MXT_RELEASE (1 << 5)
239#define MXT_PRESS (1 << 6)
240#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700241
Joonyoung Shim910d8052011-04-12 23:14:38 -0700242/* Touch orient bits */
243#define MXT_XY_SWITCH (1 << 0)
244#define MXT_X_INVERT (1 << 1)
245#define MXT_Y_INVERT (1 << 2)
246
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700247/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800248#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700249
Iiro Valkonen7686b102011-02-02 23:21:58 -0800250#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700251
Jing Lin36aee812011-10-17 17:17:28 -0700252#define T7_DATA_SIZE 3
253#define MXT_MAX_RW_TRIES 3
254#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530255
Iiro Valkonen7686b102011-02-02 23:21:58 -0800256struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700257 u8 family_id;
258 u8 variant_id;
259 u8 version;
260 u8 build;
261 u8 matrix_xsize;
262 u8 matrix_ysize;
263 u8 object_num;
264};
265
Iiro Valkonen7686b102011-02-02 23:21:58 -0800266struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700267 u8 type;
268 u16 start_address;
269 u8 size;
270 u8 instances;
271 u8 num_report_ids;
272
273 /* to map object and message */
274 u8 max_reportid;
275};
276
Iiro Valkonen7686b102011-02-02 23:21:58 -0800277struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700278 u8 reportid;
279 u8 message[7];
280 u8 checksum;
281};
282
Iiro Valkonen7686b102011-02-02 23:21:58 -0800283struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700284 int status;
285 int x;
286 int y;
287 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700288 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700289};
290
291/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800292struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700293 struct i2c_client *client;
294 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800295 const struct mxt_platform_data *pdata;
Jing Lindc4413c2012-01-16 15:22:52 -0800296 const struct mxt_config_info *config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800297 struct mxt_object *object_table;
298 struct mxt_info info;
299 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700300 unsigned int irq;
Amy Maloche21115eb2011-11-02 09:04:37 -0700301 struct regulator *vcc_ana;
302 struct regulator *vcc_dig;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530303 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530304#if defined(CONFIG_HAS_EARLYSUSPEND)
305 struct early_suspend early_suspend;
306#endif
Jing Lin36aee812011-10-17 17:17:28 -0700307
Amy Maloche52262212011-09-15 16:46:57 -0700308 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700309 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700310 u8 t9_ctrl;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800311 u32 keyarray_old;
312 u32 keyarray_new;
313 u8 t9_max_reportid;
314 u8 t9_min_reportid;
315 u8 t15_max_reportid;
316 u8 t15_min_reportid;
Jing Lindc4413c2012-01-16 15:22:52 -0800317 u8 curr_cfg_version;
318 int cfg_version_idx;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700319};
320
Iiro Valkonen7686b102011-02-02 23:21:58 -0800321static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700322{
323 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800324 case MXT_GEN_MESSAGE_T5:
325 case MXT_GEN_COMMAND_T6:
326 case MXT_GEN_POWER_T7:
327 case MXT_GEN_ACQUIRE_T8:
328 case MXT_GEN_DATASOURCE_T53:
329 case MXT_TOUCH_MULTI_T9:
330 case MXT_TOUCH_KEYARRAY_T15:
331 case MXT_TOUCH_PROXIMITY_T23:
332 case MXT_TOUCH_PROXKEY_T52:
333 case MXT_PROCI_GRIPFACE_T20:
334 case MXT_PROCG_NOISE_T22:
335 case MXT_PROCI_ONETOUCH_T24:
336 case MXT_PROCI_TWOTOUCH_T27:
337 case MXT_PROCI_GRIP_T40:
338 case MXT_PROCI_PALM_T41:
339 case MXT_PROCI_TOUCHSUPPRESSION_T42:
340 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800341 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800342 case MXT_PROCG_NOISESUPPRESSION_T48:
343 case MXT_SPT_COMMSCONFIG_T18:
344 case MXT_SPT_GPIOPWM_T19:
345 case MXT_SPT_SELFTEST_T25:
346 case MXT_SPT_CTECONFIG_T28:
347 case MXT_SPT_USERDATA_T38:
348 case MXT_SPT_DIGITIZER_T43:
349 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700350 return true;
351 default:
352 return false;
353 }
354}
355
Iiro Valkonen7686b102011-02-02 23:21:58 -0800356static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700357{
358 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800359 case MXT_GEN_COMMAND_T6:
360 case MXT_GEN_POWER_T7:
361 case MXT_GEN_ACQUIRE_T8:
362 case MXT_TOUCH_MULTI_T9:
363 case MXT_TOUCH_KEYARRAY_T15:
364 case MXT_TOUCH_PROXIMITY_T23:
365 case MXT_TOUCH_PROXKEY_T52:
366 case MXT_PROCI_GRIPFACE_T20:
367 case MXT_PROCG_NOISE_T22:
368 case MXT_PROCI_ONETOUCH_T24:
369 case MXT_PROCI_TWOTOUCH_T27:
370 case MXT_PROCI_GRIP_T40:
371 case MXT_PROCI_PALM_T41:
372 case MXT_PROCI_TOUCHSUPPRESSION_T42:
373 case MXT_PROCI_STYLUS_T47:
Jing Linc7fc4052011-12-21 16:16:19 -0800374 case MXT_PROCI_SHIELDLESS_T56:
Iiro Valkonene8645592011-11-18 12:56:19 -0800375 case MXT_PROCG_NOISESUPPRESSION_T48:
376 case MXT_SPT_COMMSCONFIG_T18:
377 case MXT_SPT_GPIOPWM_T19:
378 case MXT_SPT_SELFTEST_T25:
379 case MXT_SPT_CTECONFIG_T28:
380 case MXT_SPT_USERDATA_T38:
381 case MXT_SPT_DIGITIZER_T43:
382 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700383 return true;
384 default:
385 return false;
386 }
387}
388
Iiro Valkonen7686b102011-02-02 23:21:58 -0800389static void mxt_dump_message(struct device *dev,
390 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700391{
392 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
393 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
394 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
395 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
396 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
397 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
398 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
399 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
400 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
401}
402
Iiro Valkonen7686b102011-02-02 23:21:58 -0800403static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700404 unsigned int state)
405{
406 u8 val;
407
408recheck:
409 if (i2c_master_recv(client, &val, 1) != 1) {
410 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
411 return -EIO;
412 }
413
414 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800415 case MXT_WAITING_BOOTLOAD_CMD:
416 case MXT_WAITING_FRAME_DATA:
417 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700418 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800419 case MXT_FRAME_CRC_PASS:
420 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700421 goto recheck;
422 break;
423 default:
424 return -EINVAL;
425 }
426
427 if (val != state) {
428 dev_err(&client->dev, "Unvalid bootloader mode state\n");
429 return -EINVAL;
430 }
431
432 return 0;
433}
434
Iiro Valkonen7686b102011-02-02 23:21:58 -0800435static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700436{
437 u8 buf[2];
438
Iiro Valkonen7686b102011-02-02 23:21:58 -0800439 buf[0] = MXT_UNLOCK_CMD_LSB;
440 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700441
442 if (i2c_master_send(client, buf, 2) != 2) {
443 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
444 return -EIO;
445 }
446
447 return 0;
448}
449
Iiro Valkonen7686b102011-02-02 23:21:58 -0800450static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700451 const u8 *data, unsigned int frame_size)
452{
453 if (i2c_master_send(client, data, frame_size) != frame_size) {
454 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
455 return -EIO;
456 }
457
458 return 0;
459}
460
Iiro Valkonen7686b102011-02-02 23:21:58 -0800461static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700462 u16 reg, u16 len, void *val)
463{
464 struct i2c_msg xfer[2];
465 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700466 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700467
468 buf[0] = reg & 0xff;
469 buf[1] = (reg >> 8) & 0xff;
470
471 /* Write register */
472 xfer[0].addr = client->addr;
473 xfer[0].flags = 0;
474 xfer[0].len = 2;
475 xfer[0].buf = buf;
476
477 /* Read data */
478 xfer[1].addr = client->addr;
479 xfer[1].flags = I2C_M_RD;
480 xfer[1].len = len;
481 xfer[1].buf = val;
482
Jing Lin36aee812011-10-17 17:17:28 -0700483 do {
484 if (i2c_transfer(client->adapter, xfer, 2) == 2)
485 return 0;
486 msleep(MXT_WAKE_TIME);
487 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488
Jing Lin36aee812011-10-17 17:17:28 -0700489 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
490 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491}
492
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700494{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800495 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700496}
497
Jing Lin36aee812011-10-17 17:17:28 -0700498static int __mxt_write_reg(struct i2c_client *client,
499 u16 addr, u16 length, u8 *value)
500{
501 u8 buf[MXT_BLOCK_SIZE + 2];
502 int i, tries = 0;
503
504 if (length > MXT_BLOCK_SIZE)
505 return -EINVAL;
506
507 buf[0] = addr & 0xff;
508 buf[1] = (addr >> 8) & 0xff;
509 for (i = 0; i < length; i++)
510 buf[i + 2] = *value++;
511
512 do {
513 if (i2c_master_send(client, buf, length + 2) == (length + 2))
514 return 0;
515 msleep(MXT_WAKE_TIME);
516 } while (++tries < MXT_MAX_RW_TRIES);
517
518 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
519 return -EIO;
520}
521
Iiro Valkonen7686b102011-02-02 23:21:58 -0800522static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700523{
Jing Lin36aee812011-10-17 17:17:28 -0700524 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525}
526
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528 u16 reg, u8 *object_buf)
529{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800530 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700531 object_buf);
532}
533
Iiro Valkonen7686b102011-02-02 23:21:58 -0800534static struct mxt_object *
535mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538 int i;
539
540 for (i = 0; i < data->info.object_num; i++) {
541 object = data->object_table + i;
542 if (object->type == type)
543 return object;
544 }
545
546 dev_err(&data->client->dev, "Invalid object type\n");
547 return NULL;
548}
549
Iiro Valkonen7686b102011-02-02 23:21:58 -0800550static int mxt_read_message(struct mxt_data *data,
551 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700552{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800553 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554 u16 reg;
555
Iiro Valkonene8645592011-11-18 12:56:19 -0800556 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557 if (!object)
558 return -EINVAL;
559
560 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800561 return __mxt_read_reg(data->client, reg,
562 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700563}
564
Iiro Valkonen7686b102011-02-02 23:21:58 -0800565static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700566 u8 type, u8 offset, u8 *val)
567{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800568 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569 u16 reg;
570
Iiro Valkonen7686b102011-02-02 23:21:58 -0800571 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572 if (!object)
573 return -EINVAL;
574
575 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800576 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577}
578
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580 u8 type, u8 offset, u8 val)
581{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800582 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700583 u16 reg;
584
Iiro Valkonen7686b102011-02-02 23:21:58 -0800585 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586 if (!object)
587 return -EINVAL;
588
589 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591}
592
Iiro Valkonen7686b102011-02-02 23:21:58 -0800593static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800595 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700596 struct input_dev *input_dev = data->input_dev;
597 int status = finger[single_id].status;
598 int finger_num = 0;
599 int id;
600
Iiro Valkonen7686b102011-02-02 23:21:58 -0800601 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700602 if (!finger[id].status)
603 continue;
604
Amy Maloche2b59bab2011-10-13 16:08:16 -0700605 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
606 finger[id].status != MXT_RELEASE ?
607 finger[id].area : 0);
608 input_report_abs(input_dev, ABS_MT_POSITION_X,
609 finger[id].x);
610 input_report_abs(input_dev, ABS_MT_POSITION_Y,
611 finger[id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700612 input_report_abs(input_dev, ABS_MT_PRESSURE,
Mohan Pallaka5e7343f2012-01-02 18:30:16 +0800613 finger[id].status != MXT_RELEASE ?
614 finger[id].pressure : 0);
Amy Maloche2b59bab2011-10-13 16:08:16 -0700615 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616
Amy Maloche2b59bab2011-10-13 16:08:16 -0700617 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700618 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700619 else
620 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700621 }
622
623 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
624
Iiro Valkonen7686b102011-02-02 23:21:58 -0800625 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700626 input_report_abs(input_dev, ABS_X, finger[single_id].x);
627 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700628 input_report_abs(input_dev,
629 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700630 }
631
632 input_sync(input_dev);
633}
634
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635static void mxt_input_touchevent(struct mxt_data *data,
636 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800638 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639 struct device *dev = &data->client->dev;
640 u8 status = message->message[0];
641 int x;
642 int y;
643 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700644 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700645
646 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800647 if (!(status & MXT_DETECT)) {
648 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700649 dev_dbg(dev, "[%d] released\n", id);
650
Iiro Valkonen7686b102011-02-02 23:21:58 -0800651 finger[id].status = MXT_RELEASE;
652 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653 }
654 return;
655 }
656
657 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800658 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700659 return;
660
Joonyoung Shim910d8052011-04-12 23:14:38 -0700661 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
662 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700663 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700664 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700665 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700666 y = y >> 2;
667
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700669 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700670
671 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800672 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700673 x, y, area);
674
Iiro Valkonen7686b102011-02-02 23:21:58 -0800675 finger[id].status = status & MXT_MOVE ?
676 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677 finger[id].x = x;
678 finger[id].y = y;
679 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700680 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700681
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700683}
684
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800685static void mxt_handle_key_array(struct mxt_data *data,
686 struct mxt_message *message)
687{
688 u32 keys_changed;
689 int i;
690
691 if (!data->pdata->key_codes) {
692 dev_err(&data->client->dev, "keyarray is not supported\n");
693 return;
694 }
695
696 data->keyarray_new = message->message[1] |
697 (message->message[2] << 8) |
698 (message->message[3] << 16) |
699 (message->message[4] << 24);
700
701 keys_changed = data->keyarray_old ^ data->keyarray_new;
702
703 if (!keys_changed) {
704 dev_dbg(&data->client->dev, "no keys changed\n");
705 return;
706 }
707
708 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
709 if (!(keys_changed & (1 << i)))
710 continue;
711
712 input_report_key(data->input_dev, data->pdata->key_codes[i],
713 (data->keyarray_new & (1 << i)));
714 input_sync(data->input_dev);
715 }
716
717 data->keyarray_old = data->keyarray_new;
718}
719
Iiro Valkonen7686b102011-02-02 23:21:58 -0800720static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800722 struct mxt_data *data = dev_id;
723 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700724 struct device *dev = &data->client->dev;
725 int id;
726 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727
728 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730 dev_err(dev, "Failed to read message\n");
731 goto end;
732 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700733 reportid = message.reportid;
734
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800735 if (!reportid) {
736 dev_dbg(dev, "Report id 0 is reserved\n");
737 continue;
738 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800740 /* check whether report id is part of T9 or T15 */
741 id = reportid - data->t9_min_reportid;
742
743 if (reportid >= data->t9_min_reportid &&
744 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800745 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800746 else if (reportid >= data->t15_min_reportid &&
747 reportid <= data->t15_max_reportid)
748 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800750 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700751 } while (reportid != 0xff);
752
753end:
754 return IRQ_HANDLED;
755}
756
Iiro Valkonen7686b102011-02-02 23:21:58 -0800757static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758{
Jing Lindc4413c2012-01-16 15:22:52 -0800759 const struct mxt_config_info *config_info = data->config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800760 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761 struct device *dev = &data->client->dev;
762 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800763 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700764
Jing Lindc4413c2012-01-16 15:22:52 -0800765 if (!config_info) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800766 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
767 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700768 }
769
770 for (i = 0; i < data->info.object_num; i++) {
771 object = data->object_table + i;
772
Iiro Valkonen7686b102011-02-02 23:21:58 -0800773 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700774 continue;
775
Iiro Valkonen71749f52011-02-15 13:36:52 -0800776 for (j = 0; j < object->size + 1; j++) {
777 config_offset = index + j;
Jing Lindc4413c2012-01-16 15:22:52 -0800778 if (config_offset > config_info->config_length) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800779 dev_err(dev, "Not enough config data!\n");
780 return -EINVAL;
781 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800782 mxt_write_object(data, object->type, j,
Jing Lindc4413c2012-01-16 15:22:52 -0800783 config_info->config[config_offset]);
Iiro Valkonen71749f52011-02-15 13:36:52 -0800784 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700785 index += object->size + 1;
786 }
787
788 return 0;
789}
790
Iiro Valkonen7686b102011-02-02 23:21:58 -0800791static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700792{
793 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800794 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700795 int count = 10;
796 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700797
798 /* Read dummy message to make high CHG pin */
799 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800800 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801 if (error)
802 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800803 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700804
805 if (!count) {
806 dev_err(dev, "CHG pin isn't cleared\n");
807 return -EBUSY;
808 }
809
810 return 0;
811}
812
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814{
815 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800816 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700817 int error;
818 u8 val;
819
Iiro Valkonen7686b102011-02-02 23:21:58 -0800820 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700821 if (error)
822 return error;
823 info->family_id = val;
824
Iiro Valkonen7686b102011-02-02 23:21:58 -0800825 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700826 if (error)
827 return error;
828 info->variant_id = val;
829
Iiro Valkonen7686b102011-02-02 23:21:58 -0800830 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831 if (error)
832 return error;
833 info->version = val;
834
Iiro Valkonen7686b102011-02-02 23:21:58 -0800835 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700836 if (error)
837 return error;
838 info->build = val;
839
Iiro Valkonen7686b102011-02-02 23:21:58 -0800840 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700841 if (error)
842 return error;
843 info->object_num = val;
844
845 return 0;
846}
847
Iiro Valkonen7686b102011-02-02 23:21:58 -0800848static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700849{
850 int error;
851 int i;
852 u16 reg;
853 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800854 u8 buf[MXT_OBJECT_SIZE];
Jing Lindc4413c2012-01-16 15:22:52 -0800855 bool found_t38 = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856
857 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700859
Iiro Valkonen7686b102011-02-02 23:21:58 -0800860 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
861 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700862 if (error)
863 return error;
864
865 object->type = buf[0];
866 object->start_address = (buf[2] << 8) | buf[1];
867 object->size = buf[3];
868 object->instances = buf[4];
869 object->num_report_ids = buf[5];
870
871 if (object->num_report_ids) {
872 reportid += object->num_report_ids *
873 (object->instances + 1);
874 object->max_reportid = reportid;
875 }
Jing Lindc4413c2012-01-16 15:22:52 -0800876
877 /* Calculate index for config major version in config array.
878 * Major version is the first byte in object T38.
879 */
880 if (object->type == MXT_SPT_USERDATA_T38)
881 found_t38 = true;
882 if (!found_t38 && mxt_object_writable(object->type))
883 data->cfg_version_idx += object->size + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700884 }
885
886 return 0;
887}
888
Jing Lindc4413c2012-01-16 15:22:52 -0800889static int mxt_search_config_array(struct mxt_data *data, bool version_match)
890{
891
892 const struct mxt_platform_data *pdata = data->pdata;
893 const struct mxt_config_info *cfg_info;
894 struct mxt_info *info = &data->info;
895 int i;
896 u8 cfg_version;
897
898 for (i = 0; i < pdata->config_array_size; i++) {
899
900 cfg_info = &pdata->config_array[i];
901
902 if (!cfg_info->config || !cfg_info->config_length)
903 continue;
904
905 if (info->family_id == cfg_info->family_id &&
906 info->variant_id == cfg_info->variant_id &&
907 info->version == cfg_info->version &&
908 info->build == cfg_info->build) {
909
910 cfg_version = cfg_info->config[data->cfg_version_idx];
911 if (data->curr_cfg_version == cfg_version ||
912 !version_match) {
913 data->config_info = cfg_info;
914 return 0;
915 }
916 }
917 }
918
919 dev_info(&data->client->dev,
920 "Config not found: F: %d, V: %d, FW: %d.%d.%d, CFG: %d\n",
921 info->family_id, info->variant_id,
922 info->version >> 4, info->version & 0xF, info->build,
923 data->curr_cfg_version);
924 return -EINVAL;
925}
926
927static int mxt_get_config(struct mxt_data *data)
928{
929 const struct mxt_platform_data *pdata = data->pdata;
930 struct device *dev = &data->client->dev;
931 struct mxt_object *object;
932 int error;
933
934 if (!pdata->config_array || !pdata->config_array_size) {
935 dev_dbg(dev, "No cfg data provided by platform data\n");
936 return 0;
937 }
938
939 /* Get current config version */
940 object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
941 if (!object) {
942 dev_err(dev, "Unable to obtain USERDATA object\n");
943 return -EINVAL;
944 }
945
946 error = mxt_read_reg(data->client, object->start_address,
947 &data->curr_cfg_version);
948 if (error) {
949 dev_err(dev, "Unable to read config version\n");
950 return error;
951 }
952
953 /* It is possible that the config data on the controller is not
954 * versioned and the version number returns 0. In this case,
955 * find a match without the config version checking.
956 */
957 error = mxt_search_config_array(data,
958 data->curr_cfg_version != 0 ? true : false);
959 if (error)
960 return error;
961
962 return 0;
963}
Amy Maloche7e447432011-09-14 11:36:30 -0700964static void mxt_reset_delay(struct mxt_data *data)
965{
966 struct mxt_info *info = &data->info;
967
968 switch (info->family_id) {
969 case MXT224_ID:
970 msleep(MXT224_RESET_TIME);
971 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -0700972 case MXT224E_ID:
973 msleep(MXT224E_RESET_TIME);
974 break;
Amy Maloche7e447432011-09-14 11:36:30 -0700975 case MXT1386_ID:
976 msleep(MXT1386_RESET_TIME);
977 break;
978 default:
979 msleep(MXT_RESET_TIME);
980 }
981}
982
Iiro Valkonen7686b102011-02-02 23:21:58 -0800983static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700984{
985 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800986 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700987 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700988 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700989 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700990 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700991 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800992 struct mxt_object *t9_object;
993 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700994
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996 if (error)
997 return error;
998
999 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001000 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001 GFP_KERNEL);
1002 if (!data->object_table) {
1003 dev_err(&client->dev, "Failed to allocate memory\n");
1004 return -ENOMEM;
1005 }
1006
1007 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001008 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001010 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001011
Jing Lindc4413c2012-01-16 15:22:52 -08001012 /* Get config data from platform data */
1013 error = mxt_get_config(data);
1014 if (error)
1015 dev_dbg(&client->dev, "Config info not found.\n");
1016
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001017 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001018 error = mxt_check_reg_init(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
Amy Maloche52262212011-09-15 16:46:57 -07001022 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -08001023 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -07001024 if (!t7_object) {
1025 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -07001026 error = -EINVAL;
1027 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -07001028 }
1029
1030 data->t7_start_addr = t7_object->start_address;
1031 error = __mxt_read_reg(client, data->t7_start_addr,
1032 T7_DATA_SIZE, data->t7_data);
1033 if (error < 0) {
1034 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -07001035 "Failed to save current power state\n");
1036 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001037 }
Iiro Valkonene8645592011-11-18 12:56:19 -08001038 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -07001039 &data->t9_ctrl);
1040 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -07001041 dev_err(&client->dev, "Failed to save current touch object\n");
1042 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001043 }
1044
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001045 /* Store T9, T15's min and max report ids */
1046 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1047 if (!t9_object) {
1048 dev_err(&client->dev, "Failed to get T9 object\n");
1049 error = -EINVAL;
1050 goto free_object_table;
1051 }
1052 data->t9_max_reportid = t9_object->max_reportid;
1053 data->t9_min_reportid = t9_object->max_reportid -
1054 t9_object->num_report_ids + 1;
1055
1056 if (data->pdata->key_codes) {
1057 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
1058 if (!t15_object)
1059 dev_dbg(&client->dev, "T15 object is not available\n");
1060 else {
1061 data->t15_max_reportid = t15_object->max_reportid;
1062 data->t15_min_reportid = t15_object->max_reportid -
1063 t15_object->num_report_ids + 1;
1064 }
1065 }
1066
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -08001068 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001069 MXT_COMMAND_BACKUPNV,
1070 MXT_BACKUP_VALUE);
1071 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001072 do {
Iiro Valkonene8645592011-11-18 12:56:19 -08001073 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001074 MXT_COMMAND_BACKUPNV,
1075 &command_register);
1076 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001077 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -07001078 usleep_range(1000, 2000);
1079 } while ((command_register != 0) && (++timeout_counter <= 100));
1080 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001081 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -07001082 error = -EIO;
1083 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001084 }
1085
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001086
1087 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -08001088 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001089 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001090
Amy Maloche7e447432011-09-14 11:36:30 -07001091 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001092
1093 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001094 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001096 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097 info->matrix_xsize = val;
1098
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001101 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001102 info->matrix_ysize = val;
1103
1104 dev_info(&client->dev,
1105 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
1106 info->family_id, info->variant_id, info->version,
1107 info->build);
1108
1109 dev_info(&client->dev,
1110 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
1111 info->matrix_xsize, info->matrix_ysize,
1112 info->object_num);
1113
1114 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001115
1116free_object_table:
1117 kfree(data->object_table);
1118 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001119}
1120
Iiro Valkonen7686b102011-02-02 23:21:58 -08001121static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001122 struct device_attribute *attr, char *buf)
1123{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001124 struct mxt_data *data = dev_get_drvdata(dev);
1125 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001126 int count = 0;
1127 int i, j;
1128 int error;
1129 u8 val;
1130
1131 for (i = 0; i < data->info.object_num; i++) {
1132 object = data->object_table + i;
1133
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001134 count += snprintf(buf + count, PAGE_SIZE - count,
1135 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001136 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001137 if (count >= PAGE_SIZE)
1138 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001139
Iiro Valkonen7686b102011-02-02 23:21:58 -08001140 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001141 count += snprintf(buf + count, PAGE_SIZE - count,
1142 "\n");
1143 if (count >= PAGE_SIZE)
1144 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001145 continue;
1146 }
1147
1148 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001149 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001150 object->type, j, &val);
1151 if (error)
1152 return error;
1153
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001154 count += snprintf(buf + count, PAGE_SIZE - count,
1155 "\t[%2d]: %02x (%d)\n", j, val, val);
1156 if (count >= PAGE_SIZE)
1157 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001158 }
1159
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001160 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1161 if (count >= PAGE_SIZE)
1162 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001163 }
1164
1165 return count;
1166}
1167
Iiro Valkonen7686b102011-02-02 23:21:58 -08001168static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001169{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001170 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001171 struct i2c_client *client = data->client;
1172 const struct firmware *fw = NULL;
1173 unsigned int frame_size;
1174 unsigned int pos = 0;
1175 int ret;
1176
1177 ret = request_firmware(&fw, fn, dev);
1178 if (ret) {
1179 dev_err(dev, "Unable to open firmware %s\n", fn);
1180 return ret;
1181 }
1182
1183 /* Change to the bootloader mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001184 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001185 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001186
1187 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001188
1189 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001190 if (client->addr == MXT_APP_LOW)
1191 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001192 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001193 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001194
Iiro Valkonen7686b102011-02-02 23:21:58 -08001195 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001196 if (ret)
1197 goto out;
1198
1199 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001200 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201
1202 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001203 ret = mxt_check_bootloader(client,
1204 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001205 if (ret)
1206 goto out;
1207
1208 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1209
1210 /* We should add 2 at frame size as the the firmware data is not
1211 * included the CRC bytes.
1212 */
1213 frame_size += 2;
1214
1215 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001216 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001217
Iiro Valkonen7686b102011-02-02 23:21:58 -08001218 ret = mxt_check_bootloader(client,
1219 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001220 if (ret)
1221 goto out;
1222
1223 pos += frame_size;
1224
1225 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1226 }
1227
1228out:
1229 release_firmware(fw);
1230
1231 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001232 if (client->addr == MXT_BOOT_LOW)
1233 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001234 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001235 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001236
1237 return ret;
1238}
1239
Iiro Valkonen7686b102011-02-02 23:21:58 -08001240static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001241 struct device_attribute *attr,
1242 const char *buf, size_t count)
1243{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001244 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001245 int error;
1246
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247 disable_irq(data->irq);
1248
Iiro Valkonen7686b102011-02-02 23:21:58 -08001249 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001250 if (error) {
1251 dev_err(dev, "The firmware update failed(%d)\n", error);
1252 count = error;
1253 } else {
1254 dev_dbg(dev, "The firmware update succeeded\n");
1255
1256 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001257 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001258
1259 kfree(data->object_table);
1260 data->object_table = NULL;
1261
Iiro Valkonen7686b102011-02-02 23:21:58 -08001262 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001263 }
1264
1265 enable_irq(data->irq);
1266
Iiro Valkonen08960a02011-04-12 23:16:40 -07001267 error = mxt_make_highchg(data);
1268 if (error)
1269 return error;
1270
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001271 return count;
1272}
1273
Iiro Valkonen7686b102011-02-02 23:21:58 -08001274static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1275static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001276
Iiro Valkonen7686b102011-02-02 23:21:58 -08001277static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001278 &dev_attr_object.attr,
1279 &dev_attr_update_fw.attr,
1280 NULL
1281};
1282
Iiro Valkonen7686b102011-02-02 23:21:58 -08001283static const struct attribute_group mxt_attr_group = {
1284 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001285};
1286
Amy Maloche52262212011-09-15 16:46:57 -07001287static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001288{
Jing Lin36aee812011-10-17 17:17:28 -07001289 int error;
1290
Amy Maloche52262212011-09-15 16:46:57 -07001291 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001292 error = __mxt_write_reg(data->client, data->t7_start_addr,
1293 T7_DATA_SIZE, data->t7_data);
1294 if (error < 0) {
1295 dev_err(&data->client->dev,
1296 "failed to restore old power state\n");
1297 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001298 }
Jing Lin36aee812011-10-17 17:17:28 -07001299
Amy Maloche52262212011-09-15 16:46:57 -07001300 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001301 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001302 if (error < 0) {
1303 dev_err(&data->client->dev, "failed to restore touch\n");
1304 return error;
1305 }
1306
1307 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001308}
1309
Amy Maloche52262212011-09-15 16:46:57 -07001310static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001311{
Jing Lin36aee812011-10-17 17:17:28 -07001312 int error;
1313 u8 t7_data[T7_DATA_SIZE] = {0};
1314
1315 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001316 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001317 if (error < 0) {
1318 dev_err(&data->client->dev, "failed to disable touch\n");
1319 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001320 }
1321
Jing Lin36aee812011-10-17 17:17:28 -07001322 error = __mxt_write_reg(data->client, data->t7_start_addr,
1323 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001324 if (error < 0) {
1325 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001326 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001327 return error;
1328 }
1329
1330 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001331}
1332
Iiro Valkonen7686b102011-02-02 23:21:58 -08001333static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001334{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001335 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001336 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001337
Amy Maloche52262212011-09-15 16:46:57 -07001338 error = mxt_start(data);
1339 if (error < 0) {
1340 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1341 return error;
1342 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001343
1344 return 0;
1345}
1346
Iiro Valkonen7686b102011-02-02 23:21:58 -08001347static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001348{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001349 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001350 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001351
Amy Maloche52262212011-09-15 16:46:57 -07001352 error = mxt_stop(data);
1353 if (error < 0)
1354 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1355
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001356}
1357
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301358static int mxt_power_on(struct mxt_data *data, bool on)
1359{
1360 int rc;
1361
1362 if (on == false)
1363 goto power_off;
1364
Amy Maloche21115eb2011-11-02 09:04:37 -07001365 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301366 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001367 dev_err(&data->client->dev,
1368 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301369 return rc;
1370 }
1371
Amy Maloche21115eb2011-11-02 09:04:37 -07001372 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301373 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001374 dev_err(&data->client->dev,
1375 "Regulator vcc_ana enable failed rc=%d\n", rc);
1376 goto error_reg_en_vcc_ana;
1377 }
1378
1379 if (data->pdata->digital_pwr_regulator) {
1380 rc = regulator_set_optimum_mode(data->vcc_dig,
1381 MXT_ACTIVE_LOAD_DIG_UA);
1382 if (rc < 0) {
1383 dev_err(&data->client->dev,
1384 "Regulator vcc_dig set_opt failed rc=%d\n",
1385 rc);
1386 goto error_reg_opt_vcc_dig;
1387 }
1388
1389 rc = regulator_enable(data->vcc_dig);
1390 if (rc) {
1391 dev_err(&data->client->dev,
1392 "Regulator vcc_dig enable failed rc=%d\n", rc);
1393 goto error_reg_en_vcc_dig;
1394 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301395 }
1396
1397 if (data->pdata->i2c_pull_up) {
1398 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1399 if (rc < 0) {
1400 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001401 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301402 goto error_reg_opt_i2c;
1403 }
1404
1405 rc = regulator_enable(data->vcc_i2c);
1406 if (rc) {
1407 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001408 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301409 goto error_reg_en_vcc_i2c;
1410 }
1411 }
1412
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001413 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301414
1415 return 0;
1416
1417error_reg_en_vcc_i2c:
1418 if (data->pdata->i2c_pull_up)
1419 regulator_set_optimum_mode(data->vcc_i2c, 0);
1420error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001421 if (data->pdata->digital_pwr_regulator)
1422 regulator_disable(data->vcc_dig);
1423error_reg_en_vcc_dig:
1424 if (data->pdata->digital_pwr_regulator)
1425 regulator_set_optimum_mode(data->vcc_dig, 0);
1426error_reg_opt_vcc_dig:
1427 regulator_disable(data->vcc_ana);
1428error_reg_en_vcc_ana:
1429 regulator_set_optimum_mode(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301430 return rc;
1431
1432power_off:
Amy Maloche21115eb2011-11-02 09:04:37 -07001433 regulator_set_optimum_mode(data->vcc_ana, 0);
1434 regulator_disable(data->vcc_ana);
1435 if (data->pdata->digital_pwr_regulator) {
1436 regulator_set_optimum_mode(data->vcc_dig, 0);
1437 regulator_disable(data->vcc_dig);
1438 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301439 if (data->pdata->i2c_pull_up) {
1440 regulator_set_optimum_mode(data->vcc_i2c, 0);
1441 regulator_disable(data->vcc_i2c);
1442 }
1443 msleep(50);
1444 return 0;
1445}
1446
1447static int mxt_regulator_configure(struct mxt_data *data, bool on)
1448{
1449 int rc;
1450
1451 if (on == false)
1452 goto hw_shutdown;
1453
Amy Maloche21115eb2011-11-02 09:04:37 -07001454 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1455 if (IS_ERR(data->vcc_ana)) {
1456 rc = PTR_ERR(data->vcc_ana);
1457 dev_err(&data->client->dev,
1458 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301459 return rc;
1460 }
1461
Amy Maloche21115eb2011-11-02 09:04:37 -07001462 if (regulator_count_voltages(data->vcc_ana) > 0) {
1463 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301464 MXT_VTG_MAX_UV);
1465 if (rc) {
1466 dev_err(&data->client->dev,
1467 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001468 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301469 }
1470 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001471 if (data->pdata->digital_pwr_regulator) {
1472 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1473 if (IS_ERR(data->vcc_dig)) {
1474 rc = PTR_ERR(data->vcc_dig);
1475 dev_err(&data->client->dev,
1476 "Regulator get dig failed rc=%d\n", rc);
1477 goto error_get_vtg_vcc_dig;
1478 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301479
Amy Maloche21115eb2011-11-02 09:04:37 -07001480 if (regulator_count_voltages(data->vcc_dig) > 0) {
1481 rc = regulator_set_voltage(data->vcc_dig,
1482 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1483 if (rc) {
1484 dev_err(&data->client->dev,
1485 "regulator set_vtg failed rc=%d\n", rc);
1486 goto error_set_vtg_vcc_dig;
1487 }
1488 }
1489 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301490 if (data->pdata->i2c_pull_up) {
1491 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1492 if (IS_ERR(data->vcc_i2c)) {
1493 rc = PTR_ERR(data->vcc_i2c);
1494 dev_err(&data->client->dev,
1495 "Regulator get failed rc=%d\n", rc);
1496 goto error_get_vtg_i2c;
1497 }
1498 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1499 rc = regulator_set_voltage(data->vcc_i2c,
1500 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1501 if (rc) {
1502 dev_err(&data->client->dev,
1503 "regulator set_vtg failed rc=%d\n", rc);
1504 goto error_set_vtg_i2c;
1505 }
1506 }
1507 }
1508
1509 return 0;
1510
1511error_set_vtg_i2c:
1512 regulator_put(data->vcc_i2c);
1513error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001514 if (data->pdata->digital_pwr_regulator)
1515 if (regulator_count_voltages(data->vcc_dig) > 0)
1516 regulator_set_voltage(data->vcc_dig, 0,
1517 MXT_VTG_DIG_MAX_UV);
1518error_set_vtg_vcc_dig:
1519 if (data->pdata->digital_pwr_regulator)
1520 regulator_put(data->vcc_dig);
1521error_get_vtg_vcc_dig:
1522 if (regulator_count_voltages(data->vcc_ana) > 0)
1523 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1524error_set_vtg_vcc_ana:
1525 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301526 return rc;
1527
1528hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001529 if (regulator_count_voltages(data->vcc_ana) > 0)
1530 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1531 regulator_put(data->vcc_ana);
1532 if (data->pdata->digital_pwr_regulator) {
1533 if (regulator_count_voltages(data->vcc_dig) > 0)
1534 regulator_set_voltage(data->vcc_dig, 0,
1535 MXT_VTG_DIG_MAX_UV);
1536 regulator_put(data->vcc_dig);
1537 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301538 if (data->pdata->i2c_pull_up) {
1539 if (regulator_count_voltages(data->vcc_i2c) > 0)
1540 regulator_set_voltage(data->vcc_i2c, 0,
1541 MXT_I2C_VTG_MAX_UV);
1542 regulator_put(data->vcc_i2c);
1543 }
1544 return 0;
1545}
1546
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301547#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001548static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1549{
1550
1551 int rc;
1552
1553 if (on == false)
1554 goto regulator_hpm;
1555
Amy Maloche21115eb2011-11-02 09:04:37 -07001556 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001557 if (rc < 0) {
1558 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001559 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001560 goto fail_regulator_lpm;
1561 }
1562
Amy Maloche21115eb2011-11-02 09:04:37 -07001563 if (data->pdata->digital_pwr_regulator) {
1564 rc = regulator_set_optimum_mode(data->vcc_dig,
1565 MXT_LPM_LOAD_DIG_UA);
1566 if (rc < 0) {
1567 dev_err(&data->client->dev,
1568 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1569 goto fail_regulator_lpm;
1570 }
1571 }
1572
Jing Linbace50b2011-10-18 22:55:47 -07001573 if (data->pdata->i2c_pull_up) {
1574 rc = regulator_set_optimum_mode(data->vcc_i2c,
1575 MXT_I2C_LPM_LOAD_UA);
1576 if (rc < 0) {
1577 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001578 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001579 goto fail_regulator_lpm;
1580 }
1581 }
1582
1583 return 0;
1584
1585regulator_hpm:
1586
Amy Maloche21115eb2011-11-02 09:04:37 -07001587 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001588 if (rc < 0) {
1589 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001590 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001591 goto fail_regulator_hpm;
1592 }
1593
Amy Maloche21115eb2011-11-02 09:04:37 -07001594 if (data->pdata->digital_pwr_regulator) {
1595 rc = regulator_set_optimum_mode(data->vcc_dig,
1596 MXT_ACTIVE_LOAD_DIG_UA);
1597 if (rc < 0) {
1598 dev_err(&data->client->dev,
1599 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1600 goto fail_regulator_hpm;
1601 }
1602 }
1603
Jing Linbace50b2011-10-18 22:55:47 -07001604 if (data->pdata->i2c_pull_up) {
1605 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1606 if (rc < 0) {
1607 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001608 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001609 goto fail_regulator_hpm;
1610 }
1611 }
1612
1613 return 0;
1614
1615fail_regulator_lpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001616 regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
1617 if (data->pdata->digital_pwr_regulator)
1618 regulator_set_optimum_mode(data->vcc_dig,
1619 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001620 if (data->pdata->i2c_pull_up)
1621 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1622
1623 return rc;
1624
1625fail_regulator_hpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001626 regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
1627 if (data->pdata->digital_pwr_regulator)
1628 regulator_set_optimum_mode(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001629 if (data->pdata->i2c_pull_up)
1630 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1631
1632 return rc;
1633}
1634
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301635static int mxt_suspend(struct device *dev)
1636{
1637 struct i2c_client *client = to_i2c_client(dev);
1638 struct mxt_data *data = i2c_get_clientdata(client);
1639 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001640 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301641
1642 mutex_lock(&input_dev->mutex);
1643
Amy Maloche52262212011-09-15 16:46:57 -07001644 if (input_dev->users) {
1645 error = mxt_stop(data);
1646 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001647 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001648 mutex_unlock(&input_dev->mutex);
1649 return error;
1650 }
1651
1652 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301653
1654 mutex_unlock(&input_dev->mutex);
1655
Jing Linbace50b2011-10-18 22:55:47 -07001656 /* put regulators in low power mode */
1657 error = mxt_regulator_lpm(data, true);
1658 if (error < 0) {
1659 dev_err(dev, "failed to enter low power mode\n");
1660 return error;
1661 }
1662
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301663 return 0;
1664}
1665
1666static int mxt_resume(struct device *dev)
1667{
1668 struct i2c_client *client = to_i2c_client(dev);
1669 struct mxt_data *data = i2c_get_clientdata(client);
1670 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001671 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301672
Jing Linbace50b2011-10-18 22:55:47 -07001673 /* put regulators in high power mode */
1674 error = mxt_regulator_lpm(data, false);
1675 if (error < 0) {
1676 dev_err(dev, "failed to enter high power mode\n");
1677 return error;
1678 }
1679
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301680 mutex_lock(&input_dev->mutex);
1681
Amy Maloche52262212011-09-15 16:46:57 -07001682 if (input_dev->users) {
1683 error = mxt_start(data);
1684 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001685 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001686 mutex_unlock(&input_dev->mutex);
1687 return error;
1688 }
1689 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301690
1691 mutex_unlock(&input_dev->mutex);
1692
1693 return 0;
1694}
1695
1696#if defined(CONFIG_HAS_EARLYSUSPEND)
1697static void mxt_early_suspend(struct early_suspend *h)
1698{
1699 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1700
1701 mxt_suspend(&data->client->dev);
1702}
1703
1704static void mxt_late_resume(struct early_suspend *h)
1705{
1706 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1707
1708 mxt_resume(&data->client->dev);
1709}
1710#endif
1711
1712static const struct dev_pm_ops mxt_pm_ops = {
1713#ifndef CONFIG_HAS_EARLYSUSPEND
1714 .suspend = mxt_suspend,
1715 .resume = mxt_resume,
1716#endif
1717};
1718#endif
1719
Iiro Valkonen7686b102011-02-02 23:21:58 -08001720static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001721 const struct i2c_device_id *id)
1722{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001723 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001724 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001725 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001726 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001727
Iiro Valkonen919ed892011-02-15 13:36:52 -08001728 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001729 return -EINVAL;
1730
Iiro Valkonen7686b102011-02-02 23:21:58 -08001731 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001732 input_dev = input_allocate_device();
1733 if (!data || !input_dev) {
1734 dev_err(&client->dev, "Failed to allocate memory\n");
1735 error = -ENOMEM;
1736 goto err_free_mem;
1737 }
1738
Iiro Valkonen7686b102011-02-02 23:21:58 -08001739 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001740 input_dev->id.bustype = BUS_I2C;
1741 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001742 input_dev->open = mxt_input_open;
1743 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001744
Joonyoung Shim910d8052011-04-12 23:14:38 -07001745 data->client = client;
1746 data->input_dev = input_dev;
1747 data->pdata = pdata;
1748 data->irq = client->irq;
1749
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001750 __set_bit(EV_ABS, input_dev->evbit);
1751 __set_bit(EV_KEY, input_dev->evbit);
1752 __set_bit(BTN_TOUCH, input_dev->keybit);
1753
1754 /* For single touch */
1755 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001756 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001757 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001758 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001759 input_set_abs_params(input_dev, ABS_PRESSURE,
1760 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001761
1762 /* For multi touch */
1763 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001764 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001765 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001766 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001767 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001768 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001769 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1770 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001771
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001772 /* set key array supported keys */
1773 if (pdata->key_codes) {
1774 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
1775 if (pdata->key_codes[i])
1776 input_set_capability(input_dev, EV_KEY,
1777 pdata->key_codes[i]);
1778 }
1779 }
1780
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001781 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001782 i2c_set_clientdata(client, data);
1783
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301784 if (pdata->init_hw)
1785 error = pdata->init_hw(true);
1786 else
1787 error = mxt_regulator_configure(data, true);
1788 if (error) {
1789 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001790 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301791 }
1792
1793 if (pdata->power_on)
1794 error = pdata->power_on(true);
1795 else
1796 error = mxt_power_on(data, true);
1797 if (error) {
1798 dev_err(&client->dev, "Failed to power on hardware\n");
1799 goto err_regulator_on;
1800 }
1801
Amy Maloche08266db2011-11-04 11:07:16 -07001802 if (gpio_is_valid(pdata->irq_gpio)) {
1803 /* configure touchscreen irq gpio */
1804 error = gpio_request(pdata->irq_gpio,
1805 "mxt_irq_gpio");
1806 if (error) {
1807 pr_err("%s: unable to request gpio [%d]\n", __func__,
1808 pdata->irq_gpio);
1809 goto err_power_on;
1810 }
1811 error = gpio_direction_input(pdata->irq_gpio);
1812 if (error) {
1813 pr_err("%s: unable to set_direction for gpio [%d]\n",
1814 __func__, pdata->irq_gpio);
1815 goto err_irq_gpio_req;
1816 }
1817 }
1818
1819 if (gpio_is_valid(pdata->reset_gpio)) {
1820 /* configure touchscreen reset out gpio */
1821 error = gpio_request(pdata->reset_gpio,
1822 "mxt_reset_gpio");
1823 if (error) {
1824 pr_err("%s: unable to request reset gpio %d\n",
1825 __func__, pdata->reset_gpio);
1826 goto err_irq_gpio_req;
1827 }
1828
1829 error = gpio_direction_output(
1830 pdata->reset_gpio, 1);
1831 if (error) {
1832 pr_err("%s: unable to set direction for gpio %d\n",
1833 __func__, pdata->reset_gpio);
1834 goto err_reset_gpio_req;
1835 }
1836 }
1837
1838 mxt_reset_delay(data);
1839
Iiro Valkonen7686b102011-02-02 23:21:58 -08001840 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001841 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07001842 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001843
Iiro Valkonen7686b102011-02-02 23:21:58 -08001844 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001845 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001846 if (error) {
1847 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001848 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001849 }
1850
Iiro Valkonen08960a02011-04-12 23:16:40 -07001851 error = mxt_make_highchg(data);
1852 if (error)
1853 goto err_free_irq;
1854
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001855 error = input_register_device(input_dev);
1856 if (error)
1857 goto err_free_irq;
1858
Iiro Valkonen7686b102011-02-02 23:21:58 -08001859 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001860 if (error)
1861 goto err_unregister_device;
1862
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301863#if defined(CONFIG_HAS_EARLYSUSPEND)
1864 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1865 MXT_SUSPEND_LEVEL;
1866 data->early_suspend.suspend = mxt_early_suspend;
1867 data->early_suspend.resume = mxt_late_resume;
1868 register_early_suspend(&data->early_suspend);
1869#endif
1870
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001871 return 0;
1872
1873err_unregister_device:
1874 input_unregister_device(input_dev);
1875 input_dev = NULL;
1876err_free_irq:
1877 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001878err_free_object:
1879 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07001880err_reset_gpio_req:
1881 if (gpio_is_valid(pdata->reset_gpio))
1882 gpio_free(pdata->reset_gpio);
1883err_irq_gpio_req:
1884 if (gpio_is_valid(pdata->irq_gpio))
1885 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301886err_power_on:
1887 if (pdata->power_on)
1888 pdata->power_on(false);
1889 else
1890 mxt_power_on(data, false);
1891err_regulator_on:
1892 if (pdata->init_hw)
1893 pdata->init_hw(false);
1894 else
1895 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001896err_free_mem:
1897 input_free_device(input_dev);
1898 kfree(data);
1899 return error;
1900}
1901
Iiro Valkonen7686b102011-02-02 23:21:58 -08001902static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001903{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001904 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001905
Iiro Valkonen7686b102011-02-02 23:21:58 -08001906 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001907 free_irq(data->irq, data);
1908 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301909#if defined(CONFIG_HAS_EARLYSUSPEND)
1910 unregister_early_suspend(&data->early_suspend);
1911#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301912
1913 if (data->pdata->power_on)
1914 data->pdata->power_on(false);
1915 else
1916 mxt_power_on(data, false);
1917
1918 if (data->pdata->init_hw)
1919 data->pdata->init_hw(false);
1920 else
1921 mxt_regulator_configure(data, false);
1922
Mohan Pallakabfe8f302012-01-02 18:32:08 +08001923 if (gpio_is_valid(data->pdata->reset_gpio))
1924 gpio_free(data->pdata->reset_gpio);
1925
1926 if (gpio_is_valid(data->pdata->irq_gpio))
1927 gpio_free(data->pdata->irq_gpio);
1928
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001929 kfree(data->object_table);
1930 kfree(data);
1931
1932 return 0;
1933}
1934
Iiro Valkonen7686b102011-02-02 23:21:58 -08001935static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001936 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001937 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001938 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001939 { }
1940};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001941MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001942
Iiro Valkonen7686b102011-02-02 23:21:58 -08001943static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001944 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001945 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001946 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001947#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001948 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001949#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001950 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001951 .probe = mxt_probe,
1952 .remove = __devexit_p(mxt_remove),
1953 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001954};
1955
Iiro Valkonen7686b102011-02-02 23:21:58 -08001956static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001957{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001958 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001959}
1960
Iiro Valkonen7686b102011-02-02 23:21:58 -08001961static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001962{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001963 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001964}
1965
Iiro Valkonen7686b102011-02-02 23:21:58 -08001966module_init(mxt_init);
1967module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001968
1969/* Module information */
1970MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001971MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001972MODULE_LICENSE("GPL");