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