blob: 33ef1411a0a091e9288cc1dd8db98302d93730fd [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:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001060 Read chip 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.
1066 2: succeed, otherwise: failed
Andrew Huangab45aab2013-06-27 10:06:56 +00001067*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001068int gtp_read_version(struct i2c_client *client, u16 *version)
Andrew Huangab45aab2013-06-27 10:06:56 +00001069{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001070 int ret = -EIO;
1071 u8 buf[8] = { GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff };
Andrew Huangab45aab2013-06-27 10:06:56 +00001072
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001073 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00001074
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001075 ret = gtp_i2c_read(client, buf, sizeof(buf));
1076 if (ret < 0) {
1077 dev_err(&client->dev, "GTP read version failed.\n");
1078 return ret;
1079 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001080
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001081 if (version)
1082 *version = (buf[7] << 8) | buf[6];
1083
1084 if (buf[5] == 0x00) {
1085 dev_dbg(&client->dev, "IC Version: %c%c%c_%02x%02x\n", buf[2],
1086 buf[3], buf[4], buf[7], buf[6]);
1087 } else {
1088 if (buf[5] == 'S' || buf[5] == 's')
1089 chip_gt9xxs = 1;
1090 dev_dbg(&client->dev, "IC Version: %c%c%c%c_%02x%02x\n", buf[2],
1091 buf[3], buf[4], buf[5], buf[7], buf[6]);
1092 }
1093 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001094}
1095
1096/*******************************************************
1097Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001098 I2c test Function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001099Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001100 client:i2c client.
Andrew Huangab45aab2013-06-27 10:06:56 +00001101Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001102 Executive outcomes.
1103 2: succeed, otherwise failed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001104*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001105static int gtp_i2c_test(struct i2c_client *client)
Andrew Huangab45aab2013-06-27 10:06:56 +00001106{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001107 u8 buf[3] = { GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff };
1108 int retry = 5;
1109 int ret = -EIO;
1110
1111 GTP_DEBUG_FUNC();
1112
1113 while (retry--) {
1114 ret = gtp_i2c_read(client, buf, 3);
1115 if (ret > 0)
1116 return ret;
1117 dev_err(&client->dev, "GTP i2c test failed time %d.\n", retry);
1118 msleep(20);
1119 }
1120 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001121}
1122
1123/*******************************************************
1124Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001125 Request gpio(INT & RST) ports.
Andrew Huangab45aab2013-06-27 10:06:56 +00001126Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001127 ts: private data.
Andrew Huangab45aab2013-06-27 10:06:56 +00001128Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001129 Executive outcomes.
1130 = 0: succeed, != 0: failed
Andrew Huangab45aab2013-06-27 10:06:56 +00001131*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001132static int gtp_request_io_port(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001133{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001134 struct i2c_client *client = ts->client;
1135 struct goodix_ts_platform_data *pdata = ts->pdata;
1136 int ret;
1137 if (gpio_is_valid(pdata->irq_gpio)) {
1138 ret = gpio_request(pdata->irq_gpio, "goodix_ts_irq_gpio");
1139 if (ret) {
1140 dev_err(&client->dev, "irq gpio request failed\n");
1141 goto pwr_off;
1142 }
1143 ret = gpio_direction_input(pdata->irq_gpio);
1144 if (ret) {
1145 dev_err(&client->dev,
1146 "set_direction for irq gpio failed\n");
1147 goto free_irq_gpio;
1148 }
1149 } else {
1150 dev_err(&client->dev, "irq gpio is invalid!\n");
1151 ret = -EINVAL;
1152 goto free_irq_gpio;
1153 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001154
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001155 if (gpio_is_valid(pdata->reset_gpio)) {
1156 ret = gpio_request(pdata->reset_gpio, "goodix_ts__reset_gpio");
1157 if (ret) {
1158 dev_err(&client->dev, "reset gpio request failed\n");
1159 goto free_irq_gpio;
1160 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001161
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001162 ret = gpio_direction_output(pdata->reset_gpio, 0);
1163 if (ret) {
1164 dev_err(&client->dev,
1165 "set_direction for reset gpio failed\n");
1166 goto free_reset_gpio;
1167 }
1168 } else {
1169 dev_err(&client->dev, "reset gpio is invalid!\n");
1170 ret = -EINVAL;
1171 goto free_reset_gpio;
1172 }
1173 gpio_direction_input(pdata->reset_gpio);
Andrew Huangab45aab2013-06-27 10:06:56 +00001174
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001175 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001176
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001177free_reset_gpio:
1178 if (gpio_is_valid(pdata->reset_gpio))
1179 gpio_free(pdata->reset_gpio);
1180free_irq_gpio:
1181 if (gpio_is_valid(pdata->irq_gpio))
1182 gpio_free(pdata->irq_gpio);
1183pwr_off:
1184 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001185}
1186
1187/*******************************************************
1188Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001189 Request interrupt.
Andrew Huangab45aab2013-06-27 10:06:56 +00001190Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001191 ts: private data.
Andrew Huangab45aab2013-06-27 10:06:56 +00001192Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001193 Executive outcomes.
1194 0: succeed, -1: failed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001195*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001196static int gtp_request_irq(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001197{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001198 int ret;
1199 const u8 irq_table[] = GTP_IRQ_TAB;
Andrew Huangab45aab2013-06-27 10:06:56 +00001200
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001201 GTP_DEBUG("INT trigger type:%x, irq=%d", ts->int_trigger_type,
1202 ts->client->irq);
Andrew Huangab45aab2013-06-27 10:06:56 +00001203
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001204 ret = request_irq(ts->client->irq, goodix_ts_irq_handler,
1205 irq_table[ts->int_trigger_type],
1206 ts->client->name, ts);
1207 if (ret) {
1208 dev_err(&ts->client->dev, "Request IRQ failed!ERRNO:%d.\n",
1209 ret);
1210 gpio_direction_input(ts->pdata->irq_gpio);
Andrew Huangab45aab2013-06-27 10:06:56 +00001211
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001212 hrtimer_init(&ts->timer, CLOCK_MONOTONIC,
1213 HRTIMER_MODE_REL);
1214 ts->timer.function = goodix_ts_timer_handler;
1215 hrtimer_start(&ts->timer, ktime_set(1, 0),
1216 HRTIMER_MODE_REL);
1217 ts->use_irq = false;
1218 return ret;
1219 } else {
1220 gtp_irq_disable(ts);
1221 ts->use_irq = true;
1222 return 0;
1223 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001224}
1225
1226/*******************************************************
1227Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001228 Request input device Function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001229Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001230 ts:private data.
Andrew Huangab45aab2013-06-27 10:06:56 +00001231Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001232 Executive outcomes.
1233 0: succeed, otherwise: failed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001234*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001235static int gtp_request_input_dev(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001236{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001237 int ret;
1238 char phys[PHY_BUF_SIZE];
Andrew Huangab45aab2013-06-27 10:06:56 +00001239#if GTP_HAVE_TOUCH_KEY
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001240 int index = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00001241#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001242
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001243 GTP_DEBUG_FUNC();
1244
1245 ts->input_dev = input_allocate_device();
1246 if (ts->input_dev == NULL) {
1247 dev_err(&ts->client->dev,
1248 "Failed to allocate input device.\n");
1249 return -ENOMEM;
1250 }
1251
1252 ts->input_dev->evbit[0] =
1253 BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
Bingzhe Cai6beb0112013-08-27 00:27:26 +08001254 set_bit(BTN_TOOL_FINGER, ts->input_dev->keybit);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001255 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
1256 input_mt_init_slots(ts->input_dev, 10);/* in case of "out of memory" */
Andrew Huangab45aab2013-06-27 10:06:56 +00001257
1258#if GTP_HAVE_TOUCH_KEY
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001259 for (index = 0; index < GTP_MAX_KEY_NUM; index++) {
1260 input_set_capability(ts->input_dev,
1261 EV_KEY, touch_key_array[index]);
1262 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001263#endif
1264
1265#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001266 input_set_capability(ts->input_dev, EV_KEY, KEY_POWER);
1267#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001268
1269#if GTP_WITH_PEN
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001270 /* pen support */
1271 __set_bit(BTN_TOOL_PEN, ts->input_dev->keybit);
1272 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
1273 __set_bit(INPUT_PROP_POINTER, ts->input_dev->propbit);
Andrew Huangab45aab2013-06-27 10:06:56 +00001274#endif
1275
1276#if GTP_CHANGE_X2Y
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001277 GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
Andrew Huangab45aab2013-06-27 10:06:56 +00001278#endif
1279
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001280 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
1281 0, ts->abs_x_max, 0, 0);
1282 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
1283 0, ts->abs_y_max, 0, 0);
1284 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR,
1285 0, 255, 0, 0);
1286 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR,
1287 0, 255, 0, 0);
1288 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID,
1289 0, 255, 0, 0);
Andrew Huangab45aab2013-06-27 10:06:56 +00001290
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001291 snprintf(phys, PHY_BUF_SIZE, "input/ts");
1292 ts->input_dev->name = GOODIX_DEV_NAME;
1293 ts->input_dev->phys = phys;
1294 ts->input_dev->id.bustype = BUS_I2C;
1295 ts->input_dev->id.vendor = 0xDEAD;
1296 ts->input_dev->id.product = 0xBEEF;
1297 ts->input_dev->id.version = 10427;
Andrew Huangab45aab2013-06-27 10:06:56 +00001298
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001299 ret = input_register_device(ts->input_dev);
1300 if (ret) {
1301 dev_err(&ts->client->dev,
1302 "Register %s input device failed.\n",
1303 ts->input_dev->name);
1304 goto exit_free_inputdev;
1305 }
1306
1307 return 0;
1308
1309exit_free_inputdev:
1310 input_free_device(ts->input_dev);
1311 ts->input_dev = NULL;
1312 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001313}
1314
Bingzhe Caic68e6112013-07-22 00:12:48 +08001315static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1316{
1317 return (regulator_count_voltages(reg) > 0) ?
1318 regulator_set_optimum_mode(reg, load_uA) : 0;
1319}
1320
1321/**
1322 * goodix_power_on - Turn device power ON
1323 * @ts: driver private data
1324 *
1325 * Returns zero on success, else an error.
1326 */
1327static int goodix_power_on(struct goodix_ts_data *ts)
1328{
1329 int ret;
1330
1331 if (!IS_ERR(ts->avdd)) {
1332 ret = reg_set_optimum_mode_check(ts->avdd,
1333 GOODIX_VDD_LOAD_MAX_UA);
1334 if (ret < 0) {
1335 dev_err(&ts->client->dev,
1336 "Regulator avdd set_opt failed rc=%d\n", ret);
1337 goto err_set_opt_avdd;
1338 }
1339 ret = regulator_enable(ts->avdd);
1340 if (ret) {
1341 dev_err(&ts->client->dev,
1342 "Regulator avdd enable failed ret=%d\n", ret);
1343 goto err_enable_avdd;
1344 }
1345 }
1346
1347 if (!IS_ERR(ts->vdd)) {
1348 ret = regulator_set_voltage(ts->vdd, GOODIX_VTG_MIN_UV,
1349 GOODIX_VTG_MAX_UV);
1350 if (ret) {
1351 dev_err(&ts->client->dev,
1352 "Regulator set_vtg failed vdd ret=%d\n", ret);
1353 goto err_set_vtg_vdd;
1354 }
1355 ret = reg_set_optimum_mode_check(ts->vdd,
1356 GOODIX_VDD_LOAD_MAX_UA);
1357 if (ret < 0) {
1358 dev_err(&ts->client->dev,
1359 "Regulator vdd set_opt failed rc=%d\n", ret);
1360 goto err_set_opt_vdd;
1361 }
1362 ret = regulator_enable(ts->vdd);
1363 if (ret) {
1364 dev_err(&ts->client->dev,
1365 "Regulator vdd enable failed ret=%d\n", ret);
1366 goto err_enable_vdd;
1367 }
1368 }
1369
1370 if (!IS_ERR(ts->vcc_i2c)) {
1371 ret = regulator_set_voltage(ts->vcc_i2c, GOODIX_I2C_VTG_MIN_UV,
1372 GOODIX_I2C_VTG_MAX_UV);
1373 if (ret) {
1374 dev_err(&ts->client->dev,
1375 "Regulator set_vtg failed vcc_i2c ret=%d\n",
1376 ret);
1377 goto err_set_vtg_vcc_i2c;
1378 }
1379 ret = reg_set_optimum_mode_check(ts->vcc_i2c,
1380 GOODIX_VIO_LOAD_MAX_UA);
1381 if (ret < 0) {
1382 dev_err(&ts->client->dev,
1383 "Regulator vcc_i2c set_opt failed rc=%d\n",
1384 ret);
1385 goto err_set_opt_vcc_i2c;
1386 }
1387 ret = regulator_enable(ts->vcc_i2c);
1388 if (ret) {
1389 dev_err(&ts->client->dev,
1390 "Regulator vcc_i2c enable failed ret=%d\n",
1391 ret);
1392 regulator_disable(ts->vdd);
1393 goto err_enable_vcc_i2c;
1394 }
1395 }
1396
1397 return 0;
1398
1399err_enable_vcc_i2c:
1400err_set_opt_vcc_i2c:
1401 if (!IS_ERR(ts->vcc_i2c))
1402 regulator_set_voltage(ts->vcc_i2c, 0, GOODIX_I2C_VTG_MAX_UV);
1403err_set_vtg_vcc_i2c:
1404 if (!IS_ERR(ts->vdd))
1405 regulator_disable(ts->vdd);
1406err_enable_vdd:
1407err_set_opt_vdd:
1408 if (!IS_ERR(ts->vdd))
1409 regulator_set_voltage(ts->vdd, 0, GOODIX_VTG_MAX_UV);
1410err_set_vtg_vdd:
1411 if (!IS_ERR(ts->avdd))
1412 regulator_disable(ts->avdd);
1413err_enable_avdd:
1414err_set_opt_avdd:
1415 return ret;
1416}
1417
1418/**
1419 * goodix_power_off - Turn device power OFF
1420 * @ts: driver private data
1421 *
1422 * Returns zero on success, else an error.
1423 */
1424static int goodix_power_off(struct goodix_ts_data *ts)
1425{
1426 int ret;
1427
1428 if (!IS_ERR(ts->vcc_i2c)) {
1429 ret = regulator_set_voltage(ts->vcc_i2c, 0,
1430 GOODIX_I2C_VTG_MAX_UV);
1431 if (ret < 0)
1432 dev_err(&ts->client->dev,
1433 "Regulator vcc_i2c set_vtg failed ret=%d\n",
1434 ret);
1435 ret = regulator_disable(ts->vcc_i2c);
1436 if (ret)
1437 dev_err(&ts->client->dev,
1438 "Regulator vcc_i2c disable failed ret=%d\n",
1439 ret);
1440 }
1441
1442 if (!IS_ERR(ts->vdd)) {
1443 ret = regulator_set_voltage(ts->vdd, 0, GOODIX_VTG_MAX_UV);
1444 if (ret < 0)
1445 dev_err(&ts->client->dev,
1446 "Regulator vdd set_vtg failed ret=%d\n", ret);
1447 ret = regulator_disable(ts->vdd);
1448 if (ret)
1449 dev_err(&ts->client->dev,
1450 "Regulator vdd disable failed ret=%d\n", ret);
1451 }
1452
1453 if (!IS_ERR(ts->avdd)) {
1454 ret = regulator_disable(ts->avdd);
1455 if (ret)
1456 dev_err(&ts->client->dev,
1457 "Regulator avdd disable failed ret=%d\n", ret);
1458 }
1459
1460 return 0;
1461}
1462
1463/**
1464 * goodix_power_init - Initialize device power
1465 * @ts: driver private data
1466 *
1467 * Returns zero on success, else an error.
1468 */
1469static int goodix_power_init(struct goodix_ts_data *ts)
1470{
1471 int ret;
1472
1473 ts->avdd = regulator_get(&ts->client->dev, "avdd");
1474 if (IS_ERR(ts->avdd)) {
1475 ret = PTR_ERR(ts->avdd);
1476 dev_info(&ts->client->dev,
1477 "Regulator get failed avdd ret=%d\n", ret);
1478 }
1479
1480 ts->vdd = regulator_get(&ts->client->dev, "vdd");
1481 if (IS_ERR(ts->vdd)) {
1482 ret = PTR_ERR(ts->vdd);
1483 dev_info(&ts->client->dev,
1484 "Regulator get failed vdd ret=%d\n", ret);
1485 }
1486
1487 ts->vcc_i2c = regulator_get(&ts->client->dev, "vcc-i2c");
1488 if (IS_ERR(ts->vcc_i2c)) {
1489 ret = PTR_ERR(ts->vcc_i2c);
1490 dev_info(&ts->client->dev,
1491 "Regulator get failed vcc_i2c ret=%d\n", ret);
1492 }
1493
1494 return 0;
1495}
1496
1497/**
1498 * goodix_power_deinit - Deinitialize device power
1499 * @ts: driver private data
1500 *
1501 * Returns zero on success, else an error.
1502 */
1503static int goodix_power_deinit(struct goodix_ts_data *ts)
1504{
1505 regulator_put(ts->vdd);
1506 regulator_put(ts->vcc_i2c);
1507 regulator_put(ts->avdd);
1508
1509 return 0;
1510}
1511
Bingzhe Caid0361172013-07-18 19:22:11 +08001512static int goodix_ts_get_dt_coords(struct device *dev, char *name,
1513 struct goodix_ts_platform_data *pdata)
1514{
1515 struct property *prop;
1516 struct device_node *np = dev->of_node;
1517 int rc;
1518 u32 coords[GOODIX_COORDS_ARR_SIZE];
1519
1520 prop = of_find_property(np, name, NULL);
1521 if (!prop)
1522 return -EINVAL;
1523 if (!prop->value)
1524 return -ENODATA;
1525
1526 rc = of_property_read_u32_array(np, name, coords,
1527 GOODIX_COORDS_ARR_SIZE);
1528 if (rc && (rc != -EINVAL)) {
1529 dev_err(dev, "Unable to read %s\n", name);
1530 return rc;
1531 }
1532
1533 if (!strcmp(name, "goodix,panel-coords")) {
1534 pdata->panel_minx = coords[0];
1535 pdata->panel_miny = coords[1];
1536 pdata->panel_maxx = coords[2];
1537 pdata->panel_maxy = coords[3];
1538 } else if (!strcmp(name, "goodix,display-coords")) {
1539 pdata->x_min = coords[0];
1540 pdata->y_min = coords[1];
1541 pdata->x_max = coords[2];
1542 pdata->y_max = coords[3];
1543 } else {
1544 dev_err(dev, "unsupported property %s\n", name);
1545 return -EINVAL;
1546 }
1547
1548 return 0;
1549}
1550
1551static int goodix_parse_dt(struct device *dev,
1552 struct goodix_ts_platform_data *pdata)
1553{
1554 int rc;
1555 struct device_node *np = dev->of_node;
1556 struct property *prop;
1557 u32 temp_val, num_buttons;
1558 u32 button_map[MAX_BUTTONS];
1559
1560 rc = goodix_ts_get_dt_coords(dev, "goodix,panel-coords", pdata);
1561 if (rc && (rc != -EINVAL))
1562 return rc;
1563
1564 rc = goodix_ts_get_dt_coords(dev, "goodix,display-coords", pdata);
1565 if (rc)
1566 return rc;
1567
1568 pdata->i2c_pull_up = of_property_read_bool(np,
1569 "goodix,i2c-pull-up");
1570
1571 pdata->no_force_update = of_property_read_bool(np,
1572 "goodix,no-force-update");
1573 /* reset, irq gpio info */
1574 pdata->reset_gpio = of_get_named_gpio_flags(np, "reset-gpios",
1575 0, &pdata->reset_gpio_flags);
1576 if (pdata->reset_gpio < 0)
1577 return pdata->reset_gpio;
1578
1579 pdata->irq_gpio = of_get_named_gpio_flags(np, "interrupt-gpios",
1580 0, &pdata->irq_gpio_flags);
1581 if (pdata->irq_gpio < 0)
1582 return pdata->irq_gpio;
1583
1584 rc = of_property_read_u32(np, "goodix,family-id", &temp_val);
1585 if (!rc)
1586 pdata->family_id = temp_val;
1587 else
1588 return rc;
1589
1590 prop = of_find_property(np, "goodix,button-map", NULL);
1591 if (prop) {
1592 num_buttons = prop->length / sizeof(temp_val);
1593 if (num_buttons > MAX_BUTTONS)
1594 return -EINVAL;
1595
1596 rc = of_property_read_u32_array(np,
1597 "goodix,button-map", button_map,
1598 num_buttons);
1599 if (rc) {
1600 dev_err(dev, "Unable to read key codes\n");
1601 return rc;
1602 }
1603 }
1604
1605 prop = of_find_property(np, "goodix,cfg-data", &pdata->gtp_cfg_len);
1606 if (prop && prop->value) {
1607 pdata->config_data = devm_kzalloc(dev,
1608 GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH, GFP_KERNEL);
1609 if (!pdata->config_data) {
1610 dev_err(dev, "Not enough memory for panel config data\n");
1611 return -ENOMEM;
1612 }
1613
1614 pdata->config_data[0] = GTP_REG_CONFIG_DATA >> 8;
1615 pdata->config_data[1] = GTP_REG_CONFIG_DATA & 0xff;
1616 memset(&pdata->config_data[GTP_ADDR_LENGTH], 0,
1617 GTP_CONFIG_MAX_LENGTH);
1618 memcpy(&pdata->config_data[GTP_ADDR_LENGTH],
1619 prop->value, pdata->gtp_cfg_len);
1620 } else {
1621 dev_err(dev,
1622 "Unable to get configure data, default will be used.\n");
1623 pdata->gtp_cfg_len = 0;
1624 }
1625
1626 return 0;
1627}
Bingzhe Caic68e6112013-07-22 00:12:48 +08001628
Andrew Huangab45aab2013-06-27 10:06:56 +00001629/*******************************************************
1630Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001631 I2c probe.
Andrew Huangab45aab2013-06-27 10:06:56 +00001632Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001633 client: i2c device struct.
1634 id: device id.
Andrew Huangab45aab2013-06-27 10:06:56 +00001635Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001636 Executive outcomes.
1637 0: succeed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001638*******************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001639
1640static int goodix_ts_probe(struct i2c_client *client,
1641 const struct i2c_device_id *id)
Andrew Huangab45aab2013-06-27 10:06:56 +00001642{
Bingzhe Caid0361172013-07-18 19:22:11 +08001643 struct goodix_ts_platform_data *pdata;
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001644 struct goodix_ts_data *ts;
1645 u16 version_info;
1646 int ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00001647
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001648 dev_dbg(&client->dev, "GTP I2C Address: 0x%02x\n", client->addr);
Bingzhe Caid0361172013-07-18 19:22:11 +08001649 if (client->dev.of_node) {
1650 pdata = devm_kzalloc(&client->dev,
1651 sizeof(struct goodix_ts_platform_data), GFP_KERNEL);
1652 if (!pdata) {
1653 dev_err(&client->dev,
1654 "GTP Failed to allocate memory for pdata\n");
1655 return -ENOMEM;
1656 }
1657
1658 ret = goodix_parse_dt(&client->dev, pdata);
1659 if (ret)
1660 return ret;
1661 } else {
1662 pdata = client->dev.platform_data;
1663 }
1664
1665 if (!pdata) {
1666 dev_err(&client->dev, "GTP invalid pdata\n");
1667 return -EINVAL;
1668 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001669
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001670#if GTP_ESD_PROTECT
1671 i2c_connect_client = client;
1672#endif
Bingzhe Caid0361172013-07-18 19:22:11 +08001673
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001674 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1675 dev_err(&client->dev, "GTP I2C not supported\n");
1676 return -ENODEV;
1677 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001678
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001679 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1680 if (!ts) {
1681 dev_err(&client->dev, "GTP not enough memory for ts\n");
1682 return -ENOMEM;
1683 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001684
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001685 memset(ts, 0, sizeof(*ts));
1686 ts->client = client;
Bingzhe Caid0361172013-07-18 19:22:11 +08001687 ts->pdata = pdata;
1688 /* For 2.6.39 & later use spin_lock_init(&ts->irq_lock)
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001689 * For 2.6.39 & before, use ts->irq_lock = SPIN_LOCK_UNLOCKED
1690 */
1691 spin_lock_init(&ts->irq_lock);
1692 i2c_set_clientdata(client, ts);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001693 ts->gtp_rawdiff_mode = 0;
1694
Bingzhe Caic68e6112013-07-22 00:12:48 +08001695 ret = goodix_power_init(ts);
1696 if (ret) {
1697 dev_err(&client->dev, "GTP power init failed\n");
1698 goto exit_free_client_data;
1699 }
1700
1701 ret = goodix_power_on(ts);
1702 if (ret) {
1703 dev_err(&client->dev, "GTP power on failed\n");
1704 goto exit_deinit_power;
1705 }
1706
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001707 ret = gtp_request_io_port(ts);
1708 if (ret) {
1709 dev_err(&client->dev, "GTP request IO port failed.\n");
1710 goto exit_power_off;
1711 }
1712
1713 gtp_reset_guitar(ts, 20);
1714
1715 ret = gtp_i2c_test(client);
1716 if (ret != 2) {
1717 dev_err(&client->dev, "I2C communication ERROR!\n");
1718 goto exit_free_io_port;
1719 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001720
1721#if GTP_AUTO_UPDATE
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001722 ret = gup_init_update_proc(ts);
1723 if (ret < 0) {
1724 dev_err(&client->dev,
1725 "GTP Create firmware update thread error.\n");
1726 goto exit_free_io_port;
1727 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001728#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001729
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001730 ret = gtp_init_panel(ts);
1731 if (ret < 0) {
1732 dev_err(&client->dev, "GTP init panel failed.\n");
1733 ts->abs_x_max = GTP_MAX_WIDTH;
1734 ts->abs_y_max = GTP_MAX_HEIGHT;
1735 ts->int_trigger_type = GTP_INT_TRIGGER;
1736 }
Andrew Huangab45aab2013-06-27 10:06:56 +00001737
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001738 ret = gtp_request_input_dev(ts);
1739 if (ret) {
1740 dev_err(&client->dev, "GTP request input dev failed.\n");
1741 goto exit_free_inputdev;
1742 }
1743
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001744#if defined(CONFIG_FB)
1745 ts->fb_notif.notifier_call = fb_notifier_callback;
1746 ret = fb_register_client(&ts->fb_notif);
1747 if (ret)
1748 dev_err(&ts->client->dev,
1749 "Unable to register fb_notifier: %d\n",
1750 ret);
1751#elif defined(CONFIG_HAS_EARLYSUSPEND)
1752 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1753 ts->early_suspend.suspend = goodix_ts_early_suspend;
1754 ts->early_suspend.resume = goodix_ts_late_resume;
1755 register_early_suspend(&ts->early_suspend);
1756#endif
1757
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001758 ts->goodix_wq = create_singlethread_workqueue("goodix_wq");
1759 INIT_WORK(&ts->work, goodix_ts_work_func);
1760
1761 ret = gtp_request_irq(ts);
1762 if (ret < 0)
1763 dev_info(&client->dev, "GTP works in polling mode.\n");
1764 else
1765 dev_info(&client->dev, "GTP works in interrupt mode.\n");
1766
1767 ret = gtp_read_version(client, &version_info);
1768 if (ret != 2) {
1769 dev_err(&client->dev, "Read version failed.\n");
1770 goto exit_free_irq;
1771 }
1772 if (ts->use_irq)
1773 gtp_irq_enable(ts);
1774
Andrew Huangab45aab2013-06-27 10:06:56 +00001775#if GTP_CREATE_WR_NODE
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001776 init_wr_node(client);
Andrew Huangab45aab2013-06-27 10:06:56 +00001777#endif
Andrew Huangab45aab2013-06-27 10:06:56 +00001778
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001779#if GTP_ESD_PROTECT
1780 gtp_esd_switch(client, SWITCH_ON);
1781#endif
1782 init_done = true;
1783 return 0;
1784exit_free_irq:
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001785#if defined(CONFIG_FB)
1786 if (fb_unregister_client(&ts->fb_notif))
1787 dev_err(&client->dev,
1788 "Error occurred while unregistering fb_notifier.\n");
1789#elif defined(CONFIG_HAS_EARLYSUSPEND)
1790 unregister_early_suspend(&ts->early_suspend);
1791#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001792 if (ts->use_irq)
1793 free_irq(client->irq, ts);
1794 else
1795 hrtimer_cancel(&ts->timer);
1796 cancel_work_sync(&ts->work);
1797 flush_workqueue(ts->goodix_wq);
1798 destroy_workqueue(ts->goodix_wq);
1799
1800 input_unregister_device(ts->input_dev);
1801 if (ts->input_dev) {
1802 input_free_device(ts->input_dev);
1803 ts->input_dev = NULL;
1804 }
1805exit_free_inputdev:
1806 kfree(ts->config_data);
1807exit_free_io_port:
Bingzhe Caid0361172013-07-18 19:22:11 +08001808 if (gpio_is_valid(pdata->reset_gpio))
1809 gpio_free(pdata->reset_gpio);
1810 if (gpio_is_valid(pdata->irq_gpio))
1811 gpio_free(pdata->irq_gpio);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001812exit_power_off:
Bingzhe Caic68e6112013-07-22 00:12:48 +08001813 goodix_power_off(ts);
1814exit_deinit_power:
1815 goodix_power_deinit(ts);
1816exit_free_client_data:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001817 i2c_set_clientdata(client, NULL);
1818 kfree(ts);
1819 return ret;
1820}
Andrew Huangab45aab2013-06-27 10:06:56 +00001821
1822/*******************************************************
1823Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001824 Goodix touchscreen driver release function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001825Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001826 client: i2c device struct.
Andrew Huangab45aab2013-06-27 10:06:56 +00001827Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001828 Executive outcomes. 0---succeed.
Andrew Huangab45aab2013-06-27 10:06:56 +00001829*******************************************************/
1830static int goodix_ts_remove(struct i2c_client *client)
1831{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001832 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1833
1834 GTP_DEBUG_FUNC();
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001835#if defined(CONFIG_FB)
1836 if (fb_unregister_client(&ts->fb_notif))
1837 dev_err(&client->dev,
1838 "Error occurred while unregistering fb_notifier.\n");
1839#elif defined(CONFIG_HAS_EARLYSUSPEND)
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001840 unregister_early_suspend(&ts->early_suspend);
Andrew Huangab45aab2013-06-27 10:06:56 +00001841#endif
1842
1843#if GTP_CREATE_WR_NODE
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001844 uninit_wr_node();
Andrew Huangab45aab2013-06-27 10:06:56 +00001845#endif
1846
1847#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001848 cancel_work_sync(gtp_esd_check_workqueue);
1849 flush_workqueue(gtp_esd_check_workqueue);
1850 destroy_workqueue(gtp_esd_check_workqueue);
Andrew Huangab45aab2013-06-27 10:06:56 +00001851#endif
1852
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001853 if (ts) {
1854 if (ts->use_irq)
1855 free_irq(client->irq, ts);
1856 else
1857 hrtimer_cancel(&ts->timer);
Andrew Huangab45aab2013-06-27 10:06:56 +00001858
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001859 cancel_work_sync(&ts->work);
1860 flush_workqueue(ts->goodix_wq);
1861 destroy_workqueue(ts->goodix_wq);
1862
1863 input_unregister_device(ts->input_dev);
1864 if (ts->input_dev) {
1865 input_free_device(ts->input_dev);
1866 ts->input_dev = NULL;
1867 }
1868 kfree(ts->config_data);
1869
1870 if (gpio_is_valid(ts->pdata->reset_gpio))
1871 gpio_free(ts->pdata->reset_gpio);
1872 if (gpio_is_valid(ts->pdata->irq_gpio))
1873 gpio_free(ts->pdata->irq_gpio);
1874
Bingzhe Caic68e6112013-07-22 00:12:48 +08001875 goodix_power_off(ts);
1876 goodix_power_deinit(ts);
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001877 i2c_set_clientdata(client, NULL);
1878 kfree(ts);
1879 }
1880
1881 return 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00001882}
1883
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001884#if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_FB)
Andrew Huangab45aab2013-06-27 10:06:56 +00001885/*******************************************************
1886Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001887 Early suspend function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001888Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001889 h: early_suspend struct.
Andrew Huangab45aab2013-06-27 10:06:56 +00001890Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001891 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00001892*******************************************************/
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001893static void goodix_ts_suspend(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001894{
Shantanu Jain33b1aee2013-08-05 16:52:47 +05301895 int ret = -1, i;
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001896
1897 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00001898
1899#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001900 ts->gtp_is_suspend = 1;
1901 gtp_esd_switch(ts->client, SWITCH_OFF);
Andrew Huangab45aab2013-06-27 10:06:56 +00001902#endif
1903
1904#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001905 ret = gtp_enter_doze(ts);
Andrew Huangab45aab2013-06-27 10:06:56 +00001906#else
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001907 if (ts->use_irq)
1908 gtp_irq_disable(ts);
1909 else
1910 hrtimer_cancel(&ts->timer);
Shantanu Jain33b1aee2013-08-05 16:52:47 +05301911
1912 for (i = 0; i < GTP_MAX_TOUCH; i++)
1913 gtp_touch_up(ts, i);
1914
Shantanu Jain33b1aee2013-08-05 16:52:47 +05301915 input_sync(ts->input_dev);
1916
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001917 ret = gtp_enter_sleep(ts);
1918#endif
1919 if (ret < 0)
1920 dev_err(&ts->client->dev, "GTP early suspend failed.\n");
1921 /* to avoid waking up while not sleeping,
1922 * delay 48 + 10ms to ensure reliability
1923 */
1924 msleep(58);
Andrew Huangab45aab2013-06-27 10:06:56 +00001925}
1926
1927/*******************************************************
1928Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001929 Late resume function.
Andrew Huangab45aab2013-06-27 10:06:56 +00001930Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001931 h: early_suspend struct.
Andrew Huangab45aab2013-06-27 10:06:56 +00001932Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001933 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00001934*******************************************************/
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001935static void goodix_ts_resume(struct goodix_ts_data *ts)
Andrew Huangab45aab2013-06-27 10:06:56 +00001936{
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001937 int ret = -1;
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001938
1939 GTP_DEBUG_FUNC();
1940
1941 ret = gtp_wakeup_sleep(ts);
Andrew Huangab45aab2013-06-27 10:06:56 +00001942
1943#if GTP_SLIDE_WAKEUP
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001944 doze_status = DOZE_DISABLED;
Andrew Huangab45aab2013-06-27 10:06:56 +00001945#endif
1946
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001947 if (ret < 0)
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001948 dev_err(&ts->client->dev, "GTP resume failed.\n");
Andrew Huangab45aab2013-06-27 10:06:56 +00001949
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001950 if (ts->use_irq)
1951 gtp_irq_enable(ts);
1952 else
1953 hrtimer_start(&ts->timer,
1954 ktime_set(1, 0), HRTIMER_MODE_REL);
Andrew Huangab45aab2013-06-27 10:06:56 +00001955
1956#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08001957 ts->gtp_is_suspend = 0;
1958 gtp_esd_switch(ts->client, SWITCH_ON);
Andrew Huangab45aab2013-06-27 10:06:56 +00001959#endif
1960}
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08001961
1962#if defined(CONFIG_FB)
1963static int fb_notifier_callback(struct notifier_block *self,
1964 unsigned long event, void *data)
1965{
1966 struct fb_event *evdata = data;
1967 int *blank;
1968 struct goodix_ts_data *ts =
1969 container_of(self, struct goodix_ts_data, fb_notif);
1970
1971 if (evdata && evdata->data && event == FB_EVENT_BLANK &&
1972 ts && ts->client) {
1973 blank = evdata->data;
1974 if (*blank == FB_BLANK_UNBLANK)
1975 goodix_ts_resume(ts);
1976 else if (*blank == FB_BLANK_POWERDOWN)
1977 goodix_ts_suspend(ts);
1978 }
1979
1980 return 0;
1981}
1982#elif defined(CONFIG_HAS_EARLYSUSPEND)
1983/*******************************************************
1984Function:
1985 Early suspend function.
1986Input:
1987 h: early_suspend struct.
1988Output:
1989 None.
1990*******************************************************/
1991static void goodix_ts_early_suspend(struct early_suspend *h)
1992{
1993 struct goodix_ts_data *ts;
1994
1995 ts = container_of(h, struct goodix_ts_data, early_suspend);
1996 goodix_ts_suspend(ts);
1997 return;
1998}
1999
2000/*******************************************************
2001Function:
2002 Late resume function.
2003Input:
2004 h: early_suspend struct.
2005Output:
2006 None.
2007*******************************************************/
2008static void goodix_ts_late_resume(struct early_suspend *h)
2009{
2010 struct goodix_ts_data *ts;
2011
2012 ts = container_of(h, struct goodix_ts_data, early_suspend);
2013 goodix_ts_late_resume(ts);
2014 return;
2015}
Andrew Huangab45aab2013-06-27 10:06:56 +00002016#endif
Bingzhe Cai7d49bb32013-07-22 18:23:20 +08002017#endif /* !CONFIG_HAS_EARLYSUSPEND && !CONFIG_FB*/
Andrew Huangab45aab2013-06-27 10:06:56 +00002018
2019#if GTP_ESD_PROTECT
2020/*******************************************************
2021Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002022 switch on & off esd delayed work
Andrew Huangab45aab2013-06-27 10:06:56 +00002023Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002024 client: i2c device
2025 on: SWITCH_ON / SWITCH_OFF
Andrew Huangab45aab2013-06-27 10:06:56 +00002026Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002027 void
Andrew Huangab45aab2013-06-27 10:06:56 +00002028*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002029void gtp_esd_switch(struct i2c_client *client, int on)
Andrew Huangab45aab2013-06-27 10:06:56 +00002030{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002031 struct goodix_ts_data *ts;
2032
2033 ts = i2c_get_clientdata(client);
2034 if (SWITCH_ON == on) {
2035 /* switch on esd */
2036 if (!ts->esd_running) {
2037 ts->esd_running = 1;
2038 dev_dbg(&client->dev, "Esd started\n");
2039 queue_delayed_work(gtp_esd_check_workqueue,
2040 &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE);
2041 }
2042 } else {
2043 /* switch off esd */
2044 if (ts->esd_running) {
2045 ts->esd_running = 0;
2046 dev_dbg(&client->dev, "Esd cancelled\n");
2047 cancel_delayed_work_sync(&gtp_esd_check_work);
2048 }
2049 }
Andrew Huangab45aab2013-06-27 10:06:56 +00002050}
2051
2052/*******************************************************
2053Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002054 Initialize external watchdog for esd protect
Andrew Huangab45aab2013-06-27 10:06:56 +00002055Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002056 client: i2c device.
Andrew Huangab45aab2013-06-27 10:06:56 +00002057Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002058 result of i2c write operation.
2059 1: succeed, otherwise: failed
Andrew Huangab45aab2013-06-27 10:06:56 +00002060*********************************************************/
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002061static int gtp_init_ext_watchdog(struct i2c_client *client)
Andrew Huangab45aab2013-06-27 10:06:56 +00002062{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002063 /* in case of recursively reset by calling gtp_i2c_write*/
2064 struct i2c_msg msg;
2065 u8 opr_buffer[4] = {0x80, 0x40, 0xAA, 0xAA};
2066 int ret;
2067 int retries = 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00002068
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002069 GTP_DEBUG("Init external watchdog...");
2070 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00002071
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002072 msg.flags = !I2C_M_RD;
2073 msg.addr = client->addr;
2074 msg.len = 4;
2075 msg.buf = opr_buffer;
Andrew Huangab45aab2013-06-27 10:06:56 +00002076
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002077 while (retries < 5) {
2078 ret = i2c_transfer(client->adapter, &msg, 1);
2079 if (ret == 1)
2080 return 1;
2081 retries++;
2082 }
2083 if (retries >= 5)
2084 dev_err(&client->dev, "init external watchdog failed!");
2085 return 0;
Andrew Huangab45aab2013-06-27 10:06:56 +00002086}
2087
2088/*******************************************************
2089Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002090 Esd protect function.
2091 Added external watchdog by meta, 2013/03/07
Andrew Huangab45aab2013-06-27 10:06:56 +00002092Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002093 work: delayed work
Andrew Huangab45aab2013-06-27 10:06:56 +00002094Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002095 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00002096*******************************************************/
2097static void gtp_esd_check_func(struct work_struct *work)
2098{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002099 s32 i;
2100 s32 ret = -1;
2101 struct goodix_ts_data *ts = NULL;
2102 u8 test[4] = {0x80, 0x40};
Andrew Huangab45aab2013-06-27 10:06:56 +00002103
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002104 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00002105
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002106 ts = i2c_get_clientdata(i2c_connect_client);
2107
2108 if (ts->gtp_is_suspend) {
2109 dev_dbg(&ts->client->dev, "Esd terminated!\n");
2110 ts->esd_running = 0;
2111 return;
2112 }
2113#ifdef CONFIG_GT9XX_TOUCHPANEL_UPDATE
2114 if (ts->enter_update)
2115 return;
2116#endif
2117
2118 for (i = 0; i < 3; i++) {
2119 ret = gtp_i2c_read(ts->client, test, 4);
2120
2121 GTP_DEBUG("0x8040 = 0x%02X, 0x8041 = 0x%02X", test[2], test[3]);
2122 if ((ret < 0)) {
2123 /* IC works abnormally..*/
2124 continue;
2125 } else {
2126 if ((test[2] == 0xAA) || (test[3] != 0xAA)) {
2127 /* IC works abnormally..*/
2128 i = 3;
2129 break;
2130 } else {
2131 /* IC works normally, Write 0x8040 0xAA*/
2132 test[2] = 0xAA;
2133 gtp_i2c_write(ts->client, test, 3);
2134 break;
2135 }
2136 }
2137 }
2138 if (i >= 3) {
2139 dev_err(&ts->client->dev,
2140 "IC Working ABNORMALLY, Resetting Guitar...\n");
2141 gtp_reset_guitar(ts, 50);
2142 }
2143
2144 if (!ts->gtp_is_suspend)
2145 queue_delayed_work(gtp_esd_check_workqueue,
2146 &gtp_esd_check_work, GTP_ESD_CHECK_CIRCLE);
2147 else {
2148 dev_dbg(&ts->client->dev, "Esd terminated!\n");
2149 ts->esd_running = 0;
2150 }
2151
2152 return;
Andrew Huangab45aab2013-06-27 10:06:56 +00002153}
2154#endif
2155
2156static const struct i2c_device_id goodix_ts_id[] = {
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002157 { GTP_I2C_NAME, 0 },
2158 { }
Andrew Huangab45aab2013-06-27 10:06:56 +00002159};
2160
Bingzhe Caid0361172013-07-18 19:22:11 +08002161static struct of_device_id goodix_match_table[] = {
2162 { .compatible = "goodix,gt9xx", },
2163 { },
2164};
2165
Andrew Huangab45aab2013-06-27 10:06:56 +00002166static struct i2c_driver goodix_ts_driver = {
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002167 .probe = goodix_ts_probe,
2168 .remove = goodix_ts_remove,
2169#ifdef CONFIG_HAS_EARLYSUSPEND
2170 .suspend = goodix_ts_early_suspend,
2171 .resume = goodix_ts_late_resume,
Andrew Huangab45aab2013-06-27 10:06:56 +00002172#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002173 .id_table = goodix_ts_id,
2174 .driver = {
2175 .name = GTP_I2C_NAME,
2176 .owner = THIS_MODULE,
Bingzhe Caid0361172013-07-18 19:22:11 +08002177 .of_match_table = goodix_match_table,
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002178 },
Andrew Huangab45aab2013-06-27 10:06:56 +00002179};
2180
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002181/*******************************************************
Andrew Huangab45aab2013-06-27 10:06:56 +00002182Function:
2183 Driver Install function.
2184Input:
2185 None.
2186Output:
2187 Executive Outcomes. 0---succeed.
2188********************************************************/
2189static int __devinit goodix_ts_init(void)
2190{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002191 int ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00002192
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002193 GTP_DEBUG_FUNC();
Andrew Huangab45aab2013-06-27 10:06:56 +00002194#if GTP_ESD_PROTECT
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002195 INIT_DELAYED_WORK(&gtp_esd_check_work, gtp_esd_check_func);
2196 gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
Andrew Huangab45aab2013-06-27 10:06:56 +00002197#endif
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002198 ret = i2c_add_driver(&goodix_ts_driver);
2199 return ret;
Andrew Huangab45aab2013-06-27 10:06:56 +00002200}
2201
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002202/*******************************************************
Andrew Huangab45aab2013-06-27 10:06:56 +00002203Function:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002204 Driver uninstall function.
Andrew Huangab45aab2013-06-27 10:06:56 +00002205Input:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002206 None.
Andrew Huangab45aab2013-06-27 10:06:56 +00002207Output:
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002208 Executive Outcomes. 0---succeed.
Andrew Huangab45aab2013-06-27 10:06:56 +00002209********************************************************/
2210static void __exit goodix_ts_exit(void)
2211{
Bingzhe Cai83caeea2013-07-16 17:57:37 +08002212 GTP_DEBUG_FUNC();
2213 i2c_del_driver(&goodix_ts_driver);
Andrew Huangab45aab2013-06-27 10:06:56 +00002214}
2215
2216late_initcall(goodix_ts_init);
2217module_exit(goodix_ts_exit);
2218
2219MODULE_DESCRIPTION("GTP Series Driver");
2220MODULE_LICENSE("GPL");