1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include "backlight.h"
25 #include "backlight-target.h"
28 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
29 /* Table that uses combinations of current level and pwm fraction to get
30 * as many uniquely-visible brightness levels as possible. The lowest current
31 * level for any average current is used even though many combinations give
32 * duplicate values. Current (I) values are in mA. */
37 } led_md_pwm_table
[] =
39 /* I-level PWM(x/15) I-Avg */
40 { 0, 0 }, /* 0 0 0.0 */
41 { 1, 1 }, /* 3 1 0.2 */
42 { 1, 2 }, /* 3 2 0.4 */
43 { 1, 3 }, /* 3 3 0.6 */
44 { 1, 4 }, /* 3 4 0.8 */
45 { 1, 5 }, /* 3 5 1.0 */
46 { 1, 6 }, /* 3 6 1.2 */
47 { 1, 7 }, /* 3 7 1.4 */
48 { 1, 8 }, /* 3 8 1.6 */
49 { 1, 9 }, /* 3 9 1.8 */
50 { 1, 10 }, /* 3 10 2.0 */
51 { 1, 11 }, /* 3 11 2.2 */
52 { 1, 12 }, /* 3 12 2.4 */ /* default */
53 { 1, 13 }, /* 3 13 2.6 */
54 { 1, 14 }, /* 3 14 2.8 */
55 { 1, 15 }, /* 3 15 3.0 */
56 { 2, 9 }, /* 6 9 3.6 */
57 { 2, 10 }, /* 6 10 4.0 */
58 { 2, 11 }, /* 6 11 4.4 */
59 { 2, 12 }, /* 6 12 4.8 */
60 { 2, 13 }, /* 6 13 5.2 */
61 { 2, 14 }, /* 6 14 5.6 */
62 { 2, 15 }, /* 6 15 6.0 */
63 { 3, 11 }, /* 9 11 6.6 */
64 { 3, 12 }, /* 9 12 7.2 */
65 /* Anything higher is just too much */
67 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
69 /* Bits always combined with ramping bits */
70 #define MC13783_LED_CONTROL0_BITS \
71 (MC13783_BOOSTEN | MC13783_ABMODE_MONCH_LEDMD1234 | \
74 static struct mutex backlight_mutex
; /* Block brightness change while
75 * setting up fading */
76 static bool backlight_on_status
= true; /* Is on or off? */
77 static uint32_t backlight_pwm_bits
; /* Final PWM setting for fade-in */
79 /* Backlight ramping settings */
80 static uint32_t led_ramp_mask
= MC13783_LEDMDRAMPDOWN
| MC13783_LEDMDRAMPUP
;
82 bool _backlight_init(void)
84 mutex_init(&backlight_mutex
);
86 /* Set default LED register value */
87 mc13783_write(MC13783_LED_CONTROL0
,
88 MC13783_LED_CONTROL0_BITS
| MC13783_LEDEN
);
90 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
91 /* Our PWM and I-Level is different than retailos (but same apparent
92 * brightness), so init to our default. */
93 _backlight_set_brightness(DEFAULT_BRIGHTNESS_SETTING
);
96 backlight_pwm_bits
= mc13783_read(MC13783_LED_CONTROL2
) & MC13783_LEDMDDC
;
102 void backlight_set_fade_out(bool value
)
105 led_ramp_mask
|= MC13783_LEDMDRAMPDOWN
;
107 led_ramp_mask
&= ~MC13783_LEDMDRAMPDOWN
;
110 void backlight_set_fade_in(bool value
)
113 led_ramp_mask
|= MC13783_LEDMDRAMPUP
;
115 led_ramp_mask
&= ~MC13783_LEDMDRAMPUP
;
118 void _backlight_on(void)
120 static const char regs
[2] =
122 MC13783_LED_CONTROL0
,
128 mutex_lock(&backlight_mutex
);
130 #ifdef HAVE_LCD_ENABLE
134 /* Set/clear LEDRAMPUP bit, clear LEDRAMPDOWN bit,
135 * Ensure LED supply is on. */
136 data
[0] = MC13783_LED_CONTROL0_BITS
| MC13783_LEDEN
;
138 if (!backlight_on_status
)
139 data
[0] |= led_ramp_mask
& MC13783_LEDMDRAMPUP
;
141 backlight_on_status
= true;
143 /* Specify final PWM setting */
144 data
[1] = mc13783_read(MC13783_LED_CONTROL2
);
146 if (data
[1] != MC13783_DATA_ERROR
)
148 data
[1] &= ~MC13783_LEDMDDC
;
149 data
[1] |= backlight_pwm_bits
;
151 /* Write regs within 30us of each other (requires single xfer) */
152 mc13783_write_regset(regs
, data
, 2);
155 mutex_unlock(&backlight_mutex
);
158 void _backlight_off(void)
160 uint32_t ctrl0
= MC13783_LED_CONTROL0_BITS
| MC13783_LEDEN
;
162 mutex_lock(&backlight_mutex
);
164 if (backlight_on_status
)
165 ctrl0
|= led_ramp_mask
& MC13783_LEDMDRAMPDOWN
;
167 backlight_on_status
= false;
169 /* Set/clear LEDRAMPDOWN bit, clear LEDRAMPUP bit */
170 mc13783_write(MC13783_LED_CONTROL0
, ctrl0
);
172 /* Wait 100us - 500ms */
175 /* Write final PWM setting */
176 mc13783_write_masked(MC13783_LED_CONTROL2
,
177 0 << MC13783_LEDMDDC_POS
,
180 mutex_unlock(&backlight_mutex
);
183 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
184 /* Assumes that the backlight has been initialized - parameter should
185 * already be range-checked in public interface. */
186 void _backlight_set_brightness(int brightness
)
190 mutex_lock(&backlight_mutex
);
192 md
= led_md_pwm_table
[brightness
].md
;
193 backlight_pwm_bits
= backlight_on_status
?
194 (led_md_pwm_table
[brightness
].pwm
<< MC13783_LEDMDDC_POS
) : 0;
196 mc13783_write_masked(MC13783_LED_CONTROL2
,
197 (md
<< MC13783_LEDMD_POS
) | backlight_pwm_bits
,
198 MC13783_LEDMD
| MC13783_LEDMDDC
);
200 mutex_unlock(&backlight_mutex
);
202 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
204 #ifdef HAVE_LCD_SLEEP
205 /* Turn off LED supply */
206 void _backlight_lcd_sleep(void)
208 mutex_lock(&backlight_mutex
);
210 mc13783_clear(MC13783_LED_CONTROL0
, MC13783_LEDEN
);
212 mutex_unlock(&backlight_mutex
);