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 static inline void __backlight_on(void)
50 static inline void __backlight_off(void)
55 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
56 static inline void __backlight_set_brightness(int val
)
62 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
63 static inline void __buttonlight_set_brightness(int val
)
69 #endif /* SIMULATOR */
71 #if defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER)
73 const signed char backlight_timeout_value
[19] =
75 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
78 #define BACKLIGHT_ON 1
79 #define BACKLIGHT_OFF 2
80 #define REMOTE_BACKLIGHT_ON 3
81 #define REMOTE_BACKLIGHT_OFF 4
82 #define BACKLIGHT_UNBOOST_CPU 5
86 #ifdef HAVE_BUTTON_LIGHT
87 #define BUTTON_LIGHT_ON 7
88 #define BUTTON_LIGHT_OFF 8
91 static void backlight_thread(void);
92 static long backlight_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
93 static const char backlight_thread_name
[] = "backlight";
94 static struct event_queue backlight_queue
;
96 static int backlight_timer
;
97 static int backlight_timeout
;
98 static int backlight_timeout_normal
= 5*HZ
;
100 static int backlight_timeout_plugged
= 5*HZ
;
102 #ifdef HAS_BUTTON_HOLD
103 static int backlight_on_button_hold
= 0;
106 #ifdef HAVE_BUTTON_LIGHT
107 static int button_backlight_timer
;
108 static int button_backlight_timeout
= 5*HZ
;
110 /* internal interface */
111 static void _button_backlight_on(void)
114 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
115 __button_backlight_dim(false);
117 __button_backlight_on();
122 void _button_backlight_off(void)
125 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
126 if(button_backlight_timeout
>0)
127 __button_backlight_dim(true);
130 __button_backlight_off();
134 /* Update state of buttonlight according to timeout setting */
135 static void buttonlight_update_state(void)
137 button_backlight_timer
= button_backlight_timeout
;
139 /* Buttonlight == OFF in the setting? */
140 if (button_backlight_timer
< 0)
142 button_backlight_timer
= 0; /* Disable the timeout */
143 _button_backlight_off();
146 _button_backlight_on();
149 /* external interface */
150 void button_backlight_on(void)
152 queue_remove_from_head(&backlight_queue
, BUTTON_LIGHT_ON
);
153 queue_post(&backlight_queue
, BUTTON_LIGHT_ON
, 0);
156 void button_backlight_off(void)
158 queue_post(&backlight_queue
, BUTTON_LIGHT_OFF
, 0);
161 void button_backlight_set_timeout(int index
)
163 if((unsigned)index
>= sizeof(backlight_timeout_value
))
164 /* if given a weird value, use default */
166 button_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
167 buttonlight_update_state();
172 #ifdef HAVE_REMOTE_LCD
173 static int remote_backlight_timer
;
174 static int remote_backlight_timeout
;
175 static int remote_backlight_timeout_normal
= 5*HZ
;
177 static int remote_backlight_timeout_plugged
= 5*HZ
;
179 #ifdef HAS_REMOTE_BUTTON_HOLD
180 static int remote_backlight_on_button_hold
= 0;
184 #ifdef HAVE_LCD_SLEEP
185 const signed char lcd_sleep_timeout_value
[10] =
187 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
189 static int lcd_sleep_timer
;
190 static int lcd_sleep_timeout
= 10*HZ
;
193 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
194 /* backlight fading */
195 #define BL_PWM_INTERVAL 5000 /* Cycle interval in s */
196 #define BL_PWM_COUNT 100
197 static const char backlight_fade_value
[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
198 static int fade_in_count
= 1;
199 static int fade_out_count
= 4;
201 static bool bl_timer_active
= false;
202 static int bl_dim_current
= 0;
203 static int bl_dim_target
= 0;
204 static int bl_pwm_counter
= 0;
205 static volatile int bl_cycle_counter
= 0;
206 static enum {DIM_STATE_START
, DIM_STATE_MAIN
} bl_dim_state
= DIM_STATE_START
;
208 static void backlight_isr(void)
213 timer_period
= TIMER_FREQ
/ 1000 * BL_PWM_INTERVAL
/ 1000;
214 switch (bl_dim_state
)
217 case DIM_STATE_START
:
221 if (bl_dim_current
> 0 && bl_dim_current
< BL_PWM_COUNT
)
224 bl_pwm_counter
= bl_dim_current
;
225 timer_period
= timer_period
* bl_pwm_counter
/ BL_PWM_COUNT
;
226 bl_dim_state
= DIM_STATE_MAIN
;
234 if (bl_dim_current
== bl_dim_target
)
240 /* Dim main screen */
243 bl_dim_state
= DIM_STATE_START
;
244 timer_period
= timer_period
* (BL_PWM_COUNT
- bl_pwm_counter
) / BL_PWM_COUNT
;
248 if ((bl_dim_target
> bl_dim_current
) && (bl_cycle_counter
>= fade_in_count
))
251 bl_cycle_counter
= 0;
254 if ((bl_dim_target
< bl_dim_current
) && (bl_cycle_counter
>= fade_out_count
))
257 bl_cycle_counter
= 0;
263 queue_post(&backlight_queue
, BACKLIGHT_UNBOOST_CPU
, 0);
266 bl_timer_active
= false;
269 timer_set_period(timer_period
);
272 static void backlight_switch(void)
274 if (bl_dim_target
> (BL_PWM_COUNT
/2))
277 bl_dim_current
= BL_PWM_COUNT
;
286 static void backlight_release_timer(void)
292 bl_timer_active
= false;
296 static void backlight_dim(int value
)
298 /* protect from extraneous calls with the same target value */
299 if (value
== bl_dim_target
)
302 bl_dim_target
= value
;
307 if (timer_register(0, backlight_release_timer
, 2, 0, backlight_isr
))
310 /* Prevent cpu frequency changes while dimming. */
313 bl_timer_active
= true;
319 void backlight_set_fade_in(int index
)
321 fade_in_count
= backlight_fade_value
[index
];
324 void backlight_set_fade_out(int index
)
326 fade_out_count
= backlight_fade_value
[index
];
328 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
330 static void _backlight_on(void)
332 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
333 if (fade_in_count
> 0)
334 backlight_dim(BL_PWM_COUNT
);
337 bl_dim_target
= bl_dim_current
= BL_PWM_COUNT
;
340 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
341 /* call the enable from here - it takes longer than the disable */
343 __backlight_dim(false);
347 #ifdef HAVE_LCD_SLEEP
348 lcd_sleep_timer
= 0; /* LCD should be awake already */
352 static void _backlight_off(void)
354 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
355 if (fade_out_count
> 0)
359 bl_dim_target
= bl_dim_current
= 0;
362 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
363 __backlight_dim(true);
368 #ifdef HAVE_LCD_SLEEP
369 /* Start LCD sleep countdown */
370 if (lcd_sleep_timeout
< 0)
372 lcd_sleep_timer
= 0; /* Setting == Always */
376 lcd_sleep_timer
= lcd_sleep_timeout
;
380 #ifdef HAVE_REMOTE_LCD
382 static void __remote_backlight_on(void)
384 sim_remote_backlight(100);
387 static void __remote_backlight_off(void)
389 sim_remote_backlight(0);
391 #endif /* SIMULATOR */
392 #endif /* HAVE_REMOTE_LCD */
394 /* Update state of backlight according to timeout setting */
395 static void backlight_update_state(void)
397 #ifdef HAS_BUTTON_HOLD
398 if (button_hold() && (backlight_on_button_hold
!= 0))
399 backlight_timeout
= (backlight_on_button_hold
== 2) ? 0 : -1;
400 /* always on or always off */
404 if (charger_inserted()
405 #ifdef HAVE_USB_POWER
409 backlight_timeout
= backlight_timeout_plugged
;
412 backlight_timeout
= backlight_timeout_normal
;
414 /* Backlight == OFF in the setting? */
415 if (backlight_timeout
< 0)
417 backlight_timer
= 0; /* Disable the timeout */
422 backlight_timer
= backlight_timeout
;
427 #ifdef HAVE_REMOTE_LCD
428 /* Update state of remote backlight according to timeout setting */
429 static void remote_backlight_update_state(void)
431 #ifdef HAS_REMOTE_BUTTON_HOLD
432 if (remote_button_hold() && (remote_backlight_on_button_hold
!= 0))
433 remote_backlight_timeout
= (remote_backlight_on_button_hold
== 2)
434 ? 0 : -1; /* always on or always off */
438 if (charger_inserted()
439 #ifdef HAVE_USB_POWER
443 remote_backlight_timeout
= remote_backlight_timeout_plugged
;
446 remote_backlight_timeout
= remote_backlight_timeout_normal
;
448 /* Backlight == OFF in the setting? */
449 if (remote_backlight_timeout
< 0)
451 remote_backlight_timer
= 0; /* Disable the timeout */
452 __remote_backlight_off();
456 remote_backlight_timer
= remote_backlight_timeout
;
457 __remote_backlight_on();
460 #endif /* HAVE_REMOTE_LCD */
462 void backlight_thread(void)
469 queue_wait(&backlight_queue
, &ev
);
471 { /* These events must always be processed */
472 #if defined(HAVE_BACKLIGHT_PWM_FADING) && defined(CPU_COLDFIRE) \
473 && !defined(SIMULATOR)
474 case BACKLIGHT_UNBOOST_CPU
:
479 #if defined(HAVE_REMOTE_LCD) && !defined(SIMULATOR)
480 /* Here for now or else the aggressive init messes up scrolling */
481 case SYS_REMOTE_PLUGGED
:
486 case SYS_REMOTE_UNPLUGGED
:
489 #endif /* defined(HAVE_REMOTE_LCD) && !defined(SIMULATOR) */
491 case SYS_USB_CONNECTED
:
492 /* Tell the USB thread that we are safe */
493 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
494 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
497 case SYS_USB_DISCONNECTED
:
498 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
505 { /* These events are only processed if backlight isn't locked */
506 #ifdef HAVE_REMOTE_LCD
507 case REMOTE_BACKLIGHT_ON
:
508 remote_backlight_update_state();
511 case REMOTE_BACKLIGHT_OFF
:
512 remote_backlight_timer
= 0; /* Disable the timeout */
513 __remote_backlight_off();
515 #endif /* HAVE_REMOTE_LCD */
518 backlight_update_state();
522 backlight_timer
= 0; /* Disable the timeout */
526 #ifdef HAVE_LCD_SLEEP
531 #ifdef HAVE_BUTTON_LIGHT
532 case BUTTON_LIGHT_ON
:
533 buttonlight_update_state();
536 case BUTTON_LIGHT_OFF
:
537 button_backlight_timer
= 0;
538 _button_backlight_off();
542 case SYS_POWEROFF
: /* Lock backlight on poweroff so it doesn't */
543 locked
= true; /* go off before power is actually cut. */
546 case SYS_CHARGER_CONNECTED
:
547 case SYS_CHARGER_DISCONNECTED
:
549 backlight_update_state();
550 #ifdef HAVE_REMOTE_LCD
551 remote_backlight_update_state();
558 static void backlight_tick(void)
563 if(backlight_timer
== 0)
568 #ifdef HAVE_LCD_SLEEP
569 else if(lcd_sleep_timer
)
572 if(lcd_sleep_timer
== 0)
574 /* Queue on bl thread or freeze! */
575 queue_post(&backlight_queue
, LCD_SLEEP
, 0);
578 #endif /* HAVE_LCD_SLEEP */
580 #ifdef HAVE_REMOTE_LCD
581 if(remote_backlight_timer
)
583 remote_backlight_timer
--;
584 if(remote_backlight_timer
== 0)
586 remote_backlight_off();
589 #endif /* HAVE_REMOVE_LCD */
590 #ifdef HAVE_BUTTON_LIGHT
591 if (button_backlight_timer
)
593 button_backlight_timer
--;
594 if (button_backlight_timer
== 0)
596 button_backlight_off();
599 #endif /* HAVE_BUTTON_LIGHT */
602 void backlight_init(void)
604 queue_init(&backlight_queue
, true);
605 queue_set_irq_safe(&backlight_queue
, true);
608 if (__backlight_init())
610 # ifdef HAVE_BACKLIGHT_PWM_FADING
611 /* If backlight is already on, don't fade in. */
612 bl_dim_current
= BL_PWM_COUNT
;
613 bl_dim_target
= BL_PWM_COUNT
;
618 #ifdef HAVE_REMOTE_LCD
619 remote_backlight_on();
621 #ifdef HAVE_BUTTON_LIGHT
622 button_backlight_on();
625 create_thread(backlight_thread
, backlight_stack
,
626 sizeof(backlight_stack
), backlight_thread_name
627 IF_PRIO(, PRIORITY_SYSTEM
)
628 IF_COP(, CPU
, false));
629 tick_add_task(backlight_tick
);
632 void backlight_on(void)
634 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
635 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
638 void backlight_off(void)
640 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
643 /* returns true when the backlight is on OR when it's set to always off */
644 bool is_backlight_on(void)
646 if (backlight_timer
|| backlight_timeout
<= 0)
652 /* return value in ticks; 0 means always on, <0 means always off */
653 int backlight_get_current_timeout(void)
655 return backlight_timeout
;
658 void backlight_set_timeout(int index
)
660 if((unsigned)index
>= sizeof(backlight_timeout_value
))
661 /* if given a weird value, use default */
663 backlight_timeout_normal
= HZ
* backlight_timeout_value
[index
];
664 backlight_update_state();
668 void backlight_set_timeout_plugged(int index
)
670 if((unsigned)index
>= sizeof(backlight_timeout_value
))
671 /* if given a weird value, use default */
673 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
674 backlight_update_state();
676 #endif /* CONFIG_CHARGING */
678 #ifdef HAS_BUTTON_HOLD
679 /* Hold button change event handler. */
680 void backlight_hold_changed(bool hold_button
)
682 if (!hold_button
|| (backlight_on_button_hold
> 0))
683 /* if unlocked or override in effect */
687 void backlight_set_on_button_hold(int index
)
689 if ((unsigned)index
>= 3)
690 /* if given a weird value, use default */
693 backlight_on_button_hold
= index
;
694 backlight_update_state();
696 #endif /* HAS_BUTTON_HOLD */
698 #ifdef HAVE_LCD_SLEEP
699 void lcd_set_sleep_after_backlight_off(int index
)
701 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
702 /* if given a weird value, use default */
705 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
707 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
708 /* Timer will be set when bl turns off or bl set to on. */
711 /* Backlight is Off */
712 if (lcd_sleep_timeout
< 0)
713 lcd_sleep_timer
= 1; /* Always - sleep next tick */
715 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
717 #endif /* HAVE_LCD_SLEEP */
719 #ifdef HAVE_REMOTE_LCD
720 void remote_backlight_on(void)
722 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
725 void remote_backlight_off(void)
727 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
730 void remote_backlight_set_timeout(int index
)
732 if((unsigned)index
>= sizeof(backlight_timeout_value
))
733 /* if given a weird value, use default */
735 remote_backlight_timeout_normal
= HZ
* backlight_timeout_value
[index
];
736 remote_backlight_update_state();
740 void remote_backlight_set_timeout_plugged(int index
)
742 if((unsigned)index
>= sizeof(backlight_timeout_value
))
743 /* if given a weird value, use default */
745 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
746 remote_backlight_update_state();
748 #endif /* CONFIG_CHARGING */
750 #ifdef HAS_REMOTE_BUTTON_HOLD
751 /* Remote hold button change event handler. */
752 void remote_backlight_hold_changed(bool rc_hold_button
)
754 if (!rc_hold_button
|| (remote_backlight_on_button_hold
> 0))
755 /* if unlocked or override */
756 remote_backlight_on();
759 void remote_backlight_set_on_button_hold(int index
)
761 if ((unsigned)index
>= 3)
762 /* if given a weird value, use default */
765 remote_backlight_on_button_hold
= index
;
766 remote_backlight_update_state();
768 #endif /* HAS_REMOTE_BUTTON_HOLD */
770 /* return value in ticks; 0 means always on, <0 means always off */
771 int remote_backlight_get_current_timeout(void)
773 return remote_backlight_timeout
;
776 /* returns true when the backlight is on OR when it's set to always off */
777 bool is_remote_backlight_on(void)
779 if (remote_backlight_timer
!= 0 || remote_backlight_timeout
<= 0)
785 #endif /* HAVE_REMOTE_LCD */
787 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
788 void backlight_set_brightness(int val
)
790 if (val
< MIN_BRIGHTNESS_SETTING
)
791 val
= MIN_BRIGHTNESS_SETTING
;
792 else if (val
> MAX_BRIGHTNESS_SETTING
)
793 val
= MAX_BRIGHTNESS_SETTING
;
795 __backlight_set_brightness(val
);
797 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
799 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
800 void buttonlight_set_brightness(int val
)
802 if (val
< MIN_BRIGHTNESS_SETTING
)
803 val
= MIN_BRIGHTNESS_SETTING
;
804 else if (val
> MAX_BRIGHTNESS_SETTING
)
805 val
= MAX_BRIGHTNESS_SETTING
;
807 __buttonlight_set_brightness(val
);
809 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
811 #else /* !defined(HAVE_BACKLIGHT) || defined(BOOTLOADER)
812 -- no backlight, empty dummy functions */
814 #if defined(BOOTLOADER) && defined(HAVE_BACKLIGHT)
815 void backlight_init(void)
817 (void)__backlight_init();
822 void backlight_on(void) {}
823 void backlight_off(void) {}
824 void button_backlight_on(void) {}
825 void backlight_set_timeout(int index
) {(void)index
;}
826 bool is_backlight_on(void) {return true;}
827 #ifdef HAVE_REMOTE_LCD
828 void remote_backlight_on(void) {}
829 void remote_backlight_off(void) {}
830 void remote_backlight_set_timeout(int index
) {(void)index
;}
831 bool is_remote_backlight_on(void) {return true;}
832 #endif /* HAVE_REMOTE_LCD */
833 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
834 void backlight_set_brightness(int val
) { (void)val
; }
836 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
837 void buttonlight_set_brightness(int val
) { (void)val
; }
839 #endif /* defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER) */