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.h>
19 #include <linux/irq.h>
20 #include <linux/slab.h>
22 static struct work_struct bat_work
;
23 static DEFINE_MUTEX(work_lock
);
24 static int bat_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
25 static enum power_supply_property
*prop
;
27 static unsigned long wm97xx_read_bat(struct power_supply
*bat_ps
)
29 struct wm97xx_batt_pdata
*pdata
= power_supply_get_drvdata(bat_ps
);
31 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps
->dev
.parent
),
32 pdata
->batt_aux
) * pdata
->batt_mult
/
36 static unsigned long wm97xx_read_temp(struct power_supply
*bat_ps
)
38 struct wm97xx_batt_pdata
*pdata
= power_supply_get_drvdata(bat_ps
);
40 return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps
->dev
.parent
),
41 pdata
->temp_aux
) * pdata
->temp_mult
/
45 static int wm97xx_bat_get_property(struct power_supply
*bat_ps
,
46 enum power_supply_property psp
,
47 union power_supply_propval
*val
)
49 struct wm97xx_batt_pdata
*pdata
= power_supply_get_drvdata(bat_ps
);
52 case POWER_SUPPLY_PROP_STATUS
:
53 val
->intval
= bat_status
;
55 case POWER_SUPPLY_PROP_TECHNOLOGY
:
56 val
->intval
= pdata
->batt_tech
;
58 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
59 if (pdata
->batt_aux
>= 0)
60 val
->intval
= wm97xx_read_bat(bat_ps
);
64 case POWER_SUPPLY_PROP_TEMP
:
65 if (pdata
->temp_aux
>= 0)
66 val
->intval
= wm97xx_read_temp(bat_ps
);
70 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
71 if (pdata
->max_voltage
>= 0)
72 val
->intval
= pdata
->max_voltage
;
76 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
77 if (pdata
->min_voltage
>= 0)
78 val
->intval
= pdata
->min_voltage
;
82 case POWER_SUPPLY_PROP_PRESENT
:
91 static void wm97xx_bat_external_power_changed(struct power_supply
*bat_ps
)
93 schedule_work(&bat_work
);
96 static void wm97xx_bat_update(struct power_supply
*bat_ps
)
98 int old_status
= bat_status
;
99 struct wm97xx_batt_pdata
*pdata
= power_supply_get_drvdata(bat_ps
);
101 mutex_lock(&work_lock
);
103 bat_status
= (pdata
->charge_gpio
>= 0) ?
104 (gpio_get_value(pdata
->charge_gpio
) ?
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 if (gpio_is_valid(pdata
->charge_gpio
)) {
175 ret
= gpio_request(pdata
->charge_gpio
, "BATT CHRG");
178 ret
= gpio_direction_input(pdata
->charge_gpio
);
181 ret
= request_irq(gpio_to_irq(pdata
->charge_gpio
),
186 props
++; /* POWER_SUPPLY_PROP_STATUS */
189 if (pdata
->batt_tech
>= 0)
190 props
++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
191 if (pdata
->temp_aux
>= 0)
192 props
++; /* POWER_SUPPLY_PROP_TEMP */
193 if (pdata
->batt_aux
>= 0)
194 props
++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
195 if (pdata
->max_voltage
>= 0)
196 props
++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
197 if (pdata
->min_voltage
>= 0)
198 props
++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
200 prop
= kcalloc(props
, sizeof(*prop
), GFP_KERNEL
);
206 prop
[i
++] = POWER_SUPPLY_PROP_PRESENT
;
207 if (pdata
->charge_gpio
>= 0)
208 prop
[i
++] = POWER_SUPPLY_PROP_STATUS
;
209 if (pdata
->batt_tech
>= 0)
210 prop
[i
++] = POWER_SUPPLY_PROP_TECHNOLOGY
;
211 if (pdata
->temp_aux
>= 0)
212 prop
[i
++] = POWER_SUPPLY_PROP_TEMP
;
213 if (pdata
->batt_aux
>= 0)
214 prop
[i
++] = POWER_SUPPLY_PROP_VOLTAGE_NOW
;
215 if (pdata
->max_voltage
>= 0)
216 prop
[i
++] = POWER_SUPPLY_PROP_VOLTAGE_MAX
;
217 if (pdata
->min_voltage
>= 0)
218 prop
[i
++] = POWER_SUPPLY_PROP_VOLTAGE_MIN
;
220 INIT_WORK(&bat_work
, wm97xx_bat_work
);
222 if (!pdata
->batt_name
) {
223 dev_info(&dev
->dev
, "Please consider setting proper battery "
224 "name in platform definition file, falling "
225 "back to name \"wm97xx-batt\"\n");
226 bat_psy_desc
.name
= "wm97xx-batt";
228 bat_psy_desc
.name
= pdata
->batt_name
;
230 bat_psy_desc
.properties
= prop
;
231 bat_psy_desc
.num_properties
= props
;
233 bat_psy
= power_supply_register(&dev
->dev
, &bat_psy_desc
, &cfg
);
234 if (!IS_ERR(bat_psy
)) {
235 schedule_work(&bat_work
);
237 ret
= PTR_ERR(bat_psy
);
245 if (gpio_is_valid(pdata
->charge_gpio
))
246 free_irq(gpio_to_irq(pdata
->charge_gpio
), dev
);
248 if (gpio_is_valid(pdata
->charge_gpio
))
249 gpio_free(pdata
->charge_gpio
);
254 static int wm97xx_bat_remove(struct platform_device
*dev
)
256 struct wm97xx_batt_pdata
*pdata
= dev
->dev
.platform_data
;
258 if (pdata
&& gpio_is_valid(pdata
->charge_gpio
)) {
259 free_irq(gpio_to_irq(pdata
->charge_gpio
), dev
);
260 gpio_free(pdata
->charge_gpio
);
262 cancel_work_sync(&bat_work
);
263 power_supply_unregister(bat_psy
);
268 static struct platform_driver wm97xx_bat_driver
= {
270 .name
= "wm97xx-battery",
272 .pm
= &wm97xx_bat_pm_ops
,
275 .probe
= wm97xx_bat_probe
,
276 .remove
= wm97xx_bat_remove
,
279 module_platform_driver(wm97xx_bat_driver
);
281 MODULE_LICENSE("GPL");
282 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
283 MODULE_DESCRIPTION("WM97xx battery driver");