1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
32 #include "backlight.h"
34 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
35 #include "lcd.h" /* for lcd_enable() and lcd_sleep() */
37 #ifdef HAVE_REMOTE_LCD
38 #include "lcd-remote.h"
41 #include "backlight-target.h"
45 /* TODO: find a better way to do it but we need a kernel thread somewhere to
47 extern void screen_dump(void);
49 static inline void __backlight_on(void)
54 static inline void __backlight_off(void)
59 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
60 static inline void __backlight_set_brightness(int val
)
66 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
67 static inline void __buttonlight_set_brightness(int val
)
73 #endif /* SIMULATOR */
75 #if defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER)
77 const signed char backlight_timeout_value
[19] =
79 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
82 #define BACKLIGHT_ON 1
83 #define BACKLIGHT_OFF 2
84 #define REMOTE_BACKLIGHT_ON 3
85 #define REMOTE_BACKLIGHT_OFF 4
86 #define BACKLIGHT_UNBOOST_CPU 5
90 #ifdef HAVE_BUTTON_LIGHT
91 #define BUTTON_LIGHT_ON 7
92 #define BUTTON_LIGHT_OFF 8
95 static void backlight_thread(void);
96 static long backlight_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
97 static const char backlight_thread_name
[] = "backlight";
98 static struct event_queue backlight_queue
;
100 static int backlight_timer
;
101 static int backlight_timeout
;
102 static int backlight_timeout_normal
= 5*HZ
;
104 static int backlight_timeout_plugged
= 5*HZ
;
106 #ifdef HAS_BUTTON_HOLD
107 static int backlight_on_button_hold
= 0;
110 #ifdef HAVE_BUTTON_LIGHT
111 static int buttonlight_timer
;
112 static int buttonlight_timeout
= 5*HZ
;
114 /* internal interface */
115 static void _buttonlight_on(void)
118 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
119 __buttonlight_dim(false);
126 void _buttonlight_off(void)
129 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
130 if(buttonlight_timeout
>0)
131 __buttonlight_dim(true);
138 /* Update state of buttonlight according to timeout setting */
139 static void buttonlight_update_state(void)
141 buttonlight_timer
= buttonlight_timeout
;
143 /* Buttonlight == OFF in the setting? */
144 if (buttonlight_timer
< 0)
146 buttonlight_timer
= 0; /* Disable the timeout */
153 /* external interface */
154 void buttonlight_on(void)
156 queue_remove_from_head(&backlight_queue
, BUTTON_LIGHT_ON
);
157 queue_post(&backlight_queue
, BUTTON_LIGHT_ON
, 0);
160 void buttonlight_off(void)
162 queue_post(&backlight_queue
, BUTTON_LIGHT_OFF
, 0);
165 void buttonlight_set_timeout(int index
)
167 if((unsigned)index
>= sizeof(backlight_timeout_value
))
168 /* if given a weird value, use default */
170 buttonlight_timeout
= HZ
* backlight_timeout_value
[index
];
171 buttonlight_update_state();
176 #ifdef HAVE_REMOTE_LCD
177 static int remote_backlight_timer
;
178 static int remote_backlight_timeout
;
179 static int remote_backlight_timeout_normal
= 5*HZ
;
181 static int remote_backlight_timeout_plugged
= 5*HZ
;
183 #ifdef HAS_REMOTE_BUTTON_HOLD
184 static int remote_backlight_on_button_hold
= 0;
188 #ifdef HAVE_LCD_SLEEP
189 const signed char lcd_sleep_timeout_value
[10] =
191 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
193 static int lcd_sleep_timer
;
194 static int lcd_sleep_timeout
= 10*HZ
;
197 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
198 /* backlight fading */
199 #define BL_PWM_INTERVAL 5000 /* Cycle interval in s */
200 #define BL_PWM_COUNT 100
201 static const char backlight_fade_value
[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
202 static int fade_in_count
= 1;
203 static int fade_out_count
= 4;
205 static bool bl_timer_active
= false;
206 static int bl_dim_current
= 0;
207 static int bl_dim_target
= 0;
208 static int bl_pwm_counter
= 0;
209 static volatile int bl_cycle_counter
= 0;
210 static enum {DIM_STATE_START
, DIM_STATE_MAIN
} bl_dim_state
= DIM_STATE_START
;
212 static void backlight_isr(void)
217 timer_period
= TIMER_FREQ
/ 1000 * BL_PWM_INTERVAL
/ 1000;
218 switch (bl_dim_state
)
221 case DIM_STATE_START
:
225 if (bl_dim_current
> 0 && bl_dim_current
< BL_PWM_COUNT
)
228 bl_pwm_counter
= bl_dim_current
;
229 timer_period
= timer_period
* bl_pwm_counter
/ BL_PWM_COUNT
;
230 bl_dim_state
= DIM_STATE_MAIN
;
238 if (bl_dim_current
== bl_dim_target
)
244 /* Dim main screen */
247 bl_dim_state
= DIM_STATE_START
;
248 timer_period
= timer_period
* (BL_PWM_COUNT
- bl_pwm_counter
) / BL_PWM_COUNT
;
252 if ((bl_dim_target
> bl_dim_current
) && (bl_cycle_counter
>= fade_in_count
))
255 bl_cycle_counter
= 0;
258 if ((bl_dim_target
< bl_dim_current
) && (bl_cycle_counter
>= fade_out_count
))
261 bl_cycle_counter
= 0;
267 queue_post(&backlight_queue
, BACKLIGHT_UNBOOST_CPU
, 0);
270 bl_timer_active
= false;
273 timer_set_period(timer_period
);
276 static void backlight_switch(void)
278 if (bl_dim_target
> (BL_PWM_COUNT
/2))
281 bl_dim_current
= BL_PWM_COUNT
;
290 static void backlight_release_timer(void)
296 bl_timer_active
= false;
300 static void backlight_dim(int value
)
302 /* protect from extraneous calls with the same target value */
303 if (value
== bl_dim_target
)
306 bl_dim_target
= value
;
311 if (timer_register(0, backlight_release_timer
, 2, 0, backlight_isr
))
314 /* Prevent cpu frequency changes while dimming. */
317 bl_timer_active
= true;
323 void backlight_set_fade_in(int index
)
325 fade_in_count
= backlight_fade_value
[index
];
328 void backlight_set_fade_out(int index
)
330 fade_out_count
= backlight_fade_value
[index
];
332 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
334 static void _backlight_on(void)
336 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
337 if (fade_in_count
> 0)
338 backlight_dim(BL_PWM_COUNT
);
341 bl_dim_target
= bl_dim_current
= BL_PWM_COUNT
;
344 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
345 /* call the enable from here - it takes longer than the disable */
347 __backlight_dim(false);
351 #ifdef HAVE_LCD_SLEEP
352 lcd_sleep_timer
= 0; /* LCD should be awake already */
356 static void _backlight_off(void)
358 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
359 if (fade_out_count
> 0)
363 bl_dim_target
= bl_dim_current
= 0;
366 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
367 __backlight_dim(true);
372 #ifdef HAVE_LCD_SLEEP
373 /* Start LCD sleep countdown */
374 if (lcd_sleep_timeout
< 0)
376 lcd_sleep_timer
= 0; /* Setting == Always */
380 lcd_sleep_timer
= lcd_sleep_timeout
;
384 #ifdef HAVE_REMOTE_LCD
386 static void __remote_backlight_on(void)
388 sim_remote_backlight(100);
391 static void __remote_backlight_off(void)
393 sim_remote_backlight(0);
395 #endif /* SIMULATOR */
396 #endif /* HAVE_REMOTE_LCD */
398 /* Update state of backlight according to timeout setting */
399 static void backlight_update_state(void)
401 #ifdef HAS_BUTTON_HOLD
402 if (button_hold() && (backlight_on_button_hold
!= 0))
403 backlight_timeout
= (backlight_on_button_hold
== 2) ? 0 : -1;
404 /* always on or always off */
408 if (charger_inserted()
409 #ifdef HAVE_USB_POWER
413 backlight_timeout
= backlight_timeout_plugged
;
416 backlight_timeout
= backlight_timeout_normal
;
418 /* Backlight == OFF in the setting? */
419 if (backlight_timeout
< 0)
421 backlight_timer
= 0; /* Disable the timeout */
426 backlight_timer
= backlight_timeout
;
431 #ifdef HAVE_REMOTE_LCD
432 /* Update state of remote backlight according to timeout setting */
433 static void remote_backlight_update_state(void)
435 #ifdef HAS_REMOTE_BUTTON_HOLD
436 if (remote_button_hold() && (remote_backlight_on_button_hold
!= 0))
437 remote_backlight_timeout
= (remote_backlight_on_button_hold
== 2)
438 ? 0 : -1; /* always on or always off */
442 if (charger_inserted()
443 #ifdef HAVE_USB_POWER
447 remote_backlight_timeout
= remote_backlight_timeout_plugged
;
450 remote_backlight_timeout
= remote_backlight_timeout_normal
;
452 /* Backlight == OFF in the setting? */
453 if (remote_backlight_timeout
< 0)
455 remote_backlight_timer
= 0; /* Disable the timeout */
456 __remote_backlight_off();
460 remote_backlight_timer
= remote_backlight_timeout
;
461 __remote_backlight_on();
464 #endif /* HAVE_REMOTE_LCD */
466 void backlight_thread(void)
468 struct queue_event ev
;
473 queue_wait(&backlight_queue
, &ev
);
475 { /* These events must always be processed */
476 #if defined(HAVE_BACKLIGHT_PWM_FADING) && defined(CPU_COLDFIRE) \
477 && !defined(SIMULATOR)
478 case BACKLIGHT_UNBOOST_CPU
:
483 #if defined(HAVE_REMOTE_LCD) && !defined(SIMULATOR)
484 /* Here for now or else the aggressive init messes up scrolling */
485 case SYS_REMOTE_PLUGGED
:
490 case SYS_REMOTE_UNPLUGGED
:
493 #endif /* defined(HAVE_REMOTE_LCD) && !defined(SIMULATOR) */
495 /* This one here too for lack of a better place */
500 case SYS_USB_CONNECTED
:
501 /* Tell the USB thread that we are safe */
502 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
503 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
506 case SYS_USB_DISCONNECTED
:
507 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
514 { /* These events are only processed if backlight isn't locked */
515 #ifdef HAVE_REMOTE_LCD
516 case REMOTE_BACKLIGHT_ON
:
517 remote_backlight_update_state();
520 case REMOTE_BACKLIGHT_OFF
:
521 remote_backlight_timer
= 0; /* Disable the timeout */
522 __remote_backlight_off();
524 #endif /* HAVE_REMOTE_LCD */
527 backlight_update_state();
531 backlight_timer
= 0; /* Disable the timeout */
535 #ifdef HAVE_LCD_SLEEP
540 #ifdef HAVE_BUTTON_LIGHT
541 case BUTTON_LIGHT_ON
:
542 buttonlight_update_state();
545 case BUTTON_LIGHT_OFF
:
546 buttonlight_timer
= 0;
551 case SYS_POWEROFF
: /* Lock backlight on poweroff so it doesn't */
552 locked
= true; /* go off before power is actually cut. */
555 case SYS_CHARGER_CONNECTED
:
556 case SYS_CHARGER_DISCONNECTED
:
558 backlight_update_state();
559 #ifdef HAVE_REMOTE_LCD
560 remote_backlight_update_state();
567 static void backlight_tick(void)
572 if(backlight_timer
== 0)
577 #ifdef HAVE_LCD_SLEEP
578 else if(lcd_sleep_timer
)
581 if(lcd_sleep_timer
== 0)
583 /* Queue on bl thread or freeze! */
584 queue_post(&backlight_queue
, LCD_SLEEP
, 0);
587 #endif /* HAVE_LCD_SLEEP */
589 #ifdef HAVE_REMOTE_LCD
590 if(remote_backlight_timer
)
592 remote_backlight_timer
--;
593 if(remote_backlight_timer
== 0)
595 remote_backlight_off();
598 #endif /* HAVE_REMOVE_LCD */
599 #ifdef HAVE_BUTTON_LIGHT
600 if (buttonlight_timer
)
603 if (buttonlight_timer
== 0)
608 #endif /* HAVE_BUTTON_LIGHT */
611 void backlight_init(void)
613 queue_init(&backlight_queue
, true);
616 if (__backlight_init())
618 # ifdef HAVE_BACKLIGHT_PWM_FADING
619 /* If backlight is already on, don't fade in. */
620 bl_dim_current
= BL_PWM_COUNT
;
621 bl_dim_target
= BL_PWM_COUNT
;
625 /* Leave all lights as set by the bootloader here. The settings load will
626 * call the appropriate backlight_set_*() functions, only changing light
627 * status if necessary. */
629 create_thread(backlight_thread
, backlight_stack
,
630 sizeof(backlight_stack
), 0, backlight_thread_name
631 IF_PRIO(, PRIORITY_SYSTEM
)
633 tick_add_task(backlight_tick
);
636 void backlight_on(void)
638 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
639 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
642 void backlight_off(void)
644 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
647 /* returns true when the backlight is on OR when it's set to always off */
648 bool is_backlight_on(void)
650 if (backlight_timer
|| backlight_timeout
<= 0)
656 /* return value in ticks; 0 means always on, <0 means always off */
657 int backlight_get_current_timeout(void)
659 return backlight_timeout
;
662 void backlight_set_timeout(int index
)
664 if((unsigned)index
>= sizeof(backlight_timeout_value
))
665 /* if given a weird value, use default */
667 backlight_timeout_normal
= HZ
* backlight_timeout_value
[index
];
668 backlight_update_state();
672 void backlight_set_timeout_plugged(int index
)
674 if((unsigned)index
>= sizeof(backlight_timeout_value
))
675 /* if given a weird value, use default */
677 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
678 backlight_update_state();
680 #endif /* CONFIG_CHARGING */
682 #ifdef HAS_BUTTON_HOLD
683 /* Hold button change event handler. */
684 void backlight_hold_changed(bool hold_button
)
686 if (!hold_button
|| (backlight_on_button_hold
> 0))
687 /* if unlocked or override in effect */
691 void backlight_set_on_button_hold(int index
)
693 if ((unsigned)index
>= 3)
694 /* if given a weird value, use default */
697 backlight_on_button_hold
= index
;
698 backlight_update_state();
700 #endif /* HAS_BUTTON_HOLD */
702 #ifdef HAVE_LCD_SLEEP
703 void lcd_set_sleep_after_backlight_off(int index
)
705 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
706 /* if given a weird value, use default */
709 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
711 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
712 /* Timer will be set when bl turns off or bl set to on. */
715 /* Backlight is Off */
716 if (lcd_sleep_timeout
< 0)
717 lcd_sleep_timer
= 1; /* Always - sleep next tick */
719 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
721 #endif /* HAVE_LCD_SLEEP */
723 #ifdef HAVE_REMOTE_LCD
724 void remote_backlight_on(void)
726 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
729 void remote_backlight_off(void)
731 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
734 void remote_backlight_set_timeout(int index
)
736 if((unsigned)index
>= sizeof(backlight_timeout_value
))
737 /* if given a weird value, use default */
739 remote_backlight_timeout_normal
= HZ
* backlight_timeout_value
[index
];
740 remote_backlight_update_state();
744 void remote_backlight_set_timeout_plugged(int index
)
746 if((unsigned)index
>= sizeof(backlight_timeout_value
))
747 /* if given a weird value, use default */
749 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
750 remote_backlight_update_state();
752 #endif /* CONFIG_CHARGING */
754 #ifdef HAS_REMOTE_BUTTON_HOLD
755 /* Remote hold button change event handler. */
756 void remote_backlight_hold_changed(bool rc_hold_button
)
758 if (!rc_hold_button
|| (remote_backlight_on_button_hold
> 0))
759 /* if unlocked or override */
760 remote_backlight_on();
763 void remote_backlight_set_on_button_hold(int index
)
765 if ((unsigned)index
>= 3)
766 /* if given a weird value, use default */
769 remote_backlight_on_button_hold
= index
;
770 remote_backlight_update_state();
772 #endif /* HAS_REMOTE_BUTTON_HOLD */
774 /* return value in ticks; 0 means always on, <0 means always off */
775 int remote_backlight_get_current_timeout(void)
777 return remote_backlight_timeout
;
780 /* returns true when the backlight is on OR when it's set to always off */
781 bool is_remote_backlight_on(void)
783 if (remote_backlight_timer
!= 0 || remote_backlight_timeout
<= 0)
789 #endif /* HAVE_REMOTE_LCD */
791 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
792 void backlight_set_brightness(int val
)
794 if (val
< MIN_BRIGHTNESS_SETTING
)
795 val
= MIN_BRIGHTNESS_SETTING
;
796 else if (val
> MAX_BRIGHTNESS_SETTING
)
797 val
= MAX_BRIGHTNESS_SETTING
;
799 __backlight_set_brightness(val
);
801 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
803 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
804 void buttonlight_set_brightness(int val
)
806 if (val
< MIN_BRIGHTNESS_SETTING
)
807 val
= MIN_BRIGHTNESS_SETTING
;
808 else if (val
> MAX_BRIGHTNESS_SETTING
)
809 val
= MAX_BRIGHTNESS_SETTING
;
811 __buttonlight_set_brightness(val
);
813 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
815 #else /* !defined(HAVE_BACKLIGHT) || defined(BOOTLOADER)
816 -- no backlight, empty dummy functions */
818 #if defined(BOOTLOADER) && defined(HAVE_BACKLIGHT)
819 void backlight_init(void)
821 (void)__backlight_init();
826 void backlight_on(void) {}
827 void backlight_off(void) {}
828 void buttonlight_on(void) {}
829 void backlight_set_timeout(int index
) {(void)index
;}
830 bool is_backlight_on(void) {return true;}
831 #ifdef HAVE_REMOTE_LCD
832 void remote_backlight_on(void) {}
833 void remote_backlight_off(void) {}
834 void remote_backlight_set_timeout(int index
) {(void)index
;}
835 bool is_remote_backlight_on(void) {return true;}
836 #endif /* HAVE_REMOTE_LCD */
837 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
838 void backlight_set_brightness(int val
) { (void)val
; }
840 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
841 void buttonlight_set_brightness(int val
) { (void)val
; }
843 #endif /* defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER) */