summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/tsllux.c
blob: e3ddfe589f111e3e62d994a96cd28e9dff1a8592 (plain)
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
/* $NetBSD: tsllux.c,v 1.4 2022/02/12 03:24:35 riastradh Exp $ */

/*-
 * Copyright (c) 2018 Jason R. Thorpe
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tsllux.c,v 1.4 2022/02/12 03:24:35 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/sysctl.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/tsl256xreg.h>

#include <dev/sysmon/sysmonvar.h>

struct tsllux_softc {
	device_t	sc_dev;
	i2c_tag_t	sc_i2c;
	i2c_addr_t	sc_addr;

	uint32_t	sc_poweron;

	/*
	 * Locking order is:
	 *	tsllux mutex -> i2c bus
	 */
	kmutex_t	sc_lock;

	uint8_t		sc_itime;
	uint8_t		sc_gain;
	bool		sc_cs_package;
	bool		sc_auto_gain;

	struct sysmon_envsys *sc_sme;
	envsys_data_t	sc_sensor;

	struct sysctllog *sc_sysctllog;
};

#define	TSLLUX_F_CS_PACKAGE	0x01

static int	tsllux_match(device_t, cfdata_t, void *);
static void	tsllux_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(tsllux, sizeof(struct tsllux_softc),
    tsllux_match, tsllux_attach, NULL, NULL);

static const struct device_compatible_entry tsllux_compat_data[] = {
	{ .compat = "amstaos,tsl2560" },
	{ .compat = "amstaos,tsl2561" },
	DEVICE_COMPAT_EOL
};

static int	tsllux_read1(struct tsllux_softc *, uint8_t, uint8_t *);
static int	tsllux_read2(struct tsllux_softc *, uint8_t, uint16_t *);
static int	tsllux_write1(struct tsllux_softc *, uint8_t, uint8_t);
#if 0
static int	tsllux_write2(struct tsllux_softc *, uint8_t, uint16_t);
#endif

static void	tsllux_sysctl_attach(struct tsllux_softc *);

static int	tsllux_poweron(struct tsllux_softc *);
static int	tsllux_poweroff(struct tsllux_softc *);

static int	tsllux_set_integration_time(struct tsllux_softc *, uint8_t);
static int	tsllux_set_gain(struct tsllux_softc *, uint8_t);
static int	tsllux_set_autogain(struct tsllux_softc *, bool);

static int	tsllux_get_lux(struct tsllux_softc *, uint32_t *,
			       uint16_t *, uint16_t *);

static void	tsllux_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);

static int
tsllux_match(device_t parent, cfdata_t match, void *aux)
{
	struct i2c_attach_args *ia = aux;
	uint8_t id_reg;
	int error, match_result;

	if (iic_use_direct_match(ia, match, tsllux_compat_data, &match_result))
		return (match_result);

	switch (ia->ia_addr) {
	case TSL256x_SLAVEADDR_GND:
	case TSL256x_SLAVEADDR_FLOAT:
	case TSL256x_SLAVEADDR_VDD:
		break;

	default:
		return (0);
	}

	if (iic_acquire_bus(ia->ia_tag, 0) != 0)
		return (0);
	error = iic_smbus_read_byte(ia->ia_tag, ia->ia_addr,
	    TSL256x_REG_ID | COMMAND6x_CMD, &id_reg, 0);
	iic_release_bus(ia->ia_tag, 0);

	if (error)
		return (0);

	/* XXX This loses if we have a 2560 rev. 0. */
	if (id_reg == 0)
		return (I2C_MATCH_ADDRESS_ONLY);

	return (I2C_MATCH_ADDRESS_AND_PROBE);
}

static void
tsllux_attach(device_t parent, device_t self, void *aux)
{
	struct tsllux_softc *sc = device_private(self);
	struct i2c_attach_args *ia = aux;
	bool have_i2c;

	/* XXX IPL_NONE changes when we support threshold interrupts. */
	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);

	sc->sc_dev = self;
	sc->sc_i2c = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;

	if (device_cfdata(self)->cf_flags & TSLLUX_F_CS_PACKAGE)
		sc->sc_cs_package = true;

	if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
		return;
	}

	have_i2c = true;

	/* Power on the device and clear any pending interrupts. */
	if (tsllux_write1(sc, TSL256x_REG_CONTROL | COMMAND6x_CLEAR,
			  CONTROL6x_POWER_ON)) {
		aprint_error_dev(self, ": unable to power on device\n");
		goto out;
	}
	sc->sc_poweron = 1;

	/* Make sure interrupts are disabled. */
	if (tsllux_write1(sc, TSL256x_REG_INTERRUPT | COMMAND6x_CLEAR, 0)) {
		aprint_error_dev(self, ": unable to disable interrupts\n");
		goto out;
	}

	aprint_naive("\n");
	aprint_normal(": TSL256x Light-to-Digital converter%s\n",
		      sc->sc_cs_package ? " (CS package)" : "");

	/* Inititalize timing to reasonable defaults. */
	sc->sc_auto_gain = true;
	sc->sc_gain = TIMING6x_GAIN_16X;
	if (tsllux_set_integration_time(sc, TIMING6x_INTEG_101ms)) {
		aprint_error_dev(self, ": unable to set integration time\n");
		goto out;
	}

	tsllux_poweroff(sc);

	iic_release_bus(ia->ia_tag, 0);
	have_i2c = false;

	tsllux_sysctl_attach(sc);

	sc->sc_sme = sysmon_envsys_create();
	sc->sc_sme->sme_name = device_xname(self);
	sc->sc_sme->sme_cookie = sc;
	sc->sc_sme->sme_refresh = tsllux_sensors_refresh;

	sc->sc_sensor.units = ENVSYS_LUX;
	sc->sc_sensor.state = ENVSYS_SINVALID;
	snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc),
		 "ambient light");
	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor);

	sysmon_envsys_register(sc->sc_sme);

 out:
	if (have_i2c) {
		if (sc->sc_poweron)
			tsllux_poweroff(sc);
		iic_release_bus(ia->ia_tag, 0);
	}
}

static int
tsllux_sysctl_cs_package(SYSCTLFN_ARGS)
{
	struct tsllux_softc *sc;
	struct sysctlnode node;
	int error;
	u_int val;

	node = *rnode;
	sc = node.sysctl_data;

	mutex_enter(&sc->sc_lock);
	val = sc->sc_cs_package ? 1 : 0;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL) {
		mutex_exit(&sc->sc_lock);
		return (error);
	}

	/* CS package indicator is used only in software; no need for I2C. */

	sc->sc_cs_package = val ? true : false;
	mutex_exit(&sc->sc_lock);

	return (error);
}

static int
tsllux_sysctl_autogain(SYSCTLFN_ARGS)
{
	struct tsllux_softc *sc;
	struct sysctlnode node;
	int error;
	u_int val;

	node = *rnode;
	sc = node.sysctl_data;

	mutex_enter(&sc->sc_lock);
	val = sc->sc_auto_gain ? 1 : 0;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL) {
		mutex_exit(&sc->sc_lock);
		return (error);
	}

	/* Auto-gain is a software feature; no need for I2C. */

	error = tsllux_set_autogain(sc, val ? true : false);
	mutex_exit(&sc->sc_lock);

	return (error);
}

static int
tsllux_sysctl_gain(SYSCTLFN_ARGS)
{
	struct tsllux_softc *sc;
	struct sysctlnode node;
	int error;
	u_int val;
	uint8_t new_gain;
	
	node = *rnode;
	sc = node.sysctl_data;

	mutex_enter(&sc->sc_lock);

	switch (sc->sc_gain) {
	case TIMING6x_GAIN_1X:
		val = 1;
		break;
	
	case TIMING6x_GAIN_16X:
		val = 16;
		break;
	
	default:
		val = 1;
		break;
	}
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL) {
		mutex_exit(&sc->sc_lock);
		return (error);
	}

	switch (val) {
	case 1:
		new_gain = TIMING6x_GAIN_1X;
		break;
	
	case 16:
		new_gain = TIMING6x_GAIN_16X;
		break;
	
	default:
		mutex_exit(&sc->sc_lock);
		return (EINVAL);
	}

	if ((error = iic_acquire_bus(sc->sc_i2c, 0)) != 0) {
		mutex_exit(&sc->sc_lock);
		return (error);
	}

	error = tsllux_set_gain(sc, new_gain);
	iic_release_bus(sc->sc_i2c, 0);
	mutex_exit(&sc->sc_lock);

	return (error);
}

