2 * linux/drivers/power/wm97xx_battery.c
4 * Battery measurement code for WM97xx
6 * based on tosa_battery.c
8 * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
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.
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>
25 #include <linux/irq.h>
26 #include <linux/slab.h>
28 static DEFINE_MUTEX(bat_lock
);
29 static struct work_struct bat_work
;
30 static struct mutex work_lock
;
31 static int bat_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
32 static struct wm97xx_batt_info
*gpdata
;
33 static enum power_supply_property
*prop
;
35 static unsigned long wm97xx_read_bat(struct power_supply
*bat_ps
)
37 struct wm97xx_pdata
*wmdata
= bat_ps
->dev
->parent
->platform_data
;
38 struct wm97xx_batt_pdata
*pdata
= wmdata
->batt_pdata
;
40 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps
->dev
->parent
),
41 pdata
->batt_aux
) * pdata
->batt_mult
/
45 static unsigned long wm97xx_read_temp(struct power_supply
*bat_ps
)
47 struct wm97xx_pdata
*wmdata
= bat_ps
->dev
->parent
->platform_data
;
48 struct wm97xx_batt_pdata
*pdata
= wmdata
->batt_pdata
;
50 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps
->dev
->parent
),
51 pdata
->temp_aux
) * pdata
->temp_mult
/
55 static int wm97xx_bat_get_property(struct power_supply
*bat_ps
,
56 enum power_supply_property psp
,
57 union power_supply_propval
*val
)
59 struct wm97xx_pdata
*wmdata
= bat_ps
->dev
->parent
->platform_data
;
60 struct wm97xx_batt_pdata
*pdata
= wmdata
->batt_pdata
;
63 case POWER_SUPPLY_PROP_STATUS
:
64 val
->intval
= bat_status
;
66 case POWER_SUPPLY_PROP_TECHNOLOGY
:
67 val
->intval
= pdata
->batt_tech
;
69 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
70 if (pdata
->batt_aux
>= 0)
71 val
->intval
= wm97xx_read_bat(bat_ps
);
75 case POWER_SUPPLY_PROP_TEMP
:
76 if (pdata
->temp_aux
>= 0)
77 val
->intval
= wm97xx_read_temp(bat_ps
);
81 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
82 if (pdata
->max_voltage
>= 0)
83 val
->intval
= pdata
->max_voltage
;
87 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
88 if (pdata
->min_voltage
>= 0)
89 val
->intval
= pdata
->min_voltage
;
93 case POWER_SUPPLY_PROP_PRESENT
:
102 static void wm97xx_bat_external_power_changed(struct power_supply
*bat_ps
)
104 schedule_work(&bat_work
);
107 static void wm97xx_bat_update(struct power_supply
*bat_ps
)
109 int old_status
= bat_status
;
110 struct wm97xx_pdata
*wmdata
= bat_ps
->dev
->parent
->platform_data
;
111 struct wm97xx_batt_pdata
*pdata
= wmdata
->batt_pdata
;
113 mutex_lock(&work_lock
);
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
;
121 if (old_status
!= bat_status
) {
122 pr_debug("%s: %i -> %i\n", bat_ps
->name
, old_status
,
124 power_supply_changed(bat_ps
);
127 mutex_unlock(&work_lock
);
130 static 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
,
137 static void wm97xx_bat_work(struct work_struct
*work
)
139 wm97xx_bat_update(&bat_ps
);
142 static irqreturn_t
wm97xx_chrg_irq(int irq
, void *data
)
144 schedule_work(&bat_work
);
149 static int wm97xx_bat_suspend(struct device
*dev
)
151 flush_scheduled_work();
155 static int wm97xx_bat_resume(struct device
*dev
)
157 schedule_work(&bat_work
);
161 static const struct dev_pm_ops wm97xx_bat_pm_ops
= {
162 .suspend
= wm97xx_bat_suspend
,
163 .resume
= wm97xx_bat_resume
,
167 static int __devinit
wm97xx_bat_probe(struct platform_device
*dev
)
170 int props
= 1; /* POWER_SUPPLY_PROP_PRESENT */
172 struct wm97xx_pdata
*wmdata
= dev
->dev
.platform_data
;
173 struct wm97xx_batt_pdata
*pdata
;
176 dev_err(&dev
->dev
, "Do not pass platform_data through "
177 "wm97xx_bat_set_pdata!\n");
182 dev_err(&dev
->dev
, "No platform data supplied\n");
186 pdata
= wmdata
->batt_pdata
;
191 mutex_init(&work_lock
);
194 dev_err(&dev
->dev
, "No platform_data supplied\n");
198 if (gpio_is_valid(pdata
->charge_gpio
)) {
199 ret
= gpio_request(pdata
->charge_gpio
, "BATT CHRG");
202 ret
= gpio_direction_input(pdata
->charge_gpio
);
205 ret
= request_irq(gpio_to_irq(pdata
->charge_gpio
),
206 wm97xx_chrg_irq
, IRQF_DISABLED
,
210 props
++; /* POWER_SUPPLY_PROP_STATUS */
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 */
224 prop
= kzalloc(props
* sizeof(*prop
), GFP_KERNEL
);
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
;
242 INIT_WORK(&bat_work
, wm97xx_bat_work
);
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";
250 bat_ps
.name
= pdata
->batt_name
;
252 bat_ps
.properties
= prop
;
253 bat_ps
.num_properties
= props
;
255 ret
= power_supply_register(&dev
->dev
, &bat_ps
);
257 schedule_work(&bat_work
);
265 if (gpio_is_valid(pdata
->charge_gpio
))
266 free_irq(gpio_to_irq(pdata
->charge_gpio
), dev
);
268 if (gpio_is_valid(pdata
->charge_gpio
))
269 gpio_free(pdata
->charge_gpio
);
274 static int __devexit
wm97xx_bat_remove(struct platform_device
*dev
)
276 struct wm97xx_pdata
*wmdata
= dev
->dev
.platform_data
;
277 struct wm97xx_batt_pdata
*pdata
= wmdata
->batt_pdata
;
279 if (pdata
&& gpio_is_valid(pdata
->charge_gpio
)) {
280 free_irq(gpio_to_irq(pdata
->charge_gpio
), dev
);
281 gpio_free(pdata
->charge_gpio
);
283 flush_scheduled_work();
284 power_supply_unregister(&bat_ps
);
289 static struct platform_driver wm97xx_bat_driver
= {
291 .name
= "wm97xx-battery",
292 .owner
= THIS_MODULE
,
294 .pm
= &wm97xx_bat_pm_ops
,
297 .probe
= wm97xx_bat_probe
,
298 .remove
= __devexit_p(wm97xx_bat_remove
),
301 static int __init
wm97xx_bat_init(void)
303 return platform_driver_register(&wm97xx_bat_driver
);
306 static void __exit
wm97xx_bat_exit(void)
308 platform_driver_unregister(&wm97xx_bat_driver
);
311 void wm97xx_bat_set_pdata(struct wm97xx_batt_info
*data
)
315 EXPORT_SYMBOL_GPL(wm97xx_bat_set_pdata
);
317 module_init(wm97xx_bat_init
);
318 module_exit(wm97xx_bat_exit
);
320 MODULE_LICENSE("GPL");
321 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
322 MODULE_DESCRIPTION("WM97xx battery driver");