summaryrefslogtreecommitdiff
path: root/firmware/target/arm/s3c2440/gigabeat-fx/backlight-meg-fx.c
blob: ee8801f99b4716e78d0f474e299fe3cebb170247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "cpu.h"
#include "system.h"
#include "backlight-target.h"
#include "backlight.h"
#include "lcd.h"
#include "sc606-meg-fx.h"
#include "power.h"

#define BUTTONLIGHT_MENU (SC606_LED_B1)
#define BUTTONLIGHT_ALL  (SC606_LED_B1 | SC606_LED_B2 | SC606_LED_C1 | SC606_LED_C2)

#ifndef BOOTLOADER
static void led_control_service(void);

static enum sc606_states
{
    SC606_CONTROL_IDLE,
    SC606_CONTROL_A12,
    SC606_CONTROL_B12,
    SC606_CONTROL_C12,
    SC606_CONTROL_CONF
} sc606_control;
#endif /* BOOTLOADER */

static enum backlight_states
{
    BACKLIGHT_CONTROL_IDLE,
    BACKLIGHT_CONTROL_OFF,
    BACKLIGHT_CONTROL_ON,
    BACKLIGHT_CONTROL_SET,
    BACKLIGHT_CONTROL_FADE
} backlight_control;

enum buttonlight_states
{
    BUTTONLIGHT_CONTROL_IDLE,
    BUTTONLIGHT_CONTROL_OFF,
    BUTTONLIGHT_CONTROL_ON,
    BUTTONLIGHT_CONTROL_SET,
    BUTTONLIGHT_CONTROL_FADE,
} buttonlight_control;


static unsigned char backlight_brightness;
static unsigned char buttonlight_brightness;
static unsigned char backlight_target;
static unsigned char buttonlight_target;

static unsigned short buttonlight_trigger_now;

/* Assumes that the backlight has been initialized */
void _backlight_set_brightness(int brightness)
{
    /* stop the interrupt from messing us up */
    backlight_control = BACKLIGHT_CONTROL_IDLE;
    backlight_brightness = brightness;
    backlight_control = BACKLIGHT_CONTROL_SET;
}

/* only works if the buttonlight mode is set to triggered mode */
void __buttonlight_trigger(void)
{
    buttonlight_trigger_now = 1;
}

/* map the mode from the command into the state machine entries */
void __buttonlight_mode(enum buttonlight_mode mode)
{
    /* choose stop to setup mode */
    buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;

    /* which mode to use */
    switch (mode)
    {
        case BUTTONLIGHT_OFF:
        buttonlight_control = BUTTONLIGHT_CONTROL_OFF;
        break;

        case BUTTONLIGHT_ON:
        buttonlight_control = BUTTONLIGHT_CONTROL_ON;
        break;
        
        case BUTTONLIGHT_FOLLOW:
        buttonlight_control = BUTTONLIGHT_CONTROL_FADE;
        break;

        default:
        return; /* unknown mode */
    }
}

/* led_control_service runs in interrupt context - be brief!
 * This service is called once per interrupt timer tick - 100 times a second.
 *
 * There should be at most only one i2c operation per call - if more are need
 *  the calls should be spread across calls.
 *
 * Putting all led servicing in one thread means that we wont step on any
 * i2c operations - they are all serialized here in the ISR tick. It also
 * insures that we get called at equal timing for good visual effect.
 */
