Subversion Repositories freemyipod

Rev

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

Rev Author Line No. Line
29 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
29 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
29 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,
29 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/>.
29 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
 
54 theseven 32
void pmu_read_multiple(int address, int count, unsigned char* buffer)
33
{
34
    i2c_recv(0, 0xe6, address, buffer, count);
35
}
29 theseven 36
 
54 theseven 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
 
29 theseven 52
void pmu_init()
53
{
54
    mutex_init(&pmumutex);
55
}
56
 
54 theseven 57
int pmu_read_adc(unsigned int adc)
29 theseven 58
{
54 theseven 59
    int data = 0;
60
    mutex_lock(&pmumutex, TIMEOUT_BLOCK);
61
    pmu_write(0x54, 5 | (adc << 4));
62
    while ((data & 0x80) == 0)
63
    {
598 theseven 64
        sleep(1000);
54 theseven 65
        data = pmu_read(0x57);
66
    }
67
    int value = (pmu_read(0x55) << 2) | (data & 3);
68
    mutex_unlock(&pmumutex);
69
    return value;
29 theseven 70
}
54 theseven 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)
134
{
135
    pmu_write_multiple(0x59, 7, buffer);
136
}