blob: 61f9ef221d128ef6d66f8a1bd60d769d586a298a [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>
Benson Leungd79e7e42014-05-18 23:02:52 -070017#include <linux/init.h>
18#include <linux/completion.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070019#include <linux/delay.h>
20#include <linux/firmware.h>
21#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080022#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070023#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070024#include <linux/interrupt.h>
25#include <linux/slab.h>
26
27/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080028#define MXT_VER_20 20
29#define MXT_VER_21 21
30#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070031
32/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080033#define MXT_APP_LOW 0x4a
34#define MXT_APP_HIGH 0x4b
35#define MXT_BOOT_LOW 0x24
36#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070037
38/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080039#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070040
41/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080042#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080043#define MXT_FAMILY_ID 0x00
44#define MXT_VARIANT_ID 0x01
45#define MXT_VERSION 0x02
46#define MXT_BUILD 0x03
47#define MXT_MATRIX_X_SIZE 0x04
48#define MXT_MATRIX_Y_SIZE 0x05
49#define MXT_OBJECT_NUM 0x06
50#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070051
Iiro Valkonen7686b102011-02-02 23:21:58 -080052#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070053
54/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070055#define MXT_DEBUG_DIAGNOSTIC_T37 37
56#define MXT_GEN_MESSAGE_T5 5
57#define MXT_GEN_COMMAND_T6 6
58#define MXT_GEN_POWER_T7 7
59#define MXT_GEN_ACQUIRE_T8 8
60#define MXT_GEN_DATASOURCE_T53 53
61#define MXT_TOUCH_MULTI_T9 9
62#define MXT_TOUCH_KEYARRAY_T15 15
63#define MXT_TOUCH_PROXIMITY_T23 23
64#define MXT_TOUCH_PROXKEY_T52 52
65#define MXT_PROCI_GRIPFACE_T20 20
66#define MXT_PROCG_NOISE_T22 22
67#define MXT_PROCI_ONETOUCH_T24 24
68#define MXT_PROCI_TWOTOUCH_T27 27
69#define MXT_PROCI_GRIP_T40 40
70#define MXT_PROCI_PALM_T41 41
71#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
72#define MXT_PROCI_STYLUS_T47 47
73#define MXT_PROCG_NOISESUPPRESSION_T48 48
74#define MXT_SPT_COMMSCONFIG_T18 18
75#define MXT_SPT_GPIOPWM_T19 19
76#define MXT_SPT_SELFTEST_T25 25
77#define MXT_SPT_CTECONFIG_T28 28
78#define MXT_SPT_USERDATA_T38 38
79#define MXT_SPT_DIGITIZER_T43 43
80#define MXT_SPT_MESSAGECOUNT_T44 44
81#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070082
Iiro Valkonen81c88a72011-07-04 03:08:25 -070083/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080084#define MXT_COMMAND_RESET 0
85#define MXT_COMMAND_BACKUPNV 1
86#define MXT_COMMAND_CALIBRATE 2
87#define MXT_COMMAND_REPORTALL 3
88#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070089
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070090/* Define for T6 status byte */
91#define MXT_T6_STATUS_RESET (1 << 7)
92
Iiro Valkonen81c88a72011-07-04 03:08:25 -070093/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080094#define MXT_POWER_IDLEACQINT 0
95#define MXT_POWER_ACTVACQINT 1
96#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070097
Iiro Valkonen81c88a72011-07-04 03:08:25 -070098/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080099#define MXT_ACQUIRE_CHRGTIME 0
100#define MXT_ACQUIRE_TCHDRIFT 2
101#define MXT_ACQUIRE_DRIFTST 3
102#define MXT_ACQUIRE_TCHAUTOCAL 4
103#define MXT_ACQUIRE_SYNC 5
104#define MXT_ACQUIRE_ATCHCALST 6
105#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700106
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700107/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800108#define MXT_TOUCH_CTRL 0
109#define MXT_TOUCH_XORIGIN 1
110#define MXT_TOUCH_YORIGIN 2
111#define MXT_TOUCH_XSIZE 3
112#define MXT_TOUCH_YSIZE 4
113#define MXT_TOUCH_BLEN 6
114#define MXT_TOUCH_TCHTHR 7
115#define MXT_TOUCH_TCHDI 8
116#define MXT_TOUCH_ORIENT 9
117#define MXT_TOUCH_MOVHYSTI 11
118#define MXT_TOUCH_MOVHYSTN 12
119#define MXT_TOUCH_NUMTOUCH 14
120#define MXT_TOUCH_MRGHYST 15
121#define MXT_TOUCH_MRGTHR 16
122#define MXT_TOUCH_AMPHYST 17
123#define MXT_TOUCH_XRANGE_LSB 18
124#define MXT_TOUCH_XRANGE_MSB 19
125#define MXT_TOUCH_YRANGE_LSB 20
126#define MXT_TOUCH_YRANGE_MSB 21
127#define MXT_TOUCH_XLOCLIP 22
128#define MXT_TOUCH_XHICLIP 23
129#define MXT_TOUCH_YLOCLIP 24
130#define MXT_TOUCH_YHICLIP 25
131#define MXT_TOUCH_XEDGECTRL 26
132#define MXT_TOUCH_XEDGEDIST 27
133#define MXT_TOUCH_YEDGECTRL 28
134#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700135#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700136
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700137/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800138#define MXT_GRIPFACE_CTRL 0
139#define MXT_GRIPFACE_XLOGRIP 1
140#define MXT_GRIPFACE_XHIGRIP 2
141#define MXT_GRIPFACE_YLOGRIP 3
142#define MXT_GRIPFACE_YHIGRIP 4
143#define MXT_GRIPFACE_MAXTCHS 5
144#define MXT_GRIPFACE_SZTHR1 7
145#define MXT_GRIPFACE_SZTHR2 8
146#define MXT_GRIPFACE_SHPTHR1 9
147#define MXT_GRIPFACE_SHPTHR2 10
148#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700149
Iiro Valkonen7686b102011-02-02 23:21:58 -0800150/* MXT_PROCI_NOISE field */
151#define MXT_NOISE_CTRL 0
152#define MXT_NOISE_OUTFLEN 1
153#define MXT_NOISE_GCAFUL_LSB 3
154#define MXT_NOISE_GCAFUL_MSB 4
155#define MXT_NOISE_GCAFLL_LSB 5
156#define MXT_NOISE_GCAFLL_MSB 6
157#define MXT_NOISE_ACTVGCAFVALID 7
158#define MXT_NOISE_NOISETHR 8
159#define MXT_NOISE_FREQHOPSCALE 10
160#define MXT_NOISE_FREQ0 11
161#define MXT_NOISE_FREQ1 12
162#define MXT_NOISE_FREQ2 13
163#define MXT_NOISE_FREQ3 14
164#define MXT_NOISE_FREQ4 15
165#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700166
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700167/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800168#define MXT_COMMS_CTRL 0
169#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700170
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700171/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800172#define MXT_CTE_CTRL 0
173#define MXT_CTE_CMD 1
174#define MXT_CTE_MODE 2
175#define MXT_CTE_IDLEGCAFDEPTH 3
176#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700177#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700178
Iiro Valkonen7686b102011-02-02 23:21:58 -0800179#define MXT_VOLTAGE_DEFAULT 2700000
180#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700181
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700182/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800183#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700184#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800185#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700186
187/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800188#define MXT_BACKUP_TIME 50 /* msec */
189#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700190#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700191#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700192#define MXT_FW_RESET_TIME 3000 /* msec */
193#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700194
195/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800196#define MXT_UNLOCK_CMD_MSB 0xaa
197#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700198
199/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
201#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
202#define MXT_FRAME_CRC_CHECK 0x02
203#define MXT_FRAME_CRC_FAIL 0x03
204#define MXT_FRAME_CRC_PASS 0x04
205#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
206#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700207
208/* Touch status */
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800209#define MXT_UNGRIP (1 << 0)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800210#define MXT_SUPPRESS (1 << 1)
211#define MXT_AMP (1 << 2)
212#define MXT_VECTOR (1 << 3)
213#define MXT_MOVE (1 << 4)
214#define MXT_RELEASE (1 << 5)
215#define MXT_PRESS (1 << 6)
216#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217
Joonyoung Shim910d8052011-04-12 23:14:38 -0700218/* Touch orient bits */
219#define MXT_XY_SWITCH (1 << 0)
220#define MXT_X_INVERT (1 << 1)
221#define MXT_Y_INVERT (1 << 2)
222
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700223/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800224#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700225
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800226#define MXT_PIXELS_PER_MM 20
227
Iiro Valkonen7686b102011-02-02 23:21:58 -0800228struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700229 u8 family_id;
230 u8 variant_id;
231 u8 version;
232 u8 build;
233 u8 matrix_xsize;
234 u8 matrix_ysize;
235 u8 object_num;
236};
237
Iiro Valkonen7686b102011-02-02 23:21:58 -0800238struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700239 u8 type;
240 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700241 u8 size_minus_one;
242 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700243 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800244} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700245
Iiro Valkonen7686b102011-02-02 23:21:58 -0800246struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700247 u8 reportid;
248 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700249};
250
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700251/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800252struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700253 struct i2c_client *client;
254 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800255 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800256 const struct mxt_platform_data *pdata;
257 struct mxt_object *object_table;
258 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700259 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700260 unsigned int max_x;
261 unsigned int max_y;
Benson Leungd79e7e42014-05-18 23:02:52 -0700262 bool in_bootloader;
Nick Dyerc3f78042014-05-18 23:04:46 -0700263 u32 config_crc;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800264
265 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800266 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700267 u16 T6_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800268 u8 T9_reportid_min;
269 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800270 u8 T19_reportid;
Benson Leungd79e7e42014-05-18 23:02:52 -0700271
272 /* for fw update in bootloader */
273 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700274
275 /* for reset handling */
276 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700277
278 /* for config update handling */
279 struct completion crc_completion;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700280};
281
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700282static size_t mxt_obj_size(const struct mxt_object *obj)
283{
284 return obj->size_minus_one + 1;
285}
286
287static size_t mxt_obj_instances(const struct mxt_object *obj)
288{
289 return obj->instances_minus_one + 1;
290}
291
Iiro Valkonen7686b102011-02-02 23:21:58 -0800292static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700293{
294 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700295 case MXT_GEN_COMMAND_T6:
296 case MXT_GEN_POWER_T7:
297 case MXT_GEN_ACQUIRE_T8:
298 case MXT_GEN_DATASOURCE_T53:
299 case MXT_TOUCH_MULTI_T9:
300 case MXT_TOUCH_KEYARRAY_T15:
301 case MXT_TOUCH_PROXIMITY_T23:
302 case MXT_TOUCH_PROXKEY_T52:
303 case MXT_PROCI_GRIPFACE_T20:
304 case MXT_PROCG_NOISE_T22:
305 case MXT_PROCI_ONETOUCH_T24:
306 case MXT_PROCI_TWOTOUCH_T27:
307 case MXT_PROCI_GRIP_T40:
308 case MXT_PROCI_PALM_T41:
309 case MXT_PROCI_TOUCHSUPPRESSION_T42:
310 case MXT_PROCI_STYLUS_T47:
311 case MXT_PROCG_NOISESUPPRESSION_T48:
312 case MXT_SPT_COMMSCONFIG_T18:
313 case MXT_SPT_GPIOPWM_T19:
314 case MXT_SPT_SELFTEST_T25:
315 case MXT_SPT_CTECONFIG_T28:
316 case MXT_SPT_USERDATA_T38:
317 case MXT_SPT_DIGITIZER_T43:
318 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700319 return true;
320 default:
321 return false;
322 }
323}
324
Iiro Valkonen7686b102011-02-02 23:21:58 -0800325static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700326{
327 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700328 case MXT_GEN_COMMAND_T6:
329 case MXT_GEN_POWER_T7:
330 case MXT_GEN_ACQUIRE_T8:
331 case MXT_TOUCH_MULTI_T9:
332 case MXT_TOUCH_KEYARRAY_T15:
333 case MXT_TOUCH_PROXIMITY_T23:
334 case MXT_TOUCH_PROXKEY_T52:
335 case MXT_PROCI_GRIPFACE_T20:
336 case MXT_PROCG_NOISE_T22:
337 case MXT_PROCI_ONETOUCH_T24:
338 case MXT_PROCI_TWOTOUCH_T27:
339 case MXT_PROCI_GRIP_T40:
340 case MXT_PROCI_PALM_T41:
341 case MXT_PROCI_TOUCHSUPPRESSION_T42:
342 case MXT_PROCI_STYLUS_T47:
343 case MXT_PROCG_NOISESUPPRESSION_T48:
344 case MXT_SPT_COMMSCONFIG_T18:
345 case MXT_SPT_GPIOPWM_T19:
346 case MXT_SPT_SELFTEST_T25:
347 case MXT_SPT_CTECONFIG_T28:
348 case MXT_SPT_DIGITIZER_T43:
349 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700350 return true;
351 default:
352 return false;
353 }
354}
355
Iiro Valkonen7686b102011-02-02 23:21:58 -0800356static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700357 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700358{
Andy Shevchenkoeb007c82012-10-04 00:02:59 -0700359 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
360 message->reportid, 7, message->message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700361}
362
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700363static int mxt_wait_for_completion(struct mxt_data *data,
364 struct completion *comp,
365 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700366{
Benson Leungd79e7e42014-05-18 23:02:52 -0700367 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700368 unsigned long timeout = msecs_to_jiffies(timeout_ms);
369 long ret;
370
371 ret = wait_for_completion_interruptible_timeout(comp, timeout);
372 if (ret < 0) {
373 return ret;
374 } else if (ret == 0) {
375 dev_err(dev, "Wait for completion timed out.\n");
376 return -ETIMEDOUT;
377 }
378 return 0;
379}
380
381static int mxt_check_bootloader(struct mxt_data *data, unsigned int state)
382{
383 struct i2c_client *client = data->client;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700384 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700385 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700386
387recheck:
Benson Leungd79e7e42014-05-18 23:02:52 -0700388 if (state != MXT_WAITING_BOOTLOAD_CMD) {
389 /*
390 * In application update mode, the interrupt
391 * line signals state transitions. We must wait for the
392 * CHG assertion before reading the status byte.
393 * Once the status byte has been read, the line is deasserted.
394 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700395 ret = mxt_wait_for_completion(data, &data->bl_completion,
396 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700397 if (ret) {
398 /*
399 * TODO: handle -ERESTARTSYS better by terminating
400 * fw update process before returning to userspace
401 * by writing length 0x000 to device (iff we are in
402 * WAITING_FRAME_DATA state).
403 */
404 dev_err(&client->dev, "Update wait error %d\n", ret);
405 return ret;
406 }
407 }
408
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700409 if (i2c_master_recv(client, &val, 1) != 1) {
410 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
411 return -EIO;
412 }
413
414 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800415 case MXT_WAITING_BOOTLOAD_CMD:
416 case MXT_WAITING_FRAME_DATA:
417 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700418 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800419 case MXT_FRAME_CRC_PASS:
420 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700421 goto recheck;
422 break;
423 default:
424 return -EINVAL;
425 }
426
427 if (val != state) {
Nick Dyer7bed6802014-05-18 23:04:09 -0700428 dev_err(&client->dev, "Invalid bootloader state %02X != %02X\n",
429 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430 return -EINVAL;
431 }
432
433 return 0;
434}
435
Iiro Valkonen7686b102011-02-02 23:21:58 -0800436static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700437{
438 u8 buf[2];
439
Iiro Valkonen7686b102011-02-02 23:21:58 -0800440 buf[0] = MXT_UNLOCK_CMD_LSB;
441 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700442
443 if (i2c_master_send(client, buf, 2) != 2) {
444 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
445 return -EIO;
446 }
447
448 return 0;
449}
450
Iiro Valkonen7686b102011-02-02 23:21:58 -0800451static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700452 const u8 *data, unsigned int frame_size)
453{
454 if (i2c_master_send(client, data, frame_size) != frame_size) {
455 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
456 return -EIO;
457 }
458
459 return 0;
460}
461
Iiro Valkonen7686b102011-02-02 23:21:58 -0800462static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700463 u16 reg, u16 len, void *val)
464{
465 struct i2c_msg xfer[2];
466 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800467 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700468
469 buf[0] = reg & 0xff;
470 buf[1] = (reg >> 8) & 0xff;
471
472 /* Write register */
473 xfer[0].addr = client->addr;
474 xfer[0].flags = 0;
475 xfer[0].len = 2;
476 xfer[0].buf = buf;
477
478 /* Read data */
479 xfer[1].addr = client->addr;
480 xfer[1].flags = I2C_M_RD;
481 xfer[1].len = len;
482 xfer[1].buf = val;
483
Daniel Kurtz771733e2012-06-28 21:08:11 +0800484 ret = i2c_transfer(client->adapter, xfer, 2);
485 if (ret == 2) {
486 ret = 0;
487 } else {
488 if (ret >= 0)
489 ret = -EIO;
490 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
491 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700492 }
493
Daniel Kurtz771733e2012-06-28 21:08:11 +0800494 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495}
496
Iiro Valkonen7686b102011-02-02 23:21:58 -0800497static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800499 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700500}
501
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800502static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
503 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800505 u8 *buf;
506 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800507 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700508
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800509 count = len + 2;
510 buf = kmalloc(count, GFP_KERNEL);
511 if (!buf)
512 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700513
514 buf[0] = reg & 0xff;
515 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800516 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800518 ret = i2c_master_send(client, buf, count);
519 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800520 ret = 0;
521 } else {
522 if (ret >= 0)
523 ret = -EIO;
524 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
525 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700526 }
527
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800528 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800529 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530}
531
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800532static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700533{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800534 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700535}
536
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537static struct mxt_object *
538mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700539{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800540 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700541 int i;
542
543 for (i = 0; i < data->info.object_num; i++) {
544 object = data->object_table + i;
545 if (object->type == type)
546 return object;
547 }
548
Nick Dyer7bed6802014-05-18 23:04:09 -0700549 dev_err(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550 return NULL;
551}
552
Iiro Valkonen7686b102011-02-02 23:21:58 -0800553static int mxt_read_message(struct mxt_data *data,
554 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700555{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800556 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557 u16 reg;
558
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700559 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700560 if (!object)
561 return -EINVAL;
562
563 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800564 return __mxt_read_reg(data->client, reg,
565 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700566}
567
Iiro Valkonen7686b102011-02-02 23:21:58 -0800568static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569 u8 type, u8 offset, u8 val)
570{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800571 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572 u16 reg;
573
Iiro Valkonen7686b102011-02-02 23:21:58 -0800574 object = mxt_get_object(data, type);
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700575 if (!object || offset >= mxt_obj_size(object))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700576 return -EINVAL;
577
578 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580}
581
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800582static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
583{
584 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700585 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800586 bool button;
587 int i;
588
589 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700590 for (i = 0; i < pdata->t19_num_keys; i++) {
591 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800592 continue;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700593 button = !(message->message[0] & (1 << i));
594 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800595 }
596}
597
Iiro Valkonen7686b102011-02-02 23:21:58 -0800598static void mxt_input_touchevent(struct mxt_data *data,
599 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700601 struct device *dev = &data->client->dev;
602 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800603 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700604 int x;
605 int y;
606 int area;
Yufeng Shen28ac2932011-08-16 00:40:54 -0700607 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700608
Joonyoung Shim910d8052011-04-12 23:14:38 -0700609 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
610 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
611 if (data->max_x < 1024)
612 x = x >> 2;
613 if (data->max_y < 1024)
614 y = y >> 2;
615
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616 area = message->message[4];
Yufeng Shen28ac2932011-08-16 00:40:54 -0700617 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700618
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800619 dev_dbg(dev,
620 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
621 id,
622 (status & MXT_DETECT) ? 'D' : '.',
623 (status & MXT_PRESS) ? 'P' : '.',
624 (status & MXT_RELEASE) ? 'R' : '.',
625 (status & MXT_MOVE) ? 'M' : '.',
626 (status & MXT_VECTOR) ? 'V' : '.',
627 (status & MXT_AMP) ? 'A' : '.',
628 (status & MXT_SUPPRESS) ? 'S' : '.',
629 (status & MXT_UNGRIP) ? 'U' : '.',
630 x, y, area, pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800632 input_mt_slot(input_dev, id);
633 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
634 status & MXT_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800636 if (status & MXT_DETECT) {
637 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
638 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
639 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
640 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
641 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700642}
643
Nick Dyerc3f78042014-05-18 23:04:46 -0700644static u16 mxt_extract_T6_csum(const u8 *csum)
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800645{
646 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
647}
648
Daniel Kurtz04a79182012-06-28 21:08:21 +0800649static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
650{
651 u8 id = msg->reportid;
652 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653}
654
Benson Leungd79e7e42014-05-18 23:02:52 -0700655static irqreturn_t mxt_process_messages_until_invalid(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800657 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800658 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700659 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800661 bool update_input = false;
Nick Dyerc3f78042014-05-18 23:04:46 -0700662 u32 crc;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700663
664 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700667 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700668 }
669
670 reportid = message.reportid;
671
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800672 if (reportid == data->T6_reportid) {
673 u8 status = payload[0];
Nick Dyerc3f78042014-05-18 23:04:46 -0700674
675 crc = mxt_extract_T6_csum(&payload[1]);
676 if (crc != data->config_crc) {
677 data->config_crc = crc;
678 complete(&data->crc_completion);
679 }
680
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800681 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
Nick Dyerc3f78042014-05-18 23:04:46 -0700682 status, data->config_crc);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700683
684 if (status & MXT_T6_STATUS_RESET)
685 complete(&data->reset_completion);
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800686 } else if (mxt_is_T9_message(data, &message)) {
687 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800688 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800689 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800690 } else if (message.reportid == data->T19_reportid) {
691 mxt_input_button(data, &message);
692 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800693 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800694 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800695 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696 } while (reportid != 0xff);
697
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800698 if (update_input) {
699 input_mt_report_pointer_emulation(data->input_dev, false);
700 input_sync(data->input_dev);
701 }
702
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700703 return IRQ_HANDLED;
704}
705
Benson Leungd79e7e42014-05-18 23:02:52 -0700706static irqreturn_t mxt_interrupt(int irq, void *dev_id)
707{
708 struct mxt_data *data = dev_id;
709
710 if (data->in_bootloader) {
711 /* bootloader state transition completion */
712 complete(&data->bl_completion);
713 return IRQ_HANDLED;
714 }
715
716 return mxt_process_messages_until_invalid(data);
717}
718
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700719static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
720 u8 value, bool wait)
721{
722 u16 reg;
723 u8 command_register;
724 int timeout_counter = 0;
725 int ret;
726
727 reg = data->T6_address + cmd_offset;
728
729 ret = mxt_write_reg(data->client, reg, value);
730 if (ret)
731 return ret;
732
733 if (!wait)
734 return 0;
735
736 do {
737 msleep(20);
738 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
739 if (ret)
740 return ret;
741 } while (command_register != 0 && timeout_counter++ <= 100);
742
743 if (timeout_counter > 100) {
744 dev_err(&data->client->dev, "Command failed!\n");
745 return -EIO;
746 }
747
748 return 0;
749}
750
751static int mxt_soft_reset(struct mxt_data *data)
752{
753 struct device *dev = &data->client->dev;
754 int ret = 0;
755
756 dev_info(dev, "Resetting chip\n");
757
758 reinit_completion(&data->reset_completion);
759
760 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
761 if (ret)
762 return ret;
763
764 ret = mxt_wait_for_completion(data, &data->reset_completion,
765 MXT_RESET_TIMEOUT);
766 if (ret)
767 return ret;
768
769 return 0;
770}
771
Nick Dyerc3f78042014-05-18 23:04:46 -0700772static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
773{
774 /*
775 * On failure, CRC is set to 0 and config will always be
776 * downloaded.
777 */
778 data->config_crc = 0;
779 reinit_completion(&data->crc_completion);
780
781 mxt_t6_command(data, cmd, value, true);
782
783 /*
784 * Wait for crc message. On failure, CRC is set to 0 and config will
785 * always be downloaded.
786 */
787 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
788}
789
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700791{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800792 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800793 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794 struct device *dev = &data->client->dev;
795 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800796 int i, size;
797 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798
Iiro Valkonen71749f52011-02-15 13:36:52 -0800799 if (!pdata->config) {
800 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
801 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700802 }
803
Nick Dyerc3f78042014-05-18 23:04:46 -0700804 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
805
806 if (data->config_crc == pdata->config_crc) {
807 dev_info(dev, "Config CRC 0x%06X: OK\n", data->config_crc);
808 return 0;
809 }
810
811 dev_info(dev, "Config CRC 0x%06X: does not match 0x%06X\n",
812 data->config_crc, pdata->config_crc);
813
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814 for (i = 0; i < data->info.object_num; i++) {
815 object = data->object_table + i;
816
Iiro Valkonen7686b102011-02-02 23:21:58 -0800817 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700818 continue;
819
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700820 size = mxt_obj_size(object) * mxt_obj_instances(object);
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800821 if (index + size > pdata->config_length) {
822 dev_err(dev, "Not enough config data!\n");
823 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800824 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800825
826 ret = __mxt_write_reg(data->client, object->start_address,
827 size, &pdata->config[index]);
828 if (ret)
829 return ret;
830 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831 }
832
Nick Dyerc3f78042014-05-18 23:04:46 -0700833 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
834
835 ret = mxt_soft_reset(data);
836 if (ret)
837 return ret;
838
839 dev_info(dev, "Config successfully updated\n");
840
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700841 return 0;
842}
843
Iiro Valkonen7686b102011-02-02 23:21:58 -0800844static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700845{
846 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800847 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700848 int count = 10;
849 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700850
851 /* Read dummy message to make high CHG pin */
852 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800853 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700854 if (error)
855 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800856 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700857
858 if (!count) {
859 dev_err(dev, "CHG pin isn't cleared\n");
860 return -EBUSY;
861 }
862
863 return 0;
864}
865
Iiro Valkonen7686b102011-02-02 23:21:58 -0800866static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700867{
868 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800869 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700870 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700871
Daniel Kurtz23003a842012-06-28 21:08:14 +0800872 /* Read 7-byte info block starting at address 0 */
873 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700874 if (error)
875 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700876
877 return 0;
878}
879
Iiro Valkonen7686b102011-02-02 23:21:58 -0800880static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700881{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800882 struct i2c_client *client = data->client;
883 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700884 int error;
885 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800886 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700887
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800888 table_size = data->info.object_num * sizeof(struct mxt_object);
889 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
890 data->object_table);
891 if (error)
892 return error;
893
894 /* Valid Report IDs start counting from 1 */
895 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700896 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800897 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800898 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700899
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800900 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901
902 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800903 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700904 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700905 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800906 max_id = reportid - 1;
907 } else {
908 min_id = 0;
909 max_id = 0;
910 }
911
912 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -0700913 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700914 object->type, object->start_address,
915 mxt_obj_size(object), mxt_obj_instances(object),
916 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800917
918 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800919 case MXT_GEN_COMMAND_T6:
920 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700921 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800922 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800923 case MXT_TOUCH_MULTI_T9:
924 data->T9_reportid_min = min_id;
925 data->T9_reportid_max = max_id;
926 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800927 case MXT_SPT_GPIOPWM_T19:
928 data->T19_reportid = min_id;
929 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700930 }
931 }
932
933 return 0;
934}
935
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800936static void mxt_free_object_table(struct mxt_data *data)
937{
938 kfree(data->object_table);
939 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800940 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800941 data->T9_reportid_min = 0;
942 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800943 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800944}
945
Iiro Valkonen7686b102011-02-02 23:21:58 -0800946static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700947{
948 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800949 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950 int error;
951 u8 val;
952
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700954 if (error)
955 return error;
956
957 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800958 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700959 GFP_KERNEL);
960 if (!data->object_table) {
961 dev_err(&client->dev, "Failed to allocate memory\n");
962 return -ENOMEM;
963 }
964
965 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800966 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -0700967 if (error) {
968 dev_err(&client->dev, "Error %d reading object table\n", error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800969 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -0700970 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700971
972 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800973 error = mxt_check_reg_init(data);
Nick Dyer7bed6802014-05-18 23:04:09 -0700974 if (error) {
975 dev_err(&client->dev, "Error %d initializing configuration\n",
976 error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800977 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -0700978 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700979
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800981 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700982 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800983 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700984 info->matrix_xsize = val;
985
Iiro Valkonen7686b102011-02-02 23:21:58 -0800986 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700987 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800988 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700989 info->matrix_ysize = val;
990
991 dev_info(&client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -0700992 "Family: %u Variant: %u Firmware V%u.%u.%02X\n",
Daniel Kurtze0e02692012-06-28 21:08:15 +0800993 info->family_id, info->variant_id, info->version >> 4,
994 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700995
996 dev_info(&client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -0700997 "Matrix X Size: %u Matrix Y Size: %u Objects: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700998 info->matrix_xsize, info->matrix_ysize,
999 info->object_num);
1000
1001 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001002
1003err_free_object_table:
1004 mxt_free_object_table(data);
1005 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006}
1007
Joonyoung Shim910d8052011-04-12 23:14:38 -07001008static void mxt_calc_resolution(struct mxt_data *data)
1009{
1010 unsigned int max_x = data->pdata->x_size - 1;
1011 unsigned int max_y = data->pdata->y_size - 1;
1012
1013 if (data->pdata->orient & MXT_XY_SWITCH) {
1014 data->max_x = max_y;
1015 data->max_y = max_x;
1016 } else {
1017 data->max_x = max_x;
1018 data->max_y = max_y;
1019 }
1020}
1021
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001022/* Firmware Version is returned as Major.Minor.Build */
1023static ssize_t mxt_fw_version_show(struct device *dev,
1024 struct device_attribute *attr, char *buf)
1025{
1026 struct mxt_data *data = dev_get_drvdata(dev);
1027 struct mxt_info *info = &data->info;
1028 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
1029 info->version >> 4, info->version & 0xf, info->build);
1030}
1031
1032/* Hardware Version is returned as FamilyID.VariantID */
1033static ssize_t mxt_hw_version_show(struct device *dev,
1034 struct device_attribute *attr, char *buf)
1035{
1036 struct mxt_data *data = dev_get_drvdata(dev);
1037 struct mxt_info *info = &data->info;
1038 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
1039 info->family_id, info->variant_id);
1040}
1041
Daniel Kurtz794eb672012-06-28 21:08:10 +08001042static ssize_t mxt_show_instance(char *buf, int count,
1043 struct mxt_object *object, int instance,
1044 const u8 *val)
1045{
1046 int i;
1047
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001048 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001049 count += scnprintf(buf + count, PAGE_SIZE - count,
1050 "Instance %u\n", instance);
1051
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001052 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001053 count += scnprintf(buf + count, PAGE_SIZE - count,
1054 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
1055 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
1056
1057 return count;
1058}
1059
Iiro Valkonen7686b102011-02-02 23:21:58 -08001060static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061 struct device_attribute *attr, char *buf)
1062{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001063 struct mxt_data *data = dev_get_drvdata(dev);
1064 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065 int count = 0;
1066 int i, j;
1067 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001068 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001069
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001070 /* Pre-allocate buffer large enough to hold max sized object. */
1071 obuf = kmalloc(256, GFP_KERNEL);
1072 if (!obuf)
1073 return -ENOMEM;
1074
1075 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001076 for (i = 0; i < data->info.object_num; i++) {
1077 object = data->object_table + i;
1078
Daniel Kurtz91630952012-06-28 21:08:09 +08001079 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001081
Daniel Kurtz91630952012-06-28 21:08:09 +08001082 count += scnprintf(buf + count, PAGE_SIZE - count,
1083 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001084
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001085 for (j = 0; j < mxt_obj_instances(object); j++) {
1086 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08001087 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001088
Daniel Kurtz794eb672012-06-28 21:08:10 +08001089 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001090 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001091 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001092
Daniel Kurtz794eb672012-06-28 21:08:10 +08001093 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001094 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001095 }
1096
Daniel Kurtz794eb672012-06-28 21:08:10 +08001097done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001098 kfree(obuf);
1099 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100}
1101
Iiro Valkonen7686b102011-02-02 23:21:58 -08001102static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001103{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001104 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001105 struct i2c_client *client = data->client;
1106 const struct firmware *fw = NULL;
1107 unsigned int frame_size;
1108 unsigned int pos = 0;
1109 int ret;
1110
1111 ret = request_firmware(&fw, fn, dev);
1112 if (ret) {
1113 dev_err(dev, "Unable to open firmware %s\n", fn);
1114 return ret;
1115 }
1116
1117 /* Change to the bootloader mode */
Benson Leungd79e7e42014-05-18 23:02:52 -07001118 data->in_bootloader = true;
1119
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001120 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_BOOT_VALUE, false);
1121 if (ret)
1122 goto release_firmware;
1123
Iiro Valkonen7686b102011-02-02 23:21:58 -08001124 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001125
1126 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001127 if (client->addr == MXT_APP_LOW)
1128 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001130 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001131
Benson Leungd79e7e42014-05-18 23:02:52 -07001132 reinit_completion(&data->bl_completion);
1133
1134 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001135 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001136 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001137
1138 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001139 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140
1141 while (pos < fw->size) {
Benson Leungd79e7e42014-05-18 23:02:52 -07001142 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001144 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001145
1146 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1147
1148 /* We should add 2 at frame size as the the firmware data is not
1149 * included the CRC bytes.
1150 */
1151 frame_size += 2;
1152
1153 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001154 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001155
Benson Leungd79e7e42014-05-18 23:02:52 -07001156 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001157 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001158 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001159
1160 pos += frame_size;
1161
1162 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1163 }
1164
Benson Leunga0434b72014-05-18 23:03:09 -07001165 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001166 ret = mxt_wait_for_completion(data, &data->bl_completion,
1167 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001168 if (ret)
1169 goto disable_irq;
1170
1171 /*
1172 * Wait for device to reset. Some bootloader versions do not assert
1173 * the CHG line after bootloading has finished, so ignore potential
1174 * errors.
1175 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001176 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001177
Benson Leungd79e7e42014-05-18 23:02:52 -07001178 data->in_bootloader = false;
1179
1180disable_irq:
1181 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001182release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001183 release_firmware(fw);
1184
1185 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001186 if (client->addr == MXT_BOOT_LOW)
1187 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001188 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001189 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001190
1191 return ret;
1192}
1193
Iiro Valkonen7686b102011-02-02 23:21:58 -08001194static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001195 struct device_attribute *attr,
1196 const char *buf, size_t count)
1197{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001198 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001199 int error;
1200
Iiro Valkonen7686b102011-02-02 23:21:58 -08001201 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001202 if (error) {
1203 dev_err(dev, "The firmware update failed(%d)\n", error);
1204 count = error;
1205 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07001206 dev_info(dev, "The firmware update succeeded\n");
1207
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001208 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001209
Iiro Valkonen7686b102011-02-02 23:21:58 -08001210 mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07001211
1212 enable_irq(data->irq);
1213
1214 error = mxt_make_highchg(data);
1215 if (error)
1216 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001217 }
1218
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001219 return count;
1220}
1221
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001222static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1223static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001224static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1225static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001226
Iiro Valkonen7686b102011-02-02 23:21:58 -08001227static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001228 &dev_attr_fw_version.attr,
1229 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001230 &dev_attr_object.attr,
1231 &dev_attr_update_fw.attr,
1232 NULL
1233};
1234
Iiro Valkonen7686b102011-02-02 23:21:58 -08001235static const struct attribute_group mxt_attr_group = {
1236 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001237};
1238
Iiro Valkonen7686b102011-02-02 23:21:58 -08001239static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001240{
1241 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001242 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001243 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001244}
1245
Iiro Valkonen7686b102011-02-02 23:21:58 -08001246static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247{
1248 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001249 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001250 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001251}
1252
Iiro Valkonen7686b102011-02-02 23:21:58 -08001253static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001254{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001255 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001256
Iiro Valkonen7686b102011-02-02 23:21:58 -08001257 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001258
1259 return 0;
1260}
1261
Iiro Valkonen7686b102011-02-02 23:21:58 -08001262static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001263{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001264 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265
Iiro Valkonen7686b102011-02-02 23:21:58 -08001266 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001267}
1268
Bill Pemberton5298cc42012-11-23 21:38:25 -08001269static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001270 const struct i2c_device_id *id)
1271{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001272 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001273 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001274 struct input_dev *input_dev;
1275 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001276 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001277 unsigned int mt_flags = 0;
1278 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001279
Iiro Valkonen919ed892011-02-15 13:36:52 -08001280 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001281 return -EINVAL;
1282
Iiro Valkonen7686b102011-02-02 23:21:58 -08001283 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001284 input_dev = input_allocate_device();
1285 if (!data || !input_dev) {
1286 dev_err(&client->dev, "Failed to allocate memory\n");
1287 error = -ENOMEM;
1288 goto err_free_mem;
1289 }
1290
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001291 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001292 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1293 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001294
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001295 input_dev->phys = data->phys;
1296
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001297 input_dev->id.bustype = BUS_I2C;
1298 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001299 input_dev->open = mxt_input_open;
1300 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001301
Joonyoung Shim910d8052011-04-12 23:14:38 -07001302 data->client = client;
1303 data->input_dev = input_dev;
1304 data->pdata = pdata;
1305 data->irq = client->irq;
1306
Benson Leungd79e7e42014-05-18 23:02:52 -07001307 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001308 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07001309 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07001310
Joonyoung Shim910d8052011-04-12 23:14:38 -07001311 mxt_calc_resolution(data);
1312
Daniel Kurtzcb159112012-06-28 21:08:22 +08001313 error = mxt_initialize(data);
1314 if (error)
1315 goto err_free_mem;
1316
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001317 __set_bit(EV_ABS, input_dev->evbit);
1318 __set_bit(EV_KEY, input_dev->evbit);
1319 __set_bit(BTN_TOUCH, input_dev->keybit);
1320
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001321 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001322 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1323
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001324 for (i = 0; i < pdata->t19_num_keys; i++)
1325 if (pdata->t19_keymap[i] != KEY_RESERVED)
1326 input_set_capability(input_dev, EV_KEY,
1327 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001328
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001329 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001330
1331 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1332 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1333 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1334 MXT_PIXELS_PER_MM);
1335 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1336 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001337
1338 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001339 }
1340
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001341 /* For single touch */
1342 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001343 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001344 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001345 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001346 input_set_abs_params(input_dev, ABS_PRESSURE,
1347 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001348
1349 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001350 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001351 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001352 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001353 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001354 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001355 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001356 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001357 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001359 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001360 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1361 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001362
1363 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001364 i2c_set_clientdata(client, data);
1365
Iiro Valkonen7686b102011-02-02 23:21:58 -08001366 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001367 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001368 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001369 if (error) {
1370 dev_err(&client->dev, "Failed to register interrupt\n");
1371 goto err_free_object;
1372 }
1373
Iiro Valkonen08960a02011-04-12 23:16:40 -07001374 error = mxt_make_highchg(data);
1375 if (error)
1376 goto err_free_irq;
1377
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001378 error = input_register_device(input_dev);
Nick Dyer7bed6802014-05-18 23:04:09 -07001379 if (error) {
1380 dev_err(&client->dev, "Error %d registering input device\n",
1381 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001382 goto err_free_irq;
Nick Dyer7bed6802014-05-18 23:04:09 -07001383 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001384
Iiro Valkonen7686b102011-02-02 23:21:58 -08001385 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07001386 if (error) {
1387 dev_err(&client->dev, "Failure %d creating sysfs group\n",
1388 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389 goto err_unregister_device;
Nick Dyer7bed6802014-05-18 23:04:09 -07001390 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001391
1392 return 0;
1393
1394err_unregister_device:
1395 input_unregister_device(input_dev);
1396 input_dev = NULL;
1397err_free_irq:
1398 free_irq(client->irq, data);
1399err_free_object:
1400 kfree(data->object_table);
1401err_free_mem:
1402 input_free_device(input_dev);
1403 kfree(data);
1404 return error;
1405}
1406
Bill Pembertone2619cf2012-11-23 21:50:47 -08001407static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001408{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001409 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001410
Iiro Valkonen7686b102011-02-02 23:21:58 -08001411 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001412 free_irq(data->irq, data);
1413 input_unregister_device(data->input_dev);
1414 kfree(data->object_table);
1415 kfree(data);
1416
1417 return 0;
1418}
1419
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001420#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001421static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001422{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001423 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001424 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001425 struct input_dev *input_dev = data->input_dev;
1426
1427 mutex_lock(&input_dev->mutex);
1428
1429 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001430 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001431
1432 mutex_unlock(&input_dev->mutex);
1433
1434 return 0;
1435}
1436
Iiro Valkonen7686b102011-02-02 23:21:58 -08001437static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001438{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001439 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001440 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001441 struct input_dev *input_dev = data->input_dev;
1442
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001443 mxt_soft_reset(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001444
1445 mutex_lock(&input_dev->mutex);
1446
1447 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001448 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001449
1450 mutex_unlock(&input_dev->mutex);
1451
1452 return 0;
1453}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001454#endif
1455
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001456static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1457
Iiro Valkonen7686b102011-02-02 23:21:58 -08001458static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001459 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001460 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001461 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001462 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001463 { }
1464};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001465MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001466
Iiro Valkonen7686b102011-02-02 23:21:58 -08001467static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001468 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001469 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001471 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001472 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001473 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001474 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001475 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001476};
1477
Axel Lin1b92c1c2012-03-16 23:05:41 -07001478module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001479
1480/* Module information */
1481MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001482MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001483MODULE_LICENSE("GPL");