static int
tsllux_sysctl_itime(SYSCTLFN_ARGS)
{
	struct tsllux_softc *sc;
	struct sysctlnode node;
	int error;
	u_int val;
	uint8_t new_itime;

	node = *rnode;
	sc = node.sysctl_data;

	mutex_enter(&sc->sc_lock);

	switch (sc->sc_itime) {
	case TIMING6x_INTEG_13_7ms:
		val = 13;
		break;
	
	case TIMING6x_INTEG_101ms:
		val = 101;
		break;
	
	case TIMING6x_INTEG_402ms:
	default:
		val = 402;
		break;
	}
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL) {
		mutex_exit(&sc->sc_lock);
		return (error);
	}

	switch (val) {
	case 13:
	case 14:
		new_itime = TIMING6x_INTEG_13_7ms;
		break;
	
	case 101:
		new_itime = TIMING6x_INTEG_101ms;
		break;
	
	case 402:
		new_itime = TIMING6x_INTEG_402ms;
		break;
	
	default:
		mutex_exit(&sc->sc_lock);
		return (EINVAL);
	}

	if ((error = iic_acquire_bus(sc->sc_i2c, 0)) != 0) {
		mutex_exit(&sc->sc_lock);
		return (error);
	}

	error = tsllux_set_integration_time(sc, new_itime);
	iic_release_bus(sc->sc_i2c, 0);
	mutex_exit(&sc->sc_lock);

	return (error);
}

static void
tsllux_sysctl_attach(struct tsllux_softc *sc)
{
	struct sysctllog **log = &sc->sc_sysctllog;
	const struct sysctlnode *rnode, *cnode;
	int error;

	error = sysctl_createv(log, 0, NULL, &rnode, CTLFLAG_PERMANENT,
	    CTLTYPE_NODE, device_xname(sc->sc_dev),
	    SYSCTL_DESCR("tsl256x control"),
	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
	if (error)
		return;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "cs_package",
	    SYSCTL_DESCR("sensor in Chipscale (CS) package"),
	    tsllux_sysctl_cs_package, 0,
	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
	if (error)
		return;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "auto_gain",
	    SYSCTL_DESCR("auto-gain algorithm enabled"),
	    tsllux_sysctl_autogain, 0,
	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
	if (error)
		return;
	
	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "gain",
	    SYSCTL_DESCR("sensor gain"), tsllux_sysctl_gain, 0,
	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
	if (error)
		return;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
	    "integration_time",
	    SYSCTL_DESCR("ADC integration time"), tsllux_sysctl_itime, 0,
	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
	if (error)
		return;
}

static void
tsllux_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
	struct tsllux_softc *sc = sme->sme_cookie;
	uint32_t lux;
	int error;

	if (edata != &sc->sc_sensor) {
		edata->state = ENVSYS_SINVALID;
		return;
	}

	mutex_enter(&sc->sc_lock);

	if ((error = iic_acquire_bus(sc->sc_i2c, 0)) == 0) {
		error = tsllux_get_lux(sc, &lux, NULL, NULL);
		iic_release_bus(sc->sc_i2c, 0);
	}
	
	if (error) {
		edata->state = ENVSYS_SINVALID;
	} else {
		edata->value_cur = lux;
		edata->state = ENVSYS_SVALID;
	}

	mutex_exit(&sc->sc_lock);
}

/*
 * Allow pending interrupts to be cleared as part of another operation.
 */
#define	REGMASK6x		(COMMAND6x_REGMASK | COMMAND6x_CLEAR)

static int
tsllux_read1(struct tsllux_softc *sc, uint8_t reg, uint8_t *valp)
{
	reg = (reg & REGMASK6x) | COMMAND6x_CMD;
	return (iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, valp, 0));
}

static int
tsllux_read2(struct tsllux_softc *sc, uint8_t reg, uint16_t *valp)
{
	reg = (reg & REGMASK6x) | COMMAND6x_CMD | COMMAND6x_WORD;
	return (iic_smbus_read_word(sc->sc_i2c, sc->sc_addr, reg, valp, 0));
}

static int
tsllux_write1(struct tsllux_softc *sc, uint8_t reg, uint8_t val)
{
	reg = (reg & REGMASK6x) | COMMAND6x_CMD;
	return (iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0));
}

#if 0
static int
tsllux_write2(struct tsllux_softc *sc, uint8_t reg, uint16_t val)
{
	reg = (reg & REGMASK6x) | COMMAND6x_CMD | COMMAND6x_WORD;
	return (iic_smbus_write_word(sc->sc_i2c, sc->sc_addr, reg, val, 0));
}
#endif

