blob: 44c1be65dbb423ba9e75f89cf8057fae19c57ed7 [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07005 * Copyright (C) 2012 Google, Inc.
6 *
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07007 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/module.h>
Benson Leungd79e7e42014-05-18 23:02:52 -070017#include <linux/init.h>
18#include <linux/completion.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070019#include <linux/delay.h>
20#include <linux/firmware.h>
21#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080022#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070023#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070024#include <linux/interrupt.h>
25#include <linux/slab.h>
26
27/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080028#define MXT_VER_20 20
29#define MXT_VER_21 21
30#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070031
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070032/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080033#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070034
35/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080036#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080037#define MXT_FAMILY_ID 0x00
38#define MXT_VARIANT_ID 0x01
39#define MXT_VERSION 0x02
40#define MXT_BUILD 0x03
41#define MXT_MATRIX_X_SIZE 0x04
42#define MXT_MATRIX_Y_SIZE 0x05
43#define MXT_OBJECT_NUM 0x06
44#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070045
Iiro Valkonen7686b102011-02-02 23:21:58 -080046#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070047
48/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070049#define MXT_DEBUG_DIAGNOSTIC_T37 37
50#define MXT_GEN_MESSAGE_T5 5
51#define MXT_GEN_COMMAND_T6 6
52#define MXT_GEN_POWER_T7 7
53#define MXT_GEN_ACQUIRE_T8 8
54#define MXT_GEN_DATASOURCE_T53 53
55#define MXT_TOUCH_MULTI_T9 9
56#define MXT_TOUCH_KEYARRAY_T15 15
57#define MXT_TOUCH_PROXIMITY_T23 23
58#define MXT_TOUCH_PROXKEY_T52 52
59#define MXT_PROCI_GRIPFACE_T20 20
60#define MXT_PROCG_NOISE_T22 22
61#define MXT_PROCI_ONETOUCH_T24 24
62#define MXT_PROCI_TWOTOUCH_T27 27
63#define MXT_PROCI_GRIP_T40 40
64#define MXT_PROCI_PALM_T41 41
65#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
66#define MXT_PROCI_STYLUS_T47 47
67#define MXT_PROCG_NOISESUPPRESSION_T48 48
68#define MXT_SPT_COMMSCONFIG_T18 18
69#define MXT_SPT_GPIOPWM_T19 19
70#define MXT_SPT_SELFTEST_T25 25
71#define MXT_SPT_CTECONFIG_T28 28
72#define MXT_SPT_USERDATA_T38 38
73#define MXT_SPT_DIGITIZER_T43 43
74#define MXT_SPT_MESSAGECOUNT_T44 44
75#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070076
Iiro Valkonen81c88a72011-07-04 03:08:25 -070077/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080078#define MXT_COMMAND_RESET 0
79#define MXT_COMMAND_BACKUPNV 1
80#define MXT_COMMAND_CALIBRATE 2
81#define MXT_COMMAND_REPORTALL 3
82#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070083
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070084/* Define for T6 status byte */
85#define MXT_T6_STATUS_RESET (1 << 7)
86
Iiro Valkonen81c88a72011-07-04 03:08:25 -070087/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080088#define MXT_POWER_IDLEACQINT 0
89#define MXT_POWER_ACTVACQINT 1
90#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070091
Iiro Valkonen81c88a72011-07-04 03:08:25 -070092/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080093#define MXT_ACQUIRE_CHRGTIME 0
94#define MXT_ACQUIRE_TCHDRIFT 2
95#define MXT_ACQUIRE_DRIFTST 3
96#define MXT_ACQUIRE_TCHAUTOCAL 4
97#define MXT_ACQUIRE_SYNC 5
98#define MXT_ACQUIRE_ATCHCALST 6
99#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700100
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700101/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800102#define MXT_TOUCH_CTRL 0
103#define MXT_TOUCH_XORIGIN 1
104#define MXT_TOUCH_YORIGIN 2
105#define MXT_TOUCH_XSIZE 3
106#define MXT_TOUCH_YSIZE 4
107#define MXT_TOUCH_BLEN 6
108#define MXT_TOUCH_TCHTHR 7
109#define MXT_TOUCH_TCHDI 8
110#define MXT_TOUCH_ORIENT 9
111#define MXT_TOUCH_MOVHYSTI 11
112#define MXT_TOUCH_MOVHYSTN 12
113#define MXT_TOUCH_NUMTOUCH 14
114#define MXT_TOUCH_MRGHYST 15
115#define MXT_TOUCH_MRGTHR 16
116#define MXT_TOUCH_AMPHYST 17
117#define MXT_TOUCH_XRANGE_LSB 18
118#define MXT_TOUCH_XRANGE_MSB 19
119#define MXT_TOUCH_YRANGE_LSB 20
120#define MXT_TOUCH_YRANGE_MSB 21
121#define MXT_TOUCH_XLOCLIP 22
122#define MXT_TOUCH_XHICLIP 23
123#define MXT_TOUCH_YLOCLIP 24
124#define MXT_TOUCH_YHICLIP 25
125#define MXT_TOUCH_XEDGECTRL 26
126#define MXT_TOUCH_XEDGEDIST 27
127#define MXT_TOUCH_YEDGECTRL 28
128#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700129#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700130
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700131/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800132#define MXT_GRIPFACE_CTRL 0
133#define MXT_GRIPFACE_XLOGRIP 1
134#define MXT_GRIPFACE_XHIGRIP 2
135#define MXT_GRIPFACE_YLOGRIP 3
136#define MXT_GRIPFACE_YHIGRIP 4
137#define MXT_GRIPFACE_MAXTCHS 5
138#define MXT_GRIPFACE_SZTHR1 7
139#define MXT_GRIPFACE_SZTHR2 8
140#define MXT_GRIPFACE_SHPTHR1 9
141#define MXT_GRIPFACE_SHPTHR2 10
142#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700143
Iiro Valkonen7686b102011-02-02 23:21:58 -0800144/* MXT_PROCI_NOISE field */
145#define MXT_NOISE_CTRL 0
146#define MXT_NOISE_OUTFLEN 1
147#define MXT_NOISE_GCAFUL_LSB 3
148#define MXT_NOISE_GCAFUL_MSB 4
149#define MXT_NOISE_GCAFLL_LSB 5
150#define MXT_NOISE_GCAFLL_MSB 6
151#define MXT_NOISE_ACTVGCAFVALID 7
152#define MXT_NOISE_NOISETHR 8
153#define MXT_NOISE_FREQHOPSCALE 10
154#define MXT_NOISE_FREQ0 11
155#define MXT_NOISE_FREQ1 12
156#define MXT_NOISE_FREQ2 13
157#define MXT_NOISE_FREQ3 14
158#define MXT_NOISE_FREQ4 15
159#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700160
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700161/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800162#define MXT_COMMS_CTRL 0
163#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700164
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700165/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800166#define MXT_CTE_CTRL 0
167#define MXT_CTE_CMD 1
168#define MXT_CTE_MODE 2
169#define MXT_CTE_IDLEGCAFDEPTH 3
170#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700171#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700172
Iiro Valkonen7686b102011-02-02 23:21:58 -0800173#define MXT_VOLTAGE_DEFAULT 2700000
174#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700175
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700176/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800177#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700178#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800179#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700180
181/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800182#define MXT_BACKUP_TIME 50 /* msec */
183#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700184#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700185#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700186#define MXT_FW_RESET_TIME 3000 /* msec */
187#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700188
189/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800190#define MXT_UNLOCK_CMD_MSB 0xaa
191#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700192
193/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800194#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
195#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
196#define MXT_FRAME_CRC_CHECK 0x02
197#define MXT_FRAME_CRC_FAIL 0x03
198#define MXT_FRAME_CRC_PASS 0x04
199#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
200#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700201
202/* Touch status */
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800203#define MXT_UNGRIP (1 << 0)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800204#define MXT_SUPPRESS (1 << 1)
205#define MXT_AMP (1 << 2)
206#define MXT_VECTOR (1 << 3)
207#define MXT_MOVE (1 << 4)
208#define MXT_RELEASE (1 << 5)
209#define MXT_PRESS (1 << 6)
210#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700211
Joonyoung Shim910d8052011-04-12 23:14:38 -0700212/* Touch orient bits */
213#define MXT_XY_SWITCH (1 << 0)
214#define MXT_X_INVERT (1 << 1)
215#define MXT_Y_INVERT (1 << 2)
216
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800220#define MXT_PIXELS_PER_MM 20
221
Iiro Valkonen7686b102011-02-02 23:21:58 -0800222struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700223 u8 family_id;
224 u8 variant_id;
225 u8 version;
226 u8 build;
227 u8 matrix_xsize;
228 u8 matrix_ysize;
229 u8 object_num;
230};
231
Iiro Valkonen7686b102011-02-02 23:21:58 -0800232struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233 u8 type;
234 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700235 u8 size_minus_one;
236 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700237 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800238} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700239
Iiro Valkonen7686b102011-02-02 23:21:58 -0800240struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700241 u8 reportid;
242 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700243};
244
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700245/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800246struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700247 struct i2c_client *client;
248 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800249 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800250 const struct mxt_platform_data *pdata;
251 struct mxt_object *object_table;
252 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700253 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700254 unsigned int max_x;
255 unsigned int max_y;
Benson Leungd79e7e42014-05-18 23:02:52 -0700256 bool in_bootloader;
Nick Dyerc3f78042014-05-18 23:04:46 -0700257 u32 config_crc;
Nick Dyerf28a8422014-05-18 23:10:49 -0700258 u8 bootloader_addr;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800259
260 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800261 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700262 u16 T6_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800263 u8 T9_reportid_min;
264 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800265 u8 T19_reportid;
Benson Leungd79e7e42014-05-18 23:02:52 -0700266
267 /* for fw update in bootloader */
268 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700269
270 /* for reset handling */
271 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700272
273 /* for config update handling */
274 struct completion crc_completion;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700275};
276
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700277static size_t mxt_obj_size(const struct mxt_object *obj)
278{
279 return obj->size_minus_one + 1;
280}
281
282static size_t mxt_obj_instances(const struct mxt_object *obj)
283{
284 return obj->instances_minus_one + 1;
285}
286
Iiro Valkonen7686b102011-02-02 23:21:58 -0800287static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700288{
289 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700290 case MXT_GEN_COMMAND_T6:
291 case MXT_GEN_POWER_T7:
292 case MXT_GEN_ACQUIRE_T8:
293 case MXT_GEN_DATASOURCE_T53:
294 case MXT_TOUCH_MULTI_T9:
295 case MXT_TOUCH_KEYARRAY_T15:
296 case MXT_TOUCH_PROXIMITY_T23:
297 case MXT_TOUCH_PROXKEY_T52:
298 case MXT_PROCI_GRIPFACE_T20:
299 case MXT_PROCG_NOISE_T22:
300 case MXT_PROCI_ONETOUCH_T24:
301 case MXT_PROCI_TWOTOUCH_T27:
302 case MXT_PROCI_GRIP_T40:
303 case MXT_PROCI_PALM_T41:
304 case MXT_PROCI_TOUCHSUPPRESSION_T42:
305 case MXT_PROCI_STYLUS_T47:
306 case MXT_PROCG_NOISESUPPRESSION_T48:
307 case MXT_SPT_COMMSCONFIG_T18:
308 case MXT_SPT_GPIOPWM_T19:
309 case MXT_SPT_SELFTEST_T25:
310 case MXT_SPT_CTECONFIG_T28:
311 case MXT_SPT_USERDATA_T38:
312 case MXT_SPT_DIGITIZER_T43:
313 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700314 return true;
315 default:
316 return false;
317 }
318}
319
Iiro Valkonen7686b102011-02-02 23:21:58 -0800320static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700321{
322 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700323 case MXT_GEN_COMMAND_T6:
324 case MXT_GEN_POWER_T7:
325 case MXT_GEN_ACQUIRE_T8:
326 case MXT_TOUCH_MULTI_T9:
327 case MXT_TOUCH_KEYARRAY_T15:
328 case MXT_TOUCH_PROXIMITY_T23:
329 case MXT_TOUCH_PROXKEY_T52:
330 case MXT_PROCI_GRIPFACE_T20:
331 case MXT_PROCG_NOISE_T22:
332 case MXT_PROCI_ONETOUCH_T24:
333 case MXT_PROCI_TWOTOUCH_T27:
334 case MXT_PROCI_GRIP_T40:
335 case MXT_PROCI_PALM_T41:
336 case MXT_PROCI_TOUCHSUPPRESSION_T42:
337 case MXT_PROCI_STYLUS_T47:
338 case MXT_PROCG_NOISESUPPRESSION_T48:
339 case MXT_SPT_COMMSCONFIG_T18:
340 case MXT_SPT_GPIOPWM_T19:
341 case MXT_SPT_SELFTEST_T25:
342 case MXT_SPT_CTECONFIG_T28:
343 case MXT_SPT_DIGITIZER_T43:
344 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700345 return true;
346 default:
347 return false;
348 }
349}
350
Iiro Valkonen7686b102011-02-02 23:21:58 -0800351static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700352 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700353{
Andy Shevchenkoeb007c82012-10-04 00:02:59 -0700354 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
355 message->reportid, 7, message->message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700356}
357
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700358static int mxt_wait_for_completion(struct mxt_data *data,
359 struct completion *comp,
360 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700361{
Benson Leungd79e7e42014-05-18 23:02:52 -0700362 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700363 unsigned long timeout = msecs_to_jiffies(timeout_ms);
364 long ret;
365
366 ret = wait_for_completion_interruptible_timeout(comp, timeout);
367 if (ret < 0) {
368 return ret;
369 } else if (ret == 0) {
370 dev_err(dev, "Wait for completion timed out.\n");
371 return -ETIMEDOUT;
372 }
373 return 0;
374}
375
Nick Dyerf28a8422014-05-18 23:10:49 -0700376static int mxt_bootloader_read(struct mxt_data *data,
377 u8 *val, unsigned int count)
378{
379 int ret;
380 struct i2c_msg msg;
381
382 msg.addr = data->bootloader_addr;
383 msg.flags = data->client->flags & I2C_M_TEN;
384 msg.flags |= I2C_M_RD;
385 msg.len = count;
386 msg.buf = val;
387
388 ret = i2c_transfer(data->client->adapter, &msg, 1);
389
390 if (ret == 1) {
391 ret = 0;
392 } else {
393 ret = ret < 0 ? ret : -EIO;
394 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
395 __func__, ret);
396 }
397
398 return ret;
399}
400
401static int mxt_bootloader_write(struct mxt_data *data,
402 const u8 * const val, unsigned int count)
403{
404 int ret;
405 struct i2c_msg msg;
406
407 msg.addr = data->bootloader_addr;
408 msg.flags = data->client->flags & I2C_M_TEN;
409 msg.len = count;
410 msg.buf = (u8 *)val;
411
412 ret = i2c_transfer(data->client->adapter, &msg, 1);
413 if (ret == 1) {
414 ret = 0;
415 } else {
416 ret = ret < 0 ? ret : -EIO;
417 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
418 __func__, ret);
419 }
420
421 return ret;
422}
423
424static int mxt_lookup_bootloader_address(struct mxt_data *data)
425{
426 u8 appmode = data->client->addr;
427 u8 bootloader;
428
429 switch (appmode) {
430 case 0x4a:
431 case 0x4b:
432 case 0x4c:
433 case 0x4d:
434 case 0x5a:
435 case 0x5b:
436 bootloader = appmode - 0x26;
437 break;
438 default:
439 dev_err(&data->client->dev,
440 "Appmode i2c address 0x%02x not found\n",
441 appmode);
442 return -EINVAL;
443 }
444
445 data->bootloader_addr = bootloader;
446 return 0;
447}
448
Benson Leungd79e7e42014-05-18 23:02:52 -0700449static int mxt_check_bootloader(struct mxt_data *data, unsigned int state)
450{
Nick Dyerf28a8422014-05-18 23:10:49 -0700451 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700452 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700453 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700454
455recheck:
Benson Leungd79e7e42014-05-18 23:02:52 -0700456 if (state != MXT_WAITING_BOOTLOAD_CMD) {
457 /*
458 * In application update mode, the interrupt
459 * line signals state transitions. We must wait for the
460 * CHG assertion before reading the status byte.
461 * Once the status byte has been read, the line is deasserted.
462 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700463 ret = mxt_wait_for_completion(data, &data->bl_completion,
464 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700465 if (ret) {
466 /*
467 * TODO: handle -ERESTARTSYS better by terminating
468 * fw update process before returning to userspace
469 * by writing length 0x000 to device (iff we are in
470 * WAITING_FRAME_DATA state).
471 */
Nick Dyerf28a8422014-05-18 23:10:49 -0700472 dev_err(dev, "Update wait error %d\n", ret);
Benson Leungd79e7e42014-05-18 23:02:52 -0700473 return ret;
474 }
475 }
476
Nick Dyerf28a8422014-05-18 23:10:49 -0700477 ret = mxt_bootloader_read(data, &val, 1);
478 if (ret)
479 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700480
481 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800482 case MXT_WAITING_BOOTLOAD_CMD:
483 case MXT_WAITING_FRAME_DATA:
484 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800486 case MXT_FRAME_CRC_PASS:
487 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700488 goto recheck;
489 break;
490 default:
491 return -EINVAL;
492 }
493
494 if (val != state) {
Nick Dyerf28a8422014-05-18 23:10:49 -0700495 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
Nick Dyer7bed6802014-05-18 23:04:09 -0700496 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700497 return -EINVAL;
498 }
499
500 return 0;
501}
502
Nick Dyerf28a8422014-05-18 23:10:49 -0700503static int mxt_unlock_bootloader(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700504{
Nick Dyerf28a8422014-05-18 23:10:49 -0700505 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700506 u8 buf[2];
507
Iiro Valkonen7686b102011-02-02 23:21:58 -0800508 buf[0] = MXT_UNLOCK_CMD_LSB;
509 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700510
Nick Dyerf28a8422014-05-18 23:10:49 -0700511 ret = mxt_bootloader_write(data, buf, 2);
512 if (ret)
513 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514
515 return 0;
516}
517
Iiro Valkonen7686b102011-02-02 23:21:58 -0800518static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700519 u16 reg, u16 len, void *val)
520{
521 struct i2c_msg xfer[2];
522 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800523 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700524
525 buf[0] = reg & 0xff;
526 buf[1] = (reg >> 8) & 0xff;
527
528 /* Write register */
529 xfer[0].addr = client->addr;
530 xfer[0].flags = 0;
531 xfer[0].len = 2;
532 xfer[0].buf = buf;
533
534 /* Read data */
535 xfer[1].addr = client->addr;
536 xfer[1].flags = I2C_M_RD;
537 xfer[1].len = len;
538 xfer[1].buf = val;
539
Daniel Kurtz771733e2012-06-28 21:08:11 +0800540 ret = i2c_transfer(client->adapter, xfer, 2);
541 if (ret == 2) {
542 ret = 0;
543 } else {
544 if (ret >= 0)
545 ret = -EIO;
546 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
547 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700548 }
549
Daniel Kurtz771733e2012-06-28 21:08:11 +0800550 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700551}
552
Iiro Valkonen7686b102011-02-02 23:21:58 -0800553static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800555 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700556}
557
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800558static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
559 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700560{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800561 u8 *buf;
562 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800563 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800565 count = len + 2;
566 buf = kmalloc(count, GFP_KERNEL);
567 if (!buf)
568 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700569
570 buf[0] = reg & 0xff;
571 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800572 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700573
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800574 ret = i2c_master_send(client, buf, count);
575 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800576 ret = 0;
577 } else {
578 if (ret >= 0)
579 ret = -EIO;
580 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
581 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 }
583
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800584 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800585 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586}
587
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800588static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800590 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591}
592
Iiro Valkonen7686b102011-02-02 23:21:58 -0800593static struct mxt_object *
594mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700595{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800596 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597 int i;
598
599 for (i = 0; i < data->info.object_num; i++) {
600 object = data->object_table + i;
601 if (object->type == type)
602 return object;
603 }
604
Nick Dyer7bed6802014-05-18 23:04:09 -0700605 dev_err(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 return NULL;
607}
608
Iiro Valkonen7686b102011-02-02 23:21:58 -0800609static int mxt_read_message(struct mxt_data *data,
610 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700611{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800612 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700613 u16 reg;
614
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700615 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616 if (!object)
617 return -EINVAL;
618
619 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800620 return __mxt_read_reg(data->client, reg,
621 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700622}
623
Iiro Valkonen7686b102011-02-02 23:21:58 -0800624static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625 u8 type, u8 offset, u8 val)
626{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800627 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700628 u16 reg;
629
Iiro Valkonen7686b102011-02-02 23:21:58 -0800630 object = mxt_get_object(data, type);
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700631 if (!object || offset >= mxt_obj_size(object))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700632 return -EINVAL;
633
634 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800635 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700636}
637
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800638static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
639{
640 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700641 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800642 bool button;
643 int i;
644
645 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700646 for (i = 0; i < pdata->t19_num_keys; i++) {
647 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800648 continue;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700649 button = !(message->message[0] & (1 << i));
650 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800651 }
652}
653
Iiro Valkonen7686b102011-02-02 23:21:58 -0800654static void mxt_input_touchevent(struct mxt_data *data,
655 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700657 struct device *dev = &data->client->dev;
658 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800659 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660 int x;
661 int y;
662 int area;
Yufeng Shen28ac2932011-08-16 00:40:54 -0700663 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664
Joonyoung Shim910d8052011-04-12 23:14:38 -0700665 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
666 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
667 if (data->max_x < 1024)
668 x = x >> 2;
669 if (data->max_y < 1024)
670 y = y >> 2;
671
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700672 area = message->message[4];
Yufeng Shen28ac2932011-08-16 00:40:54 -0700673 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800675 dev_dbg(dev,
676 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
677 id,
678 (status & MXT_DETECT) ? 'D' : '.',
679 (status & MXT_PRESS) ? 'P' : '.',
680 (status & MXT_RELEASE) ? 'R' : '.',
681 (status & MXT_MOVE) ? 'M' : '.',
682 (status & MXT_VECTOR) ? 'V' : '.',
683 (status & MXT_AMP) ? 'A' : '.',
684 (status & MXT_SUPPRESS) ? 'S' : '.',
685 (status & MXT_UNGRIP) ? 'U' : '.',
686 x, y, area, pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700687
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800688 input_mt_slot(input_dev, id);
689 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
690 status & MXT_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800692 if (status & MXT_DETECT) {
693 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
694 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
695 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
696 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
697 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698}
699
Nick Dyerc3f78042014-05-18 23:04:46 -0700700static u16 mxt_extract_T6_csum(const u8 *csum)
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800701{
702 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
703}
704
Daniel Kurtz04a79182012-06-28 21:08:21 +0800705static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
706{
707 u8 id = msg->reportid;
708 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700709}
710
Benson Leungd79e7e42014-05-18 23:02:52 -0700711static irqreturn_t mxt_process_messages_until_invalid(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700712{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800713 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800714 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700715 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800717 bool update_input = false;
Nick Dyerc3f78042014-05-18 23:04:46 -0700718 u32 crc;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719
720 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800721 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700722 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700723 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700724 }
725
726 reportid = message.reportid;
727
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800728 if (reportid == data->T6_reportid) {
729 u8 status = payload[0];
Nick Dyerc3f78042014-05-18 23:04:46 -0700730
731 crc = mxt_extract_T6_csum(&payload[1]);
732 if (crc != data->config_crc) {
733 data->config_crc = crc;
734 complete(&data->crc_completion);
735 }
736
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800737 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
Nick Dyerc3f78042014-05-18 23:04:46 -0700738 status, data->config_crc);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700739
740 if (status & MXT_T6_STATUS_RESET)
741 complete(&data->reset_completion);
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800742 } else if (mxt_is_T9_message(data, &message)) {
743 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800744 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800745 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800746 } else if (message.reportid == data->T19_reportid) {
747 mxt_input_button(data, &message);
748 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800749 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800750 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800751 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752 } while (reportid != 0xff);
753
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800754 if (update_input) {
755 input_mt_report_pointer_emulation(data->input_dev, false);
756 input_sync(data->input_dev);
757 }
758
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700759 return IRQ_HANDLED;
760}
761
Benson Leungd79e7e42014-05-18 23:02:52 -0700762static irqreturn_t mxt_interrupt(int irq, void *dev_id)
763{
764 struct mxt_data *data = dev_id;
765
766 if (data->in_bootloader) {
767 /* bootloader state transition completion */
768 complete(&data->bl_completion);
769 return IRQ_HANDLED;
770 }
771
772 return mxt_process_messages_until_invalid(data);
773}
774
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700775static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
776 u8 value, bool wait)
777{
778 u16 reg;
779 u8 command_register;
780 int timeout_counter = 0;
781 int ret;
782
783 reg = data->T6_address + cmd_offset;
784
785 ret = mxt_write_reg(data->client, reg, value);
786 if (ret)
787 return ret;
788
789 if (!wait)
790 return 0;
791
792 do {
793 msleep(20);
794 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
795 if (ret)
796 return ret;
797 } while (command_register != 0 && timeout_counter++ <= 100);
798
799 if (timeout_counter > 100) {
800 dev_err(&data->client->dev, "Command failed!\n");
801 return -EIO;
802 }
803
804 return 0;
805}
806
807static int mxt_soft_reset(struct mxt_data *data)
808{
809 struct device *dev = &data->client->dev;
810 int ret = 0;
811
812 dev_info(dev, "Resetting chip\n");
813
814 reinit_completion(&data->reset_completion);
815
816 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
817 if (ret)
818 return ret;
819
820 ret = mxt_wait_for_completion(data, &data->reset_completion,
821 MXT_RESET_TIMEOUT);
822 if (ret)
823 return ret;
824
825 return 0;
826}
827
Nick Dyerc3f78042014-05-18 23:04:46 -0700828static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
829{
830 /*
831 * On failure, CRC is set to 0 and config will always be
832 * downloaded.
833 */
834 data->config_crc = 0;
835 reinit_completion(&data->crc_completion);
836
837 mxt_t6_command(data, cmd, value, true);
838
839 /*
840 * Wait for crc message. On failure, CRC is set to 0 and config will
841 * always be downloaded.
842 */
843 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
844}
845
Iiro Valkonen7686b102011-02-02 23:21:58 -0800846static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700847{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800848 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800849 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700850 struct device *dev = &data->client->dev;
851 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800852 int i, size;
853 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700854
Iiro Valkonen71749f52011-02-15 13:36:52 -0800855 if (!pdata->config) {
856 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
857 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700858 }
859
Nick Dyerc3f78042014-05-18 23:04:46 -0700860 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
861
862 if (data->config_crc == pdata->config_crc) {
863 dev_info(dev, "Config CRC 0x%06X: OK\n", data->config_crc);
864 return 0;
865 }
866
867 dev_info(dev, "Config CRC 0x%06X: does not match 0x%06X\n",
868 data->config_crc, pdata->config_crc);
869
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700870 for (i = 0; i < data->info.object_num; i++) {
871 object = data->object_table + i;
872
Iiro Valkonen7686b102011-02-02 23:21:58 -0800873 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700874 continue;
875
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700876 size = mxt_obj_size(object) * mxt_obj_instances(object);
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800877 if (index + size > pdata->config_length) {
878 dev_err(dev, "Not enough config data!\n");
879 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800880 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800881
882 ret = __mxt_write_reg(data->client, object->start_address,
883 size, &pdata->config[index]);
884 if (ret)
885 return ret;
886 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700887 }
888
Nick Dyerc3f78042014-05-18 23:04:46 -0700889 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
890
891 ret = mxt_soft_reset(data);
892 if (ret)
893 return ret;
894
895 dev_info(dev, "Config successfully updated\n");
896
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700897 return 0;
898}
899
Iiro Valkonen7686b102011-02-02 23:21:58 -0800900static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700901{
902 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800903 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700904 int count = 10;
905 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700906
907 /* Read dummy message to make high CHG pin */
908 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800909 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700910 if (error)
911 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800912 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700913
914 if (!count) {
915 dev_err(dev, "CHG pin isn't cleared\n");
916 return -EBUSY;
917 }
918
919 return 0;
920}
921
Iiro Valkonen7686b102011-02-02 23:21:58 -0800922static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700923{
924 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800925 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700926 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700927
Daniel Kurtz23003a842012-06-28 21:08:14 +0800928 /* Read 7-byte info block starting at address 0 */
929 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700930 if (error)
931 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700932
933 return 0;
934}
935
Iiro Valkonen7686b102011-02-02 23:21:58 -0800936static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700937{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800938 struct i2c_client *client = data->client;
939 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700940 int error;
941 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800942 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700943
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800944 table_size = data->info.object_num * sizeof(struct mxt_object);
945 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
946 data->object_table);
947 if (error)
948 return error;
949
950 /* Valid Report IDs start counting from 1 */
951 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700952 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800953 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800954 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700955
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800956 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700957
958 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800959 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700960 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700961 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800962 max_id = reportid - 1;
963 } else {
964 min_id = 0;
965 max_id = 0;
966 }
967
968 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -0700969 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700970 object->type, object->start_address,
971 mxt_obj_size(object), mxt_obj_instances(object),
972 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800973
974 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800975 case MXT_GEN_COMMAND_T6:
976 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700977 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800978 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800979 case MXT_TOUCH_MULTI_T9:
980 data->T9_reportid_min = min_id;
981 data->T9_reportid_max = max_id;
982 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800983 case MXT_SPT_GPIOPWM_T19:
984 data->T19_reportid = min_id;
985 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700986 }
987 }
988
989 return 0;
990}
991
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800992static void mxt_free_object_table(struct mxt_data *data)
993{
994 kfree(data->object_table);
995 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800996 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800997 data->T9_reportid_min = 0;
998 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800999 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001000}
1001
Iiro Valkonen7686b102011-02-02 23:21:58 -08001002static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001003{
1004 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001005 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001006 int error;
1007 u8 val;
1008
Iiro Valkonen7686b102011-02-02 23:21:58 -08001009 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010 if (error)
1011 return error;
1012
1013 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001014 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001015 GFP_KERNEL);
1016 if (!data->object_table) {
1017 dev_err(&client->dev, "Failed to allocate memory\n");
1018 return -ENOMEM;
1019 }
1020
1021 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001022 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001023 if (error) {
1024 dev_err(&client->dev, "Error %d reading object table\n", error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001025 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -07001026 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001027
1028 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001029 error = mxt_check_reg_init(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001030 if (error) {
1031 dev_err(&client->dev, "Error %d initializing configuration\n",
1032 error);
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001033 goto err_free_object_table;
Nick Dyer7bed6802014-05-18 23:04:09 -07001034 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001036 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001037 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001038 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001039 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040 info->matrix_xsize = val;
1041
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001044 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001045 info->matrix_ysize = val;
1046
1047 dev_info(&client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001048 "Family: %u Variant: %u Firmware V%u.%u.%02X\n",
Daniel Kurtze0e02692012-06-28 21:08:15 +08001049 info->family_id, info->variant_id, info->version >> 4,
1050 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001051
1052 dev_info(&client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001053 "Matrix X Size: %u Matrix Y Size: %u Objects: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054 info->matrix_xsize, info->matrix_ysize,
1055 info->object_num);
1056
1057 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001058
1059err_free_object_table:
1060 mxt_free_object_table(data);
1061 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001062}
1063
Joonyoung Shim910d8052011-04-12 23:14:38 -07001064static void mxt_calc_resolution(struct mxt_data *data)
1065{
1066 unsigned int max_x = data->pdata->x_size - 1;
1067 unsigned int max_y = data->pdata->y_size - 1;
1068
1069 if (data->pdata->orient & MXT_XY_SWITCH) {
1070 data->max_x = max_y;
1071 data->max_y = max_x;
1072 } else {
1073 data->max_x = max_x;
1074 data->max_y = max_y;
1075 }
1076}
1077
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001078/* Firmware Version is returned as Major.Minor.Build */
1079static ssize_t mxt_fw_version_show(struct device *dev,
1080 struct device_attribute *attr, char *buf)
1081{
1082 struct mxt_data *data = dev_get_drvdata(dev);
1083 struct mxt_info *info = &data->info;
1084 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
1085 info->version >> 4, info->version & 0xf, info->build);
1086}
1087
1088/* Hardware Version is returned as FamilyID.VariantID */
1089static ssize_t mxt_hw_version_show(struct device *dev,
1090 struct device_attribute *attr, char *buf)
1091{
1092 struct mxt_data *data = dev_get_drvdata(dev);
1093 struct mxt_info *info = &data->info;
1094 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
1095 info->family_id, info->variant_id);
1096}
1097
Daniel Kurtz794eb672012-06-28 21:08:10 +08001098static ssize_t mxt_show_instance(char *buf, int count,
1099 struct mxt_object *object, int instance,
1100 const u8 *val)
1101{
1102 int i;
1103
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001104 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001105 count += scnprintf(buf + count, PAGE_SIZE - count,
1106 "Instance %u\n", instance);
1107
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001108 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001109 count += scnprintf(buf + count, PAGE_SIZE - count,
1110 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
1111 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
1112
1113 return count;
1114}
1115
Iiro Valkonen7686b102011-02-02 23:21:58 -08001116static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001117 struct device_attribute *attr, char *buf)
1118{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001119 struct mxt_data *data = dev_get_drvdata(dev);
1120 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001121 int count = 0;
1122 int i, j;
1123 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001124 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001125
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001126 /* Pre-allocate buffer large enough to hold max sized object. */
1127 obuf = kmalloc(256, GFP_KERNEL);
1128 if (!obuf)
1129 return -ENOMEM;
1130
1131 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132 for (i = 0; i < data->info.object_num; i++) {
1133 object = data->object_table + i;
1134
Daniel Kurtz91630952012-06-28 21:08:09 +08001135 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001136 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001137
Daniel Kurtz91630952012-06-28 21:08:09 +08001138 count += scnprintf(buf + count, PAGE_SIZE - count,
1139 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001141 for (j = 0; j < mxt_obj_instances(object); j++) {
1142 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08001143 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001144
Daniel Kurtz794eb672012-06-28 21:08:10 +08001145 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001146 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001147 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001148
Daniel Kurtz794eb672012-06-28 21:08:10 +08001149 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001150 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001151 }
1152
Daniel Kurtz794eb672012-06-28 21:08:10 +08001153done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001154 kfree(obuf);
1155 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001156}
1157
Iiro Valkonen7686b102011-02-02 23:21:58 -08001158static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001159{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001160 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001161 const struct firmware *fw = NULL;
1162 unsigned int frame_size;
1163 unsigned int pos = 0;
1164 int ret;
1165
1166 ret = request_firmware(&fw, fn, dev);
1167 if (ret) {
1168 dev_err(dev, "Unable to open firmware %s\n", fn);
1169 return ret;
1170 }
1171
Nick Dyerf28a8422014-05-18 23:10:49 -07001172 ret = mxt_lookup_bootloader_address(data);
1173 if (ret)
1174 goto release_firmware;
1175
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001176 /* Change to the bootloader mode */
Benson Leungd79e7e42014-05-18 23:02:52 -07001177 data->in_bootloader = true;
1178
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001179 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_BOOT_VALUE, false);
1180 if (ret)
1181 goto release_firmware;
1182
Iiro Valkonen7686b102011-02-02 23:21:58 -08001183 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001184
Benson Leungd79e7e42014-05-18 23:02:52 -07001185 reinit_completion(&data->bl_completion);
1186
1187 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001188 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001189 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001190
1191 /* Unlock bootloader */
Nick Dyerf28a8422014-05-18 23:10:49 -07001192 mxt_unlock_bootloader(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001193
1194 while (pos < fw->size) {
Benson Leungd79e7e42014-05-18 23:02:52 -07001195 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001196 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001197 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001198
1199 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1200
1201 /* We should add 2 at frame size as the the firmware data is not
1202 * included the CRC bytes.
1203 */
1204 frame_size += 2;
1205
1206 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07001207 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
1208 if (ret)
1209 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001210
Benson Leungd79e7e42014-05-18 23:02:52 -07001211 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001212 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001213 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001214
1215 pos += frame_size;
1216
1217 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1218 }
1219
Benson Leunga0434b72014-05-18 23:03:09 -07001220 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001221 ret = mxt_wait_for_completion(data, &data->bl_completion,
1222 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001223 if (ret)
1224 goto disable_irq;
1225
1226 /*
1227 * Wait for device to reset. Some bootloader versions do not assert
1228 * the CHG line after bootloading has finished, so ignore potential
1229 * errors.
1230 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001231 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001232
Benson Leungd79e7e42014-05-18 23:02:52 -07001233 data->in_bootloader = false;
1234
1235disable_irq:
1236 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001237release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001238 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001239 return ret;
1240}
1241
Iiro Valkonen7686b102011-02-02 23:21:58 -08001242static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001243 struct device_attribute *attr,
1244 const char *buf, size_t count)
1245{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001246 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001247 int error;
1248
Iiro Valkonen7686b102011-02-02 23:21:58 -08001249 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001250 if (error) {
1251 dev_err(dev, "The firmware update failed(%d)\n", error);
1252 count = error;
1253 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07001254 dev_info(dev, "The firmware update succeeded\n");
1255
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001256 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001257
Iiro Valkonen7686b102011-02-02 23:21:58 -08001258 mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07001259
1260 enable_irq(data->irq);
1261
1262 error = mxt_make_highchg(data);
1263 if (error)
1264 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265 }
1266
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001267 return count;
1268}
1269
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001270static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1271static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001272static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1273static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001274
Iiro Valkonen7686b102011-02-02 23:21:58 -08001275static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001276 &dev_attr_fw_version.attr,
1277 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001278 &dev_attr_object.attr,
1279 &dev_attr_update_fw.attr,
1280 NULL
1281};
1282
Iiro Valkonen7686b102011-02-02 23:21:58 -08001283static const struct attribute_group mxt_attr_group = {
1284 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001285};
1286
Iiro Valkonen7686b102011-02-02 23:21:58 -08001287static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001288{
1289 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001290 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001291 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001292}
1293
Iiro Valkonen7686b102011-02-02 23:21:58 -08001294static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001295{
1296 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001297 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001298 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001299}
1300
Iiro Valkonen7686b102011-02-02 23:21:58 -08001301static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001302{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001303 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001304
Iiro Valkonen7686b102011-02-02 23:21:58 -08001305 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001306
1307 return 0;
1308}
1309
Iiro Valkonen7686b102011-02-02 23:21:58 -08001310static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001311{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001312 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001313
Iiro Valkonen7686b102011-02-02 23:21:58 -08001314 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001315}
1316
Bill Pemberton5298cc42012-11-23 21:38:25 -08001317static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001318 const struct i2c_device_id *id)
1319{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001320 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001321 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001322 struct input_dev *input_dev;
1323 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001324 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001325 unsigned int mt_flags = 0;
1326 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001327
Iiro Valkonen919ed892011-02-15 13:36:52 -08001328 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001329 return -EINVAL;
1330
Iiro Valkonen7686b102011-02-02 23:21:58 -08001331 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001332 input_dev = input_allocate_device();
1333 if (!data || !input_dev) {
1334 dev_err(&client->dev, "Failed to allocate memory\n");
1335 error = -ENOMEM;
1336 goto err_free_mem;
1337 }
1338
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001339 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001340 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1341 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001342
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001343 input_dev->phys = data->phys;
1344
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001345 input_dev->id.bustype = BUS_I2C;
1346 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001347 input_dev->open = mxt_input_open;
1348 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001349
Joonyoung Shim910d8052011-04-12 23:14:38 -07001350 data->client = client;
1351 data->input_dev = input_dev;
1352 data->pdata = pdata;
1353 data->irq = client->irq;
1354
Benson Leungd79e7e42014-05-18 23:02:52 -07001355 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001356 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07001357 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07001358
Joonyoung Shim910d8052011-04-12 23:14:38 -07001359 mxt_calc_resolution(data);
1360
Daniel Kurtzcb159112012-06-28 21:08:22 +08001361 error = mxt_initialize(data);
1362 if (error)
1363 goto err_free_mem;
1364
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001365 __set_bit(EV_ABS, input_dev->evbit);
1366 __set_bit(EV_KEY, input_dev->evbit);
1367 __set_bit(BTN_TOUCH, input_dev->keybit);
1368
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001369 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001370 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1371
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001372 for (i = 0; i < pdata->t19_num_keys; i++)
1373 if (pdata->t19_keymap[i] != KEY_RESERVED)
1374 input_set_capability(input_dev, EV_KEY,
1375 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001376
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001377 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001378
1379 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1380 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1381 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1382 MXT_PIXELS_PER_MM);
1383 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1384 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001385
1386 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001387 }
1388
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389 /* For single touch */
1390 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001391 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001392 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001393 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001394 input_set_abs_params(input_dev, ABS_PRESSURE,
1395 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001396
1397 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001398 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001399 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001400 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001401 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001402 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001403 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001404 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001405 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001406 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001407 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001408 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1409 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001410
1411 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001412 i2c_set_clientdata(client, data);
1413
Iiro Valkonen7686b102011-02-02 23:21:58 -08001414 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001415 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001416 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001417 if (error) {
1418 dev_err(&client->dev, "Failed to register interrupt\n");
1419 goto err_free_object;
1420 }
1421
Iiro Valkonen08960a02011-04-12 23:16:40 -07001422 error = mxt_make_highchg(data);
1423 if (error)
1424 goto err_free_irq;
1425
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001426 error = input_register_device(input_dev);
Nick Dyer7bed6802014-05-18 23:04:09 -07001427 if (error) {
1428 dev_err(&client->dev, "Error %d registering input device\n",
1429 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001430 goto err_free_irq;
Nick Dyer7bed6802014-05-18 23:04:09 -07001431 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001432
Iiro Valkonen7686b102011-02-02 23:21:58 -08001433 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07001434 if (error) {
1435 dev_err(&client->dev, "Failure %d creating sysfs group\n",
1436 error);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001437 goto err_unregister_device;
Nick Dyer7bed6802014-05-18 23:04:09 -07001438 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001439
1440 return 0;
1441
1442err_unregister_device:
1443 input_unregister_device(input_dev);
1444 input_dev = NULL;
1445err_free_irq:
1446 free_irq(client->irq, data);
1447err_free_object:
1448 kfree(data->object_table);
1449err_free_mem:
1450 input_free_device(input_dev);
1451 kfree(data);
1452 return error;
1453}
1454
Bill Pembertone2619cf2012-11-23 21:50:47 -08001455static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001456{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001457 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001458
Iiro Valkonen7686b102011-02-02 23:21:58 -08001459 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001460 free_irq(data->irq, data);
1461 input_unregister_device(data->input_dev);
1462 kfree(data->object_table);
1463 kfree(data);
1464
1465 return 0;
1466}
1467
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001468#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001469static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001470{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001471 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001472 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001473 struct input_dev *input_dev = data->input_dev;
1474
1475 mutex_lock(&input_dev->mutex);
1476
1477 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001478 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001479
1480 mutex_unlock(&input_dev->mutex);
1481
1482 return 0;
1483}
1484
Iiro Valkonen7686b102011-02-02 23:21:58 -08001485static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001486{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001487 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001488 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001489 struct input_dev *input_dev = data->input_dev;
1490
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001491 mxt_soft_reset(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001492
1493 mutex_lock(&input_dev->mutex);
1494
1495 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001496 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001497
1498 mutex_unlock(&input_dev->mutex);
1499
1500 return 0;
1501}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001502#endif
1503
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001504static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1505
Iiro Valkonen7686b102011-02-02 23:21:58 -08001506static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001507 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001508 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001509 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001510 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001511 { }
1512};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001513MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001514
Iiro Valkonen7686b102011-02-02 23:21:58 -08001515static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001516 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001517 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001518 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001519 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001520 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001521 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001522 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001523 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001524};
1525
Axel Lin1b92c1c2012-03-16 23:05:41 -07001526module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001527
1528/* Module information */
1529MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001530MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001531MODULE_LICENSE("GPL");