summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/ypr0/powermgmt-ypr0.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/ypr0/powermgmt-ypr0.c')
-rw-r--r--firmware/target/hosted/ypr0/powermgmt-ypr0.c115
1 files changed, 35 insertions, 80 deletions
diff --git a/firmware/target/hosted/ypr0/powermgmt-ypr0.c b/firmware/target/hosted/ypr0/powermgmt-ypr0.c
index 5701e9f02f..3a97331083 100644
--- a/firmware/target/hosted/ypr0/powermgmt-ypr0.c
+++ b/firmware/target/hosted/ypr0/powermgmt-ypr0.c
@@ -17,117 +17,72 @@
17 * 17 *
18 ****************************************************************************/ 18 ****************************************************************************/
19#include "config.h" 19#include "config.h"
20#include "system.h" 20#include <sys/ioctl.h>
21#include <time.h>
22#include "kernel.h" 21#include "kernel.h"
23#include "powermgmt.h" 22#include "powermgmt.h"
23#include "power.h"
24#include "file.h"
24#include "ascodec-target.h" 25#include "ascodec-target.h"
25#include "stdio.h" 26#include "as3514.h"
27#include "sc900776.h"
26 28
27#if 0 /*still unused*/
28/* The battery manufacturer's website shows discharge curves down to 3.0V,
29 so 'dangerous' and 'shutoff' levels of 3.4V and 3.3V should be safe.
30 */
31const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = 29const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] =
32{ 30{
33 3550 31 3500
34}; 32};
35 33
34/* the OF shuts down at this voltage */
36const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] = 35const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] =
37{ 36{
38 3450 37 3450
39}; 38};
40 39
41/* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */ 40/* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
41/* FIXME: This is guessed. Make proper curve using battery_bench */
42const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] = 42const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
43{ 43{
44 { 3300, 3692, 3740, 3772, 3798, 3828, 3876, 3943, 4013, 4094, 4194 } 44 { 3450, 3692, 3740, 3772, 3798, 3828, 3876, 3943, 4013, 4094, 4194 }
45}; 45};
46 46
47#if CONFIG_CHARGING 47#if CONFIG_CHARGING
48/* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */ 48/* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
49const unsigned short percent_to_volt_charge[11] = 49/* FIXME: This is guessed. Make proper curve using battery_bench */
50const unsigned short const percent_to_volt_charge[11] =
50{ 51{
51 3417, 3802, 3856, 3888, 3905, 3931, 3973, 4025, 4084, 4161, 4219 52 3600, 3802, 3856, 3888, 3905, 3931, 3973, 4025, 4084, 4161, 4219
52}; 53};
53#endif /* CONFIG_CHARGING */
54#endif
55
56#define BATT_MINMVOLT 3450 /* minimum millivolts of battery */
57#define BATT_MAXMVOLT 4150 /* maximum millivolts of battery */
58#define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in
59 minutes */
60
61extern void send_battery_level_event(void);
62extern int last_sent_battery_level;
63extern int battery_percent;
64
65static unsigned int battery_millivolts = BATT_MAXMVOLT;
66/* estimated remaining time in minutes */
67static int powermgmt_est_runningtime_min = BATT_MAXRUNTIME;
68 54
69static void battery_status_update(void) 55unsigned int power_input_status(void)
70{ 56{
71 static time_t last_change = 0; 57 unsigned status = POWER_INPUT_NONE;
72 time_t now; 58 int fd = open("/dev/minivet", O_RDONLY);
73 59 if (fd >= 0)
74 time(&now); 60 {
75 61 if (ioctl(fd, IOCTL_MINIVET_DET_VBUS, NULL) > 0)
76 if (last_change < now) { 62 status = POWER_INPUT_MAIN_CHARGER;
77 last_change = now; 63 close(fd);
78
79 battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) /
80 (BATT_MAXMVOLT - BATT_MINMVOLT);
81
82 powermgmt_est_runningtime_min =
83 battery_percent * BATT_MAXRUNTIME / 100;
84 } 64 }
85 65 return status;
86 send_battery_level_event();
87} 66}
88 67
89void battery_read_info(int *voltage, int *level) 68#endif /* CONFIG_CHARGING */
90{
91 battery_status_update();
92
93 if (voltage)
94 *voltage = battery_millivolts;
95
96 if (level)
97 *level = battery_percent;
98}
99
100unsigned int battery_voltage(void)
101{
102 battery_status_update();
103 return battery_millivolts;
104}
105
106int battery_level(void)
107{
108 battery_status_update();
109 return battery_percent;
110}
111
112int battery_time(void)
113{
114 battery_status_update();
115 return powermgmt_est_runningtime_min;
116}
117 69
118bool battery_level_safe(void)
119{
120 return battery_level() >= 10;
121}
122 70
123void set_battery_capacity(int capacity) 71/* Returns battery voltage from ADC [millivolts],
72 * adc returns voltage in 5mV steps */
73unsigned int battery_adc_voltage(void)
124{ 74{
125 (void)capacity; 75 return adc_read(3) * 5;
126} 76}
127 77
128#if BATTERY_TYPES_COUNT > 1 78bool charging_state(void)
129void set_battery_type(int type)
130{ 79{
131 (void)type; 80 /* cannot make this static (initializer not constant error), but gcc
81 * seems to calculate at compile time anyway */
82 const unsigned short charged_thres =
83 ((percent_to_volt_charge[9] + percent_to_volt_charge[10]) / 2);
84
85 bool ret = (power_input_status() == POWER_INPUT_MAIN_CHARGER);
86 /* dont indicate for > ~95% */
87 return ret && (battery_adc_voltage() <= charged_thres);
132} 88}
133#endif