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