blob: 9821e6361e60bcb4e9c16e5712823a2ca66f5253 [file] [log] [blame]
Johannes Bergb3028872007-03-20 05:18:02 +11001/*
2 * APM emulation for PMU-based machines
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Johannes Bergb3028872007-03-20 05:18:02 +11004 * Copyright 2001 Benjamin Herrenschmidt (benh@kernel.crashing.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 *
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Johannes Bergb3028872007-03-20 05:18:02 +110020#include <linux/module.h>
21#include <linux/apm-emulation.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/adb.h>
23#include <linux/pmu.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define APM_CRITICAL 10
26#define APM_LOW 30
27
Johannes Bergb3028872007-03-20 05:18:02 +110028static void pmu_apm_get_power_status(struct apm_power_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Johannes Bergb3028872007-03-20 05:18:02 +110030 int percentage = -1;
31 int batteries = 0;
32 int time_units = -1;
33 int real_count = 0;
34 int i;
35 char charging = 0;
36 long charge = -1;
37 long amperage = 0;
38 unsigned long btype = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Johannes Bergb3028872007-03-20 05:18:02 +110040 info->battery_status = APM_BATTERY_STATUS_UNKNOWN;
41 info->battery_flag = APM_BATTERY_FLAG_UNKNOWN;
42 info->units = APM_UNITS_MINS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Johannes Bergb3028872007-03-20 05:18:02 +110044 if (pmu_power_flags & PMU_PWR_AC_PRESENT)
45 info->ac_line_status = APM_AC_ONLINE;
46 else
47 info->ac_line_status = APM_AC_OFFLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 for (i=0; i<pmu_battery_count; i++) {
50 if (pmu_batteries[i].flags & PMU_BATT_PRESENT) {
Johannes Bergb3028872007-03-20 05:18:02 +110051 batteries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (percentage < 0)
53 percentage = 0;
54 if (charge < 0)
55 charge = 0;
56 percentage += (pmu_batteries[i].charge * 100) /
57 pmu_batteries[i].max_charge;
58 charge += pmu_batteries[i].charge;
59 amperage += pmu_batteries[i].amperage;
60 if (btype == 0)
61 btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK);
62 real_count++;
63 if ((pmu_batteries[i].flags & PMU_BATT_CHARGING))
64 charging++;
65 }
66 }
Johannes Bergb3028872007-03-20 05:18:02 +110067 if (batteries == 0)
68 info->ac_line_status = APM_AC_ONLINE;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (real_count) {
71 if (amperage < 0) {
72 if (btype == PMU_BATT_TYPE_SMART)
73 time_units = (charge * 59) / (amperage * -1);
74 else
75 time_units = (charge * 16440) / (amperage * -60);
76 }
77 percentage /= real_count;
78 if (charging > 0) {
Johannes Bergb3028872007-03-20 05:18:02 +110079 info->battery_status = APM_BATTERY_STATUS_CHARGING;
80 info->battery_flag = APM_BATTERY_FLAG_CHARGING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 } else if (percentage <= APM_CRITICAL) {
Johannes Bergb3028872007-03-20 05:18:02 +110082 info->battery_status = APM_BATTERY_STATUS_CRITICAL;
83 info->battery_flag = APM_BATTERY_FLAG_CRITICAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 } else if (percentage <= APM_LOW) {
Johannes Bergb3028872007-03-20 05:18:02 +110085 info->battery_status = APM_BATTERY_STATUS_LOW;
86 info->battery_flag = APM_BATTERY_FLAG_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 } else {
Johannes Bergb3028872007-03-20 05:18:02 +110088 info->battery_status = APM_BATTERY_STATUS_HIGH;
89 info->battery_flag = APM_BATTERY_FLAG_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Johannes Bergb3028872007-03-20 05:18:02 +110093 info->battery_life = percentage;
94 info->time = time_units;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int __init apm_emu_init(void)
98{
Johannes Bergb3028872007-03-20 05:18:02 +110099 apm_get_power_status = pmu_apm_get_power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Johannes Bergb3028872007-03-20 05:18:02 +1100101 printk(KERN_INFO "apm_emu: PMU APM Emulation initialized.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 return 0;
104}
105
106static void __exit apm_emu_exit(void)
107{
Johannes Bergb3028872007-03-20 05:18:02 +1100108 if (apm_get_power_status == pmu_apm_get_power_status)
109 apm_get_power_status = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Johannes Bergb3028872007-03-20 05:18:02 +1100111 printk(KERN_INFO "apm_emu: PMU APM Emulation removed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114module_init(apm_emu_init);
115module_exit(apm_emu_exit);
116
117MODULE_AUTHOR("Benjamin Herrenschmidt");
Johannes Bergb3028872007-03-20 05:18:02 +1100118MODULE_DESCRIPTION("APM emulation for PowerMac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119MODULE_LICENSE("GPL");