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
|
/* Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/* The NVS = NVidia Sensor framework */
/* This common NVS ALS module allows, along with the NVS IIO common module, an
* ALS driver to offload the code interacting with IIO and ALS reporting, and
* just have code that interacts with the HW.
* The commonality between this module and the NVS ALS driver is the nvs_light
* structure. It is expected that the NVS ALS driver will:
* - call nvs_light_enable when the device is enabled to initialize variables.
* - read the HW and place the value in nvs_light.hw
* - call nvs_light_read
* - depending on the nvs_light_read return value:
* - -1 = poll HW using nvs_light.poll_delay_ms delay.
* - 0 = if interrupt driven, do nothing or resume regular polling
* - 1 = set new thresholds using the nvs_light.hw_thresh_lo/hi
* Reporting the lux is handled within this module.
* See nvs_light.h for nvs_light structure details.
*/
/* The NVS HAL will use the IIO scale and offset sysfs attributes to modify the
* data using the following formula: (data * scale) + offset
* A scale value of 0 disables scale.
* A scale value of 1 puts the NVS HAL into calibration mode where the scale
* and offset are read everytime the data is read to allow realtime calibration
* of the scale and offset values to be used in the device tree parameters.
* Keep in mind the data is buffered but the NVS HAL will display the data and
* scale/offset parameters in the log. See calibration steps below.
*/
/* The configuration threshold values are HW value based. In other words, to
* obtain the upper and lower HW thresholds, the configuration threshold is
* simply added or subtracted from the HW data read, respectively.
* A little history about this: this code originally expected the configuration
* threshold values to be in lux. It then converted the threshold lux value to
* a HW value by reversing the calibrated value to uncalibrated and then the
* scaling of the resolution. The idea was to make configuration easy by just
* setting how much lux needed to change before reporting. However, there were
* two issues with this method:
* 1) that's a lot of overhead for each sample, and 2) the lux range isn't
* exactly linear. Lux values in a dark room will probably want to be reported
* every +/- 100 or 10 if not less. This is opposed to a bright room or even
* outside on a sunny day where lux value changes can be reported every
* +/- 1000 or even 10000. Since many ALS's have dynamic resolution, changing
* the range depending on the lux reading, it makes sense to use HW threshold
* values that will automatically scale with the HW resolution used.
*/
/* NVS light drivers have two calibration mechanisms. Method 1 is required if
* the driver is using dynamic resolution since the resolution cannot be read
* by the NVS HAL on every data value read due to buffering. So instead.a
* mechanism allows floating point to be calculated here in the kernel by
* shifting up to integer the floating point significant amount. This allows
* real-time resolution changes without the NVS HAL having to synchronize to
* the actual resolution for each datum. The scale.fval must be a 10 base
* value, e.g. 0.1, 0.01, ... 0.000001, etc. as the significant amount.
* The NVS HAL will then convert the value to float by multiplying the integer
* float-data with scale.
* Method 1:
* This method uses interpolation and requires a low and high uncalibrated
* value along with the corresponding low and high calibrated values. The
* uncalibrated values are what is read from the sensor in the steps below.
* The corresponding calibrated values are what the correct value should be.
* All values are programmed into the device tree settings.
* 1. Read scale sysfs attribute. This value will need to be written back.
* 2. Disable device. (Write 0 = buffer/enable sysfs attribute)
* 3. Write 1 to the scale sysfs attribute (e.g. in_illuminance_scale).
* 4. Enable device. (Write 1 = buffer/enable sysfs attribute)
* 5. The NVS HAL will announce in the 'logcat -v time | grep -i "sensors"'
* that calibration mode is enabled and
* display the data along with the scale and offset parameters applied.
* 6. Write the scale value read in step 1 back to the scale sysfs attribute.
* 7. Put the device into a state where the data read is a low value.
* (Generate light source < 100 lux.)
* 8. Note the values displayed in the log. Separately measure the actual
* value with a high accuracy light meter. Make sure the light meter
* is calibrated to the same color temperature as your light source.
* The value from the sensor will be the uncalibrated value and the
* separately measured value will be the calibrated value for the current
* state (low or high values).
* Device Tree nodes related to light calibration:
* light_uncalibrated_lo <-- Value found in logcat.
* light_calibrated_lo <-- Value read from light meter.
* 9. Put the device into a state where the data read is a high value.
* (Generate light source > 3000 lux.)
* 10. Repeat step 8.
* Device Tree nodes related to light calibration:
* light_uncalibrated_hi <-- Value found in logcat.
* light_calibrated_hi <-- Value read from light meter.
* 11. Enter the values in the device tree settings for the device. Both
* calibrated and uncalibrated values will be the values before scale and
* offset are applied.
* The light sensor has the following device tree parameters for this:
* light_uncalibrated_lo
* light_calibrated_lo
* light_uncalibrated_hi
* light_calibrated_hi
*
* To test calibration values in realtime.
* An NVS ALS driver may support a simplified version of method 1 that can be
* used in realtime:
* 1. At step 8, while the light source is still at this low value,
* write the light meter value to the in_illuminance_threshold_low
* attribute. When in calibration mode this value will be written to the
* light_calibrated_lo and the current lux written to light_uncalibrated_lo
* internal to the driver.
* 2. If this is after step 9, while the light source is still at this high
* value, write the light meter value to the in_illuminance_threshold_high
* attribute.
* 3. To confirm the realtime values and see what the driver used for
* uncalibrated values, do the following at the adb prompt in the driver
* space:
* # echo 5 > nvs
* # cat nvs
* This will be a partial dump of the sensor's configuration structure that
* will show the calibrated and uncalibrated values. For example:
* ...
* uncal_lo=52 <-- Program this value into DT: light_uncalibrated_lo
* uncal_hi=1627 <-- Program this value into DT: light_uncalibrated_hi
* cal_lo=8050 <-- Program this value into DT: light_calibrated_lo
* cal_hi=308000 <-- Program this value into DT: light_calibrated_hi
* thresh_lo=10
* thresh_hi=10
* ...
* Note that the calibrated value must be the value before the scale and offset
* is applied. For example, if the calibrated lux reading is 123.4 lux, and
* the in_illuminance_scale is normally 0.01, then the value entered is 12340
* which will be 123.4 lux when the scale is applied at the HAL layer.
*
* If the thresholds have changed instead of the calibration settings, then
* the driver doesn't support this feature.
* In order to display raw values, interpolation, that uses the calibration
* values, is not executed by the driver when in calibration mode, so to test,
* disable and reenable the device to exit calibration mode and test the new
* calibration values.
*
*
* Method 2 can only be used if dynamic resolution is not used by the HW
* driver. The data passed up to the HAL is the HW value read so that the HAL
* can multiply the HW value with the scale (resolution).
* As a baseline, scale would be the same value as the static resolution.
* Method 2:
* 1. Disable device.
* 2. Write 1 to the scale sysfs attribute.
* 3. Enable device.
* 4. The NVS HAL will announce in the log that calibration mode is enabled and
* display the data along with the scale and offset parameters applied.
* 5. Write to scale and offset sysfs attributes as needed to get the data
* modified as desired.
* 6. Disabling the device disables calibration mode.
* 7. Set the new scale and offset parameters in the device tree:
* light_scale_ival = the integer value of the scale.
* light_scale_fval = the floating value of the scale.
* light_offset_ival = the integer value of the offset.
* light_offset_fval = the floating value of the offset.
* The values are in the NVS_FLOAT_SIGNIFICANCE_ format (see nvs.h).
*/
#include <linux/module.h>
#include <linux/of.h>
#include <linux/version.h>
#include <linux/nvs_light.h>
#define NVS_LIGHT_VERSION (106)
ssize_t nvs_light_dbg(struct nvs_light *nl, char *buf)
{
ssize_t t;
unsigned int n;
unsigned int i;
t = snprintf(buf, PAGE_SIZE, "%s v.%u:\n",
__func__, NVS_LIGHT_VERSION);
t += snprintf(buf + t, PAGE_SIZE - t, "timestamp=%lld\n",
nl->timestamp);
t += snprintf(buf + t, PAGE_SIZE - t, "timestamp_report=%lld\n",
nl->timestamp_report);
t += snprintf(buf + t, PAGE_SIZE - t, "lux=%u\n", nl->lux);
t += snprintf(buf + t, PAGE_SIZE - t, "lux_max=%u\n", nl->lux_max);
t += snprintf(buf + t, PAGE_SIZE - t, "hw=%u\n", nl->hw);
t += snprintf(buf + t, PAGE_SIZE - t, "hw_mask=%x\n", nl->hw_mask);
t += snprintf(buf + t, PAGE_SIZE - t, "hw_thresh_lo=%u\n",
nl->hw_thresh_lo);
t += snprintf(buf + t, PAGE_SIZE - t, "hw_thresh_hi=%u\n",
nl->hw_thresh_hi);
t += snprintf(buf + t, PAGE_SIZE - t, "hw_limit_lo=%x\n",
nl->hw_limit_lo);
t += snprintf(buf + t, PAGE_SIZE - t, "hw_limit_hi=%x\n",
nl->hw_limit_hi);
t += snprintf(buf + t, PAGE_SIZE - t, "thresh_valid_lo=%x\n",
nl->thresh_valid_lo);
t += snprintf(buf + t, PAGE_SIZE - t, "thresh_valid_hi=%x\n",
nl->thresh_valid_hi);
t += snprintf(buf + t, PAGE_SIZE - t, "thresholds_valid=%x\n",
nl->thresholds_valid);
t += snprintf(buf + t, PAGE_SIZE - t, "nld_i_change=%x\n",
nl->nld_i_change);
t += snprintf(buf + t, PAGE_SIZE - t, "calibration_en=%x\n",
nl->calibration_en);
t += snprintf(buf + t, PAGE_SIZE - t, "poll_delay_ms=%u\n",
nl->poll_delay_ms);
t += snprintf(buf + t, PAGE_SIZE - t, "delay_us=%u\n", nl->delay_us);
t += snprintf(buf + t, PAGE_SIZE - t, "report=%u\n", nl->report);
t += snprintf(buf + t, PAGE_SIZE - t, "nld_i=%u\n", nl->nld_i);
t += snprintf(buf + t, PAGE_SIZE - t, "nld_i_lo=%u\n", nl->nld_i_lo);
t += snprintf(buf + t, PAGE_SIZE - t, "nld_i_hi=%u\n", nl->nld_i_hi);
t += snprintf(buf + t, PAGE_SIZE - t, "nld_tbl_n=%u\n", nl->nld_tbl_n);
if (nl->nld_tbl) {
if (nl->nld_tbl_n) {
i = 0;
n = nl->nld_tbl_n;
} else {
i = nl->nld_i_lo;
n = nl->nld_i_hi + 1;
}
for (; i < n; i++) {
if (nl->nld_thr) {
t += snprintf(buf + t, PAGE_SIZE - t,
"nld_thr[%u].lo=%u\n",
i, nl->nld_thr[i].lo);
t += snprintf(buf + t, PAGE_SIZE - t,
"nld_thr[%u].hi=%u\n",
i, nl->nld_thr[i].hi);
t += snprintf(buf + t, PAGE_SIZE - t,
"nld_thr[%u].i_lo=%u\n",
i, nl->nld_thr[i].i_lo);
t += snprintf(buf + t, PAGE_SIZE - t,
"nld_thr[%u].i_hi=%u\n",
i, nl->nld_thr[i].i_hi);
}
if (nl->cfg->float_significance) {
t += snprintf(buf + t, PAGE_SIZE - t,
"nld_tbl[%d].resolution=%d.%09u\n",
i, nl->nld_tbl[i].resolution.ival,
nl->nld_tbl[i].resolution.fval);
t += snprintf(buf + t, PAGE_SIZE - t,
"nld_tbl[%d].max_range=%d.%09u\n",
i, nl->nld_tbl[i].max_range.ival,
nl->nld_tbl[i].max_range.fval);
t += snprintf(buf + t, PAGE_SIZE - t,
"nld_tbl[%d].milliamp=%d.%09u\n",
i, nl
|