blob: 1c341c31aa26d5c86faabe7f9233df4e21a05961 [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>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/firmware.h>
18#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080019#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070020#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070021#include <linux/interrupt.h>
22#include <linux/slab.h>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053023#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070024
Anirudh Ghayal253ce122011-08-09 19:32:57 +053025#if defined(CONFIG_HAS_EARLYSUSPEND)
26#include <linux/earlysuspend.h>
27/* Early-suspend level */
28#define MXT_SUSPEND_LEVEL 1
29#endif
30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070031/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080032#define MXT_VER_20 20
33#define MXT_VER_21 21
34#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070035
36/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_APP_LOW 0x4a
38#define MXT_APP_HIGH 0x4b
39#define MXT_BOOT_LOW 0x24
40#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070041
42/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080043#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070044
45/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080046#define MXT_FAMILY_ID 0x00
47#define MXT_VARIANT_ID 0x01
48#define MXT_VERSION 0x02
49#define MXT_BUILD 0x03
50#define MXT_MATRIX_X_SIZE 0x04
51#define MXT_MATRIX_Y_SIZE 0x05
52#define MXT_OBJECT_NUM 0x06
53#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070054
Iiro Valkonen7686b102011-02-02 23:21:58 -080055#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070056
57/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080058#define MXT_DEBUG_DIAGNOSTIC 37
59#define MXT_GEN_MESSAGE 5
60#define MXT_GEN_COMMAND 6
61#define MXT_GEN_POWER 7
62#define MXT_GEN_ACQUIRE 8
63#define MXT_TOUCH_MULTI 9
64#define MXT_TOUCH_KEYARRAY 15
65#define MXT_TOUCH_PROXIMITY 23
66#define MXT_PROCI_GRIPFACE 20
67#define MXT_PROCG_NOISE 22
68#define MXT_PROCI_ONETOUCH 24
69#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070070#define MXT_PROCI_GRIP 40
71#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070072#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080073#define MXT_SPT_GPIOPWM 19
74#define MXT_SPT_SELFTEST 25
75#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070076#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070077#define MXT_SPT_DIGITIZER 43
78#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070079
Iiro Valkonen7686b102011-02-02 23:21:58 -080080/* MXT_GEN_COMMAND field */
81#define MXT_COMMAND_RESET 0
82#define MXT_COMMAND_BACKUPNV 1
83#define MXT_COMMAND_CALIBRATE 2
84#define MXT_COMMAND_REPORTALL 3
85#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070086
Iiro Valkonen7686b102011-02-02 23:21:58 -080087/* MXT_GEN_POWER field */
88#define MXT_POWER_IDLEACQINT 0
89#define MXT_POWER_ACTVACQINT 1
90#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen7686b102011-02-02 23:21:58 -080092/* MXT_GEN_ACQUIRE field */
93#define MXT_ACQUIRE_CHRGTIME 0
94#define MXT_ACQUIRE_TCHDRIFT 2
95#define MXT_ACQUIRE_DRIFTST 3
96#define MXT_ACQUIRE_TCHAUTOCAL 4
97#define MXT_ACQUIRE_SYNC 5
98#define MXT_ACQUIRE_ATCHCALST 6
99#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700100
Iiro Valkonen7686b102011-02-02 23:21:58 -0800101/* MXT_TOUCH_MULTI field */
102#define MXT_TOUCH_CTRL 0
103#define MXT_TOUCH_XORIGIN 1
104#define MXT_TOUCH_YORIGIN 2
105#define MXT_TOUCH_XSIZE 3
106#define MXT_TOUCH_YSIZE 4
107#define MXT_TOUCH_BLEN 6
108#define MXT_TOUCH_TCHTHR 7
109#define MXT_TOUCH_TCHDI 8
110#define MXT_TOUCH_ORIENT 9
111#define MXT_TOUCH_MOVHYSTI 11
112#define MXT_TOUCH_MOVHYSTN 12
113#define MXT_TOUCH_NUMTOUCH 14
114#define MXT_TOUCH_MRGHYST 15
115#define MXT_TOUCH_MRGTHR 16
116#define MXT_TOUCH_AMPHYST 17
117#define MXT_TOUCH_XRANGE_LSB 18
118#define MXT_TOUCH_XRANGE_MSB 19
119#define MXT_TOUCH_YRANGE_LSB 20
120#define MXT_TOUCH_YRANGE_MSB 21
121#define MXT_TOUCH_XLOCLIP 22
122#define MXT_TOUCH_XHICLIP 23
123#define MXT_TOUCH_YLOCLIP 24
124#define MXT_TOUCH_YHICLIP 25
125#define MXT_TOUCH_XEDGECTRL 26
126#define MXT_TOUCH_XEDGEDIST 27
127#define MXT_TOUCH_YEDGECTRL 28
128#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700129#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700130
Iiro Valkonen7686b102011-02-02 23:21:58 -0800131/* MXT_PROCI_GRIPFACE field */
132#define MXT_GRIPFACE_CTRL 0
133#define MXT_GRIPFACE_XLOGRIP 1
134#define MXT_GRIPFACE_XHIGRIP 2
135#define MXT_GRIPFACE_YLOGRIP 3
136#define MXT_GRIPFACE_YHIGRIP 4
137#define MXT_GRIPFACE_MAXTCHS 5
138#define MXT_GRIPFACE_SZTHR1 7
139#define MXT_GRIPFACE_SZTHR2 8
140#define MXT_GRIPFACE_SHPTHR1 9
141#define MXT_GRIPFACE_SHPTHR2 10
142#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700143
Iiro Valkonen7686b102011-02-02 23:21:58 -0800144/* MXT_PROCI_NOISE field */
145#define MXT_NOISE_CTRL 0
146#define MXT_NOISE_OUTFLEN 1
147#define MXT_NOISE_GCAFUL_LSB 3
148#define MXT_NOISE_GCAFUL_MSB 4
149#define MXT_NOISE_GCAFLL_LSB 5
150#define MXT_NOISE_GCAFLL_MSB 6
151#define MXT_NOISE_ACTVGCAFVALID 7
152#define MXT_NOISE_NOISETHR 8
153#define MXT_NOISE_FREQHOPSCALE 10
154#define MXT_NOISE_FREQ0 11
155#define MXT_NOISE_FREQ1 12
156#define MXT_NOISE_FREQ2 13
157#define MXT_NOISE_FREQ3 14
158#define MXT_NOISE_FREQ4 15
159#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700160
Iiro Valkonen7686b102011-02-02 23:21:58 -0800161/* MXT_SPT_COMMSCONFIG */
162#define MXT_COMMS_CTRL 0
163#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonen7686b102011-02-02 23:21:58 -0800165/* MXT_SPT_CTECONFIG field */
166#define MXT_CTE_CTRL 0
167#define MXT_CTE_CMD 1
168#define MXT_CTE_MODE 2
169#define MXT_CTE_IDLEGCAFDEPTH 3
170#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700171#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700172
Iiro Valkonen7686b102011-02-02 23:21:58 -0800173#define MXT_VOLTAGE_DEFAULT 2700000
174#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700175
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530176#define MXT_VTG_MIN_UV 2700000
177#define MXT_VTG_MAX_UV 3300000
178#define MXT_ACTIVE_LOAD_UA 15000
179
180#define MXT_I2C_VTG_MIN_UV 1800000
181#define MXT_I2C_VTG_MAX_UV 1800000
182#define MXT_I2C_LOAD_UA 10000
183
Iiro Valkonen7686b102011-02-02 23:21:58 -0800184/* Define for MXT_GEN_COMMAND */
185#define MXT_BOOT_VALUE 0xa5
186#define MXT_BACKUP_VALUE 0x55
187#define MXT_BACKUP_TIME 25 /* msec */
188#define MXT_RESET_TIME 65 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700189
Iiro Valkonen7686b102011-02-02 23:21:58 -0800190#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700191
192/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800193#define MXT_UNLOCK_CMD_MSB 0xaa
194#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700195
196/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800197#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
198#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
199#define MXT_FRAME_CRC_CHECK 0x02
200#define MXT_FRAME_CRC_FAIL 0x03
201#define MXT_FRAME_CRC_PASS 0x04
202#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
203#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700204
205/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800206#define MXT_SUPPRESS (1 << 1)
207#define MXT_AMP (1 << 2)
208#define MXT_VECTOR (1 << 3)
209#define MXT_MOVE (1 << 4)
210#define MXT_RELEASE (1 << 5)
211#define MXT_PRESS (1 << 6)
212#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700213
Joonyoung Shim910d8052011-04-12 23:14:38 -0700214/* Touch orient bits */
215#define MXT_XY_SWITCH (1 << 0)
216#define MXT_X_INVERT (1 << 1)
217#define MXT_Y_INVERT (1 << 2)
218
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800220#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700221
Iiro Valkonen7686b102011-02-02 23:21:58 -0800222#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700223
Iiro Valkonen7686b102011-02-02 23:21:58 -0800224struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700225 u8 family_id;
226 u8 variant_id;
227 u8 version;
228 u8 build;
229 u8 matrix_xsize;
230 u8 matrix_ysize;
231 u8 object_num;
232};
233
Iiro Valkonen7686b102011-02-02 23:21:58 -0800234struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235 u8 type;
236 u16 start_address;
237 u8 size;
238 u8 instances;
239 u8 num_report_ids;
240
241 /* to map object and message */
242 u8 max_reportid;
243};
244
Iiro Valkonen7686b102011-02-02 23:21:58 -0800245struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700246 u8 reportid;
247 u8 message[7];
248 u8 checksum;
249};
250
Iiro Valkonen7686b102011-02-02 23:21:58 -0800251struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700252 int status;
253 int x;
254 int y;
255 int area;
256};
257
258/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800259struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700260 struct i2c_client *client;
261 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800262 const struct mxt_platform_data *pdata;
263 struct mxt_object *object_table;
264 struct mxt_info info;
265 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700266 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700267 unsigned int max_x;
268 unsigned int max_y;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530269 struct regulator *vcc;
270 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530271#if defined(CONFIG_HAS_EARLYSUSPEND)
272 struct early_suspend early_suspend;
273#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700274};
275
Iiro Valkonen7686b102011-02-02 23:21:58 -0800276static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700277{
278 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800279 case MXT_GEN_MESSAGE:
280 case MXT_GEN_COMMAND:
281 case MXT_GEN_POWER:
282 case MXT_GEN_ACQUIRE:
283 case MXT_TOUCH_MULTI:
284 case MXT_TOUCH_KEYARRAY:
285 case MXT_TOUCH_PROXIMITY:
286 case MXT_PROCI_GRIPFACE:
287 case MXT_PROCG_NOISE:
288 case MXT_PROCI_ONETOUCH:
289 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700290 case MXT_PROCI_GRIP:
291 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800292 case MXT_SPT_COMMSCONFIG:
293 case MXT_SPT_GPIOPWM:
294 case MXT_SPT_SELFTEST:
295 case MXT_SPT_CTECONFIG:
296 case MXT_SPT_USERDATA:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700297 return true;
298 default:
299 return false;
300 }
301}
302
Iiro Valkonen7686b102011-02-02 23:21:58 -0800303static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700304{
305 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800306 case MXT_GEN_COMMAND:
307 case MXT_GEN_POWER:
308 case MXT_GEN_ACQUIRE:
309 case MXT_TOUCH_MULTI:
310 case MXT_TOUCH_KEYARRAY:
311 case MXT_TOUCH_PROXIMITY:
312 case MXT_PROCI_GRIPFACE:
313 case MXT_PROCG_NOISE:
314 case MXT_PROCI_ONETOUCH:
315 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700316 case MXT_PROCI_GRIP:
317 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800318 case MXT_SPT_GPIOPWM:
319 case MXT_SPT_SELFTEST:
320 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530321 case MXT_SPT_USERDATA:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700322 return true;
323 default:
324 return false;
325 }
326}
327
Iiro Valkonen7686b102011-02-02 23:21:58 -0800328static void mxt_dump_message(struct device *dev,
329 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700330{
331 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
332 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
333 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
334 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
335 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
336 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
337 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
338 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
339 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
340}
341
Iiro Valkonen7686b102011-02-02 23:21:58 -0800342static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700343 unsigned int state)
344{
345 u8 val;
346
347recheck:
348 if (i2c_master_recv(client, &val, 1) != 1) {
349 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
350 return -EIO;
351 }
352
353 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800354 case MXT_WAITING_BOOTLOAD_CMD:
355 case MXT_WAITING_FRAME_DATA:
356 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700357 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800358 case MXT_FRAME_CRC_PASS:
359 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700360 goto recheck;
361 break;
362 default:
363 return -EINVAL;
364 }
365
366 if (val != state) {
367 dev_err(&client->dev, "Unvalid bootloader mode state\n");
368 return -EINVAL;
369 }
370
371 return 0;
372}
373
Iiro Valkonen7686b102011-02-02 23:21:58 -0800374static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700375{
376 u8 buf[2];
377
Iiro Valkonen7686b102011-02-02 23:21:58 -0800378 buf[0] = MXT_UNLOCK_CMD_LSB;
379 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700380
381 if (i2c_master_send(client, buf, 2) != 2) {
382 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
383 return -EIO;
384 }
385
386 return 0;
387}
388
Iiro Valkonen7686b102011-02-02 23:21:58 -0800389static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700390 const u8 *data, unsigned int frame_size)
391{
392 if (i2c_master_send(client, data, frame_size) != frame_size) {
393 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
394 return -EIO;
395 }
396
397 return 0;
398}
399
Iiro Valkonen7686b102011-02-02 23:21:58 -0800400static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700401 u16 reg, u16 len, void *val)
402{
403 struct i2c_msg xfer[2];
404 u8 buf[2];
405
406 buf[0] = reg & 0xff;
407 buf[1] = (reg >> 8) & 0xff;
408
409 /* Write register */
410 xfer[0].addr = client->addr;
411 xfer[0].flags = 0;
412 xfer[0].len = 2;
413 xfer[0].buf = buf;
414
415 /* Read data */
416 xfer[1].addr = client->addr;
417 xfer[1].flags = I2C_M_RD;
418 xfer[1].len = len;
419 xfer[1].buf = val;
420
421 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
422 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
423 return -EIO;
424 }
425
426 return 0;
427}
428
Iiro Valkonen7686b102011-02-02 23:21:58 -0800429static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800431 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700432}
433
Iiro Valkonen7686b102011-02-02 23:21:58 -0800434static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700435{
436 u8 buf[3];
437
438 buf[0] = reg & 0xff;
439 buf[1] = (reg >> 8) & 0xff;
440 buf[2] = val;
441
442 if (i2c_master_send(client, buf, 3) != 3) {
443 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
444 return -EIO;
445 }
446
447 return 0;
448}
449
Iiro Valkonen7686b102011-02-02 23:21:58 -0800450static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700451 u16 reg, u8 *object_buf)
452{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800453 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700454 object_buf);
455}
456
Iiro Valkonen7686b102011-02-02 23:21:58 -0800457static struct mxt_object *
458mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700459{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800460 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700461 int i;
462
463 for (i = 0; i < data->info.object_num; i++) {
464 object = data->object_table + i;
465 if (object->type == type)
466 return object;
467 }
468
469 dev_err(&data->client->dev, "Invalid object type\n");
470 return NULL;
471}
472
Iiro Valkonen7686b102011-02-02 23:21:58 -0800473static int mxt_read_message(struct mxt_data *data,
474 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700475{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800476 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700477 u16 reg;
478
Iiro Valkonen7686b102011-02-02 23:21:58 -0800479 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700480 if (!object)
481 return -EINVAL;
482
483 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800484 return __mxt_read_reg(data->client, reg,
485 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700486}
487
Iiro Valkonen7686b102011-02-02 23:21:58 -0800488static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700489 u8 type, u8 offset, u8 *val)
490{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800491 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700492 u16 reg;
493
Iiro Valkonen7686b102011-02-02 23:21:58 -0800494 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495 if (!object)
496 return -EINVAL;
497
498 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800499 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700500}
501
Iiro Valkonen7686b102011-02-02 23:21:58 -0800502static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700503 u8 type, u8 offset, u8 val)
504{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800505 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700506 u16 reg;
507
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700509 if (!object)
510 return -EINVAL;
511
512 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800513 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514}
515
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800518 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700519 struct input_dev *input_dev = data->input_dev;
520 int status = finger[single_id].status;
521 int finger_num = 0;
522 int id;
523
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525 if (!finger[id].status)
526 continue;
527
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700528 input_mt_slot(input_dev, id);
529 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
530 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700531
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700532 if (finger[id].status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700533 finger_num++;
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700534 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
535 finger[id].area);
536 input_report_abs(input_dev, ABS_MT_POSITION_X,
537 finger[id].x);
538 input_report_abs(input_dev, ABS_MT_POSITION_Y,
539 finger[id].y);
540 } else {
541 finger[id].status = 0;
542 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700543 }
544
545 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
546
Iiro Valkonen7686b102011-02-02 23:21:58 -0800547 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700548 input_report_abs(input_dev, ABS_X, finger[single_id].x);
549 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
550 }
551
552 input_sync(input_dev);
553}
554
Iiro Valkonen7686b102011-02-02 23:21:58 -0800555static void mxt_input_touchevent(struct mxt_data *data,
556 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800558 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700559 struct device *dev = &data->client->dev;
560 u8 status = message->message[0];
561 int x;
562 int y;
563 int area;
564
565 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800566 if (!(status & MXT_DETECT)) {
567 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700568 dev_dbg(dev, "[%d] released\n", id);
569
Iiro Valkonen7686b102011-02-02 23:21:58 -0800570 finger[id].status = MXT_RELEASE;
571 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572 }
573 return;
574 }
575
576 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800577 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700578 return;
579
Joonyoung Shim910d8052011-04-12 23:14:38 -0700580 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
581 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
582 if (data->max_x < 1024)
583 x = x >> 2;
584 if (data->max_y < 1024)
585 y = y >> 2;
586
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700587 area = message->message[4];
588
589 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591 x, y, area);
592
Iiro Valkonen7686b102011-02-02 23:21:58 -0800593 finger[id].status = status & MXT_MOVE ?
594 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700595 finger[id].x = x;
596 finger[id].y = y;
597 finger[id].area = area;
598
Iiro Valkonen7686b102011-02-02 23:21:58 -0800599 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600}
601
Iiro Valkonen7686b102011-02-02 23:21:58 -0800602static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604 struct mxt_data *data = dev_id;
605 struct mxt_message message;
606 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700607 struct device *dev = &data->client->dev;
608 int id;
609 u8 reportid;
610 u8 max_reportid;
611 u8 min_reportid;
612
613 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800614 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700615 dev_err(dev, "Failed to read message\n");
616 goto end;
617 }
618
619 reportid = message.reportid;
620
Iiro Valkonen7686b102011-02-02 23:21:58 -0800621 /* whether reportid is thing of MXT_TOUCH_MULTI */
622 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700623 if (!object)
624 goto end;
625
626 max_reportid = object->max_reportid;
627 min_reportid = max_reportid - object->num_report_ids + 1;
628 id = reportid - min_reportid;
629
630 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800631 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700632 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634 } while (reportid != 0xff);
635
636end:
637 return IRQ_HANDLED;
638}
639
Iiro Valkonen7686b102011-02-02 23:21:58 -0800640static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800642 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800643 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700644 struct device *dev = &data->client->dev;
645 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800646 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700647
Iiro Valkonen71749f52011-02-15 13:36:52 -0800648 if (!pdata->config) {
649 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
650 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700651 }
652
653 for (i = 0; i < data->info.object_num; i++) {
654 object = data->object_table + i;
655
Iiro Valkonen7686b102011-02-02 23:21:58 -0800656 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700657 continue;
658
Iiro Valkonen71749f52011-02-15 13:36:52 -0800659 for (j = 0; j < object->size + 1; j++) {
660 config_offset = index + j;
661 if (config_offset > pdata->config_length) {
662 dev_err(dev, "Not enough config data!\n");
663 return -EINVAL;
664 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800666 pdata->config[config_offset]);
667 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 index += object->size + 1;
669 }
670
671 return 0;
672}
673
Iiro Valkonen7686b102011-02-02 23:21:58 -0800674static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675{
676 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800677 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700678 int count = 10;
679 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680
681 /* Read dummy message to make high CHG pin */
682 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800683 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684 if (error)
685 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800686 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687
688 if (!count) {
689 dev_err(dev, "CHG pin isn't cleared\n");
690 return -EBUSY;
691 }
692
693 return 0;
694}
695
Iiro Valkonen7686b102011-02-02 23:21:58 -0800696static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700697{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699 u8 voltage;
700
701 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800702 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700703 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800704 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705 pdata->y_line);
706
707 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800708 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700709 pdata->orient);
710
711 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800712 mxt_write_object(data, MXT_TOUCH_MULTI,
713 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714
715 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716 mxt_write_object(data, MXT_TOUCH_MULTI,
717 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718
719 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800720 mxt_write_object(data, MXT_TOUCH_MULTI,
721 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
722 mxt_write_object(data, MXT_TOUCH_MULTI,
723 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
724 mxt_write_object(data, MXT_TOUCH_MULTI,
725 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
726 mxt_write_object(data, MXT_TOUCH_MULTI,
727 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728
729 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700730 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800731 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
732 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
733 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 voltage = 0xff - voltage + 1;
735 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800736 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
737 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700738
Iiro Valkonen7686b102011-02-02 23:21:58 -0800739 mxt_write_object(data, MXT_SPT_CTECONFIG,
740 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700741 }
742}
743
Iiro Valkonen7686b102011-02-02 23:21:58 -0800744static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700745{
746 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748 int error;
749 u8 val;
750
Iiro Valkonen7686b102011-02-02 23:21:58 -0800751 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752 if (error)
753 return error;
754 info->family_id = val;
755
Iiro Valkonen7686b102011-02-02 23:21:58 -0800756 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700757 if (error)
758 return error;
759 info->variant_id = val;
760
Iiro Valkonen7686b102011-02-02 23:21:58 -0800761 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700762 if (error)
763 return error;
764 info->version = val;
765
Iiro Valkonen7686b102011-02-02 23:21:58 -0800766 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700767 if (error)
768 return error;
769 info->build = val;
770
Iiro Valkonen7686b102011-02-02 23:21:58 -0800771 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700772 if (error)
773 return error;
774 info->object_num = val;
775
776 return 0;
777}
778
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700780{
781 int error;
782 int i;
783 u16 reg;
784 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800785 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786
787 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800788 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
791 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700792 if (error)
793 return error;
794
795 object->type = buf[0];
796 object->start_address = (buf[2] << 8) | buf[1];
797 object->size = buf[3];
798 object->instances = buf[4];
799 object->num_report_ids = buf[5];
800
801 if (object->num_report_ids) {
802 reportid += object->num_report_ids *
803 (object->instances + 1);
804 object->max_reportid = reportid;
805 }
806 }
807
808 return 0;
809}
810
Iiro Valkonen7686b102011-02-02 23:21:58 -0800811static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700812{
813 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800814 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700815 int error;
816 u8 val;
817
Iiro Valkonen7686b102011-02-02 23:21:58 -0800818 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700819 if (error)
820 return error;
821
822 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800823 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824 GFP_KERNEL);
825 if (!data->object_table) {
826 dev_err(&client->dev, "Failed to allocate memory\n");
827 return -ENOMEM;
828 }
829
830 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800831 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700832 if (error)
833 return error;
834
835 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800836 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700837 if (error)
838 return error;
839
Iiro Valkonen7686b102011-02-02 23:21:58 -0800840 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700841
842 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800843 mxt_write_object(data, MXT_GEN_COMMAND,
844 MXT_COMMAND_BACKUPNV,
845 MXT_BACKUP_VALUE);
846 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700847
848 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800849 mxt_write_object(data, MXT_GEN_COMMAND,
850 MXT_COMMAND_RESET, 1);
851 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700852
853 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800854 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700855 if (error)
856 return error;
857 info->matrix_xsize = val;
858
Iiro Valkonen7686b102011-02-02 23:21:58 -0800859 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700860 if (error)
861 return error;
862 info->matrix_ysize = val;
863
864 dev_info(&client->dev,
865 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
866 info->family_id, info->variant_id, info->version,
867 info->build);
868
869 dev_info(&client->dev,
870 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
871 info->matrix_xsize, info->matrix_ysize,
872 info->object_num);
873
874 return 0;
875}
876
Joonyoung Shim910d8052011-04-12 23:14:38 -0700877static void mxt_calc_resolution(struct mxt_data *data)
878{
879 unsigned int max_x = data->pdata->x_size - 1;
880 unsigned int max_y = data->pdata->y_size - 1;
881
882 if (data->pdata->orient & MXT_XY_SWITCH) {
883 data->max_x = max_y;
884 data->max_y = max_x;
885 } else {
886 data->max_x = max_x;
887 data->max_y = max_y;
888 }
889}
890
Iiro Valkonen7686b102011-02-02 23:21:58 -0800891static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700892 struct device_attribute *attr, char *buf)
893{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800894 struct mxt_data *data = dev_get_drvdata(dev);
895 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700896 int count = 0;
897 int i, j;
898 int error;
899 u8 val;
900
901 for (i = 0; i < data->info.object_num; i++) {
902 object = data->object_table + i;
903
904 count += sprintf(buf + count,
905 "Object Table Element %d(Type %d)\n",
906 i + 1, object->type);
907
Iiro Valkonen7686b102011-02-02 23:21:58 -0800908 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909 count += sprintf(buf + count, "\n");
910 continue;
911 }
912
913 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800914 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700915 object->type, j, &val);
916 if (error)
917 return error;
918
919 count += sprintf(buf + count,
920 " Byte %d: 0x%x (%d)\n", j, val, val);
921 }
922
923 count += sprintf(buf + count, "\n");
924 }
925
926 return count;
927}
928
Iiro Valkonen7686b102011-02-02 23:21:58 -0800929static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700930{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800931 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700932 struct i2c_client *client = data->client;
933 const struct firmware *fw = NULL;
934 unsigned int frame_size;
935 unsigned int pos = 0;
936 int ret;
937
938 ret = request_firmware(&fw, fn, dev);
939 if (ret) {
940 dev_err(dev, "Unable to open firmware %s\n", fn);
941 return ret;
942 }
943
944 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800945 mxt_write_object(data, MXT_GEN_COMMAND,
946 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
947 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700948
949 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800950 if (client->addr == MXT_APP_LOW)
951 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700952 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956 if (ret)
957 goto out;
958
959 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800960 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700961
962 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800963 ret = mxt_check_bootloader(client,
964 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700965 if (ret)
966 goto out;
967
968 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
969
970 /* We should add 2 at frame size as the the firmware data is not
971 * included the CRC bytes.
972 */
973 frame_size += 2;
974
975 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800976 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700977
Iiro Valkonen7686b102011-02-02 23:21:58 -0800978 ret = mxt_check_bootloader(client,
979 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980 if (ret)
981 goto out;
982
983 pos += frame_size;
984
985 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
986 }
987
988out:
989 release_firmware(fw);
990
991 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992 if (client->addr == MXT_BOOT_LOW)
993 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700994 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996
997 return ret;
998}
999
Iiro Valkonen7686b102011-02-02 23:21:58 -08001000static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001 struct device_attribute *attr,
1002 const char *buf, size_t count)
1003{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001004 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005 int error;
1006
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 disable_irq(data->irq);
1008
Iiro Valkonen7686b102011-02-02 23:21:58 -08001009 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010 if (error) {
1011 dev_err(dev, "The firmware update failed(%d)\n", error);
1012 count = error;
1013 } else {
1014 dev_dbg(dev, "The firmware update succeeded\n");
1015
1016 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
1019 kfree(data->object_table);
1020 data->object_table = NULL;
1021
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001023 }
1024
1025 enable_irq(data->irq);
1026
Iiro Valkonen08960a02011-04-12 23:16:40 -07001027 error = mxt_make_highchg(data);
1028 if (error)
1029 return error;
1030
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001031 return count;
1032}
1033
Iiro Valkonen7686b102011-02-02 23:21:58 -08001034static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1035static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001036
Iiro Valkonen7686b102011-02-02 23:21:58 -08001037static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038 &dev_attr_object.attr,
1039 &dev_attr_update_fw.attr,
1040 NULL
1041};
1042
Iiro Valkonen7686b102011-02-02 23:21:58 -08001043static const struct attribute_group mxt_attr_group = {
1044 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045};
1046
Iiro Valkonen7686b102011-02-02 23:21:58 -08001047static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001048{
1049 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001050 mxt_write_object(data,
1051 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001052}
1053
Iiro Valkonen7686b102011-02-02 23:21:58 -08001054static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001055{
1056 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001057 mxt_write_object(data,
1058 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001059}
1060
Iiro Valkonen7686b102011-02-02 23:21:58 -08001061static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001062{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001063 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001064
Iiro Valkonen7686b102011-02-02 23:21:58 -08001065 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001066
1067 return 0;
1068}
1069
Iiro Valkonen7686b102011-02-02 23:21:58 -08001070static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001071{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001072 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001073
Iiro Valkonen7686b102011-02-02 23:21:58 -08001074 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001075}
1076
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301077static int mxt_power_on(struct mxt_data *data, bool on)
1078{
1079 int rc;
1080
1081 if (on == false)
1082 goto power_off;
1083
1084 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1085 if (rc < 0) {
1086 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1087 rc);
1088 return rc;
1089 }
1090
1091 rc = regulator_enable(data->vcc);
1092 if (rc) {
1093 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1094 rc);
1095 goto error_reg_en_vcc;
1096 }
1097
1098 if (data->pdata->i2c_pull_up) {
1099 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1100 if (rc < 0) {
1101 dev_err(&data->client->dev,
1102 "Regulator set_opt failed rc=%d\n", rc);
1103 goto error_reg_opt_i2c;
1104 }
1105
1106 rc = regulator_enable(data->vcc_i2c);
1107 if (rc) {
1108 dev_err(&data->client->dev,
1109 "Regulator enable failed rc=%d\n", rc);
1110 goto error_reg_en_vcc_i2c;
1111 }
1112 }
1113
1114 msleep(50);
1115
1116 return 0;
1117
1118error_reg_en_vcc_i2c:
1119 if (data->pdata->i2c_pull_up)
1120 regulator_set_optimum_mode(data->vcc_i2c, 0);
1121error_reg_opt_i2c:
1122 regulator_disable(data->vcc);
1123error_reg_en_vcc:
1124 regulator_set_optimum_mode(data->vcc, 0);
1125 return rc;
1126
1127power_off:
1128 regulator_set_optimum_mode(data->vcc, 0);
1129 regulator_disable(data->vcc);
1130 if (data->pdata->i2c_pull_up) {
1131 regulator_set_optimum_mode(data->vcc_i2c, 0);
1132 regulator_disable(data->vcc_i2c);
1133 }
1134 msleep(50);
1135 return 0;
1136}
1137
1138static int mxt_regulator_configure(struct mxt_data *data, bool on)
1139{
1140 int rc;
1141
1142 if (on == false)
1143 goto hw_shutdown;
1144
1145 data->vcc = regulator_get(&data->client->dev, "vdd");
1146 if (IS_ERR(data->vcc)) {
1147 rc = PTR_ERR(data->vcc);
1148 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1149 rc);
1150 return rc;
1151 }
1152
1153 if (regulator_count_voltages(data->vcc) > 0) {
1154 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1155 MXT_VTG_MAX_UV);
1156 if (rc) {
1157 dev_err(&data->client->dev,
1158 "regulator set_vtg failed rc=%d\n", rc);
1159 goto error_set_vtg_vcc;
1160 }
1161 }
1162
1163 if (data->pdata->i2c_pull_up) {
1164 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1165 if (IS_ERR(data->vcc_i2c)) {
1166 rc = PTR_ERR(data->vcc_i2c);
1167 dev_err(&data->client->dev,
1168 "Regulator get failed rc=%d\n", rc);
1169 goto error_get_vtg_i2c;
1170 }
1171 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1172 rc = regulator_set_voltage(data->vcc_i2c,
1173 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1174 if (rc) {
1175 dev_err(&data->client->dev,
1176 "regulator set_vtg failed rc=%d\n", rc);
1177 goto error_set_vtg_i2c;
1178 }
1179 }
1180 }
1181
1182 return 0;
1183
1184error_set_vtg_i2c:
1185 regulator_put(data->vcc_i2c);
1186error_get_vtg_i2c:
1187 if (regulator_count_voltages(data->vcc) > 0)
1188 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1189error_set_vtg_vcc:
1190 regulator_put(data->vcc);
1191 return rc;
1192
1193hw_shutdown:
1194 if (regulator_count_voltages(data->vcc) > 0)
1195 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1196 regulator_put(data->vcc);
1197 if (data->pdata->i2c_pull_up) {
1198 if (regulator_count_voltages(data->vcc_i2c) > 0)
1199 regulator_set_voltage(data->vcc_i2c, 0,
1200 MXT_I2C_VTG_MAX_UV);
1201 regulator_put(data->vcc_i2c);
1202 }
1203 return 0;
1204}
1205
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301206#ifdef CONFIG_PM
1207static int mxt_suspend(struct device *dev)
1208{
1209 struct i2c_client *client = to_i2c_client(dev);
1210 struct mxt_data *data = i2c_get_clientdata(client);
1211 struct input_dev *input_dev = data->input_dev;
1212
1213 mutex_lock(&input_dev->mutex);
1214
1215 if (input_dev->users)
1216 mxt_stop(data);
1217
1218 mutex_unlock(&input_dev->mutex);
1219
1220 return 0;
1221}
1222
1223static int mxt_resume(struct device *dev)
1224{
1225 struct i2c_client *client = to_i2c_client(dev);
1226 struct mxt_data *data = i2c_get_clientdata(client);
1227 struct input_dev *input_dev = data->input_dev;
1228
1229 /* Soft reset */
1230 mxt_write_object(data, MXT_GEN_COMMAND,
1231 MXT_COMMAND_RESET, 1);
1232
1233 msleep(MXT_RESET_TIME);
1234
1235 mutex_lock(&input_dev->mutex);
1236
1237 if (input_dev->users)
1238 mxt_start(data);
1239
1240 mutex_unlock(&input_dev->mutex);
1241
1242 return 0;
1243}
1244
1245#if defined(CONFIG_HAS_EARLYSUSPEND)
1246static void mxt_early_suspend(struct early_suspend *h)
1247{
1248 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1249
1250 mxt_suspend(&data->client->dev);
1251}
1252
1253static void mxt_late_resume(struct early_suspend *h)
1254{
1255 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1256
1257 mxt_resume(&data->client->dev);
1258}
1259#endif
1260
1261static const struct dev_pm_ops mxt_pm_ops = {
1262#ifndef CONFIG_HAS_EARLYSUSPEND
1263 .suspend = mxt_suspend,
1264 .resume = mxt_resume,
1265#endif
1266};
1267#endif
1268
Iiro Valkonen7686b102011-02-02 23:21:58 -08001269static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001270 const struct i2c_device_id *id)
1271{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001272 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001273 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001274 struct input_dev *input_dev;
1275 int error;
1276
Iiro Valkonen919ed892011-02-15 13:36:52 -08001277 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001278 return -EINVAL;
1279
Iiro Valkonen7686b102011-02-02 23:21:58 -08001280 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001281 input_dev = input_allocate_device();
1282 if (!data || !input_dev) {
1283 dev_err(&client->dev, "Failed to allocate memory\n");
1284 error = -ENOMEM;
1285 goto err_free_mem;
1286 }
1287
Iiro Valkonen7686b102011-02-02 23:21:58 -08001288 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001289 input_dev->id.bustype = BUS_I2C;
1290 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001291 input_dev->open = mxt_input_open;
1292 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001293
Joonyoung Shim910d8052011-04-12 23:14:38 -07001294 data->client = client;
1295 data->input_dev = input_dev;
1296 data->pdata = pdata;
1297 data->irq = client->irq;
1298
1299 mxt_calc_resolution(data);
1300
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001301 __set_bit(EV_ABS, input_dev->evbit);
1302 __set_bit(EV_KEY, input_dev->evbit);
1303 __set_bit(BTN_TOUCH, input_dev->keybit);
1304
1305 /* For single touch */
1306 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001307 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001308 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001309 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001310
1311 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001312 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001313 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001314 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001315 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001316 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001317 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001318 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001319
1320 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001321 i2c_set_clientdata(client, data);
1322
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301323 if (pdata->init_hw)
1324 error = pdata->init_hw(true);
1325 else
1326 error = mxt_regulator_configure(data, true);
1327 if (error) {
1328 dev_err(&client->dev, "Failed to intialize hardware\n");
1329 goto err_free_object;
1330 }
1331
1332 if (pdata->power_on)
1333 error = pdata->power_on(true);
1334 else
1335 error = mxt_power_on(data, true);
1336 if (error) {
1337 dev_err(&client->dev, "Failed to power on hardware\n");
1338 goto err_regulator_on;
1339 }
1340
Iiro Valkonen7686b102011-02-02 23:21:58 -08001341 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001342 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301343 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001344
Iiro Valkonen7686b102011-02-02 23:21:58 -08001345 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001346 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001347 if (error) {
1348 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301349 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001350 }
1351
Iiro Valkonen08960a02011-04-12 23:16:40 -07001352 error = mxt_make_highchg(data);
1353 if (error)
1354 goto err_free_irq;
1355
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001356 error = input_register_device(input_dev);
1357 if (error)
1358 goto err_free_irq;
1359
Iiro Valkonen7686b102011-02-02 23:21:58 -08001360 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001361 if (error)
1362 goto err_unregister_device;
1363
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301364#if defined(CONFIG_HAS_EARLYSUSPEND)
1365 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1366 MXT_SUSPEND_LEVEL;
1367 data->early_suspend.suspend = mxt_early_suspend;
1368 data->early_suspend.resume = mxt_late_resume;
1369 register_early_suspend(&data->early_suspend);
1370#endif
1371
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001372 return 0;
1373
1374err_unregister_device:
1375 input_unregister_device(input_dev);
1376 input_dev = NULL;
1377err_free_irq:
1378 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301379err_power_on:
1380 if (pdata->power_on)
1381 pdata->power_on(false);
1382 else
1383 mxt_power_on(data, false);
1384err_regulator_on:
1385 if (pdata->init_hw)
1386 pdata->init_hw(false);
1387 else
1388 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389err_free_object:
1390 kfree(data->object_table);
1391err_free_mem:
1392 input_free_device(input_dev);
1393 kfree(data);
1394 return error;
1395}
1396
Iiro Valkonen7686b102011-02-02 23:21:58 -08001397static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001398{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001399 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001400
Iiro Valkonen7686b102011-02-02 23:21:58 -08001401 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001402 free_irq(data->irq, data);
1403 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301404#if defined(CONFIG_HAS_EARLYSUSPEND)
1405 unregister_early_suspend(&data->early_suspend);
1406#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301407
1408 if (data->pdata->power_on)
1409 data->pdata->power_on(false);
1410 else
1411 mxt_power_on(data, false);
1412
1413 if (data->pdata->init_hw)
1414 data->pdata->init_hw(false);
1415 else
1416 mxt_regulator_configure(data, false);
1417
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001418 kfree(data->object_table);
1419 kfree(data);
1420
1421 return 0;
1422}
1423
Iiro Valkonen7686b102011-02-02 23:21:58 -08001424static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001425 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001426 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001427 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001428 { }
1429};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001430MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001431
Iiro Valkonen7686b102011-02-02 23:21:58 -08001432static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001433 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001434 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001435 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001436#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001437 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001438#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001439 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001440 .probe = mxt_probe,
1441 .remove = __devexit_p(mxt_remove),
1442 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001443};
1444
Iiro Valkonen7686b102011-02-02 23:21:58 -08001445static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001446{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001447 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001448}
1449
Iiro Valkonen7686b102011-02-02 23:21:58 -08001450static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001451{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001452 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001453}
1454
Iiro Valkonen7686b102011-02-02 23:21:58 -08001455module_init(mxt_init);
1456module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001457
1458/* Module information */
1459MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001460MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001461MODULE_LICENSE("GPL");