-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm1637_kmod.c
1378 lines (1163 loc) · 33.4 KB
/
tm1637_kmod.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
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (C) 2020 Alexander Mishin
* 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 AUTHOR 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 AUTHOR 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.
*/
//#define FDT
#include <sys/types.h>
#include <sys/param.h> /* defines used in kernel.h */
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/systm.h> /* uprintf */
#include <sys/sysctl.h>
#include <sys/conf.h> /* cdevsw struct */
#include <sys/kernel.h> /* types used in module initialization */
#include <sys/bus.h>
#include <sys/uio.h> /* uio struct */
#include <sys/malloc.h>
#include <sys/gpio.h>
#include <dev/iicbus/iiconf.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/gpio/gpiobusvar.h>
#include <dev/fdt/fdt_pinctrl.h>
#include "include/dev/tm1637/tm1637.h"
#define TM1637_DESC "TM1637 %u digit 7 segment display with %s"
#define TM1637_CDEV_NAME "tm1637"
#define TM1637_SCL_PROPERTY "scl-gpios"
#define TM1637_SDA_PROPERTY "sda-gpios"
#define TM1637_SCL_IDX 0
#define TM1637_SDA_IDX 1
#define TM1637_MIN_PINS 2
#define TM1637_BUSFREQ 200000
#define TM1637_ADDRESS_AUTO 0x40
#define TM1637_ADDRESS_FIXED 0x44
#define TM1637_ADDRESS_START 0xc0
#define TM1637_DISPLAY_OFF 0x80
#define TM1637_DISPLAY_CTRL 0x88
#define TM1637_8TH_COLON 0
#define TM1637_8TH_DOT 1
#define TM1637_LOCK_INIT(sc) \
mtx_init(&(sc)->mtx, "tm1637_mtx", NULL, MTX_DEF)
#define TM1637_LOCK_DESTROY(sc) \
mtx_destroy(&(sc)->mtx)
#define TM1637_LOCK(sc) \
mtx_lock(&(sc)->mtx)
#define TM1637_UNLOCK(sc) \
mtx_unlock(&(sc)->mtx)
#define IICBB_DEBUG
#ifdef IICBB_DEBUG
static int i2c_debug = 0;
SYSCTL_DECL(_hw_i2c);
SYSCTL_INT(_hw_i2c, OID_AUTO, iicbb_debug, CTLFLAG_RWTUN,
&i2c_debug, 0, "Enable i2c bit-banging driver debug");
#define I2C_DEBUG(x) do { \
if (i2c_debug) (x); \
} while (0)
#else
#define I2C_DEBUG(x)
#endif
#define GPIOBB_GETSDA(dev) (gpiobb_getsda(dev))
#define GPIOBB_SETSDA(dev, x) (gpiobb_setsda(dev, x))
#define GPIOBB_GETSCL(dev) (gpiobb_getscl(dev))
#define GPIOBB_SETSCL(dev, x) (gpiobb_setscl(dev, x))
/* Based on the SMBus specification. */
#define DEFAULT_SCL_LOW_TIMEOUT (25 * 1000)
static const u_char char_code[10] = {
CHR_0, CHR_1, CHR_2, CHR_3, CHR_4,
CHR_5, CHR_6, CHR_7, CHR_8, CHR_9
};
/* Buffer struct */
MALLOC_DEFINE(M_TM1637BUF, "tm1637buffer", "Buffer for tm1637 display");
MALLOC_DEFINE(M_TM1637COD, "tm1637codes", "Masks array for tm1637 display");
struct tm1637_buf_t {
uint8_t length;
uint8_t *codes;
uint8_t text_length;
unsigned char *text;
};
struct tm1637_dispinfo {
const uint8_t number;
const uint8_t *position;
const uint8_t buffer_length;
const uint8_t type;
};
struct tm1637_softc {
device_t dev;
phandle_t node;
gpio_pin_t sclpin;
gpio_pin_t sdapin;
uint8_t brightness;
bool on;
bool needupdate;
bool inuse;
struct tm1637_buf_t buffer;
u_int udelay; /* signal toggle delay in usec */
u_int scl_low_timeout;
struct mtx mtx;
const struct tm1637_dispinfo *info;
struct cdev *cdev;
};
static int tm1637_probe(device_t);
static int tm1637_attach(device_t);
static int tm1637_detach(device_t);
static void gpiobb_setsda(const device_t, const bool);
static void gpiobb_setscl(const device_t, const bool);
static int gpiobb_getsda(const device_t);
static int gpiobb_getscl(const device_t);
static int gpiobb_getack(const device_t);
static int gpiobb_start(const device_t);
static int gpiobb_stop(const device_t);
static void tm1637_set_speed(struct tm1637_softc *, const u_int);
static void tm1637_sysctl_register(struct tm1637_softc *);
static int digit_convert(u_char *, const unsigned char);
static int buffer_convert(struct tm1637_softc *);
static int is_raw_command(struct tm1637_softc *);
static int tm1637_send_command(struct tm1637_softc *, const uint8_t);
static int tm1637_send_data1(struct tm1637_softc *, const uint8_t);
static int tm1637_send_data(struct tm1637_softc *, size_t, const uint8_t);
static int tm1637_write(struct cdev*, struct uio*, int ioflag);
static int tm1637_ioctl(struct cdev*, u_long cmd, caddr_t data, int fflag, struct thread*);
static void tm1637_display_on(struct tm1637_softc*);
static void tm1637_display_off(struct tm1637_softc*);
static void tm1637_set_brightness(struct tm1637_softc*, const uint8_t);
/* Sequences of digit positions for a display type */
static const uint8_t dig4pos[] = { 0, 1, 2, 3 };
static const uint8_t dig6pos[] = { 2, 1, 0, 5, 4, 3 };
static const struct tm1637_dispinfo tm1637_disp_infos[] = {
{ 4, dig4pos, 6, TM1637_8TH_COLON },
{ 4, dig4pos, 10, TM1637_8TH_DOT },
{ 6, dig6pos, 14, TM1637_8TH_DOT },
};
static const struct ofw_compat_data tm1637_compat_data[] = {
{"tm1637-4-colon", (uintptr_t)&tm1637_disp_infos[0]},
{"tm1637-4-dots", (uintptr_t)&tm1637_disp_infos[1]},
{"tm1637-6-dots", (uintptr_t)&tm1637_disp_infos[2]},
{"tm1637", (uintptr_t)&tm1637_disp_infos[0]},
{NULL, (uintptr_t)NULL}
};
OFWBUS_PNP_INFO(tm1637_compat_data);
SIMPLEBUS_PNP_INFO(tm1637_compat_data);
static device_method_t tm1637_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, tm1637_probe),
DEVMETHOD(device_attach, tm1637_attach),
DEVMETHOD(device_detach, tm1637_detach),
DEVMETHOD_END
};
static devclass_t tm1637_devclass;
DEFINE_CLASS_0(tm1637, tm1637_driver, tm1637_methods, sizeof(struct tm1637_softc));
#ifdef FDT
DRIVER_MODULE(tm1637, simplebus, tm1637_driver, tm1637_devclass, NULL, NULL);
#endif
DRIVER_MODULE(tm1637, gpiobus, tm1637_driver, tm1637_devclass, NULL, NULL);
MODULE_VERSION(tm1637, 1);
MODULE_DEPEND(tm1637, gpiobus, 1, 1, 1);
#ifdef FDT
static int
tm1637_fdt_get_params(struct tm1637_softc *sc)
{
pcell_t param_cell;
ssize_t param_found;
uint32_t param;
param_found = OF_getencprop(sc->node, "default-brightness-level", ¶m_cell, sizeof(param_cell));
if (param_found > 0) {
param = (uint32_t)param_cell;
if (param > BRIGHT_BRIGHTEST) {
device_printf(sc->dev,
"could not acquire correct brightness level from DTS\n");
return (EINVAL);
}
sc->brightness = (uint8_t)param;
if (bootverbose)
device_printf(sc->dev,
"Acquired brightness level: %u %s\n", sc->brightness, "from DTS");
}
else {
sc->brightness = BRIGHT_TYPICAL;
if (bootverbose)
device_printf(sc->dev,
"Acquired brightness level: %u %s\n", sc->brightness, "by default");
}
return (0);
}
static int
tm1637_setup_fdt_pins(struct tm1637_softc *sc)
{
int err;
/*
* Historically, we used the first two array elements of the gpios
* property. The modern bindings specify separate scl-gpios and
* sda-gpios properties. We cope with whichever is present.
*/
if (OF_hasprop(sc->node, "gpios")) {
if ((err = gpio_pin_get_by_ofw_idx(sc->dev, sc->node, TM1637_SCL_IDX, &sc->sclpin)) != 0)
{
device_printf(sc->dev, "invalid gpios property for index:%d\n", TM1637_SCL_IDX);
return (err);
}
if ((err = gpio_pin_get_by_ofw_idx(sc->dev, sc->node, TM1637_SDA_IDX, &sc->sdapin)) != 0)
{
device_printf(sc->dev, "invalid gpios property for index:%d\n", TM1637_SDA_IDX);
return (err);
}
} else {
if ((err = gpio_pin_get_by_ofw_property(sc->dev, sc->node, TM1637_SCL_PROPERTY, &sc->sclpin)) != 0)
{
device_printf(sc->dev, "missing %s property\n", TM1637_SCL_PROPERTY);
return (err);
}
if ((err = gpio_pin_get_by_ofw_property(sc->dev, sc->node, TM1637_SDA_PROPERTY, &sc->sdapin)) != 0)
{
device_printf(sc->dev, "missing %s property\n", TM1637_SDA_PROPERTY);
return (err);
}
}
/* Get pin configuration from pinctrl-0 and ignore errors */
err = fdt_pinctrl_configure(sc->dev, 0);
#ifdef DEBUG
if (err != 0)
uprintf("No valid pinctrl-0 configuration. Error: %d\n", err);
else
uprintf("pinctrl-0 configration is OK\n");
#endif
return (0);
}
#endif /* FDT */
/*
* Sysctl parameter: brightness
*/
static int
tm1637_brightness_sysctl(SYSCTL_HANDLER_ARGS)
{
struct tm1637_softc *sc = arg1;
uint8_t brightness = sc->brightness;
int error;
error = SYSCTL_OUT(req, &brightness, sizeof(brightness));
if (error != 0 || req->newptr == NULL)
return (error);
error = SYSCTL_IN(req, &brightness, sizeof(brightness));
if (error != 0)
return (error);
if (brightness > 7)
return (EINVAL);
tm1637_set_brightness(sc, brightness);
return (0);
}
/*
* Sysctl parameter: on
*/
static int
tm1637_set_on_sysctl(SYSCTL_HANDLER_ARGS)
{
struct tm1637_softc *sc = arg1;
uint8_t _on = sc->on;
int error = 0;
error = SYSCTL_OUT(req, &_on, sizeof(_on));
if (error != 0 || req->newptr == NULL)
return (error);
error = SYSCTL_IN(req, &_on, sizeof(_on));
if (error != 0)
return (error);
switch(_on)
{
case 0:
tm1637_display_off(sc);
break;
case 1:
tm1637_display_on(sc);
break;
default:
error = EINVAL;
}
return (error);
}
static int
digit_convert(u_char *tm1637_digit, const unsigned char c)
{
switch (c) {
case '#': // Skip the digit but clear a dot or a colon
*tm1637_digit &= 0x7f;
break;
case ' ':
*tm1637_digit = CHR_SPACE;
break;
case '-':
*tm1637_digit = CHR_GYPHEN;
break;
default:
if ((c >= '0') && (c <= '9'))
*tm1637_digit = char_code[c&0x0f];
else
return (-1);
}
return (0);
}
/* Convert the date written to device */
static int
buffer_convert(struct tm1637_softc *sc)
{
const struct tm1637_dispinfo *info = sc->info;
const uint8_t *position = info->position;
const struct tm1637_buf_t *buf = &sc->buffer;
const unsigned char *text = buf->text;
uint8_t *codes = buf->codes;
int8_t n = info->number;
int8_t i = buf->length;
int8_t p;
if (info->type == TM1637_8TH_DOT) {
/* tm1637 with decimals
* Reverse order for right aligned result */
while (n-- > 0) {
unsigned char c;
p = position[n];
/* When the text buffer is cleared before the digits run out,
* a leading space will be displayed.
*/
if (i <= 0) {
codes[p] = CHR_SPACE;
continue;
}
c = text[--i];
if (c == '.') {
/* If a dot check for buffer is not empty,
* get a number followed by the dot (backward, of course),
* and set its eighth bit to light the dot segment
*/
if ((i <= 0) ||
(digit_convert(&codes[p], text[--i]) < 0))
return (-1);
/* Set a dot segment */
codes[p] |= 0x80;
}
else
if (digit_convert(&codes[p], c) < 0)
return (-1);
}
}
else
if(buf->length <= info->number) {
/* tm1637 4 digits with colon. Format: integer
* Reverse order for right aligned result */
while (n-- > 0) {
p = position[n];
/* When the text buffer is cleared before the digits run out,
* a leading space will be displayed.
*/
if (i <= 0)
codes[p] = CHR_SPACE;
else
if (digit_convert(&codes[p], text[--i]) < 0)
return (-1);
}
}
else
if (buf->length == 5) {
/* tm1637 4 digits with colon. Format: clock
* Reverse order for right aligned result */
while (i--, n-- > 0) {
p = position[n];
/* Skip a clockpoint position */
if (i == TM1637_COLON_POSITION + 1)
i--;
if (digit_convert(&codes[p], text[i]) < 0)
return (-1);
}
if (text[TM1637_COLON_POSITION] == ':') {
/* Set a dot segment */
p = position[TM1637_COLON_POSITION - 1];
codes[p] |= 0x80;
}
else
if (text[TM1637_COLON_POSITION] == ' ') {
/* Clear a dot segment */
p = position[TM1637_COLON_POSITION - 1];
codes[p] &= 0x7f;
}
else
return (-1);
}
/* The text buffer must be empty now */
if (i > 0)
return (-1);
return (0);
}
static int
is_raw_command(struct tm1637_softc *sc)
{
struct tm1637_buf_t *buf = &sc->buffer;
size_t start, stop, tmp;
uint8_t c;
switch (buf->text[0]) {
/* Send one byte at a fixed position */
case TM1637_ADDRESS_FIXED:
if ((buf->length < 3) ||
((start = buf->text[1]^TM1637_ADDRESS_START) >= sc->info->number))
return (-1);
buf->codes[start] = buf->text[2];
tm1637_send_data1(sc, start);
break;
/* Send up to 4 bytes at an autoincremented position */
case TM1637_ADDRESS_AUTO:
if ((buf->length < 3) ||
((start = buf->text[1]^TM1637_ADDRESS_START) >= sc->info->number))
return (-1);
tmp = start;
stop = MIN(buf->length-2, sc->info->number);
for (size_t i=2; tmp<stop; tmp++)
buf->codes[tmp] = buf->text[i++];
tm1637_send_data(sc, start, stop);
break;
/* Send one byte command to turn display off */
case TM1637_DISPLAY_OFF:
tm1637_display_off(sc);
break;
default:
/* Send one byte command to light display with the bright level 0..7 */
if ((c = (buf->text[0]^TM1637_DISPLAY_CTRL)) <= BRIGHT_BRIGHTEST) {
sc->brightness = c;
tm1637_display_on(sc);
break;
}
return (-1);
}
return (0);
}
static void
gpiobb_setsda(const device_t dev, const bool sda)
{
struct tm1637_softc *sc = device_get_softc(dev);
#ifndef I2C_LIKE
gpio_pin_setflags(sc->sdapin, GPIO_PIN_OUTPUT|GPIO_PIN_OPENDRAIN);
gpio_pin_set_active(sc->sdapin, sda);
#else
if (sda) {
gpio_pin_setflags(sc->sdapin, GPIO_PIN_INPUT);
} else {
gpio_pin_setflags(sc->sdapin, GPIO_PIN_OUTPUT|GPIO_PIN_OPENDRAIN);
gpio_pin_set_active(sc->sdapin, 0);
}
#endif
}
static void
gpiobb_setscl(const device_t dev, const bool scl)
{
struct tm1637_softc *sc = device_get_softc(dev);
#ifndef I2C_LIKE
gpio_pin_setflags(sc->sclpin, GPIO_PIN_OUTPUT|GPIO_PIN_OPENDRAIN);
gpio_pin_set_active(sc->sclpin, scl);
#else
if (scl) {
gpio_pin_setflags(sc->sclpin, GPIO_PIN_INPUT);
} else {
gpio_pin_setflags(sc->sclpin, GPIO_PIN_OUTPUT|GPIO_PIN_OPENDRAIN);
gpio_pin_set_active(sc->sclpin, 0);
}
#endif
}
static int
gpiobb_getsda(const device_t dev)
{
struct tm1637_softc *sc = device_get_softc(dev);
bool val;
gpio_pin_setflags(sc->sdapin, GPIO_PIN_INPUT);
gpio_pin_is_active(sc->sdapin, &val);
return (val);
}
static int
gpiobb_getscl(const device_t dev)
{
struct tm1637_softc *sc = device_get_softc(dev);
bool val;
gpio_pin_setflags(sc->sclpin, GPIO_PIN_INPUT);
gpio_pin_is_active(sc->sclpin, &val);
return (val);
}
static int
gpiobb_waitforscl(const device_t dev)
{
struct tm1637_softc *sc = device_get_softc(dev);
sbintime_t fast_timeout;
sbintime_t now, timeout;
/* Spin for up to 1 ms, then switch to pause. */
now = sbinuptime();
fast_timeout = now + SBT_1MS;
timeout = now + sc->scl_low_timeout * SBT_1US;
do {
if (GPIOBB_GETSCL(dev))
return (0);
now = sbinuptime();
} while (now < fast_timeout);
do {
I2C_DEBUG(printf("."));
pause_sbt("gpiobb-scl-low", SBT_1MS, C_PREL(8), 0);
if (GPIOBB_GETSCL(dev))
return (0);
now = sbinuptime();
} while (now < timeout);
I2C_DEBUG(printf("*"));
return (IIC_ETIMEOUT);
}
/* Start the high phase of the clock. */
static int
gpiobb_clockin(const device_t dev, const bool sda)
{
/*
* Precondition: SCL is low.
* Action:
* - set SDA to the value;
* - release SCL and wait until it's high.
* The caller is responsible for keeping SCL high for udelay.
*
* There should be a data set-up time, 250 ns minimum, between setting
* SDA and raising SCL. It's expected that the I/O access latency will
* naturally provide that delay.
*/
GPIOBB_SETSDA(dev, sda);
GPIOBB_SETSCL(dev, 1);
return (gpiobb_waitforscl(dev));
}
/*
* End the high phase of the clock and wait out the low phase
* as nothing interesting happens during it anyway.
*/
static void
gpiobb_clockout(const device_t dev)
{
struct tm1637_softc *sc = device_get_softc(dev);
/*
* Precondition: SCL is high.
* Action:
* - pull SCL low and hold for udelay.
*/
GPIOBB_SETSCL(dev, 0);
DELAY(sc->udelay);
}
static int
gpiobb_sendbit(const device_t dev, const bool bit)
{
#ifdef I2C_LIKE
struct tm1637_softc *sc = device_get_softc(dev);
#endif
int err;
err = gpiobb_clockin(dev, bit);
if (err != 0)
return (err);
#ifdef I2C_LIKE
DELAY(sc->udelay);
#endif
gpiobb_clockout(dev);
return (0);
}
/*
* Start command or data transmission
*/
static int
gpiobb_start(const device_t dev)
{
struct tm1637_softc *sc = device_get_softc(dev);
I2C_DEBUG(printf("<<"));
/* SCL must be high on the idle bus. */
if (gpiobb_waitforscl(dev) != 0) {
I2C_DEBUG(printf("C!\n"));
return (IIC_EBUSERR);
}
/*
* SDA must be high after the earlier stop condition or the end
* of Ack/NoAck pulse.
*/
if (!GPIOBB_GETSDA(dev)) {
I2C_DEBUG(printf("D!\n"));
return (IIC_EBUSERR);
}
/* Start: SDA high->low. */
GPIOBB_SETSDA(dev, 0);
/* Wait the second half of the SCL high phase. */
DELAY((sc->udelay + 1) / 2);
/* Pull SCL low to keep the bus reserved. */
gpiobb_clockout(dev);
return (0);
}
/*
* Stop command or data transmission
*/
static int
gpiobb_stop(const device_t dev)
{
struct tm1637_softc *sc = device_get_softc(dev);
int err = 0;
/*
* Stop: SDA goes from low to high in the middle of the SCL high phase.
*/
err = gpiobb_clockin(dev, 0);
if (err != 0)
return (err);
DELAY((sc->udelay + 1) / 2);
GPIOBB_SETSDA(dev, 1);
DELAY((sc->udelay + 1) / 2);
I2C_DEBUG(printf("%s>>", err != 0 ? "!" : ""));
I2C_DEBUG(printf("\n"));
return (err);
}
static int
gpiobb_getack(const device_t dev)
{
struct tm1637_softc *sc = device_get_softc(dev);
int noack, err;
int t;
/* Release SDA so that the slave can drive it. */
err = gpiobb_clockin(dev, 1);
if (err != 0) {
I2C_DEBUG(printf("! "));
return (err);
}
/* Sample SDA until ACK (low) or udelay runs out. */
for (t = 0; t < sc->udelay; t++) {
noack = GPIOBB_GETSDA(dev);
if (!noack)
break;
DELAY(1);
}
#ifdef I2C_LIKE
DELAY(sc->udelay - t);
#endif
gpiobb_clockout(dev);
I2C_DEBUG(printf("%c ", noack ? '-' : '+'));
return (noack ? IIC_ENOACK : 0);
}
/*
* Send a byte of information w/ ack checking
*/
static int
tm1637_gpio_sendbyte(const device_t dev, const uint8_t data)
{
int err;
uint8_t i;
// LSB first
for(i=0; i<=7; i++) {
err = gpiobb_sendbit(dev, (data & (1 << i)) != 0);
if (err != 0) {
I2C_DEBUG(printf("w!"));
return (err);
}
}
I2C_DEBUG(printf("w%02x", data));
return (gpiobb_getack(dev));
}
/*
* Sends one byte command with a retry if no acnowledge occurs
* Returns zero on success
*/
static int
tm1637_send_command(struct tm1637_softc *sc, const uint8_t cmd)
{
const device_t dev = sc->dev;
int err;
TM1637_LOCK(sc);
gpiobb_start(dev);
err = tm1637_gpio_sendbyte(dev, cmd);
gpiobb_stop(dev);
TM1637_UNLOCK(sc);
return err;
}
/*
* Sends an address and one byte data
* Returns zero on success
*/
static int
tm1637_send_data1(struct tm1637_softc *sc, const uint8_t pos)
{
const device_t dev = sc->dev;
const uint8_t addr = TM1637_ADDRESS_START + pos;
const uint8_t data = sc->buffer.codes[pos];
if (sc->on) {
TM1637_LOCK(sc);
gpiobb_start(dev);
tm1637_gpio_sendbyte(dev, TM1637_ADDRESS_FIXED);
gpiobb_stop(dev);
gpiobb_start(dev);
tm1637_gpio_sendbyte(dev, addr);
tm1637_gpio_sendbyte(dev, data);
gpiobb_stop(dev);
TM1637_UNLOCK(sc);
}
return (0);
}
/*
* Send number of bytes fron first to last address
* Returns zero on success
*/
static int
tm1637_send_data(struct tm1637_softc *sc, size_t pos, const uint8_t stop)
{
const device_t dev = sc->dev;
const uint8_t addr = TM1637_ADDRESS_START + pos;
const uint8_t *codes = sc->buffer.codes;
if (sc->on) {
TM1637_LOCK(sc);
gpiobb_start(dev);
tm1637_gpio_sendbyte(dev, TM1637_ADDRESS_AUTO);
gpiobb_stop(dev);
gpiobb_start(dev);
tm1637_gpio_sendbyte(dev, addr);
while(pos < stop)
tm1637_gpio_sendbyte(dev, codes[pos++]);
gpiobb_stop(dev);
TM1637_UNLOCK(sc);
}
return (0);
}
/*
* Display or clear a clockpoint
*/
static void
tm1637_display_clockpoint(struct tm1637_softc *sc, const bool clockpoint)
{
const uint8_t p = sc->info->position[TM1637_COLON_POSITION - 1];
if (clockpoint)
sc->buffer.codes[p] |= 0x80;
else
sc->buffer.codes[p] &= 0x7f;
tm1637_send_data1(sc, p);
}
/*
* Send to display all 4 digits (6 bytes will be sended)
*/
static void
tm1637_update_display(struct tm1637_softc *sc)
{
if(sc->on) {
tm1637_send_data(sc, 0, sc->info->number);
/* Clear the flag if it is set */
if(sc->needupdate)
sc->needupdate = false;
}
else
sc->needupdate = true; /* Do nothing but set flag */
}
/*
* Writes all blanks to a display
*/
static void
tm1637_clear_display(struct tm1637_softc *sc)
{
uint8_t position = sc->info->number;
/* Display all blanks */
while(position > 0)
sc->buffer.codes[--position] = CHR_SPACE;
/* If display is off right now then mark it for update when it is on */
if(sc->on)
tm1637_send_data(sc, 0, sc->info->number);
else
sc->needupdate = true;
}
/*
* Sets a display on with a brightness value from softc
*/
static void
tm1637_display_on(struct tm1637_softc *sc)
{
if(!sc->on) {
sc->on = true;
tm1637_send_data(sc, 0, sc->info->number);
}
/* Light the display anyway */
tm1637_send_command(sc, TM1637_DISPLAY_CTRL|sc->brightness);
}
/*
* Sets a display on with a brightness value as parameter
*/
static void
tm1637_set_brightness(struct tm1637_softc *sc, const uint8_t brightness)
{
/* If brightness level is correct */
if (brightness <= BRIGHT_BRIGHTEST) {
/* Store a value as a current brightness now */
sc->brightness = brightness;
/* Do not send a command unless a display is already on */
if(sc->on)
tm1637_send_command(sc, TM1637_DISPLAY_CTRL|brightness);
}
}
/*
* Set a display off
*/
static void
tm1637_display_off(struct tm1637_softc *sc)
{
/* Do nothing is always off */
if(sc->on) {
sc->on = false;
tm1637_send_command(sc, TM1637_DISPLAY_OFF);
}
}
/*
* Sets time to the display
*/
static void
tm1637_set_clock(struct tm1637_softc *sc, struct tm1637_clock_t clock)
{
const uint8_t *position = sc->info->position;
uint8_t *codes = sc->buffer.codes;
uint8_t p, t;
t = clock.tm_hour / 10;
p = position[0];
codes[p] = char_code[t&0x0f];
t = clock.tm_hour % 10;
p = position[1];
codes[p] = char_code[t];
t = clock.tm_min / 10;
p = position[2];
codes[p] = char_code[t&0x0f];
t = clock.tm_min % 10;
p = position[3];
codes[p] = char_code[t];
/* Clockpoint */
if (clock.tm_colon) {
p = position[TM1637_COLON_POSITION - 1];
codes[p] |= 0x80;
}
tm1637_update_display(sc);
}
static int
tm1637_setup_hinted_pins(struct tm1637_softc *sc)
{
device_t busdev;
const char *busname, *devname;
int err, numpins, sclnum, sdanum, unit;