blob: d2feb9c771a406e29d7c43b69f817cef9b7ae757 [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>
28
29/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080030#define MXT_VER_20 20
31#define MXT_VER_21 21
32#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070033
Nick Dyer50a77c62014-07-23 12:38:48 -070034/* Firmware files */
Iiro Valkonen7686b102011-02-02 23:21:58 -080035#define MXT_FW_NAME "maxtouch.fw"
Nick Dyer50a77c62014-07-23 12:38:48 -070036#define MXT_CFG_NAME "maxtouch.cfg"
37#define MXT_CFG_MAGIC "OBP_RAW V1"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070038
39/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080040#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080041#define MXT_FAMILY_ID 0x00
42#define MXT_VARIANT_ID 0x01
43#define MXT_VERSION 0x02
44#define MXT_BUILD 0x03
45#define MXT_MATRIX_X_SIZE 0x04
46#define MXT_MATRIX_Y_SIZE 0x05
47#define MXT_OBJECT_NUM 0x06
48#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
Iiro Valkonen7686b102011-02-02 23:21:58 -080050#define MXT_OBJECT_SIZE 6
Nick Dyer4ce6fa02014-07-23 12:40:09 -070051#define MXT_INFO_CHECKSUM_SIZE 3
52#define MXT_MAX_BLOCK_WRITE 256
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070053
54/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070055#define MXT_DEBUG_DIAGNOSTIC_T37 37
56#define MXT_GEN_MESSAGE_T5 5
57#define MXT_GEN_COMMAND_T6 6
58#define MXT_GEN_POWER_T7 7
59#define MXT_GEN_ACQUIRE_T8 8
60#define MXT_GEN_DATASOURCE_T53 53
61#define MXT_TOUCH_MULTI_T9 9
62#define MXT_TOUCH_KEYARRAY_T15 15
63#define MXT_TOUCH_PROXIMITY_T23 23
64#define MXT_TOUCH_PROXKEY_T52 52
65#define MXT_PROCI_GRIPFACE_T20 20
66#define MXT_PROCG_NOISE_T22 22
67#define MXT_PROCI_ONETOUCH_T24 24
68#define MXT_PROCI_TWOTOUCH_T27 27
69#define MXT_PROCI_GRIP_T40 40
70#define MXT_PROCI_PALM_T41 41
71#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
72#define MXT_PROCI_STYLUS_T47 47
73#define MXT_PROCG_NOISESUPPRESSION_T48 48
74#define MXT_SPT_COMMSCONFIG_T18 18
75#define MXT_SPT_GPIOPWM_T19 19
76#define MXT_SPT_SELFTEST_T25 25
77#define MXT_SPT_CTECONFIG_T28 28
78#define MXT_SPT_USERDATA_T38 38
79#define MXT_SPT_DIGITIZER_T43 43
80#define MXT_SPT_MESSAGECOUNT_T44 44
81#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070082
Iiro Valkonen81c88a72011-07-04 03:08:25 -070083/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080084#define MXT_COMMAND_RESET 0
85#define MXT_COMMAND_BACKUPNV 1
86#define MXT_COMMAND_CALIBRATE 2
87#define MXT_COMMAND_REPORTALL 3
88#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070089
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070090/* Define for T6 status byte */
91#define MXT_T6_STATUS_RESET (1 << 7)
92
Iiro Valkonen81c88a72011-07-04 03:08:25 -070093/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080094#define MXT_POWER_IDLEACQINT 0
95#define MXT_POWER_ACTVACQINT 1
96#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070097
Iiro Valkonen81c88a72011-07-04 03:08:25 -070098/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080099#define MXT_ACQUIRE_CHRGTIME 0
100#define MXT_ACQUIRE_TCHDRIFT 2
101#define MXT_ACQUIRE_DRIFTST 3
102#define MXT_ACQUIRE_TCHAUTOCAL 4
103#define MXT_ACQUIRE_SYNC 5
104#define MXT_ACQUIRE_ATCHCALST 6
105#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700106
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700107/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800108#define MXT_TOUCH_CTRL 0
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700109#define MXT_T9_ORIENT 9
110#define MXT_T9_RANGE 18
111
Nick Dyerf3889ed2014-05-18 23:22:04 -0700112/* MXT_TOUCH_MULTI_T9 status */
113#define MXT_T9_UNGRIP (1 << 0)
114#define MXT_T9_SUPPRESS (1 << 1)
115#define MXT_T9_AMP (1 << 2)
116#define MXT_T9_VECTOR (1 << 3)
117#define MXT_T9_MOVE (1 << 4)
118#define MXT_T9_RELEASE (1 << 5)
119#define MXT_T9_PRESS (1 << 6)
120#define MXT_T9_DETECT (1 << 7)
121
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700122struct t9_range {
123 u16 x;
124 u16 y;
125} __packed;
126
Nick Dyerf3889ed2014-05-18 23:22:04 -0700127/* MXT_TOUCH_MULTI_T9 orient */
128#define MXT_T9_ORIENT_SWITCH (1 << 0)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700129
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700130/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800131#define MXT_GRIPFACE_CTRL 0
132#define MXT_GRIPFACE_XLOGRIP 1
133#define MXT_GRIPFACE_XHIGRIP 2
134#define MXT_GRIPFACE_YLOGRIP 3
135#define MXT_GRIPFACE_YHIGRIP 4
136#define MXT_GRIPFACE_MAXTCHS 5
137#define MXT_GRIPFACE_SZTHR1 7
138#define MXT_GRIPFACE_SZTHR2 8
139#define MXT_GRIPFACE_SHPTHR1 9
140#define MXT_GRIPFACE_SHPTHR2 10
141#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700142
Iiro Valkonen7686b102011-02-02 23:21:58 -0800143/* MXT_PROCI_NOISE field */
144#define MXT_NOISE_CTRL 0
145#define MXT_NOISE_OUTFLEN 1
146#define MXT_NOISE_GCAFUL_LSB 3
147#define MXT_NOISE_GCAFUL_MSB 4
148#define MXT_NOISE_GCAFLL_LSB 5
149#define MXT_NOISE_GCAFLL_MSB 6
150#define MXT_NOISE_ACTVGCAFVALID 7
151#define MXT_NOISE_NOISETHR 8
152#define MXT_NOISE_FREQHOPSCALE 10
153#define MXT_NOISE_FREQ0 11
154#define MXT_NOISE_FREQ1 12
155#define MXT_NOISE_FREQ2 13
156#define MXT_NOISE_FREQ3 14
157#define MXT_NOISE_FREQ4 15
158#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700159
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700160/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800161#define MXT_COMMS_CTRL 0
162#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700163
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700164/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800165#define MXT_CTE_CTRL 0
166#define MXT_CTE_CMD 1
167#define MXT_CTE_MODE 2
168#define MXT_CTE_IDLEGCAFDEPTH 3
169#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700170#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700171
Iiro Valkonen7686b102011-02-02 23:21:58 -0800172#define MXT_VOLTAGE_DEFAULT 2700000
173#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700174
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700175/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800176#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700177#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800178#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700179
180/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800181#define MXT_BACKUP_TIME 50 /* msec */
182#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700183#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700184#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700185#define MXT_FW_RESET_TIME 3000 /* msec */
186#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700187
188/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800189#define MXT_UNLOCK_CMD_MSB 0xaa
190#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700191
192/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800193#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
194#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
195#define MXT_FRAME_CRC_CHECK 0x02
196#define MXT_FRAME_CRC_FAIL 0x03
197#define MXT_FRAME_CRC_PASS 0x04
198#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
199#define MXT_BOOT_STATUS_MASK 0x3f
Nick Dyere57a66a2014-05-18 23:13:40 -0700200#define MXT_BOOT_EXTENDED_ID (1 << 5)
201#define MXT_BOOT_ID_MASK 0x1f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700202
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700203/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800204#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700205
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800206#define MXT_PIXELS_PER_MM 20
207
Iiro Valkonen7686b102011-02-02 23:21:58 -0800208struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700209 u8 family_id;
210 u8 variant_id;
211 u8 version;
212 u8 build;
213 u8 matrix_xsize;
214 u8 matrix_ysize;
215 u8 object_num;
216};
217
Iiro Valkonen7686b102011-02-02 23:21:58 -0800218struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219 u8 type;
220 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700221 u8 size_minus_one;
222 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700223 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800224} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700225
Iiro Valkonen7686b102011-02-02 23:21:58 -0800226struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700227 u8 reportid;
228 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700229};
230
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800232struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233 struct i2c_client *client;
234 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800235 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800236 const struct mxt_platform_data *pdata;
237 struct mxt_object *object_table;
238 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700239 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700240 unsigned int max_x;
241 unsigned int max_y;
Benson Leungd79e7e42014-05-18 23:02:52 -0700242 bool in_bootloader;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700243 u16 mem_size;
Nick Dyerc3f78042014-05-18 23:04:46 -0700244 u32 config_crc;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700245 u32 info_crc;
Nick Dyerf28a8422014-05-18 23:10:49 -0700246 u8 bootloader_addr;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800247
248 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800249 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700250 u16 T6_address;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700251 u16 T7_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800252 u8 T9_reportid_min;
253 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800254 u8 T19_reportid;
Benson Leungd79e7e42014-05-18 23:02:52 -0700255
256 /* for fw update in bootloader */
257 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700258
259 /* for reset handling */
260 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700261
262 /* for config update handling */
263 struct completion crc_completion;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700264};
265
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700266static size_t mxt_obj_size(const struct mxt_object *obj)
267{
268 return obj->size_minus_one + 1;
269}
270
271static size_t mxt_obj_instances(const struct mxt_object *obj)
272{
273 return obj->instances_minus_one + 1;
274}
275
Iiro Valkonen7686b102011-02-02 23:21:58 -0800276static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700277{
278 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700279 case MXT_GEN_COMMAND_T6:
280 case MXT_GEN_POWER_T7:
281 case MXT_GEN_ACQUIRE_T8:
282 case MXT_GEN_DATASOURCE_T53:
283 case MXT_TOUCH_MULTI_T9:
284 case MXT_TOUCH_KEYARRAY_T15:
285 case MXT_TOUCH_PROXIMITY_T23:
286 case MXT_TOUCH_PROXKEY_T52:
287 case MXT_PROCI_GRIPFACE_T20:
288 case MXT_PROCG_NOISE_T22:
289 case MXT_PROCI_ONETOUCH_T24:
290 case MXT_PROCI_TWOTOUCH_T27:
291 case MXT_PROCI_GRIP_T40:
292 case MXT_PROCI_PALM_T41:
293 case MXT_PROCI_TOUCHSUPPRESSION_T42:
294 case MXT_PROCI_STYLUS_T47:
295 case MXT_PROCG_NOISESUPPRESSION_T48:
296 case MXT_SPT_COMMSCONFIG_T18:
297 case MXT_SPT_GPIOPWM_T19:
298 case MXT_SPT_SELFTEST_T25:
299 case MXT_SPT_CTECONFIG_T28:
300 case MXT_SPT_USERDATA_T38:
301 case MXT_SPT_DIGITIZER_T43:
302 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700303 return true;
304 default:
305 return false;
306 }
307}
308
Iiro Valkonen7686b102011-02-02 23:21:58 -0800309static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700310 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700311{
Andy Shevchenkoeb007c82012-10-04 00:02:59 -0700312 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
313 message->reportid, 7, message->message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700314}
315
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700316static int mxt_wait_for_completion(struct mxt_data *data,
317 struct completion *comp,
318 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700319{
Benson Leungd79e7e42014-05-18 23:02:52 -0700320 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700321 unsigned long timeout = msecs_to_jiffies(timeout_ms);
322 long ret;
323
324 ret = wait_for_completion_interruptible_timeout(comp, timeout);
325 if (ret < 0) {
326 return ret;
327 } else if (ret == 0) {
328 dev_err(dev, "Wait for completion timed out.\n");
329 return -ETIMEDOUT;
330 }
331 return 0;
332}
333
Nick Dyerf28a8422014-05-18 23:10:49 -0700334static int mxt_bootloader_read(struct mxt_data *data,
335 u8 *val, unsigned int count)
336{
337 int ret;
338 struct i2c_msg msg;
339
340 msg.addr = data->bootloader_addr;
341 msg.flags = data->client->flags & I2C_M_TEN;
342 msg.flags |= I2C_M_RD;
343 msg.len = count;
344 msg.buf = val;
345
346 ret = i2c_transfer(data->client->adapter, &msg, 1);
347
348 if (ret == 1) {
349 ret = 0;
350 } else {
351 ret = ret < 0 ? ret : -EIO;
352 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
353 __func__, ret);
354 }
355
356 return ret;
357}
358
359static int mxt_bootloader_write(struct mxt_data *data,
360 const u8 * const val, unsigned int count)
361{
362 int ret;
363 struct i2c_msg msg;
364
365 msg.addr = data->bootloader_addr;
366 msg.flags = data->client->flags & I2C_M_TEN;
367 msg.len = count;
368 msg.buf = (u8 *)val;
369
370 ret = i2c_transfer(data->client->adapter, &msg, 1);
371 if (ret == 1) {
372 ret = 0;
373 } else {
374 ret = ret < 0 ? ret : -EIO;
375 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
376 __func__, ret);
377 }
378
379 return ret;
380}
381
382static int mxt_lookup_bootloader_address(struct mxt_data *data)
383{
384 u8 appmode = data->client->addr;
385 u8 bootloader;
386
387 switch (appmode) {
388 case 0x4a:
389 case 0x4b:
390 case 0x4c:
391 case 0x4d:
392 case 0x5a:
393 case 0x5b:
394 bootloader = appmode - 0x26;
395 break;
396 default:
397 dev_err(&data->client->dev,
398 "Appmode i2c address 0x%02x not found\n",
399 appmode);
400 return -EINVAL;
401 }
402
403 data->bootloader_addr = bootloader;
404 return 0;
405}
406
Nick Dyere57a66a2014-05-18 23:13:40 -0700407static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
408{
409 struct device *dev = &data->client->dev;
410 u8 buf[3];
411
412 if (val & MXT_BOOT_EXTENDED_ID) {
413 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
414 dev_err(dev, "%s: i2c failure\n", __func__);
Nick Dyer68807a02014-06-07 23:17:26 -0700415 return val;
Nick Dyere57a66a2014-05-18 23:13:40 -0700416 }
417
418 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
419
420 return buf[0];
421 } else {
422 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
423
424 return val;
425 }
426}
427
Benson Leungd79e7e42014-05-18 23:02:52 -0700428static int mxt_check_bootloader(struct mxt_data *data, unsigned int state)
429{
Nick Dyerf28a8422014-05-18 23:10:49 -0700430 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700431 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700432 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700433
434recheck:
Benson Leungd79e7e42014-05-18 23:02:52 -0700435 if (state != MXT_WAITING_BOOTLOAD_CMD) {
436 /*
437 * In application update mode, the interrupt
438 * line signals state transitions. We must wait for the
439 * CHG assertion before reading the status byte.
440 * Once the status byte has been read, the line is deasserted.
441 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700442 ret = mxt_wait_for_completion(data, &data->bl_completion,
443 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700444 if (ret) {
445 /*
446 * TODO: handle -ERESTARTSYS better by terminating
447 * fw update process before returning to userspace
448 * by writing length 0x000 to device (iff we are in
449 * WAITING_FRAME_DATA state).
450 */
Nick Dyerf28a8422014-05-18 23:10:49 -0700451 dev_err(dev, "Update wait error %d\n", ret);
Benson Leungd79e7e42014-05-18 23:02:52 -0700452 return ret;
453 }
454 }
455
Nick Dyerf28a8422014-05-18 23:10:49 -0700456 ret = mxt_bootloader_read(data, &val, 1);
457 if (ret)
458 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700459
Nick Dyere57a66a2014-05-18 23:13:40 -0700460 if (state == MXT_WAITING_BOOTLOAD_CMD)
461 val = mxt_get_bootloader_version(data, val);
462
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700463 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800464 case MXT_WAITING_BOOTLOAD_CMD:
465 case MXT_WAITING_FRAME_DATA:
466 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700467 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800468 case MXT_FRAME_CRC_PASS:
Nick Dyerf943c742014-05-18 23:14:20 -0700469 if (val == MXT_FRAME_CRC_CHECK) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700470 goto recheck;
Nick Dyerf943c742014-05-18 23:14:20 -0700471 } else if (val == MXT_FRAME_CRC_FAIL) {
472 dev_err(dev, "Bootloader CRC fail\n");
473 return -EINVAL;
474 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700475 break;
476 default:
477 return -EINVAL;
478 }
479
480 if (val != state) {
Nick Dyerf28a8422014-05-18 23:10:49 -0700481 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
Nick Dyer7bed6802014-05-18 23:04:09 -0700482 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483 return -EINVAL;
484 }
485
486 return 0;
487}
488
Nick Dyerf28a8422014-05-18 23:10:49 -0700489static int mxt_unlock_bootloader(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700490{
Nick Dyerf28a8422014-05-18 23:10:49 -0700491 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700492 u8 buf[2];
493
Iiro Valkonen7686b102011-02-02 23:21:58 -0800494 buf[0] = MXT_UNLOCK_CMD_LSB;
495 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700496
Nick Dyerf28a8422014-05-18 23:10:49 -0700497 ret = mxt_bootloader_write(data, buf, 2);
498 if (ret)
499 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700500
501 return 0;
502}
503
Iiro Valkonen7686b102011-02-02 23:21:58 -0800504static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700505 u16 reg, u16 len, void *val)
506{
507 struct i2c_msg xfer[2];
508 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800509 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700510
511 buf[0] = reg & 0xff;
512 buf[1] = (reg >> 8) & 0xff;
513
514 /* Write register */
515 xfer[0].addr = client->addr;
516 xfer[0].flags = 0;
517 xfer[0].len = 2;
518 xfer[0].buf = buf;
519
520 /* Read data */
521 xfer[1].addr = client->addr;
522 xfer[1].flags = I2C_M_RD;
523 xfer[1].len = len;
524 xfer[1].buf = val;
525
Daniel Kurtz771733e2012-06-28 21:08:11 +0800526 ret = i2c_transfer(client->adapter, xfer, 2);
527 if (ret == 2) {
528 ret = 0;
529 } else {
530 if (ret >= 0)
531 ret = -EIO;
532 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
533 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700534 }
535
Daniel Kurtz771733e2012-06-28 21:08:11 +0800536 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700537}
538
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800539static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
540 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700541{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800542 u8 *buf;
543 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800544 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700545
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800546 count = len + 2;
547 buf = kmalloc(count, GFP_KERNEL);
548 if (!buf)
549 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550
551 buf[0] = reg & 0xff;
552 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800553 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800555 ret = i2c_master_send(client, buf, count);
556 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800557 ret = 0;
558 } else {
559 if (ret >= 0)
560 ret = -EIO;
561 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
562 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700563 }
564
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800565 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800566 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700567}
568
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800569static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700570{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800571 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572}
573
Iiro Valkonen7686b102011-02-02 23:21:58 -0800574static struct mxt_object *
575mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700576{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800577 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700578 int i;
579
580 for (i = 0; i < data->info.object_num; i++) {
581 object = data->object_table + i;
582 if (object->type == type)
583 return object;
584 }
585
Nick Dyer50a77c62014-07-23 12:38:48 -0700586 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700587 return NULL;
588}
589
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590static int mxt_read_message(struct mxt_data *data,
591 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800593 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594 u16 reg;
595
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700596 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597 if (!object)
598 return -EINVAL;
599
600 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800601 return __mxt_read_reg(data->client, reg,
602 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700603}
604
Iiro Valkonen7686b102011-02-02 23:21:58 -0800605static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 u8 type, u8 offset, u8 val)
607{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800608 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700609 u16 reg;
610
Iiro Valkonen7686b102011-02-02 23:21:58 -0800611 object = mxt_get_object(data, type);
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700612 if (!object || offset >= mxt_obj_size(object))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700613 return -EINVAL;
614
615 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800616 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700617}
618
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800619static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
620{
621 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700622 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800623 bool button;
624 int i;
625
626 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700627 for (i = 0; i < pdata->t19_num_keys; i++) {
628 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800629 continue;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700630 button = !(message->message[0] & (1 << i));
631 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800632 }
633}
634
Benson Leungb735fbe2014-07-23 12:22:27 -0700635static void mxt_input_sync(struct mxt_data *data)
Nick Dyereef820d2014-05-18 23:22:22 -0700636{
Benson Leungb735fbe2014-07-23 12:22:27 -0700637 input_mt_report_pointer_emulation(data->input_dev,
638 data->pdata->t19_num_keys);
639 input_sync(data->input_dev);
Nick Dyereef820d2014-05-18 23:22:22 -0700640}
641
Iiro Valkonen7686b102011-02-02 23:21:58 -0800642static void mxt_input_touchevent(struct mxt_data *data,
643 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700644{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700645 struct device *dev = &data->client->dev;
646 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800647 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700648 int x;
649 int y;
650 int area;
Nick Dyerfea9e462014-05-18 23:21:48 -0700651 int amplitude;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700652
Joonyoung Shim910d8052011-04-12 23:14:38 -0700653 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
654 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
Nick Dyereef820d2014-05-18 23:22:22 -0700655
656 /* Handle 10/12 bit switching */
Joonyoung Shim910d8052011-04-12 23:14:38 -0700657 if (data->max_x < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700658 x >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700659 if (data->max_y < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700660 y >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700661
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700662 area = message->message[4];
Nick Dyerfea9e462014-05-18 23:21:48 -0700663 amplitude = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800665 dev_dbg(dev,
666 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
667 id,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700668 (status & MXT_T9_DETECT) ? 'D' : '.',
669 (status & MXT_T9_PRESS) ? 'P' : '.',
670 (status & MXT_T9_RELEASE) ? 'R' : '.',
671 (status & MXT_T9_MOVE) ? 'M' : '.',
672 (status & MXT_T9_VECTOR) ? 'V' : '.',
673 (status & MXT_T9_AMP) ? 'A' : '.',
674 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
675 (status & MXT_T9_UNGRIP) ? 'U' : '.',
Nick Dyerfea9e462014-05-18 23:21:48 -0700676 x, y, area, amplitude);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700677
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800678 input_mt_slot(input_dev, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679
Nick Dyerf3889ed2014-05-18 23:22:04 -0700680 if (status & MXT_T9_DETECT) {
Nick Dyereef820d2014-05-18 23:22:22 -0700681 /*
682 * Multiple bits may be set if the host is slow to read
683 * the status messages, indicating all the events that
684 * have happened.
685 */
686 if (status & MXT_T9_RELEASE) {
687 input_mt_report_slot_state(input_dev,
688 MT_TOOL_FINGER, 0);
Benson Leungb735fbe2014-07-23 12:22:27 -0700689 mxt_input_sync(data);
Nick Dyereef820d2014-05-18 23:22:22 -0700690 }
691
692 /* Touch active */
693 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800694 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
695 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
Nick Dyerfea9e462014-05-18 23:21:48 -0700696 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800697 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
Nick Dyereef820d2014-05-18 23:22:22 -0700698 } else {
699 /* Touch no longer active, close out slot */
700 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800701 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702}
703
Nick Dyerc3f78042014-05-18 23:04:46 -0700704static u16 mxt_extract_T6_csum(const u8 *csum)
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800705{
706 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
707}
708
Daniel Kurtz04a79182012-06-28 21:08:21 +0800709static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
710{
711 u8 id = msg->reportid;
712 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700713}
714
Benson Leungd79e7e42014-05-18 23:02:52 -0700715static irqreturn_t mxt_process_messages_until_invalid(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800717 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800718 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700720 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800721 bool update_input = false;
Nick Dyerc3f78042014-05-18 23:04:46 -0700722 u32 crc;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700723
724 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800725 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700726 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700727 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700728 }
729
730 reportid = message.reportid;
731
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800732 if (reportid == data->T6_reportid) {
733 u8 status = payload[0];
Nick Dyerc3f78042014-05-18 23:04:46 -0700734
735 crc = mxt_extract_T6_csum(&payload[1]);
736 if (crc != data->config_crc) {
737 data->config_crc = crc;
738 complete(&data->crc_completion);
739 }
740
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800741 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
Nick Dyerc3f78042014-05-18 23:04:46 -0700742 status, data->config_crc);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700743
744 if (status & MXT_T6_STATUS_RESET)
745 complete(&data->reset_completion);
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700746 } else if (!data->input_dev) {
747 /*
748 * do not report events if input device
749 * is not yet registered
750 */
751 mxt_dump_message(dev, &message);
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800752 } else if (mxt_is_T9_message(data, &message)) {
753 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800754 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800755 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800756 } else if (message.reportid == data->T19_reportid) {
757 mxt_input_button(data, &message);
758 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800759 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800760 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800761 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700762 } while (reportid != 0xff);
763
Nick Dyereef820d2014-05-18 23:22:22 -0700764 if (update_input)
Benson Leungb735fbe2014-07-23 12:22:27 -0700765 mxt_input_sync(data);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800766
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700767 return IRQ_HANDLED;
768}
769
Benson Leungd79e7e42014-05-18 23:02:52 -0700770static irqreturn_t mxt_interrupt(int irq, void *dev_id)
771{
772 struct mxt_data *data = dev_id;
773
774 if (data->in_bootloader) {
775 /* bootloader state transition completion */
776 complete(&data->bl_completion);
777 return IRQ_HANDLED;
778 }
779
Nick Dyerdd24dcf2014-07-23 11:25:55 -0700780 if (!data->object_table)
781 return IRQ_HANDLED;
782
Benson Leungd79e7e42014-05-18 23:02:52 -0700783 return mxt_process_messages_until_invalid(data);
784}
785
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700786static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
787 u8 value, bool wait)
788{
789 u16 reg;
790 u8 command_register;
791 int timeout_counter = 0;
792 int ret;
793
794 reg = data->T6_address + cmd_offset;
795
796 ret = mxt_write_reg(data->client, reg, value);
797 if (ret)
798 return ret;
799
800 if (!wait)
801 return 0;
802
803 do {
804 msleep(20);
805 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
806 if (ret)
807 return ret;
808 } while (command_register != 0 && timeout_counter++ <= 100);
809
810 if (timeout_counter > 100) {
811 dev_err(&data->client->dev, "Command failed!\n");
812 return -EIO;
813 }
814
815 return 0;
816}
817
818static int mxt_soft_reset(struct mxt_data *data)
819{
820 struct device *dev = &data->client->dev;
821 int ret = 0;
822
823 dev_info(dev, "Resetting chip\n");
824
825 reinit_completion(&data->reset_completion);
826
827 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
828 if (ret)
829 return ret;
830
831 ret = mxt_wait_for_completion(data, &data->reset_completion,
832 MXT_RESET_TIMEOUT);
833 if (ret)
834 return ret;
835
836 return 0;
837}
838
Nick Dyerc3f78042014-05-18 23:04:46 -0700839static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
840{
841 /*
842 * On failure, CRC is set to 0 and config will always be
843 * downloaded.
844 */
845 data->config_crc = 0;
846 reinit_completion(&data->crc_completion);
847
848 mxt_t6_command(data, cmd, value, true);
849
850 /*
851 * Wait for crc message. On failure, CRC is set to 0 and config will
852 * always be downloaded.
853 */
854 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
855}
856
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700857static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
858{
859 static const unsigned int crcpoly = 0x80001B;
860 u32 result;
861 u32 data_word;
862
863 data_word = (secondbyte << 8) | firstbyte;
864 result = ((*crc << 1) ^ data_word);
865
866 if (result & 0x1000000)
867 result ^= crcpoly;
868
869 *crc = result;
870}
871
872static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
873{
874 u32 crc = 0;
875 u8 *ptr = base + start_off;
876 u8 *last_val = base + end_off - 1;
877
878 if (end_off < start_off)
879 return -EINVAL;
880
881 while (ptr < last_val) {
882 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
883 ptr += 2;
884 }
885
886 /* if len is odd, fill the last byte with 0 */
887 if (ptr == last_val)
888 mxt_calc_crc24(&crc, *ptr, 0);
889
890 /* Mask to 24-bit */
891 crc &= 0x00FFFFFF;
892
893 return crc;
894}
895
Nick Dyer50a77c62014-07-23 12:38:48 -0700896/*
897 * mxt_update_cfg - download configuration to chip
898 *
899 * Atmel Raw Config File Format
900 *
901 * The first four lines of the raw config file contain:
902 * 1) Version
903 * 2) Chip ID Information (first 7 bytes of device memory)
904 * 3) Chip Information Block 24-bit CRC Checksum
905 * 4) Chip Configuration 24-bit CRC Checksum
906 *
907 * The rest of the file consists of one line per object instance:
908 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
909 *
910 * <TYPE> - 2-byte object type as hex
911 * <INSTANCE> - 2-byte object instance number as hex
912 * <SIZE> - 2-byte object size as hex
913 * <CONTENTS> - array of <SIZE> 1-byte hex values
914 */
915static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700916{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700917 struct device *dev = &data->client->dev;
Nick Dyer50a77c62014-07-23 12:38:48 -0700918 struct mxt_info cfg_info;
919 struct mxt_object *object;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800920 int ret;
Nick Dyer50a77c62014-07-23 12:38:48 -0700921 int offset;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700922 int data_pos;
923 int byte_offset;
Nick Dyer50a77c62014-07-23 12:38:48 -0700924 int i;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700925 int cfg_start_ofs;
926 u32 info_crc, config_crc, calculated_crc;
927 u8 *config_mem;
928 size_t config_mem_size;
Nick Dyer50a77c62014-07-23 12:38:48 -0700929 unsigned int type, instance, size;
930 u8 val;
931 u16 reg;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700932
Nick Dyerc3f78042014-05-18 23:04:46 -0700933 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
934
Nick Dyer50a77c62014-07-23 12:38:48 -0700935 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
936 dev_err(dev, "Unrecognised config file\n");
937 ret = -EINVAL;
938 goto release;
Nick Dyerc3f78042014-05-18 23:04:46 -0700939 }
940
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700941 data_pos = strlen(MXT_CFG_MAGIC);
Nick Dyerc3f78042014-05-18 23:04:46 -0700942
Nick Dyer50a77c62014-07-23 12:38:48 -0700943 /* Load information block and check */
944 for (i = 0; i < sizeof(struct mxt_info); i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700945 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Nick Dyer50a77c62014-07-23 12:38:48 -0700946 (unsigned char *)&cfg_info + i,
947 &offset);
948 if (ret != 1) {
949 dev_err(dev, "Bad format\n");
950 ret = -EINVAL;
951 goto release;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800952 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800953
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700954 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -0700955 }
956
957 if (cfg_info.family_id != data->info.family_id) {
958 dev_err(dev, "Family ID mismatch!\n");
959 ret = -EINVAL;
960 goto release;
961 }
962
963 if (cfg_info.variant_id != data->info.variant_id) {
964 dev_err(dev, "Variant ID mismatch!\n");
965 ret = -EINVAL;
966 goto release;
967 }
968
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700969 /* Read CRCs */
970 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset);
Nick Dyer50a77c62014-07-23 12:38:48 -0700971 if (ret != 1) {
972 dev_err(dev, "Bad format: failed to parse Info CRC\n");
973 ret = -EINVAL;
974 goto release;
975 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700976 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -0700977
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700978 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset);
Nick Dyer50a77c62014-07-23 12:38:48 -0700979 if (ret != 1) {
980 dev_err(dev, "Bad format: failed to parse Config CRC\n");
981 ret = -EINVAL;
982 goto release;
983 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700984 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -0700985
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700986 /*
987 * The Info Block CRC is calculated over mxt_info and the object
988 * table. If it does not match then we are trying to load the
989 * configuration from a different chip or firmware version, so
990 * the configuration CRC is invalid anyway.
991 */
992 if (info_crc == data->info_crc) {
993 if (config_crc == 0 || data->config_crc == 0) {
994 dev_info(dev, "CRC zero, attempting to apply config\n");
995 } else if (config_crc == data->config_crc) {
996 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
997 data->config_crc);
998 ret = 0;
999 goto release;
1000 } else {
1001 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1002 data->config_crc, config_crc);
1003 }
1004 } else {
1005 dev_warn(dev,
1006 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1007 data->info_crc, info_crc);
1008 }
1009
1010 /* Malloc memory to store configuration */
1011 cfg_start_ofs = MXT_OBJECT_START +
1012 data->info.object_num * sizeof(struct mxt_object) +
1013 MXT_INFO_CHECKSUM_SIZE;
1014 config_mem_size = data->mem_size - cfg_start_ofs;
1015 config_mem = kzalloc(config_mem_size, GFP_KERNEL);
1016 if (!config_mem) {
1017 dev_err(dev, "Failed to allocate memory\n");
1018 ret = -ENOMEM;
Nick Dyer50a77c62014-07-23 12:38:48 -07001019 goto release;
1020 }
1021
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001022 while (data_pos < cfg->size) {
Nick Dyer50a77c62014-07-23 12:38:48 -07001023 /* Read type, instance, length */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001024 ret = sscanf(cfg->data + data_pos, "%x %x %x%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001025 &type, &instance, &size, &offset);
1026 if (ret == 0) {
1027 /* EOF */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001028 break;
Nick Dyer50a77c62014-07-23 12:38:48 -07001029 } else if (ret != 3) {
1030 dev_err(dev, "Bad format: failed to parse object\n");
1031 ret = -EINVAL;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001032 goto release_mem;
Nick Dyer50a77c62014-07-23 12:38:48 -07001033 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001034 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001035
1036 object = mxt_get_object(data, type);
1037 if (!object) {
1038 /* Skip object */
1039 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001040 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001041 &val,
1042 &offset);
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001043 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001044 }
1045 continue;
1046 }
1047
1048 if (size > mxt_obj_size(object)) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001049 /*
1050 * Either we are in fallback mode due to wrong
1051 * config or config from a later fw version,
1052 * or the file is corrupt or hand-edited.
1053 */
1054 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1055 size - mxt_obj_size(object), type);
1056 } else if (mxt_obj_size(object) > size) {
1057 /*
1058 * If firmware is upgraded, new bytes may be added to
1059 * end of objects. It is generally forward compatible
1060 * to zero these bytes - previous behaviour will be
1061 * retained. However this does invalidate the CRC and
1062 * will force fallback mode until the configuration is
1063 * updated. We warn here but do nothing else - the
1064 * malloc has zeroed the entire configuration.
1065 */
1066 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1067 mxt_obj_size(object) - size, type);
Nick Dyer50a77c62014-07-23 12:38:48 -07001068 }
1069
1070 if (instance >= mxt_obj_instances(object)) {
1071 dev_err(dev, "Object instances exceeded!\n");
1072 ret = -EINVAL;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001073 goto release_mem;
Nick Dyer50a77c62014-07-23 12:38:48 -07001074 }
1075
1076 reg = object->start_address + mxt_obj_size(object) * instance;
1077
1078 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001079 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001080 &val,
1081 &offset);
1082 if (ret != 1) {
1083 dev_err(dev, "Bad format in T%d\n", type);
1084 ret = -EINVAL;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001085 goto release_mem;
Nick Dyer50a77c62014-07-23 12:38:48 -07001086 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001087 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001088
1089 if (i > mxt_obj_size(object))
1090 continue;
1091
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001092 byte_offset = reg + i - cfg_start_ofs;
Nick Dyer50a77c62014-07-23 12:38:48 -07001093
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001094 if ((byte_offset >= 0)
1095 && (byte_offset <= config_mem_size)) {
1096 *(config_mem + byte_offset) = val;
1097 } else {
1098 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1099 reg, object->type, byte_offset);
1100 ret = -EINVAL;
1101 goto release_mem;
Nick Dyer50a77c62014-07-23 12:38:48 -07001102 }
1103 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104 }
1105
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001106 /* Calculate crc of the received configs (not the raw config file) */
1107 if (data->T7_address < cfg_start_ofs) {
1108 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n",
1109 data->T7_address, cfg_start_ofs);
1110 ret = 0;
1111 goto release_mem;
1112 }
1113
1114 calculated_crc = mxt_calculate_crc(config_mem,
1115 data->T7_address - cfg_start_ofs,
1116 config_mem_size);
1117
1118 if (config_crc > 0 && (config_crc != calculated_crc))
1119 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n",
1120 calculated_crc, config_crc);
1121
1122 /* Write configuration as blocks */
1123 byte_offset = 0;
1124 while (byte_offset < config_mem_size) {
1125 size = config_mem_size - byte_offset;
1126
1127 if (size > MXT_MAX_BLOCK_WRITE)
1128 size = MXT_MAX_BLOCK_WRITE;
1129
1130 ret = __mxt_write_reg(data->client,
1131 cfg_start_ofs + byte_offset,
1132 size, config_mem + byte_offset);
1133 if (ret != 0) {
1134 dev_err(dev, "Config write error, ret=%d\n", ret);
1135 goto release_mem;
1136 }
1137
1138 byte_offset += size;
1139 }
1140
Nick Dyerc3f78042014-05-18 23:04:46 -07001141 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1142
1143 ret = mxt_soft_reset(data);
1144 if (ret)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001145 goto release_mem;
Nick Dyerc3f78042014-05-18 23:04:46 -07001146
1147 dev_info(dev, "Config successfully updated\n");
1148
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001149release_mem:
1150 kfree(config_mem);
Nick Dyer50a77c62014-07-23 12:38:48 -07001151release:
1152 release_firmware(cfg);
1153 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001154}
1155
Iiro Valkonen7686b102011-02-02 23:21:58 -08001156static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001157{
1158 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -08001159 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001160 int count = 10;
1161 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001162
1163 /* Read dummy message to make high CHG pin */
1164 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -08001165 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001166 if (error)
1167 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -08001168 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001169
1170 if (!count) {
1171 dev_err(dev, "CHG pin isn't cleared\n");
1172 return -EBUSY;
1173 }
1174
1175 return 0;
1176}
1177
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001178static int mxt_acquire_irq(struct mxt_data *data)
1179{
1180 int error;
1181
1182 enable_irq(data->irq);
1183
1184 error = mxt_make_highchg(data);
1185 if (error)
1186 return error;
1187
1188 return 0;
1189}
1190
Iiro Valkonen7686b102011-02-02 23:21:58 -08001191static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001192{
1193 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001194 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001195 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001196
Daniel Kurtz23003a842012-06-28 21:08:14 +08001197 /* Read 7-byte info block starting at address 0 */
1198 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001199 if (error)
1200 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201
1202 return 0;
1203}
1204
Iiro Valkonen7686b102011-02-02 23:21:58 -08001205static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001206{
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001207 struct i2c_client *client = data->client;
1208 size_t table_size;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001209 struct mxt_object *object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001210 int error;
1211 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001212 u8 reportid;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001213 u16 end_address;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001214
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001215 table_size = data->info.object_num * sizeof(struct mxt_object);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001216 object_table = kzalloc(table_size, GFP_KERNEL);
1217 if (!object_table) {
1218 dev_err(&data->client->dev, "Failed to allocate memory\n");
1219 return -ENOMEM;
1220 }
1221
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001222 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001223 object_table);
1224 if (error) {
1225 kfree(object_table);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001226 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001227 }
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001228
1229 /* Valid Report IDs start counting from 1 */
1230 reportid = 1;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001231 data->mem_size = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001232 for (i = 0; i < data->info.object_num; i++) {
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001233 struct mxt_object *object = object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001234 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001235
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001236 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001237
1238 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001239 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001240 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001241 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001242 max_id = reportid - 1;
1243 } else {
1244 min_id = 0;
1245 max_id = 0;
1246 }
1247
1248 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001249 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001250 object->type, object->start_address,
1251 mxt_obj_size(object), mxt_obj_instances(object),
1252 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001253
1254 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001255 case MXT_GEN_COMMAND_T6:
1256 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001257 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001258 break;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001259 case MXT_GEN_POWER_T7:
1260 data->T7_address = object->start_address;
1261 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001262 case MXT_TOUCH_MULTI_T9:
1263 data->T9_reportid_min = min_id;
1264 data->T9_reportid_max = max_id;
1265 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001266 case MXT_SPT_GPIOPWM_T19:
1267 data->T19_reportid = min_id;
1268 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001270
1271 end_address = object->start_address
1272 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1273
1274 if (end_address >= data->mem_size)
1275 data->mem_size = end_address + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001276 }
1277
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001278 data->object_table = object_table;
1279
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280 return 0;
1281}
1282
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001283static void mxt_free_object_table(struct mxt_data *data)
1284{
Nick Dyer7a53d602014-07-23 12:21:26 -07001285 input_unregister_device(data->input_dev);
1286 data->input_dev = NULL;
1287
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001288 kfree(data->object_table);
1289 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001290 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001291 data->T9_reportid_min = 0;
1292 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001293 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001294}
1295
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001296static int mxt_read_t9_resolution(struct mxt_data *data)
1297{
1298 struct i2c_client *client = data->client;
1299 int error;
1300 struct t9_range range;
1301 unsigned char orient;
1302 struct mxt_object *object;
1303
1304 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1305 if (!object)
1306 return -EINVAL;
1307
1308 error = __mxt_read_reg(client,
1309 object->start_address + MXT_T9_RANGE,
1310 sizeof(range), &range);
1311 if (error)
1312 return error;
1313
1314 le16_to_cpus(&range.x);
1315 le16_to_cpus(&range.y);
1316
1317 error = __mxt_read_reg(client,
1318 object->start_address + MXT_T9_ORIENT,
1319 1, &orient);
1320 if (error)
1321 return error;
1322
1323 /* Handle default values */
1324 if (range.x == 0)
1325 range.x = 1023;
1326
1327 if (range.y == 0)
1328 range.y = 1023;
1329
Nick Dyerf3889ed2014-05-18 23:22:04 -07001330 if (orient & MXT_T9_ORIENT_SWITCH) {
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001331 data->max_x = range.y;
1332 data->max_y = range.x;
1333 } else {
1334 data->max_x = range.x;
1335 data->max_y = range.y;
1336 }
1337
1338 dev_dbg(&client->dev,
1339 "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1340
1341 return 0;
1342}
1343
Nick Dyer7a53d602014-07-23 12:21:26 -07001344static int mxt_input_open(struct input_dev *dev);
1345static void mxt_input_close(struct input_dev *dev);
1346
1347static int mxt_initialize_t9_input_device(struct mxt_data *data)
1348{
1349 struct device *dev = &data->client->dev;
1350 const struct mxt_platform_data *pdata = data->pdata;
1351 struct input_dev *input_dev;
1352 int error;
1353 unsigned int num_mt_slots;
1354 unsigned int mt_flags = 0;
1355 int i;
1356
1357 error = mxt_read_t9_resolution(data);
1358 if (error)
1359 dev_warn(dev, "Failed to initialize T9 resolution\n");
1360
1361 input_dev = input_allocate_device();
1362 if (!input_dev) {
1363 dev_err(dev, "Failed to allocate memory\n");
1364 return -ENOMEM;
1365 }
1366
1367 input_dev->name = "Atmel maXTouch Touchscreen";
1368 input_dev->phys = data->phys;
1369 input_dev->id.bustype = BUS_I2C;
1370 input_dev->dev.parent = dev;
1371 input_dev->open = mxt_input_open;
1372 input_dev->close = mxt_input_close;
1373
1374 __set_bit(EV_ABS, input_dev->evbit);
1375 __set_bit(EV_KEY, input_dev->evbit);
1376 __set_bit(BTN_TOUCH, input_dev->keybit);
1377
1378 if (pdata->t19_num_keys) {
1379 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1380
1381 for (i = 0; i < pdata->t19_num_keys; i++)
1382 if (pdata->t19_keymap[i] != KEY_RESERVED)
1383 input_set_capability(input_dev, EV_KEY,
1384 pdata->t19_keymap[i]);
1385
1386 mt_flags |= INPUT_MT_POINTER;
1387
1388 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1389 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1390 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1391 MXT_PIXELS_PER_MM);
1392 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1393 MXT_PIXELS_PER_MM);
1394
1395 input_dev->name = "Atmel maXTouch Touchpad";
1396 }
1397
1398 /* For single touch */
1399 input_set_abs_params(input_dev, ABS_X,
1400 0, data->max_x, 0, 0);
1401 input_set_abs_params(input_dev, ABS_Y,
1402 0, data->max_y, 0, 0);
1403 input_set_abs_params(input_dev, ABS_PRESSURE,
1404 0, 255, 0, 0);
1405
1406 /* For multi touch */
1407 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1408 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
1409 if (error) {
1410 dev_err(dev, "Error %d initialising slots\n", error);
1411 goto err_free_mem;
1412 }
1413
1414 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1415 0, MXT_MAX_AREA, 0, 0);
1416 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1417 0, data->max_x, 0, 0);
1418 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1419 0, data->max_y, 0, 0);
1420 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1421 0, 255, 0, 0);
1422
1423 input_set_drvdata(input_dev, data);
1424
1425 error = input_register_device(input_dev);
1426 if (error) {
1427 dev_err(dev, "Error %d registering input device\n", error);
1428 goto err_free_mem;
1429 }
1430
1431 data->input_dev = input_dev;
1432
1433 return 0;
1434
1435err_free_mem:
1436 input_free_device(input_dev);
1437 return error;
1438}
1439
Nick Dyer50a77c62014-07-23 12:38:48 -07001440static int mxt_configure_objects(struct mxt_data *data,
1441 const struct firmware *cfg);
1442
1443static void mxt_config_cb(const struct firmware *cfg, void *ctx)
1444{
1445 mxt_configure_objects(ctx, cfg);
1446}
1447
Iiro Valkonen7686b102011-02-02 23:21:58 -08001448static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001449{
1450 struct i2c_client *client = data->client;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001451 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001452
Iiro Valkonen7686b102011-02-02 23:21:58 -08001453 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001454 if (error)
1455 return error;
1456
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001457 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001458 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001459 if (error) {
1460 dev_err(&client->dev, "Error %d reading object table\n", error);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001461 return error;
Nick Dyer7bed6802014-05-18 23:04:09 -07001462 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001463
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001464 mxt_acquire_irq(data);
1465 if (error)
1466 goto err_free_object_table;
1467
Nick Dyer50a77c62014-07-23 12:38:48 -07001468 request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
1469 &data->client->dev, GFP_KERNEL, data,
1470 mxt_config_cb);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001471
1472 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001473
1474err_free_object_table:
1475 mxt_free_object_table(data);
1476 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001477}
1478
Nick Dyer50a77c62014-07-23 12:38:48 -07001479static int mxt_configure_objects(struct mxt_data *data,
1480 const struct firmware *cfg)
1481{
1482 struct device *dev = &data->client->dev;
1483 struct mxt_info *info = &data->info;
1484 int error;
1485
1486 if (cfg) {
1487 error = mxt_update_cfg(data, cfg);
1488 if (error)
1489 dev_warn(dev, "Error %d updating config\n", error);
1490 }
1491
1492 error = mxt_initialize_t9_input_device(data);
1493 if (error)
1494 return error;
1495
1496 dev_info(dev,
1497 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1498 info->family_id, info->variant_id, info->version >> 4,
1499 info->version & 0xf, info->build, info->object_num);
1500
1501 return 0;
1502}
1503
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001504/* Firmware Version is returned as Major.Minor.Build */
1505static ssize_t mxt_fw_version_show(struct device *dev,
1506 struct device_attribute *attr, char *buf)
1507{
1508 struct mxt_data *data = dev_get_drvdata(dev);
1509 struct mxt_info *info = &data->info;
1510 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
1511 info->version >> 4, info->version & 0xf, info->build);
1512}
1513
1514/* Hardware Version is returned as FamilyID.VariantID */
1515static ssize_t mxt_hw_version_show(struct device *dev,
1516 struct device_attribute *attr, char *buf)
1517{
1518 struct mxt_data *data = dev_get_drvdata(dev);
1519 struct mxt_info *info = &data->info;
1520 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
1521 info->family_id, info->variant_id);
1522}
1523
Daniel Kurtz794eb672012-06-28 21:08:10 +08001524static ssize_t mxt_show_instance(char *buf, int count,
1525 struct mxt_object *object, int instance,
1526 const u8 *val)
1527{
1528 int i;
1529
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001530 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001531 count += scnprintf(buf + count, PAGE_SIZE - count,
1532 "Instance %u\n", instance);
1533
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001534 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001535 count += scnprintf(buf + count, PAGE_SIZE - count,
1536 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
1537 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
1538
1539 return count;
1540}
1541
Iiro Valkonen7686b102011-02-02 23:21:58 -08001542static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001543 struct device_attribute *attr, char *buf)
1544{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001545 struct mxt_data *data = dev_get_drvdata(dev);
1546 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001547 int count = 0;
1548 int i, j;
1549 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001550 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001551
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001552 /* Pre-allocate buffer large enough to hold max sized object. */
1553 obuf = kmalloc(256, GFP_KERNEL);
1554 if (!obuf)
1555 return -ENOMEM;
1556
1557 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001558 for (i = 0; i < data->info.object_num; i++) {
1559 object = data->object_table + i;
1560
Daniel Kurtz91630952012-06-28 21:08:09 +08001561 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001562 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001563
Daniel Kurtz91630952012-06-28 21:08:09 +08001564 count += scnprintf(buf + count, PAGE_SIZE - count,
1565 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001566
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001567 for (j = 0; j < mxt_obj_instances(object); j++) {
1568 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08001569 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001570
Daniel Kurtz794eb672012-06-28 21:08:10 +08001571 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001572 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08001573 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001574
Daniel Kurtz794eb672012-06-28 21:08:10 +08001575 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001576 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001577 }
1578
Daniel Kurtz794eb672012-06-28 21:08:10 +08001579done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08001580 kfree(obuf);
1581 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001582}
1583
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001584static int mxt_check_firmware_format(struct device *dev,
1585 const struct firmware *fw)
1586{
1587 unsigned int pos = 0;
1588 char c;
1589
1590 while (pos < fw->size) {
1591 c = *(fw->data + pos);
1592
1593 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
1594 return 0;
1595
1596 pos++;
1597 }
1598
1599 /*
1600 * To convert file try:
1601 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
1602 */
1603 dev_err(dev, "Aborting: firmware file must be in binary format\n");
1604
1605 return -EINVAL;
1606}
1607
Iiro Valkonen7686b102011-02-02 23:21:58 -08001608static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001609{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001610 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001611 const struct firmware *fw = NULL;
1612 unsigned int frame_size;
1613 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07001614 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07001615 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001616 int ret;
1617
1618 ret = request_firmware(&fw, fn, dev);
1619 if (ret) {
1620 dev_err(dev, "Unable to open firmware %s\n", fn);
1621 return ret;
1622 }
1623
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07001624 /* Check for incorrect enc file */
1625 ret = mxt_check_firmware_format(dev, fw);
1626 if (ret)
1627 goto release_firmware;
1628
Nick Dyerf28a8422014-05-18 23:10:49 -07001629 ret = mxt_lookup_bootloader_address(data);
1630 if (ret)
1631 goto release_firmware;
1632
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001633 /* Change to the bootloader mode */
Benson Leungd79e7e42014-05-18 23:02:52 -07001634 data->in_bootloader = true;
1635
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001636 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_BOOT_VALUE, false);
1637 if (ret)
1638 goto release_firmware;
1639
Iiro Valkonen7686b102011-02-02 23:21:58 -08001640 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001641
Benson Leungd79e7e42014-05-18 23:02:52 -07001642 reinit_completion(&data->bl_completion);
1643
1644 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001645 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001646 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001647
1648 /* Unlock bootloader */
Nick Dyerf28a8422014-05-18 23:10:49 -07001649 mxt_unlock_bootloader(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001650
1651 while (pos < fw->size) {
Benson Leungd79e7e42014-05-18 23:02:52 -07001652 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001653 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001654 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001655
1656 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1657
Nick Dyerf943c742014-05-18 23:14:20 -07001658 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001659 frame_size += 2;
1660
1661 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07001662 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
1663 if (ret)
1664 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001665
Benson Leungd79e7e42014-05-18 23:02:52 -07001666 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
Nick Dyerf943c742014-05-18 23:14:20 -07001667 if (ret) {
1668 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001669
Nick Dyerf943c742014-05-18 23:14:20 -07001670 /* Back off by 20ms per retry */
1671 msleep(retry * 20);
1672
1673 if (retry > 20) {
1674 dev_err(dev, "Retry count exceeded\n");
1675 goto disable_irq;
1676 }
1677 } else {
1678 retry = 0;
1679 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07001680 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07001681 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001682
Nick Dyerf477c752014-05-18 23:14:45 -07001683 if (frame % 50 == 0)
1684 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
1685 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001686 }
1687
Benson Leunga0434b72014-05-18 23:03:09 -07001688 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001689 ret = mxt_wait_for_completion(data, &data->bl_completion,
1690 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001691 if (ret)
1692 goto disable_irq;
1693
Nick Dyerf477c752014-05-18 23:14:45 -07001694 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
1695
Benson Leunga0434b72014-05-18 23:03:09 -07001696 /*
1697 * Wait for device to reset. Some bootloader versions do not assert
1698 * the CHG line after bootloading has finished, so ignore potential
1699 * errors.
1700 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001701 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07001702
Benson Leungd79e7e42014-05-18 23:02:52 -07001703 data->in_bootloader = false;
1704
1705disable_irq:
1706 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001707release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001708 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001709 return ret;
1710}
1711
Iiro Valkonen7686b102011-02-02 23:21:58 -08001712static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001713 struct device_attribute *attr,
1714 const char *buf, size_t count)
1715{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001716 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001717 int error;
1718
Iiro Valkonen7686b102011-02-02 23:21:58 -08001719 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001720 if (error) {
1721 dev_err(dev, "The firmware update failed(%d)\n", error);
1722 count = error;
1723 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07001724 dev_info(dev, "The firmware update succeeded\n");
1725
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001726 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001727
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001728 error = mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07001729 if (error)
1730 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001731 }
1732
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001733 return count;
1734}
1735
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001736static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1737static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001738static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1739static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001740
Iiro Valkonen7686b102011-02-02 23:21:58 -08001741static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001742 &dev_attr_fw_version.attr,
1743 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001744 &dev_attr_object.attr,
1745 &dev_attr_update_fw.attr,
1746 NULL
1747};
1748
Iiro Valkonen7686b102011-02-02 23:21:58 -08001749static const struct attribute_group mxt_attr_group = {
1750 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001751};
1752
Iiro Valkonen7686b102011-02-02 23:21:58 -08001753static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001754{
1755 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001756 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001757 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001758}
1759
Iiro Valkonen7686b102011-02-02 23:21:58 -08001760static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001761{
1762 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001763 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001764 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001765}
1766
Iiro Valkonen7686b102011-02-02 23:21:58 -08001767static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001768{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001769 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001770
Iiro Valkonen7686b102011-02-02 23:21:58 -08001771 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001772
1773 return 0;
1774}
1775
Iiro Valkonen7686b102011-02-02 23:21:58 -08001776static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001777{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001778 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001779
Iiro Valkonen7686b102011-02-02 23:21:58 -08001780 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001781}
1782
Stephen Warren78188be2014-07-23 12:23:23 -07001783#ifdef CONFIG_OF
1784static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
1785{
1786 struct mxt_platform_data *pdata;
1787 u32 *keymap;
1788 u32 keycode;
1789 int proplen, i, ret;
1790
1791 if (!client->dev.of_node)
1792 return ERR_PTR(-ENODEV);
1793
1794 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1795 if (!pdata)
1796 return ERR_PTR(-ENOMEM);
1797
1798 if (of_find_property(client->dev.of_node, "linux,gpio-keymap",
1799 &proplen)) {
1800 pdata->t19_num_keys = proplen / sizeof(u32);
1801
1802 keymap = devm_kzalloc(&client->dev,
1803 pdata->t19_num_keys * sizeof(keymap[0]),
1804 GFP_KERNEL);
1805 if (!keymap)
1806 return ERR_PTR(-ENOMEM);
1807
1808 for (i = 0; i < pdata->t19_num_keys; i++) {
1809 ret = of_property_read_u32_index(client->dev.of_node,
1810 "linux,gpio-keymap", i, &keycode);
1811 if (ret)
1812 keycode = KEY_RESERVED;
1813
1814 keymap[i] = keycode;
1815 }
1816
1817 pdata->t19_keymap = keymap;
1818 }
1819
1820 return pdata;
1821}
1822#else
1823static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
1824{
1825 dev_dbg(&client->dev, "No platform data specified\n");
1826 return ERR_PTR(-EINVAL);
1827}
1828#endif
1829
1830static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001831{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001832 struct mxt_data *data;
Stephen Warren78188be2014-07-23 12:23:23 -07001833 const struct mxt_platform_data *pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001834 int error;
1835
Stephen Warren78188be2014-07-23 12:23:23 -07001836 pdata = dev_get_platdata(&client->dev);
1837 if (!pdata) {
1838 pdata = mxt_parse_dt(client);
1839 if (IS_ERR(pdata))
1840 return PTR_ERR(pdata);
1841 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001842
Iiro Valkonen7686b102011-02-02 23:21:58 -08001843 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Nick Dyer7a53d602014-07-23 12:21:26 -07001844 if (!data) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001845 dev_err(&client->dev, "Failed to allocate memory\n");
Nick Dyer7a53d602014-07-23 12:21:26 -07001846 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001847 }
1848
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001849 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1850 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001851
Joonyoung Shim910d8052011-04-12 23:14:38 -07001852 data->client = client;
Joonyoung Shim910d8052011-04-12 23:14:38 -07001853 data->pdata = pdata;
1854 data->irq = client->irq;
Nick Dyer7a53d602014-07-23 12:21:26 -07001855 i2c_set_clientdata(client, data);
Joonyoung Shim910d8052011-04-12 23:14:38 -07001856
Benson Leungd79e7e42014-05-18 23:02:52 -07001857 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001858 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07001859 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07001860
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001861 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
1862 pdata->irqflags | IRQF_ONESHOT,
1863 client->name, data);
1864 if (error) {
1865 dev_err(&client->dev, "Failed to register interrupt\n");
1866 goto err_free_mem;
1867 }
1868
1869 disable_irq(client->irq);
1870
Daniel Kurtzcb159112012-06-28 21:08:22 +08001871 error = mxt_initialize(data);
1872 if (error)
Nick Dyer7a53d602014-07-23 12:21:26 -07001873 goto err_free_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001874
Iiro Valkonen7686b102011-02-02 23:21:58 -08001875 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07001876 if (error) {
1877 dev_err(&client->dev, "Failure %d creating sysfs group\n",
1878 error);
Nick Dyer7a53d602014-07-23 12:21:26 -07001879 goto err_free_object;
Nick Dyer7bed6802014-05-18 23:04:09 -07001880 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001881
1882 return 0;
1883
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001884err_free_object:
1885 kfree(data->object_table);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001886err_free_irq:
1887 free_irq(client->irq, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001888err_free_mem:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001889 kfree(data);
1890 return error;
1891}
1892
Bill Pembertone2619cf2012-11-23 21:50:47 -08001893static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001894{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001895 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001896
Iiro Valkonen7686b102011-02-02 23:21:58 -08001897 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001898 free_irq(data->irq, data);
1899 input_unregister_device(data->input_dev);
1900 kfree(data->object_table);
1901 kfree(data);
1902
1903 return 0;
1904}
1905
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001906#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001907static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001908{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001909 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001910 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001911 struct input_dev *input_dev = data->input_dev;
1912
1913 mutex_lock(&input_dev->mutex);
1914
1915 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001916 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001917
1918 mutex_unlock(&input_dev->mutex);
1919
1920 return 0;
1921}
1922
Iiro Valkonen7686b102011-02-02 23:21:58 -08001923static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001924{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001925 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001926 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001927 struct input_dev *input_dev = data->input_dev;
1928
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001929 mxt_soft_reset(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001930
1931 mutex_lock(&input_dev->mutex);
1932
1933 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001934 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001935
1936 mutex_unlock(&input_dev->mutex);
1937
1938 return 0;
1939}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001940#endif
1941
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001942static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1943
Stephen Warren78188be2014-07-23 12:23:23 -07001944static const struct of_device_id mxt_of_match[] = {
1945 { .compatible = "atmel,maxtouch", },
1946 {},
1947};
1948MODULE_DEVICE_TABLE(of, mxt_of_match);
1949
Iiro Valkonen7686b102011-02-02 23:21:58 -08001950static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001951 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001952 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001953 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001954 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001955 { }
1956};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001957MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001958
Iiro Valkonen7686b102011-02-02 23:21:58 -08001959static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001960 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001961 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001962 .owner = THIS_MODULE,
Stephen Warren78188be2014-07-23 12:23:23 -07001963 .of_match_table = of_match_ptr(mxt_of_match),
Iiro Valkonen7686b102011-02-02 23:21:58 -08001964 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001965 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001966 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001967 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001968 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001969};
1970
Axel Lin1b92c1c2012-03-16 23:05:41 -07001971module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001972
1973/* Module information */
1974MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001975MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001976MODULE_LICENSE("GPL");