"Mosaic" is in fact spelled "Mosaique". Just change the name for now.
[Rockbox.git] / firmware / backlight.c
blobcbd15ee46ba050dd2451e5acc50c8285a918f533
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include "config.h"
20 #include <stdlib.h>
21 #include "cpu.h"
22 #include "kernel.h"
23 #include "thread.h"
24 #include "i2c.h"
25 #include "debug.h"
26 #include "rtc.h"
27 #include "usb.h"
28 #include "power.h"
29 #include "system.h"
30 #include "button.h"
31 #include "timer.h"
32 #include "backlight.h"
34 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
35 #include "pcf50606.h" /* iRiver, iAudio X5 brightness */
36 #endif
38 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
39 #include "lcd.h" /* for lcd_enable() and lcd_sleep() */
40 #endif
41 #ifdef HAVE_REMOTE_LCD
42 #include "lcd-remote.h"
43 #endif
44 #if defined(TARGET_TREE) && !defined(SIMULATOR)
45 #include "backlight-target.h"
46 #endif
48 #ifdef SIMULATOR
49 static inline void __backlight_on(void)
51 sim_backlight(100);
54 static inline void __backlight_off(void)
56 sim_backlight(0);
58 #else
59 /* Basic low-level code that simply switches backlight on or off. Probably
60 * a nice candidate for inclusion in the target/ dir. */
61 #ifndef TARGET_TREE
62 static inline void __backlight_on(void)
64 #if CONFIG_BACKLIGHT == BL_IRIVER_H100
65 and_l(~0x00020000, &GPIO1_OUT);
66 #elif CONFIG_BACKLIGHT == BL_IRIVER_H300
67 lcd_enable(true);
68 sleep(HZ/100); /* lcd needs time - avoid flashing for dark screens */
69 or_l(0x00020000, &GPIO1_OUT);
70 #elif CONFIG_BACKLIGHT == BL_RTC
71 /* Enable square wave */
72 rtc_write(0x0a, rtc_read(0x0a) | 0x40);
73 #elif CONFIG_BACKLIGHT == BL_PA14_LO /* Player */
74 and_b(~0x40, &PADRH); /* drive and set low */
75 or_b(0x40, &PAIORH);
76 #elif CONFIG_BACKLIGHT == BL_PA14_HI /* Ondio */
77 or_b(0x40, &PADRH); /* drive it high */
78 #elif CONFIG_BACKLIGHT == BL_GMINI
79 P1 |= 0x10;
80 #elif CONFIG_BACKLIGHT == BL_IPOD4G
81 /* brightness full */
82 outl(0x80000000 | (0xff << 16), 0x7000a010);
84 /* set port b bit 3 on */
85 outl(((0x100 | 1) << 3), 0x6000d824);
86 #elif CONFIG_BACKLIGHT==BL_IPODMINI
87 /* set port B03 on */
88 outl(((0x100 | 1) << 3), 0x6000d824);
89 #elif CONFIG_BACKLIGHT==BL_IPODNANO
90 /* set port B03 on */
91 outl(((0x100 | 1) << 3), 0x6000d824);
93 /* set port L07 on */
94 outl(((0x100 | 1) << 7), 0x6000d12c);
95 #elif CONFIG_BACKLIGHT==BL_IPOD3G
96 outl(inl(0xc0001000) | 0x02, 0xc0001000);
97 #elif CONFIG_BACKLIGHT==BL_IRIVER_IFP7XX
98 GPIO3_SET = 1;
99 #endif
102 static inline void __backlight_off(void)
104 #if CONFIG_BACKLIGHT == BL_IRIVER_H100
105 or_l(0x00020000, &GPIO1_OUT);
106 #elif CONFIG_BACKLIGHT == BL_IRIVER_H300
107 and_l(~0x00020000, &GPIO1_OUT);
108 lcd_enable(false);
109 #elif CONFIG_BACKLIGHT == BL_RTC
110 /* Disable square wave */
111 rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
112 #elif CONFIG_BACKLIGHT == BL_PA14_LO /* Player */
113 and_b(~0x40, &PAIORH); /* let it float (up) */
114 #elif CONFIG_BACKLIGHT == BL_PA14_HI /* Ondio */
115 and_b(~0x40, &PADRH); /* drive it low */
116 #elif CONFIG_BACKLIGHT == BL_GMINI
117 P1 &= ~0x10;
118 #elif CONFIG_BACKLIGHT == BL_IPOD4G
119 /* fades backlight off on 4g */
120 outl(inl(0x70000084) & ~0x2000000, 0x70000084);
121 outl(0x80000000, 0x7000a010);
122 #elif CONFIG_BACKLIGHT==BL_IPODNANO
123 /* set port B03 off */
124 outl(((0x100 | 0) << 3), 0x6000d824);
126 /* set port L07 off */
127 outl(((0x100 | 0) << 7), 0x6000d12c);
128 #elif CONFIG_BACKLIGHT==BL_IRIVER_IFP7XX
129 GPIO3_CLR = 1;
130 #elif CONFIG_BACKLIGHT==BL_IPOD3G
131 outl(inl(0xc0001000) & ~0x02, 0xc0001000);
132 #elif CONFIG_BACKLIGHT==BL_IPODMINI
133 /* set port B03 off */
134 outl(((0x100 | 0) << 3), 0x6000d824);
135 #endif
137 #endif
138 #endif /* SIMULATOR */
140 #if defined(CONFIG_BACKLIGHT) && !defined(BOOTLOADER)
142 const signed char backlight_timeout_value[19] =
144 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
147 #define BACKLIGHT_ON 1
148 #define BACKLIGHT_OFF 2
149 #define REMOTE_BACKLIGHT_ON 3
150 #define REMOTE_BACKLIGHT_OFF 4
151 #define BACKLIGHT_UNBOOST_CPU 5
152 #ifdef HAVE_LCD_SLEEP
153 #define LCD_SLEEP 6
154 #endif
156 static void backlight_thread(void);
157 static long backlight_stack[DEFAULT_STACK_SIZE/sizeof(long)];
158 #ifdef X5_BACKLIGHT_SHUTDOWN
159 #define BACKLIGHT_QUIT 256
160 /* Need to save this for x5 shutdown */
161 struct thread_entry* backlight_thread_id;
162 #endif
163 static const char backlight_thread_name[] = "backlight";
164 static struct event_queue backlight_queue;
166 static int backlight_timer;
167 static int backlight_timeout = 5*HZ;
168 #ifdef CONFIG_CHARGING
169 static int backlight_timeout_plugged = 5*HZ;
170 #endif
171 #ifdef HAS_BUTTON_HOLD
172 static int backlight_on_button_hold = 0;
173 #endif
175 #ifdef HAVE_REMOTE_LCD
176 static int remote_backlight_timer;
177 static int remote_backlight_timeout = 5*HZ;
178 #ifdef CONFIG_CHARGING
179 static int remote_backlight_timeout_plugged = 5*HZ;
180 #endif
181 #ifdef HAS_REMOTE_BUTTON_HOLD
182 static int remote_backlight_on_button_hold = 0;
183 #endif
184 #endif
186 #ifdef HAVE_LCD_SLEEP
187 const signed char lcd_sleep_timeout_value[10] =
189 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
191 static int lcd_sleep_timer;
192 static int lcd_sleep_timeout = 10*HZ;
193 #endif
195 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
196 /* backlight fading */
197 #define BL_PWM_INTERVAL 5000 /* Cycle interval in µs */
198 #define BL_PWM_COUNT 100
199 static const char backlight_fade_value[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
200 static int fade_in_count = 1;
201 static int fade_out_count = 4;
203 static bool bl_timer_active = false;
204 static int bl_dim_current = BL_PWM_COUNT;
205 static int bl_dim_target = BL_PWM_COUNT;
206 static int bl_pwm_counter = 0;
207 static volatile int bl_cycle_counter = 0;
208 static enum {DIM_STATE_START, DIM_STATE_MAIN} bl_dim_state = DIM_STATE_START;
210 static void backlight_isr(void)
212 int timer_period;
213 bool idle = false;
215 timer_period = TIMER_FREQ / 1000 * BL_PWM_INTERVAL / 1000;
216 switch (bl_dim_state)
218 /* New cycle */
219 case DIM_STATE_START:
220 bl_pwm_counter = 0;
221 bl_cycle_counter++;
223 if (bl_dim_current > 0 && bl_dim_current < BL_PWM_COUNT)
225 __backlight_on();
226 bl_pwm_counter = bl_dim_current;
227 timer_period = timer_period * bl_pwm_counter / BL_PWM_COUNT;
228 bl_dim_state = DIM_STATE_MAIN;
230 else
232 if (bl_dim_current)
233 __backlight_on();
234 else
235 __backlight_off();
236 if (bl_dim_current == bl_dim_target)
237 idle = true;
240 break ;
242 /* Dim main screen */
243 case DIM_STATE_MAIN:
244 __backlight_off();
245 bl_dim_state = DIM_STATE_START;
246 timer_period = timer_period * (BL_PWM_COUNT - bl_pwm_counter) / BL_PWM_COUNT;
247 break ;
250 if ((bl_dim_target > bl_dim_current) && (bl_cycle_counter >= fade_in_count))
252 bl_dim_current++;
253 bl_cycle_counter = 0;
256 if ((bl_dim_target < bl_dim_current) && (bl_cycle_counter >= fade_out_count))
258 bl_dim_current--;
259 bl_cycle_counter = 0;
262 if (idle)
264 #ifdef CPU_COLDFIRE
265 queue_post(&backlight_queue, BACKLIGHT_UNBOOST_CPU, NULL);
266 #endif
267 timer_unregister();
268 bl_timer_active = false;
270 else
271 timer_set_period(timer_period);
274 static void backlight_switch(void)
276 if (bl_dim_target > (BL_PWM_COUNT/2))
278 __backlight_on();
279 bl_dim_current = BL_PWM_COUNT;
281 else
283 __backlight_off();
284 bl_dim_current = 0;
288 static void backlight_release_timer(void)
290 #ifdef CPU_COLDFIRE
291 cpu_boost(false);
292 #endif
293 timer_unregister();
294 bl_timer_active = false;
295 backlight_switch();
298 static void backlight_dim(int value)
300 /* protect from extraneous calls with the same target value */
301 if (value == bl_dim_target)
302 return;
304 bl_dim_target = value;
306 if (bl_timer_active)
307 return ;
309 if (timer_register(0, backlight_release_timer, 2, 0, backlight_isr))
311 #ifdef CPU_COLDFIRE
312 /* Prevent cpu frequency changes while dimming. */
313 cpu_boost(true);
314 #endif
315 bl_timer_active = true;
317 else
318 backlight_switch();
321 void backlight_set_fade_in(int index)
323 fade_in_count = backlight_fade_value[index];
326 void backlight_set_fade_out(int index)
328 fade_out_count = backlight_fade_value[index];
330 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
332 static void _backlight_on(void)
334 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
335 if (fade_in_count > 0)
336 backlight_dim(BL_PWM_COUNT);
337 else
339 bl_dim_target = bl_dim_current = BL_PWM_COUNT;
340 __backlight_on();
342 #else
343 __backlight_on();
344 #endif
345 #ifdef HAVE_LCD_SLEEP
346 lcd_sleep_timer = 0; /* LCD should be awake already */
347 #endif
350 static void _backlight_off(void)
352 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
353 if (fade_out_count > 0)
354 backlight_dim(0);
355 else
357 bl_dim_target = bl_dim_current = 0;
358 __backlight_off();
360 #else
361 __backlight_off();
362 #endif
364 #ifdef HAVE_LCD_SLEEP
365 /* Start LCD sleep countdown */
366 if (lcd_sleep_timeout < 0)
368 lcd_sleep_timer = 0; /* Setting == Always */
369 lcd_sleep();
371 else
372 lcd_sleep_timer = lcd_sleep_timeout;
373 #endif
376 #ifdef HAVE_REMOTE_LCD
377 #ifdef SIMULATOR
378 static void __remote_backlight_on(void)
380 sim_remote_backlight(100);
383 static void __remote_backlight_off(void)
385 sim_remote_backlight(0);
387 #else
388 #ifndef TARGET_TREE
389 static void __remote_backlight_on(void)
391 #if defined(IRIVER_H300_SERIES)
392 and_l(~0x00000002, &GPIO1_OUT);
393 #else
394 and_l(~0x00000800, &GPIO_OUT);
395 #endif
398 static void __remote_backlight_off(void)
400 #if defined(IRIVER_H300_SERIES)
401 or_l(0x00000002, &GPIO1_OUT);
402 #else
403 or_l(0x00000800, &GPIO_OUT);
404 #endif
406 #endif /* TARGET_TREE */
407 #endif /* SIMULATOR */
408 #endif /* HAVE_REMOTE_LCD */
410 /* Update state of backlight according to timeout setting */
411 static void backlight_update_state(void)
413 #ifdef CONFIG_CHARGING
414 if (charger_inserted()
415 #ifdef HAVE_USB_POWER
416 || usb_powered()
417 #endif
419 backlight_timer = backlight_timeout_plugged;
420 else
421 #endif
422 backlight_timer = backlight_timeout;
424 /* Backlight == OFF in the setting? */
425 if (backlight_timer < 0)
427 backlight_timer = 0; /* Disable the timeout */
428 #ifdef HAS_BUTTON_HOLD
429 if (backlight_on_button_hold == 2 && button_hold())
430 return; /* Keep on if "On" */
431 #endif
432 _backlight_off();
434 else
436 #ifdef HAS_BUTTON_HOLD
437 if (backlight_on_button_hold == 1 && button_hold())
439 /* Keep off if "Off". */
440 backlight_timer = 0; /* Disable the timeout */
441 return;
443 #endif
444 _backlight_on();
448 #ifdef HAVE_REMOTE_LCD
449 /* Update state of remote backlight according to timeout setting */
450 static void remote_backlight_update_state(void)
452 #ifdef CONFIG_CHARGING
453 if (charger_inserted()
454 #ifdef HAVE_USB_POWER
455 || usb_powered()
456 #endif
458 remote_backlight_timer = remote_backlight_timeout_plugged;
459 else
460 #endif
461 remote_backlight_timer = remote_backlight_timeout;
463 /* Backlight == OFF in the setting? */
464 if (remote_backlight_timer < 0)
466 remote_backlight_timer = 0; /* Disable the timeout */
467 #ifdef HAS_REMOTE_BUTTON_HOLD
468 if (remote_backlight_on_button_hold == 2 && remote_button_hold())
469 return; /* Keep on if "On" */
470 #endif
471 __remote_backlight_off();
473 else
475 #if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
476 if (remote_type() == REMOTETYPE_H300_NONLCD)
478 backlight_update_state();
480 else
481 #endif
483 #ifdef HAS_REMOTE_BUTTON_HOLD
484 if (remote_backlight_on_button_hold == 1 && remote_button_hold())
486 /* Keep off if "Off". */
487 remote_backlight_timer = 0; /* Disable the timeout */
488 return;
490 #endif
491 __remote_backlight_on();
495 #endif /* HAVE_REMOTE_LCD */
497 void backlight_thread(void)
499 struct event ev;
501 while(1)
503 queue_wait(&backlight_queue, &ev);
504 switch(ev.id)
506 #ifdef HAVE_REMOTE_LCD
507 case REMOTE_BACKLIGHT_ON:
508 remote_backlight_update_state();
509 break;
511 case REMOTE_BACKLIGHT_OFF:
512 remote_backlight_timer = 0; /* Disable the timeout */
513 #ifdef HAS_REMOTE_BUTTON_HOLD
514 if (remote_backlight_on_button_hold == 2 &&
515 remote_button_hold())
516 break; /* Keep on if "On" */
517 #endif
518 __remote_backlight_off();
519 break;
520 #endif /* HAVE_REMOTE_LCD */
522 case BACKLIGHT_ON:
523 backlight_update_state();
524 break;
526 case BACKLIGHT_OFF:
527 backlight_timer = 0; /* Disable the timeout */
528 #ifdef HAS_BUTTON_HOLD
529 if (backlight_on_button_hold == 2 && button_hold())
530 break; /* Keep on if "On" */
531 #endif
532 _backlight_off();
533 break;
535 #ifdef HAVE_LCD_SLEEP
536 case LCD_SLEEP:
537 lcd_sleep();
538 break;
539 #endif
541 #ifdef X5_BACKLIGHT_SHUTDOWN
542 case BACKLIGHT_QUIT:
543 remove_thread(backlight_thread_id);
544 break;
545 #endif
547 #if defined(HAVE_BACKLIGHT_PWM_FADING) && defined(CPU_COLDFIRE) \
548 && !defined(SIMULATOR)
549 case BACKLIGHT_UNBOOST_CPU:
550 cpu_boost(false);
551 break;
552 #endif
554 case SYS_USB_CONNECTED:
555 /* Tell the USB thread that we are safe */
556 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
557 usb_acknowledge(SYS_USB_CONNECTED_ACK);
558 break;
560 case SYS_USB_DISCONNECTED:
561 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
562 break;
563 } /* end switch */
564 } /* end while */
567 static void backlight_tick(void)
569 #ifdef CONFIG_CHARGING
570 static bool charger_was_inserted = false;
571 bool charger_is_inserted = charger_inserted()
572 #ifdef HAVE_USB_POWER
573 || usb_powered()
574 #endif
577 if( charger_was_inserted != charger_is_inserted )
579 backlight_on();
580 #ifdef HAVE_REMOTE_LCD
581 remote_backlight_on();
582 #endif
584 charger_was_inserted = charger_is_inserted;
585 #endif /* CONFIG_CHARGING */
587 if(backlight_timer)
589 backlight_timer--;
590 if(backlight_timer == 0)
592 backlight_off();
595 #ifdef HAVE_LCD_SLEEP
596 else if(lcd_sleep_timer)
598 lcd_sleep_timer--;
599 if(lcd_sleep_timer == 0)
601 /* Queue on bl thread or freeze! */
602 queue_post(&backlight_queue, LCD_SLEEP, NULL);
605 #endif /* HAVE_LCD_SLEEP */
607 #ifdef HAVE_REMOTE_LCD
608 if(remote_backlight_timer)
610 remote_backlight_timer--;
611 if(remote_backlight_timer == 0)
613 remote_backlight_off();
616 #endif /* HAVE_REMOVE_LCD */
619 void backlight_init(void)
621 queue_init(&backlight_queue, true);
622 #ifdef X5_BACKLIGHT_SHUTDOWN
623 backlight_thread_id =
624 #endif
625 create_thread(backlight_thread, backlight_stack,
626 sizeof(backlight_stack), backlight_thread_name
627 IF_PRIO(, PRIORITY_SYSTEM));
628 tick_add_task(backlight_tick);
629 #ifdef SIMULATOR
630 /* do nothing */
631 #elif CONFIG_BACKLIGHT == BL_IRIVER_H100
632 or_l(0x00020000, &GPIO1_ENABLE);
633 or_l(0x00020000, &GPIO1_FUNCTION);
634 and_l(~0x00020000, &GPIO1_OUT); /* Start with the backlight ON */
635 #elif CONFIG_BACKLIGHT == BL_IRIVER_H300
636 or_l(0x00020000, &GPIO1_ENABLE);
637 or_l(0x00020000, &GPIO1_FUNCTION);
638 or_l(0x00020000, &GPIO1_OUT); /* Start with the backlight ON */
639 #elif CONFIG_BACKLIGHT == BL_PA14_LO || CONFIG_BACKLIGHT == BL_PA14_HI
640 PACR1 &= ~0x3000; /* Set PA14 (backlight control) to GPIO */
641 or_b(0x40, &PAIORH); /* ..and output */
642 #elif CONFIG_BACKLIGHT == BL_GMINI
643 P1CON |= 0x10; /* P1.4 C-MOS output mode */
644 #endif
645 backlight_on();
646 #ifdef HAVE_REMOTE_LCD
647 remote_backlight_on();
648 #endif
651 #ifdef X5_BACKLIGHT_SHUTDOWN
652 void x5_backlight_shutdown(void)
654 /* Turn on the screen and don't let anyone else mess with it. Called
655 from clean_shutdown in misc.c. */
656 queue_empty(&backlight_queue);
657 tick_remove_task(backlight_tick);
658 /* Next time the thread runs, if at all, it will just remove itself. */
659 queue_post(&backlight_queue, BACKLIGHT_QUIT, NULL);
660 __backlight_on();
662 #endif /* X5_BACKLIGHT_SHUTDOWN */
664 void backlight_on(void)
666 queue_post(&backlight_queue, BACKLIGHT_ON, NULL);
669 void backlight_off(void)
671 queue_post(&backlight_queue, BACKLIGHT_OFF, NULL);
674 /* returns true when the backlight is on OR when it's set to always off */
675 bool is_backlight_on(void)
677 if (backlight_timer || backlight_get_current_timeout() <= 0)
678 return true;
679 else
680 return false;
683 /* return value in ticks; 0 means always on, <0 means always off */
684 int backlight_get_current_timeout(void)
686 #ifdef CONFIG_CHARGING
687 if (charger_inserted()
688 #ifdef HAVE_USB_POWER
689 || usb_powered()
690 #endif
692 return backlight_timeout_plugged;
693 else
694 return backlight_timeout;
695 #else
696 return backlight_timeout;
697 #endif
700 void backlight_set_timeout(int index)
702 if((unsigned)index >= sizeof(backlight_timeout_value))
703 /* if given a weird value, use default */
704 index = 6;
705 backlight_timeout = HZ * backlight_timeout_value[index];
706 backlight_update_state();
709 #ifdef CONFIG_CHARGING
710 void backlight_set_timeout_plugged(int index)
712 if((unsigned)index >= sizeof(backlight_timeout_value))
713 /* if given a weird value, use default */
714 index = 6;
715 backlight_timeout_plugged = HZ * backlight_timeout_value[index];
716 backlight_update_state();
718 #endif /* CONFIG_CHARGING */
720 #ifdef HAS_BUTTON_HOLD
721 /* Hold button change event handler. */
722 void backlight_hold_changed(bool hold_button)
724 /* Hold switch overrides all backlight behavior except when
725 set to "Normal" */
726 /* Queue or freeze */
727 if (hold_button && backlight_on_button_hold == 1)
728 backlight_off(); /* setting == Off */
729 else /* setting == On, Normal, no hold button, or anything else */
730 backlight_on();
733 void backlight_set_on_button_hold(int index)
735 if ((unsigned)index >= 3)
736 /* if given a weird value, use default */
737 index = 0;
739 if (index == backlight_on_button_hold)
740 return;
742 backlight_on_button_hold = index;
743 backlight_hold_changed(button_hold());
745 #endif /* HAS_BUTTON_HOLD */
747 #ifdef HAVE_LCD_SLEEP
748 void lcd_set_sleep_after_backlight_off(int index)
750 if ((unsigned)index >= sizeof(lcd_sleep_timeout_value))
751 /* if given a weird value, use default */
752 index = 3;
754 lcd_sleep_timeout = HZ * lcd_sleep_timeout_value[index];
756 if (backlight_timer > 0 || backlight_get_current_timeout() == 0)
757 /* Timer will be set when bl turns off or bl set to on. */
758 return;
760 /* Backlight is Off */
761 if (lcd_sleep_timeout < 0)
762 lcd_sleep_timer = 1; /* Always - sleep next tick */
763 else
764 lcd_sleep_timer = lcd_sleep_timeout; /* Never, other */
766 #endif /* HAVE_LCD_SLEEP */
768 #ifdef HAVE_REMOTE_LCD
769 void remote_backlight_on(void)
771 queue_post(&backlight_queue, REMOTE_BACKLIGHT_ON, NULL);
774 void remote_backlight_off(void)
776 queue_post(&backlight_queue, REMOTE_BACKLIGHT_OFF, NULL);
779 void remote_backlight_set_timeout(int index)
781 if((unsigned)index >= sizeof(backlight_timeout_value))
782 /* if given a weird value, use default */
783 index=6;
784 remote_backlight_timeout = HZ * backlight_timeout_value[index];
785 remote_backlight_update_state();
788 #ifdef CONFIG_CHARGING
789 void remote_backlight_set_timeout_plugged(int index)
791 if((unsigned)index >= sizeof(backlight_timeout_value))
792 /* if given a weird value, use default */
793 index=6;
794 remote_backlight_timeout_plugged = HZ * backlight_timeout_value[index];
795 remote_backlight_update_state();
797 #endif /* CONFIG_CHARGING */
799 #ifdef HAS_REMOTE_BUTTON_HOLD
800 /* Remote hold button change event handler. */
801 void remote_backlight_hold_changed(bool rc_hold_button)
803 /* Hold switch overrides all backlight behavior except when
804 set to "Normal" */
805 /* Queue or freeze */
806 if (rc_hold_button && remote_backlight_on_button_hold == 1)
807 remote_backlight_off(); /* setting == Off */
808 else /* setting == On, Normal, no hold button, or anything else */
809 remote_backlight_on();
813 void remote_backlight_set_on_button_hold(int index)
815 if ((unsigned)index >= 3)
816 /* if given a weird value, use default */
817 index = 0;
819 if (index == remote_backlight_on_button_hold)
820 return;
822 remote_backlight_on_button_hold = index;
823 remote_backlight_hold_changed(remote_button_hold());
825 #endif /* HAS_REMOTE_BUTTON_HOLD */
827 /* return value in ticks; 0 means always on, <0 means always off */
828 int remote_backlight_get_current_timeout(void)
830 #ifdef CONFIG_CHARGING
831 if (charger_inserted()
832 #ifdef HAVE_USB_POWER
833 || usb_powered()
834 #endif
836 return remote_backlight_timeout_plugged;
837 else
838 return remote_backlight_timeout;
839 #else
840 return remote_backlight_timeout;
841 #endif
844 /* returns true when the backlight is on OR when it's set to always off */
845 bool is_remote_backlight_on(void)
847 if (remote_backlight_timer != 0 ||
848 remote_backlight_get_current_timeout() <= 0)
849 return true;
850 else
851 return false;
854 #endif /* HAVE_REMOTE_LCD */
856 #else /* !defined(CONFIG_BACKLIGHT) || defined(BOOTLOADER)
857 -- no backlight, empty dummy functions */
859 #if defined(BOOTLOADER) && defined(CONFIG_BACKLIGHT)
860 void backlight_init(void)
862 #ifdef IRIVER_H300_SERIES
863 or_l(0x00020000, &GPIO1_OUT);
864 or_l(0x00020000, &GPIO1_ENABLE);
865 or_l(0x00020000, &GPIO1_FUNCTION);
866 #endif
868 #endif
870 void backlight_on(void) {}
871 void backlight_off(void) {}
872 void backlight_set_timeout(int index) {(void)index;}
873 bool is_backlight_on(void) {return true;}
874 #ifdef HAVE_REMOTE_LCD
875 void remote_backlight_on(void) {}
876 void remote_backlight_off(void) {}
877 void remote_backlight_set_timeout(int index) {(void)index;}
878 bool is_remote_backlight_on(void) {return true;}
879 #endif
880 #endif /* defined(CONFIG_BACKLIGHT) && !defined(BOOTLOADER) */
882 /* TODO: Move low level code to target/ tree. Create
883 __backlight_set_brightness and call from here. */
884 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
885 #if defined(IRIVER_H300_SERIES) || defined(IAUDIO_X5)
886 void backlight_set_brightness(int val)
888 #ifndef SIMULATOR
889 /* set brightness by changing the PWM
890 * accepts 0..15 but note that 0 and 1 give a black display on H300!
891 * 0 is black on the X5.
894 /* disable IRQs while bitbanging */
895 int old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
897 /* Clamp setting to range */
898 if(val<MIN_BRIGHTNESS_SETTING)
899 val=MIN_BRIGHTNESS_SETTING;
900 else if(val>MAX_BRIGHTNESS_SETTING)
901 val=MAX_BRIGHTNESS_SETTING;
903 pcf50606_write(0x35, (val << 1) | 0x01); /* 512Hz, Enable PWM */
905 /* enable IRQs again */
906 set_irq_level(old_irq_level);
907 #else
908 val=0;
909 #endif
911 #endif
912 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */