blob: 7e29b90a4f6377d51351828a8bf3daded4ad9d49 [file] [log] [blame]
Anton Vorontsov3788ec92007-05-04 00:43:24 +04001/*
2 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
3 * Copyright © 2007 Eugeny Boger <eugenyboger@dgap.mipt.ru>
4 *
5 * Author: Eugeny Boger <eugenyboger@dgap.mipt.ru>
6 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 */
11
12#include <linux/module.h>
13#include <linux/power_supply.h>
14#include <linux/apm-emulation.h>
15
Dave Young443cad92008-01-22 13:58:22 +080016static DEFINE_MUTEX(apm_mutex);
Anton Vorontsov3788ec92007-05-04 00:43:24 +040017#define PSY_PROP(psy, prop, val) psy->get_property(psy, \
18 POWER_SUPPLY_PROP_##prop, val)
19
20#define _MPSY_PROP(prop, val) main_battery->get_property(main_battery, \
21 prop, val)
22
23#define MPSY_PROP(prop, val) _MPSY_PROP(POWER_SUPPLY_PROP_##prop, val)
24
25static struct power_supply *main_battery;
26
Dave Young443cad92008-01-22 13:58:22 +080027struct find_bat_param {
28 struct power_supply *main;
29 struct power_supply *bat;
30 struct power_supply *max_charge_bat;
31 struct power_supply *max_energy_bat;
Anton Vorontsov3788ec92007-05-04 00:43:24 +040032 union power_supply_propval full;
Dave Young443cad92008-01-22 13:58:22 +080033 int max_charge;
34 int max_energy;
35};
Anton Vorontsov3788ec92007-05-04 00:43:24 +040036
Dave Young443cad92008-01-22 13:58:22 +080037static int __find_main_battery(struct device *dev, void *data)
38{
39 struct find_bat_param *bp = (struct find_bat_param *)data;
Anton Vorontsovd3853762007-10-05 01:05:00 +040040
Dave Young443cad92008-01-22 13:58:22 +080041 bp->bat = dev_get_drvdata(dev);
Anton Vorontsovd3853762007-10-05 01:05:00 +040042
Dave Young443cad92008-01-22 13:58:22 +080043 if (bp->bat->use_for_apm) {
44 /* nice, we explicitly asked to report this battery. */
45 bp->main = bp->bat;
46 return 1;
Anton Vorontsov3788ec92007-05-04 00:43:24 +040047 }
Anton Vorontsovd3853762007-10-05 01:05:00 +040048
Dave Young443cad92008-01-22 13:58:22 +080049 if (!PSY_PROP(bp->bat, CHARGE_FULL_DESIGN, &bp->full) ||
50 !PSY_PROP(bp->bat, CHARGE_FULL, &bp->full)) {
51 if (bp->full.intval > bp->max_charge) {
52 bp->max_charge_bat = bp->bat;
53 bp->max_charge = bp->full.intval;
54 }
55 } else if (!PSY_PROP(bp->bat, ENERGY_FULL_DESIGN, &bp->full) ||
56 !PSY_PROP(bp->bat, ENERGY_FULL, &bp->full)) {
57 if (bp->full.intval > bp->max_energy) {
58 bp->max_energy_bat = bp->bat;
59 bp->max_energy = bp->full.intval;
60 }
61 }
62 return 0;
63}
64
65static void find_main_battery(void)
66{
67 struct find_bat_param bp;
68 int error;
69
70 memset(&bp, 0, sizeof(struct find_bat_param));
71 main_battery = NULL;
72 bp.main = main_battery;
73
74 error = class_for_each_device(power_supply_class, &bp,
75 __find_main_battery);
76 if (error) {
77 main_battery = bp.main;
78 return;
79 }
80
81 if ((bp.max_energy_bat && bp.max_charge_bat) &&
82 (bp.max_energy_bat != bp.max_charge_bat)) {
Anton Vorontsovd3853762007-10-05 01:05:00 +040083 /* try guess battery with more capacity */
Dave Young443cad92008-01-22 13:58:22 +080084 if (!PSY_PROP(bp.max_charge_bat, VOLTAGE_MAX_DESIGN,
85 &bp.full)) {
86 if (bp.max_energy > bp.max_charge * bp.full.intval)
87 main_battery = bp.max_energy_bat;
Anton Vorontsovd3853762007-10-05 01:05:00 +040088 else
Dave Young443cad92008-01-22 13:58:22 +080089 main_battery = bp.max_charge_bat;
90 } else if (!PSY_PROP(bp.max_energy_bat, VOLTAGE_MAX_DESIGN,
91 &bp.full)) {
92 if (bp.max_charge > bp.max_energy / bp.full.intval)
93 main_battery = bp.max_charge_bat;
Anton Vorontsovd3853762007-10-05 01:05:00 +040094 else
Dave Young443cad92008-01-22 13:58:22 +080095 main_battery = bp.max_energy_bat;
Anton Vorontsovd3853762007-10-05 01:05:00 +040096 } else {
97 /* give up, choice any */
Dave Young443cad92008-01-22 13:58:22 +080098 main_battery = bp.max_energy_bat;
Anton Vorontsovd3853762007-10-05 01:05:00 +040099 }
Dave Young443cad92008-01-22 13:58:22 +0800100 } else if (bp.max_charge_bat) {
101 main_battery = bp.max_charge_bat;
102 } else if (bp.max_energy_bat) {
103 main_battery = bp.max_energy_bat;
Anton Vorontsovd3853762007-10-05 01:05:00 +0400104 } else {
105 /* give up, try the last if any */
Dave Young443cad92008-01-22 13:58:22 +0800106 main_battery = bp.bat;
Anton Vorontsovd3853762007-10-05 01:05:00 +0400107 }
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400108}
109
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400110static int calculate_time(int status, int using_charge)
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400111{
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400112 union power_supply_propval full;
113 union power_supply_propval empty;
114 union power_supply_propval cur;
115 union power_supply_propval I;
116 enum power_supply_property full_prop;
117 enum power_supply_property full_design_prop;
118 enum power_supply_property empty_prop;
119 enum power_supply_property empty_design_prop;
120 enum power_supply_property cur_avg_prop;
121 enum power_supply_property cur_now_prop;
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400122
123 if (MPSY_PROP(CURRENT_AVG, &I)) {
124 /* if battery can't report average value, use momentary */
125 if (MPSY_PROP(CURRENT_NOW, &I))
126 return -1;
127 }
128
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400129 if (using_charge) {
130 full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
131 full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
132 empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
133 empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
134 cur_avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
135 cur_now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
136 } else {
137 full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
138 full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
139 empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
140 empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
141 cur_avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
142 cur_now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
143 }
144
145 if (_MPSY_PROP(full_prop, &full)) {
146 /* if battery can't report this property, use design value */
147 if (_MPSY_PROP(full_design_prop, &full))
148 return -1;
149 }
150
151 if (_MPSY_PROP(empty_prop, &empty)) {
152 /* if battery can't report this property, use design value */
153 if (_MPSY_PROP(empty_design_prop, &empty))
154 empty.intval = 0;
155 }
156
157 if (_MPSY_PROP(cur_avg_prop, &cur)) {
158 /* if battery can't report average value, use momentary */
159 if (_MPSY_PROP(cur_now_prop, &cur))
160 return -1;
161 }
162
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400163 if (status == POWER_SUPPLY_STATUS_CHARGING)
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400164 return ((cur.intval - full.intval) * 60L) / I.intval;
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400165 else
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400166 return -((cur.intval - empty.intval) * 60L) / I.intval;
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400167}
168
169static int calculate_capacity(int using_charge)
170{
171 enum power_supply_property full_prop, empty_prop;
172 enum power_supply_property full_design_prop, empty_design_prop;
173 enum power_supply_property now_prop, avg_prop;
174 union power_supply_propval empty, full, cur;
175 int ret;
176
177 if (using_charge) {
178 full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
179 empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
180 full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
181 empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
182 now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
183 avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
184 } else {
185 full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
186 empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
187 full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
188 empty_design_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
189 now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
190 avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
191 }
192
193 if (_MPSY_PROP(full_prop, &full)) {
194 /* if battery can't report this property, use design value */
195 if (_MPSY_PROP(full_design_prop, &full))
196 return -1;
197 }
198
199 if (_MPSY_PROP(avg_prop, &cur)) {
200 /* if battery can't report average value, use momentary */
201 if (_MPSY_PROP(now_prop, &cur))
202 return -1;
203 }
204
205 if (_MPSY_PROP(empty_prop, &empty)) {
206 /* if battery can't report this property, use design value */
207 if (_MPSY_PROP(empty_design_prop, &empty))
208 empty.intval = 0;
209 }
210
211 if (full.intval - empty.intval)
212 ret = ((cur.intval - empty.intval) * 100L) /
213 (full.intval - empty.intval);
214 else
215 return -1;
216
217 if (ret > 100)
218 return 100;
219 else if (ret < 0)
220 return 0;
221
222 return ret;
223}
224
225static void apm_battery_apm_get_power_status(struct apm_power_info *info)
226{
227 union power_supply_propval status;
228 union power_supply_propval capacity, time_to_full, time_to_empty;
229
Dave Young443cad92008-01-22 13:58:22 +0800230 mutex_lock(&apm_mutex);
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400231 find_main_battery();
232 if (!main_battery) {
Dave Young443cad92008-01-22 13:58:22 +0800233 mutex_unlock(&apm_mutex);
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400234 return;
235 }
236
237 /* status */
238
239 if (MPSY_PROP(STATUS, &status))
240 status.intval = POWER_SUPPLY_STATUS_UNKNOWN;
241
242 /* ac line status */
243
244 if ((status.intval == POWER_SUPPLY_STATUS_CHARGING) ||
245 (status.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) ||
246 (status.intval == POWER_SUPPLY_STATUS_FULL))
247 info->ac_line_status = APM_AC_ONLINE;
248 else
249 info->ac_line_status = APM_AC_OFFLINE;
250
251 /* battery life (i.e. capacity, in percents) */
252
253 if (MPSY_PROP(CAPACITY, &capacity) == 0) {
254 info->battery_life = capacity.intval;
255 } else {
256 /* try calculate using energy */
257 info->battery_life = calculate_capacity(0);
258 /* if failed try calculate using charge instead */
259 if (info->battery_life == -1)
260 info->battery_life = calculate_capacity(1);
261 }
262
263 /* charging status */
264
265 if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
266 info->battery_status = APM_BATTERY_STATUS_CHARGING;
267 } else {
268 if (info->battery_life > 50)
269 info->battery_status = APM_BATTERY_STATUS_HIGH;
270 else if (info->battery_life > 5)
271 info->battery_status = APM_BATTERY_STATUS_LOW;
272 else
273 info->battery_status = APM_BATTERY_STATUS_CRITICAL;
274 }
275 info->battery_flag = info->battery_status;
276
277 /* time */
278
279 info->units = APM_UNITS_MINS;
280
281 if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
Anton Vorontsovcd1ebcc2007-10-05 01:04:59 +0400282 if (!MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full) ||
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400283 !MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full)) {
Anton Vorontsovcd1ebcc2007-10-05 01:04:59 +0400284 info->time = time_to_full.intval / 60;
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400285 } else {
286 info->time = calculate_time(status.intval, 0);
287 if (info->time == -1)
288 info->time = calculate_time(status.intval, 1);
289 }
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400290 } else {
Anton Vorontsovcd1ebcc2007-10-05 01:04:59 +0400291 if (!MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty) ||
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400292 !MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty)) {
Anton Vorontsovcd1ebcc2007-10-05 01:04:59 +0400293 info->time = time_to_empty.intval / 60;
Anton Vorontsov2a721df2007-10-05 01:05:00 +0400294 } else {
295 info->time = calculate_time(status.intval, 0);
296 if (info->time == -1)
297 info->time = calculate_time(status.intval, 1);
298 }
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400299 }
300
Dave Young443cad92008-01-22 13:58:22 +0800301 mutex_unlock(&apm_mutex);
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400302}
303
304static int __init apm_battery_init(void)
305{
306 printk(KERN_INFO "APM Battery Driver\n");
307
308 apm_get_power_status = apm_battery_apm_get_power_status;
309 return 0;
310}
311
312static void __exit apm_battery_exit(void)
313{
314 apm_get_power_status = NULL;
Anton Vorontsov3788ec92007-05-04 00:43:24 +0400315}
316
317module_init(apm_battery_init);
318module_exit(apm_battery_exit);
319
320MODULE_AUTHOR("Eugeny Boger <eugenyboger@dgap.mipt.ru>");
321MODULE_DESCRIPTION("APM emulation driver for battery monitoring class");
322MODULE_LICENSE("GPL");