aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/eint/eint_drv.c
blob: 035501f133df265a00d0c3a99e71633a5ea2c370 (plain) (blame)
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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <mach/eint_drv.h>

static struct mt_eint_driver mt_eint_drv = {
	.driver = {
		   .driver = {
			      .name = "eint",
			      .bus = &platform_bus_type,
			      .owner = THIS_MODULE,
			      },
		   },
};

static unsigned int cur_eint_num;

struct mt_eint_driver *get_mt_eint_drv(void)
{
	return &mt_eint_drv;
}

static ssize_t cur_eint_show(struct device_driver *driver, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", cur_eint_num);
}

static ssize_t cur_eint_store(struct device_driver *driver, const char *buf, size_t count)
{
	char *p = (char *)buf;
	unsigned int num;

	num = simple_strtoul(p, &p, 10);

	if (mt_eint_drv.eint_max_channel) {
		if (num >= mt_eint_drv.eint_max_channel()) {
			pr_err("Invalid EINT number %d from user space", num);
		} else {
			cur_eint_num = num;
		}
	} else {
		pr_err("mt_eint_drv.eint_max_channel is NULL");
	}

	return count;
}

DRIVER_ATTR(current_eint, 0664, cur_eint_show, cur_eint_store);

static ssize_t cur_eint_sens_show(struct device_driver *driver, char *buf)
{
	if (mt_eint_drv.get_sens) {
		return snprintf(buf, PAGE_SIZE, "%d\n", mt_eint_drv.get_sens(cur_eint_num));
	} else {
		pr_err("mt_eint_drv.get_sens is NULL");
		return snprintf(buf, PAGE_SIZE, "ERROR");
	}
}

static ssize_t cur_eint_sens_store(struct device_driver *driver, const char *buf, size_t count)
{
	char *p = (char *)buf;
	int sens;

	sens = simple_strtoul(p, &p, 10);
	if (sens != 1 && sens != 0) {
		pr_err("Invalid value: %d", sens);
		return count;
	}

	if (mt_eint_drv.set_sens && mt_eint_drv.is_disable
	    && mt_eint_drv.disable && mt_eint_drv.enable) {
		/*
		 * No lock is used here.
		 * It is assumed that other drivers won't use the same EINT at the same time.
		 * The user should note this assumption.
		 */
		if (mt_eint_drv.is_disable(cur_eint_num)) {
			mt_eint_drv.set_sens(cur_eint_num, sens);
		} else {
			mt_eint_drv.disable(cur_eint_num);
			mt_eint_drv.set_sens(cur_eint_num, sens);
			mt_eint_drv.enable(cur_eint_num);
		}
	} else {
		pr_err("Fail to set EINT sensitivity");
	}

	return count;
}

DRIVER_ATTR(current_eint_sens, 0644, cur_eint_sens_show, cur_eint_sens_store);

static ssize_t cur_eint_pol_show(struct device_driver *driver, char *buf)
{
	if (mt_eint_drv.get_polarity) {
		return snprintf(buf, PAGE_SIZE, "%d\n", mt_eint_drv.get_polarity(cur_eint_num));
	} else {
		pr_err("mt_eint_drv.get_polarity is NULL");
		return snprintf(buf, PAGE_SIZE, "ERROR");
	}
}

static ssize_t cur_eint_pol_store(struct device_driver *driver, const char *buf, size_t count)
{
	char *p = (char *)buf;
	int pol;

	pol = simple_strtoul(p, &p, 10);
	if (pol != 1 && pol != 0) {
		pr_err("Invalid value: %d\n", pol);
		return count;
	}

	if (mt_eint_drv.set_polarity && mt_eint_drv.is_disable
	    && mt_eint_drv.disable && mt_eint_drv.enable) {
		/*
		 * No lock is used here.
		 * It is assumed that other drivers won't use the same EINT at the same time.
		 * The user should note this assumption.
		 */
		if (mt_eint_drv.is_disable(cur_eint_num)) {
			mt_eint_drv.set_polarity(cur_eint_num, pol);
		} else {
			mt_eint_drv.disable(cur_eint_num);
			mt_eint_drv.set_polarity(cur_eint_num, pol);
			mt_eint_drv.enable(cur_eint_num);
		}
	} else {
		pr_err("Fail to set EINT polarity");
	}

	return count;
}

DRIVER_ATTR(current_eint_pol, 0644, cur_eint_pol_show, cur_eint_pol_store);

static ssize_t cur_eint_deb_show(struct device_driver *driver, char *buf)
{
	if (mt_eint_drv.get_debounce_cnt) {
		return snprintf(buf, PAGE_SIZE, "%d\n", mt_eint_drv.get_debounce_cnt(cur_eint_num));
	} else {
		pr_err("mt_eint_drv.get_debounce_cnt is NULL");
		return snprintf(buf, PAGE_SIZE, "ERROR");
	}
}

static ssize_t cur_eint_deb_store(struct device_driver *driver, const char *buf, size_t count)
{
	char *p = (char *)buf;
	unsigned int cnt;

	cnt = simple_strtoul(p, &p, 10);

	if (mt_eint_drv.set_debounce_cnt && mt_eint_drv.is_disable
	    && mt_eint_drv.disable && mt_eint_drv.enable) {
		/*
		 * No lock is used here.
		 * It is assumed that other drivers won't use the same EINT at the same time.
		 * The user should note this assumption.
		 */
		if (mt_eint_drv.is_disable(cur_eint_num)) {
			mt_eint_drv.set_debounce_cnt(cur_eint_num, cnt);
		} else {
			mt_eint_drv.disable(cur_eint_num);
			mt_eint_drv.set_debounce_cnt(cur_eint_num, cnt);
			mt_eint_drv.enable(cur_eint_num);
		}
	} else {
		pr_err("Fail to set EINT debounce time");
	}

	return count;
}

