apm_emu.c 2.7 KB

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