blob: 02414db6a94c151208bab6ff51917765cc074db1 [file] [log] [blame]
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +00001/*
2 * Battery measurement code for Ingenic JZ SOC.
3 *
4 * Copyright (C) 2009 Jiejing Zhang <kzjeef@gmail.com>
5 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
6 *
7 * based on tosa_battery.c
8 *
9 * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
10*
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
Anton Vorontsovf3dcab72010-12-22 02:44:49 +030022#include <linux/io.h>
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +000023
24#include <linux/delay.h>
25#include <linux/gpio.h>
26#include <linux/mfd/core.h>
27#include <linux/power_supply.h>
28
29#include <linux/power/jz4740-battery.h>
30#include <linux/jz4740-adc.h>
31
32struct jz_battery {
33 struct jz_battery_platform_data *pdata;
34 struct platform_device *pdev;
35
36 struct resource *mem;
37 void __iomem *base;
38
39 int irq;
40 int charge_irq;
41
42 struct mfd_cell *cell;
43
44 int status;
45 long voltage;
46
47 struct completion read_completion;
48
49 struct power_supply battery;
50 struct delayed_work work;
Lars-Peter Clausen8ec98fe2010-11-11 19:00:52 +010051
52 struct mutex lock;
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +000053};
54
55static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
56{
57 return container_of(psy, struct jz_battery, battery);
58}
59
60static irqreturn_t jz_battery_irq_handler(int irq, void *devid)
61{
62 struct jz_battery *battery = devid;
63
64 complete(&battery->read_completion);
65 return IRQ_HANDLED;
66}
67
68static long jz_battery_read_voltage(struct jz_battery *battery)
69{
70 unsigned long t;
71 unsigned long val;
72 long voltage;
73
Lars-Peter Clausen8ec98fe2010-11-11 19:00:52 +010074 mutex_lock(&battery->lock);
75
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +000076 INIT_COMPLETION(battery->read_completion);
77
78 enable_irq(battery->irq);
79 battery->cell->enable(battery->pdev);
80
81 t = wait_for_completion_interruptible_timeout(&battery->read_completion,
82 HZ);
83
84 if (t > 0) {
85 val = readw(battery->base) & 0xfff;
86
87 if (battery->pdata->info.voltage_max_design <= 2500000)
88 val = (val * 78125UL) >> 7UL;
89 else
90 val = ((val * 924375UL) >> 9UL) + 33000;
91 voltage = (long)val;
92 } else {
93 voltage = t ? t : -ETIMEDOUT;
94 }
95
96 battery->cell->disable(battery->pdev);
97 disable_irq(battery->irq);
98
Lars-Peter Clausen8ec98fe2010-11-11 19:00:52 +010099 mutex_unlock(&battery->lock);
100
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000101 return voltage;
102}
103
104static int jz_battery_get_capacity(struct power_supply *psy)
105{
106 struct jz_battery *jz_battery = psy_to_jz_battery(psy);
107 struct power_supply_info *info = &jz_battery->pdata->info;
108 long voltage;
109 int ret;
110 int voltage_span;
111
112 voltage = jz_battery_read_voltage(jz_battery);
113
114 if (voltage < 0)
115 return voltage;
116
117 voltage_span = info->voltage_max_design - info->voltage_min_design;
118 ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
119
120 if (ret > 100)
121 ret = 100;
122 else if (ret < 0)
123 ret = 0;
124
125 return ret;
126}
127
128static int jz_battery_get_property(struct power_supply *psy,
129 enum power_supply_property psp, union power_supply_propval *val)
130{
131 struct jz_battery *jz_battery = psy_to_jz_battery(psy);
132 struct power_supply_info *info = &jz_battery->pdata->info;
133 long voltage;
134
135 switch (psp) {
136 case POWER_SUPPLY_PROP_STATUS:
137 val->intval = jz_battery->status;
138 break;
139 case POWER_SUPPLY_PROP_TECHNOLOGY:
140 val->intval = jz_battery->pdata->info.technology;
141 break;
142 case POWER_SUPPLY_PROP_HEALTH:
143 voltage = jz_battery_read_voltage(jz_battery);
144 if (voltage < info->voltage_min_design)
145 val->intval = POWER_SUPPLY_HEALTH_DEAD;
146 else
147 val->intval = POWER_SUPPLY_HEALTH_GOOD;
148 break;
149 case POWER_SUPPLY_PROP_CAPACITY:
150 val->intval = jz_battery_get_capacity(psy);
151 break;
152 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
153 val->intval = jz_battery_read_voltage(jz_battery);
154 if (val->intval < 0)
155 return val->intval;
156 break;
157 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
158 val->intval = info->voltage_max_design;
159 break;
160 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
161 val->intval = info->voltage_min_design;
162 break;
163 case POWER_SUPPLY_PROP_PRESENT:
164 val->intval = 1;
165 break;
166 default:
167 return -EINVAL;
168 }
169 return 0;
170}
171
172static void jz_battery_external_power_changed(struct power_supply *psy)
173{
174 struct jz_battery *jz_battery = psy_to_jz_battery(psy);
175
176 cancel_delayed_work(&jz_battery->work);
177 schedule_delayed_work(&jz_battery->work, 0);
178}
179
180static irqreturn_t jz_battery_charge_irq(int irq, void *data)
181{
182 struct jz_battery *jz_battery = data;
183
184 cancel_delayed_work(&jz_battery->work);
185 schedule_delayed_work(&jz_battery->work, 0);
186
187 return IRQ_HANDLED;
188}
189
190static void jz_battery_update(struct jz_battery *jz_battery)
191{
192 int status;
193 long voltage;
194 bool has_changed = false;
195 int is_charging;
196
197 if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
198 is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
199 is_charging ^= jz_battery->pdata->gpio_charge_active_low;
200 if (is_charging)
201 status = POWER_SUPPLY_STATUS_CHARGING;
202 else
203 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
204
205 if (status != jz_battery->status) {
206 jz_battery->status = status;
207 has_changed = true;
208 }
209 }
210
211 voltage = jz_battery_read_voltage(jz_battery);
212 if (abs(voltage - jz_battery->voltage) < 50000) {
213 jz_battery->voltage = voltage;
214 has_changed = true;
215 }
216
217 if (has_changed)
218 power_supply_changed(&jz_battery->battery);
219}
220
221static enum power_supply_property jz_battery_properties[] = {
222 POWER_SUPPLY_PROP_STATUS,
223 POWER_SUPPLY_PROP_TECHNOLOGY,
224 POWER_SUPPLY_PROP_HEALTH,
225 POWER_SUPPLY_PROP_CAPACITY,
226 POWER_SUPPLY_PROP_VOLTAGE_NOW,
227 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
228 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
229 POWER_SUPPLY_PROP_PRESENT,
230};
231
232static void jz_battery_work(struct work_struct *work)
233{
234 /* Too small interval will increase system workload */
235 const int interval = HZ * 30;
236 struct jz_battery *jz_battery = container_of(work, struct jz_battery,
237 work.work);
238
239 jz_battery_update(jz_battery);
240 schedule_delayed_work(&jz_battery->work, interval);
241}
242
243static int __devinit jz_battery_probe(struct platform_device *pdev)
244{
245 int ret = 0;
246 struct jz_battery_platform_data *pdata = pdev->dev.parent->platform_data;
247 struct jz_battery *jz_battery;
248 struct power_supply *battery;
249
Lars-Peter Clausen8477ced2010-11-11 19:00:53 +0100250 if (!pdata) {
251 dev_err(&pdev->dev, "No platform_data supplied\n");
252 return -ENXIO;
253 }
254
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000255 jz_battery = kzalloc(sizeof(*jz_battery), GFP_KERNEL);
256 if (!jz_battery) {
257 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
258 return -ENOMEM;
259 }
260
261 jz_battery->cell = pdev->dev.platform_data;
262
263 jz_battery->irq = platform_get_irq(pdev, 0);
264 if (jz_battery->irq < 0) {
265 ret = jz_battery->irq;
266 dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
267 goto err_free;
268 }
269
270 jz_battery->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
271 if (!jz_battery->mem) {
272 ret = -ENOENT;
273 dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
274 goto err_free;
275 }
276
277 jz_battery->mem = request_mem_region(jz_battery->mem->start,
278 resource_size(jz_battery->mem), pdev->name);
279 if (!jz_battery->mem) {
280 ret = -EBUSY;
281 dev_err(&pdev->dev, "Failed to request mmio memory region\n");
282 goto err_free;
283 }
284
285 jz_battery->base = ioremap_nocache(jz_battery->mem->start,
286 resource_size(jz_battery->mem));
287 if (!jz_battery->base) {
288 ret = -EBUSY;
289 dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
290 goto err_release_mem_region;
291 }
292
293 battery = &jz_battery->battery;
294 battery->name = pdata->info.name;
295 battery->type = POWER_SUPPLY_TYPE_BATTERY;
296 battery->properties = jz_battery_properties;
297 battery->num_properties = ARRAY_SIZE(jz_battery_properties);
298 battery->get_property = jz_battery_get_property;
299 battery->external_power_changed = jz_battery_external_power_changed;
300 battery->use_for_apm = 1;
301
302 jz_battery->pdata = pdata;
303 jz_battery->pdev = pdev;
304
305 init_completion(&jz_battery->read_completion);
Lars-Peter Clausen8ec98fe2010-11-11 19:00:52 +0100306 mutex_init(&jz_battery->lock);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000307
308 INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
309
310 ret = request_irq(jz_battery->irq, jz_battery_irq_handler, 0, pdev->name,
311 jz_battery);
312 if (ret) {
313 dev_err(&pdev->dev, "Failed to request irq %d\n", ret);
314 goto err_iounmap;
315 }
316 disable_irq(jz_battery->irq);
317
318 if (gpio_is_valid(pdata->gpio_charge)) {
319 ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
320 if (ret) {
321 dev_err(&pdev->dev, "charger state gpio request failed.\n");
322 goto err_free_irq;
323 }
324 ret = gpio_direction_input(pdata->gpio_charge);
325 if (ret) {
326 dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
327 goto err_free_gpio;
328 }
329
330 jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
331
332 if (jz_battery->charge_irq >= 0) {
333 ret = request_irq(jz_battery->charge_irq,
334 jz_battery_charge_irq,
335 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
336 dev_name(&pdev->dev), jz_battery);
337 if (ret) {
338 dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
339 goto err_free_gpio;
340 }
341 }
342 } else {
343 jz_battery->charge_irq = -1;
344 }
345
346 if (jz_battery->pdata->info.voltage_max_design <= 2500000)
347 jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB,
348 JZ_ADC_CONFIG_BAT_MB);
349 else
350 jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB, 0);
351
352 ret = power_supply_register(&pdev->dev, &jz_battery->battery);
353 if (ret) {
354 dev_err(&pdev->dev, "power supply battery register failed.\n");
355 goto err_free_charge_irq;
356 }
357
358 platform_set_drvdata(pdev, jz_battery);
359 schedule_delayed_work(&jz_battery->work, 0);
360
361 return 0;
362
363err_free_charge_irq:
364 if (jz_battery->charge_irq >= 0)
365 free_irq(jz_battery->charge_irq, jz_battery);
366err_free_gpio:
367 if (gpio_is_valid(pdata->gpio_charge))
368 gpio_free(jz_battery->pdata->gpio_charge);
369err_free_irq:
370 free_irq(jz_battery->irq, jz_battery);
371err_iounmap:
372 platform_set_drvdata(pdev, NULL);
373 iounmap(jz_battery->base);
374err_release_mem_region:
375 release_mem_region(jz_battery->mem->start, resource_size(jz_battery->mem));
376err_free:
377 kfree(jz_battery);
378 return ret;
379}
380
381static int __devexit jz_battery_remove(struct platform_device *pdev)
382{
383 struct jz_battery *jz_battery = platform_get_drvdata(pdev);
384
385 cancel_delayed_work_sync(&jz_battery->work);
386
387 if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
388 if (jz_battery->charge_irq >= 0)
389 free_irq(jz_battery->charge_irq, jz_battery);
390 gpio_free(jz_battery->pdata->gpio_charge);
391 }
392
393 power_supply_unregister(&jz_battery->battery);
394
395 free_irq(jz_battery->irq, jz_battery);
396
397 iounmap(jz_battery->base);
398 release_mem_region(jz_battery->mem->start, resource_size(jz_battery->mem));
Axel Lin5f298952010-09-27 15:08:31 +0800399 kfree(jz_battery);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000400
401 return 0;
402}
403
404#ifdef CONFIG_PM
405static int jz_battery_suspend(struct device *dev)
406{
407 struct jz_battery *jz_battery = dev_get_drvdata(dev);
408
409 cancel_delayed_work_sync(&jz_battery->work);
410 jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
411
412 return 0;
413}
414
415static int jz_battery_resume(struct device *dev)
416{
417 struct jz_battery *jz_battery = dev_get_drvdata(dev);
418
419 schedule_delayed_work(&jz_battery->work, 0);
420
421 return 0;
422}
423
424static const struct dev_pm_ops jz_battery_pm_ops = {
425 .suspend = jz_battery_suspend,
426 .resume = jz_battery_resume,
427};
428
429#define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
430#else
431#define JZ_BATTERY_PM_OPS NULL
432#endif
433
434static struct platform_driver jz_battery_driver = {
435 .probe = jz_battery_probe,
436 .remove = __devexit_p(jz_battery_remove),
437 .driver = {
438 .name = "jz4740-battery",
439 .owner = THIS_MODULE,
440 .pm = JZ_BATTERY_PM_OPS,
441 },
442};
443
444static int __init jz_battery_init(void)
445{
446 return platform_driver_register(&jz_battery_driver);
447}
448module_init(jz_battery_init);
449
450static void __exit jz_battery_exit(void)
451{
452 platform_driver_unregister(&jz_battery_driver);
453}
454module_exit(jz_battery_exit);
455
456MODULE_ALIAS("platform:jz4740-battery");
457MODULE_LICENSE("GPL");
458MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
459MODULE_DESCRIPTION("JZ4740 SoC battery driver");