Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1 | /* |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2 | * Atmel maXTouch Touchscreen driver |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics Co.Ltd |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 5 | * Copyright (C) 2011-2014 Atmel Corporation |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 6 | * Copyright (C) 2012 Google, Inc. |
| 7 | * |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 8 | * Author: Joonyoung Shim <jy0922.shim@samsung.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/completion.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> |
| 21 | #include <linux/firmware.h> |
| 22 | #include <linux/i2c.h> |
Dmitry Torokhov | 964de52 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 23 | #include <linux/i2c/atmel_mxt_ts.h> |
Joonyoung Shim | 8b86c1c | 2011-04-12 23:18:59 -0700 | [diff] [blame] | 24 | #include <linux/input/mt.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 26 | #include <linux/of.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 27 | #include <linux/slab.h> |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 29 | |
| 30 | /* Version */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 31 | #define MXT_VER_20 20 |
| 32 | #define MXT_VER_21 21 |
| 33 | #define MXT_VER_22 22 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 34 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 35 | /* Firmware files */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 36 | #define MXT_FW_NAME "maxtouch.fw" |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 37 | #define MXT_CFG_NAME "maxtouch.cfg" |
| 38 | #define MXT_CFG_MAGIC "OBP_RAW V1" |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 39 | |
| 40 | /* Registers */ |
Daniel Kurtz | 23003a84 | 2012-06-28 21:08:14 +0800 | [diff] [blame] | 41 | #define MXT_INFO 0x00 |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 42 | #define MXT_FAMILY_ID 0x00 |
| 43 | #define MXT_VARIANT_ID 0x01 |
| 44 | #define MXT_VERSION 0x02 |
| 45 | #define MXT_BUILD 0x03 |
| 46 | #define MXT_MATRIX_X_SIZE 0x04 |
| 47 | #define MXT_MATRIX_Y_SIZE 0x05 |
| 48 | #define MXT_OBJECT_NUM 0x06 |
| 49 | #define MXT_OBJECT_START 0x07 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 50 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 51 | #define MXT_OBJECT_SIZE 6 |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 52 | #define MXT_INFO_CHECKSUM_SIZE 3 |
| 53 | #define MXT_MAX_BLOCK_WRITE 256 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 54 | |
| 55 | /* Object types */ |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 56 | #define MXT_DEBUG_DIAGNOSTIC_T37 37 |
| 57 | #define MXT_GEN_MESSAGE_T5 5 |
| 58 | #define MXT_GEN_COMMAND_T6 6 |
| 59 | #define MXT_GEN_POWER_T7 7 |
| 60 | #define MXT_GEN_ACQUIRE_T8 8 |
| 61 | #define MXT_GEN_DATASOURCE_T53 53 |
| 62 | #define MXT_TOUCH_MULTI_T9 9 |
| 63 | #define MXT_TOUCH_KEYARRAY_T15 15 |
| 64 | #define MXT_TOUCH_PROXIMITY_T23 23 |
| 65 | #define MXT_TOUCH_PROXKEY_T52 52 |
| 66 | #define MXT_PROCI_GRIPFACE_T20 20 |
| 67 | #define MXT_PROCG_NOISE_T22 22 |
| 68 | #define MXT_PROCI_ONETOUCH_T24 24 |
| 69 | #define MXT_PROCI_TWOTOUCH_T27 27 |
| 70 | #define MXT_PROCI_GRIP_T40 40 |
| 71 | #define MXT_PROCI_PALM_T41 41 |
| 72 | #define MXT_PROCI_TOUCHSUPPRESSION_T42 42 |
| 73 | #define MXT_PROCI_STYLUS_T47 47 |
| 74 | #define MXT_PROCG_NOISESUPPRESSION_T48 48 |
| 75 | #define MXT_SPT_COMMSCONFIG_T18 18 |
| 76 | #define MXT_SPT_GPIOPWM_T19 19 |
| 77 | #define MXT_SPT_SELFTEST_T25 25 |
| 78 | #define MXT_SPT_CTECONFIG_T28 28 |
| 79 | #define MXT_SPT_USERDATA_T38 38 |
| 80 | #define MXT_SPT_DIGITIZER_T43 43 |
| 81 | #define MXT_SPT_MESSAGECOUNT_T44 44 |
| 82 | #define MXT_SPT_CTECONFIG_T46 46 |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 83 | #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 84 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 85 | /* MXT_GEN_MESSAGE_T5 object */ |
| 86 | #define MXT_RPTID_NOMSG 0xff |
| 87 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 88 | /* MXT_GEN_COMMAND_T6 field */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 89 | #define MXT_COMMAND_RESET 0 |
| 90 | #define MXT_COMMAND_BACKUPNV 1 |
| 91 | #define MXT_COMMAND_CALIBRATE 2 |
| 92 | #define MXT_COMMAND_REPORTALL 3 |
| 93 | #define MXT_COMMAND_DIAGNOSTIC 5 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 94 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 95 | /* Define for T6 status byte */ |
| 96 | #define MXT_T6_STATUS_RESET (1 << 7) |
Nick Dyer | 497767d | 2014-07-23 12:47:50 -0700 | [diff] [blame] | 97 | #define MXT_T6_STATUS_OFL (1 << 6) |
| 98 | #define MXT_T6_STATUS_SIGERR (1 << 5) |
| 99 | #define MXT_T6_STATUS_CAL (1 << 4) |
| 100 | #define MXT_T6_STATUS_CFGERR (1 << 3) |
| 101 | #define MXT_T6_STATUS_COMSERR (1 << 2) |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 102 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 103 | /* MXT_GEN_POWER_T7 field */ |
Linus Torvalds | 7f40548 | 2014-12-31 12:59:34 -0800 | [diff] [blame] | 104 | #define MXT_POWER_IDLEACQINT 0 |
| 105 | #define MXT_POWER_ACTVACQINT 1 |
| 106 | #define MXT_POWER_ACTV2IDLETO 2 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 107 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 108 | /* MXT_GEN_ACQUIRE_T8 field */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 109 | #define MXT_ACQUIRE_CHRGTIME 0 |
| 110 | #define MXT_ACQUIRE_TCHDRIFT 2 |
| 111 | #define MXT_ACQUIRE_DRIFTST 3 |
| 112 | #define MXT_ACQUIRE_TCHAUTOCAL 4 |
| 113 | #define MXT_ACQUIRE_SYNC 5 |
| 114 | #define MXT_ACQUIRE_ATCHCALST 6 |
| 115 | #define MXT_ACQUIRE_ATCHCALSTHR 7 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 116 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 117 | /* MXT_TOUCH_MULTI_T9 field */ |
Linus Torvalds | 7f40548 | 2014-12-31 12:59:34 -0800 | [diff] [blame] | 118 | #define MXT_TOUCH_CTRL 0 |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 119 | #define MXT_T9_ORIENT 9 |
| 120 | #define MXT_T9_RANGE 18 |
| 121 | |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 122 | /* MXT_TOUCH_MULTI_T9 status */ |
| 123 | #define MXT_T9_UNGRIP (1 << 0) |
| 124 | #define MXT_T9_SUPPRESS (1 << 1) |
| 125 | #define MXT_T9_AMP (1 << 2) |
| 126 | #define MXT_T9_VECTOR (1 << 3) |
| 127 | #define MXT_T9_MOVE (1 << 4) |
| 128 | #define MXT_T9_RELEASE (1 << 5) |
| 129 | #define MXT_T9_PRESS (1 << 6) |
| 130 | #define MXT_T9_DETECT (1 << 7) |
| 131 | |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 132 | struct t9_range { |
| 133 | u16 x; |
| 134 | u16 y; |
| 135 | } __packed; |
| 136 | |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 137 | /* MXT_TOUCH_MULTI_T9 orient */ |
| 138 | #define MXT_T9_ORIENT_SWITCH (1 << 0) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 139 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 140 | /* MXT_PROCI_GRIPFACE_T20 field */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 141 | #define MXT_GRIPFACE_CTRL 0 |
| 142 | #define MXT_GRIPFACE_XLOGRIP 1 |
| 143 | #define MXT_GRIPFACE_XHIGRIP 2 |
| 144 | #define MXT_GRIPFACE_YLOGRIP 3 |
| 145 | #define MXT_GRIPFACE_YHIGRIP 4 |
| 146 | #define MXT_GRIPFACE_MAXTCHS 5 |
| 147 | #define MXT_GRIPFACE_SZTHR1 7 |
| 148 | #define MXT_GRIPFACE_SZTHR2 8 |
| 149 | #define MXT_GRIPFACE_SHPTHR1 9 |
| 150 | #define MXT_GRIPFACE_SHPTHR2 10 |
| 151 | #define MXT_GRIPFACE_SUPEXTTO 11 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 152 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 153 | /* MXT_PROCI_NOISE field */ |
| 154 | #define MXT_NOISE_CTRL 0 |
| 155 | #define MXT_NOISE_OUTFLEN 1 |
| 156 | #define MXT_NOISE_GCAFUL_LSB 3 |
| 157 | #define MXT_NOISE_GCAFUL_MSB 4 |
| 158 | #define MXT_NOISE_GCAFLL_LSB 5 |
| 159 | #define MXT_NOISE_GCAFLL_MSB 6 |
| 160 | #define MXT_NOISE_ACTVGCAFVALID 7 |
| 161 | #define MXT_NOISE_NOISETHR 8 |
| 162 | #define MXT_NOISE_FREQHOPSCALE 10 |
| 163 | #define MXT_NOISE_FREQ0 11 |
| 164 | #define MXT_NOISE_FREQ1 12 |
| 165 | #define MXT_NOISE_FREQ2 13 |
| 166 | #define MXT_NOISE_FREQ3 14 |
| 167 | #define MXT_NOISE_FREQ4 15 |
| 168 | #define MXT_NOISE_IDLEGCAFVALID 16 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 169 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 170 | /* MXT_SPT_COMMSCONFIG_T18 */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 171 | #define MXT_COMMS_CTRL 0 |
| 172 | #define MXT_COMMS_CMD 1 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 173 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 174 | /* MXT_SPT_CTECONFIG_T28 field */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 175 | #define MXT_CTE_CTRL 0 |
| 176 | #define MXT_CTE_CMD 1 |
| 177 | #define MXT_CTE_MODE 2 |
| 178 | #define MXT_CTE_IDLEGCAFDEPTH 3 |
| 179 | #define MXT_CTE_ACTVGCAFDEPTH 4 |
Joonyoung Shim | 979a72d | 2011-03-14 21:41:34 -0700 | [diff] [blame] | 180 | #define MXT_CTE_VOLTAGE 5 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 181 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 182 | #define MXT_VOLTAGE_DEFAULT 2700000 |
| 183 | #define MXT_VOLTAGE_STEP 10000 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 184 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 185 | /* Define for MXT_GEN_COMMAND_T6 */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 186 | #define MXT_BOOT_VALUE 0xa5 |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 187 | #define MXT_RESET_VALUE 0x01 |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 188 | #define MXT_BACKUP_VALUE 0x55 |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 189 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 190 | /* T100 Multiple Touch Touchscreen */ |
| 191 | #define MXT_T100_CTRL 0 |
| 192 | #define MXT_T100_CFG1 1 |
| 193 | #define MXT_T100_TCHAUX 3 |
| 194 | #define MXT_T100_XRANGE 13 |
| 195 | #define MXT_T100_YRANGE 24 |
| 196 | |
| 197 | #define MXT_T100_CFG_SWITCHXY BIT(5) |
| 198 | |
| 199 | #define MXT_T100_TCHAUX_VECT BIT(0) |
| 200 | #define MXT_T100_TCHAUX_AMPL BIT(1) |
| 201 | #define MXT_T100_TCHAUX_AREA BIT(2) |
| 202 | |
| 203 | #define MXT_T100_DETECT BIT(7) |
| 204 | #define MXT_T100_TYPE_MASK 0x70 |
| 205 | |
| 206 | enum t100_type { |
| 207 | MXT_T100_TYPE_FINGER = 1, |
| 208 | MXT_T100_TYPE_PASSIVE_STYLUS = 2, |
| 209 | MXT_T100_TYPE_HOVERING_FINGER = 4, |
| 210 | MXT_T100_TYPE_GLOVE = 5, |
| 211 | MXT_T100_TYPE_LARGE_TOUCH = 6, |
| 212 | }; |
| 213 | |
| 214 | #define MXT_DISTANCE_ACTIVE_TOUCH 0 |
| 215 | #define MXT_DISTANCE_HOVERING 1 |
| 216 | |
| 217 | #define MXT_TOUCH_MAJOR_DEFAULT 1 |
| 218 | #define MXT_PRESSURE_DEFAULT 1 |
| 219 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 220 | /* Delay times */ |
Linus Torvalds | 8343bce | 2013-03-09 10:31:01 -0800 | [diff] [blame] | 221 | #define MXT_BACKUP_TIME 50 /* msec */ |
| 222 | #define MXT_RESET_TIME 200 /* msec */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 223 | #define MXT_RESET_TIMEOUT 3000 /* msec */ |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 224 | #define MXT_CRC_TIMEOUT 1000 /* msec */ |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 225 | #define MXT_FW_RESET_TIME 3000 /* msec */ |
| 226 | #define MXT_FW_CHG_TIMEOUT 300 /* msec */ |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 227 | |
| 228 | /* Command to unlock bootloader */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 229 | #define MXT_UNLOCK_CMD_MSB 0xaa |
| 230 | #define MXT_UNLOCK_CMD_LSB 0xdc |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 231 | |
| 232 | /* Bootloader mode status */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 233 | #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */ |
| 234 | #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */ |
| 235 | #define MXT_FRAME_CRC_CHECK 0x02 |
| 236 | #define MXT_FRAME_CRC_FAIL 0x03 |
| 237 | #define MXT_FRAME_CRC_PASS 0x04 |
| 238 | #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */ |
| 239 | #define MXT_BOOT_STATUS_MASK 0x3f |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 240 | #define MXT_BOOT_EXTENDED_ID (1 << 5) |
| 241 | #define MXT_BOOT_ID_MASK 0x1f |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 242 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 243 | /* Touchscreen absolute values */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 244 | #define MXT_MAX_AREA 0xff |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 245 | |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 246 | #define MXT_PIXELS_PER_MM 20 |
| 247 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 248 | struct mxt_info { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 249 | u8 family_id; |
| 250 | u8 variant_id; |
| 251 | u8 version; |
| 252 | u8 build; |
| 253 | u8 matrix_xsize; |
| 254 | u8 matrix_ysize; |
| 255 | u8 object_num; |
| 256 | }; |
| 257 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 258 | struct mxt_object { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 259 | u8 type; |
| 260 | u16 start_address; |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 261 | u8 size_minus_one; |
| 262 | u8 instances_minus_one; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 263 | u8 num_report_ids; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 264 | } __packed; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 265 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 266 | /* Each client has this additional data */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 267 | struct mxt_data { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 268 | struct i2c_client *client; |
| 269 | struct input_dev *input_dev; |
Daniel Kurtz | ec02ac2 | 2012-06-28 21:08:02 +0800 | [diff] [blame] | 270 | char phys[64]; /* device physical location */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 271 | const struct mxt_platform_data *pdata; |
| 272 | struct mxt_object *object_table; |
| 273 | struct mxt_info info; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 274 | unsigned int irq; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 275 | unsigned int max_x; |
| 276 | unsigned int max_y; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 277 | bool in_bootloader; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 278 | u16 mem_size; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 279 | u8 t100_aux_ampl; |
| 280 | u8 t100_aux_area; |
| 281 | u8 t100_aux_vect; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 282 | u8 max_reportid; |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 283 | u32 config_crc; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 284 | u32 info_crc; |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 285 | u8 bootloader_addr; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 286 | u8 *msg_buf; |
Nick Dyer | 497767d | 2014-07-23 12:47:50 -0700 | [diff] [blame] | 287 | u8 t6_status; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 288 | bool update_input; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 289 | u8 last_message_count; |
| 290 | u8 num_touchids; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 291 | u8 multitouch; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 292 | |
| 293 | /* Cached parameters from object table */ |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 294 | u16 T5_address; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 295 | u8 T5_msg_size; |
Daniel Kurtz | fdf80421 | 2012-06-28 21:08:24 +0800 | [diff] [blame] | 296 | u8 T6_reportid; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 297 | u16 T6_address; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 298 | u16 T7_address; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 299 | u8 T9_reportid_min; |
| 300 | u8 T9_reportid_max; |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 301 | u8 T19_reportid; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 302 | u16 T44_address; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 303 | u8 T100_reportid_min; |
| 304 | u8 T100_reportid_max; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 305 | |
| 306 | /* for fw update in bootloader */ |
| 307 | struct completion bl_completion; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 308 | |
| 309 | /* for reset handling */ |
| 310 | struct completion reset_completion; |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 311 | |
| 312 | /* for config update handling */ |
| 313 | struct completion crc_completion; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 316 | static size_t mxt_obj_size(const struct mxt_object *obj) |
| 317 | { |
| 318 | return obj->size_minus_one + 1; |
| 319 | } |
| 320 | |
| 321 | static size_t mxt_obj_instances(const struct mxt_object *obj) |
| 322 | { |
| 323 | return obj->instances_minus_one + 1; |
| 324 | } |
| 325 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 326 | static bool mxt_object_readable(unsigned int type) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 327 | { |
| 328 | switch (type) { |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 329 | case MXT_GEN_COMMAND_T6: |
| 330 | case MXT_GEN_POWER_T7: |
| 331 | case MXT_GEN_ACQUIRE_T8: |
| 332 | case MXT_GEN_DATASOURCE_T53: |
| 333 | case MXT_TOUCH_MULTI_T9: |
| 334 | case MXT_TOUCH_KEYARRAY_T15: |
| 335 | case MXT_TOUCH_PROXIMITY_T23: |
| 336 | case MXT_TOUCH_PROXKEY_T52: |
| 337 | case MXT_PROCI_GRIPFACE_T20: |
| 338 | case MXT_PROCG_NOISE_T22: |
| 339 | case MXT_PROCI_ONETOUCH_T24: |
| 340 | case MXT_PROCI_TWOTOUCH_T27: |
| 341 | case MXT_PROCI_GRIP_T40: |
| 342 | case MXT_PROCI_PALM_T41: |
| 343 | case MXT_PROCI_TOUCHSUPPRESSION_T42: |
| 344 | case MXT_PROCI_STYLUS_T47: |
| 345 | case MXT_PROCG_NOISESUPPRESSION_T48: |
| 346 | case MXT_SPT_COMMSCONFIG_T18: |
| 347 | case MXT_SPT_GPIOPWM_T19: |
| 348 | case MXT_SPT_SELFTEST_T25: |
| 349 | case MXT_SPT_CTECONFIG_T28: |
| 350 | case MXT_SPT_USERDATA_T38: |
| 351 | case MXT_SPT_DIGITIZER_T43: |
| 352 | case MXT_SPT_CTECONFIG_T46: |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 353 | return true; |
| 354 | default: |
| 355 | return false; |
| 356 | } |
| 357 | } |
| 358 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 359 | static void mxt_dump_message(struct mxt_data *data, u8 *message) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 360 | { |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 361 | dev_dbg(&data->client->dev, "message: %*ph\n", |
| 362 | data->T5_msg_size, message); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 365 | static int mxt_wait_for_completion(struct mxt_data *data, |
| 366 | struct completion *comp, |
| 367 | unsigned int timeout_ms) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 368 | { |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 369 | struct device *dev = &data->client->dev; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 370 | unsigned long timeout = msecs_to_jiffies(timeout_ms); |
| 371 | long ret; |
| 372 | |
| 373 | ret = wait_for_completion_interruptible_timeout(comp, timeout); |
| 374 | if (ret < 0) { |
| 375 | return ret; |
| 376 | } else if (ret == 0) { |
| 377 | dev_err(dev, "Wait for completion timed out.\n"); |
| 378 | return -ETIMEDOUT; |
| 379 | } |
| 380 | return 0; |
| 381 | } |
| 382 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 383 | static int mxt_bootloader_read(struct mxt_data *data, |
| 384 | u8 *val, unsigned int count) |
| 385 | { |
| 386 | int ret; |
| 387 | struct i2c_msg msg; |
| 388 | |
| 389 | msg.addr = data->bootloader_addr; |
| 390 | msg.flags = data->client->flags & I2C_M_TEN; |
| 391 | msg.flags |= I2C_M_RD; |
| 392 | msg.len = count; |
| 393 | msg.buf = val; |
| 394 | |
| 395 | ret = i2c_transfer(data->client->adapter, &msg, 1); |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 396 | if (ret == 1) { |
| 397 | ret = 0; |
| 398 | } else { |
| 399 | ret = ret < 0 ? ret : -EIO; |
| 400 | dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n", |
| 401 | __func__, ret); |
| 402 | } |
| 403 | |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | static int mxt_bootloader_write(struct mxt_data *data, |
| 408 | const u8 * const val, unsigned int count) |
| 409 | { |
| 410 | int ret; |
| 411 | struct i2c_msg msg; |
| 412 | |
| 413 | msg.addr = data->bootloader_addr; |
| 414 | msg.flags = data->client->flags & I2C_M_TEN; |
| 415 | msg.len = count; |
| 416 | msg.buf = (u8 *)val; |
| 417 | |
| 418 | ret = i2c_transfer(data->client->adapter, &msg, 1); |
| 419 | if (ret == 1) { |
| 420 | ret = 0; |
| 421 | } else { |
| 422 | ret = ret < 0 ? ret : -EIO; |
| 423 | dev_err(&data->client->dev, "%s: i2c send failed (%d)\n", |
| 424 | __func__, ret); |
| 425 | } |
| 426 | |
| 427 | return ret; |
| 428 | } |
| 429 | |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 430 | static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry) |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 431 | { |
| 432 | u8 appmode = data->client->addr; |
| 433 | u8 bootloader; |
| 434 | |
| 435 | switch (appmode) { |
| 436 | case 0x4a: |
| 437 | case 0x4b: |
Nick Dyer | 44a0bab | 2014-07-23 12:45:26 -0700 | [diff] [blame] | 438 | /* Chips after 1664S use different scheme */ |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 439 | if (retry || data->info.family_id >= 0xa2) { |
Nick Dyer | 44a0bab | 2014-07-23 12:45:26 -0700 | [diff] [blame] | 440 | bootloader = appmode - 0x24; |
| 441 | break; |
| 442 | } |
| 443 | /* Fall through for normal case */ |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 444 | case 0x4c: |
| 445 | case 0x4d: |
| 446 | case 0x5a: |
| 447 | case 0x5b: |
| 448 | bootloader = appmode - 0x26; |
| 449 | break; |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 450 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 451 | default: |
| 452 | dev_err(&data->client->dev, |
| 453 | "Appmode i2c address 0x%02x not found\n", |
| 454 | appmode); |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | data->bootloader_addr = bootloader; |
| 459 | return 0; |
| 460 | } |
| 461 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 462 | static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address) |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 463 | { |
| 464 | struct device *dev = &data->client->dev; |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 465 | int error; |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 466 | u8 val; |
| 467 | bool crc_failure; |
| 468 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 469 | error = mxt_lookup_bootloader_address(data, alt_address); |
| 470 | if (error) |
| 471 | return error; |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 472 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 473 | error = mxt_bootloader_read(data, &val, 1); |
| 474 | if (error) |
| 475 | return error; |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 476 | |
| 477 | /* Check app crc fail mode */ |
| 478 | crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL; |
| 479 | |
| 480 | dev_err(dev, "Detected bootloader, status:%02X%s\n", |
| 481 | val, crc_failure ? ", APP_CRC_FAIL" : ""); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 486 | static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val) |
| 487 | { |
| 488 | struct device *dev = &data->client->dev; |
| 489 | u8 buf[3]; |
| 490 | |
| 491 | if (val & MXT_BOOT_EXTENDED_ID) { |
| 492 | if (mxt_bootloader_read(data, &buf[0], 3) != 0) { |
| 493 | dev_err(dev, "%s: i2c failure\n", __func__); |
Nick Dyer | 68807a0 | 2014-06-07 23:17:26 -0700 | [diff] [blame] | 494 | return val; |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]); |
| 498 | |
| 499 | return buf[0]; |
| 500 | } else { |
| 501 | dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK); |
| 502 | |
| 503 | return val; |
| 504 | } |
| 505 | } |
| 506 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 507 | static int mxt_check_bootloader(struct mxt_data *data, unsigned int state, |
| 508 | bool wait) |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 509 | { |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 510 | struct device *dev = &data->client->dev; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 511 | u8 val; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 512 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 513 | |
| 514 | recheck: |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 515 | if (wait) { |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 516 | /* |
| 517 | * In application update mode, the interrupt |
| 518 | * line signals state transitions. We must wait for the |
| 519 | * CHG assertion before reading the status byte. |
| 520 | * Once the status byte has been read, the line is deasserted. |
| 521 | */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 522 | ret = mxt_wait_for_completion(data, &data->bl_completion, |
| 523 | MXT_FW_CHG_TIMEOUT); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 524 | if (ret) { |
| 525 | /* |
| 526 | * TODO: handle -ERESTARTSYS better by terminating |
| 527 | * fw update process before returning to userspace |
| 528 | * by writing length 0x000 to device (iff we are in |
| 529 | * WAITING_FRAME_DATA state). |
| 530 | */ |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 531 | dev_err(dev, "Update wait error %d\n", ret); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 532 | return ret; |
| 533 | } |
| 534 | } |
| 535 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 536 | ret = mxt_bootloader_read(data, &val, 1); |
| 537 | if (ret) |
| 538 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 539 | |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 540 | if (state == MXT_WAITING_BOOTLOAD_CMD) |
| 541 | val = mxt_get_bootloader_version(data, val); |
| 542 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 543 | switch (state) { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 544 | case MXT_WAITING_BOOTLOAD_CMD: |
| 545 | case MXT_WAITING_FRAME_DATA: |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 546 | case MXT_APP_CRC_FAIL: |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 547 | val &= ~MXT_BOOT_STATUS_MASK; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 548 | break; |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 549 | case MXT_FRAME_CRC_PASS: |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 550 | if (val == MXT_FRAME_CRC_CHECK) { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 551 | goto recheck; |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 552 | } else if (val == MXT_FRAME_CRC_FAIL) { |
| 553 | dev_err(dev, "Bootloader CRC fail\n"); |
| 554 | return -EINVAL; |
| 555 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 556 | break; |
| 557 | default: |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | |
| 561 | if (val != state) { |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 562 | dev_err(dev, "Invalid bootloader state %02X != %02X\n", |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 563 | val, state); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 564 | return -EINVAL; |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 570 | static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 571 | { |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 572 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 573 | u8 buf[2]; |
| 574 | |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 575 | if (unlock) { |
| 576 | buf[0] = MXT_UNLOCK_CMD_LSB; |
| 577 | buf[1] = MXT_UNLOCK_CMD_MSB; |
| 578 | } else { |
| 579 | buf[0] = 0x01; |
| 580 | buf[1] = 0x01; |
| 581 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 582 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 583 | ret = mxt_bootloader_write(data, buf, 2); |
| 584 | if (ret) |
| 585 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 590 | static int __mxt_read_reg(struct i2c_client *client, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 591 | u16 reg, u16 len, void *val) |
| 592 | { |
| 593 | struct i2c_msg xfer[2]; |
| 594 | u8 buf[2]; |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 595 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 596 | |
| 597 | buf[0] = reg & 0xff; |
| 598 | buf[1] = (reg >> 8) & 0xff; |
| 599 | |
| 600 | /* Write register */ |
| 601 | xfer[0].addr = client->addr; |
| 602 | xfer[0].flags = 0; |
| 603 | xfer[0].len = 2; |
| 604 | xfer[0].buf = buf; |
| 605 | |
| 606 | /* Read data */ |
| 607 | xfer[1].addr = client->addr; |
| 608 | xfer[1].flags = I2C_M_RD; |
| 609 | xfer[1].len = len; |
| 610 | xfer[1].buf = val; |
| 611 | |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 612 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 613 | if (ret == 2) { |
| 614 | ret = 0; |
| 615 | } else { |
| 616 | if (ret >= 0) |
| 617 | ret = -EIO; |
| 618 | dev_err(&client->dev, "%s: i2c transfer failed (%d)\n", |
| 619 | __func__, ret); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 622 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 625 | static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len, |
| 626 | const void *val) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 627 | { |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 628 | u8 *buf; |
| 629 | size_t count; |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 630 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 631 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 632 | count = len + 2; |
| 633 | buf = kmalloc(count, GFP_KERNEL); |
| 634 | if (!buf) |
| 635 | return -ENOMEM; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 636 | |
| 637 | buf[0] = reg & 0xff; |
| 638 | buf[1] = (reg >> 8) & 0xff; |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 639 | memcpy(&buf[2], val, len); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 640 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 641 | ret = i2c_master_send(client, buf, count); |
| 642 | if (ret == count) { |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 643 | ret = 0; |
| 644 | } else { |
| 645 | if (ret >= 0) |
| 646 | ret = -EIO; |
| 647 | dev_err(&client->dev, "%s: i2c send failed (%d)\n", |
| 648 | __func__, ret); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 651 | kfree(buf); |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 652 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 655 | static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 656 | { |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 657 | return __mxt_write_reg(client, reg, 1, &val); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 660 | static struct mxt_object * |
| 661 | mxt_get_object(struct mxt_data *data, u8 type) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 662 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 663 | struct mxt_object *object; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 664 | int i; |
| 665 | |
| 666 | for (i = 0; i < data->info.object_num; i++) { |
| 667 | object = data->object_table + i; |
| 668 | if (object->type == type) |
| 669 | return object; |
| 670 | } |
| 671 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 672 | dev_warn(&data->client->dev, "Invalid object type T%u\n", type); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 673 | return NULL; |
| 674 | } |
| 675 | |
Nick Dyer | 497767d | 2014-07-23 12:47:50 -0700 | [diff] [blame] | 676 | static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg) |
| 677 | { |
| 678 | struct device *dev = &data->client->dev; |
| 679 | u8 status = msg[1]; |
| 680 | u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16); |
| 681 | |
| 682 | complete(&data->crc_completion); |
| 683 | |
| 684 | if (crc != data->config_crc) { |
| 685 | data->config_crc = crc; |
| 686 | dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc); |
| 687 | } |
| 688 | |
| 689 | /* Detect reset */ |
| 690 | if (status & MXT_T6_STATUS_RESET) |
| 691 | complete(&data->reset_completion); |
| 692 | |
| 693 | /* Output debug if status has changed */ |
| 694 | if (status != data->t6_status) |
| 695 | dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n", |
| 696 | status, |
| 697 | status == 0 ? " OK" : "", |
| 698 | status & MXT_T6_STATUS_RESET ? " RESET" : "", |
| 699 | status & MXT_T6_STATUS_OFL ? " OFL" : "", |
| 700 | status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "", |
| 701 | status & MXT_T6_STATUS_CAL ? " CAL" : "", |
| 702 | status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "", |
| 703 | status & MXT_T6_STATUS_COMSERR ? " COMSERR" : ""); |
| 704 | |
| 705 | /* Save current status */ |
| 706 | data->t6_status = status; |
| 707 | } |
| 708 | |
Linus Torvalds | 7f40548 | 2014-12-31 12:59:34 -0800 | [diff] [blame] | 709 | static int mxt_write_object(struct mxt_data *data, |
| 710 | u8 type, u8 offset, u8 val) |
| 711 | { |
| 712 | struct mxt_object *object; |
| 713 | u16 reg; |
| 714 | |
| 715 | object = mxt_get_object(data, type); |
| 716 | if (!object || offset >= mxt_obj_size(object)) |
| 717 | return -EINVAL; |
| 718 | |
| 719 | reg = object->start_address; |
| 720 | return mxt_write_reg(data->client, reg + offset, val); |
| 721 | } |
| 722 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 723 | static void mxt_input_button(struct mxt_data *data, u8 *message) |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 724 | { |
| 725 | struct input_dev *input = data->input_dev; |
Nick Dyer | fb5e4c3e | 2014-05-18 23:00:15 -0700 | [diff] [blame] | 726 | const struct mxt_platform_data *pdata = data->pdata; |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 727 | bool button; |
| 728 | int i; |
| 729 | |
| 730 | /* Active-low switch */ |
Nick Dyer | fb5e4c3e | 2014-05-18 23:00:15 -0700 | [diff] [blame] | 731 | for (i = 0; i < pdata->t19_num_keys; i++) { |
| 732 | if (pdata->t19_keymap[i] == KEY_RESERVED) |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 733 | continue; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 734 | button = !(message[1] & (1 << i)); |
Nick Dyer | fb5e4c3e | 2014-05-18 23:00:15 -0700 | [diff] [blame] | 735 | input_report_key(input, pdata->t19_keymap[i], button); |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 739 | static void mxt_input_sync(struct mxt_data *data) |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 740 | { |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 741 | input_mt_report_pointer_emulation(data->input_dev, |
| 742 | data->pdata->t19_num_keys); |
| 743 | input_sync(data->input_dev); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 746 | static void mxt_proc_t9_message(struct mxt_data *data, u8 *message) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 747 | { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 748 | struct device *dev = &data->client->dev; |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 749 | struct input_dev *input_dev = data->input_dev; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 750 | int id; |
| 751 | u8 status; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 752 | int x; |
| 753 | int y; |
| 754 | int area; |
Nick Dyer | fea9e46 | 2014-05-18 23:21:48 -0700 | [diff] [blame] | 755 | int amplitude; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 756 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 757 | id = message[0] - data->T9_reportid_min; |
| 758 | status = message[1]; |
| 759 | x = (message[2] << 4) | ((message[4] >> 4) & 0xf); |
| 760 | y = (message[3] << 4) | ((message[4] & 0xf)); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 761 | |
| 762 | /* Handle 10/12 bit switching */ |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 763 | if (data->max_x < 1024) |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 764 | x >>= 2; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 765 | if (data->max_y < 1024) |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 766 | y >>= 2; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 767 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 768 | area = message[5]; |
| 769 | amplitude = message[6]; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 770 | |
Daniel Kurtz | b2e459b | 2012-06-28 21:08:18 +0800 | [diff] [blame] | 771 | dev_dbg(dev, |
| 772 | "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n", |
| 773 | id, |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 774 | (status & MXT_T9_DETECT) ? 'D' : '.', |
| 775 | (status & MXT_T9_PRESS) ? 'P' : '.', |
| 776 | (status & MXT_T9_RELEASE) ? 'R' : '.', |
| 777 | (status & MXT_T9_MOVE) ? 'M' : '.', |
| 778 | (status & MXT_T9_VECTOR) ? 'V' : '.', |
| 779 | (status & MXT_T9_AMP) ? 'A' : '.', |
| 780 | (status & MXT_T9_SUPPRESS) ? 'S' : '.', |
| 781 | (status & MXT_T9_UNGRIP) ? 'U' : '.', |
Nick Dyer | fea9e46 | 2014-05-18 23:21:48 -0700 | [diff] [blame] | 782 | x, y, area, amplitude); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 783 | |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 784 | input_mt_slot(input_dev, id); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 785 | |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 786 | if (status & MXT_T9_DETECT) { |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 787 | /* |
| 788 | * Multiple bits may be set if the host is slow to read |
| 789 | * the status messages, indicating all the events that |
| 790 | * have happened. |
| 791 | */ |
| 792 | if (status & MXT_T9_RELEASE) { |
| 793 | input_mt_report_slot_state(input_dev, |
| 794 | MT_TOOL_FINGER, 0); |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 795 | mxt_input_sync(data); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* Touch active */ |
| 799 | input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1); |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 800 | input_report_abs(input_dev, ABS_MT_POSITION_X, x); |
| 801 | input_report_abs(input_dev, ABS_MT_POSITION_Y, y); |
Nick Dyer | fea9e46 | 2014-05-18 23:21:48 -0700 | [diff] [blame] | 802 | input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude); |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 803 | input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 804 | } else { |
| 805 | /* Touch no longer active, close out slot */ |
| 806 | input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0); |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 807 | } |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 808 | |
| 809 | data->update_input = true; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 812 | static void mxt_proc_t100_message(struct mxt_data *data, u8 *message) |
| 813 | { |
| 814 | struct device *dev = &data->client->dev; |
| 815 | struct input_dev *input_dev = data->input_dev; |
| 816 | int id; |
| 817 | u8 status; |
| 818 | u8 type = 0; |
| 819 | u16 x; |
| 820 | u16 y; |
| 821 | int distance = 0; |
| 822 | int tool = 0; |
| 823 | u8 major = 0; |
| 824 | u8 pressure = 0; |
| 825 | u8 orientation = 0; |
| 826 | |
| 827 | id = message[0] - data->T100_reportid_min - 2; |
| 828 | |
| 829 | /* ignore SCRSTATUS events */ |
| 830 | if (id < 0) |
| 831 | return; |
| 832 | |
| 833 | status = message[1]; |
| 834 | x = get_unaligned_le16(&message[2]); |
| 835 | y = get_unaligned_le16(&message[4]); |
| 836 | |
| 837 | if (status & MXT_T100_DETECT) { |
| 838 | type = (status & MXT_T100_TYPE_MASK) >> 4; |
| 839 | |
| 840 | switch (type) { |
| 841 | case MXT_T100_TYPE_HOVERING_FINGER: |
| 842 | tool = MT_TOOL_FINGER; |
| 843 | distance = MXT_DISTANCE_HOVERING; |
| 844 | |
| 845 | if (data->t100_aux_vect) |
| 846 | orientation = message[data->t100_aux_vect]; |
| 847 | |
| 848 | break; |
| 849 | |
| 850 | case MXT_T100_TYPE_FINGER: |
| 851 | case MXT_T100_TYPE_GLOVE: |
| 852 | tool = MT_TOOL_FINGER; |
| 853 | distance = MXT_DISTANCE_ACTIVE_TOUCH; |
| 854 | |
| 855 | if (data->t100_aux_area) |
| 856 | major = message[data->t100_aux_area]; |
| 857 | |
| 858 | if (data->t100_aux_ampl) |
| 859 | pressure = message[data->t100_aux_ampl]; |
| 860 | |
| 861 | if (data->t100_aux_vect) |
| 862 | orientation = message[data->t100_aux_vect]; |
| 863 | |
| 864 | break; |
| 865 | |
| 866 | case MXT_T100_TYPE_PASSIVE_STYLUS: |
| 867 | tool = MT_TOOL_PEN; |
| 868 | |
| 869 | /* |
| 870 | * Passive stylus is reported with size zero so |
| 871 | * hardcode. |
| 872 | */ |
| 873 | major = MXT_TOUCH_MAJOR_DEFAULT; |
| 874 | |
| 875 | if (data->t100_aux_ampl) |
| 876 | pressure = message[data->t100_aux_ampl]; |
| 877 | |
| 878 | break; |
| 879 | |
| 880 | case MXT_T100_TYPE_LARGE_TOUCH: |
| 881 | /* Ignore suppressed touch */ |
| 882 | break; |
| 883 | |
| 884 | default: |
| 885 | dev_dbg(dev, "Unexpected T100 type\n"); |
| 886 | return; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Values reported should be non-zero if tool is touching the |
| 892 | * device |
| 893 | */ |
| 894 | if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER) |
| 895 | pressure = MXT_PRESSURE_DEFAULT; |
| 896 | |
| 897 | input_mt_slot(input_dev, id); |
| 898 | |
| 899 | if (status & MXT_T100_DETECT) { |
| 900 | dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n", |
| 901 | id, type, x, y, major, pressure, orientation); |
| 902 | |
| 903 | input_mt_report_slot_state(input_dev, tool, 1); |
| 904 | input_report_abs(input_dev, ABS_MT_POSITION_X, x); |
| 905 | input_report_abs(input_dev, ABS_MT_POSITION_Y, y); |
| 906 | input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major); |
| 907 | input_report_abs(input_dev, ABS_MT_PRESSURE, pressure); |
| 908 | input_report_abs(input_dev, ABS_MT_DISTANCE, distance); |
| 909 | input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation); |
| 910 | } else { |
| 911 | dev_dbg(dev, "[%u] release\n", id); |
| 912 | |
| 913 | /* close out slot */ |
| 914 | input_mt_report_slot_state(input_dev, 0, 0); |
| 915 | } |
| 916 | |
| 917 | data->update_input = true; |
| 918 | } |
| 919 | |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 920 | static int mxt_proc_message(struct mxt_data *data, u8 *message) |
Daniel Kurtz | 04a7918 | 2012-06-28 21:08:21 +0800 | [diff] [blame] | 921 | { |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 922 | u8 report_id = message[0]; |
| 923 | |
| 924 | if (report_id == MXT_RPTID_NOMSG) |
| 925 | return 0; |
| 926 | |
| 927 | if (report_id == data->T6_reportid) { |
| 928 | mxt_proc_t6_messages(data, message); |
| 929 | } else if (!data->input_dev) { |
| 930 | /* |
| 931 | * Do not report events if input device |
| 932 | * is not yet registered. |
| 933 | */ |
| 934 | mxt_dump_message(data, message); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 935 | } else if (report_id >= data->T9_reportid_min && |
| 936 | report_id <= data->T9_reportid_max) { |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 937 | mxt_proc_t9_message(data, message); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 938 | } else if (report_id >= data->T100_reportid_min && |
| 939 | report_id <= data->T100_reportid_max) { |
| 940 | mxt_proc_t100_message(data, message); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 941 | } else if (report_id == data->T19_reportid) { |
| 942 | mxt_input_button(data, message); |
| 943 | data->update_input = true; |
| 944 | } else { |
| 945 | mxt_dump_message(data, message); |
| 946 | } |
| 947 | |
| 948 | return 1; |
| 949 | } |
| 950 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 951 | static int mxt_read_and_process_messages(struct mxt_data *data, u8 count) |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 952 | { |
| 953 | struct device *dev = &data->client->dev; |
| 954 | int ret; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 955 | int i; |
| 956 | u8 num_valid = 0; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 957 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 958 | /* Safety check for msg_buf */ |
| 959 | if (count > data->max_reportid) |
| 960 | return -EINVAL; |
| 961 | |
| 962 | /* Process remaining messages if necessary */ |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 963 | ret = __mxt_read_reg(data->client, data->T5_address, |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 964 | data->T5_msg_size * count, data->msg_buf); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 965 | if (ret) { |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 966 | dev_err(dev, "Failed to read %u messages (%d)\n", count, ret); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 967 | return ret; |
| 968 | } |
| 969 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 970 | for (i = 0; i < count; i++) { |
| 971 | ret = mxt_proc_message(data, |
| 972 | data->msg_buf + data->T5_msg_size * i); |
| 973 | |
| 974 | if (ret == 1) |
| 975 | num_valid++; |
| 976 | } |
| 977 | |
| 978 | /* return number of messages read */ |
| 979 | return num_valid; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 982 | static irqreturn_t mxt_process_messages_t44(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 983 | { |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 984 | struct device *dev = &data->client->dev; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 985 | int ret; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 986 | u8 count, num_left; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 987 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 988 | /* Read T44 and T5 together */ |
| 989 | ret = __mxt_read_reg(data->client, data->T44_address, |
| 990 | data->T5_msg_size + 1, data->msg_buf); |
| 991 | if (ret) { |
| 992 | dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret); |
| 993 | return IRQ_NONE; |
| 994 | } |
| 995 | |
| 996 | count = data->msg_buf[0]; |
| 997 | |
| 998 | if (count == 0) { |
Nick Dyer | 651b460 | 2014-09-09 11:24:21 -0700 | [diff] [blame] | 999 | /* |
| 1000 | * This condition is caused by the CHG line being configured |
| 1001 | * in Mode 0. It results in unnecessary I2C operations but it |
| 1002 | * is benign. |
| 1003 | */ |
| 1004 | dev_dbg(dev, "Interrupt triggered but zero messages\n"); |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1005 | return IRQ_NONE; |
| 1006 | } else if (count > data->max_reportid) { |
| 1007 | dev_err(dev, "T44 count %d exceeded max report id\n", count); |
| 1008 | count = data->max_reportid; |
| 1009 | } |
| 1010 | |
| 1011 | /* Process first message */ |
| 1012 | ret = mxt_proc_message(data, data->msg_buf + 1); |
| 1013 | if (ret < 0) { |
| 1014 | dev_warn(dev, "Unexpected invalid message\n"); |
| 1015 | return IRQ_NONE; |
| 1016 | } |
| 1017 | |
| 1018 | num_left = count - 1; |
| 1019 | |
| 1020 | /* Process remaining messages if necessary */ |
| 1021 | if (num_left) { |
| 1022 | ret = mxt_read_and_process_messages(data, num_left); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1023 | if (ret < 0) |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1024 | goto end; |
| 1025 | else if (ret != num_left) |
| 1026 | dev_warn(dev, "Unexpected invalid message\n"); |
| 1027 | } |
| 1028 | |
| 1029 | end: |
| 1030 | if (data->update_input) { |
| 1031 | mxt_input_sync(data); |
| 1032 | data->update_input = false; |
| 1033 | } |
| 1034 | |
| 1035 | return IRQ_HANDLED; |
| 1036 | } |
| 1037 | |
| 1038 | static int mxt_process_messages_until_invalid(struct mxt_data *data) |
| 1039 | { |
| 1040 | struct device *dev = &data->client->dev; |
| 1041 | int count, read; |
| 1042 | u8 tries = 2; |
| 1043 | |
| 1044 | count = data->max_reportid; |
| 1045 | |
| 1046 | /* Read messages until we force an invalid */ |
| 1047 | do { |
| 1048 | read = mxt_read_and_process_messages(data, count); |
| 1049 | if (read < count) |
| 1050 | return 0; |
| 1051 | } while (--tries); |
| 1052 | |
| 1053 | if (data->update_input) { |
| 1054 | mxt_input_sync(data); |
| 1055 | data->update_input = false; |
| 1056 | } |
| 1057 | |
| 1058 | dev_err(dev, "CHG pin isn't cleared\n"); |
| 1059 | return -EBUSY; |
| 1060 | } |
| 1061 | |
| 1062 | static irqreturn_t mxt_process_messages(struct mxt_data *data) |
| 1063 | { |
| 1064 | int total_handled, num_handled; |
| 1065 | u8 count = data->last_message_count; |
| 1066 | |
| 1067 | if (count < 1 || count > data->max_reportid) |
| 1068 | count = 1; |
| 1069 | |
| 1070 | /* include final invalid message */ |
| 1071 | total_handled = mxt_read_and_process_messages(data, count + 1); |
| 1072 | if (total_handled < 0) |
| 1073 | return IRQ_NONE; |
| 1074 | /* if there were invalid messages, then we are done */ |
| 1075 | else if (total_handled <= count) |
| 1076 | goto update_count; |
| 1077 | |
| 1078 | /* keep reading two msgs until one is invalid or reportid limit */ |
| 1079 | do { |
| 1080 | num_handled = mxt_read_and_process_messages(data, 2); |
| 1081 | if (num_handled < 0) |
Nick Dyer | 8d4e163 | 2014-05-18 23:00:56 -0700 | [diff] [blame] | 1082 | return IRQ_NONE; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1083 | |
| 1084 | total_handled += num_handled; |
| 1085 | |
| 1086 | if (num_handled < 2) |
| 1087 | break; |
| 1088 | } while (total_handled < data->num_touchids); |
| 1089 | |
| 1090 | update_count: |
| 1091 | data->last_message_count = total_handled; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1092 | |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1093 | if (data->update_input) { |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 1094 | mxt_input_sync(data); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1095 | data->update_input = false; |
| 1096 | } |
Daniel Kurtz | 64464ae | 2012-06-28 21:08:23 +0800 | [diff] [blame] | 1097 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1098 | return IRQ_HANDLED; |
| 1099 | } |
| 1100 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 1101 | static irqreturn_t mxt_interrupt(int irq, void *dev_id) |
| 1102 | { |
| 1103 | struct mxt_data *data = dev_id; |
| 1104 | |
| 1105 | if (data->in_bootloader) { |
| 1106 | /* bootloader state transition completion */ |
| 1107 | complete(&data->bl_completion); |
| 1108 | return IRQ_HANDLED; |
| 1109 | } |
| 1110 | |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1111 | if (!data->object_table) |
| 1112 | return IRQ_HANDLED; |
| 1113 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1114 | if (data->T44_address) { |
| 1115 | return mxt_process_messages_t44(data); |
| 1116 | } else { |
| 1117 | return mxt_process_messages(data); |
| 1118 | } |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 1121 | static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset, |
| 1122 | u8 value, bool wait) |
| 1123 | { |
| 1124 | u16 reg; |
| 1125 | u8 command_register; |
| 1126 | int timeout_counter = 0; |
| 1127 | int ret; |
| 1128 | |
| 1129 | reg = data->T6_address + cmd_offset; |
| 1130 | |
| 1131 | ret = mxt_write_reg(data->client, reg, value); |
| 1132 | if (ret) |
| 1133 | return ret; |
| 1134 | |
| 1135 | if (!wait) |
| 1136 | return 0; |
| 1137 | |
| 1138 | do { |
| 1139 | msleep(20); |
| 1140 | ret = __mxt_read_reg(data->client, reg, 1, &command_register); |
| 1141 | if (ret) |
| 1142 | return ret; |
| 1143 | } while (command_register != 0 && timeout_counter++ <= 100); |
| 1144 | |
| 1145 | if (timeout_counter > 100) { |
| 1146 | dev_err(&data->client->dev, "Command failed!\n"); |
| 1147 | return -EIO; |
| 1148 | } |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | static int mxt_soft_reset(struct mxt_data *data) |
| 1154 | { |
| 1155 | struct device *dev = &data->client->dev; |
| 1156 | int ret = 0; |
| 1157 | |
| 1158 | dev_info(dev, "Resetting chip\n"); |
| 1159 | |
| 1160 | reinit_completion(&data->reset_completion); |
| 1161 | |
| 1162 | ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false); |
| 1163 | if (ret) |
| 1164 | return ret; |
| 1165 | |
| 1166 | ret = mxt_wait_for_completion(data, &data->reset_completion, |
| 1167 | MXT_RESET_TIMEOUT); |
| 1168 | if (ret) |
| 1169 | return ret; |
| 1170 | |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 1174 | static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value) |
| 1175 | { |
| 1176 | /* |
| 1177 | * On failure, CRC is set to 0 and config will always be |
| 1178 | * downloaded. |
| 1179 | */ |
| 1180 | data->config_crc = 0; |
| 1181 | reinit_completion(&data->crc_completion); |
| 1182 | |
| 1183 | mxt_t6_command(data, cmd, value, true); |
| 1184 | |
| 1185 | /* |
| 1186 | * Wait for crc message. On failure, CRC is set to 0 and config will |
| 1187 | * always be downloaded. |
| 1188 | */ |
| 1189 | mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT); |
| 1190 | } |
| 1191 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1192 | static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte) |
| 1193 | { |
| 1194 | static const unsigned int crcpoly = 0x80001B; |
| 1195 | u32 result; |
| 1196 | u32 data_word; |
| 1197 | |
| 1198 | data_word = (secondbyte << 8) | firstbyte; |
| 1199 | result = ((*crc << 1) ^ data_word); |
| 1200 | |
| 1201 | if (result & 0x1000000) |
| 1202 | result ^= crcpoly; |
| 1203 | |
| 1204 | *crc = result; |
| 1205 | } |
| 1206 | |
| 1207 | static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off) |
| 1208 | { |
| 1209 | u32 crc = 0; |
| 1210 | u8 *ptr = base + start_off; |
| 1211 | u8 *last_val = base + end_off - 1; |
| 1212 | |
| 1213 | if (end_off < start_off) |
| 1214 | return -EINVAL; |
| 1215 | |
| 1216 | while (ptr < last_val) { |
| 1217 | mxt_calc_crc24(&crc, *ptr, *(ptr + 1)); |
| 1218 | ptr += 2; |
| 1219 | } |
| 1220 | |
| 1221 | /* if len is odd, fill the last byte with 0 */ |
| 1222 | if (ptr == last_val) |
| 1223 | mxt_calc_crc24(&crc, *ptr, 0); |
| 1224 | |
| 1225 | /* Mask to 24-bit */ |
| 1226 | crc &= 0x00FFFFFF; |
| 1227 | |
| 1228 | return crc; |
| 1229 | } |
| 1230 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1231 | static int mxt_prepare_cfg_mem(struct mxt_data *data, |
| 1232 | const struct firmware *cfg, |
| 1233 | unsigned int data_pos, |
| 1234 | unsigned int cfg_start_ofs, |
| 1235 | u8 *config_mem, |
| 1236 | size_t config_mem_size) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1237 | { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1238 | struct device *dev = &data->client->dev; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1239 | struct mxt_object *object; |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1240 | unsigned int type, instance, size, byte_offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1241 | int offset; |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1242 | int ret; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1243 | int i; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1244 | u16 reg; |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1245 | u8 val; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1246 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1247 | while (data_pos < cfg->size) { |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1248 | /* Read type, instance, length */ |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1249 | ret = sscanf(cfg->data + data_pos, "%x %x %x%n", |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1250 | &type, &instance, &size, &offset); |
| 1251 | if (ret == 0) { |
| 1252 | /* EOF */ |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1253 | break; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1254 | } else if (ret != 3) { |
| 1255 | dev_err(dev, "Bad format: failed to parse object\n"); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1256 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1257 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1258 | data_pos += offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1259 | |
| 1260 | object = mxt_get_object(data, type); |
| 1261 | if (!object) { |
| 1262 | /* Skip object */ |
| 1263 | for (i = 0; i < size; i++) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1264 | ret = sscanf(cfg->data + data_pos, "%hhx%n", |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1265 | &val, &offset); |
| 1266 | if (ret != 1) { |
| 1267 | dev_err(dev, "Bad format in T%d at %d\n", |
| 1268 | type, i); |
| 1269 | return -EINVAL; |
| 1270 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1271 | data_pos += offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1272 | } |
| 1273 | continue; |
| 1274 | } |
| 1275 | |
| 1276 | if (size > mxt_obj_size(object)) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1277 | /* |
| 1278 | * Either we are in fallback mode due to wrong |
| 1279 | * config or config from a later fw version, |
| 1280 | * or the file is corrupt or hand-edited. |
| 1281 | */ |
| 1282 | dev_warn(dev, "Discarding %zu byte(s) in T%u\n", |
| 1283 | size - mxt_obj_size(object), type); |
| 1284 | } else if (mxt_obj_size(object) > size) { |
| 1285 | /* |
| 1286 | * If firmware is upgraded, new bytes may be added to |
| 1287 | * end of objects. It is generally forward compatible |
| 1288 | * to zero these bytes - previous behaviour will be |
| 1289 | * retained. However this does invalidate the CRC and |
| 1290 | * will force fallback mode until the configuration is |
| 1291 | * updated. We warn here but do nothing else - the |
| 1292 | * malloc has zeroed the entire configuration. |
| 1293 | */ |
| 1294 | dev_warn(dev, "Zeroing %zu byte(s) in T%d\n", |
| 1295 | mxt_obj_size(object) - size, type); |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | if (instance >= mxt_obj_instances(object)) { |
| 1299 | dev_err(dev, "Object instances exceeded!\n"); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1300 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | reg = object->start_address + mxt_obj_size(object) * instance; |
| 1304 | |
| 1305 | for (i = 0; i < size; i++) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1306 | ret = sscanf(cfg->data + data_pos, "%hhx%n", |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1307 | &val, |
| 1308 | &offset); |
| 1309 | if (ret != 1) { |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1310 | dev_err(dev, "Bad format in T%d at %d\n", |
| 1311 | type, i); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1312 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1313 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1314 | data_pos += offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1315 | |
| 1316 | if (i > mxt_obj_size(object)) |
| 1317 | continue; |
| 1318 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1319 | byte_offset = reg + i - cfg_start_ofs; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1320 | |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1321 | if (byte_offset >= 0 && byte_offset < config_mem_size) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1322 | *(config_mem + byte_offset) = val; |
| 1323 | } else { |
| 1324 | dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n", |
| 1325 | reg, object->type, byte_offset); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1326 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1327 | } |
| 1328 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start, |
| 1335 | u8 *config_mem, size_t config_mem_size) |
| 1336 | { |
| 1337 | unsigned int byte_offset = 0; |
| 1338 | int error; |
| 1339 | |
| 1340 | /* Write configuration as blocks */ |
| 1341 | while (byte_offset < config_mem_size) { |
| 1342 | unsigned int size = config_mem_size - byte_offset; |
| 1343 | |
| 1344 | if (size > MXT_MAX_BLOCK_WRITE) |
| 1345 | size = MXT_MAX_BLOCK_WRITE; |
| 1346 | |
| 1347 | error = __mxt_write_reg(data->client, |
| 1348 | cfg_start + byte_offset, |
| 1349 | size, config_mem + byte_offset); |
| 1350 | if (error) { |
| 1351 | dev_err(&data->client->dev, |
| 1352 | "Config write error, ret=%d\n", error); |
| 1353 | return error; |
| 1354 | } |
| 1355 | |
| 1356 | byte_offset += size; |
| 1357 | } |
| 1358 | |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
| 1362 | /* |
| 1363 | * mxt_update_cfg - download configuration to chip |
| 1364 | * |
| 1365 | * Atmel Raw Config File Format |
| 1366 | * |
| 1367 | * The first four lines of the raw config file contain: |
| 1368 | * 1) Version |
| 1369 | * 2) Chip ID Information (first 7 bytes of device memory) |
| 1370 | * 3) Chip Information Block 24-bit CRC Checksum |
| 1371 | * 4) Chip Configuration 24-bit CRC Checksum |
| 1372 | * |
| 1373 | * The rest of the file consists of one line per object instance: |
| 1374 | * <TYPE> <INSTANCE> <SIZE> <CONTENTS> |
| 1375 | * |
| 1376 | * <TYPE> - 2-byte object type as hex |
| 1377 | * <INSTANCE> - 2-byte object instance number as hex |
| 1378 | * <SIZE> - 2-byte object size as hex |
| 1379 | * <CONTENTS> - array of <SIZE> 1-byte hex values |
| 1380 | */ |
| 1381 | static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg) |
| 1382 | { |
| 1383 | struct device *dev = &data->client->dev; |
| 1384 | struct mxt_info cfg_info; |
| 1385 | int ret; |
| 1386 | int offset; |
| 1387 | int data_pos; |
| 1388 | int i; |
| 1389 | int cfg_start_ofs; |
| 1390 | u32 info_crc, config_crc, calculated_crc; |
| 1391 | u8 *config_mem; |
| 1392 | size_t config_mem_size; |
| 1393 | |
| 1394 | mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1); |
| 1395 | |
| 1396 | if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) { |
| 1397 | dev_err(dev, "Unrecognised config file\n"); |
| 1398 | return -EINVAL; |
| 1399 | } |
| 1400 | |
| 1401 | data_pos = strlen(MXT_CFG_MAGIC); |
| 1402 | |
| 1403 | /* Load information block and check */ |
| 1404 | for (i = 0; i < sizeof(struct mxt_info); i++) { |
| 1405 | ret = sscanf(cfg->data + data_pos, "%hhx%n", |
| 1406 | (unsigned char *)&cfg_info + i, |
| 1407 | &offset); |
| 1408 | if (ret != 1) { |
| 1409 | dev_err(dev, "Bad format\n"); |
| 1410 | return -EINVAL; |
| 1411 | } |
| 1412 | |
| 1413 | data_pos += offset; |
| 1414 | } |
| 1415 | |
| 1416 | if (cfg_info.family_id != data->info.family_id) { |
| 1417 | dev_err(dev, "Family ID mismatch!\n"); |
| 1418 | return -EINVAL; |
| 1419 | } |
| 1420 | |
| 1421 | if (cfg_info.variant_id != data->info.variant_id) { |
| 1422 | dev_err(dev, "Variant ID mismatch!\n"); |
| 1423 | return -EINVAL; |
| 1424 | } |
| 1425 | |
| 1426 | /* Read CRCs */ |
| 1427 | ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset); |
| 1428 | if (ret != 1) { |
| 1429 | dev_err(dev, "Bad format: failed to parse Info CRC\n"); |
| 1430 | return -EINVAL; |
| 1431 | } |
| 1432 | data_pos += offset; |
| 1433 | |
| 1434 | ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset); |
| 1435 | if (ret != 1) { |
| 1436 | dev_err(dev, "Bad format: failed to parse Config CRC\n"); |
| 1437 | return -EINVAL; |
| 1438 | } |
| 1439 | data_pos += offset; |
| 1440 | |
| 1441 | /* |
| 1442 | * The Info Block CRC is calculated over mxt_info and the object |
| 1443 | * table. If it does not match then we are trying to load the |
| 1444 | * configuration from a different chip or firmware version, so |
| 1445 | * the configuration CRC is invalid anyway. |
| 1446 | */ |
| 1447 | if (info_crc == data->info_crc) { |
| 1448 | if (config_crc == 0 || data->config_crc == 0) { |
| 1449 | dev_info(dev, "CRC zero, attempting to apply config\n"); |
| 1450 | } else if (config_crc == data->config_crc) { |
| 1451 | dev_dbg(dev, "Config CRC 0x%06X: OK\n", |
| 1452 | data->config_crc); |
| 1453 | return 0; |
| 1454 | } else { |
| 1455 | dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n", |
| 1456 | data->config_crc, config_crc); |
| 1457 | } |
| 1458 | } else { |
| 1459 | dev_warn(dev, |
| 1460 | "Warning: Info CRC error - device=0x%06X file=0x%06X\n", |
| 1461 | data->info_crc, info_crc); |
| 1462 | } |
| 1463 | |
| 1464 | /* Malloc memory to store configuration */ |
| 1465 | cfg_start_ofs = MXT_OBJECT_START + |
| 1466 | data->info.object_num * sizeof(struct mxt_object) + |
| 1467 | MXT_INFO_CHECKSUM_SIZE; |
| 1468 | config_mem_size = data->mem_size - cfg_start_ofs; |
| 1469 | config_mem = kzalloc(config_mem_size, GFP_KERNEL); |
| 1470 | if (!config_mem) { |
| 1471 | dev_err(dev, "Failed to allocate memory\n"); |
| 1472 | return -ENOMEM; |
| 1473 | } |
| 1474 | |
| 1475 | ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs, |
| 1476 | config_mem, config_mem_size); |
| 1477 | if (ret) |
| 1478 | goto release_mem; |
| 1479 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1480 | /* Calculate crc of the received configs (not the raw config file) */ |
| 1481 | if (data->T7_address < cfg_start_ofs) { |
| 1482 | dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n", |
| 1483 | data->T7_address, cfg_start_ofs); |
| 1484 | ret = 0; |
| 1485 | goto release_mem; |
| 1486 | } |
| 1487 | |
| 1488 | calculated_crc = mxt_calculate_crc(config_mem, |
| 1489 | data->T7_address - cfg_start_ofs, |
| 1490 | config_mem_size); |
| 1491 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1492 | if (config_crc > 0 && config_crc != calculated_crc) |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1493 | dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n", |
| 1494 | calculated_crc, config_crc); |
| 1495 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1496 | ret = mxt_upload_cfg_mem(data, cfg_start_ofs, |
| 1497 | config_mem, config_mem_size); |
| 1498 | if (ret) |
| 1499 | goto release_mem; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1500 | |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 1501 | mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE); |
| 1502 | |
| 1503 | ret = mxt_soft_reset(data); |
| 1504 | if (ret) |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1505 | goto release_mem; |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 1506 | |
| 1507 | dev_info(dev, "Config successfully updated\n"); |
| 1508 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1509 | release_mem: |
| 1510 | kfree(config_mem); |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1511 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1514 | static int mxt_acquire_irq(struct mxt_data *data) |
| 1515 | { |
| 1516 | int error; |
| 1517 | |
| 1518 | enable_irq(data->irq); |
| 1519 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1520 | error = mxt_process_messages_until_invalid(data); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1521 | if (error) |
| 1522 | return error; |
| 1523 | |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 1527 | static int mxt_get_info(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1528 | { |
| 1529 | struct i2c_client *client = data->client; |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 1530 | struct mxt_info *info = &data->info; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1531 | int error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1532 | |
Daniel Kurtz | 23003a84 | 2012-06-28 21:08:14 +0800 | [diff] [blame] | 1533 | /* Read 7-byte info block starting at address 0 */ |
| 1534 | error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1535 | if (error) |
| 1536 | return error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1537 | |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 1541 | static void mxt_free_input_device(struct mxt_data *data) |
| 1542 | { |
| 1543 | if (data->input_dev) { |
| 1544 | input_unregister_device(data->input_dev); |
| 1545 | data->input_dev = NULL; |
| 1546 | } |
| 1547 | } |
| 1548 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1549 | static void mxt_free_object_table(struct mxt_data *data) |
| 1550 | { |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1551 | kfree(data->object_table); |
| 1552 | data->object_table = NULL; |
| 1553 | kfree(data->msg_buf); |
| 1554 | data->msg_buf = NULL; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1555 | data->T5_address = 0; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1556 | data->T5_msg_size = 0; |
| 1557 | data->T6_reportid = 0; |
| 1558 | data->T7_address = 0; |
| 1559 | data->T9_reportid_min = 0; |
| 1560 | data->T9_reportid_max = 0; |
| 1561 | data->T19_reportid = 0; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1562 | data->T44_address = 0; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1563 | data->T100_reportid_min = 0; |
| 1564 | data->T100_reportid_max = 0; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1565 | data->max_reportid = 0; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 1568 | static int mxt_get_object_table(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1569 | { |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1570 | struct i2c_client *client = data->client; |
| 1571 | size_t table_size; |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1572 | struct mxt_object *object_table; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1573 | int error; |
| 1574 | int i; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1575 | u8 reportid; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1576 | u16 end_address; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1577 | |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1578 | table_size = data->info.object_num * sizeof(struct mxt_object); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1579 | object_table = kzalloc(table_size, GFP_KERNEL); |
| 1580 | if (!object_table) { |
| 1581 | dev_err(&data->client->dev, "Failed to allocate memory\n"); |
| 1582 | return -ENOMEM; |
| 1583 | } |
| 1584 | |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1585 | error = __mxt_read_reg(client, MXT_OBJECT_START, table_size, |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1586 | object_table); |
| 1587 | if (error) { |
| 1588 | kfree(object_table); |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1589 | return error; |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1590 | } |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1591 | |
| 1592 | /* Valid Report IDs start counting from 1 */ |
| 1593 | reportid = 1; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1594 | data->mem_size = 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1595 | for (i = 0; i < data->info.object_num; i++) { |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1596 | struct mxt_object *object = object_table + i; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1597 | u8 min_id, max_id; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1598 | |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1599 | le16_to_cpus(&object->start_address); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1600 | |
| 1601 | if (object->num_report_ids) { |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1602 | min_id = reportid; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1603 | reportid += object->num_report_ids * |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 1604 | mxt_obj_instances(object); |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1605 | max_id = reportid - 1; |
| 1606 | } else { |
| 1607 | min_id = 0; |
| 1608 | max_id = 0; |
| 1609 | } |
| 1610 | |
| 1611 | dev_dbg(&data->client->dev, |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 1612 | "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n", |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 1613 | object->type, object->start_address, |
| 1614 | mxt_obj_size(object), mxt_obj_instances(object), |
| 1615 | min_id, max_id); |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1616 | |
| 1617 | switch (object->type) { |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1618 | case MXT_GEN_MESSAGE_T5: |
Nick Dyer | 437d4f3 | 2014-08-07 09:56:01 -0700 | [diff] [blame] | 1619 | if (data->info.family_id == 0x80 && |
| 1620 | data->info.version < 0x20) { |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1621 | /* |
Nick Dyer | 437d4f3 | 2014-08-07 09:56:01 -0700 | [diff] [blame] | 1622 | * On mXT224 firmware versions prior to V2.0 |
| 1623 | * read and discard unused CRC byte otherwise |
| 1624 | * DMA reads are misaligned. |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1625 | */ |
| 1626 | data->T5_msg_size = mxt_obj_size(object); |
| 1627 | } else { |
| 1628 | /* CRC not enabled, so skip last byte */ |
| 1629 | data->T5_msg_size = mxt_obj_size(object) - 1; |
| 1630 | } |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1631 | data->T5_address = object->start_address; |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1632 | break; |
Daniel Kurtz | fdf80421 | 2012-06-28 21:08:24 +0800 | [diff] [blame] | 1633 | case MXT_GEN_COMMAND_T6: |
| 1634 | data->T6_reportid = min_id; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 1635 | data->T6_address = object->start_address; |
Daniel Kurtz | fdf80421 | 2012-06-28 21:08:24 +0800 | [diff] [blame] | 1636 | break; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1637 | case MXT_GEN_POWER_T7: |
| 1638 | data->T7_address = object->start_address; |
| 1639 | break; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1640 | case MXT_TOUCH_MULTI_T9: |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1641 | data->multitouch = MXT_TOUCH_MULTI_T9; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1642 | data->T9_reportid_min = min_id; |
| 1643 | data->T9_reportid_max = max_id; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1644 | data->num_touchids = object->num_report_ids |
| 1645 | * mxt_obj_instances(object); |
| 1646 | break; |
| 1647 | case MXT_SPT_MESSAGECOUNT_T44: |
| 1648 | data->T44_address = object->start_address; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1649 | break; |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 1650 | case MXT_SPT_GPIOPWM_T19: |
| 1651 | data->T19_reportid = min_id; |
| 1652 | break; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1653 | case MXT_TOUCH_MULTITOUCHSCREEN_T100: |
| 1654 | data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100; |
| 1655 | data->T100_reportid_min = min_id; |
| 1656 | data->T100_reportid_max = max_id; |
| 1657 | /* first two report IDs reserved */ |
| 1658 | data->num_touchids = object->num_report_ids - 2; |
| 1659 | break; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1660 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1661 | |
| 1662 | end_address = object->start_address |
| 1663 | + mxt_obj_size(object) * mxt_obj_instances(object) - 1; |
| 1664 | |
| 1665 | if (end_address >= data->mem_size) |
| 1666 | data->mem_size = end_address + 1; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1669 | /* Store maximum reportid */ |
| 1670 | data->max_reportid = reportid; |
| 1671 | |
| 1672 | /* If T44 exists, T5 position has to be directly after */ |
| 1673 | if (data->T44_address && (data->T5_address != data->T44_address + 1)) { |
| 1674 | dev_err(&client->dev, "Invalid T44 position\n"); |
| 1675 | error = -EINVAL; |
| 1676 | goto free_object_table; |
| 1677 | } |
| 1678 | |
| 1679 | data->msg_buf = kcalloc(data->max_reportid, |
| 1680 | data->T5_msg_size, GFP_KERNEL); |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1681 | if (!data->msg_buf) { |
| 1682 | dev_err(&client->dev, "Failed to allocate message buffer\n"); |
| 1683 | error = -ENOMEM; |
| 1684 | goto free_object_table; |
| 1685 | } |
| 1686 | |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1687 | data->object_table = object_table; |
| 1688 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1689 | return 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1690 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1691 | free_object_table: |
| 1692 | mxt_free_object_table(data); |
| 1693 | return error; |
Daniel Kurtz | 7d4fa10 | 2012-06-28 21:08:19 +0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 1696 | static int mxt_read_t9_resolution(struct mxt_data *data) |
| 1697 | { |
| 1698 | struct i2c_client *client = data->client; |
| 1699 | int error; |
| 1700 | struct t9_range range; |
| 1701 | unsigned char orient; |
| 1702 | struct mxt_object *object; |
| 1703 | |
| 1704 | object = mxt_get_object(data, MXT_TOUCH_MULTI_T9); |
| 1705 | if (!object) |
| 1706 | return -EINVAL; |
| 1707 | |
| 1708 | error = __mxt_read_reg(client, |
| 1709 | object->start_address + MXT_T9_RANGE, |
| 1710 | sizeof(range), &range); |
| 1711 | if (error) |
| 1712 | return error; |
| 1713 | |
| 1714 | le16_to_cpus(&range.x); |
| 1715 | le16_to_cpus(&range.y); |
| 1716 | |
| 1717 | error = __mxt_read_reg(client, |
| 1718 | object->start_address + MXT_T9_ORIENT, |
| 1719 | 1, &orient); |
| 1720 | if (error) |
| 1721 | return error; |
| 1722 | |
| 1723 | /* Handle default values */ |
| 1724 | if (range.x == 0) |
| 1725 | range.x = 1023; |
| 1726 | |
| 1727 | if (range.y == 0) |
| 1728 | range.y = 1023; |
| 1729 | |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 1730 | if (orient & MXT_T9_ORIENT_SWITCH) { |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 1731 | data->max_x = range.y; |
| 1732 | data->max_y = range.x; |
| 1733 | } else { |
| 1734 | data->max_x = range.x; |
| 1735 | data->max_y = range.y; |
| 1736 | } |
| 1737 | |
| 1738 | dev_dbg(&client->dev, |
| 1739 | "Touchscreen size X%uY%u\n", data->max_x, data->max_y); |
| 1740 | |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1744 | static int mxt_read_t100_config(struct mxt_data *data) |
| 1745 | { |
| 1746 | struct i2c_client *client = data->client; |
| 1747 | int error; |
| 1748 | struct mxt_object *object; |
| 1749 | u16 range_x, range_y; |
| 1750 | u8 cfg, tchaux; |
| 1751 | u8 aux; |
| 1752 | |
| 1753 | object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100); |
| 1754 | if (!object) |
| 1755 | return -EINVAL; |
| 1756 | |
| 1757 | error = __mxt_read_reg(client, |
| 1758 | object->start_address + MXT_T100_XRANGE, |
| 1759 | sizeof(range_x), &range_x); |
| 1760 | if (error) |
| 1761 | return error; |
| 1762 | |
| 1763 | le16_to_cpus(&range_x); |
| 1764 | |
| 1765 | error = __mxt_read_reg(client, |
| 1766 | object->start_address + MXT_T100_YRANGE, |
| 1767 | sizeof(range_y), &range_y); |
| 1768 | if (error) |
| 1769 | return error; |
| 1770 | |
| 1771 | le16_to_cpus(&range_y); |
| 1772 | |
| 1773 | error = __mxt_read_reg(client, |
| 1774 | object->start_address + MXT_T100_CFG1, |
| 1775 | 1, &cfg); |
| 1776 | if (error) |
| 1777 | return error; |
| 1778 | |
| 1779 | error = __mxt_read_reg(client, |
| 1780 | object->start_address + MXT_T100_TCHAUX, |
| 1781 | 1, &tchaux); |
| 1782 | if (error) |
| 1783 | return error; |
| 1784 | |
| 1785 | /* Handle default values */ |
| 1786 | if (range_x == 0) |
| 1787 | range_x = 1023; |
| 1788 | |
| 1789 | if (range_y == 0) |
| 1790 | range_y = 1023; |
| 1791 | |
| 1792 | if (cfg & MXT_T100_CFG_SWITCHXY) { |
| 1793 | data->max_x = range_y; |
| 1794 | data->max_y = range_x; |
| 1795 | } else { |
| 1796 | data->max_x = range_x; |
| 1797 | data->max_y = range_y; |
| 1798 | } |
| 1799 | |
| 1800 | /* allocate aux bytes */ |
| 1801 | aux = 6; |
| 1802 | |
| 1803 | if (tchaux & MXT_T100_TCHAUX_VECT) |
| 1804 | data->t100_aux_vect = aux++; |
| 1805 | |
| 1806 | if (tchaux & MXT_T100_TCHAUX_AMPL) |
| 1807 | data->t100_aux_ampl = aux++; |
| 1808 | |
| 1809 | if (tchaux & MXT_T100_TCHAUX_AREA) |
| 1810 | data->t100_aux_area = aux++; |
| 1811 | |
| 1812 | dev_dbg(&client->dev, |
| 1813 | "T100 aux mappings vect:%u ampl:%u area:%u\n", |
| 1814 | data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area); |
| 1815 | |
| 1816 | dev_info(&client->dev, |
| 1817 | "T100 Touchscreen size X%uY%u\n", data->max_x, data->max_y); |
| 1818 | |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1822 | static int mxt_input_open(struct input_dev *dev); |
| 1823 | static void mxt_input_close(struct input_dev *dev); |
| 1824 | |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame^] | 1825 | static void mxt_set_up_as_touchpad(struct input_dev *input_dev, |
| 1826 | struct mxt_data *data) |
| 1827 | { |
| 1828 | const struct mxt_platform_data *pdata = data->pdata; |
| 1829 | int i; |
| 1830 | |
| 1831 | input_dev->name = "Atmel maXTouch Touchpad"; |
| 1832 | |
| 1833 | __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); |
| 1834 | |
| 1835 | input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM); |
| 1836 | input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM); |
| 1837 | input_abs_set_res(input_dev, ABS_MT_POSITION_X, |
| 1838 | MXT_PIXELS_PER_MM); |
| 1839 | input_abs_set_res(input_dev, ABS_MT_POSITION_Y, |
| 1840 | MXT_PIXELS_PER_MM); |
| 1841 | |
| 1842 | for (i = 0; i < pdata->t19_num_keys; i++) |
| 1843 | if (pdata->t19_keymap[i] != KEY_RESERVED) |
| 1844 | input_set_capability(input_dev, EV_KEY, |
| 1845 | pdata->t19_keymap[i]); |
| 1846 | } |
| 1847 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1848 | static int mxt_initialize_input_device(struct mxt_data *data) |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1849 | { |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1850 | const struct mxt_platform_data *pdata = data->pdata; |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame^] | 1851 | struct device *dev = &data->client->dev; |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1852 | struct input_dev *input_dev; |
| 1853 | int error; |
| 1854 | unsigned int num_mt_slots; |
| 1855 | unsigned int mt_flags = 0; |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1856 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1857 | switch (data->multitouch) { |
| 1858 | case MXT_TOUCH_MULTI_T9: |
| 1859 | num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1; |
| 1860 | error = mxt_read_t9_resolution(data); |
| 1861 | if (error) |
| 1862 | dev_warn(dev, "Failed to initialize T9 resolution\n"); |
| 1863 | break; |
| 1864 | |
| 1865 | case MXT_TOUCH_MULTITOUCHSCREEN_T100: |
| 1866 | num_mt_slots = data->num_touchids; |
| 1867 | error = mxt_read_t100_config(data); |
| 1868 | if (error) |
| 1869 | dev_warn(dev, "Failed to read T100 config\n"); |
| 1870 | break; |
| 1871 | |
| 1872 | default: |
| 1873 | dev_err(dev, "Invalid multitouch object\n"); |
| 1874 | return -EINVAL; |
| 1875 | } |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1876 | |
| 1877 | input_dev = input_allocate_device(); |
| 1878 | if (!input_dev) { |
| 1879 | dev_err(dev, "Failed to allocate memory\n"); |
| 1880 | return -ENOMEM; |
| 1881 | } |
| 1882 | |
| 1883 | input_dev->name = "Atmel maXTouch Touchscreen"; |
| 1884 | input_dev->phys = data->phys; |
| 1885 | input_dev->id.bustype = BUS_I2C; |
| 1886 | input_dev->dev.parent = dev; |
| 1887 | input_dev->open = mxt_input_open; |
| 1888 | input_dev->close = mxt_input_close; |
| 1889 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1890 | input_set_capability(input_dev, EV_KEY, BTN_TOUCH); |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1891 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1892 | /* For single touch */ |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1893 | input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0); |
| 1894 | input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0); |
| 1895 | |
| 1896 | if (data->multitouch == MXT_TOUCH_MULTI_T9 || |
| 1897 | (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 1898 | data->t100_aux_ampl)) { |
| 1899 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0); |
| 1900 | } |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1901 | |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame^] | 1902 | /* If device has buttons we assume it is a touchpad */ |
| 1903 | if (pdata->t19_num_keys) { |
| 1904 | mxt_set_up_as_touchpad(input_dev, data); |
| 1905 | mt_flags |= INPUT_MT_POINTER; |
| 1906 | } |
| 1907 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1908 | /* For multi touch */ |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1909 | error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags); |
| 1910 | if (error) { |
| 1911 | dev_err(dev, "Error %d initialising slots\n", error); |
| 1912 | goto err_free_mem; |
| 1913 | } |
| 1914 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1915 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) { |
| 1916 | input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE, |
| 1917 | 0, MT_TOOL_MAX, 0, 0); |
| 1918 | input_set_abs_params(input_dev, ABS_MT_DISTANCE, |
| 1919 | MXT_DISTANCE_ACTIVE_TOUCH, |
| 1920 | MXT_DISTANCE_HOVERING, |
| 1921 | 0, 0); |
| 1922 | } |
| 1923 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1924 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, |
| 1925 | 0, data->max_x, 0, 0); |
| 1926 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, |
| 1927 | 0, data->max_y, 0, 0); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1928 | |
| 1929 | if (data->multitouch == MXT_TOUCH_MULTI_T9 || |
| 1930 | (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 1931 | data->t100_aux_area)) { |
| 1932 | input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, |
| 1933 | 0, MXT_MAX_AREA, 0, 0); |
| 1934 | } |
| 1935 | |
| 1936 | if (data->multitouch == MXT_TOUCH_MULTI_T9 || |
| 1937 | (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 1938 | data->t100_aux_ampl)) { |
| 1939 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, |
| 1940 | 0, 255, 0, 0); |
| 1941 | } |
| 1942 | |
| 1943 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 1944 | data->t100_aux_vect) { |
| 1945 | input_set_abs_params(input_dev, ABS_MT_ORIENTATION, |
| 1946 | 0, 255, 0, 0); |
| 1947 | } |
| 1948 | |
| 1949 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 1950 | data->t100_aux_ampl) { |
| 1951 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, |
| 1952 | 0, 255, 0, 0); |
| 1953 | } |
| 1954 | |
| 1955 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 1956 | data->t100_aux_vect) { |
| 1957 | input_set_abs_params(input_dev, ABS_MT_ORIENTATION, |
| 1958 | 0, 255, 0, 0); |
| 1959 | } |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1960 | |
| 1961 | input_set_drvdata(input_dev, data); |
| 1962 | |
| 1963 | error = input_register_device(input_dev); |
| 1964 | if (error) { |
| 1965 | dev_err(dev, "Error %d registering input device\n", error); |
| 1966 | goto err_free_mem; |
| 1967 | } |
| 1968 | |
| 1969 | data->input_dev = input_dev; |
| 1970 | |
| 1971 | return 0; |
| 1972 | |
| 1973 | err_free_mem: |
| 1974 | input_free_device(input_dev); |
| 1975 | return error; |
| 1976 | } |
| 1977 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1978 | static int mxt_configure_objects(struct mxt_data *data, |
| 1979 | const struct firmware *cfg); |
| 1980 | |
| 1981 | static void mxt_config_cb(const struct firmware *cfg, void *ctx) |
| 1982 | { |
| 1983 | mxt_configure_objects(ctx, cfg); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1984 | release_firmware(cfg); |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 1987 | static int mxt_initialize(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1988 | { |
| 1989 | struct i2c_client *client = data->client; |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 1990 | int recovery_attempts = 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1991 | int error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1992 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 1993 | while (1) { |
| 1994 | error = mxt_get_info(data); |
| 1995 | if (!error) |
| 1996 | break; |
| 1997 | |
| 1998 | /* Check bootloader state */ |
| 1999 | error = mxt_probe_bootloader(data, false); |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2000 | if (error) { |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2001 | dev_info(&client->dev, "Trying alternate bootloader address\n"); |
| 2002 | error = mxt_probe_bootloader(data, true); |
| 2003 | if (error) { |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2004 | /* Chip is not in appmode or bootloader mode */ |
| 2005 | return error; |
| 2006 | } |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2007 | } |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2008 | |
| 2009 | /* OK, we are in bootloader, see if we can recover */ |
| 2010 | if (++recovery_attempts > 1) { |
| 2011 | dev_err(&client->dev, "Could not recover from bootloader mode\n"); |
| 2012 | /* |
| 2013 | * We can reflash from this state, so do not |
| 2014 | * abort initialization. |
| 2015 | */ |
| 2016 | data->in_bootloader = true; |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | /* Attempt to exit bootloader into app mode */ |
| 2021 | mxt_send_bootloader_cmd(data, false); |
| 2022 | msleep(MXT_FW_RESET_TIME); |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2023 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2024 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2025 | /* Get object table information */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2026 | error = mxt_get_object_table(data); |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 2027 | if (error) { |
| 2028 | dev_err(&client->dev, "Error %d reading object table\n", error); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2029 | return error; |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 2030 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2031 | |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 2032 | error = mxt_acquire_irq(data); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2033 | if (error) |
| 2034 | goto err_free_object_table; |
| 2035 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2036 | error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME, |
| 2037 | &client->dev, GFP_KERNEL, data, |
| 2038 | mxt_config_cb); |
| 2039 | if (error) { |
| 2040 | dev_err(&client->dev, "Failed to invoke firmware loader: %d\n", |
| 2041 | error); |
| 2042 | goto err_free_object_table; |
| 2043 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2044 | |
| 2045 | return 0; |
Daniel Kurtz | 7d4fa10 | 2012-06-28 21:08:19 +0800 | [diff] [blame] | 2046 | |
| 2047 | err_free_object_table: |
| 2048 | mxt_free_object_table(data); |
| 2049 | return error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2052 | static int mxt_configure_objects(struct mxt_data *data, |
| 2053 | const struct firmware *cfg) |
| 2054 | { |
| 2055 | struct device *dev = &data->client->dev; |
| 2056 | struct mxt_info *info = &data->info; |
| 2057 | int error; |
| 2058 | |
| 2059 | if (cfg) { |
| 2060 | error = mxt_update_cfg(data, cfg); |
| 2061 | if (error) |
| 2062 | dev_warn(dev, "Error %d updating config\n", error); |
| 2063 | } |
| 2064 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 2065 | if (data->multitouch) { |
| 2066 | error = mxt_initialize_input_device(data); |
| 2067 | if (error) |
| 2068 | return error; |
| 2069 | } else { |
| 2070 | dev_warn(dev, "No touch object detected\n"); |
| 2071 | } |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2072 | |
| 2073 | dev_info(dev, |
| 2074 | "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n", |
| 2075 | info->family_id, info->variant_id, info->version >> 4, |
| 2076 | info->version & 0xf, info->build, info->object_num); |
| 2077 | |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2081 | /* Firmware Version is returned as Major.Minor.Build */ |
| 2082 | static ssize_t mxt_fw_version_show(struct device *dev, |
| 2083 | struct device_attribute *attr, char *buf) |
| 2084 | { |
| 2085 | struct mxt_data *data = dev_get_drvdata(dev); |
| 2086 | struct mxt_info *info = &data->info; |
| 2087 | return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n", |
| 2088 | info->version >> 4, info->version & 0xf, info->build); |
| 2089 | } |
| 2090 | |
| 2091 | /* Hardware Version is returned as FamilyID.VariantID */ |
| 2092 | static ssize_t mxt_hw_version_show(struct device *dev, |
| 2093 | struct device_attribute *attr, char *buf) |
| 2094 | { |
| 2095 | struct mxt_data *data = dev_get_drvdata(dev); |
| 2096 | struct mxt_info *info = &data->info; |
| 2097 | return scnprintf(buf, PAGE_SIZE, "%u.%u\n", |
| 2098 | info->family_id, info->variant_id); |
| 2099 | } |
| 2100 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2101 | static ssize_t mxt_show_instance(char *buf, int count, |
| 2102 | struct mxt_object *object, int instance, |
| 2103 | const u8 *val) |
| 2104 | { |
| 2105 | int i; |
| 2106 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 2107 | if (mxt_obj_instances(object) > 1) |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2108 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 2109 | "Instance %u\n", instance); |
| 2110 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 2111 | for (i = 0; i < mxt_obj_size(object); i++) |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2112 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 2113 | "\t[%2u]: %02x (%d)\n", i, val[i], val[i]); |
| 2114 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 2115 | |
| 2116 | return count; |
| 2117 | } |
| 2118 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2119 | static ssize_t mxt_object_show(struct device *dev, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2120 | struct device_attribute *attr, char *buf) |
| 2121 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2122 | struct mxt_data *data = dev_get_drvdata(dev); |
| 2123 | struct mxt_object *object; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2124 | int count = 0; |
| 2125 | int i, j; |
| 2126 | int error; |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2127 | u8 *obuf; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2128 | |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2129 | /* Pre-allocate buffer large enough to hold max sized object. */ |
| 2130 | obuf = kmalloc(256, GFP_KERNEL); |
| 2131 | if (!obuf) |
| 2132 | return -ENOMEM; |
| 2133 | |
| 2134 | error = 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2135 | for (i = 0; i < data->info.object_num; i++) { |
| 2136 | object = data->object_table + i; |
| 2137 | |
Daniel Kurtz | 9163095 | 2012-06-28 21:08:09 +0800 | [diff] [blame] | 2138 | if (!mxt_object_readable(object->type)) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2139 | continue; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2140 | |
Daniel Kurtz | 9163095 | 2012-06-28 21:08:09 +0800 | [diff] [blame] | 2141 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 2142 | "T%u:\n", object->type); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2143 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 2144 | for (j = 0; j < mxt_obj_instances(object); j++) { |
| 2145 | u16 size = mxt_obj_size(object); |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2146 | u16 addr = object->start_address + j * size; |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2147 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2148 | error = __mxt_read_reg(data->client, addr, size, obuf); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2149 | if (error) |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2150 | goto done; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2151 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2152 | count = mxt_show_instance(buf, count, object, j, obuf); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2153 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2156 | done: |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2157 | kfree(obuf); |
| 2158 | return error ?: count; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Nick Dyer | f2ac6cb | 2014-05-18 23:15:01 -0700 | [diff] [blame] | 2161 | static int mxt_check_firmware_format(struct device *dev, |
| 2162 | const struct firmware *fw) |
| 2163 | { |
| 2164 | unsigned int pos = 0; |
| 2165 | char c; |
| 2166 | |
| 2167 | while (pos < fw->size) { |
| 2168 | c = *(fw->data + pos); |
| 2169 | |
| 2170 | if (c < '0' || (c > '9' && c < 'A') || c > 'F') |
| 2171 | return 0; |
| 2172 | |
| 2173 | pos++; |
| 2174 | } |
| 2175 | |
| 2176 | /* |
| 2177 | * To convert file try: |
| 2178 | * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw |
| 2179 | */ |
| 2180 | dev_err(dev, "Aborting: firmware file must be in binary format\n"); |
| 2181 | |
| 2182 | return -EINVAL; |
| 2183 | } |
| 2184 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2185 | static int mxt_load_fw(struct device *dev, const char *fn) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2186 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2187 | struct mxt_data *data = dev_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2188 | const struct firmware *fw = NULL; |
| 2189 | unsigned int frame_size; |
| 2190 | unsigned int pos = 0; |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2191 | unsigned int retry = 0; |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2192 | unsigned int frame = 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2193 | int ret; |
| 2194 | |
| 2195 | ret = request_firmware(&fw, fn, dev); |
| 2196 | if (ret) { |
| 2197 | dev_err(dev, "Unable to open firmware %s\n", fn); |
| 2198 | return ret; |
| 2199 | } |
| 2200 | |
Nick Dyer | f2ac6cb | 2014-05-18 23:15:01 -0700 | [diff] [blame] | 2201 | /* Check for incorrect enc file */ |
| 2202 | ret = mxt_check_firmware_format(dev, fw); |
| 2203 | if (ret) |
| 2204 | goto release_firmware; |
| 2205 | |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2206 | if (!data->in_bootloader) { |
| 2207 | /* Change to the bootloader mode */ |
| 2208 | data->in_bootloader = true; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2209 | |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2210 | ret = mxt_t6_command(data, MXT_COMMAND_RESET, |
| 2211 | MXT_BOOT_VALUE, false); |
| 2212 | if (ret) |
| 2213 | goto release_firmware; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2214 | |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2215 | msleep(MXT_RESET_TIME); |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2216 | |
| 2217 | /* Do not need to scan since we know family ID */ |
| 2218 | ret = mxt_lookup_bootloader_address(data, 0); |
| 2219 | if (ret) |
| 2220 | goto release_firmware; |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 2221 | |
| 2222 | mxt_free_input_device(data); |
| 2223 | mxt_free_object_table(data); |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2224 | } else { |
| 2225 | enable_irq(data->irq); |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2226 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2227 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2228 | reinit_completion(&data->bl_completion); |
| 2229 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2230 | ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false); |
| 2231 | if (ret) { |
| 2232 | /* Bootloader may still be unlocked from previous attempt */ |
| 2233 | ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false); |
| 2234 | if (ret) |
| 2235 | goto disable_irq; |
| 2236 | } else { |
| 2237 | dev_info(dev, "Unlocking bootloader\n"); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2238 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2239 | /* Unlock bootloader */ |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2240 | ret = mxt_send_bootloader_cmd(data, true); |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2241 | if (ret) |
| 2242 | goto disable_irq; |
| 2243 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2244 | |
| 2245 | while (pos < fw->size) { |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2246 | ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2247 | if (ret) |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2248 | goto disable_irq; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2249 | |
| 2250 | frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1)); |
| 2251 | |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2252 | /* Take account of CRC bytes */ |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2253 | frame_size += 2; |
| 2254 | |
| 2255 | /* Write one frame to device */ |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 2256 | ret = mxt_bootloader_write(data, fw->data + pos, frame_size); |
| 2257 | if (ret) |
| 2258 | goto disable_irq; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2259 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2260 | ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true); |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2261 | if (ret) { |
| 2262 | retry++; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2263 | |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2264 | /* Back off by 20ms per retry */ |
| 2265 | msleep(retry * 20); |
| 2266 | |
| 2267 | if (retry > 20) { |
| 2268 | dev_err(dev, "Retry count exceeded\n"); |
| 2269 | goto disable_irq; |
| 2270 | } |
| 2271 | } else { |
| 2272 | retry = 0; |
| 2273 | pos += frame_size; |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2274 | frame++; |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2275 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2276 | |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2277 | if (frame % 50 == 0) |
| 2278 | dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n", |
| 2279 | frame, pos, fw->size); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2282 | /* Wait for flash. */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2283 | ret = mxt_wait_for_completion(data, &data->bl_completion, |
| 2284 | MXT_FW_RESET_TIME); |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2285 | if (ret) |
| 2286 | goto disable_irq; |
| 2287 | |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2288 | dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos); |
| 2289 | |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2290 | /* |
| 2291 | * Wait for device to reset. Some bootloader versions do not assert |
| 2292 | * the CHG line after bootloading has finished, so ignore potential |
| 2293 | * errors. |
| 2294 | */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2295 | mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME); |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2296 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2297 | data->in_bootloader = false; |
| 2298 | |
| 2299 | disable_irq: |
| 2300 | disable_irq(data->irq); |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2301 | release_firmware: |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2302 | release_firmware(fw); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2303 | return ret; |
| 2304 | } |
| 2305 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2306 | static ssize_t mxt_update_fw_store(struct device *dev, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2307 | struct device_attribute *attr, |
| 2308 | const char *buf, size_t count) |
| 2309 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2310 | struct mxt_data *data = dev_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2311 | int error; |
| 2312 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2313 | error = mxt_load_fw(dev, MXT_FW_NAME); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2314 | if (error) { |
| 2315 | dev_err(dev, "The firmware update failed(%d)\n", error); |
| 2316 | count = error; |
| 2317 | } else { |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 2318 | dev_info(dev, "The firmware update succeeded\n"); |
| 2319 | |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2320 | error = mxt_initialize(data); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2321 | if (error) |
| 2322 | return error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2325 | return count; |
| 2326 | } |
| 2327 | |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2328 | static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL); |
| 2329 | static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL); |
Daniel Kurtz | 71b3e93 | 2012-05-08 22:30:14 -0700 | [diff] [blame] | 2330 | static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL); |
| 2331 | static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2332 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2333 | static struct attribute *mxt_attrs[] = { |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2334 | &dev_attr_fw_version.attr, |
| 2335 | &dev_attr_hw_version.attr, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2336 | &dev_attr_object.attr, |
| 2337 | &dev_attr_update_fw.attr, |
| 2338 | NULL |
| 2339 | }; |
| 2340 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2341 | static const struct attribute_group mxt_attr_group = { |
| 2342 | .attrs = mxt_attrs, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2343 | }; |
| 2344 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2345 | static void mxt_start(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2346 | { |
Linus Torvalds | 7f40548 | 2014-12-31 12:59:34 -0800 | [diff] [blame] | 2347 | /* Touch enable */ |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 2348 | mxt_write_object(data, data->multitouch, MXT_TOUCH_CTRL, 0x83); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2351 | static void mxt_stop(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2352 | { |
Linus Torvalds | 7f40548 | 2014-12-31 12:59:34 -0800 | [diff] [blame] | 2353 | /* Touch disable */ |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 2354 | mxt_write_object(data, data->multitouch, MXT_TOUCH_CTRL, 0); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2357 | static int mxt_input_open(struct input_dev *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2358 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2359 | struct mxt_data *data = input_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2360 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2361 | mxt_start(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2362 | |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2366 | static void mxt_input_close(struct input_dev *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2367 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2368 | struct mxt_data *data = input_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2369 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2370 | mxt_stop(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2373 | #ifdef CONFIG_OF |
| 2374 | static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client) |
| 2375 | { |
| 2376 | struct mxt_platform_data *pdata; |
| 2377 | u32 *keymap; |
| 2378 | u32 keycode; |
| 2379 | int proplen, i, ret; |
| 2380 | |
| 2381 | if (!client->dev.of_node) |
| 2382 | return ERR_PTR(-ENODEV); |
| 2383 | |
| 2384 | pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); |
| 2385 | if (!pdata) |
| 2386 | return ERR_PTR(-ENOMEM); |
| 2387 | |
| 2388 | if (of_find_property(client->dev.of_node, "linux,gpio-keymap", |
| 2389 | &proplen)) { |
| 2390 | pdata->t19_num_keys = proplen / sizeof(u32); |
| 2391 | |
| 2392 | keymap = devm_kzalloc(&client->dev, |
| 2393 | pdata->t19_num_keys * sizeof(keymap[0]), |
| 2394 | GFP_KERNEL); |
| 2395 | if (!keymap) |
| 2396 | return ERR_PTR(-ENOMEM); |
| 2397 | |
| 2398 | for (i = 0; i < pdata->t19_num_keys; i++) { |
| 2399 | ret = of_property_read_u32_index(client->dev.of_node, |
| 2400 | "linux,gpio-keymap", i, &keycode); |
| 2401 | if (ret) |
| 2402 | keycode = KEY_RESERVED; |
| 2403 | |
| 2404 | keymap[i] = keycode; |
| 2405 | } |
| 2406 | |
| 2407 | pdata->t19_keymap = keymap; |
| 2408 | } |
| 2409 | |
| 2410 | return pdata; |
| 2411 | } |
| 2412 | #else |
| 2413 | static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client) |
| 2414 | { |
| 2415 | dev_dbg(&client->dev, "No platform data specified\n"); |
| 2416 | return ERR_PTR(-EINVAL); |
| 2417 | } |
| 2418 | #endif |
| 2419 | |
| 2420 | static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2421 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2422 | struct mxt_data *data; |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2423 | const struct mxt_platform_data *pdata; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2424 | int error; |
| 2425 | |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2426 | pdata = dev_get_platdata(&client->dev); |
| 2427 | if (!pdata) { |
| 2428 | pdata = mxt_parse_dt(client); |
| 2429 | if (IS_ERR(pdata)) |
| 2430 | return PTR_ERR(pdata); |
| 2431 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2432 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2433 | data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL); |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2434 | if (!data) { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2435 | dev_err(&client->dev, "Failed to allocate memory\n"); |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2436 | return -ENOMEM; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
Daniel Kurtz | ec02ac2 | 2012-06-28 21:08:02 +0800 | [diff] [blame] | 2439 | snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0", |
| 2440 | client->adapter->nr, client->addr); |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 2441 | |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 2442 | data->client = client; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 2443 | data->pdata = pdata; |
| 2444 | data->irq = client->irq; |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2445 | i2c_set_clientdata(client, data); |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 2446 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2447 | init_completion(&data->bl_completion); |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2448 | init_completion(&data->reset_completion); |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 2449 | init_completion(&data->crc_completion); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2450 | |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2451 | error = request_threaded_irq(client->irq, NULL, mxt_interrupt, |
| 2452 | pdata->irqflags | IRQF_ONESHOT, |
| 2453 | client->name, data); |
| 2454 | if (error) { |
| 2455 | dev_err(&client->dev, "Failed to register interrupt\n"); |
| 2456 | goto err_free_mem; |
| 2457 | } |
| 2458 | |
| 2459 | disable_irq(client->irq); |
| 2460 | |
Daniel Kurtz | cb15911 | 2012-06-28 21:08:22 +0800 | [diff] [blame] | 2461 | error = mxt_initialize(data); |
| 2462 | if (error) |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2463 | goto err_free_irq; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2464 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2465 | error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group); |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 2466 | if (error) { |
| 2467 | dev_err(&client->dev, "Failure %d creating sysfs group\n", |
| 2468 | error); |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2469 | goto err_free_object; |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 2470 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2471 | |
| 2472 | return 0; |
| 2473 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2474 | err_free_object: |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 2475 | mxt_free_input_device(data); |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 2476 | mxt_free_object_table(data); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2477 | err_free_irq: |
| 2478 | free_irq(client->irq, data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2479 | err_free_mem: |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2480 | kfree(data); |
| 2481 | return error; |
| 2482 | } |
| 2483 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 2484 | static int mxt_remove(struct i2c_client *client) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2485 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2486 | struct mxt_data *data = i2c_get_clientdata(client); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2487 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2488 | sysfs_remove_group(&client->dev.kobj, &mxt_attr_group); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2489 | free_irq(data->irq, data); |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 2490 | mxt_free_input_device(data); |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 2491 | mxt_free_object_table(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2492 | kfree(data); |
| 2493 | |
| 2494 | return 0; |
| 2495 | } |
| 2496 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 2497 | static int __maybe_unused mxt_suspend(struct device *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2498 | { |
Dmitry Torokhov | 8b5fce0 | 2010-11-18 00:14:03 -0800 | [diff] [blame] | 2499 | struct i2c_client *client = to_i2c_client(dev); |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2500 | struct mxt_data *data = i2c_get_clientdata(client); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2501 | struct input_dev *input_dev = data->input_dev; |
| 2502 | |
| 2503 | mutex_lock(&input_dev->mutex); |
| 2504 | |
| 2505 | if (input_dev->users) |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2506 | mxt_stop(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2507 | |
| 2508 | mutex_unlock(&input_dev->mutex); |
| 2509 | |
| 2510 | return 0; |
| 2511 | } |
| 2512 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 2513 | static int __maybe_unused mxt_resume(struct device *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2514 | { |
Dmitry Torokhov | 8b5fce0 | 2010-11-18 00:14:03 -0800 | [diff] [blame] | 2515 | struct i2c_client *client = to_i2c_client(dev); |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2516 | struct mxt_data *data = i2c_get_clientdata(client); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2517 | struct input_dev *input_dev = data->input_dev; |
| 2518 | |
Linus Torvalds | 7f40548 | 2014-12-31 12:59:34 -0800 | [diff] [blame] | 2519 | mxt_soft_reset(data); |
| 2520 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2521 | mutex_lock(&input_dev->mutex); |
| 2522 | |
| 2523 | if (input_dev->users) |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2524 | mxt_start(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2525 | |
| 2526 | mutex_unlock(&input_dev->mutex); |
| 2527 | |
| 2528 | return 0; |
| 2529 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2530 | |
Daniel Kurtz | 3a73c81 | 2012-05-08 22:29:14 -0700 | [diff] [blame] | 2531 | static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume); |
| 2532 | |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2533 | static const struct of_device_id mxt_of_match[] = { |
| 2534 | { .compatible = "atmel,maxtouch", }, |
| 2535 | {}, |
| 2536 | }; |
| 2537 | MODULE_DEVICE_TABLE(of, mxt_of_match); |
| 2538 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2539 | static const struct i2c_device_id mxt_id[] = { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2540 | { "qt602240_ts", 0 }, |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2541 | { "atmel_mxt_ts", 0 }, |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 2542 | { "atmel_mxt_tp", 0 }, |
Chris Leech | 46ee2a0 | 2011-02-15 13:36:52 -0800 | [diff] [blame] | 2543 | { "mXT224", 0 }, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2544 | { } |
| 2545 | }; |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2546 | MODULE_DEVICE_TABLE(i2c, mxt_id); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2547 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2548 | static struct i2c_driver mxt_driver = { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2549 | .driver = { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2550 | .name = "atmel_mxt_ts", |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2551 | .owner = THIS_MODULE, |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2552 | .of_match_table = of_match_ptr(mxt_of_match), |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2553 | .pm = &mxt_pm_ops, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2554 | }, |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2555 | .probe = mxt_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 2556 | .remove = mxt_remove, |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2557 | .id_table = mxt_id, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2558 | }; |
| 2559 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 2560 | module_i2c_driver(mxt_driver); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2561 | |
| 2562 | /* Module information */ |
| 2563 | MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2564 | MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver"); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2565 | MODULE_LICENSE("GPL"); |