Gigabeat S: Reclaim about 800K of memory that was laying unused. Get rid of DEVBSS_AT...
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / backlight-gigabeat-s.c
blob95f894bbce307f05a9025328a075fd0dbcd6b329
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
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"
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. */
33 static const struct
35 unsigned char md;
36 unsigned char pwm;
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 | \
72 MC13783_ABREF_400MV)
74 static bool backlight_on_status = true; /* Is on or off? */
75 static uint32_t backlight_pwm_bits; /* Final PWM setting for fade-in */
77 /* Backlight ramping settings */
78 static uint32_t led_ramp_mask = MC13783_LEDMDRAMPDOWN | MC13783_LEDMDRAMPUP;
80 bool _backlight_init(void)
82 /* Set default LED register value */
83 mc13783_write(MC13783_LED_CONTROL0,
84 MC13783_LED_CONTROL0_BITS | MC13783_LEDEN);
86 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
87 /* Our PWM and I-Level is different than retailos (but same apparent
88 * brightness), so init to our default. */
89 _backlight_set_brightness(DEFAULT_BRIGHTNESS_SETTING);
90 #else
91 /* Use default PWM */
92 backlight_pwm_bits = mc13783_read(MC13783_LED_CONTROL2) & MC13783_LEDMDDC;
93 #endif
95 return true;
98 void backlight_set_fade_out(bool value)
100 if (value)
101 led_ramp_mask |= MC13783_LEDMDRAMPDOWN;
102 else
103 led_ramp_mask &= ~MC13783_LEDMDRAMPDOWN;
106 void backlight_set_fade_in(bool value)
108 if (value)
109 led_ramp_mask |= MC13783_LEDMDRAMPUP;
110 else
111 led_ramp_mask &= ~MC13783_LEDMDRAMPUP;
114 void _backlight_on(void)
116 static const char regs[2] =
118 MC13783_LED_CONTROL0,
119 MC13783_LED_CONTROL2
122 uint32_t data[2];
124 #ifdef HAVE_LCD_ENABLE
125 lcd_enable(true);
126 #endif
128 /* Set/clear LEDRAMPUP bit, clear LEDRAMPDOWN bit,
129 * Ensure LED supply is on. */
130 data[0] = MC13783_LED_CONTROL0_BITS | MC13783_LEDEN;
132 if (!backlight_on_status)
133 data[0] |= led_ramp_mask & MC13783_LEDMDRAMPUP;
135 backlight_on_status = true;
137 /* Specify final PWM setting */
138 data[1] = mc13783_read(MC13783_LED_CONTROL2);
140 if (data[1] != MC13783_DATA_ERROR)
142 data[1] &= ~MC13783_LEDMDDC;
143 data[1] |= backlight_pwm_bits;
145 /* Write regs within 30us of each other (requires single xfer) */
146 mc13783_write_regs(regs, data, 2);
150 void _backlight_off(void)
152 uint32_t ctrl0 = MC13783_LED_CONTROL0_BITS | MC13783_LEDEN;
154 if (backlight_on_status)
155 ctrl0 |= led_ramp_mask & MC13783_LEDMDRAMPDOWN;
157 backlight_on_status = false;
159 /* Set/clear LEDRAMPDOWN bit, clear LEDRAMPUP bit */
160 mc13783_write(MC13783_LED_CONTROL0, ctrl0);
162 /* Wait 100us - 500ms */
163 sleep(HZ/100);
165 /* Write final PWM setting */
166 mc13783_write_masked(MC13783_LED_CONTROL2,
167 0 << MC13783_LEDMDDC_POS,
168 MC13783_LEDMDDC);
171 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
172 /* Assumes that the backlight has been initialized - parameter should
173 * already be range-checked in public interface. */
174 void _backlight_set_brightness(int brightness)
176 uint32_t md = led_md_pwm_table[brightness].md;
177 backlight_pwm_bits = backlight_on_status ?
178 (led_md_pwm_table[brightness].pwm << MC13783_LEDMDDC_POS) : 0;
180 mc13783_write_masked(MC13783_LED_CONTROL2,
181 (md << MC13783_LEDMD_POS) | backlight_pwm_bits,
182 MC13783_LEDMD | MC13783_LEDMDDC);
184 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
186 #ifdef HAVE_LCD_SLEEP
187 /* Turn off LED supply */
188 void _backlight_lcd_sleep(void)
190 mc13783_clear(MC13783_LED_CONTROL0, MC13783_LEDEN);
192 #endif