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. |
Nick Dyer | f3c4a8f | 2016-09-15 17:30:43 -0300 | [diff] [blame] | 7 | * Copyright (C) 2016 Zodiac Inflight Innovations |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 8 | * |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 9 | * Author: Joonyoung Shim <jy0922.shim@samsung.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2 of the License, or (at your |
| 14 | * option) any later version. |
| 15 | * |
| 16 | */ |
| 17 | |
Dmitry Torokhov | 4f8d808 | 2015-04-07 16:30:01 -0700 | [diff] [blame] | 18 | #include <linux/acpi.h> |
| 19 | #include <linux/dmi.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/completion.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/firmware.h> |
| 25 | #include <linux/i2c.h> |
Joonyoung Shim | 8b86c1c | 2011-04-12 23:18:59 -0700 | [diff] [blame] | 26 | #include <linux/input/mt.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 28 | #include <linux/of.h> |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 29 | #include <linux/property.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 30 | #include <linux/slab.h> |
Sebastian Reichel | f657b00 | 2017-07-25 14:11:34 -0700 | [diff] [blame] | 31 | #include <linux/gpio/consumer.h> |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 32 | #include <linux/property.h> |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 33 | #include <asm/unaligned.h> |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 34 | #include <media/v4l2-device.h> |
| 35 | #include <media/v4l2-ioctl.h> |
| 36 | #include <media/videobuf2-v4l2.h> |
| 37 | #include <media/videobuf2-vmalloc.h> |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 38 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 39 | /* Firmware files */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 40 | #define MXT_FW_NAME "maxtouch.fw" |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 41 | #define MXT_CFG_NAME "maxtouch.cfg" |
| 42 | #define MXT_CFG_MAGIC "OBP_RAW V1" |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 43 | |
| 44 | /* Registers */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 45 | #define MXT_OBJECT_START 0x07 |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 46 | #define MXT_OBJECT_SIZE 6 |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 47 | #define MXT_INFO_CHECKSUM_SIZE 3 |
| 48 | #define MXT_MAX_BLOCK_WRITE 256 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 49 | |
| 50 | /* Object types */ |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 51 | #define MXT_DEBUG_DIAGNOSTIC_T37 37 |
| 52 | #define MXT_GEN_MESSAGE_T5 5 |
| 53 | #define MXT_GEN_COMMAND_T6 6 |
| 54 | #define MXT_GEN_POWER_T7 7 |
| 55 | #define MXT_GEN_ACQUIRE_T8 8 |
| 56 | #define MXT_GEN_DATASOURCE_T53 53 |
| 57 | #define MXT_TOUCH_MULTI_T9 9 |
| 58 | #define MXT_TOUCH_KEYARRAY_T15 15 |
| 59 | #define MXT_TOUCH_PROXIMITY_T23 23 |
| 60 | #define MXT_TOUCH_PROXKEY_T52 52 |
| 61 | #define MXT_PROCI_GRIPFACE_T20 20 |
| 62 | #define MXT_PROCG_NOISE_T22 22 |
| 63 | #define MXT_PROCI_ONETOUCH_T24 24 |
| 64 | #define MXT_PROCI_TWOTOUCH_T27 27 |
| 65 | #define MXT_PROCI_GRIP_T40 40 |
| 66 | #define MXT_PROCI_PALM_T41 41 |
| 67 | #define MXT_PROCI_TOUCHSUPPRESSION_T42 42 |
| 68 | #define MXT_PROCI_STYLUS_T47 47 |
| 69 | #define MXT_PROCG_NOISESUPPRESSION_T48 48 |
| 70 | #define MXT_SPT_COMMSCONFIG_T18 18 |
| 71 | #define MXT_SPT_GPIOPWM_T19 19 |
| 72 | #define MXT_SPT_SELFTEST_T25 25 |
| 73 | #define MXT_SPT_CTECONFIG_T28 28 |
| 74 | #define MXT_SPT_USERDATA_T38 38 |
| 75 | #define MXT_SPT_DIGITIZER_T43 43 |
| 76 | #define MXT_SPT_MESSAGECOUNT_T44 44 |
| 77 | #define MXT_SPT_CTECONFIG_T46 46 |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 78 | #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 79 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 80 | /* MXT_GEN_MESSAGE_T5 object */ |
| 81 | #define MXT_RPTID_NOMSG 0xff |
| 82 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 83 | /* MXT_GEN_COMMAND_T6 field */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 84 | #define MXT_COMMAND_RESET 0 |
| 85 | #define MXT_COMMAND_BACKUPNV 1 |
| 86 | #define MXT_COMMAND_CALIBRATE 2 |
| 87 | #define MXT_COMMAND_REPORTALL 3 |
| 88 | #define MXT_COMMAND_DIAGNOSTIC 5 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 89 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 90 | /* Define for T6 status byte */ |
Nick Dyer | 204b4ea | 2018-07-27 11:44:41 -0700 | [diff] [blame^] | 91 | #define MXT_T6_STATUS_RESET BIT(7) |
| 92 | #define MXT_T6_STATUS_OFL BIT(6) |
| 93 | #define MXT_T6_STATUS_SIGERR BIT(5) |
| 94 | #define MXT_T6_STATUS_CAL BIT(4) |
| 95 | #define MXT_T6_STATUS_CFGERR BIT(3) |
| 96 | #define MXT_T6_STATUS_COMSERR BIT(2) |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 97 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 98 | /* MXT_GEN_POWER_T7 field */ |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 99 | struct t7_config { |
| 100 | u8 idle; |
| 101 | u8 active; |
| 102 | } __packed; |
| 103 | |
| 104 | #define MXT_POWER_CFG_RUN 0 |
| 105 | #define MXT_POWER_CFG_DEEPSLEEP 1 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 106 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 107 | /* MXT_TOUCH_MULTI_T9 field */ |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 108 | #define MXT_T9_CTRL 0 |
Nick Dyer | 2786489 | 2016-07-18 18:10:33 -0300 | [diff] [blame] | 109 | #define MXT_T9_XSIZE 3 |
| 110 | #define MXT_T9_YSIZE 4 |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 111 | #define MXT_T9_ORIENT 9 |
| 112 | #define MXT_T9_RANGE 18 |
| 113 | |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 114 | /* MXT_TOUCH_MULTI_T9 status */ |
Nick Dyer | 204b4ea | 2018-07-27 11:44:41 -0700 | [diff] [blame^] | 115 | #define MXT_T9_UNGRIP BIT(0) |
| 116 | #define MXT_T9_SUPPRESS BIT(1) |
| 117 | #define MXT_T9_AMP BIT(2) |
| 118 | #define MXT_T9_VECTOR BIT(3) |
| 119 | #define MXT_T9_MOVE BIT(4) |
| 120 | #define MXT_T9_RELEASE BIT(5) |
| 121 | #define MXT_T9_PRESS BIT(6) |
| 122 | #define MXT_T9_DETECT BIT(7) |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 123 | |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 124 | struct t9_range { |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 125 | __le16 x; |
| 126 | __le16 y; |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 127 | } __packed; |
| 128 | |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 129 | /* MXT_TOUCH_MULTI_T9 orient */ |
Nick Dyer | 204b4ea | 2018-07-27 11:44:41 -0700 | [diff] [blame^] | 130 | #define MXT_T9_ORIENT_SWITCH BIT(0) |
| 131 | #define MXT_T9_ORIENT_INVERTX BIT(1) |
| 132 | #define MXT_T9_ORIENT_INVERTY BIT(2) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 133 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 134 | /* MXT_SPT_COMMSCONFIG_T18 */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 135 | #define MXT_COMMS_CTRL 0 |
| 136 | #define MXT_COMMS_CMD 1 |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 137 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 138 | /* MXT_DEBUG_DIAGNOSTIC_T37 */ |
| 139 | #define MXT_DIAGNOSTIC_PAGEUP 0x01 |
| 140 | #define MXT_DIAGNOSTIC_DELTAS 0x10 |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 141 | #define MXT_DIAGNOSTIC_REFS 0x11 |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 142 | #define MXT_DIAGNOSTIC_SIZE 128 |
| 143 | |
Nick Dyer | 566d533 | 2016-07-18 18:10:35 -0300 | [diff] [blame] | 144 | #define MXT_FAMILY_1386 160 |
| 145 | #define MXT1386_COLUMNS 3 |
| 146 | #define MXT1386_PAGES_PER_COLUMN 8 |
| 147 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 148 | struct t37_debug { |
| 149 | #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37 |
| 150 | u8 mode; |
| 151 | u8 page; |
| 152 | u8 data[MXT_DIAGNOSTIC_SIZE]; |
| 153 | #endif |
| 154 | }; |
| 155 | |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 156 | /* Define for MXT_GEN_COMMAND_T6 */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 157 | #define MXT_BOOT_VALUE 0xa5 |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 158 | #define MXT_RESET_VALUE 0x01 |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 159 | #define MXT_BACKUP_VALUE 0x55 |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 160 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 161 | /* T100 Multiple Touch Touchscreen */ |
| 162 | #define MXT_T100_CTRL 0 |
| 163 | #define MXT_T100_CFG1 1 |
| 164 | #define MXT_T100_TCHAUX 3 |
Nick Dyer | 2786489 | 2016-07-18 18:10:33 -0300 | [diff] [blame] | 165 | #define MXT_T100_XSIZE 9 |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 166 | #define MXT_T100_XRANGE 13 |
Nick Dyer | 2786489 | 2016-07-18 18:10:33 -0300 | [diff] [blame] | 167 | #define MXT_T100_YSIZE 20 |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 168 | #define MXT_T100_YRANGE 24 |
| 169 | |
| 170 | #define MXT_T100_CFG_SWITCHXY BIT(5) |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 171 | #define MXT_T100_CFG_INVERTY BIT(6) |
| 172 | #define MXT_T100_CFG_INVERTX BIT(7) |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 173 | |
| 174 | #define MXT_T100_TCHAUX_VECT BIT(0) |
| 175 | #define MXT_T100_TCHAUX_AMPL BIT(1) |
| 176 | #define MXT_T100_TCHAUX_AREA BIT(2) |
| 177 | |
| 178 | #define MXT_T100_DETECT BIT(7) |
| 179 | #define MXT_T100_TYPE_MASK 0x70 |
| 180 | |
| 181 | enum t100_type { |
| 182 | MXT_T100_TYPE_FINGER = 1, |
| 183 | MXT_T100_TYPE_PASSIVE_STYLUS = 2, |
| 184 | MXT_T100_TYPE_HOVERING_FINGER = 4, |
| 185 | MXT_T100_TYPE_GLOVE = 5, |
| 186 | MXT_T100_TYPE_LARGE_TOUCH = 6, |
| 187 | }; |
| 188 | |
| 189 | #define MXT_DISTANCE_ACTIVE_TOUCH 0 |
| 190 | #define MXT_DISTANCE_HOVERING 1 |
| 191 | |
| 192 | #define MXT_TOUCH_MAJOR_DEFAULT 1 |
| 193 | #define MXT_PRESSURE_DEFAULT 1 |
| 194 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 195 | /* Delay times */ |
Linus Torvalds | 8343bce | 2013-03-09 10:31:01 -0800 | [diff] [blame] | 196 | #define MXT_BACKUP_TIME 50 /* msec */ |
Sebastian Reichel | ca1cd36 | 2018-05-24 09:33:36 -0700 | [diff] [blame] | 197 | #define MXT_RESET_GPIO_TIME 20 /* msec */ |
| 198 | #define MXT_RESET_INVALID_CHG 100 /* msec */ |
Linus Torvalds | 8343bce | 2013-03-09 10:31:01 -0800 | [diff] [blame] | 199 | #define MXT_RESET_TIME 200 /* msec */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 200 | #define MXT_RESET_TIMEOUT 3000 /* msec */ |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 201 | #define MXT_CRC_TIMEOUT 1000 /* msec */ |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 202 | #define MXT_FW_RESET_TIME 3000 /* msec */ |
| 203 | #define MXT_FW_CHG_TIMEOUT 300 /* msec */ |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 204 | |
| 205 | /* Command to unlock bootloader */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 206 | #define MXT_UNLOCK_CMD_MSB 0xaa |
| 207 | #define MXT_UNLOCK_CMD_LSB 0xdc |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 208 | |
| 209 | /* Bootloader mode status */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 210 | #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */ |
| 211 | #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */ |
| 212 | #define MXT_FRAME_CRC_CHECK 0x02 |
| 213 | #define MXT_FRAME_CRC_FAIL 0x03 |
| 214 | #define MXT_FRAME_CRC_PASS 0x04 |
| 215 | #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */ |
| 216 | #define MXT_BOOT_STATUS_MASK 0x3f |
Nick Dyer | 204b4ea | 2018-07-27 11:44:41 -0700 | [diff] [blame^] | 217 | #define MXT_BOOT_EXTENDED_ID BIT(5) |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 218 | #define MXT_BOOT_ID_MASK 0x1f |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 219 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 220 | /* Touchscreen absolute values */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 221 | #define MXT_MAX_AREA 0xff |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 222 | |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 223 | #define MXT_PIXELS_PER_MM 20 |
| 224 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 225 | struct mxt_info { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 226 | u8 family_id; |
| 227 | u8 variant_id; |
| 228 | u8 version; |
| 229 | u8 build; |
| 230 | u8 matrix_xsize; |
| 231 | u8 matrix_ysize; |
| 232 | u8 object_num; |
| 233 | }; |
| 234 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 235 | struct mxt_object { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 236 | u8 type; |
| 237 | u16 start_address; |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 238 | u8 size_minus_one; |
| 239 | u8 instances_minus_one; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 240 | u8 num_report_ids; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 241 | } __packed; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 242 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 243 | struct mxt_dbg { |
| 244 | u16 t37_address; |
| 245 | u16 diag_cmd_address; |
| 246 | struct t37_debug *t37_buf; |
| 247 | unsigned int t37_pages; |
| 248 | unsigned int t37_nodes; |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 249 | |
| 250 | struct v4l2_device v4l2; |
| 251 | struct v4l2_pix_format format; |
| 252 | struct video_device vdev; |
| 253 | struct vb2_queue queue; |
| 254 | struct mutex lock; |
| 255 | int input; |
| 256 | }; |
| 257 | |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 258 | enum v4l_dbg_inputs { |
| 259 | MXT_V4L_INPUT_DELTAS, |
| 260 | MXT_V4L_INPUT_REFS, |
| 261 | MXT_V4L_INPUT_MAX, |
| 262 | }; |
| 263 | |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 264 | static const struct v4l2_file_operations mxt_video_fops = { |
| 265 | .owner = THIS_MODULE, |
| 266 | .open = v4l2_fh_open, |
| 267 | .release = vb2_fop_release, |
| 268 | .unlocked_ioctl = video_ioctl2, |
| 269 | .read = vb2_fop_read, |
| 270 | .mmap = vb2_fop_mmap, |
| 271 | .poll = vb2_fop_poll, |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 272 | }; |
| 273 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 274 | enum mxt_suspend_mode { |
| 275 | MXT_SUSPEND_DEEP_SLEEP = 0, |
| 276 | MXT_SUSPEND_T9_CTRL = 1, |
| 277 | }; |
| 278 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 279 | /* Each client has this additional data */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 280 | struct mxt_data { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 281 | struct i2c_client *client; |
| 282 | struct input_dev *input_dev; |
Daniel Kurtz | ec02ac2 | 2012-06-28 21:08:02 +0800 | [diff] [blame] | 283 | char phys[64]; /* device physical location */ |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 284 | struct mxt_object *object_table; |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 285 | struct mxt_info *info; |
| 286 | void *raw_info_block; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 287 | unsigned int irq; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 288 | unsigned int max_x; |
| 289 | unsigned int max_y; |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 290 | bool invertx; |
| 291 | bool inverty; |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 292 | bool xy_switch; |
Nick Dyer | 2786489 | 2016-07-18 18:10:33 -0300 | [diff] [blame] | 293 | u8 xsize; |
| 294 | u8 ysize; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 295 | bool in_bootloader; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 296 | u16 mem_size; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 297 | u8 t100_aux_ampl; |
| 298 | u8 t100_aux_area; |
| 299 | u8 t100_aux_vect; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 300 | u8 max_reportid; |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 301 | u32 config_crc; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 302 | u32 info_crc; |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 303 | u8 bootloader_addr; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 304 | u8 *msg_buf; |
Nick Dyer | 497767d | 2014-07-23 12:47:50 -0700 | [diff] [blame] | 305 | u8 t6_status; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 306 | bool update_input; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 307 | u8 last_message_count; |
| 308 | u8 num_touchids; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 309 | u8 multitouch; |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 310 | struct t7_config t7_cfg; |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 311 | struct mxt_dbg dbg; |
Sebastian Reichel | f657b00 | 2017-07-25 14:11:34 -0700 | [diff] [blame] | 312 | struct gpio_desc *reset_gpio; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 313 | |
| 314 | /* Cached parameters from object table */ |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 315 | u16 T5_address; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 316 | u8 T5_msg_size; |
Daniel Kurtz | fdf80421 | 2012-06-28 21:08:24 +0800 | [diff] [blame] | 317 | u8 T6_reportid; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 318 | u16 T6_address; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 319 | u16 T7_address; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 320 | u8 T9_reportid_min; |
| 321 | u8 T9_reportid_max; |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 322 | u8 T19_reportid; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 323 | u16 T44_address; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 324 | u8 T100_reportid_min; |
| 325 | u8 T100_reportid_max; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 326 | |
| 327 | /* for fw update in bootloader */ |
| 328 | struct completion bl_completion; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 329 | |
| 330 | /* for reset handling */ |
| 331 | struct completion reset_completion; |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 332 | |
| 333 | /* for config update handling */ |
| 334 | struct completion crc_completion; |
Dmitry Torokhov | d80808e | 2018-03-20 15:31:25 -0700 | [diff] [blame] | 335 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 336 | u32 *t19_keymap; |
| 337 | unsigned int t19_num_keys; |
| 338 | |
Dmitry Torokhov | d80808e | 2018-03-20 15:31:25 -0700 | [diff] [blame] | 339 | enum mxt_suspend_mode suspend_mode; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 340 | }; |
| 341 | |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 342 | struct mxt_vb2_buffer { |
| 343 | struct vb2_buffer vb; |
| 344 | struct list_head list; |
| 345 | }; |
| 346 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 347 | static size_t mxt_obj_size(const struct mxt_object *obj) |
| 348 | { |
| 349 | return obj->size_minus_one + 1; |
| 350 | } |
| 351 | |
| 352 | static size_t mxt_obj_instances(const struct mxt_object *obj) |
| 353 | { |
| 354 | return obj->instances_minus_one + 1; |
| 355 | } |
| 356 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 357 | static bool mxt_object_readable(unsigned int type) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 358 | { |
| 359 | switch (type) { |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 360 | case MXT_GEN_COMMAND_T6: |
| 361 | case MXT_GEN_POWER_T7: |
| 362 | case MXT_GEN_ACQUIRE_T8: |
| 363 | case MXT_GEN_DATASOURCE_T53: |
| 364 | case MXT_TOUCH_MULTI_T9: |
| 365 | case MXT_TOUCH_KEYARRAY_T15: |
| 366 | case MXT_TOUCH_PROXIMITY_T23: |
| 367 | case MXT_TOUCH_PROXKEY_T52: |
Maxime Roussin-Bélanger | 089b50d | 2017-05-19 14:59:03 -0700 | [diff] [blame] | 368 | case MXT_TOUCH_MULTITOUCHSCREEN_T100: |
Iiro Valkonen | 81c88a7 | 2011-07-04 03:08:25 -0700 | [diff] [blame] | 369 | case MXT_PROCI_GRIPFACE_T20: |
| 370 | case MXT_PROCG_NOISE_T22: |
| 371 | case MXT_PROCI_ONETOUCH_T24: |
| 372 | case MXT_PROCI_TWOTOUCH_T27: |
| 373 | case MXT_PROCI_GRIP_T40: |
| 374 | case MXT_PROCI_PALM_T41: |
| 375 | case MXT_PROCI_TOUCHSUPPRESSION_T42: |
| 376 | case MXT_PROCI_STYLUS_T47: |
| 377 | case MXT_PROCG_NOISESUPPRESSION_T48: |
| 378 | case MXT_SPT_COMMSCONFIG_T18: |
| 379 | case MXT_SPT_GPIOPWM_T19: |
| 380 | case MXT_SPT_SELFTEST_T25: |
| 381 | case MXT_SPT_CTECONFIG_T28: |
| 382 | case MXT_SPT_USERDATA_T38: |
| 383 | case MXT_SPT_DIGITIZER_T43: |
| 384 | case MXT_SPT_CTECONFIG_T46: |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 385 | return true; |
| 386 | default: |
| 387 | return false; |
| 388 | } |
| 389 | } |
| 390 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 391 | static void mxt_dump_message(struct mxt_data *data, u8 *message) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 392 | { |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 393 | dev_dbg(&data->client->dev, "message: %*ph\n", |
| 394 | data->T5_msg_size, message); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 397 | static int mxt_wait_for_completion(struct mxt_data *data, |
| 398 | struct completion *comp, |
| 399 | unsigned int timeout_ms) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 400 | { |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 401 | struct device *dev = &data->client->dev; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 402 | unsigned long timeout = msecs_to_jiffies(timeout_ms); |
| 403 | long ret; |
| 404 | |
| 405 | ret = wait_for_completion_interruptible_timeout(comp, timeout); |
| 406 | if (ret < 0) { |
| 407 | return ret; |
| 408 | } else if (ret == 0) { |
| 409 | dev_err(dev, "Wait for completion timed out.\n"); |
| 410 | return -ETIMEDOUT; |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 415 | static int mxt_bootloader_read(struct mxt_data *data, |
| 416 | u8 *val, unsigned int count) |
| 417 | { |
| 418 | int ret; |
| 419 | struct i2c_msg msg; |
| 420 | |
| 421 | msg.addr = data->bootloader_addr; |
| 422 | msg.flags = data->client->flags & I2C_M_TEN; |
| 423 | msg.flags |= I2C_M_RD; |
| 424 | msg.len = count; |
| 425 | msg.buf = val; |
| 426 | |
| 427 | ret = i2c_transfer(data->client->adapter, &msg, 1); |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 428 | if (ret == 1) { |
| 429 | ret = 0; |
| 430 | } else { |
| 431 | ret = ret < 0 ? ret : -EIO; |
| 432 | dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n", |
| 433 | __func__, ret); |
| 434 | } |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static int mxt_bootloader_write(struct mxt_data *data, |
| 440 | const u8 * const val, unsigned int count) |
| 441 | { |
| 442 | int ret; |
| 443 | struct i2c_msg msg; |
| 444 | |
| 445 | msg.addr = data->bootloader_addr; |
| 446 | msg.flags = data->client->flags & I2C_M_TEN; |
| 447 | msg.len = count; |
| 448 | msg.buf = (u8 *)val; |
| 449 | |
| 450 | ret = i2c_transfer(data->client->adapter, &msg, 1); |
| 451 | if (ret == 1) { |
| 452 | ret = 0; |
| 453 | } else { |
| 454 | ret = ret < 0 ? ret : -EIO; |
| 455 | dev_err(&data->client->dev, "%s: i2c send failed (%d)\n", |
| 456 | __func__, ret); |
| 457 | } |
| 458 | |
| 459 | return ret; |
| 460 | } |
| 461 | |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 462 | static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry) |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 463 | { |
| 464 | u8 appmode = data->client->addr; |
| 465 | u8 bootloader; |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 466 | u8 family_id = data->info ? data->info->family_id : 0; |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 467 | |
| 468 | switch (appmode) { |
| 469 | case 0x4a: |
| 470 | case 0x4b: |
Nick Dyer | 44a0bab | 2014-07-23 12:45:26 -0700 | [diff] [blame] | 471 | /* Chips after 1664S use different scheme */ |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 472 | if (retry || family_id >= 0xa2) { |
Nick Dyer | 44a0bab | 2014-07-23 12:45:26 -0700 | [diff] [blame] | 473 | bootloader = appmode - 0x24; |
| 474 | break; |
| 475 | } |
| 476 | /* Fall through for normal case */ |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 477 | case 0x4c: |
| 478 | case 0x4d: |
| 479 | case 0x5a: |
| 480 | case 0x5b: |
| 481 | bootloader = appmode - 0x26; |
| 482 | break; |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 483 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 484 | default: |
| 485 | dev_err(&data->client->dev, |
| 486 | "Appmode i2c address 0x%02x not found\n", |
| 487 | appmode); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | data->bootloader_addr = bootloader; |
| 492 | return 0; |
| 493 | } |
| 494 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 495 | static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address) |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 496 | { |
| 497 | struct device *dev = &data->client->dev; |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 498 | int error; |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 499 | u8 val; |
| 500 | bool crc_failure; |
| 501 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 502 | error = mxt_lookup_bootloader_address(data, alt_address); |
| 503 | if (error) |
| 504 | return error; |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 505 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 506 | error = mxt_bootloader_read(data, &val, 1); |
| 507 | if (error) |
| 508 | return error; |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 509 | |
| 510 | /* Check app crc fail mode */ |
| 511 | crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL; |
| 512 | |
| 513 | dev_err(dev, "Detected bootloader, status:%02X%s\n", |
| 514 | val, crc_failure ? ", APP_CRC_FAIL" : ""); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 519 | static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val) |
| 520 | { |
| 521 | struct device *dev = &data->client->dev; |
| 522 | u8 buf[3]; |
| 523 | |
| 524 | if (val & MXT_BOOT_EXTENDED_ID) { |
| 525 | if (mxt_bootloader_read(data, &buf[0], 3) != 0) { |
| 526 | dev_err(dev, "%s: i2c failure\n", __func__); |
Nick Dyer | 68807a0 | 2014-06-07 23:17:26 -0700 | [diff] [blame] | 527 | return val; |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]); |
| 531 | |
| 532 | return buf[0]; |
| 533 | } else { |
| 534 | dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK); |
| 535 | |
| 536 | return val; |
| 537 | } |
| 538 | } |
| 539 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 540 | static int mxt_check_bootloader(struct mxt_data *data, unsigned int state, |
| 541 | bool wait) |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 542 | { |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 543 | struct device *dev = &data->client->dev; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 544 | u8 val; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 545 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 546 | |
| 547 | recheck: |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 548 | if (wait) { |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 549 | /* |
| 550 | * In application update mode, the interrupt |
| 551 | * line signals state transitions. We must wait for the |
| 552 | * CHG assertion before reading the status byte. |
| 553 | * Once the status byte has been read, the line is deasserted. |
| 554 | */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 555 | ret = mxt_wait_for_completion(data, &data->bl_completion, |
| 556 | MXT_FW_CHG_TIMEOUT); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 557 | if (ret) { |
| 558 | /* |
| 559 | * TODO: handle -ERESTARTSYS better by terminating |
| 560 | * fw update process before returning to userspace |
| 561 | * by writing length 0x000 to device (iff we are in |
| 562 | * WAITING_FRAME_DATA state). |
| 563 | */ |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 564 | dev_err(dev, "Update wait error %d\n", ret); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 565 | return ret; |
| 566 | } |
| 567 | } |
| 568 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 569 | ret = mxt_bootloader_read(data, &val, 1); |
| 570 | if (ret) |
| 571 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 572 | |
Nick Dyer | e57a66a | 2014-05-18 23:13:40 -0700 | [diff] [blame] | 573 | if (state == MXT_WAITING_BOOTLOAD_CMD) |
| 574 | val = mxt_get_bootloader_version(data, val); |
| 575 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 576 | switch (state) { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 577 | case MXT_WAITING_BOOTLOAD_CMD: |
| 578 | case MXT_WAITING_FRAME_DATA: |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 579 | case MXT_APP_CRC_FAIL: |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 580 | val &= ~MXT_BOOT_STATUS_MASK; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 581 | break; |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 582 | case MXT_FRAME_CRC_PASS: |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 583 | if (val == MXT_FRAME_CRC_CHECK) { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 584 | goto recheck; |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 585 | } else if (val == MXT_FRAME_CRC_FAIL) { |
| 586 | dev_err(dev, "Bootloader CRC fail\n"); |
| 587 | return -EINVAL; |
| 588 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 589 | break; |
| 590 | default: |
| 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | if (val != state) { |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 595 | dev_err(dev, "Invalid bootloader state %02X != %02X\n", |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 596 | val, state); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 597 | return -EINVAL; |
| 598 | } |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 603 | static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 604 | { |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 605 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 606 | u8 buf[2]; |
| 607 | |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 608 | if (unlock) { |
| 609 | buf[0] = MXT_UNLOCK_CMD_LSB; |
| 610 | buf[1] = MXT_UNLOCK_CMD_MSB; |
| 611 | } else { |
| 612 | buf[0] = 0x01; |
| 613 | buf[1] = 0x01; |
| 614 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 615 | |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 616 | ret = mxt_bootloader_write(data, buf, 2); |
| 617 | if (ret) |
| 618 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 623 | static int __mxt_read_reg(struct i2c_client *client, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 624 | u16 reg, u16 len, void *val) |
| 625 | { |
| 626 | struct i2c_msg xfer[2]; |
| 627 | u8 buf[2]; |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 628 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 629 | |
| 630 | buf[0] = reg & 0xff; |
| 631 | buf[1] = (reg >> 8) & 0xff; |
| 632 | |
| 633 | /* Write register */ |
| 634 | xfer[0].addr = client->addr; |
| 635 | xfer[0].flags = 0; |
| 636 | xfer[0].len = 2; |
| 637 | xfer[0].buf = buf; |
| 638 | |
| 639 | /* Read data */ |
| 640 | xfer[1].addr = client->addr; |
| 641 | xfer[1].flags = I2C_M_RD; |
| 642 | xfer[1].len = len; |
| 643 | xfer[1].buf = val; |
| 644 | |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 645 | ret = i2c_transfer(client->adapter, xfer, 2); |
| 646 | if (ret == 2) { |
| 647 | ret = 0; |
| 648 | } else { |
| 649 | if (ret >= 0) |
| 650 | ret = -EIO; |
| 651 | dev_err(&client->dev, "%s: i2c transfer failed (%d)\n", |
| 652 | __func__, ret); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 655 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 658 | static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len, |
| 659 | const void *val) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 660 | { |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 661 | u8 *buf; |
| 662 | size_t count; |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 663 | int ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 664 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 665 | count = len + 2; |
| 666 | buf = kmalloc(count, GFP_KERNEL); |
| 667 | if (!buf) |
| 668 | return -ENOMEM; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 669 | |
| 670 | buf[0] = reg & 0xff; |
| 671 | buf[1] = (reg >> 8) & 0xff; |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 672 | memcpy(&buf[2], val, len); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 673 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 674 | ret = i2c_master_send(client, buf, count); |
| 675 | if (ret == count) { |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 676 | ret = 0; |
| 677 | } else { |
| 678 | if (ret >= 0) |
| 679 | ret = -EIO; |
| 680 | dev_err(&client->dev, "%s: i2c send failed (%d)\n", |
| 681 | __func__, ret); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 684 | kfree(buf); |
Daniel Kurtz | 771733e | 2012-06-28 21:08:11 +0800 | [diff] [blame] | 685 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 688 | 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] | 689 | { |
Daniel Kurtz | 9638ab7 | 2012-06-28 21:08:12 +0800 | [diff] [blame] | 690 | return __mxt_write_reg(client, reg, 1, &val); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 693 | static struct mxt_object * |
| 694 | mxt_get_object(struct mxt_data *data, u8 type) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 695 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 696 | struct mxt_object *object; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 697 | int i; |
| 698 | |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 699 | for (i = 0; i < data->info->object_num; i++) { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 700 | object = data->object_table + i; |
| 701 | if (object->type == type) |
| 702 | return object; |
| 703 | } |
| 704 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 705 | dev_warn(&data->client->dev, "Invalid object type T%u\n", type); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 706 | return NULL; |
| 707 | } |
| 708 | |
Nick Dyer | 497767d | 2014-07-23 12:47:50 -0700 | [diff] [blame] | 709 | static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg) |
| 710 | { |
| 711 | struct device *dev = &data->client->dev; |
| 712 | u8 status = msg[1]; |
| 713 | u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16); |
| 714 | |
| 715 | complete(&data->crc_completion); |
| 716 | |
| 717 | if (crc != data->config_crc) { |
| 718 | data->config_crc = crc; |
| 719 | dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc); |
| 720 | } |
| 721 | |
| 722 | /* Detect reset */ |
| 723 | if (status & MXT_T6_STATUS_RESET) |
| 724 | complete(&data->reset_completion); |
| 725 | |
| 726 | /* Output debug if status has changed */ |
| 727 | if (status != data->t6_status) |
| 728 | dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n", |
| 729 | status, |
| 730 | status == 0 ? " OK" : "", |
| 731 | status & MXT_T6_STATUS_RESET ? " RESET" : "", |
| 732 | status & MXT_T6_STATUS_OFL ? " OFL" : "", |
| 733 | status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "", |
| 734 | status & MXT_T6_STATUS_CAL ? " CAL" : "", |
| 735 | status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "", |
| 736 | status & MXT_T6_STATUS_COMSERR ? " COMSERR" : ""); |
| 737 | |
| 738 | /* Save current status */ |
| 739 | data->t6_status = status; |
| 740 | } |
| 741 | |
Linus Torvalds | 7f40548 | 2014-12-31 12:59:34 -0800 | [diff] [blame] | 742 | static int mxt_write_object(struct mxt_data *data, |
| 743 | u8 type, u8 offset, u8 val) |
| 744 | { |
| 745 | struct mxt_object *object; |
| 746 | u16 reg; |
| 747 | |
| 748 | object = mxt_get_object(data, type); |
| 749 | if (!object || offset >= mxt_obj_size(object)) |
| 750 | return -EINVAL; |
| 751 | |
| 752 | reg = object->start_address; |
| 753 | return mxt_write_reg(data->client, reg + offset, val); |
| 754 | } |
| 755 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 756 | static void mxt_input_button(struct mxt_data *data, u8 *message) |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 757 | { |
| 758 | struct input_dev *input = data->input_dev; |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 759 | int i; |
| 760 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 761 | for (i = 0; i < data->t19_num_keys; i++) { |
| 762 | if (data->t19_keymap[i] == KEY_RESERVED) |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 763 | continue; |
Dmitry Torokhov | c37f6d3 | 2015-04-15 10:31:10 -0700 | [diff] [blame] | 764 | |
| 765 | /* Active-low switch */ |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 766 | input_report_key(input, data->t19_keymap[i], |
Dmitry Torokhov | c37f6d3 | 2015-04-15 10:31:10 -0700 | [diff] [blame] | 767 | !(message[1] & BIT(i))); |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 771 | static void mxt_input_sync(struct mxt_data *data) |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 772 | { |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 773 | input_mt_report_pointer_emulation(data->input_dev, |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 774 | data->t19_num_keys); |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 775 | input_sync(data->input_dev); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 778 | static void mxt_proc_t9_message(struct mxt_data *data, u8 *message) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 779 | { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 780 | struct device *dev = &data->client->dev; |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 781 | struct input_dev *input_dev = data->input_dev; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 782 | int id; |
| 783 | u8 status; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 784 | int x; |
| 785 | int y; |
| 786 | int area; |
Nick Dyer | fea9e46 | 2014-05-18 23:21:48 -0700 | [diff] [blame] | 787 | int amplitude; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 788 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 789 | id = message[0] - data->T9_reportid_min; |
| 790 | status = message[1]; |
| 791 | x = (message[2] << 4) | ((message[4] >> 4) & 0xf); |
| 792 | y = (message[3] << 4) | ((message[4] & 0xf)); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 793 | |
| 794 | /* Handle 10/12 bit switching */ |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 795 | if (data->max_x < 1024) |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 796 | x >>= 2; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 797 | if (data->max_y < 1024) |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 798 | y >>= 2; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 799 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 800 | area = message[5]; |
| 801 | amplitude = message[6]; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 802 | |
Daniel Kurtz | b2e459b | 2012-06-28 21:08:18 +0800 | [diff] [blame] | 803 | dev_dbg(dev, |
| 804 | "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n", |
| 805 | id, |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 806 | (status & MXT_T9_DETECT) ? 'D' : '.', |
| 807 | (status & MXT_T9_PRESS) ? 'P' : '.', |
| 808 | (status & MXT_T9_RELEASE) ? 'R' : '.', |
| 809 | (status & MXT_T9_MOVE) ? 'M' : '.', |
| 810 | (status & MXT_T9_VECTOR) ? 'V' : '.', |
| 811 | (status & MXT_T9_AMP) ? 'A' : '.', |
| 812 | (status & MXT_T9_SUPPRESS) ? 'S' : '.', |
| 813 | (status & MXT_T9_UNGRIP) ? 'U' : '.', |
Nick Dyer | fea9e46 | 2014-05-18 23:21:48 -0700 | [diff] [blame] | 814 | x, y, area, amplitude); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 815 | |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 816 | input_mt_slot(input_dev, id); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 817 | |
Nick Dyer | f3889ed | 2014-05-18 23:22:04 -0700 | [diff] [blame] | 818 | if (status & MXT_T9_DETECT) { |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 819 | /* |
| 820 | * Multiple bits may be set if the host is slow to read |
| 821 | * the status messages, indicating all the events that |
| 822 | * have happened. |
| 823 | */ |
| 824 | if (status & MXT_T9_RELEASE) { |
| 825 | input_mt_report_slot_state(input_dev, |
| 826 | MT_TOOL_FINGER, 0); |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 827 | mxt_input_sync(data); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* Touch active */ |
| 831 | input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1); |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 832 | input_report_abs(input_dev, ABS_MT_POSITION_X, x); |
| 833 | input_report_abs(input_dev, ABS_MT_POSITION_Y, y); |
Nick Dyer | fea9e46 | 2014-05-18 23:21:48 -0700 | [diff] [blame] | 834 | input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude); |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 835 | input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area); |
Nick Dyer | eef820d | 2014-05-18 23:22:22 -0700 | [diff] [blame] | 836 | } else { |
| 837 | /* Touch no longer active, close out slot */ |
| 838 | input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0); |
Daniel Kurtz | fba5bc3 | 2012-06-28 21:08:17 +0800 | [diff] [blame] | 839 | } |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 840 | |
| 841 | data->update_input = true; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 844 | static void mxt_proc_t100_message(struct mxt_data *data, u8 *message) |
| 845 | { |
| 846 | struct device *dev = &data->client->dev; |
| 847 | struct input_dev *input_dev = data->input_dev; |
| 848 | int id; |
| 849 | u8 status; |
| 850 | u8 type = 0; |
| 851 | u16 x; |
| 852 | u16 y; |
| 853 | int distance = 0; |
| 854 | int tool = 0; |
| 855 | u8 major = 0; |
| 856 | u8 pressure = 0; |
| 857 | u8 orientation = 0; |
| 858 | |
| 859 | id = message[0] - data->T100_reportid_min - 2; |
| 860 | |
| 861 | /* ignore SCRSTATUS events */ |
| 862 | if (id < 0) |
| 863 | return; |
| 864 | |
| 865 | status = message[1]; |
| 866 | x = get_unaligned_le16(&message[2]); |
| 867 | y = get_unaligned_le16(&message[4]); |
| 868 | |
| 869 | if (status & MXT_T100_DETECT) { |
| 870 | type = (status & MXT_T100_TYPE_MASK) >> 4; |
| 871 | |
| 872 | switch (type) { |
| 873 | case MXT_T100_TYPE_HOVERING_FINGER: |
| 874 | tool = MT_TOOL_FINGER; |
| 875 | distance = MXT_DISTANCE_HOVERING; |
| 876 | |
| 877 | if (data->t100_aux_vect) |
| 878 | orientation = message[data->t100_aux_vect]; |
| 879 | |
| 880 | break; |
| 881 | |
| 882 | case MXT_T100_TYPE_FINGER: |
| 883 | case MXT_T100_TYPE_GLOVE: |
| 884 | tool = MT_TOOL_FINGER; |
| 885 | distance = MXT_DISTANCE_ACTIVE_TOUCH; |
| 886 | |
| 887 | if (data->t100_aux_area) |
| 888 | major = message[data->t100_aux_area]; |
| 889 | |
| 890 | if (data->t100_aux_ampl) |
| 891 | pressure = message[data->t100_aux_ampl]; |
| 892 | |
| 893 | if (data->t100_aux_vect) |
| 894 | orientation = message[data->t100_aux_vect]; |
| 895 | |
| 896 | break; |
| 897 | |
| 898 | case MXT_T100_TYPE_PASSIVE_STYLUS: |
| 899 | tool = MT_TOOL_PEN; |
| 900 | |
| 901 | /* |
| 902 | * Passive stylus is reported with size zero so |
| 903 | * hardcode. |
| 904 | */ |
| 905 | major = MXT_TOUCH_MAJOR_DEFAULT; |
| 906 | |
| 907 | if (data->t100_aux_ampl) |
| 908 | pressure = message[data->t100_aux_ampl]; |
| 909 | |
| 910 | break; |
| 911 | |
| 912 | case MXT_T100_TYPE_LARGE_TOUCH: |
| 913 | /* Ignore suppressed touch */ |
| 914 | break; |
| 915 | |
| 916 | default: |
| 917 | dev_dbg(dev, "Unexpected T100 type\n"); |
| 918 | return; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Values reported should be non-zero if tool is touching the |
| 924 | * device |
| 925 | */ |
| 926 | if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER) |
| 927 | pressure = MXT_PRESSURE_DEFAULT; |
| 928 | |
| 929 | input_mt_slot(input_dev, id); |
| 930 | |
| 931 | if (status & MXT_T100_DETECT) { |
| 932 | dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n", |
| 933 | id, type, x, y, major, pressure, orientation); |
| 934 | |
| 935 | input_mt_report_slot_state(input_dev, tool, 1); |
| 936 | input_report_abs(input_dev, ABS_MT_POSITION_X, x); |
| 937 | input_report_abs(input_dev, ABS_MT_POSITION_Y, y); |
| 938 | input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major); |
| 939 | input_report_abs(input_dev, ABS_MT_PRESSURE, pressure); |
| 940 | input_report_abs(input_dev, ABS_MT_DISTANCE, distance); |
| 941 | input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation); |
| 942 | } else { |
| 943 | dev_dbg(dev, "[%u] release\n", id); |
| 944 | |
| 945 | /* close out slot */ |
| 946 | input_mt_report_slot_state(input_dev, 0, 0); |
| 947 | } |
| 948 | |
| 949 | data->update_input = true; |
| 950 | } |
| 951 | |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 952 | static int mxt_proc_message(struct mxt_data *data, u8 *message) |
Daniel Kurtz | 04a7918 | 2012-06-28 21:08:21 +0800 | [diff] [blame] | 953 | { |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 954 | u8 report_id = message[0]; |
| 955 | |
| 956 | if (report_id == MXT_RPTID_NOMSG) |
| 957 | return 0; |
| 958 | |
| 959 | if (report_id == data->T6_reportid) { |
| 960 | mxt_proc_t6_messages(data, message); |
| 961 | } else if (!data->input_dev) { |
| 962 | /* |
| 963 | * Do not report events if input device |
| 964 | * is not yet registered. |
| 965 | */ |
| 966 | mxt_dump_message(data, message); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 967 | } else if (report_id >= data->T9_reportid_min && |
| 968 | report_id <= data->T9_reportid_max) { |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 969 | mxt_proc_t9_message(data, message); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 970 | } else if (report_id >= data->T100_reportid_min && |
| 971 | report_id <= data->T100_reportid_max) { |
| 972 | mxt_proc_t100_message(data, message); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 973 | } else if (report_id == data->T19_reportid) { |
| 974 | mxt_input_button(data, message); |
| 975 | data->update_input = true; |
| 976 | } else { |
| 977 | mxt_dump_message(data, message); |
| 978 | } |
| 979 | |
| 980 | return 1; |
| 981 | } |
| 982 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 983 | 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] | 984 | { |
| 985 | struct device *dev = &data->client->dev; |
| 986 | int ret; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 987 | int i; |
| 988 | u8 num_valid = 0; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 989 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 990 | /* Safety check for msg_buf */ |
| 991 | if (count > data->max_reportid) |
| 992 | return -EINVAL; |
| 993 | |
| 994 | /* Process remaining messages if necessary */ |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 995 | ret = __mxt_read_reg(data->client, data->T5_address, |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 996 | data->T5_msg_size * count, data->msg_buf); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 997 | if (ret) { |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 998 | dev_err(dev, "Failed to read %u messages (%d)\n", count, ret); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 999 | return ret; |
| 1000 | } |
| 1001 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1002 | for (i = 0; i < count; i++) { |
| 1003 | ret = mxt_proc_message(data, |
| 1004 | data->msg_buf + data->T5_msg_size * i); |
| 1005 | |
| 1006 | if (ret == 1) |
| 1007 | num_valid++; |
| 1008 | } |
| 1009 | |
| 1010 | /* return number of messages read */ |
| 1011 | return num_valid; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1014 | static irqreturn_t mxt_process_messages_t44(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1015 | { |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1016 | struct device *dev = &data->client->dev; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1017 | int ret; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1018 | u8 count, num_left; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1019 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1020 | /* Read T44 and T5 together */ |
| 1021 | ret = __mxt_read_reg(data->client, data->T44_address, |
| 1022 | data->T5_msg_size + 1, data->msg_buf); |
| 1023 | if (ret) { |
| 1024 | dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret); |
| 1025 | return IRQ_NONE; |
| 1026 | } |
| 1027 | |
| 1028 | count = data->msg_buf[0]; |
| 1029 | |
Nick Dyer | 507584e | 2015-08-04 16:58:05 -0700 | [diff] [blame] | 1030 | /* |
| 1031 | * This condition may be caused by the CHG line being configured in |
| 1032 | * Mode 0. It results in unnecessary I2C operations but it is benign. |
| 1033 | */ |
| 1034 | if (count == 0) |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1035 | return IRQ_NONE; |
Nick Dyer | 507584e | 2015-08-04 16:58:05 -0700 | [diff] [blame] | 1036 | |
| 1037 | if (count > data->max_reportid) { |
| 1038 | dev_warn(dev, "T44 count %d exceeded max report id\n", count); |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1039 | count = data->max_reportid; |
| 1040 | } |
| 1041 | |
| 1042 | /* Process first message */ |
| 1043 | ret = mxt_proc_message(data, data->msg_buf + 1); |
| 1044 | if (ret < 0) { |
| 1045 | dev_warn(dev, "Unexpected invalid message\n"); |
| 1046 | return IRQ_NONE; |
| 1047 | } |
| 1048 | |
| 1049 | num_left = count - 1; |
| 1050 | |
| 1051 | /* Process remaining messages if necessary */ |
| 1052 | if (num_left) { |
| 1053 | ret = mxt_read_and_process_messages(data, num_left); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1054 | if (ret < 0) |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1055 | goto end; |
| 1056 | else if (ret != num_left) |
| 1057 | dev_warn(dev, "Unexpected invalid message\n"); |
| 1058 | } |
| 1059 | |
| 1060 | end: |
| 1061 | if (data->update_input) { |
| 1062 | mxt_input_sync(data); |
| 1063 | data->update_input = false; |
| 1064 | } |
| 1065 | |
| 1066 | return IRQ_HANDLED; |
| 1067 | } |
| 1068 | |
| 1069 | static int mxt_process_messages_until_invalid(struct mxt_data *data) |
| 1070 | { |
| 1071 | struct device *dev = &data->client->dev; |
| 1072 | int count, read; |
| 1073 | u8 tries = 2; |
| 1074 | |
| 1075 | count = data->max_reportid; |
| 1076 | |
| 1077 | /* Read messages until we force an invalid */ |
| 1078 | do { |
| 1079 | read = mxt_read_and_process_messages(data, count); |
| 1080 | if (read < count) |
| 1081 | return 0; |
| 1082 | } while (--tries); |
| 1083 | |
| 1084 | if (data->update_input) { |
| 1085 | mxt_input_sync(data); |
| 1086 | data->update_input = false; |
| 1087 | } |
| 1088 | |
| 1089 | dev_err(dev, "CHG pin isn't cleared\n"); |
| 1090 | return -EBUSY; |
| 1091 | } |
| 1092 | |
| 1093 | static irqreturn_t mxt_process_messages(struct mxt_data *data) |
| 1094 | { |
| 1095 | int total_handled, num_handled; |
| 1096 | u8 count = data->last_message_count; |
| 1097 | |
| 1098 | if (count < 1 || count > data->max_reportid) |
| 1099 | count = 1; |
| 1100 | |
| 1101 | /* include final invalid message */ |
| 1102 | total_handled = mxt_read_and_process_messages(data, count + 1); |
| 1103 | if (total_handled < 0) |
| 1104 | return IRQ_NONE; |
| 1105 | /* if there were invalid messages, then we are done */ |
| 1106 | else if (total_handled <= count) |
| 1107 | goto update_count; |
| 1108 | |
| 1109 | /* keep reading two msgs until one is invalid or reportid limit */ |
| 1110 | do { |
| 1111 | num_handled = mxt_read_and_process_messages(data, 2); |
| 1112 | if (num_handled < 0) |
Nick Dyer | 8d4e163 | 2014-05-18 23:00:56 -0700 | [diff] [blame] | 1113 | return IRQ_NONE; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1114 | |
| 1115 | total_handled += num_handled; |
| 1116 | |
| 1117 | if (num_handled < 2) |
| 1118 | break; |
| 1119 | } while (total_handled < data->num_touchids); |
| 1120 | |
| 1121 | update_count: |
| 1122 | data->last_message_count = total_handled; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1123 | |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1124 | if (data->update_input) { |
Benson Leung | b735fbe | 2014-07-23 12:22:27 -0700 | [diff] [blame] | 1125 | mxt_input_sync(data); |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1126 | data->update_input = false; |
| 1127 | } |
Daniel Kurtz | 64464ae | 2012-06-28 21:08:23 +0800 | [diff] [blame] | 1128 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1129 | return IRQ_HANDLED; |
| 1130 | } |
| 1131 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 1132 | static irqreturn_t mxt_interrupt(int irq, void *dev_id) |
| 1133 | { |
| 1134 | struct mxt_data *data = dev_id; |
| 1135 | |
| 1136 | if (data->in_bootloader) { |
| 1137 | /* bootloader state transition completion */ |
| 1138 | complete(&data->bl_completion); |
| 1139 | return IRQ_HANDLED; |
| 1140 | } |
| 1141 | |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1142 | if (!data->object_table) |
| 1143 | return IRQ_HANDLED; |
| 1144 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1145 | if (data->T44_address) { |
| 1146 | return mxt_process_messages_t44(data); |
| 1147 | } else { |
| 1148 | return mxt_process_messages(data); |
| 1149 | } |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 1152 | static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset, |
| 1153 | u8 value, bool wait) |
| 1154 | { |
| 1155 | u16 reg; |
| 1156 | u8 command_register; |
| 1157 | int timeout_counter = 0; |
| 1158 | int ret; |
| 1159 | |
| 1160 | reg = data->T6_address + cmd_offset; |
| 1161 | |
| 1162 | ret = mxt_write_reg(data->client, reg, value); |
| 1163 | if (ret) |
| 1164 | return ret; |
| 1165 | |
| 1166 | if (!wait) |
| 1167 | return 0; |
| 1168 | |
| 1169 | do { |
| 1170 | msleep(20); |
| 1171 | ret = __mxt_read_reg(data->client, reg, 1, &command_register); |
| 1172 | if (ret) |
| 1173 | return ret; |
| 1174 | } while (command_register != 0 && timeout_counter++ <= 100); |
| 1175 | |
| 1176 | if (timeout_counter > 100) { |
| 1177 | dev_err(&data->client->dev, "Command failed!\n"); |
| 1178 | return -EIO; |
| 1179 | } |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Nick Dyer | eb43335 | 2016-04-25 14:18:52 -0700 | [diff] [blame] | 1184 | static int mxt_acquire_irq(struct mxt_data *data) |
| 1185 | { |
| 1186 | int error; |
| 1187 | |
| 1188 | enable_irq(data->irq); |
| 1189 | |
| 1190 | error = mxt_process_messages_until_invalid(data); |
| 1191 | if (error) |
| 1192 | return error; |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 1197 | static int mxt_soft_reset(struct mxt_data *data) |
| 1198 | { |
| 1199 | struct device *dev = &data->client->dev; |
| 1200 | int ret = 0; |
| 1201 | |
Nick Dyer | 885f3fb | 2015-08-04 16:57:25 -0700 | [diff] [blame] | 1202 | dev_info(dev, "Resetting device\n"); |
| 1203 | |
| 1204 | disable_irq(data->irq); |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 1205 | |
| 1206 | reinit_completion(&data->reset_completion); |
| 1207 | |
| 1208 | ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false); |
| 1209 | if (ret) |
| 1210 | return ret; |
| 1211 | |
Nick Dyer | 885f3fb | 2015-08-04 16:57:25 -0700 | [diff] [blame] | 1212 | /* Ignore CHG line for 100ms after reset */ |
Sebastian Reichel | ca1cd36 | 2018-05-24 09:33:36 -0700 | [diff] [blame] | 1213 | msleep(MXT_RESET_INVALID_CHG); |
Nick Dyer | 885f3fb | 2015-08-04 16:57:25 -0700 | [diff] [blame] | 1214 | |
Nick Dyer | eb43335 | 2016-04-25 14:18:52 -0700 | [diff] [blame] | 1215 | mxt_acquire_irq(data); |
Nick Dyer | 885f3fb | 2015-08-04 16:57:25 -0700 | [diff] [blame] | 1216 | |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 1217 | ret = mxt_wait_for_completion(data, &data->reset_completion, |
| 1218 | MXT_RESET_TIMEOUT); |
| 1219 | if (ret) |
| 1220 | return ret; |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 1225 | static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value) |
| 1226 | { |
| 1227 | /* |
| 1228 | * On failure, CRC is set to 0 and config will always be |
| 1229 | * downloaded. |
| 1230 | */ |
| 1231 | data->config_crc = 0; |
| 1232 | reinit_completion(&data->crc_completion); |
| 1233 | |
| 1234 | mxt_t6_command(data, cmd, value, true); |
| 1235 | |
| 1236 | /* |
| 1237 | * Wait for crc message. On failure, CRC is set to 0 and config will |
| 1238 | * always be downloaded. |
| 1239 | */ |
| 1240 | mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT); |
| 1241 | } |
| 1242 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1243 | static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte) |
| 1244 | { |
| 1245 | static const unsigned int crcpoly = 0x80001B; |
| 1246 | u32 result; |
| 1247 | u32 data_word; |
| 1248 | |
| 1249 | data_word = (secondbyte << 8) | firstbyte; |
| 1250 | result = ((*crc << 1) ^ data_word); |
| 1251 | |
| 1252 | if (result & 0x1000000) |
| 1253 | result ^= crcpoly; |
| 1254 | |
| 1255 | *crc = result; |
| 1256 | } |
| 1257 | |
| 1258 | static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off) |
| 1259 | { |
| 1260 | u32 crc = 0; |
| 1261 | u8 *ptr = base + start_off; |
| 1262 | u8 *last_val = base + end_off - 1; |
| 1263 | |
| 1264 | if (end_off < start_off) |
| 1265 | return -EINVAL; |
| 1266 | |
| 1267 | while (ptr < last_val) { |
| 1268 | mxt_calc_crc24(&crc, *ptr, *(ptr + 1)); |
| 1269 | ptr += 2; |
| 1270 | } |
| 1271 | |
| 1272 | /* if len is odd, fill the last byte with 0 */ |
| 1273 | if (ptr == last_val) |
| 1274 | mxt_calc_crc24(&crc, *ptr, 0); |
| 1275 | |
| 1276 | /* Mask to 24-bit */ |
| 1277 | crc &= 0x00FFFFFF; |
| 1278 | |
| 1279 | return crc; |
| 1280 | } |
| 1281 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1282 | static int mxt_prepare_cfg_mem(struct mxt_data *data, |
| 1283 | const struct firmware *cfg, |
| 1284 | unsigned int data_pos, |
| 1285 | unsigned int cfg_start_ofs, |
| 1286 | u8 *config_mem, |
| 1287 | size_t config_mem_size) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1288 | { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1289 | struct device *dev = &data->client->dev; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1290 | struct mxt_object *object; |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1291 | unsigned int type, instance, size, byte_offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1292 | int offset; |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1293 | int ret; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1294 | int i; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1295 | u16 reg; |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1296 | u8 val; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1297 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1298 | while (data_pos < cfg->size) { |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1299 | /* Read type, instance, length */ |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1300 | ret = sscanf(cfg->data + data_pos, "%x %x %x%n", |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1301 | &type, &instance, &size, &offset); |
| 1302 | if (ret == 0) { |
| 1303 | /* EOF */ |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1304 | break; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1305 | } else if (ret != 3) { |
| 1306 | dev_err(dev, "Bad format: failed to parse object\n"); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1307 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1308 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1309 | data_pos += offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1310 | |
| 1311 | object = mxt_get_object(data, type); |
| 1312 | if (!object) { |
| 1313 | /* Skip object */ |
| 1314 | for (i = 0; i < size; i++) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1315 | ret = sscanf(cfg->data + data_pos, "%hhx%n", |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1316 | &val, &offset); |
| 1317 | if (ret != 1) { |
| 1318 | dev_err(dev, "Bad format in T%d at %d\n", |
| 1319 | type, i); |
| 1320 | return -EINVAL; |
| 1321 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1322 | data_pos += offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1323 | } |
| 1324 | continue; |
| 1325 | } |
| 1326 | |
| 1327 | if (size > mxt_obj_size(object)) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1328 | /* |
| 1329 | * Either we are in fallback mode due to wrong |
| 1330 | * config or config from a later fw version, |
| 1331 | * or the file is corrupt or hand-edited. |
| 1332 | */ |
| 1333 | dev_warn(dev, "Discarding %zu byte(s) in T%u\n", |
| 1334 | size - mxt_obj_size(object), type); |
| 1335 | } else if (mxt_obj_size(object) > size) { |
| 1336 | /* |
| 1337 | * If firmware is upgraded, new bytes may be added to |
| 1338 | * end of objects. It is generally forward compatible |
| 1339 | * to zero these bytes - previous behaviour will be |
| 1340 | * retained. However this does invalidate the CRC and |
| 1341 | * will force fallback mode until the configuration is |
| 1342 | * updated. We warn here but do nothing else - the |
| 1343 | * malloc has zeroed the entire configuration. |
| 1344 | */ |
| 1345 | dev_warn(dev, "Zeroing %zu byte(s) in T%d\n", |
| 1346 | mxt_obj_size(object) - size, type); |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | if (instance >= mxt_obj_instances(object)) { |
| 1350 | dev_err(dev, "Object instances exceeded!\n"); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1351 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | reg = object->start_address + mxt_obj_size(object) * instance; |
| 1355 | |
| 1356 | for (i = 0; i < size; i++) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1357 | ret = sscanf(cfg->data + data_pos, "%hhx%n", |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1358 | &val, |
| 1359 | &offset); |
| 1360 | if (ret != 1) { |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1361 | dev_err(dev, "Bad format in T%d at %d\n", |
| 1362 | type, i); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1363 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1364 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1365 | data_pos += offset; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1366 | |
| 1367 | if (i > mxt_obj_size(object)) |
| 1368 | continue; |
| 1369 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1370 | byte_offset = reg + i - cfg_start_ofs; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1371 | |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1372 | if (byte_offset >= 0 && byte_offset < config_mem_size) { |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1373 | *(config_mem + byte_offset) = val; |
| 1374 | } else { |
| 1375 | dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n", |
| 1376 | reg, object->type, byte_offset); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1377 | return -EINVAL; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1378 | } |
| 1379 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start, |
| 1386 | u8 *config_mem, size_t config_mem_size) |
| 1387 | { |
| 1388 | unsigned int byte_offset = 0; |
| 1389 | int error; |
| 1390 | |
| 1391 | /* Write configuration as blocks */ |
| 1392 | while (byte_offset < config_mem_size) { |
| 1393 | unsigned int size = config_mem_size - byte_offset; |
| 1394 | |
| 1395 | if (size > MXT_MAX_BLOCK_WRITE) |
| 1396 | size = MXT_MAX_BLOCK_WRITE; |
| 1397 | |
| 1398 | error = __mxt_write_reg(data->client, |
| 1399 | cfg_start + byte_offset, |
| 1400 | size, config_mem + byte_offset); |
| 1401 | if (error) { |
| 1402 | dev_err(&data->client->dev, |
| 1403 | "Config write error, ret=%d\n", error); |
| 1404 | return error; |
| 1405 | } |
| 1406 | |
| 1407 | byte_offset += size; |
| 1408 | } |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 1413 | static int mxt_init_t7_power_cfg(struct mxt_data *data); |
| 1414 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1415 | /* |
| 1416 | * mxt_update_cfg - download configuration to chip |
| 1417 | * |
| 1418 | * Atmel Raw Config File Format |
| 1419 | * |
| 1420 | * The first four lines of the raw config file contain: |
| 1421 | * 1) Version |
| 1422 | * 2) Chip ID Information (first 7 bytes of device memory) |
| 1423 | * 3) Chip Information Block 24-bit CRC Checksum |
| 1424 | * 4) Chip Configuration 24-bit CRC Checksum |
| 1425 | * |
| 1426 | * The rest of the file consists of one line per object instance: |
| 1427 | * <TYPE> <INSTANCE> <SIZE> <CONTENTS> |
| 1428 | * |
| 1429 | * <TYPE> - 2-byte object type as hex |
| 1430 | * <INSTANCE> - 2-byte object instance number as hex |
| 1431 | * <SIZE> - 2-byte object size as hex |
| 1432 | * <CONTENTS> - array of <SIZE> 1-byte hex values |
| 1433 | */ |
| 1434 | static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg) |
| 1435 | { |
| 1436 | struct device *dev = &data->client->dev; |
| 1437 | struct mxt_info cfg_info; |
| 1438 | int ret; |
| 1439 | int offset; |
| 1440 | int data_pos; |
| 1441 | int i; |
| 1442 | int cfg_start_ofs; |
| 1443 | u32 info_crc, config_crc, calculated_crc; |
| 1444 | u8 *config_mem; |
| 1445 | size_t config_mem_size; |
| 1446 | |
| 1447 | mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1); |
| 1448 | |
| 1449 | if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) { |
| 1450 | dev_err(dev, "Unrecognised config file\n"); |
| 1451 | return -EINVAL; |
| 1452 | } |
| 1453 | |
| 1454 | data_pos = strlen(MXT_CFG_MAGIC); |
| 1455 | |
| 1456 | /* Load information block and check */ |
| 1457 | for (i = 0; i < sizeof(struct mxt_info); i++) { |
| 1458 | ret = sscanf(cfg->data + data_pos, "%hhx%n", |
| 1459 | (unsigned char *)&cfg_info + i, |
| 1460 | &offset); |
| 1461 | if (ret != 1) { |
| 1462 | dev_err(dev, "Bad format\n"); |
| 1463 | return -EINVAL; |
| 1464 | } |
| 1465 | |
| 1466 | data_pos += offset; |
| 1467 | } |
| 1468 | |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1469 | if (cfg_info.family_id != data->info->family_id) { |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1470 | dev_err(dev, "Family ID mismatch!\n"); |
| 1471 | return -EINVAL; |
| 1472 | } |
| 1473 | |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1474 | if (cfg_info.variant_id != data->info->variant_id) { |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1475 | dev_err(dev, "Variant ID mismatch!\n"); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | |
| 1479 | /* Read CRCs */ |
| 1480 | ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset); |
| 1481 | if (ret != 1) { |
| 1482 | dev_err(dev, "Bad format: failed to parse Info CRC\n"); |
| 1483 | return -EINVAL; |
| 1484 | } |
| 1485 | data_pos += offset; |
| 1486 | |
| 1487 | ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset); |
| 1488 | if (ret != 1) { |
| 1489 | dev_err(dev, "Bad format: failed to parse Config CRC\n"); |
| 1490 | return -EINVAL; |
| 1491 | } |
| 1492 | data_pos += offset; |
| 1493 | |
| 1494 | /* |
| 1495 | * The Info Block CRC is calculated over mxt_info and the object |
| 1496 | * table. If it does not match then we are trying to load the |
| 1497 | * configuration from a different chip or firmware version, so |
| 1498 | * the configuration CRC is invalid anyway. |
| 1499 | */ |
| 1500 | if (info_crc == data->info_crc) { |
| 1501 | if (config_crc == 0 || data->config_crc == 0) { |
| 1502 | dev_info(dev, "CRC zero, attempting to apply config\n"); |
| 1503 | } else if (config_crc == data->config_crc) { |
| 1504 | dev_dbg(dev, "Config CRC 0x%06X: OK\n", |
| 1505 | data->config_crc); |
| 1506 | return 0; |
| 1507 | } else { |
| 1508 | dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n", |
| 1509 | data->config_crc, config_crc); |
| 1510 | } |
| 1511 | } else { |
| 1512 | dev_warn(dev, |
| 1513 | "Warning: Info CRC error - device=0x%06X file=0x%06X\n", |
| 1514 | data->info_crc, info_crc); |
| 1515 | } |
| 1516 | |
| 1517 | /* Malloc memory to store configuration */ |
| 1518 | cfg_start_ofs = MXT_OBJECT_START + |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1519 | data->info->object_num * sizeof(struct mxt_object) + |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1520 | MXT_INFO_CHECKSUM_SIZE; |
| 1521 | config_mem_size = data->mem_size - cfg_start_ofs; |
| 1522 | config_mem = kzalloc(config_mem_size, GFP_KERNEL); |
| 1523 | if (!config_mem) { |
| 1524 | dev_err(dev, "Failed to allocate memory\n"); |
| 1525 | return -ENOMEM; |
| 1526 | } |
| 1527 | |
| 1528 | ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs, |
| 1529 | config_mem, config_mem_size); |
| 1530 | if (ret) |
| 1531 | goto release_mem; |
| 1532 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1533 | /* Calculate crc of the received configs (not the raw config file) */ |
| 1534 | if (data->T7_address < cfg_start_ofs) { |
| 1535 | dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n", |
| 1536 | data->T7_address, cfg_start_ofs); |
| 1537 | ret = 0; |
| 1538 | goto release_mem; |
| 1539 | } |
| 1540 | |
| 1541 | calculated_crc = mxt_calculate_crc(config_mem, |
| 1542 | data->T7_address - cfg_start_ofs, |
| 1543 | config_mem_size); |
| 1544 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1545 | if (config_crc > 0 && config_crc != calculated_crc) |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1546 | dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n", |
| 1547 | calculated_crc, config_crc); |
| 1548 | |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 1549 | ret = mxt_upload_cfg_mem(data, cfg_start_ofs, |
| 1550 | config_mem, config_mem_size); |
| 1551 | if (ret) |
| 1552 | goto release_mem; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1553 | |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 1554 | mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE); |
| 1555 | |
| 1556 | ret = mxt_soft_reset(data); |
| 1557 | if (ret) |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1558 | goto release_mem; |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 1559 | |
| 1560 | dev_info(dev, "Config successfully updated\n"); |
| 1561 | |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 1562 | /* T7 config may have changed */ |
| 1563 | mxt_init_t7_power_cfg(data); |
| 1564 | |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1565 | release_mem: |
| 1566 | kfree(config_mem); |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 1567 | return ret; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 1570 | static void mxt_free_input_device(struct mxt_data *data) |
| 1571 | { |
| 1572 | if (data->input_dev) { |
| 1573 | input_unregister_device(data->input_dev); |
| 1574 | data->input_dev = NULL; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1578 | static void mxt_free_object_table(struct mxt_data *data) |
| 1579 | { |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 1580 | #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37 |
| 1581 | video_unregister_device(&data->dbg.vdev); |
| 1582 | v4l2_device_unregister(&data->dbg.v4l2); |
| 1583 | #endif |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1584 | data->object_table = NULL; |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1585 | data->info = NULL; |
| 1586 | kfree(data->raw_info_block); |
| 1587 | data->raw_info_block = NULL; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1588 | kfree(data->msg_buf); |
| 1589 | data->msg_buf = NULL; |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1590 | data->T5_address = 0; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1591 | data->T5_msg_size = 0; |
| 1592 | data->T6_reportid = 0; |
| 1593 | data->T7_address = 0; |
| 1594 | data->T9_reportid_min = 0; |
| 1595 | data->T9_reportid_max = 0; |
| 1596 | data->T19_reportid = 0; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1597 | data->T44_address = 0; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1598 | data->T100_reportid_min = 0; |
| 1599 | data->T100_reportid_max = 0; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1600 | data->max_reportid = 0; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1603 | static int mxt_parse_object_table(struct mxt_data *data, |
| 1604 | struct mxt_object *object_table) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1605 | { |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1606 | struct i2c_client *client = data->client; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1607 | int i; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1608 | u8 reportid; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1609 | u16 end_address; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1610 | |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1611 | /* Valid Report IDs start counting from 1 */ |
| 1612 | reportid = 1; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1613 | data->mem_size = 0; |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1614 | for (i = 0; i < data->info->object_num; i++) { |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1615 | struct mxt_object *object = object_table + i; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1616 | u8 min_id, max_id; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1617 | |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1618 | le16_to_cpus(&object->start_address); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1619 | |
| 1620 | if (object->num_report_ids) { |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1621 | min_id = reportid; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1622 | reportid += object->num_report_ids * |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 1623 | mxt_obj_instances(object); |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1624 | max_id = reportid - 1; |
| 1625 | } else { |
| 1626 | min_id = 0; |
| 1627 | max_id = 0; |
| 1628 | } |
| 1629 | |
| 1630 | dev_dbg(&data->client->dev, |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 1631 | "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] | 1632 | object->type, object->start_address, |
| 1633 | mxt_obj_size(object), mxt_obj_instances(object), |
| 1634 | min_id, max_id); |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1635 | |
| 1636 | switch (object->type) { |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1637 | case MXT_GEN_MESSAGE_T5: |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1638 | if (data->info->family_id == 0x80 && |
| 1639 | data->info->version < 0x20) { |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1640 | /* |
Nick Dyer | 437d4f3 | 2014-08-07 09:56:01 -0700 | [diff] [blame] | 1641 | * On mXT224 firmware versions prior to V2.0 |
| 1642 | * read and discard unused CRC byte otherwise |
| 1643 | * DMA reads are misaligned. |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1644 | */ |
| 1645 | data->T5_msg_size = mxt_obj_size(object); |
| 1646 | } else { |
| 1647 | /* CRC not enabled, so skip last byte */ |
| 1648 | data->T5_msg_size = mxt_obj_size(object) - 1; |
| 1649 | } |
Nick Dyer | b9b05a8 | 2014-07-23 12:48:13 -0700 | [diff] [blame] | 1650 | data->T5_address = object->start_address; |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 1651 | break; |
Daniel Kurtz | fdf80421 | 2012-06-28 21:08:24 +0800 | [diff] [blame] | 1652 | case MXT_GEN_COMMAND_T6: |
| 1653 | data->T6_reportid = min_id; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 1654 | data->T6_address = object->start_address; |
Daniel Kurtz | fdf80421 | 2012-06-28 21:08:24 +0800 | [diff] [blame] | 1655 | break; |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1656 | case MXT_GEN_POWER_T7: |
| 1657 | data->T7_address = object->start_address; |
| 1658 | break; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1659 | case MXT_TOUCH_MULTI_T9: |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1660 | data->multitouch = MXT_TOUCH_MULTI_T9; |
Nick Dyer | 36f5d9e | 2018-07-27 11:44:20 -0700 | [diff] [blame] | 1661 | /* Only handle messages from first T9 instance */ |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1662 | data->T9_reportid_min = min_id; |
Nick Dyer | 36f5d9e | 2018-07-27 11:44:20 -0700 | [diff] [blame] | 1663 | data->T9_reportid_max = min_id + |
| 1664 | object->num_report_ids - 1; |
| 1665 | data->num_touchids = object->num_report_ids; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1666 | break; |
| 1667 | case MXT_SPT_MESSAGECOUNT_T44: |
| 1668 | data->T44_address = object->start_address; |
Daniel Kurtz | 333e5a9 | 2012-06-28 21:08:20 +0800 | [diff] [blame] | 1669 | break; |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 1670 | case MXT_SPT_GPIOPWM_T19: |
| 1671 | data->T19_reportid = min_id; |
| 1672 | break; |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1673 | case MXT_TOUCH_MULTITOUCHSCREEN_T100: |
| 1674 | data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100; |
| 1675 | data->T100_reportid_min = min_id; |
| 1676 | data->T100_reportid_max = max_id; |
| 1677 | /* first two report IDs reserved */ |
| 1678 | data->num_touchids = object->num_report_ids - 2; |
| 1679 | break; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1680 | } |
Nick Dyer | 4ce6fa0 | 2014-07-23 12:40:09 -0700 | [diff] [blame] | 1681 | |
| 1682 | end_address = object->start_address |
| 1683 | + mxt_obj_size(object) * mxt_obj_instances(object) - 1; |
| 1684 | |
| 1685 | if (end_address >= data->mem_size) |
| 1686 | data->mem_size = end_address + 1; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1689 | /* Store maximum reportid */ |
| 1690 | data->max_reportid = reportid; |
| 1691 | |
| 1692 | /* If T44 exists, T5 position has to be directly after */ |
| 1693 | if (data->T44_address && (data->T5_address != data->T44_address + 1)) { |
| 1694 | dev_err(&client->dev, "Invalid T44 position\n"); |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1695 | return -EINVAL; |
Nick Dyer | 9d8dc3e | 2014-07-23 12:49:04 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | data->msg_buf = kcalloc(data->max_reportid, |
| 1699 | data->T5_msg_size, GFP_KERNEL); |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1700 | if (!data->msg_buf) |
| 1701 | return -ENOMEM; |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | static int mxt_read_info_block(struct mxt_data *data) |
| 1707 | { |
| 1708 | struct i2c_client *client = data->client; |
| 1709 | int error; |
| 1710 | size_t size; |
| 1711 | void *id_buf, *buf; |
| 1712 | uint8_t num_objects; |
| 1713 | u32 calculated_crc; |
| 1714 | u8 *crc_ptr; |
| 1715 | |
| 1716 | /* If info block already allocated, free it */ |
| 1717 | if (data->raw_info_block) |
| 1718 | mxt_free_object_table(data); |
| 1719 | |
| 1720 | /* Read 7-byte ID information block starting at address 0 */ |
| 1721 | size = sizeof(struct mxt_info); |
| 1722 | id_buf = kzalloc(size, GFP_KERNEL); |
| 1723 | if (!id_buf) |
| 1724 | return -ENOMEM; |
| 1725 | |
| 1726 | error = __mxt_read_reg(client, 0, size, id_buf); |
| 1727 | if (error) |
| 1728 | goto err_free_mem; |
| 1729 | |
| 1730 | /* Resize buffer to give space for rest of info block */ |
| 1731 | num_objects = ((struct mxt_info *)id_buf)->object_num; |
| 1732 | size += (num_objects * sizeof(struct mxt_object)) |
| 1733 | + MXT_INFO_CHECKSUM_SIZE; |
| 1734 | |
| 1735 | buf = krealloc(id_buf, size, GFP_KERNEL); |
| 1736 | if (!buf) { |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1737 | error = -ENOMEM; |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1738 | goto err_free_mem; |
| 1739 | } |
| 1740 | id_buf = buf; |
| 1741 | |
| 1742 | /* Read rest of info block */ |
| 1743 | error = __mxt_read_reg(client, MXT_OBJECT_START, |
| 1744 | size - MXT_OBJECT_START, |
| 1745 | id_buf + MXT_OBJECT_START); |
| 1746 | if (error) |
| 1747 | goto err_free_mem; |
| 1748 | |
| 1749 | /* Extract & calculate checksum */ |
| 1750 | crc_ptr = id_buf + size - MXT_INFO_CHECKSUM_SIZE; |
| 1751 | data->info_crc = crc_ptr[0] | (crc_ptr[1] << 8) | (crc_ptr[2] << 16); |
| 1752 | |
| 1753 | calculated_crc = mxt_calculate_crc(id_buf, 0, |
| 1754 | size - MXT_INFO_CHECKSUM_SIZE); |
| 1755 | |
| 1756 | /* |
| 1757 | * CRC mismatch can be caused by data corruption due to I2C comms |
| 1758 | * issue or else device is not using Object Based Protocol (eg i2c-hid) |
| 1759 | */ |
| 1760 | if ((data->info_crc == 0) || (data->info_crc != calculated_crc)) { |
| 1761 | dev_err(&client->dev, |
| 1762 | "Info Block CRC error calculated=0x%06X read=0x%06X\n", |
| 1763 | calculated_crc, data->info_crc); |
| 1764 | error = -EIO; |
| 1765 | goto err_free_mem; |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1768 | data->raw_info_block = id_buf; |
| 1769 | data->info = (struct mxt_info *)id_buf; |
| 1770 | |
| 1771 | dev_info(&client->dev, |
| 1772 | "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n", |
| 1773 | data->info->family_id, data->info->variant_id, |
| 1774 | data->info->version >> 4, data->info->version & 0xf, |
| 1775 | data->info->build, data->info->object_num); |
| 1776 | |
| 1777 | /* Parse object table information */ |
| 1778 | error = mxt_parse_object_table(data, id_buf + MXT_OBJECT_START); |
| 1779 | if (error) { |
| 1780 | dev_err(&client->dev, "Error %d parsing object table\n", error); |
| 1781 | mxt_free_object_table(data); |
| 1782 | goto err_free_mem; |
| 1783 | } |
| 1784 | |
| 1785 | data->object_table = (struct mxt_object *)(id_buf + MXT_OBJECT_START); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 1786 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1787 | return 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 1788 | |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 1789 | err_free_mem: |
| 1790 | kfree(id_buf); |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 1791 | return error; |
Daniel Kurtz | 7d4fa10 | 2012-06-28 21:08:19 +0800 | [diff] [blame] | 1792 | } |
| 1793 | |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 1794 | static int mxt_read_t9_resolution(struct mxt_data *data) |
| 1795 | { |
| 1796 | struct i2c_client *client = data->client; |
| 1797 | int error; |
| 1798 | struct t9_range range; |
| 1799 | unsigned char orient; |
| 1800 | struct mxt_object *object; |
| 1801 | |
| 1802 | object = mxt_get_object(data, MXT_TOUCH_MULTI_T9); |
| 1803 | if (!object) |
| 1804 | return -EINVAL; |
| 1805 | |
| 1806 | error = __mxt_read_reg(client, |
Nick Dyer | 2786489 | 2016-07-18 18:10:33 -0300 | [diff] [blame] | 1807 | object->start_address + MXT_T9_XSIZE, |
| 1808 | sizeof(data->xsize), &data->xsize); |
| 1809 | if (error) |
| 1810 | return error; |
| 1811 | |
| 1812 | error = __mxt_read_reg(client, |
| 1813 | object->start_address + MXT_T9_YSIZE, |
| 1814 | sizeof(data->ysize), &data->ysize); |
| 1815 | if (error) |
| 1816 | return error; |
| 1817 | |
| 1818 | error = __mxt_read_reg(client, |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 1819 | object->start_address + MXT_T9_RANGE, |
| 1820 | sizeof(range), &range); |
| 1821 | if (error) |
| 1822 | return error; |
| 1823 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1824 | data->max_x = get_unaligned_le16(&range.x); |
| 1825 | data->max_y = get_unaligned_le16(&range.y); |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 1826 | |
| 1827 | error = __mxt_read_reg(client, |
| 1828 | object->start_address + MXT_T9_ORIENT, |
| 1829 | 1, &orient); |
| 1830 | if (error) |
| 1831 | return error; |
| 1832 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1833 | data->xy_switch = orient & MXT_T9_ORIENT_SWITCH; |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 1834 | data->invertx = orient & MXT_T9_ORIENT_INVERTX; |
| 1835 | data->inverty = orient & MXT_T9_ORIENT_INVERTY; |
Nick Dyer | 61dc1ab | 2014-05-18 23:16:49 -0700 | [diff] [blame] | 1836 | |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1840 | static int mxt_read_t100_config(struct mxt_data *data) |
| 1841 | { |
| 1842 | struct i2c_client *client = data->client; |
| 1843 | int error; |
| 1844 | struct mxt_object *object; |
| 1845 | u16 range_x, range_y; |
| 1846 | u8 cfg, tchaux; |
| 1847 | u8 aux; |
| 1848 | |
| 1849 | object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100); |
| 1850 | if (!object) |
| 1851 | return -EINVAL; |
| 1852 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1853 | /* read touchscreen dimensions */ |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1854 | error = __mxt_read_reg(client, |
| 1855 | object->start_address + MXT_T100_XRANGE, |
| 1856 | sizeof(range_x), &range_x); |
| 1857 | if (error) |
| 1858 | return error; |
| 1859 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1860 | data->max_x = get_unaligned_le16(&range_x); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1861 | |
| 1862 | error = __mxt_read_reg(client, |
| 1863 | object->start_address + MXT_T100_YRANGE, |
| 1864 | sizeof(range_y), &range_y); |
| 1865 | if (error) |
| 1866 | return error; |
| 1867 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1868 | data->max_y = get_unaligned_le16(&range_y); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1869 | |
Nick Dyer | 2786489 | 2016-07-18 18:10:33 -0300 | [diff] [blame] | 1870 | error = __mxt_read_reg(client, |
| 1871 | object->start_address + MXT_T100_XSIZE, |
| 1872 | sizeof(data->xsize), &data->xsize); |
| 1873 | if (error) |
| 1874 | return error; |
| 1875 | |
| 1876 | error = __mxt_read_reg(client, |
| 1877 | object->start_address + MXT_T100_YSIZE, |
| 1878 | sizeof(data->ysize), &data->ysize); |
| 1879 | if (error) |
| 1880 | return error; |
| 1881 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1882 | /* read orientation config */ |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1883 | error = __mxt_read_reg(client, |
| 1884 | object->start_address + MXT_T100_CFG1, |
| 1885 | 1, &cfg); |
| 1886 | if (error) |
| 1887 | return error; |
| 1888 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1889 | data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY; |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 1890 | data->invertx = cfg & MXT_T100_CFG_INVERTX; |
| 1891 | data->inverty = cfg & MXT_T100_CFG_INVERTY; |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1892 | |
| 1893 | /* allocate aux bytes */ |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1894 | error = __mxt_read_reg(client, |
| 1895 | object->start_address + MXT_T100_TCHAUX, |
| 1896 | 1, &tchaux); |
| 1897 | if (error) |
| 1898 | return error; |
| 1899 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1900 | aux = 6; |
| 1901 | |
| 1902 | if (tchaux & MXT_T100_TCHAUX_VECT) |
| 1903 | data->t100_aux_vect = aux++; |
| 1904 | |
| 1905 | if (tchaux & MXT_T100_TCHAUX_AMPL) |
| 1906 | data->t100_aux_ampl = aux++; |
| 1907 | |
| 1908 | if (tchaux & MXT_T100_TCHAUX_AREA) |
| 1909 | data->t100_aux_area = aux++; |
| 1910 | |
| 1911 | dev_dbg(&client->dev, |
| 1912 | "T100 aux mappings vect:%u ampl:%u area:%u\n", |
| 1913 | data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area); |
| 1914 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1915 | return 0; |
| 1916 | } |
| 1917 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1918 | static int mxt_input_open(struct input_dev *dev); |
| 1919 | static void mxt_input_close(struct input_dev *dev); |
| 1920 | |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 1921 | static void mxt_set_up_as_touchpad(struct input_dev *input_dev, |
| 1922 | struct mxt_data *data) |
| 1923 | { |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 1924 | int i; |
| 1925 | |
| 1926 | input_dev->name = "Atmel maXTouch Touchpad"; |
| 1927 | |
| 1928 | __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); |
| 1929 | |
| 1930 | input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM); |
| 1931 | input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM); |
| 1932 | input_abs_set_res(input_dev, ABS_MT_POSITION_X, |
| 1933 | MXT_PIXELS_PER_MM); |
| 1934 | input_abs_set_res(input_dev, ABS_MT_POSITION_Y, |
| 1935 | MXT_PIXELS_PER_MM); |
| 1936 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 1937 | for (i = 0; i < data->t19_num_keys; i++) |
| 1938 | if (data->t19_keymap[i] != KEY_RESERVED) |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 1939 | input_set_capability(input_dev, EV_KEY, |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 1940 | data->t19_keymap[i]); |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1943 | static int mxt_initialize_input_device(struct mxt_data *data) |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1944 | { |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 1945 | struct device *dev = &data->client->dev; |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1946 | struct input_dev *input_dev; |
| 1947 | int error; |
| 1948 | unsigned int num_mt_slots; |
| 1949 | unsigned int mt_flags = 0; |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1950 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1951 | switch (data->multitouch) { |
| 1952 | case MXT_TOUCH_MULTI_T9: |
| 1953 | num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1; |
| 1954 | error = mxt_read_t9_resolution(data); |
| 1955 | if (error) |
| 1956 | dev_warn(dev, "Failed to initialize T9 resolution\n"); |
| 1957 | break; |
| 1958 | |
| 1959 | case MXT_TOUCH_MULTITOUCHSCREEN_T100: |
| 1960 | num_mt_slots = data->num_touchids; |
| 1961 | error = mxt_read_t100_config(data); |
| 1962 | if (error) |
| 1963 | dev_warn(dev, "Failed to read T100 config\n"); |
| 1964 | break; |
| 1965 | |
| 1966 | default: |
| 1967 | dev_err(dev, "Invalid multitouch object\n"); |
| 1968 | return -EINVAL; |
| 1969 | } |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1970 | |
Nick Dyer | 1c0276d | 2016-01-10 18:29:34 -0800 | [diff] [blame] | 1971 | /* Handle default values and orientation switch */ |
| 1972 | if (data->max_x == 0) |
| 1973 | data->max_x = 1023; |
| 1974 | |
| 1975 | if (data->max_y == 0) |
| 1976 | data->max_y = 1023; |
| 1977 | |
| 1978 | if (data->xy_switch) |
| 1979 | swap(data->max_x, data->max_y); |
| 1980 | |
| 1981 | dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y); |
| 1982 | |
| 1983 | /* Register input device */ |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1984 | input_dev = input_allocate_device(); |
| 1985 | if (!input_dev) { |
| 1986 | dev_err(dev, "Failed to allocate memory\n"); |
| 1987 | return -ENOMEM; |
| 1988 | } |
| 1989 | |
| 1990 | input_dev->name = "Atmel maXTouch Touchscreen"; |
| 1991 | input_dev->phys = data->phys; |
| 1992 | input_dev->id.bustype = BUS_I2C; |
| 1993 | input_dev->dev.parent = dev; |
| 1994 | input_dev->open = mxt_input_open; |
| 1995 | input_dev->close = mxt_input_close; |
| 1996 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 1997 | input_set_capability(input_dev, EV_KEY, BTN_TOUCH); |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1998 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 1999 | /* For single touch */ |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 2000 | input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0); |
| 2001 | input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0); |
| 2002 | |
| 2003 | if (data->multitouch == MXT_TOUCH_MULTI_T9 || |
| 2004 | (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 2005 | data->t100_aux_ampl)) { |
| 2006 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0); |
| 2007 | } |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2008 | |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 2009 | /* If device has buttons we assume it is a touchpad */ |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2010 | if (data->t19_num_keys) { |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 2011 | mxt_set_up_as_touchpad(input_dev, data); |
| 2012 | mt_flags |= INPUT_MT_POINTER; |
Nick Dyer | eafc0c8 | 2015-08-04 16:57:40 -0700 | [diff] [blame] | 2013 | } else { |
| 2014 | mt_flags |= INPUT_MT_DIRECT; |
Sjoerd Simons | b6d2d32 | 2015-04-06 13:10:30 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2017 | /* For multi touch */ |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2018 | error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags); |
| 2019 | if (error) { |
| 2020 | dev_err(dev, "Error %d initialising slots\n", error); |
| 2021 | goto err_free_mem; |
| 2022 | } |
| 2023 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 2024 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) { |
| 2025 | input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE, |
| 2026 | 0, MT_TOOL_MAX, 0, 0); |
| 2027 | input_set_abs_params(input_dev, ABS_MT_DISTANCE, |
| 2028 | MXT_DISTANCE_ACTIVE_TOUCH, |
| 2029 | MXT_DISTANCE_HOVERING, |
| 2030 | 0, 0); |
| 2031 | } |
| 2032 | |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2033 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, |
| 2034 | 0, data->max_x, 0, 0); |
| 2035 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, |
| 2036 | 0, data->max_y, 0, 0); |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 2037 | |
| 2038 | if (data->multitouch == MXT_TOUCH_MULTI_T9 || |
| 2039 | (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 2040 | data->t100_aux_area)) { |
| 2041 | input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, |
| 2042 | 0, MXT_MAX_AREA, 0, 0); |
| 2043 | } |
| 2044 | |
| 2045 | if (data->multitouch == MXT_TOUCH_MULTI_T9 || |
| 2046 | (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 2047 | data->t100_aux_ampl)) { |
| 2048 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, |
| 2049 | 0, 255, 0, 0); |
| 2050 | } |
| 2051 | |
| 2052 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 2053 | data->t100_aux_vect) { |
| 2054 | input_set_abs_params(input_dev, ABS_MT_ORIENTATION, |
| 2055 | 0, 255, 0, 0); |
| 2056 | } |
| 2057 | |
| 2058 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 2059 | data->t100_aux_ampl) { |
| 2060 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, |
| 2061 | 0, 255, 0, 0); |
| 2062 | } |
| 2063 | |
| 2064 | if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && |
| 2065 | data->t100_aux_vect) { |
| 2066 | input_set_abs_params(input_dev, ABS_MT_ORIENTATION, |
| 2067 | 0, 255, 0, 0); |
| 2068 | } |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 2069 | |
| 2070 | input_set_drvdata(input_dev, data); |
| 2071 | |
| 2072 | error = input_register_device(input_dev); |
| 2073 | if (error) { |
| 2074 | dev_err(dev, "Error %d registering input device\n", error); |
| 2075 | goto err_free_mem; |
| 2076 | } |
| 2077 | |
| 2078 | data->input_dev = input_dev; |
| 2079 | |
| 2080 | return 0; |
| 2081 | |
| 2082 | err_free_mem: |
| 2083 | input_free_device(input_dev); |
| 2084 | return error; |
| 2085 | } |
| 2086 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2087 | static int mxt_configure_objects(struct mxt_data *data, |
| 2088 | const struct firmware *cfg); |
| 2089 | |
| 2090 | static void mxt_config_cb(const struct firmware *cfg, void *ctx) |
| 2091 | { |
| 2092 | mxt_configure_objects(ctx, cfg); |
Dmitry Torokhov | efdbd7a | 2014-08-08 09:29:06 -0700 | [diff] [blame] | 2093 | release_firmware(cfg); |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2096 | static int mxt_initialize(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2097 | { |
| 2098 | struct i2c_client *client = data->client; |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2099 | int recovery_attempts = 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2100 | int error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2101 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2102 | while (1) { |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2103 | error = mxt_read_info_block(data); |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2104 | if (!error) |
| 2105 | break; |
| 2106 | |
| 2107 | /* Check bootloader state */ |
| 2108 | error = mxt_probe_bootloader(data, false); |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2109 | if (error) { |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2110 | dev_info(&client->dev, "Trying alternate bootloader address\n"); |
| 2111 | error = mxt_probe_bootloader(data, true); |
| 2112 | if (error) { |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2113 | /* Chip is not in appmode or bootloader mode */ |
| 2114 | return error; |
| 2115 | } |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2116 | } |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2117 | |
| 2118 | /* OK, we are in bootloader, see if we can recover */ |
| 2119 | if (++recovery_attempts > 1) { |
| 2120 | dev_err(&client->dev, "Could not recover from bootloader mode\n"); |
| 2121 | /* |
| 2122 | * We can reflash from this state, so do not |
| 2123 | * abort initialization. |
| 2124 | */ |
| 2125 | data->in_bootloader = true; |
| 2126 | return 0; |
| 2127 | } |
| 2128 | |
| 2129 | /* Attempt to exit bootloader into app mode */ |
| 2130 | mxt_send_bootloader_cmd(data, false); |
| 2131 | msleep(MXT_FW_RESET_TIME); |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2132 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2133 | |
Dmitry Torokhov | 041fa15 | 2014-08-11 10:58:29 -0700 | [diff] [blame] | 2134 | error = mxt_acquire_irq(data); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2135 | if (error) |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2136 | return error; |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2137 | |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2138 | error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME, |
| 2139 | &client->dev, GFP_KERNEL, data, |
| 2140 | mxt_config_cb); |
| 2141 | if (error) { |
| 2142 | dev_err(&client->dev, "Failed to invoke firmware loader: %d\n", |
| 2143 | error); |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2144 | return error; |
Dmitry Torokhov | 6cd1ab0 | 2014-08-08 09:28:45 -0700 | [diff] [blame] | 2145 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2146 | |
| 2147 | return 0; |
| 2148 | } |
| 2149 | |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 2150 | static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep) |
| 2151 | { |
| 2152 | struct device *dev = &data->client->dev; |
| 2153 | int error; |
| 2154 | struct t7_config *new_config; |
| 2155 | struct t7_config deepsleep = { .active = 0, .idle = 0 }; |
| 2156 | |
| 2157 | if (sleep == MXT_POWER_CFG_DEEPSLEEP) |
| 2158 | new_config = &deepsleep; |
| 2159 | else |
| 2160 | new_config = &data->t7_cfg; |
| 2161 | |
| 2162 | error = __mxt_write_reg(data->client, data->T7_address, |
| 2163 | sizeof(data->t7_cfg), new_config); |
| 2164 | if (error) |
| 2165 | return error; |
| 2166 | |
| 2167 | dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n", |
| 2168 | new_config->active, new_config->idle); |
| 2169 | |
| 2170 | return 0; |
| 2171 | } |
| 2172 | |
| 2173 | static int mxt_init_t7_power_cfg(struct mxt_data *data) |
| 2174 | { |
| 2175 | struct device *dev = &data->client->dev; |
| 2176 | int error; |
| 2177 | bool retry = false; |
| 2178 | |
| 2179 | recheck: |
| 2180 | error = __mxt_read_reg(data->client, data->T7_address, |
| 2181 | sizeof(data->t7_cfg), &data->t7_cfg); |
| 2182 | if (error) |
| 2183 | return error; |
| 2184 | |
| 2185 | if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) { |
| 2186 | if (!retry) { |
| 2187 | dev_dbg(dev, "T7 cfg zero, resetting\n"); |
| 2188 | mxt_soft_reset(data); |
| 2189 | retry = true; |
| 2190 | goto recheck; |
| 2191 | } else { |
| 2192 | dev_dbg(dev, "T7 cfg zero after reset, overriding\n"); |
| 2193 | data->t7_cfg.active = 20; |
| 2194 | data->t7_cfg.idle = 100; |
| 2195 | return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN); |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n", |
| 2200 | data->t7_cfg.active, data->t7_cfg.idle); |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2204 | #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37 |
| 2205 | static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x, |
| 2206 | unsigned int y) |
| 2207 | { |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2208 | struct mxt_info *info = data->info; |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2209 | struct mxt_dbg *dbg = &data->dbg; |
| 2210 | unsigned int ofs, page; |
Nick Dyer | 566d533 | 2016-07-18 18:10:35 -0300 | [diff] [blame] | 2211 | unsigned int col = 0; |
| 2212 | unsigned int col_width; |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2213 | |
Nick Dyer | 566d533 | 2016-07-18 18:10:35 -0300 | [diff] [blame] | 2214 | if (info->family_id == MXT_FAMILY_1386) { |
| 2215 | col_width = info->matrix_ysize / MXT1386_COLUMNS; |
| 2216 | col = y / col_width; |
| 2217 | y = y % col_width; |
| 2218 | } else { |
| 2219 | col_width = info->matrix_ysize; |
| 2220 | } |
| 2221 | |
| 2222 | ofs = (y + (x * col_width)) * sizeof(u16); |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2223 | page = ofs / MXT_DIAGNOSTIC_SIZE; |
| 2224 | ofs %= MXT_DIAGNOSTIC_SIZE; |
| 2225 | |
Nick Dyer | 566d533 | 2016-07-18 18:10:35 -0300 | [diff] [blame] | 2226 | if (info->family_id == MXT_FAMILY_1386) |
| 2227 | page += col * MXT1386_PAGES_PER_COLUMN; |
| 2228 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2229 | return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]); |
| 2230 | } |
| 2231 | |
| 2232 | static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf) |
| 2233 | { |
| 2234 | struct mxt_dbg *dbg = &data->dbg; |
| 2235 | unsigned int x = 0; |
| 2236 | unsigned int y = 0; |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 2237 | unsigned int i, rx, ry; |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2238 | |
| 2239 | for (i = 0; i < dbg->t37_nodes; i++) { |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 2240 | /* Handle orientation */ |
| 2241 | rx = data->xy_switch ? y : x; |
| 2242 | ry = data->xy_switch ? x : y; |
| 2243 | rx = data->invertx ? (data->xsize - 1 - rx) : rx; |
| 2244 | ry = data->inverty ? (data->ysize - 1 - ry) : ry; |
| 2245 | |
| 2246 | outbuf[i] = mxt_get_debug_value(data, rx, ry); |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2247 | |
| 2248 | /* Next value */ |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 2249 | if (++x >= (data->xy_switch ? data->ysize : data->xsize)) { |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2250 | x = 0; |
| 2251 | y++; |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | return 0; |
| 2256 | } |
| 2257 | |
| 2258 | static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode, |
| 2259 | u16 *outbuf) |
| 2260 | { |
| 2261 | struct mxt_dbg *dbg = &data->dbg; |
| 2262 | int retries = 0; |
| 2263 | int page; |
| 2264 | int ret; |
| 2265 | u8 cmd = mode; |
| 2266 | struct t37_debug *p; |
| 2267 | u8 cmd_poll; |
| 2268 | |
| 2269 | for (page = 0; page < dbg->t37_pages; page++) { |
| 2270 | p = dbg->t37_buf + page; |
| 2271 | |
| 2272 | ret = mxt_write_reg(data->client, dbg->diag_cmd_address, |
| 2273 | cmd); |
| 2274 | if (ret) |
| 2275 | return ret; |
| 2276 | |
| 2277 | retries = 0; |
| 2278 | msleep(20); |
| 2279 | wait_cmd: |
| 2280 | /* Read back command byte */ |
| 2281 | ret = __mxt_read_reg(data->client, dbg->diag_cmd_address, |
| 2282 | sizeof(cmd_poll), &cmd_poll); |
| 2283 | if (ret) |
| 2284 | return ret; |
| 2285 | |
| 2286 | /* Field is cleared once the command has been processed */ |
| 2287 | if (cmd_poll) { |
| 2288 | if (retries++ > 100) |
| 2289 | return -EINVAL; |
| 2290 | |
| 2291 | msleep(20); |
| 2292 | goto wait_cmd; |
| 2293 | } |
| 2294 | |
| 2295 | /* Read T37 page */ |
| 2296 | ret = __mxt_read_reg(data->client, dbg->t37_address, |
| 2297 | sizeof(struct t37_debug), p); |
| 2298 | if (ret) |
| 2299 | return ret; |
| 2300 | |
| 2301 | if (p->mode != mode || p->page != page) { |
| 2302 | dev_err(&data->client->dev, "T37 page mismatch\n"); |
| 2303 | return -EINVAL; |
| 2304 | } |
| 2305 | |
| 2306 | dev_dbg(&data->client->dev, "%s page:%d retries:%d\n", |
| 2307 | __func__, page, retries); |
| 2308 | |
| 2309 | /* For remaining pages, write PAGEUP rather than mode */ |
| 2310 | cmd = MXT_DIAGNOSTIC_PAGEUP; |
| 2311 | } |
| 2312 | |
| 2313 | return mxt_convert_debug_pages(data, outbuf); |
| 2314 | } |
| 2315 | |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2316 | static int mxt_queue_setup(struct vb2_queue *q, |
| 2317 | unsigned int *nbuffers, unsigned int *nplanes, |
| 2318 | unsigned int sizes[], struct device *alloc_devs[]) |
| 2319 | { |
| 2320 | struct mxt_data *data = q->drv_priv; |
| 2321 | size_t size = data->dbg.t37_nodes * sizeof(u16); |
| 2322 | |
| 2323 | if (*nplanes) |
| 2324 | return sizes[0] < size ? -EINVAL : 0; |
| 2325 | |
| 2326 | *nplanes = 1; |
| 2327 | sizes[0] = size; |
| 2328 | |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
| 2332 | static void mxt_buffer_queue(struct vb2_buffer *vb) |
| 2333 | { |
| 2334 | struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue); |
| 2335 | u16 *ptr; |
| 2336 | int ret; |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 2337 | u8 mode; |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2338 | |
| 2339 | ptr = vb2_plane_vaddr(vb, 0); |
| 2340 | if (!ptr) { |
| 2341 | dev_err(&data->client->dev, "Error acquiring frame ptr\n"); |
| 2342 | goto fault; |
| 2343 | } |
| 2344 | |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 2345 | switch (data->dbg.input) { |
| 2346 | case MXT_V4L_INPUT_DELTAS: |
| 2347 | default: |
| 2348 | mode = MXT_DIAGNOSTIC_DELTAS; |
| 2349 | break; |
| 2350 | |
| 2351 | case MXT_V4L_INPUT_REFS: |
| 2352 | mode = MXT_DIAGNOSTIC_REFS; |
| 2353 | break; |
| 2354 | } |
| 2355 | |
| 2356 | ret = mxt_read_diagnostic_debug(data, mode, ptr); |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2357 | if (ret) |
| 2358 | goto fault; |
| 2359 | |
| 2360 | vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16)); |
| 2361 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
| 2362 | return; |
| 2363 | |
| 2364 | fault: |
| 2365 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); |
| 2366 | } |
| 2367 | |
| 2368 | /* V4L2 structures */ |
| 2369 | static const struct vb2_ops mxt_queue_ops = { |
| 2370 | .queue_setup = mxt_queue_setup, |
| 2371 | .buf_queue = mxt_buffer_queue, |
| 2372 | .wait_prepare = vb2_ops_wait_prepare, |
| 2373 | .wait_finish = vb2_ops_wait_finish, |
| 2374 | }; |
| 2375 | |
| 2376 | static const struct vb2_queue mxt_queue = { |
| 2377 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 2378 | .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ, |
| 2379 | .buf_struct_size = sizeof(struct mxt_vb2_buffer), |
| 2380 | .ops = &mxt_queue_ops, |
| 2381 | .mem_ops = &vb2_vmalloc_memops, |
| 2382 | .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC, |
| 2383 | .min_buffers_needed = 1, |
| 2384 | }; |
| 2385 | |
| 2386 | static int mxt_vidioc_querycap(struct file *file, void *priv, |
| 2387 | struct v4l2_capability *cap) |
| 2388 | { |
| 2389 | struct mxt_data *data = video_drvdata(file); |
| 2390 | |
| 2391 | strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver)); |
| 2392 | strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card)); |
| 2393 | snprintf(cap->bus_info, sizeof(cap->bus_info), |
| 2394 | "I2C:%s", dev_name(&data->client->dev)); |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
| 2398 | static int mxt_vidioc_enum_input(struct file *file, void *priv, |
| 2399 | struct v4l2_input *i) |
| 2400 | { |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 2401 | if (i->index >= MXT_V4L_INPUT_MAX) |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2402 | return -EINVAL; |
| 2403 | |
| 2404 | i->type = V4L2_INPUT_TYPE_TOUCH; |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 2405 | |
| 2406 | switch (i->index) { |
| 2407 | case MXT_V4L_INPUT_REFS: |
| 2408 | strlcpy(i->name, "Mutual Capacitance References", |
| 2409 | sizeof(i->name)); |
| 2410 | break; |
| 2411 | case MXT_V4L_INPUT_DELTAS: |
| 2412 | strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name)); |
| 2413 | break; |
| 2414 | } |
| 2415 | |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2416 | return 0; |
| 2417 | } |
| 2418 | |
| 2419 | static int mxt_set_input(struct mxt_data *data, unsigned int i) |
| 2420 | { |
| 2421 | struct v4l2_pix_format *f = &data->dbg.format; |
| 2422 | |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 2423 | if (i >= MXT_V4L_INPUT_MAX) |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2424 | return -EINVAL; |
| 2425 | |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 2426 | if (i == MXT_V4L_INPUT_DELTAS) |
| 2427 | f->pixelformat = V4L2_TCH_FMT_DELTA_TD16; |
| 2428 | else |
| 2429 | f->pixelformat = V4L2_TCH_FMT_TU16; |
| 2430 | |
Nick Dyer | de601f7 | 2016-07-18 18:10:34 -0300 | [diff] [blame] | 2431 | f->width = data->xy_switch ? data->ysize : data->xsize; |
| 2432 | f->height = data->xy_switch ? data->xsize : data->ysize; |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2433 | f->field = V4L2_FIELD_NONE; |
| 2434 | f->colorspace = V4L2_COLORSPACE_RAW; |
| 2435 | f->bytesperline = f->width * sizeof(u16); |
| 2436 | f->sizeimage = f->width * f->height * sizeof(u16); |
| 2437 | |
| 2438 | data->dbg.input = i; |
| 2439 | |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
| 2443 | static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 2444 | { |
| 2445 | return mxt_set_input(video_drvdata(file), i); |
| 2446 | } |
| 2447 | |
| 2448 | static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 2449 | { |
| 2450 | struct mxt_data *data = video_drvdata(file); |
| 2451 | |
| 2452 | *i = data->dbg.input; |
| 2453 | |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
| 2457 | static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f) |
| 2458 | { |
| 2459 | struct mxt_data *data = video_drvdata(file); |
| 2460 | |
| 2461 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 2462 | f->fmt.pix = data->dbg.format; |
| 2463 | |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
| 2467 | static int mxt_vidioc_enum_fmt(struct file *file, void *priv, |
| 2468 | struct v4l2_fmtdesc *fmt) |
| 2469 | { |
| 2470 | if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 2471 | return -EINVAL; |
| 2472 | |
Nick Dyer | 06b3d3f | 2016-07-18 18:10:36 -0300 | [diff] [blame] | 2473 | switch (fmt->index) { |
| 2474 | case 0: |
| 2475 | fmt->pixelformat = V4L2_TCH_FMT_TU16; |
| 2476 | break; |
| 2477 | |
| 2478 | case 1: |
| 2479 | fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16; |
| 2480 | break; |
| 2481 | |
| 2482 | default: |
| 2483 | return -EINVAL; |
| 2484 | } |
| 2485 | |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | static int mxt_vidioc_g_parm(struct file *file, void *fh, |
| 2490 | struct v4l2_streamparm *a) |
| 2491 | { |
| 2492 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 2493 | return -EINVAL; |
| 2494 | |
| 2495 | a->parm.capture.readbuffers = 1; |
| 2496 | a->parm.capture.timeperframe.numerator = 1; |
| 2497 | a->parm.capture.timeperframe.denominator = 10; |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
| 2501 | static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = { |
| 2502 | .vidioc_querycap = mxt_vidioc_querycap, |
| 2503 | |
| 2504 | .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt, |
| 2505 | .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt, |
| 2506 | .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt, |
| 2507 | .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt, |
| 2508 | .vidioc_g_parm = mxt_vidioc_g_parm, |
| 2509 | |
| 2510 | .vidioc_enum_input = mxt_vidioc_enum_input, |
| 2511 | .vidioc_g_input = mxt_vidioc_g_input, |
| 2512 | .vidioc_s_input = mxt_vidioc_s_input, |
| 2513 | |
| 2514 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
| 2515 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
| 2516 | .vidioc_querybuf = vb2_ioctl_querybuf, |
| 2517 | .vidioc_qbuf = vb2_ioctl_qbuf, |
| 2518 | .vidioc_dqbuf = vb2_ioctl_dqbuf, |
| 2519 | .vidioc_expbuf = vb2_ioctl_expbuf, |
| 2520 | |
| 2521 | .vidioc_streamon = vb2_ioctl_streamon, |
| 2522 | .vidioc_streamoff = vb2_ioctl_streamoff, |
| 2523 | }; |
| 2524 | |
| 2525 | static const struct video_device mxt_video_device = { |
| 2526 | .name = "Atmel maxTouch", |
| 2527 | .fops = &mxt_video_fops, |
| 2528 | .ioctl_ops = &mxt_video_ioctl_ops, |
| 2529 | .release = video_device_release_empty, |
| 2530 | .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH | |
| 2531 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING, |
| 2532 | }; |
| 2533 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2534 | static void mxt_debug_init(struct mxt_data *data) |
| 2535 | { |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2536 | struct mxt_info *info = data->info; |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2537 | struct mxt_dbg *dbg = &data->dbg; |
| 2538 | struct mxt_object *object; |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2539 | int error; |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2540 | |
| 2541 | object = mxt_get_object(data, MXT_GEN_COMMAND_T6); |
| 2542 | if (!object) |
| 2543 | goto error; |
| 2544 | |
| 2545 | dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC; |
| 2546 | |
| 2547 | object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37); |
| 2548 | if (!object) |
| 2549 | goto error; |
| 2550 | |
| 2551 | if (mxt_obj_size(object) != sizeof(struct t37_debug)) { |
| 2552 | dev_warn(&data->client->dev, "Bad T37 size"); |
| 2553 | goto error; |
| 2554 | } |
| 2555 | |
| 2556 | dbg->t37_address = object->start_address; |
| 2557 | |
| 2558 | /* Calculate size of data and allocate buffer */ |
Nick Dyer | 2786489 | 2016-07-18 18:10:33 -0300 | [diff] [blame] | 2559 | dbg->t37_nodes = data->xsize * data->ysize; |
Nick Dyer | 566d533 | 2016-07-18 18:10:35 -0300 | [diff] [blame] | 2560 | |
| 2561 | if (info->family_id == MXT_FAMILY_1386) |
| 2562 | dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN; |
| 2563 | else |
| 2564 | dbg->t37_pages = DIV_ROUND_UP(data->xsize * |
Guenter Roeck | d7ddf15 | 2017-01-21 23:46:47 -0800 | [diff] [blame] | 2565 | info->matrix_ysize * |
Nick Dyer | 566d533 | 2016-07-18 18:10:35 -0300 | [diff] [blame] | 2566 | sizeof(u16), |
| 2567 | sizeof(dbg->t37_buf->data)); |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2568 | |
| 2569 | dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages, |
| 2570 | sizeof(struct t37_debug), GFP_KERNEL); |
| 2571 | if (!dbg->t37_buf) |
| 2572 | goto error; |
| 2573 | |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2574 | /* init channel to zero */ |
| 2575 | mxt_set_input(data, 0); |
| 2576 | |
| 2577 | /* register video device */ |
| 2578 | snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts"); |
| 2579 | error = v4l2_device_register(&data->client->dev, &dbg->v4l2); |
| 2580 | if (error) |
| 2581 | goto error; |
| 2582 | |
| 2583 | /* initialize the queue */ |
| 2584 | mutex_init(&dbg->lock); |
| 2585 | dbg->queue = mxt_queue; |
| 2586 | dbg->queue.drv_priv = data; |
| 2587 | dbg->queue.lock = &dbg->lock; |
| 2588 | dbg->queue.dev = &data->client->dev; |
| 2589 | |
| 2590 | error = vb2_queue_init(&dbg->queue); |
| 2591 | if (error) |
| 2592 | goto error_unreg_v4l2; |
| 2593 | |
| 2594 | dbg->vdev = mxt_video_device; |
| 2595 | dbg->vdev.v4l2_dev = &dbg->v4l2; |
| 2596 | dbg->vdev.lock = &dbg->lock; |
| 2597 | dbg->vdev.vfl_dir = VFL_DIR_RX; |
| 2598 | dbg->vdev.queue = &dbg->queue; |
| 2599 | video_set_drvdata(&dbg->vdev, data); |
| 2600 | |
| 2601 | error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1); |
| 2602 | if (error) |
| 2603 | goto error_unreg_v4l2; |
| 2604 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2605 | return; |
| 2606 | |
Nick Dyer | ecfdd7e | 2016-07-18 18:10:32 -0300 | [diff] [blame] | 2607 | error_unreg_v4l2: |
| 2608 | v4l2_device_unregister(&dbg->v4l2); |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2609 | error: |
| 2610 | dev_warn(&data->client->dev, "Error initializing T37\n"); |
| 2611 | } |
| 2612 | #else |
| 2613 | static void mxt_debug_init(struct mxt_data *data) |
| 2614 | { |
| 2615 | } |
| 2616 | #endif |
| 2617 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2618 | static int mxt_configure_objects(struct mxt_data *data, |
| 2619 | const struct firmware *cfg) |
| 2620 | { |
| 2621 | struct device *dev = &data->client->dev; |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2622 | int error; |
| 2623 | |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 2624 | error = mxt_init_t7_power_cfg(data); |
| 2625 | if (error) { |
| 2626 | dev_err(dev, "Failed to initialize power cfg\n"); |
| 2627 | return error; |
| 2628 | } |
| 2629 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2630 | if (cfg) { |
| 2631 | error = mxt_update_cfg(data, cfg); |
| 2632 | if (error) |
| 2633 | dev_warn(dev, "Error %d updating config\n", error); |
| 2634 | } |
| 2635 | |
Nick Dyer | b23157d | 2015-04-06 11:25:13 -0700 | [diff] [blame] | 2636 | if (data->multitouch) { |
| 2637 | error = mxt_initialize_input_device(data); |
| 2638 | if (error) |
| 2639 | return error; |
| 2640 | } else { |
| 2641 | dev_warn(dev, "No touch object detected\n"); |
| 2642 | } |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2643 | |
Nick Dyer | d6a3940 | 2016-07-18 18:10:31 -0300 | [diff] [blame] | 2644 | mxt_debug_init(data); |
| 2645 | |
Nick Dyer | 50a77c6 | 2014-07-23 12:38:48 -0700 | [diff] [blame] | 2646 | return 0; |
| 2647 | } |
| 2648 | |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2649 | /* Firmware Version is returned as Major.Minor.Build */ |
| 2650 | static ssize_t mxt_fw_version_show(struct device *dev, |
| 2651 | struct device_attribute *attr, char *buf) |
| 2652 | { |
| 2653 | struct mxt_data *data = dev_get_drvdata(dev); |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2654 | struct mxt_info *info = data->info; |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2655 | return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n", |
| 2656 | info->version >> 4, info->version & 0xf, info->build); |
| 2657 | } |
| 2658 | |
| 2659 | /* Hardware Version is returned as FamilyID.VariantID */ |
| 2660 | static ssize_t mxt_hw_version_show(struct device *dev, |
| 2661 | struct device_attribute *attr, char *buf) |
| 2662 | { |
| 2663 | struct mxt_data *data = dev_get_drvdata(dev); |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2664 | struct mxt_info *info = data->info; |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2665 | return scnprintf(buf, PAGE_SIZE, "%u.%u\n", |
| 2666 | info->family_id, info->variant_id); |
| 2667 | } |
| 2668 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2669 | static ssize_t mxt_show_instance(char *buf, int count, |
| 2670 | struct mxt_object *object, int instance, |
| 2671 | const u8 *val) |
| 2672 | { |
| 2673 | int i; |
| 2674 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 2675 | if (mxt_obj_instances(object) > 1) |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2676 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 2677 | "Instance %u\n", instance); |
| 2678 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 2679 | for (i = 0; i < mxt_obj_size(object); i++) |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2680 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 2681 | "\t[%2u]: %02x (%d)\n", i, val[i], val[i]); |
| 2682 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 2683 | |
| 2684 | return count; |
| 2685 | } |
| 2686 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2687 | static ssize_t mxt_object_show(struct device *dev, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2688 | struct device_attribute *attr, char *buf) |
| 2689 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2690 | struct mxt_data *data = dev_get_drvdata(dev); |
| 2691 | struct mxt_object *object; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2692 | int count = 0; |
| 2693 | int i, j; |
| 2694 | int error; |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2695 | u8 *obuf; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2696 | |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2697 | /* Pre-allocate buffer large enough to hold max sized object. */ |
| 2698 | obuf = kmalloc(256, GFP_KERNEL); |
| 2699 | if (!obuf) |
| 2700 | return -ENOMEM; |
| 2701 | |
| 2702 | error = 0; |
Nick Dyer | 068bdb6 | 2018-05-01 11:40:18 -0700 | [diff] [blame] | 2703 | for (i = 0; i < data->info->object_num; i++) { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2704 | object = data->object_table + i; |
| 2705 | |
Daniel Kurtz | 9163095 | 2012-06-28 21:08:09 +0800 | [diff] [blame] | 2706 | if (!mxt_object_readable(object->type)) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2707 | continue; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2708 | |
Daniel Kurtz | 9163095 | 2012-06-28 21:08:09 +0800 | [diff] [blame] | 2709 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 2710 | "T%u:\n", object->type); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2711 | |
Daniel Kurtz | 1e0c0c5 | 2014-05-18 23:01:12 -0700 | [diff] [blame] | 2712 | for (j = 0; j < mxt_obj_instances(object); j++) { |
| 2713 | u16 size = mxt_obj_size(object); |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2714 | u16 addr = object->start_address + j * size; |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2715 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2716 | error = __mxt_read_reg(data->client, addr, size, obuf); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2717 | if (error) |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2718 | goto done; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2719 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2720 | count = mxt_show_instance(buf, count, object, j, obuf); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2721 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
Daniel Kurtz | 794eb67 | 2012-06-28 21:08:10 +0800 | [diff] [blame] | 2724 | done: |
Daniel Kurtz | 43a91d5 | 2012-06-28 21:08:08 +0800 | [diff] [blame] | 2725 | kfree(obuf); |
| 2726 | return error ?: count; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
Nick Dyer | f2ac6cb | 2014-05-18 23:15:01 -0700 | [diff] [blame] | 2729 | static int mxt_check_firmware_format(struct device *dev, |
| 2730 | const struct firmware *fw) |
| 2731 | { |
| 2732 | unsigned int pos = 0; |
| 2733 | char c; |
| 2734 | |
| 2735 | while (pos < fw->size) { |
| 2736 | c = *(fw->data + pos); |
| 2737 | |
| 2738 | if (c < '0' || (c > '9' && c < 'A') || c > 'F') |
| 2739 | return 0; |
| 2740 | |
| 2741 | pos++; |
| 2742 | } |
| 2743 | |
| 2744 | /* |
| 2745 | * To convert file try: |
| 2746 | * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw |
| 2747 | */ |
| 2748 | dev_err(dev, "Aborting: firmware file must be in binary format\n"); |
| 2749 | |
| 2750 | return -EINVAL; |
| 2751 | } |
| 2752 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2753 | static int mxt_load_fw(struct device *dev, const char *fn) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2754 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2755 | struct mxt_data *data = dev_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2756 | const struct firmware *fw = NULL; |
| 2757 | unsigned int frame_size; |
| 2758 | unsigned int pos = 0; |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2759 | unsigned int retry = 0; |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2760 | unsigned int frame = 0; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2761 | int ret; |
| 2762 | |
| 2763 | ret = request_firmware(&fw, fn, dev); |
| 2764 | if (ret) { |
| 2765 | dev_err(dev, "Unable to open firmware %s\n", fn); |
| 2766 | return ret; |
| 2767 | } |
| 2768 | |
Nick Dyer | f2ac6cb | 2014-05-18 23:15:01 -0700 | [diff] [blame] | 2769 | /* Check for incorrect enc file */ |
| 2770 | ret = mxt_check_firmware_format(dev, fw); |
| 2771 | if (ret) |
| 2772 | goto release_firmware; |
| 2773 | |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2774 | if (!data->in_bootloader) { |
| 2775 | /* Change to the bootloader mode */ |
| 2776 | data->in_bootloader = true; |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2777 | |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2778 | ret = mxt_t6_command(data, MXT_COMMAND_RESET, |
| 2779 | MXT_BOOT_VALUE, false); |
| 2780 | if (ret) |
| 2781 | goto release_firmware; |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2782 | |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2783 | msleep(MXT_RESET_TIME); |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2784 | |
| 2785 | /* Do not need to scan since we know family ID */ |
| 2786 | ret = mxt_lookup_bootloader_address(data, 0); |
| 2787 | if (ret) |
| 2788 | goto release_firmware; |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 2789 | |
| 2790 | mxt_free_input_device(data); |
| 2791 | mxt_free_object_table(data); |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2792 | } else { |
| 2793 | enable_irq(data->irq); |
Nick Dyer | a9fdd1e | 2014-07-23 12:41:58 -0700 | [diff] [blame] | 2794 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2795 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2796 | reinit_completion(&data->bl_completion); |
| 2797 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2798 | ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false); |
| 2799 | if (ret) { |
| 2800 | /* Bootloader may still be unlocked from previous attempt */ |
| 2801 | ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false); |
| 2802 | if (ret) |
| 2803 | goto disable_irq; |
| 2804 | } else { |
| 2805 | dev_info(dev, "Unlocking bootloader\n"); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2806 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2807 | /* Unlock bootloader */ |
Nick Dyer | 8efaa5e | 2014-07-23 12:45:49 -0700 | [diff] [blame] | 2808 | ret = mxt_send_bootloader_cmd(data, true); |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2809 | if (ret) |
| 2810 | goto disable_irq; |
| 2811 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2812 | |
| 2813 | while (pos < fw->size) { |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2814 | ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2815 | if (ret) |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2816 | goto disable_irq; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2817 | |
| 2818 | frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1)); |
| 2819 | |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2820 | /* Take account of CRC bytes */ |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2821 | frame_size += 2; |
| 2822 | |
| 2823 | /* Write one frame to device */ |
Nick Dyer | f28a842 | 2014-05-18 23:10:49 -0700 | [diff] [blame] | 2824 | ret = mxt_bootloader_write(data, fw->data + pos, frame_size); |
| 2825 | if (ret) |
| 2826 | goto disable_irq; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2827 | |
Nick Dyer | 385deb9 | 2014-07-23 12:42:40 -0700 | [diff] [blame] | 2828 | ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true); |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2829 | if (ret) { |
| 2830 | retry++; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2831 | |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2832 | /* Back off by 20ms per retry */ |
| 2833 | msleep(retry * 20); |
| 2834 | |
| 2835 | if (retry > 20) { |
| 2836 | dev_err(dev, "Retry count exceeded\n"); |
| 2837 | goto disable_irq; |
| 2838 | } |
| 2839 | } else { |
| 2840 | retry = 0; |
| 2841 | pos += frame_size; |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2842 | frame++; |
Nick Dyer | f943c74 | 2014-05-18 23:14:20 -0700 | [diff] [blame] | 2843 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2844 | |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2845 | if (frame % 50 == 0) |
| 2846 | dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n", |
| 2847 | frame, pos, fw->size); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2848 | } |
| 2849 | |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2850 | /* Wait for flash. */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2851 | ret = mxt_wait_for_completion(data, &data->bl_completion, |
| 2852 | MXT_FW_RESET_TIME); |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2853 | if (ret) |
| 2854 | goto disable_irq; |
| 2855 | |
Nick Dyer | f477c75 | 2014-05-18 23:14:45 -0700 | [diff] [blame] | 2856 | dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos); |
| 2857 | |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2858 | /* |
| 2859 | * Wait for device to reset. Some bootloader versions do not assert |
| 2860 | * the CHG line after bootloading has finished, so ignore potential |
| 2861 | * errors. |
| 2862 | */ |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2863 | mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME); |
Benson Leung | a0434b7 | 2014-05-18 23:03:09 -0700 | [diff] [blame] | 2864 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2865 | data->in_bootloader = false; |
| 2866 | |
| 2867 | disable_irq: |
| 2868 | disable_irq(data->irq); |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 2869 | release_firmware: |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2870 | release_firmware(fw); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2871 | return ret; |
| 2872 | } |
| 2873 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2874 | static ssize_t mxt_update_fw_store(struct device *dev, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2875 | struct device_attribute *attr, |
| 2876 | const char *buf, size_t count) |
| 2877 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2878 | struct mxt_data *data = dev_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2879 | int error; |
| 2880 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2881 | error = mxt_load_fw(dev, MXT_FW_NAME); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2882 | if (error) { |
| 2883 | dev_err(dev, "The firmware update failed(%d)\n", error); |
| 2884 | count = error; |
| 2885 | } else { |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 2886 | dev_info(dev, "The firmware update succeeded\n"); |
| 2887 | |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 2888 | error = mxt_initialize(data); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 2889 | if (error) |
| 2890 | return error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2891 | } |
| 2892 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2893 | return count; |
| 2894 | } |
| 2895 | |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2896 | static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL); |
| 2897 | static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL); |
Daniel Kurtz | 71b3e93 | 2012-05-08 22:30:14 -0700 | [diff] [blame] | 2898 | static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL); |
| 2899 | static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2900 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2901 | static struct attribute *mxt_attrs[] = { |
Daniel Kurtz | b19fc9e | 2012-06-28 21:08:16 +0800 | [diff] [blame] | 2902 | &dev_attr_fw_version.attr, |
| 2903 | &dev_attr_hw_version.attr, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2904 | &dev_attr_object.attr, |
| 2905 | &dev_attr_update_fw.attr, |
| 2906 | NULL |
| 2907 | }; |
| 2908 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2909 | static const struct attribute_group mxt_attr_group = { |
| 2910 | .attrs = mxt_attrs, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2911 | }; |
| 2912 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2913 | static void mxt_start(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2914 | { |
Dmitry Torokhov | d80808e | 2018-03-20 15:31:25 -0700 | [diff] [blame] | 2915 | switch (data->suspend_mode) { |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 2916 | case MXT_SUSPEND_T9_CTRL: |
| 2917 | mxt_soft_reset(data); |
| 2918 | |
| 2919 | /* Touch enable */ |
| 2920 | /* 0x83 = SCANEN | RPTEN | ENABLE */ |
| 2921 | mxt_write_object(data, |
| 2922 | MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83); |
| 2923 | break; |
| 2924 | |
| 2925 | case MXT_SUSPEND_DEEP_SLEEP: |
| 2926 | default: |
| 2927 | mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN); |
| 2928 | |
| 2929 | /* Recalibrate since chip has been in deep sleep */ |
| 2930 | mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false); |
| 2931 | break; |
| 2932 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2935 | static void mxt_stop(struct mxt_data *data) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2936 | { |
Dmitry Torokhov | d80808e | 2018-03-20 15:31:25 -0700 | [diff] [blame] | 2937 | switch (data->suspend_mode) { |
Nick Dyer | 7f3884f7 | 2015-08-04 16:36:29 -0700 | [diff] [blame] | 2938 | case MXT_SUSPEND_T9_CTRL: |
| 2939 | /* Touch disable */ |
| 2940 | mxt_write_object(data, |
| 2941 | MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0); |
| 2942 | break; |
| 2943 | |
| 2944 | case MXT_SUSPEND_DEEP_SLEEP: |
| 2945 | default: |
| 2946 | mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP); |
| 2947 | break; |
| 2948 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2951 | static int mxt_input_open(struct input_dev *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2952 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2953 | struct mxt_data *data = input_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2954 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2955 | mxt_start(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2956 | |
| 2957 | return 0; |
| 2958 | } |
| 2959 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2960 | static void mxt_input_close(struct input_dev *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2961 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2962 | struct mxt_data *data = input_get_drvdata(dev); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2963 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 2964 | mxt_stop(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2967 | static int mxt_parse_device_properties(struct mxt_data *data) |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2968 | { |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2969 | static const char keymap_property[] = "linux,gpio-keymap"; |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2970 | struct device *dev = &data->client->dev; |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2971 | u32 *keymap; |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2972 | int n_keys; |
| 2973 | int error; |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2974 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2975 | if (device_property_present(dev, keymap_property)) { |
| 2976 | n_keys = device_property_read_u32_array(dev, keymap_property, |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2977 | NULL, 0); |
| 2978 | if (n_keys <= 0) { |
| 2979 | error = n_keys < 0 ? n_keys : -EINVAL; |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2980 | dev_err(dev, "invalid/malformed '%s' property: %d\n", |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2981 | keymap_property, error); |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2982 | return error; |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2983 | } |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2984 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2985 | keymap = devm_kmalloc_array(dev, n_keys, sizeof(*keymap), |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2986 | GFP_KERNEL); |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2987 | if (!keymap) |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2988 | return -ENOMEM; |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2989 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2990 | error = device_property_read_u32_array(dev, keymap_property, |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2991 | keymap, n_keys); |
| 2992 | if (error) { |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2993 | dev_err(dev, "failed to parse '%s' property: %d\n", |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2994 | keymap_property, error); |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2995 | return error; |
Dmitry Torokhov | 93afb1d | 2018-03-20 15:31:26 -0700 | [diff] [blame] | 2996 | } |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 2997 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 2998 | data->t19_keymap = keymap; |
| 2999 | data->t19_num_keys = n_keys; |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 3002 | return 0; |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 3003 | } |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 3004 | |
Dmitry Torokhov | d80808e | 2018-03-20 15:31:25 -0700 | [diff] [blame] | 3005 | static const struct dmi_system_id chromebook_T9_suspend_dmi[] = { |
| 3006 | { |
| 3007 | .matches = { |
| 3008 | DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"), |
| 3009 | DMI_MATCH(DMI_PRODUCT_NAME, "Link"), |
| 3010 | }, |
| 3011 | }, |
| 3012 | { |
| 3013 | .matches = { |
| 3014 | DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"), |
| 3015 | }, |
| 3016 | }, |
| 3017 | { } |
| 3018 | }; |
| 3019 | |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 3020 | 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] | 3021 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3022 | struct mxt_data *data; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3023 | int error; |
| 3024 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 3025 | /* |
Dmitry Torokhov | 7cf432b | 2018-05-03 17:14:51 -0700 | [diff] [blame] | 3026 | * Ignore devices that do not have device properties attached to |
| 3027 | * them, as we need help determining whether we are dealing with |
| 3028 | * touch screen or touchpad. |
| 3029 | * |
| 3030 | * So far on x86 the only users of Atmel touch controllers are |
| 3031 | * Chromebooks, and chromeos_laptop driver will ensure that |
| 3032 | * necessary properties are provided (if firmware does not do that). |
| 3033 | */ |
| 3034 | if (!device_property_present(&client->dev, "compatible")) |
| 3035 | return -ENXIO; |
| 3036 | |
| 3037 | /* |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 3038 | * Ignore ACPI devices representing bootloader mode. |
| 3039 | * |
| 3040 | * This is a bit of a hack: Google Chromebook BIOS creates ACPI |
| 3041 | * devices for both application and bootloader modes, but we are |
| 3042 | * interested in application mode only (if device is in bootloader |
| 3043 | * mode we'll end up switching into application anyway). So far |
| 3044 | * application mode addresses were all above 0x40, so we'll use it |
| 3045 | * as a threshold. |
| 3046 | */ |
| 3047 | if (ACPI_COMPANION(&client->dev) && client->addr < 0x40) |
| 3048 | return -ENXIO; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3049 | |
Sebastian Reichel | 8cc8446 | 2017-07-25 14:08:48 -0700 | [diff] [blame] | 3050 | data = devm_kzalloc(&client->dev, sizeof(struct mxt_data), GFP_KERNEL); |
| 3051 | if (!data) |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 3052 | return -ENOMEM; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3053 | |
Daniel Kurtz | ec02ac2 | 2012-06-28 21:08:02 +0800 | [diff] [blame] | 3054 | snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0", |
| 3055 | client->adapter->nr, client->addr); |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 3056 | |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 3057 | data->client = client; |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 3058 | data->irq = client->irq; |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 3059 | i2c_set_clientdata(client, data); |
Joonyoung Shim | 910d805 | 2011-04-12 23:14:38 -0700 | [diff] [blame] | 3060 | |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 3061 | init_completion(&data->bl_completion); |
Iiro Valkonen | a4a2ef4 | 2014-05-18 23:03:44 -0700 | [diff] [blame] | 3062 | init_completion(&data->reset_completion); |
Nick Dyer | c3f7804 | 2014-05-18 23:04:46 -0700 | [diff] [blame] | 3063 | init_completion(&data->crc_completion); |
Benson Leung | d79e7e4 | 2014-05-18 23:02:52 -0700 | [diff] [blame] | 3064 | |
Dmitry Torokhov | d80808e | 2018-03-20 15:31:25 -0700 | [diff] [blame] | 3065 | data->suspend_mode = dmi_check_system(chromebook_T9_suspend_dmi) ? |
| 3066 | MXT_SUSPEND_T9_CTRL : MXT_SUSPEND_DEEP_SLEEP; |
| 3067 | |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 3068 | error = mxt_parse_device_properties(data); |
| 3069 | if (error) |
| 3070 | return error; |
| 3071 | |
Sebastian Reichel | f657b00 | 2017-07-25 14:11:34 -0700 | [diff] [blame] | 3072 | data->reset_gpio = devm_gpiod_get_optional(&client->dev, |
| 3073 | "reset", GPIOD_OUT_LOW); |
| 3074 | if (IS_ERR(data->reset_gpio)) { |
| 3075 | error = PTR_ERR(data->reset_gpio); |
| 3076 | dev_err(&client->dev, "Failed to get reset gpio: %d\n", error); |
| 3077 | return error; |
| 3078 | } |
| 3079 | |
Sebastian Reichel | 8cc8446 | 2017-07-25 14:08:48 -0700 | [diff] [blame] | 3080 | error = devm_request_threaded_irq(&client->dev, client->irq, |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 3081 | NULL, mxt_interrupt, IRQF_ONESHOT, |
Sebastian Reichel | 8cc8446 | 2017-07-25 14:08:48 -0700 | [diff] [blame] | 3082 | client->name, data); |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 3083 | if (error) { |
| 3084 | dev_err(&client->dev, "Failed to register interrupt\n"); |
Sebastian Reichel | 8cc8446 | 2017-07-25 14:08:48 -0700 | [diff] [blame] | 3085 | return error; |
Nick Dyer | dd24dcf | 2014-07-23 11:25:55 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | disable_irq(client->irq); |
| 3089 | |
Sebastian Reichel | ca1cd36 | 2018-05-24 09:33:36 -0700 | [diff] [blame] | 3090 | if (data->reset_gpio) { |
| 3091 | msleep(MXT_RESET_GPIO_TIME); |
| 3092 | gpiod_set_value(data->reset_gpio, 1); |
| 3093 | msleep(MXT_RESET_INVALID_CHG); |
| 3094 | } |
| 3095 | |
Daniel Kurtz | cb15911 | 2012-06-28 21:08:22 +0800 | [diff] [blame] | 3096 | error = mxt_initialize(data); |
| 3097 | if (error) |
Sebastian Reichel | 8cc8446 | 2017-07-25 14:08:48 -0700 | [diff] [blame] | 3098 | return error; |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3099 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3100 | error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group); |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 3101 | if (error) { |
| 3102 | dev_err(&client->dev, "Failure %d creating sysfs group\n", |
| 3103 | error); |
Nick Dyer | 7a53d60 | 2014-07-23 12:21:26 -0700 | [diff] [blame] | 3104 | goto err_free_object; |
Nick Dyer | 7bed680 | 2014-05-18 23:04:09 -0700 | [diff] [blame] | 3105 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3106 | |
| 3107 | return 0; |
| 3108 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3109 | err_free_object: |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 3110 | mxt_free_input_device(data); |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 3111 | mxt_free_object_table(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3112 | return error; |
| 3113 | } |
| 3114 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 3115 | static int mxt_remove(struct i2c_client *client) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3116 | { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3117 | struct mxt_data *data = i2c_get_clientdata(client); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3118 | |
Sebastian Reichel | 8cc8446 | 2017-07-25 14:08:48 -0700 | [diff] [blame] | 3119 | disable_irq(data->irq); |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3120 | sysfs_remove_group(&client->dev.kobj, &mxt_attr_group); |
Stephen Warren | 58e4aeee | 2014-09-10 10:01:10 -0700 | [diff] [blame] | 3121 | mxt_free_input_device(data); |
Nick Dyer | 5f3f9bc | 2014-07-23 12:46:55 -0700 | [diff] [blame] | 3122 | mxt_free_object_table(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3123 | |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 3127 | static int __maybe_unused mxt_suspend(struct device *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3128 | { |
Dmitry Torokhov | 8b5fce0 | 2010-11-18 00:14:03 -0800 | [diff] [blame] | 3129 | struct i2c_client *client = to_i2c_client(dev); |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3130 | struct mxt_data *data = i2c_get_clientdata(client); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3131 | struct input_dev *input_dev = data->input_dev; |
| 3132 | |
Pan Xinhui | 50fabb0 | 2015-08-04 16:53:04 -0700 | [diff] [blame] | 3133 | if (!input_dev) |
| 3134 | return 0; |
| 3135 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3136 | mutex_lock(&input_dev->mutex); |
| 3137 | |
| 3138 | if (input_dev->users) |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3139 | mxt_stop(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3140 | |
| 3141 | mutex_unlock(&input_dev->mutex); |
| 3142 | |
| 3143 | return 0; |
| 3144 | } |
| 3145 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 3146 | static int __maybe_unused mxt_resume(struct device *dev) |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3147 | { |
Dmitry Torokhov | 8b5fce0 | 2010-11-18 00:14:03 -0800 | [diff] [blame] | 3148 | struct i2c_client *client = to_i2c_client(dev); |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3149 | struct mxt_data *data = i2c_get_clientdata(client); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3150 | struct input_dev *input_dev = data->input_dev; |
| 3151 | |
Pan Xinhui | 50fabb0 | 2015-08-04 16:53:04 -0700 | [diff] [blame] | 3152 | if (!input_dev) |
| 3153 | return 0; |
| 3154 | |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3155 | mutex_lock(&input_dev->mutex); |
| 3156 | |
| 3157 | if (input_dev->users) |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3158 | mxt_start(data); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3159 | |
| 3160 | mutex_unlock(&input_dev->mutex); |
| 3161 | |
| 3162 | return 0; |
| 3163 | } |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3164 | |
Daniel Kurtz | 3a73c81 | 2012-05-08 22:29:14 -0700 | [diff] [blame] | 3165 | static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume); |
| 3166 | |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 3167 | static const struct of_device_id mxt_of_match[] = { |
| 3168 | { .compatible = "atmel,maxtouch", }, |
Javier Martinez Canillas | f6eeb9e | 2018-05-01 11:44:53 -0700 | [diff] [blame] | 3169 | /* Compatibles listed below are deprecated */ |
| 3170 | { .compatible = "atmel,qt602240_ts", }, |
| 3171 | { .compatible = "atmel,atmel_mxt_ts", }, |
| 3172 | { .compatible = "atmel,atmel_mxt_tp", }, |
| 3173 | { .compatible = "atmel,mXT224", }, |
Stephen Warren | 78188be | 2014-07-23 12:23:23 -0700 | [diff] [blame] | 3174 | {}, |
| 3175 | }; |
| 3176 | MODULE_DEVICE_TABLE(of, mxt_of_match); |
| 3177 | |
Dmitry Torokhov | 4f8d808 | 2015-04-07 16:30:01 -0700 | [diff] [blame] | 3178 | #ifdef CONFIG_ACPI |
| 3179 | static const struct acpi_device_id mxt_acpi_id[] = { |
| 3180 | { "ATML0000", 0 }, /* Touchpad */ |
| 3181 | { "ATML0001", 0 }, /* Touchscreen */ |
| 3182 | { } |
| 3183 | }; |
| 3184 | MODULE_DEVICE_TABLE(acpi, mxt_acpi_id); |
| 3185 | #endif |
| 3186 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3187 | static const struct i2c_device_id mxt_id[] = { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3188 | { "qt602240_ts", 0 }, |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3189 | { "atmel_mxt_ts", 0 }, |
Daniel Kurtz | 22dfab7 | 2013-03-07 19:43:33 -0800 | [diff] [blame] | 3190 | { "atmel_mxt_tp", 0 }, |
Javier Martinez Canillas | b7d2105 | 2015-12-11 13:22:39 -0800 | [diff] [blame] | 3191 | { "maxtouch", 0 }, |
Chris Leech | 46ee2a0 | 2011-02-15 13:36:52 -0800 | [diff] [blame] | 3192 | { "mXT224", 0 }, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3193 | { } |
| 3194 | }; |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3195 | MODULE_DEVICE_TABLE(i2c, mxt_id); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3196 | |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3197 | static struct i2c_driver mxt_driver = { |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3198 | .driver = { |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3199 | .name = "atmel_mxt_ts", |
Dmitry Torokhov | 96a938aa | 2018-03-20 15:31:38 -0700 | [diff] [blame] | 3200 | .of_match_table = mxt_of_match, |
Dmitry Torokhov | 4f8d808 | 2015-04-07 16:30:01 -0700 | [diff] [blame] | 3201 | .acpi_match_table = ACPI_PTR(mxt_acpi_id), |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3202 | .pm = &mxt_pm_ops, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3203 | }, |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3204 | .probe = mxt_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 3205 | .remove = mxt_remove, |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3206 | .id_table = mxt_id, |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3207 | }; |
| 3208 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 3209 | module_i2c_driver(mxt_driver); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3210 | |
| 3211 | /* Module information */ |
| 3212 | MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); |
Iiro Valkonen | 7686b10 | 2011-02-02 23:21:58 -0800 | [diff] [blame] | 3213 | MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver"); |
Joonyoung Shim | 4cf51c3 | 2010-07-14 21:55:30 -0700 | [diff] [blame] | 3214 | MODULE_LICENSE("GPL"); |