merge of '0b604857bbf871639fdb43ee8380222e8ef64bb7'
[vuplus_openembedded] / packages / linux / linux-rp-2.6.24 / tosa / 0029-Support-using-VOLTAGE_-properties-for-apm-calculati.patch
1 From 57d1450b4e5f27fa78c75895dc30213bde7191bc Mon Sep 17 00:00:00 2001
2 From: Dmitry Baryshkov <dbaryshkov@gmail.com>
3 Date: Wed, 9 Jan 2008 02:08:18 +0300
4 Subject: [PATCH 29/64] Support using VOLTAGE_* properties for apm calculations. It's pretty
5  dummy, but useful for batteries for which we can only get voltages.
6
7 ---
8  drivers/power/apm_power.c |   63 ++++++++++++++++++++++++++++++++++++--------
9  1 files changed, 51 insertions(+), 12 deletions(-)
10
11 diff --git a/drivers/power/apm_power.c b/drivers/power/apm_power.c
12 index bbf3ee1..526c96e 100644
13 --- a/drivers/power/apm_power.c
14 +++ b/drivers/power/apm_power.c
15 @@ -13,6 +13,12 @@
16  #include <linux/power_supply.h>
17  #include <linux/apm-emulation.h>
18  
19 +typedef enum {
20 +       SOURCE_ENERGY,
21 +       SOURCE_CHARGE,
22 +       SOURCE_VOLTAGE,
23 +} apm_source;
24 +
25  #define PSY_PROP(psy, prop, val) psy->get_property(psy, \
26                          POWER_SUPPLY_PROP_##prop, val)
27  
28 @@ -87,7 +93,7 @@ static void find_main_battery(void)
29         }
30  }
31  
32 -static int calculate_time(int status, int using_charge)
33 +static int calculate_time(int status, apm_source source)
34  {
35         union power_supply_propval full;
36         union power_supply_propval empty;
37 @@ -106,20 +112,34 @@ static int calculate_time(int status, int using_charge)
38                         return -1;
39         }
40  
41 -       if (using_charge) {
42 +       switch (source) {
43 +       case SOURCE_CHARGE:
44                 full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
45                 full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
46                 empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
47                 empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
48                 cur_avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
49                 cur_now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
50 -       } else {
51 +               break;
52 +       case SOURCE_ENERGY:
53                 full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
54                 full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
55                 empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
56                 empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
57                 cur_avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
58                 cur_now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
59 +               break;
60 +       case SOURCE_VOLTAGE:
61 +               full_prop = POWER_SUPPLY_PROP_VOLTAGE_MAX;
62 +               full_design_prop = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
63 +               empty_prop = POWER_SUPPLY_PROP_VOLTAGE_MIN;
64 +               empty_design_prop = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
65 +               cur_avg_prop = POWER_SUPPLY_PROP_VOLTAGE_AVG;
66 +               cur_now_prop = POWER_SUPPLY_PROP_VOLTAGE_NOW;
67 +               break;
68 +       default:
69 +               printk(KERN_ERR "Unsupported source: %d\n", source);
70 +               return -1;
71         }
72  
73         if (_MPSY_PROP(full_prop, &full)) {
74 @@ -146,7 +166,7 @@ static int calculate_time(int status, int using_charge)
75                 return -((cur.intval - empty.intval) * 60L) / I.intval;
76  }
77  
78 -static int calculate_capacity(int using_charge)
79 +static int calculate_capacity(apm_source source)
80  {
81         enum power_supply_property full_prop, empty_prop;
82         enum power_supply_property full_design_prop, empty_design_prop;
83 @@ -154,20 +174,33 @@ static int calculate_capacity(int using_charge)
84         union power_supply_propval empty, full, cur;
85         int ret;
86  
87 -       if (using_charge) {
88 +       switch (source) {
89 +       case SOURCE_CHARGE:
90                 full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
91                 empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
92                 full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
93                 empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
94                 now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
95                 avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
96 -       } else {
97 +               break;
98 +       case SOURCE_ENERGY:
99                 full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
100                 empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
101                 full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
102                 empty_design_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
103                 now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
104                 avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
105 +       case SOURCE_VOLTAGE:
106 +               full_prop = POWER_SUPPLY_PROP_VOLTAGE_MAX;
107 +               empty_prop = POWER_SUPPLY_PROP_VOLTAGE_MIN;
108 +               full_design_prop = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN;
109 +               empty_design_prop = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN;
110 +               now_prop = POWER_SUPPLY_PROP_VOLTAGE_NOW;
111 +               avg_prop = POWER_SUPPLY_PROP_VOLTAGE_AVG;
112 +               break;
113 +       default:
114 +               printk(KERN_ERR "Unsupported source: %d\n", source);
115 +               return -1;
116         }
117  
118         if (_MPSY_PROP(full_prop, &full)) {
119 @@ -234,10 +267,12 @@ static void apm_battery_apm_get_power_status(struct apm_power_info *info)
120                 info->battery_life = capacity.intval;
121         } else {
122                 /* try calculate using energy */
123 -               info->battery_life = calculate_capacity(0);
124 +               info->battery_life = calculate_capacity(SOURCE_ENERGY);
125                 /* if failed try calculate using charge instead */
126                 if (info->battery_life == -1)
127 -                       info->battery_life = calculate_capacity(1);
128 +                       info->battery_life = calculate_capacity(SOURCE_CHARGE);
129 +               if (info->battery_life == -1)
130 +                       info->battery_life = calculate_capacity(SOURCE_VOLTAGE);
131         }
132  
133         /* charging status */
134 @@ -263,18 +298,22 @@ static void apm_battery_apm_get_power_status(struct apm_power_info *info)
135                                 !MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full)) {
136                         info->time = time_to_full.intval / 60;
137                 } else {
138 -                       info->time = calculate_time(status.intval, 0);
139 +                       info->time = calculate_time(status.intval, SOURCE_ENERGY);
140                         if (info->time == -1)
141 -                               info->time = calculate_time(status.intval, 1);
142 +                               info->time = calculate_time(status.intval, SOURCE_CHARGE);
143 +                       if (info->time == -1)
144 +                               info->time = calculate_time(status.intval, SOURCE_VOLTAGE);
145                 }
146         } else {
147                 if (!MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty) ||
148                               !MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty)) {
149                         info->time = time_to_empty.intval / 60;
150                 } else {
151 -                       info->time = calculate_time(status.intval, 0);
152 +                       info->time = calculate_time(status.intval, SOURCE_ENERGY);
153 +                       if (info->time == -1)
154 +                               info->time = calculate_time(status.intval, SOURCE_CHARGE);
155                         if (info->time == -1)
156 -                               info->time = calculate_time(status.intval, 1);
157 +                               info->time = calculate_time(status.intval, SOURCE_VOLTAGE);
158                 }
159         }
160  
161 -- 
162 1.5.3.8
163