add the 'goldfish' target, useful for experimenting with virtual phone hardware ...
[openwrt/svn-archive/archive.git] / target / linux / goldfish / patches-2.6.30 / 0126--ARM-goldfish-POWER-New-power-supply-driver-for-g.patch
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.
5
6 Signed-off-by: Mike A. Chan <mikechan@google.com>
7 ---
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
13
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
19 help
20 Say Y to include support for NXP PCF50633 Main Battery Charger.
21
22 +config BATTERY_GOLDFISH
23 + tristate "Goldfish battery driver"
24 + help
25 + Say Y to enable support for the battery and AC power in the Goldfish emulator.
26 +
27 endif # POWER_SUPPLY
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
34
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
47 new file mode 100644
48 index 0000000..868dc83
49 --- /dev/null
50 +++ b/drivers/power/goldfish_battery.c
51 @@ -0,0 +1,254 @@
52 +/* drivers/power/goldfish_battery.c
53 + *
54 + * Power supply driver for the goldfish emulator
55 + *
56 + * Copyright (C) 2008 Google, Inc.
57 + * Author: Mike Lockwood <lockwood@android.com>
58 + *
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.
62 + *
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.
67 + *
68 + */
69 +
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>
77 +#include <asm/io.h>
78 +
79 +
80 +struct goldfish_battery_data {
81 + uint32_t reg_base;
82 + int irq;
83 + spinlock_t lock;
84 +
85 + struct power_supply battery;
86 + struct power_supply ac;
87 +};
88 +
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))
91 +
92 +
93 +/* temporary variable used between goldfish_battery_probe() and goldfish_battery_open() */
94 +static struct goldfish_battery_data *battery_data;
95 +
96 +enum {
97 + /* status register */
98 + BATTERY_INT_STATUS = 0x00,
99 + /* set this to enable IRQ */
100 + BATTERY_INT_ENABLE = 0x04,
101 +
102 + BATTERY_AC_ONLINE = 0x08,
103 + BATTERY_STATUS = 0x0C,
104 + BATTERY_HEALTH = 0x10,
105 + BATTERY_PRESENT = 0x14,
106 + BATTERY_CAPACITY = 0x18,
107 +
108 + BATTERY_STATUS_CHANGED = 1U << 0,
109 + AC_STATUS_CHANGED = 1U << 1,
110 + BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
111 +};
112 +
113 +
114 +static int goldfish_ac_get_property(struct power_supply *psy,
115 + enum power_supply_property psp,
116 + union power_supply_propval *val)
117 +{
118 + struct goldfish_battery_data *data = container_of(psy,
119 + struct goldfish_battery_data, ac);
120 + int ret = 0;
121 +
122 + switch (psp) {
123 + case POWER_SUPPLY_PROP_ONLINE:
124 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_AC_ONLINE);
125 + break;
126 + default:
127 + ret = -EINVAL;
128 + break;
129 + }
130 + return ret;
131 +}
132 +
133 +static int goldfish_battery_get_property(struct power_supply *psy,
134 + enum power_supply_property psp,
135 + union power_supply_propval *val)
136 +{
137 + struct goldfish_battery_data *data = container_of(psy,
138 + struct goldfish_battery_data, battery);
139 + int ret = 0;
140 +
141 + switch (psp) {
142 + case POWER_SUPPLY_PROP_STATUS:
143 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_STATUS);
144 + break;
145 + case POWER_SUPPLY_PROP_HEALTH:
146 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_HEALTH);
147 + break;
148 + case POWER_SUPPLY_PROP_PRESENT:
149 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_PRESENT);
150 + break;
151 + case POWER_SUPPLY_PROP_TECHNOLOGY:
152 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
153 + break;
154 + case POWER_SUPPLY_PROP_CAPACITY:
155 + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
156 + break;
157 + default:
158 + ret = -EINVAL;
159 + break;
160 + }
161 +
162 + return ret;
163 +}
164 +
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,
171 +};
172 +
173 +static enum power_supply_property goldfish_ac_props[] = {
174 + POWER_SUPPLY_PROP_ONLINE,
175 +};
176 +
177 +static irqreturn_t goldfish_battery_interrupt(int irq, void *dev_id)
178 +{
179 + unsigned long irq_flags;
180 + struct goldfish_battery_data *data = dev_id;
181 + uint32_t status;
182 +
183 + spin_lock_irqsave(&data->lock, irq_flags);
184 +
185 + /* read status flags, which will clear the interrupt */
186 + status = GOLDFISH_BATTERY_READ(data, BATTERY_INT_STATUS);
187 + status &= BATTERY_INT_MASK;
188 +
189 + if (status & BATTERY_STATUS_CHANGED)
190 + power_supply_changed(&data->battery);
191 + if (status & AC_STATUS_CHANGED)
192 + power_supply_changed(&data->ac);
193 +
194 + spin_unlock_irqrestore(&data->lock, irq_flags);
195 + return status ? IRQ_HANDLED : IRQ_NONE;
196 +}
197 +
198 +
199 +static int goldfish_battery_probe(struct platform_device *pdev)
200 +{
201 + int ret;
202 + struct resource *r;
203 + struct goldfish_battery_data *data;
204 +
205 + data = kzalloc(sizeof(*data), GFP_KERNEL);
206 + if (data == NULL) {
207 + ret = -ENOMEM;
208 + goto err_data_alloc_failed;
209 + }
210 + spin_lock_init(&data->lock);
211 +
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;
217 +
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;
223 +
224 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
225 + if (r == NULL) {
226 + printk(KERN_ERR "%s: platform_get_resource failed\n", pdev->name);
227 + ret = -ENODEV;
228 + goto err_no_io_base;
229 + }
230 + data->reg_base = IO_ADDRESS(r->start - IO_START);
231 +
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);
235 + ret = -ENODEV;
236 + goto err_no_irq;
237 + }
238 +
239 + ret = request_irq(data->irq, goldfish_battery_interrupt, IRQF_SHARED, pdev->name, data);
240 + if (ret)
241 + goto err_request_irq_failed;
242 +
243 + ret = power_supply_register(&pdev->dev, &data->ac);
244 + if (ret)
245 + goto err_ac_failed;
246 +
247 + ret = power_supply_register(&pdev->dev, &data->battery);
248 + if (ret)
249 + goto err_battery_failed;
250 +
251 + platform_set_drvdata(pdev, data);
252 + battery_data = data;
253 +
254 + GOLDFISH_BATTERY_WRITE(data, BATTERY_INT_ENABLE, BATTERY_INT_MASK);
255 + return 0;
256 +
257 +err_battery_failed:
258 + power_supply_unregister(&data->ac);
259 +err_ac_failed:
260 + free_irq(data->irq, data);
261 +err_request_irq_failed:
262 +err_no_irq:
263 +err_no_io_base:
264 + kfree(data);
265 +err_data_alloc_failed:
266 + return ret;
267 +}
268 +
269 +static int goldfish_battery_remove(struct platform_device *pdev)
270 +{
271 + struct goldfish_battery_data *data = platform_get_drvdata(pdev);
272 +
273 + power_supply_unregister(&data->battery);
274 + power_supply_unregister(&data->ac);
275 +
276 + free_irq(data->irq, data);
277 + kfree(data);
278 + battery_data = NULL;
279 + return 0;
280 +}
281 +
282 +static struct platform_driver goldfish_battery_device = {
283 + .probe = goldfish_battery_probe,
284 + .remove = goldfish_battery_remove,
285 + .driver = {
286 + .name = "goldfish-battery"
287 + }
288 +};
289 +
290 +static int __init goldfish_battery_init(void)
291 +{
292 + return platform_driver_register(&goldfish_battery_device);
293 +}
294 +
295 +static void __exit goldfish_battery_exit(void)
296 +{
297 + platform_driver_unregister(&goldfish_battery_device);
298 +}
299 +
300 +module_init(goldfish_battery_init);
301 +module_exit(goldfish_battery_exit);
302 +
303 +MODULE_AUTHOR("Mike Lockwood lockwood@android.com");
304 +MODULE_LICENSE("GPL");
305 +MODULE_DESCRIPTION("Battery driver for the Goldfish emulator");
306 --
307 1.6.2
308