wm97xx_battery: Use platform_data
[deliverable/linux.git] / drivers / power / wm97xx_battery.c
CommitLineData
4e9687d9
MV
1/*
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>
4e9687d9
MV
25
26static DEFINE_MUTEX(bat_lock);
27static struct work_struct bat_work;
28struct mutex work_lock;
29static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
b8bdc1d0 30static struct wm97xx_batt_info *gpdata;
4e9687d9
MV
31static enum power_supply_property *prop;
32
33static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
34{
b8bdc1d0
MV
35 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
36 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
37
4e9687d9
MV
38 return wm97xx_read_aux_adc(bat_ps->dev->parent->driver_data,
39 pdata->batt_aux) * pdata->batt_mult /
40 pdata->batt_div;
41}
42
43static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
44{
b8bdc1d0
MV
45 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
46 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
47
4e9687d9
MV
48 return wm97xx_read_aux_adc(bat_ps->dev->parent->driver_data,
49 pdata->temp_aux) * pdata->temp_mult /
50 pdata->temp_div;
51}
52
53static int wm97xx_bat_get_property(struct power_supply *bat_ps,
54 enum power_supply_property psp,
55 union power_supply_propval *val)
56{
b8bdc1d0
MV
57 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
58 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
59
4e9687d9
MV
60 switch (psp) {
61 case POWER_SUPPLY_PROP_STATUS:
62 val->intval = bat_status;
63 break;
64 case POWER_SUPPLY_PROP_TECHNOLOGY:
65 val->intval = pdata->batt_tech;
66 break;
67 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
68 if (pdata->batt_aux >= 0)
69 val->intval = wm97xx_read_bat(bat_ps);
70 else
71 return -EINVAL;
72 break;
73 case POWER_SUPPLY_PROP_TEMP:
74 if (pdata->temp_aux >= 0)
75 val->intval = wm97xx_read_temp(bat_ps);
76 else
77 return -EINVAL;
78 break;
79 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
80 if (pdata->max_voltage >= 0)
81 val->intval = pdata->max_voltage;
82 else
83 return -EINVAL;
84 break;
85 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
86 if (pdata->min_voltage >= 0)
87 val->intval = pdata->min_voltage;
88 else
89 return -EINVAL;
90 break;
91 case POWER_SUPPLY_PROP_PRESENT:
92 val->intval = 1;
93 break;
94 default:
95 return -EINVAL;
96 }
97 return 0;
98}
99
100static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
101{
102 schedule_work(&bat_work);
103}
104
105static void wm97xx_bat_update(struct power_supply *bat_ps)
106{
107 int old_status = bat_status;
b8bdc1d0
MV
108 struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
109 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
4e9687d9
MV
110
111 mutex_lock(&work_lock);
112
113 bat_status = (pdata->charge_gpio >= 0) ?
114 (gpio_get_value(pdata->charge_gpio) ?
115 POWER_SUPPLY_STATUS_DISCHARGING :
116 POWER_SUPPLY_STATUS_CHARGING) :
117 POWER_SUPPLY_STATUS_UNKNOWN;
118
119 if (old_status != bat_status) {
120 pr_debug("%s: %i -> %i\n", bat_ps->name, old_status,
121 bat_status);
122 power_supply_changed(bat_ps);
123 }
124
125 mutex_unlock(&work_lock);
126}
127
128static struct power_supply bat_ps = {
129 .type = POWER_SUPPLY_TYPE_BATTERY,
130 .get_property = wm97xx_bat_get_property,
131 .external_power_changed = wm97xx_bat_external_power_changed,
132 .use_for_apm = 1,
133};
134
135static void wm97xx_bat_work(struct work_struct *work)
136{
137 wm97xx_bat_update(&bat_ps);
138}
139
140#ifdef CONFIG_PM
141static int wm97xx_bat_suspend(struct platform_device *dev, pm_message_t state)
142{
143 flush_scheduled_work();
144 return 0;
145}
146
147static int wm97xx_bat_resume(struct platform_device *dev)
148{
149 schedule_work(&bat_work);
150 return 0;
151}
152#else
153#define wm97xx_bat_suspend NULL
154#define wm97xx_bat_resume NULL
155#endif
156
157static int __devinit wm97xx_bat_probe(struct platform_device *dev)
158{
159 int ret = 0;
160 int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
161 int i = 0;
b8bdc1d0
MV
162 struct wm97xx_pdata *wmdata = dev->dev.platform_data;
163 struct wm97xx_batt_pdata *pdata;
164
165 if (gpdata) {
166 dev_err(&dev->dev, "Do not pass platform_data through "
167 "wm97xx_bat_set_pdata!\n");
168 return -EINVAL;
169 } else
170 pdata = wmdata->batt_pdata;
4e9687d9
MV
171
172 if (dev->id != -1)
173 return -EINVAL;
174
175 mutex_init(&work_lock);
176
177 if (!pdata) {
b8bdc1d0 178 dev_err(&dev->dev, "No platform_data supplied\n");
4e9687d9
MV
179 return -EINVAL;
180 }
181
182 if (pdata->charge_gpio >= 0 && gpio_is_valid(pdata->charge_gpio)) {
183 ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
184 if (ret)
185 goto err;
186 ret = gpio_direction_input(pdata->charge_gpio);
187 if (ret)
188 goto err2;
189 props++; /* POWER_SUPPLY_PROP_STATUS */
190 }
191
192 if (pdata->batt_tech >= 0)
193 props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
194 if (pdata->temp_aux >= 0)
195 props++; /* POWER_SUPPLY_PROP_TEMP */
196 if (pdata->batt_aux >= 0)
197 props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
198 if (pdata->max_voltage >= 0)
199 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
200 if (pdata->min_voltage >= 0)
201 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
202
203 prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
204 if (!prop)
205 goto err2;
206
207 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
208 if (pdata->charge_gpio >= 0)
209 prop[i++] = POWER_SUPPLY_PROP_STATUS;
210 if (pdata->batt_tech >= 0)
211 prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
212 if (pdata->temp_aux >= 0)
213 prop[i++] = POWER_SUPPLY_PROP_TEMP;
214 if (pdata->batt_aux >= 0)
215 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
216 if (pdata->max_voltage >= 0)
217 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
218 if (pdata->min_voltage >= 0)
219 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
220
221 INIT_WORK(&bat_work, wm97xx_bat_work);
222
223 if (!pdata->batt_name) {
224 dev_info(&dev->dev, "Please consider setting proper battery "
225 "name in platform definition file, falling "
226 "back to name \"wm97xx-batt\"\n");
227 bat_ps.name = "wm97xx-batt";
228 } else
229 bat_ps.name = pdata->batt_name;
230
231 bat_ps.properties = prop;
232 bat_ps.num_properties = props;
233
234 ret = power_supply_register(&dev->dev, &bat_ps);
235 if (!ret)
236 schedule_work(&bat_work);
237 else
238 goto err3;
239
240 return 0;
241err3:
242 kfree(prop);
243err2:
244 gpio_free(pdata->charge_gpio);
245err:
246 return ret;
247}
248
249static int __devexit wm97xx_bat_remove(struct platform_device *dev)
250{
b8bdc1d0
MV
251 struct wm97xx_pdata *wmdata = dev->dev.platform_data;
252 struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
253
4e9687d9
MV
254 if (pdata && pdata->charge_gpio && pdata->charge_gpio >= 0)
255 gpio_free(pdata->charge_gpio);
256 flush_scheduled_work();
257 power_supply_unregister(&bat_ps);
258 kfree(prop);
259 return 0;
260}
261
262static struct platform_driver wm97xx_bat_driver = {
263 .driver = {
264 .name = "wm97xx-battery",
265 .owner = THIS_MODULE,
266 },
267 .probe = wm97xx_bat_probe,
268 .remove = __devexit_p(wm97xx_bat_remove),
269 .suspend = wm97xx_bat_suspend,
270 .resume = wm97xx_bat_resume,
271};
272
273static int __init wm97xx_bat_init(void)
274{
275 return platform_driver_register(&wm97xx_bat_driver);
276}
277
278static void __exit wm97xx_bat_exit(void)
279{
280 platform_driver_unregister(&wm97xx_bat_driver);
281}
282
b8bdc1d0 283void wm97xx_bat_set_pdata(struct wm97xx_batt_info *data)
4e9687d9 284{
b8bdc1d0 285 gpdata = data;
4e9687d9
MV
286}
287EXPORT_SYMBOL_GPL(wm97xx_bat_set_pdata);
288
289module_init(wm97xx_bat_init);
290module_exit(wm97xx_bat_exit);
291
292MODULE_LICENSE("GPL");
293MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
294MODULE_DESCRIPTION("WM97xx battery driver");
This page took 0.102624 seconds and 5 git commands to generate.