summaryrefstree
path: root/kernel/drivers/input/hall.c
blob: 2e0d5134f539be82b5a55788ed0f7421271bb643 (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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include <linux/module.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/gpio.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>
#include <linux/of.h>
#include <linux/spinlock.h>
#include <linux/wakelock.h>
#include <linux/hall.h>
#include <linux/notifier.h>


struct device *sec_device_create(void *drvdata, const char *fmt);

struct hall_drvdata {
	struct input_dev *input;
	int gpio_flip_cover;
	int irq_flip_cover;
	struct work_struct work;
	struct delayed_work flip_cover_dwork;
	struct wake_lock flip_wake_lock;
/* WorkAround for Hall IRQ Noise problem in connect to GSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
	struct mutex irq_lock;
	bool gsm_area;
	bool irq_state;
	bool cover_state;
#endif
#ifdef	CONFIG_SENSORS_HALL_FOLDER
	bool folder_type;
#endif
};

static bool flip_cover = 1;

/* WorkAround for Hall IRQ Noise problem in connect to GSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
struct hall_drvdata *g_drvdata;

#define enable_hall_irq() \
	do { \
		if (g_drvdata->irq_state == false) { \
			g_drvdata->irq_state = true; \
			enable_irq(g_drvdata->irq_flip_cover); \
			pr_info("%s():irq is enabled\n", __func__);\
		} else { \
			pr_info("%s():irq is already enabled\n",\
					__func__);\
		}\
	} while (0)

#define disable_hall_irq() \
	do { \
		if (g_drvdata->irq_state == true) { \
			g_drvdata->irq_state = false; \
			disable_irq(g_drvdata->irq_flip_cover); \
			pr_info("%s():irq is disabled\n", __func__);\
		} else { \
			pr_info("%s():irq is already disabled\n",\
					__func__);\
		}\
	} while (0)

void hall_irq_set(int state, bool auth_changed)
{
	if (auth_changed)
		g_drvdata->cover_state = state;

	pr_info("%s: gsm: %d, cover: %d, irq: %d, state: %d, auth: %d\n",
			__func__, g_drvdata->gsm_area, g_drvdata->cover_state,
			g_drvdata->irq_state, state, auth_changed);

	if (g_drvdata->gsm_area) {
		mutex_lock(&g_drvdata->irq_lock);

		if (state)
			enable_hall_irq();
		else
			disable_hall_irq();

		mutex_unlock(&g_drvdata->irq_lock);
	}
}

static ssize_t hall_irq_ctrl_store(struct device *dev,
		struct device_attribute *attr, const char *buf,
		size_t count)
{
	pr_info("%s: %s\n", __func__, buf);

	if (!strncasecmp(buf, "ON", 2)) {
		g_drvdata->gsm_area = true;
		if (!g_drvdata->cover_state)
			hall_irq_set(disable, false);
	} else if (!strncasecmp(buf, "OFF", 3)) {
		hall_irq_set(enable, false);
		g_drvdata->gsm_area = false;
	} else {
		pr_info("%s: Wrong command, current state %s\n",
			__func__, g_drvdata->gsm_area?"ON":"OFF");
	}

	return count;
}

static DEVICE_ATTR(hall_irq_ctrl, 0664, NULL, hall_irq_ctrl_store);
#endif

static ssize_t hall_detect_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	if (flip_cover) {
		sprintf(buf, "OPEN\n");
	} else {
		sprintf(buf, "CLOSE\n");
	}

	return strlen(buf);
}
static DEVICE_ATTR(hall_detect, 0664, hall_detect_show, NULL);

#if defined(CONFIG_SENSORS_HALL_FOLDER)
static ssize_t sysfs_flip_status_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	if (!flip_cover)
		snprintf(buf, 3, "0\n");
	else
		snprintf(buf, 3, "1\n");

	return strlen(buf);
}

static DEVICE_ATTR(flipStatus, 0444, sysfs_flip_status_show, NULL);
#endif

static struct attribute *hall_attrs[] = {
	&dev_attr_hall_detect.attr,
/* WorkAround for Hall IRQ Noise problem in connect to GSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
	&dev_attr_hall_irq_ctrl.attr,
#endif
	NULL,
};

static struct attribute_group hall_attr_group = {
	.attrs = hall_attrs,
};

#ifdef CONFIG_SENSORS_HALL_FOLDER
static BLOCKING_NOTIFIER_HEAD(hall_ic_notifier_list);

void hall_ic_register_notify(struct notifier_block *nb) {
	int ret;

	ret = blocking_notifier_chain_register(&hall_ic_notifier_list, nb);//need return
	printk("[notifier hall]hall ic %s,%p,%p  : %d*** \n",
		__func__,&nb,&nb->notifier_call, ret);
}
EXPORT_SYMBOL(hall_ic_register_notify);
#endif

#ifdef CONFIG_FOLDER_DUAL_PANEL
extern void decon_display_switch_by_hall(int hall_ic_state);
#endif

#ifdef CONFIG_SEC_FACTORY
static void flip_cover_work(struct work_struct *work)
{
	bool first,second;
	struct hall_drvdata *ddata =
		container_of(work, struct hall_drvdata,
				flip_cover_dwork.work);

	first = gpio_get_value(ddata->gpio_flip_cover);

	printk("[keys] %s flip_status #1 : %d (%s)\n", __func__, first, first?"open":"close");

	msleep(50);

	second = gpio_get_value(ddata->gpio_flip_cover);

	printk("[keys] %s flip_status #2 : %d (%s)\n", __func__, second, second?"open":"close");

	if(first == second) {
		flip_cover = first;

#ifdef CONFIG_FOLDER_DUAL_PANEL
		/* For display switching */
		if (ddata->folder_type)
			decon_display_switch_by_hall(!flip_cover);
		else
			decon_display_switch_by_hall(flip_cover);
#endif

#ifdef CONFIG_SENSORS_HALL_FOLDER
		if (ddata->folder_type) {
			/* foder open : 0, close : 1 */
			blocking_notifier_call_chain(&hall_ic_notifier_list, !flip_cover, NULL);
			input_report_switch(ddata->input, SW_LID, !flip_cover);
		} else {  /* flip open : 1, close : 0 */
			blocking_notifier_call_chain(&hall_ic_notifier_list, flip_cover, NULL);
			input_report_switch(ddata->input, SW_FLIP, flip_cover);
		}
#else
		input_report_switch(ddata->input, SW_FLIP, flip_cover);
#endif

		input_sync(ddata->input);
	}
}
#else
#ifdef CONFIG_HALL_LOOPCHECK_WA
static void hall_close(struct input_dev *input);
static void flip_cover_work(struct work_struct *work)
{
#define HALL_COMPARISONS 6
        struct hall_drvdata *ddata =
                container_of(work, struct hall_drvdata,
                                flip_cover_dwork.work);
	bool comp_val[HALL_COMPARISONS]={0};
	int i;

        comp_val[0] = gpio_get_value(ddata->gpio_flip_cover);

	printk("[keys] %s flip_status : %d (%s)\n",
		__func__, comp_val[0], comp_val[0]?"open":"close");

	for(i=1;i<HALL_COMPARISONS;i++){
		mdelay(6);
		comp_val[i] = gpio_get_value(ddata->gpio_flip_cover);
		if(comp_val[i]!=comp_val[0]){
			pr_err("%s : Value is not same!\n", __func__);
			goto out; 
		}
	}

	flip_cover = comp_val[0];
	printk("[keys] hall ic reported value: %d (%s)\n",
		flip_cover, flip_cover?"open":"close");

#ifdef CONFIG_FOLDER_DUAL_PANEL
	/* For display switching */
	if (ddata->folder_type)
		decon_display_switch_by_hall(!flip_cover);
	else
		decon_display_switch_by_hall(flip_cover);
#endif

#ifdef CONFIG_SENSORS_HALL_FOLDER
	if (ddata->folder_type) {
		/* foder open : 0, close : 1 */
		blocking_notifier_call_chain(&hall_ic_notifier_list, !flip_cover, NULL);
		input_report_switch(ddata->input, SW_LID, !flip_cover);
	} else {  /* flip open : 1, close : 0 */
		blocking_notifier_call_chain(&hall_ic_notifier_list, flip_cover, NULL);
		input_report_switch(ddata->input, SW_FLIP, flip_cover);
	}
#else
	input_report_switch(ddata->input, SW_FLIP, flip_cover);
#endif
	input_sync(ddata->input);
out:
	hall_close(ddata->input);
} /* Noise Problem - Hall defence WA code */
#else
static void flip_cover_work(struct work_struct *work)
{
	bool first;
/* WorkAround for Hall IRQ Noise problem in connect to GGSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
	bool second;
#endif
	struct hall_drvdata *ddata =
		container_of(work, struct hall_drvdata,
				flip_cover_dwork.work);

	first = gpio_get_value(ddata->gpio_flip_cover);

	printk("[keys] %s flip_status : %d (%s)\n", __func__, first, first?"open":"close");

/* WorkAround for Hall IRQ Noise problem in connect to GGSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
	if (g_drvdata->gsm_area) {
		pr_info("%s: NDT\n", __func__);
		mdelay(10);
		second = gpio_get_value(ddata->gpio_flip_cover);
		if (first != second) {
			pr_info("%s: NDT, not stable value\n", __func__);
			return;
		}
	}
#endif

	flip_cover = first;
	input_report_switch(ddata->input,
			SW_FLIP, flip_cover);
	input_sync(ddata->input);
}
#endif
#endif

static void __flip_cover_detect(struct hall_drvdata *ddata, bool flip_status)
{
	cancel_delayed_work_sync(&ddata->flip_cover_dwork);
#ifdef CONFIG_SEC_FACTORY
	schedule_delayed_work(&ddata->flip_cover_dwork, HZ / 20);
#else
	if(flip_status)	{
		wake_lock_timeout(&ddata->flip_wake_lock, HZ * 5 / 100); /* 50ms */
		schedule_delayed_work(&ddata->flip_cover_dwork, HZ * 1 / 100); /* 10ms */
	} else {
		wake_unlock(&ddata->flip_wake_lock);
		schedule_delayed_work(&ddata->flip_cover_dwork, 0);
	}
