| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* ROHM BD99954 charger driver |
| 4 |
* |
| 5 |
* Copyright (C) 2020 Rohm Semiconductors |
| 6 |
* Originally written by: |
| 7 |
* Mikko Mutanen <[email protected]> |
| 8 |
* Markus Laine <[email protected]> |
| 9 |
* Bugs added by: |
| 10 |
* Matti Vaittinen <[email protected]> |
| 11 |
*/ |
| 12 |
|
| 13 |
/* |
| 14 |
* The battery charging profile of BD99954. |
| 15 |
* |
| 16 |
* Curve (1) represents charging current. |
| 17 |
* Curve (2) represents battery voltage. |
| 18 |
* |
| 19 |
* The BD99954 data sheet divides charging to three phases. |
| 20 |
* a) Trickle-charge with constant current (8). |
| 21 |
* b) pre-charge with constant current (6) |
| 22 |
* c) fast-charge, first with constant current (5) phase. After |
| 23 |
* the battery voltage has reached target level (4) we have constant |
| 24 |
* voltage phase until charging current has dropped to termination |
| 25 |
* level (7) |
| 26 |
* |
| 27 |
* V ^ ^ I |
| 28 |
* . . |
| 29 |
* . . |
| 30 |
*(4)` `.` ` ` ` ` ` ` ` ` ` ` ` ` ` ----------------------------. |
| 31 |
* . :/ . |
| 32 |
* . o----+/:/ ` ` ` ` ` ` ` ` ` ` ` ` `.` ` (5) |
| 33 |
* . + :: + . |
| 34 |
* . + /- -- . |
| 35 |
* . +`/- + . |
| 36 |
* . o/- -: . |
| 37 |
* . .s. +` . |
| 38 |
* . .--+ `/ . |
| 39 |
* . ..`` + .: . |
| 40 |
* . -` + -- . |
| 41 |
* . (2) ...`` + :- . |
| 42 |
* . ...`` + -: . |
| 43 |
*(3)` `.`."" ` ` ` `+-------- ` ` ` ` ` ` `.:` ` ` ` ` ` ` ` ` .` ` (6) |
| 44 |
* . + `:. . |
| 45 |
* . + -: . |
| 46 |
* . + -:. . |
| 47 |
* . + .--. . |
| 48 |
* . (1) + `.+` ` ` `.` ` (7) |
| 49 |
* -..............` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` + ` ` ` .` ` (8) |
| 50 |
* . + - |
| 51 |
* -------------------------------------------------+++++++++--> |
| 52 |
* | trickle | pre | fast | |
| 53 |
* |
| 54 |
* Details of DT properties for different limits can be found from BD99954 |
| 55 |
* device tree binding documentation. |
| 56 |
*/ |
| 57 |
|
| 58 |
#include <linux/delay.h> |
| 59 |
#include <linux/interrupt.h> |
| 60 |
#include <linux/i2c.h> |
| 61 |
#include <linux/kernel.h> |
| 62 |
#include <linux/linear_range.h> |
| 63 |
#include <linux/module.h> |
| 64 |
#include <linux/mod_devicetable.h> |
| 65 |
#include <linux/power_supply.h> |
| 66 |
#include <linux/property.h> |
| 67 |
#include <linux/regmap.h> |
| 68 |
#include <linux/types.h> |
| 69 |
|
| 70 |
#include "bd99954-charger.h" |
| 71 |
|
| 72 |
/* Initial field values, converted to initial register values */ |
| 73 |
struct bd9995x_init_data { |
| 74 |
u16 vsysreg_set; /* VSYS Regulation Setting */ |
| 75 |
u16 ibus_lim_set; /* VBUS input current limitation */ |
| 76 |
u16 icc_lim_set; /* VCC/VACP Input Current Limit Setting */ |
| 77 |
u16 itrich_set; /* Trickle-charge Current Setting */ |
| 78 |
u16 iprech_set; /* Pre-Charge Current Setting */ |
| 79 |
u16 ichg_set; /* Fast-Charge constant current */ |
| 80 |
u16 vfastchg_reg_set1; /* Fast Charging Regulation Voltage */ |
| 81 |
u16 vprechg_th_set; /* Pre-charge Voltage Threshold Setting */ |
| 82 |
u16 vrechg_set; /* Re-charge Battery Voltage Setting */ |
| 83 |
u16 vbatovp_set; /* Battery Over Voltage Threshold Setting */ |
| 84 |
u16 iterm_set; /* Charging termination current */ |
| 85 |
}; |
| 86 |
|
| 87 |
struct bd9995x_state { |
| 88 |
u8 online; |
| 89 |
u16 chgstm_status; |
| 90 |
u16 vbat_vsys_status; |
| 91 |
u16 vbus_vcc_status; |
| 92 |
}; |
| 93 |
|
| 94 |
struct bd9995x_device { |
| 95 |
struct i2c_client *client; |
| 96 |
struct device *dev; |
| 97 |
struct power_supply *charger; |
| 98 |
|
| 99 |
struct regmap *rmap; |
| 100 |
struct regmap_field *rmap_fields[F_MAX_FIELDS]; |
| 101 |
|
| 102 |
int chip_id; |
| 103 |
int chip_rev; |
| 104 |
struct bd9995x_init_data init_data; |
| 105 |
struct bd9995x_state state; |
| 106 |
|
| 107 |
struct mutex lock; /* Protect state data */ |
| 108 |
}; |
| 109 |
|
| 110 |
static const struct regmap_range bd9995x_readonly_reg_ranges[] = { |
| 111 |
regmap_reg_range(CHGSTM_STATUS, SEL_ILIM_VAL), |
| 112 |
regmap_reg_range(IOUT_DACIN_VAL, IOUT_DACIN_VAL), |
| 113 |
regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS), |
| 114 |
regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS), |
| 115 |
regmap_reg_range(CHIP_ID, CHIP_REV), |
| 116 |
regmap_reg_range(SYSTEM_STATUS, SYSTEM_STATUS), |
| 117 |
regmap_reg_range(IBATP_VAL, VBAT_AVE_VAL), |
| 118 |
regmap_reg_range(VTH_VAL, EXTIADP_AVE_VAL), |
| 119 |
}; |
| 120 |
|
| 121 |
static const struct regmap_access_table bd9995x_writeable_regs = { |
| 122 |
.no_ranges = bd9995x_readonly_reg_ranges, |
| 123 |
.n_no_ranges = ARRAY_SIZE(bd9995x_readonly_reg_ranges), |
| 124 |
}; |
| 125 |
|
| 126 |
static const struct regmap_range bd9995x_volatile_reg_ranges[] = { |
| 127 |
regmap_reg_range(CHGSTM_STATUS, WDT_STATUS), |
| 128 |
regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS), |
| 129 |
regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS), |
| 130 |
regmap_reg_range(INT0_STATUS, INT7_STATUS), |
| 131 |
regmap_reg_range(SYSTEM_STATUS, SYSTEM_CTRL_SET), |
| 132 |
regmap_reg_range(IBATP_VAL, EXTIADP_AVE_VAL), /* Measurement regs */ |
| 133 |
}; |
| 134 |
|
| 135 |
static const struct regmap_access_table bd9995x_volatile_regs = { |
| 136 |
.yes_ranges = bd9995x_volatile_reg_ranges, |
| 137 |
.n_yes_ranges = ARRAY_SIZE(bd9995x_volatile_reg_ranges), |
| 138 |
}; |
| 139 |
|
| 140 |
static const struct regmap_range_cfg regmap_range_cfg[] = { |
| 141 |
{ |
| 142 |
.selector_reg = MAP_SET, |
| 143 |
.selector_mask = 0xFFFF, |
| 144 |
.selector_shift = 0, |
| 145 |
.window_start = 0, |
| 146 |
.window_len = 0x100, |
| 147 |
.range_min = 0 * 0x100, |
| 148 |
.range_max = 3 * 0x100, |
| 149 |
}, |
| 150 |
}; |
| 151 |
|
| 152 |
static const struct regmap_config bd9995x_regmap_config = { |
| 153 |
.reg_bits = 8, |
| 154 |
.val_bits = 16, |
| 155 |
.reg_stride = 1, |
| 156 |
|
| 157 |
.max_register = 3 * 0x100, |
| 158 |
.cache_type = REGCACHE_MAPLE, |
| 159 |
|
| 160 |
.ranges = regmap_range_cfg, |
| 161 |
.num_ranges = ARRAY_SIZE(regmap_range_cfg), |
| 162 |
.val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 163 |
.wr_table = &bd9995x_writeable_regs, |
| 164 |
.volatile_table = &bd9995x_volatile_regs, |
| 165 |
}; |
| 166 |
|
| 167 |
enum bd9995x_chrg_fault { |
| 168 |
CHRG_FAULT_NORMAL, |
| 169 |
CHRG_FAULT_INPUT, |
| 170 |
CHRG_FAULT_THERMAL_SHUTDOWN, |
| 171 |
CHRG_FAULT_TIMER_EXPIRED, |
| 172 |
}; |
| 173 |
|
| 174 |
static int bd9995x_get_prop_batt_health(struct bd9995x_device *bd) |
| 175 |
{ |
| 176 |
int ret, tmp; |
| 177 |
|
| 178 |
ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp); |
| 179 |
if (ret) |
| 180 |
return POWER_SUPPLY_HEALTH_UNKNOWN; |
| 181 |
|
| 182 |
/* TODO: Check these against datasheet page 34 */ |
| 183 |
|
| 184 |
switch (tmp) { |
| 185 |
case ROOM: |
| 186 |
return POWER_SUPPLY_HEALTH_GOOD; |
| 187 |
case HOT1: |
| 188 |
case HOT2: |
| 189 |
case HOT3: |
| 190 |
return POWER_SUPPLY_HEALTH_OVERHEAT; |
| 191 |
case COLD1: |
| 192 |
case COLD2: |
| 193 |
return POWER_SUPPLY_HEALTH_COLD; |
| 194 |
case TEMP_DIS: |
| 195 |
case BATT_OPEN: |
| 196 |
default: |
| 197 |
return POWER_SUPPLY_HEALTH_UNKNOWN; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
static int bd9995x_get_prop_charge_type(struct bd9995x_device *bd) |
| 202 |
{ |
| 203 |
int ret, tmp; |
| 204 |
|
| 205 |
ret = regmap_field_read(bd->rmap_fields[F_CHGSTM_STATE], &tmp); |
| 206 |
if (ret) |
| 207 |
return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; |
| 208 |
|
| 209 |
switch (tmp) { |
| 210 |
case CHGSTM_TRICKLE_CHARGE: |
| 211 |
case CHGSTM_PRE_CHARGE: |
| 212 |
return POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 213 |
case CHGSTM_FAST_CHARGE: |
| 214 |
return POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 215 |
case CHGSTM_TOP_OFF: |
| 216 |
case CHGSTM_DONE: |
| 217 |
case CHGSTM_SUSPEND: |
| 218 |
return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 219 |
default: /* Rest of the states are error related, no charging */ |
| 220 |
return POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
static bool bd9995x_get_prop_batt_present(struct bd9995x_device *bd) |
| 225 |
{ |
| 226 |
int ret, tmp; |
| 227 |
|
| 228 |
ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp); |
| 229 |
if (ret) |
| 230 |
return false; |
| 231 |
|
| 232 |
return tmp != BATT_OPEN; |
| 233 |
} |
| 234 |
|
| 235 |
static int bd9995x_get_prop_batt_voltage(struct bd9995x_device *bd) |
| 236 |
{ |
| 237 |
int ret, tmp; |
| 238 |
|
| 239 |
ret = regmap_field_read(bd->rmap_fields[F_VBAT_VAL], &tmp); |
| 240 |
if (ret) |
| 241 |
return 0; |
| 242 |
|
| 243 |
tmp = min(tmp, 19200); |
| 244 |
|
| 245 |
return tmp * 1000; |
| 246 |
} |
| 247 |
|
| 248 |
static int bd9995x_get_prop_batt_current(struct bd9995x_device *bd) |
| 249 |
{ |
| 250 |
int ret, tmp; |
| 251 |
|
| 252 |
ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp); |
| 253 |
if (ret) |
| 254 |
return 0; |
| 255 |
|
| 256 |
return tmp * 1000; |
| 257 |
} |
| 258 |
|
| 259 |
#define DEFAULT_BATTERY_TEMPERATURE 250 |
| 260 |
|
| 261 |
static int bd9995x_get_prop_batt_temp(struct bd9995x_device *bd) |
| 262 |
{ |
| 263 |
int ret, tmp; |
| 264 |
|
| 265 |
ret = regmap_field_read(bd->rmap_fields[F_THERM_VAL], &tmp); |
| 266 |
if (ret) |
| 267 |
return DEFAULT_BATTERY_TEMPERATURE; |
| 268 |
|
| 269 |
return (200 - tmp) * 10; |
| 270 |
} |
| 271 |
|
| 272 |
static int bd9995x_power_supply_get_property(struct power_supply *psy, |
| 273 |
enum power_supply_property psp, |
| 274 |
union power_supply_propval *val) |
| 275 |
{ |
| 276 |
int ret, tmp; |
| 277 |
struct bd9995x_device *bd = power_supply_get_drvdata(psy); |
| 278 |
struct bd9995x_state state; |
| 279 |
|
| 280 |
mutex_lock(&bd->lock); |
| 281 |
state = bd->state; |
| 282 |
mutex_unlock(&bd->lock); |
| 283 |
|
| 284 |
switch (psp) { |
| 285 |
case POWER_SUPPLY_PROP_STATUS: |
| 286 |
switch (state.chgstm_status) { |
| 287 |
case CHGSTM_TRICKLE_CHARGE: |
| 288 |
case CHGSTM_PRE_CHARGE: |
| 289 |
case CHGSTM_FAST_CHARGE: |
| 290 |
case CHGSTM_TOP_OFF: |
| 291 |
val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 292 |
break; |
| 293 |
|
| 294 |
case CHGSTM_DONE: |
| 295 |
val->intval = POWER_SUPPLY_STATUS_FULL; |
| 296 |
break; |
| 297 |
|
| 298 |
case CHGSTM_SUSPEND: |
| 299 |
case CHGSTM_TEMPERATURE_ERROR_1: |
| 300 |
case CHGSTM_TEMPERATURE_ERROR_2: |
| 301 |
case CHGSTM_TEMPERATURE_ERROR_3: |
| 302 |
case CHGSTM_TEMPERATURE_ERROR_4: |
| 303 |
case CHGSTM_TEMPERATURE_ERROR_5: |
| 304 |
case CHGSTM_TEMPERATURE_ERROR_6: |
| 305 |
case CHGSTM_TEMPERATURE_ERROR_7: |
| 306 |
case CHGSTM_THERMAL_SHUT_DOWN_1: |
| 307 |
case CHGSTM_THERMAL_SHUT_DOWN_2: |
| 308 |
case CHGSTM_THERMAL_SHUT_DOWN_3: |
| 309 |
case CHGSTM_THERMAL_SHUT_DOWN_4: |
| 310 |
case CHGSTM_THERMAL_SHUT_DOWN_5: |
| 311 |
case CHGSTM_THERMAL_SHUT_DOWN_6: |
| 312 |
case CHGSTM_THERMAL_SHUT_DOWN_7: |
| 313 |
case CHGSTM_BATTERY_ERROR: |
| 314 |
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 315 |
break; |
| 316 |
|
| 317 |
default: |
| 318 |
val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 319 |
break; |
| 320 |
} |
| 321 |
break; |
| 322 |
|
| 323 |
case POWER_SUPPLY_PROP_MANUFACTURER: |
| 324 |
val->strval = BD9995X_MANUFACTURER; |
| 325 |
break; |
| 326 |
|
| 327 |
case POWER_SUPPLY_PROP_ONLINE: |
| 328 |
val->intval = state.online; |
| 329 |
break; |
| 330 |
|
| 331 |
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: |
| 332 |
ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp); |
| 333 |
if (ret) |
| 334 |
return ret; |
| 335 |
val->intval = tmp * 1000; |
| 336 |
break; |
| 337 |
|
| 338 |
case POWER_SUPPLY_PROP_CHARGE_AVG: |
| 339 |
ret = regmap_field_read(bd->rmap_fields[F_IBATP_AVE_VAL], &tmp); |
| 340 |
if (ret) |
| 341 |
return ret; |
| 342 |
val->intval = tmp * 1000; |
| 343 |
break; |
| 344 |
|
| 345 |
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| 346 |
/* |
| 347 |
* Currently the DT uses this property to give the |
| 348 |
* target current for fast-charging constant current phase. |
| 349 |
* I think it is correct in a sense. |
| 350 |
* |
| 351 |
* Yet, this prop we read and return here is the programmed |
| 352 |
* safety limit for combined input currents. This feels |
| 353 |
* also correct in a sense. |
| 354 |
* |
| 355 |
* However, this results a mismatch to DT value and value |
| 356 |
* read from sysfs. |
| 357 |
*/ |
| 358 |
ret = regmap_field_read(bd->rmap_fields[F_SEL_ILIM_VAL], &tmp); |
| 359 |
if (ret) |
| 360 |
return ret; |
| 361 |
val->intval = tmp * 1000; |
| 362 |
break; |
| 363 |
|
| 364 |
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: |
| 365 |
if (!state.online) { |
| 366 |
val->intval = 0; |
| 367 |
break; |
| 368 |
} |
| 369 |
|
| 370 |
ret = regmap_field_read(bd->rmap_fields[F_VFASTCHG_REG_SET1], |
| 371 |
&tmp); |
| 372 |
if (ret) |
| 373 |
return ret; |
| 374 |
|
| 375 |
/* |
| 376 |
* The actual range : 2560 to 19200 mV. No matter what the |
| 377 |
* register says |
| 378 |
*/ |
| 379 |
val->intval = clamp_val(tmp << 4, 2560, 19200); |
| 380 |
val->intval *= 1000; |
| 381 |
break; |
| 382 |
|
| 383 |
case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: |
| 384 |
ret = regmap_field_read(bd->rmap_fields[F_ITERM_SET], &tmp); |
| 385 |
if (ret) |
| 386 |
return ret; |
| 387 |
/* Start step is 64 mA */ |
| 388 |
val->intval = tmp << 6; |
| 389 |
/* Maximum is 1024 mA - no matter what register says */ |
| 390 |
val->intval = min(val->intval, 1024); |
| 391 |
val->intval *= 1000; |
| 392 |
break; |
| 393 |
|
| 394 |
/* Battery properties which we access through charger */ |
| 395 |
case POWER_SUPPLY_PROP_PRESENT: |
| 396 |
val->intval = bd9995x_get_prop_batt_present(bd); |
| 397 |
break; |
| 398 |
|
| 399 |
case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 400 |
val->intval = bd9995x_get_prop_batt_voltage(bd); |
| 401 |
break; |
| 402 |
|
| 403 |
case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 404 |
val->intval = bd9995x_get_prop_batt_current(bd); |
| 405 |
break; |
| 406 |
|
| 407 |
case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 408 |
val->intval = bd9995x_get_prop_charge_type(bd); |
| 409 |
break; |
| 410 |
|
| 411 |
case POWER_SUPPLY_PROP_HEALTH: |
| 412 |
val->intval = bd9995x_get_prop_batt_health(bd); |
| 413 |
break; |
| 414 |
|
| 415 |
case POWER_SUPPLY_PROP_TEMP: |
| 416 |
val->intval = bd9995x_get_prop_batt_temp(bd); |
| 417 |
break; |
| 418 |
|
| 419 |
case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 420 |
val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 421 |
break; |
| 422 |
|
| 423 |
case POWER_SUPPLY_PROP_MODEL_NAME: |
| 424 |
val->strval = "bd99954"; |
| 425 |
break; |
| 426 |
|
| 427 |
default: |
| 428 |
return -EINVAL; |
| 429 |
|
| 430 |
} |
| 431 |
|
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
static int bd9995x_get_chip_state(struct bd9995x_device *bd, |
| 436 |
struct bd9995x_state *state) |
| 437 |
{ |
| 438 |
int i, ret, tmp; |
| 439 |
struct { |
| 440 |
struct regmap_field *id; |
| 441 |
u16 *data; |
| 442 |
} state_fields[] = { |
| 443 |
{ |
| 444 |
bd->rmap_fields[F_CHGSTM_STATE], &state->chgstm_status, |
| 445 |
}, { |
| 446 |
bd->rmap_fields[F_VBAT_VSYS_STATUS], |
| 447 |
&state->vbat_vsys_status, |
| 448 |
}, { |
| 449 |
bd->rmap_fields[F_VBUS_VCC_STATUS], |
| 450 |
&state->vbus_vcc_status, |
| 451 |
}, |
| 452 |
}; |
| 453 |
|
| 454 |
|
| 455 |
for (i = 0; i < ARRAY_SIZE(state_fields); i++) { |
| 456 |
ret = regmap_field_read(state_fields[i].id, &tmp); |
| 457 |
if (ret) |
| 458 |
return ret; |
| 459 |
|
| 460 |
*state_fields[i].data = tmp; |
| 461 |
} |
| 462 |
|
| 463 |
if (state->vbus_vcc_status & STATUS_VCC_DET || |
| 464 |
state->vbus_vcc_status & STATUS_VBUS_DET) |
| 465 |
state->online = 1; |
| 466 |
else |
| 467 |
state->online = 0; |
| 468 |
|
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
static irqreturn_t bd9995x_irq_handler_thread(int irq, void *private) |
| 473 |
{ |
| 474 |
struct bd9995x_device *bd = private; |
| 475 |
int ret, status, mask, i; |
| 476 |
unsigned long tmp; |
| 477 |
struct bd9995x_state state; |
| 478 |
|
| 479 |
/* |
| 480 |
* The bd9995x does not seem to generate big amount of interrupts. |
| 481 |
* The logic regarding which interrupts can cause relevant |
| 482 |
* status changes seem to be pretty complex. |
| 483 |
* |
| 484 |
* So lets implement really simple and hopefully bullet-proof handler: |
| 485 |
* It does not really matter which IRQ we handle, we just go and |
| 486 |
* re-read all interesting statuses + give the framework a nudge. |
| 487 |
* |
| 488 |
* Other option would be building a _complex_ and error prone logic |
| 489 |
* trying to decide what could have been changed (resulting this IRQ |
| 490 |
* we are now handling). During the normal operation the BD99954 does |
| 491 |
* not seem to be generating much of interrupts so benefit from such |
| 492 |
* logic would probably be minimal. |
| 493 |
*/ |
| 494 |
|
| 495 |
ret = regmap_read(bd->rmap, INT0_STATUS, &status); |
| 496 |
if (ret) { |
| 497 |
dev_err(bd->dev, "Failed to read IRQ status\n"); |
| 498 |
return IRQ_NONE; |
| 499 |
} |
| 500 |
|
| 501 |
ret = regmap_field_read(bd->rmap_fields[F_INT0_SET], &mask); |
| 502 |
if (ret) { |
| 503 |
dev_err(bd->dev, "Failed to read IRQ mask\n"); |
| 504 |
return IRQ_NONE; |
| 505 |
} |
| 506 |
|
| 507 |
/* Handle only IRQs that are not masked */ |
| 508 |
status &= mask; |
| 509 |
tmp = status; |
| 510 |
|
| 511 |
/* Lowest bit does not represent any sub-registers */ |
| 512 |
tmp >>= 1; |
| 513 |
|
| 514 |
/* |
| 515 |
* Mask and ack IRQs we will handle (+ the idiot bit) |
| 516 |
*/ |
| 517 |
ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], 0); |
| 518 |
if (ret) { |
| 519 |
dev_err(bd->dev, "Failed to mask F_INT0\n"); |
| 520 |
return IRQ_NONE; |
| 521 |
} |
| 522 |
|
| 523 |
ret = regmap_write(bd->rmap, INT0_STATUS, status); |
| 524 |
if (ret) { |
| 525 |
dev_err(bd->dev, "Failed to ack F_INT0\n"); |
| 526 |
goto err_umask; |
| 527 |
} |
| 528 |
|
| 529 |
for_each_set_bit(i, &tmp, 7) { |
| 530 |
int sub_status, sub_mask; |
| 531 |
static const int sub_status_reg[] = { |
| 532 |
INT1_STATUS, INT2_STATUS, INT3_STATUS, INT4_STATUS, |
| 533 |
INT5_STATUS, INT6_STATUS, INT7_STATUS, |
| 534 |
}; |
| 535 |
struct regmap_field *sub_mask_f[] = { |
| 536 |
bd->rmap_fields[F_INT1_SET], |
| 537 |
bd->rmap_fields[F_INT2_SET], |
| 538 |
bd->rmap_fields[F_INT3_SET], |
| 539 |
bd->rmap_fields[F_INT4_SET], |
| 540 |
bd->rmap_fields[F_INT5_SET], |
| 541 |
bd->rmap_fields[F_INT6_SET], |
| 542 |
bd->rmap_fields[F_INT7_SET], |
| 543 |
}; |
| 544 |
|
| 545 |
/* Clear sub IRQs */ |
| 546 |
ret = regmap_read(bd->rmap, sub_status_reg[i], &sub_status); |
| 547 |
if (ret) { |
| 548 |
dev_err(bd->dev, "Failed to read IRQ sub-status\n"); |
| 549 |
goto err_umask; |
| 550 |
} |
| 551 |
|
| 552 |
ret = regmap_field_read(sub_mask_f[i], &sub_mask); |
| 553 |
if (ret) { |
| 554 |
dev_err(bd->dev, "Failed to read IRQ sub-mask\n"); |
| 555 |
goto err_umask; |
| 556 |
} |
| 557 |
|
| 558 |
/* Ack active sub-statuses */ |
| 559 |
sub_status &= sub_mask; |
| 560 |
|
| 561 |
ret = regmap_write(bd->rmap, sub_status_reg[i], sub_status); |
| 562 |
if (ret) { |
| 563 |
dev_err(bd->dev, "Failed to ack sub-IRQ\n"); |
| 564 |
goto err_umask; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask); |
| 569 |
if (ret) |
| 570 |
/* May as well retry once */ |
| 571 |
goto err_umask; |
| 572 |
|
| 573 |
/* Read whole chip state */ |
| 574 |
ret = bd9995x_get_chip_state(bd, &state); |
| 575 |
if (ret < 0) { |
| 576 |
dev_err(bd->dev, "Failed to read chip state\n"); |
| 577 |
} else { |
| 578 |
mutex_lock(&bd->lock); |
| 579 |
bd->state = state; |
| 580 |
mutex_unlock(&bd->lock); |
| 581 |
|
| 582 |
power_supply_changed(bd->charger); |
| 583 |
} |
| 584 |
|
| 585 |
return IRQ_HANDLED; |
| 586 |
|
| 587 |
err_umask: |
| 588 |
ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask); |
| 589 |
if (ret) |
| 590 |
dev_err(bd->dev, |
| 591 |
"Failed to un-mask F_INT0 - IRQ permanently disabled\n"); |
| 592 |
|
| 593 |
return IRQ_NONE; |
| 594 |
} |
| 595 |
|
| 596 |
static int __bd9995x_chip_reset(struct bd9995x_device *bd) |
| 597 |
{ |
| 598 |
int ret, state; |
| 599 |
int rst_check_counter = 10; |
| 600 |
u16 tmp = ALLRST | OTPLD; |
| 601 |
|
| 602 |
ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2); |
| 603 |
if (ret < 0) |
| 604 |
return ret; |
| 605 |
|
| 606 |
do { |
| 607 |
ret = regmap_field_read(bd->rmap_fields[F_OTPLD_STATE], &state); |
| 608 |
if (ret) |
| 609 |
return ret; |
| 610 |
|
| 611 |
msleep(10); |
| 612 |
} while (state == 0 && --rst_check_counter); |
| 613 |
|
| 614 |
if (!rst_check_counter) { |
| 615 |
dev_err(bd->dev, "chip reset not completed\n"); |
| 616 |
return -ETIMEDOUT; |
| 617 |
} |
| 618 |
|
| 619 |
tmp = 0; |
| 620 |
ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2); |
| 621 |
|
| 622 |
return ret; |
| 623 |
} |
| 624 |
|
| 625 |
static int bd9995x_hw_init(struct bd9995x_device *bd) |
| 626 |
{ |
| 627 |
int ret; |
| 628 |
int i; |
| 629 |
struct bd9995x_state state; |
| 630 |
struct bd9995x_init_data *id = &bd->init_data; |
| 631 |
|
| 632 |
const struct { |
| 633 |
enum bd9995x_fields id; |
| 634 |
u16 value; |
| 635 |
} init_data[] = { |
| 636 |
/* Enable the charging trigger after SDP charger attached */ |
| 637 |
{F_SDP_CHG_TRIG_EN, 1}, |
| 638 |
/* Enable charging trigger after SDP charger attached */ |
| 639 |
{F_SDP_CHG_TRIG, 1}, |
| 640 |
/* Disable charging trigger by BC1.2 detection */ |
| 641 |
{F_VBUS_BC_DISEN, 1}, |
| 642 |
/* Disable charging trigger by BC1.2 detection */ |
| 643 |
{F_VCC_BC_DISEN, 1}, |
| 644 |
/* Disable automatic limitation of the input current */ |
| 645 |
{F_ILIM_AUTO_DISEN, 1}, |
| 646 |
/* Select current limitation when SDP charger attached*/ |
| 647 |
{F_SDP_500_SEL, 1}, |
| 648 |
/* Select current limitation when DCP charger attached */ |
| 649 |
{F_DCP_2500_SEL, 1}, |
| 650 |
{F_VSYSREG_SET, id->vsysreg_set}, |
| 651 |
/* Activate USB charging and DC/DC converter */ |
| 652 |
{F_USB_SUS, 0}, |
| 653 |
/* DCDC clock: 1200 kHz*/ |
| 654 |
{F_DCDC_CLK_SEL, 3}, |
| 655 |
/* Enable charging */ |
| 656 |
{F_CHG_EN, 1}, |
| 657 |
/* Disable Input current Limit setting voltage measurement */ |
| 658 |
{F_EXTIADPEN, 0}, |
| 659 |
/* Disable input current limiting */ |
| 660 |
{F_VSYS_PRIORITY, 1}, |
| 661 |
{F_IBUS_LIM_SET, id->ibus_lim_set}, |
| 662 |
{F_ICC_LIM_SET, id->icc_lim_set}, |
| 663 |
/* Charge Termination Current Setting to 0*/ |
| 664 |
{F_ITERM_SET, id->iterm_set}, |
| 665 |
/* Trickle-charge Current Setting */ |
| 666 |
{F_ITRICH_SET, id->itrich_set}, |
| 667 |
/* Pre-charge Current setting */ |
| 668 |
{F_IPRECH_SET, id->iprech_set}, |
| 669 |
/* Fast Charge Current for constant current phase */ |
| 670 |
{F_ICHG_SET, id->ichg_set}, |
| 671 |
/* Fast Charge Voltage Regulation Setting */ |
| 672 |
{F_VFASTCHG_REG_SET1, id->vfastchg_reg_set1}, |
| 673 |
/* Set Pre-charge Voltage Threshold for trickle charging. */ |
| 674 |
{F_VPRECHG_TH_SET, id->vprechg_th_set}, |
| 675 |
{F_VRECHG_SET, id->vrechg_set}, |
| 676 |
{F_VBATOVP_SET, id->vbatovp_set}, |
| 677 |
/* Reverse buck boost voltage Setting */ |
| 678 |
{F_VRBOOST_SET, 0}, |
| 679 |
/* Disable fast-charging watchdog */ |
| 680 |
{F_WDT_FST, 0}, |
| 681 |
/* Disable pre-charging watchdog */ |
| 682 |
{F_WDT_PRE, 0}, |
| 683 |
/* Power save off */ |
| 684 |
{F_POWER_SAVE_MODE, 0}, |
| 685 |
{F_INT1_SET, INT1_ALL}, |
| 686 |
{F_INT2_SET, INT2_ALL}, |
| 687 |
{F_INT3_SET, INT3_ALL}, |
| 688 |
{F_INT4_SET, INT4_ALL}, |
| 689 |
{F_INT5_SET, INT5_ALL}, |
| 690 |
{F_INT6_SET, INT6_ALL}, |
| 691 |
{F_INT7_SET, INT7_ALL}, |
| 692 |
}; |
| 693 |
|
| 694 |
/* |
| 695 |
* Currently we initialize charger to a known state at startup. |
| 696 |
* If we want to allow for example the boot code to initialize |
| 697 |
* charger we should get rid of this. |
| 698 |
*/ |
| 699 |
ret = __bd9995x_chip_reset(bd); |
| 700 |
if (ret < 0) |
| 701 |
return ret; |
| 702 |
|
| 703 |
/* Initialize currents/voltages and other parameters */ |
| 704 |
for (i = 0; i < ARRAY_SIZE(init_data); i++) { |
| 705 |
ret = regmap_field_write(bd->rmap_fields[init_data[i].id], |
| 706 |
init_data[i].value); |
| 707 |
if (ret) { |
| 708 |
dev_err(bd->dev, "failed to initialize charger (%d)\n", |
| 709 |
ret); |
| 710 |
return ret; |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
ret = bd9995x_get_chip_state(bd, &state); |
| 715 |
if (ret < 0) |
| 716 |
return ret; |
| 717 |
|
| 718 |
mutex_lock(&bd->lock); |
| 719 |
bd->state = state; |
| 720 |
mutex_unlock(&bd->lock); |
| 721 |
|
| 722 |
return 0; |
| 723 |
} |
| 724 |
|
| 725 |
static enum power_supply_property bd9995x_power_supply_props[] = { |
| 726 |
POWER_SUPPLY_PROP_MANUFACTURER, |
| 727 |
POWER_SUPPLY_PROP_STATUS, |
| 728 |
POWER_SUPPLY_PROP_ONLINE, |
| 729 |
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, |
| 730 |
POWER_SUPPLY_PROP_CHARGE_AVG, |
| 731 |
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
| 732 |
POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, |
| 733 |
POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, |
| 734 |
/* Battery props we access through charger */ |
| 735 |
POWER_SUPPLY_PROP_PRESENT, |
| 736 |
POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 737 |
POWER_SUPPLY_PROP_CURRENT_NOW, |
| 738 |
POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 739 |
POWER_SUPPLY_PROP_HEALTH, |
| 740 |
POWER_SUPPLY_PROP_TEMP, |
| 741 |
POWER_SUPPLY_PROP_TECHNOLOGY, |
| 742 |
POWER_SUPPLY_PROP_MODEL_NAME, |
| 743 |
}; |
| 744 |
|
| 745 |
static const struct power_supply_desc bd9995x_power_supply_desc = { |
| 746 |
.name = "bd9995x-charger", |
| 747 |
.type = POWER_SUPPLY_TYPE_USB, |
| 748 |
.properties = bd9995x_power_supply_props, |
| 749 |
.num_properties = ARRAY_SIZE(bd9995x_power_supply_props), |
| 750 |
.get_property = bd9995x_power_supply_get_property, |
| 751 |
}; |
| 752 |
|
| 753 |
/* |
| 754 |
* Limit configurations for vbus-input-current and vcc-vacp-input-current |
| 755 |
* Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is |
| 756 |
* configured by writing a register so that each increment in register |
| 757 |
* value equals to 32000 uA limit increment. |
| 758 |
* |
| 759 |
* Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ... |
| 760 |
* Describe the setting in linear_range table. |
| 761 |
*/ |
| 762 |
static const struct linear_range input_current_limit_ranges[] = { |
| 763 |
LINEAR_RANGE(0, 0x0, 0x1ff, 32000), |
| 764 |
}; |
| 765 |
|
| 766 |
/* Possible trickle, pre-charging and termination current values */ |
| 767 |
static const struct linear_range charging_current_ranges[] = { |
| 768 |
LINEAR_RANGE(0, 0x0, 0x10, 64000), |
| 769 |
LINEAR_RANGE(1024000, 0x11, 0x1f, 0), |
| 770 |
}; |
| 771 |
|
| 772 |
/* |
| 773 |
* Fast charging voltage regulation, starting re-charging limit |
| 774 |
* and battery over voltage protection have same possible values |
| 775 |
*/ |
| 776 |
static const struct linear_range charge_voltage_regulation_ranges[] = { |
| 777 |
LINEAR_RANGE(2560000, 0, 0xA0, 0), |
| 778 |
LINEAR_RANGE(2560000, 0xA0, 0x4B0, 16000), |
| 779 |
LINEAR_RANGE(19200000, 0x4B0, 0x7FF, 0), |
| 780 |
}; |
| 781 |
|
| 782 |
/* Possible VSYS voltage regulation values */ |
| 783 |
static const struct linear_range vsys_voltage_regulation_ranges[] = { |
| 784 |
LINEAR_RANGE(2560000, 0, 0x28, 0), |
| 785 |
LINEAR_RANGE(2560000, 0x28, 0x12C, 64000), |
| 786 |
LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0), |
| 787 |
}; |
| 788 |
|
| 789 |
/* Possible settings for switching from trickle to pre-charging limits */ |
| 790 |
static const struct linear_range trickle_to_pre_threshold_ranges[] = { |
| 791 |
LINEAR_RANGE(2048000, 0, 0x20, 0), |
| 792 |
LINEAR_RANGE(2048000, 0x20, 0x12C, 64000), |
| 793 |
LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0), |
| 794 |
}; |
| 795 |
|
| 796 |
/* Possible current values for fast-charging constant current phase */ |
| 797 |
static const struct linear_range fast_charge_current_ranges[] = { |
| 798 |
LINEAR_RANGE(0, 0, 0xFF, 64000), |
| 799 |
}; |
| 800 |
|
| 801 |
struct battery_init { |
| 802 |
const char *name; |
| 803 |
int *info_data; |
| 804 |
const struct linear_range *range; |
| 805 |
int ranges; |
| 806 |
u16 *data; |
| 807 |
}; |
| 808 |
|
| 809 |
struct dt_init { |
| 810 |
char *prop; |
| 811 |
const struct linear_range *range; |
| 812 |
int ranges; |
| 813 |
u16 *data; |
| 814 |
}; |
| 815 |
|
| 816 |
static int bd9995x_fw_probe(struct bd9995x_device *bd) |
| 817 |
{ |
| 818 |
int ret; |
| 819 |
struct power_supply_battery_info *info; |
| 820 |
u32 property; |
| 821 |
int i; |
| 822 |
int regval; |
| 823 |
bool found; |
| 824 |
struct bd9995x_init_data *init = &bd->init_data; |
| 825 |
struct battery_init battery_inits[] = { |
| 826 |
{ |
| 827 |
.name = "trickle-charging current", |
| 828 |
.range = &charging_current_ranges[0], |
| 829 |
.ranges = 2, |
| 830 |
.data = &init->itrich_set, |
| 831 |
}, { |
| 832 |
.name = "pre-charging current", |
| 833 |
.range = &charging_current_ranges[0], |
| 834 |
.ranges = 2, |
| 835 |
.data = &init->iprech_set, |
| 836 |
}, { |
| 837 |
.name = "pre-to-trickle charge voltage threshold", |
| 838 |
.range = &trickle_to_pre_threshold_ranges[0], |
| 839 |
.ranges = 2, |
| 840 |
.data = &init->vprechg_th_set, |
| 841 |
}, { |
| 842 |
.name = "charging termination current", |
| 843 |
.range = &charging_current_ranges[0], |
| 844 |
.ranges = 2, |
| 845 |
.data = &init->iterm_set, |
| 846 |
}, { |
| 847 |
.name = "charging re-start voltage", |
| 848 |
.range = &charge_voltage_regulation_ranges[0], |
| 849 |
.ranges = 2, |
| 850 |
.data = &init->vrechg_set, |
| 851 |
}, { |
| 852 |
.name = "battery overvoltage limit", |
| 853 |
.range = &charge_voltage_regulation_ranges[0], |
| 854 |
.ranges = 2, |
| 855 |
.data = &init->vbatovp_set, |
| 856 |
}, { |
| 857 |
.name = "fast-charging max current", |
| 858 |
.range = &fast_charge_current_ranges[0], |
| 859 |
.ranges = 1, |
| 860 |
.data = &init->ichg_set, |
| 861 |
}, { |
| 862 |
.name = "fast-charging voltage", |
| 863 |
.range = &charge_voltage_regulation_ranges[0], |
| 864 |
.ranges = 2, |
| 865 |
.data = &init->vfastchg_reg_set1, |
| 866 |
}, |
| 867 |
}; |
| 868 |
struct dt_init props[] = { |
| 869 |
{ |
| 870 |
.prop = "rohm,vsys-regulation-microvolt", |
| 871 |
.range = &vsys_voltage_regulation_ranges[0], |
| 872 |
.ranges = 2, |
| 873 |
.data = &init->vsysreg_set, |
| 874 |
}, { |
| 875 |
.prop = "rohm,vbus-input-current-limit-microamp", |
| 876 |
.range = &input_current_limit_ranges[0], |
| 877 |
.ranges = 1, |
| 878 |
.data = &init->ibus_lim_set, |
| 879 |
}, { |
| 880 |
.prop = "rohm,vcc-input-current-limit-microamp", |
| 881 |
.range = &input_current_limit_ranges[0], |
| 882 |
.ranges = 1, |
| 883 |
.data = &init->icc_lim_set, |
| 884 |
}, |
| 885 |
}; |
| 886 |
|
| 887 |
/* |
| 888 |
* The power_supply_get_battery_info() does not support getting values |
| 889 |
* from ACPI. Let's fix it if ACPI is required here. |
| 890 |
*/ |
| 891 |
ret = power_supply_get_battery_info(bd->charger, &info); |
| 892 |
if (ret < 0) |
| 893 |
return ret; |
| 894 |
|
| 895 |
/* Put pointers to the generic battery info */ |
| 896 |
battery_inits[0].info_data = &info->tricklecharge_current_ua; |
| 897 |
battery_inits[1].info_data = &info->precharge_current_ua; |
| 898 |
battery_inits[2].info_data = &info->precharge_voltage_max_uv; |
| 899 |
battery_inits[3].info_data = &info->charge_term_current_ua; |
| 900 |
battery_inits[4].info_data = &info->charge_restart_voltage_uv; |
| 901 |
battery_inits[5].info_data = &info->overvoltage_limit_uv; |
| 902 |
battery_inits[6].info_data = &info->constant_charge_current_max_ua; |
| 903 |
battery_inits[7].info_data = &info->constant_charge_voltage_max_uv; |
| 904 |
|
| 905 |
for (i = 0; i < ARRAY_SIZE(battery_inits); i++) { |
| 906 |
int val = *battery_inits[i].info_data; |
| 907 |
const struct linear_range *range = battery_inits[i].range; |
| 908 |
int ranges = battery_inits[i].ranges; |
| 909 |
|
| 910 |
if (val == -EINVAL) |
| 911 |
continue; |
| 912 |
|
| 913 |
ret = linear_range_get_selector_low_array(range, ranges, val, |
| 914 |
®val, &found); |
| 915 |
if (ret) { |
| 916 |
dev_err(bd->dev, "Unsupported value for %s\n", |
| 917 |
battery_inits[i].name); |
| 918 |
|
| 919 |
power_supply_put_battery_info(bd->charger, info); |
| 920 |
return -EINVAL; |
| 921 |
} |
| 922 |
if (!found) { |
| 923 |
dev_warn(bd->dev, |
| 924 |
"Unsupported value for %s - using smaller\n", |
| 925 |
battery_inits[i].name); |
| 926 |
} |
| 927 |
*(battery_inits[i].data) = regval; |
| 928 |
} |
| 929 |
|
| 930 |
power_supply_put_battery_info(bd->charger, info); |
| 931 |
|
| 932 |
for (i = 0; i < ARRAY_SIZE(props); i++) { |
| 933 |
ret = device_property_read_u32(bd->dev, props[i].prop, |
| 934 |
&property); |
| 935 |
if (ret < 0) { |
| 936 |
dev_err(bd->dev, "failed to read %s", props[i].prop); |
| 937 |
|
| 938 |
return ret; |
| 939 |
} |
| 940 |
|
| 941 |
ret = linear_range_get_selector_low_array(props[i].range, |
| 942 |
props[i].ranges, |
| 943 |
property, ®val, |
| 944 |
&found); |
| 945 |
if (ret) { |
| 946 |
dev_err(bd->dev, "Unsupported value for '%s'\n", |
| 947 |
props[i].prop); |
| 948 |
|
| 949 |
return -EINVAL; |
| 950 |
} |
| 951 |
|
| 952 |
if (!found) { |
| 953 |
dev_warn(bd->dev, |
| 954 |
"Unsupported value for '%s' - using smaller\n", |
| 955 |
props[i].prop); |
| 956 |
} |
| 957 |
|
| 958 |
*(props[i].data) = regval; |
| 959 |
} |
| 960 |
|
| 961 |
return 0; |
| 962 |
} |
| 963 |
|
| 964 |
static void bd9995x_chip_reset(void *bd) |
| 965 |
{ |
| 966 |
__bd9995x_chip_reset(bd); |
| 967 |
} |
| 968 |
|
| 969 |
static int bd9995x_probe(struct i2c_client *client) |
| 970 |
{ |
| 971 |
struct device *dev = &client->dev; |
| 972 |
struct bd9995x_device *bd; |
| 973 |
struct power_supply_config psy_cfg = {}; |
| 974 |
int ret; |
| 975 |
int i; |
| 976 |
|
| 977 |
bd = devm_kzalloc(dev, sizeof(*bd), GFP_KERNEL); |
| 978 |
if (!bd) |
| 979 |
return -ENOMEM; |
| 980 |
|
| 981 |
bd->client = client; |
| 982 |
bd->dev = dev; |
| 983 |
psy_cfg.drv_data = bd; |
| 984 |
psy_cfg.fwnode = dev_fwnode(dev); |
| 985 |
|
| 986 |
mutex_init(&bd->lock); |
| 987 |
|
| 988 |
bd->rmap = devm_regmap_init_i2c(client, &bd9995x_regmap_config); |
| 989 |
if (IS_ERR(bd->rmap)) { |
| 990 |
dev_err(dev, "Failed to setup register access via i2c\n"); |
| 991 |
return PTR_ERR(bd->rmap); |
| 992 |
} |
| 993 |
|
| 994 |
for (i = 0; i < ARRAY_SIZE(bd9995x_reg_fields); i++) { |
| 995 |
const struct reg_field *reg_fields = bd9995x_reg_fields; |
| 996 |
|
| 997 |
bd->rmap_fields[i] = devm_regmap_field_alloc(dev, bd->rmap, |
| 998 |
reg_fields[i]); |
| 999 |
if (IS_ERR(bd->rmap_fields[i])) { |
| 1000 |
dev_err(dev, "cannot allocate regmap field\n"); |
| 1001 |
return PTR_ERR(bd->rmap_fields[i]); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
i2c_set_clientdata(client, bd); |
| 1006 |
|
| 1007 |
ret = regmap_field_read(bd->rmap_fields[F_CHIP_ID], &bd->chip_id); |
| 1008 |
if (ret) { |
| 1009 |
dev_err(dev, "Cannot read chip ID.\n"); |
| 1010 |
return ret; |
| 1011 |
} |
| 1012 |
|
| 1013 |
if (bd->chip_id != BD99954_ID) { |
| 1014 |
dev_err(dev, "Chip with ID=0x%x, not supported!\n", |
| 1015 |
bd->chip_id); |
| 1016 |
return -ENODEV; |
| 1017 |
} |
| 1018 |
|
| 1019 |
ret = regmap_field_read(bd->rmap_fields[F_CHIP_REV], &bd->chip_rev); |
| 1020 |
if (ret) { |
| 1021 |
dev_err(dev, "Cannot read revision.\n"); |
| 1022 |
return ret; |
| 1023 |
} |
| 1024 |
|
| 1025 |
dev_info(bd->dev, "Found BD99954 chip rev %d\n", bd->chip_rev); |
| 1026 |
|
| 1027 |
/* |
| 1028 |
* We need to init the psy before we can call |
| 1029 |
* power_supply_get_battery_info() for it |
| 1030 |
*/ |
| 1031 |
bd->charger = devm_power_supply_register(bd->dev, |
| 1032 |
&bd9995x_power_supply_desc, |
| 1033 |
&psy_cfg); |
| 1034 |
if (IS_ERR(bd->charger)) { |
| 1035 |
dev_err(dev, "Failed to register power supply\n"); |
| 1036 |
return PTR_ERR(bd->charger); |
| 1037 |
} |
| 1038 |
|
| 1039 |
ret = bd9995x_fw_probe(bd); |
| 1040 |
if (ret < 0) { |
| 1041 |
dev_err(dev, "Cannot read device properties.\n"); |
| 1042 |
return ret; |
| 1043 |
} |
| 1044 |
|
| 1045 |
ret = bd9995x_hw_init(bd); |
| 1046 |
if (ret < 0) { |
| 1047 |
dev_err(dev, "Cannot initialize the chip.\n"); |
| 1048 |
return ret; |
| 1049 |
} |
| 1050 |
|
| 1051 |
ret = devm_add_action_or_reset(dev, bd9995x_chip_reset, bd); |
| 1052 |
if (ret) |
| 1053 |
return ret; |
| 1054 |
|
| 1055 |
return devm_request_threaded_irq(dev, client->irq, NULL, |
| 1056 |
bd9995x_irq_handler_thread, |
| 1057 |
IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 1058 |
BD9995X_IRQ_PIN, bd); |
| 1059 |
} |
| 1060 |
|
| 1061 |
static const struct of_device_id bd9995x_of_match[] = { |
| 1062 |
{ .compatible = "rohm,bd99954", }, |
| 1063 |
{ } |
| 1064 |
}; |
| 1065 |
MODULE_DEVICE_TABLE(of, bd9995x_of_match); |
| 1066 |
|
| 1067 |
static struct i2c_driver bd9995x_driver = { |
| 1068 |
.driver = { |
| 1069 |
.name = "bd9995x-charger", |
| 1070 |
.of_match_table = bd9995x_of_match, |
| 1071 |
}, |
| 1072 |
.probe = bd9995x_probe, |
| 1073 |
}; |
| 1074 |
module_i2c_driver(bd9995x_driver); |
| 1075 |
|
| 1076 |
MODULE_AUTHOR("Laine Markus <[email protected]>"); |
| 1077 |
MODULE_DESCRIPTION("ROHM BD99954 charger driver"); |
| 1078 |
MODULE_LICENSE("GPL"); |