Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * DA9150 Charger Driver |
| 3 | * |
| 4 | * Copyright (c) 2014 Dialog Semiconductor |
| 5 | * |
| 6 | * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/power_supply.h> |
| 22 | #include <linux/notifier.h> |
| 23 | #include <linux/usb/phy.h> |
| 24 | #include <linux/iio/consumer.h> |
| 25 | #include <linux/mfd/da9150/core.h> |
| 26 | #include <linux/mfd/da9150/registers.h> |
| 27 | |
| 28 | /* Private data */ |
| 29 | struct da9150_charger { |
| 30 | struct da9150 *da9150; |
| 31 | struct device *dev; |
| 32 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 33 | struct power_supply *usb; |
| 34 | struct power_supply *battery; |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 35 | struct power_supply *supply_online; |
| 36 | |
| 37 | struct usb_phy *usb_phy; |
| 38 | struct notifier_block otg_nb; |
| 39 | struct work_struct otg_work; |
| 40 | unsigned long usb_event; |
| 41 | |
| 42 | struct iio_channel *ibus_chan; |
| 43 | struct iio_channel *vbus_chan; |
| 44 | struct iio_channel *tjunc_chan; |
| 45 | struct iio_channel *vbat_chan; |
| 46 | }; |
| 47 | |
| 48 | static inline int da9150_charger_supply_online(struct da9150_charger *charger, |
| 49 | struct power_supply *psy, |
| 50 | union power_supply_propval *val) |
| 51 | { |
| 52 | val->intval = (psy == charger->supply_online) ? 1 : 0; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* Charger Properties */ |
| 58 | static int da9150_charger_vbus_voltage_now(struct da9150_charger *charger, |
| 59 | union power_supply_propval *val) |
| 60 | { |
| 61 | int v_val, ret; |
| 62 | |
| 63 | /* Read processed value - mV units */ |
| 64 | ret = iio_read_channel_processed(charger->vbus_chan, &v_val); |
| 65 | if (ret < 0) |
| 66 | return ret; |
| 67 | |
| 68 | /* Convert voltage to expected uV units */ |
| 69 | val->intval = v_val * 1000; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int da9150_charger_ibus_current_avg(struct da9150_charger *charger, |
| 75 | union power_supply_propval *val) |
| 76 | { |
| 77 | int i_val, ret; |
| 78 | |
| 79 | /* Read processed value - mA units */ |
| 80 | ret = iio_read_channel_processed(charger->ibus_chan, &i_val); |
| 81 | if (ret < 0) |
| 82 | return ret; |
| 83 | |
| 84 | /* Convert current to expected uA units */ |
| 85 | val->intval = i_val * 1000; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int da9150_charger_tjunc_temp(struct da9150_charger *charger, |
| 91 | union power_supply_propval *val) |
| 92 | { |
| 93 | int t_val, ret; |
| 94 | |
| 95 | /* Read processed value - 0.001 degrees C units */ |
| 96 | ret = iio_read_channel_processed(charger->tjunc_chan, &t_val); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
| 100 | /* Convert temp to expect 0.1 degrees C units */ |
| 101 | val->intval = t_val / 100; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static enum power_supply_property da9150_charger_props[] = { |
| 107 | POWER_SUPPLY_PROP_ONLINE, |
| 108 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 109 | POWER_SUPPLY_PROP_CURRENT_AVG, |
| 110 | POWER_SUPPLY_PROP_TEMP, |
| 111 | }; |
| 112 | |
| 113 | static int da9150_charger_get_prop(struct power_supply *psy, |
| 114 | enum power_supply_property psp, |
| 115 | union power_supply_propval *val) |
| 116 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 117 | struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 118 | int ret; |
| 119 | |
| 120 | switch (psp) { |
| 121 | case POWER_SUPPLY_PROP_ONLINE: |
| 122 | ret = da9150_charger_supply_online(charger, psy, val); |
| 123 | break; |
| 124 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 125 | ret = da9150_charger_vbus_voltage_now(charger, val); |
| 126 | break; |
| 127 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
| 128 | ret = da9150_charger_ibus_current_avg(charger, val); |
| 129 | break; |
| 130 | case POWER_SUPPLY_PROP_TEMP: |
| 131 | ret = da9150_charger_tjunc_temp(charger, val); |
| 132 | break; |
| 133 | default: |
| 134 | ret = -EINVAL; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | /* Battery Properties */ |
| 142 | static int da9150_charger_battery_status(struct da9150_charger *charger, |
| 143 | union power_supply_propval *val) |
| 144 | { |
| 145 | u8 reg; |
| 146 | |
| 147 | /* Check to see if battery is discharging */ |
| 148 | reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H); |
| 149 | |
| 150 | if (((reg & DA9150_VBUS_STAT_MASK) == DA9150_VBUS_STAT_OFF) || |
| 151 | ((reg & DA9150_VBUS_STAT_MASK) == DA9150_VBUS_STAT_WAIT)) { |
| 152 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); |
| 158 | |
| 159 | /* Now check for other states */ |
| 160 | switch (reg & DA9150_CHG_STAT_MASK) { |
| 161 | case DA9150_CHG_STAT_ACT: |
| 162 | case DA9150_CHG_STAT_PRE: |
| 163 | case DA9150_CHG_STAT_CC: |
| 164 | case DA9150_CHG_STAT_CV: |
| 165 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 166 | break; |
| 167 | case DA9150_CHG_STAT_OFF: |
| 168 | case DA9150_CHG_STAT_SUSP: |
| 169 | case DA9150_CHG_STAT_TEMP: |
| 170 | case DA9150_CHG_STAT_TIME: |
| 171 | case DA9150_CHG_STAT_BAT: |
| 172 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 173 | break; |
| 174 | case DA9150_CHG_STAT_FULL: |
| 175 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 176 | break; |
| 177 | default: |
| 178 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int da9150_charger_battery_health(struct da9150_charger *charger, |
| 186 | union power_supply_propval *val) |
| 187 | { |
| 188 | u8 reg; |
| 189 | |
| 190 | reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); |
| 191 | |
| 192 | /* Check if temperature limit reached */ |
| 193 | switch (reg & DA9150_CHG_TEMP_MASK) { |
| 194 | case DA9150_CHG_TEMP_UNDER: |
| 195 | val->intval = POWER_SUPPLY_HEALTH_COLD; |
| 196 | return 0; |
| 197 | case DA9150_CHG_TEMP_OVER: |
| 198 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 199 | return 0; |
| 200 | default: |
| 201 | break; |
| 202 | } |
| 203 | |
| 204 | /* Check for other health states */ |
| 205 | switch (reg & DA9150_CHG_STAT_MASK) { |
| 206 | case DA9150_CHG_STAT_ACT: |
| 207 | case DA9150_CHG_STAT_PRE: |
| 208 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 209 | break; |
| 210 | case DA9150_CHG_STAT_TIME: |
| 211 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 212 | break; |
| 213 | default: |
| 214 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int da9150_charger_battery_present(struct da9150_charger *charger, |
| 222 | union power_supply_propval *val) |
| 223 | { |
| 224 | u8 reg; |
| 225 | |
| 226 | /* Check if battery present or removed */ |
| 227 | reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); |
| 228 | if ((reg & DA9150_CHG_STAT_MASK) == DA9150_CHG_STAT_BAT) |
| 229 | val->intval = 0; |
| 230 | else |
| 231 | val->intval = 1; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int da9150_charger_battery_charge_type(struct da9150_charger *charger, |
| 237 | union power_supply_propval *val) |
| 238 | { |
| 239 | u8 reg; |
| 240 | |
| 241 | reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); |
| 242 | |
| 243 | switch (reg & DA9150_CHG_STAT_MASK) { |
| 244 | case DA9150_CHG_STAT_CC: |
| 245 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 246 | break; |
| 247 | case DA9150_CHG_STAT_ACT: |
| 248 | case DA9150_CHG_STAT_PRE: |
| 249 | case DA9150_CHG_STAT_CV: |
| 250 | val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 251 | break; |
| 252 | default: |
| 253 | val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int da9150_charger_battery_voltage_min(struct da9150_charger *charger, |
| 261 | union power_supply_propval *val) |
| 262 | { |
| 263 | u8 reg; |
| 264 | |
| 265 | reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_C); |
| 266 | |
| 267 | /* Value starts at 2500 mV, 50 mV increments, presented in uV */ |
| 268 | val->intval = ((reg & DA9150_CHG_VFAULT_MASK) * 50000) + 2500000; |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int da9150_charger_battery_voltage_now(struct da9150_charger *charger, |
| 274 | union power_supply_propval *val) |
| 275 | { |
| 276 | int v_val, ret; |
| 277 | |
| 278 | /* Read processed value - mV units */ |
| 279 | ret = iio_read_channel_processed(charger->vbat_chan, &v_val); |
| 280 | if (ret < 0) |
| 281 | return ret; |
| 282 | |
| 283 | val->intval = v_val * 1000; |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int da9150_charger_battery_current_max(struct da9150_charger *charger, |
| 289 | union power_supply_propval *val) |
| 290 | { |
| 291 | int reg; |
| 292 | |
| 293 | reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_D); |
| 294 | |
| 295 | /* 25mA increments */ |
| 296 | val->intval = reg * 25000; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int da9150_charger_battery_voltage_max(struct da9150_charger *charger, |
| 302 | union power_supply_propval *val) |
| 303 | { |
| 304 | u8 reg; |
| 305 | |
| 306 | reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_B); |
| 307 | |
| 308 | /* Value starts at 3650 mV, 25 mV increments, presented in uV */ |
| 309 | val->intval = ((reg & DA9150_CHG_VBAT_MASK) * 25000) + 3650000; |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static enum power_supply_property da9150_charger_bat_props[] = { |
| 314 | POWER_SUPPLY_PROP_STATUS, |
| 315 | POWER_SUPPLY_PROP_ONLINE, |
| 316 | POWER_SUPPLY_PROP_HEALTH, |
| 317 | POWER_SUPPLY_PROP_PRESENT, |
| 318 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 319 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 320 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 321 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
| 322 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, |
| 323 | }; |
| 324 | |
| 325 | static int da9150_charger_battery_get_prop(struct power_supply *psy, |
| 326 | enum power_supply_property psp, |
| 327 | union power_supply_propval *val) |
| 328 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 329 | struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 330 | int ret; |
| 331 | |
| 332 | switch (psp) { |
| 333 | case POWER_SUPPLY_PROP_STATUS: |
| 334 | ret = da9150_charger_battery_status(charger, val); |
| 335 | break; |
| 336 | case POWER_SUPPLY_PROP_ONLINE: |
| 337 | ret = da9150_charger_supply_online(charger, psy, val); |
| 338 | break; |
| 339 | case POWER_SUPPLY_PROP_HEALTH: |
| 340 | ret = da9150_charger_battery_health(charger, val); |
| 341 | break; |
| 342 | case POWER_SUPPLY_PROP_PRESENT: |
| 343 | ret = da9150_charger_battery_present(charger, val); |
| 344 | break; |
| 345 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 346 | ret = da9150_charger_battery_charge_type(charger, val); |
| 347 | break; |
| 348 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
| 349 | ret = da9150_charger_battery_voltage_min(charger, val); |
| 350 | break; |
| 351 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 352 | ret = da9150_charger_battery_voltage_now(charger, val); |
| 353 | break; |
| 354 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| 355 | ret = da9150_charger_battery_current_max(charger, val); |
| 356 | break; |
| 357 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: |
| 358 | ret = da9150_charger_battery_voltage_max(charger, val); |
| 359 | break; |
| 360 | default: |
| 361 | ret = -EINVAL; |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | static irqreturn_t da9150_charger_chg_irq(int irq, void *data) |
| 369 | { |
| 370 | struct da9150_charger *charger = data; |
| 371 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 372 | power_supply_changed(charger->battery); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 373 | |
| 374 | return IRQ_HANDLED; |
| 375 | } |
| 376 | |
| 377 | static irqreturn_t da9150_charger_tjunc_irq(int irq, void *data) |
| 378 | { |
| 379 | struct da9150_charger *charger = data; |
| 380 | |
| 381 | /* Nothing we can really do except report this. */ |
| 382 | dev_crit(charger->dev, "TJunc over temperature!!!\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 383 | power_supply_changed(charger->usb); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 384 | |
| 385 | return IRQ_HANDLED; |
| 386 | } |
| 387 | |
| 388 | static irqreturn_t da9150_charger_vfault_irq(int irq, void *data) |
| 389 | { |
| 390 | struct da9150_charger *charger = data; |
| 391 | |
| 392 | /* Nothing we can really do except report this. */ |
| 393 | dev_crit(charger->dev, "VSYS under voltage!!!\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 394 | power_supply_changed(charger->usb); |
| 395 | power_supply_changed(charger->battery); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 396 | |
| 397 | return IRQ_HANDLED; |
| 398 | } |
| 399 | |
| 400 | static irqreturn_t da9150_charger_vbus_irq(int irq, void *data) |
| 401 | { |
| 402 | struct da9150_charger *charger = data; |
| 403 | u8 reg; |
| 404 | |
| 405 | reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H); |
| 406 | |
| 407 | /* Charger plugged in or battery only */ |
| 408 | switch (reg & DA9150_VBUS_STAT_MASK) { |
| 409 | case DA9150_VBUS_STAT_OFF: |
| 410 | case DA9150_VBUS_STAT_WAIT: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 411 | charger->supply_online = charger->battery; |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 412 | break; |
| 413 | case DA9150_VBUS_STAT_CHG: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 414 | charger->supply_online = charger->usb; |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 415 | break; |
| 416 | default: |
| 417 | dev_warn(charger->dev, "Unknown VBUS state - reg = 0x%x\n", |
| 418 | reg); |
| 419 | charger->supply_online = NULL; |
| 420 | break; |
| 421 | } |
| 422 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 423 | power_supply_changed(charger->usb); |
| 424 | power_supply_changed(charger->battery); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 425 | |
| 426 | return IRQ_HANDLED; |
| 427 | } |
| 428 | |
| 429 | static void da9150_charger_otg_work(struct work_struct *data) |
| 430 | { |
| 431 | struct da9150_charger *charger = |
| 432 | container_of(data, struct da9150_charger, otg_work); |
| 433 | |
| 434 | switch (charger->usb_event) { |
| 435 | case USB_EVENT_ID: |
| 436 | /* Enable OTG Boost */ |
| 437 | da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A, |
| 438 | DA9150_VBUS_MODE_MASK, DA9150_VBUS_MODE_OTG); |
| 439 | break; |
| 440 | case USB_EVENT_NONE: |
| 441 | /* Revert to charge mode */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 442 | power_supply_changed(charger->usb); |
| 443 | power_supply_changed(charger->battery); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 444 | da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A, |
| 445 | DA9150_VBUS_MODE_MASK, DA9150_VBUS_MODE_CHG); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | static int da9150_charger_otg_ncb(struct notifier_block *nb, unsigned long val, |
| 451 | void *priv) |
| 452 | { |
| 453 | struct da9150_charger *charger = |
| 454 | container_of(nb, struct da9150_charger, otg_nb); |
| 455 | |
| 456 | dev_dbg(charger->dev, "DA9150 OTG notify %lu\n", val); |
| 457 | |
| 458 | charger->usb_event = val; |
| 459 | schedule_work(&charger->otg_work); |
| 460 | |
| 461 | return NOTIFY_OK; |
| 462 | } |
| 463 | |
| 464 | static int da9150_charger_register_irq(struct platform_device *pdev, |
| 465 | irq_handler_t handler, |
| 466 | const char *irq_name) |
| 467 | { |
| 468 | struct device *dev = &pdev->dev; |
| 469 | struct da9150_charger *charger = platform_get_drvdata(pdev); |
| 470 | int irq, ret; |
| 471 | |
| 472 | irq = platform_get_irq_byname(pdev, irq_name); |
| 473 | if (irq < 0) { |
| 474 | dev_err(dev, "Failed to get IRQ CHG_STATUS: %d\n", irq); |
| 475 | return irq; |
| 476 | } |
| 477 | |
| 478 | ret = request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT, irq_name, |
| 479 | charger); |
| 480 | if (ret) |
| 481 | dev_err(dev, "Failed to request IRQ %d: %d\n", irq, ret); |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | static void da9150_charger_unregister_irq(struct platform_device *pdev, |
| 487 | const char *irq_name) |
| 488 | { |
| 489 | struct device *dev = &pdev->dev; |
| 490 | struct da9150_charger *charger = platform_get_drvdata(pdev); |
| 491 | int irq; |
| 492 | |
| 493 | irq = platform_get_irq_byname(pdev, irq_name); |
| 494 | if (irq < 0) { |
| 495 | dev_err(dev, "Failed to get IRQ CHG_STATUS: %d\n", irq); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | free_irq(irq, charger); |
| 500 | } |
| 501 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 502 | static const struct power_supply_desc usb_desc = { |
| 503 | .name = "da9150-usb", |
| 504 | .type = POWER_SUPPLY_TYPE_USB, |
| 505 | .properties = da9150_charger_props, |
| 506 | .num_properties = ARRAY_SIZE(da9150_charger_props), |
| 507 | .get_property = da9150_charger_get_prop, |
| 508 | }; |
| 509 | |
| 510 | static const struct power_supply_desc battery_desc = { |
| 511 | .name = "da9150-battery", |
| 512 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 513 | .properties = da9150_charger_bat_props, |
| 514 | .num_properties = ARRAY_SIZE(da9150_charger_bat_props), |
| 515 | .get_property = da9150_charger_battery_get_prop, |
| 516 | }; |
| 517 | |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 518 | static int da9150_charger_probe(struct platform_device *pdev) |
| 519 | { |
| 520 | struct device *dev = &pdev->dev; |
| 521 | struct da9150 *da9150 = dev_get_drvdata(dev->parent); |
| 522 | struct da9150_charger *charger; |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 523 | u8 reg; |
| 524 | int ret; |
| 525 | |
| 526 | charger = devm_kzalloc(dev, sizeof(struct da9150_charger), GFP_KERNEL); |
| 527 | if (!charger) |
| 528 | return -ENOMEM; |
| 529 | |
| 530 | platform_set_drvdata(pdev, charger); |
| 531 | charger->da9150 = da9150; |
| 532 | charger->dev = dev; |
| 533 | |
| 534 | /* Acquire ADC channels */ |
| 535 | charger->ibus_chan = iio_channel_get(dev, "CHAN_IBUS"); |
| 536 | if (IS_ERR(charger->ibus_chan)) { |
| 537 | ret = PTR_ERR(charger->ibus_chan); |
| 538 | goto ibus_chan_fail; |
| 539 | } |
| 540 | |
| 541 | charger->vbus_chan = iio_channel_get(dev, "CHAN_VBUS"); |
| 542 | if (IS_ERR(charger->vbus_chan)) { |
| 543 | ret = PTR_ERR(charger->vbus_chan); |
| 544 | goto vbus_chan_fail; |
| 545 | } |
| 546 | |
| 547 | charger->tjunc_chan = iio_channel_get(dev, "CHAN_TJUNC"); |
| 548 | if (IS_ERR(charger->tjunc_chan)) { |
| 549 | ret = PTR_ERR(charger->tjunc_chan); |
| 550 | goto tjunc_chan_fail; |
| 551 | } |
| 552 | |
| 553 | charger->vbat_chan = iio_channel_get(dev, "CHAN_VBAT"); |
| 554 | if (IS_ERR(charger->vbat_chan)) { |
| 555 | ret = PTR_ERR(charger->vbat_chan); |
| 556 | goto vbat_chan_fail; |
| 557 | } |
| 558 | |
| 559 | /* Register power supplies */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 560 | charger->usb = power_supply_register(dev, &usb_desc, NULL); |
| 561 | if (IS_ERR(charger->usb)) { |
| 562 | ret = PTR_ERR(charger->usb); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 563 | goto usb_fail; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 564 | } |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 565 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 566 | charger->battery = power_supply_register(dev, &battery_desc, NULL); |
| 567 | if (IS_ERR(charger->battery)) { |
| 568 | ret = PTR_ERR(charger->battery); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 569 | goto battery_fail; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 570 | } |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 571 | |
| 572 | /* Get initial online supply */ |
| 573 | reg = da9150_reg_read(da9150, DA9150_STATUS_H); |
| 574 | |
| 575 | switch (reg & DA9150_VBUS_STAT_MASK) { |
| 576 | case DA9150_VBUS_STAT_OFF: |
| 577 | case DA9150_VBUS_STAT_WAIT: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 578 | charger->supply_online = charger->battery; |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 579 | break; |
| 580 | case DA9150_VBUS_STAT_CHG: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 581 | charger->supply_online = charger->usb; |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 582 | break; |
| 583 | default: |
| 584 | dev_warn(dev, "Unknown VBUS state - reg = 0x%x\n", reg); |
| 585 | charger->supply_online = NULL; |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | /* Setup OTG reporting & configuration */ |
| 590 | charger->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); |
| 591 | if (!IS_ERR_OR_NULL(charger->usb_phy)) { |
| 592 | INIT_WORK(&charger->otg_work, da9150_charger_otg_work); |
| 593 | charger->otg_nb.notifier_call = da9150_charger_otg_ncb; |
| 594 | usb_register_notifier(charger->usb_phy, &charger->otg_nb); |
| 595 | } |
| 596 | |
| 597 | /* Register IRQs */ |
| 598 | ret = da9150_charger_register_irq(pdev, da9150_charger_chg_irq, |
| 599 | "CHG_STATUS"); |
| 600 | if (ret < 0) |
| 601 | goto chg_irq_fail; |
| 602 | |
| 603 | ret = da9150_charger_register_irq(pdev, da9150_charger_tjunc_irq, |
| 604 | "CHG_TJUNC"); |
| 605 | if (ret < 0) |
| 606 | goto tjunc_irq_fail; |
| 607 | |
| 608 | ret = da9150_charger_register_irq(pdev, da9150_charger_vfault_irq, |
| 609 | "CHG_VFAULT"); |
| 610 | if (ret < 0) |
| 611 | goto vfault_irq_fail; |
| 612 | |
| 613 | ret = da9150_charger_register_irq(pdev, da9150_charger_vbus_irq, |
| 614 | "CHG_VBUS"); |
| 615 | if (ret < 0) |
| 616 | goto vbus_irq_fail; |
| 617 | |
| 618 | return 0; |
| 619 | |
| 620 | |
| 621 | vbus_irq_fail: |
| 622 | da9150_charger_unregister_irq(pdev, "CHG_VFAULT"); |
| 623 | vfault_irq_fail: |
| 624 | da9150_charger_unregister_irq(pdev, "CHG_TJUNC"); |
| 625 | tjunc_irq_fail: |
| 626 | da9150_charger_unregister_irq(pdev, "CHG_STATUS"); |
| 627 | chg_irq_fail: |
| 628 | if (!IS_ERR_OR_NULL(charger->usb_phy)) |
| 629 | usb_unregister_notifier(charger->usb_phy, &charger->otg_nb); |
| 630 | battery_fail: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 631 | power_supply_unregister(charger->usb); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 632 | |
| 633 | usb_fail: |
| 634 | iio_channel_release(charger->vbat_chan); |
| 635 | |
| 636 | vbat_chan_fail: |
| 637 | iio_channel_release(charger->tjunc_chan); |
| 638 | |
| 639 | tjunc_chan_fail: |
| 640 | iio_channel_release(charger->vbus_chan); |
| 641 | |
| 642 | vbus_chan_fail: |
| 643 | iio_channel_release(charger->ibus_chan); |
| 644 | |
| 645 | ibus_chan_fail: |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | static int da9150_charger_remove(struct platform_device *pdev) |
| 650 | { |
| 651 | struct da9150_charger *charger = platform_get_drvdata(pdev); |
| 652 | int irq; |
| 653 | |
| 654 | /* Make sure IRQs are released before unregistering power supplies */ |
| 655 | irq = platform_get_irq_byname(pdev, "CHG_VBUS"); |
| 656 | free_irq(irq, charger); |
| 657 | |
| 658 | irq = platform_get_irq_byname(pdev, "CHG_VFAULT"); |
| 659 | free_irq(irq, charger); |
| 660 | |
| 661 | irq = platform_get_irq_byname(pdev, "CHG_TJUNC"); |
| 662 | free_irq(irq, charger); |
| 663 | |
| 664 | irq = platform_get_irq_byname(pdev, "CHG_STATUS"); |
| 665 | free_irq(irq, charger); |
| 666 | |
| 667 | if (!IS_ERR_OR_NULL(charger->usb_phy)) |
| 668 | usb_unregister_notifier(charger->usb_phy, &charger->otg_nb); |
| 669 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 670 | power_supply_unregister(charger->battery); |
| 671 | power_supply_unregister(charger->usb); |
Adam Thomson | c1a281e | 2015-02-18 14:08:32 +0000 | [diff] [blame] | 672 | |
| 673 | /* Release ADC channels */ |
| 674 | iio_channel_release(charger->ibus_chan); |
| 675 | iio_channel_release(charger->vbus_chan); |
| 676 | iio_channel_release(charger->tjunc_chan); |
| 677 | iio_channel_release(charger->vbat_chan); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static struct platform_driver da9150_charger_driver = { |
| 683 | .driver = { |
| 684 | .name = "da9150-charger", |
| 685 | }, |
| 686 | .probe = da9150_charger_probe, |
| 687 | .remove = da9150_charger_remove, |
| 688 | }; |
| 689 | |
| 690 | module_platform_driver(da9150_charger_driver); |
| 691 | |
| 692 | MODULE_DESCRIPTION("Charger Driver for DA9150"); |
| 693 | MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>"); |
| 694 | MODULE_LICENSE("GPL"); |