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);
562 #elif defined(__BACKLIGHT_INIT)
563 /* Remove the __BACKLIGHT_INIT references when __backlight_init is
564 available on all backlighted targets. Take them out of the
565 backlight-target.h files as well */
566 if (__backlight_init())
568 # if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
569 /* If backlight is already on, don't fade in. */
570 bl_dim_current
= BL_PWM_COUNT
;
571 bl_dim_target
= BL_PWM_COUNT
;
574 #elif CONFIG_BACKLIGHT == BL_PA14_LO || CONFIG_BACKLIGHT == BL_PA14_HI
575 PACR1
&= ~0x3000; /* Set PA14 (backlight control) to GPIO */
576 or_b(0x40, &PAIORH
); /* ..and output */
579 #ifdef HAVE_REMOTE_LCD
580 remote_backlight_on();
583 create_thread(backlight_thread
, backlight_stack
,
584 sizeof(backlight_stack
), backlight_thread_name
585 IF_PRIO(, PRIORITY_SYSTEM
)
586 IF_COP(, CPU
, false));
587 tick_add_task(backlight_tick
);
590 #ifdef X5_BACKLIGHT_SHUTDOWN
591 void x5_backlight_shutdown(void)
593 /* Turn on the screen and don't let anyone else mess with it. Called
594 from clean_shutdown in misc.c. */
595 queue_empty(&backlight_queue
);
596 tick_remove_task(backlight_tick
);
597 /* Next time the thread runs, if at all, it will just remove itself. */
598 queue_post(&backlight_queue
, BACKLIGHT_QUIT
, 0);
601 #endif /* X5_BACKLIGHT_SHUTDOWN */
603 void backlight_on(void)
605 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
606 queue_post(&backlight_queue
, BACKLIGHT_ON
, 0);
609 void backlight_off(void)
611 queue_post(&backlight_queue
, BACKLIGHT_OFF
, 0);
614 /* returns true when the backlight is on OR when it's set to always off */
615 bool is_backlight_on(void)
617 if (backlight_timer
|| backlight_get_current_timeout() <= 0)
623 /* return value in ticks; 0 means always on, <0 means always off */
624 int backlight_get_current_timeout(void)
627 if (charger_inserted()
628 #ifdef HAVE_USB_POWER
632 return backlight_timeout_plugged
;
634 return backlight_timeout
;
636 return backlight_timeout
;
640 void backlight_set_timeout(int index
)
642 if((unsigned)index
>= sizeof(backlight_timeout_value
))
643 /* if given a weird value, use default */
645 backlight_timeout
= HZ
* backlight_timeout_value
[index
];
646 backlight_update_state();
650 void backlight_set_timeout_plugged(int index
)
652 if((unsigned)index
>= sizeof(backlight_timeout_value
))
653 /* if given a weird value, use default */
655 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
656 backlight_update_state();
658 #endif /* CONFIG_CHARGING */
660 #ifdef HAS_BUTTON_HOLD
661 /* Hold button change event handler. */
662 void backlight_hold_changed(bool hold_button
)
664 /* Hold switch overrides all backlight behavior except when
666 /* Queue or freeze */
667 if (hold_button
&& backlight_on_button_hold
== 1)
668 backlight_off(); /* setting == Off */
669 else /* setting == On, Normal, no hold button, or anything else */
673 void backlight_set_on_button_hold(int index
)
675 if ((unsigned)index
>= 3)
676 /* if given a weird value, use default */
679 if (index
== backlight_on_button_hold
)
682 backlight_on_button_hold
= index
;
683 backlight_hold_changed(button_hold());
685 #endif /* HAS_BUTTON_HOLD */
687 #ifdef HAVE_LCD_SLEEP
688 void lcd_set_sleep_after_backlight_off(int index
)
690 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
691 /* if given a weird value, use default */
694 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
696 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
697 /* Timer will be set when bl turns off or bl set to on. */
700 /* Backlight is Off */
701 if (lcd_sleep_timeout
< 0)
702 lcd_sleep_timer
= 1; /* Always - sleep next tick */
704 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
706 #endif /* HAVE_LCD_SLEEP */
708 #ifdef HAVE_REMOTE_LCD
709 void remote_backlight_on(void)
711 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, 0);
714 void remote_backlight_off(void)
716 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, 0);
719 void remote_backlight_set_timeout(int index
)
721 if((unsigned)index
>= sizeof(backlight_timeout_value
))
722 /* if given a weird value, use default */
724 remote_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
725 remote_backlight_update_state();
729 void remote_backlight_set_timeout_plugged(int index
)
731 if((unsigned)index
>= sizeof(backlight_timeout_value
))
732 /* if given a weird value, use default */
734 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
735 remote_backlight_update_state();
737 #endif /* CONFIG_CHARGING */
739 #ifdef HAS_REMOTE_BUTTON_HOLD
740 /* Remote hold button change event handler. */
741 void remote_backlight_hold_changed(bool rc_hold_button
)
743 /* Hold switch overrides all backlight behavior except when
745 /* Queue or freeze */
746 if (rc_hold_button
&& remote_backlight_on_button_hold
== 1)
747 remote_backlight_off(); /* setting == Off */
748 else /* setting == On, Normal, no hold button, or anything else */
749 remote_backlight_on();
753 void remote_backlight_set_on_button_hold(int index
)
755 if ((unsigned)index
>= 3)
756 /* if given a weird value, use default */
759 if (index
== remote_backlight_on_button_hold
)
762 remote_backlight_on_button_hold
= index
;
763 remote_backlight_hold_changed(remote_button_hold());
765 #endif /* HAS_REMOTE_BUTTON_HOLD */
767 /* return value in ticks; 0 means always on, <0 means always off */
768 int remote_backlight_get_current_timeout(void)
771 if (charger_inserted()
772 #ifdef HAVE_USB_POWER
776 return remote_backlight_timeout_plugged
;
778 return remote_backlight_timeout
;
780 return remote_backlight_timeout
;
784 /* returns true when the backlight is on OR when it's set to always off */
785 bool is_remote_backlight_on(void)
787 if (remote_backlight_timer
!= 0 ||
788 remote_backlight_get_current_timeout() <= 0)
794 #endif /* HAVE_REMOTE_LCD */
796 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
797 void backlight_set_brightness(int val
)
799 if (val
< MIN_BRIGHTNESS_SETTING
)
800 val
= MIN_BRIGHTNESS_SETTING
;
801 else if (val
> MAX_BRIGHTNESS_SETTING
)
802 val
= MAX_BRIGHTNESS_SETTING
;
804 __backlight_set_brightness(val
);
806 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
808 #else /* (CONFIG_BACKLIGHT == 0) || defined(BOOTLOADER)
809 -- no backlight, empty dummy functions */
811 #if defined(BOOTLOADER) && CONFIG_BACKLIGHT
812 void backlight_init(void)
814 #ifdef __BACKLIGHT_INIT
821 void backlight_on(void) {}
822 void backlight_off(void) {}
823 void backlight_set_timeout(int index
) {(void)index
;}
824 bool is_backlight_on(void) {return true;}
825 #ifdef HAVE_REMOTE_LCD
826 void remote_backlight_on(void) {}
827 void remote_backlight_off(void) {}
828 void remote_backlight_set_timeout(int index
) {(void)index
;}
829 bool is_remote_backlight_on(void) {return true;}
830 #endif /* HAVE_REMOTE_LCD */
831 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
832 void backlight_set_brightness(int val
) { (void)val
; }
834 #endif /* CONFIG_BACKLIGHT && !defined(BOOTLOADER) */