blob: 4c1922290e23bbe597654b4ec5349a31bdf01f02 [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>
Jing Lin2f863172011-10-17 10:56:58 -07006 * Copyright (c) 2011, 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>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053024#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070025
Anirudh Ghayal253ce122011-08-09 19:32:57 +053026#if defined(CONFIG_HAS_EARLYSUSPEND)
27#include <linux/earlysuspend.h>
28/* Early-suspend level */
29#define MXT_SUSPEND_LEVEL 1
30#endif
31
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070032/* Family ID */
33#define MXT224_ID 0x80
34#define MXT1386_ID 0xA0
35
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070036/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_VER_20 20
38#define MXT_VER_21 21
39#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070040
41/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080042#define MXT_APP_LOW 0x4a
43#define MXT_APP_HIGH 0x4b
44#define MXT_BOOT_LOW 0x24
45#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070046
47/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080048#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
50/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080051#define MXT_FAMILY_ID 0x00
52#define MXT_VARIANT_ID 0x01
53#define MXT_VERSION 0x02
54#define MXT_BUILD 0x03
55#define MXT_MATRIX_X_SIZE 0x04
56#define MXT_MATRIX_Y_SIZE 0x05
57#define MXT_OBJECT_NUM 0x06
58#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070059
Iiro Valkonen7686b102011-02-02 23:21:58 -080060#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070061
62/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080063#define MXT_DEBUG_DIAGNOSTIC 37
64#define MXT_GEN_MESSAGE 5
65#define MXT_GEN_COMMAND 6
66#define MXT_GEN_POWER 7
67#define MXT_GEN_ACQUIRE 8
68#define MXT_TOUCH_MULTI 9
69#define MXT_TOUCH_KEYARRAY 15
70#define MXT_TOUCH_PROXIMITY 23
71#define MXT_PROCI_GRIPFACE 20
72#define MXT_PROCG_NOISE 22
73#define MXT_PROCI_ONETOUCH 24
74#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070075#define MXT_PROCI_GRIP 40
76#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070077#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080078#define MXT_SPT_GPIOPWM 19
79#define MXT_SPT_SELFTEST 25
80#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070081#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070082#define MXT_SPT_DIGITIZER 43
83#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070084
Iiro Valkonen7686b102011-02-02 23:21:58 -080085/* MXT_GEN_COMMAND field */
86#define MXT_COMMAND_RESET 0
87#define MXT_COMMAND_BACKUPNV 1
88#define MXT_COMMAND_CALIBRATE 2
89#define MXT_COMMAND_REPORTALL 3
90#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen7686b102011-02-02 23:21:58 -080092/* MXT_GEN_POWER field */
93#define MXT_POWER_IDLEACQINT 0
94#define MXT_POWER_ACTVACQINT 1
95#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070096
Iiro Valkonen7686b102011-02-02 23:21:58 -080097/* MXT_GEN_ACQUIRE field */
98#define MXT_ACQUIRE_CHRGTIME 0
99#define MXT_ACQUIRE_TCHDRIFT 2
100#define MXT_ACQUIRE_DRIFTST 3
101#define MXT_ACQUIRE_TCHAUTOCAL 4
102#define MXT_ACQUIRE_SYNC 5
103#define MXT_ACQUIRE_ATCHCALST 6
104#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700105
Iiro Valkonen7686b102011-02-02 23:21:58 -0800106/* MXT_TOUCH_MULTI field */
107#define MXT_TOUCH_CTRL 0
108#define MXT_TOUCH_XORIGIN 1
109#define MXT_TOUCH_YORIGIN 2
110#define MXT_TOUCH_XSIZE 3
111#define MXT_TOUCH_YSIZE 4
112#define MXT_TOUCH_BLEN 6
113#define MXT_TOUCH_TCHTHR 7
114#define MXT_TOUCH_TCHDI 8
115#define MXT_TOUCH_ORIENT 9
116#define MXT_TOUCH_MOVHYSTI 11
117#define MXT_TOUCH_MOVHYSTN 12
118#define MXT_TOUCH_NUMTOUCH 14
119#define MXT_TOUCH_MRGHYST 15
120#define MXT_TOUCH_MRGTHR 16
121#define MXT_TOUCH_AMPHYST 17
122#define MXT_TOUCH_XRANGE_LSB 18
123#define MXT_TOUCH_XRANGE_MSB 19
124#define MXT_TOUCH_YRANGE_LSB 20
125#define MXT_TOUCH_YRANGE_MSB 21
126#define MXT_TOUCH_XLOCLIP 22
127#define MXT_TOUCH_XHICLIP 23
128#define MXT_TOUCH_YLOCLIP 24
129#define MXT_TOUCH_YHICLIP 25
130#define MXT_TOUCH_XEDGECTRL 26
131#define MXT_TOUCH_XEDGEDIST 27
132#define MXT_TOUCH_YEDGECTRL 28
133#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700134#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700135
Iiro Valkonen7686b102011-02-02 23:21:58 -0800136/* MXT_PROCI_GRIPFACE field */
137#define MXT_GRIPFACE_CTRL 0
138#define MXT_GRIPFACE_XLOGRIP 1
139#define MXT_GRIPFACE_XHIGRIP 2
140#define MXT_GRIPFACE_YLOGRIP 3
141#define MXT_GRIPFACE_YHIGRIP 4
142#define MXT_GRIPFACE_MAXTCHS 5
143#define MXT_GRIPFACE_SZTHR1 7
144#define MXT_GRIPFACE_SZTHR2 8
145#define MXT_GRIPFACE_SHPTHR1 9
146#define MXT_GRIPFACE_SHPTHR2 10
147#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700148
Iiro Valkonen7686b102011-02-02 23:21:58 -0800149/* MXT_PROCI_NOISE field */
150#define MXT_NOISE_CTRL 0
151#define MXT_NOISE_OUTFLEN 1
152#define MXT_NOISE_GCAFUL_LSB 3
153#define MXT_NOISE_GCAFUL_MSB 4
154#define MXT_NOISE_GCAFLL_LSB 5
155#define MXT_NOISE_GCAFLL_MSB 6
156#define MXT_NOISE_ACTVGCAFVALID 7
157#define MXT_NOISE_NOISETHR 8
158#define MXT_NOISE_FREQHOPSCALE 10
159#define MXT_NOISE_FREQ0 11
160#define MXT_NOISE_FREQ1 12
161#define MXT_NOISE_FREQ2 13
162#define MXT_NOISE_FREQ3 14
163#define MXT_NOISE_FREQ4 15
164#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700165
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166/* MXT_SPT_COMMSCONFIG */
167#define MXT_COMMS_CTRL 0
168#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700169
Iiro Valkonen7686b102011-02-02 23:21:58 -0800170/* MXT_SPT_CTECONFIG field */
171#define MXT_CTE_CTRL 0
172#define MXT_CTE_CMD 1
173#define MXT_CTE_MODE 2
174#define MXT_CTE_IDLEGCAFDEPTH 3
175#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700176#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700177
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_VOLTAGE_DEFAULT 2700000
179#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700180
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530181#define MXT_VTG_MIN_UV 2700000
182#define MXT_VTG_MAX_UV 3300000
183#define MXT_ACTIVE_LOAD_UA 15000
Jing Linbace50b2011-10-18 22:55:47 -0700184#define MXT_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530185
186#define MXT_I2C_VTG_MIN_UV 1800000
187#define MXT_I2C_VTG_MAX_UV 1800000
188#define MXT_I2C_LOAD_UA 10000
Jing Linbace50b2011-10-18 22:55:47 -0700189#define MXT_I2C_LPM_LOAD_UA 10
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530190
Iiro Valkonen7686b102011-02-02 23:21:58 -0800191/* Define for MXT_GEN_COMMAND */
192#define MXT_BOOT_VALUE 0xa5
193#define MXT_BACKUP_VALUE 0x55
194#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700195#define MXT224_RESET_TIME 65 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700196#define MXT1386_RESET_TIME 250 /* msec */
197#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700198#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700201
Jing Lin36aee812011-10-17 17:17:28 -0700202#define MXT_WAKE_TIME 25
203
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700204/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800205#define MXT_UNLOCK_CMD_MSB 0xaa
206#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700207
208/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800209#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
210#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
211#define MXT_FRAME_CRC_CHECK 0x02
212#define MXT_FRAME_CRC_FAIL 0x03
213#define MXT_FRAME_CRC_PASS 0x04
214#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
215#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700216
217/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218#define MXT_SUPPRESS (1 << 1)
219#define MXT_AMP (1 << 2)
220#define MXT_VECTOR (1 << 3)
221#define MXT_MOVE (1 << 4)
222#define MXT_RELEASE (1 << 5)
223#define MXT_PRESS (1 << 6)
224#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700225
Joonyoung Shim910d8052011-04-12 23:14:38 -0700226/* Touch orient bits */
227#define MXT_XY_SWITCH (1 << 0)
228#define MXT_X_INVERT (1 << 1)
229#define MXT_Y_INVERT (1 << 2)
230
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800232#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233
Iiro Valkonen7686b102011-02-02 23:21:58 -0800234#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235
Jing Lin36aee812011-10-17 17:17:28 -0700236#define T7_DATA_SIZE 3
237#define MXT_MAX_RW_TRIES 3
238#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530239
Iiro Valkonen7686b102011-02-02 23:21:58 -0800240struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700241 u8 family_id;
242 u8 variant_id;
243 u8 version;
244 u8 build;
245 u8 matrix_xsize;
246 u8 matrix_ysize;
247 u8 object_num;
248};
249
Iiro Valkonen7686b102011-02-02 23:21:58 -0800250struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700251 u8 type;
252 u16 start_address;
253 u8 size;
254 u8 instances;
255 u8 num_report_ids;
256
257 /* to map object and message */
258 u8 max_reportid;
259};
260
Iiro Valkonen7686b102011-02-02 23:21:58 -0800261struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700262 u8 reportid;
263 u8 message[7];
264 u8 checksum;
265};
266
Iiro Valkonen7686b102011-02-02 23:21:58 -0800267struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700268 int status;
269 int x;
270 int y;
271 int area;
272};
273
274/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800275struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700276 struct i2c_client *client;
277 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800278 const struct mxt_platform_data *pdata;
279 struct mxt_object *object_table;
280 struct mxt_info info;
281 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700282 unsigned int irq;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530283 struct regulator *vcc;
284 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530285#if defined(CONFIG_HAS_EARLYSUSPEND)
286 struct early_suspend early_suspend;
287#endif
Jing Lin36aee812011-10-17 17:17:28 -0700288
Amy Maloche52262212011-09-15 16:46:57 -0700289 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700290 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700291 u8 t9_ctrl;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700292};
293
Iiro Valkonen7686b102011-02-02 23:21:58 -0800294static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700295{
296 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800297 case MXT_GEN_MESSAGE:
298 case MXT_GEN_COMMAND:
299 case MXT_GEN_POWER:
300 case MXT_GEN_ACQUIRE:
301 case MXT_TOUCH_MULTI:
302 case MXT_TOUCH_KEYARRAY:
303 case MXT_TOUCH_PROXIMITY:
304 case MXT_PROCI_GRIPFACE:
305 case MXT_PROCG_NOISE:
306 case MXT_PROCI_ONETOUCH:
307 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700308 case MXT_PROCI_GRIP:
309 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800310 case MXT_SPT_COMMSCONFIG:
311 case MXT_SPT_GPIOPWM:
312 case MXT_SPT_SELFTEST:
313 case MXT_SPT_CTECONFIG:
314 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530315 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700316 return true;
317 default:
318 return false;
319 }
320}
321
Iiro Valkonen7686b102011-02-02 23:21:58 -0800322static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700323{
324 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800325 case MXT_GEN_COMMAND:
326 case MXT_GEN_POWER:
327 case MXT_GEN_ACQUIRE:
328 case MXT_TOUCH_MULTI:
329 case MXT_TOUCH_KEYARRAY:
330 case MXT_TOUCH_PROXIMITY:
331 case MXT_PROCI_GRIPFACE:
332 case MXT_PROCG_NOISE:
333 case MXT_PROCI_ONETOUCH:
334 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700335 case MXT_PROCI_GRIP:
336 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800337 case MXT_SPT_GPIOPWM:
338 case MXT_SPT_SELFTEST:
339 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530340 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530341 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700342 return true;
343 default:
344 return false;
345 }
346}
347
Iiro Valkonen7686b102011-02-02 23:21:58 -0800348static void mxt_dump_message(struct device *dev,
349 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700350{
351 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
352 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
353 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
354 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
355 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
356 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
357 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
358 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
359 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
360}
361
Iiro Valkonen7686b102011-02-02 23:21:58 -0800362static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700363 unsigned int state)
364{
365 u8 val;
366
367recheck:
368 if (i2c_master_recv(client, &val, 1) != 1) {
369 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
370 return -EIO;
371 }
372
373 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800374 case MXT_WAITING_BOOTLOAD_CMD:
375 case MXT_WAITING_FRAME_DATA:
376 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700377 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800378 case MXT_FRAME_CRC_PASS:
379 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700380 goto recheck;
381 break;
382 default:
383 return -EINVAL;
384 }
385
386 if (val != state) {
387 dev_err(&client->dev, "Unvalid bootloader mode state\n");
388 return -EINVAL;
389 }
390
391 return 0;
392}
393
Iiro Valkonen7686b102011-02-02 23:21:58 -0800394static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700395{
396 u8 buf[2];
397
Iiro Valkonen7686b102011-02-02 23:21:58 -0800398 buf[0] = MXT_UNLOCK_CMD_LSB;
399 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700400
401 if (i2c_master_send(client, buf, 2) != 2) {
402 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
403 return -EIO;
404 }
405
406 return 0;
407}
408
Iiro Valkonen7686b102011-02-02 23:21:58 -0800409static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700410 const u8 *data, unsigned int frame_size)
411{
412 if (i2c_master_send(client, data, frame_size) != frame_size) {
413 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
414 return -EIO;
415 }
416
417 return 0;
418}
419
Iiro Valkonen7686b102011-02-02 23:21:58 -0800420static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700421 u16 reg, u16 len, void *val)
422{
423 struct i2c_msg xfer[2];
424 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700425 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700426
427 buf[0] = reg & 0xff;
428 buf[1] = (reg >> 8) & 0xff;
429
430 /* Write register */
431 xfer[0].addr = client->addr;
432 xfer[0].flags = 0;
433 xfer[0].len = 2;
434 xfer[0].buf = buf;
435
436 /* Read data */
437 xfer[1].addr = client->addr;
438 xfer[1].flags = I2C_M_RD;
439 xfer[1].len = len;
440 xfer[1].buf = val;
441
Jing Lin36aee812011-10-17 17:17:28 -0700442 do {
443 if (i2c_transfer(client->adapter, xfer, 2) == 2)
444 return 0;
445 msleep(MXT_WAKE_TIME);
446 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700447
Jing Lin36aee812011-10-17 17:17:28 -0700448 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
449 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700450}
451
Iiro Valkonen7686b102011-02-02 23:21:58 -0800452static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700453{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800454 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700455}
456
Jing Lin36aee812011-10-17 17:17:28 -0700457static int __mxt_write_reg(struct i2c_client *client,
458 u16 addr, u16 length, u8 *value)
459{
460 u8 buf[MXT_BLOCK_SIZE + 2];
461 int i, tries = 0;
462
463 if (length > MXT_BLOCK_SIZE)
464 return -EINVAL;
465
466 buf[0] = addr & 0xff;
467 buf[1] = (addr >> 8) & 0xff;
468 for (i = 0; i < length; i++)
469 buf[i + 2] = *value++;
470
471 do {
472 if (i2c_master_send(client, buf, length + 2) == (length + 2))
473 return 0;
474 msleep(MXT_WAKE_TIME);
475 } while (++tries < MXT_MAX_RW_TRIES);
476
477 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
478 return -EIO;
479}
480
Iiro Valkonen7686b102011-02-02 23:21:58 -0800481static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482{
Jing Lin36aee812011-10-17 17:17:28 -0700483 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700484}
485
Iiro Valkonen7686b102011-02-02 23:21:58 -0800486static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700487 u16 reg, u8 *object_buf)
488{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800489 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700490 object_buf);
491}
492
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493static struct mxt_object *
494mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800496 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497 int i;
498
499 for (i = 0; i < data->info.object_num; i++) {
500 object = data->object_table + i;
501 if (object->type == type)
502 return object;
503 }
504
505 dev_err(&data->client->dev, "Invalid object type\n");
506 return NULL;
507}
508
Iiro Valkonen7686b102011-02-02 23:21:58 -0800509static int mxt_read_message(struct mxt_data *data,
510 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800512 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700513 u16 reg;
514
Iiro Valkonen7686b102011-02-02 23:21:58 -0800515 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700516 if (!object)
517 return -EINVAL;
518
519 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800520 return __mxt_read_reg(data->client, reg,
521 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522}
523
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525 u8 type, u8 offset, u8 *val)
526{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528 u16 reg;
529
Iiro Valkonen7686b102011-02-02 23:21:58 -0800530 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700531 if (!object)
532 return -EINVAL;
533
534 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800535 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536}
537
Iiro Valkonen7686b102011-02-02 23:21:58 -0800538static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700539 u8 type, u8 offset, u8 val)
540{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800541 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542 u16 reg;
543
Iiro Valkonen7686b102011-02-02 23:21:58 -0800544 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545 if (!object)
546 return -EINVAL;
547
548 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800549 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550}
551
Iiro Valkonen7686b102011-02-02 23:21:58 -0800552static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700553{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800554 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700555 struct input_dev *input_dev = data->input_dev;
556 int status = finger[single_id].status;
557 int finger_num = 0;
558 int id;
559
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561 if (!finger[id].status)
562 continue;
563
Amy Maloche2b59bab2011-10-13 16:08:16 -0700564 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
565 finger[id].status != MXT_RELEASE ?
566 finger[id].area : 0);
567 input_report_abs(input_dev, ABS_MT_POSITION_X,
568 finger[id].x);
569 input_report_abs(input_dev, ABS_MT_POSITION_Y,
570 finger[id].y);
571 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572
Amy Maloche2b59bab2011-10-13 16:08:16 -0700573 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700574 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700575 else
576 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 }
578
579 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
580
Iiro Valkonen7686b102011-02-02 23:21:58 -0800581 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 input_report_abs(input_dev, ABS_X, finger[single_id].x);
583 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
584 }
585
586 input_sync(input_dev);
587}
588
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589static void mxt_input_touchevent(struct mxt_data *data,
590 struct mxt_message *message, int 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 device *dev = &data->client->dev;
594 u8 status = message->message[0];
595 int x;
596 int y;
597 int area;
598
599 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800600 if (!(status & MXT_DETECT)) {
601 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700602 dev_dbg(dev, "[%d] released\n", id);
603
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604 finger[id].status = MXT_RELEASE;
605 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 }
607 return;
608 }
609
610 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800611 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700612 return;
613
Joonyoung Shim910d8052011-04-12 23:14:38 -0700614 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
615 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700616 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700617 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700618 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700619 y = y >> 2;
620
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700621 area = message->message[4];
622
623 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800624 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625 x, y, area);
626
Iiro Valkonen7686b102011-02-02 23:21:58 -0800627 finger[id].status = status & MXT_MOVE ?
628 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700629 finger[id].x = x;
630 finger[id].y = y;
631 finger[id].area = area;
632
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634}
635
Iiro Valkonen7686b102011-02-02 23:21:58 -0800636static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800638 struct mxt_data *data = dev_id;
639 struct mxt_message message;
640 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641 struct device *dev = &data->client->dev;
642 int id;
643 u8 reportid;
644 u8 max_reportid;
645 u8 min_reportid;
646
647 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800648 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700649 dev_err(dev, "Failed to read message\n");
650 goto end;
651 }
652
653 reportid = message.reportid;
654
Iiro Valkonen7686b102011-02-02 23:21:58 -0800655 /* whether reportid is thing of MXT_TOUCH_MULTI */
656 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700657 if (!object)
658 goto end;
659
660 max_reportid = object->max_reportid;
661 min_reportid = max_reportid - object->num_report_ids + 1;
662 id = reportid - min_reportid;
663
664 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800667 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 } while (reportid != 0xff);
669
670end:
671 return IRQ_HANDLED;
672}
673
Iiro Valkonen7686b102011-02-02 23:21:58 -0800674static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800676 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800677 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700678 struct device *dev = &data->client->dev;
679 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800680 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700681
Iiro Valkonen71749f52011-02-15 13:36:52 -0800682 if (!pdata->config) {
683 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
684 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700685 }
686
687 for (i = 0; i < data->info.object_num; i++) {
688 object = data->object_table + i;
689
Iiro Valkonen7686b102011-02-02 23:21:58 -0800690 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691 continue;
692
Iiro Valkonen71749f52011-02-15 13:36:52 -0800693 for (j = 0; j < object->size + 1; j++) {
694 config_offset = index + j;
695 if (config_offset > pdata->config_length) {
696 dev_err(dev, "Not enough config data!\n");
697 return -EINVAL;
698 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800699 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800700 pdata->config[config_offset]);
701 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702 index += object->size + 1;
703 }
704
705 return 0;
706}
707
Iiro Valkonen7686b102011-02-02 23:21:58 -0800708static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700709{
710 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800711 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700712 int count = 10;
713 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714
715 /* Read dummy message to make high CHG pin */
716 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800717 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718 if (error)
719 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800720 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721
722 if (!count) {
723 dev_err(dev, "CHG pin isn't cleared\n");
724 return -EBUSY;
725 }
726
727 return 0;
728}
729
Iiro Valkonen7686b102011-02-02 23:21:58 -0800730static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731{
732 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800733 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 int error;
735 u8 val;
736
Iiro Valkonen7686b102011-02-02 23:21:58 -0800737 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700738 if (error)
739 return error;
740 info->family_id = val;
741
Iiro Valkonen7686b102011-02-02 23:21:58 -0800742 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743 if (error)
744 return error;
745 info->variant_id = val;
746
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748 if (error)
749 return error;
750 info->version = val;
751
Iiro Valkonen7686b102011-02-02 23:21:58 -0800752 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700753 if (error)
754 return error;
755 info->build = val;
756
Iiro Valkonen7686b102011-02-02 23:21:58 -0800757 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758 if (error)
759 return error;
760 info->object_num = val;
761
762 return 0;
763}
764
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766{
767 int error;
768 int i;
769 u16 reg;
770 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800771 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700772
773 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800774 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700775
Iiro Valkonen7686b102011-02-02 23:21:58 -0800776 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
777 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700778 if (error)
779 return error;
780
781 object->type = buf[0];
782 object->start_address = (buf[2] << 8) | buf[1];
783 object->size = buf[3];
784 object->instances = buf[4];
785 object->num_report_ids = buf[5];
786
787 if (object->num_report_ids) {
788 reportid += object->num_report_ids *
789 (object->instances + 1);
790 object->max_reportid = reportid;
791 }
792 }
793
794 return 0;
795}
796
Amy Maloche7e447432011-09-14 11:36:30 -0700797static void mxt_reset_delay(struct mxt_data *data)
798{
799 struct mxt_info *info = &data->info;
800
801 switch (info->family_id) {
802 case MXT224_ID:
803 msleep(MXT224_RESET_TIME);
804 break;
805 case MXT1386_ID:
806 msleep(MXT1386_RESET_TIME);
807 break;
808 default:
809 msleep(MXT_RESET_TIME);
810 }
811}
812
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814{
815 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800816 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700817 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700818 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700819 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700820 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700821 struct mxt_object *t7_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700822
Iiro Valkonen7686b102011-02-02 23:21:58 -0800823 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824 if (error)
825 return error;
826
827 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800828 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700829 GFP_KERNEL);
830 if (!data->object_table) {
831 dev_err(&client->dev, "Failed to allocate memory\n");
832 return -ENOMEM;
833 }
834
835 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800836 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700837 if (error)
838 return error;
839
840 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800841 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842 if (error)
843 return error;
844
Amy Maloche52262212011-09-15 16:46:57 -0700845 /* Store T7 and T9 locally, used in suspend/resume operations */
Jing Lin36aee812011-10-17 17:17:28 -0700846 t7_object = mxt_get_object(data, MXT_GEN_POWER);
847 if (!t7_object) {
848 dev_err(&client->dev, "Failed to get T7 object\n");
849 return -EINVAL;
850 }
851
852 data->t7_start_addr = t7_object->start_address;
853 error = __mxt_read_reg(client, data->t7_start_addr,
854 T7_DATA_SIZE, data->t7_data);
855 if (error < 0) {
856 dev_err(&client->dev,
857 "failed to save current power state\n");
858 return error;
Amy Maloche52262212011-09-15 16:46:57 -0700859 }
860 error = mxt_read_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL,
861 &data->t9_ctrl);
862 if (error < 0) {
863 dev_err(&client->dev, "failed to save current touch object\n");
864 return error;
865 }
866
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700867 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800868 mxt_write_object(data, MXT_GEN_COMMAND,
869 MXT_COMMAND_BACKUPNV,
870 MXT_BACKUP_VALUE);
871 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700872 do {
873 error = mxt_read_object(data, MXT_GEN_COMMAND,
874 MXT_COMMAND_BACKUPNV,
875 &command_register);
876 if (error)
877 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700878 usleep_range(1000, 2000);
879 } while ((command_register != 0) && (++timeout_counter <= 100));
880 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700881 dev_err(&client->dev, "No response after backup!\n");
882 return -EIO;
883 }
884
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700885
886 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800887 mxt_write_object(data, MXT_GEN_COMMAND,
888 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700889
Amy Maloche7e447432011-09-14 11:36:30 -0700890 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700891
892 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800893 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700894 if (error)
895 return error;
896 info->matrix_xsize = val;
897
Iiro Valkonen7686b102011-02-02 23:21:58 -0800898 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700899 if (error)
900 return error;
901 info->matrix_ysize = val;
902
903 dev_info(&client->dev,
904 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
905 info->family_id, info->variant_id, info->version,
906 info->build);
907
908 dev_info(&client->dev,
909 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
910 info->matrix_xsize, info->matrix_ysize,
911 info->object_num);
912
913 return 0;
914}
915
Iiro Valkonen7686b102011-02-02 23:21:58 -0800916static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700917 struct device_attribute *attr, char *buf)
918{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800919 struct mxt_data *data = dev_get_drvdata(dev);
920 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700921 int count = 0;
922 int i, j;
923 int error;
924 u8 val;
925
926 for (i = 0; i < data->info.object_num; i++) {
927 object = data->object_table + i;
928
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700929 count += snprintf(buf + count, PAGE_SIZE - count,
930 "Object[%d] (Type %d)\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931 i + 1, object->type);
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700932 if (count >= PAGE_SIZE)
933 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700934
Iiro Valkonen7686b102011-02-02 23:21:58 -0800935 if (!mxt_object_readable(object->type)) {
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700936 count += snprintf(buf + count, PAGE_SIZE - count,
937 "\n");
938 if (count >= PAGE_SIZE)
939 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700940 continue;
941 }
942
943 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800944 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700945 object->type, j, &val);
946 if (error)
947 return error;
948
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700949 count += snprintf(buf + count, PAGE_SIZE - count,
950 "\t[%2d]: %02x (%d)\n", j, val, val);
951 if (count >= PAGE_SIZE)
952 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953 }
954
Daniel Kurtz4ef11a82011-11-02 10:43:08 -0700955 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
956 if (count >= PAGE_SIZE)
957 return PAGE_SIZE - 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958 }
959
960 return count;
961}
962
Iiro Valkonen7686b102011-02-02 23:21:58 -0800963static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700964{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800965 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700966 struct i2c_client *client = data->client;
967 const struct firmware *fw = NULL;
968 unsigned int frame_size;
969 unsigned int pos = 0;
970 int ret;
971
972 ret = request_firmware(&fw, fn, dev);
973 if (ret) {
974 dev_err(dev, "Unable to open firmware %s\n", fn);
975 return ret;
976 }
977
978 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800979 mxt_write_object(data, MXT_GEN_COMMAND,
980 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700981
982 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983
984 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800985 if (client->addr == MXT_APP_LOW)
986 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700987 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800988 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700989
Iiro Valkonen7686b102011-02-02 23:21:58 -0800990 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991 if (ret)
992 goto out;
993
994 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996
997 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800998 ret = mxt_check_bootloader(client,
999 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000 if (ret)
1001 goto out;
1002
1003 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1004
1005 /* We should add 2 at frame size as the the firmware data is not
1006 * included the CRC bytes.
1007 */
1008 frame_size += 2;
1009
1010 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001011 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012
Iiro Valkonen7686b102011-02-02 23:21:58 -08001013 ret = mxt_check_bootloader(client,
1014 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001015 if (ret)
1016 goto out;
1017
1018 pos += frame_size;
1019
1020 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1021 }
1022
1023out:
1024 release_firmware(fw);
1025
1026 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027 if (client->addr == MXT_BOOT_LOW)
1028 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001030 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001031
1032 return ret;
1033}
1034
Iiro Valkonen7686b102011-02-02 23:21:58 -08001035static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001036 struct device_attribute *attr,
1037 const char *buf, size_t count)
1038{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001039 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040 int error;
1041
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042 disable_irq(data->irq);
1043
Iiro Valkonen7686b102011-02-02 23:21:58 -08001044 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045 if (error) {
1046 dev_err(dev, "The firmware update failed(%d)\n", error);
1047 count = error;
1048 } else {
1049 dev_dbg(dev, "The firmware update succeeded\n");
1050
1051 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001052 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001053
1054 kfree(data->object_table);
1055 data->object_table = NULL;
1056
Iiro Valkonen7686b102011-02-02 23:21:58 -08001057 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001058 }
1059
1060 enable_irq(data->irq);
1061
Iiro Valkonen08960a02011-04-12 23:16:40 -07001062 error = mxt_make_highchg(data);
1063 if (error)
1064 return error;
1065
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001066 return count;
1067}
1068
Iiro Valkonen7686b102011-02-02 23:21:58 -08001069static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1070static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001071
Iiro Valkonen7686b102011-02-02 23:21:58 -08001072static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001073 &dev_attr_object.attr,
1074 &dev_attr_update_fw.attr,
1075 NULL
1076};
1077
Iiro Valkonen7686b102011-02-02 23:21:58 -08001078static const struct attribute_group mxt_attr_group = {
1079 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080};
1081
Amy Maloche52262212011-09-15 16:46:57 -07001082static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083{
Jing Lin36aee812011-10-17 17:17:28 -07001084 int error;
1085
Amy Maloche52262212011-09-15 16:46:57 -07001086 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001087 error = __mxt_write_reg(data->client, data->t7_start_addr,
1088 T7_DATA_SIZE, data->t7_data);
1089 if (error < 0) {
1090 dev_err(&data->client->dev,
1091 "failed to restore old power state\n");
1092 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001093 }
Jing Lin36aee812011-10-17 17:17:28 -07001094
Amy Maloche52262212011-09-15 16:46:57 -07001095 error = mxt_write_object(data,
1096 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1097 if (error < 0) {
1098 dev_err(&data->client->dev, "failed to restore touch\n");
1099 return error;
1100 }
1101
1102 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001103}
1104
Amy Maloche52262212011-09-15 16:46:57 -07001105static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001106{
Jing Lin36aee812011-10-17 17:17:28 -07001107 int error;
1108 u8 t7_data[T7_DATA_SIZE] = {0};
1109
1110 /* disable touch and configure deep sleep mode */
1111 error = mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
1112 if (error < 0) {
1113 dev_err(&data->client->dev, "failed to disable touch\n");
1114 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001115 }
1116
Jing Lin36aee812011-10-17 17:17:28 -07001117 error = __mxt_write_reg(data->client, data->t7_start_addr,
1118 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001119 if (error < 0) {
1120 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001121 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001122 return error;
1123 }
1124
1125 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001126}
1127
Iiro Valkonen7686b102011-02-02 23:21:58 -08001128static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001130 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001131 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132
Amy Maloche52262212011-09-15 16:46:57 -07001133 error = mxt_start(data);
1134 if (error < 0) {
1135 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1136 return error;
1137 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001138
1139 return 0;
1140}
1141
Iiro Valkonen7686b102011-02-02 23:21:58 -08001142static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001144 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001145 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146
Amy Maloche52262212011-09-15 16:46:57 -07001147 error = mxt_stop(data);
1148 if (error < 0)
1149 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1150
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001151}
1152
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301153static int mxt_power_on(struct mxt_data *data, bool on)
1154{
1155 int rc;
1156
1157 if (on == false)
1158 goto power_off;
1159
1160 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1161 if (rc < 0) {
1162 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1163 rc);
1164 return rc;
1165 }
1166
1167 rc = regulator_enable(data->vcc);
1168 if (rc) {
1169 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1170 rc);
1171 goto error_reg_en_vcc;
1172 }
1173
1174 if (data->pdata->i2c_pull_up) {
1175 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1176 if (rc < 0) {
1177 dev_err(&data->client->dev,
1178 "Regulator set_opt failed rc=%d\n", rc);
1179 goto error_reg_opt_i2c;
1180 }
1181
1182 rc = regulator_enable(data->vcc_i2c);
1183 if (rc) {
1184 dev_err(&data->client->dev,
1185 "Regulator enable failed rc=%d\n", rc);
1186 goto error_reg_en_vcc_i2c;
1187 }
1188 }
1189
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001190 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301191
1192 return 0;
1193
1194error_reg_en_vcc_i2c:
1195 if (data->pdata->i2c_pull_up)
1196 regulator_set_optimum_mode(data->vcc_i2c, 0);
1197error_reg_opt_i2c:
1198 regulator_disable(data->vcc);
1199error_reg_en_vcc:
1200 regulator_set_optimum_mode(data->vcc, 0);
1201 return rc;
1202
1203power_off:
1204 regulator_set_optimum_mode(data->vcc, 0);
1205 regulator_disable(data->vcc);
1206 if (data->pdata->i2c_pull_up) {
1207 regulator_set_optimum_mode(data->vcc_i2c, 0);
1208 regulator_disable(data->vcc_i2c);
1209 }
1210 msleep(50);
1211 return 0;
1212}
1213
1214static int mxt_regulator_configure(struct mxt_data *data, bool on)
1215{
1216 int rc;
1217
1218 if (on == false)
1219 goto hw_shutdown;
1220
1221 data->vcc = regulator_get(&data->client->dev, "vdd");
1222 if (IS_ERR(data->vcc)) {
1223 rc = PTR_ERR(data->vcc);
1224 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1225 rc);
1226 return rc;
1227 }
1228
1229 if (regulator_count_voltages(data->vcc) > 0) {
1230 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1231 MXT_VTG_MAX_UV);
1232 if (rc) {
1233 dev_err(&data->client->dev,
1234 "regulator set_vtg failed rc=%d\n", rc);
1235 goto error_set_vtg_vcc;
1236 }
1237 }
1238
1239 if (data->pdata->i2c_pull_up) {
1240 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1241 if (IS_ERR(data->vcc_i2c)) {
1242 rc = PTR_ERR(data->vcc_i2c);
1243 dev_err(&data->client->dev,
1244 "Regulator get failed rc=%d\n", rc);
1245 goto error_get_vtg_i2c;
1246 }
1247 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1248 rc = regulator_set_voltage(data->vcc_i2c,
1249 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1250 if (rc) {
1251 dev_err(&data->client->dev,
1252 "regulator set_vtg failed rc=%d\n", rc);
1253 goto error_set_vtg_i2c;
1254 }
1255 }
1256 }
1257
1258 return 0;
1259
1260error_set_vtg_i2c:
1261 regulator_put(data->vcc_i2c);
1262error_get_vtg_i2c:
1263 if (regulator_count_voltages(data->vcc) > 0)
1264 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1265error_set_vtg_vcc:
1266 regulator_put(data->vcc);
1267 return rc;
1268
1269hw_shutdown:
1270 if (regulator_count_voltages(data->vcc) > 0)
1271 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1272 regulator_put(data->vcc);
1273 if (data->pdata->i2c_pull_up) {
1274 if (regulator_count_voltages(data->vcc_i2c) > 0)
1275 regulator_set_voltage(data->vcc_i2c, 0,
1276 MXT_I2C_VTG_MAX_UV);
1277 regulator_put(data->vcc_i2c);
1278 }
1279 return 0;
1280}
1281
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301282#ifdef CONFIG_PM
Jing Linbace50b2011-10-18 22:55:47 -07001283static int mxt_regulator_lpm(struct mxt_data *data, bool on)
1284{
1285
1286 int rc;
1287
1288 if (on == false)
1289 goto regulator_hpm;
1290
1291 rc = regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1292 if (rc < 0) {
1293 dev_err(&data->client->dev,
1294 "Regulator set_opt failed rc=%d\n", rc);
1295 goto fail_regulator_lpm;
1296 }
1297
1298 if (data->pdata->i2c_pull_up) {
1299 rc = regulator_set_optimum_mode(data->vcc_i2c,
1300 MXT_I2C_LPM_LOAD_UA);
1301 if (rc < 0) {
1302 dev_err(&data->client->dev,
1303 "Regulator set_opt failed rc=%d\n", rc);
1304 goto fail_regulator_lpm;
1305 }
1306 }
1307
1308 return 0;
1309
1310regulator_hpm:
1311
1312 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1313 if (rc < 0) {
1314 dev_err(&data->client->dev,
1315 "Regulator set_opt failed rc=%d\n", rc);
1316 goto fail_regulator_hpm;
1317 }
1318
1319 if (data->pdata->i2c_pull_up) {
1320 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1321 if (rc < 0) {
1322 dev_err(&data->client->dev,
1323 "Regulator set_opt failed rc=%d\n", rc);
1324 goto fail_regulator_hpm;
1325 }
1326 }
1327
1328 return 0;
1329
1330fail_regulator_lpm:
1331 regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1332 if (data->pdata->i2c_pull_up)
1333 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1334
1335 return rc;
1336
1337fail_regulator_hpm:
1338 regulator_set_optimum_mode(data->vcc, MXT_LPM_LOAD_UA);
1339 if (data->pdata->i2c_pull_up)
1340 regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LPM_LOAD_UA);
1341
1342 return rc;
1343}
1344
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301345static int mxt_suspend(struct device *dev)
1346{
1347 struct i2c_client *client = to_i2c_client(dev);
1348 struct mxt_data *data = i2c_get_clientdata(client);
1349 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001350 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301351
1352 mutex_lock(&input_dev->mutex);
1353
Amy Maloche52262212011-09-15 16:46:57 -07001354 if (input_dev->users) {
1355 error = mxt_stop(data);
1356 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001357 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001358 mutex_unlock(&input_dev->mutex);
1359 return error;
1360 }
1361
1362 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301363
1364 mutex_unlock(&input_dev->mutex);
1365
Jing Linbace50b2011-10-18 22:55:47 -07001366 /* put regulators in low power mode */
1367 error = mxt_regulator_lpm(data, true);
1368 if (error < 0) {
1369 dev_err(dev, "failed to enter low power mode\n");
1370 return error;
1371 }
1372
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301373 return 0;
1374}
1375
1376static int mxt_resume(struct device *dev)
1377{
1378 struct i2c_client *client = to_i2c_client(dev);
1379 struct mxt_data *data = i2c_get_clientdata(client);
1380 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001381 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301382
Jing Linbace50b2011-10-18 22:55:47 -07001383 /* put regulators in high power mode */
1384 error = mxt_regulator_lpm(data, false);
1385 if (error < 0) {
1386 dev_err(dev, "failed to enter high power mode\n");
1387 return error;
1388 }
1389
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301390 mutex_lock(&input_dev->mutex);
1391
Amy Maloche52262212011-09-15 16:46:57 -07001392 if (input_dev->users) {
1393 error = mxt_start(data);
1394 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001395 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001396 mutex_unlock(&input_dev->mutex);
1397 return error;
1398 }
1399 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301400
1401 mutex_unlock(&input_dev->mutex);
1402
1403 return 0;
1404}
1405
1406#if defined(CONFIG_HAS_EARLYSUSPEND)
1407static void mxt_early_suspend(struct early_suspend *h)
1408{
1409 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1410
1411 mxt_suspend(&data->client->dev);
1412}
1413
1414static void mxt_late_resume(struct early_suspend *h)
1415{
1416 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1417
1418 mxt_resume(&data->client->dev);
1419}
1420#endif
1421
1422static const struct dev_pm_ops mxt_pm_ops = {
1423#ifndef CONFIG_HAS_EARLYSUSPEND
1424 .suspend = mxt_suspend,
1425 .resume = mxt_resume,
1426#endif
1427};
1428#endif
1429
Iiro Valkonen7686b102011-02-02 23:21:58 -08001430static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001431 const struct i2c_device_id *id)
1432{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001433 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001434 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001435 struct input_dev *input_dev;
1436 int error;
1437
Iiro Valkonen919ed892011-02-15 13:36:52 -08001438 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001439 return -EINVAL;
1440
Iiro Valkonen7686b102011-02-02 23:21:58 -08001441 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001442 input_dev = input_allocate_device();
1443 if (!data || !input_dev) {
1444 dev_err(&client->dev, "Failed to allocate memory\n");
1445 error = -ENOMEM;
1446 goto err_free_mem;
1447 }
1448
Iiro Valkonen7686b102011-02-02 23:21:58 -08001449 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001450 input_dev->id.bustype = BUS_I2C;
1451 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001452 input_dev->open = mxt_input_open;
1453 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001454
Joonyoung Shim910d8052011-04-12 23:14:38 -07001455 data->client = client;
1456 data->input_dev = input_dev;
1457 data->pdata = pdata;
1458 data->irq = client->irq;
1459
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001460 __set_bit(EV_ABS, input_dev->evbit);
1461 __set_bit(EV_KEY, input_dev->evbit);
1462 __set_bit(BTN_TOUCH, input_dev->keybit);
1463
1464 /* For single touch */
1465 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001466 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001467 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001468 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001469
1470 /* For multi touch */
1471 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001472 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001473 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001474 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001475 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001476 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001477
1478 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001479 i2c_set_clientdata(client, data);
1480
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301481 if (pdata->init_hw)
1482 error = pdata->init_hw(true);
1483 else
1484 error = mxt_regulator_configure(data, true);
1485 if (error) {
1486 dev_err(&client->dev, "Failed to intialize hardware\n");
1487 goto err_free_object;
1488 }
1489
1490 if (pdata->power_on)
1491 error = pdata->power_on(true);
1492 else
1493 error = mxt_power_on(data, true);
1494 if (error) {
1495 dev_err(&client->dev, "Failed to power on hardware\n");
1496 goto err_regulator_on;
1497 }
1498
Iiro Valkonen7686b102011-02-02 23:21:58 -08001499 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001500 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301501 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001502
Iiro Valkonen7686b102011-02-02 23:21:58 -08001503 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001504 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001505 if (error) {
1506 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301507 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001508 }
1509
Iiro Valkonen08960a02011-04-12 23:16:40 -07001510 error = mxt_make_highchg(data);
1511 if (error)
1512 goto err_free_irq;
1513
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001514 error = input_register_device(input_dev);
1515 if (error)
1516 goto err_free_irq;
1517
Iiro Valkonen7686b102011-02-02 23:21:58 -08001518 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001519 if (error)
1520 goto err_unregister_device;
1521
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301522#if defined(CONFIG_HAS_EARLYSUSPEND)
1523 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1524 MXT_SUSPEND_LEVEL;
1525 data->early_suspend.suspend = mxt_early_suspend;
1526 data->early_suspend.resume = mxt_late_resume;
1527 register_early_suspend(&data->early_suspend);
1528#endif
1529
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001530 return 0;
1531
1532err_unregister_device:
1533 input_unregister_device(input_dev);
1534 input_dev = NULL;
1535err_free_irq:
1536 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301537err_power_on:
1538 if (pdata->power_on)
1539 pdata->power_on(false);
1540 else
1541 mxt_power_on(data, false);
1542err_regulator_on:
1543 if (pdata->init_hw)
1544 pdata->init_hw(false);
1545 else
1546 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001547err_free_object:
1548 kfree(data->object_table);
1549err_free_mem:
1550 input_free_device(input_dev);
1551 kfree(data);
1552 return error;
1553}
1554
Iiro Valkonen7686b102011-02-02 23:21:58 -08001555static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001556{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001557 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001558
Iiro Valkonen7686b102011-02-02 23:21:58 -08001559 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001560 free_irq(data->irq, data);
1561 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301562#if defined(CONFIG_HAS_EARLYSUSPEND)
1563 unregister_early_suspend(&data->early_suspend);
1564#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301565
1566 if (data->pdata->power_on)
1567 data->pdata->power_on(false);
1568 else
1569 mxt_power_on(data, false);
1570
1571 if (data->pdata->init_hw)
1572 data->pdata->init_hw(false);
1573 else
1574 mxt_regulator_configure(data, false);
1575
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001576 kfree(data->object_table);
1577 kfree(data);
1578
1579 return 0;
1580}
1581
Iiro Valkonen7686b102011-02-02 23:21:58 -08001582static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001583 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001584 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001585 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001586 { }
1587};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001588MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001589
Iiro Valkonen7686b102011-02-02 23:21:58 -08001590static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001591 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001592 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001593 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001594#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001595 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001596#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001597 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001598 .probe = mxt_probe,
1599 .remove = __devexit_p(mxt_remove),
1600 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001601};
1602
Iiro Valkonen7686b102011-02-02 23:21:58 -08001603static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001604{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001605 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001606}
1607
Iiro Valkonen7686b102011-02-02 23:21:58 -08001608static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001609{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001610 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001611}
1612
Iiro Valkonen7686b102011-02-02 23:21:58 -08001613module_init(mxt_init);
1614module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001615
1616/* Module information */
1617MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001618MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001619MODULE_LICENSE("GPL");