blob: 5ed0ac65cf5915602c4c45afe414b722a190d377 [file] [log] [blame]
Thomas Gleixner3e0a4e82019-05-23 11:14:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Johannes Bergb3028872007-03-20 05:18:02 +11002/*
3 * APM emulation for PMU-based machines
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Johannes Bergb3028872007-03-20 05:18:02 +11005 * Copyright 2001 Benjamin Herrenschmidt (benh@kernel.crashing.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
Johannes Bergb3028872007-03-20 05:18:02 +11009#include <linux/module.h>
10#include <linux/apm-emulation.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/adb.h>
12#include <linux/pmu.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define APM_CRITICAL 10
15#define APM_LOW 30
16
Johannes Bergb3028872007-03-20 05:18:02 +110017static void pmu_apm_get_power_status(struct apm_power_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Johannes Bergb3028872007-03-20 05:18:02 +110019 int percentage = -1;
20 int batteries = 0;
21 int time_units = -1;
22 int real_count = 0;
23 int i;
24 char charging = 0;
25 long charge = -1;
26 long amperage = 0;
27 unsigned long btype = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Johannes Bergb3028872007-03-20 05:18:02 +110029 info->battery_status = APM_BATTERY_STATUS_UNKNOWN;
30 info->battery_flag = APM_BATTERY_FLAG_UNKNOWN;
31 info->units = APM_UNITS_MINS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Johannes Bergb3028872007-03-20 05:18:02 +110033 if (pmu_power_flags & PMU_PWR_AC_PRESENT)
34 info->ac_line_status = APM_AC_ONLINE;
35 else
36 info->ac_line_status = APM_AC_OFFLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 for (i=0; i<pmu_battery_count; i++) {
39 if (pmu_batteries[i].flags & PMU_BATT_PRESENT) {
Johannes Bergb3028872007-03-20 05:18:02 +110040 batteries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (percentage < 0)
42 percentage = 0;
43 if (charge < 0)
44 charge = 0;
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;
49 if (btype == 0)
50 btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK);
51 real_count++;
52 if ((pmu_batteries[i].flags & PMU_BATT_CHARGING))
53 charging++;
54 }
55 }
Johannes Bergb3028872007-03-20 05:18:02 +110056 if (batteries == 0)
57 info->ac_line_status = APM_AC_ONLINE;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (real_count) {
60 if (amperage < 0) {
61 if (btype == PMU_BATT_TYPE_SMART)
62 time_units = (charge * 59) / (amperage * -1);
63 else
64 time_units = (charge * 16440) / (amperage * -60);
65 }
66 percentage /= real_count;
67 if (charging > 0) {
Johannes Bergb3028872007-03-20 05:18:02 +110068 info->battery_status = APM_BATTERY_STATUS_CHARGING;
69 info->battery_flag = APM_BATTERY_FLAG_CHARGING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 } else if (percentage <= APM_CRITICAL) {
Johannes Bergb3028872007-03-20 05:18:02 +110071 info->battery_status = APM_BATTERY_STATUS_CRITICAL;
72 info->battery_flag = APM_BATTERY_FLAG_CRITICAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 } else if (percentage <= APM_LOW) {
Johannes Bergb3028872007-03-20 05:18:02 +110074 info->battery_status = APM_BATTERY_STATUS_LOW;
75 info->battery_flag = APM_BATTERY_FLAG_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 } else {
Johannes Bergb3028872007-03-20 05:18:02 +110077 info->battery_status = APM_BATTERY_STATUS_HIGH;
78 info->battery_flag = APM_BATTERY_FLAG_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Johannes Bergb3028872007-03-20 05:18:02 +110082 info->battery_life = percentage;
83 info->time = time_units;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int __init apm_emu_init(void)
87{
Johannes Bergb3028872007-03-20 05:18:02 +110088 apm_get_power_status = pmu_apm_get_power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Johannes Bergb3028872007-03-20 05:18:02 +110090 printk(KERN_INFO "apm_emu: PMU APM Emulation initialized.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 return 0;
93}
94
95static void __exit apm_emu_exit(void)
96{
Johannes Bergb3028872007-03-20 05:18:02 +110097 if (apm_get_power_status == pmu_apm_get_power_status)
98 apm_get_power_status = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Johannes Bergb3028872007-03-20 05:18:02 +1100100 printk(KERN_INFO "apm_emu: PMU APM Emulation removed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103module_init(apm_emu_init);
104module_exit(apm_emu_exit);
105
106MODULE_AUTHOR("Benjamin Herrenschmidt");
Johannes Bergb3028872007-03-20 05:18:02 +1100107MODULE_DESCRIPTION("APM emulation for PowerMac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108MODULE_LICENSE("GPL");