blob: 19378f200c635706a3f8388a31a8b81a94281792 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08003 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07004 *
5 * Copyright (C) 2010 Samsung Electronics Co.Ltd
Nick Dyer50a77c62014-07-23 12:38:48 -07006 * Copyright (C) 2011-2014 Atmel Corporation
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07007 * Copyright (C) 2012 Google, Inc.
Nick Dyerf3c4a8f2016-09-15 17:30:43 -03008 * Copyright (C) 2016 Zodiac Inflight Innovations
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07009 *
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070010 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070011 */
12
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -070013#include <linux/acpi.h>
14#include <linux/dmi.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070015#include <linux/module.h>
Benson Leungd79e7e42014-05-18 23:02:52 -070016#include <linux/init.h>
17#include <linux/completion.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070018#include <linux/delay.h>
19#include <linux/firmware.h>
20#include <linux/i2c.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070021#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070022#include <linux/interrupt.h>
Stephen Warren78188be2014-07-23 12:23:23 -070023#include <linux/of.h>
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -070024#include <linux/property.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070025#include <linux/slab.h>
Sebastian Reichelf657b002017-07-25 14:11:34 -070026#include <linux/gpio/consumer.h>
Nick Dyerb23157d2015-04-06 11:25:13 -070027#include <asm/unaligned.h>
Nick Dyerecfdd7e2016-07-18 18:10:32 -030028#include <media/v4l2-device.h>
29#include <media/v4l2-ioctl.h>
30#include <media/videobuf2-v4l2.h>
31#include <media/videobuf2-vmalloc.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070032
Nick Dyer50a77c62014-07-23 12:38:48 -070033/* Firmware files */
Iiro Valkonen7686b102011-02-02 23:21:58 -080034#define MXT_FW_NAME "maxtouch.fw"
Nick Dyer50a77c62014-07-23 12:38:48 -070035#define MXT_CFG_NAME "maxtouch.cfg"
36#define MXT_CFG_MAGIC "OBP_RAW V1"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070037
38/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080039#define MXT_OBJECT_START 0x07
Iiro Valkonen7686b102011-02-02 23:21:58 -080040#define MXT_OBJECT_SIZE 6
Nick Dyer4ce6fa02014-07-23 12:40:09 -070041#define MXT_INFO_CHECKSUM_SIZE 3
42#define MXT_MAX_BLOCK_WRITE 256
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070043
44/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070045#define MXT_DEBUG_DIAGNOSTIC_T37 37
46#define MXT_GEN_MESSAGE_T5 5
47#define MXT_GEN_COMMAND_T6 6
48#define MXT_GEN_POWER_T7 7
49#define MXT_GEN_ACQUIRE_T8 8
50#define MXT_GEN_DATASOURCE_T53 53
51#define MXT_TOUCH_MULTI_T9 9
52#define MXT_TOUCH_KEYARRAY_T15 15
53#define MXT_TOUCH_PROXIMITY_T23 23
54#define MXT_TOUCH_PROXKEY_T52 52
55#define MXT_PROCI_GRIPFACE_T20 20
56#define MXT_PROCG_NOISE_T22 22
57#define MXT_PROCI_ONETOUCH_T24 24
58#define MXT_PROCI_TWOTOUCH_T27 27
59#define MXT_PROCI_GRIP_T40 40
60#define MXT_PROCI_PALM_T41 41
61#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
62#define MXT_PROCI_STYLUS_T47 47
63#define MXT_PROCG_NOISESUPPRESSION_T48 48
64#define MXT_SPT_COMMSCONFIG_T18 18
65#define MXT_SPT_GPIOPWM_T19 19
66#define MXT_SPT_SELFTEST_T25 25
67#define MXT_SPT_CTECONFIG_T28 28
68#define MXT_SPT_USERDATA_T38 38
69#define MXT_SPT_DIGITIZER_T43 43
70#define MXT_SPT_MESSAGECOUNT_T44 44
71#define MXT_SPT_CTECONFIG_T46 46
Nick Dyer15082bd2018-07-27 11:45:47 -070072#define MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71 71
Nick Dyerb23157d2015-04-06 11:25:13 -070073#define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070074
Nick Dyer5f3f9bc2014-07-23 12:46:55 -070075/* MXT_GEN_MESSAGE_T5 object */
76#define MXT_RPTID_NOMSG 0xff
77
Iiro Valkonen81c88a72011-07-04 03:08:25 -070078/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080079#define MXT_COMMAND_RESET 0
80#define MXT_COMMAND_BACKUPNV 1
81#define MXT_COMMAND_CALIBRATE 2
82#define MXT_COMMAND_REPORTALL 3
83#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070084
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070085/* Define for T6 status byte */
Nick Dyer204b4ea2018-07-27 11:44:41 -070086#define MXT_T6_STATUS_RESET BIT(7)
87#define MXT_T6_STATUS_OFL BIT(6)
88#define MXT_T6_STATUS_SIGERR BIT(5)
89#define MXT_T6_STATUS_CAL BIT(4)
90#define MXT_T6_STATUS_CFGERR BIT(3)
91#define MXT_T6_STATUS_COMSERR BIT(2)
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070092
Iiro Valkonen81c88a72011-07-04 03:08:25 -070093/* MXT_GEN_POWER_T7 field */
Nick Dyer7f3884f72015-08-04 16:36:29 -070094struct t7_config {
95 u8 idle;
96 u8 active;
97} __packed;
98
99#define MXT_POWER_CFG_RUN 0
100#define MXT_POWER_CFG_DEEPSLEEP 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700101
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700102/* MXT_TOUCH_MULTI_T9 field */
Nick Dyer7f3884f72015-08-04 16:36:29 -0700103#define MXT_T9_CTRL 0
Nick Dyer27864892016-07-18 18:10:33 -0300104#define MXT_T9_XSIZE 3
105#define MXT_T9_YSIZE 4
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700106#define MXT_T9_ORIENT 9
107#define MXT_T9_RANGE 18
108
Nick Dyerf3889ed2014-05-18 23:22:04 -0700109/* MXT_TOUCH_MULTI_T9 status */
Nick Dyer204b4ea2018-07-27 11:44:41 -0700110#define MXT_T9_UNGRIP BIT(0)
111#define MXT_T9_SUPPRESS BIT(1)
112#define MXT_T9_AMP BIT(2)
113#define MXT_T9_VECTOR BIT(3)
114#define MXT_T9_MOVE BIT(4)
115#define MXT_T9_RELEASE BIT(5)
116#define MXT_T9_PRESS BIT(6)
117#define MXT_T9_DETECT BIT(7)
Nick Dyerf3889ed2014-05-18 23:22:04 -0700118
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700119struct t9_range {
Nick Dyer1c0276d2016-01-10 18:29:34 -0800120 __le16 x;
121 __le16 y;
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700122} __packed;
123
Nick Dyerf3889ed2014-05-18 23:22:04 -0700124/* MXT_TOUCH_MULTI_T9 orient */
Nick Dyer204b4ea2018-07-27 11:44:41 -0700125#define MXT_T9_ORIENT_SWITCH BIT(0)
126#define MXT_T9_ORIENT_INVERTX BIT(1)
127#define MXT_T9_ORIENT_INVERTY BIT(2)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700128
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700129/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800130#define MXT_COMMS_CTRL 0
131#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700132
Nick Dyerd6a39402016-07-18 18:10:31 -0300133/* MXT_DEBUG_DIAGNOSTIC_T37 */
134#define MXT_DIAGNOSTIC_PAGEUP 0x01
135#define MXT_DIAGNOSTIC_DELTAS 0x10
Nick Dyer06b3d3f2016-07-18 18:10:36 -0300136#define MXT_DIAGNOSTIC_REFS 0x11
Nick Dyerd6a39402016-07-18 18:10:31 -0300137#define MXT_DIAGNOSTIC_SIZE 128
138
Nick Dyer566d5332016-07-18 18:10:35 -0300139#define MXT_FAMILY_1386 160
140#define MXT1386_COLUMNS 3
141#define MXT1386_PAGES_PER_COLUMN 8
142
Nick Dyerd6a39402016-07-18 18:10:31 -0300143struct t37_debug {
144#ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
145 u8 mode;
146 u8 page;
147 u8 data[MXT_DIAGNOSTIC_SIZE];
148#endif
149};
150
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700151/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800152#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700153#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800154#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700155
Nick Dyerb23157d2015-04-06 11:25:13 -0700156/* T100 Multiple Touch Touchscreen */
157#define MXT_T100_CTRL 0
158#define MXT_T100_CFG1 1
159#define MXT_T100_TCHAUX 3
Nick Dyer27864892016-07-18 18:10:33 -0300160#define MXT_T100_XSIZE 9
Nick Dyerb23157d2015-04-06 11:25:13 -0700161#define MXT_T100_XRANGE 13
Nick Dyer27864892016-07-18 18:10:33 -0300162#define MXT_T100_YSIZE 20
Nick Dyerb23157d2015-04-06 11:25:13 -0700163#define MXT_T100_YRANGE 24
164
165#define MXT_T100_CFG_SWITCHXY BIT(5)
Nick Dyerde601f72016-07-18 18:10:34 -0300166#define MXT_T100_CFG_INVERTY BIT(6)
167#define MXT_T100_CFG_INVERTX BIT(7)
Nick Dyerb23157d2015-04-06 11:25:13 -0700168
169#define MXT_T100_TCHAUX_VECT BIT(0)
170#define MXT_T100_TCHAUX_AMPL BIT(1)
171#define MXT_T100_TCHAUX_AREA BIT(2)
172
173#define MXT_T100_DETECT BIT(7)
174#define MXT_T100_TYPE_MASK 0x70
175
176enum t100_type {
177 MXT_T100_TYPE_FINGER = 1,
178 MXT_T100_TYPE_PASSIVE_STYLUS = 2,
179 MXT_T100_TYPE_HOVERING_FINGER = 4,
180 MXT_T100_TYPE_GLOVE = 5,
181 MXT_T100_TYPE_LARGE_TOUCH = 6,
182};
183
184#define MXT_DISTANCE_ACTIVE_TOUCH 0
185#define MXT_DISTANCE_HOVERING 1
186
187#define MXT_TOUCH_MAJOR_DEFAULT 1
188#define MXT_PRESSURE_DEFAULT 1
189
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700190/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800191#define MXT_BACKUP_TIME 50 /* msec */
Sebastian Reichelca1cd362018-05-24 09:33:36 -0700192#define MXT_RESET_GPIO_TIME 20 /* msec */
193#define MXT_RESET_INVALID_CHG 100 /* msec */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800194#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700195#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700196#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700197#define MXT_FW_RESET_TIME 3000 /* msec */
198#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
200/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800201#define MXT_UNLOCK_CMD_MSB 0xaa
202#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700203
204/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800205#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
206#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
207#define MXT_FRAME_CRC_CHECK 0x02
208#define MXT_FRAME_CRC_FAIL 0x03
209#define MXT_FRAME_CRC_PASS 0x04
210#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
211#define MXT_BOOT_STATUS_MASK 0x3f
Nick Dyer204b4ea2018-07-27 11:44:41 -0700212#define MXT_BOOT_EXTENDED_ID BIT(5)
Nick Dyere57a66a2014-05-18 23:13:40 -0700213#define MXT_BOOT_ID_MASK 0x1f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700214
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700215/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800216#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800218#define MXT_PIXELS_PER_MM 20
219
Iiro Valkonen7686b102011-02-02 23:21:58 -0800220struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700221 u8 family_id;
222 u8 variant_id;
223 u8 version;
224 u8 build;
225 u8 matrix_xsize;
226 u8 matrix_ysize;
227 u8 object_num;
228};
229
Iiro Valkonen7686b102011-02-02 23:21:58 -0800230struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231 u8 type;
232 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700233 u8 size_minus_one;
234 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800236} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700237
Nick Dyerd6a39402016-07-18 18:10:31 -0300238struct mxt_dbg {
239 u16 t37_address;
240 u16 diag_cmd_address;
241 struct t37_debug *t37_buf;
242 unsigned int t37_pages;
243 unsigned int t37_nodes;
Nick Dyerecfdd7e2016-07-18 18:10:32 -0300244
245 struct v4l2_device v4l2;
246 struct v4l2_pix_format format;
247 struct video_device vdev;
248 struct vb2_queue queue;
249 struct mutex lock;
250 int input;
251};
252
Nick Dyer06b3d3f2016-07-18 18:10:36 -0300253enum v4l_dbg_inputs {
254 MXT_V4L_INPUT_DELTAS,
255 MXT_V4L_INPUT_REFS,
256 MXT_V4L_INPUT_MAX,
257};
258
Nick Dyerecfdd7e2016-07-18 18:10:32 -0300259static const struct v4l2_file_operations mxt_video_fops = {
260 .owner = THIS_MODULE,
261 .open = v4l2_fh_open,
262 .release = vb2_fop_release,
263 .unlocked_ioctl = video_ioctl2,
264 .read = vb2_fop_read,
265 .mmap = vb2_fop_mmap,
266 .poll = vb2_fop_poll,
Nick Dyerd6a39402016-07-18 18:10:31 -0300267};
268
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700269enum mxt_suspend_mode {
270 MXT_SUSPEND_DEEP_SLEEP = 0,
271 MXT_SUSPEND_T9_CTRL = 1,
272};
273
Nick Dyerf865df72018-07-27 11:46:29 -0700274/* Config update context */
275struct mxt_cfg {
Nick Dyera4891f12018-07-27 11:46:46 -0700276 u8 *raw;
Nick Dyerf865df72018-07-27 11:46:29 -0700277 size_t raw_size;
278 off_t raw_pos;
279
280 u8 *mem;
281 size_t mem_size;
282 int start_ofs;
283
284 struct mxt_info info;
285};
286
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700287/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800288struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700289 struct i2c_client *client;
290 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800291 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800292 struct mxt_object *object_table;
Nick Dyer068bdb62018-05-01 11:40:18 -0700293 struct mxt_info *info;
294 void *raw_info_block;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700295 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700296 unsigned int max_x;
297 unsigned int max_y;
Nick Dyerde601f72016-07-18 18:10:34 -0300298 bool invertx;
299 bool inverty;
Nick Dyer1c0276d2016-01-10 18:29:34 -0800300 bool xy_switch;
Nick Dyer27864892016-07-18 18:10:33 -0300301 u8 xsize;
302 u8 ysize;
Benson Leungd79e7e42014-05-18 23:02:52 -0700303 bool in_bootloader;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700304 u16 mem_size;
Nick Dyerb23157d2015-04-06 11:25:13 -0700305 u8 t100_aux_ampl;
306 u8 t100_aux_area;
307 u8 t100_aux_vect;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700308 u8 max_reportid;
Nick Dyerc3f78042014-05-18 23:04:46 -0700309 u32 config_crc;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700310 u32 info_crc;
Nick Dyerf28a8422014-05-18 23:10:49 -0700311 u8 bootloader_addr;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700312 u8 *msg_buf;
Nick Dyer497767d2014-07-23 12:47:50 -0700313 u8 t6_status;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700314 bool update_input;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700315 u8 last_message_count;
316 u8 num_touchids;
Nick Dyerb23157d2015-04-06 11:25:13 -0700317 u8 multitouch;
Nick Dyer7f3884f72015-08-04 16:36:29 -0700318 struct t7_config t7_cfg;
Nick Dyerd6a39402016-07-18 18:10:31 -0300319 struct mxt_dbg dbg;
Sebastian Reichelf657b002017-07-25 14:11:34 -0700320 struct gpio_desc *reset_gpio;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800321
322 /* Cached parameters from object table */
Nick Dyerb9b05a82014-07-23 12:48:13 -0700323 u16 T5_address;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700324 u8 T5_msg_size;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800325 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700326 u16 T6_address;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700327 u16 T7_address;
Nick Dyer15082bd2018-07-27 11:45:47 -0700328 u16 T71_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800329 u8 T9_reportid_min;
330 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800331 u8 T19_reportid;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700332 u16 T44_address;
Nick Dyerb23157d2015-04-06 11:25:13 -0700333 u8 T100_reportid_min;
334 u8 T100_reportid_max;
Benson Leungd79e7e42014-05-18 23:02:52 -0700335
336 /* for fw update in bootloader */
337 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700338
339 /* for reset handling */
340 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700341
342 /* for config update handling */
343 struct completion crc_completion;
Dmitry Torokhovd80808e2018-03-20 15:31:25 -0700344
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700345 u32 *t19_keymap;
346 unsigned int t19_num_keys;
347
Dmitry Torokhovd80808e2018-03-20 15:31:25 -0700348 enum mxt_suspend_mode suspend_mode;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700349};
350
Nick Dyerecfdd7e2016-07-18 18:10:32 -0300351struct mxt_vb2_buffer {
352 struct vb2_buffer vb;
353 struct list_head list;
354};
355
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700356static size_t mxt_obj_size(const struct mxt_object *obj)
357{
358 return obj->size_minus_one + 1;
359}
360
361static size_t mxt_obj_instances(const struct mxt_object *obj)
362{
363 return obj->instances_minus_one + 1;
364}
365
Iiro Valkonen7686b102011-02-02 23:21:58 -0800366static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700367{
368 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700369 case MXT_GEN_COMMAND_T6:
370 case MXT_GEN_POWER_T7:
371 case MXT_GEN_ACQUIRE_T8:
372 case MXT_GEN_DATASOURCE_T53:
373 case MXT_TOUCH_MULTI_T9:
374 case MXT_TOUCH_KEYARRAY_T15:
375 case MXT_TOUCH_PROXIMITY_T23:
376 case MXT_TOUCH_PROXKEY_T52:
Maxime Roussin-Bélanger089b50d2017-05-19 14:59:03 -0700377 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700378 case MXT_PROCI_GRIPFACE_T20:
379 case MXT_PROCG_NOISE_T22:
380 case MXT_PROCI_ONETOUCH_T24:
381 case MXT_PROCI_TWOTOUCH_T27:
382 case MXT_PROCI_GRIP_T40:
383 case MXT_PROCI_PALM_T41:
384 case MXT_PROCI_TOUCHSUPPRESSION_T42:
385 case MXT_PROCI_STYLUS_T47:
386 case MXT_PROCG_NOISESUPPRESSION_T48:
387 case MXT_SPT_COMMSCONFIG_T18:
388 case MXT_SPT_GPIOPWM_T19:
389 case MXT_SPT_SELFTEST_T25:
390 case MXT_SPT_CTECONFIG_T28:
391 case MXT_SPT_USERDATA_T38:
392 case MXT_SPT_DIGITIZER_T43:
393 case MXT_SPT_CTECONFIG_T46:
Nick Dyer15082bd2018-07-27 11:45:47 -0700394 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700395 return true;
396 default:
397 return false;
398 }
399}
400
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700401static void mxt_dump_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700402{
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700403 dev_dbg(&data->client->dev, "message: %*ph\n",
404 data->T5_msg_size, message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700405}
406
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700407static int mxt_wait_for_completion(struct mxt_data *data,
408 struct completion *comp,
409 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700410{
Benson Leungd79e7e42014-05-18 23:02:52 -0700411 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700412 unsigned long timeout = msecs_to_jiffies(timeout_ms);
413 long ret;
414
415 ret = wait_for_completion_interruptible_timeout(comp, timeout);
416 if (ret < 0) {
417 return ret;
418 } else if (ret == 0) {
419 dev_err(dev, "Wait for completion timed out.\n");
420 return -ETIMEDOUT;
421 }
422 return 0;
423}
424
Nick Dyerf28a8422014-05-18 23:10:49 -0700425static int mxt_bootloader_read(struct mxt_data *data,
426 u8 *val, unsigned int count)
427{
428 int ret;
429 struct i2c_msg msg;
430
431 msg.addr = data->bootloader_addr;
432 msg.flags = data->client->flags & I2C_M_TEN;
433 msg.flags |= I2C_M_RD;
434 msg.len = count;
435 msg.buf = val;
436
437 ret = i2c_transfer(data->client->adapter, &msg, 1);
Nick Dyerf28a8422014-05-18 23:10:49 -0700438 if (ret == 1) {
439 ret = 0;
440 } else {
441 ret = ret < 0 ? ret : -EIO;
442 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
443 __func__, ret);
444 }
445
446 return ret;
447}
448
449static int mxt_bootloader_write(struct mxt_data *data,
450 const u8 * const val, unsigned int count)
451{
452 int ret;
453 struct i2c_msg msg;
454
455 msg.addr = data->bootloader_addr;
456 msg.flags = data->client->flags & I2C_M_TEN;
457 msg.len = count;
458 msg.buf = (u8 *)val;
459
460 ret = i2c_transfer(data->client->adapter, &msg, 1);
461 if (ret == 1) {
462 ret = 0;
463 } else {
464 ret = ret < 0 ? ret : -EIO;
465 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
466 __func__, ret);
467 }
468
469 return ret;
470}
471
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700472static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
Nick Dyerf28a8422014-05-18 23:10:49 -0700473{
474 u8 appmode = data->client->addr;
475 u8 bootloader;
Nick Dyer068bdb62018-05-01 11:40:18 -0700476 u8 family_id = data->info ? data->info->family_id : 0;
Nick Dyerf28a8422014-05-18 23:10:49 -0700477
478 switch (appmode) {
479 case 0x4a:
480 case 0x4b:
Nick Dyer44a0bab2014-07-23 12:45:26 -0700481 /* Chips after 1664S use different scheme */
Nick Dyer068bdb62018-05-01 11:40:18 -0700482 if (retry || family_id >= 0xa2) {
Nick Dyer44a0bab2014-07-23 12:45:26 -0700483 bootloader = appmode - 0x24;
484 break;
485 }
Gustavo A. R. Silva03bf6782018-10-15 11:21:47 -0700486 /* Fall through - for normal case */
Nick Dyerf28a8422014-05-18 23:10:49 -0700487 case 0x4c:
488 case 0x4d:
489 case 0x5a:
490 case 0x5b:
491 bootloader = appmode - 0x26;
492 break;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700493
Nick Dyerf28a8422014-05-18 23:10:49 -0700494 default:
495 dev_err(&data->client->dev,
496 "Appmode i2c address 0x%02x not found\n",
497 appmode);
498 return -EINVAL;
499 }
500
501 data->bootloader_addr = bootloader;
502 return 0;
503}
504
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700505static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700506{
507 struct device *dev = &data->client->dev;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700508 int error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700509 u8 val;
510 bool crc_failure;
511
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700512 error = mxt_lookup_bootloader_address(data, alt_address);
513 if (error)
514 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700515
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700516 error = mxt_bootloader_read(data, &val, 1);
517 if (error)
518 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700519
520 /* Check app crc fail mode */
521 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
522
523 dev_err(dev, "Detected bootloader, status:%02X%s\n",
524 val, crc_failure ? ", APP_CRC_FAIL" : "");
525
526 return 0;
527}
528
Nick Dyere57a66a2014-05-18 23:13:40 -0700529static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
530{
531 struct device *dev = &data->client->dev;
532 u8 buf[3];
533
534 if (val & MXT_BOOT_EXTENDED_ID) {
535 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
536 dev_err(dev, "%s: i2c failure\n", __func__);
Nick Dyer68807a02014-06-07 23:17:26 -0700537 return val;
Nick Dyere57a66a2014-05-18 23:13:40 -0700538 }
539
540 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
541
542 return buf[0];
543 } else {
544 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
545
546 return val;
547 }
548}
549
Nick Dyer385deb92014-07-23 12:42:40 -0700550static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
551 bool wait)
Benson Leungd79e7e42014-05-18 23:02:52 -0700552{
Nick Dyerf28a8422014-05-18 23:10:49 -0700553 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700554 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700555 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700556
557recheck:
Nick Dyer385deb92014-07-23 12:42:40 -0700558 if (wait) {
Benson Leungd79e7e42014-05-18 23:02:52 -0700559 /*
560 * In application update mode, the interrupt
561 * line signals state transitions. We must wait for the
562 * CHG assertion before reading the status byte.
563 * Once the status byte has been read, the line is deasserted.
564 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700565 ret = mxt_wait_for_completion(data, &data->bl_completion,
566 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700567 if (ret) {
568 /*
569 * TODO: handle -ERESTARTSYS better by terminating
570 * fw update process before returning to userspace
571 * by writing length 0x000 to device (iff we are in
572 * WAITING_FRAME_DATA state).
573 */
Nick Dyerf28a8422014-05-18 23:10:49 -0700574 dev_err(dev, "Update wait error %d\n", ret);
Benson Leungd79e7e42014-05-18 23:02:52 -0700575 return ret;
576 }
577 }
578
Nick Dyerf28a8422014-05-18 23:10:49 -0700579 ret = mxt_bootloader_read(data, &val, 1);
580 if (ret)
581 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582
Nick Dyere57a66a2014-05-18 23:13:40 -0700583 if (state == MXT_WAITING_BOOTLOAD_CMD)
584 val = mxt_get_bootloader_version(data, val);
585
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800587 case MXT_WAITING_BOOTLOAD_CMD:
588 case MXT_WAITING_FRAME_DATA:
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700589 case MXT_APP_CRC_FAIL:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800590 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700591 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800592 case MXT_FRAME_CRC_PASS:
Nick Dyerf943c742014-05-18 23:14:20 -0700593 if (val == MXT_FRAME_CRC_CHECK) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594 goto recheck;
Nick Dyerf943c742014-05-18 23:14:20 -0700595 } else if (val == MXT_FRAME_CRC_FAIL) {
596 dev_err(dev, "Bootloader CRC fail\n");
597 return -EINVAL;
598 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599 break;
600 default:
601 return -EINVAL;
602 }
603
604 if (val != state) {
Nick Dyerf28a8422014-05-18 23:10:49 -0700605 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
Nick Dyer7bed6802014-05-18 23:04:09 -0700606 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700607 return -EINVAL;
608 }
609
610 return 0;
611}
612
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700613static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700614{
Nick Dyerf28a8422014-05-18 23:10:49 -0700615 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616 u8 buf[2];
617
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700618 if (unlock) {
619 buf[0] = MXT_UNLOCK_CMD_LSB;
620 buf[1] = MXT_UNLOCK_CMD_MSB;
621 } else {
622 buf[0] = 0x01;
623 buf[1] = 0x01;
624 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700625
Nick Dyerf28a8422014-05-18 23:10:49 -0700626 ret = mxt_bootloader_write(data, buf, 2);
627 if (ret)
628 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700629
630 return 0;
631}
632
Iiro Valkonen7686b102011-02-02 23:21:58 -0800633static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634 u16 reg, u16 len, void *val)
635{
636 struct i2c_msg xfer[2];
637 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800638 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700639
640 buf[0] = reg & 0xff;
641 buf[1] = (reg >> 8) & 0xff;
642
643 /* Write register */
644 xfer[0].addr = client->addr;
645 xfer[0].flags = 0;
646 xfer[0].len = 2;
647 xfer[0].buf = buf;
648
649 /* Read data */
650 xfer[1].addr = client->addr;
651 xfer[1].flags = I2C_M_RD;
652 xfer[1].len = len;
653 xfer[1].buf = val;
654
Daniel Kurtz771733e2012-06-28 21:08:11 +0800655 ret = i2c_transfer(client->adapter, xfer, 2);
656 if (ret == 2) {
657 ret = 0;
658 } else {
659 if (ret >= 0)
660 ret = -EIO;
661 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
662 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700663 }
664
Daniel Kurtz771733e2012-06-28 21:08:11 +0800665 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700666}
667
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800668static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
669 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700670{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800671 u8 *buf;
672 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800673 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800675 count = len + 2;
676 buf = kmalloc(count, GFP_KERNEL);
677 if (!buf)
678 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700679
680 buf[0] = reg & 0xff;
681 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800682 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700683
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800684 ret = i2c_master_send(client, buf, count);
685 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800686 ret = 0;
687 } else {
688 if (ret >= 0)
689 ret = -EIO;
690 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
691 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700692 }
693
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800694 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800695 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696}
697
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800698static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800700 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700701}
702
Iiro Valkonen7686b102011-02-02 23:21:58 -0800703static struct mxt_object *
704mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700705{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800706 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700707 int i;
708
Nick Dyer068bdb62018-05-01 11:40:18 -0700709 for (i = 0; i < data->info->object_num; i++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700710 object = data->object_table + i;
711 if (object->type == type)
712 return object;
713 }
714
Nick Dyer50a77c62014-07-23 12:38:48 -0700715 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716 return NULL;
717}
718
Nick Dyer497767d2014-07-23 12:47:50 -0700719static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
720{
721 struct device *dev = &data->client->dev;
722 u8 status = msg[1];
723 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
724
Nick Dyer497767d2014-07-23 12:47:50 -0700725 if (crc != data->config_crc) {
726 data->config_crc = crc;
727 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
728 }
729
Nick Dyer19a71212018-07-27 11:49:29 -0700730 complete(&data->crc_completion);
731
Nick Dyer497767d2014-07-23 12:47:50 -0700732 /* Detect reset */
733 if (status & MXT_T6_STATUS_RESET)
734 complete(&data->reset_completion);
735
736 /* Output debug if status has changed */
737 if (status != data->t6_status)
738 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
739 status,
740 status == 0 ? " OK" : "",
741 status & MXT_T6_STATUS_RESET ? " RESET" : "",
742 status & MXT_T6_STATUS_OFL ? " OFL" : "",
743 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
744 status & MXT_T6_STATUS_CAL ? " CAL" : "",
745 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
746 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
747
748 /* Save current status */
749 data->t6_status = status;
750}
751
Linus Torvalds7f405482014-12-31 12:59:34 -0800752static int mxt_write_object(struct mxt_data *data,
753 u8 type, u8 offset, u8 val)
754{
755 struct mxt_object *object;
756 u16 reg;
757
758 object = mxt_get_object(data, type);
759 if (!object || offset >= mxt_obj_size(object))
760 return -EINVAL;
761
762 reg = object->start_address;
763 return mxt_write_reg(data->client, reg + offset, val);
764}
765
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700766static void mxt_input_button(struct mxt_data *data, u8 *message)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800767{
768 struct input_dev *input = data->input_dev;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800769 int i;
770
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700771 for (i = 0; i < data->t19_num_keys; i++) {
772 if (data->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800773 continue;
Dmitry Torokhovc37f6d32015-04-15 10:31:10 -0700774
775 /* Active-low switch */
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700776 input_report_key(input, data->t19_keymap[i],
Dmitry Torokhovc37f6d32015-04-15 10:31:10 -0700777 !(message[1] & BIT(i)));
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800778 }
779}
780
Benson Leungb735fbe2014-07-23 12:22:27 -0700781static void mxt_input_sync(struct mxt_data *data)
Nick Dyereef820d2014-05-18 23:22:22 -0700782{
Benson Leungb735fbe2014-07-23 12:22:27 -0700783 input_mt_report_pointer_emulation(data->input_dev,
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700784 data->t19_num_keys);
Benson Leungb735fbe2014-07-23 12:22:27 -0700785 input_sync(data->input_dev);
Nick Dyereef820d2014-05-18 23:22:22 -0700786}
787
Nick Dyerb9b05a82014-07-23 12:48:13 -0700788static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700789{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700790 struct device *dev = &data->client->dev;
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800791 struct input_dev *input_dev = data->input_dev;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700792 int id;
793 u8 status;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700794 int x;
795 int y;
796 int area;
Nick Dyerfea9e462014-05-18 23:21:48 -0700797 int amplitude;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700799 id = message[0] - data->T9_reportid_min;
800 status = message[1];
801 x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
802 y = (message[3] << 4) | ((message[4] & 0xf));
Nick Dyereef820d2014-05-18 23:22:22 -0700803
804 /* Handle 10/12 bit switching */
Joonyoung Shim910d8052011-04-12 23:14:38 -0700805 if (data->max_x < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700806 x >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700807 if (data->max_y < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700808 y >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700809
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700810 area = message[5];
811 amplitude = message[6];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700812
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800813 dev_dbg(dev,
814 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
815 id,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700816 (status & MXT_T9_DETECT) ? 'D' : '.',
817 (status & MXT_T9_PRESS) ? 'P' : '.',
818 (status & MXT_T9_RELEASE) ? 'R' : '.',
819 (status & MXT_T9_MOVE) ? 'M' : '.',
820 (status & MXT_T9_VECTOR) ? 'V' : '.',
821 (status & MXT_T9_AMP) ? 'A' : '.',
822 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
823 (status & MXT_T9_UNGRIP) ? 'U' : '.',
Nick Dyerfea9e462014-05-18 23:21:48 -0700824 x, y, area, amplitude);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700825
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800826 input_mt_slot(input_dev, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700827
Nick Dyerf3889ed2014-05-18 23:22:04 -0700828 if (status & MXT_T9_DETECT) {
Nick Dyereef820d2014-05-18 23:22:22 -0700829 /*
830 * Multiple bits may be set if the host is slow to read
831 * the status messages, indicating all the events that
832 * have happened.
833 */
834 if (status & MXT_T9_RELEASE) {
835 input_mt_report_slot_state(input_dev,
836 MT_TOOL_FINGER, 0);
Benson Leungb735fbe2014-07-23 12:22:27 -0700837 mxt_input_sync(data);
Nick Dyereef820d2014-05-18 23:22:22 -0700838 }
839
Nick Dyer2ca3ba02018-07-27 11:49:10 -0700840 /* if active, pressure must be non-zero */
841 if (!amplitude)
842 amplitude = MXT_PRESSURE_DEFAULT;
843
Nick Dyereef820d2014-05-18 23:22:22 -0700844 /* Touch active */
845 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800846 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
847 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
Nick Dyerfea9e462014-05-18 23:21:48 -0700848 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800849 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
Nick Dyereef820d2014-05-18 23:22:22 -0700850 } else {
851 /* Touch no longer active, close out slot */
852 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800853 }
Nick Dyerb9b05a82014-07-23 12:48:13 -0700854
855 data->update_input = true;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856}
857
Nick Dyerb23157d2015-04-06 11:25:13 -0700858static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
859{
860 struct device *dev = &data->client->dev;
861 struct input_dev *input_dev = data->input_dev;
862 int id;
863 u8 status;
864 u8 type = 0;
865 u16 x;
866 u16 y;
867 int distance = 0;
868 int tool = 0;
869 u8 major = 0;
870 u8 pressure = 0;
871 u8 orientation = 0;
872
873 id = message[0] - data->T100_reportid_min - 2;
874
875 /* ignore SCRSTATUS events */
876 if (id < 0)
877 return;
878
879 status = message[1];
880 x = get_unaligned_le16(&message[2]);
881 y = get_unaligned_le16(&message[4]);
882
883 if (status & MXT_T100_DETECT) {
884 type = (status & MXT_T100_TYPE_MASK) >> 4;
885
886 switch (type) {
887 case MXT_T100_TYPE_HOVERING_FINGER:
888 tool = MT_TOOL_FINGER;
889 distance = MXT_DISTANCE_HOVERING;
890
891 if (data->t100_aux_vect)
892 orientation = message[data->t100_aux_vect];
893
894 break;
895
896 case MXT_T100_TYPE_FINGER:
897 case MXT_T100_TYPE_GLOVE:
898 tool = MT_TOOL_FINGER;
899 distance = MXT_DISTANCE_ACTIVE_TOUCH;
900
901 if (data->t100_aux_area)
902 major = message[data->t100_aux_area];
903
904 if (data->t100_aux_ampl)
905 pressure = message[data->t100_aux_ampl];
906
907 if (data->t100_aux_vect)
908 orientation = message[data->t100_aux_vect];
909
910 break;
911
912 case MXT_T100_TYPE_PASSIVE_STYLUS:
913 tool = MT_TOOL_PEN;
914
915 /*
916 * Passive stylus is reported with size zero so
917 * hardcode.
918 */
919 major = MXT_TOUCH_MAJOR_DEFAULT;
920
921 if (data->t100_aux_ampl)
922 pressure = message[data->t100_aux_ampl];
923
924 break;
925
926 case MXT_T100_TYPE_LARGE_TOUCH:
927 /* Ignore suppressed touch */
928 break;
929
930 default:
931 dev_dbg(dev, "Unexpected T100 type\n");
932 return;
933 }
934 }
935
936 /*
937 * Values reported should be non-zero if tool is touching the
938 * device
939 */
940 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
941 pressure = MXT_PRESSURE_DEFAULT;
942
943 input_mt_slot(input_dev, id);
944
945 if (status & MXT_T100_DETECT) {
946 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
947 id, type, x, y, major, pressure, orientation);
948
949 input_mt_report_slot_state(input_dev, tool, 1);
950 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
951 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
952 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
953 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
954 input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
955 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
956 } else {
957 dev_dbg(dev, "[%u] release\n", id);
958
959 /* close out slot */
960 input_mt_report_slot_state(input_dev, 0, 0);
961 }
962
963 data->update_input = true;
964}
965
Nick Dyerb9b05a82014-07-23 12:48:13 -0700966static int mxt_proc_message(struct mxt_data *data, u8 *message)
Daniel Kurtz04a79182012-06-28 21:08:21 +0800967{
Nick Dyerb9b05a82014-07-23 12:48:13 -0700968 u8 report_id = message[0];
969
970 if (report_id == MXT_RPTID_NOMSG)
971 return 0;
972
973 if (report_id == data->T6_reportid) {
974 mxt_proc_t6_messages(data, message);
975 } else if (!data->input_dev) {
976 /*
977 * Do not report events if input device
978 * is not yet registered.
979 */
980 mxt_dump_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700981 } else if (report_id >= data->T9_reportid_min &&
982 report_id <= data->T9_reportid_max) {
Nick Dyerb9b05a82014-07-23 12:48:13 -0700983 mxt_proc_t9_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700984 } else if (report_id >= data->T100_reportid_min &&
985 report_id <= data->T100_reportid_max) {
986 mxt_proc_t100_message(data, message);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700987 } else if (report_id == data->T19_reportid) {
988 mxt_input_button(data, message);
989 data->update_input = true;
990 } else {
991 mxt_dump_message(data, message);
992 }
993
994 return 1;
995}
996
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700997static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
Nick Dyerb9b05a82014-07-23 12:48:13 -0700998{
999 struct device *dev = &data->client->dev;
1000 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001001 int i;
1002 u8 num_valid = 0;
Nick Dyerb9b05a82014-07-23 12:48:13 -07001003
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001004 /* Safety check for msg_buf */
1005 if (count > data->max_reportid)
1006 return -EINVAL;
1007
1008 /* Process remaining messages if necessary */
Nick Dyerb9b05a82014-07-23 12:48:13 -07001009 ret = __mxt_read_reg(data->client, data->T5_address,
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001010 data->T5_msg_size * count, data->msg_buf);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001011 if (ret) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001012 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001013 return ret;
1014 }
1015
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001016 for (i = 0; i < count; i++) {
1017 ret = mxt_proc_message(data,
1018 data->msg_buf + data->T5_msg_size * i);
1019
1020 if (ret == 1)
1021 num_valid++;
1022 }
1023
1024 /* return number of messages read */
1025 return num_valid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001026}
1027
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001028static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029{
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001030 struct device *dev = &data->client->dev;
Nick Dyerb9b05a82014-07-23 12:48:13 -07001031 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001032 u8 count, num_left;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001033
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001034 /* Read T44 and T5 together */
1035 ret = __mxt_read_reg(data->client, data->T44_address,
1036 data->T5_msg_size + 1, data->msg_buf);
1037 if (ret) {
1038 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
1039 return IRQ_NONE;
1040 }
1041
1042 count = data->msg_buf[0];
1043
Nick Dyer507584e2015-08-04 16:58:05 -07001044 /*
1045 * This condition may be caused by the CHG line being configured in
1046 * Mode 0. It results in unnecessary I2C operations but it is benign.
1047 */
1048 if (count == 0)
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001049 return IRQ_NONE;
Nick Dyer507584e2015-08-04 16:58:05 -07001050
1051 if (count > data->max_reportid) {
1052 dev_warn(dev, "T44 count %d exceeded max report id\n", count);
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001053 count = data->max_reportid;
1054 }
1055
1056 /* Process first message */
1057 ret = mxt_proc_message(data, data->msg_buf + 1);
1058 if (ret < 0) {
1059 dev_warn(dev, "Unexpected invalid message\n");
1060 return IRQ_NONE;
1061 }
1062
1063 num_left = count - 1;
1064
1065 /* Process remaining messages if necessary */
1066 if (num_left) {
1067 ret = mxt_read_and_process_messages(data, num_left);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001068 if (ret < 0)
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001069 goto end;
1070 else if (ret != num_left)
1071 dev_warn(dev, "Unexpected invalid message\n");
1072 }
1073
1074end:
1075 if (data->update_input) {
1076 mxt_input_sync(data);
1077 data->update_input = false;
1078 }
1079
1080 return IRQ_HANDLED;
1081}
1082
1083static int mxt_process_messages_until_invalid(struct mxt_data *data)
1084{
1085 struct device *dev = &data->client->dev;
1086 int count, read;
1087 u8 tries = 2;
1088
1089 count = data->max_reportid;
1090
1091 /* Read messages until we force an invalid */
1092 do {
1093 read = mxt_read_and_process_messages(data, count);
1094 if (read < count)
1095 return 0;
1096 } while (--tries);
1097
1098 if (data->update_input) {
1099 mxt_input_sync(data);
1100 data->update_input = false;
1101 }
1102
1103 dev_err(dev, "CHG pin isn't cleared\n");
1104 return -EBUSY;
1105}
1106
1107static irqreturn_t mxt_process_messages(struct mxt_data *data)
1108{
1109 int total_handled, num_handled;
1110 u8 count = data->last_message_count;
1111
1112 if (count < 1 || count > data->max_reportid)
1113 count = 1;
1114
1115 /* include final invalid message */
1116 total_handled = mxt_read_and_process_messages(data, count + 1);
1117 if (total_handled < 0)
1118 return IRQ_NONE;
1119 /* if there were invalid messages, then we are done */
1120 else if (total_handled <= count)
1121 goto update_count;
1122
1123 /* keep reading two msgs until one is invalid or reportid limit */
1124 do {
1125 num_handled = mxt_read_and_process_messages(data, 2);
1126 if (num_handled < 0)
Nick Dyer8d4e1632014-05-18 23:00:56 -07001127 return IRQ_NONE;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001128
1129 total_handled += num_handled;
1130
1131 if (num_handled < 2)
1132 break;
1133 } while (total_handled < data->num_touchids);
1134
1135update_count:
1136 data->last_message_count = total_handled;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001137
Nick Dyerb9b05a82014-07-23 12:48:13 -07001138 if (data->update_input) {
Benson Leungb735fbe2014-07-23 12:22:27 -07001139 mxt_input_sync(data);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001140 data->update_input = false;
1141 }
Daniel Kurtz64464ae2012-06-28 21:08:23 +08001142
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143 return IRQ_HANDLED;
1144}
1145
Benson Leungd79e7e42014-05-18 23:02:52 -07001146static irqreturn_t mxt_interrupt(int irq, void *dev_id)
1147{
1148 struct mxt_data *data = dev_id;
1149
1150 if (data->in_bootloader) {
1151 /* bootloader state transition completion */
1152 complete(&data->bl_completion);
1153 return IRQ_HANDLED;
1154 }
1155
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001156 if (!data->object_table)
1157 return IRQ_HANDLED;
1158
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001159 if (data->T44_address) {
1160 return mxt_process_messages_t44(data);
1161 } else {
1162 return mxt_process_messages(data);
1163 }
Benson Leungd79e7e42014-05-18 23:02:52 -07001164}
1165
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001166static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
1167 u8 value, bool wait)
1168{
1169 u16 reg;
1170 u8 command_register;
1171 int timeout_counter = 0;
1172 int ret;
1173
1174 reg = data->T6_address + cmd_offset;
1175
1176 ret = mxt_write_reg(data->client, reg, value);
1177 if (ret)
1178 return ret;
1179
1180 if (!wait)
1181 return 0;
1182
1183 do {
1184 msleep(20);
1185 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
1186 if (ret)
1187 return ret;
1188 } while (command_register != 0 && timeout_counter++ <= 100);
1189
1190 if (timeout_counter > 100) {
1191 dev_err(&data->client->dev, "Command failed!\n");
1192 return -EIO;
1193 }
1194
1195 return 0;
1196}
1197
Nick Dyereb433352016-04-25 14:18:52 -07001198static int mxt_acquire_irq(struct mxt_data *data)
1199{
1200 int error;
1201
1202 enable_irq(data->irq);
1203
1204 error = mxt_process_messages_until_invalid(data);
1205 if (error)
1206 return error;
1207
1208 return 0;
1209}
1210
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001211static int mxt_soft_reset(struct mxt_data *data)
1212{
1213 struct device *dev = &data->client->dev;
1214 int ret = 0;
1215
Nick Dyer885f3fb2015-08-04 16:57:25 -07001216 dev_info(dev, "Resetting device\n");
1217
1218 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001219
1220 reinit_completion(&data->reset_completion);
1221
1222 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
1223 if (ret)
1224 return ret;
1225
Nick Dyer885f3fb2015-08-04 16:57:25 -07001226 /* Ignore CHG line for 100ms after reset */
Sebastian Reichelca1cd362018-05-24 09:33:36 -07001227 msleep(MXT_RESET_INVALID_CHG);
Nick Dyer885f3fb2015-08-04 16:57:25 -07001228
Nick Dyereb433352016-04-25 14:18:52 -07001229 mxt_acquire_irq(data);
Nick Dyer885f3fb2015-08-04 16:57:25 -07001230
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001231 ret = mxt_wait_for_completion(data, &data->reset_completion,
1232 MXT_RESET_TIMEOUT);
1233 if (ret)
1234 return ret;
1235
1236 return 0;
1237}
1238
Nick Dyerc3f78042014-05-18 23:04:46 -07001239static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
1240{
1241 /*
1242 * On failure, CRC is set to 0 and config will always be
1243 * downloaded.
1244 */
1245 data->config_crc = 0;
1246 reinit_completion(&data->crc_completion);
1247
1248 mxt_t6_command(data, cmd, value, true);
1249
1250 /*
1251 * Wait for crc message. On failure, CRC is set to 0 and config will
1252 * always be downloaded.
1253 */
1254 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
1255}
1256
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001257static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
1258{
1259 static const unsigned int crcpoly = 0x80001B;
1260 u32 result;
1261 u32 data_word;
1262
1263 data_word = (secondbyte << 8) | firstbyte;
1264 result = ((*crc << 1) ^ data_word);
1265
1266 if (result & 0x1000000)
1267 result ^= crcpoly;
1268
1269 *crc = result;
1270}
1271
1272static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
1273{
1274 u32 crc = 0;
1275 u8 *ptr = base + start_off;
1276 u8 *last_val = base + end_off - 1;
1277
1278 if (end_off < start_off)
1279 return -EINVAL;
1280
1281 while (ptr < last_val) {
1282 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
1283 ptr += 2;
1284 }
1285
1286 /* if len is odd, fill the last byte with 0 */
1287 if (ptr == last_val)
1288 mxt_calc_crc24(&crc, *ptr, 0);
1289
1290 /* Mask to 24-bit */
1291 crc &= 0x00FFFFFF;
1292
1293 return crc;
1294}
1295
Nick Dyerf865df72018-07-27 11:46:29 -07001296static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001297{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001298 struct device *dev = &data->client->dev;
Nick Dyer50a77c62014-07-23 12:38:48 -07001299 struct mxt_object *object;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001300 unsigned int type, instance, size, byte_offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001301 int offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001302 int ret;
Nick Dyer50a77c62014-07-23 12:38:48 -07001303 int i;
Nick Dyer50a77c62014-07-23 12:38:48 -07001304 u16 reg;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001305 u8 val;
Nick Dyer50a77c62014-07-23 12:38:48 -07001306
Nick Dyerf865df72018-07-27 11:46:29 -07001307 while (cfg->raw_pos < cfg->raw_size) {
Nick Dyer50a77c62014-07-23 12:38:48 -07001308 /* Read type, instance, length */
Nick Dyerf865df72018-07-27 11:46:29 -07001309 ret = sscanf(cfg->raw + cfg->raw_pos, "%x %x %x%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001310 &type, &instance, &size, &offset);
1311 if (ret == 0) {
1312 /* EOF */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001313 break;
Nick Dyer50a77c62014-07-23 12:38:48 -07001314 } else if (ret != 3) {
1315 dev_err(dev, "Bad format: failed to parse object\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001316 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001317 }
Nick Dyerf865df72018-07-27 11:46:29 -07001318 cfg->raw_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001319
1320 object = mxt_get_object(data, type);
1321 if (!object) {
1322 /* Skip object */
1323 for (i = 0; i < size; i++) {
Nick Dyerf865df72018-07-27 11:46:29 -07001324 ret = sscanf(cfg->raw + cfg->raw_pos, "%hhx%n",
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001325 &val, &offset);
1326 if (ret != 1) {
1327 dev_err(dev, "Bad format in T%d at %d\n",
1328 type, i);
1329 return -EINVAL;
1330 }
Nick Dyerf865df72018-07-27 11:46:29 -07001331 cfg->raw_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001332 }
1333 continue;
1334 }
1335
1336 if (size > mxt_obj_size(object)) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001337 /*
1338 * Either we are in fallback mode due to wrong
1339 * config or config from a later fw version,
1340 * or the file is corrupt or hand-edited.
1341 */
1342 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1343 size - mxt_obj_size(object), type);
1344 } else if (mxt_obj_size(object) > size) {
1345 /*
1346 * If firmware is upgraded, new bytes may be added to
1347 * end of objects. It is generally forward compatible
1348 * to zero these bytes - previous behaviour will be
1349 * retained. However this does invalidate the CRC and
1350 * will force fallback mode until the configuration is
1351 * updated. We warn here but do nothing else - the
1352 * malloc has zeroed the entire configuration.
1353 */
1354 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1355 mxt_obj_size(object) - size, type);
Nick Dyer50a77c62014-07-23 12:38:48 -07001356 }
1357
1358 if (instance >= mxt_obj_instances(object)) {
1359 dev_err(dev, "Object instances exceeded!\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001360 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001361 }
1362
1363 reg = object->start_address + mxt_obj_size(object) * instance;
1364
1365 for (i = 0; i < size; i++) {
Nick Dyerf865df72018-07-27 11:46:29 -07001366 ret = sscanf(cfg->raw + cfg->raw_pos, "%hhx%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001367 &val,
1368 &offset);
1369 if (ret != 1) {
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001370 dev_err(dev, "Bad format in T%d at %d\n",
1371 type, i);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001372 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001373 }
Nick Dyerf865df72018-07-27 11:46:29 -07001374 cfg->raw_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001375
1376 if (i > mxt_obj_size(object))
1377 continue;
1378
Nick Dyerf865df72018-07-27 11:46:29 -07001379 byte_offset = reg + i - cfg->start_ofs;
Nick Dyer50a77c62014-07-23 12:38:48 -07001380
Nick Dyerf865df72018-07-27 11:46:29 -07001381 if (byte_offset >= 0 && byte_offset < cfg->mem_size) {
1382 *(cfg->mem + byte_offset) = val;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001383 } else {
1384 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1385 reg, object->type, byte_offset);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001386 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001387 }
1388 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001389 }
1390
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001391 return 0;
1392}
1393
Nick Dyerf865df72018-07-27 11:46:29 -07001394static int mxt_upload_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001395{
1396 unsigned int byte_offset = 0;
1397 int error;
1398
1399 /* Write configuration as blocks */
Nick Dyerf865df72018-07-27 11:46:29 -07001400 while (byte_offset < cfg->mem_size) {
1401 unsigned int size = cfg->mem_size - byte_offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001402
1403 if (size > MXT_MAX_BLOCK_WRITE)
1404 size = MXT_MAX_BLOCK_WRITE;
1405
1406 error = __mxt_write_reg(data->client,
Nick Dyerf865df72018-07-27 11:46:29 -07001407 cfg->start_ofs + byte_offset,
1408 size, cfg->mem + byte_offset);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001409 if (error) {
1410 dev_err(&data->client->dev,
1411 "Config write error, ret=%d\n", error);
1412 return error;
1413 }
1414
1415 byte_offset += size;
1416 }
1417
1418 return 0;
1419}
1420
Nick Dyer7f3884f72015-08-04 16:36:29 -07001421static int mxt_init_t7_power_cfg(struct mxt_data *data);
1422
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001423/*
1424 * mxt_update_cfg - download configuration to chip
1425 *
1426 * Atmel Raw Config File Format
1427 *
1428 * The first four lines of the raw config file contain:
1429 * 1) Version
1430 * 2) Chip ID Information (first 7 bytes of device memory)
1431 * 3) Chip Information Block 24-bit CRC Checksum
1432 * 4) Chip Configuration 24-bit CRC Checksum
1433 *
1434 * The rest of the file consists of one line per object instance:
1435 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1436 *
1437 * <TYPE> - 2-byte object type as hex
1438 * <INSTANCE> - 2-byte object instance number as hex
1439 * <SIZE> - 2-byte object size as hex
1440 * <CONTENTS> - array of <SIZE> 1-byte hex values
1441 */
Nick Dyerf865df72018-07-27 11:46:29 -07001442static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw)
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001443{
1444 struct device *dev = &data->client->dev;
Nick Dyerf865df72018-07-27 11:46:29 -07001445 struct mxt_cfg cfg;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001446 int ret;
1447 int offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001448 int i;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001449 u32 info_crc, config_crc, calculated_crc;
Nick Dyer15082bd2018-07-27 11:45:47 -07001450 u16 crc_start = 0;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001451
Nick Dyera4891f12018-07-27 11:46:46 -07001452 /* Make zero terminated copy of the OBP_RAW file */
1453 cfg.raw = kmemdup_nul(fw->data, fw->size, GFP_KERNEL);
1454 if (!cfg.raw)
1455 return -ENOMEM;
1456
Nick Dyerf865df72018-07-27 11:46:29 -07001457 cfg.raw_size = fw->size;
1458
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001459 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
1460
Nick Dyerf865df72018-07-27 11:46:29 -07001461 if (strncmp(cfg.raw, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001462 dev_err(dev, "Unrecognised config file\n");
Nick Dyera4891f12018-07-27 11:46:46 -07001463 ret = -EINVAL;
1464 goto release_raw;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001465 }
1466
Nick Dyerf865df72018-07-27 11:46:29 -07001467 cfg.raw_pos = strlen(MXT_CFG_MAGIC);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001468
1469 /* Load information block and check */
1470 for (i = 0; i < sizeof(struct mxt_info); i++) {
Nick Dyerf865df72018-07-27 11:46:29 -07001471 ret = sscanf(cfg.raw + cfg.raw_pos, "%hhx%n",
1472 (unsigned char *)&cfg.info + i,
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001473 &offset);
1474 if (ret != 1) {
1475 dev_err(dev, "Bad format\n");
Nick Dyera4891f12018-07-27 11:46:46 -07001476 ret = -EINVAL;
1477 goto release_raw;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001478 }
1479
Nick Dyerf865df72018-07-27 11:46:29 -07001480 cfg.raw_pos += offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001481 }
1482
Nick Dyerf865df72018-07-27 11:46:29 -07001483 if (cfg.info.family_id != data->info->family_id) {
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001484 dev_err(dev, "Family ID mismatch!\n");
Nick Dyera4891f12018-07-27 11:46:46 -07001485 ret = -EINVAL;
1486 goto release_raw;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001487 }
1488
Nick Dyerf865df72018-07-27 11:46:29 -07001489 if (cfg.info.variant_id != data->info->variant_id) {
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001490 dev_err(dev, "Variant ID mismatch!\n");
Nick Dyera4891f12018-07-27 11:46:46 -07001491 ret = -EINVAL;
1492 goto release_raw;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001493 }
1494
1495 /* Read CRCs */
Nick Dyerf865df72018-07-27 11:46:29 -07001496 ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &info_crc, &offset);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001497 if (ret != 1) {
1498 dev_err(dev, "Bad format: failed to parse Info CRC\n");
Nick Dyera4891f12018-07-27 11:46:46 -07001499 ret = -EINVAL;
1500 goto release_raw;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001501 }
Nick Dyerf865df72018-07-27 11:46:29 -07001502 cfg.raw_pos += offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001503
Nick Dyerf865df72018-07-27 11:46:29 -07001504 ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &config_crc, &offset);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001505 if (ret != 1) {
1506 dev_err(dev, "Bad format: failed to parse Config CRC\n");
Nick Dyera4891f12018-07-27 11:46:46 -07001507 ret = -EINVAL;
1508 goto release_raw;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001509 }
Nick Dyerf865df72018-07-27 11:46:29 -07001510 cfg.raw_pos += offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001511
1512 /*
1513 * The Info Block CRC is calculated over mxt_info and the object
1514 * table. If it does not match then we are trying to load the
1515 * configuration from a different chip or firmware version, so
1516 * the configuration CRC is invalid anyway.
1517 */
1518 if (info_crc == data->info_crc) {
1519 if (config_crc == 0 || data->config_crc == 0) {
1520 dev_info(dev, "CRC zero, attempting to apply config\n");
1521 } else if (config_crc == data->config_crc) {
1522 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
1523 data->config_crc);
1524 return 0;
1525 } else {
1526 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1527 data->config_crc, config_crc);
1528 }
1529 } else {
1530 dev_warn(dev,
1531 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1532 data->info_crc, info_crc);
1533 }
1534
1535 /* Malloc memory to store configuration */
Nick Dyerf865df72018-07-27 11:46:29 -07001536 cfg.start_ofs = MXT_OBJECT_START +
Nick Dyer068bdb62018-05-01 11:40:18 -07001537 data->info->object_num * sizeof(struct mxt_object) +
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001538 MXT_INFO_CHECKSUM_SIZE;
Nick Dyerf865df72018-07-27 11:46:29 -07001539 cfg.mem_size = data->mem_size - cfg.start_ofs;
1540 cfg.mem = kzalloc(cfg.mem_size, GFP_KERNEL);
Nick Dyera4891f12018-07-27 11:46:46 -07001541 if (!cfg.mem) {
1542 ret = -ENOMEM;
1543 goto release_raw;
1544 }
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001545
Nick Dyerf865df72018-07-27 11:46:29 -07001546 ret = mxt_prepare_cfg_mem(data, &cfg);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001547 if (ret)
1548 goto release_mem;
1549
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001550 /* Calculate crc of the received configs (not the raw config file) */
Nick Dyer15082bd2018-07-27 11:45:47 -07001551 if (data->T71_address)
1552 crc_start = data->T71_address;
1553 else if (data->T7_address)
1554 crc_start = data->T7_address;
1555 else
1556 dev_warn(dev, "Could not find CRC start\n");
1557
Nick Dyerf865df72018-07-27 11:46:29 -07001558 if (crc_start > cfg.start_ofs) {
1559 calculated_crc = mxt_calculate_crc(cfg.mem,
1560 crc_start - cfg.start_ofs,
1561 cfg.mem_size);
Nick Dyer15082bd2018-07-27 11:45:47 -07001562
1563 if (config_crc > 0 && config_crc != calculated_crc)
1564 dev_warn(dev, "Config CRC in file inconsistent, calculated=%06X, file=%06X\n",
1565 calculated_crc, config_crc);
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001566 }
1567
Nick Dyerf865df72018-07-27 11:46:29 -07001568 ret = mxt_upload_cfg_mem(data, &cfg);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001569 if (ret)
1570 goto release_mem;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001571
Nick Dyerc3f78042014-05-18 23:04:46 -07001572 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1573
1574 ret = mxt_soft_reset(data);
1575 if (ret)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001576 goto release_mem;
Nick Dyerc3f78042014-05-18 23:04:46 -07001577
1578 dev_info(dev, "Config successfully updated\n");
1579
Nick Dyer7f3884f72015-08-04 16:36:29 -07001580 /* T7 config may have changed */
1581 mxt_init_t7_power_cfg(data);
1582
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001583release_mem:
Nick Dyerf865df72018-07-27 11:46:29 -07001584 kfree(cfg.mem);
Sanjeev Chugh1e3c3362018-12-28 17:04:31 -08001585release_raw:
1586 kfree(cfg.raw);
Nick Dyer50a77c62014-07-23 12:38:48 -07001587 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001588}
1589
Stephen Warren58e4aeee2014-09-10 10:01:10 -07001590static void mxt_free_input_device(struct mxt_data *data)
1591{
1592 if (data->input_dev) {
1593 input_unregister_device(data->input_dev);
1594 data->input_dev = NULL;
1595 }
1596}
1597
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001598static void mxt_free_object_table(struct mxt_data *data)
1599{
Nick Dyerecfdd7e2016-07-18 18:10:32 -03001600#ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1601 video_unregister_device(&data->dbg.vdev);
1602 v4l2_device_unregister(&data->dbg.v4l2);
1603#endif
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001604 data->object_table = NULL;
Nick Dyer068bdb62018-05-01 11:40:18 -07001605 data->info = NULL;
1606 kfree(data->raw_info_block);
1607 data->raw_info_block = NULL;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001608 kfree(data->msg_buf);
1609 data->msg_buf = NULL;
Nick Dyerb9b05a82014-07-23 12:48:13 -07001610 data->T5_address = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001611 data->T5_msg_size = 0;
1612 data->T6_reportid = 0;
1613 data->T7_address = 0;
Nick Dyer15082bd2018-07-27 11:45:47 -07001614 data->T71_address = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001615 data->T9_reportid_min = 0;
1616 data->T9_reportid_max = 0;
1617 data->T19_reportid = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001618 data->T44_address = 0;
Nick Dyerb23157d2015-04-06 11:25:13 -07001619 data->T100_reportid_min = 0;
1620 data->T100_reportid_max = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001621 data->max_reportid = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001622}
1623
Nick Dyer068bdb62018-05-01 11:40:18 -07001624static int mxt_parse_object_table(struct mxt_data *data,
1625 struct mxt_object *object_table)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001626{
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001627 struct i2c_client *client = data->client;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001628 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001629 u8 reportid;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001630 u16 end_address;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001631
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001632 /* Valid Report IDs start counting from 1 */
1633 reportid = 1;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001634 data->mem_size = 0;
Nick Dyer068bdb62018-05-01 11:40:18 -07001635 for (i = 0; i < data->info->object_num; i++) {
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001636 struct mxt_object *object = object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001637 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001638
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001639 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001640
1641 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001642 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001643 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001644 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001645 max_id = reportid - 1;
1646 } else {
1647 min_id = 0;
1648 max_id = 0;
1649 }
1650
1651 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001652 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001653 object->type, object->start_address,
1654 mxt_obj_size(object), mxt_obj_instances(object),
1655 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001656
1657 switch (object->type) {
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001658 case MXT_GEN_MESSAGE_T5:
Nick Dyer068bdb62018-05-01 11:40:18 -07001659 if (data->info->family_id == 0x80 &&
1660 data->info->version < 0x20) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001661 /*
Nick Dyer437d4f32014-08-07 09:56:01 -07001662 * On mXT224 firmware versions prior to V2.0
1663 * read and discard unused CRC byte otherwise
1664 * DMA reads are misaligned.
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001665 */
1666 data->T5_msg_size = mxt_obj_size(object);
1667 } else {
1668 /* CRC not enabled, so skip last byte */
1669 data->T5_msg_size = mxt_obj_size(object) - 1;
1670 }
Nick Dyerb9b05a82014-07-23 12:48:13 -07001671 data->T5_address = object->start_address;
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001672 break;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001673 case MXT_GEN_COMMAND_T6:
1674 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001675 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001676 break;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001677 case MXT_GEN_POWER_T7:
1678 data->T7_address = object->start_address;
1679 break;
Nick Dyer15082bd2018-07-27 11:45:47 -07001680 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71:
1681 data->T71_address = object->start_address;
1682 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001683 case MXT_TOUCH_MULTI_T9:
Nick Dyerb23157d2015-04-06 11:25:13 -07001684 data->multitouch = MXT_TOUCH_MULTI_T9;
Nick Dyer36f5d9e2018-07-27 11:44:20 -07001685 /* Only handle messages from first T9 instance */
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001686 data->T9_reportid_min = min_id;
Nick Dyer36f5d9e2018-07-27 11:44:20 -07001687 data->T9_reportid_max = min_id +
1688 object->num_report_ids - 1;
1689 data->num_touchids = object->num_report_ids;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001690 break;
1691 case MXT_SPT_MESSAGECOUNT_T44:
1692 data->T44_address = object->start_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001693 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001694 case MXT_SPT_GPIOPWM_T19:
1695 data->T19_reportid = min_id;
1696 break;
Nick Dyerb23157d2015-04-06 11:25:13 -07001697 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1698 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
1699 data->T100_reportid_min = min_id;
1700 data->T100_reportid_max = max_id;
1701 /* first two report IDs reserved */
1702 data->num_touchids = object->num_report_ids - 2;
1703 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001704 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001705
1706 end_address = object->start_address
1707 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1708
1709 if (end_address >= data->mem_size)
1710 data->mem_size = end_address + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001711 }
1712
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001713 /* Store maximum reportid */
1714 data->max_reportid = reportid;
1715
1716 /* If T44 exists, T5 position has to be directly after */
1717 if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
1718 dev_err(&client->dev, "Invalid T44 position\n");
Nick Dyer068bdb62018-05-01 11:40:18 -07001719 return -EINVAL;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001720 }
1721
1722 data->msg_buf = kcalloc(data->max_reportid,
1723 data->T5_msg_size, GFP_KERNEL);
Nick Dyer068bdb62018-05-01 11:40:18 -07001724 if (!data->msg_buf)
1725 return -ENOMEM;
1726
1727 return 0;
1728}
1729
1730static int mxt_read_info_block(struct mxt_data *data)
1731{
1732 struct i2c_client *client = data->client;
1733 int error;
1734 size_t size;
1735 void *id_buf, *buf;
1736 uint8_t num_objects;
1737 u32 calculated_crc;
1738 u8 *crc_ptr;
1739
1740 /* If info block already allocated, free it */
1741 if (data->raw_info_block)
1742 mxt_free_object_table(data);
1743
1744 /* Read 7-byte ID information block starting at address 0 */
1745 size = sizeof(struct mxt_info);
1746 id_buf = kzalloc(size, GFP_KERNEL);
1747 if (!id_buf)
1748 return -ENOMEM;
1749
1750 error = __mxt_read_reg(client, 0, size, id_buf);
1751 if (error)
1752 goto err_free_mem;
1753
1754 /* Resize buffer to give space for rest of info block */
1755 num_objects = ((struct mxt_info *)id_buf)->object_num;
1756 size += (num_objects * sizeof(struct mxt_object))
1757 + MXT_INFO_CHECKSUM_SIZE;
1758
1759 buf = krealloc(id_buf, size, GFP_KERNEL);
1760 if (!buf) {
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001761 error = -ENOMEM;
Nick Dyer068bdb62018-05-01 11:40:18 -07001762 goto err_free_mem;
1763 }
1764 id_buf = buf;
1765
1766 /* Read rest of info block */
1767 error = __mxt_read_reg(client, MXT_OBJECT_START,
1768 size - MXT_OBJECT_START,
1769 id_buf + MXT_OBJECT_START);
1770 if (error)
1771 goto err_free_mem;
1772
1773 /* Extract & calculate checksum */
1774 crc_ptr = id_buf + size - MXT_INFO_CHECKSUM_SIZE;
1775 data->info_crc = crc_ptr[0] | (crc_ptr[1] << 8) | (crc_ptr[2] << 16);
1776
1777 calculated_crc = mxt_calculate_crc(id_buf, 0,
1778 size - MXT_INFO_CHECKSUM_SIZE);
1779
1780 /*
1781 * CRC mismatch can be caused by data corruption due to I2C comms
1782 * issue or else device is not using Object Based Protocol (eg i2c-hid)
1783 */
1784 if ((data->info_crc == 0) || (data->info_crc != calculated_crc)) {
1785 dev_err(&client->dev,
1786 "Info Block CRC error calculated=0x%06X read=0x%06X\n",
1787 calculated_crc, data->info_crc);
1788 error = -EIO;
1789 goto err_free_mem;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001790 }
1791
Nick Dyer068bdb62018-05-01 11:40:18 -07001792 data->raw_info_block = id_buf;
1793 data->info = (struct mxt_info *)id_buf;
1794
1795 dev_info(&client->dev,
1796 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1797 data->info->family_id, data->info->variant_id,
1798 data->info->version >> 4, data->info->version & 0xf,
1799 data->info->build, data->info->object_num);
1800
1801 /* Parse object table information */
1802 error = mxt_parse_object_table(data, id_buf + MXT_OBJECT_START);
1803 if (error) {
1804 dev_err(&client->dev, "Error %d parsing object table\n", error);
1805 mxt_free_object_table(data);
1806 goto err_free_mem;
1807 }
1808
1809 data->object_table = (struct mxt_object *)(id_buf + MXT_OBJECT_START);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001810
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001811 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001812
Nick Dyer068bdb62018-05-01 11:40:18 -07001813err_free_mem:
1814 kfree(id_buf);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001815 return error;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001816}
1817
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001818static int mxt_read_t9_resolution(struct mxt_data *data)
1819{
1820 struct i2c_client *client = data->client;
1821 int error;
1822 struct t9_range range;
1823 unsigned char orient;
1824 struct mxt_object *object;
1825
1826 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1827 if (!object)
1828 return -EINVAL;
1829
1830 error = __mxt_read_reg(client,
Nick Dyer27864892016-07-18 18:10:33 -03001831 object->start_address + MXT_T9_XSIZE,
1832 sizeof(data->xsize), &data->xsize);
1833 if (error)
1834 return error;
1835
1836 error = __mxt_read_reg(client,
1837 object->start_address + MXT_T9_YSIZE,
1838 sizeof(data->ysize), &data->ysize);
1839 if (error)
1840 return error;
1841
1842 error = __mxt_read_reg(client,
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001843 object->start_address + MXT_T9_RANGE,
1844 sizeof(range), &range);
1845 if (error)
1846 return error;
1847
Nick Dyer1c0276d2016-01-10 18:29:34 -08001848 data->max_x = get_unaligned_le16(&range.x);
1849 data->max_y = get_unaligned_le16(&range.y);
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001850
1851 error = __mxt_read_reg(client,
1852 object->start_address + MXT_T9_ORIENT,
1853 1, &orient);
1854 if (error)
1855 return error;
1856
Nick Dyer1c0276d2016-01-10 18:29:34 -08001857 data->xy_switch = orient & MXT_T9_ORIENT_SWITCH;
Nick Dyerde601f72016-07-18 18:10:34 -03001858 data->invertx = orient & MXT_T9_ORIENT_INVERTX;
1859 data->inverty = orient & MXT_T9_ORIENT_INVERTY;
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001860
1861 return 0;
1862}
1863
Nick Dyerb23157d2015-04-06 11:25:13 -07001864static int mxt_read_t100_config(struct mxt_data *data)
1865{
1866 struct i2c_client *client = data->client;
1867 int error;
1868 struct mxt_object *object;
1869 u16 range_x, range_y;
1870 u8 cfg, tchaux;
1871 u8 aux;
1872
1873 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
1874 if (!object)
1875 return -EINVAL;
1876
Nick Dyer1c0276d2016-01-10 18:29:34 -08001877 /* read touchscreen dimensions */
Nick Dyerb23157d2015-04-06 11:25:13 -07001878 error = __mxt_read_reg(client,
1879 object->start_address + MXT_T100_XRANGE,
1880 sizeof(range_x), &range_x);
1881 if (error)
1882 return error;
1883
Nick Dyer1c0276d2016-01-10 18:29:34 -08001884 data->max_x = get_unaligned_le16(&range_x);
Nick Dyerb23157d2015-04-06 11:25:13 -07001885
1886 error = __mxt_read_reg(client,
1887 object->start_address + MXT_T100_YRANGE,
1888 sizeof(range_y), &range_y);
1889 if (error)
1890 return error;
1891
Nick Dyer1c0276d2016-01-10 18:29:34 -08001892 data->max_y = get_unaligned_le16(&range_y);
Nick Dyerb23157d2015-04-06 11:25:13 -07001893
Nick Dyer27864892016-07-18 18:10:33 -03001894 error = __mxt_read_reg(client,
1895 object->start_address + MXT_T100_XSIZE,
1896 sizeof(data->xsize), &data->xsize);
1897 if (error)
1898 return error;
1899
1900 error = __mxt_read_reg(client,
1901 object->start_address + MXT_T100_YSIZE,
1902 sizeof(data->ysize), &data->ysize);
1903 if (error)
1904 return error;
1905
Nick Dyer1c0276d2016-01-10 18:29:34 -08001906 /* read orientation config */
Nick Dyerb23157d2015-04-06 11:25:13 -07001907 error = __mxt_read_reg(client,
1908 object->start_address + MXT_T100_CFG1,
1909 1, &cfg);
1910 if (error)
1911 return error;
1912
Nick Dyer1c0276d2016-01-10 18:29:34 -08001913 data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY;
Nick Dyerde601f72016-07-18 18:10:34 -03001914 data->invertx = cfg & MXT_T100_CFG_INVERTX;
1915 data->inverty = cfg & MXT_T100_CFG_INVERTY;
Nick Dyer1c0276d2016-01-10 18:29:34 -08001916
1917 /* allocate aux bytes */
Nick Dyerb23157d2015-04-06 11:25:13 -07001918 error = __mxt_read_reg(client,
1919 object->start_address + MXT_T100_TCHAUX,
1920 1, &tchaux);
1921 if (error)
1922 return error;
1923
Nick Dyerb23157d2015-04-06 11:25:13 -07001924 aux = 6;
1925
1926 if (tchaux & MXT_T100_TCHAUX_VECT)
1927 data->t100_aux_vect = aux++;
1928
1929 if (tchaux & MXT_T100_TCHAUX_AMPL)
1930 data->t100_aux_ampl = aux++;
1931
1932 if (tchaux & MXT_T100_TCHAUX_AREA)
1933 data->t100_aux_area = aux++;
1934
1935 dev_dbg(&client->dev,
1936 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1937 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
1938
Nick Dyerb23157d2015-04-06 11:25:13 -07001939 return 0;
1940}
1941
Nick Dyer7a53d602014-07-23 12:21:26 -07001942static int mxt_input_open(struct input_dev *dev);
1943static void mxt_input_close(struct input_dev *dev);
1944
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001945static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
1946 struct mxt_data *data)
1947{
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001948 int i;
1949
1950 input_dev->name = "Atmel maXTouch Touchpad";
1951
1952 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1953
1954 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1955 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1956 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1957 MXT_PIXELS_PER_MM);
1958 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1959 MXT_PIXELS_PER_MM);
1960
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07001961 for (i = 0; i < data->t19_num_keys; i++)
1962 if (data->t19_keymap[i] != KEY_RESERVED)
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001963 input_set_capability(input_dev, EV_KEY,
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07001964 data->t19_keymap[i]);
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001965}
1966
Nick Dyerb23157d2015-04-06 11:25:13 -07001967static int mxt_initialize_input_device(struct mxt_data *data)
Nick Dyer7a53d602014-07-23 12:21:26 -07001968{
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001969 struct device *dev = &data->client->dev;
Nick Dyer7a53d602014-07-23 12:21:26 -07001970 struct input_dev *input_dev;
1971 int error;
1972 unsigned int num_mt_slots;
1973 unsigned int mt_flags = 0;
Nick Dyer7a53d602014-07-23 12:21:26 -07001974
Nick Dyerb23157d2015-04-06 11:25:13 -07001975 switch (data->multitouch) {
1976 case MXT_TOUCH_MULTI_T9:
1977 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1978 error = mxt_read_t9_resolution(data);
1979 if (error)
1980 dev_warn(dev, "Failed to initialize T9 resolution\n");
1981 break;
1982
1983 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1984 num_mt_slots = data->num_touchids;
1985 error = mxt_read_t100_config(data);
1986 if (error)
1987 dev_warn(dev, "Failed to read T100 config\n");
1988 break;
1989
1990 default:
1991 dev_err(dev, "Invalid multitouch object\n");
1992 return -EINVAL;
1993 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001994
Nick Dyer1c0276d2016-01-10 18:29:34 -08001995 /* Handle default values and orientation switch */
1996 if (data->max_x == 0)
1997 data->max_x = 1023;
1998
1999 if (data->max_y == 0)
2000 data->max_y = 1023;
2001
2002 if (data->xy_switch)
2003 swap(data->max_x, data->max_y);
2004
2005 dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
2006
2007 /* Register input device */
Nick Dyer7a53d602014-07-23 12:21:26 -07002008 input_dev = input_allocate_device();
Nick Dyer01cc75f2018-07-27 11:45:30 -07002009 if (!input_dev)
Nick Dyer7a53d602014-07-23 12:21:26 -07002010 return -ENOMEM;
Nick Dyer7a53d602014-07-23 12:21:26 -07002011
2012 input_dev->name = "Atmel maXTouch Touchscreen";
2013 input_dev->phys = data->phys;
2014 input_dev->id.bustype = BUS_I2C;
2015 input_dev->dev.parent = dev;
2016 input_dev->open = mxt_input_open;
2017 input_dev->close = mxt_input_close;
2018
Nick Dyerb23157d2015-04-06 11:25:13 -07002019 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
Nick Dyer7a53d602014-07-23 12:21:26 -07002020
Nick Dyer7a53d602014-07-23 12:21:26 -07002021 /* For single touch */
Nick Dyerb23157d2015-04-06 11:25:13 -07002022 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
2023 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
2024
2025 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2026 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2027 data->t100_aux_ampl)) {
2028 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
2029 }
Nick Dyer7a53d602014-07-23 12:21:26 -07002030
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07002031 /* If device has buttons we assume it is a touchpad */
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002032 if (data->t19_num_keys) {
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07002033 mxt_set_up_as_touchpad(input_dev, data);
2034 mt_flags |= INPUT_MT_POINTER;
Nick Dyereafc0c82015-08-04 16:57:40 -07002035 } else {
2036 mt_flags |= INPUT_MT_DIRECT;
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07002037 }
2038
Nick Dyer7a53d602014-07-23 12:21:26 -07002039 /* For multi touch */
Nick Dyer7a53d602014-07-23 12:21:26 -07002040 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
2041 if (error) {
2042 dev_err(dev, "Error %d initialising slots\n", error);
2043 goto err_free_mem;
2044 }
2045
Nick Dyerb23157d2015-04-06 11:25:13 -07002046 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
2047 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
2048 0, MT_TOOL_MAX, 0, 0);
2049 input_set_abs_params(input_dev, ABS_MT_DISTANCE,
2050 MXT_DISTANCE_ACTIVE_TOUCH,
2051 MXT_DISTANCE_HOVERING,
2052 0, 0);
2053 }
2054
Nick Dyer7a53d602014-07-23 12:21:26 -07002055 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
2056 0, data->max_x, 0, 0);
2057 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
2058 0, data->max_y, 0, 0);
Nick Dyerb23157d2015-04-06 11:25:13 -07002059
2060 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2061 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2062 data->t100_aux_area)) {
2063 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
2064 0, MXT_MAX_AREA, 0, 0);
2065 }
2066
2067 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2068 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2069 data->t100_aux_ampl)) {
2070 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
2071 0, 255, 0, 0);
2072 }
2073
2074 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2075 data->t100_aux_vect) {
2076 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2077 0, 255, 0, 0);
2078 }
2079
2080 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
Nick Dyerb23157d2015-04-06 11:25:13 -07002081 data->t100_aux_vect) {
2082 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2083 0, 255, 0, 0);
2084 }
Nick Dyer7a53d602014-07-23 12:21:26 -07002085
2086 input_set_drvdata(input_dev, data);
2087
2088 error = input_register_device(input_dev);
2089 if (error) {
2090 dev_err(dev, "Error %d registering input device\n", error);
2091 goto err_free_mem;
2092 }
2093
2094 data->input_dev = input_dev;
2095
2096 return 0;
2097
2098err_free_mem:
2099 input_free_device(input_dev);
2100 return error;
2101}
2102
Nick Dyer50a77c62014-07-23 12:38:48 -07002103static int mxt_configure_objects(struct mxt_data *data,
2104 const struct firmware *cfg);
2105
2106static void mxt_config_cb(const struct firmware *cfg, void *ctx)
2107{
2108 mxt_configure_objects(ctx, cfg);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07002109 release_firmware(cfg);
Nick Dyer50a77c62014-07-23 12:38:48 -07002110}
2111
Iiro Valkonen7686b102011-02-02 23:21:58 -08002112static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002113{
2114 struct i2c_client *client = data->client;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002115 int recovery_attempts = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002116 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002117
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002118 while (1) {
Nick Dyer068bdb62018-05-01 11:40:18 -07002119 error = mxt_read_info_block(data);
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002120 if (!error)
2121 break;
2122
2123 /* Check bootloader state */
2124 error = mxt_probe_bootloader(data, false);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002125 if (error) {
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002126 dev_info(&client->dev, "Trying alternate bootloader address\n");
2127 error = mxt_probe_bootloader(data, true);
2128 if (error) {
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002129 /* Chip is not in appmode or bootloader mode */
2130 return error;
2131 }
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002132 }
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002133
2134 /* OK, we are in bootloader, see if we can recover */
2135 if (++recovery_attempts > 1) {
2136 dev_err(&client->dev, "Could not recover from bootloader mode\n");
2137 /*
2138 * We can reflash from this state, so do not
2139 * abort initialization.
2140 */
2141 data->in_bootloader = true;
2142 return 0;
2143 }
2144
2145 /* Attempt to exit bootloader into app mode */
2146 mxt_send_bootloader_cmd(data, false);
2147 msleep(MXT_FW_RESET_TIME);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002148 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002149
Dmitry Torokhov041fa152014-08-11 10:58:29 -07002150 error = mxt_acquire_irq(data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002151 if (error)
Nick Dyer068bdb62018-05-01 11:40:18 -07002152 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002153
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002154 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
2155 &client->dev, GFP_KERNEL, data,
2156 mxt_config_cb);
2157 if (error) {
2158 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
2159 error);
Nick Dyer068bdb62018-05-01 11:40:18 -07002160 return error;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002161 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002162
2163 return 0;
2164}
2165
Nick Dyer7f3884f72015-08-04 16:36:29 -07002166static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
2167{
2168 struct device *dev = &data->client->dev;
2169 int error;
2170 struct t7_config *new_config;
2171 struct t7_config deepsleep = { .active = 0, .idle = 0 };
2172
2173 if (sleep == MXT_POWER_CFG_DEEPSLEEP)
2174 new_config = &deepsleep;
2175 else
2176 new_config = &data->t7_cfg;
2177
2178 error = __mxt_write_reg(data->client, data->T7_address,
2179 sizeof(data->t7_cfg), new_config);
2180 if (error)
2181 return error;
2182
2183 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
2184 new_config->active, new_config->idle);
2185
2186 return 0;
2187}
2188
2189static int mxt_init_t7_power_cfg(struct mxt_data *data)
2190{
2191 struct device *dev = &data->client->dev;
2192 int error;
2193 bool retry = false;
2194
2195recheck:
2196 error = __mxt_read_reg(data->client, data->T7_address,
2197 sizeof(data->t7_cfg), &data->t7_cfg);
2198 if (error)
2199 return error;
2200
2201 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
2202 if (!retry) {
2203 dev_dbg(dev, "T7 cfg zero, resetting\n");
2204 mxt_soft_reset(data);
2205 retry = true;
2206 goto recheck;
2207 } else {
2208 dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
2209 data->t7_cfg.active = 20;
2210 data->t7_cfg.idle = 100;
2211 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2212 }
2213 }
2214
2215 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
2216 data->t7_cfg.active, data->t7_cfg.idle);
2217 return 0;
2218}
2219
Nick Dyerd6a39402016-07-18 18:10:31 -03002220#ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2221static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x,
2222 unsigned int y)
2223{
Nick Dyer068bdb62018-05-01 11:40:18 -07002224 struct mxt_info *info = data->info;
Nick Dyerd6a39402016-07-18 18:10:31 -03002225 struct mxt_dbg *dbg = &data->dbg;
2226 unsigned int ofs, page;
Nick Dyer566d5332016-07-18 18:10:35 -03002227 unsigned int col = 0;
2228 unsigned int col_width;
Nick Dyerd6a39402016-07-18 18:10:31 -03002229
Nick Dyer566d5332016-07-18 18:10:35 -03002230 if (info->family_id == MXT_FAMILY_1386) {
2231 col_width = info->matrix_ysize / MXT1386_COLUMNS;
2232 col = y / col_width;
2233 y = y % col_width;
2234 } else {
2235 col_width = info->matrix_ysize;
2236 }
2237
2238 ofs = (y + (x * col_width)) * sizeof(u16);
Nick Dyerd6a39402016-07-18 18:10:31 -03002239 page = ofs / MXT_DIAGNOSTIC_SIZE;
2240 ofs %= MXT_DIAGNOSTIC_SIZE;
2241
Nick Dyer566d5332016-07-18 18:10:35 -03002242 if (info->family_id == MXT_FAMILY_1386)
2243 page += col * MXT1386_PAGES_PER_COLUMN;
2244
Nick Dyerd6a39402016-07-18 18:10:31 -03002245 return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]);
2246}
2247
2248static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf)
2249{
2250 struct mxt_dbg *dbg = &data->dbg;
2251 unsigned int x = 0;
2252 unsigned int y = 0;
Nick Dyerde601f72016-07-18 18:10:34 -03002253 unsigned int i, rx, ry;
Nick Dyerd6a39402016-07-18 18:10:31 -03002254
2255 for (i = 0; i < dbg->t37_nodes; i++) {
Nick Dyerde601f72016-07-18 18:10:34 -03002256 /* Handle orientation */
2257 rx = data->xy_switch ? y : x;
2258 ry = data->xy_switch ? x : y;
2259 rx = data->invertx ? (data->xsize - 1 - rx) : rx;
2260 ry = data->inverty ? (data->ysize - 1 - ry) : ry;
2261
2262 outbuf[i] = mxt_get_debug_value(data, rx, ry);
Nick Dyerd6a39402016-07-18 18:10:31 -03002263
2264 /* Next value */
Nick Dyerde601f72016-07-18 18:10:34 -03002265 if (++x >= (data->xy_switch ? data->ysize : data->xsize)) {
Nick Dyerd6a39402016-07-18 18:10:31 -03002266 x = 0;
2267 y++;
2268 }
2269 }
2270
2271 return 0;
2272}
2273
2274static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode,
2275 u16 *outbuf)
2276{
2277 struct mxt_dbg *dbg = &data->dbg;
2278 int retries = 0;
2279 int page;
2280 int ret;
2281 u8 cmd = mode;
2282 struct t37_debug *p;
2283 u8 cmd_poll;
2284
2285 for (page = 0; page < dbg->t37_pages; page++) {
2286 p = dbg->t37_buf + page;
2287
2288 ret = mxt_write_reg(data->client, dbg->diag_cmd_address,
2289 cmd);
2290 if (ret)
2291 return ret;
2292
2293 retries = 0;
2294 msleep(20);
2295wait_cmd:
2296 /* Read back command byte */
2297 ret = __mxt_read_reg(data->client, dbg->diag_cmd_address,
2298 sizeof(cmd_poll), &cmd_poll);
2299 if (ret)
2300 return ret;
2301
2302 /* Field is cleared once the command has been processed */
2303 if (cmd_poll) {
2304 if (retries++ > 100)
2305 return -EINVAL;
2306
2307 msleep(20);
2308 goto wait_cmd;
2309 }
2310
2311 /* Read T37 page */
2312 ret = __mxt_read_reg(data->client, dbg->t37_address,
2313 sizeof(struct t37_debug), p);
2314 if (ret)
2315 return ret;
2316
2317 if (p->mode != mode || p->page != page) {
2318 dev_err(&data->client->dev, "T37 page mismatch\n");
2319 return -EINVAL;
2320 }
2321
2322 dev_dbg(&data->client->dev, "%s page:%d retries:%d\n",
2323 __func__, page, retries);
2324
2325 /* For remaining pages, write PAGEUP rather than mode */
2326 cmd = MXT_DIAGNOSTIC_PAGEUP;
2327 }
2328
2329 return mxt_convert_debug_pages(data, outbuf);
2330}
2331
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002332static int mxt_queue_setup(struct vb2_queue *q,
2333 unsigned int *nbuffers, unsigned int *nplanes,
2334 unsigned int sizes[], struct device *alloc_devs[])
2335{
2336 struct mxt_data *data = q->drv_priv;
2337 size_t size = data->dbg.t37_nodes * sizeof(u16);
2338
2339 if (*nplanes)
2340 return sizes[0] < size ? -EINVAL : 0;
2341
2342 *nplanes = 1;
2343 sizes[0] = size;
2344
2345 return 0;
2346}
2347
2348static void mxt_buffer_queue(struct vb2_buffer *vb)
2349{
2350 struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue);
2351 u16 *ptr;
2352 int ret;
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002353 u8 mode;
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002354
2355 ptr = vb2_plane_vaddr(vb, 0);
2356 if (!ptr) {
2357 dev_err(&data->client->dev, "Error acquiring frame ptr\n");
2358 goto fault;
2359 }
2360
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002361 switch (data->dbg.input) {
2362 case MXT_V4L_INPUT_DELTAS:
2363 default:
2364 mode = MXT_DIAGNOSTIC_DELTAS;
2365 break;
2366
2367 case MXT_V4L_INPUT_REFS:
2368 mode = MXT_DIAGNOSTIC_REFS;
2369 break;
2370 }
2371
2372 ret = mxt_read_diagnostic_debug(data, mode, ptr);
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002373 if (ret)
2374 goto fault;
2375
2376 vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16));
2377 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2378 return;
2379
2380fault:
2381 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2382}
2383
2384/* V4L2 structures */
2385static const struct vb2_ops mxt_queue_ops = {
2386 .queue_setup = mxt_queue_setup,
2387 .buf_queue = mxt_buffer_queue,
2388 .wait_prepare = vb2_ops_wait_prepare,
2389 .wait_finish = vb2_ops_wait_finish,
2390};
2391
2392static const struct vb2_queue mxt_queue = {
2393 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2394 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
2395 .buf_struct_size = sizeof(struct mxt_vb2_buffer),
2396 .ops = &mxt_queue_ops,
2397 .mem_ops = &vb2_vmalloc_memops,
2398 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
2399 .min_buffers_needed = 1,
2400};
2401
2402static int mxt_vidioc_querycap(struct file *file, void *priv,
2403 struct v4l2_capability *cap)
2404{
2405 struct mxt_data *data = video_drvdata(file);
2406
2407 strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver));
2408 strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card));
2409 snprintf(cap->bus_info, sizeof(cap->bus_info),
2410 "I2C:%s", dev_name(&data->client->dev));
2411 return 0;
2412}
2413
2414static int mxt_vidioc_enum_input(struct file *file, void *priv,
2415 struct v4l2_input *i)
2416{
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002417 if (i->index >= MXT_V4L_INPUT_MAX)
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002418 return -EINVAL;
2419
2420 i->type = V4L2_INPUT_TYPE_TOUCH;
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002421
2422 switch (i->index) {
2423 case MXT_V4L_INPUT_REFS:
2424 strlcpy(i->name, "Mutual Capacitance References",
2425 sizeof(i->name));
2426 break;
2427 case MXT_V4L_INPUT_DELTAS:
2428 strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name));
2429 break;
2430 }
2431
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002432 return 0;
2433}
2434
2435static int mxt_set_input(struct mxt_data *data, unsigned int i)
2436{
2437 struct v4l2_pix_format *f = &data->dbg.format;
2438
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002439 if (i >= MXT_V4L_INPUT_MAX)
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002440 return -EINVAL;
2441
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002442 if (i == MXT_V4L_INPUT_DELTAS)
2443 f->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2444 else
2445 f->pixelformat = V4L2_TCH_FMT_TU16;
2446
Nick Dyerde601f72016-07-18 18:10:34 -03002447 f->width = data->xy_switch ? data->ysize : data->xsize;
2448 f->height = data->xy_switch ? data->xsize : data->ysize;
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002449 f->field = V4L2_FIELD_NONE;
2450 f->colorspace = V4L2_COLORSPACE_RAW;
2451 f->bytesperline = f->width * sizeof(u16);
2452 f->sizeimage = f->width * f->height * sizeof(u16);
2453
2454 data->dbg.input = i;
2455
2456 return 0;
2457}
2458
2459static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i)
2460{
2461 return mxt_set_input(video_drvdata(file), i);
2462}
2463
2464static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
2465{
2466 struct mxt_data *data = video_drvdata(file);
2467
2468 *i = data->dbg.input;
2469
2470 return 0;
2471}
2472
2473static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f)
2474{
2475 struct mxt_data *data = video_drvdata(file);
2476
2477 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2478 f->fmt.pix = data->dbg.format;
2479
2480 return 0;
2481}
2482
2483static int mxt_vidioc_enum_fmt(struct file *file, void *priv,
2484 struct v4l2_fmtdesc *fmt)
2485{
2486 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2487 return -EINVAL;
2488
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002489 switch (fmt->index) {
2490 case 0:
2491 fmt->pixelformat = V4L2_TCH_FMT_TU16;
2492 break;
2493
2494 case 1:
2495 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2496 break;
2497
2498 default:
2499 return -EINVAL;
2500 }
2501
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002502 return 0;
2503}
2504
2505static int mxt_vidioc_g_parm(struct file *file, void *fh,
2506 struct v4l2_streamparm *a)
2507{
2508 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2509 return -EINVAL;
2510
2511 a->parm.capture.readbuffers = 1;
2512 a->parm.capture.timeperframe.numerator = 1;
2513 a->parm.capture.timeperframe.denominator = 10;
2514 return 0;
2515}
2516
2517static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = {
2518 .vidioc_querycap = mxt_vidioc_querycap,
2519
2520 .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt,
2521 .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt,
2522 .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt,
2523 .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt,
2524 .vidioc_g_parm = mxt_vidioc_g_parm,
2525
2526 .vidioc_enum_input = mxt_vidioc_enum_input,
2527 .vidioc_g_input = mxt_vidioc_g_input,
2528 .vidioc_s_input = mxt_vidioc_s_input,
2529
2530 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2531 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2532 .vidioc_querybuf = vb2_ioctl_querybuf,
2533 .vidioc_qbuf = vb2_ioctl_qbuf,
2534 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2535 .vidioc_expbuf = vb2_ioctl_expbuf,
2536
2537 .vidioc_streamon = vb2_ioctl_streamon,
2538 .vidioc_streamoff = vb2_ioctl_streamoff,
2539};
2540
2541static const struct video_device mxt_video_device = {
2542 .name = "Atmel maxTouch",
2543 .fops = &mxt_video_fops,
2544 .ioctl_ops = &mxt_video_ioctl_ops,
2545 .release = video_device_release_empty,
2546 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
2547 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
2548};
2549
Nick Dyerd6a39402016-07-18 18:10:31 -03002550static void mxt_debug_init(struct mxt_data *data)
2551{
Nick Dyer068bdb62018-05-01 11:40:18 -07002552 struct mxt_info *info = data->info;
Nick Dyerd6a39402016-07-18 18:10:31 -03002553 struct mxt_dbg *dbg = &data->dbg;
2554 struct mxt_object *object;
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002555 int error;
Nick Dyerd6a39402016-07-18 18:10:31 -03002556
2557 object = mxt_get_object(data, MXT_GEN_COMMAND_T6);
2558 if (!object)
2559 goto error;
2560
2561 dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC;
2562
2563 object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37);
2564 if (!object)
2565 goto error;
2566
2567 if (mxt_obj_size(object) != sizeof(struct t37_debug)) {
2568 dev_warn(&data->client->dev, "Bad T37 size");
2569 goto error;
2570 }
2571
2572 dbg->t37_address = object->start_address;
2573
2574 /* Calculate size of data and allocate buffer */
Nick Dyer27864892016-07-18 18:10:33 -03002575 dbg->t37_nodes = data->xsize * data->ysize;
Nick Dyer566d5332016-07-18 18:10:35 -03002576
2577 if (info->family_id == MXT_FAMILY_1386)
2578 dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN;
2579 else
2580 dbg->t37_pages = DIV_ROUND_UP(data->xsize *
Guenter Roeckd7ddf152017-01-21 23:46:47 -08002581 info->matrix_ysize *
Nick Dyer566d5332016-07-18 18:10:35 -03002582 sizeof(u16),
2583 sizeof(dbg->t37_buf->data));
Nick Dyerd6a39402016-07-18 18:10:31 -03002584
2585 dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages,
2586 sizeof(struct t37_debug), GFP_KERNEL);
2587 if (!dbg->t37_buf)
2588 goto error;
2589
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002590 /* init channel to zero */
2591 mxt_set_input(data, 0);
2592
2593 /* register video device */
2594 snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts");
2595 error = v4l2_device_register(&data->client->dev, &dbg->v4l2);
2596 if (error)
2597 goto error;
2598
2599 /* initialize the queue */
2600 mutex_init(&dbg->lock);
2601 dbg->queue = mxt_queue;
2602 dbg->queue.drv_priv = data;
2603 dbg->queue.lock = &dbg->lock;
2604 dbg->queue.dev = &data->client->dev;
2605
2606 error = vb2_queue_init(&dbg->queue);
2607 if (error)
2608 goto error_unreg_v4l2;
2609
2610 dbg->vdev = mxt_video_device;
2611 dbg->vdev.v4l2_dev = &dbg->v4l2;
2612 dbg->vdev.lock = &dbg->lock;
2613 dbg->vdev.vfl_dir = VFL_DIR_RX;
2614 dbg->vdev.queue = &dbg->queue;
2615 video_set_drvdata(&dbg->vdev, data);
2616
2617 error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1);
2618 if (error)
2619 goto error_unreg_v4l2;
2620
Nick Dyerd6a39402016-07-18 18:10:31 -03002621 return;
2622
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002623error_unreg_v4l2:
2624 v4l2_device_unregister(&dbg->v4l2);
Nick Dyerd6a39402016-07-18 18:10:31 -03002625error:
2626 dev_warn(&data->client->dev, "Error initializing T37\n");
2627}
2628#else
2629static void mxt_debug_init(struct mxt_data *data)
2630{
2631}
2632#endif
2633
Nick Dyer50a77c62014-07-23 12:38:48 -07002634static int mxt_configure_objects(struct mxt_data *data,
2635 const struct firmware *cfg)
2636{
2637 struct device *dev = &data->client->dev;
Nick Dyer50a77c62014-07-23 12:38:48 -07002638 int error;
2639
Nick Dyer7f3884f72015-08-04 16:36:29 -07002640 error = mxt_init_t7_power_cfg(data);
2641 if (error) {
2642 dev_err(dev, "Failed to initialize power cfg\n");
2643 return error;
2644 }
2645
Nick Dyer50a77c62014-07-23 12:38:48 -07002646 if (cfg) {
2647 error = mxt_update_cfg(data, cfg);
2648 if (error)
2649 dev_warn(dev, "Error %d updating config\n", error);
2650 }
2651
Nick Dyerb23157d2015-04-06 11:25:13 -07002652 if (data->multitouch) {
2653 error = mxt_initialize_input_device(data);
2654 if (error)
2655 return error;
2656 } else {
2657 dev_warn(dev, "No touch object detected\n");
2658 }
Nick Dyer50a77c62014-07-23 12:38:48 -07002659
Nick Dyerd6a39402016-07-18 18:10:31 -03002660 mxt_debug_init(data);
2661
Nick Dyer50a77c62014-07-23 12:38:48 -07002662 return 0;
2663}
2664
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002665/* Firmware Version is returned as Major.Minor.Build */
2666static ssize_t mxt_fw_version_show(struct device *dev,
2667 struct device_attribute *attr, char *buf)
2668{
2669 struct mxt_data *data = dev_get_drvdata(dev);
Nick Dyer068bdb62018-05-01 11:40:18 -07002670 struct mxt_info *info = data->info;
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002671 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
2672 info->version >> 4, info->version & 0xf, info->build);
2673}
2674
2675/* Hardware Version is returned as FamilyID.VariantID */
2676static ssize_t mxt_hw_version_show(struct device *dev,
2677 struct device_attribute *attr, char *buf)
2678{
2679 struct mxt_data *data = dev_get_drvdata(dev);
Nick Dyer068bdb62018-05-01 11:40:18 -07002680 struct mxt_info *info = data->info;
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002681 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
2682 info->family_id, info->variant_id);
2683}
2684
Daniel Kurtz794eb672012-06-28 21:08:10 +08002685static ssize_t mxt_show_instance(char *buf, int count,
2686 struct mxt_object *object, int instance,
2687 const u8 *val)
2688{
2689 int i;
2690
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002691 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002692 count += scnprintf(buf + count, PAGE_SIZE - count,
2693 "Instance %u\n", instance);
2694
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002695 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002696 count += scnprintf(buf + count, PAGE_SIZE - count,
2697 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
2698 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
2699
2700 return count;
2701}
2702
Iiro Valkonen7686b102011-02-02 23:21:58 -08002703static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002704 struct device_attribute *attr, char *buf)
2705{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002706 struct mxt_data *data = dev_get_drvdata(dev);
2707 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002708 int count = 0;
2709 int i, j;
2710 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002711 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002712
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002713 /* Pre-allocate buffer large enough to hold max sized object. */
2714 obuf = kmalloc(256, GFP_KERNEL);
2715 if (!obuf)
2716 return -ENOMEM;
2717
2718 error = 0;
Nick Dyer068bdb62018-05-01 11:40:18 -07002719 for (i = 0; i < data->info->object_num; i++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002720 object = data->object_table + i;
2721
Daniel Kurtz91630952012-06-28 21:08:09 +08002722 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002723 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002724
Daniel Kurtz91630952012-06-28 21:08:09 +08002725 count += scnprintf(buf + count, PAGE_SIZE - count,
2726 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002727
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002728 for (j = 0; j < mxt_obj_instances(object); j++) {
2729 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08002730 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002731
Daniel Kurtz794eb672012-06-28 21:08:10 +08002732 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002733 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002734 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002735
Daniel Kurtz794eb672012-06-28 21:08:10 +08002736 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002737 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002738 }
2739
Daniel Kurtz794eb672012-06-28 21:08:10 +08002740done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002741 kfree(obuf);
2742 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002743}
2744
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002745static int mxt_check_firmware_format(struct device *dev,
2746 const struct firmware *fw)
2747{
2748 unsigned int pos = 0;
2749 char c;
2750
2751 while (pos < fw->size) {
2752 c = *(fw->data + pos);
2753
2754 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
2755 return 0;
2756
2757 pos++;
2758 }
2759
2760 /*
2761 * To convert file try:
2762 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2763 */
2764 dev_err(dev, "Aborting: firmware file must be in binary format\n");
2765
2766 return -EINVAL;
2767}
2768
Iiro Valkonen7686b102011-02-02 23:21:58 -08002769static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002770{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002771 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002772 const struct firmware *fw = NULL;
2773 unsigned int frame_size;
2774 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07002775 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07002776 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002777 int ret;
2778
2779 ret = request_firmware(&fw, fn, dev);
2780 if (ret) {
2781 dev_err(dev, "Unable to open firmware %s\n", fn);
2782 return ret;
2783 }
2784
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002785 /* Check for incorrect enc file */
2786 ret = mxt_check_firmware_format(dev, fw);
2787 if (ret)
2788 goto release_firmware;
2789
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002790 if (!data->in_bootloader) {
2791 /* Change to the bootloader mode */
2792 data->in_bootloader = true;
Benson Leungd79e7e42014-05-18 23:02:52 -07002793
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002794 ret = mxt_t6_command(data, MXT_COMMAND_RESET,
2795 MXT_BOOT_VALUE, false);
2796 if (ret)
2797 goto release_firmware;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002798
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002799 msleep(MXT_RESET_TIME);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002800
2801 /* Do not need to scan since we know family ID */
2802 ret = mxt_lookup_bootloader_address(data, 0);
2803 if (ret)
2804 goto release_firmware;
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002805
2806 mxt_free_input_device(data);
2807 mxt_free_object_table(data);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002808 } else {
2809 enable_irq(data->irq);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002810 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002811
Benson Leungd79e7e42014-05-18 23:02:52 -07002812 reinit_completion(&data->bl_completion);
2813
Nick Dyer385deb92014-07-23 12:42:40 -07002814 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
2815 if (ret) {
2816 /* Bootloader may still be unlocked from previous attempt */
2817 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
2818 if (ret)
2819 goto disable_irq;
2820 } else {
2821 dev_info(dev, "Unlocking bootloader\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002822
Nick Dyer385deb92014-07-23 12:42:40 -07002823 /* Unlock bootloader */
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002824 ret = mxt_send_bootloader_cmd(data, true);
Nick Dyer385deb92014-07-23 12:42:40 -07002825 if (ret)
2826 goto disable_irq;
2827 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002828
2829 while (pos < fw->size) {
Nick Dyer385deb92014-07-23 12:42:40 -07002830 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002831 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07002832 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002833
2834 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
2835
Nick Dyerf943c742014-05-18 23:14:20 -07002836 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002837 frame_size += 2;
2838
2839 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07002840 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
2841 if (ret)
2842 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002843
Nick Dyer385deb92014-07-23 12:42:40 -07002844 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
Nick Dyerf943c742014-05-18 23:14:20 -07002845 if (ret) {
2846 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002847
Nick Dyerf943c742014-05-18 23:14:20 -07002848 /* Back off by 20ms per retry */
2849 msleep(retry * 20);
2850
2851 if (retry > 20) {
2852 dev_err(dev, "Retry count exceeded\n");
2853 goto disable_irq;
2854 }
2855 } else {
2856 retry = 0;
2857 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07002858 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07002859 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002860
Nick Dyerf477c752014-05-18 23:14:45 -07002861 if (frame % 50 == 0)
2862 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
2863 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002864 }
2865
Benson Leunga0434b72014-05-18 23:03:09 -07002866 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002867 ret = mxt_wait_for_completion(data, &data->bl_completion,
2868 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002869 if (ret)
2870 goto disable_irq;
2871
Nick Dyerf477c752014-05-18 23:14:45 -07002872 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
2873
Benson Leunga0434b72014-05-18 23:03:09 -07002874 /*
2875 * Wait for device to reset. Some bootloader versions do not assert
2876 * the CHG line after bootloading has finished, so ignore potential
2877 * errors.
2878 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002879 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002880
Benson Leungd79e7e42014-05-18 23:02:52 -07002881 data->in_bootloader = false;
2882
2883disable_irq:
2884 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002885release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002886 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002887 return ret;
2888}
2889
Iiro Valkonen7686b102011-02-02 23:21:58 -08002890static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002891 struct device_attribute *attr,
2892 const char *buf, size_t count)
2893{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002894 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002895 int error;
2896
Iiro Valkonen7686b102011-02-02 23:21:58 -08002897 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002898 if (error) {
2899 dev_err(dev, "The firmware update failed(%d)\n", error);
2900 count = error;
2901 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07002902 dev_info(dev, "The firmware update succeeded\n");
2903
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002904 error = mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07002905 if (error)
2906 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002907 }
2908
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002909 return count;
2910}
2911
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002912static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
2913static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07002914static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
2915static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002916
Iiro Valkonen7686b102011-02-02 23:21:58 -08002917static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002918 &dev_attr_fw_version.attr,
2919 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002920 &dev_attr_object.attr,
2921 &dev_attr_update_fw.attr,
2922 NULL
2923};
2924
Iiro Valkonen7686b102011-02-02 23:21:58 -08002925static const struct attribute_group mxt_attr_group = {
2926 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002927};
2928
Iiro Valkonen7686b102011-02-02 23:21:58 -08002929static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002930{
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07002931 switch (data->suspend_mode) {
Nick Dyer7f3884f72015-08-04 16:36:29 -07002932 case MXT_SUSPEND_T9_CTRL:
2933 mxt_soft_reset(data);
2934
2935 /* Touch enable */
2936 /* 0x83 = SCANEN | RPTEN | ENABLE */
2937 mxt_write_object(data,
2938 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
2939 break;
2940
2941 case MXT_SUSPEND_DEEP_SLEEP:
2942 default:
2943 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2944
2945 /* Recalibrate since chip has been in deep sleep */
2946 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
2947 break;
2948 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002949}
2950
Iiro Valkonen7686b102011-02-02 23:21:58 -08002951static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002952{
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07002953 switch (data->suspend_mode) {
Nick Dyer7f3884f72015-08-04 16:36:29 -07002954 case MXT_SUSPEND_T9_CTRL:
2955 /* Touch disable */
2956 mxt_write_object(data,
2957 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
2958 break;
2959
2960 case MXT_SUSPEND_DEEP_SLEEP:
2961 default:
2962 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
2963 break;
2964 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002965}
2966
Iiro Valkonen7686b102011-02-02 23:21:58 -08002967static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002968{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002969 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002970
Iiro Valkonen7686b102011-02-02 23:21:58 -08002971 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002972
2973 return 0;
2974}
2975
Iiro Valkonen7686b102011-02-02 23:21:58 -08002976static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002977{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002978 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002979
Iiro Valkonen7686b102011-02-02 23:21:58 -08002980 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002981}
2982
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002983static int mxt_parse_device_properties(struct mxt_data *data)
Stephen Warren78188be2014-07-23 12:23:23 -07002984{
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002985 static const char keymap_property[] = "linux,gpio-keymap";
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002986 struct device *dev = &data->client->dev;
Stephen Warren78188be2014-07-23 12:23:23 -07002987 u32 *keymap;
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002988 int n_keys;
2989 int error;
Stephen Warren78188be2014-07-23 12:23:23 -07002990
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002991 if (device_property_present(dev, keymap_property)) {
2992 n_keys = device_property_read_u32_array(dev, keymap_property,
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002993 NULL, 0);
2994 if (n_keys <= 0) {
2995 error = n_keys < 0 ? n_keys : -EINVAL;
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002996 dev_err(dev, "invalid/malformed '%s' property: %d\n",
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002997 keymap_property, error);
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002998 return error;
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002999 }
Stephen Warren78188be2014-07-23 12:23:23 -07003000
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003001 keymap = devm_kmalloc_array(dev, n_keys, sizeof(*keymap),
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07003002 GFP_KERNEL);
Stephen Warren78188be2014-07-23 12:23:23 -07003003 if (!keymap)
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003004 return -ENOMEM;
Stephen Warren78188be2014-07-23 12:23:23 -07003005
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003006 error = device_property_read_u32_array(dev, keymap_property,
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07003007 keymap, n_keys);
3008 if (error) {
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003009 dev_err(dev, "failed to parse '%s' property: %d\n",
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07003010 keymap_property, error);
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003011 return error;
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07003012 }
Stephen Warren78188be2014-07-23 12:23:23 -07003013
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003014 data->t19_keymap = keymap;
3015 data->t19_num_keys = n_keys;
Stephen Warren78188be2014-07-23 12:23:23 -07003016 }
3017
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003018 return 0;
Stephen Warren78188be2014-07-23 12:23:23 -07003019}
Stephen Warren78188be2014-07-23 12:23:23 -07003020
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07003021static const struct dmi_system_id chromebook_T9_suspend_dmi[] = {
3022 {
3023 .matches = {
3024 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
3025 DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
3026 },
3027 },
3028 {
3029 .matches = {
3030 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
3031 },
3032 },
3033 { }
3034};
3035
Stephen Warren78188be2014-07-23 12:23:23 -07003036static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003037{
Iiro Valkonen7686b102011-02-02 23:21:58 -08003038 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003039 int error;
3040
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003041 /*
Dmitry Torokhov7cf432b2018-05-03 17:14:51 -07003042 * Ignore devices that do not have device properties attached to
3043 * them, as we need help determining whether we are dealing with
3044 * touch screen or touchpad.
3045 *
3046 * So far on x86 the only users of Atmel touch controllers are
3047 * Chromebooks, and chromeos_laptop driver will ensure that
3048 * necessary properties are provided (if firmware does not do that).
3049 */
3050 if (!device_property_present(&client->dev, "compatible"))
3051 return -ENXIO;
3052
3053 /*
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003054 * Ignore ACPI devices representing bootloader mode.
3055 *
3056 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3057 * devices for both application and bootloader modes, but we are
3058 * interested in application mode only (if device is in bootloader
3059 * mode we'll end up switching into application anyway). So far
3060 * application mode addresses were all above 0x40, so we'll use it
3061 * as a threshold.
3062 */
3063 if (ACPI_COMPANION(&client->dev) && client->addr < 0x40)
3064 return -ENXIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003065
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003066 data = devm_kzalloc(&client->dev, sizeof(struct mxt_data), GFP_KERNEL);
3067 if (!data)
Nick Dyer7a53d602014-07-23 12:21:26 -07003068 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003069
Daniel Kurtzec02ac22012-06-28 21:08:02 +08003070 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
3071 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08003072
Joonyoung Shim910d8052011-04-12 23:14:38 -07003073 data->client = client;
Joonyoung Shim910d8052011-04-12 23:14:38 -07003074 data->irq = client->irq;
Nick Dyer7a53d602014-07-23 12:21:26 -07003075 i2c_set_clientdata(client, data);
Joonyoung Shim910d8052011-04-12 23:14:38 -07003076
Benson Leungd79e7e42014-05-18 23:02:52 -07003077 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07003078 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07003079 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07003080
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07003081 data->suspend_mode = dmi_check_system(chromebook_T9_suspend_dmi) ?
3082 MXT_SUSPEND_T9_CTRL : MXT_SUSPEND_DEEP_SLEEP;
3083
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003084 error = mxt_parse_device_properties(data);
3085 if (error)
3086 return error;
3087
Sebastian Reichelf657b002017-07-25 14:11:34 -07003088 data->reset_gpio = devm_gpiod_get_optional(&client->dev,
3089 "reset", GPIOD_OUT_LOW);
3090 if (IS_ERR(data->reset_gpio)) {
3091 error = PTR_ERR(data->reset_gpio);
3092 dev_err(&client->dev, "Failed to get reset gpio: %d\n", error);
3093 return error;
3094 }
3095
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003096 error = devm_request_threaded_irq(&client->dev, client->irq,
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003097 NULL, mxt_interrupt, IRQF_ONESHOT,
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003098 client->name, data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07003099 if (error) {
3100 dev_err(&client->dev, "Failed to register interrupt\n");
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003101 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07003102 }
3103
3104 disable_irq(client->irq);
3105
Sebastian Reichelca1cd362018-05-24 09:33:36 -07003106 if (data->reset_gpio) {
3107 msleep(MXT_RESET_GPIO_TIME);
3108 gpiod_set_value(data->reset_gpio, 1);
3109 msleep(MXT_RESET_INVALID_CHG);
3110 }
3111
Daniel Kurtzcb159112012-06-28 21:08:22 +08003112 error = mxt_initialize(data);
3113 if (error)
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003114 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003115
Iiro Valkonen7686b102011-02-02 23:21:58 -08003116 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07003117 if (error) {
3118 dev_err(&client->dev, "Failure %d creating sysfs group\n",
3119 error);
Nick Dyer7a53d602014-07-23 12:21:26 -07003120 goto err_free_object;
Nick Dyer7bed6802014-05-18 23:04:09 -07003121 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003122
3123 return 0;
3124
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003125err_free_object:
Stephen Warren58e4aeee2014-09-10 10:01:10 -07003126 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07003127 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003128 return error;
3129}
3130
Bill Pembertone2619cf2012-11-23 21:50:47 -08003131static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003132{
Iiro Valkonen7686b102011-02-02 23:21:58 -08003133 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003134
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003135 disable_irq(data->irq);
Iiro Valkonen7686b102011-02-02 23:21:58 -08003136 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Stephen Warren58e4aeee2014-09-10 10:01:10 -07003137 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07003138 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003139
3140 return 0;
3141}
3142
Jingoo Han02b6a582014-11-02 00:04:14 -07003143static int __maybe_unused mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003144{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08003145 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08003146 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003147 struct input_dev *input_dev = data->input_dev;
3148
Pan Xinhui50fabb02015-08-04 16:53:04 -07003149 if (!input_dev)
3150 return 0;
3151
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003152 mutex_lock(&input_dev->mutex);
3153
3154 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08003155 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003156
3157 mutex_unlock(&input_dev->mutex);
3158
3159 return 0;
3160}
3161
Jingoo Han02b6a582014-11-02 00:04:14 -07003162static int __maybe_unused mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003163{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08003164 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08003165 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003166 struct input_dev *input_dev = data->input_dev;
3167
Pan Xinhui50fabb02015-08-04 16:53:04 -07003168 if (!input_dev)
3169 return 0;
3170
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003171 mutex_lock(&input_dev->mutex);
3172
3173 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08003174 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003175
3176 mutex_unlock(&input_dev->mutex);
3177
3178 return 0;
3179}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003180
Daniel Kurtz3a73c812012-05-08 22:29:14 -07003181static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
3182
Stephen Warren78188be2014-07-23 12:23:23 -07003183static const struct of_device_id mxt_of_match[] = {
3184 { .compatible = "atmel,maxtouch", },
Javier Martinez Canillasf6eeb9e2018-05-01 11:44:53 -07003185 /* Compatibles listed below are deprecated */
3186 { .compatible = "atmel,qt602240_ts", },
3187 { .compatible = "atmel,atmel_mxt_ts", },
3188 { .compatible = "atmel,atmel_mxt_tp", },
3189 { .compatible = "atmel,mXT224", },
Stephen Warren78188be2014-07-23 12:23:23 -07003190 {},
3191};
3192MODULE_DEVICE_TABLE(of, mxt_of_match);
3193
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07003194#ifdef CONFIG_ACPI
3195static const struct acpi_device_id mxt_acpi_id[] = {
3196 { "ATML0000", 0 }, /* Touchpad */
3197 { "ATML0001", 0 }, /* Touchscreen */
3198 { }
3199};
3200MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
3201#endif
3202
Iiro Valkonen7686b102011-02-02 23:21:58 -08003203static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003204 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08003205 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08003206 { "atmel_mxt_tp", 0 },
Javier Martinez Canillasb7d21052015-12-11 13:22:39 -08003207 { "maxtouch", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08003208 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003209 { }
3210};
Iiro Valkonen7686b102011-02-02 23:21:58 -08003211MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003212
Iiro Valkonen7686b102011-02-02 23:21:58 -08003213static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003214 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08003215 .name = "atmel_mxt_ts",
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003216 .of_match_table = mxt_of_match,
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07003217 .acpi_match_table = ACPI_PTR(mxt_acpi_id),
Iiro Valkonen7686b102011-02-02 23:21:58 -08003218 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003219 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08003220 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08003221 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08003222 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003223};
3224
Axel Lin1b92c1c2012-03-16 23:05:41 -07003225module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003226
3227/* Module information */
3228MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08003229MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003230MODULE_LICENSE("GPL");