| 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 | * 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/string_choices.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/notifier.h> |
| 21 | #include <linux/mod_devicetable.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 | static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata) |
| 177 | { |
| 178 | struct iio_channel *channel; |
| 179 | int error, value = 0; |
| 180 | |
| 181 | channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE]; |
| 182 | error = iio_read_channel_processed(chan: channel, val: &value); |
| 183 | if (error < 0) { |
| 184 | dev_warn(ddata->dev, "%s failed: %i\n" , __func__, error); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | return value; |
| 190 | } |
| 191 | |
| 192 | static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata) |
| 193 | { |
| 194 | struct iio_channel *channel; |
| 195 | int error, value = 0; |
| 196 | |
| 197 | channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT]; |
| 198 | error = iio_read_channel_processed(chan: channel, val: &value); |
| 199 | if (error < 0) { |
| 200 | dev_warn(ddata->dev, "%s failed: %i\n" , __func__, error); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | return value; |
| 206 | } |
| 207 | |
| 208 | static int cpcap_charger_get_property(struct power_supply *psy, |
| 209 | enum power_supply_property psp, |
| 210 | union power_supply_propval *val) |
| 211 | { |
| 212 | struct cpcap_charger_ddata *ddata = dev_get_drvdata(dev: psy->dev.parent); |
| 213 | |
| 214 | switch (psp) { |
| 215 | case POWER_SUPPLY_PROP_STATUS: |
| 216 | val->intval = ddata->status; |
| 217 | break; |
| 218 | case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: |
| 219 | val->intval = ddata->limit_current; |
| 220 | break; |
| 221 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: |
| 222 | val->intval = ddata->voltage; |
| 223 | break; |
| 224 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 225 | if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) |
| 226 | val->intval = cpcap_charger_get_charge_voltage(ddata) * |
| 227 | 1000; |
| 228 | else |
| 229 | val->intval = 0; |
| 230 | break; |
| 231 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 232 | if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) |
| 233 | val->intval = cpcap_charger_get_charge_current(ddata) * |
| 234 | 1000; |
| 235 | else |
| 236 | val->intval = 0; |
| 237 | break; |
| 238 | case POWER_SUPPLY_PROP_ONLINE: |
| 239 | val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING; |
| 240 | break; |
| 241 | default: |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int cpcap_charger_match_voltage(int voltage) |
| 249 | { |
| 250 | switch (voltage) { |
| 251 | case 0 ... 4100000 - 1: return 3800000; |
| 252 | case 4100000 ... 4120000 - 1: return 4100000; |
| 253 | case 4120000 ... 4150000 - 1: return 4120000; |
| 254 | case 4150000 ... 4170000 - 1: return 4150000; |
| 255 | case 4170000 ... 4200000 - 1: return 4170000; |
| 256 | case 4200000 ... 4230000 - 1: return 4200000; |
| 257 | case 4230000 ... 4250000 - 1: return 4230000; |
| 258 | case 4250000 ... 4270000 - 1: return 4250000; |
| 259 | case 4270000 ... 4300000 - 1: return 4270000; |
| 260 | case 4300000 ... 4330000 - 1: return 4300000; |
| 261 | case 4330000 ... 4350000 - 1: return 4330000; |
| 262 | case 4350000 ... 4380000 - 1: return 4350000; |
| 263 | case 4380000 ... 4400000 - 1: return 4380000; |
| 264 | case 4400000 ... 4420000 - 1: return 4400000; |
| 265 | case 4420000 ... 4440000 - 1: return 4420000; |
| 266 | case 4440000: return 4440000; |
| 267 | default: return 0; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata) |
| 273 | { |
| 274 | union power_supply_propval prop; |
| 275 | struct power_supply *battery; |
| 276 | int voltage = ddata->voltage; |
| 277 | int error; |
| 278 | |
| 279 | battery = power_supply_get_by_name(name: "battery" ); |
| 280 | if (battery) { |
| 281 | error = power_supply_get_property(psy: battery, |
| 282 | psp: POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, |
| 283 | val: &prop); |
| 284 | if (!error) |
| 285 | voltage = prop.intval; |
| 286 | |
| 287 | power_supply_put(psy: battery); |
| 288 | } |
| 289 | |
| 290 | return voltage; |
| 291 | } |
| 292 | |
| 293 | static int cpcap_charger_current_to_regval(int microamp) |
| 294 | { |
| 295 | int miliamp = microamp / 1000; |
| 296 | int res; |
| 297 | |
| 298 | if (miliamp < 0) |
| 299 | return -EINVAL; |
| 300 | if (miliamp < 70) |
| 301 | return CPCAP_REG_CRM_ICHRG(0x0); |
| 302 | if (miliamp < 177) |
| 303 | return CPCAP_REG_CRM_ICHRG(0x1); |
| 304 | if (miliamp >= 1596) |
| 305 | return CPCAP_REG_CRM_ICHRG(0xe); |
| 306 | |
| 307 | res = microamp / 88666; |
| 308 | if (res > 0xd) |
| 309 | res = 0xd; |
| 310 | return CPCAP_REG_CRM_ICHRG(res); |
| 311 | } |
| 312 | |
| 313 | static int cpcap_charger_set_property(struct power_supply *psy, |
| 314 | enum power_supply_property psp, |
| 315 | const union power_supply_propval *val) |
| 316 | { |
| 317 | struct cpcap_charger_ddata *ddata = dev_get_drvdata(dev: psy->dev.parent); |
| 318 | int voltage, batvolt; |
| 319 | |
| 320 | switch (psp) { |
| 321 | case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: |
| 322 | if (cpcap_charger_current_to_regval(microamp: val->intval) < 0) |
| 323 | return -EINVAL; |
| 324 | ddata->limit_current = val->intval; |
| 325 | schedule_delayed_work(dwork: &ddata->detect_work, delay: 0); |
| 326 | break; |
| 327 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: |
| 328 | voltage = cpcap_charger_match_voltage(voltage: val->intval); |
| 329 | batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata); |
| 330 | if (voltage > batvolt) |
| 331 | voltage = batvolt; |
| 332 | ddata->voltage = voltage; |
| 333 | schedule_delayed_work(dwork: &ddata->detect_work, delay: 0); |
| 334 | break; |
| 335 | default: |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int cpcap_charger_property_is_writeable(struct power_supply *psy, |
| 343 | enum power_supply_property psp) |
| 344 | { |
| 345 | switch (psp) { |
| 346 | case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: |
| 347 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: |
| 348 | return 1; |
| 349 | default: |
| 350 | return 0; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata, |
| 355 | bool enabled) |
| 356 | { |
| 357 | if (!ddata->gpio[0]) |
| 358 | return; |
| 359 | |
| 360 | gpiod_set_value(desc: ddata->gpio[0], value: enabled); |
| 361 | } |
| 362 | |
| 363 | static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata, |
| 364 | bool enabled) |
| 365 | { |
| 366 | if (!ddata->gpio[1]) |
| 367 | return; |
| 368 | |
| 369 | gpiod_set_value(desc: ddata->gpio[1], value: enabled); |
| 370 | } |
| 371 | |
| 372 | static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata, |
| 373 | int state) |
| 374 | { |
| 375 | const char *status; |
| 376 | |
| 377 | if (state > POWER_SUPPLY_STATUS_FULL) { |
| 378 | dev_warn(ddata->dev, "unknown state: %i\n" , state); |
| 379 | |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | ddata->status = state; |
| 384 | |
| 385 | switch (state) { |
| 386 | case POWER_SUPPLY_STATUS_DISCHARGING: |
| 387 | status = "DISCONNECTED" ; |
| 388 | break; |
| 389 | case POWER_SUPPLY_STATUS_NOT_CHARGING: |
| 390 | status = "DETECTING" ; |
| 391 | break; |
| 392 | case POWER_SUPPLY_STATUS_CHARGING: |
| 393 | status = "CHARGING" ; |
| 394 | break; |
| 395 | case POWER_SUPPLY_STATUS_FULL: |
| 396 | status = "DONE" ; |
| 397 | break; |
| 398 | default: |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | dev_dbg(ddata->dev, "state: %s\n" , status); |
| 403 | } |
| 404 | |
| 405 | static int cpcap_charger_disable(struct cpcap_charger_ddata *ddata) |
| 406 | { |
| 407 | int error; |
| 408 | |
| 409 | error = regmap_update_bits(map: ddata->reg, CPCAP_REG_CRM, mask: 0x3fff, |
| 410 | CPCAP_REG_CRM_FET_OVRD | |
| 411 | CPCAP_REG_CRM_FET_CTRL); |
| 412 | if (error) |
| 413 | dev_err(ddata->dev, "%s failed with %i\n" , __func__, error); |
| 414 | |
| 415 | return error; |
| 416 | } |
| 417 | |
| 418 | static int cpcap_charger_enable(struct cpcap_charger_ddata *ddata, |
| 419 | int max_voltage, int charge_current, |
| 420 | int trickle_current) |
| 421 | { |
| 422 | int error; |
| 423 | |
| 424 | if (!max_voltage || !charge_current) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | dev_dbg(ddata->dev, "enable: %i %i %i\n" , |
| 428 | max_voltage, charge_current, trickle_current); |
| 429 | |
| 430 | error = regmap_update_bits(map: ddata->reg, CPCAP_REG_CRM, mask: 0x3fff, |
| 431 | CPCAP_REG_CRM_CHRG_LED_EN | |
| 432 | trickle_current | |
| 433 | CPCAP_REG_CRM_FET_OVRD | |
| 434 | CPCAP_REG_CRM_FET_CTRL | |
| 435 | max_voltage | |
| 436 | charge_current); |
| 437 | if (error) |
| 438 | dev_err(ddata->dev, "%s failed with %i\n" , __func__, error); |
| 439 | |
| 440 | return error; |
| 441 | } |
| 442 | |
| 443 | static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata) |
| 444 | { |
| 445 | int error, value = 0; |
| 446 | struct iio_channel *channel = |
| 447 | ddata->channels[CPCAP_CHARGER_IIO_VBUS]; |
| 448 | |
| 449 | error = iio_read_channel_processed(chan: channel, val: &value); |
| 450 | if (error >= 0) |
| 451 | return value > 3900; |
| 452 | |
| 453 | dev_err(ddata->dev, "error reading VBUS: %i\n" , error); |
| 454 | |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | /* VBUS control functions for the USB PHY companion */ |
| 459 | static void cpcap_charger_vbus_work(struct work_struct *work) |
| 460 | { |
| 461 | struct cpcap_charger_ddata *ddata; |
| 462 | bool vbus = false; |
| 463 | int error; |
| 464 | |
| 465 | ddata = container_of(work, struct cpcap_charger_ddata, |
| 466 | vbus_work.work); |
| 467 | |
| 468 | if (ddata->vbus_enabled) { |
| 469 | vbus = cpcap_charger_vbus_valid(ddata); |
| 470 | if (vbus) { |
| 471 | dev_dbg(ddata->dev, "VBUS already provided\n" ); |
| 472 | |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | ddata->feeding_vbus = true; |
| 477 | cpcap_charger_set_cable_path(ddata, enabled: false); |
| 478 | cpcap_charger_set_inductive_path(ddata, enabled: false); |
| 479 | |
| 480 | error = cpcap_charger_disable(ddata); |
| 481 | if (error) |
| 482 | goto out_err; |
| 483 | |
| 484 | cpcap_charger_update_state(ddata, |
| 485 | state: POWER_SUPPLY_STATUS_DISCHARGING); |
| 486 | |
| 487 | error = regmap_update_bits(map: ddata->reg, CPCAP_REG_VUSBC, |
| 488 | CPCAP_BIT_VBUS_SWITCH, |
| 489 | CPCAP_BIT_VBUS_SWITCH); |
| 490 | if (error) |
| 491 | goto out_err; |
| 492 | |
| 493 | error = regmap_update_bits(map: ddata->reg, CPCAP_REG_CRM, |
| 494 | CPCAP_REG_CRM_RVRSMODE, |
| 495 | CPCAP_REG_CRM_RVRSMODE); |
| 496 | if (error) |
| 497 | goto out_err; |
| 498 | } else { |
| 499 | error = regmap_update_bits(map: ddata->reg, CPCAP_REG_VUSBC, |
| 500 | CPCAP_BIT_VBUS_SWITCH, val: 0); |
| 501 | if (error) |
| 502 | goto out_err; |
| 503 | |
| 504 | error = regmap_update_bits(map: ddata->reg, CPCAP_REG_CRM, |
| 505 | CPCAP_REG_CRM_RVRSMODE, val: 0); |
| 506 | if (error) |
| 507 | goto out_err; |
| 508 | |
| 509 | cpcap_charger_set_cable_path(ddata, enabled: true); |
| 510 | cpcap_charger_set_inductive_path(ddata, enabled: true); |
| 511 | ddata->feeding_vbus = false; |
| 512 | } |
| 513 | |
| 514 | return; |
| 515 | |
| 516 | out_err: |
| 517 | cpcap_charger_update_state(ddata, state: POWER_SUPPLY_STATUS_UNKNOWN); |
| 518 | dev_err(ddata->dev, "%s could not %s vbus: %i\n" , __func__, |
| 519 | str_enable_disable(ddata->vbus_enabled), error); |
| 520 | } |
| 521 | |
| 522 | static int cpcap_charger_set_vbus(struct phy_companion *comparator, |
| 523 | bool enabled) |
| 524 | { |
| 525 | struct cpcap_charger_ddata *ddata = |
| 526 | container_of(comparator, struct cpcap_charger_ddata, |
| 527 | comparator); |
| 528 | |
| 529 | ddata->vbus_enabled = enabled; |
| 530 | schedule_delayed_work(dwork: &ddata->vbus_work, delay: 0); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* Charger interrupt handling functions */ |
| 536 | |
| 537 | static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata, |
| 538 | struct cpcap_charger_ints_state *s) |
| 539 | { |
| 540 | int val, error; |
| 541 | |
| 542 | error = regmap_read(map: ddata->reg, CPCAP_REG_INTS1, val: &val); |
| 543 | if (error) |
| 544 | return error; |
| 545 | |
| 546 | s->chrg_det = val & BIT(13); |
| 547 | s->rvrs_chrg = val & BIT(12); |
| 548 | s->vbusov = val & BIT(11); |
| 549 | |
| 550 | error = regmap_read(map: ddata->reg, CPCAP_REG_INTS2, val: &val); |
| 551 | if (error) |
| 552 | return error; |
| 553 | |
| 554 | s->chrg_se1b = val & BIT(13); |
| 555 | s->rvrs_mode = val & BIT(6); |
| 556 | s->chrgcurr2 = val & BIT(5); |
| 557 | s->chrgcurr1 = val & BIT(4); |
| 558 | s->vbusvld = val & BIT(3); |
| 559 | |
| 560 | error = regmap_read(map: ddata->reg, CPCAP_REG_INTS4, val: &val); |
| 561 | if (error) |
| 562 | return error; |
| 563 | |
| 564 | s->battdetb = val & BIT(6); |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int cpcap_charger_voltage_to_regval(int voltage) |
| 570 | { |
| 571 | int offset; |
| 572 | |
| 573 | switch (voltage) { |
| 574 | case 0 ... 4100000 - 1: |
| 575 | return 0; |
| 576 | case 4100000 ... 4200000 - 1: |
| 577 | offset = 1; |
| 578 | break; |
| 579 | case 4200000 ... 4300000 - 1: |
| 580 | offset = 0; |
| 581 | break; |
| 582 | case 4300000 ... 4380000 - 1: |
| 583 | offset = -1; |
| 584 | break; |
| 585 | case 4380000 ... 4440000: |
| 586 | offset = -2; |
| 587 | break; |
| 588 | default: |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | return ((voltage - 4100000) / 20000) + offset; |
| 593 | } |
| 594 | |
| 595 | static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata, |
| 596 | int state, unsigned long delay) |
| 597 | { |
| 598 | int error; |
| 599 | |
| 600 | /* Update battery state before disconnecting the charger */ |
| 601 | switch (state) { |
| 602 | case POWER_SUPPLY_STATUS_DISCHARGING: |
| 603 | case POWER_SUPPLY_STATUS_FULL: |
| 604 | power_supply_changed(psy: ddata->usb); |
| 605 | break; |
| 606 | default: |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | error = cpcap_charger_disable(ddata); |
| 611 | if (error) { |
| 612 | cpcap_charger_update_state(ddata, state: POWER_SUPPLY_STATUS_UNKNOWN); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | cpcap_charger_update_state(ddata, state); |
| 617 | power_supply_changed(psy: ddata->usb); |
| 618 | schedule_delayed_work(dwork: &ddata->detect_work, delay); |
| 619 | } |
| 620 | |
| 621 | static void cpcap_usb_detect(struct work_struct *work) |
| 622 | { |
| 623 | struct cpcap_charger_ddata *ddata; |
| 624 | struct cpcap_charger_ints_state s; |
| 625 | int error, new_state; |
| 626 | |
| 627 | ddata = container_of(work, struct cpcap_charger_ddata, |
| 628 | detect_work.work); |
| 629 | |
| 630 | error = cpcap_charger_get_ints_state(ddata, s: &s); |
| 631 | if (error) |
| 632 | return; |
| 633 | |
| 634 | /* Just init the state if a charger is connected with no chrg_det set */ |
| 635 | if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) { |
| 636 | cpcap_charger_update_state(ddata, |
| 637 | state: POWER_SUPPLY_STATUS_NOT_CHARGING); |
| 638 | |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * If battery voltage is higher than charge voltage, it may have been |
| 644 | * charged to 4.35V by Android. Try again in 10 minutes. |
| 645 | */ |
| 646 | if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) { |
| 647 | cpcap_charger_disconnect(ddata, |
| 648 | state: POWER_SUPPLY_STATUS_NOT_CHARGING, |
| 649 | HZ * 60 * 10); |
| 650 | |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | /* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */ |
| 655 | usleep_range(min: 80000, max: 120000); |
| 656 | |
| 657 | /* Throttle chrgcurr2 interrupt for charger done and retry */ |
| 658 | switch (ddata->status) { |
| 659 | case POWER_SUPPLY_STATUS_CHARGING: |
| 660 | if (s.chrgcurr2) |
| 661 | break; |
| 662 | new_state = POWER_SUPPLY_STATUS_FULL; |
| 663 | |
| 664 | if (s.chrgcurr1 && s.vbusvld) { |
| 665 | cpcap_charger_disconnect(ddata, state: new_state, HZ * 5); |
| 666 | return; |
| 667 | } |
| 668 | break; |
| 669 | case POWER_SUPPLY_STATUS_FULL: |
| 670 | if (!s.chrgcurr2) |
| 671 | break; |
| 672 | if (s.vbusvld) |
| 673 | new_state = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 674 | else |
| 675 | new_state = POWER_SUPPLY_STATUS_DISCHARGING; |
| 676 | |
| 677 | cpcap_charger_disconnect(ddata, state: new_state, HZ * 5); |
| 678 | |
| 679 | return; |
| 680 | default: |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) && |
| 685 | s.chrgcurr1) { |
| 686 | int max_current; |
| 687 | int vchrg, ichrg; |
| 688 | union power_supply_propval val; |
| 689 | struct power_supply *battery; |
| 690 | |
| 691 | battery = power_supply_get_by_name(name: "battery" ); |
| 692 | if (!battery) { |
| 693 | dev_err(ddata->dev, "battery power_supply not available\n" ); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | error = power_supply_get_property(psy: battery, psp: POWER_SUPPLY_PROP_PRESENT, val: &val); |
| 698 | power_supply_put(psy: battery); |
| 699 | if (error) |
| 700 | goto out_err; |
| 701 | |
| 702 | if (val.intval) { |
| 703 | max_current = 1596000; |
| 704 | } else { |
| 705 | dev_info(ddata->dev, "battery not inserted, charging disabled\n" ); |
| 706 | max_current = 0; |
| 707 | } |
| 708 | |
| 709 | if (max_current > ddata->limit_current) |
| 710 | max_current = ddata->limit_current; |
| 711 | |
| 712 | ichrg = cpcap_charger_current_to_regval(microamp: max_current); |
| 713 | vchrg = cpcap_charger_voltage_to_regval(voltage: ddata->voltage); |
| 714 | error = cpcap_charger_enable(ddata, |
| 715 | CPCAP_REG_CRM_VCHRG(vchrg), |
| 716 | charge_current: ichrg, trickle_current: 0); |
| 717 | if (error) |
| 718 | goto out_err; |
| 719 | cpcap_charger_update_state(ddata, |
| 720 | state: 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 | state: POWER_SUPPLY_STATUS_DISCHARGING); |
| 727 | } |
| 728 | |
| 729 | power_supply_changed(psy: ddata->usb); |
| 730 | return; |
| 731 | |
| 732 | out_err: |
| 733 | cpcap_charger_update_state(ddata, state: 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(v: &ddata->active)) |
| 742 | return IRQ_NONE; |
| 743 | |
| 744 | schedule_delayed_work(dwork: &ddata->detect_work, delay: 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(dev: ddata->dev, irq, NULL, |
| 761 | thread_fn: cpcap_charger_irq_thread, |
| 762 | IRQF_SHARED | IRQF_ONESHOT, |
| 763 | devname: name, dev_id: 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(dev: ddata->dev, size: sizeof(*d), GFP_KERNEL); |
| 772 | if (!d) |
| 773 | return -ENOMEM; |
| 774 | |
| 775 | d->name = name; |
| 776 | d->irq = irq; |
| 777 | list_add(new: &d->node, head: &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, name: 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(dev: ddata->dev, con_id: "mode" , |
| 813 | idx: i, flags: GPIOD_OUT_HIGH); |
| 814 | if (IS_ERR(ptr: 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(dev: ddata->dev, |
| 831 | consumer_channel: names[i]); |
| 832 | if (IS_ERR(ptr: ddata->channels[i])) { |
| 833 | error = PTR_ERR(ptr: 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 | static const struct of_device_id cpcap_charger_id_table[] = { |
| 868 | { |
| 869 | .compatible = "motorola,mapphone-cpcap-charger" , |
| 870 | }, |
| 871 | {}, |
| 872 | }; |
| 873 | MODULE_DEVICE_TABLE(of, cpcap_charger_id_table); |
| 874 | |
| 875 | static int cpcap_charger_probe(struct platform_device *pdev) |
| 876 | { |
| 877 | struct cpcap_charger_ddata *ddata; |
| 878 | struct power_supply_config psy_cfg = {}; |
| 879 | int error; |
| 880 | |
| 881 | ddata = devm_kzalloc(dev: &pdev->dev, size: sizeof(*ddata), GFP_KERNEL); |
| 882 | if (!ddata) |
| 883 | return -ENOMEM; |
| 884 | |
| 885 | ddata->dev = &pdev->dev; |
| 886 | ddata->voltage = 4200000; |
| 887 | ddata->limit_current = 532000; |
| 888 | |
| 889 | ddata->reg = dev_get_regmap(dev: ddata->dev->parent, NULL); |
| 890 | if (!ddata->reg) |
| 891 | return -ENODEV; |
| 892 | |
| 893 | INIT_LIST_HEAD(list: &ddata->irq_list); |
| 894 | INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); |
| 895 | INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work); |
| 896 | platform_set_drvdata(pdev, data: ddata); |
| 897 | |
| 898 | error = cpcap_charger_init_iio(ddata); |
| 899 | if (error) |
| 900 | return error; |
| 901 | |
| 902 | atomic_set(v: &ddata->active, i: 1); |
| 903 | |
| 904 | psy_cfg.fwnode = dev_fwnode(&pdev->dev); |
| 905 | psy_cfg.drv_data = ddata; |
| 906 | psy_cfg.supplied_to = cpcap_charger_supplied_to; |
| 907 | psy_cfg.num_supplicants = ARRAY_SIZE(cpcap_charger_supplied_to); |
| 908 | |
| 909 | ddata->usb = devm_power_supply_register(parent: ddata->dev, |
| 910 | desc: &cpcap_charger_usb_desc, |
| 911 | cfg: &psy_cfg); |
| 912 | if (IS_ERR(ptr: ddata->usb)) { |
| 913 | error = PTR_ERR(ptr: ddata->usb); |
| 914 | dev_err(ddata->dev, "failed to register USB charger: %i\n" , |
| 915 | error); |
| 916 | |
| 917 | return error; |
| 918 | } |
| 919 | |
| 920 | error = cpcap_usb_init_interrupts(pdev, ddata); |
| 921 | if (error) |
| 922 | return error; |
| 923 | |
| 924 | ddata->comparator.set_vbus = cpcap_charger_set_vbus; |
| 925 | error = omap_usb2_set_comparator(comparator: &ddata->comparator); |
| 926 | if (error == -ENODEV) { |
| 927 | dev_info(ddata->dev, "charger needs phy, deferring probe\n" ); |
| 928 | return -EPROBE_DEFER; |
| 929 | } |
| 930 | |
| 931 | cpcap_charger_init_optional_gpios(ddata); |
| 932 | |
| 933 | schedule_delayed_work(dwork: &ddata->detect_work, delay: 0); |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | static void cpcap_charger_shutdown(struct platform_device *pdev) |
| 939 | { |
| 940 | struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev); |
| 941 | int error; |
| 942 | |
| 943 | atomic_set(v: &ddata->active, i: 0); |
| 944 | error = omap_usb2_set_comparator(NULL); |
| 945 | if (error) |
| 946 | dev_warn(ddata->dev, "could not clear USB comparator: %i\n" , |
| 947 | error); |
| 948 | |
| 949 | error = cpcap_charger_disable(ddata); |
| 950 | if (error) { |
| 951 | cpcap_charger_update_state(ddata, state: POWER_SUPPLY_STATUS_UNKNOWN); |
| 952 | dev_warn(ddata->dev, "could not clear charger: %i\n" , |
| 953 | error); |
| 954 | } |
| 955 | cpcap_charger_update_state(ddata, state: POWER_SUPPLY_STATUS_DISCHARGING); |
| 956 | cancel_delayed_work_sync(dwork: &ddata->vbus_work); |
| 957 | cancel_delayed_work_sync(dwork: &ddata->detect_work); |
| 958 | } |
| 959 | |
| 960 | static void cpcap_charger_remove(struct platform_device *pdev) |
| 961 | { |
| 962 | cpcap_charger_shutdown(pdev); |
| 963 | } |
| 964 | |
| 965 | static struct platform_driver cpcap_charger_driver = { |
| 966 | .probe = cpcap_charger_probe, |
| 967 | .driver = { |
| 968 | .name = "cpcap-charger" , |
| 969 | .of_match_table = cpcap_charger_id_table, |
| 970 | }, |
| 971 | .shutdown = cpcap_charger_shutdown, |
| 972 | .remove = cpcap_charger_remove, |
| 973 | }; |
| 974 | module_platform_driver(cpcap_charger_driver); |
| 975 | |
| 976 | MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>" ); |
| 977 | MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver" ); |
| 978 | MODULE_LICENSE("GPL v2" ); |
| 979 | MODULE_ALIAS("platform:cpcap-charger" ); |
| 980 | |