1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backup battery driver for Wolfson Microelectronics wm831x PMICs
5 * Copyright 2009 Wolfson Microelectronics PLC.
8 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/power_supply.h>
12 #include <linux/slab.h>
14 #include <linux/mfd/wm831x/core.h>
15 #include <linux/mfd/wm831x/auxadc.h>
16 #include <linux/mfd/wm831x/pmu.h>
17 #include <linux/mfd/wm831x/pdata.h>
19 struct wm831x_backup
{
20 struct wm831x
*wm831x
;
21 struct power_supply
*backup
;
22 struct power_supply_desc backup_desc
;
26 static int wm831x_backup_read_voltage(struct wm831x
*wm831x
,
27 enum wm831x_auxadc src
,
28 union power_supply_propval
*val
)
32 ret
= wm831x_auxadc_read_uv(wm831x
, src
);
39 /*********************************************************************
40 * Backup supply properties
41 *********************************************************************/
43 static void wm831x_config_backup(struct wm831x
*wm831x
)
45 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
46 struct wm831x_backup_pdata
*pdata
;
49 if (!wm831x_pdata
|| !wm831x_pdata
->backup
) {
51 "No backup battery charger configuration\n");
55 pdata
= wm831x_pdata
->backup
;
59 if (pdata
->charger_enable
)
60 reg
|= WM831X_BKUP_CHG_ENA
| WM831X_BKUP_BATT_DET_ENA
;
61 if (pdata
->no_constant_voltage
)
62 reg
|= WM831X_BKUP_CHG_MODE
;
64 switch (pdata
->vlim
) {
68 reg
|= WM831X_BKUP_CHG_VLIM
;
71 dev_err(wm831x
->dev
, "Invalid backup voltage limit %dmV\n",
75 switch (pdata
->ilim
) {
88 dev_err(wm831x
->dev
, "Invalid backup current limit %duA\n",
92 ret
= wm831x_reg_unlock(wm831x
);
94 dev_err(wm831x
->dev
, "Failed to unlock registers: %d\n", ret
);
98 ret
= wm831x_set_bits(wm831x
, WM831X_BACKUP_CHARGER_CONTROL
,
99 WM831X_BKUP_CHG_ENA_MASK
|
100 WM831X_BKUP_CHG_MODE_MASK
|
101 WM831X_BKUP_BATT_DET_ENA_MASK
|
102 WM831X_BKUP_CHG_VLIM_MASK
|
103 WM831X_BKUP_CHG_ILIM_MASK
,
107 "Failed to set backup charger config: %d\n", ret
);
109 wm831x_reg_lock(wm831x
);
112 static int wm831x_backup_get_prop(struct power_supply
*psy
,
113 enum power_supply_property psp
,
114 union power_supply_propval
*val
)
116 struct wm831x_backup
*devdata
= dev_get_drvdata(psy
->dev
.parent
);
117 struct wm831x
*wm831x
= devdata
->wm831x
;
120 ret
= wm831x_reg_read(wm831x
, WM831X_BACKUP_CHARGER_CONTROL
);
125 case POWER_SUPPLY_PROP_STATUS
:
126 if (ret
& WM831X_BKUP_CHG_STS
)
127 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
129 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
132 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
133 ret
= wm831x_backup_read_voltage(wm831x
, WM831X_AUX_BKUP_BATT
,
137 case POWER_SUPPLY_PROP_PRESENT
:
138 if (ret
& WM831X_BKUP_CHG_STS
)
152 static enum power_supply_property wm831x_backup_props
[] = {
153 POWER_SUPPLY_PROP_STATUS
,
154 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
155 POWER_SUPPLY_PROP_PRESENT
,
158 /*********************************************************************
160 *********************************************************************/
162 static int wm831x_backup_probe(struct platform_device
*pdev
)
164 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
165 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
166 struct wm831x_backup
*devdata
;
168 devdata
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_backup
),
173 devdata
->wm831x
= wm831x
;
174 platform_set_drvdata(pdev
, devdata
);
176 /* We ignore configuration failures since we can still read
177 * back the status without enabling the charger (which may
178 * already be enabled anyway).
180 wm831x_config_backup(wm831x
);
182 if (wm831x_pdata
&& wm831x_pdata
->wm831x_num
)
183 snprintf(devdata
->name
, sizeof(devdata
->name
),
184 "wm831x-backup.%d", wm831x_pdata
->wm831x_num
);
186 snprintf(devdata
->name
, sizeof(devdata
->name
),
189 devdata
->backup_desc
.name
= devdata
->name
;
190 devdata
->backup_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
191 devdata
->backup_desc
.properties
= wm831x_backup_props
;
192 devdata
->backup_desc
.num_properties
= ARRAY_SIZE(wm831x_backup_props
);
193 devdata
->backup_desc
.get_property
= wm831x_backup_get_prop
;
194 devdata
->backup
= power_supply_register(&pdev
->dev
,
195 &devdata
->backup_desc
, NULL
);
197 return PTR_ERR_OR_ZERO(devdata
->backup
);
200 static int wm831x_backup_remove(struct platform_device
*pdev
)
202 struct wm831x_backup
*devdata
= platform_get_drvdata(pdev
);
204 power_supply_unregister(devdata
->backup
);
209 static struct platform_driver wm831x_backup_driver
= {
210 .probe
= wm831x_backup_probe
,
211 .remove
= wm831x_backup_remove
,
213 .name
= "wm831x-backup",
217 module_platform_driver(wm831x_backup_driver
);
219 MODULE_DESCRIPTION("Backup battery charger driver for WM831x PMICs");
220 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
221 MODULE_LICENSE("GPL");
222 MODULE_ALIAS("platform:wm831x-backup");