blob: 7951d9bcee4a929ccfc800b7a864ba430f852725 [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__);
437 return -EIO;
438 }
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
Iiro Valkonen7686b102011-02-02 23:21:58 -0800657static void mxt_input_touchevent(struct mxt_data *data,
658 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700659{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660 struct device *dev = &data->client->dev;
661 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800662 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700663 int x;
664 int y;
665 int area;
Nick Dyerfea9e462014-05-18 23:21:48 -0700666 int amplitude;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667
Joonyoung Shim910d8052011-04-12 23:14:38 -0700668 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
669 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
670 if (data->max_x < 1024)
671 x = x >> 2;
672 if (data->max_y < 1024)
673 y = y >> 2;
674
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700675 area = message->message[4];
Nick Dyerfea9e462014-05-18 23:21:48 -0700676 amplitude = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800678 dev_dbg(dev,
679 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
680 id,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700681 (status & MXT_T9_DETECT) ? 'D' : '.',
682 (status & MXT_T9_PRESS) ? 'P' : '.',
683 (status & MXT_T9_RELEASE) ? 'R' : '.',
684 (status & MXT_T9_MOVE) ? 'M' : '.',
685 (status & MXT_T9_VECTOR) ? 'V' : '.',
686 (status & MXT_T9_AMP) ? 'A' : '.',
687 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
688 (status & MXT_T9_UNGRIP) ? 'U' : '.',
Nick Dyerfea9e462014-05-18 23:21:48 -0700689 x, y, area, amplitude);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700690
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800691 input_mt_slot(input_dev, id);
692 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700693 status & MXT_T9_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700694
Nick Dyerf3889ed2014-05-18 23:22:04 -0700695 if (status & MXT_T9_DETECT) {
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800696 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
697 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
Nick Dyerfea9e462014-05-18 23:21:48 -0700698 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800699 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
700 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701}
702
Nick Dyerc3f78042014-05-18 23:04:46 -0700703static u16 mxt_extract_T6_csum(const u8 *csum)
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800704{
705 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
706}
707
Daniel Kurtz04a79182012-06-28 21:08:21 +0800708static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
709{
710 u8 id = msg->reportid;
711 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700712}
713
Benson Leungd79e7e42014-05-18 23:02:52 -0700714static irqreturn_t mxt_process_messages_until_invalid(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800716 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800717 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700718 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800720 bool update_input = false;
Nick Dyerc3f78042014-05-18 23:04:46 -0700721 u32 crc;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700722
723 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800724 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700725 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700726 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727 }
728
729 reportid = message.reportid;
730
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800731 if (reportid == data->T6_reportid) {
732 u8 status = payload[0];
Nick Dyerc3f78042014-05-18 23:04:46 -0700733
734 crc = mxt_extract_T6_csum(&payload[1]);
735 if (crc != data->config_crc) {
736 data->config_crc = crc;
737 complete(&data->crc_completion);
738 }
739
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800740 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
Nick Dyerc3f78042014-05-18 23:04:46 -0700741 status, data->config_crc);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700742
743 if (status & MXT_T6_STATUS_RESET)
744 complete(&data->reset_completion);
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800745 } else if (mxt_is_T9_message(data, &message)) {
746 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800747 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800748 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800749 } else if (message.reportid == data->T19_reportid) {
750 mxt_input_button(data, &message);
751 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800752 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800753 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800754 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700755 } while (reportid != 0xff);
756
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800757 if (update_input) {
758 input_mt_report_pointer_emulation(data->input_dev, false);
759 input_sync(data->input_dev);
760 }
761
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700762 return IRQ_HANDLED;
763}
764
Benson Leungd79e7e42014-05-18 23:02:52 -0700765static irqreturn_t mxt_interrupt(int irq, void *dev_id)
766{
767 struct mxt_data *data = dev_id;
768
769 if (data->in_bootloader) {
770 /* bootloader state transition completion */
771 complete(&data->bl_completion);
772 return IRQ_HANDLED;
773 }
774
775 return mxt_process_messages_until_invalid(data);
776}
777
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700778static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
779 u8 value, bool wait)
780{
781 u16 reg;
782 u8 command_register;
783 int timeout_counter = 0;
784 int ret;
785
786 reg = data->T6_address + cmd_offset;
787
788 ret = mxt_write_reg(data->client, reg, value);
789 if (ret)
790 return ret;
791
792 if (!wait)
793 return 0;
794
795 do {
796 msleep(20);
797 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
798 if (ret)
799 return ret;
800 } while (command_register != 0 && timeout_counter++ <= 100);
801
802 if (timeout_counter > 100) {
803 dev_err(&data->client->dev, "Command failed!\n");
804 return -EIO;
805 }
806
807 return 0;
808}
809
810static int mxt_soft_reset(struct mxt_data *data)
811{
812 struct device *dev = &data->client->dev;
813 int ret = 0;
814
815 dev_info(dev, "Resetting chip\n");
816
817 reinit_completion(&data->reset_completion);
818
819 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
820 if (ret)
821 return ret;
822
823 ret = mxt_wait_for_completion(data, &data->reset_completion,
824 MXT_RESET_TIMEOUT);
825 if (ret)
826 return ret;
827
828 return 0;
829}
830
Nick Dyerc3f78042014-05-18 23:04:46 -0700831static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
832{
833 /*
834 * On failure, CRC is set to 0 and config will always be
835 * downloaded.
836 */
837 data->config_crc = 0;
838 reinit_completion(&data->crc_completion);
839
840 mxt_t6_command(data, cmd, value, true);
841
842 /*
843 * Wait for crc message. On failure, CRC is set to 0 and config will
844 * always be downloaded.
845 */
846 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
847}
848
Iiro Valkonen7686b102011-02-02 23:21:58 -0800849static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700850{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800851 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800852 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700853 struct device *dev = &data->client->dev;
854 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800855 int i, size;
856 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700857
Iiro Valkonen71749f52011-02-15 13:36:52 -0800858 if (!pdata->config) {
859 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
860 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700861 }
862
Nick Dyerc3f78042014-05-18 23:04:46 -0700863 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
864
865 if (data->config_crc == pdata->config_crc) {
866 dev_info(dev, "Config CRC 0x%06X: OK\n", data->config_crc);
867 return 0;
868 }
869
870 dev_info(dev, "Config CRC 0x%06X: does not match 0x%06X\n",
871 data->config_crc, pdata->config_crc);
872
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700873 for (i = 0; i < data->info.object_num; i++) {
874 object = data->object_table + i;
875
Iiro Valkonen7686b102011-02-02 23:21:58 -0800876 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700877 continue;
878
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700879 size = mxt_obj_size(object) * mxt_obj_instances(object);
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800880 if (index + size > pdata->config_length) {
881 dev_err(dev, "Not enough config data!\n");
882 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800883 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800884
885 ret = __mxt_write_reg(data->client, object->start_address,
886 size, &pdata->config[index]);
887 if (ret)
888 return ret;
889 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700890 }
891
Nick Dyerc3f78042014-05-18 23:04:46 -0700892 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
893
894 ret = mxt_soft_reset(data);
895 if (ret)
896 return ret;
897
898 dev_info(dev, "Config successfully updated\n");
899
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700900 return 0;
901}
902
Iiro Valkonen7686b102011-02-02 23:21:58 -0800903static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700904{
905 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800906 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700907 int count = 10;
908 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909
910 /* Read dummy message to make high CHG pin */
911 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800912 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700913 if (error)
914 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800915 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700916
917 if (!count) {
918 dev_err(dev, "CHG pin isn't cleared\n");
919 return -EBUSY;
920 }
921
922 return 0;
923}
924
Iiro Valkonen7686b102011-02-02 23:21:58 -0800925static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700926{
927 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800928 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700929 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700930
Daniel Kurtz23003a842012-06-28 21:08:14 +0800931 /* Read 7-byte info block starting at address 0 */
932 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700933 if (error)
934 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700935
936 return 0;
937}
938
Iiro Valkonen7686b102011-02-02 23:21:58 -0800939static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700940{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800941 struct i2c_client *client = data->client;
942 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700943 int error;
944 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800945 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800947 table_size = data->info.object_num * sizeof(struct mxt_object);
948 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
949 data->object_table);
950 if (error)
951 return error;
952
953 /* Valid Report IDs start counting from 1 */
954 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800956 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800957 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700958
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800959 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700960
961 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800962 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700963 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700964 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800965 max_id = reportid - 1;
966 } else {
967 min_id = 0;
968 max_id = 0;
969 }
970
971 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -0700972 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700973 object->type, object->start_address,
974 mxt_obj_size(object), mxt_obj_instances(object),
975 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800976
977 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800978 case MXT_GEN_COMMAND_T6:
979 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700980 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800981 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800982 case MXT_TOUCH_MULTI_T9:
983 data->T9_reportid_min = min_id;
984 data->T9_reportid_max = max_id;
985 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800986 case MXT_SPT_GPIOPWM_T19:
987 data->T19_reportid = min_id;
988 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700989 }
990 }
991
992 return 0;
993}
994
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800995static void mxt_free_object_table(struct mxt_data *data)
996{
997 kfree(data->object_table);
998 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800999 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001000 data->T9_reportid_min = 0;
1001 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001002 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001003}
1004
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001005static int mxt_read_t9_resolution(struct mxt_data *data)
1006{
1007 struct i2c_client *client = data->client;
1008 int error;
1009 struct t9_range range;
1010 unsigned char orient;
1011 struct mxt_object *object;
1012
1013 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1014 if (!object)
1015 return -EINVAL;
1016
1017 error = __mxt_read_reg(client,
1018 object->start_address + MXT_T9_RANGE,
1019 sizeof(range), &range);
1020 if (error)
1021 return error;
1022
1023 le16_to_cpus(&range.x);
1024 le16_to_cpus(&range.y);
1025
1026 error = __mxt_read_reg(client,
1027 object->start_address + MXT_T9_ORIENT,
1028 1, &orient);
1029 if (error)
1030 return error;
1031
1032 /* Handle default values */
1033 if (range.x == 0)
1034 range.x = 1023;
1035
1036 if (range.y == 0)
1037 range.y = 1023;
1038
Nick Dyerf3889ed2014-05-18 23:22:04 -07001039 if (orient & MXT_T9_ORIENT_SWITCH) {
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001040 data->max_x = range.y;
1041 data->max_y = range.x;
1042 } else {
1043 data->max_x = range.x;
1044 data->max_y = range.y;
1045 }
1046
1047 dev_dbg(&client->dev,
1048 "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1049
1050 return 0;
1051}
1052
Iiro Valkonen7686b102011-02-02 23:21:58 -08001053static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054{
1055 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001056 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001057 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001058
Iiro Valkonen7686b102011-02-02 23:21:58 -08001059 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001060 if (error)
1061 return error;
1062
1063 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001064 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001065 GFP_KERNEL);
1066 if (!data->object_table) {
1067 dev_err(&client->dev, "Failed to allocate memory\n");
1068 return -ENOMEM;
1069 }
1070
1071 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001072 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001073 if (error) {
1074 dev_err(&client->dev, "Error %d reading object table\n", error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001075 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -07001076 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001077
1078 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001079 error = mxt_check_reg_init(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001080 if (error) {
1081 dev_err(&client->dev, "Error %d initializing configuration\n",
1082 error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001083 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -07001084 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001085
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001086 error = mxt_read_t9_resolution(data);
1087 if (error) {
1088 dev_err(&client->dev, "Failed to initialize T9 resolution\n");
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001089 goto err_free_object_table;
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001090 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001091
1092 dev_info(&client->dev,
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001093 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1094 info->family_id, info->variant_id, info->version >> 4,
1095 info->version & 0xf, info->build, info->object_num);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001096
1097 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001098
1099err_free_object_table:
1100 mxt_free_object_table(data);
1101 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001102}
1103
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001104/* Firmware Version is returned as Major.Minor.Build */
1105static ssize_t mxt_fw_version_show(struct device *dev,
1106 struct device_attribute *attr, char *buf)
1107{
1108 struct mxt_data *data = dev_get_drvdata(dev);
1109 struct mxt_info *info = &data->info;
1110 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
1111 info->version >> 4, info->version & 0xf, info->build);
1112}
1113
1114/* Hardware Version is returned as FamilyID.VariantID */
1115static ssize_t mxt_hw_version_show(struct device *dev,
1116 struct device_attribute *attr, char *buf)
1117{
1118 struct mxt_data *data = dev_get_drvdata(dev);
1119 struct mxt_info *info = &data->info;
1120 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
1121 info->family_id, info->variant_id);
1122}
1123
Daniel Kurtz794eb672012-06-28 21:08:10 +08001124static ssize_t mxt_show_instance(char *buf, int count,
1125 struct mxt_object *object, int instance,
1126 const u8 *val)
1127{
1128 int i;
1129
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001130 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001131 count += scnprintf(buf + count, PAGE_SIZE - count,
1132 "Instance %u\n", instance);
1133
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001134 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001135 count += scnprintf(buf + count, PAGE_SIZE - count,
1136 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
1137 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
1138
1139 return count;
1140}
1141
Iiro Valkonen7686b102011-02-02 23:21:58 -08001142static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143 struct device_attribute *attr, char *buf)
1144{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001145 struct mxt_data *data = dev_get_drvdata(dev);
1146 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001147 int count = 0;
1148 int i, j;
1149 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001150 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001151
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001152 /* Pre-allocate buffer large enough to hold max sized object. */
1153 obuf = kmalloc(256, GFP_KERNEL);
1154 if (!obuf)
1155 return -ENOMEM;
1156
1157 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001158 for (i = 0; i < data->info.object_num; i++) {
1159 object = data->object_table + i;
1160
Daniel Kurtz91630952012-06-28 21:08:09 +08001161 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001162 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001163
Daniel Kurtz91630952012-06-28 21:08:09 +08001164 count += scnprintf(buf + count, PAGE_SIZE - count,
1165 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001167 for (j = 0; j < mxt_obj_instances(object); j++) {
1168 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08001169 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001170
Daniel Kurtz794eb672012-06-28 21:08:10 +08001171 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001172 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001173 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001174
Daniel Kurtz794eb672012-06-28 21:08:10 +08001175 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001177 }
1178
Daniel Kurtz794eb672012-06-28 21:08:10 +08001179done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001180 kfree(obuf);
1181 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001182}
1183
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001184static int mxt_check_firmware_format(struct device *dev,
1185 const struct firmware *fw)
1186{
1187 unsigned int pos = 0;
1188 char c;
1189
1190 while (pos < fw->size) {
1191 c = *(fw->data + pos);
1192
1193 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
1194 return 0;
1195
1196 pos++;
1197 }
1198
1199 /*
1200 * To convert file try:
1201 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
1202 */
1203 dev_err(dev, "Aborting: firmware file must be in binary format\n");
1204
1205 return -EINVAL;
1206}
1207
Iiro Valkonen7686b102011-02-02 23:21:58 -08001208static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001209{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001210 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001211 const struct firmware *fw = NULL;
1212 unsigned int frame_size;
1213 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07001214 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07001215 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001216 int ret;
1217
1218 ret = request_firmware(&fw, fn, dev);
1219 if (ret) {
1220 dev_err(dev, "Unable to open firmware %s\n", fn);
1221 return ret;
1222 }
1223
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001224 /* Check for incorrect enc file */
1225 ret = mxt_check_firmware_format(dev, fw);
1226 if (ret)
1227 goto release_firmware;
1228
Nick Dyerf28a8422014-05-18 23:10:49 -07001229 ret = mxt_lookup_bootloader_address(data);
1230 if (ret)
1231 goto release_firmware;
1232
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001233 /* Change to the bootloader mode */
Benson Leungd79e7e42014-05-18 23:02:52 -07001234 data->in_bootloader = true;
1235
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001236 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_BOOT_VALUE, false);
1237 if (ret)
1238 goto release_firmware;
1239
Iiro Valkonen7686b102011-02-02 23:21:58 -08001240 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001241
Benson Leungd79e7e42014-05-18 23:02:52 -07001242 reinit_completion(&data->bl_completion);
1243
1244 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001245 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001246 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247
1248 /* Unlock bootloader */
Nick Dyerf28a8422014-05-18 23:10:49 -07001249 mxt_unlock_bootloader(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001250
1251 while (pos < fw->size) {
Benson Leungd79e7e42014-05-18 23:02:52 -07001252 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001253 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001254 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001255
1256 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1257
Nick Dyerf943c742014-05-18 23:14:20 -07001258 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001259 frame_size += 2;
1260
1261 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07001262 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
1263 if (ret)
1264 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265
Benson Leungd79e7e42014-05-18 23:02:52 -07001266 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
Nick Dyerf943c742014-05-18 23:14:20 -07001267 if (ret) {
1268 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269
Nick Dyerf943c742014-05-18 23:14:20 -07001270 /* Back off by 20ms per retry */
1271 msleep(retry * 20);
1272
1273 if (retry > 20) {
1274 dev_err(dev, "Retry count exceeded\n");
1275 goto disable_irq;
1276 }
1277 } else {
1278 retry = 0;
1279 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07001280 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07001281 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001282
Nick Dyerf477c752014-05-18 23:14:45 -07001283 if (frame % 50 == 0)
1284 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
1285 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001286 }
1287
Benson Leunga0434b72014-05-18 23:03:09 -07001288 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001289 ret = mxt_wait_for_completion(data, &data->bl_completion,
1290 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001291 if (ret)
1292 goto disable_irq;
1293
Nick Dyerf477c752014-05-18 23:14:45 -07001294 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
1295
Benson Leunga0434b72014-05-18 23:03:09 -07001296 /*
1297 * Wait for device to reset. Some bootloader versions do not assert
1298 * the CHG line after bootloading has finished, so ignore potential
1299 * errors.
1300 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001301 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001302
Benson Leungd79e7e42014-05-18 23:02:52 -07001303 data->in_bootloader = false;
1304
1305disable_irq:
1306 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001307release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001308 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001309 return ret;
1310}
1311
Iiro Valkonen7686b102011-02-02 23:21:58 -08001312static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001313 struct device_attribute *attr,
1314 const char *buf, size_t count)
1315{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001316 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001317 int error;
1318
Iiro Valkonen7686b102011-02-02 23:21:58 -08001319 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001320 if (error) {
1321 dev_err(dev, "The firmware update failed(%d)\n", error);
1322 count = error;
1323 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07001324 dev_info(dev, "The firmware update succeeded\n");
1325
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001326 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001327
Iiro Valkonen7686b102011-02-02 23:21:58 -08001328 mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07001329
1330 enable_irq(data->irq);
1331
1332 error = mxt_make_highchg(data);
1333 if (error)
1334 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001335 }
1336
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001337 return count;
1338}
1339
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001340static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1341static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001342static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1343static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001344
Iiro Valkonen7686b102011-02-02 23:21:58 -08001345static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001346 &dev_attr_fw_version.attr,
1347 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001348 &dev_attr_object.attr,
1349 &dev_attr_update_fw.attr,
1350 NULL
1351};
1352
Iiro Valkonen7686b102011-02-02 23:21:58 -08001353static const struct attribute_group mxt_attr_group = {
1354 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001355};
1356
Iiro Valkonen7686b102011-02-02 23:21:58 -08001357static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001358{
1359 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001360 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001361 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001362}
1363
Iiro Valkonen7686b102011-02-02 23:21:58 -08001364static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001365{
1366 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001367 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001368 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001369}
1370
Iiro Valkonen7686b102011-02-02 23:21:58 -08001371static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001372{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001373 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001374
Iiro Valkonen7686b102011-02-02 23:21:58 -08001375 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001376
1377 return 0;
1378}
1379
Iiro Valkonen7686b102011-02-02 23:21:58 -08001380static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001381{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001382 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001383
Iiro Valkonen7686b102011-02-02 23:21:58 -08001384 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001385}
1386
Bill Pemberton5298cc42012-11-23 21:38:25 -08001387static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001388 const struct i2c_device_id *id)
1389{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001390 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001391 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392 struct input_dev *input_dev;
1393 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001394 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001395 unsigned int mt_flags = 0;
1396 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001397
Iiro Valkonen919ed892011-02-15 13:36:52 -08001398 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001399 return -EINVAL;
1400
Iiro Valkonen7686b102011-02-02 23:21:58 -08001401 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001402 input_dev = input_allocate_device();
1403 if (!data || !input_dev) {
1404 dev_err(&client->dev, "Failed to allocate memory\n");
1405 error = -ENOMEM;
1406 goto err_free_mem;
1407 }
1408
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001409 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001410 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1411 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001412
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001413 input_dev->phys = data->phys;
1414
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001415 input_dev->id.bustype = BUS_I2C;
1416 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001417 input_dev->open = mxt_input_open;
1418 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001419
Joonyoung Shim910d8052011-04-12 23:14:38 -07001420 data->client = client;
1421 data->input_dev = input_dev;
1422 data->pdata = pdata;
1423 data->irq = client->irq;
1424
Benson Leungd79e7e42014-05-18 23:02:52 -07001425 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001426 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07001427 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07001428
Daniel Kurtzcb159112012-06-28 21:08:22 +08001429 error = mxt_initialize(data);
1430 if (error)
1431 goto err_free_mem;
1432
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001433 __set_bit(EV_ABS, input_dev->evbit);
1434 __set_bit(EV_KEY, input_dev->evbit);
1435 __set_bit(BTN_TOUCH, input_dev->keybit);
1436
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001437 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001438 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1439
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001440 for (i = 0; i < pdata->t19_num_keys; i++)
1441 if (pdata->t19_keymap[i] != KEY_RESERVED)
1442 input_set_capability(input_dev, EV_KEY,
1443 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001444
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001445 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001446
1447 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1448 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1449 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1450 MXT_PIXELS_PER_MM);
1451 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1452 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001453
1454 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001455 }
1456
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001457 /* For single touch */
1458 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001459 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001460 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001461 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001462 input_set_abs_params(input_dev, ABS_PRESSURE,
1463 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001464
1465 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001466 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001467 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001468 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001469 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001471 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001472 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001473 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001474 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001475 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001476 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1477 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001478
1479 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001480 i2c_set_clientdata(client, data);
1481
Iiro Valkonen7686b102011-02-02 23:21:58 -08001482 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001483 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001484 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001485 if (error) {
1486 dev_err(&client->dev, "Failed to register interrupt\n");
1487 goto err_free_object;
1488 }
1489
Iiro Valkonen08960a02011-04-12 23:16:40 -07001490 error = mxt_make_highchg(data);
1491 if (error)
1492 goto err_free_irq;
1493
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001494 error = input_register_device(input_dev);
Nick Dyer7bed6802014-05-18 23:04:09 -07001495 if (error) {
1496 dev_err(&client->dev, "Error %d registering input device\n",
1497 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001498 goto err_free_irq;
Nick Dyer7bed6802014-05-18 23:04:09 -07001499 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001500
Iiro Valkonen7686b102011-02-02 23:21:58 -08001501 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07001502 if (error) {
1503 dev_err(&client->dev, "Failure %d creating sysfs group\n",
1504 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001505 goto err_unregister_device;
Nick Dyer7bed6802014-05-18 23:04:09 -07001506 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001507
1508 return 0;
1509
1510err_unregister_device:
1511 input_unregister_device(input_dev);
1512 input_dev = NULL;
1513err_free_irq:
1514 free_irq(client->irq, data);
1515err_free_object:
1516 kfree(data->object_table);
1517err_free_mem:
1518 input_free_device(input_dev);
1519 kfree(data);
1520 return error;
1521}
1522
Bill Pembertone2619cf2012-11-23 21:50:47 -08001523static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001524{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001525 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001526
Iiro Valkonen7686b102011-02-02 23:21:58 -08001527 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001528 free_irq(data->irq, data);
1529 input_unregister_device(data->input_dev);
1530 kfree(data->object_table);
1531 kfree(data);
1532
1533 return 0;
1534}
1535
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001536#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001537static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001538{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001539 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001540 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001541 struct input_dev *input_dev = data->input_dev;
1542
1543 mutex_lock(&input_dev->mutex);
1544
1545 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001546 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001547
1548 mutex_unlock(&input_dev->mutex);
1549
1550 return 0;
1551}
1552
Iiro Valkonen7686b102011-02-02 23:21:58 -08001553static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001554{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001555 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001556 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001557 struct input_dev *input_dev = data->input_dev;
1558
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001559 mxt_soft_reset(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001560
1561 mutex_lock(&input_dev->mutex);
1562
1563 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001564 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001565
1566 mutex_unlock(&input_dev->mutex);
1567
1568 return 0;
1569}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001570#endif
1571
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001572static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1573
Iiro Valkonen7686b102011-02-02 23:21:58 -08001574static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001575 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001576 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001577 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001578 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001579 { }
1580};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001581MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001582
Iiro Valkonen7686b102011-02-02 23:21:58 -08001583static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001584 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001585 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001586 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001587 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001588 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001589 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001590 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001591 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001592};
1593
Axel Lin1b92c1c2012-03-16 23:05:41 -07001594module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001595
1596/* Module information */
1597MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001598MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001599MODULE_LICENSE("GPL");