#undef REGMASK

static int
tsllux_poweron(struct tsllux_softc *sc)
{
	int error;

	if (sc->sc_poweron++ == 0) {
		uint8_t val;

		error = tsllux_write1(sc, TSL256x_REG_CONTROL,
				      CONTROL6x_POWER_ON);
		if (error)
			return (error);

		error = tsllux_read1(sc, TSL256x_REG_CONTROL, &val);
		if (error)
			return (error);

		if (val != CONTROL6x_POWER_ON) {
			aprint_error_dev(sc->sc_dev,
					 "failed to power on sensor\n");
			return (EIO);
		}
	}
	return (0);
}

static int
tsllux_poweroff(struct tsllux_softc *sc)
{
	if (sc->sc_poweron && --sc->sc_poweron == 0)
		return (tsllux_write1(sc, TSL256x_REG_CONTROL,
				      CONTROL6x_POWER_OFF));
	return (0);
}

static int
tsllux_set_integration_time(struct tsllux_softc *sc, uint8_t time)
{
	int error;

	switch (time) {
	case TIMING6x_INTEG_13_7ms:
	case TIMING6x_INTEG_101ms:
	case TIMING6x_INTEG_402ms:
		break;
	
	default:
		return (EINVAL);
	}

	if ((error = tsllux_poweron(sc)) != 0)
		return (error);
	
	if ((error = tsllux_write1(sc, TSL256x_REG_TIMING,
				   time | sc->sc_gain)) != 0)
		goto out;
	
	sc->sc_itime = time;

 out:
	(void) tsllux_poweroff(sc);
	return (error);
}

static int
tsllux_set_gain0(struct tsllux_softc *sc, uint8_t gain)
{
	int error;

	if ((error = tsllux_write1(sc, TSL256x_REG_TIMING,
				   sc->sc_itime | gain)) != 0)
		return (error);
	
	sc->sc_gain = gain;
	return (0);
}

static int
tsllux_set_gain(struct tsllux_softc *sc, uint8_t gain)
{
	int error;

	switch (gain) {
	case TIMING6x_GAIN_1X:
	case TIMING6x_GAIN_16X:
		break;
	
	default:
		return (EINVAL);
	}

	if ((error = tsllux_poweron(sc)) != 0)
		return (error);

	if ((error = tsllux_set_gain0(sc, gain)) != 0)
		goto out;
	
	sc->sc_auto_gain = false;

 out:
	(void) tsllux_poweroff(sc);
	return (error);
}

static int
tsllux_set_autogain(struct tsllux_softc *sc, bool use_autogain)
{

	sc->sc_auto_gain = use_autogain;
	return (0);
}

static int
tsllux_wait_for_adcs(struct tsllux_softc *sc)
{
	int ms;

	switch (sc->sc_itime) {
	case TIMING6x_INTEG_13_7ms:
		/* Wait 15ms for 13.7ms integration */
		ms = 15;
		break;
	
	case TIMING6x_INTEG_101ms:
		/* Wait 120ms for 101ms integration */
		ms = 120;
		break;
	
	case TIMING6x_INTEG_402ms:
	default:
		/* Wait 450ms for 402ms integration */
		ms = 450;
		break;
	}

	if (ms < hztoms(1)) {
		/* Just busy-wait if we want to wait for less than 1 tick. */
		delay(ms * 1000);
	} else {
		/* Round up one tick for the case where we sleep. */
		(void) kpause("tslluxwait", false, mstohz(ms) + 1, NULL);
	}

	return (0);
}

static int
tsllux_read_adcs(struct tsllux_softc *sc, uint16_t *adc0valp,
		 uint16_t *adc1valp)
{
	int error;

	if ((error = tsllux_read2(sc, TSL256x_REG_DATA0LOW, adc0valp)) == 0)
		error = tsllux_read2(sc, TSL256x_REG_DATA1LOW, adc1valp);
	
	return (error);
}

