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
;
92 else if (pmu_power_flags
& PMU_PWR_AC_PRESENT
)
93 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
95 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
97 case POWER_SUPPLY_PROP_PRESENT
:
98 val
->intval
= !!(pbi
->flags
& PMU_BATT_PRESENT
);
100 case POWER_SUPPLY_PROP_MODEL_NAME
:
101 val
->strval
= pmu_bat_get_model_name(pbi
);
103 case POWER_SUPPLY_PROP_ENERGY_AVG
:
104 val
->intval
= pbi
->charge
* 1000; /* mWh -> µWh */
106 case POWER_SUPPLY_PROP_ENERGY_FULL
:
107 val
->intval
= pbi
->max_charge
* 1000; /* mWh -> µWh */
109 case POWER_SUPPLY_PROP_CURRENT_AVG
:
110 val
->intval
= pbi
->amperage
* 1000; /* mA -> µA */
112 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
113 val
->intval
= pbi
->voltage
* 1000; /* mV -> µV */
115 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
116 val
->intval
= pbi
->time_remaining
;
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
,
136 /*********************************************************************
138 *********************************************************************/
140 static struct platform_device
*bat_pdev
;
142 static int __init
pmu_bat_init(void)
147 bat_pdev
= platform_device_register_simple("pmu-battery",
149 if (IS_ERR(bat_pdev
)) {
150 ret
= PTR_ERR(bat_pdev
);
151 goto pdev_register_failed
;
154 ret
= power_supply_register(&bat_pdev
->dev
, &pmu_ac
);
156 goto ac_register_failed
;
158 for (i
= 0; i
< pmu_battery_count
; i
++) {
159 struct pmu_battery_dev
*pbat
= kzalloc(sizeof(*pbat
),
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
];
171 ret
= power_supply_register(&bat_pdev
->dev
, &pbat
->bat
);
174 goto battery_register_failed
;
181 battery_register_failed
:
185 power_supply_unregister(&pbats
[i
]->bat
);
188 power_supply_unregister(&pmu_ac
);
190 platform_device_unregister(bat_pdev
);
191 pdev_register_failed
:
196 static void __exit
pmu_bat_exit(void)
200 for (i
= 0; i
< PMU_MAX_BATTERIES
; i
++) {
203 power_supply_unregister(&pbats
[i
]->bat
);
206 power_supply_unregister(&pmu_ac
);
207 platform_device_unregister(bat_pdev
);
210 module_init(pmu_bat_init
);
211 module_exit(pmu_bat_exit
);
213 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
214 MODULE_LICENSE("GPL");
215 MODULE_DESCRIPTION("PMU battery driver");