DRIVER_ATTR(current_eint_deb, 0644, cur_eint_deb_show, cur_eint_deb_store);

static ssize_t cur_eint_deb_en_show(struct device_driver *driver, char *buf)
{
	if (mt_eint_drv.is_debounce_en) {
		return snprintf(buf, PAGE_SIZE, "%d\n", mt_eint_drv.is_debounce_en(cur_eint_num));
	} else {
		pr_err("mt_eint_drv.is_debounce_en is NULL");
		return snprintf(buf, PAGE_SIZE, "ERROR");
	}
}

static ssize_t cur_eint_deb_en_store(struct device_driver *driver, const char *buf, size_t count)
{
	char *p = (char *)buf;
	unsigned int enable;

	enable = simple_strtoul(p, &p, 10);

	if (enable != 1 && enable != 0) {
		pr_err("Invalid value: %d", enable);
		return count;
	}

	if (mt_eint_drv.enable_debounce && mt_eint_drv.disable_debounce
	    && mt_eint_drv.is_disable && mt_eint_drv.disable && mt_eint_drv.enable) {
		/*
		 * No lock is used here.
		 * It is assumed that other drivers won't use the same EINT at the same time.
		 * The user should note this assumption.
		 */
		if (mt_eint_drv.is_disable(cur_eint_num)) {
			if (enable) {
				mt_eint_drv.enable_debounce(cur_eint_num);
			} else {
				mt_eint_drv.disable_debounce(cur_eint_num);
			}
		} else {
			mt_eint_drv.disable(cur_eint_num);
			if (enable) {
				mt_eint_drv.enable_debounce(cur_eint_num);
			} else {
				mt_eint_drv.disable_debounce(cur_eint_num);
			}
			mt_eint_drv.enable(cur_eint_num);
		}
	} else {
		pr_err("Fail to set EINT debounce");
	}

	return count;
}

DRIVER_ATTR(current_eint_deb_en, 0644, cur_eint_deb_en_show, cur_eint_deb_en_store);

#if defined(EINT_DRV_TEST)
static ssize_t current_eint_enable_show(struct device_driver *driver, char *buf)
{
	if (mt_eint_drv.is_disable) {
		return snprintf(buf, PAGE_SIZE, "%s",
				mt_eint_drv.is_disable(cur_eint_num) ? "disable" : "enable");
	} else {
		pr_err("mt_eint_drv.is_disable is NULL");
		return snprintf(buf, PAGE_SIZE, "ERROR");
	}
}

static ssize_t current_eint_enable_store(struct device_driver *driver,
					 const char *buf, size_t count)
{
	if (mt_eint_drv.enable) {
		mt_eint_drv.enable(cur_eint_num);
		if (mt_eint_drv.is_disable(cur_eint_num)) {
			pr_crit("EINT %d should be enabled but it is still disabled", cur_eint_num);
		}
	} else {
		pr_err("mt_eint_drv.enable is NULL");
	}

	return count;
}

DRIVER_ATTR(current_eint_enable, 0644, current_eint_enable_show, current_eint_enable_store);

static ssize_t current_eint_disable_show(struct device_driver *driver, char *buf)
{
	if (mt_eint_drv.is_disable) {
		return snprintf(buf, PAGE_SIZE, "%s",
				mt_eint_drv.is_disable(cur_eint_num) ? "disable" : "enable");
	} else {
		pr_err("mt_eint_drv.is_disable is NULL");
		return snprintf(buf, PAGE_SIZE, "ERROR");
	}
}

static ssize_t current_eint_disable_store(struct device_driver *driver,
					  const char *buf, size_t count)
{
	if (mt_eint_drv.disable) {
		mt_eint_drv.disable(cur_eint_num);
	} else {
		pr_err("mt_eint_drv.enable is NULL");
	}

	return count;
}

DRIVER_ATTR(current_eint_disable, 0644, current_eint_disable_show, current_eint_disable_store);
#endif

static int __init eint_drv_init(void)
{
	int ret;

	ret = driver_register(&mt_eint_drv.driver.driver);
	if (ret) {
		pr_err("Fail to register mt_eint_drv");
	}

	ret = driver_create_file(&mt_eint_drv.driver.driver, &driver_attr_current_eint);
	ret |= driver_create_file(&mt_eint_drv.driver.driver, &driver_attr_current_eint_sens);
	ret |= driver_create_file(&mt_eint_drv.driver.driver, &driver_attr_current_eint_pol);
	ret |= driver_create_file(&mt_eint_drv.driver.driver, &driver_attr_current_eint_deb);
	ret |= driver_create_file(&mt_eint_drv.driver.driver, &driver_attr_current_eint_deb_en);
	if (ret) {
		pr_err("Fail to create mt_eint_drv sysfs files");
	}
#if defined(EINT_DRV_TEST)
	ret = driver_create_file(&mt_eint_drv.driver.driver, &driver_attr_current_eint_enable);
	ret |= driver_create_file(&mt_eint_drv.driver.driver, &driver_attr_current_eint_disable);
	if (ret) {
		pr_err("Fail to create EINT sysfs files");
	}
#endif

	return 0;
}
arch_initcall(eint_drv_init);