2 * Battery measurement code for Ingenic JZ SOC.
4 * Copyright (C) 2009 Jiejing Zhang <kzjeef@gmail.com>
5 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
7 * based on tosa_battery.c
9 * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
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.
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>
23 #include <linux/delay.h>
24 #include <linux/gpio.h>
25 #include <linux/mfd/core.h>
26 #include <linux/power_supply.h>
28 #include <linux/power/jz4740-battery.h>
29 #include <linux/jz4740-adc.h>
32 struct jz_battery_platform_data
*pdata
;
33 struct platform_device
*pdev
;
41 struct mfd_cell
*cell
;
46 struct completion read_completion
;
48 struct power_supply battery
;
49 struct delayed_work work
;
52 static inline struct jz_battery
*psy_to_jz_battery(struct power_supply
*psy
)
54 return container_of(psy
, struct jz_battery
, battery
);
57 static irqreturn_t
jz_battery_irq_handler(int irq
, void *devid
)
59 struct jz_battery
*battery
= devid
;
61 complete(&battery
->read_completion
);
65 static long jz_battery_read_voltage(struct jz_battery
*battery
)
71 INIT_COMPLETION(battery
->read_completion
);
73 enable_irq(battery
->irq
);
74 battery
->cell
->enable(battery
->pdev
);
76 t
= wait_for_completion_interruptible_timeout(&battery
->read_completion
,
80 val
= readw(battery
->base
) & 0xfff;
82 if (battery
->pdata
->info
.voltage_max_design
<= 2500000)
83 val
= (val
* 78125UL) >> 7UL;
85 val
= ((val
* 924375UL) >> 9UL) + 33000;
88 voltage
= t
? t
: -ETIMEDOUT
;
91 battery
->cell
->disable(battery
->pdev
);
92 disable_irq(battery
->irq
);
97 static int jz_battery_get_capacity(struct power_supply
*psy
)
99 struct jz_battery
*jz_battery
= psy_to_jz_battery(psy
);
100 struct power_supply_info
*info
= &jz_battery
->pdata
->info
;
105 voltage
= jz_battery_read_voltage(jz_battery
);
110 voltage_span
= info
->voltage_max_design
- info
->voltage_min_design
;
111 ret
= ((voltage
- info
->voltage_min_design
) * 100) / voltage_span
;
121 static int jz_battery_get_property(struct power_supply
*psy
,
122 enum power_supply_property psp
, union power_supply_propval
*val
)
124 struct jz_battery
*jz_battery
= psy_to_jz_battery(psy
);
125 struct power_supply_info
*info
= &jz_battery
->pdata
->info
;
129 case POWER_SUPPLY_PROP_STATUS
:
130 val
->intval
= jz_battery
->status
;
132 case POWER_SUPPLY_PROP_TECHNOLOGY
:
133 val
->intval
= jz_battery
->pdata
->info
.technology
;
135 case POWER_SUPPLY_PROP_HEALTH
:
136 voltage
= jz_battery_read_voltage(jz_battery
);
137 if (voltage
< info
->voltage_min_design
)
138 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
140 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
142 case POWER_SUPPLY_PROP_CAPACITY
:
143 val
->intval
= jz_battery_get_capacity(psy
);
145 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
146 val
->intval
= jz_battery_read_voltage(jz_battery
);
150 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
151 val
->intval
= info
->voltage_max_design
;
153 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
154 val
->intval
= info
->voltage_min_design
;
156 case POWER_SUPPLY_PROP_PRESENT
:
165 static void jz_battery_external_power_changed(struct power_supply
*psy
)
167 struct jz_battery
*jz_battery
= psy_to_jz_battery(psy
);
169 cancel_delayed_work(&jz_battery
->work
);
170 schedule_delayed_work(&jz_battery
->work
, 0);
173 static irqreturn_t
jz_battery_charge_irq(int irq
, void *data
)
175 struct jz_battery
*jz_battery
= data
;
177 cancel_delayed_work(&jz_battery
->work
);
178 schedule_delayed_work(&jz_battery
->work
, 0);
183 static void jz_battery_update(struct jz_battery
*jz_battery
)
187 bool has_changed
= false;
190 if (gpio_is_valid(jz_battery
->pdata
->gpio_charge
)) {
191 is_charging
= gpio_get_value(jz_battery
->pdata
->gpio_charge
);
192 is_charging
^= jz_battery
->pdata
->gpio_charge_active_low
;
194 status
= POWER_SUPPLY_STATUS_CHARGING
;
196 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
198 if (status
!= jz_battery
->status
) {
199 jz_battery
->status
= status
;
204 voltage
= jz_battery_read_voltage(jz_battery
);
205 if (abs(voltage
- jz_battery
->voltage
) < 50000) {
206 jz_battery
->voltage
= voltage
;
211 power_supply_changed(&jz_battery
->battery
);
214 static enum power_supply_property jz_battery_properties
[] = {
215 POWER_SUPPLY_PROP_STATUS
,
216 POWER_SUPPLY_PROP_TECHNOLOGY
,
217 POWER_SUPPLY_PROP_HEALTH
,
218 POWER_SUPPLY_PROP_CAPACITY
,
219 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
220 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
221 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
222 POWER_SUPPLY_PROP_PRESENT
,
225 static void jz_battery_work(struct work_struct
*work
)
227 /* Too small interval will increase system workload */
228 const int interval
= HZ
* 30;
229 struct jz_battery
*jz_battery
= container_of(work
, struct jz_battery
,
232 jz_battery_update(jz_battery
);
233 schedule_delayed_work(&jz_battery
->work
, interval
);
236 static int __devinit
jz_battery_probe(struct platform_device
*pdev
)
239 struct jz_battery_platform_data
*pdata
= pdev
->dev
.parent
->platform_data
;
240 struct jz_battery
*jz_battery
;
241 struct power_supply
*battery
;
243 jz_battery
= kzalloc(sizeof(*jz_battery
), GFP_KERNEL
);
245 dev_err(&pdev
->dev
, "Failed to allocate driver structure\n");
249 jz_battery
->cell
= pdev
->dev
.platform_data
;
251 jz_battery
->irq
= platform_get_irq(pdev
, 0);
252 if (jz_battery
->irq
< 0) {
253 ret
= jz_battery
->irq
;
254 dev_err(&pdev
->dev
, "Failed to get platform irq: %d\n", ret
);
258 jz_battery
->mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
259 if (!jz_battery
->mem
) {
261 dev_err(&pdev
->dev
, "Failed to get platform mmio resource\n");
265 jz_battery
->mem
= request_mem_region(jz_battery
->mem
->start
,
266 resource_size(jz_battery
->mem
), pdev
->name
);
267 if (!jz_battery
->mem
) {
269 dev_err(&pdev
->dev
, "Failed to request mmio memory region\n");
273 jz_battery
->base
= ioremap_nocache(jz_battery
->mem
->start
,
274 resource_size(jz_battery
->mem
));
275 if (!jz_battery
->base
) {
277 dev_err(&pdev
->dev
, "Failed to ioremap mmio memory\n");
278 goto err_release_mem_region
;
281 battery
= &jz_battery
->battery
;
282 battery
->name
= pdata
->info
.name
;
283 battery
->type
= POWER_SUPPLY_TYPE_BATTERY
;
284 battery
->properties
= jz_battery_properties
;
285 battery
->num_properties
= ARRAY_SIZE(jz_battery_properties
);
286 battery
->get_property
= jz_battery_get_property
;
287 battery
->external_power_changed
= jz_battery_external_power_changed
;
288 battery
->use_for_apm
= 1;
290 jz_battery
->pdata
= pdata
;
291 jz_battery
->pdev
= pdev
;
293 init_completion(&jz_battery
->read_completion
);
295 INIT_DELAYED_WORK(&jz_battery
->work
, jz_battery_work
);
297 ret
= request_irq(jz_battery
->irq
, jz_battery_irq_handler
, 0, pdev
->name
,
300 dev_err(&pdev
->dev
, "Failed to request irq %d\n", ret
);
303 disable_irq(jz_battery
->irq
);
305 if (gpio_is_valid(pdata
->gpio_charge
)) {
306 ret
= gpio_request(pdata
->gpio_charge
, dev_name(&pdev
->dev
));
308 dev_err(&pdev
->dev
, "charger state gpio request failed.\n");
311 ret
= gpio_direction_input(pdata
->gpio_charge
);
313 dev_err(&pdev
->dev
, "charger state gpio set direction failed.\n");
317 jz_battery
->charge_irq
= gpio_to_irq(pdata
->gpio_charge
);
319 if (jz_battery
->charge_irq
>= 0) {
320 ret
= request_irq(jz_battery
->charge_irq
,
321 jz_battery_charge_irq
,
322 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
323 dev_name(&pdev
->dev
), jz_battery
);
325 dev_err(&pdev
->dev
, "Failed to request charge irq: %d\n", ret
);
330 jz_battery
->charge_irq
= -1;
333 if (jz_battery
->pdata
->info
.voltage_max_design
<= 2500000)
334 jz4740_adc_set_config(pdev
->dev
.parent
, JZ_ADC_CONFIG_BAT_MB
,
335 JZ_ADC_CONFIG_BAT_MB
);
337 jz4740_adc_set_config(pdev
->dev
.parent
, JZ_ADC_CONFIG_BAT_MB
, 0);
339 ret
= power_supply_register(&pdev
->dev
, &jz_battery
->battery
);
341 dev_err(&pdev
->dev
, "power supply battery register failed.\n");
342 goto err_free_charge_irq
;
345 platform_set_drvdata(pdev
, jz_battery
);
346 schedule_delayed_work(&jz_battery
->work
, 0);
351 if (jz_battery
->charge_irq
>= 0)
352 free_irq(jz_battery
->charge_irq
, jz_battery
);
354 if (gpio_is_valid(pdata
->gpio_charge
))
355 gpio_free(jz_battery
->pdata
->gpio_charge
);
357 free_irq(jz_battery
->irq
, jz_battery
);
359 platform_set_drvdata(pdev
, NULL
);
360 iounmap(jz_battery
->base
);
361 err_release_mem_region
:
362 release_mem_region(jz_battery
->mem
->start
, resource_size(jz_battery
->mem
));
368 static int __devexit
jz_battery_remove(struct platform_device
*pdev
)
370 struct jz_battery
*jz_battery
= platform_get_drvdata(pdev
);
372 cancel_delayed_work_sync(&jz_battery
->work
);
374 if (gpio_is_valid(jz_battery
->pdata
->gpio_charge
)) {
375 if (jz_battery
->charge_irq
>= 0)
376 free_irq(jz_battery
->charge_irq
, jz_battery
);
377 gpio_free(jz_battery
->pdata
->gpio_charge
);
380 power_supply_unregister(&jz_battery
->battery
);
382 free_irq(jz_battery
->irq
, jz_battery
);
384 iounmap(jz_battery
->base
);
385 release_mem_region(jz_battery
->mem
->start
, resource_size(jz_battery
->mem
));
391 static int jz_battery_suspend(struct device
*dev
)
393 struct jz_battery
*jz_battery
= dev_get_drvdata(dev
);
395 cancel_delayed_work_sync(&jz_battery
->work
);
396 jz_battery
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
401 static int jz_battery_resume(struct device
*dev
)
403 struct jz_battery
*jz_battery
= dev_get_drvdata(dev
);
405 schedule_delayed_work(&jz_battery
->work
, 0);
410 static const struct dev_pm_ops jz_battery_pm_ops
= {
411 .suspend
= jz_battery_suspend
,
412 .resume
= jz_battery_resume
,
415 #define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
417 #define JZ_BATTERY_PM_OPS NULL
420 static struct platform_driver jz_battery_driver
= {
421 .probe
= jz_battery_probe
,
422 .remove
= __devexit_p(jz_battery_remove
),
424 .name
= "jz4740-battery",
425 .owner
= THIS_MODULE
,
426 .pm
= JZ_BATTERY_PM_OPS
,
430 static int __init
jz_battery_init(void)
432 return platform_driver_register(&jz_battery_driver
);
434 module_init(jz_battery_init
);
436 static void __exit
jz_battery_exit(void)
438 platform_driver_unregister(&jz_battery_driver
);
440 module_exit(jz_battery_exit
);
442 MODULE_ALIAS("platform:jz4740-battery");
443 MODULE_LICENSE("GPL");
444 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
445 MODULE_DESCRIPTION("JZ4740 SoC battery driver");