Detect C200v2 variant by reading A7, use A5 or A7 to control backlight and buttonligh...
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-c200v2 / backlight-c200v2.c
blob9c236d365318ece28cfd70441ae6a3a3e2dbaded
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Barry Wardell
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 "backlight-target.h"
23 #include "system.h"
24 #include "lcd.h"
25 #include "backlight.h"
26 #include "ascodec-target.h"
27 #include "as3514.h"
29 int buttonlight_is_on = 0;
30 int backlight_is_on = 0;
31 static int backlight_level = 0;
33 /* logarithmic lookup table for brightness s*/
34 static const int brightness_table[MAX_BRIGHTNESS_SETTING+1] = {
35 0, 21, 47, 78, 118, 165, 224, 296, 386, 495, 630, 796, 1000
38 static void _ll_backlight_on(void)
40 if (c200v2_variant == 0) {
41 GPIOA_PIN(5) = 1<<5;
42 } else {
43 GPIOA_PIN(7) = 1<<7;
47 static void _ll_backlight_off(void)
49 if (c200v2_variant == 0) {
50 GPIOA_PIN(5) = 0;
51 } else {
52 GPIOA_PIN(7) = 0;
56 static void _ll_buttonlight_on(void)
58 if (c200v2_variant == 1) {
59 /* Main buttonlight is on A5 */
60 GPIOA_PIN(5) = 1<<5;
61 } else {
62 /* Needed for buttonlight and MicroSD to work at the same time */
63 /* Turn ROD control on, as the OF does */
64 GPIOD_DIR |= (1<<7);
65 SD_MCI_POWER |= (1<<7);
66 GPIOD_PIN(7) = (1<<7);
70 static void _ll_buttonlight_off(void)
72 if (c200v2_variant == 1) {
73 /* Main buttonlight is on A5 */
74 GPIOA_PIN(5) = 0;
75 } else {
76 /* Needed for buttonlight and MicroSD to work at the same time */
77 /* Turn ROD control off, as the OF does */
78 SD_MCI_POWER &= ~(1<<7);
79 GPIOD_PIN(7) = 0;
80 GPIOD_DIR &= ~(1<<7);
84 void _backlight_pwm(int on)
86 if (on) {
87 if (backlight_is_on)
88 _ll_backlight_on();
90 if (buttonlight_is_on)
91 _ll_buttonlight_on();
92 } else {
93 if (backlight_is_on)
94 _ll_backlight_off();
96 if (buttonlight_is_on)
97 _ll_buttonlight_off();
101 bool _backlight_init(void)
103 GPIOA_DIR |= 1<<5;
104 if (c200v2_variant == 1) {
105 /* On this variant A7 is the backlight and
106 * A5 is the buttonlight */
107 GPIOA_DIR |= 1<<7;
109 return true;
112 void _backlight_set_brightness(int brightness)
114 backlight_level = brightness_table[brightness];
116 if (brightness > 0)
117 _backlight_on();
118 else
119 _backlight_off();
122 static void _pwm_on(void)
124 _set_timer2_pwm_ratio(backlight_level);
127 static void _pwm_off(void)
129 if (buttonlight_is_on == 0 && backlight_is_on == 0)
130 _set_timer2_pwm_ratio(0);
133 void _backlight_on(void)
135 if (backlight_is_on == 1) {
136 /* Update pwm ratio in case user changed the brightness */
137 _pwm_on();
138 return;
141 #ifdef HAVE_LCD_ENABLE
142 lcd_enable(true); /* power on lcd + visible display */
143 #endif
144 _ll_backlight_on();
145 _pwm_on();
146 backlight_is_on = 1;
149 void _backlight_off(void)
151 if (backlight_is_on == 0)
152 return;
154 backlight_is_on = 0;
155 _pwm_off();
156 _ll_backlight_off();
157 #ifdef HAVE_LCD_ENABLE
158 lcd_enable(false); /* power off visible display */
159 #endif
162 void _buttonlight_on(void)
164 if (buttonlight_is_on == 1)
165 return;
167 _ll_buttonlight_on();
168 _pwm_on();
169 buttonlight_is_on = 1;
172 void _buttonlight_off(void)
174 if (buttonlight_is_on == 0)
175 return;
177 buttonlight_is_on = 0;
178 _pwm_off();
179 _ll_buttonlight_off();
182 /* vim:set ts=4 sw=4 et: */