forked from SanniZ/fpc1020-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpc1020_main.c
1923 lines (1406 loc) · 47.2 KB
/
fpc1020_main.c
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* FPC1020 Touch sensor driver
*
* Copyright (c) 2013,2014 Fingerprint Cards AB <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License Version 2
* as published by the Free Software Foundation.
*/
#define DEBUG
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spi/spi.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/kthread.h>
#include <linux/poll.h>
#include <linux/types.h>
#ifndef CONFIG_OF
#include <linux/spi/fpc1020.h>
#include <linux/spi/fpc1020_common.h>
#include <linux/spi/fpc1020_regs.h>
#include <linux/spi/fpc1020_input.h>
#include <linux/spi/fpc1020_capture.h>
#include <linux/spi/fpc1020_regulator.h>
#else
#include <linux/of.h>
#include "fpc1020.h"
#include "fpc1020_common.h"
#include "fpc1020_regs.h"
#include "fpc1020_input.h"
#include "fpc1020_capture.h"
#include "fpc1020_regulator.h"
#endif
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Fingerprint Cards AB <[email protected]>");
MODULE_DESCRIPTION("FPC1020 touch sensor driver.");
/* -------------------------------------------------------------------- */
/* fpc1020 sensor commands and registers */
/* -------------------------------------------------------------------- */
typedef enum {
FPC_1020_ERROR_REG_BIT_FIFO_UNDERFLOW = 1 << 0
} fpc1020_error_reg_t;
/* -------------------------------------------------------------------- */
/* global variables */
/* -------------------------------------------------------------------- */
static int fpc1020_device_count;
/* -------------------------------------------------------------------- */
/* fpc1020 data types */
/* -------------------------------------------------------------------- */
struct fpc1020_attribute {
struct device_attribute attr;
size_t offset;
};
enum {
FPC1020_WORKER_IDLE_MODE = 0,
FPC1020_WORKER_CAPTURE_MODE,
FPC1020_WORKER_INPUT_MODE,
FPC1020_WORKER_EXIT
};
/* -------------------------------------------------------------------- */
/* fpc1020 driver constants */
/* -------------------------------------------------------------------- */
#define FPC1020_CLASS_NAME "fpsensor"
#define FPC1020_WORKER_THREAD_NAME "fpc1020_worker"
/* -------------------------------------------------------------------- */
/* function prototypes */
/* -------------------------------------------------------------------- */
static int __init fpc1020_init(void);
static void __exit fpc1020_exit(void);
static int __devinit fpc1020_probe(struct spi_device *spi);
static int __devexit fpc1020_remove(struct spi_device *spi);
static int fpc1020_suspend(struct device *dev);
static int fpc1020_resume(struct device *dev);
static int fpc1020_open(struct inode *inode, struct file *file);
static ssize_t fpc1020_write(struct file *file, const char *buff,
size_t count, loff_t *ppos);
static ssize_t fpc1020_read(struct file *file, char *buff,
size_t count, loff_t *ppos);
static int fpc1020_release(struct inode *inode, struct file *file);
static unsigned int fpc1020_poll(struct file *file, poll_table *wait);
static int fpc1020_cleanup(fpc1020_data_t *fpc1020, struct spi_device *spidev);
static int __devinit fpc1020_param_init(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata);
static int __devinit fpc1020_supply_init(fpc1020_data_t *fpc1020);
static int __devinit fpc1020_reset_init(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata);
static int __devinit fpc1020_irq_init(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata);
static int __devinit fpc1020_spi_setup(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata);
static int __devinit fpc1020_worker_init(fpc1020_data_t *fpc1020);
static int __devexit fpc1020_worker_destroy(fpc1020_data_t *fpc1020);
static int __devinit fpc1020_get_of_pdata(struct device *dev,
struct fpc1020_platform_data *pdata);
static int __devinit fpc1020_create_class(fpc1020_data_t *fpc1020);
static int __devinit fpc1020_create_device(fpc1020_data_t *fpc1020);
static int fpc1020_manage_sysfs(fpc1020_data_t *fpc1020,
struct spi_device *spi, bool create);
irqreturn_t fpc1020_interrupt(int irq, void *_fpc1020);
static ssize_t fpc1020_show_attr_setup(struct device *dev,
struct device_attribute *attr,
char *buf);
static ssize_t fpc1020_store_attr_setup(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count);
static ssize_t fpc1020_show_attr_diag(struct device *dev,
struct device_attribute *attr,
char *buf);
static ssize_t fpc1020_store_attr_diag(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count);
static u8 fpc1020_selftest_short(fpc1020_data_t *fpc1020);
static int fpc1020_start_capture(fpc1020_data_t *fpc1020);
static int fpc1020_new_job(fpc1020_data_t *fpc1020, int new_job);
static int fpc1020_worker_goto_idle(fpc1020_data_t *fpc1020);
static int fpc1020_worker_function(void *_fpc1020);
static int fpc1020_start_input(fpc1020_data_t *fpc1020);
/* -------------------------------------------------------------------- */
/* External interface */
/* -------------------------------------------------------------------- */
module_init(fpc1020_init);
module_exit(fpc1020_exit);
static const struct dev_pm_ops fpc1020_pm = {
.suspend = fpc1020_suspend,
.resume = fpc1020_resume
};
#ifdef CONFIG_OF
static struct of_device_id fpc1020_of_match[] __devinitdata = {
{ .compatible = "fpc,fpc1020", },
{}
};
MODULE_DEVICE_TABLE(of, fpc1020_of_match);
#endif
static struct spi_driver fpc1020_driver = {
.driver = {
.name = FPC1020_DEV_NAME,
.bus = &spi_bus_type,
.owner = THIS_MODULE,
.pm = &fpc1020_pm,
#ifdef CONFIG_OF
.of_match_table = fpc1020_of_match,
#endif
},
.probe = fpc1020_probe,
.remove = __devexit_p(fpc1020_remove)
};
static const struct file_operations fpc1020_fops = {
.owner = THIS_MODULE,
.open = fpc1020_open,
.write = fpc1020_write,
.read = fpc1020_read,
.release = fpc1020_release,
.poll = fpc1020_poll,
};
/* -------------------------------------------------------------------- */
/* devfs */
/* -------------------------------------------------------------------- */
#define FPC1020_ATTR(__grp, __field, __mode) \
{ \
.attr = __ATTR(__field, (__mode), \
fpc1020_show_attr_##__grp, \
fpc1020_store_attr_##__grp), \
.offset = offsetof(struct fpc1020_##__grp, __field) \
}
#define FPC1020_DEV_ATTR(_grp, _field, _mode) \
struct fpc1020_attribute fpc1020_attr_##_field = \
FPC1020_ATTR(_grp, _field, (_mode))
#define DEVFS_SETUP_MODE (S_IWUSR|S_IWGRP|S_IWOTH|S_IRUSR|S_IRGRP|S_IROTH)
static FPC1020_DEV_ATTR(setup, adc_gain, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, adc_shift, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, capture_mode, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, capture_count, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, capture_settings_mux, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, pxl_ctrl, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, capture_row_start, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, capture_row_count, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, capture_col_start, DEVFS_SETUP_MODE);
static FPC1020_DEV_ATTR(setup, capture_col_groups, DEVFS_SETUP_MODE);
static struct attribute *fpc1020_setup_attrs[] = {
&fpc1020_attr_adc_gain.attr.attr,
&fpc1020_attr_adc_shift.attr.attr,
&fpc1020_attr_capture_mode.attr.attr,
&fpc1020_attr_capture_count.attr.attr,
&fpc1020_attr_capture_settings_mux.attr.attr,
&fpc1020_attr_pxl_ctrl.attr.attr,
&fpc1020_attr_capture_row_start.attr.attr,
&fpc1020_attr_capture_row_count.attr.attr,
&fpc1020_attr_capture_col_start.attr.attr,
&fpc1020_attr_capture_col_groups.attr.attr,
NULL
};
static const struct attribute_group fpc1020_setup_attr_group = {
.attrs = fpc1020_setup_attrs,
.name = "setup"
};
#define DEVFS_DIAG_MODE_RO (S_IRUSR|S_IRGRP|S_IROTH)
#define DEVFS_DIAG_MODE_RW (S_IWUSR|S_IWGRP|S_IWOTH|S_IRUSR|S_IRGRP|S_IROTH)
static FPC1020_DEV_ATTR(diag, chip_id, DEVFS_DIAG_MODE_RO);
static FPC1020_DEV_ATTR(diag, selftest, DEVFS_DIAG_MODE_RO);
static FPC1020_DEV_ATTR(diag, spi_register, DEVFS_DIAG_MODE_RW);
static FPC1020_DEV_ATTR(diag, spi_regsize, DEVFS_DIAG_MODE_RO);
static FPC1020_DEV_ATTR(diag, spi_data , DEVFS_DIAG_MODE_RW);
static FPC1020_DEV_ATTR(diag, last_capture_time,DEVFS_DIAG_MODE_RO);
static struct attribute *fpc1020_diag_attrs[] = {
&fpc1020_attr_chip_id.attr.attr,
&fpc1020_attr_selftest.attr.attr,
&fpc1020_attr_spi_register.attr.attr,
&fpc1020_attr_spi_regsize.attr.attr,
&fpc1020_attr_spi_data.attr.attr,
&fpc1020_attr_last_capture_time.attr.attr,
NULL
};
static const struct attribute_group fpc1020_diag_attr_group = {
.attrs = fpc1020_diag_attrs,
.name = "diag"
};
/* -------------------------------------------------------------------- */
/* SPI debug interface, prototypes */
/* -------------------------------------------------------------------- */
static int fpc1020_spi_debug_select(fpc1020_data_t *fpc1020,
fpc1020_reg_t reg);
static int fpc1020_spi_debug_value_write(fpc1020_data_t *fpc1020, u64 data);
static int fpc1020_spi_debug_buffer_write(fpc1020_data_t *fpc1020,
const char *data,
size_t count);
static int fpc1020_spi_debug_value_read(fpc1020_data_t *fpc1020,
u64 *data);
static int fpc1020_spi_debug_buffer_read(fpc1020_data_t *fpc1020,
u8 *data,
size_t max_count);
static void fpc1020_spi_debug_buffer_to_hex_string(char *string,
u8 *buffer,
size_t bytes);
static int fpc1020_spi_debug_hex_string_to_buffer(u8 *buffer,
size_t buf_size,
const char *string,
size_t chars);
/* -------------------------------------------------------------------- */
/* function definitions */
/* -------------------------------------------------------------------- */
static int __init fpc1020_init(void)
{
if (spi_register_driver(&fpc1020_driver))
return -EINVAL;
return 0;
}
/* -------------------------------------------------------------------- */
static void __exit fpc1020_exit(void)
{
printk(KERN_INFO "%s\n", __func__);
spi_unregister_driver(&fpc1020_driver);
}
/* -------------------------------------------------------------------- */
static int __devinit fpc1020_probe(struct spi_device *spi)
{
struct fpc1020_platform_data *fpc1020_pdata;
struct fpc1020_platform_data pdata_of;
struct device *dev = &spi->dev;
int error = 0;
fpc1020_data_t *fpc1020 = NULL;
size_t buffer_size;
fpc1020 = kzalloc(sizeof(*fpc1020), GFP_KERNEL);
if (!fpc1020) {
dev_err(&spi->dev,
"failed to allocate memory for struct fpc1020_data\n");
return -ENOMEM;
}
printk(KERN_INFO "%s\n", __func__);
buffer_size = fpc1020_calc_huge_buffer_minsize(fpc1020);
error = fpc1020_manage_huge_buffer(fpc1020, buffer_size);
if (error)
goto err;
spi_set_drvdata(spi, fpc1020);
fpc1020->spi = spi;
fpc1020->reset_gpio = -EINVAL;
fpc1020->irq_gpio = -EINVAL;
fpc1020->cs_gpio = -EINVAL;
fpc1020->irq = -EINVAL;
init_waitqueue_head(&fpc1020->wq_irq_return);
error = fpc1020_init_capture(fpc1020);
if (error)
goto err;
fpc1020_pdata = spi->dev.platform_data;
if (!fpc1020_pdata) {
error = fpc1020_get_of_pdata(dev, &pdata_of);
fpc1020_pdata = &pdata_of;
if (error)
goto err;
}
if (!fpc1020_pdata) {
dev_err(&fpc1020->spi->dev,
"spi->dev.platform_data is NULL.\n");
error = -EINVAL;
goto err;
}
error = fpc1020_param_init(fpc1020, fpc1020_pdata);
if (error)
goto err;
error = fpc1020_supply_init(fpc1020);
if (error)
goto err;
error = fpc1020_reset_init(fpc1020, fpc1020_pdata);
if (error)
goto err;
error = fpc1020_irq_init(fpc1020, fpc1020_pdata);
if (error)
goto err;
error = fpc1020_spi_setup(fpc1020, fpc1020_pdata);
if (error)
goto err;
error = fpc1020_reset(fpc1020);
if (error)
goto err;
error = fpc1020_check_hw_id(fpc1020);
if (error)
goto err;
buffer_size = fpc1020_calc_huge_buffer_minsize(fpc1020);
error = fpc1020_manage_huge_buffer(fpc1020, buffer_size);
if (error)
goto err;
error = fpc1020_setup_defaults(fpc1020);
if (error)
goto err;
error = fpc1020_create_class(fpc1020);
if (error)
goto err;
error = fpc1020_create_device(fpc1020);
if (error)
goto err;
sema_init(&fpc1020->mutex, 0);
error = fpc1020_manage_sysfs(fpc1020, spi, true);
if (error)
goto err;
cdev_init(&fpc1020->cdev, &fpc1020_fops);
fpc1020->cdev.owner = THIS_MODULE;
error = cdev_add(&fpc1020->cdev, fpc1020->devno, 1);
if (error) {
dev_err(&fpc1020->spi->dev, "cdev_add failed.\n");
goto err_chrdev;
}
error = fpc1020_worker_init(fpc1020);
if (error)
goto err_cdev;
error = fpc1020_calc_finger_detect_threshold_min(fpc1020);
if (error < 0)
goto err_cdev;
error = fpc1020_set_finger_detect_threshold(fpc1020, error);
if (error < 0)
goto err_cdev;
error = fpc1020_input_init(fpc1020);
if (error)
goto err_cdev;
error = fpc1020_start_input(fpc1020);
if (error)
goto err_cdev;
/*
error = fpc1020_sleep(fpc1020, true);
if (error)
goto err_cdev;
*/
up(&fpc1020->mutex);
return 0;
err_cdev:
cdev_del(&fpc1020->cdev);
err_chrdev:
unregister_chrdev_region(fpc1020->devno, 1);
fpc1020_manage_sysfs(fpc1020, spi, false);
err:
fpc1020_cleanup(fpc1020, spi);
return error;
}
/* -------------------------------------------------------------------- */
static int __devexit fpc1020_remove(struct spi_device *spi)
{
fpc1020_data_t *fpc1020 = spi_get_drvdata(spi);
printk(KERN_INFO "%s\n", __func__);
fpc1020_manage_sysfs(fpc1020, spi, false);
fpc1020_sleep(fpc1020, true);
cdev_del(&fpc1020->cdev);
unregister_chrdev_region(fpc1020->devno, 1);
fpc1020_cleanup(fpc1020, spi);
return 0;
}
/* -------------------------------------------------------------------- */
static int fpc1020_suspend(struct device *dev)
{
fpc1020_data_t *fpc1020 = dev_get_drvdata(dev);
dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
fpc1020_worker_goto_idle(fpc1020);
return fpc1020_sleep(fpc1020, true);
}
/* -------------------------------------------------------------------- */
static int fpc1020_resume(struct device *dev)
{
fpc1020_data_t *fpc1020 = dev_get_drvdata(dev);
dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
if (fpc1020->input.enabled)
fpc1020_start_input(fpc1020);
return 0;
}
/* -------------------------------------------------------------------- */
static int fpc1020_open(struct inode *inode, struct file *file)
{
fpc1020_data_t *fpc1020;
printk(KERN_INFO "%s\n", __func__);
fpc1020 = container_of(inode->i_cdev, fpc1020_data_t, cdev);
if (down_interruptible(&fpc1020->mutex))
return -ERESTARTSYS;
file->private_data = fpc1020;
up(&fpc1020->mutex);
return 0;
}
/* -------------------------------------------------------------------- */
static ssize_t fpc1020_write(struct file *file, const char *buff,
size_t count, loff_t *ppos)
{
printk(KERN_INFO "%s\n", __func__);
return -ENOTTY;
}
/* -------------------------------------------------------------------- */
static ssize_t fpc1020_read(struct file *file, char *buff,
size_t count, loff_t *ppos)
{
fpc1020_data_t *fpc1020 = file->private_data;
int error = 0;
u32 max_data;
u32 avail_data;
if (down_interruptible(&fpc1020->mutex))
return -ERESTARTSYS;
if (fpc1020->capture.available_bytes > 0) {
goto copy_data;
} else {
if (fpc1020->capture.read_pending_eof) {
fpc1020->capture.read_pending_eof = false;
error = 0;
goto out;
}
if (file->f_flags & O_NONBLOCK) {
if (fpc1020_capture_check_ready(fpc1020)) {
error = fpc1020_start_capture(fpc1020);
if (error)
goto out;
}
error = -EWOULDBLOCK;
goto out;
} else {
error = fpc1020_start_capture(fpc1020);
if (error)
goto out;
}
}
error = wait_event_interruptible(
fpc1020->capture.wq_data_avail,
(fpc1020->capture.available_bytes > 0));
if (error)
goto out;
if (fpc1020->capture.last_error != 0) {
error = fpc1020->capture.last_error;
goto out;
}
copy_data:
avail_data = fpc1020->capture.available_bytes;
max_data = (count > avail_data) ? avail_data : count;
if (max_data) {
error = copy_to_user(buff,
&fpc1020->huge_buffer[fpc1020->capture.read_offset],
max_data);
if (error)
goto out;
fpc1020->capture.read_offset += max_data;
fpc1020->capture.available_bytes -= max_data;
error = max_data;
if (fpc1020->capture.available_bytes == 0)
fpc1020->capture.read_pending_eof = true;
}
out:
up(&fpc1020->mutex);
return error;
}
/* -------------------------------------------------------------------- */
static int fpc1020_release(struct inode *inode, struct file *file)
{
fpc1020_data_t *fpc1020 = file->private_data;
int status = 0;
printk(KERN_INFO "%s\n", __func__);
if (down_interruptible(&fpc1020->mutex))
return -ERESTARTSYS;
fpc1020_start_input(fpc1020);
/*
fpc1020_worker_goto_idle(fpc1020);
fpc1020_sleep(fpc1020, true);
*/
up(&fpc1020->mutex);
return status;
}
/* -------------------------------------------------------------------- */
static unsigned int fpc1020_poll(struct file *file, poll_table *wait)
{
fpc1020_data_t *fpc1020 = file->private_data;
unsigned int ret = 0;
fpc1020_capture_mode_t mode = fpc1020->setup.capture_mode;
bool blocking_op;
if (down_interruptible(&fpc1020->mutex))
return -ERESTARTSYS;
if (fpc1020->capture.available_bytes > 0)
ret |= (POLLIN | POLLRDNORM);
else if (fpc1020->capture.read_pending_eof)
ret |= POLLHUP;
else { /* available_bytes == 0 && !pending_eof */
blocking_op =
(mode == FPC1020_MODE_WAIT_AND_CAPTURE) ? true : false;
switch (fpc1020->capture.state) {
case FPC1020_CAPTURE_STATE_IDLE:
if (!blocking_op)
ret |= POLLIN;
break;
case FPC1020_CAPTURE_STATE_STARTED:
case FPC1020_CAPTURE_STATE_PENDING:
case FPC1020_CAPTURE_STATE_WRITE_SETTINGS:
case FPC1020_CAPTURE_STATE_WAIT_FOR_FINGER_DOWN:
case FPC1020_CAPTURE_STATE_ACQUIRE:
case FPC1020_CAPTURE_STATE_FETCH:
case FPC1020_CAPTURE_STATE_WAIT_FOR_FINGER_UP:
case FPC1020_CAPTURE_STATE_COMPLETED:
ret |= POLLIN;
poll_wait(file, &fpc1020->capture.wq_data_avail, wait);
if (fpc1020->capture.available_bytes > 0)
ret |= POLLRDNORM;
else if (blocking_op)
ret = 0;
break;
case FPC1020_CAPTURE_STATE_FAILED:
if (!blocking_op)
ret |= POLLIN;
break;
default:
dev_err(&fpc1020->spi->dev,
"%s unknown state\n", __func__);
break;
}
}
up(&fpc1020->mutex);
return ret;
}
/* -------------------------------------------------------------------- */
static int fpc1020_cleanup(fpc1020_data_t *fpc1020, struct spi_device *spidev)
{
dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
fpc1020_worker_destroy(fpc1020);
if (!IS_ERR_OR_NULL(fpc1020->device))
device_destroy(fpc1020->class, fpc1020->devno);
class_destroy(fpc1020->class);
if (fpc1020->irq >= 0)
free_irq(fpc1020->irq, fpc1020);
if (gpio_is_valid(fpc1020->irq_gpio))
gpio_free(fpc1020->irq_gpio);
if (gpio_is_valid(fpc1020->reset_gpio))
gpio_free(fpc1020->reset_gpio);
if (gpio_is_valid(fpc1020->cs_gpio))
gpio_free(fpc1020->cs_gpio);
fpc1020_manage_huge_buffer(fpc1020, 0);
fpc1020_input_destroy(fpc1020);
fpc1020_regulator_release(fpc1020);
kfree(fpc1020);
spi_set_drvdata(spidev, NULL);
return 0;
}
/* -------------------------------------------------------------------- */
static int __devinit fpc1020_param_init(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata)
{
fpc1020->vddtx_mv = pdata->external_supply_mv;
fpc1020->txout_boost = pdata->txout_boost;
if (fpc1020->vddtx_mv > 0) {
dev_info(&fpc1020->spi->dev,
"External TxOut supply (%d mV)\n",
fpc1020->vddtx_mv);
} else {
dev_info(&fpc1020->spi->dev,
"Internal TxOut supply (boost %s)\n",
(fpc1020->txout_boost) ? "ON" : "OFF");
}
return 0;
}
/* -------------------------------------------------------------------- */
static int __devinit fpc1020_supply_init(fpc1020_data_t *fpc1020)
{
int error = 0;
error = fpc1020_regulator_configure(fpc1020);
if (error) {
dev_err(&fpc1020->spi->dev,
"fpc1020_probe - regulator configuration failed.\n");
goto err;
}
error = fpc1020_regulator_set(fpc1020, true);
if (error) {
dev_err(&fpc1020->spi->dev,
"fpc1020_probe - regulator enable failed.\n");
goto err;
}
err:
return error;
}
/* -------------------------------------------------------------------- */
static int __devinit fpc1020_reset_init(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata)
{
int error = 0;
if (gpio_is_valid(pdata->reset_gpio)) {
dev_info(&fpc1020->spi->dev,
"Assign HW reset -> GPIO%d\n", pdata->reset_gpio);
fpc1020->soft_reset_enabled = false;
error = gpio_request(pdata->reset_gpio, "fpc1020_reset");
if (error) {
dev_err(&fpc1020->spi->dev,
"gpio_request (reset) failed.\n");
return error;
}
fpc1020->reset_gpio = pdata->reset_gpio;
error = gpio_direction_output(fpc1020->reset_gpio, 1);
if (error) {
dev_err(&fpc1020->spi->dev,
"gpio_direction_output(reset) failed.\n");
return error;
}
} else {
dev_info(&fpc1020->spi->dev, "Using soft reset\n");
fpc1020->soft_reset_enabled = true;
}
return error;
}
/* -------------------------------------------------------------------- */
static int __devinit fpc1020_irq_init(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata)
{
int error = 0;
if (gpio_is_valid(pdata->irq_gpio)) {
dev_info(&fpc1020->spi->dev,
"Assign IRQ -> GPIO%d\n",
pdata->irq_gpio);
error = gpio_request(pdata->irq_gpio, "fpc1020_irq");
if (error) {
dev_err(&fpc1020->spi->dev,
"gpio_request (irq) failed.\n");
return error;
}
fpc1020->irq_gpio = pdata->irq_gpio;
error = gpio_direction_input(fpc1020->irq_gpio);
if (error) {
dev_err(&fpc1020->spi->dev,
"gpio_direction_input (irq) failed.\n");
return error;
}
} else {
return -EINVAL;
}
fpc1020->irq = gpio_to_irq(fpc1020->irq_gpio);
if (fpc1020->irq < 0) {
dev_err(&fpc1020->spi->dev, "gpio_to_irq failed.\n");
error = fpc1020->irq;
return error;
}
error = request_irq(fpc1020->irq, fpc1020_interrupt,
IRQF_TRIGGER_RISING, "fpc1020", fpc1020);
if (error) {
dev_err(&fpc1020->spi->dev,
"request_irq %i failed.\n",
fpc1020->irq);
fpc1020->irq = -EINVAL;
return error;
}
return error;
}
/* -------------------------------------------------------------------- */
static int __devinit fpc1020_spi_setup(fpc1020_data_t *fpc1020,
struct fpc1020_platform_data *pdata)
{
int error = 0;
printk(KERN_INFO "%s\n", __func__);
fpc1020->spi->mode = SPI_MODE_0;
fpc1020->spi->bits_per_word = 8;
error = spi_setup(fpc1020->spi);
if (error) {
dev_err(&fpc1020->spi->dev, "spi_setup failed\n");
goto out_err;
}
if (gpio_is_valid(pdata->cs_gpio)) {
dev_info(&fpc1020->spi->dev,
"Assign SPI.CS -> GPIO%d\n",
pdata->cs_gpio);
error = gpio_request(pdata->cs_gpio, "fpc1020_cs");
if (error) {
dev_err(&fpc1020->spi->dev,
"gpio_request (cs) failed.\n");
goto out_err;
}
fpc1020->cs_gpio = pdata->cs_gpio;
error = gpio_direction_output(fpc1020->cs_gpio, 1);
if (error) {
dev_err(&fpc1020->spi->dev,
"gpio_direction_output(cs) failed.\n");
goto out_err;
}
} else {
error = -EINVAL;
}
out_err:
return error;
}
/* -------------------------------------------------------------------- */
static int __devinit fpc1020_worker_init(fpc1020_data_t *fpc1020)
{
int error = 0;
printk(KERN_INFO "%s\n", __func__);
init_waitqueue_head(&fpc1020->worker.wq_wait_job);
sema_init(&fpc1020->worker.sem_idle, 0);
fpc1020->worker.req_mode = FPC1020_WORKER_IDLE_MODE;
fpc1020->worker.thread = kthread_run(fpc1020_worker_function,
fpc1020, "%s",
FPC1020_WORKER_THREAD_NAME);
if (IS_ERR(fpc1020->worker.thread)) {
dev_err(&fpc1020->spi->dev, "kthread_run failed.\n");
error = (int)PTR_ERR(fpc1020->worker.thread);
}
return error;