Debian Codegraph @

drivers/power/supply/wm97xx_battery.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Battery measurement code for WM97xx
4 *
5 * based on tosa_battery.c
6 *
7 * Copyright (C) 2008 Marek Vasut <[email protected]>
8 */
9
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/wm97xx.h>
16 #include <linux/spinlock.h>
17 #include <linux/interrupt.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/irq.h>
20 #include <linux/slab.h>
21
22 static struct work_struct bat_work;
23 static struct gpio_desc *charge_gpiod;
24 static DEFINE_MUTEX(work_lock);
25 static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
26 static enum power_supply_property *prop;
27
28 static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
29 {
30 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
31
32 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
33 pdata->batt_aux) * pdata->batt_mult /
34 pdata->batt_div;
35 }
36
37 static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
38 {
39 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
40
41 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
42 pdata->temp_aux) * pdata->temp_mult /
43 pdata->temp_div;
44 }
45
46 static int wm97xx_bat_get_property(struct power_supply *bat_ps,
47 enum power_supply_property psp,
48 union power_supply_propval *val)
49 {
50 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
51
52 switch (psp) {
53 case POWER_SUPPLY_PROP_STATUS:
54 val->intval = bat_status;
55 break;
56 case POWER_SUPPLY_PROP_TECHNOLOGY:
57 val->intval = pdata->batt_tech;
58 break;
59 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
60 if (pdata->batt_aux >= 0)
61 val->intval = wm97xx_read_bat(bat_ps);
62 else
63 return -EINVAL;
64 break;
65 case POWER_SUPPLY_PROP_TEMP:
66 if (pdata->temp_aux >= 0)
67 val->intval = wm97xx_read_temp(bat_ps);
68 else
69 return -EINVAL;
70 break;
71 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
72 if (pdata->max_voltage >= 0)
73 val->intval = pdata->max_voltage;
74 else
75 return -EINVAL;
76 break;
77 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
78 if (pdata->min_voltage >= 0)
79 val->intval = pdata->min_voltage;
80 else
81 return -EINVAL;
82 break;
83 case POWER_SUPPLY_PROP_PRESENT:
84 val->intval = 1;
85 break;
86 default:
87 return -EINVAL;
88 }
89 return 0;
90 }
91
92 static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
93 {
94 schedule_work(&bat_work);
95 }
96
97 static void wm97xx_bat_update(struct power_supply *bat_ps)
98 {
99 int old_status = bat_status;
100
101 mutex_lock(&work_lock);
102
103 bat_status = (charge_gpiod) ?
104 (gpiod_get_value(charge_gpiod) ?
105 POWER_SUPPLY_STATUS_DISCHARGING :
106 POWER_SUPPLY_STATUS_CHARGING) :
107 POWER_SUPPLY_STATUS_UNKNOWN;
108
109 if (old_status != bat_status) {
110 pr_debug("%s: %i -> %i\n", bat_ps->desc->name, old_status,
111 bat_status);
112 power_supply_changed(bat_ps);
113 }
114
115 mutex_unlock(&work_lock);
116 }
117
118 static struct power_supply *bat_psy;
119 static struct power_supply_desc bat_psy_desc = {
120 .type = POWER_SUPPLY_TYPE_BATTERY,
121 .get_property = wm97xx_bat_get_property,
122 .external_power_changed = wm97xx_bat_external_power_changed,
123 .use_for_apm = 1,
124 };
125
126 static void wm97xx_bat_work(struct work_struct *work)
127 {
128 wm97xx_bat_update(bat_psy);
129 }
130
131 static irqreturn_t wm97xx_chrg_irq(int irq, void *data)
132 {
133 schedule_work(&bat_work);
134 return IRQ_HANDLED;
135 }
136
137 #ifdef CONFIG_PM
138 static int wm97xx_bat_suspend(struct device *dev)
139 {
140 flush_work(&bat_work);
141 return 0;
142 }
143
144 static int wm97xx_bat_resume(struct device *dev)
145 {
146 schedule_work(&bat_work);
147 return 0;
148 }
149
150 static const struct dev_pm_ops wm97xx_bat_pm_ops = {
151 .suspend = wm97xx_bat_suspend,
152 .resume = wm97xx_bat_resume,
153 };
154 #endif
155
156 static int wm97xx_bat_probe(struct platform_device *dev)
157 {
158 int ret = 0;
159 int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
160 int i = 0;
161 struct wm97xx_batt_pdata *pdata = dev->dev.platform_data;
162 struct power_supply_config cfg = {};
163
164 if (!pdata) {
165 dev_err(&dev->dev, "No platform data supplied\n");
166 return -EINVAL;
167 }
168
169 cfg.drv_data = pdata;
170
171 if (dev->id != -1)
172 return -EINVAL;
173
174 charge_gpiod = devm_gpiod_get_optional(&dev->dev, NULL, GPIOD_IN);
175 if (IS_ERR(charge_gpiod))
176 return dev_err_probe(&dev->dev,
177 PTR_ERR(charge_gpiod),
178 "failed to get charge GPIO\n");
179 if (charge_gpiod) {
180 gpiod_set_consumer_name(charge_gpiod, "BATT CHRG");
181 props++; /* POWER_SUPPLY_PROP_STATUS */
182 }
183
184 if (pdata->batt_tech >= 0)
185 props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
186 if (pdata->temp_aux >= 0)
187 props++; /* POWER_SUPPLY_PROP_TEMP */
188 if (pdata->batt_aux >= 0)
189 props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
190 if (pdata->max_voltage >= 0)
191 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
192 if (pdata->min_voltage >= 0)
193 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
194
195 prop = devm_kcalloc(&dev->dev, props, sizeof(*prop), GFP_KERNEL);
196 if (!prop)
197 return -ENOMEM;
198
199 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
200 if (charge_gpiod)
201 prop[i++] = POWER_SUPPLY_PROP_STATUS;
202 if (pdata->batt_tech >= 0)
203 prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
204 if (pdata->temp_aux >= 0)
205 prop[i++] = POWER_SUPPLY_PROP_TEMP;
206 if (pdata->batt_aux >= 0)
207 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
208 if (pdata->max_voltage >= 0)
209 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
210 if (pdata->min_voltage >= 0)
211 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
212
213 INIT_WORK(&bat_work, wm97xx_bat_work);
214
215 if (!pdata->batt_name) {
216 dev_info(&dev->dev, "Please consider setting proper battery "
217 "name in platform definition file, falling "
218 "back to name \"wm97xx-batt\"\n");
219 bat_psy_desc.name = "wm97xx-batt";
220 } else
221 bat_psy_desc.name = pdata->batt_name;
222
223 bat_psy_desc.properties = prop;
224 bat_psy_desc.num_properties = props;
225
226 bat_psy = devm_power_supply_register(&dev->dev, &bat_psy_desc, &cfg);
227 if (IS_ERR(bat_psy))
228 return PTR_ERR(bat_psy);
229
230 schedule_work(&bat_work);
231
232 if (charge_gpiod) {
233 ret = request_irq(gpiod_to_irq(charge_gpiod), wm97xx_chrg_irq,
234 0, "AC Detect", dev);
235 if (ret)
236 return dev_err_probe(&dev->dev, ret,
237 "failed to request GPIO irq\n");
238 }
239
240 return 0;
241 }
242
243 static void wm97xx_bat_remove(struct platform_device *dev)
244 {
245 if (charge_gpiod)
246 free_irq(gpiod_to_irq(charge_gpiod), dev);
247 cancel_work_sync(&bat_work);
248 }
249
250 static struct platform_driver wm97xx_bat_driver = {
251 .driver = {
252 .name = "wm97xx-battery",
253 #ifdef CONFIG_PM
254 .pm = &wm97xx_bat_pm_ops,
255 #endif
256 },
257 .probe = wm97xx_bat_probe,
258 .remove = wm97xx_bat_remove,
259 };
260
261 module_platform_driver(wm97xx_bat_driver);
262
263 MODULE_AUTHOR("Marek Vasut <[email protected]>");
264 MODULE_DESCRIPTION("WM97xx battery driver");