summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/focaltech_touch/focaltech_ex_mode.c
blob: 6ab5d47170cce99e36ee6c05636c1f58094f0e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 *
 * FocalTech ftxxxx TouchScreen driver.
 *
 * Copyright (c) 2010-2016, Focaltech Ltd. All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

/*****************************************************************************
*
* File Name: focaltech_ex_mode.c
*
*    Author: Liu WeiGuang
*
*   Created: 2016-08-31
*
*  Abstract:
*
* Reference:
*
*****************************************************************************/

/*****************************************************************************
* 1.Included header files
*****************************************************************************/
#include "focaltech_core.h"

/*****************************************************************************
* 2.Private constant and macro definitions using #define
*****************************************************************************/

/*****************************************************************************
* 3.Private enumerations, structures and unions using typedef
*****************************************************************************/
struct fts_mode_flag {
	int fts_glove_mode_flag;
	int fts_cover_mode_flag;
	int fts_charger_mode_flag;
};

struct fts_mode_flag g_fts_mode_flag;

/*****************************************************************************
* 4.Static variables
*****************************************************************************/

/*****************************************************************************
* 5.Global variable or extern global variabls/functions
*****************************************************************************/
int fts_enter_glove_mode(struct i2c_client *client, int mode);
int fts_glove_init(struct i2c_client *client);
int fts_glove_exit(struct i2c_client *client);

int fts_enter_cover_mode(struct i2c_client *client, int mode);
int fts_cover_init(struct i2c_client *client);
int fts_cover_exit(struct i2c_client *client);

int fts_enter_charger_mode(struct i2c_client *client, int mode);
int fts_charger_init(struct i2c_client *client);
int fts_charger_exit(struct i2c_client *client);

/*****************************************************************************
* 6.Static function prototypes
*******************************************************************************/

#if FTS_GLOVE_EN
static ssize_t fts_touch_glove_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "Glove: %s\n",
			g_fts_mode_flag.fts_glove_mode_flag ? "On" : "Off");
}

static ssize_t fts_touch_glove_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	int ret;

	if (FTS_SYSFS_ECHO_ON(buf)) {
		if (!g_fts_mode_flag.fts_glove_mode_flag) {
			FTS_INFO("[Mode]enter glove mode");
			ret = fts_enter_glove_mode(fts_i2c_client, true);
			if (ret >= 0)
				g_fts_mode_flag.fts_glove_mode_flag = true;
		}
	} else if (FTS_SYSFS_ECHO_OFF(buf)) {
		if (g_fts_mode_flag.fts_glove_mode_flag) {
			FTS_INFO("[Mode]exit glove mode");
			ret = fts_enter_glove_mode(fts_i2c_client, false);
			if (ret >= 0)
				g_fts_mode_flag.fts_glove_mode_flag = false;
		}
	}
	FTS_INFO("[Mode]glove mode status:  %d",
		 g_fts_mode_flag.fts_glove_mode_flag);
	return count;
}

/************************************************************************
* Name: fts_enter_glove_mode
* Brief:  change glove mode
* Input:  glove mode
* Output: no
* Return: success >=0, otherwise failed
***********************************************************************/
int fts_enter_glove_mode(struct i2c_client *client, int mode)
{
	int ret = 0;
	static u8 buf_addr[2] = { 0 };
	static u8 buf_value[2] = { 0 };

	buf_addr[0] = FTS_REG_GLOVE_MODE_EN;

	if (mode)
		buf_value[0] = 0x01;
	else
		buf_value[0] = 0x00;

	ret = fts_i2c_write_reg(client, buf_addr[0], buf_value[0]);
	if (ret < 0)
		FTS_ERROR("[Mode]fts_enter_glove_mode write value fail");

	return ret;

}

/* read and write glove mode
*   read example: cat  fts_touch_glove_mode---read  glove mode
*   write example:echo 01 > fts_touch_glove_mode ---write glove mode to 01
*
*/
static DEVICE_ATTR(fts_glove_mode, S_IRUGO | S_IWUSR, fts_touch_glove_show,
		   fts_touch_glove_store);

#endif

#if FTS_COVER_EN
static ssize_t fts_touch_cover_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "Cover: %s\n",
			g_fts_mode_flag.fts_cover_mode_flag ? "On" : "Off");
}

static ssize_t fts_touch_cover_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	int ret;

	if (FTS_SYSFS_ECHO_ON(buf)) {
		if (!g_fts_mode_flag.fts_cover_mode_flag) {
			FTS_INFO("[Mode]enter cover mode");
			ret = fts_enter_cover_mode(fts_i2c_client, true);
			if (ret >= 0)
				g_fts_mode_flag.fts_cover_mode_flag = true;
		}
	} else if (FTS_SYSFS_ECHO_OFF(buf)) {
		if (g_fts_mode_flag.fts_cover_mode_flag) {
			FTS_INFO("[Mode]exit cover mode");
			ret = fts_enter_cover_mode(fts_i2c_client, false);
			if (ret >= 0)
				g_fts_mode_flag.fts_cover_mode_flag = false;
		}
	}
	FTS_INFO("[Mode]cover mode status:  %d",
		 g_fts_mode_flag.fts_cover_mode_flag);
	return count;
}

