1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Motorola CPCAP PMIC battery charger driver 4 * 5 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com> 6 * 7 * Rewritten for Linux power framework with some parts based on 8 * on earlier driver found in the Motorola Linux kernel: 9 * 10 * Copyright (C) 2009-2010 Motorola, Inc. 11 */ 12 13 #include <linux/atomic.h> 14 #include <linux/init.h> 15 #include <linux/module.h> 16 #include <linux/slab.h> 17 #include <linux/err.h> 18 #include <linux/interrupt.h> 19 #include <linux/notifier.h> 20 #include <linux/of.h> 21 #include <linux/of_platform.h> 22 #include <linux/platform_device.h> 23 #include <linux/power_supply.h> 24 #include <linux/regmap.h> 25 26 #include <linux/gpio/consumer.h> 27 #include <linux/usb/phy_companion.h> 28 #include <linux/phy/omap_usb.h> 29 #include <linux/usb/otg.h> 30 #include <linux/iio/consumer.h> 31 #include <linux/mfd/motorola-cpcap.h> 32 33 /* 34 * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware, 35 * see NXP "MC13783 Power Management and Audio Circuit Users's Guide" 36 * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers 37 * and values for CPCAP are different, but some of the internal components seem 38 * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1 39 * bits that seem to describe the CRM register. 40 */ 41 #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */ 42 #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */ 43 #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13) /* Charger LED */ 44 #define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */ 45 #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */ 46 #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10) 47 #define CPCAP_REG_CRM_FET_OVRD BIT(9) /* 0 = hardware, 1 = FET_CTRL */ 48 #define CPCAP_REG_CRM_FET_CTRL BIT(8) /* BPFET 1 if FET_OVRD set */ 49 #define CPCAP_REG_CRM_VCHRG3 BIT(7) /* Charge voltage bits */ 50 #define CPCAP_REG_CRM_VCHRG2 BIT(6) 51 #define CPCAP_REG_CRM_VCHRG1 BIT(5) 52 #define CPCAP_REG_CRM_VCHRG0 BIT(4) 53 #define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */ 54 #define CPCAP_REG_CRM_ICHRG2 BIT(2) 55 #define CPCAP_REG_CRM_ICHRG1 BIT(1) 56 #define CPCAP_REG_CRM_ICHRG0 BIT(0) 57 58 /* CPCAP_REG_CRM trickle charge voltages */ 59 #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10) 60 #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0) 61 #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1) 62 #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2) 63 #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4) 64 65 /* 66 * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values. 67 * Note that these register bits don't match MC13783UG.pdf VCHRG 68 * register bits. 69 */ 70 #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4) 71 #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0) 72 #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1) 73 #define CPCAP_REG_CRM_VCHRG_4V12 CPCAP_REG_CRM_VCHRG(0x2) 74 #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x3) 75 #define CPCAP_REG_CRM_VCHRG_4V17 CPCAP_REG_CRM_VCHRG(0x4) 76 #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x5) 77 #define CPCAP_REG_CRM_VCHRG_4V23 CPCAP_REG_CRM_VCHRG(0x6) 78 #define CPCAP_REG_CRM_VCHRG_4V25 CPCAP_REG_CRM_VCHRG(0x7) 79 #define CPCAP_REG_CRM_VCHRG_4V27 CPCAP_REG_CRM_VCHRG(0x8) 80 #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x9) 81 #define CPCAP_REG_CRM_VCHRG_4V33 CPCAP_REG_CRM_VCHRG(0xa) 82 #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb) 83 #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc) 84 #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd) 85 #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe) 86 #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf) 87 88 /* 89 * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf 90 * values in "Table 8-3. Charge Path Regulator Current Limit 91 * Characteristics" for the nominal values. 92 */ 93 #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0) 94 #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0) 95 #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1) 96 #define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2) 97 #define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3) 98 #define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4) 99 #define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5) 100 #define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6) 101 #define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7) 102 #define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8) 103 #define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9) 104 #define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa) 105 #define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb) 106 #define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc) 107 #define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd) 108 #define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe) 109 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf) 110 111 enum { 112 CPCAP_CHARGER_IIO_BATTDET, 113 CPCAP_CHARGER_IIO_VOLTAGE, 114 CPCAP_CHARGER_IIO_VBUS, 115 CPCAP_CHARGER_IIO_CHRG_CURRENT, 116 CPCAP_CHARGER_IIO_BATT_CURRENT, 117 CPCAP_CHARGER_IIO_NR, 118 }; 119 120 struct cpcap_charger_ddata { 121 struct device *dev; 122 struct regmap *reg; 123 struct list_head irq_list; 124 struct delayed_work detect_work; 125 struct delayed_work vbus_work; 126 struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */ 127 128 struct iio_channel *channels[CPCAP_CHARGER_IIO_NR]; 129 130 struct power_supply *usb; 131 132 struct phy_companion comparator; /* For USB VBUS */ 133 bool vbus_enabled; 134 atomic_t active; 135 136 int status; 137 }; 138 139 struct cpcap_interrupt_desc { 140 int irq; 141 struct list_head node; 142 const char *name; 143 }; 144 145 struct cpcap_charger_ints_state { 146 bool chrg_det; 147 bool rvrs_chrg; 148 bool vbusov; 149 150 bool chrg_se1b; 151 bool rvrs_mode; 152 bool chrgcurr1; 153 bool vbusvld; 154 155 bool battdetb; 156 }; 157 158 static enum power_supply_property cpcap_charger_props[] = { 159 POWER_SUPPLY_PROP_STATUS, 160 POWER_SUPPLY_PROP_ONLINE, 161 POWER_SUPPLY_PROP_VOLTAGE_NOW, 162 POWER_SUPPLY_PROP_CURRENT_NOW, 163 }; 164 165 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata) 166 { 167 struct iio_channel *channel; 168 int error, value; 169 170 channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET]; 171 error = iio_read_channel_raw(channel, &value); 172 if (error < 0) { 173 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 174 175 return false; 176 } 177 178 return value == 1; 179 } 180 181 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata) 182 { 183 struct iio_channel *channel; 184 int error, value = 0; 185 186 channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE]; 187 error = iio_read_channel_processed(channel, &value); 188 if (error < 0) { 189 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 190 191 return 0; 192 } 193 194 return value; 195 } 196 197 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata) 198 { 199 struct iio_channel *channel; 200 int error, value = 0; 201 202 channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT]; 203 error = iio_read_channel_processed(channel, &value); 204 if (error < 0) { 205 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 206 207 return 0; 208 } 209 210 return value; 211 } 212 213 static int cpcap_charger_get_property(struct power_supply *psy, 214 enum power_supply_property psp, 215 union power_supply_propval *val) 216 { 217 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent); 218 219 switch (psp) { 220 case POWER_SUPPLY_PROP_STATUS: 221 val->intval = ddata->status; 222 break; 223 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 224 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) 225 val->intval = cpcap_charger_get_charge_voltage(ddata) * 226 1000; 227 else 228 val->intval = 0; 229 break; 230 case POWER_SUPPLY_PROP_CURRENT_NOW: 231 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) 232 val->intval = cpcap_charger_get_charge_current(ddata) * 233 1000; 234 else 235 val->intval = 0; 236 break; 237 case POWER_SUPPLY_PROP_ONLINE: 238 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING; 239 break; 240 default: 241 return -EINVAL; 242 } 243 244 return 0; 245 } 246 247 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata, 248 bool enabled) 249 { 250 if (!ddata->gpio[0]) 251 return; 252 253 gpiod_set_value(ddata->gpio[0], enabled); 254 } 255 256 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata, 257 bool enabled) 258 { 259 if (!ddata->gpio[1]) 260 return; 261 262 gpiod_set_value(ddata->gpio[1], enabled); 263 } 264 265 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata, 266 int max_voltage, int charge_current, 267 int trickle_current) 268 { 269 bool enable; 270 int error; 271 272 enable = (charge_current || trickle_current); 273 dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable); 274 275 if (!enable) { 276 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 277 0x3fff, 278 CPCAP_REG_CRM_FET_OVRD | 279 CPCAP_REG_CRM_FET_CTRL); 280 if (error) { 281 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN; 282 goto out_err; 283 } 284 285 ddata->status = POWER_SUPPLY_STATUS_DISCHARGING; 286 287 return 0; 288 } 289 290 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, 291 CPCAP_REG_CRM_CHRG_LED_EN | 292 trickle_current | 293 CPCAP_REG_CRM_FET_OVRD | 294 CPCAP_REG_CRM_FET_CTRL | 295 max_voltage | 296 charge_current); 297 if (error) { 298 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN; 299 goto out_err; 300 } 301 302 ddata->status = POWER_SUPPLY_STATUS_CHARGING; 303 304 return 0; 305 306 out_err: 307 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 308 309 return error; 310 } 311 312 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata) 313 { 314 int error, value = 0; 315 struct iio_channel *channel = 316 ddata->channels[CPCAP_CHARGER_IIO_VBUS]; 317 318 error = iio_read_channel_processed(channel, &value); 319 if (error >= 0) 320 return value > 3900 ? true : false; 321 322 dev_err(ddata->dev, "error reading VBUS: %i\n", error); 323 324 return false; 325 } 326 327 /* VBUS control functions for the USB PHY companion */ 328 329 static void cpcap_charger_vbus_work(struct work_struct *work) 330 { 331 struct cpcap_charger_ddata *ddata; 332 bool vbus = false; 333 int error; 334 335 ddata = container_of(work, struct cpcap_charger_ddata, 336 vbus_work.work); 337 338 if (ddata->vbus_enabled) { 339 vbus = cpcap_charger_vbus_valid(ddata); 340 if (vbus) { 341 dev_info(ddata->dev, "VBUS already provided\n"); 342 343 return; 344 } 345 346 cpcap_charger_set_cable_path(ddata, false); 347 cpcap_charger_set_inductive_path(ddata, false); 348 349 error = cpcap_charger_set_state(ddata, 0, 0, 0); 350 if (error) 351 goto out_err; 352 353 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 354 CPCAP_REG_CRM_RVRSMODE, 355 CPCAP_REG_CRM_RVRSMODE); 356 if (error) 357 goto out_err; 358 } else { 359 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 360 CPCAP_REG_CRM_RVRSMODE, 0); 361 if (error) 362 goto out_err; 363 364 cpcap_charger_set_cable_path(ddata, true); 365 cpcap_charger_set_inductive_path(ddata, true); 366 } 367 368 return; 369 370 out_err: 371 dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__, 372 ddata->vbus_enabled ? "enable" : "disable", error); 373 } 374 375 static int cpcap_charger_set_vbus(struct phy_companion *comparator, 376 bool enabled) 377 { 378 struct cpcap_charger_ddata *ddata = 379 container_of(comparator, struct cpcap_charger_ddata, 380 comparator); 381 382 ddata->vbus_enabled = enabled; 383 schedule_delayed_work(&ddata->vbus_work, 0); 384 385 return 0; 386 } 387 388 /* Charger interrupt handling functions */ 389 390 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata, 391 struct cpcap_charger_ints_state *s) 392 { 393 int val, error; 394 395 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val); 396 if (error) 397 return error; 398 399 s->chrg_det = val & BIT(13); 400 s->rvrs_chrg = val & BIT(12); 401 s->vbusov = val & BIT(11); 402 403 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val); 404 if (error) 405 return error; 406 407 s->chrg_se1b = val & BIT(13); 408 s->rvrs_mode = val & BIT(6); 409 s->chrgcurr1 = val & BIT(4); 410 s->vbusvld = val & BIT(3); 411 412 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val); 413 if (error) 414 return error; 415 416 s->battdetb = val & BIT(6); 417 418 return 0; 419 } 420 421 static void cpcap_usb_detect(struct work_struct *work) 422 { 423 struct cpcap_charger_ddata *ddata; 424 struct cpcap_charger_ints_state s; 425 int error; 426 427 ddata = container_of(work, struct cpcap_charger_ddata, 428 detect_work.work); 429 430 error = cpcap_charger_get_ints_state(ddata, &s); 431 if (error) 432 return; 433 434 if (cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) { 435 int max_current; 436 437 if (cpcap_charger_battery_found(ddata)) 438 max_current = CPCAP_REG_CRM_ICHRG_1A596; 439 else 440 max_current = CPCAP_REG_CRM_ICHRG_0A532; 441 442 error = cpcap_charger_set_state(ddata, 443 CPCAP_REG_CRM_VCHRG_4V35, 444 max_current, 0); 445 if (error) 446 goto out_err; 447 } else { 448 error = cpcap_charger_set_state(ddata, 0, 0, 0); 449 if (error) 450 goto out_err; 451 } 452 453 power_supply_changed(ddata->usb); 454 return; 455 456 out_err: 457 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 458 } 459 460 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data) 461 { 462 struct cpcap_charger_ddata *ddata = data; 463 464 if (!atomic_read(&ddata->active)) 465 return IRQ_NONE; 466 467 schedule_delayed_work(&ddata->detect_work, 0); 468 469 return IRQ_HANDLED; 470 } 471 472 static int cpcap_usb_init_irq(struct platform_device *pdev, 473 struct cpcap_charger_ddata *ddata, 474 const char *name) 475 { 476 struct cpcap_interrupt_desc *d; 477 int irq, error; 478 479 irq = platform_get_irq_byname(pdev, name); 480 if (irq < 0) 481 return -ENODEV; 482 483 error = devm_request_threaded_irq(ddata->dev, irq, NULL, 484 cpcap_charger_irq_thread, 485 IRQF_SHARED, 486 name, ddata); 487 if (error) { 488 dev_err(ddata->dev, "could not get irq %s: %i\n", 489 name, error); 490 491 return error; 492 } 493 494 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL); 495 if (!d) 496 return -ENOMEM; 497 498 d->name = name; 499 d->irq = irq; 500 list_add(&d->node, &ddata->irq_list); 501 502 return 0; 503 } 504 505 static const char * const cpcap_charger_irqs[] = { 506 /* REG_INT_0 */ 507 "chrg_det", "rvrs_chrg", 508 509 /* REG_INT1 */ 510 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld", 511 512 /* REG_INT_3 */ 513 "battdetb", 514 }; 515 516 static int cpcap_usb_init_interrupts(struct platform_device *pdev, 517 struct cpcap_charger_ddata *ddata) 518 { 519 int i, error; 520 521 for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) { 522 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]); 523 if (error) 524 return error; 525 } 526 527 return 0; 528 } 529 530 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata) 531 { 532 int i; 533 534 for (i = 0; i < 2; i++) { 535 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", 536 i, GPIOD_OUT_HIGH); 537 if (IS_ERR(ddata->gpio[i])) { 538 dev_info(ddata->dev, "no mode change GPIO%i: %li\n", 539 i, PTR_ERR(ddata->gpio[i])); 540 ddata->gpio[i] = NULL; 541 } 542 } 543 } 544 545 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata) 546 { 547 const char * const names[CPCAP_CHARGER_IIO_NR] = { 548 "battdetb", "battp", "vbus", "chg_isense", "batti", 549 }; 550 int error, i; 551 552 for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) { 553 ddata->channels[i] = devm_iio_channel_get(ddata->dev, 554 names[i]); 555 if (IS_ERR(ddata->channels[i])) { 556 error = PTR_ERR(ddata->channels[i]); 557 goto out_err; 558 } 559 560 if (!ddata->channels[i]->indio_dev) { 561 error = -ENXIO; 562 goto out_err; 563 } 564 } 565 566 return 0; 567 568 out_err: 569 if (error != -EPROBE_DEFER) 570 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", 571 error); 572 573 return error; 574 } 575 576 static const struct power_supply_desc cpcap_charger_usb_desc = { 577 .name = "usb", 578 .type = POWER_SUPPLY_TYPE_USB, 579 .properties = cpcap_charger_props, 580 .num_properties = ARRAY_SIZE(cpcap_charger_props), 581 .get_property = cpcap_charger_get_property, 582 }; 583 584 #ifdef CONFIG_OF 585 static const struct of_device_id cpcap_charger_id_table[] = { 586 { 587 .compatible = "motorola,mapphone-cpcap-charger", 588 }, 589 {}, 590 }; 591 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table); 592 #endif 593 594 static int cpcap_charger_probe(struct platform_device *pdev) 595 { 596 struct cpcap_charger_ddata *ddata; 597 const struct of_device_id *of_id; 598 struct power_supply_config psy_cfg = {}; 599 int error; 600 601 of_id = of_match_device(of_match_ptr(cpcap_charger_id_table), 602 &pdev->dev); 603 if (!of_id) 604 return -EINVAL; 605 606 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 607 if (!ddata) 608 return -ENOMEM; 609 610 ddata->dev = &pdev->dev; 611 612 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL); 613 if (!ddata->reg) 614 return -ENODEV; 615 616 INIT_LIST_HEAD(&ddata->irq_list); 617 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); 618 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work); 619 platform_set_drvdata(pdev, ddata); 620 621 error = cpcap_charger_init_iio(ddata); 622 if (error) 623 return error; 624 625 atomic_set(&ddata->active, 1); 626 627 psy_cfg.of_node = pdev->dev.of_node; 628 psy_cfg.drv_data = ddata; 629 630 ddata->usb = devm_power_supply_register(ddata->dev, 631 &cpcap_charger_usb_desc, 632 &psy_cfg); 633 if (IS_ERR(ddata->usb)) { 634 error = PTR_ERR(ddata->usb); 635 dev_err(ddata->dev, "failed to register USB charger: %i\n", 636 error); 637 638 return error; 639 } 640 641 error = cpcap_usb_init_interrupts(pdev, ddata); 642 if (error) 643 return error; 644 645 ddata->comparator.set_vbus = cpcap_charger_set_vbus; 646 error = omap_usb2_set_comparator(&ddata->comparator); 647 if (error == -ENODEV) { 648 dev_info(ddata->dev, "charger needs phy, deferring probe\n"); 649 return -EPROBE_DEFER; 650 } 651 652 cpcap_charger_init_optional_gpios(ddata); 653 654 schedule_delayed_work(&ddata->detect_work, 0); 655 656 return 0; 657 } 658 659 static int cpcap_charger_remove(struct platform_device *pdev) 660 { 661 struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev); 662 int error; 663 664 atomic_set(&ddata->active, 0); 665 error = omap_usb2_set_comparator(NULL); 666 if (error) 667 dev_warn(ddata->dev, "could not clear USB comparator: %i\n", 668 error); 669 670 error = cpcap_charger_set_state(ddata, 0, 0, 0); 671 if (error) 672 dev_warn(ddata->dev, "could not clear charger: %i\n", 673 error); 674 cancel_delayed_work_sync(&ddata->vbus_work); 675 cancel_delayed_work_sync(&ddata->detect_work); 676 677 return 0; 678 } 679 680 static struct platform_driver cpcap_charger_driver = { 681 .probe = cpcap_charger_probe, 682 .driver = { 683 .name = "cpcap-charger", 684 .of_match_table = of_match_ptr(cpcap_charger_id_table), 685 }, 686 .remove = cpcap_charger_remove, 687 }; 688 module_platform_driver(cpcap_charger_driver); 689 690 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 691 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver"); 692 MODULE_LICENSE("GPL v2"); 693 MODULE_ALIAS("platform:cpcap-charger"); 694