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
|
// SPDX-License-Identifier: GPL-2.0
/*
* AWINIC aw96103 proximity sensor driver
*
* Author: Wang Shuaijie <wangshuaijie@awinic.com>
*
* Copyright (c) 2024 awinic Technology CO., LTD
*/
#include <linux/bits.h>
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/iio/events.h>
#include <linux/iio/iio.h>
#include <linux/regulator/consumer.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/unaligned.h>
#define AW_DATA_PROCESS_FACTOR 1024
#define AW96103_CHIP_ID 0xa961
#define AW96103_BIN_VALID_DATA_OFFSET 64
#define AW96103_BIN_DATA_LEN_OFFSET 16
#define AW96103_BIN_DATA_REG_NUM_SIZE 4
#define AW96103_BIN_CHIP_TYPE_SIZE 8
#define AW96103_BIN_CHIP_TYPE_OFFSET 24
#define AW96103_REG_SCANCTRL0 0x0000
#define AW96103_REG_STAT0 0x0090
#define AW96103_REG_BLFILT_CH0 0x00A8
#define AW96103_REG_BLRSTRNG_CH0 0x00B4
#define AW96103_REG_DIFF_CH0 0x0240
#define AW96103_REG_FWVER2 0x0410
#define AW96103_REG_CMD 0xF008
#define AW96103_REG_IRQSRC 0xF080
#define AW96103_REG_IRQEN 0xF084
#define AW96103_REG_RESET 0xFF0C
#define AW96103_REG_CHIPID 0xFF10
#define AW96103_REG_EEDA0 0x0408
#define AW96103_REG_EEDA1 0x040C
#define AW96103_REG_PROXCTRL_CH0 0x00B0
#define AW96103_REG_PROXTH0_CH0 0x00B8
#define AW96103_PROXTH_CH_STEP 0x3C
#define AW96103_THHYST_MASK GENMASK(13, 12)
#define AW96103_INDEB_MASK GENMASK(11, 10)
#define AW96103_OUTDEB_MASK GENMASK(9, 8)
#define AW96103_INITOVERIRQ_MASK BIT(0)
#define AW96103_BLFILT_CH_STEP 0x3C
#define AW96103_BLRSTRNG_MASK GENMASK(5, 0)
#define AW96103_CHIPID_MASK GENMASK(31, 16)
#define AW96103_BLERRTRIG_MASK BIT(25)
#define AW96103_CHAN_EN_MASK GENMASK(5, 0)
#define AW96103_REG_PROXCTRL_CH(x) \
(AW96103_REG_PROXCTRL_CH0 + (x) * AW96103_PROXTH_CH_STEP)
#define AW96103_REG_PROXTH0_CH(x) \
(AW96103_REG_PROXTH0_CH0 + (x) * AW96103_PROXTH_CH_STEP)
/**
* struct aw_bin - Store the data obtained from parsing the configuration file.
* @chip_type: Frame header information-chip type
* @valid_data_len: Length of valid data obtained after parsing
* @valid_data_addr: The offset address of the valid data obtained
* after parsing relative to info
* @len: The size of the bin file obtained from the firmware
* @data: Store the bin file obtained from the firmware
*/
struct aw_bin {
unsigned char chip_type[8];
unsigned int valid_data_len;
unsigned int valid_data_addr;
unsigned int len;
unsigned char data[] __counted_by(len);
};
enum aw96103_sar_vers {
AW96103 = 2,
AW96103A = 6,
AW96103B = 0xa,
};
enum aw96103_operation_mode {
AW96103_ACTIVE_MODE = 1,
AW96103_SLEEP_MODE = 2,
AW96103_DEEPSLEEP_MODE = 3,
AW96103B_DEEPSLEEP_MODE = 4,
};
enum aw96103_sensor_type {
AW96103_VAL,
AW96105_VAL,
};
struct aw_channels_info {
bool used;
unsigned int old_irq_status;
};
struct aw_chip_info {
const char *name;
struct iio_chan_spec const *channels;
int num_channels;
};
struct aw96103 {
unsigned int hostirqen;
struct regmap *regmap;
struct device *dev;
/*
* There is one more logical channel than the actual channels,
* and the extra logical channel is used for temperature detection
* but not for status detection. The specific channel used for
* temperature detection is determined by the register configuration.
*/
struct aw_channels_info channels_arr[6];
unsigned int max_channels;
unsigned int chan_en;
};
static const unsigned int aw96103_reg_default[] = {
0x0000, 0x00003f3f, 0x0004, 0x00000064, 0x0008, 0x0017c11e,
0x000c, 0x05000000, 0x0010, 0x00093ffd, 0x0014, 0x19240009,
0x0018, 0xd81c0207, 0x001c, 0xff000000, 0x0020, 0x00241900,
0x0024, 0x00093ff7, 0x0028, 0x58020009, 0x002c, 0xd81c0207,
0x0030, 0xff000000, 0x0034, 0x00025800, 0x0038, 0x00093fdf,
0x003c, 0x7d3b0009, 0x0040, 0xd81c0207, 0x0044, 0xff000000,
0x0048, 0x003b7d00, 0x004c, 0x00093f7f, 0x0050, 0xe9310009,
0x0054, 0xd81c0207, 0x0058, 0xff000000, 0x005c, 0x0031e900,
0x0060, 0x00093dff, 0x0064, 0x1a0c0009, 0x0068, 0xd81c0207,
0x006c, 0xff000000, 0x0070, 0x000c1a00, 0x0074, 0x80093fff,
0x0078, 0x043d0009, 0x007c, 0xd81c0207, 0x0080, 0xff000000,
0x0084, 0x003d0400, 0x00a0, 0xe6400000, 0x00a4, 0x00000000,
0x00a8, 0x010408d2, 0x00ac, 0x00000000, 0x00b0, 0x00000000,
0x00b8, 0x00005fff, 0x00bc, 0x00000000, 0x00c0, 0x00000000,
0x00c4, 0x00000000, 0x00c8, 0x00000000, 0x00cc, 0x00000000,
0x00d0, 0x00000000, 0x00d4, 0x00000000, 0x00d8, 0x00000000,
0x00dc, 0xe6447800, 0x00e0, 0x78000000, 0x00e4, 0x010408d2,
0x00e8, 0x00000000, 0x00ec, 0x00000000, 0x00f4, 0x00005fff,
0x00f8, 0x00000000, 0x00fc, 0x00000000, 0x0100, 0x00000000,
0x0104, 0x00000000, 0x0108, 0x00000000, 0x010c, 0x02000000,
0x0110, 0x00000000, 0x0114, 0x00000000, 0x0118, 0xe6447800,
0x011c, 0x78000000, 0x0120, 0x010408d2, 0x0124, 0x00000000,
0x0128, 0x00000000, 0x0130, 0x00005fff, 0x0134, 0x00000000,
0x0138, 0x00000000, 0x013c, 0x00000000, 0x0140, 0x00000000,
0x0144, 0x00000000, 0x0148, 0x02000000, 0x014c, 0x00000000,
0x0150, 0x00000000, 0x0154, 0xe6447800, 0x0158, 0x78000000,
0x015c, 0x010408d2, 0x0160, 0x00000000, 0x0164, 0x00000000,
0x016c, 0x00005fff, 0x0170, 0x00000000, 0x0174, 0x00000000,
0x0178, 0x00000000, 0x017c, 0x00000000, 0x0180, 0x00000000,
0x0184, 0x02000000, 0x0188, 0x00000000, 0x018c, 0x00000000,
0x0190, 0xe6447800, 0x0194, 0x78000000, 0x0198, 0x010408d2,
0x019c, 0x00000000, 0x01a0, 0x00000000, 0x01a8, 0x00005fff,
0x01ac, 0x00000000, 0x01b0, 0x00000000, 0x01b4, 0x00000000,
0x01b8, 0x00000000, 0x01bc, 0x00000000, 0x01c0, 0x02000000,
0x01c4, 0x00000000, 0x01c8, 0x00000000, 0x01cc, 0xe6407800,
0x01d0, 0x78000000, 0x01d4, 0x010408d2, 0x01d8, 0x00000000,
0x01dc, 0x00000000, 0x01e4, 0x00005fff, 0x01e8, 0x00000000,
0x01ec, 0x00000000, 0x01f0, 0x00000000, 0x01f4, 0x00000000,
0x01f8, 0x00000000, 0x01fc, 0x02000000, 0x0200, 0x00000000,
0x0204, 0x00000000, 0x0208, 0x00000008, 0x020c, 0x0000000d,
0x41fc, 0x00000000, 0x4400, 0x00000000, 0x4410, 0x00000000,
0x4420, 0x00000000, 0x4430, 0x00000000, 0x4440, 0x00000000,
0x4450, 0x00000000, 0x4460, 0x00000000, 0x4470, 0x00000000,
0xf080, 0x00003018, 0xf084, 0x00000fff, 0xf800, 0x00000000,
0xf804, 0x00002e00, 0xf8d0, 0x00000001, 0xf8d4, 0x00000000,
0xff00, 0x00000301, 0xff0c, 0x01000000, 0xffe0, 0x00000000,
0xfff4, 0x00004011, 0x0090, 0x00000000, 0x0094, 0x00000000,
0x0098, 0x00000000, 0x009c, 0x3f3f3f3f,
};
static const struct iio_event_spec aw_common_events[3] = {
{
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_RISING,
.mask_separate = BIT(IIO_EV_INFO_PERIOD),
},
{
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_FALLING,
.mask_separate = BIT(IIO_EV_INFO_PERIOD),
},
{
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_EITHER,
.mask_separate = BIT(IIO_EV_INFO_ENABLE) |
BIT(IIO_EV_INFO_HYSTERESIS) |
BIT(IIO_EV_INFO_VALUE),
}
};
#define AW_IIO_CHANNEL(idx) \
{ \
.type = IIO_PROXIMITY, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
.indexed = 1, \
.channel = idx, \
.event_spec = aw_common_events, \
.num_event_specs = ARRAY_SIZE(aw_common_events), \
|