1 From 7041346d379e563e9ee66f9dd31277e4fb6b1b1f Mon Sep 17 00:00:00 2001
2 From: Mike Lockwood <lockwood@android.com>
3 Date: Wed, 6 Feb 2008 11:37:10 -0500
4 Subject: [PATCH 126/134] [ARM] goldfish: POWER: New power supply driver for goldfish.
6 Signed-off-by: Mike A. Chan <mikechan@google.com>
8 drivers/power/Kconfig | 5 +
9 drivers/power/Makefile | 3 +-
10 drivers/power/goldfish_battery.c | 254 ++++++++++++++++++++++++++++++++++++++
11 3 files changed, 261 insertions(+), 1 deletions(-)
12 create mode 100644 drivers/power/goldfish_battery.c
14 diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
15 index 33da112..593c616 100644
16 --- a/drivers/power/Kconfig
17 +++ b/drivers/power/Kconfig
18 @@ -88,4 +88,9 @@ config CHARGER_PCF50633
20 Say Y to include support for NXP PCF50633 Main Battery Charger.
22 +config BATTERY_GOLDFISH
23 + tristate "Goldfish battery driver"
25 + Say Y to enable support for the battery and AC power in the Goldfish emulator.
28 diff --git a/drivers/power/Makefile b/drivers/power/Makefile
29 index 2fcf41d..5be0fbb 100644
30 --- a/drivers/power/Makefile
31 +++ b/drivers/power/Makefile
32 @@ -19,10 +19,11 @@ obj-$(CONFIG_APM_POWER) += apm_power.o
33 obj-$(CONFIG_WM8350_POWER) += wm8350_power.o
35 obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
36 +obj-$(CONFIG_BATTERY_GOLDFISH) += goldfish_battery.o
37 obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
38 obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o
39 obj-$(CONFIG_BATTERY_TOSA) += tosa_battery.o
40 obj-$(CONFIG_BATTERY_WM97XX) += wm97xx_battery.o
41 obj-$(CONFIG_BATTERY_BQ27x00) += bq27x00_battery.o
42 obj-$(CONFIG_BATTERY_DA9030) += da9030_battery.o
43 -obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o
44 \ No newline at end of file
45 +obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o
46 diff --git a/drivers/power/goldfish_battery.c b/drivers/power/goldfish_battery.c
48 index 0000000..868dc83
50 +++ b/drivers/power/goldfish_battery.c
52 +/* drivers/power/goldfish_battery.c
54 + * Power supply driver for the goldfish emulator
56 + * Copyright (C) 2008 Google, Inc.
57 + * Author: Mike Lockwood <lockwood@android.com>
59 + * This software is licensed under the terms of the GNU General Public
60 + * License version 2, as published by the Free Software Foundation, and
61 + * may be copied, distributed, and modified under those terms.
63 + * This program is distributed in the hope that it will be useful,
64 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 + * GNU General Public License for more details.
70 +#include <linux/module.h>
71 +#include <linux/err.h>
72 +#include <linux/platform_device.h>
73 +#include <linux/power_supply.h>
74 +#include <linux/types.h>
75 +#include <linux/pci.h>
76 +#include <linux/interrupt.h>
80 +struct goldfish_battery_data {
85 + struct power_supply battery;
86 + struct power_supply ac;
89 +#define GOLDFISH_BATTERY_READ(data, addr) (readl(data->reg_base + addr))
90 +#define GOLDFISH_BATTERY_WRITE(data, addr, x) (writel(x, data->reg_base + addr))
93 +/* temporary variable used between goldfish_battery_probe() and goldfish_battery_open() */
94 +static struct goldfish_battery_data *battery_data;
97 + /* status register */
98 + BATTERY_INT_STATUS = 0x00,
99 + /* set this to enable IRQ */
100 + BATTERY_INT_ENABLE = 0x04,
102 + BATTERY_AC_ONLINE = 0x08,
103 + BATTERY_STATUS = 0x0C,
104 + BATTERY_HEALTH = 0x10,
105 + BATTERY_PRESENT = 0x14,
106 + BATTERY_CAPACITY = 0x18,
108 + BATTERY_STATUS_CHANGED = 1U << 0,
109 + AC_STATUS_CHANGED = 1U << 1,
110 + BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
114 +static int goldfish_ac_get_property(struct power_supply *psy,
115 + enum power_supply_property psp,
116 + union power_supply_propval *val)
118 + struct goldfish_battery_data *data = container_of(psy,
119 + struct goldfish_battery_data, ac);
123 + case POWER_SUPPLY_PROP_ONLINE:
124 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_AC_ONLINE);
133 +static int goldfish_battery_get_property(struct power_supply *psy,
134 + enum power_supply_property psp,
135 + union power_supply_propval *val)
137 + struct goldfish_battery_data *data = container_of(psy,
138 + struct goldfish_battery_data, battery);
142 + case POWER_SUPPLY_PROP_STATUS:
143 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_STATUS);
145 + case POWER_SUPPLY_PROP_HEALTH:
146 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_HEALTH);
148 + case POWER_SUPPLY_PROP_PRESENT:
149 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_PRESENT);
151 + case POWER_SUPPLY_PROP_TECHNOLOGY:
152 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
154 + case POWER_SUPPLY_PROP_CAPACITY:
155 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
165 +static enum power_supply_property goldfish_battery_props[] = {
166 + POWER_SUPPLY_PROP_STATUS,
167 + POWER_SUPPLY_PROP_HEALTH,
168 + POWER_SUPPLY_PROP_PRESENT,
169 + POWER_SUPPLY_PROP_TECHNOLOGY,
170 + POWER_SUPPLY_PROP_CAPACITY,
173 +static enum power_supply_property goldfish_ac_props[] = {
174 + POWER_SUPPLY_PROP_ONLINE,
177 +static irqreturn_t goldfish_battery_interrupt(int irq, void *dev_id)
179 + unsigned long irq_flags;
180 + struct goldfish_battery_data *data = dev_id;
183 + spin_lock_irqsave(&data->lock, irq_flags);
185 + /* read status flags, which will clear the interrupt */
186 + status = GOLDFISH_BATTERY_READ(data, BATTERY_INT_STATUS);
187 + status &= BATTERY_INT_MASK;
189 + if (status & BATTERY_STATUS_CHANGED)
190 + power_supply_changed(&data->battery);
191 + if (status & AC_STATUS_CHANGED)
192 + power_supply_changed(&data->ac);
194 + spin_unlock_irqrestore(&data->lock, irq_flags);
195 + return status ? IRQ_HANDLED : IRQ_NONE;
199 +static int goldfish_battery_probe(struct platform_device *pdev)
202 + struct resource *r;
203 + struct goldfish_battery_data *data;
205 + data = kzalloc(sizeof(*data), GFP_KERNEL);
206 + if (data == NULL) {
208 + goto err_data_alloc_failed;
210 + spin_lock_init(&data->lock);
212 + data->battery.properties = goldfish_battery_props;
213 + data->battery.num_properties = ARRAY_SIZE(goldfish_battery_props);
214 + data->battery.get_property = goldfish_battery_get_property;
215 + data->battery.name = "battery";
216 + data->battery.type = POWER_SUPPLY_TYPE_BATTERY;
218 + data->ac.properties = goldfish_ac_props;
219 + data->ac.num_properties = ARRAY_SIZE(goldfish_ac_props);
220 + data->ac.get_property = goldfish_ac_get_property;
221 + data->ac.name = "ac";
222 + data->ac.type = POWER_SUPPLY_TYPE_MAINS;
224 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 + printk(KERN_ERR "%s: platform_get_resource failed\n", pdev->name);
228 + goto err_no_io_base;
230 + data->reg_base = IO_ADDRESS(r->start - IO_START);
232 + data->irq = platform_get_irq(pdev, 0);
233 + if (data->irq < 0) {
234 + printk(KERN_ERR "%s: platform_get_irq failed\n", pdev->name);
239 + ret = request_irq(data->irq, goldfish_battery_interrupt, IRQF_SHARED, pdev->name, data);
241 + goto err_request_irq_failed;
243 + ret = power_supply_register(&pdev->dev, &data->ac);
245 + goto err_ac_failed;
247 + ret = power_supply_register(&pdev->dev, &data->battery);
249 + goto err_battery_failed;
251 + platform_set_drvdata(pdev, data);
252 + battery_data = data;
254 + GOLDFISH_BATTERY_WRITE(data, BATTERY_INT_ENABLE, BATTERY_INT_MASK);
258 + power_supply_unregister(&data->ac);
260 + free_irq(data->irq, data);
261 +err_request_irq_failed:
265 +err_data_alloc_failed:
269 +static int goldfish_battery_remove(struct platform_device *pdev)
271 + struct goldfish_battery_data *data = platform_get_drvdata(pdev);
273 + power_supply_unregister(&data->battery);
274 + power_supply_unregister(&data->ac);
276 + free_irq(data->irq, data);
278 + battery_data = NULL;
282 +static struct platform_driver goldfish_battery_device = {
283 + .probe = goldfish_battery_probe,
284 + .remove = goldfish_battery_remove,
286 + .name = "goldfish-battery"
290 +static int __init goldfish_battery_init(void)
292 + return platform_driver_register(&goldfish_battery_device);
295 +static void __exit goldfish_battery_exit(void)
297 + platform_driver_unregister(&goldfish_battery_device);
300 +module_init(goldfish_battery_init);
301 +module_exit(goldfish_battery_exit);
303 +MODULE_AUTHOR("Mike Lockwood lockwood@android.com");
304 +MODULE_LICENSE("GPL");
305 +MODULE_DESCRIPTION("Battery driver for the Goldfish emulator");