summaryrefslogtreecommitdiff
path: root/firmware/target/arm/imx31/gigabeat-s/backlight-gigabeat-s.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/imx31/gigabeat-s/backlight-gigabeat-s.c')
-rw-r--r--firmware/target/arm/imx31/gigabeat-s/backlight-gigabeat-s.c214
1 files changed, 214 insertions, 0 deletions
diff --git a/firmware/target/arm/imx31/gigabeat-s/backlight-gigabeat-s.c b/firmware/target/arm/imx31/gigabeat-s/backlight-gigabeat-s.c
new file mode 100644
index 0000000000..ec7bf7e8a9
--- /dev/null
+++ b/firmware/target/arm/imx31/gigabeat-s/backlight-gigabeat-s.c
@@ -0,0 +1,214 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "config.h"
22#include "system.h"
23#include "backlight.h"
24#include "mc13783.h"
25#include "backlight-target.h"
26#include "lcd.h"
27
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. */
33static const struct
34{
35 unsigned char md;
36 unsigned char pwm;
37} led_md_pwm_table[] =
38{
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 */
66};
67#endif /* HAVE_BACKLIGHT_BRIGHTNESS */
68
69/* Bits always combined with ramping bits */
70#define MC13783_LED_CONTROL0_BITS \
71 (MC13783_BOOSTEN | MC13783_ABMODE_MONCH_LEDMD1234 | \
72 MC13783_ABREF_400MV)
73
74static struct mutex backlight_mutex; /* Block brightness change while
75 * setting up fading */
76static bool backlight_on_status = true; /* Is on or off? */
77static uint32_t backlight_pwm_bits; /* Final PWM setting for fade-in */
78
79/* Backlight ramping settings */
80static uint32_t led_ramp_mask = MC13783_LEDMDRAMPDOWN | MC13783_LEDMDRAMPUP;
81
82bool _backlight_init(void)
83{
84 mutex_init(&backlight_mutex);
85
86 /* Set default LED register value */
87 mc13783_write(MC13783_LED_CONTROL0,
88 MC13783_LED_CONTROL0_BITS | MC13783_LEDEN);
89
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);
94#else
95 /* Use default PWM */
96 backlight_pwm_bits = mc13783_read(MC13783_LED_CONTROL2) & MC13783_LEDMDDC;
97#endif
98
99 return true;
100}
101
102void backlight_set_fade_out(bool value)
103{
104 if (value)
105 led_ramp_mask |= MC13783_LEDMDRAMPDOWN;
106 else
107 led_ramp_mask &= ~MC13783_LEDMDRAMPDOWN;
108}
109
110void backlight_set_fade_in(bool value)
111{
112 if (value)
113 led_ramp_mask |= MC13783_LEDMDRAMPUP;
114 else
115 led_ramp_mask &= ~MC13783_LEDMDRAMPUP;
116}
117
118void _backlight_on(void)
119{
120 static const char regs[2] =
121 {
122 MC13783_LED_CONTROL0,
123 MC13783_LED_CONTROL2
124 };
125
126 uint32_t data[2];
127
128 mutex_lock(&backlight_mutex);
129
130#ifdef HAVE_LCD_ENABLE
131 lcd_enable(true);
132#endif
133
134 /* Set/clear LEDRAMPUP bit, clear LEDRAMPDOWN bit,
135 * Ensure LED supply is on. */
136 data[0] = MC13783_LED_CONTROL0_BITS | MC13783_LEDEN;
137
138 if (!backlight_on_status)
139 data[0] |= led_ramp_mask & MC13783_LEDMDRAMPUP;
140
141 backlight_on_status = true;
142
143 /* Specify final PWM setting */
144 data[1] = mc13783_read(MC13783_LED_CONTROL2);
145
146 if (data[1] != MC13783_DATA_ERROR)
147 {
148 data[1] &= ~MC13783_LEDMDDC;
149 data[1] |= backlight_pwm_bits;
150
151 /* Write regs within 30us of each other (requires single xfer) */
152 mc13783_write_regset(regs, data, 2);
153 }
154
155 mutex_unlock(&backlight_mutex);
156}
157
158void _backlight_off(void)
159{
160 uint32_t ctrl0 = MC13783_LED_CONTROL0_BITS | MC13783_LEDEN;
161
162 mutex_lock(&backlight_mutex);
163
164 if (backlight_on_status)
165 ctrl0 |= led_ramp_mask & MC13783_LEDMDRAMPDOWN;
166
167 backlight_on_status = false;
168
169 /* Set/clear LEDRAMPDOWN bit, clear LEDRAMPUP bit */
170 mc13783_write(MC13783_LED_CONTROL0, ctrl0);
171
172 /* Wait 100us - 500ms */
173 sleep(HZ/100);
174
175 /* Write final PWM setting */
176 mc13783_write_masked(MC13783_LED_CONTROL2,
177 0 << MC13783_LEDMDDC_POS,
178 MC13783_LEDMDDC);
179
180 mutex_unlock(&backlight_mutex);
181}
182
183#ifdef HAVE_BACKLIGHT_BRIGHTNESS
184/* Assumes that the backlight has been initialized - parameter should
185 * already be range-checked in public interface. */
186void _backlight_set_brightness(int brightness)
187{
188 uint32_t md;
189
190 mutex_lock(&backlight_mutex);
191
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;
195
196 mc13783_write_masked(MC13783_LED_CONTROL2,
197 (md << MC13783_LEDMD_POS) | backlight_pwm_bits,
198 MC13783_LEDMD | MC13783_LEDMDDC);
199
200 mutex_unlock(&backlight_mutex);
201}
202#endif /* HAVE_BACKLIGHT_BRIGHTNESS */
203
204#ifdef HAVE_LCD_SLEEP
205/* Turn off LED supply */
206void _backlight_lcd_sleep(void)
207{
208 mutex_lock(&backlight_mutex);
209
210 mc13783_clear(MC13783_LED_CONTROL0, MC13783_LEDEN);
211
212 mutex_unlock(&backlight_mutex);
213}
214#endif