2009-10-05 20:21:33 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright © 2008 Rafaël Carré
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "i2c-s5l8700.h"
|
2010-08-08 10:49:32 +00:00
|
|
|
#include "pmu-target.h"
|
2009-10-05 20:21:33 +00:00
|
|
|
|
|
|
|
static struct mutex pmu_adc_mutex;
|
|
|
|
|
2010-02-17 15:54:48 +00:00
|
|
|
int pmu_read_multiple(int address, int count, unsigned char* buffer)
|
2009-10-05 20:21:33 +00:00
|
|
|
{
|
2010-02-17 15:54:48 +00:00
|
|
|
return i2c_read(0xe6, address, count, buffer);
|
2009-10-05 20:21:33 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 15:54:48 +00:00
|
|
|
int pmu_write_multiple(int address, int count, unsigned char* buffer)
|
2009-10-05 20:21:33 +00:00
|
|
|
{
|
2010-02-17 15:54:48 +00:00
|
|
|
return i2c_write(0xe6, address, count, buffer);
|
2009-10-05 20:21:33 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 20:56:51 +00:00
|
|
|
unsigned char pmu_read(int address)
|
2009-10-05 20:21:33 +00:00
|
|
|
{
|
2009-10-08 20:56:51 +00:00
|
|
|
unsigned char tmp;
|
|
|
|
|
|
|
|
pmu_read_multiple(address, 1, &tmp);
|
|
|
|
|
|
|
|
return tmp;
|
2009-10-05 20:21:33 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 15:54:48 +00:00
|
|
|
int pmu_write(int address, unsigned char val)
|
2009-10-05 20:21:33 +00:00
|
|
|
{
|
2010-02-17 15:54:48 +00:00
|
|
|
return pmu_write_multiple(address, 1, &val);
|
2009-10-05 20:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pmu_init(void)
|
|
|
|
{
|
|
|
|
mutex_init(&pmu_adc_mutex);
|
|
|
|
}
|
|
|
|
|
2009-10-11 18:20:56 +00:00
|
|
|
int pmu_read_adc(unsigned int adc)
|
2009-10-05 20:21:33 +00:00
|
|
|
{
|
|
|
|
int data = 0;
|
|
|
|
mutex_lock(&pmu_adc_mutex);
|
2009-10-11 18:20:56 +00:00
|
|
|
pmu_write(0x54, 5 | (adc << 4));
|
2009-10-05 20:21:33 +00:00
|
|
|
while ((data & 0x80) == 0)
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
data = pmu_read(0x57);
|
|
|
|
}
|
2009-10-11 18:20:56 +00:00
|
|
|
int value = (pmu_read(0x55) << 2) | (data & 3);
|
2009-10-05 20:21:33 +00:00
|
|
|
mutex_unlock(&pmu_adc_mutex);
|
2009-10-11 18:20:56 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* millivolts */
|
|
|
|
int pmu_read_battery_voltage(void)
|
|
|
|
{
|
|
|
|
return pmu_read_adc(0) * 6;
|
2009-10-05 20:21:33 +00:00
|
|
|
}
|
|
|
|
|
2009-10-11 18:20:56 +00:00
|
|
|
/* milliamps */
|
2009-10-05 20:21:33 +00:00
|
|
|
int pmu_read_battery_current(void)
|
|
|
|
{
|
2009-10-11 18:20:56 +00:00
|
|
|
return pmu_read_adc(2);
|
2009-10-05 20:21:33 +00:00
|
|
|
}
|
2009-10-08 20:56:51 +00:00
|
|
|
|
2009-10-11 18:20:56 +00:00
|
|
|
void pmu_ldo_on_in_standby(unsigned int ldo, int onoff)
|
2009-10-08 20:56:51 +00:00
|
|
|
{
|
2009-10-11 18:20:56 +00:00
|
|
|
if (ldo < 4)
|
2009-10-08 20:56:51 +00:00
|
|
|
{
|
2009-10-11 18:20:56 +00:00
|
|
|
unsigned char newval = pmu_read(0x3B) & ~(1 << (2 * ldo));
|
|
|
|
if (onoff) newval |= 1 << (2 * ldo);
|
2009-10-08 20:56:51 +00:00
|
|
|
pmu_write(0x3B, newval);
|
|
|
|
}
|
2009-10-11 18:20:56 +00:00
|
|
|
else if (ldo < 8)
|
2009-10-08 20:56:51 +00:00
|
|
|
{
|
2009-10-11 18:20:56 +00:00
|
|
|
unsigned char newval = pmu_read(0x3C) & ~(1 << (2 * (ldo - 4)));
|
|
|
|
if (onoff) newval |= 1 << (2 * (ldo - 4));
|
2009-10-08 20:56:51 +00:00
|
|
|
pmu_write(0x3C, newval);
|
|
|
|
}
|
2009-10-09 20:36:09 +00:00
|
|
|
}
|
2009-10-11 18:20:56 +00:00
|
|
|
|
|
|
|
void pmu_ldo_set_voltage(unsigned int ldo, unsigned char voltage)
|
|
|
|
{
|
|
|
|
if (ldo > 6) return;
|
|
|
|
pmu_write(0x2d + (ldo << 1), voltage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pmu_ldo_power_on(unsigned int ldo)
|
|
|
|
{
|
|
|
|
if (ldo > 6) return;
|
|
|
|
pmu_write(0x2e + (ldo << 1), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pmu_ldo_power_off(unsigned int ldo)
|
|
|
|
{
|
|
|
|
if (ldo > 6) return;
|
|
|
|
pmu_write(0x2e + (ldo << 1), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pmu_set_wake_condition(unsigned char condition)
|
|
|
|
{
|
|
|
|
pmu_write(0xd, condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pmu_enter_standby(void)
|
|
|
|
{
|
|
|
|
pmu_write(0xc, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pmu_read_rtc(unsigned char* buffer)
|
|
|
|
{
|
|
|
|
pmu_read_multiple(0x59, 7, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pmu_write_rtc(unsigned char* buffer)
|
|
|
|
{
|
|
|
|
pmu_write_multiple(0x59, 7, buffer);
|
|
|
|
}
|