pmu_battery.c 5.6 KB

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