blob: 54fe190fd4bc62b3de83d509ca28c0d0c4ec6696 [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
Nick Dyer50a77c62014-07-23 12:38:48 -07005 * Copyright (C) 2011-2014 Atmel Corporation
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07006 * Copyright (C) 2012 Google, Inc.
Nick Dyerf3c4a8f2016-09-15 17:30:43 -03007 * Copyright (C) 2016 Zodiac Inflight Innovations
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07008 *
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07009 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 */
17
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -070018#include <linux/acpi.h>
19#include <linux/dmi.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070020#include <linux/module.h>
Benson Leungd79e7e42014-05-18 23:02:52 -070021#include <linux/init.h>
22#include <linux/completion.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070023#include <linux/delay.h>
24#include <linux/firmware.h>
25#include <linux/i2c.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070026#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070027#include <linux/interrupt.h>
Stephen Warren78188be2014-07-23 12:23:23 -070028#include <linux/of.h>
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -070029#include <linux/property.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070030#include <linux/slab.h>
Sebastian Reichelf657b002017-07-25 14:11:34 -070031#include <linux/gpio/consumer.h>
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -070032#include <linux/property.h>
Nick Dyerb23157d2015-04-06 11:25:13 -070033#include <asm/unaligned.h>
Nick Dyerecfdd7e2016-07-18 18:10:32 -030034#include <media/v4l2-device.h>
35#include <media/v4l2-ioctl.h>
36#include <media/videobuf2-v4l2.h>
37#include <media/videobuf2-vmalloc.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070038
Nick Dyer50a77c62014-07-23 12:38:48 -070039/* Firmware files */
Iiro Valkonen7686b102011-02-02 23:21:58 -080040#define MXT_FW_NAME "maxtouch.fw"
Nick Dyer50a77c62014-07-23 12:38:48 -070041#define MXT_CFG_NAME "maxtouch.cfg"
42#define MXT_CFG_MAGIC "OBP_RAW V1"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070043
44/* Registers */
Iiro Valkonen7686b102011-02-02 23:21:58 -080045#define MXT_OBJECT_START 0x07
Iiro Valkonen7686b102011-02-02 23:21:58 -080046#define MXT_OBJECT_SIZE 6
Nick Dyer4ce6fa02014-07-23 12:40:09 -070047#define MXT_INFO_CHECKSUM_SIZE 3
48#define MXT_MAX_BLOCK_WRITE 256
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
50/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070051#define MXT_DEBUG_DIAGNOSTIC_T37 37
52#define MXT_GEN_MESSAGE_T5 5
53#define MXT_GEN_COMMAND_T6 6
54#define MXT_GEN_POWER_T7 7
55#define MXT_GEN_ACQUIRE_T8 8
56#define MXT_GEN_DATASOURCE_T53 53
57#define MXT_TOUCH_MULTI_T9 9
58#define MXT_TOUCH_KEYARRAY_T15 15
59#define MXT_TOUCH_PROXIMITY_T23 23
60#define MXT_TOUCH_PROXKEY_T52 52
61#define MXT_PROCI_GRIPFACE_T20 20
62#define MXT_PROCG_NOISE_T22 22
63#define MXT_PROCI_ONETOUCH_T24 24
64#define MXT_PROCI_TWOTOUCH_T27 27
65#define MXT_PROCI_GRIP_T40 40
66#define MXT_PROCI_PALM_T41 41
67#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
68#define MXT_PROCI_STYLUS_T47 47
69#define MXT_PROCG_NOISESUPPRESSION_T48 48
70#define MXT_SPT_COMMSCONFIG_T18 18
71#define MXT_SPT_GPIOPWM_T19 19
72#define MXT_SPT_SELFTEST_T25 25
73#define MXT_SPT_CTECONFIG_T28 28
74#define MXT_SPT_USERDATA_T38 38
75#define MXT_SPT_DIGITIZER_T43 43
76#define MXT_SPT_MESSAGECOUNT_T44 44
77#define MXT_SPT_CTECONFIG_T46 46
Nick Dyerb23157d2015-04-06 11:25:13 -070078#define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070079
Nick Dyer5f3f9bc2014-07-23 12:46:55 -070080/* MXT_GEN_MESSAGE_T5 object */
81#define MXT_RPTID_NOMSG 0xff
82
Iiro Valkonen81c88a72011-07-04 03:08:25 -070083/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080084#define MXT_COMMAND_RESET 0
85#define MXT_COMMAND_BACKUPNV 1
86#define MXT_COMMAND_CALIBRATE 2
87#define MXT_COMMAND_REPORTALL 3
88#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070089
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070090/* Define for T6 status byte */
91#define MXT_T6_STATUS_RESET (1 << 7)
Nick Dyer497767d2014-07-23 12:47:50 -070092#define MXT_T6_STATUS_OFL (1 << 6)
93#define MXT_T6_STATUS_SIGERR (1 << 5)
94#define MXT_T6_STATUS_CAL (1 << 4)
95#define MXT_T6_STATUS_CFGERR (1 << 3)
96#define MXT_T6_STATUS_COMSERR (1 << 2)
Iiro Valkonena4a2ef42014-05-18 23:03:44 -070097
Iiro Valkonen81c88a72011-07-04 03:08:25 -070098/* MXT_GEN_POWER_T7 field */
Nick Dyer7f3884f72015-08-04 16:36:29 -070099struct t7_config {
100 u8 idle;
101 u8 active;
102} __packed;
103
104#define MXT_POWER_CFG_RUN 0
105#define MXT_POWER_CFG_DEEPSLEEP 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700106
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700107/* MXT_TOUCH_MULTI_T9 field */
Nick Dyer7f3884f72015-08-04 16:36:29 -0700108#define MXT_T9_CTRL 0
Nick Dyer27864892016-07-18 18:10:33 -0300109#define MXT_T9_XSIZE 3
110#define MXT_T9_YSIZE 4
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700111#define MXT_T9_ORIENT 9
112#define MXT_T9_RANGE 18
113
Nick Dyerf3889ed2014-05-18 23:22:04 -0700114/* MXT_TOUCH_MULTI_T9 status */
115#define MXT_T9_UNGRIP (1 << 0)
116#define MXT_T9_SUPPRESS (1 << 1)
117#define MXT_T9_AMP (1 << 2)
118#define MXT_T9_VECTOR (1 << 3)
119#define MXT_T9_MOVE (1 << 4)
120#define MXT_T9_RELEASE (1 << 5)
121#define MXT_T9_PRESS (1 << 6)
122#define MXT_T9_DETECT (1 << 7)
123
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700124struct t9_range {
Nick Dyer1c0276d2016-01-10 18:29:34 -0800125 __le16 x;
126 __le16 y;
Nick Dyer61dc1ab2014-05-18 23:16:49 -0700127} __packed;
128
Nick Dyerf3889ed2014-05-18 23:22:04 -0700129/* MXT_TOUCH_MULTI_T9 orient */
130#define MXT_T9_ORIENT_SWITCH (1 << 0)
Nick Dyerde601f72016-07-18 18:10:34 -0300131#define MXT_T9_ORIENT_INVERTX (1 << 1)
132#define MXT_T9_ORIENT_INVERTY (1 << 2)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700133
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700134/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800135#define MXT_COMMS_CTRL 0
136#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700137
Nick Dyerd6a39402016-07-18 18:10:31 -0300138/* MXT_DEBUG_DIAGNOSTIC_T37 */
139#define MXT_DIAGNOSTIC_PAGEUP 0x01
140#define MXT_DIAGNOSTIC_DELTAS 0x10
Nick Dyer06b3d3f2016-07-18 18:10:36 -0300141#define MXT_DIAGNOSTIC_REFS 0x11
Nick Dyerd6a39402016-07-18 18:10:31 -0300142#define MXT_DIAGNOSTIC_SIZE 128
143
Nick Dyer566d5332016-07-18 18:10:35 -0300144#define MXT_FAMILY_1386 160
145#define MXT1386_COLUMNS 3
146#define MXT1386_PAGES_PER_COLUMN 8
147
Nick Dyerd6a39402016-07-18 18:10:31 -0300148struct t37_debug {
149#ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
150 u8 mode;
151 u8 page;
152 u8 data[MXT_DIAGNOSTIC_SIZE];
153#endif
154};
155
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700156/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800157#define MXT_BOOT_VALUE 0xa5
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700158#define MXT_RESET_VALUE 0x01
Iiro Valkonen7686b102011-02-02 23:21:58 -0800159#define MXT_BACKUP_VALUE 0x55
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700160
Nick Dyerb23157d2015-04-06 11:25:13 -0700161/* T100 Multiple Touch Touchscreen */
162#define MXT_T100_CTRL 0
163#define MXT_T100_CFG1 1
164#define MXT_T100_TCHAUX 3
Nick Dyer27864892016-07-18 18:10:33 -0300165#define MXT_T100_XSIZE 9
Nick Dyerb23157d2015-04-06 11:25:13 -0700166#define MXT_T100_XRANGE 13
Nick Dyer27864892016-07-18 18:10:33 -0300167#define MXT_T100_YSIZE 20
Nick Dyerb23157d2015-04-06 11:25:13 -0700168#define MXT_T100_YRANGE 24
169
170#define MXT_T100_CFG_SWITCHXY BIT(5)
Nick Dyerde601f72016-07-18 18:10:34 -0300171#define MXT_T100_CFG_INVERTY BIT(6)
172#define MXT_T100_CFG_INVERTX BIT(7)
Nick Dyerb23157d2015-04-06 11:25:13 -0700173
174#define MXT_T100_TCHAUX_VECT BIT(0)
175#define MXT_T100_TCHAUX_AMPL BIT(1)
176#define MXT_T100_TCHAUX_AREA BIT(2)
177
178#define MXT_T100_DETECT BIT(7)
179#define MXT_T100_TYPE_MASK 0x70
180
181enum t100_type {
182 MXT_T100_TYPE_FINGER = 1,
183 MXT_T100_TYPE_PASSIVE_STYLUS = 2,
184 MXT_T100_TYPE_HOVERING_FINGER = 4,
185 MXT_T100_TYPE_GLOVE = 5,
186 MXT_T100_TYPE_LARGE_TOUCH = 6,
187};
188
189#define MXT_DISTANCE_ACTIVE_TOUCH 0
190#define MXT_DISTANCE_HOVERING 1
191
192#define MXT_TOUCH_MAJOR_DEFAULT 1
193#define MXT_PRESSURE_DEFAULT 1
194
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700195/* Delay times */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800196#define MXT_BACKUP_TIME 50 /* msec */
Sebastian Reichelca1cd362018-05-24 09:33:36 -0700197#define MXT_RESET_GPIO_TIME 20 /* msec */
198#define MXT_RESET_INVALID_CHG 100 /* msec */
Linus Torvalds8343bce2013-03-09 10:31:01 -0800199#define MXT_RESET_TIME 200 /* msec */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700200#define MXT_RESET_TIMEOUT 3000 /* msec */
Nick Dyerc3f78042014-05-18 23:04:46 -0700201#define MXT_CRC_TIMEOUT 1000 /* msec */
Benson Leunga0434b72014-05-18 23:03:09 -0700202#define MXT_FW_RESET_TIME 3000 /* msec */
203#define MXT_FW_CHG_TIMEOUT 300 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700204
205/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800206#define MXT_UNLOCK_CMD_MSB 0xaa
207#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700208
209/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800210#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
211#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
212#define MXT_FRAME_CRC_CHECK 0x02
213#define MXT_FRAME_CRC_FAIL 0x03
214#define MXT_FRAME_CRC_PASS 0x04
215#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
216#define MXT_BOOT_STATUS_MASK 0x3f
Nick Dyere57a66a2014-05-18 23:13:40 -0700217#define MXT_BOOT_EXTENDED_ID (1 << 5)
218#define MXT_BOOT_ID_MASK 0x1f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700219
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700220/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800221#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700222
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800223#define MXT_PIXELS_PER_MM 20
224
Iiro Valkonen7686b102011-02-02 23:21:58 -0800225struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700226 u8 family_id;
227 u8 variant_id;
228 u8 version;
229 u8 build;
230 u8 matrix_xsize;
231 u8 matrix_ysize;
232 u8 object_num;
233};
234
Iiro Valkonen7686b102011-02-02 23:21:58 -0800235struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700236 u8 type;
237 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700238 u8 size_minus_one;
239 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700240 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800241} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700242
Nick Dyerd6a39402016-07-18 18:10:31 -0300243struct mxt_dbg {
244 u16 t37_address;
245 u16 diag_cmd_address;
246 struct t37_debug *t37_buf;
247 unsigned int t37_pages;
248 unsigned int t37_nodes;
Nick Dyerecfdd7e2016-07-18 18:10:32 -0300249
250 struct v4l2_device v4l2;
251 struct v4l2_pix_format format;
252 struct video_device vdev;
253 struct vb2_queue queue;
254 struct mutex lock;
255 int input;
256};
257
Nick Dyer06b3d3f2016-07-18 18:10:36 -0300258enum v4l_dbg_inputs {
259 MXT_V4L_INPUT_DELTAS,
260 MXT_V4L_INPUT_REFS,
261 MXT_V4L_INPUT_MAX,
262};
263
Nick Dyerecfdd7e2016-07-18 18:10:32 -0300264static const struct v4l2_file_operations mxt_video_fops = {
265 .owner = THIS_MODULE,
266 .open = v4l2_fh_open,
267 .release = vb2_fop_release,
268 .unlocked_ioctl = video_ioctl2,
269 .read = vb2_fop_read,
270 .mmap = vb2_fop_mmap,
271 .poll = vb2_fop_poll,
Nick Dyerd6a39402016-07-18 18:10:31 -0300272};
273
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700274enum mxt_suspend_mode {
275 MXT_SUSPEND_DEEP_SLEEP = 0,
276 MXT_SUSPEND_T9_CTRL = 1,
277};
278
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700279/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800280struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700281 struct i2c_client *client;
282 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800283 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800284 struct mxt_object *object_table;
Nick Dyer068bdb62018-05-01 11:40:18 -0700285 struct mxt_info *info;
286 void *raw_info_block;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700287 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700288 unsigned int max_x;
289 unsigned int max_y;
Nick Dyerde601f72016-07-18 18:10:34 -0300290 bool invertx;
291 bool inverty;
Nick Dyer1c0276d2016-01-10 18:29:34 -0800292 bool xy_switch;
Nick Dyer27864892016-07-18 18:10:33 -0300293 u8 xsize;
294 u8 ysize;
Benson Leungd79e7e42014-05-18 23:02:52 -0700295 bool in_bootloader;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700296 u16 mem_size;
Nick Dyerb23157d2015-04-06 11:25:13 -0700297 u8 t100_aux_ampl;
298 u8 t100_aux_area;
299 u8 t100_aux_vect;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700300 u8 max_reportid;
Nick Dyerc3f78042014-05-18 23:04:46 -0700301 u32 config_crc;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700302 u32 info_crc;
Nick Dyerf28a8422014-05-18 23:10:49 -0700303 u8 bootloader_addr;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700304 u8 *msg_buf;
Nick Dyer497767d2014-07-23 12:47:50 -0700305 u8 t6_status;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700306 bool update_input;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700307 u8 last_message_count;
308 u8 num_touchids;
Nick Dyerb23157d2015-04-06 11:25:13 -0700309 u8 multitouch;
Nick Dyer7f3884f72015-08-04 16:36:29 -0700310 struct t7_config t7_cfg;
Nick Dyerd6a39402016-07-18 18:10:31 -0300311 struct mxt_dbg dbg;
Sebastian Reichelf657b002017-07-25 14:11:34 -0700312 struct gpio_desc *reset_gpio;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800313
314 /* Cached parameters from object table */
Nick Dyerb9b05a82014-07-23 12:48:13 -0700315 u16 T5_address;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700316 u8 T5_msg_size;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800317 u8 T6_reportid;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700318 u16 T6_address;
Nick Dyer4ce6fa02014-07-23 12:40:09 -0700319 u16 T7_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800320 u8 T9_reportid_min;
321 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800322 u8 T19_reportid;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700323 u16 T44_address;
Nick Dyerb23157d2015-04-06 11:25:13 -0700324 u8 T100_reportid_min;
325 u8 T100_reportid_max;
Benson Leungd79e7e42014-05-18 23:02:52 -0700326
327 /* for fw update in bootloader */
328 struct completion bl_completion;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700329
330 /* for reset handling */
331 struct completion reset_completion;
Nick Dyerc3f78042014-05-18 23:04:46 -0700332
333 /* for config update handling */
334 struct completion crc_completion;
Dmitry Torokhovd80808e2018-03-20 15:31:25 -0700335
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700336 u32 *t19_keymap;
337 unsigned int t19_num_keys;
338
Dmitry Torokhovd80808e2018-03-20 15:31:25 -0700339 enum mxt_suspend_mode suspend_mode;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700340};
341
Nick Dyerecfdd7e2016-07-18 18:10:32 -0300342struct mxt_vb2_buffer {
343 struct vb2_buffer vb;
344 struct list_head list;
345};
346
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700347static size_t mxt_obj_size(const struct mxt_object *obj)
348{
349 return obj->size_minus_one + 1;
350}
351
352static size_t mxt_obj_instances(const struct mxt_object *obj)
353{
354 return obj->instances_minus_one + 1;
355}
356
Iiro Valkonen7686b102011-02-02 23:21:58 -0800357static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700358{
359 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700360 case MXT_GEN_COMMAND_T6:
361 case MXT_GEN_POWER_T7:
362 case MXT_GEN_ACQUIRE_T8:
363 case MXT_GEN_DATASOURCE_T53:
364 case MXT_TOUCH_MULTI_T9:
365 case MXT_TOUCH_KEYARRAY_T15:
366 case MXT_TOUCH_PROXIMITY_T23:
367 case MXT_TOUCH_PROXKEY_T52:
Maxime Roussin-Bélanger089b50d2017-05-19 14:59:03 -0700368 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700369 case MXT_PROCI_GRIPFACE_T20:
370 case MXT_PROCG_NOISE_T22:
371 case MXT_PROCI_ONETOUCH_T24:
372 case MXT_PROCI_TWOTOUCH_T27:
373 case MXT_PROCI_GRIP_T40:
374 case MXT_PROCI_PALM_T41:
375 case MXT_PROCI_TOUCHSUPPRESSION_T42:
376 case MXT_PROCI_STYLUS_T47:
377 case MXT_PROCG_NOISESUPPRESSION_T48:
378 case MXT_SPT_COMMSCONFIG_T18:
379 case MXT_SPT_GPIOPWM_T19:
380 case MXT_SPT_SELFTEST_T25:
381 case MXT_SPT_CTECONFIG_T28:
382 case MXT_SPT_USERDATA_T38:
383 case MXT_SPT_DIGITIZER_T43:
384 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700385 return true;
386 default:
387 return false;
388 }
389}
390
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700391static void mxt_dump_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700392{
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700393 dev_dbg(&data->client->dev, "message: %*ph\n",
394 data->T5_msg_size, message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700395}
396
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700397static int mxt_wait_for_completion(struct mxt_data *data,
398 struct completion *comp,
399 unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700400{
Benson Leungd79e7e42014-05-18 23:02:52 -0700401 struct device *dev = &data->client->dev;
Benson Leungd79e7e42014-05-18 23:02:52 -0700402 unsigned long timeout = msecs_to_jiffies(timeout_ms);
403 long ret;
404
405 ret = wait_for_completion_interruptible_timeout(comp, timeout);
406 if (ret < 0) {
407 return ret;
408 } else if (ret == 0) {
409 dev_err(dev, "Wait for completion timed out.\n");
410 return -ETIMEDOUT;
411 }
412 return 0;
413}
414
Nick Dyerf28a8422014-05-18 23:10:49 -0700415static int mxt_bootloader_read(struct mxt_data *data,
416 u8 *val, unsigned int count)
417{
418 int ret;
419 struct i2c_msg msg;
420
421 msg.addr = data->bootloader_addr;
422 msg.flags = data->client->flags & I2C_M_TEN;
423 msg.flags |= I2C_M_RD;
424 msg.len = count;
425 msg.buf = val;
426
427 ret = i2c_transfer(data->client->adapter, &msg, 1);
Nick Dyerf28a8422014-05-18 23:10:49 -0700428 if (ret == 1) {
429 ret = 0;
430 } else {
431 ret = ret < 0 ? ret : -EIO;
432 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
433 __func__, ret);
434 }
435
436 return ret;
437}
438
439static int mxt_bootloader_write(struct mxt_data *data,
440 const u8 * const val, unsigned int count)
441{
442 int ret;
443 struct i2c_msg msg;
444
445 msg.addr = data->bootloader_addr;
446 msg.flags = data->client->flags & I2C_M_TEN;
447 msg.len = count;
448 msg.buf = (u8 *)val;
449
450 ret = i2c_transfer(data->client->adapter, &msg, 1);
451 if (ret == 1) {
452 ret = 0;
453 } else {
454 ret = ret < 0 ? ret : -EIO;
455 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
456 __func__, ret);
457 }
458
459 return ret;
460}
461
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700462static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
Nick Dyerf28a8422014-05-18 23:10:49 -0700463{
464 u8 appmode = data->client->addr;
465 u8 bootloader;
Nick Dyer068bdb62018-05-01 11:40:18 -0700466 u8 family_id = data->info ? data->info->family_id : 0;
Nick Dyerf28a8422014-05-18 23:10:49 -0700467
468 switch (appmode) {
469 case 0x4a:
470 case 0x4b:
Nick Dyer44a0bab2014-07-23 12:45:26 -0700471 /* Chips after 1664S use different scheme */
Nick Dyer068bdb62018-05-01 11:40:18 -0700472 if (retry || family_id >= 0xa2) {
Nick Dyer44a0bab2014-07-23 12:45:26 -0700473 bootloader = appmode - 0x24;
474 break;
475 }
476 /* Fall through for normal case */
Nick Dyerf28a8422014-05-18 23:10:49 -0700477 case 0x4c:
478 case 0x4d:
479 case 0x5a:
480 case 0x5b:
481 bootloader = appmode - 0x26;
482 break;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700483
Nick Dyerf28a8422014-05-18 23:10:49 -0700484 default:
485 dev_err(&data->client->dev,
486 "Appmode i2c address 0x%02x not found\n",
487 appmode);
488 return -EINVAL;
489 }
490
491 data->bootloader_addr = bootloader;
492 return 0;
493}
494
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700495static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700496{
497 struct device *dev = &data->client->dev;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700498 int error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700499 u8 val;
500 bool crc_failure;
501
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700502 error = mxt_lookup_bootloader_address(data, alt_address);
503 if (error)
504 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700505
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -0700506 error = mxt_bootloader_read(data, &val, 1);
507 if (error)
508 return error;
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700509
510 /* Check app crc fail mode */
511 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
512
513 dev_err(dev, "Detected bootloader, status:%02X%s\n",
514 val, crc_failure ? ", APP_CRC_FAIL" : "");
515
516 return 0;
517}
518
Nick Dyere57a66a2014-05-18 23:13:40 -0700519static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
520{
521 struct device *dev = &data->client->dev;
522 u8 buf[3];
523
524 if (val & MXT_BOOT_EXTENDED_ID) {
525 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
526 dev_err(dev, "%s: i2c failure\n", __func__);
Nick Dyer68807a02014-06-07 23:17:26 -0700527 return val;
Nick Dyere57a66a2014-05-18 23:13:40 -0700528 }
529
530 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
531
532 return buf[0];
533 } else {
534 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
535
536 return val;
537 }
538}
539
Nick Dyer385deb92014-07-23 12:42:40 -0700540static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
541 bool wait)
Benson Leungd79e7e42014-05-18 23:02:52 -0700542{
Nick Dyerf28a8422014-05-18 23:10:49 -0700543 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700545 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700546
547recheck:
Nick Dyer385deb92014-07-23 12:42:40 -0700548 if (wait) {
Benson Leungd79e7e42014-05-18 23:02:52 -0700549 /*
550 * In application update mode, the interrupt
551 * line signals state transitions. We must wait for the
552 * CHG assertion before reading the status byte.
553 * Once the status byte has been read, the line is deasserted.
554 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -0700555 ret = mxt_wait_for_completion(data, &data->bl_completion,
556 MXT_FW_CHG_TIMEOUT);
Benson Leungd79e7e42014-05-18 23:02:52 -0700557 if (ret) {
558 /*
559 * TODO: handle -ERESTARTSYS better by terminating
560 * fw update process before returning to userspace
561 * by writing length 0x000 to device (iff we are in
562 * WAITING_FRAME_DATA state).
563 */
Nick Dyerf28a8422014-05-18 23:10:49 -0700564 dev_err(dev, "Update wait error %d\n", ret);
Benson Leungd79e7e42014-05-18 23:02:52 -0700565 return ret;
566 }
567 }
568
Nick Dyerf28a8422014-05-18 23:10:49 -0700569 ret = mxt_bootloader_read(data, &val, 1);
570 if (ret)
571 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700572
Nick Dyere57a66a2014-05-18 23:13:40 -0700573 if (state == MXT_WAITING_BOOTLOAD_CMD)
574 val = mxt_get_bootloader_version(data, val);
575
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700576 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800577 case MXT_WAITING_BOOTLOAD_CMD:
578 case MXT_WAITING_FRAME_DATA:
Nick Dyera9fdd1e2014-07-23 12:41:58 -0700579 case MXT_APP_CRC_FAIL:
Iiro Valkonen7686b102011-02-02 23:21:58 -0800580 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700581 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800582 case MXT_FRAME_CRC_PASS:
Nick Dyerf943c742014-05-18 23:14:20 -0700583 if (val == MXT_FRAME_CRC_CHECK) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700584 goto recheck;
Nick Dyerf943c742014-05-18 23:14:20 -0700585 } else if (val == MXT_FRAME_CRC_FAIL) {
586 dev_err(dev, "Bootloader CRC fail\n");
587 return -EINVAL;
588 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589 break;
590 default:
591 return -EINVAL;
592 }
593
594 if (val != state) {
Nick Dyerf28a8422014-05-18 23:10:49 -0700595 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
Nick Dyer7bed6802014-05-18 23:04:09 -0700596 val, state);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597 return -EINVAL;
598 }
599
600 return 0;
601}
602
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700603static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700604{
Nick Dyerf28a8422014-05-18 23:10:49 -0700605 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700606 u8 buf[2];
607
Nick Dyer8efaa5e2014-07-23 12:45:49 -0700608 if (unlock) {
609 buf[0] = MXT_UNLOCK_CMD_LSB;
610 buf[1] = MXT_UNLOCK_CMD_MSB;
611 } else {
612 buf[0] = 0x01;
613 buf[1] = 0x01;
614 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700615
Nick Dyerf28a8422014-05-18 23:10:49 -0700616 ret = mxt_bootloader_write(data, buf, 2);
617 if (ret)
618 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700619
620 return 0;
621}
622
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624 u16 reg, u16 len, void *val)
625{
626 struct i2c_msg xfer[2];
627 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800628 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700629
630 buf[0] = reg & 0xff;
631 buf[1] = (reg >> 8) & 0xff;
632
633 /* Write register */
634 xfer[0].addr = client->addr;
635 xfer[0].flags = 0;
636 xfer[0].len = 2;
637 xfer[0].buf = buf;
638
639 /* Read data */
640 xfer[1].addr = client->addr;
641 xfer[1].flags = I2C_M_RD;
642 xfer[1].len = len;
643 xfer[1].buf = val;
644
Daniel Kurtz771733e2012-06-28 21:08:11 +0800645 ret = i2c_transfer(client->adapter, xfer, 2);
646 if (ret == 2) {
647 ret = 0;
648 } else {
649 if (ret >= 0)
650 ret = -EIO;
651 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
652 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700653 }
654
Daniel Kurtz771733e2012-06-28 21:08:11 +0800655 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700656}
657
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800658static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
659 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800661 u8 *buf;
662 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800663 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700664
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800665 count = len + 2;
666 buf = kmalloc(count, GFP_KERNEL);
667 if (!buf)
668 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669
670 buf[0] = reg & 0xff;
671 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800672 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700673
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800674 ret = i2c_master_send(client, buf, count);
675 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800676 ret = 0;
677 } else {
678 if (ret >= 0)
679 ret = -EIO;
680 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
681 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700682 }
683
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800684 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800685 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686}
687
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800688static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800690 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691}
692
Iiro Valkonen7686b102011-02-02 23:21:58 -0800693static struct mxt_object *
694mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700695{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800696 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700697 int i;
698
Nick Dyer068bdb62018-05-01 11:40:18 -0700699 for (i = 0; i < data->info->object_num; i++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700700 object = data->object_table + i;
701 if (object->type == type)
702 return object;
703 }
704
Nick Dyer50a77c62014-07-23 12:38:48 -0700705 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700706 return NULL;
707}
708
Nick Dyer497767d2014-07-23 12:47:50 -0700709static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
710{
711 struct device *dev = &data->client->dev;
712 u8 status = msg[1];
713 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
714
715 complete(&data->crc_completion);
716
717 if (crc != data->config_crc) {
718 data->config_crc = crc;
719 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
720 }
721
722 /* Detect reset */
723 if (status & MXT_T6_STATUS_RESET)
724 complete(&data->reset_completion);
725
726 /* Output debug if status has changed */
727 if (status != data->t6_status)
728 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
729 status,
730 status == 0 ? " OK" : "",
731 status & MXT_T6_STATUS_RESET ? " RESET" : "",
732 status & MXT_T6_STATUS_OFL ? " OFL" : "",
733 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
734 status & MXT_T6_STATUS_CAL ? " CAL" : "",
735 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
736 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
737
738 /* Save current status */
739 data->t6_status = status;
740}
741
Linus Torvalds7f405482014-12-31 12:59:34 -0800742static int mxt_write_object(struct mxt_data *data,
743 u8 type, u8 offset, u8 val)
744{
745 struct mxt_object *object;
746 u16 reg;
747
748 object = mxt_get_object(data, type);
749 if (!object || offset >= mxt_obj_size(object))
750 return -EINVAL;
751
752 reg = object->start_address;
753 return mxt_write_reg(data->client, reg + offset, val);
754}
755
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700756static void mxt_input_button(struct mxt_data *data, u8 *message)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800757{
758 struct input_dev *input = data->input_dev;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800759 int i;
760
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700761 for (i = 0; i < data->t19_num_keys; i++) {
762 if (data->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800763 continue;
Dmitry Torokhovc37f6d32015-04-15 10:31:10 -0700764
765 /* Active-low switch */
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700766 input_report_key(input, data->t19_keymap[i],
Dmitry Torokhovc37f6d32015-04-15 10:31:10 -0700767 !(message[1] & BIT(i)));
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800768 }
769}
770
Benson Leungb735fbe2014-07-23 12:22:27 -0700771static void mxt_input_sync(struct mxt_data *data)
Nick Dyereef820d2014-05-18 23:22:22 -0700772{
Benson Leungb735fbe2014-07-23 12:22:27 -0700773 input_mt_report_pointer_emulation(data->input_dev,
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -0700774 data->t19_num_keys);
Benson Leungb735fbe2014-07-23 12:22:27 -0700775 input_sync(data->input_dev);
Nick Dyereef820d2014-05-18 23:22:22 -0700776}
777
Nick Dyerb9b05a82014-07-23 12:48:13 -0700778static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700779{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700780 struct device *dev = &data->client->dev;
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800781 struct input_dev *input_dev = data->input_dev;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700782 int id;
783 u8 status;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700784 int x;
785 int y;
786 int area;
Nick Dyerfea9e462014-05-18 23:21:48 -0700787 int amplitude;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700789 id = message[0] - data->T9_reportid_min;
790 status = message[1];
791 x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
792 y = (message[3] << 4) | ((message[4] & 0xf));
Nick Dyereef820d2014-05-18 23:22:22 -0700793
794 /* Handle 10/12 bit switching */
Joonyoung Shim910d8052011-04-12 23:14:38 -0700795 if (data->max_x < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700796 x >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700797 if (data->max_y < 1024)
Nick Dyereef820d2014-05-18 23:22:22 -0700798 y >>= 2;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700799
Nick Dyer5f3f9bc2014-07-23 12:46:55 -0700800 area = message[5];
801 amplitude = message[6];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700802
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800803 dev_dbg(dev,
804 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
805 id,
Nick Dyerf3889ed2014-05-18 23:22:04 -0700806 (status & MXT_T9_DETECT) ? 'D' : '.',
807 (status & MXT_T9_PRESS) ? 'P' : '.',
808 (status & MXT_T9_RELEASE) ? 'R' : '.',
809 (status & MXT_T9_MOVE) ? 'M' : '.',
810 (status & MXT_T9_VECTOR) ? 'V' : '.',
811 (status & MXT_T9_AMP) ? 'A' : '.',
812 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
813 (status & MXT_T9_UNGRIP) ? 'U' : '.',
Nick Dyerfea9e462014-05-18 23:21:48 -0700814 x, y, area, amplitude);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700815
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800816 input_mt_slot(input_dev, id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700817
Nick Dyerf3889ed2014-05-18 23:22:04 -0700818 if (status & MXT_T9_DETECT) {
Nick Dyereef820d2014-05-18 23:22:22 -0700819 /*
820 * Multiple bits may be set if the host is slow to read
821 * the status messages, indicating all the events that
822 * have happened.
823 */
824 if (status & MXT_T9_RELEASE) {
825 input_mt_report_slot_state(input_dev,
826 MT_TOOL_FINGER, 0);
Benson Leungb735fbe2014-07-23 12:22:27 -0700827 mxt_input_sync(data);
Nick Dyereef820d2014-05-18 23:22:22 -0700828 }
829
830 /* Touch active */
831 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800832 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
833 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
Nick Dyerfea9e462014-05-18 23:21:48 -0700834 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800835 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
Nick Dyereef820d2014-05-18 23:22:22 -0700836 } else {
837 /* Touch no longer active, close out slot */
838 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800839 }
Nick Dyerb9b05a82014-07-23 12:48:13 -0700840
841 data->update_input = true;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700842}
843
Nick Dyerb23157d2015-04-06 11:25:13 -0700844static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
845{
846 struct device *dev = &data->client->dev;
847 struct input_dev *input_dev = data->input_dev;
848 int id;
849 u8 status;
850 u8 type = 0;
851 u16 x;
852 u16 y;
853 int distance = 0;
854 int tool = 0;
855 u8 major = 0;
856 u8 pressure = 0;
857 u8 orientation = 0;
858
859 id = message[0] - data->T100_reportid_min - 2;
860
861 /* ignore SCRSTATUS events */
862 if (id < 0)
863 return;
864
865 status = message[1];
866 x = get_unaligned_le16(&message[2]);
867 y = get_unaligned_le16(&message[4]);
868
869 if (status & MXT_T100_DETECT) {
870 type = (status & MXT_T100_TYPE_MASK) >> 4;
871
872 switch (type) {
873 case MXT_T100_TYPE_HOVERING_FINGER:
874 tool = MT_TOOL_FINGER;
875 distance = MXT_DISTANCE_HOVERING;
876
877 if (data->t100_aux_vect)
878 orientation = message[data->t100_aux_vect];
879
880 break;
881
882 case MXT_T100_TYPE_FINGER:
883 case MXT_T100_TYPE_GLOVE:
884 tool = MT_TOOL_FINGER;
885 distance = MXT_DISTANCE_ACTIVE_TOUCH;
886
887 if (data->t100_aux_area)
888 major = message[data->t100_aux_area];
889
890 if (data->t100_aux_ampl)
891 pressure = message[data->t100_aux_ampl];
892
893 if (data->t100_aux_vect)
894 orientation = message[data->t100_aux_vect];
895
896 break;
897
898 case MXT_T100_TYPE_PASSIVE_STYLUS:
899 tool = MT_TOOL_PEN;
900
901 /*
902 * Passive stylus is reported with size zero so
903 * hardcode.
904 */
905 major = MXT_TOUCH_MAJOR_DEFAULT;
906
907 if (data->t100_aux_ampl)
908 pressure = message[data->t100_aux_ampl];
909
910 break;
911
912 case MXT_T100_TYPE_LARGE_TOUCH:
913 /* Ignore suppressed touch */
914 break;
915
916 default:
917 dev_dbg(dev, "Unexpected T100 type\n");
918 return;
919 }
920 }
921
922 /*
923 * Values reported should be non-zero if tool is touching the
924 * device
925 */
926 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
927 pressure = MXT_PRESSURE_DEFAULT;
928
929 input_mt_slot(input_dev, id);
930
931 if (status & MXT_T100_DETECT) {
932 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
933 id, type, x, y, major, pressure, orientation);
934
935 input_mt_report_slot_state(input_dev, tool, 1);
936 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
937 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
938 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
939 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
940 input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
941 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
942 } else {
943 dev_dbg(dev, "[%u] release\n", id);
944
945 /* close out slot */
946 input_mt_report_slot_state(input_dev, 0, 0);
947 }
948
949 data->update_input = true;
950}
951
Nick Dyerb9b05a82014-07-23 12:48:13 -0700952static int mxt_proc_message(struct mxt_data *data, u8 *message)
Daniel Kurtz04a79182012-06-28 21:08:21 +0800953{
Nick Dyerb9b05a82014-07-23 12:48:13 -0700954 u8 report_id = message[0];
955
956 if (report_id == MXT_RPTID_NOMSG)
957 return 0;
958
959 if (report_id == data->T6_reportid) {
960 mxt_proc_t6_messages(data, message);
961 } else if (!data->input_dev) {
962 /*
963 * Do not report events if input device
964 * is not yet registered.
965 */
966 mxt_dump_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700967 } else if (report_id >= data->T9_reportid_min &&
968 report_id <= data->T9_reportid_max) {
Nick Dyerb9b05a82014-07-23 12:48:13 -0700969 mxt_proc_t9_message(data, message);
Nick Dyerb23157d2015-04-06 11:25:13 -0700970 } else if (report_id >= data->T100_reportid_min &&
971 report_id <= data->T100_reportid_max) {
972 mxt_proc_t100_message(data, message);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700973 } else if (report_id == data->T19_reportid) {
974 mxt_input_button(data, message);
975 data->update_input = true;
976 } else {
977 mxt_dump_message(data, message);
978 }
979
980 return 1;
981}
982
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700983static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
Nick Dyerb9b05a82014-07-23 12:48:13 -0700984{
985 struct device *dev = &data->client->dev;
986 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700987 int i;
988 u8 num_valid = 0;
Nick Dyerb9b05a82014-07-23 12:48:13 -0700989
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700990 /* Safety check for msg_buf */
991 if (count > data->max_reportid)
992 return -EINVAL;
993
994 /* Process remaining messages if necessary */
Nick Dyerb9b05a82014-07-23 12:48:13 -0700995 ret = __mxt_read_reg(data->client, data->T5_address,
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700996 data->T5_msg_size * count, data->msg_buf);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700997 if (ret) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -0700998 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
Nick Dyerb9b05a82014-07-23 12:48:13 -0700999 return ret;
1000 }
1001
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001002 for (i = 0; i < count; i++) {
1003 ret = mxt_proc_message(data,
1004 data->msg_buf + data->T5_msg_size * i);
1005
1006 if (ret == 1)
1007 num_valid++;
1008 }
1009
1010 /* return number of messages read */
1011 return num_valid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012}
1013
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001014static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001015{
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001016 struct device *dev = &data->client->dev;
Nick Dyerb9b05a82014-07-23 12:48:13 -07001017 int ret;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001018 u8 count, num_left;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001019
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001020 /* Read T44 and T5 together */
1021 ret = __mxt_read_reg(data->client, data->T44_address,
1022 data->T5_msg_size + 1, data->msg_buf);
1023 if (ret) {
1024 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
1025 return IRQ_NONE;
1026 }
1027
1028 count = data->msg_buf[0];
1029
Nick Dyer507584e2015-08-04 16:58:05 -07001030 /*
1031 * This condition may be caused by the CHG line being configured in
1032 * Mode 0. It results in unnecessary I2C operations but it is benign.
1033 */
1034 if (count == 0)
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001035 return IRQ_NONE;
Nick Dyer507584e2015-08-04 16:58:05 -07001036
1037 if (count > data->max_reportid) {
1038 dev_warn(dev, "T44 count %d exceeded max report id\n", count);
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001039 count = data->max_reportid;
1040 }
1041
1042 /* Process first message */
1043 ret = mxt_proc_message(data, data->msg_buf + 1);
1044 if (ret < 0) {
1045 dev_warn(dev, "Unexpected invalid message\n");
1046 return IRQ_NONE;
1047 }
1048
1049 num_left = count - 1;
1050
1051 /* Process remaining messages if necessary */
1052 if (num_left) {
1053 ret = mxt_read_and_process_messages(data, num_left);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001054 if (ret < 0)
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001055 goto end;
1056 else if (ret != num_left)
1057 dev_warn(dev, "Unexpected invalid message\n");
1058 }
1059
1060end:
1061 if (data->update_input) {
1062 mxt_input_sync(data);
1063 data->update_input = false;
1064 }
1065
1066 return IRQ_HANDLED;
1067}
1068
1069static int mxt_process_messages_until_invalid(struct mxt_data *data)
1070{
1071 struct device *dev = &data->client->dev;
1072 int count, read;
1073 u8 tries = 2;
1074
1075 count = data->max_reportid;
1076
1077 /* Read messages until we force an invalid */
1078 do {
1079 read = mxt_read_and_process_messages(data, count);
1080 if (read < count)
1081 return 0;
1082 } while (--tries);
1083
1084 if (data->update_input) {
1085 mxt_input_sync(data);
1086 data->update_input = false;
1087 }
1088
1089 dev_err(dev, "CHG pin isn't cleared\n");
1090 return -EBUSY;
1091}
1092
1093static irqreturn_t mxt_process_messages(struct mxt_data *data)
1094{
1095 int total_handled, num_handled;
1096 u8 count = data->last_message_count;
1097
1098 if (count < 1 || count > data->max_reportid)
1099 count = 1;
1100
1101 /* include final invalid message */
1102 total_handled = mxt_read_and_process_messages(data, count + 1);
1103 if (total_handled < 0)
1104 return IRQ_NONE;
1105 /* if there were invalid messages, then we are done */
1106 else if (total_handled <= count)
1107 goto update_count;
1108
1109 /* keep reading two msgs until one is invalid or reportid limit */
1110 do {
1111 num_handled = mxt_read_and_process_messages(data, 2);
1112 if (num_handled < 0)
Nick Dyer8d4e1632014-05-18 23:00:56 -07001113 return IRQ_NONE;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001114
1115 total_handled += num_handled;
1116
1117 if (num_handled < 2)
1118 break;
1119 } while (total_handled < data->num_touchids);
1120
1121update_count:
1122 data->last_message_count = total_handled;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001123
Nick Dyerb9b05a82014-07-23 12:48:13 -07001124 if (data->update_input) {
Benson Leungb735fbe2014-07-23 12:22:27 -07001125 mxt_input_sync(data);
Nick Dyerb9b05a82014-07-23 12:48:13 -07001126 data->update_input = false;
1127 }
Daniel Kurtz64464ae2012-06-28 21:08:23 +08001128
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001129 return IRQ_HANDLED;
1130}
1131
Benson Leungd79e7e42014-05-18 23:02:52 -07001132static irqreturn_t mxt_interrupt(int irq, void *dev_id)
1133{
1134 struct mxt_data *data = dev_id;
1135
1136 if (data->in_bootloader) {
1137 /* bootloader state transition completion */
1138 complete(&data->bl_completion);
1139 return IRQ_HANDLED;
1140 }
1141
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001142 if (!data->object_table)
1143 return IRQ_HANDLED;
1144
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001145 if (data->T44_address) {
1146 return mxt_process_messages_t44(data);
1147 } else {
1148 return mxt_process_messages(data);
1149 }
Benson Leungd79e7e42014-05-18 23:02:52 -07001150}
1151
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001152static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
1153 u8 value, bool wait)
1154{
1155 u16 reg;
1156 u8 command_register;
1157 int timeout_counter = 0;
1158 int ret;
1159
1160 reg = data->T6_address + cmd_offset;
1161
1162 ret = mxt_write_reg(data->client, reg, value);
1163 if (ret)
1164 return ret;
1165
1166 if (!wait)
1167 return 0;
1168
1169 do {
1170 msleep(20);
1171 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
1172 if (ret)
1173 return ret;
1174 } while (command_register != 0 && timeout_counter++ <= 100);
1175
1176 if (timeout_counter > 100) {
1177 dev_err(&data->client->dev, "Command failed!\n");
1178 return -EIO;
1179 }
1180
1181 return 0;
1182}
1183
Nick Dyereb433352016-04-25 14:18:52 -07001184static int mxt_acquire_irq(struct mxt_data *data)
1185{
1186 int error;
1187
1188 enable_irq(data->irq);
1189
1190 error = mxt_process_messages_until_invalid(data);
1191 if (error)
1192 return error;
1193
1194 return 0;
1195}
1196
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001197static int mxt_soft_reset(struct mxt_data *data)
1198{
1199 struct device *dev = &data->client->dev;
1200 int ret = 0;
1201
Nick Dyer885f3fb2015-08-04 16:57:25 -07001202 dev_info(dev, "Resetting device\n");
1203
1204 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001205
1206 reinit_completion(&data->reset_completion);
1207
1208 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
1209 if (ret)
1210 return ret;
1211
Nick Dyer885f3fb2015-08-04 16:57:25 -07001212 /* Ignore CHG line for 100ms after reset */
Sebastian Reichelca1cd362018-05-24 09:33:36 -07001213 msleep(MXT_RESET_INVALID_CHG);
Nick Dyer885f3fb2015-08-04 16:57:25 -07001214
Nick Dyereb433352016-04-25 14:18:52 -07001215 mxt_acquire_irq(data);
Nick Dyer885f3fb2015-08-04 16:57:25 -07001216
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001217 ret = mxt_wait_for_completion(data, &data->reset_completion,
1218 MXT_RESET_TIMEOUT);
1219 if (ret)
1220 return ret;
1221
1222 return 0;
1223}
1224
Nick Dyerc3f78042014-05-18 23:04:46 -07001225static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
1226{
1227 /*
1228 * On failure, CRC is set to 0 and config will always be
1229 * downloaded.
1230 */
1231 data->config_crc = 0;
1232 reinit_completion(&data->crc_completion);
1233
1234 mxt_t6_command(data, cmd, value, true);
1235
1236 /*
1237 * Wait for crc message. On failure, CRC is set to 0 and config will
1238 * always be downloaded.
1239 */
1240 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
1241}
1242
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001243static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
1244{
1245 static const unsigned int crcpoly = 0x80001B;
1246 u32 result;
1247 u32 data_word;
1248
1249 data_word = (secondbyte << 8) | firstbyte;
1250 result = ((*crc << 1) ^ data_word);
1251
1252 if (result & 0x1000000)
1253 result ^= crcpoly;
1254
1255 *crc = result;
1256}
1257
1258static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
1259{
1260 u32 crc = 0;
1261 u8 *ptr = base + start_off;
1262 u8 *last_val = base + end_off - 1;
1263
1264 if (end_off < start_off)
1265 return -EINVAL;
1266
1267 while (ptr < last_val) {
1268 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
1269 ptr += 2;
1270 }
1271
1272 /* if len is odd, fill the last byte with 0 */
1273 if (ptr == last_val)
1274 mxt_calc_crc24(&crc, *ptr, 0);
1275
1276 /* Mask to 24-bit */
1277 crc &= 0x00FFFFFF;
1278
1279 return crc;
1280}
1281
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001282static int mxt_prepare_cfg_mem(struct mxt_data *data,
1283 const struct firmware *cfg,
1284 unsigned int data_pos,
1285 unsigned int cfg_start_ofs,
1286 u8 *config_mem,
1287 size_t config_mem_size)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001288{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001289 struct device *dev = &data->client->dev;
Nick Dyer50a77c62014-07-23 12:38:48 -07001290 struct mxt_object *object;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001291 unsigned int type, instance, size, byte_offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001292 int offset;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001293 int ret;
Nick Dyer50a77c62014-07-23 12:38:48 -07001294 int i;
Nick Dyer50a77c62014-07-23 12:38:48 -07001295 u16 reg;
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001296 u8 val;
Nick Dyer50a77c62014-07-23 12:38:48 -07001297
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001298 while (data_pos < cfg->size) {
Nick Dyer50a77c62014-07-23 12:38:48 -07001299 /* Read type, instance, length */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001300 ret = sscanf(cfg->data + data_pos, "%x %x %x%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001301 &type, &instance, &size, &offset);
1302 if (ret == 0) {
1303 /* EOF */
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001304 break;
Nick Dyer50a77c62014-07-23 12:38:48 -07001305 } else if (ret != 3) {
1306 dev_err(dev, "Bad format: failed to parse object\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001307 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001308 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001309 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001310
1311 object = mxt_get_object(data, type);
1312 if (!object) {
1313 /* Skip object */
1314 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001315 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001316 &val, &offset);
1317 if (ret != 1) {
1318 dev_err(dev, "Bad format in T%d at %d\n",
1319 type, i);
1320 return -EINVAL;
1321 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001322 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001323 }
1324 continue;
1325 }
1326
1327 if (size > mxt_obj_size(object)) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001328 /*
1329 * Either we are in fallback mode due to wrong
1330 * config or config from a later fw version,
1331 * or the file is corrupt or hand-edited.
1332 */
1333 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1334 size - mxt_obj_size(object), type);
1335 } else if (mxt_obj_size(object) > size) {
1336 /*
1337 * If firmware is upgraded, new bytes may be added to
1338 * end of objects. It is generally forward compatible
1339 * to zero these bytes - previous behaviour will be
1340 * retained. However this does invalidate the CRC and
1341 * will force fallback mode until the configuration is
1342 * updated. We warn here but do nothing else - the
1343 * malloc has zeroed the entire configuration.
1344 */
1345 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1346 mxt_obj_size(object) - size, type);
Nick Dyer50a77c62014-07-23 12:38:48 -07001347 }
1348
1349 if (instance >= mxt_obj_instances(object)) {
1350 dev_err(dev, "Object instances exceeded!\n");
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001351 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001352 }
1353
1354 reg = object->start_address + mxt_obj_size(object) * instance;
1355
1356 for (i = 0; i < size; i++) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001357 ret = sscanf(cfg->data + data_pos, "%hhx%n",
Nick Dyer50a77c62014-07-23 12:38:48 -07001358 &val,
1359 &offset);
1360 if (ret != 1) {
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001361 dev_err(dev, "Bad format in T%d at %d\n",
1362 type, i);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001363 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001364 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001365 data_pos += offset;
Nick Dyer50a77c62014-07-23 12:38:48 -07001366
1367 if (i > mxt_obj_size(object))
1368 continue;
1369
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001370 byte_offset = reg + i - cfg_start_ofs;
Nick Dyer50a77c62014-07-23 12:38:48 -07001371
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001372 if (byte_offset >= 0 && byte_offset < config_mem_size) {
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001373 *(config_mem + byte_offset) = val;
1374 } else {
1375 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1376 reg, object->type, byte_offset);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001377 return -EINVAL;
Nick Dyer50a77c62014-07-23 12:38:48 -07001378 }
1379 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001380 }
1381
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001382 return 0;
1383}
1384
1385static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start,
1386 u8 *config_mem, size_t config_mem_size)
1387{
1388 unsigned int byte_offset = 0;
1389 int error;
1390
1391 /* Write configuration as blocks */
1392 while (byte_offset < config_mem_size) {
1393 unsigned int size = config_mem_size - byte_offset;
1394
1395 if (size > MXT_MAX_BLOCK_WRITE)
1396 size = MXT_MAX_BLOCK_WRITE;
1397
1398 error = __mxt_write_reg(data->client,
1399 cfg_start + byte_offset,
1400 size, config_mem + byte_offset);
1401 if (error) {
1402 dev_err(&data->client->dev,
1403 "Config write error, ret=%d\n", error);
1404 return error;
1405 }
1406
1407 byte_offset += size;
1408 }
1409
1410 return 0;
1411}
1412
Nick Dyer7f3884f72015-08-04 16:36:29 -07001413static int mxt_init_t7_power_cfg(struct mxt_data *data);
1414
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001415/*
1416 * mxt_update_cfg - download configuration to chip
1417 *
1418 * Atmel Raw Config File Format
1419 *
1420 * The first four lines of the raw config file contain:
1421 * 1) Version
1422 * 2) Chip ID Information (first 7 bytes of device memory)
1423 * 3) Chip Information Block 24-bit CRC Checksum
1424 * 4) Chip Configuration 24-bit CRC Checksum
1425 *
1426 * The rest of the file consists of one line per object instance:
1427 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1428 *
1429 * <TYPE> - 2-byte object type as hex
1430 * <INSTANCE> - 2-byte object instance number as hex
1431 * <SIZE> - 2-byte object size as hex
1432 * <CONTENTS> - array of <SIZE> 1-byte hex values
1433 */
1434static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg)
1435{
1436 struct device *dev = &data->client->dev;
1437 struct mxt_info cfg_info;
1438 int ret;
1439 int offset;
1440 int data_pos;
1441 int i;
1442 int cfg_start_ofs;
1443 u32 info_crc, config_crc, calculated_crc;
1444 u8 *config_mem;
1445 size_t config_mem_size;
1446
1447 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
1448
1449 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
1450 dev_err(dev, "Unrecognised config file\n");
1451 return -EINVAL;
1452 }
1453
1454 data_pos = strlen(MXT_CFG_MAGIC);
1455
1456 /* Load information block and check */
1457 for (i = 0; i < sizeof(struct mxt_info); i++) {
1458 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1459 (unsigned char *)&cfg_info + i,
1460 &offset);
1461 if (ret != 1) {
1462 dev_err(dev, "Bad format\n");
1463 return -EINVAL;
1464 }
1465
1466 data_pos += offset;
1467 }
1468
Nick Dyer068bdb62018-05-01 11:40:18 -07001469 if (cfg_info.family_id != data->info->family_id) {
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001470 dev_err(dev, "Family ID mismatch!\n");
1471 return -EINVAL;
1472 }
1473
Nick Dyer068bdb62018-05-01 11:40:18 -07001474 if (cfg_info.variant_id != data->info->variant_id) {
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001475 dev_err(dev, "Variant ID mismatch!\n");
1476 return -EINVAL;
1477 }
1478
1479 /* Read CRCs */
1480 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset);
1481 if (ret != 1) {
1482 dev_err(dev, "Bad format: failed to parse Info CRC\n");
1483 return -EINVAL;
1484 }
1485 data_pos += offset;
1486
1487 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset);
1488 if (ret != 1) {
1489 dev_err(dev, "Bad format: failed to parse Config CRC\n");
1490 return -EINVAL;
1491 }
1492 data_pos += offset;
1493
1494 /*
1495 * The Info Block CRC is calculated over mxt_info and the object
1496 * table. If it does not match then we are trying to load the
1497 * configuration from a different chip or firmware version, so
1498 * the configuration CRC is invalid anyway.
1499 */
1500 if (info_crc == data->info_crc) {
1501 if (config_crc == 0 || data->config_crc == 0) {
1502 dev_info(dev, "CRC zero, attempting to apply config\n");
1503 } else if (config_crc == data->config_crc) {
1504 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
1505 data->config_crc);
1506 return 0;
1507 } else {
1508 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1509 data->config_crc, config_crc);
1510 }
1511 } else {
1512 dev_warn(dev,
1513 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1514 data->info_crc, info_crc);
1515 }
1516
1517 /* Malloc memory to store configuration */
1518 cfg_start_ofs = MXT_OBJECT_START +
Nick Dyer068bdb62018-05-01 11:40:18 -07001519 data->info->object_num * sizeof(struct mxt_object) +
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001520 MXT_INFO_CHECKSUM_SIZE;
1521 config_mem_size = data->mem_size - cfg_start_ofs;
1522 config_mem = kzalloc(config_mem_size, GFP_KERNEL);
1523 if (!config_mem) {
1524 dev_err(dev, "Failed to allocate memory\n");
1525 return -ENOMEM;
1526 }
1527
1528 ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs,
1529 config_mem, config_mem_size);
1530 if (ret)
1531 goto release_mem;
1532
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001533 /* Calculate crc of the received configs (not the raw config file) */
1534 if (data->T7_address < cfg_start_ofs) {
1535 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n",
1536 data->T7_address, cfg_start_ofs);
1537 ret = 0;
1538 goto release_mem;
1539 }
1540
1541 calculated_crc = mxt_calculate_crc(config_mem,
1542 data->T7_address - cfg_start_ofs,
1543 config_mem_size);
1544
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001545 if (config_crc > 0 && config_crc != calculated_crc)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001546 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n",
1547 calculated_crc, config_crc);
1548
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07001549 ret = mxt_upload_cfg_mem(data, cfg_start_ofs,
1550 config_mem, config_mem_size);
1551 if (ret)
1552 goto release_mem;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001553
Nick Dyerc3f78042014-05-18 23:04:46 -07001554 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1555
1556 ret = mxt_soft_reset(data);
1557 if (ret)
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001558 goto release_mem;
Nick Dyerc3f78042014-05-18 23:04:46 -07001559
1560 dev_info(dev, "Config successfully updated\n");
1561
Nick Dyer7f3884f72015-08-04 16:36:29 -07001562 /* T7 config may have changed */
1563 mxt_init_t7_power_cfg(data);
1564
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001565release_mem:
1566 kfree(config_mem);
Nick Dyer50a77c62014-07-23 12:38:48 -07001567 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001568}
1569
Stephen Warren58e4aeee2014-09-10 10:01:10 -07001570static void mxt_free_input_device(struct mxt_data *data)
1571{
1572 if (data->input_dev) {
1573 input_unregister_device(data->input_dev);
1574 data->input_dev = NULL;
1575 }
1576}
1577
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001578static void mxt_free_object_table(struct mxt_data *data)
1579{
Nick Dyerecfdd7e2016-07-18 18:10:32 -03001580#ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1581 video_unregister_device(&data->dbg.vdev);
1582 v4l2_device_unregister(&data->dbg.v4l2);
1583#endif
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001584 data->object_table = NULL;
Nick Dyer068bdb62018-05-01 11:40:18 -07001585 data->info = NULL;
1586 kfree(data->raw_info_block);
1587 data->raw_info_block = NULL;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001588 kfree(data->msg_buf);
1589 data->msg_buf = NULL;
Nick Dyerb9b05a82014-07-23 12:48:13 -07001590 data->T5_address = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001591 data->T5_msg_size = 0;
1592 data->T6_reportid = 0;
1593 data->T7_address = 0;
1594 data->T9_reportid_min = 0;
1595 data->T9_reportid_max = 0;
1596 data->T19_reportid = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001597 data->T44_address = 0;
Nick Dyerb23157d2015-04-06 11:25:13 -07001598 data->T100_reportid_min = 0;
1599 data->T100_reportid_max = 0;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001600 data->max_reportid = 0;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001601}
1602
Nick Dyer068bdb62018-05-01 11:40:18 -07001603static int mxt_parse_object_table(struct mxt_data *data,
1604 struct mxt_object *object_table)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001605{
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001606 struct i2c_client *client = data->client;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001607 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001608 u8 reportid;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001609 u16 end_address;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001610
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001611 /* Valid Report IDs start counting from 1 */
1612 reportid = 1;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001613 data->mem_size = 0;
Nick Dyer068bdb62018-05-01 11:40:18 -07001614 for (i = 0; i < data->info->object_num; i++) {
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001615 struct mxt_object *object = object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001616 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001617
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001618 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001619
1620 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001621 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001622 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001623 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001624 max_id = reportid - 1;
1625 } else {
1626 min_id = 0;
1627 max_id = 0;
1628 }
1629
1630 dev_dbg(&data->client->dev,
Nick Dyer7bed6802014-05-18 23:04:09 -07001631 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07001632 object->type, object->start_address,
1633 mxt_obj_size(object), mxt_obj_instances(object),
1634 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001635
1636 switch (object->type) {
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001637 case MXT_GEN_MESSAGE_T5:
Nick Dyer068bdb62018-05-01 11:40:18 -07001638 if (data->info->family_id == 0x80 &&
1639 data->info->version < 0x20) {
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001640 /*
Nick Dyer437d4f32014-08-07 09:56:01 -07001641 * On mXT224 firmware versions prior to V2.0
1642 * read and discard unused CRC byte otherwise
1643 * DMA reads are misaligned.
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001644 */
1645 data->T5_msg_size = mxt_obj_size(object);
1646 } else {
1647 /* CRC not enabled, so skip last byte */
1648 data->T5_msg_size = mxt_obj_size(object) - 1;
1649 }
Nick Dyerb9b05a82014-07-23 12:48:13 -07001650 data->T5_address = object->start_address;
Dmitry Torokhov041fa152014-08-11 10:58:29 -07001651 break;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001652 case MXT_GEN_COMMAND_T6:
1653 data->T6_reportid = min_id;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07001654 data->T6_address = object->start_address;
Daniel Kurtzfdf804212012-06-28 21:08:24 +08001655 break;
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001656 case MXT_GEN_POWER_T7:
1657 data->T7_address = object->start_address;
1658 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001659 case MXT_TOUCH_MULTI_T9:
Nick Dyerb23157d2015-04-06 11:25:13 -07001660 data->multitouch = MXT_TOUCH_MULTI_T9;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001661 data->T9_reportid_min = min_id;
1662 data->T9_reportid_max = max_id;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001663 data->num_touchids = object->num_report_ids
1664 * mxt_obj_instances(object);
1665 break;
1666 case MXT_SPT_MESSAGECOUNT_T44:
1667 data->T44_address = object->start_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +08001668 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001669 case MXT_SPT_GPIOPWM_T19:
1670 data->T19_reportid = min_id;
1671 break;
Nick Dyerb23157d2015-04-06 11:25:13 -07001672 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1673 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
1674 data->T100_reportid_min = min_id;
1675 data->T100_reportid_max = max_id;
1676 /* first two report IDs reserved */
1677 data->num_touchids = object->num_report_ids - 2;
1678 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001679 }
Nick Dyer4ce6fa02014-07-23 12:40:09 -07001680
1681 end_address = object->start_address
1682 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1683
1684 if (end_address >= data->mem_size)
1685 data->mem_size = end_address + 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001686 }
1687
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001688 /* Store maximum reportid */
1689 data->max_reportid = reportid;
1690
1691 /* If T44 exists, T5 position has to be directly after */
1692 if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
1693 dev_err(&client->dev, "Invalid T44 position\n");
Nick Dyer068bdb62018-05-01 11:40:18 -07001694 return -EINVAL;
Nick Dyer9d8dc3e2014-07-23 12:49:04 -07001695 }
1696
1697 data->msg_buf = kcalloc(data->max_reportid,
1698 data->T5_msg_size, GFP_KERNEL);
Nick Dyer068bdb62018-05-01 11:40:18 -07001699 if (!data->msg_buf)
1700 return -ENOMEM;
1701
1702 return 0;
1703}
1704
1705static int mxt_read_info_block(struct mxt_data *data)
1706{
1707 struct i2c_client *client = data->client;
1708 int error;
1709 size_t size;
1710 void *id_buf, *buf;
1711 uint8_t num_objects;
1712 u32 calculated_crc;
1713 u8 *crc_ptr;
1714
1715 /* If info block already allocated, free it */
1716 if (data->raw_info_block)
1717 mxt_free_object_table(data);
1718
1719 /* Read 7-byte ID information block starting at address 0 */
1720 size = sizeof(struct mxt_info);
1721 id_buf = kzalloc(size, GFP_KERNEL);
1722 if (!id_buf)
1723 return -ENOMEM;
1724
1725 error = __mxt_read_reg(client, 0, size, id_buf);
1726 if (error)
1727 goto err_free_mem;
1728
1729 /* Resize buffer to give space for rest of info block */
1730 num_objects = ((struct mxt_info *)id_buf)->object_num;
1731 size += (num_objects * sizeof(struct mxt_object))
1732 + MXT_INFO_CHECKSUM_SIZE;
1733
1734 buf = krealloc(id_buf, size, GFP_KERNEL);
1735 if (!buf) {
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001736 error = -ENOMEM;
Nick Dyer068bdb62018-05-01 11:40:18 -07001737 goto err_free_mem;
1738 }
1739 id_buf = buf;
1740
1741 /* Read rest of info block */
1742 error = __mxt_read_reg(client, MXT_OBJECT_START,
1743 size - MXT_OBJECT_START,
1744 id_buf + MXT_OBJECT_START);
1745 if (error)
1746 goto err_free_mem;
1747
1748 /* Extract & calculate checksum */
1749 crc_ptr = id_buf + size - MXT_INFO_CHECKSUM_SIZE;
1750 data->info_crc = crc_ptr[0] | (crc_ptr[1] << 8) | (crc_ptr[2] << 16);
1751
1752 calculated_crc = mxt_calculate_crc(id_buf, 0,
1753 size - MXT_INFO_CHECKSUM_SIZE);
1754
1755 /*
1756 * CRC mismatch can be caused by data corruption due to I2C comms
1757 * issue or else device is not using Object Based Protocol (eg i2c-hid)
1758 */
1759 if ((data->info_crc == 0) || (data->info_crc != calculated_crc)) {
1760 dev_err(&client->dev,
1761 "Info Block CRC error calculated=0x%06X read=0x%06X\n",
1762 calculated_crc, data->info_crc);
1763 error = -EIO;
1764 goto err_free_mem;
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001765 }
1766
Nick Dyer068bdb62018-05-01 11:40:18 -07001767 data->raw_info_block = id_buf;
1768 data->info = (struct mxt_info *)id_buf;
1769
1770 dev_info(&client->dev,
1771 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1772 data->info->family_id, data->info->variant_id,
1773 data->info->version >> 4, data->info->version & 0xf,
1774 data->info->build, data->info->object_num);
1775
1776 /* Parse object table information */
1777 error = mxt_parse_object_table(data, id_buf + MXT_OBJECT_START);
1778 if (error) {
1779 dev_err(&client->dev, "Error %d parsing object table\n", error);
1780 mxt_free_object_table(data);
1781 goto err_free_mem;
1782 }
1783
1784 data->object_table = (struct mxt_object *)(id_buf + MXT_OBJECT_START);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07001785
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001786 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001787
Nick Dyer068bdb62018-05-01 11:40:18 -07001788err_free_mem:
1789 kfree(id_buf);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07001790 return error;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001791}
1792
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001793static int mxt_read_t9_resolution(struct mxt_data *data)
1794{
1795 struct i2c_client *client = data->client;
1796 int error;
1797 struct t9_range range;
1798 unsigned char orient;
1799 struct mxt_object *object;
1800
1801 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1802 if (!object)
1803 return -EINVAL;
1804
1805 error = __mxt_read_reg(client,
Nick Dyer27864892016-07-18 18:10:33 -03001806 object->start_address + MXT_T9_XSIZE,
1807 sizeof(data->xsize), &data->xsize);
1808 if (error)
1809 return error;
1810
1811 error = __mxt_read_reg(client,
1812 object->start_address + MXT_T9_YSIZE,
1813 sizeof(data->ysize), &data->ysize);
1814 if (error)
1815 return error;
1816
1817 error = __mxt_read_reg(client,
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001818 object->start_address + MXT_T9_RANGE,
1819 sizeof(range), &range);
1820 if (error)
1821 return error;
1822
Nick Dyer1c0276d2016-01-10 18:29:34 -08001823 data->max_x = get_unaligned_le16(&range.x);
1824 data->max_y = get_unaligned_le16(&range.y);
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001825
1826 error = __mxt_read_reg(client,
1827 object->start_address + MXT_T9_ORIENT,
1828 1, &orient);
1829 if (error)
1830 return error;
1831
Nick Dyer1c0276d2016-01-10 18:29:34 -08001832 data->xy_switch = orient & MXT_T9_ORIENT_SWITCH;
Nick Dyerde601f72016-07-18 18:10:34 -03001833 data->invertx = orient & MXT_T9_ORIENT_INVERTX;
1834 data->inverty = orient & MXT_T9_ORIENT_INVERTY;
Nick Dyer61dc1ab2014-05-18 23:16:49 -07001835
1836 return 0;
1837}
1838
Nick Dyerb23157d2015-04-06 11:25:13 -07001839static int mxt_read_t100_config(struct mxt_data *data)
1840{
1841 struct i2c_client *client = data->client;
1842 int error;
1843 struct mxt_object *object;
1844 u16 range_x, range_y;
1845 u8 cfg, tchaux;
1846 u8 aux;
1847
1848 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
1849 if (!object)
1850 return -EINVAL;
1851
Nick Dyer1c0276d2016-01-10 18:29:34 -08001852 /* read touchscreen dimensions */
Nick Dyerb23157d2015-04-06 11:25:13 -07001853 error = __mxt_read_reg(client,
1854 object->start_address + MXT_T100_XRANGE,
1855 sizeof(range_x), &range_x);
1856 if (error)
1857 return error;
1858
Nick Dyer1c0276d2016-01-10 18:29:34 -08001859 data->max_x = get_unaligned_le16(&range_x);
Nick Dyerb23157d2015-04-06 11:25:13 -07001860
1861 error = __mxt_read_reg(client,
1862 object->start_address + MXT_T100_YRANGE,
1863 sizeof(range_y), &range_y);
1864 if (error)
1865 return error;
1866
Nick Dyer1c0276d2016-01-10 18:29:34 -08001867 data->max_y = get_unaligned_le16(&range_y);
Nick Dyerb23157d2015-04-06 11:25:13 -07001868
Nick Dyer27864892016-07-18 18:10:33 -03001869 error = __mxt_read_reg(client,
1870 object->start_address + MXT_T100_XSIZE,
1871 sizeof(data->xsize), &data->xsize);
1872 if (error)
1873 return error;
1874
1875 error = __mxt_read_reg(client,
1876 object->start_address + MXT_T100_YSIZE,
1877 sizeof(data->ysize), &data->ysize);
1878 if (error)
1879 return error;
1880
Nick Dyer1c0276d2016-01-10 18:29:34 -08001881 /* read orientation config */
Nick Dyerb23157d2015-04-06 11:25:13 -07001882 error = __mxt_read_reg(client,
1883 object->start_address + MXT_T100_CFG1,
1884 1, &cfg);
1885 if (error)
1886 return error;
1887
Nick Dyer1c0276d2016-01-10 18:29:34 -08001888 data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY;
Nick Dyerde601f72016-07-18 18:10:34 -03001889 data->invertx = cfg & MXT_T100_CFG_INVERTX;
1890 data->inverty = cfg & MXT_T100_CFG_INVERTY;
Nick Dyer1c0276d2016-01-10 18:29:34 -08001891
1892 /* allocate aux bytes */
Nick Dyerb23157d2015-04-06 11:25:13 -07001893 error = __mxt_read_reg(client,
1894 object->start_address + MXT_T100_TCHAUX,
1895 1, &tchaux);
1896 if (error)
1897 return error;
1898
Nick Dyerb23157d2015-04-06 11:25:13 -07001899 aux = 6;
1900
1901 if (tchaux & MXT_T100_TCHAUX_VECT)
1902 data->t100_aux_vect = aux++;
1903
1904 if (tchaux & MXT_T100_TCHAUX_AMPL)
1905 data->t100_aux_ampl = aux++;
1906
1907 if (tchaux & MXT_T100_TCHAUX_AREA)
1908 data->t100_aux_area = aux++;
1909
1910 dev_dbg(&client->dev,
1911 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1912 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
1913
Nick Dyerb23157d2015-04-06 11:25:13 -07001914 return 0;
1915}
1916
Nick Dyer7a53d602014-07-23 12:21:26 -07001917static int mxt_input_open(struct input_dev *dev);
1918static void mxt_input_close(struct input_dev *dev);
1919
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001920static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
1921 struct mxt_data *data)
1922{
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001923 int i;
1924
1925 input_dev->name = "Atmel maXTouch Touchpad";
1926
1927 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1928
1929 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1930 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1931 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1932 MXT_PIXELS_PER_MM);
1933 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1934 MXT_PIXELS_PER_MM);
1935
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07001936 for (i = 0; i < data->t19_num_keys; i++)
1937 if (data->t19_keymap[i] != KEY_RESERVED)
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001938 input_set_capability(input_dev, EV_KEY,
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07001939 data->t19_keymap[i]);
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001940}
1941
Nick Dyerb23157d2015-04-06 11:25:13 -07001942static int mxt_initialize_input_device(struct mxt_data *data)
Nick Dyer7a53d602014-07-23 12:21:26 -07001943{
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07001944 struct device *dev = &data->client->dev;
Nick Dyer7a53d602014-07-23 12:21:26 -07001945 struct input_dev *input_dev;
1946 int error;
1947 unsigned int num_mt_slots;
1948 unsigned int mt_flags = 0;
Nick Dyer7a53d602014-07-23 12:21:26 -07001949
Nick Dyerb23157d2015-04-06 11:25:13 -07001950 switch (data->multitouch) {
1951 case MXT_TOUCH_MULTI_T9:
1952 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1953 error = mxt_read_t9_resolution(data);
1954 if (error)
1955 dev_warn(dev, "Failed to initialize T9 resolution\n");
1956 break;
1957
1958 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1959 num_mt_slots = data->num_touchids;
1960 error = mxt_read_t100_config(data);
1961 if (error)
1962 dev_warn(dev, "Failed to read T100 config\n");
1963 break;
1964
1965 default:
1966 dev_err(dev, "Invalid multitouch object\n");
1967 return -EINVAL;
1968 }
Nick Dyer7a53d602014-07-23 12:21:26 -07001969
Nick Dyer1c0276d2016-01-10 18:29:34 -08001970 /* Handle default values and orientation switch */
1971 if (data->max_x == 0)
1972 data->max_x = 1023;
1973
1974 if (data->max_y == 0)
1975 data->max_y = 1023;
1976
1977 if (data->xy_switch)
1978 swap(data->max_x, data->max_y);
1979
1980 dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1981
1982 /* Register input device */
Nick Dyer7a53d602014-07-23 12:21:26 -07001983 input_dev = input_allocate_device();
1984 if (!input_dev) {
1985 dev_err(dev, "Failed to allocate memory\n");
1986 return -ENOMEM;
1987 }
1988
1989 input_dev->name = "Atmel maXTouch Touchscreen";
1990 input_dev->phys = data->phys;
1991 input_dev->id.bustype = BUS_I2C;
1992 input_dev->dev.parent = dev;
1993 input_dev->open = mxt_input_open;
1994 input_dev->close = mxt_input_close;
1995
Nick Dyerb23157d2015-04-06 11:25:13 -07001996 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
Nick Dyer7a53d602014-07-23 12:21:26 -07001997
Nick Dyer7a53d602014-07-23 12:21:26 -07001998 /* For single touch */
Nick Dyerb23157d2015-04-06 11:25:13 -07001999 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
2000 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
2001
2002 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2003 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2004 data->t100_aux_ampl)) {
2005 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
2006 }
Nick Dyer7a53d602014-07-23 12:21:26 -07002007
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07002008 /* If device has buttons we assume it is a touchpad */
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002009 if (data->t19_num_keys) {
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07002010 mxt_set_up_as_touchpad(input_dev, data);
2011 mt_flags |= INPUT_MT_POINTER;
Nick Dyereafc0c82015-08-04 16:57:40 -07002012 } else {
2013 mt_flags |= INPUT_MT_DIRECT;
Sjoerd Simonsb6d2d322015-04-06 13:10:30 -07002014 }
2015
Nick Dyer7a53d602014-07-23 12:21:26 -07002016 /* For multi touch */
Nick Dyer7a53d602014-07-23 12:21:26 -07002017 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
2018 if (error) {
2019 dev_err(dev, "Error %d initialising slots\n", error);
2020 goto err_free_mem;
2021 }
2022
Nick Dyerb23157d2015-04-06 11:25:13 -07002023 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
2024 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
2025 0, MT_TOOL_MAX, 0, 0);
2026 input_set_abs_params(input_dev, ABS_MT_DISTANCE,
2027 MXT_DISTANCE_ACTIVE_TOUCH,
2028 MXT_DISTANCE_HOVERING,
2029 0, 0);
2030 }
2031
Nick Dyer7a53d602014-07-23 12:21:26 -07002032 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
2033 0, data->max_x, 0, 0);
2034 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
2035 0, data->max_y, 0, 0);
Nick Dyerb23157d2015-04-06 11:25:13 -07002036
2037 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2038 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2039 data->t100_aux_area)) {
2040 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
2041 0, MXT_MAX_AREA, 0, 0);
2042 }
2043
2044 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
2045 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2046 data->t100_aux_ampl)) {
2047 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
2048 0, 255, 0, 0);
2049 }
2050
2051 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2052 data->t100_aux_vect) {
2053 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2054 0, 255, 0, 0);
2055 }
2056
2057 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2058 data->t100_aux_ampl) {
2059 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
2060 0, 255, 0, 0);
2061 }
2062
2063 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2064 data->t100_aux_vect) {
2065 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2066 0, 255, 0, 0);
2067 }
Nick Dyer7a53d602014-07-23 12:21:26 -07002068
2069 input_set_drvdata(input_dev, data);
2070
2071 error = input_register_device(input_dev);
2072 if (error) {
2073 dev_err(dev, "Error %d registering input device\n", error);
2074 goto err_free_mem;
2075 }
2076
2077 data->input_dev = input_dev;
2078
2079 return 0;
2080
2081err_free_mem:
2082 input_free_device(input_dev);
2083 return error;
2084}
2085
Nick Dyer50a77c62014-07-23 12:38:48 -07002086static int mxt_configure_objects(struct mxt_data *data,
2087 const struct firmware *cfg);
2088
2089static void mxt_config_cb(const struct firmware *cfg, void *ctx)
2090{
2091 mxt_configure_objects(ctx, cfg);
Dmitry Torokhovefdbd7a2014-08-08 09:29:06 -07002092 release_firmware(cfg);
Nick Dyer50a77c62014-07-23 12:38:48 -07002093}
2094
Iiro Valkonen7686b102011-02-02 23:21:58 -08002095static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002096{
2097 struct i2c_client *client = data->client;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002098 int recovery_attempts = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002099 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002100
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002101 while (1) {
Nick Dyer068bdb62018-05-01 11:40:18 -07002102 error = mxt_read_info_block(data);
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002103 if (!error)
2104 break;
2105
2106 /* Check bootloader state */
2107 error = mxt_probe_bootloader(data, false);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002108 if (error) {
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002109 dev_info(&client->dev, "Trying alternate bootloader address\n");
2110 error = mxt_probe_bootloader(data, true);
2111 if (error) {
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002112 /* Chip is not in appmode or bootloader mode */
2113 return error;
2114 }
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002115 }
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002116
2117 /* OK, we are in bootloader, see if we can recover */
2118 if (++recovery_attempts > 1) {
2119 dev_err(&client->dev, "Could not recover from bootloader mode\n");
2120 /*
2121 * We can reflash from this state, so do not
2122 * abort initialization.
2123 */
2124 data->in_bootloader = true;
2125 return 0;
2126 }
2127
2128 /* Attempt to exit bootloader into app mode */
2129 mxt_send_bootloader_cmd(data, false);
2130 msleep(MXT_FW_RESET_TIME);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002131 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002132
Dmitry Torokhov041fa152014-08-11 10:58:29 -07002133 error = mxt_acquire_irq(data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002134 if (error)
Nick Dyer068bdb62018-05-01 11:40:18 -07002135 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002136
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002137 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
2138 &client->dev, GFP_KERNEL, data,
2139 mxt_config_cb);
2140 if (error) {
2141 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
2142 error);
Nick Dyer068bdb62018-05-01 11:40:18 -07002143 return error;
Dmitry Torokhov6cd1ab02014-08-08 09:28:45 -07002144 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002145
2146 return 0;
2147}
2148
Nick Dyer7f3884f72015-08-04 16:36:29 -07002149static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
2150{
2151 struct device *dev = &data->client->dev;
2152 int error;
2153 struct t7_config *new_config;
2154 struct t7_config deepsleep = { .active = 0, .idle = 0 };
2155
2156 if (sleep == MXT_POWER_CFG_DEEPSLEEP)
2157 new_config = &deepsleep;
2158 else
2159 new_config = &data->t7_cfg;
2160
2161 error = __mxt_write_reg(data->client, data->T7_address,
2162 sizeof(data->t7_cfg), new_config);
2163 if (error)
2164 return error;
2165
2166 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
2167 new_config->active, new_config->idle);
2168
2169 return 0;
2170}
2171
2172static int mxt_init_t7_power_cfg(struct mxt_data *data)
2173{
2174 struct device *dev = &data->client->dev;
2175 int error;
2176 bool retry = false;
2177
2178recheck:
2179 error = __mxt_read_reg(data->client, data->T7_address,
2180 sizeof(data->t7_cfg), &data->t7_cfg);
2181 if (error)
2182 return error;
2183
2184 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
2185 if (!retry) {
2186 dev_dbg(dev, "T7 cfg zero, resetting\n");
2187 mxt_soft_reset(data);
2188 retry = true;
2189 goto recheck;
2190 } else {
2191 dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
2192 data->t7_cfg.active = 20;
2193 data->t7_cfg.idle = 100;
2194 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2195 }
2196 }
2197
2198 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
2199 data->t7_cfg.active, data->t7_cfg.idle);
2200 return 0;
2201}
2202
Nick Dyerd6a39402016-07-18 18:10:31 -03002203#ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2204static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x,
2205 unsigned int y)
2206{
Nick Dyer068bdb62018-05-01 11:40:18 -07002207 struct mxt_info *info = data->info;
Nick Dyerd6a39402016-07-18 18:10:31 -03002208 struct mxt_dbg *dbg = &data->dbg;
2209 unsigned int ofs, page;
Nick Dyer566d5332016-07-18 18:10:35 -03002210 unsigned int col = 0;
2211 unsigned int col_width;
Nick Dyerd6a39402016-07-18 18:10:31 -03002212
Nick Dyer566d5332016-07-18 18:10:35 -03002213 if (info->family_id == MXT_FAMILY_1386) {
2214 col_width = info->matrix_ysize / MXT1386_COLUMNS;
2215 col = y / col_width;
2216 y = y % col_width;
2217 } else {
2218 col_width = info->matrix_ysize;
2219 }
2220
2221 ofs = (y + (x * col_width)) * sizeof(u16);
Nick Dyerd6a39402016-07-18 18:10:31 -03002222 page = ofs / MXT_DIAGNOSTIC_SIZE;
2223 ofs %= MXT_DIAGNOSTIC_SIZE;
2224
Nick Dyer566d5332016-07-18 18:10:35 -03002225 if (info->family_id == MXT_FAMILY_1386)
2226 page += col * MXT1386_PAGES_PER_COLUMN;
2227
Nick Dyerd6a39402016-07-18 18:10:31 -03002228 return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]);
2229}
2230
2231static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf)
2232{
2233 struct mxt_dbg *dbg = &data->dbg;
2234 unsigned int x = 0;
2235 unsigned int y = 0;
Nick Dyerde601f72016-07-18 18:10:34 -03002236 unsigned int i, rx, ry;
Nick Dyerd6a39402016-07-18 18:10:31 -03002237
2238 for (i = 0; i < dbg->t37_nodes; i++) {
Nick Dyerde601f72016-07-18 18:10:34 -03002239 /* Handle orientation */
2240 rx = data->xy_switch ? y : x;
2241 ry = data->xy_switch ? x : y;
2242 rx = data->invertx ? (data->xsize - 1 - rx) : rx;
2243 ry = data->inverty ? (data->ysize - 1 - ry) : ry;
2244
2245 outbuf[i] = mxt_get_debug_value(data, rx, ry);
Nick Dyerd6a39402016-07-18 18:10:31 -03002246
2247 /* Next value */
Nick Dyerde601f72016-07-18 18:10:34 -03002248 if (++x >= (data->xy_switch ? data->ysize : data->xsize)) {
Nick Dyerd6a39402016-07-18 18:10:31 -03002249 x = 0;
2250 y++;
2251 }
2252 }
2253
2254 return 0;
2255}
2256
2257static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode,
2258 u16 *outbuf)
2259{
2260 struct mxt_dbg *dbg = &data->dbg;
2261 int retries = 0;
2262 int page;
2263 int ret;
2264 u8 cmd = mode;
2265 struct t37_debug *p;
2266 u8 cmd_poll;
2267
2268 for (page = 0; page < dbg->t37_pages; page++) {
2269 p = dbg->t37_buf + page;
2270
2271 ret = mxt_write_reg(data->client, dbg->diag_cmd_address,
2272 cmd);
2273 if (ret)
2274 return ret;
2275
2276 retries = 0;
2277 msleep(20);
2278wait_cmd:
2279 /* Read back command byte */
2280 ret = __mxt_read_reg(data->client, dbg->diag_cmd_address,
2281 sizeof(cmd_poll), &cmd_poll);
2282 if (ret)
2283 return ret;
2284
2285 /* Field is cleared once the command has been processed */
2286 if (cmd_poll) {
2287 if (retries++ > 100)
2288 return -EINVAL;
2289
2290 msleep(20);
2291 goto wait_cmd;
2292 }
2293
2294 /* Read T37 page */
2295 ret = __mxt_read_reg(data->client, dbg->t37_address,
2296 sizeof(struct t37_debug), p);
2297 if (ret)
2298 return ret;
2299
2300 if (p->mode != mode || p->page != page) {
2301 dev_err(&data->client->dev, "T37 page mismatch\n");
2302 return -EINVAL;
2303 }
2304
2305 dev_dbg(&data->client->dev, "%s page:%d retries:%d\n",
2306 __func__, page, retries);
2307
2308 /* For remaining pages, write PAGEUP rather than mode */
2309 cmd = MXT_DIAGNOSTIC_PAGEUP;
2310 }
2311
2312 return mxt_convert_debug_pages(data, outbuf);
2313}
2314
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002315static int mxt_queue_setup(struct vb2_queue *q,
2316 unsigned int *nbuffers, unsigned int *nplanes,
2317 unsigned int sizes[], struct device *alloc_devs[])
2318{
2319 struct mxt_data *data = q->drv_priv;
2320 size_t size = data->dbg.t37_nodes * sizeof(u16);
2321
2322 if (*nplanes)
2323 return sizes[0] < size ? -EINVAL : 0;
2324
2325 *nplanes = 1;
2326 sizes[0] = size;
2327
2328 return 0;
2329}
2330
2331static void mxt_buffer_queue(struct vb2_buffer *vb)
2332{
2333 struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue);
2334 u16 *ptr;
2335 int ret;
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002336 u8 mode;
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002337
2338 ptr = vb2_plane_vaddr(vb, 0);
2339 if (!ptr) {
2340 dev_err(&data->client->dev, "Error acquiring frame ptr\n");
2341 goto fault;
2342 }
2343
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002344 switch (data->dbg.input) {
2345 case MXT_V4L_INPUT_DELTAS:
2346 default:
2347 mode = MXT_DIAGNOSTIC_DELTAS;
2348 break;
2349
2350 case MXT_V4L_INPUT_REFS:
2351 mode = MXT_DIAGNOSTIC_REFS;
2352 break;
2353 }
2354
2355 ret = mxt_read_diagnostic_debug(data, mode, ptr);
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002356 if (ret)
2357 goto fault;
2358
2359 vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16));
2360 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2361 return;
2362
2363fault:
2364 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2365}
2366
2367/* V4L2 structures */
2368static const struct vb2_ops mxt_queue_ops = {
2369 .queue_setup = mxt_queue_setup,
2370 .buf_queue = mxt_buffer_queue,
2371 .wait_prepare = vb2_ops_wait_prepare,
2372 .wait_finish = vb2_ops_wait_finish,
2373};
2374
2375static const struct vb2_queue mxt_queue = {
2376 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2377 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
2378 .buf_struct_size = sizeof(struct mxt_vb2_buffer),
2379 .ops = &mxt_queue_ops,
2380 .mem_ops = &vb2_vmalloc_memops,
2381 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
2382 .min_buffers_needed = 1,
2383};
2384
2385static int mxt_vidioc_querycap(struct file *file, void *priv,
2386 struct v4l2_capability *cap)
2387{
2388 struct mxt_data *data = video_drvdata(file);
2389
2390 strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver));
2391 strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card));
2392 snprintf(cap->bus_info, sizeof(cap->bus_info),
2393 "I2C:%s", dev_name(&data->client->dev));
2394 return 0;
2395}
2396
2397static int mxt_vidioc_enum_input(struct file *file, void *priv,
2398 struct v4l2_input *i)
2399{
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002400 if (i->index >= MXT_V4L_INPUT_MAX)
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002401 return -EINVAL;
2402
2403 i->type = V4L2_INPUT_TYPE_TOUCH;
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002404
2405 switch (i->index) {
2406 case MXT_V4L_INPUT_REFS:
2407 strlcpy(i->name, "Mutual Capacitance References",
2408 sizeof(i->name));
2409 break;
2410 case MXT_V4L_INPUT_DELTAS:
2411 strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name));
2412 break;
2413 }
2414
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002415 return 0;
2416}
2417
2418static int mxt_set_input(struct mxt_data *data, unsigned int i)
2419{
2420 struct v4l2_pix_format *f = &data->dbg.format;
2421
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002422 if (i >= MXT_V4L_INPUT_MAX)
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002423 return -EINVAL;
2424
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002425 if (i == MXT_V4L_INPUT_DELTAS)
2426 f->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2427 else
2428 f->pixelformat = V4L2_TCH_FMT_TU16;
2429
Nick Dyerde601f72016-07-18 18:10:34 -03002430 f->width = data->xy_switch ? data->ysize : data->xsize;
2431 f->height = data->xy_switch ? data->xsize : data->ysize;
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002432 f->field = V4L2_FIELD_NONE;
2433 f->colorspace = V4L2_COLORSPACE_RAW;
2434 f->bytesperline = f->width * sizeof(u16);
2435 f->sizeimage = f->width * f->height * sizeof(u16);
2436
2437 data->dbg.input = i;
2438
2439 return 0;
2440}
2441
2442static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i)
2443{
2444 return mxt_set_input(video_drvdata(file), i);
2445}
2446
2447static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
2448{
2449 struct mxt_data *data = video_drvdata(file);
2450
2451 *i = data->dbg.input;
2452
2453 return 0;
2454}
2455
2456static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f)
2457{
2458 struct mxt_data *data = video_drvdata(file);
2459
2460 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2461 f->fmt.pix = data->dbg.format;
2462
2463 return 0;
2464}
2465
2466static int mxt_vidioc_enum_fmt(struct file *file, void *priv,
2467 struct v4l2_fmtdesc *fmt)
2468{
2469 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2470 return -EINVAL;
2471
Nick Dyer06b3d3f2016-07-18 18:10:36 -03002472 switch (fmt->index) {
2473 case 0:
2474 fmt->pixelformat = V4L2_TCH_FMT_TU16;
2475 break;
2476
2477 case 1:
2478 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2479 break;
2480
2481 default:
2482 return -EINVAL;
2483 }
2484
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002485 return 0;
2486}
2487
2488static int mxt_vidioc_g_parm(struct file *file, void *fh,
2489 struct v4l2_streamparm *a)
2490{
2491 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2492 return -EINVAL;
2493
2494 a->parm.capture.readbuffers = 1;
2495 a->parm.capture.timeperframe.numerator = 1;
2496 a->parm.capture.timeperframe.denominator = 10;
2497 return 0;
2498}
2499
2500static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = {
2501 .vidioc_querycap = mxt_vidioc_querycap,
2502
2503 .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt,
2504 .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt,
2505 .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt,
2506 .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt,
2507 .vidioc_g_parm = mxt_vidioc_g_parm,
2508
2509 .vidioc_enum_input = mxt_vidioc_enum_input,
2510 .vidioc_g_input = mxt_vidioc_g_input,
2511 .vidioc_s_input = mxt_vidioc_s_input,
2512
2513 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2514 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2515 .vidioc_querybuf = vb2_ioctl_querybuf,
2516 .vidioc_qbuf = vb2_ioctl_qbuf,
2517 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2518 .vidioc_expbuf = vb2_ioctl_expbuf,
2519
2520 .vidioc_streamon = vb2_ioctl_streamon,
2521 .vidioc_streamoff = vb2_ioctl_streamoff,
2522};
2523
2524static const struct video_device mxt_video_device = {
2525 .name = "Atmel maxTouch",
2526 .fops = &mxt_video_fops,
2527 .ioctl_ops = &mxt_video_ioctl_ops,
2528 .release = video_device_release_empty,
2529 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
2530 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
2531};
2532
Nick Dyerd6a39402016-07-18 18:10:31 -03002533static void mxt_debug_init(struct mxt_data *data)
2534{
Nick Dyer068bdb62018-05-01 11:40:18 -07002535 struct mxt_info *info = data->info;
Nick Dyerd6a39402016-07-18 18:10:31 -03002536 struct mxt_dbg *dbg = &data->dbg;
2537 struct mxt_object *object;
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002538 int error;
Nick Dyerd6a39402016-07-18 18:10:31 -03002539
2540 object = mxt_get_object(data, MXT_GEN_COMMAND_T6);
2541 if (!object)
2542 goto error;
2543
2544 dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC;
2545
2546 object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37);
2547 if (!object)
2548 goto error;
2549
2550 if (mxt_obj_size(object) != sizeof(struct t37_debug)) {
2551 dev_warn(&data->client->dev, "Bad T37 size");
2552 goto error;
2553 }
2554
2555 dbg->t37_address = object->start_address;
2556
2557 /* Calculate size of data and allocate buffer */
Nick Dyer27864892016-07-18 18:10:33 -03002558 dbg->t37_nodes = data->xsize * data->ysize;
Nick Dyer566d5332016-07-18 18:10:35 -03002559
2560 if (info->family_id == MXT_FAMILY_1386)
2561 dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN;
2562 else
2563 dbg->t37_pages = DIV_ROUND_UP(data->xsize *
Guenter Roeckd7ddf152017-01-21 23:46:47 -08002564 info->matrix_ysize *
Nick Dyer566d5332016-07-18 18:10:35 -03002565 sizeof(u16),
2566 sizeof(dbg->t37_buf->data));
Nick Dyerd6a39402016-07-18 18:10:31 -03002567
2568 dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages,
2569 sizeof(struct t37_debug), GFP_KERNEL);
2570 if (!dbg->t37_buf)
2571 goto error;
2572
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002573 /* init channel to zero */
2574 mxt_set_input(data, 0);
2575
2576 /* register video device */
2577 snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts");
2578 error = v4l2_device_register(&data->client->dev, &dbg->v4l2);
2579 if (error)
2580 goto error;
2581
2582 /* initialize the queue */
2583 mutex_init(&dbg->lock);
2584 dbg->queue = mxt_queue;
2585 dbg->queue.drv_priv = data;
2586 dbg->queue.lock = &dbg->lock;
2587 dbg->queue.dev = &data->client->dev;
2588
2589 error = vb2_queue_init(&dbg->queue);
2590 if (error)
2591 goto error_unreg_v4l2;
2592
2593 dbg->vdev = mxt_video_device;
2594 dbg->vdev.v4l2_dev = &dbg->v4l2;
2595 dbg->vdev.lock = &dbg->lock;
2596 dbg->vdev.vfl_dir = VFL_DIR_RX;
2597 dbg->vdev.queue = &dbg->queue;
2598 video_set_drvdata(&dbg->vdev, data);
2599
2600 error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1);
2601 if (error)
2602 goto error_unreg_v4l2;
2603
Nick Dyerd6a39402016-07-18 18:10:31 -03002604 return;
2605
Nick Dyerecfdd7e2016-07-18 18:10:32 -03002606error_unreg_v4l2:
2607 v4l2_device_unregister(&dbg->v4l2);
Nick Dyerd6a39402016-07-18 18:10:31 -03002608error:
2609 dev_warn(&data->client->dev, "Error initializing T37\n");
2610}
2611#else
2612static void mxt_debug_init(struct mxt_data *data)
2613{
2614}
2615#endif
2616
Nick Dyer50a77c62014-07-23 12:38:48 -07002617static int mxt_configure_objects(struct mxt_data *data,
2618 const struct firmware *cfg)
2619{
2620 struct device *dev = &data->client->dev;
Nick Dyer50a77c62014-07-23 12:38:48 -07002621 int error;
2622
Nick Dyer7f3884f72015-08-04 16:36:29 -07002623 error = mxt_init_t7_power_cfg(data);
2624 if (error) {
2625 dev_err(dev, "Failed to initialize power cfg\n");
2626 return error;
2627 }
2628
Nick Dyer50a77c62014-07-23 12:38:48 -07002629 if (cfg) {
2630 error = mxt_update_cfg(data, cfg);
2631 if (error)
2632 dev_warn(dev, "Error %d updating config\n", error);
2633 }
2634
Nick Dyerb23157d2015-04-06 11:25:13 -07002635 if (data->multitouch) {
2636 error = mxt_initialize_input_device(data);
2637 if (error)
2638 return error;
2639 } else {
2640 dev_warn(dev, "No touch object detected\n");
2641 }
Nick Dyer50a77c62014-07-23 12:38:48 -07002642
Nick Dyerd6a39402016-07-18 18:10:31 -03002643 mxt_debug_init(data);
2644
Nick Dyer50a77c62014-07-23 12:38:48 -07002645 return 0;
2646}
2647
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002648/* Firmware Version is returned as Major.Minor.Build */
2649static ssize_t mxt_fw_version_show(struct device *dev,
2650 struct device_attribute *attr, char *buf)
2651{
2652 struct mxt_data *data = dev_get_drvdata(dev);
Nick Dyer068bdb62018-05-01 11:40:18 -07002653 struct mxt_info *info = data->info;
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002654 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
2655 info->version >> 4, info->version & 0xf, info->build);
2656}
2657
2658/* Hardware Version is returned as FamilyID.VariantID */
2659static ssize_t mxt_hw_version_show(struct device *dev,
2660 struct device_attribute *attr, char *buf)
2661{
2662 struct mxt_data *data = dev_get_drvdata(dev);
Nick Dyer068bdb62018-05-01 11:40:18 -07002663 struct mxt_info *info = data->info;
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002664 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
2665 info->family_id, info->variant_id);
2666}
2667
Daniel Kurtz794eb672012-06-28 21:08:10 +08002668static ssize_t mxt_show_instance(char *buf, int count,
2669 struct mxt_object *object, int instance,
2670 const u8 *val)
2671{
2672 int i;
2673
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002674 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002675 count += scnprintf(buf + count, PAGE_SIZE - count,
2676 "Instance %u\n", instance);
2677
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002678 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002679 count += scnprintf(buf + count, PAGE_SIZE - count,
2680 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
2681 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
2682
2683 return count;
2684}
2685
Iiro Valkonen7686b102011-02-02 23:21:58 -08002686static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002687 struct device_attribute *attr, char *buf)
2688{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002689 struct mxt_data *data = dev_get_drvdata(dev);
2690 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002691 int count = 0;
2692 int i, j;
2693 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002694 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002695
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002696 /* Pre-allocate buffer large enough to hold max sized object. */
2697 obuf = kmalloc(256, GFP_KERNEL);
2698 if (!obuf)
2699 return -ENOMEM;
2700
2701 error = 0;
Nick Dyer068bdb62018-05-01 11:40:18 -07002702 for (i = 0; i < data->info->object_num; i++) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002703 object = data->object_table + i;
2704
Daniel Kurtz91630952012-06-28 21:08:09 +08002705 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002706 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002707
Daniel Kurtz91630952012-06-28 21:08:09 +08002708 count += scnprintf(buf + count, PAGE_SIZE - count,
2709 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002710
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07002711 for (j = 0; j < mxt_obj_instances(object); j++) {
2712 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +08002713 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002714
Daniel Kurtz794eb672012-06-28 21:08:10 +08002715 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002716 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +08002717 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002718
Daniel Kurtz794eb672012-06-28 21:08:10 +08002719 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002720 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002721 }
2722
Daniel Kurtz794eb672012-06-28 21:08:10 +08002723done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +08002724 kfree(obuf);
2725 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002726}
2727
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002728static int mxt_check_firmware_format(struct device *dev,
2729 const struct firmware *fw)
2730{
2731 unsigned int pos = 0;
2732 char c;
2733
2734 while (pos < fw->size) {
2735 c = *(fw->data + pos);
2736
2737 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
2738 return 0;
2739
2740 pos++;
2741 }
2742
2743 /*
2744 * To convert file try:
2745 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2746 */
2747 dev_err(dev, "Aborting: firmware file must be in binary format\n");
2748
2749 return -EINVAL;
2750}
2751
Iiro Valkonen7686b102011-02-02 23:21:58 -08002752static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002753{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002754 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002755 const struct firmware *fw = NULL;
2756 unsigned int frame_size;
2757 unsigned int pos = 0;
Nick Dyerf943c742014-05-18 23:14:20 -07002758 unsigned int retry = 0;
Nick Dyerf477c752014-05-18 23:14:45 -07002759 unsigned int frame = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002760 int ret;
2761
2762 ret = request_firmware(&fw, fn, dev);
2763 if (ret) {
2764 dev_err(dev, "Unable to open firmware %s\n", fn);
2765 return ret;
2766 }
2767
Nick Dyerf2ac6cb2014-05-18 23:15:01 -07002768 /* Check for incorrect enc file */
2769 ret = mxt_check_firmware_format(dev, fw);
2770 if (ret)
2771 goto release_firmware;
2772
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002773 if (!data->in_bootloader) {
2774 /* Change to the bootloader mode */
2775 data->in_bootloader = true;
Benson Leungd79e7e42014-05-18 23:02:52 -07002776
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002777 ret = mxt_t6_command(data, MXT_COMMAND_RESET,
2778 MXT_BOOT_VALUE, false);
2779 if (ret)
2780 goto release_firmware;
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002781
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002782 msleep(MXT_RESET_TIME);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002783
2784 /* Do not need to scan since we know family ID */
2785 ret = mxt_lookup_bootloader_address(data, 0);
2786 if (ret)
2787 goto release_firmware;
Stephen Warren58e4aeee2014-09-10 10:01:10 -07002788
2789 mxt_free_input_device(data);
2790 mxt_free_object_table(data);
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002791 } else {
2792 enable_irq(data->irq);
Nick Dyera9fdd1e2014-07-23 12:41:58 -07002793 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002794
Benson Leungd79e7e42014-05-18 23:02:52 -07002795 reinit_completion(&data->bl_completion);
2796
Nick Dyer385deb92014-07-23 12:42:40 -07002797 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
2798 if (ret) {
2799 /* Bootloader may still be unlocked from previous attempt */
2800 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
2801 if (ret)
2802 goto disable_irq;
2803 } else {
2804 dev_info(dev, "Unlocking bootloader\n");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002805
Nick Dyer385deb92014-07-23 12:42:40 -07002806 /* Unlock bootloader */
Nick Dyer8efaa5e2014-07-23 12:45:49 -07002807 ret = mxt_send_bootloader_cmd(data, true);
Nick Dyer385deb92014-07-23 12:42:40 -07002808 if (ret)
2809 goto disable_irq;
2810 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002811
2812 while (pos < fw->size) {
Nick Dyer385deb92014-07-23 12:42:40 -07002813 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002814 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07002815 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002816
2817 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
2818
Nick Dyerf943c742014-05-18 23:14:20 -07002819 /* Take account of CRC bytes */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002820 frame_size += 2;
2821
2822 /* Write one frame to device */
Nick Dyerf28a8422014-05-18 23:10:49 -07002823 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
2824 if (ret)
2825 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002826
Nick Dyer385deb92014-07-23 12:42:40 -07002827 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
Nick Dyerf943c742014-05-18 23:14:20 -07002828 if (ret) {
2829 retry++;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002830
Nick Dyerf943c742014-05-18 23:14:20 -07002831 /* Back off by 20ms per retry */
2832 msleep(retry * 20);
2833
2834 if (retry > 20) {
2835 dev_err(dev, "Retry count exceeded\n");
2836 goto disable_irq;
2837 }
2838 } else {
2839 retry = 0;
2840 pos += frame_size;
Nick Dyerf477c752014-05-18 23:14:45 -07002841 frame++;
Nick Dyerf943c742014-05-18 23:14:20 -07002842 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002843
Nick Dyerf477c752014-05-18 23:14:45 -07002844 if (frame % 50 == 0)
2845 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
2846 frame, pos, fw->size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002847 }
2848
Benson Leunga0434b72014-05-18 23:03:09 -07002849 /* Wait for flash. */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002850 ret = mxt_wait_for_completion(data, &data->bl_completion,
2851 MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002852 if (ret)
2853 goto disable_irq;
2854
Nick Dyerf477c752014-05-18 23:14:45 -07002855 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
2856
Benson Leunga0434b72014-05-18 23:03:09 -07002857 /*
2858 * Wait for device to reset. Some bootloader versions do not assert
2859 * the CHG line after bootloading has finished, so ignore potential
2860 * errors.
2861 */
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002862 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
Benson Leunga0434b72014-05-18 23:03:09 -07002863
Benson Leungd79e7e42014-05-18 23:02:52 -07002864 data->in_bootloader = false;
2865
2866disable_irq:
2867 disable_irq(data->irq);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07002868release_firmware:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002869 release_firmware(fw);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002870 return ret;
2871}
2872
Iiro Valkonen7686b102011-02-02 23:21:58 -08002873static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002874 struct device_attribute *attr,
2875 const char *buf, size_t count)
2876{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002877 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002878 int error;
2879
Iiro Valkonen7686b102011-02-02 23:21:58 -08002880 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002881 if (error) {
2882 dev_err(dev, "The firmware update failed(%d)\n", error);
2883 count = error;
2884 } else {
Nick Dyer7bed6802014-05-18 23:04:09 -07002885 dev_info(dev, "The firmware update succeeded\n");
2886
Nick Dyerdd24dcf2014-07-23 11:25:55 -07002887 error = mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07002888 if (error)
2889 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002890 }
2891
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002892 return count;
2893}
2894
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002895static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
2896static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07002897static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
2898static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002899
Iiro Valkonen7686b102011-02-02 23:21:58 -08002900static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08002901 &dev_attr_fw_version.attr,
2902 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002903 &dev_attr_object.attr,
2904 &dev_attr_update_fw.attr,
2905 NULL
2906};
2907
Iiro Valkonen7686b102011-02-02 23:21:58 -08002908static const struct attribute_group mxt_attr_group = {
2909 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002910};
2911
Iiro Valkonen7686b102011-02-02 23:21:58 -08002912static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002913{
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07002914 switch (data->suspend_mode) {
Nick Dyer7f3884f72015-08-04 16:36:29 -07002915 case MXT_SUSPEND_T9_CTRL:
2916 mxt_soft_reset(data);
2917
2918 /* Touch enable */
2919 /* 0x83 = SCANEN | RPTEN | ENABLE */
2920 mxt_write_object(data,
2921 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
2922 break;
2923
2924 case MXT_SUSPEND_DEEP_SLEEP:
2925 default:
2926 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2927
2928 /* Recalibrate since chip has been in deep sleep */
2929 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
2930 break;
2931 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002932}
2933
Iiro Valkonen7686b102011-02-02 23:21:58 -08002934static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002935{
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07002936 switch (data->suspend_mode) {
Nick Dyer7f3884f72015-08-04 16:36:29 -07002937 case MXT_SUSPEND_T9_CTRL:
2938 /* Touch disable */
2939 mxt_write_object(data,
2940 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
2941 break;
2942
2943 case MXT_SUSPEND_DEEP_SLEEP:
2944 default:
2945 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
2946 break;
2947 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002948}
2949
Iiro Valkonen7686b102011-02-02 23:21:58 -08002950static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002951{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002952 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002953
Iiro Valkonen7686b102011-02-02 23:21:58 -08002954 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002955
2956 return 0;
2957}
2958
Iiro Valkonen7686b102011-02-02 23:21:58 -08002959static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002960{
Iiro Valkonen7686b102011-02-02 23:21:58 -08002961 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002962
Iiro Valkonen7686b102011-02-02 23:21:58 -08002963 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07002964}
2965
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002966static int mxt_parse_device_properties(struct mxt_data *data)
Stephen Warren78188be2014-07-23 12:23:23 -07002967{
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002968 static const char keymap_property[] = "linux,gpio-keymap";
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002969 struct device *dev = &data->client->dev;
Stephen Warren78188be2014-07-23 12:23:23 -07002970 u32 *keymap;
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002971 int n_keys;
2972 int error;
Stephen Warren78188be2014-07-23 12:23:23 -07002973
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002974 if (device_property_present(dev, keymap_property)) {
2975 n_keys = device_property_read_u32_array(dev, keymap_property,
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002976 NULL, 0);
2977 if (n_keys <= 0) {
2978 error = n_keys < 0 ? n_keys : -EINVAL;
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002979 dev_err(dev, "invalid/malformed '%s' property: %d\n",
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002980 keymap_property, error);
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002981 return error;
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002982 }
Stephen Warren78188be2014-07-23 12:23:23 -07002983
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002984 keymap = devm_kmalloc_array(dev, n_keys, sizeof(*keymap),
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002985 GFP_KERNEL);
Stephen Warren78188be2014-07-23 12:23:23 -07002986 if (!keymap)
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002987 return -ENOMEM;
Stephen Warren78188be2014-07-23 12:23:23 -07002988
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002989 error = device_property_read_u32_array(dev, keymap_property,
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002990 keymap, n_keys);
2991 if (error) {
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002992 dev_err(dev, "failed to parse '%s' property: %d\n",
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002993 keymap_property, error);
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002994 return error;
Dmitry Torokhov93afb1d2018-03-20 15:31:26 -07002995 }
Stephen Warren78188be2014-07-23 12:23:23 -07002996
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07002997 data->t19_keymap = keymap;
2998 data->t19_num_keys = n_keys;
Stephen Warren78188be2014-07-23 12:23:23 -07002999 }
3000
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003001 return 0;
Stephen Warren78188be2014-07-23 12:23:23 -07003002}
Stephen Warren78188be2014-07-23 12:23:23 -07003003
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07003004static const struct dmi_system_id chromebook_T9_suspend_dmi[] = {
3005 {
3006 .matches = {
3007 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
3008 DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
3009 },
3010 },
3011 {
3012 .matches = {
3013 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
3014 },
3015 },
3016 { }
3017};
3018
Stephen Warren78188be2014-07-23 12:23:23 -07003019static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003020{
Iiro Valkonen7686b102011-02-02 23:21:58 -08003021 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003022 int error;
3023
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003024 /*
Dmitry Torokhov7cf432b2018-05-03 17:14:51 -07003025 * Ignore devices that do not have device properties attached to
3026 * them, as we need help determining whether we are dealing with
3027 * touch screen or touchpad.
3028 *
3029 * So far on x86 the only users of Atmel touch controllers are
3030 * Chromebooks, and chromeos_laptop driver will ensure that
3031 * necessary properties are provided (if firmware does not do that).
3032 */
3033 if (!device_property_present(&client->dev, "compatible"))
3034 return -ENXIO;
3035
3036 /*
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003037 * Ignore ACPI devices representing bootloader mode.
3038 *
3039 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3040 * devices for both application and bootloader modes, but we are
3041 * interested in application mode only (if device is in bootloader
3042 * mode we'll end up switching into application anyway). So far
3043 * application mode addresses were all above 0x40, so we'll use it
3044 * as a threshold.
3045 */
3046 if (ACPI_COMPANION(&client->dev) && client->addr < 0x40)
3047 return -ENXIO;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003048
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003049 data = devm_kzalloc(&client->dev, sizeof(struct mxt_data), GFP_KERNEL);
3050 if (!data)
Nick Dyer7a53d602014-07-23 12:21:26 -07003051 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003052
Daniel Kurtzec02ac22012-06-28 21:08:02 +08003053 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
3054 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08003055
Joonyoung Shim910d8052011-04-12 23:14:38 -07003056 data->client = client;
Joonyoung Shim910d8052011-04-12 23:14:38 -07003057 data->irq = client->irq;
Nick Dyer7a53d602014-07-23 12:21:26 -07003058 i2c_set_clientdata(client, data);
Joonyoung Shim910d8052011-04-12 23:14:38 -07003059
Benson Leungd79e7e42014-05-18 23:02:52 -07003060 init_completion(&data->bl_completion);
Iiro Valkonena4a2ef42014-05-18 23:03:44 -07003061 init_completion(&data->reset_completion);
Nick Dyerc3f78042014-05-18 23:04:46 -07003062 init_completion(&data->crc_completion);
Benson Leungd79e7e42014-05-18 23:02:52 -07003063
Dmitry Torokhovd80808e2018-03-20 15:31:25 -07003064 data->suspend_mode = dmi_check_system(chromebook_T9_suspend_dmi) ?
3065 MXT_SUSPEND_T9_CTRL : MXT_SUSPEND_DEEP_SLEEP;
3066
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003067 error = mxt_parse_device_properties(data);
3068 if (error)
3069 return error;
3070
Sebastian Reichelf657b002017-07-25 14:11:34 -07003071 data->reset_gpio = devm_gpiod_get_optional(&client->dev,
3072 "reset", GPIOD_OUT_LOW);
3073 if (IS_ERR(data->reset_gpio)) {
3074 error = PTR_ERR(data->reset_gpio);
3075 dev_err(&client->dev, "Failed to get reset gpio: %d\n", error);
3076 return error;
3077 }
3078
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003079 error = devm_request_threaded_irq(&client->dev, client->irq,
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003080 NULL, mxt_interrupt, IRQF_ONESHOT,
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003081 client->name, data);
Nick Dyerdd24dcf2014-07-23 11:25:55 -07003082 if (error) {
3083 dev_err(&client->dev, "Failed to register interrupt\n");
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003084 return error;
Nick Dyerdd24dcf2014-07-23 11:25:55 -07003085 }
3086
3087 disable_irq(client->irq);
3088
Sebastian Reichelca1cd362018-05-24 09:33:36 -07003089 if (data->reset_gpio) {
3090 msleep(MXT_RESET_GPIO_TIME);
3091 gpiod_set_value(data->reset_gpio, 1);
3092 msleep(MXT_RESET_INVALID_CHG);
3093 }
3094
Daniel Kurtzcb159112012-06-28 21:08:22 +08003095 error = mxt_initialize(data);
3096 if (error)
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003097 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003098
Iiro Valkonen7686b102011-02-02 23:21:58 -08003099 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Nick Dyer7bed6802014-05-18 23:04:09 -07003100 if (error) {
3101 dev_err(&client->dev, "Failure %d creating sysfs group\n",
3102 error);
Nick Dyer7a53d602014-07-23 12:21:26 -07003103 goto err_free_object;
Nick Dyer7bed6802014-05-18 23:04:09 -07003104 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003105
3106 return 0;
3107
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003108err_free_object:
Stephen Warren58e4aeee2014-09-10 10:01:10 -07003109 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07003110 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003111 return error;
3112}
3113
Bill Pembertone2619cf2012-11-23 21:50:47 -08003114static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003115{
Iiro Valkonen7686b102011-02-02 23:21:58 -08003116 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003117
Sebastian Reichel8cc84462017-07-25 14:08:48 -07003118 disable_irq(data->irq);
Iiro Valkonen7686b102011-02-02 23:21:58 -08003119 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Stephen Warren58e4aeee2014-09-10 10:01:10 -07003120 mxt_free_input_device(data);
Nick Dyer5f3f9bc2014-07-23 12:46:55 -07003121 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003122
3123 return 0;
3124}
3125
Jingoo Han02b6a582014-11-02 00:04:14 -07003126static int __maybe_unused mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003127{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08003128 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08003129 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003130 struct input_dev *input_dev = data->input_dev;
3131
Pan Xinhui50fabb02015-08-04 16:53:04 -07003132 if (!input_dev)
3133 return 0;
3134
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003135 mutex_lock(&input_dev->mutex);
3136
3137 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08003138 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003139
3140 mutex_unlock(&input_dev->mutex);
3141
3142 return 0;
3143}
3144
Jingoo Han02b6a582014-11-02 00:04:14 -07003145static int __maybe_unused mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003146{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08003147 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08003148 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003149 struct input_dev *input_dev = data->input_dev;
3150
Pan Xinhui50fabb02015-08-04 16:53:04 -07003151 if (!input_dev)
3152 return 0;
3153
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003154 mutex_lock(&input_dev->mutex);
3155
3156 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08003157 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003158
3159 mutex_unlock(&input_dev->mutex);
3160
3161 return 0;
3162}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003163
Daniel Kurtz3a73c812012-05-08 22:29:14 -07003164static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
3165
Stephen Warren78188be2014-07-23 12:23:23 -07003166static const struct of_device_id mxt_of_match[] = {
3167 { .compatible = "atmel,maxtouch", },
Javier Martinez Canillasf6eeb9e2018-05-01 11:44:53 -07003168 /* Compatibles listed below are deprecated */
3169 { .compatible = "atmel,qt602240_ts", },
3170 { .compatible = "atmel,atmel_mxt_ts", },
3171 { .compatible = "atmel,atmel_mxt_tp", },
3172 { .compatible = "atmel,mXT224", },
Stephen Warren78188be2014-07-23 12:23:23 -07003173 {},
3174};
3175MODULE_DEVICE_TABLE(of, mxt_of_match);
3176
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07003177#ifdef CONFIG_ACPI
3178static const struct acpi_device_id mxt_acpi_id[] = {
3179 { "ATML0000", 0 }, /* Touchpad */
3180 { "ATML0001", 0 }, /* Touchscreen */
3181 { }
3182};
3183MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
3184#endif
3185
Iiro Valkonen7686b102011-02-02 23:21:58 -08003186static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003187 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08003188 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08003189 { "atmel_mxt_tp", 0 },
Javier Martinez Canillasb7d21052015-12-11 13:22:39 -08003190 { "maxtouch", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08003191 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003192 { }
3193};
Iiro Valkonen7686b102011-02-02 23:21:58 -08003194MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003195
Iiro Valkonen7686b102011-02-02 23:21:58 -08003196static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003197 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08003198 .name = "atmel_mxt_ts",
Dmitry Torokhov96a938aa2018-03-20 15:31:38 -07003199 .of_match_table = mxt_of_match,
Dmitry Torokhov4f8d8082015-04-07 16:30:01 -07003200 .acpi_match_table = ACPI_PTR(mxt_acpi_id),
Iiro Valkonen7686b102011-02-02 23:21:58 -08003201 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003202 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08003203 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08003204 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08003205 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003206};
3207
Axel Lin1b92c1c2012-03-16 23:05:41 -07003208module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003209
3210/* Module information */
3211MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08003212MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003213MODULE_LICENSE("GPL");