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 case SYS_USB_CONNECTED
:
549 /* Tell the USB thread that we are safe */
550 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
551 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
554 case SYS_USB_DISCONNECTED
:
555 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
561 static void backlight_tick(void)
564 static bool charger_was_inserted
= false;
565 bool charger_is_inserted
= charger_inserted()
566 #ifdef HAVE_USB_POWER
571 if( charger_was_inserted
!= charger_is_inserted
)
574 #ifdef HAVE_REMOTE_LCD
575 remote_backlight_on();
578 charger_was_inserted
= charger_is_inserted
;
579 #endif /* CONFIG_CHARGING */
584 if(backlight_timer
== 0)
589 #ifdef HAVE_LCD_SLEEP
590 else if(lcd_sleep_timer
)
593 if(lcd_sleep_timer
== 0)
595 /* Queue on bl thread or freeze! */
596 queue_post(&backlight_queue
, LCD_SLEEP
, 0);
599 #endif /* HAVE_LCD_SLEEP */
601 #ifdef HAVE_REMOTE_LCD
602 if(remote_backlight_timer
)
604 remote_backlight_timer
--;
605 if(remote_backlight_timer
== 0)
607 remote_backlight_off();
610 #endif /* HAVE_REMOVE_LCD */
611 #ifdef HAVE_BUTTON_LIGHT
612 if (button_backlight_timer
)
614 button_backlight_timer
--;
615 if (button_backlight_timer
== 0)
617 button_backlight_off();
620 #endif /* HAVE_BUTTON_LIGHT */
623 void backlight_init(void)
625 queue_init(&backlight_queue
, true);
626 queue_set_irq_safe(&backlight_queue
, true);
629 if (__backlight_init())
631 # ifdef HAVE_BACKLIGHT_PWM_FADING
632 /* If backlight is already on, don't fade in. */
633 bl_dim_current
= BL_PWM_COUNT
;
634 bl_dim_target
= BL_PWM_COUNT
;
639 #ifdef HAVE_REMOTE_LCD
640 remote_backlight_on();
642 #ifdef HAVE_BUTTON_LIGHT
643 button_backlight_on();
646 create_thread(backlight_thread
, backlight_stack
,
647 sizeof(backlight_stack
), backlight_thread_name
648 IF_PRIO(, PRIORITY_SYSTEM
)
649 IF_COP(, CPU
, false));
650 tick_add_task(backlight_tick
);
653 #ifdef X5_BACKLIGHT_SHUTDOWN
654 void x5_backlight_shutdown(void)
656 /* Turn on the screen and don't let anyone else mess with it. Called
657 from clean_shutdown in misc.c. */
658 queue_empty(&backlight_queue
);
659 tick_remove_task(backlight_tick
);
660 /* Next time the thread runs, if at all, it will just remove itself. */
661 queue_post(&backlight_queue
, BACKLIGHT_QUIT
, 0);
664 #endif /* X5_BACKLIGHT_SHUTDOWN */
666 void backlight_on(void)
668 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
669 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
672 void backlight_off(void)
674 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
677 /* returns true when the backlight is on OR when it's set to always off */
678 bool is_backlight_on(void)
680 if (backlight_timer
|| backlight_get_current_timeout() <= 0)
686 /* return value in ticks; 0 means always on, <0 means always off */
687 int backlight_get_current_timeout(void)
690 if (charger_inserted()
691 #ifdef HAVE_USB_POWER
695 return backlight_timeout_plugged
;
697 return backlight_timeout
;
699 return backlight_timeout
;
703 void backlight_set_timeout(int index
)
705 if((unsigned)index
>= sizeof(backlight_timeout_value
))
706 /* if given a weird value, use default */
708 backlight_timeout
= HZ
* backlight_timeout_value
[index
];
709 backlight_update_state();
713 void backlight_set_timeout_plugged(int index
)
715 if((unsigned)index
>= sizeof(backlight_timeout_value
))
716 /* if given a weird value, use default */
718 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
719 backlight_update_state();
721 #endif /* CONFIG_CHARGING */
723 #ifdef HAS_BUTTON_HOLD
724 /* Hold button change event handler. */
725 void backlight_hold_changed(bool hold_button
)
727 /* Hold switch overrides all backlight behavior except when
729 /* Queue or freeze */
730 if (hold_button
&& backlight_on_button_hold
== 1)
731 backlight_off(); /* setting == Off */
732 else /* setting == On, Normal, no hold button, or anything else */
736 void backlight_set_on_button_hold(int index
)
738 if ((unsigned)index
>= 3)
739 /* if given a weird value, use default */
742 if (index
== backlight_on_button_hold
)
745 backlight_on_button_hold
= index
;
746 backlight_hold_changed(button_hold());
748 #endif /* HAS_BUTTON_HOLD */
750 #ifdef HAVE_LCD_SLEEP
751 void lcd_set_sleep_after_backlight_off(int index
)
753 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
754 /* if given a weird value, use default */
757 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
759 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
760 /* Timer will be set when bl turns off or bl set to on. */
763 /* Backlight is Off */
764 if (lcd_sleep_timeout
< 0)
765 lcd_sleep_timer
= 1; /* Always - sleep next tick */
767 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
769 #endif /* HAVE_LCD_SLEEP */
771 #ifdef HAVE_REMOTE_LCD
772 void remote_backlight_on(void)
774 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
777 void remote_backlight_off(void)
779 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
782 void remote_backlight_set_timeout(int index
)
784 if((unsigned)index
>= sizeof(backlight_timeout_value
))
785 /* if given a weird value, use default */
787 remote_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
788 remote_backlight_update_state();
792 void remote_backlight_set_timeout_plugged(int index
)
794 if((unsigned)index
>= sizeof(backlight_timeout_value
))
795 /* if given a weird value, use default */
797 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
798 remote_backlight_update_state();
800 #endif /* CONFIG_CHARGING */
802 #ifdef HAS_REMOTE_BUTTON_HOLD
803 /* Remote hold button change event handler. */
804 void remote_backlight_hold_changed(bool rc_hold_button
)
806 /* Hold switch overrides all backlight behavior except when
808 /* Queue or freeze */
809 if (rc_hold_button
&& remote_backlight_on_button_hold
== 1)
810 remote_backlight_off(); /* setting == Off */
811 else /* setting == On, Normal, no hold button, or anything else */
812 remote_backlight_on();
815 void remote_backlight_set_on_button_hold(int index
)
817 if ((unsigned)index
>= 3)
818 /* if given a weird value, use default */
821 if (index
== remote_backlight_on_button_hold
)
824 remote_backlight_on_button_hold
= index
;
825 remote_backlight_hold_changed(remote_button_hold());
827 #endif /* HAS_REMOTE_BUTTON_HOLD */
829 /* return value in ticks; 0 means always on, <0 means always off */
830 int remote_backlight_get_current_timeout(void)
833 if (charger_inserted()
834 #ifdef HAVE_USB_POWER
838 return remote_backlight_timeout_plugged
;
840 return remote_backlight_timeout
;
842 return remote_backlight_timeout
;
846 /* returns true when the backlight is on OR when it's set to always off */
847 bool is_remote_backlight_on(void)
849 if (remote_backlight_timer
!= 0 ||
850 remote_backlight_get_current_timeout() <= 0)
856 #endif /* HAVE_REMOTE_LCD */
858 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
859 void backlight_set_brightness(int val
)
861 if (val
< MIN_BRIGHTNESS_SETTING
)
862 val
= MIN_BRIGHTNESS_SETTING
;
863 else if (val
> MAX_BRIGHTNESS_SETTING
)
864 val
= MAX_BRIGHTNESS_SETTING
;
866 __backlight_set_brightness(val
);
868 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
870 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
871 void buttonlight_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 __buttonlight_set_brightness(val
);
880 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
882 #else /* !defined(HAVE_BACKLIGHT) || defined(BOOTLOADER)
883 -- no backlight, empty dummy functions */
885 #if defined(BOOTLOADER) && defined(HAVE_BACKLIGHT)
886 void backlight_init(void)
888 (void)__backlight_init();
893 void backlight_on(void) {}
894 void backlight_off(void) {}
895 void button_backlight_on(void) {}
896 void backlight_set_timeout(int index
) {(void)index
;}
897 bool is_backlight_on(void) {return true;}
898 #ifdef HAVE_REMOTE_LCD
899 void remote_backlight_on(void) {}
900 void remote_backlight_off(void) {}
901 void remote_backlight_set_timeout(int index
) {(void)index
;}
902 bool is_remote_backlight_on(void) {return true;}
903 #endif /* HAVE_REMOTE_LCD */
904 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
905 void backlight_set_brightness(int val
) { (void)val
; }
907 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
908 void buttonlight_set_brightness(int val
) { (void)val
; }
910 #endif /* defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER) */