/************************************************************************
* Name: fts_enter_cover_mode
* Brief:  change cover mode
* Input:  cover mode
* Output: no
* Return: success >=0, otherwise failed
***********************************************************************/
int fts_enter_cover_mode(struct i2c_client *client, int mode)
{
	int ret = 0;
	static u8 buf_addr[2] = { 0 };
	static u8 buf_value[2] = { 0 };

	buf_addr[0] = FTS_REG_COVER_MODE_EN;

	if (mode)
		buf_value[0] = 0x01;
	else
		buf_value[0] = 0x00;

	ret = fts_i2c_write_reg(client, buf_addr[0], buf_value[0]);
	if (ret < 0)
		FTS_ERROR("[Mode] fts_enter_cover_mode write value fail\n");

	return ret;

}

/* read and write cover mode
*   read example: cat  fts_touch_cover_mode---read  cover mode
*   write example:echo 01 > fts_touch_cover_mode ---write cover mode to 01
*
*/
static DEVICE_ATTR(fts_cover_mode, S_IRUGO | S_IWUSR, fts_touch_cover_show,
		   fts_touch_cover_store);

#endif

#if FTS_CHARGER_EN
static ssize_t fts_touch_charger_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "Charger: %s\n",
			g_fts_mode_flag.fts_charger_mode_flag ? "On" : "Off");
}

static ssize_t fts_touch_charger_store(struct device *dev,
				       struct device_attribute *attr,
				       const char *buf, size_t count)
{
	int ret;

	if (FTS_SYSFS_ECHO_ON(buf)) {
		if (!g_fts_mode_flag.fts_charger_mode_flag) {
			FTS_INFO("[Mode]enter charger mode");
			ret = fts_enter_charger_mode(fts_i2c_client, true);
			if (ret >= 0)
				g_fts_mode_flag.fts_charger_mode_flag = true;
		}
	} else if (FTS_SYSFS_ECHO_OFF(buf)) {
		if (g_fts_mode_flag.fts_charger_mode_flag) {
			FTS_INFO("[Mode]exit charger mode");
			ret = fts_enter_charger_mode(fts_i2c_client, false);
			if (ret >= 0)
				g_fts_mode_flag.fts_charger_mode_flag = false;
		}
	}
	FTS_INFO("[Mode]charger mode status: %d",
		 g_fts_mode_flag.fts_charger_mode_flag);
	return count;
}

/************************************************************************
* Name: fts_enter_charger_mode
* Brief:  change charger mode
* Input:  charger mode
* Output: no
* Return: success >=0, otherwise failed
***********************************************************************/
int fts_enter_charger_mode(struct i2c_client *client, int mode)
{
	int ret = 0;
	static u8 buf_addr[2] = { 0 };
	static u8 buf_value[2] = { 0 };

	buf_addr[0] = FTS_REG_CHARGER_MODE_EN;

	if (mode)
		buf_value[0] = 0x01;
	else
		buf_value[0] = 0x00;

	ret = fts_i2c_write_reg(client, buf_addr[0], buf_value[0]);
	if (ret < 0)
		FTS_DEBUG("[Mode]fts_enter_charger_mode write value fail");

	return ret;

}

/* read and write charger mode
*   read example: cat  fts_touch_charger_mode---read  charger mode
*   write example:echo 01 > fts_touch_charger_mode ---write charger mode to 01
*
*/
static DEVICE_ATTR(fts_charger_mode, S_IRUGO | S_IWUSR, fts_touch_charger_show,
		   fts_touch_charger_store);

#endif

static struct attribute *fts_touch_mode_attrs[] = {
#if FTS_GLOVE_EN
	&dev_attr_fts_glove_mode.attr,
#endif

#if FTS_COVER_EN
	&dev_attr_fts_cover_mode.attr,
#endif

#if FTS_CHARGER_EN
	&dev_attr_fts_charger_mode.attr,
#endif

	NULL,
};

static struct attribute_group fts_touch_mode_group = {
	.attrs = fts_touch_mode_attrs,
};

int fts_ex_mode_init(struct i2c_client *client)
{
	int err = 0;

	g_fts_mode_flag.fts_glove_mode_flag = false;
	g_fts_mode_flag.fts_cover_mode_flag = false;
	g_fts_mode_flag.fts_charger_mode_flag = false;

	err = sysfs_create_group(&client->dev.kobj, &fts_touch_mode_group);
	if (0 != err) {
		FTS_ERROR("[Mode]create sysfs failed.");
		sysfs_remove_group(&client->dev.kobj, &fts_touch_mode_group);
		return -EIO;
	}

	FTS_DEBUG("[Mode]create sysfs succeeded");

	return err;

}

int fts_ex_mode_exit(struct i2c_client *client)
{
	sysfs_remove_group(&client->dev.kobj, &fts_touch_mode_group);
	return 0;
}

int fts_ex_mode_recovery(struct i2c_client *client)
{
	int ret = 0;
#if FTS_GLOVE_EN
	if (g_fts_mode_flag.fts_glove_mode_flag)
		ret = fts_enter_glove_mode(client, true);
#endif

#if FTS_COVER_EN
	if (g_fts_mode_flag.fts_cover_mode_flag)
		ret = fts_enter_cover_mode(client, true);
#endif

#if FTS_CHARGER_EN
	if (g_fts_mode_flag.fts_charger_mode_flag)
		ret = fts_enter_charger_mode(client, true);
#endif

	return ret;
}