apm_power.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  44. static int calculate_time(int status)
  45. {
  46. union power_supply_propval charge_full, charge_empty;
  47. union power_supply_propval charge, I;
  48. if (MPSY_PROP(CHARGE_FULL, &charge_full)) {
  49. /* if battery can't report this property, use design value */
  50. if (MPSY_PROP(CHARGE_FULL_DESIGN, &charge_full))
  51. return -1;
  52. }
  53. if (MPSY_PROP(CHARGE_EMPTY, &charge_empty)) {
  54. /* if battery can't report this property, use design value */
  55. if (MPSY_PROP(CHARGE_EMPTY_DESIGN, &charge_empty))
  56. charge_empty.intval = 0;
  57. }
  58. if (MPSY_PROP(CHARGE_AVG, &charge)) {
  59. /* if battery can't report average value, use momentary */
  60. if (MPSY_PROP(CHARGE_NOW, &charge))
  61. return -1;
  62. }
  63. if (MPSY_PROP(CURRENT_AVG, &I)) {
  64. /* if battery can't report average value, use momentary */
  65. if (MPSY_PROP(CURRENT_NOW, &I))
  66. return -1;
  67. }
  68. if (status == POWER_SUPPLY_STATUS_CHARGING)
  69. return ((charge.intval - charge_full.intval) * 60L) /
  70. I.intval;
  71. else
  72. return -((charge.intval - charge_empty.intval) * 60L) /
  73. I.intval;
  74. }
  75. static int calculate_capacity(int using_charge)
  76. {
  77. enum power_supply_property full_prop, empty_prop;
  78. enum power_supply_property full_design_prop, empty_design_prop;
  79. enum power_supply_property now_prop, avg_prop;
  80. union power_supply_propval empty, full, cur;
  81. int ret;
  82. if (using_charge) {
  83. full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
  84. empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
  85. full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
  86. empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
  87. now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
  88. avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
  89. } else {
  90. full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
  91. empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
  92. full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
  93. empty_design_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
  94. now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
  95. avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
  96. }
  97. if (_MPSY_PROP(full_prop, &full)) {
  98. /* if battery can't report this property, use design value */
  99. if (_MPSY_PROP(full_design_prop, &full))
  100. return -1;
  101. }
  102. if (_MPSY_PROP(avg_prop, &cur)) {
  103. /* if battery can't report average value, use momentary */
  104. if (_MPSY_PROP(now_prop, &cur))
  105. return -1;
  106. }
  107. if (_MPSY_PROP(empty_prop, &empty)) {
  108. /* if battery can't report this property, use design value */
  109. if (_MPSY_PROP(empty_design_prop, &empty))
  110. empty.intval = 0;
  111. }
  112. if (full.intval - empty.intval)
  113. ret = ((cur.intval - empty.intval) * 100L) /
  114. (full.intval - empty.intval);
  115. else
  116. return -1;
  117. if (ret > 100)
  118. return 100;
  119. else if (ret < 0)
  120. return 0;
  121. return ret;
  122. }
  123. static void apm_battery_apm_get_power_status(struct apm_power_info *info)
  124. {
  125. union power_supply_propval status;
  126. union power_supply_propval capacity, time_to_full, time_to_empty;
  127. down(&power_supply_class->sem);
  128. find_main_battery();
  129. if (!main_battery) {
  130. up(&power_supply_class->sem);
  131. return;
  132. }
  133. /* status */
  134. if (MPSY_PROP(STATUS, &status))
  135. status.intval = POWER_SUPPLY_STATUS_UNKNOWN;
  136. /* ac line status */
  137. if ((status.intval == POWER_SUPPLY_STATUS_CHARGING) ||
  138. (status.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) ||
  139. (status.intval == POWER_SUPPLY_STATUS_FULL))
  140. info->ac_line_status = APM_AC_ONLINE;
  141. else
  142. info->ac_line_status = APM_AC_OFFLINE;
  143. /* battery life (i.e. capacity, in percents) */
  144. if (MPSY_PROP(CAPACITY, &capacity) == 0) {
  145. info->battery_life = capacity.intval;
  146. } else {
  147. /* try calculate using energy */
  148. info->battery_life = calculate_capacity(0);
  149. /* if failed try calculate using charge instead */
  150. if (info->battery_life == -1)
  151. info->battery_life = calculate_capacity(1);
  152. }
  153. /* charging status */
  154. if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
  155. info->battery_status = APM_BATTERY_STATUS_CHARGING;
  156. } else {
  157. if (info->battery_life > 50)
  158. info->battery_status = APM_BATTERY_STATUS_HIGH;
  159. else if (info->battery_life > 5)
  160. info->battery_status = APM_BATTERY_STATUS_LOW;
  161. else
  162. info->battery_status = APM_BATTERY_STATUS_CRITICAL;
  163. }
  164. info->battery_flag = info->battery_status;
  165. /* time */
  166. info->units = APM_UNITS_MINS;
  167. if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
  168. if (MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full)) {
  169. if (MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full))
  170. info->time = calculate_time(status.intval);
  171. else
  172. info->time = time_to_full.intval / 60;
  173. }
  174. } else {
  175. if (MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty)) {
  176. if (MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty))
  177. info->time = calculate_time(status.intval);
  178. else
  179. info->time = time_to_empty.intval / 60;
  180. }
  181. }
  182. up(&power_supply_class->sem);
  183. }
  184. static int __init apm_battery_init(void)
  185. {
  186. printk(KERN_INFO "APM Battery Driver\n");
  187. apm_get_power_status = apm_battery_apm_get_power_status;
  188. return 0;
  189. }
  190. static void __exit apm_battery_exit(void)
  191. {
  192. apm_get_power_status = NULL;
  193. }
  194. module_init(apm_battery_init);
  195. module_exit(apm_battery_exit);
  196. MODULE_AUTHOR("Eugeny Boger <eugenyboger@dgap.mipt.ru>");
  197. MODULE_DESCRIPTION("APM emulation driver for battery monitoring class");
  198. MODULE_LICENSE("GPL");