#endif
}

static irqreturn_t flip_cover_detect(int irq, void *dev_id)
{
	bool flip_status;
	struct hall_drvdata *ddata = dev_id;

	flip_status = gpio_get_value(ddata->gpio_flip_cover);

	printk(KERN_DEBUG "keys:%s flip_status : %d\n",
		 __func__, flip_status);

	__flip_cover_detect(ddata, flip_status);

	return IRQ_HANDLED;
}

static int hall_open(struct input_dev *input)
{
	struct hall_drvdata *ddata = input_get_drvdata(input);
	/* update the current status */
	schedule_delayed_work(&ddata->flip_cover_dwork, HZ / 2);
	/* Report current state of buttons that are connected to GPIOs */
	input_sync(input);

	return 0;
}

static void hall_close(struct input_dev *input)
{
}


static void init_hall_ic_irq(struct input_dev *input)
{
	struct hall_drvdata *ddata = input_get_drvdata(input);

	int ret = 0;
	int irq = ddata->irq_flip_cover;

	flip_cover = gpio_get_value(ddata->gpio_flip_cover);

	INIT_DELAYED_WORK(&ddata->flip_cover_dwork, flip_cover_work);

	ret =
		request_threaded_irq(
		irq, NULL,
		flip_cover_detect,
		IRQF_DISABLED | IRQF_TRIGGER_RISING |
		IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
		"flip_cover", ddata);
	if (ret < 0) {
		printk(KERN_ERR
		"keys: failed to request flip cover irq %d gpio %d\n",
		irq, ddata->gpio_flip_cover);
	} else {
#ifdef CONFIG_FOLDER_DUAL_PANEL
		/* update the current status when use double panel*/
		schedule_delayed_work(&ddata->flip_cover_dwork, HZ / 2);
#endif
		pr_info("%s : success\n", __func__);
/* WorkAround for Hall IRQ Noise problem in connect to GSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
		g_drvdata->irq_state = true;
#endif
	}
}

#ifdef CONFIG_OF
static int of_hall_data_parsing_dt(struct hall_drvdata *ddata)
{
	struct device_node *np_haptic;
	int gpio;
	enum of_gpio_flags flags;

	np_haptic = of_find_node_by_path("/hall");
	if (np_haptic == NULL) {
		printk("%s : error to get dt node\n", __func__);
		return -EINVAL;
	}

	gpio = of_get_named_gpio_flags(np_haptic, "hall,gpio_flip_cover", 0, &flags);
	if (gpio < 0) {
		pr_info("%s: fail to get flip_cover \n", __func__ );
		return -EINVAL;
	}
	ddata->gpio_flip_cover = gpio;

	gpio = gpio_to_irq(gpio);
	if (gpio < 0) {
		pr_info("%s: fail to return irq corresponding gpio \n", __func__ );
		return -EINVAL;
	}
	ddata->irq_flip_cover = gpio;

#ifdef CONFIG_SENSORS_HALL_FOLDER
	ddata->folder_type = of_property_read_bool(np_haptic, "hall,folder_type");
	pr_info("[keys] phone type : (%s)\n", ddata->folder_type ? "Folder" : "Bar");
#endif

	return 0;
}
#endif

static int hall_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct hall_drvdata *ddata;
	struct input_dev *input;
	int error;
	int wakeup = 0;

#if defined(CONFIG_SENSORS_HALL_FOLDER)
	struct device *sec_flip;
#endif

	ddata = kzalloc(sizeof(struct hall_drvdata), GFP_KERNEL);
	if (!ddata) {
		dev_err(dev, "failed to allocate state\n");
		return -ENOMEM;
	}

#ifdef CONFIG_OF
	if(dev->of_node) {
		error = of_hall_data_parsing_dt(ddata);
		if (error < 0) {
			pr_info("%s : fail to get the dt (HALL)\n", __func__);
			goto fail1;
		}
	}
#endif

	input = input_allocate_device();
	if (!input) {
		dev_err(dev, "failed to allocate state\n");
		error = -ENOMEM;
		goto fail1;
	}

	ddata->input = input;

	wake_lock_init(&ddata->flip_wake_lock, WAKE_LOCK_SUSPEND,
		"flip wake lock");

	platform_set_drvdata(pdev, ddata);
	input_set_drvdata(input, ddata);

	input->name = "hall";
	input->phys = "hall";
	input->dev.parent = &pdev->dev;

	input->evbit[0] |= BIT_MASK(EV_SW);
#ifdef CONFIG_SENSORS_HALL_FOLDER
	if (ddata->folder_type)
		input_set_capability(input, EV_SW, SW_LID);
	else
		input_set_capability(input, EV_SW, SW_FLIP);
#else
	input_set_capability(input, EV_SW, SW_FLIP);
#endif

	input->open = hall_open;
	input->close = hall_close;

	/* Enable auto repeat feature of Linux input subsystem */
	__set_bit(EV_REP, input->evbit);

/* WorkAround for Hall IRQ Noise problem in connect to GSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
	mutex_init(&ddata->irq_lock);

	ddata->gsm_area = false;
	ddata->cover_state = false;
	g_drvdata = ddata;
#endif

	init_hall_ic_irq(input);

	error = sysfs_create_group(&sec_key->kobj, &hall_attr_group);
	if (error) {
		dev_err(dev, "Unable to export keys/switches, error: %d\n",
			error);
		goto fail2;
	}

#if defined(CONFIG_SENSORS_HALL_FOLDER)
	sec_flip = sec_device_create(pdev, "sec_flip");

	if (device_create_file(sec_flip, &dev_attr_flipStatus) < 0)
		pr_err("Failed to create device file(%s) !\n", dev_attr_flipStatus.attr.name);
#endif

	error = input_register_device(input);
	if (error) {
		dev_err(dev, "Unable to register input device, error: %d\n",
			error);
		goto fail3;
	}

	device_init_wakeup(&pdev->dev, wakeup);

	return 0;

 fail3:
	sysfs_remove_group(&pdev->dev.kobj, &hall_attr_group);
 fail2:
	platform_set_drvdata(pdev, NULL);
	wake_lock_destroy(&ddata->flip_wake_lock);
	input_free_device(input);
 fail1:
	kfree(ddata);

	return error;
}

static int hall_remove(struct platform_device *pdev)
{
	struct hall_drvdata *ddata = platform_get_drvdata(pdev);
	struct input_dev *input = ddata->input;

	printk("%s start\n", __func__);
	sysfs_remove_group(&pdev->dev.kobj, &hall_attr_group);

	device_init_wakeup(&pdev->dev, 0);

	input_unregister_device(input);

	wake_lock_destroy(&ddata->flip_wake_lock);

	kfree(ddata);

	return 0;
}

#if defined(CONFIG_OF)
static struct of_device_id hall_dt_ids[] = {
	{ .compatible = "hall" },
	{ },
};
MODULE_DEVICE_TABLE(of, hall_dt_ids);
#endif /* CONFIG_OF */

#ifdef CONFIG_PM_SLEEP
static int hall_suspend(struct device *dev)
{
	struct hall_drvdata *ddata = dev_get_drvdata(dev);
	struct input_dev *input = ddata->input;
	bool status;

	printk("%s start\n", __func__);
	status = gpio_get_value(ddata->gpio_flip_cover);
	printk("[keys] %s flip_status : %d (%s)\n", __func__, status, status?"open":"close");

/* need to be change */
/* Without below one line, it is not able to get the irq during freezing */

/* WorkAround for Hall IRQ Noise problem in connect to GGSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
	/* gsm_area can be controlled only in hall_irq_set */
	if (!g_drvdata->cover_state && g_drvdata->gsm_area)
		disable_irq_wake(ddata->irq_flip_cover);
	else
		enable_irq_wake(ddata->irq_flip_cover);
#else
	enable_irq_wake(ddata->irq_flip_cover);
#endif

	if (device_may_wakeup(dev)) {
		enable_irq_wake(ddata->irq_flip_cover);
	} else {
		mutex_lock(&input->mutex);
		if (input->users)
			hall_close(input);
		mutex_unlock(&input->mutex);
	}

	return 0;
}

static int hall_resume(struct device *dev)
{
	struct hall_drvdata *ddata = dev_get_drvdata(dev);
	struct input_dev *input = ddata->input;
	bool status;

	printk("%s start\n", __func__);
	status = gpio_get_value(ddata->gpio_flip_cover);
	printk("[keys] %s flip_status : %d (%s)\n", __func__, status, status?"open":"close");
	input_sync(input);
/* WorkAround for Hall IRQ Noise problem in connect to GGSM band */
#ifdef CONFIG_SENSORS_HALL_IRQ_CTRL
	/* gsm_area can be controlled only in hall_irq_set */
	if (g_drvdata->cover_state && g_drvdata->gsm_area)
		hall_irq_set(enable, false);
#endif

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(hall_pm_ops, hall_suspend, hall_resume);

static struct platform_driver hall_device_driver = {
	.probe		= hall_probe,
	.remove		= hall_remove,
	.driver		= {
		.name	= "hall",
		.owner	= THIS_MODULE,
		.pm	= &hall_pm_ops,
#if defined(CONFIG_OF)
		.of_match_table	= hall_dt_ids,
#endif /* CONFIG_OF */
	}
};

static int __init hall_init(void)
{
	printk("%s start\n", __func__);
	return platform_driver_register(&hall_device_driver);
}

static void __exit hall_exit(void)
{
	printk("%s start\n", __func__);
	platform_driver_unregister(&hall_device_driver);
}

late_initcall(hall_init);
module_exit(hall_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Insun Choi <insun77.choi@samsung.com>");
MODULE_DESCRIPTION("Hall IC driver for GPIOs");
MODULE_ALIAS("platform:gpio-keys");