blob: e92615d0b1b0330b334b1a54a603354fdc3a8960 [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/firmware.h>
18#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080019#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070020#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070021#include <linux/interrupt.h>
22#include <linux/slab.h>
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 */
Daniel Kurtz23003a842012-06-28 21:08:14 +080039#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080040#define MXT_FAMILY_ID 0x00
41#define MXT_VARIANT_ID 0x01
42#define MXT_VERSION 0x02
43#define MXT_BUILD 0x03
44#define MXT_MATRIX_X_SIZE 0x04
45#define MXT_MATRIX_Y_SIZE 0x05
46#define MXT_OBJECT_NUM 0x06
47#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070048
Iiro Valkonen7686b102011-02-02 23:21:58 -080049#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070050
51/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070052#define MXT_DEBUG_DIAGNOSTIC_T37 37
53#define MXT_GEN_MESSAGE_T5 5
54#define MXT_GEN_COMMAND_T6 6
55#define MXT_GEN_POWER_T7 7
56#define MXT_GEN_ACQUIRE_T8 8
57#define MXT_GEN_DATASOURCE_T53 53
58#define MXT_TOUCH_MULTI_T9 9
59#define MXT_TOUCH_KEYARRAY_T15 15
60#define MXT_TOUCH_PROXIMITY_T23 23
61#define MXT_TOUCH_PROXKEY_T52 52
62#define MXT_PROCI_GRIPFACE_T20 20
63#define MXT_PROCG_NOISE_T22 22
64#define MXT_PROCI_ONETOUCH_T24 24
65#define MXT_PROCI_TWOTOUCH_T27 27
66#define MXT_PROCI_GRIP_T40 40
67#define MXT_PROCI_PALM_T41 41
68#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
69#define MXT_PROCI_STYLUS_T47 47
70#define MXT_PROCG_NOISESUPPRESSION_T48 48
71#define MXT_SPT_COMMSCONFIG_T18 18
72#define MXT_SPT_GPIOPWM_T19 19
73#define MXT_SPT_SELFTEST_T25 25
74#define MXT_SPT_CTECONFIG_T28 28
75#define MXT_SPT_USERDATA_T38 38
76#define MXT_SPT_DIGITIZER_T43 43
77#define MXT_SPT_MESSAGECOUNT_T44 44
78#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070079
Iiro Valkonen81c88a72011-07-04 03:08:25 -070080/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080081#define MXT_COMMAND_RESET 0
82#define MXT_COMMAND_BACKUPNV 1
83#define MXT_COMMAND_CALIBRATE 2
84#define MXT_COMMAND_REPORTALL 3
85#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070086
Iiro Valkonen81c88a72011-07-04 03:08:25 -070087/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080088#define MXT_POWER_IDLEACQINT 0
89#define MXT_POWER_ACTVACQINT 1
90#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen81c88a72011-07-04 03:08:25 -070092/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080093#define MXT_ACQUIRE_CHRGTIME 0
94#define MXT_ACQUIRE_TCHDRIFT 2
95#define MXT_ACQUIRE_DRIFTST 3
96#define MXT_ACQUIRE_TCHAUTOCAL 4
97#define MXT_ACQUIRE_SYNC 5
98#define MXT_ACQUIRE_ATCHCALST 6
99#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700100
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700101/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800102#define MXT_TOUCH_CTRL 0
103#define MXT_TOUCH_XORIGIN 1
104#define MXT_TOUCH_YORIGIN 2
105#define MXT_TOUCH_XSIZE 3
106#define MXT_TOUCH_YSIZE 4
107#define MXT_TOUCH_BLEN 6
108#define MXT_TOUCH_TCHTHR 7
109#define MXT_TOUCH_TCHDI 8
110#define MXT_TOUCH_ORIENT 9
111#define MXT_TOUCH_MOVHYSTI 11
112#define MXT_TOUCH_MOVHYSTN 12
113#define MXT_TOUCH_NUMTOUCH 14
114#define MXT_TOUCH_MRGHYST 15
115#define MXT_TOUCH_MRGTHR 16
116#define MXT_TOUCH_AMPHYST 17
117#define MXT_TOUCH_XRANGE_LSB 18
118#define MXT_TOUCH_XRANGE_MSB 19
119#define MXT_TOUCH_YRANGE_LSB 20
120#define MXT_TOUCH_YRANGE_MSB 21
121#define MXT_TOUCH_XLOCLIP 22
122#define MXT_TOUCH_XHICLIP 23
123#define MXT_TOUCH_YLOCLIP 24
124#define MXT_TOUCH_YHICLIP 25
125#define MXT_TOUCH_XEDGECTRL 26
126#define MXT_TOUCH_XEDGEDIST 27
127#define MXT_TOUCH_YEDGECTRL 28
128#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700129#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700130
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700131/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800132#define MXT_GRIPFACE_CTRL 0
133#define MXT_GRIPFACE_XLOGRIP 1
134#define MXT_GRIPFACE_XHIGRIP 2
135#define MXT_GRIPFACE_YLOGRIP 3
136#define MXT_GRIPFACE_YHIGRIP 4
137#define MXT_GRIPFACE_MAXTCHS 5
138#define MXT_GRIPFACE_SZTHR1 7
139#define MXT_GRIPFACE_SZTHR2 8
140#define MXT_GRIPFACE_SHPTHR1 9
141#define MXT_GRIPFACE_SHPTHR2 10
142#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700143
Iiro Valkonen7686b102011-02-02 23:21:58 -0800144/* MXT_PROCI_NOISE field */
145#define MXT_NOISE_CTRL 0
146#define MXT_NOISE_OUTFLEN 1
147#define MXT_NOISE_GCAFUL_LSB 3
148#define MXT_NOISE_GCAFUL_MSB 4
149#define MXT_NOISE_GCAFLL_LSB 5
150#define MXT_NOISE_GCAFLL_MSB 6
151#define MXT_NOISE_ACTVGCAFVALID 7
152#define MXT_NOISE_NOISETHR 8
153#define MXT_NOISE_FREQHOPSCALE 10
154#define MXT_NOISE_FREQ0 11
155#define MXT_NOISE_FREQ1 12
156#define MXT_NOISE_FREQ2 13
157#define MXT_NOISE_FREQ3 14
158#define MXT_NOISE_FREQ4 15
159#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700160
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700161/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800162#define MXT_COMMS_CTRL 0
163#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700165/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166#define MXT_CTE_CTRL 0
167#define MXT_CTE_CMD 1
168#define MXT_CTE_MODE 2
169#define MXT_CTE_IDLEGCAFDEPTH 3
170#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700171#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700172
Iiro Valkonen7686b102011-02-02 23:21:58 -0800173#define MXT_VOLTAGE_DEFAULT 2700000
174#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700175
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700176/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800177#define MXT_BOOT_VALUE 0xa5
178#define MXT_BACKUP_VALUE 0x55
179#define MXT_BACKUP_TIME 25 /* msec */
180#define MXT_RESET_TIME 65 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700181
Iiro Valkonen7686b102011-02-02 23:21:58 -0800182#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700183
184/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800185#define MXT_UNLOCK_CMD_MSB 0xaa
186#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700187
188/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800189#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
190#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
191#define MXT_FRAME_CRC_CHECK 0x02
192#define MXT_FRAME_CRC_FAIL 0x03
193#define MXT_FRAME_CRC_PASS 0x04
194#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
195#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700196
197/* Touch status */
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800198#define MXT_UNGRIP (1 << 0)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800199#define MXT_SUPPRESS (1 << 1)
200#define MXT_AMP (1 << 2)
201#define MXT_VECTOR (1 << 3)
202#define MXT_MOVE (1 << 4)
203#define MXT_RELEASE (1 << 5)
204#define MXT_PRESS (1 << 6)
205#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700206
Joonyoung Shim910d8052011-04-12 23:14:38 -0700207/* Touch orient bits */
208#define MXT_XY_SWITCH (1 << 0)
209#define MXT_X_INVERT (1 << 1)
210#define MXT_Y_INVERT (1 << 2)
211
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700212/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800213#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700214
Iiro Valkonen7686b102011-02-02 23:21:58 -0800215struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700216 u8 family_id;
217 u8 variant_id;
218 u8 version;
219 u8 build;
220 u8 matrix_xsize;
221 u8 matrix_ysize;
222 u8 object_num;
223};
224
Iiro Valkonen7686b102011-02-02 23:21:58 -0800225struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700226 u8 type;
227 u16 start_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800228 u8 size; /* Size of each instance - 1 */
229 u8 instances; /* Number of instances - 1 */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700230 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800231} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700232
Iiro Valkonen7686b102011-02-02 23:21:58 -0800233struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700234 u8 reportid;
235 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700236};
237
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700238/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800239struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700240 struct i2c_client *client;
241 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800242 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800243 const struct mxt_platform_data *pdata;
244 struct mxt_object *object_table;
245 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700246 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700247 unsigned int max_x;
248 unsigned int max_y;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800249
250 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800251 u8 T6_reportid;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800252 u8 T9_reportid_min;
253 u8 T9_reportid_max;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700254};
255
Iiro Valkonen7686b102011-02-02 23:21:58 -0800256static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700257{
258 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700259 case MXT_GEN_COMMAND_T6:
260 case MXT_GEN_POWER_T7:
261 case MXT_GEN_ACQUIRE_T8:
262 case MXT_GEN_DATASOURCE_T53:
263 case MXT_TOUCH_MULTI_T9:
264 case MXT_TOUCH_KEYARRAY_T15:
265 case MXT_TOUCH_PROXIMITY_T23:
266 case MXT_TOUCH_PROXKEY_T52:
267 case MXT_PROCI_GRIPFACE_T20:
268 case MXT_PROCG_NOISE_T22:
269 case MXT_PROCI_ONETOUCH_T24:
270 case MXT_PROCI_TWOTOUCH_T27:
271 case MXT_PROCI_GRIP_T40:
272 case MXT_PROCI_PALM_T41:
273 case MXT_PROCI_TOUCHSUPPRESSION_T42:
274 case MXT_PROCI_STYLUS_T47:
275 case MXT_PROCG_NOISESUPPRESSION_T48:
276 case MXT_SPT_COMMSCONFIG_T18:
277 case MXT_SPT_GPIOPWM_T19:
278 case MXT_SPT_SELFTEST_T25:
279 case MXT_SPT_CTECONFIG_T28:
280 case MXT_SPT_USERDATA_T38:
281 case MXT_SPT_DIGITIZER_T43:
282 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700283 return true;
284 default:
285 return false;
286 }
287}
288
Iiro Valkonen7686b102011-02-02 23:21:58 -0800289static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700290{
291 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700292 case MXT_GEN_COMMAND_T6:
293 case MXT_GEN_POWER_T7:
294 case MXT_GEN_ACQUIRE_T8:
295 case MXT_TOUCH_MULTI_T9:
296 case MXT_TOUCH_KEYARRAY_T15:
297 case MXT_TOUCH_PROXIMITY_T23:
298 case MXT_TOUCH_PROXKEY_T52:
299 case MXT_PROCI_GRIPFACE_T20:
300 case MXT_PROCG_NOISE_T22:
301 case MXT_PROCI_ONETOUCH_T24:
302 case MXT_PROCI_TWOTOUCH_T27:
303 case MXT_PROCI_GRIP_T40:
304 case MXT_PROCI_PALM_T41:
305 case MXT_PROCI_TOUCHSUPPRESSION_T42:
306 case MXT_PROCI_STYLUS_T47:
307 case MXT_PROCG_NOISESUPPRESSION_T48:
308 case MXT_SPT_COMMSCONFIG_T18:
309 case MXT_SPT_GPIOPWM_T19:
310 case MXT_SPT_SELFTEST_T25:
311 case MXT_SPT_CTECONFIG_T28:
312 case MXT_SPT_DIGITIZER_T43:
313 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700314 return true;
315 default:
316 return false;
317 }
318}
319
Iiro Valkonen7686b102011-02-02 23:21:58 -0800320static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700321 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700322{
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700323 dev_dbg(dev, "reportid: %u\tmessage: %02x %02x %02x %02x %02x %02x %02x\n",
324 message->reportid, message->message[0], message->message[1],
325 message->message[2], message->message[3], message->message[4],
326 message->message[5], message->message[6]);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700327}
328
Iiro Valkonen7686b102011-02-02 23:21:58 -0800329static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700330 unsigned int state)
331{
332 u8 val;
333
334recheck:
335 if (i2c_master_recv(client, &val, 1) != 1) {
336 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
337 return -EIO;
338 }
339
340 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800341 case MXT_WAITING_BOOTLOAD_CMD:
342 case MXT_WAITING_FRAME_DATA:
343 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700344 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800345 case MXT_FRAME_CRC_PASS:
346 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700347 goto recheck;
348 break;
349 default:
350 return -EINVAL;
351 }
352
353 if (val != state) {
354 dev_err(&client->dev, "Unvalid bootloader mode state\n");
355 return -EINVAL;
356 }
357
358 return 0;
359}
360
Iiro Valkonen7686b102011-02-02 23:21:58 -0800361static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700362{
363 u8 buf[2];
364
Iiro Valkonen7686b102011-02-02 23:21:58 -0800365 buf[0] = MXT_UNLOCK_CMD_LSB;
366 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700367
368 if (i2c_master_send(client, buf, 2) != 2) {
369 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
370 return -EIO;
371 }
372
373 return 0;
374}
375
Iiro Valkonen7686b102011-02-02 23:21:58 -0800376static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700377 const u8 *data, unsigned int frame_size)
378{
379 if (i2c_master_send(client, data, frame_size) != frame_size) {
380 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
381 return -EIO;
382 }
383
384 return 0;
385}
386
Iiro Valkonen7686b102011-02-02 23:21:58 -0800387static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700388 u16 reg, u16 len, void *val)
389{
390 struct i2c_msg xfer[2];
391 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800392 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700393
394 buf[0] = reg & 0xff;
395 buf[1] = (reg >> 8) & 0xff;
396
397 /* Write register */
398 xfer[0].addr = client->addr;
399 xfer[0].flags = 0;
400 xfer[0].len = 2;
401 xfer[0].buf = buf;
402
403 /* Read data */
404 xfer[1].addr = client->addr;
405 xfer[1].flags = I2C_M_RD;
406 xfer[1].len = len;
407 xfer[1].buf = val;
408
Daniel Kurtz771733e2012-06-28 21:08:11 +0800409 ret = i2c_transfer(client->adapter, xfer, 2);
410 if (ret == 2) {
411 ret = 0;
412 } else {
413 if (ret >= 0)
414 ret = -EIO;
415 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
416 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700417 }
418
Daniel Kurtz771733e2012-06-28 21:08:11 +0800419 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700420}
421
Iiro Valkonen7686b102011-02-02 23:21:58 -0800422static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700423{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800424 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700425}
426
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800427static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
428 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700429{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800430 u8 *buf;
431 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800432 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700433
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800434 count = len + 2;
435 buf = kmalloc(count, GFP_KERNEL);
436 if (!buf)
437 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700438
439 buf[0] = reg & 0xff;
440 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800441 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700442
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800443 ret = i2c_master_send(client, buf, count);
444 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800445 ret = 0;
446 } else {
447 if (ret >= 0)
448 ret = -EIO;
449 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
450 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700451 }
452
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800453 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800454 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700455}
456
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800457static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700458{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800459 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700460}
461
Iiro Valkonen7686b102011-02-02 23:21:58 -0800462static struct mxt_object *
463mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700464{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800465 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700466 int i;
467
468 for (i = 0; i < data->info.object_num; i++) {
469 object = data->object_table + i;
470 if (object->type == type)
471 return object;
472 }
473
474 dev_err(&data->client->dev, "Invalid object type\n");
475 return NULL;
476}
477
Iiro Valkonen7686b102011-02-02 23:21:58 -0800478static int mxt_read_message(struct mxt_data *data,
479 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700480{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800481 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700482 u16 reg;
483
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700484 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485 if (!object)
486 return -EINVAL;
487
488 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800489 return __mxt_read_reg(data->client, reg,
490 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700491}
492
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700494 u8 type, u8 offset, u8 val)
495{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800496 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497 u16 reg;
498
Iiro Valkonen7686b102011-02-02 23:21:58 -0800499 object = mxt_get_object(data, type);
Daniel Kurtzd1ff3202012-05-08 22:38:52 -0700500 if (!object || offset >= object->size + 1)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700501 return -EINVAL;
502
503 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800504 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700505}
506
Iiro Valkonen7686b102011-02-02 23:21:58 -0800507static void mxt_input_touchevent(struct mxt_data *data,
508 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700509{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700510 struct device *dev = &data->client->dev;
511 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800512 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700513 int x;
514 int y;
515 int area;
Yufeng Shen28ac2932011-08-16 00:40:54 -0700516 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517
Joonyoung Shim910d8052011-04-12 23:14:38 -0700518 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
519 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
520 if (data->max_x < 1024)
521 x = x >> 2;
522 if (data->max_y < 1024)
523 y = y >> 2;
524
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700525 area = message->message[4];
Yufeng Shen28ac2932011-08-16 00:40:54 -0700526 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700527
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800528 dev_dbg(dev,
529 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
530 id,
531 (status & MXT_DETECT) ? 'D' : '.',
532 (status & MXT_PRESS) ? 'P' : '.',
533 (status & MXT_RELEASE) ? 'R' : '.',
534 (status & MXT_MOVE) ? 'M' : '.',
535 (status & MXT_VECTOR) ? 'V' : '.',
536 (status & MXT_AMP) ? 'A' : '.',
537 (status & MXT_SUPPRESS) ? 'S' : '.',
538 (status & MXT_UNGRIP) ? 'U' : '.',
539 x, y, area, pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700540
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800541 input_mt_slot(input_dev, id);
542 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
543 status & MXT_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800545 if (status & MXT_DETECT) {
546 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
547 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
548 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
549 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
550 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700551}
552
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800553static unsigned mxt_extract_T6_csum(const u8 *csum)
554{
555 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
556}
557
Daniel Kurtz04a79182012-06-28 21:08:21 +0800558static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
559{
560 u8 id = msg->reportid;
561 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700562}
563
Iiro Valkonen7686b102011-02-02 23:21:58 -0800564static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700565{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800566 struct mxt_data *data = dev_id;
567 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800568 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700570 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800571 bool update_input = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572
573 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800574 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700575 dev_err(dev, "Failed to read message\n");
576 goto end;
577 }
578
579 reportid = message.reportid;
580
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800581 if (reportid == data->T6_reportid) {
582 u8 status = payload[0];
583 unsigned csum = mxt_extract_T6_csum(&payload[1]);
584 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
585 status, csum);
586 } else if (mxt_is_T9_message(data, &message)) {
587 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800588 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800589 update_input = true;
590 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800591 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800592 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 } while (reportid != 0xff);
594
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800595 if (update_input) {
596 input_mt_report_pointer_emulation(data->input_dev, false);
597 input_sync(data->input_dev);
598 }
599
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600end:
601 return IRQ_HANDLED;
602}
603
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700605{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800606 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800607 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700608 struct device *dev = &data->client->dev;
609 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800610 int i, size;
611 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700612
Iiro Valkonen71749f52011-02-15 13:36:52 -0800613 if (!pdata->config) {
614 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
615 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616 }
617
618 for (i = 0; i < data->info.object_num; i++) {
619 object = data->object_table + i;
620
Iiro Valkonen7686b102011-02-02 23:21:58 -0800621 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700622 continue;
623
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800624 size = (object->size + 1) * (object->instances + 1);
625 if (index + size > pdata->config_length) {
626 dev_err(dev, "Not enough config data!\n");
627 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800628 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800629
630 ret = __mxt_write_reg(data->client, object->start_address,
631 size, &pdata->config[index]);
632 if (ret)
633 return ret;
634 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635 }
636
637 return 0;
638}
639
Iiro Valkonen7686b102011-02-02 23:21:58 -0800640static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641{
642 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800643 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700644 int count = 10;
645 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646
647 /* Read dummy message to make high CHG pin */
648 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800649 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700650 if (error)
651 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800652 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653
654 if (!count) {
655 dev_err(dev, "CHG pin isn't cleared\n");
656 return -EBUSY;
657 }
658
659 return 0;
660}
661
Iiro Valkonen7686b102011-02-02 23:21:58 -0800662static void mxt_handle_pdata(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700663{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800664 const struct mxt_platform_data *pdata = data->pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665 u8 voltage;
666
667 /* Set touchscreen lines */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700668 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_XSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669 pdata->x_line);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700670 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_YSIZE,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700671 pdata->y_line);
672
673 /* Set touchscreen orient */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700674 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_ORIENT,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675 pdata->orient);
676
677 /* Set touchscreen burst length */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700678 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800679 MXT_TOUCH_BLEN, pdata->blen);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700680
681 /* Set touchscreen threshold */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700682 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800683 MXT_TOUCH_TCHTHR, pdata->threshold);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684
685 /* Set touchscreen resolution */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700686 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800687 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700688 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800689 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700690 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800691 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700692 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800693 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700694
695 /* Set touchscreen voltage */
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700696 if (pdata->voltage) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800697 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
698 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
699 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700 voltage = 0xff - voltage + 1;
701 } else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800702 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
703 MXT_VOLTAGE_STEP;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700704
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700705 mxt_write_object(data, MXT_SPT_CTECONFIG_T28,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800706 MXT_CTE_VOLTAGE, voltage);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700707 }
708}
709
Iiro Valkonen7686b102011-02-02 23:21:58 -0800710static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711{
712 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800713 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715
Daniel Kurtz23003a842012-06-28 21:08:14 +0800716 /* Read 7-byte info block starting at address 0 */
717 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718 if (error)
719 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720
721 return 0;
722}
723
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800726 struct i2c_client *client = data->client;
727 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728 int error;
729 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800730 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800732 table_size = data->info.object_num * sizeof(struct mxt_object);
733 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
734 data->object_table);
735 if (error)
736 return error;
737
738 /* Valid Report IDs start counting from 1 */
739 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700740 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800741 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800742 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800744 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700745
746 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800747 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748 reportid += object->num_report_ids *
749 (object->instances + 1);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800750 max_id = reportid - 1;
751 } else {
752 min_id = 0;
753 max_id = 0;
754 }
755
756 dev_dbg(&data->client->dev,
757 "Type %2d Start %3d Size %3d Instances %2d ReportIDs %3u : %3u\n",
758 object->type, object->start_address, object->size + 1,
759 object->instances + 1, min_id, max_id);
760
761 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800762 case MXT_GEN_COMMAND_T6:
763 data->T6_reportid = min_id;
764 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800765 case MXT_TOUCH_MULTI_T9:
766 data->T9_reportid_min = min_id;
767 data->T9_reportid_max = max_id;
768 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700769 }
770 }
771
772 return 0;
773}
774
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800775static void mxt_free_object_table(struct mxt_data *data)
776{
777 kfree(data->object_table);
778 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800779 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800780 data->T9_reportid_min = 0;
781 data->T9_reportid_max = 0;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800782
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800783}
784
Iiro Valkonen7686b102011-02-02 23:21:58 -0800785static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786{
787 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800788 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789 int error;
790 u8 val;
791
Iiro Valkonen7686b102011-02-02 23:21:58 -0800792 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700793 if (error)
794 return error;
795
796 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800797 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798 GFP_KERNEL);
799 if (!data->object_table) {
800 dev_err(&client->dev, "Failed to allocate memory\n");
801 return -ENOMEM;
802 }
803
804 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800805 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700806 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800807 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700808
809 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800810 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800812 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700813
Iiro Valkonen7686b102011-02-02 23:21:58 -0800814 mxt_handle_pdata(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700815
816 /* Backup to memory */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700817 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800818 MXT_COMMAND_BACKUPNV,
819 MXT_BACKUP_VALUE);
820 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700821
822 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700823 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800824 MXT_COMMAND_RESET, 1);
825 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700826
827 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800828 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700829 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800830 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831 info->matrix_xsize = val;
832
Iiro Valkonen7686b102011-02-02 23:21:58 -0800833 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700834 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800835 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700836 info->matrix_ysize = val;
837
838 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800839 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
840 info->family_id, info->variant_id, info->version >> 4,
841 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842
843 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800844 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845 info->matrix_xsize, info->matrix_ysize,
846 info->object_num);
847
848 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800849
850err_free_object_table:
851 mxt_free_object_table(data);
852 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700853}
854
Joonyoung Shim910d8052011-04-12 23:14:38 -0700855static void mxt_calc_resolution(struct mxt_data *data)
856{
857 unsigned int max_x = data->pdata->x_size - 1;
858 unsigned int max_y = data->pdata->y_size - 1;
859
860 if (data->pdata->orient & MXT_XY_SWITCH) {
861 data->max_x = max_y;
862 data->max_y = max_x;
863 } else {
864 data->max_x = max_x;
865 data->max_y = max_y;
866 }
867}
868
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +0800869/* Firmware Version is returned as Major.Minor.Build */
870static ssize_t mxt_fw_version_show(struct device *dev,
871 struct device_attribute *attr, char *buf)
872{
873 struct mxt_data *data = dev_get_drvdata(dev);
874 struct mxt_info *info = &data->info;
875 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
876 info->version >> 4, info->version & 0xf, info->build);
877}
878
879/* Hardware Version is returned as FamilyID.VariantID */
880static ssize_t mxt_hw_version_show(struct device *dev,
881 struct device_attribute *attr, char *buf)
882{
883 struct mxt_data *data = dev_get_drvdata(dev);
884 struct mxt_info *info = &data->info;
885 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
886 info->family_id, info->variant_id);
887}
888
Daniel Kurtz794eb672012-06-28 21:08:10 +0800889static ssize_t mxt_show_instance(char *buf, int count,
890 struct mxt_object *object, int instance,
891 const u8 *val)
892{
893 int i;
894
895 if (object->instances > 0)
896 count += scnprintf(buf + count, PAGE_SIZE - count,
897 "Instance %u\n", instance);
898
899 for (i = 0; i < object->size + 1; i++)
900 count += scnprintf(buf + count, PAGE_SIZE - count,
901 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
902 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
903
904 return count;
905}
906
Iiro Valkonen7686b102011-02-02 23:21:58 -0800907static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700908 struct device_attribute *attr, char *buf)
909{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800910 struct mxt_data *data = dev_get_drvdata(dev);
911 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700912 int count = 0;
913 int i, j;
914 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800915 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700916
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800917 /* Pre-allocate buffer large enough to hold max sized object. */
918 obuf = kmalloc(256, GFP_KERNEL);
919 if (!obuf)
920 return -ENOMEM;
921
922 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700923 for (i = 0; i < data->info.object_num; i++) {
924 object = data->object_table + i;
925
Daniel Kurtz91630952012-06-28 21:08:09 +0800926 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700927 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700928
Daniel Kurtz91630952012-06-28 21:08:09 +0800929 count += scnprintf(buf + count, PAGE_SIZE - count,
930 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931
Daniel Kurtz794eb672012-06-28 21:08:10 +0800932 for (j = 0; j < object->instances + 1; j++) {
933 u16 size = object->size + 1;
934 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800935
Daniel Kurtz794eb672012-06-28 21:08:10 +0800936 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800938 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700939
Daniel Kurtz794eb672012-06-28 21:08:10 +0800940 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700941 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700942 }
943
Daniel Kurtz794eb672012-06-28 21:08:10 +0800944done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800945 kfree(obuf);
946 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700947}
948
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800951 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700952 struct i2c_client *client = data->client;
953 const struct firmware *fw = NULL;
954 unsigned int frame_size;
955 unsigned int pos = 0;
956 int ret;
957
958 ret = request_firmware(&fw, fn, dev);
959 if (ret) {
960 dev_err(dev, "Unable to open firmware %s\n", fn);
961 return ret;
962 }
963
964 /* Change to the bootloader mode */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700965 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800966 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
967 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700968
969 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800970 if (client->addr == MXT_APP_LOW)
971 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800973 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700974
Iiro Valkonen7686b102011-02-02 23:21:58 -0800975 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700976 if (ret)
977 goto out;
978
979 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800980 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700981
982 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800983 ret = mxt_check_bootloader(client,
984 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700985 if (ret)
986 goto out;
987
988 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
989
990 /* We should add 2 at frame size as the the firmware data is not
991 * included the CRC bytes.
992 */
993 frame_size += 2;
994
995 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800996 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997
Iiro Valkonen7686b102011-02-02 23:21:58 -0800998 ret = mxt_check_bootloader(client,
999 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001000 if (ret)
1001 goto out;
1002
1003 pos += frame_size;
1004
1005 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1006 }
1007
1008out:
1009 release_firmware(fw);
1010
1011 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001012 if (client->addr == MXT_BOOT_LOW)
1013 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001014 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001015 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016
1017 return ret;
1018}
1019
Iiro Valkonen7686b102011-02-02 23:21:58 -08001020static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001021 struct device_attribute *attr,
1022 const char *buf, size_t count)
1023{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001024 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025 int error;
1026
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027 disable_irq(data->irq);
1028
Iiro Valkonen7686b102011-02-02 23:21:58 -08001029 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001030 if (error) {
1031 dev_err(dev, "The firmware update failed(%d)\n", error);
1032 count = error;
1033 } else {
1034 dev_dbg(dev, "The firmware update succeeded\n");
1035
1036 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001037 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001039 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040
Iiro Valkonen7686b102011-02-02 23:21:58 -08001041 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042 }
1043
1044 enable_irq(data->irq);
1045
Iiro Valkonen08960a02011-04-12 23:16:40 -07001046 error = mxt_make_highchg(data);
1047 if (error)
1048 return error;
1049
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050 return count;
1051}
1052
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001053static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1054static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001055static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1056static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001057
Iiro Valkonen7686b102011-02-02 23:21:58 -08001058static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001059 &dev_attr_fw_version.attr,
1060 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061 &dev_attr_object.attr,
1062 &dev_attr_update_fw.attr,
1063 NULL
1064};
1065
Iiro Valkonen7686b102011-02-02 23:21:58 -08001066static const struct attribute_group mxt_attr_group = {
1067 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001068};
1069
Iiro Valkonen7686b102011-02-02 23:21:58 -08001070static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001071{
1072 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001074 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001075}
1076
Iiro Valkonen7686b102011-02-02 23:21:58 -08001077static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078{
1079 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001080 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001081 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001082}
1083
Iiro Valkonen7686b102011-02-02 23:21:58 -08001084static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001085{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001087
Iiro Valkonen7686b102011-02-02 23:21:58 -08001088 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001089
1090 return 0;
1091}
1092
Iiro Valkonen7686b102011-02-02 23:21:58 -08001093static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001094{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001095 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001096
Iiro Valkonen7686b102011-02-02 23:21:58 -08001097 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001098}
1099
Iiro Valkonen7686b102011-02-02 23:21:58 -08001100static int __devinit mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001101 const struct i2c_device_id *id)
1102{
Iiro Valkonen919ed892011-02-15 13:36:52 -08001103 const struct mxt_platform_data *pdata = client->dev.platform_data;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001104 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001105 struct input_dev *input_dev;
1106 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001107 unsigned int num_mt_slots;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001108
Iiro Valkonen919ed892011-02-15 13:36:52 -08001109 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001110 return -EINVAL;
1111
Iiro Valkonen7686b102011-02-02 23:21:58 -08001112 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001113 input_dev = input_allocate_device();
1114 if (!data || !input_dev) {
1115 dev_err(&client->dev, "Failed to allocate memory\n");
1116 error = -ENOMEM;
1117 goto err_free_mem;
1118 }
1119
Iiro Valkonen7686b102011-02-02 23:21:58 -08001120 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001121 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1122 client->adapter->nr, client->addr);
1123 input_dev->phys = data->phys;
1124
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001125 input_dev->id.bustype = BUS_I2C;
1126 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001127 input_dev->open = mxt_input_open;
1128 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129
Joonyoung Shim910d8052011-04-12 23:14:38 -07001130 data->client = client;
1131 data->input_dev = input_dev;
1132 data->pdata = pdata;
1133 data->irq = client->irq;
1134
1135 mxt_calc_resolution(data);
1136
Daniel Kurtzcb159112012-06-28 21:08:22 +08001137 error = mxt_initialize(data);
1138 if (error)
1139 goto err_free_mem;
1140
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001141 __set_bit(EV_ABS, input_dev->evbit);
1142 __set_bit(EV_KEY, input_dev->evbit);
1143 __set_bit(BTN_TOUCH, input_dev->keybit);
1144
1145 /* For single touch */
1146 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001147 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001148 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001149 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001150 input_set_abs_params(input_dev, ABS_PRESSURE,
1151 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001152
1153 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001154 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +02001155 error = input_mt_init_slots(input_dev, num_mt_slots, 0);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001156 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001157 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001158 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001159 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001160 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001161 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001162 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001163 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001164 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1165 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166
1167 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168 i2c_set_clientdata(client, data);
1169
Iiro Valkonen7686b102011-02-02 23:21:58 -08001170 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001171 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001172 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001173 if (error) {
1174 dev_err(&client->dev, "Failed to register interrupt\n");
1175 goto err_free_object;
1176 }
1177
Iiro Valkonen08960a02011-04-12 23:16:40 -07001178 error = mxt_make_highchg(data);
1179 if (error)
1180 goto err_free_irq;
1181
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001182 error = input_register_device(input_dev);
1183 if (error)
1184 goto err_free_irq;
1185
Iiro Valkonen7686b102011-02-02 23:21:58 -08001186 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001187 if (error)
1188 goto err_unregister_device;
1189
1190 return 0;
1191
1192err_unregister_device:
1193 input_unregister_device(input_dev);
1194 input_dev = NULL;
1195err_free_irq:
1196 free_irq(client->irq, data);
1197err_free_object:
1198 kfree(data->object_table);
1199err_free_mem:
1200 input_free_device(input_dev);
1201 kfree(data);
1202 return error;
1203}
1204
Iiro Valkonen7686b102011-02-02 23:21:58 -08001205static int __devexit mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001206{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001207 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001208
Iiro Valkonen7686b102011-02-02 23:21:58 -08001209 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001210 free_irq(data->irq, data);
1211 input_unregister_device(data->input_dev);
1212 kfree(data->object_table);
1213 kfree(data);
1214
1215 return 0;
1216}
1217
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001218#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001219static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001220{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001221 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001222 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001223 struct input_dev *input_dev = data->input_dev;
1224
1225 mutex_lock(&input_dev->mutex);
1226
1227 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001228 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001229
1230 mutex_unlock(&input_dev->mutex);
1231
1232 return 0;
1233}
1234
Iiro Valkonen7686b102011-02-02 23:21:58 -08001235static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001236{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001237 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001238 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001239 struct input_dev *input_dev = data->input_dev;
1240
1241 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001242 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001243 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001244
Iiro Valkonen7686b102011-02-02 23:21:58 -08001245 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001246
1247 mutex_lock(&input_dev->mutex);
1248
1249 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001250 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001251
1252 mutex_unlock(&input_dev->mutex);
1253
1254 return 0;
1255}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001256#endif
1257
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001258static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1259
Iiro Valkonen7686b102011-02-02 23:21:58 -08001260static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001261 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001262 { "atmel_mxt_ts", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001263 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001264 { }
1265};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001266MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001267
Iiro Valkonen7686b102011-02-02 23:21:58 -08001268static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001270 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001271 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001272 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001274 .probe = mxt_probe,
1275 .remove = __devexit_p(mxt_remove),
1276 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001277};
1278
Axel Lin1b92c1c2012-03-16 23:05:41 -07001279module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280
1281/* Module information */
1282MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001283MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001284MODULE_LICENSE("GPL");