summaryrefslogtreecommitdiff
path: root/firmware/target/arm/s5l8700/ipodnano2g/pmu-nano2g.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/s5l8700/ipodnano2g/pmu-nano2g.c')
-rw-r--r--firmware/target/arm/s5l8700/ipodnano2g/pmu-nano2g.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/firmware/target/arm/s5l8700/ipodnano2g/pmu-nano2g.c b/firmware/target/arm/s5l8700/ipodnano2g/pmu-nano2g.c
new file mode 100644
index 0000000000..7a6407e809
--- /dev/null
+++ b/firmware/target/arm/s5l8700/ipodnano2g/pmu-nano2g.c
@@ -0,0 +1,90 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright © 2008 Rafaël Carré
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "config.h"
23#include "kernel.h"
24#include "i2c-s5l8700.h"
25
26static struct mutex pmu_adc_mutex;
27int pmu_initialized;
28
29unsigned char pmu_read(int address)
30{
31 unsigned char tmp;
32
33 i2c_read(0xe6, address, 1, &tmp);
34
35 return tmp;
36}
37
38void pmu_write(int address, unsigned char val)
39{
40 i2c_write(0xe6, address, 1, &val);
41}
42
43void pmu_read_multiple(int address, int count, unsigned char* buffer)
44{
45 i2c_read(0xe6, address, count, buffer);
46}
47
48void pmu_write_multiple(int address, int count, unsigned char* buffer)
49{
50 i2c_write(0xe6, address, count, buffer);
51}
52
53void pmu_init(void)
54{
55 if (pmu_initialized) return;
56 mutex_init(&pmu_adc_mutex);
57 pmu_initialized = 1;
58}
59
60int pmu_read_battery_voltage(void)
61{
62 int data = 0;
63 if (!pmu_initialized) pmu_init();
64 mutex_lock(&pmu_adc_mutex);
65 pmu_write(0x54, 0x05);
66 while ((data & 0x80) == 0)
67 {
68 yield();
69 data = pmu_read(0x57);
70 }
71 int millivolts = ((pmu_read(0x55) << 2) | (data & 3)) * 6;
72 mutex_unlock(&pmu_adc_mutex);
73 return millivolts;
74}
75
76int pmu_read_battery_current(void)
77{
78 int data = 0;
79 if (!pmu_initialized) pmu_init();
80 mutex_lock(&pmu_adc_mutex);
81 pmu_write(0x54, 0x25);
82 while ((data & 0x80) == 0)
83 {
84 yield();
85 data = pmu_read(0x57);
86 }
87 int milliamps = (pmu_read(0x55) << 2) | (data & 3);
88 mutex_unlock(&pmu_adc_mutex);
89 return milliamps;
90}