ipod nano 2g: make functions/variables static where possible, add missing #includes
[maemo-rb.git] / firmware / target / arm / s5l8700 / ipodnano2g / pmu-nano2g.c
blob37cc1dcced64f42172d1fe89fe717e519735f552
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright © 2008 Rafaël Carré
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "kernel.h"
24 #include "i2c-s5l8700.h"
25 #include "pmu-target.h"
27 static struct mutex pmu_adc_mutex;
29 int pmu_read_multiple(int address, int count, unsigned char* buffer)
31 return i2c_read(0xe6, address, count, buffer);
34 int pmu_write_multiple(int address, int count, unsigned char* buffer)
36 return i2c_write(0xe6, address, count, buffer);
39 unsigned char pmu_read(int address)
41 unsigned char tmp;
43 pmu_read_multiple(address, 1, &tmp);
45 return tmp;
48 int pmu_write(int address, unsigned char val)
50 return pmu_write_multiple(address, 1, &val);
53 void pmu_init(void)
55 mutex_init(&pmu_adc_mutex);
58 int pmu_read_adc(unsigned int adc)
60 int data = 0;
61 mutex_lock(&pmu_adc_mutex);
62 pmu_write(0x54, 5 | (adc << 4));
63 while ((data & 0x80) == 0)
65 yield();
66 data = pmu_read(0x57);
68 int value = (pmu_read(0x55) << 2) | (data & 3);
69 mutex_unlock(&pmu_adc_mutex);
70 return value;
73 /* millivolts */
74 int pmu_read_battery_voltage(void)
76 return pmu_read_adc(0) * 6;
79 /* milliamps */
80 int pmu_read_battery_current(void)
82 return pmu_read_adc(2);
85 void pmu_ldo_on_in_standby(unsigned int ldo, int onoff)
87 if (ldo < 4)
89 unsigned char newval = pmu_read(0x3B) & ~(1 << (2 * ldo));
90 if (onoff) newval |= 1 << (2 * ldo);
91 pmu_write(0x3B, newval);
93 else if (ldo < 8)
95 unsigned char newval = pmu_read(0x3C) & ~(1 << (2 * (ldo - 4)));
96 if (onoff) newval |= 1 << (2 * (ldo - 4));
97 pmu_write(0x3C, newval);
101 void pmu_ldo_set_voltage(unsigned int ldo, unsigned char voltage)
103 if (ldo > 6) return;
104 pmu_write(0x2d + (ldo << 1), voltage);
107 void pmu_ldo_power_on(unsigned int ldo)
109 if (ldo > 6) return;
110 pmu_write(0x2e + (ldo << 1), 1);
113 void pmu_ldo_power_off(unsigned int ldo)
115 if (ldo > 6) return;
116 pmu_write(0x2e + (ldo << 1), 0);
119 void pmu_set_wake_condition(unsigned char condition)
121 pmu_write(0xd, condition);
124 void pmu_enter_standby(void)
126 pmu_write(0xc, 1);
129 void pmu_read_rtc(unsigned char* buffer)
131 pmu_read_multiple(0x59, 7, buffer);
134 void pmu_write_rtc(unsigned char* buffer)
136 pmu_write_multiple(0x59, 7, buffer);