blob: 5af7907d0af4dd6ab79639ee91287cdf0bc90202 [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
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -070017#include <linux/acpi.h>
18#include <linux/dmi.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070019#include <linux/module.h>
Benson Leungd79e7e42014-05-18 23:02:52 -070020#include <linux/init.h>
21#include <linux/completion.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070022#include <linux/delay.h>
23#include <linux/firmware.h>
24#include <linux/i2c.h>
Nick Dyer7f3884f72015-08-04 16:36:29 -070025#include <linux/platform_data/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070026#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070027#include <linux/interrupt.h>
Stephen Warren78188be2014-07-23 12:23:23 -070028#include <linux/of.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070029#include <linux/slab.h>
Nick Dyerb23157d2015-04-06 11:25:13 -070030#include <asm/unaligned.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070031
Nick Dyer50a77c62014-07-23 12:38:48 -070032/* Firmware files */
Iiro Valkonen7686b102011-02-02 23:21:58 -080033#define MXT_FW_NAME "maxtouch.fw"
Nick Dyer50a77c62014-07-23 12:38:48 -070034#define MXT_CFG_NAME "maxtouch.cfg"
35#define MXT_CFG_MAGIC "OBP_RAW V1"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070036
37/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080038#define MXT_OBJECT_START 0x07
Iiro Valkonen7686b102011-02-02 23:21:58 -080039#define MXT_OBJECT_SIZE 6
Nick Dyer4ce6fa02014-07-23 12:40:09 -070040#define MXT_INFO_CHECKSUM_SIZE 3
41#define MXT_MAX_BLOCK_WRITE 256
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070042
43/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070044#define MXT_DEBUG_DIAGNOSTIC_T37 37
45#define MXT_GEN_MESSAGE_T5 5
46#define MXT_GEN_COMMAND_T6 6
47#define MXT_GEN_POWER_T7 7
48#define MXT_GEN_ACQUIRE_T8 8
49#define MXT_GEN_DATASOURCE_T53 53
50#define MXT_TOUCH_MULTI_T9 9
51#define MXT_TOUCH_KEYARRAY_T15 15
52#define MXT_TOUCH_PROXIMITY_T23 23
53#define MXT_TOUCH_PROXKEY_T52 52
54#define MXT_PROCI_GRIPFACE_T20 20
55#define MXT_PROCG_NOISE_T22 22
56#define MXT_PROCI_ONETOUCH_T24 24
57#define MXT_PROCI_TWOTOUCH_T27 27
58#define MXT_PROCI_GRIP_T40 40
59#define MXT_PROCI_PALM_T41 41
60#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
61#define MXT_PROCI_STYLUS_T47 47
62#define MXT_PROCG_NOISESUPPRESSION_T48 48
63#define MXT_SPT_COMMSCONFIG_T18 18
64#define MXT_SPT_GPIOPWM_T19 19
65#define MXT_SPT_SELFTEST_T25 25
66#define MXT_SPT_CTECONFIG_T28 28
67#define MXT_SPT_USERDATA_T38 38
68#define MXT_SPT_DIGITIZER_T43 43
69#define MXT_SPT_MESSAGECOUNT_T44 44
70#define MXT_SPT_CTECONFIG_T46 46
Nick Dyerb23157d2015-04-06 11:25:13 -070071#define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070072
Nick Dyer5f3f9bc2014-07-23 12:46:55 -070073/* MXT_GEN_MESSAGE_T5 object */
74#define MXT_RPTID_NOMSG 0xff
75
Iiro Valkonen81c88a72011-07-04 03:08:25 -070076/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080077#define MXT_COMMAND_RESET 0
78#define MXT_COMMAND_BACKUPNV 1
79#define MXT_COMMAND_CALIBRATE 2
80#define MXT_COMMAND_REPORTALL 3
81#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070082
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070083/* Define for T6 status byte */
84#define MXT_T6_STATUS_RESET (1 << 7)
Nick Dyer497767d2014-07-23 12:47:50 -070085#define MXT_T6_STATUS_OFL (1 << 6)
86#define MXT_T6_STATUS_SIGERR (1 << 5)
87#define MXT_T6_STATUS_CAL (1 << 4)
88#define MXT_T6_STATUS_CFGERR (1 << 3)
89#define MXT_T6_STATUS_COMSERR (1 << 2)
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070090
Iiro Valkonen81c88a72011-07-04 03:08:25 -070091/* MXT_GEN_POWER_T7 field */
Nick Dyer7f3884f72015-08-04 16:36:29 -070092struct t7_config {
93 u8 idle;
94 u8 active;
95} __packed;
96
97#define MXT_POWER_CFG_RUN 0
98#define MXT_POWER_CFG_DEEPSLEEP 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070099
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700100/* MXT_TOUCH_MULTI_T9 field */
Nick Dyer7f3884f72015-08-04 16:36:29 -0700101#define MXT_T9_CTRL 0
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700102#define MXT_T9_ORIENT 9
103#define MXT_T9_RANGE 18
104
Nick Dyerf3889ed2014-05-18 23:22:04 -0700105/* MXT_TOUCH_MULTI_T9 status */
106#define MXT_T9_UNGRIP (1 << 0)
107#define MXT_T9_SUPPRESS (1 << 1)
108#define MXT_T9_AMP (1 << 2)
109#define MXT_T9_VECTOR (1 << 3)
110#define MXT_T9_MOVE (1 << 4)
111#define MXT_T9_RELEASE (1 << 5)
112#define MXT_T9_PRESS (1 << 6)
113#define MXT_T9_DETECT (1 << 7)
114
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700115struct t9_range {
Nick Dyer1c0276d2016-01-10 18:29:34 -0800116 __le16 x;
117 __le16 y;
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700118} __packed;
119
Nick Dyerf3889ed2014-05-18 23:22:04 -0700120/* MXT_TOUCH_MULTI_T9 orient */
121#define MXT_T9_ORIENT_SWITCH (1 << 0)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700122
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700123/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800124#define MXT_COMMS_CTRL 0
125#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700126
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700127/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800128#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700129#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800130#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700131
Nick Dyerb23157d2015-04-06 11:25:13 -0700132/* T100 Multiple Touch Touchscreen */
133#define MXT_T100_CTRL 0
134#define MXT_T100_CFG1 1
135#define MXT_T100_TCHAUX 3
136#define MXT_T100_XRANGE 13
137#define MXT_T100_YRANGE 24
138
139#define MXT_T100_CFG_SWITCHXY BIT(5)
140
141#define MXT_T100_TCHAUX_VECT BIT(0)
142#define MXT_T100_TCHAUX_AMPL BIT(1)
143#define MXT_T100_TCHAUX_AREA BIT(2)
144
145#define MXT_T100_DETECT BIT(7)
146#define MXT_T100_TYPE_MASK 0x70
147
148enum t100_type {
149 MXT_T100_TYPE_FINGER = 1,
150 MXT_T100_TYPE_PASSIVE_STYLUS = 2,
151 MXT_T100_TYPE_HOVERING_FINGER = 4,
152 MXT_T100_TYPE_GLOVE = 5,
153 MXT_T100_TYPE_LARGE_TOUCH = 6,
154};
155
156#define MXT_DISTANCE_ACTIVE_TOUCH 0
157#define MXT_DISTANCE_HOVERING 1
158
159#define MXT_TOUCH_MAJOR_DEFAULT 1
160#define MXT_PRESSURE_DEFAULT 1
161
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700162/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800163#define MXT_BACKUP_TIME 50 /* msec */
164#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700165#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700166#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700167#define MXT_FW_RESET_TIME 3000 /* msec */
168#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700169
170/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800171#define MXT_UNLOCK_CMD_MSB 0xaa
172#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700173
174/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800175#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
176#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
177#define MXT_FRAME_CRC_CHECK 0x02
178#define MXT_FRAME_CRC_FAIL 0x03
179#define MXT_FRAME_CRC_PASS 0x04
180#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
181#define MXT_BOOT_STATUS_MASK 0x3f
Nick Dyere57a66a2014-05-18 23:13:40 -0700182#define MXT_BOOT_EXTENDED_ID (1 << 5)
183#define MXT_BOOT_ID_MASK 0x1f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700184
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700185/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800186#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700187
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800188#define MXT_PIXELS_PER_MM 20
189
Iiro Valkonen7686b102011-02-02 23:21:58 -0800190struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700191 u8 family_id;
192 u8 variant_id;
193 u8 version;
194 u8 build;
195 u8 matrix_xsize;
196 u8 matrix_ysize;
197 u8 object_num;
198};
199
Iiro Valkonen7686b102011-02-02 23:21:58 -0800200struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700201 u8 type;
202 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700203 u8 size_minus_one;
204 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700205 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800206} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700207
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700208/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800209struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700210 struct i2c_client *client;
211 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800212 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800213 const struct mxt_platform_data *pdata;
214 struct mxt_object *object_table;
215 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700216 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700217 unsigned int max_x;
218 unsigned int max_y;
Nick Dyer1c0276d2016-01-10 18:29:34 -0800219 bool xy_switch;
Benson Leungd79e7e42014-05-18 23:02:52 -0700220 bool in_bootloader;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700221 u16 mem_size;
Nick Dyerb23157d2015-04-06 11:25:13 -0700222 u8 t100_aux_ampl;
223 u8 t100_aux_area;
224 u8 t100_aux_vect;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700225 u8 max_reportid;
Nick Dyerc3f78042014-05-18 23:04:46 -0700226 u32 config_crc;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700227 u32 info_crc;
Nick Dyerf28a8422014-05-18 23:10:49 -0700228 u8 bootloader_addr;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700229 u8 *msg_buf;
Nick Dyer497767d2014-07-23 12:47:50 -0700230 u8 t6_status;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700231 bool update_input;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700232 u8 last_message_count;
233 u8 num_touchids;
Nick Dyerb23157d2015-04-06 11:25:13 -0700234 u8 multitouch;
Nick Dyer7f3884f72015-08-04 16:36:29 -0700235 struct t7_config t7_cfg;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800236
237 /* Cached parameters from object table */
Nick Dyerb9b05a82014-07-23 12:48:13 -0700238 u16 T5_address;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700239 u8 T5_msg_size;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800240 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700241 u16 T6_address;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700242 u16 T7_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800243 u8 T9_reportid_min;
244 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800245 u8 T19_reportid;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700246 u16 T44_address;
Nick Dyerb23157d2015-04-06 11:25:13 -0700247 u8 T100_reportid_min;
248 u8 T100_reportid_max;
Benson Leungd79e7e42014-05-18 23:02:52 -0700249
250 /* for fw update in bootloader */
251 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700252
253 /* for reset handling */
254 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700255
256 /* for config update handling */
257 struct completion crc_completion;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700258};
259
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700260static size_t mxt_obj_size(const struct mxt_object *obj)
261{
262 return obj->size_minus_one + 1;
263}
264
265static size_t mxt_obj_instances(const struct mxt_object *obj)
266{
267 return obj->instances_minus_one + 1;
268}
269
Iiro Valkonen7686b102011-02-02 23:21:58 -0800270static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700271{
272 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700273 case MXT_GEN_COMMAND_T6:
274 case MXT_GEN_POWER_T7:
275 case MXT_GEN_ACQUIRE_T8:
276 case MXT_GEN_DATASOURCE_T53:
277 case MXT_TOUCH_MULTI_T9:
278 case MXT_TOUCH_KEYARRAY_T15:
279 case MXT_TOUCH_PROXIMITY_T23:
280 case MXT_TOUCH_PROXKEY_T52:
281 case MXT_PROCI_GRIPFACE_T20:
282 case MXT_PROCG_NOISE_T22:
283 case MXT_PROCI_ONETOUCH_T24:
284 case MXT_PROCI_TWOTOUCH_T27:
285 case MXT_PROCI_GRIP_T40:
286 case MXT_PROCI_PALM_T41:
287 case MXT_PROCI_TOUCHSUPPRESSION_T42:
288 case MXT_PROCI_STYLUS_T47:
289 case MXT_PROCG_NOISESUPPRESSION_T48:
290 case MXT_SPT_COMMSCONFIG_T18:
291 case MXT_SPT_GPIOPWM_T19:
292 case MXT_SPT_SELFTEST_T25:
293 case MXT_SPT_CTECONFIG_T28:
294 case MXT_SPT_USERDATA_T38:
295 case MXT_SPT_DIGITIZER_T43:
296 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700297 return true;
298 default:
299 return false;
300 }
301}
302
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700303static void mxt_dump_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700304{
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700305 dev_dbg(&data->client->dev, "message: %*ph\n",
306 data->T5_msg_size, message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700307}
308
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700309static int mxt_wait_for_completion(struct mxt_data *data,
310 struct completion *comp,
311 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700312{
Benson Leungd79e7e42014-05-18 23:02:52 -0700313 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700314 unsigned long timeout = msecs_to_jiffies(timeout_ms);
315 long ret;
316
317 ret = wait_for_completion_interruptible_timeout(comp, timeout);
318 if (ret < 0) {
319 return ret;
320 } else if (ret == 0) {
321 dev_err(dev, "Wait for completion timed out.\n");
322 return -ETIMEDOUT;
323 }
324 return 0;
325}
326
Nick Dyerf28a8422014-05-18 23:10:49 -0700327static int mxt_bootloader_read(struct mxt_data *data,
328 u8 *val, unsigned int count)
329{
330 int ret;
331 struct i2c_msg msg;
332
333 msg.addr = data->bootloader_addr;
334 msg.flags = data->client->flags & I2C_M_TEN;
335 msg.flags |= I2C_M_RD;
336 msg.len = count;
337 msg.buf = val;
338
339 ret = i2c_transfer(data->client->adapter, &msg, 1);
Nick Dyerf28a8422014-05-18 23:10:49 -0700340 if (ret == 1) {
341 ret = 0;
342 } else {
343 ret = ret < 0 ? ret : -EIO;
344 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
345 __func__, ret);
346 }
347
348 return ret;
349}
350
351static int mxt_bootloader_write(struct mxt_data *data,
352 const u8 * const val, unsigned int count)
353{
354 int ret;
355 struct i2c_msg msg;
356
357 msg.addr = data->bootloader_addr;
358 msg.flags = data->client->flags & I2C_M_TEN;
359 msg.len = count;
360 msg.buf = (u8 *)val;
361
362 ret = i2c_transfer(data->client->adapter, &msg, 1);
363 if (ret == 1) {
364 ret = 0;
365 } else {
366 ret = ret < 0 ? ret : -EIO;
367 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
368 __func__, ret);
369 }
370
371 return ret;
372}
373
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700374static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
Nick Dyerf28a8422014-05-18 23:10:49 -0700375{
376 u8 appmode = data->client->addr;
377 u8 bootloader;
378
379 switch (appmode) {
380 case 0x4a:
381 case 0x4b:
Nick Dyer44a0bab2014-07-23 12:45:26 -0700382 /* Chips after 1664S use different scheme */
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700383 if (retry || data->info.family_id >= 0xa2) {
Nick Dyer44a0bab2014-07-23 12:45:26 -0700384 bootloader = appmode - 0x24;
385 break;
386 }
387 /* Fall through for normal case */
Nick Dyerf28a8422014-05-18 23:10:49 -0700388 case 0x4c:
389 case 0x4d:
390 case 0x5a:
391 case 0x5b:
392 bootloader = appmode - 0x26;
393 break;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700394
Nick Dyerf28a8422014-05-18 23:10:49 -0700395 default:
396 dev_err(&data->client->dev,
397 "Appmode i2c address 0x%02x not found\n",
398 appmode);
399 return -EINVAL;
400 }
401
402 data->bootloader_addr = bootloader;
403 return 0;
404}
405
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700406static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700407{
408 struct device *dev = &data->client->dev;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700409 int error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700410 u8 val;
411 bool crc_failure;
412
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700413 error = mxt_lookup_bootloader_address(data, alt_address);
414 if (error)
415 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700416
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700417 error = mxt_bootloader_read(data, &val, 1);
418 if (error)
419 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700420
421 /* Check app crc fail mode */
422 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
423
424 dev_err(dev, "Detected bootloader, status:%02X%s\n",
425 val, crc_failure ? ", APP_CRC_FAIL" : "");
426
427 return 0;
428}
429
Nick Dyere57a66a2014-05-18 23:13:40 -0700430static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
431{
432 struct device *dev = &data->client->dev;
433 u8 buf[3];
434
435 if (val & MXT_BOOT_EXTENDED_ID) {
436 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
437 dev_err(dev, "%s: i2c failure\n", __func__);
Nick Dyer68807a02014-06-07 23:17:26 -0700438 return val;
Nick Dyere57a66a2014-05-18 23:13:40 -0700439 }
440
441 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
442
443 return buf[0];
444 } else {
445 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
446
447 return val;
448 }
449}
450
Nick Dyer385deb92014-07-23 12:42:40 -0700451static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
452 bool wait)
Benson Leungd79e7e42014-05-18 23:02:52 -0700453{
Nick Dyerf28a8422014-05-18 23:10:49 -0700454 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700455 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700456 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700457
458recheck:
Nick Dyer385deb92014-07-23 12:42:40 -0700459 if (wait) {
Benson Leungd79e7e42014-05-18 23:02:52 -0700460 /*
461 * In application update mode, the interrupt
462 * line signals state transitions. We must wait for the
463 * CHG assertion before reading the status byte.
464 * Once the status byte has been read, the line is deasserted.
465 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700466 ret = mxt_wait_for_completion(data, &data->bl_completion,
467 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700468 if (ret) {
469 /*
470 * TODO: handle -ERESTARTSYS better by terminating
471 * fw update process before returning to userspace
472 * by writing length 0x000 to device (iff we are in
473 * WAITING_FRAME_DATA state).
474 */
Nick Dyerf28a8422014-05-18 23:10:49 -0700475 dev_err(dev, "Update wait error %d\n", ret);
Benson Leungd79e7e42014-05-18 23:02:52 -0700476 return ret;
477 }
478 }
479
Nick Dyerf28a8422014-05-18 23:10:49 -0700480 ret = mxt_bootloader_read(data, &val, 1);
481 if (ret)
482 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483
Nick Dyere57a66a2014-05-18 23:13:40 -0700484 if (state == MXT_WAITING_BOOTLOAD_CMD)
485 val = mxt_get_bootloader_version(data, val);
486
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700487 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800488 case MXT_WAITING_BOOTLOAD_CMD:
489 case MXT_WAITING_FRAME_DATA:
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700490 case MXT_APP_CRC_FAIL:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800491 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700492 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800493 case MXT_FRAME_CRC_PASS:
Nick Dyerf943c742014-05-18 23:14:20 -0700494 if (val == MXT_FRAME_CRC_CHECK) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495 goto recheck;
Nick Dyerf943c742014-05-18 23:14:20 -0700496 } else if (val == MXT_FRAME_CRC_FAIL) {
497 dev_err(dev, "Bootloader CRC fail\n");
498 return -EINVAL;
499 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700500 break;
501 default:
502 return -EINVAL;
503 }
504
505 if (val != state) {
Nick Dyerf28a8422014-05-18 23:10:49 -0700506 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
Nick Dyer7bed6802014-05-18 23:04:09 -0700507 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700508 return -EINVAL;
509 }
510
511 return 0;
512}
513
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700514static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700515{
Nick Dyerf28a8422014-05-18 23:10:49 -0700516 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700517 u8 buf[2];
518
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700519 if (unlock) {
520 buf[0] = MXT_UNLOCK_CMD_LSB;
521 buf[1] = MXT_UNLOCK_CMD_MSB;
522 } else {
523 buf[0] = 0x01;
524 buf[1] = 0x01;
525 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700526
Nick Dyerf28a8422014-05-18 23:10:49 -0700527 ret = mxt_bootloader_write(data, buf, 2);
528 if (ret)
529 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530
531 return 0;
532}
533
Iiro Valkonen7686b102011-02-02 23:21:58 -0800534static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700535 u16 reg, u16 len, void *val)
536{
537 struct i2c_msg xfer[2];
538 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800539 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700540
541 buf[0] = reg & 0xff;
542 buf[1] = (reg >> 8) & 0xff;
543
544 /* Write register */
545 xfer[0].addr = client->addr;
546 xfer[0].flags = 0;
547 xfer[0].len = 2;
548 xfer[0].buf = buf;
549
550 /* Read data */
551 xfer[1].addr = client->addr;
552 xfer[1].flags = I2C_M_RD;
553 xfer[1].len = len;
554 xfer[1].buf = val;
555
Daniel Kurtz771733e2012-06-28 21:08:11 +0800556 ret = i2c_transfer(client->adapter, xfer, 2);
557 if (ret == 2) {
558 ret = 0;
559 } else {
560 if (ret >= 0)
561 ret = -EIO;
562 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
563 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700564 }
565
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, u16 len,
570 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700571{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800572 u8 *buf;
573 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800574 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700575
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800576 count = len + 2;
577 buf = kmalloc(count, GFP_KERNEL);
578 if (!buf)
579 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700580
581 buf[0] = reg & 0xff;
582 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800583 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700584
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800585 ret = i2c_master_send(client, buf, count);
586 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800587 ret = 0;
588 } else {
589 if (ret >= 0)
590 ret = -EIO;
591 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
592 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700593 }
594
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800595 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800596 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597}
598
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800599static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700600{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800601 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700602}
603
Iiro Valkonen7686b102011-02-02 23:21:58 -0800604static struct mxt_object *
605mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800607 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700608 int i;
609
610 for (i = 0; i < data->info.object_num; i++) {
611 object = data->object_table + i;
612 if (object->type == type)
613 return object;
614 }
615
Nick Dyer50a77c62014-07-23 12:38:48 -0700616 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700617 return NULL;
618}
619
Nick Dyer497767d2014-07-23 12:47:50 -0700620static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
621{
622 struct device *dev = &data->client->dev;
623 u8 status = msg[1];
624 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
625
626 complete(&data->crc_completion);
627
628 if (crc != data->config_crc) {
629 data->config_crc = crc;
630 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
631 }
632
633 /* Detect reset */
634 if (status & MXT_T6_STATUS_RESET)
635 complete(&data->reset_completion);
636
637 /* Output debug if status has changed */
638 if (status != data->t6_status)
639 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
640 status,
641 status == 0 ? " OK" : "",
642 status & MXT_T6_STATUS_RESET ? " RESET" : "",
643 status & MXT_T6_STATUS_OFL ? " OFL" : "",
644 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
645 status & MXT_T6_STATUS_CAL ? " CAL" : "",
646 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
647 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
648
649 /* Save current status */
650 data->t6_status = status;
651}
652
Linus Torvalds7f405482014-12-31 12:59:34 -0800653static int mxt_write_object(struct mxt_data *data,
654 u8 type, u8 offset, u8 val)
655{
656 struct mxt_object *object;
657 u16 reg;
658
659 object = mxt_get_object(data, type);
660 if (!object || offset >= mxt_obj_size(object))
661 return -EINVAL;
662
663 reg = object->start_address;
664 return mxt_write_reg(data->client, reg + offset, val);
665}
666
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700667static void mxt_input_button(struct mxt_data *data, u8 *message)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800668{
669 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700670 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800671 int i;
672
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700673 for (i = 0; i < pdata->t19_num_keys; i++) {
674 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800675 continue;
Dmitry Torokhovc37f6d32015-04-15 10:31:10 -0700676
677 /* Active-low switch */
678 input_report_key(input, pdata->t19_keymap[i],
679 !(message[1] & BIT(i)));
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800680 }
681}
682
Benson Leungb735fbe2014-07-23 12:22:27 -0700683static void mxt_input_sync(struct mxt_data *data)
Nick Dyereef820d2014-05-18 23:22:22 -0700684{
Benson Leungb735fbe2014-07-23 12:22:27 -0700685 input_mt_report_pointer_emulation(data->input_dev,
686 data->pdata->t19_num_keys);
687 input_sync(data->input_dev);
Nick Dyereef820d2014-05-18 23:22:22 -0700688}
689
Nick Dyerb9b05a82014-07-23 12:48:13 -0700690static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692 struct device *dev = &data->client->dev;
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800693 struct input_dev *input_dev = data->input_dev;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700694 int id;
695 u8 status;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696 int x;
697 int y;
698 int area;
Nick Dyerfea9e462014-05-18 23:21:48 -0700699 int amplitude;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700701 id = message[0] - data->T9_reportid_min;
702 status = message[1];
703 x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
704 y = (message[3] << 4) | ((message[4] & 0xf));
Nick Dyereef820d2014-05-18 23:22:22 -0700705
706 /* Handle 10/12 bit switching */
Joonyoung Shim910d8052011-04-12 23:14:38 -0700707 if (data->max_x < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700708 x >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700709 if (data->max_y < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700710 y >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700711
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700712 area = message[5];
713 amplitude = message[6];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800715 dev_dbg(dev,
716 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
717 id,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700718 (status & MXT_T9_DETECT) ? 'D' : '.',
719 (status & MXT_T9_PRESS) ? 'P' : '.',
720 (status & MXT_T9_RELEASE) ? 'R' : '.',
721 (status & MXT_T9_MOVE) ? 'M' : '.',
722 (status & MXT_T9_VECTOR) ? 'V' : '.',
723 (status & MXT_T9_AMP) ? 'A' : '.',
724 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
725 (status & MXT_T9_UNGRIP) ? 'U' : '.',
Nick Dyerfea9e462014-05-18 23:21:48 -0700726 x, y, area, amplitude);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800728 input_mt_slot(input_dev, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700729
Nick Dyerf3889ed2014-05-18 23:22:04 -0700730 if (status & MXT_T9_DETECT) {
Nick Dyereef820d2014-05-18 23:22:22 -0700731 /*
732 * Multiple bits may be set if the host is slow to read
733 * the status messages, indicating all the events that
734 * have happened.
735 */
736 if (status & MXT_T9_RELEASE) {
737 input_mt_report_slot_state(input_dev,
738 MT_TOOL_FINGER, 0);
Benson Leungb735fbe2014-07-23 12:22:27 -0700739 mxt_input_sync(data);
Nick Dyereef820d2014-05-18 23:22:22 -0700740 }
741
742 /* Touch active */
743 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800744 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
745 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
Nick Dyerfea9e462014-05-18 23:21:48 -0700746 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800747 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
Nick Dyereef820d2014-05-18 23:22:22 -0700748 } else {
749 /* Touch no longer active, close out slot */
750 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800751 }
Nick Dyerb9b05a82014-07-23 12:48:13 -0700752
753 data->update_input = true;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700754}
755
Nick Dyerb23157d2015-04-06 11:25:13 -0700756static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
757{
758 struct device *dev = &data->client->dev;
759 struct input_dev *input_dev = data->input_dev;
760 int id;
761 u8 status;
762 u8 type = 0;
763 u16 x;
764 u16 y;
765 int distance = 0;
766 int tool = 0;
767 u8 major = 0;
768 u8 pressure = 0;
769 u8 orientation = 0;
770
771 id = message[0] - data->T100_reportid_min - 2;
772
773 /* ignore SCRSTATUS events */
774 if (id < 0)
775 return;
776
777 status = message[1];
778 x = get_unaligned_le16(&message[2]);
779 y = get_unaligned_le16(&message[4]);
780
781 if (status & MXT_T100_DETECT) {
782 type = (status & MXT_T100_TYPE_MASK) >> 4;
783
784 switch (type) {
785 case MXT_T100_TYPE_HOVERING_FINGER:
786 tool = MT_TOOL_FINGER;
787 distance = MXT_DISTANCE_HOVERING;
788
789 if (data->t100_aux_vect)
790 orientation = message[data->t100_aux_vect];
791
792 break;
793
794 case MXT_T100_TYPE_FINGER:
795 case MXT_T100_TYPE_GLOVE:
796 tool = MT_TOOL_FINGER;
797 distance = MXT_DISTANCE_ACTIVE_TOUCH;
798
799 if (data->t100_aux_area)
800 major = message[data->t100_aux_area];
801
802 if (data->t100_aux_ampl)
803 pressure = message[data->t100_aux_ampl];
804
805 if (data->t100_aux_vect)
806 orientation = message[data->t100_aux_vect];
807
808 break;
809
810 case MXT_T100_TYPE_PASSIVE_STYLUS:
811 tool = MT_TOOL_PEN;
812
813 /*
814 * Passive stylus is reported with size zero so
815 * hardcode.
816 */
817 major = MXT_TOUCH_MAJOR_DEFAULT;
818
819 if (data->t100_aux_ampl)
820 pressure = message[data->t100_aux_ampl];
821
822 break;
823
824 case MXT_T100_TYPE_LARGE_TOUCH:
825 /* Ignore suppressed touch */
826 break;
827
828 default:
829 dev_dbg(dev, "Unexpected T100 type\n");
830 return;
831 }
832 }
833
834 /*
835 * Values reported should be non-zero if tool is touching the
836 * device
837 */
838 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
839 pressure = MXT_PRESSURE_DEFAULT;
840
841 input_mt_slot(input_dev, id);
842
843 if (status & MXT_T100_DETECT) {
844 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
845 id, type, x, y, major, pressure, orientation);
846
847 input_mt_report_slot_state(input_dev, tool, 1);
848 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
849 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
850 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
851 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
852 input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
853 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
854 } else {
855 dev_dbg(dev, "[%u] release\n", id);
856
857 /* close out slot */
858 input_mt_report_slot_state(input_dev, 0, 0);
859 }
860
861 data->update_input = true;
862}
863
Nick Dyerb9b05a82014-07-23 12:48:13 -0700864static int mxt_proc_message(struct mxt_data *data, u8 *message)
Daniel Kurtz04a79182012-06-28 21:08:21 +0800865{
Nick Dyerb9b05a82014-07-23 12:48:13 -0700866 u8 report_id = message[0];
867
868 if (report_id == MXT_RPTID_NOMSG)
869 return 0;
870
871 if (report_id == data->T6_reportid) {
872 mxt_proc_t6_messages(data, message);
873 } else if (!data->input_dev) {
874 /*
875 * Do not report events if input device
876 * is not yet registered.
877 */
878 mxt_dump_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700879 } else if (report_id >= data->T9_reportid_min &&
880 report_id <= data->T9_reportid_max) {
Nick Dyerb9b05a82014-07-23 12:48:13 -0700881 mxt_proc_t9_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700882 } else if (report_id >= data->T100_reportid_min &&
883 report_id <= data->T100_reportid_max) {
884 mxt_proc_t100_message(data, message);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700885 } else if (report_id == data->T19_reportid) {
886 mxt_input_button(data, message);
887 data->update_input = true;
888 } else {
889 mxt_dump_message(data, message);
890 }
891
892 return 1;
893}
894
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700895static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
Nick Dyerb9b05a82014-07-23 12:48:13 -0700896{
897 struct device *dev = &data->client->dev;
898 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700899 int i;
900 u8 num_valid = 0;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700901
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700902 /* Safety check for msg_buf */
903 if (count > data->max_reportid)
904 return -EINVAL;
905
906 /* Process remaining messages if necessary */
Nick Dyerb9b05a82014-07-23 12:48:13 -0700907 ret = __mxt_read_reg(data->client, data->T5_address,
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700908 data->T5_msg_size * count, data->msg_buf);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700909 if (ret) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700910 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700911 return ret;
912 }
913
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700914 for (i = 0; i < count; i++) {
915 ret = mxt_proc_message(data,
916 data->msg_buf + data->T5_msg_size * i);
917
918 if (ret == 1)
919 num_valid++;
920 }
921
922 /* return number of messages read */
923 return num_valid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700924}
925
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700926static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700927{
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700928 struct device *dev = &data->client->dev;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700929 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700930 u8 count, num_left;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700931
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700932 /* Read T44 and T5 together */
933 ret = __mxt_read_reg(data->client, data->T44_address,
934 data->T5_msg_size + 1, data->msg_buf);
935 if (ret) {
936 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
937 return IRQ_NONE;
938 }
939
940 count = data->msg_buf[0];
941
Nick Dyer507584e2015-08-04 16:58:05 -0700942 /*
943 * This condition may be caused by the CHG line being configured in
944 * Mode 0. It results in unnecessary I2C operations but it is benign.
945 */
946 if (count == 0)
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700947 return IRQ_NONE;
Nick Dyer507584e2015-08-04 16:58:05 -0700948
949 if (count > data->max_reportid) {
950 dev_warn(dev, "T44 count %d exceeded max report id\n", count);
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700951 count = data->max_reportid;
952 }
953
954 /* Process first message */
955 ret = mxt_proc_message(data, data->msg_buf + 1);
956 if (ret < 0) {
957 dev_warn(dev, "Unexpected invalid message\n");
958 return IRQ_NONE;
959 }
960
961 num_left = count - 1;
962
963 /* Process remaining messages if necessary */
964 if (num_left) {
965 ret = mxt_read_and_process_messages(data, num_left);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700966 if (ret < 0)
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700967 goto end;
968 else if (ret != num_left)
969 dev_warn(dev, "Unexpected invalid message\n");
970 }
971
972end:
973 if (data->update_input) {
974 mxt_input_sync(data);
975 data->update_input = false;
976 }
977
978 return IRQ_HANDLED;
979}
980
981static int mxt_process_messages_until_invalid(struct mxt_data *data)
982{
983 struct device *dev = &data->client->dev;
984 int count, read;
985 u8 tries = 2;
986
987 count = data->max_reportid;
988
989 /* Read messages until we force an invalid */
990 do {
991 read = mxt_read_and_process_messages(data, count);
992 if (read < count)
993 return 0;
994 } while (--tries);
995
996 if (data->update_input) {
997 mxt_input_sync(data);
998 data->update_input = false;
999 }
1000
1001 dev_err(dev, "CHG pin isn't cleared\n");
1002 return -EBUSY;
1003}
1004
1005static irqreturn_t mxt_process_messages(struct mxt_data *data)
1006{
1007 int total_handled, num_handled;
1008 u8 count = data->last_message_count;
1009
1010 if (count < 1 || count > data->max_reportid)
1011 count = 1;
1012
1013 /* include final invalid message */
1014 total_handled = mxt_read_and_process_messages(data, count + 1);
1015 if (total_handled < 0)
1016 return IRQ_NONE;
1017 /* if there were invalid messages, then we are done */
1018 else if (total_handled <= count)
1019 goto update_count;
1020
1021 /* keep reading two msgs until one is invalid or reportid limit */
1022 do {
1023 num_handled = mxt_read_and_process_messages(data, 2);
1024 if (num_handled < 0)
Nick Dyer8d4e1632014-05-18 23:00:56 -07001025 return IRQ_NONE;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001026
1027 total_handled += num_handled;
1028
1029 if (num_handled < 2)
1030 break;
1031 } while (total_handled < data->num_touchids);
1032
1033update_count:
1034 data->last_message_count = total_handled;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001035
Nick Dyerb9b05a82014-07-23 12:48:13 -07001036 if (data->update_input) {
Benson Leungb735fbe2014-07-23 12:22:27 -07001037 mxt_input_sync(data);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001038 data->update_input = false;
1039 }
Daniel Kurtz64464ae2012-06-28 21:08:23 +08001040
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001041 return IRQ_HANDLED;
1042}
1043
Benson Leungd79e7e42014-05-18 23:02:52 -07001044static irqreturn_t mxt_interrupt(int irq, void *dev_id)
1045{
1046 struct mxt_data *data = dev_id;
1047
1048 if (data->in_bootloader) {
1049 /* bootloader state transition completion */
1050 complete(&data->bl_completion);
1051 return IRQ_HANDLED;
1052 }
1053
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001054 if (!data->object_table)
1055 return IRQ_HANDLED;
1056
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001057 if (data->T44_address) {
1058 return mxt_process_messages_t44(data);
1059 } else {
1060 return mxt_process_messages(data);
1061 }
Benson Leungd79e7e42014-05-18 23:02:52 -07001062}
1063
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001064static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
1065 u8 value, bool wait)
1066{
1067 u16 reg;
1068 u8 command_register;
1069 int timeout_counter = 0;
1070 int ret;
1071
1072 reg = data->T6_address + cmd_offset;
1073
1074 ret = mxt_write_reg(data->client, reg, value);
1075 if (ret)
1076 return ret;
1077
1078 if (!wait)
1079 return 0;
1080
1081 do {
1082 msleep(20);
1083 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
1084 if (ret)
1085 return ret;
1086 } while (command_register != 0 && timeout_counter++ <= 100);
1087
1088 if (timeout_counter > 100) {
1089 dev_err(&data->client->dev, "Command failed!\n");
1090 return -EIO;
1091 }
1092
1093 return 0;
1094}
1095
Nick Dyereb433352016-04-25 14:18:52 -07001096static int mxt_acquire_irq(struct mxt_data *data)
1097{
1098 int error;
1099
1100 enable_irq(data->irq);
1101
1102 error = mxt_process_messages_until_invalid(data);
1103 if (error)
1104 return error;
1105
1106 return 0;
1107}
1108
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001109static int mxt_soft_reset(struct mxt_data *data)
1110{
1111 struct device *dev = &data->client->dev;
1112 int ret = 0;
1113
Nick Dyer885f3fb2015-08-04 16:57:25 -07001114 dev_info(dev, "Resetting device\n");
1115
1116 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001117
1118 reinit_completion(&data->reset_completion);
1119
1120 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
1121 if (ret)
1122 return ret;
1123
Nick Dyer885f3fb2015-08-04 16:57:25 -07001124 /* Ignore CHG line for 100ms after reset */
1125 msleep(100);
1126
Nick Dyereb433352016-04-25 14:18:52 -07001127 mxt_acquire_irq(data);
Nick Dyer885f3fb2015-08-04 16:57:25 -07001128
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001129 ret = mxt_wait_for_completion(data, &data->reset_completion,
1130 MXT_RESET_TIMEOUT);
1131 if (ret)
1132 return ret;
1133
1134 return 0;
1135}
1136
Nick Dyerc3f78042014-05-18 23:04:46 -07001137static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
1138{
1139 /*
1140 * On failure, CRC is set to 0 and config will always be
1141 * downloaded.
1142 */
1143 data->config_crc = 0;
1144 reinit_completion(&data->crc_completion);
1145
1146 mxt_t6_command(data, cmd, value, true);
1147
1148 /*
1149 * Wait for crc message. On failure, CRC is set to 0 and config will
1150 * always be downloaded.
1151 */
1152 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
1153}
1154
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001155static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
1156{
1157 static const unsigned int crcpoly = 0x80001B;
1158 u32 result;
1159 u32 data_word;
1160
1161 data_word = (secondbyte << 8) | firstbyte;
1162 result = ((*crc << 1) ^ data_word);
1163
1164 if (result & 0x1000000)
1165 result ^= crcpoly;
1166
1167 *crc = result;
1168}
1169
1170static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
1171{
1172 u32 crc = 0;
1173 u8 *ptr = base + start_off;
1174 u8 *last_val = base + end_off - 1;
1175
1176 if (end_off < start_off)
1177 return -EINVAL;
1178
1179 while (ptr < last_val) {
1180 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
1181 ptr += 2;
1182 }
1183
1184 /* if len is odd, fill the last byte with 0 */
1185 if (ptr == last_val)
1186 mxt_calc_crc24(&crc, *ptr, 0);
1187
1188 /* Mask to 24-bit */
1189 crc &= 0x00FFFFFF;
1190
1191 return crc;
1192}
1193
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001194static int mxt_prepare_cfg_mem(struct mxt_data *data,
1195 const struct firmware *cfg,
1196 unsigned int data_pos,
1197 unsigned int cfg_start_ofs,
1198 u8 *config_mem,
1199 size_t config_mem_size)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001200{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201 struct device *dev = &data->client->dev;
Nick Dyer50a77c62014-07-23 12:38:48 -07001202 struct mxt_object *object;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001203 unsigned int type, instance, size, byte_offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001204 int offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001205 int ret;
Nick Dyer50a77c62014-07-23 12:38:48 -07001206 int i;
Nick Dyer50a77c62014-07-23 12:38:48 -07001207 u16 reg;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001208 u8 val;
Nick Dyer50a77c62014-07-23 12:38:48 -07001209
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001210 while (data_pos < cfg->size) {
Nick Dyer50a77c62014-07-23 12:38:48 -07001211 /* Read type, instance, length */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001212 ret = sscanf(cfg->data + data_pos, "%x %x %x%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001213 &type, &instance, &size, &offset);
1214 if (ret == 0) {
1215 /* EOF */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001216 break;
Nick Dyer50a77c62014-07-23 12:38:48 -07001217 } else if (ret != 3) {
1218 dev_err(dev, "Bad format: failed to parse object\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001219 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001220 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001221 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001222
1223 object = mxt_get_object(data, type);
1224 if (!object) {
1225 /* Skip object */
1226 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001227 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001228 &val, &offset);
1229 if (ret != 1) {
1230 dev_err(dev, "Bad format in T%d at %d\n",
1231 type, i);
1232 return -EINVAL;
1233 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001234 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001235 }
1236 continue;
1237 }
1238
1239 if (size > mxt_obj_size(object)) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001240 /*
1241 * Either we are in fallback mode due to wrong
1242 * config or config from a later fw version,
1243 * or the file is corrupt or hand-edited.
1244 */
1245 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1246 size - mxt_obj_size(object), type);
1247 } else if (mxt_obj_size(object) > size) {
1248 /*
1249 * If firmware is upgraded, new bytes may be added to
1250 * end of objects. It is generally forward compatible
1251 * to zero these bytes - previous behaviour will be
1252 * retained. However this does invalidate the CRC and
1253 * will force fallback mode until the configuration is
1254 * updated. We warn here but do nothing else - the
1255 * malloc has zeroed the entire configuration.
1256 */
1257 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1258 mxt_obj_size(object) - size, type);
Nick Dyer50a77c62014-07-23 12:38:48 -07001259 }
1260
1261 if (instance >= mxt_obj_instances(object)) {
1262 dev_err(dev, "Object instances exceeded!\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001263 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001264 }
1265
1266 reg = object->start_address + mxt_obj_size(object) * instance;
1267
1268 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001269 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001270 &val,
1271 &offset);
1272 if (ret != 1) {
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001273 dev_err(dev, "Bad format in T%d at %d\n",
1274 type, i);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001275 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001276 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001277 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001278
1279 if (i > mxt_obj_size(object))
1280 continue;
1281
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001282 byte_offset = reg + i - cfg_start_ofs;
Nick Dyer50a77c62014-07-23 12:38:48 -07001283
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001284 if (byte_offset >= 0 && byte_offset < config_mem_size) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001285 *(config_mem + byte_offset) = val;
1286 } else {
1287 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1288 reg, object->type, byte_offset);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001289 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001290 }
1291 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001292 }
1293
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001294 return 0;
1295}
1296
1297static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start,
1298 u8 *config_mem, size_t config_mem_size)
1299{
1300 unsigned int byte_offset = 0;
1301 int error;
1302
1303 /* Write configuration as blocks */
1304 while (byte_offset < config_mem_size) {
1305 unsigned int size = config_mem_size - byte_offset;
1306
1307 if (size > MXT_MAX_BLOCK_WRITE)
1308 size = MXT_MAX_BLOCK_WRITE;
1309
1310 error = __mxt_write_reg(data->client,
1311 cfg_start + byte_offset,
1312 size, config_mem + byte_offset);
1313 if (error) {
1314 dev_err(&data->client->dev,
1315 "Config write error, ret=%d\n", error);
1316 return error;
1317 }
1318
1319 byte_offset += size;
1320 }
1321
1322 return 0;
1323}
1324
Nick Dyer7f3884f72015-08-04 16:36:29 -07001325static int mxt_init_t7_power_cfg(struct mxt_data *data);
1326
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001327/*
1328 * mxt_update_cfg - download configuration to chip
1329 *
1330 * Atmel Raw Config File Format
1331 *
1332 * The first four lines of the raw config file contain:
1333 * 1) Version
1334 * 2) Chip ID Information (first 7 bytes of device memory)
1335 * 3) Chip Information Block 24-bit CRC Checksum
1336 * 4) Chip Configuration 24-bit CRC Checksum
1337 *
1338 * The rest of the file consists of one line per object instance:
1339 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1340 *
1341 * <TYPE> - 2-byte object type as hex
1342 * <INSTANCE> - 2-byte object instance number as hex
1343 * <SIZE> - 2-byte object size as hex
1344 * <CONTENTS> - array of <SIZE> 1-byte hex values
1345 */
1346static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg)
1347{
1348 struct device *dev = &data->client->dev;
1349 struct mxt_info cfg_info;
1350 int ret;
1351 int offset;
1352 int data_pos;
1353 int i;
1354 int cfg_start_ofs;
1355 u32 info_crc, config_crc, calculated_crc;
1356 u8 *config_mem;
1357 size_t config_mem_size;
1358
1359 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
1360
1361 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
1362 dev_err(dev, "Unrecognised config file\n");
1363 return -EINVAL;
1364 }
1365
1366 data_pos = strlen(MXT_CFG_MAGIC);
1367
1368 /* Load information block and check */
1369 for (i = 0; i < sizeof(struct mxt_info); i++) {
1370 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1371 (unsigned char *)&cfg_info + i,
1372 &offset);
1373 if (ret != 1) {
1374 dev_err(dev, "Bad format\n");
1375 return -EINVAL;
1376 }
1377
1378 data_pos += offset;
1379 }
1380
1381 if (cfg_info.family_id != data->info.family_id) {
1382 dev_err(dev, "Family ID mismatch!\n");
1383 return -EINVAL;
1384 }
1385
1386 if (cfg_info.variant_id != data->info.variant_id) {
1387 dev_err(dev, "Variant ID mismatch!\n");
1388 return -EINVAL;
1389 }
1390
1391 /* Read CRCs */
1392 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset);
1393 if (ret != 1) {
1394 dev_err(dev, "Bad format: failed to parse Info CRC\n");
1395 return -EINVAL;
1396 }
1397 data_pos += offset;
1398
1399 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset);
1400 if (ret != 1) {
1401 dev_err(dev, "Bad format: failed to parse Config CRC\n");
1402 return -EINVAL;
1403 }
1404 data_pos += offset;
1405
1406 /*
1407 * The Info Block CRC is calculated over mxt_info and the object
1408 * table. If it does not match then we are trying to load the
1409 * configuration from a different chip or firmware version, so
1410 * the configuration CRC is invalid anyway.
1411 */
1412 if (info_crc == data->info_crc) {
1413 if (config_crc == 0 || data->config_crc == 0) {
1414 dev_info(dev, "CRC zero, attempting to apply config\n");
1415 } else if (config_crc == data->config_crc) {
1416 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
1417 data->config_crc);
1418 return 0;
1419 } else {
1420 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1421 data->config_crc, config_crc);
1422 }
1423 } else {
1424 dev_warn(dev,
1425 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1426 data->info_crc, info_crc);
1427 }
1428
1429 /* Malloc memory to store configuration */
1430 cfg_start_ofs = MXT_OBJECT_START +
1431 data->info.object_num * sizeof(struct mxt_object) +
1432 MXT_INFO_CHECKSUM_SIZE;
1433 config_mem_size = data->mem_size - cfg_start_ofs;
1434 config_mem = kzalloc(config_mem_size, GFP_KERNEL);
1435 if (!config_mem) {
1436 dev_err(dev, "Failed to allocate memory\n");
1437 return -ENOMEM;
1438 }
1439
1440 ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs,
1441 config_mem, config_mem_size);
1442 if (ret)
1443 goto release_mem;
1444
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001445 /* Calculate crc of the received configs (not the raw config file) */
1446 if (data->T7_address < cfg_start_ofs) {
1447 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n",
1448 data->T7_address, cfg_start_ofs);
1449 ret = 0;
1450 goto release_mem;
1451 }
1452
1453 calculated_crc = mxt_calculate_crc(config_mem,
1454 data->T7_address - cfg_start_ofs,
1455 config_mem_size);
1456
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001457 if (config_crc > 0 && config_crc != calculated_crc)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001458 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n",
1459 calculated_crc, config_crc);
1460
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001461 ret = mxt_upload_cfg_mem(data, cfg_start_ofs,
1462 config_mem, config_mem_size);
1463 if (ret)
1464 goto release_mem;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001465
Nick Dyerc3f78042014-05-18 23:04:46 -07001466 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1467
1468 ret = mxt_soft_reset(data);
1469 if (ret)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001470 goto release_mem;
Nick Dyerc3f78042014-05-18 23:04:46 -07001471
1472 dev_info(dev, "Config successfully updated\n");
1473
Nick Dyer7f3884f72015-08-04 16:36:29 -07001474 /* T7 config may have changed */
1475 mxt_init_t7_power_cfg(data);
1476
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001477release_mem:
1478 kfree(config_mem);
Nick Dyer50a77c62014-07-23 12:38:48 -07001479 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001480}
1481
Iiro Valkonen7686b102011-02-02 23:21:58 -08001482static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001483{
1484 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001485 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001486 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001487
Daniel Kurtz23003a842012-06-28 21:08:14 +08001488 /* Read 7-byte info block starting at address 0 */
Nick Dyer146c6a62015-08-04 16:49:34 -07001489 error = __mxt_read_reg(client, 0, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001490 if (error)
1491 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001492
1493 return 0;
1494}
1495
Stephen Warren58e4aeee2014-09-10 10:01:10 -07001496static void mxt_free_input_device(struct mxt_data *data)
1497{
1498 if (data->input_dev) {
1499 input_unregister_device(data->input_dev);
1500 data->input_dev = NULL;
1501 }
1502}
1503
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001504static void mxt_free_object_table(struct mxt_data *data)
1505{
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001506 kfree(data->object_table);
1507 data->object_table = NULL;
1508 kfree(data->msg_buf);
1509 data->msg_buf = NULL;
Nick Dyerb9b05a82014-07-23 12:48:13 -07001510 data->T5_address = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001511 data->T5_msg_size = 0;
1512 data->T6_reportid = 0;
1513 data->T7_address = 0;
1514 data->T9_reportid_min = 0;
1515 data->T9_reportid_max = 0;
1516 data->T19_reportid = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001517 data->T44_address = 0;
Nick Dyerb23157d2015-04-06 11:25:13 -07001518 data->T100_reportid_min = 0;
1519 data->T100_reportid_max = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001520 data->max_reportid = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001521}
1522
Iiro Valkonen7686b102011-02-02 23:21:58 -08001523static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001524{
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001525 struct i2c_client *client = data->client;
1526 size_t table_size;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001527 struct mxt_object *object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001528 int error;
1529 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001530 u8 reportid;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001531 u16 end_address;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001532
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001533 table_size = data->info.object_num * sizeof(struct mxt_object);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001534 object_table = kzalloc(table_size, GFP_KERNEL);
1535 if (!object_table) {
1536 dev_err(&data->client->dev, "Failed to allocate memory\n");
1537 return -ENOMEM;
1538 }
1539
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001540 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001541 object_table);
1542 if (error) {
1543 kfree(object_table);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001544 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001545 }
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001546
1547 /* Valid Report IDs start counting from 1 */
1548 reportid = 1;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001549 data->mem_size = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001550 for (i = 0; i < data->info.object_num; i++) {
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001551 struct mxt_object *object = object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001552 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001553
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001554 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001555
1556 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001557 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001558 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001559 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001560 max_id = reportid - 1;
1561 } else {
1562 min_id = 0;
1563 max_id = 0;
1564 }
1565
1566 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001567 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001568 object->type, object->start_address,
1569 mxt_obj_size(object), mxt_obj_instances(object),
1570 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001571
1572 switch (object->type) {
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001573 case MXT_GEN_MESSAGE_T5:
Nick Dyer437d4f32014-08-07 09:56:01 -07001574 if (data->info.family_id == 0x80 &&
1575 data->info.version < 0x20) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001576 /*
Nick Dyer437d4f32014-08-07 09:56:01 -07001577 * On mXT224 firmware versions prior to V2.0
1578 * read and discard unused CRC byte otherwise
1579 * DMA reads are misaligned.
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001580 */
1581 data->T5_msg_size = mxt_obj_size(object);
1582 } else {
1583 /* CRC not enabled, so skip last byte */
1584 data->T5_msg_size = mxt_obj_size(object) - 1;
1585 }
Nick Dyerb9b05a82014-07-23 12:48:13 -07001586 data->T5_address = object->start_address;
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001587 break;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001588 case MXT_GEN_COMMAND_T6:
1589 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001590 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001591 break;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001592 case MXT_GEN_POWER_T7:
1593 data->T7_address = object->start_address;
1594 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001595 case MXT_TOUCH_MULTI_T9:
Nick Dyerb23157d2015-04-06 11:25:13 -07001596 data->multitouch = MXT_TOUCH_MULTI_T9;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001597 data->T9_reportid_min = min_id;
1598 data->T9_reportid_max = max_id;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001599 data->num_touchids = object->num_report_ids
1600 * mxt_obj_instances(object);
1601 break;
1602 case MXT_SPT_MESSAGECOUNT_T44:
1603 data->T44_address = object->start_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001604 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001605 case MXT_SPT_GPIOPWM_T19:
1606 data->T19_reportid = min_id;
1607 break;
Nick Dyerb23157d2015-04-06 11:25:13 -07001608 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1609 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
1610 data->T100_reportid_min = min_id;
1611 data->T100_reportid_max = max_id;
1612 /* first two report IDs reserved */
1613 data->num_touchids = object->num_report_ids - 2;
1614 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001615 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001616
1617 end_address = object->start_address
1618 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1619
1620 if (end_address >= data->mem_size)
1621 data->mem_size = end_address + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001622 }
1623
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001624 /* Store maximum reportid */
1625 data->max_reportid = reportid;
1626
1627 /* If T44 exists, T5 position has to be directly after */
1628 if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
1629 dev_err(&client->dev, "Invalid T44 position\n");
1630 error = -EINVAL;
1631 goto free_object_table;
1632 }
1633
1634 data->msg_buf = kcalloc(data->max_reportid,
1635 data->T5_msg_size, GFP_KERNEL);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001636 if (!data->msg_buf) {
1637 dev_err(&client->dev, "Failed to allocate message buffer\n");
1638 error = -ENOMEM;
1639 goto free_object_table;
1640 }
1641
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001642 data->object_table = object_table;
1643
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001644 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001645
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001646free_object_table:
1647 mxt_free_object_table(data);
1648 return error;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001649}
1650
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001651static int mxt_read_t9_resolution(struct mxt_data *data)
1652{
1653 struct i2c_client *client = data->client;
1654 int error;
1655 struct t9_range range;
1656 unsigned char orient;
1657 struct mxt_object *object;
1658
1659 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1660 if (!object)
1661 return -EINVAL;
1662
1663 error = __mxt_read_reg(client,
1664 object->start_address + MXT_T9_RANGE,
1665 sizeof(range), &range);
1666 if (error)
1667 return error;
1668
Nick Dyer1c0276d2016-01-10 18:29:34 -08001669 data->max_x = get_unaligned_le16(&range.x);
1670 data->max_y = get_unaligned_le16(&range.y);
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001671
1672 error = __mxt_read_reg(client,
1673 object->start_address + MXT_T9_ORIENT,
1674 1, &orient);
1675 if (error)
1676 return error;
1677
Nick Dyer1c0276d2016-01-10 18:29:34 -08001678 data->xy_switch = orient & MXT_T9_ORIENT_SWITCH;
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001679
1680 return 0;
1681}
1682
Nick Dyerb23157d2015-04-06 11:25:13 -07001683static int mxt_read_t100_config(struct mxt_data *data)
1684{
1685 struct i2c_client *client = data->client;
1686 int error;
1687 struct mxt_object *object;
1688 u16 range_x, range_y;
1689 u8 cfg, tchaux;
1690 u8 aux;
1691
1692 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
1693 if (!object)
1694 return -EINVAL;
1695
Nick Dyer1c0276d2016-01-10 18:29:34 -08001696 /* read touchscreen dimensions */
Nick Dyerb23157d2015-04-06 11:25:13 -07001697 error = __mxt_read_reg(client,
1698 object->start_address + MXT_T100_XRANGE,
1699 sizeof(range_x), &range_x);
1700 if (error)
1701 return error;
1702
Nick Dyer1c0276d2016-01-10 18:29:34 -08001703 data->max_x = get_unaligned_le16(&range_x);
Nick Dyerb23157d2015-04-06 11:25:13 -07001704
1705 error = __mxt_read_reg(client,
1706 object->start_address + MXT_T100_YRANGE,
1707 sizeof(range_y), &range_y);
1708 if (error)
1709 return error;
1710
Nick Dyer1c0276d2016-01-10 18:29:34 -08001711 data->max_y = get_unaligned_le16(&range_y);
Nick Dyerb23157d2015-04-06 11:25:13 -07001712
Nick Dyer1c0276d2016-01-10 18:29:34 -08001713 /* read orientation config */
Nick Dyerb23157d2015-04-06 11:25:13 -07001714 error = __mxt_read_reg(client,
1715 object->start_address + MXT_T100_CFG1,
1716 1, &cfg);
1717 if (error)
1718 return error;
1719
Nick Dyer1c0276d2016-01-10 18:29:34 -08001720 data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY;
1721
1722 /* allocate aux bytes */
Nick Dyerb23157d2015-04-06 11:25:13 -07001723 error = __mxt_read_reg(client,
1724 object->start_address + MXT_T100_TCHAUX,
1725 1, &tchaux);
1726 if (error)
1727 return error;
1728
Nick Dyerb23157d2015-04-06 11:25:13 -07001729 aux = 6;
1730
1731 if (tchaux & MXT_T100_TCHAUX_VECT)
1732 data->t100_aux_vect = aux++;
1733
1734 if (tchaux & MXT_T100_TCHAUX_AMPL)
1735 data->t100_aux_ampl = aux++;
1736
1737 if (tchaux & MXT_T100_TCHAUX_AREA)
1738 data->t100_aux_area = aux++;
1739
1740 dev_dbg(&client->dev,
1741 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1742 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
1743
Nick Dyerb23157d2015-04-06 11:25:13 -07001744 return 0;
1745}
1746
Nick Dyer7a53d602014-07-23 12:21:26 -07001747static int mxt_input_open(struct input_dev *dev);
1748static void mxt_input_close(struct input_dev *dev);
1749
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001750static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
1751 struct mxt_data *data)
1752{
1753 const struct mxt_platform_data *pdata = data->pdata;
1754 int i;
1755
1756 input_dev->name = "Atmel maXTouch Touchpad";
1757
1758 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1759
1760 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1761 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1762 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1763 MXT_PIXELS_PER_MM);
1764 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1765 MXT_PIXELS_PER_MM);
1766
1767 for (i = 0; i < pdata->t19_num_keys; i++)
1768 if (pdata->t19_keymap[i] != KEY_RESERVED)
1769 input_set_capability(input_dev, EV_KEY,
1770 pdata->t19_keymap[i]);
1771}
1772
Nick Dyerb23157d2015-04-06 11:25:13 -07001773static int mxt_initialize_input_device(struct mxt_data *data)
Nick Dyer7a53d602014-07-23 12:21:26 -07001774{
Nick Dyer7a53d602014-07-23 12:21:26 -07001775 const struct mxt_platform_data *pdata = data->pdata;
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001776 struct device *dev = &data->client->dev;
Nick Dyer7a53d602014-07-23 12:21:26 -07001777 struct input_dev *input_dev;
1778 int error;
1779 unsigned int num_mt_slots;
1780 unsigned int mt_flags = 0;
Nick Dyer7a53d602014-07-23 12:21:26 -07001781
Nick Dyerb23157d2015-04-06 11:25:13 -07001782 switch (data->multitouch) {
1783 case MXT_TOUCH_MULTI_T9:
1784 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1785 error = mxt_read_t9_resolution(data);
1786 if (error)
1787 dev_warn(dev, "Failed to initialize T9 resolution\n");
1788 break;
1789
1790 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1791 num_mt_slots = data->num_touchids;
1792 error = mxt_read_t100_config(data);
1793 if (error)
1794 dev_warn(dev, "Failed to read T100 config\n");
1795 break;
1796
1797 default:
1798 dev_err(dev, "Invalid multitouch object\n");
1799 return -EINVAL;
1800 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001801
Nick Dyer1c0276d2016-01-10 18:29:34 -08001802 /* Handle default values and orientation switch */
1803 if (data->max_x == 0)
1804 data->max_x = 1023;
1805
1806 if (data->max_y == 0)
1807 data->max_y = 1023;
1808
1809 if (data->xy_switch)
1810 swap(data->max_x, data->max_y);
1811
1812 dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1813
1814 /* Register input device */
Nick Dyer7a53d602014-07-23 12:21:26 -07001815 input_dev = input_allocate_device();
1816 if (!input_dev) {
1817 dev_err(dev, "Failed to allocate memory\n");
1818 return -ENOMEM;
1819 }
1820
1821 input_dev->name = "Atmel maXTouch Touchscreen";
1822 input_dev->phys = data->phys;
1823 input_dev->id.bustype = BUS_I2C;
1824 input_dev->dev.parent = dev;
1825 input_dev->open = mxt_input_open;
1826 input_dev->close = mxt_input_close;
1827
Nick Dyerb23157d2015-04-06 11:25:13 -07001828 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
Nick Dyer7a53d602014-07-23 12:21:26 -07001829
Nick Dyer7a53d602014-07-23 12:21:26 -07001830 /* For single touch */
Nick Dyerb23157d2015-04-06 11:25:13 -07001831 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
1832 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
1833
1834 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1835 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1836 data->t100_aux_ampl)) {
1837 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
1838 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001839
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001840 /* If device has buttons we assume it is a touchpad */
1841 if (pdata->t19_num_keys) {
1842 mxt_set_up_as_touchpad(input_dev, data);
1843 mt_flags |= INPUT_MT_POINTER;
Nick Dyereafc0c82015-08-04 16:57:40 -07001844 } else {
1845 mt_flags |= INPUT_MT_DIRECT;
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001846 }
1847
Nick Dyer7a53d602014-07-23 12:21:26 -07001848 /* For multi touch */
Nick Dyer7a53d602014-07-23 12:21:26 -07001849 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
1850 if (error) {
1851 dev_err(dev, "Error %d initialising slots\n", error);
1852 goto err_free_mem;
1853 }
1854
Nick Dyerb23157d2015-04-06 11:25:13 -07001855 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
1856 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
1857 0, MT_TOOL_MAX, 0, 0);
1858 input_set_abs_params(input_dev, ABS_MT_DISTANCE,
1859 MXT_DISTANCE_ACTIVE_TOUCH,
1860 MXT_DISTANCE_HOVERING,
1861 0, 0);
1862 }
1863
Nick Dyer7a53d602014-07-23 12:21:26 -07001864 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1865 0, data->max_x, 0, 0);
1866 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1867 0, data->max_y, 0, 0);
Nick Dyerb23157d2015-04-06 11:25:13 -07001868
1869 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1870 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1871 data->t100_aux_area)) {
1872 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1873 0, MXT_MAX_AREA, 0, 0);
1874 }
1875
1876 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1877 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1878 data->t100_aux_ampl)) {
1879 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1880 0, 255, 0, 0);
1881 }
1882
1883 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1884 data->t100_aux_vect) {
1885 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
1886 0, 255, 0, 0);
1887 }
1888
1889 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1890 data->t100_aux_ampl) {
1891 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1892 0, 255, 0, 0);
1893 }
1894
1895 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1896 data->t100_aux_vect) {
1897 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
1898 0, 255, 0, 0);
1899 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001900
1901 input_set_drvdata(input_dev, data);
1902
1903 error = input_register_device(input_dev);
1904 if (error) {
1905 dev_err(dev, "Error %d registering input device\n", error);
1906 goto err_free_mem;
1907 }
1908
1909 data->input_dev = input_dev;
1910
1911 return 0;
1912
1913err_free_mem:
1914 input_free_device(input_dev);
1915 return error;
1916}
1917
Nick Dyer50a77c62014-07-23 12:38:48 -07001918static int mxt_configure_objects(struct mxt_data *data,
1919 const struct firmware *cfg);
1920
1921static void mxt_config_cb(const struct firmware *cfg, void *ctx)
1922{
1923 mxt_configure_objects(ctx, cfg);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001924 release_firmware(cfg);
Nick Dyer50a77c62014-07-23 12:38:48 -07001925}
1926
Iiro Valkonen7686b102011-02-02 23:21:58 -08001927static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001928{
1929 struct i2c_client *client = data->client;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07001930 int recovery_attempts = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001931 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001932
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07001933 while (1) {
1934 error = mxt_get_info(data);
1935 if (!error)
1936 break;
1937
1938 /* Check bootloader state */
1939 error = mxt_probe_bootloader(data, false);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07001940 if (error) {
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07001941 dev_info(&client->dev, "Trying alternate bootloader address\n");
1942 error = mxt_probe_bootloader(data, true);
1943 if (error) {
Nick Dyer8efaa5e2014-07-23 12:45:49 -07001944 /* Chip is not in appmode or bootloader mode */
1945 return error;
1946 }
Nick Dyer8efaa5e2014-07-23 12:45:49 -07001947 }
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07001948
1949 /* OK, we are in bootloader, see if we can recover */
1950 if (++recovery_attempts > 1) {
1951 dev_err(&client->dev, "Could not recover from bootloader mode\n");
1952 /*
1953 * We can reflash from this state, so do not
1954 * abort initialization.
1955 */
1956 data->in_bootloader = true;
1957 return 0;
1958 }
1959
1960 /* Attempt to exit bootloader into app mode */
1961 mxt_send_bootloader_cmd(data, false);
1962 msleep(MXT_FW_RESET_TIME);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07001963 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001964
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001965 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001966 error = mxt_get_object_table(data);
Nick Dyer7bed6802014-05-18 23:04:09 -07001967 if (error) {
1968 dev_err(&client->dev, "Error %d reading object table\n", error);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001969 return error;
Nick Dyer7bed6802014-05-18 23:04:09 -07001970 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001971
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001972 error = mxt_acquire_irq(data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001973 if (error)
1974 goto err_free_object_table;
1975
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07001976 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
1977 &client->dev, GFP_KERNEL, data,
1978 mxt_config_cb);
1979 if (error) {
1980 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
1981 error);
1982 goto err_free_object_table;
1983 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001984
1985 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001986
1987err_free_object_table:
1988 mxt_free_object_table(data);
1989 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001990}
1991
Nick Dyer7f3884f72015-08-04 16:36:29 -07001992static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
1993{
1994 struct device *dev = &data->client->dev;
1995 int error;
1996 struct t7_config *new_config;
1997 struct t7_config deepsleep = { .active = 0, .idle = 0 };
1998
1999 if (sleep == MXT_POWER_CFG_DEEPSLEEP)
2000 new_config = &deepsleep;
2001 else
2002 new_config = &data->t7_cfg;
2003
2004 error = __mxt_write_reg(data->client, data->T7_address,
2005 sizeof(data->t7_cfg), new_config);
2006 if (error)
2007 return error;
2008
2009 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
2010 new_config->active, new_config->idle);
2011
2012 return 0;
2013}
2014
2015static int mxt_init_t7_power_cfg(struct mxt_data *data)
2016{
2017 struct device *dev = &data->client->dev;
2018 int error;
2019 bool retry = false;
2020
2021recheck:
2022 error = __mxt_read_reg(data->client, data->T7_address,
2023 sizeof(data->t7_cfg), &data->t7_cfg);
2024 if (error)
2025 return error;
2026
2027 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
2028 if (!retry) {
2029 dev_dbg(dev, "T7 cfg zero, resetting\n");
2030 mxt_soft_reset(data);
2031 retry = true;
2032 goto recheck;
2033 } else {
2034 dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
2035 data->t7_cfg.active = 20;
2036 data->t7_cfg.idle = 100;
2037 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2038 }
2039 }
2040
2041 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
2042 data->t7_cfg.active, data->t7_cfg.idle);
2043 return 0;
2044}
2045
Nick Dyer50a77c62014-07-23 12:38:48 -07002046static int mxt_configure_objects(struct mxt_data *data,
2047 const struct firmware *cfg)
2048{
2049 struct device *dev = &data->client->dev;
2050 struct mxt_info *info = &data->info;
2051 int error;
2052
Nick Dyer7f3884f72015-08-04 16:36:29 -07002053 error = mxt_init_t7_power_cfg(data);
2054 if (error) {
2055 dev_err(dev, "Failed to initialize power cfg\n");
2056 return error;
2057 }
2058
Nick Dyer50a77c62014-07-23 12:38:48 -07002059 if (cfg) {
2060 error = mxt_update_cfg(data, cfg);
2061 if (error)
2062 dev_warn(dev, "Error %d updating config\n", error);
2063 }
2064
Nick Dyerb23157d2015-04-06 11:25:13 -07002065 if (data->multitouch) {
2066 error = mxt_initialize_input_device(data);
2067 if (error)
2068 return error;
2069 } else {
2070 dev_warn(dev, "No touch object detected\n");
2071 }
Nick Dyer50a77c62014-07-23 12:38:48 -07002072
2073 dev_info(dev,
2074 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
2075 info->family_id, info->variant_id, info->version >> 4,
2076 info->version & 0xf, info->build, info->object_num);
2077
2078 return 0;
2079}
2080
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002081/* Firmware Version is returned as Major.Minor.Build */
2082static ssize_t mxt_fw_version_show(struct device *dev,
2083 struct device_attribute *attr, char *buf)
2084{
2085 struct mxt_data *data = dev_get_drvdata(dev);
2086 struct mxt_info *info = &data->info;
2087 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
2088 info->version >> 4, info->version & 0xf, info->build);
2089}
2090
2091/* Hardware Version is returned as FamilyID.VariantID */
2092static ssize_t mxt_hw_version_show(struct device *dev,
2093 struct device_attribute *attr, char *buf)
2094{
2095 struct mxt_data *data = dev_get_drvdata(dev);
2096 struct mxt_info *info = &data->info;
2097 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
2098 info->family_id, info->variant_id);
2099}
2100
Daniel Kurtz794eb672012-06-28 21:08:10 +08002101static ssize_t mxt_show_instance(char *buf, int count,
2102 struct mxt_object *object, int instance,
2103 const u8 *val)
2104{
2105 int i;
2106
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002107 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002108 count += scnprintf(buf + count, PAGE_SIZE - count,
2109 "Instance %u\n", instance);
2110
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002111 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002112 count += scnprintf(buf + count, PAGE_SIZE - count,
2113 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
2114 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
2115
2116 return count;
2117}
2118
Iiro Valkonen7686b102011-02-02 23:21:58 -08002119static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002120 struct device_attribute *attr, char *buf)
2121{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002122 struct mxt_data *data = dev_get_drvdata(dev);
2123 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002124 int count = 0;
2125 int i, j;
2126 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002127 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002128
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002129 /* Pre-allocate buffer large enough to hold max sized object. */
2130 obuf = kmalloc(256, GFP_KERNEL);
2131 if (!obuf)
2132 return -ENOMEM;
2133
2134 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002135 for (i = 0; i < data->info.object_num; i++) {
2136 object = data->object_table + i;
2137
Daniel Kurtz91630952012-06-28 21:08:09 +08002138 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002139 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002140
Daniel Kurtz91630952012-06-28 21:08:09 +08002141 count += scnprintf(buf + count, PAGE_SIZE - count,
2142 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002143
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002144 for (j = 0; j < mxt_obj_instances(object); j++) {
2145 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08002146 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002147
Daniel Kurtz794eb672012-06-28 21:08:10 +08002148 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002149 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002150 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002151
Daniel Kurtz794eb672012-06-28 21:08:10 +08002152 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002153 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002154 }
2155
Daniel Kurtz794eb672012-06-28 21:08:10 +08002156done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002157 kfree(obuf);
2158 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002159}
2160
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002161static int mxt_check_firmware_format(struct device *dev,
2162 const struct firmware *fw)
2163{
2164 unsigned int pos = 0;
2165 char c;
2166
2167 while (pos < fw->size) {
2168 c = *(fw->data + pos);
2169
2170 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
2171 return 0;
2172
2173 pos++;
2174 }
2175
2176 /*
2177 * To convert file try:
2178 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2179 */
2180 dev_err(dev, "Aborting: firmware file must be in binary format\n");
2181
2182 return -EINVAL;
2183}
2184
Iiro Valkonen7686b102011-02-02 23:21:58 -08002185static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002186{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002187 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002188 const struct firmware *fw = NULL;
2189 unsigned int frame_size;
2190 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07002191 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07002192 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002193 int ret;
2194
2195 ret = request_firmware(&fw, fn, dev);
2196 if (ret) {
2197 dev_err(dev, "Unable to open firmware %s\n", fn);
2198 return ret;
2199 }
2200
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002201 /* Check for incorrect enc file */
2202 ret = mxt_check_firmware_format(dev, fw);
2203 if (ret)
2204 goto release_firmware;
2205
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002206 if (!data->in_bootloader) {
2207 /* Change to the bootloader mode */
2208 data->in_bootloader = true;
Benson Leungd79e7e42014-05-18 23:02:52 -07002209
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002210 ret = mxt_t6_command(data, MXT_COMMAND_RESET,
2211 MXT_BOOT_VALUE, false);
2212 if (ret)
2213 goto release_firmware;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002214
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002215 msleep(MXT_RESET_TIME);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002216
2217 /* Do not need to scan since we know family ID */
2218 ret = mxt_lookup_bootloader_address(data, 0);
2219 if (ret)
2220 goto release_firmware;
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002221
2222 mxt_free_input_device(data);
2223 mxt_free_object_table(data);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002224 } else {
2225 enable_irq(data->irq);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002226 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002227
Benson Leungd79e7e42014-05-18 23:02:52 -07002228 reinit_completion(&data->bl_completion);
2229
Nick Dyer385deb92014-07-23 12:42:40 -07002230 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
2231 if (ret) {
2232 /* Bootloader may still be unlocked from previous attempt */
2233 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
2234 if (ret)
2235 goto disable_irq;
2236 } else {
2237 dev_info(dev, "Unlocking bootloader\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002238
Nick Dyer385deb92014-07-23 12:42:40 -07002239 /* Unlock bootloader */
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002240 ret = mxt_send_bootloader_cmd(data, true);
Nick Dyer385deb92014-07-23 12:42:40 -07002241 if (ret)
2242 goto disable_irq;
2243 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002244
2245 while (pos < fw->size) {
Nick Dyer385deb92014-07-23 12:42:40 -07002246 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002247 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07002248 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002249
2250 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
2251
Nick Dyerf943c742014-05-18 23:14:20 -07002252 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002253 frame_size += 2;
2254
2255 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07002256 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
2257 if (ret)
2258 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002259
Nick Dyer385deb92014-07-23 12:42:40 -07002260 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
Nick Dyerf943c742014-05-18 23:14:20 -07002261 if (ret) {
2262 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002263
Nick Dyerf943c742014-05-18 23:14:20 -07002264 /* Back off by 20ms per retry */
2265 msleep(retry * 20);
2266
2267 if (retry > 20) {
2268 dev_err(dev, "Retry count exceeded\n");
2269 goto disable_irq;
2270 }
2271 } else {
2272 retry = 0;
2273 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07002274 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07002275 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002276
Nick Dyerf477c752014-05-18 23:14:45 -07002277 if (frame % 50 == 0)
2278 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
2279 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002280 }
2281
Benson Leunga0434b72014-05-18 23:03:09 -07002282 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002283 ret = mxt_wait_for_completion(data, &data->bl_completion,
2284 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002285 if (ret)
2286 goto disable_irq;
2287
Nick Dyerf477c752014-05-18 23:14:45 -07002288 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
2289
Benson Leunga0434b72014-05-18 23:03:09 -07002290 /*
2291 * Wait for device to reset. Some bootloader versions do not assert
2292 * the CHG line after bootloading has finished, so ignore potential
2293 * errors.
2294 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002295 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002296
Benson Leungd79e7e42014-05-18 23:02:52 -07002297 data->in_bootloader = false;
2298
2299disable_irq:
2300 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002301release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002302 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002303 return ret;
2304}
2305
Iiro Valkonen7686b102011-02-02 23:21:58 -08002306static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002307 struct device_attribute *attr,
2308 const char *buf, size_t count)
2309{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002310 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002311 int error;
2312
Iiro Valkonen7686b102011-02-02 23:21:58 -08002313 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002314 if (error) {
2315 dev_err(dev, "The firmware update failed(%d)\n", error);
2316 count = error;
2317 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07002318 dev_info(dev, "The firmware update succeeded\n");
2319
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002320 error = mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07002321 if (error)
2322 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002323 }
2324
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002325 return count;
2326}
2327
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002328static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
2329static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07002330static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
2331static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002332
Iiro Valkonen7686b102011-02-02 23:21:58 -08002333static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002334 &dev_attr_fw_version.attr,
2335 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002336 &dev_attr_object.attr,
2337 &dev_attr_update_fw.attr,
2338 NULL
2339};
2340
Iiro Valkonen7686b102011-02-02 23:21:58 -08002341static const struct attribute_group mxt_attr_group = {
2342 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002343};
2344
Iiro Valkonen7686b102011-02-02 23:21:58 -08002345static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002346{
Nick Dyer7f3884f72015-08-04 16:36:29 -07002347 switch (data->pdata->suspend_mode) {
2348 case MXT_SUSPEND_T9_CTRL:
2349 mxt_soft_reset(data);
2350
2351 /* Touch enable */
2352 /* 0x83 = SCANEN | RPTEN | ENABLE */
2353 mxt_write_object(data,
2354 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
2355 break;
2356
2357 case MXT_SUSPEND_DEEP_SLEEP:
2358 default:
2359 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2360
2361 /* Recalibrate since chip has been in deep sleep */
2362 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
2363 break;
2364 }
2365
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002366}
2367
Iiro Valkonen7686b102011-02-02 23:21:58 -08002368static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002369{
Nick Dyer7f3884f72015-08-04 16:36:29 -07002370 switch (data->pdata->suspend_mode) {
2371 case MXT_SUSPEND_T9_CTRL:
2372 /* Touch disable */
2373 mxt_write_object(data,
2374 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
2375 break;
2376
2377 case MXT_SUSPEND_DEEP_SLEEP:
2378 default:
2379 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
2380 break;
2381 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002382}
2383
Iiro Valkonen7686b102011-02-02 23:21:58 -08002384static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002385{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002386 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002387
Iiro Valkonen7686b102011-02-02 23:21:58 -08002388 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002389
2390 return 0;
2391}
2392
Iiro Valkonen7686b102011-02-02 23:21:58 -08002393static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002394{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002395 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002396
Iiro Valkonen7686b102011-02-02 23:21:58 -08002397 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002398}
2399
Stephen Warren78188be2014-07-23 12:23:23 -07002400#ifdef CONFIG_OF
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002401static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
Stephen Warren78188be2014-07-23 12:23:23 -07002402{
2403 struct mxt_platform_data *pdata;
Nick Dyer20447662015-08-04 16:53:16 -07002404 struct device_node *np = client->dev.of_node;
Stephen Warren78188be2014-07-23 12:23:23 -07002405 u32 *keymap;
Nick Dyer20447662015-08-04 16:53:16 -07002406 int proplen, ret;
Stephen Warren78188be2014-07-23 12:23:23 -07002407
Nick Dyer20447662015-08-04 16:53:16 -07002408 if (!np)
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002409 return ERR_PTR(-ENOENT);
Stephen Warren78188be2014-07-23 12:23:23 -07002410
2411 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
2412 if (!pdata)
2413 return ERR_PTR(-ENOMEM);
2414
Nick Dyer20447662015-08-04 16:53:16 -07002415 if (of_find_property(np, "linux,gpio-keymap", &proplen)) {
Stephen Warren78188be2014-07-23 12:23:23 -07002416 pdata->t19_num_keys = proplen / sizeof(u32);
2417
2418 keymap = devm_kzalloc(&client->dev,
2419 pdata->t19_num_keys * sizeof(keymap[0]),
2420 GFP_KERNEL);
2421 if (!keymap)
2422 return ERR_PTR(-ENOMEM);
2423
Nick Dyer20447662015-08-04 16:53:16 -07002424 ret = of_property_read_u32_array(np, "linux,gpio-keymap",
2425 keymap, pdata->t19_num_keys);
2426 if (ret)
2427 dev_warn(&client->dev,
2428 "Couldn't read linux,gpio-keymap: %d\n", ret);
Stephen Warren78188be2014-07-23 12:23:23 -07002429
2430 pdata->t19_keymap = keymap;
2431 }
2432
Nick Dyer7f3884f72015-08-04 16:36:29 -07002433 pdata->suspend_mode = MXT_SUSPEND_DEEP_SLEEP;
2434
Stephen Warren78188be2014-07-23 12:23:23 -07002435 return pdata;
2436}
2437#else
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002438static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
Stephen Warren78188be2014-07-23 12:23:23 -07002439{
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002440 return ERR_PTR(-ENOENT);
Stephen Warren78188be2014-07-23 12:23:23 -07002441}
2442#endif
2443
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002444#ifdef CONFIG_ACPI
2445
2446struct mxt_acpi_platform_data {
2447 const char *hid;
2448 struct mxt_platform_data pdata;
2449};
2450
2451static unsigned int samus_touchpad_buttons[] = {
2452 KEY_RESERVED,
2453 KEY_RESERVED,
2454 KEY_RESERVED,
2455 BTN_LEFT
2456};
2457
2458static struct mxt_acpi_platform_data samus_platform_data[] = {
2459 {
2460 /* Touchpad */
2461 .hid = "ATML0000",
2462 .pdata = {
2463 .t19_num_keys = ARRAY_SIZE(samus_touchpad_buttons),
2464 .t19_keymap = samus_touchpad_buttons,
2465 },
2466 },
2467 {
2468 /* Touchscreen */
2469 .hid = "ATML0001",
2470 },
2471 { }
2472};
2473
Dmitry Torokhovc67566c2015-11-20 10:58:07 -08002474static unsigned int chromebook_tp_buttons[] = {
2475 KEY_RESERVED,
2476 KEY_RESERVED,
2477 KEY_RESERVED,
2478 KEY_RESERVED,
2479 KEY_RESERVED,
2480 BTN_LEFT
2481};
2482
2483static struct mxt_acpi_platform_data chromebook_platform_data[] = {
2484 {
2485 /* Touchpad */
2486 .hid = "ATML0000",
2487 .pdata = {
2488 .t19_num_keys = ARRAY_SIZE(chromebook_tp_buttons),
2489 .t19_keymap = chromebook_tp_buttons,
2490 },
2491 },
2492 {
2493 /* Touchscreen */
2494 .hid = "ATML0001",
2495 },
2496 { }
2497};
2498
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002499static const struct dmi_system_id mxt_dmi_table[] = {
2500 {
2501 /* 2015 Google Pixel */
2502 .ident = "Chromebook Pixel 2",
2503 .matches = {
2504 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
2505 DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
2506 },
2507 .driver_data = samus_platform_data,
2508 },
Dmitry Torokhovc67566c2015-11-20 10:58:07 -08002509 {
2510 /* Other Google Chromebooks */
2511 .ident = "Chromebook",
2512 .matches = {
2513 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
2514 },
2515 .driver_data = chromebook_platform_data,
2516 },
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002517 { }
2518};
2519
2520static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
2521{
2522 struct acpi_device *adev;
2523 const struct dmi_system_id *system_id;
2524 const struct mxt_acpi_platform_data *acpi_pdata;
2525
2526 /*
2527 * Ignore ACPI devices representing bootloader mode.
2528 *
2529 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
2530 * devices for both application and bootloader modes, but we are
2531 * interested in application mode only (if device is in bootloader
2532 * mode we'll end up switching into application anyway). So far
2533 * application mode addresses were all above 0x40, so we'll use it
2534 * as a threshold.
2535 */
2536 if (client->addr < 0x40)
2537 return ERR_PTR(-ENXIO);
2538
2539 adev = ACPI_COMPANION(&client->dev);
2540 if (!adev)
2541 return ERR_PTR(-ENOENT);
2542
2543 system_id = dmi_first_match(mxt_dmi_table);
2544 if (!system_id)
2545 return ERR_PTR(-ENOENT);
2546
2547 acpi_pdata = system_id->driver_data;
2548 if (!acpi_pdata)
2549 return ERR_PTR(-ENOENT);
2550
2551 while (acpi_pdata->hid) {
2552 if (!strcmp(acpi_device_hid(adev), acpi_pdata->hid))
2553 return &acpi_pdata->pdata;
2554
2555 acpi_pdata++;
2556 }
2557
2558 return ERR_PTR(-ENOENT);
2559}
2560#else
2561static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
2562{
2563 return ERR_PTR(-ENOENT);
2564}
2565#endif
2566
2567static const struct mxt_platform_data *
2568mxt_get_platform_data(struct i2c_client *client)
2569{
2570 const struct mxt_platform_data *pdata;
2571
2572 pdata = dev_get_platdata(&client->dev);
2573 if (pdata)
2574 return pdata;
2575
2576 pdata = mxt_parse_dt(client);
2577 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
2578 return pdata;
2579
2580 pdata = mxt_parse_acpi(client);
2581 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
2582 return pdata;
2583
2584 dev_err(&client->dev, "No platform data specified\n");
2585 return ERR_PTR(-EINVAL);
2586}
2587
Stephen Warren78188be2014-07-23 12:23:23 -07002588static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002589{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002590 struct mxt_data *data;
Stephen Warren78188be2014-07-23 12:23:23 -07002591 const struct mxt_platform_data *pdata;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002592 int error;
2593
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002594 pdata = mxt_get_platform_data(client);
2595 if (IS_ERR(pdata))
2596 return PTR_ERR(pdata);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002597
Iiro Valkonen7686b102011-02-02 23:21:58 -08002598 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Nick Dyer7a53d602014-07-23 12:21:26 -07002599 if (!data) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002600 dev_err(&client->dev, "Failed to allocate memory\n");
Nick Dyer7a53d602014-07-23 12:21:26 -07002601 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002602 }
2603
Daniel Kurtzec02ac22012-06-28 21:08:02 +08002604 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
2605 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08002606
Joonyoung Shim910d8052011-04-12 23:14:38 -07002607 data->client = client;
Joonyoung Shim910d8052011-04-12 23:14:38 -07002608 data->pdata = pdata;
2609 data->irq = client->irq;
Nick Dyer7a53d602014-07-23 12:21:26 -07002610 i2c_set_clientdata(client, data);
Joonyoung Shim910d8052011-04-12 23:14:38 -07002611
Benson Leungd79e7e42014-05-18 23:02:52 -07002612 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002613 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07002614 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07002615
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002616 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
2617 pdata->irqflags | IRQF_ONESHOT,
2618 client->name, data);
2619 if (error) {
2620 dev_err(&client->dev, "Failed to register interrupt\n");
2621 goto err_free_mem;
2622 }
2623
2624 disable_irq(client->irq);
2625
Daniel Kurtzcb159112012-06-28 21:08:22 +08002626 error = mxt_initialize(data);
2627 if (error)
Nick Dyer7a53d602014-07-23 12:21:26 -07002628 goto err_free_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002629
Iiro Valkonen7686b102011-02-02 23:21:58 -08002630 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07002631 if (error) {
2632 dev_err(&client->dev, "Failure %d creating sysfs group\n",
2633 error);
Nick Dyer7a53d602014-07-23 12:21:26 -07002634 goto err_free_object;
Nick Dyer7bed6802014-05-18 23:04:09 -07002635 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002636
2637 return 0;
2638
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002639err_free_object:
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002640 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07002641 mxt_free_object_table(data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002642err_free_irq:
2643 free_irq(client->irq, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002644err_free_mem:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002645 kfree(data);
2646 return error;
2647}
2648
Bill Pembertone2619cf2012-11-23 21:50:47 -08002649static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002650{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002651 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002652
Iiro Valkonen7686b102011-02-02 23:21:58 -08002653 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002654 free_irq(data->irq, data);
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002655 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07002656 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002657 kfree(data);
2658
2659 return 0;
2660}
2661
Jingoo Han02b6a582014-11-02 00:04:14 -07002662static int __maybe_unused mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002663{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002664 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08002665 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002666 struct input_dev *input_dev = data->input_dev;
2667
Pan Xinhui50fabb02015-08-04 16:53:04 -07002668 if (!input_dev)
2669 return 0;
2670
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002671 mutex_lock(&input_dev->mutex);
2672
2673 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08002674 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002675
2676 mutex_unlock(&input_dev->mutex);
2677
2678 return 0;
2679}
2680
Jingoo Han02b6a582014-11-02 00:04:14 -07002681static int __maybe_unused mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002682{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08002683 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08002684 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002685 struct input_dev *input_dev = data->input_dev;
2686
Pan Xinhui50fabb02015-08-04 16:53:04 -07002687 if (!input_dev)
2688 return 0;
2689
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002690 mutex_lock(&input_dev->mutex);
2691
2692 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08002693 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002694
2695 mutex_unlock(&input_dev->mutex);
2696
2697 return 0;
2698}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002699
Daniel Kurtz3a73c812012-05-08 22:29:14 -07002700static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
2701
Stephen Warren78188be2014-07-23 12:23:23 -07002702static const struct of_device_id mxt_of_match[] = {
2703 { .compatible = "atmel,maxtouch", },
2704 {},
2705};
2706MODULE_DEVICE_TABLE(of, mxt_of_match);
2707
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002708#ifdef CONFIG_ACPI
2709static const struct acpi_device_id mxt_acpi_id[] = {
2710 { "ATML0000", 0 }, /* Touchpad */
2711 { "ATML0001", 0 }, /* Touchscreen */
2712 { }
2713};
2714MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
2715#endif
2716
Iiro Valkonen7686b102011-02-02 23:21:58 -08002717static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002718 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002719 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08002720 { "atmel_mxt_tp", 0 },
Javier Martinez Canillasb7d21052015-12-11 13:22:39 -08002721 { "maxtouch", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08002722 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002723 { }
2724};
Iiro Valkonen7686b102011-02-02 23:21:58 -08002725MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002726
Iiro Valkonen7686b102011-02-02 23:21:58 -08002727static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002728 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08002729 .name = "atmel_mxt_ts",
Stephen Warren78188be2014-07-23 12:23:23 -07002730 .of_match_table = of_match_ptr(mxt_of_match),
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07002731 .acpi_match_table = ACPI_PTR(mxt_acpi_id),
Iiro Valkonen7686b102011-02-02 23:21:58 -08002732 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002733 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08002734 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08002735 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08002736 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002737};
2738
Axel Lin1b92c1c2012-03-16 23:05:41 -07002739module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002740
2741/* Module information */
2742MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08002743MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002744MODULE_LICENSE("GPL");