summaryrefslogtreecommitdiff
path: root/drivers/fingerprint/fpc1020_ree.c
blob: 166040a550f2e03e1b8d879ec0090e01a7994e0f (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * ZUK FPC1150 REE driver
 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * 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.
 */
#define pr_fmt(fmt) "ZUK-FPC: %s: " fmt, __func__
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/clk.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/fb.h>
#include <linux/mutex.h>
#include <linux/types.h>
#include <linux/platform_device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/io.h>
#include <linux/of_gpio.h>
#include <linux/input.h>

#define FPC1020_TOUCH_DEV_NAME  "fpc1020tp"

#define FPC1020_RESET_LOW_US 1000
#define FPC1020_RESET_HIGH1_US 100
#define FPC1020_RESET_HIGH2_US 1250
#define FPC_TTW_HOLD_TIME 1500

struct fpc1020_data {
	struct device   *dev;
	struct pinctrl  *pin;
	wait_queue_head_t wq_irq_return;
	/*Set pins*/
	int reset_gpio;
	int irq_gpio;
	int irq;
	bool irq_enabled;
	int wakeup_enabled;
	struct notifier_block fb_notif;
	/*Input device*/
	struct input_dev *input_dev;
	struct work_struct pm_work;
	struct work_struct input_report_work;
	struct workqueue_struct *fpc1020_wq;
	u8  report_key;
	int screen_on;
	int proximity_state; /* 0:far 1:near */
};

static void config_irq(struct fpc1020_data *fpc1020, bool enabled)
{
	if (enabled != fpc1020->irq_enabled) {
		if (enabled)
			enable_irq(gpio_to_irq(fpc1020->irq_gpio));
		else
			disable_irq(gpio_to_irq(fpc1020->irq_gpio));

		dev_info(fpc1020->dev, "%s: %s fpc irq ---\n", __func__,
			enabled ?  "enable" : "disable");
		fpc1020->irq_enabled = enabled;
	} else {
		dev_info(fpc1020->dev, "%s: dual config irq status: %s\n", __func__,
			enabled ?  "true" : "false");
	}
}

/* From drivers/input/keyboard/gpio_keys.c */
extern bool home_button_pressed(void);
extern void reset_home_button(void);

bool reset;

static bool utouch_disable;

static int fb_notifier_callback(struct notifier_block *self,
		unsigned long event, void *data);

static ssize_t irq_get(struct device *device,
		struct device_attribute *attribute,
		char *buffer)
{
	struct fpc1020_data *fpc1020 = dev_get_drvdata(device);
	int irq = gpio_get_value(fpc1020->irq_gpio);

	return scnprintf(buffer, PAGE_SIZE, "%i\n", irq);
}

static ssize_t irq_set(struct device *device,
		struct device_attribute *attribute,
		const char *buffer, size_t count)
{
	int retval = 0;
	u64 val;
	struct fpc1020_data *fpc1020 = dev_get_drvdata(device);

	retval = kstrtou64(buffer, 0, &val);
	if (val)
		enable_irq(fpc1020->irq);
	else if (!val)
		disable_irq(fpc1020->irq);
	else
		return -ENOENT;
	return strnlen(buffer, count);
}

static DEVICE_ATTR(irq, S_IRUSR | S_IWUSR, irq_get, irq_set);

static ssize_t get_key(struct device *device,
		struct device_attribute *attribute, char *buffer)
{
	struct fpc1020_data *fpc1020 = dev_get_drvdata(device);

	return scnprintf(buffer, PAGE_SIZE, "%i\n", fpc1020->report_key);
}

static ssize_t set_key(struct device *device,
		struct device_attribute *attribute,
		const char *buffer, size_t count)
{
	int retval = 0;
	u64 val;
	struct fpc1020_data *fpc1020 = dev_get_drvdata(device);
	bool home_pressed;

	retval = kstrtou64(buffer, 0, &val);
	if (!retval && !utouch_disable) {
		if (val == KEY_HOME)
			/* Convert to U-touch long press keyValue */
			val = KEY_NAVI_LONG;

		home_pressed = home_button_pressed();

		if (val && home_pressed)
			val = 0;

		pr_info("home key pressed = %d\n", (int)home_pressed);
		fpc1020->report_key = (int)val;
		queue_work(fpc1020->fpc1020_wq, &fpc1020->input_report_work);

		if (!val) {
			pr_info("calling home key reset");
			reset_home_button();
		}
	} else if (retval)
		return -ENOENT;
	return strnlen(buffer, count);
}

static DEVICE_ATTR(key, S_IRUSR | S_IWUSR, get_key, set_key);

static ssize_t utouch_store_disable(struct device *dev, 
		struct device_attribute *attr, const char *buf, size_t count)
{
    int value;
 	if (1 != sscanf(buf, "%d", &value)) {
		dev_err(dev, "Failed to parse integer: <%s>\n", buf);
		return -EINVAL;
	}
 	if (value == 1) {
		utouch_disable = true;
		pr_info("utouch disabled\n");
	} else {
		utouch_disable = false;
		pr_info("utouch enabled\n");
	}
 	return count;
}

static ssize_t utouch_show_disable(struct device *dev, 
		struct device_attribute *attr, char *buf)
{
	if (utouch_disable)
		return sprintf(buf, "1\n"); 
	else
		return sprintf(buf, "0\n"); 
}
static DEVICE_ATTR(utouch_disable, S_IRUGO|S_IWUSR, utouch_show_disable, utouch_store_disable);

static ssize_t enable_wakeup_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct fpc1020_data *fpc1020 = dev_get_drvdata(dev);
	char c;

	c = fpc1020->wakeup_enabled ? '1' : '0';
	return scnprintf(buf, PAGE_SIZE, "%c\n", c);
}

