[xburst] Add 2.6.37 support
[openwrt.git] / target / linux / xburst / patches-2.6.35 / 058-battery.patch
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.
5
6 Add support for the battery voltage measurement part of the JZ4740 ADC unit.
7
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>
14 ---
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
22
23 --- a/drivers/power/Kconfig
24 +++ b/drivers/power/Kconfig
25 @@ -142,4 +142,15 @@ config CHARGER_PCF50633
26 help
27 Say Y to include support for NXP PCF50633 Main Battery Charger.
28
29 +config BATTERY_JZ4740
30 + tristate "Ingenic JZ4740 battery"
31 + depends on MACH_JZ4740
32 + depends on MFD_JZ4740_ADC
33 + help
34 + Say Y to enable support for the battery on Ingenic JZ4740 based
35 + boards.
36 +
37 + This driver can be build as a module. If so, the module will be
38 + called jz4740-battery.
39 +
40 endif # POWER_SUPPLY
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
48 --- /dev/null
49 +++ b/drivers/power/jz4740-battery.c
50 @@ -0,0 +1,445 @@
51 +/*
52 + * Battery measurement code for Ingenic JZ SOC.
53 + *
54 + * Copyright (C) 2009 Jiejing Zhang <kzjeef@gmail.com>
55 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
56 + *
57 + * based on tosa_battery.c
58 + *
59 + * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
60 +*
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.
64 + *
65 + */
66 +
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>
72 +
73 +#include <linux/delay.h>
74 +#include <linux/gpio.h>
75 +#include <linux/mfd/core.h>
76 +#include <linux/power_supply.h>
77 +
78 +#include <linux/power/jz4740-battery.h>
79 +#include <linux/jz4740-adc.h>
80 +
81 +struct jz_battery {
82 + struct jz_battery_platform_data *pdata;
83 + struct platform_device *pdev;
84 +
85 + struct resource *mem;
86 + void __iomem *base;
87 +
88 + int irq;
89 + int charge_irq;
90 +
91 + struct mfd_cell *cell;
92 +
93 + int status;
94 + long voltage;
95 +
96 + struct completion read_completion;
97 +
98 + struct power_supply battery;
99 + struct delayed_work work;
100 +};
101 +
102 +static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
103 +{
104 + return container_of(psy, struct jz_battery, battery);
105 +}
106 +
107 +static irqreturn_t jz_battery_irq_handler(int irq, void *devid)
108 +{
109 + struct jz_battery *battery = devid;
110 +
111 + complete(&battery->read_completion);
112 + return IRQ_HANDLED;
113 +}
114 +
115 +static long jz_battery_read_voltage(struct jz_battery *battery)
116 +{
117 + unsigned long t;
118 + unsigned long val;
119 + long voltage;
120 +
121 + INIT_COMPLETION(battery->read_completion);
122 +
123 + enable_irq(battery->irq);
124 + battery->cell->enable(battery->pdev);
125 +
126 + t = wait_for_completion_interruptible_timeout(&battery->read_completion,
127 + HZ);
128 +
129 + if (t > 0) {
130 + val = readw(battery->base) & 0xfff;
131 +
132 + if (battery->pdata->info.voltage_max_design <= 2500000)
133 + val = (val * 78125UL) >> 7UL;
134 + else
135 + val = ((val * 924375UL) >> 9UL) + 33000;
136 + voltage = (long)val;
137 + } else {
138 + voltage = t ? t : -ETIMEDOUT;
139 + }
140 +
141 + battery->cell->disable(battery->pdev);
142 + disable_irq(battery->irq);
143 +
144 + return voltage;
145 +}
146 +
147 +static int jz_battery_get_capacity(struct power_supply *psy)
148 +{
149 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
150 + struct power_supply_info *info = &jz_battery->pdata->info;
151 + long voltage;
152 + int ret;
153 + int voltage_span;
154 +
155 + voltage = jz_battery_read_voltage(jz_battery);
156 +
157 + if (voltage < 0)
158 + return voltage;
159 +
160 + voltage_span = info->voltage_max_design - info->voltage_min_design;
161 + ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
162 +
163 + if (ret > 100)
164 + ret = 100;
165 + else if (ret < 0)
166 + ret = 0;
167 +
168 + return ret;
169 +}
170 +
171 +static int jz_battery_get_property(struct power_supply *psy,
172 + enum power_supply_property psp, union power_supply_propval *val)
173 +{
174 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
175 + struct power_supply_info *info = &jz_battery->pdata->info;
176 + long voltage;
177 +
178 + switch (psp) {
179 + case POWER_SUPPLY_PROP_STATUS:
180 + val->intval = jz_battery->status;
181 + break;
182 + case POWER_SUPPLY_PROP_TECHNOLOGY:
183 + val->intval = jz_battery->pdata->info.technology;
184 + break;
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;
189 + else
190 + val->intval = POWER_SUPPLY_HEALTH_GOOD;
191 + break;
192 + case POWER_SUPPLY_PROP_CAPACITY:
193 + val->intval = jz_battery_get_capacity(psy);
194 + break;
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;
199 + break;
200 + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
201 + val->intval = info->voltage_max_design;
202 + break;
203 + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
204 + val->intval = info->voltage_min_design;
205 + break;
206 + case POWER_SUPPLY_PROP_PRESENT:
207 + val->intval = 1;
208 + break;
209 + default:
210 + return -EINVAL;
211 + }
212 + return 0;
213 +}
214 +
215 +static void jz_battery_external_power_changed(struct power_supply *psy)
216 +{
217 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
218 +
219 + cancel_delayed_work(&jz_battery->work);
220 + schedule_delayed_work(&jz_battery->work, 0);
221 +}
222 +
223 +static irqreturn_t jz_battery_charge_irq(int irq, void *data)
224 +{
225 + struct jz_battery *jz_battery = data;
226 +
227 + cancel_delayed_work(&jz_battery->work);
228 + schedule_delayed_work(&jz_battery->work, 0);
229 +
230 + return IRQ_HANDLED;
231 +}
232 +
233 +static void jz_battery_update(struct jz_battery *jz_battery)
234 +{
235 + int status;
236 + long voltage;
237 + bool has_changed = false;
238 + int is_charging;
239 +
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;
243 + if (is_charging)
244 + status = POWER_SUPPLY_STATUS_CHARGING;
245 + else
246 + status = POWER_SUPPLY_STATUS_NOT_CHARGING;
247 +
248 + if (status != jz_battery->status) {
249 + jz_battery->status = status;
250 + has_changed = true;
251 + }
252 + }
253 +
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;
258 + }
259 +
260 + if (has_changed)
261 + power_supply_changed(&jz_battery->battery);
262 +}
263 +
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,
273 +};
274 +
275 +static void jz_battery_work(struct work_struct *work)
276 +{
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,
280 + work.work);
281 +
282 + jz_battery_update(jz_battery);
283 + schedule_delayed_work(&jz_battery->work, interval);
284 +}
285 +
286 +static int __devinit jz_battery_probe(struct platform_device *pdev)
287 +{
288 + int ret = 0;
289 + struct jz_battery_platform_data *pdata = pdev->dev.parent->platform_data;
290 + struct jz_battery *jz_battery;
291 + struct power_supply *battery;
292 +
293 + jz_battery = kzalloc(sizeof(*jz_battery), GFP_KERNEL);
294 + if (!jz_battery) {
295 + dev_err(&pdev->dev, "Failed to allocate driver structure\n");
296 + return -ENOMEM;
297 + }
298 +
299 + jz_battery->cell = pdev->dev.platform_data;
300 +
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);
305 + goto err_free;
306 + }
307 +
308 + jz_battery->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309 + if (!jz_battery->mem) {
310 + ret = -ENOENT;
311 + dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
312 + goto err_free;
313 + }
314 +
315 + jz_battery->mem = request_mem_region(jz_battery->mem->start,
316 + resource_size(jz_battery->mem), pdev->name);
317 + if (!jz_battery->mem) {
318 + ret = -EBUSY;
319 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
320 + goto err_free;
321 + }
322 +
323 + jz_battery->base = ioremap_nocache(jz_battery->mem->start,
324 + resource_size(jz_battery->mem));
325 + if (!jz_battery->base) {
326 + ret = -EBUSY;
327 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
328 + goto err_release_mem_region;
329 + }
330 +
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;
339 +
340 + jz_battery->pdata = pdata;
341 + jz_battery->pdev = pdev;
342 +
343 + init_completion(&jz_battery->read_completion);
344 +
345 + INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
346 +
347 + ret = request_irq(jz_battery->irq, jz_battery_irq_handler, 0, pdev->name,
348 + jz_battery);
349 + if (ret) {
350 + dev_err(&pdev->dev, "Failed to request irq %d\n", ret);
351 + goto err_iounmap;
352 + }
353 + disable_irq(jz_battery->irq);
354 +
355 + if (gpio_is_valid(pdata->gpio_charge)) {
356 + ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
357 + if (ret) {
358 + dev_err(&pdev->dev, "charger state gpio request failed.\n");
359 + goto err_free_irq;
360 + }
361 + ret = gpio_direction_input(pdata->gpio_charge);
362 + if (ret) {
363 + dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
364 + goto err_free_gpio;
365 + }
366 +
367 + jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
368 +
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);
374 + if (ret) {
375 + dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
376 + goto err_free_gpio;
377 + }
378 + }
379 + } else {
380 + jz_battery->charge_irq = -1;
381 + }
382 +
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);
386 + else
387 + jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB, 0);
388 +
389 + ret = power_supply_register(&pdev->dev, &jz_battery->battery);
390 + if (ret) {
391 + dev_err(&pdev->dev, "power supply battery register failed.\n");
392 + goto err_free_charge_irq;
393 + }
394 +
395 + platform_set_drvdata(pdev, jz_battery);
396 + schedule_delayed_work(&jz_battery->work, 0);
397 +
398 + return 0;
399 +
400 +err_free_charge_irq:
401 + if (jz_battery->charge_irq >= 0)
402 + free_irq(jz_battery->charge_irq, jz_battery);
403 +err_free_gpio:
404 + if (gpio_is_valid(pdata->gpio_charge))
405 + gpio_free(jz_battery->pdata->gpio_charge);
406 +err_free_irq:
407 + free_irq(jz_battery->irq, jz_battery);
408 +err_iounmap:
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));
413 +err_free:
414 + kfree(jz_battery);
415 + return ret;
416 +}
417 +
418 +static int __devexit jz_battery_remove(struct platform_device *pdev)
419 +{
420 + struct jz_battery *jz_battery = platform_get_drvdata(pdev);
421 +
422 + cancel_delayed_work_sync(&jz_battery->work);
423 +
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);
428 + }
429 +
430 + power_supply_unregister(&jz_battery->battery);
431 +
432 + free_irq(jz_battery->irq, jz_battery);
433 +
434 + iounmap(jz_battery->base);
435 + release_mem_region(jz_battery->mem->start, resource_size(jz_battery->mem));
436 +
437 + return 0;
438 +}
439 +
440 +#ifdef CONFIG_PM
441 +static int jz_battery_suspend(struct device *dev)
442 +{
443 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
444 +
445 + cancel_delayed_work_sync(&jz_battery->work);
446 + jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
447 +
448 + return 0;
449 +}
450 +
451 +static int jz_battery_resume(struct device *dev)
452 +{
453 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
454 +
455 + schedule_delayed_work(&jz_battery->work, 0);
456 +
457 + return 0;
458 +}
459 +
460 +static const struct dev_pm_ops jz_battery_pm_ops = {
461 + .suspend = jz_battery_suspend,
462 + .resume = jz_battery_resume,
463 +};
464 +
465 +#define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
466 +#else
467 +#define JZ_BATTERY_PM_OPS NULL
468 +#endif
469 +
470 +static struct platform_driver jz_battery_driver = {
471 + .probe = jz_battery_probe,
472 + .remove = __devexit_p(jz_battery_remove),
473 + .driver = {
474 + .name = "jz4740-battery",
475 + .owner = THIS_MODULE,
476 + .pm = JZ_BATTERY_PM_OPS,
477 + },
478 +};
479 +
480 +static int __init jz_battery_init(void)
481 +{
482 + return platform_driver_register(&jz_battery_driver);
483 +}
484 +module_init(jz_battery_init);
485 +
486 +static void __exit jz_battery_exit(void)
487 +{
488 + platform_driver_unregister(&jz_battery_driver);
489 +}
490 +module_exit(jz_battery_exit);
491 +
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");
496 --- /dev/null
497 +++ b/include/linux/power/jz4740-battery.h
498 @@ -0,0 +1,24 @@
499 +/*
500 + * Copyright (C) 2009, Jiejing Zhang <kzjeef@gmail.com>
501 + *
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.
506 + *
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.
510 + *
511 + */
512 +
513 +#ifndef __JZ4740_BATTERY_H
514 +#define __JZ4740_BATTERY_H
515 +
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;
520 +};
521 +
522 +#endif
This page took 0.071794 seconds and 5 git commands to generate.