1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery measurement code for WM97xx
5 * based on tosa_battery.c
7 * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
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>
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;
28 static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
30 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
32 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
33 pdata->batt_aux) * pdata->batt_mult /
37 static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
39 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
41 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
42 pdata->temp_aux) * pdata->temp_mult /
46 static int wm97xx_bat_get_property(struct power_supply *bat_ps,
47 enum power_supply_property psp,
48 union power_supply_propval *val)
50 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
53 case POWER_SUPPLY_PROP_STATUS:
54 val->intval = bat_status;
56 case POWER_SUPPLY_PROP_TECHNOLOGY:
57 val->intval = pdata->batt_tech;
59 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
60 if (pdata->batt_aux >= 0)
61 val->intval = wm97xx_read_bat(bat_ps);
65 case POWER_SUPPLY_PROP_TEMP:
66 if (pdata->temp_aux >= 0)
67 val->intval = wm97xx_read_temp(bat_ps);
71 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
72 if (pdata->max_voltage >= 0)
73 val->intval = pdata->max_voltage;
77 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
78 if (pdata->min_voltage >= 0)
79 val->intval = pdata->min_voltage;
83 case POWER_SUPPLY_PROP_PRESENT:
92 static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
94 schedule_work(&bat_work);
97 static void wm97xx_bat_update(struct power_supply *bat_ps)
99 int old_status = bat_status;
101 mutex_lock(&work_lock);
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;
109 if (old_status != bat_status) {
110 pr_debug("%s: %i -> %i\n", bat_ps->desc->name, old_status,
112 power_supply_changed(bat_ps);
115 mutex_unlock(&work_lock);
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,
126 static void wm97xx_bat_work(struct work_struct *work)
128 wm97xx_bat_update(bat_psy);
131 static irqreturn_t wm97xx_chrg_irq(int irq, void *data)
133 schedule_work(&bat_work);
138 static int wm97xx_bat_suspend(struct device *dev)
140 flush_work(&bat_work);
144 static int wm97xx_bat_resume(struct device *dev)
146 schedule_work(&bat_work);
150 static const struct dev_pm_ops wm97xx_bat_pm_ops = {
151 .suspend = wm97xx_bat_suspend,
152 .resume = wm97xx_bat_resume,
156 static int wm97xx_bat_probe(struct platform_device *dev)
159 int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
161 struct wm97xx_batt_pdata *pdata = dev->dev.platform_data;
162 struct power_supply_config cfg = {};
165 dev_err(&dev->dev, "No platform data supplied\n");
169 cfg.drv_data = pdata;
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");
180 gpiod_set_consumer_name(charge_gpiod, "BATT CHRG");
181 ret = request_irq(gpiod_to_irq(charge_gpiod),
185 return dev_err_probe(&dev->dev, ret,
186 "failed to request GPIO irq\n");
187 props++; /* POWER_SUPPLY_PROP_STATUS */
190 if (pdata->batt_tech >= 0)
191 props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
192 if (pdata->temp_aux >= 0)
193 props++; /* POWER_SUPPLY_PROP_TEMP */
194 if (pdata->batt_aux >= 0)
195 props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
196 if (pdata->max_voltage >= 0)
197 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
198 if (pdata->min_voltage >= 0)
199 props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
201 prop = kcalloc(props, sizeof(*prop), GFP_KERNEL);
207 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
209 prop[i++] = POWER_SUPPLY_PROP_STATUS;
210 if (pdata->batt_tech >= 0)
211 prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
212 if (pdata->temp_aux >= 0)
213 prop[i++] = POWER_SUPPLY_PROP_TEMP;
214 if (pdata->batt_aux >= 0)
215 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
216 if (pdata->max_voltage >= 0)
217 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
218 if (pdata->min_voltage >= 0)
219 prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
221 INIT_WORK(&bat_work, wm97xx_bat_work);
223 if (!pdata->batt_name) {
224 dev_info(&dev->dev, "Please consider setting proper battery "
225 "name in platform definition file, falling "
226 "back to name \"wm97xx-batt\"\n");
227 bat_psy_desc.name = "wm97xx-batt";
229 bat_psy_desc.name = pdata->batt_name;
231 bat_psy_desc.properties = prop;
232 bat_psy_desc.num_properties = props;
234 bat_psy = power_supply_register(&dev->dev, &bat_psy_desc, &cfg);
235 if (!IS_ERR(bat_psy)) {
236 schedule_work(&bat_work);
238 ret = PTR_ERR(bat_psy);
247 free_irq(gpiod_to_irq(charge_gpiod), dev);
251 static int wm97xx_bat_remove(struct platform_device *dev)
254 free_irq(gpiod_to_irq(charge_gpiod), dev);
255 cancel_work_sync(&bat_work);
256 power_supply_unregister(bat_psy);
261 static struct platform_driver wm97xx_bat_driver = {
263 .name = "wm97xx-battery",
265 .pm = &wm97xx_bat_pm_ops,
268 .probe = wm97xx_bat_probe,
269 .remove = wm97xx_bat_remove,
272 module_platform_driver(wm97xx_bat_driver);
274 MODULE_LICENSE("GPL");
275 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
276 MODULE_DESCRIPTION("WM97xx battery driver");