/*
 * The following code is partially derived from Adafruit's TSL2561
 * driver for Arduino (which was in turn derived from the data sheet),
 * which carries this notice:
 * 
 * @file Adafruit_TSL2561_U.cpp
 *
 * @mainpage Adafruit TSL2561 Light/Lux sensor driver
 *
 * @section intro_sec Introduction
 *
 * This is the documentation for Adafruit's TSL2561 driver for the
 * Arduino platform.  It is designed specifically to work with the
 * Adafruit TSL2561 breakout: http://www.adafruit.com/products/439
 *
 * These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
 * to interface with the breakout.
 *
 * Adafruit invests time and resources providing this open source code,
 * please support Adafruit and open-source hardware by purchasing
 * products from Adafruit!
 *
 * @section dependencies Dependencies
 *
 * This library depends on <a href="https://github.com/adafruit/Adafruit_Sensor">
 * Adafruit_Sensor</a> being present on your system. Please make sure you have
 * installed the latest version before using this library.
 *
 * @section author Author
 *
 * Written by Kevin "KTOWN" Townsend for Adafruit Industries.
 *
 * @section license License
 *
 * BSD license, all text here must be included in any redistribution.
 *
 *   @section  HISTORY
 *
 *   v2.0 - Rewrote driver for Adafruit_Sensor and Auto-Gain support, and
 *          added lux clipping check (returns 0 lux on sensor saturation)
 *   v1.0 - First release (previously TSL2561)
 */

static int
tsllux_read_sensors(struct tsllux_softc *sc, uint16_t *adc0p, uint16_t *adc1p)
{
	int error;

	if ((error = tsllux_poweron(sc)) != 0)
		return (error);
	
	if ((error = tsllux_wait_for_adcs(sc)) != 0)
		goto out;
	
	error = tsllux_read_adcs(sc, adc0p, adc1p);

 out:
	(void) tsllux_poweroff(sc);
	return (error);
}

/*
 * Auto-gain thresholds:
 */
#define	TSL2561_AGC_THI_13MS	(4850)	/* Max value at Ti 13ms = 5047 */
#define	TSL2561_AGC_TLO_13MS	(100)	/* Min value at Ti 13ms = 100 */
#define	TSL2561_AGC_THI_101MS	(36000)	/* Max value at Ti 101ms = 37177 */
#define	TSL2561_AGC_TLO_101MS	(200)	/* Min value at Ti 101ms = 200 */
#define	TSL2561_AGC_THI_402MS	(63000)	/* Max value at Ti 402ms = 65535 */
#define	TSL2561_AGC_TLO_402MS	(500)	/* Min value at Ti 402ms = 500 */

static int
tsllux_get_sensor_data(struct tsllux_softc *sc, uint16_t *broadband,
		       uint16_t *ir)
{
	int error = 0;
	uint16_t adc0, adc1;
	bool did_adjust_gain, valid;
	uint16_t hi, lo;

	if (sc->sc_auto_gain == false) {
		error = tsllux_read_sensors(sc, &adc0, &adc1);
		goto out;
	}

	/* Set the hi / lo threshold based on current integration time. */
	switch (sc->sc_itime) {
	case TIMING6x_INTEG_13_7ms:
		hi = TSL2561_AGC_THI_13MS;
		lo = TSL2561_AGC_TLO_13MS;
		break;
	
	case TIMING6x_INTEG_101ms:
		hi = TSL2561_AGC_THI_101MS;
		lo = TSL2561_AGC_TLO_101MS;
		break;
	
	case TIMING6x_INTEG_402ms:
	default:
		hi = TSL2561_AGC_THI_402MS;
		lo = TSL2561_AGC_TLO_402MS;
	}

	/* Read data and adjust the gain until we have a valid range. */
	for (valid = false, did_adjust_gain = false; valid == false; ) {
		if ((error = tsllux_read_sensors(sc, &adc0, &adc1)) != 0)
			goto out;

		if (did_adjust_gain == false) {
			if (adc0 < lo && sc->sc_gain == TIMING6x_GAIN_1X) {
				/* Increase the gain and try again. */
				if ((error =
				     tsllux_set_gain0(sc,
				     		      TIMING6x_GAIN_16X)) != 0)
					goto out;
				did_adjust_gain = true;
			} else if (adc0 > hi &&
				   sc->sc_gain == TIMING6x_GAIN_16X) {
				/* Decrease the gain and try again. */
				if ((error =
				     tsllux_set_gain0(sc,
				     		      TIMING6x_GAIN_1X)) != 0)
					goto out;
				did_adjust_gain = true;
			} else {
				/*
				 * Reading is either valid or we're already
				 * at the chip's limits.
				 */
				valid = true;
			}
		} else {
			/*
			 * If we've already adjust the gain once, just
			 * return the new results.  This avoids endless
			 * loops where a value is at one extre pre-gain
			 * and at the other extreme post-gain.
			 */
			valid = true;
		}
	}

 out:
	if (error == 0) {
		if (broadband != NULL)
			*broadband = adc0;
		if (ir != NULL)
			*ir = adc1;
	}
	return (error);
}

