blob: 0986fa46e9dfb20c2834e4b92d9526d0352a2505 [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 Shim979a72d2011-03-14 21:41:34 -070063#define MXT_SPT_COMMSCONFIG 18
Iiro Valkonen7686b102011-02-02 23:21:58 -080064#define MXT_SPT_GPIOPWM 19
65#define MXT_SPT_SELFTEST 25
66#define MXT_SPT_CTECONFIG 28
Joonyoung Shim979a72d2011-03-14 21:41:34 -070067#define MXT_SPT_USERDATA 38
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070068
Iiro Valkonen7686b102011-02-02 23:21:58 -080069/* MXT_GEN_COMMAND field */
70#define MXT_COMMAND_RESET 0
71#define MXT_COMMAND_BACKUPNV 1
72#define MXT_COMMAND_CALIBRATE 2
73#define MXT_COMMAND_REPORTALL 3
74#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070075
Iiro Valkonen7686b102011-02-02 23:21:58 -080076/* MXT_GEN_POWER field */
77#define MXT_POWER_IDLEACQINT 0
78#define MXT_POWER_ACTVACQINT 1
79#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070080
Iiro Valkonen7686b102011-02-02 23:21:58 -080081/* MXT_GEN_ACQUIRE field */
82#define MXT_ACQUIRE_CHRGTIME 0
83#define MXT_ACQUIRE_TCHDRIFT 2
84#define MXT_ACQUIRE_DRIFTST 3
85#define MXT_ACQUIRE_TCHAUTOCAL 4
86#define MXT_ACQUIRE_SYNC 5
87#define MXT_ACQUIRE_ATCHCALST 6
88#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070089
Iiro Valkonen7686b102011-02-02 23:21:58 -080090/* MXT_TOUCH_MULTI field */
91#define MXT_TOUCH_CTRL 0
92#define MXT_TOUCH_XORIGIN 1
93#define MXT_TOUCH_YORIGIN 2
94#define MXT_TOUCH_XSIZE 3
95#define MXT_TOUCH_YSIZE 4
96#define MXT_TOUCH_BLEN 6
97#define MXT_TOUCH_TCHTHR 7
98#define MXT_TOUCH_TCHDI 8
99#define MXT_TOUCH_ORIENT 9
100#define MXT_TOUCH_MOVHYSTI 11
101#define MXT_TOUCH_MOVHYSTN 12
102#define MXT_TOUCH_NUMTOUCH 14
103#define MXT_TOUCH_MRGHYST 15
104#define MXT_TOUCH_MRGTHR 16
105#define MXT_TOUCH_AMPHYST 17
106#define MXT_TOUCH_XRANGE_LSB 18
107#define MXT_TOUCH_XRANGE_MSB 19
108#define MXT_TOUCH_YRANGE_LSB 20
109#define MXT_TOUCH_YRANGE_MSB 21
110#define MXT_TOUCH_XLOCLIP 22
111#define MXT_TOUCH_XHICLIP 23
112#define MXT_TOUCH_YLOCLIP 24
113#define MXT_TOUCH_YHICLIP 25
114#define MXT_TOUCH_XEDGECTRL 26
115#define MXT_TOUCH_XEDGEDIST 27
116#define MXT_TOUCH_YEDGECTRL 28
117#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700118#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700119
Iiro Valkonen7686b102011-02-02 23:21:58 -0800120/* MXT_PROCI_GRIPFACE field */
121#define MXT_GRIPFACE_CTRL 0
122#define MXT_GRIPFACE_XLOGRIP 1
123#define MXT_GRIPFACE_XHIGRIP 2
124#define MXT_GRIPFACE_YLOGRIP 3
125#define MXT_GRIPFACE_YHIGRIP 4
126#define MXT_GRIPFACE_MAXTCHS 5
127#define MXT_GRIPFACE_SZTHR1 7
128#define MXT_GRIPFACE_SZTHR2 8
129#define MXT_GRIPFACE_SHPTHR1 9
130#define MXT_GRIPFACE_SHPTHR2 10
131#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700132
Iiro Valkonen7686b102011-02-02 23:21:58 -0800133/* MXT_PROCI_NOISE field */
134#define MXT_NOISE_CTRL 0
135#define MXT_NOISE_OUTFLEN 1
136#define MXT_NOISE_GCAFUL_LSB 3
137#define MXT_NOISE_GCAFUL_MSB 4
138#define MXT_NOISE_GCAFLL_LSB 5
139#define MXT_NOISE_GCAFLL_MSB 6
140#define MXT_NOISE_ACTVGCAFVALID 7
141#define MXT_NOISE_NOISETHR 8
142#define MXT_NOISE_FREQHOPSCALE 10
143#define MXT_NOISE_FREQ0 11
144#define MXT_NOISE_FREQ1 12
145#define MXT_NOISE_FREQ2 13
146#define MXT_NOISE_FREQ3 14
147#define MXT_NOISE_FREQ4 15
148#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700149
Iiro Valkonen7686b102011-02-02 23:21:58 -0800150/* MXT_SPT_COMMSCONFIG */
151#define MXT_COMMS_CTRL 0
152#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700153
Iiro Valkonen7686b102011-02-02 23:21:58 -0800154/* MXT_SPT_CTECONFIG field */
155#define MXT_CTE_CTRL 0
156#define MXT_CTE_CMD 1
157#define MXT_CTE_MODE 2
158#define MXT_CTE_IDLEGCAFDEPTH 3
159#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700160#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700161
Iiro Valkonen7686b102011-02-02 23:21:58 -0800162#define MXT_VOLTAGE_DEFAULT 2700000
163#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonen7686b102011-02-02 23:21:58 -0800165/* Define for MXT_GEN_COMMAND */
166#define MXT_BOOT_VALUE 0xa5
167#define MXT_BACKUP_VALUE 0x55
168#define MXT_BACKUP_TIME 25 /* msec */
169#define MXT_RESET_TIME 65 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700170
Iiro Valkonen7686b102011-02-02 23:21:58 -0800171#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700172
173/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800174#define MXT_UNLOCK_CMD_MSB 0xaa
175#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700176
177/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
179#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
180#define MXT_FRAME_CRC_CHECK 0x02
181#define MXT_FRAME_CRC_FAIL 0x03
182#define MXT_FRAME_CRC_PASS 0x04
183#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
184#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700185
186/* Touch status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800187#define MXT_SUPPRESS (1 << 1)
188#define MXT_AMP (1 << 2)
189#define MXT_VECTOR (1 << 3)
190#define MXT_MOVE (1 << 4)
191#define MXT_RELEASE (1 << 5)
192#define MXT_PRESS (1 << 6)
193#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700194
195/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800196#define MXT_MAX_XC 0x3ff
197#define MXT_MAX_YC 0x3ff
198#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_MAX_FINGER 10
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700201
Iiro Valkonen7686b102011-02-02 23:21:58 -0800202struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700203 u8 family_id;
204 u8 variant_id;
205 u8 version;
206 u8 build;
207 u8 matrix_xsize;
208 u8 matrix_ysize;
209 u8 object_num;
210};
211
Iiro Valkonen7686b102011-02-02 23:21:58 -0800212struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700213 u8 type;
214 u16 start_address;
215 u8 size;
216 u8 instances;
217 u8 num_report_ids;
218
219 /* to map object and message */
220 u8 max_reportid;
221};
222
Iiro Valkonen7686b102011-02-02 23:21:58 -0800223struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700224 u8 reportid;
225 u8 message[7];
226 u8 checksum;
227};
228
Iiro Valkonen7686b102011-02-02 23:21:58 -0800229struct mxt_finger {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700230 int status;
231 int x;
232 int y;
233 int area;
234};
235
236/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800237struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700238 struct i2c_client *client;
239 struct input_dev *input_dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800240 const struct mxt_platform_data *pdata;
241 struct mxt_object *object_table;
242 struct mxt_info info;
243 struct mxt_finger finger[MXT_MAX_FINGER];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700244 unsigned int irq;
245};
246
Iiro Valkonen7686b102011-02-02 23:21:58 -0800247static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700248{
249 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800250 case MXT_GEN_MESSAGE:
251 case MXT_GEN_COMMAND:
252 case MXT_GEN_POWER:
253 case MXT_GEN_ACQUIRE:
254 case MXT_TOUCH_MULTI:
255 case MXT_TOUCH_KEYARRAY:
256 case MXT_TOUCH_PROXIMITY:
257 case MXT_PROCI_GRIPFACE:
258 case MXT_PROCG_NOISE:
259 case MXT_PROCI_ONETOUCH:
260 case MXT_PROCI_TWOTOUCH:
261 case MXT_SPT_COMMSCONFIG:
262 case MXT_SPT_GPIOPWM:
263 case MXT_SPT_SELFTEST:
264 case MXT_SPT_CTECONFIG:
265 case MXT_SPT_USERDATA:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700266 return true;
267 default:
268 return false;
269 }
270}
271
Iiro Valkonen7686b102011-02-02 23:21:58 -0800272static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700273{
274 switch (type) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800275 case MXT_GEN_COMMAND:
276 case MXT_GEN_POWER:
277 case MXT_GEN_ACQUIRE:
278 case MXT_TOUCH_MULTI:
279 case MXT_TOUCH_KEYARRAY:
280 case MXT_TOUCH_PROXIMITY:
281 case MXT_PROCI_GRIPFACE:
282 case MXT_PROCG_NOISE:
283 case MXT_PROCI_ONETOUCH:
284 case MXT_PROCI_TWOTOUCH:
285 case MXT_SPT_GPIOPWM:
286 case MXT_SPT_SELFTEST:
287 case MXT_SPT_CTECONFIG:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700288 return true;
289 default:
290 return false;
291 }
292}
293
Iiro Valkonen7686b102011-02-02 23:21:58 -0800294static void mxt_dump_message(struct device *dev,
295 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700296{
297 dev_dbg(dev, "reportid:\t0x%x\n", message->reportid);
298 dev_dbg(dev, "message1:\t0x%x\n", message->message[0]);
299 dev_dbg(dev, "message2:\t0x%x\n", message->message[1]);
300 dev_dbg(dev, "message3:\t0x%x\n", message->message[2]);
301 dev_dbg(dev, "message4:\t0x%x\n", message->message[3]);
302 dev_dbg(dev, "message5:\t0x%x\n", message->message[4]);
303 dev_dbg(dev, "message6:\t0x%x\n", message->message[5]);
304 dev_dbg(dev, "message7:\t0x%x\n", message->message[6]);
305 dev_dbg(dev, "checksum:\t0x%x\n", message->checksum);
306}
307
Iiro Valkonen7686b102011-02-02 23:21:58 -0800308static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700309 unsigned int state)
310{
311 u8 val;
312
313recheck:
314 if (i2c_master_recv(client, &val, 1) != 1) {
315 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
316 return -EIO;
317 }
318
319 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800320 case MXT_WAITING_BOOTLOAD_CMD:
321 case MXT_WAITING_FRAME_DATA:
322 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700323 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800324 case MXT_FRAME_CRC_PASS:
325 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700326 goto recheck;
327 break;
328 default:
329 return -EINVAL;
330 }
331
332 if (val != state) {
333 dev_err(&client->dev, "Unvalid bootloader mode state\n");
334 return -EINVAL;
335 }
336
337 return 0;
338}
339
Iiro Valkonen7686b102011-02-02 23:21:58 -0800340static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700341{
342 u8 buf[2];
343
Iiro Valkonen7686b102011-02-02 23:21:58 -0800344 buf[0] = MXT_UNLOCK_CMD_LSB;
345 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700346
347 if (i2c_master_send(client, buf, 2) != 2) {
348 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
349 return -EIO;
350 }
351
352 return 0;
353}
354
Iiro Valkonen7686b102011-02-02 23:21:58 -0800355static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700356 const u8 *data, unsigned int frame_size)
357{
358 if (i2c_master_send(client, data, frame_size) != frame_size) {
359 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
360 return -EIO;
361 }
362
363 return 0;
364}
365
Iiro Valkonen7686b102011-02-02 23:21:58 -0800366static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700367 u16 reg, u16 len, void *val)
368{
369 struct i2c_msg xfer[2];
370 u8 buf[2];
371
372 buf[0] = reg & 0xff;
373 buf[1] = (reg >> 8) & 0xff;
374
375 /* Write register */
376 xfer[0].addr = client->addr;
377 xfer[0].flags = 0;
378 xfer[0].len = 2;
379 xfer[0].buf = buf;
380
381 /* Read data */
382 xfer[1].addr = client->addr;
383 xfer[1].flags = I2C_M_RD;
384 xfer[1].len = len;
385 xfer[1].buf = val;
386
387 if (i2c_transfer(client->adapter, xfer, 2) != 2) {
388 dev_err(&client->dev, "%s: i2c transfer failed\n", __func__);
389 return -EIO;
390 }
391
392 return 0;
393}
394
Iiro Valkonen7686b102011-02-02 23:21:58 -0800395static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700396{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800397 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700398}
399
Iiro Valkonen7686b102011-02-02 23:21:58 -0800400static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700401{
402 u8 buf[3];
403
404 buf[0] = reg & 0xff;
405 buf[1] = (reg >> 8) & 0xff;
406 buf[2] = val;
407
408 if (i2c_master_send(client, buf, 3) != 3) {
409 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
410 return -EIO;
411 }
412
413 return 0;
414}
415
Iiro Valkonen7686b102011-02-02 23:21:58 -0800416static int mxt_read_object_table(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700417 u16 reg, u8 *object_buf)
418{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800419 return __mxt_read_reg(client, reg, MXT_OBJECT_SIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700420 object_buf);
421}
422
Iiro Valkonen7686b102011-02-02 23:21:58 -0800423static struct mxt_object *
424mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700425{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800426 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700427 int i;
428
429 for (i = 0; i < data->info.object_num; i++) {
430 object = data->object_table + i;
431 if (object->type == type)
432 return object;
433 }
434
435 dev_err(&data->client->dev, "Invalid object type\n");
436 return NULL;
437}
438
Iiro Valkonen7686b102011-02-02 23:21:58 -0800439static int mxt_read_message(struct mxt_data *data,
440 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700441{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800442 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700443 u16 reg;
444
Iiro Valkonen7686b102011-02-02 23:21:58 -0800445 object = mxt_get_object(data, MXT_GEN_MESSAGE);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700446 if (!object)
447 return -EINVAL;
448
449 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800450 return __mxt_read_reg(data->client, reg,
451 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700452}
453
Iiro Valkonen7686b102011-02-02 23:21:58 -0800454static int mxt_read_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700455 u8 type, u8 offset, u8 *val)
456{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800457 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700458 u16 reg;
459
Iiro Valkonen7686b102011-02-02 23:21:58 -0800460 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700461 if (!object)
462 return -EINVAL;
463
464 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800465 return __mxt_read_reg(data->client, reg + offset, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700466}
467
Iiro Valkonen7686b102011-02-02 23:21:58 -0800468static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700469 u8 type, u8 offset, u8 val)
470{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800471 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700472 u16 reg;
473
Iiro Valkonen7686b102011-02-02 23:21:58 -0800474 object = mxt_get_object(data, type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700475 if (!object)
476 return -EINVAL;
477
478 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800479 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700480}
481
Iiro Valkonen7686b102011-02-02 23:21:58 -0800482static void mxt_input_report(struct mxt_data *data, int single_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800484 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485 struct input_dev *input_dev = data->input_dev;
486 int status = finger[single_id].status;
487 int finger_num = 0;
488 int id;
489
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 for (id = 0; id < MXT_MAX_FINGER; id++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491 if (!finger[id].status)
492 continue;
493
494 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800495 finger[id].status != MXT_RELEASE ?
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700496 finger[id].area : 0);
497 input_report_abs(input_dev, ABS_MT_POSITION_X,
498 finger[id].x);
499 input_report_abs(input_dev, ABS_MT_POSITION_Y,
500 finger[id].y);
501 input_mt_sync(input_dev);
502
Iiro Valkonen7686b102011-02-02 23:21:58 -0800503 if (finger[id].status == MXT_RELEASE)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504 finger[id].status = 0;
505 else
506 finger_num++;
507 }
508
509 input_report_key(input_dev, BTN_TOUCH, finger_num > 0);
510
Iiro Valkonen7686b102011-02-02 23:21:58 -0800511 if (status != MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700512 input_report_abs(input_dev, ABS_X, finger[single_id].x);
513 input_report_abs(input_dev, ABS_Y, finger[single_id].y);
514 }
515
516 input_sync(input_dev);
517}
518
Iiro Valkonen7686b102011-02-02 23:21:58 -0800519static void mxt_input_touchevent(struct mxt_data *data,
520 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700521{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800522 struct mxt_finger *finger = data->finger;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700523 struct device *dev = &data->client->dev;
524 u8 status = message->message[0];
525 int x;
526 int y;
527 int area;
528
529 /* Check the touch is present on the screen */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800530 if (!(status & MXT_DETECT)) {
531 if (status & MXT_RELEASE) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532 dev_dbg(dev, "[%d] released\n", id);
533
Iiro Valkonen7686b102011-02-02 23:21:58 -0800534 finger[id].status = MXT_RELEASE;
535 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536 }
537 return;
538 }
539
540 /* Check only AMP detection */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800541 if (!(status & (MXT_PRESS | MXT_MOVE)))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542 return;
543
544 x = (message->message[1] << 2) | ((message->message[3] & ~0x3f) >> 6);
545 y = (message->message[2] << 2) | ((message->message[3] & ~0xf3) >> 2);
546 area = message->message[4];
547
548 dev_dbg(dev, "[%d] %s x: %d, y: %d, area: %d\n", id,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800549 status & MXT_MOVE ? "moved" : "pressed",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550 x, y, area);
551
Iiro Valkonen7686b102011-02-02 23:21:58 -0800552 finger[id].status = status & MXT_MOVE ?
553 MXT_MOVE : MXT_PRESS;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554 finger[id].x = x;
555 finger[id].y = y;
556 finger[id].area = area;
557
Iiro Valkonen7686b102011-02-02 23:21:58 -0800558 mxt_input_report(data, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700559}
560
Iiro Valkonen7686b102011-02-02 23:21:58 -0800561static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700562{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800563 struct mxt_data *data = dev_id;
564 struct mxt_message message;
565 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700566 struct device *dev = &data->client->dev;
567 int id;
568 u8 reportid;
569 u8 max_reportid;
570 u8 min_reportid;
571
572 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800573 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700574 dev_err(dev, "Failed to read message\n");
575 goto end;
576 }
577
578 reportid = message.reportid;
579
Iiro Valkonen7686b102011-02-02 23:21:58 -0800580 /* whether reportid is thing of MXT_TOUCH_MULTI */
581 object = mxt_get_object(data, MXT_TOUCH_MULTI);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 if (!object)
583 goto end;
584
585 max_reportid = object->max_reportid;
586 min_reportid = max_reportid - object->num_report_ids + 1;
587 id = reportid - min_reportid;
588
589 if (reportid >= min_reportid && reportid <= max_reportid)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590 mxt_input_touchevent(data, &message, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 mxt_dump_message(dev, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 } while (reportid != 0xff);
594
595end:
596 return IRQ_HANDLED;
597}
598
Iiro Valkonen7686b102011-02-02 23:21:58 -0800599static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800601 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800602 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603 struct device *dev = &data->client->dev;
604 int index = 0;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800605 int i, j, config_offset;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606
Iiro Valkonen71749f52011-02-15 13:36:52 -0800607 if (!pdata->config) {
608 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
609 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700610 }
611
612 for (i = 0; i < data->info.object_num; i++) {
613 object = data->object_table + i;
614
Iiro Valkonen7686b102011-02-02 23:21:58 -0800615 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616 continue;
617
Iiro Valkonen71749f52011-02-15 13:36:52 -0800618 for (j = 0; j < object->size + 1; j++) {
619 config_offset = index + j;
620 if (config_offset > pdata->config_length) {
621 dev_err(dev, "Not enough config data!\n");
622 return -EINVAL;
623 }
Iiro Valkonen7686b102011-02-02 23:21:58 -0800624 mxt_write_object(data, object->type, j,
Iiro Valkonen71749f52011-02-15 13:36:52 -0800625 pdata->config[config_offset]);
626 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700627 index += object->size + 1;
628 }
629
630 return 0;
631}
632
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634{
635 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800636 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637 int count = 10;
638 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639
640 /* Read dummy message to make high CHG pin */
641 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800642 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643 if (error)
644 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800645 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646
647 if (!count) {
648 dev_err(dev, "CHG pin isn't cleared\n");
649 return -EBUSY;
650 }
651
652 return 0;
653}
654
Iiro Valkonen7686b102011-02-02 23:21:58 -0800655static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800657 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658 u8 voltage;
659
660 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800661 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800663 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664 pdata->y_line);
665
666 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800667 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 pdata->orient);
669
670 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800671 mxt_write_object(data, MXT_TOUCH_MULTI,
672 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700673
674 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800675 mxt_write_object(data, MXT_TOUCH_MULTI,
676 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677
678 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800679 mxt_write_object(data, MXT_TOUCH_MULTI,
680 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
681 mxt_write_object(data, MXT_TOUCH_MULTI,
682 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
683 mxt_write_object(data, MXT_TOUCH_MULTI,
684 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
685 mxt_write_object(data, MXT_TOUCH_MULTI,
686 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687
688 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700689 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800690 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
691 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
692 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700693 voltage = 0xff - voltage + 1;
694 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800695 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
696 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700697
Iiro Valkonen7686b102011-02-02 23:21:58 -0800698 mxt_write_object(data, MXT_SPT_CTECONFIG,
699 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700 }
701}
702
Iiro Valkonen7686b102011-02-02 23:21:58 -0800703static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700704{
705 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800706 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700707 int error;
708 u8 val;
709
Iiro Valkonen7686b102011-02-02 23:21:58 -0800710 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 if (error)
712 return error;
713 info->family_id = val;
714
Iiro Valkonen7686b102011-02-02 23:21:58 -0800715 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716 if (error)
717 return error;
718 info->variant_id = val;
719
Iiro Valkonen7686b102011-02-02 23:21:58 -0800720 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721 if (error)
722 return error;
723 info->version = val;
724
Iiro Valkonen7686b102011-02-02 23:21:58 -0800725 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700726 if (error)
727 return error;
728 info->build = val;
729
Iiro Valkonen7686b102011-02-02 23:21:58 -0800730 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731 if (error)
732 return error;
733 info->object_num = val;
734
735 return 0;
736}
737
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739{
740 int error;
741 int i;
742 u16 reg;
743 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800744 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700745
746 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748
Iiro Valkonen7686b102011-02-02 23:21:58 -0800749 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
750 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700751 if (error)
752 return error;
753
754 object->type = buf[0];
755 object->start_address = (buf[2] << 8) | buf[1];
756 object->size = buf[3];
757 object->instances = buf[4];
758 object->num_report_ids = buf[5];
759
760 if (object->num_report_ids) {
761 reportid += object->num_report_ids *
762 (object->instances + 1);
763 object->max_reportid = reportid;
764 }
765 }
766
767 return 0;
768}
769
Iiro Valkonen7686b102011-02-02 23:21:58 -0800770static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700771{
772 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800773 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700774 int error;
775 u8 val;
776
Iiro Valkonen7686b102011-02-02 23:21:58 -0800777 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700778 if (error)
779 return error;
780
781 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800782 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783 GFP_KERNEL);
784 if (!data->object_table) {
785 dev_err(&client->dev, "Failed to allocate memory\n");
786 return -ENOMEM;
787 }
788
789 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791 if (error)
792 return error;
793
794 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800795 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700796 if (error)
797 return error;
798
Iiro Valkonen7686b102011-02-02 23:21:58 -0800799 error = mxt_make_highchg(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700800 if (error)
801 return error;
802
Iiro Valkonen7686b102011-02-02 23:21:58 -0800803 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700804
805 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800806 mxt_write_object(data, MXT_GEN_COMMAND,
807 MXT_COMMAND_BACKUPNV,
808 MXT_BACKUP_VALUE);
809 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700810
811 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800812 mxt_write_object(data, MXT_GEN_COMMAND,
813 MXT_COMMAND_RESET, 1);
814 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700815
816 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818 if (error)
819 return error;
820 info->matrix_xsize = val;
821
Iiro Valkonen7686b102011-02-02 23:21:58 -0800822 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700823 if (error)
824 return error;
825 info->matrix_ysize = val;
826
827 dev_info(&client->dev,
828 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
829 info->family_id, info->variant_id, info->version,
830 info->build);
831
832 dev_info(&client->dev,
833 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
834 info->matrix_xsize, info->matrix_ysize,
835 info->object_num);
836
837 return 0;
838}
839
Iiro Valkonen7686b102011-02-02 23:21:58 -0800840static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700841 struct device_attribute *attr, char *buf)
842{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800843 struct mxt_data *data = dev_get_drvdata(dev);
844 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845 int count = 0;
846 int i, j;
847 int error;
848 u8 val;
849
850 for (i = 0; i < data->info.object_num; i++) {
851 object = data->object_table + i;
852
853 count += sprintf(buf + count,
854 "Object Table Element %d(Type %d)\n",
855 i + 1, object->type);
856
Iiro Valkonen7686b102011-02-02 23:21:58 -0800857 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700858 count += sprintf(buf + count, "\n");
859 continue;
860 }
861
862 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800863 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700864 object->type, j, &val);
865 if (error)
866 return error;
867
868 count += sprintf(buf + count,
869 " Byte %d: 0x%x (%d)\n", j, val, val);
870 }
871
872 count += sprintf(buf + count, "\n");
873 }
874
875 return count;
876}
877
Iiro Valkonen7686b102011-02-02 23:21:58 -0800878static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700879{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800880 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700881 struct i2c_client *client = data->client;
882 const struct firmware *fw = NULL;
883 unsigned int frame_size;
884 unsigned int pos = 0;
885 int ret;
886
887 ret = request_firmware(&fw, fn, dev);
888 if (ret) {
889 dev_err(dev, "Unable to open firmware %s\n", fn);
890 return ret;
891 }
892
893 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800894 mxt_write_object(data, MXT_GEN_COMMAND,
895 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
896 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700897
898 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800899 if (client->addr == MXT_APP_LOW)
900 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800902 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700903
Iiro Valkonen7686b102011-02-02 23:21:58 -0800904 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700905 if (ret)
906 goto out;
907
908 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800909 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700910
911 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800912 ret = mxt_check_bootloader(client,
913 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700914 if (ret)
915 goto out;
916
917 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
918
919 /* We should add 2 at frame size as the the firmware data is not
920 * included the CRC bytes.
921 */
922 frame_size += 2;
923
924 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800925 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700926
Iiro Valkonen7686b102011-02-02 23:21:58 -0800927 ret = mxt_check_bootloader(client,
928 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929 if (ret)
930 goto out;
931
932 pos += frame_size;
933
934 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
935 }
936
937out:
938 release_firmware(fw);
939
940 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800941 if (client->addr == MXT_BOOT_LOW)
942 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700943 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800944 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700945
946 return ret;
947}
948
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950 struct device_attribute *attr,
951 const char *buf, size_t count)
952{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954 int error;
955
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956 disable_irq(data->irq);
957
Iiro Valkonen7686b102011-02-02 23:21:58 -0800958 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700959 if (error) {
960 dev_err(dev, "The firmware update failed(%d)\n", error);
961 count = error;
962 } else {
963 dev_dbg(dev, "The firmware update succeeded\n");
964
965 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800966 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967
968 kfree(data->object_table);
969 data->object_table = NULL;
970
Iiro Valkonen7686b102011-02-02 23:21:58 -0800971 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972 }
973
974 enable_irq(data->irq);
975
976 return count;
977}
978
Iiro Valkonen7686b102011-02-02 23:21:58 -0800979static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
980static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700981
Iiro Valkonen7686b102011-02-02 23:21:58 -0800982static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983 &dev_attr_object.attr,
984 &dev_attr_update_fw.attr,
985 NULL
986};
987
Iiro Valkonen7686b102011-02-02 23:21:58 -0800988static const struct attribute_group mxt_attr_group = {
989 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700990};
991
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993{
994 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800995 mxt_write_object(data,
996 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997}
998
Iiro Valkonen7686b102011-02-02 23:21:58 -0800999static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000{
1001 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002 mxt_write_object(data,
1003 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001004}
1005
Iiro Valkonen7686b102011-02-02 23:21:58 -08001006static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001007{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001008 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009
Iiro Valkonen7686b102011-02-02 23:21:58 -08001010 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001011
1012 return 0;
1013}
1014
Iiro Valkonen7686b102011-02-02 23:21:58 -08001015static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
Iiro Valkonen7686b102011-02-02 23:21:58 -08001019 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020}
1021
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001023 const struct i2c_device_id *id)
1024{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001025 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001026 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027 struct input_dev *input_dev;
1028 int error;
1029
Iiro Valkonen919ed892011-02-15 13:36:52 -08001030 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001031 return -EINVAL;
1032
Iiro Valkonen7686b102011-02-02 23:21:58 -08001033 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001034 input_dev = input_allocate_device();
1035 if (!data || !input_dev) {
1036 dev_err(&client->dev, "Failed to allocate memory\n");
1037 error = -ENOMEM;
1038 goto err_free_mem;
1039 }
1040
Iiro Valkonen7686b102011-02-02 23:21:58 -08001041 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042 input_dev->id.bustype = BUS_I2C;
1043 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001044 input_dev->open = mxt_input_open;
1045 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001046
1047 __set_bit(EV_ABS, input_dev->evbit);
1048 __set_bit(EV_KEY, input_dev->evbit);
1049 __set_bit(BTN_TOUCH, input_dev->keybit);
1050
1051 /* For single touch */
1052 input_set_abs_params(input_dev, ABS_X,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001053 0, MXT_MAX_XC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054 input_set_abs_params(input_dev, ABS_Y,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001055 0, MXT_MAX_YC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001056
1057 /* For multi touch */
1058 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001059 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001060 input_set_abs_params(input_dev, ABS_MT_POSITION_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_MT_POSITION_Y,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001063 0, MXT_MAX_YC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001064
1065 input_set_drvdata(input_dev, data);
1066
1067 data->client = client;
1068 data->input_dev = input_dev;
Iiro Valkonen919ed892011-02-15 13:36:52 -08001069 data->pdata = pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001070 data->irq = client->irq;
1071
1072 i2c_set_clientdata(client, data);
1073
Iiro Valkonen7686b102011-02-02 23:21:58 -08001074 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001075 if (error)
1076 goto err_free_object;
1077
Iiro Valkonen7686b102011-02-02 23:21:58 -08001078 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001079 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080 if (error) {
1081 dev_err(&client->dev, "Failed to register interrupt\n");
1082 goto err_free_object;
1083 }
1084
1085 error = input_register_device(input_dev);
1086 if (error)
1087 goto err_free_irq;
1088
Iiro Valkonen7686b102011-02-02 23:21:58 -08001089 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001090 if (error)
1091 goto err_unregister_device;
1092
1093 return 0;
1094
1095err_unregister_device:
1096 input_unregister_device(input_dev);
1097 input_dev = NULL;
1098err_free_irq:
1099 free_irq(client->irq, data);
1100err_free_object:
1101 kfree(data->object_table);
1102err_free_mem:
1103 input_free_device(input_dev);
1104 kfree(data);
1105 return error;
1106}
1107
Iiro Valkonen7686b102011-02-02 23:21:58 -08001108static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001109{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001110 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111
Iiro Valkonen7686b102011-02-02 23:21:58 -08001112 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001113 free_irq(data->irq, data);
1114 input_unregister_device(data->input_dev);
1115 kfree(data->object_table);
1116 kfree(data);
1117
1118 return 0;
1119}
1120
1121#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001122static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001123{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001124 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001125 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001126 struct input_dev *input_dev = data->input_dev;
1127
1128 mutex_lock(&input_dev->mutex);
1129
1130 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001131 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132
1133 mutex_unlock(&input_dev->mutex);
1134
1135 return 0;
1136}
1137
Iiro Valkonen7686b102011-02-02 23:21:58 -08001138static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001139{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001140 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001141 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001142 struct input_dev *input_dev = data->input_dev;
1143
1144 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001145 mxt_write_object(data, MXT_GEN_COMMAND,
1146 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001147
Iiro Valkonen7686b102011-02-02 23:21:58 -08001148 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001149
1150 mutex_lock(&input_dev->mutex);
1151
1152 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001153 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001154
1155 mutex_unlock(&input_dev->mutex);
1156
1157 return 0;
1158}
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001159
Iiro Valkonen7686b102011-02-02 23:21:58 -08001160static const struct dev_pm_ops mxt_pm_ops = {
1161 .suspend = mxt_suspend,
1162 .resume = mxt_resume,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001163};
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001164#endif
1165
Iiro Valkonen7686b102011-02-02 23:21:58 -08001166static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001167 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001168 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001169 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001170 { }
1171};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001172MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001173
Iiro Valkonen7686b102011-02-02 23:21:58 -08001174static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001175 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001176 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001177 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001178#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001179 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001180#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001181 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001182 .probe = mxt_probe,
1183 .remove = __devexit_p(mxt_remove),
1184 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001185};
1186
Iiro Valkonen7686b102011-02-02 23:21:58 -08001187static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001188{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001189 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001190}
1191
Iiro Valkonen7686b102011-02-02 23:21:58 -08001192static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001193{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001194 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001195}
1196
Iiro Valkonen7686b102011-02-02 23:21:58 -08001197module_init(mxt_init);
1198module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001199
1200/* Module information */
1201MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001202MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001203MODULE_LICENSE("GPL");