blob: 2453b6bc6084f4b908f3a51dd4e6af9bf2137cab [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
83#define MXT_PROCG_NOISESUPPRESSION_T48 48
84#define MXT_SPT_COMMSCONFIG_T18 18
85#define MXT_SPT_GPIOPWM_T19 19
86#define MXT_SPT_SELFTEST_T25 25
87#define MXT_SPT_CTECONFIG_T28 28
88#define MXT_SPT_USERDATA_T38 38
89#define MXT_SPT_DIGITIZER_T43 43
90#define MXT_SPT_MESSAGECOUNT_T44 44
91#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070092
Iiro Valkonene8645592011-11-18 12:56:19 -080093/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080094#define MXT_COMMAND_RESET 0
95#define MXT_COMMAND_BACKUPNV 1
96#define MXT_COMMAND_CALIBRATE 2
97#define MXT_COMMAND_REPORTALL 3
98#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070099
Iiro Valkonene8645592011-11-18 12:56:19 -0800100/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800101#define MXT_POWER_IDLEACQINT 0
102#define MXT_POWER_ACTVACQINT 1
103#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700104
Iiro Valkonene8645592011-11-18 12:56:19 -0800105/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800106#define MXT_ACQUIRE_CHRGTIME 0
107#define MXT_ACQUIRE_TCHDRIFT 2
108#define MXT_ACQUIRE_DRIFTST 3
109#define MXT_ACQUIRE_TCHAUTOCAL 4
110#define MXT_ACQUIRE_SYNC 5
111#define MXT_ACQUIRE_ATCHCALST 6
112#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700113
Iiro Valkonene8645592011-11-18 12:56:19 -0800114/* MXT_TOUCH_MULT_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800115#define MXT_TOUCH_CTRL 0
116#define MXT_TOUCH_XORIGIN 1
117#define MXT_TOUCH_YORIGIN 2
118#define MXT_TOUCH_XSIZE 3
119#define MXT_TOUCH_YSIZE 4
120#define MXT_TOUCH_BLEN 6
121#define MXT_TOUCH_TCHTHR 7
122#define MXT_TOUCH_TCHDI 8
123#define MXT_TOUCH_ORIENT 9
124#define MXT_TOUCH_MOVHYSTI 11
125#define MXT_TOUCH_MOVHYSTN 12
126#define MXT_TOUCH_NUMTOUCH 14
127#define MXT_TOUCH_MRGHYST 15
128#define MXT_TOUCH_MRGTHR 16
129#define MXT_TOUCH_AMPHYST 17
130#define MXT_TOUCH_XRANGE_LSB 18
131#define MXT_TOUCH_XRANGE_MSB 19
132#define MXT_TOUCH_YRANGE_LSB 20
133#define MXT_TOUCH_YRANGE_MSB 21
134#define MXT_TOUCH_XLOCLIP 22
135#define MXT_TOUCH_XHICLIP 23
136#define MXT_TOUCH_YLOCLIP 24
137#define MXT_TOUCH_YHICLIP 25
138#define MXT_TOUCH_XEDGECTRL 26
139#define MXT_TOUCH_XEDGEDIST 27
140#define MXT_TOUCH_YEDGECTRL 28
141#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700142#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700143
Iiro Valkonene8645592011-11-18 12:56:19 -0800144/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800145#define MXT_GRIPFACE_CTRL 0
146#define MXT_GRIPFACE_XLOGRIP 1
147#define MXT_GRIPFACE_XHIGRIP 2
148#define MXT_GRIPFACE_YLOGRIP 3
149#define MXT_GRIPFACE_YHIGRIP 4
150#define MXT_GRIPFACE_MAXTCHS 5
151#define MXT_GRIPFACE_SZTHR1 7
152#define MXT_GRIPFACE_SZTHR2 8
153#define MXT_GRIPFACE_SHPTHR1 9
154#define MXT_GRIPFACE_SHPTHR2 10
155#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700156
Iiro Valkonen7686b102011-02-02 23:21:58 -0800157/* MXT_PROCI_NOISE field */
158#define MXT_NOISE_CTRL 0
159#define MXT_NOISE_OUTFLEN 1
160#define MXT_NOISE_GCAFUL_LSB 3
161#define MXT_NOISE_GCAFUL_MSB 4
162#define MXT_NOISE_GCAFLL_LSB 5
163#define MXT_NOISE_GCAFLL_MSB 6
164#define MXT_NOISE_ACTVGCAFVALID 7
165#define MXT_NOISE_NOISETHR 8
166#define MXT_NOISE_FREQHOPSCALE 10
167#define MXT_NOISE_FREQ0 11
168#define MXT_NOISE_FREQ1 12
169#define MXT_NOISE_FREQ2 13
170#define MXT_NOISE_FREQ3 14
171#define MXT_NOISE_FREQ4 15
172#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700173
Iiro Valkonene8645592011-11-18 12:56:19 -0800174/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800175#define MXT_COMMS_CTRL 0
176#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700177
Iiro Valkonene8645592011-11-18 12:56:19 -0800178/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800179#define MXT_CTE_CTRL 0
180#define MXT_CTE_CMD 1
181#define MXT_CTE_MODE 2
182#define MXT_CTE_IDLEGCAFDEPTH 3
183#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700184#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700185
Iiro Valkonen7686b102011-02-02 23:21:58 -0800186#define MXT_VOLTAGE_DEFAULT 2700000
187#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700188
Amy Maloche21115eb2011-11-02 09:04:37 -0700189/* Analog voltage @2.7 V */
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530190#define MXT_VTG_MIN_UV 2700000
191#define MXT_VTG_MAX_UV 3300000
192#define MXT_ACTIVE_LOAD_UA 15000
Jing Linbace50b2011-10-18 22:55:47 -0700193#define MXT_LPM_LOAD_UA 10
Amy Maloche21115eb2011-11-02 09:04:37 -0700194/* Digital voltage @1.8 V */
195#define MXT_VTG_DIG_MIN_UV 1800000
196#define MXT_VTG_DIG_MAX_UV 1800000
197#define MXT_ACTIVE_LOAD_DIG_UA 10000
198#define MXT_LPM_LOAD_DIG_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530199
200#define MXT_I2C_VTG_MIN_UV 1800000
201#define MXT_I2C_VTG_MAX_UV 1800000
202#define MXT_I2C_LOAD_UA 10000
Jing Linbace50b2011-10-18 22:55:47 -0700203#define MXT_I2C_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530204
Iiro Valkonene8645592011-11-18 12:56:19 -0800205/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800206#define MXT_BOOT_VALUE 0xa5
207#define MXT_BACKUP_VALUE 0x55
208#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700209#define MXT224_RESET_TIME 65 /* msec */
Amy Maloche380cc0b2011-11-03 12:55:04 -0700210#define MXT224E_RESET_TIME 22 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700211#define MXT1386_RESET_TIME 250 /* msec */
212#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700213#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700214
Iiro Valkonen7686b102011-02-02 23:21:58 -0800215#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700216
Jing Lin36aee812011-10-17 17:17:28 -0700217#define MXT_WAKE_TIME 25
218
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800220#define MXT_UNLOCK_CMD_MSB 0xaa
221#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700222
223/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800224#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
225#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
226#define MXT_FRAME_CRC_CHECK 0x02
227#define MXT_FRAME_CRC_FAIL 0x03
228#define MXT_FRAME_CRC_PASS 0x04
229#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
230#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231
232/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800233#define MXT_SUPPRESS (1 << 1)
234#define MXT_AMP (1 << 2)
235#define MXT_VECTOR (1 << 3)
236#define MXT_MOVE (1 << 4)
237#define MXT_RELEASE (1 << 5)
238#define MXT_PRESS (1 << 6)
239#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700240
Joonyoung Shim910d8052011-04-12 23:14:38 -0700241/* Touch orient bits */
242#define MXT_XY_SWITCH (1 << 0)
243#define MXT_X_INVERT (1 << 1)
244#define MXT_Y_INVERT (1 << 2)
245
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700246/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800247#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700248
Iiro Valkonen7686b102011-02-02 23:21:58 -0800249#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700250
Jing Lin36aee812011-10-17 17:17:28 -0700251#define T7_DATA_SIZE 3
252#define MXT_MAX_RW_TRIES 3
253#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530254
Iiro Valkonen7686b102011-02-02 23:21:58 -0800255struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700256 u8 family_id;
257 u8 variant_id;
258 u8 version;
259 u8 build;
260 u8 matrix_xsize;
261 u8 matrix_ysize;
262 u8 object_num;
263};
264
Iiro Valkonen7686b102011-02-02 23:21:58 -0800265struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700266 u8 type;
267 u16 start_address;
268 u8 size;
269 u8 instances;
270 u8 num_report_ids;
271
272 /* to map object and message */
273 u8 max_reportid;
274};
275
Iiro Valkonen7686b102011-02-02 23:21:58 -0800276struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700277 u8 reportid;
278 u8 message[7];
279 u8 checksum;
280};
281
Iiro Valkonen7686b102011-02-02 23:21:58 -0800282struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700283 int status;
284 int x;
285 int y;
286 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700287 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700288};
289
290/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800291struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700292 struct i2c_client *client;
293 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800294 const struct mxt_platform_data *pdata;
295 struct mxt_object *object_table;
296 struct mxt_info info;
297 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700298 unsigned int irq;
Amy Maloche21115eb2011-11-02 09:04:37 -0700299 struct regulator *vcc_ana;
300 struct regulator *vcc_dig;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530301 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530302#if defined(CONFIG_HAS_EARLYSUSPEND)
303 struct early_suspend early_suspend;
304#endif
Jing Lin36aee812011-10-17 17:17:28 -0700305
Amy Maloche52262212011-09-15 16:46:57 -0700306 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700307 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700308 u8 t9_ctrl;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800309 u32 keyarray_old;
310 u32 keyarray_new;
311 u8 t9_max_reportid;
312 u8 t9_min_reportid;
313 u8 t15_max_reportid;
314 u8 t15_min_reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700315};
316
Iiro Valkonen7686b102011-02-02 23:21:58 -0800317static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700318{
319 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800320 case MXT_GEN_MESSAGE_T5:
321 case MXT_GEN_COMMAND_T6:
322 case MXT_GEN_POWER_T7:
323 case MXT_GEN_ACQUIRE_T8:
324 case MXT_GEN_DATASOURCE_T53:
325 case MXT_TOUCH_MULTI_T9:
326 case MXT_TOUCH_KEYARRAY_T15:
327 case MXT_TOUCH_PROXIMITY_T23:
328 case MXT_TOUCH_PROXKEY_T52:
329 case MXT_PROCI_GRIPFACE_T20:
330 case MXT_PROCG_NOISE_T22:
331 case MXT_PROCI_ONETOUCH_T24:
332 case MXT_PROCI_TWOTOUCH_T27:
333 case MXT_PROCI_GRIP_T40:
334 case MXT_PROCI_PALM_T41:
335 case MXT_PROCI_TOUCHSUPPRESSION_T42:
336 case MXT_PROCI_STYLUS_T47:
337 case MXT_PROCG_NOISESUPPRESSION_T48:
338 case MXT_SPT_COMMSCONFIG_T18:
339 case MXT_SPT_GPIOPWM_T19:
340 case MXT_SPT_SELFTEST_T25:
341 case MXT_SPT_CTECONFIG_T28:
342 case MXT_SPT_USERDATA_T38:
343 case MXT_SPT_DIGITIZER_T43:
344 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700345 return true;
346 default:
347 return false;
348 }
349}
350
Iiro Valkonen7686b102011-02-02 23:21:58 -0800351static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700352{
353 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800354 case MXT_GEN_COMMAND_T6:
355 case MXT_GEN_POWER_T7:
356 case MXT_GEN_ACQUIRE_T8:
357 case MXT_TOUCH_MULTI_T9:
358 case MXT_TOUCH_KEYARRAY_T15:
359 case MXT_TOUCH_PROXIMITY_T23:
360 case MXT_TOUCH_PROXKEY_T52:
361 case MXT_PROCI_GRIPFACE_T20:
362 case MXT_PROCG_NOISE_T22:
363 case MXT_PROCI_ONETOUCH_T24:
364 case MXT_PROCI_TWOTOUCH_T27:
365 case MXT_PROCI_GRIP_T40:
366 case MXT_PROCI_PALM_T41:
367 case MXT_PROCI_TOUCHSUPPRESSION_T42:
368 case MXT_PROCI_STYLUS_T47:
369 case MXT_PROCG_NOISESUPPRESSION_T48:
370 case MXT_SPT_COMMSCONFIG_T18:
371 case MXT_SPT_GPIOPWM_T19:
372 case MXT_SPT_SELFTEST_T25:
373 case MXT_SPT_CTECONFIG_T28:
374 case MXT_SPT_USERDATA_T38:
375 case MXT_SPT_DIGITIZER_T43:
376 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700377 return true;
378 default:
379 return false;
380 }
381}
382
Iiro Valkonen7686b102011-02-02 23:21:58 -0800383static void mxt_dump_message(struct device *dev,
384 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700385{
386 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
387 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
388 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
389 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
390 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
391 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
392 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
393 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
394 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
395}
396
Iiro Valkonen7686b102011-02-02 23:21:58 -0800397static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700398 unsigned int state)
399{
400 u8 val;
401
402recheck:
403 if (i2c_master_recv(client, &val, 1) != 1) {
404 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
405 return -EIO;
406 }
407
408 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800409 case MXT_WAITING_BOOTLOAD_CMD:
410 case MXT_WAITING_FRAME_DATA:
411 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700412 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800413 case MXT_FRAME_CRC_PASS:
414 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700415 goto recheck;
416 break;
417 default:
418 return -EINVAL;
419 }
420
421 if (val != state) {
422 dev_err(&client->dev, "Unvalid bootloader mode state\n");
423 return -EINVAL;
424 }
425
426 return 0;
427}
428
Iiro Valkonen7686b102011-02-02 23:21:58 -0800429static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430{
431 u8 buf[2];
432
Iiro Valkonen7686b102011-02-02 23:21:58 -0800433 buf[0] = MXT_UNLOCK_CMD_LSB;
434 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700435
436 if (i2c_master_send(client, buf, 2) != 2) {
437 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
438 return -EIO;
439 }
440
441 return 0;
442}
443
Iiro Valkonen7686b102011-02-02 23:21:58 -0800444static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700445 const u8 *data, unsigned int frame_size)
446{
447 if (i2c_master_send(client, data, frame_size) != frame_size) {
448 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
449 return -EIO;
450 }
451
452 return 0;
453}
454
Iiro Valkonen7686b102011-02-02 23:21:58 -0800455static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700456 u16 reg, u16 len, void *val)
457{
458 struct i2c_msg xfer[2];
459 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700460 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700461
462 buf[0] = reg & 0xff;
463 buf[1] = (reg >> 8) & 0xff;
464
465 /* Write register */
466 xfer[0].addr = client->addr;
467 xfer[0].flags = 0;
468 xfer[0].len = 2;
469 xfer[0].buf = buf;
470
471 /* Read data */
472 xfer[1].addr = client->addr;
473 xfer[1].flags = I2C_M_RD;
474 xfer[1].len = len;
475 xfer[1].buf = val;
476
Jing Lin36aee812011-10-17 17:17:28 -0700477 do {
478 if (i2c_transfer(client->adapter, xfer, 2) == 2)
479 return 0;
480 msleep(MXT_WAKE_TIME);
481 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482
Jing Lin36aee812011-10-17 17:17:28 -0700483 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
484 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485}
486
Iiro Valkonen7686b102011-02-02 23:21:58 -0800487static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800489 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700490}
491
Jing Lin36aee812011-10-17 17:17:28 -0700492static int __mxt_write_reg(struct i2c_client *client,
493 u16 addr, u16 length, u8 *value)
494{
495 u8 buf[MXT_BLOCK_SIZE + 2];
496 int i, tries = 0;
497
498 if (length > MXT_BLOCK_SIZE)
499 return -EINVAL;
500
501 buf[0] = addr & 0xff;
502 buf[1] = (addr >> 8) & 0xff;
503 for (i = 0; i < length; i++)
504 buf[i + 2] = *value++;
505
506 do {
507 if (i2c_master_send(client, buf, length + 2) == (length + 2))
508 return 0;
509 msleep(MXT_WAKE_TIME);
510 } while (++tries < MXT_MAX_RW_TRIES);
511
512 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
513 return -EIO;
514}
515
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517{
Jing Lin36aee812011-10-17 17:17:28 -0700518 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700519}
520
Iiro Valkonen7686b102011-02-02 23:21:58 -0800521static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522 u16 reg, u8 *object_buf)
523{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525 object_buf);
526}
527
Iiro Valkonen7686b102011-02-02 23:21:58 -0800528static struct mxt_object *
529mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800531 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532 int i;
533
534 for (i = 0; i < data->info.object_num; i++) {
535 object = data->object_table + i;
536 if (object->type == type)
537 return object;
538 }
539
540 dev_err(&data->client->dev, "Invalid object type\n");
541 return NULL;
542}
543
Iiro Valkonen7686b102011-02-02 23:21:58 -0800544static int mxt_read_message(struct mxt_data *data,
545 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700546{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800547 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700548 u16 reg;
549
Iiro Valkonene8645592011-11-18 12:56:19 -0800550 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700551 if (!object)
552 return -EINVAL;
553
554 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800555 return __mxt_read_reg(data->client, reg,
556 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557}
558
Iiro Valkonen7686b102011-02-02 23:21:58 -0800559static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700560 u8 type, u8 offset, u8 *val)
561{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800562 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700563 u16 reg;
564
Iiro Valkonen7686b102011-02-02 23:21:58 -0800565 object = mxt_get_object(data, type);
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 + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700571}
572
Iiro Valkonen7686b102011-02-02 23:21:58 -0800573static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700574 u8 type, u8 offset, u8 val)
575{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800576 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 u16 reg;
578
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580 if (!object)
581 return -EINVAL;
582
583 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800584 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700585}
586
Iiro Valkonen7686b102011-02-02 23:21:58 -0800587static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700588{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590 struct input_dev *input_dev = data->input_dev;
591 int status = finger[single_id].status;
592 int finger_num = 0;
593 int id;
594
Iiro Valkonen7686b102011-02-02 23:21:58 -0800595 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700596 if (!finger[id].status)
597 continue;
598
Amy Maloche2b59bab2011-10-13 16:08:16 -0700599 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
600 finger[id].status != MXT_RELEASE ?
601 finger[id].area : 0);
602 input_report_abs(input_dev, ABS_MT_POSITION_X,
603 finger[id].x);
604 input_report_abs(input_dev, ABS_MT_POSITION_Y,
605 finger[id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700606 input_report_abs(input_dev, ABS_MT_PRESSURE,
Mohan Pallaka5e7343f2012-01-02 18:30:16 +0800607 finger[id].status != MXT_RELEASE ?
608 finger[id].pressure : 0);
Amy Maloche2b59bab2011-10-13 16:08:16 -0700609 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700610
Amy Maloche2b59bab2011-10-13 16:08:16 -0700611 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700612 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700613 else
614 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700615 }
616
617 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
618
Iiro Valkonen7686b102011-02-02 23:21:58 -0800619 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700620 input_report_abs(input_dev, ABS_X, finger[single_id].x);
621 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700622 input_report_abs(input_dev,
623 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624 }
625
626 input_sync(input_dev);
627}
628
Iiro Valkonen7686b102011-02-02 23:21:58 -0800629static void mxt_input_touchevent(struct mxt_data *data,
630 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700633 struct device *dev = &data->client->dev;
634 u8 status = message->message[0];
635 int x;
636 int y;
637 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700638 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639
640 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800641 if (!(status & MXT_DETECT)) {
642 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643 dev_dbg(dev, "[%d] released\n", id);
644
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645 finger[id].status = MXT_RELEASE;
646 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700647 }
648 return;
649 }
650
651 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800652 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653 return;
654
Joonyoung Shim910d8052011-04-12 23:14:38 -0700655 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
656 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700657 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700658 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700659 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700660 y = y >> 2;
661
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700663 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664
665 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800666 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667 x, y, area);
668
Iiro Valkonen7686b102011-02-02 23:21:58 -0800669 finger[id].status = status & MXT_MOVE ?
670 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700671 finger[id].x = x;
672 finger[id].y = y;
673 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700674 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675
Iiro Valkonen7686b102011-02-02 23:21:58 -0800676 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677}
678
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800679static void mxt_handle_key_array(struct mxt_data *data,
680 struct mxt_message *message)
681{
682 u32 keys_changed;
683 int i;
684
685 if (!data->pdata->key_codes) {
686 dev_err(&data->client->dev, "keyarray is not supported\n");
687 return;
688 }
689
690 data->keyarray_new = message->message[1] |
691 (message->message[2] << 8) |
692 (message->message[3] << 16) |
693 (message->message[4] << 24);
694
695 keys_changed = data->keyarray_old ^ data->keyarray_new;
696
697 if (!keys_changed) {
698 dev_dbg(&data->client->dev, "no keys changed\n");
699 return;
700 }
701
702 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
703 if (!(keys_changed & (1 << i)))
704 continue;
705
706 input_report_key(data->input_dev, data->pdata->key_codes[i],
707 (data->keyarray_new & (1 << i)));
708 input_sync(data->input_dev);
709 }
710
711 data->keyarray_old = data->keyarray_new;
712}
713
Iiro Valkonen7686b102011-02-02 23:21:58 -0800714static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716 struct mxt_data *data = dev_id;
717 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718 struct device *dev = &data->client->dev;
719 int id;
720 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721
722 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800723 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700724 dev_err(dev, "Failed to read message\n");
725 goto end;
726 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727 reportid = message.reportid;
728
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800729 if (!reportid) {
730 dev_dbg(dev, "Report id 0 is reserved\n");
731 continue;
732 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700733
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800734 /* check whether report id is part of T9 or T15 */
735 id = reportid - data->t9_min_reportid;
736
737 if (reportid >= data->t9_min_reportid &&
738 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800739 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800740 else if (reportid >= data->t15_min_reportid &&
741 reportid <= data->t15_max_reportid)
742 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800744 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700745 } while (reportid != 0xff);
746
747end:
748 return IRQ_HANDLED;
749}
750
Iiro Valkonen7686b102011-02-02 23:21:58 -0800751static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800753 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800754 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700755 struct device *dev = &data->client->dev;
756 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800757 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758
Iiro Valkonen71749f52011-02-15 13:36:52 -0800759 if (!pdata->config) {
760 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
761 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700762 }
763
764 for (i = 0; i < data->info.object_num; i++) {
765 object = data->object_table + i;
766
Iiro Valkonen7686b102011-02-02 23:21:58 -0800767 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700768 continue;
769
Iiro Valkonen71749f52011-02-15 13:36:52 -0800770 for (j = 0; j < object->size + 1; j++) {
771 config_offset = index + j;
772 if (config_offset > pdata->config_length) {
773 dev_err(dev, "Not enough config data!\n");
774 return -EINVAL;
775 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800776 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800777 pdata->config[config_offset]);
778 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779 index += object->size + 1;
780 }
781
782 return 0;
783}
784
Iiro Valkonen7686b102011-02-02 23:21:58 -0800785static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786{
787 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800788 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789 int count = 10;
790 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791
792 /* Read dummy message to make high CHG pin */
793 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800794 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700795 if (error)
796 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800797 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798
799 if (!count) {
800 dev_err(dev, "CHG pin isn't cleared\n");
801 return -EBUSY;
802 }
803
804 return 0;
805}
806
Iiro Valkonen7686b102011-02-02 23:21:58 -0800807static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700808{
809 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800810 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811 int error;
812 u8 val;
813
Iiro Valkonen7686b102011-02-02 23:21:58 -0800814 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700815 if (error)
816 return error;
817 info->family_id = val;
818
Iiro Valkonen7686b102011-02-02 23:21:58 -0800819 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700820 if (error)
821 return error;
822 info->variant_id = val;
823
Iiro Valkonen7686b102011-02-02 23:21:58 -0800824 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700825 if (error)
826 return error;
827 info->version = val;
828
Iiro Valkonen7686b102011-02-02 23:21:58 -0800829 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700830 if (error)
831 return error;
832 info->build = val;
833
Iiro Valkonen7686b102011-02-02 23:21:58 -0800834 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700835 if (error)
836 return error;
837 info->object_num = val;
838
839 return 0;
840}
841
Iiro Valkonen7686b102011-02-02 23:21:58 -0800842static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700843{
844 int error;
845 int i;
846 u16 reg;
847 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800848 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700849
850 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800851 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700852
Iiro Valkonen7686b102011-02-02 23:21:58 -0800853 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
854 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700855 if (error)
856 return error;
857
858 object->type = buf[0];
859 object->start_address = (buf[2] << 8) | buf[1];
860 object->size = buf[3];
861 object->instances = buf[4];
862 object->num_report_ids = buf[5];
863
864 if (object->num_report_ids) {
865 reportid += object->num_report_ids *
866 (object->instances + 1);
867 object->max_reportid = reportid;
868 }
869 }
870
871 return 0;
872}
873
Amy Maloche7e447432011-09-14 11:36:30 -0700874static void mxt_reset_delay(struct mxt_data *data)
875{
876 struct mxt_info *info = &data->info;
877
878 switch (info->family_id) {
879 case MXT224_ID:
880 msleep(MXT224_RESET_TIME);
881 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -0700882 case MXT224E_ID:
883 msleep(MXT224E_RESET_TIME);
884 break;
Amy Maloche7e447432011-09-14 11:36:30 -0700885 case MXT1386_ID:
886 msleep(MXT1386_RESET_TIME);
887 break;
888 default:
889 msleep(MXT_RESET_TIME);
890 }
891}
892
Iiro Valkonen7686b102011-02-02 23:21:58 -0800893static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700894{
895 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800896 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700897 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700898 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700899 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700900 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700901 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800902 struct mxt_object *t9_object;
903 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700904
Iiro Valkonen7686b102011-02-02 23:21:58 -0800905 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700906 if (error)
907 return error;
908
909 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800910 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700911 GFP_KERNEL);
912 if (!data->object_table) {
913 dev_err(&client->dev, "Failed to allocate memory\n");
914 return -ENOMEM;
915 }
916
917 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800918 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700919 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700920 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700921
922 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800923 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700924 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700925 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700926
Amy Maloche52262212011-09-15 16:46:57 -0700927 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -0800928 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -0700929 if (!t7_object) {
930 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -0700931 error = -EINVAL;
932 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -0700933 }
934
935 data->t7_start_addr = t7_object->start_address;
936 error = __mxt_read_reg(client, data->t7_start_addr,
937 T7_DATA_SIZE, data->t7_data);
938 if (error < 0) {
939 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -0700940 "Failed to save current power state\n");
941 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -0700942 }
Iiro Valkonene8645592011-11-18 12:56:19 -0800943 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -0700944 &data->t9_ctrl);
945 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -0700946 dev_err(&client->dev, "Failed to save current touch object\n");
947 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -0700948 }
949
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800950 /* Store T9, T15's min and max report ids */
951 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
952 if (!t9_object) {
953 dev_err(&client->dev, "Failed to get T9 object\n");
954 error = -EINVAL;
955 goto free_object_table;
956 }
957 data->t9_max_reportid = t9_object->max_reportid;
958 data->t9_min_reportid = t9_object->max_reportid -
959 t9_object->num_report_ids + 1;
960
961 if (data->pdata->key_codes) {
962 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
963 if (!t15_object)
964 dev_dbg(&client->dev, "T15 object is not available\n");
965 else {
966 data->t15_max_reportid = t15_object->max_reportid;
967 data->t15_min_reportid = t15_object->max_reportid -
968 t15_object->num_report_ids + 1;
969 }
970 }
971
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -0800973 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800974 MXT_COMMAND_BACKUPNV,
975 MXT_BACKUP_VALUE);
976 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700977 do {
Iiro Valkonene8645592011-11-18 12:56:19 -0800978 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700979 MXT_COMMAND_BACKUPNV,
980 &command_register);
981 if (error)
Jing Lin32c72532011-11-03 12:02:33 -0700982 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -0700983 usleep_range(1000, 2000);
984 } while ((command_register != 0) && (++timeout_counter <= 100));
985 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700986 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -0700987 error = -EIO;
988 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700989 }
990
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991
992 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -0800993 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700995
Amy Maloche7e447432011-09-14 11:36:30 -0700996 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997
998 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800999 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001001 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001002 info->matrix_xsize = val;
1003
Iiro Valkonen7686b102011-02-02 23:21:58 -08001004 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001006 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 info->matrix_ysize = val;
1008
1009 dev_info(&client->dev,
1010 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
1011 info->family_id, info->variant_id, info->version,
1012 info->build);
1013
1014 dev_info(&client->dev,
1015 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
1016 info->matrix_xsize, info->matrix_ysize,
1017 info->object_num);
1018
1019 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001020
1021free_object_table:
1022 kfree(data->object_table);
1023 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001024}
1025
Iiro Valkonen7686b102011-02-02 23:21:58 -08001026static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027 struct device_attribute *attr, char *buf)
1028{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001029 struct mxt_data *data = dev_get_drvdata(dev);
1030 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001031 int count = 0;
1032 int i, j;
1033 int error;
1034 u8 val;
1035
1036 for (i = 0; i < data->info.object_num; i++) {
1037 object = data->object_table + i;
1038
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001039 count += snprintf(buf + count, PAGE_SIZE - count,
1040 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001041 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001042 if (count >= PAGE_SIZE)
1043 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001044
Iiro Valkonen7686b102011-02-02 23:21:58 -08001045 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001046 count += snprintf(buf + count, PAGE_SIZE - count,
1047 "\n");
1048 if (count >= PAGE_SIZE)
1049 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050 continue;
1051 }
1052
1053 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001054 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001055 object->type, j, &val);
1056 if (error)
1057 return error;
1058
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001059 count += snprintf(buf + count, PAGE_SIZE - count,
1060 "\t[%2d]: %02x (%d)\n", j, val, val);
1061 if (count >= PAGE_SIZE)
1062 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063 }
1064
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001065 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1066 if (count >= PAGE_SIZE)
1067 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001068 }
1069
1070 return count;
1071}
1072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001075 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001076 struct i2c_client *client = data->client;
1077 const struct firmware *fw = NULL;
1078 unsigned int frame_size;
1079 unsigned int pos = 0;
1080 int ret;
1081
1082 ret = request_firmware(&fw, fn, dev);
1083 if (ret) {
1084 dev_err(dev, "Unable to open firmware %s\n", fn);
1085 return ret;
1086 }
1087
1088 /* Change to the bootloader mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001089 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001090 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001091
1092 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093
1094 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001095 if (client->addr == MXT_APP_LOW)
1096 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001098 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001099
Iiro Valkonen7686b102011-02-02 23:21:58 -08001100 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001101 if (ret)
1102 goto out;
1103
1104 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001105 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001106
1107 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001108 ret = mxt_check_bootloader(client,
1109 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001110 if (ret)
1111 goto out;
1112
1113 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1114
1115 /* We should add 2 at frame size as the the firmware data is not
1116 * included the CRC bytes.
1117 */
1118 frame_size += 2;
1119
1120 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001121 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001122
Iiro Valkonen7686b102011-02-02 23:21:58 -08001123 ret = mxt_check_bootloader(client,
1124 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001125 if (ret)
1126 goto out;
1127
1128 pos += frame_size;
1129
1130 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1131 }
1132
1133out:
1134 release_firmware(fw);
1135
1136 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001137 if (client->addr == MXT_BOOT_LOW)
1138 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001139 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001140 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001141
1142 return ret;
1143}
1144
Iiro Valkonen7686b102011-02-02 23:21:58 -08001145static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146 struct device_attribute *attr,
1147 const char *buf, size_t count)
1148{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001149 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001150 int error;
1151
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001152 disable_irq(data->irq);
1153
Iiro Valkonen7686b102011-02-02 23:21:58 -08001154 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001155 if (error) {
1156 dev_err(dev, "The firmware update failed(%d)\n", error);
1157 count = error;
1158 } else {
1159 dev_dbg(dev, "The firmware update succeeded\n");
1160
1161 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001162 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001163
1164 kfree(data->object_table);
1165 data->object_table = NULL;
1166
Iiro Valkonen7686b102011-02-02 23:21:58 -08001167 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168 }
1169
1170 enable_irq(data->irq);
1171
Iiro Valkonen08960a02011-04-12 23:16:40 -07001172 error = mxt_make_highchg(data);
1173 if (error)
1174 return error;
1175
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176 return count;
1177}
1178
Iiro Valkonen7686b102011-02-02 23:21:58 -08001179static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1180static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001181
Iiro Valkonen7686b102011-02-02 23:21:58 -08001182static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001183 &dev_attr_object.attr,
1184 &dev_attr_update_fw.attr,
1185 NULL
1186};
1187
Iiro Valkonen7686b102011-02-02 23:21:58 -08001188static const struct attribute_group mxt_attr_group = {
1189 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001190};
1191
Amy Maloche52262212011-09-15 16:46:57 -07001192static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001193{
Jing Lin36aee812011-10-17 17:17:28 -07001194 int error;
1195
Amy Maloche52262212011-09-15 16:46:57 -07001196 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001197 error = __mxt_write_reg(data->client, data->t7_start_addr,
1198 T7_DATA_SIZE, data->t7_data);
1199 if (error < 0) {
1200 dev_err(&data->client->dev,
1201 "failed to restore old power state\n");
1202 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001203 }
Jing Lin36aee812011-10-17 17:17:28 -07001204
Amy Maloche52262212011-09-15 16:46:57 -07001205 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001206 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001207 if (error < 0) {
1208 dev_err(&data->client->dev, "failed to restore touch\n");
1209 return error;
1210 }
1211
1212 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001213}
1214
Amy Maloche52262212011-09-15 16:46:57 -07001215static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001216{
Jing Lin36aee812011-10-17 17:17:28 -07001217 int error;
1218 u8 t7_data[T7_DATA_SIZE] = {0};
1219
1220 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001221 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001222 if (error < 0) {
1223 dev_err(&data->client->dev, "failed to disable touch\n");
1224 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001225 }
1226
Jing Lin36aee812011-10-17 17:17:28 -07001227 error = __mxt_write_reg(data->client, data->t7_start_addr,
1228 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001229 if (error < 0) {
1230 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001231 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001232 return error;
1233 }
1234
1235 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001236}
1237
Iiro Valkonen7686b102011-02-02 23:21:58 -08001238static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001239{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001240 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001241 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001242
Amy Maloche52262212011-09-15 16:46:57 -07001243 error = mxt_start(data);
1244 if (error < 0) {
1245 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1246 return error;
1247 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001248
1249 return 0;
1250}
1251
Iiro Valkonen7686b102011-02-02 23:21:58 -08001252static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001253{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001254 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001255 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001256
Amy Maloche52262212011-09-15 16:46:57 -07001257 error = mxt_stop(data);
1258 if (error < 0)
1259 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1260
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001261}
1262
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301263static int mxt_power_on(struct mxt_data *data, bool on)
1264{
1265 int rc;
1266
1267 if (on == false)
1268 goto power_off;
1269
Amy Maloche21115eb2011-11-02 09:04:37 -07001270 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301271 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001272 dev_err(&data->client->dev,
1273 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301274 return rc;
1275 }
1276
Amy Maloche21115eb2011-11-02 09:04:37 -07001277 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301278 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001279 dev_err(&data->client->dev,
1280 "Regulator vcc_ana enable failed rc=%d\n", rc);
1281 goto error_reg_en_vcc_ana;
1282 }
1283
1284 if (data->pdata->digital_pwr_regulator) {
1285 rc = regulator_set_optimum_mode(data->vcc_dig,
1286 MXT_ACTIVE_LOAD_DIG_UA);
1287 if (rc < 0) {
1288 dev_err(&data->client->dev,
1289 "Regulator vcc_dig set_opt failed rc=%d\n",
1290 rc);
1291 goto error_reg_opt_vcc_dig;
1292 }
1293
1294 rc = regulator_enable(data->vcc_dig);
1295 if (rc) {
1296 dev_err(&data->client->dev,
1297 "Regulator vcc_dig enable failed rc=%d\n", rc);
1298 goto error_reg_en_vcc_dig;
1299 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301300 }
1301
1302 if (data->pdata->i2c_pull_up) {
1303 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1304 if (rc < 0) {
1305 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001306 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301307 goto error_reg_opt_i2c;
1308 }
1309
1310 rc = regulator_enable(data->vcc_i2c);
1311 if (rc) {
1312 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001313 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301314 goto error_reg_en_vcc_i2c;
1315 }
1316 }
1317
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001318 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301319
1320 return 0;
1321
1322error_reg_en_vcc_i2c:
1323 if (data->pdata->i2c_pull_up)
1324 regulator_set_optimum_mode(data->vcc_i2c, 0);
1325error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001326 if (data->pdata->digital_pwr_regulator)
1327 regulator_disable(data->vcc_dig);
1328error_reg_en_vcc_dig:
1329 if (data->pdata->digital_pwr_regulator)
1330 regulator_set_optimum_mode(data->vcc_dig, 0);
1331error_reg_opt_vcc_dig:
1332 regulator_disable(data->vcc_ana);
1333error_reg_en_vcc_ana:
1334 regulator_set_optimum_mode(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301335 return rc;
1336
1337power_off:
Amy Maloche21115eb2011-11-02 09:04:37 -07001338 regulator_set_optimum_mode(data->vcc_ana, 0);
1339 regulator_disable(data->vcc_ana);
1340 if (data->pdata->digital_pwr_regulator) {
1341 regulator_set_optimum_mode(data->vcc_dig, 0);
1342 regulator_disable(data->vcc_dig);
1343 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301344 if (data->pdata->i2c_pull_up) {
1345 regulator_set_optimum_mode(data->vcc_i2c, 0);
1346 regulator_disable(data->vcc_i2c);
1347 }
1348 msleep(50);
1349 return 0;
1350}
1351
1352static int mxt_regulator_configure(struct mxt_data *data, bool on)
1353{
1354 int rc;
1355
1356 if (on == false)
1357 goto hw_shutdown;
1358
Amy Maloche21115eb2011-11-02 09:04:37 -07001359 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1360 if (IS_ERR(data->vcc_ana)) {
1361 rc = PTR_ERR(data->vcc_ana);
1362 dev_err(&data->client->dev,
1363 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301364 return rc;
1365 }
1366
Amy Maloche21115eb2011-11-02 09:04:37 -07001367 if (regulator_count_voltages(data->vcc_ana) > 0) {
1368 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301369 MXT_VTG_MAX_UV);
1370 if (rc) {
1371 dev_err(&data->client->dev,
1372 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001373 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301374 }
1375 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001376 if (data->pdata->digital_pwr_regulator) {
1377 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1378 if (IS_ERR(data->vcc_dig)) {
1379 rc = PTR_ERR(data->vcc_dig);
1380 dev_err(&data->client->dev,
1381 "Regulator get dig failed rc=%d\n", rc);
1382 goto error_get_vtg_vcc_dig;
1383 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301384
Amy Maloche21115eb2011-11-02 09:04:37 -07001385 if (regulator_count_voltages(data->vcc_dig) > 0) {
1386 rc = regulator_set_voltage(data->vcc_dig,
1387 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1388 if (rc) {
1389 dev_err(&data->client->dev,
1390 "regulator set_vtg failed rc=%d\n", rc);
1391 goto error_set_vtg_vcc_dig;
1392 }
1393 }
1394 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301395 if (data->pdata->i2c_pull_up) {
1396 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1397 if (IS_ERR(data->vcc_i2c)) {
1398 rc = PTR_ERR(data->vcc_i2c);
1399 dev_err(&data->client->dev,
1400 "Regulator get failed rc=%d\n", rc);
1401 goto error_get_vtg_i2c;
1402 }
1403 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1404 rc = regulator_set_voltage(data->vcc_i2c,
1405 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1406 if (rc) {
1407 dev_err(&data->client->dev,
1408 "regulator set_vtg failed rc=%d\n", rc);
1409 goto error_set_vtg_i2c;
1410 }
1411 }
1412 }
1413
1414 return 0;
1415
1416error_set_vtg_i2c:
1417 regulator_put(data->vcc_i2c);
1418error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001419 if (data->pdata->digital_pwr_regulator)
1420 if (regulator_count_voltages(data->vcc_dig) > 0)
1421 regulator_set_voltage(data->vcc_dig, 0,
1422 MXT_VTG_DIG_MAX_UV);
1423error_set_vtg_vcc_dig:
1424 if (data->pdata->digital_pwr_regulator)
1425 regulator_put(data->vcc_dig);
1426error_get_vtg_vcc_dig:
1427 if (regulator_count_voltages(data->vcc_ana) > 0)
1428 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1429error_set_vtg_vcc_ana:
1430 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301431 return rc;
1432
1433hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001434 if (regulator_count_voltages(data->vcc_ana) > 0)
1435 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1436 regulator_put(data->vcc_ana);
1437 if (data->pdata->digital_pwr_regulator) {
1438 if (regulator_count_voltages(data->vcc_dig) > 0)
1439 regulator_set_voltage(data->vcc_dig, 0,
1440 MXT_VTG_DIG_MAX_UV);
1441 regulator_put(data->vcc_dig);
1442 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301443 if (data->pdata->i2c_pull_up) {
1444 if (regulator_count_voltages(data->vcc_i2c) > 0)
1445 regulator_set_voltage(data->vcc_i2c, 0,
1446 MXT_I2C_VTG_MAX_UV);
1447 regulator_put(data->vcc_i2c);
1448 }
1449 return 0;
1450}
1451
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301452#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001453static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1454{
1455
1456 int rc;
1457
1458 if (on == false)
1459 goto regulator_hpm;
1460
Amy Maloche21115eb2011-11-02 09:04:37 -07001461 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001462 if (rc < 0) {
1463 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001464 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001465 goto fail_regulator_lpm;
1466 }
1467
Amy Maloche21115eb2011-11-02 09:04:37 -07001468 if (data->pdata->digital_pwr_regulator) {
1469 rc = regulator_set_optimum_mode(data->vcc_dig,
1470 MXT_LPM_LOAD_DIG_UA);
1471 if (rc < 0) {
1472 dev_err(&data->client->dev,
1473 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1474 goto fail_regulator_lpm;
1475 }
1476 }
1477
Jing Linbace50b2011-10-18 22:55:47 -07001478 if (data->pdata->i2c_pull_up) {
1479 rc = regulator_set_optimum_mode(data->vcc_i2c,
1480 MXT_I2C_LPM_LOAD_UA);
1481 if (rc < 0) {
1482 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001483 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001484 goto fail_regulator_lpm;
1485 }
1486 }
1487
1488 return 0;
1489
1490regulator_hpm:
1491
Amy Maloche21115eb2011-11-02 09:04:37 -07001492 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001493 if (rc < 0) {
1494 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001495 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001496 goto fail_regulator_hpm;
1497 }
1498
Amy Maloche21115eb2011-11-02 09:04:37 -07001499 if (data->pdata->digital_pwr_regulator) {
1500 rc = regulator_set_optimum_mode(data->vcc_dig,
1501 MXT_ACTIVE_LOAD_DIG_UA);
1502 if (rc < 0) {
1503 dev_err(&data->client->dev,
1504 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1505 goto fail_regulator_hpm;
1506 }
1507 }
1508
Jing Linbace50b2011-10-18 22:55:47 -07001509 if (data->pdata->i2c_pull_up) {
1510 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1511 if (rc < 0) {
1512 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001513 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001514 goto fail_regulator_hpm;
1515 }
1516 }
1517
1518 return 0;
1519
1520fail_regulator_lpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001521 regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
1522 if (data->pdata->digital_pwr_regulator)
1523 regulator_set_optimum_mode(data->vcc_dig,
1524 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001525 if (data->pdata->i2c_pull_up)
1526 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1527
1528 return rc;
1529
1530fail_regulator_hpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001531 regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
1532 if (data->pdata->digital_pwr_regulator)
1533 regulator_set_optimum_mode(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001534 if (data->pdata->i2c_pull_up)
1535 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1536
1537 return rc;
1538}
1539
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301540static int mxt_suspend(struct device *dev)
1541{
1542 struct i2c_client *client = to_i2c_client(dev);
1543 struct mxt_data *data = i2c_get_clientdata(client);
1544 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001545 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301546
1547 mutex_lock(&input_dev->mutex);
1548
Amy Maloche52262212011-09-15 16:46:57 -07001549 if (input_dev->users) {
1550 error = mxt_stop(data);
1551 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001552 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001553 mutex_unlock(&input_dev->mutex);
1554 return error;
1555 }
1556
1557 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301558
1559 mutex_unlock(&input_dev->mutex);
1560
Jing Linbace50b2011-10-18 22:55:47 -07001561 /* put regulators in low power mode */
1562 error = mxt_regulator_lpm(data, true);
1563 if (error < 0) {
1564 dev_err(dev, "failed to enter low power mode\n");
1565 return error;
1566 }
1567
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301568 return 0;
1569}
1570
1571static int mxt_resume(struct device *dev)
1572{
1573 struct i2c_client *client = to_i2c_client(dev);
1574 struct mxt_data *data = i2c_get_clientdata(client);
1575 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001576 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301577
Jing Linbace50b2011-10-18 22:55:47 -07001578 /* put regulators in high power mode */
1579 error = mxt_regulator_lpm(data, false);
1580 if (error < 0) {
1581 dev_err(dev, "failed to enter high power mode\n");
1582 return error;
1583 }
1584
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301585 mutex_lock(&input_dev->mutex);
1586
Amy Maloche52262212011-09-15 16:46:57 -07001587 if (input_dev->users) {
1588 error = mxt_start(data);
1589 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001590 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001591 mutex_unlock(&input_dev->mutex);
1592 return error;
1593 }
1594 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301595
1596 mutex_unlock(&input_dev->mutex);
1597
1598 return 0;
1599}
1600
1601#if defined(CONFIG_HAS_EARLYSUSPEND)
1602static void mxt_early_suspend(struct early_suspend *h)
1603{
1604 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1605
1606 mxt_suspend(&data->client->dev);
1607}
1608
1609static void mxt_late_resume(struct early_suspend *h)
1610{
1611 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1612
1613 mxt_resume(&data->client->dev);
1614}
1615#endif
1616
1617static const struct dev_pm_ops mxt_pm_ops = {
1618#ifndef CONFIG_HAS_EARLYSUSPEND
1619 .suspend = mxt_suspend,
1620 .resume = mxt_resume,
1621#endif
1622};
1623#endif
1624
Iiro Valkonen7686b102011-02-02 23:21:58 -08001625static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001626 const struct i2c_device_id *id)
1627{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001628 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001629 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001630 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001631 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001632
Iiro Valkonen919ed892011-02-15 13:36:52 -08001633 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001634 return -EINVAL;
1635
Iiro Valkonen7686b102011-02-02 23:21:58 -08001636 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001637 input_dev = input_allocate_device();
1638 if (!data || !input_dev) {
1639 dev_err(&client->dev, "Failed to allocate memory\n");
1640 error = -ENOMEM;
1641 goto err_free_mem;
1642 }
1643
Iiro Valkonen7686b102011-02-02 23:21:58 -08001644 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001645 input_dev->id.bustype = BUS_I2C;
1646 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001647 input_dev->open = mxt_input_open;
1648 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001649
Joonyoung Shim910d8052011-04-12 23:14:38 -07001650 data->client = client;
1651 data->input_dev = input_dev;
1652 data->pdata = pdata;
1653 data->irq = client->irq;
1654
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001655 __set_bit(EV_ABS, input_dev->evbit);
1656 __set_bit(EV_KEY, input_dev->evbit);
1657 __set_bit(BTN_TOUCH, input_dev->keybit);
1658
1659 /* For single touch */
1660 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001661 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001662 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001663 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001664 input_set_abs_params(input_dev, ABS_PRESSURE,
1665 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001666
1667 /* For multi touch */
1668 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001669 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001670 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001671 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001672 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001673 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001674 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1675 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001676
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001677 /* set key array supported keys */
1678 if (pdata->key_codes) {
1679 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
1680 if (pdata->key_codes[i])
1681 input_set_capability(input_dev, EV_KEY,
1682 pdata->key_codes[i]);
1683 }
1684 }
1685
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001686 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001687 i2c_set_clientdata(client, data);
1688
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301689 if (pdata->init_hw)
1690 error = pdata->init_hw(true);
1691 else
1692 error = mxt_regulator_configure(data, true);
1693 if (error) {
1694 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001695 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301696 }
1697
1698 if (pdata->power_on)
1699 error = pdata->power_on(true);
1700 else
1701 error = mxt_power_on(data, true);
1702 if (error) {
1703 dev_err(&client->dev, "Failed to power on hardware\n");
1704 goto err_regulator_on;
1705 }
1706
Amy Maloche08266db2011-11-04 11:07:16 -07001707 if (gpio_is_valid(pdata->irq_gpio)) {
1708 /* configure touchscreen irq gpio */
1709 error = gpio_request(pdata->irq_gpio,
1710 "mxt_irq_gpio");
1711 if (error) {
1712 pr_err("%s: unable to request gpio [%d]\n", __func__,
1713 pdata->irq_gpio);
1714 goto err_power_on;
1715 }
1716 error = gpio_direction_input(pdata->irq_gpio);
1717 if (error) {
1718 pr_err("%s: unable to set_direction for gpio [%d]\n",
1719 __func__, pdata->irq_gpio);
1720 goto err_irq_gpio_req;
1721 }
1722 }
1723
1724 if (gpio_is_valid(pdata->reset_gpio)) {
1725 /* configure touchscreen reset out gpio */
1726 error = gpio_request(pdata->reset_gpio,
1727 "mxt_reset_gpio");
1728 if (error) {
1729 pr_err("%s: unable to request reset gpio %d\n",
1730 __func__, pdata->reset_gpio);
1731 goto err_irq_gpio_req;
1732 }
1733
1734 error = gpio_direction_output(
1735 pdata->reset_gpio, 1);
1736 if (error) {
1737 pr_err("%s: unable to set direction for gpio %d\n",
1738 __func__, pdata->reset_gpio);
1739 goto err_reset_gpio_req;
1740 }
1741 }
1742
1743 mxt_reset_delay(data);
1744
Iiro Valkonen7686b102011-02-02 23:21:58 -08001745 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001746 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07001747 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001748
Iiro Valkonen7686b102011-02-02 23:21:58 -08001749 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001750 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001751 if (error) {
1752 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001753 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001754 }
1755
Iiro Valkonen08960a02011-04-12 23:16:40 -07001756 error = mxt_make_highchg(data);
1757 if (error)
1758 goto err_free_irq;
1759
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001760 error = input_register_device(input_dev);
1761 if (error)
1762 goto err_free_irq;
1763
Iiro Valkonen7686b102011-02-02 23:21:58 -08001764 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001765 if (error)
1766 goto err_unregister_device;
1767
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301768#if defined(CONFIG_HAS_EARLYSUSPEND)
1769 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1770 MXT_SUSPEND_LEVEL;
1771 data->early_suspend.suspend = mxt_early_suspend;
1772 data->early_suspend.resume = mxt_late_resume;
1773 register_early_suspend(&data->early_suspend);
1774#endif
1775
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001776 return 0;
1777
1778err_unregister_device:
1779 input_unregister_device(input_dev);
1780 input_dev = NULL;
1781err_free_irq:
1782 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001783err_free_object:
1784 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07001785err_reset_gpio_req:
1786 if (gpio_is_valid(pdata->reset_gpio))
1787 gpio_free(pdata->reset_gpio);
1788err_irq_gpio_req:
1789 if (gpio_is_valid(pdata->irq_gpio))
1790 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301791err_power_on:
1792 if (pdata->power_on)
1793 pdata->power_on(false);
1794 else
1795 mxt_power_on(data, false);
1796err_regulator_on:
1797 if (pdata->init_hw)
1798 pdata->init_hw(false);
1799 else
1800 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001801err_free_mem:
1802 input_free_device(input_dev);
1803 kfree(data);
1804 return error;
1805}
1806
Iiro Valkonen7686b102011-02-02 23:21:58 -08001807static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001808{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001809 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001810
Iiro Valkonen7686b102011-02-02 23:21:58 -08001811 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001812 free_irq(data->irq, data);
1813 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301814#if defined(CONFIG_HAS_EARLYSUSPEND)
1815 unregister_early_suspend(&data->early_suspend);
1816#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301817
1818 if (data->pdata->power_on)
1819 data->pdata->power_on(false);
1820 else
1821 mxt_power_on(data, false);
1822
1823 if (data->pdata->init_hw)
1824 data->pdata->init_hw(false);
1825 else
1826 mxt_regulator_configure(data, false);
1827
Mohan Pallakabfe8f302012-01-02 18:32:08 +08001828 if (gpio_is_valid(data->pdata->reset_gpio))
1829 gpio_free(data->pdata->reset_gpio);
1830
1831 if (gpio_is_valid(data->pdata->irq_gpio))
1832 gpio_free(data->pdata->irq_gpio);
1833
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001834 kfree(data->object_table);
1835 kfree(data);
1836
1837 return 0;
1838}
1839
Iiro Valkonen7686b102011-02-02 23:21:58 -08001840static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001841 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001842 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001843 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001844 { }
1845};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001846MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001847
Iiro Valkonen7686b102011-02-02 23:21:58 -08001848static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001849 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001850 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001851 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001852#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001853 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001854#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001855 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001856 .probe = mxt_probe,
1857 .remove = __devexit_p(mxt_remove),
1858 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001859};
1860
Iiro Valkonen7686b102011-02-02 23:21:58 -08001861static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001862{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001863 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001864}
1865
Iiro Valkonen7686b102011-02-02 23:21:58 -08001866static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001867{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001868 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001869}
1870
Iiro Valkonen7686b102011-02-02 23:21:58 -08001871module_init(mxt_init);
1872module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001873
1874/* Module information */
1875MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001876MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001877MODULE_LICENSE("GPL");