2 * Battery class driver for Apple PMU
4 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
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.
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/err.h>
14 #include <linux/power_supply.h>
15 #include <linux/adb.h>
16 #include <linux/pmu.h>
18 static struct pmu_battery_dev {
19 struct power_supply bat;
20 struct pmu_battery_info *pbi;
23 } *pbats[PMU_MAX_BATTERIES];
25 #define to_pmu_battery_dev(x) container_of(x, struct pmu_battery_dev, bat)
27 /*********************************************************************
29 *********************************************************************/
31 static int pmu_get_ac_prop(struct power_supply *psy,
32 enum power_supply_property psp,
33 union power_supply_propval *val)
36 case POWER_SUPPLY_PROP_ONLINE:
37 val->intval = (!!(pmu_power_flags & PMU_PWR_AC_PRESENT)) ||
38 (pmu_battery_count == 0);
47 static enum power_supply_property pmu_ac_props[] = {
48 POWER_SUPPLY_PROP_ONLINE,
51 static struct power_supply pmu_ac = {
53 .type = POWER_SUPPLY_TYPE_MAINS,
54 .properties = pmu_ac_props,
55 .num_properties = ARRAY_SIZE(pmu_ac_props),
56 .get_property = pmu_get_ac_prop,
59 /*********************************************************************
61 *********************************************************************/
63 static char *pmu_batt_types[] = {
64 "Smart", "Comet", "Hooper", "Unknown"
67 static char *pmu_bat_get_model_name(struct pmu_battery_info *pbi)
69 switch (pbi->flags & PMU_BATT_TYPE_MASK) {
70 case PMU_BATT_TYPE_SMART:
71 return pmu_batt_types[0];
72 case PMU_BATT_TYPE_COMET:
73 return pmu_batt_types[1];
74 case PMU_BATT_TYPE_HOOPER:
75 return pmu_batt_types[2];
78 return pmu_batt_types[3];
81 static int pmu_bat_get_property(struct power_supply *psy,
82 enum power_supply_property psp,
83 union power_supply_propval *val)
85 struct pmu_battery_dev *pbat = to_pmu_battery_dev(psy);
86 struct pmu_battery_info *pbi = pbat->pbi;
89 case POWER_SUPPLY_PROP_STATUS:
90 if (pbi->flags & PMU_BATT_CHARGING)
91 val->intval = POWER_SUPPLY_STATUS_CHARGING;
93 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
95 case POWER_SUPPLY_PROP_PRESENT:
96 val->intval = !!(pbi->flags & PMU_BATT_PRESENT);
98 case POWER_SUPPLY_PROP_MODEL_NAME:
99 val->strval = pmu_bat_get_model_name(pbi);
101 case POWER_SUPPLY_PROP_ENERGY_AVG:
102 val->intval = pbi->charge * 1000; /* mWh -> µWh */
104 case POWER_SUPPLY_PROP_ENERGY_FULL:
105 val->intval = pbi->max_charge * 1000; /* mWh -> µWh */
107 case POWER_SUPPLY_PROP_CURRENT_AVG:
108 val->intval = pbi->amperage * 1000; /* mA -> µA */
110 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
111 val->intval = pbi->voltage * 1000; /* mV -> µV */
113 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
114 val->intval = pbi->time_remaining;
123 static enum power_supply_property pmu_bat_props[] = {
124 POWER_SUPPLY_PROP_STATUS,
125 POWER_SUPPLY_PROP_PRESENT,
126 POWER_SUPPLY_PROP_MODEL_NAME,
127 POWER_SUPPLY_PROP_ENERGY_AVG,
128 POWER_SUPPLY_PROP_ENERGY_FULL,
129 POWER_SUPPLY_PROP_CURRENT_AVG,
130 POWER_SUPPLY_PROP_VOLTAGE_AVG,
131 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
134 /*********************************************************************
136 *********************************************************************/
138 static struct platform_device *bat_pdev;
140 static int __init pmu_bat_init(void)
145 bat_pdev = platform_device_register_simple("pmu-battery",
147 if (IS_ERR(bat_pdev)) {
148 ret = PTR_ERR(bat_pdev);
149 goto pdev_register_failed;
152 ret = power_supply_register(&bat_pdev->dev, &pmu_ac);
154 goto ac_register_failed;
156 for (i = 0; i < pmu_battery_count; i++) {
157 struct pmu_battery_dev *pbat = kzalloc(sizeof(*pbat),
162 sprintf(pbat->name, "PMU battery %d", i);
163 pbat->bat.name = pbat->name;
164 pbat->bat.properties = pmu_bat_props;
165 pbat->bat.num_properties = ARRAY_SIZE(pmu_bat_props);
166 pbat->bat.get_property = pmu_bat_get_property;
167 pbat->pbi = &pmu_batteries[i];
169 ret = power_supply_register(&bat_pdev->dev, &pbat->bat);
172 goto battery_register_failed;
179 battery_register_failed:
183 power_supply_unregister(&pbats[i]->bat);
186 power_supply_unregister(&pmu_ac);
188 platform_device_unregister(bat_pdev);
189 pdev_register_failed:
194 static void __exit pmu_bat_exit(void)
198 for (i = 0; i < PMU_MAX_BATTERIES; i++) {
201 power_supply_unregister(&pbats[i]->bat);
204 power_supply_unregister(&pmu_ac);
205 platform_device_unregister(bat_pdev);
208 module_init(pmu_bat_init);
209 module_exit(pmu_bat_exit);
211 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
212 MODULE_LICENSE("GPL");
213 MODULE_DESCRIPTION("PMU battery driver");