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 ? true : false; 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 /* Throttle chrgcurr2 interrupt for charger done and retry */ 672 switch (ddata->status) { 673 case POWER_SUPPLY_STATUS_CHARGING: 674 if (s.chrgcurr2) 675 break; 676 new_state = POWER_SUPPLY_STATUS_FULL; 677 678 if (s.chrgcurr1 && s.vbusvld) { 679 cpcap_charger_disconnect(ddata, new_state, HZ * 5); 680 return; 681 } 682 break; 683 case POWER_SUPPLY_STATUS_FULL: 684 if (!s.chrgcurr2) 685 break; 686 if (s.vbusvld) 687 new_state = POWER_SUPPLY_STATUS_NOT_CHARGING; 688 else 689 new_state = POWER_SUPPLY_STATUS_DISCHARGING; 690 691 cpcap_charger_disconnect(ddata, new_state, HZ * 5); 692 693 return; 694 default: 695 break; 696 } 697 698 if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) && 699 s.chrgcurr1) { 700 int max_current = 532000; 701 int vchrg, ichrg; 702 703 if (cpcap_charger_battery_found(ddata)) 704 max_current = 1596000; 705 706 if (max_current > ddata->limit_current) 707 max_current = ddata->limit_current; 708 709 ichrg = cpcap_charger_current_to_regval(max_current); 710 vchrg = cpcap_charger_voltage_to_regval(ddata->voltage); 711 error = cpcap_charger_enable(ddata, 712 CPCAP_REG_CRM_VCHRG(vchrg), 713 ichrg, 0); 714 if (error) 715 goto out_err; 716 cpcap_charger_update_state(ddata, 717 POWER_SUPPLY_STATUS_CHARGING); 718 } else { 719 error = cpcap_charger_disable(ddata); 720 if (error) 721 goto out_err; 722 cpcap_charger_update_state(ddata, 723 POWER_SUPPLY_STATUS_DISCHARGING); 724 } 725 726 power_supply_changed(ddata->usb); 727 return; 728 729 out_err: 730 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); 731 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 732 } 733 734 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data) 735 { 736 struct cpcap_charger_ddata *ddata = data; 737 738 if (!atomic_read(&ddata->active)) 739 return IRQ_NONE; 740 741 schedule_delayed_work(&ddata->detect_work, 0); 742 743 return IRQ_HANDLED; 744 } 745 746 static int cpcap_usb_init_irq(struct platform_device *pdev, 747 struct cpcap_charger_ddata *ddata, 748 const char *name) 749 { 750 struct cpcap_interrupt_desc *d; 751 int irq, error; 752 753 irq = platform_get_irq_byname(pdev, name); 754 if (irq < 0) 755 return -ENODEV; 756 757 error = devm_request_threaded_irq(ddata->dev, irq, NULL, 758 cpcap_charger_irq_thread, 759 IRQF_SHARED | IRQF_ONESHOT, 760 name, ddata); 761 if (error) { 762 dev_err(ddata->dev, "could not get irq %s: %i\n", 763 name, error); 764 765 return error; 766 } 767 768 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL); 769 if (!d) 770 return -ENOMEM; 771 772 d->name = name; 773 d->irq = irq; 774 list_add(&d->node, &ddata->irq_list); 775 776 return 0; 777 } 778 779 static const char * const cpcap_charger_irqs[] = { 780 /* REG_INT_0 */ 781 "chrg_det", "rvrs_chrg", 782 783 /* REG_INT1 */ 784 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld", 785 786 /* REG_INT_3 */ 787 "battdetb", 788 }; 789 790 static int cpcap_usb_init_interrupts(struct platform_device *pdev, 791 struct cpcap_charger_ddata *ddata) 792 { 793 int i, error; 794 795 for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) { 796 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]); 797 if (error) 798 return error; 799 } 800 801 return 0; 802 } 803 804 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata) 805 { 806 int i; 807 808 for (i = 0; i < 2; i++) { 809 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", 810 i, GPIOD_OUT_HIGH); 811 if (IS_ERR(ddata->gpio[i])) { 812 dev_info(ddata->dev, "no mode change GPIO%i: %li\n", 813 i, PTR_ERR(ddata->gpio[i])); 814 ddata->gpio[i] = NULL; 815 } 816 } 817 } 818 819 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata) 820 { 821 const char * const names[CPCAP_CHARGER_IIO_NR] = { 822 "battdetb", "battp", "vbus", "chg_isense", "batti", 823 }; 824 int error, i; 825 826 for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) { 827 ddata->channels[i] = devm_iio_channel_get(ddata->dev, 828 names[i]); 829 if (IS_ERR(ddata->channels[i])) { 830 error = PTR_ERR(ddata->channels[i]); 831 goto out_err; 832 } 833 834 if (!ddata->channels[i]->indio_dev) { 835 error = -ENXIO; 836 goto out_err; 837 } 838 } 839 840 return 0; 841 842 out_err: 843 if (error != -EPROBE_DEFER) 844 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", 845 error); 846 847 return error; 848 } 849 850 static char *cpcap_charger_supplied_to[] = { 851 "battery", 852 }; 853 854 static const struct power_supply_desc cpcap_charger_usb_desc = { 855 .name = "usb", 856 .type = POWER_SUPPLY_TYPE_USB, 857 .properties = cpcap_charger_props, 858 .num_properties = ARRAY_SIZE(cpcap_charger_props), 859 .get_property = cpcap_charger_get_property, 860 .set_property = cpcap_charger_set_property, 861 .property_is_writeable = cpcap_charger_property_is_writeable, 862 }; 863 864 #ifdef CONFIG_OF 865 static const struct of_device_id cpcap_charger_id_table[] = { 866 { 867 .compatible = "motorola,mapphone-cpcap-charger", 868 }, 869 {}, 870 }; 871 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table); 872 #endif 873 874 static int cpcap_charger_probe(struct platform_device *pdev) 875 { 876 struct cpcap_charger_ddata *ddata; 877 const struct of_device_id *of_id; 878 struct power_supply_config psy_cfg = {}; 879 int error; 880 881 of_id = of_match_device(of_match_ptr(cpcap_charger_id_table), 882 &pdev->dev); 883 if (!of_id) 884 return -EINVAL; 885 886 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 887 if (!ddata) 888 return -ENOMEM; 889 890 ddata->dev = &pdev->dev; 891 ddata->voltage = 4200000; 892 ddata->limit_current = 532000; 893 894 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL); 895 if (!ddata->reg) 896 return -ENODEV; 897 898 INIT_LIST_HEAD(&ddata->irq_list); 899 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); 900 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work); 901 platform_set_drvdata(pdev, ddata); 902 903 error = cpcap_charger_init_iio(ddata); 904 if (error) 905 return error; 906 907 atomic_set(&ddata->active, 1); 908 909 psy_cfg.of_node = pdev->dev.of_node; 910 psy_cfg.drv_data = ddata; 911 psy_cfg.supplied_to = cpcap_charger_supplied_to; 912 psy_cfg.num_supplicants = ARRAY_SIZE(cpcap_charger_supplied_to), 913 914 ddata->usb = devm_power_supply_register(ddata->dev, 915 &cpcap_charger_usb_desc, 916 &psy_cfg); 917 if (IS_ERR(ddata->usb)) { 918 error = PTR_ERR(ddata->usb); 919 dev_err(ddata->dev, "failed to register USB charger: %i\n", 920 error); 921 922 return error; 923 } 924 925 error = cpcap_usb_init_interrupts(pdev, ddata); 926 if (error) 927 return error; 928 929 ddata->comparator.set_vbus = cpcap_charger_set_vbus; 930 error = omap_usb2_set_comparator(&ddata->comparator); 931 if (error == -ENODEV) { 932 dev_info(ddata->dev, "charger needs phy, deferring probe\n"); 933 return -EPROBE_DEFER; 934 } 935 936 cpcap_charger_init_optional_gpios(ddata); 937 938 schedule_delayed_work(&ddata->detect_work, 0); 939 940 return 0; 941 } 942 943 static void cpcap_charger_shutdown(struct platform_device *pdev) 944 { 945 struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev); 946 int error; 947 948 atomic_set(&ddata->active, 0); 949 error = omap_usb2_set_comparator(NULL); 950 if (error) 951 dev_warn(ddata->dev, "could not clear USB comparator: %i\n", 952 error); 953 954 error = cpcap_charger_disable(ddata); 955 if (error) { 956 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); 957 dev_warn(ddata->dev, "could not clear charger: %i\n", 958 error); 959 } 960 cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_DISCHARGING); 961 cancel_delayed_work_sync(&ddata->vbus_work); 962 cancel_delayed_work_sync(&ddata->detect_work); 963 } 964 965 static int cpcap_charger_remove(struct platform_device *pdev) 966 { 967 cpcap_charger_shutdown(pdev); 968 969 return 0; 970 } 971 972 static struct platform_driver cpcap_charger_driver = { 973 .probe = cpcap_charger_probe, 974 .driver = { 975 .name = "cpcap-charger", 976 .of_match_table = of_match_ptr(cpcap_charger_id_table), 977 }, 978 .shutdown = cpcap_charger_shutdown, 979 .remove = cpcap_charger_remove, 980 }; 981 module_platform_driver(cpcap_charger_driver); 982 983 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 984 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver"); 985 MODULE_LICENSE("GPL v2"); 986 MODULE_ALIAS("platform:cpcap-charger"); 987