static ssize_t enable_wakeup_store(struct device *dev,
			struct device_attribute *attr, const char *buf, size_t count)
{
	struct fpc1020_data *fpc1020 = dev_get_drvdata(dev);
	int i;

	if (sscanf(buf, "%u", &i) == 1 && i < 2) {
		fpc1020->wakeup_enabled = (i == 1);

		dev_info(dev, "%s\n", i ? "wakeup enabled" : "wakeup disabled");
		return count;
	} else {
		dev_info(dev, "%s: wakeup_enabled write error\n", __func__);
		return -EINVAL;
	}
}
static DEVICE_ATTR(enable_wakeup, S_IWUSR | S_IRUSR, enable_wakeup_show,
			enable_wakeup_store);


static ssize_t proximity_state_set(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct fpc1020_data *fpc1020 = dev_get_drvdata(dev);
	int rc, val;
	rc = kstrtoint(buf, 10, &val);
	if (rc)
		return -EINVAL;
	fpc1020->proximity_state = !!val;
	if (!fpc1020->screen_on) {
		if (fpc1020->proximity_state) {
			/* Disable IRQ when screen is off and proximity sensor is covered */
			config_irq(fpc1020, false);
		} else if (fpc1020->wakeup_enabled) {
			/* Enable IRQ when screen is off and proximity sensor is uncovered,
			 but only if fingerprint wake up is enabled */
			 config_irq(fpc1020, true);
		}
	}
	return count;
}
static DEVICE_ATTR(proximity_state, S_IWUSR, NULL, proximity_state_set);

static struct attribute *attributes[] = {
	&dev_attr_irq.attr,
	&dev_attr_key.attr,
	&dev_attr_enable_wakeup.attr,
	&dev_attr_proximity_state.attr,
	&dev_attr_utouch_disable.attr,
	NULL
};

static const struct attribute_group attribute_group = {
	.attrs = attributes,
};

