blob: 4e8afce0c81805cbca50d2bd0ffdfd249fbeeab2 [file] [log] [blame]
Marek Vašut4e9687d2008-09-11 19:37:32 +01001/*
2 * linux/drivers/power/wm97xx_battery.c
3 *
4 * Battery measurement code for WM97xx
5 *
6 * based on tosa_battery.c
7 *
8 * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/power_supply.h>
21#include <linux/wm97xx.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/gpio.h>
Marek Vasut7c879422009-07-23 16:02:08 +020025#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Marek Vašut4e9687d2008-09-11 19:37:32 +010027
28static DEFINE_MUTEX(bat_lock);
29static struct work_struct bat_work;
Mark Brownf8d94eb2010-01-27 20:45:35 +000030static struct mutex work_lock;
Marek Vašut4e9687d2008-09-11 19:37:32 +010031static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020032static struct wm97xx_batt_info *gpdata;
Marek Vašut4e9687d2008-09-11 19:37:32 +010033static enum power_supply_property *prop;
34
35static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
36{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020037 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
38 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
39
Alexander Beregalov38c7dc32009-06-23 17:50:06 +040040 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev->parent),
Marek Vašut4e9687d2008-09-11 19:37:32 +010041 pdata->batt_aux) * pdata->batt_mult /
42 pdata->batt_div;
43}
44
45static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
46{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020047 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
48 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
49
Alexander Beregalov38c7dc32009-06-23 17:50:06 +040050 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev->parent),
Marek Vašut4e9687d2008-09-11 19:37:32 +010051 pdata->temp_aux) * pdata->temp_mult /
52 pdata->temp_div;
53}
54
55static int wm97xx_bat_get_property(struct power_supply *bat_ps,
56 enum power_supply_property psp,
57 union power_supply_propval *val)
58{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020059 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
60 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
61
Marek Vašut4e9687d2008-09-11 19:37:32 +010062 switch (psp) {
63 case POWER_SUPPLY_PROP_STATUS:
64 val->intval = bat_status;
65 break;
66 case POWER_SUPPLY_PROP_TECHNOLOGY:
67 val->intval = pdata->batt_tech;
68 break;
69 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
70 if (pdata->batt_aux >= 0)
71 val->intval = wm97xx_read_bat(bat_ps);
72 else
73 return -EINVAL;
74 break;
75 case POWER_SUPPLY_PROP_TEMP:
76 if (pdata->temp_aux >= 0)
77 val->intval = wm97xx_read_temp(bat_ps);
78 else
79 return -EINVAL;
80 break;
81 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
82 if (pdata->max_voltage >= 0)
83 val->intval = pdata->max_voltage;
84 else
85 return -EINVAL;
86 break;
87 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
88 if (pdata->min_voltage >= 0)
89 val->intval = pdata->min_voltage;
90 else
91 return -EINVAL;
92 break;
93 case POWER_SUPPLY_PROP_PRESENT:
94 val->intval = 1;
95 break;
96 default:
97 return -EINVAL;
98 }
99 return 0;
100}
101
102static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
103{
104 schedule_work(&bat_work);
105}
106
107static void wm97xx_bat_update(struct power_supply *bat_ps)
108{
109 int old_status = bat_status;
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200110 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
111 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100112
113 mutex_lock(&work_lock);
114
115 bat_status = (pdata->charge_gpio >= 0) ?
116 (gpio_get_value(pdata->charge_gpio) ?
117 POWER_SUPPLY_STATUS_DISCHARGING :
118 POWER_SUPPLY_STATUS_CHARGING) :
119 POWER_SUPPLY_STATUS_UNKNOWN;
120
121 if (old_status != bat_status) {
122 pr_debug("%s: %i -> %i\n", bat_ps->name, old_status,
123 bat_status);
124 power_supply_changed(bat_ps);
125 }
126
127 mutex_unlock(&work_lock);
128}
129
130static struct power_supply bat_ps = {
131 .type = POWER_SUPPLY_TYPE_BATTERY,
132 .get_property = wm97xx_bat_get_property,
133 .external_power_changed = wm97xx_bat_external_power_changed,
134 .use_for_apm = 1,
135};
136
137static void wm97xx_bat_work(struct work_struct *work)
138{
139 wm97xx_bat_update(&bat_ps);
140}
141
Marek Vasut7c879422009-07-23 16:02:08 +0200142static irqreturn_t wm97xx_chrg_irq(int irq, void *data)
143{
144 schedule_work(&bat_work);
145 return IRQ_HANDLED;
146}
147
Marek Vašut4e9687d2008-09-11 19:37:32 +0100148#ifdef CONFIG_PM
Marek Vasut83a8af02009-08-22 00:36:43 +0200149static int wm97xx_bat_suspend(struct device *dev)
Marek Vašut4e9687d2008-09-11 19:37:32 +0100150{
151 flush_scheduled_work();
152 return 0;
153}
154
Marek Vasut83a8af02009-08-22 00:36:43 +0200155static int wm97xx_bat_resume(struct device *dev)
Marek Vašut4e9687d2008-09-11 19:37:32 +0100156{
157 schedule_work(&bat_work);
158 return 0;
159}
Marek Vasut83a8af02009-08-22 00:36:43 +0200160
Alexey Dobriyan47145212009-12-14 18:00:08 -0800161static const struct dev_pm_ops wm97xx_bat_pm_ops = {
Marek Vasut83a8af02009-08-22 00:36:43 +0200162 .suspend = wm97xx_bat_suspend,
163 .resume = wm97xx_bat_resume,
164};
Marek Vašut4e9687d2008-09-11 19:37:32 +0100165#endif
166
167static int __devinit wm97xx_bat_probe(struct platform_device *dev)
168{
169 int ret = 0;
170 int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
171 int i = 0;
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200172 struct wm97xx_pdata *wmdata = dev->dev.platform_data;
173 struct wm97xx_batt_pdata *pdata;
174
175 if (gpdata) {
176 dev_err(&dev->dev, "Do not pass platform_data through "
177 "wm97xx_bat_set_pdata!\n");
178 return -EINVAL;
Mark Brown12b336a2010-01-27 20:43:21 +0000179 }
180
181 if (!wmdata) {
182 dev_err(&dev->dev, "No platform data supplied\n");
183 return -EINVAL;
184 }
185
186 pdata = wmdata->batt_pdata;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100187
188 if (dev->id != -1)
189 return -EINVAL;
190
191 mutex_init(&work_lock);
192
193 if (!pdata) {
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200194 dev_err(&dev->dev, "No platform_data supplied\n");
Marek Vašut4e9687d2008-09-11 19:37:32 +0100195 return -EINVAL;
196 }
197
Marek Vasut7c879422009-07-23 16:02:08 +0200198 if (gpio_is_valid(pdata->charge_gpio)) {
Marek Vašut4e9687d2008-09-11 19:37:32 +0100199 ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
200 if (ret)
201 goto err;
202 ret = gpio_direction_input(pdata->charge_gpio);
203 if (ret)
204 goto err2;
Marek Vasut7c879422009-07-23 16:02:08 +0200205 ret = request_irq(gpio_to_irq(pdata->charge_gpio),
206 wm97xx_chrg_irq, IRQF_DISABLED,
Mark Brownf8d94eb2010-01-27 20:45:35 +0000207 "AC Detect", dev);
Marek Vasut7c879422009-07-23 16:02:08 +0200208 if (ret)
209 goto err2;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100210 props++; /* POWER_SUPPLY_PROP_STATUS */
211 }
212
213 if (pdata->batt_tech >= 0)
214 props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
215 if (pdata->temp_aux >= 0)
216 props++; /* POWER_SUPPLY_PROP_TEMP */
217 if (pdata->batt_aux >= 0)
218 props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
219 if (pdata->max_voltage >= 0)
220 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
221 if (pdata->min_voltage >= 0)
222 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
223
224 prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
225 if (!prop)
Marek Vasut7c879422009-07-23 16:02:08 +0200226 goto err3;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100227
228 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
229 if (pdata->charge_gpio >= 0)
230 prop[i++] = POWER_SUPPLY_PROP_STATUS;
231 if (pdata->batt_tech >= 0)
232 prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
233 if (pdata->temp_aux >= 0)
234 prop[i++] = POWER_SUPPLY_PROP_TEMP;
235 if (pdata->batt_aux >= 0)
236 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
237 if (pdata->max_voltage >= 0)
238 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
239 if (pdata->min_voltage >= 0)
240 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
241
242 INIT_WORK(&bat_work, wm97xx_bat_work);
243
244 if (!pdata->batt_name) {
245 dev_info(&dev->dev, "Please consider setting proper battery "
246 "name in platform definition file, falling "
247 "back to name \"wm97xx-batt\"\n");
248 bat_ps.name = "wm97xx-batt";
249 } else
250 bat_ps.name = pdata->batt_name;
251
252 bat_ps.properties = prop;
253 bat_ps.num_properties = props;
254
255 ret = power_supply_register(&dev->dev, &bat_ps);
256 if (!ret)
257 schedule_work(&bat_work);
258 else
Marek Vasut7c879422009-07-23 16:02:08 +0200259 goto err4;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100260
261 return 0;
Marek Vasut7c879422009-07-23 16:02:08 +0200262err4:
Marek Vašut4e9687d2008-09-11 19:37:32 +0100263 kfree(prop);
Marek Vasut7c879422009-07-23 16:02:08 +0200264err3:
265 if (gpio_is_valid(pdata->charge_gpio))
266 free_irq(gpio_to_irq(pdata->charge_gpio), dev);
Marek Vašut4e9687d2008-09-11 19:37:32 +0100267err2:
Marek Vasut7c879422009-07-23 16:02:08 +0200268 if (gpio_is_valid(pdata->charge_gpio))
269 gpio_free(pdata->charge_gpio);
Marek Vašut4e9687d2008-09-11 19:37:32 +0100270err:
271 return ret;
272}
273
274static int __devexit wm97xx_bat_remove(struct platform_device *dev)
275{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200276 struct wm97xx_pdata *wmdata = dev->dev.platform_data;
277 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
278
Marek Vasut7c879422009-07-23 16:02:08 +0200279 if (pdata && gpio_is_valid(pdata->charge_gpio)) {
280 free_irq(gpio_to_irq(pdata->charge_gpio), dev);
Marek Vašut4e9687d2008-09-11 19:37:32 +0100281 gpio_free(pdata->charge_gpio);
Marek Vasut7c879422009-07-23 16:02:08 +0200282 }
Marek Vašut4e9687d2008-09-11 19:37:32 +0100283 flush_scheduled_work();
284 power_supply_unregister(&bat_ps);
285 kfree(prop);
286 return 0;
287}
288
289static struct platform_driver wm97xx_bat_driver = {
290 .driver = {
291 .name = "wm97xx-battery",
292 .owner = THIS_MODULE,
Marek Vasut83a8af02009-08-22 00:36:43 +0200293#ifdef CONFIG_PM
294 .pm = &wm97xx_bat_pm_ops,
295#endif
Marek Vašut4e9687d2008-09-11 19:37:32 +0100296 },
297 .probe = wm97xx_bat_probe,
298 .remove = __devexit_p(wm97xx_bat_remove),
Marek Vašut4e9687d2008-09-11 19:37:32 +0100299};
300
301static int __init wm97xx_bat_init(void)
302{
303 return platform_driver_register(&wm97xx_bat_driver);
304}
305
306static void __exit wm97xx_bat_exit(void)
307{
308 platform_driver_unregister(&wm97xx_bat_driver);
309}
310
Anton Vorontsovb1458fb2010-05-02 23:50:38 +0400311/* The interface is deprecated, as well as linux/wm97xx_batt.h */
312void wm97xx_bat_set_pdata(struct wm97xx_batt_info *data);
313
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200314void wm97xx_bat_set_pdata(struct wm97xx_batt_info *data)
Marek Vašut4e9687d2008-09-11 19:37:32 +0100315{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200316 gpdata = data;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100317}
318EXPORT_SYMBOL_GPL(wm97xx_bat_set_pdata);
319
320module_init(wm97xx_bat_init);
321module_exit(wm97xx_bat_exit);
322
323MODULE_LICENSE("GPL");
324MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
325MODULE_DESCRIPTION("WM97xx battery driver");