blob: 3133de40dc4f87d7ad7c45e9c94f7f3596583ee2 [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;
Jing Lindc4413c2012-01-16 15:22:52 -0800295 const struct mxt_config_info *config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800296 struct mxt_object *object_table;
297 struct mxt_info info;
298 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700299 unsigned int irq;
Amy Maloche21115eb2011-11-02 09:04:37 -0700300 struct regulator *vcc_ana;
301 struct regulator *vcc_dig;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530302 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530303#if defined(CONFIG_HAS_EARLYSUSPEND)
304 struct early_suspend early_suspend;
305#endif
Jing Lin36aee812011-10-17 17:17:28 -0700306
Amy Maloche52262212011-09-15 16:46:57 -0700307 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700308 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700309 u8 t9_ctrl;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800310 u32 keyarray_old;
311 u32 keyarray_new;
312 u8 t9_max_reportid;
313 u8 t9_min_reportid;
314 u8 t15_max_reportid;
315 u8 t15_min_reportid;
Jing Lindc4413c2012-01-16 15:22:52 -0800316 u8 curr_cfg_version;
317 int cfg_version_idx;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700318};
319
Iiro Valkonen7686b102011-02-02 23:21:58 -0800320static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700321{
322 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800323 case MXT_GEN_MESSAGE_T5:
324 case MXT_GEN_COMMAND_T6:
325 case MXT_GEN_POWER_T7:
326 case MXT_GEN_ACQUIRE_T8:
327 case MXT_GEN_DATASOURCE_T53:
328 case MXT_TOUCH_MULTI_T9:
329 case MXT_TOUCH_KEYARRAY_T15:
330 case MXT_TOUCH_PROXIMITY_T23:
331 case MXT_TOUCH_PROXKEY_T52:
332 case MXT_PROCI_GRIPFACE_T20:
333 case MXT_PROCG_NOISE_T22:
334 case MXT_PROCI_ONETOUCH_T24:
335 case MXT_PROCI_TWOTOUCH_T27:
336 case MXT_PROCI_GRIP_T40:
337 case MXT_PROCI_PALM_T41:
338 case MXT_PROCI_TOUCHSUPPRESSION_T42:
339 case MXT_PROCI_STYLUS_T47:
340 case MXT_PROCG_NOISESUPPRESSION_T48:
341 case MXT_SPT_COMMSCONFIG_T18:
342 case MXT_SPT_GPIOPWM_T19:
343 case MXT_SPT_SELFTEST_T25:
344 case MXT_SPT_CTECONFIG_T28:
345 case MXT_SPT_USERDATA_T38:
346 case MXT_SPT_DIGITIZER_T43:
347 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700348 return true;
349 default:
350 return false;
351 }
352}
353
Iiro Valkonen7686b102011-02-02 23:21:58 -0800354static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700355{
356 switch (type) {
Iiro Valkonene8645592011-11-18 12:56:19 -0800357 case MXT_GEN_COMMAND_T6:
358 case MXT_GEN_POWER_T7:
359 case MXT_GEN_ACQUIRE_T8:
360 case MXT_TOUCH_MULTI_T9:
361 case MXT_TOUCH_KEYARRAY_T15:
362 case MXT_TOUCH_PROXIMITY_T23:
363 case MXT_TOUCH_PROXKEY_T52:
364 case MXT_PROCI_GRIPFACE_T20:
365 case MXT_PROCG_NOISE_T22:
366 case MXT_PROCI_ONETOUCH_T24:
367 case MXT_PROCI_TWOTOUCH_T27:
368 case MXT_PROCI_GRIP_T40:
369 case MXT_PROCI_PALM_T41:
370 case MXT_PROCI_TOUCHSUPPRESSION_T42:
371 case MXT_PROCI_STYLUS_T47:
372 case MXT_PROCG_NOISESUPPRESSION_T48:
373 case MXT_SPT_COMMSCONFIG_T18:
374 case MXT_SPT_GPIOPWM_T19:
375 case MXT_SPT_SELFTEST_T25:
376 case MXT_SPT_CTECONFIG_T28:
377 case MXT_SPT_USERDATA_T38:
378 case MXT_SPT_DIGITIZER_T43:
379 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700380 return true;
381 default:
382 return false;
383 }
384}
385
Iiro Valkonen7686b102011-02-02 23:21:58 -0800386static void mxt_dump_message(struct device *dev,
387 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700388{
389 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
390 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
391 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
392 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
393 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
394 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
395 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
396 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
397 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
398}
399
Iiro Valkonen7686b102011-02-02 23:21:58 -0800400static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700401 unsigned int state)
402{
403 u8 val;
404
405recheck:
406 if (i2c_master_recv(client, &val, 1) != 1) {
407 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
408 return -EIO;
409 }
410
411 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800412 case MXT_WAITING_BOOTLOAD_CMD:
413 case MXT_WAITING_FRAME_DATA:
414 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700415 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800416 case MXT_FRAME_CRC_PASS:
417 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700418 goto recheck;
419 break;
420 default:
421 return -EINVAL;
422 }
423
424 if (val != state) {
425 dev_err(&client->dev, "Unvalid bootloader mode state\n");
426 return -EINVAL;
427 }
428
429 return 0;
430}
431
Iiro Valkonen7686b102011-02-02 23:21:58 -0800432static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700433{
434 u8 buf[2];
435
Iiro Valkonen7686b102011-02-02 23:21:58 -0800436 buf[0] = MXT_UNLOCK_CMD_LSB;
437 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700438
439 if (i2c_master_send(client, buf, 2) != 2) {
440 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
441 return -EIO;
442 }
443
444 return 0;
445}
446
Iiro Valkonen7686b102011-02-02 23:21:58 -0800447static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700448 const u8 *data, unsigned int frame_size)
449{
450 if (i2c_master_send(client, data, frame_size) != frame_size) {
451 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
452 return -EIO;
453 }
454
455 return 0;
456}
457
Iiro Valkonen7686b102011-02-02 23:21:58 -0800458static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700459 u16 reg, u16 len, void *val)
460{
461 struct i2c_msg xfer[2];
462 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700463 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700464
465 buf[0] = reg & 0xff;
466 buf[1] = (reg >> 8) & 0xff;
467
468 /* Write register */
469 xfer[0].addr = client->addr;
470 xfer[0].flags = 0;
471 xfer[0].len = 2;
472 xfer[0].buf = buf;
473
474 /* Read data */
475 xfer[1].addr = client->addr;
476 xfer[1].flags = I2C_M_RD;
477 xfer[1].len = len;
478 xfer[1].buf = val;
479
Jing Lin36aee812011-10-17 17:17:28 -0700480 do {
481 if (i2c_transfer(client->adapter, xfer, 2) == 2)
482 return 0;
483 msleep(MXT_WAKE_TIME);
484 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485
Jing Lin36aee812011-10-17 17:17:28 -0700486 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
487 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488}
489
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800492 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700493}
494
Jing Lin36aee812011-10-17 17:17:28 -0700495static int __mxt_write_reg(struct i2c_client *client,
496 u16 addr, u16 length, u8 *value)
497{
498 u8 buf[MXT_BLOCK_SIZE + 2];
499 int i, tries = 0;
500
501 if (length > MXT_BLOCK_SIZE)
502 return -EINVAL;
503
504 buf[0] = addr & 0xff;
505 buf[1] = (addr >> 8) & 0xff;
506 for (i = 0; i < length; i++)
507 buf[i + 2] = *value++;
508
509 do {
510 if (i2c_master_send(client, buf, length + 2) == (length + 2))
511 return 0;
512 msleep(MXT_WAKE_TIME);
513 } while (++tries < MXT_MAX_RW_TRIES);
514
515 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
516 return -EIO;
517}
518
Iiro Valkonen7686b102011-02-02 23:21:58 -0800519static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700520{
Jing Lin36aee812011-10-17 17:17:28 -0700521 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522}
523
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525 u16 reg, u8 *object_buf)
526{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528 object_buf);
529}
530
Iiro Valkonen7686b102011-02-02 23:21:58 -0800531static struct mxt_object *
532mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700533{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800534 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700535 int i;
536
537 for (i = 0; i < data->info.object_num; i++) {
538 object = data->object_table + i;
539 if (object->type == type)
540 return object;
541 }
542
543 dev_err(&data->client->dev, "Invalid object type\n");
544 return NULL;
545}
546
Iiro Valkonen7686b102011-02-02 23:21:58 -0800547static int mxt_read_message(struct mxt_data *data,
548 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700549{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800550 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700551 u16 reg;
552
Iiro Valkonene8645592011-11-18 12:56:19 -0800553 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554 if (!object)
555 return -EINVAL;
556
557 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800558 return __mxt_read_reg(data->client, reg,
559 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700560}
561
Iiro Valkonen7686b102011-02-02 23:21:58 -0800562static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700563 u8 type, u8 offset, u8 *val)
564{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800565 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700566 u16 reg;
567
Iiro Valkonen7686b102011-02-02 23:21:58 -0800568 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569 if (!object)
570 return -EINVAL;
571
572 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800573 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700574}
575
Iiro Valkonen7686b102011-02-02 23:21:58 -0800576static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 u8 type, u8 offset, u8 val)
578{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580 u16 reg;
581
Iiro Valkonen7686b102011-02-02 23:21:58 -0800582 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700583 if (!object)
584 return -EINVAL;
585
586 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800587 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700588}
589
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 struct input_dev *input_dev = data->input_dev;
594 int status = finger[single_id].status;
595 int finger_num = 0;
596 int id;
597
Iiro Valkonen7686b102011-02-02 23:21:58 -0800598 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599 if (!finger[id].status)
600 continue;
601
Amy Maloche2b59bab2011-10-13 16:08:16 -0700602 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
603 finger[id].status != MXT_RELEASE ?
604 finger[id].area : 0);
605 input_report_abs(input_dev, ABS_MT_POSITION_X,
606 finger[id].x);
607 input_report_abs(input_dev, ABS_MT_POSITION_Y,
608 finger[id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700609 input_report_abs(input_dev, ABS_MT_PRESSURE,
Mohan Pallaka5e7343f2012-01-02 18:30:16 +0800610 finger[id].status != MXT_RELEASE ?
611 finger[id].pressure : 0);
Amy Maloche2b59bab2011-10-13 16:08:16 -0700612 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700613
Amy Maloche2b59bab2011-10-13 16:08:16 -0700614 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700615 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700616 else
617 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700618 }
619
620 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
621
Iiro Valkonen7686b102011-02-02 23:21:58 -0800622 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700623 input_report_abs(input_dev, ABS_X, finger[single_id].x);
624 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700625 input_report_abs(input_dev,
626 ABS_PRESSURE, finger[single_id].pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700627 }
628
629 input_sync(input_dev);
630}
631
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632static void mxt_input_touchevent(struct mxt_data *data,
633 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636 struct device *dev = &data->client->dev;
637 u8 status = message->message[0];
638 int x;
639 int y;
640 int area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700641 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700642
643 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800644 if (!(status & MXT_DETECT)) {
645 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646 dev_dbg(dev, "[%d] released\n", id);
647
Iiro Valkonen7686b102011-02-02 23:21:58 -0800648 finger[id].status = MXT_RELEASE;
649 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700650 }
651 return;
652 }
653
654 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800655 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656 return;
657
Joonyoung Shim910d8052011-04-12 23:14:38 -0700658 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
659 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700660 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700661 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700662 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700663 y = y >> 2;
664
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665 area = message->message[4];
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700666 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667
668 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800669 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700670 x, y, area);
671
Iiro Valkonen7686b102011-02-02 23:21:58 -0800672 finger[id].status = status & MXT_MOVE ?
673 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674 finger[id].x = x;
675 finger[id].y = y;
676 finger[id].area = area;
Yufeng Shene6eb36a2011-10-11 12:28:21 -0700677 finger[id].pressure = pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700678
Iiro Valkonen7686b102011-02-02 23:21:58 -0800679 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680}
681
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800682static void mxt_handle_key_array(struct mxt_data *data,
683 struct mxt_message *message)
684{
685 u32 keys_changed;
686 int i;
687
688 if (!data->pdata->key_codes) {
689 dev_err(&data->client->dev, "keyarray is not supported\n");
690 return;
691 }
692
693 data->keyarray_new = message->message[1] |
694 (message->message[2] << 8) |
695 (message->message[3] << 16) |
696 (message->message[4] << 24);
697
698 keys_changed = data->keyarray_old ^ data->keyarray_new;
699
700 if (!keys_changed) {
701 dev_dbg(&data->client->dev, "no keys changed\n");
702 return;
703 }
704
705 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
706 if (!(keys_changed & (1 << i)))
707 continue;
708
709 input_report_key(data->input_dev, data->pdata->key_codes[i],
710 (data->keyarray_new & (1 << i)));
711 input_sync(data->input_dev);
712 }
713
714 data->keyarray_old = data->keyarray_new;
715}
716
Iiro Valkonen7686b102011-02-02 23:21:58 -0800717static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800719 struct mxt_data *data = dev_id;
720 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721 struct device *dev = &data->client->dev;
722 int id;
723 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700724
725 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800726 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727 dev_err(dev, "Failed to read message\n");
728 goto end;
729 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730 reportid = message.reportid;
731
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800732 if (!reportid) {
733 dev_dbg(dev, "Report id 0 is reserved\n");
734 continue;
735 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700736
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800737 /* check whether report id is part of T9 or T15 */
738 id = reportid - data->t9_min_reportid;
739
740 if (reportid >= data->t9_min_reportid &&
741 reportid <= data->t9_max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800742 mxt_input_touchevent(data, &message, id);
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800743 else if (reportid >= data->t15_min_reportid &&
744 reportid <= data->t15_max_reportid)
745 mxt_handle_key_array(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700746 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748 } while (reportid != 0xff);
749
750end:
751 return IRQ_HANDLED;
752}
753
Iiro Valkonen7686b102011-02-02 23:21:58 -0800754static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700755{
Jing Lindc4413c2012-01-16 15:22:52 -0800756 const struct mxt_config_info *config_info = data->config_info;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800757 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758 struct device *dev = &data->client->dev;
759 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800760 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761
Jing Lindc4413c2012-01-16 15:22:52 -0800762 if (!config_info) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800763 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
764 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700765 }
766
767 for (i = 0; i < data->info.object_num; i++) {
768 object = data->object_table + i;
769
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771 continue;
772
Iiro Valkonen71749f52011-02-15 13:36:52 -0800773 for (j = 0; j < object->size + 1; j++) {
774 config_offset = index + j;
Jing Lindc4413c2012-01-16 15:22:52 -0800775 if (config_offset > config_info->config_length) {
Iiro Valkonen71749f52011-02-15 13:36:52 -0800776 dev_err(dev, "Not enough config data!\n");
777 return -EINVAL;
778 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779 mxt_write_object(data, object->type, j,
Jing Lindc4413c2012-01-16 15:22:52 -0800780 config_info->config[config_offset]);
Iiro Valkonen71749f52011-02-15 13:36:52 -0800781 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700782 index += object->size + 1;
783 }
784
785 return 0;
786}
787
Iiro Valkonen7686b102011-02-02 23:21:58 -0800788static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789{
790 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800791 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700792 int count = 10;
793 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794
795 /* Read dummy message to make high CHG pin */
796 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800797 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798 if (error)
799 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800800 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801
802 if (!count) {
803 dev_err(dev, "CHG pin isn't cleared\n");
804 return -EBUSY;
805 }
806
807 return 0;
808}
809
Iiro Valkonen7686b102011-02-02 23:21:58 -0800810static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811{
812 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814 int error;
815 u8 val;
816
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818 if (error)
819 return error;
820 info->family_id = val;
821
Iiro Valkonen7686b102011-02-02 23:21:58 -0800822 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700823 if (error)
824 return error;
825 info->variant_id = val;
826
Iiro Valkonen7686b102011-02-02 23:21:58 -0800827 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700828 if (error)
829 return error;
830 info->version = val;
831
Iiro Valkonen7686b102011-02-02 23:21:58 -0800832 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700833 if (error)
834 return error;
835 info->build = val;
836
Iiro Valkonen7686b102011-02-02 23:21:58 -0800837 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700838 if (error)
839 return error;
840 info->object_num = val;
841
842 return 0;
843}
844
Iiro Valkonen7686b102011-02-02 23:21:58 -0800845static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700846{
847 int error;
848 int i;
849 u16 reg;
850 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800851 u8 buf[MXT_OBJECT_SIZE];
Jing Lindc4413c2012-01-16 15:22:52 -0800852 bool found_t38 = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700853
854 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800855 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856
Iiro Valkonen7686b102011-02-02 23:21:58 -0800857 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
858 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700859 if (error)
860 return error;
861
862 object->type = buf[0];
863 object->start_address = (buf[2] << 8) | buf[1];
864 object->size = buf[3];
865 object->instances = buf[4];
866 object->num_report_ids = buf[5];
867
868 if (object->num_report_ids) {
869 reportid += object->num_report_ids *
870 (object->instances + 1);
871 object->max_reportid = reportid;
872 }
Jing Lindc4413c2012-01-16 15:22:52 -0800873
874 /* Calculate index for config major version in config array.
875 * Major version is the first byte in object T38.
876 */
877 if (object->type == MXT_SPT_USERDATA_T38)
878 found_t38 = true;
879 if (!found_t38 && mxt_object_writable(object->type))
880 data->cfg_version_idx += object->size + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700881 }
882
883 return 0;
884}
885
Jing Lindc4413c2012-01-16 15:22:52 -0800886static int mxt_search_config_array(struct mxt_data *data, bool version_match)
887{
888
889 const struct mxt_platform_data *pdata = data->pdata;
890 const struct mxt_config_info *cfg_info;
891 struct mxt_info *info = &data->info;
892 int i;
893 u8 cfg_version;
894
895 for (i = 0; i < pdata->config_array_size; i++) {
896
897 cfg_info = &pdata->config_array[i];
898
899 if (!cfg_info->config || !cfg_info->config_length)
900 continue;
901
902 if (info->family_id == cfg_info->family_id &&
903 info->variant_id == cfg_info->variant_id &&
904 info->version == cfg_info->version &&
905 info->build == cfg_info->build) {
906
907 cfg_version = cfg_info->config[data->cfg_version_idx];
908 if (data->curr_cfg_version == cfg_version ||
909 !version_match) {
910 data->config_info = cfg_info;
911 return 0;
912 }
913 }
914 }
915
916 dev_info(&data->client->dev,
917 "Config not found: F: %d, V: %d, FW: %d.%d.%d, CFG: %d\n",
918 info->family_id, info->variant_id,
919 info->version >> 4, info->version & 0xF, info->build,
920 data->curr_cfg_version);
921 return -EINVAL;
922}
923
924static int mxt_get_config(struct mxt_data *data)
925{
926 const struct mxt_platform_data *pdata = data->pdata;
927 struct device *dev = &data->client->dev;
928 struct mxt_object *object;
929 int error;
930
931 if (!pdata->config_array || !pdata->config_array_size) {
932 dev_dbg(dev, "No cfg data provided by platform data\n");
933 return 0;
934 }
935
936 /* Get current config version */
937 object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
938 if (!object) {
939 dev_err(dev, "Unable to obtain USERDATA object\n");
940 return -EINVAL;
941 }
942
943 error = mxt_read_reg(data->client, object->start_address,
944 &data->curr_cfg_version);
945 if (error) {
946 dev_err(dev, "Unable to read config version\n");
947 return error;
948 }
949
950 /* It is possible that the config data on the controller is not
951 * versioned and the version number returns 0. In this case,
952 * find a match without the config version checking.
953 */
954 error = mxt_search_config_array(data,
955 data->curr_cfg_version != 0 ? true : false);
956 if (error)
957 return error;
958
959 return 0;
960}
Amy Maloche7e447432011-09-14 11:36:30 -0700961static void mxt_reset_delay(struct mxt_data *data)
962{
963 struct mxt_info *info = &data->info;
964
965 switch (info->family_id) {
966 case MXT224_ID:
967 msleep(MXT224_RESET_TIME);
968 break;
Amy Maloche380cc0b2011-11-03 12:55:04 -0700969 case MXT224E_ID:
970 msleep(MXT224E_RESET_TIME);
971 break;
Amy Maloche7e447432011-09-14 11:36:30 -0700972 case MXT1386_ID:
973 msleep(MXT1386_RESET_TIME);
974 break;
975 default:
976 msleep(MXT_RESET_TIME);
977 }
978}
979
Iiro Valkonen7686b102011-02-02 23:21:58 -0800980static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700981{
982 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800983 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700984 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700985 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700986 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700987 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700988 struct mxt_object *t7_object;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +0800989 struct mxt_object *t9_object;
990 struct mxt_object *t15_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993 if (error)
994 return error;
995
996 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800997 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998 GFP_KERNEL);
999 if (!data->object_table) {
1000 dev_err(&client->dev, "Failed to allocate memory\n");
1001 return -ENOMEM;
1002 }
1003
1004 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001005 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001007 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001008
Jing Lindc4413c2012-01-16 15:22:52 -08001009 /* Get config data from platform data */
1010 error = mxt_get_config(data);
1011 if (error)
1012 dev_dbg(&client->dev, "Config info not found.\n");
1013
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001014 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001015 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001017 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
Amy Maloche52262212011-09-15 16:46:57 -07001019 /* Store T7 and T9 locally, used in suspend/resume operations */
Iiro Valkonene8645592011-11-18 12:56:19 -08001020 t7_object = mxt_get_object(data, MXT_GEN_POWER_T7);
Jing Lin36aee812011-10-17 17:17:28 -07001021 if (!t7_object) {
1022 dev_err(&client->dev, "Failed to get T7 object\n");
Jing Lin32c72532011-11-03 12:02:33 -07001023 error = -EINVAL;
1024 goto free_object_table;
Jing Lin36aee812011-10-17 17:17:28 -07001025 }
1026
1027 data->t7_start_addr = t7_object->start_address;
1028 error = __mxt_read_reg(client, data->t7_start_addr,
1029 T7_DATA_SIZE, data->t7_data);
1030 if (error < 0) {
1031 dev_err(&client->dev,
Jing Lin32c72532011-11-03 12:02:33 -07001032 "Failed to save current power state\n");
1033 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001034 }
Iiro Valkonene8645592011-11-18 12:56:19 -08001035 error = mxt_read_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL,
Amy Maloche52262212011-09-15 16:46:57 -07001036 &data->t9_ctrl);
1037 if (error < 0) {
Jing Lin32c72532011-11-03 12:02:33 -07001038 dev_err(&client->dev, "Failed to save current touch object\n");
1039 goto free_object_table;
Amy Maloche52262212011-09-15 16:46:57 -07001040 }
1041
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001042 /* Store T9, T15's min and max report ids */
1043 t9_object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1044 if (!t9_object) {
1045 dev_err(&client->dev, "Failed to get T9 object\n");
1046 error = -EINVAL;
1047 goto free_object_table;
1048 }
1049 data->t9_max_reportid = t9_object->max_reportid;
1050 data->t9_min_reportid = t9_object->max_reportid -
1051 t9_object->num_report_ids + 1;
1052
1053 if (data->pdata->key_codes) {
1054 t15_object = mxt_get_object(data, MXT_TOUCH_KEYARRAY_T15);
1055 if (!t15_object)
1056 dev_dbg(&client->dev, "T15 object is not available\n");
1057 else {
1058 data->t15_max_reportid = t15_object->max_reportid;
1059 data->t15_min_reportid = t15_object->max_reportid -
1060 t15_object->num_report_ids + 1;
1061 }
1062 }
1063
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001064 /* Backup to memory */
Iiro Valkonene8645592011-11-18 12:56:19 -08001065 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066 MXT_COMMAND_BACKUPNV,
1067 MXT_BACKUP_VALUE);
1068 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001069 do {
Iiro Valkonene8645592011-11-18 12:56:19 -08001070 error = mxt_read_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001071 MXT_COMMAND_BACKUPNV,
1072 &command_register);
1073 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001074 goto free_object_table;
Amy Maloche7e447432011-09-14 11:36:30 -07001075 usleep_range(1000, 2000);
1076 } while ((command_register != 0) && (++timeout_counter <= 100));
1077 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001078 dev_err(&client->dev, "No response after backup!\n");
Jing Lin32c72532011-11-03 12:02:33 -07001079 error = -EIO;
1080 goto free_object_table;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001081 }
1082
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083
1084 /* Soft reset */
Iiro Valkonene8645592011-11-18 12:56:19 -08001085 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -07001087
Amy Maloche7e447432011-09-14 11:36:30 -07001088 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001089
1090 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001091 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001092 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001093 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001094 info->matrix_xsize = val;
1095
Iiro Valkonen7686b102011-02-02 23:21:58 -08001096 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097 if (error)
Jing Lin32c72532011-11-03 12:02:33 -07001098 goto free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001099 info->matrix_ysize = val;
1100
1101 dev_info(&client->dev,
1102 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
1103 info->family_id, info->variant_id, info->version,
1104 info->build);
1105
1106 dev_info(&client->dev,
1107 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
1108 info->matrix_xsize, info->matrix_ysize,
1109 info->object_num);
1110
1111 return 0;
Jing Lin32c72532011-11-03 12:02:33 -07001112
1113free_object_table:
1114 kfree(data->object_table);
1115 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001116}
1117
Iiro Valkonen7686b102011-02-02 23:21:58 -08001118static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001119 struct device_attribute *attr, char *buf)
1120{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001121 struct mxt_data *data = dev_get_drvdata(dev);
1122 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001123 int count = 0;
1124 int i, j;
1125 int error;
1126 u8 val;
1127
1128 for (i = 0; i < data->info.object_num; i++) {
1129 object = data->object_table + i;
1130
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001131 count += snprintf(buf + count, PAGE_SIZE - count,
1132 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001133 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001134 if (count >= PAGE_SIZE)
1135 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001136
Iiro Valkonen7686b102011-02-02 23:21:58 -08001137 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001138 count += snprintf(buf + count, PAGE_SIZE - count,
1139 "\n");
1140 if (count >= PAGE_SIZE)
1141 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001142 continue;
1143 }
1144
1145 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001146 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001147 object->type, j, &val);
1148 if (error)
1149 return error;
1150
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001151 count += snprintf(buf + count, PAGE_SIZE - count,
1152 "\t[%2d]: %02x (%d)\n", j, val, val);
1153 if (count >= PAGE_SIZE)
1154 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001155 }
1156
Daniel Kurtz4ef11a82011-11-02 10:43:08 -07001157 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1158 if (count >= PAGE_SIZE)
1159 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001160 }
1161
1162 return count;
1163}
1164
Iiro Valkonen7686b102011-02-02 23:21:58 -08001165static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001167 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168 struct i2c_client *client = data->client;
1169 const struct firmware *fw = NULL;
1170 unsigned int frame_size;
1171 unsigned int pos = 0;
1172 int ret;
1173
1174 ret = request_firmware(&fw, fn, dev);
1175 if (ret) {
1176 dev_err(dev, "Unable to open firmware %s\n", fn);
1177 return ret;
1178 }
1179
1180 /* Change to the bootloader mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001181 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001182 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -07001183
1184 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001185
1186 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001187 if (client->addr == MXT_APP_LOW)
1188 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001189 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001190 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001191
Iiro Valkonen7686b102011-02-02 23:21:58 -08001192 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001193 if (ret)
1194 goto out;
1195
1196 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001197 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001198
1199 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001200 ret = mxt_check_bootloader(client,
1201 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001202 if (ret)
1203 goto out;
1204
1205 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1206
1207 /* We should add 2 at frame size as the the firmware data is not
1208 * included the CRC bytes.
1209 */
1210 frame_size += 2;
1211
1212 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001213 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001214
Iiro Valkonen7686b102011-02-02 23:21:58 -08001215 ret = mxt_check_bootloader(client,
1216 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001217 if (ret)
1218 goto out;
1219
1220 pos += frame_size;
1221
1222 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1223 }
1224
1225out:
1226 release_firmware(fw);
1227
1228 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001229 if (client->addr == MXT_BOOT_LOW)
1230 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001231 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001232 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001233
1234 return ret;
1235}
1236
Iiro Valkonen7686b102011-02-02 23:21:58 -08001237static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001238 struct device_attribute *attr,
1239 const char *buf, size_t count)
1240{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001241 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001242 int error;
1243
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001244 disable_irq(data->irq);
1245
Iiro Valkonen7686b102011-02-02 23:21:58 -08001246 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247 if (error) {
1248 dev_err(dev, "The firmware update failed(%d)\n", error);
1249 count = error;
1250 } else {
1251 dev_dbg(dev, "The firmware update succeeded\n");
1252
1253 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001254 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001255
1256 kfree(data->object_table);
1257 data->object_table = NULL;
1258
Iiro Valkonen7686b102011-02-02 23:21:58 -08001259 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001260 }
1261
1262 enable_irq(data->irq);
1263
Iiro Valkonen08960a02011-04-12 23:16:40 -07001264 error = mxt_make_highchg(data);
1265 if (error)
1266 return error;
1267
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001268 return count;
1269}
1270
Iiro Valkonen7686b102011-02-02 23:21:58 -08001271static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1272static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273
Iiro Valkonen7686b102011-02-02 23:21:58 -08001274static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001275 &dev_attr_object.attr,
1276 &dev_attr_update_fw.attr,
1277 NULL
1278};
1279
Iiro Valkonen7686b102011-02-02 23:21:58 -08001280static const struct attribute_group mxt_attr_group = {
1281 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001282};
1283
Amy Maloche52262212011-09-15 16:46:57 -07001284static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001285{
Jing Lin36aee812011-10-17 17:17:28 -07001286 int error;
1287
Amy Maloche52262212011-09-15 16:46:57 -07001288 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001289 error = __mxt_write_reg(data->client, data->t7_start_addr,
1290 T7_DATA_SIZE, data->t7_data);
1291 if (error < 0) {
1292 dev_err(&data->client->dev,
1293 "failed to restore old power state\n");
1294 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001295 }
Jing Lin36aee812011-10-17 17:17:28 -07001296
Amy Maloche52262212011-09-15 16:46:57 -07001297 error = mxt_write_object(data,
Iiro Valkonene8645592011-11-18 12:56:19 -08001298 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, data->t9_ctrl);
Amy Maloche52262212011-09-15 16:46:57 -07001299 if (error < 0) {
1300 dev_err(&data->client->dev, "failed to restore touch\n");
1301 return error;
1302 }
1303
1304 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001305}
1306
Amy Maloche52262212011-09-15 16:46:57 -07001307static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001308{
Jing Lin36aee812011-10-17 17:17:28 -07001309 int error;
1310 u8 t7_data[T7_DATA_SIZE] = {0};
1311
1312 /* disable touch and configure deep sleep mode */
Iiro Valkonene8645592011-11-18 12:56:19 -08001313 error = mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Jing Lin36aee812011-10-17 17:17:28 -07001314 if (error < 0) {
1315 dev_err(&data->client->dev, "failed to disable touch\n");
1316 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001317 }
1318
Jing Lin36aee812011-10-17 17:17:28 -07001319 error = __mxt_write_reg(data->client, data->t7_start_addr,
1320 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001321 if (error < 0) {
1322 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001323 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001324 return error;
1325 }
1326
1327 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001328}
1329
Iiro Valkonen7686b102011-02-02 23:21:58 -08001330static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001331{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001332 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001333 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001334
Amy Maloche52262212011-09-15 16:46:57 -07001335 error = mxt_start(data);
1336 if (error < 0) {
1337 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1338 return error;
1339 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001340
1341 return 0;
1342}
1343
Iiro Valkonen7686b102011-02-02 23:21:58 -08001344static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001345{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001346 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001347 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001348
Amy Maloche52262212011-09-15 16:46:57 -07001349 error = mxt_stop(data);
1350 if (error < 0)
1351 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1352
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001353}
1354
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301355static int mxt_power_on(struct mxt_data *data, bool on)
1356{
1357 int rc;
1358
1359 if (on == false)
1360 goto power_off;
1361
Amy Maloche21115eb2011-11-02 09:04:37 -07001362 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301363 if (rc < 0) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001364 dev_err(&data->client->dev,
1365 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301366 return rc;
1367 }
1368
Amy Maloche21115eb2011-11-02 09:04:37 -07001369 rc = regulator_enable(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301370 if (rc) {
Amy Maloche21115eb2011-11-02 09:04:37 -07001371 dev_err(&data->client->dev,
1372 "Regulator vcc_ana enable failed rc=%d\n", rc);
1373 goto error_reg_en_vcc_ana;
1374 }
1375
1376 if (data->pdata->digital_pwr_regulator) {
1377 rc = regulator_set_optimum_mode(data->vcc_dig,
1378 MXT_ACTIVE_LOAD_DIG_UA);
1379 if (rc < 0) {
1380 dev_err(&data->client->dev,
1381 "Regulator vcc_dig set_opt failed rc=%d\n",
1382 rc);
1383 goto error_reg_opt_vcc_dig;
1384 }
1385
1386 rc = regulator_enable(data->vcc_dig);
1387 if (rc) {
1388 dev_err(&data->client->dev,
1389 "Regulator vcc_dig enable failed rc=%d\n", rc);
1390 goto error_reg_en_vcc_dig;
1391 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301392 }
1393
1394 if (data->pdata->i2c_pull_up) {
1395 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1396 if (rc < 0) {
1397 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001398 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301399 goto error_reg_opt_i2c;
1400 }
1401
1402 rc = regulator_enable(data->vcc_i2c);
1403 if (rc) {
1404 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001405 "Regulator vcc_i2c enable failed rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301406 goto error_reg_en_vcc_i2c;
1407 }
1408 }
1409
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001410 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301411
1412 return 0;
1413
1414error_reg_en_vcc_i2c:
1415 if (data->pdata->i2c_pull_up)
1416 regulator_set_optimum_mode(data->vcc_i2c, 0);
1417error_reg_opt_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001418 if (data->pdata->digital_pwr_regulator)
1419 regulator_disable(data->vcc_dig);
1420error_reg_en_vcc_dig:
1421 if (data->pdata->digital_pwr_regulator)
1422 regulator_set_optimum_mode(data->vcc_dig, 0);
1423error_reg_opt_vcc_dig:
1424 regulator_disable(data->vcc_ana);
1425error_reg_en_vcc_ana:
1426 regulator_set_optimum_mode(data->vcc_ana, 0);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301427 return rc;
1428
1429power_off:
Amy Maloche21115eb2011-11-02 09:04:37 -07001430 regulator_set_optimum_mode(data->vcc_ana, 0);
1431 regulator_disable(data->vcc_ana);
1432 if (data->pdata->digital_pwr_regulator) {
1433 regulator_set_optimum_mode(data->vcc_dig, 0);
1434 regulator_disable(data->vcc_dig);
1435 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301436 if (data->pdata->i2c_pull_up) {
1437 regulator_set_optimum_mode(data->vcc_i2c, 0);
1438 regulator_disable(data->vcc_i2c);
1439 }
1440 msleep(50);
1441 return 0;
1442}
1443
1444static int mxt_regulator_configure(struct mxt_data *data, bool on)
1445{
1446 int rc;
1447
1448 if (on == false)
1449 goto hw_shutdown;
1450
Amy Maloche21115eb2011-11-02 09:04:37 -07001451 data->vcc_ana = regulator_get(&data->client->dev, "vdd_ana");
1452 if (IS_ERR(data->vcc_ana)) {
1453 rc = PTR_ERR(data->vcc_ana);
1454 dev_err(&data->client->dev,
1455 "Regulator get failed vcc_ana rc=%d\n", rc);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301456 return rc;
1457 }
1458
Amy Maloche21115eb2011-11-02 09:04:37 -07001459 if (regulator_count_voltages(data->vcc_ana) > 0) {
1460 rc = regulator_set_voltage(data->vcc_ana, MXT_VTG_MIN_UV,
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301461 MXT_VTG_MAX_UV);
1462 if (rc) {
1463 dev_err(&data->client->dev,
1464 "regulator set_vtg failed rc=%d\n", rc);
Amy Maloche21115eb2011-11-02 09:04:37 -07001465 goto error_set_vtg_vcc_ana;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301466 }
1467 }
Amy Maloche21115eb2011-11-02 09:04:37 -07001468 if (data->pdata->digital_pwr_regulator) {
1469 data->vcc_dig = regulator_get(&data->client->dev, "vdd_dig");
1470 if (IS_ERR(data->vcc_dig)) {
1471 rc = PTR_ERR(data->vcc_dig);
1472 dev_err(&data->client->dev,
1473 "Regulator get dig failed rc=%d\n", rc);
1474 goto error_get_vtg_vcc_dig;
1475 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301476
Amy Maloche21115eb2011-11-02 09:04:37 -07001477 if (regulator_count_voltages(data->vcc_dig) > 0) {
1478 rc = regulator_set_voltage(data->vcc_dig,
1479 MXT_VTG_DIG_MIN_UV, MXT_VTG_DIG_MAX_UV);
1480 if (rc) {
1481 dev_err(&data->client->dev,
1482 "regulator set_vtg failed rc=%d\n", rc);
1483 goto error_set_vtg_vcc_dig;
1484 }
1485 }
1486 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301487 if (data->pdata->i2c_pull_up) {
1488 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1489 if (IS_ERR(data->vcc_i2c)) {
1490 rc = PTR_ERR(data->vcc_i2c);
1491 dev_err(&data->client->dev,
1492 "Regulator get failed rc=%d\n", rc);
1493 goto error_get_vtg_i2c;
1494 }
1495 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1496 rc = regulator_set_voltage(data->vcc_i2c,
1497 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1498 if (rc) {
1499 dev_err(&data->client->dev,
1500 "regulator set_vtg failed rc=%d\n", rc);
1501 goto error_set_vtg_i2c;
1502 }
1503 }
1504 }
1505
1506 return 0;
1507
1508error_set_vtg_i2c:
1509 regulator_put(data->vcc_i2c);
1510error_get_vtg_i2c:
Amy Maloche21115eb2011-11-02 09:04:37 -07001511 if (data->pdata->digital_pwr_regulator)
1512 if (regulator_count_voltages(data->vcc_dig) > 0)
1513 regulator_set_voltage(data->vcc_dig, 0,
1514 MXT_VTG_DIG_MAX_UV);
1515error_set_vtg_vcc_dig:
1516 if (data->pdata->digital_pwr_regulator)
1517 regulator_put(data->vcc_dig);
1518error_get_vtg_vcc_dig:
1519 if (regulator_count_voltages(data->vcc_ana) > 0)
1520 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1521error_set_vtg_vcc_ana:
1522 regulator_put(data->vcc_ana);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301523 return rc;
1524
1525hw_shutdown:
Amy Maloche21115eb2011-11-02 09:04:37 -07001526 if (regulator_count_voltages(data->vcc_ana) > 0)
1527 regulator_set_voltage(data->vcc_ana, 0, MXT_VTG_MAX_UV);
1528 regulator_put(data->vcc_ana);
1529 if (data->pdata->digital_pwr_regulator) {
1530 if (regulator_count_voltages(data->vcc_dig) > 0)
1531 regulator_set_voltage(data->vcc_dig, 0,
1532 MXT_VTG_DIG_MAX_UV);
1533 regulator_put(data->vcc_dig);
1534 }
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301535 if (data->pdata->i2c_pull_up) {
1536 if (regulator_count_voltages(data->vcc_i2c) > 0)
1537 regulator_set_voltage(data->vcc_i2c, 0,
1538 MXT_I2C_VTG_MAX_UV);
1539 regulator_put(data->vcc_i2c);
1540 }
1541 return 0;
1542}
1543
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301544#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001545static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1546{
1547
1548 int rc;
1549
1550 if (on == false)
1551 goto regulator_hpm;
1552
Amy Maloche21115eb2011-11-02 09:04:37 -07001553 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001554 if (rc < 0) {
1555 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001556 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001557 goto fail_regulator_lpm;
1558 }
1559
Amy Maloche21115eb2011-11-02 09:04:37 -07001560 if (data->pdata->digital_pwr_regulator) {
1561 rc = regulator_set_optimum_mode(data->vcc_dig,
1562 MXT_LPM_LOAD_DIG_UA);
1563 if (rc < 0) {
1564 dev_err(&data->client->dev,
1565 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1566 goto fail_regulator_lpm;
1567 }
1568 }
1569
Jing Linbace50b2011-10-18 22:55:47 -07001570 if (data->pdata->i2c_pull_up) {
1571 rc = regulator_set_optimum_mode(data->vcc_i2c,
1572 MXT_I2C_LPM_LOAD_UA);
1573 if (rc < 0) {
1574 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001575 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001576 goto fail_regulator_lpm;
1577 }
1578 }
1579
1580 return 0;
1581
1582regulator_hpm:
1583
Amy Maloche21115eb2011-11-02 09:04:37 -07001584 rc = regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001585 if (rc < 0) {
1586 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001587 "Regulator vcc_ana set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001588 goto fail_regulator_hpm;
1589 }
1590
Amy Maloche21115eb2011-11-02 09:04:37 -07001591 if (data->pdata->digital_pwr_regulator) {
1592 rc = regulator_set_optimum_mode(data->vcc_dig,
1593 MXT_ACTIVE_LOAD_DIG_UA);
1594 if (rc < 0) {
1595 dev_err(&data->client->dev,
1596 "Regulator vcc_dig set_opt failed rc=%d\n", rc);
1597 goto fail_regulator_hpm;
1598 }
1599 }
1600
Jing Linbace50b2011-10-18 22:55:47 -07001601 if (data->pdata->i2c_pull_up) {
1602 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1603 if (rc < 0) {
1604 dev_err(&data->client->dev,
Amy Maloche21115eb2011-11-02 09:04:37 -07001605 "Regulator vcc_i2c set_opt failed rc=%d\n", rc);
Jing Linbace50b2011-10-18 22:55:47 -07001606 goto fail_regulator_hpm;
1607 }
1608 }
1609
1610 return 0;
1611
1612fail_regulator_lpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001613 regulator_set_optimum_mode(data->vcc_ana, MXT_ACTIVE_LOAD_UA);
1614 if (data->pdata->digital_pwr_regulator)
1615 regulator_set_optimum_mode(data->vcc_dig,
1616 MXT_ACTIVE_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001617 if (data->pdata->i2c_pull_up)
1618 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1619
1620 return rc;
1621
1622fail_regulator_hpm:
Amy Maloche21115eb2011-11-02 09:04:37 -07001623 regulator_set_optimum_mode(data->vcc_ana, MXT_LPM_LOAD_UA);
1624 if (data->pdata->digital_pwr_regulator)
1625 regulator_set_optimum_mode(data->vcc_dig, MXT_LPM_LOAD_DIG_UA);
Jing Linbace50b2011-10-18 22:55:47 -07001626 if (data->pdata->i2c_pull_up)
1627 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1628
1629 return rc;
1630}
1631
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301632static int mxt_suspend(struct device *dev)
1633{
1634 struct i2c_client *client = to_i2c_client(dev);
1635 struct mxt_data *data = i2c_get_clientdata(client);
1636 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001637 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301638
1639 mutex_lock(&input_dev->mutex);
1640
Amy Maloche52262212011-09-15 16:46:57 -07001641 if (input_dev->users) {
1642 error = mxt_stop(data);
1643 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001644 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001645 mutex_unlock(&input_dev->mutex);
1646 return error;
1647 }
1648
1649 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301650
1651 mutex_unlock(&input_dev->mutex);
1652
Jing Linbace50b2011-10-18 22:55:47 -07001653 /* put regulators in low power mode */
1654 error = mxt_regulator_lpm(data, true);
1655 if (error < 0) {
1656 dev_err(dev, "failed to enter low power mode\n");
1657 return error;
1658 }
1659
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301660 return 0;
1661}
1662
1663static int mxt_resume(struct device *dev)
1664{
1665 struct i2c_client *client = to_i2c_client(dev);
1666 struct mxt_data *data = i2c_get_clientdata(client);
1667 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001668 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301669
Jing Linbace50b2011-10-18 22:55:47 -07001670 /* put regulators in high power mode */
1671 error = mxt_regulator_lpm(data, false);
1672 if (error < 0) {
1673 dev_err(dev, "failed to enter high power mode\n");
1674 return error;
1675 }
1676
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301677 mutex_lock(&input_dev->mutex);
1678
Amy Maloche52262212011-09-15 16:46:57 -07001679 if (input_dev->users) {
1680 error = mxt_start(data);
1681 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001682 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001683 mutex_unlock(&input_dev->mutex);
1684 return error;
1685 }
1686 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301687
1688 mutex_unlock(&input_dev->mutex);
1689
1690 return 0;
1691}
1692
1693#if defined(CONFIG_HAS_EARLYSUSPEND)
1694static void mxt_early_suspend(struct early_suspend *h)
1695{
1696 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1697
1698 mxt_suspend(&data->client->dev);
1699}
1700
1701static void mxt_late_resume(struct early_suspend *h)
1702{
1703 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1704
1705 mxt_resume(&data->client->dev);
1706}
1707#endif
1708
1709static const struct dev_pm_ops mxt_pm_ops = {
1710#ifndef CONFIG_HAS_EARLYSUSPEND
1711 .suspend = mxt_suspend,
1712 .resume = mxt_resume,
1713#endif
1714};
1715#endif
1716
Iiro Valkonen7686b102011-02-02 23:21:58 -08001717static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001718 const struct i2c_device_id *id)
1719{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001720 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001721 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001722 struct input_dev *input_dev;
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001723 int error, i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001724
Iiro Valkonen919ed892011-02-15 13:36:52 -08001725 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001726 return -EINVAL;
1727
Iiro Valkonen7686b102011-02-02 23:21:58 -08001728 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001729 input_dev = input_allocate_device();
1730 if (!data || !input_dev) {
1731 dev_err(&client->dev, "Failed to allocate memory\n");
1732 error = -ENOMEM;
1733 goto err_free_mem;
1734 }
1735
Iiro Valkonen7686b102011-02-02 23:21:58 -08001736 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001737 input_dev->id.bustype = BUS_I2C;
1738 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001739 input_dev->open = mxt_input_open;
1740 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001741
Joonyoung Shim910d8052011-04-12 23:14:38 -07001742 data->client = client;
1743 data->input_dev = input_dev;
1744 data->pdata = pdata;
1745 data->irq = client->irq;
1746
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001747 __set_bit(EV_ABS, input_dev->evbit);
1748 __set_bit(EV_KEY, input_dev->evbit);
1749 __set_bit(BTN_TOUCH, input_dev->keybit);
1750
1751 /* For single touch */
1752 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001753 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001754 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001755 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001756 input_set_abs_params(input_dev, ABS_PRESSURE,
1757 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001758
1759 /* For multi touch */
1760 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001761 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001762 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001763 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001764 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001765 0, data->pdata->y_size, 0, 0);
Yufeng Shene6eb36a2011-10-11 12:28:21 -07001766 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1767 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001768
Mohan Pallaka382d3ce2012-01-02 20:24:28 +08001769 /* set key array supported keys */
1770 if (pdata->key_codes) {
1771 for (i = 0; i < MXT_KEYARRAY_MAX_KEYS; i++) {
1772 if (pdata->key_codes[i])
1773 input_set_capability(input_dev, EV_KEY,
1774 pdata->key_codes[i]);
1775 }
1776 }
1777
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001778 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001779 i2c_set_clientdata(client, data);
1780
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301781 if (pdata->init_hw)
1782 error = pdata->init_hw(true);
1783 else
1784 error = mxt_regulator_configure(data, true);
1785 if (error) {
1786 dev_err(&client->dev, "Failed to intialize hardware\n");
Jing Lin32c72532011-11-03 12:02:33 -07001787 goto err_free_mem;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301788 }
1789
1790 if (pdata->power_on)
1791 error = pdata->power_on(true);
1792 else
1793 error = mxt_power_on(data, true);
1794 if (error) {
1795 dev_err(&client->dev, "Failed to power on hardware\n");
1796 goto err_regulator_on;
1797 }
1798
Amy Maloche08266db2011-11-04 11:07:16 -07001799 if (gpio_is_valid(pdata->irq_gpio)) {
1800 /* configure touchscreen irq gpio */
1801 error = gpio_request(pdata->irq_gpio,
1802 "mxt_irq_gpio");
1803 if (error) {
1804 pr_err("%s: unable to request gpio [%d]\n", __func__,
1805 pdata->irq_gpio);
1806 goto err_power_on;
1807 }
1808 error = gpio_direction_input(pdata->irq_gpio);
1809 if (error) {
1810 pr_err("%s: unable to set_direction for gpio [%d]\n",
1811 __func__, pdata->irq_gpio);
1812 goto err_irq_gpio_req;
1813 }
1814 }
1815
1816 if (gpio_is_valid(pdata->reset_gpio)) {
1817 /* configure touchscreen reset out gpio */
1818 error = gpio_request(pdata->reset_gpio,
1819 "mxt_reset_gpio");
1820 if (error) {
1821 pr_err("%s: unable to request reset gpio %d\n",
1822 __func__, pdata->reset_gpio);
1823 goto err_irq_gpio_req;
1824 }
1825
1826 error = gpio_direction_output(
1827 pdata->reset_gpio, 1);
1828 if (error) {
1829 pr_err("%s: unable to set direction for gpio %d\n",
1830 __func__, pdata->reset_gpio);
1831 goto err_reset_gpio_req;
1832 }
1833 }
1834
1835 mxt_reset_delay(data);
1836
Iiro Valkonen7686b102011-02-02 23:21:58 -08001837 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001838 if (error)
Amy Maloche08266db2011-11-04 11:07:16 -07001839 goto err_reset_gpio_req;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001840
Iiro Valkonen7686b102011-02-02 23:21:58 -08001841 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001842 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001843 if (error) {
1844 dev_err(&client->dev, "Failed to register interrupt\n");
Jing Lin32c72532011-11-03 12:02:33 -07001845 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001846 }
1847
Iiro Valkonen08960a02011-04-12 23:16:40 -07001848 error = mxt_make_highchg(data);
1849 if (error)
1850 goto err_free_irq;
1851
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001852 error = input_register_device(input_dev);
1853 if (error)
1854 goto err_free_irq;
1855
Iiro Valkonen7686b102011-02-02 23:21:58 -08001856 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001857 if (error)
1858 goto err_unregister_device;
1859
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301860#if defined(CONFIG_HAS_EARLYSUSPEND)
1861 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1862 MXT_SUSPEND_LEVEL;
1863 data->early_suspend.suspend = mxt_early_suspend;
1864 data->early_suspend.resume = mxt_late_resume;
1865 register_early_suspend(&data->early_suspend);
1866#endif
1867
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001868 return 0;
1869
1870err_unregister_device:
1871 input_unregister_device(input_dev);
1872 input_dev = NULL;
1873err_free_irq:
1874 free_irq(client->irq, data);
Jing Lin32c72532011-11-03 12:02:33 -07001875err_free_object:
1876 kfree(data->object_table);
Amy Maloche08266db2011-11-04 11:07:16 -07001877err_reset_gpio_req:
1878 if (gpio_is_valid(pdata->reset_gpio))
1879 gpio_free(pdata->reset_gpio);
1880err_irq_gpio_req:
1881 if (gpio_is_valid(pdata->irq_gpio))
1882 gpio_free(pdata->irq_gpio);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301883err_power_on:
1884 if (pdata->power_on)
1885 pdata->power_on(false);
1886 else
1887 mxt_power_on(data, false);
1888err_regulator_on:
1889 if (pdata->init_hw)
1890 pdata->init_hw(false);
1891 else
1892 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001893err_free_mem:
1894 input_free_device(input_dev);
1895 kfree(data);
1896 return error;
1897}
1898
Iiro Valkonen7686b102011-02-02 23:21:58 -08001899static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001900{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001901 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001902
Iiro Valkonen7686b102011-02-02 23:21:58 -08001903 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001904 free_irq(data->irq, data);
1905 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301906#if defined(CONFIG_HAS_EARLYSUSPEND)
1907 unregister_early_suspend(&data->early_suspend);
1908#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301909
1910 if (data->pdata->power_on)
1911 data->pdata->power_on(false);
1912 else
1913 mxt_power_on(data, false);
1914
1915 if (data->pdata->init_hw)
1916 data->pdata->init_hw(false);
1917 else
1918 mxt_regulator_configure(data, false);
1919
Mohan Pallakabfe8f302012-01-02 18:32:08 +08001920 if (gpio_is_valid(data->pdata->reset_gpio))
1921 gpio_free(data->pdata->reset_gpio);
1922
1923 if (gpio_is_valid(data->pdata->irq_gpio))
1924 gpio_free(data->pdata->irq_gpio);
1925
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001926 kfree(data->object_table);
1927 kfree(data);
1928
1929 return 0;
1930}
1931
Iiro Valkonen7686b102011-02-02 23:21:58 -08001932static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001933 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001934 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001935 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001936 { }
1937};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001938MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001939
Iiro Valkonen7686b102011-02-02 23:21:58 -08001940static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001941 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001942 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001943 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001944#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001945 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001946#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001947 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001948 .probe = mxt_probe,
1949 .remove = __devexit_p(mxt_remove),
1950 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001951};
1952
Iiro Valkonen7686b102011-02-02 23:21:58 -08001953static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001954{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001955 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001956}
1957
Iiro Valkonen7686b102011-02-02 23:21:58 -08001958static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001959{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001960 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001961}
1962
Iiro Valkonen7686b102011-02-02 23:21:58 -08001963module_init(mxt_init);
1964module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001965
1966/* Module information */
1967MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001968MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001969MODULE_LICENSE("GPL");