static void fpc1020_report_work_func(struct work_struct *work)
{
	struct fpc1020_data *fpc1020 = NULL;

	fpc1020 = container_of(work, struct fpc1020_data, input_report_work);
	if (fpc1020->screen_on) {
		pr_info("Report key value = %d\n", (int)fpc1020->report_key);
		input_report_key(fpc1020->input_dev, fpc1020->report_key, 1);
		input_sync(fpc1020->input_dev);
		msleep(30);
		input_report_key(fpc1020->input_dev, fpc1020->report_key, 0);
		input_sync(fpc1020->input_dev);
		fpc1020->report_key = 0;
	}
}

static void fpc1020_hw_reset(struct fpc1020_data *fpc1020)
{
	pr_info("HW reset\n");
	gpio_set_value(fpc1020->reset_gpio, 1);
	udelay(FPC1020_RESET_HIGH1_US);

	gpio_set_value(fpc1020->reset_gpio, 0);
	udelay(FPC1020_RESET_LOW_US);

	gpio_set_value(fpc1020->reset_gpio, 1);
	udelay(FPC1020_RESET_HIGH2_US);
}

static int fpc1020_get_pins(struct fpc1020_data *fpc1020)
{
	int retval = 0;
	struct device_node *np = fpc1020->dev->of_node;

	fpc1020->irq_gpio = of_get_named_gpio(np, "fpc,gpio_irq", 0);
	if (!gpio_is_valid(fpc1020->irq_gpio)) {
		pr_err("IRQ request failed.\n");
		goto err;
	}
	fpc1020->reset_gpio = of_get_named_gpio(np, "fpc,gpio_reset", 0);
	if (!gpio_is_valid(fpc1020->reset_gpio)) {
		pr_err("RESET pin request failed\n");
		goto err;
	}

	fpc1020->pin = pinctrl_get_select_default(fpc1020->dev);
	if (IS_ERR_OR_NULL(fpc1020->pin)) {
		pr_err("pinctrl get failed.\n");
		goto err;
	}

	return 0;
err:
	pr_err("%s, err\n", __func__);
	fpc1020->irq = -EINVAL;
	fpc1020->irq_gpio = fpc1020->reset_gpio = -EINVAL;
	retval = -ENODEV;
	return retval;
}

static irqreturn_t fpc1020_irq_handler(int irq, void *_fpc1020)
{
	struct fpc1020_data *fpc1020 = _fpc1020;

	pr_info("fpc1020 IRQ interrupt\n");
	/* Make sure 'wakeup_enabled' is updated before using it
	 ** since this is interrupt context (other thread...) */
	smp_rmb();
	if (fpc1020->wakeup_enabled && !fpc1020->screen_on) {
		pm_wakeup_event(fpc1020->dev, 5000);
	}
	sysfs_notify(&fpc1020->dev->kobj, NULL, dev_attr_irq.attr.name);
	return IRQ_HANDLED;
}

static int fpc1020_initial_irq(struct fpc1020_data *fpc1020)
{
	int retval = 0;
	int irqf;
	if (!gpio_is_valid(fpc1020->irq_gpio)) {
		pr_err("IRQ pin(%d) is not valid\n", fpc1020->irq_gpio);
		return -EINVAL;
	}

	retval = gpio_request(fpc1020->irq_gpio, "fpc_irq");
	if (retval) {
		pr_err("IRQ(%d) request failed\n", fpc1020->irq_gpio);
		return -EINVAL;
	}

	retval = gpio_direction_input(fpc1020->irq_gpio);
	if (retval) {
		pr_err("Set input(%d) failed\n", fpc1020->irq_gpio);
		return -EINVAL;
	}

	fpc1020->irq = gpio_to_irq(fpc1020->irq_gpio);
	if (fpc1020->irq < 0) {
		pr_err("gpio_to_irq(%d) failed\n", fpc1020->irq_gpio);
		return -EINVAL;
	}

	irqf = IRQF_TRIGGER_RISING | IRQF_ONESHOT;
	if (of_property_read_bool(fpc1020->dev->of_node, "fpc,enable-wakeup")) {
		irqf |= IRQF_NO_SUSPEND;
		device_init_wakeup(fpc1020->dev, 1);
		fpc1020->wakeup_enabled = 1;
	}

	retval = devm_request_threaded_irq(fpc1020->dev,
			fpc1020->irq, NULL, fpc1020_irq_handler, irqf,
			dev_name(fpc1020->dev), fpc1020);

	if (retval) {
		pr_err("request irq %i failed.\n", fpc1020->irq);
		fpc1020->irq = -EINVAL;
		return -EINVAL;
	}

	dev_info(fpc1020->dev, "requested irq %d\n", fpc1020->irq);
	/* Request that the interrupt should be wakeable*/
	if (fpc1020->wakeup_enabled) {
		enable_irq_wake(fpc1020->irq);
	}
	fpc1020->irq_enabled = true;

	return 0;
}

