blob: 7003e3edbaaeb81d6d48f2a5d4c7af1916d580f5 [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:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530297 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700298 return true;
299 default:
300 return false;
301 }
302}
303
Iiro Valkonen7686b102011-02-02 23:21:58 -0800304static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700305{
306 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800307 case MXT_GEN_COMMAND:
308 case MXT_GEN_POWER:
309 case MXT_GEN_ACQUIRE:
310 case MXT_TOUCH_MULTI:
311 case MXT_TOUCH_KEYARRAY:
312 case MXT_TOUCH_PROXIMITY:
313 case MXT_PROCI_GRIPFACE:
314 case MXT_PROCG_NOISE:
315 case MXT_PROCI_ONETOUCH:
316 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700317 case MXT_PROCI_GRIP:
318 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800319 case MXT_SPT_GPIOPWM:
320 case MXT_SPT_SELFTEST:
321 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530322 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530323 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700324 return true;
325 default:
326 return false;
327 }
328}
329
Iiro Valkonen7686b102011-02-02 23:21:58 -0800330static void mxt_dump_message(struct device *dev,
331 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700332{
333 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
334 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
335 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
336 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
337 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
338 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
339 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
340 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
341 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
342}
343
Iiro Valkonen7686b102011-02-02 23:21:58 -0800344static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700345 unsigned int state)
346{
347 u8 val;
348
349recheck:
350 if (i2c_master_recv(client, &val, 1) != 1) {
351 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
352 return -EIO;
353 }
354
355 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800356 case MXT_WAITING_BOOTLOAD_CMD:
357 case MXT_WAITING_FRAME_DATA:
358 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700359 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800360 case MXT_FRAME_CRC_PASS:
361 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700362 goto recheck;
363 break;
364 default:
365 return -EINVAL;
366 }
367
368 if (val != state) {
369 dev_err(&client->dev, "Unvalid bootloader mode state\n");
370 return -EINVAL;
371 }
372
373 return 0;
374}
375
Iiro Valkonen7686b102011-02-02 23:21:58 -0800376static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700377{
378 u8 buf[2];
379
Iiro Valkonen7686b102011-02-02 23:21:58 -0800380 buf[0] = MXT_UNLOCK_CMD_LSB;
381 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700382
383 if (i2c_master_send(client, buf, 2) != 2) {
384 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
385 return -EIO;
386 }
387
388 return 0;
389}
390
Iiro Valkonen7686b102011-02-02 23:21:58 -0800391static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700392 const u8 *data, unsigned int frame_size)
393{
394 if (i2c_master_send(client, data, frame_size) != frame_size) {
395 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
396 return -EIO;
397 }
398
399 return 0;
400}
401
Iiro Valkonen7686b102011-02-02 23:21:58 -0800402static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700403 u16 reg, u16 len, void *val)
404{
405 struct i2c_msg xfer[2];
406 u8 buf[2];
407
408 buf[0] = reg & 0xff;
409 buf[1] = (reg >> 8) & 0xff;
410
411 /* Write register */
412 xfer[0].addr = client->addr;
413 xfer[0].flags = 0;
414 xfer[0].len = 2;
415 xfer[0].buf = buf;
416
417 /* Read data */
418 xfer[1].addr = client->addr;
419 xfer[1].flags = I2C_M_RD;
420 xfer[1].len = len;
421 xfer[1].buf = val;
422
423 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
424 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
425 return -EIO;
426 }
427
428 return 0;
429}
430
Iiro Valkonen7686b102011-02-02 23:21:58 -0800431static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700432{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800433 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700434}
435
Iiro Valkonen7686b102011-02-02 23:21:58 -0800436static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700437{
438 u8 buf[3];
439
440 buf[0] = reg & 0xff;
441 buf[1] = (reg >> 8) & 0xff;
442 buf[2] = val;
443
444 if (i2c_master_send(client, buf, 3) != 3) {
445 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
446 return -EIO;
447 }
448
449 return 0;
450}
451
Iiro Valkonen7686b102011-02-02 23:21:58 -0800452static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700453 u16 reg, u8 *object_buf)
454{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800455 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700456 object_buf);
457}
458
Iiro Valkonen7686b102011-02-02 23:21:58 -0800459static struct mxt_object *
460mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700461{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800462 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700463 int i;
464
465 for (i = 0; i < data->info.object_num; i++) {
466 object = data->object_table + i;
467 if (object->type == type)
468 return object;
469 }
470
471 dev_err(&data->client->dev, "Invalid object type\n");
472 return NULL;
473}
474
Iiro Valkonen7686b102011-02-02 23:21:58 -0800475static int mxt_read_message(struct mxt_data *data,
476 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700477{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800478 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700479 u16 reg;
480
Iiro Valkonen7686b102011-02-02 23:21:58 -0800481 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482 if (!object)
483 return -EINVAL;
484
485 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800486 return __mxt_read_reg(data->client, reg,
487 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488}
489
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491 u8 type, u8 offset, u8 *val)
492{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700494 u16 reg;
495
Iiro Valkonen7686b102011-02-02 23:21:58 -0800496 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497 if (!object)
498 return -EINVAL;
499
500 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800501 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700502}
503
Iiro Valkonen7686b102011-02-02 23:21:58 -0800504static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700505 u8 type, u8 offset, u8 val)
506{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800507 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700508 u16 reg;
509
Iiro Valkonen7686b102011-02-02 23:21:58 -0800510 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511 if (!object)
512 return -EINVAL;
513
514 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800515 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700516}
517
Iiro Valkonen7686b102011-02-02 23:21:58 -0800518static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700519{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800520 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700521 struct input_dev *input_dev = data->input_dev;
522 int status = finger[single_id].status;
523 int finger_num = 0;
524 int id;
525
Iiro Valkonen7686b102011-02-02 23:21:58 -0800526 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700527 if (!finger[id].status)
528 continue;
529
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700530 input_mt_slot(input_dev, id);
531 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
532 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700533
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700534 if (finger[id].status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700535 finger_num++;
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700536 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
537 finger[id].area);
538 input_report_abs(input_dev, ABS_MT_POSITION_X,
539 finger[id].x);
540 input_report_abs(input_dev, ABS_MT_POSITION_Y,
541 finger[id].y);
542 } else {
543 finger[id].status = 0;
544 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545 }
546
547 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
548
Iiro Valkonen7686b102011-02-02 23:21:58 -0800549 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550 input_report_abs(input_dev, ABS_X, finger[single_id].x);
551 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
552 }
553
554 input_sync(input_dev);
555}
556
Iiro Valkonen7686b102011-02-02 23:21:58 -0800557static void mxt_input_touchevent(struct mxt_data *data,
558 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700559{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561 struct device *dev = &data->client->dev;
562 u8 status = message->message[0];
563 int x;
564 int y;
565 int area;
566
567 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800568 if (!(status & MXT_DETECT)) {
569 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700570 dev_dbg(dev, "[%d] released\n", id);
571
Iiro Valkonen7686b102011-02-02 23:21:58 -0800572 finger[id].status = MXT_RELEASE;
573 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700574 }
575 return;
576 }
577
578 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580 return;
581
Joonyoung Shim910d8052011-04-12 23:14:38 -0700582 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
583 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
584 if (data->max_x < 1024)
585 x = x >> 2;
586 if (data->max_y < 1024)
587 y = y >> 2;
588
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589 area = message->message[4];
590
591 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 x, y, area);
594
Iiro Valkonen7686b102011-02-02 23:21:58 -0800595 finger[id].status = status & MXT_MOVE ?
596 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597 finger[id].x = x;
598 finger[id].y = y;
599 finger[id].area = area;
600
Iiro Valkonen7686b102011-02-02 23:21:58 -0800601 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700602}
603
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700605{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800606 struct mxt_data *data = dev_id;
607 struct mxt_message message;
608 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609 struct device *dev = &data->client->dev;
610 int id;
611 u8 reportid;
612 u8 max_reportid;
613 u8 min_reportid;
614
615 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800616 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700617 dev_err(dev, "Failed to read message\n");
618 goto end;
619 }
620
621 reportid = message.reportid;
622
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 /* whether reportid is thing of MXT_TOUCH_MULTI */
624 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625 if (!object)
626 goto end;
627
628 max_reportid = object->max_reportid;
629 min_reportid = max_reportid - object->num_report_ids + 1;
630 id = reportid - min_reportid;
631
632 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636 } while (reportid != 0xff);
637
638end:
639 return IRQ_HANDLED;
640}
641
Iiro Valkonen7686b102011-02-02 23:21:58 -0800642static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800644 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646 struct device *dev = &data->client->dev;
647 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800648 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700649
Iiro Valkonen71749f52011-02-15 13:36:52 -0800650 if (!pdata->config) {
651 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
652 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653 }
654
655 for (i = 0; i < data->info.object_num; i++) {
656 object = data->object_table + i;
657
Iiro Valkonen7686b102011-02-02 23:21:58 -0800658 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700659 continue;
660
Iiro Valkonen71749f52011-02-15 13:36:52 -0800661 for (j = 0; j < object->size + 1; j++) {
662 config_offset = index + j;
663 if (config_offset > pdata->config_length) {
664 dev_err(dev, "Not enough config data!\n");
665 return -EINVAL;
666 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800667 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800668 pdata->config[config_offset]);
669 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700670 index += object->size + 1;
671 }
672
673 return 0;
674}
675
Iiro Valkonen7686b102011-02-02 23:21:58 -0800676static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677{
678 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800679 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680 int count = 10;
681 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700682
683 /* Read dummy message to make high CHG pin */
684 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800685 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686 if (error)
687 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800688 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689
690 if (!count) {
691 dev_err(dev, "CHG pin isn't cleared\n");
692 return -EBUSY;
693 }
694
695 return 0;
696}
697
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800700 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701 u8 voltage;
702
703 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800704 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800706 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700707 pdata->y_line);
708
709 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800710 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 pdata->orient);
712
713 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800714 mxt_write_object(data, MXT_TOUCH_MULTI,
715 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716
717 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800718 mxt_write_object(data, MXT_TOUCH_MULTI,
719 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720
721 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800722 mxt_write_object(data, MXT_TOUCH_MULTI,
723 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
724 mxt_write_object(data, MXT_TOUCH_MULTI,
725 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
726 mxt_write_object(data, MXT_TOUCH_MULTI,
727 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
728 mxt_write_object(data, MXT_TOUCH_MULTI,
729 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730
731 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700732 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800733 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
734 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
735 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700736 voltage = 0xff - voltage + 1;
737 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
739 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700740
Iiro Valkonen7686b102011-02-02 23:21:58 -0800741 mxt_write_object(data, MXT_SPT_CTECONFIG,
742 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743 }
744}
745
Iiro Valkonen7686b102011-02-02 23:21:58 -0800746static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747{
748 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800749 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700750 int error;
751 u8 val;
752
Iiro Valkonen7686b102011-02-02 23:21:58 -0800753 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700754 if (error)
755 return error;
756 info->family_id = val;
757
Iiro Valkonen7686b102011-02-02 23:21:58 -0800758 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700759 if (error)
760 return error;
761 info->variant_id = val;
762
Iiro Valkonen7686b102011-02-02 23:21:58 -0800763 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700764 if (error)
765 return error;
766 info->version = val;
767
Iiro Valkonen7686b102011-02-02 23:21:58 -0800768 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700769 if (error)
770 return error;
771 info->build = val;
772
Iiro Valkonen7686b102011-02-02 23:21:58 -0800773 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700774 if (error)
775 return error;
776 info->object_num = val;
777
778 return 0;
779}
780
Iiro Valkonen7686b102011-02-02 23:21:58 -0800781static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700782{
783 int error;
784 int i;
785 u16 reg;
786 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800787 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788
789 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791
Iiro Valkonen7686b102011-02-02 23:21:58 -0800792 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
793 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794 if (error)
795 return error;
796
797 object->type = buf[0];
798 object->start_address = (buf[2] << 8) | buf[1];
799 object->size = buf[3];
800 object->instances = buf[4];
801 object->num_report_ids = buf[5];
802
803 if (object->num_report_ids) {
804 reportid += object->num_report_ids *
805 (object->instances + 1);
806 object->max_reportid = reportid;
807 }
808 }
809
810 return 0;
811}
812
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814{
815 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800816 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700817 int error;
818 u8 val;
819
Iiro Valkonen7686b102011-02-02 23:21:58 -0800820 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700821 if (error)
822 return error;
823
824 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800825 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700826 GFP_KERNEL);
827 if (!data->object_table) {
828 dev_err(&client->dev, "Failed to allocate memory\n");
829 return -ENOMEM;
830 }
831
832 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800833 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700834 if (error)
835 return error;
836
837 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800838 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700839 if (error)
840 return error;
841
Iiro Valkonen7686b102011-02-02 23:21:58 -0800842 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700843
844 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800845 mxt_write_object(data, MXT_GEN_COMMAND,
846 MXT_COMMAND_BACKUPNV,
847 MXT_BACKUP_VALUE);
848 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700849
850 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800851 mxt_write_object(data, MXT_GEN_COMMAND,
852 MXT_COMMAND_RESET, 1);
853 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700854
855 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800856 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700857 if (error)
858 return error;
859 info->matrix_xsize = val;
860
Iiro Valkonen7686b102011-02-02 23:21:58 -0800861 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700862 if (error)
863 return error;
864 info->matrix_ysize = val;
865
866 dev_info(&client->dev,
867 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
868 info->family_id, info->variant_id, info->version,
869 info->build);
870
871 dev_info(&client->dev,
872 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
873 info->matrix_xsize, info->matrix_ysize,
874 info->object_num);
875
876 return 0;
877}
878
Joonyoung Shim910d8052011-04-12 23:14:38 -0700879static void mxt_calc_resolution(struct mxt_data *data)
880{
881 unsigned int max_x = data->pdata->x_size - 1;
882 unsigned int max_y = data->pdata->y_size - 1;
883
884 if (data->pdata->orient & MXT_XY_SWITCH) {
885 data->max_x = max_y;
886 data->max_y = max_x;
887 } else {
888 data->max_x = max_x;
889 data->max_y = max_y;
890 }
891}
892
Iiro Valkonen7686b102011-02-02 23:21:58 -0800893static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700894 struct device_attribute *attr, char *buf)
895{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800896 struct mxt_data *data = dev_get_drvdata(dev);
897 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700898 int count = 0;
899 int i, j;
900 int error;
901 u8 val;
902
903 for (i = 0; i < data->info.object_num; i++) {
904 object = data->object_table + i;
905
906 count += sprintf(buf + count,
907 "Object Table Element %d(Type %d)\n",
908 i + 1, object->type);
909
Iiro Valkonen7686b102011-02-02 23:21:58 -0800910 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700911 count += sprintf(buf + count, "\n");
912 continue;
913 }
914
915 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800916 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700917 object->type, j, &val);
918 if (error)
919 return error;
920
921 count += sprintf(buf + count,
922 " Byte %d: 0x%x (%d)\n", j, val, val);
923 }
924
925 count += sprintf(buf + count, "\n");
926 }
927
928 return count;
929}
930
Iiro Valkonen7686b102011-02-02 23:21:58 -0800931static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700932{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800933 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700934 struct i2c_client *client = data->client;
935 const struct firmware *fw = NULL;
936 unsigned int frame_size;
937 unsigned int pos = 0;
938 int ret;
939
940 ret = request_firmware(&fw, fn, dev);
941 if (ret) {
942 dev_err(dev, "Unable to open firmware %s\n", fn);
943 return ret;
944 }
945
946 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800947 mxt_write_object(data, MXT_GEN_COMMAND,
948 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
949 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950
951 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800952 if (client->addr == MXT_APP_LOW)
953 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958 if (ret)
959 goto out;
960
961 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800962 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700963
964 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800965 ret = mxt_check_bootloader(client,
966 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967 if (ret)
968 goto out;
969
970 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
971
972 /* We should add 2 at frame size as the the firmware data is not
973 * included the CRC bytes.
974 */
975 frame_size += 2;
976
977 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800978 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700979
Iiro Valkonen7686b102011-02-02 23:21:58 -0800980 ret = mxt_check_bootloader(client,
981 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700982 if (ret)
983 goto out;
984
985 pos += frame_size;
986
987 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
988 }
989
990out:
991 release_firmware(fw);
992
993 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 if (client->addr == MXT_BOOT_LOW)
995 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800997 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998
999 return ret;
1000}
1001
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003 struct device_attribute *attr,
1004 const char *buf, size_t count)
1005{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001006 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 int error;
1008
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009 disable_irq(data->irq);
1010
Iiro Valkonen7686b102011-02-02 23:21:58 -08001011 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012 if (error) {
1013 dev_err(dev, "The firmware update failed(%d)\n", error);
1014 count = error;
1015 } else {
1016 dev_dbg(dev, "The firmware update succeeded\n");
1017
1018 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001019 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020
1021 kfree(data->object_table);
1022 data->object_table = NULL;
1023
Iiro Valkonen7686b102011-02-02 23:21:58 -08001024 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025 }
1026
1027 enable_irq(data->irq);
1028
Iiro Valkonen08960a02011-04-12 23:16:40 -07001029 error = mxt_make_highchg(data);
1030 if (error)
1031 return error;
1032
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001033 return count;
1034}
1035
Iiro Valkonen7686b102011-02-02 23:21:58 -08001036static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1037static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038
Iiro Valkonen7686b102011-02-02 23:21:58 -08001039static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040 &dev_attr_object.attr,
1041 &dev_attr_update_fw.attr,
1042 NULL
1043};
1044
Iiro Valkonen7686b102011-02-02 23:21:58 -08001045static const struct attribute_group mxt_attr_group = {
1046 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001047};
1048
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050{
1051 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001052 mxt_write_object(data,
1053 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054}
1055
Iiro Valkonen7686b102011-02-02 23:21:58 -08001056static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001057{
1058 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001059 mxt_write_object(data,
1060 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061}
1062
Iiro Valkonen7686b102011-02-02 23:21:58 -08001063static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001064{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001065 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001066
Iiro Valkonen7686b102011-02-02 23:21:58 -08001067 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001068
1069 return 0;
1070}
1071
Iiro Valkonen7686b102011-02-02 23:21:58 -08001072static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001073{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001074 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001075
Iiro Valkonen7686b102011-02-02 23:21:58 -08001076 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001077}
1078
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301079static int mxt_power_on(struct mxt_data *data, bool on)
1080{
1081 int rc;
1082
1083 if (on == false)
1084 goto power_off;
1085
1086 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1087 if (rc < 0) {
1088 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1089 rc);
1090 return rc;
1091 }
1092
1093 rc = regulator_enable(data->vcc);
1094 if (rc) {
1095 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1096 rc);
1097 goto error_reg_en_vcc;
1098 }
1099
1100 if (data->pdata->i2c_pull_up) {
1101 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1102 if (rc < 0) {
1103 dev_err(&data->client->dev,
1104 "Regulator set_opt failed rc=%d\n", rc);
1105 goto error_reg_opt_i2c;
1106 }
1107
1108 rc = regulator_enable(data->vcc_i2c);
1109 if (rc) {
1110 dev_err(&data->client->dev,
1111 "Regulator enable failed rc=%d\n", rc);
1112 goto error_reg_en_vcc_i2c;
1113 }
1114 }
1115
1116 msleep(50);
1117
1118 return 0;
1119
1120error_reg_en_vcc_i2c:
1121 if (data->pdata->i2c_pull_up)
1122 regulator_set_optimum_mode(data->vcc_i2c, 0);
1123error_reg_opt_i2c:
1124 regulator_disable(data->vcc);
1125error_reg_en_vcc:
1126 regulator_set_optimum_mode(data->vcc, 0);
1127 return rc;
1128
1129power_off:
1130 regulator_set_optimum_mode(data->vcc, 0);
1131 regulator_disable(data->vcc);
1132 if (data->pdata->i2c_pull_up) {
1133 regulator_set_optimum_mode(data->vcc_i2c, 0);
1134 regulator_disable(data->vcc_i2c);
1135 }
1136 msleep(50);
1137 return 0;
1138}
1139
1140static int mxt_regulator_configure(struct mxt_data *data, bool on)
1141{
1142 int rc;
1143
1144 if (on == false)
1145 goto hw_shutdown;
1146
1147 data->vcc = regulator_get(&data->client->dev, "vdd");
1148 if (IS_ERR(data->vcc)) {
1149 rc = PTR_ERR(data->vcc);
1150 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1151 rc);
1152 return rc;
1153 }
1154
1155 if (regulator_count_voltages(data->vcc) > 0) {
1156 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1157 MXT_VTG_MAX_UV);
1158 if (rc) {
1159 dev_err(&data->client->dev,
1160 "regulator set_vtg failed rc=%d\n", rc);
1161 goto error_set_vtg_vcc;
1162 }
1163 }
1164
1165 if (data->pdata->i2c_pull_up) {
1166 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1167 if (IS_ERR(data->vcc_i2c)) {
1168 rc = PTR_ERR(data->vcc_i2c);
1169 dev_err(&data->client->dev,
1170 "Regulator get failed rc=%d\n", rc);
1171 goto error_get_vtg_i2c;
1172 }
1173 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1174 rc = regulator_set_voltage(data->vcc_i2c,
1175 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1176 if (rc) {
1177 dev_err(&data->client->dev,
1178 "regulator set_vtg failed rc=%d\n", rc);
1179 goto error_set_vtg_i2c;
1180 }
1181 }
1182 }
1183
1184 return 0;
1185
1186error_set_vtg_i2c:
1187 regulator_put(data->vcc_i2c);
1188error_get_vtg_i2c:
1189 if (regulator_count_voltages(data->vcc) > 0)
1190 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1191error_set_vtg_vcc:
1192 regulator_put(data->vcc);
1193 return rc;
1194
1195hw_shutdown:
1196 if (regulator_count_voltages(data->vcc) > 0)
1197 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1198 regulator_put(data->vcc);
1199 if (data->pdata->i2c_pull_up) {
1200 if (regulator_count_voltages(data->vcc_i2c) > 0)
1201 regulator_set_voltage(data->vcc_i2c, 0,
1202 MXT_I2C_VTG_MAX_UV);
1203 regulator_put(data->vcc_i2c);
1204 }
1205 return 0;
1206}
1207
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301208#ifdef CONFIG_PM
1209static int mxt_suspend(struct device *dev)
1210{
1211 struct i2c_client *client = to_i2c_client(dev);
1212 struct mxt_data *data = i2c_get_clientdata(client);
1213 struct input_dev *input_dev = data->input_dev;
1214
1215 mutex_lock(&input_dev->mutex);
1216
1217 if (input_dev->users)
1218 mxt_stop(data);
1219
1220 mutex_unlock(&input_dev->mutex);
1221
1222 return 0;
1223}
1224
1225static int mxt_resume(struct device *dev)
1226{
1227 struct i2c_client *client = to_i2c_client(dev);
1228 struct mxt_data *data = i2c_get_clientdata(client);
1229 struct input_dev *input_dev = data->input_dev;
1230
1231 /* Soft reset */
1232 mxt_write_object(data, MXT_GEN_COMMAND,
1233 MXT_COMMAND_RESET, 1);
1234
1235 msleep(MXT_RESET_TIME);
1236
1237 mutex_lock(&input_dev->mutex);
1238
1239 if (input_dev->users)
1240 mxt_start(data);
1241
1242 mutex_unlock(&input_dev->mutex);
1243
1244 return 0;
1245}
1246
1247#if defined(CONFIG_HAS_EARLYSUSPEND)
1248static void mxt_early_suspend(struct early_suspend *h)
1249{
1250 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1251
1252 mxt_suspend(&data->client->dev);
1253}
1254
1255static void mxt_late_resume(struct early_suspend *h)
1256{
1257 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1258
1259 mxt_resume(&data->client->dev);
1260}
1261#endif
1262
1263static const struct dev_pm_ops mxt_pm_ops = {
1264#ifndef CONFIG_HAS_EARLYSUSPEND
1265 .suspend = mxt_suspend,
1266 .resume = mxt_resume,
1267#endif
1268};
1269#endif
1270
Iiro Valkonen7686b102011-02-02 23:21:58 -08001271static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001272 const struct i2c_device_id *id)
1273{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001274 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001275 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001276 struct input_dev *input_dev;
1277 int error;
1278
Iiro Valkonen919ed892011-02-15 13:36:52 -08001279 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280 return -EINVAL;
1281
Iiro Valkonen7686b102011-02-02 23:21:58 -08001282 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001283 input_dev = input_allocate_device();
1284 if (!data || !input_dev) {
1285 dev_err(&client->dev, "Failed to allocate memory\n");
1286 error = -ENOMEM;
1287 goto err_free_mem;
1288 }
1289
Iiro Valkonen7686b102011-02-02 23:21:58 -08001290 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001291 input_dev->id.bustype = BUS_I2C;
1292 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001293 input_dev->open = mxt_input_open;
1294 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001295
Joonyoung Shim910d8052011-04-12 23:14:38 -07001296 data->client = client;
1297 data->input_dev = input_dev;
1298 data->pdata = pdata;
1299 data->irq = client->irq;
1300
1301 mxt_calc_resolution(data);
1302
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001303 __set_bit(EV_ABS, input_dev->evbit);
1304 __set_bit(EV_KEY, input_dev->evbit);
1305 __set_bit(BTN_TOUCH, input_dev->keybit);
1306
1307 /* For single touch */
1308 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001309 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001310 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001311 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001312
1313 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001314 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001315 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001316 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001317 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001318 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001319 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001320 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001321
1322 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001323 i2c_set_clientdata(client, data);
1324
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301325 if (pdata->init_hw)
1326 error = pdata->init_hw(true);
1327 else
1328 error = mxt_regulator_configure(data, true);
1329 if (error) {
1330 dev_err(&client->dev, "Failed to intialize hardware\n");
1331 goto err_free_object;
1332 }
1333
1334 if (pdata->power_on)
1335 error = pdata->power_on(true);
1336 else
1337 error = mxt_power_on(data, true);
1338 if (error) {
1339 dev_err(&client->dev, "Failed to power on hardware\n");
1340 goto err_regulator_on;
1341 }
1342
Iiro Valkonen7686b102011-02-02 23:21:58 -08001343 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001344 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301345 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001346
Iiro Valkonen7686b102011-02-02 23:21:58 -08001347 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001348 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001349 if (error) {
1350 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301351 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001352 }
1353
Iiro Valkonen08960a02011-04-12 23:16:40 -07001354 error = mxt_make_highchg(data);
1355 if (error)
1356 goto err_free_irq;
1357
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358 error = input_register_device(input_dev);
1359 if (error)
1360 goto err_free_irq;
1361
Iiro Valkonen7686b102011-02-02 23:21:58 -08001362 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001363 if (error)
1364 goto err_unregister_device;
1365
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301366#if defined(CONFIG_HAS_EARLYSUSPEND)
1367 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1368 MXT_SUSPEND_LEVEL;
1369 data->early_suspend.suspend = mxt_early_suspend;
1370 data->early_suspend.resume = mxt_late_resume;
1371 register_early_suspend(&data->early_suspend);
1372#endif
1373
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001374 return 0;
1375
1376err_unregister_device:
1377 input_unregister_device(input_dev);
1378 input_dev = NULL;
1379err_free_irq:
1380 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301381err_power_on:
1382 if (pdata->power_on)
1383 pdata->power_on(false);
1384 else
1385 mxt_power_on(data, false);
1386err_regulator_on:
1387 if (pdata->init_hw)
1388 pdata->init_hw(false);
1389 else
1390 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001391err_free_object:
1392 kfree(data->object_table);
1393err_free_mem:
1394 input_free_device(input_dev);
1395 kfree(data);
1396 return error;
1397}
1398
Iiro Valkonen7686b102011-02-02 23:21:58 -08001399static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001400{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001401 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001402
Iiro Valkonen7686b102011-02-02 23:21:58 -08001403 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001404 free_irq(data->irq, data);
1405 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301406#if defined(CONFIG_HAS_EARLYSUSPEND)
1407 unregister_early_suspend(&data->early_suspend);
1408#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301409
1410 if (data->pdata->power_on)
1411 data->pdata->power_on(false);
1412 else
1413 mxt_power_on(data, false);
1414
1415 if (data->pdata->init_hw)
1416 data->pdata->init_hw(false);
1417 else
1418 mxt_regulator_configure(data, false);
1419
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001420 kfree(data->object_table);
1421 kfree(data);
1422
1423 return 0;
1424}
1425
Iiro Valkonen7686b102011-02-02 23:21:58 -08001426static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001427 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001428 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001429 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001430 { }
1431};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001432MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001433
Iiro Valkonen7686b102011-02-02 23:21:58 -08001434static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001435 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001436 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001437 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001438#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001439 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001440#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001441 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001442 .probe = mxt_probe,
1443 .remove = __devexit_p(mxt_remove),
1444 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001445};
1446
Iiro Valkonen7686b102011-02-02 23:21:58 -08001447static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001448{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001449 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001450}
1451
Iiro Valkonen7686b102011-02-02 23:21:58 -08001452static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001453{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001454 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001455}
1456
Iiro Valkonen7686b102011-02-02 23:21:58 -08001457module_init(mxt_init);
1458module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001459
1460/* Module information */
1461MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001462MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001463MODULE_LICENSE("GPL");