blob: b35597e179825ef798c14e99999c3951090740ab [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/firmware.h>
18#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080019#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070020#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070021#include <linux/interrupt.h>
22#include <linux/slab.h>
Anirudh Ghayala498e4d2011-08-09 19:10:12 +053023#include <linux/regulator/consumer.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070024
Anirudh Ghayal253ce122011-08-09 19:32:57 +053025#if defined(CONFIG_HAS_EARLYSUSPEND)
26#include <linux/earlysuspend.h>
27/* Early-suspend level */
28#define MXT_SUSPEND_LEVEL 1
29#endif
30
Iiro Valkonen4ac053c2011-09-08 11:10:52 -070031/* Family ID */
32#define MXT224_ID 0x80
33#define MXT1386_ID 0xA0
34
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070035/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080036#define MXT_VER_20 20
37#define MXT_VER_21 21
38#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070039
40/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080041#define MXT_APP_LOW 0x4a
42#define MXT_APP_HIGH 0x4b
43#define MXT_BOOT_LOW 0x24
44#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070045
46/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080047#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070048
49/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080050#define MXT_FAMILY_ID 0x00
51#define MXT_VARIANT_ID 0x01
52#define MXT_VERSION 0x02
53#define MXT_BUILD 0x03
54#define MXT_MATRIX_X_SIZE 0x04
55#define MXT_MATRIX_Y_SIZE 0x05
56#define MXT_OBJECT_NUM 0x06
57#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070058
Iiro Valkonen7686b102011-02-02 23:21:58 -080059#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070060
61/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080062#define MXT_DEBUG_DIAGNOSTIC 37
63#define MXT_GEN_MESSAGE 5
64#define MXT_GEN_COMMAND 6
65#define MXT_GEN_POWER 7
66#define MXT_GEN_ACQUIRE 8
67#define MXT_TOUCH_MULTI 9
68#define MXT_TOUCH_KEYARRAY 15
69#define MXT_TOUCH_PROXIMITY 23
70#define MXT_PROCI_GRIPFACE 20
71#define MXT_PROCG_NOISE 22
72#define MXT_PROCI_ONETOUCH 24
73#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070074#define MXT_PROCI_GRIP 40
75#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070076#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080077#define MXT_SPT_GPIOPWM 19
78#define MXT_SPT_SELFTEST 25
79#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070080#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070081#define MXT_SPT_DIGITIZER 43
82#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070083
Iiro Valkonen7686b102011-02-02 23:21:58 -080084/* MXT_GEN_COMMAND field */
85#define MXT_COMMAND_RESET 0
86#define MXT_COMMAND_BACKUPNV 1
87#define MXT_COMMAND_CALIBRATE 2
88#define MXT_COMMAND_REPORTALL 3
89#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070090
Iiro Valkonen7686b102011-02-02 23:21:58 -080091/* MXT_GEN_POWER field */
92#define MXT_POWER_IDLEACQINT 0
93#define MXT_POWER_ACTVACQINT 1
94#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070095
Iiro Valkonen7686b102011-02-02 23:21:58 -080096/* MXT_GEN_ACQUIRE field */
97#define MXT_ACQUIRE_CHRGTIME 0
98#define MXT_ACQUIRE_TCHDRIFT 2
99#define MXT_ACQUIRE_DRIFTST 3
100#define MXT_ACQUIRE_TCHAUTOCAL 4
101#define MXT_ACQUIRE_SYNC 5
102#define MXT_ACQUIRE_ATCHCALST 6
103#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700104
Iiro Valkonen7686b102011-02-02 23:21:58 -0800105/* MXT_TOUCH_MULTI field */
106#define MXT_TOUCH_CTRL 0
107#define MXT_TOUCH_XORIGIN 1
108#define MXT_TOUCH_YORIGIN 2
109#define MXT_TOUCH_XSIZE 3
110#define MXT_TOUCH_YSIZE 4
111#define MXT_TOUCH_BLEN 6
112#define MXT_TOUCH_TCHTHR 7
113#define MXT_TOUCH_TCHDI 8
114#define MXT_TOUCH_ORIENT 9
115#define MXT_TOUCH_MOVHYSTI 11
116#define MXT_TOUCH_MOVHYSTN 12
117#define MXT_TOUCH_NUMTOUCH 14
118#define MXT_TOUCH_MRGHYST 15
119#define MXT_TOUCH_MRGTHR 16
120#define MXT_TOUCH_AMPHYST 17
121#define MXT_TOUCH_XRANGE_LSB 18
122#define MXT_TOUCH_XRANGE_MSB 19
123#define MXT_TOUCH_YRANGE_LSB 20
124#define MXT_TOUCH_YRANGE_MSB 21
125#define MXT_TOUCH_XLOCLIP 22
126#define MXT_TOUCH_XHICLIP 23
127#define MXT_TOUCH_YLOCLIP 24
128#define MXT_TOUCH_YHICLIP 25
129#define MXT_TOUCH_XEDGECTRL 26
130#define MXT_TOUCH_XEDGEDIST 27
131#define MXT_TOUCH_YEDGECTRL 28
132#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700133#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700134
Iiro Valkonen7686b102011-02-02 23:21:58 -0800135/* MXT_PROCI_GRIPFACE field */
136#define MXT_GRIPFACE_CTRL 0
137#define MXT_GRIPFACE_XLOGRIP 1
138#define MXT_GRIPFACE_XHIGRIP 2
139#define MXT_GRIPFACE_YLOGRIP 3
140#define MXT_GRIPFACE_YHIGRIP 4
141#define MXT_GRIPFACE_MAXTCHS 5
142#define MXT_GRIPFACE_SZTHR1 7
143#define MXT_GRIPFACE_SZTHR2 8
144#define MXT_GRIPFACE_SHPTHR1 9
145#define MXT_GRIPFACE_SHPTHR2 10
146#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700147
Iiro Valkonen7686b102011-02-02 23:21:58 -0800148/* MXT_PROCI_NOISE field */
149#define MXT_NOISE_CTRL 0
150#define MXT_NOISE_OUTFLEN 1
151#define MXT_NOISE_GCAFUL_LSB 3
152#define MXT_NOISE_GCAFUL_MSB 4
153#define MXT_NOISE_GCAFLL_LSB 5
154#define MXT_NOISE_GCAFLL_MSB 6
155#define MXT_NOISE_ACTVGCAFVALID 7
156#define MXT_NOISE_NOISETHR 8
157#define MXT_NOISE_FREQHOPSCALE 10
158#define MXT_NOISE_FREQ0 11
159#define MXT_NOISE_FREQ1 12
160#define MXT_NOISE_FREQ2 13
161#define MXT_NOISE_FREQ3 14
162#define MXT_NOISE_FREQ4 15
163#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonen7686b102011-02-02 23:21:58 -0800165/* MXT_SPT_COMMSCONFIG */
166#define MXT_COMMS_CTRL 0
167#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700168
Iiro Valkonen7686b102011-02-02 23:21:58 -0800169/* MXT_SPT_CTECONFIG field */
170#define MXT_CTE_CTRL 0
171#define MXT_CTE_CMD 1
172#define MXT_CTE_MODE 2
173#define MXT_CTE_IDLEGCAFDEPTH 3
174#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700175#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700176
Iiro Valkonen7686b102011-02-02 23:21:58 -0800177#define MXT_VOLTAGE_DEFAULT 2700000
178#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700179
Anirudh Ghayala498e4d2011-08-09 19:10:12 +0530180#define MXT_VTG_MIN_UV 2700000
181#define MXT_VTG_MAX_UV 3300000
182#define MXT_ACTIVE_LOAD_UA 15000
183
184#define MXT_I2C_VTG_MIN_UV 1800000
185#define MXT_I2C_VTG_MAX_UV 1800000
186#define MXT_I2C_LOAD_UA 10000
187
Iiro Valkonen7686b102011-02-02 23:21:58 -0800188/* Define for MXT_GEN_COMMAND */
189#define MXT_BOOT_VALUE 0xa5
190#define MXT_BACKUP_VALUE 0x55
191#define MXT_BACKUP_TIME 25 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700192#define MXT224_RESET_TIME 65 /* msec */
Amy Maloche7e447432011-09-14 11:36:30 -0700193#define MXT1386_RESET_TIME 250 /* msec */
194#define MXT_RESET_TIME 250 /* msec */
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700195#define MXT_RESET_NOCHGREAD 400 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700196
Iiro Valkonen7686b102011-02-02 23:21:58 -0800197#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700198
199/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_UNLOCK_CMD_MSB 0xaa
201#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700202
203/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800204#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
205#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
206#define MXT_FRAME_CRC_CHECK 0x02
207#define MXT_FRAME_CRC_FAIL 0x03
208#define MXT_FRAME_CRC_PASS 0x04
209#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
210#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700211
212/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800213#define MXT_SUPPRESS (1 << 1)
214#define MXT_AMP (1 << 2)
215#define MXT_VECTOR (1 << 3)
216#define MXT_MOVE (1 << 4)
217#define MXT_RELEASE (1 << 5)
218#define MXT_PRESS (1 << 6)
219#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700220
Joonyoung Shim910d8052011-04-12 23:14:38 -0700221/* Touch orient bits */
222#define MXT_XY_SWITCH (1 << 0)
223#define MXT_X_INVERT (1 << 1)
224#define MXT_Y_INVERT (1 << 2)
225
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700226/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800227#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700228
Iiro Valkonen7686b102011-02-02 23:21:58 -0800229#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700230
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530231#define MXT_BUFF_SIZE 100
232
Iiro Valkonen7686b102011-02-02 23:21:58 -0800233struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700234 u8 family_id;
235 u8 variant_id;
236 u8 version;
237 u8 build;
238 u8 matrix_xsize;
239 u8 matrix_ysize;
240 u8 object_num;
241};
242
Iiro Valkonen7686b102011-02-02 23:21:58 -0800243struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700244 u8 type;
245 u16 start_address;
246 u8 size;
247 u8 instances;
248 u8 num_report_ids;
249
250 /* to map object and message */
251 u8 max_reportid;
252};
253
Iiro Valkonen7686b102011-02-02 23:21:58 -0800254struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700255 u8 reportid;
256 u8 message[7];
257 u8 checksum;
258};
259
Iiro Valkonen7686b102011-02-02 23:21:58 -0800260struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700261 int status;
262 int x;
263 int y;
264 int area;
265};
266
267/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800268struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700269 struct i2c_client *client;
270 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800271 const struct mxt_platform_data *pdata;
272 struct mxt_object *object_table;
273 struct mxt_info info;
274 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700275 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700276 unsigned int max_x;
277 unsigned int max_y;
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
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700283};
284
Iiro Valkonen7686b102011-02-02 23:21:58 -0800285static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700286{
287 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800288 case MXT_GEN_MESSAGE:
289 case MXT_GEN_COMMAND:
290 case MXT_GEN_POWER:
291 case MXT_GEN_ACQUIRE:
292 case MXT_TOUCH_MULTI:
293 case MXT_TOUCH_KEYARRAY:
294 case MXT_TOUCH_PROXIMITY:
295 case MXT_PROCI_GRIPFACE:
296 case MXT_PROCG_NOISE:
297 case MXT_PROCI_ONETOUCH:
298 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700299 case MXT_PROCI_GRIP:
300 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800301 case MXT_SPT_COMMSCONFIG:
302 case MXT_SPT_GPIOPWM:
303 case MXT_SPT_SELFTEST:
304 case MXT_SPT_CTECONFIG:
305 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530306 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700307 return true;
308 default:
309 return false;
310 }
311}
312
Iiro Valkonen7686b102011-02-02 23:21:58 -0800313static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700314{
315 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800316 case MXT_GEN_COMMAND:
317 case MXT_GEN_POWER:
318 case MXT_GEN_ACQUIRE:
319 case MXT_TOUCH_MULTI:
320 case MXT_TOUCH_KEYARRAY:
321 case MXT_TOUCH_PROXIMITY:
322 case MXT_PROCI_GRIPFACE:
323 case MXT_PROCG_NOISE:
324 case MXT_PROCI_ONETOUCH:
325 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700326 case MXT_PROCI_GRIP:
327 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800328 case MXT_SPT_GPIOPWM:
329 case MXT_SPT_SELFTEST:
330 case MXT_SPT_CTECONFIG:
Anirudh Ghayalf1071c02011-08-09 19:39:36 +0530331 case MXT_SPT_USERDATA:
Anirudh Ghayalba3bc7a2011-09-05 18:34:40 +0530332 case MXT_SPT_DIGITIZER:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700333 return true;
334 default:
335 return false;
336 }
337}
338
Iiro Valkonen7686b102011-02-02 23:21:58 -0800339static void mxt_dump_message(struct device *dev,
340 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700341{
342 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
343 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
344 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
345 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
346 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
347 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
348 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
349 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
350 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
351}
352
Iiro Valkonen7686b102011-02-02 23:21:58 -0800353static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700354 unsigned int state)
355{
356 u8 val;
357
358recheck:
359 if (i2c_master_recv(client, &val, 1) != 1) {
360 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
361 return -EIO;
362 }
363
364 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800365 case MXT_WAITING_BOOTLOAD_CMD:
366 case MXT_WAITING_FRAME_DATA:
367 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700368 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800369 case MXT_FRAME_CRC_PASS:
370 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700371 goto recheck;
372 break;
373 default:
374 return -EINVAL;
375 }
376
377 if (val != state) {
378 dev_err(&client->dev, "Unvalid bootloader mode state\n");
379 return -EINVAL;
380 }
381
382 return 0;
383}
384
Iiro Valkonen7686b102011-02-02 23:21:58 -0800385static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700386{
387 u8 buf[2];
388
Iiro Valkonen7686b102011-02-02 23:21:58 -0800389 buf[0] = MXT_UNLOCK_CMD_LSB;
390 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700391
392 if (i2c_master_send(client, buf, 2) != 2) {
393 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
394 return -EIO;
395 }
396
397 return 0;
398}
399
Iiro Valkonen7686b102011-02-02 23:21:58 -0800400static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700401 const u8 *data, unsigned int frame_size)
402{
403 if (i2c_master_send(client, data, frame_size) != frame_size) {
404 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
405 return -EIO;
406 }
407
408 return 0;
409}
410
Iiro Valkonen7686b102011-02-02 23:21:58 -0800411static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700412 u16 reg, u16 len, void *val)
413{
414 struct i2c_msg xfer[2];
415 u8 buf[2];
416
417 buf[0] = reg & 0xff;
418 buf[1] = (reg >> 8) & 0xff;
419
420 /* Write register */
421 xfer[0].addr = client->addr;
422 xfer[0].flags = 0;
423 xfer[0].len = 2;
424 xfer[0].buf = buf;
425
426 /* Read data */
427 xfer[1].addr = client->addr;
428 xfer[1].flags = I2C_M_RD;
429 xfer[1].len = len;
430 xfer[1].buf = val;
431
432 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
433 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
434 return -EIO;
435 }
436
437 return 0;
438}
439
Iiro Valkonen7686b102011-02-02 23:21:58 -0800440static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700441{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800442 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700443}
444
Iiro Valkonen7686b102011-02-02 23:21:58 -0800445static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700446{
447 u8 buf[3];
448
449 buf[0] = reg & 0xff;
450 buf[1] = (reg >> 8) & 0xff;
451 buf[2] = val;
452
453 if (i2c_master_send(client, buf, 3) != 3) {
454 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
455 return -EIO;
456 }
457
458 return 0;
459}
460
Iiro Valkonen7686b102011-02-02 23:21:58 -0800461static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700462 u16 reg, u8 *object_buf)
463{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800464 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700465 object_buf);
466}
467
Iiro Valkonen7686b102011-02-02 23:21:58 -0800468static struct mxt_object *
469mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700470{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800471 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700472 int i;
473
474 for (i = 0; i < data->info.object_num; i++) {
475 object = data->object_table + i;
476 if (object->type == type)
477 return object;
478 }
479
480 dev_err(&data->client->dev, "Invalid object type\n");
481 return NULL;
482}
483
Iiro Valkonen7686b102011-02-02 23:21:58 -0800484static int mxt_read_message(struct mxt_data *data,
485 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700486{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800487 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488 u16 reg;
489
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491 if (!object)
492 return -EINVAL;
493
494 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800495 return __mxt_read_reg(data->client, reg,
496 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497}
498
Iiro Valkonen7686b102011-02-02 23:21:58 -0800499static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700500 u8 type, u8 offset, u8 *val)
501{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800502 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700503 u16 reg;
504
Iiro Valkonen7686b102011-02-02 23:21:58 -0800505 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700506 if (!object)
507 return -EINVAL;
508
509 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800510 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511}
512
Iiro Valkonen7686b102011-02-02 23:21:58 -0800513static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514 u8 type, u8 offset, u8 val)
515{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517 u16 reg;
518
Iiro Valkonen7686b102011-02-02 23:21:58 -0800519 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700520 if (!object)
521 return -EINVAL;
522
523 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525}
526
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700528{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800529 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530 struct input_dev *input_dev = data->input_dev;
531 int status = finger[single_id].status;
532 int finger_num = 0;
533 int id;
534
Iiro Valkonen7686b102011-02-02 23:21:58 -0800535 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536 if (!finger[id].status)
537 continue;
538
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700539 input_mt_slot(input_dev, id);
540 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
541 finger[id].status != MXT_RELEASE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700543 if (finger[id].status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544 finger_num++;
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -0700545 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
546 finger[id].area);
547 input_report_abs(input_dev, ABS_MT_POSITION_X,
548 finger[id].x);
549 input_report_abs(input_dev, ABS_MT_POSITION_Y,
550 finger[id].y);
551 } else {
552 finger[id].status = 0;
553 }
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));
593 if (data->max_x < 1024)
594 x = x >> 2;
595 if (data->max_y < 1024)
596 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 void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800709 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700710 u8 voltage;
711
712 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800713 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800715 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716 pdata->y_line);
717
718 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800719 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720 pdata->orient);
721
722 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800723 mxt_write_object(data, MXT_TOUCH_MULTI,
724 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725
726 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800727 mxt_write_object(data, MXT_TOUCH_MULTI,
728 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700729
730 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800731 mxt_write_object(data, MXT_TOUCH_MULTI,
732 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
733 mxt_write_object(data, MXT_TOUCH_MULTI,
734 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
735 mxt_write_object(data, MXT_TOUCH_MULTI,
736 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
737 mxt_write_object(data, MXT_TOUCH_MULTI,
738 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739
740 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700741 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800742 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
743 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
744 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700745 voltage = 0xff - voltage + 1;
746 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
748 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749
Iiro Valkonen7686b102011-02-02 23:21:58 -0800750 mxt_write_object(data, MXT_SPT_CTECONFIG,
751 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752 }
753}
754
Iiro Valkonen7686b102011-02-02 23:21:58 -0800755static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756{
757 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800758 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700759 int error;
760 u8 val;
761
Iiro Valkonen7686b102011-02-02 23:21:58 -0800762 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700763 if (error)
764 return error;
765 info->family_id = val;
766
Iiro Valkonen7686b102011-02-02 23:21:58 -0800767 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700768 if (error)
769 return error;
770 info->variant_id = val;
771
Iiro Valkonen7686b102011-02-02 23:21:58 -0800772 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700773 if (error)
774 return error;
775 info->version = val;
776
Iiro Valkonen7686b102011-02-02 23:21:58 -0800777 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700778 if (error)
779 return error;
780 info->build = val;
781
Iiro Valkonen7686b102011-02-02 23:21:58 -0800782 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783 if (error)
784 return error;
785 info->object_num = val;
786
787 return 0;
788}
789
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791{
792 int error;
793 int i;
794 u16 reg;
795 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800796 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700797
798 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800799 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700800
Iiro Valkonen7686b102011-02-02 23:21:58 -0800801 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
802 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700803 if (error)
804 return error;
805
806 object->type = buf[0];
807 object->start_address = (buf[2] << 8) | buf[1];
808 object->size = buf[3];
809 object->instances = buf[4];
810 object->num_report_ids = buf[5];
811
812 if (object->num_report_ids) {
813 reportid += object->num_report_ids *
814 (object->instances + 1);
815 object->max_reportid = reportid;
816 }
817 }
818
819 return 0;
820}
821
Amy Maloche7e447432011-09-14 11:36:30 -0700822static void mxt_reset_delay(struct mxt_data *data)
823{
824 struct mxt_info *info = &data->info;
825
826 switch (info->family_id) {
827 case MXT224_ID:
828 msleep(MXT224_RESET_TIME);
829 break;
830 case MXT1386_ID:
831 msleep(MXT1386_RESET_TIME);
832 break;
833 default:
834 msleep(MXT_RESET_TIME);
835 }
836}
837
Iiro Valkonen7686b102011-02-02 23:21:58 -0800838static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700839{
840 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800841 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842 int error;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700843 int timeout_counter = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700844 u8 val;
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700845 u8 command_register;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700846
Iiro Valkonen7686b102011-02-02 23:21:58 -0800847 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700848 if (error)
849 return error;
850
851 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800852 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700853 GFP_KERNEL);
854 if (!data->object_table) {
855 dev_err(&client->dev, "Failed to allocate memory\n");
856 return -ENOMEM;
857 }
858
859 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800860 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700861 if (error)
862 return error;
863
864 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800865 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700866 if (error)
867 return error;
868
Iiro Valkonen7686b102011-02-02 23:21:58 -0800869 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700870
871 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800872 mxt_write_object(data, MXT_GEN_COMMAND,
873 MXT_COMMAND_BACKUPNV,
874 MXT_BACKUP_VALUE);
875 msleep(MXT_BACKUP_TIME);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700876 do {
877 error = mxt_read_object(data, MXT_GEN_COMMAND,
878 MXT_COMMAND_BACKUPNV,
879 &command_register);
880 if (error)
881 return error;
Amy Maloche7e447432011-09-14 11:36:30 -0700882 usleep_range(1000, 2000);
883 } while ((command_register != 0) && (++timeout_counter <= 100));
884 if (timeout_counter > 100) {
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700885 dev_err(&client->dev, "No response after backup!\n");
886 return -EIO;
887 }
888
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700889
890 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800891 mxt_write_object(data, MXT_GEN_COMMAND,
892 MXT_COMMAND_RESET, 1);
Iiro Valkonen4ac053c2011-09-08 11:10:52 -0700893
Amy Maloche7e447432011-09-14 11:36:30 -0700894 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700895
896 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800897 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700898 if (error)
899 return error;
900 info->matrix_xsize = val;
901
Iiro Valkonen7686b102011-02-02 23:21:58 -0800902 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700903 if (error)
904 return error;
905 info->matrix_ysize = val;
906
907 dev_info(&client->dev,
908 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
909 info->family_id, info->variant_id, info->version,
910 info->build);
911
912 dev_info(&client->dev,
913 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
914 info->matrix_xsize, info->matrix_ysize,
915 info->object_num);
916
917 return 0;
918}
919
Joonyoung Shim910d8052011-04-12 23:14:38 -0700920static void mxt_calc_resolution(struct mxt_data *data)
921{
922 unsigned int max_x = data->pdata->x_size - 1;
923 unsigned int max_y = data->pdata->y_size - 1;
924
925 if (data->pdata->orient & MXT_XY_SWITCH) {
926 data->max_x = max_y;
927 data->max_y = max_x;
928 } else {
929 data->max_x = max_x;
930 data->max_y = max_y;
931 }
932}
933
Iiro Valkonen7686b102011-02-02 23:21:58 -0800934static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700935 struct device_attribute *attr, char *buf)
936{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800937 struct mxt_data *data = dev_get_drvdata(dev);
938 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700939 int count = 0;
940 int i, j;
941 int error;
942 u8 val;
943
944 for (i = 0; i < data->info.object_num; i++) {
945 object = data->object_table + i;
946
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530947 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700948 "Object Table Element %d(Type %d)\n",
949 i + 1, object->type);
950
Iiro Valkonen7686b102011-02-02 23:21:58 -0800951 if (!mxt_object_readable(object->type)) {
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530952 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953 continue;
954 }
955
956 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958 object->type, j, &val);
959 if (error)
960 return error;
961
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530962 count += snprintf(buf + count, MXT_BUFF_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700963 " Byte %d: 0x%x (%d)\n", j, val, val);
964 }
965
Mohan Pallakaab51f2b2011-09-29 18:17:35 +0530966 count += snprintf(buf + count, MXT_BUFF_SIZE, "\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967 }
968
969 return count;
970}
971
Iiro Valkonen7686b102011-02-02 23:21:58 -0800972static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700973{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800974 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700975 struct i2c_client *client = data->client;
976 const struct firmware *fw = NULL;
977 unsigned int frame_size;
978 unsigned int pos = 0;
979 int ret;
980
981 ret = request_firmware(&fw, fn, dev);
982 if (ret) {
983 dev_err(dev, "Unable to open firmware %s\n", fn);
984 return ret;
985 }
986
987 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800988 mxt_write_object(data, MXT_GEN_COMMAND,
989 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
Amy Maloche7e447432011-09-14 11:36:30 -0700990
991 mxt_reset_delay(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700992
993 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 if (client->addr == MXT_APP_LOW)
995 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700996 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800997 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998
Iiro Valkonen7686b102011-02-02 23:21:58 -0800999 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000 if (ret)
1001 goto out;
1002
1003 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001004 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005
1006 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001007 ret = mxt_check_bootloader(client,
1008 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009 if (ret)
1010 goto out;
1011
1012 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1013
1014 /* We should add 2 at frame size as the the firmware data is not
1015 * included the CRC bytes.
1016 */
1017 frame_size += 2;
1018
1019 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001020 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022 ret = mxt_check_bootloader(client,
1023 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001024 if (ret)
1025 goto out;
1026
1027 pos += frame_size;
1028
1029 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1030 }
1031
1032out:
1033 release_firmware(fw);
1034
1035 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001036 if (client->addr == MXT_BOOT_LOW)
1037 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001039 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040
1041 return ret;
1042}
1043
Iiro Valkonen7686b102011-02-02 23:21:58 -08001044static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045 struct device_attribute *attr,
1046 const char *buf, size_t count)
1047{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001048 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001049 int error;
1050
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001051 disable_irq(data->irq);
1052
Iiro Valkonen7686b102011-02-02 23:21:58 -08001053 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054 if (error) {
1055 dev_err(dev, "The firmware update failed(%d)\n", error);
1056 count = error;
1057 } else {
1058 dev_dbg(dev, "The firmware update succeeded\n");
1059
1060 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001061 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001062
1063 kfree(data->object_table);
1064 data->object_table = NULL;
1065
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067 }
1068
1069 enable_irq(data->irq);
1070
Iiro Valkonen08960a02011-04-12 23:16:40 -07001071 error = mxt_make_highchg(data);
1072 if (error)
1073 return error;
1074
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001075 return count;
1076}
1077
Iiro Valkonen7686b102011-02-02 23:21:58 -08001078static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1079static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080
Iiro Valkonen7686b102011-02-02 23:21:58 -08001081static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001082 &dev_attr_object.attr,
1083 &dev_attr_update_fw.attr,
1084 NULL
1085};
1086
Iiro Valkonen7686b102011-02-02 23:21:58 -08001087static const struct attribute_group mxt_attr_group = {
1088 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001089};
1090
Iiro Valkonen7686b102011-02-02 23:21:58 -08001091static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001092{
1093 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001094 mxt_write_object(data,
1095 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001096}
1097
Iiro Valkonen7686b102011-02-02 23:21:58 -08001098static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001099{
1100 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001101 mxt_write_object(data,
1102 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001103}
1104
Iiro Valkonen7686b102011-02-02 23:21:58 -08001105static int mxt_input_open(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);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001108
Iiro Valkonen7686b102011-02-02 23:21:58 -08001109 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001110
1111 return 0;
1112}
1113
Iiro Valkonen7686b102011-02-02 23:21:58 -08001114static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001115{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001116 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001117
Iiro Valkonen7686b102011-02-02 23:21:58 -08001118 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001119}
1120
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301121static int mxt_power_on(struct mxt_data *data, bool on)
1122{
1123 int rc;
1124
1125 if (on == false)
1126 goto power_off;
1127
1128 rc = regulator_set_optimum_mode(data->vcc, MXT_ACTIVE_LOAD_UA);
1129 if (rc < 0) {
1130 dev_err(&data->client->dev, "Regulator set_opt failed rc=%d\n",
1131 rc);
1132 return rc;
1133 }
1134
1135 rc = regulator_enable(data->vcc);
1136 if (rc) {
1137 dev_err(&data->client->dev, "Regulator enable failed rc=%d\n",
1138 rc);
1139 goto error_reg_en_vcc;
1140 }
1141
1142 if (data->pdata->i2c_pull_up) {
1143 rc = regulator_set_optimum_mode(data->vcc_i2c, MXT_I2C_LOAD_UA);
1144 if (rc < 0) {
1145 dev_err(&data->client->dev,
1146 "Regulator set_opt failed rc=%d\n", rc);
1147 goto error_reg_opt_i2c;
1148 }
1149
1150 rc = regulator_enable(data->vcc_i2c);
1151 if (rc) {
1152 dev_err(&data->client->dev,
1153 "Regulator enable failed rc=%d\n", rc);
1154 goto error_reg_en_vcc_i2c;
1155 }
1156 }
1157
Amy Maloche7e447432011-09-14 11:36:30 -07001158 msleep(100);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301159
1160 return 0;
1161
1162error_reg_en_vcc_i2c:
1163 if (data->pdata->i2c_pull_up)
1164 regulator_set_optimum_mode(data->vcc_i2c, 0);
1165error_reg_opt_i2c:
1166 regulator_disable(data->vcc);
1167error_reg_en_vcc:
1168 regulator_set_optimum_mode(data->vcc, 0);
1169 return rc;
1170
1171power_off:
1172 regulator_set_optimum_mode(data->vcc, 0);
1173 regulator_disable(data->vcc);
1174 if (data->pdata->i2c_pull_up) {
1175 regulator_set_optimum_mode(data->vcc_i2c, 0);
1176 regulator_disable(data->vcc_i2c);
1177 }
1178 msleep(50);
1179 return 0;
1180}
1181
1182static int mxt_regulator_configure(struct mxt_data *data, bool on)
1183{
1184 int rc;
1185
1186 if (on == false)
1187 goto hw_shutdown;
1188
1189 data->vcc = regulator_get(&data->client->dev, "vdd");
1190 if (IS_ERR(data->vcc)) {
1191 rc = PTR_ERR(data->vcc);
1192 dev_err(&data->client->dev, "Regulator get failed rc=%d\n",
1193 rc);
1194 return rc;
1195 }
1196
1197 if (regulator_count_voltages(data->vcc) > 0) {
1198 rc = regulator_set_voltage(data->vcc, MXT_VTG_MIN_UV,
1199 MXT_VTG_MAX_UV);
1200 if (rc) {
1201 dev_err(&data->client->dev,
1202 "regulator set_vtg failed rc=%d\n", rc);
1203 goto error_set_vtg_vcc;
1204 }
1205 }
1206
1207 if (data->pdata->i2c_pull_up) {
1208 data->vcc_i2c = regulator_get(&data->client->dev, "vcc_i2c");
1209 if (IS_ERR(data->vcc_i2c)) {
1210 rc = PTR_ERR(data->vcc_i2c);
1211 dev_err(&data->client->dev,
1212 "Regulator get failed rc=%d\n", rc);
1213 goto error_get_vtg_i2c;
1214 }
1215 if (regulator_count_voltages(data->vcc_i2c) > 0) {
1216 rc = regulator_set_voltage(data->vcc_i2c,
1217 MXT_I2C_VTG_MIN_UV, MXT_I2C_VTG_MAX_UV);
1218 if (rc) {
1219 dev_err(&data->client->dev,
1220 "regulator set_vtg failed rc=%d\n", rc);
1221 goto error_set_vtg_i2c;
1222 }
1223 }
1224 }
1225
1226 return 0;
1227
1228error_set_vtg_i2c:
1229 regulator_put(data->vcc_i2c);
1230error_get_vtg_i2c:
1231 if (regulator_count_voltages(data->vcc) > 0)
1232 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1233error_set_vtg_vcc:
1234 regulator_put(data->vcc);
1235 return rc;
1236
1237hw_shutdown:
1238 if (regulator_count_voltages(data->vcc) > 0)
1239 regulator_set_voltage(data->vcc, 0, MXT_VTG_MAX_UV);
1240 regulator_put(data->vcc);
1241 if (data->pdata->i2c_pull_up) {
1242 if (regulator_count_voltages(data->vcc_i2c) > 0)
1243 regulator_set_voltage(data->vcc_i2c, 0,
1244 MXT_I2C_VTG_MAX_UV);
1245 regulator_put(data->vcc_i2c);
1246 }
1247 return 0;
1248}
1249
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301250#ifdef CONFIG_PM
1251static int mxt_suspend(struct device *dev)
1252{
1253 struct i2c_client *client = to_i2c_client(dev);
1254 struct mxt_data *data = i2c_get_clientdata(client);
1255 struct input_dev *input_dev = data->input_dev;
1256
1257 mutex_lock(&input_dev->mutex);
1258
1259 if (input_dev->users)
1260 mxt_stop(data);
1261
1262 mutex_unlock(&input_dev->mutex);
1263
1264 return 0;
1265}
1266
1267static int mxt_resume(struct device *dev)
1268{
1269 struct i2c_client *client = to_i2c_client(dev);
1270 struct mxt_data *data = i2c_get_clientdata(client);
1271 struct input_dev *input_dev = data->input_dev;
1272
1273 /* Soft reset */
1274 mxt_write_object(data, MXT_GEN_COMMAND,
1275 MXT_COMMAND_RESET, 1);
1276
Amy Maloche7e447432011-09-14 11:36:30 -07001277 mxt_reset_delay(data);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301278
1279 mutex_lock(&input_dev->mutex);
1280
1281 if (input_dev->users)
1282 mxt_start(data);
1283
1284 mutex_unlock(&input_dev->mutex);
1285
1286 return 0;
1287}
1288
1289#if defined(CONFIG_HAS_EARLYSUSPEND)
1290static void mxt_early_suspend(struct early_suspend *h)
1291{
1292 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1293
1294 mxt_suspend(&data->client->dev);
1295}
1296
1297static void mxt_late_resume(struct early_suspend *h)
1298{
1299 struct mxt_data *data = container_of(h, struct mxt_data, early_suspend);
1300
1301 mxt_resume(&data->client->dev);
1302}
1303#endif
1304
1305static const struct dev_pm_ops mxt_pm_ops = {
1306#ifndef CONFIG_HAS_EARLYSUSPEND
1307 .suspend = mxt_suspend,
1308 .resume = mxt_resume,
1309#endif
1310};
1311#endif
1312
Iiro Valkonen7686b102011-02-02 23:21:58 -08001313static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001314 const struct i2c_device_id *id)
1315{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001316 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001317 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001318 struct input_dev *input_dev;
1319 int error;
1320
Iiro Valkonen919ed892011-02-15 13:36:52 -08001321 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001322 return -EINVAL;
1323
Iiro Valkonen7686b102011-02-02 23:21:58 -08001324 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001325 input_dev = input_allocate_device();
1326 if (!data || !input_dev) {
1327 dev_err(&client->dev, "Failed to allocate memory\n");
1328 error = -ENOMEM;
1329 goto err_free_mem;
1330 }
1331
Iiro Valkonen7686b102011-02-02 23:21:58 -08001332 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001333 input_dev->id.bustype = BUS_I2C;
1334 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001335 input_dev->open = mxt_input_open;
1336 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001337
Joonyoung Shim910d8052011-04-12 23:14:38 -07001338 data->client = client;
1339 data->input_dev = input_dev;
1340 data->pdata = pdata;
1341 data->irq = client->irq;
1342
1343 mxt_calc_resolution(data);
1344
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001345 __set_bit(EV_ABS, input_dev->evbit);
1346 __set_bit(EV_KEY, input_dev->evbit);
1347 __set_bit(BTN_TOUCH, input_dev->keybit);
1348
1349 /* For single touch */
1350 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001351 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001352 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001353 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001354
1355 /* For multi touch */
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -07001356 input_mt_init_slots(input_dev, MXT_MAX_FINGER);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001357 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001358 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001359 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001360 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001361 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001362 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001363
1364 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001365 i2c_set_clientdata(client, data);
1366
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301367 if (pdata->init_hw)
1368 error = pdata->init_hw(true);
1369 else
1370 error = mxt_regulator_configure(data, true);
1371 if (error) {
1372 dev_err(&client->dev, "Failed to intialize hardware\n");
1373 goto err_free_object;
1374 }
1375
1376 if (pdata->power_on)
1377 error = pdata->power_on(true);
1378 else
1379 error = mxt_power_on(data, true);
1380 if (error) {
1381 dev_err(&client->dev, "Failed to power on hardware\n");
1382 goto err_regulator_on;
1383 }
1384
Iiro Valkonen7686b102011-02-02 23:21:58 -08001385 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001386 if (error)
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301387 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001388
Iiro Valkonen7686b102011-02-02 23:21:58 -08001389 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001390 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001391 if (error) {
1392 dev_err(&client->dev, "Failed to register interrupt\n");
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301393 goto err_power_on;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001394 }
1395
Iiro Valkonen08960a02011-04-12 23:16:40 -07001396 error = mxt_make_highchg(data);
1397 if (error)
1398 goto err_free_irq;
1399
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001400 error = input_register_device(input_dev);
1401 if (error)
1402 goto err_free_irq;
1403
Iiro Valkonen7686b102011-02-02 23:21:58 -08001404 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001405 if (error)
1406 goto err_unregister_device;
1407
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301408#if defined(CONFIG_HAS_EARLYSUSPEND)
1409 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
1410 MXT_SUSPEND_LEVEL;
1411 data->early_suspend.suspend = mxt_early_suspend;
1412 data->early_suspend.resume = mxt_late_resume;
1413 register_early_suspend(&data->early_suspend);
1414#endif
1415
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001416 return 0;
1417
1418err_unregister_device:
1419 input_unregister_device(input_dev);
1420 input_dev = NULL;
1421err_free_irq:
1422 free_irq(client->irq, data);
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301423err_power_on:
1424 if (pdata->power_on)
1425 pdata->power_on(false);
1426 else
1427 mxt_power_on(data, false);
1428err_regulator_on:
1429 if (pdata->init_hw)
1430 pdata->init_hw(false);
1431 else
1432 mxt_regulator_configure(data, false);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001433err_free_object:
1434 kfree(data->object_table);
1435err_free_mem:
1436 input_free_device(input_dev);
1437 kfree(data);
1438 return error;
1439}
1440
Iiro Valkonen7686b102011-02-02 23:21:58 -08001441static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001442{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001443 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001444
Iiro Valkonen7686b102011-02-02 23:21:58 -08001445 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001446 free_irq(data->irq, data);
1447 input_unregister_device(data->input_dev);
Anirudh Ghayal253ce122011-08-09 19:32:57 +05301448#if defined(CONFIG_HAS_EARLYSUSPEND)
1449 unregister_early_suspend(&data->early_suspend);
1450#endif
Anirudh Ghayala498e4d2011-08-09 19:10:12 +05301451
1452 if (data->pdata->power_on)
1453 data->pdata->power_on(false);
1454 else
1455 mxt_power_on(data, false);
1456
1457 if (data->pdata->init_hw)
1458 data->pdata->init_hw(false);
1459 else
1460 mxt_regulator_configure(data, false);
1461
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001462 kfree(data->object_table);
1463 kfree(data);
1464
1465 return 0;
1466}
1467
Iiro Valkonen7686b102011-02-02 23:21:58 -08001468static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001469 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001470 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001471 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001472 { }
1473};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001474MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001475
Iiro Valkonen7686b102011-02-02 23:21:58 -08001476static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001477 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001478 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001479 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001480#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001481 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001482#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001483 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001484 .probe = mxt_probe,
1485 .remove = __devexit_p(mxt_remove),
1486 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001487};
1488
Iiro Valkonen7686b102011-02-02 23:21:58 -08001489static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001490{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001491 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001492}
1493
Iiro Valkonen7686b102011-02-02 23:21:58 -08001494static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001495{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001496 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001497}
1498
Iiro Valkonen7686b102011-02-02 23:21:58 -08001499module_init(mxt_init);
1500module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001501
1502/* Module information */
1503MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001504MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001505MODULE_LICENSE("GPL");