pmu_battery.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Battery class driver for Apple PMU
  4. *
  5. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/err.h>
  10. #include <linux/power_supply.h>
  11. #include <linux/adb.h>
  12. #include <linux/pmu.h>
  13. #include <linux/slab.h>
  14. static struct pmu_battery_dev {
  15. struct power_supply *bat;
  16. struct power_supply_desc bat_desc;
  17. struct pmu_battery_info *pbi;
  18. char name[16];
  19. int propval;
  20. } *pbats[PMU_MAX_BATTERIES];
  21. #define to_pmu_battery_dev(x) power_supply_get_drvdata(x)
  22. /*********************************************************************
  23. * Power
  24. *********************************************************************/
  25. static int pmu_get_ac_prop(struct power_supply *psy,
  26. enum power_supply_property psp,
  27. union power_supply_propval *val)
  28. {
  29. switch (psp) {
  30. case POWER_SUPPLY_PROP_ONLINE:
  31. val->intval = (!!(pmu_power_flags & PMU_PWR_AC_PRESENT)) ||
  32. (pmu_battery_count == 0);
  33. break;
  34. default:
  35. return -EINVAL;
  36. }
  37. return 0;
  38. }
  39. static enum power_supply_property pmu_ac_props[] = {
  40. POWER_SUPPLY_PROP_ONLINE,
  41. };
  42. static const struct power_supply_desc pmu_ac_desc = {
  43. .name = "pmu-ac",
  44. .type = POWER_SUPPLY_TYPE_MAINS,
  45. .properties = pmu_ac_props,
  46. .num_properties = ARRAY_SIZE(pmu_ac_props),
  47. .get_property = pmu_get_ac_prop,
  48. };
  49. static struct power_supply *pmu_ac;
  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 = 0;
  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. pmu_ac = power_supply_register(&bat_pdev->dev, &pmu_ac_desc, NULL);
  135. if (IS_ERR(pmu_ac)) {
  136. ret = PTR_ERR(pmu_ac);
  137. goto ac_register_failed;
  138. }
  139. for (i = 0; i < pmu_battery_count; i++) {
  140. struct power_supply_config psy_cfg = {};
  141. struct pmu_battery_dev *pbat = kzalloc(sizeof(*pbat),
  142. GFP_KERNEL);
  143. if (!pbat)
  144. break;
  145. sprintf(pbat->name, "PMU_battery_%d", i);
  146. pbat->bat_desc.name = pbat->name;
  147. pbat->bat_desc.properties = pmu_bat_props;
  148. pbat->bat_desc.num_properties = ARRAY_SIZE(pmu_bat_props);
  149. pbat->bat_desc.get_property = pmu_bat_get_property;
  150. pbat->pbi = &pmu_batteries[i];
  151. psy_cfg.drv_data = pbat;
  152. pbat->bat = power_supply_register(&bat_pdev->dev,
  153. &pbat->bat_desc,
  154. &psy_cfg);
  155. if (IS_ERR(pbat->bat)) {
  156. ret = PTR_ERR(pbat->bat);
  157. kfree(pbat);
  158. goto battery_register_failed;
  159. }
  160. pbats[i] = pbat;
  161. }
  162. goto success;
  163. battery_register_failed:
  164. while (i--) {
  165. if (!pbats[i])
  166. continue;
  167. power_supply_unregister(pbats[i]->bat);
  168. kfree(pbats[i]);
  169. }
  170. power_supply_unregister(pmu_ac);
  171. ac_register_failed:
  172. platform_device_unregister(bat_pdev);
  173. pdev_register_failed:
  174. success:
  175. return ret;
  176. }
  177. static void __exit pmu_bat_exit(void)
  178. {
  179. int i;
  180. for (i = 0; i < PMU_MAX_BATTERIES; i++) {
  181. if (!pbats[i])
  182. continue;
  183. power_supply_unregister(pbats[i]->bat);
  184. kfree(pbats[i]);
  185. }
  186. power_supply_unregister(pmu_ac);
  187. platform_device_unregister(bat_pdev);
  188. }
  189. module_init(pmu_bat_init);
  190. module_exit(pmu_bat_exit);
  191. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  192. MODULE_LICENSE("GPL");
  193. MODULE_DESCRIPTION("PMU battery driver");