pmu_battery.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Battery class driver for Apple PMU
  3. *
  4. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/err.h>
  13. #include <linux/power_supply.h>
  14. #include <linux/adb.h>
  15. #include <linux/pmu.h>
  16. #include <linux/slab.h>
  17. static struct pmu_battery_dev {
  18. struct power_supply bat;
  19. struct pmu_battery_info *pbi;
  20. char name[16];
  21. int propval;
  22. } *pbats[PMU_MAX_BATTERIES];
  23. #define to_pmu_battery_dev(x) container_of(x, struct pmu_battery_dev, bat)
  24. /*********************************************************************
  25. * Power
  26. *********************************************************************/
  27. static int pmu_get_ac_prop(struct power_supply *psy,
  28. enum power_supply_property psp,
  29. union power_supply_propval *val)
  30. {
  31. switch (psp) {
  32. case POWER_SUPPLY_PROP_ONLINE:
  33. val->intval = (!!(pmu_power_flags & PMU_PWR_AC_PRESENT)) ||
  34. (pmu_battery_count == 0);
  35. break;
  36. default:
  37. return -EINVAL;
  38. }
  39. return 0;
  40. }
  41. static enum power_supply_property pmu_ac_props[] = {
  42. POWER_SUPPLY_PROP_ONLINE,
  43. };
  44. static struct power_supply pmu_ac = {
  45. .name = "pmu-ac",
  46. .type = POWER_SUPPLY_TYPE_MAINS,
  47. .properties = pmu_ac_props,
  48. .num_properties = ARRAY_SIZE(pmu_ac_props),
  49. .get_property = pmu_get_ac_prop,
  50. };
  51. /*********************************************************************
  52. * Battery properties
  53. *********************************************************************/
  54. static char *pmu_batt_types[] = {
  55. "Smart", "Comet", "Hooper", "Unknown"
  56. };
  57. static char *pmu_bat_get_model_name(struct pmu_battery_info *pbi)
  58. {
  59. switch (pbi->flags & PMU_BATT_TYPE_MASK) {
  60. case PMU_BATT_TYPE_SMART:
  61. return pmu_batt_types[0];
  62. case PMU_BATT_TYPE_COMET:
  63. return pmu_batt_types[1];
  64. case PMU_BATT_TYPE_HOOPER:
  65. return pmu_batt_types[2];
  66. default: break;
  67. }
  68. return pmu_batt_types[3];
  69. }
  70. static int pmu_bat_get_property(struct power_supply *psy,
  71. enum power_supply_property psp,
  72. union power_supply_propval *val)
  73. {
  74. struct pmu_battery_dev *pbat = to_pmu_battery_dev(psy);
  75. struct pmu_battery_info *pbi = pbat->pbi;
  76. switch (psp) {
  77. case POWER_SUPPLY_PROP_STATUS:
  78. if (pbi->flags & PMU_BATT_CHARGING)
  79. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  80. else if (pmu_power_flags & PMU_PWR_AC_PRESENT)
  81. val->intval = POWER_SUPPLY_STATUS_FULL;
  82. else
  83. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  84. break;
  85. case POWER_SUPPLY_PROP_PRESENT:
  86. val->intval = !!(pbi->flags & PMU_BATT_PRESENT);
  87. break;
  88. case POWER_SUPPLY_PROP_MODEL_NAME:
  89. val->strval = pmu_bat_get_model_name(pbi);
  90. break;
  91. case POWER_SUPPLY_PROP_ENERGY_AVG:
  92. val->intval = pbi->charge * 1000; /* mWh -> µWh */
  93. break;
  94. case POWER_SUPPLY_PROP_ENERGY_FULL:
  95. val->intval = pbi->max_charge * 1000; /* mWh -> µWh */
  96. break;
  97. case POWER_SUPPLY_PROP_CURRENT_AVG:
  98. val->intval = pbi->amperage * 1000; /* mA -> µA */
  99. break;
  100. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  101. val->intval = pbi->voltage * 1000; /* mV -> µV */
  102. break;
  103. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  104. val->intval = pbi->time_remaining;
  105. break;
  106. default:
  107. return -EINVAL;
  108. }
  109. return 0;
  110. }
  111. static enum power_supply_property pmu_bat_props[] = {
  112. POWER_SUPPLY_PROP_STATUS,
  113. POWER_SUPPLY_PROP_PRESENT,
  114. POWER_SUPPLY_PROP_MODEL_NAME,
  115. POWER_SUPPLY_PROP_ENERGY_AVG,
  116. POWER_SUPPLY_PROP_ENERGY_FULL,
  117. POWER_SUPPLY_PROP_CURRENT_AVG,
  118. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  119. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  120. };
  121. /*********************************************************************
  122. * Initialisation
  123. *********************************************************************/
  124. static struct platform_device *bat_pdev;
  125. static int __init pmu_bat_init(void)
  126. {
  127. int ret;
  128. int i;
  129. bat_pdev = platform_device_register_simple("pmu-battery",
  130. 0, NULL, 0);
  131. if (IS_ERR(bat_pdev)) {
  132. ret = PTR_ERR(bat_pdev);
  133. goto pdev_register_failed;
  134. }
  135. ret = power_supply_register(&bat_pdev->dev, &pmu_ac);
  136. if (ret)
  137. goto ac_register_failed;
  138. for (i = 0; i < pmu_battery_count; i++) {
  139. struct pmu_battery_dev *pbat = kzalloc(sizeof(*pbat),
  140. GFP_KERNEL);
  141. if (!pbat)
  142. break;
  143. sprintf(pbat->name, "PMU_battery_%d", i);
  144. pbat->bat.name = pbat->name;
  145. pbat->bat.properties = pmu_bat_props;
  146. pbat->bat.num_properties = ARRAY_SIZE(pmu_bat_props);
  147. pbat->bat.get_property = pmu_bat_get_property;
  148. pbat->pbi = &pmu_batteries[i];
  149. ret = power_supply_register(&bat_pdev->dev, &pbat->bat);
  150. if (ret) {
  151. kfree(pbat);
  152. goto battery_register_failed;
  153. }
  154. pbats[i] = pbat;
  155. }
  156. goto success;
  157. battery_register_failed:
  158. while (i--) {
  159. if (!pbats[i])
  160. continue;
  161. power_supply_unregister(&pbats[i]->bat);
  162. kfree(pbats[i]);
  163. }
  164. power_supply_unregister(&pmu_ac);
  165. ac_register_failed:
  166. platform_device_unregister(bat_pdev);
  167. pdev_register_failed:
  168. success:
  169. return ret;
  170. }
  171. static void __exit pmu_bat_exit(void)
  172. {
  173. int i;
  174. for (i = 0; i < PMU_MAX_BATTERIES; i++) {
  175. if (!pbats[i])
  176. continue;
  177. power_supply_unregister(&pbats[i]->bat);
  178. kfree(pbats[i]);
  179. }
  180. power_supply_unregister(&pmu_ac);
  181. platform_device_unregister(bat_pdev);
  182. }
  183. module_init(pmu_bat_init);
  184. module_exit(pmu_bat_exit);
  185. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  186. MODULE_LICENSE("GPL");
  187. MODULE_DESCRIPTION("PMU battery driver");