blob: 75b3eb9f343d114573924b2f6339acb60ae622cd [file] [log] [blame]
Andrew Huangab45aab2013-06-27 10:06:56 +00001/* drivers/input/touchscreen/gt9xx.c
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002 *
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 *
5 * Linux Foundation chooses to take subject only to the GPLv2 license
6 * terms, and distributes only under these terms.
7 *
Andrew Huangab45aab2013-06-27 10:06:56 +00008 * 2010 - 2013 Goodix Technology.
Bingzhe Cai83caeea2013-07-16 17:57:37 +08009 *
Andrew Huangab45aab2013-06-27 10:06:56 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
Bingzhe Cai83caeea2013-07-16 17:57:37 +080014 *
15 * This program is distributed in the hope that it will be a reference
16 * to you, when you are integrating the GOODiX's CTP IC into your system,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Andrew Huangab45aab2013-06-27 10:06:56 +000019 * General Public License for more details.
Bingzhe Cai83caeea2013-07-16 17:57:37 +080020 *
Andrew Huangab45aab2013-06-27 10:06:56 +000021 * Version: 1.8
22 * Authors: andrew@goodix.com, meta@goodix.com
23 * Release Date: 2013/04/25
24 * Revision record:
Bingzhe Cai83caeea2013-07-16 17:57:37 +080025 * V1.0:
26 * first Release. By Andrew, 2012/08/31
Andrew Huangab45aab2013-06-27 10:06:56 +000027 * V1.2:
Bingzhe Cai83caeea2013-07-16 17:57:37 +080028 * modify gtp_reset_guitar,slot report,tracking_id & 0x0F.
29 * By Andrew, 2012/10/15
Andrew Huangab45aab2013-06-27 10:06:56 +000030 * V1.4:
31 * modify gt9xx_update.c. By Andrew, 2012/12/12
Bingzhe Cai83caeea2013-07-16 17:57:37 +080032 * V1.6:
Andrew Huangab45aab2013-06-27 10:06:56 +000033 * 1. new heartbeat/esd_protect mechanism(add external watchdog)
Bingzhe Cai83caeea2013-07-16 17:57:37 +080034 * 2. doze mode, sliding wakeup
35 * 3. 3 more cfg_group(GT9 Sensor_ID: 0~5)
Andrew Huangab45aab2013-06-27 10:06:56 +000036 * 3. config length verification
37 * 4. names & comments
38 * By Meta, 2013/03/11
39 * V1.8:
Bingzhe Cai83caeea2013-07-16 17:57:37 +080040 * 1. pen/stylus identification
Andrew Huangab45aab2013-06-27 10:06:56 +000041 * 2. read double check & fixed config support
42 * 2. new esd & slide wakeup optimization
43 * By Meta, 2013/06/08
44 */
45
Bingzhe Caic68e6112013-07-22 00:12:48 +080046#include <linux/regulator/consumer.h>
Andrew Huangab45aab2013-06-27 10:06:56 +000047#include "gt9xx.h"
48
Bingzhe Caid0361172013-07-18 19:22:11 +080049#include <linux/of_gpio.h>
50
Bingzhe Cai83caeea2013-07-16 17:57:37 +080051#include <linux/input/mt.h>
Andrew Huangab45aab2013-06-27 10:06:56 +000052
Bingzhe Cai6beb0112013-08-27 00:27:26 +080053#define GOODIX_DEV_NAME "Goodix-CTP"
Bingzhe Cai83caeea2013-07-16 17:57:37 +080054#define CFG_MAX_TOUCH_POINTS 5
55#define GOODIX_COORDS_ARR_SIZE 4
56#define MAX_BUTTONS 4
57
58/* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
59#define GTP_I2C_ADDRESS_HIGH 0x14
60#define GTP_I2C_ADDRESS_LOW 0x5D
61#define CFG_GROUP_LEN(p_cfg_grp) (sizeof(p_cfg_grp) / sizeof(p_cfg_grp[0]))
62
Bingzhe Caic68e6112013-07-22 00:12:48 +080063#define GOODIX_VTG_MIN_UV 2600000
64#define GOODIX_VTG_MAX_UV 3300000
65#define GOODIX_I2C_VTG_MIN_UV 1800000
66#define GOODIX_I2C_VTG_MAX_UV 1800000
67#define GOODIX_VDD_LOAD_MIN_UA 0
68#define GOODIX_VDD_LOAD_MAX_UA 10000
69#define GOODIX_VIO_LOAD_MIN_UA 0
70#define GOODIX_VIO_LOAD_MAX_UA 10000
71
Bingzhe Cai83caeea2013-07-16 17:57:37 +080072#define RESET_DELAY_T3_US 200 /* T3: > 100us */
73#define RESET_DELAY_T4 20 /* T4: > 5ms */
74
75#define PHY_BUF_SIZE 32
76
77#define GTP_MAX_TOUCH 5
78#define GTP_ESD_CHECK_CIRCLE_MS 2000
Andrew Huangab45aab2013-06-27 10:06:56 +000079
80#if GTP_HAVE_TOUCH_KEY
Bingzhe Cai83caeea2013-07-16 17:57:37 +080081static const u16 touch_key_array[] = {KEY_MENU, KEY_HOMEPAGE, KEY_BACK};
82#define GTP_MAX_KEY_NUM (sizeof(touch_key_array)/sizeof(touch_key_array[0]))
83
Andrew Huangab45aab2013-06-27 10:06:56 +000084#if GTP_DEBUG_ON
Bingzhe Cai83caeea2013-07-16 17:57:37 +080085static const int key_codes[] = {
86 KEY_HOME, KEY_BACK, KEY_MENU, KEY_SEARCH
87};
88static const char *const key_names[] = {
89 "Key_Home", "Key_Back", "Key_Menu", "Key_Search"
90};
Andrew Huangab45aab2013-06-27 10:06:56 +000091#endif
Andrew Huangab45aab2013-06-27 10:06:56 +000092#endif
93
Bingzhe Cai83caeea2013-07-16 17:57:37 +080094static void gtp_reset_guitar(struct goodix_ts_data *ts, int ms);
95static void gtp_int_sync(struct goodix_ts_data *ts, int ms);
96static int gtp_i2c_test(struct i2c_client *client);
Andrew Huangab45aab2013-06-27 10:06:56 +000097
Bingzhe Cai7d49bb32013-07-22 18:23:20 +080098#if defined(CONFIG_FB)
99static int fb_notifier_callback(struct notifier_block *self,
100 unsigned long event, void *data);
101#elif defined(CONFIG_HAS_EARLYSUSPEND)
Andrew Huangab45aab2013-06-27 10:06:56 +0000102static void goodix_ts_early_suspend(struct early_suspend *h);
103static void goodix_ts_late_resume(struct early_suspend *h);
104#endif
Andrew Huangab45aab2013-06-27 10:06:56 +0000105
106#if GTP_ESD_PROTECT
107static struct delayed_work gtp_esd_check_work;
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800108static struct workqueue_struct *gtp_esd_check_workqueue;
109static void gtp_esd_check_func(struct work_struct *work);
110static int gtp_init_ext_watchdog(struct i2c_client *client);
111struct i2c_client *i2c_connect_client;
Andrew Huangab45aab2013-06-27 10:06:56 +0000112#endif
113
Andrew Huangab45aab2013-06-27 10:06:56 +0000114#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800115enum doze_status {
116 DOZE_DISABLED = 0,
117 DOZE_ENABLED = 1,
118 DOZE_WAKEUP = 2,
119};
120static enum doze_status = DOZE_DISABLED;
Andrew Huangab45aab2013-06-27 10:06:56 +0000121static s8 gtp_enter_doze(struct goodix_ts_data *ts);
122#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800123bool init_done;
124static u8 chip_gt9xxs; /* true if ic is gt9xxs, like gt915s */
125u8 grp_cfg_version;
Andrew Huangab45aab2013-06-27 10:06:56 +0000126
127/*******************************************************
128Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800129 Read data from the i2c slave device.
Andrew Huangab45aab2013-06-27 10:06:56 +0000130Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800131 client: i2c device.
132 buf[0~1]: read start address.
133 buf[2~len-1]: read data buffer.
134 len: GTP_ADDR_LENGTH + read bytes count
Andrew Huangab45aab2013-06-27 10:06:56 +0000135Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800136 numbers of i2c_msgs to transfer:
137 2: succeed, otherwise: failed
Andrew Huangab45aab2013-06-27 10:06:56 +0000138*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800139int gtp_i2c_read(struct i2c_client *client, u8 *buf, int len)
Andrew Huangab45aab2013-06-27 10:06:56 +0000140{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800141 struct goodix_ts_data *ts = i2c_get_clientdata(client);
142 struct i2c_msg msgs[2];
143 int ret = -EIO;
144 int retries = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +0000145
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800146 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +0000147
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800148 msgs[0].flags = !I2C_M_RD;
149 msgs[0].addr = client->addr;
150 msgs[0].len = GTP_ADDR_LENGTH;
151 msgs[0].buf = &buf[0];
Andrew Huangab45aab2013-06-27 10:06:56 +0000152
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800153 msgs[1].flags = I2C_M_RD;
154 msgs[1].addr = client->addr;
155 msgs[1].len = len - GTP_ADDR_LENGTH;
156 msgs[1].buf = &buf[GTP_ADDR_LENGTH];
157
158 while (retries < 5) {
159 ret = i2c_transfer(client->adapter, msgs, 2);
160 if (ret == 2)
161 break;
162 retries++;
163 }
164 if (retries >= 5) {
165#if GTP_SLIDE_WAKEUP
166 /* reset chip would quit doze mode */
167 if (DOZE_ENABLED == doze_status)
168 return ret;
169#endif
170 GTP_DEBUG("I2C communication timeout, resetting chip...");
171 if (init_done)
172 gtp_reset_guitar(ts, 10);
173 else
174 dev_warn(&client->dev,
175 "<GTP> gtp_reset_guitar exit init_done=%d:\n",
176 init_done);
177 }
178 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +0000179}
180
181/*******************************************************
182Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800183 Write data to the i2c slave device.
Andrew Huangab45aab2013-06-27 10:06:56 +0000184Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800185 client: i2c device.
186 buf[0~1]: write start address.
187 buf[2~len-1]: data buffer
188 len: GTP_ADDR_LENGTH + write bytes count
Andrew Huangab45aab2013-06-27 10:06:56 +0000189Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800190 numbers of i2c_msgs to transfer:
191 1: succeed, otherwise: failed
Andrew Huangab45aab2013-06-27 10:06:56 +0000192*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800193int gtp_i2c_write(struct i2c_client *client, u8 *buf, int len)
Andrew Huangab45aab2013-06-27 10:06:56 +0000194{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800195 struct goodix_ts_data *ts = i2c_get_clientdata(client);
196 struct i2c_msg msg;
197 int ret = -EIO;
198 int retries = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +0000199
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800200 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +0000201
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800202 msg.flags = !I2C_M_RD;
203 msg.addr = client->addr;
204 msg.len = len;
205 msg.buf = buf;
Andrew Huangab45aab2013-06-27 10:06:56 +0000206
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800207 while (retries < 5) {
208 ret = i2c_transfer(client->adapter, &msg, 1);
209 if (ret == 1)
210 break;
211 retries++;
212 }
213 if ((retries >= 5)) {
214#if GTP_SLIDE_WAKEUP
215 if (DOZE_ENABLED == doze_status)
216 return ret;
217#endif
218 GTP_DEBUG("I2C communication timeout, resetting chip...");
219 if (init_done)
220 gtp_reset_guitar(ts, 10);
221 else
222 dev_warn(&client->dev,
223 "<GTP> gtp_reset_guitar exit init_done=%d:\n",
224 init_done);
225 }
226 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +0000227}
228/*******************************************************
229Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800230 i2c read twice, compare the results
Andrew Huangab45aab2013-06-27 10:06:56 +0000231Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800232 client: i2c device
233 addr: operate address
234 rxbuf: read data to store, if compare successful
235 len: bytes to read
Andrew Huangab45aab2013-06-27 10:06:56 +0000236Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800237 FAIL: read failed
238 SUCCESS: read successful
Andrew Huangab45aab2013-06-27 10:06:56 +0000239*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800240int gtp_i2c_read_dbl_check(struct i2c_client *client,
241 u16 addr, u8 *rxbuf, int len)
Andrew Huangab45aab2013-06-27 10:06:56 +0000242{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800243 u8 buf[16] = {0};
244 u8 confirm_buf[16] = {0};
245 u8 retry = 0;
246
247 while (retry++ < 3) {
248 memset(buf, 0xAA, 16);
249 buf[0] = (u8)(addr >> 8);
250 buf[1] = (u8)(addr & 0xFF);
251 gtp_i2c_read(client, buf, len + 2);
252
253 memset(confirm_buf, 0xAB, 16);
254 confirm_buf[0] = (u8)(addr >> 8);
255 confirm_buf[1] = (u8)(addr & 0xFF);
256 gtp_i2c_read(client, confirm_buf, len + 2);
257
258 if (!memcmp(buf, confirm_buf, len + 2))
259 break;
260 }
261 if (retry < 3) {
262 memcpy(rxbuf, confirm_buf + 2, len);
263 return SUCCESS;
264 } else {
265 dev_err(&client->dev,
266 "i2c read 0x%04X, %d bytes, double check failed!",
267 addr, len);
268 return FAIL;
269 }
Andrew Huangab45aab2013-06-27 10:06:56 +0000270}
271
272/*******************************************************
273Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800274 Send config data.
Andrew Huangab45aab2013-06-27 10:06:56 +0000275Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800276 client: i2c device.
Andrew Huangab45aab2013-06-27 10:06:56 +0000277Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800278 result of i2c write operation.
279 > 0: succeed, otherwise: failed
Andrew Huangab45aab2013-06-27 10:06:56 +0000280*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800281static int gtp_send_cfg(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +0000282{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800283 int ret;
Andrew Huangab45aab2013-06-27 10:06:56 +0000284#if GTP_DRIVER_SEND_CFG
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800285 int retry = 0;
286
287 if (ts->fixed_cfg) {
288 dev_dbg(&ts->client->dev,
289 "Ic fixed config, no config sent!");
290 ret = 2;
291 } else {
292 for (retry = 0; retry < 5; retry++) {
293 ret = gtp_i2c_write(ts->client,
294 ts->config_data,
295 GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH);
296 if (ret > 0)
297 break;
298 }
299 }
Andrew Huangab45aab2013-06-27 10:06:56 +0000300#endif
301
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800302 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +0000303}
304
305/*******************************************************
306Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800307 Disable irq function
Andrew Huangab45aab2013-06-27 10:06:56 +0000308Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800309 ts: goodix i2c_client private data
Andrew Huangab45aab2013-06-27 10:06:56 +0000310Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800311 None.
Andrew Huangab45aab2013-06-27 10:06:56 +0000312*********************************************************/
313void gtp_irq_disable(struct goodix_ts_data *ts)
314{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800315 unsigned long irqflags;
Andrew Huangab45aab2013-06-27 10:06:56 +0000316
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800317 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +0000318
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800319 spin_lock_irqsave(&ts->irq_lock, irqflags);
320 if (!ts->irq_is_disabled) {
321 ts->irq_is_disabled = true;
322 disable_irq_nosync(ts->client->irq);
323 }
324 spin_unlock_irqrestore(&ts->irq_lock, irqflags);
Andrew Huangab45aab2013-06-27 10:06:56 +0000325}
326
327/*******************************************************
328Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800329 Enable irq function
Andrew Huangab45aab2013-06-27 10:06:56 +0000330Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800331 ts: goodix i2c_client private data
Andrew Huangab45aab2013-06-27 10:06:56 +0000332Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800333 None.
Andrew Huangab45aab2013-06-27 10:06:56 +0000334*********************************************************/
335void gtp_irq_enable(struct goodix_ts_data *ts)
336{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800337 unsigned long irqflags = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +0000338
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800339 GTP_DEBUG_FUNC();
340
341 spin_lock_irqsave(&ts->irq_lock, irqflags);
342 if (ts->irq_is_disabled) {
343 enable_irq(ts->client->irq);
344 ts->irq_is_disabled = false;
345 }
346 spin_unlock_irqrestore(&ts->irq_lock, irqflags);
Andrew Huangab45aab2013-06-27 10:06:56 +0000347}
348
Andrew Huangab45aab2013-06-27 10:06:56 +0000349/*******************************************************
350Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800351 Report touch point event
Andrew Huangab45aab2013-06-27 10:06:56 +0000352Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800353 ts: goodix i2c_client private data
354 id: trackId
355 x: input x coordinate
356 y: input y coordinate
357 w: input pressure
Andrew Huangab45aab2013-06-27 10:06:56 +0000358Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800359 None.
Andrew Huangab45aab2013-06-27 10:06:56 +0000360*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800361static void gtp_touch_down(struct goodix_ts_data *ts, int id, int x, int y,
362 int w)
Andrew Huangab45aab2013-06-27 10:06:56 +0000363{
364#if GTP_CHANGE_X2Y
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800365 GTP_SWAP(x, y);
Andrew Huangab45aab2013-06-27 10:06:56 +0000366#endif
367
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800368 input_mt_slot(ts->input_dev, id);
Bingzhe Cai6beb0112013-08-27 00:27:26 +0800369 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800370 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
371 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
372 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
373 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
Andrew Huangab45aab2013-06-27 10:06:56 +0000374
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800375 GTP_DEBUG("ID:%d, X:%d, Y:%d, W:%d", id, x, y, w);
Andrew Huangab45aab2013-06-27 10:06:56 +0000376}
377
378/*******************************************************
379Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800380 Report touch release event
Andrew Huangab45aab2013-06-27 10:06:56 +0000381Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800382 ts: goodix i2c_client private data
Andrew Huangab45aab2013-06-27 10:06:56 +0000383Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800384 None.
Andrew Huangab45aab2013-06-27 10:06:56 +0000385*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800386static void gtp_touch_up(struct goodix_ts_data *ts, int id)
Andrew Huangab45aab2013-06-27 10:06:56 +0000387{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800388 input_mt_slot(ts->input_dev, id);
Bingzhe Cai6beb0112013-08-27 00:27:26 +0800389 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800390 GTP_DEBUG("Touch id[%2d] release!", id);
Andrew Huangab45aab2013-06-27 10:06:56 +0000391}
392
393
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800394
Andrew Huangab45aab2013-06-27 10:06:56 +0000395/*******************************************************
396Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800397 Goodix touchscreen work function
Andrew Huangab45aab2013-06-27 10:06:56 +0000398Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800399 work: work struct of goodix_workqueue
Andrew Huangab45aab2013-06-27 10:06:56 +0000400Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800401 None.
Andrew Huangab45aab2013-06-27 10:06:56 +0000402*********************************************************/
403static void goodix_ts_work_func(struct work_struct *work)
404{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800405 u8 end_cmd[3] = { GTP_READ_COOR_ADDR >> 8,
406 GTP_READ_COOR_ADDR & 0xFF, 0};
407 u8 point_data[2 + 1 + 8 * GTP_MAX_TOUCH + 1] = {
408 GTP_READ_COOR_ADDR >> 8,
409 GTP_READ_COOR_ADDR & 0xFF};
410 u8 touch_num = 0;
411 u8 finger = 0;
412 static u16 pre_touch;
413 static u8 pre_key;
Andrew Huangab45aab2013-06-27 10:06:56 +0000414#if GTP_WITH_PEN
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800415 static u8 pre_pen;
Andrew Huangab45aab2013-06-27 10:06:56 +0000416#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800417 u8 key_value = 0;
418 u8 *coor_data = NULL;
419 s32 input_x = 0;
420 s32 input_y = 0;
421 s32 input_w = 0;
422 s32 id = 0;
423 s32 i = 0;
424 int ret = -1;
425 struct goodix_ts_data *ts = NULL;
Andrew Huangab45aab2013-06-27 10:06:56 +0000426
427#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800428 u8 doze_buf[3] = {0x81, 0x4B};
Andrew Huangab45aab2013-06-27 10:06:56 +0000429#endif
430
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800431 GTP_DEBUG_FUNC();
432
433 ts = container_of(work, struct goodix_ts_data, work);
434#ifdef CONFIG_GT9XX_TOUCHPANEL_UPDATE
435 if (ts->enter_update)
436 return;
437#endif
438
Andrew Huangab45aab2013-06-27 10:06:56 +0000439#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800440 if (DOZE_ENABLED == doze_status) {
441 ret = gtp_i2c_read(ts->client, doze_buf, 3);
442 GTP_DEBUG("0x814B = 0x%02X", doze_buf[2]);
443 if (ret > 0) {
444 if (doze_buf[2] == 0xAA) {
445 dev_dbg(&ts->client->dev,
446 "Slide(0xAA) To Light up the screen!");
447 doze_status = DOZE_WAKEUP;
448 input_report_key(
449 ts->input_dev, KEY_POWER, 1);
450 input_sync(ts->input_dev);
451 input_report_key(
452 ts->input_dev, KEY_POWER, 0);
453 input_sync(ts->input_dev);
454 /* clear 0x814B */
455 doze_buf[2] = 0x00;
456 gtp_i2c_write(ts->client, doze_buf, 3);
457 } else if (doze_buf[2] == 0xBB) {
458 dev_dbg(&ts->client->dev,
459 "Slide(0xBB) To Light up the screen!");
460 doze_status = DOZE_WAKEUP;
461 input_report_key(ts->input_dev, KEY_POWER, 1);
462 input_sync(ts->input_dev);
463 input_report_key(ts->input_dev, KEY_POWER, 0);
464 input_sync(ts->input_dev);
465 /* clear 0x814B*/
466 doze_buf[2] = 0x00;
467 gtp_i2c_write(ts->client, doze_buf, 3);
468 } else if (0xC0 == (doze_buf[2] & 0xC0)) {
469 dev_dbg(&ts->client->dev,
470 "double click to light up the screen!");
471 doze_status = DOZE_WAKEUP;
472 input_report_key(ts->input_dev, KEY_POWER, 1);
473 input_sync(ts->input_dev);
474 input_report_key(ts->input_dev, KEY_POWER, 0);
475 input_sync(ts->input_dev);
476 /* clear 0x814B */
477 doze_buf[2] = 0x00;
478 gtp_i2c_write(ts->client, doze_buf, 3);
479 } else {
480 gtp_enter_doze(ts);
481 }
482 }
483 if (ts->use_irq)
484 gtp_irq_enable(ts);
485
486 return;
487 }
Andrew Huangab45aab2013-06-27 10:06:56 +0000488#endif
489
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800490 ret = gtp_i2c_read(ts->client, point_data, 12);
491 if (ret < 0) {
492 dev_err(&ts->client->dev,
493 "I2C transfer error. errno:%d\n ", ret);
494 goto exit_work_func;
495 }
Andrew Huangab45aab2013-06-27 10:06:56 +0000496
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800497 finger = point_data[GTP_ADDR_LENGTH];
498 if ((finger & 0x80) == 0)
499 goto exit_work_func;
Andrew Huangab45aab2013-06-27 10:06:56 +0000500
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800501 touch_num = finger & 0x0f;
502 if (touch_num > GTP_MAX_TOUCH)
503 goto exit_work_func;
Andrew Huangab45aab2013-06-27 10:06:56 +0000504
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800505 if (touch_num > 1) {
506 u8 buf[8 * GTP_MAX_TOUCH] = { (GTP_READ_COOR_ADDR + 10) >> 8,
507 (GTP_READ_COOR_ADDR + 10) & 0xff };
Andrew Huangab45aab2013-06-27 10:06:56 +0000508
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800509 ret = gtp_i2c_read(ts->client, buf,
510 2 + 8 * (touch_num - 1));
511 memcpy(&point_data[12], &buf[2], 8 * (touch_num - 1));
512 }
Andrew Huangab45aab2013-06-27 10:06:56 +0000513
514#if GTP_HAVE_TOUCH_KEY
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800515 key_value = point_data[3 + 8 * touch_num];
Andrew Huangab45aab2013-06-27 10:06:56 +0000516
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800517 if (key_value || pre_key) {
518 for (i = 0; i < GTP_MAX_KEY_NUM; i++) {
519#if GTP_DEBUG_ON
520 for (ret = 0; ret < 4; ++ret) {
521 if (key_codes[ret] == touch_key_array[i]) {
522 GTP_DEBUG("Key: %s %s",
523 key_names[ret],
524 (key_value & (0x01 << i))
525 ? "Down" : "Up");
526 break;
527 }
528 }
529#endif
530
531 input_report_key(ts->input_dev,
532 touch_key_array[i], key_value & (0x01<<i));
533 }
534 touch_num = 0;
535 pre_touch = 0;
536 }
537#endif
538 pre_key = key_value;
539
540 GTP_DEBUG("pre_touch:%02x, finger:%02x.", pre_touch, finger);
Andrew Huangab45aab2013-06-27 10:06:56 +0000541
Andrew Huangab45aab2013-06-27 10:06:56 +0000542#if GTP_WITH_PEN
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800543 if (pre_pen && (touch_num == 0)) {
544 GTP_DEBUG("Pen touch UP(Slot)!");
545 input_report_key(ts->input_dev, BTN_TOOL_PEN, 0);
546 input_mt_slot(ts->input_dev, 5);
547 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
548 pre_pen = 0;
549 }
Andrew Huangab45aab2013-06-27 10:06:56 +0000550#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800551 if (pre_touch || touch_num) {
552 s32 pos = 0;
553 u16 touch_index = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +0000554
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800555 coor_data = &point_data[3];
556 if (touch_num) {
557 id = coor_data[pos] & 0x0F;
558#if GTP_WITH_PEN
559 id = coor_data[pos];
560 if (id == 128) {
561 GTP_DEBUG("Pen touch DOWN(Slot)!");
562 input_x = coor_data[pos + 1]
563 | (coor_data[pos + 2] << 8);
564 input_y = coor_data[pos + 3]
565 | (coor_data[pos + 4] << 8);
566 input_w = coor_data[pos + 5]
567 | (coor_data[pos + 6] << 8);
Andrew Huangab45aab2013-06-27 10:06:56 +0000568
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800569 input_report_key(ts->input_dev,
570 BTN_TOOL_PEN, 1);
571 input_mt_slot(ts->input_dev, 5);
572 input_report_abs(ts->input_dev,
573 ABS_MT_TRACKING_ID, 5);
574 input_report_abs(ts->input_dev,
575 ABS_MT_POSITION_X, input_x);
576 input_report_abs(ts->input_dev,
577 ABS_MT_POSITION_Y, input_y);
578 input_report_abs(ts->input_dev,
579 ABS_MT_TOUCH_MAJOR, input_w);
580 GTP_DEBUG("Pen/Stylus: (%d, %d)[%d]",
581 input_x, input_y, input_w);
582 pre_pen = 1;
583 pre_touch = 0;
584 }
585#endif
586
587 touch_index |= (0x01<<id);
588 }
589
590 GTP_DEBUG("id = %d,touch_index = 0x%x, pre_touch = 0x%x\n",
591 id, touch_index, pre_touch);
592 for (i = 0; i < GTP_MAX_TOUCH; i++) {
593#if GTP_WITH_PEN
594 if (pre_pen == 1)
595 break;
596#endif
597 if (touch_index & (0x01<<i)) {
598 input_x = coor_data[pos + 1] |
599 coor_data[pos + 2] << 8;
600 input_y = coor_data[pos + 3] |
601 coor_data[pos + 4] << 8;
602 input_w = coor_data[pos + 5] |
603 coor_data[pos + 6] << 8;
604
605 gtp_touch_down(ts, id,
606 input_x, input_y, input_w);
607 pre_touch |= 0x01 << i;
608
609 pos += 8;
610 id = coor_data[pos] & 0x0F;
611 touch_index |= (0x01<<id);
612 } else {
613 gtp_touch_up(ts, i);
614 pre_touch &= ~(0x01 << i);
615 }
616 }
617 }
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800618 input_sync(ts->input_dev);
Andrew Huangab45aab2013-06-27 10:06:56 +0000619
620exit_work_func:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800621 if (!ts->gtp_rawdiff_mode) {
622 ret = gtp_i2c_write(ts->client, end_cmd, 3);
623 if (ret < 0)
624 dev_warn(&ts->client->dev, "I2C write end_cmd error!\n");
625
626 }
627 if (ts->use_irq)
628 gtp_irq_enable(ts);
629
630 return;
Andrew Huangab45aab2013-06-27 10:06:56 +0000631}
632
633/*******************************************************
634Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800635 Timer interrupt service routine for polling mode.
Andrew Huangab45aab2013-06-27 10:06:56 +0000636Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800637 timer: timer struct pointer
Andrew Huangab45aab2013-06-27 10:06:56 +0000638Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800639 Timer work mode.
640 HRTIMER_NORESTART: no restart mode
Andrew Huangab45aab2013-06-27 10:06:56 +0000641*********************************************************/
642static enum hrtimer_restart goodix_ts_timer_handler(struct hrtimer *timer)
643{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800644 struct goodix_ts_data
645 *ts = container_of(timer, struct goodix_ts_data, timer);
Andrew Huangab45aab2013-06-27 10:06:56 +0000646
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800647 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +0000648
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800649 queue_work(ts->goodix_wq, &ts->work);
650 hrtimer_start(&ts->timer, ktime_set(0, (GTP_POLL_TIME + 6) * 1000000),
651 HRTIMER_MODE_REL);
652 return HRTIMER_NORESTART;
Andrew Huangab45aab2013-06-27 10:06:56 +0000653}
654
655/*******************************************************
656Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800657 External interrupt service routine for interrupt mode.
Andrew Huangab45aab2013-06-27 10:06:56 +0000658Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800659 irq: interrupt number.
660 dev_id: private data pointer
Andrew Huangab45aab2013-06-27 10:06:56 +0000661Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800662 Handle Result.
663 IRQ_HANDLED: interrupt handled successfully
Andrew Huangab45aab2013-06-27 10:06:56 +0000664*********************************************************/
665static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
666{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800667 struct goodix_ts_data *ts = dev_id;
Andrew Huangab45aab2013-06-27 10:06:56 +0000668
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800669 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +0000670
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800671 gtp_irq_disable(ts);
672
673 queue_work(ts->goodix_wq, &ts->work);
674
675 return IRQ_HANDLED;
Andrew Huangab45aab2013-06-27 10:06:56 +0000676}
677/*******************************************************
678Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800679 Synchronization.
Andrew Huangab45aab2013-06-27 10:06:56 +0000680Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800681 ms: synchronization time in millisecond.
Andrew Huangab45aab2013-06-27 10:06:56 +0000682Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800683 None.
Andrew Huangab45aab2013-06-27 10:06:56 +0000684*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800685void gtp_int_sync(struct goodix_ts_data *ts, int ms)
Andrew Huangab45aab2013-06-27 10:06:56 +0000686{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800687 gpio_direction_output(ts->pdata->irq_gpio, 0);
688 msleep(ms);
689 gpio_direction_input(ts->pdata->irq_gpio);
Andrew Huangab45aab2013-06-27 10:06:56 +0000690}
691
692/*******************************************************
693Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800694 Reset chip.
Andrew Huangab45aab2013-06-27 10:06:56 +0000695Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800696 ms: reset time in millisecond, must >10ms
Andrew Huangab45aab2013-06-27 10:06:56 +0000697Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800698 None.
Andrew Huangab45aab2013-06-27 10:06:56 +0000699*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800700static void gtp_reset_guitar(struct goodix_ts_data *ts, int ms)
Andrew Huangab45aab2013-06-27 10:06:56 +0000701{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800702 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +0000703
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800704 /* This reset sequence will selcet I2C slave address */
705 gpio_direction_output(ts->pdata->reset_gpio, 0);
706 msleep(ms);
Andrew Huangab45aab2013-06-27 10:06:56 +0000707
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800708 if (ts->client->addr == GTP_I2C_ADDRESS_HIGH)
709 gpio_direction_output(ts->pdata->irq_gpio, 1);
710 else
711 gpio_direction_output(ts->pdata->irq_gpio, 0);
Andrew Huangab45aab2013-06-27 10:06:56 +0000712
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800713 usleep(RESET_DELAY_T3_US);
714 gpio_direction_output(ts->pdata->reset_gpio, 1);
715 msleep(RESET_DELAY_T4);
Andrew Huangab45aab2013-06-27 10:06:56 +0000716
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800717 gpio_direction_input(ts->pdata->reset_gpio);
718
719 gtp_int_sync(ts, 50);
720
Andrew Huangab45aab2013-06-27 10:06:56 +0000721#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800722 gtp_init_ext_watchdog(ts->client);
Andrew Huangab45aab2013-06-27 10:06:56 +0000723#endif
724}
725
Bingzhe Cai7d49bb32013-07-22 18:23:20 +0800726#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_FB)
Andrew Huangab45aab2013-06-27 10:06:56 +0000727#if GTP_SLIDE_WAKEUP
728/*******************************************************
729Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800730 Enter doze mode for sliding wakeup.
Andrew Huangab45aab2013-06-27 10:06:56 +0000731Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800732 ts: goodix tp private data
Andrew Huangab45aab2013-06-27 10:06:56 +0000733Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800734 1: succeed, otherwise failed
Andrew Huangab45aab2013-06-27 10:06:56 +0000735*******************************************************/
736static s8 gtp_enter_doze(struct goodix_ts_data *ts)
737{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800738 int ret = -1;
739 s8 retry = 0;
740 u8 i2c_control_buf[3] = {
741 (u8)(GTP_REG_SLEEP >> 8),
742 (u8)GTP_REG_SLEEP, 8};
Andrew Huangab45aab2013-06-27 10:06:56 +0000743
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800744 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +0000745
746#if GTP_DBL_CLK_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800747 i2c_control_buf[2] = 0x09;
Andrew Huangab45aab2013-06-27 10:06:56 +0000748#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800749 gtp_irq_disable(ts);
Andrew Huangab45aab2013-06-27 10:06:56 +0000750
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800751 GTP_DEBUG("entering doze mode...");
752 while (retry++ < 5) {
753 i2c_control_buf[0] = 0x80;
754 i2c_control_buf[1] = 0x46;
755 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
756 if (ret < 0) {
757 GTP_DEBUG(
758 "failed to set doze flag into 0x8046, %d",
759 retry);
760 continue;
761 }
762 i2c_control_buf[0] = 0x80;
763 i2c_control_buf[1] = 0x40;
764 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
765 if (ret > 0) {
766 doze_status = DOZE_ENABLED;
767 dev_dbg(&ts->client->dev,
768 "GTP has been working in doze mode!");
769 gtp_irq_enable(ts);
770 return ret;
771 }
772 msleep(20);
773 }
774 dev_err(&ts->client->dev, "GTP send doze cmd failed.\n");
775 gtp_irq_enable(ts);
776 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +0000777}
Andrew Huangab45aab2013-06-27 10:06:56 +0000778#else
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800779/*******************************************************
780Function:
781 Enter sleep mode.
782Input:
783 ts: private data.
784Output:
785 Executive outcomes.
786 1: succeed, otherwise failed.
787*******************************************************/
788static s8 gtp_enter_sleep(struct goodix_ts_data *ts)
789{
790 int ret = -1;
791 s8 retry = 0;
792 u8 i2c_control_buf[3] = {
793 (u8)(GTP_REG_SLEEP >> 8),
794 (u8)GTP_REG_SLEEP, 5};
Andrew Huangab45aab2013-06-27 10:06:56 +0000795
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800796 GTP_DEBUG_FUNC();
797
798 ret = gpio_direction_output(ts->pdata->irq_gpio, 0);
799 usleep(5000);
800 while (retry++ < 5) {
801 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
802 if (ret > 0) {
803 dev_dbg(&ts->client->dev,
804 "GTP enter sleep!");
805 return ret;
806 }
807 msleep(20);
808 }
809 dev_err(&ts->client->dev, "GTP send sleep cmd failed.\n");
810 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +0000811}
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800812#endif
Andrew Huangab45aab2013-06-27 10:06:56 +0000813
814/*******************************************************
815Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800816 Wakeup from sleep.
Andrew Huangab45aab2013-06-27 10:06:56 +0000817Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800818 ts: private data.
Andrew Huangab45aab2013-06-27 10:06:56 +0000819Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800820 Executive outcomes.
821 >0: succeed, otherwise: failed.
Andrew Huangab45aab2013-06-27 10:06:56 +0000822*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800823static s8 gtp_wakeup_sleep(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +0000824{
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800825 u8 retry = 0;
826 s8 ret = -1;
827
828 GTP_DEBUG_FUNC();
829
830#if GTP_POWER_CTRL_SLEEP
Bingzhe Cai7d49bb32013-07-22 18:23:20 +0800831 gtp_reset_guitar(ts, 20);
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800832
Bingzhe Cai7d49bb32013-07-22 18:23:20 +0800833 ret = gtp_send_cfg(ts);
834 if (ret > 0) {
835 dev_dbg(&ts->client->dev,
836 "Wakeup sleep send config success.");
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800837 return 1;
838 }
839#else
840 while (retry++ < 10) {
841#if GTP_SLIDE_WAKEUP
842 /* wakeup not by slide */
843 if (DOZE_WAKEUP != doze_status)
844 gtp_reset_guitar(ts, 10);
845 else
846 /* wakeup by slide */
847 doze_status = DOZE_DISABLED;
848#else
849 if (chip_gt9xxs == 1) {
850 gtp_reset_guitar(ts, 10);
851 } else {
852 ret = gpio_direction_output(ts->pdata->irq_gpio, 1);
853 usleep(5000);
854 }
855#endif
856 ret = gtp_i2c_test(ts->client);
857 if (ret > 0) {
858 dev_dbg(&ts->client->dev, "GTP wakeup sleep.");
859#if (!GTP_SLIDE_WAKEUP)
860 if (chip_gt9xxs == 0) {
861 gtp_int_sync(ts, 25);
862 msleep(20);
863#if GTP_ESD_PROTECT
864 gtp_init_ext_watchdog(ts->client);
865#endif
866 }
867#endif
868 return ret;
869 }
870 gtp_reset_guitar(ts, 20);
871 }
872#endif
873
874 dev_err(&ts->client->dev, "GTP wakeup sleep failed.\n");
875 return ret;
876}
Bingzhe Cai7d49bb32013-07-22 18:23:20 +0800877#endif /* !CONFIG_HAS_EARLYSUSPEND && !CONFIG_FB*/
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800878
879/*******************************************************
880Function:
881 Initialize gtp.
882Input:
883 ts: goodix private data
884Output:
885 Executive outcomes.
886 > =0: succeed, otherwise: failed
887*******************************************************/
888static int gtp_init_panel(struct goodix_ts_data *ts)
889{
890 struct i2c_client *client = ts->client;
891 unsigned char *config_data;
892 int ret = -EIO;
Andrew Huangab45aab2013-06-27 10:06:56 +0000893
894#if GTP_DRIVER_SEND_CFG
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800895 int i;
896 u8 check_sum = 0;
897 u8 opr_buf[16];
898 u8 sensor_id = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +0000899
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800900 u8 cfg_info_group1[] = CTP_CFG_GROUP1;
901 u8 cfg_info_group2[] = CTP_CFG_GROUP2;
902 u8 cfg_info_group3[] = CTP_CFG_GROUP3;
903 u8 cfg_info_group4[] = CTP_CFG_GROUP4;
904 u8 cfg_info_group5[] = CTP_CFG_GROUP5;
905 u8 cfg_info_group6[] = CTP_CFG_GROUP6;
906 u8 *send_cfg_buf[] = {cfg_info_group1, cfg_info_group2,
907 cfg_info_group3, cfg_info_group4,
908 cfg_info_group5, cfg_info_group6};
Andrew Huangab45aab2013-06-27 10:06:56 +0000909
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800910 u8 cfg_info_len[] = {CFG_GROUP_LEN(cfg_info_group1),
911 CFG_GROUP_LEN(cfg_info_group2),
912 CFG_GROUP_LEN(cfg_info_group3),
913 CFG_GROUP_LEN(cfg_info_group4),
914 CFG_GROUP_LEN(cfg_info_group5),
915 CFG_GROUP_LEN(cfg_info_group6)};
Andrew Huangab45aab2013-06-27 10:06:56 +0000916
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800917 GTP_DEBUG("Config Groups\' Lengths: %d, %d, %d, %d, %d, %d",
918 cfg_info_len[0], cfg_info_len[1], cfg_info_len[2],
919 cfg_info_len[3], cfg_info_len[4], cfg_info_len[5]);
Andrew Huangab45aab2013-06-27 10:06:56 +0000920
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800921 ret = gtp_i2c_read_dbl_check(ts->client, 0x41E4, opr_buf, 1);
922 if (SUCCESS == ret) {
923 if (opr_buf[0] != 0xBE) {
924 ts->fw_error = 1;
925 dev_err(&client->dev,
926 "Firmware error, no config sent!");
927 return -EINVAL;
928 }
929 }
930 if ((!cfg_info_len[1]) && (!cfg_info_len[2]) && (!cfg_info_len[3])
931 && (!cfg_info_len[4]) && (!cfg_info_len[5])) {
932 sensor_id = 0;
933 } else {
934 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_SENSOR_ID,
935 &sensor_id, 1);
936 if (SUCCESS == ret) {
937 if (sensor_id >= 0x06) {
938 dev_err(&client->dev,
939 "Invalid sensor_id(0x%02X), No Config Sent!",
940 sensor_id);
941 return -EINVAL;
942 }
943 } else {
944 dev_err(&client->dev,
945 "Failed to get sensor_id, No config sent!");
946 return -EINVAL;
947 }
948 }
949 GTP_DEBUG("Sensor_ID: %d", sensor_id);
950
951 ts->gtp_cfg_len = cfg_info_len[sensor_id];
952
953 if (ts->gtp_cfg_len < GTP_CONFIG_MIN_LENGTH) {
954 dev_err(&client->dev,
955 "Sensor_ID(%d) matches with NULL or INVALID CONFIG GROUP! NO Config Sent! You need to check you header file CFG_GROUP section!\n",
956 sensor_id);
957 return -EINVAL;
958 }
959 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CONFIG_DATA,
960 &opr_buf[0], 1);
961
962 if (ret == SUCCESS) {
963 if (opr_buf[0] < 90) {
964 /* backup group config version */
965 grp_cfg_version = send_cfg_buf[sensor_id][0];
966 send_cfg_buf[sensor_id][0] = 0x00;
967 ts->fixed_cfg = 0;
968 } else {
969 /* treated as fixed config, not send config */
970 dev_warn(&client->dev,
971 "Ic fixed config with config version(%d, 0x%02X)",
972 opr_buf[0], opr_buf[0]);
973 ts->fixed_cfg = 1;
974 }
975 } else {
976 dev_err(&client->dev,
977 "Failed to get ic config version!No config sent!");
978 return -EINVAL;
979 }
980
Bingzhe Caid0361172013-07-18 19:22:11 +0800981 if (ts->pdata->gtp_cfg_len) {
982 config_data = ts->pdata->config_data;
983 ts->config_data = ts->pdata->config_data;
984 ts->gtp_cfg_len = ts->pdata->gtp_cfg_len;
985 } else {
986 config_data = devm_kzalloc(&client->dev,
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800987 GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH,
Bingzhe Caid0361172013-07-18 19:22:11 +0800988 GFP_KERNEL);
989 if (!config_data) {
990 dev_err(&client->dev,
991 "Not enough memory for panel config data\n");
992 return -ENOMEM;
993 }
Bingzhe Cai83caeea2013-07-16 17:57:37 +0800994
Bingzhe Caid0361172013-07-18 19:22:11 +0800995 ts->config_data = config_data;
996 config_data[0] = GTP_REG_CONFIG_DATA >> 8;
997 config_data[1] = GTP_REG_CONFIG_DATA & 0xff;
998 memset(&config_data[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
999 memcpy(&config_data[GTP_ADDR_LENGTH], send_cfg_buf[sensor_id],
1000 ts->gtp_cfg_len);
1001 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001002
1003#if GTP_CUSTOM_CFG
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001004 config_data[RESOLUTION_LOC] =
1005 (unsigned char)(GTP_MAX_WIDTH && 0xFF);
1006 config_data[RESOLUTION_LOC + 1] =
1007 (unsigned char)(GTP_MAX_WIDTH >> 8);
1008 config_data[RESOLUTION_LOC + 2] =
1009 (unsigned char)(GTP_MAX_HEIGHT && 0xFF);
1010 config_data[RESOLUTION_LOC + 3] =
1011 (unsigned char)(GTP_MAX_HEIGHT >> 8);
Andrew Huangab45aab2013-06-27 10:06:56 +00001012
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001013 if (GTP_INT_TRIGGER == 0)
1014 config_data[TRIGGER_LOC] &= 0xfe;
1015 else if (GTP_INT_TRIGGER == 1)
1016 config_data[TRIGGER_LOC] |= 0x01;
1017#endif /* !GTP_CUSTOM_CFG */
Andrew Huangab45aab2013-06-27 10:06:56 +00001018
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001019 check_sum = 0;
1020 for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1021 check_sum += config_data[i];
1022
1023 config_data[ts->gtp_cfg_len] = (~check_sum) + 1;
1024
1025#else /* DRIVER NOT SEND CONFIG */
1026 ts->gtp_cfg_len = GTP_CONFIG_MAX_LENGTH;
1027 ret = gtp_i2c_read(ts->client, config_data,
1028 ts->gtp_cfg_len + GTP_ADDR_LENGTH);
1029 if (ret < 0) {
1030 dev_err(&client->dev,
1031 "Read Config Failed, Using DEFAULT Resolution & INT Trigger!\n");
1032 ts->abs_x_max = GTP_MAX_WIDTH;
1033 ts->abs_y_max = GTP_MAX_HEIGHT;
1034 ts->int_trigger_type = GTP_INT_TRIGGER;
1035 }
1036#endif /* !DRIVER NOT SEND CONFIG */
1037
1038 GTP_DEBUG_FUNC();
1039 if ((ts->abs_x_max == 0) && (ts->abs_y_max == 0)) {
1040 ts->abs_x_max = (config_data[RESOLUTION_LOC + 1] << 8)
1041 + config_data[RESOLUTION_LOC];
1042 ts->abs_y_max = (config_data[RESOLUTION_LOC + 3] << 8)
1043 + config_data[RESOLUTION_LOC + 2];
1044 ts->int_trigger_type = (config_data[TRIGGER_LOC]) & 0x03;
1045 }
1046 ret = gtp_send_cfg(ts);
1047 if (ret < 0)
1048 dev_err(&client->dev, "%s: Send config error.\n", __func__);
1049
1050 GTP_DEBUG("X_MAX = %d, Y_MAX = %d, TRIGGER = 0x%02x",
1051 ts->abs_x_max, ts->abs_y_max,
1052 ts->int_trigger_type);
1053
1054 msleep(20);
1055 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001056}
1057
1058/*******************************************************
1059Function:
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301060 Read firmware version
Andrew Huangab45aab2013-06-27 10:06:56 +00001061Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001062 client: i2c device
1063 version: buffer to keep ic firmware version
Andrew Huangab45aab2013-06-27 10:06:56 +00001064Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001065 read operation return.
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301066 0: succeed, otherwise: failed
Andrew Huangab45aab2013-06-27 10:06:56 +00001067*******************************************************/
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301068static int gtp_read_fw_version(struct i2c_client *client, u16 *version)
Andrew Huangab45aab2013-06-27 10:06:56 +00001069{
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301070 int ret = 0;
1071 u8 buf[GTP_FW_VERSION_BUFFER_MAXSIZE] = {
1072 GTP_REG_FW_VERSION >> 8, GTP_REG_FW_VERSION & 0xff };
Andrew Huangab45aab2013-06-27 10:06:56 +00001073
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001074 ret = gtp_i2c_read(client, buf, sizeof(buf));
1075 if (ret < 0) {
1076 dev_err(&client->dev, "GTP read version failed.\n");
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301077 return -EIO;
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001078 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001079
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001080 if (version)
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301081 *version = (buf[3] << 8) | buf[2];
1082
1083 return ret;
1084}
1085/*******************************************************
1086Function:
1087 Read and check chip id.
1088Input:
1089 client: i2c device
1090Output:
1091 read operation return.
1092 0: succeed, otherwise: failed
1093*******************************************************/
1094static int gtp_check_product_id(struct i2c_client *client)
1095{
1096 int ret = 0;
1097 char product_id[GTP_PRODUCT_ID_MAXSIZE];
1098 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1099 /* 04 bytes are used for the Product-id in the register space.*/
1100 u8 buf[GTP_PRODUCT_ID_BUFFER_MAXSIZE] = {
1101 GTP_REG_PRODUCT_ID >> 8, GTP_REG_PRODUCT_ID & 0xff };
1102
1103 ret = gtp_i2c_read(client, buf, sizeof(buf));
1104 if (ret < 0) {
1105 dev_err(&client->dev, "GTP read version failed.\n");
1106 return -EIO;
1107 }
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001108
1109 if (buf[5] == 0x00) {
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301110 /* copy (GTP_PRODUCT_ID_MAXSIZE - 1) from buffer. Ex: 915 */
1111 strlcpy(product_id, &buf[2], GTP_PRODUCT_ID_MAXSIZE - 1);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001112 } else {
1113 if (buf[5] == 'S' || buf[5] == 's')
1114 chip_gt9xxs = 1;
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301115 /* copy GTP_PRODUCT_ID_MAXSIZE from buffer. Ex: 915s */
1116 strlcpy(product_id, &buf[2], GTP_PRODUCT_ID_MAXSIZE);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001117 }
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301118
1119 dev_info(&client->dev, "Goodix Product ID = %s\n", product_id);
1120
1121 if (!IS_ERR(ts->pdata->product_id))
1122 ret = strcmp(product_id, ts->pdata->product_id);
1123
1124 if (ret != 0)
1125 return -EINVAL;
1126
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001127 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001128}
1129
1130/*******************************************************
1131Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001132 I2c test Function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001133Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001134 client:i2c client.
Andrew Huangab45aab2013-06-27 10:06:56 +00001135Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001136 Executive outcomes.
1137 2: succeed, otherwise failed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001138*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001139static int gtp_i2c_test(struct i2c_client *client)
Andrew Huangab45aab2013-06-27 10:06:56 +00001140{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001141 u8 buf[3] = { GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff };
1142 int retry = 5;
1143 int ret = -EIO;
1144
1145 GTP_DEBUG_FUNC();
1146
1147 while (retry--) {
1148 ret = gtp_i2c_read(client, buf, 3);
1149 if (ret > 0)
1150 return ret;
1151 dev_err(&client->dev, "GTP i2c test failed time %d.\n", retry);
1152 msleep(20);
1153 }
1154 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001155}
1156
1157/*******************************************************
1158Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001159 Request gpio(INT & RST) ports.
Andrew Huangab45aab2013-06-27 10:06:56 +00001160Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001161 ts: private data.
Andrew Huangab45aab2013-06-27 10:06:56 +00001162Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001163 Executive outcomes.
1164 = 0: succeed, != 0: failed
Andrew Huangab45aab2013-06-27 10:06:56 +00001165*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001166static int gtp_request_io_port(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001167{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001168 struct i2c_client *client = ts->client;
1169 struct goodix_ts_platform_data *pdata = ts->pdata;
1170 int ret;
1171 if (gpio_is_valid(pdata->irq_gpio)) {
1172 ret = gpio_request(pdata->irq_gpio, "goodix_ts_irq_gpio");
1173 if (ret) {
1174 dev_err(&client->dev, "irq gpio request failed\n");
1175 goto pwr_off;
1176 }
1177 ret = gpio_direction_input(pdata->irq_gpio);
1178 if (ret) {
1179 dev_err(&client->dev,
1180 "set_direction for irq gpio failed\n");
1181 goto free_irq_gpio;
1182 }
1183 } else {
1184 dev_err(&client->dev, "irq gpio is invalid!\n");
1185 ret = -EINVAL;
1186 goto free_irq_gpio;
1187 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001188
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001189 if (gpio_is_valid(pdata->reset_gpio)) {
1190 ret = gpio_request(pdata->reset_gpio, "goodix_ts__reset_gpio");
1191 if (ret) {
1192 dev_err(&client->dev, "reset gpio request failed\n");
1193 goto free_irq_gpio;
1194 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001195
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001196 ret = gpio_direction_output(pdata->reset_gpio, 0);
1197 if (ret) {
1198 dev_err(&client->dev,
1199 "set_direction for reset gpio failed\n");
1200 goto free_reset_gpio;
1201 }
1202 } else {
1203 dev_err(&client->dev, "reset gpio is invalid!\n");
1204 ret = -EINVAL;
1205 goto free_reset_gpio;
1206 }
1207 gpio_direction_input(pdata->reset_gpio);
Andrew Huangab45aab2013-06-27 10:06:56 +00001208
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001209 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001210
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001211free_reset_gpio:
1212 if (gpio_is_valid(pdata->reset_gpio))
1213 gpio_free(pdata->reset_gpio);
1214free_irq_gpio:
1215 if (gpio_is_valid(pdata->irq_gpio))
1216 gpio_free(pdata->irq_gpio);
1217pwr_off:
1218 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001219}
1220
1221/*******************************************************
1222Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001223 Request interrupt.
Andrew Huangab45aab2013-06-27 10:06:56 +00001224Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001225 ts: private data.
Andrew Huangab45aab2013-06-27 10:06:56 +00001226Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001227 Executive outcomes.
1228 0: succeed, -1: failed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001229*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001230static int gtp_request_irq(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001231{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001232 int ret;
1233 const u8 irq_table[] = GTP_IRQ_TAB;
Andrew Huangab45aab2013-06-27 10:06:56 +00001234
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001235 GTP_DEBUG("INT trigger type:%x, irq=%d", ts->int_trigger_type,
1236 ts->client->irq);
Andrew Huangab45aab2013-06-27 10:06:56 +00001237
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001238 ret = request_irq(ts->client->irq, goodix_ts_irq_handler,
1239 irq_table[ts->int_trigger_type],
1240 ts->client->name, ts);
1241 if (ret) {
1242 dev_err(&ts->client->dev, "Request IRQ failed!ERRNO:%d.\n",
1243 ret);
1244 gpio_direction_input(ts->pdata->irq_gpio);
Andrew Huangab45aab2013-06-27 10:06:56 +00001245
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001246 hrtimer_init(&ts->timer, CLOCK_MONOTONIC,
1247 HRTIMER_MODE_REL);
1248 ts->timer.function = goodix_ts_timer_handler;
1249 hrtimer_start(&ts->timer, ktime_set(1, 0),
1250 HRTIMER_MODE_REL);
1251 ts->use_irq = false;
1252 return ret;
1253 } else {
1254 gtp_irq_disable(ts);
1255 ts->use_irq = true;
1256 return 0;
1257 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001258}
1259
1260/*******************************************************
1261Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001262 Request input device Function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001263Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001264 ts:private data.
Andrew Huangab45aab2013-06-27 10:06:56 +00001265Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001266 Executive outcomes.
1267 0: succeed, otherwise: failed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001268*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001269static int gtp_request_input_dev(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001270{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001271 int ret;
1272 char phys[PHY_BUF_SIZE];
Andrew Huangab45aab2013-06-27 10:06:56 +00001273#if GTP_HAVE_TOUCH_KEY
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001274 int index = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00001275#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001276
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001277 GTP_DEBUG_FUNC();
1278
1279 ts->input_dev = input_allocate_device();
1280 if (ts->input_dev == NULL) {
1281 dev_err(&ts->client->dev,
1282 "Failed to allocate input device.\n");
1283 return -ENOMEM;
1284 }
1285
1286 ts->input_dev->evbit[0] =
1287 BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
Bingzhe Cai6beb0112013-08-27 00:27:26 +08001288 set_bit(BTN_TOOL_FINGER, ts->input_dev->keybit);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001289 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
1290 input_mt_init_slots(ts->input_dev, 10);/* in case of "out of memory" */
Andrew Huangab45aab2013-06-27 10:06:56 +00001291
1292#if GTP_HAVE_TOUCH_KEY
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001293 for (index = 0; index < GTP_MAX_KEY_NUM; index++) {
1294 input_set_capability(ts->input_dev,
1295 EV_KEY, touch_key_array[index]);
1296 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001297#endif
1298
1299#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001300 input_set_capability(ts->input_dev, EV_KEY, KEY_POWER);
1301#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001302
1303#if GTP_WITH_PEN
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001304 /* pen support */
1305 __set_bit(BTN_TOOL_PEN, ts->input_dev->keybit);
1306 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
1307 __set_bit(INPUT_PROP_POINTER, ts->input_dev->propbit);
Andrew Huangab45aab2013-06-27 10:06:56 +00001308#endif
1309
1310#if GTP_CHANGE_X2Y
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001311 GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
Andrew Huangab45aab2013-06-27 10:06:56 +00001312#endif
1313
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001314 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
1315 0, ts->abs_x_max, 0, 0);
1316 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
1317 0, ts->abs_y_max, 0, 0);
1318 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR,
1319 0, 255, 0, 0);
1320 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR,
1321 0, 255, 0, 0);
1322 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID,
1323 0, 255, 0, 0);
Andrew Huangab45aab2013-06-27 10:06:56 +00001324
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001325 snprintf(phys, PHY_BUF_SIZE, "input/ts");
1326 ts->input_dev->name = GOODIX_DEV_NAME;
1327 ts->input_dev->phys = phys;
1328 ts->input_dev->id.bustype = BUS_I2C;
1329 ts->input_dev->id.vendor = 0xDEAD;
1330 ts->input_dev->id.product = 0xBEEF;
1331 ts->input_dev->id.version = 10427;
Andrew Huangab45aab2013-06-27 10:06:56 +00001332
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001333 ret = input_register_device(ts->input_dev);
1334 if (ret) {
1335 dev_err(&ts->client->dev,
1336 "Register %s input device failed.\n",
1337 ts->input_dev->name);
1338 goto exit_free_inputdev;
1339 }
1340
1341 return 0;
1342
1343exit_free_inputdev:
1344 input_free_device(ts->input_dev);
1345 ts->input_dev = NULL;
1346 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001347}
1348
Bingzhe Caic68e6112013-07-22 00:12:48 +08001349static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1350{
1351 return (regulator_count_voltages(reg) > 0) ?
1352 regulator_set_optimum_mode(reg, load_uA) : 0;
1353}
1354
1355/**
1356 * goodix_power_on - Turn device power ON
1357 * @ts: driver private data
1358 *
1359 * Returns zero on success, else an error.
1360 */
1361static int goodix_power_on(struct goodix_ts_data *ts)
1362{
1363 int ret;
1364
1365 if (!IS_ERR(ts->avdd)) {
1366 ret = reg_set_optimum_mode_check(ts->avdd,
1367 GOODIX_VDD_LOAD_MAX_UA);
1368 if (ret < 0) {
1369 dev_err(&ts->client->dev,
1370 "Regulator avdd set_opt failed rc=%d\n", ret);
1371 goto err_set_opt_avdd;
1372 }
1373 ret = regulator_enable(ts->avdd);
1374 if (ret) {
1375 dev_err(&ts->client->dev,
1376 "Regulator avdd enable failed ret=%d\n", ret);
1377 goto err_enable_avdd;
1378 }
1379 }
1380
1381 if (!IS_ERR(ts->vdd)) {
1382 ret = regulator_set_voltage(ts->vdd, GOODIX_VTG_MIN_UV,
1383 GOODIX_VTG_MAX_UV);
1384 if (ret) {
1385 dev_err(&ts->client->dev,
1386 "Regulator set_vtg failed vdd ret=%d\n", ret);
1387 goto err_set_vtg_vdd;
1388 }
1389 ret = reg_set_optimum_mode_check(ts->vdd,
1390 GOODIX_VDD_LOAD_MAX_UA);
1391 if (ret < 0) {
1392 dev_err(&ts->client->dev,
1393 "Regulator vdd set_opt failed rc=%d\n", ret);
1394 goto err_set_opt_vdd;
1395 }
1396 ret = regulator_enable(ts->vdd);
1397 if (ret) {
1398 dev_err(&ts->client->dev,
1399 "Regulator vdd enable failed ret=%d\n", ret);
1400 goto err_enable_vdd;
1401 }
1402 }
1403
1404 if (!IS_ERR(ts->vcc_i2c)) {
1405 ret = regulator_set_voltage(ts->vcc_i2c, GOODIX_I2C_VTG_MIN_UV,
1406 GOODIX_I2C_VTG_MAX_UV);
1407 if (ret) {
1408 dev_err(&ts->client->dev,
1409 "Regulator set_vtg failed vcc_i2c ret=%d\n",
1410 ret);
1411 goto err_set_vtg_vcc_i2c;
1412 }
1413 ret = reg_set_optimum_mode_check(ts->vcc_i2c,
1414 GOODIX_VIO_LOAD_MAX_UA);
1415 if (ret < 0) {
1416 dev_err(&ts->client->dev,
1417 "Regulator vcc_i2c set_opt failed rc=%d\n",
1418 ret);
1419 goto err_set_opt_vcc_i2c;
1420 }
1421 ret = regulator_enable(ts->vcc_i2c);
1422 if (ret) {
1423 dev_err(&ts->client->dev,
1424 "Regulator vcc_i2c enable failed ret=%d\n",
1425 ret);
1426 regulator_disable(ts->vdd);
1427 goto err_enable_vcc_i2c;
1428 }
1429 }
1430
1431 return 0;
1432
1433err_enable_vcc_i2c:
1434err_set_opt_vcc_i2c:
1435 if (!IS_ERR(ts->vcc_i2c))
1436 regulator_set_voltage(ts->vcc_i2c, 0, GOODIX_I2C_VTG_MAX_UV);
1437err_set_vtg_vcc_i2c:
1438 if (!IS_ERR(ts->vdd))
1439 regulator_disable(ts->vdd);
1440err_enable_vdd:
1441err_set_opt_vdd:
1442 if (!IS_ERR(ts->vdd))
1443 regulator_set_voltage(ts->vdd, 0, GOODIX_VTG_MAX_UV);
1444err_set_vtg_vdd:
1445 if (!IS_ERR(ts->avdd))
1446 regulator_disable(ts->avdd);
1447err_enable_avdd:
1448err_set_opt_avdd:
1449 return ret;
1450}
1451
1452/**
1453 * goodix_power_off - Turn device power OFF
1454 * @ts: driver private data
1455 *
1456 * Returns zero on success, else an error.
1457 */
1458static int goodix_power_off(struct goodix_ts_data *ts)
1459{
1460 int ret;
1461
1462 if (!IS_ERR(ts->vcc_i2c)) {
1463 ret = regulator_set_voltage(ts->vcc_i2c, 0,
1464 GOODIX_I2C_VTG_MAX_UV);
1465 if (ret < 0)
1466 dev_err(&ts->client->dev,
1467 "Regulator vcc_i2c set_vtg failed ret=%d\n",
1468 ret);
1469 ret = regulator_disable(ts->vcc_i2c);
1470 if (ret)
1471 dev_err(&ts->client->dev,
1472 "Regulator vcc_i2c disable failed ret=%d\n",
1473 ret);
1474 }
1475
1476 if (!IS_ERR(ts->vdd)) {
1477 ret = regulator_set_voltage(ts->vdd, 0, GOODIX_VTG_MAX_UV);
1478 if (ret < 0)
1479 dev_err(&ts->client->dev,
1480 "Regulator vdd set_vtg failed ret=%d\n", ret);
1481 ret = regulator_disable(ts->vdd);
1482 if (ret)
1483 dev_err(&ts->client->dev,
1484 "Regulator vdd disable failed ret=%d\n", ret);
1485 }
1486
1487 if (!IS_ERR(ts->avdd)) {
1488 ret = regulator_disable(ts->avdd);
1489 if (ret)
1490 dev_err(&ts->client->dev,
1491 "Regulator avdd disable failed ret=%d\n", ret);
1492 }
1493
1494 return 0;
1495}
1496
1497/**
1498 * goodix_power_init - Initialize device power
1499 * @ts: driver private data
1500 *
1501 * Returns zero on success, else an error.
1502 */
1503static int goodix_power_init(struct goodix_ts_data *ts)
1504{
1505 int ret;
1506
1507 ts->avdd = regulator_get(&ts->client->dev, "avdd");
1508 if (IS_ERR(ts->avdd)) {
1509 ret = PTR_ERR(ts->avdd);
1510 dev_info(&ts->client->dev,
1511 "Regulator get failed avdd ret=%d\n", ret);
1512 }
1513
1514 ts->vdd = regulator_get(&ts->client->dev, "vdd");
1515 if (IS_ERR(ts->vdd)) {
1516 ret = PTR_ERR(ts->vdd);
1517 dev_info(&ts->client->dev,
1518 "Regulator get failed vdd ret=%d\n", ret);
1519 }
1520
1521 ts->vcc_i2c = regulator_get(&ts->client->dev, "vcc-i2c");
1522 if (IS_ERR(ts->vcc_i2c)) {
1523 ret = PTR_ERR(ts->vcc_i2c);
1524 dev_info(&ts->client->dev,
1525 "Regulator get failed vcc_i2c ret=%d\n", ret);
1526 }
1527
1528 return 0;
1529}
1530
1531/**
1532 * goodix_power_deinit - Deinitialize device power
1533 * @ts: driver private data
1534 *
1535 * Returns zero on success, else an error.
1536 */
1537static int goodix_power_deinit(struct goodix_ts_data *ts)
1538{
1539 regulator_put(ts->vdd);
1540 regulator_put(ts->vcc_i2c);
1541 regulator_put(ts->avdd);
1542
1543 return 0;
1544}
1545
Bingzhe Caid0361172013-07-18 19:22:11 +08001546static int goodix_ts_get_dt_coords(struct device *dev, char *name,
1547 struct goodix_ts_platform_data *pdata)
1548{
1549 struct property *prop;
1550 struct device_node *np = dev->of_node;
1551 int rc;
1552 u32 coords[GOODIX_COORDS_ARR_SIZE];
1553
1554 prop = of_find_property(np, name, NULL);
1555 if (!prop)
1556 return -EINVAL;
1557 if (!prop->value)
1558 return -ENODATA;
1559
1560 rc = of_property_read_u32_array(np, name, coords,
1561 GOODIX_COORDS_ARR_SIZE);
1562 if (rc && (rc != -EINVAL)) {
1563 dev_err(dev, "Unable to read %s\n", name);
1564 return rc;
1565 }
1566
1567 if (!strcmp(name, "goodix,panel-coords")) {
1568 pdata->panel_minx = coords[0];
1569 pdata->panel_miny = coords[1];
1570 pdata->panel_maxx = coords[2];
1571 pdata->panel_maxy = coords[3];
1572 } else if (!strcmp(name, "goodix,display-coords")) {
1573 pdata->x_min = coords[0];
1574 pdata->y_min = coords[1];
1575 pdata->x_max = coords[2];
1576 pdata->y_max = coords[3];
1577 } else {
1578 dev_err(dev, "unsupported property %s\n", name);
1579 return -EINVAL;
1580 }
1581
1582 return 0;
1583}
1584
1585static int goodix_parse_dt(struct device *dev,
1586 struct goodix_ts_platform_data *pdata)
1587{
1588 int rc;
1589 struct device_node *np = dev->of_node;
1590 struct property *prop;
1591 u32 temp_val, num_buttons;
1592 u32 button_map[MAX_BUTTONS];
1593
1594 rc = goodix_ts_get_dt_coords(dev, "goodix,panel-coords", pdata);
1595 if (rc && (rc != -EINVAL))
1596 return rc;
1597
1598 rc = goodix_ts_get_dt_coords(dev, "goodix,display-coords", pdata);
1599 if (rc)
1600 return rc;
1601
1602 pdata->i2c_pull_up = of_property_read_bool(np,
1603 "goodix,i2c-pull-up");
1604
1605 pdata->no_force_update = of_property_read_bool(np,
1606 "goodix,no-force-update");
1607 /* reset, irq gpio info */
1608 pdata->reset_gpio = of_get_named_gpio_flags(np, "reset-gpios",
1609 0, &pdata->reset_gpio_flags);
1610 if (pdata->reset_gpio < 0)
1611 return pdata->reset_gpio;
1612
1613 pdata->irq_gpio = of_get_named_gpio_flags(np, "interrupt-gpios",
1614 0, &pdata->irq_gpio_flags);
1615 if (pdata->irq_gpio < 0)
1616 return pdata->irq_gpio;
1617
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301618 rc = of_property_read_string(np, "goodix,product-id",
1619 &pdata->product_id);
1620 if (rc < 0 || strlen(pdata->product_id) > GTP_PRODUCT_ID_MAXSIZE)
Bingzhe Caid0361172013-07-18 19:22:11 +08001621 return rc;
1622
1623 prop = of_find_property(np, "goodix,button-map", NULL);
1624 if (prop) {
1625 num_buttons = prop->length / sizeof(temp_val);
1626 if (num_buttons > MAX_BUTTONS)
1627 return -EINVAL;
1628
1629 rc = of_property_read_u32_array(np,
1630 "goodix,button-map", button_map,
1631 num_buttons);
1632 if (rc) {
1633 dev_err(dev, "Unable to read key codes\n");
1634 return rc;
1635 }
1636 }
1637
1638 prop = of_find_property(np, "goodix,cfg-data", &pdata->gtp_cfg_len);
1639 if (prop && prop->value) {
1640 pdata->config_data = devm_kzalloc(dev,
1641 GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH, GFP_KERNEL);
1642 if (!pdata->config_data) {
1643 dev_err(dev, "Not enough memory for panel config data\n");
1644 return -ENOMEM;
1645 }
1646
1647 pdata->config_data[0] = GTP_REG_CONFIG_DATA >> 8;
1648 pdata->config_data[1] = GTP_REG_CONFIG_DATA & 0xff;
1649 memset(&pdata->config_data[GTP_ADDR_LENGTH], 0,
1650 GTP_CONFIG_MAX_LENGTH);
1651 memcpy(&pdata->config_data[GTP_ADDR_LENGTH],
1652 prop->value, pdata->gtp_cfg_len);
1653 } else {
1654 dev_err(dev,
1655 "Unable to get configure data, default will be used.\n");
1656 pdata->gtp_cfg_len = 0;
1657 }
1658
1659 return 0;
1660}
Bingzhe Caic68e6112013-07-22 00:12:48 +08001661
Andrew Huangab45aab2013-06-27 10:06:56 +00001662/*******************************************************
1663Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001664 I2c probe.
Andrew Huangab45aab2013-06-27 10:06:56 +00001665Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001666 client: i2c device struct.
1667 id: device id.
Andrew Huangab45aab2013-06-27 10:06:56 +00001668Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001669 Executive outcomes.
1670 0: succeed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001671*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001672
1673static int goodix_ts_probe(struct i2c_client *client,
1674 const struct i2c_device_id *id)
Andrew Huangab45aab2013-06-27 10:06:56 +00001675{
Bingzhe Caid0361172013-07-18 19:22:11 +08001676 struct goodix_ts_platform_data *pdata;
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001677 struct goodix_ts_data *ts;
1678 u16 version_info;
1679 int ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001680
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001681 dev_dbg(&client->dev, "GTP I2C Address: 0x%02x\n", client->addr);
Bingzhe Caid0361172013-07-18 19:22:11 +08001682 if (client->dev.of_node) {
1683 pdata = devm_kzalloc(&client->dev,
1684 sizeof(struct goodix_ts_platform_data), GFP_KERNEL);
1685 if (!pdata) {
1686 dev_err(&client->dev,
1687 "GTP Failed to allocate memory for pdata\n");
1688 return -ENOMEM;
1689 }
1690
1691 ret = goodix_parse_dt(&client->dev, pdata);
1692 if (ret)
1693 return ret;
1694 } else {
1695 pdata = client->dev.platform_data;
1696 }
1697
1698 if (!pdata) {
1699 dev_err(&client->dev, "GTP invalid pdata\n");
1700 return -EINVAL;
1701 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001702
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001703#if GTP_ESD_PROTECT
1704 i2c_connect_client = client;
1705#endif
Bingzhe Caid0361172013-07-18 19:22:11 +08001706
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001707 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1708 dev_err(&client->dev, "GTP I2C not supported\n");
1709 return -ENODEV;
1710 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001711
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001712 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1713 if (!ts) {
1714 dev_err(&client->dev, "GTP not enough memory for ts\n");
1715 return -ENOMEM;
1716 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001717
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001718 memset(ts, 0, sizeof(*ts));
1719 ts->client = client;
Bingzhe Caid0361172013-07-18 19:22:11 +08001720 ts->pdata = pdata;
1721 /* For 2.6.39 & later use spin_lock_init(&ts->irq_lock)
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001722 * For 2.6.39 & before, use ts->irq_lock = SPIN_LOCK_UNLOCKED
1723 */
1724 spin_lock_init(&ts->irq_lock);
1725 i2c_set_clientdata(client, ts);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001726 ts->gtp_rawdiff_mode = 0;
1727
Bingzhe Caic68e6112013-07-22 00:12:48 +08001728 ret = goodix_power_init(ts);
1729 if (ret) {
1730 dev_err(&client->dev, "GTP power init failed\n");
1731 goto exit_free_client_data;
1732 }
1733
1734 ret = goodix_power_on(ts);
1735 if (ret) {
1736 dev_err(&client->dev, "GTP power on failed\n");
1737 goto exit_deinit_power;
1738 }
1739
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001740 ret = gtp_request_io_port(ts);
1741 if (ret) {
1742 dev_err(&client->dev, "GTP request IO port failed.\n");
1743 goto exit_power_off;
1744 }
1745
1746 gtp_reset_guitar(ts, 20);
1747
1748 ret = gtp_i2c_test(client);
1749 if (ret != 2) {
1750 dev_err(&client->dev, "I2C communication ERROR!\n");
1751 goto exit_free_io_port;
1752 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001753
1754#if GTP_AUTO_UPDATE
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001755 ret = gup_init_update_proc(ts);
1756 if (ret < 0) {
1757 dev_err(&client->dev,
1758 "GTP Create firmware update thread error.\n");
1759 goto exit_free_io_port;
1760 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001761#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001762
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001763 ret = gtp_init_panel(ts);
1764 if (ret < 0) {
1765 dev_err(&client->dev, "GTP init panel failed.\n");
1766 ts->abs_x_max = GTP_MAX_WIDTH;
1767 ts->abs_y_max = GTP_MAX_HEIGHT;
1768 ts->int_trigger_type = GTP_INT_TRIGGER;
1769 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001770
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001771 ret = gtp_request_input_dev(ts);
1772 if (ret) {
1773 dev_err(&client->dev, "GTP request input dev failed.\n");
1774 goto exit_free_inputdev;
1775 }
1776
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001777#if defined(CONFIG_FB)
1778 ts->fb_notif.notifier_call = fb_notifier_callback;
1779 ret = fb_register_client(&ts->fb_notif);
1780 if (ret)
1781 dev_err(&ts->client->dev,
1782 "Unable to register fb_notifier: %d\n",
1783 ret);
1784#elif defined(CONFIG_HAS_EARLYSUSPEND)
1785 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1786 ts->early_suspend.suspend = goodix_ts_early_suspend;
1787 ts->early_suspend.resume = goodix_ts_late_resume;
1788 register_early_suspend(&ts->early_suspend);
1789#endif
1790
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001791 ts->goodix_wq = create_singlethread_workqueue("goodix_wq");
1792 INIT_WORK(&ts->work, goodix_ts_work_func);
1793
1794 ret = gtp_request_irq(ts);
1795 if (ret < 0)
1796 dev_info(&client->dev, "GTP works in polling mode.\n");
1797 else
1798 dev_info(&client->dev, "GTP works in interrupt mode.\n");
1799
Shantanu Jain3f80bef2013-09-18 12:05:02 +05301800 ret = gtp_read_fw_version(client, &version_info);
1801 if (ret != 0)
1802 dev_err(&client->dev, "GTP firmware version read failed.\n");
1803
1804 ret = gtp_check_product_id(client);
1805 if (ret != 0) {
1806 dev_err(&client->dev, "GTP Product id doesn't match.\n");
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001807 goto exit_free_irq;
1808 }
1809 if (ts->use_irq)
1810 gtp_irq_enable(ts);
1811
Andrew Huangab45aab2013-06-27 10:06:56 +00001812#if GTP_CREATE_WR_NODE
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001813 init_wr_node(client);
Andrew Huangab45aab2013-06-27 10:06:56 +00001814#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001815
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001816#if GTP_ESD_PROTECT
1817 gtp_esd_switch(client, SWITCH_ON);
1818#endif
1819 init_done = true;
1820 return 0;
1821exit_free_irq:
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001822#if defined(CONFIG_FB)
1823 if (fb_unregister_client(&ts->fb_notif))
1824 dev_err(&client->dev,
1825 "Error occurred while unregistering fb_notifier.\n");
1826#elif defined(CONFIG_HAS_EARLYSUSPEND)
1827 unregister_early_suspend(&ts->early_suspend);
1828#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001829 if (ts->use_irq)
1830 free_irq(client->irq, ts);
1831 else
1832 hrtimer_cancel(&ts->timer);
1833 cancel_work_sync(&ts->work);
1834 flush_workqueue(ts->goodix_wq);
1835 destroy_workqueue(ts->goodix_wq);
1836
1837 input_unregister_device(ts->input_dev);
1838 if (ts->input_dev) {
1839 input_free_device(ts->input_dev);
1840 ts->input_dev = NULL;
1841 }
1842exit_free_inputdev:
1843 kfree(ts->config_data);
1844exit_free_io_port:
Bingzhe Caid0361172013-07-18 19:22:11 +08001845 if (gpio_is_valid(pdata->reset_gpio))
1846 gpio_free(pdata->reset_gpio);
1847 if (gpio_is_valid(pdata->irq_gpio))
1848 gpio_free(pdata->irq_gpio);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001849exit_power_off:
Bingzhe Caic68e6112013-07-22 00:12:48 +08001850 goodix_power_off(ts);
1851exit_deinit_power:
1852 goodix_power_deinit(ts);
1853exit_free_client_data:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001854 i2c_set_clientdata(client, NULL);
1855 kfree(ts);
1856 return ret;
1857}
Andrew Huangab45aab2013-06-27 10:06:56 +00001858
1859/*******************************************************
1860Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001861 Goodix touchscreen driver release function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001862Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001863 client: i2c device struct.
Andrew Huangab45aab2013-06-27 10:06:56 +00001864Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001865 Executive outcomes. 0---succeed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001866*******************************************************/
1867static int goodix_ts_remove(struct i2c_client *client)
1868{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001869 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1870
1871 GTP_DEBUG_FUNC();
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001872#if defined(CONFIG_FB)
1873 if (fb_unregister_client(&ts->fb_notif))
1874 dev_err(&client->dev,
1875 "Error occurred while unregistering fb_notifier.\n");
1876#elif defined(CONFIG_HAS_EARLYSUSPEND)
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001877 unregister_early_suspend(&ts->early_suspend);
Andrew Huangab45aab2013-06-27 10:06:56 +00001878#endif
1879
1880#if GTP_CREATE_WR_NODE
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001881 uninit_wr_node();
Andrew Huangab45aab2013-06-27 10:06:56 +00001882#endif
1883
1884#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001885 cancel_work_sync(gtp_esd_check_workqueue);
1886 flush_workqueue(gtp_esd_check_workqueue);
1887 destroy_workqueue(gtp_esd_check_workqueue);
Andrew Huangab45aab2013-06-27 10:06:56 +00001888#endif
1889
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001890 if (ts) {
1891 if (ts->use_irq)
1892 free_irq(client->irq, ts);
1893 else
1894 hrtimer_cancel(&ts->timer);
Andrew Huangab45aab2013-06-27 10:06:56 +00001895
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001896 cancel_work_sync(&ts->work);
1897 flush_workqueue(ts->goodix_wq);
1898 destroy_workqueue(ts->goodix_wq);
1899
1900 input_unregister_device(ts->input_dev);
1901 if (ts->input_dev) {
1902 input_free_device(ts->input_dev);
1903 ts->input_dev = NULL;
1904 }
1905 kfree(ts->config_data);
1906
1907 if (gpio_is_valid(ts->pdata->reset_gpio))
1908 gpio_free(ts->pdata->reset_gpio);
1909 if (gpio_is_valid(ts->pdata->irq_gpio))
1910 gpio_free(ts->pdata->irq_gpio);
1911
Bingzhe Caic68e6112013-07-22 00:12:48 +08001912 goodix_power_off(ts);
1913 goodix_power_deinit(ts);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001914 i2c_set_clientdata(client, NULL);
1915 kfree(ts);
1916 }
1917
1918 return 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00001919}
1920
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001921#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_FB)
Andrew Huangab45aab2013-06-27 10:06:56 +00001922/*******************************************************
1923Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001924 Early suspend function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001925Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001926 h: early_suspend struct.
Andrew Huangab45aab2013-06-27 10:06:56 +00001927Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001928 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00001929*******************************************************/
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001930static void goodix_ts_suspend(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001931{
Shantanu Jain33b1aee2013-08-05 16:52:47 +05301932 int ret = -1, i;
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001933
1934 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00001935
1936#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001937 ts->gtp_is_suspend = 1;
1938 gtp_esd_switch(ts->client, SWITCH_OFF);
Andrew Huangab45aab2013-06-27 10:06:56 +00001939#endif
1940
1941#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001942 ret = gtp_enter_doze(ts);
Andrew Huangab45aab2013-06-27 10:06:56 +00001943#else
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001944 if (ts->use_irq)
1945 gtp_irq_disable(ts);
1946 else
1947 hrtimer_cancel(&ts->timer);
Shantanu Jain33b1aee2013-08-05 16:52:47 +05301948
1949 for (i = 0; i < GTP_MAX_TOUCH; i++)
1950 gtp_touch_up(ts, i);
1951
Shantanu Jain33b1aee2013-08-05 16:52:47 +05301952 input_sync(ts->input_dev);
1953
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001954 ret = gtp_enter_sleep(ts);
1955#endif
1956 if (ret < 0)
1957 dev_err(&ts->client->dev, "GTP early suspend failed.\n");
1958 /* to avoid waking up while not sleeping,
1959 * delay 48 + 10ms to ensure reliability
1960 */
1961 msleep(58);
Andrew Huangab45aab2013-06-27 10:06:56 +00001962}
1963
1964/*******************************************************
1965Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001966 Late resume function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001967Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001968 h: early_suspend struct.
Andrew Huangab45aab2013-06-27 10:06:56 +00001969Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001970 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00001971*******************************************************/
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001972static void goodix_ts_resume(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001973{
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001974 int ret = -1;
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001975
1976 GTP_DEBUG_FUNC();
1977
1978 ret = gtp_wakeup_sleep(ts);
Andrew Huangab45aab2013-06-27 10:06:56 +00001979
1980#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001981 doze_status = DOZE_DISABLED;
Andrew Huangab45aab2013-06-27 10:06:56 +00001982#endif
1983
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001984 if (ret < 0)
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001985 dev_err(&ts->client->dev, "GTP resume failed.\n");
Andrew Huangab45aab2013-06-27 10:06:56 +00001986
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001987 if (ts->use_irq)
1988 gtp_irq_enable(ts);
1989 else
1990 hrtimer_start(&ts->timer,
1991 ktime_set(1, 0), HRTIMER_MODE_REL);
Andrew Huangab45aab2013-06-27 10:06:56 +00001992
1993#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001994 ts->gtp_is_suspend = 0;
1995 gtp_esd_switch(ts->client, SWITCH_ON);
Andrew Huangab45aab2013-06-27 10:06:56 +00001996#endif
1997}
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001998
1999#if defined(CONFIG_FB)
2000static int fb_notifier_callback(struct notifier_block *self,
2001 unsigned long event, void *data)
2002{
2003 struct fb_event *evdata = data;
2004 int *blank;
2005 struct goodix_ts_data *ts =
2006 container_of(self, struct goodix_ts_data, fb_notif);
2007
2008 if (evdata && evdata->data && event == FB_EVENT_BLANK &&
2009 ts && ts->client) {
2010 blank = evdata->data;
2011 if (*blank == FB_BLANK_UNBLANK)
2012 goodix_ts_resume(ts);
2013 else if (*blank == FB_BLANK_POWERDOWN)
2014 goodix_ts_suspend(ts);
2015 }
2016
2017 return 0;
2018}
2019#elif defined(CONFIG_HAS_EARLYSUSPEND)
2020/*******************************************************
2021Function:
2022 Early suspend function.
2023Input:
2024 h: early_suspend struct.
2025Output:
2026 None.
2027*******************************************************/
2028static void goodix_ts_early_suspend(struct early_suspend *h)
2029{
2030 struct goodix_ts_data *ts;
2031
2032 ts = container_of(h, struct goodix_ts_data, early_suspend);
2033 goodix_ts_suspend(ts);
2034 return;
2035}
2036
2037/*******************************************************
2038Function:
2039 Late resume function.
2040Input:
2041 h: early_suspend struct.
2042Output:
2043 None.
2044*******************************************************/
2045static void goodix_ts_late_resume(struct early_suspend *h)
2046{
2047 struct goodix_ts_data *ts;
2048
2049 ts = container_of(h, struct goodix_ts_data, early_suspend);
2050 goodix_ts_late_resume(ts);
2051 return;
2052}
Andrew Huangab45aab2013-06-27 10:06:56 +00002053#endif
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08002054#endif /* !CONFIG_HAS_EARLYSUSPEND && !CONFIG_FB*/
Andrew Huangab45aab2013-06-27 10:06:56 +00002055
2056#if GTP_ESD_PROTECT
2057/*******************************************************
2058Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002059 switch on & off esd delayed work
Andrew Huangab45aab2013-06-27 10:06:56 +00002060Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002061 client: i2c device
2062 on: SWITCH_ON / SWITCH_OFF
Andrew Huangab45aab2013-06-27 10:06:56 +00002063Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002064 void
Andrew Huangab45aab2013-06-27 10:06:56 +00002065*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002066void gtp_esd_switch(struct i2c_client *client, int on)
Andrew Huangab45aab2013-06-27 10:06:56 +00002067{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002068 struct goodix_ts_data *ts;
2069
2070 ts = i2c_get_clientdata(client);
2071 if (SWITCH_ON == on) {
2072 /* switch on esd */
2073 if (!ts->esd_running) {
2074 ts->esd_running = 1;
2075 dev_dbg(&client->dev, "Esd started\n");
2076 queue_delayed_work(gtp_esd_check_workqueue,
2077 &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE);
2078 }
2079 } else {
2080 /* switch off esd */
2081 if (ts->esd_running) {
2082 ts->esd_running = 0;
2083 dev_dbg(&client->dev, "Esd cancelled\n");
2084 cancel_delayed_work_sync(&gtp_esd_check_work);
2085 }
2086 }
Andrew Huangab45aab2013-06-27 10:06:56 +00002087}
2088
2089/*******************************************************
2090Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002091 Initialize external watchdog for esd protect
Andrew Huangab45aab2013-06-27 10:06:56 +00002092Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002093 client: i2c device.
Andrew Huangab45aab2013-06-27 10:06:56 +00002094Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002095 result of i2c write operation.
2096 1: succeed, otherwise: failed
Andrew Huangab45aab2013-06-27 10:06:56 +00002097*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002098static int gtp_init_ext_watchdog(struct i2c_client *client)
Andrew Huangab45aab2013-06-27 10:06:56 +00002099{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002100 /* in case of recursively reset by calling gtp_i2c_write*/
2101 struct i2c_msg msg;
2102 u8 opr_buffer[4] = {0x80, 0x40, 0xAA, 0xAA};
2103 int ret;
2104 int retries = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00002105
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002106 GTP_DEBUG("Init external watchdog...");
2107 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00002108
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002109 msg.flags = !I2C_M_RD;
2110 msg.addr = client->addr;
2111 msg.len = 4;
2112 msg.buf = opr_buffer;
Andrew Huangab45aab2013-06-27 10:06:56 +00002113
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002114 while (retries < 5) {
2115 ret = i2c_transfer(client->adapter, &msg, 1);
2116 if (ret == 1)
2117 return 1;
2118 retries++;
2119 }
2120 if (retries >= 5)
2121 dev_err(&client->dev, "init external watchdog failed!");
2122 return 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00002123}
2124
2125/*******************************************************
2126Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002127 Esd protect function.
2128 Added external watchdog by meta, 2013/03/07
Andrew Huangab45aab2013-06-27 10:06:56 +00002129Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002130 work: delayed work
Andrew Huangab45aab2013-06-27 10:06:56 +00002131Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002132 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00002133*******************************************************/
2134static void gtp_esd_check_func(struct work_struct *work)
2135{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002136 s32 i;
2137 s32 ret = -1;
2138 struct goodix_ts_data *ts = NULL;
2139 u8 test[4] = {0x80, 0x40};
Andrew Huangab45aab2013-06-27 10:06:56 +00002140
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002141 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00002142
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002143 ts = i2c_get_clientdata(i2c_connect_client);
2144
2145 if (ts->gtp_is_suspend) {
2146 dev_dbg(&ts->client->dev, "Esd terminated!\n");
2147 ts->esd_running = 0;
2148 return;
2149 }
2150#ifdef CONFIG_GT9XX_TOUCHPANEL_UPDATE
2151 if (ts->enter_update)
2152 return;
2153#endif
2154
2155 for (i = 0; i < 3; i++) {
2156 ret = gtp_i2c_read(ts->client, test, 4);
2157
2158 GTP_DEBUG("0x8040 = 0x%02X, 0x8041 = 0x%02X", test[2], test[3]);
2159 if ((ret < 0)) {
2160 /* IC works abnormally..*/
2161 continue;
2162 } else {
2163 if ((test[2] == 0xAA) || (test[3] != 0xAA)) {
2164 /* IC works abnormally..*/
2165 i = 3;
2166 break;
2167 } else {
2168 /* IC works normally, Write 0x8040 0xAA*/
2169 test[2] = 0xAA;
2170 gtp_i2c_write(ts->client, test, 3);
2171 break;
2172 }
2173 }
2174 }
2175 if (i >= 3) {
2176 dev_err(&ts->client->dev,
2177 "IC Working ABNORMALLY, Resetting Guitar...\n");
2178 gtp_reset_guitar(ts, 50);
2179 }
2180
2181 if (!ts->gtp_is_suspend)
2182 queue_delayed_work(gtp_esd_check_workqueue,
2183 &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE);
2184 else {
2185 dev_dbg(&ts->client->dev, "Esd terminated!\n");
2186 ts->esd_running = 0;
2187 }
2188
2189 return;
Andrew Huangab45aab2013-06-27 10:06:56 +00002190}
2191#endif
2192
2193static const struct i2c_device_id goodix_ts_id[] = {
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002194 { GTP_I2C_NAME, 0 },
2195 { }
Andrew Huangab45aab2013-06-27 10:06:56 +00002196};
2197
Bingzhe Caid0361172013-07-18 19:22:11 +08002198static struct of_device_id goodix_match_table[] = {
2199 { .compatible = "goodix,gt9xx", },
2200 { },
2201};
2202
Andrew Huangab45aab2013-06-27 10:06:56 +00002203static struct i2c_driver goodix_ts_driver = {
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002204 .probe = goodix_ts_probe,
2205 .remove = goodix_ts_remove,
2206#ifdef CONFIG_HAS_EARLYSUSPEND
2207 .suspend = goodix_ts_early_suspend,
2208 .resume = goodix_ts_late_resume,
Andrew Huangab45aab2013-06-27 10:06:56 +00002209#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002210 .id_table = goodix_ts_id,
2211 .driver = {
2212 .name = GTP_I2C_NAME,
2213 .owner = THIS_MODULE,
Bingzhe Caid0361172013-07-18 19:22:11 +08002214 .of_match_table = goodix_match_table,
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002215 },
Andrew Huangab45aab2013-06-27 10:06:56 +00002216};
2217
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002218/*******************************************************
Andrew Huangab45aab2013-06-27 10:06:56 +00002219Function:
2220 Driver Install function.
2221Input:
2222 None.
2223Output:
2224 Executive Outcomes. 0---succeed.
2225********************************************************/
2226static int __devinit goodix_ts_init(void)
2227{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002228 int ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00002229
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002230 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00002231#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002232 INIT_DELAYED_WORK(&gtp_esd_check_work, gtp_esd_check_func);
2233 gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
Andrew Huangab45aab2013-06-27 10:06:56 +00002234#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002235 ret = i2c_add_driver(&goodix_ts_driver);
2236 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00002237}
2238
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002239/*******************************************************
Andrew Huangab45aab2013-06-27 10:06:56 +00002240Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002241 Driver uninstall function.
Andrew Huangab45aab2013-06-27 10:06:56 +00002242Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002243 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00002244Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002245 Executive Outcomes. 0---succeed.
Andrew Huangab45aab2013-06-27 10:06:56 +00002246********************************************************/
2247static void __exit goodix_ts_exit(void)
2248{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002249 GTP_DEBUG_FUNC();
2250 i2c_del_driver(&goodix_ts_driver);
Andrew Huangab45aab2013-06-27 10:06:56 +00002251}
2252
2253late_initcall(goodix_ts_init);
2254module_exit(goodix_ts_exit);
2255
2256MODULE_DESCRIPTION("GTP Series Driver");
2257MODULE_LICENSE("GPL");