Gigabeat S: Try to save some power. Implement lcd_enable and turn off LCD DMA channel...
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / backlight-imx31.c
blob8f75b72f58767df1ef3dcae46cc78ef83c79a1fb
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 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);
94 #else
95 /* Use default PWM */
96 backlight_pwm_bits = mc13783_read(MC13783_LED_CONTROL2) & MC13783_LEDMDDC;
97 #endif
99 return true;
102 void backlight_set_fade_out(bool value)
104 if (value)
105 led_ramp_mask |= MC13783_LEDMDRAMPDOWN;
106 else
107 led_ramp_mask &= ~MC13783_LEDMDRAMPDOWN;
110 void backlight_set_fade_in(bool value)
112 if (value)
113 led_ramp_mask |= MC13783_LEDMDRAMPUP;
114 else
115 led_ramp_mask &= ~MC13783_LEDMDRAMPUP;
118 void _backlight_on(void)
120 static const char regs[2] =
122 MC13783_LED_CONTROL0,
123 MC13783_LED_CONTROL2
126 uint32_t data[2];
128 mutex_lock(&backlight_mutex);
130 #ifdef HAVE_LCD_SLEEP
131 backlight_lcd_sleep_countdown(false); /* stop counter */
132 #endif
133 #ifdef HAVE_LCD_ENABLE
134 lcd_enable(true);
135 #endif
137 /* Set/clear LEDRAMPUP bit, clear LEDRAMPDOWN bit,
138 * Ensure LED supply is on. */
139 data[0] = MC13783_LED_CONTROL0_BITS | MC13783_LEDEN;
141 if (!backlight_on_status)
142 data[0] |= led_ramp_mask & MC13783_LEDMDRAMPUP;
144 backlight_on_status = true;
146 /* Specify final PWM setting */
147 data[1] = mc13783_read(MC13783_LED_CONTROL2);
149 if (data[1] != MC13783_DATA_ERROR)
151 data[1] &= ~MC13783_LEDMDDC;
152 data[1] |= backlight_pwm_bits;
154 /* Write regs within 30us of each other (requires single xfer) */
155 mc13783_write_regset(regs, data, 2);
158 mutex_unlock(&backlight_mutex);
161 void _backlight_off(void)
163 uint32_t ctrl0 = MC13783_LED_CONTROL0_BITS | MC13783_LEDEN;
165 mutex_lock(&backlight_mutex);
167 if (backlight_on_status)
168 ctrl0 |= led_ramp_mask & MC13783_LEDMDRAMPDOWN;
170 backlight_on_status = false;
172 /* Set/clear LEDRAMPDOWN bit, clear LEDRAMPUP bit */
173 mc13783_write(MC13783_LED_CONTROL0, ctrl0);
175 /* Wait 100us - 500ms */
176 sleep(HZ/100);
178 /* Write final PWM setting */
179 mc13783_write_masked(MC13783_LED_CONTROL2, MC13783_LEDMDDCw(0),
180 MC13783_LEDMDDC);
182 #ifdef HAVE_LCD_SLEEP
183 /* Disable lcd after fade completes (when lcd_sleep timeout expires) */
184 backlight_lcd_sleep_countdown(true); /* start countdown */
185 #endif
187 mutex_unlock(&backlight_mutex);
190 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
191 /* Assumes that the backlight has been initialized - parameter should
192 * already be range-checked in public interface. */
193 void _backlight_set_brightness(int brightness)
195 uint32_t md;
197 mutex_lock(&backlight_mutex);
199 md = led_md_pwm_table[brightness].md;
200 backlight_pwm_bits = backlight_on_status ?
201 MC13783_LEDMDDCw(led_md_pwm_table[brightness].pwm) : 0;
203 mc13783_write_masked(MC13783_LED_CONTROL2,
204 MC13783_LEDMDw(md) | backlight_pwm_bits,
205 MC13783_LEDMD | MC13783_LEDMDDC);
207 mutex_unlock(&backlight_mutex);
209 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
211 #ifdef HAVE_LCD_SLEEP
212 /* Turn off LED supply */
213 void _backlight_lcd_sleep(void)
215 mutex_lock(&backlight_mutex);
217 mc13783_clear(MC13783_LED_CONTROL0, MC13783_LEDEN);
219 mutex_unlock(&backlight_mutex);
221 #endif