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>
24 #include <linux/delay.h>
25 #include <linux/gpio.h>
26 #include <linux/mfd/core.h>
27 #include <linux/power_supply.h>
29 #include <linux/power/jz4740-battery.h>
30 #include <linux/jz4740-adc.h>
33 struct jz_battery_platform_data *pdata;
34 struct platform_device *pdev;
41 const struct mfd_cell *cell;
46 struct completion read_completion;
48 struct power_supply battery;
49 struct delayed_work work;
54 static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
56 return container_of(psy, struct jz_battery, battery);
59 static irqreturn_t jz_battery_irq_handler(int irq, void *devid)
61 struct jz_battery *battery = devid;
63 complete(&battery->read_completion);
67 static long jz_battery_read_voltage(struct jz_battery *battery)
73 mutex_lock(&battery->lock);
75 INIT_COMPLETION(battery->read_completion);
77 enable_irq(battery->irq);
78 battery->cell->enable(battery->pdev);
80 t = wait_for_completion_interruptible_timeout(&battery->read_completion,
84 val = readw(battery->base) & 0xfff;
86 if (battery->pdata->info.voltage_max_design <= 2500000)
87 val = (val * 78125UL) >> 7UL;
89 val = ((val * 924375UL) >> 9UL) + 33000;
92 voltage = t ? t : -ETIMEDOUT;
95 battery->cell->disable(battery->pdev);
96 disable_irq(battery->irq);
98 mutex_unlock(&battery->lock);
103 static int jz_battery_get_capacity(struct power_supply *psy)
105 struct jz_battery *jz_battery = psy_to_jz_battery(psy);
106 struct power_supply_info *info = &jz_battery->pdata->info;
111 voltage = jz_battery_read_voltage(jz_battery);
116 voltage_span = info->voltage_max_design - info->voltage_min_design;
117 ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
127 static int jz_battery_get_property(struct power_supply *psy,
128 enum power_supply_property psp, union power_supply_propval *val)
130 struct jz_battery *jz_battery = psy_to_jz_battery(psy);
131 struct power_supply_info *info = &jz_battery->pdata->info;
135 case POWER_SUPPLY_PROP_STATUS:
136 val->intval = jz_battery->status;
138 case POWER_SUPPLY_PROP_TECHNOLOGY:
139 val->intval = jz_battery->pdata->info.technology;
141 case POWER_SUPPLY_PROP_HEALTH:
142 voltage = jz_battery_read_voltage(jz_battery);
143 if (voltage < info->voltage_min_design)
144 val->intval = POWER_SUPPLY_HEALTH_DEAD;
146 val->intval = POWER_SUPPLY_HEALTH_GOOD;
148 case POWER_SUPPLY_PROP_CAPACITY:
149 val->intval = jz_battery_get_capacity(psy);
151 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
152 val->intval = jz_battery_read_voltage(jz_battery);
156 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
157 val->intval = info->voltage_max_design;
159 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
160 val->intval = info->voltage_min_design;
162 case POWER_SUPPLY_PROP_PRESENT:
171 static void jz_battery_external_power_changed(struct power_supply *psy)
173 struct jz_battery *jz_battery = psy_to_jz_battery(psy);
175 mod_delayed_work(system_wq, &jz_battery->work, 0);
178 static irqreturn_t jz_battery_charge_irq(int irq, void *data)
180 struct jz_battery *jz_battery = data;
182 mod_delayed_work(system_wq, &jz_battery->work, 0);
187 static void jz_battery_update(struct jz_battery *jz_battery)
191 bool has_changed = false;
194 if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
195 is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
196 is_charging ^= jz_battery->pdata->gpio_charge_active_low;
198 status = POWER_SUPPLY_STATUS_CHARGING;
200 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
202 if (status != jz_battery->status) {
203 jz_battery->status = status;
208 voltage = jz_battery_read_voltage(jz_battery);
209 if (abs(voltage - jz_battery->voltage) < 50000) {
210 jz_battery->voltage = voltage;
215 power_supply_changed(&jz_battery->battery);
218 static enum power_supply_property jz_battery_properties[] = {
219 POWER_SUPPLY_PROP_STATUS,
220 POWER_SUPPLY_PROP_TECHNOLOGY,
221 POWER_SUPPLY_PROP_HEALTH,
222 POWER_SUPPLY_PROP_CAPACITY,
223 POWER_SUPPLY_PROP_VOLTAGE_NOW,
224 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
225 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
226 POWER_SUPPLY_PROP_PRESENT,
229 static void jz_battery_work(struct work_struct *work)
231 /* Too small interval will increase system workload */
232 const int interval = HZ * 30;
233 struct jz_battery *jz_battery = container_of(work, struct jz_battery,
236 jz_battery_update(jz_battery);
237 schedule_delayed_work(&jz_battery->work, interval);
240 static int jz_battery_probe(struct platform_device *pdev)
243 struct jz_battery_platform_data *pdata = pdev->dev.parent->platform_data;
244 struct jz_battery *jz_battery;
245 struct power_supply *battery;
246 struct resource *mem;
249 dev_err(&pdev->dev, "No platform_data supplied\n");
253 jz_battery = devm_kzalloc(&pdev->dev, sizeof(*jz_battery), GFP_KERNEL);
255 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
259 jz_battery->cell = mfd_get_cell(pdev);
261 jz_battery->irq = platform_get_irq(pdev, 0);
262 if (jz_battery->irq < 0) {
263 dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
264 return jz_battery->irq;
267 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
269 jz_battery->base = devm_request_and_ioremap(&pdev->dev, mem);
270 if (!jz_battery->base)
273 battery = &jz_battery->battery;
274 battery->name = pdata->info.name;
275 battery->type = POWER_SUPPLY_TYPE_BATTERY;
276 battery->properties = jz_battery_properties;
277 battery->num_properties = ARRAY_SIZE(jz_battery_properties);
278 battery->get_property = jz_battery_get_property;
279 battery->external_power_changed = jz_battery_external_power_changed;
280 battery->use_for_apm = 1;
282 jz_battery->pdata = pdata;
283 jz_battery->pdev = pdev;
285 init_completion(&jz_battery->read_completion);
286 mutex_init(&jz_battery->lock);
288 INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
290 ret = request_irq(jz_battery->irq, jz_battery_irq_handler, 0, pdev->name,
293 dev_err(&pdev->dev, "Failed to request irq %d\n", ret);
296 disable_irq(jz_battery->irq);
298 if (gpio_is_valid(pdata->gpio_charge)) {
299 ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
301 dev_err(&pdev->dev, "charger state gpio request failed.\n");
304 ret = gpio_direction_input(pdata->gpio_charge);
306 dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
310 jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
312 if (jz_battery->charge_irq >= 0) {
313 ret = request_irq(jz_battery->charge_irq,
314 jz_battery_charge_irq,
315 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
316 dev_name(&pdev->dev), jz_battery);
318 dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
323 jz_battery->charge_irq = -1;
326 if (jz_battery->pdata->info.voltage_max_design <= 2500000)
327 jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB,
328 JZ_ADC_CONFIG_BAT_MB);
330 jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB, 0);
332 ret = power_supply_register(&pdev->dev, &jz_battery->battery);
334 dev_err(&pdev->dev, "power supply battery register failed.\n");
335 goto err_free_charge_irq;
338 platform_set_drvdata(pdev, jz_battery);
339 schedule_delayed_work(&jz_battery->work, 0);
344 if (jz_battery->charge_irq >= 0)
345 free_irq(jz_battery->charge_irq, jz_battery);
347 if (gpio_is_valid(pdata->gpio_charge))
348 gpio_free(jz_battery->pdata->gpio_charge);
350 free_irq(jz_battery->irq, jz_battery);
352 platform_set_drvdata(pdev, NULL);
356 static int jz_battery_remove(struct platform_device *pdev)
358 struct jz_battery *jz_battery = platform_get_drvdata(pdev);
360 cancel_delayed_work_sync(&jz_battery->work);
362 if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
363 if (jz_battery->charge_irq >= 0)
364 free_irq(jz_battery->charge_irq, jz_battery);
365 gpio_free(jz_battery->pdata->gpio_charge);
368 power_supply_unregister(&jz_battery->battery);
370 free_irq(jz_battery->irq, jz_battery);
376 static int jz_battery_suspend(struct device *dev)
378 struct jz_battery *jz_battery = dev_get_drvdata(dev);
380 cancel_delayed_work_sync(&jz_battery->work);
381 jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
386 static int jz_battery_resume(struct device *dev)
388 struct jz_battery *jz_battery = dev_get_drvdata(dev);
390 schedule_delayed_work(&jz_battery->work, 0);
395 static const struct dev_pm_ops jz_battery_pm_ops = {
396 .suspend = jz_battery_suspend,
397 .resume = jz_battery_resume,
400 #define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
402 #define JZ_BATTERY_PM_OPS NULL
405 static struct platform_driver jz_battery_driver = {
406 .probe = jz_battery_probe,
407 .remove = jz_battery_remove,
409 .name = "jz4740-battery",
410 .owner = THIS_MODULE,
411 .pm = JZ_BATTERY_PM_OPS,
415 module_platform_driver(jz_battery_driver);
417 MODULE_ALIAS("platform:jz4740-battery");
418 MODULE_LICENSE("GPL");
419 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
420 MODULE_DESCRIPTION("JZ4740 SoC battery driver");