2 * Backup battery driver for Wolfson Microelectronics wm831x PMICs
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/slab.h>
17 #include <linux/mfd/wm831x/core.h>
18 #include <linux/mfd/wm831x/auxadc.h>
19 #include <linux/mfd/wm831x/pmu.h>
20 #include <linux/mfd/wm831x/pdata.h>
22 struct wm831x_backup
{
23 struct wm831x
*wm831x
;
24 struct power_supply backup
;
28 static int wm831x_backup_read_voltage(struct wm831x
*wm831x
,
29 enum wm831x_auxadc src
,
30 union power_supply_propval
*val
)
34 ret
= wm831x_auxadc_read_uv(wm831x
, src
);
41 /*********************************************************************
42 * Backup supply properties
43 *********************************************************************/
45 static void wm831x_config_backup(struct wm831x
*wm831x
)
47 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
48 struct wm831x_backup_pdata
*pdata
;
51 if (!wm831x_pdata
|| !wm831x_pdata
->backup
) {
53 "No backup battery charger configuration\n");
57 pdata
= wm831x_pdata
->backup
;
61 if (pdata
->charger_enable
)
62 reg
|= WM831X_BKUP_CHG_ENA
| WM831X_BKUP_BATT_DET_ENA
;
63 if (pdata
->no_constant_voltage
)
64 reg
|= WM831X_BKUP_CHG_MODE
;
66 switch (pdata
->vlim
) {
70 reg
|= WM831X_BKUP_CHG_VLIM
;
73 dev_err(wm831x
->dev
, "Invalid backup voltage limit %dmV\n",
77 switch (pdata
->ilim
) {
90 dev_err(wm831x
->dev
, "Invalid backup current limit %duA\n",
94 ret
= wm831x_reg_unlock(wm831x
);
96 dev_err(wm831x
->dev
, "Failed to unlock registers: %d\n", ret
);
100 ret
= wm831x_set_bits(wm831x
, WM831X_BACKUP_CHARGER_CONTROL
,
101 WM831X_BKUP_CHG_ENA_MASK
|
102 WM831X_BKUP_CHG_MODE_MASK
|
103 WM831X_BKUP_BATT_DET_ENA_MASK
|
104 WM831X_BKUP_CHG_VLIM_MASK
|
105 WM831X_BKUP_CHG_ILIM_MASK
,
109 "Failed to set backup charger config: %d\n", ret
);
111 wm831x_reg_lock(wm831x
);
114 static int wm831x_backup_get_prop(struct power_supply
*psy
,
115 enum power_supply_property psp
,
116 union power_supply_propval
*val
)
118 struct wm831x_backup
*devdata
= dev_get_drvdata(psy
->dev
->parent
);
119 struct wm831x
*wm831x
= devdata
->wm831x
;
122 ret
= wm831x_reg_read(wm831x
, WM831X_BACKUP_CHARGER_CONTROL
);
127 case POWER_SUPPLY_PROP_STATUS
:
128 if (ret
& WM831X_BKUP_CHG_STS
)
129 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
131 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
134 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
135 ret
= wm831x_backup_read_voltage(wm831x
, WM831X_AUX_BKUP_BATT
,
139 case POWER_SUPPLY_PROP_PRESENT
:
140 if (ret
& WM831X_BKUP_CHG_STS
)
154 static enum power_supply_property wm831x_backup_props
[] = {
155 POWER_SUPPLY_PROP_STATUS
,
156 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
157 POWER_SUPPLY_PROP_PRESENT
,
160 /*********************************************************************
162 *********************************************************************/
164 static __devinit
int wm831x_backup_probe(struct platform_device
*pdev
)
166 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
167 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
168 struct wm831x_backup
*devdata
;
169 struct power_supply
*backup
;
172 devdata
= kzalloc(sizeof(struct wm831x_backup
), GFP_KERNEL
);
176 devdata
->wm831x
= wm831x
;
177 platform_set_drvdata(pdev
, devdata
);
179 backup
= &devdata
->backup
;
181 /* We ignore configuration failures since we can still read
182 * back the status without enabling the charger (which may
183 * already be enabled anyway).
185 wm831x_config_backup(wm831x
);
187 if (wm831x_pdata
&& wm831x_pdata
->wm831x_num
)
188 snprintf(devdata
->name
, sizeof(devdata
->name
),
189 "wm831x-backup.%d", wm831x_pdata
->wm831x_num
);
191 snprintf(devdata
->name
, sizeof(devdata
->name
),
194 backup
->name
= devdata
->name
;
195 backup
->type
= POWER_SUPPLY_TYPE_BATTERY
;
196 backup
->properties
= wm831x_backup_props
;
197 backup
->num_properties
= ARRAY_SIZE(wm831x_backup_props
);
198 backup
->get_property
= wm831x_backup_get_prop
;
199 ret
= power_supply_register(&pdev
->dev
, backup
);
210 static __devexit
int wm831x_backup_remove(struct platform_device
*pdev
)
212 struct wm831x_backup
*devdata
= platform_get_drvdata(pdev
);
214 power_supply_unregister(&devdata
->backup
);
215 kfree(devdata
->backup
.name
);
221 static struct platform_driver wm831x_backup_driver
= {
222 .probe
= wm831x_backup_probe
,
223 .remove
= __devexit_p(wm831x_backup_remove
),
225 .name
= "wm831x-backup",
229 static int __init
wm831x_backup_init(void)
231 return platform_driver_register(&wm831x_backup_driver
);
233 module_init(wm831x_backup_init
);
235 static void __exit
wm831x_backup_exit(void)
237 platform_driver_unregister(&wm831x_backup_driver
);
239 module_exit(wm831x_backup_exit
);
241 MODULE_DESCRIPTION("Backup battery charger driver for WM831x PMICs");
242 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
243 MODULE_LICENSE("GPL");
244 MODULE_ALIAS("platform:wm831x-backup");