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
)
61 #endif /* SIMULATOR */
63 #if defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER)
65 const signed char backlight_timeout_value
[19] =
67 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
70 #define BACKLIGHT_ON 1
71 #define BACKLIGHT_OFF 2
72 #define REMOTE_BACKLIGHT_ON 3
73 #define REMOTE_BACKLIGHT_OFF 4
74 #define BACKLIGHT_UNBOOST_CPU 5
78 #ifdef HAVE_BUTTON_LIGHT
79 #define BUTTON_LIGHT_ON 7
80 #define BUTTON_LIGHT_OFF 8
83 static void backlight_thread(void);
84 static long backlight_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
85 #ifdef X5_BACKLIGHT_SHUTDOWN
86 #define BACKLIGHT_QUIT 256
88 static const char backlight_thread_name
[] = "backlight";
89 static struct event_queue backlight_queue
;
91 static int backlight_timer
;
92 static int backlight_timeout
= 5*HZ
;
94 static int backlight_timeout_plugged
= 5*HZ
;
96 #ifdef HAS_BUTTON_HOLD
97 static int backlight_on_button_hold
= 0;
100 #ifdef HAVE_BUTTON_LIGHT
101 static int button_backlight_timer
;
102 static int button_backlight_timeout
= 5*HZ
;
104 /* external interface */
105 void button_backlight_on(void)
107 queue_remove_from_head(&backlight_queue
, BUTTON_LIGHT_ON
);
108 queue_post(&backlight_queue
, BUTTON_LIGHT_ON
, 0);
111 void button_backlight_off(void)
113 queue_post(&backlight_queue
, BUTTON_LIGHT_OFF
, 0);
116 void button_backlight_set_timeout(int index
)
118 if((unsigned)index
>= sizeof(backlight_timeout_value
))
119 /* if given a weird value, use default */
121 button_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
122 if (index
== 0) /* off */
123 button_backlight_off();
124 else if (index
== 1) /* on */
125 button_backlight_on();
127 if (button_backlight_timer
)
128 button_backlight_timer
= button_backlight_timeout
;
131 /* internal interface */
132 static void _button_backlight_on(void)
134 if (button_backlight_timeout
< 0)
136 button_backlight_timer
= button_backlight_timeout
;
138 __button_backlight_on();
142 static void _button_backlight_off(void)
144 if (button_backlight_timeout
== 0)
146 button_backlight_timer
= 0;
148 __button_backlight_off();
154 #ifdef HAVE_REMOTE_LCD
155 static int remote_backlight_timer
;
156 static int remote_backlight_timeout
= 5*HZ
;
158 static int remote_backlight_timeout_plugged
= 5*HZ
;
160 #ifdef HAS_REMOTE_BUTTON_HOLD
161 static int remote_backlight_on_button_hold
= 0;
165 #ifdef HAVE_LCD_SLEEP
166 const signed char lcd_sleep_timeout_value
[10] =
168 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
170 static int lcd_sleep_timer
;
171 static int lcd_sleep_timeout
= 10*HZ
;
174 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
175 /* backlight fading */
176 #define BL_PWM_INTERVAL 5000 /* Cycle interval in s */
177 #define BL_PWM_COUNT 100
178 static const char backlight_fade_value
[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
179 static int fade_in_count
= 1;
180 static int fade_out_count
= 4;
182 static bool bl_timer_active
= false;
183 static int bl_dim_current
= 0;
184 static int bl_dim_target
= 0;
185 static int bl_pwm_counter
= 0;
186 static volatile int bl_cycle_counter
= 0;
187 static enum {DIM_STATE_START
, DIM_STATE_MAIN
} bl_dim_state
= DIM_STATE_START
;
189 static void backlight_isr(void)
194 timer_period
= TIMER_FREQ
/ 1000 * BL_PWM_INTERVAL
/ 1000;
195 switch (bl_dim_state
)
198 case DIM_STATE_START
:
202 if (bl_dim_current
> 0 && bl_dim_current
< BL_PWM_COUNT
)
205 bl_pwm_counter
= bl_dim_current
;
206 timer_period
= timer_period
* bl_pwm_counter
/ BL_PWM_COUNT
;
207 bl_dim_state
= DIM_STATE_MAIN
;
215 if (bl_dim_current
== bl_dim_target
)
221 /* Dim main screen */
224 bl_dim_state
= DIM_STATE_START
;
225 timer_period
= timer_period
* (BL_PWM_COUNT
- bl_pwm_counter
) / BL_PWM_COUNT
;
229 if ((bl_dim_target
> bl_dim_current
) && (bl_cycle_counter
>= fade_in_count
))
232 bl_cycle_counter
= 0;
235 if ((bl_dim_target
< bl_dim_current
) && (bl_cycle_counter
>= fade_out_count
))
238 bl_cycle_counter
= 0;
244 queue_post(&backlight_queue
, BACKLIGHT_UNBOOST_CPU
, 0);
247 bl_timer_active
= false;
250 timer_set_period(timer_period
);
253 static void backlight_switch(void)
255 if (bl_dim_target
> (BL_PWM_COUNT
/2))
258 bl_dim_current
= BL_PWM_COUNT
;
267 static void backlight_release_timer(void)
273 bl_timer_active
= false;
277 static void backlight_dim(int value
)
279 /* protect from extraneous calls with the same target value */
280 if (value
== bl_dim_target
)
283 bl_dim_target
= value
;
288 if (timer_register(0, backlight_release_timer
, 2, 0, backlight_isr
))
291 /* Prevent cpu frequency changes while dimming. */
294 bl_timer_active
= true;
300 void backlight_set_fade_in(int index
)
302 fade_in_count
= backlight_fade_value
[index
];
305 void backlight_set_fade_out(int index
)
307 fade_out_count
= backlight_fade_value
[index
];
309 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
311 static void _backlight_on(void)
313 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
314 if (fade_in_count
> 0)
315 backlight_dim(BL_PWM_COUNT
);
318 bl_dim_target
= bl_dim_current
= BL_PWM_COUNT
;
321 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
322 /* call the enable from here - it takes longer than the disable */
324 __backlight_dim(false);
328 #ifdef HAVE_LCD_SLEEP
329 lcd_sleep_timer
= 0; /* LCD should be awake already */
333 static void _backlight_off(void)
335 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
336 if (fade_out_count
> 0)
340 bl_dim_target
= bl_dim_current
= 0;
343 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
344 __backlight_dim(true);
349 #ifdef HAVE_LCD_SLEEP
350 /* Start LCD sleep countdown */
351 if (lcd_sleep_timeout
< 0)
353 lcd_sleep_timer
= 0; /* Setting == Always */
357 lcd_sleep_timer
= lcd_sleep_timeout
;
361 #ifdef HAVE_REMOTE_LCD
363 static void __remote_backlight_on(void)
365 sim_remote_backlight(100);
368 static void __remote_backlight_off(void)
370 sim_remote_backlight(0);
372 #endif /* SIMULATOR */
373 #endif /* HAVE_REMOTE_LCD */
375 /* Update state of backlight according to timeout setting */
376 static void backlight_update_state(void)
379 if (charger_inserted()
380 #ifdef HAVE_USB_POWER
384 backlight_timer
= backlight_timeout_plugged
;
387 backlight_timer
= backlight_timeout
;
389 /* Backlight == OFF in the setting? */
390 if (backlight_timer
< 0)
392 backlight_timer
= 0; /* Disable the timeout */
393 #ifdef HAS_BUTTON_HOLD
394 if (backlight_on_button_hold
== 2 && button_hold())
395 return; /* Keep on if "On" */
401 #ifdef HAS_BUTTON_HOLD
402 if (backlight_on_button_hold
== 1 && button_hold())
404 /* Keep off if "Off". */
405 backlight_timer
= 0; /* Disable the timeout */
413 #ifdef HAVE_REMOTE_LCD
414 /* Update state of remote backlight according to timeout setting */
415 static void remote_backlight_update_state(void)
418 if (charger_inserted()
419 #ifdef HAVE_USB_POWER
423 remote_backlight_timer
= remote_backlight_timeout_plugged
;
426 remote_backlight_timer
= remote_backlight_timeout
;
428 /* Backlight == OFF in the setting? */
429 if (remote_backlight_timer
< 0)
431 remote_backlight_timer
= 0; /* Disable the timeout */
432 #ifdef HAS_REMOTE_BUTTON_HOLD
433 if (remote_backlight_on_button_hold
== 2 && remote_button_hold())
434 return; /* Keep on if "On" */
436 __remote_backlight_off();
440 #if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
441 if (remote_type() == REMOTETYPE_H300_NONLCD
)
443 backlight_update_state();
448 #ifdef HAS_REMOTE_BUTTON_HOLD
449 if (remote_backlight_on_button_hold
== 1 && remote_button_hold())
451 /* Keep off if "Off". */
452 remote_backlight_timer
= 0; /* Disable the timeout */
456 __remote_backlight_on();
460 #endif /* HAVE_REMOTE_LCD */
462 void backlight_thread(void)
468 queue_wait(&backlight_queue
, &ev
);
471 #ifdef HAVE_REMOTE_LCD
472 case REMOTE_BACKLIGHT_ON
:
473 remote_backlight_update_state();
476 case REMOTE_BACKLIGHT_OFF
:
477 remote_backlight_timer
= 0; /* Disable the timeout */
478 #ifdef HAS_REMOTE_BUTTON_HOLD
479 if (remote_backlight_on_button_hold
== 2 &&
480 remote_button_hold())
481 break; /* Keep on if "On" */
483 __remote_backlight_off();
485 #endif /* HAVE_REMOTE_LCD */
488 backlight_update_state();
492 backlight_timer
= 0; /* Disable the timeout */
493 #ifdef HAS_BUTTON_HOLD
494 if (backlight_on_button_hold
== 2 && button_hold())
495 break; /* Keep on if "On" */
500 #ifdef HAVE_LCD_SLEEP
505 #ifdef HAVE_BUTTON_LIGHT
506 case BUTTON_LIGHT_ON
:
507 _button_backlight_on();
509 case BUTTON_LIGHT_OFF
:
510 _button_backlight_off();
514 #ifdef X5_BACKLIGHT_SHUTDOWN
520 #if defined(HAVE_BACKLIGHT_PWM_FADING) && defined(CPU_COLDFIRE) \
521 && !defined(SIMULATOR)
522 case BACKLIGHT_UNBOOST_CPU
:
527 case SYS_USB_CONNECTED
:
528 /* Tell the USB thread that we are safe */
529 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
530 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
533 case SYS_USB_DISCONNECTED
:
534 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
540 static void backlight_tick(void)
543 static bool charger_was_inserted
= false;
544 bool charger_is_inserted
= charger_inserted()
545 #ifdef HAVE_USB_POWER
550 if( charger_was_inserted
!= charger_is_inserted
)
553 #ifdef HAVE_REMOTE_LCD
554 remote_backlight_on();
557 charger_was_inserted
= charger_is_inserted
;
558 #endif /* CONFIG_CHARGING */
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 #ifdef X5_BACKLIGHT_SHUTDOWN
633 void x5_backlight_shutdown(void)
635 /* Turn on the screen and don't let anyone else mess with it. Called
636 from clean_shutdown in misc.c. */
637 queue_empty(&backlight_queue
);
638 tick_remove_task(backlight_tick
);
639 /* Next time the thread runs, if at all, it will just remove itself. */
640 queue_post(&backlight_queue
, BACKLIGHT_QUIT
, 0);
643 #endif /* X5_BACKLIGHT_SHUTDOWN */
645 void backlight_on(void)
647 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
648 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
651 void backlight_off(void)
653 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
656 /* returns true when the backlight is on OR when it's set to always off */
657 bool is_backlight_on(void)
659 if (backlight_timer
|| backlight_get_current_timeout() <= 0)
665 /* return value in ticks; 0 means always on, <0 means always off */
666 int backlight_get_current_timeout(void)
669 if (charger_inserted()
670 #ifdef HAVE_USB_POWER
674 return backlight_timeout_plugged
;
676 return backlight_timeout
;
678 return backlight_timeout
;
682 void backlight_set_timeout(int index
)
684 if((unsigned)index
>= sizeof(backlight_timeout_value
))
685 /* if given a weird value, use default */
687 backlight_timeout
= HZ
* backlight_timeout_value
[index
];
688 backlight_update_state();
692 void backlight_set_timeout_plugged(int index
)
694 if((unsigned)index
>= sizeof(backlight_timeout_value
))
695 /* if given a weird value, use default */
697 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
698 backlight_update_state();
700 #endif /* CONFIG_CHARGING */
702 #ifdef HAS_BUTTON_HOLD
703 /* Hold button change event handler. */
704 void backlight_hold_changed(bool hold_button
)
706 /* Hold switch overrides all backlight behavior except when
708 /* Queue or freeze */
709 if (hold_button
&& backlight_on_button_hold
== 1)
710 backlight_off(); /* setting == Off */
711 else /* setting == On, Normal, no hold button, or anything else */
715 void backlight_set_on_button_hold(int index
)
717 if ((unsigned)index
>= 3)
718 /* if given a weird value, use default */
721 if (index
== backlight_on_button_hold
)
724 backlight_on_button_hold
= index
;
725 backlight_hold_changed(button_hold());
727 #endif /* HAS_BUTTON_HOLD */
729 #ifdef HAVE_LCD_SLEEP
730 void lcd_set_sleep_after_backlight_off(int index
)
732 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
733 /* if given a weird value, use default */
736 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
738 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
739 /* Timer will be set when bl turns off or bl set to on. */
742 /* Backlight is Off */
743 if (lcd_sleep_timeout
< 0)
744 lcd_sleep_timer
= 1; /* Always - sleep next tick */
746 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
748 #endif /* HAVE_LCD_SLEEP */
750 #ifdef HAVE_REMOTE_LCD
751 void remote_backlight_on(void)
753 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
756 void remote_backlight_off(void)
758 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
761 void remote_backlight_set_timeout(int index
)
763 if((unsigned)index
>= sizeof(backlight_timeout_value
))
764 /* if given a weird value, use default */
766 remote_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
767 remote_backlight_update_state();
771 void remote_backlight_set_timeout_plugged(int index
)
773 if((unsigned)index
>= sizeof(backlight_timeout_value
))
774 /* if given a weird value, use default */
776 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
777 remote_backlight_update_state();
779 #endif /* CONFIG_CHARGING */
781 #ifdef HAS_REMOTE_BUTTON_HOLD
782 /* Remote hold button change event handler. */
783 void remote_backlight_hold_changed(bool rc_hold_button
)
785 /* Hold switch overrides all backlight behavior except when
787 /* Queue or freeze */
788 if (rc_hold_button
&& remote_backlight_on_button_hold
== 1)
789 remote_backlight_off(); /* setting == Off */
790 else /* setting == On, Normal, no hold button, or anything else */
791 remote_backlight_on();
795 void remote_backlight_set_on_button_hold(int index
)
797 if ((unsigned)index
>= 3)
798 /* if given a weird value, use default */
801 if (index
== remote_backlight_on_button_hold
)
804 remote_backlight_on_button_hold
= index
;
805 remote_backlight_hold_changed(remote_button_hold());
807 #endif /* HAS_REMOTE_BUTTON_HOLD */
809 /* return value in ticks; 0 means always on, <0 means always off */
810 int remote_backlight_get_current_timeout(void)
813 if (charger_inserted()
814 #ifdef HAVE_USB_POWER
818 return remote_backlight_timeout_plugged
;
820 return remote_backlight_timeout
;
822 return remote_backlight_timeout
;
826 /* returns true when the backlight is on OR when it's set to always off */
827 bool is_remote_backlight_on(void)
829 if (remote_backlight_timer
!= 0 ||
830 remote_backlight_get_current_timeout() <= 0)
836 #endif /* HAVE_REMOTE_LCD */
838 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
839 void backlight_set_brightness(int val
)
841 if (val
< MIN_BRIGHTNESS_SETTING
)
842 val
= MIN_BRIGHTNESS_SETTING
;
843 else if (val
> MAX_BRIGHTNESS_SETTING
)
844 val
= MAX_BRIGHTNESS_SETTING
;
846 __backlight_set_brightness(val
);
848 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
850 #else /* !defined(HAVE_BACKLIGHT) || defined(BOOTLOADER)
851 -- no backlight, empty dummy functions */
853 #if defined(BOOTLOADER) && defined(HAVE_BACKLIGHT)
854 void backlight_init(void)
856 (void)__backlight_init();
861 void backlight_on(void) {}
862 void backlight_off(void) {}
863 void backlight_set_timeout(int index
) {(void)index
;}
864 bool is_backlight_on(void) {return true;}
865 #ifdef HAVE_REMOTE_LCD
866 void remote_backlight_on(void) {}
867 void remote_backlight_off(void) {}
868 void remote_backlight_set_timeout(int index
) {(void)index
;}
869 bool is_remote_backlight_on(void) {return true;}
870 #endif /* HAVE_REMOTE_LCD */
871 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
872 void backlight_set_brightness(int val
) { (void)val
; }
874 #endif /* defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER) */