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 --- a/drivers/power/Kconfig
15 +++ b/drivers/power/Kconfig
16 @@ -88,4 +88,9 @@ config CHARGER_PCF50633
18 Say Y to include support for NXP PCF50633 Main Battery Charger.
20 +config BATTERY_GOLDFISH
21 + tristate "Goldfish battery driver"
23 + Say Y to enable support for the battery and AC power in the Goldfish emulator.
26 --- a/drivers/power/Makefile
27 +++ b/drivers/power/Makefile
28 @@ -19,10 +19,11 @@ obj-$(CONFIG_APM_POWER) += apm_power.o
29 obj-$(CONFIG_WM8350_POWER) += wm8350_power.o
31 obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
32 +obj-$(CONFIG_BATTERY_GOLDFISH) += goldfish_battery.o
33 obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
34 obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o
35 obj-$(CONFIG_BATTERY_TOSA) += tosa_battery.o
36 obj-$(CONFIG_BATTERY_WM97XX) += wm97xx_battery.o
37 obj-$(CONFIG_BATTERY_BQ27x00) += bq27x00_battery.o
38 obj-$(CONFIG_BATTERY_DA9030) += da9030_battery.o
39 -obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o
40 \ No newline at end of file
41 +obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o
43 +++ b/drivers/power/goldfish_battery.c
45 +/* drivers/power/goldfish_battery.c
47 + * Power supply driver for the goldfish emulator
49 + * Copyright (C) 2008 Google, Inc.
50 + * Author: Mike Lockwood <lockwood@android.com>
52 + * This software is licensed under the terms of the GNU General Public
53 + * License version 2, as published by the Free Software Foundation, and
54 + * may be copied, distributed, and modified under those terms.
56 + * This program is distributed in the hope that it will be useful,
57 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
58 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59 + * GNU General Public License for more details.
63 +#include <linux/module.h>
64 +#include <linux/err.h>
65 +#include <linux/platform_device.h>
66 +#include <linux/power_supply.h>
67 +#include <linux/types.h>
68 +#include <linux/pci.h>
69 +#include <linux/interrupt.h>
73 +struct goldfish_battery_data {
78 + struct power_supply battery;
79 + struct power_supply ac;
82 +#define GOLDFISH_BATTERY_READ(data, addr) (readl(data->reg_base + addr))
83 +#define GOLDFISH_BATTERY_WRITE(data, addr, x) (writel(x, data->reg_base + addr))
86 +/* temporary variable used between goldfish_battery_probe() and goldfish_battery_open() */
87 +static struct goldfish_battery_data *battery_data;
90 + /* status register */
91 + BATTERY_INT_STATUS = 0x00,
92 + /* set this to enable IRQ */
93 + BATTERY_INT_ENABLE = 0x04,
95 + BATTERY_AC_ONLINE = 0x08,
96 + BATTERY_STATUS = 0x0C,
97 + BATTERY_HEALTH = 0x10,
98 + BATTERY_PRESENT = 0x14,
99 + BATTERY_CAPACITY = 0x18,
101 + BATTERY_STATUS_CHANGED = 1U << 0,
102 + AC_STATUS_CHANGED = 1U << 1,
103 + BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
107 +static int goldfish_ac_get_property(struct power_supply *psy,
108 + enum power_supply_property psp,
109 + union power_supply_propval *val)
111 + struct goldfish_battery_data *data = container_of(psy,
112 + struct goldfish_battery_data, ac);
116 + case POWER_SUPPLY_PROP_ONLINE:
117 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_AC_ONLINE);
126 +static int goldfish_battery_get_property(struct power_supply *psy,
127 + enum power_supply_property psp,
128 + union power_supply_propval *val)
130 + struct goldfish_battery_data *data = container_of(psy,
131 + struct goldfish_battery_data, battery);
135 + case POWER_SUPPLY_PROP_STATUS:
136 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_STATUS);
138 + case POWER_SUPPLY_PROP_HEALTH:
139 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_HEALTH);
141 + case POWER_SUPPLY_PROP_PRESENT:
142 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_PRESENT);
144 + case POWER_SUPPLY_PROP_TECHNOLOGY:
145 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
147 + case POWER_SUPPLY_PROP_CAPACITY:
148 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
158 +static enum power_supply_property goldfish_battery_props[] = {
159 + POWER_SUPPLY_PROP_STATUS,
160 + POWER_SUPPLY_PROP_HEALTH,
161 + POWER_SUPPLY_PROP_PRESENT,
162 + POWER_SUPPLY_PROP_TECHNOLOGY,
163 + POWER_SUPPLY_PROP_CAPACITY,
166 +static enum power_supply_property goldfish_ac_props[] = {
167 + POWER_SUPPLY_PROP_ONLINE,
170 +static irqreturn_t goldfish_battery_interrupt(int irq, void *dev_id)
172 + unsigned long irq_flags;
173 + struct goldfish_battery_data *data = dev_id;
176 + spin_lock_irqsave(&data->lock, irq_flags);
178 + /* read status flags, which will clear the interrupt */
179 + status = GOLDFISH_BATTERY_READ(data, BATTERY_INT_STATUS);
180 + status &= BATTERY_INT_MASK;
182 + if (status & BATTERY_STATUS_CHANGED)
183 + power_supply_changed(&data->battery);
184 + if (status & AC_STATUS_CHANGED)
185 + power_supply_changed(&data->ac);
187 + spin_unlock_irqrestore(&data->lock, irq_flags);
188 + return status ? IRQ_HANDLED : IRQ_NONE;
192 +static int goldfish_battery_probe(struct platform_device *pdev)
195 + struct resource *r;
196 + struct goldfish_battery_data *data;
198 + data = kzalloc(sizeof(*data), GFP_KERNEL);
199 + if (data == NULL) {
201 + goto err_data_alloc_failed;
203 + spin_lock_init(&data->lock);
205 + data->battery.properties = goldfish_battery_props;
206 + data->battery.num_properties = ARRAY_SIZE(goldfish_battery_props);
207 + data->battery.get_property = goldfish_battery_get_property;
208 + data->battery.name = "battery";
209 + data->battery.type = POWER_SUPPLY_TYPE_BATTERY;
211 + data->ac.properties = goldfish_ac_props;
212 + data->ac.num_properties = ARRAY_SIZE(goldfish_ac_props);
213 + data->ac.get_property = goldfish_ac_get_property;
214 + data->ac.name = "ac";
215 + data->ac.type = POWER_SUPPLY_TYPE_MAINS;
217 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
219 + printk(KERN_ERR "%s: platform_get_resource failed\n", pdev->name);
221 + goto err_no_io_base;
223 + data->reg_base = IO_ADDRESS(r->start - IO_START);
225 + data->irq = platform_get_irq(pdev, 0);
226 + if (data->irq < 0) {
227 + printk(KERN_ERR "%s: platform_get_irq failed\n", pdev->name);
232 + ret = request_irq(data->irq, goldfish_battery_interrupt, IRQF_SHARED, pdev->name, data);
234 + goto err_request_irq_failed;
236 + ret = power_supply_register(&pdev->dev, &data->ac);
238 + goto err_ac_failed;
240 + ret = power_supply_register(&pdev->dev, &data->battery);
242 + goto err_battery_failed;
244 + platform_set_drvdata(pdev, data);
245 + battery_data = data;
247 + GOLDFISH_BATTERY_WRITE(data, BATTERY_INT_ENABLE, BATTERY_INT_MASK);
251 + power_supply_unregister(&data->ac);
253 + free_irq(data->irq, data);
254 +err_request_irq_failed:
258 +err_data_alloc_failed:
262 +static int goldfish_battery_remove(struct platform_device *pdev)
264 + struct goldfish_battery_data *data = platform_get_drvdata(pdev);
266 + power_supply_unregister(&data->battery);
267 + power_supply_unregister(&data->ac);
269 + free_irq(data->irq, data);
271 + battery_data = NULL;
275 +static struct platform_driver goldfish_battery_device = {
276 + .probe = goldfish_battery_probe,
277 + .remove = goldfish_battery_remove,
279 + .name = "goldfish-battery"
283 +static int __init goldfish_battery_init(void)
285 + return platform_driver_register(&goldfish_battery_device);
288 +static void __exit goldfish_battery_exit(void)
290 + platform_driver_unregister(&goldfish_battery_device);
293 +module_init(goldfish_battery_init);
294 +module_exit(goldfish_battery_exit);
296 +MODULE_AUTHOR("Mike Lockwood lockwood@android.com");
297 +MODULE_LICENSE("GPL");
298 +MODULE_DESCRIPTION("Battery driver for the Goldfish emulator");