blob: 40af02c2611313f867ffdb4086ab9f332c140619 [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
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07005 * Copyright (C) 2012 Google, Inc.
6 *
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07007 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/module.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070017#include <linux/delay.h>
18#include <linux/firmware.h>
19#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080020#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070021#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070022#include <linux/interrupt.h>
23#include <linux/slab.h>
24
25/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080026#define MXT_VER_20 20
27#define MXT_VER_21 21
28#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070029
30/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080031#define MXT_APP_LOW 0x4a
32#define MXT_APP_HIGH 0x4b
33#define MXT_BOOT_LOW 0x24
34#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070035
36/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070038
39/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080040#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080041#define MXT_FAMILY_ID 0x00
42#define MXT_VARIANT_ID 0x01
43#define MXT_VERSION 0x02
44#define MXT_BUILD 0x03
45#define MXT_MATRIX_X_SIZE 0x04
46#define MXT_MATRIX_Y_SIZE 0x05
47#define MXT_OBJECT_NUM 0x06
48#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
Iiro Valkonen7686b102011-02-02 23:21:58 -080050#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070051
52/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070053#define MXT_DEBUG_DIAGNOSTIC_T37 37
54#define MXT_GEN_MESSAGE_T5 5
55#define MXT_GEN_COMMAND_T6 6
56#define MXT_GEN_POWER_T7 7
57#define MXT_GEN_ACQUIRE_T8 8
58#define MXT_GEN_DATASOURCE_T53 53
59#define MXT_TOUCH_MULTI_T9 9
60#define MXT_TOUCH_KEYARRAY_T15 15
61#define MXT_TOUCH_PROXIMITY_T23 23
62#define MXT_TOUCH_PROXKEY_T52 52
63#define MXT_PROCI_GRIPFACE_T20 20
64#define MXT_PROCG_NOISE_T22 22
65#define MXT_PROCI_ONETOUCH_T24 24
66#define MXT_PROCI_TWOTOUCH_T27 27
67#define MXT_PROCI_GRIP_T40 40
68#define MXT_PROCI_PALM_T41 41
69#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
70#define MXT_PROCI_STYLUS_T47 47
71#define MXT_PROCG_NOISESUPPRESSION_T48 48
72#define MXT_SPT_COMMSCONFIG_T18 18
73#define MXT_SPT_GPIOPWM_T19 19
74#define MXT_SPT_SELFTEST_T25 25
75#define MXT_SPT_CTECONFIG_T28 28
76#define MXT_SPT_USERDATA_T38 38
77#define MXT_SPT_DIGITIZER_T43 43
78#define MXT_SPT_MESSAGECOUNT_T44 44
79#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070080
Iiro Valkonen81c88a72011-07-04 03:08:25 -070081/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080082#define MXT_COMMAND_RESET 0
83#define MXT_COMMAND_BACKUPNV 1
84#define MXT_COMMAND_CALIBRATE 2
85#define MXT_COMMAND_REPORTALL 3
86#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070087
Iiro Valkonen81c88a72011-07-04 03:08:25 -070088/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080089#define MXT_POWER_IDLEACQINT 0
90#define MXT_POWER_ACTVACQINT 1
91#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070092
Iiro Valkonen81c88a72011-07-04 03:08:25 -070093/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080094#define MXT_ACQUIRE_CHRGTIME 0
95#define MXT_ACQUIRE_TCHDRIFT 2
96#define MXT_ACQUIRE_DRIFTST 3
97#define MXT_ACQUIRE_TCHAUTOCAL 4
98#define MXT_ACQUIRE_SYNC 5
99#define MXT_ACQUIRE_ATCHCALST 6
100#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700101
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700102/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800103#define MXT_TOUCH_CTRL 0
104#define MXT_TOUCH_XORIGIN 1
105#define MXT_TOUCH_YORIGIN 2
106#define MXT_TOUCH_XSIZE 3
107#define MXT_TOUCH_YSIZE 4
108#define MXT_TOUCH_BLEN 6
109#define MXT_TOUCH_TCHTHR 7
110#define MXT_TOUCH_TCHDI 8
111#define MXT_TOUCH_ORIENT 9
112#define MXT_TOUCH_MOVHYSTI 11
113#define MXT_TOUCH_MOVHYSTN 12
114#define MXT_TOUCH_NUMTOUCH 14
115#define MXT_TOUCH_MRGHYST 15
116#define MXT_TOUCH_MRGTHR 16
117#define MXT_TOUCH_AMPHYST 17
118#define MXT_TOUCH_XRANGE_LSB 18
119#define MXT_TOUCH_XRANGE_MSB 19
120#define MXT_TOUCH_YRANGE_LSB 20
121#define MXT_TOUCH_YRANGE_MSB 21
122#define MXT_TOUCH_XLOCLIP 22
123#define MXT_TOUCH_XHICLIP 23
124#define MXT_TOUCH_YLOCLIP 24
125#define MXT_TOUCH_YHICLIP 25
126#define MXT_TOUCH_XEDGECTRL 26
127#define MXT_TOUCH_XEDGEDIST 27
128#define MXT_TOUCH_YEDGECTRL 28
129#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700130#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700131
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700132/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800133#define MXT_GRIPFACE_CTRL 0
134#define MXT_GRIPFACE_XLOGRIP 1
135#define MXT_GRIPFACE_XHIGRIP 2
136#define MXT_GRIPFACE_YLOGRIP 3
137#define MXT_GRIPFACE_YHIGRIP 4
138#define MXT_GRIPFACE_MAXTCHS 5
139#define MXT_GRIPFACE_SZTHR1 7
140#define MXT_GRIPFACE_SZTHR2 8
141#define MXT_GRIPFACE_SHPTHR1 9
142#define MXT_GRIPFACE_SHPTHR2 10
143#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700144
Iiro Valkonen7686b102011-02-02 23:21:58 -0800145/* MXT_PROCI_NOISE field */
146#define MXT_NOISE_CTRL 0
147#define MXT_NOISE_OUTFLEN 1
148#define MXT_NOISE_GCAFUL_LSB 3
149#define MXT_NOISE_GCAFUL_MSB 4
150#define MXT_NOISE_GCAFLL_LSB 5
151#define MXT_NOISE_GCAFLL_MSB 6
152#define MXT_NOISE_ACTVGCAFVALID 7
153#define MXT_NOISE_NOISETHR 8
154#define MXT_NOISE_FREQHOPSCALE 10
155#define MXT_NOISE_FREQ0 11
156#define MXT_NOISE_FREQ1 12
157#define MXT_NOISE_FREQ2 13
158#define MXT_NOISE_FREQ3 14
159#define MXT_NOISE_FREQ4 15
160#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700161
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700162/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800163#define MXT_COMMS_CTRL 0
164#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700165
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700166/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800167#define MXT_CTE_CTRL 0
168#define MXT_CTE_CMD 1
169#define MXT_CTE_MODE 2
170#define MXT_CTE_IDLEGCAFDEPTH 3
171#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700172#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700173
Iiro Valkonen7686b102011-02-02 23:21:58 -0800174#define MXT_VOLTAGE_DEFAULT 2700000
175#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700176
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700177/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_BOOT_VALUE 0xa5
179#define MXT_BACKUP_VALUE 0x55
Linus Torvalds8343bce2013-03-09 10:31:01 -0800180#define MXT_BACKUP_TIME 50 /* msec */
181#define MXT_RESET_TIME 200 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700182
Iiro Valkonen7686b102011-02-02 23:21:58 -0800183#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700184
185/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800186#define MXT_UNLOCK_CMD_MSB 0xaa
187#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700188
189/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800190#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
191#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
192#define MXT_FRAME_CRC_CHECK 0x02
193#define MXT_FRAME_CRC_FAIL 0x03
194#define MXT_FRAME_CRC_PASS 0x04
195#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
196#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700197
198/* Touch status */
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800199#define MXT_UNGRIP (1 << 0)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_SUPPRESS (1 << 1)
201#define MXT_AMP (1 << 2)
202#define MXT_VECTOR (1 << 3)
203#define MXT_MOVE (1 << 4)
204#define MXT_RELEASE (1 << 5)
205#define MXT_PRESS (1 << 6)
206#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700207
Joonyoung Shim910d8052011-04-12 23:14:38 -0700208/* Touch orient bits */
209#define MXT_XY_SWITCH (1 << 0)
210#define MXT_X_INVERT (1 << 1)
211#define MXT_Y_INVERT (1 << 2)
212
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700213/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800214#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700215
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800216#define MXT_PIXELS_PER_MM 20
217
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219 u8 family_id;
220 u8 variant_id;
221 u8 version;
222 u8 build;
223 u8 matrix_xsize;
224 u8 matrix_ysize;
225 u8 object_num;
226};
227
Iiro Valkonen7686b102011-02-02 23:21:58 -0800228struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700229 u8 type;
230 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700231 u8 size_minus_one;
232 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800234} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235
Iiro Valkonen7686b102011-02-02 23:21:58 -0800236struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700237 u8 reportid;
238 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700239};
240
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700241/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800242struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700243 struct i2c_client *client;
244 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800245 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800246 const struct mxt_platform_data *pdata;
247 struct mxt_object *object_table;
248 struct mxt_info info;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800249
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700250 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700251 unsigned int max_x;
252 unsigned int max_y;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800253
254 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800255 u8 T6_reportid;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800256 u8 T9_reportid_min;
257 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800258 u8 T19_reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700259};
260
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700261static size_t mxt_obj_size(const struct mxt_object *obj)
262{
263 return obj->size_minus_one + 1;
264}
265
266static size_t mxt_obj_instances(const struct mxt_object *obj)
267{
268 return obj->instances_minus_one + 1;
269}
270
Iiro Valkonen7686b102011-02-02 23:21:58 -0800271static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700272{
273 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700274 case MXT_GEN_COMMAND_T6:
275 case MXT_GEN_POWER_T7:
276 case MXT_GEN_ACQUIRE_T8:
277 case MXT_GEN_DATASOURCE_T53:
278 case MXT_TOUCH_MULTI_T9:
279 case MXT_TOUCH_KEYARRAY_T15:
280 case MXT_TOUCH_PROXIMITY_T23:
281 case MXT_TOUCH_PROXKEY_T52:
282 case MXT_PROCI_GRIPFACE_T20:
283 case MXT_PROCG_NOISE_T22:
284 case MXT_PROCI_ONETOUCH_T24:
285 case MXT_PROCI_TWOTOUCH_T27:
286 case MXT_PROCI_GRIP_T40:
287 case MXT_PROCI_PALM_T41:
288 case MXT_PROCI_TOUCHSUPPRESSION_T42:
289 case MXT_PROCI_STYLUS_T47:
290 case MXT_PROCG_NOISESUPPRESSION_T48:
291 case MXT_SPT_COMMSCONFIG_T18:
292 case MXT_SPT_GPIOPWM_T19:
293 case MXT_SPT_SELFTEST_T25:
294 case MXT_SPT_CTECONFIG_T28:
295 case MXT_SPT_USERDATA_T38:
296 case MXT_SPT_DIGITIZER_T43:
297 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700298 return true;
299 default:
300 return false;
301 }
302}
303
Iiro Valkonen7686b102011-02-02 23:21:58 -0800304static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700305{
306 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700307 case MXT_GEN_COMMAND_T6:
308 case MXT_GEN_POWER_T7:
309 case MXT_GEN_ACQUIRE_T8:
310 case MXT_TOUCH_MULTI_T9:
311 case MXT_TOUCH_KEYARRAY_T15:
312 case MXT_TOUCH_PROXIMITY_T23:
313 case MXT_TOUCH_PROXKEY_T52:
314 case MXT_PROCI_GRIPFACE_T20:
315 case MXT_PROCG_NOISE_T22:
316 case MXT_PROCI_ONETOUCH_T24:
317 case MXT_PROCI_TWOTOUCH_T27:
318 case MXT_PROCI_GRIP_T40:
319 case MXT_PROCI_PALM_T41:
320 case MXT_PROCI_TOUCHSUPPRESSION_T42:
321 case MXT_PROCI_STYLUS_T47:
322 case MXT_PROCG_NOISESUPPRESSION_T48:
323 case MXT_SPT_COMMSCONFIG_T18:
324 case MXT_SPT_GPIOPWM_T19:
325 case MXT_SPT_SELFTEST_T25:
326 case MXT_SPT_CTECONFIG_T28:
327 case MXT_SPT_DIGITIZER_T43:
328 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700329 return true;
330 default:
331 return false;
332 }
333}
334
Iiro Valkonen7686b102011-02-02 23:21:58 -0800335static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700336 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700337{
Andy Shevchenkoeb007c82012-10-04 00:02:59 -0700338 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
339 message->reportid, 7, message->message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700340}
341
Iiro Valkonen7686b102011-02-02 23:21:58 -0800342static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700343 unsigned int state)
344{
345 u8 val;
346
347recheck:
348 if (i2c_master_recv(client, &val, 1) != 1) {
349 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
350 return -EIO;
351 }
352
353 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800354 case MXT_WAITING_BOOTLOAD_CMD:
355 case MXT_WAITING_FRAME_DATA:
356 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700357 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800358 case MXT_FRAME_CRC_PASS:
359 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700360 goto recheck;
361 break;
362 default:
363 return -EINVAL;
364 }
365
366 if (val != state) {
367 dev_err(&client->dev, "Unvalid bootloader mode state\n");
368 return -EINVAL;
369 }
370
371 return 0;
372}
373
Iiro Valkonen7686b102011-02-02 23:21:58 -0800374static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700375{
376 u8 buf[2];
377
Iiro Valkonen7686b102011-02-02 23:21:58 -0800378 buf[0] = MXT_UNLOCK_CMD_LSB;
379 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700380
381 if (i2c_master_send(client, buf, 2) != 2) {
382 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
383 return -EIO;
384 }
385
386 return 0;
387}
388
Iiro Valkonen7686b102011-02-02 23:21:58 -0800389static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700390 const u8 *data, unsigned int frame_size)
391{
392 if (i2c_master_send(client, data, frame_size) != frame_size) {
393 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
394 return -EIO;
395 }
396
397 return 0;
398}
399
Iiro Valkonen7686b102011-02-02 23:21:58 -0800400static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700401 u16 reg, u16 len, void *val)
402{
403 struct i2c_msg xfer[2];
404 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800405 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700406
407 buf[0] = reg & 0xff;
408 buf[1] = (reg >> 8) & 0xff;
409
410 /* Write register */
411 xfer[0].addr = client->addr;
412 xfer[0].flags = 0;
413 xfer[0].len = 2;
414 xfer[0].buf = buf;
415
416 /* Read data */
417 xfer[1].addr = client->addr;
418 xfer[1].flags = I2C_M_RD;
419 xfer[1].len = len;
420 xfer[1].buf = val;
421
Daniel Kurtz771733e2012-06-28 21:08:11 +0800422 ret = i2c_transfer(client->adapter, xfer, 2);
423 if (ret == 2) {
424 ret = 0;
425 } else {
426 if (ret >= 0)
427 ret = -EIO;
428 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
429 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430 }
431
Daniel Kurtz771733e2012-06-28 21:08:11 +0800432 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700433}
434
Iiro Valkonen7686b102011-02-02 23:21:58 -0800435static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700436{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800437 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700438}
439
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800440static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
441 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700442{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800443 u8 *buf;
444 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800445 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700446
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800447 count = len + 2;
448 buf = kmalloc(count, GFP_KERNEL);
449 if (!buf)
450 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700451
452 buf[0] = reg & 0xff;
453 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800454 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700455
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800456 ret = i2c_master_send(client, buf, count);
457 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800458 ret = 0;
459 } else {
460 if (ret >= 0)
461 ret = -EIO;
462 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
463 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700464 }
465
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800466 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800467 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700468}
469
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800470static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700471{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800472 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700473}
474
Iiro Valkonen7686b102011-02-02 23:21:58 -0800475static struct mxt_object *
476mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700477{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800478 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700479 int i;
480
481 for (i = 0; i < data->info.object_num; i++) {
482 object = data->object_table + i;
483 if (object->type == type)
484 return object;
485 }
486
487 dev_err(&data->client->dev, "Invalid object type\n");
488 return NULL;
489}
490
Iiro Valkonen7686b102011-02-02 23:21:58 -0800491static int mxt_read_message(struct mxt_data *data,
492 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700493{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800494 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495 u16 reg;
496
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700497 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498 if (!object)
499 return -EINVAL;
500
501 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800502 return __mxt_read_reg(data->client, reg,
503 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504}
505
Iiro Valkonen7686b102011-02-02 23:21:58 -0800506static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700507 u8 type, u8 offset, u8 val)
508{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800509 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700510 u16 reg;
511
Iiro Valkonen7686b102011-02-02 23:21:58 -0800512 object = mxt_get_object(data, type);
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700513 if (!object || offset >= mxt_obj_size(object))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514 return -EINVAL;
515
516 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800517 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700518}
519
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800520static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
521{
522 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700523 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800524 bool button;
525 int i;
526
527 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700528 for (i = 0; i < pdata->t19_num_keys; i++) {
529 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800530 continue;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700531 button = !(message->message[0] & (1 << i));
532 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800533 }
534}
535
Iiro Valkonen7686b102011-02-02 23:21:58 -0800536static void mxt_input_touchevent(struct mxt_data *data,
537 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700539 struct device *dev = &data->client->dev;
540 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800541 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542 int x;
543 int y;
544 int area;
Yufeng Shen28ac2932011-08-16 00:40:54 -0700545 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700546
Joonyoung Shim910d8052011-04-12 23:14:38 -0700547 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
548 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
549 if (data->max_x < 1024)
550 x = x >> 2;
551 if (data->max_y < 1024)
552 y = y >> 2;
553
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554 area = message->message[4];
Yufeng Shen28ac2932011-08-16 00:40:54 -0700555 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700556
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800557 dev_dbg(dev,
558 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
559 id,
560 (status & MXT_DETECT) ? 'D' : '.',
561 (status & MXT_PRESS) ? 'P' : '.',
562 (status & MXT_RELEASE) ? 'R' : '.',
563 (status & MXT_MOVE) ? 'M' : '.',
564 (status & MXT_VECTOR) ? 'V' : '.',
565 (status & MXT_AMP) ? 'A' : '.',
566 (status & MXT_SUPPRESS) ? 'S' : '.',
567 (status & MXT_UNGRIP) ? 'U' : '.',
568 x, y, area, pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800570 input_mt_slot(input_dev, id);
571 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
572 status & MXT_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700573
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800574 if (status & MXT_DETECT) {
575 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
576 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
577 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
578 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
579 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580}
581
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800582static unsigned mxt_extract_T6_csum(const u8 *csum)
583{
584 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
585}
586
Daniel Kurtz04a79182012-06-28 21:08:21 +0800587static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
588{
589 u8 id = msg->reportid;
590 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591}
592
Iiro Valkonen7686b102011-02-02 23:21:58 -0800593static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800595 struct mxt_data *data = dev_id;
596 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800597 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700598 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800600 bool update_input = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601
602 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800603 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700604 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700605 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 }
607
608 reportid = message.reportid;
609
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800610 if (reportid == data->T6_reportid) {
611 u8 status = payload[0];
612 unsigned csum = mxt_extract_T6_csum(&payload[1]);
613 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
614 status, csum);
615 } else if (mxt_is_T9_message(data, &message)) {
616 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800617 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800618 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800619 } else if (message.reportid == data->T19_reportid) {
620 mxt_input_button(data, &message);
621 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800622 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800624 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625 } while (reportid != 0xff);
626
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800627 if (update_input) {
628 input_mt_report_pointer_emulation(data->input_dev, false);
629 input_sync(data->input_dev);
630 }
631
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700632 return IRQ_HANDLED;
633}
634
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800637 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800638 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639 struct device *dev = &data->client->dev;
640 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800641 int i, size;
642 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643
Iiro Valkonen71749f52011-02-15 13:36:52 -0800644 if (!pdata->config) {
645 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
646 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700647 }
648
649 for (i = 0; i < data->info.object_num; i++) {
650 object = data->object_table + i;
651
Iiro Valkonen7686b102011-02-02 23:21:58 -0800652 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653 continue;
654
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700655 size = mxt_obj_size(object) * mxt_obj_instances(object);
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800656 if (index + size > pdata->config_length) {
657 dev_err(dev, "Not enough config data!\n");
658 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800659 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800660
661 ret = __mxt_write_reg(data->client, object->start_address,
662 size, &pdata->config[index]);
663 if (ret)
664 return ret;
665 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 }
667
668 return 0;
669}
670
Iiro Valkonen7686b102011-02-02 23:21:58 -0800671static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700672{
673 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800674 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675 int count = 10;
676 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677
678 /* Read dummy message to make high CHG pin */
679 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800680 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700681 if (error)
682 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800683 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700684
685 if (!count) {
686 dev_err(dev, "CHG pin isn't cleared\n");
687 return -EBUSY;
688 }
689
690 return 0;
691}
692
Iiro Valkonen7686b102011-02-02 23:21:58 -0800693static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700694{
695 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800696 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700697 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698
Daniel Kurtz23003a842012-06-28 21:08:14 +0800699 /* Read 7-byte info block starting at address 0 */
700 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701 if (error)
702 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700703
704 return 0;
705}
706
Iiro Valkonen7686b102011-02-02 23:21:58 -0800707static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800709 struct i2c_client *client = data->client;
710 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 int error;
712 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800713 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800715 table_size = data->info.object_num * sizeof(struct mxt_object);
716 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
717 data->object_table);
718 if (error)
719 return error;
720
721 /* Valid Report IDs start counting from 1 */
722 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700723 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800725 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700726
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800727 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728
729 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800730 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700731 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700732 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800733 max_id = reportid - 1;
734 } else {
735 min_id = 0;
736 max_id = 0;
737 }
738
739 dev_dbg(&data->client->dev,
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700740 "Type %2d Start %3d Size %3zd Instances %2zd ReportIDs %3u : %3u\n",
741 object->type, object->start_address,
742 mxt_obj_size(object), mxt_obj_instances(object),
743 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800744
745 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800746 case MXT_GEN_COMMAND_T6:
747 data->T6_reportid = min_id;
748 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800749 case MXT_TOUCH_MULTI_T9:
750 data->T9_reportid_min = min_id;
751 data->T9_reportid_max = max_id;
752 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800753 case MXT_SPT_GPIOPWM_T19:
754 data->T19_reportid = min_id;
755 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756 }
757 }
758
759 return 0;
760}
761
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800762static void mxt_free_object_table(struct mxt_data *data)
763{
764 kfree(data->object_table);
765 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800766 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800767 data->T9_reportid_min = 0;
768 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800769 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800770}
771
Iiro Valkonen7686b102011-02-02 23:21:58 -0800772static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700773{
774 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700776 int error;
777 u8 val;
778
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700780 if (error)
781 return error;
782
783 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800784 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700785 GFP_KERNEL);
786 if (!data->object_table) {
787 dev_err(&client->dev, "Failed to allocate memory\n");
788 return -ENOMEM;
789 }
790
791 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800792 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700793 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800794 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700795
796 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800797 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800799 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700800
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801 /* Backup to memory */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700802 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800803 MXT_COMMAND_BACKUPNV,
804 MXT_BACKUP_VALUE);
805 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700806
807 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700808 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800809 MXT_COMMAND_RESET, 1);
810 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811
812 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800813 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800815 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700816 info->matrix_xsize = val;
817
Iiro Valkonen7686b102011-02-02 23:21:58 -0800818 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700819 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800820 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700821 info->matrix_ysize = val;
822
823 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800824 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
825 info->family_id, info->variant_id, info->version >> 4,
826 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700827
828 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800829 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700830 info->matrix_xsize, info->matrix_ysize,
831 info->object_num);
832
833 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800834
835err_free_object_table:
836 mxt_free_object_table(data);
837 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700838}
839
Joonyoung Shim910d8052011-04-12 23:14:38 -0700840static void mxt_calc_resolution(struct mxt_data *data)
841{
842 unsigned int max_x = data->pdata->x_size - 1;
843 unsigned int max_y = data->pdata->y_size - 1;
844
845 if (data->pdata->orient & MXT_XY_SWITCH) {
846 data->max_x = max_y;
847 data->max_y = max_x;
848 } else {
849 data->max_x = max_x;
850 data->max_y = max_y;
851 }
852}
853
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +0800854/* Firmware Version is returned as Major.Minor.Build */
855static ssize_t mxt_fw_version_show(struct device *dev,
856 struct device_attribute *attr, char *buf)
857{
858 struct mxt_data *data = dev_get_drvdata(dev);
859 struct mxt_info *info = &data->info;
860 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
861 info->version >> 4, info->version & 0xf, info->build);
862}
863
864/* Hardware Version is returned as FamilyID.VariantID */
865static ssize_t mxt_hw_version_show(struct device *dev,
866 struct device_attribute *attr, char *buf)
867{
868 struct mxt_data *data = dev_get_drvdata(dev);
869 struct mxt_info *info = &data->info;
870 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
871 info->family_id, info->variant_id);
872}
873
Daniel Kurtz794eb672012-06-28 21:08:10 +0800874static ssize_t mxt_show_instance(char *buf, int count,
875 struct mxt_object *object, int instance,
876 const u8 *val)
877{
878 int i;
879
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700880 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800881 count += scnprintf(buf + count, PAGE_SIZE - count,
882 "Instance %u\n", instance);
883
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700884 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800885 count += scnprintf(buf + count, PAGE_SIZE - count,
886 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
887 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
888
889 return count;
890}
891
Iiro Valkonen7686b102011-02-02 23:21:58 -0800892static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893 struct device_attribute *attr, char *buf)
894{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800895 struct mxt_data *data = dev_get_drvdata(dev);
896 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700897 int count = 0;
898 int i, j;
899 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800900 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800902 /* Pre-allocate buffer large enough to hold max sized object. */
903 obuf = kmalloc(256, GFP_KERNEL);
904 if (!obuf)
905 return -ENOMEM;
906
907 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700908 for (i = 0; i < data->info.object_num; i++) {
909 object = data->object_table + i;
910
Daniel Kurtz91630952012-06-28 21:08:09 +0800911 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700912 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700913
Daniel Kurtz91630952012-06-28 21:08:09 +0800914 count += scnprintf(buf + count, PAGE_SIZE - count,
915 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700916
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700917 for (j = 0; j < mxt_obj_instances(object); j++) {
918 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +0800919 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800920
Daniel Kurtz794eb672012-06-28 21:08:10 +0800921 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700922 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800923 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700924
Daniel Kurtz794eb672012-06-28 21:08:10 +0800925 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700926 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700927 }
928
Daniel Kurtz794eb672012-06-28 21:08:10 +0800929done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800930 kfree(obuf);
931 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700932}
933
Iiro Valkonen7686b102011-02-02 23:21:58 -0800934static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700935{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800936 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937 struct i2c_client *client = data->client;
938 const struct firmware *fw = NULL;
939 unsigned int frame_size;
940 unsigned int pos = 0;
941 int ret;
942
943 ret = request_firmware(&fw, fn, dev);
944 if (ret) {
945 dev_err(dev, "Unable to open firmware %s\n", fn);
946 return ret;
947 }
948
949 /* Change to the bootloader mode */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700950 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800951 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
952 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953
954 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 if (client->addr == MXT_APP_LOW)
956 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700957 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800958 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700959
Iiro Valkonen7686b102011-02-02 23:21:58 -0800960 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700961 if (ret)
962 goto out;
963
964 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800965 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700966
967 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800968 ret = mxt_check_bootloader(client,
969 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700970 if (ret)
971 goto out;
972
973 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
974
975 /* We should add 2 at frame size as the the firmware data is not
976 * included the CRC bytes.
977 */
978 frame_size += 2;
979
980 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800981 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700982
Iiro Valkonen7686b102011-02-02 23:21:58 -0800983 ret = mxt_check_bootloader(client,
984 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700985 if (ret)
986 goto out;
987
988 pos += frame_size;
989
990 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
991 }
992
993out:
994 release_firmware(fw);
995
996 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800997 if (client->addr == MXT_BOOT_LOW)
998 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700999 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001000 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001001
1002 return ret;
1003}
1004
Iiro Valkonen7686b102011-02-02 23:21:58 -08001005static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006 struct device_attribute *attr,
1007 const char *buf, size_t count)
1008{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001009 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010 int error;
1011
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012 disable_irq(data->irq);
1013
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001015 if (error) {
1016 dev_err(dev, "The firmware update failed(%d)\n", error);
1017 count = error;
1018 } else {
1019 dev_dbg(dev, "The firmware update succeeded\n");
1020
1021 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001023
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001024 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025
Iiro Valkonen7686b102011-02-02 23:21:58 -08001026 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027 }
1028
1029 enable_irq(data->irq);
1030
Iiro Valkonen08960a02011-04-12 23:16:40 -07001031 error = mxt_make_highchg(data);
1032 if (error)
1033 return error;
1034
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035 return count;
1036}
1037
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001038static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1039static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001040static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1041static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042
Iiro Valkonen7686b102011-02-02 23:21:58 -08001043static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001044 &dev_attr_fw_version.attr,
1045 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001046 &dev_attr_object.attr,
1047 &dev_attr_update_fw.attr,
1048 NULL
1049};
1050
Iiro Valkonen7686b102011-02-02 23:21:58 -08001051static const struct attribute_group mxt_attr_group = {
1052 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001053};
1054
Iiro Valkonen7686b102011-02-02 23:21:58 -08001055static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001056{
1057 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001058 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001059 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001060}
1061
Iiro Valkonen7686b102011-02-02 23:21:58 -08001062static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001063{
1064 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001065 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001066 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001067}
1068
Iiro Valkonen7686b102011-02-02 23:21:58 -08001069static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001070{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001071 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074
1075 return 0;
1076}
1077
Iiro Valkonen7686b102011-02-02 23:21:58 -08001078static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001079{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001080 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001081
Iiro Valkonen7686b102011-02-02 23:21:58 -08001082 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083}
1084
Bill Pemberton5298cc42012-11-23 21:38:25 -08001085static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001086 const struct i2c_device_id *id)
1087{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001088 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001089 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001090 struct input_dev *input_dev;
1091 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001092 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001093 unsigned int mt_flags = 0;
1094 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095
Iiro Valkonen919ed892011-02-15 13:36:52 -08001096 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097 return -EINVAL;
1098
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100 input_dev = input_allocate_device();
1101 if (!data || !input_dev) {
1102 dev_err(&client->dev, "Failed to allocate memory\n");
1103 error = -ENOMEM;
1104 goto err_free_mem;
1105 }
1106
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001107 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001108 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1109 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001110
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001111 input_dev->phys = data->phys;
1112
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001113 input_dev->id.bustype = BUS_I2C;
1114 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001115 input_dev->open = mxt_input_open;
1116 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001117
Joonyoung Shim910d8052011-04-12 23:14:38 -07001118 data->client = client;
1119 data->input_dev = input_dev;
1120 data->pdata = pdata;
1121 data->irq = client->irq;
1122
1123 mxt_calc_resolution(data);
1124
Daniel Kurtzcb159112012-06-28 21:08:22 +08001125 error = mxt_initialize(data);
1126 if (error)
1127 goto err_free_mem;
1128
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129 __set_bit(EV_ABS, input_dev->evbit);
1130 __set_bit(EV_KEY, input_dev->evbit);
1131 __set_bit(BTN_TOUCH, input_dev->keybit);
1132
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001133 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001134 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1135
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001136 for (i = 0; i < pdata->t19_num_keys; i++)
1137 if (pdata->t19_keymap[i] != KEY_RESERVED)
1138 input_set_capability(input_dev, EV_KEY,
1139 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001140
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001141 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001142
1143 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1144 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1145 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1146 MXT_PIXELS_PER_MM);
1147 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1148 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001149
1150 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001151 }
1152
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001153 /* For single touch */
1154 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001155 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001156 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001157 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001158 input_set_abs_params(input_dev, ABS_PRESSURE,
1159 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001160
1161 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001162 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001163 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001164 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001165 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001167 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001169 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001170 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001171 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001172 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1173 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001174
1175 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176 i2c_set_clientdata(client, data);
1177
Iiro Valkonen7686b102011-02-02 23:21:58 -08001178 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001179 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001180 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001181 if (error) {
1182 dev_err(&client->dev, "Failed to register interrupt\n");
1183 goto err_free_object;
1184 }
1185
Iiro Valkonen08960a02011-04-12 23:16:40 -07001186 error = mxt_make_highchg(data);
1187 if (error)
1188 goto err_free_irq;
1189
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001190 error = input_register_device(input_dev);
1191 if (error)
1192 goto err_free_irq;
1193
Iiro Valkonen7686b102011-02-02 23:21:58 -08001194 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001195 if (error)
1196 goto err_unregister_device;
1197
1198 return 0;
1199
1200err_unregister_device:
1201 input_unregister_device(input_dev);
1202 input_dev = NULL;
1203err_free_irq:
1204 free_irq(client->irq, data);
1205err_free_object:
1206 kfree(data->object_table);
1207err_free_mem:
1208 input_free_device(input_dev);
1209 kfree(data);
1210 return error;
1211}
1212
Bill Pembertone2619cf2012-11-23 21:50:47 -08001213static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001214{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001215 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001216
Iiro Valkonen7686b102011-02-02 23:21:58 -08001217 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001218 free_irq(data->irq, data);
1219 input_unregister_device(data->input_dev);
1220 kfree(data->object_table);
1221 kfree(data);
1222
1223 return 0;
1224}
1225
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001226#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001227static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001228{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001229 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001230 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001231 struct input_dev *input_dev = data->input_dev;
1232
1233 mutex_lock(&input_dev->mutex);
1234
1235 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001236 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001237
1238 mutex_unlock(&input_dev->mutex);
1239
1240 return 0;
1241}
1242
Iiro Valkonen7686b102011-02-02 23:21:58 -08001243static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001244{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001245 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001246 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247 struct input_dev *input_dev = data->input_dev;
1248
1249 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001250 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001251 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001252
Iiro Valkonen7686b102011-02-02 23:21:58 -08001253 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001254
1255 mutex_lock(&input_dev->mutex);
1256
1257 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001258 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001259
1260 mutex_unlock(&input_dev->mutex);
1261
1262 return 0;
1263}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001264#endif
1265
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001266static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1267
Iiro Valkonen7686b102011-02-02 23:21:58 -08001268static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001270 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001271 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001272 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273 { }
1274};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001275MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001276
Iiro Valkonen7686b102011-02-02 23:21:58 -08001277static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001278 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001279 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001281 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001282 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001283 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001284 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001285 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001286};
1287
Axel Lin1b92c1c2012-03-16 23:05:41 -07001288module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001289
1290/* Module information */
1291MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001292MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001293MODULE_LICENSE("GPL");