apm_power.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 = NULL;
  24. struct power_supply *max_charge_bat = NULL;
  25. struct power_supply *max_energy_bat = NULL;
  26. union power_supply_propval full;
  27. int max_charge = 0;
  28. int max_energy = 0;
  29. main_battery = NULL;
  30. list_for_each_entry(dev, &power_supply_class->devices, node) {
  31. bat = dev_get_drvdata(dev);
  32. if (bat->use_for_apm) {
  33. /* nice, we explicitly asked to report this battery. */
  34. main_battery = bat;
  35. return;
  36. }
  37. if (!PSY_PROP(bat, CHARGE_FULL_DESIGN, &full) ||
  38. !PSY_PROP(bat, CHARGE_FULL, &full)) {
  39. if (full.intval > max_charge) {
  40. max_charge_bat = bat;
  41. max_charge = full.intval;
  42. }
  43. } else if (!PSY_PROP(bat, ENERGY_FULL_DESIGN, &full) ||
  44. !PSY_PROP(bat, ENERGY_FULL, &full)) {
  45. if (full.intval > max_energy) {
  46. max_energy_bat = bat;
  47. max_energy = full.intval;
  48. }
  49. }
  50. }
  51. if ((max_energy_bat && max_charge_bat) &&
  52. (max_energy_bat != max_charge_bat)) {
  53. /* try guess battery with more capacity */
  54. if (!PSY_PROP(max_charge_bat, VOLTAGE_MAX_DESIGN, &full)) {
  55. if (max_energy > max_charge * full.intval)
  56. main_battery = max_energy_bat;
  57. else
  58. main_battery = max_charge_bat;
  59. } else if (!PSY_PROP(max_energy_bat, VOLTAGE_MAX_DESIGN,
  60. &full)) {
  61. if (max_charge > max_energy / full.intval)
  62. main_battery = max_charge_bat;
  63. else
  64. main_battery = max_energy_bat;
  65. } else {
  66. /* give up, choice any */
  67. main_battery = max_energy_bat;
  68. }
  69. } else if (max_charge_bat) {
  70. main_battery = max_charge_bat;
  71. } else if (max_energy_bat) {
  72. main_battery = max_energy_bat;
  73. } else {
  74. /* give up, try the last if any */
  75. main_battery = bat;
  76. }
  77. }
  78. static int calculate_time(int status)
  79. {
  80. union power_supply_propval charge_full, charge_empty;
  81. union power_supply_propval charge, I;
  82. if (MPSY_PROP(CHARGE_FULL, &charge_full)) {
  83. /* if battery can't report this property, use design value */
  84. if (MPSY_PROP(CHARGE_FULL_DESIGN, &charge_full))
  85. return -1;
  86. }
  87. if (MPSY_PROP(CHARGE_EMPTY, &charge_empty)) {
  88. /* if battery can't report this property, use design value */
  89. if (MPSY_PROP(CHARGE_EMPTY_DESIGN, &charge_empty))
  90. charge_empty.intval = 0;
  91. }
  92. if (MPSY_PROP(CHARGE_AVG, &charge)) {
  93. /* if battery can't report average value, use momentary */
  94. if (MPSY_PROP(CHARGE_NOW, &charge))
  95. return -1;
  96. }
  97. if (MPSY_PROP(CURRENT_AVG, &I)) {
  98. /* if battery can't report average value, use momentary */
  99. if (MPSY_PROP(CURRENT_NOW, &I))
  100. return -1;
  101. }
  102. if (status == POWER_SUPPLY_STATUS_CHARGING)
  103. return ((charge.intval - charge_full.intval) * 60L) /
  104. I.intval;
  105. else
  106. return -((charge.intval - charge_empty.intval) * 60L) /
  107. I.intval;
  108. }
  109. static int calculate_capacity(int using_charge)
  110. {
  111. enum power_supply_property full_prop, empty_prop;
  112. enum power_supply_property full_design_prop, empty_design_prop;
  113. enum power_supply_property now_prop, avg_prop;
  114. union power_supply_propval empty, full, cur;
  115. int ret;
  116. if (using_charge) {
  117. full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
  118. empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
  119. full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
  120. empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
  121. now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
  122. avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
  123. } else {
  124. full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
  125. empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
  126. full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
  127. empty_design_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
  128. now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
  129. avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
  130. }
  131. if (_MPSY_PROP(full_prop, &full)) {
  132. /* if battery can't report this property, use design value */
  133. if (_MPSY_PROP(full_design_prop, &full))
  134. return -1;
  135. }
  136. if (_MPSY_PROP(avg_prop, &cur)) {
  137. /* if battery can't report average value, use momentary */
  138. if (_MPSY_PROP(now_prop, &cur))
  139. return -1;
  140. }
  141. if (_MPSY_PROP(empty_prop, &empty)) {
  142. /* if battery can't report this property, use design value */
  143. if (_MPSY_PROP(empty_design_prop, &empty))
  144. empty.intval = 0;
  145. }
  146. if (full.intval - empty.intval)
  147. ret = ((cur.intval - empty.intval) * 100L) /
  148. (full.intval - empty.intval);
  149. else
  150. return -1;
  151. if (ret > 100)
  152. return 100;
  153. else if (ret < 0)
  154. return 0;
  155. return ret;
  156. }
  157. static void apm_battery_apm_get_power_status(struct apm_power_info *info)
  158. {
  159. union power_supply_propval status;
  160. union power_supply_propval capacity, time_to_full, time_to_empty;
  161. down(&power_supply_class->sem);
  162. find_main_battery();
  163. if (!main_battery) {
  164. up(&power_supply_class->sem);
  165. return;
  166. }
  167. /* status */
  168. if (MPSY_PROP(STATUS, &status))
  169. status.intval = POWER_SUPPLY_STATUS_UNKNOWN;
  170. /* ac line status */
  171. if ((status.intval == POWER_SUPPLY_STATUS_CHARGING) ||
  172. (status.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) ||
  173. (status.intval == POWER_SUPPLY_STATUS_FULL))
  174. info->ac_line_status = APM_AC_ONLINE;
  175. else
  176. info->ac_line_status = APM_AC_OFFLINE;
  177. /* battery life (i.e. capacity, in percents) */
  178. if (MPSY_PROP(CAPACITY, &capacity) == 0) {
  179. info->battery_life = capacity.intval;
  180. } else {
  181. /* try calculate using energy */
  182. info->battery_life = calculate_capacity(0);
  183. /* if failed try calculate using charge instead */
  184. if (info->battery_life == -1)
  185. info->battery_life = calculate_capacity(1);
  186. }
  187. /* charging status */
  188. if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
  189. info->battery_status = APM_BATTERY_STATUS_CHARGING;
  190. } else {
  191. if (info->battery_life > 50)
  192. info->battery_status = APM_BATTERY_STATUS_HIGH;
  193. else if (info->battery_life > 5)
  194. info->battery_status = APM_BATTERY_STATUS_LOW;
  195. else
  196. info->battery_status = APM_BATTERY_STATUS_CRITICAL;
  197. }
  198. info->battery_flag = info->battery_status;
  199. /* time */
  200. info->units = APM_UNITS_MINS;
  201. if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
  202. if (!MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full) ||
  203. !MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full))
  204. info->time = time_to_full.intval / 60;
  205. else
  206. info->time = calculate_time(status.intval);
  207. } else {
  208. if (!MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty) ||
  209. !MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty))
  210. info->time = time_to_empty.intval / 60;
  211. else
  212. info->time = calculate_time(status.intval);
  213. }
  214. up(&power_supply_class->sem);
  215. }
  216. static int __init apm_battery_init(void)
  217. {
  218. printk(KERN_INFO "APM Battery Driver\n");
  219. apm_get_power_status = apm_battery_apm_get_power_status;
  220. return 0;
  221. }
  222. static void __exit apm_battery_exit(void)
  223. {
  224. apm_get_power_status = NULL;
  225. }
  226. module_init(apm_battery_init);
  227. module_exit(apm_battery_exit);
  228. MODULE_AUTHOR("Eugeny Boger <eugenyboger@dgap.mipt.ru>");
  229. MODULE_DESCRIPTION("APM emulation driver for battery monitoring class");
  230. MODULE_LICENSE("GPL");