pmu_battery.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  80. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  81. break;
  82. case POWER_SUPPLY_PROP_PRESENT:
  83. val->intval = !!(pbi->flags & PMU_BATT_PRESENT);
  84. break;
  85. case POWER_SUPPLY_PROP_MODEL_NAME:
  86. val->strval = pmu_bat_get_model_name(pbi);
  87. break;
  88. case POWER_SUPPLY_PROP_ENERGY_AVG:
  89. val->intval = pbi->charge * 1000; /* mWh -> µWh */
  90. break;
  91. case POWER_SUPPLY_PROP_ENERGY_FULL:
  92. val->intval = pbi->max_charge * 1000; /* mWh -> µWh */
  93. break;
  94. case POWER_SUPPLY_PROP_CURRENT_AVG:
  95. val->intval = pbi->amperage * 1000; /* mA -> µA */
  96. break;
  97. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  98. val->intval = pbi->voltage * 1000; /* mV -> µV */
  99. break;
  100. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  101. val->intval = pbi->time_remaining;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. return 0;
  107. }
  108. static enum power_supply_property pmu_bat_props[] = {
  109. POWER_SUPPLY_PROP_STATUS,
  110. POWER_SUPPLY_PROP_PRESENT,
  111. POWER_SUPPLY_PROP_MODEL_NAME,
  112. POWER_SUPPLY_PROP_ENERGY_AVG,
  113. POWER_SUPPLY_PROP_ENERGY_FULL,
  114. POWER_SUPPLY_PROP_CURRENT_AVG,
  115. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  116. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  117. };
  118. /*********************************************************************
  119. * Initialisation
  120. *********************************************************************/
  121. static struct platform_device *bat_pdev;
  122. static int __init pmu_bat_init(void)
  123. {
  124. int ret;
  125. int i;
  126. bat_pdev = platform_device_register_simple("pmu-battery",
  127. 0, NULL, 0);
  128. if (IS_ERR(bat_pdev)) {
  129. ret = PTR_ERR(bat_pdev);
  130. goto pdev_register_failed;
  131. }
  132. ret = power_supply_register(&bat_pdev->dev, &pmu_ac);
  133. if (ret)
  134. goto ac_register_failed;
  135. for (i = 0; i < pmu_battery_count; i++) {
  136. struct pmu_battery_dev *pbat = kzalloc(sizeof(*pbat),
  137. GFP_KERNEL);
  138. if (!pbat)
  139. break;
  140. sprintf(pbat->name, "PMU_battery_%d", i);
  141. pbat->bat.name = pbat->name;
  142. pbat->bat.properties = pmu_bat_props;
  143. pbat->bat.num_properties = ARRAY_SIZE(pmu_bat_props);
  144. pbat->bat.get_property = pmu_bat_get_property;
  145. pbat->pbi = &pmu_batteries[i];
  146. ret = power_supply_register(&bat_pdev->dev, &pbat->bat);
  147. if (ret) {
  148. kfree(pbat);
  149. goto battery_register_failed;
  150. }
  151. pbats[i] = pbat;
  152. }
  153. goto success;
  154. battery_register_failed:
  155. while (i--) {
  156. if (!pbats[i])
  157. continue;
  158. power_supply_unregister(&pbats[i]->bat);
  159. kfree(pbats[i]);
  160. }
  161. power_supply_unregister(&pmu_ac);
  162. ac_register_failed:
  163. platform_device_unregister(bat_pdev);
  164. pdev_register_failed:
  165. success:
  166. return ret;
  167. }
  168. static void __exit pmu_bat_exit(void)
  169. {
  170. int i;
  171. for (i = 0; i < PMU_MAX_BATTERIES; i++) {
  172. if (!pbats[i])
  173. continue;
  174. power_supply_unregister(&pbats[i]->bat);
  175. kfree(pbats[i]);
  176. }
  177. power_supply_unregister(&pmu_ac);
  178. platform_device_unregister(bat_pdev);
  179. }
  180. module_init(pmu_bat_init);
  181. module_exit(pmu_bat_exit);
  182. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  183. MODULE_LICENSE("GPL");
  184. MODULE_DESCRIPTION("PMU battery driver");