apm_power.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
  3. * Copyright © 2007 Eugeny Boger <eugenyboger@dgap.mipt.ru>
  4. *
  5. * Author: Eugeny Boger <eugenyboger@dgap.mipt.ru>
  6. *
  7. * Use consistent with the GNU GPL is permitted,
  8. * provided that this copyright notice is
  9. * preserved in its entirety in all copies and derived works.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/power_supply.h>
  13. #include <linux/apm-emulation.h>
  14. #define PSY_PROP(psy, prop, val) psy->get_property(psy, \
  15. POWER_SUPPLY_PROP_##prop, val)
  16. #define _MPSY_PROP(prop, val) main_battery->get_property(main_battery, \
  17. prop, val)
  18. #define MPSY_PROP(prop, val) _MPSY_PROP(POWER_SUPPLY_PROP_##prop, val)
  19. static struct power_supply *main_battery;
  20. static void find_main_battery(void)
  21. {
  22. struct device *dev;
  23. struct power_supply *bat, *batm;
  24. union power_supply_propval full;
  25. int max_charge = 0;
  26. main_battery = NULL;
  27. batm = NULL;
  28. list_for_each_entry(dev, &power_supply_class->devices, node) {
  29. bat = dev_get_drvdata(dev);
  30. /* If none of battery devices cantains 'use_for_apm' flag,
  31. choice one with maximum design charge */
  32. if (!PSY_PROP(bat, CHARGE_FULL_DESIGN, &full)) {
  33. if (full.intval > max_charge) {
  34. batm = bat;
  35. max_charge = full.intval;
  36. }
  37. }
  38. if (bat->use_for_apm)
  39. main_battery = bat;
  40. }
  41. if (!main_battery)
  42. main_battery = batm;
  43. return;
  44. }
  45. static int calculate_time(int status)
  46. {
  47. union power_supply_propval charge_full, charge_empty;
  48. union power_supply_propval charge, I;
  49. if (MPSY_PROP(CHARGE_FULL, &charge_full)) {
  50. /* if battery can't report this property, use design value */
  51. if (MPSY_PROP(CHARGE_FULL_DESIGN, &charge_full))
  52. return -1;
  53. }
  54. if (MPSY_PROP(CHARGE_EMPTY, &charge_empty)) {
  55. /* if battery can't report this property, use design value */
  56. if (MPSY_PROP(CHARGE_EMPTY_DESIGN, &charge_empty))
  57. charge_empty.intval = 0;
  58. }
  59. if (MPSY_PROP(CHARGE_AVG, &charge)) {
  60. /* if battery can't report average value, use momentary */
  61. if (MPSY_PROP(CHARGE_NOW, &charge))
  62. return -1;
  63. }
  64. if (MPSY_PROP(CURRENT_AVG, &I)) {
  65. /* if battery can't report average value, use momentary */
  66. if (MPSY_PROP(CURRENT_NOW, &I))
  67. return -1;
  68. }
  69. if (status == POWER_SUPPLY_STATUS_CHARGING)
  70. return ((charge.intval - charge_full.intval) * 60L) /
  71. I.intval;
  72. else
  73. return -((charge.intval - charge_empty.intval) * 60L) /
  74. I.intval;
  75. }
  76. static int calculate_capacity(int using_charge)
  77. {
  78. enum power_supply_property full_prop, empty_prop;
  79. enum power_supply_property full_design_prop, empty_design_prop;
  80. enum power_supply_property now_prop, avg_prop;
  81. union power_supply_propval empty, full, cur;
  82. int ret;
  83. if (using_charge) {
  84. full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
  85. empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
  86. full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
  87. empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
  88. now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
  89. avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
  90. } else {
  91. full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
  92. empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
  93. full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
  94. empty_design_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
  95. now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
  96. avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
  97. }
  98. if (_MPSY_PROP(full_prop, &full)) {
  99. /* if battery can't report this property, use design value */
  100. if (_MPSY_PROP(full_design_prop, &full))
  101. return -1;
  102. }
  103. if (_MPSY_PROP(avg_prop, &cur)) {
  104. /* if battery can't report average value, use momentary */
  105. if (_MPSY_PROP(now_prop, &cur))
  106. return -1;
  107. }
  108. if (_MPSY_PROP(empty_prop, &empty)) {
  109. /* if battery can't report this property, use design value */
  110. if (_MPSY_PROP(empty_design_prop, &empty))
  111. empty.intval = 0;
  112. }
  113. if (full.intval - empty.intval)
  114. ret = ((cur.intval - empty.intval) * 100L) /
  115. (full.intval - empty.intval);
  116. else
  117. return -1;
  118. if (ret > 100)
  119. return 100;
  120. else if (ret < 0)
  121. return 0;
  122. return ret;
  123. }
  124. static void apm_battery_apm_get_power_status(struct apm_power_info *info)
  125. {
  126. union power_supply_propval status;
  127. union power_supply_propval capacity, time_to_full, time_to_empty;
  128. down(&power_supply_class->sem);
  129. find_main_battery();
  130. if (!main_battery) {
  131. up(&power_supply_class->sem);
  132. return;
  133. }
  134. /* status */
  135. if (MPSY_PROP(STATUS, &status))
  136. status.intval = POWER_SUPPLY_STATUS_UNKNOWN;
  137. /* ac line status */
  138. if ((status.intval == POWER_SUPPLY_STATUS_CHARGING) ||
  139. (status.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) ||
  140. (status.intval == POWER_SUPPLY_STATUS_FULL))
  141. info->ac_line_status = APM_AC_ONLINE;
  142. else
  143. info->ac_line_status = APM_AC_OFFLINE;
  144. /* battery life (i.e. capacity, in percents) */
  145. if (MPSY_PROP(CAPACITY, &capacity) == 0) {
  146. info->battery_life = capacity.intval;
  147. } else {
  148. /* try calculate using energy */
  149. info->battery_life = calculate_capacity(0);
  150. /* if failed try calculate using charge instead */
  151. if (info->battery_life == -1)
  152. info->battery_life = calculate_capacity(1);
  153. }
  154. /* charging status */
  155. if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
  156. info->battery_status = APM_BATTERY_STATUS_CHARGING;
  157. } else {
  158. if (info->battery_life > 50)
  159. info->battery_status = APM_BATTERY_STATUS_HIGH;
  160. else if (info->battery_life > 5)
  161. info->battery_status = APM_BATTERY_STATUS_LOW;
  162. else
  163. info->battery_status = APM_BATTERY_STATUS_CRITICAL;
  164. }
  165. info->battery_flag = info->battery_status;
  166. /* time */
  167. info->units = APM_UNITS_MINS;
  168. if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
  169. if (MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full)) {
  170. if (MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full))
  171. info->time = calculate_time(status.intval);
  172. else
  173. info->time = time_to_full.intval / 60;
  174. }
  175. } else {
  176. if (MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty)) {
  177. if (MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty))
  178. info->time = calculate_time(status.intval);
  179. else
  180. info->time = time_to_empty.intval / 60;
  181. }
  182. }
  183. up(&power_supply_class->sem);
  184. return;
  185. }
  186. static int __init apm_battery_init(void)
  187. {
  188. printk(KERN_INFO "APM Battery Driver\n");
  189. apm_get_power_status = apm_battery_apm_get_power_status;
  190. return 0;
  191. }
  192. static void __exit apm_battery_exit(void)
  193. {
  194. apm_get_power_status = NULL;
  195. return;
  196. }
  197. module_init(apm_battery_init);
  198. module_exit(apm_battery_exit);
  199. MODULE_AUTHOR("Eugeny Boger <eugenyboger@dgap.mipt.ru>");
  200. MODULE_DESCRIPTION("APM emulation driver for battery monitoring class");
  201. MODULE_LICENSE("GPL");