1 From a48343043ec8d4095512e9758308ba4c4422151f Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 19 Jun 2010 04:08:29 +0000
4 Subject: [PATCH] POWER: Add JZ4740 battery driver.
6 Add support for the battery voltage measurement part of the JZ4740 ADC unit.
8 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
9 Acked-by: Anton Vorontsov <cbouatmailru@gmail.com>
10 Cc: linux-mips@linux-mips.org
11 Cc: linux-kernel@vger.kernel.org
12 Patchwork: https://patchwork.linux-mips.org/patch/1416/
13 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
15 drivers/power/Kconfig | 11 +
16 drivers/power/Makefile | 1 +
17 drivers/power/jz4740-battery.c | 445 ++++++++++++++++++++++++++++++++++
18 include/linux/power/jz4740-battery.h | 24 ++
19 4 files changed, 481 insertions(+), 0 deletions(-)
20 create mode 100644 drivers/power/jz4740-battery.c
21 create mode 100644 include/linux/power/jz4740-battery.h
23 --- a/drivers/power/Kconfig
24 +++ b/drivers/power/Kconfig
25 @@ -142,4 +142,15 @@ config CHARGER_PCF50633
27 Say Y to include support for NXP PCF50633 Main Battery Charger.
29 +config BATTERY_JZ4740
30 + tristate "Ingenic JZ4740 battery"
31 + depends on MACH_JZ4740
32 + depends on MFD_JZ4740_ADC
34 + Say Y to enable support for the battery on Ingenic JZ4740 based
37 + This driver can be build as a module. If so, the module will be
38 + called jz4740-battery.
41 --- a/drivers/power/Makefile
42 +++ b/drivers/power/Makefile
43 @@ -34,3 +34,4 @@ obj-$(CONFIG_BATTERY_DA9030) += da9030_b
44 obj-$(CONFIG_BATTERY_MAX17040) += max17040_battery.o
45 obj-$(CONFIG_BATTERY_Z2) += z2_battery.o
46 obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o
47 +obj-$(CONFIG_BATTERY_JZ4740) += jz4740-battery.o
49 +++ b/drivers/power/jz4740-battery.c
52 + * Battery measurement code for Ingenic JZ SOC.
54 + * Copyright (C) 2009 Jiejing Zhang <kzjeef@gmail.com>
55 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
57 + * based on tosa_battery.c
59 + * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
61 + * This program is free software; you can redistribute it and/or modify
62 + * it under the terms of the GNU General Public License version 2 as
63 + * published by the Free Software Foundation.
67 +#include <linux/interrupt.h>
68 +#include <linux/kernel.h>
69 +#include <linux/module.h>
70 +#include <linux/platform_device.h>
71 +#include <linux/slab.h>
73 +#include <linux/delay.h>
74 +#include <linux/gpio.h>
75 +#include <linux/mfd/core.h>
76 +#include <linux/power_supply.h>
78 +#include <linux/power/jz4740-battery.h>
79 +#include <linux/jz4740-adc.h>
82 + struct jz_battery_platform_data *pdata;
83 + struct platform_device *pdev;
85 + struct resource *mem;
91 + struct mfd_cell *cell;
96 + struct completion read_completion;
98 + struct power_supply battery;
99 + struct delayed_work work;
102 +static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
104 + return container_of(psy, struct jz_battery, battery);
107 +static irqreturn_t jz_battery_irq_handler(int irq, void *devid)
109 + struct jz_battery *battery = devid;
111 + complete(&battery->read_completion);
112 + return IRQ_HANDLED;
115 +static long jz_battery_read_voltage(struct jz_battery *battery)
121 + INIT_COMPLETION(battery->read_completion);
123 + enable_irq(battery->irq);
124 + battery->cell->enable(battery->pdev);
126 + t = wait_for_completion_interruptible_timeout(&battery->read_completion,
130 + val = readw(battery->base) & 0xfff;
132 + if (battery->pdata->info.voltage_max_design <= 2500000)
133 + val = (val * 78125UL) >> 7UL;
135 + val = ((val * 924375UL) >> 9UL) + 33000;
136 + voltage = (long)val;
138 + voltage = t ? t : -ETIMEDOUT;
141 + battery->cell->disable(battery->pdev);
142 + disable_irq(battery->irq);
147 +static int jz_battery_get_capacity(struct power_supply *psy)
149 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
150 + struct power_supply_info *info = &jz_battery->pdata->info;
155 + voltage = jz_battery_read_voltage(jz_battery);
160 + voltage_span = info->voltage_max_design - info->voltage_min_design;
161 + ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
171 +static int jz_battery_get_property(struct power_supply *psy,
172 + enum power_supply_property psp, union power_supply_propval *val)
174 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
175 + struct power_supply_info *info = &jz_battery->pdata->info;
179 + case POWER_SUPPLY_PROP_STATUS:
180 + val->intval = jz_battery->status;
182 + case POWER_SUPPLY_PROP_TECHNOLOGY:
183 + val->intval = jz_battery->pdata->info.technology;
185 + case POWER_SUPPLY_PROP_HEALTH:
186 + voltage = jz_battery_read_voltage(jz_battery);
187 + if (voltage < info->voltage_min_design)
188 + val->intval = POWER_SUPPLY_HEALTH_DEAD;
190 + val->intval = POWER_SUPPLY_HEALTH_GOOD;
192 + case POWER_SUPPLY_PROP_CAPACITY:
193 + val->intval = jz_battery_get_capacity(psy);
195 + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
196 + val->intval = jz_battery_read_voltage(jz_battery);
197 + if (val->intval < 0)
198 + return val->intval;
200 + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
201 + val->intval = info->voltage_max_design;
203 + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
204 + val->intval = info->voltage_min_design;
206 + case POWER_SUPPLY_PROP_PRESENT:
215 +static void jz_battery_external_power_changed(struct power_supply *psy)
217 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
219 + cancel_delayed_work(&jz_battery->work);
220 + schedule_delayed_work(&jz_battery->work, 0);
223 +static irqreturn_t jz_battery_charge_irq(int irq, void *data)
225 + struct jz_battery *jz_battery = data;
227 + cancel_delayed_work(&jz_battery->work);
228 + schedule_delayed_work(&jz_battery->work, 0);
230 + return IRQ_HANDLED;
233 +static void jz_battery_update(struct jz_battery *jz_battery)
237 + bool has_changed = false;
240 + if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
241 + is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
242 + is_charging ^= jz_battery->pdata->gpio_charge_active_low;
244 + status = POWER_SUPPLY_STATUS_CHARGING;
246 + status = POWER_SUPPLY_STATUS_NOT_CHARGING;
248 + if (status != jz_battery->status) {
249 + jz_battery->status = status;
250 + has_changed = true;
254 + voltage = jz_battery_read_voltage(jz_battery);
255 + if (abs(voltage - jz_battery->voltage) < 50000) {
256 + jz_battery->voltage = voltage;
257 + has_changed = true;
261 + power_supply_changed(&jz_battery->battery);
264 +static enum power_supply_property jz_battery_properties[] = {
265 + POWER_SUPPLY_PROP_STATUS,
266 + POWER_SUPPLY_PROP_TECHNOLOGY,
267 + POWER_SUPPLY_PROP_HEALTH,
268 + POWER_SUPPLY_PROP_CAPACITY,
269 + POWER_SUPPLY_PROP_VOLTAGE_NOW,
270 + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
271 + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
272 + POWER_SUPPLY_PROP_PRESENT,
275 +static void jz_battery_work(struct work_struct *work)
277 + /* Too small interval will increase system workload */
278 + const int interval = HZ * 30;
279 + struct jz_battery *jz_battery = container_of(work, struct jz_battery,
282 + jz_battery_update(jz_battery);
283 + schedule_delayed_work(&jz_battery->work, interval);
286 +static int __devinit jz_battery_probe(struct platform_device *pdev)
289 + struct jz_battery_platform_data *pdata = pdev->dev.parent->platform_data;
290 + struct jz_battery *jz_battery;
291 + struct power_supply *battery;
293 + jz_battery = kzalloc(sizeof(*jz_battery), GFP_KERNEL);
295 + dev_err(&pdev->dev, "Failed to allocate driver structure\n");
299 + jz_battery->cell = pdev->dev.platform_data;
301 + jz_battery->irq = platform_get_irq(pdev, 0);
302 + if (jz_battery->irq < 0) {
303 + ret = jz_battery->irq;
304 + dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
308 + jz_battery->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309 + if (!jz_battery->mem) {
311 + dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
315 + jz_battery->mem = request_mem_region(jz_battery->mem->start,
316 + resource_size(jz_battery->mem), pdev->name);
317 + if (!jz_battery->mem) {
319 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
323 + jz_battery->base = ioremap_nocache(jz_battery->mem->start,
324 + resource_size(jz_battery->mem));
325 + if (!jz_battery->base) {
327 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
328 + goto err_release_mem_region;
331 + battery = &jz_battery->battery;
332 + battery->name = pdata->info.name;
333 + battery->type = POWER_SUPPLY_TYPE_BATTERY;
334 + battery->properties = jz_battery_properties;
335 + battery->num_properties = ARRAY_SIZE(jz_battery_properties);
336 + battery->get_property = jz_battery_get_property;
337 + battery->external_power_changed = jz_battery_external_power_changed;
338 + battery->use_for_apm = 1;
340 + jz_battery->pdata = pdata;
341 + jz_battery->pdev = pdev;
343 + init_completion(&jz_battery->read_completion);
345 + INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
347 + ret = request_irq(jz_battery->irq, jz_battery_irq_handler, 0, pdev->name,
350 + dev_err(&pdev->dev, "Failed to request irq %d\n", ret);
353 + disable_irq(jz_battery->irq);
355 + if (gpio_is_valid(pdata->gpio_charge)) {
356 + ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
358 + dev_err(&pdev->dev, "charger state gpio request failed.\n");
361 + ret = gpio_direction_input(pdata->gpio_charge);
363 + dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
364 + goto err_free_gpio;
367 + jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
369 + if (jz_battery->charge_irq >= 0) {
370 + ret = request_irq(jz_battery->charge_irq,
371 + jz_battery_charge_irq,
372 + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
373 + dev_name(&pdev->dev), jz_battery);
375 + dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
376 + goto err_free_gpio;
380 + jz_battery->charge_irq = -1;
383 + if (jz_battery->pdata->info.voltage_max_design <= 2500000)
384 + jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB,
385 + JZ_ADC_CONFIG_BAT_MB);
387 + jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB, 0);
389 + ret = power_supply_register(&pdev->dev, &jz_battery->battery);
391 + dev_err(&pdev->dev, "power supply battery register failed.\n");
392 + goto err_free_charge_irq;
395 + platform_set_drvdata(pdev, jz_battery);
396 + schedule_delayed_work(&jz_battery->work, 0);
400 +err_free_charge_irq:
401 + if (jz_battery->charge_irq >= 0)
402 + free_irq(jz_battery->charge_irq, jz_battery);
404 + if (gpio_is_valid(pdata->gpio_charge))
405 + gpio_free(jz_battery->pdata->gpio_charge);
407 + free_irq(jz_battery->irq, jz_battery);
409 + platform_set_drvdata(pdev, NULL);
410 + iounmap(jz_battery->base);
411 +err_release_mem_region:
412 + release_mem_region(jz_battery->mem->start, resource_size(jz_battery->mem));
418 +static int __devexit jz_battery_remove(struct platform_device *pdev)
420 + struct jz_battery *jz_battery = platform_get_drvdata(pdev);
422 + cancel_delayed_work_sync(&jz_battery->work);
424 + if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
425 + if (jz_battery->charge_irq >= 0)
426 + free_irq(jz_battery->charge_irq, jz_battery);
427 + gpio_free(jz_battery->pdata->gpio_charge);
430 + power_supply_unregister(&jz_battery->battery);
432 + free_irq(jz_battery->irq, jz_battery);
434 + iounmap(jz_battery->base);
435 + release_mem_region(jz_battery->mem->start, resource_size(jz_battery->mem));
441 +static int jz_battery_suspend(struct device *dev)
443 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
445 + cancel_delayed_work_sync(&jz_battery->work);
446 + jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
451 +static int jz_battery_resume(struct device *dev)
453 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
455 + schedule_delayed_work(&jz_battery->work, 0);
460 +static const struct dev_pm_ops jz_battery_pm_ops = {
461 + .suspend = jz_battery_suspend,
462 + .resume = jz_battery_resume,
465 +#define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
467 +#define JZ_BATTERY_PM_OPS NULL
470 +static struct platform_driver jz_battery_driver = {
471 + .probe = jz_battery_probe,
472 + .remove = __devexit_p(jz_battery_remove),
474 + .name = "jz4740-battery",
475 + .owner = THIS_MODULE,
476 + .pm = JZ_BATTERY_PM_OPS,
480 +static int __init jz_battery_init(void)
482 + return platform_driver_register(&jz_battery_driver);
484 +module_init(jz_battery_init);
486 +static void __exit jz_battery_exit(void)
488 + platform_driver_unregister(&jz_battery_driver);
490 +module_exit(jz_battery_exit);
492 +MODULE_ALIAS("platform:jz4740-battery");
493 +MODULE_LICENSE("GPL");
494 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
495 +MODULE_DESCRIPTION("JZ4740 SoC battery driver");
497 +++ b/include/linux/power/jz4740-battery.h
500 + * Copyright (C) 2009, Jiejing Zhang <kzjeef@gmail.com>
502 + * This program is free software; you can redistribute it and/or modify it
503 + * under the terms of the GNU General Public License as published by the
504 + * Free Software Foundation; either version 2 of the License, or (at your
505 + * option) any later version.
507 + * You should have received a copy of the GNU General Public License along
508 + * with this program; if not, write to the Free Software Foundation, Inc.,
509 + * 675 Mass Ave, Cambridge, MA 02139, USA.
513 +#ifndef __JZ4740_BATTERY_H
514 +#define __JZ4740_BATTERY_H
516 +struct jz_battery_platform_data {
517 + struct power_supply_info info;
518 + int gpio_charge; /* GPIO port of Charger state */
519 + int gpio_charge_active_low;