/*
 * Clipping thresholds:
 */
#define	TSL2561_CLIPPING_13MS	(4900)
#define	TSL2561_CLIPPING_101MS	(37000)
#define	TSL2561_CLIPPING_402MS	(65000)

/*
 * Scaling factors:
 */
#define	TSL2561_LUX_LUXSCALE      (14)	   /* Scale by 2^14 */
#define	TSL2561_LUX_RATIOSCALE    (9)      /* Scale ratio by 2^9 */
#define	TSL2561_LUX_CHSCALE       (10)     /* Scale channel values by 2^10 */
#define	TSL2561_LUX_CHSCALE_TINT0 (0x7517) /* 322/11 * 2^TSL2561_LUX_CHSCALE */
#define	TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) /* 322/81 * 2^TSL2561_LUX_CHSCALE */

/*
 * Lux factors (the datasheet explains how these magic constants
 * are used):
 */
/* T, FN and CL package values */
#define TSL2561_LUX_K1T           (0x0040)  /* 0.125 * 2^RATIO_SCALE */
#define TSL2561_LUX_B1T           (0x01f2)  /* 0.0304 * 2^LUX_SCALE */
#define TSL2561_LUX_M1T           (0x01be)  /* 0.0272 * 2^LUX_SCALE */
#define TSL2561_LUX_K2T           (0x0080)  /* 0.250 * 2^RATIO_SCALE */
#define TSL2561_LUX_B2T           (0x0214)  /* 0.0325 * 2^LUX_SCALE */
#define TSL2561_LUX_M2T           (0x02d1)  /* 0.0440 * 2^LUX_SCALE */
#define TSL2561_LUX_K3T           (0x00c0)  /* 0.375 * 2^RATIO_SCALE */
#define TSL2561_LUX_B3T           (0x023f)  /* 0.0351 * 2^LUX_SCALE */
#define TSL2561_LUX_M3T           (0x037b)  /* 0.0544 * 2^LUX_SCALE */
#define TSL2561_LUX_K4T           (0x0100)  /* 0.50 * 2^RATIO_SCALE */
#define TSL2561_LUX_B4T           (0x0270)  /* 0.0381 * 2^LUX_SCALE */
#define TSL2561_LUX_M4T           (0x03fe)  /* 0.0624 * 2^LUX_SCALE */
#define TSL2561_LUX_K5T           (0x0138)  /* 0.61 * 2^RATIO_SCALE */
#define TSL2561_LUX_B5T           (0x016f)  /* 0.0224 * 2^LUX_SCALE */
#define TSL2561_LUX_M5T           (0x01fc)  /* 0.0310 * 2^LUX_SCALE */
#define TSL2561_LUX_K6T           (0x019a)  /* 0.80 * 2^RATIO_SCALE */
#define TSL2561_LUX_B6T           (0x00d2)  /* 0.0128 * 2^LUX_SCALE */
#define TSL2561_LUX_M6T           (0x00fb)  /* 0.0153 * 2^LUX_SCALE */
#define TSL2561_LUX_K7T           (0x029a)  /* 1.3 * 2^RATIO_SCALE */
#define TSL2561_LUX_B7T           (0x0018)  /* 0.00146 * 2^LUX_SCALE */
#define TSL2561_LUX_M7T           (0x0012)  /* 0.00112 * 2^LUX_SCALE */
#define TSL2561_LUX_K8T           (0x029a)  /* 1.3 * 2^RATIO_SCALE */
#define TSL2561_LUX_B8T           (0x0000)  /* 0.000 * 2^LUX_SCALE */
#define TSL2561_LUX_M8T           (0x0000)  /* 0.000 * 2^LUX_SCALE */

