summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/ipod/backlight-nano_video.c
blob: e2e9bf0341214a5fa80a579298314caf21d384aa (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include <stdlib.h>
#include "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "i2c.h"
#include "debug.h"
#include "rtc.h"
#include "usb.h"
#include "power.h"
#include "system.h"
#include "button.h"
#include "timer.h"
#include "backlight.h"
#include "backlight-target.h"

static int brightness = 1;   /* 1 to 32 */
static int current_dim = 16; /* default after enabling the backlight dimmer */
static bool enabled = false;

void backlight_hw_brightness(int val)
{
    int oldlevel;

    if (current_dim < val)
    {
        do
        {
            oldlevel = disable_irq_save();
            GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x80);
            udelay(10);
            GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x80);
            restore_irq(oldlevel);
            udelay(10);
        }
        while (++current_dim < val);
    }
    else if (current_dim > val)
    {
        do
        {
            oldlevel = disable_irq_save();
            GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x80);
            udelay(200);
            GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x80);
            restore_irq(oldlevel);
            udelay(10);
        }
        while (--current_dim > val);
    }
    brightness = val;
}

void _backlight_hw_enable(bool on)
{
#ifdef HAVE_LCD_SLEEP
    if (on)
        /* If the fade-out is interrupted, enabled will be true, but 
           lcd_awake() needs to be called anyways because the LCD 
           may be sleeping.
         */
        lcd_awake();
#endif

    if (on == enabled)
        return;

    if (on)
    {
        GPIO_SET_BITWISE(GPIOB_OUTPUT_VAL, 0x08);
        GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x80);
        sleep(HZ/100);
        current_dim = 16;
        backlight_hw_brightness(brightness);
    }
    else
    {
        GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x80);
        GPIO_CLEAR_BITWISE(GPIOB_OUTPUT_VAL, 0x08);
        sleep(HZ/20);
    }
    enabled = on;
}

/* Switch the backlight on. Works only if the backlight circuit is enabled.
 * Called in ISR context for fading, so it must be fast. */
void _backlight_led_on(void)
{
    GPIO_SET_BITWISE(GPIOL_OUTPUT_VAL, 0x80);
}

/* Switch the backlight off. Keeps the backlight circuit enabled.
 * Called in ISR context for fading, so it must be fast. */
void _backlight_led_off(void)
{
    GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_VAL, 0x80);
}

bool backlight_hw_init(void)
{
    GPIO_SET_BITWISE(GPIOB_ENABLE, 0x08);
    GPIO_SET_BITWISE(GPIOB_OUTPUT_EN, 0x08);
    GPIO_SET_BITWISE(GPIOD_ENABLE, 0x80);
    GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x80);
    _backlight_hw_enable(true);
    GPIO_SET_BITWISE(GPIOL_ENABLE, 0x80);
    GPIO_SET_BITWISE(GPIOL_OUTPUT_EN, 0x80);
    GPIO_SET_BITWISE(GPIOL_OUTPUT_VAL, 0x80);

    return true;
}