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 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
35 #include "pcf50606.h" /* iRiver, iAudio X5 brightness */
38 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
39 #include "lcd.h" /* for lcd_enable() and lcd_sleep() */
41 #ifdef HAVE_REMOTE_LCD
42 #include "lcd-remote.h"
44 #if defined(TARGET_TREE) && !defined(SIMULATOR)
45 #include "backlight-target.h"
49 static inline void __backlight_on(void)
54 static inline void __backlight_off(void)
59 /* Basic low-level code that simply switches backlight on or off. Probably
60 * a nice candidate for inclusion in the target/ dir. */
62 static inline void __backlight_on(void)
64 #if CONFIG_BACKLIGHT == BL_RTC
65 /* Enable square wave */
66 rtc_write(0x0a, rtc_read(0x0a) | 0x40);
67 #elif CONFIG_BACKLIGHT == BL_PA14_LO /* Player */
68 and_b(~0x40, &PADRH
); /* drive and set low */
70 #elif CONFIG_BACKLIGHT == BL_PA14_HI /* Ondio */
71 or_b(0x40, &PADRH
); /* drive it high */
72 #elif CONFIG_BACKLIGHT == BL_GMINI
74 #elif CONFIG_BACKLIGHT==BL_IRIVER_IFP7XX
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 */
88 #elif CONFIG_BACKLIGHT == BL_GMINI
90 #elif CONFIG_BACKLIGHT==BL_IRIVER_IFP7XX
95 #endif /* SIMULATOR */
97 #if defined(CONFIG_BACKLIGHT) && !defined(BOOTLOADER)
99 const signed char backlight_timeout_value
[19] =
101 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
104 #define BACKLIGHT_ON 1
105 #define BACKLIGHT_OFF 2
106 #define REMOTE_BACKLIGHT_ON 3
107 #define REMOTE_BACKLIGHT_OFF 4
108 #define BACKLIGHT_UNBOOST_CPU 5
109 #ifdef HAVE_LCD_SLEEP
113 static void backlight_thread(void);
114 static long backlight_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
115 #ifdef X5_BACKLIGHT_SHUTDOWN
116 #define BACKLIGHT_QUIT 256
117 /* Need to save this for x5 shutdown */
118 struct thread_entry
* backlight_thread_id
;
120 static const char backlight_thread_name
[] = "backlight";
121 static struct event_queue backlight_queue
;
123 static int backlight_timer
;
124 static int backlight_timeout
= 5*HZ
;
125 #ifdef CONFIG_CHARGING
126 static int backlight_timeout_plugged
= 5*HZ
;
128 #ifdef HAS_BUTTON_HOLD
129 static int backlight_on_button_hold
= 0;
132 #ifdef HAVE_REMOTE_LCD
133 static int remote_backlight_timer
;
134 static int remote_backlight_timeout
= 5*HZ
;
135 #ifdef CONFIG_CHARGING
136 static int remote_backlight_timeout_plugged
= 5*HZ
;
138 #ifdef HAS_REMOTE_BUTTON_HOLD
139 static int remote_backlight_on_button_hold
= 0;
143 #ifdef HAVE_LCD_SLEEP
144 const signed char lcd_sleep_timeout_value
[10] =
146 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
148 static int lcd_sleep_timer
;
149 static int lcd_sleep_timeout
= 10*HZ
;
152 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
153 /* backlight fading */
154 #define BL_PWM_INTERVAL 5000 /* Cycle interval in s */
155 #define BL_PWM_COUNT 100
156 static const char backlight_fade_value
[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
157 static int fade_in_count
= 1;
158 static int fade_out_count
= 4;
160 static bool bl_timer_active
= false;
161 static int bl_dim_current
= BL_PWM_COUNT
;
162 static int bl_dim_target
= BL_PWM_COUNT
;
163 static int bl_pwm_counter
= 0;
164 static volatile int bl_cycle_counter
= 0;
165 static enum {DIM_STATE_START
, DIM_STATE_MAIN
} bl_dim_state
= DIM_STATE_START
;
167 static void backlight_isr(void)
172 timer_period
= TIMER_FREQ
/ 1000 * BL_PWM_INTERVAL
/ 1000;
173 switch (bl_dim_state
)
176 case DIM_STATE_START
:
180 if (bl_dim_current
> 0 && bl_dim_current
< BL_PWM_COUNT
)
183 bl_pwm_counter
= bl_dim_current
;
184 timer_period
= timer_period
* bl_pwm_counter
/ BL_PWM_COUNT
;
185 bl_dim_state
= DIM_STATE_MAIN
;
193 if (bl_dim_current
== bl_dim_target
)
199 /* Dim main screen */
202 bl_dim_state
= DIM_STATE_START
;
203 timer_period
= timer_period
* (BL_PWM_COUNT
- bl_pwm_counter
) / BL_PWM_COUNT
;
207 if ((bl_dim_target
> bl_dim_current
) && (bl_cycle_counter
>= fade_in_count
))
210 bl_cycle_counter
= 0;
213 if ((bl_dim_target
< bl_dim_current
) && (bl_cycle_counter
>= fade_out_count
))
216 bl_cycle_counter
= 0;
222 queue_post(&backlight_queue
, BACKLIGHT_UNBOOST_CPU
, NULL
);
225 bl_timer_active
= false;
228 timer_set_period(timer_period
);
231 static void backlight_switch(void)
233 if (bl_dim_target
> (BL_PWM_COUNT
/2))
236 bl_dim_current
= BL_PWM_COUNT
;
245 static void backlight_release_timer(void)
248 cpu_boost_id(false, CPUBOOSTID_BACKLIGHT
);
251 bl_timer_active
= false;
255 static void backlight_dim(int value
)
257 /* protect from extraneous calls with the same target value */
258 if (value
== bl_dim_target
)
261 bl_dim_target
= value
;
266 if (timer_register(0, backlight_release_timer
, 2, 0, backlight_isr
))
269 /* Prevent cpu frequency changes while dimming. */
270 cpu_boost_id(true, CPUBOOSTID_BACKLIGHT
);
272 bl_timer_active
= true;
278 void backlight_set_fade_in(int index
)
280 fade_in_count
= backlight_fade_value
[index
];
283 void backlight_set_fade_out(int index
)
285 fade_out_count
= backlight_fade_value
[index
];
287 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
289 static void _backlight_on(void)
291 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
292 if (fade_in_count
> 0)
293 backlight_dim(BL_PWM_COUNT
);
296 bl_dim_target
= bl_dim_current
= BL_PWM_COUNT
;
302 #ifdef HAVE_LCD_SLEEP
303 lcd_sleep_timer
= 0; /* LCD should be awake already */
307 static void _backlight_off(void)
309 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
310 if (fade_out_count
> 0)
314 bl_dim_target
= bl_dim_current
= 0;
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)
350 #ifdef CONFIG_CHARGING
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)
389 #ifdef CONFIG_CHARGING
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
480 remove_thread(backlight_thread_id
);
484 #if defined(HAVE_BACKLIGHT_PWM_FADING) && defined(CPU_COLDFIRE) \
485 && !defined(SIMULATOR)
486 case BACKLIGHT_UNBOOST_CPU
:
487 cpu_boost_id(false, CPUBOOSTID_BACKLIGHT
);
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)
506 #ifdef CONFIG_CHARGING
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
, NULL
);
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 #ifdef X5_BACKLIGHT_SHUTDOWN
560 backlight_thread_id
=
562 create_thread(backlight_thread
, backlight_stack
,
563 sizeof(backlight_stack
), backlight_thread_name
564 IF_PRIO(, PRIORITY_SYSTEM
));
565 tick_add_task(backlight_tick
);
568 #elif CONFIG_BACKLIGHT == BL_IRIVER_H100
569 or_l(0x00020000, &GPIO1_ENABLE
);
570 or_l(0x00020000, &GPIO1_FUNCTION
);
571 and_l(~0x00020000, &GPIO1_OUT
); /* Start with the backlight ON */
572 #elif CONFIG_BACKLIGHT == BL_IRIVER_H300
573 or_l(0x00020000, &GPIO1_ENABLE
);
574 or_l(0x00020000, &GPIO1_FUNCTION
);
575 or_l(0x00020000, &GPIO1_OUT
); /* Start with the backlight ON */
576 #elif CONFIG_BACKLIGHT == BL_PA14_LO || CONFIG_BACKLIGHT == BL_PA14_HI
577 PACR1
&= ~0x3000; /* Set PA14 (backlight control) to GPIO */
578 or_b(0x40, &PAIORH
); /* ..and output */
579 #elif CONFIG_BACKLIGHT == BL_GMINI
580 P1CON
|= 0x10; /* P1.4 C-MOS output mode */
583 #ifdef HAVE_REMOTE_LCD
584 remote_backlight_on();
588 #ifdef X5_BACKLIGHT_SHUTDOWN
589 void x5_backlight_shutdown(void)
591 /* Turn on the screen and don't let anyone else mess with it. Called
592 from clean_shutdown in misc.c. */
593 queue_empty(&backlight_queue
);
594 tick_remove_task(backlight_tick
);
595 /* Next time the thread runs, if at all, it will just remove itself. */
596 queue_post(&backlight_queue
, BACKLIGHT_QUIT
, NULL
);
599 #endif /* X5_BACKLIGHT_SHUTDOWN */
601 void backlight_on(void)
603 queue_remove_from_head(&backlight_queue
, BACKLIGHT_ON
);
604 queue_post(&backlight_queue
, BACKLIGHT_ON
, NULL
);
607 void backlight_off(void)
609 queue_post(&backlight_queue
, BACKLIGHT_OFF
, NULL
);
612 /* returns true when the backlight is on OR when it's set to always off */
613 bool is_backlight_on(void)
615 if (backlight_timer
|| backlight_get_current_timeout() <= 0)
621 /* return value in ticks; 0 means always on, <0 means always off */
622 int backlight_get_current_timeout(void)
624 #ifdef CONFIG_CHARGING
625 if (charger_inserted()
626 #ifdef HAVE_USB_POWER
630 return backlight_timeout_plugged
;
632 return backlight_timeout
;
634 return backlight_timeout
;
638 void backlight_set_timeout(int index
)
640 if((unsigned)index
>= sizeof(backlight_timeout_value
))
641 /* if given a weird value, use default */
643 backlight_timeout
= HZ
* backlight_timeout_value
[index
];
644 backlight_update_state();
647 #ifdef CONFIG_CHARGING
648 void backlight_set_timeout_plugged(int index
)
650 if((unsigned)index
>= sizeof(backlight_timeout_value
))
651 /* if given a weird value, use default */
653 backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
654 backlight_update_state();
656 #endif /* CONFIG_CHARGING */
658 #ifdef HAS_BUTTON_HOLD
659 /* Hold button change event handler. */
660 void backlight_hold_changed(bool hold_button
)
662 /* Hold switch overrides all backlight behavior except when
664 /* Queue or freeze */
665 if (hold_button
&& backlight_on_button_hold
== 1)
666 backlight_off(); /* setting == Off */
667 else /* setting == On, Normal, no hold button, or anything else */
671 void backlight_set_on_button_hold(int index
)
673 if ((unsigned)index
>= 3)
674 /* if given a weird value, use default */
677 if (index
== backlight_on_button_hold
)
680 backlight_on_button_hold
= index
;
681 backlight_hold_changed(button_hold());
683 #endif /* HAS_BUTTON_HOLD */
685 #ifdef HAVE_LCD_SLEEP
686 void lcd_set_sleep_after_backlight_off(int index
)
688 if ((unsigned)index
>= sizeof(lcd_sleep_timeout_value
))
689 /* if given a weird value, use default */
692 lcd_sleep_timeout
= HZ
* lcd_sleep_timeout_value
[index
];
694 if (backlight_timer
> 0 || backlight_get_current_timeout() == 0)
695 /* Timer will be set when bl turns off or bl set to on. */
698 /* Backlight is Off */
699 if (lcd_sleep_timeout
< 0)
700 lcd_sleep_timer
= 1; /* Always - sleep next tick */
702 lcd_sleep_timer
= lcd_sleep_timeout
; /* Never, other */
704 #endif /* HAVE_LCD_SLEEP */
706 #ifdef HAVE_REMOTE_LCD
707 void remote_backlight_on(void)
709 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_ON
, NULL
);
712 void remote_backlight_off(void)
714 queue_post(&backlight_queue
, REMOTE_BACKLIGHT_OFF
, NULL
);
717 void remote_backlight_set_timeout(int index
)
719 if((unsigned)index
>= sizeof(backlight_timeout_value
))
720 /* if given a weird value, use default */
722 remote_backlight_timeout
= HZ
* backlight_timeout_value
[index
];
723 remote_backlight_update_state();
726 #ifdef CONFIG_CHARGING
727 void remote_backlight_set_timeout_plugged(int index
)
729 if((unsigned)index
>= sizeof(backlight_timeout_value
))
730 /* if given a weird value, use default */
732 remote_backlight_timeout_plugged
= HZ
* backlight_timeout_value
[index
];
733 remote_backlight_update_state();
735 #endif /* CONFIG_CHARGING */
737 #ifdef HAS_REMOTE_BUTTON_HOLD
738 /* Remote hold button change event handler. */
739 void remote_backlight_hold_changed(bool rc_hold_button
)
741 /* Hold switch overrides all backlight behavior except when
743 /* Queue or freeze */
744 if (rc_hold_button
&& remote_backlight_on_button_hold
== 1)
745 remote_backlight_off(); /* setting == Off */
746 else /* setting == On, Normal, no hold button, or anything else */
747 remote_backlight_on();
751 void remote_backlight_set_on_button_hold(int index
)
753 if ((unsigned)index
>= 3)
754 /* if given a weird value, use default */
757 if (index
== remote_backlight_on_button_hold
)
760 remote_backlight_on_button_hold
= index
;
761 remote_backlight_hold_changed(remote_button_hold());
763 #endif /* HAS_REMOTE_BUTTON_HOLD */
765 /* return value in ticks; 0 means always on, <0 means always off */
766 int remote_backlight_get_current_timeout(void)
768 #ifdef CONFIG_CHARGING
769 if (charger_inserted()
770 #ifdef HAVE_USB_POWER
774 return remote_backlight_timeout_plugged
;
776 return remote_backlight_timeout
;
778 return remote_backlight_timeout
;
782 /* returns true when the backlight is on OR when it's set to always off */
783 bool is_remote_backlight_on(void)
785 if (remote_backlight_timer
!= 0 ||
786 remote_backlight_get_current_timeout() <= 0)
792 #endif /* HAVE_REMOTE_LCD */
794 #else /* !defined(CONFIG_BACKLIGHT) || defined(BOOTLOADER)
795 -- no backlight, empty dummy functions */
797 #if defined(BOOTLOADER) && defined(CONFIG_BACKLIGHT)
798 void backlight_init(void)
800 #ifdef IRIVER_H300_SERIES
801 or_l(0x00020000, &GPIO1_OUT
);
802 or_l(0x00020000, &GPIO1_ENABLE
);
803 or_l(0x00020000, &GPIO1_FUNCTION
);
808 void backlight_on(void) {}
809 void backlight_off(void) {}
810 void backlight_set_timeout(int index
) {(void)index
;}
811 bool is_backlight_on(void) {return true;}
812 #ifdef HAVE_REMOTE_LCD
813 void remote_backlight_on(void) {}
814 void remote_backlight_off(void) {}
815 void remote_backlight_set_timeout(int index
) {(void)index
;}
816 bool is_remote_backlight_on(void) {return true;}
818 #endif /* defined(CONFIG_BACKLIGHT) && !defined(BOOTLOADER) */
820 /* TODO: Move low level code to target/ tree. Create
821 __backlight_set_brightness and call from here. */
822 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
823 #if defined(IRIVER_H300_SERIES) || defined(IAUDIO_X5)
824 void backlight_set_brightness(int val
)
827 /* set brightness by changing the PWM
828 * accepts 0..15 but note that 0 and 1 give a black display on H300!
829 * 0 is black on the X5.
832 /* disable IRQs while bitbanging */
833 int old_irq_level
= set_irq_level(HIGHEST_IRQ_LEVEL
);
835 /* Clamp setting to range */
836 if(val
<MIN_BRIGHTNESS_SETTING
)
837 val
=MIN_BRIGHTNESS_SETTING
;
838 else if(val
>MAX_BRIGHTNESS_SETTING
)
839 val
=MAX_BRIGHTNESS_SETTING
;
841 pcf50606_write(0x35, (val
<< 1) | 0x01); /* 512Hz, Enable PWM */
843 /* enable IRQs again */
844 set_irq_level(old_irq_level
);
850 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */