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