blob: 7f51d39ce2fb6e5b1990b1b93c75376a3319db84 [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
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -07005 * Copyright (C) 2012 Google, Inc.
6 *
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07007 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/module.h>
Benson Leungd79e7e42014-05-18 23:02:52 -070017#include <linux/init.h>
18#include <linux/completion.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070019#include <linux/delay.h>
20#include <linux/firmware.h>
21#include <linux/i2c.h>
Dmitry Torokhov964de522011-02-02 23:21:58 -080022#include <linux/i2c/atmel_mxt_ts.h>
Joonyoung Shim8b86c1c2011-04-12 23:18:59 -070023#include <linux/input/mt.h>
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070024#include <linux/interrupt.h>
25#include <linux/slab.h>
26
27/* Version */
Iiro Valkonen7686b102011-02-02 23:21:58 -080028#define MXT_VER_20 20
29#define MXT_VER_21 21
30#define MXT_VER_22 22
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070031
32/* Slave addresses */
Iiro Valkonen7686b102011-02-02 23:21:58 -080033#define MXT_APP_LOW 0x4a
34#define MXT_APP_HIGH 0x4b
35#define MXT_BOOT_LOW 0x24
36#define MXT_BOOT_HIGH 0x25
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070037
38/* Firmware */
Iiro Valkonen7686b102011-02-02 23:21:58 -080039#define MXT_FW_NAME "maxtouch.fw"
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070040
41/* Registers */
Daniel Kurtz23003a842012-06-28 21:08:14 +080042#define MXT_INFO 0x00
Iiro Valkonen7686b102011-02-02 23:21:58 -080043#define MXT_FAMILY_ID 0x00
44#define MXT_VARIANT_ID 0x01
45#define MXT_VERSION 0x02
46#define MXT_BUILD 0x03
47#define MXT_MATRIX_X_SIZE 0x04
48#define MXT_MATRIX_Y_SIZE 0x05
49#define MXT_OBJECT_NUM 0x06
50#define MXT_OBJECT_START 0x07
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070051
Iiro Valkonen7686b102011-02-02 23:21:58 -080052#define MXT_OBJECT_SIZE 6
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070053
54/* Object types */
Iiro Valkonen81c88a72011-07-04 03:08:25 -070055#define MXT_DEBUG_DIAGNOSTIC_T37 37
56#define MXT_GEN_MESSAGE_T5 5
57#define MXT_GEN_COMMAND_T6 6
58#define MXT_GEN_POWER_T7 7
59#define MXT_GEN_ACQUIRE_T8 8
60#define MXT_GEN_DATASOURCE_T53 53
61#define MXT_TOUCH_MULTI_T9 9
62#define MXT_TOUCH_KEYARRAY_T15 15
63#define MXT_TOUCH_PROXIMITY_T23 23
64#define MXT_TOUCH_PROXKEY_T52 52
65#define MXT_PROCI_GRIPFACE_T20 20
66#define MXT_PROCG_NOISE_T22 22
67#define MXT_PROCI_ONETOUCH_T24 24
68#define MXT_PROCI_TWOTOUCH_T27 27
69#define MXT_PROCI_GRIP_T40 40
70#define MXT_PROCI_PALM_T41 41
71#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
72#define MXT_PROCI_STYLUS_T47 47
73#define MXT_PROCG_NOISESUPPRESSION_T48 48
74#define MXT_SPT_COMMSCONFIG_T18 18
75#define MXT_SPT_GPIOPWM_T19 19
76#define MXT_SPT_SELFTEST_T25 25
77#define MXT_SPT_CTECONFIG_T28 28
78#define MXT_SPT_USERDATA_T38 38
79#define MXT_SPT_DIGITIZER_T43 43
80#define MXT_SPT_MESSAGECOUNT_T44 44
81#define MXT_SPT_CTECONFIG_T46 46
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070082
Iiro Valkonen81c88a72011-07-04 03:08:25 -070083/* MXT_GEN_COMMAND_T6 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080084#define MXT_COMMAND_RESET 0
85#define MXT_COMMAND_BACKUPNV 1
86#define MXT_COMMAND_CALIBRATE 2
87#define MXT_COMMAND_REPORTALL 3
88#define MXT_COMMAND_DIAGNOSTIC 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070089
Iiro Valkonen81c88a72011-07-04 03:08:25 -070090/* MXT_GEN_POWER_T7 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080091#define MXT_POWER_IDLEACQINT 0
92#define MXT_POWER_ACTVACQINT 1
93#define MXT_POWER_ACTV2IDLETO 2
Joonyoung Shim4cf51c32010-07-14 21:55:30 -070094
Iiro Valkonen81c88a72011-07-04 03:08:25 -070095/* MXT_GEN_ACQUIRE_T8 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -080096#define MXT_ACQUIRE_CHRGTIME 0
97#define MXT_ACQUIRE_TCHDRIFT 2
98#define MXT_ACQUIRE_DRIFTST 3
99#define MXT_ACQUIRE_TCHAUTOCAL 4
100#define MXT_ACQUIRE_SYNC 5
101#define MXT_ACQUIRE_ATCHCALST 6
102#define MXT_ACQUIRE_ATCHCALSTHR 7
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700103
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700104/* MXT_TOUCH_MULTI_T9 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800105#define MXT_TOUCH_CTRL 0
106#define MXT_TOUCH_XORIGIN 1
107#define MXT_TOUCH_YORIGIN 2
108#define MXT_TOUCH_XSIZE 3
109#define MXT_TOUCH_YSIZE 4
110#define MXT_TOUCH_BLEN 6
111#define MXT_TOUCH_TCHTHR 7
112#define MXT_TOUCH_TCHDI 8
113#define MXT_TOUCH_ORIENT 9
114#define MXT_TOUCH_MOVHYSTI 11
115#define MXT_TOUCH_MOVHYSTN 12
116#define MXT_TOUCH_NUMTOUCH 14
117#define MXT_TOUCH_MRGHYST 15
118#define MXT_TOUCH_MRGTHR 16
119#define MXT_TOUCH_AMPHYST 17
120#define MXT_TOUCH_XRANGE_LSB 18
121#define MXT_TOUCH_XRANGE_MSB 19
122#define MXT_TOUCH_YRANGE_LSB 20
123#define MXT_TOUCH_YRANGE_MSB 21
124#define MXT_TOUCH_XLOCLIP 22
125#define MXT_TOUCH_XHICLIP 23
126#define MXT_TOUCH_YLOCLIP 24
127#define MXT_TOUCH_YHICLIP 25
128#define MXT_TOUCH_XEDGECTRL 26
129#define MXT_TOUCH_XEDGEDIST 27
130#define MXT_TOUCH_YEDGECTRL 28
131#define MXT_TOUCH_YEDGEDIST 29
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700132#define MXT_TOUCH_JUMPLIMIT 30
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700133
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700134/* MXT_PROCI_GRIPFACE_T20 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800135#define MXT_GRIPFACE_CTRL 0
136#define MXT_GRIPFACE_XLOGRIP 1
137#define MXT_GRIPFACE_XHIGRIP 2
138#define MXT_GRIPFACE_YLOGRIP 3
139#define MXT_GRIPFACE_YHIGRIP 4
140#define MXT_GRIPFACE_MAXTCHS 5
141#define MXT_GRIPFACE_SZTHR1 7
142#define MXT_GRIPFACE_SZTHR2 8
143#define MXT_GRIPFACE_SHPTHR1 9
144#define MXT_GRIPFACE_SHPTHR2 10
145#define MXT_GRIPFACE_SUPEXTTO 11
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700146
Iiro Valkonen7686b102011-02-02 23:21:58 -0800147/* MXT_PROCI_NOISE field */
148#define MXT_NOISE_CTRL 0
149#define MXT_NOISE_OUTFLEN 1
150#define MXT_NOISE_GCAFUL_LSB 3
151#define MXT_NOISE_GCAFUL_MSB 4
152#define MXT_NOISE_GCAFLL_LSB 5
153#define MXT_NOISE_GCAFLL_MSB 6
154#define MXT_NOISE_ACTVGCAFVALID 7
155#define MXT_NOISE_NOISETHR 8
156#define MXT_NOISE_FREQHOPSCALE 10
157#define MXT_NOISE_FREQ0 11
158#define MXT_NOISE_FREQ1 12
159#define MXT_NOISE_FREQ2 13
160#define MXT_NOISE_FREQ3 14
161#define MXT_NOISE_FREQ4 15
162#define MXT_NOISE_IDLEGCAFVALID 16
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700163
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700164/* MXT_SPT_COMMSCONFIG_T18 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800165#define MXT_COMMS_CTRL 0
166#define MXT_COMMS_CMD 1
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700167
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700168/* MXT_SPT_CTECONFIG_T28 field */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800169#define MXT_CTE_CTRL 0
170#define MXT_CTE_CMD 1
171#define MXT_CTE_MODE 2
172#define MXT_CTE_IDLEGCAFDEPTH 3
173#define MXT_CTE_ACTVGCAFDEPTH 4
Joonyoung Shim979a72d2011-03-14 21:41:34 -0700174#define MXT_CTE_VOLTAGE 5
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700175
Iiro Valkonen7686b102011-02-02 23:21:58 -0800176#define MXT_VOLTAGE_DEFAULT 2700000
177#define MXT_VOLTAGE_STEP 10000
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700178
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700179/* Define for MXT_GEN_COMMAND_T6 */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800180#define MXT_BOOT_VALUE 0xa5
181#define MXT_BACKUP_VALUE 0x55
Linus Torvalds8343bce2013-03-09 10:31:01 -0800182#define MXT_BACKUP_TIME 50 /* msec */
183#define MXT_RESET_TIME 200 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700184
Iiro Valkonen7686b102011-02-02 23:21:58 -0800185#define MXT_FWRESET_TIME 175 /* msec */
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700186
187/* Command to unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800188#define MXT_UNLOCK_CMD_MSB 0xaa
189#define MXT_UNLOCK_CMD_LSB 0xdc
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700190
191/* Bootloader mode status */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800192#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
193#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
194#define MXT_FRAME_CRC_CHECK 0x02
195#define MXT_FRAME_CRC_FAIL 0x03
196#define MXT_FRAME_CRC_PASS 0x04
197#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
198#define MXT_BOOT_STATUS_MASK 0x3f
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700199
200/* Touch status */
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800201#define MXT_UNGRIP (1 << 0)
Iiro Valkonen7686b102011-02-02 23:21:58 -0800202#define MXT_SUPPRESS (1 << 1)
203#define MXT_AMP (1 << 2)
204#define MXT_VECTOR (1 << 3)
205#define MXT_MOVE (1 << 4)
206#define MXT_RELEASE (1 << 5)
207#define MXT_PRESS (1 << 6)
208#define MXT_DETECT (1 << 7)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700209
Joonyoung Shim910d8052011-04-12 23:14:38 -0700210/* Touch orient bits */
211#define MXT_XY_SWITCH (1 << 0)
212#define MXT_X_INVERT (1 << 1)
213#define MXT_Y_INVERT (1 << 2)
214
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700215/* Touchscreen absolute values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800216#define MXT_MAX_AREA 0xff
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700217
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800218#define MXT_PIXELS_PER_MM 20
219
Iiro Valkonen7686b102011-02-02 23:21:58 -0800220struct mxt_info {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700221 u8 family_id;
222 u8 variant_id;
223 u8 version;
224 u8 build;
225 u8 matrix_xsize;
226 u8 matrix_ysize;
227 u8 object_num;
228};
229
Iiro Valkonen7686b102011-02-02 23:21:58 -0800230struct mxt_object {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700231 u8 type;
232 u16 start_address;
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700233 u8 size_minus_one;
234 u8 instances_minus_one;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700235 u8 num_report_ids;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800236} __packed;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700237
Iiro Valkonen7686b102011-02-02 23:21:58 -0800238struct mxt_message {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700239 u8 reportid;
240 u8 message[7];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700241};
242
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700243/* Each client has this additional data */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800244struct mxt_data {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700245 struct i2c_client *client;
246 struct input_dev *input_dev;
Daniel Kurtzec02ac22012-06-28 21:08:02 +0800247 char phys[64]; /* device physical location */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800248 const struct mxt_platform_data *pdata;
249 struct mxt_object *object_table;
250 struct mxt_info info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700251 unsigned int irq;
Joonyoung Shim910d8052011-04-12 23:14:38 -0700252 unsigned int max_x;
253 unsigned int max_y;
Benson Leungd79e7e42014-05-18 23:02:52 -0700254 bool in_bootloader;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800255
256 /* Cached parameters from object table */
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800257 u8 T6_reportid;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800258 u8 T9_reportid_min;
259 u8 T9_reportid_max;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800260 u8 T19_reportid;
Benson Leungd79e7e42014-05-18 23:02:52 -0700261
262 /* for fw update in bootloader */
263 struct completion bl_completion;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700264};
265
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700266static size_t mxt_obj_size(const struct mxt_object *obj)
267{
268 return obj->size_minus_one + 1;
269}
270
271static size_t mxt_obj_instances(const struct mxt_object *obj)
272{
273 return obj->instances_minus_one + 1;
274}
275
Iiro Valkonen7686b102011-02-02 23:21:58 -0800276static bool mxt_object_readable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700277{
278 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700279 case MXT_GEN_COMMAND_T6:
280 case MXT_GEN_POWER_T7:
281 case MXT_GEN_ACQUIRE_T8:
282 case MXT_GEN_DATASOURCE_T53:
283 case MXT_TOUCH_MULTI_T9:
284 case MXT_TOUCH_KEYARRAY_T15:
285 case MXT_TOUCH_PROXIMITY_T23:
286 case MXT_TOUCH_PROXKEY_T52:
287 case MXT_PROCI_GRIPFACE_T20:
288 case MXT_PROCG_NOISE_T22:
289 case MXT_PROCI_ONETOUCH_T24:
290 case MXT_PROCI_TWOTOUCH_T27:
291 case MXT_PROCI_GRIP_T40:
292 case MXT_PROCI_PALM_T41:
293 case MXT_PROCI_TOUCHSUPPRESSION_T42:
294 case MXT_PROCI_STYLUS_T47:
295 case MXT_PROCG_NOISESUPPRESSION_T48:
296 case MXT_SPT_COMMSCONFIG_T18:
297 case MXT_SPT_GPIOPWM_T19:
298 case MXT_SPT_SELFTEST_T25:
299 case MXT_SPT_CTECONFIG_T28:
300 case MXT_SPT_USERDATA_T38:
301 case MXT_SPT_DIGITIZER_T43:
302 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700303 return true;
304 default:
305 return false;
306 }
307}
308
Iiro Valkonen7686b102011-02-02 23:21:58 -0800309static bool mxt_object_writable(unsigned int type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700310{
311 switch (type) {
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700312 case MXT_GEN_COMMAND_T6:
313 case MXT_GEN_POWER_T7:
314 case MXT_GEN_ACQUIRE_T8:
315 case MXT_TOUCH_MULTI_T9:
316 case MXT_TOUCH_KEYARRAY_T15:
317 case MXT_TOUCH_PROXIMITY_T23:
318 case MXT_TOUCH_PROXKEY_T52:
319 case MXT_PROCI_GRIPFACE_T20:
320 case MXT_PROCG_NOISE_T22:
321 case MXT_PROCI_ONETOUCH_T24:
322 case MXT_PROCI_TWOTOUCH_T27:
323 case MXT_PROCI_GRIP_T40:
324 case MXT_PROCI_PALM_T41:
325 case MXT_PROCI_TOUCHSUPPRESSION_T42:
326 case MXT_PROCI_STYLUS_T47:
327 case MXT_PROCG_NOISESUPPRESSION_T48:
328 case MXT_SPT_COMMSCONFIG_T18:
329 case MXT_SPT_GPIOPWM_T19:
330 case MXT_SPT_SELFTEST_T25:
331 case MXT_SPT_CTECONFIG_T28:
332 case MXT_SPT_DIGITIZER_T43:
333 case MXT_SPT_CTECONFIG_T46:
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700334 return true;
335 default:
336 return false;
337 }
338}
339
Iiro Valkonen7686b102011-02-02 23:21:58 -0800340static void mxt_dump_message(struct device *dev,
Daniel Kurtz6ee3dbf2012-05-08 22:40:29 -0700341 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700342{
Andy Shevchenkoeb007c82012-10-04 00:02:59 -0700343 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
344 message->reportid, 7, message->message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700345}
346
Benson Leungd79e7e42014-05-18 23:02:52 -0700347static int mxt_wait_for_chg(struct mxt_data *data, unsigned int timeout_ms)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700348{
Benson Leungd79e7e42014-05-18 23:02:52 -0700349 struct device *dev = &data->client->dev;
350 struct completion *comp = &data->bl_completion;
351 unsigned long timeout = msecs_to_jiffies(timeout_ms);
352 long ret;
353
354 ret = wait_for_completion_interruptible_timeout(comp, timeout);
355 if (ret < 0) {
356 return ret;
357 } else if (ret == 0) {
358 dev_err(dev, "Wait for completion timed out.\n");
359 return -ETIMEDOUT;
360 }
361 return 0;
362}
363
364static int mxt_check_bootloader(struct mxt_data *data, unsigned int state)
365{
366 struct i2c_client *client = data->client;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700367 u8 val;
Benson Leungd79e7e42014-05-18 23:02:52 -0700368 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700369
370recheck:
Benson Leungd79e7e42014-05-18 23:02:52 -0700371 if (state != MXT_WAITING_BOOTLOAD_CMD) {
372 /*
373 * In application update mode, the interrupt
374 * line signals state transitions. We must wait for the
375 * CHG assertion before reading the status byte.
376 * Once the status byte has been read, the line is deasserted.
377 */
378 ret = mxt_wait_for_chg(data, 300);
379 if (ret) {
380 /*
381 * TODO: handle -ERESTARTSYS better by terminating
382 * fw update process before returning to userspace
383 * by writing length 0x000 to device (iff we are in
384 * WAITING_FRAME_DATA state).
385 */
386 dev_err(&client->dev, "Update wait error %d\n", ret);
387 return ret;
388 }
389 }
390
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700391 if (i2c_master_recv(client, &val, 1) != 1) {
392 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
393 return -EIO;
394 }
395
396 switch (state) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800397 case MXT_WAITING_BOOTLOAD_CMD:
398 case MXT_WAITING_FRAME_DATA:
399 val &= ~MXT_BOOT_STATUS_MASK;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700400 break;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800401 case MXT_FRAME_CRC_PASS:
402 if (val == MXT_FRAME_CRC_CHECK)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700403 goto recheck;
404 break;
405 default:
406 return -EINVAL;
407 }
408
409 if (val != state) {
410 dev_err(&client->dev, "Unvalid bootloader mode state\n");
411 return -EINVAL;
412 }
413
414 return 0;
415}
416
Iiro Valkonen7686b102011-02-02 23:21:58 -0800417static int mxt_unlock_bootloader(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700418{
419 u8 buf[2];
420
Iiro Valkonen7686b102011-02-02 23:21:58 -0800421 buf[0] = MXT_UNLOCK_CMD_LSB;
422 buf[1] = MXT_UNLOCK_CMD_MSB;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700423
424 if (i2c_master_send(client, buf, 2) != 2) {
425 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
426 return -EIO;
427 }
428
429 return 0;
430}
431
Iiro Valkonen7686b102011-02-02 23:21:58 -0800432static int mxt_fw_write(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700433 const u8 *data, unsigned int frame_size)
434{
435 if (i2c_master_send(client, data, frame_size) != frame_size) {
436 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
437 return -EIO;
438 }
439
440 return 0;
441}
442
Iiro Valkonen7686b102011-02-02 23:21:58 -0800443static int __mxt_read_reg(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700444 u16 reg, u16 len, void *val)
445{
446 struct i2c_msg xfer[2];
447 u8 buf[2];
Daniel Kurtz771733e2012-06-28 21:08:11 +0800448 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700449
450 buf[0] = reg & 0xff;
451 buf[1] = (reg >> 8) & 0xff;
452
453 /* Write register */
454 xfer[0].addr = client->addr;
455 xfer[0].flags = 0;
456 xfer[0].len = 2;
457 xfer[0].buf = buf;
458
459 /* Read data */
460 xfer[1].addr = client->addr;
461 xfer[1].flags = I2C_M_RD;
462 xfer[1].len = len;
463 xfer[1].buf = val;
464
Daniel Kurtz771733e2012-06-28 21:08:11 +0800465 ret = i2c_transfer(client->adapter, xfer, 2);
466 if (ret == 2) {
467 ret = 0;
468 } else {
469 if (ret >= 0)
470 ret = -EIO;
471 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
472 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700473 }
474
Daniel Kurtz771733e2012-06-28 21:08:11 +0800475 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700476}
477
Iiro Valkonen7686b102011-02-02 23:21:58 -0800478static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700479{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800480 return __mxt_read_reg(client, reg, 1, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700481}
482
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800483static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
484 const void *val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700485{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800486 u8 *buf;
487 size_t count;
Daniel Kurtz771733e2012-06-28 21:08:11 +0800488 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700489
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800490 count = len + 2;
491 buf = kmalloc(count, GFP_KERNEL);
492 if (!buf)
493 return -ENOMEM;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700494
495 buf[0] = reg & 0xff;
496 buf[1] = (reg >> 8) & 0xff;
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800497 memcpy(&buf[2], val, len);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700498
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800499 ret = i2c_master_send(client, buf, count);
500 if (ret == count) {
Daniel Kurtz771733e2012-06-28 21:08:11 +0800501 ret = 0;
502 } else {
503 if (ret >= 0)
504 ret = -EIO;
505 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
506 __func__, ret);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700507 }
508
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800509 kfree(buf);
Daniel Kurtz771733e2012-06-28 21:08:11 +0800510 return ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700511}
512
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800513static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700514{
Daniel Kurtz9638ab72012-06-28 21:08:12 +0800515 return __mxt_write_reg(client, reg, 1, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700516}
517
Iiro Valkonen7686b102011-02-02 23:21:58 -0800518static struct mxt_object *
519mxt_get_object(struct mxt_data *data, u8 type)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700520{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800521 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700522 int i;
523
524 for (i = 0; i < data->info.object_num; i++) {
525 object = data->object_table + i;
526 if (object->type == type)
527 return object;
528 }
529
530 dev_err(&data->client->dev, "Invalid object type\n");
531 return NULL;
532}
533
Iiro Valkonen7686b102011-02-02 23:21:58 -0800534static int mxt_read_message(struct mxt_data *data,
535 struct mxt_message *message)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700536{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800537 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700538 u16 reg;
539
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700540 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700541 if (!object)
542 return -EINVAL;
543
544 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800545 return __mxt_read_reg(data->client, reg,
546 sizeof(struct mxt_message), message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700547}
548
Iiro Valkonen7686b102011-02-02 23:21:58 -0800549static int mxt_write_object(struct mxt_data *data,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700550 u8 type, u8 offset, u8 val)
551{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800552 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700553 u16 reg;
554
Iiro Valkonen7686b102011-02-02 23:21:58 -0800555 object = mxt_get_object(data, type);
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700556 if (!object || offset >= mxt_obj_size(object))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700557 return -EINVAL;
558
559 reg = object->start_address;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800560 return mxt_write_reg(data->client, reg + offset, val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700561}
562
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800563static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
564{
565 struct input_dev *input = data->input_dev;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700566 const struct mxt_platform_data *pdata = data->pdata;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800567 bool button;
568 int i;
569
570 /* Active-low switch */
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700571 for (i = 0; i < pdata->t19_num_keys; i++) {
572 if (pdata->t19_keymap[i] == KEY_RESERVED)
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800573 continue;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -0700574 button = !(message->message[0] & (1 << i));
575 input_report_key(input, pdata->t19_keymap[i], button);
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800576 }
577}
578
Iiro Valkonen7686b102011-02-02 23:21:58 -0800579static void mxt_input_touchevent(struct mxt_data *data,
580 struct mxt_message *message, int id)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700581{
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700582 struct device *dev = &data->client->dev;
583 u8 status = message->message[0];
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800584 struct input_dev *input_dev = data->input_dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700585 int x;
586 int y;
587 int area;
Yufeng Shen28ac2932011-08-16 00:40:54 -0700588 int pressure;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700589
Joonyoung Shim910d8052011-04-12 23:14:38 -0700590 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
591 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
592 if (data->max_x < 1024)
593 x = x >> 2;
594 if (data->max_y < 1024)
595 y = y >> 2;
596
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700597 area = message->message[4];
Yufeng Shen28ac2932011-08-16 00:40:54 -0700598 pressure = message->message[5];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700599
Daniel Kurtzb2e459b2012-06-28 21:08:18 +0800600 dev_dbg(dev,
601 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
602 id,
603 (status & MXT_DETECT) ? 'D' : '.',
604 (status & MXT_PRESS) ? 'P' : '.',
605 (status & MXT_RELEASE) ? 'R' : '.',
606 (status & MXT_MOVE) ? 'M' : '.',
607 (status & MXT_VECTOR) ? 'V' : '.',
608 (status & MXT_AMP) ? 'A' : '.',
609 (status & MXT_SUPPRESS) ? 'S' : '.',
610 (status & MXT_UNGRIP) ? 'U' : '.',
611 x, y, area, pressure);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700612
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800613 input_mt_slot(input_dev, id);
614 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
615 status & MXT_DETECT);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700616
Daniel Kurtzfba5bc32012-06-28 21:08:17 +0800617 if (status & MXT_DETECT) {
618 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
619 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
620 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
621 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
622 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700623}
624
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800625static unsigned mxt_extract_T6_csum(const u8 *csum)
626{
627 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
628}
629
Daniel Kurtz04a79182012-06-28 21:08:21 +0800630static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
631{
632 u8 id = msg->reportid;
633 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700634}
635
Benson Leungd79e7e42014-05-18 23:02:52 -0700636static irqreturn_t mxt_process_messages_until_invalid(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700637{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800638 struct mxt_message message;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800639 const u8 *payload = &message.message[0];
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700640 struct device *dev = &data->client->dev;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700641 u8 reportid;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800642 bool update_input = false;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700643
644 do {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800645 if (mxt_read_message(data, &message)) {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700646 dev_err(dev, "Failed to read message\n");
Nick Dyer8d4e1632014-05-18 23:00:56 -0700647 return IRQ_NONE;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700648 }
649
650 reportid = message.reportid;
651
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800652 if (reportid == data->T6_reportid) {
653 u8 status = payload[0];
654 unsigned csum = mxt_extract_T6_csum(&payload[1]);
655 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
656 status, csum);
657 } else if (mxt_is_T9_message(data, &message)) {
658 int id = reportid - data->T9_reportid_min;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800659 mxt_input_touchevent(data, &message, id);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800660 update_input = true;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800661 } else if (message.reportid == data->T19_reportid) {
662 mxt_input_button(data, &message);
663 update_input = true;
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800664 } else {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800665 mxt_dump_message(dev, &message);
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800666 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700667 } while (reportid != 0xff);
668
Daniel Kurtz64464ae2012-06-28 21:08:23 +0800669 if (update_input) {
670 input_mt_report_pointer_emulation(data->input_dev, false);
671 input_sync(data->input_dev);
672 }
673
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700674 return IRQ_HANDLED;
675}
676
Benson Leungd79e7e42014-05-18 23:02:52 -0700677static irqreturn_t mxt_interrupt(int irq, void *dev_id)
678{
679 struct mxt_data *data = dev_id;
680
681 if (data->in_bootloader) {
682 /* bootloader state transition completion */
683 complete(&data->bl_completion);
684 return IRQ_HANDLED;
685 }
686
687 return mxt_process_messages_until_invalid(data);
688}
689
Iiro Valkonen7686b102011-02-02 23:21:58 -0800690static int mxt_check_reg_init(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700691{
Iiro Valkonen71749f52011-02-15 13:36:52 -0800692 const struct mxt_platform_data *pdata = data->pdata;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800693 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700694 struct device *dev = &data->client->dev;
695 int index = 0;
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800696 int i, size;
697 int ret;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700698
Iiro Valkonen71749f52011-02-15 13:36:52 -0800699 if (!pdata->config) {
700 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
701 return 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700702 }
703
704 for (i = 0; i < data->info.object_num; i++) {
705 object = data->object_table + i;
706
Iiro Valkonen7686b102011-02-02 23:21:58 -0800707 if (!mxt_object_writable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700708 continue;
709
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700710 size = mxt_obj_size(object) * mxt_obj_instances(object);
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800711 if (index + size > pdata->config_length) {
712 dev_err(dev, "Not enough config data!\n");
713 return -EINVAL;
Iiro Valkonen71749f52011-02-15 13:36:52 -0800714 }
Daniel Kurtzcf94bc02012-06-28 21:08:13 +0800715
716 ret = __mxt_write_reg(data->client, object->start_address,
717 size, &pdata->config[index]);
718 if (ret)
719 return ret;
720 index += size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700721 }
722
723 return 0;
724}
725
Iiro Valkonen7686b102011-02-02 23:21:58 -0800726static int mxt_make_highchg(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700727{
728 struct device *dev = &data->client->dev;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800729 struct mxt_message message;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700730 int count = 10;
731 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700732
733 /* Read dummy message to make high CHG pin */
734 do {
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800735 error = mxt_read_message(data, &message);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700736 if (error)
737 return error;
Iiro Valkonen26cdb1a2011-02-04 00:51:05 -0800738 } while (message.reportid != 0xff && --count);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700739
740 if (!count) {
741 dev_err(dev, "CHG pin isn't cleared\n");
742 return -EBUSY;
743 }
744
745 return 0;
746}
747
Iiro Valkonen7686b102011-02-02 23:21:58 -0800748static int mxt_get_info(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700749{
750 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800751 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700752 int error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700753
Daniel Kurtz23003a842012-06-28 21:08:14 +0800754 /* Read 7-byte info block starting at address 0 */
755 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700756 if (error)
757 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700758
759 return 0;
760}
761
Iiro Valkonen7686b102011-02-02 23:21:58 -0800762static int mxt_get_object_table(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700763{
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800764 struct i2c_client *client = data->client;
765 size_t table_size;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700766 int error;
767 int i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800768 u8 reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700769
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800770 table_size = data->info.object_num * sizeof(struct mxt_object);
771 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
772 data->object_table);
773 if (error)
774 return error;
775
776 /* Valid Report IDs start counting from 1 */
777 reportid = 1;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700778 for (i = 0; i < data->info.object_num; i++) {
Iiro Valkonen7686b102011-02-02 23:21:58 -0800779 struct mxt_object *object = data->object_table + i;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800780 u8 min_id, max_id;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700781
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800782 le16_to_cpus(&object->start_address);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700783
784 if (object->num_report_ids) {
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800785 min_id = reportid;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700786 reportid += object->num_report_ids *
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700787 mxt_obj_instances(object);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800788 max_id = reportid - 1;
789 } else {
790 min_id = 0;
791 max_id = 0;
792 }
793
794 dev_dbg(&data->client->dev,
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700795 "Type %2d Start %3d Size %3zd Instances %2zd ReportIDs %3u : %3u\n",
796 object->type, object->start_address,
797 mxt_obj_size(object), mxt_obj_instances(object),
798 min_id, max_id);
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800799
800 switch (object->type) {
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800801 case MXT_GEN_COMMAND_T6:
802 data->T6_reportid = min_id;
803 break;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800804 case MXT_TOUCH_MULTI_T9:
805 data->T9_reportid_min = min_id;
806 data->T9_reportid_max = max_id;
807 break;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800808 case MXT_SPT_GPIOPWM_T19:
809 data->T19_reportid = min_id;
810 break;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700811 }
812 }
813
814 return 0;
815}
816
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800817static void mxt_free_object_table(struct mxt_data *data)
818{
819 kfree(data->object_table);
820 data->object_table = NULL;
Daniel Kurtzfdf804212012-06-28 21:08:24 +0800821 data->T6_reportid = 0;
Daniel Kurtz333e5a92012-06-28 21:08:20 +0800822 data->T9_reportid_min = 0;
823 data->T9_reportid_max = 0;
Daniel Kurtz22dfab72013-03-07 19:43:33 -0800824 data->T19_reportid = 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800825}
826
Iiro Valkonen7686b102011-02-02 23:21:58 -0800827static int mxt_initialize(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700828{
829 struct i2c_client *client = data->client;
Iiro Valkonen7686b102011-02-02 23:21:58 -0800830 struct mxt_info *info = &data->info;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700831 int error;
832 u8 val;
833
Iiro Valkonen7686b102011-02-02 23:21:58 -0800834 error = mxt_get_info(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700835 if (error)
836 return error;
837
838 data->object_table = kcalloc(info->object_num,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800839 sizeof(struct mxt_object),
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700840 GFP_KERNEL);
841 if (!data->object_table) {
842 dev_err(&client->dev, "Failed to allocate memory\n");
843 return -ENOMEM;
844 }
845
846 /* Get object table information */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800847 error = mxt_get_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700848 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800849 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700850
851 /* Check register init values */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800852 error = mxt_check_reg_init(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700853 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800854 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700855
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700856 /* Backup to memory */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700857 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800858 MXT_COMMAND_BACKUPNV,
859 MXT_BACKUP_VALUE);
860 msleep(MXT_BACKUP_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700861
862 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -0700863 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -0800864 MXT_COMMAND_RESET, 1);
865 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700866
867 /* Update matrix size at info struct */
Iiro Valkonen7686b102011-02-02 23:21:58 -0800868 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700869 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800870 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700871 info->matrix_xsize = val;
872
Iiro Valkonen7686b102011-02-02 23:21:58 -0800873 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700874 if (error)
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800875 goto err_free_object_table;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700876 info->matrix_ysize = val;
877
878 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800879 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
880 info->family_id, info->variant_id, info->version >> 4,
881 info->version & 0xf, info->build);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700882
883 dev_info(&client->dev,
Daniel Kurtze0e02692012-06-28 21:08:15 +0800884 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700885 info->matrix_xsize, info->matrix_ysize,
886 info->object_num);
887
888 return 0;
Daniel Kurtz7d4fa102012-06-28 21:08:19 +0800889
890err_free_object_table:
891 mxt_free_object_table(data);
892 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700893}
894
Joonyoung Shim910d8052011-04-12 23:14:38 -0700895static void mxt_calc_resolution(struct mxt_data *data)
896{
897 unsigned int max_x = data->pdata->x_size - 1;
898 unsigned int max_y = data->pdata->y_size - 1;
899
900 if (data->pdata->orient & MXT_XY_SWITCH) {
901 data->max_x = max_y;
902 data->max_y = max_x;
903 } else {
904 data->max_x = max_x;
905 data->max_y = max_y;
906 }
907}
908
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +0800909/* Firmware Version is returned as Major.Minor.Build */
910static ssize_t mxt_fw_version_show(struct device *dev,
911 struct device_attribute *attr, char *buf)
912{
913 struct mxt_data *data = dev_get_drvdata(dev);
914 struct mxt_info *info = &data->info;
915 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
916 info->version >> 4, info->version & 0xf, info->build);
917}
918
919/* Hardware Version is returned as FamilyID.VariantID */
920static ssize_t mxt_hw_version_show(struct device *dev,
921 struct device_attribute *attr, char *buf)
922{
923 struct mxt_data *data = dev_get_drvdata(dev);
924 struct mxt_info *info = &data->info;
925 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
926 info->family_id, info->variant_id);
927}
928
Daniel Kurtz794eb672012-06-28 21:08:10 +0800929static ssize_t mxt_show_instance(char *buf, int count,
930 struct mxt_object *object, int instance,
931 const u8 *val)
932{
933 int i;
934
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700935 if (mxt_obj_instances(object) > 1)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800936 count += scnprintf(buf + count, PAGE_SIZE - count,
937 "Instance %u\n", instance);
938
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700939 for (i = 0; i < mxt_obj_size(object); i++)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800940 count += scnprintf(buf + count, PAGE_SIZE - count,
941 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
942 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
943
944 return count;
945}
946
Iiro Valkonen7686b102011-02-02 23:21:58 -0800947static ssize_t mxt_object_show(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700948 struct device_attribute *attr, char *buf)
949{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800950 struct mxt_data *data = dev_get_drvdata(dev);
951 struct mxt_object *object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700952 int count = 0;
953 int i, j;
954 int error;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800955 u8 *obuf;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700956
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800957 /* Pre-allocate buffer large enough to hold max sized object. */
958 obuf = kmalloc(256, GFP_KERNEL);
959 if (!obuf)
960 return -ENOMEM;
961
962 error = 0;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700963 for (i = 0; i < data->info.object_num; i++) {
964 object = data->object_table + i;
965
Daniel Kurtz91630952012-06-28 21:08:09 +0800966 if (!mxt_object_readable(object->type))
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700967 continue;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700968
Daniel Kurtz91630952012-06-28 21:08:09 +0800969 count += scnprintf(buf + count, PAGE_SIZE - count,
970 "T%u:\n", object->type);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700971
Daniel Kurtz1e0c0c52014-05-18 23:01:12 -0700972 for (j = 0; j < mxt_obj_instances(object); j++) {
973 u16 size = mxt_obj_size(object);
Daniel Kurtz794eb672012-06-28 21:08:10 +0800974 u16 addr = object->start_address + j * size;
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800975
Daniel Kurtz794eb672012-06-28 21:08:10 +0800976 error = __mxt_read_reg(data->client, addr, size, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700977 if (error)
Daniel Kurtz794eb672012-06-28 21:08:10 +0800978 goto done;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700979
Daniel Kurtz794eb672012-06-28 21:08:10 +0800980 count = mxt_show_instance(buf, count, object, j, obuf);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700981 }
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700982 }
983
Daniel Kurtz794eb672012-06-28 21:08:10 +0800984done:
Daniel Kurtz43a91d52012-06-28 21:08:08 +0800985 kfree(obuf);
986 return error ?: count;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700987}
988
Iiro Valkonen7686b102011-02-02 23:21:58 -0800989static int mxt_load_fw(struct device *dev, const char *fn)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700990{
Iiro Valkonen7686b102011-02-02 23:21:58 -0800991 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -0700992 struct i2c_client *client = data->client;
993 const struct firmware *fw = NULL;
994 unsigned int frame_size;
995 unsigned int pos = 0;
996 int ret;
997
998 ret = request_firmware(&fw, fn, dev);
999 if (ret) {
1000 dev_err(dev, "Unable to open firmware %s\n", fn);
1001 return ret;
1002 }
1003
1004 /* Change to the bootloader mode */
Benson Leungd79e7e42014-05-18 23:02:52 -07001005 data->in_bootloader = true;
1006
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001007 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001008 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
1009 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001010
1011 /* Change to slave address of bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001012 if (client->addr == MXT_APP_LOW)
1013 client->addr = MXT_BOOT_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001014 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001015 client->addr = MXT_BOOT_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001016
Benson Leungd79e7e42014-05-18 23:02:52 -07001017 reinit_completion(&data->bl_completion);
1018
1019 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001020 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001021 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001022
1023 /* Unlock bootloader */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001024 mxt_unlock_bootloader(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001025
1026 while (pos < fw->size) {
Benson Leungd79e7e42014-05-18 23:02:52 -07001027 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001028 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001029 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001030
1031 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1032
1033 /* We should add 2 at frame size as the the firmware data is not
1034 * included the CRC bytes.
1035 */
1036 frame_size += 2;
1037
1038 /* Write one frame to device */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001039 mxt_fw_write(client, fw->data + pos, frame_size);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001040
Benson Leungd79e7e42014-05-18 23:02:52 -07001041 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001042 if (ret)
Benson Leungd79e7e42014-05-18 23:02:52 -07001043 goto disable_irq;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001044
1045 pos += frame_size;
1046
1047 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1048 }
1049
Benson Leungd79e7e42014-05-18 23:02:52 -07001050 data->in_bootloader = false;
1051
1052disable_irq:
1053 disable_irq(data->irq);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001054 release_firmware(fw);
1055
1056 /* Change to slave address of application */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001057 if (client->addr == MXT_BOOT_LOW)
1058 client->addr = MXT_APP_LOW;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001059 else
Iiro Valkonen7686b102011-02-02 23:21:58 -08001060 client->addr = MXT_APP_HIGH;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001061
1062 return ret;
1063}
1064
Iiro Valkonen7686b102011-02-02 23:21:58 -08001065static ssize_t mxt_update_fw_store(struct device *dev,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001066 struct device_attribute *attr,
1067 const char *buf, size_t count)
1068{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001069 struct mxt_data *data = dev_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001070 int error;
1071
Iiro Valkonen7686b102011-02-02 23:21:58 -08001072 error = mxt_load_fw(dev, MXT_FW_NAME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001073 if (error) {
1074 dev_err(dev, "The firmware update failed(%d)\n", error);
1075 count = error;
1076 } else {
1077 dev_dbg(dev, "The firmware update succeeded\n");
1078
1079 /* Wait for reset */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001080 msleep(MXT_FWRESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001081
Daniel Kurtz7d4fa102012-06-28 21:08:19 +08001082 mxt_free_object_table(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001083
Iiro Valkonen7686b102011-02-02 23:21:58 -08001084 mxt_initialize(data);
Benson Leungd79e7e42014-05-18 23:02:52 -07001085
1086 enable_irq(data->irq);
1087
1088 error = mxt_make_highchg(data);
1089 if (error)
1090 return error;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001091 }
1092
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001093 return count;
1094}
1095
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001096static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1097static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
Daniel Kurtz71b3e932012-05-08 22:30:14 -07001098static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1099static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001100
Iiro Valkonen7686b102011-02-02 23:21:58 -08001101static struct attribute *mxt_attrs[] = {
Daniel Kurtzb19fc9e2012-06-28 21:08:16 +08001102 &dev_attr_fw_version.attr,
1103 &dev_attr_hw_version.attr,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001104 &dev_attr_object.attr,
1105 &dev_attr_update_fw.attr,
1106 NULL
1107};
1108
Iiro Valkonen7686b102011-02-02 23:21:58 -08001109static const struct attribute_group mxt_attr_group = {
1110 .attrs = mxt_attrs,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001111};
1112
Iiro Valkonen7686b102011-02-02 23:21:58 -08001113static void mxt_start(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001114{
1115 /* Touch enable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001116 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001117 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001118}
1119
Iiro Valkonen7686b102011-02-02 23:21:58 -08001120static void mxt_stop(struct mxt_data *data)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001121{
1122 /* Touch disable */
Iiro Valkonen7686b102011-02-02 23:21:58 -08001123 mxt_write_object(data,
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001124 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001125}
1126
Iiro Valkonen7686b102011-02-02 23:21:58 -08001127static int mxt_input_open(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001128{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001129 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001130
Iiro Valkonen7686b102011-02-02 23:21:58 -08001131 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001132
1133 return 0;
1134}
1135
Iiro Valkonen7686b102011-02-02 23:21:58 -08001136static void mxt_input_close(struct input_dev *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001137{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001138 struct mxt_data *data = input_get_drvdata(dev);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001139
Iiro Valkonen7686b102011-02-02 23:21:58 -08001140 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001141}
1142
Bill Pemberton5298cc42012-11-23 21:38:25 -08001143static int mxt_probe(struct i2c_client *client,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001144 const struct i2c_device_id *id)
1145{
Jingoo Hanc838cb32013-12-05 19:21:10 -08001146 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001147 struct mxt_data *data;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001148 struct input_dev *input_dev;
1149 int error;
Daniel Kurtzcb159112012-06-28 21:08:22 +08001150 unsigned int num_mt_slots;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001151 unsigned int mt_flags = 0;
1152 int i;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001153
Iiro Valkonen919ed892011-02-15 13:36:52 -08001154 if (!pdata)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001155 return -EINVAL;
1156
Iiro Valkonen7686b102011-02-02 23:21:58 -08001157 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001158 input_dev = input_allocate_device();
1159 if (!data || !input_dev) {
1160 dev_err(&client->dev, "Failed to allocate memory\n");
1161 error = -ENOMEM;
1162 goto err_free_mem;
1163 }
1164
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001165 input_dev->name = "Atmel maXTouch Touchscreen";
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001166 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1167 client->adapter->nr, client->addr);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001168
Daniel Kurtzec02ac22012-06-28 21:08:02 +08001169 input_dev->phys = data->phys;
1170
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001171 input_dev->id.bustype = BUS_I2C;
1172 input_dev->dev.parent = &client->dev;
Iiro Valkonen7686b102011-02-02 23:21:58 -08001173 input_dev->open = mxt_input_open;
1174 input_dev->close = mxt_input_close;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001175
Joonyoung Shim910d8052011-04-12 23:14:38 -07001176 data->client = client;
1177 data->input_dev = input_dev;
1178 data->pdata = pdata;
1179 data->irq = client->irq;
1180
Benson Leungd79e7e42014-05-18 23:02:52 -07001181 init_completion(&data->bl_completion);
1182
Joonyoung Shim910d8052011-04-12 23:14:38 -07001183 mxt_calc_resolution(data);
1184
Daniel Kurtzcb159112012-06-28 21:08:22 +08001185 error = mxt_initialize(data);
1186 if (error)
1187 goto err_free_mem;
1188
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001189 __set_bit(EV_ABS, input_dev->evbit);
1190 __set_bit(EV_KEY, input_dev->evbit);
1191 __set_bit(BTN_TOUCH, input_dev->keybit);
1192
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001193 if (pdata->t19_num_keys) {
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001194 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1195
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001196 for (i = 0; i < pdata->t19_num_keys; i++)
1197 if (pdata->t19_keymap[i] != KEY_RESERVED)
1198 input_set_capability(input_dev, EV_KEY,
1199 pdata->t19_keymap[i]);
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001200
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001201 mt_flags |= INPUT_MT_POINTER;
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001202
1203 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1204 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1205 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1206 MXT_PIXELS_PER_MM);
1207 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1208 MXT_PIXELS_PER_MM);
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001209
1210 input_dev->name = "Atmel maXTouch Touchpad";
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001211 }
1212
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001213 /* For single touch */
1214 input_set_abs_params(input_dev, ABS_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001215 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001216 input_set_abs_params(input_dev, ABS_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001217 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001218 input_set_abs_params(input_dev, ABS_PRESSURE,
1219 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001220
1221 /* For multi touch */
Daniel Kurtzcb159112012-06-28 21:08:22 +08001222 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
Nick Dyerfb5e4c3e2014-05-18 23:00:15 -07001223 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
Daniel Kurtze1e16582012-06-28 21:08:04 +08001224 if (error)
Daniel Kurtzcb159112012-06-28 21:08:22 +08001225 goto err_free_object;
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001226 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001227 0, MXT_MAX_AREA, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001228 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001229 0, data->max_x, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001230 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
Joonyoung Shim910d8052011-04-12 23:14:38 -07001231 0, data->max_y, 0, 0);
Yufeng Shen28ac2932011-08-16 00:40:54 -07001232 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1233 0, 255, 0, 0);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001234
1235 input_set_drvdata(input_dev, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001236 i2c_set_clientdata(client, data);
1237
Iiro Valkonen7686b102011-02-02 23:21:58 -08001238 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001239 pdata->irqflags | IRQF_ONESHOT,
Dmitry Torokhovf053ea82012-07-07 16:18:33 -07001240 client->name, data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001241 if (error) {
1242 dev_err(&client->dev, "Failed to register interrupt\n");
1243 goto err_free_object;
1244 }
1245
Iiro Valkonen08960a02011-04-12 23:16:40 -07001246 error = mxt_make_highchg(data);
1247 if (error)
1248 goto err_free_irq;
1249
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001250 error = input_register_device(input_dev);
1251 if (error)
1252 goto err_free_irq;
1253
Iiro Valkonen7686b102011-02-02 23:21:58 -08001254 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001255 if (error)
1256 goto err_unregister_device;
1257
1258 return 0;
1259
1260err_unregister_device:
1261 input_unregister_device(input_dev);
1262 input_dev = NULL;
1263err_free_irq:
1264 free_irq(client->irq, data);
1265err_free_object:
1266 kfree(data->object_table);
1267err_free_mem:
1268 input_free_device(input_dev);
1269 kfree(data);
1270 return error;
1271}
1272
Bill Pembertone2619cf2012-11-23 21:50:47 -08001273static int mxt_remove(struct i2c_client *client)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001274{
Iiro Valkonen7686b102011-02-02 23:21:58 -08001275 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001276
Iiro Valkonen7686b102011-02-02 23:21:58 -08001277 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001278 free_irq(data->irq, data);
1279 input_unregister_device(data->input_dev);
1280 kfree(data->object_table);
1281 kfree(data);
1282
1283 return 0;
1284}
1285
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001286#ifdef CONFIG_PM_SLEEP
Iiro Valkonen7686b102011-02-02 23:21:58 -08001287static int mxt_suspend(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001288{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001289 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001290 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001291 struct input_dev *input_dev = data->input_dev;
1292
1293 mutex_lock(&input_dev->mutex);
1294
1295 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001296 mxt_stop(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001297
1298 mutex_unlock(&input_dev->mutex);
1299
1300 return 0;
1301}
1302
Iiro Valkonen7686b102011-02-02 23:21:58 -08001303static int mxt_resume(struct device *dev)
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001304{
Dmitry Torokhov8b5fce02010-11-18 00:14:03 -08001305 struct i2c_client *client = to_i2c_client(dev);
Iiro Valkonen7686b102011-02-02 23:21:58 -08001306 struct mxt_data *data = i2c_get_clientdata(client);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001307 struct input_dev *input_dev = data->input_dev;
1308
1309 /* Soft reset */
Iiro Valkonen81c88a72011-07-04 03:08:25 -07001310 mxt_write_object(data, MXT_GEN_COMMAND_T6,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001311 MXT_COMMAND_RESET, 1);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001312
Iiro Valkonen7686b102011-02-02 23:21:58 -08001313 msleep(MXT_RESET_TIME);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001314
1315 mutex_lock(&input_dev->mutex);
1316
1317 if (input_dev->users)
Iiro Valkonen7686b102011-02-02 23:21:58 -08001318 mxt_start(data);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001319
1320 mutex_unlock(&input_dev->mutex);
1321
1322 return 0;
1323}
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001324#endif
1325
Daniel Kurtz3a73c812012-05-08 22:29:14 -07001326static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1327
Iiro Valkonen7686b102011-02-02 23:21:58 -08001328static const struct i2c_device_id mxt_id[] = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001329 { "qt602240_ts", 0 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001330 { "atmel_mxt_ts", 0 },
Daniel Kurtz22dfab72013-03-07 19:43:33 -08001331 { "atmel_mxt_tp", 0 },
Chris Leech46ee2a02011-02-15 13:36:52 -08001332 { "mXT224", 0 },
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001333 { }
1334};
Iiro Valkonen7686b102011-02-02 23:21:58 -08001335MODULE_DEVICE_TABLE(i2c, mxt_id);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001336
Iiro Valkonen7686b102011-02-02 23:21:58 -08001337static struct i2c_driver mxt_driver = {
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001338 .driver = {
Iiro Valkonen7686b102011-02-02 23:21:58 -08001339 .name = "atmel_mxt_ts",
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001340 .owner = THIS_MODULE,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001341 .pm = &mxt_pm_ops,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001342 },
Iiro Valkonen7686b102011-02-02 23:21:58 -08001343 .probe = mxt_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001344 .remove = mxt_remove,
Iiro Valkonen7686b102011-02-02 23:21:58 -08001345 .id_table = mxt_id,
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001346};
1347
Axel Lin1b92c1c2012-03-16 23:05:41 -07001348module_i2c_driver(mxt_driver);
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001349
1350/* Module information */
1351MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
Iiro Valkonen7686b102011-02-02 23:21:58 -08001352MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
Joonyoung Shim4cf51c32010-07-14 21:55:30 -07001353MODULE_LICENSE("GPL");