static int fpc1020_manage_sysfs(struct fpc1020_data *fpc1020)
{
	int retval = 0;

	retval = sysfs_create_group(&fpc1020->dev->kobj, &attribute_group);
	if (retval) {
		pr_err("Could not create sysfs\n");
		return -EINVAL;
	}
	return 0;
}

static int fpc1020_alloc_input_dev(struct fpc1020_data *fpc1020)
{
	int retval = 0;

	fpc1020->input_dev = input_allocate_device();
	if (!fpc1020->input_dev) {
		pr_info("Input allocate device failed\n");
		retval = -ENOMEM;
		return retval;
	}

	fpc1020->input_dev->name = "fpc1020tp";
	set_bit(EV_KEY, fpc1020->input_dev->evbit);
	set_bit(KEY_BACK, fpc1020->input_dev->keybit);
	set_bit(KEY_LEFT, fpc1020->input_dev->keybit);
	set_bit(KEY_RIGHT, fpc1020->input_dev->keybit);
	set_bit(KEY_NAVI_LONG, fpc1020->input_dev->keybit);
	input_set_capability(fpc1020->input_dev, EV_KEY, KEY_NAVI_LEFT);
	input_set_capability(fpc1020->input_dev, EV_KEY, KEY_NAVI_RIGHT);
	input_set_capability(fpc1020->input_dev, EV_KEY, KEY_BACK);
	input_set_capability(fpc1020->input_dev, EV_KEY, KEY_NAVI_LONG);

	/* Register the input device */
	retval = input_register_device(fpc1020->input_dev);
	if (retval) {
		pr_err("Input_register_device failed.\n");
		input_free_device(fpc1020->input_dev);
		fpc1020->input_dev = NULL;
	}
	return retval;
}

static void set_fingerprintd_nice(int nice)
{
	struct task_struct *p;

	read_lock(&tasklist_lock);
	for_each_process(p) {
		if (!memcmp(p->comm, "fingerprint@2.1", 16)) {
			pr_debug("fingerprint nice changed to %i\n", nice);
			set_user_nice(p, nice);
			break;
		}
	}
	read_unlock(&tasklist_lock);
}

static void fpc1020_suspend_resume(struct work_struct *work)
{
	struct fpc1020_data *fpc1020 =
		container_of(work, typeof(*fpc1020), pm_work);

	/* Escalate fingerprintd priority when screen is off */
	if (!fpc1020->screen_on)
		set_fingerprintd_nice(MIN_NICE);
	else
		set_fingerprintd_nice(0);
}

static int fb_notifier_callback(struct notifier_block *self,
		unsigned long event, void *data)
{
	int *blank;
	struct fb_event *evdata = data;

	struct fpc1020_data *fpc1020 = container_of(self, struct fpc1020_data, fb_notif);
	blank = evdata->data;
	if (evdata && evdata->data && event == FB_EVENT_BLANK && fpc1020) {
		blank = evdata->data;
		if (*blank == FB_BLANK_UNBLANK) {
			pr_debug("ScreenOn\n");
			fpc1020->screen_on = 1;
			queue_work(fpc1020->fpc1020_wq, &fpc1020->pm_work);
			/* Unconditionally enable IRQ when screen turns on */
			config_irq(fpc1020, true);
		} else if (*blank == FB_BLANK_POWERDOWN) {
			pr_debug("ScreenOff\n");
			fpc1020->screen_on = 0;
			if (!fpc1020->wakeup_enabled)
				config_irq(fpc1020, false);
			queue_work(fpc1020->fpc1020_wq, &fpc1020->pm_work);
		}
	}
	return 0;
}

