Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PMU driver for Wolfson Microelectronics wm831x PMICs |
| 3 | * |
| 4 | * Copyright 2009 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 16 | |
| 17 | #include <linux/mfd/wm831x/core.h> |
| 18 | #include <linux/mfd/wm831x/auxadc.h> |
| 19 | #include <linux/mfd/wm831x/pmu.h> |
| 20 | #include <linux/mfd/wm831x/pdata.h> |
| 21 | |
| 22 | struct wm831x_power { |
| 23 | struct wm831x *wm831x; |
| 24 | struct power_supply wall; |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 25 | struct power_supply usb; |
| 26 | struct power_supply battery; |
| 27 | }; |
| 28 | |
| 29 | static int wm831x_power_check_online(struct wm831x *wm831x, int supply, |
| 30 | union power_supply_propval *val) |
| 31 | { |
| 32 | int ret; |
| 33 | |
| 34 | ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS); |
| 35 | if (ret < 0) |
| 36 | return ret; |
| 37 | |
| 38 | if (ret & supply) |
| 39 | val->intval = 1; |
| 40 | else |
| 41 | val->intval = 0; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int wm831x_power_read_voltage(struct wm831x *wm831x, |
| 47 | enum wm831x_auxadc src, |
| 48 | union power_supply_propval *val) |
| 49 | { |
| 50 | int ret; |
| 51 | |
| 52 | ret = wm831x_auxadc_read_uv(wm831x, src); |
| 53 | if (ret >= 0) |
| 54 | val->intval = ret; |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | /********************************************************************* |
| 60 | * WALL Power |
| 61 | *********************************************************************/ |
| 62 | static int wm831x_wall_get_prop(struct power_supply *psy, |
| 63 | enum power_supply_property psp, |
| 64 | union power_supply_propval *val) |
| 65 | { |
| 66 | struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent); |
| 67 | struct wm831x *wm831x = wm831x_power->wm831x; |
| 68 | int ret = 0; |
| 69 | |
| 70 | switch (psp) { |
| 71 | case POWER_SUPPLY_PROP_ONLINE: |
| 72 | ret = wm831x_power_check_online(wm831x, WM831X_PWR_WALL, val); |
| 73 | break; |
| 74 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 75 | ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_WALL, val); |
| 76 | break; |
| 77 | default: |
| 78 | ret = -EINVAL; |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | static enum power_supply_property wm831x_wall_props[] = { |
| 86 | POWER_SUPPLY_PROP_ONLINE, |
| 87 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 88 | }; |
| 89 | |
| 90 | /********************************************************************* |
| 91 | * USB Power |
| 92 | *********************************************************************/ |
| 93 | static int wm831x_usb_get_prop(struct power_supply *psy, |
| 94 | enum power_supply_property psp, |
| 95 | union power_supply_propval *val) |
| 96 | { |
| 97 | struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent); |
| 98 | struct wm831x *wm831x = wm831x_power->wm831x; |
| 99 | int ret = 0; |
| 100 | |
| 101 | switch (psp) { |
| 102 | case POWER_SUPPLY_PROP_ONLINE: |
| 103 | ret = wm831x_power_check_online(wm831x, WM831X_PWR_USB, val); |
| 104 | break; |
| 105 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 106 | ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_USB, val); |
| 107 | break; |
| 108 | default: |
| 109 | ret = -EINVAL; |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static enum power_supply_property wm831x_usb_props[] = { |
| 117 | POWER_SUPPLY_PROP_ONLINE, |
| 118 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 119 | }; |
| 120 | |
| 121 | /********************************************************************* |
| 122 | * Battery properties |
| 123 | *********************************************************************/ |
| 124 | |
| 125 | struct chg_map { |
| 126 | int val; |
| 127 | int reg_val; |
| 128 | }; |
| 129 | |
| 130 | static struct chg_map trickle_ilims[] = { |
| 131 | { 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT }, |
| 132 | { 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT }, |
| 133 | { 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT }, |
| 134 | { 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT }, |
| 135 | }; |
| 136 | |
| 137 | static struct chg_map vsels[] = { |
| 138 | { 4050, 0 << WM831X_CHG_VSEL_SHIFT }, |
| 139 | { 4100, 1 << WM831X_CHG_VSEL_SHIFT }, |
| 140 | { 4150, 2 << WM831X_CHG_VSEL_SHIFT }, |
| 141 | { 4200, 3 << WM831X_CHG_VSEL_SHIFT }, |
| 142 | }; |
| 143 | |
| 144 | static struct chg_map fast_ilims[] = { |
| 145 | { 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 146 | { 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 147 | { 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 148 | { 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 149 | { 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 150 | { 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 151 | { 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 152 | { 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 153 | { 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 154 | { 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 155 | { 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 156 | { 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 157 | { 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 158 | { 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 159 | { 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 160 | { 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT }, |
| 161 | }; |
| 162 | |
| 163 | static struct chg_map eoc_iterms[] = { |
| 164 | { 20, 0 << WM831X_CHG_ITERM_SHIFT }, |
| 165 | { 30, 1 << WM831X_CHG_ITERM_SHIFT }, |
| 166 | { 40, 2 << WM831X_CHG_ITERM_SHIFT }, |
| 167 | { 50, 3 << WM831X_CHG_ITERM_SHIFT }, |
| 168 | { 60, 4 << WM831X_CHG_ITERM_SHIFT }, |
| 169 | { 70, 5 << WM831X_CHG_ITERM_SHIFT }, |
| 170 | { 80, 6 << WM831X_CHG_ITERM_SHIFT }, |
| 171 | { 90, 7 << WM831X_CHG_ITERM_SHIFT }, |
| 172 | }; |
| 173 | |
| 174 | static struct chg_map chg_times[] = { |
| 175 | { 60, 0 << WM831X_CHG_TIME_SHIFT }, |
| 176 | { 90, 1 << WM831X_CHG_TIME_SHIFT }, |
| 177 | { 120, 2 << WM831X_CHG_TIME_SHIFT }, |
| 178 | { 150, 3 << WM831X_CHG_TIME_SHIFT }, |
| 179 | { 180, 4 << WM831X_CHG_TIME_SHIFT }, |
| 180 | { 210, 5 << WM831X_CHG_TIME_SHIFT }, |
| 181 | { 240, 6 << WM831X_CHG_TIME_SHIFT }, |
| 182 | { 270, 7 << WM831X_CHG_TIME_SHIFT }, |
| 183 | { 300, 8 << WM831X_CHG_TIME_SHIFT }, |
| 184 | { 330, 9 << WM831X_CHG_TIME_SHIFT }, |
| 185 | { 360, 10 << WM831X_CHG_TIME_SHIFT }, |
| 186 | { 390, 11 << WM831X_CHG_TIME_SHIFT }, |
| 187 | { 420, 12 << WM831X_CHG_TIME_SHIFT }, |
| 188 | { 450, 13 << WM831X_CHG_TIME_SHIFT }, |
| 189 | { 480, 14 << WM831X_CHG_TIME_SHIFT }, |
| 190 | { 510, 15 << WM831X_CHG_TIME_SHIFT }, |
| 191 | }; |
| 192 | |
| 193 | static void wm831x_battey_apply_config(struct wm831x *wm831x, |
| 194 | struct chg_map *map, int count, int val, |
| 195 | int *reg, const char *name, |
| 196 | const char *units) |
| 197 | { |
| 198 | int i; |
| 199 | |
| 200 | for (i = 0; i < count; i++) |
| 201 | if (val == map[i].val) |
| 202 | break; |
| 203 | if (i == count) { |
| 204 | dev_err(wm831x->dev, "Invalid %s %d%s\n", |
| 205 | name, val, units); |
| 206 | } else { |
| 207 | *reg |= map[i].reg_val; |
| 208 | dev_dbg(wm831x->dev, "Set %s of %d%s\n", name, val, units); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void wm831x_config_battery(struct wm831x *wm831x) |
| 213 | { |
| 214 | struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data; |
| 215 | struct wm831x_battery_pdata *pdata; |
| 216 | int ret, reg1, reg2; |
| 217 | |
| 218 | if (!wm831x_pdata || !wm831x_pdata->battery) { |
| 219 | dev_warn(wm831x->dev, |
| 220 | "No battery charger configuration\n"); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | pdata = wm831x_pdata->battery; |
| 225 | |
| 226 | reg1 = 0; |
| 227 | reg2 = 0; |
| 228 | |
| 229 | if (!pdata->enable) { |
| 230 | dev_info(wm831x->dev, "Battery charger disabled\n"); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | reg1 |= WM831X_CHG_ENA; |
| 235 | if (pdata->off_mask) |
| 236 | reg2 |= WM831X_CHG_OFF_MSK; |
| 237 | if (pdata->fast_enable) |
| 238 | reg1 |= WM831X_CHG_FAST; |
| 239 | |
| 240 | wm831x_battey_apply_config(wm831x, trickle_ilims, |
| 241 | ARRAY_SIZE(trickle_ilims), |
| 242 | pdata->trickle_ilim, ®2, |
| 243 | "trickle charge current limit", "mA"); |
| 244 | |
| 245 | wm831x_battey_apply_config(wm831x, vsels, ARRAY_SIZE(vsels), |
| 246 | pdata->vsel, ®2, |
| 247 | "target voltage", "mV"); |
| 248 | |
| 249 | wm831x_battey_apply_config(wm831x, fast_ilims, ARRAY_SIZE(fast_ilims), |
| 250 | pdata->fast_ilim, ®2, |
| 251 | "fast charge current limit", "mA"); |
| 252 | |
| 253 | wm831x_battey_apply_config(wm831x, eoc_iterms, ARRAY_SIZE(eoc_iterms), |
| 254 | pdata->eoc_iterm, ®1, |
| 255 | "end of charge current threshold", "mA"); |
| 256 | |
| 257 | wm831x_battey_apply_config(wm831x, chg_times, ARRAY_SIZE(chg_times), |
| 258 | pdata->timeout, ®2, |
| 259 | "charger timeout", "min"); |
| 260 | |
| 261 | ret = wm831x_reg_unlock(wm831x); |
| 262 | if (ret != 0) { |
| 263 | dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_1, |
| 268 | WM831X_CHG_ENA_MASK | |
| 269 | WM831X_CHG_FAST_MASK | |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 270 | WM831X_CHG_ITERM_MASK, |
| 271 | reg1); |
| 272 | if (ret != 0) |
| 273 | dev_err(wm831x->dev, "Failed to set charger control 1: %d\n", |
| 274 | ret); |
| 275 | |
| 276 | ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_2, |
| 277 | WM831X_CHG_OFF_MSK | |
| 278 | WM831X_CHG_TIME_MASK | |
| 279 | WM831X_CHG_FAST_ILIM_MASK | |
| 280 | WM831X_CHG_TRKL_ILIM_MASK | |
| 281 | WM831X_CHG_VSEL_MASK, |
| 282 | reg2); |
| 283 | if (ret != 0) |
| 284 | dev_err(wm831x->dev, "Failed to set charger control 2: %d\n", |
| 285 | ret); |
| 286 | |
| 287 | wm831x_reg_lock(wm831x); |
| 288 | } |
| 289 | |
| 290 | static int wm831x_bat_check_status(struct wm831x *wm831x, int *status) |
| 291 | { |
| 292 | int ret; |
| 293 | |
| 294 | ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS); |
| 295 | if (ret < 0) |
| 296 | return ret; |
| 297 | |
| 298 | if (ret & WM831X_PWR_SRC_BATT) { |
| 299 | *status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS); |
| 304 | if (ret < 0) |
| 305 | return ret; |
| 306 | |
| 307 | switch (ret & WM831X_CHG_STATE_MASK) { |
| 308 | case WM831X_CHG_STATE_OFF: |
| 309 | *status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 310 | break; |
| 311 | case WM831X_CHG_STATE_TRICKLE: |
| 312 | case WM831X_CHG_STATE_FAST: |
| 313 | *status = POWER_SUPPLY_STATUS_CHARGING; |
| 314 | break; |
| 315 | |
| 316 | default: |
| 317 | *status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int wm831x_bat_check_type(struct wm831x *wm831x, int *type) |
| 325 | { |
| 326 | int ret; |
| 327 | |
| 328 | ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS); |
| 329 | if (ret < 0) |
| 330 | return ret; |
| 331 | |
| 332 | switch (ret & WM831X_CHG_STATE_MASK) { |
| 333 | case WM831X_CHG_STATE_TRICKLE: |
| 334 | case WM831X_CHG_STATE_TRICKLE_OT: |
| 335 | *type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 336 | break; |
| 337 | case WM831X_CHG_STATE_FAST: |
| 338 | case WM831X_CHG_STATE_FAST_OT: |
| 339 | *type = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 340 | break; |
| 341 | default: |
| 342 | *type = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int wm831x_bat_check_health(struct wm831x *wm831x, int *health) |
| 350 | { |
| 351 | int ret; |
| 352 | |
| 353 | ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS); |
| 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | |
| 357 | if (ret & WM831X_BATT_HOT_STS) { |
| 358 | *health = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | if (ret & WM831X_BATT_COLD_STS) { |
| 363 | *health = POWER_SUPPLY_HEALTH_COLD; |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | if (ret & WM831X_BATT_OV_STS) { |
| 368 | *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | switch (ret & WM831X_CHG_STATE_MASK) { |
| 373 | case WM831X_CHG_STATE_TRICKLE_OT: |
| 374 | case WM831X_CHG_STATE_FAST_OT: |
| 375 | *health = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 376 | break; |
| 377 | case WM831X_CHG_STATE_DEFECTIVE: |
| 378 | *health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 379 | break; |
| 380 | default: |
| 381 | *health = POWER_SUPPLY_HEALTH_GOOD; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int wm831x_bat_get_prop(struct power_supply *psy, |
| 389 | enum power_supply_property psp, |
| 390 | union power_supply_propval *val) |
| 391 | { |
| 392 | struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent); |
| 393 | struct wm831x *wm831x = wm831x_power->wm831x; |
| 394 | int ret = 0; |
| 395 | |
| 396 | switch (psp) { |
| 397 | case POWER_SUPPLY_PROP_STATUS: |
| 398 | ret = wm831x_bat_check_status(wm831x, &val->intval); |
| 399 | break; |
| 400 | case POWER_SUPPLY_PROP_ONLINE: |
| 401 | ret = wm831x_power_check_online(wm831x, WM831X_PWR_SRC_BATT, |
| 402 | val); |
| 403 | break; |
| 404 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 405 | ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_BATT, val); |
| 406 | break; |
| 407 | case POWER_SUPPLY_PROP_HEALTH: |
| 408 | ret = wm831x_bat_check_health(wm831x, &val->intval); |
| 409 | break; |
| 410 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 411 | ret = wm831x_bat_check_type(wm831x, &val->intval); |
| 412 | break; |
| 413 | default: |
| 414 | ret = -EINVAL; |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | static enum power_supply_property wm831x_bat_props[] = { |
| 422 | POWER_SUPPLY_PROP_STATUS, |
| 423 | POWER_SUPPLY_PROP_ONLINE, |
| 424 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 425 | POWER_SUPPLY_PROP_HEALTH, |
| 426 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 427 | }; |
| 428 | |
| 429 | static const char *wm831x_bat_irqs[] = { |
| 430 | "BATT HOT", |
| 431 | "BATT COLD", |
| 432 | "BATT FAIL", |
| 433 | "OV", |
| 434 | "END", |
| 435 | "TO", |
| 436 | "MODE", |
| 437 | "START", |
| 438 | }; |
| 439 | |
| 440 | static irqreturn_t wm831x_bat_irq(int irq, void *data) |
| 441 | { |
| 442 | struct wm831x_power *wm831x_power = data; |
| 443 | struct wm831x *wm831x = wm831x_power->wm831x; |
| 444 | |
| 445 | dev_dbg(wm831x->dev, "Battery status changed: %d\n", irq); |
| 446 | |
| 447 | /* The battery charger is autonomous so we don't need to do |
| 448 | * anything except kick user space */ |
| 449 | power_supply_changed(&wm831x_power->battery); |
| 450 | |
| 451 | return IRQ_HANDLED; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | /********************************************************************* |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 456 | * Initialisation |
| 457 | *********************************************************************/ |
| 458 | |
| 459 | static irqreturn_t wm831x_syslo_irq(int irq, void *data) |
| 460 | { |
| 461 | struct wm831x_power *wm831x_power = data; |
| 462 | struct wm831x *wm831x = wm831x_power->wm831x; |
| 463 | |
| 464 | /* Not much we can actually *do* but tell people for |
| 465 | * posterity, we're probably about to run out of power. */ |
| 466 | dev_crit(wm831x->dev, "SYSVDD under voltage\n"); |
| 467 | |
| 468 | return IRQ_HANDLED; |
| 469 | } |
| 470 | |
| 471 | static irqreturn_t wm831x_pwr_src_irq(int irq, void *data) |
| 472 | { |
| 473 | struct wm831x_power *wm831x_power = data; |
| 474 | struct wm831x *wm831x = wm831x_power->wm831x; |
| 475 | |
| 476 | dev_dbg(wm831x->dev, "Power source changed\n"); |
| 477 | |
Mark Brown | c26964e | 2009-10-01 15:41:06 +0100 | [diff] [blame] | 478 | /* Just notify for everything - little harm in overnotifying. */ |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 479 | power_supply_changed(&wm831x_power->battery); |
| 480 | power_supply_changed(&wm831x_power->usb); |
| 481 | power_supply_changed(&wm831x_power->wall); |
| 482 | |
| 483 | return IRQ_HANDLED; |
| 484 | } |
| 485 | |
| 486 | static __devinit int wm831x_power_probe(struct platform_device *pdev) |
| 487 | { |
| 488 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
| 489 | struct wm831x_power *power; |
| 490 | struct power_supply *usb; |
| 491 | struct power_supply *battery; |
| 492 | struct power_supply *wall; |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 493 | int ret, irq, i; |
| 494 | |
| 495 | power = kzalloc(sizeof(struct wm831x_power), GFP_KERNEL); |
| 496 | if (power == NULL) |
| 497 | return -ENOMEM; |
| 498 | |
| 499 | power->wm831x = wm831x; |
| 500 | platform_set_drvdata(pdev, power); |
| 501 | |
| 502 | usb = &power->usb; |
| 503 | battery = &power->battery; |
| 504 | wall = &power->wall; |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 505 | |
| 506 | /* We ignore configuration failures since we can still read back |
Mark Brown | c26964e | 2009-10-01 15:41:06 +0100 | [diff] [blame] | 507 | * the status without enabling the charger. |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 508 | */ |
| 509 | wm831x_config_battery(wm831x); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 510 | |
| 511 | wall->name = "wm831x-wall"; |
| 512 | wall->type = POWER_SUPPLY_TYPE_MAINS; |
| 513 | wall->properties = wm831x_wall_props; |
| 514 | wall->num_properties = ARRAY_SIZE(wm831x_wall_props); |
| 515 | wall->get_property = wm831x_wall_get_prop; |
| 516 | ret = power_supply_register(&pdev->dev, wall); |
| 517 | if (ret) |
| 518 | goto err_kmalloc; |
| 519 | |
| 520 | battery->name = "wm831x-battery"; |
| 521 | battery->properties = wm831x_bat_props; |
| 522 | battery->num_properties = ARRAY_SIZE(wm831x_bat_props); |
| 523 | battery->get_property = wm831x_bat_get_prop; |
| 524 | battery->use_for_apm = 1; |
| 525 | ret = power_supply_register(&pdev->dev, battery); |
| 526 | if (ret) |
| 527 | goto err_wall; |
| 528 | |
| 529 | usb->name = "wm831x-usb", |
| 530 | usb->type = POWER_SUPPLY_TYPE_USB; |
| 531 | usb->properties = wm831x_usb_props; |
| 532 | usb->num_properties = ARRAY_SIZE(wm831x_usb_props); |
| 533 | usb->get_property = wm831x_usb_get_prop; |
| 534 | ret = power_supply_register(&pdev->dev, usb); |
| 535 | if (ret) |
| 536 | goto err_battery; |
| 537 | |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 538 | irq = platform_get_irq_byname(pdev, "SYSLO"); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 539 | ret = request_threaded_irq(irq, NULL, wm831x_syslo_irq, |
| 540 | IRQF_TRIGGER_RISING, "System power low", |
| 541 | power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 542 | if (ret != 0) { |
| 543 | dev_err(&pdev->dev, "Failed to request SYSLO IRQ %d: %d\n", |
| 544 | irq, ret); |
Mark Brown | c26964e | 2009-10-01 15:41:06 +0100 | [diff] [blame] | 545 | goto err_usb; |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | irq = platform_get_irq_byname(pdev, "PWR SRC"); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 549 | ret = request_threaded_irq(irq, NULL, wm831x_pwr_src_irq, |
| 550 | IRQF_TRIGGER_RISING, "Power source", |
| 551 | power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 552 | if (ret != 0) { |
| 553 | dev_err(&pdev->dev, "Failed to request PWR SRC IRQ %d: %d\n", |
| 554 | irq, ret); |
| 555 | goto err_syslo; |
| 556 | } |
| 557 | |
| 558 | for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) { |
| 559 | irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 560 | ret = request_threaded_irq(irq, NULL, wm831x_bat_irq, |
| 561 | IRQF_TRIGGER_RISING, |
| 562 | wm831x_bat_irqs[i], |
| 563 | power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 564 | if (ret != 0) { |
| 565 | dev_err(&pdev->dev, |
| 566 | "Failed to request %s IRQ %d: %d\n", |
| 567 | wm831x_bat_irqs[i], irq, ret); |
| 568 | goto err_bat_irq; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | return ret; |
| 573 | |
| 574 | err_bat_irq: |
| 575 | for (; i >= 0; i--) { |
| 576 | irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 577 | free_irq(irq, power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 578 | } |
| 579 | irq = platform_get_irq_byname(pdev, "PWR SRC"); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 580 | free_irq(irq, power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 581 | err_syslo: |
| 582 | irq = platform_get_irq_byname(pdev, "SYSLO"); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 583 | free_irq(irq, power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 584 | err_usb: |
| 585 | power_supply_unregister(usb); |
| 586 | err_battery: |
| 587 | power_supply_unregister(battery); |
| 588 | err_wall: |
| 589 | power_supply_unregister(wall); |
| 590 | err_kmalloc: |
| 591 | kfree(power); |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | static __devexit int wm831x_power_remove(struct platform_device *pdev) |
| 596 | { |
| 597 | struct wm831x_power *wm831x_power = platform_get_drvdata(pdev); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 598 | int irq, i; |
| 599 | |
| 600 | for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) { |
| 601 | irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 602 | free_irq(irq, wm831x_power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | irq = platform_get_irq_byname(pdev, "PWR SRC"); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 606 | free_irq(irq, wm831x_power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 607 | |
| 608 | irq = platform_get_irq_byname(pdev, "SYSLO"); |
Mark Brown | b5874f3 | 2010-03-10 18:27:40 +0000 | [diff] [blame] | 609 | free_irq(irq, wm831x_power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 610 | |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 611 | power_supply_unregister(&wm831x_power->battery); |
| 612 | power_supply_unregister(&wm831x_power->wall); |
| 613 | power_supply_unregister(&wm831x_power->usb); |
Axel Lin | 9c99f08 | 2010-09-27 15:10:11 +0800 | [diff] [blame] | 614 | kfree(wm831x_power); |
Mark Brown | 3961f7c | 2009-08-10 17:43:53 +0100 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static struct platform_driver wm831x_power_driver = { |
| 619 | .probe = wm831x_power_probe, |
| 620 | .remove = __devexit_p(wm831x_power_remove), |
| 621 | .driver = { |
| 622 | .name = "wm831x-power", |
| 623 | }, |
| 624 | }; |
| 625 | |
| 626 | static int __init wm831x_power_init(void) |
| 627 | { |
| 628 | return platform_driver_register(&wm831x_power_driver); |
| 629 | } |
| 630 | module_init(wm831x_power_init); |
| 631 | |
| 632 | static void __exit wm831x_power_exit(void) |
| 633 | { |
| 634 | platform_driver_unregister(&wm831x_power_driver); |
| 635 | } |
| 636 | module_exit(wm831x_power_exit); |
| 637 | |
| 638 | MODULE_DESCRIPTION("Power supply driver for WM831x PMICs"); |
| 639 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 640 | MODULE_LICENSE("GPL"); |
| 641 | MODULE_ALIAS("platform:wm831x-power"); |