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"
35 #ifdef HAVE_REMOTE_LCD
36 #include "lcd-remote.h"
39 #include "backlight-target.h"
43 /* TODO: find a better way to do it but we need a kernel thread somewhere to
45 extern void screen_dump(void);
47 static inline void _backlight_on(void)
52 static inline void _backlight_off(void)
57 static inline void _backlight_set_brightness(int val
)
62 static inline void _buttonlight_on(void)
66 static inline void _buttonlight_off(void)
70 static inline void _buttonlight_set_brightness(int 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)
93 #ifdef HAVE_REMOTE_LCD
97 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
98 BACKLIGHT_FADE_FINISH
,
100 #ifdef HAVE_LCD_SLEEP
103 #ifdef HAVE_BUTTON_LIGHT
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 SHAREDBSS_ATTR
;
115 static int backlight_timeout SHAREDBSS_ATTR
;
116 static int backlight_timeout_normal
= 5*HZ
;
118 static int backlight_timeout_plugged
= 5*HZ
;
120 #ifdef HAS_BUTTON_HOLD
121 static int backlight_on_button_hold
= 0;
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 */
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();
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
;
168 static int remote_backlight_timeout_plugged
= 5*HZ
;
170 #ifdef HAS_REMOTE_BUTTON_HOLD
171 static int remote_backlight_on_button_hold
= 0;
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
;
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);
205 switch (bl_dim_state
)
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
)
214 timer_period
= (timer_period
* bl_dim_current
) >> BL_PWM_BITS
;
215 bl_dim_state
= DIM_STATE_MAIN
;
222 _backlight_off_isr();
223 if (bl_dim_current
== bl_dim_target
)
226 if (bl_dim_current
< bl_dim_target
)
228 bl_dim_fraction
= MIN(bl_dim_fraction
+ bl_fade_in_step
,
231 else if (bl_dim_current
> bl_dim_target
)
233 bl_dim_fraction
= MAX(bl_dim_fraction
- bl_fade_out_step
, 0);
237 /* Dim main screen */
239 _backlight_off_isr();
240 timer_period
= (timer_period
* (BL_PWM_COUNT
- bl_dim_current
))
242 bl_dim_state
= DIM_STATE_START
;
247 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
248 queue_post(&backlight_queue
, BACKLIGHT_FADE_FINISH
, 0);
251 bl_timer_active
= false;
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);
266 _backlight_off_normal();
271 static void backlight_release_timer(void)
273 #ifdef _BACKLIGHT_FADE_BOOST
277 bl_timer_active
= false;
281 static void backlight_dim(int value
)
283 /* protect from extraneous calls with the same target value */
284 if (value
== bl_dim_target
)
287 bl_dim_target
= value
;
292 if (timer_register(0, backlight_release_timer
, 2, 0, backlight_isr
295 #ifdef _BACKLIGHT_FADE_BOOST
296 /* Prevent cpu frequency changes while dimming. */
299 bl_timer_active
= true;
305 static void _backlight_on(void)
307 if (bl_fade_in_step
> 0)
309 #ifdef _BACKLIGHT_FADE_ENABLE
310 _backlight_hw_enable(true);
312 backlight_dim(BL_PWM_COUNT
);
316 bl_dim_target
= BL_PWM_COUNT
;
317 bl_dim_fraction
= (BL_PWM_COUNT
<<16);
318 _backlight_on_normal();
320 #ifdef HAVE_LCD_SLEEP
321 _lcd_sleep_timer
= 0; /* LCD should be awake already */
325 static void _backlight_off(void)
327 if (bl_fade_out_step
> 0)
333 bl_dim_target
= bl_dim_fraction
= 0;
334 _backlight_off_normal();
336 #ifdef HAVE_LCD_SLEEP
337 /* Start LCD sleep countdown */
338 if (_lcd_sleep_timeout
< 0)
340 _lcd_sleep_timer
= 0; /* Setting == Always */
344 _lcd_sleep_timer
= _lcd_sleep_timeout
;
348 void backlight_set_fade_in(int value
)
351 bl_fade_in_step
= ((BL_PWM_INTERVAL
*BL_PWM_COUNT
)<<16) / value
;
356 void backlight_set_fade_out(int value
)
359 bl_fade_out_step
= ((BL_PWM_INTERVAL
*BL_PWM_COUNT
)<<16) / value
;
361 bl_fade_out_step
= 0;
363 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
365 /* Update state of backlight according to timeout setting */
366 static void backlight_update_state(void)
368 #ifdef HAS_BUTTON_HOLD
369 if ((backlight_on_button_hold
!= 0)
370 #ifdef HAVE_REMOTE_LCD_AS_MAIN
371 && remote_button_hold()
376 backlight_timeout
= (backlight_on_button_hold
== 2) ? 0 : -1;
377 /* always on or always off */
381 if (charger_inserted()
382 #ifdef HAVE_USB_POWER
386 backlight_timeout
= backlight_timeout_plugged
;
389 backlight_timeout
= backlight_timeout_normal
;
391 /* Backlight == OFF in the setting? */
392 if (backlight_timeout
< 0)
394 backlight_timer
= 0; /* Disable the timeout */
399 backlight_timer
= backlight_timeout
;
404 #ifdef HAVE_REMOTE_LCD
405 /* Update state of remote backlight according to timeout setting */
406 static void remote_backlight_update_state(void)
408 #ifdef HAS_REMOTE_BUTTON_HOLD
409 if (remote_button_hold() && (remote_backlight_on_button_hold
!= 0))
410 remote_backlight_timeout
= (remote_backlight_on_button_hold
== 2)
411 ? 0 : -1; /* always on or always off */
415 if (charger_inserted()
416 #ifdef HAVE_USB_POWER
420 remote_backlight_timeout
= remote_backlight_timeout_plugged
;
423 remote_backlight_timeout
= remote_backlight_timeout_normal
;
425 /* Backlight == OFF in the setting? */
426 if (remote_backlight_timeout
< 0)
428 remote_backlight_timer
= 0; /* Disable the timeout */
429 _remote_backlight_off();
433 remote_backlight_timer
= remote_backlight_timeout
;
434 _remote_backlight_on();
437 #endif /* HAVE_REMOTE_LCD */
439 void backlight_thread(void)
441 struct queue_event ev
;
446 queue_wait(&backlight_queue
, &ev
);
448 { /* These events must always be processed */
449 #ifdef _BACKLIGHT_FADE_BOOST
450 case BACKLIGHT_FADE_FINISH
:
454 #ifdef _BACKLIGHT_FADE_ENABLE
455 case BACKLIGHT_FADE_FINISH
:
456 _backlight_hw_enable((bl_dim_current
|bl_dim_target
) != 0);
461 /* Here for now or else the aggressive init messes up scrolling */
462 #ifdef HAVE_REMOTE_LCD
463 case SYS_REMOTE_PLUGGED
:
468 case SYS_REMOTE_UNPLUGGED
:
471 #elif defined HAVE_REMOTE_LCD_AS_MAIN
472 case SYS_REMOTE_PLUGGED
:
477 case SYS_REMOTE_UNPLUGGED
:
480 #endif /* HAVE_REMOTE_LCD/ HAVE_REMOTE_LCD_AS_MAIN */
481 #endif /* !SIMULATOR */
483 /* This one here too for lack of a better place */
488 case SYS_USB_CONNECTED
:
489 /* Tell the USB thread that we are safe */
490 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
491 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
494 case SYS_USB_DISCONNECTED
:
495 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
502 { /* These events are only processed if backlight isn't locked */
503 #ifdef HAVE_REMOTE_LCD
504 case REMOTE_BACKLIGHT_ON
:
505 remote_backlight_update_state();
508 case REMOTE_BACKLIGHT_OFF
:
509 remote_backlight_timer
= 0; /* Disable the timeout */
510 _remote_backlight_off();
512 #endif /* HAVE_REMOTE_LCD */
515 backlight_update_state();
519 backlight_timer
= 0; /* Disable the timeout */
523 #ifdef HAVE_LCD_SLEEP
528 #ifdef HAVE_BUTTON_LIGHT
529 case BUTTON_LIGHT_ON
:
530 buttonlight_update_state();
533 case BUTTON_LIGHT_OFF
:
534 buttonlight_timer
= 0;
539 case SYS_POWEROFF
: /* Lock backlight on poweroff so it doesn't */
540 locked
= true; /* go off before power is actually cut. */
543 case SYS_CHARGER_CONNECTED
:
544 case SYS_CHARGER_DISCONNECTED
:
546 backlight_update_state();
547 #ifdef HAVE_REMOTE_LCD
548 remote_backlight_update_state();
555 static void backlight_tick(void)
560 if(backlight_timer
== 0)
565 #ifdef HAVE_LCD_SLEEP
566 else if(_lcd_sleep_timer
)
569 if(_lcd_sleep_timer
== 0)
571 /* Queue on bl thread or freeze! */
572 queue_post(&backlight_queue
, LCD_SLEEP
, 0);
575 #endif /* HAVE_LCD_SLEEP */
577 #ifdef HAVE_REMOTE_LCD
578 if(remote_backlight_timer
)
580 remote_backlight_timer
--;
581 if(remote_backlight_timer
== 0)
583 remote_backlight_off();
586 #endif /* HAVE_REMOVE_LCD */
587 #ifdef HAVE_BUTTON_LIGHT
588 if (buttonlight_timer
)
591 if (buttonlight_timer
== 0)
596 #endif /* HAVE_BUTTON_LIGHT */
599 void backlight_init(void)
601 queue_init(&backlight_queue
, true);
604 if (_backlight_init())
606 # ifdef HAVE_BACKLIGHT_PWM_FADING
607 /* If backlight is already on, don't fade in. */
608 bl_dim_target
= BL_PWM_COUNT
;
609 bl_dim_fraction
= (BL_PWM_COUNT
<<16);
613 /* Leave all lights as set by the bootloader here. The settings load will
614 * call the appropriate backlight_set_*() functions, only changing light
615 * status if necessary. */
617 create_thread(backlight_thread
, backlight_stack
,
618 sizeof(backlight_stack
), 0, backlight_thread_name
619 IF_PRIO(, PRIORITY_USER_INTERFACE
)
621 tick_add_task(backlight_tick
);
624 void backlight_on(void)
626 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
627 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
630 void backlight_off(void)
632 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
635 /* returns true when the backlight is on,
636 * and optionally when it's set to always off. */
637 bool is_backlight_on(bool ignore_always_off
)
639 return (backlight_timer
> 0) /* countdown */
640 || (backlight_timeout
== 0) /* always on */
641 || ((backlight_timeout
< 0) && !ignore_always_off
);
644 /* return value in ticks; 0 means always on, <0 means always off */
645 int backlight_get_current_timeout(void)
647 return backlight_timeout
;
650 void backlight_set_timeout(int value
)
652 backlight_timeout_normal
= HZ
* value
;
653 backlight_update_state();
657 void backlight_set_timeout_plugged(int value
)
659 backlight_timeout_plugged
= HZ
* value
;
660 backlight_update_state();
662 #endif /* CONFIG_CHARGING */
664 #ifdef HAS_BUTTON_HOLD
665 /* Hold button change event handler. */
666 void backlight_hold_changed(bool hold_button
)
668 if (!hold_button
|| (backlight_on_button_hold
> 0))
669 /* if unlocked or override in effect */
673 void backlight_set_on_button_hold(int index
)
675 if ((unsigned)index
>= 3)
676 /* if given a weird value, use default */
679 backlight_on_button_hold
= index
;
680 backlight_update_state();
682 #endif /* HAS_BUTTON_HOLD */
684 #ifdef HAVE_LCD_SLEEP
685 void lcd_set_sleep_after_backlight_off(int index
)
687 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
688 /* if given a weird value, use default */
691 _lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
693 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
694 /* Timer will be set when bl turns off or bl set to on. */
697 /* Backlight is Off */
698 if (_lcd_sleep_timeout
< 0)
699 _lcd_sleep_timer
= 1; /* Always - sleep next tick */
701 _lcd_sleep_timer
= _lcd_sleep_timeout
; /* Never, other */
703 #endif /* HAVE_LCD_SLEEP */
705 #ifdef HAVE_REMOTE_LCD
706 void remote_backlight_on(void)
708 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
711 void remote_backlight_off(void)
713 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
716 void remote_backlight_set_timeout(int value
)
718 remote_backlight_timeout_normal
= HZ
* value
;
719 remote_backlight_update_state();
723 void remote_backlight_set_timeout_plugged(int value
)
725 remote_backlight_timeout_plugged
= HZ
* value
;
726 remote_backlight_update_state();
728 #endif /* CONFIG_CHARGING */
730 #ifdef HAS_REMOTE_BUTTON_HOLD
731 /* Remote hold button change event handler. */
732 void remote_backlight_hold_changed(bool rc_hold_button
)
734 if (!rc_hold_button
|| (remote_backlight_on_button_hold
> 0))
735 /* if unlocked or override */
736 remote_backlight_on();
739 void remote_backlight_set_on_button_hold(int index
)
741 if ((unsigned)index
>= 3)
742 /* if given a weird value, use default */
745 remote_backlight_on_button_hold
= index
;
746 remote_backlight_update_state();
748 #endif /* HAS_REMOTE_BUTTON_HOLD */
750 /* return value in ticks; 0 means always on, <0 means always off */
751 int remote_backlight_get_current_timeout(void)
753 return remote_backlight_timeout
;
756 /* returns true when the backlight is on, and
757 * optionally when it's set to always off */
758 bool is_remote_backlight_on(bool ignore_always_off
)
760 return (remote_backlight_timer
> 0) /* countdown */
761 || (remote_backlight_timeout
== 0) /* always on */
762 || ((remote_backlight_timeout
< 0) && !ignore_always_off
);
765 #endif /* HAVE_REMOTE_LCD */
767 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
768 void backlight_set_brightness(int val
)
770 if (val
< MIN_BRIGHTNESS_SETTING
)
771 val
= MIN_BRIGHTNESS_SETTING
;
772 else if (val
> MAX_BRIGHTNESS_SETTING
)
773 val
= MAX_BRIGHTNESS_SETTING
;
775 _backlight_set_brightness(val
);
777 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
779 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
780 void buttonlight_set_brightness(int val
)
782 if (val
< MIN_BRIGHTNESS_SETTING
)
783 val
= MIN_BRIGHTNESS_SETTING
;
784 else if (val
> MAX_BRIGHTNESS_SETTING
)
785 val
= MAX_BRIGHTNESS_SETTING
;
787 _buttonlight_set_brightness(val
);
789 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
791 #else /* !defined(HAVE_BACKLIGHT) || defined(BOOTLOADER)
792 -- no backlight, empty dummy functions */
794 #if defined(BOOTLOADER) && defined(HAVE_BACKLIGHT)
795 void backlight_init(void)
797 (void)_backlight_init();
802 void backlight_on(void) {}
803 void backlight_off(void) {}
804 void buttonlight_on(void) {}
805 void backlight_set_timeout(int value
) {(void)value
;}
807 bool is_backlight_on(bool ignore_always_off
)
809 (void)ignore_always_off
;
812 #ifdef HAVE_REMOTE_LCD
813 void remote_backlight_on(void) {}
814 void remote_backlight_off(void) {}
815 void remote_backlight_set_timeout(int value
) {(void)value
;}
817 bool is_remote_backlight_on(bool ignore_always_off
)
819 (void)ignore_always_off
;
822 #endif /* HAVE_REMOTE_LCD */
823 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
824 void backlight_set_brightness(int val
) { (void)val
; }
826 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
827 void buttonlight_set_brightness(int val
) { (void)val
; }
829 #endif /* defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER) */