Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
273 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
273 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
273 theseven 9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
273 theseven 14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
273 theseven 20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "i2c.h"
26
#include "pmu.h"
27
#include "thread.h"
28
 
29
 
30
static struct mutex pmumutex;
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
}
51
 
52
void pmu_init()
53
{
54
    mutex_init(&pmumutex);
683 theseven 55
//    pmu_write(0x1e, 0x0f);  // Vcore @ 1.000V
56
//    pmu_write(0x22, 0x22);  // Vmem @ 1.475V
273 theseven 57
}
58
 
59
int pmu_read_adc(unsigned int adc)
60
{
61
    int data = 0;
62
    mutex_lock(&pmumutex, TIMEOUT_BLOCK);
63
    pmu_write(0x54, 5 | (adc << 4));
64
    while ((data & 0x80) == 0)
65
    {
598 theseven 66
        sleep(1000);
273 theseven 67
        data = pmu_read(0x57);
68
    }
69
    int value = (pmu_read(0x55) << 2) | (data & 3);
70
    mutex_unlock(&pmumutex);
71
    return value;
72
}
73
 
74
/* millivolts */
75
int pmu_read_battery_voltage(void)
76
{
77
    return pmu_read_adc(0) * 6;
78
}
79
 
80
/* milliamps */
81
int pmu_read_battery_current(void)
82
{
622 theseven 83
    return -1;
273 theseven 84
}
85
 
86
void pmu_ldo_on_in_standby(unsigned int ldo, int onoff)
87
{
88
    if (ldo < 4)
89
    {
90
        unsigned char newval = pmu_read(0x3B) & ~(1 << (2 * ldo));
91
        if (onoff) newval |= 1 << (2 * ldo);
92
        pmu_write(0x3B, newval);
93
    }
94
    else if (ldo < 8)
95
    {
96
        unsigned char newval = pmu_read(0x3C) & ~(1 << (2 * (ldo - 4)));
97
        if (onoff) newval |= 1 << (2 * (ldo - 4));
98
        pmu_write(0x3C, newval);
99
    }
100
}
101
 
102
void pmu_ldo_set_voltage(unsigned int ldo, unsigned char voltage)
103
{
104
    if (ldo > 6) return;
105
    pmu_write(0x2d + (ldo << 1), voltage);
106
}
107
 
108
void pmu_ldo_power_on(unsigned int ldo)
109
{
110
    if (ldo > 6) return;
111
    pmu_write(0x2e + (ldo << 1), 1);
112
}
113
 
114
void pmu_ldo_power_off(unsigned int ldo)
115
{
116
    if (ldo > 6) return;
117
    pmu_write(0x2e + (ldo << 1), 0);
118
}
119
 
120
void pmu_set_wake_condition(unsigned char condition)
121
{
122
    pmu_write(0xd, condition);
123
}
124
 
125
void pmu_enter_standby(void)
126
{
127
    pmu_write(0xc, 1);
128
}
129
 
130
void pmu_read_rtc(unsigned char* buffer)
131
{
132
    pmu_read_multiple(0x59, 7, buffer);
133
}
134
 
135
void pmu_write_rtc(unsigned char* buffer)
136
{
137
    pmu_write_multiple(0x59, 7, buffer);
138
}