[kernel] kmod-usb-core is buildin on etrax target
[openwrt.git] / target / linux / xburst / patches-2.6.34 / 100-battery.patch
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
5
6 ---
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
14
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
20 help
21 Say Y to include support for NXP PCF50633 Main Battery Charger.
22
23 +config BATTERY_JZ4740
24 + tristate "Ingenic JZ4720/JZ4740 battery"
25 + depends on SOC_JZ4740
26 + depends on JZ4740_ADC
27 + help
28 + Say Y to enable support for the battery on Ingenic JZ4720/JZ4740 based
29 + boards.
30 +
31 + This driver can be build as a module. If so, the module will be
32 + called jz4740-battery.
33 +
34 endif # POWER_SUPPLY
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
45 new file mode 100644
46 index 0000000..c89b9f7
47 --- /dev/null
48 +++ b/drivers/power/jz4740-battery.c
49 @@ -0,0 +1,359 @@
50 +/*
51 + * Battery measurement code for Ingenic JZ SOC.
52 + *
53 + * Copyright (C) 2009 Jiejing Zhang <kzjeef@gmail.com>
54 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
55 + *
56 + * based on tosa_battery.c
57 + *
58 + * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
59 +*
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.
63 + *
64 + */
65 +
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>
72 +
73 +#include <linux/delay.h>
74 +#include <linux/gpio.h>
75 +#include <linux/power_supply.h>
76 +
77 +#include <linux/power/jz4740-battery.h>
78 +#include <linux/jz4740-adc.h>
79 +
80 +struct jz_battery {
81 + struct jz_battery_platform_data *pdata;
82 +
83 + int charge_irq;
84 +
85 + int status;
86 + long voltage;
87 +
88 + struct power_supply battery;
89 + struct delayed_work work;
90 +};
91 +
92 +static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
93 +{
94 + return container_of(psy, struct jz_battery, battery);
95 +}
96 +
97 +static long jz_battery_read_voltage(struct jz_battery *jz_battery)
98 +{
99 + struct device *adc = jz_battery->battery.dev->parent->parent;
100 + enum jz_adc_battery_scale scale;
101 +
102 + if (jz_battery->pdata->info.voltage_max_design > 2500000)
103 + scale = JZ_ADC_BATTERY_SCALE_7V5;
104 + else
105 + scale = JZ_ADC_BATTERY_SCALE_2V5;
106 +
107 + return jz4740_adc_read_battery_voltage(adc, scale);
108 +}
109 +
110 +static int jz_battery_get_capacity(struct power_supply *psy)
111 +{
112 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
113 + struct power_supply_info *info = &jz_battery->pdata->info;
114 + long voltage;
115 + int ret;
116 + int voltage_span;
117 +
118 + voltage = jz_battery_read_voltage(jz_battery);
119 +
120 + if (voltage < 0)
121 + return voltage;
122 +
123 + voltage_span = info->voltage_max_design - info->voltage_min_design;
124 + ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
125 +
126 + if (ret > 100)
127 + ret = 100;
128 + else if (ret < 0)
129 + ret = 0;
130 +
131 + return ret;
132 +}
133 +
134 +static int jz_battery_get_property(struct power_supply *psy,
135 + enum power_supply_property psp,
136 + union power_supply_propval *val)
137 +{
138 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
139 + struct power_supply_info *info = &jz_battery->pdata->info;
140 + long voltage;
141 +
142 + switch (psp) {
143 + case POWER_SUPPLY_PROP_STATUS:
144 + val->intval = jz_battery->status;
145 + break;
146 + case POWER_SUPPLY_PROP_TECHNOLOGY:
147 + val->intval = jz_battery->pdata->info.technology;
148 + break;
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;
153 + else
154 + val->intval = POWER_SUPPLY_HEALTH_GOOD;
155 + break;
156 + case POWER_SUPPLY_PROP_CAPACITY:
157 + val->intval = jz_battery_get_capacity(psy);
158 + break;
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;
163 + break;
164 + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
165 + val->intval = info->voltage_max_design;
166 + break;
167 + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
168 + val->intval = info->voltage_min_design;
169 + break;
170 + case POWER_SUPPLY_PROP_PRESENT:
171 + val->intval = 1;
172 + break;
173 + default:
174 + return -EINVAL;
175 + }
176 + return 0;
177 +}
178 +
179 +static void jz_battery_external_power_changed(struct power_supply *psy)
180 +{
181 + struct jz_battery *jz_battery = psy_to_jz_battery(psy);
182 +
183 + cancel_delayed_work(&jz_battery->work);
184 + schedule_delayed_work(&jz_battery->work, 0);
185 +}
186 +
187 +static irqreturn_t jz_battery_charge_irq(int irq, void *data)
188 +{
189 + struct jz_battery *jz_battery = data;
190 +
191 + cancel_delayed_work(&jz_battery->work);
192 + schedule_delayed_work(&jz_battery->work, 0);
193 +
194 + return IRQ_HANDLED;
195 +}
196 +
197 +static void jz_battery_update(struct jz_battery *jz_battery)
198 +{
199 + int status;
200 + long voltage;
201 + long voltage_difference;
202 + bool has_changed = 0;
203 +
204 + if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
205 + int is_charging;
206 +
207 + is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
208 + is_charging ^= jz_battery->pdata->gpio_charge_active_low;
209 + if (is_charging)
210 + status = POWER_SUPPLY_STATUS_CHARGING;
211 + else
212 + status = POWER_SUPPLY_STATUS_NOT_CHARGING;
213 +
214 + if (status != jz_battery->status) {
215 + jz_battery->status = status;
216 + has_changed = 1;
217 + }
218 + }
219 +
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;
224 + has_changed = 1;
225 + }
226 + if (has_changed)
227 + power_supply_changed(&jz_battery->battery);
228 +}
229 +
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,
239 +};
240 +
241 +static void jz_battery_work(struct work_struct *work)
242 +{
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,
246 + work.work);
247 +
248 + jz_battery_update(jz_battery);
249 + schedule_delayed_work(&jz_battery->work, interval);
250 +}
251 +
252 +static int __devinit jz_battery_probe(struct platform_device *pdev)
253 +{
254 + int ret = 0;
255 + struct jz_battery_platform_data *pdata = pdev->dev.platform_data;
256 + struct jz_battery *jz_battery;
257 + struct power_supply *battery;
258 +
259 + if (!pdev->dev.platform_data) {
260 + dev_err(&pdev->dev, "No platform data\n");
261 + return -EINVAL;
262 + }
263 +
264 + jz_battery = kzalloc(sizeof(*jz_battery), GFP_KERNEL);
265 +
266 + if (!jz_battery) {
267 + dev_err(&pdev->dev, "Failed to allocate driver structure\n");
268 + return -ENOMEM;
269 + }
270 +
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;
279 +
280 + jz_battery->pdata = pdata;
281 +
282 + INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
283 +
284 + if (gpio_is_valid(pdata->gpio_charge)) {
285 + ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
286 + if (ret) {
287 + dev_err(&pdev->dev, "charger state gpio request failed.\n");
288 + goto err_free;
289 + }
290 + ret = gpio_direction_input(pdata->gpio_charge);
291 + if (ret) {
292 + dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
293 + goto err_free_gpio;
294 + }
295 +
296 + jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
297 +
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);
303 + if (ret) {
304 + dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
305 + goto err_free_gpio;
306 + }
307 + }
308 + } else {
309 + jz_battery->charge_irq = -1;
310 + }
311 +
312 +
313 + ret = power_supply_register(&pdev->dev, &jz_battery->battery);
314 + if (ret) {
315 + dev_err(&pdev->dev, "power supply battery register failed.\n");
316 + goto err_free_irq;
317 + }
318 +
319 + platform_set_drvdata(pdev, jz_battery);
320 + schedule_delayed_work(&jz_battery->work, 0);
321 +
322 + return 0;
323 +
324 +err_free_irq:
325 + if (jz_battery->charge_irq >= 0)
326 + free_irq(jz_battery->charge_irq, jz_battery);
327 +err_free_gpio:
328 + if (gpio_is_valid(pdata->gpio_charge))
329 + gpio_free(jz_battery->pdata->gpio_charge);
330 +err_free:
331 + kfree(jz_battery);
332 + return ret;
333 +}
334 +
335 +static int __devexit jz_battery_remove(struct platform_device *pdev)
336 +{
337 + struct jz_battery *jz_battery = platform_get_drvdata(pdev);
338 +
339 + cancel_delayed_work_sync(&jz_battery->work);
340 +
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);
345 + }
346 +
347 + power_supply_unregister(&jz_battery->battery);
348 +
349 + return 0;
350 +}
351 +
352 +#ifdef CONFIG_PM
353 +static int jz_battery_suspend(struct device *dev)
354 +{
355 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
356 +
357 + cancel_delayed_work_sync(&jz_battery->work);
358 + jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
359 +
360 + return 0;
361 +}
362 +
363 +static int jz_battery_resume(struct device *dev)
364 +{
365 + struct jz_battery *jz_battery = dev_get_drvdata(dev);
366 +
367 + schedule_delayed_work(&jz_battery->work, 0);
368 +
369 + return 0;
370 +}
371 +
372 +static const struct dev_pm_ops jz_battery_pm_ops = {
373 + .suspend = jz_battery_suspend,
374 + .resume = jz_battery_resume,
375 +};
376 +
377 +#define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
378 +
379 +#else
380 +#define JZ_BATTERY_PM_OPS NULL
381 +#endif
382 +
383 +static struct platform_driver jz_battery_driver = {
384 + .probe = jz_battery_probe,
385 + .remove = __devexit_p(jz_battery_remove),
386 + .driver = {
387 + .name = "jz4740-battery",
388 + .owner = THIS_MODULE,
389 + .pm = JZ_BATTERY_PM_OPS,
390 + },
391 +};
392 +
393 +static int __init jz_battery_init(void)
394 +{
395 + return platform_driver_register(&jz_battery_driver);
396 +}
397 +module_init(jz_battery_init);
398 +
399 +static void __exit jz_battery_exit(void)
400 +{
401 + platform_driver_unregister(&jz_battery_driver);
402 +}
403 +module_exit(jz_battery_exit);
404 +
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
410 new file mode 100644
411 index 0000000..19c9610
412 --- /dev/null
413 +++ b/include/linux/power/jz4740-battery.h
414 @@ -0,0 +1,24 @@
415 +/*
416 + * Copyright (C) 2009, Jiejing Zhang <kzjeef@gmail.com>
417 + *
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.
422 + *
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.
426 + *
427 + */
428 +
429 +#ifndef __JZ4740_BATTERY_H
430 +#define __JZ4740_BATTERY_H
431 +
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;
436 +};
437 +
438 +#endif
439 --
440 1.5.6.5
441
This page took 0.081483 seconds and 5 git commands to generate.