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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
|
// SPDX-License-Identifier: GPL-2.0+
/*
* VEML6030 and VMEL6035 Ambient Light Sensors
*
* Copyright (c) 2019, Rishi Gupta <gupt21@gmail.com>
*
* VEML6030:
* Datasheet: https://www.vishay.com/docs/84366/veml6030.pdf
* Appnote-84367: https://www.vishay.com/docs/84367/designingveml6030.pdf
*
* VEML6035:
* Datasheet: https://www.vishay.com/docs/84889/veml6035.pdf
* Appnote-84944: https://www.vishay.com/docs/84944/designingveml6035.pdf
*/
#include <linux/bitfield.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/interrupt.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/events.h>
/* Device registers */
#define VEML6030_REG_ALS_CONF 0x00
#define VEML6030_REG_ALS_WH 0x01
#define VEML6030_REG_ALS_WL 0x02
#define VEML6030_REG_ALS_PSM 0x03
#define VEML6030_REG_ALS_DATA 0x04
#define VEML6030_REG_WH_DATA 0x05
#define VEML6030_REG_ALS_INT 0x06
/* Bit masks for specific functionality */
#define VEML6030_ALS_IT GENMASK(9, 6)
#define VEML6030_PSM GENMASK(2, 1)
#define VEML6030_ALS_PERS GENMASK(5, 4)
#define VEML6030_ALS_GAIN GENMASK(12, 11)
#define VEML6030_PSM_EN BIT(0)
#define VEML6030_INT_TH_LOW BIT(15)
#define VEML6030_INT_TH_HIGH BIT(14)
#define VEML6030_ALS_INT_EN BIT(1)
#define VEML6030_ALS_SD BIT(0)
#define VEML6035_GAIN_M GENMASK(12, 10)
#define VEML6035_GAIN BIT(10)
#define VEML6035_DG BIT(11)
#define VEML6035_SENS BIT(12)
#define VEML6035_INT_CHAN BIT(3)
#define VEML6035_CHAN_EN BIT(2)
struct veml603x_chip {
const char *name;
const int(*scale_vals)[][2];
const int num_scale_vals;
const struct iio_info *info;
const struct iio_info *info_no_irq;
int (*hw_init)(struct iio_dev *indio_dev, struct device *dev);
int (*set_als_gain)(struct iio_dev *indio_dev, int val, int val2);
int (*get_als_gain)(struct iio_dev *indio_dev, int *val, int *val2);
};
/*
* The resolution depends on both gain and integration time. The
* cur_resolution stores one of the resolution mentioned in the
* table during startup and gets updated whenever integration time
* or gain is changed.
*
* Table 'resolution and maximum detection range' in the appnotes
* is visualized as a 2D array. The cur_gain stores index of gain
* in this table (0-3 for VEML6030, 0-5 for VEML6035) while the
* cur_integration_time holds index of integration time (0-5).
*/
struct veml6030_data {
struct i2c_client *client;
struct regmap *regmap;
int cur_resolution;
int cur_gain;
int cur_integration_time;
const struct veml603x_chip *chip;
};
static const int veml6030_it_times[][2] = {
{ 0, 25000 },
{ 0, 50000 },
{ 0, 100000 },
{ 0, 200000 },
{ 0, 400000 },
{ 0, 800000 },
};
/*
* Scale is 1/gain. Value 0.125 is ALS gain x (1/8), 0.25 is
* ALS gain x (1/4), 0.5 is ALS gain x (1/2), 1.0 is ALS gain x 1,
* 2.0 is ALS gain x2, and 4.0 is ALS gain x 4.
*/
static const int veml6030_scale_vals[][2] = {
{ 0, 125000 },
{ 0, 250000 },
{ 1, 0 },
{ 2, 0 },
};
static const int veml6035_scale_vals[][2] = {
{ 0, 125000 },
{ 0, 250000 },
{ 0, 500000 },
{ 1, 0 },
{ 2, 0 },
{ 4, 0 },
};
/*
* Persistence = 1/2/4/8 x integration time
* Minimum time for which light readings must stay above configured
* threshold to assert the interrupt.
*/
static const char * const period_values[] = {
"0.1 0.2 0.4 0.8",
"0.2 0.4 0.8 1.6",
"0.4 0.8 1.6 3.2",
"0.8 1.6 3.2 6.4",
"0.05 0.1 0.2 0.4",
"0.025 0.050 0.1 0.2"
};
/*
* Return list of valid period values in seconds corresponding to
* the currently active integration time.
*/
static ssize_t in_illuminance_period_available_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int ret, reg, x;
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
struct veml6030_data *data = iio_priv(indio_dev);
ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als conf register %d\n", ret);
return ret;
}
ret = ((reg >> 6) & 0xF);
switch (ret) {
case 0:
case 1:
case 2:
case 3:
x = ret;
break;
case 8:
x = 4;
break;
case 12:
x = 5;
break;
default:
return -EINVAL;
}
return sysfs_emit(buf, "%s\n", period_values[x]);
}
static IIO_DEVICE_ATTR_RO(in_illuminance_period_available, 0);
static struct attribute *veml6030_event_attributes[] = {
&iio_dev_attr_in_illuminance_period_available.dev_attr.attr,
NULL
};
static const struct attribute_group veml6030_event_attr_group = {
.attrs = veml6030_event_attributes,
};
static int veml6030_als_pwr_on(struct veml6030_data *data)
{
int ret;
ret = regmap_clear_bits(data->regmap, VEML6030_REG_ALS_CONF,
VEML6030_ALS_SD);
if (ret)
return ret;
/* Wait 4 ms to let processor & oscillator start correctly */
fsleep(4000);
return 0;
}
static int veml6030_als_shut_down(struct veml6030_data *data)
{
return regmap_set_bits(data->regmap, VEML6030_REG_ALS_CONF,
VEML6030_ALS_SD);
}
static void veml6030_als_shut_down_action(void *data)
{
veml6030_als_shut_down(data);
}
static const struct iio_event_spec veml6030_event_spec[] = {
{
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_RISING,
.mask_separate = BIT(IIO_EV_INFO_VALUE),
}, {
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_FALLING,
.mask_separate = BIT(IIO_EV_INFO_VALUE),
}, {
.type = IIO_EV_TYPE_THRESH,
.dir = IIO_EV_DIR_EITHER,
.mask_separate = BIT(IIO_EV_INFO_PERIOD) |
BIT(IIO_EV_INFO_ENABLE),
},
};
/* Channel number */
enum veml6030_chan {
CH_ALS,
CH_WHITE,
};
static const struct iio_chan_spec veml6030_channels[] = {
{
.type = IIO_LIGHT,
.channel = CH_ALS,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_PROCESSED) |
BIT(IIO_CHAN_INFO_INT_TIME) |
BIT(IIO_CHAN_INFO_SCALE),
.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
BIT(IIO_CHAN_INFO_SCALE),
.event_spec = veml6030_event_spec,
.num_event_specs = ARRAY_SIZE(veml6030_event_spec),
},
{
.type = IIO_INTENSITY,
.channel = CH_WHITE,
.modified = 1,
.channel2 = IIO_MOD_LIGHT_BOTH,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_INT_TIME) |
BIT(IIO_CHAN_INFO_SCALE),
.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
BIT(IIO_CHAN_INFO_SCALE),
},
};
static const struct regmap_config veml6030_regmap_config = {
.name = "veml6030_regmap",
.reg_bits = 8,
.val_bits = 16,
.max_register = VEML6030_REG_ALS_INT,
.val_format_endian = REGMAP_ENDIAN_LITTLE,
};
static int veml6030_get_intgrn_tm(struct iio_dev *indio_dev,
int *val, int *val2)
{
int ret, reg;
struct veml6030_data *data = iio_priv(indio_dev);
ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als conf register %d\n", ret);
return ret;
}
switch ((reg >> 6) & 0xF) {
case 0:
*val2 = 100000;
break;
case 1:
*val2 = 200000;
break;
case 2:
*val2 = 400000;
break;
case 3:
*val2 = 800000;
break;
case 8:
*val2 = 50000;
break;
case 12:
*val2 = 25000;
break;
default:
return -EINVAL;
}
*val = 0;
return IIO_VAL_INT_PLUS_MICRO;
}
static int veml6030_set_intgrn_tm(struct iio_dev *indio_dev,
int val, int val2)
{
int ret, new_int_time, int_idx;
struct veml6030_data *data = iio_priv(indio_dev);
if (val)
return -EINVAL;
switch (val2) {
case 25000:
new_int_time = 0x300;
int_idx = 5;
break;
case 50000:
new_int_time = 0x200;
int_idx = 4;
break;
case 100000:
new_int_time = 0x00;
int_idx = 3;
break;
case 200000:
new_int_time = 0x40;
int_idx = 2;
break;
case 400000:
new_int_time = 0x80;
int_idx = 1;
break;
case 800000:
new_int_time = 0xC0;
int_idx = 0;
break;
default:
return -EINVAL;
}
ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
VEML6030_ALS_IT, new_int_time);
if (ret) {
dev_err(&data->client->dev,
"can't update als integration time %d\n", ret);
return ret;
}
/*
* Cache current integration time and update resolution. For every
* increase in integration time to next level, resolution is halved
* and vice-versa.
*/
if (data->cur_integration_time < int_idx)
data->cur_resolution <<= int_idx - data->cur_integration_time;
else if (data->cur_integration_time > int_idx)
data->cur_resolution >>= data->cur_integration_time - int_idx;
data->cur_integration_time = int_idx;
return ret;
}
static int veml6030_read_persistence(struct iio_dev *indio_dev,
int *val, int *val2)
{
int ret, reg, period, x, y;
struct veml6030_data *data = iio_priv(indio_dev);
ret = veml6030_get_intgrn_tm(indio_dev, &x, &y);
if (ret < 0)
return ret;
ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als conf register %d\n", ret);
}
/* integration time multiplied by 1/2/4/8 */
period = y * (1 << ((reg >> 4) & 0x03));
*val = period / 1000000;
*val2 = period % 1000000;
return IIO_VAL_INT_PLUS_MICRO;
}
static int veml6030_write_persistence(struct iio_dev *indio_dev,
int val, int val2)
{
int ret, period, x, y;
struct veml6030_data *data = iio_priv(indio_dev);
ret = veml6030_get_intgrn_tm(indio_dev, &x, &y);
if (ret < 0)
return ret;
if (!val) {
period = val2 / y;
} else {
if ((val == 1) && (val2 == 600000))
period = 1600000 / y;
else if ((val == 3) && (val2 == 200000))
period = 3200000 / y;
else if ((val == 6) && (val2 == 400000))
period = 6400000 / y;
else
period = -1;
}
if (period <= 0 || period > 8 || hweight8(period) != 1)
return -EINVAL;
ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
VEML6030_ALS_PERS, (ffs(period) - 1) << 4);
if (ret)
dev_err(&data->client->dev,
"can't set persistence value %d\n", ret);
return ret;
}
/*
* Cache currently set gain & update resolution. For every
* increase in the gain to next level, resolution is halved
* and vice-versa.
*/
static void veml6030_update_gain_res(struct veml6030_data *data, int gain_idx)
{
if (data->cur_gain < gain_idx)
data->cur_resolution <<= gain_idx - data->cur_gain;
else if (data->cur_gain > gain_idx)
data->cur_resolution >>= data->cur_gain - gain_idx;
data->cur_gain = gain_idx;
}
static int veml6030_set_als_gain(struct iio_dev *indio_dev,
int val, int val2)
{
int ret, new_gain, gain_idx;
struct veml6030_data *data = iio_priv(indio_dev);
if (val == 0 && val2 == 125000) {
new_gain = 0x1000; /* 0x02 << 11 */
gain_idx = 3;
} else if (val == 0 && val2 == 250000) {
new_gain = 0x1800;
gain_idx = 2;
} else if (val == 1 && val2 == 0) {
new_gain = 0x00;
gain_idx = 1;
} else if (val == 2 && val2 == 0) {
new_gain = 0x800;
gain_idx = 0;
} else {
return -EINVAL;
}
ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
VEML6030_ALS_GAIN, new_gain);
if (ret) {
dev_err(&data->client->dev,
"can't set als gain %d\n", ret);
return ret;
}
veml6030_update_gain_res(data, gain_idx);
return 0;
}
static int veml6035_set_als_gain(struct iio_dev *indio_dev, int val, int val2)
{
int ret, new_gain, gain_idx;
struct veml6030_data *data = iio_priv(indio_dev);
if (val == 0 && val2 == 125000) {
new_gain = VEML6035_SENS;
gain_idx = 5;
} else if (val == 0 && val2 == 250000) {
new_gain = VEML6035_SENS | VEML6035_GAIN;
gain_idx = 4;
} else if (val == 0 && val2 == 500000) {
new_gain = VEML6035_SENS | VEML6035_GAIN |
VEML6035_DG;
gain_idx = 3;
} else if (val == 1 && val2 == 0) {
new_gain = 0x0000;
gain_idx = 2;
} else if (val == 2 && val2 == 0) {
new_gain = VEML6035_GAIN;
gain_idx = 1;
} else if (val == 4 && val2 == 0) {
new_gain = VEML6035_GAIN | VEML6035_DG;
gain_idx = 0;
} else {
return -EINVAL;
}
ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
VEML6035_GAIN_M, new_gain);
if (ret) {
dev_err(&data->client->dev, "can't set als gain %d\n", ret);
return ret;
}
veml6030_update_gain_res(data, gain_idx);
return 0;
}
static int veml6030_get_als_gain(struct iio_dev *indio_dev,
int *val, int *val2)
{
int ret, reg;
struct veml6030_data *data = iio_priv(indio_dev);
ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als conf register %d\n", ret);
return ret;
}
switch ((reg >> 11) & 0x03) {
case 0:
*val = 1;
*val2 = 0;
break;
case 1:
*val = 2;
*val2 = 0;
break;
case 2:
*val = 0;
*val2 = 125000;
break;
case 3:
*val = 0;
*val2 = 250000;
break;
default:
return -EINVAL;
}
return IIO_VAL_INT_PLUS_MICRO;
}
static int veml6035_get_als_gain(struct iio_dev *indio_dev, int *val, int *val2)
{
int ret, reg;
struct veml6030_data *data = iio_priv(indio_dev);
ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als conf register %d\n", ret);
return ret;
}
switch (FIELD_GET(VEML6035_GAIN_M, reg)) {
case 0:
*val = 1;
*val2 = 0;
break;
case 1:
case 2:
*val = 2;
*val2 = 0;
break;
case 3:
*val = 4;
*val2 = 0;
break;
case 4:
*val = 0;
*val2 = 125000;
break;
case 5:
case 6:
*val = 0;
*val2 = 250000;
break;
case 7:
*val = 0;
*val2 = 500000;
break;
default:
return -EINVAL;
}
return IIO_VAL_INT_PLUS_MICRO;
}
static int veml6030_read_thresh(struct iio_dev *indio_dev,
int *val, int *val2, int dir)
{
int ret, reg;
struct veml6030_data *data = iio_priv(indio_dev);
if (dir == IIO_EV_DIR_RISING)
ret = regmap_read(data->regmap, VEML6030_REG_ALS_WH, ®);
else
ret = regmap_read(data->regmap, VEML6030_REG_ALS_WL, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als threshold value %d\n", ret);
return ret;
}
*val = reg & 0xffff;
return IIO_VAL_INT;
}
static int veml6030_write_thresh(struct iio_dev *indio_dev,
int val, int val2, int dir)
{
int ret;
struct veml6030_data *data = iio_priv(indio_dev);
if (val > 0xFFFF || val < 0 || val2)
return -EINVAL;
if (dir == IIO_EV_DIR_RISING) {
ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, val);
if (ret)
dev_err(&data->client->dev,
"can't set high threshold %d\n", ret);
} else {
ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, val);
if (ret)
dev_err(&data->client->dev,
"can't set low threshold %d\n", ret);
}
return ret;
}
/*
* Provide both raw as well as light reading in lux.
* light (in lux) = resolution * raw reading
*/
static int veml6030_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val,
int *val2, long mask)
{
int ret, reg;
struct veml6030_data *data = iio_priv(indio_dev);
struct regmap *regmap = data->regmap;
struct device *dev = &data->client->dev;
switch (mask) {
case IIO_CHAN_INFO_RAW:
case IIO_CHAN_INFO_PROCESSED:
switch (chan->type) {
case IIO_LIGHT:
ret = regmap_read(regmap, VEML6030_REG_ALS_DATA, ®);
if (ret < 0) {
dev_err(dev, "can't read als data %d\n", ret);
return ret;
}
if (mask == IIO_CHAN_INFO_PROCESSED) {
*val = (reg * data->cur_resolution) / 10000;
*val2 = (reg * data->cur_resolution) % 10000;
return IIO_VAL_INT_PLUS_MICRO;
}
*val = reg;
return IIO_VAL_INT;
case IIO_INTENSITY:
ret = regmap_read(regmap, VEML6030_REG_WH_DATA, ®);
if (ret < 0) {
dev_err(dev, "can't read white data %d\n", ret);
return ret;
}
*val = reg;
return IIO_VAL_INT;
default:
return -EINVAL;
}
case IIO_CHAN_INFO_INT_TIME:
return veml6030_get_intgrn_tm(indio_dev, val, val2);
case IIO_CHAN_INFO_SCALE:
return data->chip->get_als_gain(indio_dev, val, val2);
default:
return -EINVAL;
}
}
static int veml6030_read_avail(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
const int **vals, int *type, int *length,
long mask)
{
struct veml6030_data *data = iio_priv(indio_dev);
switch (mask) {
case IIO_CHAN_INFO_INT_TIME:
*vals = (int *)&veml6030_it_times;
*length = 2 * ARRAY_SIZE(veml6030_it_times);
*type = IIO_VAL_INT_PLUS_MICRO;
return IIO_AVAIL_LIST;
case IIO_CHAN_INFO_SCALE:
*vals = (int *)*data->chip->scale_vals;
*length = 2 * data->chip->num_scale_vals;
*type = IIO_VAL_INT_PLUS_MICRO;
return IIO_AVAIL_LIST;
}
return -EINVAL;
}
static int veml6030_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val, int val2, long mask)
{
struct veml6030_data *data = iio_priv(indio_dev);
switch (mask) {
case IIO_CHAN_INFO_INT_TIME:
return veml6030_set_intgrn_tm(indio_dev, val, val2);
case IIO_CHAN_INFO_SCALE:
return data->chip->set_als_gain(indio_dev, val, val2);
default:
return -EINVAL;
}
}
static int veml6030_read_event_val(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, enum iio_event_type type,
enum iio_event_direction dir, enum iio_event_info info,
int *val, int *val2)
{
switch (info) {
case IIO_EV_INFO_VALUE:
switch (dir) {
case IIO_EV_DIR_RISING:
case IIO_EV_DIR_FALLING:
return veml6030_read_thresh(indio_dev, val, val2, dir);
default:
return -EINVAL;
}
break;
case IIO_EV_INFO_PERIOD:
return veml6030_read_persistence(indio_dev, val, val2);
default:
return -EINVAL;
}
}
static int veml6030_write_event_val(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, enum iio_event_type type,
enum iio_event_direction dir, enum iio_event_info info,
int val, int val2)
{
switch (info) {
case IIO_EV_INFO_VALUE:
return veml6030_write_thresh(indio_dev, val, val2, dir);
case IIO_EV_INFO_PERIOD:
return veml6030_write_persistence(indio_dev, val, val2);
default:
return -EINVAL;
}
}
static int veml6030_read_interrupt_config(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, enum iio_event_type type,
enum iio_event_direction dir)
{
int ret, reg;
struct veml6030_data *data = iio_priv(indio_dev);
ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als conf register %d\n", ret);
return ret;
}
if (reg & VEML6030_ALS_INT_EN)
return 1;
else
return 0;
}
/*
* Sensor should not be measuring light when interrupt is configured.
* Therefore correct sequence to configure interrupt functionality is:
* shut down -> enable/disable interrupt -> power on
*
* state = 1 enables interrupt, state = 0 disables interrupt
*/
static int veml6030_write_interrupt_config(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, enum iio_event_type type,
enum iio_event_direction dir, int state)
{
int ret;
struct veml6030_data *data = iio_priv(indio_dev);
if (state < 0 || state > 1)
return -EINVAL;
ret = veml6030_als_shut_down(data);
if (ret < 0) {
dev_err(&data->client->dev,
"can't disable als to configure interrupt %d\n", ret);
return ret;
}
/* enable interrupt + power on */
ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
VEML6030_ALS_INT_EN | VEML6030_ALS_SD, state << 1);
if (ret)
dev_err(&data->client->dev,
"can't enable interrupt & poweron als %d\n", ret);
return ret;
}
static const struct iio_info veml6030_info = {
.read_raw = veml6030_read_raw,
.read_avail = veml6030_read_avail,
.write_raw = veml6030_write_raw,
.read_event_value = veml6030_read_event_val,
.write_event_value = veml6030_write_event_val,
.read_event_config = veml6030_read_interrupt_config,
.write_event_config = veml6030_write_interrupt_config,
.event_attrs = &veml6030_event_attr_group,
};
static const struct iio_info veml6035_info = {
.read_raw = veml6030_read_raw,
.read_avail = veml6030_read_avail,
.write_raw = veml6030_write_raw,
.read_event_value = veml6030_read_event_val,
.write_event_value = veml6030_write_event_val,
.read_event_config = veml6030_read_interrupt_config,
.write_event_config = veml6030_write_interrupt_config,
.event_attrs = &veml6030_event_attr_group,
};
static const struct iio_info veml6030_info_no_irq = {
.read_raw = veml6030_read_raw,
.read_avail = veml6030_read_avail,
.write_raw = veml6030_write_raw,
};
static const struct iio_info veml6035_info_no_irq = {
.read_raw = veml6030_read_raw,
.write_raw = veml6030_write_raw,
};
static irqreturn_t veml6030_event_handler(int irq, void *private)
{
int ret, reg, evtdir;
struct iio_dev *indio_dev = private;
struct veml6030_data *data = iio_priv(indio_dev);
ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, ®);
if (ret) {
dev_err(&data->client->dev,
"can't read als interrupt register %d\n", ret);
return IRQ_HANDLED;
}
/* Spurious interrupt handling */
if (!(reg & (VEML6030_INT_TH_HIGH | VEML6030_INT_TH_LOW)))
return IRQ_NONE;
if (reg & VEML6030_INT_TH_HIGH)
evtdir = IIO_EV_DIR_RISING;
else
evtdir = IIO_EV_DIR_FALLING;
iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
0, IIO_EV_TYPE_THRESH, evtdir),
iio_get_time_ns(indio_dev));
return IRQ_HANDLED;
}
/*
* Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2,
* persistence to 1 x integration time and the threshold
* interrupt disabled by default. First shutdown the sensor,
* update registers and then power on the sensor.
*/
static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev)
{
int ret, val;
struct veml6030_data *data = iio_priv(indio_dev);
ret = veml6030_als_shut_down(data);
if (ret)
return dev_err_probe(dev, ret, "can't shutdown als\n");
ret = regmap_write(data->regmap, VEML6030_REG_ALS_CONF, 0x1001);
if (ret)
return dev_err_probe(dev, ret, "can't setup als configs\n");
ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_PSM,
VEML6030_PSM | VEML6030_PSM_EN, 0x03);
if (ret)
return dev_err_probe(dev, ret, "can't setup default PSM\n");
ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, 0xFFFF);
if (ret)
return dev_err_probe(dev, ret, "can't setup high threshold\n");
ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, 0x0000);
if (ret)
return dev_err_probe(dev, ret, "can't setup low threshold\n");
ret = veml6030_als_pwr_on(data);
if (ret)
return dev_err_probe(dev, ret, "can't poweron als\n");
ret = devm_add_action_or_reset(dev, veml6030_als_shut_down_action, data);
if (ret < 0)
return ret;
/* Clear stale interrupt status bits if any during start */
ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, &val);
if (ret < 0)
return dev_err_probe(dev, ret,
"can't clear als interrupt status\n");
/* Cache currently active measurement parameters */
data->cur_gain = 3;
data->cur_resolution = 4608;
data->cur_integration_time = 3;
return ret;
}
/*
* Set ALS gain to 1/8, integration time to 100 ms, ALS and WHITE
* channel enabled, ALS channel interrupt, PSM enabled,
* PSM_WAIT = 0.8 s, persistence to 1 x integration time and the
* threshold interrupt disabled by default. First shutdown the sensor,
* update registers and then power on the sensor.
*/
static int veml6035_hw_init(struct iio_dev *indio_dev, struct device *dev)
{
int ret, val;
struct veml6030_data *data = iio_priv(indio_dev);
ret = veml6030_als_shut_down(data);
if (ret)
return dev_err_probe(dev, ret, "can't shutdown als\n");
ret = regmap_write(data->regmap, VEML6030_REG_ALS_CONF,
VEML6035_SENS | VEML6035_CHAN_EN | VEML6030_ALS_SD);
if (ret)
return dev_err_probe(dev, ret, "can't setup als configs\n");
ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_PSM,
VEML6030_PSM | VEML6030_PSM_EN, 0x03);
if (ret)
return dev_err_probe(dev, ret, "can't setup default PSM\n");
ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, 0xFFFF);
if (ret)
return dev_err_probe(dev, ret, "can't setup high threshold\n");
ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, 0x0000);
if (ret)
return dev_err_probe(dev, ret, "can't setup low threshold\n");
ret = veml6030_als_pwr_on(data);
if (ret)
return dev_err_probe(dev, ret, "can't poweron als\n");
ret = devm_add_action_or_reset(dev, veml6030_als_shut_down_action, data);
if (ret < 0)
return ret;
/* Clear stale interrupt status bits if any during start */
ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, &val);
if (ret < 0)
return dev_err_probe(dev, ret,
"can't clear als interrupt status\n");
/* Cache currently active measurement parameters */
data->cur_gain = 5;
data->cur_resolution = 1024;
data->cur_integration_time = 3;
return 0;
}
static int veml6030_probe(struct i2c_client *client)
{
int ret;
struct veml6030_data *data;
struct iio_dev *indio_dev;
struct regmap *regmap;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return dev_err_probe(&client->dev, -EOPNOTSUPP,
"i2c adapter doesn't support plain i2c\n");
regmap = devm_regmap_init_i2c(client, &veml6030_regmap_config);
if (IS_ERR(regmap))
return dev_err_probe(&client->dev, PTR_ERR(regmap),
"can't setup regmap\n");
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
if (!indio_dev)
return -ENOMEM;
data = iio_priv(indio_dev);
i2c_set_clientdata(client, indio_dev);
data->client = client;
data->regmap = regmap;
ret = devm_regulator_get_enable(&client->dev, "vdd");
if (ret)
return dev_err_probe(&client->dev, ret,
"failed to enable regulator\n");
data->chip = i2c_get_match_data(client);
if (!data->chip)
return -EINVAL;
indio_dev->name = data->chip->name;
indio_dev->channels = veml6030_channels;
indio_dev->num_channels = ARRAY_SIZE(veml6030_channels);
indio_dev->modes = INDIO_DIRECT_MODE;
if (client->irq) {
ret = devm_request_threaded_irq(&client->dev, client->irq,
NULL, veml6030_event_handler,
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
indio_dev->name, indio_dev);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
"irq %d request failed\n",
client->irq);
indio_dev->info = data->chip->info;
} else {
indio_dev->info = data->chip->info_no_irq;
}
ret = data->chip->hw_init(indio_dev, &client->dev);
if (ret < 0)
return ret;
return devm_iio_device_register(&client->dev, indio_dev);
}
static int veml6030_runtime_suspend(struct device *dev)
{
int ret;
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
struct veml6030_data *data = iio_priv(indio_dev);
ret = veml6030_als_shut_down(data);
if (ret < 0)
dev_err(&data->client->dev, "can't suspend als %d\n", ret);
return ret;
}
static int veml6030_runtime_resume(struct device *dev)
{
int ret;
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
struct veml6030_data *data = iio_priv(indio_dev);
ret = veml6030_als_pwr_on(data);
if (ret < 0)
dev_err(&data->client->dev, "can't resume als %d\n", ret);
return ret;
}
static DEFINE_RUNTIME_DEV_PM_OPS(veml6030_pm_ops, veml6030_runtime_suspend,
veml6030_runtime_resume, NULL);
static const struct veml603x_chip veml6030_chip = {
.name = "veml6030",
.scale_vals = &veml6030_scale_vals,
.num_scale_vals = ARRAY_SIZE(veml6030_scale_vals),
.info = &veml6030_info,
.info_no_irq = &veml6030_info_no_irq,
.hw_init = veml6030_hw_init,
.set_als_gain = veml6030_set_als_gain,
.get_als_gain = veml6030_get_als_gain,
};
static const struct veml603x_chip veml6035_chip = {
.name = "veml6035",
.scale_vals = &veml6035_scale_vals,
.num_scale_vals = ARRAY_SIZE(veml6035_scale_vals),
.info = &veml6035_info,
.info_no_irq = &veml6035_info_no_irq,
.hw_init = veml6035_hw_init,
.set_als_gain = veml6035_set_als_gain,
.get_als_gain = veml6035_get_als_gain,
};
static const struct of_device_id veml6030_of_match[] = {
{
.compatible = "vishay,veml6030",
.data = &veml6030_chip,
},
{
.compatible = "vishay,veml6035",
.data = &veml6035_chip,
},
{ }
};
MODULE_DEVICE_TABLE(of, veml6030_of_match);
static const struct i2c_device_id veml6030_id[] = {
{ "veml6030", (kernel_ulong_t)&veml6030_chip},
{ "veml6035", (kernel_ulong_t)&veml6035_chip},
{ }
};
MODULE_DEVICE_TABLE(i2c, veml6030_id);
static struct i2c_driver veml6030_driver = {
.driver = {
.name = "veml6030",
.of_match_table = veml6030_of_match,
.pm = pm_ptr(&veml6030_pm_ops),
},
.probe = veml6030_probe,
.id_table = veml6030_id,
};
module_i2c_driver(veml6030_driver);
MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
MODULE_DESCRIPTION("VEML6030 Ambient Light Sensor");
MODULE_LICENSE("GPL v2");
|