blob: 02c374d529675d82b9ee3b6197385644f364bcc2 [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
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070032/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080033#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070034
35/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080036#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_FAMILY_ID 0x00
38#define MXT_VARIANT_ID 0x01
39#define MXT_VERSION 0x02
40#define MXT_BUILD 0x03
41#define MXT_MATRIX_X_SIZE 0x04
42#define MXT_MATRIX_Y_SIZE 0x05
43#define MXT_OBJECT_NUM 0x06
44#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070045
Iiro Valkonen7686b102011-02-02 23:21:58 -080046#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070047
48/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070049#define MXT_DEBUG_DIAGNOSTIC_T37 37
50#define MXT_GEN_MESSAGE_T5 5
51#define MXT_GEN_COMMAND_T6 6
52#define MXT_GEN_POWER_T7 7
53#define MXT_GEN_ACQUIRE_T8 8
54#define MXT_GEN_DATASOURCE_T53 53
55#define MXT_TOUCH_MULTI_T9 9
56#define MXT_TOUCH_KEYARRAY_T15 15
57#define MXT_TOUCH_PROXIMITY_T23 23
58#define MXT_TOUCH_PROXKEY_T52 52
59#define MXT_PROCI_GRIPFACE_T20 20
60#define MXT_PROCG_NOISE_T22 22
61#define MXT_PROCI_ONETOUCH_T24 24
62#define MXT_PROCI_TWOTOUCH_T27 27
63#define MXT_PROCI_GRIP_T40 40
64#define MXT_PROCI_PALM_T41 41
65#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
66#define MXT_PROCI_STYLUS_T47 47
67#define MXT_PROCG_NOISESUPPRESSION_T48 48
68#define MXT_SPT_COMMSCONFIG_T18 18
69#define MXT_SPT_GPIOPWM_T19 19
70#define MXT_SPT_SELFTEST_T25 25
71#define MXT_SPT_CTECONFIG_T28 28
72#define MXT_SPT_USERDATA_T38 38
73#define MXT_SPT_DIGITIZER_T43 43
74#define MXT_SPT_MESSAGECOUNT_T44 44
75#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070076
Iiro Valkonen81c88a72011-07-04 03:08:25 -070077/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080078#define MXT_COMMAND_RESET 0
79#define MXT_COMMAND_BACKUPNV 1
80#define MXT_COMMAND_CALIBRATE 2
81#define MXT_COMMAND_REPORTALL 3
82#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070083
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070084/* Define for T6 status byte */
85#define MXT_T6_STATUS_RESET (1 << 7)
86
Iiro Valkonen81c88a72011-07-04 03:08:25 -070087/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080088#define MXT_POWER_IDLEACQINT 0
89#define MXT_POWER_ACTVACQINT 1
90#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen81c88a72011-07-04 03:08:25 -070092/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080093#define MXT_ACQUIRE_CHRGTIME 0
94#define MXT_ACQUIRE_TCHDRIFT 2
95#define MXT_ACQUIRE_DRIFTST 3
96#define MXT_ACQUIRE_TCHAUTOCAL 4
97#define MXT_ACQUIRE_SYNC 5
98#define MXT_ACQUIRE_ATCHCALST 6
99#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700100
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700101/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800102#define MXT_TOUCH_CTRL 0
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700103#define MXT_T9_ORIENT 9
104#define MXT_T9_RANGE 18
105
Nick Dyerf3889ed2014-05-18 23:22:04 -0700106/* MXT_TOUCH_MULTI_T9 status */
107#define MXT_T9_UNGRIP (1 << 0)
108#define MXT_T9_SUPPRESS (1 << 1)
109#define MXT_T9_AMP (1 << 2)
110#define MXT_T9_VECTOR (1 << 3)
111#define MXT_T9_MOVE (1 << 4)
112#define MXT_T9_RELEASE (1 << 5)
113#define MXT_T9_PRESS (1 << 6)
114#define MXT_T9_DETECT (1 << 7)
115
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700116struct t9_range {
117 u16 x;
118 u16 y;
119} __packed;
120
Nick Dyerf3889ed2014-05-18 23:22:04 -0700121/* MXT_TOUCH_MULTI_T9 orient */
122#define MXT_T9_ORIENT_SWITCH (1 << 0)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700123
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700124/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800125#define MXT_GRIPFACE_CTRL 0
126#define MXT_GRIPFACE_XLOGRIP 1
127#define MXT_GRIPFACE_XHIGRIP 2
128#define MXT_GRIPFACE_YLOGRIP 3
129#define MXT_GRIPFACE_YHIGRIP 4
130#define MXT_GRIPFACE_MAXTCHS 5
131#define MXT_GRIPFACE_SZTHR1 7
132#define MXT_GRIPFACE_SZTHR2 8
133#define MXT_GRIPFACE_SHPTHR1 9
134#define MXT_GRIPFACE_SHPTHR2 10
135#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700136
Iiro Valkonen7686b102011-02-02 23:21:58 -0800137/* MXT_PROCI_NOISE field */
138#define MXT_NOISE_CTRL 0
139#define MXT_NOISE_OUTFLEN 1
140#define MXT_NOISE_GCAFUL_LSB 3
141#define MXT_NOISE_GCAFUL_MSB 4
142#define MXT_NOISE_GCAFLL_LSB 5
143#define MXT_NOISE_GCAFLL_MSB 6
144#define MXT_NOISE_ACTVGCAFVALID 7
145#define MXT_NOISE_NOISETHR 8
146#define MXT_NOISE_FREQHOPSCALE 10
147#define MXT_NOISE_FREQ0 11
148#define MXT_NOISE_FREQ1 12
149#define MXT_NOISE_FREQ2 13
150#define MXT_NOISE_FREQ3 14
151#define MXT_NOISE_FREQ4 15
152#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700153
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700154/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800155#define MXT_COMMS_CTRL 0
156#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700157
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700158/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800159#define MXT_CTE_CTRL 0
160#define MXT_CTE_CMD 1
161#define MXT_CTE_MODE 2
162#define MXT_CTE_IDLEGCAFDEPTH 3
163#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700164#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700165
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166#define MXT_VOLTAGE_DEFAULT 2700000
167#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700168
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700169/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800170#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700171#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800172#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700173
174/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800175#define MXT_BACKUP_TIME 50 /* msec */
176#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700177#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700178#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700179#define MXT_FW_RESET_TIME 3000 /* msec */
180#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700181
182/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800183#define MXT_UNLOCK_CMD_MSB 0xaa
184#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700185
186/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800187#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
188#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
189#define MXT_FRAME_CRC_CHECK 0x02
190#define MXT_FRAME_CRC_FAIL 0x03
191#define MXT_FRAME_CRC_PASS 0x04
192#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
193#define MXT_BOOT_STATUS_MASK 0x3f
Nick Dyere57a66a2014-05-18 23:13:40 -0700194#define MXT_BOOT_EXTENDED_ID (1 << 5)
195#define MXT_BOOT_ID_MASK 0x1f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700196
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700197/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800198#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800200#define MXT_PIXELS_PER_MM 20
201
Iiro Valkonen7686b102011-02-02 23:21:58 -0800202struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700203 u8 family_id;
204 u8 variant_id;
205 u8 version;
206 u8 build;
207 u8 matrix_xsize;
208 u8 matrix_ysize;
209 u8 object_num;
210};
211
Iiro Valkonen7686b102011-02-02 23:21:58 -0800212struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700213 u8 type;
214 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700215 u8 size_minus_one;
216 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800218} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219
Iiro Valkonen7686b102011-02-02 23:21:58 -0800220struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700221 u8 reportid;
222 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700223};
224
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700225/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800226struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700227 struct i2c_client *client;
228 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800229 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800230 const struct mxt_platform_data *pdata;
231 struct mxt_object *object_table;
232 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700234 unsigned int max_x;
235 unsigned int max_y;
Benson Leungd79e7e42014-05-18 23:02:52 -0700236 bool in_bootloader;
Nick Dyerc3f78042014-05-18 23:04:46 -0700237 u32 config_crc;
Nick Dyerf28a8422014-05-18 23:10:49 -0700238 u8 bootloader_addr;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800239
240 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800241 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700242 u16 T6_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800243 u8 T9_reportid_min;
244 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800245 u8 T19_reportid;
Benson Leungd79e7e42014-05-18 23:02:52 -0700246
247 /* for fw update in bootloader */
248 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700249
250 /* for reset handling */
251 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700252
253 /* for config update handling */
254 struct completion crc_completion;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700255};
256
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700257static size_t mxt_obj_size(const struct mxt_object *obj)
258{
259 return obj->size_minus_one + 1;
260}
261
262static size_t mxt_obj_instances(const struct mxt_object *obj)
263{
264 return obj->instances_minus_one + 1;
265}
266
Iiro Valkonen7686b102011-02-02 23:21:58 -0800267static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700268{
269 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700270 case MXT_GEN_COMMAND_T6:
271 case MXT_GEN_POWER_T7:
272 case MXT_GEN_ACQUIRE_T8:
273 case MXT_GEN_DATASOURCE_T53:
274 case MXT_TOUCH_MULTI_T9:
275 case MXT_TOUCH_KEYARRAY_T15:
276 case MXT_TOUCH_PROXIMITY_T23:
277 case MXT_TOUCH_PROXKEY_T52:
278 case MXT_PROCI_GRIPFACE_T20:
279 case MXT_PROCG_NOISE_T22:
280 case MXT_PROCI_ONETOUCH_T24:
281 case MXT_PROCI_TWOTOUCH_T27:
282 case MXT_PROCI_GRIP_T40:
283 case MXT_PROCI_PALM_T41:
284 case MXT_PROCI_TOUCHSUPPRESSION_T42:
285 case MXT_PROCI_STYLUS_T47:
286 case MXT_PROCG_NOISESUPPRESSION_T48:
287 case MXT_SPT_COMMSCONFIG_T18:
288 case MXT_SPT_GPIOPWM_T19:
289 case MXT_SPT_SELFTEST_T25:
290 case MXT_SPT_CTECONFIG_T28:
291 case MXT_SPT_USERDATA_T38:
292 case MXT_SPT_DIGITIZER_T43:
293 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700294 return true;
295 default:
296 return false;
297 }
298}
299
Iiro Valkonen7686b102011-02-02 23:21:58 -0800300static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700301{
302 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700303 case MXT_GEN_COMMAND_T6:
304 case MXT_GEN_POWER_T7:
305 case MXT_GEN_ACQUIRE_T8:
306 case MXT_TOUCH_MULTI_T9:
307 case MXT_TOUCH_KEYARRAY_T15:
308 case MXT_TOUCH_PROXIMITY_T23:
309 case MXT_TOUCH_PROXKEY_T52:
310 case MXT_PROCI_GRIPFACE_T20:
311 case MXT_PROCG_NOISE_T22:
312 case MXT_PROCI_ONETOUCH_T24:
313 case MXT_PROCI_TWOTOUCH_T27:
314 case MXT_PROCI_GRIP_T40:
315 case MXT_PROCI_PALM_T41:
316 case MXT_PROCI_TOUCHSUPPRESSION_T42:
317 case MXT_PROCI_STYLUS_T47:
318 case MXT_PROCG_NOISESUPPRESSION_T48:
319 case MXT_SPT_COMMSCONFIG_T18:
320 case MXT_SPT_GPIOPWM_T19:
321 case MXT_SPT_SELFTEST_T25:
322 case MXT_SPT_CTECONFIG_T28:
323 case MXT_SPT_DIGITIZER_T43:
324 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700325 return true;
326 default:
327 return false;
328 }
329}
330
Iiro Valkonen7686b102011-02-02 23:21:58 -0800331static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700332 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700333{
Andy Shevchenkoeb007c82012-10-04 00:02:59 -0700334 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
335 message->reportid, 7, message->message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700336}
337
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700338static int mxt_wait_for_completion(struct mxt_data *data,
339 struct completion *comp,
340 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700341{
Benson Leungd79e7e42014-05-18 23:02:52 -0700342 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700343 unsigned long timeout = msecs_to_jiffies(timeout_ms);
344 long ret;
345
346 ret = wait_for_completion_interruptible_timeout(comp, timeout);
347 if (ret < 0) {
348 return ret;
349 } else if (ret == 0) {
350 dev_err(dev, "Wait for completion timed out.\n");
351 return -ETIMEDOUT;
352 }
353 return 0;
354}
355
Nick Dyerf28a8422014-05-18 23:10:49 -0700356static int mxt_bootloader_read(struct mxt_data *data,
357 u8 *val, unsigned int count)
358{
359 int ret;
360 struct i2c_msg msg;
361
362 msg.addr = data->bootloader_addr;
363 msg.flags = data->client->flags & I2C_M_TEN;
364 msg.flags |= I2C_M_RD;
365 msg.len = count;
366 msg.buf = val;
367
368 ret = i2c_transfer(data->client->adapter, &msg, 1);
369
370 if (ret == 1) {
371 ret = 0;
372 } else {
373 ret = ret < 0 ? ret : -EIO;
374 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
375 __func__, ret);
376 }
377
378 return ret;
379}
380
381static int mxt_bootloader_write(struct mxt_data *data,
382 const u8 * const val, unsigned int count)
383{
384 int ret;
385 struct i2c_msg msg;
386
387 msg.addr = data->bootloader_addr;
388 msg.flags = data->client->flags & I2C_M_TEN;
389 msg.len = count;
390 msg.buf = (u8 *)val;
391
392 ret = i2c_transfer(data->client->adapter, &msg, 1);
393 if (ret == 1) {
394 ret = 0;
395 } else {
396 ret = ret < 0 ? ret : -EIO;
397 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
398 __func__, ret);
399 }
400
401 return ret;
402}
403
404static int mxt_lookup_bootloader_address(struct mxt_data *data)
405{
406 u8 appmode = data->client->addr;
407 u8 bootloader;
408
409 switch (appmode) {
410 case 0x4a:
411 case 0x4b:
412 case 0x4c:
413 case 0x4d:
414 case 0x5a:
415 case 0x5b:
416 bootloader = appmode - 0x26;
417 break;
418 default:
419 dev_err(&data->client->dev,
420 "Appmode i2c address 0x%02x not found\n",
421 appmode);
422 return -EINVAL;
423 }
424
425 data->bootloader_addr = bootloader;
426 return 0;
427}
428
Nick Dyere57a66a2014-05-18 23:13:40 -0700429static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
430{
431 struct device *dev = &data->client->dev;
432 u8 buf[3];
433
434 if (val & MXT_BOOT_EXTENDED_ID) {
435 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
436 dev_err(dev, "%s: i2c failure\n", __func__);
Nick Dyer68807a02014-06-07 23:17:26 -0700437 return val;
Nick Dyere57a66a2014-05-18 23:13:40 -0700438 }
439
440 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
441
442 return buf[0];
443 } else {
444 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
445
446 return val;
447 }
448}
449
Benson Leungd79e7e42014-05-18 23:02:52 -0700450static int mxt_check_bootloader(struct mxt_data *data, unsigned int state)
451{
Nick Dyerf28a8422014-05-18 23:10:49 -0700452 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700453 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700454 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700455
456recheck:
Benson Leungd79e7e42014-05-18 23:02:52 -0700457 if (state != MXT_WAITING_BOOTLOAD_CMD) {
458 /*
459 * In application update mode, the interrupt
460 * line signals state transitions. We must wait for the
461 * CHG assertion before reading the status byte.
462 * Once the status byte has been read, the line is deasserted.
463 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700464 ret = mxt_wait_for_completion(data, &data->bl_completion,
465 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700466 if (ret) {
467 /*
468 * TODO: handle -ERESTARTSYS better by terminating
469 * fw update process before returning to userspace
470 * by writing length 0x000 to device (iff we are in
471 * WAITING_FRAME_DATA state).
472 */
Nick Dyerf28a8422014-05-18 23:10:49 -0700473 dev_err(dev, "Update wait error %d\n", ret);
Benson Leungd79e7e42014-05-18 23:02:52 -0700474 return ret;
475 }
476 }
477
Nick Dyerf28a8422014-05-18 23:10:49 -0700478 ret = mxt_bootloader_read(data, &val, 1);
479 if (ret)
480 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700481
Nick Dyere57a66a2014-05-18 23:13:40 -0700482 if (state == MXT_WAITING_BOOTLOAD_CMD)
483 val = mxt_get_bootloader_version(data, val);
484
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800486 case MXT_WAITING_BOOTLOAD_CMD:
487 case MXT_WAITING_FRAME_DATA:
488 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700489 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 case MXT_FRAME_CRC_PASS:
Nick Dyerf943c742014-05-18 23:14:20 -0700491 if (val == MXT_FRAME_CRC_CHECK) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700492 goto recheck;
Nick Dyerf943c742014-05-18 23:14:20 -0700493 } else if (val == MXT_FRAME_CRC_FAIL) {
494 dev_err(dev, "Bootloader CRC fail\n");
495 return -EINVAL;
496 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497 break;
498 default:
499 return -EINVAL;
500 }
501
502 if (val != state) {
Nick Dyerf28a8422014-05-18 23:10:49 -0700503 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
Nick Dyer7bed6802014-05-18 23:04:09 -0700504 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700505 return -EINVAL;
506 }
507
508 return 0;
509}
510
Nick Dyerf28a8422014-05-18 23:10:49 -0700511static int mxt_unlock_bootloader(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700512{
Nick Dyerf28a8422014-05-18 23:10:49 -0700513 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514 u8 buf[2];
515
Iiro Valkonen7686b102011-02-02 23:21:58 -0800516 buf[0] = MXT_UNLOCK_CMD_LSB;
517 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700518
Nick Dyerf28a8422014-05-18 23:10:49 -0700519 ret = mxt_bootloader_write(data, buf, 2);
520 if (ret)
521 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522
523 return 0;
524}
525
Iiro Valkonen7686b102011-02-02 23:21:58 -0800526static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700527 u16 reg, u16 len, void *val)
528{
529 struct i2c_msg xfer[2];
530 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800531 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700532
533 buf[0] = reg & 0xff;
534 buf[1] = (reg >> 8) & 0xff;
535
536 /* Write register */
537 xfer[0].addr = client->addr;
538 xfer[0].flags = 0;
539 xfer[0].len = 2;
540 xfer[0].buf = buf;
541
542 /* Read data */
543 xfer[1].addr = client->addr;
544 xfer[1].flags = I2C_M_RD;
545 xfer[1].len = len;
546 xfer[1].buf = val;
547
Daniel Kurtz771733e2012-06-28 21:08:11 +0800548 ret = i2c_transfer(client->adapter, xfer, 2);
549 if (ret == 2) {
550 ret = 0;
551 } else {
552 if (ret >= 0)
553 ret = -EIO;
554 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
555 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700556 }
557
Daniel Kurtz771733e2012-06-28 21:08:11 +0800558 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700559}
560
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800561static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
562 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700563{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800564 u8 *buf;
565 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800566 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700567
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800568 count = len + 2;
569 buf = kmalloc(count, GFP_KERNEL);
570 if (!buf)
571 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572
573 buf[0] = reg & 0xff;
574 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800575 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700576
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800577 ret = i2c_master_send(client, buf, count);
578 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800579 ret = 0;
580 } else {
581 if (ret >= 0)
582 ret = -EIO;
583 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
584 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700585 }
586
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800587 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800588 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589}
590
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800591static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800593 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594}
595
Iiro Valkonen7686b102011-02-02 23:21:58 -0800596static struct mxt_object *
597mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700598{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800599 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600 int i;
601
602 for (i = 0; i < data->info.object_num; i++) {
603 object = data->object_table + i;
604 if (object->type == type)
605 return object;
606 }
607
Nick Dyer7bed6802014-05-18 23:04:09 -0700608 dev_err(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609 return NULL;
610}
611
Iiro Valkonen7686b102011-02-02 23:21:58 -0800612static int mxt_read_message(struct mxt_data *data,
613 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700614{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800615 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616 u16 reg;
617
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700618 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700619 if (!object)
620 return -EINVAL;
621
622 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623 return __mxt_read_reg(data->client, reg,
624 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625}
626
Iiro Valkonen7686b102011-02-02 23:21:58 -0800627static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700628 u8 type, u8 offset, u8 val)
629{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800630 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631 u16 reg;
632
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633 object = mxt_get_object(data, type);
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700634 if (!object || offset >= mxt_obj_size(object))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635 return -EINVAL;
636
637 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800638 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639}
640
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800641static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
642{
643 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700644 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800645 bool button;
646 int i;
647
648 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700649 for (i = 0; i < pdata->t19_num_keys; i++) {
650 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800651 continue;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700652 button = !(message->message[0] & (1 << i));
653 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800654 }
655}
656
Nick Dyereef820d2014-05-18 23:22:22 -0700657static void mxt_input_sync(struct input_dev *input_dev)
658{
659 input_mt_report_pointer_emulation(input_dev, false);
660 input_sync(input_dev);
661}
662
Iiro Valkonen7686b102011-02-02 23:21:58 -0800663static void mxt_input_touchevent(struct mxt_data *data,
664 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666 struct device *dev = &data->client->dev;
667 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800668 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669 int x;
670 int y;
671 int area;
Nick Dyerfea9e462014-05-18 23:21:48 -0700672 int amplitude;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700673
Joonyoung Shim910d8052011-04-12 23:14:38 -0700674 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
675 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Nick Dyereef820d2014-05-18 23:22:22 -0700676
677 /* Handle 10/12 bit switching */
Joonyoung Shim910d8052011-04-12 23:14:38 -0700678 if (data->max_x < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700679 x >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700680 if (data->max_y < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700681 y >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700682
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700683 area = message->message[4];
Nick Dyerfea9e462014-05-18 23:21:48 -0700684 amplitude = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700685
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800686 dev_dbg(dev,
687 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
688 id,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700689 (status & MXT_T9_DETECT) ? 'D' : '.',
690 (status & MXT_T9_PRESS) ? 'P' : '.',
691 (status & MXT_T9_RELEASE) ? 'R' : '.',
692 (status & MXT_T9_MOVE) ? 'M' : '.',
693 (status & MXT_T9_VECTOR) ? 'V' : '.',
694 (status & MXT_T9_AMP) ? 'A' : '.',
695 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
696 (status & MXT_T9_UNGRIP) ? 'U' : '.',
Nick Dyerfea9e462014-05-18 23:21:48 -0700697 x, y, area, amplitude);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800699 input_mt_slot(input_dev, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700
Nick Dyerf3889ed2014-05-18 23:22:04 -0700701 if (status & MXT_T9_DETECT) {
Nick Dyereef820d2014-05-18 23:22:22 -0700702 /*
703 * Multiple bits may be set if the host is slow to read
704 * the status messages, indicating all the events that
705 * have happened.
706 */
707 if (status & MXT_T9_RELEASE) {
708 input_mt_report_slot_state(input_dev,
709 MT_TOOL_FINGER, 0);
710 mxt_input_sync(input_dev);
711 }
712
713 /* Touch active */
714 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800715 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
716 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
Nick Dyerfea9e462014-05-18 23:21:48 -0700717 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800718 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
Nick Dyereef820d2014-05-18 23:22:22 -0700719 } else {
720 /* Touch no longer active, close out slot */
721 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800722 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700723}
724
Nick Dyerc3f78042014-05-18 23:04:46 -0700725static u16 mxt_extract_T6_csum(const u8 *csum)
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800726{
727 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
728}
729
Daniel Kurtz04a79182012-06-28 21:08:21 +0800730static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
731{
732 u8 id = msg->reportid;
733 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700734}
735
Benson Leungd79e7e42014-05-18 23:02:52 -0700736static irqreturn_t mxt_process_messages_until_invalid(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700737{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800738 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800739 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700740 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700741 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800742 bool update_input = false;
Nick Dyerc3f78042014-05-18 23:04:46 -0700743 u32 crc;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700744
745 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800746 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700748 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749 }
750
751 reportid = message.reportid;
752
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800753 if (reportid == data->T6_reportid) {
754 u8 status = payload[0];
Nick Dyerc3f78042014-05-18 23:04:46 -0700755
756 crc = mxt_extract_T6_csum(&payload[1]);
757 if (crc != data->config_crc) {
758 data->config_crc = crc;
759 complete(&data->crc_completion);
760 }
761
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800762 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
Nick Dyerc3f78042014-05-18 23:04:46 -0700763 status, data->config_crc);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700764
765 if (status & MXT_T6_STATUS_RESET)
766 complete(&data->reset_completion);
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700767 } else if (!data->input_dev) {
768 /*
769 * do not report events if input device
770 * is not yet registered
771 */
772 mxt_dump_message(dev, &message);
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800773 } else if (mxt_is_T9_message(data, &message)) {
774 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800776 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800777 } else if (message.reportid == data->T19_reportid) {
778 mxt_input_button(data, &message);
779 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800780 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800781 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800782 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783 } while (reportid != 0xff);
784
Nick Dyereef820d2014-05-18 23:22:22 -0700785 if (update_input)
786 mxt_input_sync(data->input_dev);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800787
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788 return IRQ_HANDLED;
789}
790
Benson Leungd79e7e42014-05-18 23:02:52 -0700791static irqreturn_t mxt_interrupt(int irq, void *dev_id)
792{
793 struct mxt_data *data = dev_id;
794
795 if (data->in_bootloader) {
796 /* bootloader state transition completion */
797 complete(&data->bl_completion);
798 return IRQ_HANDLED;
799 }
800
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700801 if (!data->object_table)
802 return IRQ_HANDLED;
803
Benson Leungd79e7e42014-05-18 23:02:52 -0700804 return mxt_process_messages_until_invalid(data);
805}
806
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700807static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
808 u8 value, bool wait)
809{
810 u16 reg;
811 u8 command_register;
812 int timeout_counter = 0;
813 int ret;
814
815 reg = data->T6_address + cmd_offset;
816
817 ret = mxt_write_reg(data->client, reg, value);
818 if (ret)
819 return ret;
820
821 if (!wait)
822 return 0;
823
824 do {
825 msleep(20);
826 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
827 if (ret)
828 return ret;
829 } while (command_register != 0 && timeout_counter++ <= 100);
830
831 if (timeout_counter > 100) {
832 dev_err(&data->client->dev, "Command failed!\n");
833 return -EIO;
834 }
835
836 return 0;
837}
838
839static int mxt_soft_reset(struct mxt_data *data)
840{
841 struct device *dev = &data->client->dev;
842 int ret = 0;
843
844 dev_info(dev, "Resetting chip\n");
845
846 reinit_completion(&data->reset_completion);
847
848 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
849 if (ret)
850 return ret;
851
852 ret = mxt_wait_for_completion(data, &data->reset_completion,
853 MXT_RESET_TIMEOUT);
854 if (ret)
855 return ret;
856
857 return 0;
858}
859
Nick Dyerc3f78042014-05-18 23:04:46 -0700860static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
861{
862 /*
863 * On failure, CRC is set to 0 and config will always be
864 * downloaded.
865 */
866 data->config_crc = 0;
867 reinit_completion(&data->crc_completion);
868
869 mxt_t6_command(data, cmd, value, true);
870
871 /*
872 * Wait for crc message. On failure, CRC is set to 0 and config will
873 * always be downloaded.
874 */
875 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
876}
877
Iiro Valkonen7686b102011-02-02 23:21:58 -0800878static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700879{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800880 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800881 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700882 struct device *dev = &data->client->dev;
883 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800884 int i, size;
885 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700886
Iiro Valkonen71749f52011-02-15 13:36:52 -0800887 if (!pdata->config) {
888 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
889 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700890 }
891
Nick Dyerc3f78042014-05-18 23:04:46 -0700892 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
893
894 if (data->config_crc == pdata->config_crc) {
895 dev_info(dev, "Config CRC 0x%06X: OK\n", data->config_crc);
896 return 0;
897 }
898
899 dev_info(dev, "Config CRC 0x%06X: does not match 0x%06X\n",
900 data->config_crc, pdata->config_crc);
901
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700902 for (i = 0; i < data->info.object_num; i++) {
903 object = data->object_table + i;
904
Iiro Valkonen7686b102011-02-02 23:21:58 -0800905 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700906 continue;
907
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700908 size = mxt_obj_size(object) * mxt_obj_instances(object);
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800909 if (index + size > pdata->config_length) {
910 dev_err(dev, "Not enough config data!\n");
911 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800912 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800913
914 ret = __mxt_write_reg(data->client, object->start_address,
915 size, &pdata->config[index]);
916 if (ret)
917 return ret;
918 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700919 }
920
Nick Dyerc3f78042014-05-18 23:04:46 -0700921 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
922
923 ret = mxt_soft_reset(data);
924 if (ret)
925 return ret;
926
927 dev_info(dev, "Config successfully updated\n");
928
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929 return 0;
930}
931
Iiro Valkonen7686b102011-02-02 23:21:58 -0800932static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700933{
934 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800935 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700936 int count = 10;
937 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700938
939 /* Read dummy message to make high CHG pin */
940 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800941 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700942 if (error)
943 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800944 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700945
946 if (!count) {
947 dev_err(dev, "CHG pin isn't cleared\n");
948 return -EBUSY;
949 }
950
951 return 0;
952}
953
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700954static int mxt_acquire_irq(struct mxt_data *data)
955{
956 int error;
957
958 enable_irq(data->irq);
959
960 error = mxt_make_highchg(data);
961 if (error)
962 return error;
963
964 return 0;
965}
966
Iiro Valkonen7686b102011-02-02 23:21:58 -0800967static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700968{
969 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800970 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700971 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972
Daniel Kurtz23003a842012-06-28 21:08:14 +0800973 /* Read 7-byte info block starting at address 0 */
974 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700975 if (error)
976 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700977
978 return 0;
979}
980
Iiro Valkonen7686b102011-02-02 23:21:58 -0800981static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700982{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800983 struct i2c_client *client = data->client;
984 size_t table_size;
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700985 struct mxt_object *object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700986 int error;
987 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800988 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700989
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800990 table_size = data->info.object_num * sizeof(struct mxt_object);
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700991 object_table = kzalloc(table_size, GFP_KERNEL);
992 if (!object_table) {
993 dev_err(&data->client->dev, "Failed to allocate memory\n");
994 return -ENOMEM;
995 }
996
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800997 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700998 object_table);
999 if (error) {
1000 kfree(object_table);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001001 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001002 }
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001003
1004 /* Valid Report IDs start counting from 1 */
1005 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006 for (i = 0; i < data->info.object_num; i++) {
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001007 struct mxt_object *object = object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001008 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001010 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001011
1012 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001013 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001014 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001015 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001016 max_id = reportid - 1;
1017 } else {
1018 min_id = 0;
1019 max_id = 0;
1020 }
1021
1022 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001023 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001024 object->type, object->start_address,
1025 mxt_obj_size(object), mxt_obj_instances(object),
1026 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001027
1028 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001029 case MXT_GEN_COMMAND_T6:
1030 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001031 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001032 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001033 case MXT_TOUCH_MULTI_T9:
1034 data->T9_reportid_min = min_id;
1035 data->T9_reportid_max = max_id;
1036 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001037 case MXT_SPT_GPIOPWM_T19:
1038 data->T19_reportid = min_id;
1039 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040 }
1041 }
1042
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001043 data->object_table = object_table;
1044
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045 return 0;
1046}
1047
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001048static void mxt_free_object_table(struct mxt_data *data)
1049{
1050 kfree(data->object_table);
1051 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001052 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001053 data->T9_reportid_min = 0;
1054 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001055 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001056}
1057
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001058static int mxt_read_t9_resolution(struct mxt_data *data)
1059{
1060 struct i2c_client *client = data->client;
1061 int error;
1062 struct t9_range range;
1063 unsigned char orient;
1064 struct mxt_object *object;
1065
1066 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1067 if (!object)
1068 return -EINVAL;
1069
1070 error = __mxt_read_reg(client,
1071 object->start_address + MXT_T9_RANGE,
1072 sizeof(range), &range);
1073 if (error)
1074 return error;
1075
1076 le16_to_cpus(&range.x);
1077 le16_to_cpus(&range.y);
1078
1079 error = __mxt_read_reg(client,
1080 object->start_address + MXT_T9_ORIENT,
1081 1, &orient);
1082 if (error)
1083 return error;
1084
1085 /* Handle default values */
1086 if (range.x == 0)
1087 range.x = 1023;
1088
1089 if (range.y == 0)
1090 range.y = 1023;
1091
Nick Dyerf3889ed2014-05-18 23:22:04 -07001092 if (orient & MXT_T9_ORIENT_SWITCH) {
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001093 data->max_x = range.y;
1094 data->max_y = range.x;
1095 } else {
1096 data->max_x = range.x;
1097 data->max_y = range.y;
1098 }
1099
1100 dev_dbg(&client->dev,
1101 "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1102
1103 return 0;
1104}
1105
Iiro Valkonen7686b102011-02-02 23:21:58 -08001106static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001107{
1108 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001109 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001110 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111
Iiro Valkonen7686b102011-02-02 23:21:58 -08001112 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001113 if (error)
1114 return error;
1115
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001116 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001117 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001118 if (error) {
1119 dev_err(&client->dev, "Error %d reading object table\n", error);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001120 return error;
Nick Dyer7bed6802014-05-18 23:04:09 -07001121 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001122
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001123 mxt_acquire_irq(data);
1124 if (error)
1125 goto err_free_object_table;
1126
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001127 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001128 error = mxt_check_reg_init(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001129 if (error) {
1130 dev_err(&client->dev, "Error %d initializing configuration\n",
1131 error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001132 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -07001133 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001134
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001135 error = mxt_read_t9_resolution(data);
1136 if (error) {
1137 dev_err(&client->dev, "Failed to initialize T9 resolution\n");
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001138 goto err_free_object_table;
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001139 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140
1141 dev_info(&client->dev,
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001142 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1143 info->family_id, info->variant_id, info->version >> 4,
1144 info->version & 0xf, info->build, info->object_num);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001145
1146 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001147
1148err_free_object_table:
1149 mxt_free_object_table(data);
1150 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001151}
1152
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001153/* Firmware Version is returned as Major.Minor.Build */
1154static ssize_t mxt_fw_version_show(struct device *dev,
1155 struct device_attribute *attr, char *buf)
1156{
1157 struct mxt_data *data = dev_get_drvdata(dev);
1158 struct mxt_info *info = &data->info;
1159 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
1160 info->version >> 4, info->version & 0xf, info->build);
1161}
1162
1163/* Hardware Version is returned as FamilyID.VariantID */
1164static ssize_t mxt_hw_version_show(struct device *dev,
1165 struct device_attribute *attr, char *buf)
1166{
1167 struct mxt_data *data = dev_get_drvdata(dev);
1168 struct mxt_info *info = &data->info;
1169 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
1170 info->family_id, info->variant_id);
1171}
1172
Daniel Kurtz794eb672012-06-28 21:08:10 +08001173static ssize_t mxt_show_instance(char *buf, int count,
1174 struct mxt_object *object, int instance,
1175 const u8 *val)
1176{
1177 int i;
1178
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001179 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001180 count += scnprintf(buf + count, PAGE_SIZE - count,
1181 "Instance %u\n", instance);
1182
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001183 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001184 count += scnprintf(buf + count, PAGE_SIZE - count,
1185 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
1186 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
1187
1188 return count;
1189}
1190
Iiro Valkonen7686b102011-02-02 23:21:58 -08001191static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001192 struct device_attribute *attr, char *buf)
1193{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001194 struct mxt_data *data = dev_get_drvdata(dev);
1195 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001196 int count = 0;
1197 int i, j;
1198 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001199 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001200
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001201 /* Pre-allocate buffer large enough to hold max sized object. */
1202 obuf = kmalloc(256, GFP_KERNEL);
1203 if (!obuf)
1204 return -ENOMEM;
1205
1206 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001207 for (i = 0; i < data->info.object_num; i++) {
1208 object = data->object_table + i;
1209
Daniel Kurtz91630952012-06-28 21:08:09 +08001210 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001211 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001212
Daniel Kurtz91630952012-06-28 21:08:09 +08001213 count += scnprintf(buf + count, PAGE_SIZE - count,
1214 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001215
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001216 for (j = 0; j < mxt_obj_instances(object); j++) {
1217 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08001218 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001219
Daniel Kurtz794eb672012-06-28 21:08:10 +08001220 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001221 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001222 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001223
Daniel Kurtz794eb672012-06-28 21:08:10 +08001224 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001225 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001226 }
1227
Daniel Kurtz794eb672012-06-28 21:08:10 +08001228done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001229 kfree(obuf);
1230 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001231}
1232
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001233static int mxt_check_firmware_format(struct device *dev,
1234 const struct firmware *fw)
1235{
1236 unsigned int pos = 0;
1237 char c;
1238
1239 while (pos < fw->size) {
1240 c = *(fw->data + pos);
1241
1242 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
1243 return 0;
1244
1245 pos++;
1246 }
1247
1248 /*
1249 * To convert file try:
1250 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
1251 */
1252 dev_err(dev, "Aborting: firmware file must be in binary format\n");
1253
1254 return -EINVAL;
1255}
1256
Iiro Valkonen7686b102011-02-02 23:21:58 -08001257static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001258{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001259 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001260 const struct firmware *fw = NULL;
1261 unsigned int frame_size;
1262 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07001263 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07001264 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265 int ret;
1266
1267 ret = request_firmware(&fw, fn, dev);
1268 if (ret) {
1269 dev_err(dev, "Unable to open firmware %s\n", fn);
1270 return ret;
1271 }
1272
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001273 /* Check for incorrect enc file */
1274 ret = mxt_check_firmware_format(dev, fw);
1275 if (ret)
1276 goto release_firmware;
1277
Nick Dyerf28a8422014-05-18 23:10:49 -07001278 ret = mxt_lookup_bootloader_address(data);
1279 if (ret)
1280 goto release_firmware;
1281
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001282 /* Change to the bootloader mode */
Benson Leungd79e7e42014-05-18 23:02:52 -07001283 data->in_bootloader = true;
1284
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001285 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_BOOT_VALUE, false);
1286 if (ret)
1287 goto release_firmware;
1288
Iiro Valkonen7686b102011-02-02 23:21:58 -08001289 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001290
Benson Leungd79e7e42014-05-18 23:02:52 -07001291 reinit_completion(&data->bl_completion);
1292
1293 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001294 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001295 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001296
1297 /* Unlock bootloader */
Nick Dyerf28a8422014-05-18 23:10:49 -07001298 mxt_unlock_bootloader(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001299
1300 while (pos < fw->size) {
Benson Leungd79e7e42014-05-18 23:02:52 -07001301 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001302 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001303 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001304
1305 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1306
Nick Dyerf943c742014-05-18 23:14:20 -07001307 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001308 frame_size += 2;
1309
1310 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07001311 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
1312 if (ret)
1313 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001314
Benson Leungd79e7e42014-05-18 23:02:52 -07001315 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
Nick Dyerf943c742014-05-18 23:14:20 -07001316 if (ret) {
1317 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001318
Nick Dyerf943c742014-05-18 23:14:20 -07001319 /* Back off by 20ms per retry */
1320 msleep(retry * 20);
1321
1322 if (retry > 20) {
1323 dev_err(dev, "Retry count exceeded\n");
1324 goto disable_irq;
1325 }
1326 } else {
1327 retry = 0;
1328 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07001329 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07001330 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001331
Nick Dyerf477c752014-05-18 23:14:45 -07001332 if (frame % 50 == 0)
1333 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
1334 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001335 }
1336
Benson Leunga0434b72014-05-18 23:03:09 -07001337 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001338 ret = mxt_wait_for_completion(data, &data->bl_completion,
1339 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001340 if (ret)
1341 goto disable_irq;
1342
Nick Dyerf477c752014-05-18 23:14:45 -07001343 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
1344
Benson Leunga0434b72014-05-18 23:03:09 -07001345 /*
1346 * Wait for device to reset. Some bootloader versions do not assert
1347 * the CHG line after bootloading has finished, so ignore potential
1348 * errors.
1349 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001350 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001351
Benson Leungd79e7e42014-05-18 23:02:52 -07001352 data->in_bootloader = false;
1353
1354disable_irq:
1355 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001356release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001357 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358 return ret;
1359}
1360
Iiro Valkonen7686b102011-02-02 23:21:58 -08001361static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001362 struct device_attribute *attr,
1363 const char *buf, size_t count)
1364{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001365 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001366 int error;
1367
Iiro Valkonen7686b102011-02-02 23:21:58 -08001368 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001369 if (error) {
1370 dev_err(dev, "The firmware update failed(%d)\n", error);
1371 count = error;
1372 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07001373 dev_info(dev, "The firmware update succeeded\n");
1374
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001375 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001376
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001377 error = mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07001378 if (error)
1379 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001380 }
1381
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001382 return count;
1383}
1384
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001385static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1386static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001387static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1388static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389
Iiro Valkonen7686b102011-02-02 23:21:58 -08001390static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001391 &dev_attr_fw_version.attr,
1392 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001393 &dev_attr_object.attr,
1394 &dev_attr_update_fw.attr,
1395 NULL
1396};
1397
Iiro Valkonen7686b102011-02-02 23:21:58 -08001398static const struct attribute_group mxt_attr_group = {
1399 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001400};
1401
Iiro Valkonen7686b102011-02-02 23:21:58 -08001402static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001403{
1404 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001405 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001406 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001407}
1408
Iiro Valkonen7686b102011-02-02 23:21:58 -08001409static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001410{
1411 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001412 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001413 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001414}
1415
Iiro Valkonen7686b102011-02-02 23:21:58 -08001416static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001417{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001418 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001419
Iiro Valkonen7686b102011-02-02 23:21:58 -08001420 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001421
1422 return 0;
1423}
1424
Iiro Valkonen7686b102011-02-02 23:21:58 -08001425static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001426{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001427 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001428
Iiro Valkonen7686b102011-02-02 23:21:58 -08001429 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001430}
1431
Bill Pemberton5298cc42012-11-23 21:38:25 -08001432static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001433 const struct i2c_device_id *id)
1434{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001435 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001436 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001437 struct input_dev *input_dev;
1438 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001439 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001440 unsigned int mt_flags = 0;
1441 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001442
Iiro Valkonen919ed892011-02-15 13:36:52 -08001443 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001444 return -EINVAL;
1445
Iiro Valkonen7686b102011-02-02 23:21:58 -08001446 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001447 input_dev = input_allocate_device();
1448 if (!data || !input_dev) {
1449 dev_err(&client->dev, "Failed to allocate memory\n");
1450 error = -ENOMEM;
1451 goto err_free_mem;
1452 }
1453
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001454 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001455 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1456 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001457
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001458 input_dev->phys = data->phys;
1459
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001460 input_dev->id.bustype = BUS_I2C;
1461 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001462 input_dev->open = mxt_input_open;
1463 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001464
Joonyoung Shim910d8052011-04-12 23:14:38 -07001465 data->client = client;
1466 data->input_dev = input_dev;
1467 data->pdata = pdata;
1468 data->irq = client->irq;
1469
Benson Leungd79e7e42014-05-18 23:02:52 -07001470 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001471 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07001472 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07001473
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001474 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
1475 pdata->irqflags | IRQF_ONESHOT,
1476 client->name, data);
1477 if (error) {
1478 dev_err(&client->dev, "Failed to register interrupt\n");
1479 goto err_free_mem;
1480 }
1481
1482 disable_irq(client->irq);
1483
1484 error = input_register_device(input_dev);
1485 if (error) {
1486 dev_err(&client->dev, "Error %d registering input device\n",
1487 error);
1488 goto err_free_irq;
1489 }
1490
Daniel Kurtzcb159112012-06-28 21:08:22 +08001491 error = mxt_initialize(data);
1492 if (error)
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001493 goto err_unregister_device;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001494
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001495 __set_bit(EV_ABS, input_dev->evbit);
1496 __set_bit(EV_KEY, input_dev->evbit);
1497 __set_bit(BTN_TOUCH, input_dev->keybit);
1498
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001499 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001500 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1501
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001502 for (i = 0; i < pdata->t19_num_keys; i++)
1503 if (pdata->t19_keymap[i] != KEY_RESERVED)
1504 input_set_capability(input_dev, EV_KEY,
1505 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001506
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001507 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001508
1509 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1510 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1511 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1512 MXT_PIXELS_PER_MM);
1513 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1514 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001515
1516 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001517 }
1518
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001519 /* For single touch */
1520 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001521 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001522 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001523 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001524 input_set_abs_params(input_dev, ABS_PRESSURE,
1525 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001526
1527 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001528 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001529 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001530 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001531 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001532 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001533 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001534 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001535 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001536 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001537 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001538 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1539 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001540
1541 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001542 i2c_set_clientdata(client, data);
1543
Iiro Valkonen7686b102011-02-02 23:21:58 -08001544 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07001545 if (error) {
1546 dev_err(&client->dev, "Failure %d creating sysfs group\n",
1547 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001548 goto err_unregister_device;
Nick Dyer7bed6802014-05-18 23:04:09 -07001549 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001550
1551 return 0;
1552
1553err_unregister_device:
1554 input_unregister_device(input_dev);
1555 input_dev = NULL;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001556err_free_object:
1557 kfree(data->object_table);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001558err_free_irq:
1559 free_irq(client->irq, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001560err_free_mem:
1561 input_free_device(input_dev);
1562 kfree(data);
1563 return error;
1564}
1565
Bill Pembertone2619cf2012-11-23 21:50:47 -08001566static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001567{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001568 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001569
Iiro Valkonen7686b102011-02-02 23:21:58 -08001570 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001571 free_irq(data->irq, data);
1572 input_unregister_device(data->input_dev);
1573 kfree(data->object_table);
1574 kfree(data);
1575
1576 return 0;
1577}
1578
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001579#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001580static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001581{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001582 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001583 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001584 struct input_dev *input_dev = data->input_dev;
1585
1586 mutex_lock(&input_dev->mutex);
1587
1588 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001589 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001590
1591 mutex_unlock(&input_dev->mutex);
1592
1593 return 0;
1594}
1595
Iiro Valkonen7686b102011-02-02 23:21:58 -08001596static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001597{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001598 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001599 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001600 struct input_dev *input_dev = data->input_dev;
1601
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001602 mxt_soft_reset(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001603
1604 mutex_lock(&input_dev->mutex);
1605
1606 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001607 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001608
1609 mutex_unlock(&input_dev->mutex);
1610
1611 return 0;
1612}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001613#endif
1614
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001615static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1616
Iiro Valkonen7686b102011-02-02 23:21:58 -08001617static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001618 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001619 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001620 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001621 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001622 { }
1623};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001624MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001625
Iiro Valkonen7686b102011-02-02 23:21:58 -08001626static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001627 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001628 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001629 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001630 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001631 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001632 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001633 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001634 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001635};
1636
Axel Lin1b92c1c2012-03-16 23:05:41 -07001637module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001638
1639/* Module information */
1640MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001641MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001642MODULE_LICENSE("GPL");