Subversion Repositories freemyipod

Rev

Rev 29 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29 Rev 54
Line 27... Line 27...
27
#include "thread.h"
27
#include "thread.h"
28
 
28
 
29
 
29
 
30
static struct mutex pmumutex;
30
static struct mutex pmumutex;
31
 
31
 
-
 
32
void pmu_read_multiple(int address, int count, unsigned char* buffer)
-
 
33
{
-
 
34
    i2c_recv(0, 0xe6, address, buffer, count);
-
 
35
}
-
 
36
 
-
 
37
void pmu_write_multiple(int address, int count, unsigned char* buffer)
-
 
38
{
-
 
39
    i2c_send(0, 0xe6, address, buffer, count);
-
 
40
}
-
 
41
 
-
 
42
unsigned char pmu_read(int address)
-
 
43
{
-
 
44
    return i2c_recvbyte(0, 0xe6, address);
-
 
45
}
-
 
46
 
-
 
47
void pmu_write(int address, unsigned char val)
-
 
48
{
-
 
49
    i2c_sendbyte(0, 0xe6, address, val);
-
 
50
}
32
 
51
 
33
void pmu_init()
52
void pmu_init()
34
{
53
{
35
    mutex_init(&pmumutex);
54
    mutex_init(&pmumutex);
36
}
55
}
37
 
56
 
-
 
57
int pmu_read_adc(unsigned int adc)
-
 
58
{
-
 
59
    int data = 0;
-
 
60
    mutex_lock(&pmumutex, TIMEOUT_BLOCK);
-
 
61
    pmu_write(0x54, 5 | (adc << 4));
-
 
62
    while ((data & 0x80) == 0)
-
 
63
    {
38
void poweroff()
64
        yield();
-
 
65
        data = pmu_read(0x57);
-
 
66
    }
-
 
67
    int value = (pmu_read(0x55) << 2) | (data & 3);
-
 
68
    mutex_unlock(&pmumutex);
-
 
69
    return value;
-
 
70
}
-
 
71
 
-
 
72
/* millivolts */
-
 
73
int pmu_read_battery_voltage(void)
-
 
74
{
-
 
75
    return pmu_read_adc(0) * 6;
-
 
76
}
-
 
77
 
-
 
78
/* milliamps */
-
 
79
int pmu_read_battery_current(void)
-
 
80
{
-
 
81
    return pmu_read_adc(2);
-
 
82
}
-
 
83
 
-
 
84
void pmu_ldo_on_in_standby(unsigned int ldo, int onoff)
-
 
85
{
-
 
86
    if (ldo < 4)
-
 
87
    {
-
 
88
        unsigned char newval = pmu_read(0x3B) & ~(1 << (2 * ldo));
-
 
89
        if (onoff) newval |= 1 << (2 * ldo);
-
 
90
        pmu_write(0x3B, newval);
-
 
91
    }
-
 
92
    else if (ldo < 8)
-
 
93
    {
-
 
94
        unsigned char newval = pmu_read(0x3C) & ~(1 << (2 * (ldo - 4)));
-
 
95
        if (onoff) newval |= 1 << (2 * (ldo - 4));
-
 
96
        pmu_write(0x3C, newval);
-
 
97
    }
-
 
98
}
-
 
99
 
-
 
100
void pmu_ldo_set_voltage(unsigned int ldo, unsigned char voltage)
-
 
101
{
-
 
102
    if (ldo > 6) return;
-
 
103
    pmu_write(0x2d + (ldo << 1), voltage);
-
 
104
}
-
 
105
 
-
 
106
void pmu_ldo_power_on(unsigned int ldo)
-
 
107
{
-
 
108
    if (ldo > 6) return;
-
 
109
    pmu_write(0x2e + (ldo << 1), 1);
-
 
110
}
-
 
111
 
-
 
112
void pmu_ldo_power_off(unsigned int ldo)
-
 
113
{
-
 
114
    if (ldo > 6) return;
-
 
115
    pmu_write(0x2e + (ldo << 1), 0);
-
 
116
}
-
 
117
 
-
 
118
void pmu_set_wake_condition(unsigned char condition)
-
 
119
{
-
 
120
    pmu_write(0xd, condition);
-
 
121
}
-
 
122
 
-
 
123
void pmu_enter_standby(void)
-
 
124
{
-
 
125
    pmu_write(0xc, 1);
-
 
126
}
-
 
127
 
-
 
128
void pmu_read_rtc(unsigned char* buffer)
-
 
129
{
-
 
130
    pmu_read_multiple(0x59, 7, buffer);
-
 
131
}
-
 
132
 
-
 
133
void pmu_write_rtc(unsigned char* buffer)
39
{
134
{
40
    i2c_sendbyte(0, 0xe6, 0xc, 1);
135
    pmu_write_multiple(0x59, 7, buffer);
41
}
136
}