/* CS package values */
#define TSL2561_LUX_K1C           (0x0043)  /* 0.130 * 2^RATIO_SCALE */
#define TSL2561_LUX_B1C           (0x0204)  /* 0.0315 * 2^LUX_SCALE */
#define TSL2561_LUX_M1C           (0x01ad)  /* 0.0262 * 2^LUX_SCALE */
#define TSL2561_LUX_K2C           (0x0085)  /* 0.260 * 2^RATIO_SCALE */
#define TSL2561_LUX_B2C           (0x0228)  /* 0.0337 * 2^LUX_SCALE */
#define TSL2561_LUX_M2C           (0x02c1)  /* 0.0430 * 2^LUX_SCALE */
#define TSL2561_LUX_K3C           (0x00c8)  /* 0.390 * 2^RATIO_SCALE */
#define TSL2561_LUX_B3C           (0x0253)  /* 0.0363 * 2^LUX_SCALE */
#define TSL2561_LUX_M3C           (0x0363)  /* 0.0529 * 2^LUX_SCALE */
#define TSL2561_LUX_K4C           (0x010a)  /* 0.520 * 2^RATIO_SCALE */
#define TSL2561_LUX_B4C           (0x0282)  /* 0.0392 * 2^LUX_SCALE */
#define TSL2561_LUX_M4C           (0x03df)  /* 0.0605 * 2^LUX_SCALE */
#define TSL2561_LUX_K5C           (0x014d)  /* 0.65 * 2^RATIO_SCALE */
#define TSL2561_LUX_B5C           (0x0177)  /* 0.0229 * 2^LUX_SCALE */
#define TSL2561_LUX_M5C           (0x01dd)  /* 0.0291 * 2^LUX_SCALE */
#define TSL2561_LUX_K6C           (0x019a)  /* 0.80 * 2^RATIO_SCALE */
#define TSL2561_LUX_B6C           (0x0101)  /* 0.0157 * 2^LUX_SCALE */
#define TSL2561_LUX_M6C           (0x0127)  /* 0.0180 * 2^LUX_SCALE */
#define TSL2561_LUX_K7C           (0x029a)  /* 1.3 * 2^RATIO_SCALE */
#define TSL2561_LUX_B7C           (0x0037)  /* 0.00338 * 2^LUX_SCALE */
#define TSL2561_LUX_M7C           (0x002b)  /* 0.00260 * 2^LUX_SCALE */
#define TSL2561_LUX_K8C           (0x029a)  /* 1.3 * 2^RATIO_SCALE */
#define TSL2561_LUX_B8C           (0x0000)  /* 0.000 * 2^LUX_SCALE */
#define TSL2561_LUX_M8C           (0x0000)  /* 0.000 * 2^LUX_SCALE */

struct lux_factor_table_entry {
	uint16_t	k;
	uint16_t	b;
	uint16_t	m;
};

static const struct lux_factor_table_entry lux_factor_table[] = {
	{ TSL2561_LUX_K1T,	TSL2561_LUX_B1T,	TSL2561_LUX_M1T },
	{ TSL2561_LUX_K2T,	TSL2561_LUX_B2T,	TSL2561_LUX_M2T },
	{ TSL2561_LUX_K3T,	TSL2561_LUX_B3T,	TSL2561_LUX_M3T },
	{ TSL2561_LUX_K4T,	TSL2561_LUX_B4T,	TSL2561_LUX_M4T },
	{ TSL2561_LUX_K5T,	TSL2561_LUX_B5T,	TSL2561_LUX_M5T },
	{ TSL2561_LUX_K6T,	TSL2561_LUX_B6T,	TSL2561_LUX_M6T },
	{ TSL2561_LUX_K7T,	TSL2561_LUX_B7T,	TSL2561_LUX_M7T },
	{ TSL2561_LUX_K8T,	TSL2561_LUX_B8T,	TSL2561_LUX_M8T },
};
static const int lux_factor_table_last_entry =
    (sizeof(lux_factor_table) / sizeof(lux_factor_table[0])) - 1;

static const struct lux_factor_table_entry lux_factor_table_cs_package[] = {
	{ TSL2561_LUX_K1C,	TSL2561_LUX_B1C,	TSL2561_LUX_M1C },
	{ TSL2561_LUX_K2C,	TSL2561_LUX_B2C,	TSL2561_LUX_M2C },
	{ TSL2561_LUX_K3C,	TSL2561_LUX_B3C,	TSL2561_LUX_M3C },
	{ TSL2561_LUX_K4C,	TSL2561_LUX_B4C,	TSL2561_LUX_M4C },
	{ TSL2561_LUX_K5C,	TSL2561_LUX_B5C,	TSL2561_LUX_M5C },
	{ TSL2561_LUX_K6C,	TSL2561_LUX_B6C,	TSL2561_LUX_M6C },
	{ TSL2561_LUX_K7C,	TSL2561_LUX_B7C,	TSL2561_LUX_M7C },
	{ TSL2561_LUX_K8C,	TSL2561_LUX_B8C,	TSL2561_LUX_M8C },
};
static const int lux_factor_table_cs_package_last_entry =
    (sizeof(lux_factor_table_cs_package) /
     sizeof(lux_factor_table_cs_package[0])) - 1;

