blob: d2e5864ca096ae8ce00db1ea47c8132241101ff2 [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
63#define MXT_SPT_COMMSCONFIG 18 /* firmware ver 21 over */
64#define MXT_SPT_GPIOPWM 19
65#define MXT_SPT_SELFTEST 25
66#define MXT_SPT_CTECONFIG 28
67#define MXT_SPT_USERDATA 38 /* firmware ver 21 over */
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
118#define MXT_TOUCH_JUMPLIMIT 30 /* firmware ver 22 over */
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
160#define MXT_CTE_VOLTAGE 5 /* firmware ver 21 over */
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_check_matrix_size(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636 struct device *dev = &data->client->dev;
637 int mode = -1;
638 int error;
639 u8 val;
640
641 dev_dbg(dev, "Number of X lines: %d\n", pdata->x_line);
642 dev_dbg(dev, "Number of Y lines: %d\n", pdata->y_line);
643
644 switch (pdata->x_line) {
645 case 0 ... 15:
646 if (pdata->y_line <= 14)
647 mode = 0;
648 break;
649 case 16:
650 if (pdata->y_line <= 12)
651 mode = 1;
652 if (pdata->y_line == 13 || pdata->y_line == 14)
653 mode = 0;
654 break;
655 case 17:
656 if (pdata->y_line <= 11)
657 mode = 2;
658 if (pdata->y_line == 12 || pdata->y_line == 13)
659 mode = 1;
660 break;
661 case 18:
662 if (pdata->y_line <= 10)
663 mode = 3;
664 if (pdata->y_line == 11 || pdata->y_line == 12)
665 mode = 2;
666 break;
667 case 19:
668 if (pdata->y_line <= 9)
669 mode = 4;
670 if (pdata->y_line == 10 || pdata->y_line == 11)
671 mode = 3;
672 break;
673 case 20:
674 mode = 4;
675 }
676
677 if (mode < 0) {
678 dev_err(dev, "Invalid X/Y lines\n");
679 return -EINVAL;
680 }
681
Iiro Valkonen7686b102011-02-02 23:21:58 -0800682 error = mxt_read_object(data, MXT_SPT_CTECONFIG,
683 MXT_CTE_MODE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684 if (error)
685 return error;
686
687 if (mode == val)
688 return 0;
689
690 /* Change the CTE configuration */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800691 mxt_write_object(data, MXT_SPT_CTECONFIG,
692 MXT_CTE_CTRL, 1);
693 mxt_write_object(data, MXT_SPT_CTECONFIG,
694 MXT_CTE_MODE, mode);
695 mxt_write_object(data, MXT_SPT_CTECONFIG,
696 MXT_CTE_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700697
698 return 0;
699}
700
Iiro Valkonen7686b102011-02-02 23:21:58 -0800701static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702{
703 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800704 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705 int count = 10;
706 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700707
708 /* Read dummy message to make high CHG pin */
709 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800710 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 if (error)
712 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800713 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714
715 if (!count) {
716 dev_err(dev, "CHG pin isn't cleared\n");
717 return -EBUSY;
718 }
719
720 return 0;
721}
722
Iiro Valkonen7686b102011-02-02 23:21:58 -0800723static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700724{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800725 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700726 u8 voltage;
727
728 /* Set touchscreen lines */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800729 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730 pdata->x_line);
Iiro Valkonen7686b102011-02-02 23:21:58 -0800731 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700732 pdata->y_line);
733
734 /* Set touchscreen orient */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800735 mxt_write_object(data, MXT_TOUCH_MULTI, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700736 pdata->orient);
737
738 /* Set touchscreen burst length */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800739 mxt_write_object(data, MXT_TOUCH_MULTI,
740 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700741
742 /* Set touchscreen threshold */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800743 mxt_write_object(data, MXT_TOUCH_MULTI,
744 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700745
746 /* Set touchscreen resolution */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 mxt_write_object(data, MXT_TOUCH_MULTI,
748 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
749 mxt_write_object(data, MXT_TOUCH_MULTI,
750 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
751 mxt_write_object(data, MXT_TOUCH_MULTI,
752 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
753 mxt_write_object(data, MXT_TOUCH_MULTI,
754 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700755
756 /* Set touchscreen voltage */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800757 if (data->info.version >= MXT_VER_21 && pdata->voltage) {
758 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
759 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
760 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700761 voltage = 0xff - voltage + 1;
762 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800763 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
764 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700765
Iiro Valkonen7686b102011-02-02 23:21:58 -0800766 mxt_write_object(data, MXT_SPT_CTECONFIG,
767 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700768 }
769}
770
Iiro Valkonen7686b102011-02-02 23:21:58 -0800771static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700772{
773 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800774 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700775 int error;
776 u8 val;
777
Iiro Valkonen7686b102011-02-02 23:21:58 -0800778 error = mxt_read_reg(client, MXT_FAMILY_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779 if (error)
780 return error;
781 info->family_id = val;
782
Iiro Valkonen7686b102011-02-02 23:21:58 -0800783 error = mxt_read_reg(client, MXT_VARIANT_ID, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700784 if (error)
785 return error;
786 info->variant_id = val;
787
Iiro Valkonen7686b102011-02-02 23:21:58 -0800788 error = mxt_read_reg(client, MXT_VERSION, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789 if (error)
790 return error;
791 info->version = val;
792
Iiro Valkonen7686b102011-02-02 23:21:58 -0800793 error = mxt_read_reg(client, MXT_BUILD, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794 if (error)
795 return error;
796 info->build = val;
797
Iiro Valkonen7686b102011-02-02 23:21:58 -0800798 error = mxt_read_reg(client, MXT_OBJECT_NUM, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700799 if (error)
800 return error;
801 info->object_num = val;
802
803 return 0;
804}
805
Iiro Valkonen7686b102011-02-02 23:21:58 -0800806static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700807{
808 int error;
809 int i;
810 u16 reg;
811 u8 reportid = 0;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800812 u8 buf[MXT_OBJECT_SIZE];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700813
814 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800815 struct mxt_object *object = data->object_table + i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700816
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 reg = MXT_OBJECT_START + MXT_OBJECT_SIZE * i;
818 error = mxt_read_object_table(data->client, reg, buf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700819 if (error)
820 return error;
821
822 object->type = buf[0];
823 object->start_address = (buf[2] << 8) | buf[1];
824 object->size = buf[3];
825 object->instances = buf[4];
826 object->num_report_ids = buf[5];
827
828 if (object->num_report_ids) {
829 reportid += object->num_report_ids *
830 (object->instances + 1);
831 object->max_reportid = reportid;
832 }
833 }
834
835 return 0;
836}
837
Iiro Valkonen7686b102011-02-02 23:21:58 -0800838static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700839{
840 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800841 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842 int error;
843 u8 val;
844
Iiro Valkonen7686b102011-02-02 23:21:58 -0800845 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700846 if (error)
847 return error;
848
849 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800850 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700851 GFP_KERNEL);
852 if (!data->object_table) {
853 dev_err(&client->dev, "Failed to allocate memory\n");
854 return -ENOMEM;
855 }
856
857 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700859 if (error)
860 return error;
861
862 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800863 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700864 if (error)
865 return error;
866
867 /* Check X/Y matrix size */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800868 error = mxt_check_matrix_size(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700869 if (error)
870 return error;
871
Iiro Valkonen7686b102011-02-02 23:21:58 -0800872 error = mxt_make_highchg(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700873 if (error)
874 return error;
875
Iiro Valkonen7686b102011-02-02 23:21:58 -0800876 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700877
878 /* Backup to memory */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800879 mxt_write_object(data, MXT_GEN_COMMAND,
880 MXT_COMMAND_BACKUPNV,
881 MXT_BACKUP_VALUE);
882 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700883
884 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800885 mxt_write_object(data, MXT_GEN_COMMAND,
886 MXT_COMMAND_RESET, 1);
887 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700888
889 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800890 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700891 if (error)
892 return error;
893 info->matrix_xsize = val;
894
Iiro Valkonen7686b102011-02-02 23:21:58 -0800895 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700896 if (error)
897 return error;
898 info->matrix_ysize = val;
899
900 dev_info(&client->dev,
901 "Family ID: %d Variant ID: %d Version: %d Build: %d\n",
902 info->family_id, info->variant_id, info->version,
903 info->build);
904
905 dev_info(&client->dev,
906 "Matrix X Size: %d Matrix Y Size: %d Object Num: %d\n",
907 info->matrix_xsize, info->matrix_ysize,
908 info->object_num);
909
910 return 0;
911}
912
Iiro Valkonen7686b102011-02-02 23:21:58 -0800913static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700914 struct device_attribute *attr, char *buf)
915{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800916 struct mxt_data *data = dev_get_drvdata(dev);
917 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700918 int count = 0;
919 int i, j;
920 int error;
921 u8 val;
922
923 for (i = 0; i < data->info.object_num; i++) {
924 object = data->object_table + i;
925
926 count += sprintf(buf + count,
927 "Object Table Element %d(Type %d)\n",
928 i + 1, object->type);
929
Iiro Valkonen7686b102011-02-02 23:21:58 -0800930 if (!mxt_object_readable(object->type)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931 count += sprintf(buf + count, "\n");
932 continue;
933 }
934
935 for (j = 0; j < object->size + 1; j++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800936 error = mxt_read_object(data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937 object->type, j, &val);
938 if (error)
939 return error;
940
941 count += sprintf(buf + count,
942 " Byte %d: 0x%x (%d)\n", j, val, val);
943 }
944
945 count += sprintf(buf + count, "\n");
946 }
947
948 return count;
949}
950
Iiro Valkonen7686b102011-02-02 23:21:58 -0800951static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700952{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954 struct i2c_client *client = data->client;
955 const struct firmware *fw = NULL;
956 unsigned int frame_size;
957 unsigned int pos = 0;
958 int ret;
959
960 ret = request_firmware(&fw, fn, dev);
961 if (ret) {
962 dev_err(dev, "Unable to open firmware %s\n", fn);
963 return ret;
964 }
965
966 /* Change to the bootloader mode */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800967 mxt_write_object(data, MXT_GEN_COMMAND,
968 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
969 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700970
971 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800972 if (client->addr == MXT_APP_LOW)
973 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700974 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800975 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700976
Iiro Valkonen7686b102011-02-02 23:21:58 -0800977 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700978 if (ret)
979 goto out;
980
981 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800982 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983
984 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800985 ret = mxt_check_bootloader(client,
986 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700987 if (ret)
988 goto out;
989
990 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
991
992 /* We should add 2 at frame size as the the firmware data is not
993 * included the CRC bytes.
994 */
995 frame_size += 2;
996
997 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800998 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700999
Iiro Valkonen7686b102011-02-02 23:21:58 -08001000 ret = mxt_check_bootloader(client,
1001 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001002 if (ret)
1003 goto out;
1004
1005 pos += frame_size;
1006
1007 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1008 }
1009
1010out:
1011 release_firmware(fw);
1012
1013 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014 if (client->addr == MXT_BOOT_LOW)
1015 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001017 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001018
1019 return ret;
1020}
1021
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001023 struct device_attribute *attr,
1024 const char *buf, size_t count)
1025{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001026 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027 unsigned int version;
1028 int error;
1029
1030 if (sscanf(buf, "%u", &version) != 1) {
1031 dev_err(dev, "Invalid values\n");
1032 return -EINVAL;
1033 }
1034
Iiro Valkonen7686b102011-02-02 23:21:58 -08001035 if (data->info.version < MXT_VER_21 || version < MXT_VER_21) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001036 dev_err(dev, "FW update supported starting with version 21\n");
1037 return -EINVAL;
1038 }
1039
1040 disable_irq(data->irq);
1041
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043 if (error) {
1044 dev_err(dev, "The firmware update failed(%d)\n", error);
1045 count = error;
1046 } else {
1047 dev_dbg(dev, "The firmware update succeeded\n");
1048
1049 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001050 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001051
1052 kfree(data->object_table);
1053 data->object_table = NULL;
1054
Iiro Valkonen7686b102011-02-02 23:21:58 -08001055 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001056 }
1057
1058 enable_irq(data->irq);
1059
1060 return count;
1061}
1062
Iiro Valkonen7686b102011-02-02 23:21:58 -08001063static DEVICE_ATTR(object, 0444, mxt_object_show, NULL);
1064static DEVICE_ATTR(update_fw, 0664, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066static struct attribute *mxt_attrs[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067 &dev_attr_object.attr,
1068 &dev_attr_update_fw.attr,
1069 NULL
1070};
1071
Iiro Valkonen7686b102011-02-02 23:21:58 -08001072static const struct attribute_group mxt_attr_group = {
1073 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074};
1075
Iiro Valkonen7686b102011-02-02 23:21:58 -08001076static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001077{
1078 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079 mxt_write_object(data,
1080 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001081}
1082
Iiro Valkonen7686b102011-02-02 23:21:58 -08001083static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001084{
1085 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086 mxt_write_object(data,
1087 MXT_TOUCH_MULTI, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001088}
1089
Iiro Valkonen7686b102011-02-02 23:21:58 -08001090static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001091{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001092 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093
Iiro Valkonen7686b102011-02-02 23:21:58 -08001094 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095
1096 return 0;
1097}
1098
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001101 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001102
Iiro Valkonen7686b102011-02-02 23:21:58 -08001103 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104}
1105
Iiro Valkonen7686b102011-02-02 23:21:58 -08001106static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001107 const struct i2c_device_id *id)
1108{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001109 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001110 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111 struct input_dev *input_dev;
1112 int error;
1113
Iiro Valkonen919ed892011-02-15 13:36:52 -08001114 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001115 return -EINVAL;
1116
Iiro Valkonen7686b102011-02-02 23:21:58 -08001117 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001118 input_dev = input_allocate_device();
1119 if (!data || !input_dev) {
1120 dev_err(&client->dev, "Failed to allocate memory\n");
1121 error = -ENOMEM;
1122 goto err_free_mem;
1123 }
1124
Iiro Valkonen7686b102011-02-02 23:21:58 -08001125 input_dev->name = "Atmel maXTouch Touchscreen";
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001126 input_dev->id.bustype = BUS_I2C;
1127 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001128 input_dev->open = mxt_input_open;
1129 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001130
1131 __set_bit(EV_ABS, input_dev->evbit);
1132 __set_bit(EV_KEY, input_dev->evbit);
1133 __set_bit(BTN_TOUCH, input_dev->keybit);
1134
1135 /* For single touch */
1136 input_set_abs_params(input_dev, ABS_X,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001137 0, MXT_MAX_XC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001138 input_set_abs_params(input_dev, ABS_Y,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001139 0, MXT_MAX_YC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140
1141 /* For multi touch */
1142 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001143 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001144 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001145 0, MXT_MAX_XC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001147 0, MXT_MAX_YC, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001148
1149 input_set_drvdata(input_dev, data);
1150
1151 data->client = client;
1152 data->input_dev = input_dev;
Iiro Valkonen919ed892011-02-15 13:36:52 -08001153 data->pdata = pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001154 data->irq = client->irq;
1155
1156 i2c_set_clientdata(client, data);
1157
Iiro Valkonen7686b102011-02-02 23:21:58 -08001158 error = mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001159 if (error)
1160 goto err_free_object;
1161
Iiro Valkonen7686b102011-02-02 23:21:58 -08001162 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Iiro Valkonen919ed892011-02-15 13:36:52 -08001163 pdata->irqflags, client->dev.driver->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001164 if (error) {
1165 dev_err(&client->dev, "Failed to register interrupt\n");
1166 goto err_free_object;
1167 }
1168
1169 error = input_register_device(input_dev);
1170 if (error)
1171 goto err_free_irq;
1172
Iiro Valkonen7686b102011-02-02 23:21:58 -08001173 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001174 if (error)
1175 goto err_unregister_device;
1176
1177 return 0;
1178
1179err_unregister_device:
1180 input_unregister_device(input_dev);
1181 input_dev = NULL;
1182err_free_irq:
1183 free_irq(client->irq, data);
1184err_free_object:
1185 kfree(data->object_table);
1186err_free_mem:
1187 input_free_device(input_dev);
1188 kfree(data);
1189 return error;
1190}
1191
Iiro Valkonen7686b102011-02-02 23:21:58 -08001192static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001193{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001194 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001195
Iiro Valkonen7686b102011-02-02 23:21:58 -08001196 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001197 free_irq(data->irq, data);
1198 input_unregister_device(data->input_dev);
1199 kfree(data->object_table);
1200 kfree(data);
1201
1202 return 0;
1203}
1204
1205#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001206static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001207{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001208 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001209 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001210 struct input_dev *input_dev = data->input_dev;
1211
1212 mutex_lock(&input_dev->mutex);
1213
1214 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001215 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001216
1217 mutex_unlock(&input_dev->mutex);
1218
1219 return 0;
1220}
1221
Iiro Valkonen7686b102011-02-02 23:21:58 -08001222static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001223{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001224 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001225 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001226 struct input_dev *input_dev = data->input_dev;
1227
1228 /* Soft reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001229 mxt_write_object(data, MXT_GEN_COMMAND,
1230 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001231
Iiro Valkonen7686b102011-02-02 23:21:58 -08001232 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001233
1234 mutex_lock(&input_dev->mutex);
1235
1236 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001237 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001238
1239 mutex_unlock(&input_dev->mutex);
1240
1241 return 0;
1242}
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001243
Iiro Valkonen7686b102011-02-02 23:21:58 -08001244static const struct dev_pm_ops mxt_pm_ops = {
1245 .suspend = mxt_suspend,
1246 .resume = mxt_resume,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001247};
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001248#endif
1249
Iiro Valkonen7686b102011-02-02 23:21:58 -08001250static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001251 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001252 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001253 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001254 { }
1255};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001256MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001257
Iiro Valkonen7686b102011-02-02 23:21:58 -08001258static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001259 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001260 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001261 .owner = THIS_MODULE,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001262#ifdef CONFIG_PM
Iiro Valkonen7686b102011-02-02 23:21:58 -08001263 .pm = &mxt_pm_ops,
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001264#endif
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001266 .probe = mxt_probe,
1267 .remove = __devexit_p(mxt_remove),
1268 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269};
1270
Iiro Valkonen7686b102011-02-02 23:21:58 -08001271static int __init mxt_init(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001272{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001273 return i2c_add_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001274}
1275
Iiro Valkonen7686b102011-02-02 23:21:58 -08001276static void __exit mxt_exit(void)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001277{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001278 i2c_del_driver(&mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001279}
1280
Iiro Valkonen7686b102011-02-02 23:21:58 -08001281module_init(mxt_init);
1282module_exit(mxt_exit);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001283
1284/* Module information */
1285MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001286MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001287MODULE_LICENSE("GPL");