| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
// |
| 3 |
// Fuel gauge driver for Maxim 17042 / 8966 / 8997 |
| 4 |
// Note that Maxim 8966 and 8997 are mfd and this is its subdevice. |
| 5 |
// |
| 6 |
// Copyright (C) 2011 Samsung Electronics |
| 7 |
// MyungJoo Ham <[email protected]> |
| 8 |
// |
| 9 |
// This driver is based on max17040_battery.c |
| 10 |
|
| 11 |
#include <linux/acpi.h> |
| 12 |
#include <linux/devm-helpers.h> |
| 13 |
#include <linux/init.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/slab.h> |
| 16 |
#include <linux/i2c.h> |
| 17 |
#include <linux/delay.h> |
| 18 |
#include <linux/interrupt.h> |
| 19 |
#include <linux/platform_device.h> |
| 20 |
#include <linux/pm.h> |
| 21 |
#include <linux/mod_devicetable.h> |
| 22 |
#include <linux/power_supply.h> |
| 23 |
#include <linux/power/max17042_battery.h> |
| 24 |
#include <linux/of.h> |
| 25 |
#include <linux/regmap.h> |
| 26 |
|
| 27 |
/* Status register bits */ |
| 28 |
#define STATUS_POR_BIT (1 << 1) |
| 29 |
#define STATUS_BST_BIT (1 << 3) |
| 30 |
#define STATUS_VMN_BIT (1 << 8) |
| 31 |
#define STATUS_TMN_BIT (1 << 9) |
| 32 |
#define STATUS_SMN_BIT (1 << 10) |
| 33 |
#define STATUS_BI_BIT (1 << 11) |
| 34 |
#define STATUS_VMX_BIT (1 << 12) |
| 35 |
#define STATUS_TMX_BIT (1 << 13) |
| 36 |
#define STATUS_SMX_BIT (1 << 14) |
| 37 |
#define STATUS_BR_BIT (1 << 15) |
| 38 |
|
| 39 |
/* Interrupt mask bits */ |
| 40 |
#define CFG_ALRT_BIT_ENBL (1 << 2) |
| 41 |
|
| 42 |
#define VFSOC0_LOCK 0x0000 |
| 43 |
#define VFSOC0_UNLOCK 0x0080 |
| 44 |
#define MODEL_UNLOCK1 0X0059 |
| 45 |
#define MODEL_UNLOCK2 0X00C4 |
| 46 |
#define MODEL_LOCK1 0X0000 |
| 47 |
#define MODEL_LOCK2 0X0000 |
| 48 |
|
| 49 |
#define dQ_ACC_DIV 0x4 |
| 50 |
#define dP_ACC_100 0x1900 |
| 51 |
#define dP_ACC_200 0x3200 |
| 52 |
|
| 53 |
#define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */ |
| 54 |
|
| 55 |
struct max17042_chip { |
| 56 |
struct device *dev; |
| 57 |
struct regmap *regmap; |
| 58 |
struct power_supply *battery; |
| 59 |
enum max170xx_chip_type chip_type; |
| 60 |
struct max17042_platform_data *pdata; |
| 61 |
struct work_struct work; |
| 62 |
int init_complete; |
| 63 |
int irq; |
| 64 |
}; |
| 65 |
|
| 66 |
static enum power_supply_property max17042_battery_props[] = { |
| 67 |
POWER_SUPPLY_PROP_STATUS, |
| 68 |
POWER_SUPPLY_PROP_PRESENT, |
| 69 |
POWER_SUPPLY_PROP_TECHNOLOGY, |
| 70 |
POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 71 |
POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 72 |
POWER_SUPPLY_PROP_VOLTAGE_MIN, |
| 73 |
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 74 |
POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 75 |
POWER_SUPPLY_PROP_VOLTAGE_AVG, |
| 76 |
POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 77 |
POWER_SUPPLY_PROP_CAPACITY, |
| 78 |
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 79 |
POWER_SUPPLY_PROP_CHARGE_FULL, |
| 80 |
POWER_SUPPLY_PROP_CHARGE_NOW, |
| 81 |
POWER_SUPPLY_PROP_CHARGE_COUNTER, |
| 82 |
POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, |
| 83 |
POWER_SUPPLY_PROP_TEMP, |
| 84 |
POWER_SUPPLY_PROP_TEMP_ALERT_MIN, |
| 85 |
POWER_SUPPLY_PROP_TEMP_ALERT_MAX, |
| 86 |
POWER_SUPPLY_PROP_TEMP_MIN, |
| 87 |
POWER_SUPPLY_PROP_TEMP_MAX, |
| 88 |
POWER_SUPPLY_PROP_HEALTH, |
| 89 |
POWER_SUPPLY_PROP_SCOPE, |
| 90 |
POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 91 |
// these two have to be at the end on the list |
| 92 |
POWER_SUPPLY_PROP_CURRENT_NOW, |
| 93 |
POWER_SUPPLY_PROP_CURRENT_AVG, |
| 94 |
}; |
| 95 |
|
| 96 |
static int max17042_get_temperature(struct max17042_chip *chip, int *temp) |
| 97 |
{ |
| 98 |
int ret; |
| 99 |
u32 data; |
| 100 |
struct regmap *map = chip->regmap; |
| 101 |
|
| 102 |
ret = regmap_read(map, MAX17042_TEMP, &data); |
| 103 |
if (ret < 0) |
| 104 |
return ret; |
| 105 |
|
| 106 |
*temp = sign_extend32(data, 15); |
| 107 |
/* The value is converted into deci-centigrade scale */ |
| 108 |
/* Units of LSB = 1 / 256 degree Celsius */ |
| 109 |
*temp = *temp * 10 / 256; |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static int max17042_get_status(struct max17042_chip *chip, int *status) |
| 114 |
{ |
| 115 |
int ret, charge_full, charge_now; |
| 116 |
int avg_current; |
| 117 |
u32 data; |
| 118 |
|
| 119 |
ret = power_supply_am_i_supplied(chip->battery); |
| 120 |
if (ret < 0) { |
| 121 |
*status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
if (ret == 0) { |
| 125 |
*status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* The MAX170xx has builtin end-of-charge detection and will update |
| 131 |
* FullCAP to match RepCap when it detects end of charging. |
| 132 |
* |
| 133 |
* When this cycle the battery gets charged to a higher (calculated) |
| 134 |
* capacity then the previous cycle then FullCAP will get updated |
| 135 |
* continuously once end-of-charge detection kicks in, so allow the |
| 136 |
* 2 to differ a bit. |
| 137 |
*/ |
| 138 |
|
| 139 |
ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full); |
| 140 |
if (ret < 0) |
| 141 |
return ret; |
| 142 |
|
| 143 |
ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now); |
| 144 |
if (ret < 0) |
| 145 |
return ret; |
| 146 |
|
| 147 |
if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) { |
| 148 |
*status = POWER_SUPPLY_STATUS_FULL; |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* Even though we are supplied, we may still be discharging if the |
| 154 |
* supply is e.g. only delivering 5V 0.5A. Check current if available. |
| 155 |
*/ |
| 156 |
if (!chip->pdata->enable_current_sense) { |
| 157 |
*status = POWER_SUPPLY_STATUS_CHARGING; |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
ret = regmap_read(chip->regmap, MAX17042_AvgCurrent, &data); |
| 162 |
if (ret < 0) |
| 163 |
return ret; |
| 164 |
|
| 165 |
avg_current = sign_extend32(data, 15); |
| 166 |
avg_current *= 1562500 / chip->pdata->r_sns; |
| 167 |
|
| 168 |
if (avg_current > 0) |
| 169 |
*status = POWER_SUPPLY_STATUS_CHARGING; |
| 170 |
else |
| 171 |
*status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
static int max17042_get_battery_health(struct max17042_chip *chip, int *health) |
| 177 |
{ |
| 178 |
int temp, vavg, vbatt, ret; |
| 179 |
u32 val; |
| 180 |
|
| 181 |
ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val); |
| 182 |
if (ret < 0) |
| 183 |
goto health_error; |
| 184 |
|
| 185 |
/* bits [0-3] unused */ |
| 186 |
vavg = val * 625 / 8; |
| 187 |
/* Convert to millivolts */ |
| 188 |
vavg /= 1000; |
| 189 |
|
| 190 |
ret = regmap_read(chip->regmap, MAX17042_VCELL, &val); |
| 191 |
if (ret < 0) |
| 192 |
goto health_error; |
| 193 |
|
| 194 |
/* bits [0-3] unused */ |
| 195 |
vbatt = val * 625 / 8; |
| 196 |
/* Convert to millivolts */ |
| 197 |
vbatt /= 1000; |
| 198 |
|
| 199 |
if (vavg < chip->pdata->vmin) { |
| 200 |
*health = POWER_SUPPLY_HEALTH_DEAD; |
| 201 |
goto out; |
| 202 |
} |
| 203 |
|
| 204 |
if (vbatt > size_add(chip->pdata->vmax, MAX17042_VMAX_TOLERANCE)) { |
| 205 |
*health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 206 |
goto out; |
| 207 |
} |
| 208 |
|
| 209 |
ret = max17042_get_temperature(chip, &temp); |
| 210 |
if (ret < 0) |
| 211 |
goto health_error; |
| 212 |
|
| 213 |
if (temp < chip->pdata->temp_min) { |
| 214 |
*health = POWER_SUPPLY_HEALTH_COLD; |
| 215 |
goto out; |
| 216 |
} |
| 217 |
|
| 218 |
if (temp > chip->pdata->temp_max) { |
| 219 |
*health = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 220 |
goto out; |
| 221 |
} |
| 222 |
|
| 223 |
*health = POWER_SUPPLY_HEALTH_GOOD; |
| 224 |
|
| 225 |
out: |
| 226 |
return 0; |
| 227 |
|
| 228 |
health_error: |
| 229 |
return ret; |
| 230 |
} |
| 231 |
|
| 232 |
static int max17042_get_property(struct power_supply *psy, |
| 233 |
enum power_supply_property psp, |
| 234 |
union power_supply_propval *val) |
| 235 |
{ |
| 236 |
struct max17042_chip *chip = power_supply_get_drvdata(psy); |
| 237 |
struct regmap *map = chip->regmap; |
| 238 |
int ret; |
| 239 |
u32 data; |
| 240 |
u64 data64; |
| 241 |
|
| 242 |
if (!chip->init_complete) |
| 243 |
return -EAGAIN; |
| 244 |
|
| 245 |
switch (psp) { |
| 246 |
case POWER_SUPPLY_PROP_STATUS: |
| 247 |
ret = max17042_get_status(chip, &val->intval); |
| 248 |
if (ret < 0) |
| 249 |
return ret; |
| 250 |
break; |
| 251 |
case POWER_SUPPLY_PROP_PRESENT: |
| 252 |
ret = regmap_read(map, MAX17042_STATUS, &data); |
| 253 |
if (ret < 0) |
| 254 |
return ret; |
| 255 |
|
| 256 |
if (data & MAX17042_STATUS_BattAbsent) |
| 257 |
val->intval = 0; |
| 258 |
else |
| 259 |
val->intval = 1; |
| 260 |
break; |
| 261 |
case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 262 |
val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 263 |
break; |
| 264 |
case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 265 |
ret = regmap_read(map, MAX17042_Cycles, &data); |
| 266 |
if (ret < 0) |
| 267 |
return ret; |
| 268 |
|
| 269 |
val->intval = data; |
| 270 |
break; |
| 271 |
case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| 272 |
ret = regmap_read(map, MAX17042_MinMaxVolt, &data); |
| 273 |
if (ret < 0) |
| 274 |
return ret; |
| 275 |
|
| 276 |
val->intval = data >> 8; |
| 277 |
val->intval *= 20000; /* Units of LSB = 20mV */ |
| 278 |
break; |
| 279 |
case POWER_SUPPLY_PROP_VOLTAGE_MIN: |
| 280 |
ret = regmap_read(map, MAX17042_MinMaxVolt, &data); |
| 281 |
if (ret < 0) |
| 282 |
return ret; |
| 283 |
|
| 284 |
val->intval = (data & 0xff) * 20000; /* Units of 20mV */ |
| 285 |
break; |
| 286 |
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
| 287 |
if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) |
| 288 |
ret = regmap_read(map, MAX17042_V_empty, &data); |
| 289 |
else |
| 290 |
ret = regmap_read(map, MAX17047_V_empty, &data); |
| 291 |
if (ret < 0) |
| 292 |
return ret; |
| 293 |
|
| 294 |
val->intval = data >> 7; |
| 295 |
val->intval *= 10000; /* Units of LSB = 10mV */ |
| 296 |
break; |
| 297 |
case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 298 |
ret = regmap_read(map, MAX17042_VCELL, &data); |
| 299 |
if (ret < 0) |
| 300 |
return ret; |
| 301 |
|
| 302 |
val->intval = data * 625 / 8; |
| 303 |
break; |
| 304 |
case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| 305 |
ret = regmap_read(map, MAX17042_AvgVCELL, &data); |
| 306 |
if (ret < 0) |
| 307 |
return ret; |
| 308 |
|
| 309 |
val->intval = data * 625 / 8; |
| 310 |
break; |
| 311 |
case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 312 |
ret = regmap_read(map, MAX17042_OCVInternal, &data); |
| 313 |
if (ret < 0) |
| 314 |
return ret; |
| 315 |
|
| 316 |
val->intval = data * 625 / 8; |
| 317 |
break; |
| 318 |
case POWER_SUPPLY_PROP_CAPACITY: |
| 319 |
if (chip->pdata->enable_current_sense) |
| 320 |
ret = regmap_read(map, MAX17042_RepSOC, &data); |
| 321 |
else |
| 322 |
ret = regmap_read(map, MAX17042_VFSOC, &data); |
| 323 |
if (ret < 0) |
| 324 |
return ret; |
| 325 |
|
| 326 |
val->intval = data >> 8; |
| 327 |
break; |
| 328 |
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 329 |
ret = regmap_read(map, MAX17042_DesignCap, &data); |
| 330 |
if (ret < 0) |
| 331 |
return ret; |
| 332 |
|
| 333 |
data64 = data * 5000000ll; |
| 334 |
do_div(data64, chip->pdata->r_sns); |
| 335 |
val->intval = data64; |
| 336 |
break; |
| 337 |
case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 338 |
ret = regmap_read(map, MAX17042_FullCAP, &data); |
| 339 |
if (ret < 0) |
| 340 |
return ret; |
| 341 |
|
| 342 |
data64 = data * 5000000ll; |
| 343 |
do_div(data64, chip->pdata->r_sns); |
| 344 |
val->intval = data64; |
| 345 |
break; |
| 346 |
case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 347 |
ret = regmap_read(map, MAX17042_RepCap, &data); |
| 348 |
if (ret < 0) |
| 349 |
return ret; |
| 350 |
|
| 351 |
data64 = data * 5000000ll; |
| 352 |
do_div(data64, chip->pdata->r_sns); |
| 353 |
val->intval = data64; |
| 354 |
break; |
| 355 |
case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
| 356 |
ret = regmap_read(map, MAX17042_QH, &data); |
| 357 |
if (ret < 0) |
| 358 |
return ret; |
| 359 |
|
| 360 |
data64 = sign_extend64(data, 15) * 5000000ll; |
| 361 |
val->intval = div_s64(data64, chip->pdata->r_sns); |
| 362 |
break; |
| 363 |
case POWER_SUPPLY_PROP_TEMP: |
| 364 |
ret = max17042_get_temperature(chip, &val->intval); |
| 365 |
if (ret < 0) |
| 366 |
return ret; |
| 367 |
break; |
| 368 |
case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 369 |
ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 370 |
if (ret < 0) |
| 371 |
return ret; |
| 372 |
/* LSB is Alert Minimum. In deci-centigrade */ |
| 373 |
val->intval = sign_extend32(data & 0xff, 7) * 10; |
| 374 |
break; |
| 375 |
case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 376 |
ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 377 |
if (ret < 0) |
| 378 |
return ret; |
| 379 |
/* MSB is Alert Maximum. In deci-centigrade */ |
| 380 |
val->intval = sign_extend32(data >> 8, 7) * 10; |
| 381 |
break; |
| 382 |
case POWER_SUPPLY_PROP_TEMP_MIN: |
| 383 |
val->intval = chip->pdata->temp_min; |
| 384 |
break; |
| 385 |
case POWER_SUPPLY_PROP_TEMP_MAX: |
| 386 |
val->intval = chip->pdata->temp_max; |
| 387 |
break; |
| 388 |
case POWER_SUPPLY_PROP_HEALTH: |
| 389 |
ret = max17042_get_battery_health(chip, &val->intval); |
| 390 |
if (ret < 0) |
| 391 |
return ret; |
| 392 |
break; |
| 393 |
case POWER_SUPPLY_PROP_SCOPE: |
| 394 |
val->intval = POWER_SUPPLY_SCOPE_SYSTEM; |
| 395 |
break; |
| 396 |
case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 397 |
if (chip->pdata->enable_current_sense) { |
| 398 |
ret = regmap_read(map, MAX17042_Current, &data); |
| 399 |
if (ret < 0) |
| 400 |
return ret; |
| 401 |
|
| 402 |
data64 = sign_extend64(data, 15) * 1562500ll; |
| 403 |
val->intval = div_s64(data64, chip->pdata->r_sns); |
| 404 |
} else { |
| 405 |
return -EINVAL; |
| 406 |
} |
| 407 |
break; |
| 408 |
case POWER_SUPPLY_PROP_CURRENT_AVG: |
| 409 |
if (chip->pdata->enable_current_sense) { |
| 410 |
ret = regmap_read(map, MAX17042_AvgCurrent, &data); |
| 411 |
if (ret < 0) |
| 412 |
return ret; |
| 413 |
|
| 414 |
data64 = sign_extend64(data, 15) * 1562500ll; |
| 415 |
val->intval = div_s64(data64, chip->pdata->r_sns); |
| 416 |
} else { |
| 417 |
return -EINVAL; |
| 418 |
} |
| 419 |
break; |
| 420 |
case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: |
| 421 |
ret = regmap_read(map, MAX17042_ICHGTerm, &data); |
| 422 |
if (ret < 0) |
| 423 |
return ret; |
| 424 |
|
| 425 |
data64 = data * 1562500ll; |
| 426 |
val->intval = div_s64(data64, chip->pdata->r_sns); |
| 427 |
break; |
| 428 |
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
| 429 |
ret = regmap_read(map, MAX17042_TTE, &data); |
| 430 |
if (ret < 0) |
| 431 |
return ret; |
| 432 |
|
| 433 |
val->intval = data * 5625 / 1000; |
| 434 |
break; |
| 435 |
default: |
| 436 |
return -EINVAL; |
| 437 |
} |
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
static int max17042_set_property(struct power_supply *psy, |
| 442 |
enum power_supply_property psp, |
| 443 |
const union power_supply_propval *val) |
| 444 |
{ |
| 445 |
struct max17042_chip *chip = power_supply_get_drvdata(psy); |
| 446 |
struct regmap *map = chip->regmap; |
| 447 |
int ret = 0; |
| 448 |
u32 data; |
| 449 |
int8_t temp; |
| 450 |
|
| 451 |
switch (psp) { |
| 452 |
case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 453 |
ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 454 |
if (ret < 0) |
| 455 |
return ret; |
| 456 |
|
| 457 |
/* Input in deci-centigrade, convert to centigrade */ |
| 458 |
temp = val->intval / 10; |
| 459 |
/* force min < max */ |
| 460 |
if (temp >= (int8_t)(data >> 8)) |
| 461 |
temp = (int8_t)(data >> 8) - 1; |
| 462 |
/* Write both MAX and MIN ALERT */ |
| 463 |
data = (data & 0xff00) + temp; |
| 464 |
ret = regmap_write(map, MAX17042_TALRT_Th, data); |
| 465 |
break; |
| 466 |
case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 467 |
ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 468 |
if (ret < 0) |
| 469 |
return ret; |
| 470 |
|
| 471 |
/* Input in Deci-Centigrade, convert to centigrade */ |
| 472 |
temp = val->intval / 10; |
| 473 |
/* force max > min */ |
| 474 |
if (temp <= (int8_t)(data & 0xff)) |
| 475 |
temp = (int8_t)(data & 0xff) + 1; |
| 476 |
/* Write both MAX and MIN ALERT */ |
| 477 |
data = (data & 0xff) + (temp << 8); |
| 478 |
ret = regmap_write(map, MAX17042_TALRT_Th, data); |
| 479 |
break; |
| 480 |
default: |
| 481 |
ret = -EINVAL; |
| 482 |
} |
| 483 |
|
| 484 |
return ret; |
| 485 |
} |
| 486 |
|
| 487 |
static int max17042_property_is_writeable(struct power_supply *psy, |
| 488 |
enum power_supply_property psp) |
| 489 |
{ |
| 490 |
int ret; |
| 491 |
|
| 492 |
switch (psp) { |
| 493 |
case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 494 |
case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 495 |
ret = 1; |
| 496 |
break; |
| 497 |
default: |
| 498 |
ret = 0; |
| 499 |
} |
| 500 |
|
| 501 |
return ret; |
| 502 |
} |
| 503 |
|
| 504 |
static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value) |
| 505 |
{ |
| 506 |
int retries = 8; |
| 507 |
int ret; |
| 508 |
u32 read_value; |
| 509 |
|
| 510 |
do { |
| 511 |
ret = regmap_write(map, reg, value); |
| 512 |
regmap_read(map, reg, &read_value); |
| 513 |
if (read_value != value) { |
| 514 |
ret = -EIO; |
| 515 |
retries--; |
| 516 |
} |
| 517 |
} while (retries && read_value != value); |
| 518 |
|
| 519 |
if (ret < 0) |
| 520 |
pr_err("%s: err %d\n", __func__, ret); |
| 521 |
|
| 522 |
return ret; |
| 523 |
} |
| 524 |
|
| 525 |
static inline void max17042_override_por(struct regmap *map, |
| 526 |
u8 reg, u16 value) |
| 527 |
{ |
| 528 |
if (value) |
| 529 |
regmap_write(map, reg, value); |
| 530 |
} |
| 531 |
|
| 532 |
static inline void max17042_unlock_model(struct max17042_chip *chip) |
| 533 |
{ |
| 534 |
struct regmap *map = chip->regmap; |
| 535 |
|
| 536 |
regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1); |
| 537 |
regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2); |
| 538 |
} |
| 539 |
|
| 540 |
static inline void max17042_lock_model(struct max17042_chip *chip) |
| 541 |
{ |
| 542 |
struct regmap *map = chip->regmap; |
| 543 |
|
| 544 |
regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1); |
| 545 |
regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2); |
| 546 |
} |
| 547 |
|
| 548 |
static inline void max17042_write_model_data(struct max17042_chip *chip, |
| 549 |
u8 addr, int size) |
| 550 |
{ |
| 551 |
struct regmap *map = chip->regmap; |
| 552 |
int i; |
| 553 |
|
| 554 |
for (i = 0; i < size; i++) |
| 555 |
regmap_write(map, addr + i, |
| 556 |
chip->pdata->config_data->cell_char_tbl[i]); |
| 557 |
} |
| 558 |
|
| 559 |
static inline void max17042_read_model_data(struct max17042_chip *chip, |
| 560 |
u8 addr, u16 *data, int size) |
| 561 |
{ |
| 562 |
struct regmap *map = chip->regmap; |
| 563 |
int i; |
| 564 |
u32 tmp; |
| 565 |
|
| 566 |
for (i = 0; i < size; i++) { |
| 567 |
regmap_read(map, addr + i, &tmp); |
| 568 |
data[i] = (u16)tmp; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
static inline int max17042_model_data_compare(struct max17042_chip *chip, |
| 573 |
u16 *data1, u16 *data2, int size) |
| 574 |
{ |
| 575 |
int i; |
| 576 |
|
| 577 |
if (memcmp(data1, data2, size)) { |
| 578 |
dev_err(chip->dev, "%s compare failed\n", __func__); |
| 579 |
for (i = 0; i < size; i++) |
| 580 |
dev_info(chip->dev, "0x%x, 0x%x", |
| 581 |
data1[i], data2[i]); |
| 582 |
dev_info(chip->dev, "\n"); |
| 583 |
return -EINVAL; |
| 584 |
} |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
static int max17042_init_model(struct max17042_chip *chip) |
| 589 |
{ |
| 590 |
int ret; |
| 591 |
int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); |
| 592 |
u16 *temp_data; |
| 593 |
|
| 594 |
temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); |
| 595 |
if (!temp_data) |
| 596 |
return -ENOMEM; |
| 597 |
|
| 598 |
max17042_unlock_model(chip); |
| 599 |
max17042_write_model_data(chip, MAX17042_MODELChrTbl, |
| 600 |
table_size); |
| 601 |
max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, |
| 602 |
table_size); |
| 603 |
|
| 604 |
ret = max17042_model_data_compare( |
| 605 |
chip, |
| 606 |
chip->pdata->config_data->cell_char_tbl, |
| 607 |
temp_data, |
| 608 |
table_size); |
| 609 |
|
| 610 |
max17042_lock_model(chip); |
| 611 |
kfree(temp_data); |
| 612 |
|
| 613 |
return ret; |
| 614 |
} |
| 615 |
|
| 616 |
static int max17042_verify_model_lock(struct max17042_chip *chip) |
| 617 |
{ |
| 618 |
int i; |
| 619 |
int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); |
| 620 |
u16 *temp_data; |
| 621 |
int ret = 0; |
| 622 |
|
| 623 |
temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); |
| 624 |
if (!temp_data) |
| 625 |
return -ENOMEM; |
| 626 |
|
| 627 |
max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, |
| 628 |
table_size); |
| 629 |
for (i = 0; i < table_size; i++) |
| 630 |
if (temp_data[i]) |
| 631 |
ret = -EINVAL; |
| 632 |
|
| 633 |
kfree(temp_data); |
| 634 |
return ret; |
| 635 |
} |
| 636 |
|
| 637 |
static void max17042_write_config_regs(struct max17042_chip *chip) |
| 638 |
{ |
| 639 |
struct max17042_config_data *config = chip->pdata->config_data; |
| 640 |
struct regmap *map = chip->regmap; |
| 641 |
|
| 642 |
regmap_write(map, MAX17042_CONFIG, config->config); |
| 643 |
regmap_write(map, MAX17042_LearnCFG, config->learn_cfg); |
| 644 |
regmap_write(map, MAX17042_FilterCFG, |
| 645 |
config->filter_cfg); |
| 646 |
regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg); |
| 647 |
if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 || |
| 648 |
chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050 || |
| 649 |
chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055) |
| 650 |
regmap_write(map, MAX17047_FullSOCThr, |
| 651 |
config->full_soc_thresh); |
| 652 |
} |
| 653 |
|
| 654 |
static void max17042_write_custom_regs(struct max17042_chip *chip) |
| 655 |
{ |
| 656 |
struct max17042_config_data *config = chip->pdata->config_data; |
| 657 |
struct regmap *map = chip->regmap; |
| 658 |
|
| 659 |
max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0); |
| 660 |
max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0); |
| 661 |
max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term); |
| 662 |
if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { |
| 663 |
regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco); |
| 664 |
max17042_write_verify_reg(map, MAX17042_K_empty0, |
| 665 |
config->kempty0); |
| 666 |
} else { |
| 667 |
max17042_write_verify_reg(map, MAX17047_QRTbl00, |
| 668 |
config->qrtbl00); |
| 669 |
max17042_write_verify_reg(map, MAX17047_QRTbl10, |
| 670 |
config->qrtbl10); |
| 671 |
max17042_write_verify_reg(map, MAX17047_QRTbl20, |
| 672 |
config->qrtbl20); |
| 673 |
max17042_write_verify_reg(map, MAX17047_QRTbl30, |
| 674 |
config->qrtbl30); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
static void max17042_update_capacity_regs(struct max17042_chip *chip) |
| 679 |
{ |
| 680 |
struct max17042_config_data *config = chip->pdata->config_data; |
| 681 |
struct regmap *map = chip->regmap; |
| 682 |
|
| 683 |
max17042_write_verify_reg(map, MAX17042_FullCAP, |
| 684 |
config->fullcap); |
| 685 |
regmap_write(map, MAX17042_DesignCap, config->design_cap); |
| 686 |
max17042_write_verify_reg(map, MAX17042_FullCAPNom, |
| 687 |
config->fullcapnom); |
| 688 |
} |
| 689 |
|
| 690 |
static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip) |
| 691 |
{ |
| 692 |
unsigned int vfSoc; |
| 693 |
struct regmap *map = chip->regmap; |
| 694 |
|
| 695 |
regmap_read(map, MAX17042_VFSOC, &vfSoc); |
| 696 |
regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK); |
| 697 |
max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc); |
| 698 |
regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK); |
| 699 |
} |
| 700 |
|
| 701 |
static void max17042_load_new_capacity_params(struct max17042_chip *chip) |
| 702 |
{ |
| 703 |
u32 full_cap0, rep_cap, dq_acc, vfSoc; |
| 704 |
u32 rem_cap; |
| 705 |
|
| 706 |
struct max17042_config_data *config = chip->pdata->config_data; |
| 707 |
struct regmap *map = chip->regmap; |
| 708 |
|
| 709 |
regmap_read(map, MAX17042_FullCAP0, &full_cap0); |
| 710 |
regmap_read(map, MAX17042_VFSOC, &vfSoc); |
| 711 |
|
| 712 |
/* fg_vfSoc needs to shifted by 8 bits to get the |
| 713 |
* perc in 1% accuracy, to get the right rem_cap multiply |
| 714 |
* full_cap0, fg_vfSoc and devide by 100 |
| 715 |
*/ |
| 716 |
rem_cap = ((vfSoc >> 8) * full_cap0) / 100; |
| 717 |
max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap); |
| 718 |
|
| 719 |
rep_cap = rem_cap; |
| 720 |
max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap); |
| 721 |
|
| 722 |
/* Write dQ_acc to 200% of Capacity and dP_acc to 200% */ |
| 723 |
dq_acc = config->fullcap / dQ_ACC_DIV; |
| 724 |
max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc); |
| 725 |
max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200); |
| 726 |
|
| 727 |
max17042_write_verify_reg(map, MAX17042_FullCAP, |
| 728 |
config->fullcap); |
| 729 |
regmap_write(map, MAX17042_DesignCap, |
| 730 |
config->design_cap); |
| 731 |
max17042_write_verify_reg(map, MAX17042_FullCAPNom, |
| 732 |
config->fullcapnom); |
| 733 |
/* Update SOC register with new SOC */ |
| 734 |
regmap_write(map, MAX17042_RepSOC, vfSoc); |
| 735 |
} |
| 736 |
|
| 737 |
/* |
| 738 |
* Block write all the override values coming from platform data. |
| 739 |
* This function MUST be called before the POR initialization procedure |
| 740 |
* specified by maxim. |
| 741 |
*/ |
| 742 |
static inline void max17042_override_por_values(struct max17042_chip *chip) |
| 743 |
{ |
| 744 |
struct regmap *map = chip->regmap; |
| 745 |
struct max17042_config_data *config = chip->pdata->config_data; |
| 746 |
|
| 747 |
max17042_override_por(map, MAX17042_TGAIN, config->tgain); |
| 748 |
max17042_override_por(map, MAX17042_TOFF, config->toff); |
| 749 |
max17042_override_por(map, MAX17042_CGAIN, config->cgain); |
| 750 |
max17042_override_por(map, MAX17042_COFF, config->coff); |
| 751 |
|
| 752 |
max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh); |
| 753 |
max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh); |
| 754 |
max17042_override_por(map, MAX17042_SALRT_Th, |
| 755 |
config->soc_alrt_thresh); |
| 756 |
max17042_override_por(map, MAX17042_CONFIG, config->config); |
| 757 |
max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer); |
| 758 |
|
| 759 |
max17042_override_por(map, MAX17042_DesignCap, config->design_cap); |
| 760 |
max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term); |
| 761 |
|
| 762 |
max17042_override_por(map, MAX17042_AtRate, config->at_rate); |
| 763 |
max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg); |
| 764 |
max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg); |
| 765 |
max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg); |
| 766 |
max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg); |
| 767 |
|
| 768 |
max17042_override_por(map, MAX17042_FullCAP, config->fullcap); |
| 769 |
max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom); |
| 770 |
max17042_override_por(map, MAX17042_dQacc, config->dqacc); |
| 771 |
max17042_override_por(map, MAX17042_dPacc, config->dpacc); |
| 772 |
|
| 773 |
max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0); |
| 774 |
max17042_override_por(map, MAX17042_TempCo, config->tcompc0); |
| 775 |
|
| 776 |
if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { |
| 777 |
max17042_override_por(map, MAX17042_MaskSOC, config->masksoc); |
| 778 |
max17042_override_por(map, MAX17042_SOC_empty, config->socempty); |
| 779 |
max17042_override_por(map, MAX17042_V_empty, config->vempty); |
| 780 |
max17042_override_por(map, MAX17042_EmptyTempCo, config->empty_tempco); |
| 781 |
max17042_override_por(map, MAX17042_K_empty0, config->kempty0); |
| 782 |
} |
| 783 |
|
| 784 |
if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) || |
| 785 |
(chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || |
| 786 |
(chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { |
| 787 |
max17042_override_por(map, MAX17042_IAvg_empty, config->iavg_empty); |
| 788 |
max17042_override_por(map, MAX17042_TempNom, config->temp_nom); |
| 789 |
max17042_override_por(map, MAX17042_TempLim, config->temp_lim); |
| 790 |
max17042_override_por(map, MAX17042_FCTC, config->fctc); |
| 791 |
} |
| 792 |
|
| 793 |
if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || |
| 794 |
(chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050) || |
| 795 |
(chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055)) { |
| 796 |
max17042_override_por(map, MAX17047_V_empty, config->vempty); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
static int max17042_init_chip(struct max17042_chip *chip) |
| 801 |
{ |
| 802 |
struct regmap *map = chip->regmap; |
| 803 |
int ret; |
| 804 |
|
| 805 |
max17042_override_por_values(chip); |
| 806 |
/* After Power up, the MAX17042 requires 500mS in order |
| 807 |
* to perform signal debouncing and initial SOC reporting |
| 808 |
*/ |
| 809 |
msleep(500); |
| 810 |
|
| 811 |
/* Initialize configuration */ |
| 812 |
max17042_write_config_regs(chip); |
| 813 |
|
| 814 |
/* write cell characterization data */ |
| 815 |
ret = max17042_init_model(chip); |
| 816 |
if (ret) { |
| 817 |
dev_err(chip->dev, "%s init failed\n", |
| 818 |
__func__); |
| 819 |
return -EIO; |
| 820 |
} |
| 821 |
|
| 822 |
ret = max17042_verify_model_lock(chip); |
| 823 |
if (ret) { |
| 824 |
dev_err(chip->dev, "%s lock verify failed\n", |
| 825 |
__func__); |
| 826 |
return -EIO; |
| 827 |
} |
| 828 |
/* write custom parameters */ |
| 829 |
max17042_write_custom_regs(chip); |
| 830 |
|
| 831 |
/* update capacity params */ |
| 832 |
max17042_update_capacity_regs(chip); |
| 833 |
|
| 834 |
/* delay must be atleast 350mS to allow VFSOC |
| 835 |
* to be calculated from the new configuration |
| 836 |
*/ |
| 837 |
msleep(350); |
| 838 |
|
| 839 |
/* reset vfsoc0 reg */ |
| 840 |
max17042_reset_vfsoc0_reg(chip); |
| 841 |
|
| 842 |
/* load new capacity params */ |
| 843 |
max17042_load_new_capacity_params(chip); |
| 844 |
|
| 845 |
/* Init complete, Clear the POR bit */ |
| 846 |
regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0); |
| 847 |
return 0; |
| 848 |
} |
| 849 |
|
| 850 |
static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off) |
| 851 |
{ |
| 852 |
struct regmap *map = chip->regmap; |
| 853 |
u32 soc, soc_tr; |
| 854 |
|
| 855 |
/* program interrupt thresholds such that we should |
| 856 |
* get interrupt for every 'off' perc change in the soc |
| 857 |
*/ |
| 858 |
if (chip->pdata->enable_current_sense) |
| 859 |
regmap_read(map, MAX17042_RepSOC, &soc); |
| 860 |
else |
| 861 |
regmap_read(map, MAX17042_VFSOC, &soc); |
| 862 |
soc >>= 8; |
| 863 |
soc_tr = (soc + off) << 8; |
| 864 |
if (off < soc) |
| 865 |
soc_tr |= soc - off; |
| 866 |
regmap_write(map, MAX17042_SALRT_Th, soc_tr); |
| 867 |
} |
| 868 |
|
| 869 |
static irqreturn_t max17042_thread_handler(int id, void *dev) |
| 870 |
{ |
| 871 |
struct max17042_chip *chip = dev; |
| 872 |
u32 val; |
| 873 |
int ret; |
| 874 |
|
| 875 |
ret = regmap_read(chip->regmap, MAX17042_STATUS, &val); |
| 876 |
if (ret) |
| 877 |
return IRQ_HANDLED; |
| 878 |
|
| 879 |
if ((val & STATUS_SMN_BIT) || (val & STATUS_SMX_BIT)) { |
| 880 |
dev_dbg(chip->dev, "SOC threshold INTR\n"); |
| 881 |
max17042_set_soc_threshold(chip, 1); |
| 882 |
} |
| 883 |
|
| 884 |
/* we implicitly handle all alerts via power_supply_changed */ |
| 885 |
regmap_clear_bits(chip->regmap, MAX17042_STATUS, |
| 886 |
0xFFFF & ~(STATUS_POR_BIT | STATUS_BST_BIT)); |
| 887 |
|
| 888 |
power_supply_changed(chip->battery); |
| 889 |
return IRQ_HANDLED; |
| 890 |
} |
| 891 |
|
| 892 |
static void max17042_init_worker(struct work_struct *work) |
| 893 |
{ |
| 894 |
struct max17042_chip *chip = container_of(work, |
| 895 |
struct max17042_chip, work); |
| 896 |
int ret; |
| 897 |
|
| 898 |
/* Initialize registers according to values from the platform data */ |
| 899 |
if (chip->pdata->enable_por_init && chip->pdata->config_data) { |
| 900 |
ret = max17042_init_chip(chip); |
| 901 |
if (ret) |
| 902 |
return; |
| 903 |
} |
| 904 |
|
| 905 |
chip->init_complete = 1; |
| 906 |
} |
| 907 |
|
| 908 |
#ifdef CONFIG_OF |
| 909 |
static struct max17042_platform_data * |
| 910 |
max17042_get_of_pdata(struct max17042_chip *chip) |
| 911 |
{ |
| 912 |
struct device *dev = chip->dev; |
| 913 |
struct device_node *np = dev->of_node; |
| 914 |
u32 prop; |
| 915 |
struct max17042_platform_data *pdata; |
| 916 |
|
| 917 |
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 918 |
if (!pdata) |
| 919 |
return NULL; |
| 920 |
|
| 921 |
/* |
| 922 |
* Require current sense resistor value to be specified for |
| 923 |
* current-sense functionality to be enabled at all. |
| 924 |
*/ |
| 925 |
if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) { |
| 926 |
pdata->r_sns = prop; |
| 927 |
pdata->enable_current_sense = true; |
| 928 |
} |
| 929 |
|
| 930 |
if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min)) |
| 931 |
pdata->temp_min = INT_MIN; |
| 932 |
if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max)) |
| 933 |
pdata->temp_max = INT_MAX; |
| 934 |
if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin)) |
| 935 |
pdata->vmin = INT_MIN; |
| 936 |
if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax)) |
| 937 |
pdata->vmax = INT_MAX; |
| 938 |
|
| 939 |
return pdata; |
| 940 |
} |
| 941 |
#endif |
| 942 |
|
| 943 |
static struct max17042_reg_data max17047_default_pdata_init_regs[] = { |
| 944 |
/* |
| 945 |
* Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection |
| 946 |
* when the voltage FG reports 95%, as recommended in the datasheet. |
| 947 |
*/ |
| 948 |
{ MAX17047_FullSOCThr, MAX17042_BATTERY_FULL << 8 }, |
| 949 |
}; |
| 950 |
|
| 951 |
static struct max17042_platform_data * |
| 952 |
max17042_get_default_pdata(struct max17042_chip *chip) |
| 953 |
{ |
| 954 |
struct device *dev = chip->dev; |
| 955 |
struct max17042_platform_data *pdata; |
| 956 |
int ret, misc_cfg; |
| 957 |
|
| 958 |
/* |
| 959 |
* The MAX17047 gets used on x86 where we might not have pdata, assume |
| 960 |
* the firmware will already have initialized the fuel-gauge and provide |
| 961 |
* default values for the non init bits to make things work. |
| 962 |
*/ |
| 963 |
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 964 |
if (!pdata) |
| 965 |
return pdata; |
| 966 |
|
| 967 |
if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || |
| 968 |
(chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { |
| 969 |
pdata->init_data = max17047_default_pdata_init_regs; |
| 970 |
pdata->num_init_data = |
| 971 |
ARRAY_SIZE(max17047_default_pdata_init_regs); |
| 972 |
} |
| 973 |
|
| 974 |
ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg); |
| 975 |
if (ret < 0) |
| 976 |
return NULL; |
| 977 |
|
| 978 |
/* If bits 0-1 are set to 3 then only Voltage readings are used */ |
| 979 |
if ((misc_cfg & 0x3) == 0x3) |
| 980 |
pdata->enable_current_sense = false; |
| 981 |
else |
| 982 |
pdata->enable_current_sense = true; |
| 983 |
|
| 984 |
pdata->vmin = MAX17042_DEFAULT_VMIN; |
| 985 |
pdata->vmax = MAX17042_DEFAULT_VMAX; |
| 986 |
pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN; |
| 987 |
pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX; |
| 988 |
|
| 989 |
return pdata; |
| 990 |
} |
| 991 |
|
| 992 |
static struct max17042_platform_data * |
| 993 |
max17042_get_pdata(struct max17042_chip *chip) |
| 994 |
{ |
| 995 |
struct device *dev = chip->dev; |
| 996 |
|
| 997 |
#ifdef CONFIG_OF |
| 998 |
if (dev->of_node) |
| 999 |
return max17042_get_of_pdata(chip); |
| 1000 |
#endif |
| 1001 |
if (dev->platform_data) |
| 1002 |
return dev->platform_data; |
| 1003 |
|
| 1004 |
return max17042_get_default_pdata(chip); |
| 1005 |
} |
| 1006 |
|
| 1007 |
static const struct regmap_config max17042_regmap_config = { |
| 1008 |
.name = "max17042", |
| 1009 |
.reg_bits = 8, |
| 1010 |
.val_bits = 16, |
| 1011 |
.val_format_endian = REGMAP_ENDIAN_NATIVE, |
| 1012 |
}; |
| 1013 |
|
| 1014 |
static const struct power_supply_desc max17042_psy_desc = { |
| 1015 |
.name = "max170xx_battery", |
| 1016 |
.type = POWER_SUPPLY_TYPE_BATTERY, |
| 1017 |
.get_property = max17042_get_property, |
| 1018 |
.set_property = max17042_set_property, |
| 1019 |
.property_is_writeable = max17042_property_is_writeable, |
| 1020 |
.external_power_changed = power_supply_changed, |
| 1021 |
.properties = max17042_battery_props, |
| 1022 |
.num_properties = ARRAY_SIZE(max17042_battery_props), |
| 1023 |
}; |
| 1024 |
|
| 1025 |
static const struct power_supply_desc max17042_no_current_sense_psy_desc = { |
| 1026 |
.name = "max170xx_battery", |
| 1027 |
.type = POWER_SUPPLY_TYPE_BATTERY, |
| 1028 |
.get_property = max17042_get_property, |
| 1029 |
.set_property = max17042_set_property, |
| 1030 |
.property_is_writeable = max17042_property_is_writeable, |
| 1031 |
.properties = max17042_battery_props, |
| 1032 |
.num_properties = ARRAY_SIZE(max17042_battery_props) - 2, |
| 1033 |
}; |
| 1034 |
|
| 1035 |
static int max17042_probe(struct i2c_client *client, struct device *dev, int irq, |
| 1036 |
enum max170xx_chip_type chip_type) |
| 1037 |
{ |
| 1038 |
struct i2c_adapter *adapter = client->adapter; |
| 1039 |
const struct power_supply_desc *max17042_desc = &max17042_psy_desc; |
| 1040 |
struct power_supply_config psy_cfg = {}; |
| 1041 |
struct max17042_chip *chip; |
| 1042 |
int ret; |
| 1043 |
int i; |
| 1044 |
u32 val; |
| 1045 |
|
| 1046 |
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
| 1047 |
return -EIO; |
| 1048 |
|
| 1049 |
chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); |
| 1050 |
if (!chip) |
| 1051 |
return -ENOMEM; |
| 1052 |
|
| 1053 |
chip->dev = dev; |
| 1054 |
chip->chip_type = chip_type; |
| 1055 |
chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config); |
| 1056 |
if (IS_ERR(chip->regmap)) { |
| 1057 |
dev_err(dev, "Failed to initialize regmap\n"); |
| 1058 |
return -EINVAL; |
| 1059 |
} |
| 1060 |
|
| 1061 |
chip->pdata = max17042_get_pdata(chip); |
| 1062 |
if (!chip->pdata) { |
| 1063 |
dev_err(dev, "no platform data provided\n"); |
| 1064 |
return -EINVAL; |
| 1065 |
} |
| 1066 |
|
| 1067 |
dev_set_drvdata(dev, chip); |
| 1068 |
psy_cfg.drv_data = chip; |
| 1069 |
psy_cfg.fwnode = dev_fwnode(dev); |
| 1070 |
|
| 1071 |
/* When current is not measured, |
| 1072 |
* CURRENT_NOW and CURRENT_AVG properties should be invisible. */ |
| 1073 |
if (!chip->pdata->enable_current_sense) |
| 1074 |
max17042_desc = &max17042_no_current_sense_psy_desc; |
| 1075 |
|
| 1076 |
if (chip->pdata->r_sns == 0) |
| 1077 |
chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; |
| 1078 |
|
| 1079 |
if (chip->pdata->init_data) |
| 1080 |
for (i = 0; i < chip->pdata->num_init_data; i++) |
| 1081 |
regmap_write(chip->regmap, |
| 1082 |
chip->pdata->init_data[i].addr, |
| 1083 |
chip->pdata->init_data[i].data); |
| 1084 |
|
| 1085 |
if (!chip->pdata->enable_current_sense) { |
| 1086 |
regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000); |
| 1087 |
regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003); |
| 1088 |
regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007); |
| 1089 |
} |
| 1090 |
|
| 1091 |
chip->battery = devm_power_supply_register(dev, max17042_desc, |
| 1092 |
&psy_cfg); |
| 1093 |
if (IS_ERR(chip->battery)) { |
| 1094 |
dev_err(dev, "failed: power supply register\n"); |
| 1095 |
return PTR_ERR(chip->battery); |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (irq) { |
| 1099 |
unsigned int flags = IRQF_ONESHOT | IRQF_SHARED | IRQF_PROBE_SHARED; |
| 1100 |
|
| 1101 |
ret = devm_request_threaded_irq(dev, irq, |
| 1102 |
NULL, |
| 1103 |
max17042_thread_handler, flags, |
| 1104 |
chip->battery->desc->name, |
| 1105 |
chip); |
| 1106 |
if (!ret) { |
| 1107 |
regmap_update_bits(chip->regmap, MAX17042_CONFIG, |
| 1108 |
CFG_ALRT_BIT_ENBL, |
| 1109 |
CFG_ALRT_BIT_ENBL); |
| 1110 |
max17042_set_soc_threshold(chip, 1); |
| 1111 |
} else { |
| 1112 |
irq = 0; |
| 1113 |
if (ret != -EBUSY) |
| 1114 |
dev_err(dev, "Failed to get IRQ\n"); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
/* Not able to update the charge threshold when exceeded? -> disable */ |
| 1118 |
if (!irq) |
| 1119 |
regmap_write(chip->regmap, MAX17042_SALRT_Th, 0xff00); |
| 1120 |
|
| 1121 |
chip->irq = irq; |
| 1122 |
|
| 1123 |
regmap_read(chip->regmap, MAX17042_STATUS, &val); |
| 1124 |
if (val & STATUS_POR_BIT) { |
| 1125 |
ret = devm_work_autocancel(dev, &chip->work, |
| 1126 |
max17042_init_worker); |
| 1127 |
if (ret) |
| 1128 |
return ret; |
| 1129 |
schedule_work(&chip->work); |
| 1130 |
} else { |
| 1131 |
chip->init_complete = 1; |
| 1132 |
} |
| 1133 |
|
| 1134 |
return 0; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static int max17042_i2c_probe(struct i2c_client *client) |
| 1138 |
{ |
| 1139 |
const struct i2c_device_id *id = i2c_client_get_device_id(client); |
| 1140 |
const struct acpi_device_id *acpi_id = NULL; |
| 1141 |
struct device *dev = &client->dev; |
| 1142 |
enum max170xx_chip_type chip_type; |
| 1143 |
|
| 1144 |
if (id) { |
| 1145 |
chip_type = id->driver_data; |
| 1146 |
} else { |
| 1147 |
acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); |
| 1148 |
if (!acpi_id) |
| 1149 |
return -ENODEV; |
| 1150 |
|
| 1151 |
chip_type = acpi_id->driver_data; |
| 1152 |
} |
| 1153 |
|
| 1154 |
return max17042_probe(client, dev, client->irq, chip_type); |
| 1155 |
} |
| 1156 |
|
| 1157 |
static int max17042_platform_probe(struct platform_device *pdev) |
| 1158 |
{ |
| 1159 |
struct device *dev = &pdev->dev; |
| 1160 |
struct i2c_client *i2c; |
| 1161 |
const struct platform_device_id *id; |
| 1162 |
int irq; |
| 1163 |
|
| 1164 |
i2c = to_i2c_client(pdev->dev.parent); |
| 1165 |
if (!i2c) |
| 1166 |
return -EINVAL; |
| 1167 |
|
| 1168 |
dev->of_node = dev->parent->of_node; |
| 1169 |
id = platform_get_device_id(pdev); |
| 1170 |
irq = platform_get_irq(pdev, 0); |
| 1171 |
|
| 1172 |
return max17042_probe(i2c, dev, irq, id->driver_data); |
| 1173 |
} |
| 1174 |
|
| 1175 |
#ifdef CONFIG_PM_SLEEP |
| 1176 |
static int max17042_suspend(struct device *dev) |
| 1177 |
{ |
| 1178 |
struct max17042_chip *chip = dev_get_drvdata(dev); |
| 1179 |
|
| 1180 |
/* |
| 1181 |
* disable the irq and enable irq_wake |
| 1182 |
* capability to the interrupt line. |
| 1183 |
*/ |
| 1184 |
if (chip->irq) { |
| 1185 |
disable_irq(chip->irq); |
| 1186 |
enable_irq_wake(chip->irq); |
| 1187 |
} |
| 1188 |
|
| 1189 |
return 0; |
| 1190 |
} |
| 1191 |
|
| 1192 |
static int max17042_resume(struct device *dev) |
| 1193 |
{ |
| 1194 |
struct max17042_chip *chip = dev_get_drvdata(dev); |
| 1195 |
|
| 1196 |
if (chip->irq) { |
| 1197 |
disable_irq_wake(chip->irq); |
| 1198 |
enable_irq(chip->irq); |
| 1199 |
/* re-program the SOC thresholds to 1% change */ |
| 1200 |
max17042_set_soc_threshold(chip, 1); |
| 1201 |
} |
| 1202 |
|
| 1203 |
return 0; |
| 1204 |
} |
| 1205 |
#endif |
| 1206 |
|
| 1207 |
static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend, |
| 1208 |
max17042_resume); |
| 1209 |
|
| 1210 |
#ifdef CONFIG_ACPI |
| 1211 |
static const struct acpi_device_id max17042_acpi_match[] = { |
| 1212 |
{ "MAX17047", MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1213 |
{ } |
| 1214 |
}; |
| 1215 |
MODULE_DEVICE_TABLE(acpi, max17042_acpi_match); |
| 1216 |
#endif |
| 1217 |
|
| 1218 |
#ifdef CONFIG_OF |
| 1219 |
/* |
| 1220 |
* Device may be instantiated through parent MFD device and device matching is done |
| 1221 |
* through platform_device_id. |
| 1222 |
* |
| 1223 |
* However if device's DT node contains proper clock compatible and driver is |
| 1224 |
* built as a module, then the *module* matching will be done trough DT aliases. |
| 1225 |
* This requires of_device_id table. In the same time this will not change the |
| 1226 |
* actual *device* matching so do not add .of_match_table. |
| 1227 |
*/ |
| 1228 |
static const struct of_device_id max17042_dt_match[] __used = { |
| 1229 |
{ .compatible = "maxim,max17042", |
| 1230 |
.data = (void *) MAXIM_DEVICE_TYPE_MAX17042 }, |
| 1231 |
{ .compatible = "maxim,max17047", |
| 1232 |
.data = (void *) MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1233 |
{ .compatible = "maxim,max17050", |
| 1234 |
.data = (void *) MAXIM_DEVICE_TYPE_MAX17050 }, |
| 1235 |
{ .compatible = "maxim,max17055", |
| 1236 |
.data = (void *) MAXIM_DEVICE_TYPE_MAX17055 }, |
| 1237 |
{ .compatible = "maxim,max77705-battery", |
| 1238 |
.data = (void *) MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1239 |
{ .compatible = "maxim,max77849-battery", |
| 1240 |
.data = (void *) MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1241 |
{ }, |
| 1242 |
}; |
| 1243 |
MODULE_DEVICE_TABLE(of, max17042_dt_match); |
| 1244 |
#endif |
| 1245 |
|
| 1246 |
static const struct i2c_device_id max17042_id[] = { |
| 1247 |
{ "max17042", MAXIM_DEVICE_TYPE_MAX17042 }, |
| 1248 |
{ "max17047", MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1249 |
{ "max17050", MAXIM_DEVICE_TYPE_MAX17050 }, |
| 1250 |
{ "max17055", MAXIM_DEVICE_TYPE_MAX17055 }, |
| 1251 |
{ "max77849-battery", MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1252 |
{ } |
| 1253 |
}; |
| 1254 |
MODULE_DEVICE_TABLE(i2c, max17042_id); |
| 1255 |
|
| 1256 |
static const struct platform_device_id max17042_platform_id[] = { |
| 1257 |
{ "max17042", MAXIM_DEVICE_TYPE_MAX17042 }, |
| 1258 |
{ "max17047", MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1259 |
{ "max17050", MAXIM_DEVICE_TYPE_MAX17050 }, |
| 1260 |
{ "max17055", MAXIM_DEVICE_TYPE_MAX17055 }, |
| 1261 |
{ "max77705-battery", MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1262 |
{ "max77849-battery", MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1263 |
{ } |
| 1264 |
}; |
| 1265 |
MODULE_DEVICE_TABLE(platform, max17042_platform_id); |
| 1266 |
|
| 1267 |
static struct i2c_driver max17042_i2c_driver = { |
| 1268 |
.driver = { |
| 1269 |
.name = "max17042", |
| 1270 |
.acpi_match_table = ACPI_PTR(max17042_acpi_match), |
| 1271 |
.of_match_table = of_match_ptr(max17042_dt_match), |
| 1272 |
.pm = &max17042_pm_ops, |
| 1273 |
}, |
| 1274 |
.probe = max17042_i2c_probe, |
| 1275 |
.id_table = max17042_id, |
| 1276 |
}; |
| 1277 |
|
| 1278 |
static struct platform_driver max17042_platform_driver = { |
| 1279 |
.driver = { |
| 1280 |
.name = "max17042", |
| 1281 |
.acpi_match_table = ACPI_PTR(max17042_acpi_match), |
| 1282 |
.pm = &max17042_pm_ops, |
| 1283 |
}, |
| 1284 |
.probe = max17042_platform_probe, |
| 1285 |
.id_table = max17042_platform_id, |
| 1286 |
}; |
| 1287 |
|
| 1288 |
static int __init max17042_init(void) |
| 1289 |
{ |
| 1290 |
int ret; |
| 1291 |
|
| 1292 |
ret = platform_driver_register(&max17042_platform_driver); |
| 1293 |
if (ret) |
| 1294 |
return ret; |
| 1295 |
|
| 1296 |
ret = i2c_add_driver(&max17042_i2c_driver); |
| 1297 |
if (ret) { |
| 1298 |
platform_driver_unregister(&max17042_platform_driver); |
| 1299 |
return ret; |
| 1300 |
} |
| 1301 |
|
| 1302 |
return 0; |
| 1303 |
} |
| 1304 |
module_init(max17042_init); |
| 1305 |
|
| 1306 |
static void __exit max17042_exit(void) |
| 1307 |
{ |
| 1308 |
i2c_del_driver(&max17042_i2c_driver); |
| 1309 |
platform_driver_unregister(&max17042_platform_driver); |
| 1310 |
} |
| 1311 |
module_exit(max17042_exit); |
| 1312 |
|
| 1313 |
MODULE_AUTHOR("MyungJoo Ham <[email protected]>"); |
| 1314 |
MODULE_DESCRIPTION("MAX17042 Fuel Gauge"); |
| 1315 |
MODULE_LICENSE("GPL"); |