#ifndef BOOTLOADER
static void led_control_service(void)
{
    static unsigned char
        sc606regAval=DEFAULT_BRIGHTNESS_SETTING,
        sc606regBval=0,
        sc606regCval=0,
        sc606regCONFval=0x03;

    static bool sc606_changed=false, sc606_CONF_changed=false;

    if(sc606_changed==false)
    {
        switch (backlight_control)
        {
            case BACKLIGHT_CONTROL_IDLE:
                backlight_control = BACKLIGHT_CONTROL_IDLE;
                break;
            case BACKLIGHT_CONTROL_OFF:
                sc606_changed=true;
                sc606_CONF_changed=true;
                sc606regCONFval &= ~0x03;
                sc606regAval=0;
                backlight_control = BACKLIGHT_CONTROL_IDLE;
                break;
            case BACKLIGHT_CONTROL_ON:
                sc606_changed=true;
                sc606_CONF_changed=true;
                sc606regCONFval |= 0x03;
                sc606regAval=backlight_brightness;
                backlight_control = BACKLIGHT_CONTROL_IDLE;
                break;
            case BACKLIGHT_CONTROL_SET:
                if(!(sc606regCONFval&0x03))
                    break;
                sc606_changed=true;
                sc606regAval=backlight_brightness;
                backlight_control = BACKLIGHT_CONTROL_IDLE;
                break;
            case BACKLIGHT_CONTROL_FADE:
                /* Was this mode set while the backlight is already on/off? */
                if(backlight_target==sc606regAval)
                {
                    backlight_control = BACKLIGHT_CONTROL_IDLE;
                    break;
                }
                sc606_changed=true;
                if(!(sc606regCONFval&0x03))
                {
                    sc606_CONF_changed=true;
                    sc606regCONFval |= 0x03;
                }
                if(backlight_target>sc606regAval)
                {
                    sc606regAval++;
                    if(backlight_target==sc606regAval)
                        backlight_control = BACKLIGHT_CONTROL_IDLE;
                }
                else
                {
                    sc606regAval--;
                    if(sc606regAval==0)
                        backlight_control = BACKLIGHT_CONTROL_OFF;
                    else if (backlight_target==sc606regAval)
                        backlight_control = BACKLIGHT_CONTROL_IDLE;
                }

                break;
            default:
                backlight_control = BACKLIGHT_CONTROL_IDLE;
                break;
        }
        switch (buttonlight_control)
        {
            case BUTTONLIGHT_CONTROL_IDLE:
                buttonlight_control=BUTTONLIGHT_CONTROL_IDLE;
                break;
            case BUTTONLIGHT_CONTROL_OFF:
                sc606_changed=true;
                sc606_CONF_changed=true;
                sc606regCONFval &= ~BUTTONLIGHT_ALL;
                sc606regBval=sc606regCval=0;
                buttonlight_control=BUTTONLIGHT_CONTROL_IDLE;
                break;
            case BUTTONLIGHT_CONTROL_ON:
                sc606_changed=true;
                sc606_CONF_changed=true;
                sc606regCONFval |= BUTTONLIGHT_ALL;
                sc606regBval=sc606regCval=buttonlight_brightness;
                buttonlight_control=BUTTONLIGHT_CONTROL_IDLE;
                break;
            case BUTTONLIGHT_CONTROL_SET:
                if(!(sc606regCONFval&BUTTONLIGHT_ALL))
                    break;
                sc606_changed=true;
                sc606regBval=sc606regCval=buttonlight_brightness;
                buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;
                break;
            case BUTTONLIGHT_CONTROL_FADE:
                /* Was this mode set while the button light is already on? */
                if(buttonlight_target==sc606regBval)
                {
                    buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;
                    break;
                }
                sc606_changed=true;
                if(!(sc606regCONFval&BUTTONLIGHT_ALL))
                {
                    sc606_CONF_changed=true;
                    sc606regCONFval |= BUTTONLIGHT_ALL;
                }
                if(buttonlight_target>sc606regBval)
                {
                    sc606regCval=++sc606regBval;
                    if(buttonlight_target==sc606regBval)
                        buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;
                }
                else
                {
                    sc606regCval=--sc606regBval;
                    if(sc606regBval==0)
                        buttonlight_control = BUTTONLIGHT_CONTROL_OFF;
                    else if (buttonlight_target==sc606regBval)
                        backlight_control = BACKLIGHT_CONTROL_IDLE;
                }

                break;
            default:
                buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;
                break;
        }
    }

    switch (sc606_control)
    {
        case SC606_CONTROL_IDLE:
            if(sc606_changed)
                sc606_control=SC606_CONTROL_A12;
            else
                sc606_control=SC606_CONTROL_IDLE;
            break;

        case SC606_CONTROL_A12:
            sc606_write(SC606_REG_A , sc606regAval);
            sc606_control=SC606_CONTROL_B12;
            break;

        case SC606_CONTROL_B12:
            sc606_write(SC606_REG_B , sc606regBval);
            sc606_control=SC606_CONTROL_C12;
            break;

        case SC606_CONTROL_C12:
            sc606_write(SC606_REG_C , sc606regCval);
            if(sc606_CONF_changed!=true)
            {
                sc606_changed=false;
                if(backlight_control != BACKLIGHT_CONTROL_IDLE ||
                    buttonlight_control != BUTTONLIGHT_CONTROL_IDLE)
                {
                    sc606_control=SC606_CONTROL_A12;
                }
                else
                {
                    sc606_control=SC606_CONTROL_IDLE;
                }
            }
            else
            {
                sc606_control=SC606_CONTROL_CONF;
            }
            break;

        case SC606_CONTROL_CONF:
            sc606_write(SC606_REG_CONF , sc606regCONFval);
            sc606_changed=false;
            sc606_CONF_changed=false;
            if(backlight_control != BACKLIGHT_CONTROL_IDLE ||
                buttonlight_control != BUTTONLIGHT_CONTROL_IDLE)
            {
                sc606_control=SC606_CONTROL_A12;
            }
            else
            {
                sc606_control=SC606_CONTROL_IDLE;
            }
            break;

        default:
            sc606_control=SC606_CONTROL_IDLE;
            break;
    }

    if(sc606regCONFval&0x03)
        lcd_enable(true);
    else
        lcd_enable(false);
}
#endif /* BOOTLOADER */

