blob: 6ea3cb5837c707963784555aabafe14c35b0b48a [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>
Marek Vašut4e9687d2008-09-11 19:37:32 +010026
27static DEFINE_MUTEX(bat_lock);
28static struct work_struct bat_work;
29struct mutex work_lock;
30static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020031static struct wm97xx_batt_info *gpdata;
Marek Vašut4e9687d2008-09-11 19:37:32 +010032static enum power_supply_property *prop;
33
34static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
35{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020036 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
37 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
38
Alexander Beregalov38c7dc32009-06-23 17:50:06 +040039 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev->parent),
Marek Vašut4e9687d2008-09-11 19:37:32 +010040 pdata->batt_aux) * pdata->batt_mult /
41 pdata->batt_div;
42}
43
44static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
45{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020046 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
47 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
48
Alexander Beregalov38c7dc32009-06-23 17:50:06 +040049 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev->parent),
Marek Vašut4e9687d2008-09-11 19:37:32 +010050 pdata->temp_aux) * pdata->temp_mult /
51 pdata->temp_div;
52}
53
54static int wm97xx_bat_get_property(struct power_supply *bat_ps,
55 enum power_supply_property psp,
56 union power_supply_propval *val)
57{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +020058 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
59 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
60
Marek Vašut4e9687d2008-09-11 19:37:32 +010061 switch (psp) {
62 case POWER_SUPPLY_PROP_STATUS:
63 val->intval = bat_status;
64 break;
65 case POWER_SUPPLY_PROP_TECHNOLOGY:
66 val->intval = pdata->batt_tech;
67 break;
68 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
69 if (pdata->batt_aux >= 0)
70 val->intval = wm97xx_read_bat(bat_ps);
71 else
72 return -EINVAL;
73 break;
74 case POWER_SUPPLY_PROP_TEMP:
75 if (pdata->temp_aux >= 0)
76 val->intval = wm97xx_read_temp(bat_ps);
77 else
78 return -EINVAL;
79 break;
80 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
81 if (pdata->max_voltage >= 0)
82 val->intval = pdata->max_voltage;
83 else
84 return -EINVAL;
85 break;
86 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
87 if (pdata->min_voltage >= 0)
88 val->intval = pdata->min_voltage;
89 else
90 return -EINVAL;
91 break;
92 case POWER_SUPPLY_PROP_PRESENT:
93 val->intval = 1;
94 break;
95 default:
96 return -EINVAL;
97 }
98 return 0;
99}
100
101static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
102{
103 schedule_work(&bat_work);
104}
105
106static void wm97xx_bat_update(struct power_supply *bat_ps)
107{
108 int old_status = bat_status;
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200109 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
110 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100111
112 mutex_lock(&work_lock);
113
114 bat_status = (pdata->charge_gpio >= 0) ?
115 (gpio_get_value(pdata->charge_gpio) ?
116 POWER_SUPPLY_STATUS_DISCHARGING :
117 POWER_SUPPLY_STATUS_CHARGING) :
118 POWER_SUPPLY_STATUS_UNKNOWN;
119
120 if (old_status != bat_status) {
121 pr_debug("%s: %i -> %i\n", bat_ps->name, old_status,
122 bat_status);
123 power_supply_changed(bat_ps);
124 }
125
126 mutex_unlock(&work_lock);
127}
128
129static struct power_supply bat_ps = {
130 .type = POWER_SUPPLY_TYPE_BATTERY,
131 .get_property = wm97xx_bat_get_property,
132 .external_power_changed = wm97xx_bat_external_power_changed,
133 .use_for_apm = 1,
134};
135
136static void wm97xx_bat_work(struct work_struct *work)
137{
138 wm97xx_bat_update(&bat_ps);
139}
140
Marek Vasut7c879422009-07-23 16:02:08 +0200141static irqreturn_t wm97xx_chrg_irq(int irq, void *data)
142{
143 schedule_work(&bat_work);
144 return IRQ_HANDLED;
145}
146
Marek Vašut4e9687d2008-09-11 19:37:32 +0100147#ifdef CONFIG_PM
Marek Vasut83a8af02009-08-22 00:36:43 +0200148static int wm97xx_bat_suspend(struct device *dev)
Marek Vašut4e9687d2008-09-11 19:37:32 +0100149{
150 flush_scheduled_work();
151 return 0;
152}
153
Marek Vasut83a8af02009-08-22 00:36:43 +0200154static int wm97xx_bat_resume(struct device *dev)
Marek Vašut4e9687d2008-09-11 19:37:32 +0100155{
156 schedule_work(&bat_work);
157 return 0;
158}
Marek Vasut83a8af02009-08-22 00:36:43 +0200159
Alexey Dobriyan47145212009-12-14 18:00:08 -0800160static const struct dev_pm_ops wm97xx_bat_pm_ops = {
Marek Vasut83a8af02009-08-22 00:36:43 +0200161 .suspend = wm97xx_bat_suspend,
162 .resume = wm97xx_bat_resume,
163};
Marek Vašut4e9687d2008-09-11 19:37:32 +0100164#endif
165
166static int __devinit wm97xx_bat_probe(struct platform_device *dev)
167{
168 int ret = 0;
169 int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
170 int i = 0;
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200171 struct wm97xx_pdata *wmdata = dev->dev.platform_data;
172 struct wm97xx_batt_pdata *pdata;
173
174 if (gpdata) {
175 dev_err(&dev->dev, "Do not pass platform_data through "
176 "wm97xx_bat_set_pdata!\n");
177 return -EINVAL;
Mark Brown12b336a2010-01-27 20:43:21 +0000178 }
179
180 if (!wmdata) {
181 dev_err(&dev->dev, "No platform data supplied\n");
182 return -EINVAL;
183 }
184
185 pdata = wmdata->batt_pdata;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100186
187 if (dev->id != -1)
188 return -EINVAL;
189
190 mutex_init(&work_lock);
191
192 if (!pdata) {
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200193 dev_err(&dev->dev, "No platform_data supplied\n");
Marek Vašut4e9687d2008-09-11 19:37:32 +0100194 return -EINVAL;
195 }
196
Marek Vasut7c879422009-07-23 16:02:08 +0200197 if (gpio_is_valid(pdata->charge_gpio)) {
Marek Vašut4e9687d2008-09-11 19:37:32 +0100198 ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
199 if (ret)
200 goto err;
201 ret = gpio_direction_input(pdata->charge_gpio);
202 if (ret)
203 goto err2;
Marek Vasut7c879422009-07-23 16:02:08 +0200204 ret = request_irq(gpio_to_irq(pdata->charge_gpio),
205 wm97xx_chrg_irq, IRQF_DISABLED,
206 "AC Detect", 0);
207 if (ret)
208 goto err2;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100209 props++; /* POWER_SUPPLY_PROP_STATUS */
210 }
211
212 if (pdata->batt_tech >= 0)
213 props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
214 if (pdata->temp_aux >= 0)
215 props++; /* POWER_SUPPLY_PROP_TEMP */
216 if (pdata->batt_aux >= 0)
217 props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
218 if (pdata->max_voltage >= 0)
219 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
220 if (pdata->min_voltage >= 0)
221 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
222
223 prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
224 if (!prop)
Marek Vasut7c879422009-07-23 16:02:08 +0200225 goto err3;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100226
227 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
228 if (pdata->charge_gpio >= 0)
229 prop[i++] = POWER_SUPPLY_PROP_STATUS;
230 if (pdata->batt_tech >= 0)
231 prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
232 if (pdata->temp_aux >= 0)
233 prop[i++] = POWER_SUPPLY_PROP_TEMP;
234 if (pdata->batt_aux >= 0)
235 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
236 if (pdata->max_voltage >= 0)
237 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
238 if (pdata->min_voltage >= 0)
239 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
240
241 INIT_WORK(&bat_work, wm97xx_bat_work);
242
243 if (!pdata->batt_name) {
244 dev_info(&dev->dev, "Please consider setting proper battery "
245 "name in platform definition file, falling "
246 "back to name \"wm97xx-batt\"\n");
247 bat_ps.name = "wm97xx-batt";
248 } else
249 bat_ps.name = pdata->batt_name;
250
251 bat_ps.properties = prop;
252 bat_ps.num_properties = props;
253
254 ret = power_supply_register(&dev->dev, &bat_ps);
255 if (!ret)
256 schedule_work(&bat_work);
257 else
Marek Vasut7c879422009-07-23 16:02:08 +0200258 goto err4;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100259
260 return 0;
Marek Vasut7c879422009-07-23 16:02:08 +0200261err4:
Marek Vašut4e9687d2008-09-11 19:37:32 +0100262 kfree(prop);
Marek Vasut7c879422009-07-23 16:02:08 +0200263err3:
264 if (gpio_is_valid(pdata->charge_gpio))
265 free_irq(gpio_to_irq(pdata->charge_gpio), dev);
Marek Vašut4e9687d2008-09-11 19:37:32 +0100266err2:
Marek Vasut7c879422009-07-23 16:02:08 +0200267 if (gpio_is_valid(pdata->charge_gpio))
268 gpio_free(pdata->charge_gpio);
Marek Vašut4e9687d2008-09-11 19:37:32 +0100269err:
270 return ret;
271}
272
273static int __devexit wm97xx_bat_remove(struct platform_device *dev)
274{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200275 struct wm97xx_pdata *wmdata = dev->dev.platform_data;
276 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
277
Marek Vasut7c879422009-07-23 16:02:08 +0200278 if (pdata && gpio_is_valid(pdata->charge_gpio)) {
279 free_irq(gpio_to_irq(pdata->charge_gpio), dev);
Marek Vašut4e9687d2008-09-11 19:37:32 +0100280 gpio_free(pdata->charge_gpio);
Marek Vasut7c879422009-07-23 16:02:08 +0200281 }
Marek Vašut4e9687d2008-09-11 19:37:32 +0100282 flush_scheduled_work();
283 power_supply_unregister(&bat_ps);
284 kfree(prop);
285 return 0;
286}
287
288static struct platform_driver wm97xx_bat_driver = {
289 .driver = {
290 .name = "wm97xx-battery",
291 .owner = THIS_MODULE,
Marek Vasut83a8af02009-08-22 00:36:43 +0200292#ifdef CONFIG_PM
293 .pm = &wm97xx_bat_pm_ops,
294#endif
Marek Vašut4e9687d2008-09-11 19:37:32 +0100295 },
296 .probe = wm97xx_bat_probe,
297 .remove = __devexit_p(wm97xx_bat_remove),
Marek Vašut4e9687d2008-09-11 19:37:32 +0100298};
299
300static int __init wm97xx_bat_init(void)
301{
302 return platform_driver_register(&wm97xx_bat_driver);
303}
304
305static void __exit wm97xx_bat_exit(void)
306{
307 platform_driver_unregister(&wm97xx_bat_driver);
308}
309
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200310void wm97xx_bat_set_pdata(struct wm97xx_batt_info *data)
Marek Vašut4e9687d2008-09-11 19:37:32 +0100311{
Marek Vasutb8bdc1d2009-08-31 06:20:12 +0200312 gpdata = data;
Marek Vašut4e9687d2008-09-11 19:37:32 +0100313}
314EXPORT_SYMBOL_GPL(wm97xx_bat_set_pdata);
315
316module_init(wm97xx_bat_init);
317module_exit(wm97xx_bat_exit);
318
319MODULE_LICENSE("GPL");
320MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
321MODULE_DESCRIPTION("WM97xx battery driver");