blob: 0865cc259f79e53db8ce7638bc585582b7ca3ee6 [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
Jing Lin2f863172011-10-17 10:56:58 -07006 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/firmware.h>
19#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080020#include <linux/i2c/atmel_mxt_ts.h>
Amy Maloche2b59bab2011-10-13 16:08:16 -070021#include <linux/input.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070022#include <linux/interrupt.h>
23#include <linux/slab.h>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053024#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070025
Anirudh Ghayal253ce122011-08-09 19:32:57 +053026#if defined(CONFIG_HAS_EARLYSUSPEND)
27#include <linux/earlysuspend.h>
28/* Early-suspend level */
29#define MXT_SUSPEND_LEVEL 1
30#endif
31
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070032/* Family ID */
33#define MXT224_ID 0x80
34#define MXT1386_ID 0xA0
35
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070036/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_VER_20 20
38#define MXT_VER_21 21
39#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070040
41/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080042#define MXT_APP_LOW 0x4a
43#define MXT_APP_HIGH 0x4b
44#define MXT_BOOT_LOW 0x24
45#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070046
47/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080048#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
50/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080051#define MXT_FAMILY_ID 0x00
52#define MXT_VARIANT_ID 0x01
53#define MXT_VERSION 0x02
54#define MXT_BUILD 0x03
55#define MXT_MATRIX_X_SIZE 0x04
56#define MXT_MATRIX_Y_SIZE 0x05
57#define MXT_OBJECT_NUM 0x06
58#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070059
Iiro Valkonen7686b102011-02-02 23:21:58 -080060#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070061
62/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080063#define MXT_DEBUG_DIAGNOSTIC 37
64#define MXT_GEN_MESSAGE 5
65#define MXT_GEN_COMMAND 6
66#define MXT_GEN_POWER 7
67#define MXT_GEN_ACQUIRE 8
68#define MXT_TOUCH_MULTI 9
69#define MXT_TOUCH_KEYARRAY 15
70#define MXT_TOUCH_PROXIMITY 23
71#define MXT_PROCI_GRIPFACE 20
72#define MXT_PROCG_NOISE 22
73#define MXT_PROCI_ONETOUCH 24
74#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070075#define MXT_PROCI_GRIP 40
76#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070077#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080078#define MXT_SPT_GPIOPWM 19
79#define MXT_SPT_SELFTEST 25
80#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070081#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070082#define MXT_SPT_DIGITIZER 43
83#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070084
Iiro Valkonen7686b102011-02-02 23:21:58 -080085/* MXT_GEN_COMMAND field */
86#define MXT_COMMAND_RESET 0
87#define MXT_COMMAND_BACKUPNV 1
88#define MXT_COMMAND_CALIBRATE 2
89#define MXT_COMMAND_REPORTALL 3
90#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen7686b102011-02-02 23:21:58 -080092/* MXT_GEN_POWER field */
93#define MXT_POWER_IDLEACQINT 0
94#define MXT_POWER_ACTVACQINT 1
95#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070096
Iiro Valkonen7686b102011-02-02 23:21:58 -080097/* MXT_GEN_ACQUIRE field */
98#define MXT_ACQUIRE_CHRGTIME 0
99#define MXT_ACQUIRE_TCHDRIFT 2
100#define MXT_ACQUIRE_DRIFTST 3
101#define MXT_ACQUIRE_TCHAUTOCAL 4
102#define MXT_ACQUIRE_SYNC 5
103#define MXT_ACQUIRE_ATCHCALST 6
104#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700105
Iiro Valkonen7686b102011-02-02 23:21:58 -0800106/* MXT_TOUCH_MULTI field */
107#define MXT_TOUCH_CTRL 0
108#define MXT_TOUCH_XORIGIN 1
109#define MXT_TOUCH_YORIGIN 2
110#define MXT_TOUCH_XSIZE 3
111#define MXT_TOUCH_YSIZE 4
112#define MXT_TOUCH_BLEN 6
113#define MXT_TOUCH_TCHTHR 7
114#define MXT_TOUCH_TCHDI 8
115#define MXT_TOUCH_ORIENT 9
116#define MXT_TOUCH_MOVHYSTI 11
117#define MXT_TOUCH_MOVHYSTN 12
118#define MXT_TOUCH_NUMTOUCH 14
119#define MXT_TOUCH_MRGHYST 15
120#define MXT_TOUCH_MRGTHR 16
121#define MXT_TOUCH_AMPHYST 17
122#define MXT_TOUCH_XRANGE_LSB 18
123#define MXT_TOUCH_XRANGE_MSB 19
124#define MXT_TOUCH_YRANGE_LSB 20
125#define MXT_TOUCH_YRANGE_MSB 21
126#define MXT_TOUCH_XLOCLIP 22
127#define MXT_TOUCH_XHICLIP 23
128#define MXT_TOUCH_YLOCLIP 24
129#define MXT_TOUCH_YHICLIP 25
130#define MXT_TOUCH_XEDGECTRL 26
131#define MXT_TOUCH_XEDGEDIST 27
132#define MXT_TOUCH_YEDGECTRL 28
133#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700134#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700135
Iiro Valkonen7686b102011-02-02 23:21:58 -0800136/* MXT_PROCI_GRIPFACE field */
137#define MXT_GRIPFACE_CTRL 0
138#define MXT_GRIPFACE_XLOGRIP 1
139#define MXT_GRIPFACE_XHIGRIP 2
140#define MXT_GRIPFACE_YLOGRIP 3
141#define MXT_GRIPFACE_YHIGRIP 4
142#define MXT_GRIPFACE_MAXTCHS 5
143#define MXT_GRIPFACE_SZTHR1 7
144#define MXT_GRIPFACE_SZTHR2 8
145#define MXT_GRIPFACE_SHPTHR1 9
146#define MXT_GRIPFACE_SHPTHR2 10
147#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700148
Iiro Valkonen7686b102011-02-02 23:21:58 -0800149/* MXT_PROCI_NOISE field */
150#define MXT_NOISE_CTRL 0
151#define MXT_NOISE_OUTFLEN 1
152#define MXT_NOISE_GCAFUL_LSB 3
153#define MXT_NOISE_GCAFUL_MSB 4
154#define MXT_NOISE_GCAFLL_LSB 5
155#define MXT_NOISE_GCAFLL_MSB 6
156#define MXT_NOISE_ACTVGCAFVALID 7
157#define MXT_NOISE_NOISETHR 8
158#define MXT_NOISE_FREQHOPSCALE 10
159#define MXT_NOISE_FREQ0 11
160#define MXT_NOISE_FREQ1 12
161#define MXT_NOISE_FREQ2 13
162#define MXT_NOISE_FREQ3 14
163#define MXT_NOISE_FREQ4 15
164#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700165
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166/* MXT_SPT_COMMSCONFIG */
167#define MXT_COMMS_CTRL 0
168#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700169
Iiro Valkonen7686b102011-02-02 23:21:58 -0800170/* MXT_SPT_CTECONFIG field */
171#define MXT_CTE_CTRL 0
172#define MXT_CTE_CMD 1
173#define MXT_CTE_MODE 2
174#define MXT_CTE_IDLEGCAFDEPTH 3
175#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700176#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700177
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_VOLTAGE_DEFAULT 2700000
179#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700180
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530181#define MXT_VTG_MIN_UV 2700000
182#define MXT_VTG_MAX_UV 3300000
183#define MXT_ACTIVE_LOAD_UA 15000
184
185#define MXT_I2C_VTG_MIN_UV 1800000
186#define MXT_I2C_VTG_MAX_UV 1800000
187#define MXT_I2C_LOAD_UA 10000
188
Iiro Valkonen7686b102011-02-02 23:21:58 -0800189/* Define for MXT_GEN_COMMAND */
190#define MXT_BOOT_VALUE 0xa5
191#define MXT_BACKUP_VALUE 0x55
192#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700193#define MXT224_RESET_TIME 65 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700194#define MXT1386_RESET_TIME 250 /* msec */
195#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700196#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700197
Iiro Valkonen7686b102011-02-02 23:21:58 -0800198#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
200/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800201#define MXT_UNLOCK_CMD_MSB 0xaa
202#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700203
204/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800205#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
206#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
207#define MXT_FRAME_CRC_CHECK 0x02
208#define MXT_FRAME_CRC_FAIL 0x03
209#define MXT_FRAME_CRC_PASS 0x04
210#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
211#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700212
213/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800214#define MXT_SUPPRESS (1 << 1)
215#define MXT_AMP (1 << 2)
216#define MXT_VECTOR (1 << 3)
217#define MXT_MOVE (1 << 4)
218#define MXT_RELEASE (1 << 5)
219#define MXT_PRESS (1 << 6)
220#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700221
Joonyoung Shim910d8052011-04-12 23:14:38 -0700222/* Touch orient bits */
223#define MXT_XY_SWITCH (1 << 0)
224#define MXT_X_INVERT (1 << 1)
225#define MXT_Y_INVERT (1 << 2)
226
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700227/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800228#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700229
Iiro Valkonen7686b102011-02-02 23:21:58 -0800230#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530232#define MXT_BUFF_SIZE 100
Amy Maloche52262212011-09-15 16:46:57 -0700233#define T7_DATA_SIZE 3
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530234
Iiro Valkonen7686b102011-02-02 23:21:58 -0800235struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700236 u8 family_id;
237 u8 variant_id;
238 u8 version;
239 u8 build;
240 u8 matrix_xsize;
241 u8 matrix_ysize;
242 u8 object_num;
243};
244
Iiro Valkonen7686b102011-02-02 23:21:58 -0800245struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700246 u8 type;
247 u16 start_address;
248 u8 size;
249 u8 instances;
250 u8 num_report_ids;
251
252 /* to map object and message */
253 u8 max_reportid;
254};
255
Iiro Valkonen7686b102011-02-02 23:21:58 -0800256struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700257 u8 reportid;
258 u8 message[7];
259 u8 checksum;
260};
261
Iiro Valkonen7686b102011-02-02 23:21:58 -0800262struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700263 int status;
264 int x;
265 int y;
266 int area;
267};
268
269/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800270struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700271 struct i2c_client *client;
272 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800273 const struct mxt_platform_data *pdata;
274 struct mxt_object *object_table;
275 struct mxt_info info;
276 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700277 unsigned int irq;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530278 struct regulator *vcc;
279 struct regulator *vcc_i2c;
Anirudh Ghayal253ce122011-08-09 19:32:57 +0530280#if defined(CONFIG_HAS_EARLYSUSPEND)
281 struct early_suspend early_suspend;
282#endif
Amy Maloche52262212011-09-15 16:46:57 -0700283 u8 t7_data[T7_DATA_SIZE];
284 u8 t9_ctrl;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700285};
286
Iiro Valkonen7686b102011-02-02 23:21:58 -0800287static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700288{
289 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800290 case MXT_GEN_MESSAGE:
291 case MXT_GEN_COMMAND:
292 case MXT_GEN_POWER:
293 case MXT_GEN_ACQUIRE:
294 case MXT_TOUCH_MULTI:
295 case MXT_TOUCH_KEYARRAY:
296 case MXT_TOUCH_PROXIMITY:
297 case MXT_PROCI_GRIPFACE:
298 case MXT_PROCG_NOISE:
299 case MXT_PROCI_ONETOUCH:
300 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700301 case MXT_PROCI_GRIP:
302 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800303 case MXT_SPT_COMMSCONFIG:
304 case MXT_SPT_GPIOPWM:
305 case MXT_SPT_SELFTEST:
306 case MXT_SPT_CTECONFIG:
307 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530308 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700309 return true;
310 default:
311 return false;
312 }
313}
314
Iiro Valkonen7686b102011-02-02 23:21:58 -0800315static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700316{
317 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800318 case MXT_GEN_COMMAND:
319 case MXT_GEN_POWER:
320 case MXT_GEN_ACQUIRE:
321 case MXT_TOUCH_MULTI:
322 case MXT_TOUCH_KEYARRAY:
323 case MXT_TOUCH_PROXIMITY:
324 case MXT_PROCI_GRIPFACE:
325 case MXT_PROCG_NOISE:
326 case MXT_PROCI_ONETOUCH:
327 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700328 case MXT_PROCI_GRIP:
329 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800330 case MXT_SPT_GPIOPWM:
331 case MXT_SPT_SELFTEST:
332 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530333 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530334 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700335 return true;
336 default:
337 return false;
338 }
339}
340
Iiro Valkonen7686b102011-02-02 23:21:58 -0800341static void mxt_dump_message(struct device *dev,
342 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700343{
344 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
345 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
346 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
347 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
348 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
349 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
350 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
351 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
352 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
353}
354
Iiro Valkonen7686b102011-02-02 23:21:58 -0800355static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700356 unsigned int state)
357{
358 u8 val;
359
360recheck:
361 if (i2c_master_recv(client, &val, 1) != 1) {
362 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
363 return -EIO;
364 }
365
366 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800367 case MXT_WAITING_BOOTLOAD_CMD:
368 case MXT_WAITING_FRAME_DATA:
369 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700370 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800371 case MXT_FRAME_CRC_PASS:
372 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700373 goto recheck;
374 break;
375 default:
376 return -EINVAL;
377 }
378
379 if (val != state) {
380 dev_err(&client->dev, "Unvalid bootloader mode state\n");
381 return -EINVAL;
382 }
383
384 return 0;
385}
386
Iiro Valkonen7686b102011-02-02 23:21:58 -0800387static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700388{
389 u8 buf[2];
390
Iiro Valkonen7686b102011-02-02 23:21:58 -0800391 buf[0] = MXT_UNLOCK_CMD_LSB;
392 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700393
394 if (i2c_master_send(client, buf, 2) != 2) {
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_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700403 const u8 *data, unsigned int frame_size)
404{
405 if (i2c_master_send(client, data, frame_size) != frame_size) {
406 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
407 return -EIO;
408 }
409
410 return 0;
411}
412
Iiro Valkonen7686b102011-02-02 23:21:58 -0800413static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700414 u16 reg, u16 len, void *val)
415{
416 struct i2c_msg xfer[2];
417 u8 buf[2];
418
419 buf[0] = reg & 0xff;
420 buf[1] = (reg >> 8) & 0xff;
421
422 /* Write register */
423 xfer[0].addr = client->addr;
424 xfer[0].flags = 0;
425 xfer[0].len = 2;
426 xfer[0].buf = buf;
427
428 /* Read data */
429 xfer[1].addr = client->addr;
430 xfer[1].flags = I2C_M_RD;
431 xfer[1].len = len;
432 xfer[1].buf = val;
433
434 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
435 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
436 return -EIO;
437 }
438
439 return 0;
440}
441
Iiro Valkonen7686b102011-02-02 23:21:58 -0800442static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700443{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800444 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700445}
446
Iiro Valkonen7686b102011-02-02 23:21:58 -0800447static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700448{
449 u8 buf[3];
450
451 buf[0] = reg & 0xff;
452 buf[1] = (reg >> 8) & 0xff;
453 buf[2] = val;
454
455 if (i2c_master_send(client, buf, 3) != 3) {
456 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
457 return -EIO;
458 }
459
460 return 0;
461}
462
Iiro Valkonen7686b102011-02-02 23:21:58 -0800463static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700464 u16 reg, u8 *object_buf)
465{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800466 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700467 object_buf);
468}
469
Iiro Valkonen7686b102011-02-02 23:21:58 -0800470static struct mxt_object *
471mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700472{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800473 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700474 int i;
475
476 for (i = 0; i < data->info.object_num; i++) {
477 object = data->object_table + i;
478 if (object->type == type)
479 return object;
480 }
481
482 dev_err(&data->client->dev, "Invalid object type\n");
483 return NULL;
484}
485
Iiro Valkonen7686b102011-02-02 23:21:58 -0800486static int mxt_read_message(struct mxt_data *data,
487 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800489 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700490 u16 reg;
491
Iiro Valkonen7686b102011-02-02 23:21:58 -0800492 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700493 if (!object)
494 return -EINVAL;
495
496 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800497 return __mxt_read_reg(data->client, reg,
498 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700499}
500
Iiro Valkonen7686b102011-02-02 23:21:58 -0800501static int mxt_read_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_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700513}
514
Iiro Valkonen7686b102011-02-02 23:21:58 -0800515static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700516 u8 type, u8 offset, u8 val)
517{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800518 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700519 u16 reg;
520
Iiro Valkonen7686b102011-02-02 23:21:58 -0800521 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522 if (!object)
523 return -EINVAL;
524
525 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800526 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700527}
528
Iiro Valkonen7686b102011-02-02 23:21:58 -0800529static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800531 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532 struct input_dev *input_dev = data->input_dev;
533 int status = finger[single_id].status;
534 int finger_num = 0;
535 int id;
536
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538 if (!finger[id].status)
539 continue;
540
Amy Maloche2b59bab2011-10-13 16:08:16 -0700541 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
542 finger[id].status != MXT_RELEASE ?
543 finger[id].area : 0);
544 input_report_abs(input_dev, ABS_MT_POSITION_X,
545 finger[id].x);
546 input_report_abs(input_dev, ABS_MT_POSITION_Y,
547 finger[id].y);
548 input_mt_sync(input_dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700549
Amy Maloche2b59bab2011-10-13 16:08:16 -0700550 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700551 finger[id].status = 0;
Amy Maloche2b59bab2011-10-13 16:08:16 -0700552 else
553 finger_num++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554 }
555
556 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
557
Iiro Valkonen7686b102011-02-02 23:21:58 -0800558 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700559 input_report_abs(input_dev, ABS_X, finger[single_id].x);
560 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
561 }
562
563 input_sync(input_dev);
564}
565
Iiro Valkonen7686b102011-02-02 23:21:58 -0800566static void mxt_input_touchevent(struct mxt_data *data,
567 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700568{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800569 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700570 struct device *dev = &data->client->dev;
571 u8 status = message->message[0];
572 int x;
573 int y;
574 int area;
575
576 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800577 if (!(status & MXT_DETECT)) {
578 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700579 dev_dbg(dev, "[%d] released\n", id);
580
Iiro Valkonen7686b102011-02-02 23:21:58 -0800581 finger[id].status = MXT_RELEASE;
582 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700583 }
584 return;
585 }
586
587 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800588 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589 return;
590
Joonyoung Shim910d8052011-04-12 23:14:38 -0700591 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
592 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Jing Lin2f863172011-10-17 10:56:58 -0700593 if (data->pdata->x_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700594 x = x >> 2;
Jing Lin2f863172011-10-17 10:56:58 -0700595 if (data->pdata->y_size < 1024)
Joonyoung Shim910d8052011-04-12 23:14:38 -0700596 y = y >> 2;
597
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700598 area = message->message[4];
599
600 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800601 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700602 x, y, area);
603
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604 finger[id].status = status & MXT_MOVE ?
605 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 finger[id].x = x;
607 finger[id].y = y;
608 finger[id].area = area;
609
Iiro Valkonen7686b102011-02-02 23:21:58 -0800610 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611}
612
Iiro Valkonen7686b102011-02-02 23:21:58 -0800613static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700614{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800615 struct mxt_data *data = dev_id;
616 struct mxt_message message;
617 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700618 struct device *dev = &data->client->dev;
619 int id;
620 u8 reportid;
621 u8 max_reportid;
622 u8 min_reportid;
623
624 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800625 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700626 dev_err(dev, "Failed to read message\n");
627 goto end;
628 }
629
630 reportid = message.reportid;
631
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632 /* whether reportid is thing of MXT_TOUCH_MULTI */
633 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634 if (!object)
635 goto end;
636
637 max_reportid = object->max_reportid;
638 min_reportid = max_reportid - object->num_report_ids + 1;
639 id = reportid - min_reportid;
640
641 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800642 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800644 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700645 } while (reportid != 0xff);
646
647end:
648 return IRQ_HANDLED;
649}
650
Iiro Valkonen7686b102011-02-02 23:21:58 -0800651static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700652{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800653 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800654 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700655 struct device *dev = &data->client->dev;
656 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800657 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658
Iiro Valkonen71749f52011-02-15 13:36:52 -0800659 if (!pdata->config) {
660 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
661 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662 }
663
664 for (i = 0; i < data->info.object_num; i++) {
665 object = data->object_table + i;
666
Iiro Valkonen7686b102011-02-02 23:21:58 -0800667 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 continue;
669
Iiro Valkonen71749f52011-02-15 13:36:52 -0800670 for (j = 0; j < object->size + 1; j++) {
671 config_offset = index + j;
672 if (config_offset > pdata->config_length) {
673 dev_err(dev, "Not enough config data!\n");
674 return -EINVAL;
675 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800676 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800677 pdata->config[config_offset]);
678 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679 index += object->size + 1;
680 }
681
682 return 0;
683}
684
Iiro Valkonen7686b102011-02-02 23:21:58 -0800685static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686{
687 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800688 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689 int count = 10;
690 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691
692 /* Read dummy message to make high CHG pin */
693 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800694 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700695 if (error)
696 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800697 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698
699 if (!count) {
700 dev_err(dev, "CHG pin isn't cleared\n");
701 return -EBUSY;
702 }
703
704 return 0;
705}
706
Iiro Valkonen7686b102011-02-02 23:21:58 -0800707static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708{
709 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800710 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 int error;
712 u8 val;
713
Iiro Valkonen7686b102011-02-02 23:21:58 -0800714 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715 if (error)
716 return error;
717 info->family_id = val;
718
Iiro Valkonen7686b102011-02-02 23:21:58 -0800719 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720 if (error)
721 return error;
722 info->variant_id = val;
723
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725 if (error)
726 return error;
727 info->version = val;
728
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730 if (error)
731 return error;
732 info->build = val;
733
Iiro Valkonen7686b102011-02-02 23:21:58 -0800734 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700735 if (error)
736 return error;
737 info->object_num = val;
738
739 return 0;
740}
741
Iiro Valkonen7686b102011-02-02 23:21:58 -0800742static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743{
744 int error;
745 int i;
746 u16 reg;
747 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800748 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749
750 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800751 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752
Iiro Valkonen7686b102011-02-02 23:21:58 -0800753 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
754 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700755 if (error)
756 return error;
757
758 object->type = buf[0];
759 object->start_address = (buf[2] << 8) | buf[1];
760 object->size = buf[3];
761 object->instances = buf[4];
762 object->num_report_ids = buf[5];
763
764 if (object->num_report_ids) {
765 reportid += object->num_report_ids *
766 (object->instances + 1);
767 object->max_reportid = reportid;
768 }
769 }
770
771 return 0;
772}
773
Amy Maloche7e447432011-09-14 11:36:30 -0700774static void mxt_reset_delay(struct mxt_data *data)
775{
776 struct mxt_info *info = &data->info;
777
778 switch (info->family_id) {
779 case MXT224_ID:
780 msleep(MXT224_RESET_TIME);
781 break;
782 case MXT1386_ID:
783 msleep(MXT1386_RESET_TIME);
784 break;
785 default:
786 msleep(MXT_RESET_TIME);
787 }
788}
789
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791{
792 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800793 struct mxt_info *info = &data->info;
Amy Maloche52262212011-09-15 16:46:57 -0700794 int error, i;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700795 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700796 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700797 u8 command_register;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798
Iiro Valkonen7686b102011-02-02 23:21:58 -0800799 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700800 if (error)
801 return error;
802
803 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800804 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700805 GFP_KERNEL);
806 if (!data->object_table) {
807 dev_err(&client->dev, "Failed to allocate memory\n");
808 return -ENOMEM;
809 }
810
811 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800812 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700813 if (error)
814 return error;
815
816 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818 if (error)
819 return error;
820
Amy Maloche52262212011-09-15 16:46:57 -0700821 /* Store T7 and T9 locally, used in suspend/resume operations */
822 for (i = 0; i < T7_DATA_SIZE; i++) {
823 error = mxt_read_object(data, MXT_GEN_POWER, i,
824 &data->t7_data[i]);
825 if (error < 0) {
826 dev_err(&client->dev,
827 "failed to save current power state\n");
828 return error;
829 }
830 }
831 error = mxt_read_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_CTRL,
832 &data->t9_ctrl);
833 if (error < 0) {
834 dev_err(&client->dev, "failed to save current touch object\n");
835 return error;
836 }
837
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700838 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800839 mxt_write_object(data, MXT_GEN_COMMAND,
840 MXT_COMMAND_BACKUPNV,
841 MXT_BACKUP_VALUE);
842 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700843 do {
844 error = mxt_read_object(data, MXT_GEN_COMMAND,
845 MXT_COMMAND_BACKUPNV,
846 &command_register);
847 if (error)
848 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700849 usleep_range(1000, 2000);
850 } while ((command_register != 0) && (++timeout_counter <= 100));
851 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700852 dev_err(&client->dev, "No response after backup!\n");
853 return -EIO;
854 }
855
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856
857 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858 mxt_write_object(data, MXT_GEN_COMMAND,
859 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700860
Amy Maloche7e447432011-09-14 11:36:30 -0700861 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700862
863 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800864 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700865 if (error)
866 return error;
867 info->matrix_xsize = val;
868
Iiro Valkonen7686b102011-02-02 23:21:58 -0800869 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700870 if (error)
871 return error;
872 info->matrix_ysize = val;
873
874 dev_info(&client->dev,
875 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
876 info->family_id, info->variant_id, info->version,
877 info->build);
878
879 dev_info(&client->dev,
880 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
881 info->matrix_xsize, info->matrix_ysize,
882 info->object_num);
883
884 return 0;
885}
886
Iiro Valkonen7686b102011-02-02 23:21:58 -0800887static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700888 struct device_attribute *attr, char *buf)
889{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800890 struct mxt_data *data = dev_get_drvdata(dev);
891 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700892 int count = 0;
893 int i, j;
894 int error;
895 u8 val;
896
897 for (i = 0; i < data->info.object_num; i++) {
898 object = data->object_table + i;
899
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530900 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901 "Object Table Element %d(Type %d)\n",
902 i + 1, object->type);
903
Iiro Valkonen7686b102011-02-02 23:21:58 -0800904 if (!mxt_object_readable(object->type)) {
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530905 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700906 continue;
907 }
908
909 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800910 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700911 object->type, j, &val);
912 if (error)
913 return error;
914
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530915 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700916 " Byte %d: 0x%x (%d)\n", j, val, val);
917 }
918
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530919 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700920 }
921
922 return count;
923}
924
Iiro Valkonen7686b102011-02-02 23:21:58 -0800925static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700926{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800927 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700928 struct i2c_client *client = data->client;
929 const struct firmware *fw = NULL;
930 unsigned int frame_size;
931 unsigned int pos = 0;
932 int ret;
933
934 ret = request_firmware(&fw, fn, dev);
935 if (ret) {
936 dev_err(dev, "Unable to open firmware %s\n", fn);
937 return ret;
938 }
939
940 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800941 mxt_write_object(data, MXT_GEN_COMMAND,
942 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700943
944 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700945
946 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800947 if (client->addr == MXT_APP_LOW)
948 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700949 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800950 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700951
Iiro Valkonen7686b102011-02-02 23:21:58 -0800952 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953 if (ret)
954 goto out;
955
956 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958
959 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800960 ret = mxt_check_bootloader(client,
961 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700962 if (ret)
963 goto out;
964
965 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
966
967 /* We should add 2 at frame size as the the firmware data is not
968 * included the CRC bytes.
969 */
970 frame_size += 2;
971
972 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800973 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700974
Iiro Valkonen7686b102011-02-02 23:21:58 -0800975 ret = mxt_check_bootloader(client,
976 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700977 if (ret)
978 goto out;
979
980 pos += frame_size;
981
982 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
983 }
984
985out:
986 release_firmware(fw);
987
988 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800989 if (client->addr == MXT_BOOT_LOW)
990 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993
994 return ret;
995}
996
Iiro Valkonen7686b102011-02-02 23:21:58 -0800997static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998 struct device_attribute *attr,
999 const char *buf, size_t count)
1000{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001001 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001002 int error;
1003
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001004 disable_irq(data->irq);
1005
Iiro Valkonen7686b102011-02-02 23:21:58 -08001006 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007 if (error) {
1008 dev_err(dev, "The firmware update failed(%d)\n", error);
1009 count = error;
1010 } else {
1011 dev_dbg(dev, "The firmware update succeeded\n");
1012
1013 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001015
1016 kfree(data->object_table);
1017 data->object_table = NULL;
1018
Iiro Valkonen7686b102011-02-02 23:21:58 -08001019 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020 }
1021
1022 enable_irq(data->irq);
1023
Iiro Valkonen08960a02011-04-12 23:16:40 -07001024 error = mxt_make_highchg(data);
1025 if (error)
1026 return error;
1027
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001028 return count;
1029}
1030
Iiro Valkonen7686b102011-02-02 23:21:58 -08001031static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1032static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001033
Iiro Valkonen7686b102011-02-02 23:21:58 -08001034static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035 &dev_attr_object.attr,
1036 &dev_attr_update_fw.attr,
1037 NULL
1038};
1039
Iiro Valkonen7686b102011-02-02 23:21:58 -08001040static const struct attribute_group mxt_attr_group = {
1041 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042};
1043
Amy Maloche52262212011-09-15 16:46:57 -07001044static int mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045{
Amy Maloche52262212011-09-15 16:46:57 -07001046 int i, error;
1047 /* restore the old power state values and reenable touch */
1048 for (i = 0; i < T7_DATA_SIZE; i++) {
1049 error = mxt_write_object(data, MXT_GEN_POWER, i,
1050 data->t7_data[i]);
1051 if (error < 0) {
1052 dev_err(&data->client->dev,
1053 "failed to restore old power state\n");
1054 return error;
1055 }
1056 }
1057 error = mxt_write_object(data,
1058 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, data->t9_ctrl);
1059 if (error < 0) {
1060 dev_err(&data->client->dev, "failed to restore touch\n");
1061 return error;
1062 }
1063
1064 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065}
1066
Amy Maloche52262212011-09-15 16:46:57 -07001067static int mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001068{
Amy Maloche52262212011-09-15 16:46:57 -07001069 int i, error;
1070 /* configure deep sleep mode and disable touch */
1071 for (i = 0; i < T7_DATA_SIZE; i++) {
1072 error = mxt_write_object(data, MXT_GEN_POWER, i, 0);
1073 if (error < 0) {
1074 dev_err(&data->client->dev,
1075 "failed to configure deep sleep mode\n");
1076 return error;
1077 }
1078 }
1079
1080 error = mxt_write_object(data,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001081 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Amy Maloche52262212011-09-15 16:46:57 -07001082 if (error < 0) {
1083 dev_err(&data->client->dev,
1084 "failed to disable touch\n");
1085 return error;
1086 }
1087
1088 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001089}
1090
Iiro Valkonen7686b102011-02-02 23:21:58 -08001091static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001092{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001093 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001094 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095
Amy Maloche52262212011-09-15 16:46:57 -07001096 error = mxt_start(data);
1097 if (error < 0) {
1098 dev_err(&data->client->dev, "mxt_start failed in input_open\n");
1099 return error;
1100 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001101
1102 return 0;
1103}
1104
Iiro Valkonen7686b102011-02-02 23:21:58 -08001105static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001106{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001107 struct mxt_data *data = input_get_drvdata(dev);
Amy Maloche52262212011-09-15 16:46:57 -07001108 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109
Amy Maloche52262212011-09-15 16:46:57 -07001110 error = mxt_stop(data);
1111 if (error < 0)
1112 dev_err(&data->client->dev, "mxt_stop failed in input_close\n");
1113
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001114}
1115
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301116static int mxt_power_on(struct mxt_data *data, bool on)
1117{
1118 int rc;
1119
1120 if (on == false)
1121 goto power_off;
1122
1123 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1124 if (rc < 0) {
1125 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1126 rc);
1127 return rc;
1128 }
1129
1130 rc = regulator_enable(data->vcc);
1131 if (rc) {
1132 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1133 rc);
1134 goto error_reg_en_vcc;
1135 }
1136
1137 if (data->pdata->i2c_pull_up) {
1138 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1139 if (rc < 0) {
1140 dev_err(&data->client->dev,
1141 "Regulator set_opt failed rc=%d\n", rc);
1142 goto error_reg_opt_i2c;
1143 }
1144
1145 rc = regulator_enable(data->vcc_i2c);
1146 if (rc) {
1147 dev_err(&data->client->dev,
1148 "Regulator enable failed rc=%d\n", rc);
1149 goto error_reg_en_vcc_i2c;
1150 }
1151 }
1152
Amy Malochef0d7b8d2011-10-17 12:10:51 -07001153 msleep(130);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301154
1155 return 0;
1156
1157error_reg_en_vcc_i2c:
1158 if (data->pdata->i2c_pull_up)
1159 regulator_set_optimum_mode(data->vcc_i2c, 0);
1160error_reg_opt_i2c:
1161 regulator_disable(data->vcc);
1162error_reg_en_vcc:
1163 regulator_set_optimum_mode(data->vcc, 0);
1164 return rc;
1165
1166power_off:
1167 regulator_set_optimum_mode(data->vcc, 0);
1168 regulator_disable(data->vcc);
1169 if (data->pdata->i2c_pull_up) {
1170 regulator_set_optimum_mode(data->vcc_i2c, 0);
1171 regulator_disable(data->vcc_i2c);
1172 }
1173 msleep(50);
1174 return 0;
1175}
1176
1177static int mxt_regulator_configure(struct mxt_data *data, bool on)
1178{
1179 int rc;
1180
1181 if (on == false)
1182 goto hw_shutdown;
1183
1184 data->vcc = regulator_get(&data->client->dev, "vdd");
1185 if (IS_ERR(data->vcc)) {
1186 rc = PTR_ERR(data->vcc);
1187 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1188 rc);
1189 return rc;
1190 }
1191
1192 if (regulator_count_voltages(data->vcc) > 0) {
1193 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1194 MXT_VTG_MAX_UV);
1195 if (rc) {
1196 dev_err(&data->client->dev,
1197 "regulator set_vtg failed rc=%d\n", rc);
1198 goto error_set_vtg_vcc;
1199 }
1200 }
1201
1202 if (data->pdata->i2c_pull_up) {
1203 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1204 if (IS_ERR(data->vcc_i2c)) {
1205 rc = PTR_ERR(data->vcc_i2c);
1206 dev_err(&data->client->dev,
1207 "Regulator get failed rc=%d\n", rc);
1208 goto error_get_vtg_i2c;
1209 }
1210 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1211 rc = regulator_set_voltage(data->vcc_i2c,
1212 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1213 if (rc) {
1214 dev_err(&data->client->dev,
1215 "regulator set_vtg failed rc=%d\n", rc);
1216 goto error_set_vtg_i2c;
1217 }
1218 }
1219 }
1220
1221 return 0;
1222
1223error_set_vtg_i2c:
1224 regulator_put(data->vcc_i2c);
1225error_get_vtg_i2c:
1226 if (regulator_count_voltages(data->vcc) > 0)
1227 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1228error_set_vtg_vcc:
1229 regulator_put(data->vcc);
1230 return rc;
1231
1232hw_shutdown:
1233 if (regulator_count_voltages(data->vcc) > 0)
1234 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1235 regulator_put(data->vcc);
1236 if (data->pdata->i2c_pull_up) {
1237 if (regulator_count_voltages(data->vcc_i2c) > 0)
1238 regulator_set_voltage(data->vcc_i2c, 0,
1239 MXT_I2C_VTG_MAX_UV);
1240 regulator_put(data->vcc_i2c);
1241 }
1242 return 0;
1243}
1244
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301245#ifdef CONFIG_PM
1246static int mxt_suspend(struct device *dev)
1247{
1248 struct i2c_client *client = to_i2c_client(dev);
1249 struct mxt_data *data = i2c_get_clientdata(client);
1250 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001251 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301252
1253 mutex_lock(&input_dev->mutex);
1254
Amy Maloche52262212011-09-15 16:46:57 -07001255 if (input_dev->users) {
1256 error = mxt_stop(data);
1257 if (error < 0) {
1258 dev_err(&client->dev, "mxt_stop failed in suspend\n");
1259 mutex_unlock(&input_dev->mutex);
1260 return error;
1261 }
1262
1263 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301264
1265 mutex_unlock(&input_dev->mutex);
1266
1267 return 0;
1268}
1269
1270static int mxt_resume(struct device *dev)
1271{
1272 struct i2c_client *client = to_i2c_client(dev);
1273 struct mxt_data *data = i2c_get_clientdata(client);
1274 struct input_dev *input_dev = data->input_dev;
Amy Maloche52262212011-09-15 16:46:57 -07001275 int error;
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301276 /* Soft reset */
1277 mxt_write_object(data, MXT_GEN_COMMAND,
1278 MXT_COMMAND_RESET, 1);
1279
Amy Maloche7e447432011-09-14 11:36:30 -07001280 mxt_reset_delay(data);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301281
1282 mutex_lock(&input_dev->mutex);
1283
Amy Maloche52262212011-09-15 16:46:57 -07001284 if (input_dev->users) {
1285 error = mxt_start(data);
1286 if (error < 0) {
1287 dev_err(&client->dev, "mxt_start failed in resume\n");
1288 mutex_unlock(&input_dev->mutex);
1289 return error;
1290 }
1291 }
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301292
1293 mutex_unlock(&input_dev->mutex);
1294
1295 return 0;
1296}
1297
1298#if defined(CONFIG_HAS_EARLYSUSPEND)
1299static void mxt_early_suspend(struct early_suspend *h)
1300{
1301 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1302
1303 mxt_suspend(&data->client->dev);
1304}
1305
1306static void mxt_late_resume(struct early_suspend *h)
1307{
1308 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1309
1310 mxt_resume(&data->client->dev);
1311}
1312#endif
1313
1314static const struct dev_pm_ops mxt_pm_ops = {
1315#ifndef CONFIG_HAS_EARLYSUSPEND
1316 .suspend = mxt_suspend,
1317 .resume = mxt_resume,
1318#endif
1319};
1320#endif
1321
Iiro Valkonen7686b102011-02-02 23:21:58 -08001322static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001323 const struct i2c_device_id *id)
1324{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001325 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001326 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001327 struct input_dev *input_dev;
1328 int error;
1329
Iiro Valkonen919ed892011-02-15 13:36:52 -08001330 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001331 return -EINVAL;
1332
Iiro Valkonen7686b102011-02-02 23:21:58 -08001333 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001334 input_dev = input_allocate_device();
1335 if (!data || !input_dev) {
1336 dev_err(&client->dev, "Failed to allocate memory\n");
1337 error = -ENOMEM;
1338 goto err_free_mem;
1339 }
1340
Iiro Valkonen7686b102011-02-02 23:21:58 -08001341 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001342 input_dev->id.bustype = BUS_I2C;
1343 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001344 input_dev->open = mxt_input_open;
1345 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001346
Joonyoung Shim910d8052011-04-12 23:14:38 -07001347 data->client = client;
1348 data->input_dev = input_dev;
1349 data->pdata = pdata;
1350 data->irq = client->irq;
1351
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001352 __set_bit(EV_ABS, input_dev->evbit);
1353 __set_bit(EV_KEY, input_dev->evbit);
1354 __set_bit(BTN_TOUCH, input_dev->keybit);
1355
1356 /* For single touch */
1357 input_set_abs_params(input_dev, ABS_X,
Jing Lin2f863172011-10-17 10:56:58 -07001358 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001359 input_set_abs_params(input_dev, ABS_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001360 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001361
1362 /* For multi touch */
1363 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001364 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001365 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Jing Lin2f863172011-10-17 10:56:58 -07001366 0, data->pdata->x_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001367 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Jing Lin2f863172011-10-17 10:56:58 -07001368 0, data->pdata->y_size, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001369
1370 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001371 i2c_set_clientdata(client, data);
1372
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301373 if (pdata->init_hw)
1374 error = pdata->init_hw(true);
1375 else
1376 error = mxt_regulator_configure(data, true);
1377 if (error) {
1378 dev_err(&client->dev, "Failed to intialize hardware\n");
1379 goto err_free_object;
1380 }
1381
1382 if (pdata->power_on)
1383 error = pdata->power_on(true);
1384 else
1385 error = mxt_power_on(data, true);
1386 if (error) {
1387 dev_err(&client->dev, "Failed to power on hardware\n");
1388 goto err_regulator_on;
1389 }
1390
Iiro Valkonen7686b102011-02-02 23:21:58 -08001391 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301393 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001394
Iiro Valkonen7686b102011-02-02 23:21:58 -08001395 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001396 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001397 if (error) {
1398 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301399 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001400 }
1401
Iiro Valkonen08960a02011-04-12 23:16:40 -07001402 error = mxt_make_highchg(data);
1403 if (error)
1404 goto err_free_irq;
1405
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001406 error = input_register_device(input_dev);
1407 if (error)
1408 goto err_free_irq;
1409
Iiro Valkonen7686b102011-02-02 23:21:58 -08001410 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001411 if (error)
1412 goto err_unregister_device;
1413
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301414#if defined(CONFIG_HAS_EARLYSUSPEND)
1415 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1416 MXT_SUSPEND_LEVEL;
1417 data->early_suspend.suspend = mxt_early_suspend;
1418 data->early_suspend.resume = mxt_late_resume;
1419 register_early_suspend(&data->early_suspend);
1420#endif
1421
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001422 return 0;
1423
1424err_unregister_device:
1425 input_unregister_device(input_dev);
1426 input_dev = NULL;
1427err_free_irq:
1428 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301429err_power_on:
1430 if (pdata->power_on)
1431 pdata->power_on(false);
1432 else
1433 mxt_power_on(data, false);
1434err_regulator_on:
1435 if (pdata->init_hw)
1436 pdata->init_hw(false);
1437 else
1438 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001439err_free_object:
1440 kfree(data->object_table);
1441err_free_mem:
1442 input_free_device(input_dev);
1443 kfree(data);
1444 return error;
1445}
1446
Iiro Valkonen7686b102011-02-02 23:21:58 -08001447static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001448{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001449 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001450
Iiro Valkonen7686b102011-02-02 23:21:58 -08001451 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001452 free_irq(data->irq, data);
1453 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301454#if defined(CONFIG_HAS_EARLYSUSPEND)
1455 unregister_early_suspend(&data->early_suspend);
1456#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301457
1458 if (data->pdata->power_on)
1459 data->pdata->power_on(false);
1460 else
1461 mxt_power_on(data, false);
1462
1463 if (data->pdata->init_hw)
1464 data->pdata->init_hw(false);
1465 else
1466 mxt_regulator_configure(data, false);
1467
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001468 kfree(data->object_table);
1469 kfree(data);
1470
1471 return 0;
1472}
1473
Iiro Valkonen7686b102011-02-02 23:21:58 -08001474static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001475 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001476 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001477 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001478 { }
1479};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001480MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001481
Iiro Valkonen7686b102011-02-02 23:21:58 -08001482static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001483 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001484 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001485 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001486#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001487 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001488#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001489 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001490 .probe = mxt_probe,
1491 .remove = __devexit_p(mxt_remove),
1492 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001493};
1494
Iiro Valkonen7686b102011-02-02 23:21:58 -08001495static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001496{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001497 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001498}
1499
Iiro Valkonen7686b102011-02-02 23:21:58 -08001500static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001501{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001502 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001503}
1504
Iiro Valkonen7686b102011-02-02 23:21:58 -08001505module_init(mxt_init);
1506module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001507
1508/* Module information */
1509MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001510MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001511MODULE_LICENSE("GPL");