1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * APM emulation for PMU-based machines
5 * Copyright 2001 Benjamin Herrenschmidt (benh@kernel.crashing.org)
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/apm-emulation.h>
11 #include <linux/adb.h>
12 #include <linux/pmu.h>
14 #define APM_CRITICAL 10
17 static void pmu_apm_get_power_status(struct apm_power_info
*info
)
27 unsigned long btype
= 0;
29 info
->battery_status
= APM_BATTERY_STATUS_UNKNOWN
;
30 info
->battery_flag
= APM_BATTERY_FLAG_UNKNOWN
;
31 info
->units
= APM_UNITS_MINS
;
33 if (pmu_power_flags
& PMU_PWR_AC_PRESENT
)
34 info
->ac_line_status
= APM_AC_ONLINE
;
36 info
->ac_line_status
= APM_AC_OFFLINE
;
38 for (i
=0; i
<pmu_battery_count
; i
++) {
39 if (pmu_batteries
[i
].flags
& PMU_BATT_PRESENT
) {
45 percentage
+= (pmu_batteries
[i
].charge
* 100) /
46 pmu_batteries
[i
].max_charge
;
47 charge
+= pmu_batteries
[i
].charge
;
48 amperage
+= pmu_batteries
[i
].amperage
;
50 btype
= (pmu_batteries
[i
].flags
& PMU_BATT_TYPE_MASK
);
52 if ((pmu_batteries
[i
].flags
& PMU_BATT_CHARGING
))
57 info
->ac_line_status
= APM_AC_ONLINE
;
61 if (btype
== PMU_BATT_TYPE_SMART
)
62 time_units
= (charge
* 59) / (amperage
* -1);
64 time_units
= (charge
* 16440) / (amperage
* -60);
66 percentage
/= real_count
;
68 info
->battery_status
= APM_BATTERY_STATUS_CHARGING
;
69 info
->battery_flag
= APM_BATTERY_FLAG_CHARGING
;
70 } else if (percentage
<= APM_CRITICAL
) {
71 info
->battery_status
= APM_BATTERY_STATUS_CRITICAL
;
72 info
->battery_flag
= APM_BATTERY_FLAG_CRITICAL
;
73 } else if (percentage
<= APM_LOW
) {
74 info
->battery_status
= APM_BATTERY_STATUS_LOW
;
75 info
->battery_flag
= APM_BATTERY_FLAG_LOW
;
77 info
->battery_status
= APM_BATTERY_STATUS_HIGH
;
78 info
->battery_flag
= APM_BATTERY_FLAG_HIGH
;
82 info
->battery_life
= percentage
;
83 info
->time
= time_units
;
86 static int __init
apm_emu_init(void)
88 apm_get_power_status
= pmu_apm_get_power_status
;
90 printk(KERN_INFO
"apm_emu: PMU APM Emulation initialized.\n");
95 static void __exit
apm_emu_exit(void)
97 if (apm_get_power_status
== pmu_apm_get_power_status
)
98 apm_get_power_status
= NULL
;
100 printk(KERN_INFO
"apm_emu: PMU APM Emulation removed.\n");
103 module_init(apm_emu_init
);
104 module_exit(apm_emu_exit
);
106 MODULE_AUTHOR("Benjamin Herrenschmidt");
107 MODULE_DESCRIPTION("APM emulation for PowerMac");
108 MODULE_LICENSE("GPL");