^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * APM emulation for PMU-based machines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2001 Benjamin Herrenschmidt (benh@kernel.crashing.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/apm-emulation.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/adb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define APM_CRITICAL 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define APM_LOW 30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static void pmu_apm_get_power_status(struct apm_power_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int percentage = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int batteries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int time_units = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int real_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) char charging = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) long charge = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) long amperage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long btype = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) info->battery_status = APM_BATTERY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) info->battery_flag = APM_BATTERY_FLAG_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) info->units = APM_UNITS_MINS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (pmu_power_flags & PMU_PWR_AC_PRESENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) info->ac_line_status = APM_AC_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) info->ac_line_status = APM_AC_OFFLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) for (i=0; i<pmu_battery_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (pmu_batteries[i].flags & PMU_BATT_PRESENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) batteries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (percentage < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) percentage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (charge < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) charge = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) percentage += (pmu_batteries[i].charge * 100) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pmu_batteries[i].max_charge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) charge += pmu_batteries[i].charge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) amperage += pmu_batteries[i].amperage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (btype == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) real_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if ((pmu_batteries[i].flags & PMU_BATT_CHARGING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) charging++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (batteries == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) info->ac_line_status = APM_AC_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (real_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (amperage < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (btype == PMU_BATT_TYPE_SMART)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) time_units = (charge * 59) / (amperage * -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) time_units = (charge * 16440) / (amperage * -60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) percentage /= real_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (charging > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) info->battery_status = APM_BATTERY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) info->battery_flag = APM_BATTERY_FLAG_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) } else if (percentage <= APM_CRITICAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) info->battery_status = APM_BATTERY_STATUS_CRITICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) info->battery_flag = APM_BATTERY_FLAG_CRITICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) } else if (percentage <= APM_LOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) info->battery_status = APM_BATTERY_STATUS_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) info->battery_flag = APM_BATTERY_FLAG_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) info->battery_status = APM_BATTERY_STATUS_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) info->battery_flag = APM_BATTERY_FLAG_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) info->battery_life = percentage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) info->time = time_units;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int __init apm_emu_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) apm_get_power_status = pmu_apm_get_power_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) printk(KERN_INFO "apm_emu: PMU APM Emulation initialized.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static void __exit apm_emu_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (apm_get_power_status == pmu_apm_get_power_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) apm_get_power_status = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) printk(KERN_INFO "apm_emu: PMU APM Emulation removed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) module_init(apm_emu_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) module_exit(apm_emu_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) MODULE_AUTHOR("Benjamin Herrenschmidt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) MODULE_DESCRIPTION("APM emulation for PowerMac");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) MODULE_LICENSE("GPL");