2 * (C) Copyright 2003, Li-Pro.Net <www.li-pro.net>
5 * SPDX-License-Identifier: GPL-2.0+
9 * NIOS PIO based seven segment led support functions
15 #ifdef CONFIG_SEVENSEG
17 #define SEVENDEG_MASK_DP ((SEVENSEG_DIGIT_DP << 8) | SEVENSEG_DIGIT_DP)
19 #ifdef SEVENSEG_WRONLY /* emulate read access */
20 #if (SEVENSEG_ACTIVE == 0)
21 static unsigned int sevenseg_portval = ~0;
23 static unsigned int sevenseg_portval = 0;
27 static int sevenseg_init_done = 0;
29 static inline void __sevenseg_set_masked (unsigned int mask, int value)
31 nios_pio_t *piop __attribute__((unused)) = (nios_pio_t*)SEVENSEG_BASE;
33 #ifdef SEVENSEG_WRONLY /* emulate read access */
35 #if (SEVENSEG_ACTIVE == 0)
37 sevenseg_portval &= ~mask;
39 sevenseg_portval |= mask;
42 sevenseg_portval |= mask;
44 sevenseg_portval &= ~mask;
47 piop->data = sevenseg_portval;
49 #else /* !SEVENSEG_WRONLY */
51 #if (SEVENSEG_ACTIVE == 0)
63 #endif /* SEVENSEG_WRONLY */
66 static inline void __sevenseg_toggle_masked (unsigned int mask)
68 nios_pio_t *piop = (nios_pio_t*)SEVENSEG_BASE;
70 #ifdef SEVENSEG_WRONLY /* emulate read access */
72 sevenseg_portval ^= mask;
73 piop->data = sevenseg_portval;
75 #else /* !SEVENSEG_WRONLY */
79 #endif /* SEVENSEG_WRONLY */
82 static inline void __sevenseg_set (unsigned int value)
84 nios_pio_t *piop __attribute__((unused)) = (nios_pio_t*)SEVENSEG_BASE;
86 #ifdef SEVENSEG_WRONLY /* emulate read access */
88 #if (SEVENSEG_ACTIVE == 0)
89 sevenseg_portval = (sevenseg_portval & SEVENDEG_MASK_DP)
90 | ((~value) & (~SEVENDEG_MASK_DP));
92 sevenseg_portval = (sevenseg_portval & SEVENDEG_MASK_DP)
96 piop->data = sevenseg_portval;
98 #else /* !SEVENSEG_WRONLY */
100 #if (SEVENSEG_ACTIVE == 0)
101 piop->data = (piop->data & SEVENDEG_MASK_DP)
102 | ((~value) & (~SEVENDEG_MASK_DP));
104 piop->data = (piop->data & SEVENDEG_MASK_DP)
108 #endif /* SEVENSEG_WRONLY */
111 static inline void __sevenseg_init (void)
113 nios_pio_t *piop __attribute__((unused)) = (nios_pio_t*)SEVENSEG_BASE;
117 #ifndef SEVENSEG_WRONLY /* setup direction */
119 piop->direction |= mask;
121 #endif /* SEVENSEG_WRONLY */
125 void sevenseg_set(int value)
127 unsigned char digits[] = {
146 if (!sevenseg_init_done) {
148 sevenseg_init_done++;
151 switch (value & SEVENSEG_MASK_CTRL) {
155 (digits[((value & SEVENSEG_MASK_VAL) >> 4)] << 8) |
156 digits[((value & SEVENSEG_MASK_VAL) & 0xf)] ) );
158 break; /* paranoia */
162 __sevenseg_set_masked(SEVENDEG_MASK_DP, 0);
164 break; /* paranoia */
166 case SEVENSEG_SET_DPL:
167 __sevenseg_set_masked(SEVENSEG_DIGIT_DP, 1);
169 break; /* paranoia */
171 case SEVENSEG_SET_DPH:
172 __sevenseg_set_masked((SEVENSEG_DIGIT_DP << 8), 1);
174 break; /* paranoia */
176 case SEVENSEG_RES_DPL:
177 __sevenseg_set_masked(SEVENSEG_DIGIT_DP, 0);
179 break; /* paranoia */
181 case SEVENSEG_RES_DPH:
182 __sevenseg_set_masked((SEVENSEG_DIGIT_DP << 8), 0);
184 break; /* paranoia */
186 case SEVENSEG_TOG_DPL:
187 __sevenseg_toggle_masked(SEVENSEG_DIGIT_DP);
189 break; /* paranoia */
191 case SEVENSEG_TOG_DPH:
192 __sevenseg_toggle_masked((SEVENSEG_DIGIT_DP << 8));
194 break; /* paranoia */
204 #endif /* CONFIG_SEVENSEG */