File Index
Files and directories
Sign in
FILE

bd99954-charger.c

File preview with syntax highlighting.

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