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 /* CPCAP_REG_VUSBC register bits needed for VBUS */ 112 #define CPCAP_BIT_VBUS_SWITCH BIT(0) /* VBUS boost to 5V */ 113 114 enum { 115 CPCAP_CHARGER_IIO_BATTDET, 116 CPCAP_CHARGER_IIO_VOLTAGE, 117 CPCAP_CHARGER_IIO_VBUS, 118 CPCAP_CHARGER_IIO_CHRG_CURRENT, 119 CPCAP_CHARGER_IIO_BATT_CURRENT, 120 CPCAP_CHARGER_IIO_NR, 121 }; 122 123 struct cpcap_charger_ddata { 124 struct device *dev; 125 struct regmap *reg; 126 struct list_head irq_list; 127 struct delayed_work detect_work; 128 struct delayed_work vbus_work; 129 struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */ 130 131 struct iio_channel *channels[CPCAP_CHARGER_IIO_NR]; 132 133 struct power_supply *usb; 134 135 struct phy_companion comparator; /* For USB VBUS */ 136 unsigned int vbus_enabled:1; 137 unsigned int feeding_vbus:1; 138 atomic_t active; 139 140 int status; 141 }; 142 143 struct cpcap_interrupt_desc { 144 int irq; 145 struct list_head node; 146 const char *name; 147 }; 148 149 struct cpcap_charger_ints_state { 150 bool chrg_det; 151 bool rvrs_chrg; 152 bool vbusov; 153 154 bool chrg_se1b; 155 bool rvrs_mode; 156 bool chrgcurr1; 157 bool vbusvld; 158 159 bool battdetb; 160 }; 161 162 static enum power_supply_property cpcap_charger_props[] = { 163 POWER_SUPPLY_PROP_STATUS, 164 POWER_SUPPLY_PROP_ONLINE, 165 POWER_SUPPLY_PROP_VOLTAGE_NOW, 166 POWER_SUPPLY_PROP_CURRENT_NOW, 167 }; 168 169 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata) 170 { 171 struct iio_channel *channel; 172 int error, value; 173 174 channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET]; 175 error = iio_read_channel_raw(channel, &value); 176 if (error < 0) { 177 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 178 179 return false; 180 } 181 182 return value == 1; 183 } 184 185 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata) 186 { 187 struct iio_channel *channel; 188 int error, value = 0; 189 190 channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE]; 191 error = iio_read_channel_processed(channel, &value); 192 if (error < 0) { 193 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 194 195 return 0; 196 } 197 198 return value; 199 } 200 201 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata) 202 { 203 struct iio_channel *channel; 204 int error, value = 0; 205 206 channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT]; 207 error = iio_read_channel_processed(channel, &value); 208 if (error < 0) { 209 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 210 211 return 0; 212 } 213 214 return value; 215 } 216 217 static int cpcap_charger_get_property(struct power_supply *psy, 218 enum power_supply_property psp, 219 union power_supply_propval *val) 220 { 221 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent); 222 223 switch (psp) { 224 case POWER_SUPPLY_PROP_STATUS: 225 val->intval = ddata->status; 226 break; 227 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 228 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) 229 val->intval = cpcap_charger_get_charge_voltage(ddata) * 230 1000; 231 else 232 val->intval = 0; 233 break; 234 case POWER_SUPPLY_PROP_CURRENT_NOW: 235 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) 236 val->intval = cpcap_charger_get_charge_current(ddata) * 237 1000; 238 else 239 val->intval = 0; 240 break; 241 case POWER_SUPPLY_PROP_ONLINE: 242 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING; 243 break; 244 default: 245 return -EINVAL; 246 } 247 248 return 0; 249 } 250 251 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata, 252 bool enabled) 253 { 254 if (!ddata->gpio[0]) 255 return; 256 257 gpiod_set_value(ddata->gpio[0], enabled); 258 } 259 260 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata, 261 bool enabled) 262 { 263 if (!ddata->gpio[1]) 264 return; 265 266 gpiod_set_value(ddata->gpio[1], enabled); 267 } 268 269 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata, 270 int max_voltage, int charge_current, 271 int trickle_current) 272 { 273 bool enable; 274 int error; 275 276 enable = (charge_current || trickle_current); 277 dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable); 278 279 if (!enable) { 280 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 281 0x3fff, 282 CPCAP_REG_CRM_FET_OVRD | 283 CPCAP_REG_CRM_FET_CTRL); 284 if (error) { 285 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN; 286 goto out_err; 287 } 288 289 ddata->status = POWER_SUPPLY_STATUS_DISCHARGING; 290 291 return 0; 292 } 293 294 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, 295 CPCAP_REG_CRM_CHRG_LED_EN | 296 trickle_current | 297 CPCAP_REG_CRM_FET_OVRD | 298 CPCAP_REG_CRM_FET_CTRL | 299 max_voltage | 300 charge_current); 301 if (error) { 302 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN; 303 goto out_err; 304 } 305 306 ddata->status = POWER_SUPPLY_STATUS_CHARGING; 307 308 return 0; 309 310 out_err: 311 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 312 313 return error; 314 } 315 316 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata) 317 { 318 int error, value = 0; 319 struct iio_channel *channel = 320 ddata->channels[CPCAP_CHARGER_IIO_VBUS]; 321 322 error = iio_read_channel_processed(channel, &value); 323 if (error >= 0) 324 return value > 3900 ? true : false; 325 326 dev_err(ddata->dev, "error reading VBUS: %i\n", error); 327 328 return false; 329 } 330 331 /* VBUS control functions for the USB PHY companion */ 332 static void cpcap_charger_vbus_work(struct work_struct *work) 333 { 334 struct cpcap_charger_ddata *ddata; 335 bool vbus = false; 336 int error; 337 338 ddata = container_of(work, struct cpcap_charger_ddata, 339 vbus_work.work); 340 341 if (ddata->vbus_enabled) { 342 vbus = cpcap_charger_vbus_valid(ddata); 343 if (vbus) { 344 dev_info(ddata->dev, "VBUS already provided\n"); 345 346 return; 347 } 348 349 ddata->feeding_vbus = true; 350 cpcap_charger_set_cable_path(ddata, false); 351 cpcap_charger_set_inductive_path(ddata, false); 352 353 error = cpcap_charger_set_state(ddata, 0, 0, 0); 354 if (error) 355 goto out_err; 356 357 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC, 358 CPCAP_BIT_VBUS_SWITCH, 359 CPCAP_BIT_VBUS_SWITCH); 360 if (error) 361 goto out_err; 362 363 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 364 CPCAP_REG_CRM_RVRSMODE, 365 CPCAP_REG_CRM_RVRSMODE); 366 if (error) 367 goto out_err; 368 } else { 369 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC, 370 CPCAP_BIT_VBUS_SWITCH, 0); 371 if (error) 372 goto out_err; 373 374 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 375 CPCAP_REG_CRM_RVRSMODE, 0); 376 if (error) 377 goto out_err; 378 379 cpcap_charger_set_cable_path(ddata, true); 380 cpcap_charger_set_inductive_path(ddata, true); 381 ddata->feeding_vbus = false; 382 } 383 384 return; 385 386 out_err: 387 dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__, 388 ddata->vbus_enabled ? "enable" : "disable", error); 389 } 390 391 static int cpcap_charger_set_vbus(struct phy_companion *comparator, 392 bool enabled) 393 { 394 struct cpcap_charger_ddata *ddata = 395 container_of(comparator, struct cpcap_charger_ddata, 396 comparator); 397 398 ddata->vbus_enabled = enabled; 399 schedule_delayed_work(&ddata->vbus_work, 0); 400 401 return 0; 402 } 403 404 /* Charger interrupt handling functions */ 405 406 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata, 407 struct cpcap_charger_ints_state *s) 408 { 409 int val, error; 410 411 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val); 412 if (error) 413 return error; 414 415 s->chrg_det = val & BIT(13); 416 s->rvrs_chrg = val & BIT(12); 417 s->vbusov = val & BIT(11); 418 419 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val); 420 if (error) 421 return error; 422 423 s->chrg_se1b = val & BIT(13); 424 s->rvrs_mode = val & BIT(6); 425 s->chrgcurr1 = val & BIT(4); 426 s->vbusvld = val & BIT(3); 427 428 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val); 429 if (error) 430 return error; 431 432 s->battdetb = val & BIT(6); 433 434 return 0; 435 } 436 437 static void cpcap_usb_detect(struct work_struct *work) 438 { 439 struct cpcap_charger_ddata *ddata; 440 struct cpcap_charger_ints_state s; 441 int error; 442 443 ddata = container_of(work, struct cpcap_charger_ddata, 444 detect_work.work); 445 446 error = cpcap_charger_get_ints_state(ddata, &s); 447 if (error) 448 return; 449 450 if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) && 451 s.chrgcurr1) { 452 int max_current; 453 454 if (cpcap_charger_battery_found(ddata)) 455 max_current = CPCAP_REG_CRM_ICHRG_1A596; 456 else 457 max_current = CPCAP_REG_CRM_ICHRG_0A532; 458 459 error = cpcap_charger_set_state(ddata, 460 CPCAP_REG_CRM_VCHRG_4V35, 461 max_current, 0); 462 if (error) 463 goto out_err; 464 } else { 465 error = cpcap_charger_set_state(ddata, 0, 0, 0); 466 if (error) 467 goto out_err; 468 } 469 470 power_supply_changed(ddata->usb); 471 return; 472 473 out_err: 474 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 475 } 476 477 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data) 478 { 479 struct cpcap_charger_ddata *ddata = data; 480 481 if (!atomic_read(&ddata->active)) 482 return IRQ_NONE; 483 484 schedule_delayed_work(&ddata->detect_work, 0); 485 486 return IRQ_HANDLED; 487 } 488 489 static int cpcap_usb_init_irq(struct platform_device *pdev, 490 struct cpcap_charger_ddata *ddata, 491 const char *name) 492 { 493 struct cpcap_interrupt_desc *d; 494 int irq, error; 495 496 irq = platform_get_irq_byname(pdev, name); 497 if (irq < 0) 498 return -ENODEV; 499 500 error = devm_request_threaded_irq(ddata->dev, irq, NULL, 501 cpcap_charger_irq_thread, 502 IRQF_SHARED, 503 name, ddata); 504 if (error) { 505 dev_err(ddata->dev, "could not get irq %s: %i\n", 506 name, error); 507 508 return error; 509 } 510 511 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL); 512 if (!d) 513 return -ENOMEM; 514 515 d->name = name; 516 d->irq = irq; 517 list_add(&d->node, &ddata->irq_list); 518 519 return 0; 520 } 521 522 static const char * const cpcap_charger_irqs[] = { 523 /* REG_INT_0 */ 524 "chrg_det", "rvrs_chrg", 525 526 /* REG_INT1 */ 527 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld", 528 529 /* REG_INT_3 */ 530 "battdetb", 531 }; 532 533 static int cpcap_usb_init_interrupts(struct platform_device *pdev, 534 struct cpcap_charger_ddata *ddata) 535 { 536 int i, error; 537 538 for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) { 539 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]); 540 if (error) 541 return error; 542 } 543 544 return 0; 545 } 546 547 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata) 548 { 549 int i; 550 551 for (i = 0; i < 2; i++) { 552 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", 553 i, GPIOD_OUT_HIGH); 554 if (IS_ERR(ddata->gpio[i])) { 555 dev_info(ddata->dev, "no mode change GPIO%i: %li\n", 556 i, PTR_ERR(ddata->gpio[i])); 557 ddata->gpio[i] = NULL; 558 } 559 } 560 } 561 562 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata) 563 { 564 const char * const names[CPCAP_CHARGER_IIO_NR] = { 565 "battdetb", "battp", "vbus", "chg_isense", "batti", 566 }; 567 int error, i; 568 569 for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) { 570 ddata->channels[i] = devm_iio_channel_get(ddata->dev, 571 names[i]); 572 if (IS_ERR(ddata->channels[i])) { 573 error = PTR_ERR(ddata->channels[i]); 574 goto out_err; 575 } 576 577 if (!ddata->channels[i]->indio_dev) { 578 error = -ENXIO; 579 goto out_err; 580 } 581 } 582 583 return 0; 584 585 out_err: 586 if (error != -EPROBE_DEFER) 587 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", 588 error); 589 590 return error; 591 } 592 593 static const struct power_supply_desc cpcap_charger_usb_desc = { 594 .name = "usb", 595 .type = POWER_SUPPLY_TYPE_USB, 596 .properties = cpcap_charger_props, 597 .num_properties = ARRAY_SIZE(cpcap_charger_props), 598 .get_property = cpcap_charger_get_property, 599 }; 600 601 #ifdef CONFIG_OF 602 static const struct of_device_id cpcap_charger_id_table[] = { 603 { 604 .compatible = "motorola,mapphone-cpcap-charger", 605 }, 606 {}, 607 }; 608 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table); 609 #endif 610 611 static int cpcap_charger_probe(struct platform_device *pdev) 612 { 613 struct cpcap_charger_ddata *ddata; 614 const struct of_device_id *of_id; 615 struct power_supply_config psy_cfg = {}; 616 int error; 617 618 of_id = of_match_device(of_match_ptr(cpcap_charger_id_table), 619 &pdev->dev); 620 if (!of_id) 621 return -EINVAL; 622 623 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 624 if (!ddata) 625 return -ENOMEM; 626 627 ddata->dev = &pdev->dev; 628 629 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL); 630 if (!ddata->reg) 631 return -ENODEV; 632 633 INIT_LIST_HEAD(&ddata->irq_list); 634 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); 635 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work); 636 platform_set_drvdata(pdev, ddata); 637 638 error = cpcap_charger_init_iio(ddata); 639 if (error) 640 return error; 641 642 atomic_set(&ddata->active, 1); 643 644 psy_cfg.of_node = pdev->dev.of_node; 645 psy_cfg.drv_data = ddata; 646 647 ddata->usb = devm_power_supply_register(ddata->dev, 648 &cpcap_charger_usb_desc, 649 &psy_cfg); 650 if (IS_ERR(ddata->usb)) { 651 error = PTR_ERR(ddata->usb); 652 dev_err(ddata->dev, "failed to register USB charger: %i\n", 653 error); 654 655 return error; 656 } 657 658 error = cpcap_usb_init_interrupts(pdev, ddata); 659 if (error) 660 return error; 661 662 ddata->comparator.set_vbus = cpcap_charger_set_vbus; 663 error = omap_usb2_set_comparator(&ddata->comparator); 664 if (error == -ENODEV) { 665 dev_info(ddata->dev, "charger needs phy, deferring probe\n"); 666 return -EPROBE_DEFER; 667 } 668 669 cpcap_charger_init_optional_gpios(ddata); 670 671 schedule_delayed_work(&ddata->detect_work, 0); 672 673 return 0; 674 } 675 676 static int cpcap_charger_remove(struct platform_device *pdev) 677 { 678 struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev); 679 int error; 680 681 atomic_set(&ddata->active, 0); 682 error = omap_usb2_set_comparator(NULL); 683 if (error) 684 dev_warn(ddata->dev, "could not clear USB comparator: %i\n", 685 error); 686 687 error = cpcap_charger_set_state(ddata, 0, 0, 0); 688 if (error) 689 dev_warn(ddata->dev, "could not clear charger: %i\n", 690 error); 691 cancel_delayed_work_sync(&ddata->vbus_work); 692 cancel_delayed_work_sync(&ddata->detect_work); 693 694 return 0; 695 } 696 697 static struct platform_driver cpcap_charger_driver = { 698 .probe = cpcap_charger_probe, 699 .driver = { 700 .name = "cpcap-charger", 701 .of_match_table = of_match_ptr(cpcap_charger_id_table), 702 }, 703 .remove = cpcap_charger_remove, 704 }; 705 module_platform_driver(cpcap_charger_driver); 706 707 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 708 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver"); 709 MODULE_LICENSE("GPL v2"); 710 MODULE_ALIAS("platform:cpcap-charger"); 711