wm97xx_battery.c (75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37) wm97xx_battery.c (cb6d6918c56ffd98e88164d5471f692d33dabf2b)
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 <marek.vasut@gmail.com>
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>
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 <marek.vasut@gmail.com>
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.h>
18#include <linux/gpio/consumer.h>
19#include <linux/irq.h>
20#include <linux/slab.h>
21
22static struct work_struct bat_work;
19#include <linux/irq.h>
20#include <linux/slab.h>
21
22static struct work_struct bat_work;
23static struct gpio_desc *charge_gpiod;
23static DEFINE_MUTEX(work_lock);
24static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
25static enum power_supply_property *prop;
26
27static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
28{
29 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
30

--- 60 unchanged lines hidden (view full) ---

91static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
92{
93 schedule_work(&bat_work);
94}
95
96static void wm97xx_bat_update(struct power_supply *bat_ps)
97{
98 int old_status = bat_status;
24static DEFINE_MUTEX(work_lock);
25static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
26static enum power_supply_property *prop;
27
28static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
29{
30 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
31

--- 60 unchanged lines hidden (view full) ---

92static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
93{
94 schedule_work(&bat_work);
95}
96
97static void wm97xx_bat_update(struct power_supply *bat_ps)
98{
99 int old_status = bat_status;
99 struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
100
101 mutex_lock(&work_lock);
102
100
101 mutex_lock(&work_lock);
102
103 bat_status = (pdata->charge_gpio >= 0) ?
104 (gpio_get_value(pdata->charge_gpio) ?
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);

--- 53 unchanged lines hidden (view full) ---

166 return -EINVAL;
167 }
168
169 cfg.drv_data = pdata;
170
171 if (dev->id != -1)
172 return -EINVAL;
173
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);

--- 53 unchanged lines hidden (view full) ---

166 return -EINVAL;
167 }
168
169 cfg.drv_data = pdata;
170
171 if (dev->id != -1)
172 return -EINVAL;
173
174 if (gpio_is_valid(pdata->charge_gpio)) {
175 ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
176 if (ret)
177 goto err;
178 ret = gpio_direction_input(pdata->charge_gpio);
179 if (ret)
180 goto err2;
181 ret = request_irq(gpio_to_irq(pdata->charge_gpio),
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 ret = request_irq(gpiod_to_irq(charge_gpiod),
182 wm97xx_chrg_irq, 0,
183 "AC Detect", dev);
184 if (ret)
182 wm97xx_chrg_irq, 0,
183 "AC Detect", dev);
184 if (ret)
185 goto err2;
185 return dev_err_probe(&dev->dev, ret,
186 "failed to request GPIO irq\n");
186 props++; /* POWER_SUPPLY_PROP_STATUS */
187 }
188
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)

--- 5 unchanged lines hidden (view full) ---

199
200 prop = kcalloc(props, sizeof(*prop), GFP_KERNEL);
201 if (!prop) {
202 ret = -ENOMEM;
203 goto err3;
204 }
205
206 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
187 props++; /* POWER_SUPPLY_PROP_STATUS */
188 }
189
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)

--- 5 unchanged lines hidden (view full) ---

200
201 prop = kcalloc(props, sizeof(*prop), GFP_KERNEL);
202 if (!prop) {
203 ret = -ENOMEM;
204 goto err3;
205 }
206
207 prop[i++] = POWER_SUPPLY_PROP_PRESENT;
207 if (pdata->charge_gpio >= 0)
208 if (charge_gpiod)
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)

--- 21 unchanged lines hidden (view full) ---

237 ret = PTR_ERR(bat_psy);
238 goto err4;
239 }
240
241 return 0;
242err4:
243 kfree(prop);
244err3:
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)

--- 21 unchanged lines hidden (view full) ---

238 ret = PTR_ERR(bat_psy);
239 goto err4;
240 }
241
242 return 0;
243err4:
244 kfree(prop);
245err3:
245 if (gpio_is_valid(pdata->charge_gpio))
246 free_irq(gpio_to_irq(pdata->charge_gpio), dev);
247err2:
248 if (gpio_is_valid(pdata->charge_gpio))
249 gpio_free(pdata->charge_gpio);
250err:
246 if (charge_gpiod)
247 free_irq(gpiod_to_irq(charge_gpiod), dev);
251 return ret;
252}
253
254static int wm97xx_bat_remove(struct platform_device *dev)
255{
248 return ret;
249}
250
251static int wm97xx_bat_remove(struct platform_device *dev)
252{
256 struct wm97xx_batt_pdata *pdata = dev->dev.platform_data;
257
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);
261 }
253 if (charge_gpiod)
254 free_irq(gpiod_to_irq(charge_gpiod), dev);
262 cancel_work_sync(&bat_work);
263 power_supply_unregister(bat_psy);
264 kfree(prop);
265 return 0;
266}
267
268static struct platform_driver wm97xx_bat_driver = {
269 .driver = {

--- 14 unchanged lines hidden ---
255 cancel_work_sync(&bat_work);
256 power_supply_unregister(bat_psy);
257 kfree(prop);
258 return 0;
259}
260
261static struct platform_driver wm97xx_bat_driver = {
262 .driver = {

--- 14 unchanged lines hidden ---