static void __backlight_dim(bool dim_now)
{
    /* dont let the interrupt tick happen */
    backlight_control = BACKLIGHT_CONTROL_IDLE;
    backlight_target = (dim_now == true) ? 0 : backlight_brightness;
    if(backlight_target==0 && backlight_brightness==0)
    {
        if(dim_now == false)
            backlight_control = BACKLIGHT_CONTROL_ON;
        else
            backlight_control = BACKLIGHT_CONTROL_OFF;
    }
    else
        backlight_control = BACKLIGHT_CONTROL_FADE;
}

void _backlight_on(void)
{
    lcd_enable(true);
    __backlight_dim(false);
}    

void _backlight_off(void)
{
    __backlight_dim(true);
}

static inline void __buttonlight_on(void)
{
    buttonlight_control = BUTTONLIGHT_CONTROL_ON;
}

static inline void __buttonlight_off(void)
{
    buttonlight_control = BUTTONLIGHT_CONTROL_OFF;
}

static void __buttonlight_dim(bool dim_now)
{
    buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;
    buttonlight_target = (dim_now == true) ? 0 : buttonlight_brightness;
    if(buttonlight_target==0 && buttonlight_brightness==0)
    {
        if(dim_now == false)
            buttonlight_control = BUTTONLIGHT_CONTROL_ON;
        else
            buttonlight_control = BUTTONLIGHT_CONTROL_OFF;
    }
    else
        buttonlight_control = BUTTONLIGHT_CONTROL_FADE;
}

void _buttonlight_on(void)
{
    __buttonlight_dim(false);
}

void _buttonlight_off(void)
{
    if(_buttonlight_timeout>0)
        __buttonlight_dim(true);
    else
        __buttonlight_off();
}

void _buttonlight_set_brightness(int brightness)
{
    buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;
    buttonlight_brightness = brightness;
    buttonlight_control = BUTTONLIGHT_CONTROL_SET;
}

bool _backlight_init(void)
{
    buttonlight_brightness = DEFAULT_BRIGHTNESS_SETTING;
    backlight_brightness = DEFAULT_BRIGHTNESS_SETTING;

    buttonlight_control = BUTTONLIGHT_CONTROL_IDLE;
    backlight_control = BACKLIGHT_CONTROL_ON;

    /* Set the backlight up in a known state */
    sc606_init();
    sc606_write(SC606_REG_A , DEFAULT_BRIGHTNESS_SETTING);
    sc606_write(SC606_REG_B , 0);
    sc606_write(SC606_REG_C , 0);
    sc606_write(SC606_REG_CONF , 0x03);
#ifndef BOOTLOADER
    /* put the led control on the tick list */
    tick_add_task(led_control_service);
#endif
    return true;
}