blob: c675553d46473a7f299440b2ec85f6735770c91e [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>
Thierry Redinge2cbdf32013-01-21 11:09:15 +010025#include <linux/err.h>
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +000026#include <linux/gpio.h>
27#include <linux/mfd/core.h>
28#include <linux/power_supply.h>
29
30#include <linux/power/jz4740-battery.h>
31#include <linux/jz4740-adc.h>
32
33struct jz_battery {
34 struct jz_battery_platform_data *pdata;
35 struct platform_device *pdev;
36
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +000037 void __iomem *base;
38
39 int irq;
40 int charge_irq;
41
Andres Salomone9300062011-03-01 13:35:48 -080042 const struct mfd_cell *cell;
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +000043
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{
Axel Linef7906f2011-12-08 21:05:26 +080070 long t;
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +000071 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
Tejun Heo41f63c52012-08-03 10:30:47 -0700176 mod_delayed_work(system_wq, &jz_battery->work, 0);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000177}
178
179static irqreturn_t jz_battery_charge_irq(int irq, void *data)
180{
181 struct jz_battery *jz_battery = data;
182
Tejun Heo41f63c52012-08-03 10:30:47 -0700183 mod_delayed_work(system_wq, &jz_battery->work, 0);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000184
185 return IRQ_HANDLED;
186}
187
188static void jz_battery_update(struct jz_battery *jz_battery)
189{
190 int status;
191 long voltage;
192 bool has_changed = false;
193 int is_charging;
194
195 if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
196 is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
197 is_charging ^= jz_battery->pdata->gpio_charge_active_low;
198 if (is_charging)
199 status = POWER_SUPPLY_STATUS_CHARGING;
200 else
201 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
202
203 if (status != jz_battery->status) {
204 jz_battery->status = status;
205 has_changed = true;
206 }
207 }
208
209 voltage = jz_battery_read_voltage(jz_battery);
210 if (abs(voltage - jz_battery->voltage) < 50000) {
211 jz_battery->voltage = voltage;
212 has_changed = true;
213 }
214
215 if (has_changed)
216 power_supply_changed(&jz_battery->battery);
217}
218
219static enum power_supply_property jz_battery_properties[] = {
220 POWER_SUPPLY_PROP_STATUS,
221 POWER_SUPPLY_PROP_TECHNOLOGY,
222 POWER_SUPPLY_PROP_HEALTH,
223 POWER_SUPPLY_PROP_CAPACITY,
224 POWER_SUPPLY_PROP_VOLTAGE_NOW,
225 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
226 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
227 POWER_SUPPLY_PROP_PRESENT,
228};
229
230static void jz_battery_work(struct work_struct *work)
231{
232 /* Too small interval will increase system workload */
233 const int interval = HZ * 30;
234 struct jz_battery *jz_battery = container_of(work, struct jz_battery,
235 work.work);
236
237 jz_battery_update(jz_battery);
238 schedule_delayed_work(&jz_battery->work, interval);
239}
240
Bill Pembertonc8afa642012-11-19 13:22:23 -0500241static int jz_battery_probe(struct platform_device *pdev)
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000242{
243 int ret = 0;
244 struct jz_battery_platform_data *pdata = pdev->dev.parent->platform_data;
245 struct jz_battery *jz_battery;
246 struct power_supply *battery;
Marcos Paulo de Souza9790f982012-11-06 21:11:12 -0200247 struct resource *mem;
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000248
Lars-Peter Clausen8477ced2010-11-11 19:00:53 +0100249 if (!pdata) {
250 dev_err(&pdev->dev, "No platform_data supplied\n");
251 return -ENXIO;
252 }
253
Marcos Paulo de Souzaec073c92012-11-06 21:11:11 -0200254 jz_battery = devm_kzalloc(&pdev->dev, sizeof(*jz_battery), GFP_KERNEL);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000255 if (!jz_battery) {
256 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
257 return -ENOMEM;
258 }
259
Andres Salomon6a54ac22011-02-17 19:07:10 -0800260 jz_battery->cell = mfd_get_cell(pdev);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000261
262 jz_battery->irq = platform_get_irq(pdev, 0);
263 if (jz_battery->irq < 0) {
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000264 dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
Marcos Paulo de Souzaec073c92012-11-06 21:11:11 -0200265 return jz_battery->irq;
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000266 }
267
Marcos Paulo de Souza9790f982012-11-06 21:11:12 -0200268 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000269
Thierry Redinge2cbdf32013-01-21 11:09:15 +0100270 jz_battery->base = devm_ioremap_resource(&pdev->dev, mem);
271 if (IS_ERR(jz_battery->base))
272 return PTR_ERR(jz_battery->base);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000273
274 battery = &jz_battery->battery;
275 battery->name = pdata->info.name;
276 battery->type = POWER_SUPPLY_TYPE_BATTERY;
277 battery->properties = jz_battery_properties;
278 battery->num_properties = ARRAY_SIZE(jz_battery_properties);
279 battery->get_property = jz_battery_get_property;
280 battery->external_power_changed = jz_battery_external_power_changed;
281 battery->use_for_apm = 1;
282
283 jz_battery->pdata = pdata;
284 jz_battery->pdev = pdev;
285
286 init_completion(&jz_battery->read_completion);
Lars-Peter Clausen8ec98fe2010-11-11 19:00:52 +0100287 mutex_init(&jz_battery->lock);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000288
289 INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
290
291 ret = request_irq(jz_battery->irq, jz_battery_irq_handler, 0, pdev->name,
292 jz_battery);
293 if (ret) {
294 dev_err(&pdev->dev, "Failed to request irq %d\n", ret);
Marcos Paulo de Souza9790f982012-11-06 21:11:12 -0200295 goto err;
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000296 }
297 disable_irq(jz_battery->irq);
298
299 if (gpio_is_valid(pdata->gpio_charge)) {
300 ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
301 if (ret) {
302 dev_err(&pdev->dev, "charger state gpio request failed.\n");
303 goto err_free_irq;
304 }
305 ret = gpio_direction_input(pdata->gpio_charge);
306 if (ret) {
307 dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
308 goto err_free_gpio;
309 }
310
311 jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
312
313 if (jz_battery->charge_irq >= 0) {
314 ret = request_irq(jz_battery->charge_irq,
315 jz_battery_charge_irq,
316 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
317 dev_name(&pdev->dev), jz_battery);
318 if (ret) {
319 dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
320 goto err_free_gpio;
321 }
322 }
323 } else {
324 jz_battery->charge_irq = -1;
325 }
326
327 if (jz_battery->pdata->info.voltage_max_design <= 2500000)
328 jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB,
329 JZ_ADC_CONFIG_BAT_MB);
330 else
331 jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB, 0);
332
333 ret = power_supply_register(&pdev->dev, &jz_battery->battery);
334 if (ret) {
335 dev_err(&pdev->dev, "power supply battery register failed.\n");
336 goto err_free_charge_irq;
337 }
338
339 platform_set_drvdata(pdev, jz_battery);
340 schedule_delayed_work(&jz_battery->work, 0);
341
342 return 0;
343
344err_free_charge_irq:
345 if (jz_battery->charge_irq >= 0)
346 free_irq(jz_battery->charge_irq, jz_battery);
347err_free_gpio:
348 if (gpio_is_valid(pdata->gpio_charge))
349 gpio_free(jz_battery->pdata->gpio_charge);
350err_free_irq:
351 free_irq(jz_battery->irq, jz_battery);
Marcos Paulo de Souza9790f982012-11-06 21:11:12 -0200352err:
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000353 platform_set_drvdata(pdev, NULL);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000354 return ret;
355}
356
Bill Pemberton415ec692012-11-19 13:26:07 -0500357static int jz_battery_remove(struct platform_device *pdev)
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000358{
359 struct jz_battery *jz_battery = platform_get_drvdata(pdev);
360
361 cancel_delayed_work_sync(&jz_battery->work);
362
363 if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
364 if (jz_battery->charge_irq >= 0)
365 free_irq(jz_battery->charge_irq, jz_battery);
366 gpio_free(jz_battery->pdata->gpio_charge);
367 }
368
369 power_supply_unregister(&jz_battery->battery);
370
371 free_irq(jz_battery->irq, jz_battery);
372
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000373 return 0;
374}
375
376#ifdef CONFIG_PM
377static int jz_battery_suspend(struct device *dev)
378{
379 struct jz_battery *jz_battery = dev_get_drvdata(dev);
380
381 cancel_delayed_work_sync(&jz_battery->work);
382 jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
383
384 return 0;
385}
386
387static int jz_battery_resume(struct device *dev)
388{
389 struct jz_battery *jz_battery = dev_get_drvdata(dev);
390
391 schedule_delayed_work(&jz_battery->work, 0);
392
393 return 0;
394}
395
396static const struct dev_pm_ops jz_battery_pm_ops = {
397 .suspend = jz_battery_suspend,
398 .resume = jz_battery_resume,
399};
400
401#define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
402#else
403#define JZ_BATTERY_PM_OPS NULL
404#endif
405
406static struct platform_driver jz_battery_driver = {
407 .probe = jz_battery_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500408 .remove = jz_battery_remove,
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000409 .driver = {
410 .name = "jz4740-battery",
411 .owner = THIS_MODULE,
412 .pm = JZ_BATTERY_PM_OPS,
413 },
414};
415
Axel Lin300bac72011-11-26 12:01:10 +0800416module_platform_driver(jz_battery_driver);
Lars-Peter Clausenf6a21382010-06-19 04:08:29 +0000417
418MODULE_ALIAS("platform:jz4740-battery");
419MODULE_LICENSE("GPL");
420MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
421MODULE_DESCRIPTION("JZ4740 SoC battery driver");