apm_emu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * APM emulation for PMU-based machines
  3. *
  4. * Copyright 2001 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/apm-emulation.h>
  21. #include <linux/adb.h>
  22. #include <linux/pmu.h>
  23. #define APM_CRITICAL 10
  24. #define APM_LOW 30
  25. static void pmu_apm_get_power_status(struct apm_power_info *info)
  26. {
  27. int percentage = -1;
  28. int batteries = 0;
  29. int time_units = -1;
  30. int real_count = 0;
  31. int i;
  32. char charging = 0;
  33. long charge = -1;
  34. long amperage = 0;
  35. unsigned long btype = 0;
  36. info->battery_status = APM_BATTERY_STATUS_UNKNOWN;
  37. info->battery_flag = APM_BATTERY_FLAG_UNKNOWN;
  38. info->units = APM_UNITS_MINS;
  39. if (pmu_power_flags & PMU_PWR_AC_PRESENT)
  40. info->ac_line_status = APM_AC_ONLINE;
  41. else
  42. info->ac_line_status = APM_AC_OFFLINE;
  43. for (i=0; i<pmu_battery_count; i++) {
  44. if (pmu_batteries[i].flags & PMU_BATT_PRESENT) {
  45. batteries++;
  46. if (percentage < 0)
  47. percentage = 0;
  48. if (charge < 0)
  49. charge = 0;
  50. percentage += (pmu_batteries[i].charge * 100) /
  51. pmu_batteries[i].max_charge;
  52. charge += pmu_batteries[i].charge;
  53. amperage += pmu_batteries[i].amperage;
  54. if (btype == 0)
  55. btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK);
  56. real_count++;
  57. if ((pmu_batteries[i].flags & PMU_BATT_CHARGING))
  58. charging++;
  59. }
  60. }
  61. if (batteries == 0)
  62. info->ac_line_status = APM_AC_ONLINE;
  63. if (real_count) {
  64. if (amperage < 0) {
  65. if (btype == PMU_BATT_TYPE_SMART)
  66. time_units = (charge * 59) / (amperage * -1);
  67. else
  68. time_units = (charge * 16440) / (amperage * -60);
  69. }
  70. percentage /= real_count;
  71. if (charging > 0) {
  72. info->battery_status = APM_BATTERY_STATUS_CHARGING;
  73. info->battery_flag = APM_BATTERY_FLAG_CHARGING;
  74. } else if (percentage <= APM_CRITICAL) {
  75. info->battery_status = APM_BATTERY_STATUS_CRITICAL;
  76. info->battery_flag = APM_BATTERY_FLAG_CRITICAL;
  77. } else if (percentage <= APM_LOW) {
  78. info->battery_status = APM_BATTERY_STATUS_LOW;
  79. info->battery_flag = APM_BATTERY_FLAG_LOW;
  80. } else {
  81. info->battery_status = APM_BATTERY_STATUS_HIGH;
  82. info->battery_flag = APM_BATTERY_FLAG_HIGH;
  83. }
  84. }
  85. info->battery_life = percentage;
  86. info->time = time_units;
  87. }
  88. static int __init apm_emu_init(void)
  89. {
  90. apm_get_power_status = pmu_apm_get_power_status;
  91. printk(KERN_INFO "apm_emu: PMU APM Emulation initialized.\n");
  92. return 0;
  93. }
  94. static void __exit apm_emu_exit(void)
  95. {
  96. if (apm_get_power_status == pmu_apm_get_power_status)
  97. apm_get_power_status = NULL;
  98. printk(KERN_INFO "apm_emu: PMU APM Emulation removed.\n");
  99. }
  100. module_init(apm_emu_init);
  101. module_exit(apm_emu_exit);
  102. MODULE_AUTHOR("Benjamin Herrenschmidt");
  103. MODULE_DESCRIPTION("APM emulation for PowerMac");
  104. MODULE_LICENSE("GPL");