blob: 4012436633b18ef0d087b95ca4f6792c6c8e290b [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
199/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_MAX_XC 0x3ff
201#define MXT_MAX_YC 0x3ff
202#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700203
Iiro Valkonen7686b102011-02-02 23:21:58 -0800204#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700205
Iiro Valkonen7686b102011-02-02 23:21:58 -0800206struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700207 u8 family_id;
208 u8 variant_id;
209 u8 version;
210 u8 build;
211 u8 matrix_xsize;
212 u8 matrix_ysize;
213 u8 object_num;
214};
215
Iiro Valkonen7686b102011-02-02 23:21:58 -0800216struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217 u8 type;
218 u16 start_address;
219 u8 size;
220 u8 instances;
221 u8 num_report_ids;
222
223 /* to map object and message */
224 u8 max_reportid;
225};
226
Iiro Valkonen7686b102011-02-02 23:21:58 -0800227struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700228 u8 reportid;
229 u8 message[7];
230 u8 checksum;
231};
232
Iiro Valkonen7686b102011-02-02 23:21:58 -0800233struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700234 int status;
235 int x;
236 int y;
237 int area;
238};
239
240/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800241struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700242 struct i2c_client *client;
243 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800244 const struct mxt_platform_data *pdata;
245 struct mxt_object *object_table;
246 struct mxt_info info;
247 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700248 unsigned int irq;
249};
250
Iiro Valkonen7686b102011-02-02 23:21:58 -0800251static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700252{
253 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800254 case MXT_GEN_MESSAGE:
255 case MXT_GEN_COMMAND:
256 case MXT_GEN_POWER:
257 case MXT_GEN_ACQUIRE:
258 case MXT_TOUCH_MULTI:
259 case MXT_TOUCH_KEYARRAY:
260 case MXT_TOUCH_PROXIMITY:
261 case MXT_PROCI_GRIPFACE:
262 case MXT_PROCG_NOISE:
263 case MXT_PROCI_ONETOUCH:
264 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700265 case MXT_PROCI_GRIP:
266 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800267 case MXT_SPT_COMMSCONFIG:
268 case MXT_SPT_GPIOPWM:
269 case MXT_SPT_SELFTEST:
270 case MXT_SPT_CTECONFIG:
271 case MXT_SPT_USERDATA:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700272 return true;
273 default:
274 return false;
275 }
276}
277
Iiro Valkonen7686b102011-02-02 23:21:58 -0800278static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700279{
280 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800281 case MXT_GEN_COMMAND:
282 case MXT_GEN_POWER:
283 case MXT_GEN_ACQUIRE:
284 case MXT_TOUCH_MULTI:
285 case MXT_TOUCH_KEYARRAY:
286 case MXT_TOUCH_PROXIMITY:
287 case MXT_PROCI_GRIPFACE:
288 case MXT_PROCG_NOISE:
289 case MXT_PROCI_ONETOUCH:
290 case MXT_PROCI_TWOTOUCH:
Joonyoung Shim4c75de32011-03-14 21:41:40 -0700291 case MXT_PROCI_GRIP:
292 case MXT_PROCI_PALM:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800293 case MXT_SPT_GPIOPWM:
294 case MXT_SPT_SELFTEST:
295 case MXT_SPT_CTECONFIG:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700296 return true;
297 default:
298 return false;
299 }
300}
301
Iiro Valkonen7686b102011-02-02 23:21:58 -0800302static void mxt_dump_message(struct device *dev,
303 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700304{
305 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
306 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
307 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
308 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
309 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
310 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
311 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
312 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
313 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
314}
315
Iiro Valkonen7686b102011-02-02 23:21:58 -0800316static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700317 unsigned int state)
318{
319 u8 val;
320
321recheck:
322 if (i2c_master_recv(client, &val, 1) != 1) {
323 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
324 return -EIO;
325 }
326
327 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800328 case MXT_WAITING_BOOTLOAD_CMD:
329 case MXT_WAITING_FRAME_DATA:
330 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700331 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800332 case MXT_FRAME_CRC_PASS:
333 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700334 goto recheck;
335 break;
336 default:
337 return -EINVAL;
338 }
339
340 if (val != state) {
341 dev_err(&client->dev, "Unvalid bootloader mode state\n");
342 return -EINVAL;
343 }
344
345 return 0;
346}
347
Iiro Valkonen7686b102011-02-02 23:21:58 -0800348static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700349{
350 u8 buf[2];
351
Iiro Valkonen7686b102011-02-02 23:21:58 -0800352 buf[0] = MXT_UNLOCK_CMD_LSB;
353 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700354
355 if (i2c_master_send(client, buf, 2) != 2) {
356 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
357 return -EIO;
358 }
359
360 return 0;
361}
362
Iiro Valkonen7686b102011-02-02 23:21:58 -0800363static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700364 const u8 *data, unsigned int frame_size)
365{
366 if (i2c_master_send(client, data, frame_size) != frame_size) {
367 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
368 return -EIO;
369 }
370
371 return 0;
372}
373
Iiro Valkonen7686b102011-02-02 23:21:58 -0800374static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700375 u16 reg, u16 len, void *val)
376{
377 struct i2c_msg xfer[2];
378 u8 buf[2];
379
380 buf[0] = reg & 0xff;
381 buf[1] = (reg >> 8) & 0xff;
382
383 /* Write register */
384 xfer[0].addr = client->addr;
385 xfer[0].flags = 0;
386 xfer[0].len = 2;
387 xfer[0].buf = buf;
388
389 /* Read data */
390 xfer[1].addr = client->addr;
391 xfer[1].flags = I2C_M_RD;
392 xfer[1].len = len;
393 xfer[1].buf = val;
394
395 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
396 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
397 return -EIO;
398 }
399
400 return 0;
401}
402
Iiro Valkonen7686b102011-02-02 23:21:58 -0800403static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700404{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800405 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700406}
407
Iiro Valkonen7686b102011-02-02 23:21:58 -0800408static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700409{
410 u8 buf[3];
411
412 buf[0] = reg & 0xff;
413 buf[1] = (reg >> 8) & 0xff;
414 buf[2] = val;
415
416 if (i2c_master_send(client, buf, 3) != 3) {
417 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
418 return -EIO;
419 }
420
421 return 0;
422}
423
Iiro Valkonen7686b102011-02-02 23:21:58 -0800424static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700425 u16 reg, u8 *object_buf)
426{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800427 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700428 object_buf);
429}
430
Iiro Valkonen7686b102011-02-02 23:21:58 -0800431static struct mxt_object *
432mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700433{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800434 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700435 int i;
436
437 for (i = 0; i < data->info.object_num; i++) {
438 object = data->object_table + i;
439 if (object->type == type)
440 return object;
441 }
442
443 dev_err(&data->client->dev, "Invalid object type\n");
444 return NULL;
445}
446
Iiro Valkonen7686b102011-02-02 23:21:58 -0800447static int mxt_read_message(struct mxt_data *data,
448 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700449{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800450 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700451 u16 reg;
452
Iiro Valkonen7686b102011-02-02 23:21:58 -0800453 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700454 if (!object)
455 return -EINVAL;
456
457 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800458 return __mxt_read_reg(data->client, reg,
459 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700460}
461
Iiro Valkonen7686b102011-02-02 23:21:58 -0800462static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700463 u8 type, u8 offset, u8 *val)
464{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800465 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700466 u16 reg;
467
Iiro Valkonen7686b102011-02-02 23:21:58 -0800468 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700469 if (!object)
470 return -EINVAL;
471
472 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800473 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700474}
475
Iiro Valkonen7686b102011-02-02 23:21:58 -0800476static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700477 u8 type, u8 offset, u8 val)
478{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800479 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700480 u16 reg;
481
Iiro Valkonen7686b102011-02-02 23:21:58 -0800482 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483 if (!object)
484 return -EINVAL;
485
486 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800487 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488}
489
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800492 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700493 struct input_dev *input_dev = data->input_dev;
494 int status = finger[single_id].status;
495 int finger_num = 0;
496 int id;
497
Iiro Valkonen7686b102011-02-02 23:21:58 -0800498 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700499 if (!finger[id].status)
500 continue;
501
502 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800503 finger[id].status != MXT_RELEASE ?
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504 finger[id].area : 0);
505 input_report_abs(input_dev, ABS_MT_POSITION_X,
506 finger[id].x);
507 input_report_abs(input_dev, ABS_MT_POSITION_Y,
508 finger[id].y);
509 input_mt_sync(input_dev);
510
Iiro Valkonen7686b102011-02-02 23:21:58 -0800511 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700512 finger[id].status = 0;
513 else
514 finger_num++;
515 }
516
517 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
518
Iiro Valkonen7686b102011-02-02 23:21:58 -0800519 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700520 input_report_abs(input_dev, ABS_X, finger[single_id].x);
521 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
522 }
523
524 input_sync(input_dev);
525}
526
Iiro Valkonen7686b102011-02-02 23:21:58 -0800527static void mxt_input_touchevent(struct mxt_data *data,
528 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700529{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800530 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700531 struct device *dev = &data->client->dev;
532 u8 status = message->message[0];
533 int x;
534 int y;
535 int area;
536
537 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800538 if (!(status & MXT_DETECT)) {
539 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700540 dev_dbg(dev, "[%d] released\n", id);
541
Iiro Valkonen7686b102011-02-02 23:21:58 -0800542 finger[id].status = MXT_RELEASE;
543 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544 }
545 return;
546 }
547
548 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800549 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550 return;
551
552 x = (message->message[1] << 2) | ((message->message[3] & ~0x3f) >> 6);
553 y = (message->message[2] << 2) | ((message->message[3] & ~0xf3) >> 2);
554 area = message->message[4];
555
556 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800557 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700558 x, y, area);
559
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 finger[id].status = status & MXT_MOVE ?
561 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700562 finger[id].x = x;
563 finger[id].y = y;
564 finger[id].area = area;
565
Iiro Valkonen7686b102011-02-02 23:21:58 -0800566 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700567}
568
Iiro Valkonen7686b102011-02-02 23:21:58 -0800569static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700570{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800571 struct mxt_data *data = dev_id;
572 struct mxt_message message;
573 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700574 struct device *dev = &data->client->dev;
575 int id;
576 u8 reportid;
577 u8 max_reportid;
578 u8 min_reportid;
579
580 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800581 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 dev_err(dev, "Failed to read message\n");
583 goto end;
584 }
585
586 reportid = message.reportid;
587
Iiro Valkonen7686b102011-02-02 23:21:58 -0800588 /* whether reportid is thing of MXT_TOUCH_MULTI */
589 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700590 if (!object)
591 goto end;
592
593 max_reportid = object->max_reportid;
594 min_reportid = max_reportid - object->num_report_ids + 1;
595 id = reportid - min_reportid;
596
597 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800598 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800600 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601 } while (reportid != 0xff);
602
603end:
604 return IRQ_HANDLED;
605}
606
Iiro Valkonen7686b102011-02-02 23:21:58 -0800607static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700608{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800609 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800610 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611 struct device *dev = &data->client->dev;
612 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800613 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700614
Iiro Valkonen71749f52011-02-15 13:36:52 -0800615 if (!pdata->config) {
616 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
617 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700618 }
619
620 for (i = 0; i < data->info.object_num; i++) {
621 object = data->object_table + i;
622
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624 continue;
625
Iiro Valkonen71749f52011-02-15 13:36:52 -0800626 for (j = 0; j < object->size + 1; j++) {
627 config_offset = index + j;
628 if (config_offset > pdata->config_length) {
629 dev_err(dev, "Not enough config data!\n");
630 return -EINVAL;
631 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800632 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800633 pdata->config[config_offset]);
634 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635 index += object->size + 1;
636 }
637
638 return 0;
639}
640
Iiro Valkonen7686b102011-02-02 23:21:58 -0800641static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700642{
643 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800644 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700645 int count = 10;
646 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700647
648 /* Read dummy message to make high CHG pin */
649 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800650 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700651 if (error)
652 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800653 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700654
655 if (!count) {
656 dev_err(dev, "CHG pin isn't cleared\n");
657 return -EBUSY;
658 }
659
660 return 0;
661}
662
Iiro Valkonen7686b102011-02-02 23:21:58 -0800663static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 u8 voltage;
667
668 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800669 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700670 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800671 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700672 pdata->y_line);
673
674 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800675 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700676 pdata->orient);
677
678 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800679 mxt_write_object(data, MXT_TOUCH_MULTI,
680 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700681
682 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800683 mxt_write_object(data, MXT_TOUCH_MULTI,
684 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700685
686 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800687 mxt_write_object(data, MXT_TOUCH_MULTI,
688 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
689 mxt_write_object(data, MXT_TOUCH_MULTI,
690 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
691 mxt_write_object(data, MXT_TOUCH_MULTI,
692 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
693 mxt_write_object(data, MXT_TOUCH_MULTI,
694 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700695
696 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700697 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
699 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
700 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701 voltage = 0xff - voltage + 1;
702 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800703 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
704 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705
Iiro Valkonen7686b102011-02-02 23:21:58 -0800706 mxt_write_object(data, MXT_SPT_CTECONFIG,
707 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708 }
709}
710
Iiro Valkonen7686b102011-02-02 23:21:58 -0800711static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700712{
713 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800714 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715 int error;
716 u8 val;
717
Iiro Valkonen7686b102011-02-02 23:21:58 -0800718 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719 if (error)
720 return error;
721 info->family_id = val;
722
Iiro Valkonen7686b102011-02-02 23:21:58 -0800723 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700724 if (error)
725 return error;
726 info->variant_id = val;
727
Iiro Valkonen7686b102011-02-02 23:21:58 -0800728 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700729 if (error)
730 return error;
731 info->version = val;
732
Iiro Valkonen7686b102011-02-02 23:21:58 -0800733 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734 if (error)
735 return error;
736 info->build = val;
737
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739 if (error)
740 return error;
741 info->object_num = val;
742
743 return 0;
744}
745
Iiro Valkonen7686b102011-02-02 23:21:58 -0800746static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747{
748 int error;
749 int i;
750 u16 reg;
751 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800752 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700753
754 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800755 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756
Iiro Valkonen7686b102011-02-02 23:21:58 -0800757 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
758 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700759 if (error)
760 return error;
761
762 object->type = buf[0];
763 object->start_address = (buf[2] << 8) | buf[1];
764 object->size = buf[3];
765 object->instances = buf[4];
766 object->num_report_ids = buf[5];
767
768 if (object->num_report_ids) {
769 reportid += object->num_report_ids *
770 (object->instances + 1);
771 object->max_reportid = reportid;
772 }
773 }
774
775 return 0;
776}
777
Iiro Valkonen7686b102011-02-02 23:21:58 -0800778static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779{
780 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800781 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700782 int error;
783 u8 val;
784
Iiro Valkonen7686b102011-02-02 23:21:58 -0800785 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786 if (error)
787 return error;
788
789 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791 GFP_KERNEL);
792 if (!data->object_table) {
793 dev_err(&client->dev, "Failed to allocate memory\n");
794 return -ENOMEM;
795 }
796
797 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800798 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700799 if (error)
800 return error;
801
802 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800803 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700804 if (error)
805 return error;
806
Iiro Valkonen7686b102011-02-02 23:21:58 -0800807 error = mxt_make_highchg(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700808 if (error)
809 return error;
810
Iiro Valkonen7686b102011-02-02 23:21:58 -0800811 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700812
813 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800814 mxt_write_object(data, MXT_GEN_COMMAND,
815 MXT_COMMAND_BACKUPNV,
816 MXT_BACKUP_VALUE);
817 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818
819 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800820 mxt_write_object(data, MXT_GEN_COMMAND,
821 MXT_COMMAND_RESET, 1);
822 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700823
824 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800825 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700826 if (error)
827 return error;
828 info->matrix_xsize = val;
829
Iiro Valkonen7686b102011-02-02 23:21:58 -0800830 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831 if (error)
832 return error;
833 info->matrix_ysize = val;
834
835 dev_info(&client->dev,
836 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
837 info->family_id, info->variant_id, info->version,
838 info->build);
839
840 dev_info(&client->dev,
841 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
842 info->matrix_xsize, info->matrix_ysize,
843 info->object_num);
844
845 return 0;
846}
847
Iiro Valkonen7686b102011-02-02 23:21:58 -0800848static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700849 struct device_attribute *attr, char *buf)
850{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800851 struct mxt_data *data = dev_get_drvdata(dev);
852 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700853 int count = 0;
854 int i, j;
855 int error;
856 u8 val;
857
858 for (i = 0; i < data->info.object_num; i++) {
859 object = data->object_table + i;
860
861 count += sprintf(buf + count,
862 "Object Table Element %d(Type %d)\n",
863 i + 1, object->type);
864
Iiro Valkonen7686b102011-02-02 23:21:58 -0800865 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700866 count += sprintf(buf + count, "\n");
867 continue;
868 }
869
870 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800871 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700872 object->type, j, &val);
873 if (error)
874 return error;
875
876 count += sprintf(buf + count,
877 " Byte %d: 0x%x (%d)\n", j, val, val);
878 }
879
880 count += sprintf(buf + count, "\n");
881 }
882
883 return count;
884}
885
Iiro Valkonen7686b102011-02-02 23:21:58 -0800886static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700887{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800888 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700889 struct i2c_client *client = data->client;
890 const struct firmware *fw = NULL;
891 unsigned int frame_size;
892 unsigned int pos = 0;
893 int ret;
894
895 ret = request_firmware(&fw, fn, dev);
896 if (ret) {
897 dev_err(dev, "Unable to open firmware %s\n", fn);
898 return ret;
899 }
900
901 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800902 mxt_write_object(data, MXT_GEN_COMMAND,
903 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
904 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700905
906 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800907 if (client->addr == MXT_APP_LOW)
908 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800910 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700911
Iiro Valkonen7686b102011-02-02 23:21:58 -0800912 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700913 if (ret)
914 goto out;
915
916 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800917 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700918
919 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800920 ret = mxt_check_bootloader(client,
921 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700922 if (ret)
923 goto out;
924
925 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
926
927 /* We should add 2 at frame size as the the firmware data is not
928 * included the CRC bytes.
929 */
930 frame_size += 2;
931
932 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800933 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700934
Iiro Valkonen7686b102011-02-02 23:21:58 -0800935 ret = mxt_check_bootloader(client,
936 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937 if (ret)
938 goto out;
939
940 pos += frame_size;
941
942 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
943 }
944
945out:
946 release_firmware(fw);
947
948 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949 if (client->addr == MXT_BOOT_LOW)
950 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700951 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800952 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953
954 return ret;
955}
956
Iiro Valkonen7686b102011-02-02 23:21:58 -0800957static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958 struct device_attribute *attr,
959 const char *buf, size_t count)
960{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800961 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700962 int error;
963
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700964 disable_irq(data->irq);
965
Iiro Valkonen7686b102011-02-02 23:21:58 -0800966 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967 if (error) {
968 dev_err(dev, "The firmware update failed(%d)\n", error);
969 count = error;
970 } else {
971 dev_dbg(dev, "The firmware update succeeded\n");
972
973 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800974 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700975
976 kfree(data->object_table);
977 data->object_table = NULL;
978
Iiro Valkonen7686b102011-02-02 23:21:58 -0800979 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980 }
981
982 enable_irq(data->irq);
983
984 return count;
985}
986
Iiro Valkonen7686b102011-02-02 23:21:58 -0800987static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
988static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700989
Iiro Valkonen7686b102011-02-02 23:21:58 -0800990static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700991 &dev_attr_object.attr,
992 &dev_attr_update_fw.attr,
993 NULL
994};
995
Iiro Valkonen7686b102011-02-02 23:21:58 -0800996static const struct attribute_group mxt_attr_group = {
997 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998};
999
Iiro Valkonen7686b102011-02-02 23:21:58 -08001000static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001{
1002 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001003 mxt_write_object(data,
1004 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001005}
1006
Iiro Valkonen7686b102011-02-02 23:21:58 -08001007static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001008{
1009 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001010 mxt_write_object(data,
1011 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012}
1013
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001015{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001016 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001017
Iiro Valkonen7686b102011-02-02 23:21:58 -08001018 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001019
1020 return 0;
1021}
1022
Iiro Valkonen7686b102011-02-02 23:21:58 -08001023static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001024{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001025 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001026
Iiro Valkonen7686b102011-02-02 23:21:58 -08001027 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001028}
1029
Iiro Valkonen7686b102011-02-02 23:21:58 -08001030static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001031 const struct i2c_device_id *id)
1032{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001033 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001034 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035 struct input_dev *input_dev;
1036 int error;
1037
Iiro Valkonen919ed892011-02-15 13:36:52 -08001038 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001039 return -EINVAL;
1040
Iiro Valkonen7686b102011-02-02 23:21:58 -08001041 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042 input_dev = input_allocate_device();
1043 if (!data || !input_dev) {
1044 dev_err(&client->dev, "Failed to allocate memory\n");
1045 error = -ENOMEM;
1046 goto err_free_mem;
1047 }
1048
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050 input_dev->id.bustype = BUS_I2C;
1051 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001052 input_dev->open = mxt_input_open;
1053 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054
1055 __set_bit(EV_ABS, input_dev->evbit);
1056 __set_bit(EV_KEY, input_dev->evbit);
1057 __set_bit(BTN_TOUCH, input_dev->keybit);
1058
1059 /* For single touch */
1060 input_set_abs_params(input_dev, ABS_X,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001061 0, MXT_MAX_XC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001062 input_set_abs_params(input_dev, ABS_Y,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001063 0, MXT_MAX_YC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001064
1065 /* For multi touch */
1066 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001067 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001068 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001069 0, MXT_MAX_XC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001070 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001071 0, MXT_MAX_YC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001072
1073 input_set_drvdata(input_dev, data);
1074
1075 data->client = client;
1076 data->input_dev = input_dev;
Iiro Valkonen919ed892011-02-15 13:36:52 -08001077 data->pdata = pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078 data->irq = client->irq;
1079
1080 i2c_set_clientdata(client, data);
1081
Iiro Valkonen7686b102011-02-02 23:21:58 -08001082 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083 if (error)
1084 goto err_free_object;
1085
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001087 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001088 if (error) {
1089 dev_err(&client->dev, "Failed to register interrupt\n");
1090 goto err_free_object;
1091 }
1092
1093 error = input_register_device(input_dev);
1094 if (error)
1095 goto err_free_irq;
1096
Iiro Valkonen7686b102011-02-02 23:21:58 -08001097 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001098 if (error)
1099 goto err_unregister_device;
1100
1101 return 0;
1102
1103err_unregister_device:
1104 input_unregister_device(input_dev);
1105 input_dev = NULL;
1106err_free_irq:
1107 free_irq(client->irq, data);
1108err_free_object:
1109 kfree(data->object_table);
1110err_free_mem:
1111 input_free_device(input_dev);
1112 kfree(data);
1113 return error;
1114}
1115
Iiro Valkonen7686b102011-02-02 23:21:58 -08001116static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001117{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001118 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001119
Iiro Valkonen7686b102011-02-02 23:21:58 -08001120 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001121 free_irq(data->irq, data);
1122 input_unregister_device(data->input_dev);
1123 kfree(data->object_table);
1124 kfree(data);
1125
1126 return 0;
1127}
1128
1129#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001130static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001131{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001132 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001133 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001134 struct input_dev *input_dev = data->input_dev;
1135
1136 mutex_lock(&input_dev->mutex);
1137
1138 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001139 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140
1141 mutex_unlock(&input_dev->mutex);
1142
1143 return 0;
1144}
1145
Iiro Valkonen7686b102011-02-02 23:21:58 -08001146static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001147{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001148 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001149 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001150 struct input_dev *input_dev = data->input_dev;
1151
1152 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001153 mxt_write_object(data, MXT_GEN_COMMAND,
1154 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001155
Iiro Valkonen7686b102011-02-02 23:21:58 -08001156 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001157
1158 mutex_lock(&input_dev->mutex);
1159
1160 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001161 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001162
1163 mutex_unlock(&input_dev->mutex);
1164
1165 return 0;
1166}
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001167
Iiro Valkonen7686b102011-02-02 23:21:58 -08001168static const struct dev_pm_ops mxt_pm_ops = {
1169 .suspend = mxt_suspend,
1170 .resume = mxt_resume,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001171};
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001172#endif
1173
Iiro Valkonen7686b102011-02-02 23:21:58 -08001174static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001175 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001176 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001177 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001178 { }
1179};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001180MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001181
Iiro Valkonen7686b102011-02-02 23:21:58 -08001182static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001183 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001184 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001185 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001186#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001187 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001188#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001189 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001190 .probe = mxt_probe,
1191 .remove = __devexit_p(mxt_remove),
1192 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001193};
1194
Iiro Valkonen7686b102011-02-02 23:21:58 -08001195static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001196{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001197 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001198}
1199
Iiro Valkonen7686b102011-02-02 23:21:58 -08001200static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001202 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001203}
1204
Iiro Valkonen7686b102011-02-02 23:21:58 -08001205module_init(mxt_init);
1206module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001207
1208/* Module information */
1209MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001210MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001211MODULE_LICENSE("GPL");