From be25469b9b481d8f40aeb12aa6de84e1efdc0e68 Mon Sep 17 00:00:00 2001 From: Michael Sparmann Date: Mon, 5 Oct 2009 20:21:33 +0000 Subject: Reworked iPod Nano 2G PMU code, added RTC and battery ADC. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22967 a1c6a512-1295-4272-9138-f99709370657 --- firmware/SOURCES | 2 + firmware/export/config-ipodnano2g.h | 3 +- firmware/export/config.h | 2 + .../arm/s5l8700/ipodnano2g/backlight-nano2g.c | 26 ++----- .../target/arm/s5l8700/ipodnano2g/pmu-nano2g.c | 90 ++++++++++++++++++++++ .../target/arm/s5l8700/ipodnano2g/pmu-target.h | 35 +++++++++ .../arm/s5l8700/ipodnano2g/powermgmt-nano2g.c | 9 +-- .../target/arm/s5l8700/ipodnano2g/rtc-nano2g.c | 73 ++++++++++++++++++ 8 files changed, 213 insertions(+), 27 deletions(-) create mode 100644 firmware/target/arm/s5l8700/ipodnano2g/pmu-nano2g.c create mode 100644 firmware/target/arm/s5l8700/ipodnano2g/pmu-target.h create mode 100644 firmware/target/arm/s5l8700/ipodnano2g/rtc-nano2g.c (limited to 'firmware') diff --git a/firmware/SOURCES b/firmware/SOURCES index 94c4ecd658..50f68d4e94 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -1332,6 +1332,8 @@ target/arm/s5l8700/ipodnano2g/power-nano2g.c target/arm/s5l8700/ipodnano2g/audio-nano2g.c target/arm/s5l8700/ipodnano2g/ftl-nano2g.c target/arm/s5l8700/ipodnano2g/nand-nano2g.c +target/arm/s5l8700/ipodnano2g/pmu-nano2g.c +target/arm/s5l8700/ipodnano2g/rtc-nano2g.c #endif #ifndef SIMULATOR diff --git a/firmware/export/config-ipodnano2g.h b/firmware/export/config-ipodnano2g.h index d3ea611ebc..9d36a37046 100644 --- a/firmware/export/config-ipodnano2g.h +++ b/firmware/export/config-ipodnano2g.h @@ -83,8 +83,7 @@ #define CONFIG_CODEC SWCODEC /* define this if you have a real-time clock */ -//#define CONFIG_RTC RTC_S5L8700 -#define CONFIG_RTC RTC_S35390A +#define CONFIG_RTC RTC_NANO2G #define CONFIG_LCD LCD_NANO2G diff --git a/firmware/export/config.h b/firmware/export/config.h index a1a8a9df2a..941ee5f9b4 100644 --- a/firmware/export/config.h +++ b/firmware/export/config.h @@ -263,6 +263,8 @@ Lyre prototype 1*/ #define RTC_S5L8700 14 #define RTC_S35390A 15 #define RTC_JZ47XX 16 /* Ingenic Jz47XX */ +#define RTC_NANO2G 17 /* This seems to be a PCFxxxxx, + but we don't know which one */ /* USB On-the-go */ #define USBOTG_M66591 6591 /* M:Robe 500 */ diff --git a/firmware/target/arm/s5l8700/ipodnano2g/backlight-nano2g.c b/firmware/target/arm/s5l8700/ipodnano2g/backlight-nano2g.c index fd600559c0..21ba3e0bf0 100644 --- a/firmware/target/arm/s5l8700/ipodnano2g/backlight-nano2g.c +++ b/firmware/target/arm/s5l8700/ipodnano2g/backlight-nano2g.c @@ -23,23 +23,9 @@ #include "config.h" #include "backlight.h" #include "backlight-target.h" -#include "i2c-s5l8700.h" +#include "pmu-target.h" -static unsigned char bl_i2c_readbyte(int address) -{ - unsigned char tmp; - - i2c_read(0xe6, address, 1, &tmp); - - return tmp; -} - -static void bl_i2c_writebyte(int address, unsigned char val) -{ - i2c_write(0xe6, address, 1, &val); -} - void _backlight_set_brightness(int brightness) { (void)brightness; @@ -47,19 +33,19 @@ void _backlight_set_brightness(int brightness) void _backlight_on(void) { - bl_i2c_writebyte(0x29,1); + pmu_write(0x29, 1); } void _backlight_off(void) { - bl_i2c_writebyte(0x29,0); + pmu_write(0x29, 0); } bool _backlight_init(void) { - bl_i2c_writebyte(0x2a,6); - bl_i2c_writebyte(0x28,0x2e); - bl_i2c_writebyte(0x2b,20); + pmu_write(0x2a, 6); + pmu_write(0x28, 0x2e); + pmu_write(0x2b, 20); _backlight_on(); 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 @@ +/*************************************************************************** + * __________ __ ___. + * 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" + +static struct mutex pmu_adc_mutex; +int pmu_initialized; + +unsigned char pmu_read(int address) +{ + unsigned char tmp; + + i2c_read(0xe6, address, 1, &tmp); + + return tmp; +} + +void pmu_write(int address, unsigned char val) +{ + i2c_write(0xe6, address, 1, &val); +} + +void pmu_read_multiple(int address, int count, unsigned char* buffer) +{ + i2c_read(0xe6, address, count, buffer); +} + +void pmu_write_multiple(int address, int count, unsigned char* buffer) +{ + i2c_write(0xe6, address, count, buffer); +} + +void pmu_init(void) +{ + if (pmu_initialized) return; + mutex_init(&pmu_adc_mutex); + pmu_initialized = 1; +} + +int pmu_read_battery_voltage(void) +{ + int data = 0; + if (!pmu_initialized) pmu_init(); + mutex_lock(&pmu_adc_mutex); + pmu_write(0x54, 0x05); + while ((data & 0x80) == 0) + { + yield(); + data = pmu_read(0x57); + } + int millivolts = ((pmu_read(0x55) << 2) | (data & 3)) * 6; + mutex_unlock(&pmu_adc_mutex); + return millivolts; +} + +int pmu_read_battery_current(void) +{ + int data = 0; + if (!pmu_initialized) pmu_init(); + mutex_lock(&pmu_adc_mutex); + pmu_write(0x54, 0x25); + while ((data & 0x80) == 0) + { + yield(); + data = pmu_read(0x57); + } + int milliamps = (pmu_read(0x55) << 2) | (data & 3); + mutex_unlock(&pmu_adc_mutex); + return milliamps; +} diff --git a/firmware/target/arm/s5l8700/ipodnano2g/pmu-target.h b/firmware/target/arm/s5l8700/ipodnano2g/pmu-target.h new file mode 100644 index 0000000000..608a22a8eb --- /dev/null +++ b/firmware/target/arm/s5l8700/ipodnano2g/pmu-target.h @@ -0,0 +1,35 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright © 2009 Michael Sparmann + * + * 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. + * + ****************************************************************************/ + +#ifndef __PMU_NANO2G_H__ +#define __PMU_NANO2G_H__ + +#include "config.h" + +unsigned char pmu_read(int address); +void pmu_write(int address, unsigned char val); +void pmu_read_multiple(int address, int count, unsigned char* buffer); +void pmu_write_multiple(int address, int count, unsigned char* buffer); +int pmu_read_battery_voltage(void); +int pmu_read_battery_current(void); +void pmu_init(void); + +#endif diff --git a/firmware/target/arm/s5l8700/ipodnano2g/powermgmt-nano2g.c b/firmware/target/arm/s5l8700/ipodnano2g/powermgmt-nano2g.c index 508995c436..937905f5f4 100644 --- a/firmware/target/arm/s5l8700/ipodnano2g/powermgmt-nano2g.c +++ b/firmware/target/arm/s5l8700/ipodnano2g/powermgmt-nano2g.c @@ -20,9 +20,8 @@ ****************************************************************************/ #include "config.h" -#include "adc.h" -#include "adc-target.h" #include "powermgmt.h" +#include "pmu-target.h" const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = { @@ -52,14 +51,14 @@ const unsigned short percent_to_volt_charge[11] = }; #endif /* CONFIG_CHARGING */ -/* ADC should read 0x3ff=5.12V */ -#define BATTERY_SCALE_FACTOR 5125 +/* ADC should read 0x3ff=6.00V */ +#define BATTERY_SCALE_FACTOR 6000 /* full-scale ADC readout (2^10) in millivolt */ /* Returns battery voltage from ADC [millivolts] */ unsigned int battery_adc_voltage(void) { - return 4000; + return pmu_read_battery_voltage(); } diff --git a/firmware/target/arm/s5l8700/ipodnano2g/rtc-nano2g.c b/firmware/target/arm/s5l8700/ipodnano2g/rtc-nano2g.c new file mode 100644 index 0000000000..f0975efb9f --- /dev/null +++ b/firmware/target/arm/s5l8700/ipodnano2g/rtc-nano2g.c @@ -0,0 +1,73 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum + * + * 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 "rtc.h" +#include "kernel.h" +#include "system.h" +#include "pmu-target.h" + +void rtc_init(void) +{ +} + +int rtc_read_datetime(struct tm *tm) +{ + unsigned int i; + unsigned char buf[7]; + + pmu_read_multiple(0x59, sizeof(buf), buf); + + for (i = 0; i < sizeof(buf); i++) + buf[i] = BCD2DEC(buf[i]); + + tm->tm_sec = buf[0]; + tm->tm_min = buf[1]; + tm->tm_hour = buf[2]; + tm->tm_wday = buf[3]; + tm->tm_mday = buf[4]; + tm->tm_mon = buf[5] - 1; + tm->tm_year = buf[6] + 100; + + return 0; +} + +int rtc_write_datetime(const struct tm *tm) +{ + unsigned int i; + int rc, oldlevel; + unsigned char buf[7]; + + buf[0] = tm->tm_sec; + buf[1] = tm->tm_min; + buf[2] = tm->tm_hour; + buf[3] = tm->tm_wday; + buf[4] = tm->tm_mday; + buf[5] = tm->tm_mon + 1; + buf[6] = tm->tm_year - 100; + + for (i = 0; i < sizeof(buf); i++) + buf[i] = DEC2BCD(buf[i]); + + pmu_write_multiple(0x59, sizeof(buf), buf); + + return 0; +} + -- cgit v1.2.3