blob: ffb265b8526d26286a8990879495f7ed016fdf69 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mark Brownc26964e2009-10-01 15:41:06 +01002/*
3 * Backup battery driver for Wolfson Microelectronics wm831x PMICs
4 *
5 * Copyright 2009 Wolfson Microelectronics PLC.
Mark Brownc26964e2009-10-01 15:41:06 +01006 */
7
8#include <linux/module.h>
9#include <linux/err.h>
10#include <linux/platform_device.h>
11#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Mark Brownc26964e2009-10-01 15:41:06 +010013
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>
18
19struct wm831x_backup {
20 struct wm831x *wm831x;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010021 struct power_supply *backup;
22 struct power_supply_desc backup_desc;
Mark Brownd0376032011-05-31 13:30:10 +010023 char name[20];
Mark Brownc26964e2009-10-01 15:41:06 +010024};
25
26static int wm831x_backup_read_voltage(struct wm831x *wm831x,
27 enum wm831x_auxadc src,
28 union power_supply_propval *val)
29{
30 int ret;
31
32 ret = wm831x_auxadc_read_uv(wm831x, src);
33 if (ret >= 0)
34 val->intval = ret;
35
36 return ret;
37}
38
39/*********************************************************************
40 * Backup supply properties
41 *********************************************************************/
42
43static void wm831x_config_backup(struct wm831x *wm831x)
44{
45 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
46 struct wm831x_backup_pdata *pdata;
47 int ret, reg;
48
49 if (!wm831x_pdata || !wm831x_pdata->backup) {
50 dev_warn(wm831x->dev,
51 "No backup battery charger configuration\n");
52 return;
53 }
54
55 pdata = wm831x_pdata->backup;
56
57 reg = 0;
58
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;
63
64 switch (pdata->vlim) {
65 case 2500:
66 break;
67 case 3100:
68 reg |= WM831X_BKUP_CHG_VLIM;
69 break;
70 default:
71 dev_err(wm831x->dev, "Invalid backup voltage limit %dmV\n",
72 pdata->vlim);
73 }
74
75 switch (pdata->ilim) {
76 case 100:
77 break;
78 case 200:
79 reg |= 1;
80 break;
81 case 300:
82 reg |= 2;
83 break;
84 case 400:
85 reg |= 3;
86 break;
87 default:
88 dev_err(wm831x->dev, "Invalid backup current limit %duA\n",
89 pdata->ilim);
90 }
91
92 ret = wm831x_reg_unlock(wm831x);
93 if (ret != 0) {
94 dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
95 return;
96 }
97
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,
104 reg);
105 if (ret != 0)
106 dev_err(wm831x->dev,
107 "Failed to set backup charger config: %d\n", ret);
108
109 wm831x_reg_lock(wm831x);
110}
111
112static int wm831x_backup_get_prop(struct power_supply *psy,
113 enum power_supply_property psp,
114 union power_supply_propval *val)
115{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100116 struct wm831x_backup *devdata = dev_get_drvdata(psy->dev.parent);
Mark Brownc26964e2009-10-01 15:41:06 +0100117 struct wm831x *wm831x = devdata->wm831x;
118 int ret = 0;
119
120 ret = wm831x_reg_read(wm831x, WM831X_BACKUP_CHARGER_CONTROL);
121 if (ret < 0)
122 return ret;
123
124 switch (psp) {
125 case POWER_SUPPLY_PROP_STATUS:
126 if (ret & WM831X_BKUP_CHG_STS)
127 val->intval = POWER_SUPPLY_STATUS_CHARGING;
128 else
129 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
130 break;
131
132 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
133 ret = wm831x_backup_read_voltage(wm831x, WM831X_AUX_BKUP_BATT,
134 val);
135 break;
136
137 case POWER_SUPPLY_PROP_PRESENT:
138 if (ret & WM831X_BKUP_CHG_STS)
139 val->intval = 1;
140 else
141 val->intval = 0;
142 break;
143
144 default:
145 ret = -EINVAL;
146 break;
147 }
148
149 return ret;
150}
151
152static enum power_supply_property wm831x_backup_props[] = {
153 POWER_SUPPLY_PROP_STATUS,
154 POWER_SUPPLY_PROP_VOLTAGE_NOW,
155 POWER_SUPPLY_PROP_PRESENT,
156};
157
158/*********************************************************************
159 * Initialisation
160 *********************************************************************/
161
Bill Pembertonc8afa642012-11-19 13:22:23 -0500162static int wm831x_backup_probe(struct platform_device *pdev)
Mark Brownc26964e2009-10-01 15:41:06 +0100163{
164 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Mark Brownd0376032011-05-31 13:30:10 +0100165 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
Mark Brownc26964e2009-10-01 15:41:06 +0100166 struct wm831x_backup *devdata;
Mark Brownc26964e2009-10-01 15:41:06 +0100167
Jingoo Hanf11d0182013-03-11 15:36:19 +0900168 devdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_backup),
169 GFP_KERNEL);
Mark Brownc26964e2009-10-01 15:41:06 +0100170 if (devdata == NULL)
171 return -ENOMEM;
172
173 devdata->wm831x = wm831x;
174 platform_set_drvdata(pdev, devdata);
175
Mark Brownc26964e2009-10-01 15:41:06 +0100176 /* We ignore configuration failures since we can still read
177 * back the status without enabling the charger (which may
178 * already be enabled anyway).
179 */
180 wm831x_config_backup(wm831x);
181
Mark Brownd0376032011-05-31 13:30:10 +0100182 if (wm831x_pdata && wm831x_pdata->wm831x_num)
183 snprintf(devdata->name, sizeof(devdata->name),
184 "wm831x-backup.%d", wm831x_pdata->wm831x_num);
185 else
186 snprintf(devdata->name, sizeof(devdata->name),
187 "wm831x-backup");
188
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100189 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);
Mark Brownc26964e2009-10-01 15:41:06 +0100196
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100197 return PTR_ERR_OR_ZERO(devdata->backup);
Mark Brownc26964e2009-10-01 15:41:06 +0100198}
199
Bill Pemberton415ec692012-11-19 13:26:07 -0500200static int wm831x_backup_remove(struct platform_device *pdev)
Mark Brownc26964e2009-10-01 15:41:06 +0100201{
202 struct wm831x_backup *devdata = platform_get_drvdata(pdev);
203
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100204 power_supply_unregister(devdata->backup);
Mark Brownc26964e2009-10-01 15:41:06 +0100205
206 return 0;
207}
208
209static struct platform_driver wm831x_backup_driver = {
210 .probe = wm831x_backup_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500211 .remove = wm831x_backup_remove,
Mark Brownc26964e2009-10-01 15:41:06 +0100212 .driver = {
213 .name = "wm831x-backup",
214 },
215};
216
Axel Lin300bac72011-11-26 12:01:10 +0800217module_platform_driver(wm831x_backup_driver);
Mark Brownc26964e2009-10-01 15:41:06 +0100218
219MODULE_DESCRIPTION("Backup battery charger driver for WM831x PMICs");
220MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
221MODULE_LICENSE("GPL");
222MODULE_ALIAS("platform:wm831x-backup");