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"
40 #if defined(TARGET_TREE) && !defined(SIMULATOR)
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
)
63 /* Basic low-level code that simply switches backlight on or off. Probably
64 * a nice candidate for inclusion in the target/ dir. */
66 static inline void __backlight_on(void)
68 #if CONFIG_BACKLIGHT == BL_RTC
69 /* Enable square wave */
70 rtc_write(0x0a, rtc_read(0x0a) | 0x40);
71 #elif CONFIG_BACKLIGHT == BL_PA14_LO /* Player */
72 and_b(~0x40, &PADRH
); /* drive and set low */
74 #elif CONFIG_BACKLIGHT == BL_PA14_HI /* Ondio */
75 or_b(0x40, &PADRH
); /* drive it high */
79 static inline void __backlight_off(void)
81 #if CONFIG_BACKLIGHT == BL_RTC
82 /* Disable square wave */
83 rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
84 #elif CONFIG_BACKLIGHT == BL_PA14_LO /* Player */
85 and_b(~0x40, &PAIORH
); /* let it float (up) */
86 #elif CONFIG_BACKLIGHT == BL_PA14_HI /* Ondio */
87 and_b(~0x40, &PADRH
); /* drive it low */
91 #endif /* SIMULATOR */
93 #if CONFIG_BACKLIGHT && !defined(BOOTLOADER)
95 const signed char backlight_timeout_value
[19] =
97 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
100 #define BACKLIGHT_ON 1
101 #define BACKLIGHT_OFF 2
102 #define REMOTE_BACKLIGHT_ON 3
103 #define REMOTE_BACKLIGHT_OFF 4
104 #define BACKLIGHT_UNBOOST_CPU 5
105 #ifdef HAVE_LCD_SLEEP
109 static void backlight_thread(void);
110 static long backlight_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
111 #ifdef X5_BACKLIGHT_SHUTDOWN
112 #define BACKLIGHT_QUIT 256
114 static const char backlight_thread_name
[] = "backlight";
115 static struct event_queue backlight_queue
;
117 static int backlight_timer
;
118 static int backlight_timeout
= 5*HZ
;
120 static int backlight_timeout_plugged
= 5*HZ
;
122 #ifdef HAS_BUTTON_HOLD
123 static int backlight_on_button_hold
= 0;
126 #ifdef HAVE_REMOTE_LCD
127 static int remote_backlight_timer
;
128 static int remote_backlight_timeout
= 5*HZ
;
130 static int remote_backlight_timeout_plugged
= 5*HZ
;
132 #ifdef HAS_REMOTE_BUTTON_HOLD
133 static int remote_backlight_on_button_hold
= 0;
137 #ifdef HAVE_LCD_SLEEP
138 const signed char lcd_sleep_timeout_value
[10] =
140 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
142 static int lcd_sleep_timer
;
143 static int lcd_sleep_timeout
= 10*HZ
;
146 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
147 /* backlight fading */
148 #define BL_PWM_INTERVAL 5000 /* Cycle interval in s */
149 #define BL_PWM_COUNT 100
150 static const char backlight_fade_value
[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
151 static int fade_in_count
= 1;
152 static int fade_out_count
= 4;
154 static bool bl_timer_active
= false;
155 static int bl_dim_current
= 0;
156 static int bl_dim_target
= 0;
157 static int bl_pwm_counter
= 0;
158 static volatile int bl_cycle_counter
= 0;
159 static enum {DIM_STATE_START
, DIM_STATE_MAIN
} bl_dim_state
= DIM_STATE_START
;
161 static void backlight_isr(void)
166 timer_period
= TIMER_FREQ
/ 1000 * BL_PWM_INTERVAL
/ 1000;
167 switch (bl_dim_state
)
170 case DIM_STATE_START
:
174 if (bl_dim_current
> 0 && bl_dim_current
< BL_PWM_COUNT
)
177 bl_pwm_counter
= bl_dim_current
;
178 timer_period
= timer_period
* bl_pwm_counter
/ BL_PWM_COUNT
;
179 bl_dim_state
= DIM_STATE_MAIN
;
187 if (bl_dim_current
== bl_dim_target
)
193 /* Dim main screen */
196 bl_dim_state
= DIM_STATE_START
;
197 timer_period
= timer_period
* (BL_PWM_COUNT
- bl_pwm_counter
) / BL_PWM_COUNT
;
201 if ((bl_dim_target
> bl_dim_current
) && (bl_cycle_counter
>= fade_in_count
))
204 bl_cycle_counter
= 0;
207 if ((bl_dim_target
< bl_dim_current
) && (bl_cycle_counter
>= fade_out_count
))
210 bl_cycle_counter
= 0;
216 queue_post(&backlight_queue
, BACKLIGHT_UNBOOST_CPU
, 0);
219 bl_timer_active
= false;
222 timer_set_period(timer_period
);
225 static void backlight_switch(void)
227 if (bl_dim_target
> (BL_PWM_COUNT
/2))
230 bl_dim_current
= BL_PWM_COUNT
;
239 static void backlight_release_timer(void)
245 bl_timer_active
= false;
249 static void backlight_dim(int value
)
251 /* protect from extraneous calls with the same target value */
252 if (value
== bl_dim_target
)
255 bl_dim_target
= value
;
260 if (timer_register(0, backlight_release_timer
, 2, 0, backlight_isr
))
263 /* Prevent cpu frequency changes while dimming. */
266 bl_timer_active
= true;
272 void backlight_set_fade_in(int index
)
274 fade_in_count
= backlight_fade_value
[index
];
277 void backlight_set_fade_out(int index
)
279 fade_out_count
= backlight_fade_value
[index
];
281 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
283 static void _backlight_on(void)
285 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
286 if (fade_in_count
> 0)
287 backlight_dim(BL_PWM_COUNT
);
290 bl_dim_target
= bl_dim_current
= BL_PWM_COUNT
;
293 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
294 /* call the enable from here - it takes longer than the disable */
296 __backlight_dim(false);
300 #ifdef HAVE_LCD_SLEEP
301 lcd_sleep_timer
= 0; /* LCD should be awake already */
305 static void _backlight_off(void)
307 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
308 if (fade_out_count
> 0)
312 bl_dim_target
= bl_dim_current
= 0;
315 #elif defined(HAVE_BACKLIGHT_SET_FADING) && !defined(SIMULATOR)
316 __backlight_dim(true);
321 #ifdef HAVE_LCD_SLEEP
322 /* Start LCD sleep countdown */
323 if (lcd_sleep_timeout
< 0)
325 lcd_sleep_timer
= 0; /* Setting == Always */
329 lcd_sleep_timer
= lcd_sleep_timeout
;
333 #ifdef HAVE_REMOTE_LCD
335 static void __remote_backlight_on(void)
337 sim_remote_backlight(100);
340 static void __remote_backlight_off(void)
342 sim_remote_backlight(0);
344 #endif /* SIMULATOR */
345 #endif /* HAVE_REMOTE_LCD */
347 /* Update state of backlight according to timeout setting */
348 static void backlight_update_state(void)
351 if (charger_inserted()
352 #ifdef HAVE_USB_POWER
356 backlight_timer
= backlight_timeout_plugged
;
359 backlight_timer
= backlight_timeout
;
361 /* Backlight == OFF in the setting? */
362 if (backlight_timer
< 0)
364 backlight_timer
= 0; /* Disable the timeout */
365 #ifdef HAS_BUTTON_HOLD
366 if (backlight_on_button_hold
== 2 && button_hold())
367 return; /* Keep on if "On" */
373 #ifdef HAS_BUTTON_HOLD
374 if (backlight_on_button_hold
== 1 && button_hold())
376 /* Keep off if "Off". */
377 backlight_timer
= 0; /* Disable the timeout */
385 #ifdef HAVE_REMOTE_LCD
386 /* Update state of remote backlight according to timeout setting */
387 static void remote_backlight_update_state(void)
390 if (charger_inserted()
391 #ifdef HAVE_USB_POWER
395 remote_backlight_timer
= remote_backlight_timeout_plugged
;
398 remote_backlight_timer
= remote_backlight_timeout
;
400 /* Backlight == OFF in the setting? */
401 if (remote_backlight_timer
< 0)
403 remote_backlight_timer
= 0; /* Disable the timeout */
404 #ifdef HAS_REMOTE_BUTTON_HOLD
405 if (remote_backlight_on_button_hold
== 2 && remote_button_hold())
406 return; /* Keep on if "On" */
408 __remote_backlight_off();
412 #if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
413 if (remote_type() == REMOTETYPE_H300_NONLCD
)
415 backlight_update_state();
420 #ifdef HAS_REMOTE_BUTTON_HOLD
421 if (remote_backlight_on_button_hold
== 1 && remote_button_hold())
423 /* Keep off if "Off". */
424 remote_backlight_timer
= 0; /* Disable the timeout */
428 __remote_backlight_on();
432 #endif /* HAVE_REMOTE_LCD */
434 void backlight_thread(void)
440 queue_wait(&backlight_queue
, &ev
);
443 #ifdef HAVE_REMOTE_LCD
444 case REMOTE_BACKLIGHT_ON
:
445 remote_backlight_update_state();
448 case REMOTE_BACKLIGHT_OFF
:
449 remote_backlight_timer
= 0; /* Disable the timeout */
450 #ifdef HAS_REMOTE_BUTTON_HOLD
451 if (remote_backlight_on_button_hold
== 2 &&
452 remote_button_hold())
453 break; /* Keep on if "On" */
455 __remote_backlight_off();
457 #endif /* HAVE_REMOTE_LCD */
460 backlight_update_state();
464 backlight_timer
= 0; /* Disable the timeout */
465 #ifdef HAS_BUTTON_HOLD
466 if (backlight_on_button_hold
== 2 && button_hold())
467 break; /* Keep on if "On" */
472 #ifdef HAVE_LCD_SLEEP
478 #ifdef X5_BACKLIGHT_SHUTDOWN
484 #if defined(HAVE_BACKLIGHT_PWM_FADING) && defined(CPU_COLDFIRE) \
485 && !defined(SIMULATOR)
486 case BACKLIGHT_UNBOOST_CPU
:
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
);
504 static void backlight_tick(void)
507 static bool charger_was_inserted
= false;
508 bool charger_is_inserted
= charger_inserted()
509 #ifdef HAVE_USB_POWER
514 if( charger_was_inserted
!= charger_is_inserted
)
517 #ifdef HAVE_REMOTE_LCD
518 remote_backlight_on();
521 charger_was_inserted
= charger_is_inserted
;
522 #endif /* CONFIG_CHARGING */
527 if(backlight_timer
== 0)
532 #ifdef HAVE_LCD_SLEEP
533 else if(lcd_sleep_timer
)
536 if(lcd_sleep_timer
== 0)
538 /* Queue on bl thread or freeze! */
539 queue_post(&backlight_queue
, LCD_SLEEP
, 0);
542 #endif /* HAVE_LCD_SLEEP */
544 #ifdef HAVE_REMOTE_LCD
545 if(remote_backlight_timer
)
547 remote_backlight_timer
--;
548 if(remote_backlight_timer
== 0)
550 remote_backlight_off();
553 #endif /* HAVE_REMOVE_LCD */
556 void backlight_init(void)
558 queue_init(&backlight_queue
, true);
559 queue_set_irq_safe(&backlight_queue
, true);
563 #elif defined(__BACKLIGHT_INIT)
564 /* Remove the __BACKLIGHT_INIT references when __backlight_init is
565 available on all backlighted targets. Take them out of the
566 backlight-target.h files as well */
567 if (__backlight_init())
569 # if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
570 /* If backlight is already on, don't fade in. */
571 bl_dim_current
= BL_PWM_COUNT
;
572 bl_dim_target
= BL_PWM_COUNT
;
575 #elif CONFIG_BACKLIGHT == BL_PA14_LO || CONFIG_BACKLIGHT == BL_PA14_HI
576 PACR1
&= ~0x3000; /* Set PA14 (backlight control) to GPIO */
577 or_b(0x40, &PAIORH
); /* ..and output */
580 #ifdef HAVE_REMOTE_LCD
581 remote_backlight_on();
584 create_thread(backlight_thread
, backlight_stack
,
585 sizeof(backlight_stack
), backlight_thread_name
586 IF_PRIO(, PRIORITY_SYSTEM
)
587 IF_COP(, CPU
, false));
588 tick_add_task(backlight_tick
);
591 #ifdef X5_BACKLIGHT_SHUTDOWN
592 void x5_backlight_shutdown(void)
594 /* Turn on the screen and don't let anyone else mess with it. Called
595 from clean_shutdown in misc.c. */
596 queue_empty(&backlight_queue
);
597 tick_remove_task(backlight_tick
);
598 /* Next time the thread runs, if at all, it will just remove itself. */
599 queue_post(&backlight_queue
, BACKLIGHT_QUIT
, 0);
602 #endif /* X5_BACKLIGHT_SHUTDOWN */
604 void backlight_on(void)
606 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
607 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
610 void backlight_off(void)
612 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
615 /* returns true when the backlight is on OR when it's set to always off */
616 bool is_backlight_on(void)
618 if (backlight_timer
|| backlight_get_current_timeout() <= 0)
624 /* return value in ticks; 0 means always on, <0 means always off */
625 int backlight_get_current_timeout(void)
628 if (charger_inserted()
629 #ifdef HAVE_USB_POWER
633 return backlight_timeout_plugged
;
635 return backlight_timeout
;
637 return backlight_timeout
;
641 void backlight_set_timeout(int index
)
643 if((unsigned)index
>= sizeof(backlight_timeout_value
))
644 /* if given a weird value, use default */
646 backlight_timeout
= HZ
* backlight_timeout_value
[index
];
647 backlight_update_state();
651 void backlight_set_timeout_plugged(int index
)
653 if((unsigned)index
>= sizeof(backlight_timeout_value
))
654 /* if given a weird value, use default */
656 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
657 backlight_update_state();
659 #endif /* CONFIG_CHARGING */
661 #ifdef HAS_BUTTON_HOLD
662 /* Hold button change event handler. */
663 void backlight_hold_changed(bool hold_button
)
665 /* Hold switch overrides all backlight behavior except when
667 /* Queue or freeze */
668 if (hold_button
&& backlight_on_button_hold
== 1)
669 backlight_off(); /* setting == Off */
670 else /* setting == On, Normal, no hold button, or anything else */
674 void backlight_set_on_button_hold(int index
)
676 if ((unsigned)index
>= 3)
677 /* if given a weird value, use default */
680 if (index
== backlight_on_button_hold
)
683 backlight_on_button_hold
= index
;
684 backlight_hold_changed(button_hold());
686 #endif /* HAS_BUTTON_HOLD */
688 #ifdef HAVE_LCD_SLEEP
689 void lcd_set_sleep_after_backlight_off(int index
)
691 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
692 /* if given a weird value, use default */
695 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
697 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
698 /* Timer will be set when bl turns off or bl set to on. */
701 /* Backlight is Off */
702 if (lcd_sleep_timeout
< 0)
703 lcd_sleep_timer
= 1; /* Always - sleep next tick */
705 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
707 #endif /* HAVE_LCD_SLEEP */
709 #ifdef HAVE_REMOTE_LCD
710 void remote_backlight_on(void)
712 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
715 void remote_backlight_off(void)
717 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
720 void remote_backlight_set_timeout(int index
)
722 if((unsigned)index
>= sizeof(backlight_timeout_value
))
723 /* if given a weird value, use default */
725 remote_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
726 remote_backlight_update_state();
730 void remote_backlight_set_timeout_plugged(int index
)
732 if((unsigned)index
>= sizeof(backlight_timeout_value
))
733 /* if given a weird value, use default */
735 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
736 remote_backlight_update_state();
738 #endif /* CONFIG_CHARGING */
740 #ifdef HAS_REMOTE_BUTTON_HOLD
741 /* Remote hold button change event handler. */
742 void remote_backlight_hold_changed(bool rc_hold_button
)
744 /* Hold switch overrides all backlight behavior except when
746 /* Queue or freeze */
747 if (rc_hold_button
&& remote_backlight_on_button_hold
== 1)
748 remote_backlight_off(); /* setting == Off */
749 else /* setting == On, Normal, no hold button, or anything else */
750 remote_backlight_on();
754 void remote_backlight_set_on_button_hold(int index
)
756 if ((unsigned)index
>= 3)
757 /* if given a weird value, use default */
760 if (index
== remote_backlight_on_button_hold
)
763 remote_backlight_on_button_hold
= index
;
764 remote_backlight_hold_changed(remote_button_hold());
766 #endif /* HAS_REMOTE_BUTTON_HOLD */
768 /* return value in ticks; 0 means always on, <0 means always off */
769 int remote_backlight_get_current_timeout(void)
772 if (charger_inserted()
773 #ifdef HAVE_USB_POWER
777 return remote_backlight_timeout_plugged
;
779 return remote_backlight_timeout
;
781 return remote_backlight_timeout
;
785 /* returns true when the backlight is on OR when it's set to always off */
786 bool is_remote_backlight_on(void)
788 if (remote_backlight_timer
!= 0 ||
789 remote_backlight_get_current_timeout() <= 0)
795 #endif /* HAVE_REMOTE_LCD */
797 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
798 void backlight_set_brightness(int val
)
800 if (val
< MIN_BRIGHTNESS_SETTING
)
801 val
= MIN_BRIGHTNESS_SETTING
;
802 else if (val
> MAX_BRIGHTNESS_SETTING
)
803 val
= MAX_BRIGHTNESS_SETTING
;
805 __backlight_set_brightness(val
);
807 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
809 #else /* (CONFIG_BACKLIGHT == 0) || defined(BOOTLOADER)
810 -- no backlight, empty dummy functions */
812 #if defined(BOOTLOADER) && CONFIG_BACKLIGHT
813 void backlight_init(void)
815 #ifdef __BACKLIGHT_INIT
822 void backlight_on(void) {}
823 void backlight_off(void) {}
824 void backlight_set_timeout(int index
) {(void)index
;}
825 bool is_backlight_on(void) {return true;}
826 #ifdef HAVE_REMOTE_LCD
827 void remote_backlight_on(void) {}
828 void remote_backlight_off(void) {}
829 void remote_backlight_set_timeout(int index
) {(void)index
;}
830 bool is_remote_backlight_on(void) {return true;}
831 #endif /* HAVE_REMOTE_LCD */
832 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
833 void backlight_set_brightness(int val
) { (void)val
; }
835 #endif /* CONFIG_BACKLIGHT && !defined(BOOTLOADER) */