| 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 | --- a/drivers/power/Kconfig |
| 15 | +++ b/drivers/power/Kconfig |
| 16 | @@ -88,4 +88,9 @@ config CHARGER_PCF50633 |
| 17 | help |
| 18 | Say Y to include support for NXP PCF50633 Main Battery Charger. |
| 19 | |
| 20 | +config BATTERY_GOLDFISH |
| 21 | + tristate "Goldfish battery driver" |
| 22 | + help |
| 23 | + Say Y to enable support for the battery and AC power in the Goldfish emulator. |
| 24 | + |
| 25 | endif # POWER_SUPPLY |
| 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 |
| 30 | |
| 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 |
| 42 | --- /dev/null |
| 43 | +++ b/drivers/power/goldfish_battery.c |
| 44 | @@ -0,0 +1,254 @@ |
| 45 | +/* drivers/power/goldfish_battery.c |
| 46 | + * |
| 47 | + * Power supply driver for the goldfish emulator |
| 48 | + * |
| 49 | + * Copyright (C) 2008 Google, Inc. |
| 50 | + * Author: Mike Lockwood <lockwood@android.com> |
| 51 | + * |
| 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. |
| 55 | + * |
| 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. |
| 60 | + * |
| 61 | + */ |
| 62 | + |
| 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> |
| 70 | +#include <asm/io.h> |
| 71 | + |
| 72 | + |
| 73 | +struct goldfish_battery_data { |
| 74 | + uint32_t reg_base; |
| 75 | + int irq; |
| 76 | + spinlock_t lock; |
| 77 | + |
| 78 | + struct power_supply battery; |
| 79 | + struct power_supply ac; |
| 80 | +}; |
| 81 | + |
| 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)) |
| 84 | + |
| 85 | + |
| 86 | +/* temporary variable used between goldfish_battery_probe() and goldfish_battery_open() */ |
| 87 | +static struct goldfish_battery_data *battery_data; |
| 88 | + |
| 89 | +enum { |
| 90 | + /* status register */ |
| 91 | + BATTERY_INT_STATUS = 0x00, |
| 92 | + /* set this to enable IRQ */ |
| 93 | + BATTERY_INT_ENABLE = 0x04, |
| 94 | + |
| 95 | + BATTERY_AC_ONLINE = 0x08, |
| 96 | + BATTERY_STATUS = 0x0C, |
| 97 | + BATTERY_HEALTH = 0x10, |
| 98 | + BATTERY_PRESENT = 0x14, |
| 99 | + BATTERY_CAPACITY = 0x18, |
| 100 | + |
| 101 | + BATTERY_STATUS_CHANGED = 1U << 0, |
| 102 | + AC_STATUS_CHANGED = 1U << 1, |
| 103 | + BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED, |
| 104 | +}; |
| 105 | + |
| 106 | + |
| 107 | +static int goldfish_ac_get_property(struct power_supply *psy, |
| 108 | + enum power_supply_property psp, |
| 109 | + union power_supply_propval *val) |
| 110 | +{ |
| 111 | + struct goldfish_battery_data *data = container_of(psy, |
| 112 | + struct goldfish_battery_data, ac); |
| 113 | + int ret = 0; |
| 114 | + |
| 115 | + switch (psp) { |
| 116 | + case POWER_SUPPLY_PROP_ONLINE: |
| 117 | + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_AC_ONLINE); |
| 118 | + break; |
| 119 | + default: |
| 120 | + ret = -EINVAL; |
| 121 | + break; |
| 122 | + } |
| 123 | + return ret; |
| 124 | +} |
| 125 | + |
| 126 | +static int goldfish_battery_get_property(struct power_supply *psy, |
| 127 | + enum power_supply_property psp, |
| 128 | + union power_supply_propval *val) |
| 129 | +{ |
| 130 | + struct goldfish_battery_data *data = container_of(psy, |
| 131 | + struct goldfish_battery_data, battery); |
| 132 | + int ret = 0; |
| 133 | + |
| 134 | + switch (psp) { |
| 135 | + case POWER_SUPPLY_PROP_STATUS: |
| 136 | + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_STATUS); |
| 137 | + break; |
| 138 | + case POWER_SUPPLY_PROP_HEALTH: |
| 139 | + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_HEALTH); |
| 140 | + break; |
| 141 | + case POWER_SUPPLY_PROP_PRESENT: |
| 142 | + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_PRESENT); |
| 143 | + break; |
| 144 | + case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 145 | + val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 146 | + break; |
| 147 | + case POWER_SUPPLY_PROP_CAPACITY: |
| 148 | + val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY); |
| 149 | + break; |
| 150 | + default: |
| 151 | + ret = -EINVAL; |
| 152 | + break; |
| 153 | + } |
| 154 | + |
| 155 | + return ret; |
| 156 | +} |
| 157 | + |
| 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, |
| 164 | +}; |
| 165 | + |
| 166 | +static enum power_supply_property goldfish_ac_props[] = { |
| 167 | + POWER_SUPPLY_PROP_ONLINE, |
| 168 | +}; |
| 169 | + |
| 170 | +static irqreturn_t goldfish_battery_interrupt(int irq, void *dev_id) |
| 171 | +{ |
| 172 | + unsigned long irq_flags; |
| 173 | + struct goldfish_battery_data *data = dev_id; |
| 174 | + uint32_t status; |
| 175 | + |
| 176 | + spin_lock_irqsave(&data->lock, irq_flags); |
| 177 | + |
| 178 | + /* read status flags, which will clear the interrupt */ |
| 179 | + status = GOLDFISH_BATTERY_READ(data, BATTERY_INT_STATUS); |
| 180 | + status &= BATTERY_INT_MASK; |
| 181 | + |
| 182 | + if (status & BATTERY_STATUS_CHANGED) |
| 183 | + power_supply_changed(&data->battery); |
| 184 | + if (status & AC_STATUS_CHANGED) |
| 185 | + power_supply_changed(&data->ac); |
| 186 | + |
| 187 | + spin_unlock_irqrestore(&data->lock, irq_flags); |
| 188 | + return status ? IRQ_HANDLED : IRQ_NONE; |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | +static int goldfish_battery_probe(struct platform_device *pdev) |
| 193 | +{ |
| 194 | + int ret; |
| 195 | + struct resource *r; |
| 196 | + struct goldfish_battery_data *data; |
| 197 | + |
| 198 | + data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 199 | + if (data == NULL) { |
| 200 | + ret = -ENOMEM; |
| 201 | + goto err_data_alloc_failed; |
| 202 | + } |
| 203 | + spin_lock_init(&data->lock); |
| 204 | + |
| 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; |
| 210 | + |
| 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; |
| 216 | + |
| 217 | + r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 218 | + if (r == NULL) { |
| 219 | + printk(KERN_ERR "%s: platform_get_resource failed\n", pdev->name); |
| 220 | + ret = -ENODEV; |
| 221 | + goto err_no_io_base; |
| 222 | + } |
| 223 | + data->reg_base = IO_ADDRESS(r->start - IO_START); |
| 224 | + |
| 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); |
| 228 | + ret = -ENODEV; |
| 229 | + goto err_no_irq; |
| 230 | + } |
| 231 | + |
| 232 | + ret = request_irq(data->irq, goldfish_battery_interrupt, IRQF_SHARED, pdev->name, data); |
| 233 | + if (ret) |
| 234 | + goto err_request_irq_failed; |
| 235 | + |
| 236 | + ret = power_supply_register(&pdev->dev, &data->ac); |
| 237 | + if (ret) |
| 238 | + goto err_ac_failed; |
| 239 | + |
| 240 | + ret = power_supply_register(&pdev->dev, &data->battery); |
| 241 | + if (ret) |
| 242 | + goto err_battery_failed; |
| 243 | + |
| 244 | + platform_set_drvdata(pdev, data); |
| 245 | + battery_data = data; |
| 246 | + |
| 247 | + GOLDFISH_BATTERY_WRITE(data, BATTERY_INT_ENABLE, BATTERY_INT_MASK); |
| 248 | + return 0; |
| 249 | + |
| 250 | +err_battery_failed: |
| 251 | + power_supply_unregister(&data->ac); |
| 252 | +err_ac_failed: |
| 253 | + free_irq(data->irq, data); |
| 254 | +err_request_irq_failed: |
| 255 | +err_no_irq: |
| 256 | +err_no_io_base: |
| 257 | + kfree(data); |
| 258 | +err_data_alloc_failed: |
| 259 | + return ret; |
| 260 | +} |
| 261 | + |
| 262 | +static int goldfish_battery_remove(struct platform_device *pdev) |
| 263 | +{ |
| 264 | + struct goldfish_battery_data *data = platform_get_drvdata(pdev); |
| 265 | + |
| 266 | + power_supply_unregister(&data->battery); |
| 267 | + power_supply_unregister(&data->ac); |
| 268 | + |
| 269 | + free_irq(data->irq, data); |
| 270 | + kfree(data); |
| 271 | + battery_data = NULL; |
| 272 | + return 0; |
| 273 | +} |
| 274 | + |
| 275 | +static struct platform_driver goldfish_battery_device = { |
| 276 | + .probe = goldfish_battery_probe, |
| 277 | + .remove = goldfish_battery_remove, |
| 278 | + .driver = { |
| 279 | + .name = "goldfish-battery" |
| 280 | + } |
| 281 | +}; |
| 282 | + |
| 283 | +static int __init goldfish_battery_init(void) |
| 284 | +{ |
| 285 | + return platform_driver_register(&goldfish_battery_device); |
| 286 | +} |
| 287 | + |
| 288 | +static void __exit goldfish_battery_exit(void) |
| 289 | +{ |
| 290 | + platform_driver_unregister(&goldfish_battery_device); |
| 291 | +} |
| 292 | + |
| 293 | +module_init(goldfish_battery_init); |
| 294 | +module_exit(goldfish_battery_exit); |
| 295 | + |
| 296 | +MODULE_AUTHOR("Mike Lockwood lockwood@android.com"); |
| 297 | +MODULE_LICENSE("GPL"); |
| 298 | +MODULE_DESCRIPTION("Battery driver for the Goldfish emulator"); |
| 299 | |