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