new SOURCES file that specifies which files to build in each dir, use
[kugel-rb.git] / firmware / backlight.c
blob2a726b65a4479ed9ce49d6d701efbb8bd7de4b2a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include <stdlib.h>
21 #include "sh7034.h"
22 #include "kernel.h"
23 #include "thread.h"
24 #include "i2c.h"
25 #include "debug.h"
26 #include "rtc.h"
27 #include "usb.h"
28 #include "power.h"
29 #include "system.h"
31 const char backlight_timeout_value[19] =
33 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
36 #ifdef HAVE_BACKLIGHT
38 #define BACKLIGHT_ON 1
39 #define BACKLIGHT_OFF 2
41 static void backlight_thread(void);
42 static char backlight_stack[DEFAULT_STACK_SIZE];
43 static const char backlight_thread_name[] = "backlight";
44 static struct event_queue backlight_queue;
46 static bool charger_was_inserted = 0;
47 static bool backlight_on_when_charging = 0;
49 static int backlight_timer;
50 static unsigned int backlight_timeout = 5;
52 void backlight_thread(void)
54 struct event ev;
56 while(1)
58 queue_wait(&backlight_queue, &ev);
59 switch(ev.id)
61 case BACKLIGHT_ON:
62 if( backlight_on_when_charging && charger_inserted() )
64 /* Forcing to zero keeps the lights on */
65 backlight_timer = 0;
67 else
69 backlight_timer = HZ*backlight_timeout_value[backlight_timeout];
72 if(backlight_timer < 0)
74 backlight_timer = 0; /* timer value 0 will not get ticked */
75 #ifdef HAVE_RTC
76 /* Disable square wave */
77 rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
78 #else
79 and_b(~0x40, &PAIORH);
80 #endif
82 /* else if(backlight_timer) */
83 else
85 #ifdef HAVE_RTC
86 /* Enable square wave */
87 rtc_write(0x0a, rtc_read(0x0a) | 0x40);
88 #else
89 and_b(~0x40, &PADRH);
90 or_b(0x40, &PAIORH);
91 #endif
93 break;
95 case BACKLIGHT_OFF:
96 #ifdef HAVE_RTC
97 /* Disable square wave */
98 rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
99 #else
100 and_b(~0x40, &PAIORH);
101 #endif
102 break;
104 case SYS_USB_CONNECTED:
105 /* Tell the USB thread that we are safe */
106 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
107 usb_acknowledge(SYS_USB_CONNECTED_ACK);
108 break;
110 case SYS_USB_DISCONNECTED:
111 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
112 break;
117 void backlight_on(void)
119 queue_post(&backlight_queue, BACKLIGHT_ON, NULL);
122 void backlight_off(void)
124 queue_post(&backlight_queue, BACKLIGHT_OFF, NULL);
127 int backlight_get_timeout(void)
129 return backlight_timeout;
132 void backlight_set_timeout(int index)
134 if((unsigned)index >= sizeof(backlight_timeout_value))
135 /* if given a weird value, use 0 */
136 index=0;
137 backlight_timeout = index; /* index in the backlight_timeout_value table */
138 backlight_on();
141 bool backlight_get_on_when_charging(void)
143 return backlight_on_when_charging;
146 void backlight_set_on_when_charging(bool yesno)
148 backlight_on_when_charging = yesno;
149 backlight_on();
152 void backlight_tick(void)
154 bool charger_is_inserted = charger_inserted();
155 if( backlight_on_when_charging &&
156 (charger_was_inserted != charger_is_inserted) )
158 backlight_on();
160 charger_was_inserted = charger_is_inserted;
162 if(backlight_timer)
164 backlight_timer--;
165 if(backlight_timer == 0)
167 backlight_off();
172 void backlight_init(void)
174 queue_init(&backlight_queue);
175 create_thread(backlight_thread, backlight_stack,
176 sizeof(backlight_stack), backlight_thread_name);
178 backlight_on();
181 #else /* no backlight, empty dummy functions */
183 void backlight_init(void) {}
184 void backlight_on(void) {}
185 void backlight_off(void) {}
186 void backlight_tick(void) {}
187 int backlight_get_timeout(void) {return 0;}
188 void backlight_set_timeout(int index) {(void)index;}
189 bool backlight_get_on_when_charging(void) {return 0;}
190 void backlight_set_on_when_charging(bool yesno) {(void)yesno;}
192 #endif /* #ifdef HAVE_BACKLIGHT */