Fixed red build
[kugel-rb.git] / firmware / backlight.c
blob7b58fc2f904e0bc06b416e7cd19101ea90437ea2
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"
30 #define BACKLIGHT_ON 1
31 #define BACKLIGHT_OFF 2
33 static void backlight_thread(void);
34 static char backlight_stack[DEFAULT_STACK_SIZE];
35 static char backlight_thread_name[] = "backlight";
36 static struct event_queue backlight_queue;
38 static bool charger_was_inserted = 0;
39 static bool backlight_on_when_charging = 0;
41 static int backlight_timer;
42 static int backlight_timeout = 5;
44 static char timeout_value[19] =
46 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
49 void backlight_thread(void)
51 struct event ev;
53 while(1)
55 queue_wait(&backlight_queue, &ev);
56 switch(ev.id)
58 case BACKLIGHT_ON:
59 if( backlight_on_when_charging && charger_inserted() )
61 /* Forcing to zero keeps the lights on */
62 backlight_timer = 0;
64 else
66 backlight_timer = HZ*timeout_value[backlight_timeout];
69 if(backlight_timer < 0)
71 backlight_timer = 0; /* timer value 0 will not get ticked */
72 #ifdef HAVE_RTC
73 /* Disable square wave */
74 rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
75 #else
76 PADR |= 0x4000;
77 #endif
79 /* else if(backlight_timer) */
80 else
82 #ifdef HAVE_RTC
83 /* Enable square wave */
84 rtc_write(0x0a, rtc_read(0x0a) | 0x40);
85 #else
86 PADR &= ~0x4000;
87 #endif
89 break;
91 case BACKLIGHT_OFF:
92 #ifdef HAVE_RTC
93 /* Disable square wave */
94 rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
95 #else
96 PADR |= 0x4000;
97 #endif
98 break;
100 case SYS_USB_CONNECTED:
101 /* Tell the USB thread that we are safe */
102 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
103 usb_acknowledge(SYS_USB_CONNECTED_ACK);
104 break;
106 case SYS_USB_DISCONNECTED:
107 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
108 break;
113 void backlight_on(void)
115 queue_post(&backlight_queue, BACKLIGHT_ON, NULL);
118 void backlight_off(void)
120 queue_post(&backlight_queue, BACKLIGHT_OFF, NULL);
123 void backlight_set_timeout(int seconds)
125 backlight_timeout = seconds;
126 backlight_on();
129 void backlight_set_on_when_charging(bool yesno)
131 backlight_on_when_charging = yesno;
132 backlight_on();
135 void backlight_tick(void)
137 bool charger_is_inserted = charger_inserted();
138 if( backlight_on_when_charging &&
139 (charger_was_inserted != charger_is_inserted) )
141 backlight_on();
143 charger_was_inserted = charger_is_inserted;
145 if(backlight_timer)
147 backlight_timer--;
148 if(backlight_timer == 0)
150 backlight_off();
155 void backlight_init(void)
157 queue_init(&backlight_queue);
158 create_thread(backlight_thread, backlight_stack,
159 sizeof(backlight_stack), backlight_thread_name);
161 PAIOR |= 0x4000;
163 backlight_on();
166 /* -----------------------------------------------------------------
167 * local variables:
168 * eval: (load-file "rockbox-mode.el")
169 * end: