pmu_battery.c 5.3 KB

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