blob: 158013a464893f606a5a89c25be41ef51c33a8fa [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
184
185#define MXT_I2C_VTG_MIN_UV 1800000
186#define MXT_I2C_VTG_MAX_UV 1800000
187#define MXT_I2C_LOAD_UA 10000
188
Iiro Valkonen7686b102011-02-02 23:21:58 -0800189/* Define for MXT_GEN_COMMAND */
190#define MXT_BOOT_VALUE 0xa5
191#define MXT_BACKUP_VALUE 0x55
192#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700193#define MXT224_RESET_TIME 65 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700194#define MXT1386_RESET_TIME 250 /* msec */
195#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700196#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700197
Iiro Valkonen7686b102011-02-02 23:21:58 -0800198#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
Jing Lin36aee812011-10-17 17:17:28 -0700200#define MXT_WAKE_TIME 25
201
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700202/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800203#define MXT_UNLOCK_CMD_MSB 0xaa
204#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700205
206/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800207#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
208#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
209#define MXT_FRAME_CRC_CHECK 0x02
210#define MXT_FRAME_CRC_FAIL 0x03
211#define MXT_FRAME_CRC_PASS 0x04
212#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
213#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700214
215/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800216#define MXT_SUPPRESS (1 << 1)
217#define MXT_AMP (1 << 2)
218#define MXT_VECTOR (1 << 3)
219#define MXT_MOVE (1 << 4)
220#define MXT_RELEASE (1 << 5)
221#define MXT_PRESS (1 << 6)
222#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700223
Joonyoung Shim910d8052011-04-12 23:14:38 -0700224/* Touch orient bits */
225#define MXT_XY_SWITCH (1 << 0)
226#define MXT_X_INVERT (1 << 1)
227#define MXT_Y_INVERT (1 << 2)
228
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700229/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800230#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231
Iiro Valkonen7686b102011-02-02 23:21:58 -0800232#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530234#define MXT_BUFF_SIZE 100
Jing Lin36aee812011-10-17 17:17:28 -0700235#define T7_DATA_SIZE 3
236#define MXT_MAX_RW_TRIES 3
237#define MXT_BLOCK_SIZE 256
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530238
Iiro Valkonen7686b102011-02-02 23:21:58 -0800239struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700240 u8 family_id;
241 u8 variant_id;
242 u8 version;
243 u8 build;
244 u8 matrix_xsize;
245 u8 matrix_ysize;
246 u8 object_num;
247};
248
Iiro Valkonen7686b102011-02-02 23:21:58 -0800249struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700250 u8 type;
251 u16 start_address;
252 u8 size;
253 u8 instances;
254 u8 num_report_ids;
255
256 /* to map object and message */
257 u8 max_reportid;
258};
259
Iiro Valkonen7686b102011-02-02 23:21:58 -0800260struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700261 u8 reportid;
262 u8 message[7];
263 u8 checksum;
264};
265
Iiro Valkonen7686b102011-02-02 23:21:58 -0800266struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700267 int status;
268 int x;
269 int y;
270 int area;
271};
272
273/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800274struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700275 struct i2c_client *client;
276 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800277 const struct mxt_platform_data *pdata;
278 struct mxt_object *object_table;
279 struct mxt_info info;
280 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700281 unsigned int irq;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530282 struct regulator *vcc;
283 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530284#if defined(CONFIG_HAS_EARLYSUSPEND)
285 struct early_suspend early_suspend;
286#endif
Jing Lin36aee812011-10-17 17:17:28 -0700287
Amy Maloche52262212011-09-15 16:46:57 -0700288 u8 t7_data[T7_DATA_SIZE];
Jing Lin36aee812011-10-17 17:17:28 -0700289 u16 t7_start_addr;
Amy Maloche52262212011-09-15 16:46:57 -0700290 u8 t9_ctrl;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700291};
292
Iiro Valkonen7686b102011-02-02 23:21:58 -0800293static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700294{
295 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800296 case MXT_GEN_MESSAGE:
297 case MXT_GEN_COMMAND:
298 case MXT_GEN_POWER:
299 case MXT_GEN_ACQUIRE:
300 case MXT_TOUCH_MULTI:
301 case MXT_TOUCH_KEYARRAY:
302 case MXT_TOUCH_PROXIMITY:
303 case MXT_PROCI_GRIPFACE:
304 case MXT_PROCG_NOISE:
305 case MXT_PROCI_ONETOUCH:
306 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700307 case MXT_PROCI_GRIP:
308 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800309 case MXT_SPT_COMMSCONFIG:
310 case MXT_SPT_GPIOPWM:
311 case MXT_SPT_SELFTEST:
312 case MXT_SPT_CTECONFIG:
313 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530314 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700315 return true;
316 default:
317 return false;
318 }
319}
320
Iiro Valkonen7686b102011-02-02 23:21:58 -0800321static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700322{
323 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800324 case MXT_GEN_COMMAND:
325 case MXT_GEN_POWER:
326 case MXT_GEN_ACQUIRE:
327 case MXT_TOUCH_MULTI:
328 case MXT_TOUCH_KEYARRAY:
329 case MXT_TOUCH_PROXIMITY:
330 case MXT_PROCI_GRIPFACE:
331 case MXT_PROCG_NOISE:
332 case MXT_PROCI_ONETOUCH:
333 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700334 case MXT_PROCI_GRIP:
335 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800336 case MXT_SPT_GPIOPWM:
337 case MXT_SPT_SELFTEST:
338 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530339 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530340 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700341 return true;
342 default:
343 return false;
344 }
345}
346
Iiro Valkonen7686b102011-02-02 23:21:58 -0800347static void mxt_dump_message(struct device *dev,
348 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700349{
350 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
351 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
352 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
353 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
354 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
355 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
356 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
357 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
358 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
359}
360
Iiro Valkonen7686b102011-02-02 23:21:58 -0800361static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700362 unsigned int state)
363{
364 u8 val;
365
366recheck:
367 if (i2c_master_recv(client, &val, 1) != 1) {
368 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
369 return -EIO;
370 }
371
372 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800373 case MXT_WAITING_BOOTLOAD_CMD:
374 case MXT_WAITING_FRAME_DATA:
375 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700376 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800377 case MXT_FRAME_CRC_PASS:
378 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700379 goto recheck;
380 break;
381 default:
382 return -EINVAL;
383 }
384
385 if (val != state) {
386 dev_err(&client->dev, "Unvalid bootloader mode state\n");
387 return -EINVAL;
388 }
389
390 return 0;
391}
392
Iiro Valkonen7686b102011-02-02 23:21:58 -0800393static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700394{
395 u8 buf[2];
396
Iiro Valkonen7686b102011-02-02 23:21:58 -0800397 buf[0] = MXT_UNLOCK_CMD_LSB;
398 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700399
400 if (i2c_master_send(client, buf, 2) != 2) {
401 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
402 return -EIO;
403 }
404
405 return 0;
406}
407
Iiro Valkonen7686b102011-02-02 23:21:58 -0800408static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700409 const u8 *data, unsigned int frame_size)
410{
411 if (i2c_master_send(client, data, frame_size) != frame_size) {
412 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
413 return -EIO;
414 }
415
416 return 0;
417}
418
Iiro Valkonen7686b102011-02-02 23:21:58 -0800419static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700420 u16 reg, u16 len, void *val)
421{
422 struct i2c_msg xfer[2];
423 u8 buf[2];
Jing Lin36aee812011-10-17 17:17:28 -0700424 int i = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700425
426 buf[0] = reg & 0xff;
427 buf[1] = (reg >> 8) & 0xff;
428
429 /* Write register */
430 xfer[0].addr = client->addr;
431 xfer[0].flags = 0;
432 xfer[0].len = 2;
433 xfer[0].buf = buf;
434
435 /* Read data */
436 xfer[1].addr = client->addr;
437 xfer[1].flags = I2C_M_RD;
438 xfer[1].len = len;
439 xfer[1].buf = val;
440
Jing Lin36aee812011-10-17 17:17:28 -0700441 do {
442 if (i2c_transfer(client->adapter, xfer, 2) == 2)
443 return 0;
444 msleep(MXT_WAKE_TIME);
445 } while (++i < MXT_MAX_RW_TRIES);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700446
Jing Lin36aee812011-10-17 17:17:28 -0700447 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
448 return -EIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700449}
450
Iiro Valkonen7686b102011-02-02 23:21:58 -0800451static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700452{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800453 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700454}
455
Jing Lin36aee812011-10-17 17:17:28 -0700456static int __mxt_write_reg(struct i2c_client *client,
457 u16 addr, u16 length, u8 *value)
458{
459 u8 buf[MXT_BLOCK_SIZE + 2];
460 int i, tries = 0;
461
462 if (length > MXT_BLOCK_SIZE)
463 return -EINVAL;
464
465 buf[0] = addr & 0xff;
466 buf[1] = (addr >> 8) & 0xff;
467 for (i = 0; i < length; i++)
468 buf[i + 2] = *value++;
469
470 do {
471 if (i2c_master_send(client, buf, length + 2) == (length + 2))
472 return 0;
473 msleep(MXT_WAKE_TIME);
474 } while (++tries < MXT_MAX_RW_TRIES);
475
476 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
477 return -EIO;
478}
479
Iiro Valkonen7686b102011-02-02 23:21:58 -0800480static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700481{
Jing Lin36aee812011-10-17 17:17:28 -0700482 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483}
484
Iiro Valkonen7686b102011-02-02 23:21:58 -0800485static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700486 u16 reg, u8 *object_buf)
487{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800488 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700489 object_buf);
490}
491
Iiro Valkonen7686b102011-02-02 23:21:58 -0800492static struct mxt_object *
493mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700494{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800495 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700496 int i;
497
498 for (i = 0; i < data->info.object_num; i++) {
499 object = data->object_table + i;
500 if (object->type == type)
501 return object;
502 }
503
504 dev_err(&data->client->dev, "Invalid object type\n");
505 return NULL;
506}
507
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508static int mxt_read_message(struct mxt_data *data,
509 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700510{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800511 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700512 u16 reg;
513
Iiro Valkonen7686b102011-02-02 23:21:58 -0800514 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700515 if (!object)
516 return -EINVAL;
517
518 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800519 return __mxt_read_reg(data->client, reg,
520 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700521}
522
Iiro Valkonen7686b102011-02-02 23:21:58 -0800523static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700524 u8 type, u8 offset, u8 *val)
525{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800526 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700527 u16 reg;
528
Iiro Valkonen7686b102011-02-02 23:21:58 -0800529 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530 if (!object)
531 return -EINVAL;
532
533 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800534 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700535}
536
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538 u8 type, u8 offset, u8 val)
539{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800540 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700541 u16 reg;
542
Iiro Valkonen7686b102011-02-02 23:21:58 -0800543 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544 if (!object)
545 return -EINVAL;
546
547 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800548 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700549}
550
Iiro Valkonen7686b102011-02-02 23:21:58 -0800551static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700552{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800553 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554 struct input_dev *input_dev = data->input_dev;
555 int status = finger[single_id].status;
556 int finger_num = 0;
557 int id;
558
Iiro Valkonen7686b102011-02-02 23:21:58 -0800559 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700560 if (!finger[id].status)
561 continue;
562
Amy Maloche2b59bab2011-10-13 16:08:16 -0700563 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
564 finger[id].status != MXT_RELEASE ?
565 finger[id].area : 0);
566 input_report_abs(input_dev, ABS_MT_POSITION_X,
567 finger[id].x);
568 input_report_abs(input_dev, ABS_MT_POSITION_Y,
569 finger[id].y);
570 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700571
Amy Maloche2b59bab2011-10-13 16:08:16 -0700572 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700573 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700574 else
575 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700576 }
577
578 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
579
Iiro Valkonen7686b102011-02-02 23:21:58 -0800580 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700581 input_report_abs(input_dev, ABS_X, finger[single_id].x);
582 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
583 }
584
585 input_sync(input_dev);
586}
587
Iiro Valkonen7686b102011-02-02 23:21:58 -0800588static void mxt_input_touchevent(struct mxt_data *data,
589 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800591 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592 struct device *dev = &data->client->dev;
593 u8 status = message->message[0];
594 int x;
595 int y;
596 int area;
597
598 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800599 if (!(status & MXT_DETECT)) {
600 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601 dev_dbg(dev, "[%d] released\n", id);
602
Iiro Valkonen7686b102011-02-02 23:21:58 -0800603 finger[id].status = MXT_RELEASE;
604 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700605 }
606 return;
607 }
608
609 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800610 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611 return;
612
Joonyoung Shim910d8052011-04-12 23:14:38 -0700613 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
614 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700615 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700616 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700617 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700618 y = y >> 2;
619
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700620 area = message->message[4];
621
622 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624 x, y, area);
625
Iiro Valkonen7686b102011-02-02 23:21:58 -0800626 finger[id].status = status & MXT_MOVE ?
627 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700628 finger[id].x = x;
629 finger[id].y = y;
630 finger[id].area = area;
631
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700633}
634
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800637 struct mxt_data *data = dev_id;
638 struct mxt_message message;
639 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700640 struct device *dev = &data->client->dev;
641 int id;
642 u8 reportid;
643 u8 max_reportid;
644 u8 min_reportid;
645
646 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800647 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700648 dev_err(dev, "Failed to read message\n");
649 goto end;
650 }
651
652 reportid = message.reportid;
653
Iiro Valkonen7686b102011-02-02 23:21:58 -0800654 /* whether reportid is thing of MXT_TOUCH_MULTI */
655 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656 if (!object)
657 goto end;
658
659 max_reportid = object->max_reportid;
660 min_reportid = max_reportid - object->num_report_ids + 1;
661 id = reportid - min_reportid;
662
663 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800664 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800666 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667 } while (reportid != 0xff);
668
669end:
670 return IRQ_HANDLED;
671}
672
Iiro Valkonen7686b102011-02-02 23:21:58 -0800673static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800675 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800676 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677 struct device *dev = &data->client->dev;
678 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800679 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680
Iiro Valkonen71749f52011-02-15 13:36:52 -0800681 if (!pdata->config) {
682 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
683 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684 }
685
686 for (i = 0; i < data->info.object_num; i++) {
687 object = data->object_table + i;
688
Iiro Valkonen7686b102011-02-02 23:21:58 -0800689 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700690 continue;
691
Iiro Valkonen71749f52011-02-15 13:36:52 -0800692 for (j = 0; j < object->size + 1; j++) {
693 config_offset = index + j;
694 if (config_offset > pdata->config_length) {
695 dev_err(dev, "Not enough config data!\n");
696 return -EINVAL;
697 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800699 pdata->config[config_offset]);
700 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701 index += object->size + 1;
702 }
703
704 return 0;
705}
706
Iiro Valkonen7686b102011-02-02 23:21:58 -0800707static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708{
709 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800710 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 int count = 10;
712 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700713
714 /* Read dummy message to make high CHG pin */
715 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800716 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717 if (error)
718 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800719 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720
721 if (!count) {
722 dev_err(dev, "CHG pin isn't cleared\n");
723 return -EBUSY;
724 }
725
726 return 0;
727}
728
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730{
731 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800732 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700733 int error;
734 u8 val;
735
Iiro Valkonen7686b102011-02-02 23:21:58 -0800736 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700737 if (error)
738 return error;
739 info->family_id = val;
740
Iiro Valkonen7686b102011-02-02 23:21:58 -0800741 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700742 if (error)
743 return error;
744 info->variant_id = val;
745
Iiro Valkonen7686b102011-02-02 23:21:58 -0800746 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747 if (error)
748 return error;
749 info->version = val;
750
Iiro Valkonen7686b102011-02-02 23:21:58 -0800751 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752 if (error)
753 return error;
754 info->build = val;
755
Iiro Valkonen7686b102011-02-02 23:21:58 -0800756 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700757 if (error)
758 return error;
759 info->object_num = val;
760
761 return 0;
762}
763
Iiro Valkonen7686b102011-02-02 23:21:58 -0800764static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700765{
766 int error;
767 int i;
768 u16 reg;
769 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771
772 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800773 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700774
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
776 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700777 if (error)
778 return error;
779
780 object->type = buf[0];
781 object->start_address = (buf[2] << 8) | buf[1];
782 object->size = buf[3];
783 object->instances = buf[4];
784 object->num_report_ids = buf[5];
785
786 if (object->num_report_ids) {
787 reportid += object->num_report_ids *
788 (object->instances + 1);
789 object->max_reportid = reportid;
790 }
791 }
792
793 return 0;
794}
795
Amy Maloche7e447432011-09-14 11:36:30 -0700796static void mxt_reset_delay(struct mxt_data *data)
797{
798 struct mxt_info *info = &data->info;
799
800 switch (info->family_id) {
801 case MXT224_ID:
802 msleep(MXT224_RESET_TIME);
803 break;
804 case MXT1386_ID:
805 msleep(MXT1386_RESET_TIME);
806 break;
807 default:
808 msleep(MXT_RESET_TIME);
809 }
810}
811
Iiro Valkonen7686b102011-02-02 23:21:58 -0800812static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700813{
814 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800815 struct mxt_info *info = &data->info;
Jing Lin36aee812011-10-17 17:17:28 -0700816 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700817 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700819 u8 command_register;
Jing Lin36aee812011-10-17 17:17:28 -0700820 struct mxt_object *t7_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700821
Iiro Valkonen7686b102011-02-02 23:21:58 -0800822 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700823 if (error)
824 return error;
825
826 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800827 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700828 GFP_KERNEL);
829 if (!data->object_table) {
830 dev_err(&client->dev, "Failed to allocate memory\n");
831 return -ENOMEM;
832 }
833
834 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800835 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700836 if (error)
837 return error;
838
839 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800840 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700841 if (error)
842 return error;
843
Amy Maloche52262212011-09-15 16:46:57 -0700844 /* Store T7 and T9 locally, used in suspend/resume operations */
Jing Lin36aee812011-10-17 17:17:28 -0700845 t7_object = mxt_get_object(data, MXT_GEN_POWER);
846 if (!t7_object) {
847 dev_err(&client->dev, "Failed to get T7 object\n");
848 return -EINVAL;
849 }
850
851 data->t7_start_addr = t7_object->start_address;
852 error = __mxt_read_reg(client, data->t7_start_addr,
853 T7_DATA_SIZE, data->t7_data);
854 if (error < 0) {
855 dev_err(&client->dev,
856 "failed to save current power state\n");
857 return error;
Amy Maloche52262212011-09-15 16:46:57 -0700858 }
859 error = mxt_read_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL,
860 &data->t9_ctrl);
861 if (error < 0) {
862 dev_err(&client->dev, "failed to save current touch object\n");
863 return error;
864 }
865
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700866 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800867 mxt_write_object(data, MXT_GEN_COMMAND,
868 MXT_COMMAND_BACKUPNV,
869 MXT_BACKUP_VALUE);
870 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700871 do {
872 error = mxt_read_object(data, MXT_GEN_COMMAND,
873 MXT_COMMAND_BACKUPNV,
874 &command_register);
875 if (error)
876 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700877 usleep_range(1000, 2000);
878 } while ((command_register != 0) && (++timeout_counter <= 100));
879 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700880 dev_err(&client->dev, "No response after backup!\n");
881 return -EIO;
882 }
883
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700884
885 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800886 mxt_write_object(data, MXT_GEN_COMMAND,
887 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700888
Amy Maloche7e447432011-09-14 11:36:30 -0700889 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700890
891 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800892 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893 if (error)
894 return error;
895 info->matrix_xsize = val;
896
Iiro Valkonen7686b102011-02-02 23:21:58 -0800897 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700898 if (error)
899 return error;
900 info->matrix_ysize = val;
901
902 dev_info(&client->dev,
903 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
904 info->family_id, info->variant_id, info->version,
905 info->build);
906
907 dev_info(&client->dev,
908 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
909 info->matrix_xsize, info->matrix_ysize,
910 info->object_num);
911
912 return 0;
913}
914
Iiro Valkonen7686b102011-02-02 23:21:58 -0800915static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700916 struct device_attribute *attr, char *buf)
917{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800918 struct mxt_data *data = dev_get_drvdata(dev);
919 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700920 int count = 0;
921 int i, j;
922 int error;
923 u8 val;
924
925 for (i = 0; i < data->info.object_num; i++) {
926 object = data->object_table + i;
927
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530928 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929 "Object Table Element %d(Type %d)\n",
930 i + 1, object->type);
931
Iiro Valkonen7686b102011-02-02 23:21:58 -0800932 if (!mxt_object_readable(object->type)) {
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530933 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700934 continue;
935 }
936
937 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800938 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700939 object->type, j, &val);
940 if (error)
941 return error;
942
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530943 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700944 " Byte %d: 0x%x (%d)\n", j, val, val);
945 }
946
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530947 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700948 }
949
950 return count;
951}
952
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956 struct i2c_client *client = data->client;
957 const struct firmware *fw = NULL;
958 unsigned int frame_size;
959 unsigned int pos = 0;
960 int ret;
961
962 ret = request_firmware(&fw, fn, dev);
963 if (ret) {
964 dev_err(dev, "Unable to open firmware %s\n", fn);
965 return ret;
966 }
967
968 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800969 mxt_write_object(data, MXT_GEN_COMMAND,
970 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700971
972 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700973
974 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800975 if (client->addr == MXT_APP_LOW)
976 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700977 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800978 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700979
Iiro Valkonen7686b102011-02-02 23:21:58 -0800980 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700981 if (ret)
982 goto out;
983
984 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800985 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700986
987 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800988 ret = mxt_check_bootloader(client,
989 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700990 if (ret)
991 goto out;
992
993 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
994
995 /* We should add 2 at frame size as the the firmware data is not
996 * included the CRC bytes.
997 */
998 frame_size += 2;
999
1000 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001001 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001002
Iiro Valkonen7686b102011-02-02 23:21:58 -08001003 ret = mxt_check_bootloader(client,
1004 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005 if (ret)
1006 goto out;
1007
1008 pos += frame_size;
1009
1010 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1011 }
1012
1013out:
1014 release_firmware(fw);
1015
1016 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 if (client->addr == MXT_BOOT_LOW)
1018 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001019 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001020 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021
1022 return ret;
1023}
1024
Iiro Valkonen7686b102011-02-02 23:21:58 -08001025static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001026 struct device_attribute *attr,
1027 const char *buf, size_t count)
1028{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001029 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001030 int error;
1031
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001032 disable_irq(data->irq);
1033
Iiro Valkonen7686b102011-02-02 23:21:58 -08001034 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035 if (error) {
1036 dev_err(dev, "The firmware update failed(%d)\n", error);
1037 count = error;
1038 } else {
1039 dev_dbg(dev, "The firmware update succeeded\n");
1040
1041 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043
1044 kfree(data->object_table);
1045 data->object_table = NULL;
1046
Iiro Valkonen7686b102011-02-02 23:21:58 -08001047 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001048 }
1049
1050 enable_irq(data->irq);
1051
Iiro Valkonen08960a02011-04-12 23:16:40 -07001052 error = mxt_make_highchg(data);
1053 if (error)
1054 return error;
1055
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001056 return count;
1057}
1058
Iiro Valkonen7686b102011-02-02 23:21:58 -08001059static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1060static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061
Iiro Valkonen7686b102011-02-02 23:21:58 -08001062static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063 &dev_attr_object.attr,
1064 &dev_attr_update_fw.attr,
1065 NULL
1066};
1067
Iiro Valkonen7686b102011-02-02 23:21:58 -08001068static const struct attribute_group mxt_attr_group = {
1069 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001070};
1071
Amy Maloche52262212011-09-15 16:46:57 -07001072static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001073{
Jing Lin36aee812011-10-17 17:17:28 -07001074 int error;
1075
Amy Maloche52262212011-09-15 16:46:57 -07001076 /* restore the old power state values and reenable touch */
Jing Lin36aee812011-10-17 17:17:28 -07001077 error = __mxt_write_reg(data->client, data->t7_start_addr,
1078 T7_DATA_SIZE, data->t7_data);
1079 if (error < 0) {
1080 dev_err(&data->client->dev,
1081 "failed to restore old power state\n");
1082 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001083 }
Jing Lin36aee812011-10-17 17:17:28 -07001084
Amy Maloche52262212011-09-15 16:46:57 -07001085 error = mxt_write_object(data,
1086 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1087 if (error < 0) {
1088 dev_err(&data->client->dev, "failed to restore touch\n");
1089 return error;
1090 }
1091
1092 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093}
1094
Amy Maloche52262212011-09-15 16:46:57 -07001095static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001096{
Jing Lin36aee812011-10-17 17:17:28 -07001097 int error;
1098 u8 t7_data[T7_DATA_SIZE] = {0};
1099
1100 /* disable touch and configure deep sleep mode */
1101 error = mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
1102 if (error < 0) {
1103 dev_err(&data->client->dev, "failed to disable touch\n");
1104 return error;
Amy Maloche52262212011-09-15 16:46:57 -07001105 }
1106
Jing Lin36aee812011-10-17 17:17:28 -07001107 error = __mxt_write_reg(data->client, data->t7_start_addr,
1108 T7_DATA_SIZE, t7_data);
Amy Maloche52262212011-09-15 16:46:57 -07001109 if (error < 0) {
1110 dev_err(&data->client->dev,
Jing Lin36aee812011-10-17 17:17:28 -07001111 "failed to configure deep sleep mode\n");
Amy Maloche52262212011-09-15 16:46:57 -07001112 return error;
1113 }
1114
1115 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001116}
1117
Iiro Valkonen7686b102011-02-02 23:21:58 -08001118static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001119{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001120 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001121 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001122
Amy Maloche52262212011-09-15 16:46:57 -07001123 error = mxt_start(data);
1124 if (error < 0) {
1125 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1126 return error;
1127 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001128
1129 return 0;
1130}
1131
Iiro Valkonen7686b102011-02-02 23:21:58 -08001132static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001133{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001134 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001135 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001136
Amy Maloche52262212011-09-15 16:46:57 -07001137 error = mxt_stop(data);
1138 if (error < 0)
1139 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1140
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001141}
1142
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301143static int mxt_power_on(struct mxt_data *data, bool on)
1144{
1145 int rc;
1146
1147 if (on == false)
1148 goto power_off;
1149
1150 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1151 if (rc < 0) {
1152 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1153 rc);
1154 return rc;
1155 }
1156
1157 rc = regulator_enable(data->vcc);
1158 if (rc) {
1159 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1160 rc);
1161 goto error_reg_en_vcc;
1162 }
1163
1164 if (data->pdata->i2c_pull_up) {
1165 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1166 if (rc < 0) {
1167 dev_err(&data->client->dev,
1168 "Regulator set_opt failed rc=%d\n", rc);
1169 goto error_reg_opt_i2c;
1170 }
1171
1172 rc = regulator_enable(data->vcc_i2c);
1173 if (rc) {
1174 dev_err(&data->client->dev,
1175 "Regulator enable failed rc=%d\n", rc);
1176 goto error_reg_en_vcc_i2c;
1177 }
1178 }
1179
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001180 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301181
1182 return 0;
1183
1184error_reg_en_vcc_i2c:
1185 if (data->pdata->i2c_pull_up)
1186 regulator_set_optimum_mode(data->vcc_i2c, 0);
1187error_reg_opt_i2c:
1188 regulator_disable(data->vcc);
1189error_reg_en_vcc:
1190 regulator_set_optimum_mode(data->vcc, 0);
1191 return rc;
1192
1193power_off:
1194 regulator_set_optimum_mode(data->vcc, 0);
1195 regulator_disable(data->vcc);
1196 if (data->pdata->i2c_pull_up) {
1197 regulator_set_optimum_mode(data->vcc_i2c, 0);
1198 regulator_disable(data->vcc_i2c);
1199 }
1200 msleep(50);
1201 return 0;
1202}
1203
1204static int mxt_regulator_configure(struct mxt_data *data, bool on)
1205{
1206 int rc;
1207
1208 if (on == false)
1209 goto hw_shutdown;
1210
1211 data->vcc = regulator_get(&data->client->dev, "vdd");
1212 if (IS_ERR(data->vcc)) {
1213 rc = PTR_ERR(data->vcc);
1214 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1215 rc);
1216 return rc;
1217 }
1218
1219 if (regulator_count_voltages(data->vcc) > 0) {
1220 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1221 MXT_VTG_MAX_UV);
1222 if (rc) {
1223 dev_err(&data->client->dev,
1224 "regulator set_vtg failed rc=%d\n", rc);
1225 goto error_set_vtg_vcc;
1226 }
1227 }
1228
1229 if (data->pdata->i2c_pull_up) {
1230 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1231 if (IS_ERR(data->vcc_i2c)) {
1232 rc = PTR_ERR(data->vcc_i2c);
1233 dev_err(&data->client->dev,
1234 "Regulator get failed rc=%d\n", rc);
1235 goto error_get_vtg_i2c;
1236 }
1237 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1238 rc = regulator_set_voltage(data->vcc_i2c,
1239 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1240 if (rc) {
1241 dev_err(&data->client->dev,
1242 "regulator set_vtg failed rc=%d\n", rc);
1243 goto error_set_vtg_i2c;
1244 }
1245 }
1246 }
1247
1248 return 0;
1249
1250error_set_vtg_i2c:
1251 regulator_put(data->vcc_i2c);
1252error_get_vtg_i2c:
1253 if (regulator_count_voltages(data->vcc) > 0)
1254 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1255error_set_vtg_vcc:
1256 regulator_put(data->vcc);
1257 return rc;
1258
1259hw_shutdown:
1260 if (regulator_count_voltages(data->vcc) > 0)
1261 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1262 regulator_put(data->vcc);
1263 if (data->pdata->i2c_pull_up) {
1264 if (regulator_count_voltages(data->vcc_i2c) > 0)
1265 regulator_set_voltage(data->vcc_i2c, 0,
1266 MXT_I2C_VTG_MAX_UV);
1267 regulator_put(data->vcc_i2c);
1268 }
1269 return 0;
1270}
1271
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301272#ifdef CONFIG_PM
1273static int mxt_suspend(struct device *dev)
1274{
1275 struct i2c_client *client = to_i2c_client(dev);
1276 struct mxt_data *data = i2c_get_clientdata(client);
1277 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001278 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301279
1280 mutex_lock(&input_dev->mutex);
1281
Amy Maloche52262212011-09-15 16:46:57 -07001282 if (input_dev->users) {
1283 error = mxt_stop(data);
1284 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001285 dev_err(dev, "mxt_stop failed in suspend\n");
Amy Maloche52262212011-09-15 16:46:57 -07001286 mutex_unlock(&input_dev->mutex);
1287 return error;
1288 }
1289
1290 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301291
1292 mutex_unlock(&input_dev->mutex);
1293
1294 return 0;
1295}
1296
1297static int mxt_resume(struct device *dev)
1298{
1299 struct i2c_client *client = to_i2c_client(dev);
1300 struct mxt_data *data = i2c_get_clientdata(client);
1301 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001302 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301303
1304 mutex_lock(&input_dev->mutex);
1305
Amy Maloche52262212011-09-15 16:46:57 -07001306 if (input_dev->users) {
1307 error = mxt_start(data);
1308 if (error < 0) {
Jing Lin36aee812011-10-17 17:17:28 -07001309 dev_err(dev, "mxt_start failed in resume\n");
Amy Maloche52262212011-09-15 16:46:57 -07001310 mutex_unlock(&input_dev->mutex);
1311 return error;
1312 }
1313 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301314
1315 mutex_unlock(&input_dev->mutex);
1316
1317 return 0;
1318}
1319
1320#if defined(CONFIG_HAS_EARLYSUSPEND)
1321static void mxt_early_suspend(struct early_suspend *h)
1322{
1323 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1324
1325 mxt_suspend(&data->client->dev);
1326}
1327
1328static void mxt_late_resume(struct early_suspend *h)
1329{
1330 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1331
1332 mxt_resume(&data->client->dev);
1333}
1334#endif
1335
1336static const struct dev_pm_ops mxt_pm_ops = {
1337#ifndef CONFIG_HAS_EARLYSUSPEND
1338 .suspend = mxt_suspend,
1339 .resume = mxt_resume,
1340#endif
1341};
1342#endif
1343
Iiro Valkonen7686b102011-02-02 23:21:58 -08001344static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001345 const struct i2c_device_id *id)
1346{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001347 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001348 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001349 struct input_dev *input_dev;
1350 int error;
1351
Iiro Valkonen919ed892011-02-15 13:36:52 -08001352 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001353 return -EINVAL;
1354
Iiro Valkonen7686b102011-02-02 23:21:58 -08001355 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001356 input_dev = input_allocate_device();
1357 if (!data || !input_dev) {
1358 dev_err(&client->dev, "Failed to allocate memory\n");
1359 error = -ENOMEM;
1360 goto err_free_mem;
1361 }
1362
Iiro Valkonen7686b102011-02-02 23:21:58 -08001363 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001364 input_dev->id.bustype = BUS_I2C;
1365 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001366 input_dev->open = mxt_input_open;
1367 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001368
Joonyoung Shim910d8052011-04-12 23:14:38 -07001369 data->client = client;
1370 data->input_dev = input_dev;
1371 data->pdata = pdata;
1372 data->irq = client->irq;
1373
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001374 __set_bit(EV_ABS, input_dev->evbit);
1375 __set_bit(EV_KEY, input_dev->evbit);
1376 __set_bit(BTN_TOUCH, input_dev->keybit);
1377
1378 /* For single touch */
1379 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001380 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001381 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001382 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001383
1384 /* For multi touch */
1385 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001386 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001387 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001388 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001390 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001391
1392 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001393 i2c_set_clientdata(client, data);
1394
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301395 if (pdata->init_hw)
1396 error = pdata->init_hw(true);
1397 else
1398 error = mxt_regulator_configure(data, true);
1399 if (error) {
1400 dev_err(&client->dev, "Failed to intialize hardware\n");
1401 goto err_free_object;
1402 }
1403
1404 if (pdata->power_on)
1405 error = pdata->power_on(true);
1406 else
1407 error = mxt_power_on(data, true);
1408 if (error) {
1409 dev_err(&client->dev, "Failed to power on hardware\n");
1410 goto err_regulator_on;
1411 }
1412
Iiro Valkonen7686b102011-02-02 23:21:58 -08001413 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001414 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301415 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001416
Iiro Valkonen7686b102011-02-02 23:21:58 -08001417 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001418 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001419 if (error) {
1420 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301421 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001422 }
1423
Iiro Valkonen08960a02011-04-12 23:16:40 -07001424 error = mxt_make_highchg(data);
1425 if (error)
1426 goto err_free_irq;
1427
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001428 error = input_register_device(input_dev);
1429 if (error)
1430 goto err_free_irq;
1431
Iiro Valkonen7686b102011-02-02 23:21:58 -08001432 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001433 if (error)
1434 goto err_unregister_device;
1435
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301436#if defined(CONFIG_HAS_EARLYSUSPEND)
1437 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1438 MXT_SUSPEND_LEVEL;
1439 data->early_suspend.suspend = mxt_early_suspend;
1440 data->early_suspend.resume = mxt_late_resume;
1441 register_early_suspend(&data->early_suspend);
1442#endif
1443
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001444 return 0;
1445
1446err_unregister_device:
1447 input_unregister_device(input_dev);
1448 input_dev = NULL;
1449err_free_irq:
1450 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301451err_power_on:
1452 if (pdata->power_on)
1453 pdata->power_on(false);
1454 else
1455 mxt_power_on(data, false);
1456err_regulator_on:
1457 if (pdata->init_hw)
1458 pdata->init_hw(false);
1459 else
1460 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001461err_free_object:
1462 kfree(data->object_table);
1463err_free_mem:
1464 input_free_device(input_dev);
1465 kfree(data);
1466 return error;
1467}
1468
Iiro Valkonen7686b102011-02-02 23:21:58 -08001469static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001471 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001472
Iiro Valkonen7686b102011-02-02 23:21:58 -08001473 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001474 free_irq(data->irq, data);
1475 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301476#if defined(CONFIG_HAS_EARLYSUSPEND)
1477 unregister_early_suspend(&data->early_suspend);
1478#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301479
1480 if (data->pdata->power_on)
1481 data->pdata->power_on(false);
1482 else
1483 mxt_power_on(data, false);
1484
1485 if (data->pdata->init_hw)
1486 data->pdata->init_hw(false);
1487 else
1488 mxt_regulator_configure(data, false);
1489
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001490 kfree(data->object_table);
1491 kfree(data);
1492
1493 return 0;
1494}
1495
Iiro Valkonen7686b102011-02-02 23:21:58 -08001496static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001497 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001498 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001499 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001500 { }
1501};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001502MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001503
Iiro Valkonen7686b102011-02-02 23:21:58 -08001504static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001505 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001506 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001507 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001508#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001509 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001510#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001511 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001512 .probe = mxt_probe,
1513 .remove = __devexit_p(mxt_remove),
1514 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001515};
1516
Iiro Valkonen7686b102011-02-02 23:21:58 -08001517static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001518{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001519 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001520}
1521
Iiro Valkonen7686b102011-02-02 23:21:58 -08001522static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001523{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001524 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001525}
1526
Iiro Valkonen7686b102011-02-02 23:21:58 -08001527module_init(mxt_init);
1528module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001529
1530/* Module information */
1531MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001532MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001533MODULE_LICENSE("GPL");