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