blob: 0cff8bb2ad75d1d5048963c29efb1141663e5dd7 [file] [log] [blame]
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001/*
Iiro Valkonen7686b102011-02-02 23:21:58 -08002 * Atmel maXTouch Touchscreen driver
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07003 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/module.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070015#include <linux/delay.h>
16#include <linux/firmware.h>
17#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080018#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070019#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070020#include <linux/interrupt.h>
21#include <linux/slab.h>
22
23/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080024#define MXT_VER_20 20
25#define MXT_VER_21 21
26#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070027
28/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080029#define MXT_APP_LOW 0x4a
30#define MXT_APP_HIGH 0x4b
31#define MXT_BOOT_LOW 0x24
32#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070033
34/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080035#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070036
37/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080038#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080039#define MXT_FAMILY_ID 0x00
40#define MXT_VARIANT_ID 0x01
41#define MXT_VERSION 0x02
42#define MXT_BUILD 0x03
43#define MXT_MATRIX_X_SIZE 0x04
44#define MXT_MATRIX_Y_SIZE 0x05
45#define MXT_OBJECT_NUM 0x06
46#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070047
Iiro Valkonen7686b102011-02-02 23:21:58 -080048#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070049
50/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070051#define MXT_DEBUG_DIAGNOSTIC_T37 37
52#define MXT_GEN_MESSAGE_T5 5
53#define MXT_GEN_COMMAND_T6 6
54#define MXT_GEN_POWER_T7 7
55#define MXT_GEN_ACQUIRE_T8 8
56#define MXT_GEN_DATASOURCE_T53 53
57#define MXT_TOUCH_MULTI_T9 9
58#define MXT_TOUCH_KEYARRAY_T15 15
59#define MXT_TOUCH_PROXIMITY_T23 23
60#define MXT_TOUCH_PROXKEY_T52 52
61#define MXT_PROCI_GRIPFACE_T20 20
62#define MXT_PROCG_NOISE_T22 22
63#define MXT_PROCI_ONETOUCH_T24 24
64#define MXT_PROCI_TWOTOUCH_T27 27
65#define MXT_PROCI_GRIP_T40 40
66#define MXT_PROCI_PALM_T41 41
67#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
68#define MXT_PROCI_STYLUS_T47 47
69#define MXT_PROCG_NOISESUPPRESSION_T48 48
70#define MXT_SPT_COMMSCONFIG_T18 18
71#define MXT_SPT_GPIOPWM_T19 19
72#define MXT_SPT_SELFTEST_T25 25
73#define MXT_SPT_CTECONFIG_T28 28
74#define MXT_SPT_USERDATA_T38 38
75#define MXT_SPT_DIGITIZER_T43 43
76#define MXT_SPT_MESSAGECOUNT_T44 44
77#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070078
Iiro Valkonen81c88a72011-07-04 03:08:25 -070079/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080080#define MXT_COMMAND_RESET 0
81#define MXT_COMMAND_BACKUPNV 1
82#define MXT_COMMAND_CALIBRATE 2
83#define MXT_COMMAND_REPORTALL 3
84#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070085
Iiro Valkonen81c88a72011-07-04 03:08:25 -070086/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080087#define MXT_POWER_IDLEACQINT 0
88#define MXT_POWER_ACTVACQINT 1
89#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070090
Iiro Valkonen81c88a72011-07-04 03:08:25 -070091/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080092#define MXT_ACQUIRE_CHRGTIME 0
93#define MXT_ACQUIRE_TCHDRIFT 2
94#define MXT_ACQUIRE_DRIFTST 3
95#define MXT_ACQUIRE_TCHAUTOCAL 4
96#define MXT_ACQUIRE_SYNC 5
97#define MXT_ACQUIRE_ATCHCALST 6
98#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070099
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700100/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800101#define MXT_TOUCH_CTRL 0
102#define MXT_TOUCH_XORIGIN 1
103#define MXT_TOUCH_YORIGIN 2
104#define MXT_TOUCH_XSIZE 3
105#define MXT_TOUCH_YSIZE 4
106#define MXT_TOUCH_BLEN 6
107#define MXT_TOUCH_TCHTHR 7
108#define MXT_TOUCH_TCHDI 8
109#define MXT_TOUCH_ORIENT 9
110#define MXT_TOUCH_MOVHYSTI 11
111#define MXT_TOUCH_MOVHYSTN 12
112#define MXT_TOUCH_NUMTOUCH 14
113#define MXT_TOUCH_MRGHYST 15
114#define MXT_TOUCH_MRGTHR 16
115#define MXT_TOUCH_AMPHYST 17
116#define MXT_TOUCH_XRANGE_LSB 18
117#define MXT_TOUCH_XRANGE_MSB 19
118#define MXT_TOUCH_YRANGE_LSB 20
119#define MXT_TOUCH_YRANGE_MSB 21
120#define MXT_TOUCH_XLOCLIP 22
121#define MXT_TOUCH_XHICLIP 23
122#define MXT_TOUCH_YLOCLIP 24
123#define MXT_TOUCH_YHICLIP 25
124#define MXT_TOUCH_XEDGECTRL 26
125#define MXT_TOUCH_XEDGEDIST 27
126#define MXT_TOUCH_YEDGECTRL 28
127#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700128#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700129
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700130/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800131#define MXT_GRIPFACE_CTRL 0
132#define MXT_GRIPFACE_XLOGRIP 1
133#define MXT_GRIPFACE_XHIGRIP 2
134#define MXT_GRIPFACE_YLOGRIP 3
135#define MXT_GRIPFACE_YHIGRIP 4
136#define MXT_GRIPFACE_MAXTCHS 5
137#define MXT_GRIPFACE_SZTHR1 7
138#define MXT_GRIPFACE_SZTHR2 8
139#define MXT_GRIPFACE_SHPTHR1 9
140#define MXT_GRIPFACE_SHPTHR2 10
141#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700142
Iiro Valkonen7686b102011-02-02 23:21:58 -0800143/* MXT_PROCI_NOISE field */
144#define MXT_NOISE_CTRL 0
145#define MXT_NOISE_OUTFLEN 1
146#define MXT_NOISE_GCAFUL_LSB 3
147#define MXT_NOISE_GCAFUL_MSB 4
148#define MXT_NOISE_GCAFLL_LSB 5
149#define MXT_NOISE_GCAFLL_MSB 6
150#define MXT_NOISE_ACTVGCAFVALID 7
151#define MXT_NOISE_NOISETHR 8
152#define MXT_NOISE_FREQHOPSCALE 10
153#define MXT_NOISE_FREQ0 11
154#define MXT_NOISE_FREQ1 12
155#define MXT_NOISE_FREQ2 13
156#define MXT_NOISE_FREQ3 14
157#define MXT_NOISE_FREQ4 15
158#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700159
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700160/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800161#define MXT_COMMS_CTRL 0
162#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700163
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700164/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800165#define MXT_CTE_CTRL 0
166#define MXT_CTE_CMD 1
167#define MXT_CTE_MODE 2
168#define MXT_CTE_IDLEGCAFDEPTH 3
169#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700170#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700171
Iiro Valkonen7686b102011-02-02 23:21:58 -0800172#define MXT_VOLTAGE_DEFAULT 2700000
173#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700174
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700175/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800176#define MXT_BOOT_VALUE 0xa5
177#define MXT_BACKUP_VALUE 0x55
Linus Torvalds8343bce2013-03-09 10:31:01 -0800178#define MXT_BACKUP_TIME 50 /* msec */
179#define MXT_RESET_TIME 200 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700180
Iiro Valkonen7686b102011-02-02 23:21:58 -0800181#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700182
183/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800184#define MXT_UNLOCK_CMD_MSB 0xaa
185#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700186
187/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800188#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
189#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
190#define MXT_FRAME_CRC_CHECK 0x02
191#define MXT_FRAME_CRC_FAIL 0x03
192#define MXT_FRAME_CRC_PASS 0x04
193#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
194#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700195
196/* Touch status */
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800197#define MXT_UNGRIP (1 << 0)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800198#define MXT_SUPPRESS (1 << 1)
199#define MXT_AMP (1 << 2)
200#define MXT_VECTOR (1 << 3)
201#define MXT_MOVE (1 << 4)
202#define MXT_RELEASE (1 << 5)
203#define MXT_PRESS (1 << 6)
204#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700205
Joonyoung Shim910d8052011-04-12 23:14:38 -0700206/* Touch orient bits */
207#define MXT_XY_SWITCH (1 << 0)
208#define MXT_X_INVERT (1 << 1)
209#define MXT_Y_INVERT (1 << 2)
210
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700211/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800212#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700213
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800214#define MXT_PIXELS_PER_MM 20
215
Iiro Valkonen7686b102011-02-02 23:21:58 -0800216struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217 u8 family_id;
218 u8 variant_id;
219 u8 version;
220 u8 build;
221 u8 matrix_xsize;
222 u8 matrix_ysize;
223 u8 object_num;
224};
225
Iiro Valkonen7686b102011-02-02 23:21:58 -0800226struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700227 u8 type;
228 u16 start_address;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800229 u8 size; /* Size of each instance - 1 */
230 u8 instances; /* Number of instances - 1 */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800232} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700233
Iiro Valkonen7686b102011-02-02 23:21:58 -0800234struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235 u8 reportid;
236 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700237};
238
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700239/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800240struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700241 struct i2c_client *client;
242 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800243 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800244 const struct mxt_platform_data *pdata;
245 struct mxt_object *object_table;
246 struct mxt_info info;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800247
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700248 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700249 unsigned int max_x;
250 unsigned int max_y;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800251
252 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800253 u8 T6_reportid;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800254 u8 T9_reportid_min;
255 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800256 u8 T19_reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700257};
258
Iiro Valkonen7686b102011-02-02 23:21:58 -0800259static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700260{
261 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700262 case MXT_GEN_COMMAND_T6:
263 case MXT_GEN_POWER_T7:
264 case MXT_GEN_ACQUIRE_T8:
265 case MXT_GEN_DATASOURCE_T53:
266 case MXT_TOUCH_MULTI_T9:
267 case MXT_TOUCH_KEYARRAY_T15:
268 case MXT_TOUCH_PROXIMITY_T23:
269 case MXT_TOUCH_PROXKEY_T52:
270 case MXT_PROCI_GRIPFACE_T20:
271 case MXT_PROCG_NOISE_T22:
272 case MXT_PROCI_ONETOUCH_T24:
273 case MXT_PROCI_TWOTOUCH_T27:
274 case MXT_PROCI_GRIP_T40:
275 case MXT_PROCI_PALM_T41:
276 case MXT_PROCI_TOUCHSUPPRESSION_T42:
277 case MXT_PROCI_STYLUS_T47:
278 case MXT_PROCG_NOISESUPPRESSION_T48:
279 case MXT_SPT_COMMSCONFIG_T18:
280 case MXT_SPT_GPIOPWM_T19:
281 case MXT_SPT_SELFTEST_T25:
282 case MXT_SPT_CTECONFIG_T28:
283 case MXT_SPT_USERDATA_T38:
284 case MXT_SPT_DIGITIZER_T43:
285 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700286 return true;
287 default:
288 return false;
289 }
290}
291
Iiro Valkonen7686b102011-02-02 23:21:58 -0800292static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700293{
294 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700295 case MXT_GEN_COMMAND_T6:
296 case MXT_GEN_POWER_T7:
297 case MXT_GEN_ACQUIRE_T8:
298 case MXT_TOUCH_MULTI_T9:
299 case MXT_TOUCH_KEYARRAY_T15:
300 case MXT_TOUCH_PROXIMITY_T23:
301 case MXT_TOUCH_PROXKEY_T52:
302 case MXT_PROCI_GRIPFACE_T20:
303 case MXT_PROCG_NOISE_T22:
304 case MXT_PROCI_ONETOUCH_T24:
305 case MXT_PROCI_TWOTOUCH_T27:
306 case MXT_PROCI_GRIP_T40:
307 case MXT_PROCI_PALM_T41:
308 case MXT_PROCI_TOUCHSUPPRESSION_T42:
309 case MXT_PROCI_STYLUS_T47:
310 case MXT_PROCG_NOISESUPPRESSION_T48:
311 case MXT_SPT_COMMSCONFIG_T18:
312 case MXT_SPT_GPIOPWM_T19:
313 case MXT_SPT_SELFTEST_T25:
314 case MXT_SPT_CTECONFIG_T28:
315 case MXT_SPT_DIGITIZER_T43:
316 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700317 return true;
318 default:
319 return false;
320 }
321}
322
Iiro Valkonen7686b102011-02-02 23:21:58 -0800323static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700324 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700325{
Andy Shevchenkoeb007c82012-10-04 00:02:59 -0700326 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
327 message->reportid, 7, message->message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700328}
329
Iiro Valkonen7686b102011-02-02 23:21:58 -0800330static int mxt_check_bootloader(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700331 unsigned int state)
332{
333 u8 val;
334
335recheck:
336 if (i2c_master_recv(client, &val, 1) != 1) {
337 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
338 return -EIO;
339 }
340
341 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800342 case MXT_WAITING_BOOTLOAD_CMD:
343 case MXT_WAITING_FRAME_DATA:
344 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700345 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800346 case MXT_FRAME_CRC_PASS:
347 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700348 goto recheck;
349 break;
350 default:
351 return -EINVAL;
352 }
353
354 if (val != state) {
355 dev_err(&client->dev, "Unvalid bootloader mode state\n");
356 return -EINVAL;
357 }
358
359 return 0;
360}
361
Iiro Valkonen7686b102011-02-02 23:21:58 -0800362static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700363{
364 u8 buf[2];
365
Iiro Valkonen7686b102011-02-02 23:21:58 -0800366 buf[0] = MXT_UNLOCK_CMD_LSB;
367 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700368
369 if (i2c_master_send(client, buf, 2) != 2) {
370 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
371 return -EIO;
372 }
373
374 return 0;
375}
376
Iiro Valkonen7686b102011-02-02 23:21:58 -0800377static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700378 const u8 *data, unsigned int frame_size)
379{
380 if (i2c_master_send(client, data, frame_size) != frame_size) {
381 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
382 return -EIO;
383 }
384
385 return 0;
386}
387
Iiro Valkonen7686b102011-02-02 23:21:58 -0800388static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700389 u16 reg, u16 len, void *val)
390{
391 struct i2c_msg xfer[2];
392 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800393 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700394
395 buf[0] = reg & 0xff;
396 buf[1] = (reg >> 8) & 0xff;
397
398 /* Write register */
399 xfer[0].addr = client->addr;
400 xfer[0].flags = 0;
401 xfer[0].len = 2;
402 xfer[0].buf = buf;
403
404 /* Read data */
405 xfer[1].addr = client->addr;
406 xfer[1].flags = I2C_M_RD;
407 xfer[1].len = len;
408 xfer[1].buf = val;
409
Daniel Kurtz771733e2012-06-28 21:08:11 +0800410 ret = i2c_transfer(client->adapter, xfer, 2);
411 if (ret == 2) {
412 ret = 0;
413 } else {
414 if (ret >= 0)
415 ret = -EIO;
416 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
417 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700418 }
419
Daniel Kurtz771733e2012-06-28 21:08:11 +0800420 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700421}
422
Iiro Valkonen7686b102011-02-02 23:21:58 -0800423static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700424{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800425 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700426}
427
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800428static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
429 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700430{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800431 u8 *buf;
432 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800433 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700434
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800435 count = len + 2;
436 buf = kmalloc(count, GFP_KERNEL);
437 if (!buf)
438 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700439
440 buf[0] = reg & 0xff;
441 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800442 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700443
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800444 ret = i2c_master_send(client, buf, count);
445 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800446 ret = 0;
447 } else {
448 if (ret >= 0)
449 ret = -EIO;
450 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
451 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700452 }
453
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800454 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800455 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700456}
457
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800458static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700459{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800460 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700461}
462
Iiro Valkonen7686b102011-02-02 23:21:58 -0800463static struct mxt_object *
464mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700465{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800466 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700467 int i;
468
469 for (i = 0; i < data->info.object_num; i++) {
470 object = data->object_table + i;
471 if (object->type == type)
472 return object;
473 }
474
475 dev_err(&data->client->dev, "Invalid object type\n");
476 return NULL;
477}
478
Iiro Valkonen7686b102011-02-02 23:21:58 -0800479static int mxt_read_message(struct mxt_data *data,
480 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700481{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800482 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700483 u16 reg;
484
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700485 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700486 if (!object)
487 return -EINVAL;
488
489 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800490 return __mxt_read_reg(data->client, reg,
491 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700492}
493
Iiro Valkonen7686b102011-02-02 23:21:58 -0800494static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700495 u8 type, u8 offset, u8 val)
496{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800497 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498 u16 reg;
499
Iiro Valkonen7686b102011-02-02 23:21:58 -0800500 object = mxt_get_object(data, type);
Daniel Kurtzd1ff3202012-05-08 22:38:52 -0700501 if (!object || offset >= object->size + 1)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700502 return -EINVAL;
503
504 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800505 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700506}
507
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800508static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
509{
510 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700511 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800512 bool button;
513 int i;
514
515 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700516 for (i = 0; i < pdata->t19_num_keys; i++) {
517 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800518 continue;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700519 button = !(message->message[0] & (1 << i));
520 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800521 }
522}
523
Iiro Valkonen7686b102011-02-02 23:21:58 -0800524static void mxt_input_touchevent(struct mxt_data *data,
525 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700526{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700527 struct device *dev = &data->client->dev;
528 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800529 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700530 int x;
531 int y;
532 int area;
Yufeng Shen28ac2932011-08-16 00:40:54 -0700533 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700534
Joonyoung Shim910d8052011-04-12 23:14:38 -0700535 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
536 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
537 if (data->max_x < 1024)
538 x = x >> 2;
539 if (data->max_y < 1024)
540 y = y >> 2;
541
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700542 area = message->message[4];
Yufeng Shen28ac2932011-08-16 00:40:54 -0700543 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700544
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800545 dev_dbg(dev,
546 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
547 id,
548 (status & MXT_DETECT) ? 'D' : '.',
549 (status & MXT_PRESS) ? 'P' : '.',
550 (status & MXT_RELEASE) ? 'R' : '.',
551 (status & MXT_MOVE) ? 'M' : '.',
552 (status & MXT_VECTOR) ? 'V' : '.',
553 (status & MXT_AMP) ? 'A' : '.',
554 (status & MXT_SUPPRESS) ? 'S' : '.',
555 (status & MXT_UNGRIP) ? 'U' : '.',
556 x, y, area, pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800558 input_mt_slot(input_dev, id);
559 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
560 status & MXT_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800562 if (status & MXT_DETECT) {
563 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
564 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
565 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
566 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
567 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700568}
569
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800570static unsigned mxt_extract_T6_csum(const u8 *csum)
571{
572 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
573}
574
Daniel Kurtz04a79182012-06-28 21:08:21 +0800575static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
576{
577 u8 id = msg->reportid;
578 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700579}
580
Iiro Valkonen7686b102011-02-02 23:21:58 -0800581static irqreturn_t mxt_interrupt(int irq, void *dev_id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800583 struct mxt_data *data = dev_id;
584 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800585 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700586 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700587 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800588 bool update_input = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589
590 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800591 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700592 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700593 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700594 }
595
596 reportid = message.reportid;
597
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800598 if (reportid == data->T6_reportid) {
599 u8 status = payload[0];
600 unsigned csum = mxt_extract_T6_csum(&payload[1]);
601 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
602 status, csum);
603 } else if (mxt_is_T9_message(data, &message)) {
604 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800605 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800606 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800607 } else if (message.reportid == data->T19_reportid) {
608 mxt_input_button(data, &message);
609 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800610 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800611 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800612 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700613 } while (reportid != 0xff);
614
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800615 if (update_input) {
616 input_mt_report_pointer_emulation(data->input_dev, false);
617 input_sync(data->input_dev);
618 }
619
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700620 return IRQ_HANDLED;
621}
622
Iiro Valkonen7686b102011-02-02 23:21:58 -0800623static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700624{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800625 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800626 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700627 struct device *dev = &data->client->dev;
628 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800629 int i, size;
630 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700631
Iiro Valkonen71749f52011-02-15 13:36:52 -0800632 if (!pdata->config) {
633 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
634 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700635 }
636
637 for (i = 0; i < data->info.object_num; i++) {
638 object = data->object_table + i;
639
Iiro Valkonen7686b102011-02-02 23:21:58 -0800640 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641 continue;
642
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800643 size = (object->size + 1) * (object->instances + 1);
644 if (index + size > pdata->config_length) {
645 dev_err(dev, "Not enough config data!\n");
646 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800647 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800648
649 ret = __mxt_write_reg(data->client, object->start_address,
650 size, &pdata->config[index]);
651 if (ret)
652 return ret;
653 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700654 }
655
656 return 0;
657}
658
Iiro Valkonen7686b102011-02-02 23:21:58 -0800659static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700660{
661 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800662 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700663 int count = 10;
664 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700665
666 /* Read dummy message to make high CHG pin */
667 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800668 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700669 if (error)
670 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800671 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700672
673 if (!count) {
674 dev_err(dev, "CHG pin isn't cleared\n");
675 return -EBUSY;
676 }
677
678 return 0;
679}
680
Iiro Valkonen7686b102011-02-02 23:21:58 -0800681static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700682{
683 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800684 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700685 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700686
Daniel Kurtz23003a842012-06-28 21:08:14 +0800687 /* Read 7-byte info block starting at address 0 */
688 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700689 if (error)
690 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691
692 return 0;
693}
694
Iiro Valkonen7686b102011-02-02 23:21:58 -0800695static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700696{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800697 struct i2c_client *client = data->client;
698 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700699 int error;
700 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800701 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800703 table_size = data->info.object_num * sizeof(struct mxt_object);
704 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
705 data->object_table);
706 if (error)
707 return error;
708
709 /* Valid Report IDs start counting from 1 */
710 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700711 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800712 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800713 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700714
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800715 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700716
717 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800718 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700719 reportid += object->num_report_ids *
720 (object->instances + 1);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800721 max_id = reportid - 1;
722 } else {
723 min_id = 0;
724 max_id = 0;
725 }
726
727 dev_dbg(&data->client->dev,
728 "Type %2d Start %3d Size %3d Instances %2d ReportIDs %3u : %3u\n",
729 object->type, object->start_address, object->size + 1,
730 object->instances + 1, min_id, max_id);
731
732 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800733 case MXT_GEN_COMMAND_T6:
734 data->T6_reportid = min_id;
735 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800736 case MXT_TOUCH_MULTI_T9:
737 data->T9_reportid_min = min_id;
738 data->T9_reportid_max = max_id;
739 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800740 case MXT_SPT_GPIOPWM_T19:
741 data->T19_reportid = min_id;
742 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700743 }
744 }
745
746 return 0;
747}
748
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800749static void mxt_free_object_table(struct mxt_data *data)
750{
751 kfree(data->object_table);
752 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800753 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800754 data->T9_reportid_min = 0;
755 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800756 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800757}
758
Iiro Valkonen7686b102011-02-02 23:21:58 -0800759static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700760{
761 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800762 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700763 int error;
764 u8 val;
765
Iiro Valkonen7686b102011-02-02 23:21:58 -0800766 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700767 if (error)
768 return error;
769
770 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800771 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700772 GFP_KERNEL);
773 if (!data->object_table) {
774 dev_err(&client->dev, "Failed to allocate memory\n");
775 return -ENOMEM;
776 }
777
778 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700780 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800781 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700782
783 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800784 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700785 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800786 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700787
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700788 /* Backup to memory */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700789 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800790 MXT_COMMAND_BACKUPNV,
791 MXT_BACKUP_VALUE);
792 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700793
794 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700795 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800796 MXT_COMMAND_RESET, 1);
797 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700798
799 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800800 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700801 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800802 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700803 info->matrix_xsize = val;
804
Iiro Valkonen7686b102011-02-02 23:21:58 -0800805 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700806 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800807 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700808 info->matrix_ysize = val;
809
810 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800811 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
812 info->family_id, info->variant_id, info->version >> 4,
813 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700814
815 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800816 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700817 info->matrix_xsize, info->matrix_ysize,
818 info->object_num);
819
820 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800821
822err_free_object_table:
823 mxt_free_object_table(data);
824 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700825}
826
Joonyoung Shim910d8052011-04-12 23:14:38 -0700827static void mxt_calc_resolution(struct mxt_data *data)
828{
829 unsigned int max_x = data->pdata->x_size - 1;
830 unsigned int max_y = data->pdata->y_size - 1;
831
832 if (data->pdata->orient & MXT_XY_SWITCH) {
833 data->max_x = max_y;
834 data->max_y = max_x;
835 } else {
836 data->max_x = max_x;
837 data->max_y = max_y;
838 }
839}
840
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +0800841/* Firmware Version is returned as Major.Minor.Build */
842static ssize_t mxt_fw_version_show(struct device *dev,
843 struct device_attribute *attr, char *buf)
844{
845 struct mxt_data *data = dev_get_drvdata(dev);
846 struct mxt_info *info = &data->info;
847 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
848 info->version >> 4, info->version & 0xf, info->build);
849}
850
851/* Hardware Version is returned as FamilyID.VariantID */
852static ssize_t mxt_hw_version_show(struct device *dev,
853 struct device_attribute *attr, char *buf)
854{
855 struct mxt_data *data = dev_get_drvdata(dev);
856 struct mxt_info *info = &data->info;
857 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
858 info->family_id, info->variant_id);
859}
860
Daniel Kurtz794eb672012-06-28 21:08:10 +0800861static ssize_t mxt_show_instance(char *buf, int count,
862 struct mxt_object *object, int instance,
863 const u8 *val)
864{
865 int i;
866
867 if (object->instances > 0)
868 count += scnprintf(buf + count, PAGE_SIZE - count,
869 "Instance %u\n", instance);
870
871 for (i = 0; i < object->size + 1; i++)
872 count += scnprintf(buf + count, PAGE_SIZE - count,
873 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
874 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
875
876 return count;
877}
878
Iiro Valkonen7686b102011-02-02 23:21:58 -0800879static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700880 struct device_attribute *attr, char *buf)
881{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800882 struct mxt_data *data = dev_get_drvdata(dev);
883 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700884 int count = 0;
885 int i, j;
886 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800887 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700888
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800889 /* Pre-allocate buffer large enough to hold max sized object. */
890 obuf = kmalloc(256, GFP_KERNEL);
891 if (!obuf)
892 return -ENOMEM;
893
894 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700895 for (i = 0; i < data->info.object_num; i++) {
896 object = data->object_table + i;
897
Daniel Kurtz91630952012-06-28 21:08:09 +0800898 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700899 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700900
Daniel Kurtz91630952012-06-28 21:08:09 +0800901 count += scnprintf(buf + count, PAGE_SIZE - count,
902 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700903
Daniel Kurtz794eb672012-06-28 21:08:10 +0800904 for (j = 0; j < object->instances + 1; j++) {
905 u16 size = object->size + 1;
906 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800907
Daniel Kurtz794eb672012-06-28 21:08:10 +0800908 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700909 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800910 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700911
Daniel Kurtz794eb672012-06-28 21:08:10 +0800912 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700913 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700914 }
915
Daniel Kurtz794eb672012-06-28 21:08:10 +0800916done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800917 kfree(obuf);
918 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700919}
920
Iiro Valkonen7686b102011-02-02 23:21:58 -0800921static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700922{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800923 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700924 struct i2c_client *client = data->client;
925 const struct firmware *fw = NULL;
926 unsigned int frame_size;
927 unsigned int pos = 0;
928 int ret;
929
930 ret = request_firmware(&fw, fn, dev);
931 if (ret) {
932 dev_err(dev, "Unable to open firmware %s\n", fn);
933 return ret;
934 }
935
936 /* Change to the bootloader mode */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700937 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800938 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
939 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700940
941 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800942 if (client->addr == MXT_APP_LOW)
943 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700944 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800945 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700946
Iiro Valkonen7686b102011-02-02 23:21:58 -0800947 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700948 if (ret)
949 goto out;
950
951 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800952 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700953
954 while (pos < fw->size) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800955 ret = mxt_check_bootloader(client,
956 MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700957 if (ret)
958 goto out;
959
960 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
961
962 /* We should add 2 at frame size as the the firmware data is not
963 * included the CRC bytes.
964 */
965 frame_size += 2;
966
967 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800968 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700969
Iiro Valkonen7686b102011-02-02 23:21:58 -0800970 ret = mxt_check_bootloader(client,
971 MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700972 if (ret)
973 goto out;
974
975 pos += frame_size;
976
977 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
978 }
979
980out:
981 release_firmware(fw);
982
983 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800984 if (client->addr == MXT_BOOT_LOW)
985 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700986 else
Iiro Valkonen7686b102011-02-02 23:21:58 -0800987 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700988
989 return ret;
990}
991
Iiro Valkonen7686b102011-02-02 23:21:58 -0800992static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700993 struct device_attribute *attr,
994 const char *buf, size_t count)
995{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800996 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700997 int error;
998
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700999 disable_irq(data->irq);
1000
Iiro Valkonen7686b102011-02-02 23:21:58 -08001001 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001002 if (error) {
1003 dev_err(dev, "The firmware update failed(%d)\n", error);
1004 count = error;
1005 } else {
1006 dev_dbg(dev, "The firmware update succeeded\n");
1007
1008 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001009 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001011 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001012
Iiro Valkonen7686b102011-02-02 23:21:58 -08001013 mxt_initialize(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001014 }
1015
1016 enable_irq(data->irq);
1017
Iiro Valkonen08960a02011-04-12 23:16:40 -07001018 error = mxt_make_highchg(data);
1019 if (error)
1020 return error;
1021
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022 return count;
1023}
1024
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001025static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1026static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001027static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1028static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001029
Iiro Valkonen7686b102011-02-02 23:21:58 -08001030static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001031 &dev_attr_fw_version.attr,
1032 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001033 &dev_attr_object.attr,
1034 &dev_attr_update_fw.attr,
1035 NULL
1036};
1037
Iiro Valkonen7686b102011-02-02 23:21:58 -08001038static const struct attribute_group mxt_attr_group = {
1039 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040};
1041
Iiro Valkonen7686b102011-02-02 23:21:58 -08001042static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001043{
1044 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001045 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001046 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001047}
1048
Iiro Valkonen7686b102011-02-02 23:21:58 -08001049static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001050{
1051 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001052 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001053 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054}
1055
Iiro Valkonen7686b102011-02-02 23:21:58 -08001056static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001057{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001058 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001059
Iiro Valkonen7686b102011-02-02 23:21:58 -08001060 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061
1062 return 0;
1063}
1064
Iiro Valkonen7686b102011-02-02 23:21:58 -08001065static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001066{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001067 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001068
Iiro Valkonen7686b102011-02-02 23:21:58 -08001069 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001070}
1071
Bill Pemberton5298cc42012-11-23 21:38:25 -08001072static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001073 const struct i2c_device_id *id)
1074{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001075 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001076 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001077 struct input_dev *input_dev;
1078 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001079 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001080 unsigned int mt_flags = 0;
1081 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001082
Iiro Valkonen919ed892011-02-15 13:36:52 -08001083 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001084 return -EINVAL;
1085
Iiro Valkonen7686b102011-02-02 23:21:58 -08001086 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001087 input_dev = input_allocate_device();
1088 if (!data || !input_dev) {
1089 dev_err(&client->dev, "Failed to allocate memory\n");
1090 error = -ENOMEM;
1091 goto err_free_mem;
1092 }
1093
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001094 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001095 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1096 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001097
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001098 input_dev->phys = data->phys;
1099
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100 input_dev->id.bustype = BUS_I2C;
1101 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001102 input_dev->open = mxt_input_open;
1103 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104
Joonyoung Shim910d8052011-04-12 23:14:38 -07001105 data->client = client;
1106 data->input_dev = input_dev;
1107 data->pdata = pdata;
1108 data->irq = client->irq;
1109
1110 mxt_calc_resolution(data);
1111
Daniel Kurtzcb159112012-06-28 21:08:22 +08001112 error = mxt_initialize(data);
1113 if (error)
1114 goto err_free_mem;
1115
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001116 __set_bit(EV_ABS, input_dev->evbit);
1117 __set_bit(EV_KEY, input_dev->evbit);
1118 __set_bit(BTN_TOUCH, input_dev->keybit);
1119
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001120 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001121 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1122
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001123 for (i = 0; i < pdata->t19_num_keys; i++)
1124 if (pdata->t19_keymap[i] != KEY_RESERVED)
1125 input_set_capability(input_dev, EV_KEY,
1126 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001127
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001128 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001129
1130 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1131 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1132 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1133 MXT_PIXELS_PER_MM);
1134 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1135 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001136
1137 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001138 }
1139
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001140 /* For single touch */
1141 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001142 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001143 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001144 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001145 input_set_abs_params(input_dev, ABS_PRESSURE,
1146 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001147
1148 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001149 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001150 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001151 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001152 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001153 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001154 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001155 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001156 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001157 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001158 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001159 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1160 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001161
1162 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001163 i2c_set_clientdata(client, data);
1164
Iiro Valkonen7686b102011-02-02 23:21:58 -08001165 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001166 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001167 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001168 if (error) {
1169 dev_err(&client->dev, "Failed to register interrupt\n");
1170 goto err_free_object;
1171 }
1172
Iiro Valkonen08960a02011-04-12 23:16:40 -07001173 error = mxt_make_highchg(data);
1174 if (error)
1175 goto err_free_irq;
1176
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001177 error = input_register_device(input_dev);
1178 if (error)
1179 goto err_free_irq;
1180
Iiro Valkonen7686b102011-02-02 23:21:58 -08001181 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001182 if (error)
1183 goto err_unregister_device;
1184
1185 return 0;
1186
1187err_unregister_device:
1188 input_unregister_device(input_dev);
1189 input_dev = NULL;
1190err_free_irq:
1191 free_irq(client->irq, data);
1192err_free_object:
1193 kfree(data->object_table);
1194err_free_mem:
1195 input_free_device(input_dev);
1196 kfree(data);
1197 return error;
1198}
1199
Bill Pembertone2619cf2012-11-23 21:50:47 -08001200static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001201{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001202 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001203
Iiro Valkonen7686b102011-02-02 23:21:58 -08001204 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001205 free_irq(data->irq, data);
1206 input_unregister_device(data->input_dev);
1207 kfree(data->object_table);
1208 kfree(data);
1209
1210 return 0;
1211}
1212
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001213#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001214static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001215{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001216 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001217 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001218 struct input_dev *input_dev = data->input_dev;
1219
1220 mutex_lock(&input_dev->mutex);
1221
1222 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001223 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001224
1225 mutex_unlock(&input_dev->mutex);
1226
1227 return 0;
1228}
1229
Iiro Valkonen7686b102011-02-02 23:21:58 -08001230static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001231{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001232 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001233 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001234 struct input_dev *input_dev = data->input_dev;
1235
1236 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001237 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001238 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001239
Iiro Valkonen7686b102011-02-02 23:21:58 -08001240 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001241
1242 mutex_lock(&input_dev->mutex);
1243
1244 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001245 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001246
1247 mutex_unlock(&input_dev->mutex);
1248
1249 return 0;
1250}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001251#endif
1252
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001253static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1254
Iiro Valkonen7686b102011-02-02 23:21:58 -08001255static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001256 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001257 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001258 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001259 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001260 { }
1261};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001262MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001263
Iiro Valkonen7686b102011-02-02 23:21:58 -08001264static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001265 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001266 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001267 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001268 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001269 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001270 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001271 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001272 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001273};
1274
Axel Lin1b92c1c2012-03-16 23:05:41 -07001275module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001276
1277/* Module information */
1278MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001279MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001280MODULE_LICENSE("GPL");