blob: 6e0b4a2120d3ece91d59c9fc23e50089dc28fa82 [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);
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800767 } else if (mxt_is_T9_message(data, &message)) {
768 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800769 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800770 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800771 } else if (message.reportid == data->T19_reportid) {
772 mxt_input_button(data, &message);
773 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800774 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800775 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800776 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700777 } while (reportid != 0xff);
778
Nick Dyereef820d2014-05-18 23:22:22 -0700779 if (update_input)
780 mxt_input_sync(data->input_dev);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800781
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700782 return IRQ_HANDLED;
783}
784
Benson Leungd79e7e42014-05-18 23:02:52 -0700785static irqreturn_t mxt_interrupt(int irq, void *dev_id)
786{
787 struct mxt_data *data = dev_id;
788
789 if (data->in_bootloader) {
790 /* bootloader state transition completion */
791 complete(&data->bl_completion);
792 return IRQ_HANDLED;
793 }
794
795 return mxt_process_messages_until_invalid(data);
796}
797
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700798static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
799 u8 value, bool wait)
800{
801 u16 reg;
802 u8 command_register;
803 int timeout_counter = 0;
804 int ret;
805
806 reg = data->T6_address + cmd_offset;
807
808 ret = mxt_write_reg(data->client, reg, value);
809 if (ret)
810 return ret;
811
812 if (!wait)
813 return 0;
814
815 do {
816 msleep(20);
817 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
818 if (ret)
819 return ret;
820 } while (command_register != 0 && timeout_counter++ <= 100);
821
822 if (timeout_counter > 100) {
823 dev_err(&data->client->dev, "Command failed!\n");
824 return -EIO;
825 }
826
827 return 0;
828}
829
830static int mxt_soft_reset(struct mxt_data *data)
831{
832 struct device *dev = &data->client->dev;
833 int ret = 0;
834
835 dev_info(dev, "Resetting chip\n");
836
837 reinit_completion(&data->reset_completion);
838
839 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
840 if (ret)
841 return ret;
842
843 ret = mxt_wait_for_completion(data, &data->reset_completion,
844 MXT_RESET_TIMEOUT);
845 if (ret)
846 return ret;
847
848 return 0;
849}
850
Nick Dyerc3f78042014-05-18 23:04:46 -0700851static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
852{
853 /*
854 * On failure, CRC is set to 0 and config will always be
855 * downloaded.
856 */
857 data->config_crc = 0;
858 reinit_completion(&data->crc_completion);
859
860 mxt_t6_command(data, cmd, value, true);
861
862 /*
863 * Wait for crc message. On failure, CRC is set to 0 and config will
864 * always be downloaded.
865 */
866 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
867}
868
Iiro Valkonen7686b102011-02-02 23:21:58 -0800869static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700870{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800871 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800872 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700873 struct device *dev = &data->client->dev;
874 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800875 int i, size;
876 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700877
Iiro Valkonen71749f52011-02-15 13:36:52 -0800878 if (!pdata->config) {
879 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
880 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700881 }
882
Nick Dyerc3f78042014-05-18 23:04:46 -0700883 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
884
885 if (data->config_crc == pdata->config_crc) {
886 dev_info(dev, "Config CRC 0x%06X: OK\n", data->config_crc);
887 return 0;
888 }
889
890 dev_info(dev, "Config CRC 0x%06X: does not match 0x%06X\n",
891 data->config_crc, pdata->config_crc);
892
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893 for (i = 0; i < data->info.object_num; i++) {
894 object = data->object_table + i;
895
Iiro Valkonen7686b102011-02-02 23:21:58 -0800896 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700897 continue;
898
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700899 size = mxt_obj_size(object) * mxt_obj_instances(object);
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800900 if (index + size > pdata->config_length) {
901 dev_err(dev, "Not enough config data!\n");
902 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800903 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800904
905 ret = __mxt_write_reg(data->client, object->start_address,
906 size, &pdata->config[index]);
907 if (ret)
908 return ret;
909 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700910 }
911
Nick Dyerc3f78042014-05-18 23:04:46 -0700912 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
913
914 ret = mxt_soft_reset(data);
915 if (ret)
916 return ret;
917
918 dev_info(dev, "Config successfully updated\n");
919
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700920 return 0;
921}
922
Iiro Valkonen7686b102011-02-02 23:21:58 -0800923static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700924{
925 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800926 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700927 int count = 10;
928 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929
930 /* Read dummy message to make high CHG pin */
931 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800932 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700933 if (error)
934 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800935 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700936
937 if (!count) {
938 dev_err(dev, "CHG pin isn't cleared\n");
939 return -EBUSY;
940 }
941
942 return 0;
943}
944
Iiro Valkonen7686b102011-02-02 23:21:58 -0800945static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946{
947 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800948 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700949 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700950
Daniel Kurtz23003a842012-06-28 21:08:14 +0800951 /* Read 7-byte info block starting at address 0 */
952 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953 if (error)
954 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955
956 return 0;
957}
958
Iiro Valkonen7686b102011-02-02 23:21:58 -0800959static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700960{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800961 struct i2c_client *client = data->client;
962 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700963 int error;
964 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800965 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700966
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800967 table_size = data->info.object_num * sizeof(struct mxt_object);
968 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
969 data->object_table);
970 if (error)
971 return error;
972
973 /* Valid Report IDs start counting from 1 */
974 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700975 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800976 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800977 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700978
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800979 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980
981 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800982 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700984 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800985 max_id = reportid - 1;
986 } else {
987 min_id = 0;
988 max_id = 0;
989 }
990
991 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -0700992 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700993 object->type, object->start_address,
994 mxt_obj_size(object), mxt_obj_instances(object),
995 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800996
997 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800998 case MXT_GEN_COMMAND_T6:
999 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001000 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001001 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001002 case MXT_TOUCH_MULTI_T9:
1003 data->T9_reportid_min = min_id;
1004 data->T9_reportid_max = max_id;
1005 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001006 case MXT_SPT_GPIOPWM_T19:
1007 data->T19_reportid = min_id;
1008 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001009 }
1010 }
1011
1012 return 0;
1013}
1014
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001015static void mxt_free_object_table(struct mxt_data *data)
1016{
1017 kfree(data->object_table);
1018 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001019 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001020 data->T9_reportid_min = 0;
1021 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001022 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001023}
1024
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001025static int mxt_read_t9_resolution(struct mxt_data *data)
1026{
1027 struct i2c_client *client = data->client;
1028 int error;
1029 struct t9_range range;
1030 unsigned char orient;
1031 struct mxt_object *object;
1032
1033 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1034 if (!object)
1035 return -EINVAL;
1036
1037 error = __mxt_read_reg(client,
1038 object->start_address + MXT_T9_RANGE,
1039 sizeof(range), &range);
1040 if (error)
1041 return error;
1042
1043 le16_to_cpus(&range.x);
1044 le16_to_cpus(&range.y);
1045
1046 error = __mxt_read_reg(client,
1047 object->start_address + MXT_T9_ORIENT,
1048 1, &orient);
1049 if (error)
1050 return error;
1051
1052 /* Handle default values */
1053 if (range.x == 0)
1054 range.x = 1023;
1055
1056 if (range.y == 0)
1057 range.y = 1023;
1058
Nick Dyerf3889ed2014-05-18 23:22:04 -07001059 if (orient & MXT_T9_ORIENT_SWITCH) {
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001060 data->max_x = range.y;
1061 data->max_y = range.x;
1062 } else {
1063 data->max_x = range.x;
1064 data->max_y = range.y;
1065 }
1066
1067 dev_dbg(&client->dev,
1068 "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1069
1070 return 0;
1071}
1072
Iiro Valkonen7686b102011-02-02 23:21:58 -08001073static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001074{
1075 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001076 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001077 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001078
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001080 if (error)
1081 return error;
1082
1083 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001084 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001085 GFP_KERNEL);
1086 if (!data->object_table) {
1087 dev_err(&client->dev, "Failed to allocate memory\n");
1088 return -ENOMEM;
1089 }
1090
1091 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001092 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001093 if (error) {
1094 dev_err(&client->dev, "Error %d reading object table\n", error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001095 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -07001096 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001097
1098 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001099 error = mxt_check_reg_init(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001100 if (error) {
1101 dev_err(&client->dev, "Error %d initializing configuration\n",
1102 error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001103 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -07001104 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001105
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001106 error = mxt_read_t9_resolution(data);
1107 if (error) {
1108 dev_err(&client->dev, "Failed to initialize T9 resolution\n");
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001109 goto err_free_object_table;
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001110 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111
1112 dev_info(&client->dev,
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001113 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1114 info->family_id, info->variant_id, info->version >> 4,
1115 info->version & 0xf, info->build, info->object_num);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001116
1117 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001118
1119err_free_object_table:
1120 mxt_free_object_table(data);
1121 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001122}
1123
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001124/* Firmware Version is returned as Major.Minor.Build */
1125static ssize_t mxt_fw_version_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1127{
1128 struct mxt_data *data = dev_get_drvdata(dev);
1129 struct mxt_info *info = &data->info;
1130 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
1131 info->version >> 4, info->version & 0xf, info->build);
1132}
1133
1134/* Hardware Version is returned as FamilyID.VariantID */
1135static ssize_t mxt_hw_version_show(struct device *dev,
1136 struct device_attribute *attr, char *buf)
1137{
1138 struct mxt_data *data = dev_get_drvdata(dev);
1139 struct mxt_info *info = &data->info;
1140 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
1141 info->family_id, info->variant_id);
1142}
1143
Daniel Kurtz794eb672012-06-28 21:08:10 +08001144static ssize_t mxt_show_instance(char *buf, int count,
1145 struct mxt_object *object, int instance,
1146 const u8 *val)
1147{
1148 int i;
1149
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001150 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001151 count += scnprintf(buf + count, PAGE_SIZE - count,
1152 "Instance %u\n", instance);
1153
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001154 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001155 count += scnprintf(buf + count, PAGE_SIZE - count,
1156 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
1157 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
1158
1159 return count;
1160}
1161
Iiro Valkonen7686b102011-02-02 23:21:58 -08001162static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001163 struct device_attribute *attr, char *buf)
1164{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001165 struct mxt_data *data = dev_get_drvdata(dev);
1166 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001167 int count = 0;
1168 int i, j;
1169 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001170 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001171
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001172 /* Pre-allocate buffer large enough to hold max sized object. */
1173 obuf = kmalloc(256, GFP_KERNEL);
1174 if (!obuf)
1175 return -ENOMEM;
1176
1177 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001178 for (i = 0; i < data->info.object_num; i++) {
1179 object = data->object_table + i;
1180
Daniel Kurtz91630952012-06-28 21:08:09 +08001181 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001182 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001183
Daniel Kurtz91630952012-06-28 21:08:09 +08001184 count += scnprintf(buf + count, PAGE_SIZE - count,
1185 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001186
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001187 for (j = 0; j < mxt_obj_instances(object); j++) {
1188 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08001189 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001190
Daniel Kurtz794eb672012-06-28 21:08:10 +08001191 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001192 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001193 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001194
Daniel Kurtz794eb672012-06-28 21:08:10 +08001195 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001196 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001197 }
1198
Daniel Kurtz794eb672012-06-28 21:08:10 +08001199done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001200 kfree(obuf);
1201 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001202}
1203
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001204static int mxt_check_firmware_format(struct device *dev,
1205 const struct firmware *fw)
1206{
1207 unsigned int pos = 0;
1208 char c;
1209
1210 while (pos < fw->size) {
1211 c = *(fw->data + pos);
1212
1213 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
1214 return 0;
1215
1216 pos++;
1217 }
1218
1219 /*
1220 * To convert file try:
1221 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
1222 */
1223 dev_err(dev, "Aborting: firmware file must be in binary format\n");
1224
1225 return -EINVAL;
1226}
1227
Iiro Valkonen7686b102011-02-02 23:21:58 -08001228static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001229{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001230 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001231 const struct firmware *fw = NULL;
1232 unsigned int frame_size;
1233 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07001234 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07001235 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001236 int ret;
1237
1238 ret = request_firmware(&fw, fn, dev);
1239 if (ret) {
1240 dev_err(dev, "Unable to open firmware %s\n", fn);
1241 return ret;
1242 }
1243
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001244 /* Check for incorrect enc file */
1245 ret = mxt_check_firmware_format(dev, fw);
1246 if (ret)
1247 goto release_firmware;
1248
Nick Dyerf28a8422014-05-18 23:10:49 -07001249 ret = mxt_lookup_bootloader_address(data);
1250 if (ret)
1251 goto release_firmware;
1252
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001253 /* Change to the bootloader mode */
Benson Leungd79e7e42014-05-18 23:02:52 -07001254 data->in_bootloader = true;
1255
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001256 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_BOOT_VALUE, false);
1257 if (ret)
1258 goto release_firmware;
1259
Iiro Valkonen7686b102011-02-02 23:21:58 -08001260 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001261
Benson Leungd79e7e42014-05-18 23:02:52 -07001262 reinit_completion(&data->bl_completion);
1263
1264 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001266 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001267
1268 /* Unlock bootloader */
Nick Dyerf28a8422014-05-18 23:10:49 -07001269 mxt_unlock_bootloader(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001270
1271 while (pos < fw->size) {
Benson Leungd79e7e42014-05-18 23:02:52 -07001272 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001274 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001275
1276 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1277
Nick Dyerf943c742014-05-18 23:14:20 -07001278 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001279 frame_size += 2;
1280
1281 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07001282 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
1283 if (ret)
1284 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001285
Benson Leungd79e7e42014-05-18 23:02:52 -07001286 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
Nick Dyerf943c742014-05-18 23:14:20 -07001287 if (ret) {
1288 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001289
Nick Dyerf943c742014-05-18 23:14:20 -07001290 /* Back off by 20ms per retry */
1291 msleep(retry * 20);
1292
1293 if (retry > 20) {
1294 dev_err(dev, "Retry count exceeded\n");
1295 goto disable_irq;
1296 }
1297 } else {
1298 retry = 0;
1299 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07001300 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07001301 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001302
Nick Dyerf477c752014-05-18 23:14:45 -07001303 if (frame % 50 == 0)
1304 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
1305 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001306 }
1307
Benson Leunga0434b72014-05-18 23:03:09 -07001308 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001309 ret = mxt_wait_for_completion(data, &data->bl_completion,
1310 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001311 if (ret)
1312 goto disable_irq;
1313
Nick Dyerf477c752014-05-18 23:14:45 -07001314 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
1315
Benson Leunga0434b72014-05-18 23:03:09 -07001316 /*
1317 * Wait for device to reset. Some bootloader versions do not assert
1318 * the CHG line after bootloading has finished, so ignore potential
1319 * errors.
1320 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001321 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001322
Benson Leungd79e7e42014-05-18 23:02:52 -07001323 data->in_bootloader = false;
1324
1325disable_irq:
1326 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001327release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001328 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001329 return ret;
1330}
1331
Iiro Valkonen7686b102011-02-02 23:21:58 -08001332static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001333 struct device_attribute *attr,
1334 const char *buf, size_t count)
1335{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001336 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001337 int error;
1338
Iiro Valkonen7686b102011-02-02 23:21:58 -08001339 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001340 if (error) {
1341 dev_err(dev, "The firmware update failed(%d)\n", error);
1342 count = error;
1343 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07001344 dev_info(dev, "The firmware update succeeded\n");
1345
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001346 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001347
Iiro Valkonen7686b102011-02-02 23:21:58 -08001348 mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07001349
1350 enable_irq(data->irq);
1351
1352 error = mxt_make_highchg(data);
1353 if (error)
1354 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001355 }
1356
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001357 return count;
1358}
1359
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001360static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1361static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001362static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1363static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001364
Iiro Valkonen7686b102011-02-02 23:21:58 -08001365static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001366 &dev_attr_fw_version.attr,
1367 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001368 &dev_attr_object.attr,
1369 &dev_attr_update_fw.attr,
1370 NULL
1371};
1372
Iiro Valkonen7686b102011-02-02 23:21:58 -08001373static const struct attribute_group mxt_attr_group = {
1374 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001375};
1376
Iiro Valkonen7686b102011-02-02 23:21:58 -08001377static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001378{
1379 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001380 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001381 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001382}
1383
Iiro Valkonen7686b102011-02-02 23:21:58 -08001384static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001385{
1386 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001387 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001388 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389}
1390
Iiro Valkonen7686b102011-02-02 23:21:58 -08001391static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001393 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001394
Iiro Valkonen7686b102011-02-02 23:21:58 -08001395 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001396
1397 return 0;
1398}
1399
Iiro Valkonen7686b102011-02-02 23:21:58 -08001400static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001401{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001402 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001403
Iiro Valkonen7686b102011-02-02 23:21:58 -08001404 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001405}
1406
Bill Pemberton5298cc42012-11-23 21:38:25 -08001407static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001408 const struct i2c_device_id *id)
1409{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001410 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001411 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001412 struct input_dev *input_dev;
1413 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001414 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001415 unsigned int mt_flags = 0;
1416 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001417
Iiro Valkonen919ed892011-02-15 13:36:52 -08001418 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001419 return -EINVAL;
1420
Iiro Valkonen7686b102011-02-02 23:21:58 -08001421 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001422 input_dev = input_allocate_device();
1423 if (!data || !input_dev) {
1424 dev_err(&client->dev, "Failed to allocate memory\n");
1425 error = -ENOMEM;
1426 goto err_free_mem;
1427 }
1428
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001429 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001430 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1431 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001432
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001433 input_dev->phys = data->phys;
1434
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001435 input_dev->id.bustype = BUS_I2C;
1436 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001437 input_dev->open = mxt_input_open;
1438 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001439
Joonyoung Shim910d8052011-04-12 23:14:38 -07001440 data->client = client;
1441 data->input_dev = input_dev;
1442 data->pdata = pdata;
1443 data->irq = client->irq;
1444
Benson Leungd79e7e42014-05-18 23:02:52 -07001445 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001446 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07001447 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07001448
Daniel Kurtzcb159112012-06-28 21:08:22 +08001449 error = mxt_initialize(data);
1450 if (error)
1451 goto err_free_mem;
1452
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001453 __set_bit(EV_ABS, input_dev->evbit);
1454 __set_bit(EV_KEY, input_dev->evbit);
1455 __set_bit(BTN_TOUCH, input_dev->keybit);
1456
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001457 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001458 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1459
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001460 for (i = 0; i < pdata->t19_num_keys; i++)
1461 if (pdata->t19_keymap[i] != KEY_RESERVED)
1462 input_set_capability(input_dev, EV_KEY,
1463 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001464
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001465 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001466
1467 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1468 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1469 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1470 MXT_PIXELS_PER_MM);
1471 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1472 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001473
1474 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001475 }
1476
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001477 /* For single touch */
1478 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001479 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001480 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001481 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001482 input_set_abs_params(input_dev, ABS_PRESSURE,
1483 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001484
1485 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001486 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001487 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001488 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001489 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001490 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001491 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001492 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001493 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001494 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001495 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001496 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1497 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001498
1499 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001500 i2c_set_clientdata(client, data);
1501
Iiro Valkonen7686b102011-02-02 23:21:58 -08001502 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001503 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001504 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001505 if (error) {
1506 dev_err(&client->dev, "Failed to register interrupt\n");
1507 goto err_free_object;
1508 }
1509
Iiro Valkonen08960a02011-04-12 23:16:40 -07001510 error = mxt_make_highchg(data);
1511 if (error)
1512 goto err_free_irq;
1513
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001514 error = input_register_device(input_dev);
Nick Dyer7bed6802014-05-18 23:04:09 -07001515 if (error) {
1516 dev_err(&client->dev, "Error %d registering input device\n",
1517 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001518 goto err_free_irq;
Nick Dyer7bed6802014-05-18 23:04:09 -07001519 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001520
Iiro Valkonen7686b102011-02-02 23:21:58 -08001521 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07001522 if (error) {
1523 dev_err(&client->dev, "Failure %d creating sysfs group\n",
1524 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001525 goto err_unregister_device;
Nick Dyer7bed6802014-05-18 23:04:09 -07001526 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001527
1528 return 0;
1529
1530err_unregister_device:
1531 input_unregister_device(input_dev);
1532 input_dev = NULL;
1533err_free_irq:
1534 free_irq(client->irq, data);
1535err_free_object:
1536 kfree(data->object_table);
1537err_free_mem:
1538 input_free_device(input_dev);
1539 kfree(data);
1540 return error;
1541}
1542
Bill Pembertone2619cf2012-11-23 21:50:47 -08001543static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001544{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001545 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001546
Iiro Valkonen7686b102011-02-02 23:21:58 -08001547 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001548 free_irq(data->irq, data);
1549 input_unregister_device(data->input_dev);
1550 kfree(data->object_table);
1551 kfree(data);
1552
1553 return 0;
1554}
1555
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001556#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001557static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001558{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001559 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001560 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001561 struct input_dev *input_dev = data->input_dev;
1562
1563 mutex_lock(&input_dev->mutex);
1564
1565 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001566 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001567
1568 mutex_unlock(&input_dev->mutex);
1569
1570 return 0;
1571}
1572
Iiro Valkonen7686b102011-02-02 23:21:58 -08001573static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001574{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001575 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001576 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001577 struct input_dev *input_dev = data->input_dev;
1578
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001579 mxt_soft_reset(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001580
1581 mutex_lock(&input_dev->mutex);
1582
1583 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001584 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001585
1586 mutex_unlock(&input_dev->mutex);
1587
1588 return 0;
1589}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001590#endif
1591
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001592static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1593
Iiro Valkonen7686b102011-02-02 23:21:58 -08001594static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001595 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001596 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001597 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001598 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001599 { }
1600};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001601MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001602
Iiro Valkonen7686b102011-02-02 23:21:58 -08001603static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001604 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001605 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001606 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001607 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001608 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001609 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001610 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001611 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001612};
1613
Axel Lin1b92c1c2012-03-16 23:05:41 -07001614module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001615
1616/* Module information */
1617MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001618MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001619MODULE_LICENSE("GPL");