blob: 2accf1dffee9befe6ea38de353484eb14b55d82c [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 Shim4cf51c32010-07-14 21:55:30 -070020#include <linux/input.h>
21#include <linux/interrupt.h>
22#include <linux/slab.h>
23
24/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080025#define MXT_VER_20 20
26#define MXT_VER_21 21
27#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070028
29/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080030#define MXT_APP_LOW 0x4a
31#define MXT_APP_HIGH 0x4b
32#define MXT_BOOT_LOW 0x24
33#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070034
35/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080036#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070037
38/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080039#define MXT_FAMILY_ID 0x00
40#define MXT_VARIANT_ID 0x01
41#define MXT_VERSION 0x02
42#define MXT_BUILD 0x03
43#define MXT_MATRIX_X_SIZE 0x04
44#define MXT_MATRIX_Y_SIZE 0x05
45#define MXT_OBJECT_NUM 0x06
46#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070047
Iiro Valkonen7686b102011-02-02 23:21:58 -080048#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
50/* Object types */
Iiro Valkonen7686b102011-02-02 23:21:58 -080051#define MXT_DEBUG_DIAGNOSTIC 37
52#define MXT_GEN_MESSAGE 5
53#define MXT_GEN_COMMAND 6
54#define MXT_GEN_POWER 7
55#define MXT_GEN_ACQUIRE 8
56#define MXT_TOUCH_MULTI 9
57#define MXT_TOUCH_KEYARRAY 15
58#define MXT_TOUCH_PROXIMITY 23
59#define MXT_PROCI_GRIPFACE 20
60#define MXT_PROCG_NOISE 22
61#define MXT_PROCI_ONETOUCH 24
62#define MXT_PROCI_TWOTOUCH 27
Joonyoung Shim4c75de32011-03-14 21:41:40 -070063#define MXT_PROCI_GRIP 40
64#define MXT_PROCI_PALM 41
Joonyoung Shim979a72d2011-03-14 21:41:34 -070065#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080066#define MXT_SPT_GPIOPWM 19
67#define MXT_SPT_SELFTEST 25
68#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070069#define MXT_SPT_USERDATA 38
Joonyoung Shim4c75de32011-03-14 21:41:40 -070070#define MXT_SPT_DIGITIZER 43
71#define MXT_SPT_MESSAGECOUNT 44
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070072
Iiro Valkonen7686b102011-02-02 23:21:58 -080073/* MXT_GEN_COMMAND field */
74#define MXT_COMMAND_RESET 0
75#define MXT_COMMAND_BACKUPNV 1
76#define MXT_COMMAND_CALIBRATE 2
77#define MXT_COMMAND_REPORTALL 3
78#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070079
Iiro Valkonen7686b102011-02-02 23:21:58 -080080/* MXT_GEN_POWER field */
81#define MXT_POWER_IDLEACQINT 0
82#define MXT_POWER_ACTVACQINT 1
83#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070084
Iiro Valkonen7686b102011-02-02 23:21:58 -080085/* MXT_GEN_ACQUIRE field */
86#define MXT_ACQUIRE_CHRGTIME 0
87#define MXT_ACQUIRE_TCHDRIFT 2
88#define MXT_ACQUIRE_DRIFTST 3
89#define MXT_ACQUIRE_TCHAUTOCAL 4
90#define MXT_ACQUIRE_SYNC 5
91#define MXT_ACQUIRE_ATCHCALST 6
92#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070093
Iiro Valkonen7686b102011-02-02 23:21:58 -080094/* MXT_TOUCH_MULTI field */
95#define MXT_TOUCH_CTRL 0
96#define MXT_TOUCH_XORIGIN 1
97#define MXT_TOUCH_YORIGIN 2
98#define MXT_TOUCH_XSIZE 3
99#define MXT_TOUCH_YSIZE 4
100#define MXT_TOUCH_BLEN 6
101#define MXT_TOUCH_TCHTHR 7
102#define MXT_TOUCH_TCHDI 8
103#define MXT_TOUCH_ORIENT 9
104#define MXT_TOUCH_MOVHYSTI 11
105#define MXT_TOUCH_MOVHYSTN 12
106#define MXT_TOUCH_NUMTOUCH 14
107#define MXT_TOUCH_MRGHYST 15
108#define MXT_TOUCH_MRGTHR 16
109#define MXT_TOUCH_AMPHYST 17
110#define MXT_TOUCH_XRANGE_LSB 18
111#define MXT_TOUCH_XRANGE_MSB 19
112#define MXT_TOUCH_YRANGE_LSB 20
113#define MXT_TOUCH_YRANGE_MSB 21
114#define MXT_TOUCH_XLOCLIP 22
115#define MXT_TOUCH_XHICLIP 23
116#define MXT_TOUCH_YLOCLIP 24
117#define MXT_TOUCH_YHICLIP 25
118#define MXT_TOUCH_XEDGECTRL 26
119#define MXT_TOUCH_XEDGEDIST 27
120#define MXT_TOUCH_YEDGECTRL 28
121#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700122#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700123
Iiro Valkonen7686b102011-02-02 23:21:58 -0800124/* MXT_PROCI_GRIPFACE field */
125#define MXT_GRIPFACE_CTRL 0
126#define MXT_GRIPFACE_XLOGRIP 1
127#define MXT_GRIPFACE_XHIGRIP 2
128#define MXT_GRIPFACE_YLOGRIP 3
129#define MXT_GRIPFACE_YHIGRIP 4
130#define MXT_GRIPFACE_MAXTCHS 5
131#define MXT_GRIPFACE_SZTHR1 7
132#define MXT_GRIPFACE_SZTHR2 8
133#define MXT_GRIPFACE_SHPTHR1 9
134#define MXT_GRIPFACE_SHPTHR2 10
135#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700136
Iiro Valkonen7686b102011-02-02 23:21:58 -0800137/* MXT_PROCI_NOISE field */
138#define MXT_NOISE_CTRL 0
139#define MXT_NOISE_OUTFLEN 1
140#define MXT_NOISE_GCAFUL_LSB 3
141#define MXT_NOISE_GCAFUL_MSB 4
142#define MXT_NOISE_GCAFLL_LSB 5
143#define MXT_NOISE_GCAFLL_MSB 6
144#define MXT_NOISE_ACTVGCAFVALID 7
145#define MXT_NOISE_NOISETHR 8
146#define MXT_NOISE_FREQHOPSCALE 10
147#define MXT_NOISE_FREQ0 11
148#define MXT_NOISE_FREQ1 12
149#define MXT_NOISE_FREQ2 13
150#define MXT_NOISE_FREQ3 14
151#define MXT_NOISE_FREQ4 15
152#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700153
Iiro Valkonen7686b102011-02-02 23:21:58 -0800154/* MXT_SPT_COMMSCONFIG */
155#define MXT_COMMS_CTRL 0
156#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700157
Iiro Valkonen7686b102011-02-02 23:21:58 -0800158/* MXT_SPT_CTECONFIG field */
159#define MXT_CTE_CTRL 0
160#define MXT_CTE_CMD 1
161#define MXT_CTE_MODE 2
162#define MXT_CTE_IDLEGCAFDEPTH 3
163#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700164#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700165
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166#define MXT_VOLTAGE_DEFAULT 2700000
167#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700168
Iiro Valkonen7686b102011-02-02 23:21:58 -0800169/* Define for MXT_GEN_COMMAND */
170#define MXT_BOOT_VALUE 0xa5
171#define MXT_BACKUP_VALUE 0x55
172#define MXT_BACKUP_TIME 25 /* msec */
173#define MXT_RESET_TIME 65 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700174
Iiro Valkonen7686b102011-02-02 23:21:58 -0800175#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700176
177/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_UNLOCK_CMD_MSB 0xaa
179#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700180
181/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800182#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
183#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
184#define MXT_FRAME_CRC_CHECK 0x02
185#define MXT_FRAME_CRC_FAIL 0x03
186#define MXT_FRAME_CRC_PASS 0x04
187#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
188#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700189
190/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800191#define MXT_SUPPRESS (1 << 1)
192#define MXT_AMP (1 << 2)
193#define MXT_VECTOR (1 << 3)
194#define MXT_MOVE (1 << 4)
195#define MXT_RELEASE (1 << 5)
196#define MXT_PRESS (1 << 6)
197#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700198
Joonyoung Shim910d8052011-04-12 23:14:38 -0700199/* Touch orient bits */
200#define MXT_XY_SWITCH (1 << 0)
201#define MXT_X_INVERT (1 << 1)
202#define MXT_Y_INVERT (1 << 2)
203
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700204/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800205#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700206
Iiro Valkonen7686b102011-02-02 23:21:58 -0800207#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700208
Iiro Valkonen7686b102011-02-02 23:21:58 -0800209struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700210 u8 family_id;
211 u8 variant_id;
212 u8 version;
213 u8 build;
214 u8 matrix_xsize;
215 u8 matrix_ysize;
216 u8 object_num;
217};
218
Iiro Valkonen7686b102011-02-02 23:21:58 -0800219struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700220 u8 type;
221 u16 start_address;
222 u8 size;
223 u8 instances;
224 u8 num_report_ids;
225
226 /* to map object and message */
227 u8 max_reportid;
228};
229
Iiro Valkonen7686b102011-02-02 23:21:58 -0800230struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231 u8 reportid;
232 u8 message[7];
233 u8 checksum;
234};
235
Iiro Valkonen7686b102011-02-02 23:21:58 -0800236struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700237 int status;
238 int x;
239 int y;
240 int area;
241};
242
243/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800244struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700245 struct i2c_client *client;
246 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800247 const struct mxt_platform_data *pdata;
248 struct mxt_object *object_table;
249 struct mxt_info info;
250 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700251 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700252 unsigned int max_x;
253 unsigned int max_y;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700254};
255
Iiro Valkonen7686b102011-02-02 23:21:58 -0800256static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700257{
258 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800259 case MXT_GEN_MESSAGE:
260 case MXT_GEN_COMMAND:
261 case MXT_GEN_POWER:
262 case MXT_GEN_ACQUIRE:
263 case MXT_TOUCH_MULTI:
264 case MXT_TOUCH_KEYARRAY:
265 case MXT_TOUCH_PROXIMITY:
266 case MXT_PROCI_GRIPFACE:
267 case MXT_PROCG_NOISE:
268 case MXT_PROCI_ONETOUCH:
269 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700270 case MXT_PROCI_GRIP:
271 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800272 case MXT_SPT_COMMSCONFIG:
273 case MXT_SPT_GPIOPWM:
274 case MXT_SPT_SELFTEST:
275 case MXT_SPT_CTECONFIG:
276 case MXT_SPT_USERDATA:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700277 return true;
278 default:
279 return false;
280 }
281}
282
Iiro Valkonen7686b102011-02-02 23:21:58 -0800283static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700284{
285 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800286 case MXT_GEN_COMMAND:
287 case MXT_GEN_POWER:
288 case MXT_GEN_ACQUIRE:
289 case MXT_TOUCH_MULTI:
290 case MXT_TOUCH_KEYARRAY:
291 case MXT_TOUCH_PROXIMITY:
292 case MXT_PROCI_GRIPFACE:
293 case MXT_PROCG_NOISE:
294 case MXT_PROCI_ONETOUCH:
295 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700296 case MXT_PROCI_GRIP:
297 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800298 case MXT_SPT_GPIOPWM:
299 case MXT_SPT_SELFTEST:
300 case MXT_SPT_CTECONFIG:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700301 return true;
302 default:
303 return false;
304 }
305}
306
Iiro Valkonen7686b102011-02-02 23:21:58 -0800307static void mxt_dump_message(struct device *dev,
308 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700309{
310 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
311 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
312 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
313 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
314 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
315 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
316 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
317 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
318 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
319}
320
Iiro Valkonen7686b102011-02-02 23:21:58 -0800321static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700322 unsigned int state)
323{
324 u8 val;
325
326recheck:
327 if (i2c_master_recv(client, &val, 1) != 1) {
328 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
329 return -EIO;
330 }
331
332 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800333 case MXT_WAITING_BOOTLOAD_CMD:
334 case MXT_WAITING_FRAME_DATA:
335 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700336 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800337 case MXT_FRAME_CRC_PASS:
338 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700339 goto recheck;
340 break;
341 default:
342 return -EINVAL;
343 }
344
345 if (val != state) {
346 dev_err(&client->dev, "Unvalid bootloader mode state\n");
347 return -EINVAL;
348 }
349
350 return 0;
351}
352
Iiro Valkonen7686b102011-02-02 23:21:58 -0800353static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700354{
355 u8 buf[2];
356
Iiro Valkonen7686b102011-02-02 23:21:58 -0800357 buf[0] = MXT_UNLOCK_CMD_LSB;
358 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700359
360 if (i2c_master_send(client, buf, 2) != 2) {
361 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
362 return -EIO;
363 }
364
365 return 0;
366}
367
Iiro Valkonen7686b102011-02-02 23:21:58 -0800368static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700369 const u8 *data, unsigned int frame_size)
370{
371 if (i2c_master_send(client, data, frame_size) != frame_size) {
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_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700380 u16 reg, u16 len, void *val)
381{
382 struct i2c_msg xfer[2];
383 u8 buf[2];
384
385 buf[0] = reg & 0xff;
386 buf[1] = (reg >> 8) & 0xff;
387
388 /* Write register */
389 xfer[0].addr = client->addr;
390 xfer[0].flags = 0;
391 xfer[0].len = 2;
392 xfer[0].buf = buf;
393
394 /* Read data */
395 xfer[1].addr = client->addr;
396 xfer[1].flags = I2C_M_RD;
397 xfer[1].len = len;
398 xfer[1].buf = val;
399
400 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
401 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
402 return -EIO;
403 }
404
405 return 0;
406}
407
Iiro Valkonen7686b102011-02-02 23:21:58 -0800408static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700409{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800410 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700411}
412
Iiro Valkonen7686b102011-02-02 23:21:58 -0800413static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700414{
415 u8 buf[3];
416
417 buf[0] = reg & 0xff;
418 buf[1] = (reg >> 8) & 0xff;
419 buf[2] = val;
420
421 if (i2c_master_send(client, buf, 3) != 3) {
422 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
423 return -EIO;
424 }
425
426 return 0;
427}
428
Iiro Valkonen7686b102011-02-02 23:21:58 -0800429static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430 u16 reg, u8 *object_buf)
431{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800432 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700433 object_buf);
434}
435
Iiro Valkonen7686b102011-02-02 23:21:58 -0800436static struct mxt_object *
437mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700438{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800439 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700440 int i;
441
442 for (i = 0; i < data->info.object_num; i++) {
443 object = data->object_table + i;
444 if (object->type == type)
445 return object;
446 }
447
448 dev_err(&data->client->dev, "Invalid object type\n");
449 return NULL;
450}
451
Iiro Valkonen7686b102011-02-02 23:21:58 -0800452static int mxt_read_message(struct mxt_data *data,
453 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700454{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800455 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700456 u16 reg;
457
Iiro Valkonen7686b102011-02-02 23:21:58 -0800458 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700459 if (!object)
460 return -EINVAL;
461
462 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800463 return __mxt_read_reg(data->client, reg,
464 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700465}
466
Iiro Valkonen7686b102011-02-02 23:21:58 -0800467static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700468 u8 type, u8 offset, u8 *val)
469{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800470 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700471 u16 reg;
472
Iiro Valkonen7686b102011-02-02 23:21:58 -0800473 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700474 if (!object)
475 return -EINVAL;
476
477 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800478 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700479}
480
Iiro Valkonen7686b102011-02-02 23:21:58 -0800481static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482 u8 type, u8 offset, u8 val)
483{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800484 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485 u16 reg;
486
Iiro Valkonen7686b102011-02-02 23:21:58 -0800487 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488 if (!object)
489 return -EINVAL;
490
491 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800492 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700493}
494
Iiro Valkonen7686b102011-02-02 23:21:58 -0800495static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700496{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800497 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498 struct input_dev *input_dev = data->input_dev;
499 int status = finger[single_id].status;
500 int finger_num = 0;
501 int id;
502
Iiro Valkonen7686b102011-02-02 23:21:58 -0800503 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504 if (!finger[id].status)
505 continue;
506
507 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508 finger[id].status != MXT_RELEASE ?
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700509 finger[id].area : 0);
510 input_report_abs(input_dev, ABS_MT_POSITION_X,
511 finger[id].x);
512 input_report_abs(input_dev, ABS_MT_POSITION_Y,
513 finger[id].y);
514 input_mt_sync(input_dev);
515
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517 finger[id].status = 0;
518 else
519 finger_num++;
520 }
521
522 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
523
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525 input_report_abs(input_dev, ABS_X, finger[single_id].x);
526 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
527 }
528
529 input_sync(input_dev);
530}
531
Iiro Valkonen7686b102011-02-02 23:21:58 -0800532static void mxt_input_touchevent(struct mxt_data *data,
533 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700534{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800535 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536 struct device *dev = &data->client->dev;
537 u8 status = message->message[0];
538 int x;
539 int y;
540 int area;
541
542 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800543 if (!(status & MXT_DETECT)) {
544 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545 dev_dbg(dev, "[%d] released\n", id);
546
Iiro Valkonen7686b102011-02-02 23:21:58 -0800547 finger[id].status = MXT_RELEASE;
548 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700549 }
550 return;
551 }
552
553 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800554 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700555 return;
556
Joonyoung Shim910d8052011-04-12 23:14:38 -0700557 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
558 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
559 if (data->max_x < 1024)
560 x = x >> 2;
561 if (data->max_y < 1024)
562 y = y >> 2;
563
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564 area = message->message[4];
565
566 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800567 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700568 x, y, area);
569
Iiro Valkonen7686b102011-02-02 23:21:58 -0800570 finger[id].status = status & MXT_MOVE ?
571 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572 finger[id].x = x;
573 finger[id].y = y;
574 finger[id].area = area;
575
Iiro Valkonen7686b102011-02-02 23:21:58 -0800576 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700577}
578
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800581 struct mxt_data *data = dev_id;
582 struct mxt_message message;
583 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700584 struct device *dev = &data->client->dev;
585 int id;
586 u8 reportid;
587 u8 max_reportid;
588 u8 min_reportid;
589
590 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800591 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592 dev_err(dev, "Failed to read message\n");
593 goto end;
594 }
595
596 reportid = message.reportid;
597
Iiro Valkonen7686b102011-02-02 23:21:58 -0800598 /* whether reportid is thing of MXT_TOUCH_MULTI */
599 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600 if (!object)
601 goto end;
602
603 max_reportid = object->max_reportid;
604 min_reportid = max_reportid - object->num_report_ids + 1;
605 id = reportid - min_reportid;
606
607 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800608 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800610 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611 } while (reportid != 0xff);
612
613end:
614 return IRQ_HANDLED;
615}
616
Iiro Valkonen7686b102011-02-02 23:21:58 -0800617static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700618{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800619 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800620 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700621 struct device *dev = &data->client->dev;
622 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800623 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624
Iiro Valkonen71749f52011-02-15 13:36:52 -0800625 if (!pdata->config) {
626 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
627 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700628 }
629
630 for (i = 0; i < data->info.object_num; i++) {
631 object = data->object_table + i;
632
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634 continue;
635
Iiro Valkonen71749f52011-02-15 13:36:52 -0800636 for (j = 0; j < object->size + 1; j++) {
637 config_offset = index + j;
638 if (config_offset > pdata->config_length) {
639 dev_err(dev, "Not enough config data!\n");
640 return -EINVAL;
641 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800642 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800643 pdata->config[config_offset]);
644 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700645 index += object->size + 1;
646 }
647
648 return 0;
649}
650
Iiro Valkonen7686b102011-02-02 23:21:58 -0800651static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700652{
653 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800654 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700655 int count = 10;
656 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700657
658 /* Read dummy message to make high CHG pin */
659 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800660 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700661 if (error)
662 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800663 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664
665 if (!count) {
666 dev_err(dev, "CHG pin isn't cleared\n");
667 return -EBUSY;
668 }
669
670 return 0;
671}
672
Iiro Valkonen7686b102011-02-02 23:21:58 -0800673static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800675 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700676 u8 voltage;
677
678 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800679 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800681 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700682 pdata->y_line);
683
684 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800685 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686 pdata->orient);
687
688 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800689 mxt_write_object(data, MXT_TOUCH_MULTI,
690 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691
692 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800693 mxt_write_object(data, MXT_TOUCH_MULTI,
694 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700695
696 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800697 mxt_write_object(data, MXT_TOUCH_MULTI,
698 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
699 mxt_write_object(data, MXT_TOUCH_MULTI,
700 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
701 mxt_write_object(data, MXT_TOUCH_MULTI,
702 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
703 mxt_write_object(data, MXT_TOUCH_MULTI,
704 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705
706 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700707 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800708 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
709 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
710 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 voltage = 0xff - voltage + 1;
712 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800713 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
714 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716 mxt_write_object(data, MXT_SPT_CTECONFIG,
717 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718 }
719}
720
Iiro Valkonen7686b102011-02-02 23:21:58 -0800721static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700722{
723 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725 int error;
726 u8 val;
727
Iiro Valkonen7686b102011-02-02 23:21:58 -0800728 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700729 if (error)
730 return error;
731 info->family_id = val;
732
Iiro Valkonen7686b102011-02-02 23:21:58 -0800733 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 if (error)
735 return error;
736 info->variant_id = val;
737
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739 if (error)
740 return error;
741 info->version = val;
742
Iiro Valkonen7686b102011-02-02 23:21:58 -0800743 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700744 if (error)
745 return error;
746 info->build = val;
747
Iiro Valkonen7686b102011-02-02 23:21:58 -0800748 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749 if (error)
750 return error;
751 info->object_num = val;
752
753 return 0;
754}
755
Iiro Valkonen7686b102011-02-02 23:21:58 -0800756static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700757{
758 int error;
759 int i;
760 u16 reg;
761 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800762 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700763
764 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800765 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766
Iiro Valkonen7686b102011-02-02 23:21:58 -0800767 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
768 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700769 if (error)
770 return error;
771
772 object->type = buf[0];
773 object->start_address = (buf[2] << 8) | buf[1];
774 object->size = buf[3];
775 object->instances = buf[4];
776 object->num_report_ids = buf[5];
777
778 if (object->num_report_ids) {
779 reportid += object->num_report_ids *
780 (object->instances + 1);
781 object->max_reportid = reportid;
782 }
783 }
784
785 return 0;
786}
787
Iiro Valkonen7686b102011-02-02 23:21:58 -0800788static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789{
790 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800791 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700792 int error;
793 u8 val;
794
Iiro Valkonen7686b102011-02-02 23:21:58 -0800795 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700796 if (error)
797 return error;
798
799 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800800 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801 GFP_KERNEL);
802 if (!data->object_table) {
803 dev_err(&client->dev, "Failed to allocate memory\n");
804 return -ENOMEM;
805 }
806
807 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800808 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700809 if (error)
810 return error;
811
812 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814 if (error)
815 return error;
816
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818
819 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800820 mxt_write_object(data, MXT_GEN_COMMAND,
821 MXT_COMMAND_BACKUPNV,
822 MXT_BACKUP_VALUE);
823 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700824
825 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800826 mxt_write_object(data, MXT_GEN_COMMAND,
827 MXT_COMMAND_RESET, 1);
828 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700829
830 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800831 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700832 if (error)
833 return error;
834 info->matrix_xsize = val;
835
Iiro Valkonen7686b102011-02-02 23:21:58 -0800836 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700837 if (error)
838 return error;
839 info->matrix_ysize = val;
840
841 dev_info(&client->dev,
842 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
843 info->family_id, info->variant_id, info->version,
844 info->build);
845
846 dev_info(&client->dev,
847 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
848 info->matrix_xsize, info->matrix_ysize,
849 info->object_num);
850
851 return 0;
852}
853
Joonyoung Shim910d8052011-04-12 23:14:38 -0700854static void mxt_calc_resolution(struct mxt_data *data)
855{
856 unsigned int max_x = data->pdata->x_size - 1;
857 unsigned int max_y = data->pdata->y_size - 1;
858
859 if (data->pdata->orient & MXT_XY_SWITCH) {
860 data->max_x = max_y;
861 data->max_y = max_x;
862 } else {
863 data->max_x = max_x;
864 data->max_y = max_y;
865 }
866}
867
Iiro Valkonen7686b102011-02-02 23:21:58 -0800868static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700869 struct device_attribute *attr, char *buf)
870{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800871 struct mxt_data *data = dev_get_drvdata(dev);
872 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700873 int count = 0;
874 int i, j;
875 int error;
876 u8 val;
877
878 for (i = 0; i < data->info.object_num; i++) {
879 object = data->object_table + i;
880
881 count += sprintf(buf + count,
882 "Object Table Element %d(Type %d)\n",
883 i + 1, object->type);
884
Iiro Valkonen7686b102011-02-02 23:21:58 -0800885 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700886 count += sprintf(buf + count, "\n");
887 continue;
888 }
889
890 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800891 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700892 object->type, j, &val);
893 if (error)
894 return error;
895
896 count += sprintf(buf + count,
897 " Byte %d: 0x%x (%d)\n", j, val, val);
898 }
899
900 count += sprintf(buf + count, "\n");
901 }
902
903 return count;
904}
905
Iiro Valkonen7686b102011-02-02 23:21:58 -0800906static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700907{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800908 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909 struct i2c_client *client = data->client;
910 const struct firmware *fw = NULL;
911 unsigned int frame_size;
912 unsigned int pos = 0;
913 int ret;
914
915 ret = request_firmware(&fw, fn, dev);
916 if (ret) {
917 dev_err(dev, "Unable to open firmware %s\n", fn);
918 return ret;
919 }
920
921 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800922 mxt_write_object(data, MXT_GEN_COMMAND,
923 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
924 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700925
926 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800927 if (client->addr == MXT_APP_LOW)
928 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800930 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931
Iiro Valkonen7686b102011-02-02 23:21:58 -0800932 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700933 if (ret)
934 goto out;
935
936 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800937 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700938
939 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800940 ret = mxt_check_bootloader(client,
941 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700942 if (ret)
943 goto out;
944
945 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
946
947 /* We should add 2 at frame size as the the firmware data is not
948 * included the CRC bytes.
949 */
950 frame_size += 2;
951
952 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 ret = mxt_check_bootloader(client,
956 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700957 if (ret)
958 goto out;
959
960 pos += frame_size;
961
962 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
963 }
964
965out:
966 release_firmware(fw);
967
968 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800969 if (client->addr == MXT_BOOT_LOW)
970 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700971 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800972 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700973
974 return ret;
975}
976
Iiro Valkonen7686b102011-02-02 23:21:58 -0800977static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700978 struct device_attribute *attr,
979 const char *buf, size_t count)
980{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800981 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700982 int error;
983
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700984 disable_irq(data->irq);
985
Iiro Valkonen7686b102011-02-02 23:21:58 -0800986 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700987 if (error) {
988 dev_err(dev, "The firmware update failed(%d)\n", error);
989 count = error;
990 } else {
991 dev_dbg(dev, "The firmware update succeeded\n");
992
993 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800994 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700995
996 kfree(data->object_table);
997 data->object_table = NULL;
998
Iiro Valkonen7686b102011-02-02 23:21:58 -0800999 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000 }
1001
1002 enable_irq(data->irq);
1003
Iiro Valkonen08960a02011-04-12 23:16:40 -07001004 error = mxt_make_highchg(data);
1005 if (error)
1006 return error;
1007
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001008 return count;
1009}
1010
Iiro Valkonen7686b102011-02-02 23:21:58 -08001011static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1012static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001013
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001015 &dev_attr_object.attr,
1016 &dev_attr_update_fw.attr,
1017 NULL
1018};
1019
Iiro Valkonen7686b102011-02-02 23:21:58 -08001020static const struct attribute_group mxt_attr_group = {
1021 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022};
1023
Iiro Valkonen7686b102011-02-02 23:21:58 -08001024static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025{
1026 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027 mxt_write_object(data,
1028 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029}
1030
Iiro Valkonen7686b102011-02-02 23:21:58 -08001031static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001032{
1033 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001034 mxt_write_object(data,
1035 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001036}
1037
Iiro Valkonen7686b102011-02-02 23:21:58 -08001038static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001039{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001040 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001041
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043
1044 return 0;
1045}
1046
Iiro Valkonen7686b102011-02-02 23:21:58 -08001047static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001048{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050
Iiro Valkonen7686b102011-02-02 23:21:58 -08001051 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001052}
1053
Iiro Valkonen7686b102011-02-02 23:21:58 -08001054static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001055 const struct i2c_device_id *id)
1056{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001057 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001058 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001059 struct input_dev *input_dev;
1060 int error;
1061
Iiro Valkonen919ed892011-02-15 13:36:52 -08001062 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063 return -EINVAL;
1064
Iiro Valkonen7686b102011-02-02 23:21:58 -08001065 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001066 input_dev = input_allocate_device();
1067 if (!data || !input_dev) {
1068 dev_err(&client->dev, "Failed to allocate memory\n");
1069 error = -ENOMEM;
1070 goto err_free_mem;
1071 }
1072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074 input_dev->id.bustype = BUS_I2C;
1075 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001076 input_dev->open = mxt_input_open;
1077 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078
Joonyoung Shim910d8052011-04-12 23:14:38 -07001079 data->client = client;
1080 data->input_dev = input_dev;
1081 data->pdata = pdata;
1082 data->irq = client->irq;
1083
1084 mxt_calc_resolution(data);
1085
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001086 __set_bit(EV_ABS, input_dev->evbit);
1087 __set_bit(EV_KEY, input_dev->evbit);
1088 __set_bit(BTN_TOUCH, input_dev->keybit);
1089
1090 /* For single touch */
1091 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001092 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001094 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095
1096 /* For multi touch */
1097 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001098 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001099 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001100 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001101 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001102 0, data->max_y, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001103
1104 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001105 i2c_set_clientdata(client, data);
1106
Iiro Valkonen7686b102011-02-02 23:21:58 -08001107 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001108 if (error)
1109 goto err_free_object;
1110
Iiro Valkonen7686b102011-02-02 23:21:58 -08001111 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001112 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001113 if (error) {
1114 dev_err(&client->dev, "Failed to register interrupt\n");
1115 goto err_free_object;
1116 }
1117
Iiro Valkonen08960a02011-04-12 23:16:40 -07001118 error = mxt_make_highchg(data);
1119 if (error)
1120 goto err_free_irq;
1121
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001122 error = input_register_device(input_dev);
1123 if (error)
1124 goto err_free_irq;
1125
Iiro Valkonen7686b102011-02-02 23:21:58 -08001126 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001127 if (error)
1128 goto err_unregister_device;
1129
1130 return 0;
1131
1132err_unregister_device:
1133 input_unregister_device(input_dev);
1134 input_dev = NULL;
1135err_free_irq:
1136 free_irq(client->irq, data);
1137err_free_object:
1138 kfree(data->object_table);
1139err_free_mem:
1140 input_free_device(input_dev);
1141 kfree(data);
1142 return error;
1143}
1144
Iiro Valkonen7686b102011-02-02 23:21:58 -08001145static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001147 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001148
Iiro Valkonen7686b102011-02-02 23:21:58 -08001149 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001150 free_irq(data->irq, data);
1151 input_unregister_device(data->input_dev);
1152 kfree(data->object_table);
1153 kfree(data);
1154
1155 return 0;
1156}
1157
1158#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001159static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001160{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001161 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001162 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001163 struct input_dev *input_dev = data->input_dev;
1164
1165 mutex_lock(&input_dev->mutex);
1166
1167 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001168 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001169
1170 mutex_unlock(&input_dev->mutex);
1171
1172 return 0;
1173}
1174
Iiro Valkonen7686b102011-02-02 23:21:58 -08001175static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001177 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001178 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001179 struct input_dev *input_dev = data->input_dev;
1180
1181 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001182 mxt_write_object(data, MXT_GEN_COMMAND,
1183 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001184
Iiro Valkonen7686b102011-02-02 23:21:58 -08001185 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001186
1187 mutex_lock(&input_dev->mutex);
1188
1189 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001190 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001191
1192 mutex_unlock(&input_dev->mutex);
1193
1194 return 0;
1195}
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001196
Iiro Valkonen7686b102011-02-02 23:21:58 -08001197static const struct dev_pm_ops mxt_pm_ops = {
1198 .suspend = mxt_suspend,
1199 .resume = mxt_resume,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001200};
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201#endif
1202
Iiro Valkonen7686b102011-02-02 23:21:58 -08001203static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001204 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001205 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001206 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001207 { }
1208};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001209MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001210
Iiro Valkonen7686b102011-02-02 23:21:58 -08001211static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001212 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001213 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001214 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001215#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001216 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001217#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001218 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001219 .probe = mxt_probe,
1220 .remove = __devexit_p(mxt_remove),
1221 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001222};
1223
Iiro Valkonen7686b102011-02-02 23:21:58 -08001224static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001225{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001226 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001227}
1228
Iiro Valkonen7686b102011-02-02 23:21:58 -08001229static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001230{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001231 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001232}
1233
Iiro Valkonen7686b102011-02-02 23:21:58 -08001234module_init(mxt_init);
1235module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001236
1237/* Module information */
1238MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001239MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001240MODULE_LICENSE("GPL");