summaryrefslogtreecommitdiff
path: root/firmware/target/arm/imx31/gigabeat-s/power-imx31.c
blob: 727b38bd96ea8bb296274f07c153ac8d8f0200e4 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 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 "system.h"
#include "usb.h"
#include "usb_core.h"
#include "power.h"
#include "power-imx31.h"
#include "backlight.h"
#include "backlight-target.h"
#include "avic-imx31.h"
#include "mc13783.h"
#if CONFIG_TUNER
#include "fmradio_i2c.h"
#endif

static unsigned int power_status = POWER_INPUT_NONE;

/* Detect which power sources are present. */
unsigned int power_input_status(void)
{
    unsigned int status = power_status;

    if (GPIO3_DR & (1 << 20))
        status |= POWER_INPUT_BATTERY;

    if (usb_allowed_current() < 500)
    {
        /* ACK that USB is connected but NOT chargeable */
        status &= ~(POWER_INPUT_USB_CHARGER & POWER_INPUT_CHARGER);
    }

    return status;
}

/* Detect changes in presence of the AC adaptor. */
void charger_main_detect_event(void)
{
    if (mc13783_read(MC13783_INTERRUPT_SENSE0) & MC13783_SE1S)
        power_status |= POWER_INPUT_MAIN_CHARGER;
    else
        power_status &= ~POWER_INPUT_MAIN_CHARGER;
}

/* Detect changes in USB bus power. Called from usb connect event handler. */
void charger_usb_detect_event(int status)
{
    /* USB plugged does not imply charging is possible or even
     * powering the device to maintain the battery. */
    if (status == USB_INSERTED)
        power_status |= POWER_INPUT_USB_CHARGER;
    else
        power_status &= ~POWER_INPUT_USB_CHARGER;
}

/* charging_state is implemented in powermgmt-imx31.c */

void ide_power_enable(bool on)
{
    if (!on)
    {
        /* Bus must be isolated before power off */
        imx31_regset32(&GPIO2_DR, (1 << 16));
    }

    /* HD power switch */
    imx31_regmod32(&GPIO3_DR, on ? (1 << 5) : 0, (1 << 5));

    if (on)
    {
        /* Bus switch may be turned on after powerup */
        sleep(HZ/10);
        imx31_regclr32(&GPIO2_DR, (1 << 16));
    }
}

bool ide_powered(void)
{
    return (GPIO3_DR & (1 << 5)) != 0;
}

#if CONFIG_TUNER
bool tuner_power(bool status)
{
    static bool tuner_powered = false;

    if (status == tuner_powered)
        return status;

    tuner_powered = status;

    if (status)
    {
        /* the si4700 is the only thing connected to i2c2 so
           we can diable the i2c module when not in use */
        fmradio_i2c_enable(true);
        /* enable the fm chip */
        imx31_regset32(&GPIO1_DR, (1 << 26));
        /* enable CLK32KMCU clock */
        mc13783_set(MC13783_POWER_CONTROL0, MC13783_CLK32KMCUEN);
    }
    else
    {
        /* the si4700 is the only thing connected to i2c2 so
           we can diable the i2c module when not in use */
        fmradio_i2c_enable(false);
        /* disable the fm chip */
        imx31_regclr32(&GPIO1_DR, (1 << 26));
        /* disable CLK32KMCU clock */
        mc13783_clear(MC13783_POWER_CONTROL0, MC13783_CLK32KMCUEN);
    }

    return !status;
}
#endif /* #if CONFIG_TUNER */

void power_off(void)
{
    /* Cut backlight */
    _backlight_off();

    /* Let it fade */
    sleep(5*HZ/4);

    /* Set user off mode */
    mc13783_set(MC13783_POWER_CONTROL0, MC13783_USEROFFSPI);

    /* Wait for power cut */
    disable_interrupt(IRQ_FIQ_STATUS);

    while (1);
}

void power_init(void)
{
    /* Poll initial state */
    charger_main_detect_event();

    /* Enable detect event */
    mc13783_enable_event(MC13783_SE1_EVENT);
}