static int fpc1020_probe(struct platform_device *pdev)
{
	int retval = 0;
	struct device *dev = &pdev->dev;

	struct fpc1020_data *fpc1020 = devm_kzalloc(dev,
			sizeof(struct fpc1020_data), GFP_KERNEL);
	if (fpc1020 == NULL) {
		pr_err("fpc1020 allocation error\n");
		retval = -ENOMEM;
		goto error;
	}

	fpc1020->dev = &pdev->dev;
	dev_set_drvdata(dev, fpc1020);

	retval = fpc1020_get_pins(fpc1020);
	if (retval != 0) {
		pr_err("Get pins failed\n");
		goto error;
	}

	fpc1020->wakeup_enabled = 0;

	/*create sfs nodes*/
	retval = fpc1020_manage_sysfs(fpc1020);
	if (retval != 0) {
		pr_err("Create sysfs nodes failed\n");
		goto error;
	}

	/*create input device for navigation*/
	retval = fpc1020_alloc_input_dev(fpc1020);
	if (retval != 0) {
		pr_err("Allocate input device failed\n");
		goto error_remove_sysfs;
	}

	fpc1020->fpc1020_wq = alloc_workqueue("fpc1020_wq", WQ_HIGHPRI, 1);
	if (!fpc1020->fpc1020_wq) {
		pr_err("Create input workqueue failed\n");
		goto error_unregister_device;
	}
	INIT_WORK(&fpc1020->input_report_work, fpc1020_report_work_func);
	INIT_WORK(&fpc1020->pm_work, fpc1020_suspend_resume);
	gpio_direction_output(fpc1020->reset_gpio, 1);
	/*Do HW reset*/
	fpc1020_hw_reset(fpc1020);

	fpc1020->fb_notif.notifier_call = fb_notifier_callback;
	retval = fb_register_client(&fpc1020->fb_notif);
	if (retval) {
		pr_err("Unable to register fb_notifier : %d\n", retval);
		goto error_destroy_workqueue;
	}

	device_init_wakeup(dev, true);

	retval = fpc1020_initial_irq(fpc1020);
	if (retval != 0) {
		pr_err("IRQ initialized failure\n");
		goto error_unregister_client;
	}

	/* Disable IRQ */
	disable_irq(fpc1020->irq);
	/* Enable irq wake */
	enable_irq_wake(fpc1020->irq);
	return 0;

error_unregister_client:
	fb_unregister_client(&fpc1020->fb_notif);

error_destroy_workqueue:
	destroy_workqueue(fpc1020->fpc1020_wq);

error_unregister_device:
	input_unregister_device(fpc1020->input_dev);

error_remove_sysfs:
	sysfs_remove_group(&fpc1020->dev->kobj, &attribute_group);

error:
	if (fpc1020 != NULL)
		kzfree(fpc1020);

	return retval;
}

static int fpc1020_remove(struct platform_device *pdev)
{
	int retval = 0;
	return retval;
}

static struct of_device_id fpc1020_match[] = {
	{
		.compatible = "fpc,fpc1020",
	},
	{}
};

static struct platform_driver fpc1020_plat_driver = {
	.probe = fpc1020_probe,
	.remove = fpc1020_remove,
	.driver = {
		.name = "fpc1020",
		.owner = THIS_MODULE,
		.of_match_table = fpc1020_match,
	},
};

static int fpc1020_init(void)
{
	return platform_driver_register(&fpc1020_plat_driver);
}

static void fpc1020_exit(void)
{
	platform_driver_unregister(&fpc1020_plat_driver);
}

module_init(fpc1020_init);
module_exit(fpc1020_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZUK ShenQi <lvxin1@zuk.com>");
MODULE_DESCRIPTION("FPC1020 fingerprint sensor ree driver.");