1 From 83f97dcbcc93584a7d7930b6fe06562691bd865a Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 24 Apr 2010 12:28:54 +0200
4 Subject: [PATCH] Add jz4740 battery driver
7 drivers/power/Kconfig | 11 +
8 drivers/power/Makefile | 1 +
9 drivers/power/jz4740-battery.c | 359 ++++++++++++++++++++++++++++++++++
10 include/linux/power/jz4740-battery.h | 24 +++
11 4 files changed, 395 insertions(+), 0 deletions(-)
12 create mode 100644 drivers/power/jz4740-battery.c
13 create mode 100644 include/linux/power/jz4740-battery.h
15 diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
16 index faaa9b4..2f4e51f 100644
17 --- a/drivers/power/Kconfig
18 +++ b/drivers/power/Kconfig
19 @@ -131,4 +131,15 @@ config CHARGER_PCF50633
21 Say Y to include support for NXP PCF50633 Main Battery Charger.
23 +config BATTERY_JZ4740
24 + tristate "Ingenic JZ4720/JZ4740 battery"
25 + depends on SOC_JZ4740
26 + depends on JZ4740_ADC
28 + Say Y to enable support for the battery on Ingenic JZ4720/JZ4740 based
31 + This driver can be build as a module. If so, the module will be
32 + called jz4740-battery.
35 diff --git a/drivers/power/Makefile b/drivers/power/Makefile
36 index a2ba7c8..3b6b971 100644
37 --- a/drivers/power/Makefile
38 +++ b/drivers/power/Makefile
39 @@ -32,3 +32,4 @@ obj-$(CONFIG_BATTERY_BQ27x00) += bq27x00_battery.o
40 obj-$(CONFIG_BATTERY_DA9030) += da9030_battery.o
41 obj-$(CONFIG_BATTERY_MAX17040) += max17040_battery.o
42 obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o
43 +obj-$(CONFIG_BATTERY_JZ4740) += jz4740-battery.o
44 diff --git a/drivers/power/jz4740-battery.c b/drivers/power/jz4740-battery.c
46 index 0000000..c89b9f7
48 +++ b/drivers/power/jz4740-battery.c
51 + * Battery measurement code for Ingenic JZ SOC.
53 + * Copyright (C) 2009 Jiejing Zhang <kzjeef@gmail.com>
54 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
56 + * based on tosa_battery.c
58 + * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
60 + * This program is free software; you can redistribute it and/or modify
61 + * it under the terms of the GNU General Public License version 2 as
62 + * published by the Free Software Foundation.
66 +#include <linux/interrupt.h>
67 +#include <linux/kernel.h>
68 +#include <linux/module.h>
69 +#include <linux/platform_device.h>
70 +#include <linux/slab.h>
71 +#include <linux/spinlock.h>
73 +#include <linux/delay.h>
74 +#include <linux/gpio.h>
75 +#include <linux/power_supply.h>
77 +#include <linux/power/jz4740-battery.h>
78 +#include <linux/jz4740-adc.h>
81 + struct jz_battery_platform_data *pdata;
88 + struct power_supply battery;
89 + struct delayed_work work;
92 +static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
94 + return container_of(psy, struct jz_battery, battery);
97 +static long jz_battery_read_voltage(struct jz_battery *jz_battery)
99 + struct device *adc = jz_battery->battery.dev->parent->parent;
100 + enum jz_adc_battery_scale scale;
102 + if (jz_battery->pdata->info.voltage_max_design > 2500000)
103 + scale = JZ_ADC_BATTERY_SCALE_7V5;
105 + scale = JZ_ADC_BATTERY_SCALE_2V5;
107 + return jz4740_adc_read_battery_voltage(adc, scale);
110 +static int jz_battery_get_capacity(struct power_supply *psy)
112 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
113 + struct power_supply_info *info = &jz_battery->pdata->info;
118 + voltage = jz_battery_read_voltage(jz_battery);
123 + voltage_span = info->voltage_max_design - info->voltage_min_design;
124 + ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
134 +static int jz_battery_get_property(struct power_supply *psy,
135 + enum power_supply_property psp,
136 + union power_supply_propval *val)
138 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
139 + struct power_supply_info *info = &jz_battery->pdata->info;
143 + case POWER_SUPPLY_PROP_STATUS:
144 + val->intval = jz_battery->status;
146 + case POWER_SUPPLY_PROP_TECHNOLOGY:
147 + val->intval = jz_battery->pdata->info.technology;
149 + case POWER_SUPPLY_PROP_HEALTH:
150 + voltage = jz_battery_read_voltage(jz_battery);
151 + if (voltage < info->voltage_min_design)
152 + val->intval = POWER_SUPPLY_HEALTH_DEAD;
154 + val->intval = POWER_SUPPLY_HEALTH_GOOD;
156 + case POWER_SUPPLY_PROP_CAPACITY:
157 + val->intval = jz_battery_get_capacity(psy);
159 + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
160 + val->intval = jz_battery_read_voltage(jz_battery);
161 + if (val->intval < 0)
162 + return val->intval;
164 + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
165 + val->intval = info->voltage_max_design;
167 + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
168 + val->intval = info->voltage_min_design;
170 + case POWER_SUPPLY_PROP_PRESENT:
179 +static void jz_battery_external_power_changed(struct power_supply *psy)
181 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
183 + cancel_delayed_work(&jz_battery->work);
184 + schedule_delayed_work(&jz_battery->work, 0);
187 +static irqreturn_t jz_battery_charge_irq(int irq, void *data)
189 + struct jz_battery *jz_battery = data;
191 + cancel_delayed_work(&jz_battery->work);
192 + schedule_delayed_work(&jz_battery->work, 0);
194 + return IRQ_HANDLED;
197 +static void jz_battery_update(struct jz_battery *jz_battery)
201 + long voltage_difference;
202 + bool has_changed = 0;
204 + if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
207 + is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
208 + is_charging ^= jz_battery->pdata->gpio_charge_active_low;
210 + status = POWER_SUPPLY_STATUS_CHARGING;
212 + status = POWER_SUPPLY_STATUS_NOT_CHARGING;
214 + if (status != jz_battery->status) {
215 + jz_battery->status = status;
220 + voltage = jz_battery_read_voltage(jz_battery);
221 + voltage_difference = voltage - jz_battery->voltage;
222 + if (voltage_difference > 50000 || voltage_difference < 50000) {
223 + jz_battery->voltage = voltage;
227 + power_supply_changed(&jz_battery->battery);
230 +static enum power_supply_property jz_battery_properties[] = {
231 + POWER_SUPPLY_PROP_STATUS,
232 + POWER_SUPPLY_PROP_TECHNOLOGY,
233 + POWER_SUPPLY_PROP_HEALTH,
234 + POWER_SUPPLY_PROP_CAPACITY,
235 + POWER_SUPPLY_PROP_VOLTAGE_NOW,
236 + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
237 + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
238 + POWER_SUPPLY_PROP_PRESENT,
241 +static void jz_battery_work(struct work_struct *work)
243 + /* Too small interval will increase system workload */
244 + const int interval = HZ * 30;
245 + struct jz_battery *jz_battery = container_of(work, struct jz_battery,
248 + jz_battery_update(jz_battery);
249 + schedule_delayed_work(&jz_battery->work, interval);
252 +static int __devinit jz_battery_probe(struct platform_device *pdev)
255 + struct jz_battery_platform_data *pdata = pdev->dev.platform_data;
256 + struct jz_battery *jz_battery;
257 + struct power_supply *battery;
259 + if (!pdev->dev.platform_data) {
260 + dev_err(&pdev->dev, "No platform data\n");
264 + jz_battery = kzalloc(sizeof(*jz_battery), GFP_KERNEL);
267 + dev_err(&pdev->dev, "Failed to allocate driver structure\n");
271 + battery = &jz_battery->battery;
272 + battery->name = pdata->info.name;
273 + battery->type = POWER_SUPPLY_TYPE_BATTERY;
274 + battery->properties = jz_battery_properties;
275 + battery->num_properties = ARRAY_SIZE(jz_battery_properties);
276 + battery->get_property = jz_battery_get_property;
277 + battery->external_power_changed = jz_battery_external_power_changed;
278 + battery->use_for_apm = 1;
280 + jz_battery->pdata = pdata;
282 + INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
284 + if (gpio_is_valid(pdata->gpio_charge)) {
285 + ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
287 + dev_err(&pdev->dev, "charger state gpio request failed.\n");
290 + ret = gpio_direction_input(pdata->gpio_charge);
292 + dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
293 + goto err_free_gpio;
296 + jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
298 + if (jz_battery->charge_irq >= 0) {
299 + ret = request_irq(jz_battery->charge_irq,
300 + jz_battery_charge_irq,
301 + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
302 + dev_name(&pdev->dev), jz_battery);
304 + dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
305 + goto err_free_gpio;
309 + jz_battery->charge_irq = -1;
313 + ret = power_supply_register(&pdev->dev, &jz_battery->battery);
315 + dev_err(&pdev->dev, "power supply battery register failed.\n");
319 + platform_set_drvdata(pdev, jz_battery);
320 + schedule_delayed_work(&jz_battery->work, 0);
325 + if (jz_battery->charge_irq >= 0)
326 + free_irq(jz_battery->charge_irq, jz_battery);
328 + if (gpio_is_valid(pdata->gpio_charge))
329 + gpio_free(jz_battery->pdata->gpio_charge);
335 +static int __devexit jz_battery_remove(struct platform_device *pdev)
337 + struct jz_battery *jz_battery = platform_get_drvdata(pdev);
339 + cancel_delayed_work_sync(&jz_battery->work);
341 + if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
342 + if (jz_battery->charge_irq >= 0)
343 + free_irq(jz_battery->charge_irq, jz_battery);
344 + gpio_free(jz_battery->pdata->gpio_charge);
347 + power_supply_unregister(&jz_battery->battery);
353 +static int jz_battery_suspend(struct device *dev)
355 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
357 + cancel_delayed_work_sync(&jz_battery->work);
358 + jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
363 +static int jz_battery_resume(struct device *dev)
365 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
367 + schedule_delayed_work(&jz_battery->work, 0);
372 +static const struct dev_pm_ops jz_battery_pm_ops = {
373 + .suspend = jz_battery_suspend,
374 + .resume = jz_battery_resume,
377 +#define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
380 +#define JZ_BATTERY_PM_OPS NULL
383 +static struct platform_driver jz_battery_driver = {
384 + .probe = jz_battery_probe,
385 + .remove = __devexit_p(jz_battery_remove),
387 + .name = "jz4740-battery",
388 + .owner = THIS_MODULE,
389 + .pm = JZ_BATTERY_PM_OPS,
393 +static int __init jz_battery_init(void)
395 + return platform_driver_register(&jz_battery_driver);
397 +module_init(jz_battery_init);
399 +static void __exit jz_battery_exit(void)
401 + platform_driver_unregister(&jz_battery_driver);
403 +module_exit(jz_battery_exit);
405 +MODULE_ALIAS("platform:jz4740-battery");
406 +MODULE_LICENSE("GPL");
407 +MODULE_AUTHOR("Jiejing Zhang <kzjeef@gmail.com>");
408 +MODULE_DESCRIPTION("JZ4720/JZ4740 SoC battery driver");
409 diff --git a/include/linux/power/jz4740-battery.h b/include/linux/power/jz4740-battery.h
411 index 0000000..19c9610
413 +++ b/include/linux/power/jz4740-battery.h
416 + * Copyright (C) 2009, Jiejing Zhang <kzjeef@gmail.com>
418 + * This program is free software; you can redistribute it and/or modify it
419 + * under the terms of the GNU General Public License as published by the
420 + * Free Software Foundation; either version 2 of the License, or (at your
421 + * option) any later version.
423 + * You should have received a copy of the GNU General Public License along
424 + * with this program; if not, write to the Free Software Foundation, Inc.,
425 + * 675 Mass Ave, Cambridge, MA 02139, USA.
429 +#ifndef __JZ4740_BATTERY_H
430 +#define __JZ4740_BATTERY_H
432 +struct jz_battery_platform_data {
433 + struct power_supply_info info;
434 + int gpio_charge; /* GPIO port of Charger state */
435 + int gpio_charge_active_low;