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 * Except 70mA and 1.596A and unlimited, these are simply 88.7mA / step. 94 */ 95 #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0) 96 #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0) 97 #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1) 98 #define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2) 99 #define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3) 100 #define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4) 101 #define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5) 102 #define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6) 103 #define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7) 104 #define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8) 105 #define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9) 106 #define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa) 107 #define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb) 108 #define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc) 109 #define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd) 110 #define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe) 111 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf) 112 113 /* CPCAP_REG_VUSBC register bits needed for VBUS */ 114 #define CPCAP_BIT_VBUS_SWITCH BIT(0) /* VBUS boost to 5V */ 115 116 enum { 117 CPCAP_CHARGER_IIO_BATTDET, 118 CPCAP_CHARGER_IIO_VOLTAGE, 119 CPCAP_CHARGER_IIO_VBUS, 120 CPCAP_CHARGER_IIO_CHRG_CURRENT, 121 CPCAP_CHARGER_IIO_BATT_CURRENT, 122 CPCAP_CHARGER_IIO_NR, 123 }; 124 125 struct cpcap_charger_ddata { 126 struct device *dev; 127 struct regmap *reg; 128 struct list_head irq_list; 129 struct delayed_work detect_work; 130 struct delayed_work vbus_work; 131 struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */ 132 133 struct iio_channel *channels[CPCAP_CHARGER_IIO_NR]; 134 135 struct power_supply *usb; 136 137 struct phy_companion comparator; /* For USB VBUS */ 138 unsigned int vbus_enabled:1; 139 unsigned int feeding_vbus:1; 140 atomic_t active; 141 142 int status; 143 int voltage; 144 int limit_current; 145 }; 146 147 struct cpcap_interrupt_desc { 148 int irq; 149 struct list_head node; 150 const char *name; 151 }; 152 153 struct cpcap_charger_ints_state { 154 bool chrg_det; 155 bool rvrs_chrg; 156 bool vbusov; 157 158 bool chrg_se1b; 159 bool rvrs_mode; 160 bool chrgcurr2; 161 bool chrgcurr1; 162 bool vbusvld; 163 164 bool battdetb; 165 }; 166 167 static enum power_supply_property cpcap_charger_props[] = { 168 POWER_SUPPLY_PROP_STATUS, 169 POWER_SUPPLY_PROP_ONLINE, 170 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 171 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 172 POWER_SUPPLY_PROP_VOLTAGE_NOW, 173 POWER_SUPPLY_PROP_CURRENT_NOW, 174 }; 175 176 /* No battery always shows temperature of -40000 */ 177 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata) 178 { 179 struct iio_channel *channel; 180 int error, temperature; 181 182 channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET]; 183 error = iio_read_channel_processed(channel, &temperature); 184 if (error < 0) { 185 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 186 187 return false; 188 } 189 190 return temperature > -20000 && temperature < 60000; 191 } 192 193 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata) 194 { 195 struct iio_channel *channel; 196 int error, value = 0; 197 198 channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE]; 199 error = iio_read_channel_processed(channel, &value); 200 if (error < 0) { 201 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 202 203 return 0; 204 } 205 206 return value; 207 } 208 209 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata) 210 { 211 struct iio_channel *channel; 212 int error, value = 0; 213 214 channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT]; 215 error = iio_read_channel_processed(channel, &value); 216 if (error < 0) { 217 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); 218 219 return 0; 220 } 221 222 return value; 223 } 224 225 static int cpcap_charger_get_property(struct power_supply *psy, 226 enum power_supply_property psp, 227 union power_supply_propval *val) 228 { 229 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent); 230 231 switch (psp) { 232 case POWER_SUPPLY_PROP_STATUS: 233 val->intval = ddata->status; 234 break; 235 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 236 val->intval = ddata->limit_current; 237 break; 238 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 239 val->intval = ddata->voltage; 240 break; 241 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 242 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) 243 val->intval = cpcap_charger_get_charge_voltage(ddata) * 244 1000; 245 else 246 val->intval = 0; 247 break; 248 case POWER_SUPPLY_PROP_CURRENT_NOW: 249 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) 250 val->intval = cpcap_charger_get_charge_current(ddata) * 251 1000; 252 else 253 val->intval = 0; 254 break; 255 case POWER_SUPPLY_PROP_ONLINE: 256 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING; 257 break; 258 default: 259 return -EINVAL; 260 } 261 262 return 0; 263 } 264 265 static int cpcap_charger_match_voltage(int voltage) 266 { 267 switch (voltage) { 268 case 0 ... 4100000 - 1: return 3800000; 269 case 4100000 ... 4120000 - 1: return 4100000; 270 case 4120000 ... 4150000 - 1: return 4120000; 271 case 4150000 ... 4170000 - 1: return 4150000; 272 case 4170000 ... 4200000 - 1: return 4170000; 273 case 4200000 ... 4230000 - 1: return 4200000; 274 case 4230000 ... 4250000 - 1: return 4230000; 275 case 4250000 ... 4270000 - 1: return 4250000; 276 case 4270000 ... 4300000 - 1: return 4270000; 277 case 4300000 ... 4330000 - 1: return 4300000; 278 case 4330000 ... 4350000 - 1: return 4330000; 279 case 4350000 ... 4380000 - 1: return 4350000; 280 case 4380000 ... 4400000 - 1: return 4380000; 281 case 4400000 ... 4420000 - 1: return 4400000; 282 case 4420000 ... 4440000 - 1: return 4420000; 283 case 4440000: return 4440000; 284 default: return 0; 285 } 286 } 287 288 static int 289 cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata) 290 { 291 union power_supply_propval prop; 292 struct power_supply *battery; 293 int voltage = ddata->voltage; 294 int error; 295 296 battery = power_supply_get_by_name("battery"); 297 if (battery) { 298 error = power_supply_get_property(battery, 299 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 300 &prop); 301 if (!error) 302 voltage = prop.intval; 303 304 power_supply_put(battery); 305 } 306 307 return voltage; 308 } 309 310 static int cpcap_charger_current_to_regval(int microamp) 311 { 312 int miliamp = microamp / 1000; 313 int res; 314 315 if (miliamp < 0) 316 return -EINVAL; 317 if (miliamp < 70) 318 return CPCAP_REG_CRM_ICHRG(0x0); 319 if (miliamp < 177) 320 return CPCAP_REG_CRM_ICHRG(0x1); 321 if (miliamp >= 1596) 322 return CPCAP_REG_CRM_ICHRG(0xe); 323 324 res = microamp / 88666; 325 if (res > 0xd) 326 res = 0xd; 327 return CPCAP_REG_CRM_ICHRG(res); 328 } 329 330 static int cpcap_charger_set_property(struct power_supply *psy, 331 enum power_supply_property psp, 332 const union power_supply_propval *val) 333 { 334 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent); 335 int voltage, batvolt; 336 337 switch (psp) { 338 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 339 if (cpcap_charger_current_to_regval(val->intval) < 0) 340 return -EINVAL; 341 ddata->limit_current = val->intval; 342 schedule_delayed_work(&ddata->detect_work, 0); 343 break; 344 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 345 voltage = cpcap_charger_match_voltage(val->intval); 346 batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata); 347 if (voltage > batvolt) 348 voltage = batvolt; 349 ddata->voltage = voltage; 350 schedule_delayed_work(&ddata->detect_work, 0); 351 break; 352 default: 353 return -EINVAL; 354 } 355 356 return 0; 357 } 358 359 static int cpcap_charger_property_is_writeable(struct power_supply *psy, 360 enum power_supply_property psp) 361 { 362 switch (psp) { 363 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 364 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 365 return 1; 366 default: 367 return 0; 368 } 369 } 370 371 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata, 372 bool enabled) 373 { 374 if (!ddata->gpio[0]) 375 return; 376 377 gpiod_set_value(ddata->gpio[0], enabled); 378 } 379 380 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata, 381 bool enabled) 382 { 383 if (!ddata->gpio[1]) 384 return; 385 386 gpiod_set_value(ddata->gpio[1], enabled); 387 } 388 389 static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata, 390 int state) 391 { 392 const char *status; 393 394 if (state > POWER_SUPPLY_STATUS_FULL) { 395 dev_warn(ddata->dev, "unknown state: %i\n", state); 396 397 return; 398 } 399 400 ddata->status = state; 401 402 switch (state) { 403 case POWER_SUPPLY_STATUS_DISCHARGING: 404 status = "DISCONNECTED"; 405 break; 406 case POWER_SUPPLY_STATUS_NOT_CHARGING: 407 status = "DETECTING"; 408 break; 409 case POWER_SUPPLY_STATUS_CHARGING: 410 status = "CHARGING"; 411 break; 412 case POWER_SUPPLY_STATUS_FULL: 413 status = "DONE"; 414 break; 415 default: 416 return; 417 } 418 419 dev_dbg(ddata->dev, "state: %s\n", status); 420 } 421 422 static int cpcap_charger_disable(struct cpcap_charger_ddata *ddata) 423 { 424 int error; 425 426 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, 427 CPCAP_REG_CRM_FET_OVRD | 428 CPCAP_REG_CRM_FET_CTRL); 429 if (error) 430 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 431 432 return error; 433 } 434 435 static int cpcap_charger_enable(struct cpcap_charger_ddata *ddata, 436 int max_voltage, int charge_current, 437 int trickle_current) 438 { 439 int error; 440 441 if (!max_voltage || !charge_current) 442 return -EINVAL; 443 444 dev_dbg(ddata->dev, "enable: %i %i %i\n", 445 max_voltage, charge_current, trickle_current); 446 447 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, 448 CPCAP_REG_CRM_CHRG_LED_EN | 449 trickle_current | 450 CPCAP_REG_CRM_FET_OVRD | 451 CPCAP_REG_CRM_FET_CTRL | 452 max_voltage | 453 charge_current); 454 if (error) 455 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 456 457 return error; 458 } 459 460 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata) 461 { 462 int error, value = 0; 463 struct iio_channel *channel = 464 ddata->channels[CPCAP_CHARGER_IIO_VBUS]; 465 466 error = iio_read_channel_processed(channel, &value); 467 if (error >= 0) 468 return value > 3900; 469 470 dev_err(ddata->dev, "error reading VBUS: %i\n", error); 471 472 return false; 473 } 474 475 /* VBUS control functions for the USB PHY companion */ 476 static void cpcap_charger_vbus_work(struct work_struct *work) 477 { 478 struct cpcap_charger_ddata *ddata; 479 bool vbus = false; 480 int error; 481 482 ddata = container_of(work, struct cpcap_charger_ddata, 483 vbus_work.work); 484 485 if (ddata->vbus_enabled) { 486 vbus = cpcap_charger_vbus_valid(ddata); 487 if (vbus) { 488 dev_dbg(ddata->dev, "VBUS already provided\n"); 489 490 return; 491 } 492 493 ddata->feeding_vbus = true; 494 cpcap_charger_set_cable_path(ddata, false); 495 cpcap_charger_set_inductive_path(ddata, false); 496 497 error = cpcap_charger_disable(ddata); 498 if (error) 499 goto out_err; 500 501 cpcap_charger_update_state(ddata, 502 POWER_SUPPLY_STATUS_DISCHARGING); 503 504 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC, 505 CPCAP_BIT_VBUS_SWITCH, 506 CPCAP_BIT_VBUS_SWITCH); 507 if (error) 508 goto out_err; 509 510 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 511 CPCAP_REG_CRM_RVRSMODE, 512 CPCAP_REG_CRM_RVRSMODE); 513 if (error) 514 goto out_err; 515 } else { 516 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC, 517 CPCAP_BIT_VBUS_SWITCH, 0); 518 if (error) 519 goto out_err; 520 521 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 522 CPCAP_REG_CRM_RVRSMODE, 0); 523 if (error) 524 goto out_err; 525 526 cpcap_charger_set_cable_path(ddata, true); 527 cpcap_charger_set_inductive_path(ddata, true); 528 ddata->feeding_vbus = false; 529 } 530 531 return; 532 533 out_err: 534 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); 535 dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__, 536 ddata->vbus_enabled ? "enable" : "disable", error); 537 } 538 539 static int cpcap_charger_set_vbus(struct phy_companion *comparator, 540 bool enabled) 541 { 542 struct cpcap_charger_ddata *ddata = 543 container_of(comparator, struct cpcap_charger_ddata, 544 comparator); 545 546 ddata->vbus_enabled = enabled; 547 schedule_delayed_work(&ddata->vbus_work, 0); 548 549 return 0; 550 } 551 552 /* Charger interrupt handling functions */ 553 554 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata, 555 struct cpcap_charger_ints_state *s) 556 { 557 int val, error; 558 559 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val); 560 if (error) 561 return error; 562 563 s->chrg_det = val & BIT(13); 564 s->rvrs_chrg = val & BIT(12); 565 s->vbusov = val & BIT(11); 566 567 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val); 568 if (error) 569 return error; 570 571 s->chrg_se1b = val & BIT(13); 572 s->rvrs_mode = val & BIT(6); 573 s->chrgcurr2 = val & BIT(5); 574 s->chrgcurr1 = val & BIT(4); 575 s->vbusvld = val & BIT(3); 576 577 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val); 578 if (error) 579 return error; 580 581 s->battdetb = val & BIT(6); 582 583 return 0; 584 } 585 586 static int cpcap_charger_voltage_to_regval(int voltage) 587 { 588 int offset; 589 590 switch (voltage) { 591 case 0 ... 4100000 - 1: 592 return 0; 593 case 4100000 ... 4200000 - 1: 594 offset = 1; 595 break; 596 case 4200000 ... 4300000 - 1: 597 offset = 0; 598 break; 599 case 4300000 ... 4380000 - 1: 600 offset = -1; 601 break; 602 case 4380000 ... 4440000: 603 offset = -2; 604 break; 605 default: 606 return 0; 607 } 608 609 return ((voltage - 4100000) / 20000) + offset; 610 } 611 612 static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata, 613 int state, unsigned long delay) 614 { 615 int error; 616 617 /* Update battery state before disconnecting the charger */ 618 switch (state) { 619 case POWER_SUPPLY_STATUS_DISCHARGING: 620 case POWER_SUPPLY_STATUS_FULL: 621 power_supply_changed(ddata->usb); 622 break; 623 default: 624 break; 625 } 626 627 error = cpcap_charger_disable(ddata); 628 if (error) { 629 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); 630 return; 631 } 632 633 cpcap_charger_update_state(ddata, state); 634 power_supply_changed(ddata->usb); 635 schedule_delayed_work(&ddata->detect_work, delay); 636 } 637 638 static void cpcap_usb_detect(struct work_struct *work) 639 { 640 struct cpcap_charger_ddata *ddata; 641 struct cpcap_charger_ints_state s; 642 int error, new_state; 643 644 ddata = container_of(work, struct cpcap_charger_ddata, 645 detect_work.work); 646 647 error = cpcap_charger_get_ints_state(ddata, &s); 648 if (error) 649 return; 650 651 /* Just init the state if a charger is connected with no chrg_det set */ 652 if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) { 653 cpcap_charger_update_state(ddata, 654 POWER_SUPPLY_STATUS_NOT_CHARGING); 655 656 return; 657 } 658 659 /* 660 * If battery voltage is higher than charge voltage, it may have been 661 * charged to 4.35V by Android. Try again in 10 minutes. 662 */ 663 if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) { 664 cpcap_charger_disconnect(ddata, 665 POWER_SUPPLY_STATUS_NOT_CHARGING, 666 HZ * 60 * 10); 667 668 return; 669 } 670 671 /* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */ 672 usleep_range(80000, 120000); 673 674 /* Throttle chrgcurr2 interrupt for charger done and retry */ 675 switch (ddata->status) { 676 case POWER_SUPPLY_STATUS_CHARGING: 677 if (s.chrgcurr2) 678 break; 679 new_state = POWER_SUPPLY_STATUS_FULL; 680 681 if (s.chrgcurr1 && s.vbusvld) { 682 cpcap_charger_disconnect(ddata, new_state, HZ * 5); 683 return; 684 } 685 break; 686 case POWER_SUPPLY_STATUS_FULL: 687 if (!s.chrgcurr2) 688 break; 689 if (s.vbusvld) 690 new_state = POWER_SUPPLY_STATUS_NOT_CHARGING; 691 else 692 new_state = POWER_SUPPLY_STATUS_DISCHARGING; 693 694 cpcap_charger_disconnect(ddata, new_state, HZ * 5); 695 696 return; 697 default: 698 break; 699 } 700 701 if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) && 702 s.chrgcurr1) { 703 int max_current = 532000; 704 int vchrg, ichrg; 705 706 if (cpcap_charger_battery_found(ddata)) 707 max_current = 1596000; 708 709 if (max_current > ddata->limit_current) 710 max_current = ddata->limit_current; 711 712 ichrg = cpcap_charger_current_to_regval(max_current); 713 vchrg = cpcap_charger_voltage_to_regval(ddata->voltage); 714 error = cpcap_charger_enable(ddata, 715 CPCAP_REG_CRM_VCHRG(vchrg), 716 ichrg, 0); 717 if (error) 718 goto out_err; 719 cpcap_charger_update_state(ddata, 720 POWER_SUPPLY_STATUS_CHARGING); 721 } else { 722 error = cpcap_charger_disable(ddata); 723 if (error) 724 goto out_err; 725 cpcap_charger_update_state(ddata, 726 POWER_SUPPLY_STATUS_DISCHARGING); 727 } 728 729 power_supply_changed(ddata->usb); 730 return; 731 732 out_err: 733 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); 734 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 735 } 736 737 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data) 738 { 739 struct cpcap_charger_ddata *ddata = data; 740 741 if (!atomic_read(&ddata->active)) 742 return IRQ_NONE; 743 744 schedule_delayed_work(&ddata->detect_work, 0); 745 746 return IRQ_HANDLED; 747 } 748 749 static int cpcap_usb_init_irq(struct platform_device *pdev, 750 struct cpcap_charger_ddata *ddata, 751 const char *name) 752 { 753 struct cpcap_interrupt_desc *d; 754 int irq, error; 755 756 irq = platform_get_irq_byname(pdev, name); 757 if (irq < 0) 758 return -ENODEV; 759 760 error = devm_request_threaded_irq(ddata->dev, irq, NULL, 761 cpcap_charger_irq_thread, 762 IRQF_SHARED | IRQF_ONESHOT, 763 name, ddata); 764 if (error) { 765 dev_err(ddata->dev, "could not get irq %s: %i\n", 766 name, error); 767 768 return error; 769 } 770 771 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL); 772 if (!d) 773 return -ENOMEM; 774 775 d->name = name; 776 d->irq = irq; 777 list_add(&d->node, &ddata->irq_list); 778 779 return 0; 780 } 781 782 static const char * const cpcap_charger_irqs[] = { 783 /* REG_INT_0 */ 784 "chrg_det", "rvrs_chrg", 785 786 /* REG_INT1 */ 787 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld", 788 789 /* REG_INT_3 */ 790 "battdetb", 791 }; 792 793 static int cpcap_usb_init_interrupts(struct platform_device *pdev, 794 struct cpcap_charger_ddata *ddata) 795 { 796 int i, error; 797 798 for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) { 799 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]); 800 if (error) 801 return error; 802 } 803 804 return 0; 805 } 806 807 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata) 808 { 809 int i; 810 811 for (i = 0; i < 2; i++) { 812 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", 813 i, GPIOD_OUT_HIGH); 814 if (IS_ERR(ddata->gpio[i])) { 815 dev_info(ddata->dev, "no mode change GPIO%i: %li\n", 816 i, PTR_ERR(ddata->gpio[i])); 817 ddata->gpio[i] = NULL; 818 } 819 } 820 } 821 822 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata) 823 { 824 const char * const names[CPCAP_CHARGER_IIO_NR] = { 825 "battdetb", "battp", "vbus", "chg_isense", "batti", 826 }; 827 int error, i; 828 829 for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) { 830 ddata->channels[i] = devm_iio_channel_get(ddata->dev, 831 names[i]); 832 if (IS_ERR(ddata->channels[i])) { 833 error = PTR_ERR(ddata->channels[i]); 834 goto out_err; 835 } 836 837 if (!ddata->channels[i]->indio_dev) { 838 error = -ENXIO; 839 goto out_err; 840 } 841 } 842 843 return 0; 844 845 out_err: 846 if (error != -EPROBE_DEFER) 847 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", 848 error); 849 850 return error; 851 } 852 853 static char *cpcap_charger_supplied_to[] = { 854 "battery", 855 }; 856 857 static const struct power_supply_desc cpcap_charger_usb_desc = { 858 .name = "usb", 859 .type = POWER_SUPPLY_TYPE_USB, 860 .properties = cpcap_charger_props, 861 .num_properties = ARRAY_SIZE(cpcap_charger_props), 862 .get_property = cpcap_charger_get_property, 863 .set_property = cpcap_charger_set_property, 864 .property_is_writeable = cpcap_charger_property_is_writeable, 865 }; 866 867 #ifdef CONFIG_OF 868 static const struct of_device_id cpcap_charger_id_table[] = { 869 { 870 .compatible = "motorola,mapphone-cpcap-charger", 871 }, 872 {}, 873 }; 874 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table); 875 #endif 876 877 static int cpcap_charger_probe(struct platform_device *pdev) 878 { 879 struct cpcap_charger_ddata *ddata; 880 const struct of_device_id *of_id; 881 struct power_supply_config psy_cfg = {}; 882 int error; 883 884 of_id = of_match_device(of_match_ptr(cpcap_charger_id_table), 885 &pdev->dev); 886 if (!of_id) 887 return -EINVAL; 888 889 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 890 if (!ddata) 891 return -ENOMEM; 892 893 ddata->dev = &pdev->dev; 894 ddata->voltage = 4200000; 895 ddata->limit_current = 532000; 896 897 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL); 898 if (!ddata->reg) 899 return -ENODEV; 900 901 INIT_LIST_HEAD(&ddata->irq_list); 902 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); 903 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work); 904 platform_set_drvdata(pdev, ddata); 905 906 error = cpcap_charger_init_iio(ddata); 907 if (error) 908 return error; 909 910 atomic_set(&ddata->active, 1); 911 912 psy_cfg.of_node = pdev->dev.of_node; 913 psy_cfg.drv_data = ddata; 914 psy_cfg.supplied_to = cpcap_charger_supplied_to; 915 psy_cfg.num_supplicants = ARRAY_SIZE(cpcap_charger_supplied_to), 916 917 ddata->usb = devm_power_supply_register(ddata->dev, 918 &cpcap_charger_usb_desc, 919 &psy_cfg); 920 if (IS_ERR(ddata->usb)) { 921 error = PTR_ERR(ddata->usb); 922 dev_err(ddata->dev, "failed to register USB charger: %i\n", 923 error); 924 925 return error; 926 } 927 928 error = cpcap_usb_init_interrupts(pdev, ddata); 929 if (error) 930 return error; 931 932 ddata->comparator.set_vbus = cpcap_charger_set_vbus; 933 error = omap_usb2_set_comparator(&ddata->comparator); 934 if (error == -ENODEV) { 935 dev_info(ddata->dev, "charger needs phy, deferring probe\n"); 936 return -EPROBE_DEFER; 937 } 938 939 cpcap_charger_init_optional_gpios(ddata); 940 941 schedule_delayed_work(&ddata->detect_work, 0); 942 943 return 0; 944 } 945 946 static void cpcap_charger_shutdown(struct platform_device *pdev) 947 { 948 struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev); 949 int error; 950 951 atomic_set(&ddata->active, 0); 952 error = omap_usb2_set_comparator(NULL); 953 if (error) 954 dev_warn(ddata->dev, "could not clear USB comparator: %i\n", 955 error); 956 957 error = cpcap_charger_disable(ddata); 958 if (error) { 959 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); 960 dev_warn(ddata->dev, "could not clear charger: %i\n", 961 error); 962 } 963 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_DISCHARGING); 964 cancel_delayed_work_sync(&ddata->vbus_work); 965 cancel_delayed_work_sync(&ddata->detect_work); 966 } 967 968 static int cpcap_charger_remove(struct platform_device *pdev) 969 { 970 cpcap_charger_shutdown(pdev); 971 972 return 0; 973 } 974 975 static struct platform_driver cpcap_charger_driver = { 976 .probe = cpcap_charger_probe, 977 .driver = { 978 .name = "cpcap-charger", 979 .of_match_table = of_match_ptr(cpcap_charger_id_table), 980 }, 981 .shutdown = cpcap_charger_shutdown, 982 .remove = cpcap_charger_remove, 983 }; 984 module_platform_driver(cpcap_charger_driver); 985 986 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 987 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver"); 988 MODULE_LICENSE("GPL v2"); 989 MODULE_ALIAS("platform:cpcap-charger"); 990