blob: 2e33109ca8c7b993247fb0f39a115254003dbbf7 [file] [log] [blame]
Mark Brownc26964e2009-10-01 15:41:06 +01001/*
2 * Backup battery driver for Wolfson Microelectronics wm831x PMICs
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
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.
9 */
10
11#include <linux/module.h>
12#include <linux/err.h>
13#include <linux/platform_device.h>
14#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Mark Brownc26964e2009-10-01 15:41:06 +010016
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>
21
22struct wm831x_backup {
23 struct wm831x *wm831x;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010024 struct power_supply *backup;
25 struct power_supply_desc backup_desc;
Mark Brownd0376032011-05-31 13:30:10 +010026 char name[20];
Mark Brownc26964e2009-10-01 15:41:06 +010027};
28
29static int wm831x_backup_read_voltage(struct wm831x *wm831x,
30 enum wm831x_auxadc src,
31 union power_supply_propval *val)
32{
33 int ret;
34
35 ret = wm831x_auxadc_read_uv(wm831x, src);
36 if (ret >= 0)
37 val->intval = ret;
38
39 return ret;
40}
41
42/*********************************************************************
43 * Backup supply properties
44 *********************************************************************/
45
46static void wm831x_config_backup(struct wm831x *wm831x)
47{
48 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
49 struct wm831x_backup_pdata *pdata;
50 int ret, reg;
51
52 if (!wm831x_pdata || !wm831x_pdata->backup) {
53 dev_warn(wm831x->dev,
54 "No backup battery charger configuration\n");
55 return;
56 }
57
58 pdata = wm831x_pdata->backup;
59
60 reg = 0;
61
62 if (pdata->charger_enable)
63 reg |= WM831X_BKUP_CHG_ENA | WM831X_BKUP_BATT_DET_ENA;
64 if (pdata->no_constant_voltage)
65 reg |= WM831X_BKUP_CHG_MODE;
66
67 switch (pdata->vlim) {
68 case 2500:
69 break;
70 case 3100:
71 reg |= WM831X_BKUP_CHG_VLIM;
72 break;
73 default:
74 dev_err(wm831x->dev, "Invalid backup voltage limit %dmV\n",
75 pdata->vlim);
76 }
77
78 switch (pdata->ilim) {
79 case 100:
80 break;
81 case 200:
82 reg |= 1;
83 break;
84 case 300:
85 reg |= 2;
86 break;
87 case 400:
88 reg |= 3;
89 break;
90 default:
91 dev_err(wm831x->dev, "Invalid backup current limit %duA\n",
92 pdata->ilim);
93 }
94
95 ret = wm831x_reg_unlock(wm831x);
96 if (ret != 0) {
97 dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
98 return;
99 }
100
101 ret = wm831x_set_bits(wm831x, WM831X_BACKUP_CHARGER_CONTROL,
102 WM831X_BKUP_CHG_ENA_MASK |
103 WM831X_BKUP_CHG_MODE_MASK |
104 WM831X_BKUP_BATT_DET_ENA_MASK |
105 WM831X_BKUP_CHG_VLIM_MASK |
106 WM831X_BKUP_CHG_ILIM_MASK,
107 reg);
108 if (ret != 0)
109 dev_err(wm831x->dev,
110 "Failed to set backup charger config: %d\n", ret);
111
112 wm831x_reg_lock(wm831x);
113}
114
115static int wm831x_backup_get_prop(struct power_supply *psy,
116 enum power_supply_property psp,
117 union power_supply_propval *val)
118{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100119 struct wm831x_backup *devdata = dev_get_drvdata(psy->dev.parent);
Mark Brownc26964e2009-10-01 15:41:06 +0100120 struct wm831x *wm831x = devdata->wm831x;
121 int ret = 0;
122
123 ret = wm831x_reg_read(wm831x, WM831X_BACKUP_CHARGER_CONTROL);
124 if (ret < 0)
125 return ret;
126
127 switch (psp) {
128 case POWER_SUPPLY_PROP_STATUS:
129 if (ret & WM831X_BKUP_CHG_STS)
130 val->intval = POWER_SUPPLY_STATUS_CHARGING;
131 else
132 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
133 break;
134
135 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
136 ret = wm831x_backup_read_voltage(wm831x, WM831X_AUX_BKUP_BATT,
137 val);
138 break;
139
140 case POWER_SUPPLY_PROP_PRESENT:
141 if (ret & WM831X_BKUP_CHG_STS)
142 val->intval = 1;
143 else
144 val->intval = 0;
145 break;
146
147 default:
148 ret = -EINVAL;
149 break;
150 }
151
152 return ret;
153}
154
155static enum power_supply_property wm831x_backup_props[] = {
156 POWER_SUPPLY_PROP_STATUS,
157 POWER_SUPPLY_PROP_VOLTAGE_NOW,
158 POWER_SUPPLY_PROP_PRESENT,
159};
160
161/*********************************************************************
162 * Initialisation
163 *********************************************************************/
164
Bill Pembertonc8afa642012-11-19 13:22:23 -0500165static int wm831x_backup_probe(struct platform_device *pdev)
Mark Brownc26964e2009-10-01 15:41:06 +0100166{
167 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Mark Brownd0376032011-05-31 13:30:10 +0100168 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
Mark Brownc26964e2009-10-01 15:41:06 +0100169 struct wm831x_backup *devdata;
Mark Brownc26964e2009-10-01 15:41:06 +0100170
Jingoo Hanf11d0182013-03-11 15:36:19 +0900171 devdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_backup),
172 GFP_KERNEL);
Mark Brownc26964e2009-10-01 15:41:06 +0100173 if (devdata == NULL)
174 return -ENOMEM;
175
176 devdata->wm831x = wm831x;
177 platform_set_drvdata(pdev, devdata);
178
Mark Brownc26964e2009-10-01 15:41:06 +0100179 /* We ignore configuration failures since we can still read
180 * back the status without enabling the charger (which may
181 * already be enabled anyway).
182 */
183 wm831x_config_backup(wm831x);
184
Mark Brownd0376032011-05-31 13:30:10 +0100185 if (wm831x_pdata && wm831x_pdata->wm831x_num)
186 snprintf(devdata->name, sizeof(devdata->name),
187 "wm831x-backup.%d", wm831x_pdata->wm831x_num);
188 else
189 snprintf(devdata->name, sizeof(devdata->name),
190 "wm831x-backup");
191
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100192 devdata->backup_desc.name = devdata->name;
193 devdata->backup_desc.type = POWER_SUPPLY_TYPE_BATTERY;
194 devdata->backup_desc.properties = wm831x_backup_props;
195 devdata->backup_desc.num_properties = ARRAY_SIZE(wm831x_backup_props);
196 devdata->backup_desc.get_property = wm831x_backup_get_prop;
197 devdata->backup = power_supply_register(&pdev->dev,
198 &devdata->backup_desc, NULL);
Mark Brownc26964e2009-10-01 15:41:06 +0100199
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100200 return PTR_ERR_OR_ZERO(devdata->backup);
Mark Brownc26964e2009-10-01 15:41:06 +0100201}
202
Bill Pemberton415ec692012-11-19 13:26:07 -0500203static int wm831x_backup_remove(struct platform_device *pdev)
Mark Brownc26964e2009-10-01 15:41:06 +0100204{
205 struct wm831x_backup *devdata = platform_get_drvdata(pdev);
206
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100207 power_supply_unregister(devdata->backup);
Mark Brownc26964e2009-10-01 15:41:06 +0100208
209 return 0;
210}
211
212static struct platform_driver wm831x_backup_driver = {
213 .probe = wm831x_backup_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500214 .remove = wm831x_backup_remove,
Mark Brownc26964e2009-10-01 15:41:06 +0100215 .driver = {
216 .name = "wm831x-backup",
217 },
218};
219
Axel Lin300bac72011-11-26 12:01:10 +0800220module_platform_driver(wm831x_backup_driver);
Mark Brownc26964e2009-10-01 15:41:06 +0100221
222MODULE_DESCRIPTION("Backup battery charger driver for WM831x PMICs");
223MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
224MODULE_LICENSE("GPL");
225MODULE_ALIAS("platform:wm831x-backup");