static int
tsllux_get_lux(struct tsllux_softc *sc, uint32_t *luxp,
	       uint16_t *raw_broadband, uint16_t *raw_ir)
{
	uint32_t channel0, channel1, scale, ratio, lux = 0;
	uint16_t broadband, ir;
	uint16_t clip_threshold;
	const struct lux_factor_table_entry *table;
	int idx, last_entry, error;
	int32_t temp;

	if ((error = tsllux_get_sensor_data(sc, &broadband, &ir)) != 0)
		return (error);
	
	if (luxp == NULL) {
		/*
		 * Caller doesn't want the calculated Lux value, so
		 * don't bother calculating it.  Maybe they just want
		 * the raw sensor data?
		 */
		goto out;
	}

	/*
	 * Check to see if the sensor is saturated.  If so,
	 * just return a "max brightness" value.
	 */
	switch (sc->sc_itime) {
	case TIMING6x_INTEG_13_7ms:
		clip_threshold = TSL2561_CLIPPING_13MS;
		break;
	
	case TIMING6x_INTEG_101ms:
		clip_threshold = TSL2561_CLIPPING_101MS;
		break;
	
	case TIMING6x_INTEG_402ms:
	default:
		clip_threshold = TSL2561_CLIPPING_402MS;
		break;
	}

	if (broadband > clip_threshold || ir > clip_threshold) {
		lux = 65536;
		goto out;
	}

	/* Get correct scale factor based on integration time. */
	switch (sc->sc_itime) {
	case TIMING6x_INTEG_13_7ms:
		scale = TSL2561_LUX_CHSCALE_TINT0;
		break;
	
	case TIMING6x_INTEG_101ms:
		scale = TSL2561_LUX_CHSCALE_TINT1;
		break;
	
	case TIMING6x_INTEG_402ms:
	default:
		scale = (1 << TSL2561_LUX_CHSCALE);
	}

	/* Scale for gain. */
	if (sc->sc_gain == TIMING6x_GAIN_1X)
		scale <<= 4;
	
	/* Scale the channel values. */
	channel0 = ((uint32_t)broadband * scale) >> TSL2561_LUX_CHSCALE;
	channel1 = ((uint32_t)ir * scale) >> TSL2561_LUX_CHSCALE;

	/* Find the ratio of the channel values (ir / broadband) */
	if (channel0 != 0)
		ratio = (channel1 << (TSL2561_LUX_RATIOSCALE + 1)) / channel0;
	else
		ratio = 0;
	
	/* Round the ratio value. */
	ratio = (ratio + 1) >> 1;

	if (sc->sc_cs_package) {
		table = lux_factor_table_cs_package;
		last_entry = lux_factor_table_cs_package_last_entry;
	} else {
		table = lux_factor_table;
		last_entry = lux_factor_table_last_entry;
	}

	/*
	 * The table is arranged such that we compare <= against
	 * the key, and if all else fails, we use the last entry.
	 * The pseudo-code in the data sheet shows what's going on.
	 */
	for (idx = 0; idx < last_entry; idx++) {
		if (ratio <= table[idx].k)
			break;
	}

	temp = ((channel0 * table[idx].b) - (channel1 * table[idx].m));

	/* Do not allow negative Lux value. */
	if (temp < 0)
		temp = 0;
	
	/* Round lsb (2^(LUX_SCALE-1)) */
	temp += (1 << (TSL2561_LUX_LUXSCALE-1));

	/* Strip off fractional portion */
	lux = temp >> TSL2561_LUX_LUXSCALE;

 out:
	if (error == 0) {
		if (luxp != NULL)
			*luxp = lux;
		if (raw_broadband != NULL)
			*raw_broadband = broadband;
		if (raw_ir != NULL)
			*raw_ir = ir;
	}
	return (error);
}