Fix issues with file-based bootloader installation:
[Rockbox.git] / firmware / backlight.c
blob0f66fbf574ddea0af8f54c1da29f46fe1aad63ea
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 "cpu.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"
30 #include "button.h"
31 #include "timer.h"
32 #include "backlight.h"
33 #include "lcd.h"
35 #ifdef HAVE_REMOTE_LCD
36 #include "lcd-remote.h"
37 #endif
38 #ifndef SIMULATOR
39 #include "backlight-target.h"
40 #endif
42 #ifdef SIMULATOR
43 /* TODO: find a better way to do it but we need a kernel thread somewhere to
44 handle this */
45 extern void screen_dump(void);
47 static inline void _backlight_on(void)
49 sim_backlight(100);
52 static inline void _backlight_off(void)
54 sim_backlight(0);
57 static inline void _backlight_set_brightness(int val)
59 (void)val;
62 static inline void _buttonlight_on(void)
66 static inline void _buttonlight_off(void)
70 static inline void _buttonlight_set_brightness(int val)
72 (void)val;
74 #ifdef HAVE_REMOTE_LCD
75 static inline void _remote_backlight_on(void)
77 sim_remote_backlight(100);
80 static inline void _remote_backlight_off(void)
82 sim_remote_backlight(0);
84 #endif /* HAVE_REMOTE_LCD */
86 #endif /* SIMULATOR */
88 #if defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER)
90 enum {
91 BACKLIGHT_ON,
92 BACKLIGHT_OFF,
93 #ifdef HAVE_REMOTE_LCD
94 REMOTE_BACKLIGHT_ON,
95 REMOTE_BACKLIGHT_OFF,
96 #endif
97 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
98 BACKLIGHT_FADE_FINISH,
99 #endif
100 #ifdef HAVE_LCD_SLEEP
101 LCD_SLEEP,
102 #endif
103 #ifdef HAVE_BUTTON_LIGHT
104 BUTTON_LIGHT_ON,
105 BUTTON_LIGHT_OFF,
106 #endif
109 static void backlight_thread(void);
110 static long backlight_stack[DEFAULT_STACK_SIZE/sizeof(long)];
111 static const char backlight_thread_name[] = "backlight";
112 static struct event_queue backlight_queue;
114 static int backlight_timer;
115 static int backlight_timeout;
116 static int backlight_timeout_normal = 5*HZ;
117 #if CONFIG_CHARGING
118 static int backlight_timeout_plugged = 5*HZ;
119 #endif
120 #ifdef HAS_BUTTON_HOLD
121 static int backlight_on_button_hold = 0;
122 #endif
124 #ifdef HAVE_BUTTON_LIGHT
125 static int buttonlight_timer;
126 int _buttonlight_timeout = 5*HZ;
128 /* Update state of buttonlight according to timeout setting */
129 static void buttonlight_update_state(void)
131 buttonlight_timer = _buttonlight_timeout;
133 /* Buttonlight == OFF in the setting? */
134 if (buttonlight_timer < 0)
136 buttonlight_timer = 0; /* Disable the timeout */
137 _buttonlight_off();
139 else
140 _buttonlight_on();
143 /* external interface */
144 void buttonlight_on(void)
146 queue_remove_from_head(&backlight_queue, BUTTON_LIGHT_ON);
147 queue_post(&backlight_queue, BUTTON_LIGHT_ON, 0);
150 void buttonlight_off(void)
152 queue_post(&backlight_queue, BUTTON_LIGHT_OFF, 0);
155 void buttonlight_set_timeout(int value)
157 _buttonlight_timeout = HZ * value;
158 buttonlight_update_state();
161 #endif
163 #ifdef HAVE_REMOTE_LCD
164 static int remote_backlight_timer;
165 static int remote_backlight_timeout;
166 static int remote_backlight_timeout_normal = 5*HZ;
167 #if CONFIG_CHARGING
168 static int remote_backlight_timeout_plugged = 5*HZ;
169 #endif
170 #ifdef HAS_REMOTE_BUTTON_HOLD
171 static int remote_backlight_on_button_hold = 0;
172 #endif
173 #endif
175 #ifdef HAVE_LCD_SLEEP
176 const signed char lcd_sleep_timeout_value[10] =
178 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
180 int _lcd_sleep_timer;
181 int _lcd_sleep_timeout = 10*HZ;
182 #endif
184 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
185 /* backlight fading */
186 #define BL_PWM_INTERVAL 5 /* Cycle interval in ms */
187 #define BL_PWM_BITS 8
188 #define BL_PWM_COUNT (1<<BL_PWM_BITS)
190 /* s15.16 fixed point variables */
191 static int32_t bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/300;
192 static int32_t bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/2000;
193 static int32_t bl_dim_fraction = 0;
195 static int bl_dim_target = 0;
196 static int bl_dim_current = 0;
197 static enum {DIM_STATE_START, DIM_STATE_MAIN} bl_dim_state = DIM_STATE_START;
198 static bool bl_timer_active = false;
200 static void backlight_isr(void)
202 int timer_period = (TIMER_FREQ*BL_PWM_INTERVAL/1000);
203 bool idle = false;
205 switch (bl_dim_state)
207 /* New cycle */
208 case DIM_STATE_START:
209 bl_dim_current = bl_dim_fraction >> 16;
211 if (bl_dim_current > 0 && bl_dim_current < BL_PWM_COUNT)
213 _backlight_on_isr();
214 timer_period = (timer_period * bl_dim_current) >> BL_PWM_BITS;
215 bl_dim_state = DIM_STATE_MAIN;
217 else
219 if (bl_dim_current)
220 _backlight_on_isr();
221 else
222 _backlight_off_isr();
223 if (bl_dim_current == bl_dim_target)
224 idle = true;
226 if (bl_dim_current < bl_dim_target)
228 bl_dim_fraction = MIN(bl_dim_fraction + bl_fade_in_step,
229 (BL_PWM_COUNT<<16));
231 else if (bl_dim_current > bl_dim_target)
233 bl_dim_fraction = MAX(bl_dim_fraction - bl_fade_out_step, 0);
235 break;
237 /* Dim main screen */
238 case DIM_STATE_MAIN:
239 _backlight_off_isr();
240 timer_period = (timer_period * (BL_PWM_COUNT - bl_dim_current))
241 >> BL_PWM_BITS;
242 bl_dim_state = DIM_STATE_START;
243 break ;
245 if (idle)
247 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
248 queue_post(&backlight_queue, BACKLIGHT_FADE_FINISH, 0);
249 #endif
250 timer_unregister();
251 bl_timer_active = false;
253 else
254 timer_set_period(timer_period);
257 static void backlight_switch(void)
259 if (bl_dim_target > (BL_PWM_COUNT/2))
261 _backlight_on_normal();
262 bl_dim_fraction = (BL_PWM_COUNT<<16);
264 else
266 _backlight_off_normal();
267 bl_dim_fraction = 0;
271 static void backlight_release_timer(void)
273 #ifdef _BACKLIGHT_FADE_BOOST
274 cpu_boost(false);
275 #endif
276 timer_unregister();
277 bl_timer_active = false;
278 backlight_switch();
281 static void backlight_dim(int value)
283 /* protect from extraneous calls with the same target value */
284 if (value == bl_dim_target)
285 return;
287 bl_dim_target = value;
289 if (bl_timer_active)
290 return ;
292 if (timer_register(0, backlight_release_timer, 2, 0, backlight_isr))
294 #ifdef _BACKLIGHT_FADE_BOOST
295 /* Prevent cpu frequency changes while dimming. */
296 cpu_boost(true);
297 #endif
298 bl_timer_active = true;
300 else
301 backlight_switch();
304 static void _backlight_on(void)
306 if (bl_fade_in_step > 0)
308 #ifdef _BACKLIGHT_FADE_ENABLE
309 _backlight_hw_enable(true);
310 #endif
311 backlight_dim(BL_PWM_COUNT);
313 else
315 bl_dim_target = BL_PWM_COUNT;
316 bl_dim_fraction = (BL_PWM_COUNT<<16);
317 _backlight_on_normal();
319 #ifdef HAVE_LCD_SLEEP
320 _lcd_sleep_timer = 0; /* LCD should be awake already */
321 #endif
324 static void _backlight_off(void)
326 if (bl_fade_out_step > 0)
328 backlight_dim(0);
330 else
332 bl_dim_target = bl_dim_fraction = 0;
333 _backlight_off_normal();
335 #ifdef HAVE_LCD_SLEEP
336 /* Start LCD sleep countdown */
337 if (_lcd_sleep_timeout < 0)
339 _lcd_sleep_timer = 0; /* Setting == Always */
340 lcd_sleep();
342 else
343 _lcd_sleep_timer = _lcd_sleep_timeout;
344 #endif
347 void backlight_set_fade_in(int value)
349 if (value > 0)
350 bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
351 else
352 bl_fade_in_step = 0;
355 void backlight_set_fade_out(int value)
357 if (value > 0)
358 bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
359 else
360 bl_fade_out_step = 0;
362 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
364 /* Update state of backlight according to timeout setting */
365 static void backlight_update_state(void)
367 #ifdef HAS_BUTTON_HOLD
368 if ((backlight_on_button_hold != 0)
369 #ifdef HAVE_REMOTE_LCD_AS_MAIN
370 && remote_button_hold()
371 #else
372 && button_hold()
373 #endif
375 backlight_timeout = (backlight_on_button_hold == 2) ? 0 : -1;
376 /* always on or always off */
377 else
378 #endif
379 #if CONFIG_CHARGING
380 if (charger_inserted()
381 #ifdef HAVE_USB_POWER
382 || usb_powered()
383 #endif
385 backlight_timeout = backlight_timeout_plugged;
386 else
387 #endif
388 backlight_timeout = backlight_timeout_normal;
390 /* Backlight == OFF in the setting? */
391 if (backlight_timeout < 0)
393 backlight_timer = 0; /* Disable the timeout */
394 _backlight_off();
396 else
398 backlight_timer = backlight_timeout;
399 _backlight_on();
403 #ifdef HAVE_REMOTE_LCD
404 /* Update state of remote backlight according to timeout setting */
405 static void remote_backlight_update_state(void)
407 #ifdef HAS_REMOTE_BUTTON_HOLD
408 if (remote_button_hold() && (remote_backlight_on_button_hold != 0))
409 remote_backlight_timeout = (remote_backlight_on_button_hold == 2)
410 ? 0 : -1; /* always on or always off */
411 else
412 #endif
413 #if CONFIG_CHARGING
414 if (charger_inserted()
415 #ifdef HAVE_USB_POWER
416 || usb_powered()
417 #endif
419 remote_backlight_timeout = remote_backlight_timeout_plugged;
420 else
421 #endif
422 remote_backlight_timeout = remote_backlight_timeout_normal;
424 /* Backlight == OFF in the setting? */
425 if (remote_backlight_timeout < 0)
427 remote_backlight_timer = 0; /* Disable the timeout */
428 _remote_backlight_off();
430 else
432 remote_backlight_timer = remote_backlight_timeout;
433 _remote_backlight_on();
436 #endif /* HAVE_REMOTE_LCD */
438 void backlight_thread(void)
440 struct queue_event ev;
441 bool locked = false;
443 while(1)
445 queue_wait(&backlight_queue, &ev);
446 switch(ev.id)
447 { /* These events must always be processed */
448 #ifdef _BACKLIGHT_FADE_BOOST
449 case BACKLIGHT_FADE_FINISH:
450 cpu_boost(false);
451 break;
452 #endif
453 #ifdef _BACKLIGHT_FADE_ENABLE
454 case BACKLIGHT_FADE_FINISH:
455 _backlight_hw_enable((bl_dim_current|bl_dim_target) != 0);
456 break;
457 #endif
459 #ifndef SIMULATOR
460 /* Here for now or else the aggressive init messes up scrolling */
461 #ifdef HAVE_REMOTE_LCD
462 case SYS_REMOTE_PLUGGED:
463 lcd_remote_on();
464 lcd_remote_update();
465 break;
467 case SYS_REMOTE_UNPLUGGED:
468 lcd_remote_off();
469 break;
470 #elif defined HAVE_REMOTE_LCD_AS_MAIN
471 case SYS_REMOTE_PLUGGED:
472 lcd_on();
473 lcd_update();
474 break;
476 case SYS_REMOTE_UNPLUGGED:
477 lcd_off();
478 break;
479 #endif /* HAVE_REMOTE_LCD/ HAVE_REMOTE_LCD_AS_MAIN */
480 #endif /* !SIMULATOR */
481 #ifdef SIMULATOR
482 /* This one here too for lack of a better place */
483 case SYS_SCREENDUMP:
484 screen_dump();
485 break;
486 #endif
487 case SYS_USB_CONNECTED:
488 /* Tell the USB thread that we are safe */
489 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
490 usb_acknowledge(SYS_USB_CONNECTED_ACK);
491 break;
493 case SYS_USB_DISCONNECTED:
494 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
495 break;
497 if (locked)
498 continue;
500 switch(ev.id)
501 { /* These events are only processed if backlight isn't locked */
502 #ifdef HAVE_REMOTE_LCD
503 case REMOTE_BACKLIGHT_ON:
504 remote_backlight_update_state();
505 break;
507 case REMOTE_BACKLIGHT_OFF:
508 remote_backlight_timer = 0; /* Disable the timeout */
509 _remote_backlight_off();
510 break;
511 #endif /* HAVE_REMOTE_LCD */
513 case BACKLIGHT_ON:
514 backlight_update_state();
515 break;
517 case BACKLIGHT_OFF:
518 backlight_timer = 0; /* Disable the timeout */
519 _backlight_off();
520 break;
522 #ifdef HAVE_LCD_SLEEP
523 case LCD_SLEEP:
524 lcd_sleep();
525 break;
526 #endif
527 #ifdef HAVE_BUTTON_LIGHT
528 case BUTTON_LIGHT_ON:
529 buttonlight_update_state();
530 break;
532 case BUTTON_LIGHT_OFF:
533 buttonlight_timer = 0;
534 _buttonlight_off();
535 break;
536 #endif
538 case SYS_POWEROFF: /* Lock backlight on poweroff so it doesn't */
539 locked = true; /* go off before power is actually cut. */
540 /* fall through */
541 #if CONFIG_CHARGING
542 case SYS_CHARGER_CONNECTED:
543 case SYS_CHARGER_DISCONNECTED:
544 #endif
545 backlight_update_state();
546 #ifdef HAVE_REMOTE_LCD
547 remote_backlight_update_state();
548 #endif
549 break;
551 } /* end while */
554 static void backlight_tick(void)
556 if(backlight_timer)
558 backlight_timer--;
559 if(backlight_timer == 0)
561 backlight_off();
564 #ifdef HAVE_LCD_SLEEP
565 else if(_lcd_sleep_timer)
567 _lcd_sleep_timer--;
568 if(_lcd_sleep_timer == 0)
570 /* Queue on bl thread or freeze! */
571 queue_post(&backlight_queue, LCD_SLEEP, 0);
574 #endif /* HAVE_LCD_SLEEP */
576 #ifdef HAVE_REMOTE_LCD
577 if(remote_backlight_timer)
579 remote_backlight_timer--;
580 if(remote_backlight_timer == 0)
582 remote_backlight_off();
585 #endif /* HAVE_REMOVE_LCD */
586 #ifdef HAVE_BUTTON_LIGHT
587 if (buttonlight_timer)
589 buttonlight_timer--;
590 if (buttonlight_timer == 0)
592 buttonlight_off();
595 #endif /* HAVE_BUTTON_LIGHT */
598 void backlight_init(void)
600 queue_init(&backlight_queue, true);
602 #ifndef SIMULATOR
603 if (_backlight_init())
605 # ifdef HAVE_BACKLIGHT_PWM_FADING
606 /* If backlight is already on, don't fade in. */
607 bl_dim_target = BL_PWM_COUNT;
608 bl_dim_fraction = (BL_PWM_COUNT<<16);
609 # endif
611 #endif
612 /* Leave all lights as set by the bootloader here. The settings load will
613 * call the appropriate backlight_set_*() functions, only changing light
614 * status if necessary. */
616 create_thread(backlight_thread, backlight_stack,
617 sizeof(backlight_stack), 0, backlight_thread_name
618 IF_PRIO(, PRIORITY_USER_INTERFACE)
619 IF_COP(, CPU));
620 tick_add_task(backlight_tick);
623 void backlight_on(void)
625 queue_remove_from_head(&backlight_queue, BACKLIGHT_ON);
626 queue_post(&backlight_queue, BACKLIGHT_ON, 0);
629 void backlight_off(void)
631 queue_post(&backlight_queue, BACKLIGHT_OFF, 0);
634 /* returns true when the backlight is on OR when it's set to always off */
635 bool is_backlight_on(void)
637 if (backlight_timer || backlight_timeout <= 0)
638 return true;
639 else
640 return false;
643 /* return value in ticks; 0 means always on, <0 means always off */
644 int backlight_get_current_timeout(void)
646 return backlight_timeout;
649 void backlight_set_timeout(int value)
651 backlight_timeout_normal = HZ * value;
652 backlight_update_state();
655 #if CONFIG_CHARGING
656 void backlight_set_timeout_plugged(int value)
658 backlight_timeout_plugged = HZ * value;
659 backlight_update_state();
661 #endif /* CONFIG_CHARGING */
663 #ifdef HAS_BUTTON_HOLD
664 /* Hold button change event handler. */
665 void backlight_hold_changed(bool hold_button)
667 if (!hold_button || (backlight_on_button_hold > 0))
668 /* if unlocked or override in effect */
669 backlight_on();
672 void backlight_set_on_button_hold(int index)
674 if ((unsigned)index >= 3)
675 /* if given a weird value, use default */
676 index = 0;
678 backlight_on_button_hold = index;
679 backlight_update_state();
681 #endif /* HAS_BUTTON_HOLD */
683 #ifdef HAVE_LCD_SLEEP
684 void lcd_set_sleep_after_backlight_off(int index)
686 if ((unsigned)index >= sizeof(lcd_sleep_timeout_value))
687 /* if given a weird value, use default */
688 index = 3;
690 _lcd_sleep_timeout = HZ * lcd_sleep_timeout_value[index];
692 if (backlight_timer > 0 || backlight_get_current_timeout() == 0)
693 /* Timer will be set when bl turns off or bl set to on. */
694 return;
696 /* Backlight is Off */
697 if (_lcd_sleep_timeout < 0)
698 _lcd_sleep_timer = 1; /* Always - sleep next tick */
699 else
700 _lcd_sleep_timer = _lcd_sleep_timeout; /* Never, other */
702 #endif /* HAVE_LCD_SLEEP */
704 #ifdef HAVE_REMOTE_LCD
705 void remote_backlight_on(void)
707 queue_post(&backlight_queue, REMOTE_BACKLIGHT_ON, 0);
710 void remote_backlight_off(void)
712 queue_post(&backlight_queue, REMOTE_BACKLIGHT_OFF, 0);
715 void remote_backlight_set_timeout(int value)
717 remote_backlight_timeout_normal = HZ * value;
718 remote_backlight_update_state();
721 #if CONFIG_CHARGING
722 void remote_backlight_set_timeout_plugged(int value)
724 remote_backlight_timeout_plugged = HZ * value;
725 remote_backlight_update_state();
727 #endif /* CONFIG_CHARGING */
729 #ifdef HAS_REMOTE_BUTTON_HOLD
730 /* Remote hold button change event handler. */
731 void remote_backlight_hold_changed(bool rc_hold_button)
733 if (!rc_hold_button || (remote_backlight_on_button_hold > 0))
734 /* if unlocked or override */
735 remote_backlight_on();
738 void remote_backlight_set_on_button_hold(int index)
740 if ((unsigned)index >= 3)
741 /* if given a weird value, use default */
742 index = 0;
744 remote_backlight_on_button_hold = index;
745 remote_backlight_update_state();
747 #endif /* HAS_REMOTE_BUTTON_HOLD */
749 /* return value in ticks; 0 means always on, <0 means always off */
750 int remote_backlight_get_current_timeout(void)
752 return remote_backlight_timeout;
755 /* returns true when the backlight is on OR when it's set to always off */
756 bool is_remote_backlight_on(void)
758 if (remote_backlight_timer != 0 || remote_backlight_timeout <= 0)
759 return true;
760 else
761 return false;
764 #endif /* HAVE_REMOTE_LCD */
766 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
767 void backlight_set_brightness(int val)
769 if (val < MIN_BRIGHTNESS_SETTING)
770 val = MIN_BRIGHTNESS_SETTING;
771 else if (val > MAX_BRIGHTNESS_SETTING)
772 val = MAX_BRIGHTNESS_SETTING;
774 _backlight_set_brightness(val);
776 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
778 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
779 void buttonlight_set_brightness(int val)
781 if (val < MIN_BRIGHTNESS_SETTING)
782 val = MIN_BRIGHTNESS_SETTING;
783 else if (val > MAX_BRIGHTNESS_SETTING)
784 val = MAX_BRIGHTNESS_SETTING;
786 _buttonlight_set_brightness(val);
788 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
790 #else /* !defined(HAVE_BACKLIGHT) || defined(BOOTLOADER)
791 -- no backlight, empty dummy functions */
793 #if defined(BOOTLOADER) && defined(HAVE_BACKLIGHT)
794 void backlight_init(void)
796 (void)_backlight_init();
797 _backlight_on();
799 #endif
801 void backlight_on(void) {}
802 void backlight_off(void) {}
803 void buttonlight_on(void) {}
804 void backlight_set_timeout(int value) {(void)value;}
805 bool is_backlight_on(void) {return true;}
806 #ifdef HAVE_REMOTE_LCD
807 void remote_backlight_on(void) {}
808 void remote_backlight_off(void) {}
809 void remote_backlight_set_timeout(int value) {(void)value;}
810 bool is_remote_backlight_on(void) {return true;}
811 #endif /* HAVE_REMOTE_LCD */
812 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
813 void backlight_set_brightness(int val) { (void)val; }
814 #endif
815 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
816 void buttonlight_set_brightness(int val) { (void)val; }
817 #endif
818 #endif /* defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER) */