blob: 41f879504ea7fda162c55a7b7ffe23fe94c64d24 [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:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700321 return true;
322 default:
323 return false;
324 }
325}
326
Iiro Valkonen7686b102011-02-02 23:21:58 -0800327static void mxt_dump_message(struct device *dev,
328 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700329{
330 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
331 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
332 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
333 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
334 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
335 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
336 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
337 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
338 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
339}
340
Iiro Valkonen7686b102011-02-02 23:21:58 -0800341static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700342 unsigned int state)
343{
344 u8 val;
345
346recheck:
347 if (i2c_master_recv(client, &val, 1) != 1) {
348 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
349 return -EIO;
350 }
351
352 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800353 case MXT_WAITING_BOOTLOAD_CMD:
354 case MXT_WAITING_FRAME_DATA:
355 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700356 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800357 case MXT_FRAME_CRC_PASS:
358 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700359 goto recheck;
360 break;
361 default:
362 return -EINVAL;
363 }
364
365 if (val != state) {
366 dev_err(&client->dev, "Unvalid bootloader mode state\n");
367 return -EINVAL;
368 }
369
370 return 0;
371}
372
Iiro Valkonen7686b102011-02-02 23:21:58 -0800373static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700374{
375 u8 buf[2];
376
Iiro Valkonen7686b102011-02-02 23:21:58 -0800377 buf[0] = MXT_UNLOCK_CMD_LSB;
378 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700379
380 if (i2c_master_send(client, buf, 2) != 2) {
381 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
382 return -EIO;
383 }
384
385 return 0;
386}
387
Iiro Valkonen7686b102011-02-02 23:21:58 -0800388static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700389 const u8 *data, unsigned int frame_size)
390{
391 if (i2c_master_send(client, data, frame_size) != frame_size) {
392 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
393 return -EIO;
394 }
395
396 return 0;
397}
398
Iiro Valkonen7686b102011-02-02 23:21:58 -0800399static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700400 u16 reg, u16 len, void *val)
401{
402 struct i2c_msg xfer[2];
403 u8 buf[2];
404
405 buf[0] = reg & 0xff;
406 buf[1] = (reg >> 8) & 0xff;
407
408 /* Write register */
409 xfer[0].addr = client->addr;
410 xfer[0].flags = 0;
411 xfer[0].len = 2;
412 xfer[0].buf = buf;
413
414 /* Read data */
415 xfer[1].addr = client->addr;
416 xfer[1].flags = I2C_M_RD;
417 xfer[1].len = len;
418 xfer[1].buf = val;
419
420 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
421 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
422 return -EIO;
423 }
424
425 return 0;
426}
427
Iiro Valkonen7686b102011-02-02 23:21:58 -0800428static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700429{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800430 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700431}
432
Iiro Valkonen7686b102011-02-02 23:21:58 -0800433static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700434{
435 u8 buf[3];
436
437 buf[0] = reg & 0xff;
438 buf[1] = (reg >> 8) & 0xff;
439 buf[2] = val;
440
441 if (i2c_master_send(client, buf, 3) != 3) {
442 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
443 return -EIO;
444 }
445
446 return 0;
447}
448
Iiro Valkonen7686b102011-02-02 23:21:58 -0800449static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700450 u16 reg, u8 *object_buf)
451{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800452 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700453 object_buf);
454}
455
Iiro Valkonen7686b102011-02-02 23:21:58 -0800456static struct mxt_object *
457mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700458{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800459 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700460 int i;
461
462 for (i = 0; i < data->info.object_num; i++) {
463 object = data->object_table + i;
464 if (object->type == type)
465 return object;
466 }
467
468 dev_err(&data->client->dev, "Invalid object type\n");
469 return NULL;
470}
471
Iiro Valkonen7686b102011-02-02 23:21:58 -0800472static int mxt_read_message(struct mxt_data *data,
473 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700474{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800475 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700476 u16 reg;
477
Iiro Valkonen7686b102011-02-02 23:21:58 -0800478 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700479 if (!object)
480 return -EINVAL;
481
482 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800483 return __mxt_read_reg(data->client, reg,
484 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485}
486
Iiro Valkonen7686b102011-02-02 23:21:58 -0800487static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488 u8 type, u8 offset, u8 *val)
489{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491 u16 reg;
492
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700494 if (!object)
495 return -EINVAL;
496
497 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800498 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700499}
500
Iiro Valkonen7686b102011-02-02 23:21:58 -0800501static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700502 u8 type, u8 offset, u8 val)
503{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800504 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700505 u16 reg;
506
Iiro Valkonen7686b102011-02-02 23:21:58 -0800507 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700508 if (!object)
509 return -EINVAL;
510
511 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800512 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700513}
514
Iiro Valkonen7686b102011-02-02 23:21:58 -0800515static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700516{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800517 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700518 struct input_dev *input_dev = data->input_dev;
519 int status = finger[single_id].status;
520 int finger_num = 0;
521 int id;
522
Iiro Valkonen7686b102011-02-02 23:21:58 -0800523 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700524 if (!finger[id].status)
525 continue;
526
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700527 input_mt_slot(input_dev, id);
528 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
529 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700531 if (finger[id].status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532 finger_num++;
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700533 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
534 finger[id].area);
535 input_report_abs(input_dev, ABS_MT_POSITION_X,
536 finger[id].x);
537 input_report_abs(input_dev, ABS_MT_POSITION_Y,
538 finger[id].y);
539 } else {
540 finger[id].status = 0;
541 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542 }
543
544 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
545
Iiro Valkonen7686b102011-02-02 23:21:58 -0800546 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700547 input_report_abs(input_dev, ABS_X, finger[single_id].x);
548 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
549 }
550
551 input_sync(input_dev);
552}
553
Iiro Valkonen7686b102011-02-02 23:21:58 -0800554static void mxt_input_touchevent(struct mxt_data *data,
555 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700556{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800557 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700558 struct device *dev = &data->client->dev;
559 u8 status = message->message[0];
560 int x;
561 int y;
562 int area;
563
564 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800565 if (!(status & MXT_DETECT)) {
566 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700567 dev_dbg(dev, "[%d] released\n", id);
568
Iiro Valkonen7686b102011-02-02 23:21:58 -0800569 finger[id].status = MXT_RELEASE;
570 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700571 }
572 return;
573 }
574
575 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800576 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 return;
578
Joonyoung Shim910d8052011-04-12 23:14:38 -0700579 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
580 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
581 if (data->max_x < 1024)
582 x = x >> 2;
583 if (data->max_y < 1024)
584 y = y >> 2;
585
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586 area = message->message[4];
587
588 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590 x, y, area);
591
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 finger[id].status = status & MXT_MOVE ?
593 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594 finger[id].x = x;
595 finger[id].y = y;
596 finger[id].area = area;
597
Iiro Valkonen7686b102011-02-02 23:21:58 -0800598 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599}
600
Iiro Valkonen7686b102011-02-02 23:21:58 -0800601static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700602{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800603 struct mxt_data *data = dev_id;
604 struct mxt_message message;
605 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 struct device *dev = &data->client->dev;
607 int id;
608 u8 reportid;
609 u8 max_reportid;
610 u8 min_reportid;
611
612 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800613 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700614 dev_err(dev, "Failed to read message\n");
615 goto end;
616 }
617
618 reportid = message.reportid;
619
Iiro Valkonen7686b102011-02-02 23:21:58 -0800620 /* whether reportid is thing of MXT_TOUCH_MULTI */
621 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700622 if (!object)
623 goto end;
624
625 max_reportid = object->max_reportid;
626 min_reportid = max_reportid - object->num_report_ids + 1;
627 id = reportid - min_reportid;
628
629 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800630 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700633 } while (reportid != 0xff);
634
635end:
636 return IRQ_HANDLED;
637}
638
Iiro Valkonen7686b102011-02-02 23:21:58 -0800639static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700640{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800641 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800642 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643 struct device *dev = &data->client->dev;
644 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800645 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646
Iiro Valkonen71749f52011-02-15 13:36:52 -0800647 if (!pdata->config) {
648 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
649 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700650 }
651
652 for (i = 0; i < data->info.object_num; i++) {
653 object = data->object_table + i;
654
Iiro Valkonen7686b102011-02-02 23:21:58 -0800655 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656 continue;
657
Iiro Valkonen71749f52011-02-15 13:36:52 -0800658 for (j = 0; j < object->size + 1; j++) {
659 config_offset = index + j;
660 if (config_offset > pdata->config_length) {
661 dev_err(dev, "Not enough config data!\n");
662 return -EINVAL;
663 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800664 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800665 pdata->config[config_offset]);
666 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667 index += object->size + 1;
668 }
669
670 return 0;
671}
672
Iiro Valkonen7686b102011-02-02 23:21:58 -0800673static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674{
675 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800676 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677 int count = 10;
678 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679
680 /* Read dummy message to make high CHG pin */
681 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800682 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700683 if (error)
684 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800685 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686
687 if (!count) {
688 dev_err(dev, "CHG pin isn't cleared\n");
689 return -EBUSY;
690 }
691
692 return 0;
693}
694
Iiro Valkonen7686b102011-02-02 23:21:58 -0800695static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800697 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698 u8 voltage;
699
700 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800701 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800703 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700704 pdata->y_line);
705
706 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800707 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708 pdata->orient);
709
710 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800711 mxt_write_object(data, MXT_TOUCH_MULTI,
712 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700713
714 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800715 mxt_write_object(data, MXT_TOUCH_MULTI,
716 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700717
718 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800719 mxt_write_object(data, MXT_TOUCH_MULTI,
720 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
721 mxt_write_object(data, MXT_TOUCH_MULTI,
722 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
723 mxt_write_object(data, MXT_TOUCH_MULTI,
724 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
725 mxt_write_object(data, MXT_TOUCH_MULTI,
726 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727
728 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700729 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800730 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
731 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
732 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700733 voltage = 0xff - voltage + 1;
734 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800735 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
736 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700737
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 mxt_write_object(data, MXT_SPT_CTECONFIG,
739 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700740 }
741}
742
Iiro Valkonen7686b102011-02-02 23:21:58 -0800743static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700744{
745 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800746 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747 int error;
748 u8 val;
749
Iiro Valkonen7686b102011-02-02 23:21:58 -0800750 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700751 if (error)
752 return error;
753 info->family_id = val;
754
Iiro Valkonen7686b102011-02-02 23:21:58 -0800755 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756 if (error)
757 return error;
758 info->variant_id = val;
759
Iiro Valkonen7686b102011-02-02 23:21:58 -0800760 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761 if (error)
762 return error;
763 info->version = val;
764
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766 if (error)
767 return error;
768 info->build = val;
769
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771 if (error)
772 return error;
773 info->object_num = val;
774
775 return 0;
776}
777
Iiro Valkonen7686b102011-02-02 23:21:58 -0800778static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779{
780 int error;
781 int i;
782 u16 reg;
783 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800784 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700785
786 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800787 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788
Iiro Valkonen7686b102011-02-02 23:21:58 -0800789 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
790 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791 if (error)
792 return error;
793
794 object->type = buf[0];
795 object->start_address = (buf[2] << 8) | buf[1];
796 object->size = buf[3];
797 object->instances = buf[4];
798 object->num_report_ids = buf[5];
799
800 if (object->num_report_ids) {
801 reportid += object->num_report_ids *
802 (object->instances + 1);
803 object->max_reportid = reportid;
804 }
805 }
806
807 return 0;
808}
809
Iiro Valkonen7686b102011-02-02 23:21:58 -0800810static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811{
812 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814 int error;
815 u8 val;
816
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818 if (error)
819 return error;
820
821 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800822 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700823 GFP_KERNEL);
824 if (!data->object_table) {
825 dev_err(&client->dev, "Failed to allocate memory\n");
826 return -ENOMEM;
827 }
828
829 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800830 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831 if (error)
832 return error;
833
834 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800835 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700836 if (error)
837 return error;
838
Iiro Valkonen7686b102011-02-02 23:21:58 -0800839 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700840
841 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800842 mxt_write_object(data, MXT_GEN_COMMAND,
843 MXT_COMMAND_BACKUPNV,
844 MXT_BACKUP_VALUE);
845 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700846
847 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800848 mxt_write_object(data, MXT_GEN_COMMAND,
849 MXT_COMMAND_RESET, 1);
850 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700851
852 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800853 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700854 if (error)
855 return error;
856 info->matrix_xsize = val;
857
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700859 if (error)
860 return error;
861 info->matrix_ysize = val;
862
863 dev_info(&client->dev,
864 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
865 info->family_id, info->variant_id, info->version,
866 info->build);
867
868 dev_info(&client->dev,
869 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
870 info->matrix_xsize, info->matrix_ysize,
871 info->object_num);
872
873 return 0;
874}
875
Joonyoung Shim910d8052011-04-12 23:14:38 -0700876static void mxt_calc_resolution(struct mxt_data *data)
877{
878 unsigned int max_x = data->pdata->x_size - 1;
879 unsigned int max_y = data->pdata->y_size - 1;
880
881 if (data->pdata->orient & MXT_XY_SWITCH) {
882 data->max_x = max_y;
883 data->max_y = max_x;
884 } else {
885 data->max_x = max_x;
886 data->max_y = max_y;
887 }
888}
889
Iiro Valkonen7686b102011-02-02 23:21:58 -0800890static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700891 struct device_attribute *attr, char *buf)
892{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800893 struct mxt_data *data = dev_get_drvdata(dev);
894 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700895 int count = 0;
896 int i, j;
897 int error;
898 u8 val;
899
900 for (i = 0; i < data->info.object_num; i++) {
901 object = data->object_table + i;
902
903 count += sprintf(buf + count,
904 "Object Table Element %d(Type %d)\n",
905 i + 1, object->type);
906
Iiro Valkonen7686b102011-02-02 23:21:58 -0800907 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700908 count += sprintf(buf + count, "\n");
909 continue;
910 }
911
912 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800913 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700914 object->type, j, &val);
915 if (error)
916 return error;
917
918 count += sprintf(buf + count,
919 " Byte %d: 0x%x (%d)\n", j, val, val);
920 }
921
922 count += sprintf(buf + count, "\n");
923 }
924
925 return count;
926}
927
Iiro Valkonen7686b102011-02-02 23:21:58 -0800928static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800930 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931 struct i2c_client *client = data->client;
932 const struct firmware *fw = NULL;
933 unsigned int frame_size;
934 unsigned int pos = 0;
935 int ret;
936
937 ret = request_firmware(&fw, fn, dev);
938 if (ret) {
939 dev_err(dev, "Unable to open firmware %s\n", fn);
940 return ret;
941 }
942
943 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800944 mxt_write_object(data, MXT_GEN_COMMAND,
945 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
946 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700947
948 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949 if (client->addr == MXT_APP_LOW)
950 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700951 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800952 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953
Iiro Valkonen7686b102011-02-02 23:21:58 -0800954 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955 if (ret)
956 goto out;
957
958 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800959 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700960
961 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800962 ret = mxt_check_bootloader(client,
963 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700964 if (ret)
965 goto out;
966
967 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
968
969 /* We should add 2 at frame size as the the firmware data is not
970 * included the CRC bytes.
971 */
972 frame_size += 2;
973
974 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800975 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700976
Iiro Valkonen7686b102011-02-02 23:21:58 -0800977 ret = mxt_check_bootloader(client,
978 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700979 if (ret)
980 goto out;
981
982 pos += frame_size;
983
984 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
985 }
986
987out:
988 release_firmware(fw);
989
990 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800991 if (client->addr == MXT_BOOT_LOW)
992 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700995
996 return ret;
997}
998
Iiro Valkonen7686b102011-02-02 23:21:58 -0800999static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000 struct device_attribute *attr,
1001 const char *buf, size_t count)
1002{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001003 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001004 int error;
1005
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006 disable_irq(data->irq);
1007
Iiro Valkonen7686b102011-02-02 23:21:58 -08001008 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009 if (error) {
1010 dev_err(dev, "The firmware update failed(%d)\n", error);
1011 count = error;
1012 } else {
1013 dev_dbg(dev, "The firmware update succeeded\n");
1014
1015 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001016 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001017
1018 kfree(data->object_table);
1019 data->object_table = NULL;
1020
Iiro Valkonen7686b102011-02-02 23:21:58 -08001021 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022 }
1023
1024 enable_irq(data->irq);
1025
Iiro Valkonen08960a02011-04-12 23:16:40 -07001026 error = mxt_make_highchg(data);
1027 if (error)
1028 return error;
1029
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001030 return count;
1031}
1032
Iiro Valkonen7686b102011-02-02 23:21:58 -08001033static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1034static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035
Iiro Valkonen7686b102011-02-02 23:21:58 -08001036static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001037 &dev_attr_object.attr,
1038 &dev_attr_update_fw.attr,
1039 NULL
1040};
1041
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042static const struct attribute_group mxt_attr_group = {
1043 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001044};
1045
Iiro Valkonen7686b102011-02-02 23:21:58 -08001046static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001047{
1048 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049 mxt_write_object(data,
1050 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001051}
1052
Iiro Valkonen7686b102011-02-02 23:21:58 -08001053static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054{
1055 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001056 mxt_write_object(data,
1057 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001058}
1059
Iiro Valkonen7686b102011-02-02 23:21:58 -08001060static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001062 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063
Iiro Valkonen7686b102011-02-02 23:21:58 -08001064 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065
1066 return 0;
1067}
1068
Iiro Valkonen7686b102011-02-02 23:21:58 -08001069static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001070{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001071 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074}
1075
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301076static int mxt_power_on(struct mxt_data *data, bool on)
1077{
1078 int rc;
1079
1080 if (on == false)
1081 goto power_off;
1082
1083 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1084 if (rc < 0) {
1085 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1086 rc);
1087 return rc;
1088 }
1089
1090 rc = regulator_enable(data->vcc);
1091 if (rc) {
1092 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1093 rc);
1094 goto error_reg_en_vcc;
1095 }
1096
1097 if (data->pdata->i2c_pull_up) {
1098 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1099 if (rc < 0) {
1100 dev_err(&data->client->dev,
1101 "Regulator set_opt failed rc=%d\n", rc);
1102 goto error_reg_opt_i2c;
1103 }
1104
1105 rc = regulator_enable(data->vcc_i2c);
1106 if (rc) {
1107 dev_err(&data->client->dev,
1108 "Regulator enable failed rc=%d\n", rc);
1109 goto error_reg_en_vcc_i2c;
1110 }
1111 }
1112
1113 msleep(50);
1114
1115 return 0;
1116
1117error_reg_en_vcc_i2c:
1118 if (data->pdata->i2c_pull_up)
1119 regulator_set_optimum_mode(data->vcc_i2c, 0);
1120error_reg_opt_i2c:
1121 regulator_disable(data->vcc);
1122error_reg_en_vcc:
1123 regulator_set_optimum_mode(data->vcc, 0);
1124 return rc;
1125
1126power_off:
1127 regulator_set_optimum_mode(data->vcc, 0);
1128 regulator_disable(data->vcc);
1129 if (data->pdata->i2c_pull_up) {
1130 regulator_set_optimum_mode(data->vcc_i2c, 0);
1131 regulator_disable(data->vcc_i2c);
1132 }
1133 msleep(50);
1134 return 0;
1135}
1136
1137static int mxt_regulator_configure(struct mxt_data *data, bool on)
1138{
1139 int rc;
1140
1141 if (on == false)
1142 goto hw_shutdown;
1143
1144 data->vcc = regulator_get(&data->client->dev, "vdd");
1145 if (IS_ERR(data->vcc)) {
1146 rc = PTR_ERR(data->vcc);
1147 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1148 rc);
1149 return rc;
1150 }
1151
1152 if (regulator_count_voltages(data->vcc) > 0) {
1153 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1154 MXT_VTG_MAX_UV);
1155 if (rc) {
1156 dev_err(&data->client->dev,
1157 "regulator set_vtg failed rc=%d\n", rc);
1158 goto error_set_vtg_vcc;
1159 }
1160 }
1161
1162 if (data->pdata->i2c_pull_up) {
1163 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1164 if (IS_ERR(data->vcc_i2c)) {
1165 rc = PTR_ERR(data->vcc_i2c);
1166 dev_err(&data->client->dev,
1167 "Regulator get failed rc=%d\n", rc);
1168 goto error_get_vtg_i2c;
1169 }
1170 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1171 rc = regulator_set_voltage(data->vcc_i2c,
1172 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1173 if (rc) {
1174 dev_err(&data->client->dev,
1175 "regulator set_vtg failed rc=%d\n", rc);
1176 goto error_set_vtg_i2c;
1177 }
1178 }
1179 }
1180
1181 return 0;
1182
1183error_set_vtg_i2c:
1184 regulator_put(data->vcc_i2c);
1185error_get_vtg_i2c:
1186 if (regulator_count_voltages(data->vcc) > 0)
1187 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1188error_set_vtg_vcc:
1189 regulator_put(data->vcc);
1190 return rc;
1191
1192hw_shutdown:
1193 if (regulator_count_voltages(data->vcc) > 0)
1194 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1195 regulator_put(data->vcc);
1196 if (data->pdata->i2c_pull_up) {
1197 if (regulator_count_voltages(data->vcc_i2c) > 0)
1198 regulator_set_voltage(data->vcc_i2c, 0,
1199 MXT_I2C_VTG_MAX_UV);
1200 regulator_put(data->vcc_i2c);
1201 }
1202 return 0;
1203}
1204
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301205#ifdef CONFIG_PM
1206static int mxt_suspend(struct device *dev)
1207{
1208 struct i2c_client *client = to_i2c_client(dev);
1209 struct mxt_data *data = i2c_get_clientdata(client);
1210 struct input_dev *input_dev = data->input_dev;
1211
1212 mutex_lock(&input_dev->mutex);
1213
1214 if (input_dev->users)
1215 mxt_stop(data);
1216
1217 mutex_unlock(&input_dev->mutex);
1218
1219 return 0;
1220}
1221
1222static int mxt_resume(struct device *dev)
1223{
1224 struct i2c_client *client = to_i2c_client(dev);
1225 struct mxt_data *data = i2c_get_clientdata(client);
1226 struct input_dev *input_dev = data->input_dev;
1227
1228 /* Soft reset */
1229 mxt_write_object(data, MXT_GEN_COMMAND,
1230 MXT_COMMAND_RESET, 1);
1231
1232 msleep(MXT_RESET_TIME);
1233
1234 mutex_lock(&input_dev->mutex);
1235
1236 if (input_dev->users)
1237 mxt_start(data);
1238
1239 mutex_unlock(&input_dev->mutex);
1240
1241 return 0;
1242}
1243
1244#if defined(CONFIG_HAS_EARLYSUSPEND)
1245static void mxt_early_suspend(struct early_suspend *h)
1246{
1247 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1248
1249 mxt_suspend(&data->client->dev);
1250}
1251
1252static void mxt_late_resume(struct early_suspend *h)
1253{
1254 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1255
1256 mxt_resume(&data->client->dev);
1257}
1258#endif
1259
1260static const struct dev_pm_ops mxt_pm_ops = {
1261#ifndef CONFIG_HAS_EARLYSUSPEND
1262 .suspend = mxt_suspend,
1263 .resume = mxt_resume,
1264#endif
1265};
1266#endif
1267
Iiro Valkonen7686b102011-02-02 23:21:58 -08001268static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269 const struct i2c_device_id *id)
1270{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001271 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001272 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273 struct input_dev *input_dev;
1274 int error;
1275
Iiro Valkonen919ed892011-02-15 13:36:52 -08001276 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001277 return -EINVAL;
1278
Iiro Valkonen7686b102011-02-02 23:21:58 -08001279 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280 input_dev = input_allocate_device();
1281 if (!data || !input_dev) {
1282 dev_err(&client->dev, "Failed to allocate memory\n");
1283 error = -ENOMEM;
1284 goto err_free_mem;
1285 }
1286
Iiro Valkonen7686b102011-02-02 23:21:58 -08001287 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001288 input_dev->id.bustype = BUS_I2C;
1289 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001290 input_dev->open = mxt_input_open;
1291 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001292
Joonyoung Shim910d8052011-04-12 23:14:38 -07001293 data->client = client;
1294 data->input_dev = input_dev;
1295 data->pdata = pdata;
1296 data->irq = client->irq;
1297
1298 mxt_calc_resolution(data);
1299
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001300 __set_bit(EV_ABS, input_dev->evbit);
1301 __set_bit(EV_KEY, input_dev->evbit);
1302 __set_bit(BTN_TOUCH, input_dev->keybit);
1303
1304 /* For single touch */
1305 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001306 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001307 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001308 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001309
1310 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001311 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001312 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001313 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001314 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001315 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001316 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001317 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001318
1319 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001320 i2c_set_clientdata(client, data);
1321
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301322 if (pdata->init_hw)
1323 error = pdata->init_hw(true);
1324 else
1325 error = mxt_regulator_configure(data, true);
1326 if (error) {
1327 dev_err(&client->dev, "Failed to intialize hardware\n");
1328 goto err_free_object;
1329 }
1330
1331 if (pdata->power_on)
1332 error = pdata->power_on(true);
1333 else
1334 error = mxt_power_on(data, true);
1335 if (error) {
1336 dev_err(&client->dev, "Failed to power on hardware\n");
1337 goto err_regulator_on;
1338 }
1339
Iiro Valkonen7686b102011-02-02 23:21:58 -08001340 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001341 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301342 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001343
Iiro Valkonen7686b102011-02-02 23:21:58 -08001344 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001345 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001346 if (error) {
1347 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301348 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001349 }
1350
Iiro Valkonen08960a02011-04-12 23:16:40 -07001351 error = mxt_make_highchg(data);
1352 if (error)
1353 goto err_free_irq;
1354
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001355 error = input_register_device(input_dev);
1356 if (error)
1357 goto err_free_irq;
1358
Iiro Valkonen7686b102011-02-02 23:21:58 -08001359 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001360 if (error)
1361 goto err_unregister_device;
1362
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301363#if defined(CONFIG_HAS_EARLYSUSPEND)
1364 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1365 MXT_SUSPEND_LEVEL;
1366 data->early_suspend.suspend = mxt_early_suspend;
1367 data->early_suspend.resume = mxt_late_resume;
1368 register_early_suspend(&data->early_suspend);
1369#endif
1370
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001371 return 0;
1372
1373err_unregister_device:
1374 input_unregister_device(input_dev);
1375 input_dev = NULL;
1376err_free_irq:
1377 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301378err_power_on:
1379 if (pdata->power_on)
1380 pdata->power_on(false);
1381 else
1382 mxt_power_on(data, false);
1383err_regulator_on:
1384 if (pdata->init_hw)
1385 pdata->init_hw(false);
1386 else
1387 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001388err_free_object:
1389 kfree(data->object_table);
1390err_free_mem:
1391 input_free_device(input_dev);
1392 kfree(data);
1393 return error;
1394}
1395
Iiro Valkonen7686b102011-02-02 23:21:58 -08001396static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001397{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001398 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001399
Iiro Valkonen7686b102011-02-02 23:21:58 -08001400 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001401 free_irq(data->irq, data);
1402 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301403#if defined(CONFIG_HAS_EARLYSUSPEND)
1404 unregister_early_suspend(&data->early_suspend);
1405#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301406
1407 if (data->pdata->power_on)
1408 data->pdata->power_on(false);
1409 else
1410 mxt_power_on(data, false);
1411
1412 if (data->pdata->init_hw)
1413 data->pdata->init_hw(false);
1414 else
1415 mxt_regulator_configure(data, false);
1416
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001417 kfree(data->object_table);
1418 kfree(data);
1419
1420 return 0;
1421}
1422
Iiro Valkonen7686b102011-02-02 23:21:58 -08001423static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001424 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001425 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001426 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001427 { }
1428};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001429MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001430
Iiro Valkonen7686b102011-02-02 23:21:58 -08001431static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001432 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001433 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001434 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001435#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001436 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001437#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001438 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001439 .probe = mxt_probe,
1440 .remove = __devexit_p(mxt_remove),
1441 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001442};
1443
Iiro Valkonen7686b102011-02-02 23:21:58 -08001444static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001445{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001446 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001447}
1448
Iiro Valkonen7686b102011-02-02 23:21:58 -08001449static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001450{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001451 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001452}
1453
Iiro Valkonen7686b102011-02-02 23:21:58 -08001454module_init(mxt_init);
1455module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001456
1457/* Module information */
1458MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001459MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001460MODULE_LICENSE("GPL");