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 #ifdef X5_BACKLIGHT_SHUTDOWN
94 #define BACKLIGHT_QUIT 256
96 static const char backlight_thread_name
[] = "backlight";
97 static struct event_queue backlight_queue
;
99 static int backlight_timer
;
100 static int backlight_timeout
= 5*HZ
;
102 static int backlight_timeout_plugged
= 5*HZ
;
104 #ifdef HAS_BUTTON_HOLD
105 static int backlight_on_button_hold
= 0;
108 #ifdef HAVE_BUTTON_LIGHT
109 static int button_backlight_timer
;
110 static int button_backlight_timeout
= 5*HZ
;
112 /* internal interface */
113 static void _button_backlight_on(void)
116 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
117 __button_backlight_dim(false);
119 __button_backlight_on();
124 void _button_backlight_off(void)
127 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
128 if(button_backlight_timeout
>0)
129 __button_backlight_dim(true);
132 __button_backlight_off();
136 /* Update state of buttonlight according to timeout setting */
137 static void buttonlight_update_state(void)
139 button_backlight_timer
= button_backlight_timeout
;
141 /* Buttonlight == OFF in the setting? */
142 if (button_backlight_timer
< 0)
144 button_backlight_timer
= 0; /* Disable the timeout */
145 _button_backlight_off();
148 _button_backlight_on();
151 /* external interface */
152 void button_backlight_on(void)
154 queue_remove_from_head(&backlight_queue
, BUTTON_LIGHT_ON
);
155 queue_post(&backlight_queue
, BUTTON_LIGHT_ON
, 0);
158 void button_backlight_off(void)
160 queue_post(&backlight_queue
, BUTTON_LIGHT_OFF
, 0);
163 void button_backlight_set_timeout(int index
)
165 if((unsigned)index
>= sizeof(backlight_timeout_value
))
166 /* if given a weird value, use default */
168 button_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
169 buttonlight_update_state();
174 #ifdef HAVE_REMOTE_LCD
175 static int remote_backlight_timer
;
176 static int remote_backlight_timeout
= 5*HZ
;
178 static int remote_backlight_timeout_plugged
= 5*HZ
;
180 #ifdef HAS_REMOTE_BUTTON_HOLD
181 static int remote_backlight_on_button_hold
= 0;
185 #ifdef HAVE_LCD_SLEEP
186 const signed char lcd_sleep_timeout_value
[10] =
188 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
190 static int lcd_sleep_timer
;
191 static int lcd_sleep_timeout
= 10*HZ
;
194 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
195 /* backlight fading */
196 #define BL_PWM_INTERVAL 5000 /* Cycle interval in s */
197 #define BL_PWM_COUNT 100
198 static const char backlight_fade_value
[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
199 static int fade_in_count
= 1;
200 static int fade_out_count
= 4;
202 static bool bl_timer_active
= false;
203 static int bl_dim_current
= 0;
204 static int bl_dim_target
= 0;
205 static int bl_pwm_counter
= 0;
206 static volatile int bl_cycle_counter
= 0;
207 static enum {DIM_STATE_START
, DIM_STATE_MAIN
} bl_dim_state
= DIM_STATE_START
;
209 static void backlight_isr(void)
214 timer_period
= TIMER_FREQ
/ 1000 * BL_PWM_INTERVAL
/ 1000;
215 switch (bl_dim_state
)
218 case DIM_STATE_START
:
222 if (bl_dim_current
> 0 && bl_dim_current
< BL_PWM_COUNT
)
225 bl_pwm_counter
= bl_dim_current
;
226 timer_period
= timer_period
* bl_pwm_counter
/ BL_PWM_COUNT
;
227 bl_dim_state
= DIM_STATE_MAIN
;
235 if (bl_dim_current
== bl_dim_target
)
241 /* Dim main screen */
244 bl_dim_state
= DIM_STATE_START
;
245 timer_period
= timer_period
* (BL_PWM_COUNT
- bl_pwm_counter
) / BL_PWM_COUNT
;
249 if ((bl_dim_target
> bl_dim_current
) && (bl_cycle_counter
>= fade_in_count
))
252 bl_cycle_counter
= 0;
255 if ((bl_dim_target
< bl_dim_current
) && (bl_cycle_counter
>= fade_out_count
))
258 bl_cycle_counter
= 0;
264 queue_post(&backlight_queue
, BACKLIGHT_UNBOOST_CPU
, 0);
267 bl_timer_active
= false;
270 timer_set_period(timer_period
);
273 static void backlight_switch(void)
275 if (bl_dim_target
> (BL_PWM_COUNT
/2))
278 bl_dim_current
= BL_PWM_COUNT
;
287 static void backlight_release_timer(void)
293 bl_timer_active
= false;
297 static void backlight_dim(int value
)
299 /* protect from extraneous calls with the same target value */
300 if (value
== bl_dim_target
)
303 bl_dim_target
= value
;
308 if (timer_register(0, backlight_release_timer
, 2, 0, backlight_isr
))
311 /* Prevent cpu frequency changes while dimming. */
314 bl_timer_active
= true;
320 void backlight_set_fade_in(int index
)
322 fade_in_count
= backlight_fade_value
[index
];
325 void backlight_set_fade_out(int index
)
327 fade_out_count
= backlight_fade_value
[index
];
329 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
331 static void _backlight_on(void)
333 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
334 if (fade_in_count
> 0)
335 backlight_dim(BL_PWM_COUNT
);
338 bl_dim_target
= bl_dim_current
= BL_PWM_COUNT
;
341 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
342 /* call the enable from here - it takes longer than the disable */
344 __backlight_dim(false);
348 #ifdef HAVE_LCD_SLEEP
349 lcd_sleep_timer
= 0; /* LCD should be awake already */
353 static void _backlight_off(void)
355 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
356 if (fade_out_count
> 0)
360 bl_dim_target
= bl_dim_current
= 0;
363 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
364 __backlight_dim(true);
369 #ifdef HAVE_LCD_SLEEP
370 /* Start LCD sleep countdown */
371 if (lcd_sleep_timeout
< 0)
373 lcd_sleep_timer
= 0; /* Setting == Always */
377 lcd_sleep_timer
= lcd_sleep_timeout
;
381 #ifdef HAVE_REMOTE_LCD
383 static void __remote_backlight_on(void)
385 sim_remote_backlight(100);
388 static void __remote_backlight_off(void)
390 sim_remote_backlight(0);
392 #endif /* SIMULATOR */
393 #endif /* HAVE_REMOTE_LCD */
395 /* Update state of backlight according to timeout setting */
396 static void backlight_update_state(void)
399 if (charger_inserted()
400 #ifdef HAVE_USB_POWER
404 backlight_timer
= backlight_timeout_plugged
;
407 backlight_timer
= backlight_timeout
;
409 /* Backlight == OFF in the setting? */
410 if (backlight_timer
< 0)
412 backlight_timer
= 0; /* Disable the timeout */
413 #ifdef HAS_BUTTON_HOLD
414 if (backlight_on_button_hold
== 2 && button_hold())
415 return; /* Keep on if "On" */
421 #ifdef HAS_BUTTON_HOLD
422 if (backlight_on_button_hold
== 1 && button_hold())
424 /* Keep off if "Off". */
425 backlight_timer
= 0; /* Disable the timeout */
433 #ifdef HAVE_REMOTE_LCD
434 /* Update state of remote backlight according to timeout setting */
435 static void remote_backlight_update_state(void)
438 if (charger_inserted()
439 #ifdef HAVE_USB_POWER
443 remote_backlight_timer
= remote_backlight_timeout_plugged
;
446 remote_backlight_timer
= remote_backlight_timeout
;
448 /* Backlight == OFF in the setting? */
449 if (remote_backlight_timer
< 0)
451 remote_backlight_timer
= 0; /* Disable the timeout */
452 #ifdef HAS_REMOTE_BUTTON_HOLD
453 if (remote_backlight_on_button_hold
== 2 && remote_button_hold())
454 return; /* Keep on if "On" */
456 __remote_backlight_off();
460 #if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
461 if (remote_type() == REMOTETYPE_H300_NONLCD
)
463 backlight_update_state();
468 #ifdef HAS_REMOTE_BUTTON_HOLD
469 if (remote_backlight_on_button_hold
== 1 && remote_button_hold())
471 /* Keep off if "Off". */
472 remote_backlight_timer
= 0; /* Disable the timeout */
476 __remote_backlight_on();
480 #endif /* HAVE_REMOTE_LCD */
482 void backlight_thread(void)
488 queue_wait(&backlight_queue
, &ev
);
491 #ifdef HAVE_REMOTE_LCD
492 case REMOTE_BACKLIGHT_ON
:
493 remote_backlight_update_state();
496 case REMOTE_BACKLIGHT_OFF
:
497 remote_backlight_timer
= 0; /* Disable the timeout */
498 #ifdef HAS_REMOTE_BUTTON_HOLD
499 if (remote_backlight_on_button_hold
== 2 &&
500 remote_button_hold())
501 break; /* Keep on if "On" */
503 __remote_backlight_off();
505 #endif /* HAVE_REMOTE_LCD */
508 backlight_update_state();
512 backlight_timer
= 0; /* Disable the timeout */
513 #ifdef HAS_BUTTON_HOLD
514 if (backlight_on_button_hold
== 2 && button_hold())
515 break; /* Keep on if "On" */
520 #ifdef HAVE_LCD_SLEEP
525 #ifdef HAVE_BUTTON_LIGHT
526 case BUTTON_LIGHT_ON
:
527 buttonlight_update_state();
529 case BUTTON_LIGHT_OFF
:
530 button_backlight_timer
= 0;
531 _button_backlight_off();
535 #ifdef X5_BACKLIGHT_SHUTDOWN
541 #if defined(HAVE_BACKLIGHT_PWM_FADING) && defined(CPU_COLDFIRE) \
542 && !defined(SIMULATOR)
543 case BACKLIGHT_UNBOOST_CPU
:
548 #if defined(HAVE_REMOTE_LCD) && !defined(SIMULATOR)
549 /* Here for now or else the aggressive init messes up scrolling */
550 case SYS_REMOTE_PLUGGED
:
555 case SYS_REMOTE_UNPLUGGED
:
558 #endif /* defined(HAVE_REMOTE_LCD) && !defined(SIMULATOR) */
560 case SYS_USB_CONNECTED
:
561 /* Tell the USB thread that we are safe */
562 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
563 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
566 case SYS_USB_DISCONNECTED
:
567 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
573 static void backlight_tick(void)
576 static bool charger_was_inserted
= false;
577 bool charger_is_inserted
= charger_inserted()
578 #ifdef HAVE_USB_POWER
583 if( charger_was_inserted
!= charger_is_inserted
)
586 #ifdef HAVE_REMOTE_LCD
587 remote_backlight_on();
590 charger_was_inserted
= charger_is_inserted
;
591 #endif /* CONFIG_CHARGING */
596 if(backlight_timer
== 0)
601 #ifdef HAVE_LCD_SLEEP
602 else if(lcd_sleep_timer
)
605 if(lcd_sleep_timer
== 0)
607 /* Queue on bl thread or freeze! */
608 queue_post(&backlight_queue
, LCD_SLEEP
, 0);
611 #endif /* HAVE_LCD_SLEEP */
613 #ifdef HAVE_REMOTE_LCD
614 if(remote_backlight_timer
)
616 remote_backlight_timer
--;
617 if(remote_backlight_timer
== 0)
619 remote_backlight_off();
622 #endif /* HAVE_REMOVE_LCD */
623 #ifdef HAVE_BUTTON_LIGHT
624 if (button_backlight_timer
)
626 button_backlight_timer
--;
627 if (button_backlight_timer
== 0)
629 button_backlight_off();
632 #endif /* HAVE_BUTTON_LIGHT */
635 void backlight_init(void)
637 queue_init(&backlight_queue
, true);
638 queue_set_irq_safe(&backlight_queue
, true);
641 if (__backlight_init())
643 # ifdef HAVE_BACKLIGHT_PWM_FADING
644 /* If backlight is already on, don't fade in. */
645 bl_dim_current
= BL_PWM_COUNT
;
646 bl_dim_target
= BL_PWM_COUNT
;
651 #ifdef HAVE_REMOTE_LCD
652 remote_backlight_on();
654 #ifdef HAVE_BUTTON_LIGHT
655 button_backlight_on();
658 create_thread(backlight_thread
, backlight_stack
,
659 sizeof(backlight_stack
), backlight_thread_name
660 IF_PRIO(, PRIORITY_SYSTEM
)
661 IF_COP(, CPU
, false));
662 tick_add_task(backlight_tick
);
665 #ifdef X5_BACKLIGHT_SHUTDOWN
666 void x5_backlight_shutdown(void)
668 /* Turn on the screen and don't let anyone else mess with it. Called
669 from clean_shutdown in misc.c. */
670 queue_empty(&backlight_queue
);
671 tick_remove_task(backlight_tick
);
672 /* Next time the thread runs, if at all, it will just remove itself. */
673 queue_post(&backlight_queue
, BACKLIGHT_QUIT
, 0);
676 #endif /* X5_BACKLIGHT_SHUTDOWN */
678 void backlight_on(void)
680 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
681 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
684 void backlight_off(void)
686 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
689 /* returns true when the backlight is on OR when it's set to always off */
690 bool is_backlight_on(void)
692 if (backlight_timer
|| backlight_get_current_timeout() <= 0)
698 /* return value in ticks; 0 means always on, <0 means always off */
699 int backlight_get_current_timeout(void)
702 if (charger_inserted()
703 #ifdef HAVE_USB_POWER
707 return backlight_timeout_plugged
;
709 return backlight_timeout
;
711 return backlight_timeout
;
715 void backlight_set_timeout(int index
)
717 if((unsigned)index
>= sizeof(backlight_timeout_value
))
718 /* if given a weird value, use default */
720 backlight_timeout
= HZ
* backlight_timeout_value
[index
];
721 backlight_update_state();
725 void backlight_set_timeout_plugged(int index
)
727 if((unsigned)index
>= sizeof(backlight_timeout_value
))
728 /* if given a weird value, use default */
730 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
731 backlight_update_state();
733 #endif /* CONFIG_CHARGING */
735 #ifdef HAS_BUTTON_HOLD
736 /* Hold button change event handler. */
737 void backlight_hold_changed(bool hold_button
)
739 /* Hold switch overrides all backlight behavior except when
741 /* Queue or freeze */
742 if (hold_button
&& backlight_on_button_hold
== 1)
743 backlight_off(); /* setting == Off */
744 else /* setting == On, Normal, no hold button, or anything else */
748 void backlight_set_on_button_hold(int index
)
750 if ((unsigned)index
>= 3)
751 /* if given a weird value, use default */
754 if (index
== backlight_on_button_hold
)
757 backlight_on_button_hold
= index
;
758 backlight_hold_changed(button_hold());
760 #endif /* HAS_BUTTON_HOLD */
762 #ifdef HAVE_LCD_SLEEP
763 void lcd_set_sleep_after_backlight_off(int index
)
765 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
766 /* if given a weird value, use default */
769 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
771 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
772 /* Timer will be set when bl turns off or bl set to on. */
775 /* Backlight is Off */
776 if (lcd_sleep_timeout
< 0)
777 lcd_sleep_timer
= 1; /* Always - sleep next tick */
779 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
781 #endif /* HAVE_LCD_SLEEP */
783 #ifdef HAVE_REMOTE_LCD
784 void remote_backlight_on(void)
786 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
789 void remote_backlight_off(void)
791 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
794 void remote_backlight_set_timeout(int index
)
796 if((unsigned)index
>= sizeof(backlight_timeout_value
))
797 /* if given a weird value, use default */
799 remote_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
800 remote_backlight_update_state();
804 void remote_backlight_set_timeout_plugged(int index
)
806 if((unsigned)index
>= sizeof(backlight_timeout_value
))
807 /* if given a weird value, use default */
809 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
810 remote_backlight_update_state();
812 #endif /* CONFIG_CHARGING */
814 #ifdef HAS_REMOTE_BUTTON_HOLD
815 /* Remote hold button change event handler. */
816 void remote_backlight_hold_changed(bool rc_hold_button
)
818 /* Hold switch overrides all backlight behavior except when
820 /* Queue or freeze */
821 if (rc_hold_button
&& remote_backlight_on_button_hold
== 1)
822 remote_backlight_off(); /* setting == Off */
823 else /* setting == On, Normal, no hold button, or anything else */
824 remote_backlight_on();
827 void remote_backlight_set_on_button_hold(int index
)
829 if ((unsigned)index
>= 3)
830 /* if given a weird value, use default */
833 if (index
== remote_backlight_on_button_hold
)
836 remote_backlight_on_button_hold
= index
;
837 remote_backlight_hold_changed(remote_button_hold());
839 #endif /* HAS_REMOTE_BUTTON_HOLD */
841 /* return value in ticks; 0 means always on, <0 means always off */
842 int remote_backlight_get_current_timeout(void)
845 if (charger_inserted()
846 #ifdef HAVE_USB_POWER
850 return remote_backlight_timeout_plugged
;
852 return remote_backlight_timeout
;
854 return remote_backlight_timeout
;
858 /* returns true when the backlight is on OR when it's set to always off */
859 bool is_remote_backlight_on(void)
861 if (remote_backlight_timer
!= 0 ||
862 remote_backlight_get_current_timeout() <= 0)
868 #endif /* HAVE_REMOTE_LCD */
870 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
871 void backlight_set_brightness(int val
)
873 if (val
< MIN_BRIGHTNESS_SETTING
)
874 val
= MIN_BRIGHTNESS_SETTING
;
875 else if (val
> MAX_BRIGHTNESS_SETTING
)
876 val
= MAX_BRIGHTNESS_SETTING
;
878 __backlight_set_brightness(val
);
880 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
882 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
883 void buttonlight_set_brightness(int val
)
885 if (val
< MIN_BRIGHTNESS_SETTING
)
886 val
= MIN_BRIGHTNESS_SETTING
;
887 else if (val
> MAX_BRIGHTNESS_SETTING
)
888 val
= MAX_BRIGHTNESS_SETTING
;
890 __buttonlight_set_brightness(val
);
892 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
894 #else /* !defined(HAVE_BACKLIGHT) || defined(BOOTLOADER)
895 -- no backlight, empty dummy functions */
897 #if defined(BOOTLOADER) && defined(HAVE_BACKLIGHT)
898 void backlight_init(void)
900 (void)__backlight_init();
905 void backlight_on(void) {}
906 void backlight_off(void) {}
907 void button_backlight_on(void) {}
908 void backlight_set_timeout(int index
) {(void)index
;}
909 bool is_backlight_on(void) {return true;}
910 #ifdef HAVE_REMOTE_LCD
911 void remote_backlight_on(void) {}
912 void remote_backlight_off(void) {}
913 void remote_backlight_set_timeout(int index
) {(void)index
;}
914 bool is_remote_backlight_on(void) {return true;}
915 #endif /* HAVE_REMOTE_LCD */
916 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
917 void backlight_set_brightness(int val
) { (void)val
; }
919 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
920 void buttonlight_set_brightness(int val
) { (void)val
; }
922 #endif /* defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER) */