blob: 2875ddf37289521c2059ad64d1301e6bb40b3ba4 [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
Nick Dyer50a77c62014-07-23 12:38:48 -07005 * Copyright (C) 2011-2014 Atmel Corporation
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07006 * Copyright (C) 2012 Google, Inc.
7 *
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07008 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/module.h>
Benson Leungd79e7e42014-05-18 23:02:52 -070018#include <linux/init.h>
19#include <linux/completion.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070020#include <linux/delay.h>
21#include <linux/firmware.h>
22#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080023#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070024#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070025#include <linux/interrupt.h>
Stephen Warren78188be2014-07-23 12:23:23 -070026#include <linux/of.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070027#include <linux/slab.h>
Nick Dyerb23157d2015-04-06 11:25:13 -070028#include <asm/unaligned.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070029
30/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080031#define MXT_VER_20 20
32#define MXT_VER_21 21
33#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070034
Nick Dyer50a77c62014-07-23 12:38:48 -070035/* Firmware files */
Iiro Valkonen7686b102011-02-02 23:21:58 -080036#define MXT_FW_NAME "maxtouch.fw"
Nick Dyer50a77c62014-07-23 12:38:48 -070037#define MXT_CFG_NAME "maxtouch.cfg"
38#define MXT_CFG_MAGIC "OBP_RAW V1"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070039
40/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080041#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080042#define MXT_FAMILY_ID 0x00
43#define MXT_VARIANT_ID 0x01
44#define MXT_VERSION 0x02
45#define MXT_BUILD 0x03
46#define MXT_MATRIX_X_SIZE 0x04
47#define MXT_MATRIX_Y_SIZE 0x05
48#define MXT_OBJECT_NUM 0x06
49#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070050
Iiro Valkonen7686b102011-02-02 23:21:58 -080051#define MXT_OBJECT_SIZE 6
Nick Dyer4ce6fa02014-07-23 12:40:09 -070052#define MXT_INFO_CHECKSUM_SIZE 3
53#define MXT_MAX_BLOCK_WRITE 256
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070054
55/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070056#define MXT_DEBUG_DIAGNOSTIC_T37 37
57#define MXT_GEN_MESSAGE_T5 5
58#define MXT_GEN_COMMAND_T6 6
59#define MXT_GEN_POWER_T7 7
60#define MXT_GEN_ACQUIRE_T8 8
61#define MXT_GEN_DATASOURCE_T53 53
62#define MXT_TOUCH_MULTI_T9 9
63#define MXT_TOUCH_KEYARRAY_T15 15
64#define MXT_TOUCH_PROXIMITY_T23 23
65#define MXT_TOUCH_PROXKEY_T52 52
66#define MXT_PROCI_GRIPFACE_T20 20
67#define MXT_PROCG_NOISE_T22 22
68#define MXT_PROCI_ONETOUCH_T24 24
69#define MXT_PROCI_TWOTOUCH_T27 27
70#define MXT_PROCI_GRIP_T40 40
71#define MXT_PROCI_PALM_T41 41
72#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
73#define MXT_PROCI_STYLUS_T47 47
74#define MXT_PROCG_NOISESUPPRESSION_T48 48
75#define MXT_SPT_COMMSCONFIG_T18 18
76#define MXT_SPT_GPIOPWM_T19 19
77#define MXT_SPT_SELFTEST_T25 25
78#define MXT_SPT_CTECONFIG_T28 28
79#define MXT_SPT_USERDATA_T38 38
80#define MXT_SPT_DIGITIZER_T43 43
81#define MXT_SPT_MESSAGECOUNT_T44 44
82#define MXT_SPT_CTECONFIG_T46 46
Nick Dyerb23157d2015-04-06 11:25:13 -070083#define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070084
Nick Dyer5f3f9bc2014-07-23 12:46:55 -070085/* MXT_GEN_MESSAGE_T5 object */
86#define MXT_RPTID_NOMSG 0xff
87
Iiro Valkonen81c88a72011-07-04 03:08:25 -070088/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080089#define MXT_COMMAND_RESET 0
90#define MXT_COMMAND_BACKUPNV 1
91#define MXT_COMMAND_CALIBRATE 2
92#define MXT_COMMAND_REPORTALL 3
93#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070094
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070095/* Define for T6 status byte */
96#define MXT_T6_STATUS_RESET (1 << 7)
Nick Dyer497767d2014-07-23 12:47:50 -070097#define MXT_T6_STATUS_OFL (1 << 6)
98#define MXT_T6_STATUS_SIGERR (1 << 5)
99#define MXT_T6_STATUS_CAL (1 << 4)
100#define MXT_T6_STATUS_CFGERR (1 << 3)
101#define MXT_T6_STATUS_COMSERR (1 << 2)
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700102
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700103/* MXT_GEN_POWER_T7 field */
Linus Torvalds7f405482014-12-31 12:59:34 -0800104#define MXT_POWER_IDLEACQINT 0
105#define MXT_POWER_ACTVACQINT 1
106#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700107
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700108/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800109#define MXT_ACQUIRE_CHRGTIME 0
110#define MXT_ACQUIRE_TCHDRIFT 2
111#define MXT_ACQUIRE_DRIFTST 3
112#define MXT_ACQUIRE_TCHAUTOCAL 4
113#define MXT_ACQUIRE_SYNC 5
114#define MXT_ACQUIRE_ATCHCALST 6
115#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700116
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700117/* MXT_TOUCH_MULTI_T9 field */
Linus Torvalds7f405482014-12-31 12:59:34 -0800118#define MXT_TOUCH_CTRL 0
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700119#define MXT_T9_ORIENT 9
120#define MXT_T9_RANGE 18
121
Nick Dyerf3889ed2014-05-18 23:22:04 -0700122/* MXT_TOUCH_MULTI_T9 status */
123#define MXT_T9_UNGRIP (1 << 0)
124#define MXT_T9_SUPPRESS (1 << 1)
125#define MXT_T9_AMP (1 << 2)
126#define MXT_T9_VECTOR (1 << 3)
127#define MXT_T9_MOVE (1 << 4)
128#define MXT_T9_RELEASE (1 << 5)
129#define MXT_T9_PRESS (1 << 6)
130#define MXT_T9_DETECT (1 << 7)
131
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700132struct t9_range {
133 u16 x;
134 u16 y;
135} __packed;
136
Nick Dyerf3889ed2014-05-18 23:22:04 -0700137/* MXT_TOUCH_MULTI_T9 orient */
138#define MXT_T9_ORIENT_SWITCH (1 << 0)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700139
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700140/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800141#define MXT_GRIPFACE_CTRL 0
142#define MXT_GRIPFACE_XLOGRIP 1
143#define MXT_GRIPFACE_XHIGRIP 2
144#define MXT_GRIPFACE_YLOGRIP 3
145#define MXT_GRIPFACE_YHIGRIP 4
146#define MXT_GRIPFACE_MAXTCHS 5
147#define MXT_GRIPFACE_SZTHR1 7
148#define MXT_GRIPFACE_SZTHR2 8
149#define MXT_GRIPFACE_SHPTHR1 9
150#define MXT_GRIPFACE_SHPTHR2 10
151#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700152
Iiro Valkonen7686b102011-02-02 23:21:58 -0800153/* MXT_PROCI_NOISE field */
154#define MXT_NOISE_CTRL 0
155#define MXT_NOISE_OUTFLEN 1
156#define MXT_NOISE_GCAFUL_LSB 3
157#define MXT_NOISE_GCAFUL_MSB 4
158#define MXT_NOISE_GCAFLL_LSB 5
159#define MXT_NOISE_GCAFLL_MSB 6
160#define MXT_NOISE_ACTVGCAFVALID 7
161#define MXT_NOISE_NOISETHR 8
162#define MXT_NOISE_FREQHOPSCALE 10
163#define MXT_NOISE_FREQ0 11
164#define MXT_NOISE_FREQ1 12
165#define MXT_NOISE_FREQ2 13
166#define MXT_NOISE_FREQ3 14
167#define MXT_NOISE_FREQ4 15
168#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700169
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700170/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800171#define MXT_COMMS_CTRL 0
172#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700173
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700174/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800175#define MXT_CTE_CTRL 0
176#define MXT_CTE_CMD 1
177#define MXT_CTE_MODE 2
178#define MXT_CTE_IDLEGCAFDEPTH 3
179#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700180#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700181
Iiro Valkonen7686b102011-02-02 23:21:58 -0800182#define MXT_VOLTAGE_DEFAULT 2700000
183#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700184
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700185/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800186#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700187#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800188#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700189
Nick Dyerb23157d2015-04-06 11:25:13 -0700190/* T100 Multiple Touch Touchscreen */
191#define MXT_T100_CTRL 0
192#define MXT_T100_CFG1 1
193#define MXT_T100_TCHAUX 3
194#define MXT_T100_XRANGE 13
195#define MXT_T100_YRANGE 24
196
197#define MXT_T100_CFG_SWITCHXY BIT(5)
198
199#define MXT_T100_TCHAUX_VECT BIT(0)
200#define MXT_T100_TCHAUX_AMPL BIT(1)
201#define MXT_T100_TCHAUX_AREA BIT(2)
202
203#define MXT_T100_DETECT BIT(7)
204#define MXT_T100_TYPE_MASK 0x70
205
206enum t100_type {
207 MXT_T100_TYPE_FINGER = 1,
208 MXT_T100_TYPE_PASSIVE_STYLUS = 2,
209 MXT_T100_TYPE_HOVERING_FINGER = 4,
210 MXT_T100_TYPE_GLOVE = 5,
211 MXT_T100_TYPE_LARGE_TOUCH = 6,
212};
213
214#define MXT_DISTANCE_ACTIVE_TOUCH 0
215#define MXT_DISTANCE_HOVERING 1
216
217#define MXT_TOUCH_MAJOR_DEFAULT 1
218#define MXT_PRESSURE_DEFAULT 1
219
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700220/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800221#define MXT_BACKUP_TIME 50 /* msec */
222#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700223#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700224#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700225#define MXT_FW_RESET_TIME 3000 /* msec */
226#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700227
228/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800229#define MXT_UNLOCK_CMD_MSB 0xaa
230#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231
232/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800233#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
234#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
235#define MXT_FRAME_CRC_CHECK 0x02
236#define MXT_FRAME_CRC_FAIL 0x03
237#define MXT_FRAME_CRC_PASS 0x04
238#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
239#define MXT_BOOT_STATUS_MASK 0x3f
Nick Dyere57a66a2014-05-18 23:13:40 -0700240#define MXT_BOOT_EXTENDED_ID (1 << 5)
241#define MXT_BOOT_ID_MASK 0x1f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700242
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700243/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800244#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700245
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800246#define MXT_PIXELS_PER_MM 20
247
Iiro Valkonen7686b102011-02-02 23:21:58 -0800248struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700249 u8 family_id;
250 u8 variant_id;
251 u8 version;
252 u8 build;
253 u8 matrix_xsize;
254 u8 matrix_ysize;
255 u8 object_num;
256};
257
Iiro Valkonen7686b102011-02-02 23:21:58 -0800258struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700259 u8 type;
260 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700261 u8 size_minus_one;
262 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700263 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800264} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700265
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700266/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800267struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700268 struct i2c_client *client;
269 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800270 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800271 const struct mxt_platform_data *pdata;
272 struct mxt_object *object_table;
273 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700274 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700275 unsigned int max_x;
276 unsigned int max_y;
Benson Leungd79e7e42014-05-18 23:02:52 -0700277 bool in_bootloader;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700278 u16 mem_size;
Nick Dyerb23157d2015-04-06 11:25:13 -0700279 u8 t100_aux_ampl;
280 u8 t100_aux_area;
281 u8 t100_aux_vect;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700282 u8 max_reportid;
Nick Dyerc3f78042014-05-18 23:04:46 -0700283 u32 config_crc;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700284 u32 info_crc;
Nick Dyerf28a8422014-05-18 23:10:49 -0700285 u8 bootloader_addr;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700286 u8 *msg_buf;
Nick Dyer497767d2014-07-23 12:47:50 -0700287 u8 t6_status;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700288 bool update_input;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700289 u8 last_message_count;
290 u8 num_touchids;
Nick Dyerb23157d2015-04-06 11:25:13 -0700291 u8 multitouch;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800292
293 /* Cached parameters from object table */
Nick Dyerb9b05a82014-07-23 12:48:13 -0700294 u16 T5_address;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700295 u8 T5_msg_size;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800296 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700297 u16 T6_address;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700298 u16 T7_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800299 u8 T9_reportid_min;
300 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800301 u8 T19_reportid;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700302 u16 T44_address;
Nick Dyerb23157d2015-04-06 11:25:13 -0700303 u8 T100_reportid_min;
304 u8 T100_reportid_max;
Benson Leungd79e7e42014-05-18 23:02:52 -0700305
306 /* for fw update in bootloader */
307 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700308
309 /* for reset handling */
310 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700311
312 /* for config update handling */
313 struct completion crc_completion;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700314};
315
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700316static size_t mxt_obj_size(const struct mxt_object *obj)
317{
318 return obj->size_minus_one + 1;
319}
320
321static size_t mxt_obj_instances(const struct mxt_object *obj)
322{
323 return obj->instances_minus_one + 1;
324}
325
Iiro Valkonen7686b102011-02-02 23:21:58 -0800326static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700327{
328 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700329 case MXT_GEN_COMMAND_T6:
330 case MXT_GEN_POWER_T7:
331 case MXT_GEN_ACQUIRE_T8:
332 case MXT_GEN_DATASOURCE_T53:
333 case MXT_TOUCH_MULTI_T9:
334 case MXT_TOUCH_KEYARRAY_T15:
335 case MXT_TOUCH_PROXIMITY_T23:
336 case MXT_TOUCH_PROXKEY_T52:
337 case MXT_PROCI_GRIPFACE_T20:
338 case MXT_PROCG_NOISE_T22:
339 case MXT_PROCI_ONETOUCH_T24:
340 case MXT_PROCI_TWOTOUCH_T27:
341 case MXT_PROCI_GRIP_T40:
342 case MXT_PROCI_PALM_T41:
343 case MXT_PROCI_TOUCHSUPPRESSION_T42:
344 case MXT_PROCI_STYLUS_T47:
345 case MXT_PROCG_NOISESUPPRESSION_T48:
346 case MXT_SPT_COMMSCONFIG_T18:
347 case MXT_SPT_GPIOPWM_T19:
348 case MXT_SPT_SELFTEST_T25:
349 case MXT_SPT_CTECONFIG_T28:
350 case MXT_SPT_USERDATA_T38:
351 case MXT_SPT_DIGITIZER_T43:
352 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700353 return true;
354 default:
355 return false;
356 }
357}
358
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700359static void mxt_dump_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700360{
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700361 dev_dbg(&data->client->dev, "message: %*ph\n",
362 data->T5_msg_size, message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700363}
364
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700365static int mxt_wait_for_completion(struct mxt_data *data,
366 struct completion *comp,
367 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700368{
Benson Leungd79e7e42014-05-18 23:02:52 -0700369 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700370 unsigned long timeout = msecs_to_jiffies(timeout_ms);
371 long ret;
372
373 ret = wait_for_completion_interruptible_timeout(comp, timeout);
374 if (ret < 0) {
375 return ret;
376 } else if (ret == 0) {
377 dev_err(dev, "Wait for completion timed out.\n");
378 return -ETIMEDOUT;
379 }
380 return 0;
381}
382
Nick Dyerf28a8422014-05-18 23:10:49 -0700383static int mxt_bootloader_read(struct mxt_data *data,
384 u8 *val, unsigned int count)
385{
386 int ret;
387 struct i2c_msg msg;
388
389 msg.addr = data->bootloader_addr;
390 msg.flags = data->client->flags & I2C_M_TEN;
391 msg.flags |= I2C_M_RD;
392 msg.len = count;
393 msg.buf = val;
394
395 ret = i2c_transfer(data->client->adapter, &msg, 1);
Nick Dyerf28a8422014-05-18 23:10:49 -0700396 if (ret == 1) {
397 ret = 0;
398 } else {
399 ret = ret < 0 ? ret : -EIO;
400 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
401 __func__, ret);
402 }
403
404 return ret;
405}
406
407static int mxt_bootloader_write(struct mxt_data *data,
408 const u8 * const val, unsigned int count)
409{
410 int ret;
411 struct i2c_msg msg;
412
413 msg.addr = data->bootloader_addr;
414 msg.flags = data->client->flags & I2C_M_TEN;
415 msg.len = count;
416 msg.buf = (u8 *)val;
417
418 ret = i2c_transfer(data->client->adapter, &msg, 1);
419 if (ret == 1) {
420 ret = 0;
421 } else {
422 ret = ret < 0 ? ret : -EIO;
423 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
424 __func__, ret);
425 }
426
427 return ret;
428}
429
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700430static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
Nick Dyerf28a8422014-05-18 23:10:49 -0700431{
432 u8 appmode = data->client->addr;
433 u8 bootloader;
434
435 switch (appmode) {
436 case 0x4a:
437 case 0x4b:
Nick Dyer44a0bab2014-07-23 12:45:26 -0700438 /* Chips after 1664S use different scheme */
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700439 if (retry || data->info.family_id >= 0xa2) {
Nick Dyer44a0bab2014-07-23 12:45:26 -0700440 bootloader = appmode - 0x24;
441 break;
442 }
443 /* Fall through for normal case */
Nick Dyerf28a8422014-05-18 23:10:49 -0700444 case 0x4c:
445 case 0x4d:
446 case 0x5a:
447 case 0x5b:
448 bootloader = appmode - 0x26;
449 break;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700450
Nick Dyerf28a8422014-05-18 23:10:49 -0700451 default:
452 dev_err(&data->client->dev,
453 "Appmode i2c address 0x%02x not found\n",
454 appmode);
455 return -EINVAL;
456 }
457
458 data->bootloader_addr = bootloader;
459 return 0;
460}
461
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700462static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700463{
464 struct device *dev = &data->client->dev;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700465 int error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700466 u8 val;
467 bool crc_failure;
468
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700469 error = mxt_lookup_bootloader_address(data, alt_address);
470 if (error)
471 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700472
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700473 error = mxt_bootloader_read(data, &val, 1);
474 if (error)
475 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700476
477 /* Check app crc fail mode */
478 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
479
480 dev_err(dev, "Detected bootloader, status:%02X%s\n",
481 val, crc_failure ? ", APP_CRC_FAIL" : "");
482
483 return 0;
484}
485
Nick Dyere57a66a2014-05-18 23:13:40 -0700486static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
487{
488 struct device *dev = &data->client->dev;
489 u8 buf[3];
490
491 if (val & MXT_BOOT_EXTENDED_ID) {
492 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
493 dev_err(dev, "%s: i2c failure\n", __func__);
Nick Dyer68807a02014-06-07 23:17:26 -0700494 return val;
Nick Dyere57a66a2014-05-18 23:13:40 -0700495 }
496
497 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
498
499 return buf[0];
500 } else {
501 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
502
503 return val;
504 }
505}
506
Nick Dyer385deb92014-07-23 12:42:40 -0700507static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
508 bool wait)
Benson Leungd79e7e42014-05-18 23:02:52 -0700509{
Nick Dyerf28a8422014-05-18 23:10:49 -0700510 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700512 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700513
514recheck:
Nick Dyer385deb92014-07-23 12:42:40 -0700515 if (wait) {
Benson Leungd79e7e42014-05-18 23:02:52 -0700516 /*
517 * In application update mode, the interrupt
518 * line signals state transitions. We must wait for the
519 * CHG assertion before reading the status byte.
520 * Once the status byte has been read, the line is deasserted.
521 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700522 ret = mxt_wait_for_completion(data, &data->bl_completion,
523 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700524 if (ret) {
525 /*
526 * TODO: handle -ERESTARTSYS better by terminating
527 * fw update process before returning to userspace
528 * by writing length 0x000 to device (iff we are in
529 * WAITING_FRAME_DATA state).
530 */
Nick Dyerf28a8422014-05-18 23:10:49 -0700531 dev_err(dev, "Update wait error %d\n", ret);
Benson Leungd79e7e42014-05-18 23:02:52 -0700532 return ret;
533 }
534 }
535
Nick Dyerf28a8422014-05-18 23:10:49 -0700536 ret = mxt_bootloader_read(data, &val, 1);
537 if (ret)
538 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700539
Nick Dyere57a66a2014-05-18 23:13:40 -0700540 if (state == MXT_WAITING_BOOTLOAD_CMD)
541 val = mxt_get_bootloader_version(data, val);
542
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700543 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800544 case MXT_WAITING_BOOTLOAD_CMD:
545 case MXT_WAITING_FRAME_DATA:
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700546 case MXT_APP_CRC_FAIL:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800547 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700548 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800549 case MXT_FRAME_CRC_PASS:
Nick Dyerf943c742014-05-18 23:14:20 -0700550 if (val == MXT_FRAME_CRC_CHECK) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700551 goto recheck;
Nick Dyerf943c742014-05-18 23:14:20 -0700552 } else if (val == MXT_FRAME_CRC_FAIL) {
553 dev_err(dev, "Bootloader CRC fail\n");
554 return -EINVAL;
555 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700556 break;
557 default:
558 return -EINVAL;
559 }
560
561 if (val != state) {
Nick Dyerf28a8422014-05-18 23:10:49 -0700562 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
Nick Dyer7bed6802014-05-18 23:04:09 -0700563 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564 return -EINVAL;
565 }
566
567 return 0;
568}
569
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700570static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700571{
Nick Dyerf28a8422014-05-18 23:10:49 -0700572 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700573 u8 buf[2];
574
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700575 if (unlock) {
576 buf[0] = MXT_UNLOCK_CMD_LSB;
577 buf[1] = MXT_UNLOCK_CMD_MSB;
578 } else {
579 buf[0] = 0x01;
580 buf[1] = 0x01;
581 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582
Nick Dyerf28a8422014-05-18 23:10:49 -0700583 ret = mxt_bootloader_write(data, buf, 2);
584 if (ret)
585 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586
587 return 0;
588}
589
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591 u16 reg, u16 len, void *val)
592{
593 struct i2c_msg xfer[2];
594 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800595 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700596
597 buf[0] = reg & 0xff;
598 buf[1] = (reg >> 8) & 0xff;
599
600 /* Write register */
601 xfer[0].addr = client->addr;
602 xfer[0].flags = 0;
603 xfer[0].len = 2;
604 xfer[0].buf = buf;
605
606 /* Read data */
607 xfer[1].addr = client->addr;
608 xfer[1].flags = I2C_M_RD;
609 xfer[1].len = len;
610 xfer[1].buf = val;
611
Daniel Kurtz771733e2012-06-28 21:08:11 +0800612 ret = i2c_transfer(client->adapter, xfer, 2);
613 if (ret == 2) {
614 ret = 0;
615 } else {
616 if (ret >= 0)
617 ret = -EIO;
618 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
619 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700620 }
621
Daniel Kurtz771733e2012-06-28 21:08:11 +0800622 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700623}
624
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800625static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
626 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700627{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800628 u8 *buf;
629 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800630 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800632 count = len + 2;
633 buf = kmalloc(count, GFP_KERNEL);
634 if (!buf)
635 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636
637 buf[0] = reg & 0xff;
638 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800639 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700640
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800641 ret = i2c_master_send(client, buf, count);
642 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800643 ret = 0;
644 } else {
645 if (ret >= 0)
646 ret = -EIO;
647 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
648 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700649 }
650
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800651 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800652 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653}
654
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800655static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800657 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700658}
659
Iiro Valkonen7686b102011-02-02 23:21:58 -0800660static struct mxt_object *
661mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800663 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664 int i;
665
666 for (i = 0; i < data->info.object_num; i++) {
667 object = data->object_table + i;
668 if (object->type == type)
669 return object;
670 }
671
Nick Dyer50a77c62014-07-23 12:38:48 -0700672 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700673 return NULL;
674}
675
Nick Dyer497767d2014-07-23 12:47:50 -0700676static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
677{
678 struct device *dev = &data->client->dev;
679 u8 status = msg[1];
680 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
681
682 complete(&data->crc_completion);
683
684 if (crc != data->config_crc) {
685 data->config_crc = crc;
686 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
687 }
688
689 /* Detect reset */
690 if (status & MXT_T6_STATUS_RESET)
691 complete(&data->reset_completion);
692
693 /* Output debug if status has changed */
694 if (status != data->t6_status)
695 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
696 status,
697 status == 0 ? " OK" : "",
698 status & MXT_T6_STATUS_RESET ? " RESET" : "",
699 status & MXT_T6_STATUS_OFL ? " OFL" : "",
700 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
701 status & MXT_T6_STATUS_CAL ? " CAL" : "",
702 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
703 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
704
705 /* Save current status */
706 data->t6_status = status;
707}
708
Linus Torvalds7f405482014-12-31 12:59:34 -0800709static int mxt_write_object(struct mxt_data *data,
710 u8 type, u8 offset, u8 val)
711{
712 struct mxt_object *object;
713 u16 reg;
714
715 object = mxt_get_object(data, type);
716 if (!object || offset >= mxt_obj_size(object))
717 return -EINVAL;
718
719 reg = object->start_address;
720 return mxt_write_reg(data->client, reg + offset, val);
721}
722
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700723static void mxt_input_button(struct mxt_data *data, u8 *message)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800724{
725 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700726 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800727 bool button;
728 int i;
729
730 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700731 for (i = 0; i < pdata->t19_num_keys; i++) {
732 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800733 continue;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700734 button = !(message[1] & (1 << i));
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700735 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800736 }
737}
738
Benson Leungb735fbe2014-07-23 12:22:27 -0700739static void mxt_input_sync(struct mxt_data *data)
Nick Dyereef820d2014-05-18 23:22:22 -0700740{
Benson Leungb735fbe2014-07-23 12:22:27 -0700741 input_mt_report_pointer_emulation(data->input_dev,
742 data->pdata->t19_num_keys);
743 input_sync(data->input_dev);
Nick Dyereef820d2014-05-18 23:22:22 -0700744}
745
Nick Dyerb9b05a82014-07-23 12:48:13 -0700746static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700747{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700748 struct device *dev = &data->client->dev;
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800749 struct input_dev *input_dev = data->input_dev;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700750 int id;
751 u8 status;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752 int x;
753 int y;
754 int area;
Nick Dyerfea9e462014-05-18 23:21:48 -0700755 int amplitude;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700757 id = message[0] - data->T9_reportid_min;
758 status = message[1];
759 x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
760 y = (message[3] << 4) | ((message[4] & 0xf));
Nick Dyereef820d2014-05-18 23:22:22 -0700761
762 /* Handle 10/12 bit switching */
Joonyoung Shim910d8052011-04-12 23:14:38 -0700763 if (data->max_x < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700764 x >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700765 if (data->max_y < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700766 y >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700767
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700768 area = message[5];
769 amplitude = message[6];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700770
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800771 dev_dbg(dev,
772 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
773 id,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700774 (status & MXT_T9_DETECT) ? 'D' : '.',
775 (status & MXT_T9_PRESS) ? 'P' : '.',
776 (status & MXT_T9_RELEASE) ? 'R' : '.',
777 (status & MXT_T9_MOVE) ? 'M' : '.',
778 (status & MXT_T9_VECTOR) ? 'V' : '.',
779 (status & MXT_T9_AMP) ? 'A' : '.',
780 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
781 (status & MXT_T9_UNGRIP) ? 'U' : '.',
Nick Dyerfea9e462014-05-18 23:21:48 -0700782 x, y, area, amplitude);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800784 input_mt_slot(input_dev, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700785
Nick Dyerf3889ed2014-05-18 23:22:04 -0700786 if (status & MXT_T9_DETECT) {
Nick Dyereef820d2014-05-18 23:22:22 -0700787 /*
788 * Multiple bits may be set if the host is slow to read
789 * the status messages, indicating all the events that
790 * have happened.
791 */
792 if (status & MXT_T9_RELEASE) {
793 input_mt_report_slot_state(input_dev,
794 MT_TOOL_FINGER, 0);
Benson Leungb735fbe2014-07-23 12:22:27 -0700795 mxt_input_sync(data);
Nick Dyereef820d2014-05-18 23:22:22 -0700796 }
797
798 /* Touch active */
799 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800800 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
801 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
Nick Dyerfea9e462014-05-18 23:21:48 -0700802 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800803 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
Nick Dyereef820d2014-05-18 23:22:22 -0700804 } else {
805 /* Touch no longer active, close out slot */
806 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800807 }
Nick Dyerb9b05a82014-07-23 12:48:13 -0700808
809 data->update_input = true;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700810}
811
Nick Dyerb23157d2015-04-06 11:25:13 -0700812static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
813{
814 struct device *dev = &data->client->dev;
815 struct input_dev *input_dev = data->input_dev;
816 int id;
817 u8 status;
818 u8 type = 0;
819 u16 x;
820 u16 y;
821 int distance = 0;
822 int tool = 0;
823 u8 major = 0;
824 u8 pressure = 0;
825 u8 orientation = 0;
826
827 id = message[0] - data->T100_reportid_min - 2;
828
829 /* ignore SCRSTATUS events */
830 if (id < 0)
831 return;
832
833 status = message[1];
834 x = get_unaligned_le16(&message[2]);
835 y = get_unaligned_le16(&message[4]);
836
837 if (status & MXT_T100_DETECT) {
838 type = (status & MXT_T100_TYPE_MASK) >> 4;
839
840 switch (type) {
841 case MXT_T100_TYPE_HOVERING_FINGER:
842 tool = MT_TOOL_FINGER;
843 distance = MXT_DISTANCE_HOVERING;
844
845 if (data->t100_aux_vect)
846 orientation = message[data->t100_aux_vect];
847
848 break;
849
850 case MXT_T100_TYPE_FINGER:
851 case MXT_T100_TYPE_GLOVE:
852 tool = MT_TOOL_FINGER;
853 distance = MXT_DISTANCE_ACTIVE_TOUCH;
854
855 if (data->t100_aux_area)
856 major = message[data->t100_aux_area];
857
858 if (data->t100_aux_ampl)
859 pressure = message[data->t100_aux_ampl];
860
861 if (data->t100_aux_vect)
862 orientation = message[data->t100_aux_vect];
863
864 break;
865
866 case MXT_T100_TYPE_PASSIVE_STYLUS:
867 tool = MT_TOOL_PEN;
868
869 /*
870 * Passive stylus is reported with size zero so
871 * hardcode.
872 */
873 major = MXT_TOUCH_MAJOR_DEFAULT;
874
875 if (data->t100_aux_ampl)
876 pressure = message[data->t100_aux_ampl];
877
878 break;
879
880 case MXT_T100_TYPE_LARGE_TOUCH:
881 /* Ignore suppressed touch */
882 break;
883
884 default:
885 dev_dbg(dev, "Unexpected T100 type\n");
886 return;
887 }
888 }
889
890 /*
891 * Values reported should be non-zero if tool is touching the
892 * device
893 */
894 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
895 pressure = MXT_PRESSURE_DEFAULT;
896
897 input_mt_slot(input_dev, id);
898
899 if (status & MXT_T100_DETECT) {
900 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
901 id, type, x, y, major, pressure, orientation);
902
903 input_mt_report_slot_state(input_dev, tool, 1);
904 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
905 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
906 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
907 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
908 input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
909 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
910 } else {
911 dev_dbg(dev, "[%u] release\n", id);
912
913 /* close out slot */
914 input_mt_report_slot_state(input_dev, 0, 0);
915 }
916
917 data->update_input = true;
918}
919
Nick Dyerb9b05a82014-07-23 12:48:13 -0700920static int mxt_proc_message(struct mxt_data *data, u8 *message)
Daniel Kurtz04a79182012-06-28 21:08:21 +0800921{
Nick Dyerb9b05a82014-07-23 12:48:13 -0700922 u8 report_id = message[0];
923
924 if (report_id == MXT_RPTID_NOMSG)
925 return 0;
926
927 if (report_id == data->T6_reportid) {
928 mxt_proc_t6_messages(data, message);
929 } else if (!data->input_dev) {
930 /*
931 * Do not report events if input device
932 * is not yet registered.
933 */
934 mxt_dump_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700935 } else if (report_id >= data->T9_reportid_min &&
936 report_id <= data->T9_reportid_max) {
Nick Dyerb9b05a82014-07-23 12:48:13 -0700937 mxt_proc_t9_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700938 } else if (report_id >= data->T100_reportid_min &&
939 report_id <= data->T100_reportid_max) {
940 mxt_proc_t100_message(data, message);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700941 } else if (report_id == data->T19_reportid) {
942 mxt_input_button(data, message);
943 data->update_input = true;
944 } else {
945 mxt_dump_message(data, message);
946 }
947
948 return 1;
949}
950
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700951static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
Nick Dyerb9b05a82014-07-23 12:48:13 -0700952{
953 struct device *dev = &data->client->dev;
954 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700955 int i;
956 u8 num_valid = 0;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700957
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700958 /* Safety check for msg_buf */
959 if (count > data->max_reportid)
960 return -EINVAL;
961
962 /* Process remaining messages if necessary */
Nick Dyerb9b05a82014-07-23 12:48:13 -0700963 ret = __mxt_read_reg(data->client, data->T5_address,
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700964 data->T5_msg_size * count, data->msg_buf);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700965 if (ret) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700966 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700967 return ret;
968 }
969
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700970 for (i = 0; i < count; i++) {
971 ret = mxt_proc_message(data,
972 data->msg_buf + data->T5_msg_size * i);
973
974 if (ret == 1)
975 num_valid++;
976 }
977
978 /* return number of messages read */
979 return num_valid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700980}
981
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700982static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700983{
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700984 struct device *dev = &data->client->dev;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700985 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700986 u8 count, num_left;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700987
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700988 /* Read T44 and T5 together */
989 ret = __mxt_read_reg(data->client, data->T44_address,
990 data->T5_msg_size + 1, data->msg_buf);
991 if (ret) {
992 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
993 return IRQ_NONE;
994 }
995
996 count = data->msg_buf[0];
997
998 if (count == 0) {
Nick Dyer651b4602014-09-09 11:24:21 -0700999 /*
1000 * This condition is caused by the CHG line being configured
1001 * in Mode 0. It results in unnecessary I2C operations but it
1002 * is benign.
1003 */
1004 dev_dbg(dev, "Interrupt triggered but zero messages\n");
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001005 return IRQ_NONE;
1006 } else if (count > data->max_reportid) {
1007 dev_err(dev, "T44 count %d exceeded max report id\n", count);
1008 count = data->max_reportid;
1009 }
1010
1011 /* Process first message */
1012 ret = mxt_proc_message(data, data->msg_buf + 1);
1013 if (ret < 0) {
1014 dev_warn(dev, "Unexpected invalid message\n");
1015 return IRQ_NONE;
1016 }
1017
1018 num_left = count - 1;
1019
1020 /* Process remaining messages if necessary */
1021 if (num_left) {
1022 ret = mxt_read_and_process_messages(data, num_left);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001023 if (ret < 0)
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001024 goto end;
1025 else if (ret != num_left)
1026 dev_warn(dev, "Unexpected invalid message\n");
1027 }
1028
1029end:
1030 if (data->update_input) {
1031 mxt_input_sync(data);
1032 data->update_input = false;
1033 }
1034
1035 return IRQ_HANDLED;
1036}
1037
1038static int mxt_process_messages_until_invalid(struct mxt_data *data)
1039{
1040 struct device *dev = &data->client->dev;
1041 int count, read;
1042 u8 tries = 2;
1043
1044 count = data->max_reportid;
1045
1046 /* Read messages until we force an invalid */
1047 do {
1048 read = mxt_read_and_process_messages(data, count);
1049 if (read < count)
1050 return 0;
1051 } while (--tries);
1052
1053 if (data->update_input) {
1054 mxt_input_sync(data);
1055 data->update_input = false;
1056 }
1057
1058 dev_err(dev, "CHG pin isn't cleared\n");
1059 return -EBUSY;
1060}
1061
1062static irqreturn_t mxt_process_messages(struct mxt_data *data)
1063{
1064 int total_handled, num_handled;
1065 u8 count = data->last_message_count;
1066
1067 if (count < 1 || count > data->max_reportid)
1068 count = 1;
1069
1070 /* include final invalid message */
1071 total_handled = mxt_read_and_process_messages(data, count + 1);
1072 if (total_handled < 0)
1073 return IRQ_NONE;
1074 /* if there were invalid messages, then we are done */
1075 else if (total_handled <= count)
1076 goto update_count;
1077
1078 /* keep reading two msgs until one is invalid or reportid limit */
1079 do {
1080 num_handled = mxt_read_and_process_messages(data, 2);
1081 if (num_handled < 0)
Nick Dyer8d4e1632014-05-18 23:00:56 -07001082 return IRQ_NONE;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001083
1084 total_handled += num_handled;
1085
1086 if (num_handled < 2)
1087 break;
1088 } while (total_handled < data->num_touchids);
1089
1090update_count:
1091 data->last_message_count = total_handled;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001092
Nick Dyerb9b05a82014-07-23 12:48:13 -07001093 if (data->update_input) {
Benson Leungb735fbe2014-07-23 12:22:27 -07001094 mxt_input_sync(data);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001095 data->update_input = false;
1096 }
Daniel Kurtz64464ae2012-06-28 21:08:23 +08001097
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001098 return IRQ_HANDLED;
1099}
1100
Benson Leungd79e7e42014-05-18 23:02:52 -07001101static irqreturn_t mxt_interrupt(int irq, void *dev_id)
1102{
1103 struct mxt_data *data = dev_id;
1104
1105 if (data->in_bootloader) {
1106 /* bootloader state transition completion */
1107 complete(&data->bl_completion);
1108 return IRQ_HANDLED;
1109 }
1110
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001111 if (!data->object_table)
1112 return IRQ_HANDLED;
1113
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001114 if (data->T44_address) {
1115 return mxt_process_messages_t44(data);
1116 } else {
1117 return mxt_process_messages(data);
1118 }
Benson Leungd79e7e42014-05-18 23:02:52 -07001119}
1120
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001121static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
1122 u8 value, bool wait)
1123{
1124 u16 reg;
1125 u8 command_register;
1126 int timeout_counter = 0;
1127 int ret;
1128
1129 reg = data->T6_address + cmd_offset;
1130
1131 ret = mxt_write_reg(data->client, reg, value);
1132 if (ret)
1133 return ret;
1134
1135 if (!wait)
1136 return 0;
1137
1138 do {
1139 msleep(20);
1140 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
1141 if (ret)
1142 return ret;
1143 } while (command_register != 0 && timeout_counter++ <= 100);
1144
1145 if (timeout_counter > 100) {
1146 dev_err(&data->client->dev, "Command failed!\n");
1147 return -EIO;
1148 }
1149
1150 return 0;
1151}
1152
1153static int mxt_soft_reset(struct mxt_data *data)
1154{
1155 struct device *dev = &data->client->dev;
1156 int ret = 0;
1157
1158 dev_info(dev, "Resetting chip\n");
1159
1160 reinit_completion(&data->reset_completion);
1161
1162 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
1163 if (ret)
1164 return ret;
1165
1166 ret = mxt_wait_for_completion(data, &data->reset_completion,
1167 MXT_RESET_TIMEOUT);
1168 if (ret)
1169 return ret;
1170
1171 return 0;
1172}
1173
Nick Dyerc3f78042014-05-18 23:04:46 -07001174static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
1175{
1176 /*
1177 * On failure, CRC is set to 0 and config will always be
1178 * downloaded.
1179 */
1180 data->config_crc = 0;
1181 reinit_completion(&data->crc_completion);
1182
1183 mxt_t6_command(data, cmd, value, true);
1184
1185 /*
1186 * Wait for crc message. On failure, CRC is set to 0 and config will
1187 * always be downloaded.
1188 */
1189 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
1190}
1191
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001192static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
1193{
1194 static const unsigned int crcpoly = 0x80001B;
1195 u32 result;
1196 u32 data_word;
1197
1198 data_word = (secondbyte << 8) | firstbyte;
1199 result = ((*crc << 1) ^ data_word);
1200
1201 if (result & 0x1000000)
1202 result ^= crcpoly;
1203
1204 *crc = result;
1205}
1206
1207static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
1208{
1209 u32 crc = 0;
1210 u8 *ptr = base + start_off;
1211 u8 *last_val = base + end_off - 1;
1212
1213 if (end_off < start_off)
1214 return -EINVAL;
1215
1216 while (ptr < last_val) {
1217 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
1218 ptr += 2;
1219 }
1220
1221 /* if len is odd, fill the last byte with 0 */
1222 if (ptr == last_val)
1223 mxt_calc_crc24(&crc, *ptr, 0);
1224
1225 /* Mask to 24-bit */
1226 crc &= 0x00FFFFFF;
1227
1228 return crc;
1229}
1230
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001231static int mxt_prepare_cfg_mem(struct mxt_data *data,
1232 const struct firmware *cfg,
1233 unsigned int data_pos,
1234 unsigned int cfg_start_ofs,
1235 u8 *config_mem,
1236 size_t config_mem_size)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001237{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001238 struct device *dev = &data->client->dev;
Nick Dyer50a77c62014-07-23 12:38:48 -07001239 struct mxt_object *object;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001240 unsigned int type, instance, size, byte_offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001241 int offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001242 int ret;
Nick Dyer50a77c62014-07-23 12:38:48 -07001243 int i;
Nick Dyer50a77c62014-07-23 12:38:48 -07001244 u16 reg;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001245 u8 val;
Nick Dyer50a77c62014-07-23 12:38:48 -07001246
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001247 while (data_pos < cfg->size) {
Nick Dyer50a77c62014-07-23 12:38:48 -07001248 /* Read type, instance, length */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001249 ret = sscanf(cfg->data + data_pos, "%x %x %x%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001250 &type, &instance, &size, &offset);
1251 if (ret == 0) {
1252 /* EOF */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001253 break;
Nick Dyer50a77c62014-07-23 12:38:48 -07001254 } else if (ret != 3) {
1255 dev_err(dev, "Bad format: failed to parse object\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001256 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001257 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001258 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001259
1260 object = mxt_get_object(data, type);
1261 if (!object) {
1262 /* Skip object */
1263 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001264 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001265 &val, &offset);
1266 if (ret != 1) {
1267 dev_err(dev, "Bad format in T%d at %d\n",
1268 type, i);
1269 return -EINVAL;
1270 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001271 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001272 }
1273 continue;
1274 }
1275
1276 if (size > mxt_obj_size(object)) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001277 /*
1278 * Either we are in fallback mode due to wrong
1279 * config or config from a later fw version,
1280 * or the file is corrupt or hand-edited.
1281 */
1282 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1283 size - mxt_obj_size(object), type);
1284 } else if (mxt_obj_size(object) > size) {
1285 /*
1286 * If firmware is upgraded, new bytes may be added to
1287 * end of objects. It is generally forward compatible
1288 * to zero these bytes - previous behaviour will be
1289 * retained. However this does invalidate the CRC and
1290 * will force fallback mode until the configuration is
1291 * updated. We warn here but do nothing else - the
1292 * malloc has zeroed the entire configuration.
1293 */
1294 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1295 mxt_obj_size(object) - size, type);
Nick Dyer50a77c62014-07-23 12:38:48 -07001296 }
1297
1298 if (instance >= mxt_obj_instances(object)) {
1299 dev_err(dev, "Object instances exceeded!\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001300 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001301 }
1302
1303 reg = object->start_address + mxt_obj_size(object) * instance;
1304
1305 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001306 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001307 &val,
1308 &offset);
1309 if (ret != 1) {
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001310 dev_err(dev, "Bad format in T%d at %d\n",
1311 type, i);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001312 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001313 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001314 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001315
1316 if (i > mxt_obj_size(object))
1317 continue;
1318
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001319 byte_offset = reg + i - cfg_start_ofs;
Nick Dyer50a77c62014-07-23 12:38:48 -07001320
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001321 if (byte_offset >= 0 && byte_offset < config_mem_size) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001322 *(config_mem + byte_offset) = val;
1323 } else {
1324 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1325 reg, object->type, byte_offset);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001326 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001327 }
1328 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001329 }
1330
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001331 return 0;
1332}
1333
1334static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start,
1335 u8 *config_mem, size_t config_mem_size)
1336{
1337 unsigned int byte_offset = 0;
1338 int error;
1339
1340 /* Write configuration as blocks */
1341 while (byte_offset < config_mem_size) {
1342 unsigned int size = config_mem_size - byte_offset;
1343
1344 if (size > MXT_MAX_BLOCK_WRITE)
1345 size = MXT_MAX_BLOCK_WRITE;
1346
1347 error = __mxt_write_reg(data->client,
1348 cfg_start + byte_offset,
1349 size, config_mem + byte_offset);
1350 if (error) {
1351 dev_err(&data->client->dev,
1352 "Config write error, ret=%d\n", error);
1353 return error;
1354 }
1355
1356 byte_offset += size;
1357 }
1358
1359 return 0;
1360}
1361
1362/*
1363 * mxt_update_cfg - download configuration to chip
1364 *
1365 * Atmel Raw Config File Format
1366 *
1367 * The first four lines of the raw config file contain:
1368 * 1) Version
1369 * 2) Chip ID Information (first 7 bytes of device memory)
1370 * 3) Chip Information Block 24-bit CRC Checksum
1371 * 4) Chip Configuration 24-bit CRC Checksum
1372 *
1373 * The rest of the file consists of one line per object instance:
1374 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1375 *
1376 * <TYPE> - 2-byte object type as hex
1377 * <INSTANCE> - 2-byte object instance number as hex
1378 * <SIZE> - 2-byte object size as hex
1379 * <CONTENTS> - array of <SIZE> 1-byte hex values
1380 */
1381static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg)
1382{
1383 struct device *dev = &data->client->dev;
1384 struct mxt_info cfg_info;
1385 int ret;
1386 int offset;
1387 int data_pos;
1388 int i;
1389 int cfg_start_ofs;
1390 u32 info_crc, config_crc, calculated_crc;
1391 u8 *config_mem;
1392 size_t config_mem_size;
1393
1394 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
1395
1396 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
1397 dev_err(dev, "Unrecognised config file\n");
1398 return -EINVAL;
1399 }
1400
1401 data_pos = strlen(MXT_CFG_MAGIC);
1402
1403 /* Load information block and check */
1404 for (i = 0; i < sizeof(struct mxt_info); i++) {
1405 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1406 (unsigned char *)&cfg_info + i,
1407 &offset);
1408 if (ret != 1) {
1409 dev_err(dev, "Bad format\n");
1410 return -EINVAL;
1411 }
1412
1413 data_pos += offset;
1414 }
1415
1416 if (cfg_info.family_id != data->info.family_id) {
1417 dev_err(dev, "Family ID mismatch!\n");
1418 return -EINVAL;
1419 }
1420
1421 if (cfg_info.variant_id != data->info.variant_id) {
1422 dev_err(dev, "Variant ID mismatch!\n");
1423 return -EINVAL;
1424 }
1425
1426 /* Read CRCs */
1427 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset);
1428 if (ret != 1) {
1429 dev_err(dev, "Bad format: failed to parse Info CRC\n");
1430 return -EINVAL;
1431 }
1432 data_pos += offset;
1433
1434 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset);
1435 if (ret != 1) {
1436 dev_err(dev, "Bad format: failed to parse Config CRC\n");
1437 return -EINVAL;
1438 }
1439 data_pos += offset;
1440
1441 /*
1442 * The Info Block CRC is calculated over mxt_info and the object
1443 * table. If it does not match then we are trying to load the
1444 * configuration from a different chip or firmware version, so
1445 * the configuration CRC is invalid anyway.
1446 */
1447 if (info_crc == data->info_crc) {
1448 if (config_crc == 0 || data->config_crc == 0) {
1449 dev_info(dev, "CRC zero, attempting to apply config\n");
1450 } else if (config_crc == data->config_crc) {
1451 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
1452 data->config_crc);
1453 return 0;
1454 } else {
1455 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1456 data->config_crc, config_crc);
1457 }
1458 } else {
1459 dev_warn(dev,
1460 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1461 data->info_crc, info_crc);
1462 }
1463
1464 /* Malloc memory to store configuration */
1465 cfg_start_ofs = MXT_OBJECT_START +
1466 data->info.object_num * sizeof(struct mxt_object) +
1467 MXT_INFO_CHECKSUM_SIZE;
1468 config_mem_size = data->mem_size - cfg_start_ofs;
1469 config_mem = kzalloc(config_mem_size, GFP_KERNEL);
1470 if (!config_mem) {
1471 dev_err(dev, "Failed to allocate memory\n");
1472 return -ENOMEM;
1473 }
1474
1475 ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs,
1476 config_mem, config_mem_size);
1477 if (ret)
1478 goto release_mem;
1479
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001480 /* Calculate crc of the received configs (not the raw config file) */
1481 if (data->T7_address < cfg_start_ofs) {
1482 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n",
1483 data->T7_address, cfg_start_ofs);
1484 ret = 0;
1485 goto release_mem;
1486 }
1487
1488 calculated_crc = mxt_calculate_crc(config_mem,
1489 data->T7_address - cfg_start_ofs,
1490 config_mem_size);
1491
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001492 if (config_crc > 0 && config_crc != calculated_crc)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001493 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n",
1494 calculated_crc, config_crc);
1495
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001496 ret = mxt_upload_cfg_mem(data, cfg_start_ofs,
1497 config_mem, config_mem_size);
1498 if (ret)
1499 goto release_mem;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001500
Nick Dyerc3f78042014-05-18 23:04:46 -07001501 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1502
1503 ret = mxt_soft_reset(data);
1504 if (ret)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001505 goto release_mem;
Nick Dyerc3f78042014-05-18 23:04:46 -07001506
1507 dev_info(dev, "Config successfully updated\n");
1508
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001509release_mem:
1510 kfree(config_mem);
Nick Dyer50a77c62014-07-23 12:38:48 -07001511 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001512}
1513
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001514static int mxt_acquire_irq(struct mxt_data *data)
1515{
1516 int error;
1517
1518 enable_irq(data->irq);
1519
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001520 error = mxt_process_messages_until_invalid(data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001521 if (error)
1522 return error;
1523
1524 return 0;
1525}
1526
Iiro Valkonen7686b102011-02-02 23:21:58 -08001527static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001528{
1529 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001530 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001531 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001532
Daniel Kurtz23003a842012-06-28 21:08:14 +08001533 /* Read 7-byte info block starting at address 0 */
1534 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001535 if (error)
1536 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001537
1538 return 0;
1539}
1540
Stephen Warren58e4aeee2014-09-10 10:01:10 -07001541static void mxt_free_input_device(struct mxt_data *data)
1542{
1543 if (data->input_dev) {
1544 input_unregister_device(data->input_dev);
1545 data->input_dev = NULL;
1546 }
1547}
1548
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001549static void mxt_free_object_table(struct mxt_data *data)
1550{
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001551 kfree(data->object_table);
1552 data->object_table = NULL;
1553 kfree(data->msg_buf);
1554 data->msg_buf = NULL;
Nick Dyerb9b05a82014-07-23 12:48:13 -07001555 data->T5_address = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001556 data->T5_msg_size = 0;
1557 data->T6_reportid = 0;
1558 data->T7_address = 0;
1559 data->T9_reportid_min = 0;
1560 data->T9_reportid_max = 0;
1561 data->T19_reportid = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001562 data->T44_address = 0;
Nick Dyerb23157d2015-04-06 11:25:13 -07001563 data->T100_reportid_min = 0;
1564 data->T100_reportid_max = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001565 data->max_reportid = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001566}
1567
Iiro Valkonen7686b102011-02-02 23:21:58 -08001568static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001569{
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001570 struct i2c_client *client = data->client;
1571 size_t table_size;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001572 struct mxt_object *object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001573 int error;
1574 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001575 u8 reportid;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001576 u16 end_address;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001577
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001578 table_size = data->info.object_num * sizeof(struct mxt_object);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001579 object_table = kzalloc(table_size, GFP_KERNEL);
1580 if (!object_table) {
1581 dev_err(&data->client->dev, "Failed to allocate memory\n");
1582 return -ENOMEM;
1583 }
1584
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001585 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001586 object_table);
1587 if (error) {
1588 kfree(object_table);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001589 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001590 }
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001591
1592 /* Valid Report IDs start counting from 1 */
1593 reportid = 1;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001594 data->mem_size = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001595 for (i = 0; i < data->info.object_num; i++) {
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001596 struct mxt_object *object = object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001597 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001598
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001599 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001600
1601 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001602 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001603 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001604 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001605 max_id = reportid - 1;
1606 } else {
1607 min_id = 0;
1608 max_id = 0;
1609 }
1610
1611 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001612 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001613 object->type, object->start_address,
1614 mxt_obj_size(object), mxt_obj_instances(object),
1615 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001616
1617 switch (object->type) {
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001618 case MXT_GEN_MESSAGE_T5:
Nick Dyer437d4f32014-08-07 09:56:01 -07001619 if (data->info.family_id == 0x80 &&
1620 data->info.version < 0x20) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001621 /*
Nick Dyer437d4f32014-08-07 09:56:01 -07001622 * On mXT224 firmware versions prior to V2.0
1623 * read and discard unused CRC byte otherwise
1624 * DMA reads are misaligned.
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001625 */
1626 data->T5_msg_size = mxt_obj_size(object);
1627 } else {
1628 /* CRC not enabled, so skip last byte */
1629 data->T5_msg_size = mxt_obj_size(object) - 1;
1630 }
Nick Dyerb9b05a82014-07-23 12:48:13 -07001631 data->T5_address = object->start_address;
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001632 break;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001633 case MXT_GEN_COMMAND_T6:
1634 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001635 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001636 break;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001637 case MXT_GEN_POWER_T7:
1638 data->T7_address = object->start_address;
1639 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001640 case MXT_TOUCH_MULTI_T9:
Nick Dyerb23157d2015-04-06 11:25:13 -07001641 data->multitouch = MXT_TOUCH_MULTI_T9;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001642 data->T9_reportid_min = min_id;
1643 data->T9_reportid_max = max_id;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001644 data->num_touchids = object->num_report_ids
1645 * mxt_obj_instances(object);
1646 break;
1647 case MXT_SPT_MESSAGECOUNT_T44:
1648 data->T44_address = object->start_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001649 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001650 case MXT_SPT_GPIOPWM_T19:
1651 data->T19_reportid = min_id;
1652 break;
Nick Dyerb23157d2015-04-06 11:25:13 -07001653 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1654 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
1655 data->T100_reportid_min = min_id;
1656 data->T100_reportid_max = max_id;
1657 /* first two report IDs reserved */
1658 data->num_touchids = object->num_report_ids - 2;
1659 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001660 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001661
1662 end_address = object->start_address
1663 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1664
1665 if (end_address >= data->mem_size)
1666 data->mem_size = end_address + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001667 }
1668
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001669 /* Store maximum reportid */
1670 data->max_reportid = reportid;
1671
1672 /* If T44 exists, T5 position has to be directly after */
1673 if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
1674 dev_err(&client->dev, "Invalid T44 position\n");
1675 error = -EINVAL;
1676 goto free_object_table;
1677 }
1678
1679 data->msg_buf = kcalloc(data->max_reportid,
1680 data->T5_msg_size, GFP_KERNEL);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001681 if (!data->msg_buf) {
1682 dev_err(&client->dev, "Failed to allocate message buffer\n");
1683 error = -ENOMEM;
1684 goto free_object_table;
1685 }
1686
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001687 data->object_table = object_table;
1688
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001689 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001690
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001691free_object_table:
1692 mxt_free_object_table(data);
1693 return error;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001694}
1695
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001696static int mxt_read_t9_resolution(struct mxt_data *data)
1697{
1698 struct i2c_client *client = data->client;
1699 int error;
1700 struct t9_range range;
1701 unsigned char orient;
1702 struct mxt_object *object;
1703
1704 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1705 if (!object)
1706 return -EINVAL;
1707
1708 error = __mxt_read_reg(client,
1709 object->start_address + MXT_T9_RANGE,
1710 sizeof(range), &range);
1711 if (error)
1712 return error;
1713
1714 le16_to_cpus(&range.x);
1715 le16_to_cpus(&range.y);
1716
1717 error = __mxt_read_reg(client,
1718 object->start_address + MXT_T9_ORIENT,
1719 1, &orient);
1720 if (error)
1721 return error;
1722
1723 /* Handle default values */
1724 if (range.x == 0)
1725 range.x = 1023;
1726
1727 if (range.y == 0)
1728 range.y = 1023;
1729
Nick Dyerf3889ed2014-05-18 23:22:04 -07001730 if (orient & MXT_T9_ORIENT_SWITCH) {
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001731 data->max_x = range.y;
1732 data->max_y = range.x;
1733 } else {
1734 data->max_x = range.x;
1735 data->max_y = range.y;
1736 }
1737
1738 dev_dbg(&client->dev,
1739 "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1740
1741 return 0;
1742}
1743
Nick Dyerb23157d2015-04-06 11:25:13 -07001744static int mxt_read_t100_config(struct mxt_data *data)
1745{
1746 struct i2c_client *client = data->client;
1747 int error;
1748 struct mxt_object *object;
1749 u16 range_x, range_y;
1750 u8 cfg, tchaux;
1751 u8 aux;
1752
1753 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
1754 if (!object)
1755 return -EINVAL;
1756
1757 error = __mxt_read_reg(client,
1758 object->start_address + MXT_T100_XRANGE,
1759 sizeof(range_x), &range_x);
1760 if (error)
1761 return error;
1762
1763 le16_to_cpus(&range_x);
1764
1765 error = __mxt_read_reg(client,
1766 object->start_address + MXT_T100_YRANGE,
1767 sizeof(range_y), &range_y);
1768 if (error)
1769 return error;
1770
1771 le16_to_cpus(&range_y);
1772
1773 error = __mxt_read_reg(client,
1774 object->start_address + MXT_T100_CFG1,
1775 1, &cfg);
1776 if (error)
1777 return error;
1778
1779 error = __mxt_read_reg(client,
1780 object->start_address + MXT_T100_TCHAUX,
1781 1, &tchaux);
1782 if (error)
1783 return error;
1784
1785 /* Handle default values */
1786 if (range_x == 0)
1787 range_x = 1023;
1788
1789 if (range_y == 0)
1790 range_y = 1023;
1791
1792 if (cfg & MXT_T100_CFG_SWITCHXY) {
1793 data->max_x = range_y;
1794 data->max_y = range_x;
1795 } else {
1796 data->max_x = range_x;
1797 data->max_y = range_y;
1798 }
1799
1800 /* allocate aux bytes */
1801 aux = 6;
1802
1803 if (tchaux & MXT_T100_TCHAUX_VECT)
1804 data->t100_aux_vect = aux++;
1805
1806 if (tchaux & MXT_T100_TCHAUX_AMPL)
1807 data->t100_aux_ampl = aux++;
1808
1809 if (tchaux & MXT_T100_TCHAUX_AREA)
1810 data->t100_aux_area = aux++;
1811
1812 dev_dbg(&client->dev,
1813 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1814 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
1815
1816 dev_info(&client->dev,
1817 "T100 Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1818
1819 return 0;
1820}
1821
Nick Dyer7a53d602014-07-23 12:21:26 -07001822static int mxt_input_open(struct input_dev *dev);
1823static void mxt_input_close(struct input_dev *dev);
1824
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001825static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
1826 struct mxt_data *data)
1827{
1828 const struct mxt_platform_data *pdata = data->pdata;
1829 int i;
1830
1831 input_dev->name = "Atmel maXTouch Touchpad";
1832
1833 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1834
1835 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1836 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1837 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1838 MXT_PIXELS_PER_MM);
1839 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1840 MXT_PIXELS_PER_MM);
1841
1842 for (i = 0; i < pdata->t19_num_keys; i++)
1843 if (pdata->t19_keymap[i] != KEY_RESERVED)
1844 input_set_capability(input_dev, EV_KEY,
1845 pdata->t19_keymap[i]);
1846}
1847
Nick Dyerb23157d2015-04-06 11:25:13 -07001848static int mxt_initialize_input_device(struct mxt_data *data)
Nick Dyer7a53d602014-07-23 12:21:26 -07001849{
Nick Dyer7a53d602014-07-23 12:21:26 -07001850 const struct mxt_platform_data *pdata = data->pdata;
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001851 struct device *dev = &data->client->dev;
Nick Dyer7a53d602014-07-23 12:21:26 -07001852 struct input_dev *input_dev;
1853 int error;
1854 unsigned int num_mt_slots;
1855 unsigned int mt_flags = 0;
Nick Dyer7a53d602014-07-23 12:21:26 -07001856
Nick Dyerb23157d2015-04-06 11:25:13 -07001857 switch (data->multitouch) {
1858 case MXT_TOUCH_MULTI_T9:
1859 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1860 error = mxt_read_t9_resolution(data);
1861 if (error)
1862 dev_warn(dev, "Failed to initialize T9 resolution\n");
1863 break;
1864
1865 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1866 num_mt_slots = data->num_touchids;
1867 error = mxt_read_t100_config(data);
1868 if (error)
1869 dev_warn(dev, "Failed to read T100 config\n");
1870 break;
1871
1872 default:
1873 dev_err(dev, "Invalid multitouch object\n");
1874 return -EINVAL;
1875 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001876
1877 input_dev = input_allocate_device();
1878 if (!input_dev) {
1879 dev_err(dev, "Failed to allocate memory\n");
1880 return -ENOMEM;
1881 }
1882
1883 input_dev->name = "Atmel maXTouch Touchscreen";
1884 input_dev->phys = data->phys;
1885 input_dev->id.bustype = BUS_I2C;
1886 input_dev->dev.parent = dev;
1887 input_dev->open = mxt_input_open;
1888 input_dev->close = mxt_input_close;
1889
Nick Dyerb23157d2015-04-06 11:25:13 -07001890 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
Nick Dyer7a53d602014-07-23 12:21:26 -07001891
Nick Dyer7a53d602014-07-23 12:21:26 -07001892 /* For single touch */
Nick Dyerb23157d2015-04-06 11:25:13 -07001893 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
1894 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
1895
1896 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1897 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1898 data->t100_aux_ampl)) {
1899 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
1900 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001901
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001902 /* If device has buttons we assume it is a touchpad */
1903 if (pdata->t19_num_keys) {
1904 mxt_set_up_as_touchpad(input_dev, data);
1905 mt_flags |= INPUT_MT_POINTER;
1906 }
1907
Nick Dyer7a53d602014-07-23 12:21:26 -07001908 /* For multi touch */
Nick Dyer7a53d602014-07-23 12:21:26 -07001909 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
1910 if (error) {
1911 dev_err(dev, "Error %d initialising slots\n", error);
1912 goto err_free_mem;
1913 }
1914
Nick Dyerb23157d2015-04-06 11:25:13 -07001915 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
1916 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
1917 0, MT_TOOL_MAX, 0, 0);
1918 input_set_abs_params(input_dev, ABS_MT_DISTANCE,
1919 MXT_DISTANCE_ACTIVE_TOUCH,
1920 MXT_DISTANCE_HOVERING,
1921 0, 0);
1922 }
1923
Nick Dyer7a53d602014-07-23 12:21:26 -07001924 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1925 0, data->max_x, 0, 0);
1926 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1927 0, data->max_y, 0, 0);
Nick Dyerb23157d2015-04-06 11:25:13 -07001928
1929 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1930 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1931 data->t100_aux_area)) {
1932 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1933 0, MXT_MAX_AREA, 0, 0);
1934 }
1935
1936 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1937 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1938 data->t100_aux_ampl)) {
1939 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1940 0, 255, 0, 0);
1941 }
1942
1943 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1944 data->t100_aux_vect) {
1945 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
1946 0, 255, 0, 0);
1947 }
1948
1949 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1950 data->t100_aux_ampl) {
1951 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1952 0, 255, 0, 0);
1953 }
1954
1955 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1956 data->t100_aux_vect) {
1957 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
1958 0, 255, 0, 0);
1959 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001960
1961 input_set_drvdata(input_dev, data);
1962
1963 error = input_register_device(input_dev);
1964 if (error) {
1965 dev_err(dev, "Error %d registering input device\n", error);
1966 goto err_free_mem;
1967 }
1968
1969 data->input_dev = input_dev;
1970
1971 return 0;
1972
1973err_free_mem:
1974 input_free_device(input_dev);
1975 return error;
1976}
1977
Nick Dyer50a77c62014-07-23 12:38:48 -07001978static int mxt_configure_objects(struct mxt_data *data,
1979 const struct firmware *cfg);
1980
1981static void mxt_config_cb(const struct firmware *cfg, void *ctx)
1982{
1983 mxt_configure_objects(ctx, cfg);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001984 release_firmware(cfg);
Nick Dyer50a77c62014-07-23 12:38:48 -07001985}
1986
Iiro Valkonen7686b102011-02-02 23:21:58 -08001987static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001988{
1989 struct i2c_client *client = data->client;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07001990 int recovery_attempts = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001991 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001992
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07001993 while (1) {
1994 error = mxt_get_info(data);
1995 if (!error)
1996 break;
1997
1998 /* Check bootloader state */
1999 error = mxt_probe_bootloader(data, false);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002000 if (error) {
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002001 dev_info(&client->dev, "Trying alternate bootloader address\n");
2002 error = mxt_probe_bootloader(data, true);
2003 if (error) {
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002004 /* Chip is not in appmode or bootloader mode */
2005 return error;
2006 }
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002007 }
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002008
2009 /* OK, we are in bootloader, see if we can recover */
2010 if (++recovery_attempts > 1) {
2011 dev_err(&client->dev, "Could not recover from bootloader mode\n");
2012 /*
2013 * We can reflash from this state, so do not
2014 * abort initialization.
2015 */
2016 data->in_bootloader = true;
2017 return 0;
2018 }
2019
2020 /* Attempt to exit bootloader into app mode */
2021 mxt_send_bootloader_cmd(data, false);
2022 msleep(MXT_FW_RESET_TIME);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002023 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002024
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002025 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08002026 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07002027 if (error) {
2028 dev_err(&client->dev, "Error %d reading object table\n", error);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002029 return error;
Nick Dyer7bed6802014-05-18 23:04:09 -07002030 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002031
Dmitry Torokhov041fa152014-08-11 10:58:29 -07002032 error = mxt_acquire_irq(data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002033 if (error)
2034 goto err_free_object_table;
2035
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002036 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
2037 &client->dev, GFP_KERNEL, data,
2038 mxt_config_cb);
2039 if (error) {
2040 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
2041 error);
2042 goto err_free_object_table;
2043 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002044
2045 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08002046
2047err_free_object_table:
2048 mxt_free_object_table(data);
2049 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002050}
2051
Nick Dyer50a77c62014-07-23 12:38:48 -07002052static int mxt_configure_objects(struct mxt_data *data,
2053 const struct firmware *cfg)
2054{
2055 struct device *dev = &data->client->dev;
2056 struct mxt_info *info = &data->info;
2057 int error;
2058
2059 if (cfg) {
2060 error = mxt_update_cfg(data, cfg);
2061 if (error)
2062 dev_warn(dev, "Error %d updating config\n", error);
2063 }
2064
Nick Dyerb23157d2015-04-06 11:25:13 -07002065 if (data->multitouch) {
2066 error = mxt_initialize_input_device(data);
2067 if (error)
2068 return error;
2069 } else {
2070 dev_warn(dev, "No touch object detected\n");
2071 }
Nick Dyer50a77c62014-07-23 12:38:48 -07002072
2073 dev_info(dev,
2074 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
2075 info->family_id, info->variant_id, info->version >> 4,
2076 info->version & 0xf, info->build, info->object_num);
2077
2078 return 0;
2079}
2080
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002081/* Firmware Version is returned as Major.Minor.Build */
2082static ssize_t mxt_fw_version_show(struct device *dev,
2083 struct device_attribute *attr, char *buf)
2084{
2085 struct mxt_data *data = dev_get_drvdata(dev);
2086 struct mxt_info *info = &data->info;
2087 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
2088 info->version >> 4, info->version & 0xf, info->build);
2089}
2090
2091/* Hardware Version is returned as FamilyID.VariantID */
2092static ssize_t mxt_hw_version_show(struct device *dev,
2093 struct device_attribute *attr, char *buf)
2094{
2095 struct mxt_data *data = dev_get_drvdata(dev);
2096 struct mxt_info *info = &data->info;
2097 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
2098 info->family_id, info->variant_id);
2099}
2100
Daniel Kurtz794eb672012-06-28 21:08:10 +08002101static ssize_t mxt_show_instance(char *buf, int count,
2102 struct mxt_object *object, int instance,
2103 const u8 *val)
2104{
2105 int i;
2106
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002107 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002108 count += scnprintf(buf + count, PAGE_SIZE - count,
2109 "Instance %u\n", instance);
2110
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002111 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002112 count += scnprintf(buf + count, PAGE_SIZE - count,
2113 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
2114 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
2115
2116 return count;
2117}
2118
Iiro Valkonen7686b102011-02-02 23:21:58 -08002119static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002120 struct device_attribute *attr, char *buf)
2121{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002122 struct mxt_data *data = dev_get_drvdata(dev);
2123 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002124 int count = 0;
2125 int i, j;
2126 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002127 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002128
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002129 /* Pre-allocate buffer large enough to hold max sized object. */
2130 obuf = kmalloc(256, GFP_KERNEL);
2131 if (!obuf)
2132 return -ENOMEM;
2133
2134 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002135 for (i = 0; i < data->info.object_num; i++) {
2136 object = data->object_table + i;
2137
Daniel Kurtz91630952012-06-28 21:08:09 +08002138 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002139 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002140
Daniel Kurtz91630952012-06-28 21:08:09 +08002141 count += scnprintf(buf + count, PAGE_SIZE - count,
2142 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002143
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002144 for (j = 0; j < mxt_obj_instances(object); j++) {
2145 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08002146 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002147
Daniel Kurtz794eb672012-06-28 21:08:10 +08002148 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002149 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002150 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002151
Daniel Kurtz794eb672012-06-28 21:08:10 +08002152 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002153 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002154 }
2155
Daniel Kurtz794eb672012-06-28 21:08:10 +08002156done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002157 kfree(obuf);
2158 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002159}
2160
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002161static int mxt_check_firmware_format(struct device *dev,
2162 const struct firmware *fw)
2163{
2164 unsigned int pos = 0;
2165 char c;
2166
2167 while (pos < fw->size) {
2168 c = *(fw->data + pos);
2169
2170 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
2171 return 0;
2172
2173 pos++;
2174 }
2175
2176 /*
2177 * To convert file try:
2178 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2179 */
2180 dev_err(dev, "Aborting: firmware file must be in binary format\n");
2181
2182 return -EINVAL;
2183}
2184
Iiro Valkonen7686b102011-02-02 23:21:58 -08002185static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002186{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002187 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002188 const struct firmware *fw = NULL;
2189 unsigned int frame_size;
2190 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07002191 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07002192 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002193 int ret;
2194
2195 ret = request_firmware(&fw, fn, dev);
2196 if (ret) {
2197 dev_err(dev, "Unable to open firmware %s\n", fn);
2198 return ret;
2199 }
2200
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002201 /* Check for incorrect enc file */
2202 ret = mxt_check_firmware_format(dev, fw);
2203 if (ret)
2204 goto release_firmware;
2205
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002206 if (!data->in_bootloader) {
2207 /* Change to the bootloader mode */
2208 data->in_bootloader = true;
Benson Leungd79e7e42014-05-18 23:02:52 -07002209
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002210 ret = mxt_t6_command(data, MXT_COMMAND_RESET,
2211 MXT_BOOT_VALUE, false);
2212 if (ret)
2213 goto release_firmware;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002214
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002215 msleep(MXT_RESET_TIME);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002216
2217 /* Do not need to scan since we know family ID */
2218 ret = mxt_lookup_bootloader_address(data, 0);
2219 if (ret)
2220 goto release_firmware;
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002221
2222 mxt_free_input_device(data);
2223 mxt_free_object_table(data);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002224 } else {
2225 enable_irq(data->irq);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002226 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002227
Benson Leungd79e7e42014-05-18 23:02:52 -07002228 reinit_completion(&data->bl_completion);
2229
Nick Dyer385deb92014-07-23 12:42:40 -07002230 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
2231 if (ret) {
2232 /* Bootloader may still be unlocked from previous attempt */
2233 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
2234 if (ret)
2235 goto disable_irq;
2236 } else {
2237 dev_info(dev, "Unlocking bootloader\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002238
Nick Dyer385deb92014-07-23 12:42:40 -07002239 /* Unlock bootloader */
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002240 ret = mxt_send_bootloader_cmd(data, true);
Nick Dyer385deb92014-07-23 12:42:40 -07002241 if (ret)
2242 goto disable_irq;
2243 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002244
2245 while (pos < fw->size) {
Nick Dyer385deb92014-07-23 12:42:40 -07002246 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002247 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07002248 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002249
2250 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
2251
Nick Dyerf943c742014-05-18 23:14:20 -07002252 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002253 frame_size += 2;
2254
2255 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07002256 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
2257 if (ret)
2258 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002259
Nick Dyer385deb92014-07-23 12:42:40 -07002260 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
Nick Dyerf943c742014-05-18 23:14:20 -07002261 if (ret) {
2262 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002263
Nick Dyerf943c742014-05-18 23:14:20 -07002264 /* Back off by 20ms per retry */
2265 msleep(retry * 20);
2266
2267 if (retry > 20) {
2268 dev_err(dev, "Retry count exceeded\n");
2269 goto disable_irq;
2270 }
2271 } else {
2272 retry = 0;
2273 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07002274 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07002275 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002276
Nick Dyerf477c752014-05-18 23:14:45 -07002277 if (frame % 50 == 0)
2278 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
2279 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002280 }
2281
Benson Leunga0434b72014-05-18 23:03:09 -07002282 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002283 ret = mxt_wait_for_completion(data, &data->bl_completion,
2284 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002285 if (ret)
2286 goto disable_irq;
2287
Nick Dyerf477c752014-05-18 23:14:45 -07002288 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
2289
Benson Leunga0434b72014-05-18 23:03:09 -07002290 /*
2291 * Wait for device to reset. Some bootloader versions do not assert
2292 * the CHG line after bootloading has finished, so ignore potential
2293 * errors.
2294 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002295 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002296
Benson Leungd79e7e42014-05-18 23:02:52 -07002297 data->in_bootloader = false;
2298
2299disable_irq:
2300 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002301release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002302 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002303 return ret;
2304}
2305
Iiro Valkonen7686b102011-02-02 23:21:58 -08002306static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002307 struct device_attribute *attr,
2308 const char *buf, size_t count)
2309{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002310 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002311 int error;
2312
Iiro Valkonen7686b102011-02-02 23:21:58 -08002313 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002314 if (error) {
2315 dev_err(dev, "The firmware update failed(%d)\n", error);
2316 count = error;
2317 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07002318 dev_info(dev, "The firmware update succeeded\n");
2319
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002320 error = mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07002321 if (error)
2322 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002323 }
2324
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002325 return count;
2326}
2327
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002328static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
2329static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07002330static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
2331static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002332
Iiro Valkonen7686b102011-02-02 23:21:58 -08002333static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002334 &dev_attr_fw_version.attr,
2335 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002336 &dev_attr_object.attr,
2337 &dev_attr_update_fw.attr,
2338 NULL
2339};
2340
Iiro Valkonen7686b102011-02-02 23:21:58 -08002341static const struct attribute_group mxt_attr_group = {
2342 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002343};
2344
Iiro Valkonen7686b102011-02-02 23:21:58 -08002345static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002346{
Linus Torvalds7f405482014-12-31 12:59:34 -08002347 /* Touch enable */
Nick Dyerb23157d2015-04-06 11:25:13 -07002348 mxt_write_object(data, data->multitouch, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002349}
2350
Iiro Valkonen7686b102011-02-02 23:21:58 -08002351static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002352{
Linus Torvalds7f405482014-12-31 12:59:34 -08002353 /* Touch disable */
Nick Dyerb23157d2015-04-06 11:25:13 -07002354 mxt_write_object(data, data->multitouch, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002355}
2356
Iiro Valkonen7686b102011-02-02 23:21:58 -08002357static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002358{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002359 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002360
Iiro Valkonen7686b102011-02-02 23:21:58 -08002361 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002362
2363 return 0;
2364}
2365
Iiro Valkonen7686b102011-02-02 23:21:58 -08002366static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002367{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002368 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002369
Iiro Valkonen7686b102011-02-02 23:21:58 -08002370 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002371}
2372
Stephen Warren78188be2014-07-23 12:23:23 -07002373#ifdef CONFIG_OF
2374static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
2375{
2376 struct mxt_platform_data *pdata;
2377 u32 *keymap;
2378 u32 keycode;
2379 int proplen, i, ret;
2380
2381 if (!client->dev.of_node)
2382 return ERR_PTR(-ENODEV);
2383
2384 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
2385 if (!pdata)
2386 return ERR_PTR(-ENOMEM);
2387
2388 if (of_find_property(client->dev.of_node, "linux,gpio-keymap",
2389 &proplen)) {
2390 pdata->t19_num_keys = proplen / sizeof(u32);
2391
2392 keymap = devm_kzalloc(&client->dev,
2393 pdata->t19_num_keys * sizeof(keymap[0]),
2394 GFP_KERNEL);
2395 if (!keymap)
2396 return ERR_PTR(-ENOMEM);
2397
2398 for (i = 0; i < pdata->t19_num_keys; i++) {
2399 ret = of_property_read_u32_index(client->dev.of_node,
2400 "linux,gpio-keymap", i, &keycode);
2401 if (ret)
2402 keycode = KEY_RESERVED;
2403
2404 keymap[i] = keycode;
2405 }
2406
2407 pdata->t19_keymap = keymap;
2408 }
2409
2410 return pdata;
2411}
2412#else
2413static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
2414{
2415 dev_dbg(&client->dev, "No platform data specified\n");
2416 return ERR_PTR(-EINVAL);
2417}
2418#endif
2419
2420static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002421{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002422 struct mxt_data *data;
Stephen Warren78188be2014-07-23 12:23:23 -07002423 const struct mxt_platform_data *pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002424 int error;
2425
Stephen Warren78188be2014-07-23 12:23:23 -07002426 pdata = dev_get_platdata(&client->dev);
2427 if (!pdata) {
2428 pdata = mxt_parse_dt(client);
2429 if (IS_ERR(pdata))
2430 return PTR_ERR(pdata);
2431 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002432
Iiro Valkonen7686b102011-02-02 23:21:58 -08002433 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Nick Dyer7a53d602014-07-23 12:21:26 -07002434 if (!data) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002435 dev_err(&client->dev, "Failed to allocate memory\n");
Nick Dyer7a53d602014-07-23 12:21:26 -07002436 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002437 }
2438
Daniel Kurtzec02ac22012-06-28 21:08:02 +08002439 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
2440 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08002441
Joonyoung Shim910d8052011-04-12 23:14:38 -07002442 data->client = client;
Joonyoung Shim910d8052011-04-12 23:14:38 -07002443 data->pdata = pdata;
2444 data->irq = client->irq;
Nick Dyer7a53d602014-07-23 12:21:26 -07002445 i2c_set_clientdata(client, data);
Joonyoung Shim910d8052011-04-12 23:14:38 -07002446
Benson Leungd79e7e42014-05-18 23:02:52 -07002447 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002448 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07002449 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07002450
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002451 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
2452 pdata->irqflags | IRQF_ONESHOT,
2453 client->name, data);
2454 if (error) {
2455 dev_err(&client->dev, "Failed to register interrupt\n");
2456 goto err_free_mem;
2457 }
2458
2459 disable_irq(client->irq);
2460
Daniel Kurtzcb159112012-06-28 21:08:22 +08002461 error = mxt_initialize(data);
2462 if (error)
Nick Dyer7a53d602014-07-23 12:21:26 -07002463 goto err_free_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002464
Iiro Valkonen7686b102011-02-02 23:21:58 -08002465 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07002466 if (error) {
2467 dev_err(&client->dev, "Failure %d creating sysfs group\n",
2468 error);
Nick Dyer7a53d602014-07-23 12:21:26 -07002469 goto err_free_object;
Nick Dyer7bed6802014-05-18 23:04:09 -07002470 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002471
2472 return 0;
2473
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002474err_free_object:
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002475 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07002476 mxt_free_object_table(data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002477err_free_irq:
2478 free_irq(client->irq, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002479err_free_mem:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002480 kfree(data);
2481 return error;
2482}
2483
Bill Pembertone2619cf2012-11-23 21:50:47 -08002484static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002485{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002486 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002487
Iiro Valkonen7686b102011-02-02 23:21:58 -08002488 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002489 free_irq(data->irq, data);
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002490 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07002491 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002492 kfree(data);
2493
2494 return 0;
2495}
2496
Jingoo Han02b6a582014-11-02 00:04:14 -07002497static int __maybe_unused mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002498{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002499 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08002500 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002501 struct input_dev *input_dev = data->input_dev;
2502
2503 mutex_lock(&input_dev->mutex);
2504
2505 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08002506 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002507
2508 mutex_unlock(&input_dev->mutex);
2509
2510 return 0;
2511}
2512
Jingoo Han02b6a582014-11-02 00:04:14 -07002513static int __maybe_unused mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002514{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002515 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08002516 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002517 struct input_dev *input_dev = data->input_dev;
2518
Linus Torvalds7f405482014-12-31 12:59:34 -08002519 mxt_soft_reset(data);
2520
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002521 mutex_lock(&input_dev->mutex);
2522
2523 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08002524 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002525
2526 mutex_unlock(&input_dev->mutex);
2527
2528 return 0;
2529}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002530
Daniel Kurtz3a73c812012-05-08 22:29:14 -07002531static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
2532
Stephen Warren78188be2014-07-23 12:23:23 -07002533static const struct of_device_id mxt_of_match[] = {
2534 { .compatible = "atmel,maxtouch", },
2535 {},
2536};
2537MODULE_DEVICE_TABLE(of, mxt_of_match);
2538
Iiro Valkonen7686b102011-02-02 23:21:58 -08002539static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002540 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002541 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08002542 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08002543 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002544 { }
2545};
Iiro Valkonen7686b102011-02-02 23:21:58 -08002546MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002547
Iiro Valkonen7686b102011-02-02 23:21:58 -08002548static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002549 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08002550 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002551 .owner = THIS_MODULE,
Stephen Warren78188be2014-07-23 12:23:23 -07002552 .of_match_table = of_match_ptr(mxt_of_match),
Iiro Valkonen7686b102011-02-02 23:21:58 -08002553 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002554 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002555 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08002556 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08002557 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002558};
2559
Axel Lin1b92c1c2012-03-16 23:05:41 -07002560module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002561
2562/* Module information */
2563MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08002564MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002565MODULE_LICENSE("GPL");