forked from apache/nuttx
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcypress_mbr3108.c
1141 lines (926 loc) · 29.9 KB
/
cypress_mbr3108.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
/****************************************************************************
* drivers/input/cypress_mbr3108.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <poll.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include <nuttx/signal.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/input/cypress_mbr3108.h>
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
#ifdef CONFIG_INPUT_CYPRESS_MBR3108_DEBUG
# define mbr3108_dbg(x, ...) _info(x, ##__VA_ARGS__)
#else
# define mbr3108_dbg(x, ...) iinfo(x, ##__VA_ARGS__)
#endif
/* Register macros */
#define MBR3108_SENSOR_EN 0x0
#define MBR3108_FSS_EN 0x02
#define MBR3108_TOGGLE_EN 0x04
#define MBR3108_LED_ON_EN 0x06
#define MBR3108_SENSITIVITY0 0x08
#define MBR3108_SENSITIVITY1 0x09
#define MBR3108_BASE_THRESHOLD0 0x0c
#define MBR3108_BASE_THRESHOLD1 0x0d
#define MBR3108_FINGER_THRESHOLD2 0x0e
#define MBR3108_FINGER_THRESHOLD3 0x0f
#define MBR3108_FINGER_THRESHOLD4 0x10
#define MBR3108_FINGER_THRESHOLD5 0x11
#define MBR3108_FINGER_THRESHOLD6 0x12
#define MBR3108_FINGER_THRESHOLD7 0x13
#define MBR3108_SENSOR_DEBOUNCE 0x1c
#define MBR3108_BUTTON_HYS 0x1d
#define MBR3108_BUTTON_LBR 0x1f
#define MBR3108_BUTTON_NNT 0x20
#define MBR3108_BUTTON_NT 0x21
#define MBR3108_PROX_EN 0x26
#define MBR3108_PROX_CFG 0x27
#define MBR3108_PROX_CFG2 0x28
#define MBR3108_PROX_TOUCH_TH0 0x2a
#define MBR3108_PROX_TOUCH_TH1 0x2c
#define MBR3108_PROX_RESOLUTION0 0x2e
#define MBR3108_PROX_RESOLUTION1 0x2f
#define MBR3108_PROX_HYS 0x30
#define MBR3108_PROX_LBR 0x32
#define MBR3108_PROX_NNT 0x33
#define MBR3108_PROX_NT 0x34
#define MBR3108_PROX_POSITIVE_TH0 0x35
#define MBR3108_PROX_POSITIVE_TH1 0x36
#define MBR3108_PROX_NEGATIVE_TH0 0x39
#define MBR3108_PROX_NEGATIVE_TH1 0x3a
#define MBR3108_LED_ON_TIME 0x3d
#define MBR3108_BUZZER_CFG 0x3e
#define MBR3108_BUZZER_ON_TIME 0x3f
#define MBR3108_GPO_CFG 0x40
#define MBR3108_PWM_DUTYCYCLE_CFG0 0x41
#define MBR3108_PWM_DUTYCYCLE_CFG1 0x42
#define MBR3108_PWM_DUTYCYCLE_CFG2 0x43
#define MBR3108_PWM_DUTYCYCLE_CFG3 0x44
#define MBR3108_SPO_CFG 0x4c
#define MBR3108_DEVICE_CFG0 0x4d
#define MBR3108_DEVICE_CFG1 0x4e
#define MBR3108_DEVICE_CFG2 0x4f
#define MBR3108_DEVICE_CFG3 0x50
#define MBR3108_I2C_ADDR 0x51
#define MBR3108_REFRESH_CTRL 0x52
#define MBR3108_STATE_TIMEOUT 0x55
#define MBR3108_CONFIG_CRC 0x7e
#define MBR3108_GPO_OUTPUT_STATE 0x80
#define MBR3108_SENSOR_ID 0x82
#define MBR3108_CTRL_CMD 0x86
#define MBR3108_CTRL_CMD_STATUS 0x88
#define MBR3108_CTRL_CMD_ERR 0x89
#define MBR3108_SYSTEM_STATUS 0x8a
#define MBR3108_PREV_CTRL_CMD_CODE 0x8c
#define MBR3108_FAMILY_ID 0x8f
#define MBR3108_DEVICE_ID 0x90
#define MBR3108_DEVICE_REV 0x92
#define MBR3108_CALC_CRC 0x94
#define MBR3108_TOTAL_WORKING_SNS 0x97
#define MBR3108_SNS_CP_HIGH 0x98
#define MBR3108_SNS_VDD_SHORT 0x9a
#define MBR3108_SNS_GND_SHORT 0x9c
#define MBR3108_SNS_SNS_SHORT 0x9e
#define MBR3108_CMOD_SHIELD_TEST 0xa0
#define MBR3108_BUTTON_STAT 0xaa
#define MBR3108_LATCHED_BUTTON_STAT 0xac
#define MBR3108_PROX_STAT 0xae
#define MBR3108_LATCHED_PROX_STAT 0xaf
#define MBR3108_SYNC_COUNTER0 0xb9
#define MBR3108_DIFFERENCE_COUNT_SENSOR0 0xba
#define MBR3108_DIFFERENCE_COUNT_SENSOR1 0xbc
#define MBR3108_DIFFERENCE_COUNT_SENSOR2 0xbe
#define MBR3108_DIFFERENCE_COUNT_SENSOR3 0xc0
#define MBR3108_DIFFERENCE_COUNT_SENSOR4 0xc2
#define MBR3108_DIFFERENCE_COUNT_SENSOR5 0xc4
#define MBR3108_DIFFERENCE_COUNT_SENSOR6 0xc6
#define MBR3108_DIFFERENCE_COUNT_SENSOR7 0xc8
#define MBR3108_GPO_DATA 0xda
#define MBR3108_SYNC_COUNTER1 0xdb
#define MBR3108_DEBUG_SENSOR_ID 0xdc
#define MBR3108_DEBUG_CP 0xdd
#define MBR3108_DEBUG_DIFFERENCE_COUNT0 0xde
#define MBR3108_DEBUG_BASELINE0 0xe0
#define MBR3108_DEBUG_RAW_COUNT0 0xe2
#define MBR3108_DEBUG_AVG_RAW_COUNT0 0xe4
#define MBR3108_SYNC_COUNTER2 0xe7
/* Device commands for MBR3108_CTRL_CMD */
#define MBR3108_CMD_COMPLETED 0
#define MBR3108_CMD_CHECK_CONFIG_CRC 2
#define MBR3108_CMD_SET_CONFIG_CRC 3
#define MBR3108_CMD_ENTER_LOW_POWER_MODE 7
#define MBR3108_CMD_CLEAR_LATCHED 8
#define MBR3108_CMD_RESET_ADV_LOWPASS_FILTER_PROX_SENS_0 9
#define MBR3108_CMD_RESET_ADV_LOWPASS_FILTER_PROX_SENS_1 10
#define MBR3108_CMD_SOFTWARE_RESET 255
#define MBR3108_CMD_STATUS_SUCCESS 0
#define MBR3108_CMD_STATUS_ERROR 1
#define MBR3108_CMD_STATUS_MASK 1
/* Completion times for device commands */
#define MBR3108_CMD_MSECS_CHECK_CONFIG_CRC 280 /* >220 (typ.) */
#define MBR3108_CMD_MSECS_SOFTWARE_RESET 50
#define MBR3108_CMD_MSECS_CLEAR_LATCHED 50
/* Other macros */
#define MBR3108_I2C_RETRIES 10
#define MBR3108_NUM_SENSORS 8
#define MBR3108_EXPECTED_FAMILY_ID 0x9a
#define MBR3108_EXPECTED_DEVICE_ID 0x0a03
#define MBR3108_EXPECTED_DEVICE_REV 1
#define MBR3108_SYNC_RETRIES 10
#ifndef CONFIG_MBR3108_I2C_FREQUENCY
# define CONFIG_MBR3108_I2C_FREQUENCY 400000
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct mbr3108_dev_s
{
/* I2C bus and address for device. */
struct i2c_master_s *i2c;
uint8_t addr;
/* Configuration for device. */
struct mbr3108_board_s *board;
const struct mbr3108_sensor_conf_s *sensor_conf;
mutex_t devlock;
uint8_t cref;
struct mbr3108_debug_conf_s debug_conf;
bool int_pending;
struct pollfd *fds[CONFIG_INPUT_CYPRESS_MBR3108_NPOLLWAITERS];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int mbr3108_open(FAR struct file *filep);
static int mbr3108_close(FAR struct file *filep);
static ssize_t mbr3108_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t mbr3108_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int mbr3108_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations g_mbr3108_fileops =
{
mbr3108_open, /* open */
mbr3108_close, /* close */
mbr3108_read, /* read */
mbr3108_write, /* write */
NULL, /* seek */
NULL, /* ioctl */
mbr3108_poll /* poll */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, NULL /* unlink */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int mbr3108_i2c_write(FAR struct mbr3108_dev_s *dev, uint8_t reg,
const uint8_t *buf, size_t buflen)
{
struct i2c_msg_s msgv[2] =
{
{
.frequency = CONFIG_MBR3108_I2C_FREQUENCY,
.addr = dev->addr,
.flags = 0,
.buffer = ®,
.length = 1
},
{
.frequency = CONFIG_MBR3108_I2C_FREQUENCY,
.addr = dev->addr,
.flags = I2C_M_NOSTART,
.buffer = (void *)buf,
.length = buflen
}
};
int ret = -EIO;
int retries;
/* MBR3108 will respond with NACK to address when in low-power mode. Host
* needs to retry address selection multiple times to get MBR3108 to
* wake-up.
*/
for (retries = 0; retries < MBR3108_I2C_RETRIES; retries++)
{
ret = I2C_TRANSFER(dev->i2c, msgv, 2);
if (ret == -ENXIO)
{
/* -ENXIO is returned when getting NACK from response.
* Keep trying.
*/
continue;
}
if (ret >= 0)
{
/* Success! */
return 0;
}
}
/* Failed to read sensor. */
return ret;
}
static int mbr3108_i2c_read(FAR struct mbr3108_dev_s *dev, uint8_t reg,
uint8_t *buf, size_t buflen)
{
struct i2c_msg_s msgv[2] =
{
{
.frequency = CONFIG_MBR3108_I2C_FREQUENCY,
.addr = dev->addr,
.flags = 0,
.buffer = ®,
.length = 1
},
{
.frequency = CONFIG_MBR3108_I2C_FREQUENCY,
.addr = dev->addr,
.flags = I2C_M_READ,
.buffer = buf,
.length = buflen
}
};
int ret = -EIO;
int retries;
/* MBR3108 will respond with NACK to address when in low-power mode. Host
* needs to retry address selection multiple times to get MBR3108 to
* wake-up.
*/
for (retries = 0; retries < MBR3108_I2C_RETRIES; retries++)
{
ret = I2C_TRANSFER(dev->i2c, msgv, 2);
if (ret == -ENXIO)
{
/* -ENXIO is returned when getting NACK from response.
* Keep trying.
*/
continue;
}
else if (ret >= 0)
{
/* Success! */
return 0;
}
else
{
/* Some other error. Try to reset I2C bus and keep trying. */
#ifdef CONFIG_I2C_RESET
if (retries == MBR3108_I2C_RETRIES - 1)
{
break;
}
ret = I2C_RESET(dev->i2c);
if (ret < 0)
{
mbr3108_dbg("I2C_RESET failed: %d\n", ret);
return ret;
}
#endif
}
}
/* Failed to read sensor. */
return ret;
}
static int mbr3108_check_cmd_status(FAR struct mbr3108_dev_s *dev)
{
const uint8_t start_reg = MBR3108_CTRL_CMD;
const uint8_t last_reg = MBR3108_CTRL_CMD_ERR;
uint8_t readbuf[MBR3108_CTRL_CMD_ERR - MBR3108_CTRL_CMD + 1];
uint8_t cmd;
uint8_t cmd_status;
uint8_t cmd_err;
int ret;
DEBUGASSERT(last_reg - start_reg + 1 == sizeof(readbuf));
/* Multi-byte read to get command status. */
ret = mbr3108_i2c_read(dev, start_reg, readbuf, sizeof(readbuf));
if (ret < 0)
{
mbr3108_dbg("cmd status get failed. ret=%d\n", ret);
return ret;
}
cmd = readbuf[MBR3108_CTRL_CMD - MBR3108_CTRL_CMD];
cmd_status = readbuf[MBR3108_CTRL_CMD_STATUS - MBR3108_CTRL_CMD];
cmd_err = readbuf[MBR3108_CTRL_CMD_ERR - MBR3108_CTRL_CMD];
mbr3108_dbg("cmd: %d, status: %d, err: %d\n", cmd, cmd_status, cmd_err);
if (cmd != MBR3108_CMD_COMPLETED)
{
return -EBUSY;
}
if ((cmd_status & MBR3108_CMD_STATUS_MASK) == MBR3108_CMD_STATUS_SUCCESS)
{
/* Success. */
return 0;
}
return cmd_err;
}
static int mbr3108_save_check_crc(FAR struct mbr3108_dev_s *dev)
{
uint8_t reg = MBR3108_CTRL_CMD;
uint8_t cmd = MBR3108_CMD_CHECK_CONFIG_CRC;
int ret;
ret = mbr3108_i2c_write(dev, reg, &cmd, 1);
if (ret < 0)
{
mbr3108_dbg("MBR3108_CTRL_CMD:CHECK_CONFIG_CRC write failed.\n");
return ret;
}
nxsig_usleep(MBR3108_CMD_MSECS_CHECK_CONFIG_CRC * 1000);
ret = mbr3108_check_cmd_status(dev);
if (ret != 0)
{
return ret < 0 ? ret : -EIO;
}
return 0;
}
static int mbr3108_software_reset(FAR struct mbr3108_dev_s *dev)
{
uint8_t reg = MBR3108_CTRL_CMD;
uint8_t cmd = MBR3108_CMD_SOFTWARE_RESET;
int ret;
ret = mbr3108_i2c_write(dev, reg, &cmd, 1);
if (ret < 0)
{
mbr3108_dbg("MBR3108_CTRL_CMD:SOFTWARE_RESET write failed.\n");
return ret;
}
nxsig_usleep(MBR3108_CMD_MSECS_SOFTWARE_RESET * 1000);
ret = mbr3108_check_cmd_status(dev);
if (ret != 0)
{
return ret < 0 ? ret : -EIO;
}
return 0;
}
static int mbr3108_enter_low_power_mode(FAR struct mbr3108_dev_s *dev)
{
uint8_t reg = MBR3108_CTRL_CMD;
uint8_t cmd = MBR3108_CMD_ENTER_LOW_POWER_MODE;
int ret;
ret = mbr3108_i2c_write(dev, reg, &cmd, 1);
if (ret < 0)
{
mbr3108_dbg("MBR3108_CTRL_CMD:SOFTWARE_RESET write failed.\n");
return ret;
}
/* Device is now in low-power mode and not scanning. Further communication
* will cause wake-up and make chip resume scanning operations.
*/
return 0;
}
static int mbr3108_clear_latched(FAR struct mbr3108_dev_s *dev)
{
uint8_t reg = MBR3108_CTRL_CMD;
uint8_t cmd = MBR3108_CMD_CLEAR_LATCHED;
int ret;
ret = mbr3108_i2c_write(dev, reg, &cmd, 1);
if (ret < 0)
{
mbr3108_dbg("MBR3108_CTRL_CMD: "
"MBR3108_CMD_CLEAR_LATCHED write failed.\n");
return ret;
}
nxsig_usleep(MBR3108_CMD_MSECS_CLEAR_LATCHED * 1000);
ret = mbr3108_check_cmd_status(dev);
if (ret != 0)
{
return ret < 0 ? ret : -EIO;
}
return 0;
}
static int mbr3108_debug_setup(FAR struct mbr3108_dev_s *dev,
FAR const struct mbr3108_debug_conf_s *conf)
{
uint8_t reg = MBR3108_SENSOR_ID;
int ret;
/* Store new debug configuration. */
dev->debug_conf = *conf;
if (!conf->debug_mode)
{
return 0;
}
/* Setup debug sensor id. */
ret = mbr3108_i2c_write(dev, reg, &conf->debug_sensor_id, 1);
if (ret < 0)
{
mbr3108_dbg("MBR3108_SENSOR_ID write failed.\n");
dev->debug_conf.debug_mode = false;
}
return ret;
}
static int
mbr3108_device_configuration(FAR struct mbr3108_dev_s *dev,
FAR const struct mbr3108_sensor_conf_s *conf)
{
const uint8_t start_reg = MBR3108_SENSOR_EN;
const uint8_t last_reg = MBR3108_CONFIG_CRC + 1;
uint8_t value;
int ret = 0;
DEBUGASSERT(sizeof(conf->conf_data) == last_reg - start_reg + 1);
ret = mbr3108_i2c_read(dev, MBR3108_CTRL_CMD, &value, 1);
if (ret < 0)
{
mbr3108_dbg("MBR3108_CTRL_CMD read failed.\n");
return ret;
}
if (value != MBR3108_CMD_COMPLETED)
{
/* Device is busy processing previous command. */
return -EBUSY;
}
ret = mbr3108_i2c_write(dev, start_reg, conf->conf_data,
last_reg - start_reg + 1);
if (ret < 0)
{
mbr3108_dbg("MBR3108 configuration write failed.\n");
return ret;
}
ret = mbr3108_save_check_crc(dev);
if (ret < 0)
{
mbr3108_dbg("MBR3108 save check CRC failed. ret=%d\n", ret);
return ret;
}
ret = mbr3108_software_reset(dev);
if (ret < 0)
{
mbr3108_dbg("MBR3108 software reset failed.\n");
return ret;
}
dev->board->irq_enable(dev->board, true);
return 0;
}
static int mbr3108_get_sensor_status(FAR struct mbr3108_dev_s *dev,
FAR void *buf)
{
struct mbr3108_sensor_status_s status =
{
};
const uint8_t start_reg = MBR3108_BUTTON_STAT;
const uint8_t last_reg = MBR3108_LATCHED_PROX_STAT;
uint8_t readbuf[MBR3108_LATCHED_PROX_STAT - MBR3108_BUTTON_STAT + 1];
int ret;
DEBUGASSERT(last_reg - start_reg + 1 == sizeof(readbuf));
/* Attempt to sensor status registers. */
ret = mbr3108_i2c_read(dev, start_reg, readbuf, sizeof(readbuf));
if (ret < 0)
{
mbr3108_dbg("Sensor status read failed.\n");
return ret;
}
status.button =
(readbuf[MBR3108_BUTTON_STAT + 0 - start_reg]) |
(readbuf[MBR3108_BUTTON_STAT + 1 - start_reg] << 8);
status.proximity =
readbuf[MBR3108_PROX_STAT - start_reg];
status.latched_button =
(readbuf[MBR3108_LATCHED_BUTTON_STAT + 0 - start_reg]) |
(readbuf[MBR3108_LATCHED_BUTTON_STAT + 1 - start_reg] << 8);
status.latched_proximity =
readbuf[MBR3108_LATCHED_PROX_STAT - start_reg];
memcpy(buf, &status, sizeof(status));
mbr3108_dbg("but: %x, prox: %x; latched[btn: %x, prox: %x]\n",
status.button, status.proximity, status.latched_button,
status.latched_button);
return 0;
}
static int mbr3108_get_sensor_debug_data(FAR struct mbr3108_dev_s *dev,
FAR void *buf)
{
struct mbr3108_sensor_debug_s data =
{
};
const uint8_t start_reg = MBR3108_SYNC_COUNTER1;
const uint8_t last_reg = MBR3108_SYNC_COUNTER2;
uint8_t readbuf[MBR3108_SYNC_COUNTER2 - MBR3108_SYNC_COUNTER1 + 1];
uint8_t sync1;
uint8_t sync2;
int ret;
int retries;
DEBUGASSERT(last_reg - start_reg + 1 == sizeof(readbuf));
for (retries = MBR3108_SYNC_RETRIES; retries > 0; retries--)
{
ret = mbr3108_i2c_read(dev, start_reg, readbuf, sizeof(readbuf));
if (ret < 0)
{
mbr3108_dbg("Sensor debug data read failed.\n");
return ret;
}
/* Sync counters need to match. */
sync1 = readbuf[MBR3108_SYNC_COUNTER1 - start_reg];
sync2 = readbuf[MBR3108_SYNC_COUNTER2 - start_reg];
if (sync1 == sync2)
{
break;
}
}
if (retries == 0)
{
return -EIO;
}
data.sensor_average_counts =
(readbuf[MBR3108_DEBUG_AVG_RAW_COUNT0 + 0 - start_reg]) |
(readbuf[MBR3108_DEBUG_AVG_RAW_COUNT0 + 1 - start_reg] << 8);
data.sensor_baseline =
(readbuf[MBR3108_DEBUG_BASELINE0 + 0 - start_reg]) |
(readbuf[MBR3108_DEBUG_BASELINE0 + 1 - start_reg] << 8);
data.sensor_diff_counts =
(readbuf[MBR3108_DEBUG_DIFFERENCE_COUNT0 + 0 - start_reg]) |
(readbuf[MBR3108_DEBUG_DIFFERENCE_COUNT0 + 1 - start_reg] << 8);
data.sensor_raw_counts =
(readbuf[MBR3108_DEBUG_RAW_COUNT0 + 0 - start_reg]) |
(readbuf[MBR3108_DEBUG_RAW_COUNT0 + 1 - start_reg] << 8);
data.sensor_total_capacitance = readbuf[MBR3108_DEBUG_CP - start_reg];
memcpy(buf, &data, sizeof(data));
mbr3108_dbg("avg_cnt: %d, baseline: %d, diff_cnt: %d, raw_cnt: %d, "
"total_cp: %d\n",
data.sensor_average_counts, data.sensor_baseline,
data.sensor_diff_counts, data.sensor_raw_counts,
data.sensor_total_capacitance);
return 0;
}
static int mbr3108_probe_device(FAR struct mbr3108_dev_s *dev)
{
const uint8_t start_reg = MBR3108_FAMILY_ID;
const uint8_t last_reg = MBR3108_DEVICE_REV;
uint8_t readbuf[MBR3108_DEVICE_REV - MBR3108_FAMILY_ID + 1];
uint8_t fam_id;
uint16_t dev_id;
uint8_t dev_rev;
int ret;
DEBUGASSERT(last_reg - start_reg + 1 == sizeof(readbuf));
/* Attempt to read device identification registers with multi-byte
* read.
*/
ret = mbr3108_i2c_read(dev, start_reg, readbuf, sizeof(readbuf));
if (ret < 0)
{
/* Failed to read registers from device. */
mbr3108_dbg("Probe failed.\n");
return ret;
}
/* Check result. */
fam_id = readbuf[MBR3108_FAMILY_ID - start_reg];
dev_id = (readbuf[MBR3108_DEVICE_ID + 0 - start_reg]) |
(readbuf[MBR3108_DEVICE_ID + 1 - start_reg] << 8);
dev_rev = readbuf[MBR3108_DEVICE_REV - start_reg];
mbr3108_dbg("family_id: 0x%02x, device_id: 0x%04x, device_rev: %d\n",
fam_id, dev_id, dev_rev);
if (fam_id != MBR3108_EXPECTED_FAMILY_ID ||
dev_id != MBR3108_EXPECTED_DEVICE_ID ||
dev_rev != MBR3108_EXPECTED_DEVICE_REV)
{
mbr3108_dbg("Probe failed, dev-id mismatch!\n");
mbr3108_dbg(" Expected: family_id: 0x%02x, device_id: "
"0x%04x, device_rev: %d\n",
MBR3108_EXPECTED_FAMILY_ID,
MBR3108_EXPECTED_DEVICE_ID,
MBR3108_EXPECTED_DEVICE_REV);
return -ENXIO;
}
return 0;
}
static ssize_t mbr3108_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct inode *inode;
FAR struct mbr3108_dev_s *priv;
size_t outlen;
irqstate_t flags;
int ret;
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
priv = inode->i_private;
ret = nxmutex_lock(&priv->devlock);
if (ret < 0)
{
return ret;
}
ret = -EINVAL;
if (priv->debug_conf.debug_mode)
{
outlen = sizeof(struct mbr3108_sensor_debug_s);
if (buflen >= outlen)
{
ret = mbr3108_get_sensor_debug_data(priv, buffer);
}
}
else
{
outlen = sizeof(struct mbr3108_sensor_status_s);
if (buflen >= outlen)
{
ret = mbr3108_get_sensor_status(priv, buffer);
}
}
flags = enter_critical_section();
priv->int_pending = false;
leave_critical_section(flags);
nxmutex_unlock(&priv->devlock);
return ret < 0 ? ret : outlen;
}
static ssize_t mbr3108_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
FAR struct inode *inode;
FAR struct mbr3108_dev_s *priv;
enum mbr3108_cmd_e type;
int ret;
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
priv = inode->i_private;
if (buflen < sizeof(enum mbr3108_cmd_e))
{
return -EINVAL;
}
ret = nxmutex_lock(&priv->devlock);
if (ret < 0)
{
return ret;
}
type = *(FAR const enum mbr3108_cmd_e *)buffer;
switch (type)
{
case CYPRESS_MBR3108_CMD_SENSOR_CONF:
{
FAR const struct mbr3108_cmd_sensor_conf_s *conf =
(FAR const struct mbr3108_cmd_sensor_conf_s *)buffer;
if (buflen != sizeof(*conf))
{
ret = -EINVAL;
goto out;
}
ret = mbr3108_device_configuration(priv, &conf->conf);
break;
}
case CYPRESS_MBR3108_CMD_DEBUG_CONF:
{
FAR const struct mbr3108_cmd_debug_conf_s *conf =
(FAR const struct mbr3108_cmd_debug_conf_s *)buffer;
if (buflen != sizeof(*conf))
{
ret = -EINVAL;
goto out;
}
ret = mbr3108_debug_setup(priv, &conf->conf);
break;
}
case CYPRESS_MBR3108_CMD_CLEAR_LATCHED:
{
if (buflen != sizeof(type))
{
ret = -EINVAL;
goto out;
}
ret = mbr3108_clear_latched(priv);
break;
}
default:
ret = -EINVAL;
break;
}
out:
nxmutex_unlock(&priv->devlock);
return ret < 0 ? ret : buflen;
}
static int mbr3108_open(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct mbr3108_dev_s *priv;
unsigned int use_count;
int ret;
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
priv = inode->i_private;
ret = nxmutex_lock(&priv->devlock);
if (ret < 0)
{
return ret;
}
use_count = priv->cref + 1;
if (use_count == 1)
{
/* First user, do power on. */
ret = priv->board->set_power(priv->board, true);
if (ret < 0)
{
goto out_lock;
}
/* Let chip to power up before probing */
nxsig_usleep(100 * 1000);
/* Check that device exists on I2C. */
ret = mbr3108_probe_device(priv);
if (ret < 0)
{
/* No such device. Power off the switch. */
priv->board->set_power(priv->board, false);
goto out_lock;
}
if (priv->sensor_conf)
{
/* Do configuration. */
ret = mbr3108_device_configuration(priv, priv->sensor_conf);
if (ret < 0)
{
/* Configuration failed. Power off the switch. */
priv->board->set_power(priv->board, false);
goto out_lock;
}
}
priv->cref = use_count;
}
else
{
DEBUGASSERT(use_count < UINT8_MAX && use_count > priv->cref);
priv->cref = use_count;
ret = 0;
}
out_lock:
nxmutex_unlock(&priv->devlock);
return ret;
}
static int mbr3108_close(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct mbr3108_dev_s *priv;
int use_count;
int ret;
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
priv = inode->i_private;
ret = nxmutex_lock(&priv->devlock);
if (ret < 0)
{
return ret;
}
use_count = priv->cref - 1;
if (use_count == 0)
{
/* Disable interrupt */
priv->board->irq_enable(priv->board, false);
/* Set chip in low-power mode. */
mbr3108_enter_low_power_mode(priv);
/* Last user, do power off. */
priv->board->set_power(priv->board, false);
priv->debug_conf.debug_mode = false;
priv->cref = use_count;
}
else
{
DEBUGASSERT(use_count > 0);
priv->cref = use_count;
}
nxmutex_unlock(&priv->devlock);
return 0;
}
static int mbr3108_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup)
{
FAR struct mbr3108_dev_s *priv;
FAR struct inode *inode;
bool pending;