blob: 472020478ecc76948d9a7fe0381eeb458d136578 [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
25/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080026#define MXT_VER_20 20
27#define MXT_VER_21 21
28#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070029
30/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080031#define MXT_APP_LOW 0x4a
32#define MXT_APP_HIGH 0x4b
33#define MXT_BOOT_LOW 0x24
34#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070035
36/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070038
39/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080040#define MXT_FAMILY_ID 0x00
41#define MXT_VARIANT_ID 0x01
42#define MXT_VERSION 0x02
43#define MXT_BUILD 0x03
44#define MXT_MATRIX_X_SIZE 0x04
45#define MXT_MATRIX_Y_SIZE 0x05
46#define MXT_OBJECT_NUM 0x06
47#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070048
Iiro Valkonen7686b102011-02-02 23:21:58 -080049#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070050
51/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080052#define MXT_DEBUG_DIAGNOSTIC 37
53#define MXT_GEN_MESSAGE 5
54#define MXT_GEN_COMMAND 6
55#define MXT_GEN_POWER 7
56#define MXT_GEN_ACQUIRE 8
57#define MXT_TOUCH_MULTI 9
58#define MXT_TOUCH_KEYARRAY 15
59#define MXT_TOUCH_PROXIMITY 23
60#define MXT_PROCI_GRIPFACE 20
61#define MXT_PROCG_NOISE 22
62#define MXT_PROCI_ONETOUCH 24
63#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070064#define MXT_PROCI_GRIP 40
65#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070066#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080067#define MXT_SPT_GPIOPWM 19
68#define MXT_SPT_SELFTEST 25
69#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070070#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070071#define MXT_SPT_DIGITIZER 43
72#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070073
Iiro Valkonen7686b102011-02-02 23:21:58 -080074/* MXT_GEN_COMMAND field */
75#define MXT_COMMAND_RESET 0
76#define MXT_COMMAND_BACKUPNV 1
77#define MXT_COMMAND_CALIBRATE 2
78#define MXT_COMMAND_REPORTALL 3
79#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070080
Iiro Valkonen7686b102011-02-02 23:21:58 -080081/* MXT_GEN_POWER field */
82#define MXT_POWER_IDLEACQINT 0
83#define MXT_POWER_ACTVACQINT 1
84#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070085
Iiro Valkonen7686b102011-02-02 23:21:58 -080086/* MXT_GEN_ACQUIRE field */
87#define MXT_ACQUIRE_CHRGTIME 0
88#define MXT_ACQUIRE_TCHDRIFT 2
89#define MXT_ACQUIRE_DRIFTST 3
90#define MXT_ACQUIRE_TCHAUTOCAL 4
91#define MXT_ACQUIRE_SYNC 5
92#define MXT_ACQUIRE_ATCHCALST 6
93#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070094
Iiro Valkonen7686b102011-02-02 23:21:58 -080095/* MXT_TOUCH_MULTI field */
96#define MXT_TOUCH_CTRL 0
97#define MXT_TOUCH_XORIGIN 1
98#define MXT_TOUCH_YORIGIN 2
99#define MXT_TOUCH_XSIZE 3
100#define MXT_TOUCH_YSIZE 4
101#define MXT_TOUCH_BLEN 6
102#define MXT_TOUCH_TCHTHR 7
103#define MXT_TOUCH_TCHDI 8
104#define MXT_TOUCH_ORIENT 9
105#define MXT_TOUCH_MOVHYSTI 11
106#define MXT_TOUCH_MOVHYSTN 12
107#define MXT_TOUCH_NUMTOUCH 14
108#define MXT_TOUCH_MRGHYST 15
109#define MXT_TOUCH_MRGTHR 16
110#define MXT_TOUCH_AMPHYST 17
111#define MXT_TOUCH_XRANGE_LSB 18
112#define MXT_TOUCH_XRANGE_MSB 19
113#define MXT_TOUCH_YRANGE_LSB 20
114#define MXT_TOUCH_YRANGE_MSB 21
115#define MXT_TOUCH_XLOCLIP 22
116#define MXT_TOUCH_XHICLIP 23
117#define MXT_TOUCH_YLOCLIP 24
118#define MXT_TOUCH_YHICLIP 25
119#define MXT_TOUCH_XEDGECTRL 26
120#define MXT_TOUCH_XEDGEDIST 27
121#define MXT_TOUCH_YEDGECTRL 28
122#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700123#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700124
Iiro Valkonen7686b102011-02-02 23:21:58 -0800125/* MXT_PROCI_GRIPFACE field */
126#define MXT_GRIPFACE_CTRL 0
127#define MXT_GRIPFACE_XLOGRIP 1
128#define MXT_GRIPFACE_XHIGRIP 2
129#define MXT_GRIPFACE_YLOGRIP 3
130#define MXT_GRIPFACE_YHIGRIP 4
131#define MXT_GRIPFACE_MAXTCHS 5
132#define MXT_GRIPFACE_SZTHR1 7
133#define MXT_GRIPFACE_SZTHR2 8
134#define MXT_GRIPFACE_SHPTHR1 9
135#define MXT_GRIPFACE_SHPTHR2 10
136#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700137
Iiro Valkonen7686b102011-02-02 23:21:58 -0800138/* MXT_PROCI_NOISE field */
139#define MXT_NOISE_CTRL 0
140#define MXT_NOISE_OUTFLEN 1
141#define MXT_NOISE_GCAFUL_LSB 3
142#define MXT_NOISE_GCAFUL_MSB 4
143#define MXT_NOISE_GCAFLL_LSB 5
144#define MXT_NOISE_GCAFLL_MSB 6
145#define MXT_NOISE_ACTVGCAFVALID 7
146#define MXT_NOISE_NOISETHR 8
147#define MXT_NOISE_FREQHOPSCALE 10
148#define MXT_NOISE_FREQ0 11
149#define MXT_NOISE_FREQ1 12
150#define MXT_NOISE_FREQ2 13
151#define MXT_NOISE_FREQ3 14
152#define MXT_NOISE_FREQ4 15
153#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700154
Iiro Valkonen7686b102011-02-02 23:21:58 -0800155/* MXT_SPT_COMMSCONFIG */
156#define MXT_COMMS_CTRL 0
157#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700158
Iiro Valkonen7686b102011-02-02 23:21:58 -0800159/* MXT_SPT_CTECONFIG field */
160#define MXT_CTE_CTRL 0
161#define MXT_CTE_CMD 1
162#define MXT_CTE_MODE 2
163#define MXT_CTE_IDLEGCAFDEPTH 3
164#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700165#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700166
Iiro Valkonen7686b102011-02-02 23:21:58 -0800167#define MXT_VOLTAGE_DEFAULT 2700000
168#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700169
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530170#define MXT_VTG_MIN_UV 2700000
171#define MXT_VTG_MAX_UV 3300000
172#define MXT_ACTIVE_LOAD_UA 15000
173
174#define MXT_I2C_VTG_MIN_UV 1800000
175#define MXT_I2C_VTG_MAX_UV 1800000
176#define MXT_I2C_LOAD_UA 10000
177
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178/* Define for MXT_GEN_COMMAND */
179#define MXT_BOOT_VALUE 0xa5
180#define MXT_BACKUP_VALUE 0x55
181#define MXT_BACKUP_TIME 25 /* msec */
182#define MXT_RESET_TIME 65 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700183
Iiro Valkonen7686b102011-02-02 23:21:58 -0800184#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700185
186/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800187#define MXT_UNLOCK_CMD_MSB 0xaa
188#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700189
190/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800191#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
192#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
193#define MXT_FRAME_CRC_CHECK 0x02
194#define MXT_FRAME_CRC_FAIL 0x03
195#define MXT_FRAME_CRC_PASS 0x04
196#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
197#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700198
199/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_SUPPRESS (1 << 1)
201#define MXT_AMP (1 << 2)
202#define MXT_VECTOR (1 << 3)
203#define MXT_MOVE (1 << 4)
204#define MXT_RELEASE (1 << 5)
205#define MXT_PRESS (1 << 6)
206#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700207
Joonyoung Shim910d8052011-04-12 23:14:38 -0700208/* Touch orient bits */
209#define MXT_XY_SWITCH (1 << 0)
210#define MXT_X_INVERT (1 << 1)
211#define MXT_Y_INVERT (1 << 2)
212
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700213/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800214#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700215
Iiro Valkonen7686b102011-02-02 23:21:58 -0800216#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219 u8 family_id;
220 u8 variant_id;
221 u8 version;
222 u8 build;
223 u8 matrix_xsize;
224 u8 matrix_ysize;
225 u8 object_num;
226};
227
Iiro Valkonen7686b102011-02-02 23:21:58 -0800228struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700229 u8 type;
230 u16 start_address;
231 u8 size;
232 u8 instances;
233 u8 num_report_ids;
234
235 /* to map object and message */
236 u8 max_reportid;
237};
238
Iiro Valkonen7686b102011-02-02 23:21:58 -0800239struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700240 u8 reportid;
241 u8 message[7];
242 u8 checksum;
243};
244
Iiro Valkonen7686b102011-02-02 23:21:58 -0800245struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700246 int status;
247 int x;
248 int y;
249 int area;
250};
251
252/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800253struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700254 struct i2c_client *client;
255 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800256 const struct mxt_platform_data *pdata;
257 struct mxt_object *object_table;
258 struct mxt_info info;
259 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700260 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700261 unsigned int max_x;
262 unsigned int max_y;
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530263 struct regulator *vcc;
264 struct regulator *vcc_i2c;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700265};
266
Iiro Valkonen7686b102011-02-02 23:21:58 -0800267static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700268{
269 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800270 case MXT_GEN_MESSAGE:
271 case MXT_GEN_COMMAND:
272 case MXT_GEN_POWER:
273 case MXT_GEN_ACQUIRE:
274 case MXT_TOUCH_MULTI:
275 case MXT_TOUCH_KEYARRAY:
276 case MXT_TOUCH_PROXIMITY:
277 case MXT_PROCI_GRIPFACE:
278 case MXT_PROCG_NOISE:
279 case MXT_PROCI_ONETOUCH:
280 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700281 case MXT_PROCI_GRIP:
282 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800283 case MXT_SPT_COMMSCONFIG:
284 case MXT_SPT_GPIOPWM:
285 case MXT_SPT_SELFTEST:
286 case MXT_SPT_CTECONFIG:
287 case MXT_SPT_USERDATA:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700288 return true;
289 default:
290 return false;
291 }
292}
293
Iiro Valkonen7686b102011-02-02 23:21:58 -0800294static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700295{
296 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800297 case MXT_GEN_COMMAND:
298 case MXT_GEN_POWER:
299 case MXT_GEN_ACQUIRE:
300 case MXT_TOUCH_MULTI:
301 case MXT_TOUCH_KEYARRAY:
302 case MXT_TOUCH_PROXIMITY:
303 case MXT_PROCI_GRIPFACE:
304 case MXT_PROCG_NOISE:
305 case MXT_PROCI_ONETOUCH:
306 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700307 case MXT_PROCI_GRIP:
308 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800309 case MXT_SPT_GPIOPWM:
310 case MXT_SPT_SELFTEST:
311 case MXT_SPT_CTECONFIG:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700312 return true;
313 default:
314 return false;
315 }
316}
317
Iiro Valkonen7686b102011-02-02 23:21:58 -0800318static void mxt_dump_message(struct device *dev,
319 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700320{
321 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
322 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
323 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
324 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
325 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
326 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
327 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
328 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
329 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
330}
331
Iiro Valkonen7686b102011-02-02 23:21:58 -0800332static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700333 unsigned int state)
334{
335 u8 val;
336
337recheck:
338 if (i2c_master_recv(client, &val, 1) != 1) {
339 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
340 return -EIO;
341 }
342
343 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800344 case MXT_WAITING_BOOTLOAD_CMD:
345 case MXT_WAITING_FRAME_DATA:
346 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700347 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800348 case MXT_FRAME_CRC_PASS:
349 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700350 goto recheck;
351 break;
352 default:
353 return -EINVAL;
354 }
355
356 if (val != state) {
357 dev_err(&client->dev, "Unvalid bootloader mode state\n");
358 return -EINVAL;
359 }
360
361 return 0;
362}
363
Iiro Valkonen7686b102011-02-02 23:21:58 -0800364static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700365{
366 u8 buf[2];
367
Iiro Valkonen7686b102011-02-02 23:21:58 -0800368 buf[0] = MXT_UNLOCK_CMD_LSB;
369 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700370
371 if (i2c_master_send(client, buf, 2) != 2) {
372 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
373 return -EIO;
374 }
375
376 return 0;
377}
378
Iiro Valkonen7686b102011-02-02 23:21:58 -0800379static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700380 const u8 *data, unsigned int frame_size)
381{
382 if (i2c_master_send(client, data, frame_size) != frame_size) {
383 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
384 return -EIO;
385 }
386
387 return 0;
388}
389
Iiro Valkonen7686b102011-02-02 23:21:58 -0800390static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700391 u16 reg, u16 len, void *val)
392{
393 struct i2c_msg xfer[2];
394 u8 buf[2];
395
396 buf[0] = reg & 0xff;
397 buf[1] = (reg >> 8) & 0xff;
398
399 /* Write register */
400 xfer[0].addr = client->addr;
401 xfer[0].flags = 0;
402 xfer[0].len = 2;
403 xfer[0].buf = buf;
404
405 /* Read data */
406 xfer[1].addr = client->addr;
407 xfer[1].flags = I2C_M_RD;
408 xfer[1].len = len;
409 xfer[1].buf = val;
410
411 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
412 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
413 return -EIO;
414 }
415
416 return 0;
417}
418
Iiro Valkonen7686b102011-02-02 23:21:58 -0800419static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700420{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800421 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700422}
423
Iiro Valkonen7686b102011-02-02 23:21:58 -0800424static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700425{
426 u8 buf[3];
427
428 buf[0] = reg & 0xff;
429 buf[1] = (reg >> 8) & 0xff;
430 buf[2] = val;
431
432 if (i2c_master_send(client, buf, 3) != 3) {
433 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
434 return -EIO;
435 }
436
437 return 0;
438}
439
Iiro Valkonen7686b102011-02-02 23:21:58 -0800440static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700441 u16 reg, u8 *object_buf)
442{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800443 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700444 object_buf);
445}
446
Iiro Valkonen7686b102011-02-02 23:21:58 -0800447static struct mxt_object *
448mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700449{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800450 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700451 int i;
452
453 for (i = 0; i < data->info.object_num; i++) {
454 object = data->object_table + i;
455 if (object->type == type)
456 return object;
457 }
458
459 dev_err(&data->client->dev, "Invalid object type\n");
460 return NULL;
461}
462
Iiro Valkonen7686b102011-02-02 23:21:58 -0800463static int mxt_read_message(struct mxt_data *data,
464 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700465{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800466 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700467 u16 reg;
468
Iiro Valkonen7686b102011-02-02 23:21:58 -0800469 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700470 if (!object)
471 return -EINVAL;
472
473 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800474 return __mxt_read_reg(data->client, reg,
475 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700476}
477
Iiro Valkonen7686b102011-02-02 23:21:58 -0800478static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700479 u8 type, u8 offset, u8 *val)
480{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800481 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482 u16 reg;
483
Iiro Valkonen7686b102011-02-02 23:21:58 -0800484 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485 if (!object)
486 return -EINVAL;
487
488 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800489 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700490}
491
Iiro Valkonen7686b102011-02-02 23:21:58 -0800492static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700493 u8 type, u8 offset, u8 val)
494{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800495 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700496 u16 reg;
497
Iiro Valkonen7686b102011-02-02 23:21:58 -0800498 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700499 if (!object)
500 return -EINVAL;
501
502 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800503 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504}
505
Iiro Valkonen7686b102011-02-02 23:21:58 -0800506static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700507{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700509 struct input_dev *input_dev = data->input_dev;
510 int status = finger[single_id].status;
511 int finger_num = 0;
512 int id;
513
Iiro Valkonen7686b102011-02-02 23:21:58 -0800514 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700515 if (!finger[id].status)
516 continue;
517
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700518 input_mt_slot(input_dev, id);
519 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
520 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700521
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700522 if (finger[id].status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700523 finger_num++;
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700524 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
525 finger[id].area);
526 input_report_abs(input_dev, ABS_MT_POSITION_X,
527 finger[id].x);
528 input_report_abs(input_dev, ABS_MT_POSITION_Y,
529 finger[id].y);
530 } else {
531 finger[id].status = 0;
532 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700533 }
534
535 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
536
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538 input_report_abs(input_dev, ABS_X, finger[single_id].x);
539 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
540 }
541
542 input_sync(input_dev);
543}
544
Iiro Valkonen7686b102011-02-02 23:21:58 -0800545static void mxt_input_touchevent(struct mxt_data *data,
546 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700547{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800548 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700549 struct device *dev = &data->client->dev;
550 u8 status = message->message[0];
551 int x;
552 int y;
553 int area;
554
555 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800556 if (!(status & MXT_DETECT)) {
557 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700558 dev_dbg(dev, "[%d] released\n", id);
559
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 finger[id].status = MXT_RELEASE;
561 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700562 }
563 return;
564 }
565
566 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800567 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700568 return;
569
Joonyoung Shim910d8052011-04-12 23:14:38 -0700570 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
571 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
572 if (data->max_x < 1024)
573 x = x >> 2;
574 if (data->max_y < 1024)
575 y = y >> 2;
576
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577 area = message->message[4];
578
579 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800580 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700581 x, y, area);
582
Iiro Valkonen7686b102011-02-02 23:21:58 -0800583 finger[id].status = status & MXT_MOVE ?
584 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700585 finger[id].x = x;
586 finger[id].y = y;
587 finger[id].area = area;
588
Iiro Valkonen7686b102011-02-02 23:21:58 -0800589 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590}
591
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800594 struct mxt_data *data = dev_id;
595 struct mxt_message message;
596 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597 struct device *dev = &data->client->dev;
598 int id;
599 u8 reportid;
600 u8 max_reportid;
601 u8 min_reportid;
602
603 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700605 dev_err(dev, "Failed to read message\n");
606 goto end;
607 }
608
609 reportid = message.reportid;
610
Iiro Valkonen7686b102011-02-02 23:21:58 -0800611 /* whether reportid is thing of MXT_TOUCH_MULTI */
612 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700613 if (!object)
614 goto end;
615
616 max_reportid = object->max_reportid;
617 min_reportid = max_reportid - object->num_report_ids + 1;
618 id = reportid - min_reportid;
619
620 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800621 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700622 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624 } while (reportid != 0xff);
625
626end:
627 return IRQ_HANDLED;
628}
629
Iiro Valkonen7686b102011-02-02 23:21:58 -0800630static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800632 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634 struct device *dev = &data->client->dev;
635 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800636 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637
Iiro Valkonen71749f52011-02-15 13:36:52 -0800638 if (!pdata->config) {
639 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
640 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641 }
642
643 for (i = 0; i < data->info.object_num; i++) {
644 object = data->object_table + i;
645
Iiro Valkonen7686b102011-02-02 23:21:58 -0800646 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700647 continue;
648
Iiro Valkonen71749f52011-02-15 13:36:52 -0800649 for (j = 0; j < object->size + 1; j++) {
650 config_offset = index + j;
651 if (config_offset > pdata->config_length) {
652 dev_err(dev, "Not enough config data!\n");
653 return -EINVAL;
654 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800655 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800656 pdata->config[config_offset]);
657 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658 index += object->size + 1;
659 }
660
661 return 0;
662}
663
Iiro Valkonen7686b102011-02-02 23:21:58 -0800664static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665{
666 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800667 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 int count = 10;
669 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700670
671 /* Read dummy message to make high CHG pin */
672 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800673 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674 if (error)
675 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800676 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677
678 if (!count) {
679 dev_err(dev, "CHG pin isn't cleared\n");
680 return -EBUSY;
681 }
682
683 return 0;
684}
685
Iiro Valkonen7686b102011-02-02 23:21:58 -0800686static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800688 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689 u8 voltage;
690
691 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800692 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700693 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800694 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700695 pdata->y_line);
696
697 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699 pdata->orient);
700
701 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800702 mxt_write_object(data, MXT_TOUCH_MULTI,
703 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700704
705 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800706 mxt_write_object(data, MXT_TOUCH_MULTI,
707 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708
709 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800710 mxt_write_object(data, MXT_TOUCH_MULTI,
711 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
712 mxt_write_object(data, MXT_TOUCH_MULTI,
713 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
714 mxt_write_object(data, MXT_TOUCH_MULTI,
715 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
716 mxt_write_object(data, MXT_TOUCH_MULTI,
717 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718
719 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700720 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800721 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
722 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
723 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700724 voltage = 0xff - voltage + 1;
725 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800726 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
727 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729 mxt_write_object(data, MXT_SPT_CTECONFIG,
730 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731 }
732}
733
Iiro Valkonen7686b102011-02-02 23:21:58 -0800734static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700735{
736 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800737 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700738 int error;
739 u8 val;
740
Iiro Valkonen7686b102011-02-02 23:21:58 -0800741 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700742 if (error)
743 return error;
744 info->family_id = val;
745
Iiro Valkonen7686b102011-02-02 23:21:58 -0800746 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747 if (error)
748 return error;
749 info->variant_id = val;
750
Iiro Valkonen7686b102011-02-02 23:21:58 -0800751 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752 if (error)
753 return error;
754 info->version = val;
755
Iiro Valkonen7686b102011-02-02 23:21:58 -0800756 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700757 if (error)
758 return error;
759 info->build = val;
760
Iiro Valkonen7686b102011-02-02 23:21:58 -0800761 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700762 if (error)
763 return error;
764 info->object_num = val;
765
766 return 0;
767}
768
Iiro Valkonen7686b102011-02-02 23:21:58 -0800769static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700770{
771 int error;
772 int i;
773 u16 reg;
774 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700776
777 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800778 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779
Iiro Valkonen7686b102011-02-02 23:21:58 -0800780 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
781 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700782 if (error)
783 return error;
784
785 object->type = buf[0];
786 object->start_address = (buf[2] << 8) | buf[1];
787 object->size = buf[3];
788 object->instances = buf[4];
789 object->num_report_ids = buf[5];
790
791 if (object->num_report_ids) {
792 reportid += object->num_report_ids *
793 (object->instances + 1);
794 object->max_reportid = reportid;
795 }
796 }
797
798 return 0;
799}
800
Iiro Valkonen7686b102011-02-02 23:21:58 -0800801static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700802{
803 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800804 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700805 int error;
806 u8 val;
807
Iiro Valkonen7686b102011-02-02 23:21:58 -0800808 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700809 if (error)
810 return error;
811
812 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814 GFP_KERNEL);
815 if (!data->object_table) {
816 dev_err(&client->dev, "Failed to allocate memory\n");
817 return -ENOMEM;
818 }
819
820 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800821 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700822 if (error)
823 return error;
824
825 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800826 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700827 if (error)
828 return error;
829
Iiro Valkonen7686b102011-02-02 23:21:58 -0800830 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831
832 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800833 mxt_write_object(data, MXT_GEN_COMMAND,
834 MXT_COMMAND_BACKUPNV,
835 MXT_BACKUP_VALUE);
836 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700837
838 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800839 mxt_write_object(data, MXT_GEN_COMMAND,
840 MXT_COMMAND_RESET, 1);
841 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842
843 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800844 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845 if (error)
846 return error;
847 info->matrix_xsize = val;
848
Iiro Valkonen7686b102011-02-02 23:21:58 -0800849 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700850 if (error)
851 return error;
852 info->matrix_ysize = val;
853
854 dev_info(&client->dev,
855 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
856 info->family_id, info->variant_id, info->version,
857 info->build);
858
859 dev_info(&client->dev,
860 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
861 info->matrix_xsize, info->matrix_ysize,
862 info->object_num);
863
864 return 0;
865}
866
Joonyoung Shim910d8052011-04-12 23:14:38 -0700867static void mxt_calc_resolution(struct mxt_data *data)
868{
869 unsigned int max_x = data->pdata->x_size - 1;
870 unsigned int max_y = data->pdata->y_size - 1;
871
872 if (data->pdata->orient & MXT_XY_SWITCH) {
873 data->max_x = max_y;
874 data->max_y = max_x;
875 } else {
876 data->max_x = max_x;
877 data->max_y = max_y;
878 }
879}
880
Iiro Valkonen7686b102011-02-02 23:21:58 -0800881static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700882 struct device_attribute *attr, char *buf)
883{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800884 struct mxt_data *data = dev_get_drvdata(dev);
885 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700886 int count = 0;
887 int i, j;
888 int error;
889 u8 val;
890
891 for (i = 0; i < data->info.object_num; i++) {
892 object = data->object_table + i;
893
894 count += sprintf(buf + count,
895 "Object Table Element %d(Type %d)\n",
896 i + 1, object->type);
897
Iiro Valkonen7686b102011-02-02 23:21:58 -0800898 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700899 count += sprintf(buf + count, "\n");
900 continue;
901 }
902
903 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800904 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700905 object->type, j, &val);
906 if (error)
907 return error;
908
909 count += sprintf(buf + count,
910 " Byte %d: 0x%x (%d)\n", j, val, val);
911 }
912
913 count += sprintf(buf + count, "\n");
914 }
915
916 return count;
917}
918
Iiro Valkonen7686b102011-02-02 23:21:58 -0800919static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700920{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800921 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700922 struct i2c_client *client = data->client;
923 const struct firmware *fw = NULL;
924 unsigned int frame_size;
925 unsigned int pos = 0;
926 int ret;
927
928 ret = request_firmware(&fw, fn, dev);
929 if (ret) {
930 dev_err(dev, "Unable to open firmware %s\n", fn);
931 return ret;
932 }
933
934 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800935 mxt_write_object(data, MXT_GEN_COMMAND,
936 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
937 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700938
939 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800940 if (client->addr == MXT_APP_LOW)
941 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700942 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800943 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700944
Iiro Valkonen7686b102011-02-02 23:21:58 -0800945 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946 if (ret)
947 goto out;
948
949 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800950 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700951
952 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 ret = mxt_check_bootloader(client,
954 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955 if (ret)
956 goto out;
957
958 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
959
960 /* We should add 2 at frame size as the the firmware data is not
961 * included the CRC bytes.
962 */
963 frame_size += 2;
964
965 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800966 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967
Iiro Valkonen7686b102011-02-02 23:21:58 -0800968 ret = mxt_check_bootloader(client,
969 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700970 if (ret)
971 goto out;
972
973 pos += frame_size;
974
975 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
976 }
977
978out:
979 release_firmware(fw);
980
981 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800982 if (client->addr == MXT_BOOT_LOW)
983 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700984 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800985 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700986
987 return ret;
988}
989
Iiro Valkonen7686b102011-02-02 23:21:58 -0800990static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991 struct device_attribute *attr,
992 const char *buf, size_t count)
993{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700995 int error;
996
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997 disable_irq(data->irq);
998
Iiro Valkonen7686b102011-02-02 23:21:58 -0800999 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000 if (error) {
1001 dev_err(dev, "The firmware update failed(%d)\n", error);
1002 count = error;
1003 } else {
1004 dev_dbg(dev, "The firmware update succeeded\n");
1005
1006 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001007 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001008
1009 kfree(data->object_table);
1010 data->object_table = NULL;
1011
Iiro Valkonen7686b102011-02-02 23:21:58 -08001012 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001013 }
1014
1015 enable_irq(data->irq);
1016
Iiro Valkonen08960a02011-04-12 23:16:40 -07001017 error = mxt_make_highchg(data);
1018 if (error)
1019 return error;
1020
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021 return count;
1022}
1023
Iiro Valkonen7686b102011-02-02 23:21:58 -08001024static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1025static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001026
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001028 &dev_attr_object.attr,
1029 &dev_attr_update_fw.attr,
1030 NULL
1031};
1032
Iiro Valkonen7686b102011-02-02 23:21:58 -08001033static const struct attribute_group mxt_attr_group = {
1034 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035};
1036
Iiro Valkonen7686b102011-02-02 23:21:58 -08001037static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038{
1039 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001040 mxt_write_object(data,
1041 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042}
1043
Iiro Valkonen7686b102011-02-02 23:21:58 -08001044static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045{
1046 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001047 mxt_write_object(data,
1048 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001049}
1050
Iiro Valkonen7686b102011-02-02 23:21:58 -08001051static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001052{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001053 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054
Iiro Valkonen7686b102011-02-02 23:21:58 -08001055 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001056
1057 return 0;
1058}
1059
Iiro Valkonen7686b102011-02-02 23:21:58 -08001060static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001062 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063
Iiro Valkonen7686b102011-02-02 23:21:58 -08001064 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065}
1066
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301067static int mxt_power_on(struct mxt_data *data, bool on)
1068{
1069 int rc;
1070
1071 if (on == false)
1072 goto power_off;
1073
1074 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1075 if (rc < 0) {
1076 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1077 rc);
1078 return rc;
1079 }
1080
1081 rc = regulator_enable(data->vcc);
1082 if (rc) {
1083 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1084 rc);
1085 goto error_reg_en_vcc;
1086 }
1087
1088 if (data->pdata->i2c_pull_up) {
1089 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1090 if (rc < 0) {
1091 dev_err(&data->client->dev,
1092 "Regulator set_opt failed rc=%d\n", rc);
1093 goto error_reg_opt_i2c;
1094 }
1095
1096 rc = regulator_enable(data->vcc_i2c);
1097 if (rc) {
1098 dev_err(&data->client->dev,
1099 "Regulator enable failed rc=%d\n", rc);
1100 goto error_reg_en_vcc_i2c;
1101 }
1102 }
1103
1104 msleep(50);
1105
1106 return 0;
1107
1108error_reg_en_vcc_i2c:
1109 if (data->pdata->i2c_pull_up)
1110 regulator_set_optimum_mode(data->vcc_i2c, 0);
1111error_reg_opt_i2c:
1112 regulator_disable(data->vcc);
1113error_reg_en_vcc:
1114 regulator_set_optimum_mode(data->vcc, 0);
1115 return rc;
1116
1117power_off:
1118 regulator_set_optimum_mode(data->vcc, 0);
1119 regulator_disable(data->vcc);
1120 if (data->pdata->i2c_pull_up) {
1121 regulator_set_optimum_mode(data->vcc_i2c, 0);
1122 regulator_disable(data->vcc_i2c);
1123 }
1124 msleep(50);
1125 return 0;
1126}
1127
1128static int mxt_regulator_configure(struct mxt_data *data, bool on)
1129{
1130 int rc;
1131
1132 if (on == false)
1133 goto hw_shutdown;
1134
1135 data->vcc = regulator_get(&data->client->dev, "vdd");
1136 if (IS_ERR(data->vcc)) {
1137 rc = PTR_ERR(data->vcc);
1138 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1139 rc);
1140 return rc;
1141 }
1142
1143 if (regulator_count_voltages(data->vcc) > 0) {
1144 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1145 MXT_VTG_MAX_UV);
1146 if (rc) {
1147 dev_err(&data->client->dev,
1148 "regulator set_vtg failed rc=%d\n", rc);
1149 goto error_set_vtg_vcc;
1150 }
1151 }
1152
1153 if (data->pdata->i2c_pull_up) {
1154 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1155 if (IS_ERR(data->vcc_i2c)) {
1156 rc = PTR_ERR(data->vcc_i2c);
1157 dev_err(&data->client->dev,
1158 "Regulator get failed rc=%d\n", rc);
1159 goto error_get_vtg_i2c;
1160 }
1161 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1162 rc = regulator_set_voltage(data->vcc_i2c,
1163 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1164 if (rc) {
1165 dev_err(&data->client->dev,
1166 "regulator set_vtg failed rc=%d\n", rc);
1167 goto error_set_vtg_i2c;
1168 }
1169 }
1170 }
1171
1172 return 0;
1173
1174error_set_vtg_i2c:
1175 regulator_put(data->vcc_i2c);
1176error_get_vtg_i2c:
1177 if (regulator_count_voltages(data->vcc) > 0)
1178 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1179error_set_vtg_vcc:
1180 regulator_put(data->vcc);
1181 return rc;
1182
1183hw_shutdown:
1184 if (regulator_count_voltages(data->vcc) > 0)
1185 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1186 regulator_put(data->vcc);
1187 if (data->pdata->i2c_pull_up) {
1188 if (regulator_count_voltages(data->vcc_i2c) > 0)
1189 regulator_set_voltage(data->vcc_i2c, 0,
1190 MXT_I2C_VTG_MAX_UV);
1191 regulator_put(data->vcc_i2c);
1192 }
1193 return 0;
1194}
1195
Iiro Valkonen7686b102011-02-02 23:21:58 -08001196static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001197 const struct i2c_device_id *id)
1198{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001199 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001200 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201 struct input_dev *input_dev;
1202 int error;
1203
Iiro Valkonen919ed892011-02-15 13:36:52 -08001204 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001205 return -EINVAL;
1206
Iiro Valkonen7686b102011-02-02 23:21:58 -08001207 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001208 input_dev = input_allocate_device();
1209 if (!data || !input_dev) {
1210 dev_err(&client->dev, "Failed to allocate memory\n");
1211 error = -ENOMEM;
1212 goto err_free_mem;
1213 }
1214
Iiro Valkonen7686b102011-02-02 23:21:58 -08001215 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001216 input_dev->id.bustype = BUS_I2C;
1217 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001218 input_dev->open = mxt_input_open;
1219 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001220
Joonyoung Shim910d8052011-04-12 23:14:38 -07001221 data->client = client;
1222 data->input_dev = input_dev;
1223 data->pdata = pdata;
1224 data->irq = client->irq;
1225
1226 mxt_calc_resolution(data);
1227
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001228 __set_bit(EV_ABS, input_dev->evbit);
1229 __set_bit(EV_KEY, input_dev->evbit);
1230 __set_bit(BTN_TOUCH, input_dev->keybit);
1231
1232 /* For single touch */
1233 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001234 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001235 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001236 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001237
1238 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001239 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001240 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001241 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001242 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001243 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001244 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001245 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001246
1247 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001248 i2c_set_clientdata(client, data);
1249
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301250 if (pdata->init_hw)
1251 error = pdata->init_hw(true);
1252 else
1253 error = mxt_regulator_configure(data, true);
1254 if (error) {
1255 dev_err(&client->dev, "Failed to intialize hardware\n");
1256 goto err_free_object;
1257 }
1258
1259 if (pdata->power_on)
1260 error = pdata->power_on(true);
1261 else
1262 error = mxt_power_on(data, true);
1263 if (error) {
1264 dev_err(&client->dev, "Failed to power on hardware\n");
1265 goto err_regulator_on;
1266 }
1267
Iiro Valkonen7686b102011-02-02 23:21:58 -08001268 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301270 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001271
Iiro Valkonen7686b102011-02-02 23:21:58 -08001272 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001273 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001274 if (error) {
1275 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301276 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001277 }
1278
Iiro Valkonen08960a02011-04-12 23:16:40 -07001279 error = mxt_make_highchg(data);
1280 if (error)
1281 goto err_free_irq;
1282
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001283 error = input_register_device(input_dev);
1284 if (error)
1285 goto err_free_irq;
1286
Iiro Valkonen7686b102011-02-02 23:21:58 -08001287 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001288 if (error)
1289 goto err_unregister_device;
1290
1291 return 0;
1292
1293err_unregister_device:
1294 input_unregister_device(input_dev);
1295 input_dev = NULL;
1296err_free_irq:
1297 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301298err_power_on:
1299 if (pdata->power_on)
1300 pdata->power_on(false);
1301 else
1302 mxt_power_on(data, false);
1303err_regulator_on:
1304 if (pdata->init_hw)
1305 pdata->init_hw(false);
1306 else
1307 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001308err_free_object:
1309 kfree(data->object_table);
1310err_free_mem:
1311 input_free_device(input_dev);
1312 kfree(data);
1313 return error;
1314}
1315
Iiro Valkonen7686b102011-02-02 23:21:58 -08001316static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001317{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001318 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001319
Iiro Valkonen7686b102011-02-02 23:21:58 -08001320 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001321 free_irq(data->irq, data);
1322 input_unregister_device(data->input_dev);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301323
1324 if (data->pdata->power_on)
1325 data->pdata->power_on(false);
1326 else
1327 mxt_power_on(data, false);
1328
1329 if (data->pdata->init_hw)
1330 data->pdata->init_hw(false);
1331 else
1332 mxt_regulator_configure(data, false);
1333
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001334 kfree(data->object_table);
1335 kfree(data);
1336
1337 return 0;
1338}
1339
1340#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001341static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001342{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001343 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001344 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001345 struct input_dev *input_dev = data->input_dev;
1346
1347 mutex_lock(&input_dev->mutex);
1348
1349 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001350 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001351
1352 mutex_unlock(&input_dev->mutex);
1353
1354 return 0;
1355}
1356
Iiro Valkonen7686b102011-02-02 23:21:58 -08001357static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001359 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001360 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001361 struct input_dev *input_dev = data->input_dev;
1362
1363 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001364 mxt_write_object(data, MXT_GEN_COMMAND,
1365 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001366
Iiro Valkonen7686b102011-02-02 23:21:58 -08001367 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001368
1369 mutex_lock(&input_dev->mutex);
1370
1371 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001372 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001373
1374 mutex_unlock(&input_dev->mutex);
1375
1376 return 0;
1377}
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001378
Iiro Valkonen7686b102011-02-02 23:21:58 -08001379static const struct dev_pm_ops mxt_pm_ops = {
1380 .suspend = mxt_suspend,
1381 .resume = mxt_resume,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001382};
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001383#endif
1384
Iiro Valkonen7686b102011-02-02 23:21:58 -08001385static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001386 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001387 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001388 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389 { }
1390};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001391MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392
Iiro Valkonen7686b102011-02-02 23:21:58 -08001393static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001394 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001395 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001396 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001397#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001398 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001399#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001400 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001401 .probe = mxt_probe,
1402 .remove = __devexit_p(mxt_remove),
1403 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001404};
1405
Iiro Valkonen7686b102011-02-02 23:21:58 -08001406static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001407{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001408 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001409}
1410
Iiro Valkonen7686b102011-02-02 23:21:58 -08001411static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001412{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001413 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001414}
1415
Iiro Valkonen7686b102011-02-02 23:21:58 -08001416module_init(mxt_init);
1417module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001418
1419/* Module information */
1420MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001421MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001422MODULE_LICENSE("GPL");