18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * APM emulation for PMU-based machines 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2001 Benjamin Herrenschmidt (benh@kernel.crashing.org) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/apm-emulation.h> 118c2ecf20Sopenharmony_ci#include <linux/adb.h> 128c2ecf20Sopenharmony_ci#include <linux/pmu.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define APM_CRITICAL 10 158c2ecf20Sopenharmony_ci#define APM_LOW 30 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistatic void pmu_apm_get_power_status(struct apm_power_info *info) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci int percentage = -1; 208c2ecf20Sopenharmony_ci int batteries = 0; 218c2ecf20Sopenharmony_ci int time_units = -1; 228c2ecf20Sopenharmony_ci int real_count = 0; 238c2ecf20Sopenharmony_ci int i; 248c2ecf20Sopenharmony_ci char charging = 0; 258c2ecf20Sopenharmony_ci long charge = -1; 268c2ecf20Sopenharmony_ci long amperage = 0; 278c2ecf20Sopenharmony_ci unsigned long btype = 0; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci info->battery_status = APM_BATTERY_STATUS_UNKNOWN; 308c2ecf20Sopenharmony_ci info->battery_flag = APM_BATTERY_FLAG_UNKNOWN; 318c2ecf20Sopenharmony_ci info->units = APM_UNITS_MINS; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci if (pmu_power_flags & PMU_PWR_AC_PRESENT) 348c2ecf20Sopenharmony_ci info->ac_line_status = APM_AC_ONLINE; 358c2ecf20Sopenharmony_ci else 368c2ecf20Sopenharmony_ci info->ac_line_status = APM_AC_OFFLINE; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci for (i=0; i<pmu_battery_count; i++) { 398c2ecf20Sopenharmony_ci if (pmu_batteries[i].flags & PMU_BATT_PRESENT) { 408c2ecf20Sopenharmony_ci batteries++; 418c2ecf20Sopenharmony_ci if (percentage < 0) 428c2ecf20Sopenharmony_ci percentage = 0; 438c2ecf20Sopenharmony_ci if (charge < 0) 448c2ecf20Sopenharmony_ci charge = 0; 458c2ecf20Sopenharmony_ci percentage += (pmu_batteries[i].charge * 100) / 468c2ecf20Sopenharmony_ci pmu_batteries[i].max_charge; 478c2ecf20Sopenharmony_ci charge += pmu_batteries[i].charge; 488c2ecf20Sopenharmony_ci amperage += pmu_batteries[i].amperage; 498c2ecf20Sopenharmony_ci if (btype == 0) 508c2ecf20Sopenharmony_ci btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK); 518c2ecf20Sopenharmony_ci real_count++; 528c2ecf20Sopenharmony_ci if ((pmu_batteries[i].flags & PMU_BATT_CHARGING)) 538c2ecf20Sopenharmony_ci charging++; 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci if (batteries == 0) 578c2ecf20Sopenharmony_ci info->ac_line_status = APM_AC_ONLINE; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci if (real_count) { 608c2ecf20Sopenharmony_ci if (amperage < 0) { 618c2ecf20Sopenharmony_ci if (btype == PMU_BATT_TYPE_SMART) 628c2ecf20Sopenharmony_ci time_units = (charge * 59) / (amperage * -1); 638c2ecf20Sopenharmony_ci else 648c2ecf20Sopenharmony_ci time_units = (charge * 16440) / (amperage * -60); 658c2ecf20Sopenharmony_ci } 668c2ecf20Sopenharmony_ci percentage /= real_count; 678c2ecf20Sopenharmony_ci if (charging > 0) { 688c2ecf20Sopenharmony_ci info->battery_status = APM_BATTERY_STATUS_CHARGING; 698c2ecf20Sopenharmony_ci info->battery_flag = APM_BATTERY_FLAG_CHARGING; 708c2ecf20Sopenharmony_ci } else if (percentage <= APM_CRITICAL) { 718c2ecf20Sopenharmony_ci info->battery_status = APM_BATTERY_STATUS_CRITICAL; 728c2ecf20Sopenharmony_ci info->battery_flag = APM_BATTERY_FLAG_CRITICAL; 738c2ecf20Sopenharmony_ci } else if (percentage <= APM_LOW) { 748c2ecf20Sopenharmony_ci info->battery_status = APM_BATTERY_STATUS_LOW; 758c2ecf20Sopenharmony_ci info->battery_flag = APM_BATTERY_FLAG_LOW; 768c2ecf20Sopenharmony_ci } else { 778c2ecf20Sopenharmony_ci info->battery_status = APM_BATTERY_STATUS_HIGH; 788c2ecf20Sopenharmony_ci info->battery_flag = APM_BATTERY_FLAG_HIGH; 798c2ecf20Sopenharmony_ci } 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci info->battery_life = percentage; 838c2ecf20Sopenharmony_ci info->time = time_units; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic int __init apm_emu_init(void) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci apm_get_power_status = pmu_apm_get_power_status; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci printk(KERN_INFO "apm_emu: PMU APM Emulation initialized.\n"); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci return 0; 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic void __exit apm_emu_exit(void) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci if (apm_get_power_status == pmu_apm_get_power_status) 988c2ecf20Sopenharmony_ci apm_get_power_status = NULL; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci printk(KERN_INFO "apm_emu: PMU APM Emulation removed.\n"); 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cimodule_init(apm_emu_init); 1048c2ecf20Sopenharmony_cimodule_exit(apm_emu_exit); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ciMODULE_AUTHOR("Benjamin Herrenschmidt"); 1078c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("APM emulation for PowerMac"); 1088c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 109